D3D9VertexDeclaration.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. #include "../../Precompiled.h"
  23. #include "../../Graphics/Graphics.h"
  24. #include "../../Graphics/GraphicsImpl.h"
  25. #include "../../Graphics/VertexBuffer.h"
  26. #include "../../Graphics/VertexDeclaration.h"
  27. #include "../../IO/Log.h"
  28. #include "../../DebugNew.h"
  29. namespace Atomic
  30. {
  31. const BYTE d3dElementType[] =
  32. {
  33. D3DDECLTYPE_UNUSED, // Int (not supported by D3D9)
  34. D3DDECLTYPE_FLOAT1, // Float
  35. D3DDECLTYPE_FLOAT2, // Vector2
  36. D3DDECLTYPE_FLOAT3, // Vector3
  37. D3DDECLTYPE_FLOAT4, // Vector4
  38. D3DDECLTYPE_UBYTE4, // 4 bytes, not normalized
  39. D3DDECLTYPE_UBYTE4N // 4 bytes, normalized
  40. };
  41. const BYTE d3dElementUsage[] =
  42. {
  43. D3DDECLUSAGE_POSITION,
  44. D3DDECLUSAGE_NORMAL,
  45. D3DDECLUSAGE_BINORMAL,
  46. D3DDECLUSAGE_TANGENT,
  47. D3DDECLUSAGE_TEXCOORD,
  48. D3DDECLUSAGE_COLOR,
  49. D3DDECLUSAGE_BLENDWEIGHT,
  50. D3DDECLUSAGE_BLENDINDICES,
  51. D3DDECLUSAGE_TEXCOORD // Object index (not supported by D3D9)
  52. };
  53. VertexDeclaration::VertexDeclaration(Graphics* graphics, const PODVector<VertexElement>& srcElements) :
  54. declaration_(0)
  55. {
  56. PODVector<VertexDeclarationElement> elements;
  57. for (unsigned i = 0; i < srcElements.Size(); ++i)
  58. {
  59. const VertexElement& srcElement = srcElements[i];
  60. if (srcElement.semantic_ == SEM_OBJECTINDEX)
  61. {
  62. ATOMIC_LOGWARNING("Object index attribute is not supported on Direct3D9 and will be ignored");
  63. continue;
  64. }
  65. VertexDeclarationElement element;
  66. element.semantic_ = srcElement.semantic_;
  67. element.type_ = srcElement.type_;
  68. element.index_ = srcElement.index_;
  69. element.streamIndex_ = 0;
  70. element.offset_ = srcElement.offset_;
  71. elements.Push(element);
  72. }
  73. Create(graphics, elements);
  74. }
  75. VertexDeclaration::VertexDeclaration(Graphics* graphics, const PODVector<VertexBuffer*>& buffers) :
  76. declaration_(0)
  77. {
  78. PODVector<VertexDeclarationElement> elements;
  79. unsigned prevBufferElements = 0;
  80. for (unsigned i = 0; i < buffers.Size(); ++i)
  81. {
  82. if (!buffers[i])
  83. continue;
  84. const PODVector<VertexElement>& srcElements = buffers[i]->GetElements();
  85. bool isExisting = false;
  86. for (unsigned j = 0; j < srcElements.Size(); ++j)
  87. {
  88. const VertexElement& srcElement = srcElements[j];
  89. if (srcElement.semantic_ == SEM_OBJECTINDEX)
  90. {
  91. ATOMIC_LOGWARNING("Object index attribute is not supported on Direct3D9 and will be ignored");
  92. continue;
  93. }
  94. // Override existing element if necessary
  95. for (unsigned k = 0; k < prevBufferElements; ++k)
  96. {
  97. if (elements[k].semantic_ == srcElement.semantic_ && elements[k].index_ == srcElement.index_)
  98. {
  99. isExisting = true;
  100. elements[k].streamIndex_ = i;
  101. elements[k].offset_ = srcElement.offset_;
  102. break;
  103. }
  104. }
  105. if (isExisting)
  106. continue;
  107. VertexDeclarationElement element;
  108. element.semantic_ = srcElement.semantic_;
  109. element.type_ = srcElement.type_;
  110. element.index_ = srcElement.index_;
  111. element.streamIndex_ = i;
  112. element.offset_ = srcElement.offset_;
  113. elements.Push(element);
  114. }
  115. prevBufferElements = elements.Size();
  116. }
  117. Create(graphics, elements);
  118. }
  119. VertexDeclaration::VertexDeclaration(Graphics* graphics, const Vector<SharedPtr<VertexBuffer> >& buffers) :
  120. declaration_(0)
  121. {
  122. PODVector<VertexDeclarationElement> elements;
  123. unsigned prevBufferElements = 0;
  124. for (unsigned i = 0; i < buffers.Size(); ++i)
  125. {
  126. if (!buffers[i])
  127. continue;
  128. const PODVector<VertexElement>& srcElements = buffers[i]->GetElements();
  129. bool isExisting = false;
  130. for (unsigned j = 0; j < srcElements.Size(); ++j)
  131. {
  132. const VertexElement& srcElement = srcElements[j];
  133. if (srcElement.semantic_ == SEM_OBJECTINDEX)
  134. {
  135. ATOMIC_LOGWARNING("Object index attribute is not supported on Direct3D9 and will be ignored");
  136. continue;
  137. }
  138. // Override existing element if necessary
  139. for (unsigned k = 0; k < prevBufferElements; ++k)
  140. {
  141. if (elements[k].semantic_ == srcElement.semantic_ && elements[k].index_ == srcElement.index_)
  142. {
  143. isExisting = true;
  144. elements[k].streamIndex_ = i;
  145. elements[k].offset_ = srcElement.offset_;
  146. break;
  147. }
  148. }
  149. if (isExisting)
  150. continue;
  151. VertexDeclarationElement element;
  152. element.semantic_ = srcElement.semantic_;
  153. element.type_ = srcElement.type_;
  154. element.index_ = srcElement.index_;
  155. element.streamIndex_ = i;
  156. element.offset_ = srcElement.offset_;
  157. elements.Push(element);
  158. }
  159. prevBufferElements = elements.Size();
  160. }
  161. Create(graphics, elements);
  162. }
  163. VertexDeclaration::~VertexDeclaration()
  164. {
  165. Release();
  166. }
  167. void VertexDeclaration::Create(Graphics* graphics, const PODVector<VertexDeclarationElement>& elements)
  168. {
  169. SharedArrayPtr<D3DVERTEXELEMENT9> elementArray(new D3DVERTEXELEMENT9[elements.Size() + 1]);
  170. D3DVERTEXELEMENT9* dest = elementArray;
  171. for (Vector<VertexDeclarationElement>::ConstIterator i = elements.Begin(); i != elements.End(); ++i)
  172. {
  173. dest->Stream = (WORD)i->streamIndex_;
  174. dest->Offset = (WORD)i->offset_;
  175. dest->Type = d3dElementType[i->type_];
  176. dest->Method = D3DDECLMETHOD_DEFAULT;
  177. dest->Usage = d3dElementUsage[i->semantic_];
  178. dest->UsageIndex = i->index_;
  179. dest++;
  180. }
  181. dest->Stream = 0xff;
  182. dest->Offset = 0;
  183. dest->Type = D3DDECLTYPE_UNUSED;
  184. dest->Method = 0;
  185. dest->Usage = 0;
  186. dest->UsageIndex = 0;
  187. IDirect3DDevice9* device = graphics->GetImpl()->GetDevice();
  188. HRESULT hr = device->CreateVertexDeclaration(elementArray, &declaration_);
  189. if (FAILED(hr))
  190. {
  191. ATOMIC_SAFE_RELEASE(declaration_);
  192. ATOMIC_LOGD3DERROR("Failed to create vertex declaration", hr);
  193. }
  194. }
  195. void VertexDeclaration::Release()
  196. {
  197. ATOMIC_SAFE_RELEASE(declaration_);
  198. }
  199. }