VertexBuffer.cpp 5.9 KB

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