2
0

BsVulkanVertexInputManager.h 3.1 KB

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