blob: 0a11eb13c32e6067825ea189c650f61397aac5a0 [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 Clayton225a1302019-04-02 12:28:22 +010019#include "Vulkan/VkDescriptorSet.hpp"
Alexis Hetuc0f92f22018-11-15 16:25:38 -050020#include "Device/Renderer.hpp"
Alexis Hetu000df8b2018-10-24 15:22:41 -040021
Ben Clayton895df0d2019-05-08 08:49:58 +010022namespace sw
23{
24 class ComputeProgram;
25 class SpirvShader;
26}
Chris Forbesaf4ed532018-12-06 18:33:27 -080027
Alexis Hetu000df8b2018-10-24 15:22:41 -040028namespace vk
29{
30
Ben Clayton76e9bc02019-02-26 15:02:18 +000031class PipelineLayout;
32
Alexis Hetu000df8b2018-10-24 15:22:41 -040033class Pipeline
34{
35public:
Ben Clayton76e9bc02019-02-26 15:02:18 +000036 Pipeline(PipelineLayout const *layout);
Ben Claytoncaf60312019-05-21 15:31:12 +010037 virtual ~Pipeline() = default;
Ben Clayton76e9bc02019-02-26 15:02:18 +000038
Alexis Hetue6e76c62018-12-07 16:26:05 -050039 operator VkPipeline()
40 {
Alexis Hetu67cf8a92019-05-09 17:46:07 -040041 return reinterpret_cast<VkPipeline::HandleType>(this);
Alexis Hetue6e76c62018-12-07 16:26:05 -050042 }
43
44 void destroy(const VkAllocationCallbacks* pAllocator)
45 {
46 destroyPipeline(pAllocator);
47 }
48
49 virtual void destroyPipeline(const VkAllocationCallbacks* pAllocator) = 0;
Alexis Hetu000df8b2018-10-24 15:22:41 -040050#ifndef NDEBUG
51 virtual VkPipelineBindPoint bindPoint() const = 0;
52#endif
Ben Clayton76e9bc02019-02-26 15:02:18 +000053
54 PipelineLayout const * getLayout() const { return layout; }
55
56protected:
57 PipelineLayout const *layout = nullptr;
Alexis Hetu000df8b2018-10-24 15:22:41 -040058};
59
Alexis Hetue6e76c62018-12-07 16:26:05 -050060class GraphicsPipeline : public Pipeline, public ObjectBase<GraphicsPipeline, VkPipeline>
Alexis Hetu000df8b2018-10-24 15:22:41 -040061{
62public:
63 GraphicsPipeline(const VkGraphicsPipelineCreateInfo* pCreateInfo, void* mem);
Alexis Hetue6e76c62018-12-07 16:26:05 -050064 void destroyPipeline(const VkAllocationCallbacks* pAllocator) override;
Alexis Hetu000df8b2018-10-24 15:22:41 -040065
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 Hetuc0f92f22018-11-15 16:25:38 -050074
75 void compileShaders(const VkAllocationCallbacks* pAllocator, const VkGraphicsPipelineCreateInfo* pCreateInfo);
76
Alexis Hetuc65473d2018-12-07 16:26:05 -050077 uint32_t computePrimitiveCount(uint32_t vertexCount) const;
Alexis Hetuc0f92f22018-11-15 16:25:38 -050078 const sw::Context& getContext() const;
Alexis Hetu4ef71eb2019-03-13 10:33:10 -040079 const VkRect2D& getScissor() const;
Alexis Hetuc0f92f22018-11-15 16:25:38 -050080 const VkViewport& getViewport() const;
81 const sw::Color<float>& getBlendConstants() const;
Alexis Hetu73832432019-04-11 16:43:18 -040082 bool hasDynamicState(VkDynamicState dynamicState) const;
Alexis Hetu7fe5a062019-05-09 15:35:33 -040083 bool hasPrimitiveRestartEnable() const { return primitiveRestartEnable; }
Alexis Hetuc0f92f22018-11-15 16:25:38 -050084
85private:
Chris Forbesaf4ed532018-12-06 18:33:27 -080086 sw::SpirvShader *vertexShader = nullptr;
87 sw::SpirvShader *fragmentShader = nullptr;
88
Alexis Hetu73832432019-04-11 16:43:18 -040089 uint32_t dynamicStateFlags = 0;
Alexis Hetu7fe5a062019-05-09 15:35:33 -040090 bool primitiveRestartEnable = false;
Alexis Hetuc0f92f22018-11-15 16:25:38 -050091 sw::Context context;
Alexis Hetu4ef71eb2019-03-13 10:33:10 -040092 VkRect2D scissor;
Alexis Hetuc0f92f22018-11-15 16:25:38 -050093 VkViewport viewport;
94 sw::Color<float> blendConstants;
Alexis Hetu000df8b2018-10-24 15:22:41 -040095};
96
Alexis Hetue6e76c62018-12-07 16:26:05 -050097class ComputePipeline : public Pipeline, public ObjectBase<ComputePipeline, VkPipeline>
Alexis Hetu000df8b2018-10-24 15:22:41 -040098{
99public:
100 ComputePipeline(const VkComputePipelineCreateInfo* pCreateInfo, void* mem);
Alexis Hetue6e76c62018-12-07 16:26:05 -0500101 void destroyPipeline(const VkAllocationCallbacks* pAllocator) override;
Alexis Hetu000df8b2018-10-24 15:22:41 -0400102
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 Claytonf2be26a2019-03-08 12:02:05 +0000111
112 void compileShaders(const VkAllocationCallbacks* pAllocator, const VkComputePipelineCreateInfo* pCreateInfo);
113
Chris Forbes4a4c2592019-05-13 08:53:36 -0700114 void run(uint32_t baseGroupX, uint32_t baseGroupY, uint32_t baseGroupZ,
115 uint32_t groupCountX, uint32_t groupCountY, uint32_t groupCountZ,
Ben Clayton225a1302019-04-02 12:28:22 +0100116 vk::DescriptorSet::Bindings const &descriptorSets,
117 vk::DescriptorSet::DynamicOffsets const &descriptorDynamicOffsets,
118 sw::PushConstantStorage const &pushConstants);
Ben Claytonf2be26a2019-03-08 12:02:05 +0000119
120protected:
121 sw::SpirvShader *shader = nullptr;
Ben Clayton895df0d2019-05-08 08:49:58 +0100122 sw::ComputeProgram *program = nullptr;
Alexis Hetu000df8b2018-10-24 15:22:41 -0400123};
124
125static inline Pipeline* Cast(VkPipeline object)
126{
Alexis Hetu67cf8a92019-05-09 17:46:07 -0400127 return reinterpret_cast<Pipeline*>(object.get());
Alexis Hetu000df8b2018-10-24 15:22:41 -0400128}
129
130} // namespace vk
131
132#endif // VK_PIPELINE_HPP_