Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 1 | // Copyright 2018 The SwiftShader Authors. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #ifndef VK_PIPELINE_HPP_ |
| 16 | #define VK_PIPELINE_HPP_ |
| 17 | |
| 18 | #include "VkObject.hpp" |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 19 | #include "Device/Renderer.hpp" |
Ben Clayton | 225a130 | 2019-04-02 12:28:22 +0100 | [diff] [blame] | 20 | #include "Vulkan/VkDescriptorSet.hpp" |
Alexis Hetu | 52edb17 | 2019-06-26 10:17:18 -0400 | [diff] [blame] | 21 | #include "Vulkan/VkPipelineCache.hpp" |
Alexis Hetu | 52edb17 | 2019-06-26 10:17:18 -0400 | [diff] [blame] | 22 | #include <memory> |
Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 23 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 24 | namespace sw { |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 25 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 26 | class ComputeProgram; |
| 27 | class SpirvShader; |
| 28 | |
| 29 | } // namespace sw |
| 30 | |
| 31 | namespace vk { |
Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 32 | |
Ben Clayton | 7d0ce41 | 2019-12-03 13:26:31 +0000 | [diff] [blame] | 33 | namespace dbg { |
| 34 | class Context; |
| 35 | } // namespace dbg |
| 36 | |
Alexis Hetu | 52edb17 | 2019-06-26 10:17:18 -0400 | [diff] [blame] | 37 | class PipelineCache; |
Ben Clayton | 76e9bc0 | 2019-02-26 15:02:18 +0000 | [diff] [blame] | 38 | class PipelineLayout; |
Alexis Hetu | 52edb17 | 2019-06-26 10:17:18 -0400 | [diff] [blame] | 39 | class ShaderModule; |
Nicolas Capens | a29aa77 | 2019-06-26 00:36:28 -0400 | [diff] [blame] | 40 | class Device; |
Ben Clayton | 76e9bc0 | 2019-02-26 15:02:18 +0000 | [diff] [blame] | 41 | |
Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 42 | class Pipeline |
| 43 | { |
| 44 | public: |
Nicolas Capens | a29aa77 | 2019-06-26 00:36:28 -0400 | [diff] [blame] | 45 | Pipeline(PipelineLayout const *layout, const Device *device); |
Ben Clayton | caf6031 | 2019-05-21 15:31:12 +0100 | [diff] [blame] | 46 | virtual ~Pipeline() = default; |
Ben Clayton | 76e9bc0 | 2019-02-26 15:02:18 +0000 | [diff] [blame] | 47 | |
Alexis Hetu | e6e76c6 | 2018-12-07 16:26:05 -0500 | [diff] [blame] | 48 | operator VkPipeline() |
| 49 | { |
Alexis Hetu | bd4cf81 | 2019-06-14 15:14:07 -0400 | [diff] [blame] | 50 | return vk::TtoVkT<Pipeline, VkPipeline>(this); |
| 51 | } |
| 52 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 53 | static inline Pipeline *Cast(VkPipeline object) |
Alexis Hetu | bd4cf81 | 2019-06-14 15:14:07 -0400 | [diff] [blame] | 54 | { |
| 55 | return vk::VkTtoT<Pipeline, VkPipeline>(object); |
Alexis Hetu | e6e76c6 | 2018-12-07 16:26:05 -0500 | [diff] [blame] | 56 | } |
| 57 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 58 | void destroy(const VkAllocationCallbacks *pAllocator) |
Alexis Hetu | e6e76c6 | 2018-12-07 16:26:05 -0500 | [diff] [blame] | 59 | { |
| 60 | destroyPipeline(pAllocator); |
| 61 | } |
| 62 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 63 | virtual void destroyPipeline(const VkAllocationCallbacks *pAllocator) = 0; |
Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 64 | #ifndef NDEBUG |
| 65 | virtual VkPipelineBindPoint bindPoint() const = 0; |
| 66 | #endif |
Ben Clayton | 76e9bc0 | 2019-02-26 15:02:18 +0000 | [diff] [blame] | 67 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 68 | PipelineLayout const *getLayout() const |
| 69 | { |
| 70 | return layout; |
| 71 | } |
Ben Clayton | 76e9bc0 | 2019-02-26 15:02:18 +0000 | [diff] [blame] | 72 | |
| 73 | protected: |
| 74 | PipelineLayout const *layout = nullptr; |
Ben Clayton | 7d0ce41 | 2019-12-03 13:26:31 +0000 | [diff] [blame] | 75 | Device const *const device; |
Nicolas Capens | a29aa77 | 2019-06-26 00:36:28 -0400 | [diff] [blame] | 76 | |
| 77 | const bool robustBufferAccess = true; |
Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 78 | }; |
| 79 | |
Alexis Hetu | e6e76c6 | 2018-12-07 16:26:05 -0500 | [diff] [blame] | 80 | class GraphicsPipeline : public Pipeline, public ObjectBase<GraphicsPipeline, VkPipeline> |
Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 81 | { |
| 82 | public: |
Ben Clayton | 7d0ce41 | 2019-12-03 13:26:31 +0000 | [diff] [blame] | 83 | GraphicsPipeline(const VkGraphicsPipelineCreateInfo *pCreateInfo, |
| 84 | void *mem, |
| 85 | const Device *device); |
Alexis Hetu | 52edb17 | 2019-06-26 10:17:18 -0400 | [diff] [blame] | 86 | virtual ~GraphicsPipeline() = default; |
| 87 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 88 | void destroyPipeline(const VkAllocationCallbacks *pAllocator) override; |
Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 89 | |
| 90 | #ifndef NDEBUG |
| 91 | VkPipelineBindPoint bindPoint() const override |
| 92 | { |
| 93 | return VK_PIPELINE_BIND_POINT_GRAPHICS; |
| 94 | } |
| 95 | #endif |
| 96 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 97 | static size_t ComputeRequiredAllocationSize(const VkGraphicsPipelineCreateInfo *pCreateInfo); |
Alexis Hetu | c0f92f2 | 2018-11-15 16:25:38 -0500 | [diff] [blame] | 98 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 99 | void compileShaders(const VkAllocationCallbacks *pAllocator, const VkGraphicsPipelineCreateInfo *pCreateInfo, PipelineCache *pipelineCache); |
Alexis Hetu | c0f92f2 | 2018-11-15 16:25:38 -0500 | [diff] [blame] | 100 | |
Alexis Hetu | c65473d | 2018-12-07 16:26:05 -0500 | [diff] [blame] | 101 | uint32_t computePrimitiveCount(uint32_t vertexCount) const; |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 102 | const sw::Context &getContext() const; |
| 103 | const VkRect2D &getScissor() const; |
| 104 | const VkViewport &getViewport() const; |
| 105 | const sw::Color<float> &getBlendConstants() const; |
Alexis Hetu | 7383243 | 2019-04-11 16:43:18 -0400 | [diff] [blame] | 106 | bool hasDynamicState(VkDynamicState dynamicState) const; |
Alexis Hetu | 7fe5a06 | 2019-05-09 15:35:33 -0400 | [diff] [blame] | 107 | bool hasPrimitiveRestartEnable() const { return primitiveRestartEnable; } |
Alexis Hetu | c0f92f2 | 2018-11-15 16:25:38 -0500 | [diff] [blame] | 108 | |
| 109 | private: |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 110 | void setShader(const VkShaderStageFlagBits &stage, const std::shared_ptr<sw::SpirvShader> spirvShader); |
| 111 | const std::shared_ptr<sw::SpirvShader> getShader(const VkShaderStageFlagBits &stage) const; |
Alexis Hetu | 52edb17 | 2019-06-26 10:17:18 -0400 | [diff] [blame] | 112 | std::shared_ptr<sw::SpirvShader> vertexShader; |
| 113 | std::shared_ptr<sw::SpirvShader> fragmentShader; |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 114 | |
Alexis Hetu | 7383243 | 2019-04-11 16:43:18 -0400 | [diff] [blame] | 115 | uint32_t dynamicStateFlags = 0; |
Alexis Hetu | 7fe5a06 | 2019-05-09 15:35:33 -0400 | [diff] [blame] | 116 | bool primitiveRestartEnable = false; |
Alexis Hetu | c0f92f2 | 2018-11-15 16:25:38 -0500 | [diff] [blame] | 117 | sw::Context context; |
Alexis Hetu | 4ef71eb | 2019-03-13 10:33:10 -0400 | [diff] [blame] | 118 | VkRect2D scissor; |
Alexis Hetu | c0f92f2 | 2018-11-15 16:25:38 -0500 | [diff] [blame] | 119 | VkViewport viewport; |
| 120 | sw::Color<float> blendConstants; |
Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 121 | }; |
| 122 | |
Alexis Hetu | e6e76c6 | 2018-12-07 16:26:05 -0500 | [diff] [blame] | 123 | class ComputePipeline : public Pipeline, public ObjectBase<ComputePipeline, VkPipeline> |
Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 124 | { |
| 125 | public: |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 126 | ComputePipeline(const VkComputePipelineCreateInfo *pCreateInfo, void *mem, const Device *device); |
Alexis Hetu | 52edb17 | 2019-06-26 10:17:18 -0400 | [diff] [blame] | 127 | virtual ~ComputePipeline() = default; |
| 128 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 129 | void destroyPipeline(const VkAllocationCallbacks *pAllocator) override; |
Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 130 | |
| 131 | #ifndef NDEBUG |
| 132 | VkPipelineBindPoint bindPoint() const override |
| 133 | { |
| 134 | return VK_PIPELINE_BIND_POINT_COMPUTE; |
| 135 | } |
| 136 | #endif |
| 137 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 138 | static size_t ComputeRequiredAllocationSize(const VkComputePipelineCreateInfo *pCreateInfo); |
Ben Clayton | f2be26a | 2019-03-08 12:02:05 +0000 | [diff] [blame] | 139 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 140 | void compileShaders(const VkAllocationCallbacks *pAllocator, const VkComputePipelineCreateInfo *pCreateInfo, PipelineCache *pipelineCache); |
Ben Clayton | f2be26a | 2019-03-08 12:02:05 +0000 | [diff] [blame] | 141 | |
Chris Forbes | 4a4c259 | 2019-05-13 08:53:36 -0700 | [diff] [blame] | 142 | void run(uint32_t baseGroupX, uint32_t baseGroupY, uint32_t baseGroupZ, |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 143 | uint32_t groupCountX, uint32_t groupCountY, uint32_t groupCountZ, |
| 144 | vk::DescriptorSet::Bindings const &descriptorSets, |
| 145 | vk::DescriptorSet::DynamicOffsets const &descriptorDynamicOffsets, |
| 146 | sw::PushConstantStorage const &pushConstants); |
Ben Clayton | f2be26a | 2019-03-08 12:02:05 +0000 | [diff] [blame] | 147 | |
| 148 | protected: |
Alexis Hetu | 52edb17 | 2019-06-26 10:17:18 -0400 | [diff] [blame] | 149 | std::shared_ptr<sw::SpirvShader> shader; |
| 150 | std::shared_ptr<sw::ComputeProgram> program; |
Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 151 | }; |
| 152 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 153 | static inline Pipeline *Cast(VkPipeline object) |
Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 154 | { |
Alexis Hetu | bd4cf81 | 2019-06-14 15:14:07 -0400 | [diff] [blame] | 155 | return Pipeline::Cast(object); |
Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 156 | } |
| 157 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 158 | } // namespace vk |
Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 159 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 160 | #endif // VK_PIPELINE_HPP_ |