OSDN Git Service

anv: Add a struct for storing a compiled shader
[android-x86/external-mesa.git] / src / intel / vulkan / anv_meta_resolve.c
1 /*
2  * Copyright © 2016 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 <assert.h>
25 #include <stdbool.h>
26
27 #include "anv_meta.h"
28 #include "anv_private.h"
29 #include "nir/nir_builder.h"
30
31 /**
32  * Vertex attributes used by all pipelines.
33  */
34 struct vertex_attrs {
35    struct anv_vue_header vue_header;
36    float position[2]; /**< 3DPRIM_RECTLIST */
37    float tex_position[2];
38 };
39
40 static void
41 meta_resolve_save(struct anv_meta_saved_state *saved_state,
42                   struct anv_cmd_buffer *cmd_buffer)
43 {
44    anv_meta_save(saved_state, cmd_buffer, 0);
45 }
46
47 static void
48 meta_resolve_restore(struct anv_meta_saved_state *saved_state,
49                      struct anv_cmd_buffer *cmd_buffer)
50 {
51    anv_meta_restore(saved_state, cmd_buffer);
52 }
53
54 static VkPipeline *
55 get_pipeline_h(struct anv_device *device, uint32_t samples)
56 {
57    uint32_t i = ffs(samples) - 2; /* log2(samples) - 1 */
58
59    assert(samples >= 2);
60    assert(i < ARRAY_SIZE(device->meta_state.resolve.pipelines));
61
62    return &device->meta_state.resolve.pipelines[i];
63 }
64
65 static nir_shader *
66 build_nir_vs(void)
67 {
68    const struct glsl_type *vec4 = glsl_vec4_type();
69
70    nir_builder b;
71    nir_variable *a_position;
72    nir_variable *v_position;
73    nir_variable *a_tex_position;
74    nir_variable *v_tex_position;
75
76    nir_builder_init_simple_shader(&b, NULL, MESA_SHADER_VERTEX, NULL);
77    b.shader->info.name = ralloc_strdup(b.shader, "meta_resolve_vs");
78
79    a_position = nir_variable_create(b.shader, nir_var_shader_in, vec4,
80                                     "a_position");
81    a_position->data.location = VERT_ATTRIB_GENERIC0;
82
83    v_position = nir_variable_create(b.shader, nir_var_shader_out, vec4,
84                                     "gl_Position");
85    v_position->data.location = VARYING_SLOT_POS;
86
87    a_tex_position = nir_variable_create(b.shader, nir_var_shader_in, vec4,
88                                     "a_tex_position");
89    a_tex_position->data.location = VERT_ATTRIB_GENERIC1;
90
91    v_tex_position = nir_variable_create(b.shader, nir_var_shader_out, vec4,
92                                     "v_tex_position");
93    v_tex_position->data.location = VARYING_SLOT_VAR0;
94
95    nir_copy_var(&b, v_position, a_position);
96    nir_copy_var(&b, v_tex_position, a_tex_position);
97
98    return b.shader;
99 }
100
101 static nir_shader *
102 build_nir_fs(uint32_t num_samples)
103 {
104    const struct glsl_type *vec4 = glsl_vec4_type();
105
106    const struct glsl_type *sampler2DMS =
107          glsl_sampler_type(GLSL_SAMPLER_DIM_MS,
108                            /*is_shadow*/ false,
109                            /*is_array*/ false,
110                            GLSL_TYPE_FLOAT);
111
112    nir_builder b;
113    nir_variable *u_tex; /* uniform sampler */
114    nir_variable *v_position; /* vec4, varying fragment position */
115    nir_variable *v_tex_position; /* vec4, varying texture coordinate */
116    nir_variable *f_color; /* vec4, fragment output color */
117    nir_ssa_def *accum; /* vec4, accumulation of sample values */
118
119    nir_builder_init_simple_shader(&b, NULL, MESA_SHADER_FRAGMENT, NULL);
120    b.shader->info.name = ralloc_asprintf(b.shader,
121                                          "meta_resolve_fs_samples%02d",
122                                          num_samples);
123
124    u_tex = nir_variable_create(b.shader, nir_var_uniform, sampler2DMS,
125                                    "u_tex");
126    u_tex->data.descriptor_set = 0;
127    u_tex->data.binding = 0;
128
129    v_position = nir_variable_create(b.shader, nir_var_shader_in, vec4,
130                                      "v_position");
131    v_position->data.location = VARYING_SLOT_POS;
132    v_position->data.origin_upper_left = true;
133
134    v_tex_position = nir_variable_create(b.shader, nir_var_shader_in, vec4,
135                                     "v_tex_position");
136    v_tex_position->data.location = VARYING_SLOT_VAR0;
137
138    f_color = nir_variable_create(b.shader, nir_var_shader_out, vec4,
139                                  "f_color");
140    f_color->data.location = FRAG_RESULT_DATA0;
141
142    accum = nir_imm_vec4(&b, 0, 0, 0, 0);
143
144    nir_ssa_def *tex_position_ivec =
145       nir_f2i(&b, nir_load_var(&b, v_tex_position));
146
147    for (uint32_t i = 0; i < num_samples; ++i) {
148       nir_tex_instr *tex;
149
150       tex = nir_tex_instr_create(b.shader, /*num_srcs*/ 2);
151       tex->texture = nir_deref_var_create(tex, u_tex);
152       tex->sampler = nir_deref_var_create(tex, u_tex);
153       tex->sampler_dim = GLSL_SAMPLER_DIM_MS;
154       tex->op = nir_texop_txf_ms;
155       tex->src[0].src = nir_src_for_ssa(tex_position_ivec);
156       tex->src[0].src_type = nir_tex_src_coord;
157       tex->src[1].src = nir_src_for_ssa(nir_imm_int(&b, i));
158       tex->src[1].src_type = nir_tex_src_ms_index;
159       tex->dest_type = nir_type_float;
160       tex->is_array = false;
161       tex->coord_components = 3;
162       nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, "tex");
163       nir_builder_instr_insert(&b, &tex->instr);
164
165       accum = nir_fadd(&b, accum, &tex->dest.ssa);
166    }
167
168    accum = nir_fdiv(&b, accum, nir_imm_float(&b, num_samples));
169    nir_store_var(&b, f_color, accum, /*writemask*/ 4);
170
171    return b.shader;
172 }
173
174 static VkResult
175 create_pass(struct anv_device *device)
176 {
177    VkResult result;
178    VkDevice device_h = anv_device_to_handle(device);
179    const VkAllocationCallbacks *alloc = &device->meta_state.alloc;
180
181    result = anv_CreateRenderPass(device_h,
182       &(VkRenderPassCreateInfo) {
183          .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
184          .attachmentCount = 1,
185          .pAttachments = &(VkAttachmentDescription) {
186             .format = VK_FORMAT_UNDEFINED, /* Our shaders don't care */
187             .samples = 1,
188             .loadOp = VK_ATTACHMENT_LOAD_OP_LOAD,
189             .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
190             .initialLayout = VK_IMAGE_LAYOUT_GENERAL,
191             .finalLayout = VK_IMAGE_LAYOUT_GENERAL,
192          },
193          .subpassCount = 1,
194          .pSubpasses = &(VkSubpassDescription) {
195             .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
196             .inputAttachmentCount = 0,
197             .colorAttachmentCount = 1,
198             .pColorAttachments = &(VkAttachmentReference) {
199                .attachment = 0,
200                .layout = VK_IMAGE_LAYOUT_GENERAL,
201             },
202             .pResolveAttachments = NULL,
203             .pDepthStencilAttachment = &(VkAttachmentReference) {
204                .attachment = VK_ATTACHMENT_UNUSED,
205             },
206             .preserveAttachmentCount = 0,
207             .pPreserveAttachments = NULL,
208          },
209          .dependencyCount = 0,
210       },
211       alloc,
212       &device->meta_state.resolve.pass);
213
214    return result;
215 }
216
217 static VkResult
218 create_pipeline(struct anv_device *device,
219                 uint32_t num_samples,
220                 VkShaderModule vs_module_h)
221 {
222    VkResult result;
223    VkDevice device_h = anv_device_to_handle(device);
224
225    struct anv_shader_module fs_module = {
226       .nir = build_nir_fs(num_samples),
227    };
228
229    if (!fs_module.nir) {
230       /* XXX: Need more accurate error */
231       result = VK_ERROR_OUT_OF_HOST_MEMORY;
232       goto cleanup;
233    }
234
235    result = anv_graphics_pipeline_create(device_h,
236       VK_NULL_HANDLE,
237       &(VkGraphicsPipelineCreateInfo) {
238          .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
239          .stageCount = 2,
240          .pStages = (VkPipelineShaderStageCreateInfo[]) {
241             {
242                .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
243                .stage = VK_SHADER_STAGE_VERTEX_BIT,
244                .module = vs_module_h,
245                .pName = "main",
246             },
247             {
248                .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
249                .stage = VK_SHADER_STAGE_FRAGMENT_BIT,
250                .module = anv_shader_module_to_handle(&fs_module),
251                .pName = "main",
252             },
253          },
254          .pVertexInputState = &(VkPipelineVertexInputStateCreateInfo) {
255             .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
256             .vertexBindingDescriptionCount = 1,
257             .pVertexBindingDescriptions = (VkVertexInputBindingDescription[]) {
258                {
259                   .binding = 0,
260                   .stride = sizeof(struct vertex_attrs),
261                   .inputRate = VK_VERTEX_INPUT_RATE_VERTEX
262                },
263             },
264             .vertexAttributeDescriptionCount = 3,
265             .pVertexAttributeDescriptions = (VkVertexInputAttributeDescription[]) {
266                {
267                   /* VUE Header */
268                   .location = 0,
269                   .binding = 0,
270                   .format = VK_FORMAT_R32G32B32A32_UINT,
271                   .offset = offsetof(struct vertex_attrs, vue_header),
272                },
273                {
274                   /* Position */
275                   .location = 1,
276                   .binding = 0,
277                   .format = VK_FORMAT_R32G32_SFLOAT,
278                   .offset = offsetof(struct vertex_attrs, position),
279                },
280                {
281                   /* Texture Coordinate */
282                   .location = 2,
283                   .binding = 0,
284                   .format = VK_FORMAT_R32G32_SFLOAT,
285                   .offset = offsetof(struct vertex_attrs, tex_position),
286                },
287             },
288          },
289          .pInputAssemblyState = &(VkPipelineInputAssemblyStateCreateInfo) {
290             .sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
291             .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
292             .primitiveRestartEnable = false,
293          },
294          .pViewportState = &(VkPipelineViewportStateCreateInfo) {
295             .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
296             .viewportCount = 1,
297             .scissorCount = 1,
298          },
299          .pRasterizationState = &(VkPipelineRasterizationStateCreateInfo) {
300             .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
301             .depthClampEnable = false,
302             .rasterizerDiscardEnable = false,
303             .polygonMode = VK_POLYGON_MODE_FILL,
304             .cullMode = VK_CULL_MODE_NONE,
305             .frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE,
306          },
307          .pMultisampleState = &(VkPipelineMultisampleStateCreateInfo) {
308             .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
309             .rasterizationSamples = 1,
310             .sampleShadingEnable = false,
311             .pSampleMask = (VkSampleMask[]) { 0x1 },
312             .alphaToCoverageEnable = false,
313             .alphaToOneEnable = false,
314          },
315          .pColorBlendState = &(VkPipelineColorBlendStateCreateInfo) {
316             .sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
317             .logicOpEnable = false,
318             .attachmentCount = 1,
319             .pAttachments = (VkPipelineColorBlendAttachmentState []) {
320                {
321                   .colorWriteMask = VK_COLOR_COMPONENT_R_BIT |
322                                     VK_COLOR_COMPONENT_G_BIT |
323                                     VK_COLOR_COMPONENT_B_BIT |
324                                     VK_COLOR_COMPONENT_A_BIT,
325                },
326             },
327          },
328          .pDynamicState = &(VkPipelineDynamicStateCreateInfo) {
329             .sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
330             .dynamicStateCount = 2,
331             .pDynamicStates = (VkDynamicState[]) {
332                VK_DYNAMIC_STATE_VIEWPORT,
333                VK_DYNAMIC_STATE_SCISSOR,
334             },
335          },
336          .layout = device->meta_state.resolve.pipeline_layout,
337          .renderPass = device->meta_state.resolve.pass,
338          .subpass = 0,
339       },
340       &(struct anv_graphics_pipeline_create_info) {
341          .color_attachment_count = -1,
342          .use_repclear = false,
343          .disable_vs = true,
344          .use_rectlist = true
345       },
346       &device->meta_state.alloc,
347       get_pipeline_h(device, num_samples));
348    if (result != VK_SUCCESS)
349       goto cleanup;
350
351    goto cleanup;
352
353 cleanup:
354    ralloc_free(fs_module.nir);
355    return result;
356 }
357
358 void
359 anv_device_finish_meta_resolve_state(struct anv_device *device)
360 {
361    struct anv_meta_state *state = &device->meta_state;
362    VkDevice device_h = anv_device_to_handle(device);
363    VkRenderPass pass_h = device->meta_state.resolve.pass;
364    VkPipelineLayout pipeline_layout_h = device->meta_state.resolve.pipeline_layout;
365    VkDescriptorSetLayout ds_layout_h = device->meta_state.resolve.ds_layout;
366    const VkAllocationCallbacks *alloc = &device->meta_state.alloc;
367
368    if (pass_h)
369       ANV_CALL(DestroyRenderPass)(device_h, pass_h,
370                                   &device->meta_state.alloc);
371
372    if (pipeline_layout_h)
373       ANV_CALL(DestroyPipelineLayout)(device_h, pipeline_layout_h, alloc);
374
375    if (ds_layout_h)
376       ANV_CALL(DestroyDescriptorSetLayout)(device_h, ds_layout_h, alloc);
377
378    for (uint32_t i = 0; i < ARRAY_SIZE(state->resolve.pipelines); ++i) {
379       VkPipeline pipeline_h = state->resolve.pipelines[i];
380
381       if (pipeline_h) {
382          ANV_CALL(DestroyPipeline)(device_h, pipeline_h, alloc);
383       }
384    }
385 }
386
387 VkResult
388 anv_device_init_meta_resolve_state(struct anv_device *device)
389 {
390    VkResult res = VK_SUCCESS;
391    VkDevice device_h = anv_device_to_handle(device);
392    const VkAllocationCallbacks *alloc = &device->meta_state.alloc;
393
394    const isl_sample_count_mask_t sample_count_mask =
395       isl_device_get_sample_counts(&device->isl_dev);
396
397    zero(device->meta_state.resolve);
398
399    struct anv_shader_module vs_module = { .nir = build_nir_vs() };
400    if (!vs_module.nir) {
401       /* XXX: Need more accurate error */
402       res = VK_ERROR_OUT_OF_HOST_MEMORY;
403       goto fail;
404    }
405
406    VkShaderModule vs_module_h = anv_shader_module_to_handle(&vs_module);
407
408    res = anv_CreateDescriptorSetLayout(device_h,
409       &(VkDescriptorSetLayoutCreateInfo) {
410          .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
411          .bindingCount = 1,
412          .pBindings = (VkDescriptorSetLayoutBinding[]) {
413             {
414                .binding = 0,
415                .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
416                .descriptorCount = 1,
417                .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
418             },
419          },
420       },
421       alloc,
422       &device->meta_state.resolve.ds_layout);
423    if (res != VK_SUCCESS)
424       goto fail;
425
426    res = anv_CreatePipelineLayout(device_h,
427       &(VkPipelineLayoutCreateInfo) {
428          .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
429          .setLayoutCount = 1,
430          .pSetLayouts = (VkDescriptorSetLayout[]) {
431             device->meta_state.resolve.ds_layout,
432          },
433       },
434       alloc,
435       &device->meta_state.resolve.pipeline_layout);
436    if (res != VK_SUCCESS)
437       goto fail;
438
439    res = create_pass(device);
440    if (res != VK_SUCCESS)
441       goto fail;
442
443    for (uint32_t i = 0;
444         i < ARRAY_SIZE(device->meta_state.resolve.pipelines); ++i) {
445
446       uint32_t sample_count = 1 << (1 + i);
447       if (!(sample_count_mask & sample_count))
448          continue;
449
450       res = create_pipeline(device, sample_count, vs_module_h);
451       if (res != VK_SUCCESS)
452          goto fail;
453    }
454
455    goto cleanup;
456
457 fail:
458    anv_device_finish_meta_resolve_state(device);
459
460 cleanup:
461    ralloc_free(vs_module.nir);
462
463    return res;
464 }
465
466 static void
467 emit_resolve(struct anv_cmd_buffer *cmd_buffer,
468              struct anv_image_view *src_iview,
469              const VkOffset2D *src_offset,
470              struct anv_image_view *dest_iview,
471              const VkOffset2D *dest_offset,
472              const VkExtent2D *resolve_extent)
473 {
474    struct anv_device *device = cmd_buffer->device;
475    VkDevice device_h = anv_device_to_handle(device);
476    VkCommandBuffer cmd_buffer_h = anv_cmd_buffer_to_handle(cmd_buffer);
477    const struct anv_image *src_image = src_iview->image;
478
479    const struct vertex_attrs vertex_data[3] = {
480       {
481          .vue_header = {0},
482          .position = {
483             dest_offset->x + resolve_extent->width,
484             dest_offset->y + resolve_extent->height,
485          },
486          .tex_position = {
487             src_offset->x + resolve_extent->width,
488             src_offset->y + resolve_extent->height,
489          },
490       },
491       {
492          .vue_header = {0},
493          .position = {
494             dest_offset->x,
495             dest_offset->y + resolve_extent->height,
496          },
497          .tex_position = {
498             src_offset->x,
499             src_offset->y + resolve_extent->height,
500          },
501       },
502       {
503          .vue_header = {0},
504          .position = {
505             dest_offset->x,
506             dest_offset->y,
507          },
508          .tex_position = {
509             src_offset->x,
510             src_offset->y,
511          },
512       },
513    };
514
515    struct anv_state vertex_mem =
516       anv_cmd_buffer_emit_dynamic(cmd_buffer, vertex_data,
517                                   sizeof(vertex_data), 16);
518
519    struct anv_buffer vertex_buffer = {
520       .device = device,
521       .size = sizeof(vertex_data),
522       .bo = &cmd_buffer->dynamic_state_stream.block_pool->bo,
523       .offset = vertex_mem.offset,
524    };
525
526    VkBuffer vertex_buffer_h = anv_buffer_to_handle(&vertex_buffer);
527
528    anv_CmdBindVertexBuffers(cmd_buffer_h,
529       /*firstBinding*/ 0,
530       /*bindingCount*/ 1,
531       (VkBuffer[]) { vertex_buffer_h },
532       (VkDeviceSize[]) { 0 });
533
534    VkSampler sampler_h;
535    ANV_CALL(CreateSampler)(device_h,
536       &(VkSamplerCreateInfo) {
537          .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
538          .magFilter = VK_FILTER_NEAREST,
539          .minFilter = VK_FILTER_NEAREST,
540          .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
541          .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
542          .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
543          .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
544          .mipLodBias = 0.0,
545          .anisotropyEnable = false,
546          .compareEnable = false,
547          .minLod = 0.0,
548          .maxLod = 0.0,
549          .unnormalizedCoordinates = false,
550       },
551       &cmd_buffer->pool->alloc,
552       &sampler_h);
553
554    VkDescriptorPool desc_pool;
555    anv_CreateDescriptorPool(anv_device_to_handle(device),
556       &(const VkDescriptorPoolCreateInfo) {
557          .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
558          .pNext = NULL,
559          .flags = 0,
560          .maxSets = 1,
561          .poolSizeCount = 1,
562          .pPoolSizes = (VkDescriptorPoolSize[]) {
563             {
564                .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
565                .descriptorCount = 1
566             },
567          }
568       }, &cmd_buffer->pool->alloc, &desc_pool);
569
570    VkDescriptorSet desc_set_h;
571    anv_AllocateDescriptorSets(device_h,
572       &(VkDescriptorSetAllocateInfo) {
573          .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
574          .descriptorPool = desc_pool,
575          .descriptorSetCount = 1,
576          .pSetLayouts = (VkDescriptorSetLayout[]) {
577             device->meta_state.resolve.ds_layout,
578          },
579       },
580       &desc_set_h);
581
582    anv_UpdateDescriptorSets(device_h,
583       /*writeCount*/ 1,
584       (VkWriteDescriptorSet[]) {
585          {
586             .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
587             .dstSet = desc_set_h,
588             .dstBinding = 0,
589             .dstArrayElement = 0,
590             .descriptorCount = 1,
591             .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
592             .pImageInfo = (VkDescriptorImageInfo[]) {
593                {
594                   .sampler = sampler_h,
595                   .imageView = anv_image_view_to_handle(src_iview),
596                   .imageLayout = VK_IMAGE_LAYOUT_GENERAL,
597                },
598             },
599          },
600       },
601       /*copyCount*/ 0,
602       /*copies */ NULL);
603
604    VkPipeline pipeline_h = *get_pipeline_h(device, src_image->samples);
605    ANV_FROM_HANDLE(anv_pipeline, pipeline, pipeline_h);
606
607    if (cmd_buffer->state.pipeline != pipeline) {
608       anv_CmdBindPipeline(cmd_buffer_h, VK_PIPELINE_BIND_POINT_GRAPHICS,
609                           pipeline_h);
610    }
611
612    anv_CmdBindDescriptorSets(cmd_buffer_h,
613       VK_PIPELINE_BIND_POINT_GRAPHICS,
614       device->meta_state.resolve.pipeline_layout,
615       /*firstSet*/ 0,
616       /* setCount */ 1,
617       (VkDescriptorSet[]) {
618          desc_set_h,
619       },
620       /*copyCount*/ 0,
621       /*copies */ NULL);
622
623    ANV_CALL(CmdDraw)(cmd_buffer_h, 3, 1, 0, 0);
624
625    /* All objects below are consumed by the draw call. We may safely destroy
626     * them.
627     */
628    anv_DestroyDescriptorPool(anv_device_to_handle(device),
629                              desc_pool, &cmd_buffer->pool->alloc);
630    anv_DestroySampler(device_h, sampler_h,
631                       &cmd_buffer->pool->alloc);
632 }
633
634 void anv_CmdResolveImage(
635     VkCommandBuffer                             cmd_buffer_h,
636     VkImage                                     src_image_h,
637     VkImageLayout                               src_image_layout,
638     VkImage                                     dest_image_h,
639     VkImageLayout                               dest_image_layout,
640     uint32_t                                    region_count,
641     const VkImageResolve*                       regions)
642 {
643    ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, cmd_buffer_h);
644    ANV_FROM_HANDLE(anv_image, src_image, src_image_h);
645    ANV_FROM_HANDLE(anv_image, dest_image, dest_image_h);
646    struct anv_device *device = cmd_buffer->device;
647    struct anv_meta_saved_state state;
648    VkDevice device_h = anv_device_to_handle(device);
649
650    meta_resolve_save(&state, cmd_buffer);
651
652    assert(src_image->samples > 1);
653    assert(dest_image->samples == 1);
654
655    if (src_image->samples >= 16) {
656       /* See commit aa3f9aaf31e9056a255f9e0472ebdfdaa60abe54 for the
657        * glBlitFramebuffer workaround for samples >= 16.
658        */
659       anv_finishme("vkCmdResolveImage: need interpolation workaround when "
660                    "samples >= 16");
661    }
662
663    if (src_image->array_size > 1)
664       anv_finishme("vkCmdResolveImage: multisample array images");
665
666    for (uint32_t r = 0; r < region_count; ++r) {
667       const VkImageResolve *region = &regions[r];
668
669       /* From the Vulkan 1.0 spec:
670        *
671        *    - The aspectMask member of srcSubresource and dstSubresource must
672        *      only contain VK_IMAGE_ASPECT_COLOR_BIT
673        *
674        *    - The layerCount member of srcSubresource and dstSubresource must
675        *      match
676        */
677       assert(region->srcSubresource.aspectMask == VK_IMAGE_ASPECT_COLOR_BIT);
678       assert(region->dstSubresource.aspectMask == VK_IMAGE_ASPECT_COLOR_BIT);
679       assert(region->srcSubresource.layerCount ==
680              region->dstSubresource.layerCount);
681
682       const uint32_t src_base_layer =
683          anv_meta_get_iview_layer(src_image, &region->srcSubresource,
684                                   &region->srcOffset);
685
686       const uint32_t dest_base_layer =
687          anv_meta_get_iview_layer(dest_image, &region->dstSubresource,
688                                   &region->dstOffset);
689
690       /**
691        * From Vulkan 1.0.6 spec: 18.6 Resolving Multisample Images
692        *
693        *    extent is the size in texels of the source image to resolve in width,
694        *    height and depth. 1D images use only x and width. 2D images use x, y,
695        *    width and height. 3D images use x, y, z, width, height and depth.
696        *
697        *    srcOffset and dstOffset select the initial x, y, and z offsets in
698        *    texels of the sub-regions of the source and destination image data.
699        *    extent is the size in texels of the source image to resolve in width,
700        *    height and depth. 1D images use only x and width. 2D images use x, y,
701        *    width and height. 3D images use x, y, z, width, height and depth.
702        */
703       const struct VkExtent3D extent =
704          anv_sanitize_image_extent(src_image->type, region->extent);
705       const struct VkOffset3D srcOffset =
706          anv_sanitize_image_offset(src_image->type, region->srcOffset);
707       const struct VkOffset3D dstOffset =
708          anv_sanitize_image_offset(dest_image->type, region->dstOffset);
709
710
711       for (uint32_t layer = 0; layer < region->srcSubresource.layerCount;
712            ++layer) {
713
714          struct anv_image_view src_iview;
715          anv_image_view_init(&src_iview, cmd_buffer->device,
716             &(VkImageViewCreateInfo) {
717                .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
718                .image = src_image_h,
719                .viewType = anv_meta_get_view_type(src_image),
720                .format = src_image->vk_format,
721                .subresourceRange = {
722                   .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
723                   .baseMipLevel = region->srcSubresource.mipLevel,
724                   .levelCount = 1,
725                   .baseArrayLayer = src_base_layer + layer,
726                   .layerCount = 1,
727                },
728             },
729             cmd_buffer, VK_IMAGE_USAGE_SAMPLED_BIT);
730
731          struct anv_image_view dest_iview;
732          anv_image_view_init(&dest_iview, cmd_buffer->device,
733             &(VkImageViewCreateInfo) {
734                .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
735                .image = dest_image_h,
736                .viewType = anv_meta_get_view_type(dest_image),
737                .format = dest_image->vk_format,
738                .subresourceRange = {
739                   .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
740                   .baseMipLevel = region->dstSubresource.mipLevel,
741                   .levelCount = 1,
742                   .baseArrayLayer = dest_base_layer + layer,
743                   .layerCount = 1,
744                },
745             },
746             cmd_buffer, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
747
748          VkFramebuffer fb_h;
749          anv_CreateFramebuffer(device_h,
750             &(VkFramebufferCreateInfo) {
751                .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
752                .attachmentCount = 1,
753                .pAttachments = (VkImageView[]) {
754                   anv_image_view_to_handle(&dest_iview),
755                },
756                .width = anv_minify(dest_image->extent.width,
757                                    region->dstSubresource.mipLevel),
758                .height = anv_minify(dest_image->extent.height,
759                                     region->dstSubresource.mipLevel),
760                .layers = 1
761             },
762             &cmd_buffer->pool->alloc,
763             &fb_h);
764
765          ANV_CALL(CmdBeginRenderPass)(cmd_buffer_h,
766             &(VkRenderPassBeginInfo) {
767                .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
768                .renderPass = device->meta_state.resolve.pass,
769                .framebuffer = fb_h,
770                .renderArea = {
771                   .offset = {
772                      dstOffset.x,
773                      dstOffset.y,
774                   },
775                   .extent = {
776                      extent.width,
777                      extent.height,
778                   }
779                },
780                .clearValueCount = 0,
781                .pClearValues = NULL,
782             },
783             VK_SUBPASS_CONTENTS_INLINE);
784
785          emit_resolve(cmd_buffer,
786              &src_iview,
787              &(VkOffset2D) {
788                .x = srcOffset.x,
789                .y = srcOffset.y,
790              },
791              &dest_iview,
792              &(VkOffset2D) {
793                .x = dstOffset.x,
794                .y = dstOffset.y,
795              },
796              &(VkExtent2D) {
797                .width = extent.width,
798                .height = extent.height,
799              });
800
801          ANV_CALL(CmdEndRenderPass)(cmd_buffer_h);
802
803          anv_DestroyFramebuffer(device_h, fb_h,
804                                 &cmd_buffer->pool->alloc);
805       }
806    }
807
808    meta_resolve_restore(&state, cmd_buffer);
809 }
810
811 /**
812  * Emit any needed resolves for the current subpass.
813  */
814 void
815 anv_cmd_buffer_resolve_subpass(struct anv_cmd_buffer *cmd_buffer)
816 {
817    struct anv_framebuffer *fb = cmd_buffer->state.framebuffer;
818    struct anv_subpass *subpass = cmd_buffer->state.subpass;
819    struct anv_meta_saved_state saved_state;
820
821    /* FINISHME(perf): Skip clears for resolve attachments.
822     *
823     * From the Vulkan 1.0 spec:
824     *
825     *    If the first use of an attachment in a render pass is as a resolve
826     *    attachment, then the loadOp is effectively ignored as the resolve is
827     *    guaranteed to overwrite all pixels in the render area.
828     */
829
830    if (!subpass->has_resolve)
831       return;
832
833    meta_resolve_save(&saved_state, cmd_buffer);
834
835    for (uint32_t i = 0; i < subpass->color_count; ++i) {
836       uint32_t src_att = subpass->color_attachments[i];
837       uint32_t dest_att = subpass->resolve_attachments[i];
838
839       if (dest_att == VK_ATTACHMENT_UNUSED)
840          continue;
841
842       struct anv_image_view *src_iview = fb->attachments[src_att];
843       struct anv_image_view *dest_iview = fb->attachments[dest_att];
844
845       struct anv_subpass resolve_subpass = {
846          .color_count = 1,
847          .color_attachments = (uint32_t[]) { dest_att },
848          .depth_stencil_attachment = VK_ATTACHMENT_UNUSED,
849       };
850
851       anv_cmd_buffer_set_subpass(cmd_buffer, &resolve_subpass);
852
853       /* Subpass resolves must respect the render area. We can ignore the
854        * render area here because vkCmdBeginRenderPass set the render area
855        * with 3DSTATE_DRAWING_RECTANGLE.
856        *
857        * XXX(chadv): Does the hardware really respect
858        * 3DSTATE_DRAWING_RECTANGLE when draing a 3DPRIM_RECTLIST?
859        */
860       emit_resolve(cmd_buffer,
861           src_iview,
862           &(VkOffset2D) { 0, 0 },
863           dest_iview,
864           &(VkOffset2D) { 0, 0 },
865           &(VkExtent2D) { fb->width, fb->height });
866    }
867
868    cmd_buffer->state.subpass = subpass;
869    meta_resolve_restore(&saved_state, cmd_buffer);
870 }