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;
138 state
->pma_fix_enabled
= false;
139 state
->hiz_enabled
= false;
141 if (state
->attachments
!= NULL
) {
142 vk_free(&cmd_buffer
->pool
->alloc
, state
->attachments
);
143 state
->attachments
= NULL
;
146 state
->gen7
.index_buffer
= NULL
;
150 anv_cmd_buffer_ensure_push_constants_size(struct anv_cmd_buffer
*cmd_buffer
,
151 gl_shader_stage stage
, uint32_t size
)
153 struct anv_push_constants
**ptr
= &cmd_buffer
->state
.push_constants
[stage
];
156 *ptr
= vk_alloc(&cmd_buffer
->pool
->alloc
, size
, 8,
157 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
159 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
160 } else if ((*ptr
)->size
< size
) {
161 *ptr
= vk_realloc(&cmd_buffer
->pool
->alloc
, *ptr
, size
, 8,
162 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
164 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
171 static VkResult
anv_create_cmd_buffer(
172 struct anv_device
* device
,
173 struct anv_cmd_pool
* pool
,
174 VkCommandBufferLevel level
,
175 VkCommandBuffer
* pCommandBuffer
)
177 struct anv_cmd_buffer
*cmd_buffer
;
180 cmd_buffer
= vk_alloc(&pool
->alloc
, sizeof(*cmd_buffer
), 8,
181 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
182 if (cmd_buffer
== NULL
)
183 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
185 cmd_buffer
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
186 cmd_buffer
->device
= device
;
187 cmd_buffer
->pool
= pool
;
188 cmd_buffer
->level
= level
;
189 cmd_buffer
->state
.attachments
= NULL
;
191 result
= anv_cmd_buffer_init_batch_bo_chain(cmd_buffer
);
192 if (result
!= VK_SUCCESS
)
195 anv_state_stream_init(&cmd_buffer
->surface_state_stream
,
196 &device
->surface_state_block_pool
);
197 anv_state_stream_init(&cmd_buffer
->dynamic_state_stream
,
198 &device
->dynamic_state_block_pool
);
201 list_addtail(&cmd_buffer
->pool_link
, &pool
->cmd_buffers
);
203 /* Init the pool_link so we can safefly call list_del when we destroy
206 list_inithead(&cmd_buffer
->pool_link
);
209 *pCommandBuffer
= anv_cmd_buffer_to_handle(cmd_buffer
);
214 vk_free(&cmd_buffer
->pool
->alloc
, cmd_buffer
);
219 VkResult
anv_AllocateCommandBuffers(
221 const VkCommandBufferAllocateInfo
* pAllocateInfo
,
222 VkCommandBuffer
* pCommandBuffers
)
224 ANV_FROM_HANDLE(anv_device
, device
, _device
);
225 ANV_FROM_HANDLE(anv_cmd_pool
, pool
, pAllocateInfo
->commandPool
);
227 VkResult result
= VK_SUCCESS
;
230 for (i
= 0; i
< pAllocateInfo
->commandBufferCount
; i
++) {
231 result
= anv_create_cmd_buffer(device
, pool
, pAllocateInfo
->level
,
232 &pCommandBuffers
[i
]);
233 if (result
!= VK_SUCCESS
)
237 if (result
!= VK_SUCCESS
) {
238 anv_FreeCommandBuffers(_device
, pAllocateInfo
->commandPool
,
240 for (i
= 0; i
< pAllocateInfo
->commandBufferCount
; i
++)
241 pCommandBuffers
[i
] = VK_NULL_HANDLE
;
248 anv_cmd_buffer_destroy(struct anv_cmd_buffer
*cmd_buffer
)
250 list_del(&cmd_buffer
->pool_link
);
252 anv_cmd_buffer_fini_batch_bo_chain(cmd_buffer
);
254 anv_state_stream_finish(&cmd_buffer
->surface_state_stream
);
255 anv_state_stream_finish(&cmd_buffer
->dynamic_state_stream
);
257 vk_free(&cmd_buffer
->pool
->alloc
, cmd_buffer
->state
.attachments
);
258 vk_free(&cmd_buffer
->pool
->alloc
, cmd_buffer
);
261 void anv_FreeCommandBuffers(
263 VkCommandPool commandPool
,
264 uint32_t commandBufferCount
,
265 const VkCommandBuffer
* pCommandBuffers
)
267 for (uint32_t i
= 0; i
< commandBufferCount
; i
++) {
268 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, pCommandBuffers
[i
]);
273 anv_cmd_buffer_destroy(cmd_buffer
);
278 anv_cmd_buffer_reset(struct anv_cmd_buffer
*cmd_buffer
)
280 cmd_buffer
->usage_flags
= 0;
281 cmd_buffer
->state
.current_pipeline
= UINT32_MAX
;
282 anv_cmd_buffer_reset_batch_bo_chain(cmd_buffer
);
283 anv_cmd_state_reset(cmd_buffer
);
285 anv_state_stream_finish(&cmd_buffer
->surface_state_stream
);
286 anv_state_stream_init(&cmd_buffer
->surface_state_stream
,
287 &cmd_buffer
->device
->surface_state_block_pool
);
289 anv_state_stream_finish(&cmd_buffer
->dynamic_state_stream
);
290 anv_state_stream_init(&cmd_buffer
->dynamic_state_stream
,
291 &cmd_buffer
->device
->dynamic_state_block_pool
);
295 VkResult
anv_ResetCommandBuffer(
296 VkCommandBuffer commandBuffer
,
297 VkCommandBufferResetFlags flags
)
299 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
300 return anv_cmd_buffer_reset(cmd_buffer
);
304 anv_cmd_buffer_emit_state_base_address(struct anv_cmd_buffer
*cmd_buffer
)
306 switch (cmd_buffer
->device
->info
.gen
) {
308 if (cmd_buffer
->device
->info
.is_haswell
)
309 return gen75_cmd_buffer_emit_state_base_address(cmd_buffer
);
311 return gen7_cmd_buffer_emit_state_base_address(cmd_buffer
);
313 return gen8_cmd_buffer_emit_state_base_address(cmd_buffer
);
315 return gen9_cmd_buffer_emit_state_base_address(cmd_buffer
);
317 unreachable("unsupported gen\n");
321 void anv_CmdBindPipeline(
322 VkCommandBuffer commandBuffer
,
323 VkPipelineBindPoint pipelineBindPoint
,
324 VkPipeline _pipeline
)
326 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
327 ANV_FROM_HANDLE(anv_pipeline
, pipeline
, _pipeline
);
329 switch (pipelineBindPoint
) {
330 case VK_PIPELINE_BIND_POINT_COMPUTE
:
331 cmd_buffer
->state
.compute_pipeline
= pipeline
;
332 cmd_buffer
->state
.compute_dirty
|= ANV_CMD_DIRTY_PIPELINE
;
333 cmd_buffer
->state
.push_constants_dirty
|= VK_SHADER_STAGE_COMPUTE_BIT
;
334 cmd_buffer
->state
.descriptors_dirty
|= VK_SHADER_STAGE_COMPUTE_BIT
;
337 case VK_PIPELINE_BIND_POINT_GRAPHICS
:
338 cmd_buffer
->state
.pipeline
= pipeline
;
339 cmd_buffer
->state
.vb_dirty
|= pipeline
->vb_used
;
340 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_PIPELINE
;
341 cmd_buffer
->state
.push_constants_dirty
|= pipeline
->active_stages
;
342 cmd_buffer
->state
.descriptors_dirty
|= pipeline
->active_stages
;
344 /* Apply the dynamic state from the pipeline */
345 cmd_buffer
->state
.dirty
|= pipeline
->dynamic_state_mask
;
346 anv_dynamic_state_copy(&cmd_buffer
->state
.dynamic
,
347 &pipeline
->dynamic_state
,
348 pipeline
->dynamic_state_mask
);
352 assert(!"invalid bind point");
357 void anv_CmdSetViewport(
358 VkCommandBuffer commandBuffer
,
359 uint32_t firstViewport
,
360 uint32_t viewportCount
,
361 const VkViewport
* pViewports
)
363 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
365 const uint32_t total_count
= firstViewport
+ viewportCount
;
366 if (cmd_buffer
->state
.dynamic
.viewport
.count
< total_count
)
367 cmd_buffer
->state
.dynamic
.viewport
.count
= total_count
;
369 memcpy(cmd_buffer
->state
.dynamic
.viewport
.viewports
+ firstViewport
,
370 pViewports
, viewportCount
* sizeof(*pViewports
));
372 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_VIEWPORT
;
375 void anv_CmdSetScissor(
376 VkCommandBuffer commandBuffer
,
377 uint32_t firstScissor
,
378 uint32_t scissorCount
,
379 const VkRect2D
* pScissors
)
381 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
383 const uint32_t total_count
= firstScissor
+ scissorCount
;
384 if (cmd_buffer
->state
.dynamic
.scissor
.count
< total_count
)
385 cmd_buffer
->state
.dynamic
.scissor
.count
= total_count
;
387 memcpy(cmd_buffer
->state
.dynamic
.scissor
.scissors
+ firstScissor
,
388 pScissors
, scissorCount
* sizeof(*pScissors
));
390 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_SCISSOR
;
393 void anv_CmdSetLineWidth(
394 VkCommandBuffer commandBuffer
,
397 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
399 cmd_buffer
->state
.dynamic
.line_width
= lineWidth
;
400 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_LINE_WIDTH
;
403 void anv_CmdSetDepthBias(
404 VkCommandBuffer commandBuffer
,
405 float depthBiasConstantFactor
,
406 float depthBiasClamp
,
407 float depthBiasSlopeFactor
)
409 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
411 cmd_buffer
->state
.dynamic
.depth_bias
.bias
= depthBiasConstantFactor
;
412 cmd_buffer
->state
.dynamic
.depth_bias
.clamp
= depthBiasClamp
;
413 cmd_buffer
->state
.dynamic
.depth_bias
.slope
= depthBiasSlopeFactor
;
415 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_DEPTH_BIAS
;
418 void anv_CmdSetBlendConstants(
419 VkCommandBuffer commandBuffer
,
420 const float blendConstants
[4])
422 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
424 memcpy(cmd_buffer
->state
.dynamic
.blend_constants
,
425 blendConstants
, sizeof(float) * 4);
427 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_BLEND_CONSTANTS
;
430 void anv_CmdSetDepthBounds(
431 VkCommandBuffer commandBuffer
,
432 float minDepthBounds
,
433 float maxDepthBounds
)
435 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
437 cmd_buffer
->state
.dynamic
.depth_bounds
.min
= minDepthBounds
;
438 cmd_buffer
->state
.dynamic
.depth_bounds
.max
= maxDepthBounds
;
440 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_DEPTH_BOUNDS
;
443 void anv_CmdSetStencilCompareMask(
444 VkCommandBuffer commandBuffer
,
445 VkStencilFaceFlags faceMask
,
446 uint32_t compareMask
)
448 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
450 if (faceMask
& VK_STENCIL_FACE_FRONT_BIT
)
451 cmd_buffer
->state
.dynamic
.stencil_compare_mask
.front
= compareMask
;
452 if (faceMask
& VK_STENCIL_FACE_BACK_BIT
)
453 cmd_buffer
->state
.dynamic
.stencil_compare_mask
.back
= compareMask
;
455 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_STENCIL_COMPARE_MASK
;
458 void anv_CmdSetStencilWriteMask(
459 VkCommandBuffer commandBuffer
,
460 VkStencilFaceFlags faceMask
,
463 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
465 if (faceMask
& VK_STENCIL_FACE_FRONT_BIT
)
466 cmd_buffer
->state
.dynamic
.stencil_write_mask
.front
= writeMask
;
467 if (faceMask
& VK_STENCIL_FACE_BACK_BIT
)
468 cmd_buffer
->state
.dynamic
.stencil_write_mask
.back
= writeMask
;
470 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_STENCIL_WRITE_MASK
;
473 void anv_CmdSetStencilReference(
474 VkCommandBuffer commandBuffer
,
475 VkStencilFaceFlags faceMask
,
478 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
480 if (faceMask
& VK_STENCIL_FACE_FRONT_BIT
)
481 cmd_buffer
->state
.dynamic
.stencil_reference
.front
= reference
;
482 if (faceMask
& VK_STENCIL_FACE_BACK_BIT
)
483 cmd_buffer
->state
.dynamic
.stencil_reference
.back
= reference
;
485 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_STENCIL_REFERENCE
;
488 void anv_CmdBindDescriptorSets(
489 VkCommandBuffer commandBuffer
,
490 VkPipelineBindPoint pipelineBindPoint
,
491 VkPipelineLayout _layout
,
493 uint32_t descriptorSetCount
,
494 const VkDescriptorSet
* pDescriptorSets
,
495 uint32_t dynamicOffsetCount
,
496 const uint32_t* pDynamicOffsets
)
498 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
499 ANV_FROM_HANDLE(anv_pipeline_layout
, layout
, _layout
);
500 struct anv_descriptor_set_layout
*set_layout
;
502 assert(firstSet
+ descriptorSetCount
< MAX_SETS
);
504 for (uint32_t i
= 0; i
< descriptorSetCount
; i
++) {
505 ANV_FROM_HANDLE(anv_descriptor_set
, set
, pDescriptorSets
[i
]);
506 set_layout
= layout
->set
[firstSet
+ i
].layout
;
508 if (cmd_buffer
->state
.descriptors
[firstSet
+ i
] != set
) {
509 cmd_buffer
->state
.descriptors
[firstSet
+ i
] = set
;
510 cmd_buffer
->state
.descriptors_dirty
|= set_layout
->shader_stages
;
513 if (set_layout
->dynamic_offset_count
> 0) {
514 anv_foreach_stage(s
, set_layout
->shader_stages
) {
515 anv_cmd_buffer_ensure_push_constant_field(cmd_buffer
, s
, dynamic
);
517 struct anv_push_constants
*push
=
518 cmd_buffer
->state
.push_constants
[s
];
520 unsigned d
= layout
->set
[firstSet
+ i
].dynamic_offset_start
;
521 const uint32_t *offsets
= pDynamicOffsets
;
522 struct anv_descriptor
*desc
= set
->descriptors
;
524 for (unsigned b
= 0; b
< set_layout
->binding_count
; b
++) {
525 if (set_layout
->binding
[b
].dynamic_offset_index
< 0)
528 unsigned array_size
= set_layout
->binding
[b
].array_size
;
529 for (unsigned j
= 0; j
< array_size
; j
++) {
530 push
->dynamic
[d
].offset
= *(offsets
++);
531 push
->dynamic
[d
].range
= (desc
->buffer_view
) ?
532 desc
->buffer_view
->range
: 0;
538 cmd_buffer
->state
.push_constants_dirty
|= set_layout
->shader_stages
;
543 void anv_CmdBindVertexBuffers(
544 VkCommandBuffer commandBuffer
,
545 uint32_t firstBinding
,
546 uint32_t bindingCount
,
547 const VkBuffer
* pBuffers
,
548 const VkDeviceSize
* pOffsets
)
550 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
551 struct anv_vertex_binding
*vb
= cmd_buffer
->state
.vertex_bindings
;
553 /* We have to defer setting up vertex buffer since we need the buffer
554 * stride from the pipeline. */
556 assert(firstBinding
+ bindingCount
< MAX_VBS
);
557 for (uint32_t i
= 0; i
< bindingCount
; i
++) {
558 vb
[firstBinding
+ i
].buffer
= anv_buffer_from_handle(pBuffers
[i
]);
559 vb
[firstBinding
+ i
].offset
= pOffsets
[i
];
560 cmd_buffer
->state
.vb_dirty
|= 1 << (firstBinding
+ i
);
565 anv_isl_format_for_descriptor_type(VkDescriptorType type
)
568 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
569 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
570 return ISL_FORMAT_R32G32B32A32_FLOAT
;
572 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
573 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
574 return ISL_FORMAT_RAW
;
577 unreachable("Invalid descriptor type");
582 anv_cmd_buffer_emit_dynamic(struct anv_cmd_buffer
*cmd_buffer
,
583 const void *data
, uint32_t size
, uint32_t alignment
)
585 struct anv_state state
;
587 state
= anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, size
, alignment
);
588 memcpy(state
.map
, data
, size
);
590 if (!cmd_buffer
->device
->info
.has_llc
)
591 anv_state_flush(state
);
593 VG(VALGRIND_CHECK_MEM_IS_DEFINED(state
.map
, size
));
599 anv_cmd_buffer_merge_dynamic(struct anv_cmd_buffer
*cmd_buffer
,
600 uint32_t *a
, uint32_t *b
,
601 uint32_t dwords
, uint32_t alignment
)
603 struct anv_state state
;
606 state
= anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
,
607 dwords
* 4, alignment
);
609 for (uint32_t i
= 0; i
< dwords
; i
++)
612 if (!cmd_buffer
->device
->info
.has_llc
)
613 anv_state_flush(state
);
615 VG(VALGRIND_CHECK_MEM_IS_DEFINED(p
, dwords
* 4));
621 anv_cmd_buffer_push_constants(struct anv_cmd_buffer
*cmd_buffer
,
622 gl_shader_stage stage
)
624 /* If we don't have this stage, bail. */
625 if (!anv_pipeline_has_stage(cmd_buffer
->state
.pipeline
, stage
))
626 return (struct anv_state
) { .offset
= 0 };
628 struct anv_push_constants
*data
=
629 cmd_buffer
->state
.push_constants
[stage
];
630 const struct brw_stage_prog_data
*prog_data
=
631 cmd_buffer
->state
.pipeline
->shaders
[stage
]->prog_data
;
633 /* If we don't actually have any push constants, bail. */
634 if (data
== NULL
|| prog_data
== NULL
|| prog_data
->nr_params
== 0)
635 return (struct anv_state
) { .offset
= 0 };
637 struct anv_state state
=
638 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
,
639 prog_data
->nr_params
* sizeof(float),
640 32 /* bottom 5 bits MBZ */);
642 /* Walk through the param array and fill the buffer with data */
643 uint32_t *u32_map
= state
.map
;
644 for (unsigned i
= 0; i
< prog_data
->nr_params
; i
++) {
645 uint32_t offset
= (uintptr_t)prog_data
->param
[i
];
646 u32_map
[i
] = *(uint32_t *)((uint8_t *)data
+ offset
);
649 if (!cmd_buffer
->device
->info
.has_llc
)
650 anv_state_flush(state
);
656 anv_cmd_buffer_cs_push_constants(struct anv_cmd_buffer
*cmd_buffer
)
658 struct anv_push_constants
*data
=
659 cmd_buffer
->state
.push_constants
[MESA_SHADER_COMPUTE
];
660 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute_pipeline
;
661 const struct brw_cs_prog_data
*cs_prog_data
= get_cs_prog_data(pipeline
);
662 const struct brw_stage_prog_data
*prog_data
= &cs_prog_data
->base
;
664 /* If we don't actually have any push constants, bail. */
665 if (cs_prog_data
->push
.total
.size
== 0)
666 return (struct anv_state
) { .offset
= 0 };
668 const unsigned push_constant_alignment
=
669 cmd_buffer
->device
->info
.gen
< 8 ? 32 : 64;
670 const unsigned aligned_total_push_constants_size
=
671 ALIGN(cs_prog_data
->push
.total
.size
, push_constant_alignment
);
672 struct anv_state state
=
673 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
,
674 aligned_total_push_constants_size
,
675 push_constant_alignment
);
677 /* Walk through the param array and fill the buffer with data */
678 uint32_t *u32_map
= state
.map
;
680 if (cs_prog_data
->push
.cross_thread
.size
> 0) {
681 assert(cs_prog_data
->thread_local_id_index
< 0 ||
682 cs_prog_data
->thread_local_id_index
>=
683 cs_prog_data
->push
.cross_thread
.dwords
);
685 i
< cs_prog_data
->push
.cross_thread
.dwords
;
687 uint32_t offset
= (uintptr_t)prog_data
->param
[i
];
688 u32_map
[i
] = *(uint32_t *)((uint8_t *)data
+ offset
);
692 if (cs_prog_data
->push
.per_thread
.size
> 0) {
693 for (unsigned t
= 0; t
< cs_prog_data
->threads
; t
++) {
695 8 * (cs_prog_data
->push
.per_thread
.regs
* t
+
696 cs_prog_data
->push
.cross_thread
.regs
);
697 unsigned src
= cs_prog_data
->push
.cross_thread
.dwords
;
698 for ( ; src
< prog_data
->nr_params
; src
++, dst
++) {
699 if (src
!= cs_prog_data
->thread_local_id_index
) {
700 uint32_t offset
= (uintptr_t)prog_data
->param
[src
];
701 u32_map
[dst
] = *(uint32_t *)((uint8_t *)data
+ offset
);
703 u32_map
[dst
] = t
* cs_prog_data
->simd_size
;
709 if (!cmd_buffer
->device
->info
.has_llc
)
710 anv_state_flush(state
);
715 void anv_CmdPushConstants(
716 VkCommandBuffer commandBuffer
,
717 VkPipelineLayout layout
,
718 VkShaderStageFlags stageFlags
,
723 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
725 anv_foreach_stage(stage
, stageFlags
) {
726 anv_cmd_buffer_ensure_push_constant_field(cmd_buffer
, stage
, client_data
);
728 memcpy(cmd_buffer
->state
.push_constants
[stage
]->client_data
+ offset
,
732 cmd_buffer
->state
.push_constants_dirty
|= stageFlags
;
735 VkResult
anv_CreateCommandPool(
737 const VkCommandPoolCreateInfo
* pCreateInfo
,
738 const VkAllocationCallbacks
* pAllocator
,
739 VkCommandPool
* pCmdPool
)
741 ANV_FROM_HANDLE(anv_device
, device
, _device
);
742 struct anv_cmd_pool
*pool
;
744 pool
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*pool
), 8,
745 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
747 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
750 pool
->alloc
= *pAllocator
;
752 pool
->alloc
= device
->alloc
;
754 list_inithead(&pool
->cmd_buffers
);
756 *pCmdPool
= anv_cmd_pool_to_handle(pool
);
761 void anv_DestroyCommandPool(
763 VkCommandPool commandPool
,
764 const VkAllocationCallbacks
* pAllocator
)
766 ANV_FROM_HANDLE(anv_device
, device
, _device
);
767 ANV_FROM_HANDLE(anv_cmd_pool
, pool
, commandPool
);
772 list_for_each_entry_safe(struct anv_cmd_buffer
, cmd_buffer
,
773 &pool
->cmd_buffers
, pool_link
) {
774 anv_cmd_buffer_destroy(cmd_buffer
);
777 vk_free2(&device
->alloc
, pAllocator
, pool
);
780 VkResult
anv_ResetCommandPool(
782 VkCommandPool commandPool
,
783 VkCommandPoolResetFlags flags
)
785 ANV_FROM_HANDLE(anv_cmd_pool
, pool
, commandPool
);
787 list_for_each_entry(struct anv_cmd_buffer
, cmd_buffer
,
788 &pool
->cmd_buffers
, pool_link
) {
789 anv_cmd_buffer_reset(cmd_buffer
);
795 void anv_TrimCommandPoolKHR(
797 VkCommandPool commandPool
,
798 VkCommandPoolTrimFlagsKHR flags
)
800 /* Nothing for us to do here. Our pools stay pretty tidy. */
804 * Return NULL if the current subpass has no depthstencil attachment.
806 const struct anv_image_view
*
807 anv_cmd_buffer_get_depth_stencil_view(const struct anv_cmd_buffer
*cmd_buffer
)
809 const struct anv_subpass
*subpass
= cmd_buffer
->state
.subpass
;
810 const struct anv_framebuffer
*fb
= cmd_buffer
->state
.framebuffer
;
812 if (subpass
->depth_stencil_attachment
== VK_ATTACHMENT_UNUSED
)
815 const struct anv_image_view
*iview
=
816 fb
->attachments
[subpass
->depth_stencil_attachment
];
818 assert(iview
->aspect_mask
& (VK_IMAGE_ASPECT_DEPTH_BIT
|
819 VK_IMAGE_ASPECT_STENCIL_BIT
));