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"
32 #include "vk_format_info.h"
35 /** \file anv_cmd_buffer.c
37 * This file contains all of the stuff for emitting commands into a command
38 * buffer. This includes implementations of most of the vkCmd*
39 * entrypoints. This file is concerned entirely with state emission and
40 * not with the command buffer data structure itself. As far as this file
41 * is concerned, most of anv_cmd_buffer is magic.
44 /* TODO: These are taken from GLES. We should check the Vulkan spec */
45 const struct anv_dynamic_state default_dynamic_state
= {
58 .blend_constants
= { 0.0f
, 0.0f
, 0.0f
, 0.0f
},
63 .stencil_compare_mask
= {
67 .stencil_write_mask
= {
71 .stencil_reference
= {
78 anv_dynamic_state_copy(struct anv_dynamic_state
*dest
,
79 const struct anv_dynamic_state
*src
,
82 if (copy_mask
& (1 << VK_DYNAMIC_STATE_VIEWPORT
)) {
83 dest
->viewport
.count
= src
->viewport
.count
;
84 typed_memcpy(dest
->viewport
.viewports
, src
->viewport
.viewports
,
88 if (copy_mask
& (1 << VK_DYNAMIC_STATE_SCISSOR
)) {
89 dest
->scissor
.count
= src
->scissor
.count
;
90 typed_memcpy(dest
->scissor
.scissors
, src
->scissor
.scissors
,
94 if (copy_mask
& (1 << VK_DYNAMIC_STATE_LINE_WIDTH
))
95 dest
->line_width
= src
->line_width
;
97 if (copy_mask
& (1 << VK_DYNAMIC_STATE_DEPTH_BIAS
))
98 dest
->depth_bias
= src
->depth_bias
;
100 if (copy_mask
& (1 << VK_DYNAMIC_STATE_BLEND_CONSTANTS
))
101 typed_memcpy(dest
->blend_constants
, src
->blend_constants
, 4);
103 if (copy_mask
& (1 << VK_DYNAMIC_STATE_DEPTH_BOUNDS
))
104 dest
->depth_bounds
= src
->depth_bounds
;
106 if (copy_mask
& (1 << VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK
))
107 dest
->stencil_compare_mask
= src
->stencil_compare_mask
;
109 if (copy_mask
& (1 << VK_DYNAMIC_STATE_STENCIL_WRITE_MASK
))
110 dest
->stencil_write_mask
= src
->stencil_write_mask
;
112 if (copy_mask
& (1 << VK_DYNAMIC_STATE_STENCIL_REFERENCE
))
113 dest
->stencil_reference
= src
->stencil_reference
;
117 anv_cmd_state_init(struct anv_cmd_buffer
*cmd_buffer
)
119 struct anv_cmd_state
*state
= &cmd_buffer
->state
;
121 memset(state
, 0, sizeof(*state
));
123 state
->current_pipeline
= UINT32_MAX
;
124 state
->restart_index
= UINT32_MAX
;
125 state
->gfx
.dynamic
= default_dynamic_state
;
129 anv_cmd_pipeline_state_finish(struct anv_cmd_buffer
*cmd_buffer
,
130 struct anv_cmd_pipeline_state
*pipe_state
)
132 for (uint32_t i
= 0; i
< ARRAY_SIZE(pipe_state
->push_descriptors
); i
++) {
133 if (pipe_state
->push_descriptors
[i
]) {
134 anv_descriptor_set_layout_unref(cmd_buffer
->device
,
135 pipe_state
->push_descriptors
[i
]->set
.layout
);
136 vk_free(&cmd_buffer
->pool
->alloc
, pipe_state
->push_descriptors
[i
]);
142 anv_cmd_state_finish(struct anv_cmd_buffer
*cmd_buffer
)
144 struct anv_cmd_state
*state
= &cmd_buffer
->state
;
146 anv_cmd_pipeline_state_finish(cmd_buffer
, &state
->gfx
.base
);
147 anv_cmd_pipeline_state_finish(cmd_buffer
, &state
->compute
.base
);
149 for (uint32_t i
= 0; i
< MESA_SHADER_STAGES
; i
++)
150 vk_free(&cmd_buffer
->pool
->alloc
, state
->push_constants
[i
]);
152 vk_free(&cmd_buffer
->pool
->alloc
, state
->attachments
);
156 anv_cmd_state_reset(struct anv_cmd_buffer
*cmd_buffer
)
158 anv_cmd_state_finish(cmd_buffer
);
159 anv_cmd_state_init(cmd_buffer
);
163 * This function updates the size of the push constant buffer we need to emit.
164 * This is called in various parts of the driver to ensure that different
165 * pieces of push constant data get emitted as needed. However, it is important
166 * that we never shrink the size of the buffer. For example, a compute shader
167 * dispatch will always call this for the base group id, which has an
168 * offset in the push constant buffer that is smaller than the offset for
169 * storage image data. If the compute shader has storage images, we will call
170 * this again with a larger size during binding table emission. However,
171 * if we dispatch the compute shader again without dirtying our descriptors,
172 * we would still call this function with a smaller size for the base group
173 * id, and not for the images, which would incorrectly shrink the size of the
174 * push constant data we emit with that dispatch, making us drop the image data.
177 anv_cmd_buffer_ensure_push_constants_size(struct anv_cmd_buffer
*cmd_buffer
,
178 gl_shader_stage stage
, uint32_t size
)
180 struct anv_push_constants
**ptr
= &cmd_buffer
->state
.push_constants
[stage
];
183 *ptr
= vk_alloc(&cmd_buffer
->pool
->alloc
, size
, 8,
184 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
186 anv_batch_set_error(&cmd_buffer
->batch
, VK_ERROR_OUT_OF_HOST_MEMORY
);
187 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
190 } else if ((*ptr
)->size
< size
) {
191 *ptr
= vk_realloc(&cmd_buffer
->pool
->alloc
, *ptr
, size
, 8,
192 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
194 anv_batch_set_error(&cmd_buffer
->batch
, VK_ERROR_OUT_OF_HOST_MEMORY
);
195 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
203 static VkResult
anv_create_cmd_buffer(
204 struct anv_device
* device
,
205 struct anv_cmd_pool
* pool
,
206 VkCommandBufferLevel level
,
207 VkCommandBuffer
* pCommandBuffer
)
209 struct anv_cmd_buffer
*cmd_buffer
;
212 cmd_buffer
= vk_alloc(&pool
->alloc
, sizeof(*cmd_buffer
), 8,
213 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
214 if (cmd_buffer
== NULL
)
215 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
217 cmd_buffer
->batch
.status
= VK_SUCCESS
;
219 cmd_buffer
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
220 cmd_buffer
->device
= device
;
221 cmd_buffer
->pool
= pool
;
222 cmd_buffer
->level
= level
;
224 result
= anv_cmd_buffer_init_batch_bo_chain(cmd_buffer
);
225 if (result
!= VK_SUCCESS
)
228 anv_state_stream_init(&cmd_buffer
->surface_state_stream
,
229 &device
->surface_state_pool
, 4096);
230 anv_state_stream_init(&cmd_buffer
->dynamic_state_stream
,
231 &device
->dynamic_state_pool
, 16384);
233 anv_cmd_state_init(cmd_buffer
);
236 list_addtail(&cmd_buffer
->pool_link
, &pool
->cmd_buffers
);
238 /* Init the pool_link so we can safefly call list_del when we destroy
241 list_inithead(&cmd_buffer
->pool_link
);
244 *pCommandBuffer
= anv_cmd_buffer_to_handle(cmd_buffer
);
249 vk_free(&cmd_buffer
->pool
->alloc
, cmd_buffer
);
254 VkResult
anv_AllocateCommandBuffers(
256 const VkCommandBufferAllocateInfo
* pAllocateInfo
,
257 VkCommandBuffer
* pCommandBuffers
)
259 ANV_FROM_HANDLE(anv_device
, device
, _device
);
260 ANV_FROM_HANDLE(anv_cmd_pool
, pool
, pAllocateInfo
->commandPool
);
262 VkResult result
= VK_SUCCESS
;
265 for (i
= 0; i
< pAllocateInfo
->commandBufferCount
; i
++) {
266 result
= anv_create_cmd_buffer(device
, pool
, pAllocateInfo
->level
,
267 &pCommandBuffers
[i
]);
268 if (result
!= VK_SUCCESS
)
272 if (result
!= VK_SUCCESS
) {
273 anv_FreeCommandBuffers(_device
, pAllocateInfo
->commandPool
,
275 for (i
= 0; i
< pAllocateInfo
->commandBufferCount
; i
++)
276 pCommandBuffers
[i
] = VK_NULL_HANDLE
;
283 anv_cmd_buffer_destroy(struct anv_cmd_buffer
*cmd_buffer
)
285 list_del(&cmd_buffer
->pool_link
);
287 anv_cmd_buffer_fini_batch_bo_chain(cmd_buffer
);
289 anv_state_stream_finish(&cmd_buffer
->surface_state_stream
);
290 anv_state_stream_finish(&cmd_buffer
->dynamic_state_stream
);
292 anv_cmd_state_finish(cmd_buffer
);
294 vk_free(&cmd_buffer
->pool
->alloc
, cmd_buffer
);
297 void anv_FreeCommandBuffers(
299 VkCommandPool commandPool
,
300 uint32_t commandBufferCount
,
301 const VkCommandBuffer
* pCommandBuffers
)
303 for (uint32_t i
= 0; i
< commandBufferCount
; i
++) {
304 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, pCommandBuffers
[i
]);
309 anv_cmd_buffer_destroy(cmd_buffer
);
314 anv_cmd_buffer_reset(struct anv_cmd_buffer
*cmd_buffer
)
316 cmd_buffer
->usage_flags
= 0;
317 anv_cmd_buffer_reset_batch_bo_chain(cmd_buffer
);
318 anv_cmd_state_reset(cmd_buffer
);
320 anv_state_stream_finish(&cmd_buffer
->surface_state_stream
);
321 anv_state_stream_init(&cmd_buffer
->surface_state_stream
,
322 &cmd_buffer
->device
->surface_state_pool
, 4096);
324 anv_state_stream_finish(&cmd_buffer
->dynamic_state_stream
);
325 anv_state_stream_init(&cmd_buffer
->dynamic_state_stream
,
326 &cmd_buffer
->device
->dynamic_state_pool
, 16384);
330 VkResult
anv_ResetCommandBuffer(
331 VkCommandBuffer commandBuffer
,
332 VkCommandBufferResetFlags flags
)
334 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
335 return anv_cmd_buffer_reset(cmd_buffer
);
338 #define anv_genX_call(devinfo, func, ...) \
339 switch ((devinfo)->gen) { \
341 if ((devinfo)->is_haswell) { \
342 gen75_##func(__VA_ARGS__); \
344 gen7_##func(__VA_ARGS__); \
348 gen8_##func(__VA_ARGS__); \
351 gen9_##func(__VA_ARGS__); \
354 gen10_##func(__VA_ARGS__); \
357 gen11_##func(__VA_ARGS__); \
360 assert(!"Unknown hardware generation"); \
364 anv_cmd_buffer_emit_state_base_address(struct anv_cmd_buffer
*cmd_buffer
)
366 anv_genX_call(&cmd_buffer
->device
->info
,
367 cmd_buffer_emit_state_base_address
,
372 anv_cmd_buffer_mark_image_written(struct anv_cmd_buffer
*cmd_buffer
,
373 const struct anv_image
*image
,
374 VkImageAspectFlagBits aspect
,
375 enum isl_aux_usage aux_usage
,
378 uint32_t layer_count
)
380 anv_genX_call(&cmd_buffer
->device
->info
,
381 cmd_buffer_mark_image_written
,
382 cmd_buffer
, image
, aspect
, aux_usage
,
383 level
, base_layer
, layer_count
);
387 anv_cmd_emit_conditional_render_predicate(struct anv_cmd_buffer
*cmd_buffer
)
389 anv_genX_call(&cmd_buffer
->device
->info
,
390 cmd_emit_conditional_render_predicate
,
394 void anv_CmdBindPipeline(
395 VkCommandBuffer commandBuffer
,
396 VkPipelineBindPoint pipelineBindPoint
,
397 VkPipeline _pipeline
)
399 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
400 ANV_FROM_HANDLE(anv_pipeline
, pipeline
, _pipeline
);
402 switch (pipelineBindPoint
) {
403 case VK_PIPELINE_BIND_POINT_COMPUTE
:
404 cmd_buffer
->state
.compute
.base
.pipeline
= pipeline
;
405 cmd_buffer
->state
.compute
.pipeline_dirty
= true;
406 cmd_buffer
->state
.push_constants_dirty
|= VK_SHADER_STAGE_COMPUTE_BIT
;
407 cmd_buffer
->state
.descriptors_dirty
|= VK_SHADER_STAGE_COMPUTE_BIT
;
410 case VK_PIPELINE_BIND_POINT_GRAPHICS
:
411 cmd_buffer
->state
.gfx
.base
.pipeline
= pipeline
;
412 cmd_buffer
->state
.gfx
.vb_dirty
|= pipeline
->vb_used
;
413 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_PIPELINE
;
414 cmd_buffer
->state
.push_constants_dirty
|= pipeline
->active_stages
;
415 cmd_buffer
->state
.descriptors_dirty
|= pipeline
->active_stages
;
417 /* Apply the dynamic state from the pipeline */
418 cmd_buffer
->state
.gfx
.dirty
|= pipeline
->dynamic_state_mask
;
419 anv_dynamic_state_copy(&cmd_buffer
->state
.gfx
.dynamic
,
420 &pipeline
->dynamic_state
,
421 pipeline
->dynamic_state_mask
);
425 assert(!"invalid bind point");
430 void anv_CmdSetViewport(
431 VkCommandBuffer commandBuffer
,
432 uint32_t firstViewport
,
433 uint32_t viewportCount
,
434 const VkViewport
* pViewports
)
436 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
438 const uint32_t total_count
= firstViewport
+ viewportCount
;
439 if (cmd_buffer
->state
.gfx
.dynamic
.viewport
.count
< total_count
)
440 cmd_buffer
->state
.gfx
.dynamic
.viewport
.count
= total_count
;
442 memcpy(cmd_buffer
->state
.gfx
.dynamic
.viewport
.viewports
+ firstViewport
,
443 pViewports
, viewportCount
* sizeof(*pViewports
));
445 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_VIEWPORT
;
448 void anv_CmdSetScissor(
449 VkCommandBuffer commandBuffer
,
450 uint32_t firstScissor
,
451 uint32_t scissorCount
,
452 const VkRect2D
* pScissors
)
454 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
456 const uint32_t total_count
= firstScissor
+ scissorCount
;
457 if (cmd_buffer
->state
.gfx
.dynamic
.scissor
.count
< total_count
)
458 cmd_buffer
->state
.gfx
.dynamic
.scissor
.count
= total_count
;
460 memcpy(cmd_buffer
->state
.gfx
.dynamic
.scissor
.scissors
+ firstScissor
,
461 pScissors
, scissorCount
* sizeof(*pScissors
));
463 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_SCISSOR
;
466 void anv_CmdSetLineWidth(
467 VkCommandBuffer commandBuffer
,
470 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
472 cmd_buffer
->state
.gfx
.dynamic
.line_width
= lineWidth
;
473 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_LINE_WIDTH
;
476 void anv_CmdSetDepthBias(
477 VkCommandBuffer commandBuffer
,
478 float depthBiasConstantFactor
,
479 float depthBiasClamp
,
480 float depthBiasSlopeFactor
)
482 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
484 cmd_buffer
->state
.gfx
.dynamic
.depth_bias
.bias
= depthBiasConstantFactor
;
485 cmd_buffer
->state
.gfx
.dynamic
.depth_bias
.clamp
= depthBiasClamp
;
486 cmd_buffer
->state
.gfx
.dynamic
.depth_bias
.slope
= depthBiasSlopeFactor
;
488 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_DEPTH_BIAS
;
491 void anv_CmdSetBlendConstants(
492 VkCommandBuffer commandBuffer
,
493 const float blendConstants
[4])
495 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
497 memcpy(cmd_buffer
->state
.gfx
.dynamic
.blend_constants
,
498 blendConstants
, sizeof(float) * 4);
500 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_BLEND_CONSTANTS
;
503 void anv_CmdSetDepthBounds(
504 VkCommandBuffer commandBuffer
,
505 float minDepthBounds
,
506 float maxDepthBounds
)
508 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
510 cmd_buffer
->state
.gfx
.dynamic
.depth_bounds
.min
= minDepthBounds
;
511 cmd_buffer
->state
.gfx
.dynamic
.depth_bounds
.max
= maxDepthBounds
;
513 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_DEPTH_BOUNDS
;
516 void anv_CmdSetStencilCompareMask(
517 VkCommandBuffer commandBuffer
,
518 VkStencilFaceFlags faceMask
,
519 uint32_t compareMask
)
521 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
523 if (faceMask
& VK_STENCIL_FACE_FRONT_BIT
)
524 cmd_buffer
->state
.gfx
.dynamic
.stencil_compare_mask
.front
= compareMask
;
525 if (faceMask
& VK_STENCIL_FACE_BACK_BIT
)
526 cmd_buffer
->state
.gfx
.dynamic
.stencil_compare_mask
.back
= compareMask
;
528 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_STENCIL_COMPARE_MASK
;
531 void anv_CmdSetStencilWriteMask(
532 VkCommandBuffer commandBuffer
,
533 VkStencilFaceFlags faceMask
,
536 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
538 if (faceMask
& VK_STENCIL_FACE_FRONT_BIT
)
539 cmd_buffer
->state
.gfx
.dynamic
.stencil_write_mask
.front
= writeMask
;
540 if (faceMask
& VK_STENCIL_FACE_BACK_BIT
)
541 cmd_buffer
->state
.gfx
.dynamic
.stencil_write_mask
.back
= writeMask
;
543 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_STENCIL_WRITE_MASK
;
546 void anv_CmdSetStencilReference(
547 VkCommandBuffer commandBuffer
,
548 VkStencilFaceFlags faceMask
,
551 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
553 if (faceMask
& VK_STENCIL_FACE_FRONT_BIT
)
554 cmd_buffer
->state
.gfx
.dynamic
.stencil_reference
.front
= reference
;
555 if (faceMask
& VK_STENCIL_FACE_BACK_BIT
)
556 cmd_buffer
->state
.gfx
.dynamic
.stencil_reference
.back
= reference
;
558 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_STENCIL_REFERENCE
;
562 anv_cmd_buffer_bind_descriptor_set(struct anv_cmd_buffer
*cmd_buffer
,
563 VkPipelineBindPoint bind_point
,
564 struct anv_pipeline_layout
*layout
,
566 struct anv_descriptor_set
*set
,
567 uint32_t *dynamic_offset_count
,
568 const uint32_t **dynamic_offsets
)
570 struct anv_descriptor_set_layout
*set_layout
=
571 layout
->set
[set_index
].layout
;
573 struct anv_cmd_pipeline_state
*pipe_state
;
574 if (bind_point
== VK_PIPELINE_BIND_POINT_COMPUTE
) {
575 pipe_state
= &cmd_buffer
->state
.compute
.base
;
577 assert(bind_point
== VK_PIPELINE_BIND_POINT_GRAPHICS
);
578 pipe_state
= &cmd_buffer
->state
.gfx
.base
;
580 pipe_state
->descriptors
[set_index
] = set
;
582 if (dynamic_offsets
) {
583 if (set_layout
->dynamic_offset_count
> 0) {
584 uint32_t dynamic_offset_start
=
585 layout
->set
[set_index
].dynamic_offset_start
;
587 /* Assert that everything is in range */
588 assert(set_layout
->dynamic_offset_count
<= *dynamic_offset_count
);
589 assert(dynamic_offset_start
+ set_layout
->dynamic_offset_count
<=
590 ARRAY_SIZE(pipe_state
->dynamic_offsets
));
592 typed_memcpy(&pipe_state
->dynamic_offsets
[dynamic_offset_start
],
593 *dynamic_offsets
, set_layout
->dynamic_offset_count
);
595 *dynamic_offsets
+= set_layout
->dynamic_offset_count
;
596 *dynamic_offset_count
-= set_layout
->dynamic_offset_count
;
598 if (bind_point
== VK_PIPELINE_BIND_POINT_COMPUTE
) {
599 cmd_buffer
->state
.push_constants_dirty
|=
600 VK_SHADER_STAGE_COMPUTE_BIT
;
602 cmd_buffer
->state
.push_constants_dirty
|=
603 VK_SHADER_STAGE_ALL_GRAPHICS
;
608 if (bind_point
== VK_PIPELINE_BIND_POINT_COMPUTE
) {
609 cmd_buffer
->state
.descriptors_dirty
|= VK_SHADER_STAGE_COMPUTE_BIT
;
611 assert(bind_point
== VK_PIPELINE_BIND_POINT_GRAPHICS
);
612 cmd_buffer
->state
.descriptors_dirty
|=
613 set_layout
->shader_stages
& VK_SHADER_STAGE_ALL_GRAPHICS
;
616 /* Pipeline layout objects are required to live at least while any command
617 * buffers that use them are in recording state. We need to grab a reference
618 * to the pipeline layout being bound here so we can compute correct dynamic
619 * offsets for VK_DESCRIPTOR_TYPE_*_DYNAMIC in dynamic_offset_for_binding()
620 * when we record draw commands that come after this.
622 pipe_state
->layout
= layout
;
625 void anv_CmdBindDescriptorSets(
626 VkCommandBuffer commandBuffer
,
627 VkPipelineBindPoint pipelineBindPoint
,
628 VkPipelineLayout _layout
,
630 uint32_t descriptorSetCount
,
631 const VkDescriptorSet
* pDescriptorSets
,
632 uint32_t dynamicOffsetCount
,
633 const uint32_t* pDynamicOffsets
)
635 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
636 ANV_FROM_HANDLE(anv_pipeline_layout
, layout
, _layout
);
638 assert(firstSet
+ descriptorSetCount
<= MAX_SETS
);
640 for (uint32_t i
= 0; i
< descriptorSetCount
; i
++) {
641 ANV_FROM_HANDLE(anv_descriptor_set
, set
, pDescriptorSets
[i
]);
642 anv_cmd_buffer_bind_descriptor_set(cmd_buffer
, pipelineBindPoint
,
643 layout
, firstSet
+ i
, set
,
649 void anv_CmdBindVertexBuffers(
650 VkCommandBuffer commandBuffer
,
651 uint32_t firstBinding
,
652 uint32_t bindingCount
,
653 const VkBuffer
* pBuffers
,
654 const VkDeviceSize
* pOffsets
)
656 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
657 struct anv_vertex_binding
*vb
= cmd_buffer
->state
.vertex_bindings
;
659 /* We have to defer setting up vertex buffer since we need the buffer
660 * stride from the pipeline. */
662 assert(firstBinding
+ bindingCount
<= MAX_VBS
);
663 for (uint32_t i
= 0; i
< bindingCount
; i
++) {
664 vb
[firstBinding
+ i
].buffer
= anv_buffer_from_handle(pBuffers
[i
]);
665 vb
[firstBinding
+ i
].offset
= pOffsets
[i
];
666 cmd_buffer
->state
.gfx
.vb_dirty
|= 1 << (firstBinding
+ i
);
670 void anv_CmdBindTransformFeedbackBuffersEXT(
671 VkCommandBuffer commandBuffer
,
672 uint32_t firstBinding
,
673 uint32_t bindingCount
,
674 const VkBuffer
* pBuffers
,
675 const VkDeviceSize
* pOffsets
,
676 const VkDeviceSize
* pSizes
)
678 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
679 struct anv_xfb_binding
*xfb
= cmd_buffer
->state
.xfb_bindings
;
681 /* We have to defer setting up vertex buffer since we need the buffer
682 * stride from the pipeline. */
684 assert(firstBinding
+ bindingCount
<= MAX_XFB_BUFFERS
);
685 for (uint32_t i
= 0; i
< bindingCount
; i
++) {
686 if (pBuffers
[i
] == VK_NULL_HANDLE
) {
687 xfb
[firstBinding
+ i
].buffer
= NULL
;
689 ANV_FROM_HANDLE(anv_buffer
, buffer
, pBuffers
[i
]);
690 xfb
[firstBinding
+ i
].buffer
= buffer
;
691 xfb
[firstBinding
+ i
].offset
= pOffsets
[i
];
692 xfb
[firstBinding
+ i
].size
=
693 anv_buffer_get_range(buffer
, pOffsets
[i
],
694 pSizes
? pSizes
[i
] : VK_WHOLE_SIZE
);
700 anv_isl_format_for_descriptor_type(VkDescriptorType type
)
703 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
704 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
705 return ISL_FORMAT_R32G32B32A32_FLOAT
;
707 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
708 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
709 return ISL_FORMAT_RAW
;
712 unreachable("Invalid descriptor type");
717 anv_cmd_buffer_emit_dynamic(struct anv_cmd_buffer
*cmd_buffer
,
718 const void *data
, uint32_t size
, uint32_t alignment
)
720 struct anv_state state
;
722 state
= anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, size
, alignment
);
723 memcpy(state
.map
, data
, size
);
725 VG(VALGRIND_CHECK_MEM_IS_DEFINED(state
.map
, size
));
731 anv_cmd_buffer_merge_dynamic(struct anv_cmd_buffer
*cmd_buffer
,
732 uint32_t *a
, uint32_t *b
,
733 uint32_t dwords
, uint32_t alignment
)
735 struct anv_state state
;
738 state
= anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
,
739 dwords
* 4, alignment
);
741 for (uint32_t i
= 0; i
< dwords
; i
++)
744 VG(VALGRIND_CHECK_MEM_IS_DEFINED(p
, dwords
* 4));
750 anv_push_constant_value(const struct anv_cmd_pipeline_state
*state
,
751 const struct anv_push_constants
*data
, uint32_t param
)
753 if (BRW_PARAM_IS_BUILTIN(param
)) {
755 case BRW_PARAM_BUILTIN_ZERO
:
757 case BRW_PARAM_BUILTIN_BASE_WORK_GROUP_ID_X
:
758 return data
->base_work_group_id
[0];
759 case BRW_PARAM_BUILTIN_BASE_WORK_GROUP_ID_Y
:
760 return data
->base_work_group_id
[1];
761 case BRW_PARAM_BUILTIN_BASE_WORK_GROUP_ID_Z
:
762 return data
->base_work_group_id
[2];
764 unreachable("Invalid param builtin");
766 } else if (ANV_PARAM_IS_PUSH(param
)) {
767 uint32_t offset
= ANV_PARAM_PUSH_OFFSET(param
);
768 assert(offset
% sizeof(uint32_t) == 0);
769 if (offset
< data
->size
)
770 return *(uint32_t *)((uint8_t *)data
+ offset
);
773 } else if (ANV_PARAM_IS_DYN_OFFSET(param
)) {
774 unsigned idx
= ANV_PARAM_DYN_OFFSET_IDX(param
);
775 assert(idx
< MAX_DYNAMIC_BUFFERS
);
776 return state
->dynamic_offsets
[idx
];
779 assert(!"Invalid param");
784 anv_cmd_buffer_push_constants(struct anv_cmd_buffer
*cmd_buffer
,
785 gl_shader_stage stage
)
787 struct anv_cmd_pipeline_state
*pipeline_state
= &cmd_buffer
->state
.gfx
.base
;
788 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.gfx
.base
.pipeline
;
790 /* If we don't have this stage, bail. */
791 if (!anv_pipeline_has_stage(pipeline
, stage
))
792 return (struct anv_state
) { .offset
= 0 };
794 struct anv_push_constants
*data
=
795 cmd_buffer
->state
.push_constants
[stage
];
796 const struct brw_stage_prog_data
*prog_data
=
797 pipeline
->shaders
[stage
]->prog_data
;
799 /* If we don't actually have any push constants, bail. */
800 if (prog_data
== NULL
|| prog_data
->nr_params
== 0 || data
== NULL
)
801 return (struct anv_state
) { .offset
= 0 };
803 struct anv_state state
=
804 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
,
805 prog_data
->nr_params
* sizeof(float),
806 32 /* bottom 5 bits MBZ */);
808 /* Walk through the param array and fill the buffer with data */
809 uint32_t *u32_map
= state
.map
;
810 for (unsigned i
= 0; i
< prog_data
->nr_params
; i
++) {
811 u32_map
[i
] = anv_push_constant_value(pipeline_state
, data
,
812 prog_data
->param
[i
]);
819 anv_cmd_buffer_cs_push_constants(struct anv_cmd_buffer
*cmd_buffer
)
821 struct anv_cmd_pipeline_state
*pipeline_state
= &cmd_buffer
->state
.compute
.base
;
822 struct anv_push_constants
*data
=
823 cmd_buffer
->state
.push_constants
[MESA_SHADER_COMPUTE
];
824 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute
.base
.pipeline
;
825 const struct brw_cs_prog_data
*cs_prog_data
= get_cs_prog_data(pipeline
);
826 const struct brw_stage_prog_data
*prog_data
= &cs_prog_data
->base
;
828 /* If we don't actually have any push constants, bail. */
829 if (cs_prog_data
->push
.total
.size
== 0)
830 return (struct anv_state
) { .offset
= 0 };
832 const unsigned push_constant_alignment
=
833 cmd_buffer
->device
->info
.gen
< 8 ? 32 : 64;
834 const unsigned aligned_total_push_constants_size
=
835 ALIGN(cs_prog_data
->push
.total
.size
, push_constant_alignment
);
836 struct anv_state state
=
837 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
,
838 aligned_total_push_constants_size
,
839 push_constant_alignment
);
841 /* Walk through the param array and fill the buffer with data */
842 uint32_t *u32_map
= state
.map
;
844 if (cs_prog_data
->push
.cross_thread
.size
> 0) {
846 i
< cs_prog_data
->push
.cross_thread
.dwords
;
848 assert(prog_data
->param
[i
] != BRW_PARAM_BUILTIN_SUBGROUP_ID
);
849 u32_map
[i
] = anv_push_constant_value(pipeline_state
, data
,
850 prog_data
->param
[i
]);
854 if (cs_prog_data
->push
.per_thread
.size
> 0) {
855 for (unsigned t
= 0; t
< cs_prog_data
->threads
; t
++) {
857 8 * (cs_prog_data
->push
.per_thread
.regs
* t
+
858 cs_prog_data
->push
.cross_thread
.regs
);
859 unsigned src
= cs_prog_data
->push
.cross_thread
.dwords
;
860 for ( ; src
< prog_data
->nr_params
; src
++, dst
++) {
861 if (prog_data
->param
[src
] == BRW_PARAM_BUILTIN_SUBGROUP_ID
) {
864 u32_map
[dst
] = anv_push_constant_value(pipeline_state
, data
,
865 prog_data
->param
[src
]);
874 void anv_CmdPushConstants(
875 VkCommandBuffer commandBuffer
,
876 VkPipelineLayout layout
,
877 VkShaderStageFlags stageFlags
,
882 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
884 anv_foreach_stage(stage
, stageFlags
) {
886 anv_cmd_buffer_ensure_push_constant_field(cmd_buffer
,
888 if (result
!= VK_SUCCESS
)
891 memcpy(cmd_buffer
->state
.push_constants
[stage
]->client_data
+ offset
,
895 cmd_buffer
->state
.push_constants_dirty
|= stageFlags
;
898 VkResult
anv_CreateCommandPool(
900 const VkCommandPoolCreateInfo
* pCreateInfo
,
901 const VkAllocationCallbacks
* pAllocator
,
902 VkCommandPool
* pCmdPool
)
904 ANV_FROM_HANDLE(anv_device
, device
, _device
);
905 struct anv_cmd_pool
*pool
;
907 pool
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*pool
), 8,
908 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
910 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
913 pool
->alloc
= *pAllocator
;
915 pool
->alloc
= device
->alloc
;
917 list_inithead(&pool
->cmd_buffers
);
919 *pCmdPool
= anv_cmd_pool_to_handle(pool
);
924 void anv_DestroyCommandPool(
926 VkCommandPool commandPool
,
927 const VkAllocationCallbacks
* pAllocator
)
929 ANV_FROM_HANDLE(anv_device
, device
, _device
);
930 ANV_FROM_HANDLE(anv_cmd_pool
, pool
, commandPool
);
935 list_for_each_entry_safe(struct anv_cmd_buffer
, cmd_buffer
,
936 &pool
->cmd_buffers
, pool_link
) {
937 anv_cmd_buffer_destroy(cmd_buffer
);
940 vk_free2(&device
->alloc
, pAllocator
, pool
);
943 VkResult
anv_ResetCommandPool(
945 VkCommandPool commandPool
,
946 VkCommandPoolResetFlags flags
)
948 ANV_FROM_HANDLE(anv_cmd_pool
, pool
, commandPool
);
950 list_for_each_entry(struct anv_cmd_buffer
, cmd_buffer
,
951 &pool
->cmd_buffers
, pool_link
) {
952 anv_cmd_buffer_reset(cmd_buffer
);
958 void anv_TrimCommandPool(
960 VkCommandPool commandPool
,
961 VkCommandPoolTrimFlags flags
)
963 /* Nothing for us to do here. Our pools stay pretty tidy. */
967 * Return NULL if the current subpass has no depthstencil attachment.
969 const struct anv_image_view
*
970 anv_cmd_buffer_get_depth_stencil_view(const struct anv_cmd_buffer
*cmd_buffer
)
972 const struct anv_subpass
*subpass
= cmd_buffer
->state
.subpass
;
973 const struct anv_framebuffer
*fb
= cmd_buffer
->state
.framebuffer
;
975 if (subpass
->depth_stencil_attachment
== NULL
)
978 const struct anv_image_view
*iview
=
979 fb
->attachments
[subpass
->depth_stencil_attachment
->attachment
];
981 assert(iview
->aspect_mask
& (VK_IMAGE_ASPECT_DEPTH_BIT
|
982 VK_IMAGE_ASPECT_STENCIL_BIT
));
987 static struct anv_descriptor_set
*
988 anv_cmd_buffer_push_descriptor_set(struct anv_cmd_buffer
*cmd_buffer
,
989 VkPipelineBindPoint bind_point
,
990 struct anv_descriptor_set_layout
*layout
,
993 struct anv_cmd_pipeline_state
*pipe_state
;
994 if (bind_point
== VK_PIPELINE_BIND_POINT_COMPUTE
) {
995 pipe_state
= &cmd_buffer
->state
.compute
.base
;
997 assert(bind_point
== VK_PIPELINE_BIND_POINT_GRAPHICS
);
998 pipe_state
= &cmd_buffer
->state
.gfx
.base
;
1001 struct anv_push_descriptor_set
**push_set
=
1002 &pipe_state
->push_descriptors
[_set
];
1004 if (*push_set
== NULL
) {
1005 *push_set
= vk_zalloc(&cmd_buffer
->pool
->alloc
,
1006 sizeof(struct anv_push_descriptor_set
), 8,
1007 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1008 if (*push_set
== NULL
) {
1009 anv_batch_set_error(&cmd_buffer
->batch
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1014 struct anv_descriptor_set
*set
= &(*push_set
)->set
;
1016 if (set
->layout
!= layout
) {
1018 anv_descriptor_set_layout_unref(cmd_buffer
->device
, set
->layout
);
1019 anv_descriptor_set_layout_ref(layout
);
1020 set
->layout
= layout
;
1022 set
->size
= anv_descriptor_set_layout_size(layout
);
1023 set
->buffer_view_count
= layout
->buffer_view_count
;
1024 set
->buffer_views
= (*push_set
)->buffer_views
;
1026 if (layout
->descriptor_buffer_size
&&
1027 ((*push_set
)->set_used_on_gpu
||
1028 set
->desc_mem
.alloc_size
< layout
->descriptor_buffer_size
)) {
1029 /* The previous buffer is either actively used by some GPU command (so
1030 * we can't modify it) or is too small. Allocate a new one.
1032 struct anv_state desc_mem
=
1033 anv_state_stream_alloc(&cmd_buffer
->dynamic_state_stream
,
1034 layout
->descriptor_buffer_size
, 32);
1035 if (set
->desc_mem
.alloc_size
) {
1036 /* TODO: Do we really need to copy all the time? */
1037 memcpy(desc_mem
.map
, set
->desc_mem
.map
,
1038 MIN2(desc_mem
.alloc_size
, set
->desc_mem
.alloc_size
));
1040 set
->desc_mem
= desc_mem
;
1042 struct anv_address addr
= {
1043 .bo
= cmd_buffer
->dynamic_state_stream
.state_pool
->block_pool
.bo
,
1044 .offset
= set
->desc_mem
.offset
,
1047 const struct isl_device
*isl_dev
= &cmd_buffer
->device
->isl_dev
;
1048 set
->desc_surface_state
=
1049 anv_state_stream_alloc(&cmd_buffer
->surface_state_stream
,
1050 isl_dev
->ss
.size
, isl_dev
->ss
.align
);
1051 anv_fill_buffer_surface_state(cmd_buffer
->device
,
1052 set
->desc_surface_state
,
1053 ISL_FORMAT_R32G32B32A32_FLOAT
,
1054 addr
, layout
->descriptor_buffer_size
, 1);
1060 void anv_CmdPushDescriptorSetKHR(
1061 VkCommandBuffer commandBuffer
,
1062 VkPipelineBindPoint pipelineBindPoint
,
1063 VkPipelineLayout _layout
,
1065 uint32_t descriptorWriteCount
,
1066 const VkWriteDescriptorSet
* pDescriptorWrites
)
1068 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1069 ANV_FROM_HANDLE(anv_pipeline_layout
, layout
, _layout
);
1071 assert(_set
< MAX_SETS
);
1073 struct anv_descriptor_set_layout
*set_layout
= layout
->set
[_set
].layout
;
1075 struct anv_descriptor_set
*set
=
1076 anv_cmd_buffer_push_descriptor_set(cmd_buffer
, pipelineBindPoint
,
1081 /* Go through the user supplied descriptors. */
1082 for (uint32_t i
= 0; i
< descriptorWriteCount
; i
++) {
1083 const VkWriteDescriptorSet
*write
= &pDescriptorWrites
[i
];
1085 switch (write
->descriptorType
) {
1086 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1087 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1088 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1089 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1090 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
1091 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
1092 anv_descriptor_set_write_image_view(cmd_buffer
->device
, set
,
1093 write
->pImageInfo
+ j
,
1094 write
->descriptorType
,
1096 write
->dstArrayElement
+ j
);
1100 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1101 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1102 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
1103 ANV_FROM_HANDLE(anv_buffer_view
, bview
,
1104 write
->pTexelBufferView
[j
]);
1106 anv_descriptor_set_write_buffer_view(cmd_buffer
->device
, set
,
1107 write
->descriptorType
,
1110 write
->dstArrayElement
+ j
);
1114 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1115 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1116 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1117 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1118 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
1119 assert(write
->pBufferInfo
[j
].buffer
);
1120 ANV_FROM_HANDLE(anv_buffer
, buffer
, write
->pBufferInfo
[j
].buffer
);
1123 anv_descriptor_set_write_buffer(cmd_buffer
->device
, set
,
1124 &cmd_buffer
->surface_state_stream
,
1125 write
->descriptorType
,
1128 write
->dstArrayElement
+ j
,
1129 write
->pBufferInfo
[j
].offset
,
1130 write
->pBufferInfo
[j
].range
);
1134 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
: {
1135 const VkWriteDescriptorSetInlineUniformBlockEXT
*inline_write
=
1136 vk_find_struct_const(write
->pNext
,
1137 WRITE_DESCRIPTOR_SET_INLINE_UNIFORM_BLOCK_EXT
);
1138 assert(inline_write
->dataSize
== write
->descriptorCount
);
1139 anv_descriptor_set_write_inline_uniform_data(cmd_buffer
->device
, set
,
1141 inline_write
->pData
,
1142 write
->dstArrayElement
,
1143 inline_write
->dataSize
);
1152 anv_cmd_buffer_bind_descriptor_set(cmd_buffer
, pipelineBindPoint
,
1153 layout
, _set
, set
, NULL
, NULL
);
1156 void anv_CmdPushDescriptorSetWithTemplateKHR(
1157 VkCommandBuffer commandBuffer
,
1158 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
1159 VkPipelineLayout _layout
,
1163 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1164 ANV_FROM_HANDLE(anv_descriptor_update_template
, template,
1165 descriptorUpdateTemplate
);
1166 ANV_FROM_HANDLE(anv_pipeline_layout
, layout
, _layout
);
1168 assert(_set
< MAX_PUSH_DESCRIPTORS
);
1170 struct anv_descriptor_set_layout
*set_layout
= layout
->set
[_set
].layout
;
1172 struct anv_descriptor_set
*set
=
1173 anv_cmd_buffer_push_descriptor_set(cmd_buffer
, template->bind_point
,
1178 anv_descriptor_set_write_template(cmd_buffer
->device
, set
,
1179 &cmd_buffer
->surface_state_stream
,
1183 anv_cmd_buffer_bind_descriptor_set(cmd_buffer
, template->bind_point
,
1184 layout
, _set
, set
, NULL
, NULL
);
1187 void anv_CmdSetDeviceMask(
1188 VkCommandBuffer commandBuffer
,
1189 uint32_t deviceMask
)