VertexBuffer.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. // This file contains VertexBuffer code common to all graphics APIs.
  23. #include "../Precompiled.h"
  24. #include "../Graphics/Graphics.h"
  25. #include "../Graphics/VertexBuffer.h"
  26. #include "../Math/MathDefs.h"
  27. #include "../DebugNew.h"
  28. namespace Atomic
  29. {
  30. VertexBuffer::VertexBuffer(Context* context, bool forceHeadless) :
  31. Object(context),
  32. GPUObject(forceHeadless ? (Graphics*)0 : GetSubsystem<Graphics>()),
  33. vertexCount_(0),
  34. elementMask_(0),
  35. lockState_(LOCK_NONE),
  36. lockStart_(0),
  37. lockCount_(0),
  38. lockScratchData_(0),
  39. shadowed_(false),
  40. dynamic_(false),
  41. discardLock_(false)
  42. {
  43. UpdateOffsets();
  44. // Force shadowing mode if graphics subsystem does not exist
  45. if (!graphics_)
  46. shadowed_ = true;
  47. }
  48. VertexBuffer::~VertexBuffer()
  49. {
  50. Release();
  51. }
  52. void VertexBuffer::SetShadowed(bool enable)
  53. {
  54. // If no graphics subsystem, can not disable shadowing
  55. if (!graphics_)
  56. enable = true;
  57. if (enable != shadowed_)
  58. {
  59. if (enable && vertexSize_ && vertexCount_)
  60. shadowData_ = new unsigned char[vertexCount_ * vertexSize_];
  61. else
  62. shadowData_.Reset();
  63. shadowed_ = enable;
  64. }
  65. }
  66. bool VertexBuffer::SetSize(unsigned vertexCount, unsigned elementMask, bool dynamic)
  67. {
  68. return SetSize(vertexCount, GetElements(elementMask), dynamic);
  69. }
  70. bool VertexBuffer::SetSize(unsigned vertexCount, const PODVector<VertexElement>& elements, bool dynamic)
  71. {
  72. Unlock();
  73. vertexCount_ = vertexCount;
  74. elements_ = elements;
  75. dynamic_ = dynamic;
  76. UpdateOffsets();
  77. if (shadowed_ && vertexCount_ && vertexSize_)
  78. shadowData_ = new unsigned char[vertexCount_ * vertexSize_];
  79. else
  80. shadowData_.Reset();
  81. return Create();
  82. }
  83. void VertexBuffer::UpdateOffsets()
  84. {
  85. unsigned elementOffset = 0;
  86. elementHash_ = 0;
  87. elementMask_ = 0;
  88. for (PODVector<VertexElement>::Iterator i = elements_.Begin(); i != elements_.End(); ++i)
  89. {
  90. i->offset_ = elementOffset;
  91. elementOffset += ELEMENT_TYPESIZES[i->type_];
  92. elementHash_ <<= 6;
  93. elementHash_ += (((int)i->type_ + 1) * ((int)i->semantic_ + 1) + i->index_);
  94. for (unsigned j = 0; j < MAX_LEGACY_VERTEX_ELEMENTS; ++j)
  95. {
  96. const VertexElement& legacy = LEGACY_VERTEXELEMENTS[j];
  97. if (i->type_ == legacy.type_ && i->semantic_ == legacy.semantic_ && i->index_ == legacy.index_)
  98. elementMask_ |= (1 << j);
  99. }
  100. }
  101. vertexSize_ = elementOffset;
  102. }
  103. const VertexElement* VertexBuffer::GetElement(VertexElementSemantic semantic, unsigned char index) const
  104. {
  105. for (PODVector<VertexElement>::ConstIterator i = elements_.Begin(); i != elements_.End(); ++i)
  106. {
  107. if (i->semantic_ == semantic && i->index_ == index)
  108. return &(*i);
  109. }
  110. return 0;
  111. }
  112. const VertexElement* VertexBuffer::GetElement(VertexElementType type, VertexElementSemantic semantic, unsigned char index) const
  113. {
  114. for (PODVector<VertexElement>::ConstIterator i = elements_.Begin(); i != elements_.End(); ++i)
  115. {
  116. if (i->type_ == type && i->semantic_ == semantic && i->index_ == index)
  117. return &(*i);
  118. }
  119. return 0;
  120. }
  121. const VertexElement* VertexBuffer::GetElement(const PODVector<VertexElement>& elements, VertexElementType type, VertexElementSemantic semantic, unsigned char index)
  122. {
  123. for (PODVector<VertexElement>::ConstIterator i = elements.Begin(); i != elements.End(); ++i)
  124. {
  125. if (i->type_ == type && i->semantic_ == semantic && i->index_ == index)
  126. return &(*i);
  127. }
  128. return 0;
  129. }
  130. bool VertexBuffer::HasElement(const PODVector<VertexElement>& elements, VertexElementType type, VertexElementSemantic semantic, unsigned char index)
  131. {
  132. return GetElement(elements, type, semantic, index) != 0;
  133. }
  134. unsigned VertexBuffer::GetElementOffset(const PODVector<VertexElement>& elements, VertexElementType type, VertexElementSemantic semantic, unsigned char index)
  135. {
  136. const VertexElement* element = GetElement(elements, type, semantic, index);
  137. return element ? element->offset_ : M_MAX_UNSIGNED;
  138. }
  139. PODVector<VertexElement> VertexBuffer::GetElements(unsigned elementMask)
  140. {
  141. PODVector<VertexElement> ret;
  142. for (unsigned i = 0; i < MAX_LEGACY_VERTEX_ELEMENTS; ++i)
  143. {
  144. if (elementMask & (1 << i))
  145. ret.Push(LEGACY_VERTEXELEMENTS[i]);
  146. }
  147. return ret;
  148. }
  149. unsigned VertexBuffer::GetVertexSize(const PODVector<VertexElement>& elements)
  150. {
  151. unsigned size = 0;
  152. for (unsigned i = 0; i < elements.Size(); ++i)
  153. size += ELEMENT_TYPESIZES[elements[i].type_];
  154. return size;
  155. }
  156. unsigned VertexBuffer::GetVertexSize(unsigned elementMask)
  157. {
  158. unsigned size = 0;
  159. for (unsigned i = 0; i < MAX_LEGACY_VERTEX_ELEMENTS; ++i)
  160. {
  161. if (elementMask & (1 << i))
  162. size += ELEMENT_TYPESIZES[LEGACY_VERTEXELEMENTS[i].type_];
  163. }
  164. return size;
  165. }
  166. void VertexBuffer::UpdateOffsets(PODVector<VertexElement>& elements)
  167. {
  168. unsigned elementOffset = 0;
  169. for (PODVector<VertexElement>::Iterator i = elements.Begin(); i != elements.End(); ++i)
  170. {
  171. i->offset_ = elementOffset;
  172. elementOffset += ELEMENT_TYPESIZES[i->type_];
  173. }
  174. }
  175. }