blob: c2cb295f973519292d97c9e4f7ba0f9b647d995b [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:
Nicolas Capensa29aa772019-06-26 00:36:28 -040045 Pipeline(PipelineLayout const *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
Ben Clayton2ed93ab2019-12-17 20:38:03 +000058 void destroy(const VkAllocationCallbacks *pAllocator)
Alexis Hetue6e76c62018-12-07 16:26:05 -050059 {
60 destroyPipeline(pAllocator);
61 }
62
Ben Clayton2ed93ab2019-12-17 20:38:03 +000063 virtual void destroyPipeline(const VkAllocationCallbacks *pAllocator) = 0;
Alexis Hetu000df8b2018-10-24 15:22:41 -040064#ifndef NDEBUG
65 virtual VkPipelineBindPoint bindPoint() const = 0;
66#endif
Ben Clayton76e9bc02019-02-26 15:02:18 +000067
Ben Clayton2ed93ab2019-12-17 20:38:03 +000068 PipelineLayout const *getLayout() const
69 {
70 return layout;
71 }
Ben Clayton76e9bc02019-02-26 15:02:18 +000072
73protected:
74 PipelineLayout const *layout = nullptr;
Ben Clayton7d0ce412019-12-03 13:26:31 +000075 Device const *const device;
Nicolas Capensa29aa772019-06-26 00:36:28 -040076
77 const bool robustBufferAccess = true;
Alexis Hetu000df8b2018-10-24 15:22:41 -040078};
79
Alexis Hetue6e76c62018-12-07 16:26:05 -050080class GraphicsPipeline : public Pipeline, public ObjectBase<GraphicsPipeline, VkPipeline>
Alexis Hetu000df8b2018-10-24 15:22:41 -040081{
82public:
Ben Clayton7d0ce412019-12-03 13:26:31 +000083 GraphicsPipeline(const VkGraphicsPipelineCreateInfo *pCreateInfo,
84 void *mem,
85 const Device *device);
Alexis Hetu52edb172019-06-26 10:17:18 -040086 virtual ~GraphicsPipeline() = default;
87
Ben Clayton2ed93ab2019-12-17 20:38:03 +000088 void destroyPipeline(const VkAllocationCallbacks *pAllocator) override;
Alexis Hetu000df8b2018-10-24 15:22:41 -040089
90#ifndef NDEBUG
91 VkPipelineBindPoint bindPoint() const override
92 {
93 return VK_PIPELINE_BIND_POINT_GRAPHICS;
94 }
95#endif
96
Ben Clayton2ed93ab2019-12-17 20:38:03 +000097 static size_t ComputeRequiredAllocationSize(const VkGraphicsPipelineCreateInfo *pCreateInfo);
Alexis Hetuc0f92f22018-11-15 16:25:38 -050098
Ben Clayton2ed93ab2019-12-17 20:38:03 +000099 void compileShaders(const VkAllocationCallbacks *pAllocator, const VkGraphicsPipelineCreateInfo *pCreateInfo, PipelineCache *pipelineCache);
Alexis Hetuc0f92f22018-11-15 16:25:38 -0500100
Alexis Hetuc65473d2018-12-07 16:26:05 -0500101 uint32_t computePrimitiveCount(uint32_t vertexCount) const;
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000102 const sw::Context &getContext() const;
103 const VkRect2D &getScissor() const;
104 const VkViewport &getViewport() const;
105 const sw::Color<float> &getBlendConstants() const;
Alexis Hetu73832432019-04-11 16:43:18 -0400106 bool hasDynamicState(VkDynamicState dynamicState) const;
Alexis Hetu7fe5a062019-05-09 15:35:33 -0400107 bool hasPrimitiveRestartEnable() const { return primitiveRestartEnable; }
Alexis Hetuc0f92f22018-11-15 16:25:38 -0500108
109private:
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000110 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 Hetu52edb172019-06-26 10:17:18 -0400112 std::shared_ptr<sw::SpirvShader> vertexShader;
113 std::shared_ptr<sw::SpirvShader> fragmentShader;
Chris Forbesaf4ed532018-12-06 18:33:27 -0800114
Alexis Hetu73832432019-04-11 16:43:18 -0400115 uint32_t dynamicStateFlags = 0;
Alexis Hetu7fe5a062019-05-09 15:35:33 -0400116 bool primitiveRestartEnable = false;
Alexis Hetuc0f92f22018-11-15 16:25:38 -0500117 sw::Context context;
Alexis Hetu4ef71eb2019-03-13 10:33:10 -0400118 VkRect2D scissor;
Alexis Hetuc0f92f22018-11-15 16:25:38 -0500119 VkViewport viewport;
120 sw::Color<float> blendConstants;
Alexis Hetu000df8b2018-10-24 15:22:41 -0400121};
122
Alexis Hetue6e76c62018-12-07 16:26:05 -0500123class ComputePipeline : public Pipeline, public ObjectBase<ComputePipeline, VkPipeline>
Alexis Hetu000df8b2018-10-24 15:22:41 -0400124{
125public:
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000126 ComputePipeline(const VkComputePipelineCreateInfo *pCreateInfo, void *mem, const Device *device);
Alexis Hetu52edb172019-06-26 10:17:18 -0400127 virtual ~ComputePipeline() = default;
128
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000129 void destroyPipeline(const VkAllocationCallbacks *pAllocator) override;
Alexis Hetu000df8b2018-10-24 15:22:41 -0400130
131#ifndef NDEBUG
132 VkPipelineBindPoint bindPoint() const override
133 {
134 return VK_PIPELINE_BIND_POINT_COMPUTE;
135 }
136#endif
137
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000138 static size_t ComputeRequiredAllocationSize(const VkComputePipelineCreateInfo *pCreateInfo);
Ben Claytonf2be26a2019-03-08 12:02:05 +0000139
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000140 void compileShaders(const VkAllocationCallbacks *pAllocator, const VkComputePipelineCreateInfo *pCreateInfo, PipelineCache *pipelineCache);
Ben Claytonf2be26a2019-03-08 12:02:05 +0000141
Chris Forbes4a4c2592019-05-13 08:53:36 -0700142 void run(uint32_t baseGroupX, uint32_t baseGroupY, uint32_t baseGroupZ,
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000143 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 Claytonf2be26a2019-03-08 12:02:05 +0000147
148protected:
Alexis Hetu52edb172019-06-26 10:17:18 -0400149 std::shared_ptr<sw::SpirvShader> shader;
150 std::shared_ptr<sw::ComputeProgram> program;
Alexis Hetu000df8b2018-10-24 15:22:41 -0400151};
152
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000153static inline Pipeline *Cast(VkPipeline object)
Alexis Hetu000df8b2018-10-24 15:22:41 -0400154{
Alexis Hetubd4cf812019-06-14 15:14:07 -0400155 return Pipeline::Cast(object);
Alexis Hetu000df8b2018-10-24 15:22:41 -0400156}
157
Nicolas Capens157ba262019-12-10 17:49:14 -0500158} // namespace vk
Alexis Hetu000df8b2018-10-24 15:22:41 -0400159
Ben Clayton2ed93ab2019-12-17 20:38:03 +0000160#endif // VK_PIPELINE_HPP_