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 /* Swizzle the array index which comes in as Z coordinate into the right
89 unsigned swz
[] = { 0, (tex_dim
== GLSL_SAMPLER_DIM_1D
? 2 : 1), 2 };
90 nir_ssa_def
*const tex_pos
=
91 nir_swizzle(&b
, nir_load_var(&b
, tex_pos_in
), swz
,
92 (tex_dim
== GLSL_SAMPLER_DIM_1D
? 2 : 3), false);
94 const struct glsl_type
*sampler_type
=
95 glsl_sampler_type(tex_dim
, false, tex_dim
!= GLSL_SAMPLER_DIM_3D
,
96 glsl_get_base_type(color_type
));
97 nir_variable
*sampler
= nir_variable_create(b
.shader
, nir_var_uniform
,
98 sampler_type
, "s_tex");
99 sampler
->data
.descriptor_set
= 0;
100 sampler
->data
.binding
= 0;
102 nir_tex_instr
*tex
= nir_tex_instr_create(b
.shader
, 1);
103 tex
->sampler_dim
= tex_dim
;
104 tex
->op
= nir_texop_tex
;
105 tex
->src
[0].src_type
= nir_tex_src_coord
;
106 tex
->src
[0].src
= nir_src_for_ssa(tex_pos
);
107 tex
->dest_type
= nir_type_float
; /* TODO */
108 tex
->is_array
= glsl_sampler_type_is_array(sampler_type
);
109 tex
->coord_components
= tex_pos
->num_components
;
110 tex
->sampler
= nir_deref_var_create(tex
, sampler
);
112 nir_ssa_dest_init(&tex
->instr
, &tex
->dest
, 4, "tex");
113 nir_builder_instr_insert(&b
, &tex
->instr
);
115 nir_variable
*color_out
= nir_variable_create(b
.shader
, nir_var_shader_out
,
116 color_type
, "f_color");
117 color_out
->data
.location
= FRAG_RESULT_DATA0
;
118 nir_store_var(&b
, color_out
, &tex
->dest
.ssa
, 4);
124 anv_meta_save(struct anv_meta_saved_state
*state
,
125 const struct anv_cmd_buffer
*cmd_buffer
,
126 uint32_t dynamic_mask
)
128 state
->old_pipeline
= cmd_buffer
->state
.pipeline
;
129 state
->old_descriptor_set0
= cmd_buffer
->state
.descriptors
[0];
130 memcpy(state
->old_vertex_bindings
, cmd_buffer
->state
.vertex_bindings
,
131 sizeof(state
->old_vertex_bindings
));
133 state
->dynamic_mask
= dynamic_mask
;
134 anv_dynamic_state_copy(&state
->dynamic
, &cmd_buffer
->state
.dynamic
,
139 anv_meta_restore(const struct anv_meta_saved_state
*state
,
140 struct anv_cmd_buffer
*cmd_buffer
)
142 cmd_buffer
->state
.pipeline
= state
->old_pipeline
;
143 cmd_buffer
->state
.descriptors
[0] = state
->old_descriptor_set0
;
144 memcpy(cmd_buffer
->state
.vertex_bindings
, state
->old_vertex_bindings
,
145 sizeof(state
->old_vertex_bindings
));
147 cmd_buffer
->state
.vb_dirty
|= (1 << ANV_META_VERTEX_BINDING_COUNT
) - 1;
148 cmd_buffer
->state
.dirty
|= ANV_CMD_DIRTY_PIPELINE
;
149 cmd_buffer
->state
.descriptors_dirty
|= VK_SHADER_STAGE_FRAGMENT_BIT
;
151 anv_dynamic_state_copy(&cmd_buffer
->state
.dynamic
, &state
->dynamic
,
152 state
->dynamic_mask
);
153 cmd_buffer
->state
.dirty
|= state
->dynamic_mask
;
155 /* Since we've used the pipeline with the VS disabled, set
156 * need_query_wa. See CmdBeginQuery.
158 cmd_buffer
->state
.need_query_wa
= true;
162 anv_meta_get_view_type(const struct anv_image
*image
)
164 switch (image
->type
) {
165 case VK_IMAGE_TYPE_1D
: return VK_IMAGE_VIEW_TYPE_1D
;
166 case VK_IMAGE_TYPE_2D
: return VK_IMAGE_VIEW_TYPE_2D
;
167 case VK_IMAGE_TYPE_3D
: return VK_IMAGE_VIEW_TYPE_3D
;
169 unreachable("bad VkImageViewType");
174 meta_blit_get_dest_view_base_array_slice(const struct anv_image
*dest_image
,
175 const VkImageSubresourceLayers
*dest_subresource
,
176 const VkOffset3D
*dest_offset
)
178 switch (dest_image
->type
) {
179 case VK_IMAGE_TYPE_1D
:
180 case VK_IMAGE_TYPE_2D
:
181 return dest_subresource
->baseArrayLayer
;
182 case VK_IMAGE_TYPE_3D
:
183 /* HACK: Vulkan does not allow attaching a 3D image to a framebuffer,
184 * but meta does it anyway. When doing so, we translate the
185 * destination's z offset into an array offset.
187 return dest_offset
->z
;
189 assert(!"bad VkImageType");
195 anv_device_init_meta_blit_state(struct anv_device
*device
)
199 result
= anv_CreateRenderPass(anv_device_to_handle(device
),
200 &(VkRenderPassCreateInfo
) {
201 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO
,
202 .attachmentCount
= 1,
203 .pAttachments
= &(VkAttachmentDescription
) {
204 .format
= VK_FORMAT_UNDEFINED
, /* Our shaders don't care */
205 .loadOp
= VK_ATTACHMENT_LOAD_OP_LOAD
,
206 .storeOp
= VK_ATTACHMENT_STORE_OP_STORE
,
207 .initialLayout
= VK_IMAGE_LAYOUT_GENERAL
,
208 .finalLayout
= VK_IMAGE_LAYOUT_GENERAL
,
211 .pSubpasses
= &(VkSubpassDescription
) {
212 .pipelineBindPoint
= VK_PIPELINE_BIND_POINT_GRAPHICS
,
213 .inputAttachmentCount
= 0,
214 .colorAttachmentCount
= 1,
215 .pColorAttachments
= &(VkAttachmentReference
) {
217 .layout
= VK_IMAGE_LAYOUT_GENERAL
,
219 .pResolveAttachments
= NULL
,
220 .pDepthStencilAttachment
= &(VkAttachmentReference
) {
221 .attachment
= VK_ATTACHMENT_UNUSED
,
222 .layout
= VK_IMAGE_LAYOUT_GENERAL
,
224 .preserveAttachmentCount
= 1,
225 .pPreserveAttachments
= (uint32_t[]) { 0 },
227 .dependencyCount
= 0,
228 }, &device
->meta_state
.alloc
, &device
->meta_state
.blit
.render_pass
);
229 if (result
!= VK_SUCCESS
)
232 /* We don't use a vertex shader for clearing, but instead build and pass
233 * the VUEs directly to the rasterization backend. However, we do need
234 * to provide GLSL source for the vertex shader so that the compiler
235 * does not dead-code our inputs.
237 struct anv_shader_module vs
= {
238 .nir
= build_nir_vertex_shader(false),
241 struct anv_shader_module fs_1d
= {
242 .nir
= build_nir_copy_fragment_shader(GLSL_SAMPLER_DIM_1D
),
245 struct anv_shader_module fs_2d
= {
246 .nir
= build_nir_copy_fragment_shader(GLSL_SAMPLER_DIM_2D
),
249 struct anv_shader_module fs_3d
= {
250 .nir
= build_nir_copy_fragment_shader(GLSL_SAMPLER_DIM_3D
),
253 VkPipelineVertexInputStateCreateInfo vi_create_info
= {
254 .sType
= VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO
,
255 .vertexBindingDescriptionCount
= 2,
256 .pVertexBindingDescriptions
= (VkVertexInputBindingDescription
[]) {
260 .inputRate
= VK_VERTEX_INPUT_RATE_VERTEX
264 .stride
= 5 * sizeof(float),
265 .inputRate
= VK_VERTEX_INPUT_RATE_VERTEX
268 .vertexAttributeDescriptionCount
= 3,
269 .pVertexAttributeDescriptions
= (VkVertexInputAttributeDescription
[]) {
274 .format
= VK_FORMAT_R32G32B32A32_UINT
,
281 .format
= VK_FORMAT_R32G32_SFLOAT
,
285 /* Texture Coordinate */
288 .format
= VK_FORMAT_R32G32B32_SFLOAT
,
294 VkDescriptorSetLayoutCreateInfo ds_layout_info
= {
295 .sType
= VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO
,
297 .pBindings
= (VkDescriptorSetLayoutBinding
[]) {
300 .descriptorType
= VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
,
301 .descriptorCount
= 1,
302 .stageFlags
= VK_SHADER_STAGE_FRAGMENT_BIT
,
303 .pImmutableSamplers
= NULL
307 result
= anv_CreateDescriptorSetLayout(anv_device_to_handle(device
),
309 &device
->meta_state
.alloc
,
310 &device
->meta_state
.blit
.ds_layout
);
311 if (result
!= VK_SUCCESS
)
312 goto fail_render_pass
;
314 result
= anv_CreatePipelineLayout(anv_device_to_handle(device
),
315 &(VkPipelineLayoutCreateInfo
) {
316 .sType
= VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO
,
318 .pSetLayouts
= &device
->meta_state
.blit
.ds_layout
,
320 &device
->meta_state
.alloc
, &device
->meta_state
.blit
.pipeline_layout
);
321 if (result
!= VK_SUCCESS
)
322 goto fail_descriptor_set_layout
;
324 VkPipelineShaderStageCreateInfo pipeline_shader_stages
[] = {
326 .sType
= VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
,
327 .stage
= VK_SHADER_STAGE_VERTEX_BIT
,
328 .module
= anv_shader_module_to_handle(&vs
),
330 .pSpecializationInfo
= NULL
332 .sType
= VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
,
333 .stage
= VK_SHADER_STAGE_FRAGMENT_BIT
,
334 .module
= VK_NULL_HANDLE
, /* TEMPLATE VALUE! FILL ME IN! */
336 .pSpecializationInfo
= NULL
340 const VkGraphicsPipelineCreateInfo vk_pipeline_info
= {
341 .sType
= VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO
,
342 .stageCount
= ARRAY_SIZE(pipeline_shader_stages
),
343 .pStages
= pipeline_shader_stages
,
344 .pVertexInputState
= &vi_create_info
,
345 .pInputAssemblyState
= &(VkPipelineInputAssemblyStateCreateInfo
) {
346 .sType
= VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO
,
347 .topology
= VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP
,
348 .primitiveRestartEnable
= false,
350 .pViewportState
= &(VkPipelineViewportStateCreateInfo
) {
351 .sType
= VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO
,
355 .pRasterizationState
= &(VkPipelineRasterizationStateCreateInfo
) {
356 .sType
= VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO
,
357 .rasterizerDiscardEnable
= false,
358 .polygonMode
= VK_POLYGON_MODE_FILL
,
359 .cullMode
= VK_CULL_MODE_NONE
,
360 .frontFace
= VK_FRONT_FACE_COUNTER_CLOCKWISE
362 .pMultisampleState
= &(VkPipelineMultisampleStateCreateInfo
) {
363 .sType
= VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO
,
364 .rasterizationSamples
= 1,
365 .sampleShadingEnable
= false,
366 .pSampleMask
= (VkSampleMask
[]) { UINT32_MAX
},
368 .pColorBlendState
= &(VkPipelineColorBlendStateCreateInfo
) {
369 .sType
= VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO
,
370 .attachmentCount
= 1,
371 .pAttachments
= (VkPipelineColorBlendAttachmentState
[]) {
373 VK_COLOR_COMPONENT_A_BIT
|
374 VK_COLOR_COMPONENT_R_BIT
|
375 VK_COLOR_COMPONENT_G_BIT
|
376 VK_COLOR_COMPONENT_B_BIT
},
379 .pDynamicState
= &(VkPipelineDynamicStateCreateInfo
) {
380 .sType
= VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO
,
381 .dynamicStateCount
= 9,
382 .pDynamicStates
= (VkDynamicState
[]) {
383 VK_DYNAMIC_STATE_VIEWPORT
,
384 VK_DYNAMIC_STATE_SCISSOR
,
385 VK_DYNAMIC_STATE_LINE_WIDTH
,
386 VK_DYNAMIC_STATE_DEPTH_BIAS
,
387 VK_DYNAMIC_STATE_BLEND_CONSTANTS
,
388 VK_DYNAMIC_STATE_DEPTH_BOUNDS
,
389 VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK
,
390 VK_DYNAMIC_STATE_STENCIL_WRITE_MASK
,
391 VK_DYNAMIC_STATE_STENCIL_REFERENCE
,
395 .layout
= device
->meta_state
.blit
.pipeline_layout
,
396 .renderPass
= device
->meta_state
.blit
.render_pass
,
400 const struct anv_graphics_pipeline_create_info anv_pipeline_info
= {
401 .color_attachment_count
= -1,
402 .use_repclear
= false,
403 .disable_viewport
= true,
404 .disable_scissor
= true,
409 pipeline_shader_stages
[1].module
= anv_shader_module_to_handle(&fs_1d
);
410 result
= anv_graphics_pipeline_create(anv_device_to_handle(device
),
412 &vk_pipeline_info
, &anv_pipeline_info
,
413 &device
->meta_state
.alloc
, &device
->meta_state
.blit
.pipeline_1d_src
);
414 if (result
!= VK_SUCCESS
)
415 goto fail_pipeline_layout
;
417 pipeline_shader_stages
[1].module
= anv_shader_module_to_handle(&fs_2d
);
418 result
= anv_graphics_pipeline_create(anv_device_to_handle(device
),
420 &vk_pipeline_info
, &anv_pipeline_info
,
421 &device
->meta_state
.alloc
, &device
->meta_state
.blit
.pipeline_2d_src
);
422 if (result
!= VK_SUCCESS
)
423 goto fail_pipeline_1d
;
425 pipeline_shader_stages
[1].module
= anv_shader_module_to_handle(&fs_3d
);
426 result
= anv_graphics_pipeline_create(anv_device_to_handle(device
),
428 &vk_pipeline_info
, &anv_pipeline_info
,
429 &device
->meta_state
.alloc
, &device
->meta_state
.blit
.pipeline_3d_src
);
430 if (result
!= VK_SUCCESS
)
431 goto fail_pipeline_2d
;
434 ralloc_free(fs_1d
.nir
);
435 ralloc_free(fs_2d
.nir
);
436 ralloc_free(fs_3d
.nir
);
441 anv_DestroyPipeline(anv_device_to_handle(device
),
442 device
->meta_state
.blit
.pipeline_2d_src
,
443 &device
->meta_state
.alloc
);
446 anv_DestroyPipeline(anv_device_to_handle(device
),
447 device
->meta_state
.blit
.pipeline_1d_src
,
448 &device
->meta_state
.alloc
);
450 fail_pipeline_layout
:
451 anv_DestroyPipelineLayout(anv_device_to_handle(device
),
452 device
->meta_state
.blit
.pipeline_layout
,
453 &device
->meta_state
.alloc
);
454 fail_descriptor_set_layout
:
455 anv_DestroyDescriptorSetLayout(anv_device_to_handle(device
),
456 device
->meta_state
.blit
.ds_layout
,
457 &device
->meta_state
.alloc
);
459 anv_DestroyRenderPass(anv_device_to_handle(device
),
460 device
->meta_state
.blit
.render_pass
,
461 &device
->meta_state
.alloc
);
464 ralloc_free(fs_1d
.nir
);
465 ralloc_free(fs_2d
.nir
);
466 ralloc_free(fs_3d
.nir
);
472 meta_prepare_blit(struct anv_cmd_buffer
*cmd_buffer
,
473 struct anv_meta_saved_state
*saved_state
)
475 anv_meta_save(saved_state
, cmd_buffer
,
476 (1 << VK_DYNAMIC_STATE_VIEWPORT
));
480 VkOffset3D src_offset
;
481 VkExtent3D src_extent
;
482 VkOffset3D dest_offset
;
483 VkExtent3D dest_extent
;
487 meta_emit_blit(struct anv_cmd_buffer
*cmd_buffer
,
488 struct anv_image
*src_image
,
489 struct anv_image_view
*src_iview
,
490 VkOffset3D src_offset
,
491 VkExtent3D src_extent
,
492 struct anv_image
*dest_image
,
493 struct anv_image_view
*dest_iview
,
494 VkOffset3D dest_offset
,
495 VkExtent3D dest_extent
,
496 VkFilter blit_filter
)
498 struct anv_device
*device
= cmd_buffer
->device
;
499 VkDescriptorPool dummy_desc_pool
= (VkDescriptorPool
)1;
501 struct blit_vb_data
{
506 assert(src_image
->samples
== dest_image
->samples
);
508 unsigned vb_size
= sizeof(struct anv_vue_header
) + 3 * sizeof(*vb_data
);
510 struct anv_state vb_state
=
511 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, vb_size
, 16);
512 memset(vb_state
.map
, 0, sizeof(struct anv_vue_header
));
513 vb_data
= vb_state
.map
+ sizeof(struct anv_vue_header
);
515 vb_data
[0] = (struct blit_vb_data
) {
517 dest_offset
.x
+ dest_extent
.width
,
518 dest_offset
.y
+ dest_extent
.height
,
521 (float)(src_offset
.x
+ src_extent
.width
) / (float)src_iview
->extent
.width
,
522 (float)(src_offset
.y
+ src_extent
.height
) / (float)src_iview
->extent
.height
,
523 (float)src_offset
.z
/ (float)src_iview
->extent
.depth
,
527 vb_data
[1] = (struct blit_vb_data
) {
530 dest_offset
.y
+ dest_extent
.height
,
533 (float)src_offset
.x
/ (float)src_iview
->extent
.width
,
534 (float)(src_offset
.y
+ src_extent
.height
) / (float)src_iview
->extent
.height
,
535 (float)src_offset
.z
/ (float)src_iview
->extent
.depth
,
539 vb_data
[2] = (struct blit_vb_data
) {
545 (float)src_offset
.x
/ (float)src_iview
->extent
.width
,
546 (float)src_offset
.y
/ (float)src_iview
->extent
.height
,
547 (float)src_offset
.z
/ (float)src_iview
->extent
.depth
,
551 anv_state_clflush(vb_state
);
553 struct anv_buffer vertex_buffer
= {
556 .bo
= &device
->dynamic_state_block_pool
.bo
,
557 .offset
= vb_state
.offset
,
560 anv_CmdBindVertexBuffers(anv_cmd_buffer_to_handle(cmd_buffer
), 0, 2,
562 anv_buffer_to_handle(&vertex_buffer
),
563 anv_buffer_to_handle(&vertex_buffer
)
567 sizeof(struct anv_vue_header
),
571 ANV_CALL(CreateSampler
)(anv_device_to_handle(device
),
572 &(VkSamplerCreateInfo
) {
573 .sType
= VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO
,
574 .magFilter
= blit_filter
,
575 .minFilter
= blit_filter
,
576 }, &cmd_buffer
->pool
->alloc
, &sampler
);
579 anv_AllocateDescriptorSets(anv_device_to_handle(device
),
580 &(VkDescriptorSetAllocateInfo
) {
581 .sType
= VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO
,
582 .descriptorPool
= dummy_desc_pool
,
583 .descriptorSetCount
= 1,
584 .pSetLayouts
= &device
->meta_state
.blit
.ds_layout
586 anv_UpdateDescriptorSets(anv_device_to_handle(device
),
588 (VkWriteDescriptorSet
[]) {
590 .sType
= VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET
,
593 .dstArrayElement
= 0,
594 .descriptorCount
= 1,
595 .descriptorType
= VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
,
596 .pImageInfo
= (VkDescriptorImageInfo
[]) {
599 .imageView
= anv_image_view_to_handle(src_iview
),
600 .imageLayout
= VK_IMAGE_LAYOUT_GENERAL
,
607 anv_CreateFramebuffer(anv_device_to_handle(device
),
608 &(VkFramebufferCreateInfo
) {
609 .sType
= VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
,
610 .attachmentCount
= 1,
611 .pAttachments
= (VkImageView
[]) {
612 anv_image_view_to_handle(dest_iview
),
614 .width
= dest_iview
->extent
.width
,
615 .height
= dest_iview
->extent
.height
,
617 }, &cmd_buffer
->pool
->alloc
, &fb
);
619 ANV_CALL(CmdBeginRenderPass
)(anv_cmd_buffer_to_handle(cmd_buffer
),
620 &(VkRenderPassBeginInfo
) {
621 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO
,
622 .renderPass
= device
->meta_state
.blit
.render_pass
,
625 .offset
= { dest_offset
.x
, dest_offset
.y
},
626 .extent
= { dest_extent
.width
, dest_extent
.height
},
628 .clearValueCount
= 0,
629 .pClearValues
= NULL
,
630 }, VK_SUBPASS_CONTENTS_INLINE
);
634 switch (src_image
->type
) {
635 case VK_IMAGE_TYPE_1D
:
636 pipeline
= device
->meta_state
.blit
.pipeline_1d_src
;
638 case VK_IMAGE_TYPE_2D
:
639 pipeline
= device
->meta_state
.blit
.pipeline_2d_src
;
641 case VK_IMAGE_TYPE_3D
:
642 pipeline
= device
->meta_state
.blit
.pipeline_3d_src
;
645 unreachable(!"bad VkImageType");
648 if (cmd_buffer
->state
.pipeline
!= anv_pipeline_from_handle(pipeline
)) {
649 anv_CmdBindPipeline(anv_cmd_buffer_to_handle(cmd_buffer
),
650 VK_PIPELINE_BIND_POINT_GRAPHICS
, pipeline
);
653 anv_CmdSetViewport(anv_cmd_buffer_to_handle(cmd_buffer
), 0, 1,
657 .width
= dest_iview
->extent
.width
,
658 .height
= dest_iview
->extent
.height
,
663 anv_CmdBindDescriptorSets(anv_cmd_buffer_to_handle(cmd_buffer
),
664 VK_PIPELINE_BIND_POINT_GRAPHICS
,
665 device
->meta_state
.blit
.pipeline_layout
, 0, 1,
668 ANV_CALL(CmdDraw
)(anv_cmd_buffer_to_handle(cmd_buffer
), 3, 1, 0, 0);
670 ANV_CALL(CmdEndRenderPass
)(anv_cmd_buffer_to_handle(cmd_buffer
));
672 /* At the point where we emit the draw call, all data from the
673 * descriptor sets, etc. has been used. We are free to delete it.
675 anv_descriptor_set_destroy(device
, anv_descriptor_set_from_handle(set
));
676 anv_DestroySampler(anv_device_to_handle(device
), sampler
,
677 &cmd_buffer
->pool
->alloc
);
678 anv_DestroyFramebuffer(anv_device_to_handle(device
), fb
,
679 &cmd_buffer
->pool
->alloc
);
683 meta_finish_blit(struct anv_cmd_buffer
*cmd_buffer
,
684 const struct anv_meta_saved_state
*saved_state
)
686 anv_meta_restore(saved_state
, cmd_buffer
);
690 vk_format_for_size(int bs
)
692 /* Note: We intentionally use the 4-channel formats whenever we can.
693 * This is so that, when we do a RGB <-> RGBX copy, the two formats will
694 * line up even though one of them is 3/4 the size of the other.
697 case 1: return VK_FORMAT_R8_UINT
;
698 case 2: return VK_FORMAT_R8G8_UINT
;
699 case 3: return VK_FORMAT_R8G8B8_UINT
;
700 case 4: return VK_FORMAT_R8G8B8A8_UINT
;
701 case 6: return VK_FORMAT_R16G16B16_UINT
;
702 case 8: return VK_FORMAT_R16G16B16A16_UINT
;
703 case 12: return VK_FORMAT_R32G32B32_UINT
;
704 case 16: return VK_FORMAT_R32G32B32A32_UINT
;
706 unreachable("Invalid format block size");
711 do_buffer_copy(struct anv_cmd_buffer
*cmd_buffer
,
712 struct anv_bo
*src
, uint64_t src_offset
,
713 struct anv_bo
*dest
, uint64_t dest_offset
,
714 int width
, int height
, VkFormat copy_format
)
716 VkDevice vk_device
= anv_device_to_handle(cmd_buffer
->device
);
718 VkImageCreateInfo image_info
= {
719 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
720 .imageType
= VK_IMAGE_TYPE_2D
,
721 .format
= copy_format
,
730 .tiling
= VK_IMAGE_TILING_LINEAR
,
736 image_info
.usage
= VK_IMAGE_USAGE_SAMPLED_BIT
;
737 anv_CreateImage(vk_device
, &image_info
,
738 &cmd_buffer
->pool
->alloc
, &src_image
);
741 image_info
.usage
= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
742 anv_CreateImage(vk_device
, &image_info
,
743 &cmd_buffer
->pool
->alloc
, &dest_image
);
745 /* We could use a vk call to bind memory, but that would require
746 * creating a dummy memory object etc. so there's really no point.
748 anv_image_from_handle(src_image
)->bo
= src
;
749 anv_image_from_handle(src_image
)->offset
= src_offset
;
750 anv_image_from_handle(dest_image
)->bo
= dest
;
751 anv_image_from_handle(dest_image
)->offset
= dest_offset
;
753 struct anv_image_view src_iview
;
754 anv_image_view_init(&src_iview
, cmd_buffer
->device
,
755 &(VkImageViewCreateInfo
) {
756 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
758 .viewType
= VK_IMAGE_VIEW_TYPE_2D
,
759 .format
= copy_format
,
760 .subresourceRange
= {
761 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
770 struct anv_image_view dest_iview
;
771 anv_image_view_init(&dest_iview
, cmd_buffer
->device
,
772 &(VkImageViewCreateInfo
) {
773 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
775 .viewType
= VK_IMAGE_VIEW_TYPE_2D
,
776 .format
= copy_format
,
777 .subresourceRange
= {
778 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
787 meta_emit_blit(cmd_buffer
,
788 anv_image_from_handle(src_image
),
790 (VkOffset3D
) { 0, 0, 0 },
791 (VkExtent3D
) { width
, height
, 1 },
792 anv_image_from_handle(dest_image
),
794 (VkOffset3D
) { 0, 0, 0 },
795 (VkExtent3D
) { width
, height
, 1 },
798 anv_DestroyImage(vk_device
, src_image
, &cmd_buffer
->pool
->alloc
);
799 anv_DestroyImage(vk_device
, dest_image
, &cmd_buffer
->pool
->alloc
);
802 void anv_CmdCopyBuffer(
803 VkCommandBuffer commandBuffer
,
806 uint32_t regionCount
,
807 const VkBufferCopy
* pRegions
)
809 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
810 ANV_FROM_HANDLE(anv_buffer
, src_buffer
, srcBuffer
);
811 ANV_FROM_HANDLE(anv_buffer
, dest_buffer
, destBuffer
);
813 struct anv_meta_saved_state saved_state
;
815 meta_prepare_blit(cmd_buffer
, &saved_state
);
817 for (unsigned r
= 0; r
< regionCount
; r
++) {
818 uint64_t src_offset
= src_buffer
->offset
+ pRegions
[r
].srcOffset
;
819 uint64_t dest_offset
= dest_buffer
->offset
+ pRegions
[r
].dstOffset
;
820 uint64_t copy_size
= pRegions
[r
].size
;
822 /* First, we compute the biggest format that can be used with the
823 * given offsets and size.
827 int fs
= ffs(src_offset
) - 1;
829 bs
= MIN2(bs
, 1 << fs
);
830 assert(src_offset
% bs
== 0);
832 fs
= ffs(dest_offset
) - 1;
834 bs
= MIN2(bs
, 1 << fs
);
835 assert(dest_offset
% bs
== 0);
837 fs
= ffs(pRegions
[r
].size
) - 1;
839 bs
= MIN2(bs
, 1 << fs
);
840 assert(pRegions
[r
].size
% bs
== 0);
842 VkFormat copy_format
= vk_format_for_size(bs
);
844 /* This is maximum possible width/height our HW can handle */
845 uint64_t max_surface_dim
= 1 << 14;
847 /* First, we make a bunch of max-sized copies */
848 uint64_t max_copy_size
= max_surface_dim
* max_surface_dim
* bs
;
849 while (copy_size
>= max_copy_size
) {
850 do_buffer_copy(cmd_buffer
, src_buffer
->bo
, src_offset
,
851 dest_buffer
->bo
, dest_offset
,
852 max_surface_dim
, max_surface_dim
, copy_format
);
853 copy_size
-= max_copy_size
;
854 src_offset
+= max_copy_size
;
855 dest_offset
+= max_copy_size
;
858 uint64_t height
= copy_size
/ (max_surface_dim
* bs
);
859 assert(height
< max_surface_dim
);
861 uint64_t rect_copy_size
= height
* max_surface_dim
* bs
;
862 do_buffer_copy(cmd_buffer
, src_buffer
->bo
, src_offset
,
863 dest_buffer
->bo
, dest_offset
,
864 max_surface_dim
, height
, copy_format
);
865 copy_size
-= rect_copy_size
;
866 src_offset
+= rect_copy_size
;
867 dest_offset
+= rect_copy_size
;
870 if (copy_size
!= 0) {
871 do_buffer_copy(cmd_buffer
, src_buffer
->bo
, src_offset
,
872 dest_buffer
->bo
, dest_offset
,
873 copy_size
/ bs
, 1, copy_format
);
877 meta_finish_blit(cmd_buffer
, &saved_state
);
880 void anv_CmdUpdateBuffer(
881 VkCommandBuffer commandBuffer
,
883 VkDeviceSize dstOffset
,
884 VkDeviceSize dataSize
,
885 const uint32_t* pData
)
887 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
888 ANV_FROM_HANDLE(anv_buffer
, dst_buffer
, dstBuffer
);
889 struct anv_meta_saved_state saved_state
;
891 meta_prepare_blit(cmd_buffer
, &saved_state
);
893 /* We can't quite grab a full block because the state stream needs a
894 * little data at the top to build its linked list.
896 const uint32_t max_update_size
=
897 cmd_buffer
->device
->dynamic_state_block_pool
.block_size
- 64;
899 assert(max_update_size
< (1 << 14) * 4);
902 const uint32_t copy_size
= MIN2(dataSize
, max_update_size
);
904 struct anv_state tmp_data
=
905 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, copy_size
, 64);
907 memcpy(tmp_data
.map
, pData
, copy_size
);
911 if ((copy_size
& 15) == 0 && (dstOffset
& 15) == 0) {
912 format
= VK_FORMAT_R32G32B32A32_UINT
;
914 } else if ((copy_size
& 7) == 0 && (dstOffset
& 7) == 0) {
915 format
= VK_FORMAT_R32G32_UINT
;
918 assert((copy_size
& 3) == 0 && (dstOffset
& 3) == 0);
919 format
= VK_FORMAT_R32_UINT
;
923 do_buffer_copy(cmd_buffer
,
924 &cmd_buffer
->device
->dynamic_state_block_pool
.bo
,
926 dst_buffer
->bo
, dst_buffer
->offset
+ dstOffset
,
927 copy_size
/ bs
, 1, format
);
929 dataSize
-= copy_size
;
930 dstOffset
+= copy_size
;
931 pData
= (void *)pData
+ copy_size
;
936 choose_iview_format(struct anv_image
*image
, VkImageAspectFlagBits aspect
)
938 assert(__builtin_popcount(aspect
) == 1);
940 struct isl_surf
*surf
=
941 &anv_image_get_surface_for_aspect_mask(image
, aspect
)->isl
;
943 /* vkCmdCopyImage behaves like memcpy. Therefore we choose identical UINT
944 * formats for the source and destination image views.
946 * From the Vulkan spec (2015-12-30):
948 * vkCmdCopyImage performs image copies in a similar manner to a host
949 * memcpy. It does not perform general-purpose conversions such as
950 * scaling, resizing, blending, color-space conversion, or format
951 * conversions. Rather, it simply copies raw image data. vkCmdCopyImage
952 * can copy between images with different formats, provided the formats
953 * are compatible as defined below.
955 * [The spec later defines compatibility as having the same number of
958 return vk_format_for_size(isl_format_layouts
[surf
->format
].bs
);
962 choose_buffer_format(struct anv_image
*image
, VkImageAspectFlagBits aspect
)
964 assert(__builtin_popcount(aspect
) == 1);
966 /* vkCmdCopy* commands behave like memcpy. Therefore we choose
967 * compatable UINT formats for the source and destination image views.
969 * For the buffer, we go back to the original image format and get a
970 * the format as if it were linear. This way, for RGB formats, we get
971 * an RGB format here even if the tiled image is RGBA. XXX: This doesn't
972 * work if the buffer is the destination.
974 enum isl_format linear_format
= anv_get_isl_format(image
->vk_format
, aspect
,
975 VK_IMAGE_TILING_LINEAR
);
977 return vk_format_for_size(isl_format_layouts
[linear_format
].bs
);
980 void anv_CmdCopyImage(
981 VkCommandBuffer commandBuffer
,
983 VkImageLayout srcImageLayout
,
985 VkImageLayout destImageLayout
,
986 uint32_t regionCount
,
987 const VkImageCopy
* pRegions
)
989 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
990 ANV_FROM_HANDLE(anv_image
, src_image
, srcImage
);
991 ANV_FROM_HANDLE(anv_image
, dest_image
, destImage
);
992 struct anv_meta_saved_state saved_state
;
994 /* From the Vulkan 1.0 spec:
996 * vkCmdCopyImage can be used to copy image data between multisample
997 * images, but both images must have the same number of samples.
999 assert(src_image
->samples
== dest_image
->samples
);
1001 meta_prepare_blit(cmd_buffer
, &saved_state
);
1003 for (unsigned r
= 0; r
< regionCount
; r
++) {
1004 assert(pRegions
[r
].srcSubresource
.aspectMask
==
1005 pRegions
[r
].dstSubresource
.aspectMask
);
1007 VkImageAspectFlags aspect
= pRegions
[r
].srcSubresource
.aspectMask
;
1009 VkFormat src_format
= choose_iview_format(src_image
, aspect
);
1010 VkFormat dst_format
= choose_iview_format(dest_image
, aspect
);
1012 struct anv_image_view src_iview
;
1013 anv_image_view_init(&src_iview
, cmd_buffer
->device
,
1014 &(VkImageViewCreateInfo
) {
1015 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1017 .viewType
= anv_meta_get_view_type(src_image
),
1018 .format
= src_format
,
1019 .subresourceRange
= {
1020 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1021 .baseMipLevel
= pRegions
[r
].srcSubresource
.mipLevel
,
1023 .baseArrayLayer
= pRegions
[r
].srcSubresource
.baseArrayLayer
,
1024 .layerCount
= pRegions
[r
].dstSubresource
.layerCount
,
1029 const VkOffset3D dest_offset
= {
1030 .x
= pRegions
[r
].dstOffset
.x
,
1031 .y
= pRegions
[r
].dstOffset
.y
,
1035 unsigned num_slices
;
1036 if (src_image
->type
== VK_IMAGE_TYPE_3D
) {
1037 assert(pRegions
[r
].srcSubresource
.layerCount
== 1 &&
1038 pRegions
[r
].dstSubresource
.layerCount
== 1);
1039 num_slices
= pRegions
[r
].extent
.depth
;
1041 assert(pRegions
[r
].srcSubresource
.layerCount
==
1042 pRegions
[r
].dstSubresource
.layerCount
);
1043 assert(pRegions
[r
].extent
.depth
== 1);
1044 num_slices
= pRegions
[r
].dstSubresource
.layerCount
;
1047 const uint32_t dest_base_array_slice
=
1048 meta_blit_get_dest_view_base_array_slice(dest_image
,
1049 &pRegions
[r
].dstSubresource
,
1050 &pRegions
[r
].dstOffset
);
1052 for (unsigned slice
= 0; slice
< num_slices
; slice
++) {
1053 VkOffset3D src_offset
= pRegions
[r
].srcOffset
;
1054 src_offset
.z
+= slice
;
1056 struct anv_image_view dest_iview
;
1057 anv_image_view_init(&dest_iview
, cmd_buffer
->device
,
1058 &(VkImageViewCreateInfo
) {
1059 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1061 .viewType
= anv_meta_get_view_type(dest_image
),
1062 .format
= dst_format
,
1063 .subresourceRange
= {
1064 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1065 .baseMipLevel
= pRegions
[r
].dstSubresource
.mipLevel
,
1067 .baseArrayLayer
= dest_base_array_slice
+ slice
,
1073 meta_emit_blit(cmd_buffer
,
1074 src_image
, &src_iview
,
1077 dest_image
, &dest_iview
,
1084 meta_finish_blit(cmd_buffer
, &saved_state
);
1087 void anv_CmdBlitImage(
1088 VkCommandBuffer commandBuffer
,
1090 VkImageLayout srcImageLayout
,
1092 VkImageLayout destImageLayout
,
1093 uint32_t regionCount
,
1094 const VkImageBlit
* pRegions
,
1098 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1099 ANV_FROM_HANDLE(anv_image
, src_image
, srcImage
);
1100 ANV_FROM_HANDLE(anv_image
, dest_image
, destImage
);
1101 struct anv_meta_saved_state saved_state
;
1103 /* From the Vulkan 1.0 spec:
1105 * vkCmdBlitImage must not be used for multisampled source or
1106 * destination images. Use vkCmdResolveImage for this purpose.
1108 assert(src_image
->samples
== 1);
1109 assert(dest_image
->samples
== 1);
1111 anv_finishme("respect VkFilter");
1113 meta_prepare_blit(cmd_buffer
, &saved_state
);
1115 for (unsigned r
= 0; r
< regionCount
; r
++) {
1116 struct anv_image_view src_iview
;
1117 anv_image_view_init(&src_iview
, cmd_buffer
->device
,
1118 &(VkImageViewCreateInfo
) {
1119 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1121 .viewType
= anv_meta_get_view_type(src_image
),
1122 .format
= src_image
->vk_format
,
1123 .subresourceRange
= {
1124 .aspectMask
= pRegions
[r
].srcSubresource
.aspectMask
,
1125 .baseMipLevel
= pRegions
[r
].srcSubresource
.mipLevel
,
1127 .baseArrayLayer
= pRegions
[r
].srcSubresource
.baseArrayLayer
,
1133 const VkOffset3D dest_offset
= {
1134 .x
= pRegions
[r
].dstOffsets
[0].x
,
1135 .y
= pRegions
[r
].dstOffsets
[0].y
,
1139 if (pRegions
[r
].dstOffsets
[1].x
< pRegions
[r
].dstOffsets
[0].x
||
1140 pRegions
[r
].dstOffsets
[1].y
< pRegions
[r
].dstOffsets
[0].y
||
1141 pRegions
[r
].srcOffsets
[1].x
< pRegions
[r
].srcOffsets
[0].x
||
1142 pRegions
[r
].srcOffsets
[1].y
< pRegions
[r
].srcOffsets
[0].y
)
1143 anv_finishme("FINISHME: Allow flipping in blits");
1145 const VkExtent3D dest_extent
= {
1146 .width
= pRegions
[r
].dstOffsets
[1].x
- pRegions
[r
].dstOffsets
[0].x
,
1147 .height
= pRegions
[r
].dstOffsets
[1].y
- pRegions
[r
].dstOffsets
[0].y
,
1150 const VkExtent3D src_extent
= {
1151 .width
= pRegions
[r
].srcOffsets
[1].x
- pRegions
[r
].srcOffsets
[0].x
,
1152 .height
= pRegions
[r
].srcOffsets
[1].y
- pRegions
[r
].srcOffsets
[0].y
,
1155 const uint32_t dest_array_slice
=
1156 meta_blit_get_dest_view_base_array_slice(dest_image
,
1157 &pRegions
[r
].dstSubresource
,
1158 &pRegions
[r
].dstOffsets
[0]);
1160 if (pRegions
[r
].srcSubresource
.layerCount
> 1)
1161 anv_finishme("FINISHME: copy multiple array layers");
1163 if (pRegions
[r
].srcOffsets
[0].z
+ 1 != pRegions
[r
].srcOffsets
[1].z
||
1164 pRegions
[r
].dstOffsets
[0].z
+ 1 != pRegions
[r
].dstOffsets
[1].z
)
1165 anv_finishme("FINISHME: copy multiple depth layers");
1167 struct anv_image_view dest_iview
;
1168 anv_image_view_init(&dest_iview
, cmd_buffer
->device
,
1169 &(VkImageViewCreateInfo
) {
1170 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1172 .viewType
= anv_meta_get_view_type(dest_image
),
1173 .format
= dest_image
->vk_format
,
1174 .subresourceRange
= {
1175 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1176 .baseMipLevel
= pRegions
[r
].dstSubresource
.mipLevel
,
1178 .baseArrayLayer
= dest_array_slice
,
1184 meta_emit_blit(cmd_buffer
,
1185 src_image
, &src_iview
,
1186 pRegions
[r
].srcOffsets
[0], src_extent
,
1187 dest_image
, &dest_iview
,
1188 dest_offset
, dest_extent
,
1192 meta_finish_blit(cmd_buffer
, &saved_state
);
1195 static struct anv_image
*
1196 make_image_for_buffer(VkDevice vk_device
, VkBuffer vk_buffer
, VkFormat format
,
1197 VkImageUsageFlags usage
,
1198 VkImageType image_type
,
1199 const VkAllocationCallbacks
*alloc
,
1200 const VkBufferImageCopy
*copy
)
1202 ANV_FROM_HANDLE(anv_buffer
, buffer
, vk_buffer
);
1204 VkExtent3D extent
= copy
->imageExtent
;
1205 if (copy
->bufferRowLength
)
1206 extent
.width
= copy
->bufferRowLength
;
1207 if (copy
->bufferImageHeight
)
1208 extent
.height
= copy
->bufferImageHeight
;
1212 VkResult result
= anv_CreateImage(vk_device
,
1213 &(VkImageCreateInfo
) {
1214 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
1215 .imageType
= VK_IMAGE_TYPE_2D
,
1221 .tiling
= VK_IMAGE_TILING_LINEAR
,
1224 }, alloc
, &vk_image
);
1225 assert(result
== VK_SUCCESS
);
1227 ANV_FROM_HANDLE(anv_image
, image
, vk_image
);
1229 /* We could use a vk call to bind memory, but that would require
1230 * creating a dummy memory object etc. so there's really no point.
1232 image
->bo
= buffer
->bo
;
1233 image
->offset
= buffer
->offset
+ copy
->bufferOffset
;
1238 void anv_CmdCopyBufferToImage(
1239 VkCommandBuffer commandBuffer
,
1242 VkImageLayout destImageLayout
,
1243 uint32_t regionCount
,
1244 const VkBufferImageCopy
* pRegions
)
1246 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1247 ANV_FROM_HANDLE(anv_image
, dest_image
, destImage
);
1248 VkDevice vk_device
= anv_device_to_handle(cmd_buffer
->device
);
1249 struct anv_meta_saved_state saved_state
;
1251 /* The Vulkan 1.0 spec says "dstImage must have a sample count equal to
1252 * VK_SAMPLE_COUNT_1_BIT."
1254 assert(dest_image
->samples
== 1);
1256 meta_prepare_blit(cmd_buffer
, &saved_state
);
1258 for (unsigned r
= 0; r
< regionCount
; r
++) {
1259 VkImageAspectFlags aspect
= pRegions
[r
].imageSubresource
.aspectMask
;
1261 VkFormat image_format
= choose_iview_format(dest_image
, aspect
);
1262 VkFormat buffer_format
= choose_buffer_format(dest_image
, aspect
);
1264 struct anv_image
*src_image
=
1265 make_image_for_buffer(vk_device
, srcBuffer
, buffer_format
,
1266 VK_IMAGE_USAGE_SAMPLED_BIT
,
1267 dest_image
->type
, &cmd_buffer
->pool
->alloc
,
1270 const uint32_t dest_base_array_slice
=
1271 meta_blit_get_dest_view_base_array_slice(dest_image
,
1272 &pRegions
[r
].imageSubresource
,
1273 &pRegions
[r
].imageOffset
);
1275 unsigned num_slices
;
1276 if (dest_image
->type
== VK_IMAGE_TYPE_3D
) {
1277 assert(pRegions
[r
].imageSubresource
.layerCount
== 1);
1278 num_slices
= pRegions
[r
].imageExtent
.depth
;
1280 assert(pRegions
[r
].imageExtent
.depth
== 1);
1281 num_slices
= pRegions
[r
].imageSubresource
.layerCount
;
1284 for (unsigned slice
= 0; slice
< num_slices
; slice
++) {
1285 struct anv_image_view src_iview
;
1286 anv_image_view_init(&src_iview
, cmd_buffer
->device
,
1287 &(VkImageViewCreateInfo
) {
1288 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1289 .image
= anv_image_to_handle(src_image
),
1290 .viewType
= VK_IMAGE_VIEW_TYPE_2D
,
1291 .format
= buffer_format
,
1292 .subresourceRange
= {
1293 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1296 .baseArrayLayer
= 0,
1302 struct anv_image_view dest_iview
;
1303 anv_image_view_init(&dest_iview
, cmd_buffer
->device
,
1304 &(VkImageViewCreateInfo
) {
1305 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1306 .image
= anv_image_to_handle(dest_image
),
1307 .viewType
= anv_meta_get_view_type(dest_image
),
1308 .format
= image_format
,
1309 .subresourceRange
= {
1310 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1311 .baseMipLevel
= pRegions
[r
].imageSubresource
.mipLevel
,
1313 .baseArrayLayer
= dest_base_array_slice
+ slice
,
1319 VkOffset3D src_offset
= { 0, 0, slice
};
1321 const VkOffset3D dest_offset
= {
1322 .x
= pRegions
[r
].imageOffset
.x
,
1323 .y
= pRegions
[r
].imageOffset
.y
,
1327 meta_emit_blit(cmd_buffer
,
1331 pRegions
[r
].imageExtent
,
1335 pRegions
[r
].imageExtent
,
1338 /* Once we've done the blit, all of the actual information about
1339 * the image is embedded in the command buffer so we can just
1340 * increment the offset directly in the image effectively
1341 * re-binding it to different backing memory.
1343 src_image
->offset
+= src_image
->extent
.width
*
1344 src_image
->extent
.height
*
1345 src_image
->format
->isl_layout
->bs
;
1348 anv_DestroyImage(vk_device
, anv_image_to_handle(src_image
),
1349 &cmd_buffer
->pool
->alloc
);
1352 meta_finish_blit(cmd_buffer
, &saved_state
);
1355 void anv_CmdCopyImageToBuffer(
1356 VkCommandBuffer commandBuffer
,
1358 VkImageLayout srcImageLayout
,
1359 VkBuffer destBuffer
,
1360 uint32_t regionCount
,
1361 const VkBufferImageCopy
* pRegions
)
1363 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1364 ANV_FROM_HANDLE(anv_image
, src_image
, srcImage
);
1365 VkDevice vk_device
= anv_device_to_handle(cmd_buffer
->device
);
1366 struct anv_meta_saved_state saved_state
;
1369 /* The Vulkan 1.0 spec says "srcImage must have a sample count equal to
1370 * VK_SAMPLE_COUNT_1_BIT."
1372 assert(src_image
->samples
== 1);
1374 meta_prepare_blit(cmd_buffer
, &saved_state
);
1376 for (unsigned r
= 0; r
< regionCount
; r
++) {
1377 VkImageAspectFlags aspect
= pRegions
[r
].imageSubresource
.aspectMask
;
1379 VkFormat image_format
= choose_iview_format(src_image
, aspect
);
1380 VkFormat buffer_format
= choose_buffer_format(src_image
, aspect
);
1382 struct anv_image_view src_iview
;
1383 anv_image_view_init(&src_iview
, cmd_buffer
->device
,
1384 &(VkImageViewCreateInfo
) {
1385 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1387 .viewType
= anv_meta_get_view_type(src_image
),
1388 .format
= image_format
,
1389 .subresourceRange
= {
1390 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1391 .baseMipLevel
= pRegions
[r
].imageSubresource
.mipLevel
,
1393 .baseArrayLayer
= pRegions
[r
].imageSubresource
.baseArrayLayer
,
1394 .layerCount
= pRegions
[r
].imageSubresource
.layerCount
,
1399 struct anv_image
*dest_image
=
1400 make_image_for_buffer(vk_device
, destBuffer
, buffer_format
,
1401 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
,
1402 src_image
->type
, &cmd_buffer
->pool
->alloc
,
1405 unsigned num_slices
;
1406 if (src_image
->type
== VK_IMAGE_TYPE_3D
) {
1407 assert(pRegions
[r
].imageSubresource
.layerCount
== 1);
1408 num_slices
= pRegions
[r
].imageExtent
.depth
;
1410 assert(pRegions
[r
].imageExtent
.depth
== 1);
1411 num_slices
= pRegions
[r
].imageSubresource
.layerCount
;
1414 for (unsigned slice
= 0; slice
< num_slices
; slice
++) {
1415 VkOffset3D src_offset
= pRegions
[r
].imageOffset
;
1416 src_offset
.z
+= slice
;
1418 struct anv_image_view dest_iview
;
1419 anv_image_view_init(&dest_iview
, cmd_buffer
->device
,
1420 &(VkImageViewCreateInfo
) {
1421 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1422 .image
= anv_image_to_handle(dest_image
),
1423 .viewType
= VK_IMAGE_VIEW_TYPE_2D
,
1424 .format
= buffer_format
,
1425 .subresourceRange
= {
1426 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1429 .baseArrayLayer
= 0,
1435 meta_emit_blit(cmd_buffer
,
1436 anv_image_from_handle(srcImage
),
1439 pRegions
[r
].imageExtent
,
1442 (VkOffset3D
) { 0, 0, 0 },
1443 pRegions
[r
].imageExtent
,
1446 /* Once we've done the blit, all of the actual information about
1447 * the image is embedded in the command buffer so we can just
1448 * increment the offset directly in the image effectively
1449 * re-binding it to different backing memory.
1451 dest_image
->offset
+= dest_image
->extent
.width
*
1452 dest_image
->extent
.height
*
1453 src_image
->format
->isl_layout
->bs
;
1456 anv_DestroyImage(vk_device
, anv_image_to_handle(dest_image
),
1457 &cmd_buffer
->pool
->alloc
);
1460 meta_finish_blit(cmd_buffer
, &saved_state
);
1463 void anv_CmdResolveImage(
1464 VkCommandBuffer commandBuffer
,
1466 VkImageLayout srcImageLayout
,
1468 VkImageLayout destImageLayout
,
1469 uint32_t regionCount
,
1470 const VkImageResolve
* pRegions
)
1476 meta_alloc(void* _device
, size_t size
, size_t alignment
,
1477 VkSystemAllocationScope allocationScope
)
1479 struct anv_device
*device
= _device
;
1480 return device
->alloc
.pfnAllocation(device
->alloc
.pUserData
, size
, alignment
,
1481 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
1485 meta_realloc(void* _device
, void *original
, size_t size
, size_t alignment
,
1486 VkSystemAllocationScope allocationScope
)
1488 struct anv_device
*device
= _device
;
1489 return device
->alloc
.pfnReallocation(device
->alloc
.pUserData
, original
,
1491 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
1495 meta_free(void* _device
, void *data
)
1497 struct anv_device
*device
= _device
;
1498 return device
->alloc
.pfnFree(device
->alloc
.pUserData
, data
);
1502 anv_device_init_meta(struct anv_device
*device
)
1504 device
->meta_state
.alloc
= (VkAllocationCallbacks
) {
1505 .pUserData
= device
,
1506 .pfnAllocation
= meta_alloc
,
1507 .pfnReallocation
= meta_realloc
,
1508 .pfnFree
= meta_free
,
1512 result
= anv_device_init_meta_clear_state(device
);
1513 if (result
!= VK_SUCCESS
)
1516 result
= anv_device_init_meta_blit_state(device
);
1517 if (result
!= VK_SUCCESS
) {
1518 anv_device_finish_meta_clear_state(device
);
1526 anv_device_finish_meta(struct anv_device
*device
)
1528 anv_device_finish_meta_clear_state(device
);
1531 anv_DestroyRenderPass(anv_device_to_handle(device
),
1532 device
->meta_state
.blit
.render_pass
,
1533 &device
->meta_state
.alloc
);
1534 anv_DestroyPipeline(anv_device_to_handle(device
),
1535 device
->meta_state
.blit
.pipeline_1d_src
,
1536 &device
->meta_state
.alloc
);
1537 anv_DestroyPipeline(anv_device_to_handle(device
),
1538 device
->meta_state
.blit
.pipeline_2d_src
,
1539 &device
->meta_state
.alloc
);
1540 anv_DestroyPipeline(anv_device_to_handle(device
),
1541 device
->meta_state
.blit
.pipeline_3d_src
,
1542 &device
->meta_state
.alloc
);
1543 anv_DestroyPipelineLayout(anv_device_to_handle(device
),
1544 device
->meta_state
.blit
.pipeline_layout
,
1545 &device
->meta_state
.alloc
);
1546 anv_DestroyDescriptorSetLayout(anv_device_to_handle(device
),
1547 device
->meta_state
.blit
.ds_layout
,
1548 &device
->meta_state
.alloc
);