BsD3D11InputLayoutManager.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. /** @addtogroup D3D11
  9. * @{
  10. */
  11. /** Handles creation and caching of DirectX 11 input layout objects. */
  12. class D3D11InputLayoutManager
  13. {
  14. public:
  15. /** Key uniquely identifying vertex declaration and vertex shader combination. */
  16. struct VertexDeclarationKey
  17. {
  18. UINT64 vertxDeclId;
  19. UINT32 vertexProgramId;
  20. };
  21. /** Creates a hash from vertex declaration key. */
  22. class HashFunc
  23. {
  24. public:
  25. ::std::size_t operator()(const VertexDeclarationKey &key) const;
  26. };
  27. /** Compares two vertex declaration keys. */
  28. class EqualFunc
  29. {
  30. public:
  31. bool operator()(const VertexDeclarationKey &a, const VertexDeclarationKey &b) const;
  32. };
  33. /** Contains data about a single instance of DX11 input layout object. */
  34. struct InputLayoutEntry
  35. {
  36. InputLayoutEntry() {}
  37. ID3D11InputLayout* inputLayout;
  38. UINT32 lastUsedIdx;
  39. };
  40. public:
  41. D3D11InputLayoutManager();
  42. ~D3D11InputLayoutManager();
  43. /**
  44. * Finds an existing or creates a new D3D11 input layout. Input layout maps a vertex declaration
  45. * from a vertex buffer to vertex program input declaration
  46. *
  47. * @param[in] vertexShaderDecl Vertex declaration describing vertex program input parameters.
  48. * @param[in] vertexBufferDecl Vertex declaration describing structure of a vertex buffer to be bound as input
  49. * to the GPU program.
  50. * @param[in] vertexProgram Instance of the vertex program we are creating input layout for.
  51. *
  52. * @note Error will be thrown if the vertex buffer doesn't provide all the necessary data that the shader expects.
  53. */
  54. ID3D11InputLayout* retrieveInputLayout(const SPtr<VertexDeclarationCore>& vertexShaderDecl,
  55. const SPtr<VertexDeclarationCore>& vertexBufferDecl, D3D11GpuProgramCore& vertexProgram);
  56. private:
  57. /** Creates a new input layout using the specified parameters and stores it in the input layout map. */
  58. void addNewInputLayout(const SPtr<VertexDeclarationCore>& vertexShaderDecl, const SPtr<VertexDeclarationCore>& vertexBufferDecl,
  59. D3D11GpuProgramCore& vertexProgram);
  60. /** Destroys least used input layout. */
  61. void removeLeastUsed();
  62. private:
  63. static const int DECLARATION_BUFFER_SIZE = 1024;
  64. static const int NUM_ELEMENTS_TO_PRUNE = 64;
  65. UnorderedMap<VertexDeclarationKey, InputLayoutEntry*, HashFunc, EqualFunc> mInputLayoutMap;
  66. bool mWarningShown;
  67. UINT32 mLastUsedCounter;
  68. };
  69. /** @} */
  70. }