BsD3D11InputLayoutManager.h 3.1 KB

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