BsVulkanVertexInputManager.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsVulkanPrerequisites.h"
  5. #include "BsGroupAlloc.h"
  6. #include "BsModule.h"
  7. namespace bs
  8. {
  9. /** @addtogroup Vulkan
  10. * @{
  11. */
  12. /** Contains data describing vertex inputs for a graphics pipeline. */
  13. class VulkanVertexInput
  14. {
  15. public:
  16. VulkanVertexInput(UINT32 id, const VkPipelineVertexInputStateCreateInfo& createInfo);
  17. /** Returns an object contining the necessary information to initialize the vertex input on a pipeline. */
  18. const VkPipelineVertexInputStateCreateInfo* getCreateInfo() const { return &mCreateInfo; }
  19. /** Returns an identifier which uniquely represents this vertex input configuration. */
  20. UINT32 getId() const { return mId; }
  21. private:
  22. UINT32 mId;
  23. VkPipelineVertexInputStateCreateInfo mCreateInfo;
  24. };
  25. /**
  26. * Maps vertex buffer structure and vertex shader inputs in order to create vertex input description usable by Vulkan.
  27. */
  28. class VulkanVertexInputManager : public Module<VulkanVertexInputManager>
  29. {
  30. private:
  31. /** Key uniquely identifying buffer and shader vertex declarations. */
  32. struct VertexDeclarationKey
  33. {
  34. UINT32 bufferDeclId;
  35. UINT32 shaderDeclId;
  36. };
  37. /** Creates a hash from vertex declaration key. */
  38. class HashFunc
  39. {
  40. public:
  41. ::std::size_t operator()(const VertexDeclarationKey& key) const;
  42. };
  43. /** Compares two vertex declaration keys. */
  44. class EqualFunc
  45. {
  46. public:
  47. bool operator()(const VertexDeclarationKey& a, const VertexDeclarationKey& b) const;
  48. };
  49. /** Contains data about a single instance of vertex input object. */
  50. struct VertexInputEntry
  51. {
  52. VkVertexInputAttributeDescription* attributes;
  53. VkVertexInputBindingDescription* bindings;
  54. SPtr<VulkanVertexInput> vertexInput;
  55. UINT32 lastUsedIdx;
  56. GroupAlloc allocator;
  57. };
  58. public:
  59. VulkanVertexInputManager();
  60. ~VulkanVertexInputManager();
  61. /**
  62. * Returns an object that describes how vertex buffer elements map to vertex shader inputs.
  63. *
  64. * @param[in] vbDecl Describes the structure of a single vertex in a vertex buffer.
  65. * @param[in] shaderDecl Describes the vertex element inputs expected by a vertex shader.
  66. * @return Vertex input state description, usable by Vulkan.
  67. */
  68. SPtr<VulkanVertexInput> getVertexInfo(const SPtr<VertexDeclarationCore>& vbDecl,
  69. const SPtr<VertexDeclarationCore>& shaderDecl);
  70. private:
  71. /** Creates a vertex input using the specified parameters and stores it in the input layout map. */
  72. void addNew(const SPtr<VertexDeclarationCore>& vbDecl, const SPtr<VertexDeclarationCore>& shaderDecl);
  73. /** Removes the least used vertex input. */
  74. void removeLeastUsed();
  75. private:
  76. static const int DECLARATION_BUFFER_SIZE = 1024;
  77. static const int NUM_ELEMENTS_TO_PRUNE = 64;
  78. UnorderedMap<VertexDeclarationKey, VertexInputEntry, HashFunc, EqualFunc> mVertexInputMap;
  79. UINT32 mNextId;
  80. bool mWarningShown;
  81. UINT32 mLastUsedCounter;
  82. Mutex mMutex;
  83. };
  84. /** @} */
  85. }