anv/gpu_memcpy: Add a lighter-weight GPU memcpy function
[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 nir_shader_gather_info(nir, nir_shader_get_entrypoint(nir));
385
386 /* Figure out the number of parameters */
387 prog_data->nr_params = 0;
388
389 if (nir->num_uniforms > 0) {
390 /* If the shader uses any push constants at all, we'll just give
391 * them the maximum possible number
392 */
393 assert(nir->num_uniforms <= MAX_PUSH_CONSTANTS_SIZE);
394 prog_data->nr_params += MAX_PUSH_CONSTANTS_SIZE / sizeof(float);
395 }
396
397 if (nir->info.num_images > 0) {
398 prog_data->nr_params += nir->info.num_images * BRW_IMAGE_PARAM_SIZE;
399 pipeline->needs_data_cache = true;
400 }
401
402 if (stage == MESA_SHADER_COMPUTE)
403 ((struct brw_cs_prog_data *)prog_data)->thread_local_id_index =
404 prog_data->nr_params++; /* The CS Thread ID uniform */
405
406 if (nir->info.num_ssbos > 0)
407 pipeline->needs_data_cache = true;
408
409 if (prog_data->nr_params > 0) {
410 /* XXX: I think we're leaking this */
411 prog_data->param = (const union gl_constant_value **)
412 malloc(prog_data->nr_params * sizeof(union gl_constant_value *));
413
414 /* We now set the param values to be offsets into a
415 * anv_push_constant_data structure. Since the compiler doesn't
416 * actually dereference any of the gl_constant_value pointers in the
417 * params array, it doesn't really matter what we put here.
418 */
419 struct anv_push_constants *null_data = NULL;
420 if (nir->num_uniforms > 0) {
421 /* Fill out the push constants section of the param array */
422 for (unsigned i = 0; i < MAX_PUSH_CONSTANTS_SIZE / sizeof(float); i++)
423 prog_data->param[i] = (const union gl_constant_value *)
424 &null_data->client_data[i * sizeof(float)];
425 }
426 }
427
428 /* Apply the actual pipeline layout to UBOs, SSBOs, and textures */
429 if (pipeline->layout)
430 anv_nir_apply_pipeline_layout(pipeline, nir, prog_data, map);
431
432 /* nir_lower_io will only handle the push constants; we need to set this
433 * to the full number of possible uniforms.
434 */
435 nir->num_uniforms = prog_data->nr_params * 4;
436
437 return nir;
438 }
439
440 static void
441 anv_fill_binding_table(struct brw_stage_prog_data *prog_data, unsigned bias)
442 {
443 prog_data->binding_table.size_bytes = 0;
444 prog_data->binding_table.texture_start = bias;
445 prog_data->binding_table.gather_texture_start = bias;
446 prog_data->binding_table.ubo_start = bias;
447 prog_data->binding_table.ssbo_start = bias;
448 prog_data->binding_table.image_start = bias;
449 }
450
451 static struct anv_shader_bin *
452 anv_pipeline_upload_kernel(struct anv_pipeline *pipeline,
453 struct anv_pipeline_cache *cache,
454 const void *key_data, uint32_t key_size,
455 const void *kernel_data, uint32_t kernel_size,
456 const struct brw_stage_prog_data *prog_data,
457 uint32_t prog_data_size,
458 const struct anv_pipeline_bind_map *bind_map)
459 {
460 if (cache) {
461 return anv_pipeline_cache_upload_kernel(cache, key_data, key_size,
462 kernel_data, kernel_size,
463 prog_data, prog_data_size,
464 bind_map);
465 } else {
466 return anv_shader_bin_create(pipeline->device, key_data, key_size,
467 kernel_data, kernel_size,
468 prog_data, prog_data_size,
469 prog_data->param, bind_map);
470 }
471 }
472
473
474 static void
475 anv_pipeline_add_compiled_stage(struct anv_pipeline *pipeline,
476 gl_shader_stage stage,
477 struct anv_shader_bin *shader)
478 {
479 pipeline->shaders[stage] = shader;
480 pipeline->active_stages |= mesa_to_vk_shader_stage(stage);
481 }
482
483 static VkResult
484 anv_pipeline_compile_vs(struct anv_pipeline *pipeline,
485 struct anv_pipeline_cache *cache,
486 const VkGraphicsPipelineCreateInfo *info,
487 struct anv_shader_module *module,
488 const char *entrypoint,
489 const VkSpecializationInfo *spec_info)
490 {
491 const struct brw_compiler *compiler =
492 pipeline->device->instance->physicalDevice.compiler;
493 struct anv_pipeline_bind_map map;
494 struct brw_vs_prog_key key;
495 struct anv_shader_bin *bin = NULL;
496 unsigned char sha1[20];
497
498 populate_vs_prog_key(&pipeline->device->info, &key);
499
500 if (cache) {
501 anv_pipeline_hash_shader(pipeline, module, entrypoint,
502 MESA_SHADER_VERTEX, spec_info,
503 &key, sizeof(key), sha1);
504 bin = anv_pipeline_cache_search(cache, sha1, 20);
505 }
506
507 if (bin == NULL) {
508 struct brw_vs_prog_data prog_data = { 0, };
509 struct anv_pipeline_binding surface_to_descriptor[256];
510 struct anv_pipeline_binding sampler_to_descriptor[256];
511
512 map = (struct anv_pipeline_bind_map) {
513 .surface_to_descriptor = surface_to_descriptor,
514 .sampler_to_descriptor = sampler_to_descriptor
515 };
516
517 nir_shader *nir = anv_pipeline_compile(pipeline, module, entrypoint,
518 MESA_SHADER_VERTEX, spec_info,
519 &prog_data.base.base, &map);
520 if (nir == NULL)
521 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
522
523 anv_fill_binding_table(&prog_data.base.base, 0);
524
525 void *mem_ctx = ralloc_context(NULL);
526
527 ralloc_steal(mem_ctx, nir);
528
529 brw_compute_vue_map(&pipeline->device->info,
530 &prog_data.base.vue_map,
531 nir->info.outputs_written,
532 nir->info.separate_shader);
533
534 unsigned code_size;
535 const unsigned *shader_code =
536 brw_compile_vs(compiler, NULL, mem_ctx, &key, &prog_data, nir,
537 NULL, false, -1, &code_size, NULL);
538 if (shader_code == NULL) {
539 ralloc_free(mem_ctx);
540 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
541 }
542
543 bin = anv_pipeline_upload_kernel(pipeline, cache, sha1, 20,
544 shader_code, code_size,
545 &prog_data.base.base, sizeof(prog_data),
546 &map);
547 if (!bin) {
548 ralloc_free(mem_ctx);
549 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
550 }
551
552 ralloc_free(mem_ctx);
553 }
554
555 anv_pipeline_add_compiled_stage(pipeline, MESA_SHADER_VERTEX, bin);
556
557 return VK_SUCCESS;
558 }
559
560 static void
561 merge_tess_info(struct shader_info *tes_info,
562 const struct shader_info *tcs_info)
563 {
564 /* The Vulkan 1.0.38 spec, section 21.1 Tessellator says:
565 *
566 * "PointMode. Controls generation of points rather than triangles
567 * or lines. This functionality defaults to disabled, and is
568 * enabled if either shader stage includes the execution mode.
569 *
570 * and about Triangles, Quads, IsoLines, VertexOrderCw, VertexOrderCcw,
571 * PointMode, SpacingEqual, SpacingFractionalEven, SpacingFractionalOdd,
572 * and OutputVertices, it says:
573 *
574 * "One mode must be set in at least one of the tessellation
575 * shader stages."
576 *
577 * So, the fields can be set in either the TCS or TES, but they must
578 * agree if set in both. Our backend looks at TES, so bitwise-or in
579 * the values from the TCS.
580 */
581 assert(tcs_info->tess.tcs_vertices_out == 0 ||
582 tes_info->tess.tcs_vertices_out == 0 ||
583 tcs_info->tess.tcs_vertices_out == tes_info->tess.tcs_vertices_out);
584 tes_info->tess.tcs_vertices_out |= tcs_info->tess.tcs_vertices_out;
585
586 assert(tcs_info->tess.spacing == TESS_SPACING_UNSPECIFIED ||
587 tes_info->tess.spacing == TESS_SPACING_UNSPECIFIED ||
588 tcs_info->tess.spacing == tes_info->tess.spacing);
589 tes_info->tess.spacing |= tcs_info->tess.spacing;
590
591 assert(tcs_info->tess.primitive_mode == 0 ||
592 tes_info->tess.primitive_mode == 0 ||
593 tcs_info->tess.primitive_mode == tes_info->tess.primitive_mode);
594 tes_info->tess.primitive_mode |= tcs_info->tess.primitive_mode;
595 tes_info->tess.ccw |= tcs_info->tess.ccw;
596 tes_info->tess.point_mode |= tcs_info->tess.point_mode;
597 }
598
599 static VkResult
600 anv_pipeline_compile_tcs_tes(struct anv_pipeline *pipeline,
601 struct anv_pipeline_cache *cache,
602 const VkGraphicsPipelineCreateInfo *info,
603 struct anv_shader_module *tcs_module,
604 const char *tcs_entrypoint,
605 const VkSpecializationInfo *tcs_spec_info,
606 struct anv_shader_module *tes_module,
607 const char *tes_entrypoint,
608 const VkSpecializationInfo *tes_spec_info)
609 {
610 const struct gen_device_info *devinfo = &pipeline->device->info;
611 const struct brw_compiler *compiler =
612 pipeline->device->instance->physicalDevice.compiler;
613 struct anv_pipeline_bind_map tcs_map;
614 struct anv_pipeline_bind_map tes_map;
615 struct brw_tcs_prog_key tcs_key = { 0, };
616 struct brw_tes_prog_key tes_key = { 0, };
617 struct anv_shader_bin *tcs_bin = NULL;
618 struct anv_shader_bin *tes_bin = NULL;
619 unsigned char tcs_sha1[40];
620 unsigned char tes_sha1[40];
621
622 populate_sampler_prog_key(&pipeline->device->info, &tcs_key.tex);
623 populate_sampler_prog_key(&pipeline->device->info, &tes_key.tex);
624 tcs_key.input_vertices = info->pTessellationState->patchControlPoints;
625
626 if (cache) {
627 anv_pipeline_hash_shader(pipeline, tcs_module, tcs_entrypoint,
628 MESA_SHADER_TESS_CTRL, tcs_spec_info,
629 &tcs_key, sizeof(tcs_key), tcs_sha1);
630 anv_pipeline_hash_shader(pipeline, tes_module, tes_entrypoint,
631 MESA_SHADER_TESS_EVAL, tes_spec_info,
632 &tes_key, sizeof(tes_key), tes_sha1);
633 memcpy(&tcs_sha1[20], tes_sha1, 20);
634 memcpy(&tes_sha1[20], tcs_sha1, 20);
635 tcs_bin = anv_pipeline_cache_search(cache, tcs_sha1, sizeof(tcs_sha1));
636 tes_bin = anv_pipeline_cache_search(cache, tes_sha1, sizeof(tes_sha1));
637 }
638
639 if (tcs_bin == NULL || tes_bin == NULL) {
640 struct brw_tcs_prog_data tcs_prog_data = { 0, };
641 struct brw_tes_prog_data tes_prog_data = { 0, };
642 struct anv_pipeline_binding tcs_surface_to_descriptor[256];
643 struct anv_pipeline_binding tcs_sampler_to_descriptor[256];
644 struct anv_pipeline_binding tes_surface_to_descriptor[256];
645 struct anv_pipeline_binding tes_sampler_to_descriptor[256];
646
647 tcs_map = (struct anv_pipeline_bind_map) {
648 .surface_to_descriptor = tcs_surface_to_descriptor,
649 .sampler_to_descriptor = tcs_sampler_to_descriptor
650 };
651 tes_map = (struct anv_pipeline_bind_map) {
652 .surface_to_descriptor = tes_surface_to_descriptor,
653 .sampler_to_descriptor = tes_sampler_to_descriptor
654 };
655
656 nir_shader *tcs_nir =
657 anv_pipeline_compile(pipeline, tcs_module, tcs_entrypoint,
658 MESA_SHADER_TESS_CTRL, tcs_spec_info,
659 &tcs_prog_data.base.base, &tcs_map);
660 nir_shader *tes_nir =
661 anv_pipeline_compile(pipeline, tes_module, tes_entrypoint,
662 MESA_SHADER_TESS_EVAL, tes_spec_info,
663 &tes_prog_data.base.base, &tes_map);
664 if (tcs_nir == NULL || tes_nir == NULL)
665 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
666
667 nir_lower_tes_patch_vertices(tes_nir,
668 tcs_nir->info.tess.tcs_vertices_out);
669
670 /* Copy TCS info into the TES info */
671 merge_tess_info(&tes_nir->info, &tcs_nir->info);
672
673 anv_fill_binding_table(&tcs_prog_data.base.base, 0);
674 anv_fill_binding_table(&tes_prog_data.base.base, 0);
675
676 void *mem_ctx = ralloc_context(NULL);
677
678 ralloc_steal(mem_ctx, tcs_nir);
679 ralloc_steal(mem_ctx, tes_nir);
680
681 /* Whacking the key after cache lookup is a bit sketchy, but all of
682 * this comes from the SPIR-V, which is part of the hash used for the
683 * pipeline cache. So it should be safe.
684 */
685 tcs_key.tes_primitive_mode = tes_nir->info.tess.primitive_mode;
686 tcs_key.outputs_written = tcs_nir->info.outputs_written;
687 tcs_key.patch_outputs_written = tcs_nir->info.patch_outputs_written;
688 tcs_key.quads_workaround =
689 devinfo->gen < 9 &&
690 tes_nir->info.tess.primitive_mode == 7 /* GL_QUADS */ &&
691 tes_nir->info.tess.spacing == TESS_SPACING_EQUAL;
692
693 tes_key.inputs_read = tcs_key.outputs_written;
694 tes_key.patch_inputs_read = tcs_key.patch_outputs_written;
695
696 unsigned code_size;
697 const int shader_time_index = -1;
698 const unsigned *shader_code;
699
700 shader_code =
701 brw_compile_tcs(compiler, NULL, mem_ctx, &tcs_key, &tcs_prog_data,
702 tcs_nir, shader_time_index, &code_size, NULL);
703 if (shader_code == NULL) {
704 ralloc_free(mem_ctx);
705 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
706 }
707
708 tcs_bin = anv_pipeline_upload_kernel(pipeline, cache,
709 tcs_sha1, sizeof(tcs_sha1),
710 shader_code, code_size,
711 &tcs_prog_data.base.base,
712 sizeof(tcs_prog_data),
713 &tcs_map);
714 if (!tcs_bin) {
715 ralloc_free(mem_ctx);
716 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
717 }
718
719 shader_code =
720 brw_compile_tes(compiler, NULL, mem_ctx, &tes_key,
721 &tcs_prog_data.base.vue_map, &tes_prog_data, tes_nir,
722 NULL, shader_time_index, &code_size, NULL);
723 if (shader_code == NULL) {
724 ralloc_free(mem_ctx);
725 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
726 }
727
728 tes_bin = anv_pipeline_upload_kernel(pipeline, cache,
729 tes_sha1, sizeof(tes_sha1),
730 shader_code, code_size,
731 &tes_prog_data.base.base,
732 sizeof(tes_prog_data),
733 &tes_map);
734 if (!tes_bin) {
735 ralloc_free(mem_ctx);
736 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
737 }
738
739 ralloc_free(mem_ctx);
740 }
741
742 anv_pipeline_add_compiled_stage(pipeline, MESA_SHADER_TESS_CTRL, tcs_bin);
743 anv_pipeline_add_compiled_stage(pipeline, MESA_SHADER_TESS_EVAL, tes_bin);
744
745 return VK_SUCCESS;
746 }
747
748 static VkResult
749 anv_pipeline_compile_gs(struct anv_pipeline *pipeline,
750 struct anv_pipeline_cache *cache,
751 const VkGraphicsPipelineCreateInfo *info,
752 struct anv_shader_module *module,
753 const char *entrypoint,
754 const VkSpecializationInfo *spec_info)
755 {
756 const struct brw_compiler *compiler =
757 pipeline->device->instance->physicalDevice.compiler;
758 struct anv_pipeline_bind_map map;
759 struct brw_gs_prog_key key;
760 struct anv_shader_bin *bin = NULL;
761 unsigned char sha1[20];
762
763 populate_gs_prog_key(&pipeline->device->info, &key);
764
765 if (cache) {
766 anv_pipeline_hash_shader(pipeline, module, entrypoint,
767 MESA_SHADER_GEOMETRY, spec_info,
768 &key, sizeof(key), sha1);
769 bin = anv_pipeline_cache_search(cache, sha1, 20);
770 }
771
772 if (bin == NULL) {
773 struct brw_gs_prog_data prog_data = { 0, };
774 struct anv_pipeline_binding surface_to_descriptor[256];
775 struct anv_pipeline_binding sampler_to_descriptor[256];
776
777 map = (struct anv_pipeline_bind_map) {
778 .surface_to_descriptor = surface_to_descriptor,
779 .sampler_to_descriptor = sampler_to_descriptor
780 };
781
782 nir_shader *nir = anv_pipeline_compile(pipeline, module, entrypoint,
783 MESA_SHADER_GEOMETRY, spec_info,
784 &prog_data.base.base, &map);
785 if (nir == NULL)
786 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
787
788 anv_fill_binding_table(&prog_data.base.base, 0);
789
790 void *mem_ctx = ralloc_context(NULL);
791
792 ralloc_steal(mem_ctx, nir);
793
794 brw_compute_vue_map(&pipeline->device->info,
795 &prog_data.base.vue_map,
796 nir->info.outputs_written,
797 nir->info.separate_shader);
798
799 unsigned code_size;
800 const unsigned *shader_code =
801 brw_compile_gs(compiler, NULL, mem_ctx, &key, &prog_data, nir,
802 NULL, -1, &code_size, NULL);
803 if (shader_code == NULL) {
804 ralloc_free(mem_ctx);
805 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
806 }
807
808 /* TODO: SIMD8 GS */
809 bin = anv_pipeline_upload_kernel(pipeline, cache, sha1, 20,
810 shader_code, code_size,
811 &prog_data.base.base, sizeof(prog_data),
812 &map);
813 if (!bin) {
814 ralloc_free(mem_ctx);
815 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
816 }
817
818 ralloc_free(mem_ctx);
819 }
820
821 anv_pipeline_add_compiled_stage(pipeline, MESA_SHADER_GEOMETRY, bin);
822
823 return VK_SUCCESS;
824 }
825
826 static VkResult
827 anv_pipeline_compile_fs(struct anv_pipeline *pipeline,
828 struct anv_pipeline_cache *cache,
829 const VkGraphicsPipelineCreateInfo *info,
830 struct anv_shader_module *module,
831 const char *entrypoint,
832 const VkSpecializationInfo *spec_info)
833 {
834 const struct brw_compiler *compiler =
835 pipeline->device->instance->physicalDevice.compiler;
836 struct anv_pipeline_bind_map map;
837 struct brw_wm_prog_key key;
838 struct anv_shader_bin *bin = NULL;
839 unsigned char sha1[20];
840
841 populate_wm_prog_key(pipeline, info, &key);
842
843 if (cache) {
844 anv_pipeline_hash_shader(pipeline, module, entrypoint,
845 MESA_SHADER_FRAGMENT, spec_info,
846 &key, sizeof(key), sha1);
847 bin = anv_pipeline_cache_search(cache, sha1, 20);
848 }
849
850 if (bin == NULL) {
851 struct brw_wm_prog_data prog_data = { 0, };
852 struct anv_pipeline_binding surface_to_descriptor[256];
853 struct anv_pipeline_binding sampler_to_descriptor[256];
854
855 map = (struct anv_pipeline_bind_map) {
856 .surface_to_descriptor = surface_to_descriptor + 8,
857 .sampler_to_descriptor = sampler_to_descriptor
858 };
859
860 nir_shader *nir = anv_pipeline_compile(pipeline, module, entrypoint,
861 MESA_SHADER_FRAGMENT, spec_info,
862 &prog_data.base, &map);
863 if (nir == NULL)
864 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
865
866 unsigned num_rts = 0;
867 struct anv_pipeline_binding rt_bindings[8];
868 nir_function_impl *impl = nir_shader_get_entrypoint(nir);
869 nir_foreach_variable_safe(var, &nir->outputs) {
870 if (var->data.location < FRAG_RESULT_DATA0)
871 continue;
872
873 unsigned rt = var->data.location - FRAG_RESULT_DATA0;
874 if (rt >= key.nr_color_regions) {
875 /* Out-of-bounds, throw it away */
876 var->data.mode = nir_var_local;
877 exec_node_remove(&var->node);
878 exec_list_push_tail(&impl->locals, &var->node);
879 continue;
880 }
881
882 /* Give it a new, compacted, location */
883 var->data.location = FRAG_RESULT_DATA0 + num_rts;
884
885 unsigned array_len =
886 glsl_type_is_array(var->type) ? glsl_get_length(var->type) : 1;
887 assert(num_rts + array_len <= 8);
888
889 for (unsigned i = 0; i < array_len; i++) {
890 rt_bindings[num_rts + i] = (struct anv_pipeline_binding) {
891 .set = ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS,
892 .binding = 0,
893 .index = rt + i,
894 };
895 }
896
897 num_rts += array_len;
898 }
899
900 if (num_rts == 0) {
901 /* If we have no render targets, we need a null render target */
902 rt_bindings[0] = (struct anv_pipeline_binding) {
903 .set = ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS,
904 .binding = 0,
905 .index = UINT8_MAX,
906 };
907 num_rts = 1;
908 }
909
910 assert(num_rts <= 8);
911 map.surface_to_descriptor -= num_rts;
912 map.surface_count += num_rts;
913 assert(map.surface_count <= 256);
914 memcpy(map.surface_to_descriptor, rt_bindings,
915 num_rts * sizeof(*rt_bindings));
916
917 anv_fill_binding_table(&prog_data.base, num_rts);
918
919 void *mem_ctx = ralloc_context(NULL);
920
921 ralloc_steal(mem_ctx, nir);
922
923 unsigned code_size;
924 const unsigned *shader_code =
925 brw_compile_fs(compiler, NULL, mem_ctx, &key, &prog_data, nir,
926 NULL, -1, -1, true, false, NULL, &code_size, NULL);
927 if (shader_code == NULL) {
928 ralloc_free(mem_ctx);
929 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
930 }
931
932 bin = anv_pipeline_upload_kernel(pipeline, cache, sha1, 20,
933 shader_code, code_size,
934 &prog_data.base, sizeof(prog_data),
935 &map);
936 if (!bin) {
937 ralloc_free(mem_ctx);
938 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
939 }
940
941 ralloc_free(mem_ctx);
942 }
943
944 anv_pipeline_add_compiled_stage(pipeline, MESA_SHADER_FRAGMENT, bin);
945
946 return VK_SUCCESS;
947 }
948
949 VkResult
950 anv_pipeline_compile_cs(struct anv_pipeline *pipeline,
951 struct anv_pipeline_cache *cache,
952 const VkComputePipelineCreateInfo *info,
953 struct anv_shader_module *module,
954 const char *entrypoint,
955 const VkSpecializationInfo *spec_info)
956 {
957 const struct brw_compiler *compiler =
958 pipeline->device->instance->physicalDevice.compiler;
959 struct anv_pipeline_bind_map map;
960 struct brw_cs_prog_key key;
961 struct anv_shader_bin *bin = NULL;
962 unsigned char sha1[20];
963
964 populate_cs_prog_key(&pipeline->device->info, &key);
965
966 if (cache) {
967 anv_pipeline_hash_shader(pipeline, module, entrypoint,
968 MESA_SHADER_COMPUTE, spec_info,
969 &key, sizeof(key), sha1);
970 bin = anv_pipeline_cache_search(cache, sha1, 20);
971 }
972
973 if (bin == NULL) {
974 struct brw_cs_prog_data prog_data = { 0, };
975 struct anv_pipeline_binding surface_to_descriptor[256];
976 struct anv_pipeline_binding sampler_to_descriptor[256];
977
978 map = (struct anv_pipeline_bind_map) {
979 .surface_to_descriptor = surface_to_descriptor,
980 .sampler_to_descriptor = sampler_to_descriptor
981 };
982
983 nir_shader *nir = anv_pipeline_compile(pipeline, module, entrypoint,
984 MESA_SHADER_COMPUTE, spec_info,
985 &prog_data.base, &map);
986 if (nir == NULL)
987 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
988
989 anv_fill_binding_table(&prog_data.base, 1);
990
991 void *mem_ctx = ralloc_context(NULL);
992
993 ralloc_steal(mem_ctx, nir);
994
995 unsigned code_size;
996 const unsigned *shader_code =
997 brw_compile_cs(compiler, NULL, mem_ctx, &key, &prog_data, nir,
998 -1, &code_size, NULL);
999 if (shader_code == NULL) {
1000 ralloc_free(mem_ctx);
1001 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1002 }
1003
1004 bin = anv_pipeline_upload_kernel(pipeline, cache, sha1, 20,
1005 shader_code, code_size,
1006 &prog_data.base, sizeof(prog_data),
1007 &map);
1008 if (!bin) {
1009 ralloc_free(mem_ctx);
1010 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1011 }
1012
1013 ralloc_free(mem_ctx);
1014 }
1015
1016 anv_pipeline_add_compiled_stage(pipeline, MESA_SHADER_COMPUTE, bin);
1017
1018 return VK_SUCCESS;
1019 }
1020
1021 /**
1022 * Copy pipeline state not marked as dynamic.
1023 * Dynamic state is pipeline state which hasn't been provided at pipeline
1024 * creation time, but is dynamically provided afterwards using various
1025 * vkCmdSet* functions.
1026 *
1027 * The set of state considered "non_dynamic" is determined by the pieces of
1028 * state that have their corresponding VkDynamicState enums omitted from
1029 * VkPipelineDynamicStateCreateInfo::pDynamicStates.
1030 *
1031 * @param[out] pipeline Destination non_dynamic state.
1032 * @param[in] pCreateInfo Source of non_dynamic state to be copied.
1033 */
1034 static void
1035 copy_non_dynamic_state(struct anv_pipeline *pipeline,
1036 const VkGraphicsPipelineCreateInfo *pCreateInfo)
1037 {
1038 anv_cmd_dirty_mask_t states = ANV_CMD_DIRTY_DYNAMIC_ALL;
1039 struct anv_subpass *subpass = pipeline->subpass;
1040
1041 pipeline->dynamic_state = default_dynamic_state;
1042
1043 if (pCreateInfo->pDynamicState) {
1044 /* Remove all of the states that are marked as dynamic */
1045 uint32_t count = pCreateInfo->pDynamicState->dynamicStateCount;
1046 for (uint32_t s = 0; s < count; s++)
1047 states &= ~(1 << pCreateInfo->pDynamicState->pDynamicStates[s]);
1048 }
1049
1050 struct anv_dynamic_state *dynamic = &pipeline->dynamic_state;
1051
1052 /* Section 9.2 of the Vulkan 1.0.15 spec says:
1053 *
1054 * pViewportState is [...] NULL if the pipeline
1055 * has rasterization disabled.
1056 */
1057 if (!pCreateInfo->pRasterizationState->rasterizerDiscardEnable) {
1058 assert(pCreateInfo->pViewportState);
1059
1060 dynamic->viewport.count = pCreateInfo->pViewportState->viewportCount;
1061 if (states & (1 << VK_DYNAMIC_STATE_VIEWPORT)) {
1062 typed_memcpy(dynamic->viewport.viewports,
1063 pCreateInfo->pViewportState->pViewports,
1064 pCreateInfo->pViewportState->viewportCount);
1065 }
1066
1067 dynamic->scissor.count = pCreateInfo->pViewportState->scissorCount;
1068 if (states & (1 << VK_DYNAMIC_STATE_SCISSOR)) {
1069 typed_memcpy(dynamic->scissor.scissors,
1070 pCreateInfo->pViewportState->pScissors,
1071 pCreateInfo->pViewportState->scissorCount);
1072 }
1073 }
1074
1075 if (states & (1 << VK_DYNAMIC_STATE_LINE_WIDTH)) {
1076 assert(pCreateInfo->pRasterizationState);
1077 dynamic->line_width = pCreateInfo->pRasterizationState->lineWidth;
1078 }
1079
1080 if (states & (1 << VK_DYNAMIC_STATE_DEPTH_BIAS)) {
1081 assert(pCreateInfo->pRasterizationState);
1082 dynamic->depth_bias.bias =
1083 pCreateInfo->pRasterizationState->depthBiasConstantFactor;
1084 dynamic->depth_bias.clamp =
1085 pCreateInfo->pRasterizationState->depthBiasClamp;
1086 dynamic->depth_bias.slope =
1087 pCreateInfo->pRasterizationState->depthBiasSlopeFactor;
1088 }
1089
1090 /* Section 9.2 of the Vulkan 1.0.15 spec says:
1091 *
1092 * pColorBlendState is [...] NULL if the pipeline has rasterization
1093 * disabled or if the subpass of the render pass the pipeline is
1094 * created against does not use any color attachments.
1095 */
1096 bool uses_color_att = false;
1097 for (unsigned i = 0; i < subpass->color_count; ++i) {
1098 if (subpass->color_attachments[i].attachment != VK_ATTACHMENT_UNUSED) {
1099 uses_color_att = true;
1100 break;
1101 }
1102 }
1103
1104 if (uses_color_att &&
1105 !pCreateInfo->pRasterizationState->rasterizerDiscardEnable) {
1106 assert(pCreateInfo->pColorBlendState);
1107
1108 if (states & (1 << VK_DYNAMIC_STATE_BLEND_CONSTANTS))
1109 typed_memcpy(dynamic->blend_constants,
1110 pCreateInfo->pColorBlendState->blendConstants, 4);
1111 }
1112
1113 /* If there is no depthstencil attachment, then don't read
1114 * pDepthStencilState. The Vulkan spec states that pDepthStencilState may
1115 * be NULL in this case. Even if pDepthStencilState is non-NULL, there is
1116 * no need to override the depthstencil defaults in
1117 * anv_pipeline::dynamic_state when there is no depthstencil attachment.
1118 *
1119 * Section 9.2 of the Vulkan 1.0.15 spec says:
1120 *
1121 * pDepthStencilState is [...] NULL if the pipeline has rasterization
1122 * disabled or if the subpass of the render pass the pipeline is created
1123 * against does not use a depth/stencil attachment.
1124 */
1125 if (!pCreateInfo->pRasterizationState->rasterizerDiscardEnable &&
1126 subpass->depth_stencil_attachment.attachment != VK_ATTACHMENT_UNUSED) {
1127 assert(pCreateInfo->pDepthStencilState);
1128
1129 if (states & (1 << VK_DYNAMIC_STATE_DEPTH_BOUNDS)) {
1130 dynamic->depth_bounds.min =
1131 pCreateInfo->pDepthStencilState->minDepthBounds;
1132 dynamic->depth_bounds.max =
1133 pCreateInfo->pDepthStencilState->maxDepthBounds;
1134 }
1135
1136 if (states & (1 << VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK)) {
1137 dynamic->stencil_compare_mask.front =
1138 pCreateInfo->pDepthStencilState->front.compareMask;
1139 dynamic->stencil_compare_mask.back =
1140 pCreateInfo->pDepthStencilState->back.compareMask;
1141 }
1142
1143 if (states & (1 << VK_DYNAMIC_STATE_STENCIL_WRITE_MASK)) {
1144 dynamic->stencil_write_mask.front =
1145 pCreateInfo->pDepthStencilState->front.writeMask;
1146 dynamic->stencil_write_mask.back =
1147 pCreateInfo->pDepthStencilState->back.writeMask;
1148 }
1149
1150 if (states & (1 << VK_DYNAMIC_STATE_STENCIL_REFERENCE)) {
1151 dynamic->stencil_reference.front =
1152 pCreateInfo->pDepthStencilState->front.reference;
1153 dynamic->stencil_reference.back =
1154 pCreateInfo->pDepthStencilState->back.reference;
1155 }
1156 }
1157
1158 pipeline->dynamic_state_mask = states;
1159 }
1160
1161 static void
1162 anv_pipeline_validate_create_info(const VkGraphicsPipelineCreateInfo *info)
1163 {
1164 #ifdef DEBUG
1165 struct anv_render_pass *renderpass = NULL;
1166 struct anv_subpass *subpass = NULL;
1167
1168 /* Assert that all required members of VkGraphicsPipelineCreateInfo are
1169 * present. See the Vulkan 1.0.28 spec, Section 9.2 Graphics Pipelines.
1170 */
1171 assert(info->sType == VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO);
1172
1173 renderpass = anv_render_pass_from_handle(info->renderPass);
1174 assert(renderpass);
1175
1176 assert(info->subpass < renderpass->subpass_count);
1177 subpass = &renderpass->subpasses[info->subpass];
1178
1179 assert(info->stageCount >= 1);
1180 assert(info->pVertexInputState);
1181 assert(info->pInputAssemblyState);
1182 assert(info->pRasterizationState);
1183 if (!info->pRasterizationState->rasterizerDiscardEnable) {
1184 assert(info->pViewportState);
1185 assert(info->pMultisampleState);
1186
1187 if (subpass && subpass->depth_stencil_attachment.attachment != VK_ATTACHMENT_UNUSED)
1188 assert(info->pDepthStencilState);
1189
1190 if (subpass && subpass->color_count > 0)
1191 assert(info->pColorBlendState);
1192 }
1193
1194 for (uint32_t i = 0; i < info->stageCount; ++i) {
1195 switch (info->pStages[i].stage) {
1196 case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT:
1197 case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT:
1198 assert(info->pTessellationState);
1199 break;
1200 default:
1201 break;
1202 }
1203 }
1204 #endif
1205 }
1206
1207 /**
1208 * Calculate the desired L3 partitioning based on the current state of the
1209 * pipeline. For now this simply returns the conservative defaults calculated
1210 * by get_default_l3_weights(), but we could probably do better by gathering
1211 * more statistics from the pipeline state (e.g. guess of expected URB usage
1212 * and bound surfaces), or by using feed-back from performance counters.
1213 */
1214 void
1215 anv_pipeline_setup_l3_config(struct anv_pipeline *pipeline, bool needs_slm)
1216 {
1217 const struct gen_device_info *devinfo = &pipeline->device->info;
1218
1219 const struct gen_l3_weights w =
1220 gen_get_default_l3_weights(devinfo, pipeline->needs_data_cache, needs_slm);
1221
1222 pipeline->urb.l3_config = gen_get_l3_config(devinfo, w);
1223 pipeline->urb.total_size =
1224 gen_get_l3_config_urb_size(devinfo, pipeline->urb.l3_config);
1225 }
1226
1227 VkResult
1228 anv_pipeline_init(struct anv_pipeline *pipeline,
1229 struct anv_device *device,
1230 struct anv_pipeline_cache *cache,
1231 const VkGraphicsPipelineCreateInfo *pCreateInfo,
1232 const VkAllocationCallbacks *alloc)
1233 {
1234 VkResult result;
1235
1236 anv_pipeline_validate_create_info(pCreateInfo);
1237
1238 if (alloc == NULL)
1239 alloc = &device->alloc;
1240
1241 pipeline->device = device;
1242
1243 ANV_FROM_HANDLE(anv_render_pass, render_pass, pCreateInfo->renderPass);
1244 assert(pCreateInfo->subpass < render_pass->subpass_count);
1245 pipeline->subpass = &render_pass->subpasses[pCreateInfo->subpass];
1246
1247 pipeline->layout = anv_pipeline_layout_from_handle(pCreateInfo->layout);
1248
1249 result = anv_reloc_list_init(&pipeline->batch_relocs, alloc);
1250 if (result != VK_SUCCESS)
1251 return result;
1252
1253 pipeline->batch.alloc = alloc;
1254 pipeline->batch.next = pipeline->batch.start = pipeline->batch_data;
1255 pipeline->batch.end = pipeline->batch.start + sizeof(pipeline->batch_data);
1256 pipeline->batch.relocs = &pipeline->batch_relocs;
1257 pipeline->batch.status = VK_SUCCESS;
1258
1259 copy_non_dynamic_state(pipeline, pCreateInfo);
1260 pipeline->depth_clamp_enable = pCreateInfo->pRasterizationState &&
1261 pCreateInfo->pRasterizationState->depthClampEnable;
1262
1263 pipeline->sample_shading_enable = pCreateInfo->pMultisampleState &&
1264 pCreateInfo->pMultisampleState->sampleShadingEnable;
1265
1266 pipeline->needs_data_cache = false;
1267
1268 /* When we free the pipeline, we detect stages based on the NULL status
1269 * of various prog_data pointers. Make them NULL by default.
1270 */
1271 memset(pipeline->shaders, 0, sizeof(pipeline->shaders));
1272
1273 pipeline->active_stages = 0;
1274
1275 const VkPipelineShaderStageCreateInfo *pStages[MESA_SHADER_STAGES] = { 0, };
1276 struct anv_shader_module *modules[MESA_SHADER_STAGES] = { 0, };
1277 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
1278 gl_shader_stage stage = ffs(pCreateInfo->pStages[i].stage) - 1;
1279 pStages[stage] = &pCreateInfo->pStages[i];
1280 modules[stage] = anv_shader_module_from_handle(pStages[stage]->module);
1281 }
1282
1283 if (modules[MESA_SHADER_VERTEX]) {
1284 result = anv_pipeline_compile_vs(pipeline, cache, pCreateInfo,
1285 modules[MESA_SHADER_VERTEX],
1286 pStages[MESA_SHADER_VERTEX]->pName,
1287 pStages[MESA_SHADER_VERTEX]->pSpecializationInfo);
1288 if (result != VK_SUCCESS)
1289 goto compile_fail;
1290 }
1291
1292 if (modules[MESA_SHADER_TESS_EVAL]) {
1293 anv_pipeline_compile_tcs_tes(pipeline, cache, pCreateInfo,
1294 modules[MESA_SHADER_TESS_CTRL],
1295 pStages[MESA_SHADER_TESS_CTRL]->pName,
1296 pStages[MESA_SHADER_TESS_CTRL]->pSpecializationInfo,
1297 modules[MESA_SHADER_TESS_EVAL],
1298 pStages[MESA_SHADER_TESS_EVAL]->pName,
1299 pStages[MESA_SHADER_TESS_EVAL]->pSpecializationInfo);
1300 }
1301
1302 if (modules[MESA_SHADER_GEOMETRY]) {
1303 result = anv_pipeline_compile_gs(pipeline, cache, pCreateInfo,
1304 modules[MESA_SHADER_GEOMETRY],
1305 pStages[MESA_SHADER_GEOMETRY]->pName,
1306 pStages[MESA_SHADER_GEOMETRY]->pSpecializationInfo);
1307 if (result != VK_SUCCESS)
1308 goto compile_fail;
1309 }
1310
1311 if (modules[MESA_SHADER_FRAGMENT]) {
1312 result = anv_pipeline_compile_fs(pipeline, cache, pCreateInfo,
1313 modules[MESA_SHADER_FRAGMENT],
1314 pStages[MESA_SHADER_FRAGMENT]->pName,
1315 pStages[MESA_SHADER_FRAGMENT]->pSpecializationInfo);
1316 if (result != VK_SUCCESS)
1317 goto compile_fail;
1318 }
1319
1320 assert(pipeline->active_stages & VK_SHADER_STAGE_VERTEX_BIT);
1321
1322 anv_pipeline_setup_l3_config(pipeline, false);
1323
1324 const VkPipelineVertexInputStateCreateInfo *vi_info =
1325 pCreateInfo->pVertexInputState;
1326
1327 const uint64_t inputs_read = get_vs_prog_data(pipeline)->inputs_read;
1328
1329 pipeline->vb_used = 0;
1330 for (uint32_t i = 0; i < vi_info->vertexAttributeDescriptionCount; i++) {
1331 const VkVertexInputAttributeDescription *desc =
1332 &vi_info->pVertexAttributeDescriptions[i];
1333
1334 if (inputs_read & (1ull << (VERT_ATTRIB_GENERIC0 + desc->location)))
1335 pipeline->vb_used |= 1 << desc->binding;
1336 }
1337
1338 for (uint32_t i = 0; i < vi_info->vertexBindingDescriptionCount; i++) {
1339 const VkVertexInputBindingDescription *desc =
1340 &vi_info->pVertexBindingDescriptions[i];
1341
1342 pipeline->binding_stride[desc->binding] = desc->stride;
1343
1344 /* Step rate is programmed per vertex element (attribute), not
1345 * binding. Set up a map of which bindings step per instance, for
1346 * reference by vertex element setup. */
1347 switch (desc->inputRate) {
1348 default:
1349 case VK_VERTEX_INPUT_RATE_VERTEX:
1350 pipeline->instancing_enable[desc->binding] = false;
1351 break;
1352 case VK_VERTEX_INPUT_RATE_INSTANCE:
1353 pipeline->instancing_enable[desc->binding] = true;
1354 break;
1355 }
1356 }
1357
1358 const VkPipelineInputAssemblyStateCreateInfo *ia_info =
1359 pCreateInfo->pInputAssemblyState;
1360 const VkPipelineTessellationStateCreateInfo *tess_info =
1361 pCreateInfo->pTessellationState;
1362 pipeline->primitive_restart = ia_info->primitiveRestartEnable;
1363
1364 if (anv_pipeline_has_stage(pipeline, MESA_SHADER_TESS_EVAL))
1365 pipeline->topology = _3DPRIM_PATCHLIST(tess_info->patchControlPoints);
1366 else
1367 pipeline->topology = vk_to_gen_primitive_type[ia_info->topology];
1368
1369 return VK_SUCCESS;
1370
1371 compile_fail:
1372 for (unsigned s = 0; s < MESA_SHADER_STAGES; s++) {
1373 if (pipeline->shaders[s])
1374 anv_shader_bin_unref(device, pipeline->shaders[s]);
1375 }
1376
1377 anv_reloc_list_finish(&pipeline->batch_relocs, alloc);
1378
1379 return result;
1380 }