D3D9VertexDeclaration.cpp 6.5 KB

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