D3D11VertexDeclaration.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #include "../../Precompiled.h"
  23. #include "../../Graphics/Graphics.h"
  24. #include "../../Graphics/GraphicsImpl.h"
  25. #include "../../Graphics/ShaderVariation.h"
  26. #include "../../Graphics/VertexBuffer.h"
  27. #include "../../Graphics/VertexDeclaration.h"
  28. #include "../../IO/Log.h"
  29. #include "../../DebugNew.h"
  30. namespace Urho3D
  31. {
  32. static const DXGI_FORMAT d3dElementFormats[] =
  33. {
  34. DXGI_FORMAT_R32_SINT,
  35. DXGI_FORMAT_R32_FLOAT,
  36. DXGI_FORMAT_R32G32_FLOAT,
  37. DXGI_FORMAT_R32G32B32_FLOAT,
  38. DXGI_FORMAT_R32G32B32A32_FLOAT,
  39. DXGI_FORMAT_R8G8B8A8_UINT,
  40. DXGI_FORMAT_R8G8B8A8_UNORM
  41. };
  42. VertexDeclaration::VertexDeclaration(Graphics* graphics, ShaderVariation* vertexShader, VertexBuffer** vertexBuffers) :
  43. inputLayout_(nullptr)
  44. {
  45. PODVector<D3D11_INPUT_ELEMENT_DESC> elementDescs;
  46. unsigned prevBufferDescs = 0;
  47. for (unsigned i = 0; i < MAX_VERTEX_STREAMS; ++i)
  48. {
  49. if (!vertexBuffers[i])
  50. continue;
  51. const PODVector<VertexElement>& srcElements = vertexBuffers[i]->GetElements();
  52. bool isExisting = false;
  53. for (unsigned j = 0; j < srcElements.Size(); ++j)
  54. {
  55. const VertexElement& srcElement = srcElements[j];
  56. const char* semanticName = ShaderVariation::elementSemanticNames[srcElement.semantic_];
  57. // Override existing element if necessary
  58. for (unsigned k = 0; k < prevBufferDescs; ++k)
  59. {
  60. if (elementDescs[k].SemanticName == semanticName && elementDescs[k].SemanticIndex == srcElement.index_)
  61. {
  62. isExisting = true;
  63. elementDescs[k].InputSlot = i;
  64. elementDescs[k].AlignedByteOffset = srcElement.offset_;
  65. elementDescs[k].InputSlotClass = srcElement.perInstance_ ? D3D11_INPUT_PER_INSTANCE_DATA : D3D11_INPUT_PER_VERTEX_DATA;
  66. elementDescs[k].InstanceDataStepRate = srcElement.perInstance_ ? 1 : 0;
  67. break;
  68. }
  69. }
  70. if (isExisting)
  71. continue;
  72. D3D11_INPUT_ELEMENT_DESC newDesc;
  73. newDesc.SemanticName = semanticName;
  74. newDesc.SemanticIndex = srcElement.index_;
  75. newDesc.Format = d3dElementFormats[srcElement.type_];
  76. newDesc.InputSlot = (UINT)i;
  77. newDesc.AlignedByteOffset = srcElement.offset_;
  78. newDesc.InputSlotClass = srcElement.perInstance_ ? D3D11_INPUT_PER_INSTANCE_DATA : D3D11_INPUT_PER_VERTEX_DATA;
  79. newDesc.InstanceDataStepRate = srcElement.perInstance_ ? 1 : 0;
  80. elementDescs.Push(newDesc);
  81. }
  82. prevBufferDescs = elementDescs.Size();
  83. }
  84. if (elementDescs.Empty())
  85. return;
  86. const PODVector<unsigned char>& byteCode = vertexShader->GetByteCode();
  87. HRESULT hr = graphics->GetImpl()->GetDevice()->CreateInputLayout(&elementDescs[0], (UINT)elementDescs.Size(), &byteCode[0],
  88. byteCode.Size(), (ID3D11InputLayout**)&inputLayout_);
  89. if (FAILED(hr))
  90. {
  91. URHO3D_SAFE_RELEASE(inputLayout_);
  92. URHO3D_LOGERRORF("Failed to create input layout for shader %s due to missing vertex element(s) (HRESULT %x)",
  93. vertexShader->GetFullName().CString(), (unsigned)hr);
  94. }
  95. }
  96. VertexDeclaration::~VertexDeclaration()
  97. {
  98. URHO3D_SAFE_RELEASE(inputLayout_);
  99. }
  100. }