OSDN Git Service

Merge remote-tracking branch 'origin/master' into vulkan
[android-x86/external-mesa.git] / src / intel / vulkan / anv_meta_blit.c
1 /*
2  * Copyright © 2015 Intel Corporation
3  *
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:
10  *
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
13  * Software.
14  *
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
21  * IN THE SOFTWARE.
22  */
23
24 #include "anv_meta.h"
25 #include "nir/nir_builder.h"
26
27 struct blit_region {
28    VkOffset3D src_offset;
29    VkExtent3D src_extent;
30    VkOffset3D dest_offset;
31    VkExtent3D dest_extent;
32 };
33
34 static nir_shader *
35 build_nir_vertex_shader(void)
36 {
37    const struct glsl_type *vec4 = glsl_vec4_type();
38    nir_builder b;
39
40    nir_builder_init_simple_shader(&b, NULL, MESA_SHADER_VERTEX, NULL);
41    b.shader->info.name = ralloc_strdup(b.shader, "meta_blit_vs");
42
43    nir_variable *pos_in = nir_variable_create(b.shader, nir_var_shader_in,
44                                               vec4, "a_pos");
45    pos_in->data.location = VERT_ATTRIB_GENERIC0;
46    nir_variable *pos_out = nir_variable_create(b.shader, nir_var_shader_out,
47                                                vec4, "gl_Position");
48    pos_out->data.location = VARYING_SLOT_POS;
49    nir_copy_var(&b, pos_out, pos_in);
50
51    nir_variable *tex_pos_in = nir_variable_create(b.shader, nir_var_shader_in,
52                                                   vec4, "a_tex_pos");
53    tex_pos_in->data.location = VERT_ATTRIB_GENERIC1;
54    nir_variable *tex_pos_out = nir_variable_create(b.shader, nir_var_shader_out,
55                                                    vec4, "v_tex_pos");
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);
59
60    return b.shader;
61 }
62
63 static nir_shader *
64 build_nir_copy_fragment_shader(enum glsl_sampler_dim tex_dim)
65 {
66    const struct glsl_type *vec4 = glsl_vec4_type();
67    nir_builder b;
68
69    nir_builder_init_simple_shader(&b, NULL, MESA_SHADER_FRAGMENT, NULL);
70    b.shader->info.name = ralloc_strdup(b.shader, "meta_blit_fs");
71
72    nir_variable *tex_pos_in = nir_variable_create(b.shader, nir_var_shader_in,
73                                                   vec4, "v_tex_pos");
74    tex_pos_in->data.location = VARYING_SLOT_VAR0;
75
76    /* Swizzle the array index which comes in as Z coordinate into the right
77     * position.
78     */
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);
83
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;
91
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);
102
103    nir_ssa_dest_init(&tex->instr, &tex->dest, 4, "tex");
104    nir_builder_instr_insert(&b, &tex->instr);
105
106    nir_variable *color_out = nir_variable_create(b.shader, nir_var_shader_out,
107                                                  vec4, "f_color");
108    color_out->data.location = FRAG_RESULT_DATA0;
109    nir_store_var(&b, color_out, &tex->dest.ssa, 4);
110
111    return b.shader;
112 }
113
114 static void
115 meta_prepare_blit(struct anv_cmd_buffer *cmd_buffer,
116                   struct anv_meta_saved_state *saved_state)
117 {
118    anv_meta_save(saved_state, cmd_buffer,
119                  (1 << VK_DYNAMIC_STATE_VIEWPORT));
120 }
121
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.
125  */
126 static struct VkOffset3D
127 meta_region_offset_el(const struct anv_image * image,
128                       const struct VkOffset3D * offset)
129 {
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,
135    };
136 }
137
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.
141  */
142 static struct VkExtent3D
143 meta_region_extent_el(const VkFormat format,
144                       const struct VkExtent3D * extent)
145 {
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),
152    };
153 }
154
155 static void
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)
166 {
167    struct anv_device *device = cmd_buffer->device;
168
169    struct blit_vb_data {
170       float pos[2];
171       float tex_coord[3];
172    } *vb_data;
173
174    assert(src_image->samples == dest_image->samples);
175
176    unsigned vb_size = sizeof(struct anv_vue_header) + 3 * sizeof(*vb_data);
177
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);
182
183    vb_data[0] = (struct blit_vb_data) {
184       .pos = {
185          dest_offset.x + dest_extent.width,
186          dest_offset.y + dest_extent.height,
187       },
188       .tex_coord = {
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,
192       },
193    };
194
195    vb_data[1] = (struct blit_vb_data) {
196       .pos = {
197          dest_offset.x,
198          dest_offset.y + dest_extent.height,
199       },
200       .tex_coord = {
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,
204       },
205    };
206
207    vb_data[2] = (struct blit_vb_data) {
208       .pos = {
209          dest_offset.x,
210          dest_offset.y,
211       },
212       .tex_coord = {
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,
216       },
217    };
218
219    anv_state_clflush(vb_state);
220
221    struct anv_buffer vertex_buffer = {
222       .device = device,
223       .size = vb_size,
224       .bo = &device->dynamic_state_block_pool.bo,
225       .offset = vb_state.offset,
226    };
227
228    anv_CmdBindVertexBuffers(anv_cmd_buffer_to_handle(cmd_buffer), 0, 2,
229       (VkBuffer[]) {
230          anv_buffer_to_handle(&vertex_buffer),
231          anv_buffer_to_handle(&vertex_buffer)
232       },
233       (VkDeviceSize[]) {
234          0,
235          sizeof(struct anv_vue_header),
236       });
237
238    VkSampler sampler;
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);
245
246    VkDescriptorPool desc_pool;
247    anv_CreateDescriptorPool(anv_device_to_handle(device),
248       &(const VkDescriptorPoolCreateInfo) {
249          .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
250          .pNext = NULL,
251          .flags = 0,
252          .maxSets = 1,
253          .poolSizeCount = 1,
254          .pPoolSizes = (VkDescriptorPoolSize[]) {
255             {
256                .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
257                .descriptorCount = 1
258             },
259          }
260       }, &cmd_buffer->pool->alloc, &desc_pool);
261
262    VkDescriptorSet set;
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
269       }, &set);
270
271    anv_UpdateDescriptorSets(anv_device_to_handle(device),
272       1, /* writeCount */
273       (VkWriteDescriptorSet[]) {
274          {
275             .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
276             .dstSet = set,
277             .dstBinding = 0,
278             .dstArrayElement = 0,
279             .descriptorCount = 1,
280             .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
281             .pImageInfo = (VkDescriptorImageInfo[]) {
282                {
283                   .sampler = sampler,
284                   .imageView = anv_image_view_to_handle(src_iview),
285                   .imageLayout = VK_IMAGE_LAYOUT_GENERAL,
286                },
287             }
288          }
289       }, 0, NULL);
290
291    VkFramebuffer fb;
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),
298          },
299          .width = dest_iview->extent.width,
300          .height = dest_iview->extent.height,
301          .layers = 1
302       }, &cmd_buffer->pool->alloc, &fb);
303
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,
308          .framebuffer = fb,
309          .renderArea = {
310             .offset = { dest_offset.x, dest_offset.y },
311             .extent = { dest_extent.width, dest_extent.height },
312          },
313          .clearValueCount = 0,
314          .pClearValues = NULL,
315       }, VK_SUBPASS_CONTENTS_INLINE);
316
317    VkPipeline pipeline;
318
319    switch (src_image->type) {
320    case VK_IMAGE_TYPE_1D:
321       pipeline = device->meta_state.blit.pipeline_1d_src;
322       break;
323    case VK_IMAGE_TYPE_2D:
324       pipeline = device->meta_state.blit.pipeline_2d_src;
325       break;
326    case VK_IMAGE_TYPE_3D:
327       pipeline = device->meta_state.blit.pipeline_3d_src;
328       break;
329    default:
330       unreachable(!"bad VkImageType");
331    }
332
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);
336    }
337
338    anv_CmdSetViewport(anv_cmd_buffer_to_handle(cmd_buffer), 0, 1,
339                       &(VkViewport) {
340                         .x = 0.0f,
341                         .y = 0.0f,
342                         .width = dest_iview->extent.width,
343                         .height = dest_iview->extent.height,
344                         .minDepth = 0.0f,
345                         .maxDepth = 1.0f,
346                       });
347
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,
351                              &set, 0, NULL);
352
353    ANV_CALL(CmdDraw)(anv_cmd_buffer_to_handle(cmd_buffer), 3, 1, 0, 0);
354
355    ANV_CALL(CmdEndRenderPass)(anv_cmd_buffer_to_handle(cmd_buffer));
356
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.
359     */
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);
366 }
367
368 static void
369 meta_finish_blit(struct anv_cmd_buffer *cmd_buffer,
370                  const struct anv_meta_saved_state *saved_state)
371 {
372    anv_meta_restore(saved_state, cmd_buffer);
373 }
374
375 static VkFormat
376 vk_format_for_size(int bs)
377 {
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.
381     */
382    switch (bs) {
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;
391    default:
392       unreachable("Invalid format block size");
393    }
394 }
395
396 static void
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)
401 {
402    VkDevice vk_device = anv_device_to_handle(cmd_buffer->device);
403
404    VkImageCreateInfo image_info = {
405       .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
406       .imageType = VK_IMAGE_TYPE_2D,
407       .format = copy_format,
408       .extent = {
409          .width = width,
410          .height = height,
411          .depth = 1,
412       },
413       .mipLevels = 1,
414       .arrayLayers = 1,
415       .samples = 1,
416       .tiling = VK_IMAGE_TILING_LINEAR,
417       .usage = 0,
418       .flags = 0,
419    };
420
421    VkImage src_image;
422    image_info.usage = VK_IMAGE_USAGE_SAMPLED_BIT;
423    anv_CreateImage(vk_device, &image_info,
424                    &cmd_buffer->pool->alloc, &src_image);
425
426    VkImage dest_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);
430
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.
433     */
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;
438
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,
443          .image = src_image,
444          .viewType = VK_IMAGE_VIEW_TYPE_2D,
445          .format = copy_format,
446          .subresourceRange = {
447             .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
448             .baseMipLevel = 0,
449             .levelCount = 1,
450             .baseArrayLayer = 0,
451             .layerCount = 1
452          },
453       },
454       cmd_buffer, 0, VK_IMAGE_USAGE_SAMPLED_BIT);
455
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,
460          .image = dest_image,
461          .viewType = VK_IMAGE_VIEW_TYPE_2D,
462          .format = copy_format,
463          .subresourceRange = {
464             .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
465             .baseMipLevel = 0,
466             .levelCount = 1,
467             .baseArrayLayer = 0,
468             .layerCount = 1,
469          },
470       },
471       cmd_buffer, 0, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
472
473    meta_emit_blit(cmd_buffer,
474                   anv_image_from_handle(src_image),
475                   &src_iview,
476                   (VkOffset3D) { 0, 0, 0 },
477                   (VkExtent3D) { width, height, 1 },
478                   anv_image_from_handle(dest_image),
479                   &dest_iview,
480                   (VkOffset3D) { 0, 0, 0 },
481                   (VkExtent3D) { width, height, 1 },
482                   VK_FILTER_NEAREST);
483
484    anv_DestroyImage(vk_device, src_image, &cmd_buffer->pool->alloc);
485    anv_DestroyImage(vk_device, dest_image, &cmd_buffer->pool->alloc);
486 }
487
488 void anv_CmdCopyBuffer(
489     VkCommandBuffer                             commandBuffer,
490     VkBuffer                                    srcBuffer,
491     VkBuffer                                    destBuffer,
492     uint32_t                                    regionCount,
493     const VkBufferCopy*                         pRegions)
494 {
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);
498
499    struct anv_meta_saved_state saved_state;
500
501    meta_prepare_blit(cmd_buffer, &saved_state);
502
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;
507
508       /* First, we compute the biggest format that can be used with the
509        * given offsets and size.
510        */
511       int bs = 16;
512
513       int fs = ffs(src_offset) - 1;
514       if (fs != -1)
515          bs = MIN2(bs, 1 << fs);
516       assert(src_offset % bs == 0);
517
518       fs = ffs(dest_offset) - 1;
519       if (fs != -1)
520          bs = MIN2(bs, 1 << fs);
521       assert(dest_offset % bs == 0);
522
523       fs = ffs(pRegions[r].size) - 1;
524       if (fs != -1)
525          bs = MIN2(bs, 1 << fs);
526       assert(pRegions[r].size % bs == 0);
527
528       VkFormat copy_format = vk_format_for_size(bs);
529
530       /* This is maximum possible width/height our HW can handle */
531       uint64_t max_surface_dim = 1 << 14;
532
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;
542       }
543
544       uint64_t height = copy_size / (max_surface_dim * bs);
545       assert(height < max_surface_dim);
546       if (height != 0) {
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;
554       }
555
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);
560       }
561    }
562
563    meta_finish_blit(cmd_buffer, &saved_state);
564 }
565
566 void anv_CmdUpdateBuffer(
567     VkCommandBuffer                             commandBuffer,
568     VkBuffer                                    dstBuffer,
569     VkDeviceSize                                dstOffset,
570     VkDeviceSize                                dataSize,
571     const uint32_t*                             pData)
572 {
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;
576
577    meta_prepare_blit(cmd_buffer, &saved_state);
578
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.
581     */
582    const uint32_t max_update_size =
583       cmd_buffer->device->dynamic_state_block_pool.block_size - 64;
584
585    assert(max_update_size < (1 << 14) * 4);
586
587    while (dataSize) {
588       const uint32_t copy_size = MIN2(dataSize, max_update_size);
589
590       struct anv_state tmp_data =
591          anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, copy_size, 64);
592
593       memcpy(tmp_data.map, pData, copy_size);
594
595       VkFormat format;
596       int bs;
597       if ((copy_size & 15) == 0 && (dstOffset & 15) == 0) {
598          format = VK_FORMAT_R32G32B32A32_UINT;
599          bs = 16;
600       } else if ((copy_size & 7) == 0 && (dstOffset & 7) == 0) {
601          format = VK_FORMAT_R32G32_UINT;
602          bs = 8;
603       } else {
604          assert((copy_size & 3) == 0 && (dstOffset & 3) == 0);
605          format = VK_FORMAT_R32_UINT;
606          bs = 4;
607       }
608
609       do_buffer_copy(cmd_buffer,
610                      &cmd_buffer->device->dynamic_state_block_pool.bo,
611                      tmp_data.offset,
612                      dst_buffer->bo, dst_buffer->offset + dstOffset,
613                      copy_size / bs, 1, format);
614
615       dataSize -= copy_size;
616       dstOffset += copy_size;
617       pData = (void *)pData + copy_size;
618    }
619 }
620
621 static VkFormat
622 choose_iview_format(struct anv_image *image, VkImageAspectFlagBits aspect)
623 {
624    assert(__builtin_popcount(aspect) == 1);
625
626    struct isl_surf *surf =
627       &anv_image_get_surface_for_aspect_mask(image, aspect)->isl;
628
629    /* vkCmdCopyImage behaves like memcpy. Therefore we choose identical UINT
630     * formats for the source and destination image views.
631     *
632     * From the Vulkan spec (2015-12-30):
633     *
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.
640     *
641     *    [The spec later defines compatibility as having the same number of
642     *    bytes per block].
643     */
644    return vk_format_for_size(isl_format_layouts[surf->format].bs);
645 }
646
647 static VkFormat
648 choose_buffer_format(VkFormat format, VkImageAspectFlagBits aspect)
649 {
650    assert(__builtin_popcount(aspect) == 1);
651
652    /* vkCmdCopy* commands behave like memcpy. Therefore we choose
653     * compatable UINT formats for the source and destination image views.
654     *
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.
659     */
660    enum isl_format linear_format = anv_get_isl_format(format, aspect,
661                                                       VK_IMAGE_TILING_LINEAR,
662                                                       NULL);
663
664    return vk_format_for_size(isl_format_layouts[linear_format].bs);
665 }
666
667 void anv_CmdCopyImage(
668     VkCommandBuffer                             commandBuffer,
669     VkImage                                     srcImage,
670     VkImageLayout                               srcImageLayout,
671     VkImage                                     destImage,
672     VkImageLayout                               destImageLayout,
673     uint32_t                                    regionCount,
674     const VkImageCopy*                          pRegions)
675 {
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;
680
681    /* From the Vulkan 1.0 spec:
682     *
683     *    vkCmdCopyImage can be used to copy image data between multisample
684     *    images, but both images must have the same number of samples.
685     */
686    assert(src_image->samples == dest_image->samples);
687
688    meta_prepare_blit(cmd_buffer, &saved_state);
689
690    for (unsigned r = 0; r < regionCount; r++) {
691       assert(pRegions[r].srcSubresource.aspectMask ==
692              pRegions[r].dstSubresource.aspectMask);
693
694       VkImageAspectFlags aspect = pRegions[r].srcSubresource.aspectMask;
695
696       VkFormat src_format = choose_iview_format(src_image, aspect);
697       VkFormat dst_format = choose_iview_format(dest_image, aspect);
698
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,
703             .image = srcImage,
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,
709                .levelCount = 1,
710                .baseArrayLayer = pRegions[r].srcSubresource.baseArrayLayer,
711                .layerCount = pRegions[r].dstSubresource.layerCount,
712             },
713          },
714          cmd_buffer, 0, VK_IMAGE_USAGE_SAMPLED_BIT);
715
716       const uint32_t dest_base_array_slice =
717          anv_meta_get_iview_layer(dest_image, &pRegions[r].dstSubresource,
718                                   &pRegions[r].dstOffset);
719
720
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;
728
729          uint32_t img_x = 0;
730          uint32_t img_y = 0;
731          uint32_t img_o = 0;
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);
739
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;
744
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,
749                .image = destImage,
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,
755                   .levelCount = 1,
756                   .baseArrayLayer = dest_base_array_slice +
757                                     slice_array + slice_3d,
758                   .layerCount = 1
759                },
760             },
761             cmd_buffer, img_o, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
762
763          const VkExtent3D img_extent_el = meta_region_extent_el(dest_image->vk_format,
764                                                                 &pRegions[r].extent);
765
766          meta_emit_blit(cmd_buffer,
767                         src_image, &src_iview,
768                         src_offset,
769                         img_extent_el,
770                         dest_image, &dest_iview,
771                         dest_offset_el,
772                         img_extent_el,
773                         VK_FILTER_NEAREST);
774
775          if (dest_image->type == VK_IMAGE_TYPE_3D)
776             slice_3d++;
777          else
778             slice_array++;
779       }
780    }
781
782    meta_finish_blit(cmd_buffer, &saved_state);
783 }
784
785 void anv_CmdBlitImage(
786     VkCommandBuffer                             commandBuffer,
787     VkImage                                     srcImage,
788     VkImageLayout                               srcImageLayout,
789     VkImage                                     destImage,
790     VkImageLayout                               destImageLayout,
791     uint32_t                                    regionCount,
792     const VkImageBlit*                          pRegions,
793     VkFilter                                    filter)
794
795 {
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;
800
801    /* From the Vulkan 1.0 spec:
802     *
803     *    vkCmdBlitImage must not be used for multisampled source or
804     *    destination images. Use vkCmdResolveImage for this purpose.
805     */
806    assert(src_image->samples == 1);
807    assert(dest_image->samples == 1);
808
809    anv_finishme("respect VkFilter");
810
811    meta_prepare_blit(cmd_buffer, &saved_state);
812
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,
818             .image = srcImage,
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,
824                .levelCount = 1,
825                .baseArrayLayer = pRegions[r].srcSubresource.baseArrayLayer,
826                .layerCount = 1
827             },
828          },
829          cmd_buffer, 0, VK_IMAGE_USAGE_SAMPLED_BIT);
830
831       const VkOffset3D dest_offset = {
832          .x = pRegions[r].dstOffsets[0].x,
833          .y = pRegions[r].dstOffsets[0].y,
834          .z = 0,
835       };
836
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");
842
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,
846       };
847
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,
851       };
852
853       const uint32_t dest_array_slice =
854          anv_meta_get_iview_layer(dest_image, &pRegions[r].dstSubresource,
855                                   &pRegions[r].dstOffsets[0]);
856
857       if (pRegions[r].srcSubresource.layerCount > 1)
858          anv_finishme("FINISHME: copy multiple array layers");
859
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");
863
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,
868             .image = destImage,
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,
874                .levelCount = 1,
875                .baseArrayLayer = dest_array_slice,
876                .layerCount = 1
877             },
878          },
879          cmd_buffer, 0, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
880
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,
886                      filter);
887    }
888
889    meta_finish_blit(cmd_buffer, &saved_state);
890 }
891
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)
898 {
899    ANV_FROM_HANDLE(anv_buffer, buffer, vk_buffer);
900
901    VkExtent3D extent = copy->imageExtent;
902    if (copy->bufferRowLength)
903       extent.width = copy->bufferRowLength;
904    if (copy->bufferImageHeight)
905       extent.height = copy->bufferImageHeight;
906    extent.depth = 1;
907    extent = meta_region_extent_el(format, &extent);
908
909    VkImageAspectFlags aspect = copy->imageSubresource.aspectMask;
910    VkFormat buffer_format = choose_buffer_format(format, aspect);
911
912    VkImage vk_image;
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,
918          .extent = extent,
919          .mipLevels = 1,
920          .arrayLayers = 1,
921          .samples = 1,
922          .tiling = VK_IMAGE_TILING_LINEAR,
923          .usage = usage,
924          .flags = 0,
925       }, alloc, &vk_image);
926    assert(result == VK_SUCCESS);
927
928    ANV_FROM_HANDLE(anv_image, image, vk_image);
929
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.
932     */
933    image->bo = buffer->bo;
934    image->offset = buffer->offset + copy->bufferOffset;
935
936    return image;
937 }
938
939 void anv_CmdCopyBufferToImage(
940     VkCommandBuffer                             commandBuffer,
941     VkBuffer                                    srcBuffer,
942     VkImage                                     destImage,
943     VkImageLayout                               destImageLayout,
944     uint32_t                                    regionCount,
945     const VkBufferImageCopy*                    pRegions)
946 {
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;
951
952    /* The Vulkan 1.0 spec says "dstImage must have a sample count equal to
953     * VK_SAMPLE_COUNT_1_BIT."
954     */
955    assert(dest_image->samples == 1);
956
957    meta_prepare_blit(cmd_buffer, &saved_state);
958
959    for (unsigned r = 0; r < regionCount; r++) {
960       VkImageAspectFlags aspect = pRegions[r].imageSubresource.aspectMask;
961
962       VkFormat image_format = choose_iview_format(dest_image, aspect);
963
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,
968                                &pRegions[r]);
969
970       const uint32_t dest_base_array_slice =
971          anv_meta_get_iview_layer(dest_image, &pRegions[r].imageSubresource,
972                                   &pRegions[r].imageOffset);
973
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,
988                   .baseMipLevel = 0,
989                   .levelCount = 1,
990                   .baseArrayLayer = 0,
991                   .layerCount = 1,
992                },
993             },
994             cmd_buffer, 0, VK_IMAGE_USAGE_SAMPLED_BIT);
995
996          uint32_t img_x = 0;
997          uint32_t img_y = 0;
998          uint32_t img_o = 0;
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);
1006
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;
1011
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,
1022                   .levelCount = 1,
1023                   .baseArrayLayer = dest_base_array_slice +
1024                                     slice_array + slice_3d,
1025                   .layerCount = 1
1026                },
1027             },
1028             cmd_buffer, img_o, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
1029
1030          const VkExtent3D img_extent_el = meta_region_extent_el(dest_image->vk_format,
1031                                                       &pRegions[r].imageExtent);
1032
1033          meta_emit_blit(cmd_buffer,
1034                         src_image,
1035                         &src_iview,
1036                         (VkOffset3D){0, 0, 0},
1037                         img_extent_el,
1038                         dest_image,
1039                         &dest_iview,
1040                         dest_offset_el,
1041                         img_extent_el,
1042                         VK_FILTER_NEAREST);
1043
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.
1048           */
1049          src_image->offset += src_image->extent.width *
1050                               src_image->extent.height *
1051                               src_image->format->isl_layout->bs;
1052
1053          if (dest_image->type == VK_IMAGE_TYPE_3D)
1054             slice_3d++;
1055          else
1056             slice_array++;
1057       }
1058
1059       anv_DestroyImage(vk_device, anv_image_to_handle(src_image),
1060                        &cmd_buffer->pool->alloc);
1061    }
1062
1063    meta_finish_blit(cmd_buffer, &saved_state);
1064 }
1065
1066 void anv_CmdCopyImageToBuffer(
1067     VkCommandBuffer                             commandBuffer,
1068     VkImage                                     srcImage,
1069     VkImageLayout                               srcImageLayout,
1070     VkBuffer                                    destBuffer,
1071     uint32_t                                    regionCount,
1072     const VkBufferImageCopy*                    pRegions)
1073 {
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;
1078
1079
1080    /* The Vulkan 1.0 spec says "srcImage must have a sample count equal to
1081     * VK_SAMPLE_COUNT_1_BIT."
1082     */
1083    assert(src_image->samples == 1);
1084
1085    meta_prepare_blit(cmd_buffer, &saved_state);
1086
1087    for (unsigned r = 0; r < regionCount; r++) {
1088       VkImageAspectFlags aspect = pRegions[r].imageSubresource.aspectMask;
1089
1090       VkFormat image_format = choose_iview_format(src_image, aspect);
1091
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,
1096             .image = srcImage,
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,
1102                .levelCount = 1,
1103                .baseArrayLayer = pRegions[r].imageSubresource.baseArrayLayer,
1104                .layerCount = pRegions[r].imageSubresource.layerCount,
1105             },
1106          },
1107          cmd_buffer, 0, VK_IMAGE_USAGE_SAMPLED_BIT);
1108
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,
1113                                &pRegions[r]);
1114
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;
1119       } else {
1120          assert(pRegions[r].imageExtent.depth == 1);
1121          num_slices = pRegions[r].imageSubresource.layerCount;
1122       }
1123
1124       for (unsigned slice = 0; slice < num_slices; slice++) {
1125          VkOffset3D src_offset = pRegions[r].imageOffset;
1126          src_offset.z += slice;
1127
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,
1137                   .baseMipLevel = 0,
1138                   .levelCount = 1,
1139                   .baseArrayLayer = 0,
1140                   .layerCount = 1
1141                },
1142             },
1143             cmd_buffer, 0, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
1144
1145          meta_emit_blit(cmd_buffer,
1146                         anv_image_from_handle(srcImage),
1147                         &src_iview,
1148                         src_offset,
1149                         pRegions[r].imageExtent,
1150                         dest_image,
1151                         &dest_iview,
1152                         (VkOffset3D) { 0, 0, 0 },
1153                         pRegions[r].imageExtent,
1154                         VK_FILTER_NEAREST);
1155
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.
1160           */
1161          dest_image->offset += dest_image->extent.width *
1162                                dest_image->extent.height *
1163                                src_image->format->isl_layout->bs;
1164       }
1165
1166       anv_DestroyImage(vk_device, anv_image_to_handle(dest_image),
1167                        &cmd_buffer->pool->alloc);
1168    }
1169
1170    meta_finish_blit(cmd_buffer, &saved_state);
1171 }
1172
1173 void
1174 anv_device_finish_meta_blit_state(struct anv_device *device)
1175 {
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);
1194 }
1195
1196 VkResult
1197 anv_device_init_meta_blit_state(struct anv_device *device)
1198 {
1199    VkResult result;
1200
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,
1211          },
1212          .subpassCount = 1,
1213          .pSubpasses = &(VkSubpassDescription) {
1214             .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1215             .inputAttachmentCount = 0,
1216             .colorAttachmentCount = 1,
1217             .pColorAttachments = &(VkAttachmentReference) {
1218                .attachment = 0,
1219                .layout = VK_IMAGE_LAYOUT_GENERAL,
1220             },
1221             .pResolveAttachments = NULL,
1222             .pDepthStencilAttachment = &(VkAttachmentReference) {
1223                .attachment = VK_ATTACHMENT_UNUSED,
1224                .layout = VK_IMAGE_LAYOUT_GENERAL,
1225             },
1226             .preserveAttachmentCount = 1,
1227             .pPreserveAttachments = (uint32_t[]) { 0 },
1228          },
1229          .dependencyCount = 0,
1230       }, &device->meta_state.alloc, &device->meta_state.blit.render_pass);
1231    if (result != VK_SUCCESS)
1232       goto fail;
1233
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.
1238     */
1239    struct anv_shader_module vs = {
1240       .nir = build_nir_vertex_shader(),
1241    };
1242
1243    struct anv_shader_module fs_1d = {
1244       .nir = build_nir_copy_fragment_shader(GLSL_SAMPLER_DIM_1D),
1245    };
1246
1247    struct anv_shader_module fs_2d = {
1248       .nir = build_nir_copy_fragment_shader(GLSL_SAMPLER_DIM_2D),
1249    };
1250
1251    struct anv_shader_module fs_3d = {
1252       .nir = build_nir_copy_fragment_shader(GLSL_SAMPLER_DIM_3D),
1253    };
1254
1255    VkPipelineVertexInputStateCreateInfo vi_create_info = {
1256       .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
1257       .vertexBindingDescriptionCount = 2,
1258       .pVertexBindingDescriptions = (VkVertexInputBindingDescription[]) {
1259          {
1260             .binding = 0,
1261             .stride = 0,
1262             .inputRate = VK_VERTEX_INPUT_RATE_VERTEX
1263          },
1264          {
1265             .binding = 1,
1266             .stride = 5 * sizeof(float),
1267             .inputRate = VK_VERTEX_INPUT_RATE_VERTEX
1268          },
1269       },
1270       .vertexAttributeDescriptionCount = 3,
1271       .pVertexAttributeDescriptions = (VkVertexInputAttributeDescription[]) {
1272          {
1273             /* VUE Header */
1274             .location = 0,
1275             .binding = 0,
1276             .format = VK_FORMAT_R32G32B32A32_UINT,
1277             .offset = 0
1278          },
1279          {
1280             /* Position */
1281             .location = 1,
1282             .binding = 1,
1283             .format = VK_FORMAT_R32G32_SFLOAT,
1284             .offset = 0
1285          },
1286          {
1287             /* Texture Coordinate */
1288             .location = 2,
1289             .binding = 1,
1290             .format = VK_FORMAT_R32G32B32_SFLOAT,
1291             .offset = 8
1292          }
1293       }
1294    };
1295
1296    VkDescriptorSetLayoutCreateInfo ds_layout_info = {
1297       .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
1298       .bindingCount = 1,
1299       .pBindings = (VkDescriptorSetLayoutBinding[]) {
1300          {
1301             .binding = 0,
1302             .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1303             .descriptorCount = 1,
1304             .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1305             .pImmutableSamplers = NULL
1306          },
1307       }
1308    };
1309    result = anv_CreateDescriptorSetLayout(anv_device_to_handle(device),
1310                                           &ds_layout_info,
1311                                           &device->meta_state.alloc,
1312                                           &device->meta_state.blit.ds_layout);
1313    if (result != VK_SUCCESS)
1314       goto fail_render_pass;
1315
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,
1321       },
1322       &device->meta_state.alloc, &device->meta_state.blit.pipeline_layout);
1323    if (result != VK_SUCCESS)
1324       goto fail_descriptor_set_layout;
1325
1326    VkPipelineShaderStageCreateInfo pipeline_shader_stages[] = {
1327       {
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),
1331          .pName = "main",
1332          .pSpecializationInfo = NULL
1333       }, {
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! */
1337          .pName = "main",
1338          .pSpecializationInfo = NULL
1339       },
1340    };
1341
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,
1351       },
1352       .pViewportState = &(VkPipelineViewportStateCreateInfo) {
1353          .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
1354          .viewportCount = 1,
1355          .scissorCount = 1,
1356       },
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
1363       },
1364       .pMultisampleState = &(VkPipelineMultisampleStateCreateInfo) {
1365          .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
1366          .rasterizationSamples = 1,
1367          .sampleShadingEnable = false,
1368          .pSampleMask = (VkSampleMask[]) { UINT32_MAX },
1369       },
1370       .pColorBlendState = &(VkPipelineColorBlendStateCreateInfo) {
1371          .sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
1372          .attachmentCount = 1,
1373          .pAttachments = (VkPipelineColorBlendAttachmentState []) {
1374             { .colorWriteMask =
1375                  VK_COLOR_COMPONENT_A_BIT |
1376                  VK_COLOR_COMPONENT_R_BIT |
1377                  VK_COLOR_COMPONENT_G_BIT |
1378                  VK_COLOR_COMPONENT_B_BIT },
1379          }
1380       },
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,
1394          },
1395       },
1396       .flags = 0,
1397       .layout = device->meta_state.blit.pipeline_layout,
1398       .renderPass = device->meta_state.blit.render_pass,
1399       .subpass = 0,
1400    };
1401
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,
1407       .disable_vs = true,
1408       .use_rectlist = true
1409    };
1410
1411    pipeline_shader_stages[1].module = anv_shader_module_to_handle(&fs_1d);
1412    result = anv_graphics_pipeline_create(anv_device_to_handle(device),
1413       VK_NULL_HANDLE,
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;
1418
1419    pipeline_shader_stages[1].module = anv_shader_module_to_handle(&fs_2d);
1420    result = anv_graphics_pipeline_create(anv_device_to_handle(device),
1421       VK_NULL_HANDLE,
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;
1426
1427    pipeline_shader_stages[1].module = anv_shader_module_to_handle(&fs_3d);
1428    result = anv_graphics_pipeline_create(anv_device_to_handle(device),
1429       VK_NULL_HANDLE,
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;
1434
1435    ralloc_free(vs.nir);
1436    ralloc_free(fs_1d.nir);
1437    ralloc_free(fs_2d.nir);
1438    ralloc_free(fs_3d.nir);
1439
1440    return VK_SUCCESS;
1441
1442  fail_pipeline_2d:
1443    anv_DestroyPipeline(anv_device_to_handle(device),
1444                        device->meta_state.blit.pipeline_2d_src,
1445                        &device->meta_state.alloc);
1446
1447  fail_pipeline_1d:
1448    anv_DestroyPipeline(anv_device_to_handle(device),
1449                        device->meta_state.blit.pipeline_1d_src,
1450                        &device->meta_state.alloc);
1451
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);
1460  fail_render_pass:
1461    anv_DestroyRenderPass(anv_device_to_handle(device),
1462                          device->meta_state.blit.render_pass,
1463                          &device->meta_state.alloc);
1464
1465    ralloc_free(vs.nir);
1466    ralloc_free(fs_1d.nir);
1467    ralloc_free(fs_2d.nir);
1468    ralloc_free(fs_3d.nir);
1469  fail:
1470    return result;
1471 }