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"
34 VkResult
anv_CreateShaderModule(
36 const VkShaderModuleCreateInfo
* pCreateInfo
,
37 VkShaderModule
* pShaderModule
)
39 ANV_FROM_HANDLE(anv_device
, device
, _device
);
40 struct anv_shader_module
*module
;
42 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO
);
43 assert(pCreateInfo
->flags
== 0);
45 module
= anv_device_alloc(device
, sizeof(*module
) + pCreateInfo
->codeSize
, 8,
46 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
48 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
51 module
->size
= pCreateInfo
->codeSize
;
52 memcpy(module
->data
, pCreateInfo
->pCode
, module
->size
);
54 *pShaderModule
= anv_shader_module_to_handle(module
);
59 void anv_DestroyShaderModule(
61 VkShaderModule _module
)
63 ANV_FROM_HANDLE(anv_device
, device
, _device
);
64 ANV_FROM_HANDLE(anv_shader_module
, module
, _module
);
66 anv_device_free(device
, module
);
69 VkResult
anv_CreateShader(
71 const VkShaderCreateInfo
* pCreateInfo
,
74 ANV_FROM_HANDLE(anv_device
, device
, _device
);
75 ANV_FROM_HANDLE(anv_shader_module
, module
, pCreateInfo
->module
);
76 struct anv_shader
*shader
;
78 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SHADER_CREATE_INFO
);
79 assert(pCreateInfo
->flags
== 0);
81 const char *name
= pCreateInfo
->pName
? pCreateInfo
->pName
: "main";
82 size_t name_len
= strlen(name
);
84 if (strcmp(name
, "main") != 0) {
85 anv_finishme("Multiple shaders per module not really supported");
88 shader
= anv_device_alloc(device
, sizeof(*shader
) + name_len
+ 1, 8,
89 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
91 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
93 shader
->module
= module
;
94 memcpy(shader
->entrypoint
, name
, name_len
+ 1);
96 *pShader
= anv_shader_to_handle(shader
);
101 void anv_DestroyShader(
105 ANV_FROM_HANDLE(anv_device
, device
, _device
);
106 ANV_FROM_HANDLE(anv_shader
, shader
, _shader
);
108 anv_device_free(device
, shader
);
112 VkResult
anv_CreatePipelineCache(
114 const VkPipelineCacheCreateInfo
* pCreateInfo
,
115 VkPipelineCache
* pPipelineCache
)
117 pPipelineCache
->handle
= 1;
119 stub_return(VK_SUCCESS
);
122 void anv_DestroyPipelineCache(
124 VkPipelineCache _cache
)
128 size_t anv_GetPipelineCacheSize(
130 VkPipelineCache pipelineCache
)
135 VkResult
anv_GetPipelineCacheData(
137 VkPipelineCache pipelineCache
,
140 stub_return(VK_UNSUPPORTED
);
143 VkResult
anv_MergePipelineCaches(
145 VkPipelineCache destCache
,
146 uint32_t srcCacheCount
,
147 const VkPipelineCache
* pSrcCaches
)
149 stub_return(VK_UNSUPPORTED
);
152 void anv_DestroyPipeline(
154 VkPipeline _pipeline
)
156 ANV_FROM_HANDLE(anv_device
, device
, _device
);
157 ANV_FROM_HANDLE(anv_pipeline
, pipeline
, _pipeline
);
159 anv_compiler_free(pipeline
);
160 anv_reloc_list_finish(&pipeline
->batch_relocs
, pipeline
->device
);
161 anv_state_stream_finish(&pipeline
->program_stream
);
162 anv_state_pool_free(&device
->dynamic_state_pool
, pipeline
->blend_state
);
163 anv_device_free(pipeline
->device
, pipeline
);
166 static const uint32_t vk_to_gen_primitive_type
[] = {
167 [VK_PRIMITIVE_TOPOLOGY_POINT_LIST
] = _3DPRIM_POINTLIST
,
168 [VK_PRIMITIVE_TOPOLOGY_LINE_LIST
] = _3DPRIM_LINELIST
,
169 [VK_PRIMITIVE_TOPOLOGY_LINE_STRIP
] = _3DPRIM_LINESTRIP
,
170 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST
] = _3DPRIM_TRILIST
,
171 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP
] = _3DPRIM_TRISTRIP
,
172 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN
] = _3DPRIM_TRIFAN
,
173 [VK_PRIMITIVE_TOPOLOGY_LINE_LIST_ADJ
] = _3DPRIM_LINELIST_ADJ
,
174 [VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_ADJ
] = _3DPRIM_LINESTRIP_ADJ
,
175 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_ADJ
] = _3DPRIM_TRILIST_ADJ
,
176 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_ADJ
] = _3DPRIM_TRISTRIP_ADJ
,
177 [VK_PRIMITIVE_TOPOLOGY_PATCH
] = _3DPRIM_PATCHLIST_1
181 anv_pipeline_init_dynamic_state(struct anv_pipeline
*pipeline
,
182 const VkGraphicsPipelineCreateInfo
*pCreateInfo
)
184 uint32_t states
= ANV_DYNAMIC_STATE_DIRTY_MASK
;
186 if (pCreateInfo
->pDynamicState
) {
187 /* Remove all of the states that are marked as dynamic */
188 uint32_t count
= pCreateInfo
->pDynamicState
->dynamicStateCount
;
189 for (uint32_t s
= 0; s
< count
; s
++)
190 states
&= ~(1 << pCreateInfo
->pDynamicState
->pDynamicStates
[s
]);
193 struct anv_dynamic_state
*dynamic
= &pipeline
->dynamic_state
;
195 dynamic
->viewport
.count
= pCreateInfo
->pViewportState
->viewportCount
;
196 if (states
& (1 << VK_DYNAMIC_STATE_VIEWPORT
)) {
197 typed_memcpy(dynamic
->viewport
.viewports
,
198 pCreateInfo
->pViewportState
->pViewports
,
199 pCreateInfo
->pViewportState
->viewportCount
);
202 dynamic
->scissor
.count
= pCreateInfo
->pViewportState
->scissorCount
;
203 if (states
& (1 << VK_DYNAMIC_STATE_SCISSOR
)) {
204 typed_memcpy(dynamic
->scissor
.scissors
,
205 pCreateInfo
->pViewportState
->pScissors
,
206 pCreateInfo
->pViewportState
->scissorCount
);
209 if (states
& (1 << VK_DYNAMIC_STATE_LINE_WIDTH
)) {
210 assert(pCreateInfo
->pRasterState
);
211 dynamic
->line_width
= pCreateInfo
->pRasterState
->lineWidth
;
214 if (states
& (1 << VK_DYNAMIC_STATE_DEPTH_BIAS
)) {
215 assert(pCreateInfo
->pRasterState
);
216 dynamic
->depth_bias
.bias
= pCreateInfo
->pRasterState
->depthBias
;
217 dynamic
->depth_bias
.clamp
= pCreateInfo
->pRasterState
->depthBiasClamp
;
218 dynamic
->depth_bias
.slope_scaled
=
219 pCreateInfo
->pRasterState
->slopeScaledDepthBias
;
222 if (states
& (1 << VK_DYNAMIC_STATE_BLEND_CONSTANTS
)) {
223 assert(pCreateInfo
->pColorBlendState
);
224 typed_memcpy(dynamic
->blend_constants
,
225 pCreateInfo
->pColorBlendState
->blendConst
, 4);
228 if (states
& (1 << VK_DYNAMIC_STATE_DEPTH_BOUNDS
)) {
229 assert(pCreateInfo
->pDepthStencilState
);
230 dynamic
->depth_bounds
.min
=
231 pCreateInfo
->pDepthStencilState
->minDepthBounds
;
232 dynamic
->depth_bounds
.max
=
233 pCreateInfo
->pDepthStencilState
->maxDepthBounds
;
236 if (states
& (1 << VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK
)) {
237 assert(pCreateInfo
->pDepthStencilState
);
238 dynamic
->stencil_compare_mask
.front
=
239 pCreateInfo
->pDepthStencilState
->front
.stencilCompareMask
;
240 dynamic
->stencil_compare_mask
.back
=
241 pCreateInfo
->pDepthStencilState
->back
.stencilCompareMask
;
244 if (states
& (1 << VK_DYNAMIC_STATE_STENCIL_WRITE_MASK
)) {
245 assert(pCreateInfo
->pDepthStencilState
);
246 dynamic
->stencil_write_mask
.front
=
247 pCreateInfo
->pDepthStencilState
->front
.stencilWriteMask
;
248 dynamic
->stencil_write_mask
.back
=
249 pCreateInfo
->pDepthStencilState
->back
.stencilWriteMask
;
252 if (states
& (1 << VK_DYNAMIC_STATE_STENCIL_REFERENCE
)) {
253 assert(pCreateInfo
->pDepthStencilState
);
254 dynamic
->stencil_reference
.front
=
255 pCreateInfo
->pDepthStencilState
->front
.stencilReference
;
256 dynamic
->stencil_reference
.back
=
257 pCreateInfo
->pDepthStencilState
->back
.stencilReference
;
260 pipeline
->dynamic_state_mask
= states
;
264 anv_pipeline_init(struct anv_pipeline
*pipeline
, struct anv_device
*device
,
265 const VkGraphicsPipelineCreateInfo
*pCreateInfo
,
266 const struct anv_graphics_pipeline_create_info
*extra
)
270 pipeline
->device
= device
;
271 pipeline
->layout
= anv_pipeline_layout_from_handle(pCreateInfo
->layout
);
272 memset(pipeline
->shaders
, 0, sizeof(pipeline
->shaders
));
274 result
= anv_reloc_list_init(&pipeline
->batch_relocs
, device
);
275 if (result
!= VK_SUCCESS
) {
276 anv_device_free(device
, pipeline
);
279 pipeline
->batch
.next
= pipeline
->batch
.start
= pipeline
->batch_data
;
280 pipeline
->batch
.end
= pipeline
->batch
.start
+ sizeof(pipeline
->batch_data
);
281 pipeline
->batch
.relocs
= &pipeline
->batch_relocs
;
283 anv_state_stream_init(&pipeline
->program_stream
,
284 &device
->instruction_block_pool
);
286 for (uint32_t i
= 0; i
< pCreateInfo
->stageCount
; i
++) {
287 pipeline
->shaders
[pCreateInfo
->pStages
[i
].stage
] =
288 anv_shader_from_handle(pCreateInfo
->pStages
[i
].shader
);
291 anv_pipeline_init_dynamic_state(pipeline
, pCreateInfo
);
293 if (pCreateInfo
->pTessellationState
)
294 anv_finishme("VK_STRUCTURE_TYPE_PIPELINE_TESSELLATION_STATE_CREATE_INFO");
295 if (pCreateInfo
->pMultisampleState
&&
296 pCreateInfo
->pMultisampleState
->rasterSamples
> 1)
297 anv_finishme("VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO");
299 pipeline
->use_repclear
= extra
&& extra
->use_repclear
;
301 anv_compiler_run(device
->compiler
, pipeline
);
303 const struct brw_wm_prog_data
*wm_prog_data
= &pipeline
->wm_prog_data
;
305 pipeline
->ps_ksp2
= 0;
306 pipeline
->ps_grf_start2
= 0;
307 if (pipeline
->ps_simd8
!= NO_KERNEL
) {
308 pipeline
->ps_ksp0
= pipeline
->ps_simd8
;
309 pipeline
->ps_grf_start0
= wm_prog_data
->base
.dispatch_grf_start_reg
;
310 if (pipeline
->ps_simd16
!= NO_KERNEL
) {
311 pipeline
->ps_ksp2
= pipeline
->ps_simd16
;
312 pipeline
->ps_grf_start2
= wm_prog_data
->dispatch_grf_start_reg_16
;
314 } else if (pipeline
->ps_simd16
!= NO_KERNEL
) {
315 pipeline
->ps_ksp0
= pipeline
->ps_simd16
;
316 pipeline
->ps_grf_start0
= wm_prog_data
->dispatch_grf_start_reg_16
;
318 unreachable("no ps shader");
321 const VkPipelineVertexInputStateCreateInfo
*vi_info
=
322 pCreateInfo
->pVertexInputState
;
323 pipeline
->vb_used
= 0;
324 for (uint32_t i
= 0; i
< vi_info
->bindingCount
; i
++) {
325 const VkVertexInputBindingDescription
*desc
=
326 &vi_info
->pVertexBindingDescriptions
[i
];
328 pipeline
->vb_used
|= 1 << desc
->binding
;
329 pipeline
->binding_stride
[desc
->binding
] = desc
->strideInBytes
;
331 /* Step rate is programmed per vertex element (attribute), not
332 * binding. Set up a map of which bindings step per instance, for
333 * reference by vertex element setup. */
334 switch (desc
->stepRate
) {
336 case VK_VERTEX_INPUT_STEP_RATE_VERTEX
:
337 pipeline
->instancing_enable
[desc
->binding
] = false;
339 case VK_VERTEX_INPUT_STEP_RATE_INSTANCE
:
340 pipeline
->instancing_enable
[desc
->binding
] = true;
345 const VkPipelineInputAssemblyStateCreateInfo
*ia_info
=
346 pCreateInfo
->pInputAssemblyState
;
347 pipeline
->primitive_restart
= ia_info
->primitiveRestartEnable
;
348 pipeline
->topology
= vk_to_gen_primitive_type
[ia_info
->topology
];
350 if (extra
&& extra
->use_rectlist
)
351 pipeline
->topology
= _3DPRIM_RECTLIST
;
357 anv_graphics_pipeline_create(
359 const VkGraphicsPipelineCreateInfo
*pCreateInfo
,
360 const struct anv_graphics_pipeline_create_info
*extra
,
361 VkPipeline
*pPipeline
)
363 ANV_FROM_HANDLE(anv_device
, device
, _device
);
365 switch (device
->info
.gen
) {
367 return gen7_graphics_pipeline_create(_device
, pCreateInfo
, extra
, pPipeline
);
369 return gen8_graphics_pipeline_create(_device
, pCreateInfo
, extra
, pPipeline
);
371 unreachable("unsupported gen\n");
375 VkResult
anv_CreateGraphicsPipelines(
377 VkPipelineCache pipelineCache
,
379 const VkGraphicsPipelineCreateInfo
* pCreateInfos
,
380 VkPipeline
* pPipelines
)
382 VkResult result
= VK_SUCCESS
;
385 for (; i
< count
; i
++) {
386 result
= anv_graphics_pipeline_create(_device
, &pCreateInfos
[i
],
387 NULL
, &pPipelines
[i
]);
388 if (result
!= VK_SUCCESS
) {
389 for (unsigned j
= 0; j
< i
; j
++) {
390 anv_DestroyPipeline(_device
, pPipelines
[j
]);
400 static VkResult
anv_compute_pipeline_create(
402 const VkComputePipelineCreateInfo
* pCreateInfo
,
403 VkPipeline
* pPipeline
)
405 ANV_FROM_HANDLE(anv_device
, device
, _device
);
407 switch (device
->info
.gen
) {
409 return gen7_compute_pipeline_create(_device
, pCreateInfo
, pPipeline
);
411 return gen8_compute_pipeline_create(_device
, pCreateInfo
, pPipeline
);
413 unreachable("unsupported gen\n");
417 VkResult
anv_CreateComputePipelines(
419 VkPipelineCache pipelineCache
,
421 const VkComputePipelineCreateInfo
* pCreateInfos
,
422 VkPipeline
* pPipelines
)
424 VkResult result
= VK_SUCCESS
;
427 for (; i
< count
; i
++) {
428 result
= anv_compute_pipeline_create(_device
, &pCreateInfos
[i
],
430 if (result
!= VK_SUCCESS
) {
431 for (unsigned j
= 0; j
< i
; j
++) {
432 anv_DestroyPipeline(_device
, pPipelines
[j
]);
442 // Pipeline layout functions
444 VkResult
anv_CreatePipelineLayout(
446 const VkPipelineLayoutCreateInfo
* pCreateInfo
,
447 VkPipelineLayout
* pPipelineLayout
)
449 ANV_FROM_HANDLE(anv_device
, device
, _device
);
450 struct anv_pipeline_layout l
, *layout
;
452 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO
);
454 l
.num_sets
= pCreateInfo
->descriptorSetCount
;
456 unsigned dynamic_offset_count
= 0;
458 memset(l
.stage
, 0, sizeof(l
.stage
));
459 for (uint32_t set
= 0; set
< pCreateInfo
->descriptorSetCount
; set
++) {
460 ANV_FROM_HANDLE(anv_descriptor_set_layout
, set_layout
,
461 pCreateInfo
->pSetLayouts
[set
]);
462 l
.set
[set
].layout
= set_layout
;
464 l
.set
[set
].dynamic_offset_start
= dynamic_offset_count
;
465 for (uint32_t b
= 0; b
< set_layout
->binding_count
; b
++) {
466 if (set_layout
->binding
[b
].dynamic_offset_index
>= 0)
467 dynamic_offset_count
+= set_layout
->binding
[b
].array_size
;
470 for (VkShaderStage s
= 0; s
< VK_SHADER_STAGE_NUM
; s
++) {
471 l
.set
[set
].stage
[s
].surface_start
= l
.stage
[s
].surface_count
;
472 l
.set
[set
].stage
[s
].sampler_start
= l
.stage
[s
].sampler_count
;
474 for (uint32_t b
= 0; b
< set_layout
->binding_count
; b
++) {
475 unsigned array_size
= set_layout
->binding
[b
].array_size
;
477 if (set_layout
->binding
[b
].stage
[s
].surface_index
>= 0) {
478 l
.stage
[s
].surface_count
+= array_size
;
480 if (set_layout
->binding
[b
].dynamic_offset_index
>= 0)
481 l
.stage
[s
].has_dynamic_offsets
= true;
484 if (set_layout
->binding
[b
].stage
[s
].sampler_index
>= 0)
485 l
.stage
[s
].sampler_count
+= array_size
;
490 unsigned num_bindings
= 0;
491 for (VkShaderStage s
= 0; s
< VK_SHADER_STAGE_NUM
; s
++)
492 num_bindings
+= l
.stage
[s
].surface_count
+ l
.stage
[s
].sampler_count
;
494 size_t size
= sizeof(*layout
) + num_bindings
* sizeof(layout
->entries
[0]);
496 layout
= anv_device_alloc(device
, size
, 8, VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
498 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
500 /* Now we can actually build our surface and sampler maps */
501 struct anv_pipeline_binding
*entry
= layout
->entries
;
502 for (VkShaderStage s
= 0; s
< VK_SHADER_STAGE_NUM
; s
++) {
503 l
.stage
[s
].surface_to_descriptor
= entry
;
504 entry
+= l
.stage
[s
].surface_count
;
505 l
.stage
[s
].sampler_to_descriptor
= entry
;
506 entry
+= l
.stage
[s
].sampler_count
;
510 for (uint32_t set
= 0; set
< pCreateInfo
->descriptorSetCount
; set
++) {
511 struct anv_descriptor_set_layout
*set_layout
= l
.set
[set
].layout
;
513 unsigned set_offset
= 0;
514 for (uint32_t b
= 0; b
< set_layout
->binding_count
; b
++) {
515 unsigned array_size
= set_layout
->binding
[b
].array_size
;
517 if (set_layout
->binding
[b
].stage
[s
].surface_index
>= 0) {
518 assert(surface
== l
.set
[set
].stage
[s
].surface_start
+
519 set_layout
->binding
[b
].stage
[s
].surface_index
);
520 for (unsigned i
= 0; i
< array_size
; i
++) {
521 l
.stage
[s
].surface_to_descriptor
[surface
+ i
].set
= set
;
522 l
.stage
[s
].surface_to_descriptor
[surface
+ i
].offset
= set_offset
+ i
;
524 surface
+= array_size
;
527 if (set_layout
->binding
[b
].stage
[s
].sampler_index
>= 0) {
528 assert(sampler
== l
.set
[set
].stage
[s
].sampler_start
+
529 set_layout
->binding
[b
].stage
[s
].sampler_index
);
530 for (unsigned i
= 0; i
< array_size
; i
++) {
531 l
.stage
[s
].sampler_to_descriptor
[sampler
+ i
].set
= set
;
532 l
.stage
[s
].sampler_to_descriptor
[sampler
+ i
].offset
= set_offset
+ i
;
534 sampler
+= array_size
;
537 set_offset
+= array_size
;
542 /* Finally, we're done setting it up, copy into the allocated version */
545 *pPipelineLayout
= anv_pipeline_layout_to_handle(layout
);
550 void anv_DestroyPipelineLayout(
552 VkPipelineLayout _pipelineLayout
)
554 ANV_FROM_HANDLE(anv_device
, device
, _device
);
555 ANV_FROM_HANDLE(anv_pipeline_layout
, pipeline_layout
, _pipelineLayout
);
557 anv_device_free(device
, pipeline_layout
);