anv/pipeline: Stash generated code in the pipeline stage
[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 "anv_private.h"
34 #include "compiler/brw_nir.h"
35 #include "anv_nir.h"
36 #include "nir/nir_xfb_info.h"
37 #include "spirv/nir_spirv.h"
38 #include "vk_util.h"
39
40 /* Needed for SWIZZLE macros */
41 #include "program/prog_instruction.h"
42
43 // Shader functions
44
45 VkResult anv_CreateShaderModule(
46 VkDevice _device,
47 const VkShaderModuleCreateInfo* pCreateInfo,
48 const VkAllocationCallbacks* pAllocator,
49 VkShaderModule* pShaderModule)
50 {
51 ANV_FROM_HANDLE(anv_device, device, _device);
52 struct anv_shader_module *module;
53
54 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO);
55 assert(pCreateInfo->flags == 0);
56
57 module = vk_alloc2(&device->alloc, pAllocator,
58 sizeof(*module) + pCreateInfo->codeSize, 8,
59 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
60 if (module == NULL)
61 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
62
63 module->size = pCreateInfo->codeSize;
64 memcpy(module->data, pCreateInfo->pCode, module->size);
65
66 _mesa_sha1_compute(module->data, module->size, module->sha1);
67
68 *pShaderModule = anv_shader_module_to_handle(module);
69
70 return VK_SUCCESS;
71 }
72
73 void anv_DestroyShaderModule(
74 VkDevice _device,
75 VkShaderModule _module,
76 const VkAllocationCallbacks* pAllocator)
77 {
78 ANV_FROM_HANDLE(anv_device, device, _device);
79 ANV_FROM_HANDLE(anv_shader_module, module, _module);
80
81 if (!module)
82 return;
83
84 vk_free2(&device->alloc, pAllocator, module);
85 }
86
87 #define SPIR_V_MAGIC_NUMBER 0x07230203
88
89 static const uint64_t stage_to_debug[] = {
90 [MESA_SHADER_VERTEX] = DEBUG_VS,
91 [MESA_SHADER_TESS_CTRL] = DEBUG_TCS,
92 [MESA_SHADER_TESS_EVAL] = DEBUG_TES,
93 [MESA_SHADER_GEOMETRY] = DEBUG_GS,
94 [MESA_SHADER_FRAGMENT] = DEBUG_WM,
95 [MESA_SHADER_COMPUTE] = DEBUG_CS,
96 };
97
98 struct anv_spirv_debug_data {
99 struct anv_device *device;
100 const struct anv_shader_module *module;
101 };
102
103 static void anv_spirv_nir_debug(void *private_data,
104 enum nir_spirv_debug_level level,
105 size_t spirv_offset,
106 const char *message)
107 {
108 struct anv_spirv_debug_data *debug_data = private_data;
109 static const VkDebugReportFlagsEXT vk_flags[] = {
110 [NIR_SPIRV_DEBUG_LEVEL_INFO] = VK_DEBUG_REPORT_INFORMATION_BIT_EXT,
111 [NIR_SPIRV_DEBUG_LEVEL_WARNING] = VK_DEBUG_REPORT_WARNING_BIT_EXT,
112 [NIR_SPIRV_DEBUG_LEVEL_ERROR] = VK_DEBUG_REPORT_ERROR_BIT_EXT,
113 };
114 char buffer[256];
115
116 snprintf(buffer, sizeof(buffer), "SPIR-V offset %lu: %s", (unsigned long) spirv_offset, message);
117
118 vk_debug_report(&debug_data->device->instance->debug_report_callbacks,
119 vk_flags[level],
120 VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT,
121 (uint64_t) (uintptr_t) debug_data->module,
122 0, 0, "anv", buffer);
123 }
124
125 /* Eventually, this will become part of anv_CreateShader. Unfortunately,
126 * we can't do that yet because we don't have the ability to copy nir.
127 */
128 static nir_shader *
129 anv_shader_compile_to_nir(struct anv_device *device,
130 void *mem_ctx,
131 const struct anv_shader_module *module,
132 const char *entrypoint_name,
133 gl_shader_stage stage,
134 const VkSpecializationInfo *spec_info)
135 {
136 const struct anv_physical_device *pdevice =
137 &device->instance->physicalDevice;
138 const struct brw_compiler *compiler = pdevice->compiler;
139 const nir_shader_compiler_options *nir_options =
140 compiler->glsl_compiler_options[stage].NirOptions;
141
142 uint32_t *spirv = (uint32_t *) module->data;
143 assert(spirv[0] == SPIR_V_MAGIC_NUMBER);
144 assert(module->size % 4 == 0);
145
146 uint32_t num_spec_entries = 0;
147 struct nir_spirv_specialization *spec_entries = NULL;
148 if (spec_info && spec_info->mapEntryCount > 0) {
149 num_spec_entries = spec_info->mapEntryCount;
150 spec_entries = malloc(num_spec_entries * sizeof(*spec_entries));
151 for (uint32_t i = 0; i < num_spec_entries; i++) {
152 VkSpecializationMapEntry entry = spec_info->pMapEntries[i];
153 const void *data = spec_info->pData + entry.offset;
154 assert(data + entry.size <= spec_info->pData + spec_info->dataSize);
155
156 spec_entries[i].id = spec_info->pMapEntries[i].constantID;
157 if (spec_info->dataSize == 8)
158 spec_entries[i].data64 = *(const uint64_t *)data;
159 else
160 spec_entries[i].data32 = *(const uint32_t *)data;
161 }
162 }
163
164 struct anv_spirv_debug_data spirv_debug_data = {
165 .device = device,
166 .module = module,
167 };
168 struct spirv_to_nir_options spirv_options = {
169 .frag_coord_is_sysval = true,
170 .caps = {
171 .demote_to_helper_invocation = true,
172 .derivative_group = true,
173 .descriptor_array_dynamic_indexing = true,
174 .descriptor_array_non_uniform_indexing = true,
175 .descriptor_indexing = true,
176 .device_group = true,
177 .draw_parameters = true,
178 .float16 = pdevice->info.gen >= 8,
179 .float64 = pdevice->info.gen >= 8,
180 .fragment_shader_sample_interlock = pdevice->info.gen >= 9,
181 .fragment_shader_pixel_interlock = pdevice->info.gen >= 9,
182 .geometry_streams = true,
183 .image_write_without_format = true,
184 .int8 = pdevice->info.gen >= 8,
185 .int16 = pdevice->info.gen >= 8,
186 .int64 = pdevice->info.gen >= 8,
187 .int64_atomics = pdevice->info.gen >= 9 && pdevice->use_softpin,
188 .min_lod = true,
189 .multiview = true,
190 .physical_storage_buffer_address = pdevice->has_a64_buffer_access,
191 .post_depth_coverage = pdevice->info.gen >= 9,
192 .runtime_descriptor_array = true,
193 .shader_viewport_index_layer = true,
194 .stencil_export = pdevice->info.gen >= 9,
195 .storage_8bit = pdevice->info.gen >= 8,
196 .storage_16bit = pdevice->info.gen >= 8,
197 .subgroup_arithmetic = true,
198 .subgroup_basic = true,
199 .subgroup_ballot = true,
200 .subgroup_quad = true,
201 .subgroup_shuffle = true,
202 .subgroup_vote = true,
203 .tessellation = true,
204 .transform_feedback = pdevice->info.gen >= 8,
205 .variable_pointers = true,
206 },
207 .ubo_addr_format = nir_address_format_32bit_index_offset,
208 .ssbo_addr_format =
209 anv_nir_ssbo_addr_format(pdevice, device->robust_buffer_access),
210 .phys_ssbo_addr_format = nir_address_format_64bit_global,
211 .push_const_addr_format = nir_address_format_logical,
212
213 /* TODO: Consider changing this to an address format that has the NULL
214 * pointer equals to 0. That might be a better format to play nice
215 * with certain code / code generators.
216 */
217 .shared_addr_format = nir_address_format_32bit_offset,
218 .debug = {
219 .func = anv_spirv_nir_debug,
220 .private_data = &spirv_debug_data,
221 },
222 };
223
224
225 nir_shader *nir =
226 spirv_to_nir(spirv, module->size / 4,
227 spec_entries, num_spec_entries,
228 stage, entrypoint_name, &spirv_options, nir_options);
229 assert(nir->info.stage == stage);
230 nir_validate_shader(nir, "after spirv_to_nir");
231 ralloc_steal(mem_ctx, nir);
232
233 free(spec_entries);
234
235 if (unlikely(INTEL_DEBUG & stage_to_debug[stage])) {
236 fprintf(stderr, "NIR (from SPIR-V) for %s shader:\n",
237 gl_shader_stage_name(stage));
238 nir_print_shader(nir, stderr);
239 }
240
241 /* We have to lower away local constant initializers right before we
242 * inline functions. That way they get properly initialized at the top
243 * of the function and not at the top of its caller.
244 */
245 NIR_PASS_V(nir, nir_lower_constant_initializers, nir_var_function_temp);
246 NIR_PASS_V(nir, nir_lower_returns);
247 NIR_PASS_V(nir, nir_inline_functions);
248 NIR_PASS_V(nir, nir_opt_deref);
249
250 /* Pick off the single entrypoint that we want */
251 foreach_list_typed_safe(nir_function, func, node, &nir->functions) {
252 if (!func->is_entrypoint)
253 exec_node_remove(&func->node);
254 }
255 assert(exec_list_length(&nir->functions) == 1);
256
257 /* Now that we've deleted all but the main function, we can go ahead and
258 * lower the rest of the constant initializers. We do this here so that
259 * nir_remove_dead_variables and split_per_member_structs below see the
260 * corresponding stores.
261 */
262 NIR_PASS_V(nir, nir_lower_constant_initializers, ~0);
263
264 /* Split member structs. We do this before lower_io_to_temporaries so that
265 * it doesn't lower system values to temporaries by accident.
266 */
267 NIR_PASS_V(nir, nir_split_var_copies);
268 NIR_PASS_V(nir, nir_split_per_member_structs);
269
270 NIR_PASS_V(nir, nir_remove_dead_variables,
271 nir_var_shader_in | nir_var_shader_out | nir_var_system_value);
272
273 NIR_PASS_V(nir, nir_propagate_invariant);
274 NIR_PASS_V(nir, nir_lower_io_to_temporaries,
275 nir_shader_get_entrypoint(nir), true, false);
276
277 NIR_PASS_V(nir, nir_lower_frexp);
278
279 /* Vulkan uses the separate-shader linking model */
280 nir->info.separate_shader = true;
281
282 brw_preprocess_nir(compiler, nir, NULL);
283
284 return nir;
285 }
286
287 void anv_DestroyPipeline(
288 VkDevice _device,
289 VkPipeline _pipeline,
290 const VkAllocationCallbacks* pAllocator)
291 {
292 ANV_FROM_HANDLE(anv_device, device, _device);
293 ANV_FROM_HANDLE(anv_pipeline, pipeline, _pipeline);
294
295 if (!pipeline)
296 return;
297
298 anv_reloc_list_finish(&pipeline->batch_relocs,
299 pAllocator ? pAllocator : &device->alloc);
300 if (pipeline->blend_state.map)
301 anv_state_pool_free(&device->dynamic_state_pool, pipeline->blend_state);
302
303 for (unsigned s = 0; s < MESA_SHADER_STAGES; s++) {
304 if (pipeline->shaders[s])
305 anv_shader_bin_unref(device, pipeline->shaders[s]);
306 }
307
308 vk_free2(&device->alloc, pAllocator, pipeline);
309 }
310
311 static const uint32_t vk_to_gen_primitive_type[] = {
312 [VK_PRIMITIVE_TOPOLOGY_POINT_LIST] = _3DPRIM_POINTLIST,
313 [VK_PRIMITIVE_TOPOLOGY_LINE_LIST] = _3DPRIM_LINELIST,
314 [VK_PRIMITIVE_TOPOLOGY_LINE_STRIP] = _3DPRIM_LINESTRIP,
315 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST] = _3DPRIM_TRILIST,
316 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP] = _3DPRIM_TRISTRIP,
317 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN] = _3DPRIM_TRIFAN,
318 [VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY] = _3DPRIM_LINELIST_ADJ,
319 [VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY] = _3DPRIM_LINESTRIP_ADJ,
320 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY] = _3DPRIM_TRILIST_ADJ,
321 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY] = _3DPRIM_TRISTRIP_ADJ,
322 };
323
324 static void
325 populate_sampler_prog_key(const struct gen_device_info *devinfo,
326 struct brw_sampler_prog_key_data *key)
327 {
328 /* Almost all multisampled textures are compressed. The only time when we
329 * don't compress a multisampled texture is for 16x MSAA with a surface
330 * width greater than 8k which is a bit of an edge case. Since the sampler
331 * just ignores the MCS parameter to ld2ms when MCS is disabled, it's safe
332 * to tell the compiler to always assume compression.
333 */
334 key->compressed_multisample_layout_mask = ~0;
335
336 /* SkyLake added support for 16x MSAA. With this came a new message for
337 * reading from a 16x MSAA surface with compression. The new message was
338 * needed because now the MCS data is 64 bits instead of 32 or lower as is
339 * the case for 8x, 4x, and 2x. The key->msaa_16 bit-field controls which
340 * message we use. Fortunately, the 16x message works for 8x, 4x, and 2x
341 * so we can just use it unconditionally. This may not be quite as
342 * efficient but it saves us from recompiling.
343 */
344 if (devinfo->gen >= 9)
345 key->msaa_16 = ~0;
346
347 /* XXX: Handle texture swizzle on HSW- */
348 for (int i = 0; i < MAX_SAMPLERS; i++) {
349 /* Assume color sampler, no swizzling. (Works for BDW+) */
350 key->swizzles[i] = SWIZZLE_XYZW;
351 }
352 }
353
354 static void
355 populate_base_prog_key(const struct gen_device_info *devinfo,
356 VkPipelineShaderStageCreateFlags flags,
357 struct brw_base_prog_key *key)
358 {
359 if (flags & VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT)
360 key->subgroup_size_type = BRW_SUBGROUP_SIZE_VARYING;
361 else
362 key->subgroup_size_type = BRW_SUBGROUP_SIZE_API_CONSTANT;
363
364 populate_sampler_prog_key(devinfo, &key->tex);
365 }
366
367 static void
368 populate_vs_prog_key(const struct gen_device_info *devinfo,
369 VkPipelineShaderStageCreateFlags flags,
370 struct brw_vs_prog_key *key)
371 {
372 memset(key, 0, sizeof(*key));
373
374 populate_base_prog_key(devinfo, flags, &key->base);
375
376 /* XXX: Handle vertex input work-arounds */
377
378 /* XXX: Handle sampler_prog_key */
379 }
380
381 static void
382 populate_tcs_prog_key(const struct gen_device_info *devinfo,
383 VkPipelineShaderStageCreateFlags flags,
384 unsigned input_vertices,
385 struct brw_tcs_prog_key *key)
386 {
387 memset(key, 0, sizeof(*key));
388
389 populate_base_prog_key(devinfo, flags, &key->base);
390
391 key->input_vertices = input_vertices;
392 }
393
394 static void
395 populate_tes_prog_key(const struct gen_device_info *devinfo,
396 VkPipelineShaderStageCreateFlags flags,
397 struct brw_tes_prog_key *key)
398 {
399 memset(key, 0, sizeof(*key));
400
401 populate_base_prog_key(devinfo, flags, &key->base);
402 }
403
404 static void
405 populate_gs_prog_key(const struct gen_device_info *devinfo,
406 VkPipelineShaderStageCreateFlags flags,
407 struct brw_gs_prog_key *key)
408 {
409 memset(key, 0, sizeof(*key));
410
411 populate_base_prog_key(devinfo, flags, &key->base);
412 }
413
414 static void
415 populate_wm_prog_key(const struct gen_device_info *devinfo,
416 VkPipelineShaderStageCreateFlags flags,
417 const struct anv_subpass *subpass,
418 const VkPipelineMultisampleStateCreateInfo *ms_info,
419 struct brw_wm_prog_key *key)
420 {
421 memset(key, 0, sizeof(*key));
422
423 populate_base_prog_key(devinfo, flags, &key->base);
424
425 /* We set this to 0 here and set to the actual value before we call
426 * brw_compile_fs.
427 */
428 key->input_slots_valid = 0;
429
430 /* Vulkan doesn't specify a default */
431 key->high_quality_derivatives = false;
432
433 /* XXX Vulkan doesn't appear to specify */
434 key->clamp_fragment_color = false;
435
436 assert(subpass->color_count <= MAX_RTS);
437 for (uint32_t i = 0; i < subpass->color_count; i++) {
438 if (subpass->color_attachments[i].attachment != VK_ATTACHMENT_UNUSED)
439 key->color_outputs_valid |= (1 << i);
440 }
441
442 key->nr_color_regions = util_bitcount(key->color_outputs_valid);
443
444 /* To reduce possible shader recompilations we would need to know if
445 * there is a SampleMask output variable to compute if we should emit
446 * code to workaround the issue that hardware disables alpha to coverage
447 * when there is SampleMask output.
448 */
449 key->alpha_to_coverage = ms_info && ms_info->alphaToCoverageEnable;
450
451 /* Vulkan doesn't support fixed-function alpha test */
452 key->alpha_test_replicate_alpha = false;
453
454 if (ms_info) {
455 /* We should probably pull this out of the shader, but it's fairly
456 * harmless to compute it and then let dead-code take care of it.
457 */
458 if (ms_info->rasterizationSamples > 1) {
459 key->persample_interp = ms_info->sampleShadingEnable &&
460 (ms_info->minSampleShading * ms_info->rasterizationSamples) > 1;
461 key->multisample_fbo = true;
462 }
463
464 key->frag_coord_adds_sample_pos = key->persample_interp;
465 }
466 }
467
468 static void
469 populate_cs_prog_key(const struct gen_device_info *devinfo,
470 VkPipelineShaderStageCreateFlags flags,
471 const VkPipelineShaderStageRequiredSubgroupSizeCreateInfoEXT *rss_info,
472 struct brw_cs_prog_key *key)
473 {
474 memset(key, 0, sizeof(*key));
475
476 populate_base_prog_key(devinfo, flags, &key->base);
477
478 if (rss_info) {
479 assert(key->base.subgroup_size_type != BRW_SUBGROUP_SIZE_VARYING);
480
481 /* These enum values are expressly chosen to be equal to the subgroup
482 * size that they require.
483 */
484 assert(rss_info->requiredSubgroupSize == 8 ||
485 rss_info->requiredSubgroupSize == 16 ||
486 rss_info->requiredSubgroupSize == 32);
487 key->base.subgroup_size_type = rss_info->requiredSubgroupSize;
488 } else if (flags & VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT) {
489 /* If the client expressly requests full subgroups and they don't
490 * specify a subgroup size, we need to pick one. If they're requested
491 * varying subgroup sizes, we set it to UNIFORM and let the back-end
492 * compiler pick. Otherwise, we specify the API value of 32.
493 * Performance will likely be terrible in this case but there's nothing
494 * we can do about that. The client should have chosen a size.
495 */
496 if (flags & VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT)
497 key->base.subgroup_size_type = BRW_SUBGROUP_SIZE_UNIFORM;
498 else
499 key->base.subgroup_size_type = BRW_SUBGROUP_SIZE_REQUIRE_32;
500 }
501 }
502
503 struct anv_pipeline_stage {
504 gl_shader_stage stage;
505
506 const struct anv_shader_module *module;
507 const char *entrypoint;
508 const VkSpecializationInfo *spec_info;
509
510 unsigned char shader_sha1[20];
511
512 union brw_any_prog_key key;
513
514 struct {
515 gl_shader_stage stage;
516 unsigned char sha1[20];
517 } cache_key;
518
519 nir_shader *nir;
520
521 struct anv_pipeline_binding surface_to_descriptor[256];
522 struct anv_pipeline_binding sampler_to_descriptor[256];
523 struct anv_pipeline_bind_map bind_map;
524
525 union brw_any_prog_data prog_data;
526
527 VkPipelineCreationFeedbackEXT feedback;
528
529 const unsigned *code;
530 };
531
532 static void
533 anv_pipeline_hash_shader(const struct anv_shader_module *module,
534 const char *entrypoint,
535 gl_shader_stage stage,
536 const VkSpecializationInfo *spec_info,
537 unsigned char *sha1_out)
538 {
539 struct mesa_sha1 ctx;
540 _mesa_sha1_init(&ctx);
541
542 _mesa_sha1_update(&ctx, module->sha1, sizeof(module->sha1));
543 _mesa_sha1_update(&ctx, entrypoint, strlen(entrypoint));
544 _mesa_sha1_update(&ctx, &stage, sizeof(stage));
545 if (spec_info) {
546 _mesa_sha1_update(&ctx, spec_info->pMapEntries,
547 spec_info->mapEntryCount *
548 sizeof(*spec_info->pMapEntries));
549 _mesa_sha1_update(&ctx, spec_info->pData,
550 spec_info->dataSize);
551 }
552
553 _mesa_sha1_final(&ctx, sha1_out);
554 }
555
556 static void
557 anv_pipeline_hash_graphics(struct anv_pipeline *pipeline,
558 struct anv_pipeline_layout *layout,
559 struct anv_pipeline_stage *stages,
560 unsigned char *sha1_out)
561 {
562 struct mesa_sha1 ctx;
563 _mesa_sha1_init(&ctx);
564
565 _mesa_sha1_update(&ctx, &pipeline->subpass->view_mask,
566 sizeof(pipeline->subpass->view_mask));
567
568 if (layout)
569 _mesa_sha1_update(&ctx, layout->sha1, sizeof(layout->sha1));
570
571 const bool rba = pipeline->device->robust_buffer_access;
572 _mesa_sha1_update(&ctx, &rba, sizeof(rba));
573
574 for (unsigned s = 0; s < MESA_SHADER_STAGES; s++) {
575 if (stages[s].entrypoint) {
576 _mesa_sha1_update(&ctx, stages[s].shader_sha1,
577 sizeof(stages[s].shader_sha1));
578 _mesa_sha1_update(&ctx, &stages[s].key, brw_prog_key_size(s));
579 }
580 }
581
582 _mesa_sha1_final(&ctx, sha1_out);
583 }
584
585 static void
586 anv_pipeline_hash_compute(struct anv_pipeline *pipeline,
587 struct anv_pipeline_layout *layout,
588 struct anv_pipeline_stage *stage,
589 unsigned char *sha1_out)
590 {
591 struct mesa_sha1 ctx;
592 _mesa_sha1_init(&ctx);
593
594 if (layout)
595 _mesa_sha1_update(&ctx, layout->sha1, sizeof(layout->sha1));
596
597 const bool rba = pipeline->device->robust_buffer_access;
598 _mesa_sha1_update(&ctx, &rba, sizeof(rba));
599
600 _mesa_sha1_update(&ctx, stage->shader_sha1,
601 sizeof(stage->shader_sha1));
602 _mesa_sha1_update(&ctx, &stage->key.cs, sizeof(stage->key.cs));
603
604 _mesa_sha1_final(&ctx, sha1_out);
605 }
606
607 static nir_shader *
608 anv_pipeline_stage_get_nir(struct anv_pipeline *pipeline,
609 struct anv_pipeline_cache *cache,
610 void *mem_ctx,
611 struct anv_pipeline_stage *stage)
612 {
613 const struct brw_compiler *compiler =
614 pipeline->device->instance->physicalDevice.compiler;
615 const nir_shader_compiler_options *nir_options =
616 compiler->glsl_compiler_options[stage->stage].NirOptions;
617 nir_shader *nir;
618
619 nir = anv_device_search_for_nir(pipeline->device, cache,
620 nir_options,
621 stage->shader_sha1,
622 mem_ctx);
623 if (nir) {
624 assert(nir->info.stage == stage->stage);
625 return nir;
626 }
627
628 nir = anv_shader_compile_to_nir(pipeline->device,
629 mem_ctx,
630 stage->module,
631 stage->entrypoint,
632 stage->stage,
633 stage->spec_info);
634 if (nir) {
635 anv_device_upload_nir(pipeline->device, cache, nir, stage->shader_sha1);
636 return nir;
637 }
638
639 return NULL;
640 }
641
642 static void
643 anv_pipeline_lower_nir(struct anv_pipeline *pipeline,
644 void *mem_ctx,
645 struct anv_pipeline_stage *stage,
646 struct anv_pipeline_layout *layout)
647 {
648 const struct anv_physical_device *pdevice =
649 &pipeline->device->instance->physicalDevice;
650 const struct brw_compiler *compiler = pdevice->compiler;
651
652 struct brw_stage_prog_data *prog_data = &stage->prog_data.base;
653 nir_shader *nir = stage->nir;
654
655 if (nir->info.stage == MESA_SHADER_FRAGMENT) {
656 NIR_PASS_V(nir, nir_lower_wpos_center, pipeline->sample_shading_enable);
657 NIR_PASS_V(nir, nir_lower_input_attachments, true);
658 }
659
660 NIR_PASS_V(nir, anv_nir_lower_ycbcr_textures, layout);
661
662 NIR_PASS_V(nir, anv_nir_lower_push_constants);
663
664 if (nir->info.stage != MESA_SHADER_COMPUTE)
665 NIR_PASS_V(nir, anv_nir_lower_multiview, pipeline->subpass->view_mask);
666
667 nir_shader_gather_info(nir, nir_shader_get_entrypoint(nir));
668
669 if (nir->num_uniforms > 0) {
670 assert(prog_data->nr_params == 0);
671
672 /* If the shader uses any push constants at all, we'll just give
673 * them the maximum possible number
674 */
675 assert(nir->num_uniforms <= MAX_PUSH_CONSTANTS_SIZE);
676 nir->num_uniforms = MAX_PUSH_CONSTANTS_SIZE;
677 prog_data->nr_params += MAX_PUSH_CONSTANTS_SIZE / sizeof(float);
678 prog_data->param = ralloc_array(mem_ctx, uint32_t, prog_data->nr_params);
679
680 /* We now set the param values to be offsets into a
681 * anv_push_constant_data structure. Since the compiler doesn't
682 * actually dereference any of the gl_constant_value pointers in the
683 * params array, it doesn't really matter what we put here.
684 */
685 struct anv_push_constants *null_data = NULL;
686 /* Fill out the push constants section of the param array */
687 for (unsigned i = 0; i < MAX_PUSH_CONSTANTS_SIZE / sizeof(float); i++) {
688 prog_data->param[i] = ANV_PARAM_PUSH(
689 (uintptr_t)&null_data->client_data[i * sizeof(float)]);
690 }
691 }
692
693 if (nir->info.num_ssbos > 0 || nir->info.num_images > 0)
694 pipeline->needs_data_cache = true;
695
696 NIR_PASS_V(nir, brw_nir_lower_image_load_store, compiler->devinfo);
697
698 NIR_PASS_V(nir, nir_lower_explicit_io, nir_var_mem_global,
699 nir_address_format_64bit_global);
700
701 /* Apply the actual pipeline layout to UBOs, SSBOs, and textures */
702 if (layout) {
703 anv_nir_apply_pipeline_layout(pdevice,
704 pipeline->device->robust_buffer_access,
705 layout, nir, prog_data,
706 &stage->bind_map);
707
708 NIR_PASS_V(nir, nir_lower_explicit_io, nir_var_mem_ubo,
709 nir_address_format_32bit_index_offset);
710 NIR_PASS_V(nir, nir_lower_explicit_io, nir_var_mem_ssbo,
711 anv_nir_ssbo_addr_format(pdevice,
712 pipeline->device->robust_buffer_access));
713
714 NIR_PASS_V(nir, nir_opt_constant_folding);
715
716 /* We don't support non-uniform UBOs and non-uniform SSBO access is
717 * handled naturally by falling back to A64 messages.
718 */
719 NIR_PASS_V(nir, nir_lower_non_uniform_access,
720 nir_lower_non_uniform_texture_access |
721 nir_lower_non_uniform_image_access);
722 }
723
724 if (nir->info.stage != MESA_SHADER_COMPUTE)
725 brw_nir_analyze_ubo_ranges(compiler, nir, NULL, prog_data->ubo_ranges);
726
727 assert(nir->num_uniforms == prog_data->nr_params * 4);
728
729 stage->nir = nir;
730 }
731
732 static void
733 anv_pipeline_link_vs(const struct brw_compiler *compiler,
734 struct anv_pipeline_stage *vs_stage,
735 struct anv_pipeline_stage *next_stage)
736 {
737 if (next_stage)
738 brw_nir_link_shaders(compiler, vs_stage->nir, next_stage->nir);
739 }
740
741 static void
742 anv_pipeline_compile_vs(const struct brw_compiler *compiler,
743 void *mem_ctx,
744 struct anv_device *device,
745 struct anv_pipeline_stage *vs_stage)
746 {
747 brw_compute_vue_map(compiler->devinfo,
748 &vs_stage->prog_data.vs.base.vue_map,
749 vs_stage->nir->info.outputs_written,
750 vs_stage->nir->info.separate_shader);
751
752 vs_stage->code = brw_compile_vs(compiler, device, mem_ctx,
753 &vs_stage->key.vs,
754 &vs_stage->prog_data.vs,
755 vs_stage->nir, -1, NULL, NULL);
756 }
757
758 static void
759 merge_tess_info(struct shader_info *tes_info,
760 const struct shader_info *tcs_info)
761 {
762 /* The Vulkan 1.0.38 spec, section 21.1 Tessellator says:
763 *
764 * "PointMode. Controls generation of points rather than triangles
765 * or lines. This functionality defaults to disabled, and is
766 * enabled if either shader stage includes the execution mode.
767 *
768 * and about Triangles, Quads, IsoLines, VertexOrderCw, VertexOrderCcw,
769 * PointMode, SpacingEqual, SpacingFractionalEven, SpacingFractionalOdd,
770 * and OutputVertices, it says:
771 *
772 * "One mode must be set in at least one of the tessellation
773 * shader stages."
774 *
775 * So, the fields can be set in either the TCS or TES, but they must
776 * agree if set in both. Our backend looks at TES, so bitwise-or in
777 * the values from the TCS.
778 */
779 assert(tcs_info->tess.tcs_vertices_out == 0 ||
780 tes_info->tess.tcs_vertices_out == 0 ||
781 tcs_info->tess.tcs_vertices_out == tes_info->tess.tcs_vertices_out);
782 tes_info->tess.tcs_vertices_out |= tcs_info->tess.tcs_vertices_out;
783
784 assert(tcs_info->tess.spacing == TESS_SPACING_UNSPECIFIED ||
785 tes_info->tess.spacing == TESS_SPACING_UNSPECIFIED ||
786 tcs_info->tess.spacing == tes_info->tess.spacing);
787 tes_info->tess.spacing |= tcs_info->tess.spacing;
788
789 assert(tcs_info->tess.primitive_mode == 0 ||
790 tes_info->tess.primitive_mode == 0 ||
791 tcs_info->tess.primitive_mode == tes_info->tess.primitive_mode);
792 tes_info->tess.primitive_mode |= tcs_info->tess.primitive_mode;
793 tes_info->tess.ccw |= tcs_info->tess.ccw;
794 tes_info->tess.point_mode |= tcs_info->tess.point_mode;
795 }
796
797 static void
798 anv_pipeline_link_tcs(const struct brw_compiler *compiler,
799 struct anv_pipeline_stage *tcs_stage,
800 struct anv_pipeline_stage *tes_stage)
801 {
802 assert(tes_stage && tes_stage->stage == MESA_SHADER_TESS_EVAL);
803
804 brw_nir_link_shaders(compiler, tcs_stage->nir, tes_stage->nir);
805
806 nir_lower_patch_vertices(tes_stage->nir,
807 tcs_stage->nir->info.tess.tcs_vertices_out,
808 NULL);
809
810 /* Copy TCS info into the TES info */
811 merge_tess_info(&tes_stage->nir->info, &tcs_stage->nir->info);
812
813 /* Whacking the key after cache lookup is a bit sketchy, but all of
814 * this comes from the SPIR-V, which is part of the hash used for the
815 * pipeline cache. So it should be safe.
816 */
817 tcs_stage->key.tcs.tes_primitive_mode =
818 tes_stage->nir->info.tess.primitive_mode;
819 tcs_stage->key.tcs.quads_workaround =
820 compiler->devinfo->gen < 9 &&
821 tes_stage->nir->info.tess.primitive_mode == 7 /* GL_QUADS */ &&
822 tes_stage->nir->info.tess.spacing == TESS_SPACING_EQUAL;
823 }
824
825 static void
826 anv_pipeline_compile_tcs(const struct brw_compiler *compiler,
827 void *mem_ctx,
828 struct anv_device *device,
829 struct anv_pipeline_stage *tcs_stage,
830 struct anv_pipeline_stage *prev_stage)
831 {
832 tcs_stage->key.tcs.outputs_written =
833 tcs_stage->nir->info.outputs_written;
834 tcs_stage->key.tcs.patch_outputs_written =
835 tcs_stage->nir->info.patch_outputs_written;
836
837 tcs_stage->code = brw_compile_tcs(compiler, device, mem_ctx,
838 &tcs_stage->key.tcs,
839 &tcs_stage->prog_data.tcs,
840 tcs_stage->nir, -1, NULL, NULL);
841 }
842
843 static void
844 anv_pipeline_link_tes(const struct brw_compiler *compiler,
845 struct anv_pipeline_stage *tes_stage,
846 struct anv_pipeline_stage *next_stage)
847 {
848 if (next_stage)
849 brw_nir_link_shaders(compiler, tes_stage->nir, next_stage->nir);
850 }
851
852 static void
853 anv_pipeline_compile_tes(const struct brw_compiler *compiler,
854 void *mem_ctx,
855 struct anv_device *device,
856 struct anv_pipeline_stage *tes_stage,
857 struct anv_pipeline_stage *tcs_stage)
858 {
859 tes_stage->key.tes.inputs_read =
860 tcs_stage->nir->info.outputs_written;
861 tes_stage->key.tes.patch_inputs_read =
862 tcs_stage->nir->info.patch_outputs_written;
863
864 tes_stage->code = brw_compile_tes(compiler, device, mem_ctx,
865 &tes_stage->key.tes,
866 &tcs_stage->prog_data.tcs.base.vue_map,
867 &tes_stage->prog_data.tes,
868 tes_stage->nir, NULL, -1, NULL, NULL);
869 }
870
871 static void
872 anv_pipeline_link_gs(const struct brw_compiler *compiler,
873 struct anv_pipeline_stage *gs_stage,
874 struct anv_pipeline_stage *next_stage)
875 {
876 if (next_stage)
877 brw_nir_link_shaders(compiler, gs_stage->nir, next_stage->nir);
878 }
879
880 static void
881 anv_pipeline_compile_gs(const struct brw_compiler *compiler,
882 void *mem_ctx,
883 struct anv_device *device,
884 struct anv_pipeline_stage *gs_stage,
885 struct anv_pipeline_stage *prev_stage)
886 {
887 brw_compute_vue_map(compiler->devinfo,
888 &gs_stage->prog_data.gs.base.vue_map,
889 gs_stage->nir->info.outputs_written,
890 gs_stage->nir->info.separate_shader);
891
892 gs_stage->code = brw_compile_gs(compiler, device, mem_ctx,
893 &gs_stage->key.gs,
894 &gs_stage->prog_data.gs,
895 gs_stage->nir, NULL, -1, NULL, NULL);
896 }
897
898 static void
899 anv_pipeline_link_fs(const struct brw_compiler *compiler,
900 struct anv_pipeline_stage *stage)
901 {
902 unsigned num_rts = 0;
903 const int max_rt = FRAG_RESULT_DATA7 - FRAG_RESULT_DATA0 + 1;
904 struct anv_pipeline_binding rt_bindings[max_rt];
905 nir_function_impl *impl = nir_shader_get_entrypoint(stage->nir);
906 int rt_to_bindings[max_rt];
907 memset(rt_to_bindings, -1, sizeof(rt_to_bindings));
908 bool rt_used[max_rt];
909 memset(rt_used, 0, sizeof(rt_used));
910
911 /* Flag used render targets */
912 nir_foreach_variable_safe(var, &stage->nir->outputs) {
913 if (var->data.location < FRAG_RESULT_DATA0)
914 continue;
915
916 const unsigned rt = var->data.location - FRAG_RESULT_DATA0;
917 /* Out-of-bounds */
918 if (rt >= MAX_RTS)
919 continue;
920
921 const unsigned array_len =
922 glsl_type_is_array(var->type) ? glsl_get_length(var->type) : 1;
923 assert(rt + array_len <= max_rt);
924
925 /* Unused */
926 if (!(stage->key.wm.color_outputs_valid & BITFIELD_RANGE(rt, array_len))) {
927 /* If this is the RT at location 0 and we have alpha to coverage
928 * enabled we will have to create a null RT for it, so mark it as
929 * used.
930 */
931 if (rt > 0 || !stage->key.wm.alpha_to_coverage)
932 continue;
933 }
934
935 for (unsigned i = 0; i < array_len; i++)
936 rt_used[rt + i] = true;
937 }
938
939 /* Set new, compacted, location */
940 for (unsigned i = 0; i < max_rt; i++) {
941 if (!rt_used[i])
942 continue;
943
944 rt_to_bindings[i] = num_rts;
945
946 if (stage->key.wm.color_outputs_valid & (1 << i)) {
947 rt_bindings[rt_to_bindings[i]] = (struct anv_pipeline_binding) {
948 .set = ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS,
949 .binding = 0,
950 .index = i,
951 };
952 } else {
953 /* Setup a null render target */
954 rt_bindings[rt_to_bindings[i]] = (struct anv_pipeline_binding) {
955 .set = ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS,
956 .binding = 0,
957 .index = UINT32_MAX,
958 };
959 }
960
961 num_rts++;
962 }
963
964 bool deleted_output = false;
965 nir_foreach_variable_safe(var, &stage->nir->outputs) {
966 if (var->data.location < FRAG_RESULT_DATA0)
967 continue;
968
969 const unsigned rt = var->data.location - FRAG_RESULT_DATA0;
970
971 if (rt >= MAX_RTS || !rt_used[rt]) {
972 /* Unused or out-of-bounds, throw it away, unless it is the first
973 * RT and we have alpha to coverage enabled.
974 */
975 deleted_output = true;
976 var->data.mode = nir_var_function_temp;
977 exec_node_remove(&var->node);
978 exec_list_push_tail(&impl->locals, &var->node);
979 continue;
980 }
981
982 /* Give it the new location */
983 assert(rt_to_bindings[rt] != -1);
984 var->data.location = rt_to_bindings[rt] + FRAG_RESULT_DATA0;
985 }
986
987 if (deleted_output)
988 nir_fixup_deref_modes(stage->nir);
989
990 if (num_rts == 0) {
991 /* If we have no render targets, we need a null render target */
992 rt_bindings[0] = (struct anv_pipeline_binding) {
993 .set = ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS,
994 .binding = 0,
995 .index = UINT32_MAX,
996 };
997 num_rts = 1;
998 }
999
1000 /* Now that we've determined the actual number of render targets, adjust
1001 * the key accordingly.
1002 */
1003 stage->key.wm.nr_color_regions = num_rts;
1004 stage->key.wm.color_outputs_valid = (1 << num_rts) - 1;
1005
1006 assert(num_rts <= max_rt);
1007 assert(stage->bind_map.surface_count == 0);
1008 typed_memcpy(stage->bind_map.surface_to_descriptor,
1009 rt_bindings, num_rts);
1010 stage->bind_map.surface_count += num_rts;
1011 }
1012
1013 static void
1014 anv_pipeline_compile_fs(const struct brw_compiler *compiler,
1015 void *mem_ctx,
1016 struct anv_device *device,
1017 struct anv_pipeline_stage *fs_stage,
1018 struct anv_pipeline_stage *prev_stage)
1019 {
1020 /* TODO: we could set this to 0 based on the information in nir_shader, but
1021 * we need this before we call spirv_to_nir.
1022 */
1023 assert(prev_stage);
1024 fs_stage->key.wm.input_slots_valid =
1025 prev_stage->prog_data.vue.vue_map.slots_valid;
1026
1027 fs_stage->code = brw_compile_fs(compiler, device, mem_ctx,
1028 &fs_stage->key.wm,
1029 &fs_stage->prog_data.wm,
1030 fs_stage->nir, NULL, -1, -1, -1,
1031 true, false, NULL, NULL, NULL);
1032
1033 if (fs_stage->key.wm.nr_color_regions == 0 &&
1034 !fs_stage->prog_data.wm.has_side_effects &&
1035 !fs_stage->prog_data.wm.uses_kill &&
1036 fs_stage->prog_data.wm.computed_depth_mode == BRW_PSCDEPTH_OFF &&
1037 !fs_stage->prog_data.wm.computed_stencil) {
1038 /* This fragment shader has no outputs and no side effects. Go ahead
1039 * and return the code pointer so we don't accidentally think the
1040 * compile failed but zero out prog_data which will set program_size to
1041 * zero and disable the stage.
1042 */
1043 memset(&fs_stage->prog_data, 0, sizeof(fs_stage->prog_data));
1044 }
1045 }
1046
1047 static VkResult
1048 anv_pipeline_compile_graphics(struct anv_pipeline *pipeline,
1049 struct anv_pipeline_cache *cache,
1050 const VkGraphicsPipelineCreateInfo *info)
1051 {
1052 VkPipelineCreationFeedbackEXT pipeline_feedback = {
1053 .flags = VK_PIPELINE_CREATION_FEEDBACK_VALID_BIT_EXT,
1054 };
1055 int64_t pipeline_start = os_time_get_nano();
1056
1057 const struct brw_compiler *compiler =
1058 pipeline->device->instance->physicalDevice.compiler;
1059 struct anv_pipeline_stage stages[MESA_SHADER_STAGES] = {};
1060
1061 pipeline->active_stages = 0;
1062
1063 VkResult result;
1064 for (uint32_t i = 0; i < info->stageCount; i++) {
1065 const VkPipelineShaderStageCreateInfo *sinfo = &info->pStages[i];
1066 gl_shader_stage stage = vk_to_mesa_shader_stage(sinfo->stage);
1067
1068 pipeline->active_stages |= sinfo->stage;
1069
1070 int64_t stage_start = os_time_get_nano();
1071
1072 stages[stage].stage = stage;
1073 stages[stage].module = anv_shader_module_from_handle(sinfo->module);
1074 stages[stage].entrypoint = sinfo->pName;
1075 stages[stage].spec_info = sinfo->pSpecializationInfo;
1076 anv_pipeline_hash_shader(stages[stage].module,
1077 stages[stage].entrypoint,
1078 stage,
1079 stages[stage].spec_info,
1080 stages[stage].shader_sha1);
1081
1082 const struct gen_device_info *devinfo = &pipeline->device->info;
1083 switch (stage) {
1084 case MESA_SHADER_VERTEX:
1085 populate_vs_prog_key(devinfo, sinfo->flags, &stages[stage].key.vs);
1086 break;
1087 case MESA_SHADER_TESS_CTRL:
1088 populate_tcs_prog_key(devinfo, sinfo->flags,
1089 info->pTessellationState->patchControlPoints,
1090 &stages[stage].key.tcs);
1091 break;
1092 case MESA_SHADER_TESS_EVAL:
1093 populate_tes_prog_key(devinfo, sinfo->flags, &stages[stage].key.tes);
1094 break;
1095 case MESA_SHADER_GEOMETRY:
1096 populate_gs_prog_key(devinfo, sinfo->flags, &stages[stage].key.gs);
1097 break;
1098 case MESA_SHADER_FRAGMENT:
1099 populate_wm_prog_key(devinfo, sinfo->flags,
1100 pipeline->subpass,
1101 info->pMultisampleState,
1102 &stages[stage].key.wm);
1103 break;
1104 default:
1105 unreachable("Invalid graphics shader stage");
1106 }
1107
1108 stages[stage].feedback.duration += os_time_get_nano() - stage_start;
1109 stages[stage].feedback.flags |= VK_PIPELINE_CREATION_FEEDBACK_VALID_BIT_EXT;
1110 }
1111
1112 if (pipeline->active_stages & VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT)
1113 pipeline->active_stages |= VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT;
1114
1115 assert(pipeline->active_stages & VK_SHADER_STAGE_VERTEX_BIT);
1116
1117 ANV_FROM_HANDLE(anv_pipeline_layout, layout, info->layout);
1118
1119 unsigned char sha1[20];
1120 anv_pipeline_hash_graphics(pipeline, layout, stages, sha1);
1121
1122 unsigned found = 0;
1123 unsigned cache_hits = 0;
1124 for (unsigned s = 0; s < MESA_SHADER_STAGES; s++) {
1125 if (!stages[s].entrypoint)
1126 continue;
1127
1128 int64_t stage_start = os_time_get_nano();
1129
1130 stages[s].cache_key.stage = s;
1131 memcpy(stages[s].cache_key.sha1, sha1, sizeof(sha1));
1132
1133 bool cache_hit;
1134 struct anv_shader_bin *bin =
1135 anv_device_search_for_kernel(pipeline->device, cache,
1136 &stages[s].cache_key,
1137 sizeof(stages[s].cache_key), &cache_hit);
1138 if (bin) {
1139 found++;
1140 pipeline->shaders[s] = bin;
1141 }
1142
1143 if (cache_hit) {
1144 cache_hits++;
1145 stages[s].feedback.flags |=
1146 VK_PIPELINE_CREATION_FEEDBACK_APPLICATION_PIPELINE_CACHE_HIT_BIT_EXT;
1147 }
1148 stages[s].feedback.duration += os_time_get_nano() - stage_start;
1149 }
1150
1151 if (found == __builtin_popcount(pipeline->active_stages)) {
1152 if (cache_hits == found) {
1153 pipeline_feedback.flags |=
1154 VK_PIPELINE_CREATION_FEEDBACK_APPLICATION_PIPELINE_CACHE_HIT_BIT_EXT;
1155 }
1156 /* We found all our shaders in the cache. We're done. */
1157 goto done;
1158 } else if (found > 0) {
1159 /* We found some but not all of our shaders. This shouldn't happen
1160 * most of the time but it can if we have a partially populated
1161 * pipeline cache.
1162 */
1163 assert(found < __builtin_popcount(pipeline->active_stages));
1164
1165 vk_debug_report(&pipeline->device->instance->debug_report_callbacks,
1166 VK_DEBUG_REPORT_WARNING_BIT_EXT |
1167 VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
1168 VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_CACHE_EXT,
1169 (uint64_t)(uintptr_t)cache,
1170 0, 0, "anv",
1171 "Found a partial pipeline in the cache. This is "
1172 "most likely caused by an incomplete pipeline cache "
1173 "import or export");
1174
1175 /* We're going to have to recompile anyway, so just throw away our
1176 * references to the shaders in the cache. We'll get them out of the
1177 * cache again as part of the compilation process.
1178 */
1179 for (unsigned s = 0; s < MESA_SHADER_STAGES; s++) {
1180 stages[s].feedback.flags = 0;
1181 if (pipeline->shaders[s]) {
1182 anv_shader_bin_unref(pipeline->device, pipeline->shaders[s]);
1183 pipeline->shaders[s] = NULL;
1184 }
1185 }
1186 }
1187
1188 void *pipeline_ctx = ralloc_context(NULL);
1189
1190 for (unsigned s = 0; s < MESA_SHADER_STAGES; s++) {
1191 if (!stages[s].entrypoint)
1192 continue;
1193
1194 int64_t stage_start = os_time_get_nano();
1195
1196 assert(stages[s].stage == s);
1197 assert(pipeline->shaders[s] == NULL);
1198
1199 stages[s].bind_map = (struct anv_pipeline_bind_map) {
1200 .surface_to_descriptor = stages[s].surface_to_descriptor,
1201 .sampler_to_descriptor = stages[s].sampler_to_descriptor
1202 };
1203
1204 stages[s].nir = anv_pipeline_stage_get_nir(pipeline, cache,
1205 pipeline_ctx,
1206 &stages[s]);
1207 if (stages[s].nir == NULL) {
1208 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1209 goto fail;
1210 }
1211
1212 stages[s].feedback.duration += os_time_get_nano() - stage_start;
1213 }
1214
1215 /* Walk backwards to link */
1216 struct anv_pipeline_stage *next_stage = NULL;
1217 for (int s = MESA_SHADER_STAGES - 1; s >= 0; s--) {
1218 if (!stages[s].entrypoint)
1219 continue;
1220
1221 switch (s) {
1222 case MESA_SHADER_VERTEX:
1223 anv_pipeline_link_vs(compiler, &stages[s], next_stage);
1224 break;
1225 case MESA_SHADER_TESS_CTRL:
1226 anv_pipeline_link_tcs(compiler, &stages[s], next_stage);
1227 break;
1228 case MESA_SHADER_TESS_EVAL:
1229 anv_pipeline_link_tes(compiler, &stages[s], next_stage);
1230 break;
1231 case MESA_SHADER_GEOMETRY:
1232 anv_pipeline_link_gs(compiler, &stages[s], next_stage);
1233 break;
1234 case MESA_SHADER_FRAGMENT:
1235 anv_pipeline_link_fs(compiler, &stages[s]);
1236 break;
1237 default:
1238 unreachable("Invalid graphics shader stage");
1239 }
1240
1241 next_stage = &stages[s];
1242 }
1243
1244 struct anv_pipeline_stage *prev_stage = NULL;
1245 for (unsigned s = 0; s < MESA_SHADER_STAGES; s++) {
1246 if (!stages[s].entrypoint)
1247 continue;
1248
1249 int64_t stage_start = os_time_get_nano();
1250
1251 void *stage_ctx = ralloc_context(NULL);
1252
1253 nir_xfb_info *xfb_info = NULL;
1254 if (s == MESA_SHADER_VERTEX ||
1255 s == MESA_SHADER_TESS_EVAL ||
1256 s == MESA_SHADER_GEOMETRY)
1257 xfb_info = nir_gather_xfb_info(stages[s].nir, stage_ctx);
1258
1259 anv_pipeline_lower_nir(pipeline, stage_ctx, &stages[s], layout);
1260
1261 switch (s) {
1262 case MESA_SHADER_VERTEX:
1263 anv_pipeline_compile_vs(compiler, stage_ctx, pipeline->device,
1264 &stages[s]);
1265 break;
1266 case MESA_SHADER_TESS_CTRL:
1267 anv_pipeline_compile_tcs(compiler, stage_ctx, pipeline->device,
1268 &stages[s], prev_stage);
1269 break;
1270 case MESA_SHADER_TESS_EVAL:
1271 anv_pipeline_compile_tes(compiler, stage_ctx, pipeline->device,
1272 &stages[s], prev_stage);
1273 break;
1274 case MESA_SHADER_GEOMETRY:
1275 anv_pipeline_compile_gs(compiler, stage_ctx, pipeline->device,
1276 &stages[s], prev_stage);
1277 break;
1278 case MESA_SHADER_FRAGMENT:
1279 anv_pipeline_compile_fs(compiler, stage_ctx, pipeline->device,
1280 &stages[s], prev_stage);
1281 break;
1282 default:
1283 unreachable("Invalid graphics shader stage");
1284 }
1285 if (stages[s].code == NULL) {
1286 ralloc_free(stage_ctx);
1287 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1288 goto fail;
1289 }
1290
1291 struct anv_shader_bin *bin =
1292 anv_device_upload_kernel(pipeline->device, cache,
1293 &stages[s].cache_key,
1294 sizeof(stages[s].cache_key),
1295 stages[s].code,
1296 stages[s].prog_data.base.program_size,
1297 stages[s].nir->constant_data,
1298 stages[s].nir->constant_data_size,
1299 &stages[s].prog_data.base,
1300 brw_prog_data_size(s),
1301 xfb_info, &stages[s].bind_map);
1302 if (!bin) {
1303 ralloc_free(stage_ctx);
1304 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1305 goto fail;
1306 }
1307
1308 pipeline->shaders[s] = bin;
1309 ralloc_free(stage_ctx);
1310
1311 stages[s].feedback.duration += os_time_get_nano() - stage_start;
1312
1313 prev_stage = &stages[s];
1314 }
1315
1316 ralloc_free(pipeline_ctx);
1317
1318 done:
1319
1320 if (pipeline->shaders[MESA_SHADER_FRAGMENT] &&
1321 pipeline->shaders[MESA_SHADER_FRAGMENT]->prog_data->program_size == 0) {
1322 /* This can happen if we decided to implicitly disable the fragment
1323 * shader. See anv_pipeline_compile_fs().
1324 */
1325 anv_shader_bin_unref(pipeline->device,
1326 pipeline->shaders[MESA_SHADER_FRAGMENT]);
1327 pipeline->shaders[MESA_SHADER_FRAGMENT] = NULL;
1328 pipeline->active_stages &= ~VK_SHADER_STAGE_FRAGMENT_BIT;
1329 }
1330
1331 pipeline_feedback.duration = os_time_get_nano() - pipeline_start;
1332
1333 const VkPipelineCreationFeedbackCreateInfoEXT *create_feedback =
1334 vk_find_struct_const(info->pNext, PIPELINE_CREATION_FEEDBACK_CREATE_INFO_EXT);
1335 if (create_feedback) {
1336 *create_feedback->pPipelineCreationFeedback = pipeline_feedback;
1337
1338 assert(info->stageCount == create_feedback->pipelineStageCreationFeedbackCount);
1339 for (uint32_t i = 0; i < info->stageCount; i++) {
1340 gl_shader_stage s = vk_to_mesa_shader_stage(info->pStages[i].stage);
1341 create_feedback->pPipelineStageCreationFeedbacks[i] = stages[s].feedback;
1342 }
1343 }
1344
1345 return VK_SUCCESS;
1346
1347 fail:
1348 ralloc_free(pipeline_ctx);
1349
1350 for (unsigned s = 0; s < MESA_SHADER_STAGES; s++) {
1351 if (pipeline->shaders[s])
1352 anv_shader_bin_unref(pipeline->device, pipeline->shaders[s]);
1353 }
1354
1355 return result;
1356 }
1357
1358 static void
1359 shared_type_info(const struct glsl_type *type, unsigned *size, unsigned *align)
1360 {
1361 assert(glsl_type_is_vector_or_scalar(type));
1362
1363 uint32_t comp_size = glsl_type_is_boolean(type)
1364 ? 4 : glsl_get_bit_size(type) / 8;
1365 unsigned length = glsl_get_vector_elements(type);
1366 *size = comp_size * length,
1367 *align = comp_size * (length == 3 ? 4 : length);
1368 }
1369
1370 VkResult
1371 anv_pipeline_compile_cs(struct anv_pipeline *pipeline,
1372 struct anv_pipeline_cache *cache,
1373 const VkComputePipelineCreateInfo *info,
1374 const struct anv_shader_module *module,
1375 const char *entrypoint,
1376 const VkSpecializationInfo *spec_info)
1377 {
1378 VkPipelineCreationFeedbackEXT pipeline_feedback = {
1379 .flags = VK_PIPELINE_CREATION_FEEDBACK_VALID_BIT_EXT,
1380 };
1381 int64_t pipeline_start = os_time_get_nano();
1382
1383 const struct brw_compiler *compiler =
1384 pipeline->device->instance->physicalDevice.compiler;
1385
1386 struct anv_pipeline_stage stage = {
1387 .stage = MESA_SHADER_COMPUTE,
1388 .module = module,
1389 .entrypoint = entrypoint,
1390 .spec_info = spec_info,
1391 .cache_key = {
1392 .stage = MESA_SHADER_COMPUTE,
1393 },
1394 .feedback = {
1395 .flags = VK_PIPELINE_CREATION_FEEDBACK_VALID_BIT_EXT,
1396 },
1397 };
1398 anv_pipeline_hash_shader(stage.module,
1399 stage.entrypoint,
1400 MESA_SHADER_COMPUTE,
1401 stage.spec_info,
1402 stage.shader_sha1);
1403
1404 struct anv_shader_bin *bin = NULL;
1405
1406 const VkPipelineShaderStageRequiredSubgroupSizeCreateInfoEXT *rss_info =
1407 vk_find_struct_const(info->stage.pNext,
1408 PIPELINE_SHADER_STAGE_REQUIRED_SUBGROUP_SIZE_CREATE_INFO_EXT);
1409
1410 populate_cs_prog_key(&pipeline->device->info, info->stage.flags,
1411 rss_info, &stage.key.cs);
1412
1413 ANV_FROM_HANDLE(anv_pipeline_layout, layout, info->layout);
1414
1415 anv_pipeline_hash_compute(pipeline, layout, &stage, stage.cache_key.sha1);
1416 bool cache_hit;
1417 bin = anv_device_search_for_kernel(pipeline->device, cache, &stage.cache_key,
1418 sizeof(stage.cache_key), &cache_hit);
1419
1420 if (bin == NULL) {
1421 int64_t stage_start = os_time_get_nano();
1422
1423 stage.bind_map = (struct anv_pipeline_bind_map) {
1424 .surface_to_descriptor = stage.surface_to_descriptor,
1425 .sampler_to_descriptor = stage.sampler_to_descriptor
1426 };
1427
1428 /* Set up a binding for the gl_NumWorkGroups */
1429 stage.bind_map.surface_count = 1;
1430 stage.bind_map.surface_to_descriptor[0] = (struct anv_pipeline_binding) {
1431 .set = ANV_DESCRIPTOR_SET_NUM_WORK_GROUPS,
1432 };
1433
1434 void *mem_ctx = ralloc_context(NULL);
1435
1436 stage.nir = anv_pipeline_stage_get_nir(pipeline, cache, mem_ctx, &stage);
1437 if (stage.nir == NULL) {
1438 ralloc_free(mem_ctx);
1439 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1440 }
1441
1442 anv_pipeline_lower_nir(pipeline, mem_ctx, &stage, layout);
1443
1444 NIR_PASS_V(stage.nir, anv_nir_add_base_work_group_id,
1445 &stage.prog_data.cs);
1446
1447 NIR_PASS_V(stage.nir, nir_lower_vars_to_explicit_types,
1448 nir_var_mem_shared, shared_type_info);
1449 NIR_PASS_V(stage.nir, nir_lower_explicit_io,
1450 nir_var_mem_shared, nir_address_format_32bit_offset);
1451
1452 stage.code = brw_compile_cs(compiler, pipeline->device, mem_ctx,
1453 &stage.key.cs, &stage.prog_data.cs,
1454 stage.nir, -1, NULL, NULL);
1455 if (stage.code == NULL) {
1456 ralloc_free(mem_ctx);
1457 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1458 }
1459
1460 const unsigned code_size = stage.prog_data.base.program_size;
1461 bin = anv_device_upload_kernel(pipeline->device, cache,
1462 &stage.cache_key, sizeof(stage.cache_key),
1463 stage.code, code_size,
1464 stage.nir->constant_data,
1465 stage.nir->constant_data_size,
1466 &stage.prog_data.base,
1467 sizeof(stage.prog_data.cs),
1468 NULL, &stage.bind_map);
1469 if (!bin) {
1470 ralloc_free(mem_ctx);
1471 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1472 }
1473
1474 ralloc_free(mem_ctx);
1475
1476 stage.feedback.duration = os_time_get_nano() - stage_start;
1477 }
1478
1479 if (cache_hit) {
1480 stage.feedback.flags |=
1481 VK_PIPELINE_CREATION_FEEDBACK_APPLICATION_PIPELINE_CACHE_HIT_BIT_EXT;
1482 pipeline_feedback.flags |=
1483 VK_PIPELINE_CREATION_FEEDBACK_APPLICATION_PIPELINE_CACHE_HIT_BIT_EXT;
1484 }
1485 pipeline_feedback.duration = os_time_get_nano() - pipeline_start;
1486
1487 const VkPipelineCreationFeedbackCreateInfoEXT *create_feedback =
1488 vk_find_struct_const(info->pNext, PIPELINE_CREATION_FEEDBACK_CREATE_INFO_EXT);
1489 if (create_feedback) {
1490 *create_feedback->pPipelineCreationFeedback = pipeline_feedback;
1491
1492 assert(create_feedback->pipelineStageCreationFeedbackCount == 1);
1493 create_feedback->pPipelineStageCreationFeedbacks[0] = stage.feedback;
1494 }
1495
1496 pipeline->active_stages = VK_SHADER_STAGE_COMPUTE_BIT;
1497 pipeline->shaders[MESA_SHADER_COMPUTE] = bin;
1498
1499 return VK_SUCCESS;
1500 }
1501
1502 /**
1503 * Copy pipeline state not marked as dynamic.
1504 * Dynamic state is pipeline state which hasn't been provided at pipeline
1505 * creation time, but is dynamically provided afterwards using various
1506 * vkCmdSet* functions.
1507 *
1508 * The set of state considered "non_dynamic" is determined by the pieces of
1509 * state that have their corresponding VkDynamicState enums omitted from
1510 * VkPipelineDynamicStateCreateInfo::pDynamicStates.
1511 *
1512 * @param[out] pipeline Destination non_dynamic state.
1513 * @param[in] pCreateInfo Source of non_dynamic state to be copied.
1514 */
1515 static void
1516 copy_non_dynamic_state(struct anv_pipeline *pipeline,
1517 const VkGraphicsPipelineCreateInfo *pCreateInfo)
1518 {
1519 anv_cmd_dirty_mask_t states = ANV_CMD_DIRTY_DYNAMIC_ALL;
1520 struct anv_subpass *subpass = pipeline->subpass;
1521
1522 pipeline->dynamic_state = default_dynamic_state;
1523
1524 if (pCreateInfo->pDynamicState) {
1525 /* Remove all of the states that are marked as dynamic */
1526 uint32_t count = pCreateInfo->pDynamicState->dynamicStateCount;
1527 for (uint32_t s = 0; s < count; s++) {
1528 states &= ~anv_cmd_dirty_bit_for_vk_dynamic_state(
1529 pCreateInfo->pDynamicState->pDynamicStates[s]);
1530 }
1531 }
1532
1533 struct anv_dynamic_state *dynamic = &pipeline->dynamic_state;
1534
1535 /* Section 9.2 of the Vulkan 1.0.15 spec says:
1536 *
1537 * pViewportState is [...] NULL if the pipeline
1538 * has rasterization disabled.
1539 */
1540 if (!pCreateInfo->pRasterizationState->rasterizerDiscardEnable) {
1541 assert(pCreateInfo->pViewportState);
1542
1543 dynamic->viewport.count = pCreateInfo->pViewportState->viewportCount;
1544 if (states & ANV_CMD_DIRTY_DYNAMIC_VIEWPORT) {
1545 typed_memcpy(dynamic->viewport.viewports,
1546 pCreateInfo->pViewportState->pViewports,
1547 pCreateInfo->pViewportState->viewportCount);
1548 }
1549
1550 dynamic->scissor.count = pCreateInfo->pViewportState->scissorCount;
1551 if (states & ANV_CMD_DIRTY_DYNAMIC_SCISSOR) {
1552 typed_memcpy(dynamic->scissor.scissors,
1553 pCreateInfo->pViewportState->pScissors,
1554 pCreateInfo->pViewportState->scissorCount);
1555 }
1556 }
1557
1558 if (states & ANV_CMD_DIRTY_DYNAMIC_LINE_WIDTH) {
1559 assert(pCreateInfo->pRasterizationState);
1560 dynamic->line_width = pCreateInfo->pRasterizationState->lineWidth;
1561 }
1562
1563 if (states & ANV_CMD_DIRTY_DYNAMIC_DEPTH_BIAS) {
1564 assert(pCreateInfo->pRasterizationState);
1565 dynamic->depth_bias.bias =
1566 pCreateInfo->pRasterizationState->depthBiasConstantFactor;
1567 dynamic->depth_bias.clamp =
1568 pCreateInfo->pRasterizationState->depthBiasClamp;
1569 dynamic->depth_bias.slope =
1570 pCreateInfo->pRasterizationState->depthBiasSlopeFactor;
1571 }
1572
1573 /* Section 9.2 of the Vulkan 1.0.15 spec says:
1574 *
1575 * pColorBlendState is [...] NULL if the pipeline has rasterization
1576 * disabled or if the subpass of the render pass the pipeline is
1577 * created against does not use any color attachments.
1578 */
1579 bool uses_color_att = false;
1580 for (unsigned i = 0; i < subpass->color_count; ++i) {
1581 if (subpass->color_attachments[i].attachment != VK_ATTACHMENT_UNUSED) {
1582 uses_color_att = true;
1583 break;
1584 }
1585 }
1586
1587 if (uses_color_att &&
1588 !pCreateInfo->pRasterizationState->rasterizerDiscardEnable) {
1589 assert(pCreateInfo->pColorBlendState);
1590
1591 if (states & ANV_CMD_DIRTY_DYNAMIC_BLEND_CONSTANTS)
1592 typed_memcpy(dynamic->blend_constants,
1593 pCreateInfo->pColorBlendState->blendConstants, 4);
1594 }
1595
1596 /* If there is no depthstencil attachment, then don't read
1597 * pDepthStencilState. The Vulkan spec states that pDepthStencilState may
1598 * be NULL in this case. Even if pDepthStencilState is non-NULL, there is
1599 * no need to override the depthstencil defaults in
1600 * anv_pipeline::dynamic_state when there is no depthstencil attachment.
1601 *
1602 * Section 9.2 of the Vulkan 1.0.15 spec says:
1603 *
1604 * pDepthStencilState is [...] NULL if the pipeline has rasterization
1605 * disabled or if the subpass of the render pass the pipeline is created
1606 * against does not use a depth/stencil attachment.
1607 */
1608 if (!pCreateInfo->pRasterizationState->rasterizerDiscardEnable &&
1609 subpass->depth_stencil_attachment) {
1610 assert(pCreateInfo->pDepthStencilState);
1611
1612 if (states & ANV_CMD_DIRTY_DYNAMIC_DEPTH_BOUNDS) {
1613 dynamic->depth_bounds.min =
1614 pCreateInfo->pDepthStencilState->minDepthBounds;
1615 dynamic->depth_bounds.max =
1616 pCreateInfo->pDepthStencilState->maxDepthBounds;
1617 }
1618
1619 if (states & ANV_CMD_DIRTY_DYNAMIC_STENCIL_COMPARE_MASK) {
1620 dynamic->stencil_compare_mask.front =
1621 pCreateInfo->pDepthStencilState->front.compareMask;
1622 dynamic->stencil_compare_mask.back =
1623 pCreateInfo->pDepthStencilState->back.compareMask;
1624 }
1625
1626 if (states & ANV_CMD_DIRTY_DYNAMIC_STENCIL_WRITE_MASK) {
1627 dynamic->stencil_write_mask.front =
1628 pCreateInfo->pDepthStencilState->front.writeMask;
1629 dynamic->stencil_write_mask.back =
1630 pCreateInfo->pDepthStencilState->back.writeMask;
1631 }
1632
1633 if (states & ANV_CMD_DIRTY_DYNAMIC_STENCIL_REFERENCE) {
1634 dynamic->stencil_reference.front =
1635 pCreateInfo->pDepthStencilState->front.reference;
1636 dynamic->stencil_reference.back =
1637 pCreateInfo->pDepthStencilState->back.reference;
1638 }
1639 }
1640
1641 const VkPipelineRasterizationLineStateCreateInfoEXT *line_state =
1642 vk_find_struct_const(pCreateInfo->pRasterizationState->pNext,
1643 PIPELINE_RASTERIZATION_LINE_STATE_CREATE_INFO_EXT);
1644 if (line_state) {
1645 if (states & ANV_CMD_DIRTY_DYNAMIC_LINE_STIPPLE) {
1646 dynamic->line_stipple.factor = line_state->lineStippleFactor;
1647 dynamic->line_stipple.pattern = line_state->lineStipplePattern;
1648 }
1649 }
1650
1651 pipeline->dynamic_state_mask = states;
1652 }
1653
1654 static void
1655 anv_pipeline_validate_create_info(const VkGraphicsPipelineCreateInfo *info)
1656 {
1657 #ifdef DEBUG
1658 struct anv_render_pass *renderpass = NULL;
1659 struct anv_subpass *subpass = NULL;
1660
1661 /* Assert that all required members of VkGraphicsPipelineCreateInfo are
1662 * present. See the Vulkan 1.0.28 spec, Section 9.2 Graphics Pipelines.
1663 */
1664 assert(info->sType == VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO);
1665
1666 renderpass = anv_render_pass_from_handle(info->renderPass);
1667 assert(renderpass);
1668
1669 assert(info->subpass < renderpass->subpass_count);
1670 subpass = &renderpass->subpasses[info->subpass];
1671
1672 assert(info->stageCount >= 1);
1673 assert(info->pVertexInputState);
1674 assert(info->pInputAssemblyState);
1675 assert(info->pRasterizationState);
1676 if (!info->pRasterizationState->rasterizerDiscardEnable) {
1677 assert(info->pViewportState);
1678 assert(info->pMultisampleState);
1679
1680 if (subpass && subpass->depth_stencil_attachment)
1681 assert(info->pDepthStencilState);
1682
1683 if (subpass && subpass->color_count > 0) {
1684 bool all_color_unused = true;
1685 for (int i = 0; i < subpass->color_count; i++) {
1686 if (subpass->color_attachments[i].attachment != VK_ATTACHMENT_UNUSED)
1687 all_color_unused = false;
1688 }
1689 /* pColorBlendState is ignored if the pipeline has rasterization
1690 * disabled or if the subpass of the render pass the pipeline is
1691 * created against does not use any color attachments.
1692 */
1693 assert(info->pColorBlendState || all_color_unused);
1694 }
1695 }
1696
1697 for (uint32_t i = 0; i < info->stageCount; ++i) {
1698 switch (info->pStages[i].stage) {
1699 case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT:
1700 case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT:
1701 assert(info->pTessellationState);
1702 break;
1703 default:
1704 break;
1705 }
1706 }
1707 #endif
1708 }
1709
1710 /**
1711 * Calculate the desired L3 partitioning based on the current state of the
1712 * pipeline. For now this simply returns the conservative defaults calculated
1713 * by get_default_l3_weights(), but we could probably do better by gathering
1714 * more statistics from the pipeline state (e.g. guess of expected URB usage
1715 * and bound surfaces), or by using feed-back from performance counters.
1716 */
1717 void
1718 anv_pipeline_setup_l3_config(struct anv_pipeline *pipeline, bool needs_slm)
1719 {
1720 const struct gen_device_info *devinfo = &pipeline->device->info;
1721
1722 const struct gen_l3_weights w =
1723 gen_get_default_l3_weights(devinfo, pipeline->needs_data_cache, needs_slm);
1724
1725 pipeline->urb.l3_config = gen_get_l3_config(devinfo, w);
1726 pipeline->urb.total_size =
1727 gen_get_l3_config_urb_size(devinfo, pipeline->urb.l3_config);
1728 }
1729
1730 VkResult
1731 anv_pipeline_init(struct anv_pipeline *pipeline,
1732 struct anv_device *device,
1733 struct anv_pipeline_cache *cache,
1734 const VkGraphicsPipelineCreateInfo *pCreateInfo,
1735 const VkAllocationCallbacks *alloc)
1736 {
1737 VkResult result;
1738
1739 anv_pipeline_validate_create_info(pCreateInfo);
1740
1741 if (alloc == NULL)
1742 alloc = &device->alloc;
1743
1744 pipeline->device = device;
1745
1746 ANV_FROM_HANDLE(anv_render_pass, render_pass, pCreateInfo->renderPass);
1747 assert(pCreateInfo->subpass < render_pass->subpass_count);
1748 pipeline->subpass = &render_pass->subpasses[pCreateInfo->subpass];
1749
1750 result = anv_reloc_list_init(&pipeline->batch_relocs, alloc);
1751 if (result != VK_SUCCESS)
1752 return result;
1753
1754 pipeline->batch.alloc = alloc;
1755 pipeline->batch.next = pipeline->batch.start = pipeline->batch_data;
1756 pipeline->batch.end = pipeline->batch.start + sizeof(pipeline->batch_data);
1757 pipeline->batch.relocs = &pipeline->batch_relocs;
1758 pipeline->batch.status = VK_SUCCESS;
1759
1760 copy_non_dynamic_state(pipeline, pCreateInfo);
1761 pipeline->depth_clamp_enable = pCreateInfo->pRasterizationState &&
1762 pCreateInfo->pRasterizationState->depthClampEnable;
1763
1764 /* Previously we enabled depth clipping when !depthClampEnable.
1765 * DepthClipStateCreateInfo now makes depth clipping explicit so if the
1766 * clipping info is available, use its enable value to determine clipping,
1767 * otherwise fallback to the previous !depthClampEnable logic.
1768 */
1769 const VkPipelineRasterizationDepthClipStateCreateInfoEXT *clip_info =
1770 vk_find_struct_const(pCreateInfo->pRasterizationState->pNext,
1771 PIPELINE_RASTERIZATION_DEPTH_CLIP_STATE_CREATE_INFO_EXT);
1772 pipeline->depth_clip_enable = clip_info ? clip_info->depthClipEnable : !pipeline->depth_clamp_enable;
1773
1774 pipeline->sample_shading_enable = pCreateInfo->pMultisampleState &&
1775 pCreateInfo->pMultisampleState->sampleShadingEnable;
1776
1777 pipeline->needs_data_cache = false;
1778
1779 /* When we free the pipeline, we detect stages based on the NULL status
1780 * of various prog_data pointers. Make them NULL by default.
1781 */
1782 memset(pipeline->shaders, 0, sizeof(pipeline->shaders));
1783
1784 result = anv_pipeline_compile_graphics(pipeline, cache, pCreateInfo);
1785 if (result != VK_SUCCESS) {
1786 anv_reloc_list_finish(&pipeline->batch_relocs, alloc);
1787 return result;
1788 }
1789
1790 assert(pipeline->shaders[MESA_SHADER_VERTEX]);
1791
1792 anv_pipeline_setup_l3_config(pipeline, false);
1793
1794 const VkPipelineVertexInputStateCreateInfo *vi_info =
1795 pCreateInfo->pVertexInputState;
1796
1797 const uint64_t inputs_read = get_vs_prog_data(pipeline)->inputs_read;
1798
1799 pipeline->vb_used = 0;
1800 for (uint32_t i = 0; i < vi_info->vertexAttributeDescriptionCount; i++) {
1801 const VkVertexInputAttributeDescription *desc =
1802 &vi_info->pVertexAttributeDescriptions[i];
1803
1804 if (inputs_read & (1ull << (VERT_ATTRIB_GENERIC0 + desc->location)))
1805 pipeline->vb_used |= 1 << desc->binding;
1806 }
1807
1808 for (uint32_t i = 0; i < vi_info->vertexBindingDescriptionCount; i++) {
1809 const VkVertexInputBindingDescription *desc =
1810 &vi_info->pVertexBindingDescriptions[i];
1811
1812 pipeline->vb[desc->binding].stride = desc->stride;
1813
1814 /* Step rate is programmed per vertex element (attribute), not
1815 * binding. Set up a map of which bindings step per instance, for
1816 * reference by vertex element setup. */
1817 switch (desc->inputRate) {
1818 default:
1819 case VK_VERTEX_INPUT_RATE_VERTEX:
1820 pipeline->vb[desc->binding].instanced = false;
1821 break;
1822 case VK_VERTEX_INPUT_RATE_INSTANCE:
1823 pipeline->vb[desc->binding].instanced = true;
1824 break;
1825 }
1826
1827 pipeline->vb[desc->binding].instance_divisor = 1;
1828 }
1829
1830 const VkPipelineVertexInputDivisorStateCreateInfoEXT *vi_div_state =
1831 vk_find_struct_const(vi_info->pNext,
1832 PIPELINE_VERTEX_INPUT_DIVISOR_STATE_CREATE_INFO_EXT);
1833 if (vi_div_state) {
1834 for (uint32_t i = 0; i < vi_div_state->vertexBindingDivisorCount; i++) {
1835 const VkVertexInputBindingDivisorDescriptionEXT *desc =
1836 &vi_div_state->pVertexBindingDivisors[i];
1837
1838 pipeline->vb[desc->binding].instance_divisor = desc->divisor;
1839 }
1840 }
1841
1842 /* Our implementation of VK_KHR_multiview uses instancing to draw the
1843 * different views. If the client asks for instancing, we need to multiply
1844 * the instance divisor by the number of views ensure that we repeat the
1845 * client's per-instance data once for each view.
1846 */
1847 if (pipeline->subpass->view_mask) {
1848 const uint32_t view_count = anv_subpass_view_count(pipeline->subpass);
1849 for (uint32_t vb = 0; vb < MAX_VBS; vb++) {
1850 if (pipeline->vb[vb].instanced)
1851 pipeline->vb[vb].instance_divisor *= view_count;
1852 }
1853 }
1854
1855 const VkPipelineInputAssemblyStateCreateInfo *ia_info =
1856 pCreateInfo->pInputAssemblyState;
1857 const VkPipelineTessellationStateCreateInfo *tess_info =
1858 pCreateInfo->pTessellationState;
1859 pipeline->primitive_restart = ia_info->primitiveRestartEnable;
1860
1861 if (anv_pipeline_has_stage(pipeline, MESA_SHADER_TESS_EVAL))
1862 pipeline->topology = _3DPRIM_PATCHLIST(tess_info->patchControlPoints);
1863 else
1864 pipeline->topology = vk_to_gen_primitive_type[ia_info->topology];
1865
1866 return VK_SUCCESS;
1867 }