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 nir_lower_returns(nir
);
146 nir_validate_shader(nir
);
148 nir_inline_functions(nir
);
149 nir_validate_shader(nir
);
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
);
156 assert(exec_list_length(&nir
->functions
) == 1);
157 entry_point
->name
= ralloc_strdup(entry_point
, "main");
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
);
164 nir_lower_io_to_temporaries(entry_point
->shader
, entry_point
, true, false);
166 nir_lower_system_values(nir
);
167 nir_validate_shader(nir
);
170 /* Vulkan uses the separate-shader linking model */
171 nir
->info
.separate_shader
= true;
173 nir
= brw_preprocess_nir(compiler
, nir
);
175 nir_shader_gather_info(nir
, entry_point
->impl
);
177 nir_variable_mode indirect_mask
= 0;
178 if (compiler
->glsl_compiler_options
[stage
].EmitNoIndirectInput
)
179 indirect_mask
|= nir_var_shader_in
;
180 if (compiler
->glsl_compiler_options
[stage
].EmitNoIndirectTemp
)
181 indirect_mask
|= nir_var_local
;
183 nir_lower_indirect_derefs(nir
, indirect_mask
);
188 void anv_DestroyPipeline(
190 VkPipeline _pipeline
,
191 const VkAllocationCallbacks
* pAllocator
)
193 ANV_FROM_HANDLE(anv_device
, device
, _device
);
194 ANV_FROM_HANDLE(anv_pipeline
, pipeline
, _pipeline
);
196 anv_reloc_list_finish(&pipeline
->batch_relocs
,
197 pAllocator
? pAllocator
: &device
->alloc
);
198 if (pipeline
->blend_state
.map
)
199 anv_state_pool_free(&device
->dynamic_state_pool
, pipeline
->blend_state
);
200 anv_free2(&device
->alloc
, pAllocator
, pipeline
);
203 static const uint32_t vk_to_gen_primitive_type
[] = {
204 [VK_PRIMITIVE_TOPOLOGY_POINT_LIST
] = _3DPRIM_POINTLIST
,
205 [VK_PRIMITIVE_TOPOLOGY_LINE_LIST
] = _3DPRIM_LINELIST
,
206 [VK_PRIMITIVE_TOPOLOGY_LINE_STRIP
] = _3DPRIM_LINESTRIP
,
207 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST
] = _3DPRIM_TRILIST
,
208 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP
] = _3DPRIM_TRISTRIP
,
209 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN
] = _3DPRIM_TRIFAN
,
210 [VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY
] = _3DPRIM_LINELIST_ADJ
,
211 [VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY
] = _3DPRIM_LINESTRIP_ADJ
,
212 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY
] = _3DPRIM_TRILIST_ADJ
,
213 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY
] = _3DPRIM_TRISTRIP_ADJ
,
214 /* [VK_PRIMITIVE_TOPOLOGY_PATCH_LIST] = _3DPRIM_PATCHLIST_1 */
218 populate_sampler_prog_key(const struct brw_device_info
*devinfo
,
219 struct brw_sampler_prog_key_data
*key
)
221 /* XXX: Handle texture swizzle on HSW- */
222 for (int i
= 0; i
< MAX_SAMPLERS
; i
++) {
223 /* Assume color sampler, no swizzling. (Works for BDW+) */
224 key
->swizzles
[i
] = SWIZZLE_XYZW
;
229 populate_vs_prog_key(const struct brw_device_info
*devinfo
,
230 struct brw_vs_prog_key
*key
)
232 memset(key
, 0, sizeof(*key
));
234 populate_sampler_prog_key(devinfo
, &key
->tex
);
236 /* XXX: Handle vertex input work-arounds */
238 /* XXX: Handle sampler_prog_key */
242 populate_gs_prog_key(const struct brw_device_info
*devinfo
,
243 struct brw_gs_prog_key
*key
)
245 memset(key
, 0, sizeof(*key
));
247 populate_sampler_prog_key(devinfo
, &key
->tex
);
251 populate_wm_prog_key(const struct brw_device_info
*devinfo
,
252 const VkGraphicsPipelineCreateInfo
*info
,
253 const struct anv_graphics_pipeline_create_info
*extra
,
254 struct brw_wm_prog_key
*key
)
256 ANV_FROM_HANDLE(anv_render_pass
, render_pass
, info
->renderPass
);
258 memset(key
, 0, sizeof(*key
));
260 populate_sampler_prog_key(devinfo
, &key
->tex
);
262 /* TODO: Fill out key->input_slots_valid */
264 /* Vulkan doesn't specify a default */
265 key
->high_quality_derivatives
= false;
267 /* XXX Vulkan doesn't appear to specify */
268 key
->clamp_fragment_color
= false;
270 /* Vulkan always specifies upper-left coordinates */
271 key
->drawable_height
= 0;
272 key
->render_to_fbo
= false;
274 if (extra
&& extra
->color_attachment_count
>= 0) {
275 key
->nr_color_regions
= extra
->color_attachment_count
;
277 key
->nr_color_regions
=
278 render_pass
->subpasses
[info
->subpass
].color_count
;
281 key
->replicate_alpha
= key
->nr_color_regions
> 1 &&
282 info
->pMultisampleState
&&
283 info
->pMultisampleState
->alphaToCoverageEnable
;
285 if (info
->pMultisampleState
&& info
->pMultisampleState
->rasterizationSamples
> 1) {
286 /* We should probably pull this out of the shader, but it's fairly
287 * harmless to compute it and then let dead-code take care of it.
289 key
->persample_interp
=
290 (info
->pMultisampleState
->minSampleShading
*
291 info
->pMultisampleState
->rasterizationSamples
) > 1;
292 key
->multisample_fbo
= true;
297 populate_cs_prog_key(const struct brw_device_info
*devinfo
,
298 struct brw_cs_prog_key
*key
)
300 memset(key
, 0, sizeof(*key
));
302 populate_sampler_prog_key(devinfo
, &key
->tex
);
306 anv_pipeline_compile(struct anv_pipeline
*pipeline
,
307 struct anv_shader_module
*module
,
308 const char *entrypoint
,
309 gl_shader_stage stage
,
310 const VkSpecializationInfo
*spec_info
,
311 struct brw_stage_prog_data
*prog_data
,
312 struct anv_pipeline_bind_map
*map
)
314 nir_shader
*nir
= anv_shader_compile_to_nir(pipeline
->device
,
315 module
, entrypoint
, stage
,
320 anv_nir_lower_push_constants(nir
);
322 /* Figure out the number of parameters */
323 prog_data
->nr_params
= 0;
325 if (nir
->num_uniforms
> 0) {
326 /* If the shader uses any push constants at all, we'll just give
327 * them the maximum possible number
329 prog_data
->nr_params
+= MAX_PUSH_CONSTANTS_SIZE
/ sizeof(float);
332 if (pipeline
->layout
&& pipeline
->layout
->stage
[stage
].has_dynamic_offsets
)
333 prog_data
->nr_params
+= MAX_DYNAMIC_BUFFERS
* 2;
335 if (nir
->info
.num_images
> 0) {
336 prog_data
->nr_params
+= nir
->info
.num_images
* BRW_IMAGE_PARAM_SIZE
;
337 pipeline
->needs_data_cache
= true;
340 if (nir
->info
.num_ssbos
> 0)
341 pipeline
->needs_data_cache
= true;
343 if (prog_data
->nr_params
> 0) {
344 /* XXX: I think we're leaking this */
345 prog_data
->param
= (const union gl_constant_value
**)
346 malloc(prog_data
->nr_params
* sizeof(union gl_constant_value
*));
348 /* We now set the param values to be offsets into a
349 * anv_push_constant_data structure. Since the compiler doesn't
350 * actually dereference any of the gl_constant_value pointers in the
351 * params array, it doesn't really matter what we put here.
353 struct anv_push_constants
*null_data
= NULL
;
354 if (nir
->num_uniforms
> 0) {
355 /* Fill out the push constants section of the param array */
356 for (unsigned i
= 0; i
< MAX_PUSH_CONSTANTS_SIZE
/ sizeof(float); i
++)
357 prog_data
->param
[i
] = (const union gl_constant_value
*)
358 &null_data
->client_data
[i
* sizeof(float)];
362 /* Set up dynamic offsets */
363 anv_nir_apply_dynamic_offsets(pipeline
, nir
, prog_data
);
365 /* Apply the actual pipeline layout to UBOs, SSBOs, and textures */
366 if (pipeline
->layout
)
367 anv_nir_apply_pipeline_layout(pipeline
, nir
, prog_data
, map
);
369 /* nir_lower_io will only handle the push constants; we need to set this
370 * to the full number of possible uniforms.
372 nir
->num_uniforms
= prog_data
->nr_params
* 4;
378 anv_fill_binding_table(struct brw_stage_prog_data
*prog_data
, unsigned bias
)
380 prog_data
->binding_table
.size_bytes
= 0;
381 prog_data
->binding_table
.texture_start
= bias
;
382 prog_data
->binding_table
.ubo_start
= bias
;
383 prog_data
->binding_table
.ssbo_start
= bias
;
384 prog_data
->binding_table
.image_start
= bias
;
388 anv_pipeline_add_compiled_stage(struct anv_pipeline
*pipeline
,
389 gl_shader_stage stage
,
390 const struct brw_stage_prog_data
*prog_data
,
391 struct anv_pipeline_bind_map
*map
)
393 struct brw_device_info
*devinfo
= &pipeline
->device
->info
;
394 uint32_t max_threads
[] = {
395 [MESA_SHADER_VERTEX
] = devinfo
->max_vs_threads
,
396 [MESA_SHADER_TESS_CTRL
] = devinfo
->max_hs_threads
,
397 [MESA_SHADER_TESS_EVAL
] = devinfo
->max_ds_threads
,
398 [MESA_SHADER_GEOMETRY
] = devinfo
->max_gs_threads
,
399 [MESA_SHADER_FRAGMENT
] = devinfo
->max_wm_threads
,
400 [MESA_SHADER_COMPUTE
] = devinfo
->max_cs_threads
,
403 pipeline
->prog_data
[stage
] = prog_data
;
404 pipeline
->active_stages
|= mesa_to_vk_shader_stage(stage
);
405 pipeline
->scratch_start
[stage
] = pipeline
->total_scratch
;
406 pipeline
->total_scratch
=
407 align_u32(pipeline
->total_scratch
, 1024) +
408 prog_data
->total_scratch
* max_threads
[stage
];
409 pipeline
->bindings
[stage
] = *map
;
413 anv_pipeline_compile_vs(struct anv_pipeline
*pipeline
,
414 struct anv_pipeline_cache
*cache
,
415 const VkGraphicsPipelineCreateInfo
*info
,
416 struct anv_shader_module
*module
,
417 const char *entrypoint
,
418 const VkSpecializationInfo
*spec_info
)
420 const struct brw_compiler
*compiler
=
421 pipeline
->device
->instance
->physicalDevice
.compiler
;
422 const struct brw_stage_prog_data
*stage_prog_data
;
423 struct anv_pipeline_bind_map map
;
424 struct brw_vs_prog_key key
;
425 uint32_t kernel
= NO_KERNEL
;
426 unsigned char sha1
[20];
428 populate_vs_prog_key(&pipeline
->device
->info
, &key
);
430 if (module
->size
> 0) {
431 anv_hash_shader(sha1
, &key
, sizeof(key
), module
, entrypoint
, spec_info
);
432 kernel
= anv_pipeline_cache_search(cache
, sha1
, &stage_prog_data
, &map
);
435 if (kernel
== NO_KERNEL
) {
436 struct brw_vs_prog_data prog_data
= { 0, };
437 struct anv_pipeline_binding surface_to_descriptor
[256];
438 struct anv_pipeline_binding sampler_to_descriptor
[256];
440 map
= (struct anv_pipeline_bind_map
) {
441 .surface_to_descriptor
= surface_to_descriptor
,
442 .sampler_to_descriptor
= sampler_to_descriptor
445 nir_shader
*nir
= anv_pipeline_compile(pipeline
, module
, entrypoint
,
446 MESA_SHADER_VERTEX
, spec_info
,
447 &prog_data
.base
.base
, &map
);
449 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
451 anv_fill_binding_table(&prog_data
.base
.base
, 0);
453 void *mem_ctx
= ralloc_context(NULL
);
455 if (module
->nir
== NULL
)
456 ralloc_steal(mem_ctx
, nir
);
458 prog_data
.inputs_read
= nir
->info
.inputs_read
;
460 brw_compute_vue_map(&pipeline
->device
->info
,
461 &prog_data
.base
.vue_map
,
462 nir
->info
.outputs_written
,
463 nir
->info
.separate_shader
);
466 const unsigned *shader_code
=
467 brw_compile_vs(compiler
, NULL
, mem_ctx
, &key
, &prog_data
, nir
,
468 NULL
, false, -1, &code_size
, NULL
);
469 if (shader_code
== NULL
) {
470 ralloc_free(mem_ctx
);
471 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
474 stage_prog_data
= &prog_data
.base
.base
;
475 kernel
= anv_pipeline_cache_upload_kernel(cache
,
476 module
->size
> 0 ? sha1
: NULL
,
477 shader_code
, code_size
,
478 &stage_prog_data
, sizeof(prog_data
),
480 ralloc_free(mem_ctx
);
483 const struct brw_vs_prog_data
*vs_prog_data
=
484 (const struct brw_vs_prog_data
*) stage_prog_data
;
486 if (vs_prog_data
->base
.dispatch_mode
== DISPATCH_MODE_SIMD8
) {
487 pipeline
->vs_simd8
= kernel
;
488 pipeline
->vs_vec4
= NO_KERNEL
;
490 pipeline
->vs_simd8
= NO_KERNEL
;
491 pipeline
->vs_vec4
= kernel
;
494 anv_pipeline_add_compiled_stage(pipeline
, MESA_SHADER_VERTEX
,
495 stage_prog_data
, &map
);
501 anv_pipeline_compile_gs(struct anv_pipeline
*pipeline
,
502 struct anv_pipeline_cache
*cache
,
503 const VkGraphicsPipelineCreateInfo
*info
,
504 struct anv_shader_module
*module
,
505 const char *entrypoint
,
506 const VkSpecializationInfo
*spec_info
)
508 const struct brw_compiler
*compiler
=
509 pipeline
->device
->instance
->physicalDevice
.compiler
;
510 const struct brw_stage_prog_data
*stage_prog_data
;
511 struct anv_pipeline_bind_map map
;
512 struct brw_gs_prog_key key
;
513 uint32_t kernel
= NO_KERNEL
;
514 unsigned char sha1
[20];
516 populate_gs_prog_key(&pipeline
->device
->info
, &key
);
518 if (module
->size
> 0) {
519 anv_hash_shader(sha1
, &key
, sizeof(key
), module
, entrypoint
, spec_info
);
520 kernel
= anv_pipeline_cache_search(cache
, sha1
, &stage_prog_data
, &map
);
523 if (kernel
== NO_KERNEL
) {
524 struct brw_gs_prog_data prog_data
= { 0, };
525 struct anv_pipeline_binding surface_to_descriptor
[256];
526 struct anv_pipeline_binding sampler_to_descriptor
[256];
528 map
= (struct anv_pipeline_bind_map
) {
529 .surface_to_descriptor
= surface_to_descriptor
,
530 .sampler_to_descriptor
= sampler_to_descriptor
533 nir_shader
*nir
= anv_pipeline_compile(pipeline
, module
, entrypoint
,
534 MESA_SHADER_GEOMETRY
, spec_info
,
535 &prog_data
.base
.base
, &map
);
537 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
539 anv_fill_binding_table(&prog_data
.base
.base
, 0);
541 void *mem_ctx
= ralloc_context(NULL
);
543 if (module
->nir
== NULL
)
544 ralloc_steal(mem_ctx
, nir
);
546 brw_compute_vue_map(&pipeline
->device
->info
,
547 &prog_data
.base
.vue_map
,
548 nir
->info
.outputs_written
,
549 nir
->info
.separate_shader
);
552 const unsigned *shader_code
=
553 brw_compile_gs(compiler
, NULL
, mem_ctx
, &key
, &prog_data
, nir
,
554 NULL
, -1, &code_size
, NULL
);
555 if (shader_code
== NULL
) {
556 ralloc_free(mem_ctx
);
557 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
561 stage_prog_data
= &prog_data
.base
.base
;
562 kernel
= anv_pipeline_cache_upload_kernel(cache
,
563 module
->size
> 0 ? sha1
: NULL
,
564 shader_code
, code_size
,
565 &stage_prog_data
, sizeof(prog_data
),
568 ralloc_free(mem_ctx
);
571 pipeline
->gs_kernel
= kernel
;
573 anv_pipeline_add_compiled_stage(pipeline
, MESA_SHADER_GEOMETRY
,
574 stage_prog_data
, &map
);
580 anv_pipeline_compile_fs(struct anv_pipeline
*pipeline
,
581 struct anv_pipeline_cache
*cache
,
582 const VkGraphicsPipelineCreateInfo
*info
,
583 const struct anv_graphics_pipeline_create_info
*extra
,
584 struct anv_shader_module
*module
,
585 const char *entrypoint
,
586 const VkSpecializationInfo
*spec_info
)
588 const struct brw_compiler
*compiler
=
589 pipeline
->device
->instance
->physicalDevice
.compiler
;
590 const struct brw_stage_prog_data
*stage_prog_data
;
591 struct anv_pipeline_bind_map map
;
592 struct brw_wm_prog_key key
;
593 unsigned char sha1
[20];
595 populate_wm_prog_key(&pipeline
->device
->info
, info
, extra
, &key
);
597 if (module
->size
> 0) {
598 anv_hash_shader(sha1
, &key
, sizeof(key
), module
, entrypoint
, spec_info
);
600 anv_pipeline_cache_search(cache
, sha1
, &stage_prog_data
, &map
);
603 if (pipeline
->ps_ksp0
== NO_KERNEL
) {
604 struct brw_wm_prog_data prog_data
= { 0, };
605 struct anv_pipeline_binding surface_to_descriptor
[256];
606 struct anv_pipeline_binding sampler_to_descriptor
[256];
608 map
= (struct anv_pipeline_bind_map
) {
609 .surface_to_descriptor
= surface_to_descriptor
+ 8,
610 .sampler_to_descriptor
= sampler_to_descriptor
613 nir_shader
*nir
= anv_pipeline_compile(pipeline
, module
, entrypoint
,
614 MESA_SHADER_FRAGMENT
, spec_info
,
615 &prog_data
.base
, &map
);
617 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
619 unsigned num_rts
= 0;
620 struct anv_pipeline_binding rt_bindings
[8];
621 nir_function_impl
*impl
= nir_shader_get_entrypoint(nir
)->impl
;
622 nir_foreach_variable_safe(var
, &nir
->outputs
) {
623 if (var
->data
.location
< FRAG_RESULT_DATA0
)
626 unsigned rt
= var
->data
.location
- FRAG_RESULT_DATA0
;
627 if (rt
>= key
.nr_color_regions
) {
628 /* Out-of-bounds, throw it away */
629 var
->data
.mode
= nir_var_local
;
630 exec_node_remove(&var
->node
);
631 exec_list_push_tail(&impl
->locals
, &var
->node
);
635 /* Give it a new, compacted, location */
636 var
->data
.location
= FRAG_RESULT_DATA0
+ num_rts
;
639 glsl_type_is_array(var
->type
) ? glsl_get_length(var
->type
) : 1;
640 assert(num_rts
+ array_len
<= 8);
642 for (unsigned i
= 0; i
< array_len
; i
++) {
643 rt_bindings
[num_rts
] = (struct anv_pipeline_binding
) {
644 .set
= ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS
,
649 num_rts
+= array_len
;
652 if (pipeline
->use_repclear
) {
653 assert(num_rts
== 1);
654 key
.nr_color_regions
= 1;
658 /* If we have no render targets, we need a null render target */
659 rt_bindings
[0] = (struct anv_pipeline_binding
) {
660 .set
= ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS
,
661 .offset
= UINT16_MAX
,
666 assert(num_rts
<= 8);
667 map
.surface_to_descriptor
-= num_rts
;
668 map
.surface_count
+= num_rts
;
669 assert(map
.surface_count
<= 256);
670 memcpy(map
.surface_to_descriptor
, rt_bindings
,
671 num_rts
* sizeof(*rt_bindings
));
673 anv_fill_binding_table(&prog_data
.base
, num_rts
);
675 void *mem_ctx
= ralloc_context(NULL
);
677 if (module
->nir
== NULL
)
678 ralloc_steal(mem_ctx
, nir
);
681 const unsigned *shader_code
=
682 brw_compile_fs(compiler
, NULL
, mem_ctx
, &key
, &prog_data
, nir
,
683 NULL
, -1, -1, true, pipeline
->use_repclear
,
685 if (shader_code
== NULL
) {
686 ralloc_free(mem_ctx
);
687 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
690 stage_prog_data
= &prog_data
.base
;
692 anv_pipeline_cache_upload_kernel(cache
,
693 module
->size
> 0 ? sha1
: NULL
,
694 shader_code
, code_size
,
695 &stage_prog_data
, sizeof(prog_data
),
698 ralloc_free(mem_ctx
);
701 anv_pipeline_add_compiled_stage(pipeline
, MESA_SHADER_FRAGMENT
,
702 stage_prog_data
, &map
);
708 anv_pipeline_compile_cs(struct anv_pipeline
*pipeline
,
709 struct anv_pipeline_cache
*cache
,
710 const VkComputePipelineCreateInfo
*info
,
711 struct anv_shader_module
*module
,
712 const char *entrypoint
,
713 const VkSpecializationInfo
*spec_info
)
715 const struct brw_compiler
*compiler
=
716 pipeline
->device
->instance
->physicalDevice
.compiler
;
717 const struct brw_stage_prog_data
*stage_prog_data
;
718 struct anv_pipeline_bind_map map
;
719 struct brw_cs_prog_key key
;
720 uint32_t kernel
= NO_KERNEL
;
721 unsigned char sha1
[20];
723 populate_cs_prog_key(&pipeline
->device
->info
, &key
);
725 if (module
->size
> 0) {
726 anv_hash_shader(sha1
, &key
, sizeof(key
), module
, entrypoint
, spec_info
);
727 kernel
= anv_pipeline_cache_search(cache
, sha1
, &stage_prog_data
, &map
);
730 if (module
->size
== 0 || kernel
== NO_KERNEL
) {
731 struct brw_cs_prog_data prog_data
= { 0, };
732 struct anv_pipeline_binding surface_to_descriptor
[256];
733 struct anv_pipeline_binding sampler_to_descriptor
[256];
735 map
= (struct anv_pipeline_bind_map
) {
736 .surface_to_descriptor
= surface_to_descriptor
,
737 .sampler_to_descriptor
= sampler_to_descriptor
740 nir_shader
*nir
= anv_pipeline_compile(pipeline
, module
, entrypoint
,
741 MESA_SHADER_COMPUTE
, spec_info
,
742 &prog_data
.base
, &map
);
744 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
746 anv_fill_binding_table(&prog_data
.base
, 1);
748 void *mem_ctx
= ralloc_context(NULL
);
750 if (module
->nir
== NULL
)
751 ralloc_steal(mem_ctx
, nir
);
754 const unsigned *shader_code
=
755 brw_compile_cs(compiler
, NULL
, mem_ctx
, &key
, &prog_data
, nir
,
756 -1, &code_size
, NULL
);
757 if (shader_code
== NULL
) {
758 ralloc_free(mem_ctx
);
759 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
762 stage_prog_data
= &prog_data
.base
;
763 kernel
= anv_pipeline_cache_upload_kernel(cache
,
764 module
->size
> 0 ? sha1
: NULL
,
765 shader_code
, code_size
,
766 &stage_prog_data
, sizeof(prog_data
),
769 ralloc_free(mem_ctx
);
772 pipeline
->cs_simd
= kernel
;
774 anv_pipeline_add_compiled_stage(pipeline
, MESA_SHADER_COMPUTE
,
775 stage_prog_data
, &map
);
781 gen7_compute_urb_partition(struct anv_pipeline
*pipeline
)
783 const struct brw_device_info
*devinfo
= &pipeline
->device
->info
;
784 bool vs_present
= pipeline
->active_stages
& VK_SHADER_STAGE_VERTEX_BIT
;
785 unsigned vs_size
= vs_present
?
786 get_vs_prog_data(pipeline
)->base
.urb_entry_size
: 1;
787 unsigned vs_entry_size_bytes
= vs_size
* 64;
788 bool gs_present
= pipeline
->active_stages
& VK_SHADER_STAGE_GEOMETRY_BIT
;
789 unsigned gs_size
= gs_present
?
790 get_gs_prog_data(pipeline
)->base
.urb_entry_size
: 1;
791 unsigned gs_entry_size_bytes
= gs_size
* 64;
793 /* From p35 of the Ivy Bridge PRM (section 1.7.1: 3DSTATE_URB_GS):
795 * VS Number of URB Entries must be divisible by 8 if the VS URB Entry
796 * Allocation Size is less than 9 512-bit URB entries.
798 * Similar text exists for GS.
800 unsigned vs_granularity
= (vs_size
< 9) ? 8 : 1;
801 unsigned gs_granularity
= (gs_size
< 9) ? 8 : 1;
803 /* URB allocations must be done in 8k chunks. */
804 unsigned chunk_size_bytes
= 8192;
806 /* Determine the size of the URB in chunks. */
807 unsigned urb_chunks
= devinfo
->urb
.size
* 1024 / chunk_size_bytes
;
809 /* Reserve space for push constants */
810 unsigned push_constant_kb
;
811 if (pipeline
->device
->info
.gen
>= 8)
812 push_constant_kb
= 32;
813 else if (pipeline
->device
->info
.is_haswell
)
814 push_constant_kb
= pipeline
->device
->info
.gt
== 3 ? 32 : 16;
816 push_constant_kb
= 16;
818 unsigned push_constant_bytes
= push_constant_kb
* 1024;
819 unsigned push_constant_chunks
=
820 push_constant_bytes
/ chunk_size_bytes
;
822 /* Initially, assign each stage the minimum amount of URB space it needs,
823 * and make a note of how much additional space it "wants" (the amount of
824 * additional space it could actually make use of).
827 /* VS has a lower limit on the number of URB entries */
829 ALIGN(devinfo
->urb
.min_vs_entries
* vs_entry_size_bytes
,
830 chunk_size_bytes
) / chunk_size_bytes
;
832 ALIGN(devinfo
->urb
.max_vs_entries
* vs_entry_size_bytes
,
833 chunk_size_bytes
) / chunk_size_bytes
- vs_chunks
;
835 unsigned gs_chunks
= 0;
836 unsigned gs_wants
= 0;
838 /* There are two constraints on the minimum amount of URB space we can
841 * (1) We need room for at least 2 URB entries, since we always operate
842 * the GS in DUAL_OBJECT mode.
844 * (2) We can't allocate less than nr_gs_entries_granularity.
846 gs_chunks
= ALIGN(MAX2(gs_granularity
, 2) * gs_entry_size_bytes
,
847 chunk_size_bytes
) / chunk_size_bytes
;
849 ALIGN(devinfo
->urb
.max_gs_entries
* gs_entry_size_bytes
,
850 chunk_size_bytes
) / chunk_size_bytes
- gs_chunks
;
853 /* There should always be enough URB space to satisfy the minimum
854 * requirements of each stage.
856 unsigned total_needs
= push_constant_chunks
+ vs_chunks
+ gs_chunks
;
857 assert(total_needs
<= urb_chunks
);
859 /* Mete out remaining space (if any) in proportion to "wants". */
860 unsigned total_wants
= vs_wants
+ gs_wants
;
861 unsigned remaining_space
= urb_chunks
- total_needs
;
862 if (remaining_space
> total_wants
)
863 remaining_space
= total_wants
;
864 if (remaining_space
> 0) {
865 unsigned vs_additional
= (unsigned)
866 round(vs_wants
* (((double) remaining_space
) / total_wants
));
867 vs_chunks
+= vs_additional
;
868 remaining_space
-= vs_additional
;
869 gs_chunks
+= remaining_space
;
872 /* Sanity check that we haven't over-allocated. */
873 assert(push_constant_chunks
+ vs_chunks
+ gs_chunks
<= urb_chunks
);
875 /* Finally, compute the number of entries that can fit in the space
876 * allocated to each stage.
878 unsigned nr_vs_entries
= vs_chunks
* chunk_size_bytes
/ vs_entry_size_bytes
;
879 unsigned nr_gs_entries
= gs_chunks
* chunk_size_bytes
/ gs_entry_size_bytes
;
881 /* Since we rounded up when computing *_wants, this may be slightly more
882 * than the maximum allowed amount, so correct for that.
884 nr_vs_entries
= MIN2(nr_vs_entries
, devinfo
->urb
.max_vs_entries
);
885 nr_gs_entries
= MIN2(nr_gs_entries
, devinfo
->urb
.max_gs_entries
);
887 /* Ensure that we program a multiple of the granularity. */
888 nr_vs_entries
= ROUND_DOWN_TO(nr_vs_entries
, vs_granularity
);
889 nr_gs_entries
= ROUND_DOWN_TO(nr_gs_entries
, gs_granularity
);
891 /* Finally, sanity check to make sure we have at least the minimum number
892 * of entries needed for each stage.
894 assert(nr_vs_entries
>= devinfo
->urb
.min_vs_entries
);
896 assert(nr_gs_entries
>= 2);
898 /* Lay out the URB in the following order:
903 pipeline
->urb
.start
[MESA_SHADER_VERTEX
] = push_constant_chunks
;
904 pipeline
->urb
.size
[MESA_SHADER_VERTEX
] = vs_size
;
905 pipeline
->urb
.entries
[MESA_SHADER_VERTEX
] = nr_vs_entries
;
907 pipeline
->urb
.start
[MESA_SHADER_GEOMETRY
] = push_constant_chunks
+ vs_chunks
;
908 pipeline
->urb
.size
[MESA_SHADER_GEOMETRY
] = gs_size
;
909 pipeline
->urb
.entries
[MESA_SHADER_GEOMETRY
] = nr_gs_entries
;
911 pipeline
->urb
.start
[MESA_SHADER_TESS_CTRL
] = push_constant_chunks
;
912 pipeline
->urb
.size
[MESA_SHADER_TESS_CTRL
] = 1;
913 pipeline
->urb
.entries
[MESA_SHADER_TESS_CTRL
] = 0;
915 pipeline
->urb
.start
[MESA_SHADER_TESS_EVAL
] = push_constant_chunks
;
916 pipeline
->urb
.size
[MESA_SHADER_TESS_EVAL
] = 1;
917 pipeline
->urb
.entries
[MESA_SHADER_TESS_EVAL
] = 0;
919 const unsigned stages
=
920 _mesa_bitcount(pipeline
->active_stages
& VK_SHADER_STAGE_ALL_GRAPHICS
);
921 unsigned size_per_stage
= stages
? (push_constant_kb
/ stages
) : 0;
922 unsigned used_kb
= 0;
924 /* Broadwell+ and Haswell gt3 require that the push constant sizes be in
925 * units of 2KB. Incidentally, these are the same platforms that have
926 * 32KB worth of push constant space.
928 if (push_constant_kb
== 32)
929 size_per_stage
&= ~1u;
931 for (int i
= MESA_SHADER_VERTEX
; i
< MESA_SHADER_FRAGMENT
; i
++) {
932 pipeline
->urb
.push_size
[i
] =
933 (pipeline
->active_stages
& (1 << i
)) ? size_per_stage
: 0;
934 used_kb
+= pipeline
->urb
.push_size
[i
];
935 assert(used_kb
<= push_constant_kb
);
938 pipeline
->urb
.push_size
[MESA_SHADER_FRAGMENT
] =
939 push_constant_kb
- used_kb
;
943 anv_pipeline_init_dynamic_state(struct anv_pipeline
*pipeline
,
944 const VkGraphicsPipelineCreateInfo
*pCreateInfo
)
946 anv_cmd_dirty_mask_t states
= ANV_CMD_DIRTY_DYNAMIC_ALL
;
947 ANV_FROM_HANDLE(anv_render_pass
, pass
, pCreateInfo
->renderPass
);
948 struct anv_subpass
*subpass
= &pass
->subpasses
[pCreateInfo
->subpass
];
950 pipeline
->dynamic_state
= default_dynamic_state
;
952 if (pCreateInfo
->pDynamicState
) {
953 /* Remove all of the states that are marked as dynamic */
954 uint32_t count
= pCreateInfo
->pDynamicState
->dynamicStateCount
;
955 for (uint32_t s
= 0; s
< count
; s
++)
956 states
&= ~(1 << pCreateInfo
->pDynamicState
->pDynamicStates
[s
]);
959 struct anv_dynamic_state
*dynamic
= &pipeline
->dynamic_state
;
961 dynamic
->viewport
.count
= pCreateInfo
->pViewportState
->viewportCount
;
962 if (states
& (1 << VK_DYNAMIC_STATE_VIEWPORT
)) {
963 typed_memcpy(dynamic
->viewport
.viewports
,
964 pCreateInfo
->pViewportState
->pViewports
,
965 pCreateInfo
->pViewportState
->viewportCount
);
968 dynamic
->scissor
.count
= pCreateInfo
->pViewportState
->scissorCount
;
969 if (states
& (1 << VK_DYNAMIC_STATE_SCISSOR
)) {
970 typed_memcpy(dynamic
->scissor
.scissors
,
971 pCreateInfo
->pViewportState
->pScissors
,
972 pCreateInfo
->pViewportState
->scissorCount
);
975 if (states
& (1 << VK_DYNAMIC_STATE_LINE_WIDTH
)) {
976 assert(pCreateInfo
->pRasterizationState
);
977 dynamic
->line_width
= pCreateInfo
->pRasterizationState
->lineWidth
;
980 if (states
& (1 << VK_DYNAMIC_STATE_DEPTH_BIAS
)) {
981 assert(pCreateInfo
->pRasterizationState
);
982 dynamic
->depth_bias
.bias
=
983 pCreateInfo
->pRasterizationState
->depthBiasConstantFactor
;
984 dynamic
->depth_bias
.clamp
=
985 pCreateInfo
->pRasterizationState
->depthBiasClamp
;
986 dynamic
->depth_bias
.slope
=
987 pCreateInfo
->pRasterizationState
->depthBiasSlopeFactor
;
990 if (states
& (1 << VK_DYNAMIC_STATE_BLEND_CONSTANTS
)) {
991 assert(pCreateInfo
->pColorBlendState
);
992 typed_memcpy(dynamic
->blend_constants
,
993 pCreateInfo
->pColorBlendState
->blendConstants
, 4);
996 /* If there is no depthstencil attachment, then don't read
997 * pDepthStencilState. The Vulkan spec states that pDepthStencilState may
998 * be NULL in this case. Even if pDepthStencilState is non-NULL, there is
999 * no need to override the depthstencil defaults in
1000 * anv_pipeline::dynamic_state when there is no depthstencil attachment.
1002 * From the Vulkan spec (20 Oct 2015, git-aa308cb):
1004 * pDepthStencilState [...] may only be NULL if renderPass and subpass
1005 * specify a subpass that has no depth/stencil attachment.
1007 if (subpass
->depth_stencil_attachment
!= VK_ATTACHMENT_UNUSED
) {
1008 if (states
& (1 << VK_DYNAMIC_STATE_DEPTH_BOUNDS
)) {
1009 assert(pCreateInfo
->pDepthStencilState
);
1010 dynamic
->depth_bounds
.min
=
1011 pCreateInfo
->pDepthStencilState
->minDepthBounds
;
1012 dynamic
->depth_bounds
.max
=
1013 pCreateInfo
->pDepthStencilState
->maxDepthBounds
;
1016 if (states
& (1 << VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK
)) {
1017 assert(pCreateInfo
->pDepthStencilState
);
1018 dynamic
->stencil_compare_mask
.front
=
1019 pCreateInfo
->pDepthStencilState
->front
.compareMask
;
1020 dynamic
->stencil_compare_mask
.back
=
1021 pCreateInfo
->pDepthStencilState
->back
.compareMask
;
1024 if (states
& (1 << VK_DYNAMIC_STATE_STENCIL_WRITE_MASK
)) {
1025 assert(pCreateInfo
->pDepthStencilState
);
1026 dynamic
->stencil_write_mask
.front
=
1027 pCreateInfo
->pDepthStencilState
->front
.writeMask
;
1028 dynamic
->stencil_write_mask
.back
=
1029 pCreateInfo
->pDepthStencilState
->back
.writeMask
;
1032 if (states
& (1 << VK_DYNAMIC_STATE_STENCIL_REFERENCE
)) {
1033 assert(pCreateInfo
->pDepthStencilState
);
1034 dynamic
->stencil_reference
.front
=
1035 pCreateInfo
->pDepthStencilState
->front
.reference
;
1036 dynamic
->stencil_reference
.back
=
1037 pCreateInfo
->pDepthStencilState
->back
.reference
;
1041 pipeline
->dynamic_state_mask
= states
;
1045 anv_pipeline_validate_create_info(const VkGraphicsPipelineCreateInfo
*info
)
1047 struct anv_render_pass
*renderpass
= NULL
;
1048 struct anv_subpass
*subpass
= NULL
;
1050 /* Assert that all required members of VkGraphicsPipelineCreateInfo are
1051 * present, as explained by the Vulkan (20 Oct 2015, git-aa308cb), Section
1052 * 4.2 Graphics Pipeline.
1054 assert(info
->sType
== VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO
);
1056 renderpass
= anv_render_pass_from_handle(info
->renderPass
);
1059 if (renderpass
!= &anv_meta_dummy_renderpass
) {
1060 assert(info
->subpass
< renderpass
->subpass_count
);
1061 subpass
= &renderpass
->subpasses
[info
->subpass
];
1064 assert(info
->stageCount
>= 1);
1065 assert(info
->pVertexInputState
);
1066 assert(info
->pInputAssemblyState
);
1067 assert(info
->pViewportState
);
1068 assert(info
->pRasterizationState
);
1070 if (subpass
&& subpass
->depth_stencil_attachment
!= VK_ATTACHMENT_UNUSED
)
1071 assert(info
->pDepthStencilState
);
1073 if (subpass
&& subpass
->color_count
> 0)
1074 assert(info
->pColorBlendState
);
1076 for (uint32_t i
= 0; i
< info
->stageCount
; ++i
) {
1077 switch (info
->pStages
[i
].stage
) {
1078 case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT
:
1079 case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT
:
1080 assert(info
->pTessellationState
);
1089 anv_pipeline_init(struct anv_pipeline
*pipeline
,
1090 struct anv_device
*device
,
1091 struct anv_pipeline_cache
*cache
,
1092 const VkGraphicsPipelineCreateInfo
*pCreateInfo
,
1093 const struct anv_graphics_pipeline_create_info
*extra
,
1094 const VkAllocationCallbacks
*alloc
)
1099 anv_pipeline_validate_create_info(pCreateInfo
);
1103 alloc
= &device
->alloc
;
1105 pipeline
->device
= device
;
1106 pipeline
->layout
= anv_pipeline_layout_from_handle(pCreateInfo
->layout
);
1108 result
= anv_reloc_list_init(&pipeline
->batch_relocs
, alloc
);
1109 if (result
!= VK_SUCCESS
)
1112 pipeline
->batch
.alloc
= alloc
;
1113 pipeline
->batch
.next
= pipeline
->batch
.start
= pipeline
->batch_data
;
1114 pipeline
->batch
.end
= pipeline
->batch
.start
+ sizeof(pipeline
->batch_data
);
1115 pipeline
->batch
.relocs
= &pipeline
->batch_relocs
;
1117 anv_pipeline_init_dynamic_state(pipeline
, pCreateInfo
);
1119 pipeline
->use_repclear
= extra
&& extra
->use_repclear
;
1121 pipeline
->needs_data_cache
= false;
1123 /* When we free the pipeline, we detect stages based on the NULL status
1124 * of various prog_data pointers. Make them NULL by default.
1126 memset(pipeline
->prog_data
, 0, sizeof(pipeline
->prog_data
));
1127 memset(pipeline
->scratch_start
, 0, sizeof(pipeline
->scratch_start
));
1128 memset(pipeline
->bindings
, 0, sizeof(pipeline
->bindings
));
1130 pipeline
->vs_simd8
= NO_KERNEL
;
1131 pipeline
->vs_vec4
= NO_KERNEL
;
1132 pipeline
->gs_kernel
= NO_KERNEL
;
1133 pipeline
->ps_ksp0
= NO_KERNEL
;
1135 pipeline
->active_stages
= 0;
1136 pipeline
->total_scratch
= 0;
1138 const VkPipelineShaderStageCreateInfo
*pStages
[MESA_SHADER_STAGES
] = { 0, };
1139 struct anv_shader_module
*modules
[MESA_SHADER_STAGES
] = { 0, };
1140 for (uint32_t i
= 0; i
< pCreateInfo
->stageCount
; i
++) {
1141 gl_shader_stage stage
= ffs(pCreateInfo
->pStages
[i
].stage
) - 1;
1142 pStages
[stage
] = &pCreateInfo
->pStages
[i
];
1143 modules
[stage
] = anv_shader_module_from_handle(pStages
[stage
]->module
);
1146 if (modules
[MESA_SHADER_VERTEX
]) {
1147 anv_pipeline_compile_vs(pipeline
, cache
, pCreateInfo
,
1148 modules
[MESA_SHADER_VERTEX
],
1149 pStages
[MESA_SHADER_VERTEX
]->pName
,
1150 pStages
[MESA_SHADER_VERTEX
]->pSpecializationInfo
);
1153 if (modules
[MESA_SHADER_TESS_CTRL
] || modules
[MESA_SHADER_TESS_EVAL
])
1154 anv_finishme("no tessellation support");
1156 if (modules
[MESA_SHADER_GEOMETRY
]) {
1157 anv_pipeline_compile_gs(pipeline
, cache
, pCreateInfo
,
1158 modules
[MESA_SHADER_GEOMETRY
],
1159 pStages
[MESA_SHADER_GEOMETRY
]->pName
,
1160 pStages
[MESA_SHADER_GEOMETRY
]->pSpecializationInfo
);
1163 if (modules
[MESA_SHADER_FRAGMENT
]) {
1164 anv_pipeline_compile_fs(pipeline
, cache
, pCreateInfo
, extra
,
1165 modules
[MESA_SHADER_FRAGMENT
],
1166 pStages
[MESA_SHADER_FRAGMENT
]->pName
,
1167 pStages
[MESA_SHADER_FRAGMENT
]->pSpecializationInfo
);
1170 if (!(pipeline
->active_stages
& VK_SHADER_STAGE_VERTEX_BIT
)) {
1171 /* Vertex is only optional if disable_vs is set */
1172 assert(extra
->disable_vs
);
1175 gen7_compute_urb_partition(pipeline
);
1177 const VkPipelineVertexInputStateCreateInfo
*vi_info
=
1178 pCreateInfo
->pVertexInputState
;
1180 uint64_t inputs_read
;
1181 if (extra
&& extra
->disable_vs
) {
1182 /* If the VS is disabled, just assume the user knows what they're
1183 * doing and apply the layout blindly. This can only come from
1184 * meta, so this *should* be safe.
1186 inputs_read
= ~0ull;
1188 inputs_read
= get_vs_prog_data(pipeline
)->inputs_read
;
1191 pipeline
->vb_used
= 0;
1192 for (uint32_t i
= 0; i
< vi_info
->vertexAttributeDescriptionCount
; i
++) {
1193 const VkVertexInputAttributeDescription
*desc
=
1194 &vi_info
->pVertexAttributeDescriptions
[i
];
1196 if (inputs_read
& (1 << (VERT_ATTRIB_GENERIC0
+ desc
->location
)))
1197 pipeline
->vb_used
|= 1 << desc
->binding
;
1200 for (uint32_t i
= 0; i
< vi_info
->vertexBindingDescriptionCount
; i
++) {
1201 const VkVertexInputBindingDescription
*desc
=
1202 &vi_info
->pVertexBindingDescriptions
[i
];
1204 pipeline
->binding_stride
[desc
->binding
] = desc
->stride
;
1206 /* Step rate is programmed per vertex element (attribute), not
1207 * binding. Set up a map of which bindings step per instance, for
1208 * reference by vertex element setup. */
1209 switch (desc
->inputRate
) {
1211 case VK_VERTEX_INPUT_RATE_VERTEX
:
1212 pipeline
->instancing_enable
[desc
->binding
] = false;
1214 case VK_VERTEX_INPUT_RATE_INSTANCE
:
1215 pipeline
->instancing_enable
[desc
->binding
] = true;
1220 const VkPipelineInputAssemblyStateCreateInfo
*ia_info
=
1221 pCreateInfo
->pInputAssemblyState
;
1222 pipeline
->primitive_restart
= ia_info
->primitiveRestartEnable
;
1223 pipeline
->topology
= vk_to_gen_primitive_type
[ia_info
->topology
];
1225 if (extra
&& extra
->use_rectlist
)
1226 pipeline
->topology
= _3DPRIM_RECTLIST
;
1228 while (anv_block_pool_size(&device
->scratch_block_pool
) <
1229 pipeline
->total_scratch
)
1230 anv_block_pool_alloc(&device
->scratch_block_pool
);
1236 anv_graphics_pipeline_create(
1238 VkPipelineCache _cache
,
1239 const VkGraphicsPipelineCreateInfo
*pCreateInfo
,
1240 const struct anv_graphics_pipeline_create_info
*extra
,
1241 const VkAllocationCallbacks
*pAllocator
,
1242 VkPipeline
*pPipeline
)
1244 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1245 ANV_FROM_HANDLE(anv_pipeline_cache
, cache
, _cache
);
1248 cache
= &device
->default_pipeline_cache
;
1250 switch (device
->info
.gen
) {
1252 if (device
->info
.is_haswell
)
1253 return gen75_graphics_pipeline_create(_device
, cache
, pCreateInfo
, extra
, pAllocator
, pPipeline
);
1255 return gen7_graphics_pipeline_create(_device
, cache
, pCreateInfo
, extra
, pAllocator
, pPipeline
);
1257 return gen8_graphics_pipeline_create(_device
, cache
, pCreateInfo
, extra
, pAllocator
, pPipeline
);
1259 return gen9_graphics_pipeline_create(_device
, cache
, pCreateInfo
, extra
, pAllocator
, pPipeline
);
1261 unreachable("unsupported gen\n");
1265 VkResult
anv_CreateGraphicsPipelines(
1267 VkPipelineCache pipelineCache
,
1269 const VkGraphicsPipelineCreateInfo
* pCreateInfos
,
1270 const VkAllocationCallbacks
* pAllocator
,
1271 VkPipeline
* pPipelines
)
1273 VkResult result
= VK_SUCCESS
;
1276 for (; i
< count
; i
++) {
1277 result
= anv_graphics_pipeline_create(_device
,
1280 NULL
, pAllocator
, &pPipelines
[i
]);
1281 if (result
!= VK_SUCCESS
) {
1282 for (unsigned j
= 0; j
< i
; j
++) {
1283 anv_DestroyPipeline(_device
, pPipelines
[j
], pAllocator
);
1293 static VkResult
anv_compute_pipeline_create(
1295 VkPipelineCache _cache
,
1296 const VkComputePipelineCreateInfo
* pCreateInfo
,
1297 const VkAllocationCallbacks
* pAllocator
,
1298 VkPipeline
* pPipeline
)
1300 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1301 ANV_FROM_HANDLE(anv_pipeline_cache
, cache
, _cache
);
1304 cache
= &device
->default_pipeline_cache
;
1306 switch (device
->info
.gen
) {
1308 if (device
->info
.is_haswell
)
1309 return gen75_compute_pipeline_create(_device
, cache
, pCreateInfo
, pAllocator
, pPipeline
);
1311 return gen7_compute_pipeline_create(_device
, cache
, pCreateInfo
, pAllocator
, pPipeline
);
1313 return gen8_compute_pipeline_create(_device
, cache
, pCreateInfo
, pAllocator
, pPipeline
);
1315 return gen9_compute_pipeline_create(_device
, cache
, pCreateInfo
, pAllocator
, pPipeline
);
1317 unreachable("unsupported gen\n");
1321 VkResult
anv_CreateComputePipelines(
1323 VkPipelineCache pipelineCache
,
1325 const VkComputePipelineCreateInfo
* pCreateInfos
,
1326 const VkAllocationCallbacks
* pAllocator
,
1327 VkPipeline
* pPipelines
)
1329 VkResult result
= VK_SUCCESS
;
1332 for (; i
< count
; i
++) {
1333 result
= anv_compute_pipeline_create(_device
, pipelineCache
,
1335 pAllocator
, &pPipelines
[i
]);
1336 if (result
!= VK_SUCCESS
) {
1337 for (unsigned j
= 0; j
< i
; j
++) {
1338 anv_DestroyPipeline(_device
, pPipelines
[j
], pAllocator
);