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 | 225a130 | 2019-04-02 12:28:22 +0100 | [diff] [blame] | 19 | #include "Vulkan/VkDescriptorSet.hpp" |
Alexis Hetu | c0f92f2 | 2018-11-15 16:25:38 -0500 | [diff] [blame] | 20 | #include "Device/Renderer.hpp" |
Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 21 | |
Ben Clayton | 895df0d | 2019-05-08 08:49:58 +0100 | [diff] [blame] | 22 | namespace sw |
| 23 | { |
| 24 | class ComputeProgram; |
| 25 | class SpirvShader; |
| 26 | } |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 27 | |
Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 28 | namespace vk |
| 29 | { |
| 30 | |
Ben Clayton | 76e9bc0 | 2019-02-26 15:02:18 +0000 | [diff] [blame] | 31 | class PipelineLayout; |
| 32 | |
Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 33 | class Pipeline |
| 34 | { |
| 35 | public: |
Ben Clayton | 76e9bc0 | 2019-02-26 15:02:18 +0000 | [diff] [blame] | 36 | Pipeline(PipelineLayout const *layout); |
Ben Clayton | caf6031 | 2019-05-21 15:31:12 +0100 | [diff] [blame] | 37 | virtual ~Pipeline() = default; |
Ben Clayton | 76e9bc0 | 2019-02-26 15:02:18 +0000 | [diff] [blame] | 38 | |
Alexis Hetu | e6e76c6 | 2018-12-07 16:26:05 -0500 | [diff] [blame] | 39 | operator VkPipeline() |
| 40 | { |
Alexis Hetu | 67cf8a9 | 2019-05-09 17:46:07 -0400 | [diff] [blame] | 41 | return reinterpret_cast<VkPipeline::HandleType>(this); |
Alexis Hetu | e6e76c6 | 2018-12-07 16:26:05 -0500 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | void destroy(const VkAllocationCallbacks* pAllocator) |
| 45 | { |
| 46 | destroyPipeline(pAllocator); |
| 47 | } |
| 48 | |
| 49 | virtual void destroyPipeline(const VkAllocationCallbacks* pAllocator) = 0; |
Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 50 | #ifndef NDEBUG |
| 51 | virtual VkPipelineBindPoint bindPoint() const = 0; |
| 52 | #endif |
Ben Clayton | 76e9bc0 | 2019-02-26 15:02:18 +0000 | [diff] [blame] | 53 | |
| 54 | PipelineLayout const * getLayout() const { return layout; } |
| 55 | |
| 56 | protected: |
| 57 | PipelineLayout const *layout = nullptr; |
Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 58 | }; |
| 59 | |
Alexis Hetu | e6e76c6 | 2018-12-07 16:26:05 -0500 | [diff] [blame] | 60 | class GraphicsPipeline : public Pipeline, public ObjectBase<GraphicsPipeline, VkPipeline> |
Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 61 | { |
| 62 | public: |
| 63 | GraphicsPipeline(const VkGraphicsPipelineCreateInfo* pCreateInfo, void* mem); |
Alexis Hetu | e6e76c6 | 2018-12-07 16:26:05 -0500 | [diff] [blame] | 64 | void destroyPipeline(const VkAllocationCallbacks* pAllocator) override; |
Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 65 | |
| 66 | #ifndef NDEBUG |
| 67 | VkPipelineBindPoint bindPoint() const override |
| 68 | { |
| 69 | return VK_PIPELINE_BIND_POINT_GRAPHICS; |
| 70 | } |
| 71 | #endif |
| 72 | |
| 73 | static size_t ComputeRequiredAllocationSize(const VkGraphicsPipelineCreateInfo* pCreateInfo); |
Alexis Hetu | c0f92f2 | 2018-11-15 16:25:38 -0500 | [diff] [blame] | 74 | |
| 75 | void compileShaders(const VkAllocationCallbacks* pAllocator, const VkGraphicsPipelineCreateInfo* pCreateInfo); |
| 76 | |
Alexis Hetu | c65473d | 2018-12-07 16:26:05 -0500 | [diff] [blame] | 77 | uint32_t computePrimitiveCount(uint32_t vertexCount) const; |
Alexis Hetu | c0f92f2 | 2018-11-15 16:25:38 -0500 | [diff] [blame] | 78 | const sw::Context& getContext() const; |
Alexis Hetu | 4ef71eb | 2019-03-13 10:33:10 -0400 | [diff] [blame] | 79 | const VkRect2D& getScissor() const; |
Alexis Hetu | c0f92f2 | 2018-11-15 16:25:38 -0500 | [diff] [blame] | 80 | const VkViewport& getViewport() const; |
| 81 | const sw::Color<float>& getBlendConstants() const; |
Alexis Hetu | 7383243 | 2019-04-11 16:43:18 -0400 | [diff] [blame] | 82 | bool hasDynamicState(VkDynamicState dynamicState) const; |
Alexis Hetu | 7fe5a06 | 2019-05-09 15:35:33 -0400 | [diff] [blame] | 83 | bool hasPrimitiveRestartEnable() const { return primitiveRestartEnable; } |
Alexis Hetu | c0f92f2 | 2018-11-15 16:25:38 -0500 | [diff] [blame] | 84 | |
| 85 | private: |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 86 | sw::SpirvShader *vertexShader = nullptr; |
| 87 | sw::SpirvShader *fragmentShader = nullptr; |
| 88 | |
Alexis Hetu | 7383243 | 2019-04-11 16:43:18 -0400 | [diff] [blame] | 89 | uint32_t dynamicStateFlags = 0; |
Alexis Hetu | 7fe5a06 | 2019-05-09 15:35:33 -0400 | [diff] [blame] | 90 | bool primitiveRestartEnable = false; |
Alexis Hetu | c0f92f2 | 2018-11-15 16:25:38 -0500 | [diff] [blame] | 91 | sw::Context context; |
Alexis Hetu | 4ef71eb | 2019-03-13 10:33:10 -0400 | [diff] [blame] | 92 | VkRect2D scissor; |
Alexis Hetu | c0f92f2 | 2018-11-15 16:25:38 -0500 | [diff] [blame] | 93 | VkViewport viewport; |
| 94 | sw::Color<float> blendConstants; |
Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 95 | }; |
| 96 | |
Alexis Hetu | e6e76c6 | 2018-12-07 16:26:05 -0500 | [diff] [blame] | 97 | class ComputePipeline : public Pipeline, public ObjectBase<ComputePipeline, VkPipeline> |
Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 98 | { |
| 99 | public: |
| 100 | ComputePipeline(const VkComputePipelineCreateInfo* pCreateInfo, void* mem); |
Alexis Hetu | e6e76c6 | 2018-12-07 16:26:05 -0500 | [diff] [blame] | 101 | void destroyPipeline(const VkAllocationCallbacks* pAllocator) override; |
Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 102 | |
| 103 | #ifndef NDEBUG |
| 104 | VkPipelineBindPoint bindPoint() const override |
| 105 | { |
| 106 | return VK_PIPELINE_BIND_POINT_COMPUTE; |
| 107 | } |
| 108 | #endif |
| 109 | |
| 110 | static size_t ComputeRequiredAllocationSize(const VkComputePipelineCreateInfo* pCreateInfo); |
Ben Clayton | f2be26a | 2019-03-08 12:02:05 +0000 | [diff] [blame] | 111 | |
| 112 | void compileShaders(const VkAllocationCallbacks* pAllocator, const VkComputePipelineCreateInfo* pCreateInfo); |
| 113 | |
Chris Forbes | 4a4c259 | 2019-05-13 08:53:36 -0700 | [diff] [blame] | 114 | void run(uint32_t baseGroupX, uint32_t baseGroupY, uint32_t baseGroupZ, |
| 115 | uint32_t groupCountX, uint32_t groupCountY, uint32_t groupCountZ, |
Ben Clayton | 225a130 | 2019-04-02 12:28:22 +0100 | [diff] [blame] | 116 | vk::DescriptorSet::Bindings const &descriptorSets, |
| 117 | vk::DescriptorSet::DynamicOffsets const &descriptorDynamicOffsets, |
| 118 | sw::PushConstantStorage const &pushConstants); |
Ben Clayton | f2be26a | 2019-03-08 12:02:05 +0000 | [diff] [blame] | 119 | |
| 120 | protected: |
| 121 | sw::SpirvShader *shader = nullptr; |
Ben Clayton | 895df0d | 2019-05-08 08:49:58 +0100 | [diff] [blame] | 122 | sw::ComputeProgram *program = nullptr; |
Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | static inline Pipeline* Cast(VkPipeline object) |
| 126 | { |
Alexis Hetu | 67cf8a9 | 2019-05-09 17:46:07 -0400 | [diff] [blame] | 127 | return reinterpret_cast<Pipeline*>(object.get()); |
Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | } // namespace vk |
| 131 | |
| 132 | #endif // VK_PIPELINE_HPP_ |