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"
37 /* Needed for SWIZZLE macros */
38 #include "program/prog_instruction.h"
42 VkResult
anv_CreateShaderModule(
44 const VkShaderModuleCreateInfo
* pCreateInfo
,
45 const VkAllocationCallbacks
* pAllocator
,
46 VkShaderModule
* pShaderModule
)
48 ANV_FROM_HANDLE(anv_device
, device
, _device
);
49 struct anv_shader_module
*module
;
51 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO
);
52 assert(pCreateInfo
->flags
== 0);
54 module
= vk_alloc2(&device
->alloc
, pAllocator
,
55 sizeof(*module
) + pCreateInfo
->codeSize
, 8,
56 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
58 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
);
81 vk_free2(&device
->alloc
, pAllocator
, module
);
84 #define SPIR_V_MAGIC_NUMBER 0x07230203
86 /* Eventually, this will become part of anv_CreateShader. Unfortunately,
87 * we can't do that yet because we don't have the ability to copy nir.
90 anv_shader_compile_to_nir(struct anv_pipeline
*pipeline
,
91 struct anv_shader_module
*module
,
92 const char *entrypoint_name
,
93 gl_shader_stage stage
,
94 const VkSpecializationInfo
*spec_info
)
96 const struct anv_device
*device
= pipeline
->device
;
98 const struct brw_compiler
*compiler
=
99 device
->instance
->physicalDevice
.compiler
;
100 const nir_shader_compiler_options
*nir_options
=
101 compiler
->glsl_compiler_options
[stage
].NirOptions
;
103 uint32_t *spirv
= (uint32_t *) module
->data
;
104 assert(spirv
[0] == SPIR_V_MAGIC_NUMBER
);
105 assert(module
->size
% 4 == 0);
107 uint32_t num_spec_entries
= 0;
108 struct nir_spirv_specialization
*spec_entries
= NULL
;
109 if (spec_info
&& spec_info
->mapEntryCount
> 0) {
110 num_spec_entries
= spec_info
->mapEntryCount
;
111 spec_entries
= malloc(num_spec_entries
* sizeof(*spec_entries
));
112 for (uint32_t i
= 0; i
< num_spec_entries
; i
++) {
113 VkSpecializationMapEntry entry
= spec_info
->pMapEntries
[i
];
114 const void *data
= spec_info
->pData
+ entry
.offset
;
115 assert(data
+ entry
.size
<= spec_info
->pData
+ spec_info
->dataSize
);
117 spec_entries
[i
].id
= spec_info
->pMapEntries
[i
].constantID
;
118 if (spec_info
->dataSize
== 8)
119 spec_entries
[i
].data64
= *(const uint64_t *)data
;
121 spec_entries
[i
].data32
= *(const uint32_t *)data
;
125 const struct nir_spirv_supported_extensions supported_ext
= {
126 .float64
= device
->instance
->physicalDevice
.info
.gen
>= 8,
127 .int64
= device
->instance
->physicalDevice
.info
.gen
>= 8,
128 .tessellation
= true,
129 .draw_parameters
= true,
130 .image_write_without_format
= true,
132 .variable_pointers
= true,
135 nir_function
*entry_point
=
136 spirv_to_nir(spirv
, module
->size
/ 4,
137 spec_entries
, num_spec_entries
,
138 stage
, entrypoint_name
, &supported_ext
, nir_options
);
139 nir_shader
*nir
= entry_point
->shader
;
140 assert(nir
->stage
== stage
);
141 nir_validate_shader(nir
);
145 /* We have to lower away local constant initializers right before we
146 * inline functions. That way they get properly initialized at the top
147 * of the function and not at the top of its caller.
149 NIR_PASS_V(nir
, nir_lower_constant_initializers
, nir_var_local
);
150 NIR_PASS_V(nir
, nir_lower_returns
);
151 NIR_PASS_V(nir
, nir_inline_functions
);
153 /* Pick off the single entrypoint that we want */
154 foreach_list_typed_safe(nir_function
, func
, node
, &nir
->functions
) {
155 if (func
!= entry_point
)
156 exec_node_remove(&func
->node
);
158 assert(exec_list_length(&nir
->functions
) == 1);
159 entry_point
->name
= ralloc_strdup(entry_point
, "main");
161 NIR_PASS_V(nir
, nir_remove_dead_variables
,
162 nir_var_shader_in
| nir_var_shader_out
| nir_var_system_value
);
164 if (stage
== MESA_SHADER_FRAGMENT
)
165 NIR_PASS_V(nir
, nir_lower_wpos_center
, pipeline
->sample_shading_enable
);
167 /* Now that we've deleted all but the main function, we can go ahead and
168 * lower the rest of the constant initializers.
170 NIR_PASS_V(nir
, nir_lower_constant_initializers
, ~0);
171 NIR_PASS_V(nir
, nir_propagate_invariant
);
172 NIR_PASS_V(nir
, nir_lower_io_to_temporaries
,
173 entry_point
->impl
, true, false);
174 NIR_PASS_V(nir
, nir_lower_system_values
);
176 /* Vulkan uses the separate-shader linking model */
177 nir
->info
.separate_shader
= true;
179 nir
= brw_preprocess_nir(compiler
, nir
);
181 NIR_PASS_V(nir
, nir_lower_clip_cull_distance_arrays
);
183 if (stage
== MESA_SHADER_FRAGMENT
)
184 NIR_PASS_V(nir
, anv_nir_lower_input_attachments
);
189 void anv_DestroyPipeline(
191 VkPipeline _pipeline
,
192 const VkAllocationCallbacks
* pAllocator
)
194 ANV_FROM_HANDLE(anv_device
, device
, _device
);
195 ANV_FROM_HANDLE(anv_pipeline
, pipeline
, _pipeline
);
200 anv_reloc_list_finish(&pipeline
->batch_relocs
,
201 pAllocator
? pAllocator
: &device
->alloc
);
202 if (pipeline
->blend_state
.map
)
203 anv_state_pool_free(&device
->dynamic_state_pool
, pipeline
->blend_state
);
205 for (unsigned s
= 0; s
< MESA_SHADER_STAGES
; s
++) {
206 if (pipeline
->shaders
[s
])
207 anv_shader_bin_unref(device
, pipeline
->shaders
[s
]);
210 vk_free2(&device
->alloc
, pAllocator
, pipeline
);
213 static const uint32_t vk_to_gen_primitive_type
[] = {
214 [VK_PRIMITIVE_TOPOLOGY_POINT_LIST
] = _3DPRIM_POINTLIST
,
215 [VK_PRIMITIVE_TOPOLOGY_LINE_LIST
] = _3DPRIM_LINELIST
,
216 [VK_PRIMITIVE_TOPOLOGY_LINE_STRIP
] = _3DPRIM_LINESTRIP
,
217 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST
] = _3DPRIM_TRILIST
,
218 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP
] = _3DPRIM_TRISTRIP
,
219 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN
] = _3DPRIM_TRIFAN
,
220 [VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY
] = _3DPRIM_LINELIST_ADJ
,
221 [VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY
] = _3DPRIM_LINESTRIP_ADJ
,
222 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY
] = _3DPRIM_TRILIST_ADJ
,
223 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY
] = _3DPRIM_TRISTRIP_ADJ
,
227 populate_sampler_prog_key(const struct gen_device_info
*devinfo
,
228 struct brw_sampler_prog_key_data
*key
)
230 /* Almost all multisampled textures are compressed. The only time when we
231 * don't compress a multisampled texture is for 16x MSAA with a surface
232 * width greater than 8k which is a bit of an edge case. Since the sampler
233 * just ignores the MCS parameter to ld2ms when MCS is disabled, it's safe
234 * to tell the compiler to always assume compression.
236 key
->compressed_multisample_layout_mask
= ~0;
238 /* SkyLake added support for 16x MSAA. With this came a new message for
239 * reading from a 16x MSAA surface with compression. The new message was
240 * needed because now the MCS data is 64 bits instead of 32 or lower as is
241 * the case for 8x, 4x, and 2x. The key->msaa_16 bit-field controls which
242 * message we use. Fortunately, the 16x message works for 8x, 4x, and 2x
243 * so we can just use it unconditionally. This may not be quite as
244 * efficient but it saves us from recompiling.
246 if (devinfo
->gen
>= 9)
249 /* XXX: Handle texture swizzle on HSW- */
250 for (int i
= 0; i
< MAX_SAMPLERS
; i
++) {
251 /* Assume color sampler, no swizzling. (Works for BDW+) */
252 key
->swizzles
[i
] = SWIZZLE_XYZW
;
257 populate_vs_prog_key(const struct gen_device_info
*devinfo
,
258 struct brw_vs_prog_key
*key
)
260 memset(key
, 0, sizeof(*key
));
262 populate_sampler_prog_key(devinfo
, &key
->tex
);
264 /* XXX: Handle vertex input work-arounds */
266 /* XXX: Handle sampler_prog_key */
270 populate_gs_prog_key(const struct gen_device_info
*devinfo
,
271 struct brw_gs_prog_key
*key
)
273 memset(key
, 0, sizeof(*key
));
275 populate_sampler_prog_key(devinfo
, &key
->tex
);
279 populate_wm_prog_key(const struct anv_pipeline
*pipeline
,
280 const VkGraphicsPipelineCreateInfo
*info
,
281 struct brw_wm_prog_key
*key
)
283 const struct gen_device_info
*devinfo
= &pipeline
->device
->info
;
285 memset(key
, 0, sizeof(*key
));
287 populate_sampler_prog_key(devinfo
, &key
->tex
);
289 /* TODO: we could set this to 0 based on the information in nir_shader, but
290 * this function is called before spirv_to_nir. */
291 const struct brw_vue_map
*vue_map
=
292 &anv_pipeline_get_last_vue_prog_data(pipeline
)->vue_map
;
293 key
->input_slots_valid
= vue_map
->slots_valid
;
295 /* Vulkan doesn't specify a default */
296 key
->high_quality_derivatives
= false;
298 /* XXX Vulkan doesn't appear to specify */
299 key
->clamp_fragment_color
= false;
301 key
->nr_color_regions
= pipeline
->subpass
->color_count
;
303 key
->replicate_alpha
= key
->nr_color_regions
> 1 &&
304 info
->pMultisampleState
&&
305 info
->pMultisampleState
->alphaToCoverageEnable
;
307 if (info
->pMultisampleState
) {
308 /* We should probably pull this out of the shader, but it's fairly
309 * harmless to compute it and then let dead-code take care of it.
311 if (info
->pMultisampleState
->rasterizationSamples
> 1) {
312 key
->persample_interp
=
313 (info
->pMultisampleState
->minSampleShading
*
314 info
->pMultisampleState
->rasterizationSamples
) > 1;
315 key
->multisample_fbo
= true;
318 key
->frag_coord_adds_sample_pos
=
319 info
->pMultisampleState
->sampleShadingEnable
;
324 populate_cs_prog_key(const struct gen_device_info
*devinfo
,
325 struct brw_cs_prog_key
*key
)
327 memset(key
, 0, sizeof(*key
));
329 populate_sampler_prog_key(devinfo
, &key
->tex
);
333 anv_pipeline_hash_shader(struct anv_pipeline
*pipeline
,
334 struct anv_shader_module
*module
,
335 const char *entrypoint
,
336 gl_shader_stage stage
,
337 const VkSpecializationInfo
*spec_info
,
338 const void *key
, size_t key_size
,
339 unsigned char *sha1_out
)
341 struct mesa_sha1 ctx
;
343 _mesa_sha1_init(&ctx
);
344 if (stage
!= MESA_SHADER_COMPUTE
) {
345 _mesa_sha1_update(&ctx
, &pipeline
->subpass
->view_mask
,
346 sizeof(pipeline
->subpass
->view_mask
));
348 if (pipeline
->layout
) {
349 _mesa_sha1_update(&ctx
, pipeline
->layout
->sha1
,
350 sizeof(pipeline
->layout
->sha1
));
352 _mesa_sha1_update(&ctx
, module
->sha1
, sizeof(module
->sha1
));
353 _mesa_sha1_update(&ctx
, entrypoint
, strlen(entrypoint
));
354 _mesa_sha1_update(&ctx
, &stage
, sizeof(stage
));
356 _mesa_sha1_update(&ctx
, spec_info
->pMapEntries
,
357 spec_info
->mapEntryCount
* sizeof(*spec_info
->pMapEntries
));
358 _mesa_sha1_update(&ctx
, spec_info
->pData
, spec_info
->dataSize
);
360 _mesa_sha1_update(&ctx
, key
, key_size
);
361 _mesa_sha1_final(&ctx
, sha1_out
);
365 anv_pipeline_compile(struct anv_pipeline
*pipeline
,
366 struct anv_shader_module
*module
,
367 const char *entrypoint
,
368 gl_shader_stage stage
,
369 const VkSpecializationInfo
*spec_info
,
370 struct brw_stage_prog_data
*prog_data
,
371 struct anv_pipeline_bind_map
*map
)
373 nir_shader
*nir
= anv_shader_compile_to_nir(pipeline
,
374 module
, entrypoint
, stage
,
379 NIR_PASS_V(nir
, anv_nir_lower_ycbcr_textures
, pipeline
);
381 NIR_PASS_V(nir
, anv_nir_lower_push_constants
);
383 if (stage
!= MESA_SHADER_COMPUTE
)
384 NIR_PASS_V(nir
, anv_nir_lower_multiview
, pipeline
->subpass
->view_mask
);
386 if (stage
== MESA_SHADER_COMPUTE
) {
387 NIR_PASS_V(nir
, brw_nir_lower_cs_shared
);
388 prog_data
->total_shared
= nir
->num_shared
;
391 nir_shader_gather_info(nir
, nir_shader_get_entrypoint(nir
));
393 /* Figure out the number of parameters */
394 prog_data
->nr_params
= 0;
396 if (nir
->num_uniforms
> 0) {
397 /* If the shader uses any push constants at all, we'll just give
398 * them the maximum possible number
400 assert(nir
->num_uniforms
<= MAX_PUSH_CONSTANTS_SIZE
);
401 prog_data
->nr_params
+= MAX_PUSH_CONSTANTS_SIZE
/ sizeof(float);
404 if (nir
->info
.num_images
> 0) {
405 prog_data
->nr_params
+= nir
->info
.num_images
* BRW_IMAGE_PARAM_SIZE
;
406 pipeline
->needs_data_cache
= true;
409 if (stage
== MESA_SHADER_COMPUTE
)
410 ((struct brw_cs_prog_data
*)prog_data
)->thread_local_id_index
=
411 prog_data
->nr_params
++; /* The CS Thread ID uniform */
413 if (nir
->info
.num_ssbos
> 0)
414 pipeline
->needs_data_cache
= true;
416 if (prog_data
->nr_params
> 0) {
417 /* XXX: I think we're leaking this */
418 prog_data
->param
= (const union gl_constant_value
**)
419 malloc(prog_data
->nr_params
* sizeof(union gl_constant_value
*));
421 /* We now set the param values to be offsets into a
422 * anv_push_constant_data structure. Since the compiler doesn't
423 * actually dereference any of the gl_constant_value pointers in the
424 * params array, it doesn't really matter what we put here.
426 struct anv_push_constants
*null_data
= NULL
;
427 if (nir
->num_uniforms
> 0) {
428 /* Fill out the push constants section of the param array */
429 for (unsigned i
= 0; i
< MAX_PUSH_CONSTANTS_SIZE
/ sizeof(float); i
++)
430 prog_data
->param
[i
] = (const union gl_constant_value
*)
431 &null_data
->client_data
[i
* sizeof(float)];
435 /* Apply the actual pipeline layout to UBOs, SSBOs, and textures */
436 if (pipeline
->layout
)
437 anv_nir_apply_pipeline_layout(pipeline
, nir
, prog_data
, map
);
439 /* nir_lower_io will only handle the push constants; we need to set this
440 * to the full number of possible uniforms.
442 nir
->num_uniforms
= prog_data
->nr_params
* 4;
448 anv_fill_binding_table(struct brw_stage_prog_data
*prog_data
, unsigned bias
)
450 prog_data
->binding_table
.size_bytes
= 0;
451 prog_data
->binding_table
.texture_start
= bias
;
452 prog_data
->binding_table
.gather_texture_start
= bias
;
453 prog_data
->binding_table
.ubo_start
= bias
;
454 prog_data
->binding_table
.ssbo_start
= bias
;
455 prog_data
->binding_table
.image_start
= bias
;
458 static struct anv_shader_bin
*
459 anv_pipeline_upload_kernel(struct anv_pipeline
*pipeline
,
460 struct anv_pipeline_cache
*cache
,
461 const void *key_data
, uint32_t key_size
,
462 const void *kernel_data
, uint32_t kernel_size
,
463 const struct brw_stage_prog_data
*prog_data
,
464 uint32_t prog_data_size
,
465 const struct anv_pipeline_bind_map
*bind_map
)
468 return anv_pipeline_cache_upload_kernel(cache
, key_data
, key_size
,
469 kernel_data
, kernel_size
,
470 prog_data
, prog_data_size
,
473 return anv_shader_bin_create(pipeline
->device
, key_data
, key_size
,
474 kernel_data
, kernel_size
,
475 prog_data
, prog_data_size
,
476 prog_data
->param
, bind_map
);
482 anv_pipeline_add_compiled_stage(struct anv_pipeline
*pipeline
,
483 gl_shader_stage stage
,
484 struct anv_shader_bin
*shader
)
486 pipeline
->shaders
[stage
] = shader
;
487 pipeline
->active_stages
|= mesa_to_vk_shader_stage(stage
);
491 anv_pipeline_compile_vs(struct anv_pipeline
*pipeline
,
492 struct anv_pipeline_cache
*cache
,
493 const VkGraphicsPipelineCreateInfo
*info
,
494 struct anv_shader_module
*module
,
495 const char *entrypoint
,
496 const VkSpecializationInfo
*spec_info
)
498 const struct brw_compiler
*compiler
=
499 pipeline
->device
->instance
->physicalDevice
.compiler
;
500 struct anv_pipeline_bind_map map
;
501 struct brw_vs_prog_key key
;
502 struct anv_shader_bin
*bin
= NULL
;
503 unsigned char sha1
[20];
505 populate_vs_prog_key(&pipeline
->device
->info
, &key
);
508 anv_pipeline_hash_shader(pipeline
, module
, entrypoint
,
509 MESA_SHADER_VERTEX
, spec_info
,
510 &key
, sizeof(key
), sha1
);
511 bin
= anv_pipeline_cache_search(cache
, sha1
, 20);
515 struct brw_vs_prog_data prog_data
= {};
516 struct anv_pipeline_binding surface_to_descriptor
[256];
517 struct anv_pipeline_binding sampler_to_descriptor
[256];
519 map
= (struct anv_pipeline_bind_map
) {
520 .surface_to_descriptor
= surface_to_descriptor
,
521 .sampler_to_descriptor
= sampler_to_descriptor
524 nir_shader
*nir
= anv_pipeline_compile(pipeline
, module
, entrypoint
,
525 MESA_SHADER_VERTEX
, spec_info
,
526 &prog_data
.base
.base
, &map
);
528 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
530 anv_fill_binding_table(&prog_data
.base
.base
, 0);
532 void *mem_ctx
= ralloc_context(NULL
);
534 ralloc_steal(mem_ctx
, nir
);
536 brw_compute_vue_map(&pipeline
->device
->info
,
537 &prog_data
.base
.vue_map
,
538 nir
->info
.outputs_written
,
539 nir
->info
.separate_shader
);
542 const unsigned *shader_code
=
543 brw_compile_vs(compiler
, NULL
, mem_ctx
, &key
, &prog_data
, nir
,
544 NULL
, false, -1, &code_size
, NULL
);
545 if (shader_code
== NULL
) {
546 ralloc_free(mem_ctx
);
547 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
550 bin
= anv_pipeline_upload_kernel(pipeline
, cache
, sha1
, 20,
551 shader_code
, code_size
,
552 &prog_data
.base
.base
, sizeof(prog_data
),
555 ralloc_free(mem_ctx
);
556 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
559 ralloc_free(mem_ctx
);
562 anv_pipeline_add_compiled_stage(pipeline
, MESA_SHADER_VERTEX
, bin
);
568 merge_tess_info(struct shader_info
*tes_info
,
569 const struct shader_info
*tcs_info
)
571 /* The Vulkan 1.0.38 spec, section 21.1 Tessellator says:
573 * "PointMode. Controls generation of points rather than triangles
574 * or lines. This functionality defaults to disabled, and is
575 * enabled if either shader stage includes the execution mode.
577 * and about Triangles, Quads, IsoLines, VertexOrderCw, VertexOrderCcw,
578 * PointMode, SpacingEqual, SpacingFractionalEven, SpacingFractionalOdd,
579 * and OutputVertices, it says:
581 * "One mode must be set in at least one of the tessellation
584 * So, the fields can be set in either the TCS or TES, but they must
585 * agree if set in both. Our backend looks at TES, so bitwise-or in
586 * the values from the TCS.
588 assert(tcs_info
->tess
.tcs_vertices_out
== 0 ||
589 tes_info
->tess
.tcs_vertices_out
== 0 ||
590 tcs_info
->tess
.tcs_vertices_out
== tes_info
->tess
.tcs_vertices_out
);
591 tes_info
->tess
.tcs_vertices_out
|= tcs_info
->tess
.tcs_vertices_out
;
593 assert(tcs_info
->tess
.spacing
== TESS_SPACING_UNSPECIFIED
||
594 tes_info
->tess
.spacing
== TESS_SPACING_UNSPECIFIED
||
595 tcs_info
->tess
.spacing
== tes_info
->tess
.spacing
);
596 tes_info
->tess
.spacing
|= tcs_info
->tess
.spacing
;
598 assert(tcs_info
->tess
.primitive_mode
== 0 ||
599 tes_info
->tess
.primitive_mode
== 0 ||
600 tcs_info
->tess
.primitive_mode
== tes_info
->tess
.primitive_mode
);
601 tes_info
->tess
.primitive_mode
|= tcs_info
->tess
.primitive_mode
;
602 tes_info
->tess
.ccw
|= tcs_info
->tess
.ccw
;
603 tes_info
->tess
.point_mode
|= tcs_info
->tess
.point_mode
;
607 anv_pipeline_compile_tcs_tes(struct anv_pipeline
*pipeline
,
608 struct anv_pipeline_cache
*cache
,
609 const VkGraphicsPipelineCreateInfo
*info
,
610 struct anv_shader_module
*tcs_module
,
611 const char *tcs_entrypoint
,
612 const VkSpecializationInfo
*tcs_spec_info
,
613 struct anv_shader_module
*tes_module
,
614 const char *tes_entrypoint
,
615 const VkSpecializationInfo
*tes_spec_info
)
617 const struct gen_device_info
*devinfo
= &pipeline
->device
->info
;
618 const struct brw_compiler
*compiler
=
619 pipeline
->device
->instance
->physicalDevice
.compiler
;
620 struct anv_pipeline_bind_map tcs_map
;
621 struct anv_pipeline_bind_map tes_map
;
622 struct brw_tcs_prog_key tcs_key
= {};
623 struct brw_tes_prog_key tes_key
= {};
624 struct anv_shader_bin
*tcs_bin
= NULL
;
625 struct anv_shader_bin
*tes_bin
= NULL
;
626 unsigned char tcs_sha1
[40];
627 unsigned char tes_sha1
[40];
629 populate_sampler_prog_key(&pipeline
->device
->info
, &tcs_key
.tex
);
630 populate_sampler_prog_key(&pipeline
->device
->info
, &tes_key
.tex
);
631 tcs_key
.input_vertices
= info
->pTessellationState
->patchControlPoints
;
634 anv_pipeline_hash_shader(pipeline
, tcs_module
, tcs_entrypoint
,
635 MESA_SHADER_TESS_CTRL
, tcs_spec_info
,
636 &tcs_key
, sizeof(tcs_key
), tcs_sha1
);
637 anv_pipeline_hash_shader(pipeline
, tes_module
, tes_entrypoint
,
638 MESA_SHADER_TESS_EVAL
, tes_spec_info
,
639 &tes_key
, sizeof(tes_key
), tes_sha1
);
640 memcpy(&tcs_sha1
[20], tes_sha1
, 20);
641 memcpy(&tes_sha1
[20], tcs_sha1
, 20);
642 tcs_bin
= anv_pipeline_cache_search(cache
, tcs_sha1
, sizeof(tcs_sha1
));
643 tes_bin
= anv_pipeline_cache_search(cache
, tes_sha1
, sizeof(tes_sha1
));
646 if (tcs_bin
== NULL
|| tes_bin
== NULL
) {
647 struct brw_tcs_prog_data tcs_prog_data
= {};
648 struct brw_tes_prog_data tes_prog_data
= {};
649 struct anv_pipeline_binding tcs_surface_to_descriptor
[256];
650 struct anv_pipeline_binding tcs_sampler_to_descriptor
[256];
651 struct anv_pipeline_binding tes_surface_to_descriptor
[256];
652 struct anv_pipeline_binding tes_sampler_to_descriptor
[256];
654 tcs_map
= (struct anv_pipeline_bind_map
) {
655 .surface_to_descriptor
= tcs_surface_to_descriptor
,
656 .sampler_to_descriptor
= tcs_sampler_to_descriptor
658 tes_map
= (struct anv_pipeline_bind_map
) {
659 .surface_to_descriptor
= tes_surface_to_descriptor
,
660 .sampler_to_descriptor
= tes_sampler_to_descriptor
663 nir_shader
*tcs_nir
=
664 anv_pipeline_compile(pipeline
, tcs_module
, tcs_entrypoint
,
665 MESA_SHADER_TESS_CTRL
, tcs_spec_info
,
666 &tcs_prog_data
.base
.base
, &tcs_map
);
667 nir_shader
*tes_nir
=
668 anv_pipeline_compile(pipeline
, tes_module
, tes_entrypoint
,
669 MESA_SHADER_TESS_EVAL
, tes_spec_info
,
670 &tes_prog_data
.base
.base
, &tes_map
);
671 if (tcs_nir
== NULL
|| tes_nir
== NULL
)
672 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
674 nir_lower_tes_patch_vertices(tes_nir
,
675 tcs_nir
->info
.tess
.tcs_vertices_out
);
677 /* Copy TCS info into the TES info */
678 merge_tess_info(&tes_nir
->info
, &tcs_nir
->info
);
680 anv_fill_binding_table(&tcs_prog_data
.base
.base
, 0);
681 anv_fill_binding_table(&tes_prog_data
.base
.base
, 0);
683 void *mem_ctx
= ralloc_context(NULL
);
685 ralloc_steal(mem_ctx
, tcs_nir
);
686 ralloc_steal(mem_ctx
, tes_nir
);
688 /* Whacking the key after cache lookup is a bit sketchy, but all of
689 * this comes from the SPIR-V, which is part of the hash used for the
690 * pipeline cache. So it should be safe.
692 tcs_key
.tes_primitive_mode
= tes_nir
->info
.tess
.primitive_mode
;
693 tcs_key
.outputs_written
= tcs_nir
->info
.outputs_written
;
694 tcs_key
.patch_outputs_written
= tcs_nir
->info
.patch_outputs_written
;
695 tcs_key
.quads_workaround
=
697 tes_nir
->info
.tess
.primitive_mode
== 7 /* GL_QUADS */ &&
698 tes_nir
->info
.tess
.spacing
== TESS_SPACING_EQUAL
;
700 tes_key
.inputs_read
= tcs_key
.outputs_written
;
701 tes_key
.patch_inputs_read
= tcs_key
.patch_outputs_written
;
704 const int shader_time_index
= -1;
705 const unsigned *shader_code
;
708 brw_compile_tcs(compiler
, NULL
, mem_ctx
, &tcs_key
, &tcs_prog_data
,
709 tcs_nir
, shader_time_index
, &code_size
, NULL
);
710 if (shader_code
== NULL
) {
711 ralloc_free(mem_ctx
);
712 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
715 tcs_bin
= anv_pipeline_upload_kernel(pipeline
, cache
,
716 tcs_sha1
, sizeof(tcs_sha1
),
717 shader_code
, code_size
,
718 &tcs_prog_data
.base
.base
,
719 sizeof(tcs_prog_data
),
722 ralloc_free(mem_ctx
);
723 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
727 brw_compile_tes(compiler
, NULL
, mem_ctx
, &tes_key
,
728 &tcs_prog_data
.base
.vue_map
, &tes_prog_data
, tes_nir
,
729 NULL
, shader_time_index
, &code_size
, NULL
);
730 if (shader_code
== NULL
) {
731 ralloc_free(mem_ctx
);
732 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
735 tes_bin
= anv_pipeline_upload_kernel(pipeline
, cache
,
736 tes_sha1
, sizeof(tes_sha1
),
737 shader_code
, code_size
,
738 &tes_prog_data
.base
.base
,
739 sizeof(tes_prog_data
),
742 ralloc_free(mem_ctx
);
743 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
746 ralloc_free(mem_ctx
);
749 anv_pipeline_add_compiled_stage(pipeline
, MESA_SHADER_TESS_CTRL
, tcs_bin
);
750 anv_pipeline_add_compiled_stage(pipeline
, MESA_SHADER_TESS_EVAL
, tes_bin
);
756 anv_pipeline_compile_gs(struct anv_pipeline
*pipeline
,
757 struct anv_pipeline_cache
*cache
,
758 const VkGraphicsPipelineCreateInfo
*info
,
759 struct anv_shader_module
*module
,
760 const char *entrypoint
,
761 const VkSpecializationInfo
*spec_info
)
763 const struct brw_compiler
*compiler
=
764 pipeline
->device
->instance
->physicalDevice
.compiler
;
765 struct anv_pipeline_bind_map map
;
766 struct brw_gs_prog_key key
;
767 struct anv_shader_bin
*bin
= NULL
;
768 unsigned char sha1
[20];
770 populate_gs_prog_key(&pipeline
->device
->info
, &key
);
773 anv_pipeline_hash_shader(pipeline
, module
, entrypoint
,
774 MESA_SHADER_GEOMETRY
, spec_info
,
775 &key
, sizeof(key
), sha1
);
776 bin
= anv_pipeline_cache_search(cache
, sha1
, 20);
780 struct brw_gs_prog_data prog_data
= {};
781 struct anv_pipeline_binding surface_to_descriptor
[256];
782 struct anv_pipeline_binding sampler_to_descriptor
[256];
784 map
= (struct anv_pipeline_bind_map
) {
785 .surface_to_descriptor
= surface_to_descriptor
,
786 .sampler_to_descriptor
= sampler_to_descriptor
789 nir_shader
*nir
= anv_pipeline_compile(pipeline
, module
, entrypoint
,
790 MESA_SHADER_GEOMETRY
, spec_info
,
791 &prog_data
.base
.base
, &map
);
793 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
795 anv_fill_binding_table(&prog_data
.base
.base
, 0);
797 void *mem_ctx
= ralloc_context(NULL
);
799 ralloc_steal(mem_ctx
, nir
);
801 brw_compute_vue_map(&pipeline
->device
->info
,
802 &prog_data
.base
.vue_map
,
803 nir
->info
.outputs_written
,
804 nir
->info
.separate_shader
);
807 const unsigned *shader_code
=
808 brw_compile_gs(compiler
, NULL
, mem_ctx
, &key
, &prog_data
, nir
,
809 NULL
, -1, &code_size
, NULL
);
810 if (shader_code
== NULL
) {
811 ralloc_free(mem_ctx
);
812 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
816 bin
= anv_pipeline_upload_kernel(pipeline
, cache
, sha1
, 20,
817 shader_code
, code_size
,
818 &prog_data
.base
.base
, sizeof(prog_data
),
821 ralloc_free(mem_ctx
);
822 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
825 ralloc_free(mem_ctx
);
828 anv_pipeline_add_compiled_stage(pipeline
, MESA_SHADER_GEOMETRY
, bin
);
834 anv_pipeline_compile_fs(struct anv_pipeline
*pipeline
,
835 struct anv_pipeline_cache
*cache
,
836 const VkGraphicsPipelineCreateInfo
*info
,
837 struct anv_shader_module
*module
,
838 const char *entrypoint
,
839 const VkSpecializationInfo
*spec_info
)
841 const struct brw_compiler
*compiler
=
842 pipeline
->device
->instance
->physicalDevice
.compiler
;
843 struct anv_pipeline_bind_map map
;
844 struct brw_wm_prog_key key
;
845 struct anv_shader_bin
*bin
= NULL
;
846 unsigned char sha1
[20];
848 populate_wm_prog_key(pipeline
, info
, &key
);
851 anv_pipeline_hash_shader(pipeline
, module
, entrypoint
,
852 MESA_SHADER_FRAGMENT
, spec_info
,
853 &key
, sizeof(key
), sha1
);
854 bin
= anv_pipeline_cache_search(cache
, sha1
, 20);
858 struct brw_wm_prog_data prog_data
= {};
859 struct anv_pipeline_binding surface_to_descriptor
[256];
860 struct anv_pipeline_binding sampler_to_descriptor
[256];
862 map
= (struct anv_pipeline_bind_map
) {
863 .surface_to_descriptor
= surface_to_descriptor
+ 8,
864 .sampler_to_descriptor
= sampler_to_descriptor
867 nir_shader
*nir
= anv_pipeline_compile(pipeline
, module
, entrypoint
,
868 MESA_SHADER_FRAGMENT
, spec_info
,
869 &prog_data
.base
, &map
);
871 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
873 unsigned num_rts
= 0;
874 struct anv_pipeline_binding rt_bindings
[8];
875 nir_function_impl
*impl
= nir_shader_get_entrypoint(nir
);
876 nir_foreach_variable_safe(var
, &nir
->outputs
) {
877 if (var
->data
.location
< FRAG_RESULT_DATA0
)
880 unsigned rt
= var
->data
.location
- FRAG_RESULT_DATA0
;
881 if (rt
>= key
.nr_color_regions
) {
882 /* Out-of-bounds, throw it away */
883 var
->data
.mode
= nir_var_local
;
884 exec_node_remove(&var
->node
);
885 exec_list_push_tail(&impl
->locals
, &var
->node
);
889 /* Give it a new, compacted, location */
890 var
->data
.location
= FRAG_RESULT_DATA0
+ num_rts
;
893 glsl_type_is_array(var
->type
) ? glsl_get_length(var
->type
) : 1;
894 assert(num_rts
+ array_len
<= 8);
896 for (unsigned i
= 0; i
< array_len
; i
++) {
897 rt_bindings
[num_rts
+ i
] = (struct anv_pipeline_binding
) {
898 .set
= ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS
,
904 num_rts
+= array_len
;
908 /* If we have no render targets, we need a null render target */
909 rt_bindings
[0] = (struct anv_pipeline_binding
) {
910 .set
= ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS
,
917 assert(num_rts
<= 8);
918 map
.surface_to_descriptor
-= num_rts
;
919 map
.surface_count
+= num_rts
;
920 assert(map
.surface_count
<= 256);
921 memcpy(map
.surface_to_descriptor
, rt_bindings
,
922 num_rts
* sizeof(*rt_bindings
));
924 anv_fill_binding_table(&prog_data
.base
, num_rts
);
926 void *mem_ctx
= ralloc_context(NULL
);
928 ralloc_steal(mem_ctx
, nir
);
931 const unsigned *shader_code
=
932 brw_compile_fs(compiler
, NULL
, mem_ctx
, &key
, &prog_data
, nir
,
933 NULL
, -1, -1, true, false, NULL
, &code_size
, NULL
);
934 if (shader_code
== NULL
) {
935 ralloc_free(mem_ctx
);
936 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
939 bin
= anv_pipeline_upload_kernel(pipeline
, cache
, sha1
, 20,
940 shader_code
, code_size
,
941 &prog_data
.base
, sizeof(prog_data
),
944 ralloc_free(mem_ctx
);
945 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
948 ralloc_free(mem_ctx
);
951 anv_pipeline_add_compiled_stage(pipeline
, MESA_SHADER_FRAGMENT
, bin
);
957 anv_pipeline_compile_cs(struct anv_pipeline
*pipeline
,
958 struct anv_pipeline_cache
*cache
,
959 const VkComputePipelineCreateInfo
*info
,
960 struct anv_shader_module
*module
,
961 const char *entrypoint
,
962 const VkSpecializationInfo
*spec_info
)
964 const struct brw_compiler
*compiler
=
965 pipeline
->device
->instance
->physicalDevice
.compiler
;
966 struct anv_pipeline_bind_map map
;
967 struct brw_cs_prog_key key
;
968 struct anv_shader_bin
*bin
= NULL
;
969 unsigned char sha1
[20];
971 populate_cs_prog_key(&pipeline
->device
->info
, &key
);
974 anv_pipeline_hash_shader(pipeline
, module
, entrypoint
,
975 MESA_SHADER_COMPUTE
, spec_info
,
976 &key
, sizeof(key
), sha1
);
977 bin
= anv_pipeline_cache_search(cache
, sha1
, 20);
981 struct brw_cs_prog_data prog_data
= {};
982 struct anv_pipeline_binding surface_to_descriptor
[256];
983 struct anv_pipeline_binding sampler_to_descriptor
[256];
985 map
= (struct anv_pipeline_bind_map
) {
986 .surface_to_descriptor
= surface_to_descriptor
,
987 .sampler_to_descriptor
= sampler_to_descriptor
990 nir_shader
*nir
= anv_pipeline_compile(pipeline
, module
, entrypoint
,
991 MESA_SHADER_COMPUTE
, spec_info
,
992 &prog_data
.base
, &map
);
994 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
996 anv_fill_binding_table(&prog_data
.base
, 1);
998 void *mem_ctx
= ralloc_context(NULL
);
1000 ralloc_steal(mem_ctx
, nir
);
1003 const unsigned *shader_code
=
1004 brw_compile_cs(compiler
, NULL
, mem_ctx
, &key
, &prog_data
, nir
,
1005 -1, &code_size
, NULL
);
1006 if (shader_code
== NULL
) {
1007 ralloc_free(mem_ctx
);
1008 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1011 bin
= anv_pipeline_upload_kernel(pipeline
, cache
, sha1
, 20,
1012 shader_code
, code_size
,
1013 &prog_data
.base
, sizeof(prog_data
),
1016 ralloc_free(mem_ctx
);
1017 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1020 ralloc_free(mem_ctx
);
1023 anv_pipeline_add_compiled_stage(pipeline
, MESA_SHADER_COMPUTE
, bin
);
1029 * Copy pipeline state not marked as dynamic.
1030 * Dynamic state is pipeline state which hasn't been provided at pipeline
1031 * creation time, but is dynamically provided afterwards using various
1032 * vkCmdSet* functions.
1034 * The set of state considered "non_dynamic" is determined by the pieces of
1035 * state that have their corresponding VkDynamicState enums omitted from
1036 * VkPipelineDynamicStateCreateInfo::pDynamicStates.
1038 * @param[out] pipeline Destination non_dynamic state.
1039 * @param[in] pCreateInfo Source of non_dynamic state to be copied.
1042 copy_non_dynamic_state(struct anv_pipeline
*pipeline
,
1043 const VkGraphicsPipelineCreateInfo
*pCreateInfo
)
1045 anv_cmd_dirty_mask_t states
= ANV_CMD_DIRTY_DYNAMIC_ALL
;
1046 struct anv_subpass
*subpass
= pipeline
->subpass
;
1048 pipeline
->dynamic_state
= default_dynamic_state
;
1050 if (pCreateInfo
->pDynamicState
) {
1051 /* Remove all of the states that are marked as dynamic */
1052 uint32_t count
= pCreateInfo
->pDynamicState
->dynamicStateCount
;
1053 for (uint32_t s
= 0; s
< count
; s
++)
1054 states
&= ~(1 << pCreateInfo
->pDynamicState
->pDynamicStates
[s
]);
1057 struct anv_dynamic_state
*dynamic
= &pipeline
->dynamic_state
;
1059 /* Section 9.2 of the Vulkan 1.0.15 spec says:
1061 * pViewportState is [...] NULL if the pipeline
1062 * has rasterization disabled.
1064 if (!pCreateInfo
->pRasterizationState
->rasterizerDiscardEnable
) {
1065 assert(pCreateInfo
->pViewportState
);
1067 dynamic
->viewport
.count
= pCreateInfo
->pViewportState
->viewportCount
;
1068 if (states
& (1 << VK_DYNAMIC_STATE_VIEWPORT
)) {
1069 typed_memcpy(dynamic
->viewport
.viewports
,
1070 pCreateInfo
->pViewportState
->pViewports
,
1071 pCreateInfo
->pViewportState
->viewportCount
);
1074 dynamic
->scissor
.count
= pCreateInfo
->pViewportState
->scissorCount
;
1075 if (states
& (1 << VK_DYNAMIC_STATE_SCISSOR
)) {
1076 typed_memcpy(dynamic
->scissor
.scissors
,
1077 pCreateInfo
->pViewportState
->pScissors
,
1078 pCreateInfo
->pViewportState
->scissorCount
);
1082 if (states
& (1 << VK_DYNAMIC_STATE_LINE_WIDTH
)) {
1083 assert(pCreateInfo
->pRasterizationState
);
1084 dynamic
->line_width
= pCreateInfo
->pRasterizationState
->lineWidth
;
1087 if (states
& (1 << VK_DYNAMIC_STATE_DEPTH_BIAS
)) {
1088 assert(pCreateInfo
->pRasterizationState
);
1089 dynamic
->depth_bias
.bias
=
1090 pCreateInfo
->pRasterizationState
->depthBiasConstantFactor
;
1091 dynamic
->depth_bias
.clamp
=
1092 pCreateInfo
->pRasterizationState
->depthBiasClamp
;
1093 dynamic
->depth_bias
.slope
=
1094 pCreateInfo
->pRasterizationState
->depthBiasSlopeFactor
;
1097 /* Section 9.2 of the Vulkan 1.0.15 spec says:
1099 * pColorBlendState is [...] NULL if the pipeline has rasterization
1100 * disabled or if the subpass of the render pass the pipeline is
1101 * created against does not use any color attachments.
1103 bool uses_color_att
= false;
1104 for (unsigned i
= 0; i
< subpass
->color_count
; ++i
) {
1105 if (subpass
->color_attachments
[i
].attachment
!= VK_ATTACHMENT_UNUSED
) {
1106 uses_color_att
= true;
1111 if (uses_color_att
&&
1112 !pCreateInfo
->pRasterizationState
->rasterizerDiscardEnable
) {
1113 assert(pCreateInfo
->pColorBlendState
);
1115 if (states
& (1 << VK_DYNAMIC_STATE_BLEND_CONSTANTS
))
1116 typed_memcpy(dynamic
->blend_constants
,
1117 pCreateInfo
->pColorBlendState
->blendConstants
, 4);
1120 /* If there is no depthstencil attachment, then don't read
1121 * pDepthStencilState. The Vulkan spec states that pDepthStencilState may
1122 * be NULL in this case. Even if pDepthStencilState is non-NULL, there is
1123 * no need to override the depthstencil defaults in
1124 * anv_pipeline::dynamic_state when there is no depthstencil attachment.
1126 * Section 9.2 of the Vulkan 1.0.15 spec says:
1128 * pDepthStencilState is [...] NULL if the pipeline has rasterization
1129 * disabled or if the subpass of the render pass the pipeline is created
1130 * against does not use a depth/stencil attachment.
1132 if (!pCreateInfo
->pRasterizationState
->rasterizerDiscardEnable
&&
1133 subpass
->depth_stencil_attachment
.attachment
!= VK_ATTACHMENT_UNUSED
) {
1134 assert(pCreateInfo
->pDepthStencilState
);
1136 if (states
& (1 << VK_DYNAMIC_STATE_DEPTH_BOUNDS
)) {
1137 dynamic
->depth_bounds
.min
=
1138 pCreateInfo
->pDepthStencilState
->minDepthBounds
;
1139 dynamic
->depth_bounds
.max
=
1140 pCreateInfo
->pDepthStencilState
->maxDepthBounds
;
1143 if (states
& (1 << VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK
)) {
1144 dynamic
->stencil_compare_mask
.front
=
1145 pCreateInfo
->pDepthStencilState
->front
.compareMask
;
1146 dynamic
->stencil_compare_mask
.back
=
1147 pCreateInfo
->pDepthStencilState
->back
.compareMask
;
1150 if (states
& (1 << VK_DYNAMIC_STATE_STENCIL_WRITE_MASK
)) {
1151 dynamic
->stencil_write_mask
.front
=
1152 pCreateInfo
->pDepthStencilState
->front
.writeMask
;
1153 dynamic
->stencil_write_mask
.back
=
1154 pCreateInfo
->pDepthStencilState
->back
.writeMask
;
1157 if (states
& (1 << VK_DYNAMIC_STATE_STENCIL_REFERENCE
)) {
1158 dynamic
->stencil_reference
.front
=
1159 pCreateInfo
->pDepthStencilState
->front
.reference
;
1160 dynamic
->stencil_reference
.back
=
1161 pCreateInfo
->pDepthStencilState
->back
.reference
;
1165 pipeline
->dynamic_state_mask
= states
;
1169 anv_pipeline_validate_create_info(const VkGraphicsPipelineCreateInfo
*info
)
1172 struct anv_render_pass
*renderpass
= NULL
;
1173 struct anv_subpass
*subpass
= NULL
;
1175 /* Assert that all required members of VkGraphicsPipelineCreateInfo are
1176 * present. See the Vulkan 1.0.28 spec, Section 9.2 Graphics Pipelines.
1178 assert(info
->sType
== VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO
);
1180 renderpass
= anv_render_pass_from_handle(info
->renderPass
);
1183 assert(info
->subpass
< renderpass
->subpass_count
);
1184 subpass
= &renderpass
->subpasses
[info
->subpass
];
1186 assert(info
->stageCount
>= 1);
1187 assert(info
->pVertexInputState
);
1188 assert(info
->pInputAssemblyState
);
1189 assert(info
->pRasterizationState
);
1190 if (!info
->pRasterizationState
->rasterizerDiscardEnable
) {
1191 assert(info
->pViewportState
);
1192 assert(info
->pMultisampleState
);
1194 if (subpass
&& subpass
->depth_stencil_attachment
.attachment
!= VK_ATTACHMENT_UNUSED
)
1195 assert(info
->pDepthStencilState
);
1197 if (subpass
&& subpass
->color_count
> 0)
1198 assert(info
->pColorBlendState
);
1201 for (uint32_t i
= 0; i
< info
->stageCount
; ++i
) {
1202 switch (info
->pStages
[i
].stage
) {
1203 case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT
:
1204 case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT
:
1205 assert(info
->pTessellationState
);
1215 * Calculate the desired L3 partitioning based on the current state of the
1216 * pipeline. For now this simply returns the conservative defaults calculated
1217 * by get_default_l3_weights(), but we could probably do better by gathering
1218 * more statistics from the pipeline state (e.g. guess of expected URB usage
1219 * and bound surfaces), or by using feed-back from performance counters.
1222 anv_pipeline_setup_l3_config(struct anv_pipeline
*pipeline
, bool needs_slm
)
1224 const struct gen_device_info
*devinfo
= &pipeline
->device
->info
;
1226 const struct gen_l3_weights w
=
1227 gen_get_default_l3_weights(devinfo
, pipeline
->needs_data_cache
, needs_slm
);
1229 pipeline
->urb
.l3_config
= gen_get_l3_config(devinfo
, w
);
1230 pipeline
->urb
.total_size
=
1231 gen_get_l3_config_urb_size(devinfo
, pipeline
->urb
.l3_config
);
1235 anv_pipeline_init(struct anv_pipeline
*pipeline
,
1236 struct anv_device
*device
,
1237 struct anv_pipeline_cache
*cache
,
1238 const VkGraphicsPipelineCreateInfo
*pCreateInfo
,
1239 const VkAllocationCallbacks
*alloc
)
1243 anv_pipeline_validate_create_info(pCreateInfo
);
1246 alloc
= &device
->alloc
;
1248 pipeline
->device
= device
;
1250 ANV_FROM_HANDLE(anv_render_pass
, render_pass
, pCreateInfo
->renderPass
);
1251 assert(pCreateInfo
->subpass
< render_pass
->subpass_count
);
1252 pipeline
->subpass
= &render_pass
->subpasses
[pCreateInfo
->subpass
];
1254 pipeline
->layout
= anv_pipeline_layout_from_handle(pCreateInfo
->layout
);
1256 result
= anv_reloc_list_init(&pipeline
->batch_relocs
, alloc
);
1257 if (result
!= VK_SUCCESS
)
1260 pipeline
->batch
.alloc
= alloc
;
1261 pipeline
->batch
.next
= pipeline
->batch
.start
= pipeline
->batch_data
;
1262 pipeline
->batch
.end
= pipeline
->batch
.start
+ sizeof(pipeline
->batch_data
);
1263 pipeline
->batch
.relocs
= &pipeline
->batch_relocs
;
1264 pipeline
->batch
.status
= VK_SUCCESS
;
1266 copy_non_dynamic_state(pipeline
, pCreateInfo
);
1267 pipeline
->depth_clamp_enable
= pCreateInfo
->pRasterizationState
&&
1268 pCreateInfo
->pRasterizationState
->depthClampEnable
;
1270 pipeline
->sample_shading_enable
= pCreateInfo
->pMultisampleState
&&
1271 pCreateInfo
->pMultisampleState
->sampleShadingEnable
;
1273 pipeline
->needs_data_cache
= false;
1275 /* When we free the pipeline, we detect stages based on the NULL status
1276 * of various prog_data pointers. Make them NULL by default.
1278 memset(pipeline
->shaders
, 0, sizeof(pipeline
->shaders
));
1280 pipeline
->active_stages
= 0;
1282 const VkPipelineShaderStageCreateInfo
*pStages
[MESA_SHADER_STAGES
] = {};
1283 struct anv_shader_module
*modules
[MESA_SHADER_STAGES
] = {};
1284 for (uint32_t i
= 0; i
< pCreateInfo
->stageCount
; i
++) {
1285 gl_shader_stage stage
= ffs(pCreateInfo
->pStages
[i
].stage
) - 1;
1286 pStages
[stage
] = &pCreateInfo
->pStages
[i
];
1287 modules
[stage
] = anv_shader_module_from_handle(pStages
[stage
]->module
);
1290 if (modules
[MESA_SHADER_VERTEX
]) {
1291 result
= anv_pipeline_compile_vs(pipeline
, cache
, pCreateInfo
,
1292 modules
[MESA_SHADER_VERTEX
],
1293 pStages
[MESA_SHADER_VERTEX
]->pName
,
1294 pStages
[MESA_SHADER_VERTEX
]->pSpecializationInfo
);
1295 if (result
!= VK_SUCCESS
)
1299 if (modules
[MESA_SHADER_TESS_EVAL
]) {
1300 anv_pipeline_compile_tcs_tes(pipeline
, cache
, pCreateInfo
,
1301 modules
[MESA_SHADER_TESS_CTRL
],
1302 pStages
[MESA_SHADER_TESS_CTRL
]->pName
,
1303 pStages
[MESA_SHADER_TESS_CTRL
]->pSpecializationInfo
,
1304 modules
[MESA_SHADER_TESS_EVAL
],
1305 pStages
[MESA_SHADER_TESS_EVAL
]->pName
,
1306 pStages
[MESA_SHADER_TESS_EVAL
]->pSpecializationInfo
);
1309 if (modules
[MESA_SHADER_GEOMETRY
]) {
1310 result
= anv_pipeline_compile_gs(pipeline
, cache
, pCreateInfo
,
1311 modules
[MESA_SHADER_GEOMETRY
],
1312 pStages
[MESA_SHADER_GEOMETRY
]->pName
,
1313 pStages
[MESA_SHADER_GEOMETRY
]->pSpecializationInfo
);
1314 if (result
!= VK_SUCCESS
)
1318 if (modules
[MESA_SHADER_FRAGMENT
]) {
1319 result
= anv_pipeline_compile_fs(pipeline
, cache
, pCreateInfo
,
1320 modules
[MESA_SHADER_FRAGMENT
],
1321 pStages
[MESA_SHADER_FRAGMENT
]->pName
,
1322 pStages
[MESA_SHADER_FRAGMENT
]->pSpecializationInfo
);
1323 if (result
!= VK_SUCCESS
)
1327 assert(pipeline
->active_stages
& VK_SHADER_STAGE_VERTEX_BIT
);
1329 anv_pipeline_setup_l3_config(pipeline
, false);
1331 const VkPipelineVertexInputStateCreateInfo
*vi_info
=
1332 pCreateInfo
->pVertexInputState
;
1334 const uint64_t inputs_read
= get_vs_prog_data(pipeline
)->inputs_read
;
1336 pipeline
->vb_used
= 0;
1337 for (uint32_t i
= 0; i
< vi_info
->vertexAttributeDescriptionCount
; i
++) {
1338 const VkVertexInputAttributeDescription
*desc
=
1339 &vi_info
->pVertexAttributeDescriptions
[i
];
1341 if (inputs_read
& (1ull << (VERT_ATTRIB_GENERIC0
+ desc
->location
)))
1342 pipeline
->vb_used
|= 1 << desc
->binding
;
1345 for (uint32_t i
= 0; i
< vi_info
->vertexBindingDescriptionCount
; i
++) {
1346 const VkVertexInputBindingDescription
*desc
=
1347 &vi_info
->pVertexBindingDescriptions
[i
];
1349 pipeline
->binding_stride
[desc
->binding
] = desc
->stride
;
1351 /* Step rate is programmed per vertex element (attribute), not
1352 * binding. Set up a map of which bindings step per instance, for
1353 * reference by vertex element setup. */
1354 switch (desc
->inputRate
) {
1356 case VK_VERTEX_INPUT_RATE_VERTEX
:
1357 pipeline
->instancing_enable
[desc
->binding
] = false;
1359 case VK_VERTEX_INPUT_RATE_INSTANCE
:
1360 pipeline
->instancing_enable
[desc
->binding
] = true;
1365 const VkPipelineInputAssemblyStateCreateInfo
*ia_info
=
1366 pCreateInfo
->pInputAssemblyState
;
1367 const VkPipelineTessellationStateCreateInfo
*tess_info
=
1368 pCreateInfo
->pTessellationState
;
1369 pipeline
->primitive_restart
= ia_info
->primitiveRestartEnable
;
1371 if (anv_pipeline_has_stage(pipeline
, MESA_SHADER_TESS_EVAL
))
1372 pipeline
->topology
= _3DPRIM_PATCHLIST(tess_info
->patchControlPoints
);
1374 pipeline
->topology
= vk_to_gen_primitive_type
[ia_info
->topology
];
1379 for (unsigned s
= 0; s
< MESA_SHADER_STAGES
; s
++) {
1380 if (pipeline
->shaders
[s
])
1381 anv_shader_bin_unref(device
, pipeline
->shaders
[s
]);
1384 anv_reloc_list_finish(&pipeline
->batch_relocs
, alloc
);