BsVulkanGpuPipelineState.h 6.6 KB

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