xref: /aosp_15_r20/external/mesa3d/src/vulkan/runtime/vk_pipeline.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2022 Collabora, LTD
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #ifndef VK_PIPELINE_H
25 #define VK_PIPELINE_H
26 
27 #include "vk_object.h"
28 #include "vk_util.h"
29 
30 #include <stdbool.h>
31 
32 struct nir_shader;
33 struct nir_shader_compiler_options;
34 struct spirv_to_nir_options;
35 struct vk_command_buffer;
36 struct vk_device;
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 #define VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_NIR_CREATE_INFO_MESA \
43    (VkStructureType)1000290001
44 
45 #define VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_NIR_CREATE_INFO_MESA_cast \
46    VkPipelineShaderStageNirCreateInfoMESA
47 
48 typedef struct VkPipelineShaderStageNirCreateInfoMESA {
49    VkStructureType sType;
50    const void *pNext;
51    struct nir_shader *nir;
52 } VkPipelineShaderStageNirCreateInfoMESA;
53 
54 bool
55 vk_pipeline_shader_stage_is_null(const VkPipelineShaderStageCreateInfo *info);
56 
57 bool
58 vk_pipeline_shader_stage_has_identifier(const VkPipelineShaderStageCreateInfo *info);
59 
60 VkResult
61 vk_pipeline_shader_stage_to_nir(struct vk_device *device,
62                                 VkPipelineCreateFlags2KHR pipeline_flags,
63                                 const VkPipelineShaderStageCreateInfo *info,
64                                 const struct spirv_to_nir_options *spirv_options,
65                                 const struct nir_shader_compiler_options *nir_options,
66                                 void *mem_ctx, struct nir_shader **nir_out);
67 
68 enum gl_subgroup_size
69 vk_get_subgroup_size(uint32_t spirv_version,
70                      gl_shader_stage stage,
71                      const void *info_pNext,
72                      bool allow_varying,
73                      bool require_full);
74 
75 struct vk_pipeline_robustness_state {
76    VkPipelineRobustnessBufferBehaviorEXT storage_buffers;
77    VkPipelineRobustnessBufferBehaviorEXT uniform_buffers;
78    VkPipelineRobustnessBufferBehaviorEXT vertex_inputs;
79    VkPipelineRobustnessImageBehaviorEXT images;
80    bool null_uniform_buffer_descriptor;
81    bool null_storage_buffer_descriptor;
82 };
83 
84 /** Hash VkPipelineShaderStageCreateInfo info
85  *
86  * Returns the hash of a VkPipelineShaderStageCreateInfo:
87  *    SHA1(info->module->sha1,
88  *         info->pName,
89  *         vk_stage_to_mesa_stage(info->stage),
90  *         info->pSpecializationInfo)
91  *
92  * Can only be used if VkPipelineShaderStageCreateInfo::module is a
93  * vk_shader_module object.
94  */
95 void
96 vk_pipeline_hash_shader_stage(VkPipelineCreateFlags2KHR pipeline_flags,
97                               const VkPipelineShaderStageCreateInfo *info,
98                               const struct vk_pipeline_robustness_state *rstate,
99                               unsigned char *stage_sha1);
100 
101 void
102 vk_pipeline_robustness_state_fill(const struct vk_device *device,
103                                   struct vk_pipeline_robustness_state *rs,
104                                   const void *pipeline_pNext,
105                                   const void *shader_stage_pNext);
106 
107 static inline VkPipelineCreateFlags2KHR
vk_compute_pipeline_create_flags(const VkComputePipelineCreateInfo * info)108 vk_compute_pipeline_create_flags(const VkComputePipelineCreateInfo *info)
109 {
110    const VkPipelineCreateFlags2CreateInfoKHR *flags2 =
111       vk_find_struct_const(info->pNext,
112                            PIPELINE_CREATE_FLAGS_2_CREATE_INFO_KHR);
113    if (flags2)
114       return flags2->flags;
115    else
116       return info->flags;
117 }
118 
119 static inline VkPipelineCreateFlags2KHR
vk_graphics_pipeline_create_flags(const VkGraphicsPipelineCreateInfo * info)120 vk_graphics_pipeline_create_flags(const VkGraphicsPipelineCreateInfo *info)
121 {
122    const VkPipelineCreateFlags2CreateInfoKHR *flags2 =
123       vk_find_struct_const(info->pNext,
124                            PIPELINE_CREATE_FLAGS_2_CREATE_INFO_KHR);
125    if (flags2)
126       return flags2->flags;
127    else
128       return info->flags;
129 }
130 
131 static inline VkPipelineCreateFlags2KHR
vk_rt_pipeline_create_flags(const VkRayTracingPipelineCreateInfoKHR * info)132 vk_rt_pipeline_create_flags(const VkRayTracingPipelineCreateInfoKHR *info)
133 {
134    const VkPipelineCreateFlags2CreateInfoKHR *flags2 =
135       vk_find_struct_const(info->pNext,
136                            PIPELINE_CREATE_FLAGS_2_CREATE_INFO_KHR);
137    if (flags2)
138       return flags2->flags;
139    else
140       return info->flags;
141 }
142 
143 #ifdef VK_ENABLE_BETA_EXTENSIONS
144 static inline VkPipelineCreateFlags2KHR
vk_graph_pipeline_create_flags(const VkExecutionGraphPipelineCreateInfoAMDX * info)145 vk_graph_pipeline_create_flags(const VkExecutionGraphPipelineCreateInfoAMDX *info)
146 {
147    const VkPipelineCreateFlags2CreateInfoKHR *flags2 =
148       vk_find_struct_const(info->pNext,
149                            PIPELINE_CREATE_FLAGS_2_CREATE_INFO_KHR);
150    if (flags2)
151       return flags2->flags;
152    else
153       return info->flags;
154 }
155 #endif
156 
157 struct vk_pipeline_ops;
158 
159 struct vk_pipeline {
160    struct vk_object_base base;
161 
162    const struct vk_pipeline_ops *ops;
163 
164    VkPipelineBindPoint bind_point;
165    VkPipelineCreateFlags2KHR flags;
166 };
167 
168 VK_DEFINE_NONDISP_HANDLE_CASTS(vk_pipeline, base, VkPipeline,
169                                VK_OBJECT_TYPE_PIPELINE);
170 
171 struct vk_pipeline_ops {
172    void (*destroy)(struct vk_device *device,
173                    struct vk_pipeline *pipeline,
174                    const VkAllocationCallbacks *pAllocator);
175 
176    VkResult (*get_executable_properties)(struct vk_device *device,
177                                          struct vk_pipeline *pipeline,
178                                          uint32_t *executable_count,
179                                          VkPipelineExecutablePropertiesKHR *properties);
180 
181    VkResult (*get_executable_statistics)(struct vk_device *device,
182                                          struct vk_pipeline *pipeline,
183                                          uint32_t executable_index,
184                                          uint32_t *statistic_count,
185                                          VkPipelineExecutableStatisticKHR *statistics);
186 
187    VkResult (*get_internal_representations)(
188       struct vk_device *device,
189       struct vk_pipeline *pipeline,
190       uint32_t executable_index,
191       uint32_t *internal_representation_count,
192       VkPipelineExecutableInternalRepresentationKHR* internal_representations);
193 
194    void (*cmd_bind)(struct vk_command_buffer *cmd_buffer,
195                     struct vk_pipeline *pipeline);
196 };
197 
198 void *vk_pipeline_zalloc(struct vk_device *device,
199                          const struct vk_pipeline_ops *ops,
200                          VkPipelineBindPoint bind_point,
201                          VkPipelineCreateFlags2KHR flags,
202                          const VkAllocationCallbacks *alloc,
203                          size_t size);
204 
205 void vk_pipeline_free(struct vk_device *device,
206                       const VkAllocationCallbacks *alloc,
207                       struct vk_pipeline *pipeline);
208 
209 void
210 vk_cmd_unbind_pipelines_for_stages(struct vk_command_buffer *cmd_buffer,
211                                    VkShaderStageFlags stages);
212 
213 #ifdef __cplusplus
214 }
215 #endif
216 
217 #endif /* VK_PIPELINE_H */
218