anv,turnip,radv,clover,glspirv: Run nir_copy_prop before nir_opt_deref
[mesa.git] / src / intel / vulkan / anv_pipeline.c
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/gen_l3_config.h"
33 #include "common/gen_disasm.h"
34 #include "anv_private.h"
35 #include "compiler/brw_nir.h"
36 #include "anv_nir.h"
37 #include "nir/nir_xfb_info.h"
38 #include "spirv/nir_spirv.h"
39 #include "vk_util.h"
40
41 /* Needed for SWIZZLE macros */
42 #include "program/prog_instruction.h"
43
44 // Shader functions
45
46 VkResult anv_CreateShaderModule(
47 VkDevice _device,
48 const VkShaderModuleCreateInfo* pCreateInfo,
49 const VkAllocationCallbacks* pAllocator,
50 VkShaderModule* pShaderModule)
51 {
52 ANV_FROM_HANDLE(anv_device, device, _device);
53 struct anv_shader_module *module;
54
55 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO);
56 assert(pCreateInfo->flags == 0);
57
58 module = vk_alloc2(&device->vk.alloc, pAllocator,
59 sizeof(*module) + pCreateInfo->codeSize, 8,
60 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
61 if (module == NULL)
62 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
63
64 vk_object_base_init(&device->vk, &module->base,
65 VK_OBJECT_TYPE_SHADER_MODULE);
66 module->size = pCreateInfo->codeSize;
67 memcpy(module->data, pCreateInfo->pCode, module->size);
68
69 _mesa_sha1_compute(module->data, module->size, module->sha1);
70
71 *pShaderModule = anv_shader_module_to_handle(module);
72
73 return VK_SUCCESS;
74 }
75
76 void anv_DestroyShaderModule(
77 VkDevice _device,
78 VkShaderModule _module,
79 const VkAllocationCallbacks* pAllocator)
80 {
81 ANV_FROM_HANDLE(anv_device, device, _device);
82 ANV_FROM_HANDLE(anv_shader_module, module, _module);
83
84 if (!module)
85 return;
86
87 vk_object_base_finish(&module->base);
88 vk_free2(&device->vk.alloc, pAllocator, module);
89 }
90
91 #define SPIR_V_MAGIC_NUMBER 0x07230203
92
93 struct anv_spirv_debug_data {
94 struct anv_device *device;
95 const struct anv_shader_module *module;
96 };
97
98 static void anv_spirv_nir_debug(void *private_data,
99 enum nir_spirv_debug_level level,
100 size_t spirv_offset,
101 const char *message)
102 {
103 struct anv_spirv_debug_data *debug_data = private_data;
104 struct anv_instance *instance = debug_data->device->physical->instance;
105
106 static const VkDebugReportFlagsEXT vk_flags[] = {
107 [NIR_SPIRV_DEBUG_LEVEL_INFO] = VK_DEBUG_REPORT_INFORMATION_BIT_EXT,
108 [NIR_SPIRV_DEBUG_LEVEL_WARNING] = VK_DEBUG_REPORT_WARNING_BIT_EXT,
109 [NIR_SPIRV_DEBUG_LEVEL_ERROR] = VK_DEBUG_REPORT_ERROR_BIT_EXT,
110 };
111 char buffer[256];
112
113 snprintf(buffer, sizeof(buffer), "SPIR-V offset %lu: %s", (unsigned long) spirv_offset, message);
114
115 vk_debug_report(&instance->debug_report_callbacks,
116 vk_flags[level],
117 VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT,
118 (uint64_t) (uintptr_t) debug_data->module,
119 0, 0, "anv", buffer);
120 }
121
122 /* Eventually, this will become part of anv_CreateShader. Unfortunately,
123 * we can't do that yet because we don't have the ability to copy nir.
124 */
125 static nir_shader *
126 anv_shader_compile_to_nir(struct anv_device *device,
127 void *mem_ctx,
128 const struct anv_shader_module *module,
129 const char *entrypoint_name,
130 gl_shader_stage stage,
131 const VkSpecializationInfo *spec_info)
132 {
133 const struct anv_physical_device *pdevice = device->physical;
134 const struct brw_compiler *compiler = pdevice->compiler;
135 const nir_shader_compiler_options *nir_options =
136 compiler->glsl_compiler_options[stage].NirOptions;
137
138 uint32_t *spirv = (uint32_t *) module->data;
139 assert(spirv[0] == SPIR_V_MAGIC_NUMBER);
140 assert(module->size % 4 == 0);
141
142 uint32_t num_spec_entries = 0;
143 struct nir_spirv_specialization *spec_entries = NULL;
144 if (spec_info && spec_info->mapEntryCount > 0) {
145 num_spec_entries = spec_info->mapEntryCount;
146 spec_entries = calloc(num_spec_entries, sizeof(*spec_entries));
147 for (uint32_t i = 0; i < num_spec_entries; i++) {
148 VkSpecializationMapEntry entry = spec_info->pMapEntries[i];
149 const void *data = spec_info->pData + entry.offset;
150 assert(data + entry.size <= spec_info->pData + spec_info->dataSize);
151
152 spec_entries[i].id = spec_info->pMapEntries[i].constantID;
153 switch (entry.size) {
154 case 8:
155 spec_entries[i].value.u64 = *(const uint64_t *)data;
156 break;
157 case 4:
158 spec_entries[i].value.u32 = *(const uint32_t *)data;
159 break;
160 case 2:
161 spec_entries[i].value.u16 = *(const uint16_t *)data;
162 break;
163 case 1:
164 spec_entries[i].value.u8 = *(const uint8_t *)data;
165 break;
166 default:
167 assert(!"Invalid spec constant size");
168 break;
169 }
170 }
171 }
172
173 struct anv_spirv_debug_data spirv_debug_data = {
174 .device = device,
175 .module = module,
176 };
177 struct spirv_to_nir_options spirv_options = {
178 .frag_coord_is_sysval = true,
179 .caps = {
180 .demote_to_helper_invocation = true,
181 .derivative_group = true,
182 .descriptor_array_dynamic_indexing = true,
183 .descriptor_array_non_uniform_indexing = true,
184 .descriptor_indexing = true,
185 .device_group = true,
186 .draw_parameters = true,
187 .float16 = pdevice->info.gen >= 8,
188 .float64 = pdevice->info.gen >= 8,
189 .fragment_shader_sample_interlock = pdevice->info.gen >= 9,
190 .fragment_shader_pixel_interlock = pdevice->info.gen >= 9,
191 .geometry_streams = true,
192 .image_write_without_format = true,
193 .int8 = pdevice->info.gen >= 8,
194 .int16 = pdevice->info.gen >= 8,
195 .int64 = pdevice->info.gen >= 8,
196 .int64_atomics = pdevice->info.gen >= 9 && pdevice->use_softpin,
197 .integer_functions2 = pdevice->info.gen >= 8,
198 .min_lod = true,
199 .multiview = true,
200 .physical_storage_buffer_address = pdevice->has_a64_buffer_access,
201 .post_depth_coverage = pdevice->info.gen >= 9,
202 .runtime_descriptor_array = true,
203 .float_controls = pdevice->info.gen >= 8,
204 .shader_clock = true,
205 .shader_viewport_index_layer = true,
206 .stencil_export = pdevice->info.gen >= 9,
207 .storage_8bit = pdevice->info.gen >= 8,
208 .storage_16bit = pdevice->info.gen >= 8,
209 .subgroup_arithmetic = true,
210 .subgroup_basic = true,
211 .subgroup_ballot = true,
212 .subgroup_quad = true,
213 .subgroup_shuffle = true,
214 .subgroup_vote = true,
215 .tessellation = true,
216 .transform_feedback = pdevice->info.gen >= 8,
217 .variable_pointers = true,
218 .vk_memory_model = true,
219 .vk_memory_model_device_scope = true,
220 },
221 .ubo_addr_format = nir_address_format_32bit_index_offset,
222 .ssbo_addr_format =
223 anv_nir_ssbo_addr_format(pdevice, device->robust_buffer_access),
224 .phys_ssbo_addr_format = nir_address_format_64bit_global,
225 .push_const_addr_format = nir_address_format_logical,
226
227 /* TODO: Consider changing this to an address format that has the NULL
228 * pointer equals to 0. That might be a better format to play nice
229 * with certain code / code generators.
230 */
231 .shared_addr_format = nir_address_format_32bit_offset,
232 .debug = {
233 .func = anv_spirv_nir_debug,
234 .private_data = &spirv_debug_data,
235 },
236 };
237
238
239 nir_shader *nir =
240 spirv_to_nir(spirv, module->size / 4,
241 spec_entries, num_spec_entries,
242 stage, entrypoint_name, &spirv_options, nir_options);
243 assert(nir->info.stage == stage);
244 nir_validate_shader(nir, "after spirv_to_nir");
245 ralloc_steal(mem_ctx, nir);
246
247 free(spec_entries);
248
249 if (unlikely(INTEL_DEBUG & intel_debug_flag_for_shader_stage(stage))) {
250 fprintf(stderr, "NIR (from SPIR-V) for %s shader:\n",
251 gl_shader_stage_name(stage));
252 nir_print_shader(nir, stderr);
253 }
254
255 /* We have to lower away local constant initializers right before we
256 * inline functions. That way they get properly initialized at the top
257 * of the function and not at the top of its caller.
258 */
259 NIR_PASS_V(nir, nir_lower_variable_initializers, nir_var_function_temp);
260 NIR_PASS_V(nir, nir_lower_returns);
261 NIR_PASS_V(nir, nir_inline_functions);
262 NIR_PASS_V(nir, nir_copy_prop);
263 NIR_PASS_V(nir, nir_opt_deref);
264
265 /* Pick off the single entrypoint that we want */
266 foreach_list_typed_safe(nir_function, func, node, &nir->functions) {
267 if (!func->is_entrypoint)
268 exec_node_remove(&func->node);
269 }
270 assert(exec_list_length(&nir->functions) == 1);
271
272 /* Now that we've deleted all but the main function, we can go ahead and
273 * lower the rest of the constant initializers. We do this here so that
274 * nir_remove_dead_variables and split_per_member_structs below see the
275 * corresponding stores.
276 */
277 NIR_PASS_V(nir, nir_lower_variable_initializers, ~0);
278
279 /* Split member structs. We do this before lower_io_to_temporaries so that
280 * it doesn't lower system values to temporaries by accident.
281 */
282 NIR_PASS_V(nir, nir_split_var_copies);
283 NIR_PASS_V(nir, nir_split_per_member_structs);
284
285 NIR_PASS_V(nir, nir_remove_dead_variables,
286 nir_var_shader_in | nir_var_shader_out | nir_var_system_value,
287 NULL);
288
289 NIR_PASS_V(nir, nir_propagate_invariant);
290 NIR_PASS_V(nir, nir_lower_io_to_temporaries,
291 nir_shader_get_entrypoint(nir), true, false);
292
293 NIR_PASS_V(nir, nir_lower_frexp);
294
295 /* Vulkan uses the separate-shader linking model */
296 nir->info.separate_shader = true;
297
298 brw_preprocess_nir(compiler, nir, NULL);
299
300 return nir;
301 }
302
303 VkResult
304 anv_pipeline_init(struct anv_pipeline *pipeline,
305 struct anv_device *device,
306 enum anv_pipeline_type type,
307 VkPipelineCreateFlags flags,
308 const VkAllocationCallbacks *pAllocator)
309 {
310 VkResult result;
311
312 memset(pipeline, 0, sizeof(*pipeline));
313
314 vk_object_base_init(&device->vk, &pipeline->base,
315 VK_OBJECT_TYPE_PIPELINE);
316 pipeline->device = device;
317
318 /* It's the job of the child class to provide actual backing storage for
319 * the batch by setting batch.start, batch.next, and batch.end.
320 */
321 pipeline->batch.alloc = pAllocator ? pAllocator : &device->vk.alloc;
322 pipeline->batch.relocs = &pipeline->batch_relocs;
323 pipeline->batch.status = VK_SUCCESS;
324
325 result = anv_reloc_list_init(&pipeline->batch_relocs,
326 pipeline->batch.alloc);
327 if (result != VK_SUCCESS)
328 return result;
329
330 pipeline->mem_ctx = ralloc_context(NULL);
331
332 pipeline->type = type;
333 pipeline->flags = flags;
334
335 util_dynarray_init(&pipeline->executables, pipeline->mem_ctx);
336
337 return VK_SUCCESS;
338 }
339
340 void
341 anv_pipeline_finish(struct anv_pipeline *pipeline,
342 struct anv_device *device,
343 const VkAllocationCallbacks *pAllocator)
344 {
345 anv_reloc_list_finish(&pipeline->batch_relocs,
346 pAllocator ? pAllocator : &device->vk.alloc);
347 ralloc_free(pipeline->mem_ctx);
348 vk_object_base_finish(&pipeline->base);
349 }
350
351 void anv_DestroyPipeline(
352 VkDevice _device,
353 VkPipeline _pipeline,
354 const VkAllocationCallbacks* pAllocator)
355 {
356 ANV_FROM_HANDLE(anv_device, device, _device);
357 ANV_FROM_HANDLE(anv_pipeline, pipeline, _pipeline);
358
359 if (!pipeline)
360 return;
361
362 switch (pipeline->type) {
363 case ANV_PIPELINE_GRAPHICS: {
364 struct anv_graphics_pipeline *gfx_pipeline =
365 anv_pipeline_to_graphics(pipeline);
366
367 if (gfx_pipeline->blend_state.map)
368 anv_state_pool_free(&device->dynamic_state_pool, gfx_pipeline->blend_state);
369
370 for (unsigned s = 0; s < MESA_SHADER_STAGES; s++) {
371 if (gfx_pipeline->shaders[s])
372 anv_shader_bin_unref(device, gfx_pipeline->shaders[s]);
373 }
374 break;
375 }
376
377 case ANV_PIPELINE_COMPUTE: {
378 struct anv_compute_pipeline *compute_pipeline =
379 anv_pipeline_to_compute(pipeline);
380
381 if (compute_pipeline->cs)
382 anv_shader_bin_unref(device, compute_pipeline->cs);
383
384 break;
385 }
386
387 default:
388 unreachable("invalid pipeline type");
389 }
390
391 anv_pipeline_finish(pipeline, device, pAllocator);
392 vk_free2(&device->vk.alloc, pAllocator, pipeline);
393 }
394
395 static const uint32_t vk_to_gen_primitive_type[] = {
396 [VK_PRIMITIVE_TOPOLOGY_POINT_LIST] = _3DPRIM_POINTLIST,
397 [VK_PRIMITIVE_TOPOLOGY_LINE_LIST] = _3DPRIM_LINELIST,
398 [VK_PRIMITIVE_TOPOLOGY_LINE_STRIP] = _3DPRIM_LINESTRIP,
399 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST] = _3DPRIM_TRILIST,
400 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP] = _3DPRIM_TRISTRIP,
401 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN] = _3DPRIM_TRIFAN,
402 [VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY] = _3DPRIM_LINELIST_ADJ,
403 [VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY] = _3DPRIM_LINESTRIP_ADJ,
404 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY] = _3DPRIM_TRILIST_ADJ,
405 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY] = _3DPRIM_TRISTRIP_ADJ,
406 };
407
408 static void
409 populate_sampler_prog_key(const struct gen_device_info *devinfo,
410 struct brw_sampler_prog_key_data *key)
411 {
412 /* Almost all multisampled textures are compressed. The only time when we
413 * don't compress a multisampled texture is for 16x MSAA with a surface
414 * width greater than 8k which is a bit of an edge case. Since the sampler
415 * just ignores the MCS parameter to ld2ms when MCS is disabled, it's safe
416 * to tell the compiler to always assume compression.
417 */
418 key->compressed_multisample_layout_mask = ~0;
419
420 /* SkyLake added support for 16x MSAA. With this came a new message for
421 * reading from a 16x MSAA surface with compression. The new message was
422 * needed because now the MCS data is 64 bits instead of 32 or lower as is
423 * the case for 8x, 4x, and 2x. The key->msaa_16 bit-field controls which
424 * message we use. Fortunately, the 16x message works for 8x, 4x, and 2x
425 * so we can just use it unconditionally. This may not be quite as
426 * efficient but it saves us from recompiling.
427 */
428 if (devinfo->gen >= 9)
429 key->msaa_16 = ~0;
430
431 /* XXX: Handle texture swizzle on HSW- */
432 for (int i = 0; i < MAX_SAMPLERS; i++) {
433 /* Assume color sampler, no swizzling. (Works for BDW+) */
434 key->swizzles[i] = SWIZZLE_XYZW;
435 }
436 }
437
438 static void
439 populate_base_prog_key(const struct gen_device_info *devinfo,
440 VkPipelineShaderStageCreateFlags flags,
441 struct brw_base_prog_key *key)
442 {
443 if (flags & VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT)
444 key->subgroup_size_type = BRW_SUBGROUP_SIZE_VARYING;
445 else
446 key->subgroup_size_type = BRW_SUBGROUP_SIZE_API_CONSTANT;
447
448 populate_sampler_prog_key(devinfo, &key->tex);
449 }
450
451 static void
452 populate_vs_prog_key(const struct gen_device_info *devinfo,
453 VkPipelineShaderStageCreateFlags flags,
454 struct brw_vs_prog_key *key)
455 {
456 memset(key, 0, sizeof(*key));
457
458 populate_base_prog_key(devinfo, flags, &key->base);
459
460 /* XXX: Handle vertex input work-arounds */
461
462 /* XXX: Handle sampler_prog_key */
463 }
464
465 static void
466 populate_tcs_prog_key(const struct gen_device_info *devinfo,
467 VkPipelineShaderStageCreateFlags flags,
468 unsigned input_vertices,
469 struct brw_tcs_prog_key *key)
470 {
471 memset(key, 0, sizeof(*key));
472
473 populate_base_prog_key(devinfo, flags, &key->base);
474
475 key->input_vertices = input_vertices;
476 }
477
478 static void
479 populate_tes_prog_key(const struct gen_device_info *devinfo,
480 VkPipelineShaderStageCreateFlags flags,
481 struct brw_tes_prog_key *key)
482 {
483 memset(key, 0, sizeof(*key));
484
485 populate_base_prog_key(devinfo, flags, &key->base);
486 }
487
488 static void
489 populate_gs_prog_key(const struct gen_device_info *devinfo,
490 VkPipelineShaderStageCreateFlags flags,
491 struct brw_gs_prog_key *key)
492 {
493 memset(key, 0, sizeof(*key));
494
495 populate_base_prog_key(devinfo, flags, &key->base);
496 }
497
498 static void
499 populate_wm_prog_key(const struct gen_device_info *devinfo,
500 VkPipelineShaderStageCreateFlags flags,
501 const struct anv_subpass *subpass,
502 const VkPipelineMultisampleStateCreateInfo *ms_info,
503 struct brw_wm_prog_key *key)
504 {
505 memset(key, 0, sizeof(*key));
506
507 populate_base_prog_key(devinfo, flags, &key->base);
508
509 /* We set this to 0 here and set to the actual value before we call
510 * brw_compile_fs.
511 */
512 key->input_slots_valid = 0;
513
514 /* Vulkan doesn't specify a default */
515 key->high_quality_derivatives = false;
516
517 /* XXX Vulkan doesn't appear to specify */
518 key->clamp_fragment_color = false;
519
520 key->ignore_sample_mask_out = false;
521
522 assert(subpass->color_count <= MAX_RTS);
523 for (uint32_t i = 0; i < subpass->color_count; i++) {
524 if (subpass->color_attachments[i].attachment != VK_ATTACHMENT_UNUSED)
525 key->color_outputs_valid |= (1 << i);
526 }
527
528 key->nr_color_regions = subpass->color_count;
529
530 /* To reduce possible shader recompilations we would need to know if
531 * there is a SampleMask output variable to compute if we should emit
532 * code to workaround the issue that hardware disables alpha to coverage
533 * when there is SampleMask output.
534 */
535 key->alpha_to_coverage = ms_info && ms_info->alphaToCoverageEnable;
536
537 /* Vulkan doesn't support fixed-function alpha test */
538 key->alpha_test_replicate_alpha = false;
539
540 if (ms_info) {
541 /* We should probably pull this out of the shader, but it's fairly
542 * harmless to compute it and then let dead-code take care of it.
543 */
544 if (ms_info->rasterizationSamples > 1) {
545 key->persample_interp = ms_info->sampleShadingEnable &&
546 (ms_info->minSampleShading * ms_info->rasterizationSamples) > 1;
547 key->multisample_fbo = true;
548 }
549
550 key->frag_coord_adds_sample_pos = key->persample_interp;
551 }
552 }
553
554 static void
555 populate_cs_prog_key(const struct gen_device_info *devinfo,
556 VkPipelineShaderStageCreateFlags flags,
557 const VkPipelineShaderStageRequiredSubgroupSizeCreateInfoEXT *rss_info,
558 struct brw_cs_prog_key *key)
559 {
560 memset(key, 0, sizeof(*key));
561
562 populate_base_prog_key(devinfo, flags, &key->base);
563
564 if (rss_info) {
565 assert(key->base.subgroup_size_type != BRW_SUBGROUP_SIZE_VARYING);
566
567 /* These enum values are expressly chosen to be equal to the subgroup
568 * size that they require.
569 */
570 assert(rss_info->requiredSubgroupSize == 8 ||
571 rss_info->requiredSubgroupSize == 16 ||
572 rss_info->requiredSubgroupSize == 32);
573 key->base.subgroup_size_type = rss_info->requiredSubgroupSize;
574 } else if (flags & VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT) {
575 /* If the client expressly requests full subgroups and they don't
576 * specify a subgroup size, we need to pick one. If they're requested
577 * varying subgroup sizes, we set it to UNIFORM and let the back-end
578 * compiler pick. Otherwise, we specify the API value of 32.
579 * Performance will likely be terrible in this case but there's nothing
580 * we can do about that. The client should have chosen a size.
581 */
582 if (flags & VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT)
583 key->base.subgroup_size_type = BRW_SUBGROUP_SIZE_UNIFORM;
584 else
585 key->base.subgroup_size_type = BRW_SUBGROUP_SIZE_REQUIRE_32;
586 }
587 }
588
589 struct anv_pipeline_stage {
590 gl_shader_stage stage;
591
592 const struct anv_shader_module *module;
593 const char *entrypoint;
594 const VkSpecializationInfo *spec_info;
595
596 unsigned char shader_sha1[20];
597
598 union brw_any_prog_key key;
599
600 struct {
601 gl_shader_stage stage;
602 unsigned char sha1[20];
603 } cache_key;
604
605 nir_shader *nir;
606
607 struct anv_pipeline_binding surface_to_descriptor[256];
608 struct anv_pipeline_binding sampler_to_descriptor[256];
609 struct anv_pipeline_bind_map bind_map;
610
611 union brw_any_prog_data prog_data;
612
613 uint32_t num_stats;
614 struct brw_compile_stats stats[3];
615 char *disasm[3];
616
617 VkPipelineCreationFeedbackEXT feedback;
618
619 const unsigned *code;
620 };
621
622 static void
623 anv_pipeline_hash_shader(const struct anv_shader_module *module,
624 const char *entrypoint,
625 gl_shader_stage stage,
626 const VkSpecializationInfo *spec_info,
627 unsigned char *sha1_out)
628 {
629 struct mesa_sha1 ctx;
630 _mesa_sha1_init(&ctx);
631
632 _mesa_sha1_update(&ctx, module->sha1, sizeof(module->sha1));
633 _mesa_sha1_update(&ctx, entrypoint, strlen(entrypoint));
634 _mesa_sha1_update(&ctx, &stage, sizeof(stage));
635 if (spec_info) {
636 _mesa_sha1_update(&ctx, spec_info->pMapEntries,
637 spec_info->mapEntryCount *
638 sizeof(*spec_info->pMapEntries));
639 _mesa_sha1_update(&ctx, spec_info->pData,
640 spec_info->dataSize);
641 }
642
643 _mesa_sha1_final(&ctx, sha1_out);
644 }
645
646 static void
647 anv_pipeline_hash_graphics(struct anv_graphics_pipeline *pipeline,
648 struct anv_pipeline_layout *layout,
649 struct anv_pipeline_stage *stages,
650 unsigned char *sha1_out)
651 {
652 struct mesa_sha1 ctx;
653 _mesa_sha1_init(&ctx);
654
655 _mesa_sha1_update(&ctx, &pipeline->subpass->view_mask,
656 sizeof(pipeline->subpass->view_mask));
657
658 if (layout)
659 _mesa_sha1_update(&ctx, layout->sha1, sizeof(layout->sha1));
660
661 const bool rba = pipeline->base.device->robust_buffer_access;
662 _mesa_sha1_update(&ctx, &rba, sizeof(rba));
663
664 for (unsigned s = 0; s < MESA_SHADER_STAGES; s++) {
665 if (stages[s].entrypoint) {
666 _mesa_sha1_update(&ctx, stages[s].shader_sha1,
667 sizeof(stages[s].shader_sha1));
668 _mesa_sha1_update(&ctx, &stages[s].key, brw_prog_key_size(s));
669 }
670 }
671
672 _mesa_sha1_final(&ctx, sha1_out);
673 }
674
675 static void
676 anv_pipeline_hash_compute(struct anv_compute_pipeline *pipeline,
677 struct anv_pipeline_layout *layout,
678 struct anv_pipeline_stage *stage,
679 unsigned char *sha1_out)
680 {
681 struct mesa_sha1 ctx;
682 _mesa_sha1_init(&ctx);
683
684 if (layout)
685 _mesa_sha1_update(&ctx, layout->sha1, sizeof(layout->sha1));
686
687 const bool rba = pipeline->base.device->robust_buffer_access;
688 _mesa_sha1_update(&ctx, &rba, sizeof(rba));
689
690 _mesa_sha1_update(&ctx, stage->shader_sha1,
691 sizeof(stage->shader_sha1));
692 _mesa_sha1_update(&ctx, &stage->key.cs, sizeof(stage->key.cs));
693
694 _mesa_sha1_final(&ctx, sha1_out);
695 }
696
697 static nir_shader *
698 anv_pipeline_stage_get_nir(struct anv_pipeline *pipeline,
699 struct anv_pipeline_cache *cache,
700 void *mem_ctx,
701 struct anv_pipeline_stage *stage)
702 {
703 const struct brw_compiler *compiler =
704 pipeline->device->physical->compiler;
705 const nir_shader_compiler_options *nir_options =
706 compiler->glsl_compiler_options[stage->stage].NirOptions;
707 nir_shader *nir;
708
709 nir = anv_device_search_for_nir(pipeline->device, cache,
710 nir_options,
711 stage->shader_sha1,
712 mem_ctx);
713 if (nir) {
714 assert(nir->info.stage == stage->stage);
715 return nir;
716 }
717
718 nir = anv_shader_compile_to_nir(pipeline->device,
719 mem_ctx,
720 stage->module,
721 stage->entrypoint,
722 stage->stage,
723 stage->spec_info);
724 if (nir) {
725 anv_device_upload_nir(pipeline->device, cache, nir, stage->shader_sha1);
726 return nir;
727 }
728
729 return NULL;
730 }
731
732 static void
733 anv_pipeline_lower_nir(struct anv_pipeline *pipeline,
734 void *mem_ctx,
735 struct anv_pipeline_stage *stage,
736 struct anv_pipeline_layout *layout)
737 {
738 const struct anv_physical_device *pdevice = pipeline->device->physical;
739 const struct brw_compiler *compiler = pdevice->compiler;
740
741 struct brw_stage_prog_data *prog_data = &stage->prog_data.base;
742 nir_shader *nir = stage->nir;
743
744 if (nir->info.stage == MESA_SHADER_FRAGMENT) {
745 NIR_PASS_V(nir, nir_lower_wpos_center,
746 anv_pipeline_to_graphics(pipeline)->sample_shading_enable);
747 NIR_PASS_V(nir, nir_lower_input_attachments, true);
748 }
749
750 NIR_PASS_V(nir, anv_nir_lower_ycbcr_textures, layout);
751
752 if (pipeline->type == ANV_PIPELINE_GRAPHICS) {
753 NIR_PASS_V(nir, anv_nir_lower_multiview,
754 anv_pipeline_to_graphics(pipeline));
755 }
756
757 nir_shader_gather_info(nir, nir_shader_get_entrypoint(nir));
758
759 NIR_PASS_V(nir, brw_nir_lower_image_load_store, compiler->devinfo, NULL);
760
761 NIR_PASS_V(nir, nir_lower_explicit_io, nir_var_mem_global,
762 nir_address_format_64bit_global);
763
764 /* Apply the actual pipeline layout to UBOs, SSBOs, and textures */
765 anv_nir_apply_pipeline_layout(pdevice,
766 pipeline->device->robust_buffer_access,
767 layout, nir, &stage->bind_map);
768
769 NIR_PASS_V(nir, nir_lower_explicit_io, nir_var_mem_ubo,
770 nir_address_format_32bit_index_offset);
771 NIR_PASS_V(nir, nir_lower_explicit_io, nir_var_mem_ssbo,
772 anv_nir_ssbo_addr_format(pdevice,
773 pipeline->device->robust_buffer_access));
774
775 NIR_PASS_V(nir, nir_opt_constant_folding);
776
777 /* We don't support non-uniform UBOs and non-uniform SSBO access is
778 * handled naturally by falling back to A64 messages.
779 */
780 NIR_PASS_V(nir, nir_lower_non_uniform_access,
781 nir_lower_non_uniform_texture_access |
782 nir_lower_non_uniform_image_access);
783
784 anv_nir_compute_push_layout(pdevice, pipeline->device->robust_buffer_access,
785 nir, prog_data, &stage->bind_map, mem_ctx);
786
787 stage->nir = nir;
788 }
789
790 static void
791 anv_pipeline_link_vs(const struct brw_compiler *compiler,
792 struct anv_pipeline_stage *vs_stage,
793 struct anv_pipeline_stage *next_stage)
794 {
795 if (next_stage)
796 brw_nir_link_shaders(compiler, vs_stage->nir, next_stage->nir);
797 }
798
799 static void
800 anv_pipeline_compile_vs(const struct brw_compiler *compiler,
801 void *mem_ctx,
802 struct anv_graphics_pipeline *pipeline,
803 struct anv_pipeline_stage *vs_stage)
804 {
805 /* When using Primitive Replication for multiview, each view gets its own
806 * position slot.
807 */
808 uint32_t pos_slots = pipeline->use_primitive_replication ?
809 anv_subpass_view_count(pipeline->subpass) : 1;
810
811 brw_compute_vue_map(compiler->devinfo,
812 &vs_stage->prog_data.vs.base.vue_map,
813 vs_stage->nir->info.outputs_written,
814 vs_stage->nir->info.separate_shader,
815 pos_slots);
816
817 vs_stage->num_stats = 1;
818 vs_stage->code = brw_compile_vs(compiler, pipeline->base.device, mem_ctx,
819 &vs_stage->key.vs,
820 &vs_stage->prog_data.vs,
821 vs_stage->nir, -1,
822 vs_stage->stats, NULL);
823 }
824
825 static void
826 merge_tess_info(struct shader_info *tes_info,
827 const struct shader_info *tcs_info)
828 {
829 /* The Vulkan 1.0.38 spec, section 21.1 Tessellator says:
830 *
831 * "PointMode. Controls generation of points rather than triangles
832 * or lines. This functionality defaults to disabled, and is
833 * enabled if either shader stage includes the execution mode.
834 *
835 * and about Triangles, Quads, IsoLines, VertexOrderCw, VertexOrderCcw,
836 * PointMode, SpacingEqual, SpacingFractionalEven, SpacingFractionalOdd,
837 * and OutputVertices, it says:
838 *
839 * "One mode must be set in at least one of the tessellation
840 * shader stages."
841 *
842 * So, the fields can be set in either the TCS or TES, but they must
843 * agree if set in both. Our backend looks at TES, so bitwise-or in
844 * the values from the TCS.
845 */
846 assert(tcs_info->tess.tcs_vertices_out == 0 ||
847 tes_info->tess.tcs_vertices_out == 0 ||
848 tcs_info->tess.tcs_vertices_out == tes_info->tess.tcs_vertices_out);
849 tes_info->tess.tcs_vertices_out |= tcs_info->tess.tcs_vertices_out;
850
851 assert(tcs_info->tess.spacing == TESS_SPACING_UNSPECIFIED ||
852 tes_info->tess.spacing == TESS_SPACING_UNSPECIFIED ||
853 tcs_info->tess.spacing == tes_info->tess.spacing);
854 tes_info->tess.spacing |= tcs_info->tess.spacing;
855
856 assert(tcs_info->tess.primitive_mode == 0 ||
857 tes_info->tess.primitive_mode == 0 ||
858 tcs_info->tess.primitive_mode == tes_info->tess.primitive_mode);
859 tes_info->tess.primitive_mode |= tcs_info->tess.primitive_mode;
860 tes_info->tess.ccw |= tcs_info->tess.ccw;
861 tes_info->tess.point_mode |= tcs_info->tess.point_mode;
862 }
863
864 static void
865 anv_pipeline_link_tcs(const struct brw_compiler *compiler,
866 struct anv_pipeline_stage *tcs_stage,
867 struct anv_pipeline_stage *tes_stage)
868 {
869 assert(tes_stage && tes_stage->stage == MESA_SHADER_TESS_EVAL);
870
871 brw_nir_link_shaders(compiler, tcs_stage->nir, tes_stage->nir);
872
873 nir_lower_patch_vertices(tes_stage->nir,
874 tcs_stage->nir->info.tess.tcs_vertices_out,
875 NULL);
876
877 /* Copy TCS info into the TES info */
878 merge_tess_info(&tes_stage->nir->info, &tcs_stage->nir->info);
879
880 /* Whacking the key after cache lookup is a bit sketchy, but all of
881 * this comes from the SPIR-V, which is part of the hash used for the
882 * pipeline cache. So it should be safe.
883 */
884 tcs_stage->key.tcs.tes_primitive_mode =
885 tes_stage->nir->info.tess.primitive_mode;
886 tcs_stage->key.tcs.quads_workaround =
887 compiler->devinfo->gen < 9 &&
888 tes_stage->nir->info.tess.primitive_mode == 7 /* GL_QUADS */ &&
889 tes_stage->nir->info.tess.spacing == TESS_SPACING_EQUAL;
890 }
891
892 static void
893 anv_pipeline_compile_tcs(const struct brw_compiler *compiler,
894 void *mem_ctx,
895 struct anv_device *device,
896 struct anv_pipeline_stage *tcs_stage,
897 struct anv_pipeline_stage *prev_stage)
898 {
899 tcs_stage->key.tcs.outputs_written =
900 tcs_stage->nir->info.outputs_written;
901 tcs_stage->key.tcs.patch_outputs_written =
902 tcs_stage->nir->info.patch_outputs_written;
903
904 tcs_stage->num_stats = 1;
905 tcs_stage->code = brw_compile_tcs(compiler, device, mem_ctx,
906 &tcs_stage->key.tcs,
907 &tcs_stage->prog_data.tcs,
908 tcs_stage->nir, -1,
909 tcs_stage->stats, NULL);
910 }
911
912 static void
913 anv_pipeline_link_tes(const struct brw_compiler *compiler,
914 struct anv_pipeline_stage *tes_stage,
915 struct anv_pipeline_stage *next_stage)
916 {
917 if (next_stage)
918 brw_nir_link_shaders(compiler, tes_stage->nir, next_stage->nir);
919 }
920
921 static void
922 anv_pipeline_compile_tes(const struct brw_compiler *compiler,
923 void *mem_ctx,
924 struct anv_device *device,
925 struct anv_pipeline_stage *tes_stage,
926 struct anv_pipeline_stage *tcs_stage)
927 {
928 tes_stage->key.tes.inputs_read =
929 tcs_stage->nir->info.outputs_written;
930 tes_stage->key.tes.patch_inputs_read =
931 tcs_stage->nir->info.patch_outputs_written;
932
933 tes_stage->num_stats = 1;
934 tes_stage->code = brw_compile_tes(compiler, device, mem_ctx,
935 &tes_stage->key.tes,
936 &tcs_stage->prog_data.tcs.base.vue_map,
937 &tes_stage->prog_data.tes,
938 tes_stage->nir, -1,
939 tes_stage->stats, NULL);
940 }
941
942 static void
943 anv_pipeline_link_gs(const struct brw_compiler *compiler,
944 struct anv_pipeline_stage *gs_stage,
945 struct anv_pipeline_stage *next_stage)
946 {
947 if (next_stage)
948 brw_nir_link_shaders(compiler, gs_stage->nir, next_stage->nir);
949 }
950
951 static void
952 anv_pipeline_compile_gs(const struct brw_compiler *compiler,
953 void *mem_ctx,
954 struct anv_device *device,
955 struct anv_pipeline_stage *gs_stage,
956 struct anv_pipeline_stage *prev_stage)
957 {
958 brw_compute_vue_map(compiler->devinfo,
959 &gs_stage->prog_data.gs.base.vue_map,
960 gs_stage->nir->info.outputs_written,
961 gs_stage->nir->info.separate_shader, 1);
962
963 gs_stage->num_stats = 1;
964 gs_stage->code = brw_compile_gs(compiler, device, mem_ctx,
965 &gs_stage->key.gs,
966 &gs_stage->prog_data.gs,
967 gs_stage->nir, NULL, -1,
968 gs_stage->stats, NULL);
969 }
970
971 static void
972 anv_pipeline_link_fs(const struct brw_compiler *compiler,
973 struct anv_pipeline_stage *stage)
974 {
975 unsigned num_rt_bindings;
976 struct anv_pipeline_binding rt_bindings[MAX_RTS];
977 if (stage->key.wm.nr_color_regions > 0) {
978 assert(stage->key.wm.nr_color_regions <= MAX_RTS);
979 for (unsigned rt = 0; rt < stage->key.wm.nr_color_regions; rt++) {
980 if (stage->key.wm.color_outputs_valid & BITFIELD_BIT(rt)) {
981 rt_bindings[rt] = (struct anv_pipeline_binding) {
982 .set = ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS,
983 .index = rt,
984 };
985 } else {
986 /* Setup a null render target */
987 rt_bindings[rt] = (struct anv_pipeline_binding) {
988 .set = ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS,
989 .index = UINT32_MAX,
990 };
991 }
992 }
993 num_rt_bindings = stage->key.wm.nr_color_regions;
994 } else {
995 /* Setup a null render target */
996 rt_bindings[0] = (struct anv_pipeline_binding) {
997 .set = ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS,
998 .index = UINT32_MAX,
999 };
1000 num_rt_bindings = 1;
1001 }
1002
1003 assert(num_rt_bindings <= MAX_RTS);
1004 assert(stage->bind_map.surface_count == 0);
1005 typed_memcpy(stage->bind_map.surface_to_descriptor,
1006 rt_bindings, num_rt_bindings);
1007 stage->bind_map.surface_count += num_rt_bindings;
1008
1009 /* Now that we've set up the color attachments, we can go through and
1010 * eliminate any shader outputs that map to VK_ATTACHMENT_UNUSED in the
1011 * hopes that dead code can clean them up in this and any earlier shader
1012 * stages.
1013 */
1014 nir_function_impl *impl = nir_shader_get_entrypoint(stage->nir);
1015 bool deleted_output = false;
1016 nir_foreach_variable_safe(var, &stage->nir->outputs) {
1017 /* TODO: We don't delete depth/stencil writes. We probably could if the
1018 * subpass doesn't have a depth/stencil attachment.
1019 */
1020 if (var->data.location < FRAG_RESULT_DATA0)
1021 continue;
1022
1023 const unsigned rt = var->data.location - FRAG_RESULT_DATA0;
1024
1025 /* If this is the RT at location 0 and we have alpha to coverage
1026 * enabled we still need that write because it will affect the coverage
1027 * mask even if it's never written to a color target.
1028 */
1029 if (rt == 0 && stage->key.wm.alpha_to_coverage)
1030 continue;
1031
1032 const unsigned array_len =
1033 glsl_type_is_array(var->type) ? glsl_get_length(var->type) : 1;
1034 assert(rt + array_len <= MAX_RTS);
1035
1036 if (rt >= MAX_RTS || !(stage->key.wm.color_outputs_valid &
1037 BITFIELD_RANGE(rt, array_len))) {
1038 deleted_output = true;
1039 var->data.mode = nir_var_function_temp;
1040 exec_node_remove(&var->node);
1041 exec_list_push_tail(&impl->locals, &var->node);
1042 }
1043 }
1044
1045 if (deleted_output)
1046 nir_fixup_deref_modes(stage->nir);
1047
1048 /* We stored the number of subpass color attachments in nr_color_regions
1049 * when calculating the key for caching. Now that we've computed the bind
1050 * map, we can reduce this to the actual max before we go into the back-end
1051 * compiler.
1052 */
1053 stage->key.wm.nr_color_regions =
1054 util_last_bit(stage->key.wm.color_outputs_valid);
1055 }
1056
1057 static void
1058 anv_pipeline_compile_fs(const struct brw_compiler *compiler,
1059 void *mem_ctx,
1060 struct anv_device *device,
1061 struct anv_pipeline_stage *fs_stage,
1062 struct anv_pipeline_stage *prev_stage)
1063 {
1064 /* TODO: we could set this to 0 based on the information in nir_shader, but
1065 * we need this before we call spirv_to_nir.
1066 */
1067 assert(prev_stage);
1068 fs_stage->key.wm.input_slots_valid =
1069 prev_stage->prog_data.vue.vue_map.slots_valid;
1070
1071 fs_stage->code = brw_compile_fs(compiler, device, mem_ctx,
1072 &fs_stage->key.wm,
1073 &fs_stage->prog_data.wm,
1074 fs_stage->nir, -1, -1, -1,
1075 true, false, NULL,
1076 fs_stage->stats, NULL);
1077
1078 fs_stage->num_stats = (uint32_t)fs_stage->prog_data.wm.dispatch_8 +
1079 (uint32_t)fs_stage->prog_data.wm.dispatch_16 +
1080 (uint32_t)fs_stage->prog_data.wm.dispatch_32;
1081
1082 if (fs_stage->key.wm.color_outputs_valid == 0 &&
1083 !fs_stage->prog_data.wm.has_side_effects &&
1084 !fs_stage->prog_data.wm.uses_omask &&
1085 !fs_stage->key.wm.alpha_to_coverage &&
1086 !fs_stage->prog_data.wm.uses_kill &&
1087 fs_stage->prog_data.wm.computed_depth_mode == BRW_PSCDEPTH_OFF &&
1088 !fs_stage->prog_data.wm.computed_stencil) {
1089 /* This fragment shader has no outputs and no side effects. Go ahead
1090 * and return the code pointer so we don't accidentally think the
1091 * compile failed but zero out prog_data which will set program_size to
1092 * zero and disable the stage.
1093 */
1094 memset(&fs_stage->prog_data, 0, sizeof(fs_stage->prog_data));
1095 }
1096 }
1097
1098 static void
1099 anv_pipeline_add_executable(struct anv_pipeline *pipeline,
1100 struct anv_pipeline_stage *stage,
1101 struct brw_compile_stats *stats,
1102 uint32_t code_offset)
1103 {
1104 char *nir = NULL;
1105 if (stage->nir &&
1106 (pipeline->flags &
1107 VK_PIPELINE_CREATE_CAPTURE_INTERNAL_REPRESENTATIONS_BIT_KHR)) {
1108 char *stream_data = NULL;
1109 size_t stream_size = 0;
1110 FILE *stream = open_memstream(&stream_data, &stream_size);
1111
1112 nir_print_shader(stage->nir, stream);
1113
1114 fclose(stream);
1115
1116 /* Copy it to a ralloc'd thing */
1117 nir = ralloc_size(pipeline->mem_ctx, stream_size + 1);
1118 memcpy(nir, stream_data, stream_size);
1119 nir[stream_size] = 0;
1120
1121 free(stream_data);
1122 }
1123
1124 char *disasm = NULL;
1125 if (stage->code &&
1126 (pipeline->flags &
1127 VK_PIPELINE_CREATE_CAPTURE_INTERNAL_REPRESENTATIONS_BIT_KHR)) {
1128 char *stream_data = NULL;
1129 size_t stream_size = 0;
1130 FILE *stream = open_memstream(&stream_data, &stream_size);
1131
1132 uint32_t push_size = 0;
1133 for (unsigned i = 0; i < 4; i++)
1134 push_size += stage->bind_map.push_ranges[i].length;
1135 if (push_size > 0) {
1136 fprintf(stream, "Push constant ranges:\n");
1137 for (unsigned i = 0; i < 4; i++) {
1138 if (stage->bind_map.push_ranges[i].length == 0)
1139 continue;
1140
1141 fprintf(stream, " RANGE%d (%dB): ", i,
1142 stage->bind_map.push_ranges[i].length * 32);
1143
1144 switch (stage->bind_map.push_ranges[i].set) {
1145 case ANV_DESCRIPTOR_SET_NULL:
1146 fprintf(stream, "NULL");
1147 break;
1148
1149 case ANV_DESCRIPTOR_SET_PUSH_CONSTANTS:
1150 fprintf(stream, "Vulkan push constants and API params");
1151 break;
1152
1153 case ANV_DESCRIPTOR_SET_DESCRIPTORS:
1154 fprintf(stream, "Descriptor buffer for set %d (start=%dB)",
1155 stage->bind_map.push_ranges[i].index,
1156 stage->bind_map.push_ranges[i].start * 32);
1157 break;
1158
1159 case ANV_DESCRIPTOR_SET_NUM_WORK_GROUPS:
1160 unreachable("gl_NumWorkgroups is never pushed");
1161
1162 case ANV_DESCRIPTOR_SET_SHADER_CONSTANTS:
1163 fprintf(stream, "Inline shader constant data (start=%dB)",
1164 stage->bind_map.push_ranges[i].start * 32);
1165 break;
1166
1167 case ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS:
1168 unreachable("Color attachments can't be pushed");
1169
1170 default:
1171 fprintf(stream, "UBO (set=%d binding=%d start=%dB)",
1172 stage->bind_map.push_ranges[i].set,
1173 stage->bind_map.push_ranges[i].index,
1174 stage->bind_map.push_ranges[i].start * 32);
1175 break;
1176 }
1177 fprintf(stream, "\n");
1178 }
1179 fprintf(stream, "\n");
1180 }
1181
1182 /* Creating this is far cheaper than it looks. It's perfectly fine to
1183 * do it for every binary.
1184 */
1185 struct gen_disasm *d = gen_disasm_create(&pipeline->device->info);
1186 gen_disasm_disassemble(d, stage->code, code_offset, stream);
1187 gen_disasm_destroy(d);
1188
1189 fclose(stream);
1190
1191 /* Copy it to a ralloc'd thing */
1192 disasm = ralloc_size(pipeline->mem_ctx, stream_size + 1);
1193 memcpy(disasm, stream_data, stream_size);
1194 disasm[stream_size] = 0;
1195
1196 free(stream_data);
1197 }
1198
1199 const struct anv_pipeline_executable exe = {
1200 .stage = stage->stage,
1201 .stats = *stats,
1202 .nir = nir,
1203 .disasm = disasm,
1204 };
1205 util_dynarray_append(&pipeline->executables,
1206 struct anv_pipeline_executable, exe);
1207 }
1208
1209 static void
1210 anv_pipeline_add_executables(struct anv_pipeline *pipeline,
1211 struct anv_pipeline_stage *stage,
1212 struct anv_shader_bin *bin)
1213 {
1214 if (stage->stage == MESA_SHADER_FRAGMENT) {
1215 /* We pull the prog data and stats out of the anv_shader_bin because
1216 * the anv_pipeline_stage may not be fully populated if we successfully
1217 * looked up the shader in a cache.
1218 */
1219 const struct brw_wm_prog_data *wm_prog_data =
1220 (const struct brw_wm_prog_data *)bin->prog_data;
1221 struct brw_compile_stats *stats = bin->stats;
1222
1223 if (wm_prog_data->dispatch_8) {
1224 anv_pipeline_add_executable(pipeline, stage, stats++, 0);
1225 }
1226
1227 if (wm_prog_data->dispatch_16) {
1228 anv_pipeline_add_executable(pipeline, stage, stats++,
1229 wm_prog_data->prog_offset_16);
1230 }
1231
1232 if (wm_prog_data->dispatch_32) {
1233 anv_pipeline_add_executable(pipeline, stage, stats++,
1234 wm_prog_data->prog_offset_32);
1235 }
1236 } else {
1237 anv_pipeline_add_executable(pipeline, stage, bin->stats, 0);
1238 }
1239 }
1240
1241 static void
1242 anv_pipeline_init_from_cached_graphics(struct anv_graphics_pipeline *pipeline)
1243 {
1244 /* TODO: Cache this pipeline-wide information. */
1245
1246 /* Primitive replication depends on information from all the shaders.
1247 * Recover this bit from the fact that we have more than one position slot
1248 * in the vertex shader when using it.
1249 */
1250 assert(pipeline->active_stages & VK_SHADER_STAGE_VERTEX_BIT);
1251 int pos_slots = 0;
1252 const struct brw_vue_prog_data *vue_prog_data =
1253 (const void *) pipeline->shaders[MESA_SHADER_VERTEX]->prog_data;
1254 const struct brw_vue_map *vue_map = &vue_prog_data->vue_map;
1255 for (int i = 0; i < vue_map->num_slots; i++) {
1256 if (vue_map->slot_to_varying[i] == VARYING_SLOT_POS)
1257 pos_slots++;
1258 }
1259 pipeline->use_primitive_replication = pos_slots > 1;
1260 }
1261
1262 static VkResult
1263 anv_pipeline_compile_graphics(struct anv_graphics_pipeline *pipeline,
1264 struct anv_pipeline_cache *cache,
1265 const VkGraphicsPipelineCreateInfo *info)
1266 {
1267 VkPipelineCreationFeedbackEXT pipeline_feedback = {
1268 .flags = VK_PIPELINE_CREATION_FEEDBACK_VALID_BIT_EXT,
1269 };
1270 int64_t pipeline_start = os_time_get_nano();
1271
1272 const struct brw_compiler *compiler = pipeline->base.device->physical->compiler;
1273 struct anv_pipeline_stage stages[MESA_SHADER_STAGES] = {};
1274
1275 pipeline->active_stages = 0;
1276
1277 VkResult result;
1278 for (uint32_t i = 0; i < info->stageCount; i++) {
1279 const VkPipelineShaderStageCreateInfo *sinfo = &info->pStages[i];
1280 gl_shader_stage stage = vk_to_mesa_shader_stage(sinfo->stage);
1281
1282 pipeline->active_stages |= sinfo->stage;
1283
1284 int64_t stage_start = os_time_get_nano();
1285
1286 stages[stage].stage = stage;
1287 stages[stage].module = anv_shader_module_from_handle(sinfo->module);
1288 stages[stage].entrypoint = sinfo->pName;
1289 stages[stage].spec_info = sinfo->pSpecializationInfo;
1290 anv_pipeline_hash_shader(stages[stage].module,
1291 stages[stage].entrypoint,
1292 stage,
1293 stages[stage].spec_info,
1294 stages[stage].shader_sha1);
1295
1296 const struct gen_device_info *devinfo = &pipeline->base.device->info;
1297 switch (stage) {
1298 case MESA_SHADER_VERTEX:
1299 populate_vs_prog_key(devinfo, sinfo->flags, &stages[stage].key.vs);
1300 break;
1301 case MESA_SHADER_TESS_CTRL:
1302 populate_tcs_prog_key(devinfo, sinfo->flags,
1303 info->pTessellationState->patchControlPoints,
1304 &stages[stage].key.tcs);
1305 break;
1306 case MESA_SHADER_TESS_EVAL:
1307 populate_tes_prog_key(devinfo, sinfo->flags, &stages[stage].key.tes);
1308 break;
1309 case MESA_SHADER_GEOMETRY:
1310 populate_gs_prog_key(devinfo, sinfo->flags, &stages[stage].key.gs);
1311 break;
1312 case MESA_SHADER_FRAGMENT: {
1313 const bool raster_enabled =
1314 !info->pRasterizationState->rasterizerDiscardEnable;
1315 populate_wm_prog_key(devinfo, sinfo->flags,
1316 pipeline->subpass,
1317 raster_enabled ? info->pMultisampleState : NULL,
1318 &stages[stage].key.wm);
1319 break;
1320 }
1321 default:
1322 unreachable("Invalid graphics shader stage");
1323 }
1324
1325 stages[stage].feedback.duration += os_time_get_nano() - stage_start;
1326 stages[stage].feedback.flags |= VK_PIPELINE_CREATION_FEEDBACK_VALID_BIT_EXT;
1327 }
1328
1329 if (pipeline->active_stages & VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT)
1330 pipeline->active_stages |= VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT;
1331
1332 assert(pipeline->active_stages & VK_SHADER_STAGE_VERTEX_BIT);
1333
1334 ANV_FROM_HANDLE(anv_pipeline_layout, layout, info->layout);
1335
1336 unsigned char sha1[20];
1337 anv_pipeline_hash_graphics(pipeline, layout, stages, sha1);
1338
1339 for (unsigned s = 0; s < MESA_SHADER_STAGES; s++) {
1340 if (!stages[s].entrypoint)
1341 continue;
1342
1343 stages[s].cache_key.stage = s;
1344 memcpy(stages[s].cache_key.sha1, sha1, sizeof(sha1));
1345 }
1346
1347 const bool skip_cache_lookup =
1348 (pipeline->base.flags & VK_PIPELINE_CREATE_CAPTURE_INTERNAL_REPRESENTATIONS_BIT_KHR);
1349
1350 if (!skip_cache_lookup) {
1351 unsigned found = 0;
1352 unsigned cache_hits = 0;
1353 for (unsigned s = 0; s < MESA_SHADER_STAGES; s++) {
1354 if (!stages[s].entrypoint)
1355 continue;
1356
1357 int64_t stage_start = os_time_get_nano();
1358
1359 bool cache_hit;
1360 struct anv_shader_bin *bin =
1361 anv_device_search_for_kernel(pipeline->base.device, cache,
1362 &stages[s].cache_key,
1363 sizeof(stages[s].cache_key), &cache_hit);
1364 if (bin) {
1365 found++;
1366 pipeline->shaders[s] = bin;
1367 }
1368
1369 if (cache_hit) {
1370 cache_hits++;
1371 stages[s].feedback.flags |=
1372 VK_PIPELINE_CREATION_FEEDBACK_APPLICATION_PIPELINE_CACHE_HIT_BIT_EXT;
1373 }
1374 stages[s].feedback.duration += os_time_get_nano() - stage_start;
1375 }
1376
1377 if (found == __builtin_popcount(pipeline->active_stages)) {
1378 if (cache_hits == found) {
1379 pipeline_feedback.flags |=
1380 VK_PIPELINE_CREATION_FEEDBACK_APPLICATION_PIPELINE_CACHE_HIT_BIT_EXT;
1381 }
1382 /* We found all our shaders in the cache. We're done. */
1383 for (unsigned s = 0; s < MESA_SHADER_STAGES; s++) {
1384 if (!stages[s].entrypoint)
1385 continue;
1386
1387 anv_pipeline_add_executables(&pipeline->base, &stages[s],
1388 pipeline->shaders[s]);
1389 }
1390 anv_pipeline_init_from_cached_graphics(pipeline);
1391 goto done;
1392 } else if (found > 0) {
1393 /* We found some but not all of our shaders. This shouldn't happen
1394 * most of the time but it can if we have a partially populated
1395 * pipeline cache.
1396 */
1397 assert(found < __builtin_popcount(pipeline->active_stages));
1398
1399 vk_debug_report(&pipeline->base.device->physical->instance->debug_report_callbacks,
1400 VK_DEBUG_REPORT_WARNING_BIT_EXT |
1401 VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
1402 VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_CACHE_EXT,
1403 (uint64_t)(uintptr_t)cache,
1404 0, 0, "anv",
1405 "Found a partial pipeline in the cache. This is "
1406 "most likely caused by an incomplete pipeline cache "
1407 "import or export");
1408
1409 /* We're going to have to recompile anyway, so just throw away our
1410 * references to the shaders in the cache. We'll get them out of the
1411 * cache again as part of the compilation process.
1412 */
1413 for (unsigned s = 0; s < MESA_SHADER_STAGES; s++) {
1414 stages[s].feedback.flags = 0;
1415 if (pipeline->shaders[s]) {
1416 anv_shader_bin_unref(pipeline->base.device, pipeline->shaders[s]);
1417 pipeline->shaders[s] = NULL;
1418 }
1419 }
1420 }
1421 }
1422
1423 if (info->flags & VK_PIPELINE_CREATE_FAIL_ON_PIPELINE_COMPILE_REQUIRED_BIT_EXT)
1424 return VK_PIPELINE_COMPILE_REQUIRED_EXT;
1425
1426 void *pipeline_ctx = ralloc_context(NULL);
1427
1428 for (unsigned s = 0; s < MESA_SHADER_STAGES; s++) {
1429 if (!stages[s].entrypoint)
1430 continue;
1431
1432 int64_t stage_start = os_time_get_nano();
1433
1434 assert(stages[s].stage == s);
1435 assert(pipeline->shaders[s] == NULL);
1436
1437 stages[s].bind_map = (struct anv_pipeline_bind_map) {
1438 .surface_to_descriptor = stages[s].surface_to_descriptor,
1439 .sampler_to_descriptor = stages[s].sampler_to_descriptor
1440 };
1441
1442 stages[s].nir = anv_pipeline_stage_get_nir(&pipeline->base, cache,
1443 pipeline_ctx,
1444 &stages[s]);
1445 if (stages[s].nir == NULL) {
1446 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1447 goto fail;
1448 }
1449
1450 stages[s].feedback.duration += os_time_get_nano() - stage_start;
1451 }
1452
1453 /* Walk backwards to link */
1454 struct anv_pipeline_stage *next_stage = NULL;
1455 for (int s = MESA_SHADER_STAGES - 1; s >= 0; s--) {
1456 if (!stages[s].entrypoint)
1457 continue;
1458
1459 switch (s) {
1460 case MESA_SHADER_VERTEX:
1461 anv_pipeline_link_vs(compiler, &stages[s], next_stage);
1462 break;
1463 case MESA_SHADER_TESS_CTRL:
1464 anv_pipeline_link_tcs(compiler, &stages[s], next_stage);
1465 break;
1466 case MESA_SHADER_TESS_EVAL:
1467 anv_pipeline_link_tes(compiler, &stages[s], next_stage);
1468 break;
1469 case MESA_SHADER_GEOMETRY:
1470 anv_pipeline_link_gs(compiler, &stages[s], next_stage);
1471 break;
1472 case MESA_SHADER_FRAGMENT:
1473 anv_pipeline_link_fs(compiler, &stages[s]);
1474 break;
1475 default:
1476 unreachable("Invalid graphics shader stage");
1477 }
1478
1479 next_stage = &stages[s];
1480 }
1481
1482 if (pipeline->base.device->info.gen >= 12 &&
1483 pipeline->subpass->view_mask != 0) {
1484 /* For some pipelines HW Primitive Replication can be used instead of
1485 * instancing to implement Multiview. This depend on how viewIndex is
1486 * used in all the active shaders, so this check can't be done per
1487 * individual shaders.
1488 */
1489 nir_shader *shaders[MESA_SHADER_STAGES] = {};
1490 for (unsigned s = 0; s < MESA_SHADER_STAGES; s++)
1491 shaders[s] = stages[s].nir;
1492
1493 pipeline->use_primitive_replication =
1494 anv_check_for_primitive_replication(shaders, pipeline);
1495 } else {
1496 pipeline->use_primitive_replication = false;
1497 }
1498
1499 struct anv_pipeline_stage *prev_stage = NULL;
1500 for (unsigned s = 0; s < MESA_SHADER_STAGES; s++) {
1501 if (!stages[s].entrypoint)
1502 continue;
1503
1504 int64_t stage_start = os_time_get_nano();
1505
1506 void *stage_ctx = ralloc_context(NULL);
1507
1508 nir_xfb_info *xfb_info = NULL;
1509 if (s == MESA_SHADER_VERTEX ||
1510 s == MESA_SHADER_TESS_EVAL ||
1511 s == MESA_SHADER_GEOMETRY)
1512 xfb_info = nir_gather_xfb_info(stages[s].nir, stage_ctx);
1513
1514 anv_pipeline_lower_nir(&pipeline->base, stage_ctx, &stages[s], layout);
1515
1516 switch (s) {
1517 case MESA_SHADER_VERTEX:
1518 anv_pipeline_compile_vs(compiler, stage_ctx, pipeline,
1519 &stages[s]);
1520 break;
1521 case MESA_SHADER_TESS_CTRL:
1522 anv_pipeline_compile_tcs(compiler, stage_ctx, pipeline->base.device,
1523 &stages[s], prev_stage);
1524 break;
1525 case MESA_SHADER_TESS_EVAL:
1526 anv_pipeline_compile_tes(compiler, stage_ctx, pipeline->base.device,
1527 &stages[s], prev_stage);
1528 break;
1529 case MESA_SHADER_GEOMETRY:
1530 anv_pipeline_compile_gs(compiler, stage_ctx, pipeline->base.device,
1531 &stages[s], prev_stage);
1532 break;
1533 case MESA_SHADER_FRAGMENT:
1534 anv_pipeline_compile_fs(compiler, stage_ctx, pipeline->base.device,
1535 &stages[s], prev_stage);
1536 break;
1537 default:
1538 unreachable("Invalid graphics shader stage");
1539 }
1540 if (stages[s].code == NULL) {
1541 ralloc_free(stage_ctx);
1542 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1543 goto fail;
1544 }
1545
1546 anv_nir_validate_push_layout(&stages[s].prog_data.base,
1547 &stages[s].bind_map);
1548
1549 struct anv_shader_bin *bin =
1550 anv_device_upload_kernel(pipeline->base.device, cache, s,
1551 &stages[s].cache_key,
1552 sizeof(stages[s].cache_key),
1553 stages[s].code,
1554 stages[s].prog_data.base.program_size,
1555 stages[s].nir->constant_data,
1556 stages[s].nir->constant_data_size,
1557 &stages[s].prog_data.base,
1558 brw_prog_data_size(s),
1559 stages[s].stats, stages[s].num_stats,
1560 xfb_info, &stages[s].bind_map);
1561 if (!bin) {
1562 ralloc_free(stage_ctx);
1563 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1564 goto fail;
1565 }
1566
1567 anv_pipeline_add_executables(&pipeline->base, &stages[s], bin);
1568
1569 pipeline->shaders[s] = bin;
1570 ralloc_free(stage_ctx);
1571
1572 stages[s].feedback.duration += os_time_get_nano() - stage_start;
1573
1574 prev_stage = &stages[s];
1575 }
1576
1577 ralloc_free(pipeline_ctx);
1578
1579 done:
1580
1581 if (pipeline->shaders[MESA_SHADER_FRAGMENT] &&
1582 pipeline->shaders[MESA_SHADER_FRAGMENT]->prog_data->program_size == 0) {
1583 /* This can happen if we decided to implicitly disable the fragment
1584 * shader. See anv_pipeline_compile_fs().
1585 */
1586 anv_shader_bin_unref(pipeline->base.device,
1587 pipeline->shaders[MESA_SHADER_FRAGMENT]);
1588 pipeline->shaders[MESA_SHADER_FRAGMENT] = NULL;
1589 pipeline->active_stages &= ~VK_SHADER_STAGE_FRAGMENT_BIT;
1590 }
1591
1592 pipeline_feedback.duration = os_time_get_nano() - pipeline_start;
1593
1594 const VkPipelineCreationFeedbackCreateInfoEXT *create_feedback =
1595 vk_find_struct_const(info->pNext, PIPELINE_CREATION_FEEDBACK_CREATE_INFO_EXT);
1596 if (create_feedback) {
1597 *create_feedback->pPipelineCreationFeedback = pipeline_feedback;
1598
1599 assert(info->stageCount == create_feedback->pipelineStageCreationFeedbackCount);
1600 for (uint32_t i = 0; i < info->stageCount; i++) {
1601 gl_shader_stage s = vk_to_mesa_shader_stage(info->pStages[i].stage);
1602 create_feedback->pPipelineStageCreationFeedbacks[i] = stages[s].feedback;
1603 }
1604 }
1605
1606 return VK_SUCCESS;
1607
1608 fail:
1609 ralloc_free(pipeline_ctx);
1610
1611 for (unsigned s = 0; s < MESA_SHADER_STAGES; s++) {
1612 if (pipeline->shaders[s])
1613 anv_shader_bin_unref(pipeline->base.device, pipeline->shaders[s]);
1614 }
1615
1616 return result;
1617 }
1618
1619 static void
1620 shared_type_info(const struct glsl_type *type, unsigned *size, unsigned *align)
1621 {
1622 assert(glsl_type_is_vector_or_scalar(type));
1623
1624 uint32_t comp_size = glsl_type_is_boolean(type)
1625 ? 4 : glsl_get_bit_size(type) / 8;
1626 unsigned length = glsl_get_vector_elements(type);
1627 *size = comp_size * length,
1628 *align = comp_size * (length == 3 ? 4 : length);
1629 }
1630
1631 VkResult
1632 anv_pipeline_compile_cs(struct anv_compute_pipeline *pipeline,
1633 struct anv_pipeline_cache *cache,
1634 const VkComputePipelineCreateInfo *info,
1635 const struct anv_shader_module *module,
1636 const char *entrypoint,
1637 const VkSpecializationInfo *spec_info)
1638 {
1639 VkPipelineCreationFeedbackEXT pipeline_feedback = {
1640 .flags = VK_PIPELINE_CREATION_FEEDBACK_VALID_BIT_EXT,
1641 };
1642 int64_t pipeline_start = os_time_get_nano();
1643
1644 const struct brw_compiler *compiler = pipeline->base.device->physical->compiler;
1645
1646 struct anv_pipeline_stage stage = {
1647 .stage = MESA_SHADER_COMPUTE,
1648 .module = module,
1649 .entrypoint = entrypoint,
1650 .spec_info = spec_info,
1651 .cache_key = {
1652 .stage = MESA_SHADER_COMPUTE,
1653 },
1654 .feedback = {
1655 .flags = VK_PIPELINE_CREATION_FEEDBACK_VALID_BIT_EXT,
1656 },
1657 };
1658 anv_pipeline_hash_shader(stage.module,
1659 stage.entrypoint,
1660 MESA_SHADER_COMPUTE,
1661 stage.spec_info,
1662 stage.shader_sha1);
1663
1664 struct anv_shader_bin *bin = NULL;
1665
1666 const VkPipelineShaderStageRequiredSubgroupSizeCreateInfoEXT *rss_info =
1667 vk_find_struct_const(info->stage.pNext,
1668 PIPELINE_SHADER_STAGE_REQUIRED_SUBGROUP_SIZE_CREATE_INFO_EXT);
1669
1670 populate_cs_prog_key(&pipeline->base.device->info, info->stage.flags,
1671 rss_info, &stage.key.cs);
1672
1673 ANV_FROM_HANDLE(anv_pipeline_layout, layout, info->layout);
1674
1675 const bool skip_cache_lookup =
1676 (pipeline->base.flags & VK_PIPELINE_CREATE_CAPTURE_INTERNAL_REPRESENTATIONS_BIT_KHR);
1677
1678 anv_pipeline_hash_compute(pipeline, layout, &stage, stage.cache_key.sha1);
1679
1680 bool cache_hit = false;
1681 if (!skip_cache_lookup) {
1682 bin = anv_device_search_for_kernel(pipeline->base.device, cache,
1683 &stage.cache_key,
1684 sizeof(stage.cache_key),
1685 &cache_hit);
1686 }
1687
1688 if (bin == NULL &&
1689 (info->flags & VK_PIPELINE_CREATE_FAIL_ON_PIPELINE_COMPILE_REQUIRED_BIT_EXT))
1690 return VK_PIPELINE_COMPILE_REQUIRED_EXT;
1691
1692 void *mem_ctx = ralloc_context(NULL);
1693 if (bin == NULL) {
1694 int64_t stage_start = os_time_get_nano();
1695
1696 stage.bind_map = (struct anv_pipeline_bind_map) {
1697 .surface_to_descriptor = stage.surface_to_descriptor,
1698 .sampler_to_descriptor = stage.sampler_to_descriptor
1699 };
1700
1701 /* Set up a binding for the gl_NumWorkGroups */
1702 stage.bind_map.surface_count = 1;
1703 stage.bind_map.surface_to_descriptor[0] = (struct anv_pipeline_binding) {
1704 .set = ANV_DESCRIPTOR_SET_NUM_WORK_GROUPS,
1705 };
1706
1707 stage.nir = anv_pipeline_stage_get_nir(&pipeline->base, cache, mem_ctx, &stage);
1708 if (stage.nir == NULL) {
1709 ralloc_free(mem_ctx);
1710 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1711 }
1712
1713 NIR_PASS_V(stage.nir, anv_nir_add_base_work_group_id);
1714
1715 anv_pipeline_lower_nir(&pipeline->base, mem_ctx, &stage, layout);
1716
1717 NIR_PASS_V(stage.nir, nir_lower_vars_to_explicit_types,
1718 nir_var_mem_shared, shared_type_info);
1719 NIR_PASS_V(stage.nir, nir_lower_explicit_io,
1720 nir_var_mem_shared, nir_address_format_32bit_offset);
1721 NIR_PASS_V(stage.nir, brw_nir_lower_cs_intrinsics);
1722
1723 stage.num_stats = 1;
1724 stage.code = brw_compile_cs(compiler, pipeline->base.device, mem_ctx,
1725 &stage.key.cs, &stage.prog_data.cs,
1726 stage.nir, -1, stage.stats, NULL);
1727 if (stage.code == NULL) {
1728 ralloc_free(mem_ctx);
1729 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1730 }
1731
1732 anv_nir_validate_push_layout(&stage.prog_data.base, &stage.bind_map);
1733
1734 if (!stage.prog_data.cs.uses_num_work_groups) {
1735 assert(stage.bind_map.surface_to_descriptor[0].set ==
1736 ANV_DESCRIPTOR_SET_NUM_WORK_GROUPS);
1737 stage.bind_map.surface_to_descriptor[0].set = ANV_DESCRIPTOR_SET_NULL;
1738 }
1739
1740 const unsigned code_size = stage.prog_data.base.program_size;
1741 bin = anv_device_upload_kernel(pipeline->base.device, cache,
1742 MESA_SHADER_COMPUTE,
1743 &stage.cache_key, sizeof(stage.cache_key),
1744 stage.code, code_size,
1745 stage.nir->constant_data,
1746 stage.nir->constant_data_size,
1747 &stage.prog_data.base,
1748 sizeof(stage.prog_data.cs),
1749 stage.stats, stage.num_stats,
1750 NULL, &stage.bind_map);
1751 if (!bin) {
1752 ralloc_free(mem_ctx);
1753 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1754 }
1755
1756 stage.feedback.duration = os_time_get_nano() - stage_start;
1757 }
1758
1759 anv_pipeline_add_executables(&pipeline->base, &stage, bin);
1760
1761 ralloc_free(mem_ctx);
1762
1763 if (cache_hit) {
1764 stage.feedback.flags |=
1765 VK_PIPELINE_CREATION_FEEDBACK_APPLICATION_PIPELINE_CACHE_HIT_BIT_EXT;
1766 pipeline_feedback.flags |=
1767 VK_PIPELINE_CREATION_FEEDBACK_APPLICATION_PIPELINE_CACHE_HIT_BIT_EXT;
1768 }
1769 pipeline_feedback.duration = os_time_get_nano() - pipeline_start;
1770
1771 const VkPipelineCreationFeedbackCreateInfoEXT *create_feedback =
1772 vk_find_struct_const(info->pNext, PIPELINE_CREATION_FEEDBACK_CREATE_INFO_EXT);
1773 if (create_feedback) {
1774 *create_feedback->pPipelineCreationFeedback = pipeline_feedback;
1775
1776 assert(create_feedback->pipelineStageCreationFeedbackCount == 1);
1777 create_feedback->pPipelineStageCreationFeedbacks[0] = stage.feedback;
1778 }
1779
1780 pipeline->cs = bin;
1781
1782 return VK_SUCCESS;
1783 }
1784
1785 struct anv_cs_parameters
1786 anv_cs_parameters(const struct anv_compute_pipeline *pipeline)
1787 {
1788 const struct brw_cs_prog_data *cs_prog_data = get_cs_prog_data(pipeline);
1789
1790 struct anv_cs_parameters cs_params = {};
1791
1792 cs_params.group_size = cs_prog_data->local_size[0] *
1793 cs_prog_data->local_size[1] *
1794 cs_prog_data->local_size[2];
1795 cs_params.simd_size =
1796 brw_cs_simd_size_for_group_size(&pipeline->base.device->info,
1797 cs_prog_data, cs_params.group_size);
1798 cs_params.threads = DIV_ROUND_UP(cs_params.group_size, cs_params.simd_size);
1799
1800 return cs_params;
1801 }
1802
1803 /**
1804 * Copy pipeline state not marked as dynamic.
1805 * Dynamic state is pipeline state which hasn't been provided at pipeline
1806 * creation time, but is dynamically provided afterwards using various
1807 * vkCmdSet* functions.
1808 *
1809 * The set of state considered "non_dynamic" is determined by the pieces of
1810 * state that have their corresponding VkDynamicState enums omitted from
1811 * VkPipelineDynamicStateCreateInfo::pDynamicStates.
1812 *
1813 * @param[out] pipeline Destination non_dynamic state.
1814 * @param[in] pCreateInfo Source of non_dynamic state to be copied.
1815 */
1816 static void
1817 copy_non_dynamic_state(struct anv_graphics_pipeline *pipeline,
1818 const VkGraphicsPipelineCreateInfo *pCreateInfo)
1819 {
1820 anv_cmd_dirty_mask_t states = ANV_CMD_DIRTY_DYNAMIC_ALL;
1821 struct anv_subpass *subpass = pipeline->subpass;
1822
1823 pipeline->dynamic_state = default_dynamic_state;
1824
1825 if (pCreateInfo->pDynamicState) {
1826 /* Remove all of the states that are marked as dynamic */
1827 uint32_t count = pCreateInfo->pDynamicState->dynamicStateCount;
1828 for (uint32_t s = 0; s < count; s++) {
1829 states &= ~anv_cmd_dirty_bit_for_vk_dynamic_state(
1830 pCreateInfo->pDynamicState->pDynamicStates[s]);
1831 }
1832 }
1833
1834 struct anv_dynamic_state *dynamic = &pipeline->dynamic_state;
1835
1836 /* Section 9.2 of the Vulkan 1.0.15 spec says:
1837 *
1838 * pViewportState is [...] NULL if the pipeline
1839 * has rasterization disabled.
1840 */
1841 if (!pCreateInfo->pRasterizationState->rasterizerDiscardEnable) {
1842 assert(pCreateInfo->pViewportState);
1843
1844 dynamic->viewport.count = pCreateInfo->pViewportState->viewportCount;
1845 if (states & ANV_CMD_DIRTY_DYNAMIC_VIEWPORT) {
1846 typed_memcpy(dynamic->viewport.viewports,
1847 pCreateInfo->pViewportState->pViewports,
1848 pCreateInfo->pViewportState->viewportCount);
1849 }
1850
1851 dynamic->scissor.count = pCreateInfo->pViewportState->scissorCount;
1852 if (states & ANV_CMD_DIRTY_DYNAMIC_SCISSOR) {
1853 typed_memcpy(dynamic->scissor.scissors,
1854 pCreateInfo->pViewportState->pScissors,
1855 pCreateInfo->pViewportState->scissorCount);
1856 }
1857 }
1858
1859 if (states & ANV_CMD_DIRTY_DYNAMIC_LINE_WIDTH) {
1860 assert(pCreateInfo->pRasterizationState);
1861 dynamic->line_width = pCreateInfo->pRasterizationState->lineWidth;
1862 }
1863
1864 if (states & ANV_CMD_DIRTY_DYNAMIC_DEPTH_BIAS) {
1865 assert(pCreateInfo->pRasterizationState);
1866 dynamic->depth_bias.bias =
1867 pCreateInfo->pRasterizationState->depthBiasConstantFactor;
1868 dynamic->depth_bias.clamp =
1869 pCreateInfo->pRasterizationState->depthBiasClamp;
1870 dynamic->depth_bias.slope =
1871 pCreateInfo->pRasterizationState->depthBiasSlopeFactor;
1872 }
1873
1874 /* Section 9.2 of the Vulkan 1.0.15 spec says:
1875 *
1876 * pColorBlendState is [...] NULL if the pipeline has rasterization
1877 * disabled or if the subpass of the render pass the pipeline is
1878 * created against does not use any color attachments.
1879 */
1880 bool uses_color_att = false;
1881 for (unsigned i = 0; i < subpass->color_count; ++i) {
1882 if (subpass->color_attachments[i].attachment != VK_ATTACHMENT_UNUSED) {
1883 uses_color_att = true;
1884 break;
1885 }
1886 }
1887
1888 if (uses_color_att &&
1889 !pCreateInfo->pRasterizationState->rasterizerDiscardEnable) {
1890 assert(pCreateInfo->pColorBlendState);
1891
1892 if (states & ANV_CMD_DIRTY_DYNAMIC_BLEND_CONSTANTS)
1893 typed_memcpy(dynamic->blend_constants,
1894 pCreateInfo->pColorBlendState->blendConstants, 4);
1895 }
1896
1897 /* If there is no depthstencil attachment, then don't read
1898 * pDepthStencilState. The Vulkan spec states that pDepthStencilState may
1899 * be NULL in this case. Even if pDepthStencilState is non-NULL, there is
1900 * no need to override the depthstencil defaults in
1901 * anv_pipeline::dynamic_state when there is no depthstencil attachment.
1902 *
1903 * Section 9.2 of the Vulkan 1.0.15 spec says:
1904 *
1905 * pDepthStencilState is [...] NULL if the pipeline has rasterization
1906 * disabled or if the subpass of the render pass the pipeline is created
1907 * against does not use a depth/stencil attachment.
1908 */
1909 if (!pCreateInfo->pRasterizationState->rasterizerDiscardEnable &&
1910 subpass->depth_stencil_attachment) {
1911 assert(pCreateInfo->pDepthStencilState);
1912
1913 if (states & ANV_CMD_DIRTY_DYNAMIC_DEPTH_BOUNDS) {
1914 dynamic->depth_bounds.min =
1915 pCreateInfo->pDepthStencilState->minDepthBounds;
1916 dynamic->depth_bounds.max =
1917 pCreateInfo->pDepthStencilState->maxDepthBounds;
1918 }
1919
1920 if (states & ANV_CMD_DIRTY_DYNAMIC_STENCIL_COMPARE_MASK) {
1921 dynamic->stencil_compare_mask.front =
1922 pCreateInfo->pDepthStencilState->front.compareMask;
1923 dynamic->stencil_compare_mask.back =
1924 pCreateInfo->pDepthStencilState->back.compareMask;
1925 }
1926
1927 if (states & ANV_CMD_DIRTY_DYNAMIC_STENCIL_WRITE_MASK) {
1928 dynamic->stencil_write_mask.front =
1929 pCreateInfo->pDepthStencilState->front.writeMask;
1930 dynamic->stencil_write_mask.back =
1931 pCreateInfo->pDepthStencilState->back.writeMask;
1932 }
1933
1934 if (states & ANV_CMD_DIRTY_DYNAMIC_STENCIL_REFERENCE) {
1935 dynamic->stencil_reference.front =
1936 pCreateInfo->pDepthStencilState->front.reference;
1937 dynamic->stencil_reference.back =
1938 pCreateInfo->pDepthStencilState->back.reference;
1939 }
1940 }
1941
1942 const VkPipelineRasterizationLineStateCreateInfoEXT *line_state =
1943 vk_find_struct_const(pCreateInfo->pRasterizationState->pNext,
1944 PIPELINE_RASTERIZATION_LINE_STATE_CREATE_INFO_EXT);
1945 if (line_state) {
1946 if (states & ANV_CMD_DIRTY_DYNAMIC_LINE_STIPPLE) {
1947 dynamic->line_stipple.factor = line_state->lineStippleFactor;
1948 dynamic->line_stipple.pattern = line_state->lineStipplePattern;
1949 }
1950 }
1951
1952 pipeline->dynamic_state_mask = states;
1953 }
1954
1955 static void
1956 anv_pipeline_validate_create_info(const VkGraphicsPipelineCreateInfo *info)
1957 {
1958 #ifdef DEBUG
1959 struct anv_render_pass *renderpass = NULL;
1960 struct anv_subpass *subpass = NULL;
1961
1962 /* Assert that all required members of VkGraphicsPipelineCreateInfo are
1963 * present. See the Vulkan 1.0.28 spec, Section 9.2 Graphics Pipelines.
1964 */
1965 assert(info->sType == VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO);
1966
1967 renderpass = anv_render_pass_from_handle(info->renderPass);
1968 assert(renderpass);
1969
1970 assert(info->subpass < renderpass->subpass_count);
1971 subpass = &renderpass->subpasses[info->subpass];
1972
1973 assert(info->stageCount >= 1);
1974 assert(info->pVertexInputState);
1975 assert(info->pInputAssemblyState);
1976 assert(info->pRasterizationState);
1977 if (!info->pRasterizationState->rasterizerDiscardEnable) {
1978 assert(info->pViewportState);
1979 assert(info->pMultisampleState);
1980
1981 if (subpass && subpass->depth_stencil_attachment)
1982 assert(info->pDepthStencilState);
1983
1984 if (subpass && subpass->color_count > 0) {
1985 bool all_color_unused = true;
1986 for (int i = 0; i < subpass->color_count; i++) {
1987 if (subpass->color_attachments[i].attachment != VK_ATTACHMENT_UNUSED)
1988 all_color_unused = false;
1989 }
1990 /* pColorBlendState is ignored if the pipeline has rasterization
1991 * disabled or if the subpass of the render pass the pipeline is
1992 * created against does not use any color attachments.
1993 */
1994 assert(info->pColorBlendState || all_color_unused);
1995 }
1996 }
1997
1998 for (uint32_t i = 0; i < info->stageCount; ++i) {
1999 switch (info->pStages[i].stage) {
2000 case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT:
2001 case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT:
2002 assert(info->pTessellationState);
2003 break;
2004 default:
2005 break;
2006 }
2007 }
2008 #endif
2009 }
2010
2011 /**
2012 * Calculate the desired L3 partitioning based on the current state of the
2013 * pipeline. For now this simply returns the conservative defaults calculated
2014 * by get_default_l3_weights(), but we could probably do better by gathering
2015 * more statistics from the pipeline state (e.g. guess of expected URB usage
2016 * and bound surfaces), or by using feed-back from performance counters.
2017 */
2018 void
2019 anv_pipeline_setup_l3_config(struct anv_pipeline *pipeline, bool needs_slm)
2020 {
2021 const struct gen_device_info *devinfo = &pipeline->device->info;
2022
2023 const struct gen_l3_weights w =
2024 gen_get_default_l3_weights(devinfo, true, needs_slm);
2025
2026 pipeline->l3_config = gen_get_l3_config(devinfo, w);
2027 }
2028
2029 VkResult
2030 anv_graphics_pipeline_init(struct anv_graphics_pipeline *pipeline,
2031 struct anv_device *device,
2032 struct anv_pipeline_cache *cache,
2033 const VkGraphicsPipelineCreateInfo *pCreateInfo,
2034 const VkAllocationCallbacks *alloc)
2035 {
2036 VkResult result;
2037
2038 anv_pipeline_validate_create_info(pCreateInfo);
2039
2040 result = anv_pipeline_init(&pipeline->base, device,
2041 ANV_PIPELINE_GRAPHICS, pCreateInfo->flags,
2042 alloc);
2043 if (result != VK_SUCCESS)
2044 return result;
2045
2046 anv_batch_set_storage(&pipeline->base.batch, ANV_NULL_ADDRESS,
2047 pipeline->batch_data, sizeof(pipeline->batch_data));
2048
2049 ANV_FROM_HANDLE(anv_render_pass, render_pass, pCreateInfo->renderPass);
2050 assert(pCreateInfo->subpass < render_pass->subpass_count);
2051 pipeline->subpass = &render_pass->subpasses[pCreateInfo->subpass];
2052
2053 assert(pCreateInfo->pRasterizationState);
2054
2055 copy_non_dynamic_state(pipeline, pCreateInfo);
2056 pipeline->depth_clamp_enable = pCreateInfo->pRasterizationState->depthClampEnable;
2057
2058 /* Previously we enabled depth clipping when !depthClampEnable.
2059 * DepthClipStateCreateInfo now makes depth clipping explicit so if the
2060 * clipping info is available, use its enable value to determine clipping,
2061 * otherwise fallback to the previous !depthClampEnable logic.
2062 */
2063 const VkPipelineRasterizationDepthClipStateCreateInfoEXT *clip_info =
2064 vk_find_struct_const(pCreateInfo->pRasterizationState->pNext,
2065 PIPELINE_RASTERIZATION_DEPTH_CLIP_STATE_CREATE_INFO_EXT);
2066 pipeline->depth_clip_enable = clip_info ? clip_info->depthClipEnable : !pipeline->depth_clamp_enable;
2067
2068 pipeline->sample_shading_enable =
2069 !pCreateInfo->pRasterizationState->rasterizerDiscardEnable &&
2070 pCreateInfo->pMultisampleState &&
2071 pCreateInfo->pMultisampleState->sampleShadingEnable;
2072
2073 /* When we free the pipeline, we detect stages based on the NULL status
2074 * of various prog_data pointers. Make them NULL by default.
2075 */
2076 memset(pipeline->shaders, 0, sizeof(pipeline->shaders));
2077
2078 result = anv_pipeline_compile_graphics(pipeline, cache, pCreateInfo);
2079 if (result != VK_SUCCESS) {
2080 anv_pipeline_finish(&pipeline->base, device, alloc);
2081 return result;
2082 }
2083
2084 assert(pipeline->shaders[MESA_SHADER_VERTEX]);
2085
2086 anv_pipeline_setup_l3_config(&pipeline->base, false);
2087
2088 const VkPipelineVertexInputStateCreateInfo *vi_info =
2089 pCreateInfo->pVertexInputState;
2090
2091 const uint64_t inputs_read = get_vs_prog_data(pipeline)->inputs_read;
2092
2093 pipeline->vb_used = 0;
2094 for (uint32_t i = 0; i < vi_info->vertexAttributeDescriptionCount; i++) {
2095 const VkVertexInputAttributeDescription *desc =
2096 &vi_info->pVertexAttributeDescriptions[i];
2097
2098 if (inputs_read & (1ull << (VERT_ATTRIB_GENERIC0 + desc->location)))
2099 pipeline->vb_used |= 1 << desc->binding;
2100 }
2101
2102 for (uint32_t i = 0; i < vi_info->vertexBindingDescriptionCount; i++) {
2103 const VkVertexInputBindingDescription *desc =
2104 &vi_info->pVertexBindingDescriptions[i];
2105
2106 pipeline->vb[desc->binding].stride = desc->stride;
2107
2108 /* Step rate is programmed per vertex element (attribute), not
2109 * binding. Set up a map of which bindings step per instance, for
2110 * reference by vertex element setup. */
2111 switch (desc->inputRate) {
2112 default:
2113 case VK_VERTEX_INPUT_RATE_VERTEX:
2114 pipeline->vb[desc->binding].instanced = false;
2115 break;
2116 case VK_VERTEX_INPUT_RATE_INSTANCE:
2117 pipeline->vb[desc->binding].instanced = true;
2118 break;
2119 }
2120
2121 pipeline->vb[desc->binding].instance_divisor = 1;
2122 }
2123
2124 const VkPipelineVertexInputDivisorStateCreateInfoEXT *vi_div_state =
2125 vk_find_struct_const(vi_info->pNext,
2126 PIPELINE_VERTEX_INPUT_DIVISOR_STATE_CREATE_INFO_EXT);
2127 if (vi_div_state) {
2128 for (uint32_t i = 0; i < vi_div_state->vertexBindingDivisorCount; i++) {
2129 const VkVertexInputBindingDivisorDescriptionEXT *desc =
2130 &vi_div_state->pVertexBindingDivisors[i];
2131
2132 pipeline->vb[desc->binding].instance_divisor = desc->divisor;
2133 }
2134 }
2135
2136 /* Our implementation of VK_KHR_multiview uses instancing to draw the
2137 * different views. If the client asks for instancing, we need to multiply
2138 * the instance divisor by the number of views ensure that we repeat the
2139 * client's per-instance data once for each view.
2140 */
2141 if (pipeline->subpass->view_mask && !pipeline->use_primitive_replication) {
2142 const uint32_t view_count = anv_subpass_view_count(pipeline->subpass);
2143 for (uint32_t vb = 0; vb < MAX_VBS; vb++) {
2144 if (pipeline->vb[vb].instanced)
2145 pipeline->vb[vb].instance_divisor *= view_count;
2146 }
2147 }
2148
2149 const VkPipelineInputAssemblyStateCreateInfo *ia_info =
2150 pCreateInfo->pInputAssemblyState;
2151 const VkPipelineTessellationStateCreateInfo *tess_info =
2152 pCreateInfo->pTessellationState;
2153 pipeline->primitive_restart = ia_info->primitiveRestartEnable;
2154
2155 if (anv_pipeline_has_stage(pipeline, MESA_SHADER_TESS_EVAL))
2156 pipeline->topology = _3DPRIM_PATCHLIST(tess_info->patchControlPoints);
2157 else
2158 pipeline->topology = vk_to_gen_primitive_type[ia_info->topology];
2159
2160 return VK_SUCCESS;
2161 }
2162
2163 #define WRITE_STR(field, ...) ({ \
2164 memset(field, 0, sizeof(field)); \
2165 UNUSED int i = snprintf(field, sizeof(field), __VA_ARGS__); \
2166 assert(i > 0 && i < sizeof(field)); \
2167 })
2168
2169 VkResult anv_GetPipelineExecutablePropertiesKHR(
2170 VkDevice device,
2171 const VkPipelineInfoKHR* pPipelineInfo,
2172 uint32_t* pExecutableCount,
2173 VkPipelineExecutablePropertiesKHR* pProperties)
2174 {
2175 ANV_FROM_HANDLE(anv_pipeline, pipeline, pPipelineInfo->pipeline);
2176 VK_OUTARRAY_MAKE(out, pProperties, pExecutableCount);
2177
2178 util_dynarray_foreach (&pipeline->executables, struct anv_pipeline_executable, exe) {
2179 vk_outarray_append(&out, props) {
2180 gl_shader_stage stage = exe->stage;
2181 props->stages = mesa_to_vk_shader_stage(stage);
2182
2183 unsigned simd_width = exe->stats.dispatch_width;
2184 if (stage == MESA_SHADER_FRAGMENT) {
2185 WRITE_STR(props->name, "%s%d %s",
2186 simd_width ? "SIMD" : "vec",
2187 simd_width ? simd_width : 4,
2188 _mesa_shader_stage_to_string(stage));
2189 } else {
2190 WRITE_STR(props->name, "%s", _mesa_shader_stage_to_string(stage));
2191 }
2192 WRITE_STR(props->description, "%s%d %s shader",
2193 simd_width ? "SIMD" : "vec",
2194 simd_width ? simd_width : 4,
2195 _mesa_shader_stage_to_string(stage));
2196
2197 /* The compiler gives us a dispatch width of 0 for vec4 but Vulkan
2198 * wants a subgroup size of 1.
2199 */
2200 props->subgroupSize = MAX2(simd_width, 1);
2201 }
2202 }
2203
2204 return vk_outarray_status(&out);
2205 }
2206
2207 static const struct anv_pipeline_executable *
2208 anv_pipeline_get_executable(struct anv_pipeline *pipeline, uint32_t index)
2209 {
2210 assert(index < util_dynarray_num_elements(&pipeline->executables,
2211 struct anv_pipeline_executable));
2212 return util_dynarray_element(
2213 &pipeline->executables, struct anv_pipeline_executable, index);
2214 }
2215
2216 VkResult anv_GetPipelineExecutableStatisticsKHR(
2217 VkDevice device,
2218 const VkPipelineExecutableInfoKHR* pExecutableInfo,
2219 uint32_t* pStatisticCount,
2220 VkPipelineExecutableStatisticKHR* pStatistics)
2221 {
2222 ANV_FROM_HANDLE(anv_pipeline, pipeline, pExecutableInfo->pipeline);
2223 VK_OUTARRAY_MAKE(out, pStatistics, pStatisticCount);
2224
2225 const struct anv_pipeline_executable *exe =
2226 anv_pipeline_get_executable(pipeline, pExecutableInfo->executableIndex);
2227
2228 const struct brw_stage_prog_data *prog_data;
2229 switch (pipeline->type) {
2230 case ANV_PIPELINE_GRAPHICS: {
2231 prog_data = anv_pipeline_to_graphics(pipeline)->shaders[exe->stage]->prog_data;
2232 break;
2233 }
2234 case ANV_PIPELINE_COMPUTE: {
2235 prog_data = anv_pipeline_to_compute(pipeline)->cs->prog_data;
2236 break;
2237 }
2238 default:
2239 unreachable("invalid pipeline type");
2240 }
2241
2242 vk_outarray_append(&out, stat) {
2243 WRITE_STR(stat->name, "Instruction Count");
2244 WRITE_STR(stat->description,
2245 "Number of GEN instructions in the final generated "
2246 "shader executable.");
2247 stat->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;
2248 stat->value.u64 = exe->stats.instructions;
2249 }
2250
2251 vk_outarray_append(&out, stat) {
2252 WRITE_STR(stat->name, "SEND Count");
2253 WRITE_STR(stat->description,
2254 "Number of instructions in the final generated shader "
2255 "executable which access external units such as the "
2256 "constant cache or the sampler.");
2257 stat->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;
2258 stat->value.u64 = exe->stats.sends;
2259 }
2260
2261 vk_outarray_append(&out, stat) {
2262 WRITE_STR(stat->name, "Loop Count");
2263 WRITE_STR(stat->description,
2264 "Number of loops (not unrolled) in the final generated "
2265 "shader executable.");
2266 stat->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;
2267 stat->value.u64 = exe->stats.loops;
2268 }
2269
2270 vk_outarray_append(&out, stat) {
2271 WRITE_STR(stat->name, "Cycle Count");
2272 WRITE_STR(stat->description,
2273 "Estimate of the number of EU cycles required to execute "
2274 "the final generated executable. This is an estimate only "
2275 "and may vary greatly from actual run-time performance.");
2276 stat->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;
2277 stat->value.u64 = exe->stats.cycles;
2278 }
2279
2280 vk_outarray_append(&out, stat) {
2281 WRITE_STR(stat->name, "Spill Count");
2282 WRITE_STR(stat->description,
2283 "Number of scratch spill operations. This gives a rough "
2284 "estimate of the cost incurred due to spilling temporary "
2285 "values to memory. If this is non-zero, you may want to "
2286 "adjust your shader to reduce register pressure.");
2287 stat->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;
2288 stat->value.u64 = exe->stats.spills;
2289 }
2290
2291 vk_outarray_append(&out, stat) {
2292 WRITE_STR(stat->name, "Fill Count");
2293 WRITE_STR(stat->description,
2294 "Number of scratch fill operations. This gives a rough "
2295 "estimate of the cost incurred due to spilling temporary "
2296 "values to memory. If this is non-zero, you may want to "
2297 "adjust your shader to reduce register pressure.");
2298 stat->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;
2299 stat->value.u64 = exe->stats.fills;
2300 }
2301
2302 vk_outarray_append(&out, stat) {
2303 WRITE_STR(stat->name, "Scratch Memory Size");
2304 WRITE_STR(stat->description,
2305 "Number of bytes of scratch memory required by the "
2306 "generated shader executable. If this is non-zero, you "
2307 "may want to adjust your shader to reduce register "
2308 "pressure.");
2309 stat->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;
2310 stat->value.u64 = prog_data->total_scratch;
2311 }
2312
2313 if (exe->stage == MESA_SHADER_COMPUTE) {
2314 vk_outarray_append(&out, stat) {
2315 WRITE_STR(stat->name, "Workgroup Memory Size");
2316 WRITE_STR(stat->description,
2317 "Number of bytes of workgroup shared memory used by this "
2318 "compute shader including any padding.");
2319 stat->format = VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR;
2320 stat->value.u64 = brw_cs_prog_data_const(prog_data)->slm_size;
2321 }
2322 }
2323
2324 return vk_outarray_status(&out);
2325 }
2326
2327 static bool
2328 write_ir_text(VkPipelineExecutableInternalRepresentationKHR* ir,
2329 const char *data)
2330 {
2331 ir->isText = VK_TRUE;
2332
2333 size_t data_len = strlen(data) + 1;
2334
2335 if (ir->pData == NULL) {
2336 ir->dataSize = data_len;
2337 return true;
2338 }
2339
2340 strncpy(ir->pData, data, ir->dataSize);
2341 if (ir->dataSize < data_len)
2342 return false;
2343
2344 ir->dataSize = data_len;
2345 return true;
2346 }
2347
2348 VkResult anv_GetPipelineExecutableInternalRepresentationsKHR(
2349 VkDevice device,
2350 const VkPipelineExecutableInfoKHR* pExecutableInfo,
2351 uint32_t* pInternalRepresentationCount,
2352 VkPipelineExecutableInternalRepresentationKHR* pInternalRepresentations)
2353 {
2354 ANV_FROM_HANDLE(anv_pipeline, pipeline, pExecutableInfo->pipeline);
2355 VK_OUTARRAY_MAKE(out, pInternalRepresentations,
2356 pInternalRepresentationCount);
2357 bool incomplete_text = false;
2358
2359 const struct anv_pipeline_executable *exe =
2360 anv_pipeline_get_executable(pipeline, pExecutableInfo->executableIndex);
2361
2362 if (exe->nir) {
2363 vk_outarray_append(&out, ir) {
2364 WRITE_STR(ir->name, "Final NIR");
2365 WRITE_STR(ir->description,
2366 "Final NIR before going into the back-end compiler");
2367
2368 if (!write_ir_text(ir, exe->nir))
2369 incomplete_text = true;
2370 }
2371 }
2372
2373 if (exe->disasm) {
2374 vk_outarray_append(&out, ir) {
2375 WRITE_STR(ir->name, "GEN Assembly");
2376 WRITE_STR(ir->description,
2377 "Final GEN assembly for the generated shader binary");
2378
2379 if (!write_ir_text(ir, exe->disasm))
2380 incomplete_text = true;
2381 }
2382 }
2383
2384 return incomplete_text ? VK_INCOMPLETE : vk_outarray_status(&out);
2385 }