VertexBuffer.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 "GPUObject.h"
  25. #include "GraphicsDefs.h"
  26. #include "SharedArrayPtr.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. /// Release default pool resources
  37. virtual void OnDeviceLost();
  38. /// ReCreate default pool resources
  39. virtual void OnDeviceReset();
  40. /// Release 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
  61. void ClearDataLost();
  62. /// Return whether is dynamic
  63. bool IsDynamic() const;
  64. /// Return whether default pool data lost
  65. bool IsDataLost() const { return dataLost_; }
  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 buffer hash for state sorting
  83. unsigned long long GetHash(unsigned streaindex_, unsigned useMask);
  84. /// Return vertex size corresponding to a vertex element mask
  85. static unsigned GetVertexSize(unsigned elementMask);
  86. /// Vertex element sizes
  87. static const unsigned elementSize[];
  88. /// Vertex element names
  89. static const std::string elementName[];
  90. private:
  91. /// Update offsets of vertex elements
  92. void UpdateOffsets();
  93. /// Create buffer
  94. bool Create();
  95. /// Fallback data when operating with a null graphics
  96. SharedArrayPtr<unsigned char> fallbackData_;
  97. /// Morph vertex range reset data
  98. SharedArrayPtr<unsigned char> morphRangeResetData_;
  99. /// Memory pool
  100. unsigned pool_;
  101. /// Usage type
  102. unsigned usage_;
  103. /// Number of vertices
  104. unsigned vertexCount_;
  105. /// Vertex size
  106. unsigned vertexSize_;
  107. /// Vertex element bitmask
  108. unsigned elementMask_;
  109. /// Vertex element offsets
  110. unsigned elementOffset_[MAX_VERTEX_ELEMENTS];
  111. /// Morph vertex range start
  112. unsigned morphRangeStart_;
  113. /// Number of vertices in the morph range
  114. unsigned morphRangeCount_;
  115. /// Locked flag
  116. bool locked_;
  117. /// Default pool data lost flag
  118. bool dataLost_;
  119. };