BsD3D11InputLayoutManager.h 2.8 KB

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