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
= {
82 * Copy the dynamic state from src to dest based on the copy_mask.
84 * Avoid copying states that have not changed, except for VIEWPORT, SCISSOR and
85 * BLEND_CONSTANTS (always copy them if they are in the copy_mask).
87 * Returns a mask of the states which changed.
90 anv_dynamic_state_copy(struct anv_dynamic_state
*dest
,
91 const struct anv_dynamic_state
*src
,
92 anv_cmd_dirty_mask_t copy_mask
)
94 anv_cmd_dirty_mask_t changed
= 0;
96 if (copy_mask
& ANV_CMD_DIRTY_DYNAMIC_VIEWPORT
) {
97 dest
->viewport
.count
= src
->viewport
.count
;
98 typed_memcpy(dest
->viewport
.viewports
, src
->viewport
.viewports
,
100 changed
|= ANV_CMD_DIRTY_DYNAMIC_VIEWPORT
;
103 if (copy_mask
& ANV_CMD_DIRTY_DYNAMIC_SCISSOR
) {
104 dest
->scissor
.count
= src
->scissor
.count
;
105 typed_memcpy(dest
->scissor
.scissors
, src
->scissor
.scissors
,
107 changed
|= ANV_CMD_DIRTY_DYNAMIC_SCISSOR
;
110 if (copy_mask
& ANV_CMD_DIRTY_DYNAMIC_BLEND_CONSTANTS
) {
111 typed_memcpy(dest
->blend_constants
, src
->blend_constants
, 4);
112 changed
|= ANV_CMD_DIRTY_DYNAMIC_BLEND_CONSTANTS
;
115 #define ANV_CMP_COPY(field, flag) \
116 if (copy_mask & flag) { \
117 if (dest->field != src->field) { \
118 dest->field = src->field; \
123 ANV_CMP_COPY(line_width
, ANV_CMD_DIRTY_DYNAMIC_LINE_WIDTH
);
125 ANV_CMP_COPY(depth_bias
.bias
, ANV_CMD_DIRTY_DYNAMIC_DEPTH_BIAS
);
126 ANV_CMP_COPY(depth_bias
.clamp
, ANV_CMD_DIRTY_DYNAMIC_DEPTH_BIAS
);
127 ANV_CMP_COPY(depth_bias
.slope
, ANV_CMD_DIRTY_DYNAMIC_DEPTH_BIAS
);
129 ANV_CMP_COPY(depth_bounds
.min
, ANV_CMD_DIRTY_DYNAMIC_DEPTH_BOUNDS
);
130 ANV_CMP_COPY(depth_bounds
.max
, ANV_CMD_DIRTY_DYNAMIC_DEPTH_BOUNDS
);
132 ANV_CMP_COPY(stencil_compare_mask
.front
, ANV_CMD_DIRTY_DYNAMIC_STENCIL_COMPARE_MASK
);
133 ANV_CMP_COPY(stencil_compare_mask
.back
, ANV_CMD_DIRTY_DYNAMIC_STENCIL_COMPARE_MASK
);
135 ANV_CMP_COPY(stencil_write_mask
.front
, ANV_CMD_DIRTY_DYNAMIC_STENCIL_WRITE_MASK
);
136 ANV_CMP_COPY(stencil_write_mask
.back
, ANV_CMD_DIRTY_DYNAMIC_STENCIL_WRITE_MASK
);
138 ANV_CMP_COPY(stencil_reference
.front
, ANV_CMD_DIRTY_DYNAMIC_STENCIL_REFERENCE
);
139 ANV_CMP_COPY(stencil_reference
.back
, ANV_CMD_DIRTY_DYNAMIC_STENCIL_REFERENCE
);
141 ANV_CMP_COPY(line_stipple
.factor
, ANV_CMD_DIRTY_DYNAMIC_LINE_STIPPLE
);
142 ANV_CMP_COPY(line_stipple
.pattern
, ANV_CMD_DIRTY_DYNAMIC_LINE_STIPPLE
);
150 anv_cmd_state_init(struct anv_cmd_buffer
*cmd_buffer
)
152 struct anv_cmd_state
*state
= &cmd_buffer
->state
;
154 memset(state
, 0, sizeof(*state
));
156 state
->current_pipeline
= UINT32_MAX
;
157 state
->restart_index
= UINT32_MAX
;
158 state
->gfx
.dynamic
= default_dynamic_state
;
162 anv_cmd_pipeline_state_finish(struct anv_cmd_buffer
*cmd_buffer
,
163 struct anv_cmd_pipeline_state
*pipe_state
)
165 for (uint32_t i
= 0; i
< ARRAY_SIZE(pipe_state
->push_descriptors
); i
++) {
166 if (pipe_state
->push_descriptors
[i
]) {
167 anv_descriptor_set_layout_unref(cmd_buffer
->device
,
168 pipe_state
->push_descriptors
[i
]->set
.layout
);
169 vk_free(&cmd_buffer
->pool
->alloc
, pipe_state
->push_descriptors
[i
]);
175 anv_cmd_state_finish(struct anv_cmd_buffer
*cmd_buffer
)
177 struct anv_cmd_state
*state
= &cmd_buffer
->state
;
179 anv_cmd_pipeline_state_finish(cmd_buffer
, &state
->gfx
.base
);
180 anv_cmd_pipeline_state_finish(cmd_buffer
, &state
->compute
.base
);
182 vk_free(&cmd_buffer
->pool
->alloc
, state
->attachments
);
186 anv_cmd_state_reset(struct anv_cmd_buffer
*cmd_buffer
)
188 anv_cmd_state_finish(cmd_buffer
);
189 anv_cmd_state_init(cmd_buffer
);
192 static VkResult
anv_create_cmd_buffer(
193 struct anv_device
* device
,
194 struct anv_cmd_pool
* pool
,
195 VkCommandBufferLevel level
,
196 VkCommandBuffer
* pCommandBuffer
)
198 struct anv_cmd_buffer
*cmd_buffer
;
201 cmd_buffer
= vk_alloc(&pool
->alloc
, sizeof(*cmd_buffer
), 8,
202 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
203 if (cmd_buffer
== NULL
)
204 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
206 cmd_buffer
->batch
.status
= VK_SUCCESS
;
208 cmd_buffer
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
209 cmd_buffer
->device
= device
;
210 cmd_buffer
->pool
= pool
;
211 cmd_buffer
->level
= level
;
213 result
= anv_cmd_buffer_init_batch_bo_chain(cmd_buffer
);
214 if (result
!= VK_SUCCESS
)
217 anv_state_stream_init(&cmd_buffer
->surface_state_stream
,
218 &device
->surface_state_pool
, 4096);
219 anv_state_stream_init(&cmd_buffer
->dynamic_state_stream
,
220 &device
->dynamic_state_pool
, 16384);
222 anv_cmd_state_init(cmd_buffer
);
225 list_addtail(&cmd_buffer
->pool_link
, &pool
->cmd_buffers
);
227 /* Init the pool_link so we can safefly call list_del when we destroy
230 list_inithead(&cmd_buffer
->pool_link
);
233 *pCommandBuffer
= anv_cmd_buffer_to_handle(cmd_buffer
);
238 vk_free(&cmd_buffer
->pool
->alloc
, cmd_buffer
);
243 VkResult
anv_AllocateCommandBuffers(
245 const VkCommandBufferAllocateInfo
* pAllocateInfo
,
246 VkCommandBuffer
* pCommandBuffers
)
248 ANV_FROM_HANDLE(anv_device
, device
, _device
);
249 ANV_FROM_HANDLE(anv_cmd_pool
, pool
, pAllocateInfo
->commandPool
);
251 VkResult result
= VK_SUCCESS
;
254 for (i
= 0; i
< pAllocateInfo
->commandBufferCount
; i
++) {
255 result
= anv_create_cmd_buffer(device
, pool
, pAllocateInfo
->level
,
256 &pCommandBuffers
[i
]);
257 if (result
!= VK_SUCCESS
)
261 if (result
!= VK_SUCCESS
) {
262 anv_FreeCommandBuffers(_device
, pAllocateInfo
->commandPool
,
264 for (i
= 0; i
< pAllocateInfo
->commandBufferCount
; i
++)
265 pCommandBuffers
[i
] = VK_NULL_HANDLE
;
272 anv_cmd_buffer_destroy(struct anv_cmd_buffer
*cmd_buffer
)
274 list_del(&cmd_buffer
->pool_link
);
276 anv_cmd_buffer_fini_batch_bo_chain(cmd_buffer
);
278 anv_state_stream_finish(&cmd_buffer
->surface_state_stream
);
279 anv_state_stream_finish(&cmd_buffer
->dynamic_state_stream
);
281 anv_cmd_state_finish(cmd_buffer
);
283 vk_free(&cmd_buffer
->pool
->alloc
, cmd_buffer
);
286 void anv_FreeCommandBuffers(
288 VkCommandPool commandPool
,
289 uint32_t commandBufferCount
,
290 const VkCommandBuffer
* pCommandBuffers
)
292 for (uint32_t i
= 0; i
< commandBufferCount
; i
++) {
293 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, pCommandBuffers
[i
]);
298 anv_cmd_buffer_destroy(cmd_buffer
);
303 anv_cmd_buffer_reset(struct anv_cmd_buffer
*cmd_buffer
)
305 cmd_buffer
->usage_flags
= 0;
306 anv_cmd_buffer_reset_batch_bo_chain(cmd_buffer
);
307 anv_cmd_state_reset(cmd_buffer
);
309 anv_state_stream_finish(&cmd_buffer
->surface_state_stream
);
310 anv_state_stream_init(&cmd_buffer
->surface_state_stream
,
311 &cmd_buffer
->device
->surface_state_pool
, 4096);
313 anv_state_stream_finish(&cmd_buffer
->dynamic_state_stream
);
314 anv_state_stream_init(&cmd_buffer
->dynamic_state_stream
,
315 &cmd_buffer
->device
->dynamic_state_pool
, 16384);
319 VkResult
anv_ResetCommandBuffer(
320 VkCommandBuffer commandBuffer
,
321 VkCommandBufferResetFlags flags
)
323 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
324 return anv_cmd_buffer_reset(cmd_buffer
);
327 #define anv_genX_call(devinfo, func, ...) \
328 switch ((devinfo)->gen) { \
330 if ((devinfo)->is_haswell) { \
331 gen75_##func(__VA_ARGS__); \
333 gen7_##func(__VA_ARGS__); \
337 gen8_##func(__VA_ARGS__); \
340 gen9_##func(__VA_ARGS__); \
343 gen10_##func(__VA_ARGS__); \
346 gen11_##func(__VA_ARGS__); \
349 assert(!"Unknown hardware generation"); \
353 anv_cmd_buffer_emit_state_base_address(struct anv_cmd_buffer
*cmd_buffer
)
355 anv_genX_call(&cmd_buffer
->device
->info
,
356 cmd_buffer_emit_state_base_address
,
361 anv_cmd_buffer_mark_image_written(struct anv_cmd_buffer
*cmd_buffer
,
362 const struct anv_image
*image
,
363 VkImageAspectFlagBits aspect
,
364 enum isl_aux_usage aux_usage
,
367 uint32_t layer_count
)
369 anv_genX_call(&cmd_buffer
->device
->info
,
370 cmd_buffer_mark_image_written
,
371 cmd_buffer
, image
, aspect
, aux_usage
,
372 level
, base_layer
, layer_count
);
376 anv_cmd_emit_conditional_render_predicate(struct anv_cmd_buffer
*cmd_buffer
)
378 anv_genX_call(&cmd_buffer
->device
->info
,
379 cmd_emit_conditional_render_predicate
,
383 void anv_CmdBindPipeline(
384 VkCommandBuffer commandBuffer
,
385 VkPipelineBindPoint pipelineBindPoint
,
386 VkPipeline _pipeline
)
388 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
389 ANV_FROM_HANDLE(anv_pipeline
, pipeline
, _pipeline
);
391 switch (pipelineBindPoint
) {
392 case VK_PIPELINE_BIND_POINT_COMPUTE
:
393 cmd_buffer
->state
.compute
.base
.pipeline
= pipeline
;
394 cmd_buffer
->state
.compute
.pipeline_dirty
= true;
395 cmd_buffer
->state
.push_constants_dirty
|= VK_SHADER_STAGE_COMPUTE_BIT
;
396 cmd_buffer
->state
.descriptors_dirty
|= VK_SHADER_STAGE_COMPUTE_BIT
;
399 case VK_PIPELINE_BIND_POINT_GRAPHICS
:
400 cmd_buffer
->state
.gfx
.base
.pipeline
= pipeline
;
401 cmd_buffer
->state
.gfx
.vb_dirty
|= pipeline
->vb_used
;
402 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_PIPELINE
;
403 cmd_buffer
->state
.push_constants_dirty
|= pipeline
->active_stages
;
404 cmd_buffer
->state
.descriptors_dirty
|= pipeline
->active_stages
;
406 /* Apply the dynamic state from the pipeline */
407 cmd_buffer
->state
.gfx
.dirty
|=
408 anv_dynamic_state_copy(&cmd_buffer
->state
.gfx
.dynamic
,
409 &pipeline
->dynamic_state
,
410 pipeline
->dynamic_state_mask
);
414 assert(!"invalid bind point");
419 void anv_CmdSetViewport(
420 VkCommandBuffer commandBuffer
,
421 uint32_t firstViewport
,
422 uint32_t viewportCount
,
423 const VkViewport
* pViewports
)
425 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
427 const uint32_t total_count
= firstViewport
+ viewportCount
;
428 if (cmd_buffer
->state
.gfx
.dynamic
.viewport
.count
< total_count
)
429 cmd_buffer
->state
.gfx
.dynamic
.viewport
.count
= total_count
;
431 memcpy(cmd_buffer
->state
.gfx
.dynamic
.viewport
.viewports
+ firstViewport
,
432 pViewports
, viewportCount
* sizeof(*pViewports
));
434 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_VIEWPORT
;
437 void anv_CmdSetScissor(
438 VkCommandBuffer commandBuffer
,
439 uint32_t firstScissor
,
440 uint32_t scissorCount
,
441 const VkRect2D
* pScissors
)
443 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
445 const uint32_t total_count
= firstScissor
+ scissorCount
;
446 if (cmd_buffer
->state
.gfx
.dynamic
.scissor
.count
< total_count
)
447 cmd_buffer
->state
.gfx
.dynamic
.scissor
.count
= total_count
;
449 memcpy(cmd_buffer
->state
.gfx
.dynamic
.scissor
.scissors
+ firstScissor
,
450 pScissors
, scissorCount
* sizeof(*pScissors
));
452 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_SCISSOR
;
455 void anv_CmdSetLineWidth(
456 VkCommandBuffer commandBuffer
,
459 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
461 cmd_buffer
->state
.gfx
.dynamic
.line_width
= lineWidth
;
462 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_LINE_WIDTH
;
465 void anv_CmdSetDepthBias(
466 VkCommandBuffer commandBuffer
,
467 float depthBiasConstantFactor
,
468 float depthBiasClamp
,
469 float depthBiasSlopeFactor
)
471 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
473 cmd_buffer
->state
.gfx
.dynamic
.depth_bias
.bias
= depthBiasConstantFactor
;
474 cmd_buffer
->state
.gfx
.dynamic
.depth_bias
.clamp
= depthBiasClamp
;
475 cmd_buffer
->state
.gfx
.dynamic
.depth_bias
.slope
= depthBiasSlopeFactor
;
477 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_DEPTH_BIAS
;
480 void anv_CmdSetBlendConstants(
481 VkCommandBuffer commandBuffer
,
482 const float blendConstants
[4])
484 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
486 memcpy(cmd_buffer
->state
.gfx
.dynamic
.blend_constants
,
487 blendConstants
, sizeof(float) * 4);
489 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_BLEND_CONSTANTS
;
492 void anv_CmdSetDepthBounds(
493 VkCommandBuffer commandBuffer
,
494 float minDepthBounds
,
495 float maxDepthBounds
)
497 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
499 cmd_buffer
->state
.gfx
.dynamic
.depth_bounds
.min
= minDepthBounds
;
500 cmd_buffer
->state
.gfx
.dynamic
.depth_bounds
.max
= maxDepthBounds
;
502 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_DEPTH_BOUNDS
;
505 void anv_CmdSetStencilCompareMask(
506 VkCommandBuffer commandBuffer
,
507 VkStencilFaceFlags faceMask
,
508 uint32_t compareMask
)
510 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
512 if (faceMask
& VK_STENCIL_FACE_FRONT_BIT
)
513 cmd_buffer
->state
.gfx
.dynamic
.stencil_compare_mask
.front
= compareMask
;
514 if (faceMask
& VK_STENCIL_FACE_BACK_BIT
)
515 cmd_buffer
->state
.gfx
.dynamic
.stencil_compare_mask
.back
= compareMask
;
517 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_STENCIL_COMPARE_MASK
;
520 void anv_CmdSetStencilWriteMask(
521 VkCommandBuffer commandBuffer
,
522 VkStencilFaceFlags faceMask
,
525 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
527 if (faceMask
& VK_STENCIL_FACE_FRONT_BIT
)
528 cmd_buffer
->state
.gfx
.dynamic
.stencil_write_mask
.front
= writeMask
;
529 if (faceMask
& VK_STENCIL_FACE_BACK_BIT
)
530 cmd_buffer
->state
.gfx
.dynamic
.stencil_write_mask
.back
= writeMask
;
532 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_STENCIL_WRITE_MASK
;
535 void anv_CmdSetStencilReference(
536 VkCommandBuffer commandBuffer
,
537 VkStencilFaceFlags faceMask
,
540 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
542 if (faceMask
& VK_STENCIL_FACE_FRONT_BIT
)
543 cmd_buffer
->state
.gfx
.dynamic
.stencil_reference
.front
= reference
;
544 if (faceMask
& VK_STENCIL_FACE_BACK_BIT
)
545 cmd_buffer
->state
.gfx
.dynamic
.stencil_reference
.back
= reference
;
547 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_STENCIL_REFERENCE
;
550 void anv_CmdSetLineStippleEXT(
551 VkCommandBuffer commandBuffer
,
552 uint32_t lineStippleFactor
,
553 uint16_t lineStipplePattern
)
555 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
557 cmd_buffer
->state
.gfx
.dynamic
.line_stipple
.factor
= lineStippleFactor
;
558 cmd_buffer
->state
.gfx
.dynamic
.line_stipple
.pattern
= lineStipplePattern
;
560 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_LINE_STIPPLE
;
564 anv_cmd_buffer_bind_descriptor_set(struct anv_cmd_buffer
*cmd_buffer
,
565 VkPipelineBindPoint bind_point
,
566 struct anv_pipeline_layout
*layout
,
568 struct anv_descriptor_set
*set
,
569 uint32_t *dynamic_offset_count
,
570 const uint32_t **dynamic_offsets
)
572 struct anv_descriptor_set_layout
*set_layout
=
573 layout
->set
[set_index
].layout
;
575 struct anv_cmd_pipeline_state
*pipe_state
;
576 if (bind_point
== VK_PIPELINE_BIND_POINT_COMPUTE
) {
577 pipe_state
= &cmd_buffer
->state
.compute
.base
;
579 assert(bind_point
== VK_PIPELINE_BIND_POINT_GRAPHICS
);
580 pipe_state
= &cmd_buffer
->state
.gfx
.base
;
582 pipe_state
->descriptors
[set_index
] = set
;
584 if (dynamic_offsets
) {
585 if (set_layout
->dynamic_offset_count
> 0) {
586 uint32_t dynamic_offset_start
=
587 layout
->set
[set_index
].dynamic_offset_start
;
589 anv_foreach_stage(stage
, set_layout
->shader_stages
) {
590 struct anv_push_constants
*push
=
591 &cmd_buffer
->state
.push_constants
[stage
];
593 /* Assert that everything is in range */
594 assert(set_layout
->dynamic_offset_count
<= *dynamic_offset_count
);
595 assert(dynamic_offset_start
+ set_layout
->dynamic_offset_count
<=
596 ARRAY_SIZE(push
->dynamic_offsets
));
598 typed_memcpy(&push
->dynamic_offsets
[dynamic_offset_start
],
599 *dynamic_offsets
, set_layout
->dynamic_offset_count
);
602 *dynamic_offsets
+= set_layout
->dynamic_offset_count
;
603 *dynamic_offset_count
-= set_layout
->dynamic_offset_count
;
605 if (bind_point
== VK_PIPELINE_BIND_POINT_COMPUTE
) {
606 cmd_buffer
->state
.push_constants_dirty
|=
607 VK_SHADER_STAGE_COMPUTE_BIT
;
609 cmd_buffer
->state
.push_constants_dirty
|=
610 VK_SHADER_STAGE_ALL_GRAPHICS
;
615 if (bind_point
== VK_PIPELINE_BIND_POINT_COMPUTE
) {
616 cmd_buffer
->state
.descriptors_dirty
|= VK_SHADER_STAGE_COMPUTE_BIT
;
618 assert(bind_point
== VK_PIPELINE_BIND_POINT_GRAPHICS
);
619 cmd_buffer
->state
.descriptors_dirty
|=
620 set_layout
->shader_stages
& VK_SHADER_STAGE_ALL_GRAPHICS
;
624 void anv_CmdBindDescriptorSets(
625 VkCommandBuffer commandBuffer
,
626 VkPipelineBindPoint pipelineBindPoint
,
627 VkPipelineLayout _layout
,
629 uint32_t descriptorSetCount
,
630 const VkDescriptorSet
* pDescriptorSets
,
631 uint32_t dynamicOffsetCount
,
632 const uint32_t* pDynamicOffsets
)
634 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
635 ANV_FROM_HANDLE(anv_pipeline_layout
, layout
, _layout
);
637 assert(firstSet
+ descriptorSetCount
<= MAX_SETS
);
639 for (uint32_t i
= 0; i
< descriptorSetCount
; i
++) {
640 ANV_FROM_HANDLE(anv_descriptor_set
, set
, pDescriptorSets
[i
]);
641 anv_cmd_buffer_bind_descriptor_set(cmd_buffer
, pipelineBindPoint
,
642 layout
, firstSet
+ i
, set
,
648 void anv_CmdBindVertexBuffers(
649 VkCommandBuffer commandBuffer
,
650 uint32_t firstBinding
,
651 uint32_t bindingCount
,
652 const VkBuffer
* pBuffers
,
653 const VkDeviceSize
* pOffsets
)
655 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
656 struct anv_vertex_binding
*vb
= cmd_buffer
->state
.vertex_bindings
;
658 /* We have to defer setting up vertex buffer since we need the buffer
659 * stride from the pipeline. */
661 assert(firstBinding
+ bindingCount
<= MAX_VBS
);
662 for (uint32_t i
= 0; i
< bindingCount
; i
++) {
663 vb
[firstBinding
+ i
].buffer
= anv_buffer_from_handle(pBuffers
[i
]);
664 vb
[firstBinding
+ i
].offset
= pOffsets
[i
];
665 cmd_buffer
->state
.gfx
.vb_dirty
|= 1 << (firstBinding
+ i
);
669 void anv_CmdBindTransformFeedbackBuffersEXT(
670 VkCommandBuffer commandBuffer
,
671 uint32_t firstBinding
,
672 uint32_t bindingCount
,
673 const VkBuffer
* pBuffers
,
674 const VkDeviceSize
* pOffsets
,
675 const VkDeviceSize
* pSizes
)
677 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
678 struct anv_xfb_binding
*xfb
= cmd_buffer
->state
.xfb_bindings
;
680 /* We have to defer setting up vertex buffer since we need the buffer
681 * stride from the pipeline. */
683 assert(firstBinding
+ bindingCount
<= MAX_XFB_BUFFERS
);
684 for (uint32_t i
= 0; i
< bindingCount
; i
++) {
685 if (pBuffers
[i
] == VK_NULL_HANDLE
) {
686 xfb
[firstBinding
+ i
].buffer
= NULL
;
688 ANV_FROM_HANDLE(anv_buffer
, buffer
, pBuffers
[i
]);
689 xfb
[firstBinding
+ i
].buffer
= buffer
;
690 xfb
[firstBinding
+ i
].offset
= pOffsets
[i
];
691 xfb
[firstBinding
+ i
].size
=
692 anv_buffer_get_range(buffer
, pOffsets
[i
],
693 pSizes
? pSizes
[i
] : VK_WHOLE_SIZE
);
699 anv_isl_format_for_descriptor_type(VkDescriptorType type
)
702 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
703 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
704 return ISL_FORMAT_R32G32B32A32_FLOAT
;
706 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
707 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
708 return ISL_FORMAT_RAW
;
711 unreachable("Invalid descriptor type");
716 anv_cmd_buffer_emit_dynamic(struct anv_cmd_buffer
*cmd_buffer
,
717 const void *data
, uint32_t size
, uint32_t alignment
)
719 struct anv_state state
;
721 state
= anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, size
, alignment
);
722 memcpy(state
.map
, data
, size
);
724 VG(VALGRIND_CHECK_MEM_IS_DEFINED(state
.map
, size
));
730 anv_cmd_buffer_merge_dynamic(struct anv_cmd_buffer
*cmd_buffer
,
731 uint32_t *a
, uint32_t *b
,
732 uint32_t dwords
, uint32_t alignment
)
734 struct anv_state state
;
737 state
= anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
,
738 dwords
* 4, alignment
);
740 for (uint32_t i
= 0; i
< dwords
; i
++)
743 VG(VALGRIND_CHECK_MEM_IS_DEFINED(p
, dwords
* 4));
749 anv_cmd_buffer_push_constants(struct anv_cmd_buffer
*cmd_buffer
,
750 gl_shader_stage stage
)
752 struct anv_push_constants
*data
=
753 &cmd_buffer
->state
.push_constants
[stage
];
755 struct anv_state state
=
756 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
,
757 sizeof(struct anv_push_constants
),
758 32 /* bottom 5 bits MBZ */);
759 memcpy(state
.map
, data
, sizeof(struct anv_push_constants
));
765 anv_cmd_buffer_cs_push_constants(struct anv_cmd_buffer
*cmd_buffer
)
767 struct anv_push_constants
*data
=
768 &cmd_buffer
->state
.push_constants
[MESA_SHADER_COMPUTE
];
769 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute
.base
.pipeline
;
770 const struct brw_cs_prog_data
*cs_prog_data
= get_cs_prog_data(pipeline
);
771 const struct anv_push_range
*range
=
772 &pipeline
->shaders
[MESA_SHADER_COMPUTE
]->bind_map
.push_ranges
[0];
774 if (cs_prog_data
->push
.total
.size
== 0)
775 return (struct anv_state
) { .offset
= 0 };
777 const unsigned push_constant_alignment
=
778 cmd_buffer
->device
->info
.gen
< 8 ? 32 : 64;
779 const unsigned aligned_total_push_constants_size
=
780 ALIGN(cs_prog_data
->push
.total
.size
, push_constant_alignment
);
781 struct anv_state state
=
782 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
,
783 aligned_total_push_constants_size
,
784 push_constant_alignment
);
786 void *dst
= state
.map
;
787 const void *src
= (char *)data
+ (range
->start
* 32);
789 if (cs_prog_data
->push
.cross_thread
.size
> 0) {
790 memcpy(dst
, src
, cs_prog_data
->push
.cross_thread
.size
);
791 dst
+= cs_prog_data
->push
.cross_thread
.size
;
792 src
+= cs_prog_data
->push
.cross_thread
.size
;
795 if (cs_prog_data
->push
.per_thread
.size
> 0) {
796 for (unsigned t
= 0; t
< cs_prog_data
->threads
; t
++) {
797 memcpy(dst
, src
, cs_prog_data
->push
.per_thread
.size
);
799 uint32_t *subgroup_id
= dst
+
800 offsetof(struct anv_push_constants
, cs
.subgroup_id
) -
801 (range
->start
* 32 + cs_prog_data
->push
.cross_thread
.size
);
804 dst
+= cs_prog_data
->push
.per_thread
.size
;
811 void anv_CmdPushConstants(
812 VkCommandBuffer commandBuffer
,
813 VkPipelineLayout layout
,
814 VkShaderStageFlags stageFlags
,
819 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
821 anv_foreach_stage(stage
, stageFlags
) {
822 memcpy(cmd_buffer
->state
.push_constants
[stage
].client_data
+ offset
,
826 cmd_buffer
->state
.push_constants_dirty
|= stageFlags
;
829 VkResult
anv_CreateCommandPool(
831 const VkCommandPoolCreateInfo
* pCreateInfo
,
832 const VkAllocationCallbacks
* pAllocator
,
833 VkCommandPool
* pCmdPool
)
835 ANV_FROM_HANDLE(anv_device
, device
, _device
);
836 struct anv_cmd_pool
*pool
;
838 pool
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*pool
), 8,
839 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
841 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
844 pool
->alloc
= *pAllocator
;
846 pool
->alloc
= device
->alloc
;
848 list_inithead(&pool
->cmd_buffers
);
850 *pCmdPool
= anv_cmd_pool_to_handle(pool
);
855 void anv_DestroyCommandPool(
857 VkCommandPool commandPool
,
858 const VkAllocationCallbacks
* pAllocator
)
860 ANV_FROM_HANDLE(anv_device
, device
, _device
);
861 ANV_FROM_HANDLE(anv_cmd_pool
, pool
, commandPool
);
866 list_for_each_entry_safe(struct anv_cmd_buffer
, cmd_buffer
,
867 &pool
->cmd_buffers
, pool_link
) {
868 anv_cmd_buffer_destroy(cmd_buffer
);
871 vk_free2(&device
->alloc
, pAllocator
, pool
);
874 VkResult
anv_ResetCommandPool(
876 VkCommandPool commandPool
,
877 VkCommandPoolResetFlags flags
)
879 ANV_FROM_HANDLE(anv_cmd_pool
, pool
, commandPool
);
881 list_for_each_entry(struct anv_cmd_buffer
, cmd_buffer
,
882 &pool
->cmd_buffers
, pool_link
) {
883 anv_cmd_buffer_reset(cmd_buffer
);
889 void anv_TrimCommandPool(
891 VkCommandPool commandPool
,
892 VkCommandPoolTrimFlags flags
)
894 /* Nothing for us to do here. Our pools stay pretty tidy. */
898 * Return NULL if the current subpass has no depthstencil attachment.
900 const struct anv_image_view
*
901 anv_cmd_buffer_get_depth_stencil_view(const struct anv_cmd_buffer
*cmd_buffer
)
903 const struct anv_subpass
*subpass
= cmd_buffer
->state
.subpass
;
905 if (subpass
->depth_stencil_attachment
== NULL
)
908 const struct anv_image_view
*iview
=
909 cmd_buffer
->state
.attachments
[subpass
->depth_stencil_attachment
->attachment
].image_view
;
911 assert(iview
->aspect_mask
& (VK_IMAGE_ASPECT_DEPTH_BIT
|
912 VK_IMAGE_ASPECT_STENCIL_BIT
));
917 static struct anv_descriptor_set
*
918 anv_cmd_buffer_push_descriptor_set(struct anv_cmd_buffer
*cmd_buffer
,
919 VkPipelineBindPoint bind_point
,
920 struct anv_descriptor_set_layout
*layout
,
923 struct anv_cmd_pipeline_state
*pipe_state
;
924 if (bind_point
== VK_PIPELINE_BIND_POINT_COMPUTE
) {
925 pipe_state
= &cmd_buffer
->state
.compute
.base
;
927 assert(bind_point
== VK_PIPELINE_BIND_POINT_GRAPHICS
);
928 pipe_state
= &cmd_buffer
->state
.gfx
.base
;
931 struct anv_push_descriptor_set
**push_set
=
932 &pipe_state
->push_descriptors
[_set
];
934 if (*push_set
== NULL
) {
935 *push_set
= vk_zalloc(&cmd_buffer
->pool
->alloc
,
936 sizeof(struct anv_push_descriptor_set
), 8,
937 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
938 if (*push_set
== NULL
) {
939 anv_batch_set_error(&cmd_buffer
->batch
, VK_ERROR_OUT_OF_HOST_MEMORY
);
944 struct anv_descriptor_set
*set
= &(*push_set
)->set
;
946 if (set
->layout
!= layout
) {
948 anv_descriptor_set_layout_unref(cmd_buffer
->device
, set
->layout
);
949 anv_descriptor_set_layout_ref(layout
);
950 set
->layout
= layout
;
952 set
->size
= anv_descriptor_set_layout_size(layout
);
953 set
->buffer_view_count
= layout
->buffer_view_count
;
954 set
->buffer_views
= (*push_set
)->buffer_views
;
956 if (layout
->descriptor_buffer_size
&&
957 ((*push_set
)->set_used_on_gpu
||
958 set
->desc_mem
.alloc_size
< layout
->descriptor_buffer_size
)) {
959 /* The previous buffer is either actively used by some GPU command (so
960 * we can't modify it) or is too small. Allocate a new one.
962 struct anv_state desc_mem
=
963 anv_state_stream_alloc(&cmd_buffer
->dynamic_state_stream
,
964 layout
->descriptor_buffer_size
, 32);
965 if (set
->desc_mem
.alloc_size
) {
966 /* TODO: Do we really need to copy all the time? */
967 memcpy(desc_mem
.map
, set
->desc_mem
.map
,
968 MIN2(desc_mem
.alloc_size
, set
->desc_mem
.alloc_size
));
970 set
->desc_mem
= desc_mem
;
972 struct anv_address addr
= {
973 .bo
= cmd_buffer
->dynamic_state_stream
.state_pool
->block_pool
.bo
,
974 .offset
= set
->desc_mem
.offset
,
977 const struct isl_device
*isl_dev
= &cmd_buffer
->device
->isl_dev
;
978 set
->desc_surface_state
=
979 anv_state_stream_alloc(&cmd_buffer
->surface_state_stream
,
980 isl_dev
->ss
.size
, isl_dev
->ss
.align
);
981 anv_fill_buffer_surface_state(cmd_buffer
->device
,
982 set
->desc_surface_state
,
983 ISL_FORMAT_R32G32B32A32_FLOAT
,
984 addr
, layout
->descriptor_buffer_size
, 1);
990 void anv_CmdPushDescriptorSetKHR(
991 VkCommandBuffer commandBuffer
,
992 VkPipelineBindPoint pipelineBindPoint
,
993 VkPipelineLayout _layout
,
995 uint32_t descriptorWriteCount
,
996 const VkWriteDescriptorSet
* pDescriptorWrites
)
998 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
999 ANV_FROM_HANDLE(anv_pipeline_layout
, layout
, _layout
);
1001 assert(_set
< MAX_SETS
);
1003 struct anv_descriptor_set_layout
*set_layout
= layout
->set
[_set
].layout
;
1005 struct anv_descriptor_set
*set
=
1006 anv_cmd_buffer_push_descriptor_set(cmd_buffer
, pipelineBindPoint
,
1011 /* Go through the user supplied descriptors. */
1012 for (uint32_t i
= 0; i
< descriptorWriteCount
; i
++) {
1013 const VkWriteDescriptorSet
*write
= &pDescriptorWrites
[i
];
1015 switch (write
->descriptorType
) {
1016 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1017 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1018 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1019 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1020 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
1021 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
1022 anv_descriptor_set_write_image_view(cmd_buffer
->device
, set
,
1023 write
->pImageInfo
+ j
,
1024 write
->descriptorType
,
1026 write
->dstArrayElement
+ j
);
1030 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1031 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1032 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
1033 ANV_FROM_HANDLE(anv_buffer_view
, bview
,
1034 write
->pTexelBufferView
[j
]);
1036 anv_descriptor_set_write_buffer_view(cmd_buffer
->device
, set
,
1037 write
->descriptorType
,
1040 write
->dstArrayElement
+ j
);
1044 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1045 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1046 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1047 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1048 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
1049 assert(write
->pBufferInfo
[j
].buffer
);
1050 ANV_FROM_HANDLE(anv_buffer
, buffer
, write
->pBufferInfo
[j
].buffer
);
1053 anv_descriptor_set_write_buffer(cmd_buffer
->device
, set
,
1054 &cmd_buffer
->surface_state_stream
,
1055 write
->descriptorType
,
1058 write
->dstArrayElement
+ j
,
1059 write
->pBufferInfo
[j
].offset
,
1060 write
->pBufferInfo
[j
].range
);
1069 anv_cmd_buffer_bind_descriptor_set(cmd_buffer
, pipelineBindPoint
,
1070 layout
, _set
, set
, NULL
, NULL
);
1073 void anv_CmdPushDescriptorSetWithTemplateKHR(
1074 VkCommandBuffer commandBuffer
,
1075 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
1076 VkPipelineLayout _layout
,
1080 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1081 ANV_FROM_HANDLE(anv_descriptor_update_template
, template,
1082 descriptorUpdateTemplate
);
1083 ANV_FROM_HANDLE(anv_pipeline_layout
, layout
, _layout
);
1085 assert(_set
< MAX_PUSH_DESCRIPTORS
);
1087 struct anv_descriptor_set_layout
*set_layout
= layout
->set
[_set
].layout
;
1089 struct anv_descriptor_set
*set
=
1090 anv_cmd_buffer_push_descriptor_set(cmd_buffer
, template->bind_point
,
1095 anv_descriptor_set_write_template(cmd_buffer
->device
, set
,
1096 &cmd_buffer
->surface_state_stream
,
1100 anv_cmd_buffer_bind_descriptor_set(cmd_buffer
, template->bind_point
,
1101 layout
, _set
, set
, NULL
, NULL
);
1104 void anv_CmdSetDeviceMask(
1105 VkCommandBuffer commandBuffer
,
1106 uint32_t deviceMask
)