D3D11VertexDeclaration.cpp 3.5 KB

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