Tests for VK_EXT_shader_module_identifier
[platform/upstream/VK-GL-CTS.git] / external / vulkancts / framework / vulkan / vkPipelineConstructionUtil.hpp
1 #ifndef _VKPIPELINECONSTRUCTIONUTIL_HPP
2 #define _VKPIPELINECONSTRUCTIONUTIL_HPP
3 /*------------------------------------------------------------------------
4  * Vulkan Conformance Tests
5  * ------------------------
6  *
7  * Copyright (c) 2021 The Khronos Group Inc.
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  *//*!
22  * \file
23  * \brief Wrapper that can construct monolithic pipeline or use
24           VK_EXT_graphics_pipeline_library for pipeline construction.
25  *//*--------------------------------------------------------------------*/
26
27 #include "vkRef.hpp"
28 #include "vkDefs.hpp"
29 #include "tcuDefs.hpp"
30 #include "deSharedPtr.hpp"
31 #include <vector>
32 #include <stdexcept>
33
34 namespace vk
35 {
36
37 enum PipelineConstructionType
38 {
39         PIPELINE_CONSTRUCTION_TYPE_MONOLITHIC                   = 0,    // Construct legacy - monolithic pipeline
40         PIPELINE_CONSTRUCTION_TYPE_LINK_TIME_OPTIMIZED_LIBRARY, // Use VK_EXT_graphics_pipeline_library and construc pipeline out of 4 pipeline parts
41         PIPELINE_CONSTRUCTION_TYPE_FAST_LINKED_LIBRARY                  // Same as PIPELINE_CONSTRUCTION_TYPE_OPTIMISED_LIBRARY but with fast linking
42 };
43
44 void checkPipelineLibraryRequirements (const InstanceInterface&         vki,
45                                                                            VkPhysicalDevice                             physicalDevice,
46                                                                            PipelineConstructionType             pipelineConstructionType);
47
48 // This exception may be raised in one of the intermediate steps when using shader module IDs instead of normal module objects.
49 class PipelineCompileRequiredError : public std::runtime_error
50 {
51 public:
52         PipelineCompileRequiredError (const std::string& msg)
53                 : std::runtime_error(msg)
54                 {}
55 };
56
57 // Class that can build monolithic pipeline or fully separated pipeline libraries
58 // depending on PipelineType specified in the constructor.
59 // Rarely needed configuration was extracted to setDefault*/disable* functions while common
60 // state setup is provided as arguments of four setup* functions - one for each state group.
61 class GraphicsPipelineWrapper
62 {
63 public:
64                                                                 GraphicsPipelineWrapper                         (const DeviceInterface&                         vk,
65                                                                                                                                          VkDevice                                                       device,
66                                                                                                                                          const PipelineConstructionType         pipelineConstructionType,
67                                                                                                                                          const VkPipelineCreateFlags            flags = 0u);
68
69                                                                 GraphicsPipelineWrapper                         (GraphicsPipelineWrapper&&) noexcept;
70
71                                                                 ~GraphicsPipelineWrapper                        (void) = default;
72
73
74         // By default pipelineLayout used for monotlithic pipeline is taken from layout specified
75         // in setupPreRasterizationShaderState but when there are also descriptor sets needed for fragment
76         // shader bindings then separate pipeline layout for monolithic pipeline must be provided
77         GraphicsPipelineWrapper&        setMonolithicPipelineLayout                     (const VkPipelineLayout layout);
78
79
80         // By default dynamic state has to be specified before specifying other CreateInfo structures
81         GraphicsPipelineWrapper&        setDynamicState                                         (const VkPipelineDynamicStateCreateInfo* dynamicState);
82
83         // Specify topology that is used by default InputAssemblyState in vertex input state. This needs to be
84         // specified only when there is no custom InputAssemblyState provided in setupVertexInputStete and when
85         // topology is diferent then VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST which is used by default.
86         GraphicsPipelineWrapper&        setDefaultTopology                                      (const VkPrimitiveTopology topology);
87
88         // Specify patch control points that is used by default TessellationState in pre-rasterization shader state.
89         // This can to be specified only when there is no custom TessellationState provided in
90         // setupPreRasterizationShaderState and when patchControlPoints is diferent then 3 which is used by default.
91         GraphicsPipelineWrapper&        setDefaultPatchControlPoints            (const deUint32 patchControlPoints);
92
93         // Enable discarding of primitives that is used by default RasterizationState in pre-rasterization shader state.
94         // This can be specified only when there is no custom RasterizationState provided in setupPreRasterizationShaderState.
95         GraphicsPipelineWrapper&        setDefaultRasterizerDiscardEnable       (const deBool rasterizerDiscardEnable = DE_TRUE);
96
97         // When some states are not provided then default structures can be used. This behaviour can be turned on by one of below methods.
98         // Some tests require those states to be NULL so we can't assume using default versions.
99         GraphicsPipelineWrapper&        setDefaultRasterizationState            (void);
100         GraphicsPipelineWrapper&        setDefaultDepthStencilState                     (void);
101         GraphicsPipelineWrapper&        setDefaultColorBlendState                       (void);
102         GraphicsPipelineWrapper&        setDefaultMultisampleState                      (void);
103
104         // Pre-rasterization shader state uses provieded viewports and scissors to create ViewportState. By default
105         // number of viewports and scissors is same as number of items in vector but when vectors are empty then by
106         // default count of viewports/scissors is set to 1. This can be changed by below functions.
107         GraphicsPipelineWrapper&        setDefaultViewportsCount                        (deUint32 viewportCount = 0u);
108         GraphicsPipelineWrapper&        setDefaultScissorsCount                         (deUint32 scissorCount = 0u);
109
110         // Pre-rasterization shader state uses default ViewportState, this method extends it with VkPipelineViewportDepthClipControlCreateInfoEXT.
111         GraphicsPipelineWrapper&        setDepthClipControl                                     (const VkPipelineViewportDepthClipControlCreateInfoEXT* depthClipControlCreateInfo);
112
113         // Pre-rasterization shader state uses provieded viewports and scissors to create ViewportState. When disableViewportState
114         // is used then ViewportState won't be constructed and NULL will be used.
115         GraphicsPipelineWrapper&        disableViewportState                            (void);
116
117
118         // Setup vertex input state. When VertexInputState or InputAssemblyState are not provided then default structures will be used.
119         GraphicsPipelineWrapper&        setupVertexInputStete                           (const VkPipelineVertexInputStateCreateInfo*            vertexInputState = DE_NULL,
120                                                                                                                                          const VkPipelineInputAssemblyStateCreateInfo*          inputAssemblyState = DE_NULL,
121                                                                                                                                          const VkPipelineCache                                                          partPipelineCache = DE_NULL,
122                                                                                                                                          VkPipelineCreationFeedbackCreateInfoEXT*                       partCreationFeedback = DE_NULL);
123
124         // Setup pre-rasterization shader state.
125         GraphicsPipelineWrapper&        setupPreRasterizationShaderState        (const std::vector<VkViewport>&                                         viewports,
126                                                                                                                                          const std::vector<VkRect2D>&                                           scissors,
127                                                                                                                                          const VkPipelineLayout                                                         layout,
128                                                                                                                                          const VkRenderPass                                                                     renderPass,
129                                                                                                                                          const deUint32                                                                         subpass,
130                                                                                                                                          const VkShaderModule                                                           vertexShaderModule,
131                                                                                                                                          const VkPipelineRasterizationStateCreateInfo*          rasterizationState = DE_NULL,
132                                                                                                                                          const VkShaderModule                                                           tessellationControlShaderModule = DE_NULL,
133                                                                                                                                          const VkShaderModule                                                           tessellationEvalShaderModule = DE_NULL,
134                                                                                                                                          const VkShaderModule                                                           geometryShaderModule = DE_NULL,
135                                                                                                                                          const VkSpecializationInfo*                                            specializationInfo = DE_NULL,
136                                                                                                                                          VkPipelineRenderingCreateInfoKHR*                                      rendering = DE_NULL,
137                                                                                                                                          const VkPipelineCache                                                          partPipelineCache = DE_NULL,
138                                                                                                                                          VkPipelineCreationFeedbackCreateInfoEXT*                       partCreationFeedback = DE_NULL);
139
140         GraphicsPipelineWrapper&        setupPreRasterizationShaderState2       (const std::vector<VkViewport>&                                         viewports,
141                                                                                                                                          const std::vector<VkRect2D>&                                           scissors,
142                                                                                                                                          const VkPipelineLayout                                                         layout,
143                                                                                                                                          const VkRenderPass                                                                     renderPass,
144                                                                                                                                          const deUint32                                                                         subpass,
145                                                                                                                                          const VkShaderModule                                                           vertexShaderModule,
146                                                                                                                                          const VkPipelineRasterizationStateCreateInfo*          rasterizationState = nullptr,
147                                                                                                                                          const VkShaderModule                                                           tessellationControlShaderModulnullptre = DE_NULL,
148                                                                                                                                          const VkShaderModule                                                           tessellationEvalShaderModule = DE_NULL,
149                                                                                                                                          const VkShaderModule                                                           geometryShaderModule = DE_NULL,
150                                                                                                                                          const VkSpecializationInfo*                                            vertSpecializationInfo = nullptr,
151                                                                                                                                          const VkSpecializationInfo*                                            tescSpecializationInfo = nullptr,
152                                                                                                                                          const VkSpecializationInfo*                                            teseSpecializationInfo = nullptr,
153                                                                                                                                          const VkSpecializationInfo*                                            geomSpecializationInfo = nullptr,
154                                                                                                                                          VkPipelineRenderingCreateInfoKHR*                                      rendering = nullptr,
155                                                                                                                                          const VkPipelineCache                                                          partPipelineCache = DE_NULL,
156                                                                                                                                          VkPipelineCreationFeedbackCreateInfoEXT*                       partCreationFeedback = nullptr);
157
158         // Note: VkPipelineShaderStageModuleIdentifierCreateInfoEXT::pIdentifier will not be copied. They need to continue to exist outside this wrapper.
159         GraphicsPipelineWrapper&        setupPreRasterizationShaderState3       (const std::vector<VkViewport>&                                                         viewports,
160                                                                                                                                          const std::vector<VkRect2D>&                                                           scissors,
161                                                                                                                                          const VkPipelineLayout                                                                         layout,
162                                                                                                                                          const VkRenderPass                                                                                     renderPass,
163                                                                                                                                          const deUint32                                                                                         subpass,
164                                                                                                                                          const VkShaderModule                                                                           vertexShaderModule,
165                                                                                                                                          const VkPipelineShaderStageModuleIdentifierCreateInfoEXT*      vertShaderModuleId = nullptr,
166                                                                                                                                          const VkPipelineRasterizationStateCreateInfo*                          rasterizationState = nullptr,
167                                                                                                                                          const VkShaderModule                                                                           tessellationControlShaderModule = DE_NULL,
168                                                                                                                                          const VkPipelineShaderStageModuleIdentifierCreateInfoEXT*      tescShaderModuleId = nullptr,
169                                                                                                                                          const VkShaderModule                                                                           tessellationEvalShaderModule = DE_NULL,
170                                                                                                                                          const VkPipelineShaderStageModuleIdentifierCreateInfoEXT*      teseShaderModuleId = nullptr,
171                                                                                                                                          const VkShaderModule                                                                           geometryShaderModule = DE_NULL,
172                                                                                                                                          const VkPipelineShaderStageModuleIdentifierCreateInfoEXT*      geomShaderModuleId = nullptr,
173                                                                                                                                          const VkSpecializationInfo*                                                            vertSpecializationInfo = nullptr,
174                                                                                                                                          const VkSpecializationInfo*                                                            tescSpecializationInfo = nullptr,
175                                                                                                                                          const VkSpecializationInfo*                                                            teseSpecializationInfo = nullptr,
176                                                                                                                                          const VkSpecializationInfo*                                                            geomSpecializationInfo = nullptr,
177                                                                                                                                          VkPipelineRenderingCreateInfoKHR*                                                      rendering = nullptr,
178                                                                                                                                          const VkPipelineCache                                                                          partPipelineCache = DE_NULL,
179                                                                                                                                          VkPipelineCreationFeedbackCreateInfoEXT*                                       partCreationFeedback = nullptr);
180
181         // Setup fragment shader state.
182         GraphicsPipelineWrapper&        setupFragmentShaderState                        (const VkPipelineLayout                                                         layout,
183                                                                                                                                          const VkRenderPass                                                                     renderPass,
184                                                                                                                                          const deUint32                                                                         subpass,
185                                                                                                                                          const VkShaderModule                                                           fragmentShaderModule,
186                                                                                                                                          const VkPipelineDepthStencilStateCreateInfo*           depthStencilState = DE_NULL,
187                                                                                                                                          const VkPipelineMultisampleStateCreateInfo*            multisampleState = DE_NULL,
188                                                                                                                                          VkPipelineFragmentShadingRateStateCreateInfoKHR*       fragmentShadingRateState = DE_NULL,
189                                                                                                                                          const VkSpecializationInfo*                                            specializationInfo = DE_NULL,
190                                                                                                                                          const VkPipelineCache                                                          partPipelineCache = DE_NULL,
191                                                                                                                                          VkPipelineCreationFeedbackCreateInfoEXT*                       partCreationFeedback = DE_NULL);
192
193         // Note: VkPipelineShaderStageModuleIdentifierCreateInfoEXT::pIdentifier will not be copied. They need to continue to exist outside this wrapper.
194         GraphicsPipelineWrapper&        setupFragmentShaderState2                       (const VkPipelineLayout                                                                         layout,
195                                                                                                                                          const VkRenderPass                                                                                     renderPass,
196                                                                                                                                          const deUint32                                                                                         subpass,
197                                                                                                                                          const VkShaderModule                                                                           fragmentShaderModule,
198                                                                                                                                          const VkPipelineShaderStageModuleIdentifierCreateInfoEXT*      fragmentShaderModuleId = nullptr,
199                                                                                                                                          const VkPipelineDepthStencilStateCreateInfo*                           depthStencilState = nullptr,
200                                                                                                                                          const VkPipelineMultisampleStateCreateInfo*                            multisampleState = nullptr,
201                                                                                                                                          VkPipelineFragmentShadingRateStateCreateInfoKHR*                       fragmentShadingRateState = nullptr,
202                                                                                                                                          const VkSpecializationInfo*                                                            specializationInfo = nullptr,
203                                                                                                                                          const VkPipelineCache                                                                          partPipelineCache = DE_NULL,
204                                                                                                                                          VkPipelineCreationFeedbackCreateInfoEXT*                                       partCreationFeedback = nullptr);
205
206         // Setup fragment output state.
207         GraphicsPipelineWrapper&        setupFragmentOutputState                        (const VkRenderPass                                                                     renderPass,
208                                                                                                                                          const deUint32                                                                         subpass = 0u,
209                                                                                                                                          const VkPipelineColorBlendStateCreateInfo*                     colorBlendState = DE_NULL,
210                                                                                                                                          const VkPipelineMultisampleStateCreateInfo*            multisampleState = DE_NULL,
211                                                                                                                                          const VkPipelineCache                                                          partPipelineCache = DE_NULL,
212                                                                                                                                          VkPipelineCreationFeedbackCreateInfoEXT*                       partCreationFeedback = DE_NULL);
213
214         // Build pipeline object out of provided state.
215         void                                            buildPipeline                                           (const VkPipelineCache                                                          pipelineCache = DE_NULL,
216                                                                                                                                          const VkPipeline                                                                       basePipelineHandle = DE_NULL,
217                                                                                                                                          const deInt32                                                                          basePipelineIndex = 0,
218                                                                                                                                          VkPipelineCreationFeedbackCreateInfoEXT*                       creationFeedback = DE_NULL);
219
220         // Returns true when pipeline was build using buildPipeline method.
221         deBool                                          wasBuild                                                        (void) const;
222
223         // Get compleate pipeline. GraphicsPipelineWrapper preserves ovnership and will desroy pipeline in its destructor.
224         vk::VkPipeline                          getPipeline                                                     (void) const;
225
226         // Destroy compleate pipeline - pipeline parts are not destroyed.
227         void                                            destroyPipeline                                         (void);
228
229 protected:
230
231         // No default constructor - use parametrized constructor or emplace_back in case of vectors.
232         GraphicsPipelineWrapper() = default;
233
234         struct InternalData;
235
236 protected:
237
238         // Store partial pipelines when non monolithic construction was used.
239         Move<VkPipeline>                                m_pipelineParts[4];
240
241         // Store monolithic pipeline or linked pipeline libraries.
242         Move<VkPipeline>                                m_pipelineFinal;
243
244         // Store internal data that is needed only for pipeline construction.
245         de::SharedPtr<InternalData>             m_internalData;
246 };
247
248 } // vk
249
250 #endif // _VKPIPELINECONSTRUCTIONUTIL_HPP