1 /*
2 * Copyright © 2015 Intel Corporation
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 #include <assert.h>
25 #include <stdbool.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29
30 #include "util/mesa-sha1.h"
31 #include "util/os_time.h"
32 #include "common/intel_l3_config.h"
33 #include "common/intel_sample_positions.h"
34 #include "compiler/brw_disasm.h"
35 #include "anv_private.h"
36 #include "compiler/brw_nir.h"
37 #include "compiler/brw_nir_rt.h"
38 #include "compiler/intel_nir.h"
39 #include "anv_nir.h"
40 #include "nir/nir_xfb_info.h"
41 #include "spirv/nir_spirv.h"
42 #include "vk_nir_convert_ycbcr.h"
43 #include "vk_nir.h"
44 #include "vk_pipeline.h"
45 #include "vk_render_pass.h"
46 #include "vk_util.h"
47
48 struct lower_set_vtx_and_prim_count_state {
49 nir_variable *primitive_count;
50 };
51
52 static nir_variable *
anv_nir_prim_count_store(nir_builder * b,nir_def * val)53 anv_nir_prim_count_store(nir_builder *b, nir_def *val)
54 {
55 nir_variable *primitive_count =
56 nir_variable_create(b->shader,
57 nir_var_shader_out,
58 glsl_uint_type(),
59 "gl_PrimitiveCountNV");
60 primitive_count->data.location = VARYING_SLOT_PRIMITIVE_COUNT;
61 primitive_count->data.interpolation = INTERP_MODE_NONE;
62
63 nir_def *local_invocation_index = nir_load_local_invocation_index(b);
64
65 nir_def *cmp = nir_ieq_imm(b, local_invocation_index, 0);
66 nir_if *if_stmt = nir_push_if(b, cmp);
67 {
68 nir_deref_instr *prim_count_deref = nir_build_deref_var(b, primitive_count);
69 nir_store_deref(b, prim_count_deref, val, 1);
70 }
71 nir_pop_if(b, if_stmt);
72
73 return primitive_count;
74 }
75
76 static bool
anv_nir_lower_set_vtx_and_prim_count_instr(nir_builder * b,nir_intrinsic_instr * intrin,void * data)77 anv_nir_lower_set_vtx_and_prim_count_instr(nir_builder *b,
78 nir_intrinsic_instr *intrin,
79 void *data)
80 {
81 if (intrin->intrinsic != nir_intrinsic_set_vertex_and_primitive_count)
82 return false;
83
84 /* Detect some cases of invalid primitive count. They might lead to URB
85 * memory corruption, where workgroups overwrite each other output memory.
86 */
87 if (nir_src_is_const(intrin->src[1]) &&
88 nir_src_as_uint(intrin->src[1]) > b->shader->info.mesh.max_primitives_out) {
89 assert(!"number of primitives bigger than max specified");
90 }
91
92 struct lower_set_vtx_and_prim_count_state *state = data;
93 /* this intrinsic should show up only once */
94 assert(state->primitive_count == NULL);
95
96 b->cursor = nir_before_instr(&intrin->instr);
97
98 state->primitive_count = anv_nir_prim_count_store(b, intrin->src[1].ssa);
99
100 nir_instr_remove(&intrin->instr);
101
102 return true;
103 }
104
105 static bool
anv_nir_lower_set_vtx_and_prim_count(nir_shader * nir)106 anv_nir_lower_set_vtx_and_prim_count(nir_shader *nir)
107 {
108 struct lower_set_vtx_and_prim_count_state state = { NULL, };
109
110 nir_shader_intrinsics_pass(nir, anv_nir_lower_set_vtx_and_prim_count_instr,
111 nir_metadata_none,
112 &state);
113
114 /* If we didn't find set_vertex_and_primitive_count, then we have to
115 * insert store of value 0 to primitive_count.
116 */
117 if (state.primitive_count == NULL) {
118 nir_builder b;
119 nir_function_impl *entrypoint = nir_shader_get_entrypoint(nir);
120 b = nir_builder_at(nir_before_impl(entrypoint));
121 nir_def *zero = nir_imm_int(&b, 0);
122 state.primitive_count = anv_nir_prim_count_store(&b, zero);
123 }
124
125 assert(state.primitive_count != NULL);
126 return true;
127 }
128
129 /* Eventually, this will become part of anv_CreateShader. Unfortunately,
130 * we can't do that yet because we don't have the ability to copy nir.
131 */
132 static nir_shader *
anv_shader_stage_to_nir(struct anv_device * device,const VkPipelineShaderStageCreateInfo * stage_info,enum brw_robustness_flags robust_flags,void * mem_ctx)133 anv_shader_stage_to_nir(struct anv_device *device,
134 const VkPipelineShaderStageCreateInfo *stage_info,
135 enum brw_robustness_flags robust_flags,
136 void *mem_ctx)
137 {
138 const struct anv_physical_device *pdevice = device->physical;
139 const struct anv_instance *instance = pdevice->instance;
140 const struct brw_compiler *compiler = pdevice->compiler;
141 gl_shader_stage stage = vk_to_mesa_shader_stage(stage_info->stage);
142 const nir_shader_compiler_options *nir_options =
143 compiler->nir_options[stage];
144
145 const bool rt_enabled = ANV_SUPPORT_RT && pdevice->info.has_ray_tracing;
146 const struct spirv_to_nir_options spirv_options = {
147 .caps = {
148 .amd_image_gather_bias_lod = pdevice->info.ver >= 20,
149 .cooperative_matrix = anv_has_cooperative_matrix(pdevice),
150 .demote_to_helper_invocation = true,
151 .derivative_group = true,
152 .descriptor_array_dynamic_indexing = true,
153 .descriptor_array_non_uniform_indexing = true,
154 .descriptor_indexing = true,
155 .device_group = true,
156 .draw_parameters = true,
157 .float16 = true,
158 .float32_atomic_add = pdevice->info.has_lsc,
159 .float32_atomic_min_max = true,
160 .float64 = true,
161 .float64_atomic_min_max = pdevice->info.has_lsc,
162 .fragment_shader_sample_interlock = true,
163 .fragment_shader_pixel_interlock = true,
164 .geometry_streams = true,
165 /* When using Vulkan 1.3 or KHR_format_feature_flags2 is enabled, the
166 * read/write without format is per format, so just report true. It's
167 * up to the application to check.
168 */
169 .image_read_without_format =
170 pdevice->info.verx10 >= 125 ||
171 instance->vk.app_info.api_version >= VK_API_VERSION_1_3 ||
172 device->vk.enabled_extensions.KHR_format_feature_flags2,
173 .image_write_without_format = true,
174 .int8 = true,
175 .int16 = true,
176 .int64 = true,
177 .int64_atomics = true,
178 .integer_functions2 = true,
179 .mesh_shading = pdevice->vk.supported_extensions.EXT_mesh_shader,
180 .mesh_shading_nv = false,
181 .min_lod = true,
182 .multiview = true,
183 .physical_storage_buffer_address = true,
184 .post_depth_coverage = true,
185 .runtime_descriptor_array = true,
186 .float_controls = true,
187 .ray_cull_mask = rt_enabled,
188 .ray_query = rt_enabled,
189 .ray_tracing = rt_enabled,
190 .ray_tracing_position_fetch = rt_enabled,
191 .shader_clock = true,
192 .shader_viewport_index_layer = true,
193 .sparse_residency = pdevice->has_sparse,
194 .stencil_export = true,
195 .storage_8bit = true,
196 .storage_16bit = true,
197 .subgroup_arithmetic = true,
198 .subgroup_basic = true,
199 .subgroup_ballot = true,
200 .subgroup_dispatch = true,
201 .subgroup_quad = true,
202 .subgroup_rotate = true,
203 .subgroup_uniform_control_flow = true,
204 .subgroup_shuffle = true,
205 .subgroup_vote = true,
206 .tessellation = true,
207 .transform_feedback = true,
208 .variable_pointers = true,
209 .vk_memory_model = true,
210 .vk_memory_model_device_scope = true,
211 .workgroup_memory_explicit_layout = true,
212 .fragment_shading_rate = pdevice->info.ver >= 11,
213 },
214 .ubo_addr_format = anv_nir_ubo_addr_format(pdevice, robust_flags),
215 .ssbo_addr_format = anv_nir_ssbo_addr_format(pdevice, robust_flags),
216 .phys_ssbo_addr_format = nir_address_format_64bit_global,
217 .push_const_addr_format = nir_address_format_logical,
218
219 /* TODO: Consider changing this to an address format that has the NULL
220 * pointer equals to 0. That might be a better format to play nice
221 * with certain code / code generators.
222 */
223 .shared_addr_format = nir_address_format_32bit_offset,
224
225 .min_ubo_alignment = ANV_UBO_ALIGNMENT,
226 .min_ssbo_alignment = ANV_SSBO_ALIGNMENT,
227 };
228
229 nir_shader *nir;
230 VkResult result =
231 vk_pipeline_shader_stage_to_nir(&device->vk, stage_info,
232 &spirv_options, nir_options,
233 mem_ctx, &nir);
234 if (result != VK_SUCCESS)
235 return NULL;
236
237 if (INTEL_DEBUG(intel_debug_flag_for_shader_stage(stage))) {
238 fprintf(stderr, "NIR (from SPIR-V) for %s shader:\n",
239 gl_shader_stage_name(stage));
240 nir_print_shader(nir, stderr);
241 }
242
243 NIR_PASS_V(nir, nir_lower_io_to_temporaries,
244 nir_shader_get_entrypoint(nir), true, false);
245
246 return nir;
247 }
248
249 static VkResult
anv_pipeline_init(struct anv_pipeline * pipeline,struct anv_device * device,enum anv_pipeline_type type,VkPipelineCreateFlags2KHR flags,const VkAllocationCallbacks * pAllocator)250 anv_pipeline_init(struct anv_pipeline *pipeline,
251 struct anv_device *device,
252 enum anv_pipeline_type type,
253 VkPipelineCreateFlags2KHR flags,
254 const VkAllocationCallbacks *pAllocator)
255 {
256 VkResult result;
257
258 memset(pipeline, 0, sizeof(*pipeline));
259
260 vk_object_base_init(&device->vk, &pipeline->base,
261 VK_OBJECT_TYPE_PIPELINE);
262 pipeline->device = device;
263
264 /* It's the job of the child class to provide actual backing storage for
265 * the batch by setting batch.start, batch.next, and batch.end.
266 */
267 pipeline->batch.alloc = pAllocator ? pAllocator : &device->vk.alloc;
268 pipeline->batch.relocs = &pipeline->batch_relocs;
269 pipeline->batch.status = VK_SUCCESS;
270
271 const bool uses_relocs = device->physical->uses_relocs;
272 result = anv_reloc_list_init(&pipeline->batch_relocs,
273 pipeline->batch.alloc, uses_relocs);
274 if (result != VK_SUCCESS)
275 return result;
276
277 pipeline->mem_ctx = ralloc_context(NULL);
278
279 pipeline->type = type;
280 pipeline->flags = flags;
281
282 util_dynarray_init(&pipeline->executables, pipeline->mem_ctx);
283
284 anv_pipeline_sets_layout_init(&pipeline->layout, device,
285 false /* independent_sets */);
286
287 return VK_SUCCESS;
288 }
289
290 static void
anv_pipeline_init_layout(struct anv_pipeline * pipeline,struct anv_pipeline_layout * pipeline_layout)291 anv_pipeline_init_layout(struct anv_pipeline *pipeline,
292 struct anv_pipeline_layout *pipeline_layout)
293 {
294 if (pipeline_layout) {
295 struct anv_pipeline_sets_layout *layout = &pipeline_layout->sets_layout;
296 for (uint32_t s = 0; s < layout->num_sets; s++) {
297 if (layout->set[s].layout == NULL)
298 continue;
299
300 anv_pipeline_sets_layout_add(&pipeline->layout, s,
301 layout->set[s].layout);
302 }
303 }
304
305 anv_pipeline_sets_layout_hash(&pipeline->layout);
306 assert(!pipeline_layout ||
307 !memcmp(pipeline->layout.sha1,
308 pipeline_layout->sets_layout.sha1,
309 sizeof(pipeline_layout->sets_layout.sha1)));
310 }
311
312 static void
anv_pipeline_finish(struct anv_pipeline * pipeline,struct anv_device * device)313 anv_pipeline_finish(struct anv_pipeline *pipeline,
314 struct anv_device *device)
315 {
316 anv_pipeline_sets_layout_fini(&pipeline->layout);
317 anv_reloc_list_finish(&pipeline->batch_relocs);
318 ralloc_free(pipeline->mem_ctx);
319 vk_object_base_finish(&pipeline->base);
320 }
321
anv_DestroyPipeline(VkDevice _device,VkPipeline _pipeline,const VkAllocationCallbacks * pAllocator)322 void anv_DestroyPipeline(
323 VkDevice _device,
324 VkPipeline _pipeline,
325 const VkAllocationCallbacks* pAllocator)
326 {
327 ANV_FROM_HANDLE(anv_device, device, _device);
328 ANV_FROM_HANDLE(anv_pipeline, pipeline, _pipeline);
329
330 if (!pipeline)
331 return;
332
333 ANV_RMV(resource_destroy, device, pipeline);
334
335 switch (pipeline->type) {
336 case ANV_PIPELINE_GRAPHICS_LIB: {
337 struct anv_graphics_lib_pipeline *gfx_pipeline =
338 anv_pipeline_to_graphics_lib(pipeline);
339
340 for (unsigned s = 0; s < ARRAY_SIZE(gfx_pipeline->base.shaders); s++) {
341 if (gfx_pipeline->base.shaders[s])
342 anv_shader_bin_unref(device, gfx_pipeline->base.shaders[s]);
343 }
344 break;
345 }
346
347 case ANV_PIPELINE_GRAPHICS: {
348 struct anv_graphics_pipeline *gfx_pipeline =
349 anv_pipeline_to_graphics(pipeline);
350
351 for (unsigned s = 0; s < ARRAY_SIZE(gfx_pipeline->base.shaders); s++) {
352 if (gfx_pipeline->base.shaders[s])
353 anv_shader_bin_unref(device, gfx_pipeline->base.shaders[s]);
354 }
355 break;
356 }
357
358 case ANV_PIPELINE_COMPUTE: {
359 struct anv_compute_pipeline *compute_pipeline =
360 anv_pipeline_to_compute(pipeline);
361
362 if (compute_pipeline->cs)
363 anv_shader_bin_unref(device, compute_pipeline->cs);
364
365 break;
366 }
367
368 case ANV_PIPELINE_RAY_TRACING: {
369 struct anv_ray_tracing_pipeline *rt_pipeline =
370 anv_pipeline_to_ray_tracing(pipeline);
371
372 util_dynarray_foreach(&rt_pipeline->shaders,
373 struct anv_shader_bin *, shader) {
374 anv_shader_bin_unref(device, *shader);
375 }
376 break;
377 }
378
379 default:
380 unreachable("invalid pipeline type");
381 }
382
383 anv_pipeline_finish(pipeline, device);
384 vk_free2(&device->vk.alloc, pAllocator, pipeline);
385 }
386
387 struct anv_pipeline_stage {
388 gl_shader_stage stage;
389
390 struct vk_pipeline_robustness_state rstate;
391
392 /* VkComputePipelineCreateInfo, VkGraphicsPipelineCreateInfo or
393 * VkRayTracingPipelineCreateInfoKHR pNext field
394 */
395 const void *pipeline_pNext;
396 const VkPipelineShaderStageCreateInfo *info;
397
398 unsigned char shader_sha1[20];
399 uint32_t source_hash;
400
401 union brw_any_prog_key key;
402
403 struct {
404 gl_shader_stage stage;
405 unsigned char sha1[20];
406 } cache_key;
407
408 nir_shader *nir;
409
410 struct {
411 nir_shader *nir;
412 struct anv_shader_bin *bin;
413 } imported;
414
415 struct anv_push_descriptor_info push_desc_info;
416
417 enum gl_subgroup_size subgroup_size_type;
418
419 enum brw_robustness_flags robust_flags;
420
421 struct anv_pipeline_binding surface_to_descriptor[256];
422 struct anv_pipeline_binding sampler_to_descriptor[256];
423 struct anv_pipeline_bind_map bind_map;
424
425 bool uses_bt_for_push_descs;
426
427 enum anv_dynamic_push_bits dynamic_push_values;
428
429 union brw_any_prog_data prog_data;
430
431 uint32_t num_stats;
432 struct brw_compile_stats stats[3];
433 char *disasm[3];
434
435 VkPipelineCreationFeedback feedback;
436 uint32_t feedback_idx;
437
438 const unsigned *code;
439
440 struct anv_shader_bin *bin;
441 };
442
443 static enum brw_robustness_flags
anv_get_robust_flags(const struct vk_pipeline_robustness_state * rstate)444 anv_get_robust_flags(const struct vk_pipeline_robustness_state *rstate)
445 {
446 return
447 ((rstate->storage_buffers !=
448 VK_PIPELINE_ROBUSTNESS_BUFFER_BEHAVIOR_DISABLED_EXT) ?
449 BRW_ROBUSTNESS_SSBO : 0) |
450 ((rstate->uniform_buffers !=
451 VK_PIPELINE_ROBUSTNESS_BUFFER_BEHAVIOR_DISABLED_EXT) ?
452 BRW_ROBUSTNESS_UBO : 0);
453 }
454
455 static void
populate_base_prog_key(struct anv_pipeline_stage * stage,const struct anv_device * device)456 populate_base_prog_key(struct anv_pipeline_stage *stage,
457 const struct anv_device *device)
458 {
459 stage->key.base.robust_flags = anv_get_robust_flags(&stage->rstate);
460 stage->key.base.limit_trig_input_range =
461 device->physical->instance->limit_trig_input_range;
462 }
463
464 static void
populate_vs_prog_key(struct anv_pipeline_stage * stage,const struct anv_device * device)465 populate_vs_prog_key(struct anv_pipeline_stage *stage,
466 const struct anv_device *device)
467 {
468 memset(&stage->key, 0, sizeof(stage->key));
469
470 populate_base_prog_key(stage, device);
471 }
472
473 static void
populate_tcs_prog_key(struct anv_pipeline_stage * stage,const struct anv_device * device,unsigned input_vertices)474 populate_tcs_prog_key(struct anv_pipeline_stage *stage,
475 const struct anv_device *device,
476 unsigned input_vertices)
477 {
478 memset(&stage->key, 0, sizeof(stage->key));
479
480 populate_base_prog_key(stage, device);
481
482 stage->key.tcs.input_vertices = input_vertices;
483 }
484
485 static void
populate_tes_prog_key(struct anv_pipeline_stage * stage,const struct anv_device * device)486 populate_tes_prog_key(struct anv_pipeline_stage *stage,
487 const struct anv_device *device)
488 {
489 memset(&stage->key, 0, sizeof(stage->key));
490
491 populate_base_prog_key(stage, device);
492 }
493
494 static void
populate_gs_prog_key(struct anv_pipeline_stage * stage,const struct anv_device * device)495 populate_gs_prog_key(struct anv_pipeline_stage *stage,
496 const struct anv_device *device)
497 {
498 memset(&stage->key, 0, sizeof(stage->key));
499
500 populate_base_prog_key(stage, device);
501 }
502
503 static bool
pipeline_has_coarse_pixel(const BITSET_WORD * dynamic,const struct vk_multisample_state * ms,const struct vk_fragment_shading_rate_state * fsr)504 pipeline_has_coarse_pixel(const BITSET_WORD *dynamic,
505 const struct vk_multisample_state *ms,
506 const struct vk_fragment_shading_rate_state *fsr)
507 {
508 /* The Vulkan 1.2.199 spec says:
509 *
510 * "If any of the following conditions are met, Cxy' must be set to
511 * {1,1}:
512 *
513 * * If Sample Shading is enabled.
514 * * [...]"
515 *
516 * And "sample shading" is defined as follows:
517 *
518 * "Sample shading is enabled for a graphics pipeline:
519 *
520 * * If the interface of the fragment shader entry point of the
521 * graphics pipeline includes an input variable decorated with
522 * SampleId or SamplePosition. In this case minSampleShadingFactor
523 * takes the value 1.0.
524 *
525 * * Else if the sampleShadingEnable member of the
526 * VkPipelineMultisampleStateCreateInfo structure specified when
527 * creating the graphics pipeline is set to VK_TRUE. In this case
528 * minSampleShadingFactor takes the value of
529 * VkPipelineMultisampleStateCreateInfo::minSampleShading.
530 *
531 * Otherwise, sample shading is considered disabled."
532 *
533 * The first bullet above is handled by the back-end compiler because those
534 * inputs both force per-sample dispatch. The second bullet is handled
535 * here. Note that this sample shading being enabled has nothing to do
536 * with minSampleShading.
537 */
538 if (ms != NULL && ms->sample_shading_enable)
539 return false;
540
541 /* Not dynamic & pipeline has a 1x1 fragment shading rate with no
542 * possibility for element of the pipeline to change the value or fragment
543 * shading rate not specified at all.
544 */
545 if (!BITSET_TEST(dynamic, MESA_VK_DYNAMIC_FSR) &&
546 (fsr == NULL ||
547 (fsr->fragment_size.width <= 1 &&
548 fsr->fragment_size.height <= 1 &&
549 fsr->combiner_ops[0] == VK_FRAGMENT_SHADING_RATE_COMBINER_OP_KEEP_KHR &&
550 fsr->combiner_ops[1] == VK_FRAGMENT_SHADING_RATE_COMBINER_OP_KEEP_KHR)))
551 return false;
552
553 return true;
554 }
555
556 static void
populate_task_prog_key(struct anv_pipeline_stage * stage,const struct anv_device * device)557 populate_task_prog_key(struct anv_pipeline_stage *stage,
558 const struct anv_device *device)
559 {
560 memset(&stage->key, 0, sizeof(stage->key));
561
562 populate_base_prog_key(stage, device);
563 }
564
565 static void
populate_mesh_prog_key(struct anv_pipeline_stage * stage,const struct anv_device * device,bool compact_mue)566 populate_mesh_prog_key(struct anv_pipeline_stage *stage,
567 const struct anv_device *device,
568 bool compact_mue)
569 {
570 memset(&stage->key, 0, sizeof(stage->key));
571
572 populate_base_prog_key(stage, device);
573
574 stage->key.mesh.compact_mue = compact_mue;
575 }
576
577 static uint32_t
rp_color_mask(const struct vk_render_pass_state * rp)578 rp_color_mask(const struct vk_render_pass_state *rp)
579 {
580 if (rp == NULL || !vk_render_pass_state_has_attachment_info(rp))
581 return ((1u << MAX_RTS) - 1);
582
583 uint32_t color_mask = 0;
584 for (uint32_t i = 0; i < rp->color_attachment_count; i++) {
585 if (rp->color_attachment_formats[i] != VK_FORMAT_UNDEFINED)
586 color_mask |= BITFIELD_BIT(i);
587 }
588
589 return color_mask;
590 }
591
592 static void
populate_wm_prog_key(struct anv_pipeline_stage * stage,const struct anv_graphics_base_pipeline * pipeline,const BITSET_WORD * dynamic,const struct vk_multisample_state * ms,const struct vk_fragment_shading_rate_state * fsr,const struct vk_render_pass_state * rp,const enum brw_sometimes is_mesh)593 populate_wm_prog_key(struct anv_pipeline_stage *stage,
594 const struct anv_graphics_base_pipeline *pipeline,
595 const BITSET_WORD *dynamic,
596 const struct vk_multisample_state *ms,
597 const struct vk_fragment_shading_rate_state *fsr,
598 const struct vk_render_pass_state *rp,
599 const enum brw_sometimes is_mesh)
600 {
601 const struct anv_device *device = pipeline->base.device;
602
603 memset(&stage->key, 0, sizeof(stage->key));
604
605 populate_base_prog_key(stage, device);
606
607 struct brw_wm_prog_key *key = &stage->key.wm;
608
609 /* We set this to 0 here and set to the actual value before we call
610 * brw_compile_fs.
611 */
612 key->input_slots_valid = 0;
613
614 /* XXX Vulkan doesn't appear to specify */
615 key->clamp_fragment_color = false;
616
617 key->ignore_sample_mask_out = false;
618
619 assert(rp == NULL || rp->color_attachment_count <= MAX_RTS);
620 /* Consider all inputs as valid until look at the NIR variables. */
621 key->color_outputs_valid = rp_color_mask(rp);
622 key->nr_color_regions = util_last_bit(key->color_outputs_valid);
623
624 /* To reduce possible shader recompilations we would need to know if
625 * there is a SampleMask output variable to compute if we should emit
626 * code to workaround the issue that hardware disables alpha to coverage
627 * when there is SampleMask output.
628 *
629 * If the pipeline we compile the fragment shader in includes the output
630 * interface, then we can be sure whether alpha_coverage is enabled or not.
631 * If we don't have that output interface, then we have to compile the
632 * shader with some conditionals.
633 */
634 if (ms != NULL) {
635 /* VUID-VkGraphicsPipelineCreateInfo-rasterizerDiscardEnable-00751:
636 *
637 * "If the pipeline is being created with fragment shader state,
638 * pMultisampleState must be a valid pointer to a valid
639 * VkPipelineMultisampleStateCreateInfo structure"
640 *
641 * It's also required for the fragment output interface.
642 */
643 key->alpha_to_coverage =
644 ms && ms->alpha_to_coverage_enable ? BRW_ALWAYS : BRW_NEVER;
645 key->multisample_fbo =
646 ms && ms->rasterization_samples > 1 ? BRW_ALWAYS : BRW_NEVER;
647 key->persample_interp =
648 (ms->sample_shading_enable &&
649 (ms->min_sample_shading * ms->rasterization_samples) > 1) ?
650 BRW_ALWAYS : BRW_NEVER;
651
652 /* TODO: We should make this dynamic */
653 if (device->physical->instance->sample_mask_out_opengl_behaviour)
654 key->ignore_sample_mask_out = !key->multisample_fbo;
655 } else {
656 /* Consider all inputs as valid until we look at the NIR variables. */
657 key->color_outputs_valid = (1u << MAX_RTS) - 1;
658 key->nr_color_regions = MAX_RTS;
659
660 key->alpha_to_coverage = BRW_SOMETIMES;
661 key->multisample_fbo = BRW_SOMETIMES;
662 key->persample_interp = BRW_SOMETIMES;
663 }
664
665 key->mesh_input = is_mesh;
666
667 /* Vulkan doesn't support fixed-function alpha test */
668 key->alpha_test_replicate_alpha = false;
669
670 key->coarse_pixel =
671 device->vk.enabled_extensions.KHR_fragment_shading_rate &&
672 pipeline_has_coarse_pixel(dynamic, ms, fsr);
673 }
674
675 static bool
wm_prog_data_dynamic(const struct brw_wm_prog_data * prog_data)676 wm_prog_data_dynamic(const struct brw_wm_prog_data *prog_data)
677 {
678 return prog_data->alpha_to_coverage == BRW_SOMETIMES ||
679 prog_data->coarse_pixel_dispatch == BRW_SOMETIMES ||
680 prog_data->persample_dispatch == BRW_SOMETIMES;
681 }
682
683 static void
populate_cs_prog_key(struct anv_pipeline_stage * stage,const struct anv_device * device)684 populate_cs_prog_key(struct anv_pipeline_stage *stage,
685 const struct anv_device *device)
686 {
687 memset(&stage->key, 0, sizeof(stage->key));
688
689 populate_base_prog_key(stage, device);
690 }
691
692 static void
populate_bs_prog_key(struct anv_pipeline_stage * stage,const struct anv_device * device,uint32_t ray_flags)693 populate_bs_prog_key(struct anv_pipeline_stage *stage,
694 const struct anv_device *device,
695 uint32_t ray_flags)
696 {
697 memset(&stage->key, 0, sizeof(stage->key));
698
699 populate_base_prog_key(stage, device);
700
701 stage->key.bs.pipeline_ray_flags = ray_flags;
702 stage->key.bs.pipeline_ray_flags = ray_flags;
703 }
704
705 static void
anv_stage_write_shader_hash(struct anv_pipeline_stage * stage,const struct anv_device * device)706 anv_stage_write_shader_hash(struct anv_pipeline_stage *stage,
707 const struct anv_device *device)
708 {
709 vk_pipeline_robustness_state_fill(&device->vk,
710 &stage->rstate,
711 stage->pipeline_pNext,
712 stage->info->pNext);
713
714 vk_pipeline_hash_shader_stage(stage->info, &stage->rstate, stage->shader_sha1);
715
716 stage->robust_flags =
717 ((stage->rstate.storage_buffers !=
718 VK_PIPELINE_ROBUSTNESS_BUFFER_BEHAVIOR_DISABLED_EXT) ?
719 BRW_ROBUSTNESS_SSBO : 0) |
720 ((stage->rstate.uniform_buffers !=
721 VK_PIPELINE_ROBUSTNESS_BUFFER_BEHAVIOR_DISABLED_EXT) ?
722 BRW_ROBUSTNESS_UBO : 0);
723
724 /* Use lowest dword of source shader sha1 for shader hash. */
725 stage->source_hash = ((uint32_t*)stage->shader_sha1)[0];
726 }
727
728 static bool
anv_graphics_pipeline_stage_fragment_dynamic(const struct anv_pipeline_stage * stage)729 anv_graphics_pipeline_stage_fragment_dynamic(const struct anv_pipeline_stage *stage)
730 {
731 if (stage->stage != MESA_SHADER_FRAGMENT)
732 return false;
733
734 return stage->key.wm.persample_interp == BRW_SOMETIMES ||
735 stage->key.wm.multisample_fbo == BRW_SOMETIMES ||
736 stage->key.wm.alpha_to_coverage == BRW_SOMETIMES;
737 }
738
739 static void
anv_pipeline_hash_common(struct mesa_sha1 * ctx,const struct anv_pipeline * pipeline)740 anv_pipeline_hash_common(struct mesa_sha1 *ctx,
741 const struct anv_pipeline *pipeline)
742 {
743 struct anv_device *device = pipeline->device;
744
745 _mesa_sha1_update(ctx, pipeline->layout.sha1, sizeof(pipeline->layout.sha1));
746
747 const bool indirect_descriptors = device->physical->indirect_descriptors;
748 _mesa_sha1_update(ctx, &indirect_descriptors, sizeof(indirect_descriptors));
749
750 const bool rba = device->robust_buffer_access;
751 _mesa_sha1_update(ctx, &rba, sizeof(rba));
752
753 const int spilling_rate = device->physical->compiler->spilling_rate;
754 _mesa_sha1_update(ctx, &spilling_rate, sizeof(spilling_rate));
755 }
756
757 static void
anv_pipeline_hash_graphics(struct anv_graphics_base_pipeline * pipeline,struct anv_pipeline_stage * stages,uint32_t view_mask,unsigned char * sha1_out)758 anv_pipeline_hash_graphics(struct anv_graphics_base_pipeline *pipeline,
759 struct anv_pipeline_stage *stages,
760 uint32_t view_mask,
761 unsigned char *sha1_out)
762 {
763 const struct anv_device *device = pipeline->base.device;
764 struct mesa_sha1 ctx;
765 _mesa_sha1_init(&ctx);
766
767 anv_pipeline_hash_common(&ctx, &pipeline->base);
768
769 _mesa_sha1_update(&ctx, &view_mask, sizeof(view_mask));
770
771 for (uint32_t s = 0; s < ANV_GRAPHICS_SHADER_STAGE_COUNT; s++) {
772 if (pipeline->base.active_stages & BITFIELD_BIT(s)) {
773 _mesa_sha1_update(&ctx, stages[s].shader_sha1,
774 sizeof(stages[s].shader_sha1));
775 _mesa_sha1_update(&ctx, &stages[s].key, brw_prog_key_size(s));
776 }
777 }
778
779 if (stages[MESA_SHADER_MESH].info || stages[MESA_SHADER_TASK].info) {
780 const uint8_t afs = device->physical->instance->assume_full_subgroups;
781 _mesa_sha1_update(&ctx, &afs, sizeof(afs));
782 }
783
784 _mesa_sha1_final(&ctx, sha1_out);
785 }
786
787 static void
anv_pipeline_hash_compute(struct anv_compute_pipeline * pipeline,struct anv_pipeline_stage * stage,unsigned char * sha1_out)788 anv_pipeline_hash_compute(struct anv_compute_pipeline *pipeline,
789 struct anv_pipeline_stage *stage,
790 unsigned char *sha1_out)
791 {
792 const struct anv_device *device = pipeline->base.device;
793 struct mesa_sha1 ctx;
794 _mesa_sha1_init(&ctx);
795
796 anv_pipeline_hash_common(&ctx, &pipeline->base);
797
798 const uint8_t afs = device->physical->instance->assume_full_subgroups;
799 _mesa_sha1_update(&ctx, &afs, sizeof(afs));
800
801 _mesa_sha1_update(&ctx, stage->shader_sha1,
802 sizeof(stage->shader_sha1));
803 _mesa_sha1_update(&ctx, &stage->key.cs, sizeof(stage->key.cs));
804
805 _mesa_sha1_final(&ctx, sha1_out);
806 }
807
808 static void
anv_pipeline_hash_ray_tracing_shader(struct anv_ray_tracing_pipeline * pipeline,struct anv_pipeline_stage * stage,unsigned char * sha1_out)809 anv_pipeline_hash_ray_tracing_shader(struct anv_ray_tracing_pipeline *pipeline,
810 struct anv_pipeline_stage *stage,
811 unsigned char *sha1_out)
812 {
813 struct mesa_sha1 ctx;
814 _mesa_sha1_init(&ctx);
815
816 anv_pipeline_hash_common(&ctx, &pipeline->base);
817
818 _mesa_sha1_update(&ctx, stage->shader_sha1, sizeof(stage->shader_sha1));
819 _mesa_sha1_update(&ctx, &stage->key, sizeof(stage->key.bs));
820
821 _mesa_sha1_final(&ctx, sha1_out);
822 }
823
824 static void
anv_pipeline_hash_ray_tracing_combined_shader(struct anv_ray_tracing_pipeline * pipeline,struct anv_pipeline_stage * intersection,struct anv_pipeline_stage * any_hit,unsigned char * sha1_out)825 anv_pipeline_hash_ray_tracing_combined_shader(struct anv_ray_tracing_pipeline *pipeline,
826 struct anv_pipeline_stage *intersection,
827 struct anv_pipeline_stage *any_hit,
828 unsigned char *sha1_out)
829 {
830 struct mesa_sha1 ctx;
831 _mesa_sha1_init(&ctx);
832
833 _mesa_sha1_update(&ctx, pipeline->base.layout.sha1,
834 sizeof(pipeline->base.layout.sha1));
835
836 const bool rba = pipeline->base.device->robust_buffer_access;
837 _mesa_sha1_update(&ctx, &rba, sizeof(rba));
838
839 _mesa_sha1_update(&ctx, intersection->shader_sha1, sizeof(intersection->shader_sha1));
840 _mesa_sha1_update(&ctx, &intersection->key, sizeof(intersection->key.bs));
841 _mesa_sha1_update(&ctx, any_hit->shader_sha1, sizeof(any_hit->shader_sha1));
842 _mesa_sha1_update(&ctx, &any_hit->key, sizeof(any_hit->key.bs));
843
844 _mesa_sha1_final(&ctx, sha1_out);
845 }
846
847 static nir_shader *
anv_pipeline_stage_get_nir(struct anv_pipeline * pipeline,struct vk_pipeline_cache * cache,void * mem_ctx,struct anv_pipeline_stage * stage)848 anv_pipeline_stage_get_nir(struct anv_pipeline *pipeline,
849 struct vk_pipeline_cache *cache,
850 void *mem_ctx,
851 struct anv_pipeline_stage *stage)
852 {
853 const struct brw_compiler *compiler =
854 pipeline->device->physical->compiler;
855 const nir_shader_compiler_options *nir_options =
856 compiler->nir_options[stage->stage];
857 nir_shader *nir;
858
859 nir = anv_device_search_for_nir(pipeline->device, cache,
860 nir_options,
861 stage->shader_sha1,
862 mem_ctx);
863 if (nir) {
864 assert(nir->info.stage == stage->stage);
865 return nir;
866 }
867
868 nir = anv_shader_stage_to_nir(pipeline->device, stage->info,
869 stage->key.base.robust_flags, mem_ctx);
870 if (nir) {
871 anv_device_upload_nir(pipeline->device, cache, nir, stage->shader_sha1);
872 return nir;
873 }
874
875 return NULL;
876 }
877
878 static const struct vk_ycbcr_conversion_state *
lookup_ycbcr_conversion(const void * _sets_layout,uint32_t set,uint32_t binding,uint32_t array_index)879 lookup_ycbcr_conversion(const void *_sets_layout, uint32_t set,
880 uint32_t binding, uint32_t array_index)
881 {
882 const struct anv_pipeline_sets_layout *sets_layout = _sets_layout;
883
884 assert(set < MAX_SETS);
885 assert(binding < sets_layout->set[set].layout->binding_count);
886 const struct anv_descriptor_set_binding_layout *bind_layout =
887 &sets_layout->set[set].layout->binding[binding];
888
889 if (bind_layout->immutable_samplers == NULL)
890 return NULL;
891
892 array_index = MIN2(array_index, bind_layout->array_size - 1);
893
894 const struct anv_sampler *sampler =
895 bind_layout->immutable_samplers[array_index];
896
897 return sampler && sampler->vk.ycbcr_conversion ?
898 &sampler->vk.ycbcr_conversion->state : NULL;
899 }
900
901 static void
shared_type_info(const struct glsl_type * type,unsigned * size,unsigned * align)902 shared_type_info(const struct glsl_type *type, unsigned *size, unsigned *align)
903 {
904 assert(glsl_type_is_vector_or_scalar(type));
905
906 uint32_t comp_size = glsl_type_is_boolean(type)
907 ? 4 : glsl_get_bit_size(type) / 8;
908 unsigned length = glsl_get_vector_elements(type);
909 *size = comp_size * length,
910 *align = comp_size * (length == 3 ? 4 : length);
911 }
912
913 static enum anv_dynamic_push_bits
anv_nir_compute_dynamic_push_bits(nir_shader * shader)914 anv_nir_compute_dynamic_push_bits(nir_shader *shader)
915 {
916 enum anv_dynamic_push_bits ret = 0;
917
918 nir_foreach_function_impl(impl, shader) {
919 nir_foreach_block(block, impl) {
920 nir_foreach_instr(instr, block) {
921 if (instr->type != nir_instr_type_intrinsic)
922 continue;
923
924 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
925 if (intrin->intrinsic != nir_intrinsic_load_push_constant)
926 continue;
927
928 switch (nir_intrinsic_base(intrin)) {
929 case offsetof(struct anv_push_constants, gfx.tcs_input_vertices):
930 ret |= ANV_DYNAMIC_PUSH_INPUT_VERTICES;
931 break;
932
933 default:
934 break;
935 }
936 }
937 }
938 }
939
940 return ret;
941 }
942
943 static void
anv_fixup_subgroup_size(struct anv_device * device,struct shader_info * info)944 anv_fixup_subgroup_size(struct anv_device *device, struct shader_info *info)
945 {
946 switch (info->stage) {
947 case MESA_SHADER_COMPUTE:
948 case MESA_SHADER_TASK:
949 case MESA_SHADER_MESH:
950 break;
951 default:
952 return;
953 }
954
955 unsigned local_size = info->workgroup_size[0] *
956 info->workgroup_size[1] *
957 info->workgroup_size[2];
958
959 /* Games don't always request full subgroups when they should,
960 * which can cause bugs, as they may expect bigger size of the
961 * subgroup than we choose for the execution.
962 */
963 if (device->physical->instance->assume_full_subgroups &&
964 info->uses_wide_subgroup_intrinsics &&
965 info->subgroup_size == SUBGROUP_SIZE_API_CONSTANT &&
966 local_size &&
967 local_size % BRW_SUBGROUP_SIZE == 0)
968 info->subgroup_size = SUBGROUP_SIZE_FULL_SUBGROUPS;
969
970 /* If the client requests that we dispatch full subgroups but doesn't
971 * allow us to pick a subgroup size, we have to smash it to the API
972 * value of 32. Performance will likely be terrible in this case but
973 * there's nothing we can do about that. The client should have chosen
974 * a size.
975 */
976 if (info->subgroup_size == SUBGROUP_SIZE_FULL_SUBGROUPS)
977 info->subgroup_size =
978 device->physical->instance->assume_full_subgroups != 0 ?
979 device->physical->instance->assume_full_subgroups : BRW_SUBGROUP_SIZE;
980
981 /* Cooperative matrix extension requires that all invocations in a subgroup
982 * be active. As a result, when the application does not request a specific
983 * subgroup size, we must use SIMD32.
984 */
985 if (info->stage == MESA_SHADER_COMPUTE && info->cs.has_cooperative_matrix &&
986 info->subgroup_size < SUBGROUP_SIZE_REQUIRE_8) {
987 info->subgroup_size = BRW_SUBGROUP_SIZE;
988 }
989 }
990
991 static void
anv_pipeline_lower_nir(struct anv_pipeline * pipeline,void * mem_ctx,struct anv_pipeline_stage * stage,struct anv_pipeline_sets_layout * layout,uint32_t view_mask,bool use_primitive_replication)992 anv_pipeline_lower_nir(struct anv_pipeline *pipeline,
993 void *mem_ctx,
994 struct anv_pipeline_stage *stage,
995 struct anv_pipeline_sets_layout *layout,
996 uint32_t view_mask,
997 bool use_primitive_replication)
998 {
999 const struct anv_physical_device *pdevice = pipeline->device->physical;
1000 const struct brw_compiler *compiler = pdevice->compiler;
1001
1002 struct brw_stage_prog_data *prog_data = &stage->prog_data.base;
1003 nir_shader *nir = stage->nir;
1004
1005 if (nir->info.stage == MESA_SHADER_FRAGMENT) {
1006 NIR_PASS(_, nir, nir_lower_wpos_center);
1007 NIR_PASS(_, nir, nir_lower_input_attachments,
1008 &(nir_input_attachment_options) {
1009 .use_fragcoord_sysval = true,
1010 .use_layer_id_sysval = true,
1011 });
1012 }
1013
1014 if (nir->info.stage == MESA_SHADER_MESH ||
1015 nir->info.stage == MESA_SHADER_TASK) {
1016 nir_lower_compute_system_values_options options = {
1017 .lower_cs_local_id_to_index = true,
1018 .lower_workgroup_id_to_index = true,
1019 /* nir_lower_idiv generates expensive code */
1020 .shortcut_1d_workgroup_id = compiler->devinfo->verx10 >= 125,
1021 };
1022
1023 NIR_PASS(_, nir, nir_lower_compute_system_values, &options);
1024 }
1025
1026 NIR_PASS(_, nir, nir_vk_lower_ycbcr_tex, lookup_ycbcr_conversion, layout);
1027
1028 if (pipeline->type == ANV_PIPELINE_GRAPHICS ||
1029 pipeline->type == ANV_PIPELINE_GRAPHICS_LIB) {
1030 NIR_PASS(_, nir, anv_nir_lower_multiview, view_mask,
1031 use_primitive_replication);
1032 }
1033
1034 if (nir->info.stage == MESA_SHADER_COMPUTE && nir->info.cs.has_cooperative_matrix) {
1035 anv_fixup_subgroup_size(pipeline->device, &nir->info);
1036 NIR_PASS(_, nir, brw_nir_lower_cmat, nir->info.subgroup_size);
1037 NIR_PASS_V(nir, nir_lower_indirect_derefs, nir_var_function_temp, 16);
1038 }
1039
1040 /* The patch control points are delivered through a push constant when
1041 * dynamic.
1042 */
1043 if (nir->info.stage == MESA_SHADER_TESS_CTRL &&
1044 stage->key.tcs.input_vertices == 0)
1045 NIR_PASS(_, nir, anv_nir_lower_load_patch_vertices_in);
1046
1047 nir_shader_gather_info(nir, nir_shader_get_entrypoint(nir));
1048
1049 NIR_PASS(_, nir, brw_nir_lower_storage_image,
1050 &(struct brw_nir_lower_storage_image_opts) {
1051 /* Anv only supports Gfx9+ which has better defined typed read
1052 * behavior. It allows us to only have to care about lowering
1053 * loads.
1054 */
1055 .devinfo = compiler->devinfo,
1056 .lower_loads = true,
1057 });
1058
1059 NIR_PASS(_, nir, nir_lower_explicit_io, nir_var_mem_global,
1060 nir_address_format_64bit_global);
1061 NIR_PASS(_, nir, nir_lower_explicit_io, nir_var_mem_push_const,
1062 nir_address_format_32bit_offset);
1063
1064 NIR_PASS(_, nir, brw_nir_lower_ray_queries, &pdevice->info);
1065
1066 stage->push_desc_info.used_descriptors =
1067 anv_nir_compute_used_push_descriptors(nir, layout);
1068
1069 struct anv_pipeline_push_map push_map = {};
1070
1071 /* Apply the actual pipeline layout to UBOs, SSBOs, and textures */
1072 NIR_PASS_V(nir, anv_nir_apply_pipeline_layout,
1073 pdevice, stage->key.base.robust_flags,
1074 layout->independent_sets,
1075 layout, &stage->bind_map, &push_map, mem_ctx);
1076
1077 NIR_PASS(_, nir, nir_lower_explicit_io, nir_var_mem_ubo,
1078 anv_nir_ubo_addr_format(pdevice, stage->key.base.robust_flags));
1079 NIR_PASS(_, nir, nir_lower_explicit_io, nir_var_mem_ssbo,
1080 anv_nir_ssbo_addr_format(pdevice, stage->key.base.robust_flags));
1081
1082 /* First run copy-prop to get rid of all of the vec() that address
1083 * calculations often create and then constant-fold so that, when we
1084 * get to anv_nir_lower_ubo_loads, we can detect constant offsets.
1085 */
1086 bool progress;
1087 do {
1088 progress = false;
1089 NIR_PASS(progress, nir, nir_opt_algebraic);
1090 NIR_PASS(progress, nir, nir_copy_prop);
1091 NIR_PASS(progress, nir, nir_opt_constant_folding);
1092 NIR_PASS(progress, nir, nir_opt_dce);
1093 } while (progress);
1094
1095 /* Required for nir_divergence_analysis() which is needed for
1096 * anv_nir_lower_ubo_loads.
1097 */
1098 NIR_PASS(_, nir, nir_convert_to_lcssa, true, true);
1099 nir_divergence_analysis(nir);
1100
1101 NIR_PASS(_, nir, anv_nir_lower_ubo_loads);
1102
1103 NIR_PASS(_, nir, nir_opt_remove_phis);
1104
1105 enum nir_lower_non_uniform_access_type lower_non_uniform_access_types =
1106 nir_lower_non_uniform_texture_access |
1107 nir_lower_non_uniform_image_access |
1108 nir_lower_non_uniform_get_ssbo_size;
1109
1110 /* In practice, most shaders do not have non-uniform-qualified
1111 * accesses (see
1112 * https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/17558#note_1475069)
1113 * thus a cheaper and likely to fail check is run first.
1114 */
1115 if (nir_has_non_uniform_access(nir, lower_non_uniform_access_types)) {
1116 NIR_PASS(_, nir, nir_opt_non_uniform_access);
1117
1118 /* We don't support non-uniform UBOs and non-uniform SSBO access is
1119 * handled naturally by falling back to A64 messages.
1120 */
1121 NIR_PASS(_, nir, nir_lower_non_uniform_access,
1122 &(nir_lower_non_uniform_access_options) {
1123 .types = lower_non_uniform_access_types,
1124 .callback = NULL,
1125 });
1126
1127 NIR_PASS(_, nir, intel_nir_lower_non_uniform_resource_intel);
1128 NIR_PASS(_, nir, intel_nir_cleanup_resource_intel);
1129 NIR_PASS(_, nir, nir_opt_dce);
1130 }
1131
1132 NIR_PASS_V(nir, anv_nir_update_resource_intel_block);
1133
1134 stage->dynamic_push_values = anv_nir_compute_dynamic_push_bits(nir);
1135
1136 NIR_PASS_V(nir, anv_nir_compute_push_layout,
1137 pdevice, stage->key.base.robust_flags,
1138 anv_graphics_pipeline_stage_fragment_dynamic(stage),
1139 prog_data, &stage->bind_map, &push_map, mem_ctx);
1140
1141 NIR_PASS_V(nir, anv_nir_lower_resource_intel, pdevice,
1142 pipeline->layout.type);
1143
1144 if (gl_shader_stage_uses_workgroup(nir->info.stage)) {
1145 if (!nir->info.shared_memory_explicit_layout) {
1146 NIR_PASS(_, nir, nir_lower_vars_to_explicit_types,
1147 nir_var_mem_shared, shared_type_info);
1148 }
1149
1150 NIR_PASS(_, nir, nir_lower_explicit_io,
1151 nir_var_mem_shared, nir_address_format_32bit_offset);
1152
1153 if (nir->info.zero_initialize_shared_memory &&
1154 nir->info.shared_size > 0) {
1155 /* The effective Shared Local Memory size is at least 1024 bytes and
1156 * is always rounded to a power of two, so it is OK to align the size
1157 * used by the shader to chunk_size -- which does simplify the logic.
1158 */
1159 const unsigned chunk_size = 16;
1160 const unsigned shared_size = ALIGN(nir->info.shared_size, chunk_size);
1161 assert(shared_size <=
1162 intel_calculate_slm_size(compiler->devinfo->ver, nir->info.shared_size));
1163
1164 NIR_PASS(_, nir, nir_zero_initialize_shared_memory,
1165 shared_size, chunk_size);
1166 }
1167 }
1168
1169 if (gl_shader_stage_is_compute(nir->info.stage) ||
1170 gl_shader_stage_is_mesh(nir->info.stage)) {
1171 NIR_PASS(_, nir, brw_nir_lower_cs_intrinsics, compiler->devinfo,
1172 &stage->prog_data.cs);
1173 }
1174
1175 stage->push_desc_info.used_set_buffer =
1176 anv_nir_loads_push_desc_buffer(nir, layout, &stage->bind_map);
1177 stage->push_desc_info.fully_promoted_ubo_descriptors =
1178 anv_nir_push_desc_ubo_fully_promoted(nir, layout, &stage->bind_map);
1179
1180 stage->nir = nir;
1181 }
1182
1183 static void
anv_pipeline_link_vs(const struct brw_compiler * compiler,struct anv_pipeline_stage * vs_stage,struct anv_pipeline_stage * next_stage)1184 anv_pipeline_link_vs(const struct brw_compiler *compiler,
1185 struct anv_pipeline_stage *vs_stage,
1186 struct anv_pipeline_stage *next_stage)
1187 {
1188 if (next_stage)
1189 brw_nir_link_shaders(compiler, vs_stage->nir, next_stage->nir);
1190 }
1191
1192 static void
anv_pipeline_compile_vs(const struct brw_compiler * compiler,void * mem_ctx,struct anv_graphics_base_pipeline * pipeline,struct anv_pipeline_stage * vs_stage,uint32_t view_mask)1193 anv_pipeline_compile_vs(const struct brw_compiler *compiler,
1194 void *mem_ctx,
1195 struct anv_graphics_base_pipeline *pipeline,
1196 struct anv_pipeline_stage *vs_stage,
1197 uint32_t view_mask)
1198 {
1199 /* When using Primitive Replication for multiview, each view gets its own
1200 * position slot.
1201 */
1202 uint32_t pos_slots =
1203 (vs_stage->nir->info.per_view_outputs & VARYING_BIT_POS) ?
1204 MAX2(1, util_bitcount(view_mask)) : 1;
1205
1206 /* Only position is allowed to be per-view */
1207 assert(!(vs_stage->nir->info.per_view_outputs & ~VARYING_BIT_POS));
1208
1209 brw_compute_vue_map(compiler->devinfo,
1210 &vs_stage->prog_data.vs.base.vue_map,
1211 vs_stage->nir->info.outputs_written,
1212 vs_stage->nir->info.separate_shader,
1213 pos_slots);
1214
1215 vs_stage->num_stats = 1;
1216
1217 struct brw_compile_vs_params params = {
1218 .base = {
1219 .nir = vs_stage->nir,
1220 .stats = vs_stage->stats,
1221 .log_data = pipeline->base.device,
1222 .mem_ctx = mem_ctx,
1223 .source_hash = vs_stage->source_hash,
1224 },
1225 .key = &vs_stage->key.vs,
1226 .prog_data = &vs_stage->prog_data.vs,
1227 };
1228
1229 vs_stage->code = brw_compile_vs(compiler, ¶ms);
1230 }
1231
1232 static void
merge_tess_info(struct shader_info * tes_info,const struct shader_info * tcs_info)1233 merge_tess_info(struct shader_info *tes_info,
1234 const struct shader_info *tcs_info)
1235 {
1236 /* The Vulkan 1.0.38 spec, section 21.1 Tessellator says:
1237 *
1238 * "PointMode. Controls generation of points rather than triangles
1239 * or lines. This functionality defaults to disabled, and is
1240 * enabled if either shader stage includes the execution mode.
1241 *
1242 * and about Triangles, Quads, IsoLines, VertexOrderCw, VertexOrderCcw,
1243 * PointMode, SpacingEqual, SpacingFractionalEven, SpacingFractionalOdd,
1244 * and OutputVertices, it says:
1245 *
1246 * "One mode must be set in at least one of the tessellation
1247 * shader stages."
1248 *
1249 * So, the fields can be set in either the TCS or TES, but they must
1250 * agree if set in both. Our backend looks at TES, so bitwise-or in
1251 * the values from the TCS.
1252 */
1253 assert(tcs_info->tess.tcs_vertices_out == 0 ||
1254 tes_info->tess.tcs_vertices_out == 0 ||
1255 tcs_info->tess.tcs_vertices_out == tes_info->tess.tcs_vertices_out);
1256 tes_info->tess.tcs_vertices_out |= tcs_info->tess.tcs_vertices_out;
1257
1258 assert(tcs_info->tess.spacing == TESS_SPACING_UNSPECIFIED ||
1259 tes_info->tess.spacing == TESS_SPACING_UNSPECIFIED ||
1260 tcs_info->tess.spacing == tes_info->tess.spacing);
1261 tes_info->tess.spacing |= tcs_info->tess.spacing;
1262
1263 assert(tcs_info->tess._primitive_mode == 0 ||
1264 tes_info->tess._primitive_mode == 0 ||
1265 tcs_info->tess._primitive_mode == tes_info->tess._primitive_mode);
1266 tes_info->tess._primitive_mode |= tcs_info->tess._primitive_mode;
1267 tes_info->tess.ccw |= tcs_info->tess.ccw;
1268 tes_info->tess.point_mode |= tcs_info->tess.point_mode;
1269 }
1270
1271 static void
anv_pipeline_link_tcs(const struct brw_compiler * compiler,struct anv_pipeline_stage * tcs_stage,struct anv_pipeline_stage * tes_stage)1272 anv_pipeline_link_tcs(const struct brw_compiler *compiler,
1273 struct anv_pipeline_stage *tcs_stage,
1274 struct anv_pipeline_stage *tes_stage)
1275 {
1276 assert(tes_stage && tes_stage->stage == MESA_SHADER_TESS_EVAL);
1277
1278 brw_nir_link_shaders(compiler, tcs_stage->nir, tes_stage->nir);
1279
1280 nir_lower_patch_vertices(tes_stage->nir,
1281 tcs_stage->nir->info.tess.tcs_vertices_out,
1282 NULL);
1283
1284 /* Copy TCS info into the TES info */
1285 merge_tess_info(&tes_stage->nir->info, &tcs_stage->nir->info);
1286
1287 /* Whacking the key after cache lookup is a bit sketchy, but all of
1288 * this comes from the SPIR-V, which is part of the hash used for the
1289 * pipeline cache. So it should be safe.
1290 */
1291 tcs_stage->key.tcs._tes_primitive_mode =
1292 tes_stage->nir->info.tess._primitive_mode;
1293 }
1294
1295 static void
anv_pipeline_compile_tcs(const struct brw_compiler * compiler,void * mem_ctx,struct anv_device * device,struct anv_pipeline_stage * tcs_stage,struct anv_pipeline_stage * prev_stage)1296 anv_pipeline_compile_tcs(const struct brw_compiler *compiler,
1297 void *mem_ctx,
1298 struct anv_device *device,
1299 struct anv_pipeline_stage *tcs_stage,
1300 struct anv_pipeline_stage *prev_stage)
1301 {
1302 tcs_stage->key.tcs.outputs_written =
1303 tcs_stage->nir->info.outputs_written;
1304 tcs_stage->key.tcs.patch_outputs_written =
1305 tcs_stage->nir->info.patch_outputs_written;
1306
1307 tcs_stage->num_stats = 1;
1308
1309 struct brw_compile_tcs_params params = {
1310 .base = {
1311 .nir = tcs_stage->nir,
1312 .stats = tcs_stage->stats,
1313 .log_data = device,
1314 .mem_ctx = mem_ctx,
1315 .source_hash = tcs_stage->source_hash,
1316 },
1317 .key = &tcs_stage->key.tcs,
1318 .prog_data = &tcs_stage->prog_data.tcs,
1319 };
1320
1321 tcs_stage->code = brw_compile_tcs(compiler, ¶ms);
1322 }
1323
1324 static void
anv_pipeline_link_tes(const struct brw_compiler * compiler,struct anv_pipeline_stage * tes_stage,struct anv_pipeline_stage * next_stage)1325 anv_pipeline_link_tes(const struct brw_compiler *compiler,
1326 struct anv_pipeline_stage *tes_stage,
1327 struct anv_pipeline_stage *next_stage)
1328 {
1329 if (next_stage)
1330 brw_nir_link_shaders(compiler, tes_stage->nir, next_stage->nir);
1331 }
1332
1333 static void
anv_pipeline_compile_tes(const struct brw_compiler * compiler,void * mem_ctx,struct anv_device * device,struct anv_pipeline_stage * tes_stage,struct anv_pipeline_stage * tcs_stage)1334 anv_pipeline_compile_tes(const struct brw_compiler *compiler,
1335 void *mem_ctx,
1336 struct anv_device *device,
1337 struct anv_pipeline_stage *tes_stage,
1338 struct anv_pipeline_stage *tcs_stage)
1339 {
1340 tes_stage->key.tes.inputs_read =
1341 tcs_stage->nir->info.outputs_written;
1342 tes_stage->key.tes.patch_inputs_read =
1343 tcs_stage->nir->info.patch_outputs_written;
1344
1345 tes_stage->num_stats = 1;
1346
1347 struct brw_compile_tes_params params = {
1348 .base = {
1349 .nir = tes_stage->nir,
1350 .stats = tes_stage->stats,
1351 .log_data = device,
1352 .mem_ctx = mem_ctx,
1353 .source_hash = tes_stage->source_hash,
1354 },
1355 .key = &tes_stage->key.tes,
1356 .prog_data = &tes_stage->prog_data.tes,
1357 .input_vue_map = &tcs_stage->prog_data.tcs.base.vue_map,
1358 };
1359
1360 tes_stage->code = brw_compile_tes(compiler, ¶ms);
1361 }
1362
1363 static void
anv_pipeline_link_gs(const struct brw_compiler * compiler,struct anv_pipeline_stage * gs_stage,struct anv_pipeline_stage * next_stage)1364 anv_pipeline_link_gs(const struct brw_compiler *compiler,
1365 struct anv_pipeline_stage *gs_stage,
1366 struct anv_pipeline_stage *next_stage)
1367 {
1368 if (next_stage)
1369 brw_nir_link_shaders(compiler, gs_stage->nir, next_stage->nir);
1370 }
1371
1372 static void
anv_pipeline_compile_gs(const struct brw_compiler * compiler,void * mem_ctx,struct anv_device * device,struct anv_pipeline_stage * gs_stage,struct anv_pipeline_stage * prev_stage)1373 anv_pipeline_compile_gs(const struct brw_compiler *compiler,
1374 void *mem_ctx,
1375 struct anv_device *device,
1376 struct anv_pipeline_stage *gs_stage,
1377 struct anv_pipeline_stage *prev_stage)
1378 {
1379 brw_compute_vue_map(compiler->devinfo,
1380 &gs_stage->prog_data.gs.base.vue_map,
1381 gs_stage->nir->info.outputs_written,
1382 gs_stage->nir->info.separate_shader, 1);
1383
1384 gs_stage->num_stats = 1;
1385
1386 struct brw_compile_gs_params params = {
1387 .base = {
1388 .nir = gs_stage->nir,
1389 .stats = gs_stage->stats,
1390 .log_data = device,
1391 .mem_ctx = mem_ctx,
1392 .source_hash = gs_stage->source_hash,
1393 },
1394 .key = &gs_stage->key.gs,
1395 .prog_data = &gs_stage->prog_data.gs,
1396 };
1397
1398 gs_stage->code = brw_compile_gs(compiler, ¶ms);
1399 }
1400
1401 static void
anv_pipeline_link_task(const struct brw_compiler * compiler,struct anv_pipeline_stage * task_stage,struct anv_pipeline_stage * next_stage)1402 anv_pipeline_link_task(const struct brw_compiler *compiler,
1403 struct anv_pipeline_stage *task_stage,
1404 struct anv_pipeline_stage *next_stage)
1405 {
1406 assert(next_stage);
1407 assert(next_stage->stage == MESA_SHADER_MESH);
1408 brw_nir_link_shaders(compiler, task_stage->nir, next_stage->nir);
1409 }
1410
1411 static void
anv_pipeline_compile_task(const struct brw_compiler * compiler,void * mem_ctx,struct anv_device * device,struct anv_pipeline_stage * task_stage)1412 anv_pipeline_compile_task(const struct brw_compiler *compiler,
1413 void *mem_ctx,
1414 struct anv_device *device,
1415 struct anv_pipeline_stage *task_stage)
1416 {
1417 task_stage->num_stats = 1;
1418
1419 struct brw_compile_task_params params = {
1420 .base = {
1421 .nir = task_stage->nir,
1422 .stats = task_stage->stats,
1423 .log_data = device,
1424 .mem_ctx = mem_ctx,
1425 .source_hash = task_stage->source_hash,
1426 },
1427 .key = &task_stage->key.task,
1428 .prog_data = &task_stage->prog_data.task,
1429 };
1430
1431 task_stage->code = brw_compile_task(compiler, ¶ms);
1432 }
1433
1434 static void
anv_pipeline_link_mesh(const struct brw_compiler * compiler,struct anv_pipeline_stage * mesh_stage,struct anv_pipeline_stage * next_stage)1435 anv_pipeline_link_mesh(const struct brw_compiler *compiler,
1436 struct anv_pipeline_stage *mesh_stage,
1437 struct anv_pipeline_stage *next_stage)
1438 {
1439 if (next_stage) {
1440 brw_nir_link_shaders(compiler, mesh_stage->nir, next_stage->nir);
1441 }
1442 }
1443
1444 static void
anv_pipeline_compile_mesh(const struct brw_compiler * compiler,void * mem_ctx,struct anv_device * device,struct anv_pipeline_stage * mesh_stage,struct anv_pipeline_stage * prev_stage)1445 anv_pipeline_compile_mesh(const struct brw_compiler *compiler,
1446 void *mem_ctx,
1447 struct anv_device *device,
1448 struct anv_pipeline_stage *mesh_stage,
1449 struct anv_pipeline_stage *prev_stage)
1450 {
1451 mesh_stage->num_stats = 1;
1452
1453 struct brw_compile_mesh_params params = {
1454 .base = {
1455 .nir = mesh_stage->nir,
1456 .stats = mesh_stage->stats,
1457 .log_data = device,
1458 .mem_ctx = mem_ctx,
1459 .source_hash = mesh_stage->source_hash,
1460 },
1461 .key = &mesh_stage->key.mesh,
1462 .prog_data = &mesh_stage->prog_data.mesh,
1463 };
1464
1465 if (prev_stage) {
1466 assert(prev_stage->stage == MESA_SHADER_TASK);
1467 params.tue_map = &prev_stage->prog_data.task.map;
1468 }
1469
1470 mesh_stage->code = brw_compile_mesh(compiler, ¶ms);
1471 }
1472
1473 static void
anv_pipeline_link_fs(const struct brw_compiler * compiler,struct anv_pipeline_stage * stage,const struct vk_render_pass_state * rp)1474 anv_pipeline_link_fs(const struct brw_compiler *compiler,
1475 struct anv_pipeline_stage *stage,
1476 const struct vk_render_pass_state *rp)
1477 {
1478 /* Initially the valid outputs value is set to all possible render targets
1479 * valid (see populate_wm_prog_key()), before we look at the shader
1480 * variables. Here we look at the output variables of the shader an compute
1481 * a correct number of render target outputs.
1482 */
1483 stage->key.wm.color_outputs_valid = 0;
1484 nir_foreach_shader_out_variable_safe(var, stage->nir) {
1485 if (var->data.location < FRAG_RESULT_DATA0)
1486 continue;
1487
1488 const unsigned rt = var->data.location - FRAG_RESULT_DATA0;
1489 const unsigned array_len =
1490 glsl_type_is_array(var->type) ? glsl_get_length(var->type) : 1;
1491 assert(rt + array_len <= MAX_RTS);
1492
1493 stage->key.wm.color_outputs_valid |= BITFIELD_RANGE(rt, array_len);
1494 }
1495 stage->key.wm.color_outputs_valid &= rp_color_mask(rp);
1496 stage->key.wm.nr_color_regions =
1497 util_last_bit(stage->key.wm.color_outputs_valid);
1498
1499 unsigned num_rt_bindings;
1500 struct anv_pipeline_binding rt_bindings[MAX_RTS];
1501 if (stage->key.wm.nr_color_regions > 0) {
1502 assert(stage->key.wm.nr_color_regions <= MAX_RTS);
1503 for (unsigned rt = 0; rt < stage->key.wm.nr_color_regions; rt++) {
1504 if (stage->key.wm.color_outputs_valid & BITFIELD_BIT(rt)) {
1505 rt_bindings[rt] = (struct anv_pipeline_binding) {
1506 .set = ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS,
1507 .index = rt,
1508 .binding = UINT32_MAX,
1509
1510 };
1511 } else {
1512 /* Setup a null render target */
1513 rt_bindings[rt] = (struct anv_pipeline_binding) {
1514 .set = ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS,
1515 .index = UINT32_MAX,
1516 .binding = UINT32_MAX,
1517 };
1518 }
1519 }
1520 num_rt_bindings = stage->key.wm.nr_color_regions;
1521 } else {
1522 /* Setup a null render target */
1523 rt_bindings[0] = (struct anv_pipeline_binding) {
1524 .set = ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS,
1525 .index = UINT32_MAX,
1526 };
1527 num_rt_bindings = 1;
1528 }
1529
1530 assert(num_rt_bindings <= MAX_RTS);
1531 assert(stage->bind_map.surface_count == 0);
1532 typed_memcpy(stage->bind_map.surface_to_descriptor,
1533 rt_bindings, num_rt_bindings);
1534 stage->bind_map.surface_count += num_rt_bindings;
1535 }
1536
1537 static void
anv_pipeline_compile_fs(const struct brw_compiler * compiler,void * mem_ctx,struct anv_device * device,struct anv_pipeline_stage * fs_stage,struct anv_pipeline_stage * prev_stage,struct anv_graphics_base_pipeline * pipeline,uint32_t view_mask,bool use_primitive_replication)1538 anv_pipeline_compile_fs(const struct brw_compiler *compiler,
1539 void *mem_ctx,
1540 struct anv_device *device,
1541 struct anv_pipeline_stage *fs_stage,
1542 struct anv_pipeline_stage *prev_stage,
1543 struct anv_graphics_base_pipeline *pipeline,
1544 uint32_t view_mask,
1545 bool use_primitive_replication)
1546 {
1547 /* When using Primitive Replication for multiview, each view gets its own
1548 * position slot.
1549 */
1550 uint32_t pos_slots = use_primitive_replication ?
1551 MAX2(1, util_bitcount(view_mask)) : 1;
1552
1553 /* If we have a previous stage we can use that to deduce valid slots.
1554 * Otherwise, rely on inputs of the input shader.
1555 */
1556 if (prev_stage) {
1557 fs_stage->key.wm.input_slots_valid =
1558 prev_stage->prog_data.vue.vue_map.slots_valid;
1559 } else {
1560 struct intel_vue_map prev_vue_map;
1561 brw_compute_vue_map(compiler->devinfo,
1562 &prev_vue_map,
1563 fs_stage->nir->info.inputs_read,
1564 fs_stage->nir->info.separate_shader,
1565 pos_slots);
1566
1567 fs_stage->key.wm.input_slots_valid = prev_vue_map.slots_valid;
1568 }
1569
1570 struct brw_compile_fs_params params = {
1571 .base = {
1572 .nir = fs_stage->nir,
1573 .stats = fs_stage->stats,
1574 .log_data = device,
1575 .mem_ctx = mem_ctx,
1576 .source_hash = fs_stage->source_hash,
1577 },
1578 .key = &fs_stage->key.wm,
1579 .prog_data = &fs_stage->prog_data.wm,
1580
1581 .allow_spilling = true,
1582 .max_polygons = UCHAR_MAX,
1583 };
1584
1585 if (prev_stage && prev_stage->stage == MESA_SHADER_MESH) {
1586 params.mue_map = &prev_stage->prog_data.mesh.map;
1587 /* TODO(mesh): Slots valid, do we even use/rely on it? */
1588 }
1589
1590 fs_stage->code = brw_compile_fs(compiler, ¶ms);
1591
1592 fs_stage->num_stats = (uint32_t)!!fs_stage->prog_data.wm.dispatch_multi +
1593 (uint32_t)fs_stage->prog_data.wm.dispatch_8 +
1594 (uint32_t)fs_stage->prog_data.wm.dispatch_16 +
1595 (uint32_t)fs_stage->prog_data.wm.dispatch_32;
1596 assert(fs_stage->num_stats <= ARRAY_SIZE(fs_stage->stats));
1597 }
1598
1599 static void
anv_pipeline_add_executable(struct anv_pipeline * pipeline,struct anv_pipeline_stage * stage,struct brw_compile_stats * stats,uint32_t code_offset)1600 anv_pipeline_add_executable(struct anv_pipeline *pipeline,
1601 struct anv_pipeline_stage *stage,
1602 struct brw_compile_stats *stats,
1603 uint32_t code_offset)
1604 {
1605 char *nir = NULL;
1606 if (stage->nir &&
1607 (pipeline->flags &
1608 VK_PIPELINE_CREATE_2_CAPTURE_INTERNAL_REPRESENTATIONS_BIT_KHR)) {
1609 nir = nir_shader_as_str(stage->nir, pipeline->mem_ctx);
1610 }
1611
1612 char *disasm = NULL;
1613 if (stage->code &&
1614 (pipeline->flags &
1615 VK_PIPELINE_CREATE_2_CAPTURE_INTERNAL_REPRESENTATIONS_BIT_KHR)) {
1616 char *stream_data = NULL;
1617 size_t stream_size = 0;
1618 FILE *stream = open_memstream(&stream_data, &stream_size);
1619
1620 uint32_t push_size = 0;
1621 for (unsigned i = 0; i < 4; i++)
1622 push_size += stage->bind_map.push_ranges[i].length;
1623 if (push_size > 0) {
1624 fprintf(stream, "Push constant ranges:\n");
1625 for (unsigned i = 0; i < 4; i++) {
1626 if (stage->bind_map.push_ranges[i].length == 0)
1627 continue;
1628
1629 fprintf(stream, " RANGE%d (%dB): ", i,
1630 stage->bind_map.push_ranges[i].length * 32);
1631
1632 switch (stage->bind_map.push_ranges[i].set) {
1633 case ANV_DESCRIPTOR_SET_NULL:
1634 fprintf(stream, "NULL");
1635 break;
1636
1637 case ANV_DESCRIPTOR_SET_PUSH_CONSTANTS:
1638 fprintf(stream, "Vulkan push constants and API params");
1639 break;
1640
1641 case ANV_DESCRIPTOR_SET_DESCRIPTORS:
1642 fprintf(stream, "Descriptor buffer for set %d (start=%dB)",
1643 stage->bind_map.push_ranges[i].index,
1644 stage->bind_map.push_ranges[i].start * 32);
1645 break;
1646
1647 case ANV_DESCRIPTOR_SET_NUM_WORK_GROUPS:
1648 unreachable("gl_NumWorkgroups is never pushed");
1649
1650 case ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS:
1651 unreachable("Color attachments can't be pushed");
1652
1653 default:
1654 fprintf(stream, "UBO (set=%d binding=%d start=%dB)",
1655 stage->bind_map.push_ranges[i].set,
1656 stage->bind_map.push_ranges[i].index,
1657 stage->bind_map.push_ranges[i].start * 32);
1658 break;
1659 }
1660 fprintf(stream, "\n");
1661 }
1662 fprintf(stream, "\n");
1663 }
1664
1665 /* Creating this is far cheaper than it looks. It's perfectly fine to
1666 * do it for every binary.
1667 */
1668 brw_disassemble_with_errors(&pipeline->device->physical->compiler->isa,
1669 stage->code, code_offset, stream);
1670
1671 fclose(stream);
1672
1673 /* Copy it to a ralloc'd thing */
1674 disasm = ralloc_size(pipeline->mem_ctx, stream_size + 1);
1675 memcpy(disasm, stream_data, stream_size);
1676 disasm[stream_size] = 0;
1677
1678 free(stream_data);
1679 }
1680
1681 const struct anv_pipeline_executable exe = {
1682 .stage = stage->stage,
1683 .stats = *stats,
1684 .nir = nir,
1685 .disasm = disasm,
1686 };
1687 util_dynarray_append(&pipeline->executables,
1688 struct anv_pipeline_executable, exe);
1689 }
1690
1691 static void
anv_pipeline_add_executables(struct anv_pipeline * pipeline,struct anv_pipeline_stage * stage)1692 anv_pipeline_add_executables(struct anv_pipeline *pipeline,
1693 struct anv_pipeline_stage *stage)
1694 {
1695 if (stage->stage == MESA_SHADER_FRAGMENT) {
1696 /* We pull the prog data and stats out of the anv_shader_bin because
1697 * the anv_pipeline_stage may not be fully populated if we successfully
1698 * looked up the shader in a cache.
1699 */
1700 const struct brw_wm_prog_data *wm_prog_data =
1701 (const struct brw_wm_prog_data *)stage->bin->prog_data;
1702 struct brw_compile_stats *stats = stage->bin->stats;
1703
1704 if (wm_prog_data->dispatch_8 ||
1705 wm_prog_data->dispatch_multi) {
1706 anv_pipeline_add_executable(pipeline, stage, stats++, 0);
1707 }
1708
1709 if (wm_prog_data->dispatch_16) {
1710 anv_pipeline_add_executable(pipeline, stage, stats++,
1711 wm_prog_data->prog_offset_16);
1712 }
1713
1714 if (wm_prog_data->dispatch_32) {
1715 anv_pipeline_add_executable(pipeline, stage, stats++,
1716 wm_prog_data->prog_offset_32);
1717 }
1718 } else {
1719 anv_pipeline_add_executable(pipeline, stage, stage->bin->stats, 0);
1720 }
1721 }
1722
1723 static void
anv_pipeline_account_shader(struct anv_pipeline * pipeline,struct anv_shader_bin * shader)1724 anv_pipeline_account_shader(struct anv_pipeline *pipeline,
1725 struct anv_shader_bin *shader)
1726 {
1727 pipeline->scratch_size = MAX2(pipeline->scratch_size,
1728 shader->prog_data->total_scratch);
1729
1730 pipeline->ray_queries = MAX2(pipeline->ray_queries,
1731 shader->prog_data->ray_queries);
1732
1733 if (shader->push_desc_info.used_set_buffer) {
1734 pipeline->use_push_descriptor_buffer |=
1735 mesa_to_vk_shader_stage(shader->stage);
1736 }
1737 if (shader->push_desc_info.used_descriptors &
1738 ~shader->push_desc_info.fully_promoted_ubo_descriptors)
1739 pipeline->use_push_descriptor |= mesa_to_vk_shader_stage(shader->stage);
1740 }
1741
1742 /* This function return true if a shader should not be looked at because of
1743 * fast linking. Instead we should use the shader binaries provided by
1744 * libraries.
1745 */
1746 static bool
anv_graphics_pipeline_skip_shader_compile(struct anv_graphics_base_pipeline * pipeline,struct anv_pipeline_stage * stages,bool link_optimize,gl_shader_stage stage)1747 anv_graphics_pipeline_skip_shader_compile(struct anv_graphics_base_pipeline *pipeline,
1748 struct anv_pipeline_stage *stages,
1749 bool link_optimize,
1750 gl_shader_stage stage)
1751 {
1752 /* Always skip non active stages */
1753 if (!anv_pipeline_base_has_stage(pipeline, stage))
1754 return true;
1755
1756 /* When link optimizing, consider all stages */
1757 if (link_optimize)
1758 return false;
1759
1760 /* Otherwise check if the stage was specified through
1761 * VkGraphicsPipelineCreateInfo
1762 */
1763 assert(stages[stage].info != NULL || stages[stage].imported.bin != NULL);
1764 return stages[stage].info == NULL;
1765 }
1766
1767 static void
anv_graphics_pipeline_init_keys(struct anv_graphics_base_pipeline * pipeline,const struct vk_graphics_pipeline_state * state,struct anv_pipeline_stage * stages)1768 anv_graphics_pipeline_init_keys(struct anv_graphics_base_pipeline *pipeline,
1769 const struct vk_graphics_pipeline_state *state,
1770 struct anv_pipeline_stage *stages)
1771 {
1772 for (uint32_t s = 0; s < ANV_GRAPHICS_SHADER_STAGE_COUNT; s++) {
1773 if (!anv_pipeline_base_has_stage(pipeline, s))
1774 continue;
1775
1776 int64_t stage_start = os_time_get_nano();
1777
1778 const struct anv_device *device = pipeline->base.device;
1779 switch (stages[s].stage) {
1780 case MESA_SHADER_VERTEX:
1781 populate_vs_prog_key(&stages[s], device);
1782 break;
1783 case MESA_SHADER_TESS_CTRL:
1784 populate_tcs_prog_key(&stages[s],
1785 device,
1786 BITSET_TEST(state->dynamic,
1787 MESA_VK_DYNAMIC_TS_PATCH_CONTROL_POINTS) ?
1788 0 : state->ts->patch_control_points);
1789 break;
1790 case MESA_SHADER_TESS_EVAL:
1791 populate_tes_prog_key(&stages[s], device);
1792 break;
1793 case MESA_SHADER_GEOMETRY:
1794 populate_gs_prog_key(&stages[s], device);
1795 break;
1796 case MESA_SHADER_FRAGMENT: {
1797 /* Assume rasterization enabled in any of the following case :
1798 *
1799 * - We're a pipeline library without pre-rasterization information
1800 *
1801 * - Rasterization is not disabled in the non dynamic state
1802 *
1803 * - Rasterization disable is dynamic
1804 */
1805 const bool raster_enabled =
1806 state->rs == NULL ||
1807 !state->rs->rasterizer_discard_enable ||
1808 BITSET_TEST(state->dynamic, MESA_VK_DYNAMIC_RS_RASTERIZER_DISCARD_ENABLE);
1809 enum brw_sometimes is_mesh = BRW_NEVER;
1810 if (device->vk.enabled_extensions.EXT_mesh_shader) {
1811 if (anv_pipeline_base_has_stage(pipeline, MESA_SHADER_VERTEX))
1812 is_mesh = BRW_NEVER;
1813 else if (anv_pipeline_base_has_stage(pipeline, MESA_SHADER_MESH))
1814 is_mesh = BRW_ALWAYS;
1815 else {
1816 assert(pipeline->base.type == ANV_PIPELINE_GRAPHICS_LIB);
1817 is_mesh = BRW_SOMETIMES;
1818 }
1819 }
1820 populate_wm_prog_key(&stages[s],
1821 pipeline,
1822 state->dynamic,
1823 raster_enabled ? state->ms : NULL,
1824 state->fsr, state->rp, is_mesh);
1825 break;
1826 }
1827
1828 case MESA_SHADER_TASK:
1829 populate_task_prog_key(&stages[s], device);
1830 break;
1831
1832 case MESA_SHADER_MESH: {
1833 const bool compact_mue =
1834 !(pipeline->base.type == ANV_PIPELINE_GRAPHICS_LIB &&
1835 !anv_pipeline_base_has_stage(pipeline, MESA_SHADER_FRAGMENT));
1836 populate_mesh_prog_key(&stages[s], device, compact_mue);
1837 break;
1838 }
1839
1840 default:
1841 unreachable("Invalid graphics shader stage");
1842 }
1843
1844 stages[s].feedback.duration += os_time_get_nano() - stage_start;
1845 stages[s].feedback.flags |= VK_PIPELINE_CREATION_FEEDBACK_VALID_BIT;
1846 }
1847 }
1848
1849 static void
anv_graphics_lib_retain_shaders(struct anv_graphics_base_pipeline * pipeline,struct anv_pipeline_stage * stages,bool will_compile)1850 anv_graphics_lib_retain_shaders(struct anv_graphics_base_pipeline *pipeline,
1851 struct anv_pipeline_stage *stages,
1852 bool will_compile)
1853 {
1854 /* There isn't much point in retaining NIR shaders on final pipelines. */
1855 assert(pipeline->base.type == ANV_PIPELINE_GRAPHICS_LIB);
1856
1857 struct anv_graphics_lib_pipeline *lib = (struct anv_graphics_lib_pipeline *) pipeline;
1858
1859 for (int s = 0; s < ARRAY_SIZE(pipeline->shaders); s++) {
1860 if (!anv_pipeline_base_has_stage(pipeline, s))
1861 continue;
1862
1863 memcpy(lib->retained_shaders[s].shader_sha1, stages[s].shader_sha1,
1864 sizeof(stages[s].shader_sha1));
1865
1866 lib->retained_shaders[s].subgroup_size_type = stages[s].subgroup_size_type;
1867
1868 nir_shader *nir = stages[s].nir != NULL ? stages[s].nir : stages[s].imported.nir;
1869 assert(nir != NULL);
1870
1871 if (!will_compile) {
1872 lib->retained_shaders[s].nir = nir;
1873 } else {
1874 lib->retained_shaders[s].nir =
1875 nir_shader_clone(pipeline->base.mem_ctx, nir);
1876 }
1877 }
1878 }
1879
1880 static bool
anv_graphics_pipeline_load_cached_shaders(struct anv_graphics_base_pipeline * pipeline,struct vk_pipeline_cache * cache,struct anv_pipeline_stage * stages,bool link_optimize,VkPipelineCreationFeedback * pipeline_feedback)1881 anv_graphics_pipeline_load_cached_shaders(struct anv_graphics_base_pipeline *pipeline,
1882 struct vk_pipeline_cache *cache,
1883 struct anv_pipeline_stage *stages,
1884 bool link_optimize,
1885 VkPipelineCreationFeedback *pipeline_feedback)
1886 {
1887 struct anv_device *device = pipeline->base.device;
1888 unsigned cache_hits = 0, found = 0, imported = 0;
1889
1890 for (unsigned s = 0; s < ARRAY_SIZE(pipeline->shaders); s++) {
1891 if (!anv_pipeline_base_has_stage(pipeline, s))
1892 continue;
1893
1894 int64_t stage_start = os_time_get_nano();
1895
1896 bool cache_hit;
1897 stages[s].bin =
1898 anv_device_search_for_kernel(device, cache, &stages[s].cache_key,
1899 sizeof(stages[s].cache_key), &cache_hit);
1900 if (stages[s].bin) {
1901 found++;
1902 pipeline->shaders[s] = stages[s].bin;
1903 }
1904
1905 if (cache_hit) {
1906 cache_hits++;
1907 stages[s].feedback.flags |=
1908 VK_PIPELINE_CREATION_FEEDBACK_APPLICATION_PIPELINE_CACHE_HIT_BIT;
1909 }
1910 stages[s].feedback.duration += os_time_get_nano() - stage_start;
1911 }
1912
1913 /* When not link optimizing, lookup the missing shader in the imported
1914 * libraries.
1915 */
1916 if (!link_optimize) {
1917 for (unsigned s = 0; s < ARRAY_SIZE(pipeline->shaders); s++) {
1918 if (!anv_pipeline_base_has_stage(pipeline, s))
1919 continue;
1920
1921 if (pipeline->shaders[s] != NULL)
1922 continue;
1923
1924 if (stages[s].imported.bin == NULL)
1925 continue;
1926
1927 stages[s].bin = stages[s].imported.bin;
1928 pipeline->shaders[s] = anv_shader_bin_ref(stages[s].imported.bin);
1929 pipeline->source_hashes[s] = stages[s].source_hash;
1930 imported++;
1931 }
1932 }
1933
1934 if ((found + imported) == __builtin_popcount(pipeline->base.active_stages)) {
1935 if (cache_hits == found && found != 0) {
1936 pipeline_feedback->flags |=
1937 VK_PIPELINE_CREATION_FEEDBACK_APPLICATION_PIPELINE_CACHE_HIT_BIT;
1938 }
1939 /* We found all our shaders in the cache. We're done. */
1940 for (unsigned s = 0; s < ARRAY_SIZE(pipeline->shaders); s++) {
1941 if (pipeline->shaders[s] == NULL)
1942 continue;
1943
1944 /* Only add the executables when we're not importing or doing link
1945 * optimizations. The imported executables are added earlier. Link
1946 * optimization can produce different binaries.
1947 */
1948 if (stages[s].imported.bin == NULL || link_optimize)
1949 anv_pipeline_add_executables(&pipeline->base, &stages[s]);
1950 pipeline->source_hashes[s] = stages[s].source_hash;
1951 }
1952 return true;
1953 } else if (found > 0) {
1954 /* We found some but not all of our shaders. This shouldn't happen most
1955 * of the time but it can if we have a partially populated pipeline
1956 * cache.
1957 */
1958 assert(found < __builtin_popcount(pipeline->base.active_stages));
1959
1960 vk_perf(VK_LOG_OBJS(cache ? &cache->base :
1961 &pipeline->base.device->vk.base),
1962 "Found a partial pipeline in the cache. This is "
1963 "most likely caused by an incomplete pipeline cache "
1964 "import or export");
1965
1966 /* We're going to have to recompile anyway, so just throw away our
1967 * references to the shaders in the cache. We'll get them out of the
1968 * cache again as part of the compilation process.
1969 */
1970 for (unsigned s = 0; s < ARRAY_SIZE(pipeline->shaders); s++) {
1971 stages[s].feedback.flags = 0;
1972 if (pipeline->shaders[s]) {
1973 anv_shader_bin_unref(device, pipeline->shaders[s]);
1974 pipeline->shaders[s] = NULL;
1975 }
1976 }
1977 }
1978
1979 return false;
1980 }
1981
1982 static const gl_shader_stage graphics_shader_order[] = {
1983 MESA_SHADER_VERTEX,
1984 MESA_SHADER_TESS_CTRL,
1985 MESA_SHADER_TESS_EVAL,
1986 MESA_SHADER_GEOMETRY,
1987
1988 MESA_SHADER_TASK,
1989 MESA_SHADER_MESH,
1990
1991 MESA_SHADER_FRAGMENT,
1992 };
1993
1994 /* This function loads NIR only for stages specified in
1995 * VkGraphicsPipelineCreateInfo::pStages[]
1996 */
1997 static VkResult
anv_graphics_pipeline_load_nir(struct anv_graphics_base_pipeline * pipeline,struct vk_pipeline_cache * cache,struct anv_pipeline_stage * stages,void * mem_ctx,bool need_clone)1998 anv_graphics_pipeline_load_nir(struct anv_graphics_base_pipeline *pipeline,
1999 struct vk_pipeline_cache *cache,
2000 struct anv_pipeline_stage *stages,
2001 void *mem_ctx,
2002 bool need_clone)
2003 {
2004 for (unsigned s = 0; s < ANV_GRAPHICS_SHADER_STAGE_COUNT; s++) {
2005 if (!anv_pipeline_base_has_stage(pipeline, s))
2006 continue;
2007
2008 int64_t stage_start = os_time_get_nano();
2009
2010 assert(stages[s].stage == s);
2011
2012 stages[s].bind_map = (struct anv_pipeline_bind_map) {
2013 .surface_to_descriptor = stages[s].surface_to_descriptor,
2014 .sampler_to_descriptor = stages[s].sampler_to_descriptor
2015 };
2016
2017 /* Only use the create NIR from the pStages[] element if we don't have
2018 * an imported library for the same stage.
2019 */
2020 if (stages[s].imported.bin == NULL) {
2021 stages[s].nir = anv_pipeline_stage_get_nir(&pipeline->base, cache,
2022 mem_ctx, &stages[s]);
2023 if (stages[s].nir == NULL)
2024 return vk_error(pipeline, VK_ERROR_UNKNOWN);
2025 } else {
2026 stages[s].nir = need_clone ?
2027 nir_shader_clone(mem_ctx, stages[s].imported.nir) :
2028 stages[s].imported.nir;
2029 }
2030
2031 stages[s].feedback.duration += os_time_get_nano() - stage_start;
2032 }
2033
2034 return VK_SUCCESS;
2035 }
2036
2037 static void
anv_pipeline_nir_preprocess(struct anv_pipeline * pipeline,struct anv_pipeline_stage * stage)2038 anv_pipeline_nir_preprocess(struct anv_pipeline *pipeline,
2039 struct anv_pipeline_stage *stage)
2040 {
2041 struct anv_device *device = pipeline->device;
2042 const struct brw_compiler *compiler = device->physical->compiler;
2043
2044 const struct nir_lower_sysvals_to_varyings_options sysvals_to_varyings = {
2045 .point_coord = true,
2046 };
2047 NIR_PASS(_, stage->nir, nir_lower_sysvals_to_varyings, &sysvals_to_varyings);
2048
2049 const nir_opt_access_options opt_access_options = {
2050 .is_vulkan = true,
2051 };
2052 NIR_PASS(_, stage->nir, nir_opt_access, &opt_access_options);
2053
2054 /* Vulkan uses the separate-shader linking model */
2055 stage->nir->info.separate_shader = true;
2056
2057 struct brw_nir_compiler_opts opts = {
2058 .softfp64 = device->fp64_nir,
2059 /* Assume robustness with EXT_pipeline_robustness because this can be
2060 * turned on/off per pipeline and we have no visibility on this here.
2061 */
2062 .robust_image_access = device->vk.enabled_features.robustImageAccess ||
2063 device->vk.enabled_features.robustImageAccess2 ||
2064 device->vk.enabled_extensions.EXT_pipeline_robustness,
2065 .input_vertices = stage->nir->info.stage == MESA_SHADER_TESS_CTRL ?
2066 stage->key.tcs.input_vertices : 0,
2067 };
2068 brw_preprocess_nir(compiler, stage->nir, &opts);
2069
2070 if (stage->nir->info.stage == MESA_SHADER_MESH) {
2071 NIR_PASS(_, stage->nir, anv_nir_lower_set_vtx_and_prim_count);
2072 NIR_PASS(_, stage->nir, nir_opt_dce);
2073 NIR_PASS(_, stage->nir, nir_remove_dead_variables, nir_var_shader_out, NULL);
2074 }
2075
2076 NIR_PASS(_, stage->nir, nir_opt_barrier_modes);
2077
2078 nir_shader_gather_info(stage->nir, nir_shader_get_entrypoint(stage->nir));
2079 }
2080
2081 static void
anv_fill_pipeline_creation_feedback(const struct anv_graphics_base_pipeline * pipeline,VkPipelineCreationFeedback * pipeline_feedback,const VkGraphicsPipelineCreateInfo * info,struct anv_pipeline_stage * stages)2082 anv_fill_pipeline_creation_feedback(const struct anv_graphics_base_pipeline *pipeline,
2083 VkPipelineCreationFeedback *pipeline_feedback,
2084 const VkGraphicsPipelineCreateInfo *info,
2085 struct anv_pipeline_stage *stages)
2086 {
2087 const VkPipelineCreationFeedbackCreateInfo *create_feedback =
2088 vk_find_struct_const(info->pNext, PIPELINE_CREATION_FEEDBACK_CREATE_INFO);
2089 if (create_feedback) {
2090 *create_feedback->pPipelineCreationFeedback = *pipeline_feedback;
2091
2092 /* VkPipelineCreationFeedbackCreateInfo:
2093 *
2094 * "An implementation must set or clear the
2095 * VK_PIPELINE_CREATION_FEEDBACK_VALID_BIT in
2096 * VkPipelineCreationFeedback::flags for pPipelineCreationFeedback
2097 * and every element of pPipelineStageCreationFeedbacks."
2098 *
2099 */
2100 for (uint32_t i = 0; i < create_feedback->pipelineStageCreationFeedbackCount; i++) {
2101 create_feedback->pPipelineStageCreationFeedbacks[i].flags &=
2102 ~VK_PIPELINE_CREATION_FEEDBACK_VALID_BIT;
2103 }
2104 /* This part is not really specified in the Vulkan spec at the moment.
2105 * We're kind of guessing what the CTS wants. We might need to update
2106 * when https://gitlab.khronos.org/vulkan/vulkan/-/issues/3115 is
2107 * clarified.
2108 */
2109 for (uint32_t s = 0; s < ANV_GRAPHICS_SHADER_STAGE_COUNT; s++) {
2110 if (!anv_pipeline_base_has_stage(pipeline, s))
2111 continue;
2112
2113 if (stages[s].feedback_idx < create_feedback->pipelineStageCreationFeedbackCount) {
2114 create_feedback->pPipelineStageCreationFeedbacks[
2115 stages[s].feedback_idx] = stages[s].feedback;
2116 }
2117 }
2118 }
2119 }
2120
2121 static uint32_t
anv_graphics_pipeline_imported_shader_count(struct anv_pipeline_stage * stages)2122 anv_graphics_pipeline_imported_shader_count(struct anv_pipeline_stage *stages)
2123 {
2124 uint32_t count = 0;
2125 for (uint32_t s = 0; s < ANV_GRAPHICS_SHADER_STAGE_COUNT; s++) {
2126 if (stages[s].imported.bin != NULL)
2127 count++;
2128 }
2129 return count;
2130 }
2131
2132 static VkResult
anv_graphics_pipeline_compile(struct anv_graphics_base_pipeline * pipeline,struct anv_pipeline_stage * stages,struct vk_pipeline_cache * cache,VkPipelineCreationFeedback * pipeline_feedback,const VkGraphicsPipelineCreateInfo * info,const struct vk_graphics_pipeline_state * state)2133 anv_graphics_pipeline_compile(struct anv_graphics_base_pipeline *pipeline,
2134 struct anv_pipeline_stage *stages,
2135 struct vk_pipeline_cache *cache,
2136 VkPipelineCreationFeedback *pipeline_feedback,
2137 const VkGraphicsPipelineCreateInfo *info,
2138 const struct vk_graphics_pipeline_state *state)
2139 {
2140 int64_t pipeline_start = os_time_get_nano();
2141
2142 struct anv_device *device = pipeline->base.device;
2143 const struct intel_device_info *devinfo = device->info;
2144 const struct brw_compiler *compiler = device->physical->compiler;
2145
2146 /* Setup the shaders given in this VkGraphicsPipelineCreateInfo::pStages[].
2147 * Other shaders imported from libraries should have been added by
2148 * anv_graphics_pipeline_import_lib().
2149 */
2150 uint32_t shader_count = anv_graphics_pipeline_imported_shader_count(stages);
2151 for (uint32_t i = 0; i < info->stageCount; i++) {
2152 gl_shader_stage stage = vk_to_mesa_shader_stage(info->pStages[i].stage);
2153
2154 /* If a pipeline library is loaded in this stage, we should ignore the
2155 * pStages[] entry of the same stage.
2156 */
2157 if (stages[stage].imported.bin != NULL)
2158 continue;
2159
2160 stages[stage].stage = stage;
2161 stages[stage].pipeline_pNext = info->pNext;
2162 stages[stage].info = &info->pStages[i];
2163 stages[stage].feedback_idx = shader_count++;
2164
2165 anv_stage_write_shader_hash(&stages[stage], device);
2166 }
2167
2168 /* Prepare shader keys for all shaders in pipeline->base.active_stages
2169 * (this includes libraries) before generating the hash for cache look up.
2170 *
2171 * We're doing this because the spec states that :
2172 *
2173 * "When an implementation is looking up a pipeline in a pipeline cache,
2174 * if that pipeline is being created using linked libraries,
2175 * implementations should always return an equivalent pipeline created
2176 * with VK_PIPELINE_CREATE_LINK_TIME_OPTIMIZATION_BIT_EXT if available,
2177 * whether or not that bit was specified."
2178 *
2179 * So even if the application does not request link optimization, we have
2180 * to do our cache lookup with the entire set of shader sha1s so that we
2181 * can find what would be the best optimized pipeline in the case as if we
2182 * had compiled all the shaders together and known the full graphics state.
2183 */
2184 anv_graphics_pipeline_init_keys(pipeline, state, stages);
2185
2186 uint32_t view_mask = state->rp ? state->rp->view_mask : 0;
2187
2188 unsigned char sha1[20];
2189 anv_pipeline_hash_graphics(pipeline, stages, view_mask, sha1);
2190
2191 for (unsigned s = 0; s < ANV_GRAPHICS_SHADER_STAGE_COUNT; s++) {
2192 if (!anv_pipeline_base_has_stage(pipeline, s))
2193 continue;
2194
2195 stages[s].cache_key.stage = s;
2196 memcpy(stages[s].cache_key.sha1, sha1, sizeof(sha1));
2197 }
2198
2199 const bool retain_shaders =
2200 pipeline->base.flags & VK_PIPELINE_CREATE_2_RETAIN_LINK_TIME_OPTIMIZATION_INFO_BIT_EXT;
2201 const bool link_optimize =
2202 pipeline->base.flags & VK_PIPELINE_CREATE_2_LINK_TIME_OPTIMIZATION_BIT_EXT;
2203
2204 VkResult result = VK_SUCCESS;
2205 const bool skip_cache_lookup =
2206 (pipeline->base.flags & VK_PIPELINE_CREATE_CAPTURE_INTERNAL_REPRESENTATIONS_BIT_KHR);
2207
2208 if (!skip_cache_lookup) {
2209 bool found_all_shaders =
2210 anv_graphics_pipeline_load_cached_shaders(pipeline, cache, stages,
2211 link_optimize,
2212 pipeline_feedback);
2213
2214 if (found_all_shaders) {
2215 /* If we need to retain shaders, we need to also load from the NIR
2216 * cache.
2217 */
2218 if (pipeline->base.type == ANV_PIPELINE_GRAPHICS_LIB && retain_shaders) {
2219 result = anv_graphics_pipeline_load_nir(pipeline, cache,
2220 stages,
2221 pipeline->base.mem_ctx,
2222 false /* need_clone */);
2223 if (result != VK_SUCCESS) {
2224 vk_perf(VK_LOG_OBJS(cache ? &cache->base :
2225 &pipeline->base.device->vk.base),
2226 "Found all ISA shaders in the cache but not all NIR shaders.");
2227 }
2228
2229 anv_graphics_lib_retain_shaders(pipeline, stages, false /* will_compile */);
2230 }
2231
2232 if (result == VK_SUCCESS)
2233 goto done;
2234
2235 for (unsigned s = 0; s < ANV_GRAPHICS_SHADER_STAGE_COUNT; s++) {
2236 if (!anv_pipeline_base_has_stage(pipeline, s))
2237 continue;
2238
2239 if (stages[s].nir) {
2240 ralloc_free(stages[s].nir);
2241 stages[s].nir = NULL;
2242 }
2243
2244 assert(pipeline->shaders[s] != NULL);
2245 anv_shader_bin_unref(device, pipeline->shaders[s]);
2246 pipeline->shaders[s] = NULL;
2247 }
2248 }
2249 }
2250
2251 if (pipeline->base.flags & VK_PIPELINE_CREATE_2_FAIL_ON_PIPELINE_COMPILE_REQUIRED_BIT_KHR)
2252 return VK_PIPELINE_COMPILE_REQUIRED;
2253
2254 void *tmp_ctx = ralloc_context(NULL);
2255
2256 result = anv_graphics_pipeline_load_nir(pipeline, cache, stages,
2257 tmp_ctx, link_optimize /* need_clone */);
2258 if (result != VK_SUCCESS)
2259 goto fail;
2260
2261 /* Retain shaders now if asked, this only applies to libraries */
2262 if (pipeline->base.type == ANV_PIPELINE_GRAPHICS_LIB && retain_shaders)
2263 anv_graphics_lib_retain_shaders(pipeline, stages, true /* will_compile */);
2264
2265 /* The following steps will be executed for shaders we need to compile :
2266 *
2267 * - specified through VkGraphicsPipelineCreateInfo::pStages[]
2268 *
2269 * - or compiled from libraries with retained shaders (libraries
2270 * compiled with CREATE_RETAIN_LINK_TIME_OPTIMIZATION_INFO_BIT) if the
2271 * pipeline has the CREATE_LINK_TIME_OPTIMIZATION_BIT flag.
2272 */
2273
2274 /* Preprocess all NIR shaders. */
2275 for (int s = 0; s < ARRAY_SIZE(pipeline->shaders); s++) {
2276 if (anv_graphics_pipeline_skip_shader_compile(pipeline, stages,
2277 link_optimize, s))
2278 continue;
2279
2280 anv_pipeline_nir_preprocess(&pipeline->base, &stages[s]);
2281 }
2282
2283 if (stages[MESA_SHADER_MESH].info && stages[MESA_SHADER_FRAGMENT].info) {
2284 anv_apply_per_prim_attr_wa(stages[MESA_SHADER_MESH].nir,
2285 stages[MESA_SHADER_FRAGMENT].nir,
2286 device,
2287 info);
2288 }
2289
2290 /* Walk backwards to link */
2291 struct anv_pipeline_stage *next_stage = NULL;
2292 for (int i = ARRAY_SIZE(graphics_shader_order) - 1; i >= 0; i--) {
2293 gl_shader_stage s = graphics_shader_order[i];
2294 if (anv_graphics_pipeline_skip_shader_compile(pipeline, stages,
2295 link_optimize, s))
2296 continue;
2297
2298 struct anv_pipeline_stage *stage = &stages[s];
2299
2300 switch (s) {
2301 case MESA_SHADER_VERTEX:
2302 anv_pipeline_link_vs(compiler, stage, next_stage);
2303 break;
2304 case MESA_SHADER_TESS_CTRL:
2305 anv_pipeline_link_tcs(compiler, stage, next_stage);
2306 break;
2307 case MESA_SHADER_TESS_EVAL:
2308 anv_pipeline_link_tes(compiler, stage, next_stage);
2309 break;
2310 case MESA_SHADER_GEOMETRY:
2311 anv_pipeline_link_gs(compiler, stage, next_stage);
2312 break;
2313 case MESA_SHADER_TASK:
2314 anv_pipeline_link_task(compiler, stage, next_stage);
2315 break;
2316 case MESA_SHADER_MESH:
2317 anv_pipeline_link_mesh(compiler, stage, next_stage);
2318 break;
2319 case MESA_SHADER_FRAGMENT:
2320 anv_pipeline_link_fs(compiler, stage, state->rp);
2321 break;
2322 default:
2323 unreachable("Invalid graphics shader stage");
2324 }
2325
2326 next_stage = stage;
2327 }
2328
2329 bool use_primitive_replication = false;
2330 if (devinfo->ver >= 12 && view_mask != 0) {
2331 /* For some pipelines HW Primitive Replication can be used instead of
2332 * instancing to implement Multiview. This depend on how viewIndex is
2333 * used in all the active shaders, so this check can't be done per
2334 * individual shaders.
2335 */
2336 nir_shader *shaders[ANV_GRAPHICS_SHADER_STAGE_COUNT] = {};
2337 for (unsigned s = 0; s < ARRAY_SIZE(shaders); s++)
2338 shaders[s] = stages[s].nir;
2339
2340 use_primitive_replication =
2341 anv_check_for_primitive_replication(device,
2342 pipeline->base.active_stages,
2343 shaders, view_mask);
2344 }
2345
2346 struct anv_pipeline_stage *prev_stage = NULL;
2347 for (unsigned i = 0; i < ARRAY_SIZE(graphics_shader_order); i++) {
2348 gl_shader_stage s = graphics_shader_order[i];
2349 if (anv_graphics_pipeline_skip_shader_compile(pipeline, stages,
2350 link_optimize, s))
2351 continue;
2352
2353 struct anv_pipeline_stage *stage = &stages[s];
2354
2355 int64_t stage_start = os_time_get_nano();
2356
2357 anv_pipeline_lower_nir(&pipeline->base, tmp_ctx, stage,
2358 &pipeline->base.layout, view_mask,
2359 use_primitive_replication);
2360
2361 struct shader_info *cur_info = &stage->nir->info;
2362
2363 if (prev_stage && compiler->nir_options[s]->unify_interfaces) {
2364 struct shader_info *prev_info = &prev_stage->nir->info;
2365
2366 prev_info->outputs_written |= cur_info->inputs_read &
2367 ~(VARYING_BIT_TESS_LEVEL_INNER | VARYING_BIT_TESS_LEVEL_OUTER);
2368 cur_info->inputs_read |= prev_info->outputs_written &
2369 ~(VARYING_BIT_TESS_LEVEL_INNER | VARYING_BIT_TESS_LEVEL_OUTER);
2370 prev_info->patch_outputs_written |= cur_info->patch_inputs_read;
2371 cur_info->patch_inputs_read |= prev_info->patch_outputs_written;
2372 }
2373
2374 anv_fixup_subgroup_size(device, cur_info);
2375
2376 stage->feedback.duration += os_time_get_nano() - stage_start;
2377
2378 prev_stage = stage;
2379 }
2380
2381 /* In the case the platform can write the primitive variable shading rate
2382 * and KHR_fragment_shading_rate is enabled :
2383 * - there can be a fragment shader but we don't have it yet
2384 * - the fragment shader needs fragment shading rate
2385 *
2386 * figure out the last geometry stage that should write the primitive
2387 * shading rate, and ensure it is marked as used there. The backend will
2388 * write a default value if the shader doesn't actually write it.
2389 *
2390 * We iterate backwards in the stage and stop on the first shader that can
2391 * set the value.
2392 *
2393 * Don't apply this to MESH stages, as this is a per primitive thing.
2394 */
2395 if (devinfo->has_coarse_pixel_primitive_and_cb &&
2396 device->vk.enabled_extensions.KHR_fragment_shading_rate &&
2397 pipeline_has_coarse_pixel(state->dynamic, state->ms, state->fsr) &&
2398 (!stages[MESA_SHADER_FRAGMENT].info ||
2399 stages[MESA_SHADER_FRAGMENT].key.wm.coarse_pixel) &&
2400 stages[MESA_SHADER_MESH].nir == NULL) {
2401 struct anv_pipeline_stage *last_psr = NULL;
2402
2403 for (unsigned i = 0; i < ARRAY_SIZE(graphics_shader_order); i++) {
2404 gl_shader_stage s =
2405 graphics_shader_order[ARRAY_SIZE(graphics_shader_order) - i - 1];
2406
2407 if (anv_graphics_pipeline_skip_shader_compile(pipeline, stages,
2408 link_optimize, s) ||
2409 !gl_shader_stage_can_set_fragment_shading_rate(s))
2410 continue;
2411
2412 last_psr = &stages[s];
2413 break;
2414 }
2415
2416 /* Only set primitive shading rate if there is a pre-rasterization
2417 * shader in this pipeline/pipeline-library.
2418 */
2419 if (last_psr)
2420 last_psr->nir->info.outputs_written |= VARYING_BIT_PRIMITIVE_SHADING_RATE;
2421 }
2422
2423 prev_stage = NULL;
2424 for (unsigned i = 0; i < ARRAY_SIZE(graphics_shader_order); i++) {
2425 gl_shader_stage s = graphics_shader_order[i];
2426 struct anv_pipeline_stage *stage = &stages[s];
2427
2428 if (anv_graphics_pipeline_skip_shader_compile(pipeline, stages, link_optimize, s))
2429 continue;
2430
2431 int64_t stage_start = os_time_get_nano();
2432
2433 void *stage_ctx = ralloc_context(NULL);
2434
2435 switch (s) {
2436 case MESA_SHADER_VERTEX:
2437 anv_pipeline_compile_vs(compiler, stage_ctx, pipeline,
2438 stage, view_mask);
2439 break;
2440 case MESA_SHADER_TESS_CTRL:
2441 anv_pipeline_compile_tcs(compiler, stage_ctx, device,
2442 stage, prev_stage);
2443 break;
2444 case MESA_SHADER_TESS_EVAL:
2445 anv_pipeline_compile_tes(compiler, stage_ctx, device,
2446 stage, prev_stage);
2447 break;
2448 case MESA_SHADER_GEOMETRY:
2449 anv_pipeline_compile_gs(compiler, stage_ctx, device,
2450 stage, prev_stage);
2451 break;
2452 case MESA_SHADER_TASK:
2453 anv_pipeline_compile_task(compiler, stage_ctx, device,
2454 stage);
2455 break;
2456 case MESA_SHADER_MESH:
2457 anv_pipeline_compile_mesh(compiler, stage_ctx, device,
2458 stage, prev_stage);
2459 break;
2460 case MESA_SHADER_FRAGMENT:
2461 anv_pipeline_compile_fs(compiler, stage_ctx, device,
2462 stage, prev_stage, pipeline,
2463 view_mask,
2464 use_primitive_replication);
2465 break;
2466 default:
2467 unreachable("Invalid graphics shader stage");
2468 }
2469 if (stage->code == NULL) {
2470 ralloc_free(stage_ctx);
2471 result = vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
2472 goto fail;
2473 }
2474
2475 anv_nir_validate_push_layout(&stage->prog_data.base,
2476 &stage->bind_map);
2477
2478 struct anv_shader_upload_params upload_params = {
2479 .stage = s,
2480 .key_data = &stage->cache_key,
2481 .key_size = sizeof(stage->cache_key),
2482 .kernel_data = stage->code,
2483 .kernel_size = stage->prog_data.base.program_size,
2484 .prog_data = &stage->prog_data.base,
2485 .prog_data_size = brw_prog_data_size(s),
2486 .stats = stage->stats,
2487 .num_stats = stage->num_stats,
2488 .xfb_info = stage->nir->xfb_info,
2489 .bind_map = &stage->bind_map,
2490 .push_desc_info = &stage->push_desc_info,
2491 .dynamic_push_values = stage->dynamic_push_values,
2492 };
2493
2494 stage->bin =
2495 anv_device_upload_kernel(device, cache, &upload_params);
2496 if (!stage->bin) {
2497 ralloc_free(stage_ctx);
2498 result = vk_error(pipeline, VK_ERROR_OUT_OF_HOST_MEMORY);
2499 goto fail;
2500 }
2501
2502 anv_pipeline_add_executables(&pipeline->base, stage);
2503 pipeline->source_hashes[s] = stage->source_hash;
2504 pipeline->shaders[s] = stage->bin;
2505
2506 ralloc_free(stage_ctx);
2507
2508 stage->feedback.duration += os_time_get_nano() - stage_start;
2509
2510 prev_stage = stage;
2511 }
2512
2513 /* Finally add the imported shaders that were not compiled as part of this
2514 * step.
2515 */
2516 for (unsigned s = 0; s < ARRAY_SIZE(pipeline->shaders); s++) {
2517 if (!anv_pipeline_base_has_stage(pipeline, s))
2518 continue;
2519
2520 if (pipeline->shaders[s] != NULL)
2521 continue;
2522
2523 /* We should have recompiled everything with link optimization. */
2524 assert(!link_optimize);
2525
2526 struct anv_pipeline_stage *stage = &stages[s];
2527
2528 pipeline->source_hashes[s] = stage->source_hash;
2529 pipeline->shaders[s] = anv_shader_bin_ref(stage->imported.bin);
2530 }
2531
2532 ralloc_free(tmp_ctx);
2533
2534 done:
2535
2536 /* Write the feedback index into the pipeline */
2537 for (unsigned s = 0; s < ARRAY_SIZE(pipeline->shaders); s++) {
2538 if (!anv_pipeline_base_has_stage(pipeline, s))
2539 continue;
2540
2541 struct anv_pipeline_stage *stage = &stages[s];
2542 pipeline->feedback_index[s] = stage->feedback_idx;
2543 pipeline->robust_flags[s] = stage->robust_flags;
2544
2545 anv_pipeline_account_shader(&pipeline->base, pipeline->shaders[s]);
2546 }
2547
2548 pipeline_feedback->duration = os_time_get_nano() - pipeline_start;
2549
2550 if (pipeline->shaders[MESA_SHADER_FRAGMENT]) {
2551 pipeline->fragment_dynamic =
2552 anv_graphics_pipeline_stage_fragment_dynamic(
2553 &stages[MESA_SHADER_FRAGMENT]);
2554 }
2555
2556 return VK_SUCCESS;
2557
2558 fail:
2559 ralloc_free(tmp_ctx);
2560
2561 for (unsigned s = 0; s < ARRAY_SIZE(pipeline->shaders); s++) {
2562 if (pipeline->shaders[s])
2563 anv_shader_bin_unref(device, pipeline->shaders[s]);
2564 }
2565
2566 return result;
2567 }
2568
2569 static VkResult
anv_pipeline_compile_cs(struct anv_compute_pipeline * pipeline,struct vk_pipeline_cache * cache,const VkComputePipelineCreateInfo * info)2570 anv_pipeline_compile_cs(struct anv_compute_pipeline *pipeline,
2571 struct vk_pipeline_cache *cache,
2572 const VkComputePipelineCreateInfo *info)
2573 {
2574 ASSERTED const VkPipelineShaderStageCreateInfo *sinfo = &info->stage;
2575 assert(sinfo->stage == VK_SHADER_STAGE_COMPUTE_BIT);
2576
2577 VkPipelineCreationFeedback pipeline_feedback = {
2578 .flags = VK_PIPELINE_CREATION_FEEDBACK_VALID_BIT,
2579 };
2580 int64_t pipeline_start = os_time_get_nano();
2581
2582 struct anv_device *device = pipeline->base.device;
2583 const struct brw_compiler *compiler = device->physical->compiler;
2584
2585 struct anv_pipeline_stage stage = {
2586 .stage = MESA_SHADER_COMPUTE,
2587 .info = &info->stage,
2588 .pipeline_pNext = info->pNext,
2589 .cache_key = {
2590 .stage = MESA_SHADER_COMPUTE,
2591 },
2592 .feedback = {
2593 .flags = VK_PIPELINE_CREATION_FEEDBACK_VALID_BIT,
2594 },
2595 };
2596 anv_stage_write_shader_hash(&stage, device);
2597
2598 populate_cs_prog_key(&stage, device);
2599
2600 const bool skip_cache_lookup =
2601 (pipeline->base.flags & VK_PIPELINE_CREATE_CAPTURE_INTERNAL_REPRESENTATIONS_BIT_KHR);
2602
2603 anv_pipeline_hash_compute(pipeline, &stage, stage.cache_key.sha1);
2604
2605 bool cache_hit = false;
2606 if (!skip_cache_lookup) {
2607 stage.bin = anv_device_search_for_kernel(device, cache,
2608 &stage.cache_key,
2609 sizeof(stage.cache_key),
2610 &cache_hit);
2611 }
2612
2613 if (stage.bin == NULL &&
2614 (pipeline->base.flags & VK_PIPELINE_CREATE_FAIL_ON_PIPELINE_COMPILE_REQUIRED_BIT))
2615 return VK_PIPELINE_COMPILE_REQUIRED;
2616
2617 void *mem_ctx = ralloc_context(NULL);
2618 if (stage.bin == NULL) {
2619 int64_t stage_start = os_time_get_nano();
2620
2621 stage.bind_map = (struct anv_pipeline_bind_map) {
2622 .surface_to_descriptor = stage.surface_to_descriptor,
2623 .sampler_to_descriptor = stage.sampler_to_descriptor
2624 };
2625
2626 /* Set up a binding for the gl_NumWorkGroups */
2627 stage.bind_map.surface_count = 1;
2628 stage.bind_map.surface_to_descriptor[0] = (struct anv_pipeline_binding) {
2629 .set = ANV_DESCRIPTOR_SET_NUM_WORK_GROUPS,
2630 .binding = UINT32_MAX,
2631 };
2632
2633 stage.nir = anv_pipeline_stage_get_nir(&pipeline->base, cache, mem_ctx, &stage);
2634 if (stage.nir == NULL) {
2635 ralloc_free(mem_ctx);
2636 return vk_error(pipeline, VK_ERROR_UNKNOWN);
2637 }
2638
2639 anv_pipeline_nir_preprocess(&pipeline->base, &stage);
2640
2641 anv_pipeline_lower_nir(&pipeline->base, mem_ctx, &stage,
2642 &pipeline->base.layout, 0 /* view_mask */,
2643 false /* use_primitive_replication */);
2644
2645 anv_fixup_subgroup_size(device, &stage.nir->info);
2646
2647 stage.num_stats = 1;
2648
2649 struct brw_compile_cs_params params = {
2650 .base = {
2651 .nir = stage.nir,
2652 .stats = stage.stats,
2653 .log_data = device,
2654 .mem_ctx = mem_ctx,
2655 },
2656 .key = &stage.key.cs,
2657 .prog_data = &stage.prog_data.cs,
2658 };
2659
2660 stage.code = brw_compile_cs(compiler, ¶ms);
2661 if (stage.code == NULL) {
2662 ralloc_free(mem_ctx);
2663 return vk_error(pipeline, VK_ERROR_OUT_OF_HOST_MEMORY);
2664 }
2665
2666 anv_nir_validate_push_layout(&stage.prog_data.base, &stage.bind_map);
2667
2668 if (!stage.prog_data.cs.uses_num_work_groups) {
2669 assert(stage.bind_map.surface_to_descriptor[0].set ==
2670 ANV_DESCRIPTOR_SET_NUM_WORK_GROUPS);
2671 stage.bind_map.surface_to_descriptor[0].set = ANV_DESCRIPTOR_SET_NULL;
2672 }
2673
2674 struct anv_shader_upload_params upload_params = {
2675 .stage = MESA_SHADER_COMPUTE,
2676 .key_data = &stage.cache_key,
2677 .key_size = sizeof(stage.cache_key),
2678 .kernel_data = stage.code,
2679 .kernel_size = stage.prog_data.base.program_size,
2680 .prog_data = &stage.prog_data.base,
2681 .prog_data_size = sizeof(stage.prog_data.cs),
2682 .stats = stage.stats,
2683 .num_stats = stage.num_stats,
2684 .bind_map = &stage.bind_map,
2685 .push_desc_info = &stage.push_desc_info,
2686 .dynamic_push_values = stage.dynamic_push_values,
2687 };
2688
2689 stage.bin = anv_device_upload_kernel(device, cache, &upload_params);
2690 if (!stage.bin) {
2691 ralloc_free(mem_ctx);
2692 return vk_error(pipeline, VK_ERROR_OUT_OF_HOST_MEMORY);
2693 }
2694
2695 stage.feedback.duration = os_time_get_nano() - stage_start;
2696 }
2697
2698 anv_pipeline_account_shader(&pipeline->base, stage.bin);
2699 anv_pipeline_add_executables(&pipeline->base, &stage);
2700 pipeline->source_hash = stage.source_hash;
2701
2702 ralloc_free(mem_ctx);
2703
2704 if (cache_hit) {
2705 stage.feedback.flags |=
2706 VK_PIPELINE_CREATION_FEEDBACK_APPLICATION_PIPELINE_CACHE_HIT_BIT;
2707 pipeline_feedback.flags |=
2708 VK_PIPELINE_CREATION_FEEDBACK_APPLICATION_PIPELINE_CACHE_HIT_BIT;
2709 }
2710 pipeline_feedback.duration = os_time_get_nano() - pipeline_start;
2711
2712 const VkPipelineCreationFeedbackCreateInfo *create_feedback =
2713 vk_find_struct_const(info->pNext, PIPELINE_CREATION_FEEDBACK_CREATE_INFO);
2714 if (create_feedback) {
2715 *create_feedback->pPipelineCreationFeedback = pipeline_feedback;
2716
2717 if (create_feedback->pipelineStageCreationFeedbackCount) {
2718 assert(create_feedback->pipelineStageCreationFeedbackCount == 1);
2719 create_feedback->pPipelineStageCreationFeedbacks[0] = stage.feedback;
2720 }
2721 }
2722
2723 pipeline->cs = stage.bin;
2724
2725 return VK_SUCCESS;
2726 }
2727
2728 static VkResult
anv_compute_pipeline_create(struct anv_device * device,struct vk_pipeline_cache * cache,const VkComputePipelineCreateInfo * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkPipeline * pPipeline)2729 anv_compute_pipeline_create(struct anv_device *device,
2730 struct vk_pipeline_cache *cache,
2731 const VkComputePipelineCreateInfo *pCreateInfo,
2732 const VkAllocationCallbacks *pAllocator,
2733 VkPipeline *pPipeline)
2734 {
2735 struct anv_compute_pipeline *pipeline;
2736 VkResult result;
2737
2738 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO);
2739
2740 pipeline = vk_zalloc2(&device->vk.alloc, pAllocator, sizeof(*pipeline), 8,
2741 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
2742 if (pipeline == NULL)
2743 return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
2744
2745 result = anv_pipeline_init(&pipeline->base, device,
2746 ANV_PIPELINE_COMPUTE,
2747 vk_compute_pipeline_create_flags(pCreateInfo),
2748 pAllocator);
2749 if (result != VK_SUCCESS) {
2750 vk_free2(&device->vk.alloc, pAllocator, pipeline);
2751 return result;
2752 }
2753
2754
2755 ANV_FROM_HANDLE(anv_pipeline_layout, pipeline_layout, pCreateInfo->layout);
2756 anv_pipeline_init_layout(&pipeline->base, pipeline_layout);
2757
2758 pipeline->base.active_stages = VK_SHADER_STAGE_COMPUTE_BIT;
2759
2760 anv_batch_set_storage(&pipeline->base.batch, ANV_NULL_ADDRESS,
2761 pipeline->batch_data, sizeof(pipeline->batch_data));
2762
2763 result = anv_pipeline_compile_cs(pipeline, cache, pCreateInfo);
2764 if (result != VK_SUCCESS) {
2765 anv_pipeline_finish(&pipeline->base, device);
2766 vk_free2(&device->vk.alloc, pAllocator, pipeline);
2767 return result;
2768 }
2769
2770 anv_genX(device->info, compute_pipeline_emit)(pipeline);
2771
2772 ANV_RMV(compute_pipeline_create, device, pipeline, false);
2773
2774 *pPipeline = anv_pipeline_to_handle(&pipeline->base);
2775
2776 return pipeline->base.batch.status;
2777 }
2778
anv_CreateComputePipelines(VkDevice _device,VkPipelineCache pipelineCache,uint32_t count,const VkComputePipelineCreateInfo * pCreateInfos,const VkAllocationCallbacks * pAllocator,VkPipeline * pPipelines)2779 VkResult anv_CreateComputePipelines(
2780 VkDevice _device,
2781 VkPipelineCache pipelineCache,
2782 uint32_t count,
2783 const VkComputePipelineCreateInfo* pCreateInfos,
2784 const VkAllocationCallbacks* pAllocator,
2785 VkPipeline* pPipelines)
2786 {
2787 ANV_FROM_HANDLE(anv_device, device, _device);
2788 ANV_FROM_HANDLE(vk_pipeline_cache, pipeline_cache, pipelineCache);
2789
2790 VkResult result = VK_SUCCESS;
2791
2792 unsigned i;
2793 for (i = 0; i < count; i++) {
2794 const VkPipelineCreateFlags2KHR flags =
2795 vk_compute_pipeline_create_flags(&pCreateInfos[i]);
2796 VkResult res = anv_compute_pipeline_create(device, pipeline_cache,
2797 &pCreateInfos[i],
2798 pAllocator, &pPipelines[i]);
2799
2800 if (res == VK_SUCCESS)
2801 continue;
2802
2803 /* Bail out on the first error != VK_PIPELINE_COMPILE_REQUIRED as it
2804 * is not obvious what error should be report upon 2 different failures.
2805 * */
2806 result = res;
2807 if (res != VK_PIPELINE_COMPILE_REQUIRED)
2808 break;
2809
2810 pPipelines[i] = VK_NULL_HANDLE;
2811
2812 if (flags & VK_PIPELINE_CREATE_2_EARLY_RETURN_ON_FAILURE_BIT_KHR)
2813 break;
2814 }
2815
2816 for (; i < count; i++)
2817 pPipelines[i] = VK_NULL_HANDLE;
2818
2819 return result;
2820 }
2821
2822 /**
2823 * Calculate the desired L3 partitioning based on the current state of the
2824 * pipeline. For now this simply returns the conservative defaults calculated
2825 * by get_default_l3_weights(), but we could probably do better by gathering
2826 * more statistics from the pipeline state (e.g. guess of expected URB usage
2827 * and bound surfaces), or by using feed-back from performance counters.
2828 */
2829 void
anv_pipeline_setup_l3_config(struct anv_pipeline * pipeline,bool needs_slm)2830 anv_pipeline_setup_l3_config(struct anv_pipeline *pipeline, bool needs_slm)
2831 {
2832 const struct intel_device_info *devinfo = pipeline->device->info;
2833
2834 const struct intel_l3_weights w =
2835 intel_get_default_l3_weights(devinfo, true, needs_slm);
2836
2837 pipeline->l3_config = intel_get_l3_config(devinfo, w);
2838 }
2839
2840 static uint32_t
get_vs_input_elements(const struct brw_vs_prog_data * vs_prog_data)2841 get_vs_input_elements(const struct brw_vs_prog_data *vs_prog_data)
2842 {
2843 /* Pull inputs_read out of the VS prog data */
2844 const uint64_t inputs_read = vs_prog_data->inputs_read;
2845 const uint64_t double_inputs_read =
2846 vs_prog_data->double_inputs_read & inputs_read;
2847 assert((inputs_read & ((1 << VERT_ATTRIB_GENERIC0) - 1)) == 0);
2848 const uint32_t elements = inputs_read >> VERT_ATTRIB_GENERIC0;
2849 const uint32_t elements_double = double_inputs_read >> VERT_ATTRIB_GENERIC0;
2850
2851 return __builtin_popcount(elements) -
2852 __builtin_popcount(elements_double) / 2;
2853 }
2854
2855 static void
anv_graphics_pipeline_emit(struct anv_graphics_pipeline * pipeline,const struct vk_graphics_pipeline_state * state)2856 anv_graphics_pipeline_emit(struct anv_graphics_pipeline *pipeline,
2857 const struct vk_graphics_pipeline_state *state)
2858 {
2859 pipeline->view_mask = state->rp->view_mask;
2860
2861 anv_pipeline_setup_l3_config(&pipeline->base.base, false);
2862
2863 if (anv_pipeline_is_primitive(pipeline)) {
2864 const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
2865
2866 /* The total number of vertex elements we need to program. We might need
2867 * a couple more to implement some of the draw parameters.
2868 */
2869 pipeline->svgs_count =
2870 (vs_prog_data->uses_vertexid ||
2871 vs_prog_data->uses_instanceid ||
2872 vs_prog_data->uses_firstvertex ||
2873 vs_prog_data->uses_baseinstance) + vs_prog_data->uses_drawid;
2874
2875 pipeline->vs_input_elements = get_vs_input_elements(vs_prog_data);
2876
2877 pipeline->vertex_input_elems =
2878 (BITSET_TEST(state->dynamic, MESA_VK_DYNAMIC_VI) ?
2879 0 : pipeline->vs_input_elements) + pipeline->svgs_count;
2880
2881 /* Our implementation of VK_KHR_multiview uses instancing to draw the
2882 * different views when primitive replication cannot be used. If the
2883 * client asks for instancing, we need to multiply by the client's
2884 * instance count at draw time and instance divisor in the vertex
2885 * bindings by the number of views ensure that we repeat the client's
2886 * per-instance data once for each view.
2887 */
2888 const bool uses_primitive_replication =
2889 anv_pipeline_get_last_vue_prog_data(pipeline)->vue_map.num_pos_slots > 1;
2890 pipeline->instance_multiplier = 1;
2891 if (pipeline->view_mask && !uses_primitive_replication)
2892 pipeline->instance_multiplier = util_bitcount(pipeline->view_mask);
2893 } else {
2894 assert(anv_pipeline_is_mesh(pipeline));
2895 /* TODO(mesh): Mesh vs. Multiview with Instancing. */
2896 }
2897
2898 /* Store line mode and rasterization samples, these are used
2899 * for dynamic primitive topology.
2900 */
2901 pipeline->rasterization_samples =
2902 state->ms != NULL ? state->ms->rasterization_samples : 1;
2903
2904 pipeline->dynamic_patch_control_points =
2905 anv_pipeline_has_stage(pipeline, MESA_SHADER_TESS_CTRL) &&
2906 BITSET_TEST(state->dynamic, MESA_VK_DYNAMIC_TS_PATCH_CONTROL_POINTS) &&
2907 (pipeline->base.shaders[MESA_SHADER_TESS_CTRL]->dynamic_push_values &
2908 ANV_DYNAMIC_PUSH_INPUT_VERTICES);
2909
2910 if (pipeline->base.shaders[MESA_SHADER_FRAGMENT]) {
2911 const struct brw_wm_prog_data *wm_prog_data = get_wm_prog_data(pipeline);
2912
2913 if (wm_prog_data_dynamic(wm_prog_data)) {
2914 pipeline->fs_msaa_flags = INTEL_MSAA_FLAG_ENABLE_DYNAMIC;
2915
2916 assert(wm_prog_data->persample_dispatch == BRW_SOMETIMES);
2917 if (state->ms && state->ms->rasterization_samples > 1) {
2918 pipeline->fs_msaa_flags |= INTEL_MSAA_FLAG_MULTISAMPLE_FBO;
2919
2920 if (wm_prog_data->sample_shading) {
2921 assert(wm_prog_data->persample_dispatch != BRW_NEVER);
2922 pipeline->fs_msaa_flags |= INTEL_MSAA_FLAG_PERSAMPLE_DISPATCH;
2923 }
2924
2925 if (state->ms->sample_shading_enable &&
2926 (state->ms->min_sample_shading * state->ms->rasterization_samples) > 1) {
2927 pipeline->fs_msaa_flags |= INTEL_MSAA_FLAG_PERSAMPLE_DISPATCH |
2928 INTEL_MSAA_FLAG_PERSAMPLE_INTERP;
2929 }
2930 }
2931
2932 if (state->ms && state->ms->alpha_to_coverage_enable)
2933 pipeline->fs_msaa_flags |= INTEL_MSAA_FLAG_ALPHA_TO_COVERAGE;
2934
2935 assert(wm_prog_data->coarse_pixel_dispatch != BRW_ALWAYS);
2936 if (wm_prog_data->coarse_pixel_dispatch == BRW_SOMETIMES &&
2937 !(pipeline->fs_msaa_flags & INTEL_MSAA_FLAG_PERSAMPLE_DISPATCH) &&
2938 (!state->ms || !state->ms->sample_shading_enable)) {
2939 pipeline->fs_msaa_flags |= INTEL_MSAA_FLAG_COARSE_PI_MSG |
2940 INTEL_MSAA_FLAG_COARSE_RT_WRITES;
2941 }
2942 } else {
2943 assert(wm_prog_data->alpha_to_coverage != BRW_SOMETIMES);
2944 assert(wm_prog_data->coarse_pixel_dispatch != BRW_SOMETIMES);
2945 assert(wm_prog_data->persample_dispatch != BRW_SOMETIMES);
2946 }
2947 }
2948
2949 const struct anv_device *device = pipeline->base.base.device;
2950 const struct intel_device_info *devinfo = device->info;
2951 anv_genX(devinfo, graphics_pipeline_emit)(pipeline, state);
2952 }
2953
2954 static void
anv_graphics_pipeline_import_layout(struct anv_graphics_base_pipeline * pipeline,struct anv_pipeline_sets_layout * layout)2955 anv_graphics_pipeline_import_layout(struct anv_graphics_base_pipeline *pipeline,
2956 struct anv_pipeline_sets_layout *layout)
2957 {
2958 pipeline->base.layout.independent_sets |= layout->independent_sets;
2959
2960 for (uint32_t s = 0; s < layout->num_sets; s++) {
2961 if (layout->set[s].layout == NULL)
2962 continue;
2963
2964 anv_pipeline_sets_layout_add(&pipeline->base.layout, s,
2965 layout->set[s].layout);
2966 }
2967 }
2968
2969 static void
anv_graphics_pipeline_import_lib(struct anv_graphics_base_pipeline * pipeline,bool link_optimize,bool retain_shaders,struct anv_pipeline_stage * stages,struct anv_graphics_lib_pipeline * lib)2970 anv_graphics_pipeline_import_lib(struct anv_graphics_base_pipeline *pipeline,
2971 bool link_optimize,
2972 bool retain_shaders,
2973 struct anv_pipeline_stage *stages,
2974 struct anv_graphics_lib_pipeline *lib)
2975 {
2976 struct anv_pipeline_sets_layout *lib_layout =
2977 &lib->base.base.layout;
2978 anv_graphics_pipeline_import_layout(pipeline, lib_layout);
2979
2980 /* We can't have shaders specified twice through libraries. */
2981 assert((pipeline->base.active_stages & lib->base.base.active_stages) == 0);
2982
2983 /* VK_EXT_graphics_pipeline_library:
2984 *
2985 * "To perform link time optimizations,
2986 * VK_PIPELINE_CREATE_RETAIN_LINK_TIME_OPTIMIZATION_INFO_BIT_EXT must
2987 * be specified on all pipeline libraries that are being linked
2988 * together. Implementations should retain any additional information
2989 * needed to perform optimizations at the final link step when this bit
2990 * is present."
2991 */
2992 assert(!link_optimize || lib->retain_shaders);
2993
2994 pipeline->base.active_stages |= lib->base.base.active_stages;
2995
2996 /* Propagate the fragment dynamic flag, unless we're doing link
2997 * optimization, in that case we'll have all the state information and this
2998 * will never be dynamic.
2999 */
3000 if (!link_optimize) {
3001 if (lib->base.fragment_dynamic) {
3002 assert(lib->base.base.active_stages & VK_SHADER_STAGE_FRAGMENT_BIT);
3003 pipeline->fragment_dynamic = true;
3004 }
3005 }
3006
3007 uint32_t shader_count = anv_graphics_pipeline_imported_shader_count(stages);
3008 for (uint32_t s = 0; s < ARRAY_SIZE(lib->base.shaders); s++) {
3009 if (lib->base.shaders[s] == NULL)
3010 continue;
3011
3012 stages[s].stage = s;
3013 stages[s].feedback_idx = shader_count + lib->base.feedback_index[s];
3014 stages[s].robust_flags = lib->base.robust_flags[s];
3015
3016 /* Always import the shader sha1, this will be used for cache lookup. */
3017 memcpy(stages[s].shader_sha1, lib->retained_shaders[s].shader_sha1,
3018 sizeof(stages[s].shader_sha1));
3019 stages[s].source_hash = lib->base.source_hashes[s];
3020
3021 stages[s].subgroup_size_type = lib->retained_shaders[s].subgroup_size_type;
3022 stages[s].imported.nir = lib->retained_shaders[s].nir;
3023 stages[s].imported.bin = lib->base.shaders[s];
3024
3025 stages[s].bind_map = (struct anv_pipeline_bind_map) {
3026 .surface_to_descriptor = stages[s].surface_to_descriptor,
3027 .sampler_to_descriptor = stages[s].sampler_to_descriptor
3028 };
3029 }
3030
3031 /* When not link optimizing, import the executables (shader descriptions
3032 * for VK_KHR_pipeline_executable_properties). With link optimization there
3033 * is a chance it'll produce different binaries, so we'll add the optimized
3034 * version later.
3035 */
3036 if (!link_optimize) {
3037 util_dynarray_foreach(&lib->base.base.executables,
3038 struct anv_pipeline_executable, exe) {
3039 util_dynarray_append(&pipeline->base.executables,
3040 struct anv_pipeline_executable, *exe);
3041 }
3042 }
3043 }
3044
3045 static void
anv_graphics_lib_validate_shaders(struct anv_graphics_lib_pipeline * lib,bool retained_shaders)3046 anv_graphics_lib_validate_shaders(struct anv_graphics_lib_pipeline *lib,
3047 bool retained_shaders)
3048 {
3049 for (uint32_t s = 0; s < ARRAY_SIZE(lib->retained_shaders); s++) {
3050 if (anv_pipeline_base_has_stage(&lib->base, s)) {
3051 assert(!retained_shaders || lib->retained_shaders[s].nir != NULL);
3052 assert(lib->base.shaders[s] != NULL);
3053 }
3054 }
3055 }
3056
3057 static VkResult
anv_graphics_lib_pipeline_create(struct anv_device * device,struct vk_pipeline_cache * cache,const VkGraphicsPipelineCreateInfo * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkPipeline * pPipeline)3058 anv_graphics_lib_pipeline_create(struct anv_device *device,
3059 struct vk_pipeline_cache *cache,
3060 const VkGraphicsPipelineCreateInfo *pCreateInfo,
3061 const VkAllocationCallbacks *pAllocator,
3062 VkPipeline *pPipeline)
3063 {
3064 struct anv_pipeline_stage stages[ANV_GRAPHICS_SHADER_STAGE_COUNT] = {};
3065 VkPipelineCreationFeedback pipeline_feedback = {
3066 .flags = VK_PIPELINE_CREATION_FEEDBACK_VALID_BIT,
3067 };
3068 int64_t pipeline_start = os_time_get_nano();
3069
3070 struct anv_graphics_lib_pipeline *pipeline;
3071 VkResult result;
3072
3073 const VkPipelineCreateFlags2KHR flags =
3074 vk_graphics_pipeline_create_flags(pCreateInfo);
3075 assert(flags & VK_PIPELINE_CREATE_2_LIBRARY_BIT_KHR);
3076
3077 const VkPipelineLibraryCreateInfoKHR *libs_info =
3078 vk_find_struct_const(pCreateInfo->pNext,
3079 PIPELINE_LIBRARY_CREATE_INFO_KHR);
3080
3081 pipeline = vk_zalloc2(&device->vk.alloc, pAllocator, sizeof(*pipeline), 8,
3082 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
3083 if (pipeline == NULL)
3084 return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
3085
3086 result = anv_pipeline_init(&pipeline->base.base, device,
3087 ANV_PIPELINE_GRAPHICS_LIB, flags,
3088 pAllocator);
3089 if (result != VK_SUCCESS) {
3090 vk_free2(&device->vk.alloc, pAllocator, pipeline);
3091 if (result == VK_PIPELINE_COMPILE_REQUIRED)
3092 *pPipeline = VK_NULL_HANDLE;
3093 return result;
3094 }
3095
3096 /* Capture the retain state before we compile/load any shader. */
3097 pipeline->retain_shaders =
3098 (flags & VK_PIPELINE_CREATE_2_RETAIN_LINK_TIME_OPTIMIZATION_INFO_BIT_EXT) != 0;
3099
3100 /* If we have libraries, import them first. */
3101 if (libs_info) {
3102 for (uint32_t i = 0; i < libs_info->libraryCount; i++) {
3103 ANV_FROM_HANDLE(anv_pipeline, pipeline_lib, libs_info->pLibraries[i]);
3104 struct anv_graphics_lib_pipeline *gfx_pipeline_lib =
3105 anv_pipeline_to_graphics_lib(pipeline_lib);
3106
3107 vk_graphics_pipeline_state_merge(&pipeline->state, &gfx_pipeline_lib->state);
3108 anv_graphics_pipeline_import_lib(&pipeline->base,
3109 false /* link_optimize */,
3110 pipeline->retain_shaders,
3111 stages, gfx_pipeline_lib);
3112 }
3113 }
3114
3115 result = vk_graphics_pipeline_state_fill(&device->vk,
3116 &pipeline->state, pCreateInfo,
3117 NULL /* driver_rp */,
3118 0 /* driver_rp_flags */,
3119 &pipeline->all_state, NULL, 0, NULL);
3120 if (result != VK_SUCCESS) {
3121 anv_pipeline_finish(&pipeline->base.base, device);
3122 vk_free2(&device->vk.alloc, pAllocator, pipeline);
3123 return result;
3124 }
3125
3126 pipeline->base.base.active_stages = pipeline->state.shader_stages;
3127
3128 /* After we've imported all the libraries' layouts, import the pipeline
3129 * layout and hash the whole lot.
3130 */
3131 ANV_FROM_HANDLE(anv_pipeline_layout, pipeline_layout, pCreateInfo->layout);
3132 if (pipeline_layout != NULL) {
3133 anv_graphics_pipeline_import_layout(&pipeline->base,
3134 &pipeline_layout->sets_layout);
3135 }
3136
3137 anv_pipeline_sets_layout_hash(&pipeline->base.base.layout);
3138
3139 /* Compile shaders. We can skip this if there are no active stage in that
3140 * pipeline.
3141 */
3142 if (pipeline->base.base.active_stages != 0) {
3143 result = anv_graphics_pipeline_compile(&pipeline->base, stages,
3144 cache, &pipeline_feedback,
3145 pCreateInfo, &pipeline->state);
3146 if (result != VK_SUCCESS) {
3147 anv_pipeline_finish(&pipeline->base.base, device);
3148 vk_free2(&device->vk.alloc, pAllocator, pipeline);
3149 return result;
3150 }
3151 }
3152
3153 pipeline_feedback.duration = os_time_get_nano() - pipeline_start;
3154
3155 anv_fill_pipeline_creation_feedback(&pipeline->base, &pipeline_feedback,
3156 pCreateInfo, stages);
3157
3158 anv_graphics_lib_validate_shaders(
3159 pipeline,
3160 flags & VK_PIPELINE_CREATE_2_RETAIN_LINK_TIME_OPTIMIZATION_INFO_BIT_EXT);
3161
3162 *pPipeline = anv_pipeline_to_handle(&pipeline->base.base);
3163
3164 return VK_SUCCESS;
3165 }
3166
3167 static VkResult
anv_graphics_pipeline_create(struct anv_device * device,struct vk_pipeline_cache * cache,const VkGraphicsPipelineCreateInfo * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkPipeline * pPipeline)3168 anv_graphics_pipeline_create(struct anv_device *device,
3169 struct vk_pipeline_cache *cache,
3170 const VkGraphicsPipelineCreateInfo *pCreateInfo,
3171 const VkAllocationCallbacks *pAllocator,
3172 VkPipeline *pPipeline)
3173 {
3174 struct anv_pipeline_stage stages[ANV_GRAPHICS_SHADER_STAGE_COUNT] = {};
3175 VkPipelineCreationFeedback pipeline_feedback = {
3176 .flags = VK_PIPELINE_CREATION_FEEDBACK_VALID_BIT,
3177 };
3178 int64_t pipeline_start = os_time_get_nano();
3179
3180 struct anv_graphics_pipeline *pipeline;
3181 VkResult result;
3182
3183 const VkPipelineCreateFlags2KHR flags =
3184 vk_graphics_pipeline_create_flags(pCreateInfo);
3185 assert((flags & VK_PIPELINE_CREATE_2_LIBRARY_BIT_KHR) == 0);
3186
3187 const VkPipelineLibraryCreateInfoKHR *libs_info =
3188 vk_find_struct_const(pCreateInfo->pNext,
3189 PIPELINE_LIBRARY_CREATE_INFO_KHR);
3190
3191 pipeline = vk_zalloc2(&device->vk.alloc, pAllocator, sizeof(*pipeline), 8,
3192 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
3193 if (pipeline == NULL)
3194 return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
3195
3196 /* Initialize some information required by shaders */
3197 result = anv_pipeline_init(&pipeline->base.base, device,
3198 ANV_PIPELINE_GRAPHICS, flags,
3199 pAllocator);
3200 if (result != VK_SUCCESS) {
3201 vk_free2(&device->vk.alloc, pAllocator, pipeline);
3202 return result;
3203 }
3204
3205 const bool link_optimize =
3206 (flags & VK_PIPELINE_CREATE_2_LINK_TIME_OPTIMIZATION_BIT_EXT) != 0;
3207
3208 struct vk_graphics_pipeline_all_state all;
3209 struct vk_graphics_pipeline_state state = { };
3210
3211 /* If we have libraries, import them first. */
3212 if (libs_info) {
3213 for (uint32_t i = 0; i < libs_info->libraryCount; i++) {
3214 ANV_FROM_HANDLE(anv_pipeline, pipeline_lib, libs_info->pLibraries[i]);
3215 struct anv_graphics_lib_pipeline *gfx_pipeline_lib =
3216 anv_pipeline_to_graphics_lib(pipeline_lib);
3217
3218 /* If we have link time optimization, all libraries must be created
3219 * with
3220 * VK_PIPELINE_CREATE_RETAIN_LINK_TIME_OPTIMIZATION_INFO_BIT_EXT.
3221 */
3222 assert(!link_optimize || gfx_pipeline_lib->retain_shaders);
3223
3224 vk_graphics_pipeline_state_merge(&state, &gfx_pipeline_lib->state);
3225 anv_graphics_pipeline_import_lib(&pipeline->base,
3226 link_optimize,
3227 false,
3228 stages,
3229 gfx_pipeline_lib);
3230 }
3231 }
3232
3233 result = vk_graphics_pipeline_state_fill(&device->vk, &state, pCreateInfo,
3234 NULL /* driver_rp */,
3235 0 /* driver_rp_flags */,
3236 &all, NULL, 0, NULL);
3237 if (result != VK_SUCCESS) {
3238 anv_pipeline_finish(&pipeline->base.base, device);
3239 vk_free2(&device->vk.alloc, pAllocator, pipeline);
3240 return result;
3241 }
3242
3243 pipeline->dynamic_state.vi = &pipeline->vertex_input;
3244 pipeline->dynamic_state.ms.sample_locations = &pipeline->base.sample_locations;
3245 vk_dynamic_graphics_state_fill(&pipeline->dynamic_state, &state);
3246
3247 pipeline->base.base.active_stages = state.shader_stages;
3248
3249 /* Sanity check on the shaders */
3250 assert(pipeline->base.base.active_stages & VK_SHADER_STAGE_VERTEX_BIT ||
3251 pipeline->base.base.active_stages & VK_SHADER_STAGE_MESH_BIT_EXT);
3252
3253 if (anv_pipeline_is_mesh(pipeline)) {
3254 assert(device->physical->vk.supported_extensions.EXT_mesh_shader);
3255 }
3256
3257 /* After we've imported all the libraries' layouts, import the pipeline
3258 * layout and hash the whole lot.
3259 */
3260 ANV_FROM_HANDLE(anv_pipeline_layout, pipeline_layout, pCreateInfo->layout);
3261 if (pipeline_layout != NULL) {
3262 anv_graphics_pipeline_import_layout(&pipeline->base,
3263 &pipeline_layout->sets_layout);
3264 }
3265
3266 anv_pipeline_sets_layout_hash(&pipeline->base.base.layout);
3267
3268 /* Compile shaders, all required information should be have been copied in
3269 * the previous step. We can skip this if there are no active stage in that
3270 * pipeline.
3271 */
3272 result = anv_graphics_pipeline_compile(&pipeline->base, stages,
3273 cache, &pipeline_feedback,
3274 pCreateInfo, &state);
3275 if (result != VK_SUCCESS) {
3276 anv_pipeline_finish(&pipeline->base.base, device);
3277 vk_free2(&device->vk.alloc, pAllocator, pipeline);
3278 return result;
3279 }
3280
3281 /* Prepare a batch for the commands and emit all the non dynamic ones.
3282 */
3283 anv_batch_set_storage(&pipeline->base.base.batch, ANV_NULL_ADDRESS,
3284 pipeline->batch_data, sizeof(pipeline->batch_data));
3285
3286 if (pipeline->base.base.active_stages & VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT)
3287 pipeline->base.base.active_stages |= VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT;
3288
3289 if (anv_pipeline_is_mesh(pipeline))
3290 assert(device->physical->vk.supported_extensions.EXT_mesh_shader);
3291
3292 anv_graphics_pipeline_emit(pipeline, &state);
3293
3294 pipeline_feedback.duration = os_time_get_nano() - pipeline_start;
3295
3296 anv_fill_pipeline_creation_feedback(&pipeline->base, &pipeline_feedback,
3297 pCreateInfo, stages);
3298
3299 ANV_RMV(graphics_pipeline_create, device, pipeline, false);
3300
3301 *pPipeline = anv_pipeline_to_handle(&pipeline->base.base);
3302
3303 return pipeline->base.base.batch.status;
3304 }
3305
anv_CreateGraphicsPipelines(VkDevice _device,VkPipelineCache pipelineCache,uint32_t count,const VkGraphicsPipelineCreateInfo * pCreateInfos,const VkAllocationCallbacks * pAllocator,VkPipeline * pPipelines)3306 VkResult anv_CreateGraphicsPipelines(
3307 VkDevice _device,
3308 VkPipelineCache pipelineCache,
3309 uint32_t count,
3310 const VkGraphicsPipelineCreateInfo* pCreateInfos,
3311 const VkAllocationCallbacks* pAllocator,
3312 VkPipeline* pPipelines)
3313 {
3314 ANV_FROM_HANDLE(anv_device, device, _device);
3315 ANV_FROM_HANDLE(vk_pipeline_cache, pipeline_cache, pipelineCache);
3316
3317 VkResult result = VK_SUCCESS;
3318
3319 unsigned i;
3320 for (i = 0; i < count; i++) {
3321 assert(pCreateInfos[i].sType == VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO);
3322
3323 const VkPipelineCreateFlags2KHR flags =
3324 vk_graphics_pipeline_create_flags(&pCreateInfos[i]);
3325 VkResult res;
3326 if (flags & VK_PIPELINE_CREATE_2_LIBRARY_BIT_KHR) {
3327 res = anv_graphics_lib_pipeline_create(device, pipeline_cache,
3328 &pCreateInfos[i],
3329 pAllocator,
3330 &pPipelines[i]);
3331 } else {
3332 res = anv_graphics_pipeline_create(device,
3333 pipeline_cache,
3334 &pCreateInfos[i],
3335 pAllocator, &pPipelines[i]);
3336 }
3337
3338 if (res == VK_SUCCESS)
3339 continue;
3340
3341 /* Bail out on the first error != VK_PIPELINE_COMPILE_REQUIRED as it
3342 * is not obvious what error should be report upon 2 different failures.
3343 * */
3344 result = res;
3345 if (res != VK_PIPELINE_COMPILE_REQUIRED)
3346 break;
3347
3348 pPipelines[i] = VK_NULL_HANDLE;
3349
3350 if (flags & VK_PIPELINE_CREATE_2_EARLY_RETURN_ON_FAILURE_BIT_KHR)
3351 break;
3352 }
3353
3354 for (; i < count; i++)
3355 pPipelines[i] = VK_NULL_HANDLE;
3356
3357 return result;
3358 }
3359
3360 static bool
should_remat_cb(nir_instr * instr,void * data)3361 should_remat_cb(nir_instr *instr, void *data)
3362 {
3363 if (instr->type != nir_instr_type_intrinsic)
3364 return false;
3365
3366 return nir_instr_as_intrinsic(instr)->intrinsic == nir_intrinsic_resource_intel;
3367 }
3368
3369 static VkResult
compile_upload_rt_shader(struct anv_ray_tracing_pipeline * pipeline,struct vk_pipeline_cache * cache,nir_shader * nir,struct anv_pipeline_stage * stage,void * mem_ctx)3370 compile_upload_rt_shader(struct anv_ray_tracing_pipeline *pipeline,
3371 struct vk_pipeline_cache *cache,
3372 nir_shader *nir,
3373 struct anv_pipeline_stage *stage,
3374 void *mem_ctx)
3375 {
3376 const struct brw_compiler *compiler =
3377 pipeline->base.device->physical->compiler;
3378 const struct intel_device_info *devinfo = compiler->devinfo;
3379
3380 nir_shader **resume_shaders = NULL;
3381 uint32_t num_resume_shaders = 0;
3382 if (nir->info.stage != MESA_SHADER_COMPUTE) {
3383 const nir_lower_shader_calls_options opts = {
3384 .address_format = nir_address_format_64bit_global,
3385 .stack_alignment = BRW_BTD_STACK_ALIGN,
3386 .localized_loads = true,
3387 .vectorizer_callback = brw_nir_should_vectorize_mem,
3388 .vectorizer_data = NULL,
3389 .should_remat_callback = should_remat_cb,
3390 };
3391
3392 NIR_PASS(_, nir, nir_lower_shader_calls, &opts,
3393 &resume_shaders, &num_resume_shaders, mem_ctx);
3394 NIR_PASS(_, nir, brw_nir_lower_shader_calls, &stage->key.bs);
3395 NIR_PASS_V(nir, brw_nir_lower_rt_intrinsics, devinfo);
3396 }
3397
3398 for (unsigned i = 0; i < num_resume_shaders; i++) {
3399 NIR_PASS(_,resume_shaders[i], brw_nir_lower_shader_calls, &stage->key.bs);
3400 NIR_PASS_V(resume_shaders[i], brw_nir_lower_rt_intrinsics, devinfo);
3401 }
3402
3403 struct brw_compile_bs_params params = {
3404 .base = {
3405 .nir = nir,
3406 .stats = stage->stats,
3407 .log_data = pipeline->base.device,
3408 .mem_ctx = mem_ctx,
3409 },
3410 .key = &stage->key.bs,
3411 .prog_data = &stage->prog_data.bs,
3412 .num_resume_shaders = num_resume_shaders,
3413 .resume_shaders = resume_shaders,
3414 };
3415
3416 stage->code = brw_compile_bs(compiler, ¶ms);
3417 if (stage->code == NULL)
3418 return vk_error(pipeline, VK_ERROR_OUT_OF_HOST_MEMORY);
3419
3420 /* Ray-tracing shaders don't have a "real" bind map */
3421 struct anv_pipeline_bind_map empty_bind_map = {};
3422
3423 struct anv_shader_upload_params upload_params = {
3424 .stage = stage->stage,
3425 .key_data = &stage->cache_key,
3426 .key_size = sizeof(stage->cache_key),
3427 .kernel_data = stage->code,
3428 .kernel_size = stage->prog_data.base.program_size,
3429 .prog_data = &stage->prog_data.base,
3430 .prog_data_size = brw_prog_data_size(stage->stage),
3431 .stats = stage->stats,
3432 .num_stats = 1,
3433 .bind_map = &empty_bind_map,
3434 .push_desc_info = &stage->push_desc_info,
3435 .dynamic_push_values = stage->dynamic_push_values,
3436 };
3437
3438 stage->bin =
3439 anv_device_upload_kernel(pipeline->base.device, cache, &upload_params);
3440 if (stage->bin == NULL)
3441 return vk_error(pipeline, VK_ERROR_OUT_OF_HOST_MEMORY);
3442
3443 anv_pipeline_add_executables(&pipeline->base, stage);
3444
3445 return VK_SUCCESS;
3446 }
3447
3448 static bool
is_rt_stack_size_dynamic(const VkRayTracingPipelineCreateInfoKHR * info)3449 is_rt_stack_size_dynamic(const VkRayTracingPipelineCreateInfoKHR *info)
3450 {
3451 if (info->pDynamicState == NULL)
3452 return false;
3453
3454 for (unsigned i = 0; i < info->pDynamicState->dynamicStateCount; i++) {
3455 if (info->pDynamicState->pDynamicStates[i] ==
3456 VK_DYNAMIC_STATE_RAY_TRACING_PIPELINE_STACK_SIZE_KHR)
3457 return true;
3458 }
3459
3460 return false;
3461 }
3462
3463 static void
anv_pipeline_compute_ray_tracing_stacks(struct anv_ray_tracing_pipeline * pipeline,const VkRayTracingPipelineCreateInfoKHR * info,uint32_t * stack_max)3464 anv_pipeline_compute_ray_tracing_stacks(struct anv_ray_tracing_pipeline *pipeline,
3465 const VkRayTracingPipelineCreateInfoKHR *info,
3466 uint32_t *stack_max)
3467 {
3468 if (is_rt_stack_size_dynamic(info)) {
3469 pipeline->stack_size = 0; /* 0 means dynamic */
3470 } else {
3471 /* From the Vulkan spec:
3472 *
3473 * "If the stack size is not set explicitly, the stack size for a
3474 * pipeline is:
3475 *
3476 * rayGenStackMax +
3477 * min(1, maxPipelineRayRecursionDepth) ×
3478 * max(closestHitStackMax, missStackMax,
3479 * intersectionStackMax + anyHitStackMax) +
3480 * max(0, maxPipelineRayRecursionDepth-1) ×
3481 * max(closestHitStackMax, missStackMax) +
3482 * 2 × callableStackMax"
3483 */
3484 pipeline->stack_size =
3485 stack_max[MESA_SHADER_RAYGEN] +
3486 MIN2(1, info->maxPipelineRayRecursionDepth) *
3487 MAX4(stack_max[MESA_SHADER_CLOSEST_HIT],
3488 stack_max[MESA_SHADER_MISS],
3489 stack_max[MESA_SHADER_INTERSECTION],
3490 stack_max[MESA_SHADER_ANY_HIT]) +
3491 MAX2(0, (int)info->maxPipelineRayRecursionDepth - 1) *
3492 MAX2(stack_max[MESA_SHADER_CLOSEST_HIT],
3493 stack_max[MESA_SHADER_MISS]) +
3494 2 * stack_max[MESA_SHADER_CALLABLE];
3495
3496 /* This is an extremely unlikely case but we need to set it to some
3497 * non-zero value so that we don't accidentally think it's dynamic.
3498 * Our minimum stack size is 2KB anyway so we could set to any small
3499 * value we like.
3500 */
3501 if (pipeline->stack_size == 0)
3502 pipeline->stack_size = 1;
3503 }
3504 }
3505
3506 static enum brw_rt_ray_flags
anv_pipeline_get_pipeline_ray_flags(VkPipelineCreateFlags2KHR flags)3507 anv_pipeline_get_pipeline_ray_flags(VkPipelineCreateFlags2KHR flags)
3508 {
3509 uint32_t ray_flags = 0;
3510
3511 const bool rt_skip_triangles =
3512 flags & VK_PIPELINE_CREATE_2_RAY_TRACING_SKIP_TRIANGLES_BIT_KHR;
3513 const bool rt_skip_aabbs =
3514 flags & VK_PIPELINE_CREATE_2_RAY_TRACING_SKIP_AABBS_BIT_KHR;
3515 assert(!(rt_skip_triangles && rt_skip_aabbs));
3516
3517 if (rt_skip_triangles)
3518 ray_flags |= BRW_RT_RAY_FLAG_SKIP_TRIANGLES;
3519 else if (rt_skip_aabbs)
3520 ray_flags |= BRW_RT_RAY_FLAG_SKIP_AABBS;
3521
3522 return ray_flags;
3523 }
3524
3525 static struct anv_pipeline_stage *
anv_pipeline_init_ray_tracing_stages(struct anv_ray_tracing_pipeline * pipeline,const VkRayTracingPipelineCreateInfoKHR * info,void * tmp_pipeline_ctx)3526 anv_pipeline_init_ray_tracing_stages(struct anv_ray_tracing_pipeline *pipeline,
3527 const VkRayTracingPipelineCreateInfoKHR *info,
3528 void *tmp_pipeline_ctx)
3529 {
3530 struct anv_device *device = pipeline->base.device;
3531 /* Create enough stage entries for all shader modules plus potential
3532 * combinaisons in the groups.
3533 */
3534 struct anv_pipeline_stage *stages =
3535 rzalloc_array(tmp_pipeline_ctx, struct anv_pipeline_stage, info->stageCount);
3536
3537 enum brw_rt_ray_flags ray_flags =
3538 anv_pipeline_get_pipeline_ray_flags(pipeline->base.flags);
3539
3540 for (uint32_t i = 0; i < info->stageCount; i++) {
3541 const VkPipelineShaderStageCreateInfo *sinfo = &info->pStages[i];
3542 if (vk_pipeline_shader_stage_is_null(sinfo))
3543 continue;
3544
3545 int64_t stage_start = os_time_get_nano();
3546
3547 stages[i] = (struct anv_pipeline_stage) {
3548 .stage = vk_to_mesa_shader_stage(sinfo->stage),
3549 .pipeline_pNext = info->pNext,
3550 .info = sinfo,
3551 .cache_key = {
3552 .stage = vk_to_mesa_shader_stage(sinfo->stage),
3553 },
3554 .feedback = {
3555 .flags = VK_PIPELINE_CREATION_FEEDBACK_VALID_BIT,
3556 },
3557 };
3558
3559 pipeline->base.active_stages |= sinfo->stage;
3560
3561 anv_stage_write_shader_hash(&stages[i], device);
3562
3563 populate_bs_prog_key(&stages[i],
3564 pipeline->base.device,
3565 ray_flags);
3566
3567 if (stages[i].stage != MESA_SHADER_INTERSECTION) {
3568 anv_pipeline_hash_ray_tracing_shader(pipeline, &stages[i],
3569 stages[i].cache_key.sha1);
3570 }
3571
3572 stages[i].feedback.duration += os_time_get_nano() - stage_start;
3573 }
3574
3575 for (uint32_t i = 0; i < info->groupCount; i++) {
3576 const VkRayTracingShaderGroupCreateInfoKHR *ginfo = &info->pGroups[i];
3577
3578 if (ginfo->type != VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_KHR)
3579 continue;
3580
3581 int64_t stage_start = os_time_get_nano();
3582
3583 uint32_t intersection_idx = ginfo->intersectionShader;
3584 assert(intersection_idx < info->stageCount);
3585
3586 uint32_t any_hit_idx = ginfo->anyHitShader;
3587 if (any_hit_idx != VK_SHADER_UNUSED_KHR) {
3588 assert(any_hit_idx < info->stageCount);
3589 anv_pipeline_hash_ray_tracing_combined_shader(pipeline,
3590 &stages[intersection_idx],
3591 &stages[any_hit_idx],
3592 stages[intersection_idx].cache_key.sha1);
3593 } else {
3594 anv_pipeline_hash_ray_tracing_shader(pipeline,
3595 &stages[intersection_idx],
3596 stages[intersection_idx].cache_key.sha1);
3597 }
3598
3599 stages[intersection_idx].feedback.duration += os_time_get_nano() - stage_start;
3600 }
3601
3602 return stages;
3603 }
3604
3605 static bool
anv_ray_tracing_pipeline_load_cached_shaders(struct anv_ray_tracing_pipeline * pipeline,struct vk_pipeline_cache * cache,const VkRayTracingPipelineCreateInfoKHR * info,struct anv_pipeline_stage * stages)3606 anv_ray_tracing_pipeline_load_cached_shaders(struct anv_ray_tracing_pipeline *pipeline,
3607 struct vk_pipeline_cache *cache,
3608 const VkRayTracingPipelineCreateInfoKHR *info,
3609 struct anv_pipeline_stage *stages)
3610 {
3611 uint32_t shaders = 0, cache_hits = 0;
3612 for (uint32_t i = 0; i < info->stageCount; i++) {
3613 if (stages[i].info == NULL)
3614 continue;
3615
3616 shaders++;
3617
3618 int64_t stage_start = os_time_get_nano();
3619
3620 bool cache_hit;
3621 stages[i].bin = anv_device_search_for_kernel(pipeline->base.device, cache,
3622 &stages[i].cache_key,
3623 sizeof(stages[i].cache_key),
3624 &cache_hit);
3625 if (cache_hit) {
3626 cache_hits++;
3627 stages[i].feedback.flags |=
3628 VK_PIPELINE_CREATION_FEEDBACK_APPLICATION_PIPELINE_CACHE_HIT_BIT;
3629 }
3630
3631 if (stages[i].bin != NULL)
3632 anv_pipeline_add_executables(&pipeline->base, &stages[i]);
3633
3634 stages[i].feedback.duration += os_time_get_nano() - stage_start;
3635 }
3636
3637 return cache_hits == shaders;
3638 }
3639
3640 static VkResult
anv_pipeline_compile_ray_tracing(struct anv_ray_tracing_pipeline * pipeline,void * tmp_pipeline_ctx,struct anv_pipeline_stage * stages,struct vk_pipeline_cache * cache,const VkRayTracingPipelineCreateInfoKHR * info)3641 anv_pipeline_compile_ray_tracing(struct anv_ray_tracing_pipeline *pipeline,
3642 void *tmp_pipeline_ctx,
3643 struct anv_pipeline_stage *stages,
3644 struct vk_pipeline_cache *cache,
3645 const VkRayTracingPipelineCreateInfoKHR *info)
3646 {
3647 const struct intel_device_info *devinfo = pipeline->base.device->info;
3648 VkResult result;
3649
3650 VkPipelineCreationFeedback pipeline_feedback = {
3651 .flags = VK_PIPELINE_CREATION_FEEDBACK_VALID_BIT,
3652 };
3653 int64_t pipeline_start = os_time_get_nano();
3654
3655 const bool skip_cache_lookup =
3656 (pipeline->base.flags & VK_PIPELINE_CREATE_CAPTURE_INTERNAL_REPRESENTATIONS_BIT_KHR);
3657
3658 if (!skip_cache_lookup &&
3659 anv_ray_tracing_pipeline_load_cached_shaders(pipeline, cache, info, stages)) {
3660 pipeline_feedback.flags |=
3661 VK_PIPELINE_CREATION_FEEDBACK_APPLICATION_PIPELINE_CACHE_HIT_BIT;
3662 goto done;
3663 }
3664
3665 if (pipeline->base.flags & VK_PIPELINE_CREATE_2_FAIL_ON_PIPELINE_COMPILE_REQUIRED_BIT_KHR)
3666 return VK_PIPELINE_COMPILE_REQUIRED;
3667
3668 for (uint32_t i = 0; i < info->stageCount; i++) {
3669 if (stages[i].info == NULL)
3670 continue;
3671
3672 int64_t stage_start = os_time_get_nano();
3673
3674 stages[i].nir = anv_pipeline_stage_get_nir(&pipeline->base, cache,
3675 tmp_pipeline_ctx, &stages[i]);
3676 if (stages[i].nir == NULL)
3677 return vk_error(pipeline, VK_ERROR_OUT_OF_HOST_MEMORY);
3678
3679 anv_pipeline_nir_preprocess(&pipeline->base, &stages[i]);
3680
3681 anv_pipeline_lower_nir(&pipeline->base, tmp_pipeline_ctx, &stages[i],
3682 &pipeline->base.layout, 0 /* view_mask */,
3683 false /* use_primitive_replication */);
3684
3685 stages[i].feedback.duration += os_time_get_nano() - stage_start;
3686 }
3687
3688 for (uint32_t i = 0; i < info->stageCount; i++) {
3689 if (stages[i].info == NULL)
3690 continue;
3691
3692 /* Shader found in cache already. */
3693 if (stages[i].bin != NULL)
3694 continue;
3695
3696 /* We handle intersection shaders as part of the group */
3697 if (stages[i].stage == MESA_SHADER_INTERSECTION)
3698 continue;
3699
3700 int64_t stage_start = os_time_get_nano();
3701
3702 void *tmp_stage_ctx = ralloc_context(tmp_pipeline_ctx);
3703
3704 nir_shader *nir = nir_shader_clone(tmp_stage_ctx, stages[i].nir);
3705 switch (stages[i].stage) {
3706 case MESA_SHADER_RAYGEN:
3707 brw_nir_lower_raygen(nir);
3708 break;
3709
3710 case MESA_SHADER_ANY_HIT:
3711 brw_nir_lower_any_hit(nir, devinfo);
3712 break;
3713
3714 case MESA_SHADER_CLOSEST_HIT:
3715 brw_nir_lower_closest_hit(nir);
3716 break;
3717
3718 case MESA_SHADER_MISS:
3719 brw_nir_lower_miss(nir);
3720 break;
3721
3722 case MESA_SHADER_INTERSECTION:
3723 unreachable("These are handled later");
3724
3725 case MESA_SHADER_CALLABLE:
3726 brw_nir_lower_callable(nir);
3727 break;
3728
3729 default:
3730 unreachable("Invalid ray-tracing shader stage");
3731 }
3732
3733 result = compile_upload_rt_shader(pipeline, cache, nir, &stages[i],
3734 tmp_stage_ctx);
3735 if (result != VK_SUCCESS) {
3736 ralloc_free(tmp_stage_ctx);
3737 return result;
3738 }
3739
3740 ralloc_free(tmp_stage_ctx);
3741
3742 stages[i].feedback.duration += os_time_get_nano() - stage_start;
3743 }
3744
3745 done:
3746 for (uint32_t i = 0; i < info->groupCount; i++) {
3747 const VkRayTracingShaderGroupCreateInfoKHR *ginfo = &info->pGroups[i];
3748 struct anv_rt_shader_group *group = &pipeline->groups[i];
3749 group->type = ginfo->type;
3750 switch (ginfo->type) {
3751 case VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_KHR:
3752 assert(ginfo->generalShader < info->stageCount);
3753 group->general = stages[ginfo->generalShader].bin;
3754 break;
3755
3756 case VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_KHR:
3757 if (ginfo->anyHitShader < info->stageCount)
3758 group->any_hit = stages[ginfo->anyHitShader].bin;
3759
3760 if (ginfo->closestHitShader < info->stageCount)
3761 group->closest_hit = stages[ginfo->closestHitShader].bin;
3762 break;
3763
3764 case VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_KHR: {
3765 if (ginfo->closestHitShader < info->stageCount)
3766 group->closest_hit = stages[ginfo->closestHitShader].bin;
3767
3768 uint32_t intersection_idx = info->pGroups[i].intersectionShader;
3769 assert(intersection_idx < info->stageCount);
3770
3771 /* Only compile this stage if not already found in the cache. */
3772 if (stages[intersection_idx].bin == NULL) {
3773 /* The any-hit and intersection shader have to be combined */
3774 uint32_t any_hit_idx = info->pGroups[i].anyHitShader;
3775 const nir_shader *any_hit = NULL;
3776 if (any_hit_idx < info->stageCount)
3777 any_hit = stages[any_hit_idx].nir;
3778
3779 void *tmp_group_ctx = ralloc_context(tmp_pipeline_ctx);
3780 nir_shader *intersection =
3781 nir_shader_clone(tmp_group_ctx, stages[intersection_idx].nir);
3782
3783 brw_nir_lower_combined_intersection_any_hit(intersection, any_hit,
3784 devinfo);
3785
3786 result = compile_upload_rt_shader(pipeline, cache,
3787 intersection,
3788 &stages[intersection_idx],
3789 tmp_group_ctx);
3790 ralloc_free(tmp_group_ctx);
3791 if (result != VK_SUCCESS)
3792 return result;
3793 }
3794
3795 group->intersection = stages[intersection_idx].bin;
3796 break;
3797 }
3798
3799 default:
3800 unreachable("Invalid ray tracing shader group type");
3801 }
3802 }
3803
3804 pipeline_feedback.duration = os_time_get_nano() - pipeline_start;
3805
3806 const VkPipelineCreationFeedbackCreateInfo *create_feedback =
3807 vk_find_struct_const(info->pNext, PIPELINE_CREATION_FEEDBACK_CREATE_INFO);
3808 if (create_feedback) {
3809 *create_feedback->pPipelineCreationFeedback = pipeline_feedback;
3810
3811 uint32_t stage_count = create_feedback->pipelineStageCreationFeedbackCount;
3812 assert(stage_count == 0 || info->stageCount == stage_count);
3813 for (uint32_t i = 0; i < stage_count; i++) {
3814 gl_shader_stage s = vk_to_mesa_shader_stage(info->pStages[i].stage);
3815 create_feedback->pPipelineStageCreationFeedbacks[i] = stages[s].feedback;
3816 }
3817 }
3818
3819 return VK_SUCCESS;
3820 }
3821
3822 VkResult
anv_device_init_rt_shaders(struct anv_device * device)3823 anv_device_init_rt_shaders(struct anv_device *device)
3824 {
3825 device->bvh_build_method = ANV_BVH_BUILD_METHOD_NEW_SAH;
3826
3827 if (!device->vk.enabled_extensions.KHR_ray_tracing_pipeline)
3828 return VK_SUCCESS;
3829
3830 bool cache_hit;
3831
3832 struct anv_push_descriptor_info empty_push_desc_info = {};
3833 struct anv_pipeline_bind_map empty_bind_map = {};
3834 struct brw_rt_trampoline {
3835 char name[16];
3836 struct brw_cs_prog_key key;
3837 } trampoline_key = {
3838 .name = "rt-trampoline",
3839 };
3840 device->rt_trampoline =
3841 anv_device_search_for_kernel(device, device->internal_cache,
3842 &trampoline_key, sizeof(trampoline_key),
3843 &cache_hit);
3844 if (device->rt_trampoline == NULL) {
3845
3846 void *tmp_ctx = ralloc_context(NULL);
3847 nir_shader *trampoline_nir =
3848 brw_nir_create_raygen_trampoline(device->physical->compiler, tmp_ctx);
3849
3850 trampoline_nir->info.subgroup_size = SUBGROUP_SIZE_REQUIRE_16;
3851
3852 uint32_t dummy_params[4] = { 0, };
3853 struct brw_cs_prog_data trampoline_prog_data = {
3854 .base.nr_params = 4,
3855 .base.param = dummy_params,
3856 .uses_inline_data = true,
3857 .uses_btd_stack_ids = true,
3858 };
3859 struct brw_compile_cs_params params = {
3860 .base = {
3861 .nir = trampoline_nir,
3862 .log_data = device,
3863 .mem_ctx = tmp_ctx,
3864 },
3865 .key = &trampoline_key.key,
3866 .prog_data = &trampoline_prog_data,
3867 };
3868 const unsigned *tramp_data =
3869 brw_compile_cs(device->physical->compiler, ¶ms);
3870
3871 struct anv_shader_upload_params upload_params = {
3872 .stage = MESA_SHADER_COMPUTE,
3873 .key_data = &trampoline_key,
3874 .key_size = sizeof(trampoline_key),
3875 .kernel_data = tramp_data,
3876 .kernel_size = trampoline_prog_data.base.program_size,
3877 .prog_data = &trampoline_prog_data.base,
3878 .prog_data_size = sizeof(trampoline_prog_data),
3879 .bind_map = &empty_bind_map,
3880 .push_desc_info = &empty_push_desc_info,
3881 };
3882
3883 device->rt_trampoline =
3884 anv_device_upload_kernel(device, device->internal_cache,
3885 &upload_params);
3886
3887 ralloc_free(tmp_ctx);
3888
3889 if (device->rt_trampoline == NULL)
3890 return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
3891 }
3892
3893 /* The cache already has a reference and it's not going anywhere so there
3894 * is no need to hold a second reference.
3895 */
3896 anv_shader_bin_unref(device, device->rt_trampoline);
3897
3898 struct brw_rt_trivial_return {
3899 char name[16];
3900 struct brw_bs_prog_key key;
3901 } return_key = {
3902 .name = "rt-trivial-ret",
3903 };
3904 device->rt_trivial_return =
3905 anv_device_search_for_kernel(device, device->internal_cache,
3906 &return_key, sizeof(return_key),
3907 &cache_hit);
3908 if (device->rt_trivial_return == NULL) {
3909 void *tmp_ctx = ralloc_context(NULL);
3910 nir_shader *trivial_return_nir =
3911 brw_nir_create_trivial_return_shader(device->physical->compiler, tmp_ctx);
3912
3913 NIR_PASS_V(trivial_return_nir, brw_nir_lower_rt_intrinsics, device->info);
3914
3915 struct brw_bs_prog_data return_prog_data = { 0, };
3916 struct brw_compile_bs_params params = {
3917 .base = {
3918 .nir = trivial_return_nir,
3919 .log_data = device,
3920 .mem_ctx = tmp_ctx,
3921 },
3922 .key = &return_key.key,
3923 .prog_data = &return_prog_data,
3924 };
3925 const unsigned *return_data =
3926 brw_compile_bs(device->physical->compiler, ¶ms);
3927
3928 struct anv_shader_upload_params upload_params = {
3929 .stage = MESA_SHADER_CALLABLE,
3930 .key_data = &return_key,
3931 .key_size = sizeof(return_key),
3932 .kernel_data = return_data,
3933 .kernel_size = return_prog_data.base.program_size,
3934 .prog_data = &return_prog_data.base,
3935 .prog_data_size = sizeof(return_prog_data),
3936 .bind_map = &empty_bind_map,
3937 .push_desc_info = &empty_push_desc_info,
3938 };
3939
3940 device->rt_trivial_return =
3941 anv_device_upload_kernel(device, device->internal_cache,
3942 &upload_params);
3943
3944 ralloc_free(tmp_ctx);
3945
3946 if (device->rt_trivial_return == NULL)
3947 return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
3948 }
3949
3950 /* The cache already has a reference and it's not going anywhere so there
3951 * is no need to hold a second reference.
3952 */
3953 anv_shader_bin_unref(device, device->rt_trivial_return);
3954
3955 return VK_SUCCESS;
3956 }
3957
3958 void
anv_device_finish_rt_shaders(struct anv_device * device)3959 anv_device_finish_rt_shaders(struct anv_device *device)
3960 {
3961 if (!device->vk.enabled_extensions.KHR_ray_tracing_pipeline)
3962 return;
3963 }
3964
3965 static void
anv_ray_tracing_pipeline_init(struct anv_ray_tracing_pipeline * pipeline,struct anv_device * device,struct vk_pipeline_cache * cache,const VkRayTracingPipelineCreateInfoKHR * pCreateInfo,const VkAllocationCallbacks * alloc)3966 anv_ray_tracing_pipeline_init(struct anv_ray_tracing_pipeline *pipeline,
3967 struct anv_device *device,
3968 struct vk_pipeline_cache *cache,
3969 const VkRayTracingPipelineCreateInfoKHR *pCreateInfo,
3970 const VkAllocationCallbacks *alloc)
3971 {
3972 util_dynarray_init(&pipeline->shaders, pipeline->base.mem_ctx);
3973
3974 ANV_FROM_HANDLE(anv_pipeline_layout, pipeline_layout, pCreateInfo->layout);
3975 anv_pipeline_init_layout(&pipeline->base, pipeline_layout);
3976
3977 anv_pipeline_setup_l3_config(&pipeline->base, /* needs_slm */ false);
3978 }
3979
3980 static void
assert_rt_stage_index_valid(const VkRayTracingPipelineCreateInfoKHR * pCreateInfo,uint32_t stage_idx,VkShaderStageFlags valid_stages)3981 assert_rt_stage_index_valid(const VkRayTracingPipelineCreateInfoKHR* pCreateInfo,
3982 uint32_t stage_idx,
3983 VkShaderStageFlags valid_stages)
3984 {
3985 if (stage_idx == VK_SHADER_UNUSED_KHR)
3986 return;
3987
3988 assert(stage_idx <= pCreateInfo->stageCount);
3989 assert(util_bitcount(pCreateInfo->pStages[stage_idx].stage) == 1);
3990 assert(pCreateInfo->pStages[stage_idx].stage & valid_stages);
3991 }
3992
3993 static VkResult
anv_ray_tracing_pipeline_create(VkDevice _device,struct vk_pipeline_cache * cache,const VkRayTracingPipelineCreateInfoKHR * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkPipeline * pPipeline)3994 anv_ray_tracing_pipeline_create(
3995 VkDevice _device,
3996 struct vk_pipeline_cache * cache,
3997 const VkRayTracingPipelineCreateInfoKHR* pCreateInfo,
3998 const VkAllocationCallbacks* pAllocator,
3999 VkPipeline* pPipeline)
4000 {
4001 ANV_FROM_HANDLE(anv_device, device, _device);
4002 VkResult result;
4003
4004 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_KHR);
4005
4006 uint32_t group_count = pCreateInfo->groupCount;
4007 if (pCreateInfo->pLibraryInfo) {
4008 for (uint32_t l = 0; l < pCreateInfo->pLibraryInfo->libraryCount; l++) {
4009 ANV_FROM_HANDLE(anv_pipeline, library,
4010 pCreateInfo->pLibraryInfo->pLibraries[l]);
4011 struct anv_ray_tracing_pipeline *rt_library =
4012 anv_pipeline_to_ray_tracing(library);
4013 group_count += rt_library->group_count;
4014 }
4015 }
4016
4017 VK_MULTIALLOC(ma);
4018 VK_MULTIALLOC_DECL(&ma, struct anv_ray_tracing_pipeline, pipeline, 1);
4019 VK_MULTIALLOC_DECL(&ma, struct anv_rt_shader_group, groups, group_count);
4020 if (!vk_multialloc_zalloc2(&ma, &device->vk.alloc, pAllocator,
4021 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE))
4022 return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
4023
4024 result = anv_pipeline_init(&pipeline->base, device,
4025 ANV_PIPELINE_RAY_TRACING,
4026 vk_rt_pipeline_create_flags(pCreateInfo),
4027 pAllocator);
4028 if (result != VK_SUCCESS) {
4029 vk_free2(&device->vk.alloc, pAllocator, pipeline);
4030 return result;
4031 }
4032
4033 pipeline->group_count = group_count;
4034 pipeline->groups = groups;
4035
4036 ASSERTED const VkShaderStageFlags ray_tracing_stages =
4037 VK_SHADER_STAGE_RAYGEN_BIT_KHR |
4038 VK_SHADER_STAGE_ANY_HIT_BIT_KHR |
4039 VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR |
4040 VK_SHADER_STAGE_MISS_BIT_KHR |
4041 VK_SHADER_STAGE_INTERSECTION_BIT_KHR |
4042 VK_SHADER_STAGE_CALLABLE_BIT_KHR;
4043
4044 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++)
4045 assert((pCreateInfo->pStages[i].stage & ~ray_tracing_stages) == 0);
4046
4047 for (uint32_t i = 0; i < pCreateInfo->groupCount; i++) {
4048 const VkRayTracingShaderGroupCreateInfoKHR *ginfo =
4049 &pCreateInfo->pGroups[i];
4050 assert_rt_stage_index_valid(pCreateInfo, ginfo->generalShader,
4051 VK_SHADER_STAGE_RAYGEN_BIT_KHR |
4052 VK_SHADER_STAGE_MISS_BIT_KHR |
4053 VK_SHADER_STAGE_CALLABLE_BIT_KHR);
4054 assert_rt_stage_index_valid(pCreateInfo, ginfo->closestHitShader,
4055 VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR);
4056 assert_rt_stage_index_valid(pCreateInfo, ginfo->anyHitShader,
4057 VK_SHADER_STAGE_ANY_HIT_BIT_KHR);
4058 assert_rt_stage_index_valid(pCreateInfo, ginfo->intersectionShader,
4059 VK_SHADER_STAGE_INTERSECTION_BIT_KHR);
4060 switch (ginfo->type) {
4061 case VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_KHR:
4062 assert(ginfo->generalShader < pCreateInfo->stageCount);
4063 assert(ginfo->anyHitShader == VK_SHADER_UNUSED_KHR);
4064 assert(ginfo->closestHitShader == VK_SHADER_UNUSED_KHR);
4065 assert(ginfo->intersectionShader == VK_SHADER_UNUSED_KHR);
4066 break;
4067
4068 case VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_KHR:
4069 assert(ginfo->generalShader == VK_SHADER_UNUSED_KHR);
4070 assert(ginfo->intersectionShader == VK_SHADER_UNUSED_KHR);
4071 break;
4072
4073 case VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_KHR:
4074 assert(ginfo->generalShader == VK_SHADER_UNUSED_KHR);
4075 break;
4076
4077 default:
4078 unreachable("Invalid ray-tracing shader group type");
4079 }
4080 }
4081
4082 anv_ray_tracing_pipeline_init(pipeline, device, cache,
4083 pCreateInfo, pAllocator);
4084
4085 void *tmp_ctx = ralloc_context(NULL);
4086
4087 struct anv_pipeline_stage *stages =
4088 anv_pipeline_init_ray_tracing_stages(pipeline, pCreateInfo, tmp_ctx);
4089
4090 result = anv_pipeline_compile_ray_tracing(pipeline, tmp_ctx, stages,
4091 cache, pCreateInfo);
4092 if (result != VK_SUCCESS) {
4093 ralloc_free(tmp_ctx);
4094 util_dynarray_foreach(&pipeline->shaders, struct anv_shader_bin *, shader)
4095 anv_shader_bin_unref(device, *shader);
4096 anv_pipeline_finish(&pipeline->base, device);
4097 vk_free2(&device->vk.alloc, pAllocator, pipeline);
4098 return result;
4099 }
4100
4101 /* Compute the size of the scratch BO (for register spilling) by taking the
4102 * max of all the shaders in the pipeline. Also add the shaders to the list
4103 * of executables.
4104 */
4105 uint32_t stack_max[MESA_VULKAN_SHADER_STAGES] = {};
4106 for (uint32_t s = 0; s < pCreateInfo->stageCount; s++) {
4107 util_dynarray_append(&pipeline->shaders,
4108 struct anv_shader_bin *,
4109 stages[s].bin);
4110
4111 uint32_t stack_size =
4112 brw_bs_prog_data_const(stages[s].bin->prog_data)->max_stack_size;
4113 stack_max[stages[s].stage] = MAX2(stack_max[stages[s].stage], stack_size);
4114
4115 anv_pipeline_account_shader(&pipeline->base, stages[s].bin);
4116 }
4117
4118 anv_pipeline_compute_ray_tracing_stacks(pipeline, pCreateInfo, stack_max);
4119
4120 if (pCreateInfo->pLibraryInfo) {
4121 uint32_t g = pCreateInfo->groupCount;
4122 for (uint32_t l = 0; l < pCreateInfo->pLibraryInfo->libraryCount; l++) {
4123 ANV_FROM_HANDLE(anv_pipeline, library,
4124 pCreateInfo->pLibraryInfo->pLibraries[l]);
4125 struct anv_ray_tracing_pipeline *rt_library =
4126 anv_pipeline_to_ray_tracing(library);
4127 for (uint32_t lg = 0; lg < rt_library->group_count; lg++) {
4128 pipeline->groups[g] = rt_library->groups[lg];
4129 pipeline->groups[g].imported = true;
4130 g++;
4131 }
4132
4133 /* Account for shaders in the library. */
4134 util_dynarray_foreach(&rt_library->shaders,
4135 struct anv_shader_bin *, shader) {
4136 util_dynarray_append(&pipeline->shaders,
4137 struct anv_shader_bin *,
4138 anv_shader_bin_ref(*shader));
4139 anv_pipeline_account_shader(&pipeline->base, *shader);
4140 }
4141
4142 /* Add the library shaders to this pipeline's executables. */
4143 util_dynarray_foreach(&rt_library->base.executables,
4144 struct anv_pipeline_executable, exe) {
4145 util_dynarray_append(&pipeline->base.executables,
4146 struct anv_pipeline_executable, *exe);
4147 }
4148
4149 pipeline->base.active_stages |= rt_library->base.active_stages;
4150 }
4151 }
4152
4153 anv_genX(device->info, ray_tracing_pipeline_emit)(pipeline);
4154
4155 ralloc_free(tmp_ctx);
4156
4157 ANV_RMV(rt_pipeline_create, device, pipeline, false);
4158
4159 *pPipeline = anv_pipeline_to_handle(&pipeline->base);
4160
4161 return pipeline->base.batch.status;
4162 }
4163
4164 VkResult
anv_CreateRayTracingPipelinesKHR(VkDevice _device,VkDeferredOperationKHR deferredOperation,VkPipelineCache pipelineCache,uint32_t createInfoCount,const VkRayTracingPipelineCreateInfoKHR * pCreateInfos,const VkAllocationCallbacks * pAllocator,VkPipeline * pPipelines)4165 anv_CreateRayTracingPipelinesKHR(
4166 VkDevice _device,
4167 VkDeferredOperationKHR deferredOperation,
4168 VkPipelineCache pipelineCache,
4169 uint32_t createInfoCount,
4170 const VkRayTracingPipelineCreateInfoKHR* pCreateInfos,
4171 const VkAllocationCallbacks* pAllocator,
4172 VkPipeline* pPipelines)
4173 {
4174 ANV_FROM_HANDLE(vk_pipeline_cache, pipeline_cache, pipelineCache);
4175
4176 VkResult result = VK_SUCCESS;
4177
4178 unsigned i;
4179 for (i = 0; i < createInfoCount; i++) {
4180 const VkPipelineCreateFlags2KHR flags =
4181 vk_rt_pipeline_create_flags(&pCreateInfos[i]);
4182 VkResult res = anv_ray_tracing_pipeline_create(_device, pipeline_cache,
4183 &pCreateInfos[i],
4184 pAllocator, &pPipelines[i]);
4185
4186 if (res == VK_SUCCESS)
4187 continue;
4188
4189 /* Bail out on the first error as it is not obvious what error should be
4190 * report upon 2 different failures. */
4191 result = res;
4192 if (result != VK_PIPELINE_COMPILE_REQUIRED)
4193 break;
4194
4195 pPipelines[i] = VK_NULL_HANDLE;
4196
4197 if (flags & VK_PIPELINE_CREATE_2_EARLY_RETURN_ON_FAILURE_BIT_KHR)
4198 break;
4199 }
4200
4201 for (; i < createInfoCount; i++)
4202 pPipelines[i] = VK_NULL_HANDLE;
4203
4204 return result;
4205 }
4206
4207 #define WRITE_STR(field, ...) ({ \
4208 memset(field, 0, sizeof(field)); \
4209 UNUSED int i = snprintf(field, sizeof(field), __VA_ARGS__); \
4210 assert(i > 0 && i < sizeof(field)); \
4211 })
4212
anv_GetPipelineExecutablePropertiesKHR(VkDevice device,const VkPipelineInfoKHR * pPipelineInfo,uint32_t * pExecutableCount,VkPipelineExecutablePropertiesKHR * pProperties)4213 VkResult anv_GetPipelineExecutablePropertiesKHR(
4214 VkDevice device,
4215 const VkPipelineInfoKHR* pPipelineInfo,
4216 uint32_t* pExecutableCount,
4217 VkPipelineExecutablePropertiesKHR* pProperties)
4218 {
4219 ANV_FROM_HANDLE(anv_pipeline, pipeline, pPipelineInfo->pipeline);
4220 VK_OUTARRAY_MAKE_TYPED(VkPipelineExecutablePropertiesKHR, out,
4221 pProperties, pExecutableCount);
4222
4223 util_dynarray_foreach (&pipeline->executables, struct anv_pipeline_executable, exe) {
4224 vk_outarray_append_typed(VkPipelineExecutablePropertiesKHR, &out, props) {
4225 gl_shader_stage stage = exe->stage;
4226 props->stages = mesa_to_vk_shader_stage(stage);
4227
4228 unsigned simd_width = exe->stats.dispatch_width;
4229 if (stage == MESA_SHADER_FRAGMENT) {
4230 if (exe->stats.max_polygons > 1)
4231 WRITE_STR(props->name, "SIMD%dx%d %s",
4232 exe->stats.max_polygons,
4233 simd_width / exe->stats.max_polygons,
4234 _mesa_shader_stage_to_string(stage));
4235 else
4236 WRITE_STR(props->name, "%s%d %s",
4237 simd_width ? "SIMD" : "vec",
4238 simd_width ? simd_width : 4,
4239 _mesa_shader_stage_to_string(stage));
4240 } else {
4241 WRITE_STR(props->name, "%s", _mesa_shader_stage_to_string(stage));
4242 }
4243 WRITE_STR(props->description, "%s%d %s shader",
4244 simd_width ? "SIMD" : "vec",
4245 simd_width ? simd_width : 4,
4246 _mesa_shader_stage_to_string(stage));
4247
4248 /* The compiler gives us a dispatch width of 0 for vec4 but Vulkan
4249 * wants a subgroup size of 1.
4250 */
4251 props->subgroupSize = MAX2(simd_width, 1);
4252 }
4253 }
4254
4255 return vk_outarray_status(&out);
4256 }
4257
4258 static const struct anv_pipeline_executable *
anv_pipeline_get_executable(struct anv_pipeline * pipeline,uint32_t index)4259 anv_pipeline_get_executable(struct anv_pipeline *pipeline, uint32_t index)
4260 {
4261 assert(index < util_dynarray_num_elements(&pipeline->executables,
4262 struct anv_pipeline_executable));
4263 return util_dynarray_element(
4264 &pipeline->executables, struct anv_pipeline_executable, index);
4265 }
4266
anv_GetPipelineExecutableStatisticsKHR(VkDevice device,const VkPipelineExecutableInfoKHR * pExecutableInfo,uint32_t * pStatisticCount,VkPipelineExecutableStatisticKHR * pStatistics)4267 VkResult anv_GetPipelineExecutableStatisticsKHR(
4268 VkDevice device,
4269 const VkPipelineExecutableInfoKHR* pExecutableInfo,
4270 uint32_t* pStatisticCount,
4271 VkPipelineExecutableStatisticKHR* pStatistics)
4272 {
4273 ANV_FROM_HANDLE(anv_pipeline, pipeline, pExecutableInfo->pipeline);
4274 VK_OUTARRAY_MAKE_TYPED(VkPipelineExecutableStatisticKHR, out,
4275 pStatistics, pStatisticCount);
4276
4277 const struct anv_pipeline_executable *exe =
4278 anv_pipeline_get_executable(pipeline, pExecutableInfo->executableIndex);
4279
4280 const struct brw_stage_prog_data *prog_data;
4281 switch (pipeline->type) {
4282 case ANV_PIPELINE_GRAPHICS:
4283 case ANV_PIPELINE_GRAPHICS_LIB: {
4284 prog_data = anv_pipeline_to_graphics(pipeline)->base.shaders[exe->stage]->prog_data;
4285 break;
4286 }
4287 case ANV_PIPELINE_COMPUTE: {
4288 prog_data = anv_pipeline_to_compute(pipeline)->cs->prog_data;
4289 break;
4290 }
4291 case ANV_PIPELINE_RAY_TRACING: {
4292 struct anv_shader_bin **shader =
4293 util_dynarray_element(&anv_pipeline_to_ray_tracing(pipeline)->shaders,
4294 struct anv_shader_bin *,
4295 pExecutableInfo->executableIndex);
4296 prog_data = (*shader)->prog_data;
4297 break;
4298 }
4299 default:
4300 unreachable("invalid pipeline type");
4301 }
4302
4303 vk_outarray_append_typed(VkPipelineExecutableStatisticKHR, &out, stat) {
4304 WRITE_STR(stat->name, "Instruction Count");
4305 WRITE_STR(stat->description,
4306 "Number of GEN instructions in the final generated "
4307 "shader executable.");
4308 stat->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;
4309 stat->value.u64 = exe->stats.instructions;
4310 }
4311
4312 vk_outarray_append_typed(VkPipelineExecutableStatisticKHR, &out, stat) {
4313 WRITE_STR(stat->name, "SEND Count");
4314 WRITE_STR(stat->description,
4315 "Number of instructions in the final generated shader "
4316 "executable which access external units such as the "
4317 "constant cache or the sampler.");
4318 stat->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;
4319 stat->value.u64 = exe->stats.sends;
4320 }
4321
4322 vk_outarray_append_typed(VkPipelineExecutableStatisticKHR, &out, stat) {
4323 WRITE_STR(stat->name, "Loop Count");
4324 WRITE_STR(stat->description,
4325 "Number of loops (not unrolled) in the final generated "
4326 "shader executable.");
4327 stat->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;
4328 stat->value.u64 = exe->stats.loops;
4329 }
4330
4331 vk_outarray_append_typed(VkPipelineExecutableStatisticKHR, &out, stat) {
4332 WRITE_STR(stat->name, "Cycle Count");
4333 WRITE_STR(stat->description,
4334 "Estimate of the number of EU cycles required to execute "
4335 "the final generated executable. This is an estimate only "
4336 "and may vary greatly from actual run-time performance.");
4337 stat->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;
4338 stat->value.u64 = exe->stats.cycles;
4339 }
4340
4341 vk_outarray_append_typed(VkPipelineExecutableStatisticKHR, &out, stat) {
4342 WRITE_STR(stat->name, "Spill Count");
4343 WRITE_STR(stat->description,
4344 "Number of scratch spill operations. This gives a rough "
4345 "estimate of the cost incurred due to spilling temporary "
4346 "values to memory. If this is non-zero, you may want to "
4347 "adjust your shader to reduce register pressure.");
4348 stat->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;
4349 stat->value.u64 = exe->stats.spills;
4350 }
4351
4352 vk_outarray_append_typed(VkPipelineExecutableStatisticKHR, &out, stat) {
4353 WRITE_STR(stat->name, "Fill Count");
4354 WRITE_STR(stat->description,
4355 "Number of scratch fill operations. This gives a rough "
4356 "estimate of the cost incurred due to spilling temporary "
4357 "values to memory. If this is non-zero, you may want to "
4358 "adjust your shader to reduce register pressure.");
4359 stat->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;
4360 stat->value.u64 = exe->stats.fills;
4361 }
4362
4363 vk_outarray_append_typed(VkPipelineExecutableStatisticKHR, &out, stat) {
4364 WRITE_STR(stat->name, "Scratch Memory Size");
4365 WRITE_STR(stat->description,
4366 "Number of bytes of scratch memory required by the "
4367 "generated shader executable. If this is non-zero, you "
4368 "may want to adjust your shader to reduce register "
4369 "pressure.");
4370 stat->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;
4371 stat->value.u64 = prog_data->total_scratch;
4372 }
4373
4374 vk_outarray_append_typed(VkPipelineExecutableStatisticKHR, &out, stat) {
4375 WRITE_STR(stat->name, "Max dispatch width");
4376 WRITE_STR(stat->description,
4377 "Largest SIMD dispatch width.");
4378 stat->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;
4379 /* Report the max dispatch width only on the smallest SIMD variant */
4380 if (exe->stage != MESA_SHADER_FRAGMENT || exe->stats.dispatch_width == 8)
4381 stat->value.u64 = exe->stats.max_dispatch_width;
4382 else
4383 stat->value.u64 = 0;
4384 }
4385
4386 vk_outarray_append_typed(VkPipelineExecutableStatisticKHR, &out, stat) {
4387 WRITE_STR(stat->name, "Max live registers");
4388 WRITE_STR(stat->description,
4389 "Maximum number of registers used across the entire shader.");
4390 stat->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;
4391 stat->value.u64 = exe->stats.max_live_registers;
4392 }
4393
4394 vk_outarray_append_typed(VkPipelineExecutableStatisticKHR, &out, stat) {
4395 WRITE_STR(stat->name, "Workgroup Memory Size");
4396 WRITE_STR(stat->description,
4397 "Number of bytes of workgroup shared memory used by this "
4398 "shader including any padding.");
4399 stat->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;
4400 if (gl_shader_stage_uses_workgroup(exe->stage))
4401 stat->value.u64 = prog_data->total_shared;
4402 else
4403 stat->value.u64 = 0;
4404 }
4405
4406 vk_outarray_append_typed(VkPipelineExecutableStatisticKHR, &out, stat) {
4407 uint32_t hash = pipeline->type == ANV_PIPELINE_COMPUTE ?
4408 anv_pipeline_to_compute(pipeline)->source_hash :
4409 (pipeline->type == ANV_PIPELINE_GRAPHICS_LIB ||
4410 pipeline->type == ANV_PIPELINE_GRAPHICS) ?
4411 anv_pipeline_to_graphics_base(pipeline)->source_hashes[exe->stage] :
4412 0 /* No source hash for ray tracing */;
4413 WRITE_STR(stat->name, "Source hash");
4414 WRITE_STR(stat->description,
4415 "hash = 0x%08x. Hash generated from shader source.", hash);
4416 stat->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;
4417 stat->value.u64 = hash;
4418 }
4419
4420 return vk_outarray_status(&out);
4421 }
4422
4423 static bool
write_ir_text(VkPipelineExecutableInternalRepresentationKHR * ir,const char * data)4424 write_ir_text(VkPipelineExecutableInternalRepresentationKHR* ir,
4425 const char *data)
4426 {
4427 ir->isText = VK_TRUE;
4428
4429 size_t data_len = strlen(data) + 1;
4430
4431 if (ir->pData == NULL) {
4432 ir->dataSize = data_len;
4433 return true;
4434 }
4435
4436 strncpy(ir->pData, data, ir->dataSize);
4437 if (ir->dataSize < data_len)
4438 return false;
4439
4440 ir->dataSize = data_len;
4441 return true;
4442 }
4443
anv_GetPipelineExecutableInternalRepresentationsKHR(VkDevice device,const VkPipelineExecutableInfoKHR * pExecutableInfo,uint32_t * pInternalRepresentationCount,VkPipelineExecutableInternalRepresentationKHR * pInternalRepresentations)4444 VkResult anv_GetPipelineExecutableInternalRepresentationsKHR(
4445 VkDevice device,
4446 const VkPipelineExecutableInfoKHR* pExecutableInfo,
4447 uint32_t* pInternalRepresentationCount,
4448 VkPipelineExecutableInternalRepresentationKHR* pInternalRepresentations)
4449 {
4450 ANV_FROM_HANDLE(anv_pipeline, pipeline, pExecutableInfo->pipeline);
4451 VK_OUTARRAY_MAKE_TYPED(VkPipelineExecutableInternalRepresentationKHR, out,
4452 pInternalRepresentations, pInternalRepresentationCount);
4453 bool incomplete_text = false;
4454
4455 const struct anv_pipeline_executable *exe =
4456 anv_pipeline_get_executable(pipeline, pExecutableInfo->executableIndex);
4457
4458 if (exe->nir) {
4459 vk_outarray_append_typed(VkPipelineExecutableInternalRepresentationKHR, &out, ir) {
4460 WRITE_STR(ir->name, "Final NIR");
4461 WRITE_STR(ir->description,
4462 "Final NIR before going into the back-end compiler");
4463
4464 if (!write_ir_text(ir, exe->nir))
4465 incomplete_text = true;
4466 }
4467 }
4468
4469 if (exe->disasm) {
4470 vk_outarray_append_typed(VkPipelineExecutableInternalRepresentationKHR, &out, ir) {
4471 WRITE_STR(ir->name, "GEN Assembly");
4472 WRITE_STR(ir->description,
4473 "Final GEN assembly for the generated shader binary");
4474
4475 if (!write_ir_text(ir, exe->disasm))
4476 incomplete_text = true;
4477 }
4478 }
4479
4480 return incomplete_text ? VK_INCOMPLETE : vk_outarray_status(&out);
4481 }
4482
4483 VkResult
anv_GetRayTracingShaderGroupHandlesKHR(VkDevice _device,VkPipeline _pipeline,uint32_t firstGroup,uint32_t groupCount,size_t dataSize,void * pData)4484 anv_GetRayTracingShaderGroupHandlesKHR(
4485 VkDevice _device,
4486 VkPipeline _pipeline,
4487 uint32_t firstGroup,
4488 uint32_t groupCount,
4489 size_t dataSize,
4490 void* pData)
4491 {
4492 ANV_FROM_HANDLE(anv_device, device, _device);
4493 ANV_FROM_HANDLE(anv_pipeline, pipeline, _pipeline);
4494
4495 if (pipeline->type != ANV_PIPELINE_RAY_TRACING)
4496 return vk_error(device, VK_ERROR_FEATURE_NOT_PRESENT);
4497
4498 struct anv_ray_tracing_pipeline *rt_pipeline =
4499 anv_pipeline_to_ray_tracing(pipeline);
4500
4501 assert(firstGroup + groupCount <= rt_pipeline->group_count);
4502 for (uint32_t i = 0; i < groupCount; i++) {
4503 struct anv_rt_shader_group *group = &rt_pipeline->groups[firstGroup + i];
4504 memcpy(pData, group->handle, sizeof(group->handle));
4505 pData += sizeof(group->handle);
4506 }
4507
4508 return VK_SUCCESS;
4509 }
4510
4511 VkResult
anv_GetRayTracingCaptureReplayShaderGroupHandlesKHR(VkDevice _device,VkPipeline pipeline,uint32_t firstGroup,uint32_t groupCount,size_t dataSize,void * pData)4512 anv_GetRayTracingCaptureReplayShaderGroupHandlesKHR(
4513 VkDevice _device,
4514 VkPipeline pipeline,
4515 uint32_t firstGroup,
4516 uint32_t groupCount,
4517 size_t dataSize,
4518 void* pData)
4519 {
4520 ANV_FROM_HANDLE(anv_device, device, _device);
4521 unreachable("Unimplemented");
4522 return vk_error(device, VK_ERROR_FEATURE_NOT_PRESENT);
4523 }
4524
4525 VkDeviceSize
anv_GetRayTracingShaderGroupStackSizeKHR(VkDevice device,VkPipeline _pipeline,uint32_t group,VkShaderGroupShaderKHR groupShader)4526 anv_GetRayTracingShaderGroupStackSizeKHR(
4527 VkDevice device,
4528 VkPipeline _pipeline,
4529 uint32_t group,
4530 VkShaderGroupShaderKHR groupShader)
4531 {
4532 ANV_FROM_HANDLE(anv_pipeline, pipeline, _pipeline);
4533 assert(pipeline->type == ANV_PIPELINE_RAY_TRACING);
4534
4535 struct anv_ray_tracing_pipeline *rt_pipeline =
4536 anv_pipeline_to_ray_tracing(pipeline);
4537
4538 assert(group < rt_pipeline->group_count);
4539
4540 struct anv_shader_bin *bin;
4541 switch (groupShader) {
4542 case VK_SHADER_GROUP_SHADER_GENERAL_KHR:
4543 bin = rt_pipeline->groups[group].general;
4544 break;
4545
4546 case VK_SHADER_GROUP_SHADER_CLOSEST_HIT_KHR:
4547 bin = rt_pipeline->groups[group].closest_hit;
4548 break;
4549
4550 case VK_SHADER_GROUP_SHADER_ANY_HIT_KHR:
4551 bin = rt_pipeline->groups[group].any_hit;
4552 break;
4553
4554 case VK_SHADER_GROUP_SHADER_INTERSECTION_KHR:
4555 bin = rt_pipeline->groups[group].intersection;
4556 break;
4557
4558 default:
4559 unreachable("Invalid VkShaderGroupShader enum");
4560 }
4561
4562 if (bin == NULL)
4563 return 0;
4564
4565 return brw_bs_prog_data_const(bin->prog_data)->max_stack_size;
4566 }
4567