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