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
,
384 mem_update(void *dst
, const void *src
, size_t size
)
386 if (memcmp(dst
, src
, size
) == 0)
389 memcpy(dst
, src
, size
);
394 set_dirty_for_bind_map(struct anv_cmd_buffer
*cmd_buffer
,
395 gl_shader_stage stage
,
396 const struct anv_pipeline_bind_map
*map
)
398 if (mem_update(cmd_buffer
->state
.surface_sha1s
[stage
],
399 map
->surface_sha1
, sizeof(map
->surface_sha1
)))
400 cmd_buffer
->state
.descriptors_dirty
|= mesa_to_vk_shader_stage(stage
);
402 if (mem_update(cmd_buffer
->state
.sampler_sha1s
[stage
],
403 map
->sampler_sha1
, sizeof(map
->sampler_sha1
)))
404 cmd_buffer
->state
.descriptors_dirty
|= mesa_to_vk_shader_stage(stage
);
406 if (mem_update(cmd_buffer
->state
.push_sha1s
[stage
],
407 map
->push_sha1
, sizeof(map
->push_sha1
)))
408 cmd_buffer
->state
.push_constants_dirty
|= mesa_to_vk_shader_stage(stage
);
411 void anv_CmdBindPipeline(
412 VkCommandBuffer commandBuffer
,
413 VkPipelineBindPoint pipelineBindPoint
,
414 VkPipeline _pipeline
)
416 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
417 ANV_FROM_HANDLE(anv_pipeline
, pipeline
, _pipeline
);
419 switch (pipelineBindPoint
) {
420 case VK_PIPELINE_BIND_POINT_COMPUTE
: {
421 if (cmd_buffer
->state
.compute
.base
.pipeline
== pipeline
)
424 cmd_buffer
->state
.compute
.base
.pipeline
= pipeline
;
425 cmd_buffer
->state
.compute
.pipeline_dirty
= true;
426 const struct anv_pipeline_bind_map
*bind_map
=
427 &pipeline
->shaders
[MESA_SHADER_COMPUTE
]->bind_map
;
428 set_dirty_for_bind_map(cmd_buffer
, MESA_SHADER_COMPUTE
, bind_map
);
432 case VK_PIPELINE_BIND_POINT_GRAPHICS
:
433 if (cmd_buffer
->state
.gfx
.base
.pipeline
== pipeline
)
436 cmd_buffer
->state
.gfx
.base
.pipeline
= pipeline
;
437 cmd_buffer
->state
.gfx
.vb_dirty
|= pipeline
->vb_used
;
438 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_PIPELINE
;
440 anv_foreach_stage(stage
, pipeline
->active_stages
) {
441 set_dirty_for_bind_map(cmd_buffer
, stage
,
442 &pipeline
->shaders
[stage
]->bind_map
);
445 /* Apply the dynamic state from the pipeline */
446 cmd_buffer
->state
.gfx
.dirty
|=
447 anv_dynamic_state_copy(&cmd_buffer
->state
.gfx
.dynamic
,
448 &pipeline
->dynamic_state
,
449 pipeline
->dynamic_state_mask
);
453 assert(!"invalid bind point");
458 void anv_CmdSetViewport(
459 VkCommandBuffer commandBuffer
,
460 uint32_t firstViewport
,
461 uint32_t viewportCount
,
462 const VkViewport
* pViewports
)
464 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
466 const uint32_t total_count
= firstViewport
+ viewportCount
;
467 if (cmd_buffer
->state
.gfx
.dynamic
.viewport
.count
< total_count
)
468 cmd_buffer
->state
.gfx
.dynamic
.viewport
.count
= total_count
;
470 memcpy(cmd_buffer
->state
.gfx
.dynamic
.viewport
.viewports
+ firstViewport
,
471 pViewports
, viewportCount
* sizeof(*pViewports
));
473 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_VIEWPORT
;
476 void anv_CmdSetScissor(
477 VkCommandBuffer commandBuffer
,
478 uint32_t firstScissor
,
479 uint32_t scissorCount
,
480 const VkRect2D
* pScissors
)
482 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
484 const uint32_t total_count
= firstScissor
+ scissorCount
;
485 if (cmd_buffer
->state
.gfx
.dynamic
.scissor
.count
< total_count
)
486 cmd_buffer
->state
.gfx
.dynamic
.scissor
.count
= total_count
;
488 memcpy(cmd_buffer
->state
.gfx
.dynamic
.scissor
.scissors
+ firstScissor
,
489 pScissors
, scissorCount
* sizeof(*pScissors
));
491 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_SCISSOR
;
494 void anv_CmdSetLineWidth(
495 VkCommandBuffer commandBuffer
,
498 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
500 cmd_buffer
->state
.gfx
.dynamic
.line_width
= lineWidth
;
501 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_LINE_WIDTH
;
504 void anv_CmdSetDepthBias(
505 VkCommandBuffer commandBuffer
,
506 float depthBiasConstantFactor
,
507 float depthBiasClamp
,
508 float depthBiasSlopeFactor
)
510 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
512 cmd_buffer
->state
.gfx
.dynamic
.depth_bias
.bias
= depthBiasConstantFactor
;
513 cmd_buffer
->state
.gfx
.dynamic
.depth_bias
.clamp
= depthBiasClamp
;
514 cmd_buffer
->state
.gfx
.dynamic
.depth_bias
.slope
= depthBiasSlopeFactor
;
516 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_DEPTH_BIAS
;
519 void anv_CmdSetBlendConstants(
520 VkCommandBuffer commandBuffer
,
521 const float blendConstants
[4])
523 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
525 memcpy(cmd_buffer
->state
.gfx
.dynamic
.blend_constants
,
526 blendConstants
, sizeof(float) * 4);
528 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_BLEND_CONSTANTS
;
531 void anv_CmdSetDepthBounds(
532 VkCommandBuffer commandBuffer
,
533 float minDepthBounds
,
534 float maxDepthBounds
)
536 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
538 cmd_buffer
->state
.gfx
.dynamic
.depth_bounds
.min
= minDepthBounds
;
539 cmd_buffer
->state
.gfx
.dynamic
.depth_bounds
.max
= maxDepthBounds
;
541 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_DEPTH_BOUNDS
;
544 void anv_CmdSetStencilCompareMask(
545 VkCommandBuffer commandBuffer
,
546 VkStencilFaceFlags faceMask
,
547 uint32_t compareMask
)
549 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
551 if (faceMask
& VK_STENCIL_FACE_FRONT_BIT
)
552 cmd_buffer
->state
.gfx
.dynamic
.stencil_compare_mask
.front
= compareMask
;
553 if (faceMask
& VK_STENCIL_FACE_BACK_BIT
)
554 cmd_buffer
->state
.gfx
.dynamic
.stencil_compare_mask
.back
= compareMask
;
556 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_STENCIL_COMPARE_MASK
;
559 void anv_CmdSetStencilWriteMask(
560 VkCommandBuffer commandBuffer
,
561 VkStencilFaceFlags faceMask
,
564 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
566 if (faceMask
& VK_STENCIL_FACE_FRONT_BIT
)
567 cmd_buffer
->state
.gfx
.dynamic
.stencil_write_mask
.front
= writeMask
;
568 if (faceMask
& VK_STENCIL_FACE_BACK_BIT
)
569 cmd_buffer
->state
.gfx
.dynamic
.stencil_write_mask
.back
= writeMask
;
571 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_STENCIL_WRITE_MASK
;
574 void anv_CmdSetStencilReference(
575 VkCommandBuffer commandBuffer
,
576 VkStencilFaceFlags faceMask
,
579 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
581 if (faceMask
& VK_STENCIL_FACE_FRONT_BIT
)
582 cmd_buffer
->state
.gfx
.dynamic
.stencil_reference
.front
= reference
;
583 if (faceMask
& VK_STENCIL_FACE_BACK_BIT
)
584 cmd_buffer
->state
.gfx
.dynamic
.stencil_reference
.back
= reference
;
586 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_STENCIL_REFERENCE
;
589 void anv_CmdSetLineStippleEXT(
590 VkCommandBuffer commandBuffer
,
591 uint32_t lineStippleFactor
,
592 uint16_t lineStipplePattern
)
594 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
596 cmd_buffer
->state
.gfx
.dynamic
.line_stipple
.factor
= lineStippleFactor
;
597 cmd_buffer
->state
.gfx
.dynamic
.line_stipple
.pattern
= lineStipplePattern
;
599 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_LINE_STIPPLE
;
603 anv_cmd_buffer_bind_descriptor_set(struct anv_cmd_buffer
*cmd_buffer
,
604 VkPipelineBindPoint bind_point
,
605 struct anv_pipeline_layout
*layout
,
607 struct anv_descriptor_set
*set
,
608 uint32_t *dynamic_offset_count
,
609 const uint32_t **dynamic_offsets
)
611 struct anv_descriptor_set_layout
*set_layout
=
612 layout
->set
[set_index
].layout
;
614 VkShaderStageFlags stages
= set_layout
->shader_stages
&
615 (bind_point
== VK_PIPELINE_BIND_POINT_COMPUTE
?
616 VK_SHADER_STAGE_COMPUTE_BIT
: VK_SHADER_STAGE_ALL_GRAPHICS
);
618 VkShaderStageFlags dirty_stages
= 0;
619 if (bind_point
== VK_PIPELINE_BIND_POINT_COMPUTE
) {
620 if (cmd_buffer
->state
.compute
.base
.descriptors
[set_index
] != set
) {
621 cmd_buffer
->state
.compute
.base
.descriptors
[set_index
] = set
;
622 dirty_stages
|= stages
;
625 assert(bind_point
== VK_PIPELINE_BIND_POINT_GRAPHICS
);
626 if (cmd_buffer
->state
.gfx
.base
.descriptors
[set_index
] != set
) {
627 cmd_buffer
->state
.gfx
.base
.descriptors
[set_index
] = set
;
628 dirty_stages
|= stages
;
632 /* If it's a push descriptor set, we have to flag things as dirty
633 * regardless of whether or not the CPU-side data structure changed as we
634 * may have edited in-place.
636 if (set
->pool
== NULL
)
637 dirty_stages
|= stages
;
639 if (dynamic_offsets
) {
640 if (set_layout
->dynamic_offset_count
> 0) {
641 uint32_t dynamic_offset_start
=
642 layout
->set
[set_index
].dynamic_offset_start
;
644 anv_foreach_stage(stage
, stages
) {
645 struct anv_push_constants
*push
=
646 &cmd_buffer
->state
.push_constants
[stage
];
647 uint32_t *push_offsets
=
648 &push
->dynamic_offsets
[dynamic_offset_start
];
650 /* Assert that everything is in range */
651 assert(set_layout
->dynamic_offset_count
<= *dynamic_offset_count
);
652 assert(dynamic_offset_start
+ set_layout
->dynamic_offset_count
<=
653 ARRAY_SIZE(push
->dynamic_offsets
));
655 unsigned mask
= set_layout
->stage_dynamic_offsets
[stage
];
656 STATIC_ASSERT(MAX_DYNAMIC_BUFFERS
<= sizeof(mask
) * 8);
658 int i
= u_bit_scan(&mask
);
659 if (push_offsets
[i
] != (*dynamic_offsets
)[i
]) {
660 push_offsets
[i
] = (*dynamic_offsets
)[i
];
661 dirty_stages
|= mesa_to_vk_shader_stage(stage
);
666 *dynamic_offsets
+= set_layout
->dynamic_offset_count
;
667 *dynamic_offset_count
-= set_layout
->dynamic_offset_count
;
671 cmd_buffer
->state
.descriptors_dirty
|= dirty_stages
;
672 cmd_buffer
->state
.push_constants_dirty
|= dirty_stages
;
675 void anv_CmdBindDescriptorSets(
676 VkCommandBuffer commandBuffer
,
677 VkPipelineBindPoint pipelineBindPoint
,
678 VkPipelineLayout _layout
,
680 uint32_t descriptorSetCount
,
681 const VkDescriptorSet
* pDescriptorSets
,
682 uint32_t dynamicOffsetCount
,
683 const uint32_t* pDynamicOffsets
)
685 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
686 ANV_FROM_HANDLE(anv_pipeline_layout
, layout
, _layout
);
688 assert(firstSet
+ descriptorSetCount
<= MAX_SETS
);
690 for (uint32_t i
= 0; i
< descriptorSetCount
; i
++) {
691 ANV_FROM_HANDLE(anv_descriptor_set
, set
, pDescriptorSets
[i
]);
692 anv_cmd_buffer_bind_descriptor_set(cmd_buffer
, pipelineBindPoint
,
693 layout
, firstSet
+ i
, set
,
699 void anv_CmdBindVertexBuffers(
700 VkCommandBuffer commandBuffer
,
701 uint32_t firstBinding
,
702 uint32_t bindingCount
,
703 const VkBuffer
* pBuffers
,
704 const VkDeviceSize
* pOffsets
)
706 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
707 struct anv_vertex_binding
*vb
= cmd_buffer
->state
.vertex_bindings
;
709 /* We have to defer setting up vertex buffer since we need the buffer
710 * stride from the pipeline. */
712 assert(firstBinding
+ bindingCount
<= MAX_VBS
);
713 for (uint32_t i
= 0; i
< bindingCount
; i
++) {
714 vb
[firstBinding
+ i
].buffer
= anv_buffer_from_handle(pBuffers
[i
]);
715 vb
[firstBinding
+ i
].offset
= pOffsets
[i
];
716 cmd_buffer
->state
.gfx
.vb_dirty
|= 1 << (firstBinding
+ i
);
720 void anv_CmdBindTransformFeedbackBuffersEXT(
721 VkCommandBuffer commandBuffer
,
722 uint32_t firstBinding
,
723 uint32_t bindingCount
,
724 const VkBuffer
* pBuffers
,
725 const VkDeviceSize
* pOffsets
,
726 const VkDeviceSize
* pSizes
)
728 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
729 struct anv_xfb_binding
*xfb
= cmd_buffer
->state
.xfb_bindings
;
731 /* We have to defer setting up vertex buffer since we need the buffer
732 * stride from the pipeline. */
734 assert(firstBinding
+ bindingCount
<= MAX_XFB_BUFFERS
);
735 for (uint32_t i
= 0; i
< bindingCount
; i
++) {
736 if (pBuffers
[i
] == VK_NULL_HANDLE
) {
737 xfb
[firstBinding
+ i
].buffer
= NULL
;
739 ANV_FROM_HANDLE(anv_buffer
, buffer
, pBuffers
[i
]);
740 xfb
[firstBinding
+ i
].buffer
= buffer
;
741 xfb
[firstBinding
+ i
].offset
= pOffsets
[i
];
742 xfb
[firstBinding
+ i
].size
=
743 anv_buffer_get_range(buffer
, pOffsets
[i
],
744 pSizes
? pSizes
[i
] : VK_WHOLE_SIZE
);
750 anv_isl_format_for_descriptor_type(VkDescriptorType type
)
753 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
754 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
755 return ISL_FORMAT_R32G32B32A32_FLOAT
;
757 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
758 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
759 return ISL_FORMAT_RAW
;
762 unreachable("Invalid descriptor type");
767 anv_cmd_buffer_emit_dynamic(struct anv_cmd_buffer
*cmd_buffer
,
768 const void *data
, uint32_t size
, uint32_t alignment
)
770 struct anv_state state
;
772 state
= anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, size
, alignment
);
773 memcpy(state
.map
, data
, size
);
775 VG(VALGRIND_CHECK_MEM_IS_DEFINED(state
.map
, size
));
781 anv_cmd_buffer_merge_dynamic(struct anv_cmd_buffer
*cmd_buffer
,
782 uint32_t *a
, uint32_t *b
,
783 uint32_t dwords
, uint32_t alignment
)
785 struct anv_state state
;
788 state
= anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
,
789 dwords
* 4, alignment
);
791 for (uint32_t i
= 0; i
< dwords
; i
++)
794 VG(VALGRIND_CHECK_MEM_IS_DEFINED(p
, dwords
* 4));
800 anv_cmd_buffer_push_constants(struct anv_cmd_buffer
*cmd_buffer
,
801 gl_shader_stage stage
)
803 struct anv_push_constants
*data
=
804 &cmd_buffer
->state
.push_constants
[stage
];
806 struct anv_state state
=
807 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
,
808 sizeof(struct anv_push_constants
),
809 32 /* bottom 5 bits MBZ */);
810 memcpy(state
.map
, data
, sizeof(struct anv_push_constants
));
816 anv_cmd_buffer_cs_push_constants(struct anv_cmd_buffer
*cmd_buffer
)
818 struct anv_push_constants
*data
=
819 &cmd_buffer
->state
.push_constants
[MESA_SHADER_COMPUTE
];
820 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute
.base
.pipeline
;
821 const struct brw_cs_prog_data
*cs_prog_data
= get_cs_prog_data(pipeline
);
822 const struct anv_push_range
*range
=
823 &pipeline
->shaders
[MESA_SHADER_COMPUTE
]->bind_map
.push_ranges
[0];
825 if (cs_prog_data
->push
.total
.size
== 0)
826 return (struct anv_state
) { .offset
= 0 };
828 const unsigned push_constant_alignment
=
829 cmd_buffer
->device
->info
.gen
< 8 ? 32 : 64;
830 const unsigned aligned_total_push_constants_size
=
831 ALIGN(cs_prog_data
->push
.total
.size
, push_constant_alignment
);
832 struct anv_state state
=
833 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
,
834 aligned_total_push_constants_size
,
835 push_constant_alignment
);
837 void *dst
= state
.map
;
838 const void *src
= (char *)data
+ (range
->start
* 32);
840 if (cs_prog_data
->push
.cross_thread
.size
> 0) {
841 memcpy(dst
, src
, cs_prog_data
->push
.cross_thread
.size
);
842 dst
+= cs_prog_data
->push
.cross_thread
.size
;
843 src
+= cs_prog_data
->push
.cross_thread
.size
;
846 if (cs_prog_data
->push
.per_thread
.size
> 0) {
847 for (unsigned t
= 0; t
< cs_prog_data
->threads
; t
++) {
848 memcpy(dst
, src
, cs_prog_data
->push
.per_thread
.size
);
850 uint32_t *subgroup_id
= dst
+
851 offsetof(struct anv_push_constants
, cs
.subgroup_id
) -
852 (range
->start
* 32 + cs_prog_data
->push
.cross_thread
.size
);
855 dst
+= cs_prog_data
->push
.per_thread
.size
;
862 void anv_CmdPushConstants(
863 VkCommandBuffer commandBuffer
,
864 VkPipelineLayout layout
,
865 VkShaderStageFlags stageFlags
,
870 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
872 anv_foreach_stage(stage
, stageFlags
) {
873 memcpy(cmd_buffer
->state
.push_constants
[stage
].client_data
+ offset
,
877 cmd_buffer
->state
.push_constants_dirty
|= stageFlags
;
880 VkResult
anv_CreateCommandPool(
882 const VkCommandPoolCreateInfo
* pCreateInfo
,
883 const VkAllocationCallbacks
* pAllocator
,
884 VkCommandPool
* pCmdPool
)
886 ANV_FROM_HANDLE(anv_device
, device
, _device
);
887 struct anv_cmd_pool
*pool
;
889 pool
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*pool
), 8,
890 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
892 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
895 pool
->alloc
= *pAllocator
;
897 pool
->alloc
= device
->alloc
;
899 list_inithead(&pool
->cmd_buffers
);
901 *pCmdPool
= anv_cmd_pool_to_handle(pool
);
906 void anv_DestroyCommandPool(
908 VkCommandPool commandPool
,
909 const VkAllocationCallbacks
* pAllocator
)
911 ANV_FROM_HANDLE(anv_device
, device
, _device
);
912 ANV_FROM_HANDLE(anv_cmd_pool
, pool
, commandPool
);
917 list_for_each_entry_safe(struct anv_cmd_buffer
, cmd_buffer
,
918 &pool
->cmd_buffers
, pool_link
) {
919 anv_cmd_buffer_destroy(cmd_buffer
);
922 vk_free2(&device
->alloc
, pAllocator
, pool
);
925 VkResult
anv_ResetCommandPool(
927 VkCommandPool commandPool
,
928 VkCommandPoolResetFlags flags
)
930 ANV_FROM_HANDLE(anv_cmd_pool
, pool
, commandPool
);
932 list_for_each_entry(struct anv_cmd_buffer
, cmd_buffer
,
933 &pool
->cmd_buffers
, pool_link
) {
934 anv_cmd_buffer_reset(cmd_buffer
);
940 void anv_TrimCommandPool(
942 VkCommandPool commandPool
,
943 VkCommandPoolTrimFlags flags
)
945 /* Nothing for us to do here. Our pools stay pretty tidy. */
949 * Return NULL if the current subpass has no depthstencil attachment.
951 const struct anv_image_view
*
952 anv_cmd_buffer_get_depth_stencil_view(const struct anv_cmd_buffer
*cmd_buffer
)
954 const struct anv_subpass
*subpass
= cmd_buffer
->state
.subpass
;
956 if (subpass
->depth_stencil_attachment
== NULL
)
959 const struct anv_image_view
*iview
=
960 cmd_buffer
->state
.attachments
[subpass
->depth_stencil_attachment
->attachment
].image_view
;
962 assert(iview
->aspect_mask
& (VK_IMAGE_ASPECT_DEPTH_BIT
|
963 VK_IMAGE_ASPECT_STENCIL_BIT
));
968 static struct anv_descriptor_set
*
969 anv_cmd_buffer_push_descriptor_set(struct anv_cmd_buffer
*cmd_buffer
,
970 VkPipelineBindPoint bind_point
,
971 struct anv_descriptor_set_layout
*layout
,
974 struct anv_cmd_pipeline_state
*pipe_state
;
975 if (bind_point
== VK_PIPELINE_BIND_POINT_COMPUTE
) {
976 pipe_state
= &cmd_buffer
->state
.compute
.base
;
978 assert(bind_point
== VK_PIPELINE_BIND_POINT_GRAPHICS
);
979 pipe_state
= &cmd_buffer
->state
.gfx
.base
;
982 struct anv_push_descriptor_set
**push_set
=
983 &pipe_state
->push_descriptors
[_set
];
985 if (*push_set
== NULL
) {
986 *push_set
= vk_zalloc(&cmd_buffer
->pool
->alloc
,
987 sizeof(struct anv_push_descriptor_set
), 8,
988 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
989 if (*push_set
== NULL
) {
990 anv_batch_set_error(&cmd_buffer
->batch
, VK_ERROR_OUT_OF_HOST_MEMORY
);
995 struct anv_descriptor_set
*set
= &(*push_set
)->set
;
997 if (set
->layout
!= layout
) {
999 anv_descriptor_set_layout_unref(cmd_buffer
->device
, set
->layout
);
1000 anv_descriptor_set_layout_ref(layout
);
1001 set
->layout
= layout
;
1003 set
->size
= anv_descriptor_set_layout_size(layout
);
1004 set
->buffer_view_count
= layout
->buffer_view_count
;
1005 set
->buffer_views
= (*push_set
)->buffer_views
;
1007 if (layout
->descriptor_buffer_size
&&
1008 ((*push_set
)->set_used_on_gpu
||
1009 set
->desc_mem
.alloc_size
< layout
->descriptor_buffer_size
)) {
1010 /* The previous buffer is either actively used by some GPU command (so
1011 * we can't modify it) or is too small. Allocate a new one.
1013 struct anv_state desc_mem
=
1014 anv_state_stream_alloc(&cmd_buffer
->dynamic_state_stream
,
1015 layout
->descriptor_buffer_size
, 32);
1016 if (set
->desc_mem
.alloc_size
) {
1017 /* TODO: Do we really need to copy all the time? */
1018 memcpy(desc_mem
.map
, set
->desc_mem
.map
,
1019 MIN2(desc_mem
.alloc_size
, set
->desc_mem
.alloc_size
));
1021 set
->desc_mem
= desc_mem
;
1023 struct anv_address addr
= {
1024 .bo
= cmd_buffer
->dynamic_state_stream
.state_pool
->block_pool
.bo
,
1025 .offset
= set
->desc_mem
.offset
,
1028 const struct isl_device
*isl_dev
= &cmd_buffer
->device
->isl_dev
;
1029 set
->desc_surface_state
=
1030 anv_state_stream_alloc(&cmd_buffer
->surface_state_stream
,
1031 isl_dev
->ss
.size
, isl_dev
->ss
.align
);
1032 anv_fill_buffer_surface_state(cmd_buffer
->device
,
1033 set
->desc_surface_state
,
1034 ISL_FORMAT_R32G32B32A32_FLOAT
,
1035 addr
, layout
->descriptor_buffer_size
, 1);
1041 void anv_CmdPushDescriptorSetKHR(
1042 VkCommandBuffer commandBuffer
,
1043 VkPipelineBindPoint pipelineBindPoint
,
1044 VkPipelineLayout _layout
,
1046 uint32_t descriptorWriteCount
,
1047 const VkWriteDescriptorSet
* pDescriptorWrites
)
1049 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1050 ANV_FROM_HANDLE(anv_pipeline_layout
, layout
, _layout
);
1052 assert(_set
< MAX_SETS
);
1054 struct anv_descriptor_set_layout
*set_layout
= layout
->set
[_set
].layout
;
1056 struct anv_descriptor_set
*set
=
1057 anv_cmd_buffer_push_descriptor_set(cmd_buffer
, pipelineBindPoint
,
1062 /* Go through the user supplied descriptors. */
1063 for (uint32_t i
= 0; i
< descriptorWriteCount
; i
++) {
1064 const VkWriteDescriptorSet
*write
= &pDescriptorWrites
[i
];
1066 switch (write
->descriptorType
) {
1067 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1068 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1069 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1070 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1071 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
1072 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
1073 anv_descriptor_set_write_image_view(cmd_buffer
->device
, set
,
1074 write
->pImageInfo
+ j
,
1075 write
->descriptorType
,
1077 write
->dstArrayElement
+ j
);
1081 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1082 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1083 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
1084 ANV_FROM_HANDLE(anv_buffer_view
, bview
,
1085 write
->pTexelBufferView
[j
]);
1087 anv_descriptor_set_write_buffer_view(cmd_buffer
->device
, set
,
1088 write
->descriptorType
,
1091 write
->dstArrayElement
+ j
);
1095 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1096 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1097 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1098 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1099 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
1100 assert(write
->pBufferInfo
[j
].buffer
);
1101 ANV_FROM_HANDLE(anv_buffer
, buffer
, write
->pBufferInfo
[j
].buffer
);
1104 anv_descriptor_set_write_buffer(cmd_buffer
->device
, set
,
1105 &cmd_buffer
->surface_state_stream
,
1106 write
->descriptorType
,
1109 write
->dstArrayElement
+ j
,
1110 write
->pBufferInfo
[j
].offset
,
1111 write
->pBufferInfo
[j
].range
);
1120 anv_cmd_buffer_bind_descriptor_set(cmd_buffer
, pipelineBindPoint
,
1121 layout
, _set
, set
, NULL
, NULL
);
1124 void anv_CmdPushDescriptorSetWithTemplateKHR(
1125 VkCommandBuffer commandBuffer
,
1126 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
1127 VkPipelineLayout _layout
,
1131 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1132 ANV_FROM_HANDLE(anv_descriptor_update_template
, template,
1133 descriptorUpdateTemplate
);
1134 ANV_FROM_HANDLE(anv_pipeline_layout
, layout
, _layout
);
1136 assert(_set
< MAX_PUSH_DESCRIPTORS
);
1138 struct anv_descriptor_set_layout
*set_layout
= layout
->set
[_set
].layout
;
1140 struct anv_descriptor_set
*set
=
1141 anv_cmd_buffer_push_descriptor_set(cmd_buffer
, template->bind_point
,
1146 anv_descriptor_set_write_template(cmd_buffer
->device
, set
,
1147 &cmd_buffer
->surface_state_stream
,
1151 anv_cmd_buffer_bind_descriptor_set(cmd_buffer
, template->bind_point
,
1152 layout
, _set
, set
, NULL
, NULL
);
1155 void anv_CmdSetDeviceMask(
1156 VkCommandBuffer commandBuffer
,
1157 uint32_t deviceMask
)