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
);
45 b
.shader
->info
.name
= ralloc_strdup(b
.shader
, "meta_blit_vs");
47 nir_variable
*pos_in
= nir_variable_create(b
.shader
, nir_var_shader_in
,
48 vertex_type
, "a_pos");
49 pos_in
->data
.location
= VERT_ATTRIB_GENERIC0
;
50 nir_variable
*pos_out
= nir_variable_create(b
.shader
, nir_var_shader_out
,
51 vertex_type
, "gl_Position");
52 pos_out
->data
.location
= VARYING_SLOT_POS
;
53 nir_copy_var(&b
, pos_out
, pos_in
);
55 /* Add one more pass-through attribute. For clear shaders, this is used
56 * to store the color and for blit shaders it's the texture coordinate.
58 const struct glsl_type
*attr_type
= glsl_vec4_type();
59 nir_variable
*attr_in
= nir_variable_create(b
.shader
, nir_var_shader_in
,
61 attr_in
->data
.location
= VERT_ATTRIB_GENERIC1
;
62 nir_variable
*attr_out
= nir_variable_create(b
.shader
, nir_var_shader_out
,
64 attr_out
->data
.location
= VARYING_SLOT_VAR0
;
65 attr_out
->data
.interpolation
= attr_flat
? INTERP_QUALIFIER_FLAT
:
66 INTERP_QUALIFIER_SMOOTH
;
67 nir_copy_var(&b
, attr_out
, attr_in
);
73 build_nir_copy_fragment_shader(enum glsl_sampler_dim tex_dim
)
77 nir_builder_init_simple_shader(&b
, NULL
, MESA_SHADER_FRAGMENT
, NULL
);
78 b
.shader
->info
.name
= ralloc_strdup(b
.shader
, "meta_blit_fs");
80 const struct glsl_type
*color_type
= glsl_vec4_type();
82 nir_variable
*tex_pos_in
= nir_variable_create(b
.shader
, nir_var_shader_in
,
83 glsl_vec4_type(), "v_attr");
84 tex_pos_in
->data
.location
= VARYING_SLOT_VAR0
;
86 const struct glsl_type
*sampler_type
=
87 glsl_sampler_type(tex_dim
, false, false, glsl_get_base_type(color_type
));
88 nir_variable
*sampler
= nir_variable_create(b
.shader
, nir_var_uniform
,
89 sampler_type
, "s_tex");
90 sampler
->data
.descriptor_set
= 0;
91 sampler
->data
.binding
= 0;
93 nir_tex_instr
*tex
= nir_tex_instr_create(b
.shader
, 1);
94 tex
->sampler_dim
= tex_dim
;
95 tex
->op
= nir_texop_tex
;
96 tex
->src
[0].src_type
= nir_tex_src_coord
;
97 tex
->src
[0].src
= nir_src_for_ssa(nir_load_var(&b
, tex_pos_in
));
98 tex
->dest_type
= nir_type_float
; /* TODO */
100 if (tex_dim
!= GLSL_SAMPLER_DIM_3D
)
101 tex
->is_array
= true;
103 tex
->coord_components
= 3;
105 tex
->sampler
= nir_deref_var_create(tex
, sampler
);
107 nir_ssa_dest_init(&tex
->instr
, &tex
->dest
, 4, "tex");
108 nir_builder_instr_insert(&b
, &tex
->instr
);
110 nir_variable
*color_out
= nir_variable_create(b
.shader
, nir_var_shader_out
,
111 color_type
, "f_color");
112 color_out
->data
.location
= FRAG_RESULT_DATA0
;
113 nir_store_var(&b
, color_out
, &tex
->dest
.ssa
, 4);
119 anv_meta_save(struct anv_meta_saved_state
*state
,
120 const struct anv_cmd_buffer
*cmd_buffer
,
121 uint32_t dynamic_mask
)
123 state
->old_pipeline
= cmd_buffer
->state
.pipeline
;
124 state
->old_descriptor_set0
= cmd_buffer
->state
.descriptors
[0];
125 memcpy(state
->old_vertex_bindings
, cmd_buffer
->state
.vertex_bindings
,
126 sizeof(state
->old_vertex_bindings
));
128 state
->dynamic_mask
= dynamic_mask
;
129 anv_dynamic_state_copy(&state
->dynamic
, &cmd_buffer
->state
.dynamic
,
134 anv_meta_restore(const struct anv_meta_saved_state
*state
,
135 struct anv_cmd_buffer
*cmd_buffer
)
137 cmd_buffer
->state
.pipeline
= state
->old_pipeline
;
138 cmd_buffer
->state
.descriptors
[0] = state
->old_descriptor_set0
;
139 memcpy(cmd_buffer
->state
.vertex_bindings
, state
->old_vertex_bindings
,
140 sizeof(state
->old_vertex_bindings
));
142 cmd_buffer
->state
.vb_dirty
|= (1 << ANV_META_VERTEX_BINDING_COUNT
) - 1;
143 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_PIPELINE
;
144 cmd_buffer
->state
.descriptors_dirty
|= VK_SHADER_STAGE_VERTEX_BIT
;
146 anv_dynamic_state_copy(&cmd_buffer
->state
.dynamic
, &state
->dynamic
,
147 state
->dynamic_mask
);
148 cmd_buffer
->state
.dirty
|= state
->dynamic_mask
;
150 /* Since we've used the pipeline with the VS disabled, set
151 * need_query_wa. See CmdBeginQuery.
153 cmd_buffer
->state
.need_query_wa
= true;
157 anv_meta_get_view_type(const struct anv_image
*image
)
159 switch (image
->type
) {
160 case VK_IMAGE_TYPE_1D
: return VK_IMAGE_VIEW_TYPE_1D
;
161 case VK_IMAGE_TYPE_2D
: return VK_IMAGE_VIEW_TYPE_2D
;
162 case VK_IMAGE_TYPE_3D
: return VK_IMAGE_VIEW_TYPE_3D
;
164 unreachable("bad VkImageViewType");
169 meta_blit_get_dest_view_base_array_slice(const struct anv_image
*dest_image
,
170 const VkImageSubresourceLayers
*dest_subresource
,
171 const VkOffset3D
*dest_offset
)
173 switch (dest_image
->type
) {
174 case VK_IMAGE_TYPE_1D
:
175 case VK_IMAGE_TYPE_2D
:
176 return dest_subresource
->baseArrayLayer
;
177 case VK_IMAGE_TYPE_3D
:
178 /* HACK: Vulkan does not allow attaching a 3D image to a framebuffer,
179 * but meta does it anyway. When doing so, we translate the
180 * destination's z offset into an array offset.
182 return dest_offset
->z
;
184 assert(!"bad VkImageType");
190 anv_device_init_meta_blit_state(struct anv_device
*device
)
194 result
= anv_CreateRenderPass(anv_device_to_handle(device
),
195 &(VkRenderPassCreateInfo
) {
196 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO
,
197 .attachmentCount
= 1,
198 .pAttachments
= &(VkAttachmentDescription
) {
199 .format
= VK_FORMAT_UNDEFINED
, /* Our shaders don't care */
200 .loadOp
= VK_ATTACHMENT_LOAD_OP_LOAD
,
201 .storeOp
= VK_ATTACHMENT_STORE_OP_STORE
,
202 .initialLayout
= VK_IMAGE_LAYOUT_GENERAL
,
203 .finalLayout
= VK_IMAGE_LAYOUT_GENERAL
,
206 .pSubpasses
= &(VkSubpassDescription
) {
207 .pipelineBindPoint
= VK_PIPELINE_BIND_POINT_GRAPHICS
,
208 .inputAttachmentCount
= 0,
209 .colorAttachmentCount
= 1,
210 .pColorAttachments
= &(VkAttachmentReference
) {
212 .layout
= VK_IMAGE_LAYOUT_GENERAL
,
214 .pResolveAttachments
= NULL
,
215 .pDepthStencilAttachment
= &(VkAttachmentReference
) {
216 .attachment
= VK_ATTACHMENT_UNUSED
,
217 .layout
= VK_IMAGE_LAYOUT_GENERAL
,
219 .preserveAttachmentCount
= 1,
220 .pPreserveAttachments
= (uint32_t[]) { 0 },
222 .dependencyCount
= 0,
223 }, &device
->meta_state
.alloc
, &device
->meta_state
.blit
.render_pass
);
224 if (result
!= VK_SUCCESS
)
227 /* We don't use a vertex shader for clearing, but instead build and pass
228 * the VUEs directly to the rasterization backend. However, we do need
229 * to provide GLSL source for the vertex shader so that the compiler
230 * does not dead-code our inputs.
232 struct anv_shader_module vs
= {
233 .nir
= build_nir_vertex_shader(false),
236 struct anv_shader_module fs_1d
= {
237 .nir
= build_nir_copy_fragment_shader(GLSL_SAMPLER_DIM_1D
),
240 struct anv_shader_module fs_2d
= {
241 .nir
= build_nir_copy_fragment_shader(GLSL_SAMPLER_DIM_2D
),
244 struct anv_shader_module fs_3d
= {
245 .nir
= build_nir_copy_fragment_shader(GLSL_SAMPLER_DIM_3D
),
248 VkPipelineVertexInputStateCreateInfo vi_create_info
= {
249 .sType
= VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO
,
250 .vertexBindingDescriptionCount
= 2,
251 .pVertexBindingDescriptions
= (VkVertexInputBindingDescription
[]) {
255 .inputRate
= VK_VERTEX_INPUT_RATE_VERTEX
259 .stride
= 5 * sizeof(float),
260 .inputRate
= VK_VERTEX_INPUT_RATE_VERTEX
263 .vertexAttributeDescriptionCount
= 3,
264 .pVertexAttributeDescriptions
= (VkVertexInputAttributeDescription
[]) {
269 .format
= VK_FORMAT_R32G32B32A32_UINT
,
276 .format
= VK_FORMAT_R32G32_SFLOAT
,
280 /* Texture Coordinate */
283 .format
= VK_FORMAT_R32G32B32_SFLOAT
,
289 VkDescriptorSetLayoutCreateInfo ds_layout_info
= {
290 .sType
= VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO
,
292 .pBindings
= (VkDescriptorSetLayoutBinding
[]) {
295 .descriptorType
= VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
,
296 .descriptorCount
= 1,
297 .stageFlags
= VK_SHADER_STAGE_FRAGMENT_BIT
,
298 .pImmutableSamplers
= NULL
302 result
= anv_CreateDescriptorSetLayout(anv_device_to_handle(device
),
304 &device
->meta_state
.alloc
,
305 &device
->meta_state
.blit
.ds_layout
);
306 if (result
!= VK_SUCCESS
)
307 goto fail_render_pass
;
309 result
= anv_CreatePipelineLayout(anv_device_to_handle(device
),
310 &(VkPipelineLayoutCreateInfo
) {
311 .sType
= VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO
,
313 .pSetLayouts
= &device
->meta_state
.blit
.ds_layout
,
315 &device
->meta_state
.alloc
, &device
->meta_state
.blit
.pipeline_layout
);
316 if (result
!= VK_SUCCESS
)
317 goto fail_descriptor_set_layout
;
319 VkPipelineShaderStageCreateInfo pipeline_shader_stages
[] = {
321 .sType
= VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
,
322 .stage
= VK_SHADER_STAGE_VERTEX_BIT
,
323 .module
= anv_shader_module_to_handle(&vs
),
325 .pSpecializationInfo
= NULL
327 .sType
= VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
,
328 .stage
= VK_SHADER_STAGE_FRAGMENT_BIT
,
329 .module
= VK_NULL_HANDLE
, /* TEMPLATE VALUE! FILL ME IN! */
331 .pSpecializationInfo
= NULL
335 const VkGraphicsPipelineCreateInfo vk_pipeline_info
= {
336 .sType
= VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO
,
337 .stageCount
= ARRAY_SIZE(pipeline_shader_stages
),
338 .pStages
= pipeline_shader_stages
,
339 .pVertexInputState
= &vi_create_info
,
340 .pInputAssemblyState
= &(VkPipelineInputAssemblyStateCreateInfo
) {
341 .sType
= VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO
,
342 .topology
= VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP
,
343 .primitiveRestartEnable
= false,
345 .pViewportState
= &(VkPipelineViewportStateCreateInfo
) {
346 .sType
= VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO
,
350 .pRasterizationState
= &(VkPipelineRasterizationStateCreateInfo
) {
351 .sType
= VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO
,
352 .rasterizerDiscardEnable
= false,
353 .polygonMode
= VK_POLYGON_MODE_FILL
,
354 .cullMode
= VK_CULL_MODE_NONE
,
355 .frontFace
= VK_FRONT_FACE_COUNTER_CLOCKWISE
357 .pMultisampleState
= &(VkPipelineMultisampleStateCreateInfo
) {
358 .sType
= VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO
,
359 .rasterizationSamples
= 1,
360 .sampleShadingEnable
= false,
361 .pSampleMask
= (VkSampleMask
[]) { UINT32_MAX
},
363 .pColorBlendState
= &(VkPipelineColorBlendStateCreateInfo
) {
364 .sType
= VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO
,
365 .attachmentCount
= 1,
366 .pAttachments
= (VkPipelineColorBlendAttachmentState
[]) {
368 VK_COLOR_COMPONENT_A_BIT
|
369 VK_COLOR_COMPONENT_R_BIT
|
370 VK_COLOR_COMPONENT_G_BIT
|
371 VK_COLOR_COMPONENT_B_BIT
},
374 .pDynamicState
= &(VkPipelineDynamicStateCreateInfo
) {
375 .sType
= VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO
,
376 .dynamicStateCount
= 9,
377 .pDynamicStates
= (VkDynamicState
[]) {
378 VK_DYNAMIC_STATE_VIEWPORT
,
379 VK_DYNAMIC_STATE_SCISSOR
,
380 VK_DYNAMIC_STATE_LINE_WIDTH
,
381 VK_DYNAMIC_STATE_DEPTH_BIAS
,
382 VK_DYNAMIC_STATE_BLEND_CONSTANTS
,
383 VK_DYNAMIC_STATE_DEPTH_BOUNDS
,
384 VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK
,
385 VK_DYNAMIC_STATE_STENCIL_WRITE_MASK
,
386 VK_DYNAMIC_STATE_STENCIL_REFERENCE
,
390 .layout
= device
->meta_state
.blit
.pipeline_layout
,
391 .renderPass
= device
->meta_state
.blit
.render_pass
,
395 const struct anv_graphics_pipeline_create_info anv_pipeline_info
= {
396 .color_attachment_count
= -1,
397 .use_repclear
= false,
398 .disable_viewport
= true,
399 .disable_scissor
= true,
404 pipeline_shader_stages
[1].module
= anv_shader_module_to_handle(&fs_1d
);
405 result
= anv_graphics_pipeline_create(anv_device_to_handle(device
),
407 &vk_pipeline_info
, &anv_pipeline_info
,
408 &device
->meta_state
.alloc
, &device
->meta_state
.blit
.pipeline_1d_src
);
409 if (result
!= VK_SUCCESS
)
410 goto fail_pipeline_layout
;
412 pipeline_shader_stages
[1].module
= anv_shader_module_to_handle(&fs_2d
);
413 result
= anv_graphics_pipeline_create(anv_device_to_handle(device
),
415 &vk_pipeline_info
, &anv_pipeline_info
,
416 &device
->meta_state
.alloc
, &device
->meta_state
.blit
.pipeline_2d_src
);
417 if (result
!= VK_SUCCESS
)
418 goto fail_pipeline_1d
;
420 pipeline_shader_stages
[1].module
= anv_shader_module_to_handle(&fs_3d
);
421 result
= anv_graphics_pipeline_create(anv_device_to_handle(device
),
423 &vk_pipeline_info
, &anv_pipeline_info
,
424 &device
->meta_state
.alloc
, &device
->meta_state
.blit
.pipeline_3d_src
);
425 if (result
!= VK_SUCCESS
)
426 goto fail_pipeline_2d
;
429 ralloc_free(fs_1d
.nir
);
430 ralloc_free(fs_2d
.nir
);
431 ralloc_free(fs_3d
.nir
);
436 anv_DestroyPipeline(anv_device_to_handle(device
),
437 device
->meta_state
.blit
.pipeline_2d_src
,
438 &device
->meta_state
.alloc
);
441 anv_DestroyPipeline(anv_device_to_handle(device
),
442 device
->meta_state
.blit
.pipeline_1d_src
,
443 &device
->meta_state
.alloc
);
445 fail_pipeline_layout
:
446 anv_DestroyPipelineLayout(anv_device_to_handle(device
),
447 device
->meta_state
.blit
.pipeline_layout
,
448 &device
->meta_state
.alloc
);
449 fail_descriptor_set_layout
:
450 anv_DestroyDescriptorSetLayout(anv_device_to_handle(device
),
451 device
->meta_state
.blit
.ds_layout
,
452 &device
->meta_state
.alloc
);
454 anv_DestroyRenderPass(anv_device_to_handle(device
),
455 device
->meta_state
.blit
.render_pass
,
456 &device
->meta_state
.alloc
);
459 ralloc_free(fs_1d
.nir
);
460 ralloc_free(fs_2d
.nir
);
461 ralloc_free(fs_3d
.nir
);
467 meta_prepare_blit(struct anv_cmd_buffer
*cmd_buffer
,
468 struct anv_meta_saved_state
*saved_state
)
470 anv_meta_save(saved_state
, cmd_buffer
,
471 (1 << VK_DYNAMIC_STATE_VIEWPORT
));
475 VkOffset3D src_offset
;
476 VkExtent3D src_extent
;
477 VkOffset3D dest_offset
;
478 VkExtent3D dest_extent
;
482 meta_emit_blit(struct anv_cmd_buffer
*cmd_buffer
,
483 struct anv_image
*src_image
,
484 struct anv_image_view
*src_iview
,
485 VkOffset3D src_offset
,
486 VkExtent3D src_extent
,
487 struct anv_image
*dest_image
,
488 struct anv_image_view
*dest_iview
,
489 VkOffset3D dest_offset
,
490 VkExtent3D dest_extent
,
491 VkFilter blit_filter
)
493 struct anv_device
*device
= cmd_buffer
->device
;
494 VkDescriptorPool dummy_desc_pool
= (VkDescriptorPool
)1;
496 struct blit_vb_data
{
501 unsigned vb_size
= sizeof(struct anv_vue_header
) + 3 * sizeof(*vb_data
);
503 struct anv_state vb_state
=
504 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, vb_size
, 16);
505 memset(vb_state
.map
, 0, sizeof(struct anv_vue_header
));
506 vb_data
= vb_state
.map
+ sizeof(struct anv_vue_header
);
508 vb_data
[0] = (struct blit_vb_data
) {
510 dest_offset
.x
+ dest_extent
.width
,
511 dest_offset
.y
+ dest_extent
.height
,
514 (float)(src_offset
.x
+ src_extent
.width
) / (float)src_iview
->extent
.width
,
515 (float)(src_offset
.y
+ src_extent
.height
) / (float)src_iview
->extent
.height
,
516 (float)src_offset
.z
/ (float)src_iview
->extent
.depth
,
520 vb_data
[1] = (struct blit_vb_data
) {
523 dest_offset
.y
+ dest_extent
.height
,
526 (float)src_offset
.x
/ (float)src_iview
->extent
.width
,
527 (float)(src_offset
.y
+ src_extent
.height
) / (float)src_iview
->extent
.height
,
528 (float)src_offset
.z
/ (float)src_iview
->extent
.depth
,
532 vb_data
[2] = (struct blit_vb_data
) {
538 (float)src_offset
.x
/ (float)src_iview
->extent
.width
,
539 (float)src_offset
.y
/ (float)src_iview
->extent
.height
,
540 (float)src_offset
.z
/ (float)src_iview
->extent
.depth
,
544 anv_state_clflush(vb_state
);
546 struct anv_buffer vertex_buffer
= {
549 .bo
= &device
->dynamic_state_block_pool
.bo
,
550 .offset
= vb_state
.offset
,
553 anv_CmdBindVertexBuffers(anv_cmd_buffer_to_handle(cmd_buffer
), 0, 2,
555 anv_buffer_to_handle(&vertex_buffer
),
556 anv_buffer_to_handle(&vertex_buffer
)
560 sizeof(struct anv_vue_header
),
564 ANV_CALL(CreateSampler
)(anv_device_to_handle(device
),
565 &(VkSamplerCreateInfo
) {
566 .sType
= VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO
,
567 .magFilter
= blit_filter
,
568 .minFilter
= blit_filter
,
569 }, &cmd_buffer
->pool
->alloc
, &sampler
);
572 anv_AllocateDescriptorSets(anv_device_to_handle(device
),
573 &(VkDescriptorSetAllocateInfo
) {
574 .sType
= VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO
,
575 .descriptorPool
= dummy_desc_pool
,
576 .descriptorSetCount
= 1,
577 .pSetLayouts
= &device
->meta_state
.blit
.ds_layout
579 anv_UpdateDescriptorSets(anv_device_to_handle(device
),
581 (VkWriteDescriptorSet
[]) {
583 .sType
= VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET
,
586 .dstArrayElement
= 0,
587 .descriptorCount
= 1,
588 .descriptorType
= VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
,
589 .pImageInfo
= (VkDescriptorImageInfo
[]) {
592 .imageView
= anv_image_view_to_handle(src_iview
),
593 .imageLayout
= VK_IMAGE_LAYOUT_GENERAL
,
600 anv_CreateFramebuffer(anv_device_to_handle(device
),
601 &(VkFramebufferCreateInfo
) {
602 .sType
= VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
,
603 .attachmentCount
= 1,
604 .pAttachments
= (VkImageView
[]) {
605 anv_image_view_to_handle(dest_iview
),
607 .width
= dest_iview
->extent
.width
,
608 .height
= dest_iview
->extent
.height
,
610 }, &cmd_buffer
->pool
->alloc
, &fb
);
612 ANV_CALL(CmdBeginRenderPass
)(anv_cmd_buffer_to_handle(cmd_buffer
),
613 &(VkRenderPassBeginInfo
) {
614 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO
,
615 .renderPass
= device
->meta_state
.blit
.render_pass
,
618 .offset
= { dest_offset
.x
, dest_offset
.y
},
619 .extent
= { dest_extent
.width
, dest_extent
.height
},
621 .clearValueCount
= 0,
622 .pClearValues
= NULL
,
623 }, VK_SUBPASS_CONTENTS_INLINE
);
627 switch (src_image
->type
) {
628 case VK_IMAGE_TYPE_1D
:
629 pipeline
= device
->meta_state
.blit
.pipeline_1d_src
;
631 case VK_IMAGE_TYPE_2D
:
632 pipeline
= device
->meta_state
.blit
.pipeline_2d_src
;
634 case VK_IMAGE_TYPE_3D
:
635 pipeline
= device
->meta_state
.blit
.pipeline_3d_src
;
638 unreachable(!"bad VkImageType");
641 if (cmd_buffer
->state
.pipeline
!= anv_pipeline_from_handle(pipeline
)) {
642 anv_CmdBindPipeline(anv_cmd_buffer_to_handle(cmd_buffer
),
643 VK_PIPELINE_BIND_POINT_GRAPHICS
, pipeline
);
646 anv_CmdSetViewport(anv_cmd_buffer_to_handle(cmd_buffer
), 0, 1,
650 .width
= dest_iview
->extent
.width
,
651 .height
= dest_iview
->extent
.height
,
656 anv_CmdBindDescriptorSets(anv_cmd_buffer_to_handle(cmd_buffer
),
657 VK_PIPELINE_BIND_POINT_GRAPHICS
,
658 device
->meta_state
.blit
.pipeline_layout
, 0, 1,
661 ANV_CALL(CmdDraw
)(anv_cmd_buffer_to_handle(cmd_buffer
), 3, 1, 0, 0);
663 ANV_CALL(CmdEndRenderPass
)(anv_cmd_buffer_to_handle(cmd_buffer
));
665 /* At the point where we emit the draw call, all data from the
666 * descriptor sets, etc. has been used. We are free to delete it.
668 anv_descriptor_set_destroy(device
, anv_descriptor_set_from_handle(set
));
669 anv_DestroySampler(anv_device_to_handle(device
), sampler
,
670 &cmd_buffer
->pool
->alloc
);
671 anv_DestroyFramebuffer(anv_device_to_handle(device
), fb
,
672 &cmd_buffer
->pool
->alloc
);
676 meta_finish_blit(struct anv_cmd_buffer
*cmd_buffer
,
677 const struct anv_meta_saved_state
*saved_state
)
679 anv_meta_restore(saved_state
, cmd_buffer
);
683 vk_format_for_size(int bs
)
685 /* Note: We intentionally use the 4-channel formats whenever we can.
686 * This is so that, when we do a RGB <-> RGBX copy, the two formats will
687 * line up even though one of them is 3/4 the size of the other.
690 case 1: return VK_FORMAT_R8_UINT
;
691 case 2: return VK_FORMAT_R8G8_UINT
;
692 case 3: return VK_FORMAT_R8G8B8_UINT
;
693 case 4: return VK_FORMAT_R8G8B8A8_UINT
;
694 case 6: return VK_FORMAT_R16G16B16_UINT
;
695 case 8: return VK_FORMAT_R16G16B16A16_UINT
;
696 case 12: return VK_FORMAT_R32G32B32_UINT
;
697 case 16: return VK_FORMAT_R32G32B32A32_UINT
;
699 unreachable("Invalid format block size");
704 do_buffer_copy(struct anv_cmd_buffer
*cmd_buffer
,
705 struct anv_bo
*src
, uint64_t src_offset
,
706 struct anv_bo
*dest
, uint64_t dest_offset
,
707 int width
, int height
, VkFormat copy_format
)
709 VkDevice vk_device
= anv_device_to_handle(cmd_buffer
->device
);
711 VkImageCreateInfo image_info
= {
712 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
713 .imageType
= VK_IMAGE_TYPE_2D
,
714 .format
= copy_format
,
723 .tiling
= VK_IMAGE_TILING_LINEAR
,
729 image_info
.usage
= VK_IMAGE_USAGE_SAMPLED_BIT
;
730 anv_CreateImage(vk_device
, &image_info
,
731 &cmd_buffer
->pool
->alloc
, &src_image
);
734 image_info
.usage
= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
735 anv_CreateImage(vk_device
, &image_info
,
736 &cmd_buffer
->pool
->alloc
, &dest_image
);
738 /* We could use a vk call to bind memory, but that would require
739 * creating a dummy memory object etc. so there's really no point.
741 anv_image_from_handle(src_image
)->bo
= src
;
742 anv_image_from_handle(src_image
)->offset
= src_offset
;
743 anv_image_from_handle(dest_image
)->bo
= dest
;
744 anv_image_from_handle(dest_image
)->offset
= dest_offset
;
746 struct anv_image_view src_iview
;
747 anv_image_view_init(&src_iview
, cmd_buffer
->device
,
748 &(VkImageViewCreateInfo
) {
749 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
751 .viewType
= VK_IMAGE_VIEW_TYPE_2D
,
752 .format
= copy_format
,
753 .subresourceRange
= {
754 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
763 struct anv_image_view dest_iview
;
764 anv_image_view_init(&dest_iview
, cmd_buffer
->device
,
765 &(VkImageViewCreateInfo
) {
766 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
768 .viewType
= VK_IMAGE_VIEW_TYPE_2D
,
769 .format
= copy_format
,
770 .subresourceRange
= {
771 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
780 meta_emit_blit(cmd_buffer
,
781 anv_image_from_handle(src_image
),
783 (VkOffset3D
) { 0, 0, 0 },
784 (VkExtent3D
) { width
, height
, 1 },
785 anv_image_from_handle(dest_image
),
787 (VkOffset3D
) { 0, 0, 0 },
788 (VkExtent3D
) { width
, height
, 1 },
791 anv_DestroyImage(vk_device
, src_image
, &cmd_buffer
->pool
->alloc
);
792 anv_DestroyImage(vk_device
, dest_image
, &cmd_buffer
->pool
->alloc
);
795 void anv_CmdCopyBuffer(
796 VkCommandBuffer commandBuffer
,
799 uint32_t regionCount
,
800 const VkBufferCopy
* pRegions
)
802 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
803 ANV_FROM_HANDLE(anv_buffer
, src_buffer
, srcBuffer
);
804 ANV_FROM_HANDLE(anv_buffer
, dest_buffer
, destBuffer
);
806 struct anv_meta_saved_state saved_state
;
808 meta_prepare_blit(cmd_buffer
, &saved_state
);
810 for (unsigned r
= 0; r
< regionCount
; r
++) {
811 uint64_t src_offset
= src_buffer
->offset
+ pRegions
[r
].srcOffset
;
812 uint64_t dest_offset
= dest_buffer
->offset
+ pRegions
[r
].dstOffset
;
813 uint64_t copy_size
= pRegions
[r
].size
;
815 /* First, we compute the biggest format that can be used with the
816 * given offsets and size.
820 int fs
= ffs(src_offset
) - 1;
822 bs
= MIN2(bs
, 1 << fs
);
823 assert(src_offset
% bs
== 0);
825 fs
= ffs(dest_offset
) - 1;
827 bs
= MIN2(bs
, 1 << fs
);
828 assert(dest_offset
% bs
== 0);
830 fs
= ffs(pRegions
[r
].size
) - 1;
832 bs
= MIN2(bs
, 1 << fs
);
833 assert(pRegions
[r
].size
% bs
== 0);
835 VkFormat copy_format
= vk_format_for_size(bs
);
837 /* This is maximum possible width/height our HW can handle */
838 uint64_t max_surface_dim
= 1 << 14;
840 /* First, we make a bunch of max-sized copies */
841 uint64_t max_copy_size
= max_surface_dim
* max_surface_dim
* bs
;
842 while (copy_size
> max_copy_size
) {
843 do_buffer_copy(cmd_buffer
, src_buffer
->bo
, src_offset
,
844 dest_buffer
->bo
, dest_offset
,
845 max_surface_dim
, max_surface_dim
, copy_format
);
846 copy_size
-= max_copy_size
;
847 src_offset
+= max_copy_size
;
848 dest_offset
+= max_copy_size
;
851 uint64_t height
= copy_size
/ (max_surface_dim
* bs
);
852 assert(height
< max_surface_dim
);
854 uint64_t rect_copy_size
= height
* max_surface_dim
* bs
;
855 do_buffer_copy(cmd_buffer
, src_buffer
->bo
, src_offset
,
856 dest_buffer
->bo
, dest_offset
,
857 max_surface_dim
, height
, copy_format
);
858 copy_size
-= rect_copy_size
;
859 src_offset
+= rect_copy_size
;
860 dest_offset
+= rect_copy_size
;
863 if (copy_size
!= 0) {
864 do_buffer_copy(cmd_buffer
, src_buffer
->bo
, src_offset
,
865 dest_buffer
->bo
, dest_offset
,
866 copy_size
/ bs
, 1, copy_format
);
870 meta_finish_blit(cmd_buffer
, &saved_state
);
873 void anv_CmdUpdateBuffer(
874 VkCommandBuffer commandBuffer
,
876 VkDeviceSize dstOffset
,
877 VkDeviceSize dataSize
,
878 const uint32_t* pData
)
880 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
881 ANV_FROM_HANDLE(anv_buffer
, dst_buffer
, dstBuffer
);
882 struct anv_meta_saved_state saved_state
;
884 meta_prepare_blit(cmd_buffer
, &saved_state
);
886 /* We can't quite grab a full block because the state stream needs a
887 * little data at the top to build its linked list.
889 const uint32_t max_update_size
=
890 cmd_buffer
->device
->dynamic_state_block_pool
.block_size
- 64;
892 assert(max_update_size
< (1 << 14) * 4);
895 const uint32_t copy_size
= MIN2(dataSize
, max_update_size
);
897 struct anv_state tmp_data
=
898 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, copy_size
, 64);
900 memcpy(tmp_data
.map
, pData
, copy_size
);
904 if ((copy_size
& 15) == 0 && (dstOffset
& 15) == 0) {
905 format
= VK_FORMAT_R32G32B32A32_UINT
;
907 } else if ((copy_size
& 7) == 0 && (dstOffset
& 7) == 0) {
908 format
= VK_FORMAT_R32G32_UINT
;
911 assert((copy_size
& 3) == 0 && (dstOffset
& 3) == 0);
912 format
= VK_FORMAT_R32_UINT
;
916 do_buffer_copy(cmd_buffer
,
917 &cmd_buffer
->device
->dynamic_state_block_pool
.bo
,
919 dst_buffer
->bo
, dst_buffer
->offset
+ dstOffset
,
920 copy_size
/ bs
, 1, format
);
922 dataSize
-= copy_size
;
923 pData
= (void *)pData
+ copy_size
;
928 choose_iview_format(struct anv_image
*image
, VkImageAspectFlagBits aspect
)
930 assert(__builtin_popcount(aspect
) == 1);
932 struct isl_surf
*surf
=
933 &anv_image_get_surface_for_aspect_mask(image
, aspect
)->isl
;
935 /* vkCmdCopyImage behaves like memcpy. Therefore we choose identical UINT
936 * formats for the source and destination image views.
938 * From the Vulkan spec (2015-12-30):
940 * vkCmdCopyImage performs image copies in a similar manner to a host
941 * memcpy. It does not perform general-purpose conversions such as
942 * scaling, resizing, blending, color-space conversion, or format
943 * conversions. Rather, it simply copies raw image data. vkCmdCopyImage
944 * can copy between images with different formats, provided the formats
945 * are compatible as defined below.
947 * [The spec later defines compatibility as having the same number of
950 return vk_format_for_size(isl_format_layouts
[surf
->format
].bs
);
954 choose_buffer_format(struct anv_image
*image
, VkImageAspectFlagBits aspect
)
956 assert(__builtin_popcount(aspect
) == 1);
958 /* vkCmdCopy* commands behave like memcpy. Therefore we choose
959 * compatable UINT formats for the source and destination image views.
961 * For the buffer, we go back to the original image format and get a
962 * the format as if it were linear. This way, for RGB formats, we get
963 * an RGB format here even if the tiled image is RGBA. XXX: This doesn't
964 * work if the buffer is the destination.
966 enum isl_format linear_format
= anv_get_isl_format(image
->vk_format
, aspect
,
967 VK_IMAGE_TILING_LINEAR
);
969 return vk_format_for_size(isl_format_layouts
[linear_format
].bs
);
972 void anv_CmdCopyImage(
973 VkCommandBuffer commandBuffer
,
975 VkImageLayout srcImageLayout
,
977 VkImageLayout destImageLayout
,
978 uint32_t regionCount
,
979 const VkImageCopy
* pRegions
)
981 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
982 ANV_FROM_HANDLE(anv_image
, src_image
, srcImage
);
983 ANV_FROM_HANDLE(anv_image
, dest_image
, destImage
);
985 struct anv_meta_saved_state saved_state
;
987 meta_prepare_blit(cmd_buffer
, &saved_state
);
989 for (unsigned r
= 0; r
< regionCount
; r
++) {
990 assert(pRegions
[r
].srcSubresource
.aspectMask
==
991 pRegions
[r
].dstSubresource
.aspectMask
);
993 VkImageAspectFlags aspect
= pRegions
[r
].srcSubresource
.aspectMask
;
995 VkFormat src_format
= choose_iview_format(src_image
, aspect
);
996 VkFormat dst_format
= choose_iview_format(dest_image
, aspect
);
998 struct anv_image_view src_iview
;
999 anv_image_view_init(&src_iview
, cmd_buffer
->device
,
1000 &(VkImageViewCreateInfo
) {
1001 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1003 .viewType
= anv_meta_get_view_type(src_image
),
1004 .format
= src_format
,
1005 .subresourceRange
= {
1006 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1007 .baseMipLevel
= pRegions
[r
].srcSubresource
.mipLevel
,
1009 .baseArrayLayer
= pRegions
[r
].srcSubresource
.baseArrayLayer
,
1010 .layerCount
= pRegions
[r
].dstSubresource
.layerCount
,
1015 const VkOffset3D dest_offset
= {
1016 .x
= pRegions
[r
].dstOffset
.x
,
1017 .y
= pRegions
[r
].dstOffset
.y
,
1021 unsigned num_slices
;
1022 if (src_image
->type
== VK_IMAGE_TYPE_3D
) {
1023 assert(pRegions
[r
].srcSubresource
.layerCount
== 1 &&
1024 pRegions
[r
].dstSubresource
.layerCount
== 1);
1025 num_slices
= pRegions
[r
].extent
.depth
;
1027 assert(pRegions
[r
].srcSubresource
.layerCount
==
1028 pRegions
[r
].dstSubresource
.layerCount
);
1029 assert(pRegions
[r
].extent
.depth
== 1);
1030 num_slices
= pRegions
[r
].dstSubresource
.layerCount
;
1033 const uint32_t dest_base_array_slice
=
1034 meta_blit_get_dest_view_base_array_slice(dest_image
,
1035 &pRegions
[r
].dstSubresource
,
1036 &pRegions
[r
].dstOffset
);
1038 for (unsigned slice
= 0; slice
< num_slices
; slice
++) {
1039 VkOffset3D src_offset
= pRegions
[r
].srcOffset
;
1040 src_offset
.z
+= slice
;
1042 struct anv_image_view dest_iview
;
1043 anv_image_view_init(&dest_iview
, cmd_buffer
->device
,
1044 &(VkImageViewCreateInfo
) {
1045 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1047 .viewType
= anv_meta_get_view_type(dest_image
),
1048 .format
= dst_format
,
1049 .subresourceRange
= {
1050 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1051 .baseMipLevel
= pRegions
[r
].dstSubresource
.mipLevel
,
1053 .baseArrayLayer
= dest_base_array_slice
+ slice
,
1059 meta_emit_blit(cmd_buffer
,
1060 src_image
, &src_iview
,
1063 dest_image
, &dest_iview
,
1070 meta_finish_blit(cmd_buffer
, &saved_state
);
1073 void anv_CmdBlitImage(
1074 VkCommandBuffer commandBuffer
,
1076 VkImageLayout srcImageLayout
,
1078 VkImageLayout destImageLayout
,
1079 uint32_t regionCount
,
1080 const VkImageBlit
* pRegions
,
1084 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1085 ANV_FROM_HANDLE(anv_image
, src_image
, srcImage
);
1086 ANV_FROM_HANDLE(anv_image
, dest_image
, destImage
);
1088 struct anv_meta_saved_state saved_state
;
1090 anv_finishme("respect VkFilter");
1092 meta_prepare_blit(cmd_buffer
, &saved_state
);
1094 for (unsigned r
= 0; r
< regionCount
; r
++) {
1095 struct anv_image_view src_iview
;
1096 anv_image_view_init(&src_iview
, cmd_buffer
->device
,
1097 &(VkImageViewCreateInfo
) {
1098 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1100 .viewType
= anv_meta_get_view_type(src_image
),
1101 .format
= src_image
->vk_format
,
1102 .subresourceRange
= {
1103 .aspectMask
= pRegions
[r
].srcSubresource
.aspectMask
,
1104 .baseMipLevel
= pRegions
[r
].srcSubresource
.mipLevel
,
1106 .baseArrayLayer
= pRegions
[r
].srcSubresource
.baseArrayLayer
,
1112 const VkOffset3D dest_offset
= {
1113 .x
= pRegions
[r
].dstOffsets
[0].x
,
1114 .y
= pRegions
[r
].dstOffsets
[0].y
,
1118 if (pRegions
[r
].dstOffsets
[1].x
< pRegions
[r
].dstOffsets
[0].x
||
1119 pRegions
[r
].dstOffsets
[1].y
< pRegions
[r
].dstOffsets
[0].y
||
1120 pRegions
[r
].srcOffsets
[1].x
< pRegions
[r
].srcOffsets
[0].x
||
1121 pRegions
[r
].srcOffsets
[1].y
< pRegions
[r
].srcOffsets
[0].y
)
1122 anv_finishme("FINISHME: Allow flipping in blits");
1124 const VkExtent3D dest_extent
= {
1125 .width
= pRegions
[r
].dstOffsets
[1].x
- pRegions
[r
].dstOffsets
[0].x
,
1126 .height
= pRegions
[r
].dstOffsets
[1].y
- pRegions
[r
].dstOffsets
[0].y
,
1129 const VkExtent3D src_extent
= {
1130 .width
= pRegions
[r
].srcOffsets
[1].x
- pRegions
[r
].srcOffsets
[0].x
,
1131 .height
= pRegions
[r
].srcOffsets
[1].y
- pRegions
[r
].srcOffsets
[0].y
,
1134 const uint32_t dest_array_slice
=
1135 meta_blit_get_dest_view_base_array_slice(dest_image
,
1136 &pRegions
[r
].dstSubresource
,
1137 &pRegions
[r
].dstOffsets
[0]);
1139 if (pRegions
[r
].srcSubresource
.layerCount
> 1)
1140 anv_finishme("FINISHME: copy multiple array layers");
1142 if (pRegions
[r
].srcOffsets
[0].z
+ 1 != pRegions
[r
].srcOffsets
[1].z
||
1143 pRegions
[r
].dstOffsets
[0].z
+ 1 != pRegions
[r
].dstOffsets
[1].z
)
1144 anv_finishme("FINISHME: copy multiple depth layers");
1146 struct anv_image_view dest_iview
;
1147 anv_image_view_init(&dest_iview
, cmd_buffer
->device
,
1148 &(VkImageViewCreateInfo
) {
1149 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1151 .viewType
= anv_meta_get_view_type(dest_image
),
1152 .format
= dest_image
->vk_format
,
1153 .subresourceRange
= {
1154 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1155 .baseMipLevel
= pRegions
[r
].dstSubresource
.mipLevel
,
1157 .baseArrayLayer
= dest_array_slice
,
1163 meta_emit_blit(cmd_buffer
,
1164 src_image
, &src_iview
,
1165 pRegions
[r
].srcOffsets
[0], src_extent
,
1166 dest_image
, &dest_iview
,
1167 dest_offset
, dest_extent
,
1171 meta_finish_blit(cmd_buffer
, &saved_state
);
1174 static struct anv_image
*
1175 make_image_for_buffer(VkDevice vk_device
, VkBuffer vk_buffer
, VkFormat format
,
1176 VkImageUsageFlags usage
,
1177 VkImageType image_type
,
1178 const VkAllocationCallbacks
*alloc
,
1179 const VkBufferImageCopy
*copy
)
1181 ANV_FROM_HANDLE(anv_buffer
, buffer
, vk_buffer
);
1183 VkExtent3D extent
= copy
->imageExtent
;
1184 if (copy
->bufferRowLength
)
1185 extent
.width
= copy
->bufferRowLength
;
1186 if (copy
->bufferImageHeight
)
1187 extent
.height
= copy
->bufferImageHeight
;
1191 VkResult result
= anv_CreateImage(vk_device
,
1192 &(VkImageCreateInfo
) {
1193 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
1194 .imageType
= VK_IMAGE_TYPE_2D
,
1200 .tiling
= VK_IMAGE_TILING_LINEAR
,
1203 }, alloc
, &vk_image
);
1204 assert(result
== VK_SUCCESS
);
1206 ANV_FROM_HANDLE(anv_image
, image
, vk_image
);
1208 /* We could use a vk call to bind memory, but that would require
1209 * creating a dummy memory object etc. so there's really no point.
1211 image
->bo
= buffer
->bo
;
1212 image
->offset
= buffer
->offset
+ copy
->bufferOffset
;
1217 void anv_CmdCopyBufferToImage(
1218 VkCommandBuffer commandBuffer
,
1221 VkImageLayout destImageLayout
,
1222 uint32_t regionCount
,
1223 const VkBufferImageCopy
* pRegions
)
1225 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1226 ANV_FROM_HANDLE(anv_image
, dest_image
, destImage
);
1227 VkDevice vk_device
= anv_device_to_handle(cmd_buffer
->device
);
1228 struct anv_meta_saved_state saved_state
;
1230 meta_prepare_blit(cmd_buffer
, &saved_state
);
1232 for (unsigned r
= 0; r
< regionCount
; r
++) {
1233 VkImageAspectFlags aspect
= pRegions
[r
].imageSubresource
.aspectMask
;
1235 VkFormat image_format
= choose_iview_format(dest_image
, aspect
);
1236 VkFormat buffer_format
= choose_buffer_format(dest_image
, aspect
);
1238 struct anv_image
*src_image
=
1239 make_image_for_buffer(vk_device
, srcBuffer
, buffer_format
,
1240 VK_IMAGE_USAGE_SAMPLED_BIT
,
1241 dest_image
->type
, &cmd_buffer
->pool
->alloc
,
1244 const uint32_t dest_base_array_slice
=
1245 meta_blit_get_dest_view_base_array_slice(dest_image
,
1246 &pRegions
[r
].imageSubresource
,
1247 &pRegions
[r
].imageOffset
);
1249 unsigned num_slices
;
1250 if (dest_image
->type
== VK_IMAGE_TYPE_3D
) {
1251 assert(pRegions
[r
].imageSubresource
.layerCount
== 1);
1252 num_slices
= pRegions
[r
].imageExtent
.depth
;
1254 assert(pRegions
[r
].imageExtent
.depth
== 1);
1255 num_slices
= pRegions
[r
].imageSubresource
.layerCount
;
1258 for (unsigned slice
= 0; slice
< num_slices
; slice
++) {
1259 struct anv_image_view src_iview
;
1260 anv_image_view_init(&src_iview
, cmd_buffer
->device
,
1261 &(VkImageViewCreateInfo
) {
1262 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1263 .image
= anv_image_to_handle(src_image
),
1264 .viewType
= VK_IMAGE_VIEW_TYPE_2D
,
1265 .format
= buffer_format
,
1266 .subresourceRange
= {
1267 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1270 .baseArrayLayer
= 0,
1276 struct anv_image_view dest_iview
;
1277 anv_image_view_init(&dest_iview
, cmd_buffer
->device
,
1278 &(VkImageViewCreateInfo
) {
1279 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1280 .image
= anv_image_to_handle(dest_image
),
1281 .viewType
= anv_meta_get_view_type(dest_image
),
1282 .format
= image_format
,
1283 .subresourceRange
= {
1284 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1285 .baseMipLevel
= pRegions
[r
].imageSubresource
.mipLevel
,
1287 .baseArrayLayer
= dest_base_array_slice
+ slice
,
1293 VkOffset3D src_offset
= { 0, 0, slice
};
1295 const VkOffset3D dest_offset
= {
1296 .x
= pRegions
[r
].imageOffset
.x
,
1297 .y
= pRegions
[r
].imageOffset
.y
,
1301 meta_emit_blit(cmd_buffer
,
1305 pRegions
[r
].imageExtent
,
1309 pRegions
[r
].imageExtent
,
1312 /* Once we've done the blit, all of the actual information about
1313 * the image is embedded in the command buffer so we can just
1314 * increment the offset directly in the image effectively
1315 * re-binding it to different backing memory.
1317 src_image
->offset
+= src_image
->extent
.width
*
1318 src_image
->extent
.height
*
1319 src_image
->format
->isl_layout
->bs
;
1322 anv_DestroyImage(vk_device
, anv_image_to_handle(src_image
),
1323 &cmd_buffer
->pool
->alloc
);
1326 meta_finish_blit(cmd_buffer
, &saved_state
);
1329 void anv_CmdCopyImageToBuffer(
1330 VkCommandBuffer commandBuffer
,
1332 VkImageLayout srcImageLayout
,
1333 VkBuffer destBuffer
,
1334 uint32_t regionCount
,
1335 const VkBufferImageCopy
* pRegions
)
1337 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1338 ANV_FROM_HANDLE(anv_image
, src_image
, srcImage
);
1339 VkDevice vk_device
= anv_device_to_handle(cmd_buffer
->device
);
1340 struct anv_meta_saved_state saved_state
;
1342 meta_prepare_blit(cmd_buffer
, &saved_state
);
1344 for (unsigned r
= 0; r
< regionCount
; r
++) {
1345 VkImageAspectFlags aspect
= pRegions
[r
].imageSubresource
.aspectMask
;
1347 VkFormat image_format
= choose_iview_format(src_image
, aspect
);
1348 VkFormat buffer_format
= choose_buffer_format(src_image
, aspect
);
1350 struct anv_image_view src_iview
;
1351 anv_image_view_init(&src_iview
, cmd_buffer
->device
,
1352 &(VkImageViewCreateInfo
) {
1353 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1355 .viewType
= anv_meta_get_view_type(src_image
),
1356 .format
= image_format
,
1357 .subresourceRange
= {
1358 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1359 .baseMipLevel
= pRegions
[r
].imageSubresource
.mipLevel
,
1361 .baseArrayLayer
= pRegions
[r
].imageSubresource
.baseArrayLayer
,
1362 .layerCount
= pRegions
[r
].imageSubresource
.layerCount
,
1367 struct anv_image
*dest_image
=
1368 make_image_for_buffer(vk_device
, destBuffer
, buffer_format
,
1369 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
,
1370 src_image
->type
, &cmd_buffer
->pool
->alloc
,
1373 unsigned num_slices
;
1374 if (src_image
->type
== VK_IMAGE_TYPE_3D
) {
1375 assert(pRegions
[r
].imageSubresource
.layerCount
== 1);
1376 num_slices
= pRegions
[r
].imageExtent
.depth
;
1378 assert(pRegions
[r
].imageExtent
.depth
== 1);
1379 num_slices
= pRegions
[r
].imageSubresource
.layerCount
;
1382 for (unsigned slice
= 0; slice
< num_slices
; slice
++) {
1383 VkOffset3D src_offset
= pRegions
[r
].imageOffset
;
1384 src_offset
.z
+= slice
;
1386 struct anv_image_view dest_iview
;
1387 anv_image_view_init(&dest_iview
, cmd_buffer
->device
,
1388 &(VkImageViewCreateInfo
) {
1389 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1390 .image
= anv_image_to_handle(dest_image
),
1391 .viewType
= VK_IMAGE_VIEW_TYPE_2D
,
1392 .format
= buffer_format
,
1393 .subresourceRange
= {
1394 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1397 .baseArrayLayer
= 0,
1403 meta_emit_blit(cmd_buffer
,
1404 anv_image_from_handle(srcImage
),
1407 pRegions
[r
].imageExtent
,
1410 (VkOffset3D
) { 0, 0, 0 },
1411 pRegions
[r
].imageExtent
,
1414 /* Once we've done the blit, all of the actual information about
1415 * the image is embedded in the command buffer so we can just
1416 * increment the offset directly in the image effectively
1417 * re-binding it to different backing memory.
1419 dest_image
->offset
+= dest_image
->extent
.width
*
1420 dest_image
->extent
.height
*
1421 src_image
->format
->isl_layout
->bs
;
1424 anv_DestroyImage(vk_device
, anv_image_to_handle(dest_image
),
1425 &cmd_buffer
->pool
->alloc
);
1428 meta_finish_blit(cmd_buffer
, &saved_state
);
1431 void anv_CmdResolveImage(
1432 VkCommandBuffer commandBuffer
,
1434 VkImageLayout srcImageLayout
,
1436 VkImageLayout destImageLayout
,
1437 uint32_t regionCount
,
1438 const VkImageResolve
* pRegions
)
1444 meta_alloc(void* _device
, size_t size
, size_t alignment
,
1445 VkSystemAllocationScope allocationScope
)
1447 struct anv_device
*device
= _device
;
1448 return device
->alloc
.pfnAllocation(device
->alloc
.pUserData
, size
, alignment
,
1449 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
1453 meta_realloc(void* _device
, void *original
, size_t size
, size_t alignment
,
1454 VkSystemAllocationScope allocationScope
)
1456 struct anv_device
*device
= _device
;
1457 return device
->alloc
.pfnReallocation(device
->alloc
.pUserData
, original
,
1459 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
1463 meta_free(void* _device
, void *data
)
1465 struct anv_device
*device
= _device
;
1466 return device
->alloc
.pfnFree(device
->alloc
.pUserData
, data
);
1470 anv_device_init_meta(struct anv_device
*device
)
1472 device
->meta_state
.alloc
= (VkAllocationCallbacks
) {
1473 .pUserData
= device
,
1474 .pfnAllocation
= meta_alloc
,
1475 .pfnReallocation
= meta_realloc
,
1476 .pfnFree
= meta_free
,
1480 result
= anv_device_init_meta_clear_state(device
);
1481 if (result
!= VK_SUCCESS
)
1484 result
= anv_device_init_meta_blit_state(device
);
1485 if (result
!= VK_SUCCESS
) {
1486 anv_device_finish_meta_clear_state(device
);
1494 anv_device_finish_meta(struct anv_device
*device
)
1496 anv_device_finish_meta_clear_state(device
);
1499 anv_DestroyRenderPass(anv_device_to_handle(device
),
1500 device
->meta_state
.blit
.render_pass
,
1501 &device
->meta_state
.alloc
);
1502 anv_DestroyPipeline(anv_device_to_handle(device
),
1503 device
->meta_state
.blit
.pipeline_1d_src
,
1504 &device
->meta_state
.alloc
);
1505 anv_DestroyPipeline(anv_device_to_handle(device
),
1506 device
->meta_state
.blit
.pipeline_2d_src
,
1507 &device
->meta_state
.alloc
);
1508 anv_DestroyPipeline(anv_device_to_handle(device
),
1509 device
->meta_state
.blit
.pipeline_3d_src
,
1510 &device
->meta_state
.alloc
);
1511 anv_DestroyPipelineLayout(anv_device_to_handle(device
),
1512 device
->meta_state
.blit
.pipeline_layout
,
1513 &device
->meta_state
.alloc
);
1514 anv_DestroyDescriptorSetLayout(anv_device_to_handle(device
),
1515 device
->meta_state
.blit
.ds_layout
,
1516 &device
->meta_state
.alloc
);