BsVulkanGpuPipelineState.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 "BsVulkanResource.h"
  6. #include "BsGpuPipelineState.h"
  7. namespace BansheeEngine
  8. {
  9. /** @addtogroup Vulkan
  10. * @{
  11. */
  12. /** Wrapper around a Vulkan graphics pipeline that manages its usage and lifetime. */
  13. class VulkanPipeline : public VulkanResource
  14. {
  15. public:
  16. VulkanPipeline(VulkanResourceManager* owner, VkPipeline pipeline);
  17. ~VulkanPipeline();
  18. /** Returns the internal handle to the Vulkan object. */
  19. VkPipeline getHandle() const { return mPipeline; }
  20. private:
  21. VkPipeline mPipeline;
  22. };
  23. /** Vulkan implementation of a graphics pipeline state. */
  24. class VulkanGraphicsPipelineStateCore : public GraphicsPipelineStateCore
  25. {
  26. public:
  27. ~VulkanGraphicsPipelineStateCore();
  28. /** Checks does the pipeline enable scissor tests. */
  29. bool isScissorEnabled() const { return mScissorEnabled; }
  30. /** Returns the vertex input declaration from the vertex GPU program bound on the pipeline. */
  31. SPtr<VertexDeclarationCore> getInputDeclaration() const { return mVertexDecl; }
  32. /**
  33. * Attempts to find an existing pipeline matching the provided parameters, or creates a new one if one cannot be
  34. * found.
  35. *
  36. * @param[in] deviceIdx Index of the device to retrieve the pipeline for.
  37. * @param[in] framebuffer Framebuffer object that defines the surfaces this pipeline will render to.
  38. * @param[in] readOnlyDepth True if the pipeline is only allowed to read the depth buffer, without writes.
  39. * @param[in] drawOp Type of geometry that will be drawn using the pipeline.
  40. * @param[in] vertexInput State describing inputs to the vertex program.
  41. * @return Vulkan graphics pipeline object.
  42. *
  43. * @note Thread safe.
  44. */
  45. VulkanPipeline* getPipeline(UINT32 deviceIdx, VulkanFramebuffer* framebuffer, bool readOnlyDepth,
  46. DrawOperationType drawOp, const SPtr<VulkanVertexInput>& vertexInput);
  47. /**
  48. * Returns a pipeline layout object for the specified device index. If the device index doesn't match a bit in the
  49. * device mask provided on pipeline creation, null is returned.
  50. */
  51. VkPipelineLayout getPipelineLayout(UINT32 deviceIdx) const;
  52. protected:
  53. friend class VulkanRenderStateCoreManager;
  54. VulkanGraphicsPipelineStateCore(const PIPELINE_STATE_CORE_DESC& desc, GpuDeviceFlags deviceMask);
  55. /** @copydoc GraphicsPipelineStateCore::initialize */
  56. void initialize() override;
  57. /**
  58. * Create a new Vulkan graphics pipeline.
  59. *
  60. * @param[in] deviceIdx Index of the device to create the pipeline for.
  61. * @param[in] framebuffer Framebuffer object that defines the surfaces this pipeline will render to.
  62. * @param[in] readOnlyDepth True if the pipeline is only allowed to read the depth buffer, without writes.
  63. * @param[in] drawOp Type of geometry that will be drawn using the pipeline.
  64. * @param[in] vertexInput State describing inputs to the vertex program.
  65. * @return Vulkan graphics pipeline object.
  66. *
  67. * @note Thread safe.
  68. */
  69. VulkanPipeline* createPipeline(UINT32 deviceIdx, VulkanFramebuffer* framebuffer, bool readOnlyDepth,
  70. DrawOperationType drawOp, const SPtr<VulkanVertexInput>& vertexInput);
  71. /** Key uniquely identifying GPU pipelines. */
  72. struct GpuPipelineKey
  73. {
  74. GpuPipelineKey(UINT32 framebufferId, UINT32 vertexInputId, bool readOnlyDepth, DrawOperationType drawOp);
  75. UINT32 framebufferId;
  76. UINT32 vertexInputId;
  77. bool readOnlyDepth;
  78. DrawOperationType drawOp;
  79. };
  80. /** Creates a hash from GPU pipeline key. */
  81. class HashFunc
  82. {
  83. public:
  84. ::std::size_t operator()(const GpuPipelineKey& key) const;
  85. };
  86. /** Compares two GPU pipeline keys. */
  87. class EqualFunc
  88. {
  89. public:
  90. bool operator()(const GpuPipelineKey& a, const GpuPipelineKey& b) const;
  91. };
  92. /** Contains pipeline data specific to a single Vulkan device. */
  93. struct PerDeviceData
  94. {
  95. VulkanDevice* device;
  96. VkPipelineLayout pipelineLayout;
  97. UnorderedMap<GpuPipelineKey, VulkanPipeline*, HashFunc, EqualFunc> pipelines;
  98. };
  99. VkPipelineShaderStageCreateInfo mShaderStageInfos[5];
  100. VkPipelineInputAssemblyStateCreateInfo mInputAssemblyInfo;
  101. VkPipelineTessellationStateCreateInfo mTesselationInfo;
  102. VkPipelineViewportStateCreateInfo mViewportInfo;
  103. VkPipelineRasterizationStateCreateInfo mRasterizationInfo;
  104. VkPipelineMultisampleStateCreateInfo mMultiSampleInfo;
  105. VkPipelineDepthStencilStateCreateInfo mDepthStencilInfo;
  106. VkPipelineColorBlendAttachmentState mAttachmentBlendStates[BS_MAX_MULTIPLE_RENDER_TARGETS];
  107. VkPipelineColorBlendStateCreateInfo mColorBlendStateInfo;
  108. VkPipelineDynamicStateCreateInfo mDynamicStateInfo;
  109. VkDynamicState mDynamicStates[3];
  110. VkGraphicsPipelineCreateInfo mPipelineInfo;
  111. bool mScissorEnabled;
  112. SPtr<VertexDeclarationCore> mVertexDecl;
  113. GpuDeviceFlags mDeviceMask;
  114. PerDeviceData mPerDeviceData[BS_MAX_DEVICES];
  115. Mutex mMutex;
  116. };
  117. /** Vulkan implementation of a compute pipeline state. */
  118. class VulkanComputePipelineStateCore : public ComputePipelineStateCore
  119. {
  120. public:
  121. ~VulkanComputePipelineStateCore();
  122. /**
  123. * Returns a pipeline object for the specified device index. If the device index doesn't match a bit in the
  124. * device mask provided on pipeline creation, null is returned.
  125. */
  126. VulkanPipeline* getPipeline(UINT32 deviceIdx) const;
  127. /**
  128. * Returns a pipeline layout object for the specified device index. If the device index doesn't match a bit in the
  129. * device mask provided on pipeline creation, null is returned.
  130. */
  131. VkPipelineLayout getPipelineLayout(UINT32 deviceIdx) const;
  132. protected:
  133. friend class VulkanRenderStateCoreManager;
  134. VulkanComputePipelineStateCore(const SPtr<GpuProgramCore>& program, GpuDeviceFlags deviceMask);
  135. /** @copydoc ComputePipelineStateCore::initialize */
  136. void initialize() override;
  137. /** Contains pipeline data specific to a single Vulkan device. */
  138. struct PerDeviceData
  139. {
  140. VulkanDevice* device;
  141. VulkanPipeline* pipeline;
  142. VkPipelineLayout pipelineLayout;
  143. };
  144. GpuDeviceFlags mDeviceMask;
  145. PerDeviceData mPerDeviceData[BS_MAX_DEVICES];
  146. };
  147. /** @} */
  148. }