OGLVertexBuffer.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "ArrayPtr.h"
  25. #include "GPUObject.h"
  26. #include "GraphicsDefs.h"
  27. /// Hardware vertex buffer.
  28. class VertexBuffer : public Object, public GPUObject
  29. {
  30. OBJECT(VertexBuffer);
  31. public:
  32. /// Construct.
  33. VertexBuffer(Context* context);
  34. /// Destruct.
  35. virtual ~VertexBuffer();
  36. /// Save data and release the buffer.
  37. virtual void OnDeviceLost();
  38. /// Recreate the buffer from saved data.
  39. virtual void OnDeviceReset();
  40. /// Release the buffer.
  41. virtual void Release();
  42. /// Set size and vertex elements and dynamic mode. Previous data will be lost.
  43. bool SetSize(unsigned vertexCount, unsigned elementMask, bool dynamic = false);
  44. /// Set all data in the buffer.
  45. bool SetData(const void* data);
  46. /// Set a data range in the buffer.
  47. bool SetDataRange(const void* data, unsigned first, unsigned count);
  48. /// Set the vertex range to use for morphing.
  49. bool SetMorphRange(unsigned start, unsigned count);
  50. /// Set data to be used for resetting the morph vertex range.
  51. void SetMorphRangeResetData(const SharedArrayPtr<unsigned char>& data);
  52. /// Lock a data range in the buffer. Return pointer to locked data if successful.
  53. void* Lock(unsigned start, unsigned count, LockMode mode);
  54. /// Unlock buffer.
  55. void Unlock();
  56. /// Lock the morph vertex range. Return pointer to locked data if successful.
  57. void* LockMorphRange();
  58. /// Reset the morph vertex range. Needs to be locked first.
  59. void ResetMorphRange(void* lockedMorphRange);
  60. /// Clear data lost flag. No-op on OpenGL.
  61. void ClearDataLost();
  62. /// Return whether is dynamic.
  63. bool IsDynamic() const { return dynamic_; }
  64. /// Return whether data is lost. Always false on OpenGL.
  65. bool IsDataLost() const { return false; }
  66. /// Return number of vertices.
  67. unsigned GetVertexCount() const {return vertexCount_; }
  68. /// Return vertex size.
  69. unsigned GetVertexSize() const { return vertexSize_; }
  70. /// Return bitmask of vertex elements.
  71. unsigned GetElementMask() const { return elementMask_; }
  72. /// Return offset of a specified element within a vertex.
  73. unsigned GetElementOffset(VertexElement element) const { return elementOffset_[element]; }
  74. /// Return morph vertex range start.
  75. unsigned GetMorphRangeStart() const { return morphRangeStart_; }
  76. /// Return number of vertices in the morph range.
  77. unsigned GetMorphRangeCount() const { return morphRangeCount_; }
  78. /// Return morph vertex range reset data.
  79. const SharedArrayPtr<unsigned char>& GetMorphRangeResetData() { return morphRangeResetData_; }
  80. /// Return whether has a morph vertex range defined.
  81. bool HasMorphRange() const { return morphRangeCount_ > 0; }
  82. /// Return vertex size corresponding to a vertex element mask.
  83. static unsigned GetVertexSize(unsigned elementMask);
  84. /// Vertex element sizes in bytes.
  85. static const unsigned elementSize[];
  86. /// Vertex element OpenGL types.
  87. static const unsigned elementType[];
  88. /// Vertex element OpenGL component counts.
  89. static const unsigned elementComponents[];
  90. /// Vertex element OpenGL normalization.
  91. static const unsigned elementNormalize[];
  92. /// Vertex element names.
  93. static const String elementName[];
  94. private:
  95. /// Update offsets of vertex elements.
  96. void UpdateOffsets();
  97. /// Create buffer.
  98. bool Create();
  99. /// Fallback data when operating with a null graphics subsystem.
  100. SharedArrayPtr<unsigned char> fallbackData_;
  101. /// Morph vertex range reset data.
  102. SharedArrayPtr<unsigned char> morphRangeResetData_;
  103. /// Save data when OpenGL context needs to be destroyed and recreated.
  104. SharedArrayPtr<unsigned char> saveData_;
  105. /// Memory area for discard locking.
  106. void* discardLockData_;
  107. /// Number of vertices.
  108. unsigned vertexCount_;
  109. /// Vertex size.
  110. unsigned vertexSize_;
  111. /// Vertex element bitmask.
  112. unsigned elementMask_;
  113. /// Vertex element offsets.
  114. unsigned elementOffset_[MAX_VERTEX_ELEMENTS];
  115. /// Morph vertex range start.
  116. unsigned morphRangeStart_;
  117. /// Number of vertices in the morph range.
  118. unsigned morphRangeCount_;
  119. /// Discard lock range start.
  120. unsigned discardLockStart_;
  121. /// Discard lock vertex count.
  122. unsigned discardLockCount_;
  123. /// Dynamic flag.
  124. bool dynamic_;
  125. /// Locked flag.
  126. bool locked_;
  127. };