VertexBuffer.cpp 6.1 KB

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