blob: 611eb84fb8044b71a8f497fd5096e00fb27007e1 [file] [log] [blame]
Alexis Hetu000df8b2018-10-24 15:22:41 -04001// 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 Clayton2ed93ab2019-12-17 20:38:03 +000019#include "Device/Renderer.hpp"
Ben Clayton225a1302019-04-02 12:28:22 +010020#include "Vulkan/VkDescriptorSet.hpp"
Alexis Hetu52edb172019-06-26 10:17:18 -040021#include "Vulkan/VkPipelineCache.hpp"
Alexis Hetu52edb172019-06-26 10:17:18 -040022#include <memory>
Alexis Hetu000df8b2018-10-24 15:22:41 -040023
Nicolas Capens157ba262019-12-10 17:49:14 -050024namespace sw {
Chris Forbesaf4ed532018-12-06 18:33:27 -080025
Nicolas Capens157ba262019-12-10 17:49:14 -050026class ComputeProgram;
27class SpirvShader;
28
29} // namespace sw
30
31namespace vk {
Alexis Hetu000df8b2018-10-24 15:22:41 -040032
Ben Clayton7d0ce412019-12-03 13:26:31 +000033namespace dbg {
34class Context;
35} // namespace dbg
36
Alexis Hetu52edb172019-06-26 10:17:18 -040037class PipelineCache;
Ben Clayton76e9bc02019-02-26 15:02:18 +000038class PipelineLayout;
Alexis Hetu52edb172019-06-26 10:17:18 -040039class ShaderModule;
Nicolas Capensa29aa772019-06-26 00:36:28 -040040class Device;
Ben Clayton76e9bc02019-02-26 15:02:18 +000041
Alexis Hetu000df8b2018-10-24 15:22:41 -040042class Pipeline
43{
44public:
Trevor David Black3ad285a2020-05-26 19:28:05 +000045 Pipeline(PipelineLayout *layout, const Device *device);
Ben Claytoncaf60312019-05-21 15:31:12 +010046 virtual ~Pipeline() = default;
Ben Clayton76e9bc02019-02-26 15:02:18 +000047
Alexis Hetue6e76c62018-12-07 16:26:05 -050048 operator VkPipeline()
49 {
Alexis Hetubd4cf812019-06-14 15:14:07 -040050 return vk::TtoVkT<Pipeline, VkPipeline>(this);
51 }
52
Ben Clayton2ed93ab2019-12-17 20:38:03 +000053 static inline Pipeline *Cast(VkPipeline object)
Alexis Hetubd4cf812019-06-14 15:14:07 -040054 {
55 return vk::VkTtoT<Pipeline, VkPipeline>(object);
Alexis Hetue6e76c62018-12-07 16:26:05 -050056 }
57
Trevor David Black3ad285a2020-05-26 19:28:05 +000058 void destroy(const VkAllocationCallbacks *pAllocator);
Alexis Hetue6e76c62018-12-07 16:26:05 -050059
Ben Clayton2ed93ab2019-12-17 20:38:03 +000060 virtual void destroyPipeline(const VkAllocationCallbacks *pAllocator) = 0;
Alexis Hetu000df8b2018-10-24 15:22:41 -040061#ifndef NDEBUG
62 virtual VkPipelineBindPoint bindPoint() const = 0;
63#endif
Ben Clayton76e9bc02019-02-26 15:02:18 +000064
Trevor David Black3ad285a2020-05-26 19:28:05 +000065 PipelineLayout *getLayout() const
Ben Clayton2ed93ab2019-12-17 20:38:03 +000066 {
67 return layout;
68 }
Ben Clayton76e9bc02019-02-26 15:02:18 +000069
70protected:
Trevor David Black3ad285a2020-05-26 19:28:05 +000071 PipelineLayout *layout = nullptr;
Ben Clayton7d0ce412019-12-03 13:26:31 +000072 Device const *const device;
Nicolas Capensa29aa772019-06-26 00:36:28 -040073
74 const bool robustBufferAccess = true;
Alexis Hetu000df8b2018-10-24 15:22:41 -040075};
76
Alexis Hetue6e76c62018-12-07 16:26:05 -050077class GraphicsPipeline : public Pipeline, public ObjectBase<GraphicsPipeline, VkPipeline>
Alexis Hetu000df8b2018-10-24 15:22:41 -040078{
79public:
Ben Clayton7d0ce412019-12-03 13:26:31 +000080 GraphicsPipeline(const VkGraphicsPipelineCreateInfo *pCreateInfo,
81 void *mem,
82 const Device *device);
Alexis Hetu52edb172019-06-26 10:17:18 -040083 virtual ~GraphicsPipeline() = default;
84
Ben Clayton2ed93ab2019-12-17 20:38:03 +000085 void destroyPipeline(const VkAllocationCallbacks *pAllocator) override;
Alexis Hetu000df8b2018-10-24 15:22:41 -040086
87#ifndef NDEBUG
88 VkPipelineBindPoint bindPoint() const override
89 {
90 return VK_PIPELINE_BIND_POINT_GRAPHICS;
91 }
92#endif
93
Ben Clayton2ed93ab2019-12-17 20:38:03 +000094 static size_t ComputeRequiredAllocationSize(const VkGraphicsPipelineCreateInfo *pCreateInfo);
Alexis Hetuc0f92f22018-11-15 16:25:38 -050095
Ben Clayton2ed93ab2019-12-17 20:38:03 +000096 void compileShaders(const VkAllocationCallbacks *pAllocator, const VkGraphicsPipelineCreateInfo *pCreateInfo, PipelineCache *pipelineCache);
Alexis Hetuc0f92f22018-11-15 16:25:38 -050097
Alexis Hetuc65473d2018-12-07 16:26:05 -050098 uint32_t computePrimitiveCount(uint32_t vertexCount) const;
Ben Clayton2ed93ab2019-12-17 20:38:03 +000099 const sw::Context &getContext() const;
100 const VkRect2D &getScissor() const;
101 const VkViewport &getViewport() const;
Nicolas Capens68d9ad82020-04-02 05:22:04 -0400102 const sw::float4 &getBlendConstants() const;
Alexis Hetu73832432019-04-11 16:43:18 -0400103 bool hasDynamicState(VkDynamicState dynamicState) const;
Alexis Hetu7fe5a062019-05-09 15:35:33 -0400104 bool hasPrimitiveRestartEnable() const { return primitiveRestartEnable; }
Alexis Hetuc0f92f22018-11-15 16:25:38 -0500105
106private:
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000107 void setShader(const VkShaderStageFlagBits &stage, const std::shared_ptr<sw::SpirvShader> spirvShader);
108 const std::shared_ptr<sw::SpirvShader> getShader(const VkShaderStageFlagBits &stage) const;
Alexis Hetu52edb172019-06-26 10:17:18 -0400109 std::shared_ptr<sw::SpirvShader> vertexShader;
110 std::shared_ptr<sw::SpirvShader> fragmentShader;
Chris Forbesaf4ed532018-12-06 18:33:27 -0800111
Alexis Hetu73832432019-04-11 16:43:18 -0400112 uint32_t dynamicStateFlags = 0;
Alexis Hetu7fe5a062019-05-09 15:35:33 -0400113 bool primitiveRestartEnable = false;
Alexis Hetuc0f92f22018-11-15 16:25:38 -0500114 sw::Context context;
Alexis Hetu4ef71eb2019-03-13 10:33:10 -0400115 VkRect2D scissor;
Alexis Hetuc0f92f22018-11-15 16:25:38 -0500116 VkViewport viewport;
Nicolas Capens68d9ad82020-04-02 05:22:04 -0400117 sw::float4 blendConstants;
Alexis Hetu000df8b2018-10-24 15:22:41 -0400118};
119
Alexis Hetue6e76c62018-12-07 16:26:05 -0500120class ComputePipeline : public Pipeline, public ObjectBase<ComputePipeline, VkPipeline>
Alexis Hetu000df8b2018-10-24 15:22:41 -0400121{
122public:
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000123 ComputePipeline(const VkComputePipelineCreateInfo *pCreateInfo, void *mem, const Device *device);
Alexis Hetu52edb172019-06-26 10:17:18 -0400124 virtual ~ComputePipeline() = default;
125
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000126 void destroyPipeline(const VkAllocationCallbacks *pAllocator) override;
Alexis Hetu000df8b2018-10-24 15:22:41 -0400127
128#ifndef NDEBUG
129 VkPipelineBindPoint bindPoint() const override
130 {
131 return VK_PIPELINE_BIND_POINT_COMPUTE;
132 }
133#endif
134
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000135 static size_t ComputeRequiredAllocationSize(const VkComputePipelineCreateInfo *pCreateInfo);
Ben Claytonf2be26a2019-03-08 12:02:05 +0000136
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000137 void compileShaders(const VkAllocationCallbacks *pAllocator, const VkComputePipelineCreateInfo *pCreateInfo, PipelineCache *pipelineCache);
Ben Claytonf2be26a2019-03-08 12:02:05 +0000138
Chris Forbes4a4c2592019-05-13 08:53:36 -0700139 void run(uint32_t baseGroupX, uint32_t baseGroupY, uint32_t baseGroupZ,
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000140 uint32_t groupCountX, uint32_t groupCountY, uint32_t groupCountZ,
Alexis Hetu4f438a52020-06-15 16:13:51 -0400141 vk::DescriptorSet::Array const &descriptorSetObjects,
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000142 vk::DescriptorSet::Bindings const &descriptorSets,
143 vk::DescriptorSet::DynamicOffsets const &descriptorDynamicOffsets,
144 sw::PushConstantStorage const &pushConstants);
Ben Claytonf2be26a2019-03-08 12:02:05 +0000145
146protected:
Alexis Hetu52edb172019-06-26 10:17:18 -0400147 std::shared_ptr<sw::SpirvShader> shader;
148 std::shared_ptr<sw::ComputeProgram> program;
Alexis Hetu000df8b2018-10-24 15:22:41 -0400149};
150
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000151static inline Pipeline *Cast(VkPipeline object)
Alexis Hetu000df8b2018-10-24 15:22:41 -0400152{
Alexis Hetubd4cf812019-06-14 15:14:07 -0400153 return Pipeline::Cast(object);
Alexis Hetu000df8b2018-10-24 15:22:41 -0400154}
155
Nicolas Capens157ba262019-12-10 17:49:14 -0500156} // namespace vk
Alexis Hetu000df8b2018-10-24 15:22:41 -0400157
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000158#endif // VK_PIPELINE_HPP_