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"
19 #include "Device/Renderer.hpp"
20
21 namespace sw { class SpirvShader; }
22
23 namespace vk
24 {
25
26 class Pipeline
27 {
28 public:
operator VkPipeline()29 operator VkPipeline()
30 {
31 return reinterpret_cast<VkPipeline>(this);
32 }
33
destroy(const VkAllocationCallbacks * pAllocator)34 void destroy(const VkAllocationCallbacks* pAllocator)
35 {
36 destroyPipeline(pAllocator);
37 }
38
39 virtual void destroyPipeline(const VkAllocationCallbacks* pAllocator) = 0;
40 #ifndef NDEBUG
41 virtual VkPipelineBindPoint bindPoint() const = 0;
42 #endif
43 };
44
45 class GraphicsPipeline : public Pipeline, public ObjectBase<GraphicsPipeline, VkPipeline>
46 {
47 public:
48 GraphicsPipeline(const VkGraphicsPipelineCreateInfo* pCreateInfo, void* mem);
49 ~GraphicsPipeline() = delete;
50 void destroyPipeline(const VkAllocationCallbacks* pAllocator) override;
51
52 #ifndef NDEBUG
bindPoint() const53 VkPipelineBindPoint bindPoint() const override
54 {
55 return VK_PIPELINE_BIND_POINT_GRAPHICS;
56 }
57 #endif
58
59 static size_t ComputeRequiredAllocationSize(const VkGraphicsPipelineCreateInfo* pCreateInfo);
60
61 void compileShaders(const VkAllocationCallbacks* pAllocator, const VkGraphicsPipelineCreateInfo* pCreateInfo);
62
63 uint32_t computePrimitiveCount(uint32_t vertexCount) const;
64 const sw::Context& getContext() const;
65 const sw::Rect& getScissor() const;
66 const VkViewport& getViewport() const;
67 const sw::Color<float>& getBlendConstants() const;
68
69 private:
70 sw::SpirvShader *vertexShader = nullptr;
71 sw::SpirvShader *fragmentShader = nullptr;
72
73 rr::Routine* vertexRoutine;
74 rr::Routine* fragmentRoutine;
75
76 sw::Context context;
77 sw::Rect scissor;
78 VkViewport viewport;
79 sw::Color<float> blendConstants;
80 };
81
82 class ComputePipeline : public Pipeline, public ObjectBase<ComputePipeline, VkPipeline>
83 {
84 public:
85 ComputePipeline(const VkComputePipelineCreateInfo* pCreateInfo, void* mem);
86 ~ComputePipeline() = delete;
87 void destroyPipeline(const VkAllocationCallbacks* pAllocator) override;
88
89 #ifndef NDEBUG
bindPoint() const90 VkPipelineBindPoint bindPoint() const override
91 {
92 return VK_PIPELINE_BIND_POINT_COMPUTE;
93 }
94 #endif
95
96 static size_t ComputeRequiredAllocationSize(const VkComputePipelineCreateInfo* pCreateInfo);
97 };
98
Cast(VkPipeline object)99 static inline Pipeline* Cast(VkPipeline object)
100 {
101 return reinterpret_cast<Pipeline*>(object);
102 }
103
104 } // namespace vk
105
106 #endif // VK_PIPELINE_HPP_
107