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