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"
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_device
*device
,
91 struct anv_shader_module
*module
,
92 const char *entrypoint_name
,
93 gl_shader_stage stage
,
94 const VkSpecializationInfo
*spec_info
)
96 if (strcmp(entrypoint_name
, "main") != 0) {
97 anv_finishme("Multiple shaders per module not really supported");
100 const struct brw_compiler
*compiler
=
101 device
->instance
->physicalDevice
.compiler
;
102 const nir_shader_compiler_options
*nir_options
=
103 compiler
->glsl_compiler_options
[stage
].NirOptions
;
105 uint32_t *spirv
= (uint32_t *) module
->data
;
106 assert(spirv
[0] == SPIR_V_MAGIC_NUMBER
);
107 assert(module
->size
% 4 == 0);
109 uint32_t num_spec_entries
= 0;
110 struct nir_spirv_specialization
*spec_entries
= NULL
;
111 if (spec_info
&& spec_info
->mapEntryCount
> 0) {
112 num_spec_entries
= spec_info
->mapEntryCount
;
113 spec_entries
= malloc(num_spec_entries
* sizeof(*spec_entries
));
114 for (uint32_t i
= 0; i
< num_spec_entries
; i
++) {
115 VkSpecializationMapEntry entry
= spec_info
->pMapEntries
[i
];
116 const void *data
= spec_info
->pData
+ entry
.offset
;
117 assert(data
+ entry
.size
<= spec_info
->pData
+ spec_info
->dataSize
);
119 spec_entries
[i
].id
= spec_info
->pMapEntries
[i
].constantID
;
120 if (spec_info
->dataSize
== 8)
121 spec_entries
[i
].data64
= *(const uint64_t *)data
;
123 spec_entries
[i
].data32
= *(const uint32_t *)data
;
127 const struct nir_spirv_supported_extensions supported_ext
= {
128 .float64
= device
->instance
->physicalDevice
.info
.gen
>= 8,
129 .tessellation
= true,
132 nir_function
*entry_point
=
133 spirv_to_nir(spirv
, module
->size
/ 4,
134 spec_entries
, num_spec_entries
,
135 stage
, entrypoint_name
, &supported_ext
, nir_options
);
136 nir_shader
*nir
= entry_point
->shader
;
137 assert(nir
->stage
== stage
);
138 nir_validate_shader(nir
);
142 if (stage
== MESA_SHADER_FRAGMENT
)
143 NIR_PASS_V(nir
, nir_lower_wpos_center
);
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 /* Now that we've deleted all but the main function, we can go ahead and
165 * lower the rest of the constant initializers.
167 NIR_PASS_V(nir
, nir_lower_constant_initializers
, ~0);
168 NIR_PASS_V(nir
, nir_propagate_invariant
);
169 NIR_PASS_V(nir
, nir_lower_io_to_temporaries
,
170 entry_point
->impl
, true, false);
171 NIR_PASS_V(nir
, nir_lower_system_values
);
173 /* Vulkan uses the separate-shader linking model */
174 nir
->info
->separate_shader
= true;
176 nir
= brw_preprocess_nir(compiler
, nir
);
178 NIR_PASS_V(nir
, nir_lower_clip_cull_distance_arrays
);
180 if (stage
== MESA_SHADER_FRAGMENT
)
181 NIR_PASS_V(nir
, anv_nir_lower_input_attachments
);
183 nir_shader_gather_info(nir
, entry_point
->impl
);
188 void anv_DestroyPipeline(
190 VkPipeline _pipeline
,
191 const VkAllocationCallbacks
* pAllocator
)
193 ANV_FROM_HANDLE(anv_device
, device
, _device
);
194 ANV_FROM_HANDLE(anv_pipeline
, pipeline
, _pipeline
);
199 anv_reloc_list_finish(&pipeline
->batch_relocs
,
200 pAllocator
? pAllocator
: &device
->alloc
);
201 if (pipeline
->blend_state
.map
)
202 anv_state_pool_free(&device
->dynamic_state_pool
, pipeline
->blend_state
);
204 for (unsigned s
= 0; s
< MESA_SHADER_STAGES
; s
++) {
205 if (pipeline
->shaders
[s
])
206 anv_shader_bin_unref(device
, pipeline
->shaders
[s
]);
209 vk_free2(&device
->alloc
, pAllocator
, pipeline
);
212 static const uint32_t vk_to_gen_primitive_type
[] = {
213 [VK_PRIMITIVE_TOPOLOGY_POINT_LIST
] = _3DPRIM_POINTLIST
,
214 [VK_PRIMITIVE_TOPOLOGY_LINE_LIST
] = _3DPRIM_LINELIST
,
215 [VK_PRIMITIVE_TOPOLOGY_LINE_STRIP
] = _3DPRIM_LINESTRIP
,
216 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST
] = _3DPRIM_TRILIST
,
217 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP
] = _3DPRIM_TRISTRIP
,
218 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN
] = _3DPRIM_TRIFAN
,
219 [VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY
] = _3DPRIM_LINELIST_ADJ
,
220 [VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY
] = _3DPRIM_LINESTRIP_ADJ
,
221 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY
] = _3DPRIM_TRILIST_ADJ
,
222 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY
] = _3DPRIM_TRISTRIP_ADJ
,
226 populate_sampler_prog_key(const struct gen_device_info
*devinfo
,
227 struct brw_sampler_prog_key_data
*key
)
229 /* XXX: Handle texture swizzle on HSW- */
230 for (int i
= 0; i
< MAX_SAMPLERS
; i
++) {
231 /* Assume color sampler, no swizzling. (Works for BDW+) */
232 key
->swizzles
[i
] = SWIZZLE_XYZW
;
237 populate_vs_prog_key(const struct gen_device_info
*devinfo
,
238 struct brw_vs_prog_key
*key
)
240 memset(key
, 0, sizeof(*key
));
242 populate_sampler_prog_key(devinfo
, &key
->tex
);
244 /* XXX: Handle vertex input work-arounds */
246 /* XXX: Handle sampler_prog_key */
250 populate_gs_prog_key(const struct gen_device_info
*devinfo
,
251 struct brw_gs_prog_key
*key
)
253 memset(key
, 0, sizeof(*key
));
255 populate_sampler_prog_key(devinfo
, &key
->tex
);
259 populate_wm_prog_key(const struct anv_pipeline
*pipeline
,
260 const VkGraphicsPipelineCreateInfo
*info
,
261 struct brw_wm_prog_key
*key
)
263 const struct gen_device_info
*devinfo
= &pipeline
->device
->info
;
264 ANV_FROM_HANDLE(anv_render_pass
, render_pass
, info
->renderPass
);
266 memset(key
, 0, sizeof(*key
));
268 populate_sampler_prog_key(devinfo
, &key
->tex
);
270 /* TODO: we could set this to 0 based on the information in nir_shader, but
271 * this function is called before spirv_to_nir. */
272 const struct brw_vue_map
*vue_map
=
273 anv_pipeline_get_fs_input_map(pipeline
);
274 key
->input_slots_valid
= vue_map
->slots_valid
;
276 /* Vulkan doesn't specify a default */
277 key
->high_quality_derivatives
= false;
279 /* XXX Vulkan doesn't appear to specify */
280 key
->clamp_fragment_color
= false;
282 key
->nr_color_regions
=
283 render_pass
->subpasses
[info
->subpass
].color_count
;
285 key
->replicate_alpha
= key
->nr_color_regions
> 1 &&
286 info
->pMultisampleState
&&
287 info
->pMultisampleState
->alphaToCoverageEnable
;
289 if (info
->pMultisampleState
&& info
->pMultisampleState
->rasterizationSamples
> 1) {
290 /* We should probably pull this out of the shader, but it's fairly
291 * harmless to compute it and then let dead-code take care of it.
293 key
->persample_interp
=
294 (info
->pMultisampleState
->minSampleShading
*
295 info
->pMultisampleState
->rasterizationSamples
) > 1;
296 key
->multisample_fbo
= true;
301 populate_cs_prog_key(const struct gen_device_info
*devinfo
,
302 struct brw_cs_prog_key
*key
)
304 memset(key
, 0, sizeof(*key
));
306 populate_sampler_prog_key(devinfo
, &key
->tex
);
310 anv_pipeline_compile(struct anv_pipeline
*pipeline
,
311 struct anv_shader_module
*module
,
312 const char *entrypoint
,
313 gl_shader_stage stage
,
314 const VkSpecializationInfo
*spec_info
,
315 struct brw_stage_prog_data
*prog_data
,
316 struct anv_pipeline_bind_map
*map
)
318 nir_shader
*nir
= anv_shader_compile_to_nir(pipeline
->device
,
319 module
, entrypoint
, stage
,
324 NIR_PASS_V(nir
, anv_nir_lower_push_constants
);
326 /* Figure out the number of parameters */
327 prog_data
->nr_params
= 0;
329 if (nir
->num_uniforms
> 0) {
330 /* If the shader uses any push constants at all, we'll just give
331 * them the maximum possible number
333 assert(nir
->num_uniforms
<= MAX_PUSH_CONSTANTS_SIZE
);
334 prog_data
->nr_params
+= MAX_PUSH_CONSTANTS_SIZE
/ sizeof(float);
337 if (pipeline
->layout
&& pipeline
->layout
->stage
[stage
].has_dynamic_offsets
)
338 prog_data
->nr_params
+= MAX_DYNAMIC_BUFFERS
* 2;
340 if (nir
->info
->num_images
> 0) {
341 prog_data
->nr_params
+= nir
->info
->num_images
* BRW_IMAGE_PARAM_SIZE
;
342 pipeline
->needs_data_cache
= true;
345 if (stage
== MESA_SHADER_COMPUTE
)
346 ((struct brw_cs_prog_data
*)prog_data
)->thread_local_id_index
=
347 prog_data
->nr_params
++; /* The CS Thread ID uniform */
349 if (nir
->info
->num_ssbos
> 0)
350 pipeline
->needs_data_cache
= true;
352 if (prog_data
->nr_params
> 0) {
353 /* XXX: I think we're leaking this */
354 prog_data
->param
= (const union gl_constant_value
**)
355 malloc(prog_data
->nr_params
* sizeof(union gl_constant_value
*));
357 /* We now set the param values to be offsets into a
358 * anv_push_constant_data structure. Since the compiler doesn't
359 * actually dereference any of the gl_constant_value pointers in the
360 * params array, it doesn't really matter what we put here.
362 struct anv_push_constants
*null_data
= NULL
;
363 if (nir
->num_uniforms
> 0) {
364 /* Fill out the push constants section of the param array */
365 for (unsigned i
= 0; i
< MAX_PUSH_CONSTANTS_SIZE
/ sizeof(float); i
++)
366 prog_data
->param
[i
] = (const union gl_constant_value
*)
367 &null_data
->client_data
[i
* sizeof(float)];
371 /* Set up dynamic offsets */
372 anv_nir_apply_dynamic_offsets(pipeline
, nir
, prog_data
);
374 /* Apply the actual pipeline layout to UBOs, SSBOs, and textures */
375 if (pipeline
->layout
)
376 anv_nir_apply_pipeline_layout(pipeline
, nir
, prog_data
, map
);
378 /* nir_lower_io will only handle the push constants; we need to set this
379 * to the full number of possible uniforms.
381 nir
->num_uniforms
= prog_data
->nr_params
* 4;
387 anv_fill_binding_table(struct brw_stage_prog_data
*prog_data
, unsigned bias
)
389 prog_data
->binding_table
.size_bytes
= 0;
390 prog_data
->binding_table
.texture_start
= bias
;
391 prog_data
->binding_table
.gather_texture_start
= bias
;
392 prog_data
->binding_table
.ubo_start
= bias
;
393 prog_data
->binding_table
.ssbo_start
= bias
;
394 prog_data
->binding_table
.image_start
= bias
;
397 static struct anv_shader_bin
*
398 anv_pipeline_upload_kernel(struct anv_pipeline
*pipeline
,
399 struct anv_pipeline_cache
*cache
,
400 const void *key_data
, uint32_t key_size
,
401 const void *kernel_data
, uint32_t kernel_size
,
402 const struct brw_stage_prog_data
*prog_data
,
403 uint32_t prog_data_size
,
404 const struct anv_pipeline_bind_map
*bind_map
)
407 return anv_pipeline_cache_upload_kernel(cache
, key_data
, key_size
,
408 kernel_data
, kernel_size
,
409 prog_data
, prog_data_size
,
412 return anv_shader_bin_create(pipeline
->device
, key_data
, key_size
,
413 kernel_data
, kernel_size
,
414 prog_data
, prog_data_size
,
415 prog_data
->param
, bind_map
);
421 anv_pipeline_add_compiled_stage(struct anv_pipeline
*pipeline
,
422 gl_shader_stage stage
,
423 struct anv_shader_bin
*shader
)
425 pipeline
->shaders
[stage
] = shader
;
426 pipeline
->active_stages
|= mesa_to_vk_shader_stage(stage
);
430 anv_pipeline_compile_vs(struct anv_pipeline
*pipeline
,
431 struct anv_pipeline_cache
*cache
,
432 const VkGraphicsPipelineCreateInfo
*info
,
433 struct anv_shader_module
*module
,
434 const char *entrypoint
,
435 const VkSpecializationInfo
*spec_info
)
437 const struct brw_compiler
*compiler
=
438 pipeline
->device
->instance
->physicalDevice
.compiler
;
439 struct anv_pipeline_bind_map map
;
440 struct brw_vs_prog_key key
;
441 struct anv_shader_bin
*bin
= NULL
;
442 unsigned char sha1
[20];
444 populate_vs_prog_key(&pipeline
->device
->info
, &key
);
447 anv_hash_shader(sha1
, &key
, sizeof(key
), module
, entrypoint
,
448 pipeline
->layout
, spec_info
);
449 bin
= anv_pipeline_cache_search(cache
, sha1
, 20);
453 struct brw_vs_prog_data prog_data
= { 0, };
454 struct anv_pipeline_binding surface_to_descriptor
[256];
455 struct anv_pipeline_binding sampler_to_descriptor
[256];
457 map
= (struct anv_pipeline_bind_map
) {
458 .surface_to_descriptor
= surface_to_descriptor
,
459 .sampler_to_descriptor
= sampler_to_descriptor
462 nir_shader
*nir
= anv_pipeline_compile(pipeline
, module
, entrypoint
,
463 MESA_SHADER_VERTEX
, spec_info
,
464 &prog_data
.base
.base
, &map
);
466 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
468 anv_fill_binding_table(&prog_data
.base
.base
, 0);
470 void *mem_ctx
= ralloc_context(NULL
);
472 ralloc_steal(mem_ctx
, nir
);
474 prog_data
.inputs_read
= nir
->info
->inputs_read
;
475 prog_data
.double_inputs_read
= nir
->info
->double_inputs_read
;
477 brw_compute_vue_map(&pipeline
->device
->info
,
478 &prog_data
.base
.vue_map
,
479 nir
->info
->outputs_written
,
480 nir
->info
->separate_shader
);
483 const unsigned *shader_code
=
484 brw_compile_vs(compiler
, NULL
, mem_ctx
, &key
, &prog_data
, nir
,
485 NULL
, false, -1, &code_size
, NULL
);
486 if (shader_code
== NULL
) {
487 ralloc_free(mem_ctx
);
488 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
491 bin
= anv_pipeline_upload_kernel(pipeline
, cache
, sha1
, 20,
492 shader_code
, code_size
,
493 &prog_data
.base
.base
, sizeof(prog_data
),
496 ralloc_free(mem_ctx
);
497 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
500 ralloc_free(mem_ctx
);
503 anv_pipeline_add_compiled_stage(pipeline
, MESA_SHADER_VERTEX
, bin
);
509 merge_tess_info(struct shader_info
*tes_info
,
510 const struct shader_info
*tcs_info
)
512 /* The Vulkan 1.0.38 spec, section 21.1 Tessellator says:
514 * "PointMode. Controls generation of points rather than triangles
515 * or lines. This functionality defaults to disabled, and is
516 * enabled if either shader stage includes the execution mode.
518 * and about Triangles, Quads, IsoLines, VertexOrderCw, VertexOrderCcw,
519 * PointMode, SpacingEqual, SpacingFractionalEven, SpacingFractionalOdd,
520 * and OutputVertices, it says:
522 * "One mode must be set in at least one of the tessellation
525 * So, the fields can be set in either the TCS or TES, but they must
526 * agree if set in both. Our backend looks at TES, so bitwise-or in
527 * the values from the TCS.
529 assert(tcs_info
->tess
.tcs_vertices_out
== 0 ||
530 tes_info
->tess
.tcs_vertices_out
== 0 ||
531 tcs_info
->tess
.tcs_vertices_out
== tes_info
->tess
.tcs_vertices_out
);
532 tes_info
->tess
.tcs_vertices_out
|= tcs_info
->tess
.tcs_vertices_out
;
534 assert(tcs_info
->tess
.spacing
== TESS_SPACING_UNSPECIFIED
||
535 tes_info
->tess
.spacing
== TESS_SPACING_UNSPECIFIED
||
536 tcs_info
->tess
.spacing
== tes_info
->tess
.spacing
);
537 tes_info
->tess
.spacing
|= tcs_info
->tess
.spacing
;
539 tes_info
->tess
.ccw
|= tcs_info
->tess
.ccw
;
540 tes_info
->tess
.point_mode
|= tcs_info
->tess
.point_mode
;
544 anv_pipeline_compile_tcs_tes(struct anv_pipeline
*pipeline
,
545 struct anv_pipeline_cache
*cache
,
546 const VkGraphicsPipelineCreateInfo
*info
,
547 struct anv_shader_module
*tcs_module
,
548 const char *tcs_entrypoint
,
549 const VkSpecializationInfo
*tcs_spec_info
,
550 struct anv_shader_module
*tes_module
,
551 const char *tes_entrypoint
,
552 const VkSpecializationInfo
*tes_spec_info
)
554 const struct gen_device_info
*devinfo
= &pipeline
->device
->info
;
555 const struct brw_compiler
*compiler
=
556 pipeline
->device
->instance
->physicalDevice
.compiler
;
557 struct anv_pipeline_bind_map tcs_map
;
558 struct anv_pipeline_bind_map tes_map
;
559 struct brw_tcs_prog_key tcs_key
= { 0, };
560 struct brw_tes_prog_key tes_key
= { 0, };
561 struct anv_shader_bin
*tcs_bin
= NULL
;
562 struct anv_shader_bin
*tes_bin
= NULL
;
563 unsigned char tcs_sha1
[40];
564 unsigned char tes_sha1
[40];
566 populate_sampler_prog_key(&pipeline
->device
->info
, &tcs_key
.tex
);
567 populate_sampler_prog_key(&pipeline
->device
->info
, &tes_key
.tex
);
568 tcs_key
.input_vertices
= info
->pTessellationState
->patchControlPoints
;
571 anv_hash_shader(tcs_sha1
, &tcs_key
, sizeof(tcs_key
), tcs_module
,
572 tcs_entrypoint
, pipeline
->layout
, tcs_spec_info
);
573 anv_hash_shader(tes_sha1
, &tes_key
, sizeof(tes_key
), tes_module
,
574 tes_entrypoint
, pipeline
->layout
, tes_spec_info
);
575 memcpy(&tcs_sha1
[20], tes_sha1
, 20);
576 memcpy(&tes_sha1
[20], tcs_sha1
, 20);
577 tcs_bin
= anv_pipeline_cache_search(cache
, tcs_sha1
, sizeof(tcs_sha1
));
578 tes_bin
= anv_pipeline_cache_search(cache
, tes_sha1
, sizeof(tes_sha1
));
581 if (tcs_bin
== NULL
|| tes_bin
== NULL
) {
582 struct brw_tcs_prog_data tcs_prog_data
= { 0, };
583 struct brw_tes_prog_data tes_prog_data
= { 0, };
584 struct anv_pipeline_binding tcs_surface_to_descriptor
[256];
585 struct anv_pipeline_binding tcs_sampler_to_descriptor
[256];
586 struct anv_pipeline_binding tes_surface_to_descriptor
[256];
587 struct anv_pipeline_binding tes_sampler_to_descriptor
[256];
589 tcs_map
= (struct anv_pipeline_bind_map
) {
590 .surface_to_descriptor
= tcs_surface_to_descriptor
,
591 .sampler_to_descriptor
= tcs_sampler_to_descriptor
593 tes_map
= (struct anv_pipeline_bind_map
) {
594 .surface_to_descriptor
= tes_surface_to_descriptor
,
595 .sampler_to_descriptor
= tes_sampler_to_descriptor
598 nir_shader
*tcs_nir
=
599 anv_pipeline_compile(pipeline
, tcs_module
, tcs_entrypoint
,
600 MESA_SHADER_TESS_CTRL
, tcs_spec_info
,
601 &tcs_prog_data
.base
.base
, &tcs_map
);
602 nir_shader
*tes_nir
=
603 anv_pipeline_compile(pipeline
, tes_module
, tes_entrypoint
,
604 MESA_SHADER_TESS_EVAL
, tes_spec_info
,
605 &tes_prog_data
.base
.base
, &tes_map
);
606 if (tcs_nir
== NULL
|| tes_nir
== NULL
)
607 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
609 nir_lower_tes_patch_vertices(tes_nir
,
610 tcs_nir
->info
->tess
.tcs_vertices_out
);
612 /* Copy TCS info into the TES info */
613 merge_tess_info(tes_nir
->info
, tcs_nir
->info
);
615 anv_fill_binding_table(&tcs_prog_data
.base
.base
, 0);
616 anv_fill_binding_table(&tes_prog_data
.base
.base
, 0);
618 void *mem_ctx
= ralloc_context(NULL
);
620 ralloc_steal(mem_ctx
, tcs_nir
);
621 ralloc_steal(mem_ctx
, tes_nir
);
623 /* Whacking the key after cache lookup is a bit sketchy, but all of
624 * this comes from the SPIR-V, which is part of the hash used for the
625 * pipeline cache. So it should be safe.
627 tcs_key
.tes_primitive_mode
= tes_nir
->info
->tess
.primitive_mode
;
628 tcs_key
.outputs_written
= tcs_nir
->info
->outputs_written
;
629 tcs_key
.patch_outputs_written
= tcs_nir
->info
->patch_outputs_written
;
630 tcs_key
.quads_workaround
=
632 tes_nir
->info
->tess
.primitive_mode
== 7 /* GL_QUADS */ &&
633 tes_nir
->info
->tess
.spacing
== TESS_SPACING_EQUAL
;
635 tes_key
.inputs_read
= tcs_key
.outputs_written
;
636 tes_key
.patch_inputs_read
= tcs_key
.patch_outputs_written
;
639 const int shader_time_index
= -1;
640 const unsigned *shader_code
;
643 brw_compile_tcs(compiler
, NULL
, mem_ctx
, &tcs_key
, &tcs_prog_data
,
644 tcs_nir
, shader_time_index
, &code_size
, NULL
);
645 if (shader_code
== NULL
) {
646 ralloc_free(mem_ctx
);
647 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
650 tcs_bin
= anv_pipeline_upload_kernel(pipeline
, cache
,
651 tcs_sha1
, sizeof(tcs_sha1
),
652 shader_code
, code_size
,
653 &tcs_prog_data
.base
.base
,
654 sizeof(tcs_prog_data
),
657 ralloc_free(mem_ctx
);
658 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
662 brw_compile_tes(compiler
, NULL
, mem_ctx
, &tes_key
,
663 &tcs_prog_data
.base
.vue_map
, &tes_prog_data
, tes_nir
,
664 NULL
, shader_time_index
, &code_size
, NULL
);
665 if (shader_code
== NULL
) {
666 ralloc_free(mem_ctx
);
667 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
670 tes_bin
= anv_pipeline_upload_kernel(pipeline
, cache
,
671 tes_sha1
, sizeof(tes_sha1
),
672 shader_code
, code_size
,
673 &tes_prog_data
.base
.base
,
674 sizeof(tes_prog_data
),
677 ralloc_free(mem_ctx
);
678 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
681 ralloc_free(mem_ctx
);
684 anv_pipeline_add_compiled_stage(pipeline
, MESA_SHADER_TESS_CTRL
, tcs_bin
);
685 anv_pipeline_add_compiled_stage(pipeline
, MESA_SHADER_TESS_EVAL
, tes_bin
);
691 anv_pipeline_compile_gs(struct anv_pipeline
*pipeline
,
692 struct anv_pipeline_cache
*cache
,
693 const VkGraphicsPipelineCreateInfo
*info
,
694 struct anv_shader_module
*module
,
695 const char *entrypoint
,
696 const VkSpecializationInfo
*spec_info
)
698 const struct brw_compiler
*compiler
=
699 pipeline
->device
->instance
->physicalDevice
.compiler
;
700 struct anv_pipeline_bind_map map
;
701 struct brw_gs_prog_key key
;
702 struct anv_shader_bin
*bin
= NULL
;
703 unsigned char sha1
[20];
705 populate_gs_prog_key(&pipeline
->device
->info
, &key
);
708 anv_hash_shader(sha1
, &key
, sizeof(key
), module
, entrypoint
,
709 pipeline
->layout
, spec_info
);
710 bin
= anv_pipeline_cache_search(cache
, sha1
, 20);
714 struct brw_gs_prog_data prog_data
= { 0, };
715 struct anv_pipeline_binding surface_to_descriptor
[256];
716 struct anv_pipeline_binding sampler_to_descriptor
[256];
718 map
= (struct anv_pipeline_bind_map
) {
719 .surface_to_descriptor
= surface_to_descriptor
,
720 .sampler_to_descriptor
= sampler_to_descriptor
723 nir_shader
*nir
= anv_pipeline_compile(pipeline
, module
, entrypoint
,
724 MESA_SHADER_GEOMETRY
, spec_info
,
725 &prog_data
.base
.base
, &map
);
727 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
729 anv_fill_binding_table(&prog_data
.base
.base
, 0);
731 void *mem_ctx
= ralloc_context(NULL
);
733 ralloc_steal(mem_ctx
, nir
);
735 brw_compute_vue_map(&pipeline
->device
->info
,
736 &prog_data
.base
.vue_map
,
737 nir
->info
->outputs_written
,
738 nir
->info
->separate_shader
);
741 const unsigned *shader_code
=
742 brw_compile_gs(compiler
, NULL
, mem_ctx
, &key
, &prog_data
, nir
,
743 NULL
, -1, &code_size
, NULL
);
744 if (shader_code
== NULL
) {
745 ralloc_free(mem_ctx
);
746 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
750 bin
= anv_pipeline_upload_kernel(pipeline
, cache
, sha1
, 20,
751 shader_code
, code_size
,
752 &prog_data
.base
.base
, sizeof(prog_data
),
755 ralloc_free(mem_ctx
);
756 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
759 ralloc_free(mem_ctx
);
762 anv_pipeline_add_compiled_stage(pipeline
, MESA_SHADER_GEOMETRY
, bin
);
768 anv_pipeline_compile_fs(struct anv_pipeline
*pipeline
,
769 struct anv_pipeline_cache
*cache
,
770 const VkGraphicsPipelineCreateInfo
*info
,
771 struct anv_shader_module
*module
,
772 const char *entrypoint
,
773 const VkSpecializationInfo
*spec_info
)
775 const struct brw_compiler
*compiler
=
776 pipeline
->device
->instance
->physicalDevice
.compiler
;
777 struct anv_pipeline_bind_map map
;
778 struct brw_wm_prog_key key
;
779 struct anv_shader_bin
*bin
= NULL
;
780 unsigned char sha1
[20];
782 populate_wm_prog_key(pipeline
, info
, &key
);
785 anv_hash_shader(sha1
, &key
, sizeof(key
), module
, entrypoint
,
786 pipeline
->layout
, spec_info
);
787 bin
= anv_pipeline_cache_search(cache
, sha1
, 20);
791 struct brw_wm_prog_data prog_data
= { 0, };
792 struct anv_pipeline_binding surface_to_descriptor
[256];
793 struct anv_pipeline_binding sampler_to_descriptor
[256];
795 map
= (struct anv_pipeline_bind_map
) {
796 .surface_to_descriptor
= surface_to_descriptor
+ 8,
797 .sampler_to_descriptor
= sampler_to_descriptor
800 nir_shader
*nir
= anv_pipeline_compile(pipeline
, module
, entrypoint
,
801 MESA_SHADER_FRAGMENT
, spec_info
,
802 &prog_data
.base
, &map
);
804 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
806 unsigned num_rts
= 0;
807 struct anv_pipeline_binding rt_bindings
[8];
808 nir_function_impl
*impl
= nir_shader_get_entrypoint(nir
);
809 nir_foreach_variable_safe(var
, &nir
->outputs
) {
810 if (var
->data
.location
< FRAG_RESULT_DATA0
)
813 unsigned rt
= var
->data
.location
- FRAG_RESULT_DATA0
;
814 if (rt
>= key
.nr_color_regions
) {
815 /* Out-of-bounds, throw it away */
816 var
->data
.mode
= nir_var_local
;
817 exec_node_remove(&var
->node
);
818 exec_list_push_tail(&impl
->locals
, &var
->node
);
822 /* Give it a new, compacted, location */
823 var
->data
.location
= FRAG_RESULT_DATA0
+ num_rts
;
826 glsl_type_is_array(var
->type
) ? glsl_get_length(var
->type
) : 1;
827 assert(num_rts
+ array_len
<= 8);
829 for (unsigned i
= 0; i
< array_len
; i
++) {
830 rt_bindings
[num_rts
+ i
] = (struct anv_pipeline_binding
) {
831 .set
= ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS
,
837 num_rts
+= array_len
;
841 /* If we have no render targets, we need a null render target */
842 rt_bindings
[0] = (struct anv_pipeline_binding
) {
843 .set
= ANV_DESCRIPTOR_SET_COLOR_ATTACHMENTS
,
850 assert(num_rts
<= 8);
851 map
.surface_to_descriptor
-= num_rts
;
852 map
.surface_count
+= num_rts
;
853 assert(map
.surface_count
<= 256);
854 memcpy(map
.surface_to_descriptor
, rt_bindings
,
855 num_rts
* sizeof(*rt_bindings
));
857 anv_fill_binding_table(&prog_data
.base
, num_rts
);
859 void *mem_ctx
= ralloc_context(NULL
);
861 ralloc_steal(mem_ctx
, nir
);
864 const unsigned *shader_code
=
865 brw_compile_fs(compiler
, NULL
, mem_ctx
, &key
, &prog_data
, nir
,
866 NULL
, -1, -1, true, false, NULL
, &code_size
, NULL
);
867 if (shader_code
== NULL
) {
868 ralloc_free(mem_ctx
);
869 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
872 bin
= anv_pipeline_upload_kernel(pipeline
, cache
, sha1
, 20,
873 shader_code
, code_size
,
874 &prog_data
.base
, sizeof(prog_data
),
877 ralloc_free(mem_ctx
);
878 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
881 ralloc_free(mem_ctx
);
884 anv_pipeline_add_compiled_stage(pipeline
, MESA_SHADER_FRAGMENT
, bin
);
890 anv_pipeline_compile_cs(struct anv_pipeline
*pipeline
,
891 struct anv_pipeline_cache
*cache
,
892 const VkComputePipelineCreateInfo
*info
,
893 struct anv_shader_module
*module
,
894 const char *entrypoint
,
895 const VkSpecializationInfo
*spec_info
)
897 const struct brw_compiler
*compiler
=
898 pipeline
->device
->instance
->physicalDevice
.compiler
;
899 struct anv_pipeline_bind_map map
;
900 struct brw_cs_prog_key key
;
901 struct anv_shader_bin
*bin
= NULL
;
902 unsigned char sha1
[20];
904 populate_cs_prog_key(&pipeline
->device
->info
, &key
);
907 anv_hash_shader(sha1
, &key
, sizeof(key
), module
, entrypoint
,
908 pipeline
->layout
, spec_info
);
909 bin
= anv_pipeline_cache_search(cache
, sha1
, 20);
913 struct brw_cs_prog_data prog_data
= { 0, };
914 struct anv_pipeline_binding surface_to_descriptor
[256];
915 struct anv_pipeline_binding sampler_to_descriptor
[256];
917 map
= (struct anv_pipeline_bind_map
) {
918 .surface_to_descriptor
= surface_to_descriptor
,
919 .sampler_to_descriptor
= sampler_to_descriptor
922 nir_shader
*nir
= anv_pipeline_compile(pipeline
, module
, entrypoint
,
923 MESA_SHADER_COMPUTE
, spec_info
,
924 &prog_data
.base
, &map
);
926 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
928 anv_fill_binding_table(&prog_data
.base
, 1);
930 void *mem_ctx
= ralloc_context(NULL
);
932 ralloc_steal(mem_ctx
, nir
);
935 const unsigned *shader_code
=
936 brw_compile_cs(compiler
, NULL
, mem_ctx
, &key
, &prog_data
, nir
,
937 -1, &code_size
, NULL
);
938 if (shader_code
== NULL
) {
939 ralloc_free(mem_ctx
);
940 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
943 bin
= anv_pipeline_upload_kernel(pipeline
, cache
, sha1
, 20,
944 shader_code
, code_size
,
945 &prog_data
.base
, sizeof(prog_data
),
948 ralloc_free(mem_ctx
);
949 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
952 ralloc_free(mem_ctx
);
955 anv_pipeline_add_compiled_stage(pipeline
, MESA_SHADER_COMPUTE
, bin
);
961 * Copy pipeline state not marked as dynamic.
962 * Dynamic state is pipeline state which hasn't been provided at pipeline
963 * creation time, but is dynamically provided afterwards using various
964 * vkCmdSet* functions.
966 * The set of state considered "non_dynamic" is determined by the pieces of
967 * state that have their corresponding VkDynamicState enums omitted from
968 * VkPipelineDynamicStateCreateInfo::pDynamicStates.
970 * @param[out] pipeline Destination non_dynamic state.
971 * @param[in] pCreateInfo Source of non_dynamic state to be copied.
974 copy_non_dynamic_state(struct anv_pipeline
*pipeline
,
975 const VkGraphicsPipelineCreateInfo
*pCreateInfo
)
977 anv_cmd_dirty_mask_t states
= ANV_CMD_DIRTY_DYNAMIC_ALL
;
978 ANV_FROM_HANDLE(anv_render_pass
, pass
, pCreateInfo
->renderPass
);
979 struct anv_subpass
*subpass
= &pass
->subpasses
[pCreateInfo
->subpass
];
981 pipeline
->dynamic_state
= default_dynamic_state
;
983 if (pCreateInfo
->pDynamicState
) {
984 /* Remove all of the states that are marked as dynamic */
985 uint32_t count
= pCreateInfo
->pDynamicState
->dynamicStateCount
;
986 for (uint32_t s
= 0; s
< count
; s
++)
987 states
&= ~(1 << pCreateInfo
->pDynamicState
->pDynamicStates
[s
]);
990 struct anv_dynamic_state
*dynamic
= &pipeline
->dynamic_state
;
992 /* Section 9.2 of the Vulkan 1.0.15 spec says:
994 * pViewportState is [...] NULL if the pipeline
995 * has rasterization disabled.
997 if (!pCreateInfo
->pRasterizationState
->rasterizerDiscardEnable
) {
998 assert(pCreateInfo
->pViewportState
);
1000 dynamic
->viewport
.count
= pCreateInfo
->pViewportState
->viewportCount
;
1001 if (states
& (1 << VK_DYNAMIC_STATE_VIEWPORT
)) {
1002 typed_memcpy(dynamic
->viewport
.viewports
,
1003 pCreateInfo
->pViewportState
->pViewports
,
1004 pCreateInfo
->pViewportState
->viewportCount
);
1007 dynamic
->scissor
.count
= pCreateInfo
->pViewportState
->scissorCount
;
1008 if (states
& (1 << VK_DYNAMIC_STATE_SCISSOR
)) {
1009 typed_memcpy(dynamic
->scissor
.scissors
,
1010 pCreateInfo
->pViewportState
->pScissors
,
1011 pCreateInfo
->pViewportState
->scissorCount
);
1015 if (states
& (1 << VK_DYNAMIC_STATE_LINE_WIDTH
)) {
1016 assert(pCreateInfo
->pRasterizationState
);
1017 dynamic
->line_width
= pCreateInfo
->pRasterizationState
->lineWidth
;
1020 if (states
& (1 << VK_DYNAMIC_STATE_DEPTH_BIAS
)) {
1021 assert(pCreateInfo
->pRasterizationState
);
1022 dynamic
->depth_bias
.bias
=
1023 pCreateInfo
->pRasterizationState
->depthBiasConstantFactor
;
1024 dynamic
->depth_bias
.clamp
=
1025 pCreateInfo
->pRasterizationState
->depthBiasClamp
;
1026 dynamic
->depth_bias
.slope
=
1027 pCreateInfo
->pRasterizationState
->depthBiasSlopeFactor
;
1030 /* Section 9.2 of the Vulkan 1.0.15 spec says:
1032 * pColorBlendState is [...] NULL if the pipeline has rasterization
1033 * disabled or if the subpass of the render pass the pipeline is
1034 * created against does not use any color attachments.
1036 bool uses_color_att
= false;
1037 for (unsigned i
= 0; i
< subpass
->color_count
; ++i
) {
1038 if (subpass
->color_attachments
[i
] != VK_ATTACHMENT_UNUSED
) {
1039 uses_color_att
= true;
1044 if (uses_color_att
&&
1045 !pCreateInfo
->pRasterizationState
->rasterizerDiscardEnable
) {
1046 assert(pCreateInfo
->pColorBlendState
);
1048 if (states
& (1 << VK_DYNAMIC_STATE_BLEND_CONSTANTS
))
1049 typed_memcpy(dynamic
->blend_constants
,
1050 pCreateInfo
->pColorBlendState
->blendConstants
, 4);
1053 /* If there is no depthstencil attachment, then don't read
1054 * pDepthStencilState. The Vulkan spec states that pDepthStencilState may
1055 * be NULL in this case. Even if pDepthStencilState is non-NULL, there is
1056 * no need to override the depthstencil defaults in
1057 * anv_pipeline::dynamic_state when there is no depthstencil attachment.
1059 * Section 9.2 of the Vulkan 1.0.15 spec says:
1061 * pDepthStencilState is [...] NULL if the pipeline has rasterization
1062 * disabled or if the subpass of the render pass the pipeline is created
1063 * against does not use a depth/stencil attachment.
1065 if (!pCreateInfo
->pRasterizationState
->rasterizerDiscardEnable
&&
1066 subpass
->depth_stencil_attachment
!= VK_ATTACHMENT_UNUSED
) {
1067 assert(pCreateInfo
->pDepthStencilState
);
1069 if (states
& (1 << VK_DYNAMIC_STATE_DEPTH_BOUNDS
)) {
1070 dynamic
->depth_bounds
.min
=
1071 pCreateInfo
->pDepthStencilState
->minDepthBounds
;
1072 dynamic
->depth_bounds
.max
=
1073 pCreateInfo
->pDepthStencilState
->maxDepthBounds
;
1076 if (states
& (1 << VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK
)) {
1077 dynamic
->stencil_compare_mask
.front
=
1078 pCreateInfo
->pDepthStencilState
->front
.compareMask
;
1079 dynamic
->stencil_compare_mask
.back
=
1080 pCreateInfo
->pDepthStencilState
->back
.compareMask
;
1083 if (states
& (1 << VK_DYNAMIC_STATE_STENCIL_WRITE_MASK
)) {
1084 dynamic
->stencil_write_mask
.front
=
1085 pCreateInfo
->pDepthStencilState
->front
.writeMask
;
1086 dynamic
->stencil_write_mask
.back
=
1087 pCreateInfo
->pDepthStencilState
->back
.writeMask
;
1090 if (states
& (1 << VK_DYNAMIC_STATE_STENCIL_REFERENCE
)) {
1091 dynamic
->stencil_reference
.front
=
1092 pCreateInfo
->pDepthStencilState
->front
.reference
;
1093 dynamic
->stencil_reference
.back
=
1094 pCreateInfo
->pDepthStencilState
->back
.reference
;
1098 pipeline
->dynamic_state_mask
= states
;
1102 anv_pipeline_validate_create_info(const VkGraphicsPipelineCreateInfo
*info
)
1104 struct anv_render_pass
*renderpass
= NULL
;
1105 struct anv_subpass
*subpass
= NULL
;
1107 /* Assert that all required members of VkGraphicsPipelineCreateInfo are
1108 * present. See the Vulkan 1.0.28 spec, Section 9.2 Graphics Pipelines.
1110 assert(info
->sType
== VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO
);
1112 renderpass
= anv_render_pass_from_handle(info
->renderPass
);
1115 assert(info
->subpass
< renderpass
->subpass_count
);
1116 subpass
= &renderpass
->subpasses
[info
->subpass
];
1118 assert(info
->stageCount
>= 1);
1119 assert(info
->pVertexInputState
);
1120 assert(info
->pInputAssemblyState
);
1121 assert(info
->pRasterizationState
);
1122 if (!info
->pRasterizationState
->rasterizerDiscardEnable
) {
1123 assert(info
->pViewportState
);
1124 assert(info
->pMultisampleState
);
1126 if (subpass
&& subpass
->depth_stencil_attachment
!= VK_ATTACHMENT_UNUSED
)
1127 assert(info
->pDepthStencilState
);
1129 if (subpass
&& subpass
->color_count
> 0)
1130 assert(info
->pColorBlendState
);
1133 for (uint32_t i
= 0; i
< info
->stageCount
; ++i
) {
1134 switch (info
->pStages
[i
].stage
) {
1135 case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT
:
1136 case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT
:
1137 assert(info
->pTessellationState
);
1146 * Calculate the desired L3 partitioning based on the current state of the
1147 * pipeline. For now this simply returns the conservative defaults calculated
1148 * by get_default_l3_weights(), but we could probably do better by gathering
1149 * more statistics from the pipeline state (e.g. guess of expected URB usage
1150 * and bound surfaces), or by using feed-back from performance counters.
1153 anv_pipeline_setup_l3_config(struct anv_pipeline
*pipeline
, bool needs_slm
)
1155 const struct gen_device_info
*devinfo
= &pipeline
->device
->info
;
1157 const struct gen_l3_weights w
=
1158 gen_get_default_l3_weights(devinfo
, pipeline
->needs_data_cache
, needs_slm
);
1160 pipeline
->urb
.l3_config
= gen_get_l3_config(devinfo
, w
);
1161 pipeline
->urb
.total_size
=
1162 gen_get_l3_config_urb_size(devinfo
, pipeline
->urb
.l3_config
);
1166 anv_pipeline_init(struct anv_pipeline
*pipeline
,
1167 struct anv_device
*device
,
1168 struct anv_pipeline_cache
*cache
,
1169 const VkGraphicsPipelineCreateInfo
*pCreateInfo
,
1170 const VkAllocationCallbacks
*alloc
)
1175 anv_pipeline_validate_create_info(pCreateInfo
);
1179 alloc
= &device
->alloc
;
1181 pipeline
->device
= device
;
1182 pipeline
->layout
= anv_pipeline_layout_from_handle(pCreateInfo
->layout
);
1184 result
= anv_reloc_list_init(&pipeline
->batch_relocs
, alloc
);
1185 if (result
!= VK_SUCCESS
)
1188 pipeline
->batch
.alloc
= alloc
;
1189 pipeline
->batch
.next
= pipeline
->batch
.start
= pipeline
->batch_data
;
1190 pipeline
->batch
.end
= pipeline
->batch
.start
+ sizeof(pipeline
->batch_data
);
1191 pipeline
->batch
.relocs
= &pipeline
->batch_relocs
;
1193 copy_non_dynamic_state(pipeline
, pCreateInfo
);
1194 pipeline
->depth_clamp_enable
= pCreateInfo
->pRasterizationState
&&
1195 pCreateInfo
->pRasterizationState
->depthClampEnable
;
1197 pipeline
->needs_data_cache
= false;
1199 /* When we free the pipeline, we detect stages based on the NULL status
1200 * of various prog_data pointers. Make them NULL by default.
1202 memset(pipeline
->shaders
, 0, sizeof(pipeline
->shaders
));
1204 pipeline
->active_stages
= 0;
1206 const VkPipelineShaderStageCreateInfo
*pStages
[MESA_SHADER_STAGES
] = { 0, };
1207 struct anv_shader_module
*modules
[MESA_SHADER_STAGES
] = { 0, };
1208 for (uint32_t i
= 0; i
< pCreateInfo
->stageCount
; i
++) {
1209 gl_shader_stage stage
= ffs(pCreateInfo
->pStages
[i
].stage
) - 1;
1210 pStages
[stage
] = &pCreateInfo
->pStages
[i
];
1211 modules
[stage
] = anv_shader_module_from_handle(pStages
[stage
]->module
);
1214 if (modules
[MESA_SHADER_VERTEX
]) {
1215 result
= anv_pipeline_compile_vs(pipeline
, cache
, pCreateInfo
,
1216 modules
[MESA_SHADER_VERTEX
],
1217 pStages
[MESA_SHADER_VERTEX
]->pName
,
1218 pStages
[MESA_SHADER_VERTEX
]->pSpecializationInfo
);
1219 if (result
!= VK_SUCCESS
)
1223 if (modules
[MESA_SHADER_TESS_EVAL
]) {
1224 anv_pipeline_compile_tcs_tes(pipeline
, cache
, pCreateInfo
,
1225 modules
[MESA_SHADER_TESS_CTRL
],
1226 pStages
[MESA_SHADER_TESS_CTRL
]->pName
,
1227 pStages
[MESA_SHADER_TESS_CTRL
]->pSpecializationInfo
,
1228 modules
[MESA_SHADER_TESS_EVAL
],
1229 pStages
[MESA_SHADER_TESS_EVAL
]->pName
,
1230 pStages
[MESA_SHADER_TESS_EVAL
]->pSpecializationInfo
);
1233 if (modules
[MESA_SHADER_GEOMETRY
]) {
1234 result
= anv_pipeline_compile_gs(pipeline
, cache
, pCreateInfo
,
1235 modules
[MESA_SHADER_GEOMETRY
],
1236 pStages
[MESA_SHADER_GEOMETRY
]->pName
,
1237 pStages
[MESA_SHADER_GEOMETRY
]->pSpecializationInfo
);
1238 if (result
!= VK_SUCCESS
)
1242 if (modules
[MESA_SHADER_FRAGMENT
]) {
1243 result
= anv_pipeline_compile_fs(pipeline
, cache
, pCreateInfo
,
1244 modules
[MESA_SHADER_FRAGMENT
],
1245 pStages
[MESA_SHADER_FRAGMENT
]->pName
,
1246 pStages
[MESA_SHADER_FRAGMENT
]->pSpecializationInfo
);
1247 if (result
!= VK_SUCCESS
)
1251 assert(pipeline
->active_stages
& VK_SHADER_STAGE_VERTEX_BIT
);
1253 anv_pipeline_setup_l3_config(pipeline
, false);
1255 const VkPipelineVertexInputStateCreateInfo
*vi_info
=
1256 pCreateInfo
->pVertexInputState
;
1258 const uint64_t inputs_read
= get_vs_prog_data(pipeline
)->inputs_read
;
1260 pipeline
->vb_used
= 0;
1261 for (uint32_t i
= 0; i
< vi_info
->vertexAttributeDescriptionCount
; i
++) {
1262 const VkVertexInputAttributeDescription
*desc
=
1263 &vi_info
->pVertexAttributeDescriptions
[i
];
1265 if (inputs_read
& (1 << (VERT_ATTRIB_GENERIC0
+ desc
->location
)))
1266 pipeline
->vb_used
|= 1 << desc
->binding
;
1269 for (uint32_t i
= 0; i
< vi_info
->vertexBindingDescriptionCount
; i
++) {
1270 const VkVertexInputBindingDescription
*desc
=
1271 &vi_info
->pVertexBindingDescriptions
[i
];
1273 pipeline
->binding_stride
[desc
->binding
] = desc
->stride
;
1275 /* Step rate is programmed per vertex element (attribute), not
1276 * binding. Set up a map of which bindings step per instance, for
1277 * reference by vertex element setup. */
1278 switch (desc
->inputRate
) {
1280 case VK_VERTEX_INPUT_RATE_VERTEX
:
1281 pipeline
->instancing_enable
[desc
->binding
] = false;
1283 case VK_VERTEX_INPUT_RATE_INSTANCE
:
1284 pipeline
->instancing_enable
[desc
->binding
] = true;
1289 const VkPipelineInputAssemblyStateCreateInfo
*ia_info
=
1290 pCreateInfo
->pInputAssemblyState
;
1291 const VkPipelineTessellationStateCreateInfo
*tess_info
=
1292 pCreateInfo
->pTessellationState
;
1293 pipeline
->primitive_restart
= ia_info
->primitiveRestartEnable
;
1295 if (anv_pipeline_has_stage(pipeline
, MESA_SHADER_TESS_EVAL
))
1296 pipeline
->topology
= _3DPRIM_PATCHLIST(tess_info
->patchControlPoints
);
1298 pipeline
->topology
= vk_to_gen_primitive_type
[ia_info
->topology
];
1303 for (unsigned s
= 0; s
< MESA_SHADER_STAGES
; s
++) {
1304 if (pipeline
->shaders
[s
])
1305 anv_shader_bin_unref(device
, pipeline
->shaders
[s
]);
1308 anv_reloc_list_finish(&pipeline
->batch_relocs
, alloc
);