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 "common/gen_l3_config.h"
32 #include "anv_private.h"
33 #include "compiler/brw_nir.h"
35 #include "spirv/nir_spirv.h"
38 /* Needed for SWIZZLE macros */
39 #include "program/prog_instruction.h"
43 VkResult
anv_CreateShaderModule(
45 const VkShaderModuleCreateInfo
* pCreateInfo
,
46 const VkAllocationCallbacks
* pAllocator
,
47 VkShaderModule
* pShaderModule
)
49 ANV_FROM_HANDLE(anv_device
, device
, _device
);
50 struct anv_shader_module
*module
;
52 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO
);
53 assert(pCreateInfo
->flags
== 0);
55 module
= vk_alloc2(&device
->alloc
, pAllocator
,
56 sizeof(*module
) + pCreateInfo
->codeSize
, 8,
57 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
59 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
61 module
->size
= pCreateInfo
->codeSize
;
62 memcpy(module
->data
, pCreateInfo
->pCode
, module
->size
);
64 _mesa_sha1_compute(module
->data
, module
->size
, module
->sha1
);
66 *pShaderModule
= anv_shader_module_to_handle(module
);
71 void anv_DestroyShaderModule(
73 VkShaderModule _module
,
74 const VkAllocationCallbacks
* pAllocator
)
76 ANV_FROM_HANDLE(anv_device
, device
, _device
);
77 ANV_FROM_HANDLE(anv_shader_module
, module
, _module
);
82 vk_free2(&device
->alloc
, pAllocator
, module
);
85 #define SPIR_V_MAGIC_NUMBER 0x07230203
87 static const uint64_t stage_to_debug
[] = {
88 [MESA_SHADER_VERTEX
] = DEBUG_VS
,
89 [MESA_SHADER_TESS_CTRL
] = DEBUG_TCS
,
90 [MESA_SHADER_TESS_EVAL
] = DEBUG_TES
,
91 [MESA_SHADER_GEOMETRY
] = DEBUG_GS
,
92 [MESA_SHADER_FRAGMENT
] = DEBUG_WM
,
93 [MESA_SHADER_COMPUTE
] = DEBUG_CS
,
96 /* Eventually, this will become part of anv_CreateShader. Unfortunately,
97 * we can't do that yet because we don't have the ability to copy nir.
100 anv_shader_compile_to_nir(struct anv_pipeline
*pipeline
,
102 const struct anv_shader_module
*module
,
103 const char *entrypoint_name
,
104 gl_shader_stage stage
,
105 const VkSpecializationInfo
*spec_info
)
107 const struct anv_device
*device
= pipeline
->device
;
109 const struct brw_compiler
*compiler
=
110 device
->instance
->physicalDevice
.compiler
;
111 const nir_shader_compiler_options
*nir_options
=
112 compiler
->glsl_compiler_options
[stage
].NirOptions
;
114 uint32_t *spirv
= (uint32_t *) module
->data
;
115 assert(spirv
[0] == SPIR_V_MAGIC_NUMBER
);
116 assert(module
->size
% 4 == 0);
118 uint32_t num_spec_entries
= 0;
119 struct nir_spirv_specialization
*spec_entries
= NULL
;
120 if (spec_info
&& spec_info
->mapEntryCount
> 0) {
121 num_spec_entries
= spec_info
->mapEntryCount
;
122 spec_entries
= malloc(num_spec_entries
* sizeof(*spec_entries
));
123 for (uint32_t i
= 0; i
< num_spec_entries
; i
++) {
124 VkSpecializationMapEntry entry
= spec_info
->pMapEntries
[i
];
125 const void *data
= spec_info
->pData
+ entry
.offset
;
126 assert(data
+ entry
.size
<= spec_info
->pData
+ spec_info
->dataSize
);
128 spec_entries
[i
].id
= spec_info
->pMapEntries
[i
].constantID
;
129 if (spec_info
->dataSize
== 8)
130 spec_entries
[i
].data64
= *(const uint64_t *)data
;
132 spec_entries
[i
].data32
= *(const uint32_t *)data
;
136 struct spirv_to_nir_options spirv_options
= {
137 .lower_workgroup_access_to_offsets
= true,
138 .lower_ubo_ssbo_access_to_offsets
= true,
140 .float64
= device
->instance
->physicalDevice
.info
.gen
>= 8,
141 .int64
= device
->instance
->physicalDevice
.info
.gen
>= 8,
142 .tessellation
= true,
143 .device_group
= true,
144 .draw_parameters
= true,
145 .image_write_without_format
= true,
148 .variable_pointers
= true,
149 .storage_16bit
= device
->instance
->physicalDevice
.info
.gen
>= 8,
150 .int16
= device
->instance
->physicalDevice
.info
.gen
>= 8,
151 .shader_viewport_index_layer
= true,
152 .subgroup_arithmetic
= true,
153 .subgroup_basic
= true,
154 .subgroup_ballot
= true,
155 .subgroup_quad
= true,
156 .subgroup_shuffle
= true,
157 .subgroup_vote
= true,
158 .stencil_export
= device
->instance
->physicalDevice
.info
.gen
>= 9,
159 .storage_8bit
= device
->instance
->physicalDevice
.info
.gen
>= 8,
160 .post_depth_coverage
= device
->instance
->physicalDevice
.info
.gen
>= 9,
162 .ubo_ptr_type
= glsl_vector_type(GLSL_TYPE_UINT
, 2),
163 .ssbo_ptr_type
= glsl_vector_type(GLSL_TYPE_UINT
, 2),
164 .push_const_ptr_type
= glsl_uint_type(),
165 .shared_ptr_type
= glsl_uint_type(),
168 nir_function
*entry_point
=
169 spirv_to_nir(spirv
, module
->size
/ 4,
170 spec_entries
, num_spec_entries
,
171 stage
, entrypoint_name
, &spirv_options
, nir_options
);
172 nir_shader
*nir
= entry_point
->shader
;
173 assert(nir
->info
.stage
== stage
);
174 nir_validate_shader(nir
, "after spirv_to_nir");
175 ralloc_steal(mem_ctx
, nir
);
179 if (unlikely(INTEL_DEBUG
& stage_to_debug
[stage
])) {
180 fprintf(stderr
, "NIR (from SPIR-V) for %s shader:\n",
181 gl_shader_stage_name(stage
));
182 nir_print_shader(nir
, stderr
);
185 /* We have to lower away local constant initializers right before we
186 * inline functions. That way they get properly initialized at the top
187 * of the function and not at the top of its caller.
189 NIR_PASS_V(nir
, nir_lower_constant_initializers
, nir_var_local
);
190 NIR_PASS_V(nir
, nir_lower_returns
);
191 NIR_PASS_V(nir
, nir_inline_functions
);
192 NIR_PASS_V(nir
, nir_opt_deref
);
194 /* Pick off the single entrypoint that we want */
195 foreach_list_typed_safe(nir_function
, func
, node
, &nir
->functions
) {
196 if (func
!= entry_point
)
197 exec_node_remove(&func
->node
);
199 assert(exec_list_length(&nir
->functions
) == 1);
201 /* Now that we've deleted all but the main function, we can go ahead and
202 * lower the rest of the constant initializers. We do this here so that
203 * nir_remove_dead_variables and split_per_member_structs below see the
204 * corresponding stores.
206 NIR_PASS_V(nir
, nir_lower_constant_initializers
, ~0);
208 /* Split member structs. We do this before lower_io_to_temporaries so that
209 * it doesn't lower system values to temporaries by accident.
211 NIR_PASS_V(nir
, nir_split_var_copies
);
212 NIR_PASS_V(nir
, nir_split_per_member_structs
);
214 NIR_PASS_V(nir
, nir_remove_dead_variables
,
215 nir_var_shader_in
| nir_var_shader_out
| nir_var_system_value
);
217 if (stage
== MESA_SHADER_FRAGMENT
)
218 NIR_PASS_V(nir
, nir_lower_wpos_center
, pipeline
->sample_shading_enable
);
220 NIR_PASS_V(nir
, nir_propagate_invariant
);
221 NIR_PASS_V(nir
, nir_lower_io_to_temporaries
,
222 entry_point
->impl
, true, false);
224 /* Vulkan uses the separate-shader linking model */
225 nir
->info
.separate_shader
= true;
227 nir
= brw_preprocess_nir(compiler
, nir
);
229 if (stage
== MESA_SHADER_FRAGMENT
)
230 NIR_PASS_V(nir
, anv_nir_lower_input_attachments
);
235 void anv_DestroyPipeline(
237 VkPipeline _pipeline
,
238 const VkAllocationCallbacks
* pAllocator
)
240 ANV_FROM_HANDLE(anv_device
, device
, _device
);
241 ANV_FROM_HANDLE(anv_pipeline
, pipeline
, _pipeline
);
246 anv_reloc_list_finish(&pipeline
->batch_relocs
,
247 pAllocator
? pAllocator
: &device
->alloc
);
248 if (pipeline
->blend_state
.map
)
249 anv_state_pool_free(&device
->dynamic_state_pool
, pipeline
->blend_state
);
251 for (unsigned s
= 0; s
< MESA_SHADER_STAGES
; s
++) {
252 if (pipeline
->shaders
[s
])
253 anv_shader_bin_unref(device
, pipeline
->shaders
[s
]);
256 vk_free2(&device
->alloc
, pAllocator
, pipeline
);
259 static const uint32_t vk_to_gen_primitive_type
[] = {
260 [VK_PRIMITIVE_TOPOLOGY_POINT_LIST
] = _3DPRIM_POINTLIST
,
261 [VK_PRIMITIVE_TOPOLOGY_LINE_LIST
] = _3DPRIM_LINELIST
,
262 [VK_PRIMITIVE_TOPOLOGY_LINE_STRIP
] = _3DPRIM_LINESTRIP
,
263 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST
] = _3DPRIM_TRILIST
,
264 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP
] = _3DPRIM_TRISTRIP
,
265 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN
] = _3DPRIM_TRIFAN
,
266 [VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY
] = _3DPRIM_LINELIST_ADJ
,
267 [VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY
] = _3DPRIM_LINESTRIP_ADJ
,
268 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY
] = _3DPRIM_TRILIST_ADJ
,
269 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY
] = _3DPRIM_TRISTRIP_ADJ
,
273 populate_sampler_prog_key(const struct gen_device_info
*devinfo
,
274 struct brw_sampler_prog_key_data
*key
)
276 /* Almost all multisampled textures are compressed. The only time when we
277 * don't compress a multisampled texture is for 16x MSAA with a surface
278 * width greater than 8k which is a bit of an edge case. Since the sampler
279 * just ignores the MCS parameter to ld2ms when MCS is disabled, it's safe
280 * to tell the compiler to always assume compression.
282 key
->compressed_multisample_layout_mask
= ~0;
284 /* SkyLake added support for 16x MSAA. With this came a new message for
285 * reading from a 16x MSAA surface with compression. The new message was
286 * needed because now the MCS data is 64 bits instead of 32 or lower as is
287 * the case for 8x, 4x, and 2x. The key->msaa_16 bit-field controls which
288 * message we use. Fortunately, the 16x message works for 8x, 4x, and 2x
289 * so we can just use it unconditionally. This may not be quite as
290 * efficient but it saves us from recompiling.
292 if (devinfo
->gen
>= 9)
295 /* XXX: Handle texture swizzle on HSW- */
296 for (int i
= 0; i
< MAX_SAMPLERS
; i
++) {
297 /* Assume color sampler, no swizzling. (Works for BDW+) */
298 key
->swizzles
[i
] = SWIZZLE_XYZW
;
303 populate_vs_prog_key(const struct gen_device_info
*devinfo
,
304 struct brw_vs_prog_key
*key
)
306 memset(key
, 0, sizeof(*key
));
308 populate_sampler_prog_key(devinfo
, &key
->tex
);
310 /* XXX: Handle vertex input work-arounds */
312 /* XXX: Handle sampler_prog_key */
316 populate_tcs_prog_key(const struct gen_device_info
*devinfo
,
317 unsigned input_vertices
,
318 struct brw_tcs_prog_key
*key
)
320 memset(key
, 0, sizeof(*key
));
322 populate_sampler_prog_key(devinfo
, &key
->tex
);
324 key
->input_vertices
= input_vertices
;
328 populate_tes_prog_key(const struct gen_device_info
*devinfo
,
329 struct brw_tes_prog_key
*key
)
331 memset(key
, 0, sizeof(*key
));
333 populate_sampler_prog_key(devinfo
, &key
->tex
);
337 populate_gs_prog_key(const struct gen_device_info
*devinfo
,
338 struct brw_gs_prog_key
*key
)
340 memset(key
, 0, sizeof(*key
));
342 populate_sampler_prog_key(devinfo
, &key
->tex
);
346 populate_wm_prog_key(const struct gen_device_info
*devinfo
,
347 const struct anv_subpass
*subpass
,
348 const VkPipelineMultisampleStateCreateInfo
*ms_info
,
349 struct brw_wm_prog_key
*key
)
351 memset(key
, 0, sizeof(*key
));
353 populate_sampler_prog_key(devinfo
, &key
->tex
);
355 /* We set this to 0 here and set to the actual value before we call
358 key
->input_slots_valid
= 0;
360 /* Vulkan doesn't specify a default */
361 key
->high_quality_derivatives
= false;
363 /* XXX Vulkan doesn't appear to specify */
364 key
->clamp_fragment_color
= false;
366 assert(subpass
->color_count
<= MAX_RTS
);
367 for (uint32_t i
= 0; i
< subpass
->color_count
; i
++) {
368 if (subpass
->color_attachments
[i
].attachment
!= VK_ATTACHMENT_UNUSED
)
369 key
->color_outputs_valid
|= (1 << i
);
372 key
->nr_color_regions
= util_bitcount(key
->color_outputs_valid
);
374 key
->replicate_alpha
= key
->nr_color_regions
> 1 &&
375 ms_info
&& ms_info
->alphaToCoverageEnable
;
378 /* We should probably pull this out of the shader, but it's fairly
379 * harmless to compute it and then let dead-code take care of it.
381 if (ms_info
->rasterizationSamples
> 1) {
382 key
->persample_interp
=
383 (ms_info
->minSampleShading
* ms_info
->rasterizationSamples
) > 1;
384 key
->multisample_fbo
= true;
387 key
->frag_coord_adds_sample_pos
= ms_info
->sampleShadingEnable
;
392 populate_cs_prog_key(const struct gen_device_info
*devinfo
,
393 struct brw_cs_prog_key
*key
)
395 memset(key
, 0, sizeof(*key
));
397 populate_sampler_prog_key(devinfo
, &key
->tex
);
400 struct anv_pipeline_stage
{
401 gl_shader_stage stage
;
403 const struct anv_shader_module
*module
;
404 const char *entrypoint
;
405 const VkSpecializationInfo
*spec_info
;
407 union brw_any_prog_key key
;
410 gl_shader_stage stage
;
411 unsigned char sha1
[20];
416 struct anv_pipeline_binding surface_to_descriptor
[256];
417 struct anv_pipeline_binding sampler_to_descriptor
[256];
418 struct anv_pipeline_bind_map bind_map
;
420 union brw_any_prog_data prog_data
;
424 anv_pipeline_hash_shader(struct mesa_sha1
*ctx
,
425 struct anv_pipeline_stage
*stage
)
427 _mesa_sha1_update(ctx
, stage
->module
->sha1
, sizeof(stage
->module
->sha1
));
428 _mesa_sha1_update(ctx
, stage
->entrypoint
, strlen(stage
->entrypoint
));
429 _mesa_sha1_update(ctx
, &stage
->stage
, sizeof(stage
->stage
));
430 if (stage
->spec_info
) {
431 _mesa_sha1_update(ctx
, stage
->spec_info
->pMapEntries
,
432 stage
->spec_info
->mapEntryCount
*
433 sizeof(*stage
->spec_info
->pMapEntries
));
434 _mesa_sha1_update(ctx
, stage
->spec_info
->pData
,
435 stage
->spec_info
->dataSize
);
437 _mesa_sha1_update(ctx
, &stage
->key
, brw_prog_key_size(stage
->stage
));
441 anv_pipeline_hash_graphics(struct anv_pipeline
*pipeline
,
442 struct anv_pipeline_layout
*layout
,
443 struct anv_pipeline_stage
*stages
,
444 unsigned char *sha1_out
)
446 struct mesa_sha1 ctx
;
447 _mesa_sha1_init(&ctx
);
449 _mesa_sha1_update(&ctx
, &pipeline
->subpass
->view_mask
,
450 sizeof(pipeline
->subpass
->view_mask
));
453 _mesa_sha1_update(&ctx
, layout
->sha1
, sizeof(layout
->sha1
));
455 const bool rba
= pipeline
->device
->robust_buffer_access
;
456 _mesa_sha1_update(&ctx
, &rba
, sizeof(rba
));
458 for (unsigned s
= 0; s
< MESA_SHADER_STAGES
; s
++) {
459 if (stages
[s
].entrypoint
)
460 anv_pipeline_hash_shader(&ctx
, &stages
[s
]);
463 _mesa_sha1_final(&ctx
, sha1_out
);
467 anv_pipeline_hash_compute(struct anv_pipeline
*pipeline
,
468 struct anv_pipeline_layout
*layout
,
469 struct anv_pipeline_stage
*stage
,
470 unsigned char *sha1_out
)
472 struct mesa_sha1 ctx
;
473 _mesa_sha1_init(&ctx
);
476 _mesa_sha1_update(&ctx
, layout
->sha1
, sizeof(layout
->sha1
));
478 const bool rba
= pipeline
->device
->robust_buffer_access
;
479 _mesa_sha1_update(&ctx
, &rba
, sizeof(rba
));
481 anv_pipeline_hash_shader(&ctx
, stage
);
483 _mesa_sha1_final(&ctx
, sha1_out
);
487 anv_pipeline_lower_nir(struct anv_pipeline
*pipeline
,
489 struct anv_pipeline_stage
*stage
,
490 struct anv_pipeline_layout
*layout
)
492 const struct brw_compiler
*compiler
=
493 pipeline
->device
->instance
->physicalDevice
.compiler
;
495 struct brw_stage_prog_data
*prog_data
= &stage
->prog_data
.base
;
496 nir_shader
*nir
= stage
->nir
;
498 NIR_PASS_V(nir
, anv_nir_lower_ycbcr_textures
, layout
);
500 NIR_PASS_V(nir
, anv_nir_lower_push_constants
);
502 if (nir
->info
.stage
!= MESA_SHADER_COMPUTE
)
503 NIR_PASS_V(nir
, anv_nir_lower_multiview
, pipeline
->subpass
->view_mask
);
505 if (nir
->info
.stage
== MESA_SHADER_COMPUTE
)
506 prog_data
->total_shared
= nir
->num_shared
;
508 nir_shader_gather_info(nir
, nir_shader_get_entrypoint(nir
));
510 if (nir
->num_uniforms
> 0) {
511 assert(prog_data
->nr_params
== 0);
513 /* If the shader uses any push constants at all, we'll just give
514 * them the maximum possible number
516 assert(nir
->num_uniforms
<= MAX_PUSH_CONSTANTS_SIZE
);
517 nir
->num_uniforms
= MAX_PUSH_CONSTANTS_SIZE
;
518 prog_data
->nr_params
+= MAX_PUSH_CONSTANTS_SIZE
/ sizeof(float);
519 prog_data
->param
= ralloc_array(mem_ctx
, uint32_t, prog_data
->nr_params
);
521 /* We now set the param values to be offsets into a
522 * anv_push_constant_data structure. Since the compiler doesn't
523 * actually dereference any of the gl_constant_value pointers in the
524 * params array, it doesn't really matter what we put here.
526 struct anv_push_constants
*null_data
= NULL
;
527 /* Fill out the push constants section of the param array */
528 for (unsigned i
= 0; i
< MAX_PUSH_CONSTANTS_SIZE
/ sizeof(float); i
++) {
529 prog_data
->param
[i
] = ANV_PARAM_PUSH(
530 (uintptr_t)&null_data
->client_data
[i
* sizeof(float)]);
534 if (nir
->info
.num_ssbos
> 0 || nir
->info
.num_images
> 0)
535 pipeline
->needs_data_cache
= true;
537 NIR_PASS_V(nir
, brw_nir_lower_image_load_store
, compiler
->devinfo
);
539 /* Apply the actual pipeline layout to UBOs, SSBOs, and textures */
541 anv_nir_apply_pipeline_layout(&pipeline
->device
->instance
->physicalDevice
,
542 pipeline
->device
->robust_buffer_access
,
543 layout
, nir
, prog_data
,
547 if (nir
->info
.stage
!= MESA_SHADER_COMPUTE
)
548 brw_nir_analyze_ubo_ranges(compiler
, nir
, NULL
, prog_data
->ubo_ranges
);
550 assert(nir
->num_uniforms
== prog_data
->nr_params
* 4);
556 anv_fill_binding_table(struct brw_stage_prog_data
*prog_data
, unsigned bias
)
558 prog_data
->binding_table
.size_bytes
= 0;
559 prog_data
->binding_table
.texture_start
= bias
;
560 prog_data
->binding_table
.gather_texture_start
= bias
;
561 prog_data
->binding_table
.ubo_start
= bias
;
562 prog_data
->binding_table
.ssbo_start
= bias
;
563 prog_data
->binding_table
.image_start
= bias
;
567 anv_pipeline_link_vs(const struct brw_compiler
*compiler
,
568 struct anv_pipeline_stage
*vs_stage
,
569 struct anv_pipeline_stage
*next_stage
)
571 anv_fill_binding_table(&vs_stage
->prog_data
.vs
.base
.base
, 0);
574 brw_nir_link_shaders(compiler
, &vs_stage
->nir
, &next_stage
->nir
);
577 static const unsigned *
578 anv_pipeline_compile_vs(const struct brw_compiler
*compiler
,
580 struct anv_pipeline_stage
*vs_stage
)
582 brw_compute_vue_map(compiler
->devinfo
,
583 &vs_stage
->prog_data
.vs
.base
.vue_map
,
584 vs_stage
->nir
->info
.outputs_written
,
585 vs_stage
->nir
->info
.separate_shader
);
587 return brw_compile_vs(compiler
, NULL
, mem_ctx
, &vs_stage
->key
.vs
,
588 &vs_stage
->prog_data
.vs
, vs_stage
->nir
, -1, NULL
);
592 merge_tess_info(struct shader_info
*tes_info
,
593 const struct shader_info
*tcs_info
)
595 /* The Vulkan 1.0.38 spec, section 21.1 Tessellator says:
597 * "PointMode. Controls generation of points rather than triangles
598 * or lines. This functionality defaults to disabled, and is
599 * enabled if either shader stage includes the execution mode.
601 * and about Triangles, Quads, IsoLines, VertexOrderCw, VertexOrderCcw,
602 * PointMode, SpacingEqual, SpacingFractionalEven, SpacingFractionalOdd,
603 * and OutputVertices, it says:
605 * "One mode must be set in at least one of the tessellation
608 * So, the fields can be set in either the TCS or TES, but they must
609 * agree if set in both. Our backend looks at TES, so bitwise-or in
610 * the values from the TCS.
612 assert(tcs_info
->tess
.tcs_vertices_out
== 0 ||
613 tes_info
->tess
.tcs_vertices_out
== 0 ||
614 tcs_info
->tess
.tcs_vertices_out
== tes_info
->tess
.tcs_vertices_out
);
615 tes_info
->tess
.tcs_vertices_out
|= tcs_info
->tess
.tcs_vertices_out
;
617 assert(tcs_info
->tess
.spacing
== TESS_SPACING_UNSPECIFIED
||
618 tes_info
->tess
.spacing
== TESS_SPACING_UNSPECIFIED
||
619 tcs_info
->tess
.spacing
== tes_info
->tess
.spacing
);
620 tes_info
->tess
.spacing
|= tcs_info
->tess
.spacing
;
622 assert(tcs_info
->tess
.primitive_mode
== 0 ||
623 tes_info
->tess
.primitive_mode
== 0 ||
624 tcs_info
->tess
.primitive_mode
== tes_info
->tess
.primitive_mode
);
625 tes_info
->tess
.primitive_mode
|= tcs_info
->tess
.primitive_mode
;
626 tes_info
->tess
.ccw
|= tcs_info
->tess
.ccw
;
627 tes_info
->tess
.point_mode
|= tcs_info
->tess
.point_mode
;
631 anv_pipeline_link_tcs(const struct brw_compiler
*compiler
,
632 struct anv_pipeline_stage
*tcs_stage
,
633 struct anv_pipeline_stage
*tes_stage
)
635 assert(tes_stage
&& tes_stage
->stage
== MESA_SHADER_TESS_EVAL
);
637 anv_fill_binding_table(&tcs_stage
->prog_data
.tcs
.base
.base
, 0);
639 brw_nir_link_shaders(compiler
, &tcs_stage
->nir
, &tes_stage
->nir
);
641 nir_lower_patch_vertices(tes_stage
->nir
,
642 tcs_stage
->nir
->info
.tess
.tcs_vertices_out
,
645 /* Copy TCS info into the TES info */
646 merge_tess_info(&tes_stage
->nir
->info
, &tcs_stage
->nir
->info
);
648 anv_fill_binding_table(&tcs_stage
->prog_data
.tcs
.base
.base
, 0);
649 anv_fill_binding_table(&tes_stage
->prog_data
.tes
.base
.base
, 0);
651 /* Whacking the key after cache lookup is a bit sketchy, but all of
652 * this comes from the SPIR-V, which is part of the hash used for the
653 * pipeline cache. So it should be safe.
655 tcs_stage
->key
.tcs
.tes_primitive_mode
=
656 tes_stage
->nir
->info
.tess
.primitive_mode
;
657 tcs_stage
->key
.tcs
.quads_workaround
=
658 compiler
->devinfo
->gen
< 9 &&
659 tes_stage
->nir
->info
.tess
.primitive_mode
== 7 /* GL_QUADS */ &&
660 tes_stage
->nir
->info
.tess
.spacing
== TESS_SPACING_EQUAL
;
663 static const unsigned *
664 anv_pipeline_compile_tcs(const struct brw_compiler
*compiler
,
666 struct anv_pipeline_stage
*tcs_stage
,
667 struct anv_pipeline_stage
*prev_stage
)
669 tcs_stage
->key
.tcs
.outputs_written
=
670 tcs_stage
->nir
->info
.outputs_written
;
671 tcs_stage
->key
.tcs
.patch_outputs_written
=
672 tcs_stage
->nir
->info
.patch_outputs_written
;
674 return brw_compile_tcs(compiler
, NULL
, mem_ctx
, &tcs_stage
->key
.tcs
,
675 &tcs_stage
->prog_data
.tcs
, tcs_stage
->nir
,
680 anv_pipeline_link_tes(const struct brw_compiler
*compiler
,
681 struct anv_pipeline_stage
*tes_stage
,
682 struct anv_pipeline_stage
*next_stage
)
684 anv_fill_binding_table(&tes_stage
->prog_data
.tes
.base
.base
, 0);
687 brw_nir_link_shaders(compiler
, &tes_stage
->nir
, &next_stage
->nir
);
690 static const unsigned *
691 anv_pipeline_compile_tes(const struct brw_compiler
*compiler
,
693 struct anv_pipeline_stage
*tes_stage
,
694 struct anv_pipeline_stage
*tcs_stage
)
696 tes_stage
->key
.tes
.inputs_read
=
697 tcs_stage
->nir
->info
.outputs_written
;
698 tes_stage
->key
.tes
.patch_inputs_read
=
699 tcs_stage
->nir
->info
.patch_outputs_written
;
701 return brw_compile_tes(compiler
, NULL
, mem_ctx
, &tes_stage
->key
.tes
,
702 &tcs_stage
->prog_data
.tcs
.base
.vue_map
,
703 &tes_stage
->prog_data
.tes
, tes_stage
->nir
,
708 anv_pipeline_link_gs(const struct brw_compiler
*compiler
,
709 struct anv_pipeline_stage
*gs_stage
,
710 struct anv_pipeline_stage
*next_stage
)
712 anv_fill_binding_table(&gs_stage
->prog_data
.gs
.base
.base
, 0);
715 brw_nir_link_shaders(compiler
, &gs_stage
->nir
, &next_stage
->nir
);
718 static const unsigned *
719 anv_pipeline_compile_gs(const struct brw_compiler
*compiler
,
721 struct anv_pipeline_stage
*gs_stage
,
722 struct anv_pipeline_stage
*prev_stage
)
724 brw_compute_vue_map(compiler
->devinfo
,
725 &gs_stage
->prog_data
.gs
.base
.vue_map
,
726 gs_stage
->nir
->info
.outputs_written
,
727 gs_stage
->nir
->info
.separate_shader
);
729 return brw_compile_gs(compiler
, NULL
, mem_ctx
, &gs_stage
->key
.gs
,
730 &gs_stage
->prog_data
.gs
, gs_stage
->nir
,
735 anv_pipeline_link_fs(const struct brw_compiler
*compiler
,
736 struct anv_pipeline_stage
*stage
)
738 unsigned num_rts
= 0;
739 const int max_rt
= FRAG_RESULT_DATA7
- FRAG_RESULT_DATA0
+ 1;
740 struct anv_pipeline_binding rt_bindings
[max_rt
];
741 nir_function_impl
*impl
= nir_shader_get_entrypoint(stage
->nir
);
742 int rt_to_bindings
[max_rt
];
743 memset(rt_to_bindings
, -1, sizeof(rt_to_bindings
));
744 bool rt_used
[max_rt
];
745 memset(rt_used
, 0, sizeof(rt_used
));
747 /* Flag used render targets */
748 nir_foreach_variable_safe(var
, &stage
->nir
->outputs
) {
749 if (var
->data
.location
< FRAG_RESULT_DATA0
)
752 const unsigned rt
= var
->data
.location
- FRAG_RESULT_DATA0
;
753 /* Unused or out-of-bounds */
754 if (rt
>= MAX_RTS
|| !(stage
->key
.wm
.color_outputs_valid
& (1 << rt
)))
757 const unsigned array_len
=
758 glsl_type_is_array(var
->type
) ? glsl_get_length(var
->type
) : 1;
759 assert(rt
+ array_len
<= max_rt
);
761 for (unsigned i
= 0; i
< array_len
; i
++)
762 rt_used
[rt
+ i
] = true;
765 /* Set new, compacted, location */
766 for (unsigned i
= 0; i
< max_rt
; i
++) {
770 rt_to_bindings
[i
] = num_rts
;
771 rt_bindings
[rt_to_bindings
[i
]] = (struct anv_pipeline_binding
) {
772 .set
= ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS
,
779 bool deleted_output
= false;
780 nir_foreach_variable_safe(var
, &stage
->nir
->outputs
) {
781 if (var
->data
.location
< FRAG_RESULT_DATA0
)
784 const unsigned rt
= var
->data
.location
- FRAG_RESULT_DATA0
;
786 !(stage
->key
.wm
.color_outputs_valid
& (1 << rt
))) {
787 /* Unused or out-of-bounds, throw it away */
788 deleted_output
= true;
789 var
->data
.mode
= nir_var_local
;
790 exec_node_remove(&var
->node
);
791 exec_list_push_tail(&impl
->locals
, &var
->node
);
795 /* Give it the new location */
796 assert(rt_to_bindings
[rt
] != -1);
797 var
->data
.location
= rt_to_bindings
[rt
] + FRAG_RESULT_DATA0
;
801 nir_fixup_deref_modes(stage
->nir
);
804 /* If we have no render targets, we need a null render target */
805 rt_bindings
[0] = (struct anv_pipeline_binding
) {
806 .set
= ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS
,
813 /* Now that we've determined the actual number of render targets, adjust
814 * the key accordingly.
816 stage
->key
.wm
.nr_color_regions
= num_rts
;
817 stage
->key
.wm
.color_outputs_valid
= (1 << num_rts
) - 1;
819 assert(num_rts
<= max_rt
);
820 assert(stage
->bind_map
.surface_count
== 0);
821 typed_memcpy(stage
->bind_map
.surface_to_descriptor
,
822 rt_bindings
, num_rts
);
823 stage
->bind_map
.surface_count
+= num_rts
;
825 anv_fill_binding_table(&stage
->prog_data
.wm
.base
, 0);
828 static const unsigned *
829 anv_pipeline_compile_fs(const struct brw_compiler
*compiler
,
831 struct anv_pipeline_stage
*fs_stage
,
832 struct anv_pipeline_stage
*prev_stage
)
834 /* TODO: we could set this to 0 based on the information in nir_shader, but
835 * we need this before we call spirv_to_nir.
838 fs_stage
->key
.wm
.input_slots_valid
=
839 prev_stage
->prog_data
.vue
.vue_map
.slots_valid
;
841 const unsigned *code
=
842 brw_compile_fs(compiler
, NULL
, mem_ctx
, &fs_stage
->key
.wm
,
843 &fs_stage
->prog_data
.wm
, fs_stage
->nir
,
844 NULL
, -1, -1, -1, true, false, NULL
, NULL
);
846 if (fs_stage
->key
.wm
.nr_color_regions
== 0 &&
847 !fs_stage
->prog_data
.wm
.has_side_effects
&&
848 !fs_stage
->prog_data
.wm
.uses_kill
&&
849 fs_stage
->prog_data
.wm
.computed_depth_mode
== BRW_PSCDEPTH_OFF
&&
850 !fs_stage
->prog_data
.wm
.computed_stencil
) {
851 /* This fragment shader has no outputs and no side effects. Go ahead
852 * and return the code pointer so we don't accidentally think the
853 * compile failed but zero out prog_data which will set program_size to
854 * zero and disable the stage.
856 memset(&fs_stage
->prog_data
, 0, sizeof(fs_stage
->prog_data
));
863 anv_pipeline_compile_graphics(struct anv_pipeline
*pipeline
,
864 struct anv_pipeline_cache
*cache
,
865 const VkGraphicsPipelineCreateInfo
*info
)
867 const struct brw_compiler
*compiler
=
868 pipeline
->device
->instance
->physicalDevice
.compiler
;
869 struct anv_pipeline_stage stages
[MESA_SHADER_STAGES
] = {};
871 pipeline
->active_stages
= 0;
874 for (uint32_t i
= 0; i
< info
->stageCount
; i
++) {
875 const VkPipelineShaderStageCreateInfo
*sinfo
= &info
->pStages
[i
];
876 gl_shader_stage stage
= vk_to_mesa_shader_stage(sinfo
->stage
);
878 pipeline
->active_stages
|= sinfo
->stage
;
880 stages
[stage
].stage
= stage
;
881 stages
[stage
].module
= anv_shader_module_from_handle(sinfo
->module
);
882 stages
[stage
].entrypoint
= sinfo
->pName
;
883 stages
[stage
].spec_info
= sinfo
->pSpecializationInfo
;
885 const struct gen_device_info
*devinfo
= &pipeline
->device
->info
;
887 case MESA_SHADER_VERTEX
:
888 populate_vs_prog_key(devinfo
, &stages
[stage
].key
.vs
);
890 case MESA_SHADER_TESS_CTRL
:
891 populate_tcs_prog_key(devinfo
,
892 info
->pTessellationState
->patchControlPoints
,
893 &stages
[stage
].key
.tcs
);
895 case MESA_SHADER_TESS_EVAL
:
896 populate_tes_prog_key(devinfo
, &stages
[stage
].key
.tes
);
898 case MESA_SHADER_GEOMETRY
:
899 populate_gs_prog_key(devinfo
, &stages
[stage
].key
.gs
);
901 case MESA_SHADER_FRAGMENT
:
902 populate_wm_prog_key(devinfo
, pipeline
->subpass
,
903 info
->pMultisampleState
,
904 &stages
[stage
].key
.wm
);
907 unreachable("Invalid graphics shader stage");
911 if (pipeline
->active_stages
& VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT
)
912 pipeline
->active_stages
|= VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT
;
914 assert(pipeline
->active_stages
& VK_SHADER_STAGE_VERTEX_BIT
);
916 ANV_FROM_HANDLE(anv_pipeline_layout
, layout
, info
->layout
);
918 unsigned char sha1
[20];
919 anv_pipeline_hash_graphics(pipeline
, layout
, stages
, sha1
);
922 for (unsigned s
= 0; s
< MESA_SHADER_STAGES
; s
++) {
923 if (!stages
[s
].entrypoint
)
926 stages
[s
].cache_key
.stage
= s
;
927 memcpy(stages
[s
].cache_key
.sha1
, sha1
, sizeof(sha1
));
929 struct anv_shader_bin
*bin
=
930 anv_device_search_for_kernel(pipeline
->device
, cache
,
931 &stages
[s
].cache_key
,
932 sizeof(stages
[s
].cache_key
));
935 pipeline
->shaders
[s
] = bin
;
939 if (found
== __builtin_popcount(pipeline
->active_stages
)) {
940 /* We found all our shaders in the cache. We're done. */
942 } else if (found
> 0) {
943 /* We found some but not all of our shaders. This shouldn't happen
944 * most of the time but it can if we have a partially populated
947 assert(found
< __builtin_popcount(pipeline
->active_stages
));
949 vk_debug_report(&pipeline
->device
->instance
->debug_report_callbacks
,
950 VK_DEBUG_REPORT_WARNING_BIT_EXT
|
951 VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT
,
952 VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_CACHE_EXT
,
953 (uint64_t)(uintptr_t)cache
,
955 "Found a partial pipeline in the cache. This is "
956 "most likely caused by an incomplete pipeline cache "
959 /* We're going to have to recompile anyway, so just throw away our
960 * references to the shaders in the cache. We'll get them out of the
961 * cache again as part of the compilation process.
963 for (unsigned s
= 0; s
< MESA_SHADER_STAGES
; s
++) {
964 if (pipeline
->shaders
[s
]) {
965 anv_shader_bin_unref(pipeline
->device
, pipeline
->shaders
[s
]);
966 pipeline
->shaders
[s
] = NULL
;
971 void *pipeline_ctx
= ralloc_context(NULL
);
973 for (unsigned s
= 0; s
< MESA_SHADER_STAGES
; s
++) {
974 if (!stages
[s
].entrypoint
)
977 assert(stages
[s
].stage
== s
);
978 assert(pipeline
->shaders
[s
] == NULL
);
980 stages
[s
].bind_map
= (struct anv_pipeline_bind_map
) {
981 .surface_to_descriptor
= stages
[s
].surface_to_descriptor
,
982 .sampler_to_descriptor
= stages
[s
].sampler_to_descriptor
985 stages
[s
].nir
= anv_shader_compile_to_nir(pipeline
, pipeline_ctx
,
987 stages
[s
].entrypoint
,
989 stages
[s
].spec_info
);
990 if (stages
[s
].nir
== NULL
) {
991 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
996 /* Walk backwards to link */
997 struct anv_pipeline_stage
*next_stage
= NULL
;
998 for (int s
= MESA_SHADER_STAGES
- 1; s
>= 0; s
--) {
999 if (!stages
[s
].entrypoint
)
1003 case MESA_SHADER_VERTEX
:
1004 anv_pipeline_link_vs(compiler
, &stages
[s
], next_stage
);
1006 case MESA_SHADER_TESS_CTRL
:
1007 anv_pipeline_link_tcs(compiler
, &stages
[s
], next_stage
);
1009 case MESA_SHADER_TESS_EVAL
:
1010 anv_pipeline_link_tes(compiler
, &stages
[s
], next_stage
);
1012 case MESA_SHADER_GEOMETRY
:
1013 anv_pipeline_link_gs(compiler
, &stages
[s
], next_stage
);
1015 case MESA_SHADER_FRAGMENT
:
1016 anv_pipeline_link_fs(compiler
, &stages
[s
]);
1019 unreachable("Invalid graphics shader stage");
1022 next_stage
= &stages
[s
];
1025 struct anv_pipeline_stage
*prev_stage
= NULL
;
1026 for (unsigned s
= 0; s
< MESA_SHADER_STAGES
; s
++) {
1027 if (!stages
[s
].entrypoint
)
1030 void *stage_ctx
= ralloc_context(NULL
);
1032 anv_pipeline_lower_nir(pipeline
, stage_ctx
, &stages
[s
], layout
);
1034 const unsigned *code
;
1036 case MESA_SHADER_VERTEX
:
1037 code
= anv_pipeline_compile_vs(compiler
, stage_ctx
, &stages
[s
]);
1039 case MESA_SHADER_TESS_CTRL
:
1040 code
= anv_pipeline_compile_tcs(compiler
, stage_ctx
,
1041 &stages
[s
], prev_stage
);
1043 case MESA_SHADER_TESS_EVAL
:
1044 code
= anv_pipeline_compile_tes(compiler
, stage_ctx
,
1045 &stages
[s
], prev_stage
);
1047 case MESA_SHADER_GEOMETRY
:
1048 code
= anv_pipeline_compile_gs(compiler
, stage_ctx
,
1049 &stages
[s
], prev_stage
);
1051 case MESA_SHADER_FRAGMENT
:
1052 code
= anv_pipeline_compile_fs(compiler
, stage_ctx
,
1053 &stages
[s
], prev_stage
);
1056 unreachable("Invalid graphics shader stage");
1059 ralloc_free(stage_ctx
);
1060 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1064 struct anv_shader_bin
*bin
=
1065 anv_device_upload_kernel(pipeline
->device
, cache
,
1066 &stages
[s
].cache_key
,
1067 sizeof(stages
[s
].cache_key
),
1068 code
, stages
[s
].prog_data
.base
.program_size
,
1069 stages
[s
].nir
->constant_data
,
1070 stages
[s
].nir
->constant_data_size
,
1071 &stages
[s
].prog_data
.base
,
1072 brw_prog_data_size(s
),
1073 &stages
[s
].bind_map
);
1075 ralloc_free(stage_ctx
);
1076 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1080 pipeline
->shaders
[s
] = bin
;
1081 ralloc_free(stage_ctx
);
1083 prev_stage
= &stages
[s
];
1086 ralloc_free(pipeline_ctx
);
1090 if (pipeline
->shaders
[MESA_SHADER_FRAGMENT
] &&
1091 pipeline
->shaders
[MESA_SHADER_FRAGMENT
]->prog_data
->program_size
== 0) {
1092 /* This can happen if we decided to implicitly disable the fragment
1093 * shader. See anv_pipeline_compile_fs().
1095 anv_shader_bin_unref(pipeline
->device
,
1096 pipeline
->shaders
[MESA_SHADER_FRAGMENT
]);
1097 pipeline
->shaders
[MESA_SHADER_FRAGMENT
] = NULL
;
1098 pipeline
->active_stages
&= ~VK_SHADER_STAGE_FRAGMENT_BIT
;
1104 ralloc_free(pipeline_ctx
);
1106 for (unsigned s
= 0; s
< MESA_SHADER_STAGES
; s
++) {
1107 if (pipeline
->shaders
[s
])
1108 anv_shader_bin_unref(pipeline
->device
, pipeline
->shaders
[s
]);
1115 anv_pipeline_compile_cs(struct anv_pipeline
*pipeline
,
1116 struct anv_pipeline_cache
*cache
,
1117 const VkComputePipelineCreateInfo
*info
,
1118 const struct anv_shader_module
*module
,
1119 const char *entrypoint
,
1120 const VkSpecializationInfo
*spec_info
)
1122 const struct brw_compiler
*compiler
=
1123 pipeline
->device
->instance
->physicalDevice
.compiler
;
1125 struct anv_pipeline_stage stage
= {
1126 .stage
= MESA_SHADER_COMPUTE
,
1128 .entrypoint
= entrypoint
,
1129 .spec_info
= spec_info
,
1131 .stage
= MESA_SHADER_COMPUTE
,
1135 struct anv_shader_bin
*bin
= NULL
;
1137 populate_cs_prog_key(&pipeline
->device
->info
, &stage
.key
.cs
);
1139 ANV_FROM_HANDLE(anv_pipeline_layout
, layout
, info
->layout
);
1141 anv_pipeline_hash_compute(pipeline
, layout
, &stage
, stage
.cache_key
.sha1
);
1142 bin
= anv_device_search_for_kernel(pipeline
->device
, cache
, &stage
.cache_key
,
1143 sizeof(stage
.cache_key
));
1146 stage
.bind_map
= (struct anv_pipeline_bind_map
) {
1147 .surface_to_descriptor
= stage
.surface_to_descriptor
,
1148 .sampler_to_descriptor
= stage
.sampler_to_descriptor
1151 void *mem_ctx
= ralloc_context(NULL
);
1153 stage
.nir
= anv_shader_compile_to_nir(pipeline
, mem_ctx
,
1158 if (stage
.nir
== NULL
) {
1159 ralloc_free(mem_ctx
);
1160 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1163 anv_pipeline_lower_nir(pipeline
, mem_ctx
, &stage
, layout
);
1165 NIR_PASS_V(stage
.nir
, anv_nir_add_base_work_group_id
,
1166 &stage
.prog_data
.cs
);
1168 anv_fill_binding_table(&stage
.prog_data
.cs
.base
, 1);
1170 const unsigned *shader_code
=
1171 brw_compile_cs(compiler
, NULL
, mem_ctx
, &stage
.key
.cs
,
1172 &stage
.prog_data
.cs
, stage
.nir
, -1, NULL
);
1173 if (shader_code
== NULL
) {
1174 ralloc_free(mem_ctx
);
1175 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1178 const unsigned code_size
= stage
.prog_data
.base
.program_size
;
1179 bin
= anv_device_upload_kernel(pipeline
->device
, cache
,
1180 &stage
.cache_key
, sizeof(stage
.cache_key
),
1181 shader_code
, code_size
,
1182 stage
.nir
->constant_data
,
1183 stage
.nir
->constant_data_size
,
1184 &stage
.prog_data
.base
,
1185 sizeof(stage
.prog_data
.cs
),
1188 ralloc_free(mem_ctx
);
1189 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1192 ralloc_free(mem_ctx
);
1195 pipeline
->active_stages
= VK_SHADER_STAGE_COMPUTE_BIT
;
1196 pipeline
->shaders
[MESA_SHADER_COMPUTE
] = bin
;
1202 * Copy pipeline state not marked as dynamic.
1203 * Dynamic state is pipeline state which hasn't been provided at pipeline
1204 * creation time, but is dynamically provided afterwards using various
1205 * vkCmdSet* functions.
1207 * The set of state considered "non_dynamic" is determined by the pieces of
1208 * state that have their corresponding VkDynamicState enums omitted from
1209 * VkPipelineDynamicStateCreateInfo::pDynamicStates.
1211 * @param[out] pipeline Destination non_dynamic state.
1212 * @param[in] pCreateInfo Source of non_dynamic state to be copied.
1215 copy_non_dynamic_state(struct anv_pipeline
*pipeline
,
1216 const VkGraphicsPipelineCreateInfo
*pCreateInfo
)
1218 anv_cmd_dirty_mask_t states
= ANV_CMD_DIRTY_DYNAMIC_ALL
;
1219 struct anv_subpass
*subpass
= pipeline
->subpass
;
1221 pipeline
->dynamic_state
= default_dynamic_state
;
1223 if (pCreateInfo
->pDynamicState
) {
1224 /* Remove all of the states that are marked as dynamic */
1225 uint32_t count
= pCreateInfo
->pDynamicState
->dynamicStateCount
;
1226 for (uint32_t s
= 0; s
< count
; s
++)
1227 states
&= ~(1 << pCreateInfo
->pDynamicState
->pDynamicStates
[s
]);
1230 struct anv_dynamic_state
*dynamic
= &pipeline
->dynamic_state
;
1232 /* Section 9.2 of the Vulkan 1.0.15 spec says:
1234 * pViewportState is [...] NULL if the pipeline
1235 * has rasterization disabled.
1237 if (!pCreateInfo
->pRasterizationState
->rasterizerDiscardEnable
) {
1238 assert(pCreateInfo
->pViewportState
);
1240 dynamic
->viewport
.count
= pCreateInfo
->pViewportState
->viewportCount
;
1241 if (states
& (1 << VK_DYNAMIC_STATE_VIEWPORT
)) {
1242 typed_memcpy(dynamic
->viewport
.viewports
,
1243 pCreateInfo
->pViewportState
->pViewports
,
1244 pCreateInfo
->pViewportState
->viewportCount
);
1247 dynamic
->scissor
.count
= pCreateInfo
->pViewportState
->scissorCount
;
1248 if (states
& (1 << VK_DYNAMIC_STATE_SCISSOR
)) {
1249 typed_memcpy(dynamic
->scissor
.scissors
,
1250 pCreateInfo
->pViewportState
->pScissors
,
1251 pCreateInfo
->pViewportState
->scissorCount
);
1255 if (states
& (1 << VK_DYNAMIC_STATE_LINE_WIDTH
)) {
1256 assert(pCreateInfo
->pRasterizationState
);
1257 dynamic
->line_width
= pCreateInfo
->pRasterizationState
->lineWidth
;
1260 if (states
& (1 << VK_DYNAMIC_STATE_DEPTH_BIAS
)) {
1261 assert(pCreateInfo
->pRasterizationState
);
1262 dynamic
->depth_bias
.bias
=
1263 pCreateInfo
->pRasterizationState
->depthBiasConstantFactor
;
1264 dynamic
->depth_bias
.clamp
=
1265 pCreateInfo
->pRasterizationState
->depthBiasClamp
;
1266 dynamic
->depth_bias
.slope
=
1267 pCreateInfo
->pRasterizationState
->depthBiasSlopeFactor
;
1270 /* Section 9.2 of the Vulkan 1.0.15 spec says:
1272 * pColorBlendState is [...] NULL if the pipeline has rasterization
1273 * disabled or if the subpass of the render pass the pipeline is
1274 * created against does not use any color attachments.
1276 bool uses_color_att
= false;
1277 for (unsigned i
= 0; i
< subpass
->color_count
; ++i
) {
1278 if (subpass
->color_attachments
[i
].attachment
!= VK_ATTACHMENT_UNUSED
) {
1279 uses_color_att
= true;
1284 if (uses_color_att
&&
1285 !pCreateInfo
->pRasterizationState
->rasterizerDiscardEnable
) {
1286 assert(pCreateInfo
->pColorBlendState
);
1288 if (states
& (1 << VK_DYNAMIC_STATE_BLEND_CONSTANTS
))
1289 typed_memcpy(dynamic
->blend_constants
,
1290 pCreateInfo
->pColorBlendState
->blendConstants
, 4);
1293 /* If there is no depthstencil attachment, then don't read
1294 * pDepthStencilState. The Vulkan spec states that pDepthStencilState may
1295 * be NULL in this case. Even if pDepthStencilState is non-NULL, there is
1296 * no need to override the depthstencil defaults in
1297 * anv_pipeline::dynamic_state when there is no depthstencil attachment.
1299 * Section 9.2 of the Vulkan 1.0.15 spec says:
1301 * pDepthStencilState is [...] NULL if the pipeline has rasterization
1302 * disabled or if the subpass of the render pass the pipeline is created
1303 * against does not use a depth/stencil attachment.
1305 if (!pCreateInfo
->pRasterizationState
->rasterizerDiscardEnable
&&
1306 subpass
->depth_stencil_attachment
) {
1307 assert(pCreateInfo
->pDepthStencilState
);
1309 if (states
& (1 << VK_DYNAMIC_STATE_DEPTH_BOUNDS
)) {
1310 dynamic
->depth_bounds
.min
=
1311 pCreateInfo
->pDepthStencilState
->minDepthBounds
;
1312 dynamic
->depth_bounds
.max
=
1313 pCreateInfo
->pDepthStencilState
->maxDepthBounds
;
1316 if (states
& (1 << VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK
)) {
1317 dynamic
->stencil_compare_mask
.front
=
1318 pCreateInfo
->pDepthStencilState
->front
.compareMask
;
1319 dynamic
->stencil_compare_mask
.back
=
1320 pCreateInfo
->pDepthStencilState
->back
.compareMask
;
1323 if (states
& (1 << VK_DYNAMIC_STATE_STENCIL_WRITE_MASK
)) {
1324 dynamic
->stencil_write_mask
.front
=
1325 pCreateInfo
->pDepthStencilState
->front
.writeMask
;
1326 dynamic
->stencil_write_mask
.back
=
1327 pCreateInfo
->pDepthStencilState
->back
.writeMask
;
1330 if (states
& (1 << VK_DYNAMIC_STATE_STENCIL_REFERENCE
)) {
1331 dynamic
->stencil_reference
.front
=
1332 pCreateInfo
->pDepthStencilState
->front
.reference
;
1333 dynamic
->stencil_reference
.back
=
1334 pCreateInfo
->pDepthStencilState
->back
.reference
;
1338 pipeline
->dynamic_state_mask
= states
;
1342 anv_pipeline_validate_create_info(const VkGraphicsPipelineCreateInfo
*info
)
1345 struct anv_render_pass
*renderpass
= NULL
;
1346 struct anv_subpass
*subpass
= NULL
;
1348 /* Assert that all required members of VkGraphicsPipelineCreateInfo are
1349 * present. See the Vulkan 1.0.28 spec, Section 9.2 Graphics Pipelines.
1351 assert(info
->sType
== VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO
);
1353 renderpass
= anv_render_pass_from_handle(info
->renderPass
);
1356 assert(info
->subpass
< renderpass
->subpass_count
);
1357 subpass
= &renderpass
->subpasses
[info
->subpass
];
1359 assert(info
->stageCount
>= 1);
1360 assert(info
->pVertexInputState
);
1361 assert(info
->pInputAssemblyState
);
1362 assert(info
->pRasterizationState
);
1363 if (!info
->pRasterizationState
->rasterizerDiscardEnable
) {
1364 assert(info
->pViewportState
);
1365 assert(info
->pMultisampleState
);
1367 if (subpass
&& subpass
->depth_stencil_attachment
)
1368 assert(info
->pDepthStencilState
);
1370 if (subpass
&& subpass
->color_count
> 0) {
1371 bool all_color_unused
= true;
1372 for (int i
= 0; i
< subpass
->color_count
; i
++) {
1373 if (subpass
->color_attachments
[i
].attachment
!= VK_ATTACHMENT_UNUSED
)
1374 all_color_unused
= false;
1376 /* pColorBlendState is ignored if the pipeline has rasterization
1377 * disabled or if the subpass of the render pass the pipeline is
1378 * created against does not use any color attachments.
1380 assert(info
->pColorBlendState
|| all_color_unused
);
1384 for (uint32_t i
= 0; i
< info
->stageCount
; ++i
) {
1385 switch (info
->pStages
[i
].stage
) {
1386 case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT
:
1387 case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT
:
1388 assert(info
->pTessellationState
);
1398 * Calculate the desired L3 partitioning based on the current state of the
1399 * pipeline. For now this simply returns the conservative defaults calculated
1400 * by get_default_l3_weights(), but we could probably do better by gathering
1401 * more statistics from the pipeline state (e.g. guess of expected URB usage
1402 * and bound surfaces), or by using feed-back from performance counters.
1405 anv_pipeline_setup_l3_config(struct anv_pipeline
*pipeline
, bool needs_slm
)
1407 const struct gen_device_info
*devinfo
= &pipeline
->device
->info
;
1409 const struct gen_l3_weights w
=
1410 gen_get_default_l3_weights(devinfo
, pipeline
->needs_data_cache
, needs_slm
);
1412 pipeline
->urb
.l3_config
= gen_get_l3_config(devinfo
, w
);
1413 pipeline
->urb
.total_size
=
1414 gen_get_l3_config_urb_size(devinfo
, pipeline
->urb
.l3_config
);
1418 anv_pipeline_init(struct anv_pipeline
*pipeline
,
1419 struct anv_device
*device
,
1420 struct anv_pipeline_cache
*cache
,
1421 const VkGraphicsPipelineCreateInfo
*pCreateInfo
,
1422 const VkAllocationCallbacks
*alloc
)
1426 anv_pipeline_validate_create_info(pCreateInfo
);
1429 alloc
= &device
->alloc
;
1431 pipeline
->device
= device
;
1433 ANV_FROM_HANDLE(anv_render_pass
, render_pass
, pCreateInfo
->renderPass
);
1434 assert(pCreateInfo
->subpass
< render_pass
->subpass_count
);
1435 pipeline
->subpass
= &render_pass
->subpasses
[pCreateInfo
->subpass
];
1437 result
= anv_reloc_list_init(&pipeline
->batch_relocs
, alloc
);
1438 if (result
!= VK_SUCCESS
)
1441 pipeline
->batch
.alloc
= alloc
;
1442 pipeline
->batch
.next
= pipeline
->batch
.start
= pipeline
->batch_data
;
1443 pipeline
->batch
.end
= pipeline
->batch
.start
+ sizeof(pipeline
->batch_data
);
1444 pipeline
->batch
.relocs
= &pipeline
->batch_relocs
;
1445 pipeline
->batch
.status
= VK_SUCCESS
;
1447 copy_non_dynamic_state(pipeline
, pCreateInfo
);
1448 pipeline
->depth_clamp_enable
= pCreateInfo
->pRasterizationState
&&
1449 pCreateInfo
->pRasterizationState
->depthClampEnable
;
1451 pipeline
->sample_shading_enable
= pCreateInfo
->pMultisampleState
&&
1452 pCreateInfo
->pMultisampleState
->sampleShadingEnable
;
1454 pipeline
->needs_data_cache
= false;
1456 /* When we free the pipeline, we detect stages based on the NULL status
1457 * of various prog_data pointers. Make them NULL by default.
1459 memset(pipeline
->shaders
, 0, sizeof(pipeline
->shaders
));
1461 result
= anv_pipeline_compile_graphics(pipeline
, cache
, pCreateInfo
);
1462 if (result
!= VK_SUCCESS
) {
1463 anv_reloc_list_finish(&pipeline
->batch_relocs
, alloc
);
1467 assert(pipeline
->shaders
[MESA_SHADER_VERTEX
]);
1469 anv_pipeline_setup_l3_config(pipeline
, false);
1471 const VkPipelineVertexInputStateCreateInfo
*vi_info
=
1472 pCreateInfo
->pVertexInputState
;
1474 const uint64_t inputs_read
= get_vs_prog_data(pipeline
)->inputs_read
;
1476 pipeline
->vb_used
= 0;
1477 for (uint32_t i
= 0; i
< vi_info
->vertexAttributeDescriptionCount
; i
++) {
1478 const VkVertexInputAttributeDescription
*desc
=
1479 &vi_info
->pVertexAttributeDescriptions
[i
];
1481 if (inputs_read
& (1ull << (VERT_ATTRIB_GENERIC0
+ desc
->location
)))
1482 pipeline
->vb_used
|= 1 << desc
->binding
;
1485 for (uint32_t i
= 0; i
< vi_info
->vertexBindingDescriptionCount
; i
++) {
1486 const VkVertexInputBindingDescription
*desc
=
1487 &vi_info
->pVertexBindingDescriptions
[i
];
1489 pipeline
->vb
[desc
->binding
].stride
= desc
->stride
;
1491 /* Step rate is programmed per vertex element (attribute), not
1492 * binding. Set up a map of which bindings step per instance, for
1493 * reference by vertex element setup. */
1494 switch (desc
->inputRate
) {
1496 case VK_VERTEX_INPUT_RATE_VERTEX
:
1497 pipeline
->vb
[desc
->binding
].instanced
= false;
1499 case VK_VERTEX_INPUT_RATE_INSTANCE
:
1500 pipeline
->vb
[desc
->binding
].instanced
= true;
1504 pipeline
->vb
[desc
->binding
].instance_divisor
= 1;
1507 const VkPipelineVertexInputDivisorStateCreateInfoEXT
*vi_div_state
=
1508 vk_find_struct_const(vi_info
->pNext
,
1509 PIPELINE_VERTEX_INPUT_DIVISOR_STATE_CREATE_INFO_EXT
);
1511 for (uint32_t i
= 0; i
< vi_div_state
->vertexBindingDivisorCount
; i
++) {
1512 const VkVertexInputBindingDivisorDescriptionEXT
*desc
=
1513 &vi_div_state
->pVertexBindingDivisors
[i
];
1515 pipeline
->vb
[desc
->binding
].instance_divisor
= desc
->divisor
;
1519 /* Our implementation of VK_KHR_multiview uses instancing to draw the
1520 * different views. If the client asks for instancing, we need to multiply
1521 * the instance divisor by the number of views ensure that we repeat the
1522 * client's per-instance data once for each view.
1524 if (pipeline
->subpass
->view_mask
) {
1525 const uint32_t view_count
= anv_subpass_view_count(pipeline
->subpass
);
1526 for (uint32_t vb
= 0; vb
< MAX_VBS
; vb
++) {
1527 if (pipeline
->vb
[vb
].instanced
)
1528 pipeline
->vb
[vb
].instance_divisor
*= view_count
;
1532 const VkPipelineInputAssemblyStateCreateInfo
*ia_info
=
1533 pCreateInfo
->pInputAssemblyState
;
1534 const VkPipelineTessellationStateCreateInfo
*tess_info
=
1535 pCreateInfo
->pTessellationState
;
1536 pipeline
->primitive_restart
= ia_info
->primitiveRestartEnable
;
1538 if (anv_pipeline_has_stage(pipeline
, MESA_SHADER_TESS_EVAL
))
1539 pipeline
->topology
= _3DPRIM_PATCHLIST(tess_info
->patchControlPoints
);
1541 pipeline
->topology
= vk_to_gen_primitive_type
[ia_info
->topology
];