OSDN Git Service

Implement indirect draws
[android-x86/external-swiftshader.git] / src / Vulkan / VkPipeline.hpp
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 PipelineLayout;
27
28 class Pipeline
29 {
30 public:
31         Pipeline(PipelineLayout const *layout);
32
33         operator VkPipeline()
34         {
35                 return reinterpret_cast<VkPipeline>(this);
36         }
37
38         void destroy(const VkAllocationCallbacks* pAllocator)
39         {
40                 destroyPipeline(pAllocator);
41         }
42
43         virtual void destroyPipeline(const VkAllocationCallbacks* pAllocator) = 0;
44 #ifndef NDEBUG
45         virtual VkPipelineBindPoint bindPoint() const = 0;
46 #endif
47
48         PipelineLayout const * getLayout() const { return layout; }
49
50 protected:
51         PipelineLayout const *layout = nullptr;
52 };
53
54 class GraphicsPipeline : public Pipeline, public ObjectBase<GraphicsPipeline, VkPipeline>
55 {
56 public:
57         GraphicsPipeline(const VkGraphicsPipelineCreateInfo* pCreateInfo, void* mem);
58         ~GraphicsPipeline() = delete;
59         void destroyPipeline(const VkAllocationCallbacks* pAllocator) override;
60
61 #ifndef NDEBUG
62         VkPipelineBindPoint bindPoint() const override
63         {
64                 return VK_PIPELINE_BIND_POINT_GRAPHICS;
65         }
66 #endif
67
68         static size_t ComputeRequiredAllocationSize(const VkGraphicsPipelineCreateInfo* pCreateInfo);
69
70         void compileShaders(const VkAllocationCallbacks* pAllocator, const VkGraphicsPipelineCreateInfo* pCreateInfo);
71
72         uint32_t computePrimitiveCount(uint32_t vertexCount) const;
73         const sw::Context& getContext() const;
74         const VkRect2D& getScissor() const;
75         const VkViewport& getViewport() const;
76         const sw::Color<float>& getBlendConstants() const;
77
78 private:
79         sw::SpirvShader *vertexShader = nullptr;
80         sw::SpirvShader *fragmentShader = nullptr;
81
82         sw::Context context;
83         VkRect2D scissor;
84         VkViewport viewport;
85         sw::Color<float> blendConstants;
86 };
87
88 class ComputePipeline : public Pipeline, public ObjectBase<ComputePipeline, VkPipeline>
89 {
90 public:
91         ComputePipeline(const VkComputePipelineCreateInfo* pCreateInfo, void* mem);
92         ~ComputePipeline() = delete;
93         void destroyPipeline(const VkAllocationCallbacks* pAllocator) override;
94
95 #ifndef NDEBUG
96         VkPipelineBindPoint bindPoint() const override
97         {
98                 return VK_PIPELINE_BIND_POINT_COMPUTE;
99         }
100 #endif
101
102         static size_t ComputeRequiredAllocationSize(const VkComputePipelineCreateInfo* pCreateInfo);
103
104         void compileShaders(const VkAllocationCallbacks* pAllocator, const VkComputePipelineCreateInfo* pCreateInfo);
105
106         void run(uint32_t groupCountX, uint32_t groupCountY, uint32_t groupCountZ,
107                 size_t numDescriptorSets, VkDescriptorSet *descriptorSets, sw::PushConstantStorage const &pushConstants);
108
109 protected:
110         sw::SpirvShader *shader = nullptr;
111         rr::Routine *routine = nullptr;
112 };
113
114 static inline Pipeline* Cast(VkPipeline object)
115 {
116         return reinterpret_cast<Pipeline*>(object);
117 }
118
119 } // namespace vk
120
121 #endif // VK_PIPELINE_HPP_