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"
34 /** \file anv_cmd_buffer.c
36 * This file contains all of the stuff for emitting commands into a command
37 * buffer. This includes implementations of most of the vkCmd*
38 * entrypoints. This file is concerned entirely with state emission and
39 * not with the command buffer data structure itself. As far as this file
40 * is concerned, most of anv_cmd_buffer is magic.
43 /* TODO: These are taken from GLES. We should check the Vulkan spec */
44 const struct anv_dynamic_state default_dynamic_state
= {
57 .blend_constants
= { 0.0f
, 0.0f
, 0.0f
, 0.0f
},
62 .stencil_compare_mask
= {
66 .stencil_write_mask
= {
70 .stencil_reference
= {
77 anv_dynamic_state_copy(struct anv_dynamic_state
*dest
,
78 const struct anv_dynamic_state
*src
,
81 if (copy_mask
& (1 << VK_DYNAMIC_STATE_VIEWPORT
)) {
82 dest
->viewport
.count
= src
->viewport
.count
;
83 typed_memcpy(dest
->viewport
.viewports
, src
->viewport
.viewports
,
87 if (copy_mask
& (1 << VK_DYNAMIC_STATE_SCISSOR
)) {
88 dest
->scissor
.count
= src
->scissor
.count
;
89 typed_memcpy(dest
->scissor
.scissors
, src
->scissor
.scissors
,
93 if (copy_mask
& (1 << VK_DYNAMIC_STATE_LINE_WIDTH
))
94 dest
->line_width
= src
->line_width
;
96 if (copy_mask
& (1 << VK_DYNAMIC_STATE_DEPTH_BIAS
))
97 dest
->depth_bias
= src
->depth_bias
;
99 if (copy_mask
& (1 << VK_DYNAMIC_STATE_BLEND_CONSTANTS
))
100 typed_memcpy(dest
->blend_constants
, src
->blend_constants
, 4);
102 if (copy_mask
& (1 << VK_DYNAMIC_STATE_DEPTH_BOUNDS
))
103 dest
->depth_bounds
= src
->depth_bounds
;
105 if (copy_mask
& (1 << VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK
))
106 dest
->stencil_compare_mask
= src
->stencil_compare_mask
;
108 if (copy_mask
& (1 << VK_DYNAMIC_STATE_STENCIL_WRITE_MASK
))
109 dest
->stencil_write_mask
= src
->stencil_write_mask
;
111 if (copy_mask
& (1 << VK_DYNAMIC_STATE_STENCIL_REFERENCE
))
112 dest
->stencil_reference
= src
->stencil_reference
;
116 anv_cmd_state_reset(struct anv_cmd_buffer
*cmd_buffer
)
118 struct anv_cmd_state
*state
= &cmd_buffer
->state
;
120 cmd_buffer
->batch
.status
= VK_SUCCESS
;
122 memset(&state
->descriptors
, 0, sizeof(state
->descriptors
));
123 for (uint32_t i
= 0; i
< ARRAY_SIZE(state
->push_descriptors
); i
++) {
124 vk_free(&cmd_buffer
->pool
->alloc
, state
->push_descriptors
[i
]);
125 state
->push_descriptors
[i
] = NULL
;
127 for (uint32_t i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
128 vk_free(&cmd_buffer
->pool
->alloc
, state
->push_constants
[i
]);
129 state
->push_constants
[i
] = NULL
;
131 memset(state
->binding_tables
, 0, sizeof(state
->binding_tables
));
132 memset(state
->samplers
, 0, sizeof(state
->samplers
));
134 /* 0 isn't a valid config. This ensures that we always configure L3$. */
135 cmd_buffer
->state
.current_l3_config
= 0;
139 state
->pending_pipe_bits
= 0;
140 state
->descriptors_dirty
= 0;
141 state
->push_constants_dirty
= 0;
142 state
->pipeline
= NULL
;
143 state
->framebuffer
= NULL
;
145 state
->subpass
= NULL
;
146 state
->push_constant_stages
= 0;
147 state
->restart_index
= UINT32_MAX
;
148 state
->dynamic
= default_dynamic_state
;
149 state
->need_query_wa
= true;
150 state
->pma_fix_enabled
= false;
151 state
->hiz_enabled
= false;
153 vk_free(&cmd_buffer
->pool
->alloc
, state
->attachments
);
154 state
->attachments
= NULL
;
156 state
->gen7
.index_buffer
= NULL
;
160 anv_cmd_buffer_ensure_push_constants_size(struct anv_cmd_buffer
*cmd_buffer
,
161 gl_shader_stage stage
, uint32_t size
)
163 struct anv_push_constants
**ptr
= &cmd_buffer
->state
.push_constants
[stage
];
166 *ptr
= vk_alloc(&cmd_buffer
->pool
->alloc
, size
, 8,
167 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
169 anv_batch_set_error(&cmd_buffer
->batch
, VK_ERROR_OUT_OF_HOST_MEMORY
);
170 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
172 } else if ((*ptr
)->size
< size
) {
173 *ptr
= vk_realloc(&cmd_buffer
->pool
->alloc
, *ptr
, size
, 8,
174 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
176 anv_batch_set_error(&cmd_buffer
->batch
, VK_ERROR_OUT_OF_HOST_MEMORY
);
177 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
185 static VkResult
anv_create_cmd_buffer(
186 struct anv_device
* device
,
187 struct anv_cmd_pool
* pool
,
188 VkCommandBufferLevel level
,
189 VkCommandBuffer
* pCommandBuffer
)
191 struct anv_cmd_buffer
*cmd_buffer
;
194 cmd_buffer
= vk_alloc(&pool
->alloc
, sizeof(*cmd_buffer
), 8,
195 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
196 if (cmd_buffer
== NULL
)
197 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
199 cmd_buffer
->batch
.status
= VK_SUCCESS
;
201 for (uint32_t i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
202 cmd_buffer
->state
.push_constants
[i
] = NULL
;
204 cmd_buffer
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
205 cmd_buffer
->device
= device
;
206 cmd_buffer
->pool
= pool
;
207 cmd_buffer
->level
= level
;
208 cmd_buffer
->state
.attachments
= NULL
;
210 result
= anv_cmd_buffer_init_batch_bo_chain(cmd_buffer
);
211 if (result
!= VK_SUCCESS
)
214 anv_state_stream_init(&cmd_buffer
->surface_state_stream
,
215 &device
->surface_state_pool
, 4096);
216 anv_state_stream_init(&cmd_buffer
->dynamic_state_stream
,
217 &device
->dynamic_state_pool
, 16384);
219 memset(cmd_buffer
->state
.push_descriptors
, 0,
220 sizeof(cmd_buffer
->state
.push_descriptors
));
223 list_addtail(&cmd_buffer
->pool_link
, &pool
->cmd_buffers
);
225 /* Init the pool_link so we can safefly call list_del when we destroy
228 list_inithead(&cmd_buffer
->pool_link
);
231 *pCommandBuffer
= anv_cmd_buffer_to_handle(cmd_buffer
);
236 vk_free(&cmd_buffer
->pool
->alloc
, cmd_buffer
);
241 VkResult
anv_AllocateCommandBuffers(
243 const VkCommandBufferAllocateInfo
* pAllocateInfo
,
244 VkCommandBuffer
* pCommandBuffers
)
246 ANV_FROM_HANDLE(anv_device
, device
, _device
);
247 ANV_FROM_HANDLE(anv_cmd_pool
, pool
, pAllocateInfo
->commandPool
);
249 VkResult result
= VK_SUCCESS
;
252 for (i
= 0; i
< pAllocateInfo
->commandBufferCount
; i
++) {
253 result
= anv_create_cmd_buffer(device
, pool
, pAllocateInfo
->level
,
254 &pCommandBuffers
[i
]);
255 if (result
!= VK_SUCCESS
)
259 if (result
!= VK_SUCCESS
) {
260 anv_FreeCommandBuffers(_device
, pAllocateInfo
->commandPool
,
262 for (i
= 0; i
< pAllocateInfo
->commandBufferCount
; i
++)
263 pCommandBuffers
[i
] = VK_NULL_HANDLE
;
270 anv_cmd_buffer_destroy(struct anv_cmd_buffer
*cmd_buffer
)
272 list_del(&cmd_buffer
->pool_link
);
274 anv_cmd_buffer_fini_batch_bo_chain(cmd_buffer
);
276 anv_state_stream_finish(&cmd_buffer
->surface_state_stream
);
277 anv_state_stream_finish(&cmd_buffer
->dynamic_state_stream
);
279 anv_cmd_state_reset(cmd_buffer
);
281 vk_free(&cmd_buffer
->pool
->alloc
, cmd_buffer
);
284 void anv_FreeCommandBuffers(
286 VkCommandPool commandPool
,
287 uint32_t commandBufferCount
,
288 const VkCommandBuffer
* pCommandBuffers
)
290 for (uint32_t i
= 0; i
< commandBufferCount
; i
++) {
291 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, pCommandBuffers
[i
]);
296 anv_cmd_buffer_destroy(cmd_buffer
);
301 anv_cmd_buffer_reset(struct anv_cmd_buffer
*cmd_buffer
)
303 cmd_buffer
->usage_flags
= 0;
304 cmd_buffer
->state
.current_pipeline
= UINT32_MAX
;
305 anv_cmd_buffer_reset_batch_bo_chain(cmd_buffer
);
306 anv_cmd_state_reset(cmd_buffer
);
308 anv_state_stream_finish(&cmd_buffer
->surface_state_stream
);
309 anv_state_stream_init(&cmd_buffer
->surface_state_stream
,
310 &cmd_buffer
->device
->surface_state_pool
, 4096);
312 anv_state_stream_finish(&cmd_buffer
->dynamic_state_stream
);
313 anv_state_stream_init(&cmd_buffer
->dynamic_state_stream
,
314 &cmd_buffer
->device
->dynamic_state_pool
, 16384);
318 VkResult
anv_ResetCommandBuffer(
319 VkCommandBuffer commandBuffer
,
320 VkCommandBufferResetFlags flags
)
322 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
323 return anv_cmd_buffer_reset(cmd_buffer
);
327 anv_cmd_buffer_emit_state_base_address(struct anv_cmd_buffer
*cmd_buffer
)
329 switch (cmd_buffer
->device
->info
.gen
) {
331 if (cmd_buffer
->device
->info
.is_haswell
)
332 return gen75_cmd_buffer_emit_state_base_address(cmd_buffer
);
334 return gen7_cmd_buffer_emit_state_base_address(cmd_buffer
);
336 return gen8_cmd_buffer_emit_state_base_address(cmd_buffer
);
338 return gen9_cmd_buffer_emit_state_base_address(cmd_buffer
);
340 return gen10_cmd_buffer_emit_state_base_address(cmd_buffer
);
342 unreachable("unsupported gen\n");
346 void anv_CmdBindPipeline(
347 VkCommandBuffer commandBuffer
,
348 VkPipelineBindPoint pipelineBindPoint
,
349 VkPipeline _pipeline
)
351 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
352 ANV_FROM_HANDLE(anv_pipeline
, pipeline
, _pipeline
);
354 switch (pipelineBindPoint
) {
355 case VK_PIPELINE_BIND_POINT_COMPUTE
:
356 cmd_buffer
->state
.compute_pipeline
= pipeline
;
357 cmd_buffer
->state
.compute_dirty
|= ANV_CMD_DIRTY_PIPELINE
;
358 cmd_buffer
->state
.push_constants_dirty
|= VK_SHADER_STAGE_COMPUTE_BIT
;
359 cmd_buffer
->state
.descriptors_dirty
|= VK_SHADER_STAGE_COMPUTE_BIT
;
362 case VK_PIPELINE_BIND_POINT_GRAPHICS
:
363 cmd_buffer
->state
.pipeline
= pipeline
;
364 cmd_buffer
->state
.vb_dirty
|= pipeline
->vb_used
;
365 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_PIPELINE
;
366 cmd_buffer
->state
.push_constants_dirty
|= pipeline
->active_stages
;
367 cmd_buffer
->state
.descriptors_dirty
|= pipeline
->active_stages
;
369 /* Apply the dynamic state from the pipeline */
370 cmd_buffer
->state
.dirty
|= pipeline
->dynamic_state_mask
;
371 anv_dynamic_state_copy(&cmd_buffer
->state
.dynamic
,
372 &pipeline
->dynamic_state
,
373 pipeline
->dynamic_state_mask
);
377 assert(!"invalid bind point");
382 void anv_CmdSetViewport(
383 VkCommandBuffer commandBuffer
,
384 uint32_t firstViewport
,
385 uint32_t viewportCount
,
386 const VkViewport
* pViewports
)
388 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
390 const uint32_t total_count
= firstViewport
+ viewportCount
;
391 if (cmd_buffer
->state
.dynamic
.viewport
.count
< total_count
)
392 cmd_buffer
->state
.dynamic
.viewport
.count
= total_count
;
394 memcpy(cmd_buffer
->state
.dynamic
.viewport
.viewports
+ firstViewport
,
395 pViewports
, viewportCount
* sizeof(*pViewports
));
397 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_VIEWPORT
;
400 void anv_CmdSetScissor(
401 VkCommandBuffer commandBuffer
,
402 uint32_t firstScissor
,
403 uint32_t scissorCount
,
404 const VkRect2D
* pScissors
)
406 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
408 const uint32_t total_count
= firstScissor
+ scissorCount
;
409 if (cmd_buffer
->state
.dynamic
.scissor
.count
< total_count
)
410 cmd_buffer
->state
.dynamic
.scissor
.count
= total_count
;
412 memcpy(cmd_buffer
->state
.dynamic
.scissor
.scissors
+ firstScissor
,
413 pScissors
, scissorCount
* sizeof(*pScissors
));
415 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_SCISSOR
;
418 void anv_CmdSetLineWidth(
419 VkCommandBuffer commandBuffer
,
422 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
424 cmd_buffer
->state
.dynamic
.line_width
= lineWidth
;
425 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_LINE_WIDTH
;
428 void anv_CmdSetDepthBias(
429 VkCommandBuffer commandBuffer
,
430 float depthBiasConstantFactor
,
431 float depthBiasClamp
,
432 float depthBiasSlopeFactor
)
434 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
436 cmd_buffer
->state
.dynamic
.depth_bias
.bias
= depthBiasConstantFactor
;
437 cmd_buffer
->state
.dynamic
.depth_bias
.clamp
= depthBiasClamp
;
438 cmd_buffer
->state
.dynamic
.depth_bias
.slope
= depthBiasSlopeFactor
;
440 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_DEPTH_BIAS
;
443 void anv_CmdSetBlendConstants(
444 VkCommandBuffer commandBuffer
,
445 const float blendConstants
[4])
447 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
449 memcpy(cmd_buffer
->state
.dynamic
.blend_constants
,
450 blendConstants
, sizeof(float) * 4);
452 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_BLEND_CONSTANTS
;
455 void anv_CmdSetDepthBounds(
456 VkCommandBuffer commandBuffer
,
457 float minDepthBounds
,
458 float maxDepthBounds
)
460 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
462 cmd_buffer
->state
.dynamic
.depth_bounds
.min
= minDepthBounds
;
463 cmd_buffer
->state
.dynamic
.depth_bounds
.max
= maxDepthBounds
;
465 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_DEPTH_BOUNDS
;
468 void anv_CmdSetStencilCompareMask(
469 VkCommandBuffer commandBuffer
,
470 VkStencilFaceFlags faceMask
,
471 uint32_t compareMask
)
473 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
475 if (faceMask
& VK_STENCIL_FACE_FRONT_BIT
)
476 cmd_buffer
->state
.dynamic
.stencil_compare_mask
.front
= compareMask
;
477 if (faceMask
& VK_STENCIL_FACE_BACK_BIT
)
478 cmd_buffer
->state
.dynamic
.stencil_compare_mask
.back
= compareMask
;
480 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_STENCIL_COMPARE_MASK
;
483 void anv_CmdSetStencilWriteMask(
484 VkCommandBuffer commandBuffer
,
485 VkStencilFaceFlags faceMask
,
488 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
490 if (faceMask
& VK_STENCIL_FACE_FRONT_BIT
)
491 cmd_buffer
->state
.dynamic
.stencil_write_mask
.front
= writeMask
;
492 if (faceMask
& VK_STENCIL_FACE_BACK_BIT
)
493 cmd_buffer
->state
.dynamic
.stencil_write_mask
.back
= writeMask
;
495 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_STENCIL_WRITE_MASK
;
498 void anv_CmdSetStencilReference(
499 VkCommandBuffer commandBuffer
,
500 VkStencilFaceFlags faceMask
,
503 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
505 if (faceMask
& VK_STENCIL_FACE_FRONT_BIT
)
506 cmd_buffer
->state
.dynamic
.stencil_reference
.front
= reference
;
507 if (faceMask
& VK_STENCIL_FACE_BACK_BIT
)
508 cmd_buffer
->state
.dynamic
.stencil_reference
.back
= reference
;
510 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_STENCIL_REFERENCE
;
513 void anv_CmdBindDescriptorSets(
514 VkCommandBuffer commandBuffer
,
515 VkPipelineBindPoint pipelineBindPoint
,
516 VkPipelineLayout _layout
,
518 uint32_t descriptorSetCount
,
519 const VkDescriptorSet
* pDescriptorSets
,
520 uint32_t dynamicOffsetCount
,
521 const uint32_t* pDynamicOffsets
)
523 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
524 ANV_FROM_HANDLE(anv_pipeline_layout
, layout
, _layout
);
525 struct anv_descriptor_set_layout
*set_layout
;
527 assert(firstSet
+ descriptorSetCount
< MAX_SETS
);
529 uint32_t dynamic_slot
= 0;
530 for (uint32_t i
= 0; i
< descriptorSetCount
; i
++) {
531 ANV_FROM_HANDLE(anv_descriptor_set
, set
, pDescriptorSets
[i
]);
532 set_layout
= layout
->set
[firstSet
+ i
].layout
;
534 cmd_buffer
->state
.descriptors
[firstSet
+ i
] = set
;
536 if (set_layout
->dynamic_offset_count
> 0) {
537 uint32_t dynamic_offset_start
=
538 layout
->set
[firstSet
+ i
].dynamic_offset_start
;
540 /* Assert that everything is in range */
541 assert(dynamic_offset_start
+ set_layout
->dynamic_offset_count
<=
542 ARRAY_SIZE(cmd_buffer
->state
.dynamic_offsets
));
543 assert(dynamic_slot
+ set_layout
->dynamic_offset_count
<=
546 typed_memcpy(&cmd_buffer
->state
.dynamic_offsets
[dynamic_offset_start
],
547 &pDynamicOffsets
[dynamic_slot
],
548 set_layout
->dynamic_offset_count
);
550 dynamic_slot
+= set_layout
->dynamic_offset_count
;
553 cmd_buffer
->state
.descriptors_dirty
|= set_layout
->shader_stages
;
557 void anv_CmdBindVertexBuffers(
558 VkCommandBuffer commandBuffer
,
559 uint32_t firstBinding
,
560 uint32_t bindingCount
,
561 const VkBuffer
* pBuffers
,
562 const VkDeviceSize
* pOffsets
)
564 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
565 struct anv_vertex_binding
*vb
= cmd_buffer
->state
.vertex_bindings
;
567 /* We have to defer setting up vertex buffer since we need the buffer
568 * stride from the pipeline. */
570 assert(firstBinding
+ bindingCount
<= MAX_VBS
);
571 for (uint32_t i
= 0; i
< bindingCount
; i
++) {
572 vb
[firstBinding
+ i
].buffer
= anv_buffer_from_handle(pBuffers
[i
]);
573 vb
[firstBinding
+ i
].offset
= pOffsets
[i
];
574 cmd_buffer
->state
.vb_dirty
|= 1 << (firstBinding
+ i
);
579 anv_isl_format_for_descriptor_type(VkDescriptorType type
)
582 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
583 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
584 return ISL_FORMAT_R32G32B32A32_FLOAT
;
586 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
587 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
588 return ISL_FORMAT_RAW
;
591 unreachable("Invalid descriptor type");
596 anv_cmd_buffer_emit_dynamic(struct anv_cmd_buffer
*cmd_buffer
,
597 const void *data
, uint32_t size
, uint32_t alignment
)
599 struct anv_state state
;
601 state
= anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, size
, alignment
);
602 memcpy(state
.map
, data
, size
);
604 anv_state_flush(cmd_buffer
->device
, state
);
606 VG(VALGRIND_CHECK_MEM_IS_DEFINED(state
.map
, size
));
612 anv_cmd_buffer_merge_dynamic(struct anv_cmd_buffer
*cmd_buffer
,
613 uint32_t *a
, uint32_t *b
,
614 uint32_t dwords
, uint32_t alignment
)
616 struct anv_state state
;
619 state
= anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
,
620 dwords
* 4, alignment
);
622 for (uint32_t i
= 0; i
< dwords
; i
++)
625 anv_state_flush(cmd_buffer
->device
, state
);
627 VG(VALGRIND_CHECK_MEM_IS_DEFINED(p
, dwords
* 4));
633 anv_cmd_buffer_push_constants(struct anv_cmd_buffer
*cmd_buffer
,
634 gl_shader_stage stage
)
636 /* If we don't have this stage, bail. */
637 if (!anv_pipeline_has_stage(cmd_buffer
->state
.pipeline
, stage
))
638 return (struct anv_state
) { .offset
= 0 };
640 struct anv_push_constants
*data
=
641 cmd_buffer
->state
.push_constants
[stage
];
642 const struct brw_stage_prog_data
*prog_data
=
643 cmd_buffer
->state
.pipeline
->shaders
[stage
]->prog_data
;
645 /* If we don't actually have any push constants, bail. */
646 if (data
== NULL
|| prog_data
== NULL
|| prog_data
->nr_params
== 0)
647 return (struct anv_state
) { .offset
= 0 };
649 struct anv_state state
=
650 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
,
651 prog_data
->nr_params
* sizeof(float),
652 32 /* bottom 5 bits MBZ */);
654 /* Walk through the param array and fill the buffer with data */
655 uint32_t *u32_map
= state
.map
;
656 for (unsigned i
= 0; i
< prog_data
->nr_params
; i
++) {
657 uint32_t offset
= (uintptr_t)prog_data
->param
[i
];
658 u32_map
[i
] = *(uint32_t *)((uint8_t *)data
+ offset
);
661 anv_state_flush(cmd_buffer
->device
, state
);
667 anv_cmd_buffer_cs_push_constants(struct anv_cmd_buffer
*cmd_buffer
)
669 struct anv_push_constants
*data
=
670 cmd_buffer
->state
.push_constants
[MESA_SHADER_COMPUTE
];
671 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute_pipeline
;
672 const struct brw_cs_prog_data
*cs_prog_data
= get_cs_prog_data(pipeline
);
673 const struct brw_stage_prog_data
*prog_data
= &cs_prog_data
->base
;
675 /* If we don't actually have any push constants, bail. */
676 if (cs_prog_data
->push
.total
.size
== 0)
677 return (struct anv_state
) { .offset
= 0 };
679 const unsigned push_constant_alignment
=
680 cmd_buffer
->device
->info
.gen
< 8 ? 32 : 64;
681 const unsigned aligned_total_push_constants_size
=
682 ALIGN(cs_prog_data
->push
.total
.size
, push_constant_alignment
);
683 struct anv_state state
=
684 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
,
685 aligned_total_push_constants_size
,
686 push_constant_alignment
);
688 /* Walk through the param array and fill the buffer with data */
689 uint32_t *u32_map
= state
.map
;
691 if (cs_prog_data
->push
.cross_thread
.size
> 0) {
692 assert(cs_prog_data
->thread_local_id_index
< 0 ||
693 cs_prog_data
->thread_local_id_index
>=
694 cs_prog_data
->push
.cross_thread
.dwords
);
696 i
< cs_prog_data
->push
.cross_thread
.dwords
;
698 uint32_t offset
= (uintptr_t)prog_data
->param
[i
];
699 u32_map
[i
] = *(uint32_t *)((uint8_t *)data
+ offset
);
703 if (cs_prog_data
->push
.per_thread
.size
> 0) {
704 for (unsigned t
= 0; t
< cs_prog_data
->threads
; t
++) {
706 8 * (cs_prog_data
->push
.per_thread
.regs
* t
+
707 cs_prog_data
->push
.cross_thread
.regs
);
708 unsigned src
= cs_prog_data
->push
.cross_thread
.dwords
;
709 for ( ; src
< prog_data
->nr_params
; src
++, dst
++) {
710 if (src
!= cs_prog_data
->thread_local_id_index
) {
711 uint32_t offset
= (uintptr_t)prog_data
->param
[src
];
712 u32_map
[dst
] = *(uint32_t *)((uint8_t *)data
+ offset
);
714 u32_map
[dst
] = t
* cs_prog_data
->simd_size
;
720 anv_state_flush(cmd_buffer
->device
, state
);
725 void anv_CmdPushConstants(
726 VkCommandBuffer commandBuffer
,
727 VkPipelineLayout layout
,
728 VkShaderStageFlags stageFlags
,
733 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
735 anv_foreach_stage(stage
, stageFlags
) {
737 anv_cmd_buffer_ensure_push_constant_field(cmd_buffer
,
739 if (result
!= VK_SUCCESS
)
742 memcpy(cmd_buffer
->state
.push_constants
[stage
]->client_data
+ offset
,
746 cmd_buffer
->state
.push_constants_dirty
|= stageFlags
;
749 VkResult
anv_CreateCommandPool(
751 const VkCommandPoolCreateInfo
* pCreateInfo
,
752 const VkAllocationCallbacks
* pAllocator
,
753 VkCommandPool
* pCmdPool
)
755 ANV_FROM_HANDLE(anv_device
, device
, _device
);
756 struct anv_cmd_pool
*pool
;
758 pool
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*pool
), 8,
759 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
761 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
764 pool
->alloc
= *pAllocator
;
766 pool
->alloc
= device
->alloc
;
768 list_inithead(&pool
->cmd_buffers
);
770 *pCmdPool
= anv_cmd_pool_to_handle(pool
);
775 void anv_DestroyCommandPool(
777 VkCommandPool commandPool
,
778 const VkAllocationCallbacks
* pAllocator
)
780 ANV_FROM_HANDLE(anv_device
, device
, _device
);
781 ANV_FROM_HANDLE(anv_cmd_pool
, pool
, commandPool
);
786 list_for_each_entry_safe(struct anv_cmd_buffer
, cmd_buffer
,
787 &pool
->cmd_buffers
, pool_link
) {
788 anv_cmd_buffer_destroy(cmd_buffer
);
791 vk_free2(&device
->alloc
, pAllocator
, pool
);
794 VkResult
anv_ResetCommandPool(
796 VkCommandPool commandPool
,
797 VkCommandPoolResetFlags flags
)
799 ANV_FROM_HANDLE(anv_cmd_pool
, pool
, commandPool
);
801 list_for_each_entry(struct anv_cmd_buffer
, cmd_buffer
,
802 &pool
->cmd_buffers
, pool_link
) {
803 anv_cmd_buffer_reset(cmd_buffer
);
809 void anv_TrimCommandPoolKHR(
811 VkCommandPool commandPool
,
812 VkCommandPoolTrimFlagsKHR flags
)
814 /* Nothing for us to do here. Our pools stay pretty tidy. */
818 * Return NULL if the current subpass has no depthstencil attachment.
820 const struct anv_image_view
*
821 anv_cmd_buffer_get_depth_stencil_view(const struct anv_cmd_buffer
*cmd_buffer
)
823 const struct anv_subpass
*subpass
= cmd_buffer
->state
.subpass
;
824 const struct anv_framebuffer
*fb
= cmd_buffer
->state
.framebuffer
;
826 if (subpass
->depth_stencil_attachment
.attachment
== VK_ATTACHMENT_UNUSED
)
829 const struct anv_image_view
*iview
=
830 fb
->attachments
[subpass
->depth_stencil_attachment
.attachment
];
832 assert(iview
->aspect_mask
& (VK_IMAGE_ASPECT_DEPTH_BIT
|
833 VK_IMAGE_ASPECT_STENCIL_BIT
));
839 anv_cmd_buffer_ensure_push_descriptor_set(struct anv_cmd_buffer
*cmd_buffer
,
842 struct anv_push_descriptor_set
**push_set
=
843 &cmd_buffer
->state
.push_descriptors
[set
];
845 if (*push_set
== NULL
) {
846 *push_set
= vk_alloc(&cmd_buffer
->pool
->alloc
,
847 sizeof(struct anv_push_descriptor_set
), 8,
848 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
849 if (*push_set
== NULL
) {
850 anv_batch_set_error(&cmd_buffer
->batch
, VK_ERROR_OUT_OF_HOST_MEMORY
);
851 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
858 void anv_CmdPushDescriptorSetKHR(
859 VkCommandBuffer commandBuffer
,
860 VkPipelineBindPoint pipelineBindPoint
,
861 VkPipelineLayout _layout
,
863 uint32_t descriptorWriteCount
,
864 const VkWriteDescriptorSet
* pDescriptorWrites
)
866 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
867 ANV_FROM_HANDLE(anv_pipeline_layout
, layout
, _layout
);
869 assert(pipelineBindPoint
== VK_PIPELINE_BIND_POINT_GRAPHICS
||
870 pipelineBindPoint
== VK_PIPELINE_BIND_POINT_COMPUTE
);
871 assert(_set
< MAX_SETS
);
873 const struct anv_descriptor_set_layout
*set_layout
=
874 layout
->set
[_set
].layout
;
876 if (anv_cmd_buffer_ensure_push_descriptor_set(cmd_buffer
, _set
) != VK_SUCCESS
)
878 struct anv_push_descriptor_set
*push_set
=
879 cmd_buffer
->state
.push_descriptors
[_set
];
880 struct anv_descriptor_set
*set
= &push_set
->set
;
882 set
->layout
= set_layout
;
883 set
->size
= anv_descriptor_set_layout_size(set_layout
);
884 set
->buffer_count
= set_layout
->buffer_count
;
885 set
->buffer_views
= push_set
->buffer_views
;
887 /* Go through the user supplied descriptors. */
888 for (uint32_t i
= 0; i
< descriptorWriteCount
; i
++) {
889 const VkWriteDescriptorSet
*write
= &pDescriptorWrites
[i
];
891 switch (write
->descriptorType
) {
892 case VK_DESCRIPTOR_TYPE_SAMPLER
:
893 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
894 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
895 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
896 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
897 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
898 anv_descriptor_set_write_image_view(set
, &cmd_buffer
->device
->info
,
899 write
->pImageInfo
+ j
,
900 write
->descriptorType
,
902 write
->dstArrayElement
+ j
);
906 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
907 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
908 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
909 ANV_FROM_HANDLE(anv_buffer_view
, bview
,
910 write
->pTexelBufferView
[j
]);
912 anv_descriptor_set_write_buffer_view(set
,
913 write
->descriptorType
,
916 write
->dstArrayElement
+ j
);
920 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
921 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
922 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
923 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
924 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
925 assert(write
->pBufferInfo
[j
].buffer
);
926 ANV_FROM_HANDLE(anv_buffer
, buffer
, write
->pBufferInfo
[j
].buffer
);
929 anv_descriptor_set_write_buffer(set
,
931 &cmd_buffer
->surface_state_stream
,
932 write
->descriptorType
,
935 write
->dstArrayElement
+ j
,
936 write
->pBufferInfo
[j
].offset
,
937 write
->pBufferInfo
[j
].range
);
946 cmd_buffer
->state
.descriptors
[_set
] = set
;
947 cmd_buffer
->state
.descriptors_dirty
|= set_layout
->shader_stages
;
950 void anv_CmdPushDescriptorSetWithTemplateKHR(
951 VkCommandBuffer commandBuffer
,
952 VkDescriptorUpdateTemplateKHR descriptorUpdateTemplate
,
953 VkPipelineLayout _layout
,
957 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
958 ANV_FROM_HANDLE(anv_descriptor_update_template
, template,
959 descriptorUpdateTemplate
);
960 ANV_FROM_HANDLE(anv_pipeline_layout
, layout
, _layout
);
962 assert(_set
< MAX_PUSH_DESCRIPTORS
);
964 const struct anv_descriptor_set_layout
*set_layout
=
965 layout
->set
[_set
].layout
;
967 if (anv_cmd_buffer_ensure_push_descriptor_set(cmd_buffer
, _set
) != VK_SUCCESS
)
969 struct anv_push_descriptor_set
*push_set
=
970 cmd_buffer
->state
.push_descriptors
[_set
];
971 struct anv_descriptor_set
*set
= &push_set
->set
;
973 set
->layout
= set_layout
;
974 set
->size
= anv_descriptor_set_layout_size(set_layout
);
975 set
->buffer_count
= set_layout
->buffer_count
;
976 set
->buffer_views
= push_set
->buffer_views
;
978 anv_descriptor_set_write_template(set
,
980 &cmd_buffer
->surface_state_stream
,
984 cmd_buffer
->state
.descriptors
[_set
] = set
;
985 cmd_buffer
->state
.descriptors_dirty
|= set_layout
->shader_stages
;