BsVulkanGpuPipelineState.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 bs
  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] preserveContents True if the existing framebuffer contents should be preserved for potential read
  40. * or blend operations.
  41. * @param[in] drawOp Type of geometry that will be drawn using the pipeline.
  42. * @param[in] vertexInput State describing inputs to the vertex program.
  43. * @return Vulkan graphics pipeline object.
  44. *
  45. * @note Thread safe.
  46. */
  47. VulkanPipeline* getPipeline(UINT32 deviceIdx, VulkanFramebuffer* framebuffer, bool readOnlyDepth,
  48. bool preserveContents,DrawOperationType drawOp, const SPtr<VulkanVertexInput>& vertexInput);
  49. /**
  50. * Returns a pipeline layout object for the specified device index. If the device index doesn't match a bit in the
  51. * device mask provided on pipeline creation, null is returned.
  52. */
  53. VkPipelineLayout getPipelineLayout(UINT32 deviceIdx) const;
  54. /**
  55. * Registers any resources used by the pipeline with the provided command buffer. This should be called whenever
  56. * a pipeline is bound to a command buffer.
  57. */
  58. void registerPipelineResources(VulkanCmdBuffer* cmdBuffer);
  59. protected:
  60. friend class VulkanRenderStateCoreManager;
  61. VulkanGraphicsPipelineStateCore(const PIPELINE_STATE_CORE_DESC& desc, GpuDeviceFlags deviceMask);
  62. /** @copydoc GraphicsPipelineStateCore::initialize */
  63. void initialize() override;
  64. /**
  65. * Create a new Vulkan graphics pipeline.
  66. *
  67. * @param[in] deviceIdx Index of the device to create the pipeline for.
  68. * @param[in] framebuffer Framebuffer object that defines the surfaces this pipeline will render to.
  69. * @param[in] readOnlyDepth True if the pipeline is only allowed to read the depth buffer, without writes.
  70. * @param[in] preserveContents True if the existing framebuffer contents should be preserved for potential read
  71. * or blend operations.
  72. * @param[in] drawOp Type of geometry that will be drawn using the pipeline.
  73. * @param[in] vertexInput State describing inputs to the vertex program.
  74. * @return Vulkan graphics pipeline object.
  75. *
  76. * @note Thread safe.
  77. */
  78. VulkanPipeline* createPipeline(UINT32 deviceIdx, VulkanFramebuffer* framebuffer, bool readOnlyDepth,
  79. bool preserveContents, DrawOperationType drawOp, const SPtr<VulkanVertexInput>& vertexInput);
  80. /** Key uniquely identifying GPU pipelines. */
  81. struct GpuPipelineKey
  82. {
  83. GpuPipelineKey(UINT32 framebufferId, UINT32 vertexInputId, bool readOnlyDepth, bool preserveContents,
  84. DrawOperationType drawOp);
  85. UINT32 framebufferId;
  86. UINT32 vertexInputId;
  87. bool readOnlyDepth;
  88. bool preserveContents;
  89. DrawOperationType drawOp;
  90. };
  91. /** Creates a hash from GPU pipeline key. */
  92. class HashFunc
  93. {
  94. public:
  95. ::std::size_t operator()(const GpuPipelineKey& key) const;
  96. };
  97. /** Compares two GPU pipeline keys. */
  98. class EqualFunc
  99. {
  100. public:
  101. bool operator()(const GpuPipelineKey& a, const GpuPipelineKey& b) const;
  102. };
  103. /** Contains pipeline data specific to a single Vulkan device. */
  104. struct PerDeviceData
  105. {
  106. VulkanDevice* device;
  107. VkPipelineLayout pipelineLayout;
  108. UnorderedMap<GpuPipelineKey, VulkanPipeline*, HashFunc, EqualFunc> pipelines;
  109. };
  110. VkPipelineShaderStageCreateInfo mShaderStageInfos[5];
  111. VkPipelineInputAssemblyStateCreateInfo mInputAssemblyInfo;
  112. VkPipelineTessellationStateCreateInfo mTesselationInfo;
  113. VkPipelineViewportStateCreateInfo mViewportInfo;
  114. VkPipelineRasterizationStateCreateInfo mRasterizationInfo;
  115. VkPipelineMultisampleStateCreateInfo mMultiSampleInfo;
  116. VkPipelineDepthStencilStateCreateInfo mDepthStencilInfo;
  117. VkPipelineColorBlendAttachmentState mAttachmentBlendStates[BS_MAX_MULTIPLE_RENDER_TARGETS];
  118. VkPipelineColorBlendStateCreateInfo mColorBlendStateInfo;
  119. VkPipelineDynamicStateCreateInfo mDynamicStateInfo;
  120. VkDynamicState mDynamicStates[3];
  121. VkGraphicsPipelineCreateInfo mPipelineInfo;
  122. bool mScissorEnabled;
  123. SPtr<VertexDeclarationCore> mVertexDecl;
  124. GpuDeviceFlags mDeviceMask;
  125. PerDeviceData mPerDeviceData[BS_MAX_DEVICES];
  126. Mutex mMutex;
  127. };
  128. /** Vulkan implementation of a compute pipeline state. */
  129. class VulkanComputePipelineStateCore : public ComputePipelineStateCore
  130. {
  131. public:
  132. ~VulkanComputePipelineStateCore();
  133. /**
  134. * Returns a pipeline object for the specified device index. If the device index doesn't match a bit in the
  135. * device mask provided on pipeline creation, null is returned.
  136. */
  137. VulkanPipeline* getPipeline(UINT32 deviceIdx) const;
  138. /**
  139. * Returns a pipeline layout object for the specified device index. If the device index doesn't match a bit in the
  140. * device mask provided on pipeline creation, null is returned.
  141. */
  142. VkPipelineLayout getPipelineLayout(UINT32 deviceIdx) const;
  143. /**
  144. * Registers any resources used by the pipeline with the provided command buffer. This should be called whenever
  145. * a pipeline is bound to a command buffer.
  146. */
  147. void registerPipelineResources(VulkanCmdBuffer* cmdBuffer);
  148. protected:
  149. friend class VulkanRenderStateCoreManager;
  150. VulkanComputePipelineStateCore(const SPtr<GpuProgramCore>& program, GpuDeviceFlags deviceMask);
  151. /** @copydoc ComputePipelineStateCore::initialize */
  152. void initialize() override;
  153. /** Contains pipeline data specific to a single Vulkan device. */
  154. struct PerDeviceData
  155. {
  156. VulkanDevice* device;
  157. VulkanPipeline* pipeline;
  158. VkPipelineLayout pipelineLayout;
  159. };
  160. GpuDeviceFlags mDeviceMask;
  161. PerDeviceData mPerDeviceData[BS_MAX_DEVICES];
  162. };
  163. /** @} */
  164. }