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 anv_dynamic_state_copy(struct anv_dynamic_state
*dest
,
83 const struct anv_dynamic_state
*src
,
84 anv_cmd_dirty_mask_t copy_mask
)
86 if (copy_mask
& ANV_CMD_DIRTY_DYNAMIC_VIEWPORT
) {
87 dest
->viewport
.count
= src
->viewport
.count
;
88 typed_memcpy(dest
->viewport
.viewports
, src
->viewport
.viewports
,
92 if (copy_mask
& ANV_CMD_DIRTY_DYNAMIC_SCISSOR
) {
93 dest
->scissor
.count
= src
->scissor
.count
;
94 typed_memcpy(dest
->scissor
.scissors
, src
->scissor
.scissors
,
98 if (copy_mask
& ANV_CMD_DIRTY_DYNAMIC_LINE_WIDTH
)
99 dest
->line_width
= src
->line_width
;
101 if (copy_mask
& ANV_CMD_DIRTY_DYNAMIC_DEPTH_BIAS
)
102 dest
->depth_bias
= src
->depth_bias
;
104 if (copy_mask
& ANV_CMD_DIRTY_DYNAMIC_BLEND_CONSTANTS
)
105 typed_memcpy(dest
->blend_constants
, src
->blend_constants
, 4);
107 if (copy_mask
& ANV_CMD_DIRTY_DYNAMIC_DEPTH_BOUNDS
)
108 dest
->depth_bounds
= src
->depth_bounds
;
110 if (copy_mask
& ANV_CMD_DIRTY_DYNAMIC_STENCIL_COMPARE_MASK
)
111 dest
->stencil_compare_mask
= src
->stencil_compare_mask
;
113 if (copy_mask
& ANV_CMD_DIRTY_DYNAMIC_STENCIL_WRITE_MASK
)
114 dest
->stencil_write_mask
= src
->stencil_write_mask
;
116 if (copy_mask
& ANV_CMD_DIRTY_DYNAMIC_STENCIL_REFERENCE
)
117 dest
->stencil_reference
= src
->stencil_reference
;
119 if (copy_mask
& ANV_CMD_DIRTY_DYNAMIC_LINE_STIPPLE
)
120 dest
->line_stipple
= src
->line_stipple
;
124 anv_cmd_state_init(struct anv_cmd_buffer
*cmd_buffer
)
126 struct anv_cmd_state
*state
= &cmd_buffer
->state
;
128 memset(state
, 0, sizeof(*state
));
130 state
->current_pipeline
= UINT32_MAX
;
131 state
->restart_index
= UINT32_MAX
;
132 state
->gfx
.dynamic
= default_dynamic_state
;
136 anv_cmd_pipeline_state_finish(struct anv_cmd_buffer
*cmd_buffer
,
137 struct anv_cmd_pipeline_state
*pipe_state
)
139 for (uint32_t i
= 0; i
< ARRAY_SIZE(pipe_state
->push_descriptors
); i
++) {
140 if (pipe_state
->push_descriptors
[i
]) {
141 anv_descriptor_set_layout_unref(cmd_buffer
->device
,
142 pipe_state
->push_descriptors
[i
]->set
.layout
);
143 vk_free(&cmd_buffer
->pool
->alloc
, pipe_state
->push_descriptors
[i
]);
149 anv_cmd_state_finish(struct anv_cmd_buffer
*cmd_buffer
)
151 struct anv_cmd_state
*state
= &cmd_buffer
->state
;
153 anv_cmd_pipeline_state_finish(cmd_buffer
, &state
->gfx
.base
);
154 anv_cmd_pipeline_state_finish(cmd_buffer
, &state
->compute
.base
);
156 vk_free(&cmd_buffer
->pool
->alloc
, state
->attachments
);
160 anv_cmd_state_reset(struct anv_cmd_buffer
*cmd_buffer
)
162 anv_cmd_state_finish(cmd_buffer
);
163 anv_cmd_state_init(cmd_buffer
);
166 static VkResult
anv_create_cmd_buffer(
167 struct anv_device
* device
,
168 struct anv_cmd_pool
* pool
,
169 VkCommandBufferLevel level
,
170 VkCommandBuffer
* pCommandBuffer
)
172 struct anv_cmd_buffer
*cmd_buffer
;
175 cmd_buffer
= vk_alloc(&pool
->alloc
, sizeof(*cmd_buffer
), 8,
176 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
177 if (cmd_buffer
== NULL
)
178 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
180 cmd_buffer
->batch
.status
= VK_SUCCESS
;
182 cmd_buffer
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
183 cmd_buffer
->device
= device
;
184 cmd_buffer
->pool
= pool
;
185 cmd_buffer
->level
= level
;
187 result
= anv_cmd_buffer_init_batch_bo_chain(cmd_buffer
);
188 if (result
!= VK_SUCCESS
)
191 anv_state_stream_init(&cmd_buffer
->surface_state_stream
,
192 &device
->surface_state_pool
, 4096);
193 anv_state_stream_init(&cmd_buffer
->dynamic_state_stream
,
194 &device
->dynamic_state_pool
, 16384);
196 anv_cmd_state_init(cmd_buffer
);
199 list_addtail(&cmd_buffer
->pool_link
, &pool
->cmd_buffers
);
201 /* Init the pool_link so we can safefly call list_del when we destroy
204 list_inithead(&cmd_buffer
->pool_link
);
207 *pCommandBuffer
= anv_cmd_buffer_to_handle(cmd_buffer
);
212 vk_free(&cmd_buffer
->pool
->alloc
, cmd_buffer
);
217 VkResult
anv_AllocateCommandBuffers(
219 const VkCommandBufferAllocateInfo
* pAllocateInfo
,
220 VkCommandBuffer
* pCommandBuffers
)
222 ANV_FROM_HANDLE(anv_device
, device
, _device
);
223 ANV_FROM_HANDLE(anv_cmd_pool
, pool
, pAllocateInfo
->commandPool
);
225 VkResult result
= VK_SUCCESS
;
228 for (i
= 0; i
< pAllocateInfo
->commandBufferCount
; i
++) {
229 result
= anv_create_cmd_buffer(device
, pool
, pAllocateInfo
->level
,
230 &pCommandBuffers
[i
]);
231 if (result
!= VK_SUCCESS
)
235 if (result
!= VK_SUCCESS
) {
236 anv_FreeCommandBuffers(_device
, pAllocateInfo
->commandPool
,
238 for (i
= 0; i
< pAllocateInfo
->commandBufferCount
; i
++)
239 pCommandBuffers
[i
] = VK_NULL_HANDLE
;
246 anv_cmd_buffer_destroy(struct anv_cmd_buffer
*cmd_buffer
)
248 list_del(&cmd_buffer
->pool_link
);
250 anv_cmd_buffer_fini_batch_bo_chain(cmd_buffer
);
252 anv_state_stream_finish(&cmd_buffer
->surface_state_stream
);
253 anv_state_stream_finish(&cmd_buffer
->dynamic_state_stream
);
255 anv_cmd_state_finish(cmd_buffer
);
257 vk_free(&cmd_buffer
->pool
->alloc
, cmd_buffer
);
260 void anv_FreeCommandBuffers(
262 VkCommandPool commandPool
,
263 uint32_t commandBufferCount
,
264 const VkCommandBuffer
* pCommandBuffers
)
266 for (uint32_t i
= 0; i
< commandBufferCount
; i
++) {
267 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, pCommandBuffers
[i
]);
272 anv_cmd_buffer_destroy(cmd_buffer
);
277 anv_cmd_buffer_reset(struct anv_cmd_buffer
*cmd_buffer
)
279 cmd_buffer
->usage_flags
= 0;
280 anv_cmd_buffer_reset_batch_bo_chain(cmd_buffer
);
281 anv_cmd_state_reset(cmd_buffer
);
283 anv_state_stream_finish(&cmd_buffer
->surface_state_stream
);
284 anv_state_stream_init(&cmd_buffer
->surface_state_stream
,
285 &cmd_buffer
->device
->surface_state_pool
, 4096);
287 anv_state_stream_finish(&cmd_buffer
->dynamic_state_stream
);
288 anv_state_stream_init(&cmd_buffer
->dynamic_state_stream
,
289 &cmd_buffer
->device
->dynamic_state_pool
, 16384);
293 VkResult
anv_ResetCommandBuffer(
294 VkCommandBuffer commandBuffer
,
295 VkCommandBufferResetFlags flags
)
297 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
298 return anv_cmd_buffer_reset(cmd_buffer
);
301 #define anv_genX_call(devinfo, func, ...) \
302 switch ((devinfo)->gen) { \
304 if ((devinfo)->is_haswell) { \
305 gen75_##func(__VA_ARGS__); \
307 gen7_##func(__VA_ARGS__); \
311 gen8_##func(__VA_ARGS__); \
314 gen9_##func(__VA_ARGS__); \
317 gen10_##func(__VA_ARGS__); \
320 gen11_##func(__VA_ARGS__); \
323 assert(!"Unknown hardware generation"); \
327 anv_cmd_buffer_emit_state_base_address(struct anv_cmd_buffer
*cmd_buffer
)
329 anv_genX_call(&cmd_buffer
->device
->info
,
330 cmd_buffer_emit_state_base_address
,
335 anv_cmd_buffer_mark_image_written(struct anv_cmd_buffer
*cmd_buffer
,
336 const struct anv_image
*image
,
337 VkImageAspectFlagBits aspect
,
338 enum isl_aux_usage aux_usage
,
341 uint32_t layer_count
)
343 anv_genX_call(&cmd_buffer
->device
->info
,
344 cmd_buffer_mark_image_written
,
345 cmd_buffer
, image
, aspect
, aux_usage
,
346 level
, base_layer
, layer_count
);
350 anv_cmd_emit_conditional_render_predicate(struct anv_cmd_buffer
*cmd_buffer
)
352 anv_genX_call(&cmd_buffer
->device
->info
,
353 cmd_emit_conditional_render_predicate
,
357 void anv_CmdBindPipeline(
358 VkCommandBuffer commandBuffer
,
359 VkPipelineBindPoint pipelineBindPoint
,
360 VkPipeline _pipeline
)
362 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
363 ANV_FROM_HANDLE(anv_pipeline
, pipeline
, _pipeline
);
365 switch (pipelineBindPoint
) {
366 case VK_PIPELINE_BIND_POINT_COMPUTE
:
367 cmd_buffer
->state
.compute
.base
.pipeline
= pipeline
;
368 cmd_buffer
->state
.compute
.pipeline_dirty
= true;
369 cmd_buffer
->state
.push_constants_dirty
|= VK_SHADER_STAGE_COMPUTE_BIT
;
370 cmd_buffer
->state
.descriptors_dirty
|= VK_SHADER_STAGE_COMPUTE_BIT
;
373 case VK_PIPELINE_BIND_POINT_GRAPHICS
:
374 cmd_buffer
->state
.gfx
.base
.pipeline
= pipeline
;
375 cmd_buffer
->state
.gfx
.vb_dirty
|= pipeline
->vb_used
;
376 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_PIPELINE
;
377 cmd_buffer
->state
.push_constants_dirty
|= pipeline
->active_stages
;
378 cmd_buffer
->state
.descriptors_dirty
|= pipeline
->active_stages
;
380 /* Apply the dynamic state from the pipeline */
381 cmd_buffer
->state
.gfx
.dirty
|= pipeline
->dynamic_state_mask
;
382 anv_dynamic_state_copy(&cmd_buffer
->state
.gfx
.dynamic
,
383 &pipeline
->dynamic_state
,
384 pipeline
->dynamic_state_mask
);
388 assert(!"invalid bind point");
393 void anv_CmdSetViewport(
394 VkCommandBuffer commandBuffer
,
395 uint32_t firstViewport
,
396 uint32_t viewportCount
,
397 const VkViewport
* pViewports
)
399 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
401 const uint32_t total_count
= firstViewport
+ viewportCount
;
402 if (cmd_buffer
->state
.gfx
.dynamic
.viewport
.count
< total_count
)
403 cmd_buffer
->state
.gfx
.dynamic
.viewport
.count
= total_count
;
405 memcpy(cmd_buffer
->state
.gfx
.dynamic
.viewport
.viewports
+ firstViewport
,
406 pViewports
, viewportCount
* sizeof(*pViewports
));
408 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_VIEWPORT
;
411 void anv_CmdSetScissor(
412 VkCommandBuffer commandBuffer
,
413 uint32_t firstScissor
,
414 uint32_t scissorCount
,
415 const VkRect2D
* pScissors
)
417 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
419 const uint32_t total_count
= firstScissor
+ scissorCount
;
420 if (cmd_buffer
->state
.gfx
.dynamic
.scissor
.count
< total_count
)
421 cmd_buffer
->state
.gfx
.dynamic
.scissor
.count
= total_count
;
423 memcpy(cmd_buffer
->state
.gfx
.dynamic
.scissor
.scissors
+ firstScissor
,
424 pScissors
, scissorCount
* sizeof(*pScissors
));
426 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_SCISSOR
;
429 void anv_CmdSetLineWidth(
430 VkCommandBuffer commandBuffer
,
433 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
435 cmd_buffer
->state
.gfx
.dynamic
.line_width
= lineWidth
;
436 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_LINE_WIDTH
;
439 void anv_CmdSetDepthBias(
440 VkCommandBuffer commandBuffer
,
441 float depthBiasConstantFactor
,
442 float depthBiasClamp
,
443 float depthBiasSlopeFactor
)
445 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
447 cmd_buffer
->state
.gfx
.dynamic
.depth_bias
.bias
= depthBiasConstantFactor
;
448 cmd_buffer
->state
.gfx
.dynamic
.depth_bias
.clamp
= depthBiasClamp
;
449 cmd_buffer
->state
.gfx
.dynamic
.depth_bias
.slope
= depthBiasSlopeFactor
;
451 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_DEPTH_BIAS
;
454 void anv_CmdSetBlendConstants(
455 VkCommandBuffer commandBuffer
,
456 const float blendConstants
[4])
458 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
460 memcpy(cmd_buffer
->state
.gfx
.dynamic
.blend_constants
,
461 blendConstants
, sizeof(float) * 4);
463 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_BLEND_CONSTANTS
;
466 void anv_CmdSetDepthBounds(
467 VkCommandBuffer commandBuffer
,
468 float minDepthBounds
,
469 float maxDepthBounds
)
471 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
473 cmd_buffer
->state
.gfx
.dynamic
.depth_bounds
.min
= minDepthBounds
;
474 cmd_buffer
->state
.gfx
.dynamic
.depth_bounds
.max
= maxDepthBounds
;
476 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_DEPTH_BOUNDS
;
479 void anv_CmdSetStencilCompareMask(
480 VkCommandBuffer commandBuffer
,
481 VkStencilFaceFlags faceMask
,
482 uint32_t compareMask
)
484 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
486 if (faceMask
& VK_STENCIL_FACE_FRONT_BIT
)
487 cmd_buffer
->state
.gfx
.dynamic
.stencil_compare_mask
.front
= compareMask
;
488 if (faceMask
& VK_STENCIL_FACE_BACK_BIT
)
489 cmd_buffer
->state
.gfx
.dynamic
.stencil_compare_mask
.back
= compareMask
;
491 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_STENCIL_COMPARE_MASK
;
494 void anv_CmdSetStencilWriteMask(
495 VkCommandBuffer commandBuffer
,
496 VkStencilFaceFlags faceMask
,
499 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
501 if (faceMask
& VK_STENCIL_FACE_FRONT_BIT
)
502 cmd_buffer
->state
.gfx
.dynamic
.stencil_write_mask
.front
= writeMask
;
503 if (faceMask
& VK_STENCIL_FACE_BACK_BIT
)
504 cmd_buffer
->state
.gfx
.dynamic
.stencil_write_mask
.back
= writeMask
;
506 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_STENCIL_WRITE_MASK
;
509 void anv_CmdSetStencilReference(
510 VkCommandBuffer commandBuffer
,
511 VkStencilFaceFlags faceMask
,
514 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
516 if (faceMask
& VK_STENCIL_FACE_FRONT_BIT
)
517 cmd_buffer
->state
.gfx
.dynamic
.stencil_reference
.front
= reference
;
518 if (faceMask
& VK_STENCIL_FACE_BACK_BIT
)
519 cmd_buffer
->state
.gfx
.dynamic
.stencil_reference
.back
= reference
;
521 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_STENCIL_REFERENCE
;
524 void anv_CmdSetLineStippleEXT(
525 VkCommandBuffer commandBuffer
,
526 uint32_t lineStippleFactor
,
527 uint16_t lineStipplePattern
)
529 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
531 cmd_buffer
->state
.gfx
.dynamic
.line_stipple
.factor
= lineStippleFactor
;
532 cmd_buffer
->state
.gfx
.dynamic
.line_stipple
.pattern
= lineStipplePattern
;
534 cmd_buffer
->state
.gfx
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_LINE_STIPPLE
;
538 anv_cmd_buffer_bind_descriptor_set(struct anv_cmd_buffer
*cmd_buffer
,
539 VkPipelineBindPoint bind_point
,
540 struct anv_pipeline_layout
*layout
,
542 struct anv_descriptor_set
*set
,
543 uint32_t *dynamic_offset_count
,
544 const uint32_t **dynamic_offsets
)
546 struct anv_descriptor_set_layout
*set_layout
=
547 layout
->set
[set_index
].layout
;
549 struct anv_cmd_pipeline_state
*pipe_state
;
550 if (bind_point
== VK_PIPELINE_BIND_POINT_COMPUTE
) {
551 pipe_state
= &cmd_buffer
->state
.compute
.base
;
553 assert(bind_point
== VK_PIPELINE_BIND_POINT_GRAPHICS
);
554 pipe_state
= &cmd_buffer
->state
.gfx
.base
;
556 pipe_state
->descriptors
[set_index
] = set
;
558 if (dynamic_offsets
) {
559 if (set_layout
->dynamic_offset_count
> 0) {
560 uint32_t dynamic_offset_start
=
561 layout
->set
[set_index
].dynamic_offset_start
;
563 /* Assert that everything is in range */
564 assert(set_layout
->dynamic_offset_count
<= *dynamic_offset_count
);
565 assert(dynamic_offset_start
+ set_layout
->dynamic_offset_count
<=
566 ARRAY_SIZE(pipe_state
->dynamic_offsets
));
568 typed_memcpy(&pipe_state
->dynamic_offsets
[dynamic_offset_start
],
569 *dynamic_offsets
, set_layout
->dynamic_offset_count
);
571 *dynamic_offsets
+= set_layout
->dynamic_offset_count
;
572 *dynamic_offset_count
-= set_layout
->dynamic_offset_count
;
574 if (bind_point
== VK_PIPELINE_BIND_POINT_COMPUTE
) {
575 cmd_buffer
->state
.push_constants_dirty
|=
576 VK_SHADER_STAGE_COMPUTE_BIT
;
578 cmd_buffer
->state
.push_constants_dirty
|=
579 VK_SHADER_STAGE_ALL_GRAPHICS
;
584 if (bind_point
== VK_PIPELINE_BIND_POINT_COMPUTE
) {
585 cmd_buffer
->state
.descriptors_dirty
|= VK_SHADER_STAGE_COMPUTE_BIT
;
587 assert(bind_point
== VK_PIPELINE_BIND_POINT_GRAPHICS
);
588 cmd_buffer
->state
.descriptors_dirty
|=
589 set_layout
->shader_stages
& VK_SHADER_STAGE_ALL_GRAPHICS
;
592 /* Pipeline layout objects are required to live at least while any command
593 * buffers that use them are in recording state. We need to grab a reference
594 * to the pipeline layout being bound here so we can compute correct dynamic
595 * offsets for VK_DESCRIPTOR_TYPE_*_DYNAMIC in dynamic_offset_for_binding()
596 * when we record draw commands that come after this.
598 pipe_state
->layout
= layout
;
601 void anv_CmdBindDescriptorSets(
602 VkCommandBuffer commandBuffer
,
603 VkPipelineBindPoint pipelineBindPoint
,
604 VkPipelineLayout _layout
,
606 uint32_t descriptorSetCount
,
607 const VkDescriptorSet
* pDescriptorSets
,
608 uint32_t dynamicOffsetCount
,
609 const uint32_t* pDynamicOffsets
)
611 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
612 ANV_FROM_HANDLE(anv_pipeline_layout
, layout
, _layout
);
614 assert(firstSet
+ descriptorSetCount
<= MAX_SETS
);
616 for (uint32_t i
= 0; i
< descriptorSetCount
; i
++) {
617 ANV_FROM_HANDLE(anv_descriptor_set
, set
, pDescriptorSets
[i
]);
618 anv_cmd_buffer_bind_descriptor_set(cmd_buffer
, pipelineBindPoint
,
619 layout
, firstSet
+ i
, set
,
625 void anv_CmdBindVertexBuffers(
626 VkCommandBuffer commandBuffer
,
627 uint32_t firstBinding
,
628 uint32_t bindingCount
,
629 const VkBuffer
* pBuffers
,
630 const VkDeviceSize
* pOffsets
)
632 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
633 struct anv_vertex_binding
*vb
= cmd_buffer
->state
.vertex_bindings
;
635 /* We have to defer setting up vertex buffer since we need the buffer
636 * stride from the pipeline. */
638 assert(firstBinding
+ bindingCount
<= MAX_VBS
);
639 for (uint32_t i
= 0; i
< bindingCount
; i
++) {
640 vb
[firstBinding
+ i
].buffer
= anv_buffer_from_handle(pBuffers
[i
]);
641 vb
[firstBinding
+ i
].offset
= pOffsets
[i
];
642 cmd_buffer
->state
.gfx
.vb_dirty
|= 1 << (firstBinding
+ i
);
646 void anv_CmdBindTransformFeedbackBuffersEXT(
647 VkCommandBuffer commandBuffer
,
648 uint32_t firstBinding
,
649 uint32_t bindingCount
,
650 const VkBuffer
* pBuffers
,
651 const VkDeviceSize
* pOffsets
,
652 const VkDeviceSize
* pSizes
)
654 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
655 struct anv_xfb_binding
*xfb
= cmd_buffer
->state
.xfb_bindings
;
657 /* We have to defer setting up vertex buffer since we need the buffer
658 * stride from the pipeline. */
660 assert(firstBinding
+ bindingCount
<= MAX_XFB_BUFFERS
);
661 for (uint32_t i
= 0; i
< bindingCount
; i
++) {
662 if (pBuffers
[i
] == VK_NULL_HANDLE
) {
663 xfb
[firstBinding
+ i
].buffer
= NULL
;
665 ANV_FROM_HANDLE(anv_buffer
, buffer
, pBuffers
[i
]);
666 xfb
[firstBinding
+ i
].buffer
= buffer
;
667 xfb
[firstBinding
+ i
].offset
= pOffsets
[i
];
668 xfb
[firstBinding
+ i
].size
=
669 anv_buffer_get_range(buffer
, pOffsets
[i
],
670 pSizes
? pSizes
[i
] : VK_WHOLE_SIZE
);
676 anv_isl_format_for_descriptor_type(VkDescriptorType type
)
679 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
680 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
681 return ISL_FORMAT_R32G32B32A32_FLOAT
;
683 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
684 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
685 return ISL_FORMAT_RAW
;
688 unreachable("Invalid descriptor type");
693 anv_cmd_buffer_emit_dynamic(struct anv_cmd_buffer
*cmd_buffer
,
694 const void *data
, uint32_t size
, uint32_t alignment
)
696 struct anv_state state
;
698 state
= anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, size
, alignment
);
699 memcpy(state
.map
, data
, size
);
701 VG(VALGRIND_CHECK_MEM_IS_DEFINED(state
.map
, size
));
707 anv_cmd_buffer_merge_dynamic(struct anv_cmd_buffer
*cmd_buffer
,
708 uint32_t *a
, uint32_t *b
,
709 uint32_t dwords
, uint32_t alignment
)
711 struct anv_state state
;
714 state
= anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
,
715 dwords
* 4, alignment
);
717 for (uint32_t i
= 0; i
< dwords
; i
++)
720 VG(VALGRIND_CHECK_MEM_IS_DEFINED(p
, dwords
* 4));
726 anv_push_constant_value(const struct anv_cmd_pipeline_state
*state
,
727 const struct anv_push_constants
*data
, uint32_t param
)
729 if (BRW_PARAM_IS_BUILTIN(param
)) {
731 case BRW_PARAM_BUILTIN_ZERO
:
733 case BRW_PARAM_BUILTIN_BASE_WORK_GROUP_ID_X
:
734 return data
->base_work_group_id
[0];
735 case BRW_PARAM_BUILTIN_BASE_WORK_GROUP_ID_Y
:
736 return data
->base_work_group_id
[1];
737 case BRW_PARAM_BUILTIN_BASE_WORK_GROUP_ID_Z
:
738 return data
->base_work_group_id
[2];
740 unreachable("Invalid param builtin");
742 } else if (ANV_PARAM_IS_PUSH(param
)) {
743 uint32_t offset
= ANV_PARAM_PUSH_OFFSET(param
);
744 assert(offset
% sizeof(uint32_t) == 0);
745 if (offset
< sizeof(data
->client_data
))
746 return *(uint32_t *)((uint8_t *)data
+ offset
);
749 } else if (ANV_PARAM_IS_DYN_OFFSET(param
)) {
750 unsigned idx
= ANV_PARAM_DYN_OFFSET_IDX(param
);
751 assert(idx
< MAX_DYNAMIC_BUFFERS
);
752 return state
->dynamic_offsets
[idx
];
755 assert(!"Invalid param");
760 anv_cmd_buffer_push_constants(struct anv_cmd_buffer
*cmd_buffer
,
761 gl_shader_stage stage
)
763 struct anv_cmd_pipeline_state
*pipeline_state
= &cmd_buffer
->state
.gfx
.base
;
764 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.gfx
.base
.pipeline
;
766 /* If we don't have this stage, bail. */
767 if (!anv_pipeline_has_stage(pipeline
, stage
))
768 return (struct anv_state
) { .offset
= 0 };
770 struct anv_push_constants
*data
=
771 &cmd_buffer
->state
.push_constants
[stage
];
772 const struct brw_stage_prog_data
*prog_data
=
773 pipeline
->shaders
[stage
]->prog_data
;
775 /* If we don't actually have any push constants, bail. */
776 if (prog_data
== NULL
|| prog_data
->nr_params
== 0)
777 return (struct anv_state
) { .offset
= 0 };
779 struct anv_state state
=
780 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
,
781 prog_data
->nr_params
* sizeof(float),
782 32 /* bottom 5 bits MBZ */);
784 /* Walk through the param array and fill the buffer with data */
785 uint32_t *u32_map
= state
.map
;
786 for (unsigned i
= 0; i
< prog_data
->nr_params
; i
++) {
787 u32_map
[i
] = anv_push_constant_value(pipeline_state
, data
,
788 prog_data
->param
[i
]);
795 anv_cmd_buffer_cs_push_constants(struct anv_cmd_buffer
*cmd_buffer
)
797 struct anv_cmd_pipeline_state
*pipeline_state
= &cmd_buffer
->state
.compute
.base
;
798 struct anv_push_constants
*data
=
799 &cmd_buffer
->state
.push_constants
[MESA_SHADER_COMPUTE
];
800 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute
.base
.pipeline
;
801 const struct brw_cs_prog_data
*cs_prog_data
= get_cs_prog_data(pipeline
);
802 const struct brw_stage_prog_data
*prog_data
= &cs_prog_data
->base
;
804 /* If we don't actually have any push constants, bail. */
805 if (cs_prog_data
->push
.total
.size
== 0)
806 return (struct anv_state
) { .offset
= 0 };
808 const unsigned push_constant_alignment
=
809 cmd_buffer
->device
->info
.gen
< 8 ? 32 : 64;
810 const unsigned aligned_total_push_constants_size
=
811 ALIGN(cs_prog_data
->push
.total
.size
, push_constant_alignment
);
812 struct anv_state state
=
813 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
,
814 aligned_total_push_constants_size
,
815 push_constant_alignment
);
817 /* Walk through the param array and fill the buffer with data */
818 uint32_t *u32_map
= state
.map
;
820 if (cs_prog_data
->push
.cross_thread
.size
> 0) {
822 i
< cs_prog_data
->push
.cross_thread
.dwords
;
824 assert(prog_data
->param
[i
] != BRW_PARAM_BUILTIN_SUBGROUP_ID
);
825 u32_map
[i
] = anv_push_constant_value(pipeline_state
, data
,
826 prog_data
->param
[i
]);
830 if (cs_prog_data
->push
.per_thread
.size
> 0) {
831 for (unsigned t
= 0; t
< cs_prog_data
->threads
; t
++) {
833 8 * (cs_prog_data
->push
.per_thread
.regs
* t
+
834 cs_prog_data
->push
.cross_thread
.regs
);
835 unsigned src
= cs_prog_data
->push
.cross_thread
.dwords
;
836 for ( ; src
< prog_data
->nr_params
; src
++, dst
++) {
837 if (prog_data
->param
[src
] == BRW_PARAM_BUILTIN_SUBGROUP_ID
) {
840 u32_map
[dst
] = anv_push_constant_value(pipeline_state
, data
,
841 prog_data
->param
[src
]);
850 void anv_CmdPushConstants(
851 VkCommandBuffer commandBuffer
,
852 VkPipelineLayout layout
,
853 VkShaderStageFlags stageFlags
,
858 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
860 anv_foreach_stage(stage
, stageFlags
) {
861 memcpy(cmd_buffer
->state
.push_constants
[stage
].client_data
+ offset
,
865 cmd_buffer
->state
.push_constants_dirty
|= stageFlags
;
868 VkResult
anv_CreateCommandPool(
870 const VkCommandPoolCreateInfo
* pCreateInfo
,
871 const VkAllocationCallbacks
* pAllocator
,
872 VkCommandPool
* pCmdPool
)
874 ANV_FROM_HANDLE(anv_device
, device
, _device
);
875 struct anv_cmd_pool
*pool
;
877 pool
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*pool
), 8,
878 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
880 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
883 pool
->alloc
= *pAllocator
;
885 pool
->alloc
= device
->alloc
;
887 list_inithead(&pool
->cmd_buffers
);
889 *pCmdPool
= anv_cmd_pool_to_handle(pool
);
894 void anv_DestroyCommandPool(
896 VkCommandPool commandPool
,
897 const VkAllocationCallbacks
* pAllocator
)
899 ANV_FROM_HANDLE(anv_device
, device
, _device
);
900 ANV_FROM_HANDLE(anv_cmd_pool
, pool
, commandPool
);
905 list_for_each_entry_safe(struct anv_cmd_buffer
, cmd_buffer
,
906 &pool
->cmd_buffers
, pool_link
) {
907 anv_cmd_buffer_destroy(cmd_buffer
);
910 vk_free2(&device
->alloc
, pAllocator
, pool
);
913 VkResult
anv_ResetCommandPool(
915 VkCommandPool commandPool
,
916 VkCommandPoolResetFlags flags
)
918 ANV_FROM_HANDLE(anv_cmd_pool
, pool
, commandPool
);
920 list_for_each_entry(struct anv_cmd_buffer
, cmd_buffer
,
921 &pool
->cmd_buffers
, pool_link
) {
922 anv_cmd_buffer_reset(cmd_buffer
);
928 void anv_TrimCommandPool(
930 VkCommandPool commandPool
,
931 VkCommandPoolTrimFlags flags
)
933 /* Nothing for us to do here. Our pools stay pretty tidy. */
937 * Return NULL if the current subpass has no depthstencil attachment.
939 const struct anv_image_view
*
940 anv_cmd_buffer_get_depth_stencil_view(const struct anv_cmd_buffer
*cmd_buffer
)
942 const struct anv_subpass
*subpass
= cmd_buffer
->state
.subpass
;
944 if (subpass
->depth_stencil_attachment
== NULL
)
947 const struct anv_image_view
*iview
=
948 cmd_buffer
->state
.attachments
[subpass
->depth_stencil_attachment
->attachment
].image_view
;
950 assert(iview
->aspect_mask
& (VK_IMAGE_ASPECT_DEPTH_BIT
|
951 VK_IMAGE_ASPECT_STENCIL_BIT
));
956 static struct anv_descriptor_set
*
957 anv_cmd_buffer_push_descriptor_set(struct anv_cmd_buffer
*cmd_buffer
,
958 VkPipelineBindPoint bind_point
,
959 struct anv_descriptor_set_layout
*layout
,
962 struct anv_cmd_pipeline_state
*pipe_state
;
963 if (bind_point
== VK_PIPELINE_BIND_POINT_COMPUTE
) {
964 pipe_state
= &cmd_buffer
->state
.compute
.base
;
966 assert(bind_point
== VK_PIPELINE_BIND_POINT_GRAPHICS
);
967 pipe_state
= &cmd_buffer
->state
.gfx
.base
;
970 struct anv_push_descriptor_set
**push_set
=
971 &pipe_state
->push_descriptors
[_set
];
973 if (*push_set
== NULL
) {
974 *push_set
= vk_zalloc(&cmd_buffer
->pool
->alloc
,
975 sizeof(struct anv_push_descriptor_set
), 8,
976 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
977 if (*push_set
== NULL
) {
978 anv_batch_set_error(&cmd_buffer
->batch
, VK_ERROR_OUT_OF_HOST_MEMORY
);
983 struct anv_descriptor_set
*set
= &(*push_set
)->set
;
985 if (set
->layout
!= layout
) {
987 anv_descriptor_set_layout_unref(cmd_buffer
->device
, set
->layout
);
988 anv_descriptor_set_layout_ref(layout
);
989 set
->layout
= layout
;
991 set
->size
= anv_descriptor_set_layout_size(layout
);
992 set
->buffer_view_count
= layout
->buffer_view_count
;
993 set
->buffer_views
= (*push_set
)->buffer_views
;
995 if (layout
->descriptor_buffer_size
&&
996 ((*push_set
)->set_used_on_gpu
||
997 set
->desc_mem
.alloc_size
< layout
->descriptor_buffer_size
)) {
998 /* The previous buffer is either actively used by some GPU command (so
999 * we can't modify it) or is too small. Allocate a new one.
1001 struct anv_state desc_mem
=
1002 anv_state_stream_alloc(&cmd_buffer
->dynamic_state_stream
,
1003 layout
->descriptor_buffer_size
, 32);
1004 if (set
->desc_mem
.alloc_size
) {
1005 /* TODO: Do we really need to copy all the time? */
1006 memcpy(desc_mem
.map
, set
->desc_mem
.map
,
1007 MIN2(desc_mem
.alloc_size
, set
->desc_mem
.alloc_size
));
1009 set
->desc_mem
= desc_mem
;
1011 struct anv_address addr
= {
1012 .bo
= cmd_buffer
->dynamic_state_stream
.state_pool
->block_pool
.bo
,
1013 .offset
= set
->desc_mem
.offset
,
1016 const struct isl_device
*isl_dev
= &cmd_buffer
->device
->isl_dev
;
1017 set
->desc_surface_state
=
1018 anv_state_stream_alloc(&cmd_buffer
->surface_state_stream
,
1019 isl_dev
->ss
.size
, isl_dev
->ss
.align
);
1020 anv_fill_buffer_surface_state(cmd_buffer
->device
,
1021 set
->desc_surface_state
,
1022 ISL_FORMAT_R32G32B32A32_FLOAT
,
1023 addr
, layout
->descriptor_buffer_size
, 1);
1029 void anv_CmdPushDescriptorSetKHR(
1030 VkCommandBuffer commandBuffer
,
1031 VkPipelineBindPoint pipelineBindPoint
,
1032 VkPipelineLayout _layout
,
1034 uint32_t descriptorWriteCount
,
1035 const VkWriteDescriptorSet
* pDescriptorWrites
)
1037 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1038 ANV_FROM_HANDLE(anv_pipeline_layout
, layout
, _layout
);
1040 assert(_set
< MAX_SETS
);
1042 struct anv_descriptor_set_layout
*set_layout
= layout
->set
[_set
].layout
;
1044 struct anv_descriptor_set
*set
=
1045 anv_cmd_buffer_push_descriptor_set(cmd_buffer
, pipelineBindPoint
,
1050 /* Go through the user supplied descriptors. */
1051 for (uint32_t i
= 0; i
< descriptorWriteCount
; i
++) {
1052 const VkWriteDescriptorSet
*write
= &pDescriptorWrites
[i
];
1054 switch (write
->descriptorType
) {
1055 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1056 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1057 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1058 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1059 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
1060 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
1061 anv_descriptor_set_write_image_view(cmd_buffer
->device
, set
,
1062 write
->pImageInfo
+ j
,
1063 write
->descriptorType
,
1065 write
->dstArrayElement
+ j
);
1069 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1070 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1071 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
1072 ANV_FROM_HANDLE(anv_buffer_view
, bview
,
1073 write
->pTexelBufferView
[j
]);
1075 anv_descriptor_set_write_buffer_view(cmd_buffer
->device
, set
,
1076 write
->descriptorType
,
1079 write
->dstArrayElement
+ j
);
1083 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1084 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1085 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1086 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1087 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
1088 assert(write
->pBufferInfo
[j
].buffer
);
1089 ANV_FROM_HANDLE(anv_buffer
, buffer
, write
->pBufferInfo
[j
].buffer
);
1092 anv_descriptor_set_write_buffer(cmd_buffer
->device
, set
,
1093 &cmd_buffer
->surface_state_stream
,
1094 write
->descriptorType
,
1097 write
->dstArrayElement
+ j
,
1098 write
->pBufferInfo
[j
].offset
,
1099 write
->pBufferInfo
[j
].range
);
1108 anv_cmd_buffer_bind_descriptor_set(cmd_buffer
, pipelineBindPoint
,
1109 layout
, _set
, set
, NULL
, NULL
);
1112 void anv_CmdPushDescriptorSetWithTemplateKHR(
1113 VkCommandBuffer commandBuffer
,
1114 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
1115 VkPipelineLayout _layout
,
1119 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1120 ANV_FROM_HANDLE(anv_descriptor_update_template
, template,
1121 descriptorUpdateTemplate
);
1122 ANV_FROM_HANDLE(anv_pipeline_layout
, layout
, _layout
);
1124 assert(_set
< MAX_PUSH_DESCRIPTORS
);
1126 struct anv_descriptor_set_layout
*set_layout
= layout
->set
[_set
].layout
;
1128 struct anv_descriptor_set
*set
=
1129 anv_cmd_buffer_push_descriptor_set(cmd_buffer
, template->bind_point
,
1134 anv_descriptor_set_write_template(cmd_buffer
->device
, set
,
1135 &cmd_buffer
->surface_state_stream
,
1139 anv_cmd_buffer_bind_descriptor_set(cmd_buffer
, template->bind_point
,
1140 layout
, _set
, set
, NULL
, NULL
);
1143 void anv_CmdSetDeviceMask(
1144 VkCommandBuffer commandBuffer
,
1145 uint32_t deviceMask
)