BsD3D11InputLayoutManager.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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(VertexDeclarationPtr vertexShaderDecl, VertexDeclarationPtr vertexBufferDecl, D3D11GpuProgram& vertexProgram);
  60. private:
  61. /**
  62. * @brief Creates a new input layout using the specified parameters and stores it in the input layout map.
  63. */
  64. void addNewInputLayout(VertexDeclarationPtr vertexShaderDecl, VertexDeclarationPtr vertexBufferDecl, D3D11GpuProgram& vertexProgram);
  65. /**
  66. * @brief Destroys least used input layout.
  67. */
  68. void removeLeastUsed();
  69. /**
  70. * @brief Checks are the specified vertex shader input and vertex buffer declarations compatible.
  71. * Vertex buffer must provide all inputs to the shader in order for them to be compatible
  72. * (extra data is allowed).
  73. */
  74. bool areCompatible(VertexDeclarationPtr vertexShaderDecl, VertexDeclarationPtr vertexBufferDecl);
  75. private:
  76. static const int DECLARATION_BUFFER_SIZE = 1024;
  77. static const int NUM_ELEMENTS_TO_PRUNE = 64;
  78. UnorderedMap<VertexDeclarationKey, InputLayoutEntry*, HashFunc, EqualFunc> mInputLayoutMap;
  79. bool mWarningShown;
  80. UINT32 mLastUsedCounter;
  81. };
  82. }