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 memset(&state
->descriptors
, 0, sizeof(state
->descriptors
));
121 memset(&state
->push_constants
, 0, sizeof(state
->push_constants
));
122 memset(state
->binding_tables
, 0, sizeof(state
->binding_tables
));
123 memset(state
->samplers
, 0, sizeof(state
->samplers
));
125 /* 0 isn't a valid config. This ensures that we always configure L3$. */
126 cmd_buffer
->state
.current_l3_config
= 0;
130 state
->pending_pipe_bits
= 0;
131 state
->descriptors_dirty
= 0;
132 state
->push_constants_dirty
= 0;
133 state
->pipeline
= NULL
;
134 state
->push_constant_stages
= 0;
135 state
->restart_index
= UINT32_MAX
;
136 state
->dynamic
= default_dynamic_state
;
137 state
->need_query_wa
= true;
139 if (state
->attachments
!= NULL
) {
140 vk_free(&cmd_buffer
->pool
->alloc
, state
->attachments
);
141 state
->attachments
= NULL
;
144 state
->gen7
.index_buffer
= NULL
;
148 * Setup anv_cmd_state::attachments for vkCmdBeginRenderPass.
151 anv_cmd_state_setup_attachments(struct anv_cmd_buffer
*cmd_buffer
,
152 const VkRenderPassBeginInfo
*info
)
154 struct anv_cmd_state
*state
= &cmd_buffer
->state
;
155 ANV_FROM_HANDLE(anv_render_pass
, pass
, info
->renderPass
);
157 vk_free(&cmd_buffer
->pool
->alloc
, state
->attachments
);
159 if (pass
->attachment_count
== 0) {
160 state
->attachments
= NULL
;
164 state
->attachments
= vk_alloc(&cmd_buffer
->pool
->alloc
,
165 pass
->attachment_count
*
166 sizeof(state
->attachments
[0]),
167 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
168 if (state
->attachments
== NULL
) {
169 /* FIXME: Propagate VK_ERROR_OUT_OF_HOST_MEMORY to vkEndCommandBuffer */
173 for (uint32_t i
= 0; i
< pass
->attachment_count
; ++i
) {
174 struct anv_render_pass_attachment
*att
= &pass
->attachments
[i
];
175 VkImageAspectFlags att_aspects
= vk_format_aspects(att
->format
);
176 VkImageAspectFlags clear_aspects
= 0;
178 if (att_aspects
== VK_IMAGE_ASPECT_COLOR_BIT
) {
179 /* color attachment */
180 if (att
->load_op
== VK_ATTACHMENT_LOAD_OP_CLEAR
) {
181 clear_aspects
|= VK_IMAGE_ASPECT_COLOR_BIT
;
184 /* depthstencil attachment */
185 if ((att_aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
) &&
186 att
->load_op
== VK_ATTACHMENT_LOAD_OP_CLEAR
) {
187 clear_aspects
|= VK_IMAGE_ASPECT_DEPTH_BIT
;
189 if ((att_aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
) &&
190 att
->stencil_load_op
== VK_ATTACHMENT_LOAD_OP_CLEAR
) {
191 clear_aspects
|= VK_IMAGE_ASPECT_STENCIL_BIT
;
195 state
->attachments
[i
].pending_clear_aspects
= clear_aspects
;
197 assert(info
->clearValueCount
> i
);
198 state
->attachments
[i
].clear_value
= info
->pClearValues
[i
];
204 anv_cmd_buffer_ensure_push_constants_size(struct anv_cmd_buffer
*cmd_buffer
,
205 gl_shader_stage stage
, uint32_t size
)
207 struct anv_push_constants
**ptr
= &cmd_buffer
->state
.push_constants
[stage
];
210 *ptr
= vk_alloc(&cmd_buffer
->pool
->alloc
, size
, 8,
211 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
213 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
214 } else if ((*ptr
)->size
< size
) {
215 *ptr
= vk_realloc(&cmd_buffer
->pool
->alloc
, *ptr
, size
, 8,
216 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
218 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
225 static VkResult
anv_create_cmd_buffer(
226 struct anv_device
* device
,
227 struct anv_cmd_pool
* pool
,
228 VkCommandBufferLevel level
,
229 VkCommandBuffer
* pCommandBuffer
)
231 struct anv_cmd_buffer
*cmd_buffer
;
234 cmd_buffer
= vk_alloc(&pool
->alloc
, sizeof(*cmd_buffer
), 8,
235 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
236 if (cmd_buffer
== NULL
)
237 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
239 cmd_buffer
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
240 cmd_buffer
->device
= device
;
241 cmd_buffer
->pool
= pool
;
242 cmd_buffer
->level
= level
;
243 cmd_buffer
->state
.attachments
= NULL
;
245 result
= anv_cmd_buffer_init_batch_bo_chain(cmd_buffer
);
246 if (result
!= VK_SUCCESS
)
249 anv_state_stream_init(&cmd_buffer
->surface_state_stream
,
250 &device
->surface_state_block_pool
);
251 anv_state_stream_init(&cmd_buffer
->dynamic_state_stream
,
252 &device
->dynamic_state_block_pool
);
255 list_addtail(&cmd_buffer
->pool_link
, &pool
->cmd_buffers
);
257 /* Init the pool_link so we can safefly call list_del when we destroy
260 list_inithead(&cmd_buffer
->pool_link
);
263 *pCommandBuffer
= anv_cmd_buffer_to_handle(cmd_buffer
);
268 vk_free(&cmd_buffer
->pool
->alloc
, cmd_buffer
);
273 VkResult
anv_AllocateCommandBuffers(
275 const VkCommandBufferAllocateInfo
* pAllocateInfo
,
276 VkCommandBuffer
* pCommandBuffers
)
278 ANV_FROM_HANDLE(anv_device
, device
, _device
);
279 ANV_FROM_HANDLE(anv_cmd_pool
, pool
, pAllocateInfo
->commandPool
);
281 VkResult result
= VK_SUCCESS
;
284 for (i
= 0; i
< pAllocateInfo
->commandBufferCount
; i
++) {
285 result
= anv_create_cmd_buffer(device
, pool
, pAllocateInfo
->level
,
286 &pCommandBuffers
[i
]);
287 if (result
!= VK_SUCCESS
)
291 if (result
!= VK_SUCCESS
)
292 anv_FreeCommandBuffers(_device
, pAllocateInfo
->commandPool
,
299 anv_cmd_buffer_destroy(struct anv_cmd_buffer
*cmd_buffer
)
301 list_del(&cmd_buffer
->pool_link
);
303 anv_cmd_buffer_fini_batch_bo_chain(cmd_buffer
);
305 anv_state_stream_finish(&cmd_buffer
->surface_state_stream
);
306 anv_state_stream_finish(&cmd_buffer
->dynamic_state_stream
);
308 vk_free(&cmd_buffer
->pool
->alloc
, cmd_buffer
->state
.attachments
);
309 vk_free(&cmd_buffer
->pool
->alloc
, cmd_buffer
);
312 void anv_FreeCommandBuffers(
314 VkCommandPool commandPool
,
315 uint32_t commandBufferCount
,
316 const VkCommandBuffer
* pCommandBuffers
)
318 for (uint32_t i
= 0; i
< commandBufferCount
; i
++) {
319 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, pCommandBuffers
[i
]);
321 anv_cmd_buffer_destroy(cmd_buffer
);
326 anv_cmd_buffer_reset(struct anv_cmd_buffer
*cmd_buffer
)
328 cmd_buffer
->usage_flags
= 0;
329 cmd_buffer
->state
.current_pipeline
= UINT32_MAX
;
330 anv_cmd_buffer_reset_batch_bo_chain(cmd_buffer
);
331 anv_cmd_state_reset(cmd_buffer
);
333 anv_state_stream_finish(&cmd_buffer
->surface_state_stream
);
334 anv_state_stream_init(&cmd_buffer
->surface_state_stream
,
335 &cmd_buffer
->device
->surface_state_block_pool
);
337 anv_state_stream_finish(&cmd_buffer
->dynamic_state_stream
);
338 anv_state_stream_init(&cmd_buffer
->dynamic_state_stream
,
339 &cmd_buffer
->device
->dynamic_state_block_pool
);
343 VkResult
anv_ResetCommandBuffer(
344 VkCommandBuffer commandBuffer
,
345 VkCommandBufferResetFlags flags
)
347 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
348 return anv_cmd_buffer_reset(cmd_buffer
);
351 void anv_CmdBindPipeline(
352 VkCommandBuffer commandBuffer
,
353 VkPipelineBindPoint pipelineBindPoint
,
354 VkPipeline _pipeline
)
356 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
357 ANV_FROM_HANDLE(anv_pipeline
, pipeline
, _pipeline
);
359 switch (pipelineBindPoint
) {
360 case VK_PIPELINE_BIND_POINT_COMPUTE
:
361 cmd_buffer
->state
.compute_pipeline
= pipeline
;
362 cmd_buffer
->state
.compute_dirty
|= ANV_CMD_DIRTY_PIPELINE
;
363 cmd_buffer
->state
.push_constants_dirty
|= VK_SHADER_STAGE_COMPUTE_BIT
;
364 cmd_buffer
->state
.descriptors_dirty
|= VK_SHADER_STAGE_COMPUTE_BIT
;
367 case VK_PIPELINE_BIND_POINT_GRAPHICS
:
368 cmd_buffer
->state
.pipeline
= pipeline
;
369 cmd_buffer
->state
.vb_dirty
|= pipeline
->vb_used
;
370 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_PIPELINE
;
371 cmd_buffer
->state
.push_constants_dirty
|= pipeline
->active_stages
;
372 cmd_buffer
->state
.descriptors_dirty
|= pipeline
->active_stages
;
374 /* Apply the dynamic state from the pipeline */
375 cmd_buffer
->state
.dirty
|= pipeline
->dynamic_state_mask
;
376 anv_dynamic_state_copy(&cmd_buffer
->state
.dynamic
,
377 &pipeline
->dynamic_state
,
378 pipeline
->dynamic_state_mask
);
382 assert(!"invalid bind point");
387 void anv_CmdSetViewport(
388 VkCommandBuffer commandBuffer
,
389 uint32_t firstViewport
,
390 uint32_t viewportCount
,
391 const VkViewport
* pViewports
)
393 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
395 const uint32_t total_count
= firstViewport
+ viewportCount
;
396 if (cmd_buffer
->state
.dynamic
.viewport
.count
< total_count
)
397 cmd_buffer
->state
.dynamic
.viewport
.count
= total_count
;
399 memcpy(cmd_buffer
->state
.dynamic
.viewport
.viewports
+ firstViewport
,
400 pViewports
, viewportCount
* sizeof(*pViewports
));
402 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_VIEWPORT
;
405 void anv_CmdSetScissor(
406 VkCommandBuffer commandBuffer
,
407 uint32_t firstScissor
,
408 uint32_t scissorCount
,
409 const VkRect2D
* pScissors
)
411 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
413 const uint32_t total_count
= firstScissor
+ scissorCount
;
414 if (cmd_buffer
->state
.dynamic
.scissor
.count
< total_count
)
415 cmd_buffer
->state
.dynamic
.scissor
.count
= total_count
;
417 memcpy(cmd_buffer
->state
.dynamic
.scissor
.scissors
+ firstScissor
,
418 pScissors
, scissorCount
* sizeof(*pScissors
));
420 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_SCISSOR
;
423 void anv_CmdSetLineWidth(
424 VkCommandBuffer commandBuffer
,
427 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
429 cmd_buffer
->state
.dynamic
.line_width
= lineWidth
;
430 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_LINE_WIDTH
;
433 void anv_CmdSetDepthBias(
434 VkCommandBuffer commandBuffer
,
435 float depthBiasConstantFactor
,
436 float depthBiasClamp
,
437 float depthBiasSlopeFactor
)
439 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
441 cmd_buffer
->state
.dynamic
.depth_bias
.bias
= depthBiasConstantFactor
;
442 cmd_buffer
->state
.dynamic
.depth_bias
.clamp
= depthBiasClamp
;
443 cmd_buffer
->state
.dynamic
.depth_bias
.slope
= depthBiasSlopeFactor
;
445 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_DEPTH_BIAS
;
448 void anv_CmdSetBlendConstants(
449 VkCommandBuffer commandBuffer
,
450 const float blendConstants
[4])
452 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
454 memcpy(cmd_buffer
->state
.dynamic
.blend_constants
,
455 blendConstants
, sizeof(float) * 4);
457 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_BLEND_CONSTANTS
;
460 void anv_CmdSetDepthBounds(
461 VkCommandBuffer commandBuffer
,
462 float minDepthBounds
,
463 float maxDepthBounds
)
465 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
467 cmd_buffer
->state
.dynamic
.depth_bounds
.min
= minDepthBounds
;
468 cmd_buffer
->state
.dynamic
.depth_bounds
.max
= maxDepthBounds
;
470 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_DEPTH_BOUNDS
;
473 void anv_CmdSetStencilCompareMask(
474 VkCommandBuffer commandBuffer
,
475 VkStencilFaceFlags faceMask
,
476 uint32_t compareMask
)
478 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
480 if (faceMask
& VK_STENCIL_FACE_FRONT_BIT
)
481 cmd_buffer
->state
.dynamic
.stencil_compare_mask
.front
= compareMask
;
482 if (faceMask
& VK_STENCIL_FACE_BACK_BIT
)
483 cmd_buffer
->state
.dynamic
.stencil_compare_mask
.back
= compareMask
;
485 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_STENCIL_COMPARE_MASK
;
488 void anv_CmdSetStencilWriteMask(
489 VkCommandBuffer commandBuffer
,
490 VkStencilFaceFlags faceMask
,
493 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
495 if (faceMask
& VK_STENCIL_FACE_FRONT_BIT
)
496 cmd_buffer
->state
.dynamic
.stencil_write_mask
.front
= writeMask
;
497 if (faceMask
& VK_STENCIL_FACE_BACK_BIT
)
498 cmd_buffer
->state
.dynamic
.stencil_write_mask
.back
= writeMask
;
500 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_STENCIL_WRITE_MASK
;
503 void anv_CmdSetStencilReference(
504 VkCommandBuffer commandBuffer
,
505 VkStencilFaceFlags faceMask
,
508 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
510 if (faceMask
& VK_STENCIL_FACE_FRONT_BIT
)
511 cmd_buffer
->state
.dynamic
.stencil_reference
.front
= reference
;
512 if (faceMask
& VK_STENCIL_FACE_BACK_BIT
)
513 cmd_buffer
->state
.dynamic
.stencil_reference
.back
= reference
;
515 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_STENCIL_REFERENCE
;
518 void anv_CmdBindDescriptorSets(
519 VkCommandBuffer commandBuffer
,
520 VkPipelineBindPoint pipelineBindPoint
,
521 VkPipelineLayout _layout
,
523 uint32_t descriptorSetCount
,
524 const VkDescriptorSet
* pDescriptorSets
,
525 uint32_t dynamicOffsetCount
,
526 const uint32_t* pDynamicOffsets
)
528 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
529 ANV_FROM_HANDLE(anv_pipeline_layout
, layout
, _layout
);
530 struct anv_descriptor_set_layout
*set_layout
;
532 assert(firstSet
+ descriptorSetCount
< MAX_SETS
);
534 for (uint32_t i
= 0; i
< descriptorSetCount
; i
++) {
535 ANV_FROM_HANDLE(anv_descriptor_set
, set
, pDescriptorSets
[i
]);
536 set_layout
= layout
->set
[firstSet
+ i
].layout
;
538 if (cmd_buffer
->state
.descriptors
[firstSet
+ i
] != set
) {
539 cmd_buffer
->state
.descriptors
[firstSet
+ i
] = set
;
540 cmd_buffer
->state
.descriptors_dirty
|= set_layout
->shader_stages
;
543 if (set_layout
->dynamic_offset_count
> 0) {
544 anv_foreach_stage(s
, set_layout
->shader_stages
) {
545 anv_cmd_buffer_ensure_push_constant_field(cmd_buffer
, s
, dynamic
);
547 struct anv_push_constants
*push
=
548 cmd_buffer
->state
.push_constants
[s
];
550 unsigned d
= layout
->set
[firstSet
+ i
].dynamic_offset_start
;
551 const uint32_t *offsets
= pDynamicOffsets
;
552 struct anv_descriptor
*desc
= set
->descriptors
;
554 for (unsigned b
= 0; b
< set_layout
->binding_count
; b
++) {
555 if (set_layout
->binding
[b
].dynamic_offset_index
< 0)
558 unsigned array_size
= set_layout
->binding
[b
].array_size
;
559 for (unsigned j
= 0; j
< array_size
; j
++) {
560 push
->dynamic
[d
].offset
= *(offsets
++);
561 push
->dynamic
[d
].range
= (desc
->buffer_view
) ?
562 desc
->buffer_view
->range
: 0;
568 cmd_buffer
->state
.push_constants_dirty
|= set_layout
->shader_stages
;
573 void anv_CmdBindVertexBuffers(
574 VkCommandBuffer commandBuffer
,
575 uint32_t firstBinding
,
576 uint32_t bindingCount
,
577 const VkBuffer
* pBuffers
,
578 const VkDeviceSize
* pOffsets
)
580 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
581 struct anv_vertex_binding
*vb
= cmd_buffer
->state
.vertex_bindings
;
583 /* We have to defer setting up vertex buffer since we need the buffer
584 * stride from the pipeline. */
586 assert(firstBinding
+ bindingCount
< MAX_VBS
);
587 for (uint32_t i
= 0; i
< bindingCount
; i
++) {
588 vb
[firstBinding
+ i
].buffer
= anv_buffer_from_handle(pBuffers
[i
]);
589 vb
[firstBinding
+ i
].offset
= pOffsets
[i
];
590 cmd_buffer
->state
.vb_dirty
|= 1 << (firstBinding
+ i
);
595 anv_isl_format_for_descriptor_type(VkDescriptorType type
)
598 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
599 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
600 return ISL_FORMAT_R32G32B32A32_FLOAT
;
602 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
603 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
604 return ISL_FORMAT_RAW
;
607 unreachable("Invalid descriptor type");
612 anv_cmd_buffer_emit_dynamic(struct anv_cmd_buffer
*cmd_buffer
,
613 const void *data
, uint32_t size
, uint32_t alignment
)
615 struct anv_state state
;
617 state
= anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, size
, alignment
);
618 memcpy(state
.map
, data
, size
);
620 if (!cmd_buffer
->device
->info
.has_llc
)
621 anv_state_clflush(state
);
623 VG(VALGRIND_CHECK_MEM_IS_DEFINED(state
.map
, size
));
629 anv_cmd_buffer_merge_dynamic(struct anv_cmd_buffer
*cmd_buffer
,
630 uint32_t *a
, uint32_t *b
,
631 uint32_t dwords
, uint32_t alignment
)
633 struct anv_state state
;
636 state
= anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
,
637 dwords
* 4, alignment
);
639 for (uint32_t i
= 0; i
< dwords
; i
++)
642 if (!cmd_buffer
->device
->info
.has_llc
)
643 anv_state_clflush(state
);
645 VG(VALGRIND_CHECK_MEM_IS_DEFINED(p
, dwords
* 4));
651 anv_cmd_buffer_push_constants(struct anv_cmd_buffer
*cmd_buffer
,
652 gl_shader_stage stage
)
654 /* If we don't have this stage, bail. */
655 if (!anv_pipeline_has_stage(cmd_buffer
->state
.pipeline
, stage
))
656 return (struct anv_state
) { .offset
= 0 };
658 struct anv_push_constants
*data
=
659 cmd_buffer
->state
.push_constants
[stage
];
660 const struct brw_stage_prog_data
*prog_data
=
661 cmd_buffer
->state
.pipeline
->shaders
[stage
]->prog_data
;
663 /* If we don't actually have any push constants, bail. */
664 if (data
== NULL
|| prog_data
== NULL
|| prog_data
->nr_params
== 0)
665 return (struct anv_state
) { .offset
= 0 };
667 struct anv_state state
=
668 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
,
669 prog_data
->nr_params
* sizeof(float),
670 32 /* bottom 5 bits MBZ */);
672 /* Walk through the param array and fill the buffer with data */
673 uint32_t *u32_map
= state
.map
;
674 for (unsigned i
= 0; i
< prog_data
->nr_params
; i
++) {
675 uint32_t offset
= (uintptr_t)prog_data
->param
[i
];
676 u32_map
[i
] = *(uint32_t *)((uint8_t *)data
+ offset
);
679 if (!cmd_buffer
->device
->info
.has_llc
)
680 anv_state_clflush(state
);
686 anv_cmd_buffer_cs_push_constants(struct anv_cmd_buffer
*cmd_buffer
)
688 struct anv_push_constants
*data
=
689 cmd_buffer
->state
.push_constants
[MESA_SHADER_COMPUTE
];
690 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute_pipeline
;
691 const struct brw_cs_prog_data
*cs_prog_data
= get_cs_prog_data(pipeline
);
692 const struct brw_stage_prog_data
*prog_data
= &cs_prog_data
->base
;
694 /* If we don't actually have any push constants, bail. */
695 if (cs_prog_data
->push
.total
.size
== 0)
696 return (struct anv_state
) { .offset
= 0 };
698 const unsigned push_constant_alignment
=
699 cmd_buffer
->device
->info
.gen
< 8 ? 32 : 64;
700 const unsigned aligned_total_push_constants_size
=
701 ALIGN(cs_prog_data
->push
.total
.size
, push_constant_alignment
);
702 struct anv_state state
=
703 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
,
704 aligned_total_push_constants_size
,
705 push_constant_alignment
);
707 /* Walk through the param array and fill the buffer with data */
708 uint32_t *u32_map
= state
.map
;
710 if (cs_prog_data
->push
.cross_thread
.size
> 0) {
711 assert(cs_prog_data
->thread_local_id_index
< 0 ||
712 cs_prog_data
->thread_local_id_index
>=
713 cs_prog_data
->push
.cross_thread
.dwords
);
715 i
< cs_prog_data
->push
.cross_thread
.dwords
;
717 uint32_t offset
= (uintptr_t)prog_data
->param
[i
];
718 u32_map
[i
] = *(uint32_t *)((uint8_t *)data
+ offset
);
722 if (cs_prog_data
->push
.per_thread
.size
> 0) {
723 for (unsigned t
= 0; t
< cs_prog_data
->threads
; t
++) {
725 8 * (cs_prog_data
->push
.per_thread
.regs
* t
+
726 cs_prog_data
->push
.cross_thread
.regs
);
727 unsigned src
= cs_prog_data
->push
.cross_thread
.dwords
;
728 for ( ; src
< prog_data
->nr_params
; src
++, dst
++) {
729 if (src
!= cs_prog_data
->thread_local_id_index
) {
730 uint32_t offset
= (uintptr_t)prog_data
->param
[src
];
731 u32_map
[dst
] = *(uint32_t *)((uint8_t *)data
+ offset
);
733 u32_map
[dst
] = t
* cs_prog_data
->simd_size
;
739 if (!cmd_buffer
->device
->info
.has_llc
)
740 anv_state_clflush(state
);
745 void anv_CmdPushConstants(
746 VkCommandBuffer commandBuffer
,
747 VkPipelineLayout layout
,
748 VkShaderStageFlags stageFlags
,
753 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
755 anv_foreach_stage(stage
, stageFlags
) {
756 anv_cmd_buffer_ensure_push_constant_field(cmd_buffer
, stage
, client_data
);
758 memcpy(cmd_buffer
->state
.push_constants
[stage
]->client_data
+ offset
,
762 cmd_buffer
->state
.push_constants_dirty
|= stageFlags
;
765 VkResult
anv_CreateCommandPool(
767 const VkCommandPoolCreateInfo
* pCreateInfo
,
768 const VkAllocationCallbacks
* pAllocator
,
769 VkCommandPool
* pCmdPool
)
771 ANV_FROM_HANDLE(anv_device
, device
, _device
);
772 struct anv_cmd_pool
*pool
;
774 pool
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*pool
), 8,
775 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
777 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
780 pool
->alloc
= *pAllocator
;
782 pool
->alloc
= device
->alloc
;
784 list_inithead(&pool
->cmd_buffers
);
786 *pCmdPool
= anv_cmd_pool_to_handle(pool
);
791 void anv_DestroyCommandPool(
793 VkCommandPool commandPool
,
794 const VkAllocationCallbacks
* pAllocator
)
796 ANV_FROM_HANDLE(anv_device
, device
, _device
);
797 ANV_FROM_HANDLE(anv_cmd_pool
, pool
, commandPool
);
799 list_for_each_entry_safe(struct anv_cmd_buffer
, cmd_buffer
,
800 &pool
->cmd_buffers
, pool_link
) {
801 anv_cmd_buffer_destroy(cmd_buffer
);
804 vk_free2(&device
->alloc
, pAllocator
, pool
);
807 VkResult
anv_ResetCommandPool(
809 VkCommandPool commandPool
,
810 VkCommandPoolResetFlags flags
)
812 ANV_FROM_HANDLE(anv_cmd_pool
, pool
, commandPool
);
814 list_for_each_entry(struct anv_cmd_buffer
, cmd_buffer
,
815 &pool
->cmd_buffers
, pool_link
) {
816 anv_cmd_buffer_reset(cmd_buffer
);
823 * Return NULL if the current subpass has no depthstencil attachment.
825 const struct anv_image_view
*
826 anv_cmd_buffer_get_depth_stencil_view(const struct anv_cmd_buffer
*cmd_buffer
)
828 const struct anv_subpass
*subpass
= cmd_buffer
->state
.subpass
;
829 const struct anv_framebuffer
*fb
= cmd_buffer
->state
.framebuffer
;
831 if (subpass
->depth_stencil_attachment
== VK_ATTACHMENT_UNUSED
)
834 const struct anv_image_view
*iview
=
835 fb
->attachments
[subpass
->depth_stencil_attachment
];
837 assert(iview
->aspect_mask
& (VK_IMAGE_ASPECT_DEPTH_BIT
|
838 VK_IMAGE_ASPECT_STENCIL_BIT
));