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 anv_cmd_buffer_ensure_push_constants_size(struct anv_cmd_buffer
*cmd_buffer
,
149 gl_shader_stage stage
, uint32_t size
)
151 struct anv_push_constants
**ptr
= &cmd_buffer
->state
.push_constants
[stage
];
154 *ptr
= vk_alloc(&cmd_buffer
->pool
->alloc
, size
, 8,
155 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
157 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
158 } else if ((*ptr
)->size
< size
) {
159 *ptr
= vk_realloc(&cmd_buffer
->pool
->alloc
, *ptr
, size
, 8,
160 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
162 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
169 static VkResult
anv_create_cmd_buffer(
170 struct anv_device
* device
,
171 struct anv_cmd_pool
* pool
,
172 VkCommandBufferLevel level
,
173 VkCommandBuffer
* pCommandBuffer
)
175 struct anv_cmd_buffer
*cmd_buffer
;
178 cmd_buffer
= vk_alloc(&pool
->alloc
, sizeof(*cmd_buffer
), 8,
179 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
180 if (cmd_buffer
== NULL
)
181 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
183 cmd_buffer
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
184 cmd_buffer
->device
= device
;
185 cmd_buffer
->pool
= pool
;
186 cmd_buffer
->level
= level
;
187 cmd_buffer
->state
.attachments
= NULL
;
189 result
= anv_cmd_buffer_init_batch_bo_chain(cmd_buffer
);
190 if (result
!= VK_SUCCESS
)
193 anv_state_stream_init(&cmd_buffer
->surface_state_stream
,
194 &device
->surface_state_block_pool
);
195 anv_state_stream_init(&cmd_buffer
->dynamic_state_stream
,
196 &device
->dynamic_state_block_pool
);
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
,
243 anv_cmd_buffer_destroy(struct anv_cmd_buffer
*cmd_buffer
)
245 list_del(&cmd_buffer
->pool_link
);
247 anv_cmd_buffer_fini_batch_bo_chain(cmd_buffer
);
249 anv_state_stream_finish(&cmd_buffer
->surface_state_stream
);
250 anv_state_stream_finish(&cmd_buffer
->dynamic_state_stream
);
252 vk_free(&cmd_buffer
->pool
->alloc
, cmd_buffer
->state
.attachments
);
253 vk_free(&cmd_buffer
->pool
->alloc
, cmd_buffer
);
256 void anv_FreeCommandBuffers(
258 VkCommandPool commandPool
,
259 uint32_t commandBufferCount
,
260 const VkCommandBuffer
* pCommandBuffers
)
262 for (uint32_t i
= 0; i
< commandBufferCount
; i
++) {
263 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, pCommandBuffers
[i
]);
265 anv_cmd_buffer_destroy(cmd_buffer
);
270 anv_cmd_buffer_reset(struct anv_cmd_buffer
*cmd_buffer
)
272 cmd_buffer
->usage_flags
= 0;
273 cmd_buffer
->state
.current_pipeline
= UINT32_MAX
;
274 anv_cmd_buffer_reset_batch_bo_chain(cmd_buffer
);
275 anv_cmd_state_reset(cmd_buffer
);
277 anv_state_stream_finish(&cmd_buffer
->surface_state_stream
);
278 anv_state_stream_init(&cmd_buffer
->surface_state_stream
,
279 &cmd_buffer
->device
->surface_state_block_pool
);
281 anv_state_stream_finish(&cmd_buffer
->dynamic_state_stream
);
282 anv_state_stream_init(&cmd_buffer
->dynamic_state_stream
,
283 &cmd_buffer
->device
->dynamic_state_block_pool
);
287 VkResult
anv_ResetCommandBuffer(
288 VkCommandBuffer commandBuffer
,
289 VkCommandBufferResetFlags flags
)
291 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
292 return anv_cmd_buffer_reset(cmd_buffer
);
296 anv_cmd_buffer_emit_state_base_address(struct anv_cmd_buffer
*cmd_buffer
)
298 switch (cmd_buffer
->device
->info
.gen
) {
300 if (cmd_buffer
->device
->info
.is_haswell
)
301 return gen75_cmd_buffer_emit_state_base_address(cmd_buffer
);
303 return gen7_cmd_buffer_emit_state_base_address(cmd_buffer
);
305 return gen8_cmd_buffer_emit_state_base_address(cmd_buffer
);
307 return gen9_cmd_buffer_emit_state_base_address(cmd_buffer
);
309 unreachable("unsupported gen\n");
313 void anv_CmdBindPipeline(
314 VkCommandBuffer commandBuffer
,
315 VkPipelineBindPoint pipelineBindPoint
,
316 VkPipeline _pipeline
)
318 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
319 ANV_FROM_HANDLE(anv_pipeline
, pipeline
, _pipeline
);
321 switch (pipelineBindPoint
) {
322 case VK_PIPELINE_BIND_POINT_COMPUTE
:
323 cmd_buffer
->state
.compute_pipeline
= pipeline
;
324 cmd_buffer
->state
.compute_dirty
|= ANV_CMD_DIRTY_PIPELINE
;
325 cmd_buffer
->state
.push_constants_dirty
|= VK_SHADER_STAGE_COMPUTE_BIT
;
326 cmd_buffer
->state
.descriptors_dirty
|= VK_SHADER_STAGE_COMPUTE_BIT
;
329 case VK_PIPELINE_BIND_POINT_GRAPHICS
:
330 cmd_buffer
->state
.pipeline
= pipeline
;
331 cmd_buffer
->state
.vb_dirty
|= pipeline
->vb_used
;
332 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_PIPELINE
;
333 cmd_buffer
->state
.push_constants_dirty
|= pipeline
->active_stages
;
334 cmd_buffer
->state
.descriptors_dirty
|= pipeline
->active_stages
;
336 /* Apply the dynamic state from the pipeline */
337 cmd_buffer
->state
.dirty
|= pipeline
->dynamic_state_mask
;
338 anv_dynamic_state_copy(&cmd_buffer
->state
.dynamic
,
339 &pipeline
->dynamic_state
,
340 pipeline
->dynamic_state_mask
);
344 assert(!"invalid bind point");
349 void anv_CmdSetViewport(
350 VkCommandBuffer commandBuffer
,
351 uint32_t firstViewport
,
352 uint32_t viewportCount
,
353 const VkViewport
* pViewports
)
355 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
357 const uint32_t total_count
= firstViewport
+ viewportCount
;
358 if (cmd_buffer
->state
.dynamic
.viewport
.count
< total_count
)
359 cmd_buffer
->state
.dynamic
.viewport
.count
= total_count
;
361 memcpy(cmd_buffer
->state
.dynamic
.viewport
.viewports
+ firstViewport
,
362 pViewports
, viewportCount
* sizeof(*pViewports
));
364 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_VIEWPORT
;
367 void anv_CmdSetScissor(
368 VkCommandBuffer commandBuffer
,
369 uint32_t firstScissor
,
370 uint32_t scissorCount
,
371 const VkRect2D
* pScissors
)
373 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
375 const uint32_t total_count
= firstScissor
+ scissorCount
;
376 if (cmd_buffer
->state
.dynamic
.scissor
.count
< total_count
)
377 cmd_buffer
->state
.dynamic
.scissor
.count
= total_count
;
379 memcpy(cmd_buffer
->state
.dynamic
.scissor
.scissors
+ firstScissor
,
380 pScissors
, scissorCount
* sizeof(*pScissors
));
382 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_SCISSOR
;
385 void anv_CmdSetLineWidth(
386 VkCommandBuffer commandBuffer
,
389 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
391 cmd_buffer
->state
.dynamic
.line_width
= lineWidth
;
392 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_LINE_WIDTH
;
395 void anv_CmdSetDepthBias(
396 VkCommandBuffer commandBuffer
,
397 float depthBiasConstantFactor
,
398 float depthBiasClamp
,
399 float depthBiasSlopeFactor
)
401 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
403 cmd_buffer
->state
.dynamic
.depth_bias
.bias
= depthBiasConstantFactor
;
404 cmd_buffer
->state
.dynamic
.depth_bias
.clamp
= depthBiasClamp
;
405 cmd_buffer
->state
.dynamic
.depth_bias
.slope
= depthBiasSlopeFactor
;
407 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_DEPTH_BIAS
;
410 void anv_CmdSetBlendConstants(
411 VkCommandBuffer commandBuffer
,
412 const float blendConstants
[4])
414 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
416 memcpy(cmd_buffer
->state
.dynamic
.blend_constants
,
417 blendConstants
, sizeof(float) * 4);
419 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_BLEND_CONSTANTS
;
422 void anv_CmdSetDepthBounds(
423 VkCommandBuffer commandBuffer
,
424 float minDepthBounds
,
425 float maxDepthBounds
)
427 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
429 cmd_buffer
->state
.dynamic
.depth_bounds
.min
= minDepthBounds
;
430 cmd_buffer
->state
.dynamic
.depth_bounds
.max
= maxDepthBounds
;
432 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_DEPTH_BOUNDS
;
435 void anv_CmdSetStencilCompareMask(
436 VkCommandBuffer commandBuffer
,
437 VkStencilFaceFlags faceMask
,
438 uint32_t compareMask
)
440 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
442 if (faceMask
& VK_STENCIL_FACE_FRONT_BIT
)
443 cmd_buffer
->state
.dynamic
.stencil_compare_mask
.front
= compareMask
;
444 if (faceMask
& VK_STENCIL_FACE_BACK_BIT
)
445 cmd_buffer
->state
.dynamic
.stencil_compare_mask
.back
= compareMask
;
447 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_STENCIL_COMPARE_MASK
;
450 void anv_CmdSetStencilWriteMask(
451 VkCommandBuffer commandBuffer
,
452 VkStencilFaceFlags faceMask
,
455 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
457 if (faceMask
& VK_STENCIL_FACE_FRONT_BIT
)
458 cmd_buffer
->state
.dynamic
.stencil_write_mask
.front
= writeMask
;
459 if (faceMask
& VK_STENCIL_FACE_BACK_BIT
)
460 cmd_buffer
->state
.dynamic
.stencil_write_mask
.back
= writeMask
;
462 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_STENCIL_WRITE_MASK
;
465 void anv_CmdSetStencilReference(
466 VkCommandBuffer commandBuffer
,
467 VkStencilFaceFlags faceMask
,
470 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
472 if (faceMask
& VK_STENCIL_FACE_FRONT_BIT
)
473 cmd_buffer
->state
.dynamic
.stencil_reference
.front
= reference
;
474 if (faceMask
& VK_STENCIL_FACE_BACK_BIT
)
475 cmd_buffer
->state
.dynamic
.stencil_reference
.back
= reference
;
477 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_DYNAMIC_STENCIL_REFERENCE
;
480 void anv_CmdBindDescriptorSets(
481 VkCommandBuffer commandBuffer
,
482 VkPipelineBindPoint pipelineBindPoint
,
483 VkPipelineLayout _layout
,
485 uint32_t descriptorSetCount
,
486 const VkDescriptorSet
* pDescriptorSets
,
487 uint32_t dynamicOffsetCount
,
488 const uint32_t* pDynamicOffsets
)
490 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
491 ANV_FROM_HANDLE(anv_pipeline_layout
, layout
, _layout
);
492 struct anv_descriptor_set_layout
*set_layout
;
494 assert(firstSet
+ descriptorSetCount
< MAX_SETS
);
496 for (uint32_t i
= 0; i
< descriptorSetCount
; i
++) {
497 ANV_FROM_HANDLE(anv_descriptor_set
, set
, pDescriptorSets
[i
]);
498 set_layout
= layout
->set
[firstSet
+ i
].layout
;
500 if (cmd_buffer
->state
.descriptors
[firstSet
+ i
] != set
) {
501 cmd_buffer
->state
.descriptors
[firstSet
+ i
] = set
;
502 cmd_buffer
->state
.descriptors_dirty
|= set_layout
->shader_stages
;
505 if (set_layout
->dynamic_offset_count
> 0) {
506 anv_foreach_stage(s
, set_layout
->shader_stages
) {
507 anv_cmd_buffer_ensure_push_constant_field(cmd_buffer
, s
, dynamic
);
509 struct anv_push_constants
*push
=
510 cmd_buffer
->state
.push_constants
[s
];
512 unsigned d
= layout
->set
[firstSet
+ i
].dynamic_offset_start
;
513 const uint32_t *offsets
= pDynamicOffsets
;
514 struct anv_descriptor
*desc
= set
->descriptors
;
516 for (unsigned b
= 0; b
< set_layout
->binding_count
; b
++) {
517 if (set_layout
->binding
[b
].dynamic_offset_index
< 0)
520 unsigned array_size
= set_layout
->binding
[b
].array_size
;
521 for (unsigned j
= 0; j
< array_size
; j
++) {
522 push
->dynamic
[d
].offset
= *(offsets
++);
523 push
->dynamic
[d
].range
= (desc
->buffer_view
) ?
524 desc
->buffer_view
->range
: 0;
530 cmd_buffer
->state
.push_constants_dirty
|= set_layout
->shader_stages
;
535 void anv_CmdBindVertexBuffers(
536 VkCommandBuffer commandBuffer
,
537 uint32_t firstBinding
,
538 uint32_t bindingCount
,
539 const VkBuffer
* pBuffers
,
540 const VkDeviceSize
* pOffsets
)
542 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
543 struct anv_vertex_binding
*vb
= cmd_buffer
->state
.vertex_bindings
;
545 /* We have to defer setting up vertex buffer since we need the buffer
546 * stride from the pipeline. */
548 assert(firstBinding
+ bindingCount
< MAX_VBS
);
549 for (uint32_t i
= 0; i
< bindingCount
; i
++) {
550 vb
[firstBinding
+ i
].buffer
= anv_buffer_from_handle(pBuffers
[i
]);
551 vb
[firstBinding
+ i
].offset
= pOffsets
[i
];
552 cmd_buffer
->state
.vb_dirty
|= 1 << (firstBinding
+ i
);
557 anv_isl_format_for_descriptor_type(VkDescriptorType type
)
560 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
561 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
562 return ISL_FORMAT_R32G32B32A32_FLOAT
;
564 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
565 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
566 return ISL_FORMAT_RAW
;
569 unreachable("Invalid descriptor type");
574 anv_cmd_buffer_emit_dynamic(struct anv_cmd_buffer
*cmd_buffer
,
575 const void *data
, uint32_t size
, uint32_t alignment
)
577 struct anv_state state
;
579 state
= anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, size
, alignment
);
580 memcpy(state
.map
, data
, size
);
582 if (!cmd_buffer
->device
->info
.has_llc
)
583 anv_state_clflush(state
);
585 VG(VALGRIND_CHECK_MEM_IS_DEFINED(state
.map
, size
));
591 anv_cmd_buffer_merge_dynamic(struct anv_cmd_buffer
*cmd_buffer
,
592 uint32_t *a
, uint32_t *b
,
593 uint32_t dwords
, uint32_t alignment
)
595 struct anv_state state
;
598 state
= anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
,
599 dwords
* 4, alignment
);
601 for (uint32_t i
= 0; i
< dwords
; i
++)
604 if (!cmd_buffer
->device
->info
.has_llc
)
605 anv_state_clflush(state
);
607 VG(VALGRIND_CHECK_MEM_IS_DEFINED(p
, dwords
* 4));
613 anv_cmd_buffer_push_constants(struct anv_cmd_buffer
*cmd_buffer
,
614 gl_shader_stage stage
)
616 /* If we don't have this stage, bail. */
617 if (!anv_pipeline_has_stage(cmd_buffer
->state
.pipeline
, stage
))
618 return (struct anv_state
) { .offset
= 0 };
620 struct anv_push_constants
*data
=
621 cmd_buffer
->state
.push_constants
[stage
];
622 const struct brw_stage_prog_data
*prog_data
=
623 cmd_buffer
->state
.pipeline
->shaders
[stage
]->prog_data
;
625 /* If we don't actually have any push constants, bail. */
626 if (data
== NULL
|| prog_data
== NULL
|| prog_data
->nr_params
== 0)
627 return (struct anv_state
) { .offset
= 0 };
629 struct anv_state state
=
630 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
,
631 prog_data
->nr_params
* sizeof(float),
632 32 /* bottom 5 bits MBZ */);
634 /* Walk through the param array and fill the buffer with data */
635 uint32_t *u32_map
= state
.map
;
636 for (unsigned i
= 0; i
< prog_data
->nr_params
; i
++) {
637 uint32_t offset
= (uintptr_t)prog_data
->param
[i
];
638 u32_map
[i
] = *(uint32_t *)((uint8_t *)data
+ offset
);
641 if (!cmd_buffer
->device
->info
.has_llc
)
642 anv_state_clflush(state
);
648 anv_cmd_buffer_cs_push_constants(struct anv_cmd_buffer
*cmd_buffer
)
650 struct anv_push_constants
*data
=
651 cmd_buffer
->state
.push_constants
[MESA_SHADER_COMPUTE
];
652 struct anv_pipeline
*pipeline
= cmd_buffer
->state
.compute_pipeline
;
653 const struct brw_cs_prog_data
*cs_prog_data
= get_cs_prog_data(pipeline
);
654 const struct brw_stage_prog_data
*prog_data
= &cs_prog_data
->base
;
656 /* If we don't actually have any push constants, bail. */
657 if (cs_prog_data
->push
.total
.size
== 0)
658 return (struct anv_state
) { .offset
= 0 };
660 const unsigned push_constant_alignment
=
661 cmd_buffer
->device
->info
.gen
< 8 ? 32 : 64;
662 const unsigned aligned_total_push_constants_size
=
663 ALIGN(cs_prog_data
->push
.total
.size
, push_constant_alignment
);
664 struct anv_state state
=
665 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
,
666 aligned_total_push_constants_size
,
667 push_constant_alignment
);
669 /* Walk through the param array and fill the buffer with data */
670 uint32_t *u32_map
= state
.map
;
672 if (cs_prog_data
->push
.cross_thread
.size
> 0) {
673 assert(cs_prog_data
->thread_local_id_index
< 0 ||
674 cs_prog_data
->thread_local_id_index
>=
675 cs_prog_data
->push
.cross_thread
.dwords
);
677 i
< cs_prog_data
->push
.cross_thread
.dwords
;
679 uint32_t offset
= (uintptr_t)prog_data
->param
[i
];
680 u32_map
[i
] = *(uint32_t *)((uint8_t *)data
+ offset
);
684 if (cs_prog_data
->push
.per_thread
.size
> 0) {
685 for (unsigned t
= 0; t
< cs_prog_data
->threads
; t
++) {
687 8 * (cs_prog_data
->push
.per_thread
.regs
* t
+
688 cs_prog_data
->push
.cross_thread
.regs
);
689 unsigned src
= cs_prog_data
->push
.cross_thread
.dwords
;
690 for ( ; src
< prog_data
->nr_params
; src
++, dst
++) {
691 if (src
!= cs_prog_data
->thread_local_id_index
) {
692 uint32_t offset
= (uintptr_t)prog_data
->param
[src
];
693 u32_map
[dst
] = *(uint32_t *)((uint8_t *)data
+ offset
);
695 u32_map
[dst
] = t
* cs_prog_data
->simd_size
;
701 if (!cmd_buffer
->device
->info
.has_llc
)
702 anv_state_clflush(state
);
707 void anv_CmdPushConstants(
708 VkCommandBuffer commandBuffer
,
709 VkPipelineLayout layout
,
710 VkShaderStageFlags stageFlags
,
715 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
717 anv_foreach_stage(stage
, stageFlags
) {
718 anv_cmd_buffer_ensure_push_constant_field(cmd_buffer
, stage
, client_data
);
720 memcpy(cmd_buffer
->state
.push_constants
[stage
]->client_data
+ offset
,
724 cmd_buffer
->state
.push_constants_dirty
|= stageFlags
;
727 VkResult
anv_CreateCommandPool(
729 const VkCommandPoolCreateInfo
* pCreateInfo
,
730 const VkAllocationCallbacks
* pAllocator
,
731 VkCommandPool
* pCmdPool
)
733 ANV_FROM_HANDLE(anv_device
, device
, _device
);
734 struct anv_cmd_pool
*pool
;
736 pool
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*pool
), 8,
737 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
739 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
742 pool
->alloc
= *pAllocator
;
744 pool
->alloc
= device
->alloc
;
746 list_inithead(&pool
->cmd_buffers
);
748 *pCmdPool
= anv_cmd_pool_to_handle(pool
);
753 void anv_DestroyCommandPool(
755 VkCommandPool commandPool
,
756 const VkAllocationCallbacks
* pAllocator
)
758 ANV_FROM_HANDLE(anv_device
, device
, _device
);
759 ANV_FROM_HANDLE(anv_cmd_pool
, pool
, commandPool
);
761 list_for_each_entry_safe(struct anv_cmd_buffer
, cmd_buffer
,
762 &pool
->cmd_buffers
, pool_link
) {
763 anv_cmd_buffer_destroy(cmd_buffer
);
766 vk_free2(&device
->alloc
, pAllocator
, pool
);
769 VkResult
anv_ResetCommandPool(
771 VkCommandPool commandPool
,
772 VkCommandPoolResetFlags flags
)
774 ANV_FROM_HANDLE(anv_cmd_pool
, pool
, commandPool
);
776 list_for_each_entry(struct anv_cmd_buffer
, cmd_buffer
,
777 &pool
->cmd_buffers
, pool_link
) {
778 anv_cmd_buffer_reset(cmd_buffer
);
785 * Return NULL if the current subpass has no depthstencil attachment.
787 const struct anv_image_view
*
788 anv_cmd_buffer_get_depth_stencil_view(const struct anv_cmd_buffer
*cmd_buffer
)
790 const struct anv_subpass
*subpass
= cmd_buffer
->state
.subpass
;
791 const struct anv_framebuffer
*fb
= cmd_buffer
->state
.framebuffer
;
793 if (subpass
->depth_stencil_attachment
== VK_ATTACHMENT_UNUSED
)
796 const struct anv_image_view
*iview
=
797 fb
->attachments
[subpass
->depth_stencil_attachment
];
799 assert(iview
->aspect_mask
& (VK_IMAGE_ASPECT_DEPTH_BIT
|
800 VK_IMAGE_ASPECT_STENCIL_BIT
));