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