BsD3D11InputLayoutManager.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #pragma once
  2. #include "BsD3D11Prerequisites.h"
  3. #include "BsVertexDeclaration.h"
  4. namespace BansheeEngine
  5. {
  6. /**
  7. * @brief Handles creation and caching of DirectX 11 input layout objects.
  8. */
  9. class D3D11InputLayoutManager
  10. {
  11. public:
  12. /**
  13. * @brief Key uniquely identifying vertex declaration and vertex shader combination.
  14. */
  15. struct VertexDeclarationKey
  16. {
  17. UINT64 vertxDeclId;
  18. UINT32 vertexProgramId;
  19. };
  20. /**
  21. * @brief Creates a hash from vertex declaration key.
  22. */
  23. class HashFunc
  24. {
  25. public:
  26. ::std::size_t operator()(const VertexDeclarationKey &key) const;
  27. };
  28. /**
  29. * @brief Compares two vertex declaration keys.
  30. */
  31. class EqualFunc
  32. {
  33. public:
  34. bool operator()(const VertexDeclarationKey &a, const VertexDeclarationKey &b) const;
  35. };
  36. /**
  37. * @brief Contains data about a single instance of DX11 input layout object.
  38. */
  39. struct InputLayoutEntry
  40. {
  41. InputLayoutEntry() {}
  42. ID3D11InputLayout* inputLayout;
  43. UINT32 lastUsedIdx;
  44. };
  45. public:
  46. D3D11InputLayoutManager();
  47. ~D3D11InputLayoutManager();
  48. /**
  49. * Finds an existing or creates a new D3D11 input layout. Input layout maps a vertex declaration
  50. * from a vertex buffer to vertex program input declaration
  51. *
  52. * @param vertexShaderDecl Vertex declaration describing vertex program input parameters.
  53. * @param vertexBufferDecl Vertex declaration describing structure of a vertex buffer to be bound as input
  54. * to the GPU program.
  55. * @param vertexProgram Instance of the vertex program we are creating input layout for.
  56. *
  57. * @note Error will be thrown if the vertex buffer doesn't provide all the necessary data that the shader expects.
  58. */
  59. ID3D11InputLayout* retrieveInputLayout(const SPtr<VertexDeclarationCore>& vertexShaderDecl,
  60. const SPtr<VertexDeclarationCore>& vertexBufferDecl, D3D11GpuProgramCore& vertexProgram);
  61. private:
  62. /**
  63. * @brief Creates a new input layout using the specified parameters and stores it in the input layout map.
  64. */
  65. void addNewInputLayout(const SPtr<VertexDeclarationCore>& vertexShaderDecl, const SPtr<VertexDeclarationCore>& vertexBufferDecl,
  66. D3D11GpuProgramCore& vertexProgram);
  67. /**
  68. * @brief Destroys least used input layout.
  69. */
  70. void removeLeastUsed();
  71. /**
  72. * @brief Checks are the specified vertex shader input and vertex buffer declarations compatible.
  73. * Vertex buffer must provide all inputs to the shader in order for them to be compatible
  74. * (extra data is allowed).
  75. */
  76. bool areCompatible(const SPtr<VertexDeclarationCore>& vertexShaderDecl, const SPtr<VertexDeclarationCore>& vertexBufferDecl);
  77. private:
  78. static const int DECLARATION_BUFFER_SIZE = 1024;
  79. static const int NUM_ELEMENTS_TO_PRUNE = 64;
  80. UnorderedMap<VertexDeclarationKey, InputLayoutEntry*, HashFunc, EqualFunc> mInputLayoutMap;
  81. bool mWarningShown;
  82. UINT32 mLastUsedCounter;
  83. };
  84. }