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
25 #include "nir/nir_builder.h"
28 VkOffset3D src_offset
;
29 VkExtent3D src_extent
;
30 VkOffset3D dest_offset
;
31 VkExtent3D dest_extent
;
35 build_nir_vertex_shader(void)
37 const struct glsl_type
*vec4
= glsl_vec4_type();
40 nir_builder_init_simple_shader(&b
, NULL
, MESA_SHADER_VERTEX
, NULL
);
41 b
.shader
->info
.name
= ralloc_strdup(b
.shader
, "meta_blit_vs");
43 nir_variable
*pos_in
= nir_variable_create(b
.shader
, nir_var_shader_in
,
45 pos_in
->data
.location
= VERT_ATTRIB_GENERIC0
;
46 nir_variable
*pos_out
= nir_variable_create(b
.shader
, nir_var_shader_out
,
48 pos_out
->data
.location
= VARYING_SLOT_POS
;
49 nir_copy_var(&b
, pos_out
, pos_in
);
51 nir_variable
*tex_pos_in
= nir_variable_create(b
.shader
, nir_var_shader_in
,
53 tex_pos_in
->data
.location
= VERT_ATTRIB_GENERIC1
;
54 nir_variable
*tex_pos_out
= nir_variable_create(b
.shader
, nir_var_shader_out
,
56 tex_pos_out
->data
.location
= VARYING_SLOT_VAR0
;
57 tex_pos_out
->data
.interpolation
= INTERP_QUALIFIER_SMOOTH
;
58 nir_copy_var(&b
, tex_pos_out
, tex_pos_in
);
64 build_nir_copy_fragment_shader(enum glsl_sampler_dim tex_dim
)
66 const struct glsl_type
*vec4
= glsl_vec4_type();
69 nir_builder_init_simple_shader(&b
, NULL
, MESA_SHADER_FRAGMENT
, NULL
);
70 b
.shader
->info
.name
= ralloc_strdup(b
.shader
, "meta_blit_fs");
72 nir_variable
*tex_pos_in
= nir_variable_create(b
.shader
, nir_var_shader_in
,
74 tex_pos_in
->data
.location
= VARYING_SLOT_VAR0
;
76 /* Swizzle the array index which comes in as Z coordinate into the right
79 unsigned swz
[] = { 0, (tex_dim
== GLSL_SAMPLER_DIM_1D
? 2 : 1), 2 };
80 nir_ssa_def
*const tex_pos
=
81 nir_swizzle(&b
, nir_load_var(&b
, tex_pos_in
), swz
,
82 (tex_dim
== GLSL_SAMPLER_DIM_1D
? 2 : 3), false);
84 const struct glsl_type
*sampler_type
=
85 glsl_sampler_type(tex_dim
, false, tex_dim
!= GLSL_SAMPLER_DIM_3D
,
86 glsl_get_base_type(vec4
));
87 nir_variable
*sampler
= nir_variable_create(b
.shader
, nir_var_uniform
,
88 sampler_type
, "s_tex");
89 sampler
->data
.descriptor_set
= 0;
90 sampler
->data
.binding
= 0;
92 nir_tex_instr
*tex
= nir_tex_instr_create(b
.shader
, 1);
93 tex
->sampler_dim
= tex_dim
;
94 tex
->op
= nir_texop_tex
;
95 tex
->src
[0].src_type
= nir_tex_src_coord
;
96 tex
->src
[0].src
= nir_src_for_ssa(tex_pos
);
97 tex
->dest_type
= nir_type_float
; /* TODO */
98 tex
->is_array
= glsl_sampler_type_is_array(sampler_type
);
99 tex
->coord_components
= tex_pos
->num_components
;
100 tex
->texture
= nir_deref_var_create(tex
, sampler
);
101 tex
->sampler
= nir_deref_var_create(tex
, sampler
);
103 nir_ssa_dest_init(&tex
->instr
, &tex
->dest
, 4, "tex");
104 nir_builder_instr_insert(&b
, &tex
->instr
);
106 nir_variable
*color_out
= nir_variable_create(b
.shader
, nir_var_shader_out
,
108 color_out
->data
.location
= FRAG_RESULT_DATA0
;
109 nir_store_var(&b
, color_out
, &tex
->dest
.ssa
, 4);
115 meta_prepare_blit(struct anv_cmd_buffer
*cmd_buffer
,
116 struct anv_meta_saved_state
*saved_state
)
118 anv_meta_save(saved_state
, cmd_buffer
,
119 (1 << VK_DYNAMIC_STATE_VIEWPORT
));
122 /* Returns the user-provided VkBufferImageCopy::imageOffset in units of
123 * elements rather than texels. One element equals one texel or one block
124 * if Image is uncompressed or compressed, respectively.
126 static struct VkOffset3D
127 meta_region_offset_el(const struct anv_image
* image
,
128 const struct VkOffset3D
* offset
)
130 const struct isl_format_layout
* isl_layout
= image
->format
->isl_layout
;
131 return (VkOffset3D
) {
132 .x
= offset
->x
/ isl_layout
->bw
,
133 .y
= offset
->y
/ isl_layout
->bh
,
134 .z
= offset
->z
/ isl_layout
->bd
,
138 /* Returns the user-provided VkBufferImageCopy::imageExtent in units of
139 * elements rather than texels. One element equals one texel or one block
140 * if Image is uncompressed or compressed, respectively.
142 static struct VkExtent3D
143 meta_region_extent_el(const VkFormat format
,
144 const struct VkExtent3D
* extent
)
146 const struct isl_format_layout
* isl_layout
=
147 anv_format_for_vk_format(format
)->isl_layout
;
148 return (VkExtent3D
) {
149 .width
= DIV_ROUND_UP(extent
->width
, isl_layout
->bw
),
150 .height
= DIV_ROUND_UP(extent
->height
, isl_layout
->bh
),
151 .depth
= DIV_ROUND_UP(extent
->depth
, isl_layout
->bd
),
156 meta_emit_blit(struct anv_cmd_buffer
*cmd_buffer
,
157 struct anv_image
*src_image
,
158 struct anv_image_view
*src_iview
,
159 VkOffset3D src_offset
,
160 VkExtent3D src_extent
,
161 struct anv_image
*dest_image
,
162 struct anv_image_view
*dest_iview
,
163 VkOffset3D dest_offset
,
164 VkExtent3D dest_extent
,
165 VkFilter blit_filter
)
167 struct anv_device
*device
= cmd_buffer
->device
;
169 struct blit_vb_data
{
174 assert(src_image
->samples
== dest_image
->samples
);
176 unsigned vb_size
= sizeof(struct anv_vue_header
) + 3 * sizeof(*vb_data
);
178 struct anv_state vb_state
=
179 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, vb_size
, 16);
180 memset(vb_state
.map
, 0, sizeof(struct anv_vue_header
));
181 vb_data
= vb_state
.map
+ sizeof(struct anv_vue_header
);
183 vb_data
[0] = (struct blit_vb_data
) {
185 dest_offset
.x
+ dest_extent
.width
,
186 dest_offset
.y
+ dest_extent
.height
,
189 (float)(src_offset
.x
+ src_extent
.width
) / (float)src_iview
->extent
.width
,
190 (float)(src_offset
.y
+ src_extent
.height
) / (float)src_iview
->extent
.height
,
191 (float)src_offset
.z
/ (float)src_iview
->extent
.depth
,
195 vb_data
[1] = (struct blit_vb_data
) {
198 dest_offset
.y
+ dest_extent
.height
,
201 (float)src_offset
.x
/ (float)src_iview
->extent
.width
,
202 (float)(src_offset
.y
+ src_extent
.height
) / (float)src_iview
->extent
.height
,
203 (float)src_offset
.z
/ (float)src_iview
->extent
.depth
,
207 vb_data
[2] = (struct blit_vb_data
) {
213 (float)src_offset
.x
/ (float)src_iview
->extent
.width
,
214 (float)src_offset
.y
/ (float)src_iview
->extent
.height
,
215 (float)src_offset
.z
/ (float)src_iview
->extent
.depth
,
219 anv_state_clflush(vb_state
);
221 struct anv_buffer vertex_buffer
= {
224 .bo
= &device
->dynamic_state_block_pool
.bo
,
225 .offset
= vb_state
.offset
,
228 anv_CmdBindVertexBuffers(anv_cmd_buffer_to_handle(cmd_buffer
), 0, 2,
230 anv_buffer_to_handle(&vertex_buffer
),
231 anv_buffer_to_handle(&vertex_buffer
)
235 sizeof(struct anv_vue_header
),
239 ANV_CALL(CreateSampler
)(anv_device_to_handle(device
),
240 &(VkSamplerCreateInfo
) {
241 .sType
= VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO
,
242 .magFilter
= blit_filter
,
243 .minFilter
= blit_filter
,
244 }, &cmd_buffer
->pool
->alloc
, &sampler
);
246 VkDescriptorPool desc_pool
;
247 anv_CreateDescriptorPool(anv_device_to_handle(device
),
248 &(const VkDescriptorPoolCreateInfo
) {
249 .sType
= VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO
,
254 .pPoolSizes
= (VkDescriptorPoolSize
[]) {
256 .type
= VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
,
260 }, &cmd_buffer
->pool
->alloc
, &desc_pool
);
263 anv_AllocateDescriptorSets(anv_device_to_handle(device
),
264 &(VkDescriptorSetAllocateInfo
) {
265 .sType
= VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO
,
266 .descriptorPool
= desc_pool
,
267 .descriptorSetCount
= 1,
268 .pSetLayouts
= &device
->meta_state
.blit
.ds_layout
271 anv_UpdateDescriptorSets(anv_device_to_handle(device
),
273 (VkWriteDescriptorSet
[]) {
275 .sType
= VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET
,
278 .dstArrayElement
= 0,
279 .descriptorCount
= 1,
280 .descriptorType
= VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
,
281 .pImageInfo
= (VkDescriptorImageInfo
[]) {
284 .imageView
= anv_image_view_to_handle(src_iview
),
285 .imageLayout
= VK_IMAGE_LAYOUT_GENERAL
,
292 anv_CreateFramebuffer(anv_device_to_handle(device
),
293 &(VkFramebufferCreateInfo
) {
294 .sType
= VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
,
295 .attachmentCount
= 1,
296 .pAttachments
= (VkImageView
[]) {
297 anv_image_view_to_handle(dest_iview
),
299 .width
= dest_iview
->extent
.width
,
300 .height
= dest_iview
->extent
.height
,
302 }, &cmd_buffer
->pool
->alloc
, &fb
);
304 ANV_CALL(CmdBeginRenderPass
)(anv_cmd_buffer_to_handle(cmd_buffer
),
305 &(VkRenderPassBeginInfo
) {
306 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO
,
307 .renderPass
= device
->meta_state
.blit
.render_pass
,
310 .offset
= { dest_offset
.x
, dest_offset
.y
},
311 .extent
= { dest_extent
.width
, dest_extent
.height
},
313 .clearValueCount
= 0,
314 .pClearValues
= NULL
,
315 }, VK_SUBPASS_CONTENTS_INLINE
);
319 switch (src_image
->type
) {
320 case VK_IMAGE_TYPE_1D
:
321 pipeline
= device
->meta_state
.blit
.pipeline_1d_src
;
323 case VK_IMAGE_TYPE_2D
:
324 pipeline
= device
->meta_state
.blit
.pipeline_2d_src
;
326 case VK_IMAGE_TYPE_3D
:
327 pipeline
= device
->meta_state
.blit
.pipeline_3d_src
;
330 unreachable(!"bad VkImageType");
333 if (cmd_buffer
->state
.pipeline
!= anv_pipeline_from_handle(pipeline
)) {
334 anv_CmdBindPipeline(anv_cmd_buffer_to_handle(cmd_buffer
),
335 VK_PIPELINE_BIND_POINT_GRAPHICS
, pipeline
);
338 anv_CmdSetViewport(anv_cmd_buffer_to_handle(cmd_buffer
), 0, 1,
342 .width
= dest_iview
->extent
.width
,
343 .height
= dest_iview
->extent
.height
,
348 anv_CmdBindDescriptorSets(anv_cmd_buffer_to_handle(cmd_buffer
),
349 VK_PIPELINE_BIND_POINT_GRAPHICS
,
350 device
->meta_state
.blit
.pipeline_layout
, 0, 1,
353 ANV_CALL(CmdDraw
)(anv_cmd_buffer_to_handle(cmd_buffer
), 3, 1, 0, 0);
355 ANV_CALL(CmdEndRenderPass
)(anv_cmd_buffer_to_handle(cmd_buffer
));
357 /* At the point where we emit the draw call, all data from the
358 * descriptor sets, etc. has been used. We are free to delete it.
360 anv_DestroyDescriptorPool(anv_device_to_handle(device
),
361 desc_pool
, &cmd_buffer
->pool
->alloc
);
362 anv_DestroySampler(anv_device_to_handle(device
), sampler
,
363 &cmd_buffer
->pool
->alloc
);
364 anv_DestroyFramebuffer(anv_device_to_handle(device
), fb
,
365 &cmd_buffer
->pool
->alloc
);
369 meta_finish_blit(struct anv_cmd_buffer
*cmd_buffer
,
370 const struct anv_meta_saved_state
*saved_state
)
372 anv_meta_restore(saved_state
, cmd_buffer
);
376 vk_format_for_size(int bs
)
378 /* Note: We intentionally use the 4-channel formats whenever we can.
379 * This is so that, when we do a RGB <-> RGBX copy, the two formats will
380 * line up even though one of them is 3/4 the size of the other.
383 case 1: return VK_FORMAT_R8_UINT
;
384 case 2: return VK_FORMAT_R8G8_UINT
;
385 case 3: return VK_FORMAT_R8G8B8_UINT
;
386 case 4: return VK_FORMAT_R8G8B8A8_UINT
;
387 case 6: return VK_FORMAT_R16G16B16_UINT
;
388 case 8: return VK_FORMAT_R16G16B16A16_UINT
;
389 case 12: return VK_FORMAT_R32G32B32_UINT
;
390 case 16: return VK_FORMAT_R32G32B32A32_UINT
;
392 unreachable("Invalid format block size");
397 do_buffer_copy(struct anv_cmd_buffer
*cmd_buffer
,
398 struct anv_bo
*src
, uint64_t src_offset
,
399 struct anv_bo
*dest
, uint64_t dest_offset
,
400 int width
, int height
, VkFormat copy_format
)
402 VkDevice vk_device
= anv_device_to_handle(cmd_buffer
->device
);
404 VkImageCreateInfo image_info
= {
405 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
406 .imageType
= VK_IMAGE_TYPE_2D
,
407 .format
= copy_format
,
416 .tiling
= VK_IMAGE_TILING_LINEAR
,
422 image_info
.usage
= VK_IMAGE_USAGE_SAMPLED_BIT
;
423 anv_CreateImage(vk_device
, &image_info
,
424 &cmd_buffer
->pool
->alloc
, &src_image
);
427 image_info
.usage
= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
428 anv_CreateImage(vk_device
, &image_info
,
429 &cmd_buffer
->pool
->alloc
, &dest_image
);
431 /* We could use a vk call to bind memory, but that would require
432 * creating a dummy memory object etc. so there's really no point.
434 anv_image_from_handle(src_image
)->bo
= src
;
435 anv_image_from_handle(src_image
)->offset
= src_offset
;
436 anv_image_from_handle(dest_image
)->bo
= dest
;
437 anv_image_from_handle(dest_image
)->offset
= dest_offset
;
439 struct anv_image_view src_iview
;
440 anv_image_view_init(&src_iview
, cmd_buffer
->device
,
441 &(VkImageViewCreateInfo
) {
442 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
444 .viewType
= VK_IMAGE_VIEW_TYPE_2D
,
445 .format
= copy_format
,
446 .subresourceRange
= {
447 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
454 cmd_buffer
, 0, VK_IMAGE_USAGE_SAMPLED_BIT
);
456 struct anv_image_view dest_iview
;
457 anv_image_view_init(&dest_iview
, cmd_buffer
->device
,
458 &(VkImageViewCreateInfo
) {
459 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
461 .viewType
= VK_IMAGE_VIEW_TYPE_2D
,
462 .format
= copy_format
,
463 .subresourceRange
= {
464 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
471 cmd_buffer
, 0, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
);
473 meta_emit_blit(cmd_buffer
,
474 anv_image_from_handle(src_image
),
476 (VkOffset3D
) { 0, 0, 0 },
477 (VkExtent3D
) { width
, height
, 1 },
478 anv_image_from_handle(dest_image
),
480 (VkOffset3D
) { 0, 0, 0 },
481 (VkExtent3D
) { width
, height
, 1 },
484 anv_DestroyImage(vk_device
, src_image
, &cmd_buffer
->pool
->alloc
);
485 anv_DestroyImage(vk_device
, dest_image
, &cmd_buffer
->pool
->alloc
);
488 void anv_CmdCopyBuffer(
489 VkCommandBuffer commandBuffer
,
492 uint32_t regionCount
,
493 const VkBufferCopy
* pRegions
)
495 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
496 ANV_FROM_HANDLE(anv_buffer
, src_buffer
, srcBuffer
);
497 ANV_FROM_HANDLE(anv_buffer
, dest_buffer
, destBuffer
);
499 struct anv_meta_saved_state saved_state
;
501 meta_prepare_blit(cmd_buffer
, &saved_state
);
503 for (unsigned r
= 0; r
< regionCount
; r
++) {
504 uint64_t src_offset
= src_buffer
->offset
+ pRegions
[r
].srcOffset
;
505 uint64_t dest_offset
= dest_buffer
->offset
+ pRegions
[r
].dstOffset
;
506 uint64_t copy_size
= pRegions
[r
].size
;
508 /* First, we compute the biggest format that can be used with the
509 * given offsets and size.
513 int fs
= ffs(src_offset
) - 1;
515 bs
= MIN2(bs
, 1 << fs
);
516 assert(src_offset
% bs
== 0);
518 fs
= ffs(dest_offset
) - 1;
520 bs
= MIN2(bs
, 1 << fs
);
521 assert(dest_offset
% bs
== 0);
523 fs
= ffs(pRegions
[r
].size
) - 1;
525 bs
= MIN2(bs
, 1 << fs
);
526 assert(pRegions
[r
].size
% bs
== 0);
528 VkFormat copy_format
= vk_format_for_size(bs
);
530 /* This is maximum possible width/height our HW can handle */
531 uint64_t max_surface_dim
= 1 << 14;
533 /* First, we make a bunch of max-sized copies */
534 uint64_t max_copy_size
= max_surface_dim
* max_surface_dim
* bs
;
535 while (copy_size
>= max_copy_size
) {
536 do_buffer_copy(cmd_buffer
, src_buffer
->bo
, src_offset
,
537 dest_buffer
->bo
, dest_offset
,
538 max_surface_dim
, max_surface_dim
, copy_format
);
539 copy_size
-= max_copy_size
;
540 src_offset
+= max_copy_size
;
541 dest_offset
+= max_copy_size
;
544 uint64_t height
= copy_size
/ (max_surface_dim
* bs
);
545 assert(height
< max_surface_dim
);
547 uint64_t rect_copy_size
= height
* max_surface_dim
* bs
;
548 do_buffer_copy(cmd_buffer
, src_buffer
->bo
, src_offset
,
549 dest_buffer
->bo
, dest_offset
,
550 max_surface_dim
, height
, copy_format
);
551 copy_size
-= rect_copy_size
;
552 src_offset
+= rect_copy_size
;
553 dest_offset
+= rect_copy_size
;
556 if (copy_size
!= 0) {
557 do_buffer_copy(cmd_buffer
, src_buffer
->bo
, src_offset
,
558 dest_buffer
->bo
, dest_offset
,
559 copy_size
/ bs
, 1, copy_format
);
563 meta_finish_blit(cmd_buffer
, &saved_state
);
566 void anv_CmdUpdateBuffer(
567 VkCommandBuffer commandBuffer
,
569 VkDeviceSize dstOffset
,
570 VkDeviceSize dataSize
,
571 const uint32_t* pData
)
573 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
574 ANV_FROM_HANDLE(anv_buffer
, dst_buffer
, dstBuffer
);
575 struct anv_meta_saved_state saved_state
;
577 meta_prepare_blit(cmd_buffer
, &saved_state
);
579 /* We can't quite grab a full block because the state stream needs a
580 * little data at the top to build its linked list.
582 const uint32_t max_update_size
=
583 cmd_buffer
->device
->dynamic_state_block_pool
.block_size
- 64;
585 assert(max_update_size
< (1 << 14) * 4);
588 const uint32_t copy_size
= MIN2(dataSize
, max_update_size
);
590 struct anv_state tmp_data
=
591 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer
, copy_size
, 64);
593 memcpy(tmp_data
.map
, pData
, copy_size
);
597 if ((copy_size
& 15) == 0 && (dstOffset
& 15) == 0) {
598 format
= VK_FORMAT_R32G32B32A32_UINT
;
600 } else if ((copy_size
& 7) == 0 && (dstOffset
& 7) == 0) {
601 format
= VK_FORMAT_R32G32_UINT
;
604 assert((copy_size
& 3) == 0 && (dstOffset
& 3) == 0);
605 format
= VK_FORMAT_R32_UINT
;
609 do_buffer_copy(cmd_buffer
,
610 &cmd_buffer
->device
->dynamic_state_block_pool
.bo
,
612 dst_buffer
->bo
, dst_buffer
->offset
+ dstOffset
,
613 copy_size
/ bs
, 1, format
);
615 dataSize
-= copy_size
;
616 dstOffset
+= copy_size
;
617 pData
= (void *)pData
+ copy_size
;
622 choose_iview_format(struct anv_image
*image
, VkImageAspectFlagBits aspect
)
624 assert(__builtin_popcount(aspect
) == 1);
626 struct isl_surf
*surf
=
627 &anv_image_get_surface_for_aspect_mask(image
, aspect
)->isl
;
629 /* vkCmdCopyImage behaves like memcpy. Therefore we choose identical UINT
630 * formats for the source and destination image views.
632 * From the Vulkan spec (2015-12-30):
634 * vkCmdCopyImage performs image copies in a similar manner to a host
635 * memcpy. It does not perform general-purpose conversions such as
636 * scaling, resizing, blending, color-space conversion, or format
637 * conversions. Rather, it simply copies raw image data. vkCmdCopyImage
638 * can copy between images with different formats, provided the formats
639 * are compatible as defined below.
641 * [The spec later defines compatibility as having the same number of
644 return vk_format_for_size(isl_format_layouts
[surf
->format
].bs
);
648 choose_buffer_format(VkFormat format
, VkImageAspectFlagBits aspect
)
650 assert(__builtin_popcount(aspect
) == 1);
652 /* vkCmdCopy* commands behave like memcpy. Therefore we choose
653 * compatable UINT formats for the source and destination image views.
655 * For the buffer, we go back to the original image format and get a
656 * the format as if it were linear. This way, for RGB formats, we get
657 * an RGB format here even if the tiled image is RGBA. XXX: This doesn't
658 * work if the buffer is the destination.
660 enum isl_format linear_format
= anv_get_isl_format(format
, aspect
,
661 VK_IMAGE_TILING_LINEAR
,
664 return vk_format_for_size(isl_format_layouts
[linear_format
].bs
);
667 void anv_CmdCopyImage(
668 VkCommandBuffer commandBuffer
,
670 VkImageLayout srcImageLayout
,
672 VkImageLayout destImageLayout
,
673 uint32_t regionCount
,
674 const VkImageCopy
* pRegions
)
676 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
677 ANV_FROM_HANDLE(anv_image
, src_image
, srcImage
);
678 ANV_FROM_HANDLE(anv_image
, dest_image
, destImage
);
679 struct anv_meta_saved_state saved_state
;
681 /* From the Vulkan 1.0 spec:
683 * vkCmdCopyImage can be used to copy image data between multisample
684 * images, but both images must have the same number of samples.
686 assert(src_image
->samples
== dest_image
->samples
);
688 meta_prepare_blit(cmd_buffer
, &saved_state
);
690 for (unsigned r
= 0; r
< regionCount
; r
++) {
691 assert(pRegions
[r
].srcSubresource
.aspectMask
==
692 pRegions
[r
].dstSubresource
.aspectMask
);
694 VkImageAspectFlags aspect
= pRegions
[r
].srcSubresource
.aspectMask
;
696 VkFormat src_format
= choose_iview_format(src_image
, aspect
);
697 VkFormat dst_format
= choose_iview_format(dest_image
, aspect
);
699 struct anv_image_view src_iview
;
700 anv_image_view_init(&src_iview
, cmd_buffer
->device
,
701 &(VkImageViewCreateInfo
) {
702 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
704 .viewType
= anv_meta_get_view_type(src_image
),
705 .format
= src_format
,
706 .subresourceRange
= {
707 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
708 .baseMipLevel
= pRegions
[r
].srcSubresource
.mipLevel
,
710 .baseArrayLayer
= pRegions
[r
].srcSubresource
.baseArrayLayer
,
711 .layerCount
= pRegions
[r
].dstSubresource
.layerCount
,
714 cmd_buffer
, 0, VK_IMAGE_USAGE_SAMPLED_BIT
);
716 const uint32_t dest_base_array_slice
=
717 anv_meta_get_iview_layer(dest_image
, &pRegions
[r
].dstSubresource
,
718 &pRegions
[r
].dstOffset
);
721 unsigned num_slices_3d
= pRegions
[r
].extent
.depth
;
722 unsigned num_slices_array
= pRegions
[r
].dstSubresource
.layerCount
;
723 unsigned slice_3d
= 0;
724 unsigned slice_array
= 0;
725 while (slice_3d
< num_slices_3d
&& slice_array
< num_slices_array
) {
726 VkOffset3D src_offset
= pRegions
[r
].srcOffset
;
727 src_offset
.z
+= slice_3d
+ slice_array
;
732 if (isl_format_is_compressed(dest_image
->format
->isl_format
))
733 isl_surf_get_image_intratile_offset_el(&cmd_buffer
->device
->isl_dev
,
734 &dest_image
->color_surface
.isl
,
735 pRegions
[r
].dstSubresource
.mipLevel
,
736 pRegions
[r
].dstSubresource
.baseArrayLayer
+ slice_array
,
737 pRegions
[r
].dstOffset
.z
+ slice_3d
,
738 &img_o
, &img_x
, &img_y
);
740 VkOffset3D dest_offset_el
= meta_region_offset_el(dest_image
, &pRegions
[r
].dstOffset
);
741 dest_offset_el
.x
+= img_x
;
742 dest_offset_el
.y
+= img_y
;
743 dest_offset_el
.z
= 0;
745 struct anv_image_view dest_iview
;
746 anv_image_view_init(&dest_iview
, cmd_buffer
->device
,
747 &(VkImageViewCreateInfo
) {
748 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
750 .viewType
= anv_meta_get_view_type(dest_image
),
751 .format
= dst_format
,
752 .subresourceRange
= {
753 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
754 .baseMipLevel
= pRegions
[r
].dstSubresource
.mipLevel
,
756 .baseArrayLayer
= dest_base_array_slice
+
757 slice_array
+ slice_3d
,
761 cmd_buffer
, img_o
, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
);
763 const VkExtent3D img_extent_el
= meta_region_extent_el(dest_image
->vk_format
,
764 &pRegions
[r
].extent
);
766 meta_emit_blit(cmd_buffer
,
767 src_image
, &src_iview
,
770 dest_image
, &dest_iview
,
775 if (dest_image
->type
== VK_IMAGE_TYPE_3D
)
782 meta_finish_blit(cmd_buffer
, &saved_state
);
785 void anv_CmdBlitImage(
786 VkCommandBuffer commandBuffer
,
788 VkImageLayout srcImageLayout
,
790 VkImageLayout destImageLayout
,
791 uint32_t regionCount
,
792 const VkImageBlit
* pRegions
,
796 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
797 ANV_FROM_HANDLE(anv_image
, src_image
, srcImage
);
798 ANV_FROM_HANDLE(anv_image
, dest_image
, destImage
);
799 struct anv_meta_saved_state saved_state
;
801 /* From the Vulkan 1.0 spec:
803 * vkCmdBlitImage must not be used for multisampled source or
804 * destination images. Use vkCmdResolveImage for this purpose.
806 assert(src_image
->samples
== 1);
807 assert(dest_image
->samples
== 1);
809 anv_finishme("respect VkFilter");
811 meta_prepare_blit(cmd_buffer
, &saved_state
);
813 for (unsigned r
= 0; r
< regionCount
; r
++) {
814 struct anv_image_view src_iview
;
815 anv_image_view_init(&src_iview
, cmd_buffer
->device
,
816 &(VkImageViewCreateInfo
) {
817 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
819 .viewType
= anv_meta_get_view_type(src_image
),
820 .format
= src_image
->vk_format
,
821 .subresourceRange
= {
822 .aspectMask
= pRegions
[r
].srcSubresource
.aspectMask
,
823 .baseMipLevel
= pRegions
[r
].srcSubresource
.mipLevel
,
825 .baseArrayLayer
= pRegions
[r
].srcSubresource
.baseArrayLayer
,
829 cmd_buffer
, 0, VK_IMAGE_USAGE_SAMPLED_BIT
);
831 const VkOffset3D dest_offset
= {
832 .x
= pRegions
[r
].dstOffsets
[0].x
,
833 .y
= pRegions
[r
].dstOffsets
[0].y
,
837 if (pRegions
[r
].dstOffsets
[1].x
< pRegions
[r
].dstOffsets
[0].x
||
838 pRegions
[r
].dstOffsets
[1].y
< pRegions
[r
].dstOffsets
[0].y
||
839 pRegions
[r
].srcOffsets
[1].x
< pRegions
[r
].srcOffsets
[0].x
||
840 pRegions
[r
].srcOffsets
[1].y
< pRegions
[r
].srcOffsets
[0].y
)
841 anv_finishme("FINISHME: Allow flipping in blits");
843 const VkExtent3D dest_extent
= {
844 .width
= pRegions
[r
].dstOffsets
[1].x
- pRegions
[r
].dstOffsets
[0].x
,
845 .height
= pRegions
[r
].dstOffsets
[1].y
- pRegions
[r
].dstOffsets
[0].y
,
848 const VkExtent3D src_extent
= {
849 .width
= pRegions
[r
].srcOffsets
[1].x
- pRegions
[r
].srcOffsets
[0].x
,
850 .height
= pRegions
[r
].srcOffsets
[1].y
- pRegions
[r
].srcOffsets
[0].y
,
853 const uint32_t dest_array_slice
=
854 anv_meta_get_iview_layer(dest_image
, &pRegions
[r
].dstSubresource
,
855 &pRegions
[r
].dstOffsets
[0]);
857 if (pRegions
[r
].srcSubresource
.layerCount
> 1)
858 anv_finishme("FINISHME: copy multiple array layers");
860 if (pRegions
[r
].srcOffsets
[0].z
+ 1 != pRegions
[r
].srcOffsets
[1].z
||
861 pRegions
[r
].dstOffsets
[0].z
+ 1 != pRegions
[r
].dstOffsets
[1].z
)
862 anv_finishme("FINISHME: copy multiple depth layers");
864 struct anv_image_view dest_iview
;
865 anv_image_view_init(&dest_iview
, cmd_buffer
->device
,
866 &(VkImageViewCreateInfo
) {
867 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
869 .viewType
= anv_meta_get_view_type(dest_image
),
870 .format
= dest_image
->vk_format
,
871 .subresourceRange
= {
872 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
873 .baseMipLevel
= pRegions
[r
].dstSubresource
.mipLevel
,
875 .baseArrayLayer
= dest_array_slice
,
879 cmd_buffer
, 0, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
);
881 meta_emit_blit(cmd_buffer
,
882 src_image
, &src_iview
,
883 pRegions
[r
].srcOffsets
[0], src_extent
,
884 dest_image
, &dest_iview
,
885 dest_offset
, dest_extent
,
889 meta_finish_blit(cmd_buffer
, &saved_state
);
892 static struct anv_image
*
893 make_image_for_buffer(VkDevice vk_device
, VkBuffer vk_buffer
, VkFormat format
,
894 VkImageUsageFlags usage
,
895 VkImageType image_type
,
896 const VkAllocationCallbacks
*alloc
,
897 const VkBufferImageCopy
*copy
)
899 ANV_FROM_HANDLE(anv_buffer
, buffer
, vk_buffer
);
901 VkExtent3D extent
= copy
->imageExtent
;
902 if (copy
->bufferRowLength
)
903 extent
.width
= copy
->bufferRowLength
;
904 if (copy
->bufferImageHeight
)
905 extent
.height
= copy
->bufferImageHeight
;
907 extent
= meta_region_extent_el(format
, &extent
);
909 VkImageAspectFlags aspect
= copy
->imageSubresource
.aspectMask
;
910 VkFormat buffer_format
= choose_buffer_format(format
, aspect
);
913 VkResult result
= anv_CreateImage(vk_device
,
914 &(VkImageCreateInfo
) {
915 .sType
= VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
,
916 .imageType
= VK_IMAGE_TYPE_2D
,
917 .format
= buffer_format
,
922 .tiling
= VK_IMAGE_TILING_LINEAR
,
925 }, alloc
, &vk_image
);
926 assert(result
== VK_SUCCESS
);
928 ANV_FROM_HANDLE(anv_image
, image
, vk_image
);
930 /* We could use a vk call to bind memory, but that would require
931 * creating a dummy memory object etc. so there's really no point.
933 image
->bo
= buffer
->bo
;
934 image
->offset
= buffer
->offset
+ copy
->bufferOffset
;
939 void anv_CmdCopyBufferToImage(
940 VkCommandBuffer commandBuffer
,
943 VkImageLayout destImageLayout
,
944 uint32_t regionCount
,
945 const VkBufferImageCopy
* pRegions
)
947 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
948 ANV_FROM_HANDLE(anv_image
, dest_image
, destImage
);
949 VkDevice vk_device
= anv_device_to_handle(cmd_buffer
->device
);
950 struct anv_meta_saved_state saved_state
;
952 /* The Vulkan 1.0 spec says "dstImage must have a sample count equal to
953 * VK_SAMPLE_COUNT_1_BIT."
955 assert(dest_image
->samples
== 1);
957 meta_prepare_blit(cmd_buffer
, &saved_state
);
959 for (unsigned r
= 0; r
< regionCount
; r
++) {
960 VkImageAspectFlags aspect
= pRegions
[r
].imageSubresource
.aspectMask
;
962 VkFormat image_format
= choose_iview_format(dest_image
, aspect
);
964 struct anv_image
*src_image
=
965 make_image_for_buffer(vk_device
, srcBuffer
, dest_image
->vk_format
,
966 VK_IMAGE_USAGE_SAMPLED_BIT
,
967 dest_image
->type
, &cmd_buffer
->pool
->alloc
,
970 const uint32_t dest_base_array_slice
=
971 anv_meta_get_iview_layer(dest_image
, &pRegions
[r
].imageSubresource
,
972 &pRegions
[r
].imageOffset
);
974 unsigned num_slices_3d
= pRegions
[r
].imageExtent
.depth
;
975 unsigned num_slices_array
= pRegions
[r
].imageSubresource
.layerCount
;
976 unsigned slice_3d
= 0;
977 unsigned slice_array
= 0;
978 while (slice_3d
< num_slices_3d
&& slice_array
< num_slices_array
) {
979 struct anv_image_view src_iview
;
980 anv_image_view_init(&src_iview
, cmd_buffer
->device
,
981 &(VkImageViewCreateInfo
) {
982 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
983 .image
= anv_image_to_handle(src_image
),
984 .viewType
= VK_IMAGE_VIEW_TYPE_2D
,
985 .format
= src_image
->vk_format
,
986 .subresourceRange
= {
987 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
994 cmd_buffer
, 0, VK_IMAGE_USAGE_SAMPLED_BIT
);
999 if (isl_format_is_compressed(dest_image
->format
->isl_format
))
1000 isl_surf_get_image_intratile_offset_el(&cmd_buffer
->device
->isl_dev
,
1001 &dest_image
->color_surface
.isl
,
1002 pRegions
[r
].imageSubresource
.mipLevel
,
1003 pRegions
[r
].imageSubresource
.baseArrayLayer
+ slice_array
,
1004 pRegions
[r
].imageOffset
.z
+ slice_3d
,
1005 &img_o
, &img_x
, &img_y
);
1007 VkOffset3D dest_offset_el
= meta_region_offset_el(dest_image
, & pRegions
[r
].imageOffset
);
1008 dest_offset_el
.x
+= img_x
;
1009 dest_offset_el
.y
+= img_y
;
1010 dest_offset_el
.z
= 0;
1012 struct anv_image_view dest_iview
;
1013 anv_image_view_init(&dest_iview
, cmd_buffer
->device
,
1014 &(VkImageViewCreateInfo
) {
1015 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1016 .image
= anv_image_to_handle(dest_image
),
1017 .viewType
= anv_meta_get_view_type(dest_image
),
1018 .format
= image_format
,
1019 .subresourceRange
= {
1020 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1021 .baseMipLevel
= pRegions
[r
].imageSubresource
.mipLevel
,
1023 .baseArrayLayer
= dest_base_array_slice
+
1024 slice_array
+ slice_3d
,
1028 cmd_buffer
, img_o
, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
);
1030 const VkExtent3D img_extent_el
= meta_region_extent_el(dest_image
->vk_format
,
1031 &pRegions
[r
].imageExtent
);
1033 meta_emit_blit(cmd_buffer
,
1036 (VkOffset3D
){0, 0, 0},
1044 /* Once we've done the blit, all of the actual information about
1045 * the image is embedded in the command buffer so we can just
1046 * increment the offset directly in the image effectively
1047 * re-binding it to different backing memory.
1049 src_image
->offset
+= src_image
->extent
.width
*
1050 src_image
->extent
.height
*
1051 src_image
->format
->isl_layout
->bs
;
1053 if (dest_image
->type
== VK_IMAGE_TYPE_3D
)
1059 anv_DestroyImage(vk_device
, anv_image_to_handle(src_image
),
1060 &cmd_buffer
->pool
->alloc
);
1063 meta_finish_blit(cmd_buffer
, &saved_state
);
1066 void anv_CmdCopyImageToBuffer(
1067 VkCommandBuffer commandBuffer
,
1069 VkImageLayout srcImageLayout
,
1070 VkBuffer destBuffer
,
1071 uint32_t regionCount
,
1072 const VkBufferImageCopy
* pRegions
)
1074 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, commandBuffer
);
1075 ANV_FROM_HANDLE(anv_image
, src_image
, srcImage
);
1076 VkDevice vk_device
= anv_device_to_handle(cmd_buffer
->device
);
1077 struct anv_meta_saved_state saved_state
;
1080 /* The Vulkan 1.0 spec says "srcImage must have a sample count equal to
1081 * VK_SAMPLE_COUNT_1_BIT."
1083 assert(src_image
->samples
== 1);
1085 meta_prepare_blit(cmd_buffer
, &saved_state
);
1087 for (unsigned r
= 0; r
< regionCount
; r
++) {
1088 VkImageAspectFlags aspect
= pRegions
[r
].imageSubresource
.aspectMask
;
1090 VkFormat image_format
= choose_iview_format(src_image
, aspect
);
1092 struct anv_image_view src_iview
;
1093 anv_image_view_init(&src_iview
, cmd_buffer
->device
,
1094 &(VkImageViewCreateInfo
) {
1095 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1097 .viewType
= anv_meta_get_view_type(src_image
),
1098 .format
= image_format
,
1099 .subresourceRange
= {
1100 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1101 .baseMipLevel
= pRegions
[r
].imageSubresource
.mipLevel
,
1103 .baseArrayLayer
= pRegions
[r
].imageSubresource
.baseArrayLayer
,
1104 .layerCount
= pRegions
[r
].imageSubresource
.layerCount
,
1107 cmd_buffer
, 0, VK_IMAGE_USAGE_SAMPLED_BIT
);
1109 struct anv_image
*dest_image
=
1110 make_image_for_buffer(vk_device
, destBuffer
, src_image
->vk_format
,
1111 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
,
1112 src_image
->type
, &cmd_buffer
->pool
->alloc
,
1115 unsigned num_slices
;
1116 if (src_image
->type
== VK_IMAGE_TYPE_3D
) {
1117 assert(pRegions
[r
].imageSubresource
.layerCount
== 1);
1118 num_slices
= pRegions
[r
].imageExtent
.depth
;
1120 assert(pRegions
[r
].imageExtent
.depth
== 1);
1121 num_slices
= pRegions
[r
].imageSubresource
.layerCount
;
1124 for (unsigned slice
= 0; slice
< num_slices
; slice
++) {
1125 VkOffset3D src_offset
= pRegions
[r
].imageOffset
;
1126 src_offset
.z
+= slice
;
1128 struct anv_image_view dest_iview
;
1129 anv_image_view_init(&dest_iview
, cmd_buffer
->device
,
1130 &(VkImageViewCreateInfo
) {
1131 .sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
,
1132 .image
= anv_image_to_handle(dest_image
),
1133 .viewType
= VK_IMAGE_VIEW_TYPE_2D
,
1134 .format
= dest_image
->vk_format
,
1135 .subresourceRange
= {
1136 .aspectMask
= VK_IMAGE_ASPECT_COLOR_BIT
,
1139 .baseArrayLayer
= 0,
1143 cmd_buffer
, 0, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
);
1145 meta_emit_blit(cmd_buffer
,
1146 anv_image_from_handle(srcImage
),
1149 pRegions
[r
].imageExtent
,
1152 (VkOffset3D
) { 0, 0, 0 },
1153 pRegions
[r
].imageExtent
,
1156 /* Once we've done the blit, all of the actual information about
1157 * the image is embedded in the command buffer so we can just
1158 * increment the offset directly in the image effectively
1159 * re-binding it to different backing memory.
1161 dest_image
->offset
+= dest_image
->extent
.width
*
1162 dest_image
->extent
.height
*
1163 src_image
->format
->isl_layout
->bs
;
1166 anv_DestroyImage(vk_device
, anv_image_to_handle(dest_image
),
1167 &cmd_buffer
->pool
->alloc
);
1170 meta_finish_blit(cmd_buffer
, &saved_state
);
1174 anv_device_finish_meta_blit_state(struct anv_device
*device
)
1176 anv_DestroyRenderPass(anv_device_to_handle(device
),
1177 device
->meta_state
.blit
.render_pass
,
1178 &device
->meta_state
.alloc
);
1179 anv_DestroyPipeline(anv_device_to_handle(device
),
1180 device
->meta_state
.blit
.pipeline_1d_src
,
1181 &device
->meta_state
.alloc
);
1182 anv_DestroyPipeline(anv_device_to_handle(device
),
1183 device
->meta_state
.blit
.pipeline_2d_src
,
1184 &device
->meta_state
.alloc
);
1185 anv_DestroyPipeline(anv_device_to_handle(device
),
1186 device
->meta_state
.blit
.pipeline_3d_src
,
1187 &device
->meta_state
.alloc
);
1188 anv_DestroyPipelineLayout(anv_device_to_handle(device
),
1189 device
->meta_state
.blit
.pipeline_layout
,
1190 &device
->meta_state
.alloc
);
1191 anv_DestroyDescriptorSetLayout(anv_device_to_handle(device
),
1192 device
->meta_state
.blit
.ds_layout
,
1193 &device
->meta_state
.alloc
);
1197 anv_device_init_meta_blit_state(struct anv_device
*device
)
1201 result
= anv_CreateRenderPass(anv_device_to_handle(device
),
1202 &(VkRenderPassCreateInfo
) {
1203 .sType
= VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO
,
1204 .attachmentCount
= 1,
1205 .pAttachments
= &(VkAttachmentDescription
) {
1206 .format
= VK_FORMAT_UNDEFINED
, /* Our shaders don't care */
1207 .loadOp
= VK_ATTACHMENT_LOAD_OP_LOAD
,
1208 .storeOp
= VK_ATTACHMENT_STORE_OP_STORE
,
1209 .initialLayout
= VK_IMAGE_LAYOUT_GENERAL
,
1210 .finalLayout
= VK_IMAGE_LAYOUT_GENERAL
,
1213 .pSubpasses
= &(VkSubpassDescription
) {
1214 .pipelineBindPoint
= VK_PIPELINE_BIND_POINT_GRAPHICS
,
1215 .inputAttachmentCount
= 0,
1216 .colorAttachmentCount
= 1,
1217 .pColorAttachments
= &(VkAttachmentReference
) {
1219 .layout
= VK_IMAGE_LAYOUT_GENERAL
,
1221 .pResolveAttachments
= NULL
,
1222 .pDepthStencilAttachment
= &(VkAttachmentReference
) {
1223 .attachment
= VK_ATTACHMENT_UNUSED
,
1224 .layout
= VK_IMAGE_LAYOUT_GENERAL
,
1226 .preserveAttachmentCount
= 1,
1227 .pPreserveAttachments
= (uint32_t[]) { 0 },
1229 .dependencyCount
= 0,
1230 }, &device
->meta_state
.alloc
, &device
->meta_state
.blit
.render_pass
);
1231 if (result
!= VK_SUCCESS
)
1234 /* We don't use a vertex shader for blitting, but instead build and pass
1235 * the VUEs directly to the rasterization backend. However, we do need
1236 * to provide GLSL source for the vertex shader so that the compiler
1237 * does not dead-code our inputs.
1239 struct anv_shader_module vs
= {
1240 .nir
= build_nir_vertex_shader(),
1243 struct anv_shader_module fs_1d
= {
1244 .nir
= build_nir_copy_fragment_shader(GLSL_SAMPLER_DIM_1D
),
1247 struct anv_shader_module fs_2d
= {
1248 .nir
= build_nir_copy_fragment_shader(GLSL_SAMPLER_DIM_2D
),
1251 struct anv_shader_module fs_3d
= {
1252 .nir
= build_nir_copy_fragment_shader(GLSL_SAMPLER_DIM_3D
),
1255 VkPipelineVertexInputStateCreateInfo vi_create_info
= {
1256 .sType
= VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO
,
1257 .vertexBindingDescriptionCount
= 2,
1258 .pVertexBindingDescriptions
= (VkVertexInputBindingDescription
[]) {
1262 .inputRate
= VK_VERTEX_INPUT_RATE_VERTEX
1266 .stride
= 5 * sizeof(float),
1267 .inputRate
= VK_VERTEX_INPUT_RATE_VERTEX
1270 .vertexAttributeDescriptionCount
= 3,
1271 .pVertexAttributeDescriptions
= (VkVertexInputAttributeDescription
[]) {
1276 .format
= VK_FORMAT_R32G32B32A32_UINT
,
1283 .format
= VK_FORMAT_R32G32_SFLOAT
,
1287 /* Texture Coordinate */
1290 .format
= VK_FORMAT_R32G32B32_SFLOAT
,
1296 VkDescriptorSetLayoutCreateInfo ds_layout_info
= {
1297 .sType
= VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO
,
1299 .pBindings
= (VkDescriptorSetLayoutBinding
[]) {
1302 .descriptorType
= VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
,
1303 .descriptorCount
= 1,
1304 .stageFlags
= VK_SHADER_STAGE_FRAGMENT_BIT
,
1305 .pImmutableSamplers
= NULL
1309 result
= anv_CreateDescriptorSetLayout(anv_device_to_handle(device
),
1311 &device
->meta_state
.alloc
,
1312 &device
->meta_state
.blit
.ds_layout
);
1313 if (result
!= VK_SUCCESS
)
1314 goto fail_render_pass
;
1316 result
= anv_CreatePipelineLayout(anv_device_to_handle(device
),
1317 &(VkPipelineLayoutCreateInfo
) {
1318 .sType
= VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO
,
1319 .setLayoutCount
= 1,
1320 .pSetLayouts
= &device
->meta_state
.blit
.ds_layout
,
1322 &device
->meta_state
.alloc
, &device
->meta_state
.blit
.pipeline_layout
);
1323 if (result
!= VK_SUCCESS
)
1324 goto fail_descriptor_set_layout
;
1326 VkPipelineShaderStageCreateInfo pipeline_shader_stages
[] = {
1328 .sType
= VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
,
1329 .stage
= VK_SHADER_STAGE_VERTEX_BIT
,
1330 .module
= anv_shader_module_to_handle(&vs
),
1332 .pSpecializationInfo
= NULL
1334 .sType
= VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
,
1335 .stage
= VK_SHADER_STAGE_FRAGMENT_BIT
,
1336 .module
= VK_NULL_HANDLE
, /* TEMPLATE VALUE! FILL ME IN! */
1338 .pSpecializationInfo
= NULL
1342 const VkGraphicsPipelineCreateInfo vk_pipeline_info
= {
1343 .sType
= VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO
,
1344 .stageCount
= ARRAY_SIZE(pipeline_shader_stages
),
1345 .pStages
= pipeline_shader_stages
,
1346 .pVertexInputState
= &vi_create_info
,
1347 .pInputAssemblyState
= &(VkPipelineInputAssemblyStateCreateInfo
) {
1348 .sType
= VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO
,
1349 .topology
= VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP
,
1350 .primitiveRestartEnable
= false,
1352 .pViewportState
= &(VkPipelineViewportStateCreateInfo
) {
1353 .sType
= VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO
,
1357 .pRasterizationState
= &(VkPipelineRasterizationStateCreateInfo
) {
1358 .sType
= VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO
,
1359 .rasterizerDiscardEnable
= false,
1360 .polygonMode
= VK_POLYGON_MODE_FILL
,
1361 .cullMode
= VK_CULL_MODE_NONE
,
1362 .frontFace
= VK_FRONT_FACE_COUNTER_CLOCKWISE
1364 .pMultisampleState
= &(VkPipelineMultisampleStateCreateInfo
) {
1365 .sType
= VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO
,
1366 .rasterizationSamples
= 1,
1367 .sampleShadingEnable
= false,
1368 .pSampleMask
= (VkSampleMask
[]) { UINT32_MAX
},
1370 .pColorBlendState
= &(VkPipelineColorBlendStateCreateInfo
) {
1371 .sType
= VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO
,
1372 .attachmentCount
= 1,
1373 .pAttachments
= (VkPipelineColorBlendAttachmentState
[]) {
1375 VK_COLOR_COMPONENT_A_BIT
|
1376 VK_COLOR_COMPONENT_R_BIT
|
1377 VK_COLOR_COMPONENT_G_BIT
|
1378 VK_COLOR_COMPONENT_B_BIT
},
1381 .pDynamicState
= &(VkPipelineDynamicStateCreateInfo
) {
1382 .sType
= VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO
,
1383 .dynamicStateCount
= 9,
1384 .pDynamicStates
= (VkDynamicState
[]) {
1385 VK_DYNAMIC_STATE_VIEWPORT
,
1386 VK_DYNAMIC_STATE_SCISSOR
,
1387 VK_DYNAMIC_STATE_LINE_WIDTH
,
1388 VK_DYNAMIC_STATE_DEPTH_BIAS
,
1389 VK_DYNAMIC_STATE_BLEND_CONSTANTS
,
1390 VK_DYNAMIC_STATE_DEPTH_BOUNDS
,
1391 VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK
,
1392 VK_DYNAMIC_STATE_STENCIL_WRITE_MASK
,
1393 VK_DYNAMIC_STATE_STENCIL_REFERENCE
,
1397 .layout
= device
->meta_state
.blit
.pipeline_layout
,
1398 .renderPass
= device
->meta_state
.blit
.render_pass
,
1402 const struct anv_graphics_pipeline_create_info anv_pipeline_info
= {
1403 .color_attachment_count
= -1,
1404 .use_repclear
= false,
1405 .disable_viewport
= true,
1406 .disable_scissor
= true,
1408 .use_rectlist
= true
1411 pipeline_shader_stages
[1].module
= anv_shader_module_to_handle(&fs_1d
);
1412 result
= anv_graphics_pipeline_create(anv_device_to_handle(device
),
1414 &vk_pipeline_info
, &anv_pipeline_info
,
1415 &device
->meta_state
.alloc
, &device
->meta_state
.blit
.pipeline_1d_src
);
1416 if (result
!= VK_SUCCESS
)
1417 goto fail_pipeline_layout
;
1419 pipeline_shader_stages
[1].module
= anv_shader_module_to_handle(&fs_2d
);
1420 result
= anv_graphics_pipeline_create(anv_device_to_handle(device
),
1422 &vk_pipeline_info
, &anv_pipeline_info
,
1423 &device
->meta_state
.alloc
, &device
->meta_state
.blit
.pipeline_2d_src
);
1424 if (result
!= VK_SUCCESS
)
1425 goto fail_pipeline_1d
;
1427 pipeline_shader_stages
[1].module
= anv_shader_module_to_handle(&fs_3d
);
1428 result
= anv_graphics_pipeline_create(anv_device_to_handle(device
),
1430 &vk_pipeline_info
, &anv_pipeline_info
,
1431 &device
->meta_state
.alloc
, &device
->meta_state
.blit
.pipeline_3d_src
);
1432 if (result
!= VK_SUCCESS
)
1433 goto fail_pipeline_2d
;
1435 ralloc_free(vs
.nir
);
1436 ralloc_free(fs_1d
.nir
);
1437 ralloc_free(fs_2d
.nir
);
1438 ralloc_free(fs_3d
.nir
);
1443 anv_DestroyPipeline(anv_device_to_handle(device
),
1444 device
->meta_state
.blit
.pipeline_2d_src
,
1445 &device
->meta_state
.alloc
);
1448 anv_DestroyPipeline(anv_device_to_handle(device
),
1449 device
->meta_state
.blit
.pipeline_1d_src
,
1450 &device
->meta_state
.alloc
);
1452 fail_pipeline_layout
:
1453 anv_DestroyPipelineLayout(anv_device_to_handle(device
),
1454 device
->meta_state
.blit
.pipeline_layout
,
1455 &device
->meta_state
.alloc
);
1456 fail_descriptor_set_layout
:
1457 anv_DestroyDescriptorSetLayout(anv_device_to_handle(device
),
1458 device
->meta_state
.blit
.ds_layout
,
1459 &device
->meta_state
.alloc
);
1461 anv_DestroyRenderPass(anv_device_to_handle(device
),
1462 device
->meta_state
.blit
.render_pass
,
1463 &device
->meta_state
.alloc
);
1465 ralloc_free(vs
.nir
);
1466 ralloc_free(fs_1d
.nir
);
1467 ralloc_free(fs_2d
.nir
);
1468 ralloc_free(fs_3d
.nir
);