672640ac24ccd841be66512bba91bad93d43c608
[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 "anv_private.h"
32 #include "brw_nir.h"
33 #include "anv_nir.h"
34 #include "nir/spirv/nir_spirv.h"
35
36 /* Needed for SWIZZLE macros */
37 #include "program/prog_instruction.h"
38
39 // Shader functions
40
41 VkResult anv_CreateShaderModule(
42 VkDevice _device,
43 const VkShaderModuleCreateInfo* pCreateInfo,
44 const VkAllocationCallbacks* pAllocator,
45 VkShaderModule* pShaderModule)
46 {
47 ANV_FROM_HANDLE(anv_device, device, _device);
48 struct anv_shader_module *module;
49
50 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO);
51 assert(pCreateInfo->flags == 0);
52
53 module = anv_alloc2(&device->alloc, pAllocator,
54 sizeof(*module) + pCreateInfo->codeSize, 8,
55 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
56 if (module == NULL)
57 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
58
59 module->nir = NULL;
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 anv_free2(&device->alloc, pAllocator, module);
79 }
80
81 #define SPIR_V_MAGIC_NUMBER 0x07230203
82
83 /* Eventually, this will become part of anv_CreateShader. Unfortunately,
84 * we can't do that yet because we don't have the ability to copy nir.
85 */
86 static nir_shader *
87 anv_shader_compile_to_nir(struct anv_device *device,
88 struct anv_shader_module *module,
89 const char *entrypoint_name,
90 gl_shader_stage stage,
91 const VkSpecializationInfo *spec_info)
92 {
93 if (strcmp(entrypoint_name, "main") != 0) {
94 anv_finishme("Multiple shaders per module not really supported");
95 }
96
97 const struct brw_compiler *compiler =
98 device->instance->physicalDevice.compiler;
99 const nir_shader_compiler_options *nir_options =
100 compiler->glsl_compiler_options[stage].NirOptions;
101
102 nir_shader *nir;
103 nir_function *entry_point;
104 if (module->nir) {
105 /* Some things such as our meta clear/blit code will give us a NIR
106 * shader directly. In that case, we just ignore the SPIR-V entirely
107 * and just use the NIR shader */
108 nir = module->nir;
109 nir->options = nir_options;
110 nir_validate_shader(nir);
111
112 assert(exec_list_length(&nir->functions) == 1);
113 struct exec_node *node = exec_list_get_head(&nir->functions);
114 entry_point = exec_node_data(nir_function, node, node);
115 } else {
116 uint32_t *spirv = (uint32_t *) module->data;
117 assert(spirv[0] == SPIR_V_MAGIC_NUMBER);
118 assert(module->size % 4 == 0);
119
120 uint32_t num_spec_entries = 0;
121 struct nir_spirv_specialization *spec_entries = NULL;
122 if (spec_info && spec_info->mapEntryCount > 0) {
123 num_spec_entries = spec_info->mapEntryCount;
124 spec_entries = malloc(num_spec_entries * sizeof(*spec_entries));
125 for (uint32_t i = 0; i < num_spec_entries; i++) {
126 const uint32_t *data =
127 spec_info->pData + spec_info->pMapEntries[i].offset;
128 assert((const void *)(data + 1) <=
129 spec_info->pData + spec_info->dataSize);
130
131 spec_entries[i].id = spec_info->pMapEntries[i].constantID;
132 spec_entries[i].data = *data;
133 }
134 }
135
136 entry_point = spirv_to_nir(spirv, module->size / 4,
137 spec_entries, num_spec_entries,
138 stage, entrypoint_name, nir_options);
139 nir = entry_point->shader;
140 assert(nir->stage == stage);
141 nir_validate_shader(nir);
142
143 free(spec_entries);
144
145 nir_lower_returns(nir);
146 nir_validate_shader(nir);
147
148 nir_inline_functions(nir);
149 nir_validate_shader(nir);
150
151 /* Pick off the single entrypoint that we want */
152 foreach_list_typed_safe(nir_function, func, node, &nir->functions) {
153 if (func != entry_point)
154 exec_node_remove(&func->node);
155 }
156 assert(exec_list_length(&nir->functions) == 1);
157 entry_point->name = ralloc_strdup(entry_point, "main");
158
159 nir_remove_dead_variables(nir, nir_var_shader_in);
160 nir_remove_dead_variables(nir, nir_var_shader_out);
161 nir_remove_dead_variables(nir, nir_var_system_value);
162 nir_validate_shader(nir);
163
164 nir_lower_outputs_to_temporaries(entry_point->shader, entry_point);
165
166 nir_lower_system_values(nir);
167 nir_validate_shader(nir);
168 }
169
170 /* Vulkan uses the separate-shader linking model */
171 nir->info.separate_shader = true;
172
173 nir = brw_preprocess_nir(nir, compiler->scalar_stage[stage]);
174
175 nir_shader_gather_info(nir, entry_point->impl);
176
177 uint32_t indirect_mask = 0;
178 if (compiler->glsl_compiler_options[stage].EmitNoIndirectInput)
179 indirect_mask |= (1 << nir_var_shader_in);
180 if (compiler->glsl_compiler_options[stage].EmitNoIndirectTemp)
181 indirect_mask |= 1 << nir_var_local;
182
183 nir_lower_indirect_derefs(nir, indirect_mask);
184
185 return nir;
186 }
187
188 void anv_DestroyPipeline(
189 VkDevice _device,
190 VkPipeline _pipeline,
191 const VkAllocationCallbacks* pAllocator)
192 {
193 ANV_FROM_HANDLE(anv_device, device, _device);
194 ANV_FROM_HANDLE(anv_pipeline, pipeline, _pipeline);
195
196 for (unsigned s = 0; s < MESA_SHADER_STAGES; s++) {
197 free(pipeline->bindings[s].surface_to_descriptor);
198 free(pipeline->bindings[s].sampler_to_descriptor);
199 }
200
201 anv_reloc_list_finish(&pipeline->batch_relocs,
202 pAllocator ? pAllocator : &device->alloc);
203 if (pipeline->blend_state.map)
204 anv_state_pool_free(&device->dynamic_state_pool, pipeline->blend_state);
205 anv_free2(&device->alloc, pAllocator, pipeline);
206 }
207
208 static const uint32_t vk_to_gen_primitive_type[] = {
209 [VK_PRIMITIVE_TOPOLOGY_POINT_LIST] = _3DPRIM_POINTLIST,
210 [VK_PRIMITIVE_TOPOLOGY_LINE_LIST] = _3DPRIM_LINELIST,
211 [VK_PRIMITIVE_TOPOLOGY_LINE_STRIP] = _3DPRIM_LINESTRIP,
212 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST] = _3DPRIM_TRILIST,
213 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP] = _3DPRIM_TRISTRIP,
214 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN] = _3DPRIM_TRIFAN,
215 [VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY] = _3DPRIM_LINELIST_ADJ,
216 [VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY] = _3DPRIM_LINESTRIP_ADJ,
217 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY] = _3DPRIM_TRILIST_ADJ,
218 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY] = _3DPRIM_TRISTRIP_ADJ,
219 /* [VK_PRIMITIVE_TOPOLOGY_PATCH_LIST] = _3DPRIM_PATCHLIST_1 */
220 };
221
222 static void
223 populate_sampler_prog_key(const struct brw_device_info *devinfo,
224 struct brw_sampler_prog_key_data *key)
225 {
226 /* XXX: Handle texture swizzle on HSW- */
227 for (int i = 0; i < MAX_SAMPLERS; i++) {
228 /* Assume color sampler, no swizzling. (Works for BDW+) */
229 key->swizzles[i] = SWIZZLE_XYZW;
230 }
231 }
232
233 static void
234 populate_vs_prog_key(const struct brw_device_info *devinfo,
235 struct brw_vs_prog_key *key)
236 {
237 memset(key, 0, sizeof(*key));
238
239 populate_sampler_prog_key(devinfo, &key->tex);
240
241 /* XXX: Handle vertex input work-arounds */
242
243 /* XXX: Handle sampler_prog_key */
244 }
245
246 static void
247 populate_gs_prog_key(const struct brw_device_info *devinfo,
248 struct brw_gs_prog_key *key)
249 {
250 memset(key, 0, sizeof(*key));
251
252 populate_sampler_prog_key(devinfo, &key->tex);
253 }
254
255 static void
256 populate_wm_prog_key(const struct brw_device_info *devinfo,
257 const VkGraphicsPipelineCreateInfo *info,
258 const struct anv_graphics_pipeline_create_info *extra,
259 struct brw_wm_prog_key *key)
260 {
261 ANV_FROM_HANDLE(anv_render_pass, render_pass, info->renderPass);
262
263 memset(key, 0, sizeof(*key));
264
265 populate_sampler_prog_key(devinfo, &key->tex);
266
267 /* TODO: Fill out key->input_slots_valid */
268
269 /* Vulkan doesn't specify a default */
270 key->high_quality_derivatives = false;
271
272 /* XXX Vulkan doesn't appear to specify */
273 key->clamp_fragment_color = false;
274
275 /* Vulkan always specifies upper-left coordinates */
276 key->drawable_height = 0;
277 key->render_to_fbo = false;
278
279 if (extra && extra->color_attachment_count >= 0) {
280 key->nr_color_regions = extra->color_attachment_count;
281 } else {
282 key->nr_color_regions =
283 render_pass->subpasses[info->subpass].color_count;
284 }
285
286 key->replicate_alpha = key->nr_color_regions > 1 &&
287 info->pMultisampleState &&
288 info->pMultisampleState->alphaToCoverageEnable;
289
290 if (info->pMultisampleState && info->pMultisampleState->rasterizationSamples > 1) {
291 /* We should probably pull this out of the shader, but it's fairly
292 * harmless to compute it and then let dead-code take care of it.
293 */
294 key->persample_shading = info->pMultisampleState->sampleShadingEnable;
295 if (key->persample_shading)
296 key->persample_2x = info->pMultisampleState->rasterizationSamples == 2;
297
298 key->compute_pos_offset = info->pMultisampleState->sampleShadingEnable;
299 key->compute_sample_id = info->pMultisampleState->sampleShadingEnable;
300 }
301 }
302
303 static void
304 populate_cs_prog_key(const struct brw_device_info *devinfo,
305 struct brw_cs_prog_key *key)
306 {
307 memset(key, 0, sizeof(*key));
308
309 populate_sampler_prog_key(devinfo, &key->tex);
310 }
311
312 static nir_shader *
313 anv_pipeline_compile(struct anv_pipeline *pipeline,
314 struct anv_shader_module *module,
315 const char *entrypoint,
316 gl_shader_stage stage,
317 const VkSpecializationInfo *spec_info,
318 struct brw_stage_prog_data *prog_data)
319 {
320 const struct brw_compiler *compiler =
321 pipeline->device->instance->physicalDevice.compiler;
322
323 nir_shader *nir = anv_shader_compile_to_nir(pipeline->device,
324 module, entrypoint, stage,
325 spec_info);
326 if (nir == NULL)
327 return NULL;
328
329 anv_nir_lower_push_constants(nir, compiler->scalar_stage[stage]);
330
331 /* Figure out the number of parameters */
332 prog_data->nr_params = 0;
333
334 if (nir->num_uniforms > 0) {
335 /* If the shader uses any push constants at all, we'll just give
336 * them the maximum possible number
337 */
338 prog_data->nr_params += MAX_PUSH_CONSTANTS_SIZE / sizeof(float);
339 }
340
341 if (pipeline->layout && pipeline->layout->stage[stage].has_dynamic_offsets)
342 prog_data->nr_params += MAX_DYNAMIC_BUFFERS * 2;
343
344 if (nir->info.num_images > 0)
345 prog_data->nr_params += nir->info.num_images * BRW_IMAGE_PARAM_SIZE;
346
347 if (prog_data->nr_params > 0) {
348 /* XXX: I think we're leaking this */
349 prog_data->param = (const union gl_constant_value **)
350 malloc(prog_data->nr_params * sizeof(union gl_constant_value *));
351
352 /* We now set the param values to be offsets into a
353 * anv_push_constant_data structure. Since the compiler doesn't
354 * actually dereference any of the gl_constant_value pointers in the
355 * params array, it doesn't really matter what we put here.
356 */
357 struct anv_push_constants *null_data = NULL;
358 if (nir->num_uniforms > 0) {
359 /* Fill out the push constants section of the param array */
360 for (unsigned i = 0; i < MAX_PUSH_CONSTANTS_SIZE / sizeof(float); i++)
361 prog_data->param[i] = (const union gl_constant_value *)
362 &null_data->client_data[i * sizeof(float)];
363 }
364 }
365
366 /* Set up dynamic offsets */
367 anv_nir_apply_dynamic_offsets(pipeline, nir, prog_data);
368
369 char surface_usage_mask[256], sampler_usage_mask[256];
370 zero(surface_usage_mask);
371 zero(sampler_usage_mask);
372
373 /* Apply the actual pipeline layout to UBOs, SSBOs, and textures */
374 if (pipeline->layout)
375 anv_nir_apply_pipeline_layout(pipeline, nir, prog_data);
376
377 /* All binding table offsets provided by apply_pipeline_layout() are
378 * relative to the start of the bindint table (plus MAX_RTS for VS).
379 */
380 unsigned bias;
381 switch (stage) {
382 case MESA_SHADER_FRAGMENT:
383 bias = MAX_RTS;
384 break;
385 case MESA_SHADER_COMPUTE:
386 bias = 1;
387 break;
388 default:
389 bias = 0;
390 break;
391 }
392 prog_data->binding_table.size_bytes = 0;
393 prog_data->binding_table.texture_start = bias;
394 prog_data->binding_table.ubo_start = bias;
395 prog_data->binding_table.ssbo_start = bias;
396 prog_data->binding_table.image_start = bias;
397
398 /* Finish the optimization and compilation process */
399 if (nir->stage != MESA_SHADER_VERTEX &&
400 nir->stage != MESA_SHADER_TESS_CTRL &&
401 nir->stage != MESA_SHADER_TESS_EVAL &&
402 nir->stage != MESA_SHADER_FRAGMENT) {
403 nir = brw_nir_lower_io(nir, &pipeline->device->info,
404 compiler->scalar_stage[stage], false, NULL);
405 }
406
407 /* nir_lower_io will only handle the push constants; we need to set this
408 * to the full number of possible uniforms.
409 */
410 nir->num_uniforms = prog_data->nr_params * 4;
411
412 return nir;
413 }
414
415 static void
416 anv_pipeline_add_compiled_stage(struct anv_pipeline *pipeline,
417 gl_shader_stage stage,
418 struct brw_stage_prog_data *prog_data)
419 {
420 struct brw_device_info *devinfo = &pipeline->device->info;
421 uint32_t max_threads[] = {
422 [MESA_SHADER_VERTEX] = devinfo->max_vs_threads,
423 [MESA_SHADER_TESS_CTRL] = 0,
424 [MESA_SHADER_TESS_EVAL] = 0,
425 [MESA_SHADER_GEOMETRY] = devinfo->max_gs_threads,
426 [MESA_SHADER_FRAGMENT] = devinfo->max_wm_threads,
427 [MESA_SHADER_COMPUTE] = devinfo->max_cs_threads,
428 };
429
430 pipeline->prog_data[stage] = prog_data;
431 pipeline->active_stages |= mesa_to_vk_shader_stage(stage);
432 pipeline->scratch_start[stage] = pipeline->total_scratch;
433 pipeline->total_scratch =
434 align_u32(pipeline->total_scratch, 1024) +
435 prog_data->total_scratch * max_threads[stage];
436 }
437
438 static VkResult
439 anv_pipeline_compile_vs(struct anv_pipeline *pipeline,
440 struct anv_pipeline_cache *cache,
441 const VkGraphicsPipelineCreateInfo *info,
442 struct anv_shader_module *module,
443 const char *entrypoint,
444 const VkSpecializationInfo *spec_info)
445 {
446 const struct brw_compiler *compiler =
447 pipeline->device->instance->physicalDevice.compiler;
448 struct brw_vs_prog_data *prog_data = &pipeline->vs_prog_data;
449 struct brw_vs_prog_key key;
450 uint32_t kernel;
451 unsigned char sha1[20], *hash;
452
453 populate_vs_prog_key(&pipeline->device->info, &key);
454
455 if (module->size > 0) {
456 hash = sha1;
457 anv_hash_shader(hash, &key, sizeof(key), module, entrypoint, spec_info);
458 kernel = anv_pipeline_cache_search(cache, hash, prog_data);
459 } else {
460 hash = NULL;
461 }
462
463 if (module->size == 0 || kernel == NO_KERNEL) {
464 memset(prog_data, 0, sizeof(*prog_data));
465
466 nir_shader *nir = anv_pipeline_compile(pipeline, module, entrypoint,
467 MESA_SHADER_VERTEX, spec_info,
468 &prog_data->base.base);
469 if (nir == NULL)
470 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
471
472 void *mem_ctx = ralloc_context(NULL);
473
474 if (module->nir == NULL)
475 ralloc_steal(mem_ctx, nir);
476
477 prog_data->inputs_read = nir->info.inputs_read;
478 if (nir->info.outputs_written & (1ull << VARYING_SLOT_PSIZ))
479 pipeline->writes_point_size = true;
480
481 brw_compute_vue_map(&pipeline->device->info,
482 &prog_data->base.vue_map,
483 nir->info.outputs_written,
484 nir->info.separate_shader);
485
486 unsigned code_size;
487 const unsigned *shader_code =
488 brw_compile_vs(compiler, NULL, mem_ctx, &key, prog_data, nir,
489 NULL, false, -1, &code_size, NULL);
490 if (shader_code == NULL) {
491 ralloc_free(mem_ctx);
492 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
493 }
494
495 kernel = anv_pipeline_cache_upload_kernel(cache, hash,
496 shader_code, code_size,
497 prog_data, sizeof(*prog_data));
498 ralloc_free(mem_ctx);
499 }
500
501 if (prog_data->base.dispatch_mode == DISPATCH_MODE_SIMD8) {
502 pipeline->vs_simd8 = kernel;
503 pipeline->vs_vec4 = NO_KERNEL;
504 } else {
505 pipeline->vs_simd8 = NO_KERNEL;
506 pipeline->vs_vec4 = kernel;
507 }
508
509 anv_pipeline_add_compiled_stage(pipeline, MESA_SHADER_VERTEX,
510 &prog_data->base.base);
511
512 return VK_SUCCESS;
513 }
514
515 static VkResult
516 anv_pipeline_compile_gs(struct anv_pipeline *pipeline,
517 struct anv_pipeline_cache *cache,
518 const VkGraphicsPipelineCreateInfo *info,
519 struct anv_shader_module *module,
520 const char *entrypoint,
521 const VkSpecializationInfo *spec_info)
522 {
523 const struct brw_compiler *compiler =
524 pipeline->device->instance->physicalDevice.compiler;
525 struct brw_gs_prog_data *prog_data = &pipeline->gs_prog_data;
526 struct brw_gs_prog_key key;
527 uint32_t kernel;
528 unsigned char sha1[20], *hash;
529
530 populate_gs_prog_key(&pipeline->device->info, &key);
531
532 if (module->size > 0) {
533 hash = sha1;
534 anv_hash_shader(hash, &key, sizeof(key), module, entrypoint, spec_info);
535 kernel = anv_pipeline_cache_search(cache, hash, prog_data);
536 } else {
537 hash = NULL;
538 }
539
540 if (module->size == 0 || kernel == NO_KERNEL) {
541 memset(prog_data, 0, sizeof(*prog_data));
542
543 nir_shader *nir = anv_pipeline_compile(pipeline, module, entrypoint,
544 MESA_SHADER_GEOMETRY, spec_info,
545 &prog_data->base.base);
546 if (nir == NULL)
547 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
548
549 void *mem_ctx = ralloc_context(NULL);
550
551 if (module->nir == NULL)
552 ralloc_steal(mem_ctx, nir);
553
554 if (nir->info.outputs_written & (1ull << VARYING_SLOT_PSIZ))
555 pipeline->writes_point_size = true;
556
557 brw_compute_vue_map(&pipeline->device->info,
558 &prog_data->base.vue_map,
559 nir->info.outputs_written,
560 nir->info.separate_shader);
561
562 unsigned code_size;
563 const unsigned *shader_code =
564 brw_compile_gs(compiler, NULL, mem_ctx, &key, prog_data, nir,
565 NULL, -1, &code_size, NULL);
566 if (shader_code == NULL) {
567 ralloc_free(mem_ctx);
568 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
569 }
570
571 /* TODO: SIMD8 GS */
572 kernel = anv_pipeline_cache_upload_kernel(cache, hash,
573 shader_code, code_size,
574 prog_data, sizeof(*prog_data));
575
576 ralloc_free(mem_ctx);
577 }
578
579 pipeline->gs_kernel = kernel;
580
581 anv_pipeline_add_compiled_stage(pipeline, MESA_SHADER_GEOMETRY,
582 &prog_data->base.base);
583
584 return VK_SUCCESS;
585 }
586
587 static VkResult
588 anv_pipeline_compile_fs(struct anv_pipeline *pipeline,
589 struct anv_pipeline_cache *cache,
590 const VkGraphicsPipelineCreateInfo *info,
591 const struct anv_graphics_pipeline_create_info *extra,
592 struct anv_shader_module *module,
593 const char *entrypoint,
594 const VkSpecializationInfo *spec_info)
595 {
596 const struct brw_compiler *compiler =
597 pipeline->device->instance->physicalDevice.compiler;
598 struct brw_wm_prog_data *prog_data = &pipeline->wm_prog_data;
599 struct brw_wm_prog_key key;
600 uint32_t kernel;
601 unsigned char sha1[20], *hash;
602
603 populate_wm_prog_key(&pipeline->device->info, info, extra, &key);
604
605 if (pipeline->use_repclear)
606 key.nr_color_regions = 1;
607
608 if (module->size > 0) {
609 hash = sha1;
610 anv_hash_shader(hash, &key, sizeof(key), module, entrypoint, spec_info);
611 kernel = anv_pipeline_cache_search(cache, hash, prog_data);
612 } else {
613 hash = NULL;
614 }
615
616 if (module->size == 0 || kernel == NO_KERNEL) {
617 memset(prog_data, 0, sizeof(*prog_data));
618
619 prog_data->binding_table.render_target_start = 0;
620
621 nir_shader *nir = anv_pipeline_compile(pipeline, module, entrypoint,
622 MESA_SHADER_FRAGMENT, spec_info,
623 &prog_data->base);
624 if (nir == NULL)
625 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
626
627 nir_function_impl *impl = nir_shader_get_entrypoint(nir)->impl;
628 nir_foreach_variable_safe(var, &nir->outputs) {
629 if (var->data.location < FRAG_RESULT_DATA0)
630 continue;
631
632 unsigned rt = var->data.location - FRAG_RESULT_DATA0;
633 if (rt >= key.nr_color_regions) {
634 var->data.mode = nir_var_local;
635 exec_node_remove(&var->node);
636 exec_list_push_tail(&impl->locals, &var->node);
637 }
638 }
639
640 void *mem_ctx = ralloc_context(NULL);
641
642 if (module->nir == NULL)
643 ralloc_steal(mem_ctx, nir);
644
645 unsigned code_size;
646 const unsigned *shader_code =
647 brw_compile_fs(compiler, NULL, mem_ctx, &key, prog_data, nir,
648 NULL, -1, -1, pipeline->use_repclear, &code_size, NULL);
649 if (shader_code == NULL) {
650 ralloc_free(mem_ctx);
651 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
652 }
653
654 kernel = anv_pipeline_cache_upload_kernel(cache, hash,
655 shader_code, code_size,
656 prog_data, sizeof(*prog_data));
657
658 ralloc_free(mem_ctx);
659 }
660
661 if (prog_data->no_8)
662 pipeline->ps_simd8 = NO_KERNEL;
663 else
664 pipeline->ps_simd8 = kernel;
665
666 if (prog_data->no_8 || prog_data->prog_offset_16) {
667 pipeline->ps_simd16 = kernel + prog_data->prog_offset_16;
668 } else {
669 pipeline->ps_simd16 = NO_KERNEL;
670 }
671
672 pipeline->ps_ksp2 = 0;
673 pipeline->ps_grf_start2 = 0;
674 if (pipeline->ps_simd8 != NO_KERNEL) {
675 pipeline->ps_ksp0 = pipeline->ps_simd8;
676 pipeline->ps_grf_start0 = prog_data->base.dispatch_grf_start_reg;
677 if (pipeline->ps_simd16 != NO_KERNEL) {
678 pipeline->ps_ksp2 = pipeline->ps_simd16;
679 pipeline->ps_grf_start2 = prog_data->dispatch_grf_start_reg_16;
680 }
681 } else if (pipeline->ps_simd16 != NO_KERNEL) {
682 pipeline->ps_ksp0 = pipeline->ps_simd16;
683 pipeline->ps_grf_start0 = prog_data->dispatch_grf_start_reg_16;
684 }
685
686 anv_pipeline_add_compiled_stage(pipeline, MESA_SHADER_FRAGMENT,
687 &prog_data->base);
688
689 return VK_SUCCESS;
690 }
691
692 VkResult
693 anv_pipeline_compile_cs(struct anv_pipeline *pipeline,
694 struct anv_pipeline_cache *cache,
695 const VkComputePipelineCreateInfo *info,
696 struct anv_shader_module *module,
697 const char *entrypoint,
698 const VkSpecializationInfo *spec_info)
699 {
700 const struct brw_compiler *compiler =
701 pipeline->device->instance->physicalDevice.compiler;
702 struct brw_cs_prog_data *prog_data = &pipeline->cs_prog_data;
703 struct brw_cs_prog_key key;
704 uint32_t kernel;
705 unsigned char sha1[20], *hash;
706
707 populate_cs_prog_key(&pipeline->device->info, &key);
708
709 if (module->size > 0) {
710 hash = sha1;
711 anv_hash_shader(hash, &key, sizeof(key), module, entrypoint, spec_info);
712 kernel = anv_pipeline_cache_search(cache, hash, prog_data);
713 } else {
714 hash = NULL;
715 }
716
717 if (module->size == 0 || kernel == NO_KERNEL) {
718 memset(prog_data, 0, sizeof(*prog_data));
719
720 prog_data->binding_table.work_groups_start = 0;
721
722 nir_shader *nir = anv_pipeline_compile(pipeline, module, entrypoint,
723 MESA_SHADER_COMPUTE, spec_info,
724 &prog_data->base);
725 if (nir == NULL)
726 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
727
728 prog_data->base.total_shared = nir->num_shared;
729
730 void *mem_ctx = ralloc_context(NULL);
731
732 if (module->nir == NULL)
733 ralloc_steal(mem_ctx, nir);
734
735 unsigned code_size;
736 const unsigned *shader_code =
737 brw_compile_cs(compiler, NULL, mem_ctx, &key, prog_data, nir,
738 -1, &code_size, NULL);
739 if (shader_code == NULL) {
740 ralloc_free(mem_ctx);
741 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
742 }
743
744 kernel = anv_pipeline_cache_upload_kernel(cache, hash,
745 shader_code, code_size,
746 prog_data, sizeof(*prog_data));
747 ralloc_free(mem_ctx);
748 }
749
750 pipeline->cs_simd = kernel;
751
752 anv_pipeline_add_compiled_stage(pipeline, MESA_SHADER_COMPUTE,
753 &prog_data->base);
754
755 return VK_SUCCESS;
756 }
757
758 static const int gen8_push_size = 32 * 1024;
759
760 static void
761 gen7_compute_urb_partition(struct anv_pipeline *pipeline)
762 {
763 const struct brw_device_info *devinfo = &pipeline->device->info;
764 bool vs_present = pipeline->active_stages & VK_SHADER_STAGE_VERTEX_BIT;
765 unsigned vs_size = vs_present ? pipeline->vs_prog_data.base.urb_entry_size : 1;
766 unsigned vs_entry_size_bytes = vs_size * 64;
767 bool gs_present = pipeline->active_stages & VK_SHADER_STAGE_GEOMETRY_BIT;
768 unsigned gs_size = gs_present ? pipeline->gs_prog_data.base.urb_entry_size : 1;
769 unsigned gs_entry_size_bytes = gs_size * 64;
770
771 /* From p35 of the Ivy Bridge PRM (section 1.7.1: 3DSTATE_URB_GS):
772 *
773 * VS Number of URB Entries must be divisible by 8 if the VS URB Entry
774 * Allocation Size is less than 9 512-bit URB entries.
775 *
776 * Similar text exists for GS.
777 */
778 unsigned vs_granularity = (vs_size < 9) ? 8 : 1;
779 unsigned gs_granularity = (gs_size < 9) ? 8 : 1;
780
781 /* URB allocations must be done in 8k chunks. */
782 unsigned chunk_size_bytes = 8192;
783
784 /* Determine the size of the URB in chunks. */
785 unsigned urb_chunks = devinfo->urb.size * 1024 / chunk_size_bytes;
786
787 /* Reserve space for push constants */
788 unsigned push_constant_bytes = gen8_push_size;
789 unsigned push_constant_chunks =
790 push_constant_bytes / chunk_size_bytes;
791
792 /* Initially, assign each stage the minimum amount of URB space it needs,
793 * and make a note of how much additional space it "wants" (the amount of
794 * additional space it could actually make use of).
795 */
796
797 /* VS has a lower limit on the number of URB entries */
798 unsigned vs_chunks =
799 ALIGN(devinfo->urb.min_vs_entries * vs_entry_size_bytes,
800 chunk_size_bytes) / chunk_size_bytes;
801 unsigned vs_wants =
802 ALIGN(devinfo->urb.max_vs_entries * vs_entry_size_bytes,
803 chunk_size_bytes) / chunk_size_bytes - vs_chunks;
804
805 unsigned gs_chunks = 0;
806 unsigned gs_wants = 0;
807 if (gs_present) {
808 /* There are two constraints on the minimum amount of URB space we can
809 * allocate:
810 *
811 * (1) We need room for at least 2 URB entries, since we always operate
812 * the GS in DUAL_OBJECT mode.
813 *
814 * (2) We can't allocate less than nr_gs_entries_granularity.
815 */
816 gs_chunks = ALIGN(MAX2(gs_granularity, 2) * gs_entry_size_bytes,
817 chunk_size_bytes) / chunk_size_bytes;
818 gs_wants =
819 ALIGN(devinfo->urb.max_gs_entries * gs_entry_size_bytes,
820 chunk_size_bytes) / chunk_size_bytes - gs_chunks;
821 }
822
823 /* There should always be enough URB space to satisfy the minimum
824 * requirements of each stage.
825 */
826 unsigned total_needs = push_constant_chunks + vs_chunks + gs_chunks;
827 assert(total_needs <= urb_chunks);
828
829 /* Mete out remaining space (if any) in proportion to "wants". */
830 unsigned total_wants = vs_wants + gs_wants;
831 unsigned remaining_space = urb_chunks - total_needs;
832 if (remaining_space > total_wants)
833 remaining_space = total_wants;
834 if (remaining_space > 0) {
835 unsigned vs_additional = (unsigned)
836 round(vs_wants * (((double) remaining_space) / total_wants));
837 vs_chunks += vs_additional;
838 remaining_space -= vs_additional;
839 gs_chunks += remaining_space;
840 }
841
842 /* Sanity check that we haven't over-allocated. */
843 assert(push_constant_chunks + vs_chunks + gs_chunks <= urb_chunks);
844
845 /* Finally, compute the number of entries that can fit in the space
846 * allocated to each stage.
847 */
848 unsigned nr_vs_entries = vs_chunks * chunk_size_bytes / vs_entry_size_bytes;
849 unsigned nr_gs_entries = gs_chunks * chunk_size_bytes / gs_entry_size_bytes;
850
851 /* Since we rounded up when computing *_wants, this may be slightly more
852 * than the maximum allowed amount, so correct for that.
853 */
854 nr_vs_entries = MIN2(nr_vs_entries, devinfo->urb.max_vs_entries);
855 nr_gs_entries = MIN2(nr_gs_entries, devinfo->urb.max_gs_entries);
856
857 /* Ensure that we program a multiple of the granularity. */
858 nr_vs_entries = ROUND_DOWN_TO(nr_vs_entries, vs_granularity);
859 nr_gs_entries = ROUND_DOWN_TO(nr_gs_entries, gs_granularity);
860
861 /* Finally, sanity check to make sure we have at least the minimum number
862 * of entries needed for each stage.
863 */
864 assert(nr_vs_entries >= devinfo->urb.min_vs_entries);
865 if (gs_present)
866 assert(nr_gs_entries >= 2);
867
868 /* Lay out the URB in the following order:
869 * - push constants
870 * - VS
871 * - GS
872 */
873 pipeline->urb.start[MESA_SHADER_VERTEX] = push_constant_chunks;
874 pipeline->urb.size[MESA_SHADER_VERTEX] = vs_size;
875 pipeline->urb.entries[MESA_SHADER_VERTEX] = nr_vs_entries;
876
877 pipeline->urb.start[MESA_SHADER_GEOMETRY] = push_constant_chunks + vs_chunks;
878 pipeline->urb.size[MESA_SHADER_GEOMETRY] = gs_size;
879 pipeline->urb.entries[MESA_SHADER_GEOMETRY] = nr_gs_entries;
880
881 pipeline->urb.start[MESA_SHADER_TESS_CTRL] = push_constant_chunks;
882 pipeline->urb.size[MESA_SHADER_TESS_CTRL] = 1;
883 pipeline->urb.entries[MESA_SHADER_TESS_CTRL] = 0;
884
885 pipeline->urb.start[MESA_SHADER_TESS_EVAL] = push_constant_chunks;
886 pipeline->urb.size[MESA_SHADER_TESS_EVAL] = 1;
887 pipeline->urb.entries[MESA_SHADER_TESS_EVAL] = 0;
888
889 pipeline->urb.push_size[MESA_SHADER_VERTEX] = 4;
890 pipeline->urb.push_size[MESA_SHADER_TESS_CTRL] = 0;
891 pipeline->urb.push_size[MESA_SHADER_TESS_EVAL] = 0;
892 pipeline->urb.push_size[MESA_SHADER_GEOMETRY] = 4;
893 pipeline->urb.push_size[MESA_SHADER_FRAGMENT] = 4;
894 }
895
896 static void
897 anv_pipeline_init_dynamic_state(struct anv_pipeline *pipeline,
898 const VkGraphicsPipelineCreateInfo *pCreateInfo)
899 {
900 anv_cmd_dirty_mask_t states = ANV_CMD_DIRTY_DYNAMIC_ALL;
901 ANV_FROM_HANDLE(anv_render_pass, pass, pCreateInfo->renderPass);
902 struct anv_subpass *subpass = &pass->subpasses[pCreateInfo->subpass];
903
904 pipeline->dynamic_state = default_dynamic_state;
905
906 if (pCreateInfo->pDynamicState) {
907 /* Remove all of the states that are marked as dynamic */
908 uint32_t count = pCreateInfo->pDynamicState->dynamicStateCount;
909 for (uint32_t s = 0; s < count; s++)
910 states &= ~(1 << pCreateInfo->pDynamicState->pDynamicStates[s]);
911 }
912
913 struct anv_dynamic_state *dynamic = &pipeline->dynamic_state;
914
915 dynamic->viewport.count = pCreateInfo->pViewportState->viewportCount;
916 if (states & (1 << VK_DYNAMIC_STATE_VIEWPORT)) {
917 typed_memcpy(dynamic->viewport.viewports,
918 pCreateInfo->pViewportState->pViewports,
919 pCreateInfo->pViewportState->viewportCount);
920 }
921
922 dynamic->scissor.count = pCreateInfo->pViewportState->scissorCount;
923 if (states & (1 << VK_DYNAMIC_STATE_SCISSOR)) {
924 typed_memcpy(dynamic->scissor.scissors,
925 pCreateInfo->pViewportState->pScissors,
926 pCreateInfo->pViewportState->scissorCount);
927 }
928
929 if (states & (1 << VK_DYNAMIC_STATE_LINE_WIDTH)) {
930 assert(pCreateInfo->pRasterizationState);
931 dynamic->line_width = pCreateInfo->pRasterizationState->lineWidth;
932 }
933
934 if (states & (1 << VK_DYNAMIC_STATE_DEPTH_BIAS)) {
935 assert(pCreateInfo->pRasterizationState);
936 dynamic->depth_bias.bias =
937 pCreateInfo->pRasterizationState->depthBiasConstantFactor;
938 dynamic->depth_bias.clamp =
939 pCreateInfo->pRasterizationState->depthBiasClamp;
940 dynamic->depth_bias.slope =
941 pCreateInfo->pRasterizationState->depthBiasSlopeFactor;
942 }
943
944 if (states & (1 << VK_DYNAMIC_STATE_BLEND_CONSTANTS)) {
945 assert(pCreateInfo->pColorBlendState);
946 typed_memcpy(dynamic->blend_constants,
947 pCreateInfo->pColorBlendState->blendConstants, 4);
948 }
949
950 /* If there is no depthstencil attachment, then don't read
951 * pDepthStencilState. The Vulkan spec states that pDepthStencilState may
952 * be NULL in this case. Even if pDepthStencilState is non-NULL, there is
953 * no need to override the depthstencil defaults in
954 * anv_pipeline::dynamic_state when there is no depthstencil attachment.
955 *
956 * From the Vulkan spec (20 Oct 2015, git-aa308cb):
957 *
958 * pDepthStencilState [...] may only be NULL if renderPass and subpass
959 * specify a subpass that has no depth/stencil attachment.
960 */
961 if (subpass->depth_stencil_attachment != VK_ATTACHMENT_UNUSED) {
962 if (states & (1 << VK_DYNAMIC_STATE_DEPTH_BOUNDS)) {
963 assert(pCreateInfo->pDepthStencilState);
964 dynamic->depth_bounds.min =
965 pCreateInfo->pDepthStencilState->minDepthBounds;
966 dynamic->depth_bounds.max =
967 pCreateInfo->pDepthStencilState->maxDepthBounds;
968 }
969
970 if (states & (1 << VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK)) {
971 assert(pCreateInfo->pDepthStencilState);
972 dynamic->stencil_compare_mask.front =
973 pCreateInfo->pDepthStencilState->front.compareMask;
974 dynamic->stencil_compare_mask.back =
975 pCreateInfo->pDepthStencilState->back.compareMask;
976 }
977
978 if (states & (1 << VK_DYNAMIC_STATE_STENCIL_WRITE_MASK)) {
979 assert(pCreateInfo->pDepthStencilState);
980 dynamic->stencil_write_mask.front =
981 pCreateInfo->pDepthStencilState->front.writeMask;
982 dynamic->stencil_write_mask.back =
983 pCreateInfo->pDepthStencilState->back.writeMask;
984 }
985
986 if (states & (1 << VK_DYNAMIC_STATE_STENCIL_REFERENCE)) {
987 assert(pCreateInfo->pDepthStencilState);
988 dynamic->stencil_reference.front =
989 pCreateInfo->pDepthStencilState->front.reference;
990 dynamic->stencil_reference.back =
991 pCreateInfo->pDepthStencilState->back.reference;
992 }
993 }
994
995 pipeline->dynamic_state_mask = states;
996 }
997
998 static void
999 anv_pipeline_validate_create_info(const VkGraphicsPipelineCreateInfo *info)
1000 {
1001 struct anv_render_pass *renderpass = NULL;
1002 struct anv_subpass *subpass = NULL;
1003
1004 /* Assert that all required members of VkGraphicsPipelineCreateInfo are
1005 * present, as explained by the Vulkan (20 Oct 2015, git-aa308cb), Section
1006 * 4.2 Graphics Pipeline.
1007 */
1008 assert(info->sType == VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO);
1009
1010 renderpass = anv_render_pass_from_handle(info->renderPass);
1011 assert(renderpass);
1012
1013 if (renderpass != &anv_meta_dummy_renderpass) {
1014 assert(info->subpass < renderpass->subpass_count);
1015 subpass = &renderpass->subpasses[info->subpass];
1016 }
1017
1018 assert(info->stageCount >= 1);
1019 assert(info->pVertexInputState);
1020 assert(info->pInputAssemblyState);
1021 assert(info->pViewportState);
1022 assert(info->pRasterizationState);
1023
1024 if (subpass && subpass->depth_stencil_attachment != VK_ATTACHMENT_UNUSED)
1025 assert(info->pDepthStencilState);
1026
1027 if (subpass && subpass->color_count > 0)
1028 assert(info->pColorBlendState);
1029
1030 for (uint32_t i = 0; i < info->stageCount; ++i) {
1031 switch (info->pStages[i].stage) {
1032 case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT:
1033 case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT:
1034 assert(info->pTessellationState);
1035 break;
1036 default:
1037 break;
1038 }
1039 }
1040 }
1041
1042 VkResult
1043 anv_pipeline_init(struct anv_pipeline *pipeline,
1044 struct anv_device *device,
1045 struct anv_pipeline_cache *cache,
1046 const VkGraphicsPipelineCreateInfo *pCreateInfo,
1047 const struct anv_graphics_pipeline_create_info *extra,
1048 const VkAllocationCallbacks *alloc)
1049 {
1050 VkResult result;
1051
1052 anv_validate {
1053 anv_pipeline_validate_create_info(pCreateInfo);
1054 }
1055
1056 if (alloc == NULL)
1057 alloc = &device->alloc;
1058
1059 pipeline->device = device;
1060 pipeline->layout = anv_pipeline_layout_from_handle(pCreateInfo->layout);
1061
1062 result = anv_reloc_list_init(&pipeline->batch_relocs, alloc);
1063 if (result != VK_SUCCESS)
1064 return result;
1065
1066 pipeline->batch.alloc = alloc;
1067 pipeline->batch.next = pipeline->batch.start = pipeline->batch_data;
1068 pipeline->batch.end = pipeline->batch.start + sizeof(pipeline->batch_data);
1069 pipeline->batch.relocs = &pipeline->batch_relocs;
1070
1071 anv_pipeline_init_dynamic_state(pipeline, pCreateInfo);
1072
1073 if (pCreateInfo->pTessellationState)
1074 anv_finishme("VK_STRUCTURE_TYPE_PIPELINE_TESSELLATION_STATE_CREATE_INFO");
1075
1076 pipeline->use_repclear = extra && extra->use_repclear;
1077 pipeline->writes_point_size = false;
1078
1079 /* When we free the pipeline, we detect stages based on the NULL status
1080 * of various prog_data pointers. Make them NULL by default.
1081 */
1082 memset(pipeline->prog_data, 0, sizeof(pipeline->prog_data));
1083 memset(pipeline->scratch_start, 0, sizeof(pipeline->scratch_start));
1084 memset(pipeline->bindings, 0, sizeof(pipeline->bindings));
1085
1086 pipeline->vs_simd8 = NO_KERNEL;
1087 pipeline->vs_vec4 = NO_KERNEL;
1088 pipeline->gs_kernel = NO_KERNEL;
1089 pipeline->ps_ksp0 = NO_KERNEL;
1090
1091 pipeline->active_stages = 0;
1092 pipeline->total_scratch = 0;
1093
1094 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
1095 ANV_FROM_HANDLE(anv_shader_module, module,
1096 pCreateInfo->pStages[i].module);
1097
1098 switch (pCreateInfo->pStages[i].stage) {
1099 case VK_SHADER_STAGE_VERTEX_BIT:
1100 anv_pipeline_compile_vs(pipeline, cache, pCreateInfo, module,
1101 pCreateInfo->pStages[i].pName,
1102 pCreateInfo->pStages[i].pSpecializationInfo);
1103 break;
1104 case VK_SHADER_STAGE_GEOMETRY_BIT:
1105 anv_pipeline_compile_gs(pipeline, cache, pCreateInfo, module,
1106 pCreateInfo->pStages[i].pName,
1107 pCreateInfo->pStages[i].pSpecializationInfo);
1108 break;
1109 case VK_SHADER_STAGE_FRAGMENT_BIT:
1110 anv_pipeline_compile_fs(pipeline, cache, pCreateInfo, extra, module,
1111 pCreateInfo->pStages[i].pName,
1112 pCreateInfo->pStages[i].pSpecializationInfo);
1113 break;
1114 default:
1115 anv_finishme("Unsupported shader stage");
1116 }
1117 }
1118
1119 if (!(pipeline->active_stages & VK_SHADER_STAGE_VERTEX_BIT)) {
1120 /* Vertex is only optional if disable_vs is set */
1121 assert(extra->disable_vs);
1122 memset(&pipeline->vs_prog_data, 0, sizeof(pipeline->vs_prog_data));
1123 }
1124
1125 gen7_compute_urb_partition(pipeline);
1126
1127 const VkPipelineVertexInputStateCreateInfo *vi_info =
1128 pCreateInfo->pVertexInputState;
1129
1130 uint64_t inputs_read;
1131 if (extra && extra->disable_vs) {
1132 /* If the VS is disabled, just assume the user knows what they're
1133 * doing and apply the layout blindly. This can only come from
1134 * meta, so this *should* be safe.
1135 */
1136 inputs_read = ~0ull;
1137 } else {
1138 inputs_read = pipeline->vs_prog_data.inputs_read;
1139 }
1140
1141 pipeline->vb_used = 0;
1142 for (uint32_t i = 0; i < vi_info->vertexAttributeDescriptionCount; i++) {
1143 const VkVertexInputAttributeDescription *desc =
1144 &vi_info->pVertexAttributeDescriptions[i];
1145
1146 if (inputs_read & (1 << (VERT_ATTRIB_GENERIC0 + desc->location)))
1147 pipeline->vb_used |= 1 << desc->binding;
1148 }
1149
1150 for (uint32_t i = 0; i < vi_info->vertexBindingDescriptionCount; i++) {
1151 const VkVertexInputBindingDescription *desc =
1152 &vi_info->pVertexBindingDescriptions[i];
1153
1154 pipeline->binding_stride[desc->binding] = desc->stride;
1155
1156 /* Step rate is programmed per vertex element (attribute), not
1157 * binding. Set up a map of which bindings step per instance, for
1158 * reference by vertex element setup. */
1159 switch (desc->inputRate) {
1160 default:
1161 case VK_VERTEX_INPUT_RATE_VERTEX:
1162 pipeline->instancing_enable[desc->binding] = false;
1163 break;
1164 case VK_VERTEX_INPUT_RATE_INSTANCE:
1165 pipeline->instancing_enable[desc->binding] = true;
1166 break;
1167 }
1168 }
1169
1170 const VkPipelineInputAssemblyStateCreateInfo *ia_info =
1171 pCreateInfo->pInputAssemblyState;
1172 pipeline->primitive_restart = ia_info->primitiveRestartEnable;
1173 pipeline->topology = vk_to_gen_primitive_type[ia_info->topology];
1174
1175 if (extra && extra->use_rectlist)
1176 pipeline->topology = _3DPRIM_RECTLIST;
1177
1178 while (anv_block_pool_size(&device->scratch_block_pool) <
1179 pipeline->total_scratch)
1180 anv_block_pool_alloc(&device->scratch_block_pool);
1181
1182 return VK_SUCCESS;
1183 }
1184
1185 VkResult
1186 anv_graphics_pipeline_create(
1187 VkDevice _device,
1188 VkPipelineCache _cache,
1189 const VkGraphicsPipelineCreateInfo *pCreateInfo,
1190 const struct anv_graphics_pipeline_create_info *extra,
1191 const VkAllocationCallbacks *pAllocator,
1192 VkPipeline *pPipeline)
1193 {
1194 ANV_FROM_HANDLE(anv_device, device, _device);
1195 ANV_FROM_HANDLE(anv_pipeline_cache, cache, _cache);
1196
1197 if (cache == NULL)
1198 cache = &device->default_pipeline_cache;
1199
1200 switch (device->info.gen) {
1201 case 7:
1202 if (device->info.is_haswell)
1203 return gen75_graphics_pipeline_create(_device, cache, pCreateInfo, extra, pAllocator, pPipeline);
1204 else
1205 return gen7_graphics_pipeline_create(_device, cache, pCreateInfo, extra, pAllocator, pPipeline);
1206 case 8:
1207 return gen8_graphics_pipeline_create(_device, cache, pCreateInfo, extra, pAllocator, pPipeline);
1208 case 9:
1209 return gen9_graphics_pipeline_create(_device, cache, pCreateInfo, extra, pAllocator, pPipeline);
1210 default:
1211 unreachable("unsupported gen\n");
1212 }
1213 }
1214
1215 VkResult anv_CreateGraphicsPipelines(
1216 VkDevice _device,
1217 VkPipelineCache pipelineCache,
1218 uint32_t count,
1219 const VkGraphicsPipelineCreateInfo* pCreateInfos,
1220 const VkAllocationCallbacks* pAllocator,
1221 VkPipeline* pPipelines)
1222 {
1223 VkResult result = VK_SUCCESS;
1224
1225 unsigned i = 0;
1226 for (; i < count; i++) {
1227 result = anv_graphics_pipeline_create(_device,
1228 pipelineCache,
1229 &pCreateInfos[i],
1230 NULL, pAllocator, &pPipelines[i]);
1231 if (result != VK_SUCCESS) {
1232 for (unsigned j = 0; j < i; j++) {
1233 anv_DestroyPipeline(_device, pPipelines[j], pAllocator);
1234 }
1235
1236 return result;
1237 }
1238 }
1239
1240 return VK_SUCCESS;
1241 }
1242
1243 static VkResult anv_compute_pipeline_create(
1244 VkDevice _device,
1245 VkPipelineCache _cache,
1246 const VkComputePipelineCreateInfo* pCreateInfo,
1247 const VkAllocationCallbacks* pAllocator,
1248 VkPipeline* pPipeline)
1249 {
1250 ANV_FROM_HANDLE(anv_device, device, _device);
1251 ANV_FROM_HANDLE(anv_pipeline_cache, cache, _cache);
1252
1253 if (cache == NULL)
1254 cache = &device->default_pipeline_cache;
1255
1256 switch (device->info.gen) {
1257 case 7:
1258 if (device->info.is_haswell)
1259 return gen75_compute_pipeline_create(_device, cache, pCreateInfo, pAllocator, pPipeline);
1260 else
1261 return gen7_compute_pipeline_create(_device, cache, pCreateInfo, pAllocator, pPipeline);
1262 case 8:
1263 return gen8_compute_pipeline_create(_device, cache, pCreateInfo, pAllocator, pPipeline);
1264 case 9:
1265 return gen9_compute_pipeline_create(_device, cache, pCreateInfo, pAllocator, pPipeline);
1266 default:
1267 unreachable("unsupported gen\n");
1268 }
1269 }
1270
1271 VkResult anv_CreateComputePipelines(
1272 VkDevice _device,
1273 VkPipelineCache pipelineCache,
1274 uint32_t count,
1275 const VkComputePipelineCreateInfo* pCreateInfos,
1276 const VkAllocationCallbacks* pAllocator,
1277 VkPipeline* pPipelines)
1278 {
1279 VkResult result = VK_SUCCESS;
1280
1281 unsigned i = 0;
1282 for (; i < count; i++) {
1283 result = anv_compute_pipeline_create(_device, pipelineCache,
1284 &pCreateInfos[i],
1285 pAllocator, &pPipelines[i]);
1286 if (result != VK_SUCCESS) {
1287 for (unsigned j = 0; j < i; j++) {
1288 anv_DestroyPipeline(_device, pPipelines[j], pAllocator);
1289 }
1290
1291 return result;
1292 }
1293 }
1294
1295 return VK_SUCCESS;
1296 }