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