IndexBuffer.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Container/ArrayPtr.h"
  5. #include "../Core/Object.h"
  6. #include "../GraphicsAPI/GPUObject.h"
  7. #include "../GraphicsAPI/GraphicsDefs.h"
  8. namespace Urho3D
  9. {
  10. /// Hardware index buffer.
  11. class URHO3D_API IndexBuffer : public Object, public GPUObject
  12. {
  13. URHO3D_OBJECT(IndexBuffer, Object);
  14. public:
  15. /// Construct. Optionally force headless (no GPU-side buffer) operation.
  16. explicit IndexBuffer(Context* context, bool forceHeadless = false);
  17. /// Destruct.
  18. ~IndexBuffer() override;
  19. /// Mark the buffer destroyed on graphics context destruction. May be a no-op depending on the API.
  20. void OnDeviceLost() override;
  21. /// Recreate the buffer and restore data if applicable. May be a no-op depending on the API.
  22. void OnDeviceReset() override;
  23. /// Release buffer.
  24. void Release() override;
  25. /// Enable shadowing in CPU memory. Shadowing is forced on if the graphics subsystem does not exist.
  26. /// @property
  27. void SetShadowed(bool enable);
  28. /// Set size and vertex elements and dynamic mode. Previous data will be lost.
  29. bool SetSize(i32 indexCount, bool largeIndices, bool dynamic = false);
  30. /// Set all data in the buffer.
  31. bool SetData(const void* data);
  32. /// Set a data range in the buffer. Optionally discard data outside the range.
  33. bool SetDataRange(const void* data, i32 start, i32 count, bool discard = false);
  34. /// Lock the buffer for write-only editing. Return data pointer if successful. Optionally discard data outside the range.
  35. void* Lock(i32 start, i32 count, bool discard = false);
  36. /// Unlock the buffer and apply changes to the GPU buffer.
  37. void Unlock();
  38. /// Return whether CPU memory shadowing is enabled.
  39. /// @property
  40. bool IsShadowed() const { return shadowed_; }
  41. /// Return whether is dynamic.
  42. /// @property
  43. bool IsDynamic() const { return dynamic_; }
  44. /// Return whether is currently locked.
  45. bool IsLocked() const { return lockState_ != LOCK_NONE; }
  46. /// Return number of indices.
  47. /// @property
  48. i32 GetIndexCount() const { return indexCount_; }
  49. /// Return index size in bytes.
  50. /// @property
  51. i32 GetIndexSize() const { return indexSize_; }
  52. /// Return used vertex range from index range.
  53. bool GetUsedVertexRange(i32 start, i32 count, i32& minVertex, i32& vertexCount);
  54. /// Return CPU memory shadow data.
  55. byte* GetShadowData() const { return shadowData_.Get(); }
  56. /// Return shared array pointer to the CPU memory shadow data.
  57. SharedArrayPtr<byte> GetShadowDataShared() const { return shadowData_; }
  58. private:
  59. /// Create buffer.
  60. bool Create();
  61. /// Update the shadow data to the GPU buffer.
  62. bool UpdateToGPU();
  63. /// Map the GPU buffer into CPU memory. Not used on OpenGL.
  64. void* MapBuffer(i32 start, i32 count, bool discard);
  65. /// Unmap the GPU buffer. Not used on OpenGL.
  66. void UnmapBuffer();
  67. #ifdef URHO3D_OPENGL
  68. void OnDeviceLost_OGL();
  69. void OnDeviceReset_OGL();
  70. void Release_OGL();
  71. bool SetData_OGL(const void* data);
  72. bool SetDataRange_OGL(const void* data, i32 start, i32 count, bool discard = false);
  73. void* Lock_OGL(i32 start, i32 count, bool discard);
  74. void Unlock_OGL();
  75. bool Create_OGL();
  76. bool UpdateToGPU_OGL();
  77. void* MapBuffer_OGL(i32 start, i32 count, bool discard);
  78. void UnmapBuffer_OGL();
  79. #endif // def URHO3D_OPENGL
  80. #ifdef URHO3D_D3D11
  81. void OnDeviceLost_D3D11();
  82. void OnDeviceReset_D3D11();
  83. void Release_D3D11();
  84. bool SetData_D3D11(const void* data);
  85. bool SetDataRange_D3D11(const void* data, i32 start, i32 count, bool discard = false);
  86. void* Lock_D3D11(i32 start, i32 count, bool discard);
  87. void Unlock_D3D11();
  88. bool Create_D3D11();
  89. bool UpdateToGPU_D3D11();
  90. void* MapBuffer_D3D11(i32 start, i32 count, bool discard);
  91. void UnmapBuffer_D3D11();
  92. #endif // def URHO3D_D3D11
  93. /// Shadow data.
  94. SharedArrayPtr<byte> shadowData_;
  95. /// Number of indices.
  96. i32 indexCount_;
  97. /// Index size.
  98. i32 indexSize_;
  99. /// Buffer locking state.
  100. LockState lockState_;
  101. /// Lock start vertex.
  102. i32 lockStart_;
  103. /// Lock number of vertices.
  104. i32 lockCount_;
  105. /// Scratch buffer for fallback locking.
  106. void* lockScratchData_;
  107. /// Dynamic flag.
  108. bool dynamic_;
  109. /// Shadowed flag.
  110. bool shadowed_;
  111. /// Discard lock flag. Used by OpenGL only.
  112. bool discardLock_;
  113. };
  114. }