xref: /aosp_15_r20/external/swiftshader/src/Vulkan/VkPipeline.hpp (revision 03ce13f70fcc45d86ee91b7ee4cab1936a95046e)
1*03ce13f7SAndroid Build Coastguard Worker // Copyright 2018 The SwiftShader Authors. All Rights Reserved.
2*03ce13f7SAndroid Build Coastguard Worker //
3*03ce13f7SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*03ce13f7SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*03ce13f7SAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*03ce13f7SAndroid Build Coastguard Worker //
7*03ce13f7SAndroid Build Coastguard Worker //    http://www.apache.org/licenses/LICENSE-2.0
8*03ce13f7SAndroid Build Coastguard Worker //
9*03ce13f7SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*03ce13f7SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*03ce13f7SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*03ce13f7SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*03ce13f7SAndroid Build Coastguard Worker // limitations under the License.
14*03ce13f7SAndroid Build Coastguard Worker 
15*03ce13f7SAndroid Build Coastguard Worker #ifndef VK_PIPELINE_HPP_
16*03ce13f7SAndroid Build Coastguard Worker #define VK_PIPELINE_HPP_
17*03ce13f7SAndroid Build Coastguard Worker 
18*03ce13f7SAndroid Build Coastguard Worker #include "Device/Context.hpp"
19*03ce13f7SAndroid Build Coastguard Worker #include "Vulkan/VkPipelineCache.hpp"
20*03ce13f7SAndroid Build Coastguard Worker #include <memory>
21*03ce13f7SAndroid Build Coastguard Worker 
22*03ce13f7SAndroid Build Coastguard Worker namespace sw {
23*03ce13f7SAndroid Build Coastguard Worker 
24*03ce13f7SAndroid Build Coastguard Worker class ComputeProgram;
25*03ce13f7SAndroid Build Coastguard Worker class SpirvShader;
26*03ce13f7SAndroid Build Coastguard Worker 
27*03ce13f7SAndroid Build Coastguard Worker }  // namespace sw
28*03ce13f7SAndroid Build Coastguard Worker 
29*03ce13f7SAndroid Build Coastguard Worker namespace vk {
30*03ce13f7SAndroid Build Coastguard Worker 
31*03ce13f7SAndroid Build Coastguard Worker class ShaderModule;
32*03ce13f7SAndroid Build Coastguard Worker 
33*03ce13f7SAndroid Build Coastguard Worker class Pipeline
34*03ce13f7SAndroid Build Coastguard Worker {
35*03ce13f7SAndroid Build Coastguard Worker public:
36*03ce13f7SAndroid Build Coastguard Worker 	Pipeline(PipelineLayout *layout, Device *device, bool robustBufferAccess);
37*03ce13f7SAndroid Build Coastguard Worker 	virtual ~Pipeline() = default;
38*03ce13f7SAndroid Build Coastguard Worker 
operator VkPipeline()39*03ce13f7SAndroid Build Coastguard Worker 	operator VkPipeline()
40*03ce13f7SAndroid Build Coastguard Worker 	{
41*03ce13f7SAndroid Build Coastguard Worker 		return vk::TtoVkT<Pipeline, VkPipeline>(this);
42*03ce13f7SAndroid Build Coastguard Worker 	}
43*03ce13f7SAndroid Build Coastguard Worker 
Cast(VkPipeline object)44*03ce13f7SAndroid Build Coastguard Worker 	static inline Pipeline *Cast(VkPipeline object)
45*03ce13f7SAndroid Build Coastguard Worker 	{
46*03ce13f7SAndroid Build Coastguard Worker 		return vk::VkTtoT<Pipeline, VkPipeline>(object);
47*03ce13f7SAndroid Build Coastguard Worker 	}
48*03ce13f7SAndroid Build Coastguard Worker 
49*03ce13f7SAndroid Build Coastguard Worker 	void destroy(const VkAllocationCallbacks *pAllocator);
50*03ce13f7SAndroid Build Coastguard Worker 
51*03ce13f7SAndroid Build Coastguard Worker 	virtual void destroyPipeline(const VkAllocationCallbacks *pAllocator) = 0;
52*03ce13f7SAndroid Build Coastguard Worker #ifndef NDEBUG
53*03ce13f7SAndroid Build Coastguard Worker 	virtual VkPipelineBindPoint bindPoint() const = 0;
54*03ce13f7SAndroid Build Coastguard Worker #endif
55*03ce13f7SAndroid Build Coastguard Worker 
getLayout() const56*03ce13f7SAndroid Build Coastguard Worker 	PipelineLayout *getLayout() const
57*03ce13f7SAndroid Build Coastguard Worker 	{
58*03ce13f7SAndroid Build Coastguard Worker 		return layout;
59*03ce13f7SAndroid Build Coastguard Worker 	}
60*03ce13f7SAndroid Build Coastguard Worker 
61*03ce13f7SAndroid Build Coastguard Worker 	struct PushConstantStorage
62*03ce13f7SAndroid Build Coastguard Worker 	{
63*03ce13f7SAndroid Build Coastguard Worker 		unsigned char data[vk::MAX_PUSH_CONSTANT_SIZE];
64*03ce13f7SAndroid Build Coastguard Worker 	};
65*03ce13f7SAndroid Build Coastguard Worker 
66*03ce13f7SAndroid Build Coastguard Worker protected:
67*03ce13f7SAndroid Build Coastguard Worker 	PipelineLayout *layout = nullptr;
68*03ce13f7SAndroid Build Coastguard Worker 	Device *const device;
69*03ce13f7SAndroid Build Coastguard Worker 
70*03ce13f7SAndroid Build Coastguard Worker 	const bool robustBufferAccess = true;
71*03ce13f7SAndroid Build Coastguard Worker };
72*03ce13f7SAndroid Build Coastguard Worker 
73*03ce13f7SAndroid Build Coastguard Worker class GraphicsPipeline : public Pipeline, public ObjectBase<GraphicsPipeline, VkPipeline>
74*03ce13f7SAndroid Build Coastguard Worker {
75*03ce13f7SAndroid Build Coastguard Worker public:
76*03ce13f7SAndroid Build Coastguard Worker 	GraphicsPipeline(const VkGraphicsPipelineCreateInfo *pCreateInfo,
77*03ce13f7SAndroid Build Coastguard Worker 	                 void *mem,
78*03ce13f7SAndroid Build Coastguard Worker 	                 Device *device);
79*03ce13f7SAndroid Build Coastguard Worker 	virtual ~GraphicsPipeline() = default;
80*03ce13f7SAndroid Build Coastguard Worker 
81*03ce13f7SAndroid Build Coastguard Worker 	void destroyPipeline(const VkAllocationCallbacks *pAllocator) override;
82*03ce13f7SAndroid Build Coastguard Worker 
83*03ce13f7SAndroid Build Coastguard Worker #ifndef NDEBUG
bindPoint() const84*03ce13f7SAndroid Build Coastguard Worker 	VkPipelineBindPoint bindPoint() const override
85*03ce13f7SAndroid Build Coastguard Worker 	{
86*03ce13f7SAndroid Build Coastguard Worker 		return VK_PIPELINE_BIND_POINT_GRAPHICS;
87*03ce13f7SAndroid Build Coastguard Worker 	}
88*03ce13f7SAndroid Build Coastguard Worker #endif
89*03ce13f7SAndroid Build Coastguard Worker 
90*03ce13f7SAndroid Build Coastguard Worker 	static size_t ComputeRequiredAllocationSize(const VkGraphicsPipelineCreateInfo *pCreateInfo);
91*03ce13f7SAndroid Build Coastguard Worker 	static VkGraphicsPipelineLibraryFlagsEXT GetGraphicsPipelineSubset(const VkGraphicsPipelineCreateInfo *pCreateInfo);
92*03ce13f7SAndroid Build Coastguard Worker 
93*03ce13f7SAndroid Build Coastguard Worker 	VkResult compileShaders(const VkAllocationCallbacks *pAllocator, const VkGraphicsPipelineCreateInfo *pCreateInfo, PipelineCache *pipelineCache);
94*03ce13f7SAndroid Build Coastguard Worker 
getCombinedState(const DynamicState & ds) const95*03ce13f7SAndroid Build Coastguard Worker 	GraphicsState getCombinedState(const DynamicState &ds) const { return state.combineStates(ds); }
getState() const96*03ce13f7SAndroid Build Coastguard Worker 	const GraphicsState &getState() const { return state; }
97*03ce13f7SAndroid Build Coastguard Worker 
98*03ce13f7SAndroid Build Coastguard Worker 	void getIndexBuffers(const vk::DynamicState &dynamicState, uint32_t count, uint32_t first, bool indexed, std::vector<std::pair<uint32_t, void *>> *indexBuffers) const;
99*03ce13f7SAndroid Build Coastguard Worker 
getIndexBuffer()100*03ce13f7SAndroid Build Coastguard Worker 	IndexBuffer &getIndexBuffer() { return indexBuffer; }
getIndexBuffer() const101*03ce13f7SAndroid Build Coastguard Worker 	const IndexBuffer &getIndexBuffer() const { return indexBuffer; }
getAttachments()102*03ce13f7SAndroid Build Coastguard Worker 	Attachments &getAttachments() { return attachments; }
getAttachments() const103*03ce13f7SAndroid Build Coastguard Worker 	const Attachments &getAttachments() const { return attachments; }
getInputs()104*03ce13f7SAndroid Build Coastguard Worker 	Inputs &getInputs() { return inputs; }
getInputs() const105*03ce13f7SAndroid Build Coastguard Worker 	const Inputs &getInputs() const { return inputs; }
106*03ce13f7SAndroid Build Coastguard Worker 
107*03ce13f7SAndroid Build Coastguard Worker 	bool preRasterizationContainsImageWrite() const;
108*03ce13f7SAndroid Build Coastguard Worker 	bool fragmentContainsImageWrite() const;
109*03ce13f7SAndroid Build Coastguard Worker 
110*03ce13f7SAndroid Build Coastguard Worker 	const std::shared_ptr<sw::SpirvShader> getShader(const VkShaderStageFlagBits &stage) const;
111*03ce13f7SAndroid Build Coastguard Worker 
112*03ce13f7SAndroid Build Coastguard Worker private:
113*03ce13f7SAndroid Build Coastguard Worker 	void setShader(const VkShaderStageFlagBits &stage, const std::shared_ptr<sw::SpirvShader> spirvShader);
114*03ce13f7SAndroid Build Coastguard Worker 	std::shared_ptr<sw::SpirvShader> vertexShader;
115*03ce13f7SAndroid Build Coastguard Worker 	std::shared_ptr<sw::SpirvShader> fragmentShader;
116*03ce13f7SAndroid Build Coastguard Worker 
117*03ce13f7SAndroid Build Coastguard Worker 	const GraphicsState state;
118*03ce13f7SAndroid Build Coastguard Worker 
119*03ce13f7SAndroid Build Coastguard Worker 	IndexBuffer indexBuffer;
120*03ce13f7SAndroid Build Coastguard Worker 	Attachments attachments;
121*03ce13f7SAndroid Build Coastguard Worker 	Inputs inputs;
122*03ce13f7SAndroid Build Coastguard Worker };
123*03ce13f7SAndroid Build Coastguard Worker 
124*03ce13f7SAndroid Build Coastguard Worker class ComputePipeline : public Pipeline, public ObjectBase<ComputePipeline, VkPipeline>
125*03ce13f7SAndroid Build Coastguard Worker {
126*03ce13f7SAndroid Build Coastguard Worker public:
127*03ce13f7SAndroid Build Coastguard Worker 	ComputePipeline(const VkComputePipelineCreateInfo *pCreateInfo, void *mem, Device *device);
128*03ce13f7SAndroid Build Coastguard Worker 	virtual ~ComputePipeline() = default;
129*03ce13f7SAndroid Build Coastguard Worker 
130*03ce13f7SAndroid Build Coastguard Worker 	void destroyPipeline(const VkAllocationCallbacks *pAllocator) override;
131*03ce13f7SAndroid Build Coastguard Worker 
132*03ce13f7SAndroid Build Coastguard Worker #ifndef NDEBUG
bindPoint() const133*03ce13f7SAndroid Build Coastguard Worker 	VkPipelineBindPoint bindPoint() const override
134*03ce13f7SAndroid Build Coastguard Worker 	{
135*03ce13f7SAndroid Build Coastguard Worker 		return VK_PIPELINE_BIND_POINT_COMPUTE;
136*03ce13f7SAndroid Build Coastguard Worker 	}
137*03ce13f7SAndroid Build Coastguard Worker #endif
138*03ce13f7SAndroid Build Coastguard Worker 
139*03ce13f7SAndroid Build Coastguard Worker 	static size_t ComputeRequiredAllocationSize(const VkComputePipelineCreateInfo *pCreateInfo);
140*03ce13f7SAndroid Build Coastguard Worker 
141*03ce13f7SAndroid Build Coastguard Worker 	VkResult compileShaders(const VkAllocationCallbacks *pAllocator, const VkComputePipelineCreateInfo *pCreateInfo, PipelineCache *pipelineCache);
142*03ce13f7SAndroid Build Coastguard Worker 
143*03ce13f7SAndroid Build Coastguard Worker 	void run(uint32_t baseGroupX, uint32_t baseGroupY, uint32_t baseGroupZ,
144*03ce13f7SAndroid Build Coastguard Worker 	         uint32_t groupCountX, uint32_t groupCountY, uint32_t groupCountZ,
145*03ce13f7SAndroid Build Coastguard Worker 	         const vk::DescriptorSet::Array &descriptorSetObjects,
146*03ce13f7SAndroid Build Coastguard Worker 	         const vk::DescriptorSet::Bindings &descriptorSets,
147*03ce13f7SAndroid Build Coastguard Worker 	         const vk::DescriptorSet::DynamicOffsets &descriptorDynamicOffsets,
148*03ce13f7SAndroid Build Coastguard Worker 	         const vk::Pipeline::PushConstantStorage &pushConstants);
149*03ce13f7SAndroid Build Coastguard Worker 
150*03ce13f7SAndroid Build Coastguard Worker protected:
151*03ce13f7SAndroid Build Coastguard Worker 	std::shared_ptr<sw::SpirvShader> shader;
152*03ce13f7SAndroid Build Coastguard Worker 	std::shared_ptr<sw::ComputeProgram> program;
153*03ce13f7SAndroid Build Coastguard Worker };
154*03ce13f7SAndroid Build Coastguard Worker 
Cast(VkPipeline object)155*03ce13f7SAndroid Build Coastguard Worker static inline Pipeline *Cast(VkPipeline object)
156*03ce13f7SAndroid Build Coastguard Worker {
157*03ce13f7SAndroid Build Coastguard Worker 	return Pipeline::Cast(object);
158*03ce13f7SAndroid Build Coastguard Worker }
159*03ce13f7SAndroid Build Coastguard Worker 
160*03ce13f7SAndroid Build Coastguard Worker }  // namespace vk
161*03ce13f7SAndroid Build Coastguard Worker 
162*03ce13f7SAndroid Build Coastguard Worker #endif  // VK_PIPELINE_HPP_
163