2 * Copyright © 2015 Intel Corporation
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:
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
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
30 #include "util/mesa-sha1.h"
31 #include "anv_private.h"
34 #include "spirv/nir_spirv.h"
36 /* Needed for SWIZZLE macros */
37 #include "program/prog_instruction.h"
41 VkResult
anv_CreateShaderModule(
43 const VkShaderModuleCreateInfo
* pCreateInfo
,
44 const VkAllocationCallbacks
* pAllocator
,
45 VkShaderModule
* pShaderModule
)
47 ANV_FROM_HANDLE(anv_device
, device
, _device
);
48 struct anv_shader_module
*module
;
50 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO
);
51 assert(pCreateInfo
->flags
== 0);
53 module
= anv_alloc2(&device
->alloc
, pAllocator
,
54 sizeof(*module
) + pCreateInfo
->codeSize
, 8,
55 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
57 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
60 module
->size
= pCreateInfo
->codeSize
;
61 memcpy(module
->data
, pCreateInfo
->pCode
, module
->size
);
63 _mesa_sha1_compute(module
->data
, module
->size
, module
->sha1
);
65 *pShaderModule
= anv_shader_module_to_handle(module
);
70 void anv_DestroyShaderModule(
72 VkShaderModule _module
,
73 const VkAllocationCallbacks
* pAllocator
)
75 ANV_FROM_HANDLE(anv_device
, device
, _device
);
76 ANV_FROM_HANDLE(anv_shader_module
, module
, _module
);
78 anv_free2(&device
->alloc
, pAllocator
, module
);
81 #define SPIR_V_MAGIC_NUMBER 0x07230203
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.
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
)
93 if (strcmp(entrypoint_name
, "main") != 0) {
94 anv_finishme("Multiple shaders per module not really supported");
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
;
103 nir_function
*entry_point
;
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 */
109 nir
->options
= nir_options
;
110 nir_validate_shader(nir
);
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
);
116 uint32_t *spirv
= (uint32_t *) module
->data
;
117 assert(spirv
[0] == SPIR_V_MAGIC_NUMBER
);
118 assert(module
->size
% 4 == 0);
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
);
131 spec_entries
[i
].id
= spec_info
->pMapEntries
[i
].constantID
;
132 spec_entries
[i
].data
= *data
;
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
);
145 if (stage
== MESA_SHADER_FRAGMENT
) {
146 nir_lower_wpos_center(nir
);
147 nir_validate_shader(nir
);
150 nir_lower_returns(nir
);
151 nir_validate_shader(nir
);
153 nir_inline_functions(nir
);
154 nir_validate_shader(nir
);
156 /* Pick off the single entrypoint that we want */
157 foreach_list_typed_safe(nir_function
, func
, node
, &nir
->functions
) {
158 if (func
!= entry_point
)
159 exec_node_remove(&func
->node
);
161 assert(exec_list_length(&nir
->functions
) == 1);
162 entry_point
->name
= ralloc_strdup(entry_point
, "main");
164 nir_remove_dead_variables(nir
, nir_var_shader_in
);
165 nir_remove_dead_variables(nir
, nir_var_shader_out
);
166 nir_remove_dead_variables(nir
, nir_var_system_value
);
167 nir_validate_shader(nir
);
169 nir_lower_io_to_temporaries(entry_point
->shader
, entry_point
, true, false);
171 nir_lower_system_values(nir
);
172 nir_validate_shader(nir
);
175 /* Vulkan uses the separate-shader linking model */
176 nir
->info
.separate_shader
= true;
178 nir
= brw_preprocess_nir(compiler
, nir
);
180 nir_shader_gather_info(nir
, entry_point
->impl
);
182 nir_variable_mode indirect_mask
= 0;
183 if (compiler
->glsl_compiler_options
[stage
].EmitNoIndirectInput
)
184 indirect_mask
|= nir_var_shader_in
;
185 if (compiler
->glsl_compiler_options
[stage
].EmitNoIndirectTemp
)
186 indirect_mask
|= nir_var_local
;
188 nir_lower_indirect_derefs(nir
, indirect_mask
);
193 void anv_DestroyPipeline(
195 VkPipeline _pipeline
,
196 const VkAllocationCallbacks
* pAllocator
)
198 ANV_FROM_HANDLE(anv_device
, device
, _device
);
199 ANV_FROM_HANDLE(anv_pipeline
, pipeline
, _pipeline
);
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
);
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 */
223 populate_sampler_prog_key(const struct brw_device_info
*devinfo
,
224 struct brw_sampler_prog_key_data
*key
)
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
;
234 populate_vs_prog_key(const struct brw_device_info
*devinfo
,
235 struct brw_vs_prog_key
*key
)
237 memset(key
, 0, sizeof(*key
));
239 populate_sampler_prog_key(devinfo
, &key
->tex
);
241 /* XXX: Handle vertex input work-arounds */
243 /* XXX: Handle sampler_prog_key */
247 populate_gs_prog_key(const struct brw_device_info
*devinfo
,
248 struct brw_gs_prog_key
*key
)
250 memset(key
, 0, sizeof(*key
));
252 populate_sampler_prog_key(devinfo
, &key
->tex
);
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
)
261 ANV_FROM_HANDLE(anv_render_pass
, render_pass
, info
->renderPass
);
263 memset(key
, 0, sizeof(*key
));
265 populate_sampler_prog_key(devinfo
, &key
->tex
);
267 /* TODO: Fill out key->input_slots_valid */
269 /* Vulkan doesn't specify a default */
270 key
->high_quality_derivatives
= false;
272 /* XXX Vulkan doesn't appear to specify */
273 key
->clamp_fragment_color
= false;
275 /* Vulkan always specifies upper-left coordinates */
276 key
->drawable_height
= 0;
277 key
->render_to_fbo
= false;
279 if (extra
&& extra
->color_attachment_count
>= 0) {
280 key
->nr_color_regions
= extra
->color_attachment_count
;
282 key
->nr_color_regions
=
283 render_pass
->subpasses
[info
->subpass
].color_count
;
286 key
->replicate_alpha
= key
->nr_color_regions
> 1 &&
287 info
->pMultisampleState
&&
288 info
->pMultisampleState
->alphaToCoverageEnable
;
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.
294 key
->persample_interp
=
295 (info
->pMultisampleState
->minSampleShading
*
296 info
->pMultisampleState
->rasterizationSamples
) > 1;
297 key
->multisample_fbo
= true;
302 populate_cs_prog_key(const struct brw_device_info
*devinfo
,
303 struct brw_cs_prog_key
*key
)
305 memset(key
, 0, sizeof(*key
));
307 populate_sampler_prog_key(devinfo
, &key
->tex
);
311 anv_pipeline_compile(struct anv_pipeline
*pipeline
,
312 struct anv_shader_module
*module
,
313 const char *entrypoint
,
314 gl_shader_stage stage
,
315 const VkSpecializationInfo
*spec_info
,
316 struct brw_stage_prog_data
*prog_data
,
317 struct anv_pipeline_bind_map
*map
)
319 nir_shader
*nir
= anv_shader_compile_to_nir(pipeline
->device
,
320 module
, entrypoint
, stage
,
325 anv_nir_lower_push_constants(nir
);
327 /* Figure out the number of parameters */
328 prog_data
->nr_params
= 0;
330 if (nir
->num_uniforms
> 0) {
331 /* If the shader uses any push constants at all, we'll just give
332 * them the maximum possible number
334 prog_data
->nr_params
+= MAX_PUSH_CONSTANTS_SIZE
/ sizeof(float);
337 if (pipeline
->layout
&& pipeline
->layout
->stage
[stage
].has_dynamic_offsets
)
338 prog_data
->nr_params
+= MAX_DYNAMIC_BUFFERS
* 2;
340 if (nir
->info
.num_images
> 0) {
341 prog_data
->nr_params
+= nir
->info
.num_images
* BRW_IMAGE_PARAM_SIZE
;
342 pipeline
->needs_data_cache
= true;
345 if (nir
->info
.num_ssbos
> 0)
346 pipeline
->needs_data_cache
= true;
348 if (prog_data
->nr_params
> 0) {
349 /* XXX: I think we're leaking this */
350 prog_data
->param
= (const union gl_constant_value
**)
351 malloc(prog_data
->nr_params
* sizeof(union gl_constant_value
*));
353 /* We now set the param values to be offsets into a
354 * anv_push_constant_data structure. Since the compiler doesn't
355 * actually dereference any of the gl_constant_value pointers in the
356 * params array, it doesn't really matter what we put here.
358 struct anv_push_constants
*null_data
= NULL
;
359 if (nir
->num_uniforms
> 0) {
360 /* Fill out the push constants section of the param array */
361 for (unsigned i
= 0; i
< MAX_PUSH_CONSTANTS_SIZE
/ sizeof(float); i
++)
362 prog_data
->param
[i
] = (const union gl_constant_value
*)
363 &null_data
->client_data
[i
* sizeof(float)];
367 /* Set up dynamic offsets */
368 anv_nir_apply_dynamic_offsets(pipeline
, nir
, prog_data
);
370 /* Apply the actual pipeline layout to UBOs, SSBOs, and textures */
371 if (pipeline
->layout
)
372 anv_nir_apply_pipeline_layout(pipeline
, nir
, prog_data
, map
);
374 /* nir_lower_io will only handle the push constants; we need to set this
375 * to the full number of possible uniforms.
377 nir
->num_uniforms
= prog_data
->nr_params
* 4;
383 anv_fill_binding_table(struct brw_stage_prog_data
*prog_data
, unsigned bias
)
385 prog_data
->binding_table
.size_bytes
= 0;
386 prog_data
->binding_table
.texture_start
= bias
;
387 prog_data
->binding_table
.ubo_start
= bias
;
388 prog_data
->binding_table
.ssbo_start
= bias
;
389 prog_data
->binding_table
.image_start
= bias
;
393 anv_pipeline_add_compiled_stage(struct anv_pipeline
*pipeline
,
394 gl_shader_stage stage
,
395 const struct brw_stage_prog_data
*prog_data
,
396 struct anv_pipeline_bind_map
*map
)
398 struct brw_device_info
*devinfo
= &pipeline
->device
->info
;
399 uint32_t max_threads
[] = {
400 [MESA_SHADER_VERTEX
] = devinfo
->max_vs_threads
,
401 [MESA_SHADER_TESS_CTRL
] = devinfo
->max_hs_threads
,
402 [MESA_SHADER_TESS_EVAL
] = devinfo
->max_ds_threads
,
403 [MESA_SHADER_GEOMETRY
] = devinfo
->max_gs_threads
,
404 [MESA_SHADER_FRAGMENT
] = devinfo
->max_wm_threads
,
405 [MESA_SHADER_COMPUTE
] = devinfo
->max_cs_threads
,
408 pipeline
->prog_data
[stage
] = prog_data
;
409 pipeline
->active_stages
|= mesa_to_vk_shader_stage(stage
);
410 pipeline
->scratch_start
[stage
] = pipeline
->total_scratch
;
411 pipeline
->total_scratch
=
412 align_u32(pipeline
->total_scratch
, 1024) +
413 prog_data
->total_scratch
* max_threads
[stage
];
414 pipeline
->bindings
[stage
] = *map
;
418 anv_pipeline_compile_vs(struct anv_pipeline
*pipeline
,
419 struct anv_pipeline_cache
*cache
,
420 const VkGraphicsPipelineCreateInfo
*info
,
421 struct anv_shader_module
*module
,
422 const char *entrypoint
,
423 const VkSpecializationInfo
*spec_info
)
425 const struct brw_compiler
*compiler
=
426 pipeline
->device
->instance
->physicalDevice
.compiler
;
427 const struct brw_stage_prog_data
*stage_prog_data
;
428 struct anv_pipeline_bind_map map
;
429 struct brw_vs_prog_key key
;
430 uint32_t kernel
= NO_KERNEL
;
431 unsigned char sha1
[20];
433 populate_vs_prog_key(&pipeline
->device
->info
, &key
);
435 if (module
->size
> 0) {
436 anv_hash_shader(sha1
, &key
, sizeof(key
), module
, entrypoint
, spec_info
);
437 kernel
= anv_pipeline_cache_search(cache
, sha1
, &stage_prog_data
, &map
);
440 if (kernel
== NO_KERNEL
) {
441 struct brw_vs_prog_data prog_data
= { 0, };
442 struct anv_pipeline_binding surface_to_descriptor
[256];
443 struct anv_pipeline_binding sampler_to_descriptor
[256];
445 map
= (struct anv_pipeline_bind_map
) {
446 .surface_to_descriptor
= surface_to_descriptor
,
447 .sampler_to_descriptor
= sampler_to_descriptor
450 nir_shader
*nir
= anv_pipeline_compile(pipeline
, module
, entrypoint
,
451 MESA_SHADER_VERTEX
, spec_info
,
452 &prog_data
.base
.base
, &map
);
454 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
456 anv_fill_binding_table(&prog_data
.base
.base
, 0);
458 void *mem_ctx
= ralloc_context(NULL
);
460 if (module
->nir
== NULL
)
461 ralloc_steal(mem_ctx
, nir
);
463 prog_data
.inputs_read
= nir
->info
.inputs_read
;
465 brw_compute_vue_map(&pipeline
->device
->info
,
466 &prog_data
.base
.vue_map
,
467 nir
->info
.outputs_written
,
468 nir
->info
.separate_shader
);
471 const unsigned *shader_code
=
472 brw_compile_vs(compiler
, NULL
, mem_ctx
, &key
, &prog_data
, nir
,
473 NULL
, false, -1, &code_size
, NULL
);
474 if (shader_code
== NULL
) {
475 ralloc_free(mem_ctx
);
476 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
479 stage_prog_data
= &prog_data
.base
.base
;
480 kernel
= anv_pipeline_cache_upload_kernel(cache
,
481 module
->size
> 0 ? sha1
: NULL
,
482 shader_code
, code_size
,
483 &stage_prog_data
, sizeof(prog_data
),
485 ralloc_free(mem_ctx
);
488 const struct brw_vs_prog_data
*vs_prog_data
=
489 (const struct brw_vs_prog_data
*) stage_prog_data
;
491 if (vs_prog_data
->base
.dispatch_mode
== DISPATCH_MODE_SIMD8
) {
492 pipeline
->vs_simd8
= kernel
;
493 pipeline
->vs_vec4
= NO_KERNEL
;
495 pipeline
->vs_simd8
= NO_KERNEL
;
496 pipeline
->vs_vec4
= kernel
;
499 anv_pipeline_add_compiled_stage(pipeline
, MESA_SHADER_VERTEX
,
500 stage_prog_data
, &map
);
506 anv_pipeline_compile_gs(struct anv_pipeline
*pipeline
,
507 struct anv_pipeline_cache
*cache
,
508 const VkGraphicsPipelineCreateInfo
*info
,
509 struct anv_shader_module
*module
,
510 const char *entrypoint
,
511 const VkSpecializationInfo
*spec_info
)
513 const struct brw_compiler
*compiler
=
514 pipeline
->device
->instance
->physicalDevice
.compiler
;
515 const struct brw_stage_prog_data
*stage_prog_data
;
516 struct anv_pipeline_bind_map map
;
517 struct brw_gs_prog_key key
;
518 uint32_t kernel
= NO_KERNEL
;
519 unsigned char sha1
[20];
521 populate_gs_prog_key(&pipeline
->device
->info
, &key
);
523 if (module
->size
> 0) {
524 anv_hash_shader(sha1
, &key
, sizeof(key
), module
, entrypoint
, spec_info
);
525 kernel
= anv_pipeline_cache_search(cache
, sha1
, &stage_prog_data
, &map
);
528 if (kernel
== NO_KERNEL
) {
529 struct brw_gs_prog_data prog_data
= { 0, };
530 struct anv_pipeline_binding surface_to_descriptor
[256];
531 struct anv_pipeline_binding sampler_to_descriptor
[256];
533 map
= (struct anv_pipeline_bind_map
) {
534 .surface_to_descriptor
= surface_to_descriptor
,
535 .sampler_to_descriptor
= sampler_to_descriptor
538 nir_shader
*nir
= anv_pipeline_compile(pipeline
, module
, entrypoint
,
539 MESA_SHADER_GEOMETRY
, spec_info
,
540 &prog_data
.base
.base
, &map
);
542 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
544 anv_fill_binding_table(&prog_data
.base
.base
, 0);
546 void *mem_ctx
= ralloc_context(NULL
);
548 if (module
->nir
== NULL
)
549 ralloc_steal(mem_ctx
, nir
);
551 brw_compute_vue_map(&pipeline
->device
->info
,
552 &prog_data
.base
.vue_map
,
553 nir
->info
.outputs_written
,
554 nir
->info
.separate_shader
);
557 const unsigned *shader_code
=
558 brw_compile_gs(compiler
, NULL
, mem_ctx
, &key
, &prog_data
, nir
,
559 NULL
, -1, &code_size
, NULL
);
560 if (shader_code
== NULL
) {
561 ralloc_free(mem_ctx
);
562 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
566 stage_prog_data
= &prog_data
.base
.base
;
567 kernel
= anv_pipeline_cache_upload_kernel(cache
,
568 module
->size
> 0 ? sha1
: NULL
,
569 shader_code
, code_size
,
570 &stage_prog_data
, sizeof(prog_data
),
573 ralloc_free(mem_ctx
);
576 pipeline
->gs_kernel
= kernel
;
578 anv_pipeline_add_compiled_stage(pipeline
, MESA_SHADER_GEOMETRY
,
579 stage_prog_data
, &map
);
585 anv_pipeline_compile_fs(struct anv_pipeline
*pipeline
,
586 struct anv_pipeline_cache
*cache
,
587 const VkGraphicsPipelineCreateInfo
*info
,
588 const struct anv_graphics_pipeline_create_info
*extra
,
589 struct anv_shader_module
*module
,
590 const char *entrypoint
,
591 const VkSpecializationInfo
*spec_info
)
593 const struct brw_compiler
*compiler
=
594 pipeline
->device
->instance
->physicalDevice
.compiler
;
595 const struct brw_stage_prog_data
*stage_prog_data
;
596 struct anv_pipeline_bind_map map
;
597 struct brw_wm_prog_key key
;
598 unsigned char sha1
[20];
600 populate_wm_prog_key(&pipeline
->device
->info
, info
, extra
, &key
);
602 if (module
->size
> 0) {
603 anv_hash_shader(sha1
, &key
, sizeof(key
), module
, entrypoint
, spec_info
);
605 anv_pipeline_cache_search(cache
, sha1
, &stage_prog_data
, &map
);
608 if (pipeline
->ps_ksp0
== NO_KERNEL
) {
609 struct brw_wm_prog_data prog_data
= { 0, };
610 struct anv_pipeline_binding surface_to_descriptor
[256];
611 struct anv_pipeline_binding sampler_to_descriptor
[256];
613 map
= (struct anv_pipeline_bind_map
) {
614 .surface_to_descriptor
= surface_to_descriptor
+ 8,
615 .sampler_to_descriptor
= sampler_to_descriptor
618 nir_shader
*nir
= anv_pipeline_compile(pipeline
, module
, entrypoint
,
619 MESA_SHADER_FRAGMENT
, spec_info
,
620 &prog_data
.base
, &map
);
622 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
624 unsigned num_rts
= 0;
625 struct anv_pipeline_binding rt_bindings
[8];
626 nir_function_impl
*impl
= nir_shader_get_entrypoint(nir
)->impl
;
627 nir_foreach_variable_safe(var
, &nir
->outputs
) {
628 if (var
->data
.location
< FRAG_RESULT_DATA0
)
631 unsigned rt
= var
->data
.location
- FRAG_RESULT_DATA0
;
632 if (rt
>= key
.nr_color_regions
) {
633 /* Out-of-bounds, throw it away */
634 var
->data
.mode
= nir_var_local
;
635 exec_node_remove(&var
->node
);
636 exec_list_push_tail(&impl
->locals
, &var
->node
);
640 /* Give it a new, compacted, location */
641 var
->data
.location
= FRAG_RESULT_DATA0
+ num_rts
;
644 glsl_type_is_array(var
->type
) ? glsl_get_length(var
->type
) : 1;
645 assert(num_rts
+ array_len
<= 8);
647 for (unsigned i
= 0; i
< array_len
; i
++) {
648 rt_bindings
[num_rts
] = (struct anv_pipeline_binding
) {
649 .set
= ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS
,
654 num_rts
+= array_len
;
657 if (pipeline
->use_repclear
) {
658 assert(num_rts
== 1);
659 key
.nr_color_regions
= 1;
663 /* If we have no render targets, we need a null render target */
664 rt_bindings
[0] = (struct anv_pipeline_binding
) {
665 .set
= ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS
,
666 .offset
= UINT16_MAX
,
671 assert(num_rts
<= 8);
672 map
.surface_to_descriptor
-= num_rts
;
673 map
.surface_count
+= num_rts
;
674 assert(map
.surface_count
<= 256);
675 memcpy(map
.surface_to_descriptor
, rt_bindings
,
676 num_rts
* sizeof(*rt_bindings
));
678 anv_fill_binding_table(&prog_data
.base
, num_rts
);
680 void *mem_ctx
= ralloc_context(NULL
);
682 if (module
->nir
== NULL
)
683 ralloc_steal(mem_ctx
, nir
);
686 const unsigned *shader_code
=
687 brw_compile_fs(compiler
, NULL
, mem_ctx
, &key
, &prog_data
, nir
,
688 NULL
, -1, -1, true, pipeline
->use_repclear
,
690 if (shader_code
== NULL
) {
691 ralloc_free(mem_ctx
);
692 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
695 stage_prog_data
= &prog_data
.base
;
697 anv_pipeline_cache_upload_kernel(cache
,
698 module
->size
> 0 ? sha1
: NULL
,
699 shader_code
, code_size
,
700 &stage_prog_data
, sizeof(prog_data
),
703 ralloc_free(mem_ctx
);
706 anv_pipeline_add_compiled_stage(pipeline
, MESA_SHADER_FRAGMENT
,
707 stage_prog_data
, &map
);
713 anv_pipeline_compile_cs(struct anv_pipeline
*pipeline
,
714 struct anv_pipeline_cache
*cache
,
715 const VkComputePipelineCreateInfo
*info
,
716 struct anv_shader_module
*module
,
717 const char *entrypoint
,
718 const VkSpecializationInfo
*spec_info
)
720 const struct brw_compiler
*compiler
=
721 pipeline
->device
->instance
->physicalDevice
.compiler
;
722 const struct brw_stage_prog_data
*stage_prog_data
;
723 struct anv_pipeline_bind_map map
;
724 struct brw_cs_prog_key key
;
725 uint32_t kernel
= NO_KERNEL
;
726 unsigned char sha1
[20];
728 populate_cs_prog_key(&pipeline
->device
->info
, &key
);
730 if (module
->size
> 0) {
731 anv_hash_shader(sha1
, &key
, sizeof(key
), module
, entrypoint
, spec_info
);
732 kernel
= anv_pipeline_cache_search(cache
, sha1
, &stage_prog_data
, &map
);
735 if (module
->size
== 0 || kernel
== NO_KERNEL
) {
736 struct brw_cs_prog_data prog_data
= { 0, };
737 struct anv_pipeline_binding surface_to_descriptor
[256];
738 struct anv_pipeline_binding sampler_to_descriptor
[256];
740 map
= (struct anv_pipeline_bind_map
) {
741 .surface_to_descriptor
= surface_to_descriptor
,
742 .sampler_to_descriptor
= sampler_to_descriptor
745 nir_shader
*nir
= anv_pipeline_compile(pipeline
, module
, entrypoint
,
746 MESA_SHADER_COMPUTE
, spec_info
,
747 &prog_data
.base
, &map
);
749 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
751 anv_fill_binding_table(&prog_data
.base
, 1);
753 void *mem_ctx
= ralloc_context(NULL
);
755 if (module
->nir
== NULL
)
756 ralloc_steal(mem_ctx
, nir
);
759 const unsigned *shader_code
=
760 brw_compile_cs(compiler
, NULL
, mem_ctx
, &key
, &prog_data
, nir
,
761 -1, &code_size
, NULL
);
762 if (shader_code
== NULL
) {
763 ralloc_free(mem_ctx
);
764 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
767 stage_prog_data
= &prog_data
.base
;
768 kernel
= anv_pipeline_cache_upload_kernel(cache
,
769 module
->size
> 0 ? sha1
: NULL
,
770 shader_code
, code_size
,
771 &stage_prog_data
, sizeof(prog_data
),
774 ralloc_free(mem_ctx
);
777 pipeline
->cs_simd
= kernel
;
779 anv_pipeline_add_compiled_stage(pipeline
, MESA_SHADER_COMPUTE
,
780 stage_prog_data
, &map
);
787 anv_setup_pipeline_l3_config(struct anv_pipeline
*pipeline
)
789 const struct brw_device_info
*devinfo
= &pipeline
->device
->info
;
790 switch (devinfo
->gen
) {
792 if (devinfo
->is_haswell
)
793 gen75_setup_pipeline_l3_config(pipeline
);
795 gen7_setup_pipeline_l3_config(pipeline
);
798 gen8_setup_pipeline_l3_config(pipeline
);
801 gen9_setup_pipeline_l3_config(pipeline
);
804 unreachable("unsupported gen\n");
809 anv_compute_urb_partition(struct anv_pipeline
*pipeline
)
811 const struct brw_device_info
*devinfo
= &pipeline
->device
->info
;
813 bool vs_present
= pipeline
->active_stages
& VK_SHADER_STAGE_VERTEX_BIT
;
814 unsigned vs_size
= vs_present
?
815 get_vs_prog_data(pipeline
)->base
.urb_entry_size
: 1;
816 unsigned vs_entry_size_bytes
= vs_size
* 64;
817 bool gs_present
= pipeline
->active_stages
& VK_SHADER_STAGE_GEOMETRY_BIT
;
818 unsigned gs_size
= gs_present
?
819 get_gs_prog_data(pipeline
)->base
.urb_entry_size
: 1;
820 unsigned gs_entry_size_bytes
= gs_size
* 64;
822 /* From p35 of the Ivy Bridge PRM (section 1.7.1: 3DSTATE_URB_GS):
824 * VS Number of URB Entries must be divisible by 8 if the VS URB Entry
825 * Allocation Size is less than 9 512-bit URB entries.
827 * Similar text exists for GS.
829 unsigned vs_granularity
= (vs_size
< 9) ? 8 : 1;
830 unsigned gs_granularity
= (gs_size
< 9) ? 8 : 1;
832 /* URB allocations must be done in 8k chunks. */
833 unsigned chunk_size_bytes
= 8192;
835 /* Determine the size of the URB in chunks. */
836 unsigned urb_chunks
= pipeline
->urb
.total_size
* 1024 / chunk_size_bytes
;
838 /* Reserve space for push constants */
839 unsigned push_constant_kb
;
840 if (pipeline
->device
->info
.gen
>= 8)
841 push_constant_kb
= 32;
842 else if (pipeline
->device
->info
.is_haswell
)
843 push_constant_kb
= pipeline
->device
->info
.gt
== 3 ? 32 : 16;
845 push_constant_kb
= 16;
847 unsigned push_constant_bytes
= push_constant_kb
* 1024;
848 unsigned push_constant_chunks
=
849 push_constant_bytes
/ chunk_size_bytes
;
851 /* Initially, assign each stage the minimum amount of URB space it needs,
852 * and make a note of how much additional space it "wants" (the amount of
853 * additional space it could actually make use of).
856 /* VS has a lower limit on the number of URB entries */
858 ALIGN(devinfo
->urb
.min_vs_entries
* vs_entry_size_bytes
,
859 chunk_size_bytes
) / chunk_size_bytes
;
861 ALIGN(devinfo
->urb
.max_vs_entries
* vs_entry_size_bytes
,
862 chunk_size_bytes
) / chunk_size_bytes
- vs_chunks
;
864 unsigned gs_chunks
= 0;
865 unsigned gs_wants
= 0;
867 /* There are two constraints on the minimum amount of URB space we can
870 * (1) We need room for at least 2 URB entries, since we always operate
871 * the GS in DUAL_OBJECT mode.
873 * (2) We can't allocate less than nr_gs_entries_granularity.
875 gs_chunks
= ALIGN(MAX2(gs_granularity
, 2) * gs_entry_size_bytes
,
876 chunk_size_bytes
) / chunk_size_bytes
;
878 ALIGN(devinfo
->urb
.max_gs_entries
* gs_entry_size_bytes
,
879 chunk_size_bytes
) / chunk_size_bytes
- gs_chunks
;
882 /* There should always be enough URB space to satisfy the minimum
883 * requirements of each stage.
885 unsigned total_needs
= push_constant_chunks
+ vs_chunks
+ gs_chunks
;
886 assert(total_needs
<= urb_chunks
);
888 /* Mete out remaining space (if any) in proportion to "wants". */
889 unsigned total_wants
= vs_wants
+ gs_wants
;
890 unsigned remaining_space
= urb_chunks
- total_needs
;
891 if (remaining_space
> total_wants
)
892 remaining_space
= total_wants
;
893 if (remaining_space
> 0) {
894 unsigned vs_additional
= (unsigned)
895 round(vs_wants
* (((double) remaining_space
) / total_wants
));
896 vs_chunks
+= vs_additional
;
897 remaining_space
-= vs_additional
;
898 gs_chunks
+= remaining_space
;
901 /* Sanity check that we haven't over-allocated. */
902 assert(push_constant_chunks
+ vs_chunks
+ gs_chunks
<= urb_chunks
);
904 /* Finally, compute the number of entries that can fit in the space
905 * allocated to each stage.
907 unsigned nr_vs_entries
= vs_chunks
* chunk_size_bytes
/ vs_entry_size_bytes
;
908 unsigned nr_gs_entries
= gs_chunks
* chunk_size_bytes
/ gs_entry_size_bytes
;
910 /* Since we rounded up when computing *_wants, this may be slightly more
911 * than the maximum allowed amount, so correct for that.
913 nr_vs_entries
= MIN2(nr_vs_entries
, devinfo
->urb
.max_vs_entries
);
914 nr_gs_entries
= MIN2(nr_gs_entries
, devinfo
->urb
.max_gs_entries
);
916 /* Ensure that we program a multiple of the granularity. */
917 nr_vs_entries
= ROUND_DOWN_TO(nr_vs_entries
, vs_granularity
);
918 nr_gs_entries
= ROUND_DOWN_TO(nr_gs_entries
, gs_granularity
);
920 /* Finally, sanity check to make sure we have at least the minimum number
921 * of entries needed for each stage.
923 assert(nr_vs_entries
>= devinfo
->urb
.min_vs_entries
);
925 assert(nr_gs_entries
>= 2);
927 /* Lay out the URB in the following order:
932 pipeline
->urb
.start
[MESA_SHADER_VERTEX
] = push_constant_chunks
;
933 pipeline
->urb
.size
[MESA_SHADER_VERTEX
] = vs_size
;
934 pipeline
->urb
.entries
[MESA_SHADER_VERTEX
] = nr_vs_entries
;
936 pipeline
->urb
.start
[MESA_SHADER_GEOMETRY
] = push_constant_chunks
+ vs_chunks
;
937 pipeline
->urb
.size
[MESA_SHADER_GEOMETRY
] = gs_size
;
938 pipeline
->urb
.entries
[MESA_SHADER_GEOMETRY
] = nr_gs_entries
;
940 pipeline
->urb
.start
[MESA_SHADER_TESS_CTRL
] = push_constant_chunks
;
941 pipeline
->urb
.size
[MESA_SHADER_TESS_CTRL
] = 1;
942 pipeline
->urb
.entries
[MESA_SHADER_TESS_CTRL
] = 0;
944 pipeline
->urb
.start
[MESA_SHADER_TESS_EVAL
] = push_constant_chunks
;
945 pipeline
->urb
.size
[MESA_SHADER_TESS_EVAL
] = 1;
946 pipeline
->urb
.entries
[MESA_SHADER_TESS_EVAL
] = 0;
948 const unsigned stages
=
949 _mesa_bitcount(pipeline
->active_stages
& VK_SHADER_STAGE_ALL_GRAPHICS
);
950 unsigned size_per_stage
= stages
? (push_constant_kb
/ stages
) : 0;
951 unsigned used_kb
= 0;
953 /* Broadwell+ and Haswell gt3 require that the push constant sizes be in
954 * units of 2KB. Incidentally, these are the same platforms that have
955 * 32KB worth of push constant space.
957 if (push_constant_kb
== 32)
958 size_per_stage
&= ~1u;
960 for (int i
= MESA_SHADER_VERTEX
; i
< MESA_SHADER_FRAGMENT
; i
++) {
961 pipeline
->urb
.push_size
[i
] =
962 (pipeline
->active_stages
& (1 << i
)) ? size_per_stage
: 0;
963 used_kb
+= pipeline
->urb
.push_size
[i
];
964 assert(used_kb
<= push_constant_kb
);
967 pipeline
->urb
.push_size
[MESA_SHADER_FRAGMENT
] =
968 push_constant_kb
- used_kb
;
972 anv_pipeline_init_dynamic_state(struct anv_pipeline
*pipeline
,
973 const VkGraphicsPipelineCreateInfo
*pCreateInfo
)
975 anv_cmd_dirty_mask_t states
= ANV_CMD_DIRTY_DYNAMIC_ALL
;
976 ANV_FROM_HANDLE(anv_render_pass
, pass
, pCreateInfo
->renderPass
);
977 struct anv_subpass
*subpass
= &pass
->subpasses
[pCreateInfo
->subpass
];
979 pipeline
->dynamic_state
= default_dynamic_state
;
981 if (pCreateInfo
->pDynamicState
) {
982 /* Remove all of the states that are marked as dynamic */
983 uint32_t count
= pCreateInfo
->pDynamicState
->dynamicStateCount
;
984 for (uint32_t s
= 0; s
< count
; s
++)
985 states
&= ~(1 << pCreateInfo
->pDynamicState
->pDynamicStates
[s
]);
988 struct anv_dynamic_state
*dynamic
= &pipeline
->dynamic_state
;
990 dynamic
->viewport
.count
= pCreateInfo
->pViewportState
->viewportCount
;
991 if (states
& (1 << VK_DYNAMIC_STATE_VIEWPORT
)) {
992 typed_memcpy(dynamic
->viewport
.viewports
,
993 pCreateInfo
->pViewportState
->pViewports
,
994 pCreateInfo
->pViewportState
->viewportCount
);
997 dynamic
->scissor
.count
= pCreateInfo
->pViewportState
->scissorCount
;
998 if (states
& (1 << VK_DYNAMIC_STATE_SCISSOR
)) {
999 typed_memcpy(dynamic
->scissor
.scissors
,
1000 pCreateInfo
->pViewportState
->pScissors
,
1001 pCreateInfo
->pViewportState
->scissorCount
);
1004 if (states
& (1 << VK_DYNAMIC_STATE_LINE_WIDTH
)) {
1005 assert(pCreateInfo
->pRasterizationState
);
1006 dynamic
->line_width
= pCreateInfo
->pRasterizationState
->lineWidth
;
1009 if (states
& (1 << VK_DYNAMIC_STATE_DEPTH_BIAS
)) {
1010 assert(pCreateInfo
->pRasterizationState
);
1011 dynamic
->depth_bias
.bias
=
1012 pCreateInfo
->pRasterizationState
->depthBiasConstantFactor
;
1013 dynamic
->depth_bias
.clamp
=
1014 pCreateInfo
->pRasterizationState
->depthBiasClamp
;
1015 dynamic
->depth_bias
.slope
=
1016 pCreateInfo
->pRasterizationState
->depthBiasSlopeFactor
;
1019 if (states
& (1 << VK_DYNAMIC_STATE_BLEND_CONSTANTS
)) {
1020 assert(pCreateInfo
->pColorBlendState
);
1021 typed_memcpy(dynamic
->blend_constants
,
1022 pCreateInfo
->pColorBlendState
->blendConstants
, 4);
1025 /* If there is no depthstencil attachment, then don't read
1026 * pDepthStencilState. The Vulkan spec states that pDepthStencilState may
1027 * be NULL in this case. Even if pDepthStencilState is non-NULL, there is
1028 * no need to override the depthstencil defaults in
1029 * anv_pipeline::dynamic_state when there is no depthstencil attachment.
1031 * From the Vulkan spec (20 Oct 2015, git-aa308cb):
1033 * pDepthStencilState [...] may only be NULL if renderPass and subpass
1034 * specify a subpass that has no depth/stencil attachment.
1036 if (subpass
->depth_stencil_attachment
!= VK_ATTACHMENT_UNUSED
) {
1037 if (states
& (1 << VK_DYNAMIC_STATE_DEPTH_BOUNDS
)) {
1038 assert(pCreateInfo
->pDepthStencilState
);
1039 dynamic
->depth_bounds
.min
=
1040 pCreateInfo
->pDepthStencilState
->minDepthBounds
;
1041 dynamic
->depth_bounds
.max
=
1042 pCreateInfo
->pDepthStencilState
->maxDepthBounds
;
1045 if (states
& (1 << VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK
)) {
1046 assert(pCreateInfo
->pDepthStencilState
);
1047 dynamic
->stencil_compare_mask
.front
=
1048 pCreateInfo
->pDepthStencilState
->front
.compareMask
;
1049 dynamic
->stencil_compare_mask
.back
=
1050 pCreateInfo
->pDepthStencilState
->back
.compareMask
;
1053 if (states
& (1 << VK_DYNAMIC_STATE_STENCIL_WRITE_MASK
)) {
1054 assert(pCreateInfo
->pDepthStencilState
);
1055 dynamic
->stencil_write_mask
.front
=
1056 pCreateInfo
->pDepthStencilState
->front
.writeMask
;
1057 dynamic
->stencil_write_mask
.back
=
1058 pCreateInfo
->pDepthStencilState
->back
.writeMask
;
1061 if (states
& (1 << VK_DYNAMIC_STATE_STENCIL_REFERENCE
)) {
1062 assert(pCreateInfo
->pDepthStencilState
);
1063 dynamic
->stencil_reference
.front
=
1064 pCreateInfo
->pDepthStencilState
->front
.reference
;
1065 dynamic
->stencil_reference
.back
=
1066 pCreateInfo
->pDepthStencilState
->back
.reference
;
1070 pipeline
->dynamic_state_mask
= states
;
1074 anv_pipeline_validate_create_info(const VkGraphicsPipelineCreateInfo
*info
)
1076 struct anv_render_pass
*renderpass
= NULL
;
1077 struct anv_subpass
*subpass
= NULL
;
1079 /* Assert that all required members of VkGraphicsPipelineCreateInfo are
1080 * present, as explained by the Vulkan (20 Oct 2015, git-aa308cb), Section
1081 * 4.2 Graphics Pipeline.
1083 assert(info
->sType
== VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO
);
1085 renderpass
= anv_render_pass_from_handle(info
->renderPass
);
1088 if (renderpass
!= &anv_meta_dummy_renderpass
) {
1089 assert(info
->subpass
< renderpass
->subpass_count
);
1090 subpass
= &renderpass
->subpasses
[info
->subpass
];
1093 assert(info
->stageCount
>= 1);
1094 assert(info
->pVertexInputState
);
1095 assert(info
->pInputAssemblyState
);
1096 assert(info
->pViewportState
);
1097 assert(info
->pRasterizationState
);
1099 if (subpass
&& subpass
->depth_stencil_attachment
!= VK_ATTACHMENT_UNUSED
)
1100 assert(info
->pDepthStencilState
);
1102 if (subpass
&& subpass
->color_count
> 0)
1103 assert(info
->pColorBlendState
);
1105 for (uint32_t i
= 0; i
< info
->stageCount
; ++i
) {
1106 switch (info
->pStages
[i
].stage
) {
1107 case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT
:
1108 case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT
:
1109 assert(info
->pTessellationState
);
1118 anv_pipeline_init(struct anv_pipeline
*pipeline
,
1119 struct anv_device
*device
,
1120 struct anv_pipeline_cache
*cache
,
1121 const VkGraphicsPipelineCreateInfo
*pCreateInfo
,
1122 const struct anv_graphics_pipeline_create_info
*extra
,
1123 const VkAllocationCallbacks
*alloc
)
1128 anv_pipeline_validate_create_info(pCreateInfo
);
1132 alloc
= &device
->alloc
;
1134 pipeline
->device
= device
;
1135 pipeline
->layout
= anv_pipeline_layout_from_handle(pCreateInfo
->layout
);
1137 result
= anv_reloc_list_init(&pipeline
->batch_relocs
, alloc
);
1138 if (result
!= VK_SUCCESS
)
1141 pipeline
->batch
.alloc
= alloc
;
1142 pipeline
->batch
.next
= pipeline
->batch
.start
= pipeline
->batch_data
;
1143 pipeline
->batch
.end
= pipeline
->batch
.start
+ sizeof(pipeline
->batch_data
);
1144 pipeline
->batch
.relocs
= &pipeline
->batch_relocs
;
1146 anv_pipeline_init_dynamic_state(pipeline
, pCreateInfo
);
1148 pipeline
->use_repclear
= extra
&& extra
->use_repclear
;
1150 pipeline
->needs_data_cache
= false;
1152 /* When we free the pipeline, we detect stages based on the NULL status
1153 * of various prog_data pointers. Make them NULL by default.
1155 memset(pipeline
->prog_data
, 0, sizeof(pipeline
->prog_data
));
1156 memset(pipeline
->scratch_start
, 0, sizeof(pipeline
->scratch_start
));
1157 memset(pipeline
->bindings
, 0, sizeof(pipeline
->bindings
));
1159 pipeline
->vs_simd8
= NO_KERNEL
;
1160 pipeline
->vs_vec4
= NO_KERNEL
;
1161 pipeline
->gs_kernel
= NO_KERNEL
;
1162 pipeline
->ps_ksp0
= NO_KERNEL
;
1164 pipeline
->active_stages
= 0;
1165 pipeline
->total_scratch
= 0;
1167 const VkPipelineShaderStageCreateInfo
*pStages
[MESA_SHADER_STAGES
] = { 0, };
1168 struct anv_shader_module
*modules
[MESA_SHADER_STAGES
] = { 0, };
1169 for (uint32_t i
= 0; i
< pCreateInfo
->stageCount
; i
++) {
1170 gl_shader_stage stage
= ffs(pCreateInfo
->pStages
[i
].stage
) - 1;
1171 pStages
[stage
] = &pCreateInfo
->pStages
[i
];
1172 modules
[stage
] = anv_shader_module_from_handle(pStages
[stage
]->module
);
1175 if (modules
[MESA_SHADER_VERTEX
]) {
1176 anv_pipeline_compile_vs(pipeline
, cache
, pCreateInfo
,
1177 modules
[MESA_SHADER_VERTEX
],
1178 pStages
[MESA_SHADER_VERTEX
]->pName
,
1179 pStages
[MESA_SHADER_VERTEX
]->pSpecializationInfo
);
1182 if (modules
[MESA_SHADER_TESS_CTRL
] || modules
[MESA_SHADER_TESS_EVAL
])
1183 anv_finishme("no tessellation support");
1185 if (modules
[MESA_SHADER_GEOMETRY
]) {
1186 anv_pipeline_compile_gs(pipeline
, cache
, pCreateInfo
,
1187 modules
[MESA_SHADER_GEOMETRY
],
1188 pStages
[MESA_SHADER_GEOMETRY
]->pName
,
1189 pStages
[MESA_SHADER_GEOMETRY
]->pSpecializationInfo
);
1192 if (modules
[MESA_SHADER_FRAGMENT
]) {
1193 anv_pipeline_compile_fs(pipeline
, cache
, pCreateInfo
, extra
,
1194 modules
[MESA_SHADER_FRAGMENT
],
1195 pStages
[MESA_SHADER_FRAGMENT
]->pName
,
1196 pStages
[MESA_SHADER_FRAGMENT
]->pSpecializationInfo
);
1199 if (!(pipeline
->active_stages
& VK_SHADER_STAGE_VERTEX_BIT
)) {
1200 /* Vertex is only optional if disable_vs is set */
1201 assert(extra
->disable_vs
);
1204 anv_setup_pipeline_l3_config(pipeline
);
1205 anv_compute_urb_partition(pipeline
);
1207 const VkPipelineVertexInputStateCreateInfo
*vi_info
=
1208 pCreateInfo
->pVertexInputState
;
1210 uint64_t inputs_read
;
1211 if (extra
&& extra
->disable_vs
) {
1212 /* If the VS is disabled, just assume the user knows what they're
1213 * doing and apply the layout blindly. This can only come from
1214 * meta, so this *should* be safe.
1216 inputs_read
= ~0ull;
1218 inputs_read
= get_vs_prog_data(pipeline
)->inputs_read
;
1221 pipeline
->vb_used
= 0;
1222 for (uint32_t i
= 0; i
< vi_info
->vertexAttributeDescriptionCount
; i
++) {
1223 const VkVertexInputAttributeDescription
*desc
=
1224 &vi_info
->pVertexAttributeDescriptions
[i
];
1226 if (inputs_read
& (1 << (VERT_ATTRIB_GENERIC0
+ desc
->location
)))
1227 pipeline
->vb_used
|= 1 << desc
->binding
;
1230 for (uint32_t i
= 0; i
< vi_info
->vertexBindingDescriptionCount
; i
++) {
1231 const VkVertexInputBindingDescription
*desc
=
1232 &vi_info
->pVertexBindingDescriptions
[i
];
1234 pipeline
->binding_stride
[desc
->binding
] = desc
->stride
;
1236 /* Step rate is programmed per vertex element (attribute), not
1237 * binding. Set up a map of which bindings step per instance, for
1238 * reference by vertex element setup. */
1239 switch (desc
->inputRate
) {
1241 case VK_VERTEX_INPUT_RATE_VERTEX
:
1242 pipeline
->instancing_enable
[desc
->binding
] = false;
1244 case VK_VERTEX_INPUT_RATE_INSTANCE
:
1245 pipeline
->instancing_enable
[desc
->binding
] = true;
1250 const VkPipelineInputAssemblyStateCreateInfo
*ia_info
=
1251 pCreateInfo
->pInputAssemblyState
;
1252 pipeline
->primitive_restart
= ia_info
->primitiveRestartEnable
;
1253 pipeline
->topology
= vk_to_gen_primitive_type
[ia_info
->topology
];
1255 if (extra
&& extra
->use_rectlist
)
1256 pipeline
->topology
= _3DPRIM_RECTLIST
;
1258 while (anv_block_pool_size(&device
->scratch_block_pool
) <
1259 pipeline
->total_scratch
)
1260 anv_block_pool_alloc(&device
->scratch_block_pool
);
1266 anv_graphics_pipeline_create(
1268 VkPipelineCache _cache
,
1269 const VkGraphicsPipelineCreateInfo
*pCreateInfo
,
1270 const struct anv_graphics_pipeline_create_info
*extra
,
1271 const VkAllocationCallbacks
*pAllocator
,
1272 VkPipeline
*pPipeline
)
1274 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1275 ANV_FROM_HANDLE(anv_pipeline_cache
, cache
, _cache
);
1278 cache
= &device
->default_pipeline_cache
;
1280 switch (device
->info
.gen
) {
1282 if (device
->info
.is_haswell
)
1283 return gen75_graphics_pipeline_create(_device
, cache
, pCreateInfo
, extra
, pAllocator
, pPipeline
);
1285 return gen7_graphics_pipeline_create(_device
, cache
, pCreateInfo
, extra
, pAllocator
, pPipeline
);
1287 return gen8_graphics_pipeline_create(_device
, cache
, pCreateInfo
, extra
, pAllocator
, pPipeline
);
1289 return gen9_graphics_pipeline_create(_device
, cache
, pCreateInfo
, extra
, pAllocator
, pPipeline
);
1291 unreachable("unsupported gen\n");
1295 VkResult
anv_CreateGraphicsPipelines(
1297 VkPipelineCache pipelineCache
,
1299 const VkGraphicsPipelineCreateInfo
* pCreateInfos
,
1300 const VkAllocationCallbacks
* pAllocator
,
1301 VkPipeline
* pPipelines
)
1303 VkResult result
= VK_SUCCESS
;
1306 for (; i
< count
; i
++) {
1307 result
= anv_graphics_pipeline_create(_device
,
1310 NULL
, pAllocator
, &pPipelines
[i
]);
1311 if (result
!= VK_SUCCESS
) {
1312 for (unsigned j
= 0; j
< i
; j
++) {
1313 anv_DestroyPipeline(_device
, pPipelines
[j
], pAllocator
);
1323 static VkResult
anv_compute_pipeline_create(
1325 VkPipelineCache _cache
,
1326 const VkComputePipelineCreateInfo
* pCreateInfo
,
1327 const VkAllocationCallbacks
* pAllocator
,
1328 VkPipeline
* pPipeline
)
1330 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1331 ANV_FROM_HANDLE(anv_pipeline_cache
, cache
, _cache
);
1334 cache
= &device
->default_pipeline_cache
;
1336 switch (device
->info
.gen
) {
1338 if (device
->info
.is_haswell
)
1339 return gen75_compute_pipeline_create(_device
, cache
, pCreateInfo
, pAllocator
, pPipeline
);
1341 return gen7_compute_pipeline_create(_device
, cache
, pCreateInfo
, pAllocator
, pPipeline
);
1343 return gen8_compute_pipeline_create(_device
, cache
, pCreateInfo
, pAllocator
, pPipeline
);
1345 return gen9_compute_pipeline_create(_device
, cache
, pCreateInfo
, pAllocator
, pPipeline
);
1347 unreachable("unsupported gen\n");
1351 VkResult
anv_CreateComputePipelines(
1353 VkPipelineCache pipelineCache
,
1355 const VkComputePipelineCreateInfo
* pCreateInfos
,
1356 const VkAllocationCallbacks
* pAllocator
,
1357 VkPipeline
* pPipelines
)
1359 VkResult result
= VK_SUCCESS
;
1362 for (; i
< count
; i
++) {
1363 result
= anv_compute_pipeline_create(_device
, pipelineCache
,
1365 pAllocator
, &pPipelines
[i
]);
1366 if (result
!= VK_SUCCESS
) {
1367 for (unsigned j
= 0; j
< i
; j
++) {
1368 anv_DestroyPipeline(_device
, pPipelines
[j
], pAllocator
);