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
31 #include "anv_meta_clear.h"
32 #include "anv_private.h"
33 #include "glsl/nir/nir_builder.h"
35 struct anv_render_pass anv_meta_dummy_renderpass
= {0};
38 build_nir_vertex_shader(bool attr_flat
)
42 const struct glsl_type
*vertex_type
= glsl_vec4_type();
44 nir_builder_init_simple_shader(&b
, NULL
, MESA_SHADER_VERTEX
, NULL
);
46 nir_variable
*pos_in
= nir_variable_create(b
.shader
, nir_var_shader_in
,
47 vertex_type
, "a_pos");
48 pos_in
->data
.location
= VERT_ATTRIB_GENERIC0
;
49 nir_variable
*pos_out
= nir_variable_create(b
.shader
, nir_var_shader_out
,
50 vertex_type
, "gl_Position");
51 pos_out
->data
.location
= VARYING_SLOT_POS
;
52 nir_copy_var(&b
, pos_out
, pos_in
);
54 /* Add one more pass-through attribute. For clear shaders, this is used
55 * to store the color and for blit shaders it's the texture coordinate.
57 const struct glsl_type
*attr_type
= glsl_vec4_type();
58 nir_variable
*attr_in
= nir_variable_create(b
.shader
, nir_var_shader_in
,
60 attr_in
->data
.location
= VERT_ATTRIB_GENERIC1
;
61 nir_variable
*attr_out
= nir_variable_create(b
.shader
, nir_var_shader_out
,
63 attr_out
->data
.location
= VARYING_SLOT_VAR0
;
64 attr_out
->data
.interpolation
= attr_flat
? INTERP_QUALIFIER_FLAT
:
65 INTERP_QUALIFIER_SMOOTH
;
66 nir_copy_var(&b
, attr_out
, attr_in
);
72 build_nir_copy_fragment_shader(enum glsl_sampler_dim tex_dim
)
76 nir_builder_init_simple_shader(&b
, NULL
, MESA_SHADER_FRAGMENT
, NULL
);
78 const struct glsl_type
*color_type
= glsl_vec4_type();
80 nir_variable
*tex_pos_in
= nir_variable_create(b
.shader
, nir_var_shader_in
,
81 glsl_vec4_type(), "v_attr");
82 tex_pos_in
->data
.location
= VARYING_SLOT_VAR0
;
84 const struct glsl_type
*sampler_type
=
85 glsl_sampler_type(tex_dim
, false, false, glsl_get_base_type(color_type
));
86 nir_variable
*sampler
= nir_variable_create(b
.shader
, nir_var_uniform
,
87 sampler_type
, "s_tex");
88 sampler
->data
.descriptor_set
= 0;
89 sampler
->data
.binding
= 0;
91 nir_tex_instr
*tex
= nir_tex_instr_create(b
.shader
, 1);
92 tex
->sampler_dim
= tex_dim
;
93 tex
->op
= nir_texop_tex
;
94 tex
->src
[0].src_type
= nir_tex_src_coord
;
95 tex
->src
[0].src
= nir_src_for_ssa(nir_load_var(&b
, tex_pos_in
));
96 tex
->dest_type
= nir_type_float
; /* TODO */
98 if (tex_dim
!= GLSL_SAMPLER_DIM_3D
)
101 tex
->coord_components
= 3;
103 tex
->sampler
= nir_deref_var_create(tex
, sampler
);
105 nir_ssa_dest_init(&tex
->instr
, &tex
->dest
, 4, "tex");
106 nir_builder_instr_insert(&b
, &tex
->instr
);
108 nir_variable
*color_out
= nir_variable_create(b
.shader
, nir_var_shader_out
,
109 color_type
, "f_color");
110 color_out
->data
.location
= FRAG_RESULT_DATA0
;
111 nir_store_var(&b
, color_out
, &tex
->dest
.ssa
, 4);
117 anv_meta_save(struct anv_meta_saved_state
*state
,
118 const struct anv_cmd_buffer
*cmd_buffer
,
119 uint32_t dynamic_mask
)
121 state
->old_pipeline
= cmd_buffer
->state
.pipeline
;
122 state
->old_descriptor_set0
= cmd_buffer
->state
.descriptors
[0];
123 memcpy(state
->old_vertex_bindings
, cmd_buffer
->state
.vertex_bindings
,
124 sizeof(state
->old_vertex_bindings
));
126 state
->dynamic_mask
= dynamic_mask
;
127 anv_dynamic_state_copy(&state
->dynamic
, &cmd_buffer
->state
.dynamic
,
132 anv_meta_restore(const struct anv_meta_saved_state
*state
,
133 struct anv_cmd_buffer
*cmd_buffer
)
135 cmd_buffer
->state
.pipeline
= state
->old_pipeline
;
136 cmd_buffer
->state
.descriptors
[0] = state
->old_descriptor_set0
;
137 memcpy(cmd_buffer
->state
.vertex_bindings
, state
->old_vertex_bindings
,
138 sizeof(state
->old_vertex_bindings
));
140 cmd_buffer
->state
.vb_dirty
|= (1 << ANV_META_VERTEX_BINDING_COUNT
) - 1;
141 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_PIPELINE
;
142 cmd_buffer
->state
.descriptors_dirty
|= VK_SHADER_STAGE_VERTEX_BIT
;
144 anv_dynamic_state_copy(&cmd_buffer
->state
.dynamic
, &state
->dynamic
,
145 state
->dynamic_mask
);
146 cmd_buffer
->state
.dirty
|= state
->dynamic_mask
;
148 /* Since we've used the pipeline with the VS disabled, set
149 * need_query_wa. See CmdBeginQuery.
151 cmd_buffer
->state
.need_query_wa
= true;
155 anv_meta_get_view_type(const struct anv_image
*image
)
157 switch (image
->type
) {
158 case VK_IMAGE_TYPE_1D
: return VK_IMAGE_VIEW_TYPE_1D
;
159 case VK_IMAGE_TYPE_2D
: return VK_IMAGE_VIEW_TYPE_2D
;
160 case VK_IMAGE_TYPE_3D
: return VK_IMAGE_VIEW_TYPE_3D
;
162 unreachable("bad VkImageViewType");
167 meta_blit_get_dest_view_base_array_slice(const struct anv_image
*dest_image
,
168 const VkImageSubresourceLayers
*dest_subresource
,
169 const VkOffset3D
*dest_offset
)
171 switch (dest_image
->type
) {
172 case VK_IMAGE_TYPE_1D
:
173 case VK_IMAGE_TYPE_2D
:
174 return dest_subresource
->baseArrayLayer
;
175 case VK_IMAGE_TYPE_3D
:
176 /* HACK: Vulkan does not allow attaching a 3D image to a framebuffer,
177 * but meta does it anyway. When doing so, we translate the
178 * destination's z offset into an array offset.
180 return dest_offset
->z
;
182 assert(!"bad VkImageType");
188 anv_device_init_meta_blit_state(struct anv_device
*device
)
192 result
= anv_CreateRenderPass(anv_device_to_handle(device
),
193 &(VkRenderPassCreateInfo
) {
194 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO
,
195 .attachmentCount
= 1,
196 .pAttachments
= &(VkAttachmentDescription
) {
197 .format
= VK_FORMAT_UNDEFINED
, /* Our shaders don't care */
198 .loadOp
= VK_ATTACHMENT_LOAD_OP_LOAD
,
199 .storeOp
= VK_ATTACHMENT_STORE_OP_STORE
,
200 .initialLayout
= VK_IMAGE_LAYOUT_GENERAL
,
201 .finalLayout
= VK_IMAGE_LAYOUT_GENERAL
,
204 .pSubpasses
= &(VkSubpassDescription
) {
205 .pipelineBindPoint
= VK_PIPELINE_BIND_POINT_GRAPHICS
,
206 .inputAttachmentCount
= 0,
207 .colorAttachmentCount
= 1,
208 .pColorAttachments
= &(VkAttachmentReference
) {
210 .layout
= VK_IMAGE_LAYOUT_GENERAL
,
212 .pResolveAttachments
= NULL
,
213 .pDepthStencilAttachment
= &(VkAttachmentReference
) {
214 .attachment
= VK_ATTACHMENT_UNUSED
,
215 .layout
= VK_IMAGE_LAYOUT_GENERAL
,
217 .preserveAttachmentCount
= 1,
218 .pPreserveAttachments
= (uint32_t[]) { 0 },
220 .dependencyCount
= 0,
221 }, NULL
, &device
->meta_state
.blit
.render_pass
);
222 if (result
!= VK_SUCCESS
)
225 /* We don't use a vertex shader for clearing, but instead build and pass
226 * the VUEs directly to the rasterization backend. However, we do need
227 * to provide GLSL source for the vertex shader so that the compiler
228 * does not dead-code our inputs.
230 struct anv_shader_module vs
= {
231 .nir
= build_nir_vertex_shader(false),
234 struct anv_shader_module fs_1d
= {
235 .nir
= build_nir_copy_fragment_shader(GLSL_SAMPLER_DIM_1D
),
238 struct anv_shader_module fs_2d
= {
239 .nir
= build_nir_copy_fragment_shader(GLSL_SAMPLER_DIM_2D
),
242 struct anv_shader_module fs_3d
= {
243 .nir
= build_nir_copy_fragment_shader(GLSL_SAMPLER_DIM_3D
),
246 VkPipelineVertexInputStateCreateInfo vi_create_info
= {
247 .sType
= VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO
,
248 .vertexBindingDescriptionCount
= 2,
249 .pVertexBindingDescriptions
= (VkVertexInputBindingDescription
[]) {
253 .inputRate
= VK_VERTEX_INPUT_RATE_VERTEX
257 .stride
= 5 * sizeof(float),
258 .inputRate
= VK_VERTEX_INPUT_RATE_VERTEX
261 .vertexAttributeDescriptionCount
= 3,
262 .pVertexAttributeDescriptions
= (VkVertexInputAttributeDescription
[]) {
267 .format
= VK_FORMAT_R32G32B32A32_UINT
,
274 .format
= VK_FORMAT_R32G32_SFLOAT
,
278 /* Texture Coordinate */
281 .format
= VK_FORMAT_R32G32B32_SFLOAT
,
287 VkDescriptorSetLayoutCreateInfo ds_layout_info
= {
288 .sType
= VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO
,
290 .pBindings
= (VkDescriptorSetLayoutBinding
[]) {
293 .descriptorType
= VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
,
294 .descriptorCount
= 1,
295 .stageFlags
= VK_SHADER_STAGE_FRAGMENT_BIT
,
296 .pImmutableSamplers
= NULL
300 result
= anv_CreateDescriptorSetLayout(anv_device_to_handle(device
),
301 &ds_layout_info
, NULL
,
302 &device
->meta_state
.blit
.ds_layout
);
303 if (result
!= VK_SUCCESS
)
304 goto fail_render_pass
;
306 result
= anv_CreatePipelineLayout(anv_device_to_handle(device
),
307 &(VkPipelineLayoutCreateInfo
) {
308 .sType
= VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO
,
310 .pSetLayouts
= &device
->meta_state
.blit
.ds_layout
,
312 NULL
, &device
->meta_state
.blit
.pipeline_layout
);
313 if (result
!= VK_SUCCESS
)
314 goto fail_descriptor_set_layout
;
316 VkPipelineShaderStageCreateInfo pipeline_shader_stages
[] = {
318 .sType
= VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
,
319 .stage
= VK_SHADER_STAGE_VERTEX_BIT
,
320 .module
= anv_shader_module_to_handle(&vs
),
322 .pSpecializationInfo
= NULL
324 .sType
= VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
,
325 .stage
= VK_SHADER_STAGE_FRAGMENT_BIT
,
326 .module
= VK_NULL_HANDLE
, /* TEMPLATE VALUE! FILL ME IN! */
328 .pSpecializationInfo
= NULL
332 const VkGraphicsPipelineCreateInfo vk_pipeline_info
= {
333 .sType
= VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO
,
334 .stageCount
= ARRAY_SIZE(pipeline_shader_stages
),
335 .pStages
= pipeline_shader_stages
,
336 .pVertexInputState
= &vi_create_info
,
337 .pInputAssemblyState
= &(VkPipelineInputAssemblyStateCreateInfo
) {
338 .sType
= VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO
,
339 .topology
= VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP
,
340 .primitiveRestartEnable
= false,
342 .pViewportState
= &(VkPipelineViewportStateCreateInfo
) {
343 .sType
= VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO
,
347 .pRasterizationState
= &(VkPipelineRasterizationStateCreateInfo
) {
348 .sType
= VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO
,
349 .rasterizerDiscardEnable
= false,
350 .polygonMode
= VK_POLYGON_MODE_FILL
,
351 .cullMode
= VK_CULL_MODE_NONE
,
352 .frontFace
= VK_FRONT_FACE_COUNTER_CLOCKWISE
354 .pMultisampleState
= &(VkPipelineMultisampleStateCreateInfo
) {
355 .sType
= VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO
,
356 .rasterizationSamples
= 1,
357 .sampleShadingEnable
= false,
358 .pSampleMask
= (VkSampleMask
[]) { UINT32_MAX
},
360 .pColorBlendState
= &(VkPipelineColorBlendStateCreateInfo
) {
361 .sType
= VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO
,
362 .attachmentCount
= 1,
363 .pAttachments
= (VkPipelineColorBlendAttachmentState
[]) {
365 VK_COLOR_COMPONENT_A_BIT
|
366 VK_COLOR_COMPONENT_R_BIT
|
367 VK_COLOR_COMPONENT_G_BIT
|
368 VK_COLOR_COMPONENT_B_BIT
},
371 .pDynamicState
= &(VkPipelineDynamicStateCreateInfo
) {
372 .sType
= VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO
,
373 .dynamicStateCount
= 9,
374 .pDynamicStates
= (VkDynamicState
[]) {
375 VK_DYNAMIC_STATE_VIEWPORT
,
376 VK_DYNAMIC_STATE_SCISSOR
,
377 VK_DYNAMIC_STATE_LINE_WIDTH
,
378 VK_DYNAMIC_STATE_DEPTH_BIAS
,
379 VK_DYNAMIC_STATE_BLEND_CONSTANTS
,
380 VK_DYNAMIC_STATE_DEPTH_BOUNDS
,
381 VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK
,
382 VK_DYNAMIC_STATE_STENCIL_WRITE_MASK
,
383 VK_DYNAMIC_STATE_STENCIL_REFERENCE
,
387 .layout
= device
->meta_state
.blit
.pipeline_layout
,
388 .renderPass
= device
->meta_state
.blit
.render_pass
,
392 const struct anv_graphics_pipeline_create_info anv_pipeline_info
= {
393 .use_repclear
= false,
394 .disable_viewport
= true,
395 .disable_scissor
= true,
400 pipeline_shader_stages
[1].module
= anv_shader_module_to_handle(&fs_1d
);
401 result
= anv_graphics_pipeline_create(anv_device_to_handle(device
),
403 &vk_pipeline_info
, &anv_pipeline_info
,
404 NULL
, &device
->meta_state
.blit
.pipeline_1d_src
);
405 if (result
!= VK_SUCCESS
)
406 goto fail_pipeline_layout
;
408 pipeline_shader_stages
[1].module
= anv_shader_module_to_handle(&fs_2d
);
409 result
= anv_graphics_pipeline_create(anv_device_to_handle(device
),
411 &vk_pipeline_info
, &anv_pipeline_info
,
412 NULL
, &device
->meta_state
.blit
.pipeline_2d_src
);
413 if (result
!= VK_SUCCESS
)
414 goto fail_pipeline_1d
;
416 pipeline_shader_stages
[1].module
= anv_shader_module_to_handle(&fs_3d
);
417 result
= anv_graphics_pipeline_create(anv_device_to_handle(device
),
419 &vk_pipeline_info
, &anv_pipeline_info
,
420 NULL
, &device
->meta_state
.blit
.pipeline_3d_src
);
421 if (result
!= VK_SUCCESS
)
422 goto fail_pipeline_2d
;
425 ralloc_free(fs_1d
.nir
);
426 ralloc_free(fs_2d
.nir
);
427 ralloc_free(fs_3d
.nir
);
432 anv_DestroyPipeline(anv_device_to_handle(device
),
433 device
->meta_state
.blit
.pipeline_2d_src
, NULL
);
436 anv_DestroyPipeline(anv_device_to_handle(device
),
437 device
->meta_state
.blit
.pipeline_1d_src
, NULL
);
439 fail_pipeline_layout
:
440 anv_DestroyPipelineLayout(anv_device_to_handle(device
),
441 device
->meta_state
.blit
.pipeline_layout
, NULL
);
442 fail_descriptor_set_layout
:
443 anv_DestroyDescriptorSetLayout(anv_device_to_handle(device
),
444 device
->meta_state
.blit
.ds_layout
, NULL
);
446 anv_DestroyRenderPass(anv_device_to_handle(device
),
447 device
->meta_state
.blit
.render_pass
, NULL
);
450 ralloc_free(fs_1d
.nir
);
451 ralloc_free(fs_2d
.nir
);
452 ralloc_free(fs_3d
.nir
);
458 meta_prepare_blit(struct anv_cmd_buffer
*cmd_buffer
,
459 struct anv_meta_saved_state
*saved_state
)
461 anv_meta_save(saved_state
, cmd_buffer
,
462 (1 << VK_DYNAMIC_STATE_VIEWPORT
));
466 VkOffset3D src_offset
;
467 VkExtent3D src_extent
;
468 VkOffset3D dest_offset
;
469 VkExtent3D dest_extent
;
473 meta_emit_blit(struct anv_cmd_buffer
*cmd_buffer
,
474 struct anv_image
*src_image
,
475 struct anv_image_view
*src_iview
,
476 VkOffset3D src_offset
,
477 VkExtent3D src_extent
,
478 struct anv_image
*dest_image
,
479 struct anv_image_view
*dest_iview
,
480 VkOffset3D dest_offset
,
481 VkExtent3D dest_extent
,
482 VkFilter blit_filter
)
484 struct anv_device
*device
= cmd_buffer
->device
;
485 VkDescriptorPool dummy_desc_pool
= (VkDescriptorPool
)1;
487 struct blit_vb_data
{
492 unsigned vb_size
= sizeof(struct anv_vue_header
) + 3 * sizeof(*vb_data
);
494 struct anv_state vb_state
=
495 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, vb_size
, 16);
496 memset(vb_state
.map
, 0, sizeof(struct anv_vue_header
));
497 vb_data
= vb_state
.map
+ sizeof(struct anv_vue_header
);
499 vb_data
[0] = (struct blit_vb_data
) {
501 dest_offset
.x
+ dest_extent
.width
,
502 dest_offset
.y
+ dest_extent
.height
,
505 (float)(src_offset
.x
+ src_extent
.width
) / (float)src_iview
->extent
.width
,
506 (float)(src_offset
.y
+ src_extent
.height
) / (float)src_iview
->extent
.height
,
507 (float)src_offset
.z
/ (float)src_iview
->extent
.depth
,
511 vb_data
[1] = (struct blit_vb_data
) {
514 dest_offset
.y
+ dest_extent
.height
,
517 (float)src_offset
.x
/ (float)src_iview
->extent
.width
,
518 (float)(src_offset
.y
+ src_extent
.height
) / (float)src_iview
->extent
.height
,
519 (float)src_offset
.z
/ (float)src_iview
->extent
.depth
,
523 vb_data
[2] = (struct blit_vb_data
) {
529 (float)src_offset
.x
/ (float)src_iview
->extent
.width
,
530 (float)src_offset
.y
/ (float)src_iview
->extent
.height
,
531 (float)src_offset
.z
/ (float)src_iview
->extent
.depth
,
535 anv_state_clflush(vb_state
);
537 struct anv_buffer vertex_buffer
= {
540 .bo
= &device
->dynamic_state_block_pool
.bo
,
541 .offset
= vb_state
.offset
,
544 anv_CmdBindVertexBuffers(anv_cmd_buffer_to_handle(cmd_buffer
), 0, 2,
546 anv_buffer_to_handle(&vertex_buffer
),
547 anv_buffer_to_handle(&vertex_buffer
)
551 sizeof(struct anv_vue_header
),
555 ANV_CALL(CreateSampler
)(anv_device_to_handle(device
),
556 &(VkSamplerCreateInfo
) {
557 .sType
= VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO
,
558 .magFilter
= blit_filter
,
559 .minFilter
= blit_filter
,
560 }, &cmd_buffer
->pool
->alloc
, &sampler
);
563 anv_AllocateDescriptorSets(anv_device_to_handle(device
),
564 &(VkDescriptorSetAllocateInfo
) {
565 .sType
= VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO
,
566 .descriptorPool
= dummy_desc_pool
,
567 .descriptorSetCount
= 1,
568 .pSetLayouts
= &device
->meta_state
.blit
.ds_layout
570 anv_UpdateDescriptorSets(anv_device_to_handle(device
),
572 (VkWriteDescriptorSet
[]) {
574 .sType
= VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET
,
577 .dstArrayElement
= 0,
578 .descriptorCount
= 1,
579 .descriptorType
= VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
,
580 .pImageInfo
= (VkDescriptorImageInfo
[]) {
583 .imageView
= anv_image_view_to_handle(src_iview
),
584 .imageLayout
= VK_IMAGE_LAYOUT_GENERAL
,
591 anv_CreateFramebuffer(anv_device_to_handle(device
),
592 &(VkFramebufferCreateInfo
) {
593 .sType
= VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
,
594 .attachmentCount
= 1,
595 .pAttachments
= (VkImageView
[]) {
596 anv_image_view_to_handle(dest_iview
),
598 .width
= dest_iview
->extent
.width
,
599 .height
= dest_iview
->extent
.height
,
601 }, &cmd_buffer
->pool
->alloc
, &fb
);
603 ANV_CALL(CmdBeginRenderPass
)(anv_cmd_buffer_to_handle(cmd_buffer
),
604 &(VkRenderPassBeginInfo
) {
605 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO
,
606 .renderPass
= device
->meta_state
.blit
.render_pass
,
609 .offset
= { dest_offset
.x
, dest_offset
.y
},
610 .extent
= { dest_extent
.width
, dest_extent
.height
},
612 .clearValueCount
= 0,
613 .pClearValues
= NULL
,
614 }, VK_SUBPASS_CONTENTS_INLINE
);
618 switch (src_image
->type
) {
619 case VK_IMAGE_TYPE_1D
:
620 pipeline
= device
->meta_state
.blit
.pipeline_1d_src
;
622 case VK_IMAGE_TYPE_2D
:
623 pipeline
= device
->meta_state
.blit
.pipeline_2d_src
;
625 case VK_IMAGE_TYPE_3D
:
626 pipeline
= device
->meta_state
.blit
.pipeline_3d_src
;
629 unreachable(!"bad VkImageType");
632 if (cmd_buffer
->state
.pipeline
!= anv_pipeline_from_handle(pipeline
)) {
633 anv_CmdBindPipeline(anv_cmd_buffer_to_handle(cmd_buffer
),
634 VK_PIPELINE_BIND_POINT_GRAPHICS
, pipeline
);
637 anv_CmdSetViewport(anv_cmd_buffer_to_handle(cmd_buffer
), 0, 1,
641 .width
= dest_iview
->extent
.width
,
642 .height
= dest_iview
->extent
.height
,
647 anv_CmdBindDescriptorSets(anv_cmd_buffer_to_handle(cmd_buffer
),
648 VK_PIPELINE_BIND_POINT_GRAPHICS
,
649 device
->meta_state
.blit
.pipeline_layout
, 0, 1,
652 ANV_CALL(CmdDraw
)(anv_cmd_buffer_to_handle(cmd_buffer
), 3, 1, 0, 0);
654 ANV_CALL(CmdEndRenderPass
)(anv_cmd_buffer_to_handle(cmd_buffer
));
656 /* At the point where we emit the draw call, all data from the
657 * descriptor sets, etc. has been used. We are free to delete it.
659 anv_descriptor_set_destroy(device
, anv_descriptor_set_from_handle(set
));
660 anv_DestroySampler(anv_device_to_handle(device
), sampler
,
661 &cmd_buffer
->pool
->alloc
);
662 anv_DestroyFramebuffer(anv_device_to_handle(device
), fb
,
663 &cmd_buffer
->pool
->alloc
);
667 meta_finish_blit(struct anv_cmd_buffer
*cmd_buffer
,
668 const struct anv_meta_saved_state
*saved_state
)
670 anv_meta_restore(saved_state
, cmd_buffer
);
674 vk_format_for_size(int bs
)
676 /* Note: We intentionally use the 4-channel formats whenever we can.
677 * This is so that, when we do a RGB <-> RGBX copy, the two formats will
678 * line up even though one of them is 3/4 the size of the other.
681 case 1: return VK_FORMAT_R8_UINT
;
682 case 2: return VK_FORMAT_R8G8_UINT
;
683 case 3: return VK_FORMAT_R8G8B8_UINT
;
684 case 4: return VK_FORMAT_R8G8B8A8_UINT
;
685 case 6: return VK_FORMAT_R16G16B16_UINT
;
686 case 8: return VK_FORMAT_R16G16B16A16_UINT
;
687 case 12: return VK_FORMAT_R32G32B32_UINT
;
688 case 16: return VK_FORMAT_R32G32B32A32_UINT
;
690 unreachable("Invalid format block size");
695 do_buffer_copy(struct anv_cmd_buffer
*cmd_buffer
,
696 struct anv_bo
*src
, uint64_t src_offset
,
697 struct anv_bo
*dest
, uint64_t dest_offset
,
698 int width
, int height
, VkFormat copy_format
)
700 VkDevice vk_device
= anv_device_to_handle(cmd_buffer
->device
);
702 VkImageCreateInfo image_info
= {
703 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
704 .imageType
= VK_IMAGE_TYPE_2D
,
705 .format
= copy_format
,
714 .tiling
= VK_IMAGE_TILING_LINEAR
,
720 image_info
.usage
= VK_IMAGE_USAGE_SAMPLED_BIT
;
721 anv_CreateImage(vk_device
, &image_info
,
722 &cmd_buffer
->pool
->alloc
, &src_image
);
725 image_info
.usage
= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
726 anv_CreateImage(vk_device
, &image_info
,
727 &cmd_buffer
->pool
->alloc
, &dest_image
);
729 /* We could use a vk call to bind memory, but that would require
730 * creating a dummy memory object etc. so there's really no point.
732 anv_image_from_handle(src_image
)->bo
= src
;
733 anv_image_from_handle(src_image
)->offset
= src_offset
;
734 anv_image_from_handle(dest_image
)->bo
= dest
;
735 anv_image_from_handle(dest_image
)->offset
= dest_offset
;
737 struct anv_image_view src_iview
;
738 anv_image_view_init(&src_iview
, cmd_buffer
->device
,
739 &(VkImageViewCreateInfo
) {
740 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
742 .viewType
= VK_IMAGE_VIEW_TYPE_2D
,
743 .format
= copy_format
,
744 .subresourceRange
= {
745 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
754 struct anv_image_view dest_iview
;
755 anv_image_view_init(&dest_iview
, cmd_buffer
->device
,
756 &(VkImageViewCreateInfo
) {
757 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
759 .viewType
= VK_IMAGE_VIEW_TYPE_2D
,
760 .format
= copy_format
,
761 .subresourceRange
= {
762 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
771 meta_emit_blit(cmd_buffer
,
772 anv_image_from_handle(src_image
),
774 (VkOffset3D
) { 0, 0, 0 },
775 (VkExtent3D
) { width
, height
, 1 },
776 anv_image_from_handle(dest_image
),
778 (VkOffset3D
) { 0, 0, 0 },
779 (VkExtent3D
) { width
, height
, 1 },
782 anv_DestroyImage(vk_device
, src_image
, &cmd_buffer
->pool
->alloc
);
783 anv_DestroyImage(vk_device
, dest_image
, &cmd_buffer
->pool
->alloc
);
786 void anv_CmdCopyBuffer(
787 VkCommandBuffer commandBuffer
,
790 uint32_t regionCount
,
791 const VkBufferCopy
* pRegions
)
793 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
794 ANV_FROM_HANDLE(anv_buffer
, src_buffer
, srcBuffer
);
795 ANV_FROM_HANDLE(anv_buffer
, dest_buffer
, destBuffer
);
797 struct anv_meta_saved_state saved_state
;
799 meta_prepare_blit(cmd_buffer
, &saved_state
);
801 for (unsigned r
= 0; r
< regionCount
; r
++) {
802 uint64_t src_offset
= src_buffer
->offset
+ pRegions
[r
].srcOffset
;
803 uint64_t dest_offset
= dest_buffer
->offset
+ pRegions
[r
].dstOffset
;
804 uint64_t copy_size
= pRegions
[r
].size
;
806 /* First, we compute the biggest format that can be used with the
807 * given offsets and size.
811 int fs
= ffs(src_offset
) - 1;
813 bs
= MIN2(bs
, 1 << fs
);
814 assert(src_offset
% bs
== 0);
816 fs
= ffs(dest_offset
) - 1;
818 bs
= MIN2(bs
, 1 << fs
);
819 assert(dest_offset
% bs
== 0);
821 fs
= ffs(pRegions
[r
].size
) - 1;
823 bs
= MIN2(bs
, 1 << fs
);
824 assert(pRegions
[r
].size
% bs
== 0);
826 VkFormat copy_format
= vk_format_for_size(bs
);
828 /* This is maximum possible width/height our HW can handle */
829 uint64_t max_surface_dim
= 1 << 14;
831 /* First, we make a bunch of max-sized copies */
832 uint64_t max_copy_size
= max_surface_dim
* max_surface_dim
* bs
;
833 while (copy_size
> max_copy_size
) {
834 do_buffer_copy(cmd_buffer
, src_buffer
->bo
, src_offset
,
835 dest_buffer
->bo
, dest_offset
,
836 max_surface_dim
, max_surface_dim
, copy_format
);
837 copy_size
-= max_copy_size
;
838 src_offset
+= max_copy_size
;
839 dest_offset
+= max_copy_size
;
842 uint64_t height
= copy_size
/ (max_surface_dim
* bs
);
843 assert(height
< max_surface_dim
);
845 uint64_t rect_copy_size
= height
* max_surface_dim
* bs
;
846 do_buffer_copy(cmd_buffer
, src_buffer
->bo
, src_offset
,
847 dest_buffer
->bo
, dest_offset
,
848 max_surface_dim
, height
, copy_format
);
849 copy_size
-= rect_copy_size
;
850 src_offset
+= rect_copy_size
;
851 dest_offset
+= rect_copy_size
;
854 if (copy_size
!= 0) {
855 do_buffer_copy(cmd_buffer
, src_buffer
->bo
, src_offset
,
856 dest_buffer
->bo
, dest_offset
,
857 copy_size
/ bs
, 1, copy_format
);
861 meta_finish_blit(cmd_buffer
, &saved_state
);
865 choose_iview_format(struct anv_image
*image
, VkImageAspectFlagBits aspect
)
867 assert(__builtin_popcount(aspect
) == 1);
869 struct isl_surf
*surf
=
870 &anv_image_get_surface_for_aspect_mask(image
, aspect
)->isl
;
872 /* vkCmdCopyImage behaves like memcpy. Therefore we choose identical UINT
873 * formats for the source and destination image views.
875 * From the Vulkan spec (2015-12-30):
877 * vkCmdCopyImage performs image copies in a similar manner to a host
878 * memcpy. It does not perform general-purpose conversions such as
879 * scaling, resizing, blending, color-space conversion, or format
880 * conversions. Rather, it simply copies raw image data. vkCmdCopyImage
881 * can copy between images with different formats, provided the formats
882 * are compatible as defined below.
884 * [The spec later defines compatibility as having the same number of
887 return vk_format_for_size(isl_format_layouts
[surf
->format
].bs
);
891 choose_buffer_format(struct anv_image
*image
, VkImageAspectFlagBits aspect
)
893 assert(__builtin_popcount(aspect
) == 1);
895 /* vkCmdCopy* commands behave like memcpy. Therefore we choose
896 * compatable UINT formats for the source and destination image views.
898 * For the buffer, we go back to the original image format and get a
899 * the format as if it were linear. This way, for RGB formats, we get
900 * an RGB format here even if the tiled image is RGBA. XXX: This doesn't
901 * work if the buffer is the destination.
903 enum isl_format linear_format
= anv_get_isl_format(image
->vk_format
, aspect
,
904 VK_IMAGE_TILING_LINEAR
);
906 return vk_format_for_size(isl_format_layouts
[linear_format
].bs
);
909 void anv_CmdCopyImage(
910 VkCommandBuffer commandBuffer
,
912 VkImageLayout srcImageLayout
,
914 VkImageLayout destImageLayout
,
915 uint32_t regionCount
,
916 const VkImageCopy
* pRegions
)
918 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
919 ANV_FROM_HANDLE(anv_image
, src_image
, srcImage
);
920 ANV_FROM_HANDLE(anv_image
, dest_image
, destImage
);
922 struct anv_meta_saved_state saved_state
;
924 meta_prepare_blit(cmd_buffer
, &saved_state
);
926 for (unsigned r
= 0; r
< regionCount
; r
++) {
927 assert(pRegions
[r
].srcSubresource
.aspectMask
==
928 pRegions
[r
].dstSubresource
.aspectMask
);
930 VkImageAspectFlags aspect
= pRegions
[r
].srcSubresource
.aspectMask
;
932 VkFormat src_format
= choose_iview_format(src_image
, aspect
);
933 VkFormat dst_format
= choose_iview_format(dest_image
, aspect
);
935 struct anv_image_view src_iview
;
936 anv_image_view_init(&src_iview
, cmd_buffer
->device
,
937 &(VkImageViewCreateInfo
) {
938 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
940 .viewType
= anv_meta_get_view_type(src_image
),
941 .format
= src_format
,
942 .subresourceRange
= {
943 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
944 .baseMipLevel
= pRegions
[r
].srcSubresource
.mipLevel
,
946 .baseArrayLayer
= pRegions
[r
].srcSubresource
.baseArrayLayer
,
947 .layerCount
= pRegions
[r
].dstSubresource
.layerCount
,
952 const VkOffset3D dest_offset
= {
953 .x
= pRegions
[r
].dstOffset
.x
,
954 .y
= pRegions
[r
].dstOffset
.y
,
959 if (src_image
->type
== VK_IMAGE_TYPE_3D
) {
960 assert(pRegions
[r
].srcSubresource
.layerCount
== 1 &&
961 pRegions
[r
].dstSubresource
.layerCount
== 1);
962 num_slices
= pRegions
[r
].extent
.depth
;
964 assert(pRegions
[r
].srcSubresource
.layerCount
==
965 pRegions
[r
].dstSubresource
.layerCount
);
966 assert(pRegions
[r
].extent
.depth
== 1);
967 num_slices
= pRegions
[r
].dstSubresource
.layerCount
;
970 const uint32_t dest_base_array_slice
=
971 meta_blit_get_dest_view_base_array_slice(dest_image
,
972 &pRegions
[r
].dstSubresource
,
973 &pRegions
[r
].dstOffset
);
975 for (unsigned slice
= 0; slice
< num_slices
; slice
++) {
976 VkOffset3D src_offset
= pRegions
[r
].srcOffset
;
977 src_offset
.z
+= slice
;
979 struct anv_image_view dest_iview
;
980 anv_image_view_init(&dest_iview
, cmd_buffer
->device
,
981 &(VkImageViewCreateInfo
) {
982 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
984 .viewType
= anv_meta_get_view_type(dest_image
),
985 .format
= dst_format
,
986 .subresourceRange
= {
987 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
988 .baseMipLevel
= pRegions
[r
].dstSubresource
.mipLevel
,
990 .baseArrayLayer
= dest_base_array_slice
+ slice
,
996 meta_emit_blit(cmd_buffer
,
997 src_image
, &src_iview
,
1000 dest_image
, &dest_iview
,
1007 meta_finish_blit(cmd_buffer
, &saved_state
);
1010 void anv_CmdBlitImage(
1011 VkCommandBuffer commandBuffer
,
1013 VkImageLayout srcImageLayout
,
1015 VkImageLayout destImageLayout
,
1016 uint32_t regionCount
,
1017 const VkImageBlit
* pRegions
,
1021 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1022 ANV_FROM_HANDLE(anv_image
, src_image
, srcImage
);
1023 ANV_FROM_HANDLE(anv_image
, dest_image
, destImage
);
1025 struct anv_meta_saved_state saved_state
;
1027 anv_finishme("respect VkFilter");
1029 meta_prepare_blit(cmd_buffer
, &saved_state
);
1031 for (unsigned r
= 0; r
< regionCount
; r
++) {
1032 struct anv_image_view src_iview
;
1033 anv_image_view_init(&src_iview
, cmd_buffer
->device
,
1034 &(VkImageViewCreateInfo
) {
1035 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1037 .viewType
= anv_meta_get_view_type(src_image
),
1038 .format
= src_image
->vk_format
,
1039 .subresourceRange
= {
1040 .aspectMask
= pRegions
[r
].srcSubresource
.aspectMask
,
1041 .baseMipLevel
= pRegions
[r
].srcSubresource
.mipLevel
,
1043 .baseArrayLayer
= pRegions
[r
].srcSubresource
.baseArrayLayer
,
1049 const VkOffset3D dest_offset
= {
1050 .x
= pRegions
[r
].dstOffset
.x
,
1051 .y
= pRegions
[r
].dstOffset
.y
,
1055 const uint32_t dest_array_slice
=
1056 meta_blit_get_dest_view_base_array_slice(dest_image
,
1057 &pRegions
[r
].dstSubresource
,
1058 &pRegions
[r
].dstOffset
);
1060 if (pRegions
[r
].srcSubresource
.layerCount
> 1)
1061 anv_finishme("FINISHME: copy multiple array layers");
1063 if (pRegions
[r
].dstExtent
.depth
> 1)
1064 anv_finishme("FINISHME: copy multiple depth layers");
1066 struct anv_image_view dest_iview
;
1067 anv_image_view_init(&dest_iview
, cmd_buffer
->device
,
1068 &(VkImageViewCreateInfo
) {
1069 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1071 .viewType
= anv_meta_get_view_type(dest_image
),
1072 .format
= dest_image
->vk_format
,
1073 .subresourceRange
= {
1074 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1075 .baseMipLevel
= pRegions
[r
].dstSubresource
.mipLevel
,
1077 .baseArrayLayer
= dest_array_slice
,
1083 meta_emit_blit(cmd_buffer
,
1084 src_image
, &src_iview
,
1085 pRegions
[r
].srcOffset
,
1086 pRegions
[r
].srcExtent
,
1087 dest_image
, &dest_iview
,
1089 pRegions
[r
].dstExtent
,
1093 meta_finish_blit(cmd_buffer
, &saved_state
);
1096 static struct anv_image
*
1097 make_image_for_buffer(VkDevice vk_device
, VkBuffer vk_buffer
, VkFormat format
,
1098 VkImageUsageFlags usage
,
1099 VkImageType image_type
,
1100 const VkAllocationCallbacks
*alloc
,
1101 const VkBufferImageCopy
*copy
)
1103 ANV_FROM_HANDLE(anv_buffer
, buffer
, vk_buffer
);
1105 VkExtent3D extent
= copy
->imageExtent
;
1106 if (copy
->bufferRowLength
)
1107 extent
.width
= copy
->bufferRowLength
;
1108 if (copy
->bufferImageHeight
)
1109 extent
.height
= copy
->bufferImageHeight
;
1113 VkResult result
= anv_CreateImage(vk_device
,
1114 &(VkImageCreateInfo
) {
1115 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
1116 .imageType
= VK_IMAGE_TYPE_2D
,
1122 .tiling
= VK_IMAGE_TILING_LINEAR
,
1125 }, alloc
, &vk_image
);
1126 assert(result
== VK_SUCCESS
);
1128 ANV_FROM_HANDLE(anv_image
, image
, vk_image
);
1130 /* We could use a vk call to bind memory, but that would require
1131 * creating a dummy memory object etc. so there's really no point.
1133 image
->bo
= buffer
->bo
;
1134 image
->offset
= buffer
->offset
+ copy
->bufferOffset
;
1139 void anv_CmdCopyBufferToImage(
1140 VkCommandBuffer commandBuffer
,
1143 VkImageLayout destImageLayout
,
1144 uint32_t regionCount
,
1145 const VkBufferImageCopy
* pRegions
)
1147 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1148 ANV_FROM_HANDLE(anv_image
, dest_image
, destImage
);
1149 VkDevice vk_device
= anv_device_to_handle(cmd_buffer
->device
);
1150 struct anv_meta_saved_state saved_state
;
1152 meta_prepare_blit(cmd_buffer
, &saved_state
);
1154 for (unsigned r
= 0; r
< regionCount
; r
++) {
1155 VkImageAspectFlags aspect
= pRegions
[r
].imageSubresource
.aspectMask
;
1157 VkFormat image_format
= choose_iview_format(dest_image
, aspect
);
1158 VkFormat buffer_format
= choose_buffer_format(dest_image
, aspect
);
1160 struct anv_image
*src_image
=
1161 make_image_for_buffer(vk_device
, srcBuffer
, buffer_format
,
1162 VK_IMAGE_USAGE_SAMPLED_BIT
,
1163 dest_image
->type
, &cmd_buffer
->pool
->alloc
,
1166 const uint32_t dest_base_array_slice
=
1167 meta_blit_get_dest_view_base_array_slice(dest_image
,
1168 &pRegions
[r
].imageSubresource
,
1169 &pRegions
[r
].imageOffset
);
1171 unsigned num_slices
;
1172 if (dest_image
->type
== VK_IMAGE_TYPE_3D
) {
1173 assert(pRegions
[r
].imageSubresource
.layerCount
== 1);
1174 num_slices
= pRegions
[r
].imageExtent
.depth
;
1176 assert(pRegions
[r
].imageExtent
.depth
== 1);
1177 num_slices
= pRegions
[r
].imageSubresource
.layerCount
;
1180 for (unsigned slice
= 0; slice
< num_slices
; slice
++) {
1181 struct anv_image_view src_iview
;
1182 anv_image_view_init(&src_iview
, cmd_buffer
->device
,
1183 &(VkImageViewCreateInfo
) {
1184 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1185 .image
= anv_image_to_handle(src_image
),
1186 .viewType
= VK_IMAGE_VIEW_TYPE_2D
,
1187 .format
= buffer_format
,
1188 .subresourceRange
= {
1189 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1192 .baseArrayLayer
= 0,
1198 struct anv_image_view dest_iview
;
1199 anv_image_view_init(&dest_iview
, cmd_buffer
->device
,
1200 &(VkImageViewCreateInfo
) {
1201 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1202 .image
= anv_image_to_handle(dest_image
),
1203 .viewType
= anv_meta_get_view_type(dest_image
),
1204 .format
= image_format
,
1205 .subresourceRange
= {
1206 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1207 .baseMipLevel
= pRegions
[r
].imageSubresource
.mipLevel
,
1209 .baseArrayLayer
= dest_base_array_slice
+ slice
,
1215 VkOffset3D src_offset
= { 0, 0, slice
};
1217 const VkOffset3D dest_offset
= {
1218 .x
= pRegions
[r
].imageOffset
.x
,
1219 .y
= pRegions
[r
].imageOffset
.y
,
1223 meta_emit_blit(cmd_buffer
,
1227 pRegions
[r
].imageExtent
,
1231 pRegions
[r
].imageExtent
,
1234 /* Once we've done the blit, all of the actual information about
1235 * the image is embedded in the command buffer so we can just
1236 * increment the offset directly in the image effectively
1237 * re-binding it to different backing memory.
1239 src_image
->offset
+= src_image
->extent
.width
*
1240 src_image
->extent
.height
*
1241 src_image
->format
->isl_layout
->bs
;
1244 anv_DestroyImage(vk_device
, anv_image_to_handle(src_image
),
1245 &cmd_buffer
->pool
->alloc
);
1248 meta_finish_blit(cmd_buffer
, &saved_state
);
1251 void anv_CmdCopyImageToBuffer(
1252 VkCommandBuffer commandBuffer
,
1254 VkImageLayout srcImageLayout
,
1255 VkBuffer destBuffer
,
1256 uint32_t regionCount
,
1257 const VkBufferImageCopy
* pRegions
)
1259 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1260 ANV_FROM_HANDLE(anv_image
, src_image
, srcImage
);
1261 VkDevice vk_device
= anv_device_to_handle(cmd_buffer
->device
);
1262 struct anv_meta_saved_state saved_state
;
1264 meta_prepare_blit(cmd_buffer
, &saved_state
);
1266 for (unsigned r
= 0; r
< regionCount
; r
++) {
1267 VkImageAspectFlags aspect
= pRegions
[r
].imageSubresource
.aspectMask
;
1269 VkFormat image_format
= choose_iview_format(src_image
, aspect
);
1270 VkFormat buffer_format
= choose_buffer_format(src_image
, aspect
);
1272 struct anv_image_view src_iview
;
1273 anv_image_view_init(&src_iview
, cmd_buffer
->device
,
1274 &(VkImageViewCreateInfo
) {
1275 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1277 .viewType
= anv_meta_get_view_type(src_image
),
1278 .format
= image_format
,
1279 .subresourceRange
= {
1280 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1281 .baseMipLevel
= pRegions
[r
].imageSubresource
.mipLevel
,
1283 .baseArrayLayer
= pRegions
[r
].imageSubresource
.baseArrayLayer
,
1284 .layerCount
= pRegions
[r
].imageSubresource
.layerCount
,
1289 struct anv_image
*dest_image
=
1290 make_image_for_buffer(vk_device
, destBuffer
, buffer_format
,
1291 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
,
1292 src_image
->type
, &cmd_buffer
->pool
->alloc
,
1295 unsigned num_slices
;
1296 if (src_image
->type
== VK_IMAGE_TYPE_3D
) {
1297 assert(pRegions
[r
].imageSubresource
.layerCount
== 1);
1298 num_slices
= pRegions
[r
].imageExtent
.depth
;
1300 assert(pRegions
[r
].imageExtent
.depth
== 1);
1301 num_slices
= pRegions
[r
].imageSubresource
.layerCount
;
1304 for (unsigned slice
= 0; slice
< num_slices
; slice
++) {
1305 VkOffset3D src_offset
= pRegions
[r
].imageOffset
;
1306 src_offset
.z
+= slice
;
1308 struct anv_image_view dest_iview
;
1309 anv_image_view_init(&dest_iview
, cmd_buffer
->device
,
1310 &(VkImageViewCreateInfo
) {
1311 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1312 .image
= anv_image_to_handle(dest_image
),
1313 .viewType
= VK_IMAGE_VIEW_TYPE_2D
,
1314 .format
= buffer_format
,
1315 .subresourceRange
= {
1316 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1319 .baseArrayLayer
= 0,
1325 meta_emit_blit(cmd_buffer
,
1326 anv_image_from_handle(srcImage
),
1329 pRegions
[r
].imageExtent
,
1332 (VkOffset3D
) { 0, 0, 0 },
1333 pRegions
[r
].imageExtent
,
1336 /* Once we've done the blit, all of the actual information about
1337 * the image is embedded in the command buffer so we can just
1338 * increment the offset directly in the image effectively
1339 * re-binding it to different backing memory.
1341 dest_image
->offset
+= dest_image
->extent
.width
*
1342 dest_image
->extent
.height
*
1343 src_image
->format
->isl_layout
->bs
;
1346 anv_DestroyImage(vk_device
, anv_image_to_handle(dest_image
),
1347 &cmd_buffer
->pool
->alloc
);
1350 meta_finish_blit(cmd_buffer
, &saved_state
);
1353 void anv_CmdUpdateBuffer(
1354 VkCommandBuffer commandBuffer
,
1355 VkBuffer destBuffer
,
1356 VkDeviceSize destOffset
,
1357 VkDeviceSize dataSize
,
1358 const uint32_t* pData
)
1363 void anv_CmdFillBuffer(
1364 VkCommandBuffer commandBuffer
,
1365 VkBuffer destBuffer
,
1366 VkDeviceSize destOffset
,
1367 VkDeviceSize fillSize
,
1373 void anv_CmdResolveImage(
1374 VkCommandBuffer commandBuffer
,
1376 VkImageLayout srcImageLayout
,
1378 VkImageLayout destImageLayout
,
1379 uint32_t regionCount
,
1380 const VkImageResolve
* pRegions
)
1386 anv_device_init_meta(struct anv_device
*device
)
1389 result
= anv_device_init_meta_clear_state(device
);
1390 if (result
!= VK_SUCCESS
)
1393 result
= anv_device_init_meta_blit_state(device
);
1394 if (result
!= VK_SUCCESS
)
1401 anv_device_finish_meta(struct anv_device
*device
)
1403 anv_device_finish_meta_clear_state(device
);
1406 anv_DestroyRenderPass(anv_device_to_handle(device
),
1407 device
->meta_state
.blit
.render_pass
, NULL
);
1408 anv_DestroyPipeline(anv_device_to_handle(device
),
1409 device
->meta_state
.blit
.pipeline_1d_src
, NULL
);
1410 anv_DestroyPipeline(anv_device_to_handle(device
),
1411 device
->meta_state
.blit
.pipeline_2d_src
, NULL
);
1412 anv_DestroyPipeline(anv_device_to_handle(device
),
1413 device
->meta_state
.blit
.pipeline_3d_src
, NULL
);
1414 anv_DestroyPipelineLayout(anv_device_to_handle(device
),
1415 device
->meta_state
.blit
.pipeline_layout
, NULL
);
1416 anv_DestroyDescriptorSetLayout(anv_device_to_handle(device
),
1417 device
->meta_state
.blit
.ds_layout
, NULL
);