intel/compiler: Account for built-in uniforms in analyze_ubo_ranges
[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 "common/gen_l3_config.h"
32 #include "anv_private.h"
33 #include "compiler/brw_nir.h"
34 #include "anv_nir.h"
35 #include "spirv/nir_spirv.h"
36 #include "vk_util.h"
37
38 /* Needed for SWIZZLE macros */
39 #include "program/prog_instruction.h"
40
41 // Shader functions
42
43 VkResult anv_CreateShaderModule(
44 VkDevice _device,
45 const VkShaderModuleCreateInfo* pCreateInfo,
46 const VkAllocationCallbacks* pAllocator,
47 VkShaderModule* pShaderModule)
48 {
49 ANV_FROM_HANDLE(anv_device, device, _device);
50 struct anv_shader_module *module;
51
52 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO);
53 assert(pCreateInfo->flags == 0);
54
55 module = vk_alloc2(&device->alloc, pAllocator,
56 sizeof(*module) + pCreateInfo->codeSize, 8,
57 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
58 if (module == NULL)
59 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
60
61 module->size = pCreateInfo->codeSize;
62 memcpy(module->data, pCreateInfo->pCode, module->size);
63
64 _mesa_sha1_compute(module->data, module->size, module->sha1);
65
66 *pShaderModule = anv_shader_module_to_handle(module);
67
68 return VK_SUCCESS;
69 }
70
71 void anv_DestroyShaderModule(
72 VkDevice _device,
73 VkShaderModule _module,
74 const VkAllocationCallbacks* pAllocator)
75 {
76 ANV_FROM_HANDLE(anv_device, device, _device);
77 ANV_FROM_HANDLE(anv_shader_module, module, _module);
78
79 if (!module)
80 return;
81
82 vk_free2(&device->alloc, pAllocator, module);
83 }
84
85 #define SPIR_V_MAGIC_NUMBER 0x07230203
86
87 static const uint64_t stage_to_debug[] = {
88 [MESA_SHADER_VERTEX] = DEBUG_VS,
89 [MESA_SHADER_TESS_CTRL] = DEBUG_TCS,
90 [MESA_SHADER_TESS_EVAL] = DEBUG_TES,
91 [MESA_SHADER_GEOMETRY] = DEBUG_GS,
92 [MESA_SHADER_FRAGMENT] = DEBUG_WM,
93 [MESA_SHADER_COMPUTE] = DEBUG_CS,
94 };
95
96 /* Eventually, this will become part of anv_CreateShader. Unfortunately,
97 * we can't do that yet because we don't have the ability to copy nir.
98 */
99 static nir_shader *
100 anv_shader_compile_to_nir(struct anv_pipeline *pipeline,
101 void *mem_ctx,
102 struct anv_shader_module *module,
103 const char *entrypoint_name,
104 gl_shader_stage stage,
105 const VkSpecializationInfo *spec_info)
106 {
107 const struct anv_device *device = pipeline->device;
108
109 const struct brw_compiler *compiler =
110 device->instance->physicalDevice.compiler;
111 const nir_shader_compiler_options *nir_options =
112 compiler->glsl_compiler_options[stage].NirOptions;
113
114 uint32_t *spirv = (uint32_t *) module->data;
115 assert(spirv[0] == SPIR_V_MAGIC_NUMBER);
116 assert(module->size % 4 == 0);
117
118 uint32_t num_spec_entries = 0;
119 struct nir_spirv_specialization *spec_entries = NULL;
120 if (spec_info && spec_info->mapEntryCount > 0) {
121 num_spec_entries = spec_info->mapEntryCount;
122 spec_entries = malloc(num_spec_entries * sizeof(*spec_entries));
123 for (uint32_t i = 0; i < num_spec_entries; i++) {
124 VkSpecializationMapEntry entry = spec_info->pMapEntries[i];
125 const void *data = spec_info->pData + entry.offset;
126 assert(data + entry.size <= spec_info->pData + spec_info->dataSize);
127
128 spec_entries[i].id = spec_info->pMapEntries[i].constantID;
129 if (spec_info->dataSize == 8)
130 spec_entries[i].data64 = *(const uint64_t *)data;
131 else
132 spec_entries[i].data32 = *(const uint32_t *)data;
133 }
134 }
135
136 struct spirv_to_nir_options spirv_options = {
137 .lower_workgroup_access_to_offsets = true,
138 .caps = {
139 .float64 = device->instance->physicalDevice.info.gen >= 8,
140 .int64 = device->instance->physicalDevice.info.gen >= 8,
141 .tessellation = true,
142 .device_group = true,
143 .draw_parameters = true,
144 .image_write_without_format = true,
145 .multiview = true,
146 .variable_pointers = true,
147 .storage_16bit = device->instance->physicalDevice.info.gen >= 8,
148 .int16 = device->instance->physicalDevice.info.gen >= 8,
149 .shader_viewport_index_layer = true,
150 .subgroup_arithmetic = true,
151 .subgroup_basic = true,
152 .subgroup_ballot = true,
153 .subgroup_quad = true,
154 .subgroup_shuffle = true,
155 .subgroup_vote = true,
156 .stencil_export = device->instance->physicalDevice.info.gen >= 9,
157 .storage_8bit = device->instance->physicalDevice.info.gen >= 8,
158 .post_depth_coverage = device->instance->physicalDevice.info.gen >= 9,
159 },
160 };
161
162 nir_function *entry_point =
163 spirv_to_nir(spirv, module->size / 4,
164 spec_entries, num_spec_entries,
165 stage, entrypoint_name, &spirv_options, nir_options);
166 nir_shader *nir = entry_point->shader;
167 assert(nir->info.stage == stage);
168 nir_validate_shader(nir);
169 ralloc_steal(mem_ctx, nir);
170
171 free(spec_entries);
172
173 if (unlikely(INTEL_DEBUG & stage_to_debug[stage])) {
174 fprintf(stderr, "NIR (from SPIR-V) for %s shader:\n",
175 gl_shader_stage_name(stage));
176 nir_print_shader(nir, stderr);
177 }
178
179 /* We have to lower away local constant initializers right before we
180 * inline functions. That way they get properly initialized at the top
181 * of the function and not at the top of its caller.
182 */
183 NIR_PASS_V(nir, nir_lower_constant_initializers, nir_var_local);
184 NIR_PASS_V(nir, nir_lower_returns);
185 NIR_PASS_V(nir, nir_inline_functions);
186 NIR_PASS_V(nir, nir_copy_prop);
187
188 /* Pick off the single entrypoint that we want */
189 foreach_list_typed_safe(nir_function, func, node, &nir->functions) {
190 if (func != entry_point)
191 exec_node_remove(&func->node);
192 }
193 assert(exec_list_length(&nir->functions) == 1);
194 entry_point->name = ralloc_strdup(entry_point, "main");
195
196 /* Now that we've deleted all but the main function, we can go ahead and
197 * lower the rest of the constant initializers. We do this here so that
198 * nir_remove_dead_variables and split_per_member_structs below see the
199 * corresponding stores.
200 */
201 NIR_PASS_V(nir, nir_lower_constant_initializers, ~0);
202
203 /* Split member structs. We do this before lower_io_to_temporaries so that
204 * it doesn't lower system values to temporaries by accident.
205 */
206 NIR_PASS_V(nir, nir_split_var_copies);
207 NIR_PASS_V(nir, nir_split_per_member_structs);
208
209 NIR_PASS_V(nir, nir_remove_dead_variables,
210 nir_var_shader_in | nir_var_shader_out | nir_var_system_value);
211
212 if (stage == MESA_SHADER_FRAGMENT)
213 NIR_PASS_V(nir, nir_lower_wpos_center, pipeline->sample_shading_enable);
214
215 NIR_PASS_V(nir, nir_propagate_invariant);
216 NIR_PASS_V(nir, nir_lower_io_to_temporaries,
217 entry_point->impl, true, false);
218
219 /* Vulkan uses the separate-shader linking model */
220 nir->info.separate_shader = true;
221
222 nir = brw_preprocess_nir(compiler, nir);
223
224 if (stage == MESA_SHADER_FRAGMENT)
225 NIR_PASS_V(nir, anv_nir_lower_input_attachments);
226
227 return nir;
228 }
229
230 void anv_DestroyPipeline(
231 VkDevice _device,
232 VkPipeline _pipeline,
233 const VkAllocationCallbacks* pAllocator)
234 {
235 ANV_FROM_HANDLE(anv_device, device, _device);
236 ANV_FROM_HANDLE(anv_pipeline, pipeline, _pipeline);
237
238 if (!pipeline)
239 return;
240
241 anv_reloc_list_finish(&pipeline->batch_relocs,
242 pAllocator ? pAllocator : &device->alloc);
243 if (pipeline->blend_state.map)
244 anv_state_pool_free(&device->dynamic_state_pool, pipeline->blend_state);
245
246 for (unsigned s = 0; s < MESA_SHADER_STAGES; s++) {
247 if (pipeline->shaders[s])
248 anv_shader_bin_unref(device, pipeline->shaders[s]);
249 }
250
251 vk_free2(&device->alloc, pAllocator, pipeline);
252 }
253
254 static const uint32_t vk_to_gen_primitive_type[] = {
255 [VK_PRIMITIVE_TOPOLOGY_POINT_LIST] = _3DPRIM_POINTLIST,
256 [VK_PRIMITIVE_TOPOLOGY_LINE_LIST] = _3DPRIM_LINELIST,
257 [VK_PRIMITIVE_TOPOLOGY_LINE_STRIP] = _3DPRIM_LINESTRIP,
258 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST] = _3DPRIM_TRILIST,
259 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP] = _3DPRIM_TRISTRIP,
260 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN] = _3DPRIM_TRIFAN,
261 [VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY] = _3DPRIM_LINELIST_ADJ,
262 [VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY] = _3DPRIM_LINESTRIP_ADJ,
263 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY] = _3DPRIM_TRILIST_ADJ,
264 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY] = _3DPRIM_TRISTRIP_ADJ,
265 };
266
267 static void
268 populate_sampler_prog_key(const struct gen_device_info *devinfo,
269 struct brw_sampler_prog_key_data *key)
270 {
271 /* Almost all multisampled textures are compressed. The only time when we
272 * don't compress a multisampled texture is for 16x MSAA with a surface
273 * width greater than 8k which is a bit of an edge case. Since the sampler
274 * just ignores the MCS parameter to ld2ms when MCS is disabled, it's safe
275 * to tell the compiler to always assume compression.
276 */
277 key->compressed_multisample_layout_mask = ~0;
278
279 /* SkyLake added support for 16x MSAA. With this came a new message for
280 * reading from a 16x MSAA surface with compression. The new message was
281 * needed because now the MCS data is 64 bits instead of 32 or lower as is
282 * the case for 8x, 4x, and 2x. The key->msaa_16 bit-field controls which
283 * message we use. Fortunately, the 16x message works for 8x, 4x, and 2x
284 * so we can just use it unconditionally. This may not be quite as
285 * efficient but it saves us from recompiling.
286 */
287 if (devinfo->gen >= 9)
288 key->msaa_16 = ~0;
289
290 /* XXX: Handle texture swizzle on HSW- */
291 for (int i = 0; i < MAX_SAMPLERS; i++) {
292 /* Assume color sampler, no swizzling. (Works for BDW+) */
293 key->swizzles[i] = SWIZZLE_XYZW;
294 }
295 }
296
297 static void
298 populate_vs_prog_key(const struct gen_device_info *devinfo,
299 struct brw_vs_prog_key *key)
300 {
301 memset(key, 0, sizeof(*key));
302
303 populate_sampler_prog_key(devinfo, &key->tex);
304
305 /* XXX: Handle vertex input work-arounds */
306
307 /* XXX: Handle sampler_prog_key */
308 }
309
310 static void
311 populate_gs_prog_key(const struct gen_device_info *devinfo,
312 struct brw_gs_prog_key *key)
313 {
314 memset(key, 0, sizeof(*key));
315
316 populate_sampler_prog_key(devinfo, &key->tex);
317 }
318
319 static void
320 populate_wm_prog_key(const struct anv_pipeline *pipeline,
321 const VkGraphicsPipelineCreateInfo *info,
322 struct brw_wm_prog_key *key)
323 {
324 const struct gen_device_info *devinfo = &pipeline->device->info;
325
326 memset(key, 0, sizeof(*key));
327
328 populate_sampler_prog_key(devinfo, &key->tex);
329
330 /* TODO: we could set this to 0 based on the information in nir_shader, but
331 * this function is called before spirv_to_nir. */
332 const struct brw_vue_map *vue_map =
333 &anv_pipeline_get_last_vue_prog_data(pipeline)->vue_map;
334 key->input_slots_valid = vue_map->slots_valid;
335
336 /* Vulkan doesn't specify a default */
337 key->high_quality_derivatives = false;
338
339 /* XXX Vulkan doesn't appear to specify */
340 key->clamp_fragment_color = false;
341
342 key->nr_color_regions = pipeline->subpass->color_count;
343
344 key->replicate_alpha = key->nr_color_regions > 1 &&
345 info->pMultisampleState &&
346 info->pMultisampleState->alphaToCoverageEnable;
347
348 if (info->pMultisampleState) {
349 /* We should probably pull this out of the shader, but it's fairly
350 * harmless to compute it and then let dead-code take care of it.
351 */
352 if (info->pMultisampleState->rasterizationSamples > 1) {
353 key->persample_interp =
354 (info->pMultisampleState->minSampleShading *
355 info->pMultisampleState->rasterizationSamples) > 1;
356 key->multisample_fbo = true;
357 }
358
359 key->frag_coord_adds_sample_pos =
360 info->pMultisampleState->sampleShadingEnable;
361 }
362 }
363
364 static void
365 populate_cs_prog_key(const struct gen_device_info *devinfo,
366 struct brw_cs_prog_key *key)
367 {
368 memset(key, 0, sizeof(*key));
369
370 populate_sampler_prog_key(devinfo, &key->tex);
371 }
372
373 static void
374 anv_pipeline_hash_shader(struct anv_pipeline *pipeline,
375 struct anv_pipeline_layout *layout,
376 struct anv_shader_module *module,
377 const char *entrypoint,
378 gl_shader_stage stage,
379 const VkSpecializationInfo *spec_info,
380 const void *key, size_t key_size,
381 unsigned char *sha1_out)
382 {
383 struct mesa_sha1 ctx;
384
385 _mesa_sha1_init(&ctx);
386 if (stage != MESA_SHADER_COMPUTE) {
387 _mesa_sha1_update(&ctx, &pipeline->subpass->view_mask,
388 sizeof(pipeline->subpass->view_mask));
389 }
390 if (layout)
391 _mesa_sha1_update(&ctx, layout->sha1, sizeof(layout->sha1));
392 _mesa_sha1_update(&ctx, module->sha1, sizeof(module->sha1));
393 _mesa_sha1_update(&ctx, entrypoint, strlen(entrypoint));
394 _mesa_sha1_update(&ctx, &stage, sizeof(stage));
395 if (spec_info) {
396 _mesa_sha1_update(&ctx, spec_info->pMapEntries,
397 spec_info->mapEntryCount * sizeof(*spec_info->pMapEntries));
398 _mesa_sha1_update(&ctx, spec_info->pData, spec_info->dataSize);
399 }
400 _mesa_sha1_update(&ctx, key, key_size);
401 _mesa_sha1_final(&ctx, sha1_out);
402 }
403
404 static nir_shader *
405 anv_pipeline_compile(struct anv_pipeline *pipeline,
406 void *mem_ctx,
407 struct anv_pipeline_layout *layout,
408 struct anv_shader_module *module,
409 const char *entrypoint,
410 gl_shader_stage stage,
411 const VkSpecializationInfo *spec_info,
412 struct brw_stage_prog_data *prog_data,
413 struct anv_pipeline_bind_map *map)
414 {
415 const struct brw_compiler *compiler =
416 pipeline->device->instance->physicalDevice.compiler;
417
418 nir_shader *nir = anv_shader_compile_to_nir(pipeline, mem_ctx,
419 module, entrypoint, stage,
420 spec_info);
421 if (nir == NULL)
422 return NULL;
423
424 NIR_PASS_V(nir, anv_nir_lower_ycbcr_textures, layout);
425
426 NIR_PASS_V(nir, anv_nir_lower_push_constants);
427
428 if (stage != MESA_SHADER_COMPUTE)
429 NIR_PASS_V(nir, anv_nir_lower_multiview, pipeline->subpass->view_mask);
430
431 if (stage == MESA_SHADER_COMPUTE)
432 prog_data->total_shared = nir->num_shared;
433
434 nir_shader_gather_info(nir, nir_shader_get_entrypoint(nir));
435
436 if (nir->num_uniforms > 0) {
437 assert(prog_data->nr_params == 0);
438
439 /* If the shader uses any push constants at all, we'll just give
440 * them the maximum possible number
441 */
442 assert(nir->num_uniforms <= MAX_PUSH_CONSTANTS_SIZE);
443 nir->num_uniforms = MAX_PUSH_CONSTANTS_SIZE;
444 prog_data->nr_params += MAX_PUSH_CONSTANTS_SIZE / sizeof(float);
445 prog_data->param = ralloc_array(mem_ctx, uint32_t, prog_data->nr_params);
446
447 /* We now set the param values to be offsets into a
448 * anv_push_constant_data structure. Since the compiler doesn't
449 * actually dereference any of the gl_constant_value pointers in the
450 * params array, it doesn't really matter what we put here.
451 */
452 struct anv_push_constants *null_data = NULL;
453 /* Fill out the push constants section of the param array */
454 for (unsigned i = 0; i < MAX_PUSH_CONSTANTS_SIZE / sizeof(float); i++) {
455 prog_data->param[i] = ANV_PARAM_PUSH(
456 (uintptr_t)&null_data->client_data[i * sizeof(float)]);
457 }
458 }
459
460 if (nir->info.num_ssbos > 0 || nir->info.num_images > 0)
461 pipeline->needs_data_cache = true;
462
463 /* Apply the actual pipeline layout to UBOs, SSBOs, and textures */
464 if (layout)
465 anv_nir_apply_pipeline_layout(pipeline, layout, nir, prog_data, map);
466
467 if (stage != MESA_SHADER_COMPUTE)
468 brw_nir_analyze_ubo_ranges(compiler, nir, NULL, prog_data->ubo_ranges);
469
470 assert(nir->num_uniforms == prog_data->nr_params * 4);
471
472 return nir;
473 }
474
475 static void
476 anv_fill_binding_table(struct brw_stage_prog_data *prog_data, unsigned bias)
477 {
478 prog_data->binding_table.size_bytes = 0;
479 prog_data->binding_table.texture_start = bias;
480 prog_data->binding_table.gather_texture_start = bias;
481 prog_data->binding_table.ubo_start = bias;
482 prog_data->binding_table.ssbo_start = bias;
483 prog_data->binding_table.image_start = bias;
484 }
485
486 static void
487 anv_pipeline_add_compiled_stage(struct anv_pipeline *pipeline,
488 gl_shader_stage stage,
489 struct anv_shader_bin *shader)
490 {
491 pipeline->shaders[stage] = shader;
492 }
493
494 static VkResult
495 anv_pipeline_compile_vs(struct anv_pipeline *pipeline,
496 struct anv_pipeline_cache *cache,
497 const VkGraphicsPipelineCreateInfo *info,
498 struct anv_shader_module *module,
499 const char *entrypoint,
500 const VkSpecializationInfo *spec_info)
501 {
502 const struct brw_compiler *compiler =
503 pipeline->device->instance->physicalDevice.compiler;
504 struct brw_vs_prog_key key;
505 struct anv_shader_bin *bin = NULL;
506
507 populate_vs_prog_key(&pipeline->device->info, &key);
508
509 ANV_FROM_HANDLE(anv_pipeline_layout, layout, info->layout);
510
511 unsigned char sha1[20];
512 anv_pipeline_hash_shader(pipeline, layout, module, entrypoint,
513 MESA_SHADER_VERTEX, spec_info,
514 &key, sizeof(key), sha1);
515 bin = anv_device_search_for_kernel(pipeline->device, cache, sha1, 20);
516
517 if (bin == NULL) {
518 struct brw_vs_prog_data prog_data = {};
519 struct anv_pipeline_binding surface_to_descriptor[256];
520 struct anv_pipeline_binding sampler_to_descriptor[256];
521
522 struct anv_pipeline_bind_map map = {
523 .surface_to_descriptor = surface_to_descriptor,
524 .sampler_to_descriptor = sampler_to_descriptor
525 };
526
527 void *mem_ctx = ralloc_context(NULL);
528
529 nir_shader *nir = anv_pipeline_compile(pipeline, mem_ctx, layout,
530 module, entrypoint,
531 MESA_SHADER_VERTEX, spec_info,
532 &prog_data.base.base, &map);
533 if (nir == NULL) {
534 ralloc_free(mem_ctx);
535 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
536 }
537
538 anv_fill_binding_table(&prog_data.base.base, 0);
539
540 brw_compute_vue_map(&pipeline->device->info,
541 &prog_data.base.vue_map,
542 nir->info.outputs_written,
543 nir->info.separate_shader);
544
545 const unsigned *shader_code =
546 brw_compile_vs(compiler, NULL, mem_ctx, &key, &prog_data, nir,
547 -1, NULL);
548 if (shader_code == NULL) {
549 ralloc_free(mem_ctx);
550 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
551 }
552
553 unsigned code_size = prog_data.base.base.program_size;
554 bin = anv_device_upload_kernel(pipeline->device, cache, sha1, 20,
555 shader_code, code_size,
556 nir->constant_data,
557 nir->constant_data_size,
558 &prog_data.base.base, sizeof(prog_data),
559 &map);
560 if (!bin) {
561 ralloc_free(mem_ctx);
562 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
563 }
564
565 ralloc_free(mem_ctx);
566 }
567
568 anv_pipeline_add_compiled_stage(pipeline, MESA_SHADER_VERTEX, bin);
569
570 return VK_SUCCESS;
571 }
572
573 static void
574 merge_tess_info(struct shader_info *tes_info,
575 const struct shader_info *tcs_info)
576 {
577 /* The Vulkan 1.0.38 spec, section 21.1 Tessellator says:
578 *
579 * "PointMode. Controls generation of points rather than triangles
580 * or lines. This functionality defaults to disabled, and is
581 * enabled if either shader stage includes the execution mode.
582 *
583 * and about Triangles, Quads, IsoLines, VertexOrderCw, VertexOrderCcw,
584 * PointMode, SpacingEqual, SpacingFractionalEven, SpacingFractionalOdd,
585 * and OutputVertices, it says:
586 *
587 * "One mode must be set in at least one of the tessellation
588 * shader stages."
589 *
590 * So, the fields can be set in either the TCS or TES, but they must
591 * agree if set in both. Our backend looks at TES, so bitwise-or in
592 * the values from the TCS.
593 */
594 assert(tcs_info->tess.tcs_vertices_out == 0 ||
595 tes_info->tess.tcs_vertices_out == 0 ||
596 tcs_info->tess.tcs_vertices_out == tes_info->tess.tcs_vertices_out);
597 tes_info->tess.tcs_vertices_out |= tcs_info->tess.tcs_vertices_out;
598
599 assert(tcs_info->tess.spacing == TESS_SPACING_UNSPECIFIED ||
600 tes_info->tess.spacing == TESS_SPACING_UNSPECIFIED ||
601 tcs_info->tess.spacing == tes_info->tess.spacing);
602 tes_info->tess.spacing |= tcs_info->tess.spacing;
603
604 assert(tcs_info->tess.primitive_mode == 0 ||
605 tes_info->tess.primitive_mode == 0 ||
606 tcs_info->tess.primitive_mode == tes_info->tess.primitive_mode);
607 tes_info->tess.primitive_mode |= tcs_info->tess.primitive_mode;
608 tes_info->tess.ccw |= tcs_info->tess.ccw;
609 tes_info->tess.point_mode |= tcs_info->tess.point_mode;
610 }
611
612 static VkResult
613 anv_pipeline_compile_tcs_tes(struct anv_pipeline *pipeline,
614 struct anv_pipeline_cache *cache,
615 const VkGraphicsPipelineCreateInfo *info,
616 struct anv_shader_module *tcs_module,
617 const char *tcs_entrypoint,
618 const VkSpecializationInfo *tcs_spec_info,
619 struct anv_shader_module *tes_module,
620 const char *tes_entrypoint,
621 const VkSpecializationInfo *tes_spec_info)
622 {
623 const struct gen_device_info *devinfo = &pipeline->device->info;
624 const struct brw_compiler *compiler =
625 pipeline->device->instance->physicalDevice.compiler;
626 struct brw_tcs_prog_key tcs_key = {};
627 struct brw_tes_prog_key tes_key = {};
628 struct anv_shader_bin *tcs_bin = NULL;
629 struct anv_shader_bin *tes_bin = NULL;
630
631 populate_sampler_prog_key(&pipeline->device->info, &tcs_key.tex);
632 populate_sampler_prog_key(&pipeline->device->info, &tes_key.tex);
633 tcs_key.input_vertices = info->pTessellationState->patchControlPoints;
634
635 ANV_FROM_HANDLE(anv_pipeline_layout, layout, info->layout);
636
637 unsigned char tcs_sha1[40];
638 unsigned char tes_sha1[40];
639 anv_pipeline_hash_shader(pipeline, layout, tcs_module, tcs_entrypoint,
640 MESA_SHADER_TESS_CTRL, tcs_spec_info,
641 &tcs_key, sizeof(tcs_key), tcs_sha1);
642 anv_pipeline_hash_shader(pipeline, layout, tes_module, tes_entrypoint,
643 MESA_SHADER_TESS_EVAL, tes_spec_info,
644 &tes_key, sizeof(tes_key), tes_sha1);
645 memcpy(&tcs_sha1[20], tes_sha1, 20);
646 memcpy(&tes_sha1[20], tcs_sha1, 20);
647
648 tcs_bin = anv_device_search_for_kernel(pipeline->device, cache,
649 tcs_sha1, sizeof(tcs_sha1));
650 tes_bin = anv_device_search_for_kernel(pipeline->device, cache,
651 tes_sha1, sizeof(tes_sha1));
652
653 if (tcs_bin == NULL || tes_bin == NULL) {
654 struct brw_tcs_prog_data tcs_prog_data = {};
655 struct brw_tes_prog_data tes_prog_data = {};
656 struct anv_pipeline_binding tcs_surface_to_descriptor[256];
657 struct anv_pipeline_binding tcs_sampler_to_descriptor[256];
658 struct anv_pipeline_binding tes_surface_to_descriptor[256];
659 struct anv_pipeline_binding tes_sampler_to_descriptor[256];
660
661 struct anv_pipeline_bind_map tcs_map = {
662 .surface_to_descriptor = tcs_surface_to_descriptor,
663 .sampler_to_descriptor = tcs_sampler_to_descriptor
664 };
665 struct anv_pipeline_bind_map tes_map = {
666 .surface_to_descriptor = tes_surface_to_descriptor,
667 .sampler_to_descriptor = tes_sampler_to_descriptor
668 };
669
670 void *mem_ctx = ralloc_context(NULL);
671
672 nir_shader *tcs_nir =
673 anv_pipeline_compile(pipeline, mem_ctx, layout,
674 tcs_module, tcs_entrypoint,
675 MESA_SHADER_TESS_CTRL, tcs_spec_info,
676 &tcs_prog_data.base.base, &tcs_map);
677 nir_shader *tes_nir =
678 anv_pipeline_compile(pipeline, mem_ctx, layout,
679 tes_module, tes_entrypoint,
680 MESA_SHADER_TESS_EVAL, tes_spec_info,
681 &tes_prog_data.base.base, &tes_map);
682 if (tcs_nir == NULL || tes_nir == NULL) {
683 ralloc_free(mem_ctx);
684 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
685 }
686
687 nir_lower_tes_patch_vertices(tes_nir,
688 tcs_nir->info.tess.tcs_vertices_out);
689
690 /* Copy TCS info into the TES info */
691 merge_tess_info(&tes_nir->info, &tcs_nir->info);
692
693 anv_fill_binding_table(&tcs_prog_data.base.base, 0);
694 anv_fill_binding_table(&tes_prog_data.base.base, 0);
695
696 /* Whacking the key after cache lookup is a bit sketchy, but all of
697 * this comes from the SPIR-V, which is part of the hash used for the
698 * pipeline cache. So it should be safe.
699 */
700 tcs_key.tes_primitive_mode = tes_nir->info.tess.primitive_mode;
701 tcs_key.outputs_written = tcs_nir->info.outputs_written;
702 tcs_key.patch_outputs_written = tcs_nir->info.patch_outputs_written;
703 tcs_key.quads_workaround =
704 devinfo->gen < 9 &&
705 tes_nir->info.tess.primitive_mode == 7 /* GL_QUADS */ &&
706 tes_nir->info.tess.spacing == TESS_SPACING_EQUAL;
707
708 tes_key.inputs_read = tcs_key.outputs_written;
709 tes_key.patch_inputs_read = tcs_key.patch_outputs_written;
710
711 const int shader_time_index = -1;
712 const unsigned *shader_code;
713
714 shader_code =
715 brw_compile_tcs(compiler, NULL, mem_ctx, &tcs_key, &tcs_prog_data,
716 tcs_nir, shader_time_index, NULL);
717 if (shader_code == NULL) {
718 ralloc_free(mem_ctx);
719 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
720 }
721
722 unsigned code_size = tcs_prog_data.base.base.program_size;
723 tcs_bin = anv_device_upload_kernel(pipeline->device, cache,
724 tcs_sha1, sizeof(tcs_sha1),
725 shader_code, code_size,
726 tcs_nir->constant_data,
727 tcs_nir->constant_data_size,
728 &tcs_prog_data.base.base,
729 sizeof(tcs_prog_data),
730 &tcs_map);
731 if (!tcs_bin) {
732 ralloc_free(mem_ctx);
733 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
734 }
735
736 shader_code =
737 brw_compile_tes(compiler, NULL, mem_ctx, &tes_key,
738 &tcs_prog_data.base.vue_map, &tes_prog_data, tes_nir,
739 NULL, shader_time_index, NULL);
740 if (shader_code == NULL) {
741 ralloc_free(mem_ctx);
742 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
743 }
744
745 code_size = tes_prog_data.base.base.program_size;
746 tes_bin = anv_device_upload_kernel(pipeline->device, cache,
747 tes_sha1, sizeof(tes_sha1),
748 shader_code, code_size,
749 tes_nir->constant_data,
750 tes_nir->constant_data_size,
751 &tes_prog_data.base.base,
752 sizeof(tes_prog_data),
753 &tes_map);
754 if (!tes_bin) {
755 ralloc_free(mem_ctx);
756 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
757 }
758
759 ralloc_free(mem_ctx);
760 }
761
762 anv_pipeline_add_compiled_stage(pipeline, MESA_SHADER_TESS_CTRL, tcs_bin);
763 anv_pipeline_add_compiled_stage(pipeline, MESA_SHADER_TESS_EVAL, tes_bin);
764
765 return VK_SUCCESS;
766 }
767
768 static VkResult
769 anv_pipeline_compile_gs(struct anv_pipeline *pipeline,
770 struct anv_pipeline_cache *cache,
771 const VkGraphicsPipelineCreateInfo *info,
772 struct anv_shader_module *module,
773 const char *entrypoint,
774 const VkSpecializationInfo *spec_info)
775 {
776 const struct brw_compiler *compiler =
777 pipeline->device->instance->physicalDevice.compiler;
778 struct brw_gs_prog_key key;
779 struct anv_shader_bin *bin = NULL;
780
781 populate_gs_prog_key(&pipeline->device->info, &key);
782
783 ANV_FROM_HANDLE(anv_pipeline_layout, layout, info->layout);
784
785 unsigned char sha1[20];
786 anv_pipeline_hash_shader(pipeline, layout, module, entrypoint,
787 MESA_SHADER_GEOMETRY, spec_info,
788 &key, sizeof(key), sha1);
789 bin = anv_device_search_for_kernel(pipeline->device, cache, sha1, 20);
790
791 if (bin == NULL) {
792 struct brw_gs_prog_data prog_data = {};
793 struct anv_pipeline_binding surface_to_descriptor[256];
794 struct anv_pipeline_binding sampler_to_descriptor[256];
795
796 struct anv_pipeline_bind_map map = {
797 .surface_to_descriptor = surface_to_descriptor,
798 .sampler_to_descriptor = sampler_to_descriptor
799 };
800
801 void *mem_ctx = ralloc_context(NULL);
802
803 nir_shader *nir = anv_pipeline_compile(pipeline, mem_ctx, layout,
804 module, entrypoint,
805 MESA_SHADER_GEOMETRY, spec_info,
806 &prog_data.base.base, &map);
807 if (nir == NULL) {
808 ralloc_free(mem_ctx);
809 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
810 }
811
812 anv_fill_binding_table(&prog_data.base.base, 0);
813
814 brw_compute_vue_map(&pipeline->device->info,
815 &prog_data.base.vue_map,
816 nir->info.outputs_written,
817 nir->info.separate_shader);
818
819 const unsigned *shader_code =
820 brw_compile_gs(compiler, NULL, mem_ctx, &key, &prog_data, nir,
821 NULL, -1, NULL);
822 if (shader_code == NULL) {
823 ralloc_free(mem_ctx);
824 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
825 }
826
827 /* TODO: SIMD8 GS */
828 const unsigned code_size = prog_data.base.base.program_size;
829 bin = anv_device_upload_kernel(pipeline->device, cache, sha1, 20,
830 shader_code, code_size,
831 nir->constant_data,
832 nir->constant_data_size,
833 &prog_data.base.base, sizeof(prog_data),
834 &map);
835 if (!bin) {
836 ralloc_free(mem_ctx);
837 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
838 }
839
840 ralloc_free(mem_ctx);
841 }
842
843 anv_pipeline_add_compiled_stage(pipeline, MESA_SHADER_GEOMETRY, bin);
844
845 return VK_SUCCESS;
846 }
847
848 static VkResult
849 anv_pipeline_compile_fs(struct anv_pipeline *pipeline,
850 struct anv_pipeline_cache *cache,
851 const VkGraphicsPipelineCreateInfo *info,
852 struct anv_shader_module *module,
853 const char *entrypoint,
854 const VkSpecializationInfo *spec_info)
855 {
856 const struct brw_compiler *compiler =
857 pipeline->device->instance->physicalDevice.compiler;
858 struct brw_wm_prog_key key;
859 struct anv_shader_bin *bin = NULL;
860
861 populate_wm_prog_key(pipeline, info, &key);
862
863 ANV_FROM_HANDLE(anv_pipeline_layout, layout, info->layout);
864
865 unsigned char sha1[20];
866 anv_pipeline_hash_shader(pipeline, layout, module, entrypoint,
867 MESA_SHADER_FRAGMENT, spec_info,
868 &key, sizeof(key), sha1);
869 bin = anv_device_search_for_kernel(pipeline->device, cache, sha1, 20);
870
871 if (bin == NULL) {
872 struct brw_wm_prog_data prog_data = {};
873 struct anv_pipeline_binding surface_to_descriptor[256];
874 struct anv_pipeline_binding sampler_to_descriptor[256];
875
876 struct anv_pipeline_bind_map map = {
877 .surface_to_descriptor = surface_to_descriptor + 8,
878 .sampler_to_descriptor = sampler_to_descriptor
879 };
880
881 void *mem_ctx = ralloc_context(NULL);
882
883 nir_shader *nir = anv_pipeline_compile(pipeline, mem_ctx, layout,
884 module, entrypoint,
885 MESA_SHADER_FRAGMENT, spec_info,
886 &prog_data.base, &map);
887 if (nir == NULL) {
888 ralloc_free(mem_ctx);
889 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
890 }
891
892 unsigned num_rts = 0;
893 const int max_rt = FRAG_RESULT_DATA7 - FRAG_RESULT_DATA0 + 1;
894 struct anv_pipeline_binding rt_bindings[max_rt];
895 nir_function_impl *impl = nir_shader_get_entrypoint(nir);
896 int rt_to_bindings[max_rt];
897 memset(rt_to_bindings, -1, sizeof(rt_to_bindings));
898 bool rt_used[max_rt];
899 memset(rt_used, 0, sizeof(rt_used));
900
901 /* Flag used render targets */
902 nir_foreach_variable_safe(var, &nir->outputs) {
903 if (var->data.location < FRAG_RESULT_DATA0)
904 continue;
905
906 const unsigned rt = var->data.location - FRAG_RESULT_DATA0;
907 /* Out-of-bounds */
908 if (rt >= key.nr_color_regions)
909 continue;
910
911 const unsigned array_len =
912 glsl_type_is_array(var->type) ? glsl_get_length(var->type) : 1;
913 assert(rt + array_len <= max_rt);
914
915 for (unsigned i = 0; i < array_len; i++)
916 rt_used[rt + i] = true;
917 }
918
919 /* Set new, compacted, location */
920 for (unsigned i = 0; i < max_rt; i++) {
921 if (!rt_used[i])
922 continue;
923
924 rt_to_bindings[i] = num_rts;
925 rt_bindings[rt_to_bindings[i]] = (struct anv_pipeline_binding) {
926 .set = ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS,
927 .binding = 0,
928 .index = i,
929 };
930 num_rts++;
931 }
932
933 nir_foreach_variable_safe(var, &nir->outputs) {
934 if (var->data.location < FRAG_RESULT_DATA0)
935 continue;
936
937 const unsigned rt = var->data.location - FRAG_RESULT_DATA0;
938 if (rt >= key.nr_color_regions) {
939 /* Out-of-bounds, throw it away */
940 var->data.mode = nir_var_local;
941 exec_node_remove(&var->node);
942 exec_list_push_tail(&impl->locals, &var->node);
943 continue;
944 }
945
946 /* Give it the new location */
947 assert(rt_to_bindings[rt] != -1);
948 var->data.location = rt_to_bindings[rt] + FRAG_RESULT_DATA0;
949 }
950
951 if (num_rts == 0) {
952 /* If we have no render targets, we need a null render target */
953 rt_bindings[0] = (struct anv_pipeline_binding) {
954 .set = ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS,
955 .binding = 0,
956 .index = UINT32_MAX,
957 };
958 num_rts = 1;
959 }
960
961 assert(num_rts <= max_rt);
962 map.surface_to_descriptor -= num_rts;
963 map.surface_count += num_rts;
964 assert(map.surface_count <= 256);
965 memcpy(map.surface_to_descriptor, rt_bindings,
966 num_rts * sizeof(*rt_bindings));
967
968 anv_fill_binding_table(&prog_data.base, num_rts);
969
970 const unsigned *shader_code =
971 brw_compile_fs(compiler, NULL, mem_ctx, &key, &prog_data, nir,
972 NULL, -1, -1, -1, true, false, NULL, NULL);
973 if (shader_code == NULL) {
974 ralloc_free(mem_ctx);
975 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
976 }
977
978 unsigned code_size = prog_data.base.program_size;
979 bin = anv_device_upload_kernel(pipeline->device, cache, sha1, 20,
980 shader_code, code_size,
981 nir->constant_data,
982 nir->constant_data_size,
983 &prog_data.base, sizeof(prog_data),
984 &map);
985 if (!bin) {
986 ralloc_free(mem_ctx);
987 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
988 }
989
990 ralloc_free(mem_ctx);
991 }
992
993 anv_pipeline_add_compiled_stage(pipeline, MESA_SHADER_FRAGMENT, bin);
994
995 return VK_SUCCESS;
996 }
997
998 VkResult
999 anv_pipeline_compile_cs(struct anv_pipeline *pipeline,
1000 struct anv_pipeline_cache *cache,
1001 const VkComputePipelineCreateInfo *info,
1002 struct anv_shader_module *module,
1003 const char *entrypoint,
1004 const VkSpecializationInfo *spec_info)
1005 {
1006 const struct brw_compiler *compiler =
1007 pipeline->device->instance->physicalDevice.compiler;
1008 struct brw_cs_prog_key key;
1009 struct anv_shader_bin *bin = NULL;
1010
1011 populate_cs_prog_key(&pipeline->device->info, &key);
1012
1013 ANV_FROM_HANDLE(anv_pipeline_layout, layout, info->layout);
1014
1015 unsigned char sha1[20];
1016 anv_pipeline_hash_shader(pipeline, layout, module, entrypoint,
1017 MESA_SHADER_COMPUTE, spec_info,
1018 &key, sizeof(key), sha1);
1019 bin = anv_device_search_for_kernel(pipeline->device, cache, sha1, 20);
1020
1021 if (bin == NULL) {
1022 struct brw_cs_prog_data prog_data = {};
1023 struct anv_pipeline_binding surface_to_descriptor[256];
1024 struct anv_pipeline_binding sampler_to_descriptor[256];
1025
1026 struct anv_pipeline_bind_map map = {
1027 .surface_to_descriptor = surface_to_descriptor,
1028 .sampler_to_descriptor = sampler_to_descriptor
1029 };
1030
1031 void *mem_ctx = ralloc_context(NULL);
1032
1033 nir_shader *nir = anv_pipeline_compile(pipeline, mem_ctx, layout,
1034 module, entrypoint,
1035 MESA_SHADER_COMPUTE, spec_info,
1036 &prog_data.base, &map);
1037 if (nir == NULL) {
1038 ralloc_free(mem_ctx);
1039 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1040 }
1041
1042 NIR_PASS_V(nir, anv_nir_add_base_work_group_id, &prog_data);
1043
1044 anv_fill_binding_table(&prog_data.base, 1);
1045
1046 const unsigned *shader_code =
1047 brw_compile_cs(compiler, NULL, mem_ctx, &key, &prog_data, nir,
1048 -1, NULL);
1049 if (shader_code == NULL) {
1050 ralloc_free(mem_ctx);
1051 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1052 }
1053
1054 const unsigned code_size = prog_data.base.program_size;
1055 bin = anv_device_upload_kernel(pipeline->device, cache, sha1, 20,
1056 shader_code, code_size,
1057 nir->constant_data,
1058 nir->constant_data_size,
1059 &prog_data.base, sizeof(prog_data),
1060 &map);
1061 if (!bin) {
1062 ralloc_free(mem_ctx);
1063 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1064 }
1065
1066 ralloc_free(mem_ctx);
1067 }
1068
1069 anv_pipeline_add_compiled_stage(pipeline, MESA_SHADER_COMPUTE, bin);
1070
1071 return VK_SUCCESS;
1072 }
1073
1074 /**
1075 * Copy pipeline state not marked as dynamic.
1076 * Dynamic state is pipeline state which hasn't been provided at pipeline
1077 * creation time, but is dynamically provided afterwards using various
1078 * vkCmdSet* functions.
1079 *
1080 * The set of state considered "non_dynamic" is determined by the pieces of
1081 * state that have their corresponding VkDynamicState enums omitted from
1082 * VkPipelineDynamicStateCreateInfo::pDynamicStates.
1083 *
1084 * @param[out] pipeline Destination non_dynamic state.
1085 * @param[in] pCreateInfo Source of non_dynamic state to be copied.
1086 */
1087 static void
1088 copy_non_dynamic_state(struct anv_pipeline *pipeline,
1089 const VkGraphicsPipelineCreateInfo *pCreateInfo)
1090 {
1091 anv_cmd_dirty_mask_t states = ANV_CMD_DIRTY_DYNAMIC_ALL;
1092 struct anv_subpass *subpass = pipeline->subpass;
1093
1094 pipeline->dynamic_state = default_dynamic_state;
1095
1096 if (pCreateInfo->pDynamicState) {
1097 /* Remove all of the states that are marked as dynamic */
1098 uint32_t count = pCreateInfo->pDynamicState->dynamicStateCount;
1099 for (uint32_t s = 0; s < count; s++)
1100 states &= ~(1 << pCreateInfo->pDynamicState->pDynamicStates[s]);
1101 }
1102
1103 struct anv_dynamic_state *dynamic = &pipeline->dynamic_state;
1104
1105 /* Section 9.2 of the Vulkan 1.0.15 spec says:
1106 *
1107 * pViewportState is [...] NULL if the pipeline
1108 * has rasterization disabled.
1109 */
1110 if (!pCreateInfo->pRasterizationState->rasterizerDiscardEnable) {
1111 assert(pCreateInfo->pViewportState);
1112
1113 dynamic->viewport.count = pCreateInfo->pViewportState->viewportCount;
1114 if (states & (1 << VK_DYNAMIC_STATE_VIEWPORT)) {
1115 typed_memcpy(dynamic->viewport.viewports,
1116 pCreateInfo->pViewportState->pViewports,
1117 pCreateInfo->pViewportState->viewportCount);
1118 }
1119
1120 dynamic->scissor.count = pCreateInfo->pViewportState->scissorCount;
1121 if (states & (1 << VK_DYNAMIC_STATE_SCISSOR)) {
1122 typed_memcpy(dynamic->scissor.scissors,
1123 pCreateInfo->pViewportState->pScissors,
1124 pCreateInfo->pViewportState->scissorCount);
1125 }
1126 }
1127
1128 if (states & (1 << VK_DYNAMIC_STATE_LINE_WIDTH)) {
1129 assert(pCreateInfo->pRasterizationState);
1130 dynamic->line_width = pCreateInfo->pRasterizationState->lineWidth;
1131 }
1132
1133 if (states & (1 << VK_DYNAMIC_STATE_DEPTH_BIAS)) {
1134 assert(pCreateInfo->pRasterizationState);
1135 dynamic->depth_bias.bias =
1136 pCreateInfo->pRasterizationState->depthBiasConstantFactor;
1137 dynamic->depth_bias.clamp =
1138 pCreateInfo->pRasterizationState->depthBiasClamp;
1139 dynamic->depth_bias.slope =
1140 pCreateInfo->pRasterizationState->depthBiasSlopeFactor;
1141 }
1142
1143 /* Section 9.2 of the Vulkan 1.0.15 spec says:
1144 *
1145 * pColorBlendState is [...] NULL if the pipeline has rasterization
1146 * disabled or if the subpass of the render pass the pipeline is
1147 * created against does not use any color attachments.
1148 */
1149 bool uses_color_att = false;
1150 for (unsigned i = 0; i < subpass->color_count; ++i) {
1151 if (subpass->color_attachments[i].attachment != VK_ATTACHMENT_UNUSED) {
1152 uses_color_att = true;
1153 break;
1154 }
1155 }
1156
1157 if (uses_color_att &&
1158 !pCreateInfo->pRasterizationState->rasterizerDiscardEnable) {
1159 assert(pCreateInfo->pColorBlendState);
1160
1161 if (states & (1 << VK_DYNAMIC_STATE_BLEND_CONSTANTS))
1162 typed_memcpy(dynamic->blend_constants,
1163 pCreateInfo->pColorBlendState->blendConstants, 4);
1164 }
1165
1166 /* If there is no depthstencil attachment, then don't read
1167 * pDepthStencilState. The Vulkan spec states that pDepthStencilState may
1168 * be NULL in this case. Even if pDepthStencilState is non-NULL, there is
1169 * no need to override the depthstencil defaults in
1170 * anv_pipeline::dynamic_state when there is no depthstencil attachment.
1171 *
1172 * Section 9.2 of the Vulkan 1.0.15 spec says:
1173 *
1174 * pDepthStencilState is [...] NULL if the pipeline has rasterization
1175 * disabled or if the subpass of the render pass the pipeline is created
1176 * against does not use a depth/stencil attachment.
1177 */
1178 if (!pCreateInfo->pRasterizationState->rasterizerDiscardEnable &&
1179 subpass->depth_stencil_attachment) {
1180 assert(pCreateInfo->pDepthStencilState);
1181
1182 if (states & (1 << VK_DYNAMIC_STATE_DEPTH_BOUNDS)) {
1183 dynamic->depth_bounds.min =
1184 pCreateInfo->pDepthStencilState->minDepthBounds;
1185 dynamic->depth_bounds.max =
1186 pCreateInfo->pDepthStencilState->maxDepthBounds;
1187 }
1188
1189 if (states & (1 << VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK)) {
1190 dynamic->stencil_compare_mask.front =
1191 pCreateInfo->pDepthStencilState->front.compareMask;
1192 dynamic->stencil_compare_mask.back =
1193 pCreateInfo->pDepthStencilState->back.compareMask;
1194 }
1195
1196 if (states & (1 << VK_DYNAMIC_STATE_STENCIL_WRITE_MASK)) {
1197 dynamic->stencil_write_mask.front =
1198 pCreateInfo->pDepthStencilState->front.writeMask;
1199 dynamic->stencil_write_mask.back =
1200 pCreateInfo->pDepthStencilState->back.writeMask;
1201 }
1202
1203 if (states & (1 << VK_DYNAMIC_STATE_STENCIL_REFERENCE)) {
1204 dynamic->stencil_reference.front =
1205 pCreateInfo->pDepthStencilState->front.reference;
1206 dynamic->stencil_reference.back =
1207 pCreateInfo->pDepthStencilState->back.reference;
1208 }
1209 }
1210
1211 pipeline->dynamic_state_mask = states;
1212 }
1213
1214 static void
1215 anv_pipeline_validate_create_info(const VkGraphicsPipelineCreateInfo *info)
1216 {
1217 #ifdef DEBUG
1218 struct anv_render_pass *renderpass = NULL;
1219 struct anv_subpass *subpass = NULL;
1220
1221 /* Assert that all required members of VkGraphicsPipelineCreateInfo are
1222 * present. See the Vulkan 1.0.28 spec, Section 9.2 Graphics Pipelines.
1223 */
1224 assert(info->sType == VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO);
1225
1226 renderpass = anv_render_pass_from_handle(info->renderPass);
1227 assert(renderpass);
1228
1229 assert(info->subpass < renderpass->subpass_count);
1230 subpass = &renderpass->subpasses[info->subpass];
1231
1232 assert(info->stageCount >= 1);
1233 assert(info->pVertexInputState);
1234 assert(info->pInputAssemblyState);
1235 assert(info->pRasterizationState);
1236 if (!info->pRasterizationState->rasterizerDiscardEnable) {
1237 assert(info->pViewportState);
1238 assert(info->pMultisampleState);
1239
1240 if (subpass && subpass->depth_stencil_attachment)
1241 assert(info->pDepthStencilState);
1242
1243 if (subpass && subpass->color_count > 0) {
1244 bool all_color_unused = true;
1245 for (int i = 0; i < subpass->color_count; i++) {
1246 if (subpass->color_attachments[i].attachment != VK_ATTACHMENT_UNUSED)
1247 all_color_unused = false;
1248 }
1249 /* pColorBlendState is ignored if the pipeline has rasterization
1250 * disabled or if the subpass of the render pass the pipeline is
1251 * created against does not use any color attachments.
1252 */
1253 assert(info->pColorBlendState || all_color_unused);
1254 }
1255 }
1256
1257 for (uint32_t i = 0; i < info->stageCount; ++i) {
1258 switch (info->pStages[i].stage) {
1259 case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT:
1260 case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT:
1261 assert(info->pTessellationState);
1262 break;
1263 default:
1264 break;
1265 }
1266 }
1267 #endif
1268 }
1269
1270 /**
1271 * Calculate the desired L3 partitioning based on the current state of the
1272 * pipeline. For now this simply returns the conservative defaults calculated
1273 * by get_default_l3_weights(), but we could probably do better by gathering
1274 * more statistics from the pipeline state (e.g. guess of expected URB usage
1275 * and bound surfaces), or by using feed-back from performance counters.
1276 */
1277 void
1278 anv_pipeline_setup_l3_config(struct anv_pipeline *pipeline, bool needs_slm)
1279 {
1280 const struct gen_device_info *devinfo = &pipeline->device->info;
1281
1282 const struct gen_l3_weights w =
1283 gen_get_default_l3_weights(devinfo, pipeline->needs_data_cache, needs_slm);
1284
1285 pipeline->urb.l3_config = gen_get_l3_config(devinfo, w);
1286 pipeline->urb.total_size =
1287 gen_get_l3_config_urb_size(devinfo, pipeline->urb.l3_config);
1288 }
1289
1290 VkResult
1291 anv_pipeline_init(struct anv_pipeline *pipeline,
1292 struct anv_device *device,
1293 struct anv_pipeline_cache *cache,
1294 const VkGraphicsPipelineCreateInfo *pCreateInfo,
1295 const VkAllocationCallbacks *alloc)
1296 {
1297 VkResult result;
1298
1299 anv_pipeline_validate_create_info(pCreateInfo);
1300
1301 if (alloc == NULL)
1302 alloc = &device->alloc;
1303
1304 pipeline->device = device;
1305
1306 ANV_FROM_HANDLE(anv_render_pass, render_pass, pCreateInfo->renderPass);
1307 assert(pCreateInfo->subpass < render_pass->subpass_count);
1308 pipeline->subpass = &render_pass->subpasses[pCreateInfo->subpass];
1309
1310 result = anv_reloc_list_init(&pipeline->batch_relocs, alloc);
1311 if (result != VK_SUCCESS)
1312 return result;
1313
1314 pipeline->batch.alloc = alloc;
1315 pipeline->batch.next = pipeline->batch.start = pipeline->batch_data;
1316 pipeline->batch.end = pipeline->batch.start + sizeof(pipeline->batch_data);
1317 pipeline->batch.relocs = &pipeline->batch_relocs;
1318 pipeline->batch.status = VK_SUCCESS;
1319
1320 copy_non_dynamic_state(pipeline, pCreateInfo);
1321 pipeline->depth_clamp_enable = pCreateInfo->pRasterizationState &&
1322 pCreateInfo->pRasterizationState->depthClampEnable;
1323
1324 pipeline->sample_shading_enable = pCreateInfo->pMultisampleState &&
1325 pCreateInfo->pMultisampleState->sampleShadingEnable;
1326
1327 pipeline->needs_data_cache = false;
1328
1329 /* When we free the pipeline, we detect stages based on the NULL status
1330 * of various prog_data pointers. Make them NULL by default.
1331 */
1332 memset(pipeline->shaders, 0, sizeof(pipeline->shaders));
1333
1334 pipeline->active_stages = 0;
1335
1336 const VkPipelineShaderStageCreateInfo *pStages[MESA_SHADER_STAGES] = {};
1337 struct anv_shader_module *modules[MESA_SHADER_STAGES] = {};
1338 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
1339 VkShaderStageFlagBits vk_stage = pCreateInfo->pStages[i].stage;
1340 gl_shader_stage stage = vk_to_mesa_shader_stage(vk_stage);
1341 pStages[stage] = &pCreateInfo->pStages[i];
1342 modules[stage] = anv_shader_module_from_handle(pStages[stage]->module);
1343 pipeline->active_stages |= vk_stage;
1344 }
1345
1346 if (pipeline->active_stages & VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT)
1347 pipeline->active_stages |= VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT;
1348
1349 assert(pipeline->active_stages & VK_SHADER_STAGE_VERTEX_BIT);
1350
1351 if (modules[MESA_SHADER_VERTEX]) {
1352 result = anv_pipeline_compile_vs(pipeline, cache, pCreateInfo,
1353 modules[MESA_SHADER_VERTEX],
1354 pStages[MESA_SHADER_VERTEX]->pName,
1355 pStages[MESA_SHADER_VERTEX]->pSpecializationInfo);
1356 if (result != VK_SUCCESS)
1357 goto compile_fail;
1358 }
1359
1360 if (modules[MESA_SHADER_TESS_EVAL]) {
1361 result = anv_pipeline_compile_tcs_tes(pipeline, cache, pCreateInfo,
1362 modules[MESA_SHADER_TESS_CTRL],
1363 pStages[MESA_SHADER_TESS_CTRL]->pName,
1364 pStages[MESA_SHADER_TESS_CTRL]->pSpecializationInfo,
1365 modules[MESA_SHADER_TESS_EVAL],
1366 pStages[MESA_SHADER_TESS_EVAL]->pName,
1367 pStages[MESA_SHADER_TESS_EVAL]->pSpecializationInfo);
1368 if (result != VK_SUCCESS)
1369 goto compile_fail;
1370 }
1371
1372 if (modules[MESA_SHADER_GEOMETRY]) {
1373 result = anv_pipeline_compile_gs(pipeline, cache, pCreateInfo,
1374 modules[MESA_SHADER_GEOMETRY],
1375 pStages[MESA_SHADER_GEOMETRY]->pName,
1376 pStages[MESA_SHADER_GEOMETRY]->pSpecializationInfo);
1377 if (result != VK_SUCCESS)
1378 goto compile_fail;
1379 }
1380
1381 if (modules[MESA_SHADER_FRAGMENT]) {
1382 result = anv_pipeline_compile_fs(pipeline, cache, pCreateInfo,
1383 modules[MESA_SHADER_FRAGMENT],
1384 pStages[MESA_SHADER_FRAGMENT]->pName,
1385 pStages[MESA_SHADER_FRAGMENT]->pSpecializationInfo);
1386 if (result != VK_SUCCESS)
1387 goto compile_fail;
1388 }
1389
1390 assert(pipeline->shaders[MESA_SHADER_VERTEX]);
1391
1392 anv_pipeline_setup_l3_config(pipeline, false);
1393
1394 const VkPipelineVertexInputStateCreateInfo *vi_info =
1395 pCreateInfo->pVertexInputState;
1396
1397 const uint64_t inputs_read = get_vs_prog_data(pipeline)->inputs_read;
1398
1399 pipeline->vb_used = 0;
1400 for (uint32_t i = 0; i < vi_info->vertexAttributeDescriptionCount; i++) {
1401 const VkVertexInputAttributeDescription *desc =
1402 &vi_info->pVertexAttributeDescriptions[i];
1403
1404 if (inputs_read & (1ull << (VERT_ATTRIB_GENERIC0 + desc->location)))
1405 pipeline->vb_used |= 1 << desc->binding;
1406 }
1407
1408 for (uint32_t i = 0; i < vi_info->vertexBindingDescriptionCount; i++) {
1409 const VkVertexInputBindingDescription *desc =
1410 &vi_info->pVertexBindingDescriptions[i];
1411
1412 pipeline->vb[desc->binding].stride = desc->stride;
1413
1414 /* Step rate is programmed per vertex element (attribute), not
1415 * binding. Set up a map of which bindings step per instance, for
1416 * reference by vertex element setup. */
1417 switch (desc->inputRate) {
1418 default:
1419 case VK_VERTEX_INPUT_RATE_VERTEX:
1420 pipeline->vb[desc->binding].instanced = false;
1421 break;
1422 case VK_VERTEX_INPUT_RATE_INSTANCE:
1423 pipeline->vb[desc->binding].instanced = true;
1424 break;
1425 }
1426
1427 pipeline->vb[desc->binding].instance_divisor = 1;
1428 }
1429
1430 const VkPipelineVertexInputDivisorStateCreateInfoEXT *vi_div_state =
1431 vk_find_struct_const(vi_info->pNext,
1432 PIPELINE_VERTEX_INPUT_DIVISOR_STATE_CREATE_INFO_EXT);
1433 if (vi_div_state) {
1434 for (uint32_t i = 0; i < vi_div_state->vertexBindingDivisorCount; i++) {
1435 const VkVertexInputBindingDivisorDescriptionEXT *desc =
1436 &vi_div_state->pVertexBindingDivisors[i];
1437
1438 pipeline->vb[desc->binding].instance_divisor = desc->divisor;
1439 }
1440 }
1441
1442 /* Our implementation of VK_KHR_multiview uses instancing to draw the
1443 * different views. If the client asks for instancing, we need to multiply
1444 * the instance divisor by the number of views ensure that we repeat the
1445 * client's per-instance data once for each view.
1446 */
1447 if (pipeline->subpass->view_mask) {
1448 const uint32_t view_count = anv_subpass_view_count(pipeline->subpass);
1449 for (uint32_t vb = 0; vb < MAX_VBS; vb++) {
1450 if (pipeline->vb[vb].instanced)
1451 pipeline->vb[vb].instance_divisor *= view_count;
1452 }
1453 }
1454
1455 const VkPipelineInputAssemblyStateCreateInfo *ia_info =
1456 pCreateInfo->pInputAssemblyState;
1457 const VkPipelineTessellationStateCreateInfo *tess_info =
1458 pCreateInfo->pTessellationState;
1459 pipeline->primitive_restart = ia_info->primitiveRestartEnable;
1460
1461 if (anv_pipeline_has_stage(pipeline, MESA_SHADER_TESS_EVAL))
1462 pipeline->topology = _3DPRIM_PATCHLIST(tess_info->patchControlPoints);
1463 else
1464 pipeline->topology = vk_to_gen_primitive_type[ia_info->topology];
1465
1466 return VK_SUCCESS;
1467
1468 compile_fail:
1469 for (unsigned s = 0; s < MESA_SHADER_STAGES; s++) {
1470 if (pipeline->shaders[s])
1471 anv_shader_bin_unref(device, pipeline->shaders[s]);
1472 }
1473
1474 anv_reloc_list_finish(&pipeline->batch_relocs, alloc);
1475
1476 return result;
1477 }