VertexBuffer.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Container/ArrayPtr.h"
  24. #include "../Core/Object.h"
  25. #include "../Graphics/GPUObject.h"
  26. #include "../Graphics/GraphicsDefs.h"
  27. namespace Atomic
  28. {
  29. /// Hardware vertex buffer.
  30. class ATOMIC_API VertexBuffer : public Object, public GPUObject
  31. {
  32. ATOMIC_OBJECT(VertexBuffer, Object);
  33. public:
  34. /// Construct. Optionally force headless (no GPU-side buffer) operation.
  35. VertexBuffer(Context* context, bool forceHeadless = false);
  36. /// Destruct.
  37. virtual ~VertexBuffer();
  38. /// Mark the buffer destroyed on graphics context destruction. May be a no-op depending on the API.
  39. virtual void OnDeviceLost();
  40. /// Recreate the buffer and restore data if applicable. May be a no-op depending on the API.
  41. virtual void OnDeviceReset();
  42. /// Release buffer.
  43. virtual void Release();
  44. /// Enable shadowing in CPU memory. Shadowing is forced on if the graphics subsystem does not exist.
  45. void SetShadowed(bool enable);
  46. /// Set size, vertex elements and dynamic mode. Previous data will be lost.
  47. bool SetSize(unsigned vertexCount, const PODVector<VertexElement>& elements, bool dynamic = false);
  48. /// Set size and vertex elements and dynamic mode using legacy element bitmask. Previous data will be lost.
  49. bool SetSize(unsigned vertexCount, unsigned elementMask, bool dynamic = false);
  50. /// Set all data in the buffer.
  51. bool SetData(const void* data);
  52. /// Set a data range in the buffer. Optionally discard data outside the range.
  53. bool SetDataRange(const void* data, unsigned start, unsigned count, bool discard = false);
  54. /// Lock the buffer for write-only editing. Return data pointer if successful. Optionally discard data outside the range.
  55. void* Lock(unsigned start, unsigned count, bool discard = false);
  56. /// Unlock the buffer and apply changes to the GPU buffer.
  57. void Unlock();
  58. /// Return whether CPU memory shadowing is enabled.
  59. bool IsShadowed() const { return shadowed_; }
  60. /// Return whether is dynamic.
  61. bool IsDynamic() const { return dynamic_; }
  62. /// Return whether is currently locked.
  63. bool IsLocked() const { return lockState_ != LOCK_NONE; }
  64. /// Return number of vertices.
  65. unsigned GetVertexCount() const { return vertexCount_; }
  66. /// Return vertex size in bytes.
  67. unsigned GetVertexSize() const { return vertexSize_; }
  68. /// Return vertex elements.
  69. const PODVector<VertexElement>& GetElements() const { return elements_; }
  70. /// Return vertex element, or null if does not exist.
  71. const VertexElement* GetElement(VertexElementSemantic semantic, unsigned char index = 0) const;
  72. /// Return vertex element with specific type, or null if does not exist.
  73. const VertexElement* GetElement(VertexElementType type, VertexElementSemantic semantic, unsigned char index = 0) const;
  74. /// Return whether has a specified element semantic.
  75. bool HasElement(VertexElementSemantic semantic, unsigned char index = 0) const { return GetElement(semantic, index) != 0; }
  76. /// Return whether has an element semantic with specific type.
  77. bool HasElement(VertexElementType type, VertexElementSemantic semantic, unsigned char index = 0) const { return GetElement(type, semantic, index) != 0; }
  78. /// Return offset of a element within vertex, or M_MAX_UNSIGNED if does not exist.
  79. unsigned GetElementOffset(VertexElementSemantic semantic, unsigned char index = 0) const { const VertexElement* element = GetElement(semantic, index); return element ? element->offset_ : M_MAX_UNSIGNED; }
  80. /// Return offset of a element with specific type within vertex, or M_MAX_UNSIGNED if element does not exist.
  81. unsigned GetElementOffset(VertexElementType type, VertexElementSemantic semantic, unsigned char index = 0) const { const VertexElement* element = GetElement(type, semantic, index); return element ? element->offset_ : M_MAX_UNSIGNED; }
  82. /// Return legacy vertex element mask. Note that both semantic and type must match the legacy element for a mask bit to be set.
  83. unsigned GetElementMask() const { return elementMask_; }
  84. /// Return CPU memory shadow data.
  85. unsigned char* GetShadowData() const { return shadowData_.Get(); }
  86. /// Return shared array pointer to the CPU memory shadow data.
  87. SharedArrayPtr<unsigned char> GetShadowDataShared() const { return shadowData_; }
  88. /// Return buffer hash for building vertex declarations. Used internally.
  89. unsigned long long GetBufferHash(unsigned streamIndex) { return elementHash_ << (streamIndex * 16); }
  90. /// Return element with specified type and semantic from a vertex element list, or null if does not exist.
  91. static const VertexElement* GetElement(const PODVector<VertexElement>& elements, VertexElementType type, VertexElementSemantic semantic, unsigned char index = 0);
  92. /// Return whether element list has a specified element type and semantic.
  93. static bool HasElement(const PODVector<VertexElement>& elements, VertexElementType type, VertexElementSemantic semantic, unsigned char index = 0);
  94. /// Return element offset for specified type and semantic from a vertex element list, or M_MAX_UNSIGNED if does not exist.
  95. static unsigned GetElementOffset(const PODVector<VertexElement>& elements, VertexElementType type, VertexElementSemantic semantic, unsigned char index = 0);
  96. /// Return a vertex element list from a legacy element bitmask.
  97. static PODVector<VertexElement> GetElements(unsigned elementMask);
  98. /// Return vertex size from an element list.
  99. static unsigned GetVertexSize(const PODVector<VertexElement>& elements);
  100. /// Return vertex size for a legacy vertex element bitmask.
  101. static unsigned GetVertexSize(unsigned elementMask);
  102. /// Update offsets of vertex elements.
  103. static void UpdateOffsets(PODVector<VertexElement>& elements);
  104. private:
  105. /// Update offsets of vertex elements.
  106. void UpdateOffsets();
  107. /// Create buffer.
  108. bool Create();
  109. /// Update the shadow data to the GPU buffer.
  110. bool UpdateToGPU();
  111. /// Map the GPU buffer into CPU memory. Not used on OpenGL.
  112. void* MapBuffer(unsigned start, unsigned count, bool discard);
  113. /// Unmap the GPU buffer. Not used on OpenGL.
  114. void UnmapBuffer();
  115. /// Shadow data.
  116. SharedArrayPtr<unsigned char> shadowData_;
  117. /// Number of vertices.
  118. unsigned vertexCount_;
  119. /// Vertex size.
  120. unsigned vertexSize_;
  121. /// Vertex elements.
  122. PODVector<VertexElement> elements_;
  123. /// Vertex element hash.
  124. unsigned long long elementHash_;
  125. /// Vertex element legacy bitmask.
  126. unsigned elementMask_;
  127. /// Buffer locking state.
  128. LockState lockState_;
  129. /// Lock start vertex.
  130. unsigned lockStart_;
  131. /// Lock number of vertices.
  132. unsigned lockCount_;
  133. /// Scratch buffer for fallback locking.
  134. void* lockScratchData_;
  135. /// Dynamic flag.
  136. bool dynamic_;
  137. /// Shadowed flag.
  138. bool shadowed_;
  139. /// Discard lock flag. Used by OpenGL only.
  140. bool discardLock_;
  141. };
  142. }