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));
123 anv_meta_begin_blit2d(struct anv_cmd_buffer *cmd_buffer,
124 struct anv_meta_saved_state *save)
126 meta_prepare_blit(cmd_buffer, save);
130 /* Returns the user-provided VkBufferImageCopy::imageOffset in units of
131 * elements rather than texels. One element equals one texel or one block
132 * if Image is uncompressed or compressed, respectively.
134 static struct VkOffset3D
135 meta_region_offset_el(const struct anv_image * image,
136 const struct VkOffset3D * offset)
138 const struct isl_format_layout * isl_layout = image->format->isl_layout;
139 return (VkOffset3D) {
140 .x = offset->x / isl_layout->bw,
141 .y = offset->y / isl_layout->bh,
142 .z = offset->z / isl_layout->bd,
146 /* Returns the user-provided VkBufferImageCopy::imageExtent in units of
147 * elements rather than texels. One element equals one texel or one block
148 * if Image is uncompressed or compressed, respectively.
150 static struct VkExtent3D
151 meta_region_extent_el(const VkFormat format,
152 const struct VkExtent3D * extent)
154 const struct isl_format_layout * isl_layout =
155 anv_format_for_vk_format(format)->isl_layout;
156 return (VkExtent3D) {
157 .width = DIV_ROUND_UP(extent->width , isl_layout->bw),
158 .height = DIV_ROUND_UP(extent->height, isl_layout->bh),
159 .depth = DIV_ROUND_UP(extent->depth , isl_layout->bd),
164 meta_emit_blit(struct anv_cmd_buffer *cmd_buffer,
165 struct anv_image *src_image,
166 struct anv_image_view *src_iview,
167 VkOffset3D src_offset,
168 VkExtent3D src_extent,
169 struct anv_image *dest_image,
170 struct anv_image_view *dest_iview,
171 VkOffset3D dest_offset,
172 VkExtent3D dest_extent,
173 VkFilter blit_filter)
175 struct anv_device *device = cmd_buffer->device;
177 struct blit_vb_data {
182 assert(src_image->samples == dest_image->samples);
184 unsigned vb_size = sizeof(struct anv_vue_header) + 3 * sizeof(*vb_data);
186 struct anv_state vb_state =
187 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, vb_size, 16);
188 memset(vb_state.map, 0, sizeof(struct anv_vue_header));
189 vb_data = vb_state.map + sizeof(struct anv_vue_header);
191 vb_data[0] = (struct blit_vb_data) {
193 dest_offset.x + dest_extent.width,
194 dest_offset.y + dest_extent.height,
197 (float)(src_offset.x + src_extent.width) / (float)src_iview->extent.width,
198 (float)(src_offset.y + src_extent.height) / (float)src_iview->extent.height,
199 (float)src_offset.z / (float)src_iview->extent.depth,
203 vb_data[1] = (struct blit_vb_data) {
206 dest_offset.y + dest_extent.height,
209 (float)src_offset.x / (float)src_iview->extent.width,
210 (float)(src_offset.y + src_extent.height) / (float)src_iview->extent.height,
211 (float)src_offset.z / (float)src_iview->extent.depth,
215 vb_data[2] = (struct blit_vb_data) {
221 (float)src_offset.x / (float)src_iview->extent.width,
222 (float)src_offset.y / (float)src_iview->extent.height,
223 (float)src_offset.z / (float)src_iview->extent.depth,
227 anv_state_clflush(vb_state);
229 struct anv_buffer vertex_buffer = {
232 .bo = &device->dynamic_state_block_pool.bo,
233 .offset = vb_state.offset,
236 anv_CmdBindVertexBuffers(anv_cmd_buffer_to_handle(cmd_buffer), 0, 2,
238 anv_buffer_to_handle(&vertex_buffer),
239 anv_buffer_to_handle(&vertex_buffer)
243 sizeof(struct anv_vue_header),
247 ANV_CALL(CreateSampler)(anv_device_to_handle(device),
248 &(VkSamplerCreateInfo) {
249 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
250 .magFilter = blit_filter,
251 .minFilter = blit_filter,
252 }, &cmd_buffer->pool->alloc, &sampler);
254 VkDescriptorPool desc_pool;
255 anv_CreateDescriptorPool(anv_device_to_handle(device),
256 &(const VkDescriptorPoolCreateInfo) {
257 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
262 .pPoolSizes = (VkDescriptorPoolSize[]) {
264 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
268 }, &cmd_buffer->pool->alloc, &desc_pool);
271 anv_AllocateDescriptorSets(anv_device_to_handle(device),
272 &(VkDescriptorSetAllocateInfo) {
273 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
274 .descriptorPool = desc_pool,
275 .descriptorSetCount = 1,
276 .pSetLayouts = &device->meta_state.blit.ds_layout
279 anv_UpdateDescriptorSets(anv_device_to_handle(device),
281 (VkWriteDescriptorSet[]) {
283 .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
286 .dstArrayElement = 0,
287 .descriptorCount = 1,
288 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
289 .pImageInfo = (VkDescriptorImageInfo[]) {
292 .imageView = anv_image_view_to_handle(src_iview),
293 .imageLayout = VK_IMAGE_LAYOUT_GENERAL,
300 anv_CreateFramebuffer(anv_device_to_handle(device),
301 &(VkFramebufferCreateInfo) {
302 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
303 .attachmentCount = 1,
304 .pAttachments = (VkImageView[]) {
305 anv_image_view_to_handle(dest_iview),
307 .width = dest_iview->extent.width,
308 .height = dest_iview->extent.height,
310 }, &cmd_buffer->pool->alloc, &fb);
312 ANV_CALL(CmdBeginRenderPass)(anv_cmd_buffer_to_handle(cmd_buffer),
313 &(VkRenderPassBeginInfo) {
314 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
315 .renderPass = device->meta_state.blit.render_pass,
318 .offset = { dest_offset.x, dest_offset.y },
319 .extent = { dest_extent.width, dest_extent.height },
321 .clearValueCount = 0,
322 .pClearValues = NULL,
323 }, VK_SUBPASS_CONTENTS_INLINE);
327 switch (src_image->type) {
328 case VK_IMAGE_TYPE_1D:
329 pipeline = device->meta_state.blit.pipeline_1d_src;
331 case VK_IMAGE_TYPE_2D:
332 pipeline = device->meta_state.blit.pipeline_2d_src;
334 case VK_IMAGE_TYPE_3D:
335 pipeline = device->meta_state.blit.pipeline_3d_src;
338 unreachable(!"bad VkImageType");
341 if (cmd_buffer->state.pipeline != anv_pipeline_from_handle(pipeline)) {
342 anv_CmdBindPipeline(anv_cmd_buffer_to_handle(cmd_buffer),
343 VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
346 anv_CmdSetViewport(anv_cmd_buffer_to_handle(cmd_buffer), 0, 1,
350 .width = dest_iview->extent.width,
351 .height = dest_iview->extent.height,
356 anv_CmdBindDescriptorSets(anv_cmd_buffer_to_handle(cmd_buffer),
357 VK_PIPELINE_BIND_POINT_GRAPHICS,
358 device->meta_state.blit.pipeline_layout, 0, 1,
361 ANV_CALL(CmdDraw)(anv_cmd_buffer_to_handle(cmd_buffer), 3, 1, 0, 0);
363 ANV_CALL(CmdEndRenderPass)(anv_cmd_buffer_to_handle(cmd_buffer));
365 /* At the point where we emit the draw call, all data from the
366 * descriptor sets, etc. has been used. We are free to delete it.
368 anv_DestroyDescriptorPool(anv_device_to_handle(device),
369 desc_pool, &cmd_buffer->pool->alloc);
370 anv_DestroySampler(anv_device_to_handle(device), sampler,
371 &cmd_buffer->pool->alloc);
372 anv_DestroyFramebuffer(anv_device_to_handle(device), fb,
373 &cmd_buffer->pool->alloc);
377 meta_finish_blit(struct anv_cmd_buffer *cmd_buffer,
378 const struct anv_meta_saved_state *saved_state)
380 anv_meta_restore(saved_state, cmd_buffer);
384 anv_meta_end_blit2d(struct anv_cmd_buffer *cmd_buffer,
385 struct anv_meta_saved_state *save)
387 meta_finish_blit(cmd_buffer, save);
391 vk_format_for_size(int bs)
393 /* The choice of UNORM and UINT formats is very intentional here. Most of
394 * the time, we want to use a UINT format to avoid any rounding error in
395 * the blit. For stencil blits, R8_UINT is required by the hardware.
396 * (It's the only format allowed in conjunction with W-tiling.) Also we
397 * intentionally use the 4-channel formats whenever we can. This is so
398 * that, when we do a RGB <-> RGBX copy, the two formats will line up even
399 * though one of them is 3/4 the size of the other. The choice of UNORM
400 * vs. UINT is also very intentional because Haswell doesn't handle 8 or
401 * 16-bit RGB UINT formats at all so we have to use UNORM there.
402 * Fortunately, the only time we should ever use two different formats in
403 * the table below is for RGB -> RGBA blits and so we will never have any
404 * UNORM/UINT mismatch.
407 case 1: return VK_FORMAT_R8_UINT;
408 case 2: return VK_FORMAT_R8G8_UINT;
409 case 3: return VK_FORMAT_R8G8B8_UNORM;
410 case 4: return VK_FORMAT_R8G8B8A8_UNORM;
411 case 6: return VK_FORMAT_R16G16B16_UNORM;
412 case 8: return VK_FORMAT_R16G16B16A16_UNORM;
413 case 12: return VK_FORMAT_R32G32B32_UINT;
414 case 16: return VK_FORMAT_R32G32B32A32_UINT;
416 unreachable("Invalid format block size");
421 anv_meta_blit2d(struct anv_cmd_buffer *cmd_buffer,
422 struct anv_meta_blit2d_surf *src,
423 struct anv_meta_blit2d_surf *dst,
425 struct anv_meta_blit2d_rect *rects)
427 VkDevice vk_device = anv_device_to_handle(cmd_buffer->device);
428 VkFormat src_format = vk_format_for_size(src->bs);
429 VkFormat dst_format = vk_format_for_size(dst->bs);
431 for (unsigned r = 0; r < num_rects; ++r) {
433 /* Create VkImages */
434 VkImageCreateInfo image_info = {
435 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
436 .imageType = VK_IMAGE_TYPE_2D,
437 .format = 0, /* TEMPLATE */
439 .width = 0, /* TEMPLATE */
440 /* Pad to highest tile height to compensate for a vertical intratile offset */
441 .height = MIN(rects[r].height + 64, 1 << 14),
447 .tiling = 0, /* TEMPLATE */
448 .usage = 0, /* TEMPLATE */
450 struct anv_image_create_info anv_image_info = {
451 .vk_info = &image_info,
452 .isl_tiling_flags = 0, /* TEMPLATE */
455 anv_image_info.isl_tiling_flags = 1 << src->tiling;
456 image_info.tiling = anv_image_info.isl_tiling_flags == ISL_TILING_LINEAR_BIT ?
457 VK_IMAGE_TILING_LINEAR : VK_IMAGE_TILING_OPTIMAL;
458 image_info.usage = VK_IMAGE_USAGE_SAMPLED_BIT;
459 image_info.format = src_format,
460 image_info.extent.width = src->pitch / src->bs;
462 anv_image_create(vk_device, &anv_image_info,
463 &cmd_buffer->pool->alloc, &src_image);
465 anv_image_info.isl_tiling_flags = 1 << dst->tiling;
466 image_info.tiling = anv_image_info.isl_tiling_flags == ISL_TILING_LINEAR_BIT ?
467 VK_IMAGE_TILING_LINEAR : VK_IMAGE_TILING_OPTIMAL;
468 image_info.usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
469 image_info.format = dst_format,
470 image_info.extent.width = dst->pitch / dst->bs;
472 anv_image_create(vk_device, &anv_image_info,
473 &cmd_buffer->pool->alloc, &dst_image);
475 /* We could use a vk call to bind memory, but that would require
476 * creating a dummy memory object etc. so there's really no point.
478 anv_image_from_handle(src_image)->bo = src->bo;
479 anv_image_from_handle(src_image)->offset = src->base_offset;
480 anv_image_from_handle(dst_image)->bo = dst->bo;
481 anv_image_from_handle(dst_image)->offset = dst->base_offset;
483 /* Create VkImageViews */
484 VkImageViewCreateInfo iview_info = {
485 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
486 .image = 0, /* TEMPLATE */
487 .viewType = VK_IMAGE_VIEW_TYPE_2D,
488 .format = 0, /* TEMPLATE */
489 .subresourceRange = {
490 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
499 iview_info.image = src_image;
500 iview_info.format = src_format;
501 VkOffset3D src_offset_el = {0};
502 isl_surf_get_image_intratile_offset_el_xy(&cmd_buffer->device->isl_dev,
503 &anv_image_from_handle(src_image)->
508 (uint32_t*)&src_offset_el.x,
509 (uint32_t*)&src_offset_el.y);
511 struct anv_image_view src_iview;
512 anv_image_view_init(&src_iview, cmd_buffer->device,
513 &iview_info, cmd_buffer, img_o, VK_IMAGE_USAGE_SAMPLED_BIT);
515 iview_info.image = dst_image;
516 iview_info.format = dst_format;
517 VkOffset3D dst_offset_el = {0};
518 isl_surf_get_image_intratile_offset_el_xy(&cmd_buffer->device->isl_dev,
519 &anv_image_from_handle(dst_image)->
524 (uint32_t*)&dst_offset_el.x,
525 (uint32_t*)&dst_offset_el.y);
526 struct anv_image_view dst_iview;
527 anv_image_view_init(&dst_iview, cmd_buffer->device,
528 &iview_info, cmd_buffer, img_o, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
531 meta_emit_blit(cmd_buffer,
532 anv_image_from_handle(src_image),
535 (VkExtent3D){rects[r].width, rects[r].height, 1},
536 anv_image_from_handle(dst_image),
539 (VkExtent3D){rects[r].width, rects[r].height, 1},
542 anv_DestroyImage(vk_device, src_image, &cmd_buffer->pool->alloc);
543 anv_DestroyImage(vk_device, dst_image, &cmd_buffer->pool->alloc);
548 do_buffer_copy(struct anv_cmd_buffer *cmd_buffer,
549 struct anv_bo *src, uint64_t src_offset,
550 struct anv_bo *dest, uint64_t dest_offset,
551 int width, int height, int bs)
553 VkDevice vk_device = anv_device_to_handle(cmd_buffer->device);
554 VkFormat copy_format = vk_format_for_size(bs);
556 VkImageCreateInfo image_info = {
557 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
558 .imageType = VK_IMAGE_TYPE_2D,
559 .format = copy_format,
568 .tiling = VK_IMAGE_TILING_LINEAR,
574 image_info.usage = VK_IMAGE_USAGE_SAMPLED_BIT;
575 anv_CreateImage(vk_device, &image_info,
576 &cmd_buffer->pool->alloc, &src_image);
579 image_info.usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
580 anv_CreateImage(vk_device, &image_info,
581 &cmd_buffer->pool->alloc, &dest_image);
583 /* We could use a vk call to bind memory, but that would require
584 * creating a dummy memory object etc. so there's really no point.
586 anv_image_from_handle(src_image)->bo = src;
587 anv_image_from_handle(src_image)->offset = src_offset;
588 anv_image_from_handle(dest_image)->bo = dest;
589 anv_image_from_handle(dest_image)->offset = dest_offset;
591 VkImageViewCreateInfo iview_info = {
592 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
593 .image = 0, /* TEMPLATE */
594 .viewType = VK_IMAGE_VIEW_TYPE_2D,
595 .format = copy_format,
596 .subresourceRange = {
597 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
605 struct anv_image_view src_iview;
606 iview_info.image = src_image;
607 anv_image_view_init(&src_iview, cmd_buffer->device,
608 &iview_info, cmd_buffer, 0, VK_IMAGE_USAGE_SAMPLED_BIT);
610 struct anv_image_view dest_iview;
611 iview_info.image = dest_image;
612 anv_image_view_init(&dest_iview, cmd_buffer->device,
613 &iview_info, cmd_buffer, 0, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
615 meta_emit_blit(cmd_buffer,
616 anv_image_from_handle(src_image),
618 (VkOffset3D) { 0, 0, 0 },
619 (VkExtent3D) { width, height, 1 },
620 anv_image_from_handle(dest_image),
622 (VkOffset3D) { 0, 0, 0 },
623 (VkExtent3D) { width, height, 1 },
626 anv_DestroyImage(vk_device, src_image, &cmd_buffer->pool->alloc);
627 anv_DestroyImage(vk_device, dest_image, &cmd_buffer->pool->alloc);
630 void anv_CmdCopyBuffer(
631 VkCommandBuffer commandBuffer,
634 uint32_t regionCount,
635 const VkBufferCopy* pRegions)
637 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
638 ANV_FROM_HANDLE(anv_buffer, src_buffer, srcBuffer);
639 ANV_FROM_HANDLE(anv_buffer, dest_buffer, destBuffer);
641 struct anv_meta_saved_state saved_state;
643 meta_prepare_blit(cmd_buffer, &saved_state);
645 for (unsigned r = 0; r < regionCount; r++) {
646 uint64_t src_offset = src_buffer->offset + pRegions[r].srcOffset;
647 uint64_t dest_offset = dest_buffer->offset + pRegions[r].dstOffset;
648 uint64_t copy_size = pRegions[r].size;
650 /* First, we compute the biggest format that can be used with the
651 * given offsets and size.
655 int fs = ffs(src_offset) - 1;
657 bs = MIN2(bs, 1 << fs);
658 assert(src_offset % bs == 0);
660 fs = ffs(dest_offset) - 1;
662 bs = MIN2(bs, 1 << fs);
663 assert(dest_offset % bs == 0);
665 fs = ffs(pRegions[r].size) - 1;
667 bs = MIN2(bs, 1 << fs);
668 assert(pRegions[r].size % bs == 0);
670 /* This is maximum possible width/height our HW can handle */
671 uint64_t max_surface_dim = 1 << 14;
673 /* First, we make a bunch of max-sized copies */
674 uint64_t max_copy_size = max_surface_dim * max_surface_dim * bs;
675 while (copy_size >= max_copy_size) {
676 do_buffer_copy(cmd_buffer, src_buffer->bo, src_offset,
677 dest_buffer->bo, dest_offset,
678 max_surface_dim, max_surface_dim, bs);
679 copy_size -= max_copy_size;
680 src_offset += max_copy_size;
681 dest_offset += max_copy_size;
684 uint64_t height = copy_size / (max_surface_dim * bs);
685 assert(height < max_surface_dim);
687 uint64_t rect_copy_size = height * max_surface_dim * bs;
688 do_buffer_copy(cmd_buffer, src_buffer->bo, src_offset,
689 dest_buffer->bo, dest_offset,
690 max_surface_dim, height, bs);
691 copy_size -= rect_copy_size;
692 src_offset += rect_copy_size;
693 dest_offset += rect_copy_size;
696 if (copy_size != 0) {
697 do_buffer_copy(cmd_buffer, src_buffer->bo, src_offset,
698 dest_buffer->bo, dest_offset,
699 copy_size / bs, 1, bs);
703 meta_finish_blit(cmd_buffer, &saved_state);
706 void anv_CmdUpdateBuffer(
707 VkCommandBuffer commandBuffer,
709 VkDeviceSize dstOffset,
710 VkDeviceSize dataSize,
711 const uint32_t* pData)
713 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
714 ANV_FROM_HANDLE(anv_buffer, dst_buffer, dstBuffer);
715 struct anv_meta_saved_state saved_state;
717 meta_prepare_blit(cmd_buffer, &saved_state);
719 /* We can't quite grab a full block because the state stream needs a
720 * little data at the top to build its linked list.
722 const uint32_t max_update_size =
723 cmd_buffer->device->dynamic_state_block_pool.block_size - 64;
725 assert(max_update_size < (1 << 14) * 4);
728 const uint32_t copy_size = MIN2(dataSize, max_update_size);
730 struct anv_state tmp_data =
731 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, copy_size, 64);
733 memcpy(tmp_data.map, pData, copy_size);
736 if ((copy_size & 15) == 0 && (dstOffset & 15) == 0) {
738 } else if ((copy_size & 7) == 0 && (dstOffset & 7) == 0) {
741 assert((copy_size & 3) == 0 && (dstOffset & 3) == 0);
745 do_buffer_copy(cmd_buffer,
746 &cmd_buffer->device->dynamic_state_block_pool.bo,
748 dst_buffer->bo, dst_buffer->offset + dstOffset,
749 copy_size / bs, 1, bs);
751 dataSize -= copy_size;
752 dstOffset += copy_size;
753 pData = (void *)pData + copy_size;
756 meta_finish_blit(cmd_buffer, &saved_state);
760 choose_iview_format(struct anv_image *image, VkImageAspectFlagBits aspect)
762 assert(__builtin_popcount(aspect) == 1);
764 struct isl_surf *surf =
765 &anv_image_get_surface_for_aspect_mask(image, aspect)->isl;
767 /* vkCmdCopyImage behaves like memcpy. Therefore we choose identical UINT
768 * formats for the source and destination image views.
770 * From the Vulkan spec (2015-12-30):
772 * vkCmdCopyImage performs image copies in a similar manner to a host
773 * memcpy. It does not perform general-purpose conversions such as
774 * scaling, resizing, blending, color-space conversion, or format
775 * conversions. Rather, it simply copies raw image data. vkCmdCopyImage
776 * can copy between images with different formats, provided the formats
777 * are compatible as defined below.
779 * [The spec later defines compatibility as having the same number of
782 return vk_format_for_size(isl_format_layouts[surf->format].bs);
786 choose_buffer_format(VkFormat format, VkImageAspectFlagBits aspect)
788 assert(__builtin_popcount(aspect) == 1);
790 /* vkCmdCopy* commands behave like memcpy. Therefore we choose
791 * compatable UINT formats for the source and destination image views.
793 * For the buffer, we go back to the original image format and get a
794 * the format as if it were linear. This way, for RGB formats, we get
795 * an RGB format here even if the tiled image is RGBA. XXX: This doesn't
796 * work if the buffer is the destination.
798 enum isl_format linear_format = anv_get_isl_format(format, aspect,
799 VK_IMAGE_TILING_LINEAR,
802 return vk_format_for_size(isl_format_layouts[linear_format].bs);
805 void anv_CmdCopyImage(
806 VkCommandBuffer commandBuffer,
808 VkImageLayout srcImageLayout,
810 VkImageLayout destImageLayout,
811 uint32_t regionCount,
812 const VkImageCopy* pRegions)
814 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
815 ANV_FROM_HANDLE(anv_image, src_image, srcImage);
816 ANV_FROM_HANDLE(anv_image, dest_image, destImage);
817 struct anv_meta_saved_state saved_state;
819 /* From the Vulkan 1.0 spec:
821 * vkCmdCopyImage can be used to copy image data between multisample
822 * images, but both images must have the same number of samples.
824 assert(src_image->samples == dest_image->samples);
826 meta_prepare_blit(cmd_buffer, &saved_state);
828 for (unsigned r = 0; r < regionCount; r++) {
829 assert(pRegions[r].srcSubresource.aspectMask ==
830 pRegions[r].dstSubresource.aspectMask);
832 VkImageAspectFlags aspect = pRegions[r].srcSubresource.aspectMask;
834 VkFormat src_format = choose_iview_format(src_image, aspect);
835 VkFormat dst_format = choose_iview_format(dest_image, aspect);
837 struct anv_image_view src_iview;
838 anv_image_view_init(&src_iview, cmd_buffer->device,
839 &(VkImageViewCreateInfo) {
840 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
842 .viewType = anv_meta_get_view_type(src_image),
843 .format = src_format,
844 .subresourceRange = {
845 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
846 .baseMipLevel = pRegions[r].srcSubresource.mipLevel,
848 .baseArrayLayer = pRegions[r].srcSubresource.baseArrayLayer,
849 .layerCount = pRegions[r].dstSubresource.layerCount,
852 cmd_buffer, 0, VK_IMAGE_USAGE_SAMPLED_BIT);
854 const uint32_t dest_base_array_slice =
855 anv_meta_get_iview_layer(dest_image, &pRegions[r].dstSubresource,
856 &pRegions[r].dstOffset);
859 unsigned num_slices_3d = pRegions[r].extent.depth;
860 unsigned num_slices_array = pRegions[r].dstSubresource.layerCount;
861 unsigned slice_3d = 0;
862 unsigned slice_array = 0;
863 while (slice_3d < num_slices_3d && slice_array < num_slices_array) {
864 VkOffset3D src_offset = pRegions[r].srcOffset;
865 src_offset.z += slice_3d + slice_array;
870 if (isl_format_is_compressed(dest_image->format->isl_format))
871 isl_surf_get_image_intratile_offset_el(&cmd_buffer->device->isl_dev,
872 &dest_image->color_surface.isl,
873 pRegions[r].dstSubresource.mipLevel,
874 pRegions[r].dstSubresource.baseArrayLayer + slice_array,
875 pRegions[r].dstOffset.z + slice_3d,
876 &img_o, &img_x, &img_y);
878 VkOffset3D dest_offset_el = meta_region_offset_el(dest_image, &pRegions[r].dstOffset);
879 dest_offset_el.x += img_x;
880 dest_offset_el.y += img_y;
881 dest_offset_el.z = 0;
883 struct anv_image_view dest_iview;
884 anv_image_view_init(&dest_iview, cmd_buffer->device,
885 &(VkImageViewCreateInfo) {
886 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
888 .viewType = anv_meta_get_view_type(dest_image),
889 .format = dst_format,
890 .subresourceRange = {
891 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
892 .baseMipLevel = pRegions[r].dstSubresource.mipLevel,
894 .baseArrayLayer = dest_base_array_slice +
895 slice_array + slice_3d,
899 cmd_buffer, img_o, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
901 const VkExtent3D img_extent_el = meta_region_extent_el(dest_image->vk_format,
902 &pRegions[r].extent);
904 meta_emit_blit(cmd_buffer,
905 src_image, &src_iview,
908 dest_image, &dest_iview,
913 if (dest_image->type == VK_IMAGE_TYPE_3D)
920 meta_finish_blit(cmd_buffer, &saved_state);
923 void anv_CmdBlitImage(
924 VkCommandBuffer commandBuffer,
926 VkImageLayout srcImageLayout,
928 VkImageLayout destImageLayout,
929 uint32_t regionCount,
930 const VkImageBlit* pRegions,
934 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
935 ANV_FROM_HANDLE(anv_image, src_image, srcImage);
936 ANV_FROM_HANDLE(anv_image, dest_image, destImage);
937 struct anv_meta_saved_state saved_state;
939 /* From the Vulkan 1.0 spec:
941 * vkCmdBlitImage must not be used for multisampled source or
942 * destination images. Use vkCmdResolveImage for this purpose.
944 assert(src_image->samples == 1);
945 assert(dest_image->samples == 1);
947 anv_finishme("respect VkFilter");
949 meta_prepare_blit(cmd_buffer, &saved_state);
951 for (unsigned r = 0; r < regionCount; r++) {
952 struct anv_image_view src_iview;
953 anv_image_view_init(&src_iview, cmd_buffer->device,
954 &(VkImageViewCreateInfo) {
955 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
957 .viewType = anv_meta_get_view_type(src_image),
958 .format = src_image->vk_format,
959 .subresourceRange = {
960 .aspectMask = pRegions[r].srcSubresource.aspectMask,
961 .baseMipLevel = pRegions[r].srcSubresource.mipLevel,
963 .baseArrayLayer = pRegions[r].srcSubresource.baseArrayLayer,
967 cmd_buffer, 0, VK_IMAGE_USAGE_SAMPLED_BIT);
969 const VkOffset3D dest_offset = {
970 .x = pRegions[r].dstOffsets[0].x,
971 .y = pRegions[r].dstOffsets[0].y,
975 if (pRegions[r].dstOffsets[1].x < pRegions[r].dstOffsets[0].x ||
976 pRegions[r].dstOffsets[1].y < pRegions[r].dstOffsets[0].y ||
977 pRegions[r].srcOffsets[1].x < pRegions[r].srcOffsets[0].x ||
978 pRegions[r].srcOffsets[1].y < pRegions[r].srcOffsets[0].y)
979 anv_finishme("FINISHME: Allow flipping in blits");
981 const VkExtent3D dest_extent = {
982 .width = pRegions[r].dstOffsets[1].x - pRegions[r].dstOffsets[0].x,
983 .height = pRegions[r].dstOffsets[1].y - pRegions[r].dstOffsets[0].y,
986 const VkExtent3D src_extent = {
987 .width = pRegions[r].srcOffsets[1].x - pRegions[r].srcOffsets[0].x,
988 .height = pRegions[r].srcOffsets[1].y - pRegions[r].srcOffsets[0].y,
991 const uint32_t dest_array_slice =
992 anv_meta_get_iview_layer(dest_image, &pRegions[r].dstSubresource,
993 &pRegions[r].dstOffsets[0]);
995 if (pRegions[r].srcSubresource.layerCount > 1)
996 anv_finishme("FINISHME: copy multiple array layers");
998 if (pRegions[r].srcOffsets[0].z + 1 != pRegions[r].srcOffsets[1].z ||
999 pRegions[r].dstOffsets[0].z + 1 != pRegions[r].dstOffsets[1].z)
1000 anv_finishme("FINISHME: copy multiple depth layers");
1002 struct anv_image_view dest_iview;
1003 anv_image_view_init(&dest_iview, cmd_buffer->device,
1004 &(VkImageViewCreateInfo) {
1005 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
1007 .viewType = anv_meta_get_view_type(dest_image),
1008 .format = dest_image->vk_format,
1009 .subresourceRange = {
1010 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
1011 .baseMipLevel = pRegions[r].dstSubresource.mipLevel,
1013 .baseArrayLayer = dest_array_slice,
1017 cmd_buffer, 0, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
1019 meta_emit_blit(cmd_buffer,
1020 src_image, &src_iview,
1021 pRegions[r].srcOffsets[0], src_extent,
1022 dest_image, &dest_iview,
1023 dest_offset, dest_extent,
1027 meta_finish_blit(cmd_buffer, &saved_state);
1030 static struct anv_image *
1031 make_image_for_buffer(VkDevice vk_device, VkBuffer vk_buffer, VkFormat format,
1032 VkImageUsageFlags usage,
1033 VkImageType image_type,
1034 const VkAllocationCallbacks *alloc,
1035 const VkBufferImageCopy *copy)
1037 ANV_FROM_HANDLE(anv_buffer, buffer, vk_buffer);
1039 VkExtent3D extent = copy->imageExtent;
1040 if (copy->bufferRowLength)
1041 extent.width = copy->bufferRowLength;
1042 if (copy->bufferImageHeight)
1043 extent.height = copy->bufferImageHeight;
1045 extent = meta_region_extent_el(format, &extent);
1047 VkImageAspectFlags aspect = copy->imageSubresource.aspectMask;
1048 VkFormat buffer_format = choose_buffer_format(format, aspect);
1051 VkResult result = anv_CreateImage(vk_device,
1052 &(VkImageCreateInfo) {
1053 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
1054 .imageType = VK_IMAGE_TYPE_2D,
1055 .format = buffer_format,
1060 .tiling = VK_IMAGE_TILING_LINEAR,
1063 }, alloc, &vk_image);
1064 assert(result == VK_SUCCESS);
1066 ANV_FROM_HANDLE(anv_image, image, vk_image);
1068 /* We could use a vk call to bind memory, but that would require
1069 * creating a dummy memory object etc. so there's really no point.
1071 image->bo = buffer->bo;
1072 image->offset = buffer->offset + copy->bufferOffset;
1077 void anv_CmdCopyBufferToImage(
1078 VkCommandBuffer commandBuffer,
1081 VkImageLayout destImageLayout,
1082 uint32_t regionCount,
1083 const VkBufferImageCopy* pRegions)
1085 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
1086 ANV_FROM_HANDLE(anv_image, dest_image, destImage);
1087 VkDevice vk_device = anv_device_to_handle(cmd_buffer->device);
1088 struct anv_meta_saved_state saved_state;
1090 /* The Vulkan 1.0 spec says "dstImage must have a sample count equal to
1091 * VK_SAMPLE_COUNT_1_BIT."
1093 assert(dest_image->samples == 1);
1095 meta_prepare_blit(cmd_buffer, &saved_state);
1097 for (unsigned r = 0; r < regionCount; r++) {
1098 VkImageAspectFlags aspect = pRegions[r].imageSubresource.aspectMask;
1100 VkFormat image_format = choose_iview_format(dest_image, aspect);
1102 struct anv_image *src_image =
1103 make_image_for_buffer(vk_device, srcBuffer, dest_image->vk_format,
1104 VK_IMAGE_USAGE_SAMPLED_BIT,
1105 dest_image->type, &cmd_buffer->pool->alloc,
1108 const uint32_t dest_base_array_slice =
1109 anv_meta_get_iview_layer(dest_image, &pRegions[r].imageSubresource,
1110 &pRegions[r].imageOffset);
1112 unsigned num_slices_3d = pRegions[r].imageExtent.depth;
1113 unsigned num_slices_array = pRegions[r].imageSubresource.layerCount;
1114 unsigned slice_3d = 0;
1115 unsigned slice_array = 0;
1116 while (slice_3d < num_slices_3d && slice_array < num_slices_array) {
1117 struct anv_image_view src_iview;
1118 anv_image_view_init(&src_iview, cmd_buffer->device,
1119 &(VkImageViewCreateInfo) {
1120 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
1121 .image = anv_image_to_handle(src_image),
1122 .viewType = VK_IMAGE_VIEW_TYPE_2D,
1123 .format = src_image->vk_format,
1124 .subresourceRange = {
1125 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
1128 .baseArrayLayer = 0,
1132 cmd_buffer, 0, VK_IMAGE_USAGE_SAMPLED_BIT);
1137 if (isl_format_is_compressed(dest_image->format->isl_format))
1138 isl_surf_get_image_intratile_offset_el(&cmd_buffer->device->isl_dev,
1139 &dest_image->color_surface.isl,
1140 pRegions[r].imageSubresource.mipLevel,
1141 pRegions[r].imageSubresource.baseArrayLayer + slice_array,
1142 pRegions[r].imageOffset.z + slice_3d,
1143 &img_o, &img_x, &img_y);
1145 VkOffset3D dest_offset_el = meta_region_offset_el(dest_image, & pRegions[r].imageOffset);
1146 dest_offset_el.x += img_x;
1147 dest_offset_el.y += img_y;
1148 dest_offset_el.z = 0;
1150 struct anv_image_view dest_iview;
1151 anv_image_view_init(&dest_iview, cmd_buffer->device,
1152 &(VkImageViewCreateInfo) {
1153 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
1154 .image = anv_image_to_handle(dest_image),
1155 .viewType = anv_meta_get_view_type(dest_image),
1156 .format = image_format,
1157 .subresourceRange = {
1158 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
1159 .baseMipLevel = pRegions[r].imageSubresource.mipLevel,
1161 .baseArrayLayer = dest_base_array_slice +
1162 slice_array + slice_3d,
1166 cmd_buffer, img_o, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
1168 const VkExtent3D img_extent_el = meta_region_extent_el(dest_image->vk_format,
1169 &pRegions[r].imageExtent);
1171 meta_emit_blit(cmd_buffer,
1174 (VkOffset3D){0, 0, 0},
1182 /* Once we've done the blit, all of the actual information about
1183 * the image is embedded in the command buffer so we can just
1184 * increment the offset directly in the image effectively
1185 * re-binding it to different backing memory.
1187 src_image->offset += src_image->extent.width *
1188 src_image->extent.height *
1189 src_image->format->isl_layout->bs;
1191 if (dest_image->type == VK_IMAGE_TYPE_3D)
1197 anv_DestroyImage(vk_device, anv_image_to_handle(src_image),
1198 &cmd_buffer->pool->alloc);
1201 meta_finish_blit(cmd_buffer, &saved_state);
1204 void anv_CmdCopyImageToBuffer(
1205 VkCommandBuffer commandBuffer,
1207 VkImageLayout srcImageLayout,
1208 VkBuffer destBuffer,
1209 uint32_t regionCount,
1210 const VkBufferImageCopy* pRegions)
1212 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
1213 ANV_FROM_HANDLE(anv_image, src_image, srcImage);
1214 VkDevice vk_device = anv_device_to_handle(cmd_buffer->device);
1215 struct anv_meta_saved_state saved_state;
1218 /* The Vulkan 1.0 spec says "srcImage must have a sample count equal to
1219 * VK_SAMPLE_COUNT_1_BIT."
1221 assert(src_image->samples == 1);
1223 meta_prepare_blit(cmd_buffer, &saved_state);
1225 for (unsigned r = 0; r < regionCount; r++) {
1226 VkImageAspectFlags aspect = pRegions[r].imageSubresource.aspectMask;
1228 VkFormat image_format = choose_iview_format(src_image, aspect);
1230 struct anv_image_view src_iview;
1231 anv_image_view_init(&src_iview, cmd_buffer->device,
1232 &(VkImageViewCreateInfo) {
1233 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
1235 .viewType = anv_meta_get_view_type(src_image),
1236 .format = image_format,
1237 .subresourceRange = {
1238 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
1239 .baseMipLevel = pRegions[r].imageSubresource.mipLevel,
1241 .baseArrayLayer = pRegions[r].imageSubresource.baseArrayLayer,
1242 .layerCount = pRegions[r].imageSubresource.layerCount,
1245 cmd_buffer, 0, VK_IMAGE_USAGE_SAMPLED_BIT);
1247 struct anv_image *dest_image =
1248 make_image_for_buffer(vk_device, destBuffer, src_image->vk_format,
1249 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
1250 src_image->type, &cmd_buffer->pool->alloc,
1253 unsigned num_slices;
1254 if (src_image->type == VK_IMAGE_TYPE_3D) {
1255 assert(pRegions[r].imageSubresource.layerCount == 1);
1256 num_slices = pRegions[r].imageExtent.depth;
1258 assert(pRegions[r].imageExtent.depth == 1);
1259 num_slices = pRegions[r].imageSubresource.layerCount;
1262 for (unsigned slice = 0; slice < num_slices; slice++) {
1263 VkOffset3D src_offset = pRegions[r].imageOffset;
1264 src_offset.z += slice;
1266 struct anv_image_view dest_iview;
1267 anv_image_view_init(&dest_iview, cmd_buffer->device,
1268 &(VkImageViewCreateInfo) {
1269 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
1270 .image = anv_image_to_handle(dest_image),
1271 .viewType = VK_IMAGE_VIEW_TYPE_2D,
1272 .format = dest_image->vk_format,
1273 .subresourceRange = {
1274 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
1277 .baseArrayLayer = 0,
1281 cmd_buffer, 0, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
1283 meta_emit_blit(cmd_buffer,
1284 anv_image_from_handle(srcImage),
1287 pRegions[r].imageExtent,
1290 (VkOffset3D) { 0, 0, 0 },
1291 pRegions[r].imageExtent,
1294 /* Once we've done the blit, all of the actual information about
1295 * the image is embedded in the command buffer so we can just
1296 * increment the offset directly in the image effectively
1297 * re-binding it to different backing memory.
1299 dest_image->offset += dest_image->extent.width *
1300 dest_image->extent.height *
1301 src_image->format->isl_layout->bs;
1304 anv_DestroyImage(vk_device, anv_image_to_handle(dest_image),
1305 &cmd_buffer->pool->alloc);
1308 meta_finish_blit(cmd_buffer, &saved_state);
1312 anv_device_finish_meta_blit_state(struct anv_device *device)
1314 anv_DestroyRenderPass(anv_device_to_handle(device),
1315 device->meta_state.blit.render_pass,
1316 &device->meta_state.alloc);
1317 anv_DestroyPipeline(anv_device_to_handle(device),
1318 device->meta_state.blit.pipeline_1d_src,
1319 &device->meta_state.alloc);
1320 anv_DestroyPipeline(anv_device_to_handle(device),
1321 device->meta_state.blit.pipeline_2d_src,
1322 &device->meta_state.alloc);
1323 anv_DestroyPipeline(anv_device_to_handle(device),
1324 device->meta_state.blit.pipeline_3d_src,
1325 &device->meta_state.alloc);
1326 anv_DestroyPipelineLayout(anv_device_to_handle(device),
1327 device->meta_state.blit.pipeline_layout,
1328 &device->meta_state.alloc);
1329 anv_DestroyDescriptorSetLayout(anv_device_to_handle(device),
1330 device->meta_state.blit.ds_layout,
1331 &device->meta_state.alloc);
1335 anv_device_init_meta_blit_state(struct anv_device *device)
1339 result = anv_CreateRenderPass(anv_device_to_handle(device),
1340 &(VkRenderPassCreateInfo) {
1341 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1342 .attachmentCount = 1,
1343 .pAttachments = &(VkAttachmentDescription) {
1344 .format = VK_FORMAT_UNDEFINED, /* Our shaders don't care */
1345 .loadOp = VK_ATTACHMENT_LOAD_OP_LOAD,
1346 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1347 .initialLayout = VK_IMAGE_LAYOUT_GENERAL,
1348 .finalLayout = VK_IMAGE_LAYOUT_GENERAL,
1351 .pSubpasses = &(VkSubpassDescription) {
1352 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1353 .inputAttachmentCount = 0,
1354 .colorAttachmentCount = 1,
1355 .pColorAttachments = &(VkAttachmentReference) {
1357 .layout = VK_IMAGE_LAYOUT_GENERAL,
1359 .pResolveAttachments = NULL,
1360 .pDepthStencilAttachment = &(VkAttachmentReference) {
1361 .attachment = VK_ATTACHMENT_UNUSED,
1362 .layout = VK_IMAGE_LAYOUT_GENERAL,
1364 .preserveAttachmentCount = 1,
1365 .pPreserveAttachments = (uint32_t[]) { 0 },
1367 .dependencyCount = 0,
1368 }, &device->meta_state.alloc, &device->meta_state.blit.render_pass);
1369 if (result != VK_SUCCESS)
1372 /* We don't use a vertex shader for blitting, but instead build and pass
1373 * the VUEs directly to the rasterization backend. However, we do need
1374 * to provide GLSL source for the vertex shader so that the compiler
1375 * does not dead-code our inputs.
1377 struct anv_shader_module vs = {
1378 .nir = build_nir_vertex_shader(),
1381 struct anv_shader_module fs_1d = {
1382 .nir = build_nir_copy_fragment_shader(GLSL_SAMPLER_DIM_1D),
1385 struct anv_shader_module fs_2d = {
1386 .nir = build_nir_copy_fragment_shader(GLSL_SAMPLER_DIM_2D),
1389 struct anv_shader_module fs_3d = {
1390 .nir = build_nir_copy_fragment_shader(GLSL_SAMPLER_DIM_3D),
1393 VkPipelineVertexInputStateCreateInfo vi_create_info = {
1394 .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
1395 .vertexBindingDescriptionCount = 2,
1396 .pVertexBindingDescriptions = (VkVertexInputBindingDescription[]) {
1400 .inputRate = VK_VERTEX_INPUT_RATE_VERTEX
1404 .stride = 5 * sizeof(float),
1405 .inputRate = VK_VERTEX_INPUT_RATE_VERTEX
1408 .vertexAttributeDescriptionCount = 3,
1409 .pVertexAttributeDescriptions = (VkVertexInputAttributeDescription[]) {
1414 .format = VK_FORMAT_R32G32B32A32_UINT,
1421 .format = VK_FORMAT_R32G32_SFLOAT,
1425 /* Texture Coordinate */
1428 .format = VK_FORMAT_R32G32B32_SFLOAT,
1434 VkDescriptorSetLayoutCreateInfo ds_layout_info = {
1435 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
1437 .pBindings = (VkDescriptorSetLayoutBinding[]) {
1440 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1441 .descriptorCount = 1,
1442 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1443 .pImmutableSamplers = NULL
1447 result = anv_CreateDescriptorSetLayout(anv_device_to_handle(device),
1449 &device->meta_state.alloc,
1450 &device->meta_state.blit.ds_layout);
1451 if (result != VK_SUCCESS)
1452 goto fail_render_pass;
1454 result = anv_CreatePipelineLayout(anv_device_to_handle(device),
1455 &(VkPipelineLayoutCreateInfo) {
1456 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1457 .setLayoutCount = 1,
1458 .pSetLayouts = &device->meta_state.blit.ds_layout,
1460 &device->meta_state.alloc, &device->meta_state.blit.pipeline_layout);
1461 if (result != VK_SUCCESS)
1462 goto fail_descriptor_set_layout;
1464 VkPipelineShaderStageCreateInfo pipeline_shader_stages[] = {
1466 .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
1467 .stage = VK_SHADER_STAGE_VERTEX_BIT,
1468 .module = anv_shader_module_to_handle(&vs),
1470 .pSpecializationInfo = NULL
1472 .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
1473 .stage = VK_SHADER_STAGE_FRAGMENT_BIT,
1474 .module = VK_NULL_HANDLE, /* TEMPLATE VALUE! FILL ME IN! */
1476 .pSpecializationInfo = NULL
1480 const VkGraphicsPipelineCreateInfo vk_pipeline_info = {
1481 .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
1482 .stageCount = ARRAY_SIZE(pipeline_shader_stages),
1483 .pStages = pipeline_shader_stages,
1484 .pVertexInputState = &vi_create_info,
1485 .pInputAssemblyState = &(VkPipelineInputAssemblyStateCreateInfo) {
1486 .sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
1487 .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
1488 .primitiveRestartEnable = false,
1490 .pViewportState = &(VkPipelineViewportStateCreateInfo) {
1491 .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
1495 .pRasterizationState = &(VkPipelineRasterizationStateCreateInfo) {
1496 .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
1497 .rasterizerDiscardEnable = false,
1498 .polygonMode = VK_POLYGON_MODE_FILL,
1499 .cullMode = VK_CULL_MODE_NONE,
1500 .frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE
1502 .pMultisampleState = &(VkPipelineMultisampleStateCreateInfo) {
1503 .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
1504 .rasterizationSamples = 1,
1505 .sampleShadingEnable = false,
1506 .pSampleMask = (VkSampleMask[]) { UINT32_MAX },
1508 .pColorBlendState = &(VkPipelineColorBlendStateCreateInfo) {
1509 .sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
1510 .attachmentCount = 1,
1511 .pAttachments = (VkPipelineColorBlendAttachmentState []) {
1513 VK_COLOR_COMPONENT_A_BIT |
1514 VK_COLOR_COMPONENT_R_BIT |
1515 VK_COLOR_COMPONENT_G_BIT |
1516 VK_COLOR_COMPONENT_B_BIT },
1519 .pDynamicState = &(VkPipelineDynamicStateCreateInfo) {
1520 .sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
1521 .dynamicStateCount = 9,
1522 .pDynamicStates = (VkDynamicState[]) {
1523 VK_DYNAMIC_STATE_VIEWPORT,
1524 VK_DYNAMIC_STATE_SCISSOR,
1525 VK_DYNAMIC_STATE_LINE_WIDTH,
1526 VK_DYNAMIC_STATE_DEPTH_BIAS,
1527 VK_DYNAMIC_STATE_BLEND_CONSTANTS,
1528 VK_DYNAMIC_STATE_DEPTH_BOUNDS,
1529 VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK,
1530 VK_DYNAMIC_STATE_STENCIL_WRITE_MASK,
1531 VK_DYNAMIC_STATE_STENCIL_REFERENCE,
1535 .layout = device->meta_state.blit.pipeline_layout,
1536 .renderPass = device->meta_state.blit.render_pass,
1540 const struct anv_graphics_pipeline_create_info anv_pipeline_info = {
1541 .color_attachment_count = -1,
1542 .use_repclear = false,
1543 .disable_viewport = true,
1544 .disable_scissor = true,
1546 .use_rectlist = true
1549 pipeline_shader_stages[1].module = anv_shader_module_to_handle(&fs_1d);
1550 result = anv_graphics_pipeline_create(anv_device_to_handle(device),
1552 &vk_pipeline_info, &anv_pipeline_info,
1553 &device->meta_state.alloc, &device->meta_state.blit.pipeline_1d_src);
1554 if (result != VK_SUCCESS)
1555 goto fail_pipeline_layout;
1557 pipeline_shader_stages[1].module = anv_shader_module_to_handle(&fs_2d);
1558 result = anv_graphics_pipeline_create(anv_device_to_handle(device),
1560 &vk_pipeline_info, &anv_pipeline_info,
1561 &device->meta_state.alloc, &device->meta_state.blit.pipeline_2d_src);
1562 if (result != VK_SUCCESS)
1563 goto fail_pipeline_1d;
1565 pipeline_shader_stages[1].module = anv_shader_module_to_handle(&fs_3d);
1566 result = anv_graphics_pipeline_create(anv_device_to_handle(device),
1568 &vk_pipeline_info, &anv_pipeline_info,
1569 &device->meta_state.alloc, &device->meta_state.blit.pipeline_3d_src);
1570 if (result != VK_SUCCESS)
1571 goto fail_pipeline_2d;
1573 ralloc_free(vs.nir);
1574 ralloc_free(fs_1d.nir);
1575 ralloc_free(fs_2d.nir);
1576 ralloc_free(fs_3d.nir);
1581 anv_DestroyPipeline(anv_device_to_handle(device),
1582 device->meta_state.blit.pipeline_2d_src,
1583 &device->meta_state.alloc);
1586 anv_DestroyPipeline(anv_device_to_handle(device),
1587 device->meta_state.blit.pipeline_1d_src,
1588 &device->meta_state.alloc);
1590 fail_pipeline_layout:
1591 anv_DestroyPipelineLayout(anv_device_to_handle(device),
1592 device->meta_state.blit.pipeline_layout,
1593 &device->meta_state.alloc);
1594 fail_descriptor_set_layout:
1595 anv_DestroyDescriptorSetLayout(anv_device_to_handle(device),
1596 device->meta_state.blit.ds_layout,
1597 &device->meta_state.alloc);
1599 anv_DestroyRenderPass(anv_device_to_handle(device),
1600 device->meta_state.blit.render_pass,
1601 &device->meta_state.alloc);
1603 ralloc_free(vs.nir);
1604 ralloc_free(fs_1d.nir);
1605 ralloc_free(fs_2d.nir);
1606 ralloc_free(fs_3d.nir);