D3D11VertexDeclaration.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // Copyright (c) 2008-2015 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 "../../Graphics/Graphics.h"
  23. #include "../../Graphics/GraphicsImpl.h"
  24. #include "../../Graphics/ShaderVariation.h"
  25. #include "../../Graphics/VertexBuffer.h"
  26. #include "../../Graphics/VertexDeclaration.h"
  27. #include "../../IO/Log.h"
  28. #include "../../DebugNew.h"
  29. namespace Urho3D
  30. {
  31. VertexDeclaration::VertexDeclaration(Graphics* graphics, ShaderVariation* vertexShader, VertexBuffer** vertexBuffers, unsigned* elementMasks) :
  32. inputLayout_(0)
  33. {
  34. PODVector<D3D11_INPUT_ELEMENT_DESC> elementDescs;
  35. unsigned vbElementMask = 0;
  36. for (unsigned i = 0; i < MAX_VERTEX_STREAMS; ++i)
  37. {
  38. if (vertexBuffers[i] && elementMasks[i])
  39. {
  40. for (unsigned j = 0; j < MAX_VERTEX_ELEMENTS; ++j)
  41. {
  42. if (elementMasks[i] & (1 << j))
  43. {
  44. D3D11_INPUT_ELEMENT_DESC newDesc;
  45. newDesc.SemanticName = VertexBuffer::elementSemantics[j];
  46. newDesc.SemanticIndex = VertexBuffer::elementSemanticIndices[j];
  47. newDesc.Format = (DXGI_FORMAT)VertexBuffer::elementFormats[j];
  48. newDesc.InputSlot = (unsigned)i;
  49. newDesc.AlignedByteOffset = vertexBuffers[i]->GetElementOffset((VertexElement)j);
  50. newDesc.InputSlotClass = (j >= ELEMENT_INSTANCEMATRIX1 && j <= ELEMENT_INSTANCEMATRIX3) ?
  51. D3D11_INPUT_PER_INSTANCE_DATA : D3D11_INPUT_PER_VERTEX_DATA;
  52. newDesc.InstanceDataStepRate = (j >= ELEMENT_INSTANCEMATRIX1 && j <= ELEMENT_INSTANCEMATRIX3) ? 1 : 0;
  53. elementDescs.Push(newDesc);
  54. vbElementMask |= 1 << j;
  55. }
  56. }
  57. }
  58. }
  59. if (elementDescs.Empty())
  60. return;
  61. ID3D11InputLayout* d3dInputLayout = 0;
  62. const PODVector<unsigned char>& byteCode = vertexShader->GetByteCode();
  63. graphics->GetImpl()->GetDevice()->CreateInputLayout(&elementDescs[0], (unsigned)elementDescs.Size(), &byteCode[0],
  64. byteCode.Size(), &d3dInputLayout);
  65. if (d3dInputLayout)
  66. inputLayout_ = d3dInputLayout;
  67. else
  68. LOGERRORF("Failed to create input layout for shader %s, missing element mask %d",
  69. vertexShader->GetFullName().CString(), vertexShader->GetElementMask() & ~vbElementMask);
  70. }
  71. VertexDeclaration::~VertexDeclaration()
  72. {
  73. if (inputLayout_)
  74. {
  75. ((ID3D11InputLayout*)inputLayout_)->Release();
  76. inputLayout_ = 0;
  77. }
  78. }
  79. }