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 "anv_private.h"
33 #include "glsl/nir/nir_spirv.h"
35 /* Needed for SWIZZLE macros */
36 #include "program/prog_instruction.h"
40 VkResult
anv_CreateShaderModule(
42 const VkShaderModuleCreateInfo
* pCreateInfo
,
43 const VkAllocationCallbacks
* pAllocator
,
44 VkShaderModule
* pShaderModule
)
46 ANV_FROM_HANDLE(anv_device
, device
, _device
);
47 struct anv_shader_module
*module
;
49 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO
);
50 assert(pCreateInfo
->flags
== 0);
52 module
= anv_alloc2(&device
->alloc
, pAllocator
,
53 sizeof(*module
) + pCreateInfo
->codeSize
, 8,
54 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
56 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
59 module
->size
= pCreateInfo
->codeSize
;
60 memcpy(module
->data
, pCreateInfo
->pCode
, module
->size
);
62 *pShaderModule
= anv_shader_module_to_handle(module
);
67 void anv_DestroyShaderModule(
69 VkShaderModule _module
,
70 const VkAllocationCallbacks
* pAllocator
)
72 ANV_FROM_HANDLE(anv_device
, device
, _device
);
73 ANV_FROM_HANDLE(anv_shader_module
, module
, _module
);
75 anv_free2(&device
->alloc
, pAllocator
, module
);
78 #define SPIR_V_MAGIC_NUMBER 0x07230203
80 /* Eventually, this will become part of anv_CreateShader. Unfortunately,
81 * we can't do that yet because we don't have the ability to copy nir.
84 anv_shader_compile_to_nir(struct anv_device
*device
,
85 struct anv_shader_module
*module
,
86 const char *entrypoint_name
,
87 gl_shader_stage stage
)
89 if (strcmp(entrypoint_name
, "main") != 0) {
90 anv_finishme("Multiple shaders per module not really supported");
93 const struct brw_compiler
*compiler
=
94 device
->instance
->physicalDevice
.compiler
;
95 const nir_shader_compiler_options
*nir_options
=
96 compiler
->glsl_compiler_options
[stage
].NirOptions
;
100 /* Some things such as our meta clear/blit code will give us a NIR
101 * shader directly. In that case, we just ignore the SPIR-V entirely
102 * and just use the NIR shader */
104 nir
->options
= nir_options
;
106 uint32_t *spirv
= (uint32_t *) module
->data
;
107 assert(spirv
[0] == SPIR_V_MAGIC_NUMBER
);
108 assert(module
->size
% 4 == 0);
110 nir
= spirv_to_nir(spirv
, module
->size
/ 4, stage
, nir_options
);
112 nir_validate_shader(nir
);
114 /* Vulkan uses the separate-shader linking model */
115 nir
->info
.separate_shader
= true;
117 /* Make sure the provided shader has exactly one entrypoint and that the
118 * name matches the name that came in from the VkShader.
120 nir_function_impl
*entrypoint
= NULL
;
121 nir_foreach_overload(nir
, overload
) {
122 if (strcmp(entrypoint_name
, overload
->function
->name
) == 0 &&
124 assert(entrypoint
== NULL
);
125 entrypoint
= overload
->impl
;
128 assert(entrypoint
!= NULL
);
130 nir
= brw_preprocess_nir(nir
, compiler
->scalar_stage
[stage
]);
132 nir_shader_gather_info(nir
, entrypoint
);
137 VkResult
anv_CreatePipelineCache(
139 const VkPipelineCacheCreateInfo
* pCreateInfo
,
140 const VkAllocationCallbacks
* pAllocator
,
141 VkPipelineCache
* pPipelineCache
)
143 *pPipelineCache
= (VkPipelineCache
)1;
145 stub_return(VK_SUCCESS
);
148 void anv_DestroyPipelineCache(
150 VkPipelineCache _cache
,
151 const VkAllocationCallbacks
* pAllocator
)
155 VkResult
anv_GetPipelineCacheData(
157 VkPipelineCache pipelineCache
,
162 stub_return(VK_SUCCESS
);
165 VkResult
anv_MergePipelineCaches(
167 VkPipelineCache destCache
,
168 uint32_t srcCacheCount
,
169 const VkPipelineCache
* pSrcCaches
)
171 stub_return(VK_SUCCESS
);
174 void anv_DestroyPipeline(
176 VkPipeline _pipeline
,
177 const VkAllocationCallbacks
* pAllocator
)
179 ANV_FROM_HANDLE(anv_device
, device
, _device
);
180 ANV_FROM_HANDLE(anv_pipeline
, pipeline
, _pipeline
);
182 anv_reloc_list_finish(&pipeline
->batch_relocs
,
183 pAllocator
? pAllocator
: &device
->alloc
);
184 anv_state_stream_finish(&pipeline
->program_stream
);
185 if (pipeline
->blend_state
.map
)
186 anv_state_pool_free(&device
->dynamic_state_pool
, pipeline
->blend_state
);
187 anv_free2(&device
->alloc
, pAllocator
, pipeline
);
190 static const uint32_t vk_to_gen_primitive_type
[] = {
191 [VK_PRIMITIVE_TOPOLOGY_POINT_LIST
] = _3DPRIM_POINTLIST
,
192 [VK_PRIMITIVE_TOPOLOGY_LINE_LIST
] = _3DPRIM_LINELIST
,
193 [VK_PRIMITIVE_TOPOLOGY_LINE_STRIP
] = _3DPRIM_LINESTRIP
,
194 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST
] = _3DPRIM_TRILIST
,
195 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP
] = _3DPRIM_TRISTRIP
,
196 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN
] = _3DPRIM_TRIFAN
,
197 [VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY
] = _3DPRIM_LINELIST_ADJ
,
198 [VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY
] = _3DPRIM_LINESTRIP_ADJ
,
199 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY
] = _3DPRIM_TRILIST_ADJ
,
200 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY
] = _3DPRIM_TRISTRIP_ADJ
,
201 /* [VK_PRIMITIVE_TOPOLOGY_PATCH_LIST] = _3DPRIM_PATCHLIST_1 */
205 populate_sampler_prog_key(const struct brw_device_info
*devinfo
,
206 struct brw_sampler_prog_key_data
*key
)
208 /* XXX: Handle texture swizzle on HSW- */
209 for (int i
= 0; i
< MAX_SAMPLERS
; i
++) {
210 /* Assume color sampler, no swizzling. (Works for BDW+) */
211 key
->swizzles
[i
] = SWIZZLE_XYZW
;
216 populate_vs_prog_key(const struct brw_device_info
*devinfo
,
217 struct brw_vs_prog_key
*key
)
219 memset(key
, 0, sizeof(*key
));
221 populate_sampler_prog_key(devinfo
, &key
->tex
);
223 /* XXX: Handle vertex input work-arounds */
225 /* XXX: Handle sampler_prog_key */
229 populate_gs_prog_key(const struct brw_device_info
*devinfo
,
230 struct brw_gs_prog_key
*key
)
232 memset(key
, 0, sizeof(*key
));
234 populate_sampler_prog_key(devinfo
, &key
->tex
);
238 populate_wm_prog_key(const struct brw_device_info
*devinfo
,
239 const VkGraphicsPipelineCreateInfo
*info
,
240 struct brw_wm_prog_key
*key
)
242 ANV_FROM_HANDLE(anv_render_pass
, render_pass
, info
->renderPass
);
244 memset(key
, 0, sizeof(*key
));
246 populate_sampler_prog_key(devinfo
, &key
->tex
);
248 /* TODO: Fill out key->input_slots_valid */
250 /* Vulkan doesn't specify a default */
251 key
->high_quality_derivatives
= false;
253 /* XXX Vulkan doesn't appear to specify */
254 key
->clamp_fragment_color
= false;
256 /* Vulkan always specifies upper-left coordinates */
257 key
->drawable_height
= 0;
258 key
->render_to_fbo
= false;
260 key
->nr_color_regions
= render_pass
->subpasses
[info
->subpass
].color_count
;
262 key
->replicate_alpha
= key
->nr_color_regions
> 1 &&
263 info
->pMultisampleState
&&
264 info
->pMultisampleState
->alphaToCoverageEnable
;
266 if (info
->pMultisampleState
&& info
->pMultisampleState
->rasterizationSamples
> 1) {
267 /* We should probably pull this out of the shader, but it's fairly
268 * harmless to compute it and then let dead-code take care of it.
270 key
->persample_shading
= info
->pMultisampleState
->sampleShadingEnable
;
271 if (key
->persample_shading
)
272 key
->persample_2x
= info
->pMultisampleState
->rasterizationSamples
== 2;
274 key
->compute_pos_offset
= info
->pMultisampleState
->sampleShadingEnable
;
275 key
->compute_sample_id
= info
->pMultisampleState
->sampleShadingEnable
;
280 populate_cs_prog_key(const struct brw_device_info
*devinfo
,
281 struct brw_cs_prog_key
*key
)
283 memset(key
, 0, sizeof(*key
));
285 populate_sampler_prog_key(devinfo
, &key
->tex
);
289 anv_pipeline_compile(struct anv_pipeline
*pipeline
,
290 struct anv_shader_module
*module
,
291 const char *entrypoint
,
292 gl_shader_stage stage
,
293 struct brw_stage_prog_data
*prog_data
)
295 const struct brw_compiler
*compiler
=
296 pipeline
->device
->instance
->physicalDevice
.compiler
;
298 nir_shader
*nir
= anv_shader_compile_to_nir(pipeline
->device
,
299 module
, entrypoint
, stage
);
303 anv_nir_lower_push_constants(nir
, compiler
->scalar_stage
[stage
]);
305 /* Figure out the number of parameters */
306 prog_data
->nr_params
= 0;
308 if (nir
->num_uniforms
> 0) {
309 /* If the shader uses any push constants at all, we'll just give
310 * them the maximum possible number
312 prog_data
->nr_params
+= MAX_PUSH_CONSTANTS_SIZE
/ sizeof(float);
315 if (pipeline
->layout
&& pipeline
->layout
->stage
[stage
].has_dynamic_offsets
)
316 prog_data
->nr_params
+= MAX_DYNAMIC_BUFFERS
* 2;
318 if (prog_data
->nr_params
> 0) {
319 /* XXX: I think we're leaking this */
320 prog_data
->param
= (const gl_constant_value
**)
321 malloc(prog_data
->nr_params
* sizeof(gl_constant_value
*));
323 /* We now set the param values to be offsets into a
324 * anv_push_constant_data structure. Since the compiler doesn't
325 * actually dereference any of the gl_constant_value pointers in the
326 * params array, it doesn't really matter what we put here.
328 struct anv_push_constants
*null_data
= NULL
;
329 if (nir
->num_uniforms
> 0) {
330 /* Fill out the push constants section of the param array */
331 for (unsigned i
= 0; i
< MAX_PUSH_CONSTANTS_SIZE
/ sizeof(float); i
++)
332 prog_data
->param
[i
] = (const gl_constant_value
*)
333 &null_data
->client_data
[i
* sizeof(float)];
337 /* Set up dynamic offsets */
338 anv_nir_apply_dynamic_offsets(pipeline
, nir
, prog_data
);
340 /* Apply the actual pipeline layout to UBOs, SSBOs, and textures */
341 anv_nir_apply_pipeline_layout(nir
, pipeline
->layout
);
343 /* All binding table offsets provided by apply_pipeline_layout() are
344 * relative to the start of the bindint table (plus MAX_RTS for VS).
346 unsigned bias
= stage
== MESA_SHADER_FRAGMENT
? MAX_RTS
: 0;
347 prog_data
->binding_table
.size_bytes
= 0;
348 prog_data
->binding_table
.texture_start
= bias
;
349 prog_data
->binding_table
.ubo_start
= bias
;
350 prog_data
->binding_table
.ssbo_start
= bias
;
351 prog_data
->binding_table
.image_start
= bias
;
353 /* Finish the optimization and compilation process */
354 nir
= brw_lower_nir(nir
, &pipeline
->device
->info
, NULL
,
355 compiler
->scalar_stage
[stage
]);
357 /* nir_lower_io will only handle the push constants; we need to set this
358 * to the full number of possible uniforms.
360 nir
->num_uniforms
= prog_data
->nr_params
;
366 anv_pipeline_upload_kernel(struct anv_pipeline
*pipeline
,
367 const void *data
, size_t size
)
369 struct anv_state state
=
370 anv_state_stream_alloc(&pipeline
->program_stream
, size
, 64);
372 assert(size
< pipeline
->program_stream
.block_pool
->block_size
);
374 memcpy(state
.map
, data
, size
);
376 if (!pipeline
->device
->info
.has_llc
)
377 anv_state_clflush(state
);
383 anv_pipeline_add_compiled_stage(struct anv_pipeline
*pipeline
,
384 gl_shader_stage stage
,
385 struct brw_stage_prog_data
*prog_data
)
387 struct brw_device_info
*devinfo
= &pipeline
->device
->info
;
388 uint32_t max_threads
[] = {
389 [MESA_SHADER_VERTEX
] = devinfo
->max_vs_threads
,
390 [MESA_SHADER_TESS_CTRL
] = 0,
391 [MESA_SHADER_TESS_EVAL
] = 0,
392 [MESA_SHADER_GEOMETRY
] = devinfo
->max_gs_threads
,
393 [MESA_SHADER_FRAGMENT
] = devinfo
->max_wm_threads
,
394 [MESA_SHADER_COMPUTE
] = devinfo
->max_cs_threads
,
397 pipeline
->prog_data
[stage
] = prog_data
;
398 pipeline
->active_stages
|= mesa_to_vk_shader_stage(stage
);
399 pipeline
->scratch_start
[stage
] = pipeline
->total_scratch
;
400 pipeline
->total_scratch
=
401 align_u32(pipeline
->total_scratch
, 1024) +
402 prog_data
->total_scratch
* max_threads
[stage
];
406 anv_pipeline_compile_vs(struct anv_pipeline
*pipeline
,
407 const VkGraphicsPipelineCreateInfo
*info
,
408 struct anv_shader_module
*module
,
409 const char *entrypoint
)
411 const struct brw_compiler
*compiler
=
412 pipeline
->device
->instance
->physicalDevice
.compiler
;
413 struct brw_vs_prog_data
*prog_data
= &pipeline
->vs_prog_data
;
414 struct brw_vs_prog_key key
;
416 populate_vs_prog_key(&pipeline
->device
->info
, &key
);
418 /* TODO: Look up shader in cache */
420 memset(prog_data
, 0, sizeof(*prog_data
));
422 nir_shader
*nir
= anv_pipeline_compile(pipeline
, module
, entrypoint
,
424 &prog_data
->base
.base
);
426 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
428 void *mem_ctx
= ralloc_context(NULL
);
430 if (module
->nir
== NULL
)
431 ralloc_steal(mem_ctx
, nir
);
433 prog_data
->inputs_read
= nir
->info
.inputs_read
;
434 pipeline
->writes_point_size
= nir
->info
.outputs_written
& VARYING_SLOT_PSIZ
;
436 brw_compute_vue_map(&pipeline
->device
->info
,
437 &prog_data
->base
.vue_map
,
438 nir
->info
.outputs_written
,
439 nir
->info
.separate_shader
);
442 const unsigned *shader_code
=
443 brw_compile_vs(compiler
, NULL
, mem_ctx
, &key
, prog_data
, nir
,
444 NULL
, false, -1, &code_size
, NULL
);
445 if (shader_code
== NULL
) {
446 ralloc_free(mem_ctx
);
447 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
450 const uint32_t offset
=
451 anv_pipeline_upload_kernel(pipeline
, shader_code
, code_size
);
452 if (prog_data
->base
.dispatch_mode
== DISPATCH_MODE_SIMD8
) {
453 pipeline
->vs_simd8
= offset
;
454 pipeline
->vs_vec4
= NO_KERNEL
;
456 pipeline
->vs_simd8
= NO_KERNEL
;
457 pipeline
->vs_vec4
= offset
;
460 ralloc_free(mem_ctx
);
462 anv_pipeline_add_compiled_stage(pipeline
, MESA_SHADER_VERTEX
,
463 &prog_data
->base
.base
);
469 anv_pipeline_compile_gs(struct anv_pipeline
*pipeline
,
470 const VkGraphicsPipelineCreateInfo
*info
,
471 struct anv_shader_module
*module
,
472 const char *entrypoint
)
474 const struct brw_compiler
*compiler
=
475 pipeline
->device
->instance
->physicalDevice
.compiler
;
476 struct brw_gs_prog_data
*prog_data
= &pipeline
->gs_prog_data
;
477 struct brw_gs_prog_key key
;
479 populate_gs_prog_key(&pipeline
->device
->info
, &key
);
481 /* TODO: Look up shader in cache */
483 memset(prog_data
, 0, sizeof(*prog_data
));
485 nir_shader
*nir
= anv_pipeline_compile(pipeline
, module
, entrypoint
,
486 MESA_SHADER_GEOMETRY
,
487 &prog_data
->base
.base
);
489 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
491 void *mem_ctx
= ralloc_context(NULL
);
493 if (module
->nir
== NULL
)
494 ralloc_steal(mem_ctx
, nir
);
496 brw_compute_vue_map(&pipeline
->device
->info
,
497 &prog_data
->base
.vue_map
,
498 nir
->info
.outputs_written
,
499 nir
->info
.separate_shader
);
502 const unsigned *shader_code
=
503 brw_compile_gs(compiler
, NULL
, mem_ctx
, &key
, prog_data
, nir
,
504 NULL
, -1, &code_size
, NULL
);
505 if (shader_code
== NULL
) {
506 ralloc_free(mem_ctx
);
507 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
512 anv_pipeline_upload_kernel(pipeline
, shader_code
, code_size
);
513 pipeline
->gs_vertex_count
= nir
->info
.gs
.vertices_in
;
515 ralloc_free(mem_ctx
);
517 anv_pipeline_add_compiled_stage(pipeline
, MESA_SHADER_GEOMETRY
,
518 &prog_data
->base
.base
);
524 anv_pipeline_compile_fs(struct anv_pipeline
*pipeline
,
525 const VkGraphicsPipelineCreateInfo
*info
,
526 struct anv_shader_module
*module
,
527 const char *entrypoint
)
529 const struct brw_compiler
*compiler
=
530 pipeline
->device
->instance
->physicalDevice
.compiler
;
531 struct brw_wm_prog_data
*prog_data
= &pipeline
->wm_prog_data
;
532 struct brw_wm_prog_key key
;
534 populate_wm_prog_key(&pipeline
->device
->info
, info
, &key
);
536 if (pipeline
->use_repclear
)
537 key
.nr_color_regions
= 1;
539 /* TODO: Look up shader in cache */
541 memset(prog_data
, 0, sizeof(*prog_data
));
543 prog_data
->binding_table
.render_target_start
= 0;
545 nir_shader
*nir
= anv_pipeline_compile(pipeline
, module
, entrypoint
,
546 MESA_SHADER_FRAGMENT
,
549 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
551 void *mem_ctx
= ralloc_context(NULL
);
553 if (module
->nir
== NULL
)
554 ralloc_steal(mem_ctx
, nir
);
557 const unsigned *shader_code
=
558 brw_compile_fs(compiler
, NULL
, mem_ctx
, &key
, prog_data
, nir
,
559 NULL
, -1, -1, pipeline
->use_repclear
, &code_size
, NULL
);
560 if (shader_code
== NULL
) {
561 ralloc_free(mem_ctx
);
562 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
565 uint32_t offset
= anv_pipeline_upload_kernel(pipeline
,
566 shader_code
, code_size
);
568 pipeline
->ps_simd8
= NO_KERNEL
;
570 pipeline
->ps_simd8
= offset
;
572 if (prog_data
->no_8
|| prog_data
->prog_offset_16
) {
573 pipeline
->ps_simd16
= offset
+ prog_data
->prog_offset_16
;
575 pipeline
->ps_simd16
= NO_KERNEL
;
578 pipeline
->ps_ksp2
= 0;
579 pipeline
->ps_grf_start2
= 0;
580 if (pipeline
->ps_simd8
!= NO_KERNEL
) {
581 pipeline
->ps_ksp0
= pipeline
->ps_simd8
;
582 pipeline
->ps_grf_start0
= prog_data
->base
.dispatch_grf_start_reg
;
583 if (pipeline
->ps_simd16
!= NO_KERNEL
) {
584 pipeline
->ps_ksp2
= pipeline
->ps_simd16
;
585 pipeline
->ps_grf_start2
= prog_data
->dispatch_grf_start_reg_16
;
587 } else if (pipeline
->ps_simd16
!= NO_KERNEL
) {
588 pipeline
->ps_ksp0
= pipeline
->ps_simd16
;
589 pipeline
->ps_grf_start0
= prog_data
->dispatch_grf_start_reg_16
;
592 ralloc_free(mem_ctx
);
594 anv_pipeline_add_compiled_stage(pipeline
, MESA_SHADER_FRAGMENT
,
601 anv_pipeline_compile_cs(struct anv_pipeline
*pipeline
,
602 const VkComputePipelineCreateInfo
*info
,
603 struct anv_shader_module
*module
,
604 const char *entrypoint
)
606 const struct brw_compiler
*compiler
=
607 pipeline
->device
->instance
->physicalDevice
.compiler
;
608 struct brw_cs_prog_data
*prog_data
= &pipeline
->cs_prog_data
;
609 struct brw_cs_prog_key key
;
611 populate_cs_prog_key(&pipeline
->device
->info
, &key
);
613 /* TODO: Look up shader in cache */
615 memset(prog_data
, 0, sizeof(*prog_data
));
617 nir_shader
*nir
= anv_pipeline_compile(pipeline
, module
, entrypoint
,
621 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
623 void *mem_ctx
= ralloc_context(NULL
);
625 if (module
->nir
== NULL
)
626 ralloc_steal(mem_ctx
, nir
);
629 const unsigned *shader_code
=
630 brw_compile_cs(compiler
, NULL
, mem_ctx
, &key
, prog_data
, nir
,
631 -1, &code_size
, NULL
);
632 if (shader_code
== NULL
) {
633 ralloc_free(mem_ctx
);
634 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
637 pipeline
->cs_simd
= anv_pipeline_upload_kernel(pipeline
,
638 shader_code
, code_size
);
639 ralloc_free(mem_ctx
);
641 anv_pipeline_add_compiled_stage(pipeline
, MESA_SHADER_COMPUTE
,
647 static const int gen8_push_size
= 32 * 1024;
650 gen7_compute_urb_partition(struct anv_pipeline
*pipeline
)
652 const struct brw_device_info
*devinfo
= &pipeline
->device
->info
;
653 bool vs_present
= pipeline
->active_stages
& VK_SHADER_STAGE_VERTEX_BIT
;
654 unsigned vs_size
= vs_present
? pipeline
->vs_prog_data
.base
.urb_entry_size
: 1;
655 unsigned vs_entry_size_bytes
= vs_size
* 64;
656 bool gs_present
= pipeline
->active_stages
& VK_SHADER_STAGE_GEOMETRY_BIT
;
657 unsigned gs_size
= gs_present
? pipeline
->gs_prog_data
.base
.urb_entry_size
: 1;
658 unsigned gs_entry_size_bytes
= gs_size
* 64;
660 /* From p35 of the Ivy Bridge PRM (section 1.7.1: 3DSTATE_URB_GS):
662 * VS Number of URB Entries must be divisible by 8 if the VS URB Entry
663 * Allocation Size is less than 9 512-bit URB entries.
665 * Similar text exists for GS.
667 unsigned vs_granularity
= (vs_size
< 9) ? 8 : 1;
668 unsigned gs_granularity
= (gs_size
< 9) ? 8 : 1;
670 /* URB allocations must be done in 8k chunks. */
671 unsigned chunk_size_bytes
= 8192;
673 /* Determine the size of the URB in chunks. */
674 unsigned urb_chunks
= devinfo
->urb
.size
* 1024 / chunk_size_bytes
;
676 /* Reserve space for push constants */
677 unsigned push_constant_bytes
= gen8_push_size
;
678 unsigned push_constant_chunks
=
679 push_constant_bytes
/ chunk_size_bytes
;
681 /* Initially, assign each stage the minimum amount of URB space it needs,
682 * and make a note of how much additional space it "wants" (the amount of
683 * additional space it could actually make use of).
686 /* VS has a lower limit on the number of URB entries */
688 ALIGN(devinfo
->urb
.min_vs_entries
* vs_entry_size_bytes
,
689 chunk_size_bytes
) / chunk_size_bytes
;
691 ALIGN(devinfo
->urb
.max_vs_entries
* vs_entry_size_bytes
,
692 chunk_size_bytes
) / chunk_size_bytes
- vs_chunks
;
694 unsigned gs_chunks
= 0;
695 unsigned gs_wants
= 0;
697 /* There are two constraints on the minimum amount of URB space we can
700 * (1) We need room for at least 2 URB entries, since we always operate
701 * the GS in DUAL_OBJECT mode.
703 * (2) We can't allocate less than nr_gs_entries_granularity.
705 gs_chunks
= ALIGN(MAX2(gs_granularity
, 2) * gs_entry_size_bytes
,
706 chunk_size_bytes
) / chunk_size_bytes
;
708 ALIGN(devinfo
->urb
.max_gs_entries
* gs_entry_size_bytes
,
709 chunk_size_bytes
) / chunk_size_bytes
- gs_chunks
;
712 /* There should always be enough URB space to satisfy the minimum
713 * requirements of each stage.
715 unsigned total_needs
= push_constant_chunks
+ vs_chunks
+ gs_chunks
;
716 assert(total_needs
<= urb_chunks
);
718 /* Mete out remaining space (if any) in proportion to "wants". */
719 unsigned total_wants
= vs_wants
+ gs_wants
;
720 unsigned remaining_space
= urb_chunks
- total_needs
;
721 if (remaining_space
> total_wants
)
722 remaining_space
= total_wants
;
723 if (remaining_space
> 0) {
724 unsigned vs_additional
= (unsigned)
725 round(vs_wants
* (((double) remaining_space
) / total_wants
));
726 vs_chunks
+= vs_additional
;
727 remaining_space
-= vs_additional
;
728 gs_chunks
+= remaining_space
;
731 /* Sanity check that we haven't over-allocated. */
732 assert(push_constant_chunks
+ vs_chunks
+ gs_chunks
<= urb_chunks
);
734 /* Finally, compute the number of entries that can fit in the space
735 * allocated to each stage.
737 unsigned nr_vs_entries
= vs_chunks
* chunk_size_bytes
/ vs_entry_size_bytes
;
738 unsigned nr_gs_entries
= gs_chunks
* chunk_size_bytes
/ gs_entry_size_bytes
;
740 /* Since we rounded up when computing *_wants, this may be slightly more
741 * than the maximum allowed amount, so correct for that.
743 nr_vs_entries
= MIN2(nr_vs_entries
, devinfo
->urb
.max_vs_entries
);
744 nr_gs_entries
= MIN2(nr_gs_entries
, devinfo
->urb
.max_gs_entries
);
746 /* Ensure that we program a multiple of the granularity. */
747 nr_vs_entries
= ROUND_DOWN_TO(nr_vs_entries
, vs_granularity
);
748 nr_gs_entries
= ROUND_DOWN_TO(nr_gs_entries
, gs_granularity
);
750 /* Finally, sanity check to make sure we have at least the minimum number
751 * of entries needed for each stage.
753 assert(nr_vs_entries
>= devinfo
->urb
.min_vs_entries
);
755 assert(nr_gs_entries
>= 2);
757 /* Lay out the URB in the following order:
762 pipeline
->urb
.vs_start
= push_constant_chunks
;
763 pipeline
->urb
.vs_size
= vs_size
;
764 pipeline
->urb
.nr_vs_entries
= nr_vs_entries
;
766 pipeline
->urb
.gs_start
= push_constant_chunks
+ vs_chunks
;
767 pipeline
->urb
.gs_size
= gs_size
;
768 pipeline
->urb
.nr_gs_entries
= nr_gs_entries
;
772 anv_pipeline_init_dynamic_state(struct anv_pipeline
*pipeline
,
773 const VkGraphicsPipelineCreateInfo
*pCreateInfo
)
775 anv_cmd_dirty_mask_t states
= ANV_CMD_DIRTY_DYNAMIC_ALL
;
776 ANV_FROM_HANDLE(anv_render_pass
, pass
, pCreateInfo
->renderPass
);
777 struct anv_subpass
*subpass
= &pass
->subpasses
[pCreateInfo
->subpass
];
779 pipeline
->dynamic_state
= default_dynamic_state
;
781 if (pCreateInfo
->pDynamicState
) {
782 /* Remove all of the states that are marked as dynamic */
783 uint32_t count
= pCreateInfo
->pDynamicState
->dynamicStateCount
;
784 for (uint32_t s
= 0; s
< count
; s
++)
785 states
&= ~(1 << pCreateInfo
->pDynamicState
->pDynamicStates
[s
]);
788 struct anv_dynamic_state
*dynamic
= &pipeline
->dynamic_state
;
790 dynamic
->viewport
.count
= pCreateInfo
->pViewportState
->viewportCount
;
791 if (states
& (1 << VK_DYNAMIC_STATE_VIEWPORT
)) {
792 typed_memcpy(dynamic
->viewport
.viewports
,
793 pCreateInfo
->pViewportState
->pViewports
,
794 pCreateInfo
->pViewportState
->viewportCount
);
797 dynamic
->scissor
.count
= pCreateInfo
->pViewportState
->scissorCount
;
798 if (states
& (1 << VK_DYNAMIC_STATE_SCISSOR
)) {
799 typed_memcpy(dynamic
->scissor
.scissors
,
800 pCreateInfo
->pViewportState
->pScissors
,
801 pCreateInfo
->pViewportState
->scissorCount
);
804 if (states
& (1 << VK_DYNAMIC_STATE_LINE_WIDTH
)) {
805 assert(pCreateInfo
->pRasterizationState
);
806 dynamic
->line_width
= pCreateInfo
->pRasterizationState
->lineWidth
;
809 if (states
& (1 << VK_DYNAMIC_STATE_DEPTH_BIAS
)) {
810 assert(pCreateInfo
->pRasterizationState
);
811 dynamic
->depth_bias
.bias
=
812 pCreateInfo
->pRasterizationState
->depthBiasConstantFactor
;
813 dynamic
->depth_bias
.clamp
=
814 pCreateInfo
->pRasterizationState
->depthBiasClamp
;
815 dynamic
->depth_bias
.slope
=
816 pCreateInfo
->pRasterizationState
->depthBiasSlopeFactor
;
819 if (states
& (1 << VK_DYNAMIC_STATE_BLEND_CONSTANTS
)) {
820 assert(pCreateInfo
->pColorBlendState
);
821 typed_memcpy(dynamic
->blend_constants
,
822 pCreateInfo
->pColorBlendState
->blendConstants
, 4);
825 /* If there is no depthstencil attachment, then don't read
826 * pDepthStencilState. The Vulkan spec states that pDepthStencilState may
827 * be NULL in this case. Even if pDepthStencilState is non-NULL, there is
828 * no need to override the depthstencil defaults in
829 * anv_pipeline::dynamic_state when there is no depthstencil attachment.
831 * From the Vulkan spec (20 Oct 2015, git-aa308cb):
833 * pDepthStencilState [...] may only be NULL if renderPass and subpass
834 * specify a subpass that has no depth/stencil attachment.
836 if (subpass
->depth_stencil_attachment
!= VK_ATTACHMENT_UNUSED
) {
837 if (states
& (1 << VK_DYNAMIC_STATE_DEPTH_BOUNDS
)) {
838 assert(pCreateInfo
->pDepthStencilState
);
839 dynamic
->depth_bounds
.min
=
840 pCreateInfo
->pDepthStencilState
->minDepthBounds
;
841 dynamic
->depth_bounds
.max
=
842 pCreateInfo
->pDepthStencilState
->maxDepthBounds
;
845 if (states
& (1 << VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK
)) {
846 assert(pCreateInfo
->pDepthStencilState
);
847 dynamic
->stencil_compare_mask
.front
=
848 pCreateInfo
->pDepthStencilState
->front
.compareMask
;
849 dynamic
->stencil_compare_mask
.back
=
850 pCreateInfo
->pDepthStencilState
->back
.compareMask
;
853 if (states
& (1 << VK_DYNAMIC_STATE_STENCIL_WRITE_MASK
)) {
854 assert(pCreateInfo
->pDepthStencilState
);
855 dynamic
->stencil_write_mask
.front
=
856 pCreateInfo
->pDepthStencilState
->front
.writeMask
;
857 dynamic
->stencil_write_mask
.back
=
858 pCreateInfo
->pDepthStencilState
->back
.writeMask
;
861 if (states
& (1 << VK_DYNAMIC_STATE_STENCIL_REFERENCE
)) {
862 assert(pCreateInfo
->pDepthStencilState
);
863 dynamic
->stencil_reference
.front
=
864 pCreateInfo
->pDepthStencilState
->front
.reference
;
865 dynamic
->stencil_reference
.back
=
866 pCreateInfo
->pDepthStencilState
->back
.reference
;
870 pipeline
->dynamic_state_mask
= states
;
874 anv_pipeline_validate_create_info(const VkGraphicsPipelineCreateInfo
*info
)
876 struct anv_render_pass
*renderpass
= NULL
;
877 struct anv_subpass
*subpass
= NULL
;
879 /* Assert that all required members of VkGraphicsPipelineCreateInfo are
880 * present, as explained by the Vulkan (20 Oct 2015, git-aa308cb), Section
881 * 4.2 Graphics Pipeline.
883 assert(info
->sType
== VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO
);
885 renderpass
= anv_render_pass_from_handle(info
->renderPass
);
888 if (renderpass
!= &anv_meta_dummy_renderpass
) {
889 assert(info
->subpass
< renderpass
->subpass_count
);
890 subpass
= &renderpass
->subpasses
[info
->subpass
];
893 assert(info
->stageCount
>= 1);
894 assert(info
->pVertexInputState
);
895 assert(info
->pInputAssemblyState
);
896 assert(info
->pViewportState
);
897 assert(info
->pRasterizationState
);
898 assert(info
->pMultisampleState
);
900 if (subpass
&& subpass
->depth_stencil_attachment
!= VK_ATTACHMENT_UNUSED
)
901 assert(info
->pDepthStencilState
);
903 if (subpass
&& subpass
->color_count
> 0)
904 assert(info
->pColorBlendState
);
906 for (uint32_t i
= 0; i
< info
->stageCount
; ++i
) {
907 switch (info
->pStages
[i
].stage
) {
908 case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT
:
909 case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT
:
910 assert(info
->pTessellationState
);
919 anv_pipeline_init(struct anv_pipeline
*pipeline
, struct anv_device
*device
,
920 const VkGraphicsPipelineCreateInfo
*pCreateInfo
,
921 const struct anv_graphics_pipeline_create_info
*extra
,
922 const VkAllocationCallbacks
*alloc
)
925 anv_pipeline_validate_create_info(pCreateInfo
);
929 alloc
= &device
->alloc
;
931 pipeline
->device
= device
;
932 pipeline
->layout
= anv_pipeline_layout_from_handle(pCreateInfo
->layout
);
934 anv_reloc_list_init(&pipeline
->batch_relocs
, alloc
);
935 /* TODO: Handle allocation fail */
937 pipeline
->batch
.alloc
= alloc
;
938 pipeline
->batch
.next
= pipeline
->batch
.start
= pipeline
->batch_data
;
939 pipeline
->batch
.end
= pipeline
->batch
.start
+ sizeof(pipeline
->batch_data
);
940 pipeline
->batch
.relocs
= &pipeline
->batch_relocs
;
942 anv_state_stream_init(&pipeline
->program_stream
,
943 &device
->instruction_block_pool
);
945 anv_pipeline_init_dynamic_state(pipeline
, pCreateInfo
);
947 if (pCreateInfo
->pTessellationState
)
948 anv_finishme("VK_STRUCTURE_TYPE_PIPELINE_TESSELLATION_STATE_CREATE_INFO");
949 if (pCreateInfo
->pMultisampleState
&&
950 pCreateInfo
->pMultisampleState
->rasterizationSamples
> 1)
951 anv_finishme("VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO");
953 pipeline
->use_repclear
= extra
&& extra
->use_repclear
;
954 pipeline
->writes_point_size
= false;
956 /* When we free the pipeline, we detect stages based on the NULL status
957 * of various prog_data pointers. Make them NULL by default.
959 memset(pipeline
->prog_data
, 0, sizeof(pipeline
->prog_data
));
960 memset(pipeline
->scratch_start
, 0, sizeof(pipeline
->scratch_start
));
962 pipeline
->vs_simd8
= NO_KERNEL
;
963 pipeline
->vs_vec4
= NO_KERNEL
;
964 pipeline
->gs_vec4
= NO_KERNEL
;
966 pipeline
->active_stages
= 0;
967 pipeline
->total_scratch
= 0;
969 for (uint32_t i
= 0; i
< pCreateInfo
->stageCount
; i
++) {
970 ANV_FROM_HANDLE(anv_shader_module
, module
,
971 pCreateInfo
->pStages
[i
].module
);
972 const char *entrypoint
= pCreateInfo
->pStages
[i
].pName
;
974 switch (pCreateInfo
->pStages
[i
].stage
) {
975 case VK_SHADER_STAGE_VERTEX_BIT
:
976 anv_pipeline_compile_vs(pipeline
, pCreateInfo
, module
, entrypoint
);
978 case VK_SHADER_STAGE_GEOMETRY_BIT
:
979 anv_pipeline_compile_gs(pipeline
, pCreateInfo
, module
, entrypoint
);
981 case VK_SHADER_STAGE_FRAGMENT_BIT
:
982 anv_pipeline_compile_fs(pipeline
, pCreateInfo
, module
, entrypoint
);
985 anv_finishme("Unsupported shader stage");
989 if (!(pipeline
->active_stages
& VK_SHADER_STAGE_VERTEX_BIT
)) {
990 /* Vertex is only optional if disable_vs is set */
991 assert(extra
->disable_vs
);
992 memset(&pipeline
->vs_prog_data
, 0, sizeof(pipeline
->vs_prog_data
));
995 gen7_compute_urb_partition(pipeline
);
997 const VkPipelineVertexInputStateCreateInfo
*vi_info
=
998 pCreateInfo
->pVertexInputState
;
999 pipeline
->vb_used
= 0;
1000 for (uint32_t i
= 0; i
< vi_info
->vertexBindingDescriptionCount
; i
++) {
1001 const VkVertexInputBindingDescription
*desc
=
1002 &vi_info
->pVertexBindingDescriptions
[i
];
1004 pipeline
->vb_used
|= 1 << desc
->binding
;
1005 pipeline
->binding_stride
[desc
->binding
] = desc
->stride
;
1007 /* Step rate is programmed per vertex element (attribute), not
1008 * binding. Set up a map of which bindings step per instance, for
1009 * reference by vertex element setup. */
1010 switch (desc
->inputRate
) {
1012 case VK_VERTEX_INPUT_RATE_VERTEX
:
1013 pipeline
->instancing_enable
[desc
->binding
] = false;
1015 case VK_VERTEX_INPUT_RATE_INSTANCE
:
1016 pipeline
->instancing_enable
[desc
->binding
] = true;
1021 const VkPipelineInputAssemblyStateCreateInfo
*ia_info
=
1022 pCreateInfo
->pInputAssemblyState
;
1023 pipeline
->primitive_restart
= ia_info
->primitiveRestartEnable
;
1024 pipeline
->topology
= vk_to_gen_primitive_type
[ia_info
->topology
];
1026 if (extra
&& extra
->use_rectlist
)
1027 pipeline
->topology
= _3DPRIM_RECTLIST
;
1033 anv_graphics_pipeline_create(
1035 const VkGraphicsPipelineCreateInfo
*pCreateInfo
,
1036 const struct anv_graphics_pipeline_create_info
*extra
,
1037 const VkAllocationCallbacks
*pAllocator
,
1038 VkPipeline
*pPipeline
)
1040 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1042 switch (device
->info
.gen
) {
1044 if (device
->info
.is_haswell
)
1045 return gen75_graphics_pipeline_create(_device
, pCreateInfo
, extra
, pAllocator
, pPipeline
);
1047 return gen7_graphics_pipeline_create(_device
, pCreateInfo
, extra
, pAllocator
, pPipeline
);
1049 return gen8_graphics_pipeline_create(_device
, pCreateInfo
, extra
, pAllocator
, pPipeline
);
1051 return gen9_graphics_pipeline_create(_device
, pCreateInfo
, extra
, pAllocator
, pPipeline
);
1053 unreachable("unsupported gen\n");
1057 VkResult
anv_CreateGraphicsPipelines(
1059 VkPipelineCache pipelineCache
,
1061 const VkGraphicsPipelineCreateInfo
* pCreateInfos
,
1062 const VkAllocationCallbacks
* pAllocator
,
1063 VkPipeline
* pPipelines
)
1065 VkResult result
= VK_SUCCESS
;
1068 for (; i
< count
; i
++) {
1069 result
= anv_graphics_pipeline_create(_device
, &pCreateInfos
[i
],
1070 NULL
, pAllocator
, &pPipelines
[i
]);
1071 if (result
!= VK_SUCCESS
) {
1072 for (unsigned j
= 0; j
< i
; j
++) {
1073 anv_DestroyPipeline(_device
, pPipelines
[j
], pAllocator
);
1083 static VkResult
anv_compute_pipeline_create(
1085 const VkComputePipelineCreateInfo
* pCreateInfo
,
1086 const VkAllocationCallbacks
* pAllocator
,
1087 VkPipeline
* pPipeline
)
1089 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1091 switch (device
->info
.gen
) {
1093 if (device
->info
.is_haswell
)
1094 return gen75_compute_pipeline_create(_device
, pCreateInfo
, pAllocator
, pPipeline
);
1096 return gen7_compute_pipeline_create(_device
, pCreateInfo
, pAllocator
, pPipeline
);
1098 return gen8_compute_pipeline_create(_device
, pCreateInfo
, pAllocator
, pPipeline
);
1100 return gen9_compute_pipeline_create(_device
, pCreateInfo
, pAllocator
, pPipeline
);
1102 unreachable("unsupported gen\n");
1106 VkResult
anv_CreateComputePipelines(
1108 VkPipelineCache pipelineCache
,
1110 const VkComputePipelineCreateInfo
* pCreateInfos
,
1111 const VkAllocationCallbacks
* pAllocator
,
1112 VkPipeline
* pPipelines
)
1114 VkResult result
= VK_SUCCESS
;
1117 for (; i
< count
; i
++) {
1118 result
= anv_compute_pipeline_create(_device
, &pCreateInfos
[i
],
1119 pAllocator
, &pPipelines
[i
]);
1120 if (result
!= VK_SUCCESS
) {
1121 for (unsigned j
= 0; j
< i
; j
++) {
1122 anv_DestroyPipeline(_device
, pPipelines
[j
], pAllocator
);