OSDN Git Service

anv: Add a struct for storing a compiled shader
[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, 32, "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, 0xf);
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, 0);
119 }
120
121 static void
122 meta_emit_blit(struct anv_cmd_buffer *cmd_buffer,
123                struct anv_image *src_image,
124                struct anv_image_view *src_iview,
125                VkOffset3D src_offset,
126                VkExtent3D src_extent,
127                struct anv_image *dest_image,
128                struct anv_image_view *dest_iview,
129                VkOffset3D dest_offset,
130                VkExtent3D dest_extent,
131                VkFilter blit_filter)
132 {
133    struct anv_device *device = cmd_buffer->device;
134
135    struct blit_vb_data {
136       float pos[2];
137       float tex_coord[3];
138    } *vb_data;
139
140    assert(src_image->samples == dest_image->samples);
141
142    unsigned vb_size = sizeof(struct anv_vue_header) + 3 * sizeof(*vb_data);
143
144    struct anv_state vb_state =
145       anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, vb_size, 16);
146    memset(vb_state.map, 0, sizeof(struct anv_vue_header));
147    vb_data = vb_state.map + sizeof(struct anv_vue_header);
148
149    vb_data[0] = (struct blit_vb_data) {
150       .pos = {
151          dest_offset.x + dest_extent.width,
152          dest_offset.y + dest_extent.height,
153       },
154       .tex_coord = {
155          (float)(src_offset.x + src_extent.width)
156             / (float)src_iview->extent.width,
157          (float)(src_offset.y + src_extent.height)
158             / (float)src_iview->extent.height,
159          (float)src_offset.z / (float)src_iview->extent.depth,
160       },
161    };
162
163    vb_data[1] = (struct blit_vb_data) {
164       .pos = {
165          dest_offset.x,
166          dest_offset.y + dest_extent.height,
167       },
168       .tex_coord = {
169          (float)src_offset.x / (float)src_iview->extent.width,
170          (float)(src_offset.y + src_extent.height) /
171             (float)src_iview->extent.height,
172          (float)src_offset.z / (float)src_iview->extent.depth,
173       },
174    };
175
176    vb_data[2] = (struct blit_vb_data) {
177       .pos = {
178          dest_offset.x,
179          dest_offset.y,
180       },
181       .tex_coord = {
182          (float)src_offset.x / (float)src_iview->extent.width,
183          (float)src_offset.y / (float)src_iview->extent.height,
184          (float)src_offset.z / (float)src_iview->extent.depth,
185       },
186    };
187
188    if (!device->info.has_llc)
189       anv_state_clflush(vb_state);
190
191    struct anv_buffer vertex_buffer = {
192       .device = device,
193       .size = vb_size,
194       .bo = &device->dynamic_state_block_pool.bo,
195       .offset = vb_state.offset,
196    };
197
198    anv_CmdBindVertexBuffers(anv_cmd_buffer_to_handle(cmd_buffer), 0, 2,
199       (VkBuffer[]) {
200          anv_buffer_to_handle(&vertex_buffer),
201          anv_buffer_to_handle(&vertex_buffer)
202       },
203       (VkDeviceSize[]) {
204          0,
205          sizeof(struct anv_vue_header),
206       });
207
208    VkSampler sampler;
209    ANV_CALL(CreateSampler)(anv_device_to_handle(device),
210       &(VkSamplerCreateInfo) {
211          .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
212          .magFilter = blit_filter,
213          .minFilter = blit_filter,
214          .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
215          .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
216          .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
217       }, &cmd_buffer->pool->alloc, &sampler);
218
219    VkDescriptorPool desc_pool;
220    anv_CreateDescriptorPool(anv_device_to_handle(device),
221       &(const VkDescriptorPoolCreateInfo) {
222          .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
223          .pNext = NULL,
224          .flags = 0,
225          .maxSets = 1,
226          .poolSizeCount = 1,
227          .pPoolSizes = (VkDescriptorPoolSize[]) {
228             {
229                .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
230                .descriptorCount = 1
231             },
232          }
233       }, &cmd_buffer->pool->alloc, &desc_pool);
234
235    VkDescriptorSet set;
236    anv_AllocateDescriptorSets(anv_device_to_handle(device),
237       &(VkDescriptorSetAllocateInfo) {
238          .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
239          .descriptorPool = desc_pool,
240          .descriptorSetCount = 1,
241          .pSetLayouts = &device->meta_state.blit.ds_layout
242       }, &set);
243
244    anv_UpdateDescriptorSets(anv_device_to_handle(device),
245       1, /* writeCount */
246       (VkWriteDescriptorSet[]) {
247          {
248             .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
249             .dstSet = set,
250             .dstBinding = 0,
251             .dstArrayElement = 0,
252             .descriptorCount = 1,
253             .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
254             .pImageInfo = (VkDescriptorImageInfo[]) {
255                {
256                   .sampler = sampler,
257                   .imageView = anv_image_view_to_handle(src_iview),
258                   .imageLayout = VK_IMAGE_LAYOUT_GENERAL,
259                },
260             }
261          }
262       }, 0, NULL);
263
264    VkFramebuffer fb;
265    anv_CreateFramebuffer(anv_device_to_handle(device),
266       &(VkFramebufferCreateInfo) {
267          .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
268          .attachmentCount = 1,
269          .pAttachments = (VkImageView[]) {
270             anv_image_view_to_handle(dest_iview),
271          },
272          .width = dest_iview->extent.width,
273          .height = dest_iview->extent.height,
274          .layers = 1
275       }, &cmd_buffer->pool->alloc, &fb);
276
277    ANV_CALL(CmdBeginRenderPass)(anv_cmd_buffer_to_handle(cmd_buffer),
278       &(VkRenderPassBeginInfo) {
279          .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
280          .renderPass = device->meta_state.blit.render_pass,
281          .framebuffer = fb,
282          .renderArea = {
283             .offset = { dest_offset.x, dest_offset.y },
284             .extent = { dest_extent.width, dest_extent.height },
285          },
286          .clearValueCount = 0,
287          .pClearValues = NULL,
288       }, VK_SUBPASS_CONTENTS_INLINE);
289
290    VkPipeline pipeline;
291
292    switch (src_image->type) {
293    case VK_IMAGE_TYPE_1D:
294       pipeline = device->meta_state.blit.pipeline_1d_src;
295       break;
296    case VK_IMAGE_TYPE_2D:
297       pipeline = device->meta_state.blit.pipeline_2d_src;
298       break;
299    case VK_IMAGE_TYPE_3D:
300       pipeline = device->meta_state.blit.pipeline_3d_src;
301       break;
302    default:
303       unreachable(!"bad VkImageType");
304    }
305
306    if (cmd_buffer->state.pipeline != anv_pipeline_from_handle(pipeline)) {
307       anv_CmdBindPipeline(anv_cmd_buffer_to_handle(cmd_buffer),
308                           VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
309    }
310
311    anv_CmdBindDescriptorSets(anv_cmd_buffer_to_handle(cmd_buffer),
312                              VK_PIPELINE_BIND_POINT_GRAPHICS,
313                              device->meta_state.blit.pipeline_layout, 0, 1,
314                              &set, 0, NULL);
315
316    ANV_CALL(CmdDraw)(anv_cmd_buffer_to_handle(cmd_buffer), 3, 1, 0, 0);
317
318    ANV_CALL(CmdEndRenderPass)(anv_cmd_buffer_to_handle(cmd_buffer));
319
320    /* At the point where we emit the draw call, all data from the
321     * descriptor sets, etc. has been used.  We are free to delete it.
322     */
323    anv_DestroyDescriptorPool(anv_device_to_handle(device),
324                              desc_pool, &cmd_buffer->pool->alloc);
325    anv_DestroySampler(anv_device_to_handle(device), sampler,
326                       &cmd_buffer->pool->alloc);
327    anv_DestroyFramebuffer(anv_device_to_handle(device), fb,
328                           &cmd_buffer->pool->alloc);
329 }
330
331 static void
332 meta_finish_blit(struct anv_cmd_buffer *cmd_buffer,
333                  const struct anv_meta_saved_state *saved_state)
334 {
335    anv_meta_restore(saved_state, cmd_buffer);
336 }
337
338 void anv_CmdBlitImage(
339     VkCommandBuffer                             commandBuffer,
340     VkImage                                     srcImage,
341     VkImageLayout                               srcImageLayout,
342     VkImage                                     destImage,
343     VkImageLayout                               destImageLayout,
344     uint32_t                                    regionCount,
345     const VkImageBlit*                          pRegions,
346     VkFilter                                    filter)
347
348 {
349    ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
350    ANV_FROM_HANDLE(anv_image, src_image, srcImage);
351    ANV_FROM_HANDLE(anv_image, dest_image, destImage);
352    struct anv_meta_saved_state saved_state;
353
354    /* From the Vulkan 1.0 spec:
355     *
356     *    vkCmdBlitImage must not be used for multisampled source or
357     *    destination images. Use vkCmdResolveImage for this purpose.
358     */
359    assert(src_image->samples == 1);
360    assert(dest_image->samples == 1);
361
362    meta_prepare_blit(cmd_buffer, &saved_state);
363
364    for (unsigned r = 0; r < regionCount; r++) {
365       struct anv_image_view src_iview;
366       anv_image_view_init(&src_iview, cmd_buffer->device,
367          &(VkImageViewCreateInfo) {
368             .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
369             .image = srcImage,
370             .viewType = anv_meta_get_view_type(src_image),
371             .format = src_image->vk_format,
372             .subresourceRange = {
373                .aspectMask = pRegions[r].srcSubresource.aspectMask,
374                .baseMipLevel = pRegions[r].srcSubresource.mipLevel,
375                .levelCount = 1,
376                .baseArrayLayer = pRegions[r].srcSubresource.baseArrayLayer,
377                .layerCount = 1
378             },
379          },
380          cmd_buffer, VK_IMAGE_USAGE_SAMPLED_BIT);
381
382       const VkOffset3D dest_offset = {
383          .x = pRegions[r].dstOffsets[0].x,
384          .y = pRegions[r].dstOffsets[0].y,
385          .z = 0,
386       };
387
388       if (pRegions[r].dstOffsets[1].x < pRegions[r].dstOffsets[0].x ||
389           pRegions[r].dstOffsets[1].y < pRegions[r].dstOffsets[0].y ||
390           pRegions[r].srcOffsets[1].x < pRegions[r].srcOffsets[0].x ||
391           pRegions[r].srcOffsets[1].y < pRegions[r].srcOffsets[0].y)
392          anv_finishme("FINISHME: Allow flipping in blits");
393
394       const VkExtent3D dest_extent = {
395          .width = pRegions[r].dstOffsets[1].x - pRegions[r].dstOffsets[0].x,
396          .height = pRegions[r].dstOffsets[1].y - pRegions[r].dstOffsets[0].y,
397       };
398
399       const VkExtent3D src_extent = {
400          .width = pRegions[r].srcOffsets[1].x - pRegions[r].srcOffsets[0].x,
401          .height = pRegions[r].srcOffsets[1].y - pRegions[r].srcOffsets[0].y,
402       };
403
404       const uint32_t dest_array_slice =
405          anv_meta_get_iview_layer(dest_image, &pRegions[r].dstSubresource,
406                                   &pRegions[r].dstOffsets[0]);
407
408       if (pRegions[r].srcSubresource.layerCount > 1)
409          anv_finishme("FINISHME: copy multiple array layers");
410
411       if (pRegions[r].srcOffsets[0].z + 1 != pRegions[r].srcOffsets[1].z ||
412           pRegions[r].dstOffsets[0].z + 1 != pRegions[r].dstOffsets[1].z)
413          anv_finishme("FINISHME: copy multiple depth layers");
414
415       struct anv_image_view dest_iview;
416       anv_image_view_init(&dest_iview, cmd_buffer->device,
417          &(VkImageViewCreateInfo) {
418             .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
419             .image = destImage,
420             .viewType = anv_meta_get_view_type(dest_image),
421             .format = dest_image->vk_format,
422             .subresourceRange = {
423                .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
424                .baseMipLevel = pRegions[r].dstSubresource.mipLevel,
425                .levelCount = 1,
426                .baseArrayLayer = dest_array_slice,
427                .layerCount = 1
428             },
429          },
430          cmd_buffer, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
431
432       meta_emit_blit(cmd_buffer,
433                      src_image, &src_iview,
434                      pRegions[r].srcOffsets[0], src_extent,
435                      dest_image, &dest_iview,
436                      dest_offset, dest_extent,
437                      filter);
438    }
439
440    meta_finish_blit(cmd_buffer, &saved_state);
441 }
442
443 void
444 anv_device_finish_meta_blit_state(struct anv_device *device)
445 {
446    anv_DestroyRenderPass(anv_device_to_handle(device),
447                          device->meta_state.blit.render_pass,
448                          &device->meta_state.alloc);
449    anv_DestroyPipeline(anv_device_to_handle(device),
450                        device->meta_state.blit.pipeline_1d_src,
451                        &device->meta_state.alloc);
452    anv_DestroyPipeline(anv_device_to_handle(device),
453                        device->meta_state.blit.pipeline_2d_src,
454                        &device->meta_state.alloc);
455    anv_DestroyPipeline(anv_device_to_handle(device),
456                        device->meta_state.blit.pipeline_3d_src,
457                        &device->meta_state.alloc);
458    anv_DestroyPipelineLayout(anv_device_to_handle(device),
459                              device->meta_state.blit.pipeline_layout,
460                              &device->meta_state.alloc);
461    anv_DestroyDescriptorSetLayout(anv_device_to_handle(device),
462                                   device->meta_state.blit.ds_layout,
463                                   &device->meta_state.alloc);
464 }
465
466 VkResult
467 anv_device_init_meta_blit_state(struct anv_device *device)
468 {
469    VkResult result;
470
471    result = anv_CreateRenderPass(anv_device_to_handle(device),
472       &(VkRenderPassCreateInfo) {
473          .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
474          .attachmentCount = 1,
475          .pAttachments = &(VkAttachmentDescription) {
476             .format = VK_FORMAT_UNDEFINED, /* Our shaders don't care */
477             .loadOp = VK_ATTACHMENT_LOAD_OP_LOAD,
478             .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
479             .initialLayout = VK_IMAGE_LAYOUT_GENERAL,
480             .finalLayout = VK_IMAGE_LAYOUT_GENERAL,
481          },
482          .subpassCount = 1,
483          .pSubpasses = &(VkSubpassDescription) {
484             .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
485             .inputAttachmentCount = 0,
486             .colorAttachmentCount = 1,
487             .pColorAttachments = &(VkAttachmentReference) {
488                .attachment = 0,
489                .layout = VK_IMAGE_LAYOUT_GENERAL,
490             },
491             .pResolveAttachments = NULL,
492             .pDepthStencilAttachment = &(VkAttachmentReference) {
493                .attachment = VK_ATTACHMENT_UNUSED,
494                .layout = VK_IMAGE_LAYOUT_GENERAL,
495             },
496             .preserveAttachmentCount = 1,
497             .pPreserveAttachments = (uint32_t[]) { 0 },
498          },
499          .dependencyCount = 0,
500       }, &device->meta_state.alloc, &device->meta_state.blit.render_pass);
501    if (result != VK_SUCCESS)
502       goto fail;
503
504    /* We don't use a vertex shader for blitting, but instead build and pass
505     * the VUEs directly to the rasterization backend.  However, we do need
506     * to provide GLSL source for the vertex shader so that the compiler
507     * does not dead-code our inputs.
508     */
509    struct anv_shader_module vs = {
510       .nir = build_nir_vertex_shader(),
511    };
512
513    struct anv_shader_module fs_1d = {
514       .nir = build_nir_copy_fragment_shader(GLSL_SAMPLER_DIM_1D),
515    };
516
517    struct anv_shader_module fs_2d = {
518       .nir = build_nir_copy_fragment_shader(GLSL_SAMPLER_DIM_2D),
519    };
520
521    struct anv_shader_module fs_3d = {
522       .nir = build_nir_copy_fragment_shader(GLSL_SAMPLER_DIM_3D),
523    };
524
525    VkPipelineVertexInputStateCreateInfo vi_create_info = {
526       .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
527       .vertexBindingDescriptionCount = 2,
528       .pVertexBindingDescriptions = (VkVertexInputBindingDescription[]) {
529          {
530             .binding = 0,
531             .stride = 0,
532             .inputRate = VK_VERTEX_INPUT_RATE_INSTANCE
533          },
534          {
535             .binding = 1,
536             .stride = 5 * sizeof(float),
537             .inputRate = VK_VERTEX_INPUT_RATE_VERTEX
538          },
539       },
540       .vertexAttributeDescriptionCount = 3,
541       .pVertexAttributeDescriptions = (VkVertexInputAttributeDescription[]) {
542          {
543             /* VUE Header */
544             .location = 0,
545             .binding = 0,
546             .format = VK_FORMAT_R32G32B32A32_UINT,
547             .offset = 0
548          },
549          {
550             /* Position */
551             .location = 1,
552             .binding = 1,
553             .format = VK_FORMAT_R32G32_SFLOAT,
554             .offset = 0
555          },
556          {
557             /* Texture Coordinate */
558             .location = 2,
559             .binding = 1,
560             .format = VK_FORMAT_R32G32B32_SFLOAT,
561             .offset = 8
562          }
563       }
564    };
565
566    VkDescriptorSetLayoutCreateInfo ds_layout_info = {
567       .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
568       .bindingCount = 1,
569       .pBindings = (VkDescriptorSetLayoutBinding[]) {
570          {
571             .binding = 0,
572             .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
573             .descriptorCount = 1,
574             .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
575             .pImmutableSamplers = NULL
576          },
577       }
578    };
579    result = anv_CreateDescriptorSetLayout(anv_device_to_handle(device),
580                                           &ds_layout_info,
581                                           &device->meta_state.alloc,
582                                           &device->meta_state.blit.ds_layout);
583    if (result != VK_SUCCESS)
584       goto fail_render_pass;
585
586    result = anv_CreatePipelineLayout(anv_device_to_handle(device),
587       &(VkPipelineLayoutCreateInfo) {
588          .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
589          .setLayoutCount = 1,
590          .pSetLayouts = &device->meta_state.blit.ds_layout,
591       },
592       &device->meta_state.alloc, &device->meta_state.blit.pipeline_layout);
593    if (result != VK_SUCCESS)
594       goto fail_descriptor_set_layout;
595
596    VkPipelineShaderStageCreateInfo pipeline_shader_stages[] = {
597       {
598          .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
599          .stage = VK_SHADER_STAGE_VERTEX_BIT,
600          .module = anv_shader_module_to_handle(&vs),
601          .pName = "main",
602          .pSpecializationInfo = NULL
603       }, {
604          .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
605          .stage = VK_SHADER_STAGE_FRAGMENT_BIT,
606          .module = VK_NULL_HANDLE, /* TEMPLATE VALUE! FILL ME IN! */
607          .pName = "main",
608          .pSpecializationInfo = NULL
609       },
610    };
611
612    const VkGraphicsPipelineCreateInfo vk_pipeline_info = {
613       .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
614       .stageCount = ARRAY_SIZE(pipeline_shader_stages),
615       .pStages = pipeline_shader_stages,
616       .pVertexInputState = &vi_create_info,
617       .pInputAssemblyState = &(VkPipelineInputAssemblyStateCreateInfo) {
618          .sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
619          .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
620          .primitiveRestartEnable = false,
621       },
622       .pViewportState = &(VkPipelineViewportStateCreateInfo) {
623          .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
624          .viewportCount = 1,
625          .scissorCount = 1,
626       },
627       .pRasterizationState = &(VkPipelineRasterizationStateCreateInfo) {
628          .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
629          .rasterizerDiscardEnable = false,
630          .polygonMode = VK_POLYGON_MODE_FILL,
631          .cullMode = VK_CULL_MODE_NONE,
632          .frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE
633       },
634       .pMultisampleState = &(VkPipelineMultisampleStateCreateInfo) {
635          .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
636          .rasterizationSamples = 1,
637          .sampleShadingEnable = false,
638          .pSampleMask = (VkSampleMask[]) { UINT32_MAX },
639       },
640       .pColorBlendState = &(VkPipelineColorBlendStateCreateInfo) {
641          .sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
642          .attachmentCount = 1,
643          .pAttachments = (VkPipelineColorBlendAttachmentState []) {
644             { .colorWriteMask =
645                  VK_COLOR_COMPONENT_A_BIT |
646                  VK_COLOR_COMPONENT_R_BIT |
647                  VK_COLOR_COMPONENT_G_BIT |
648                  VK_COLOR_COMPONENT_B_BIT },
649          }
650       },
651       .pDynamicState = &(VkPipelineDynamicStateCreateInfo) {
652          .sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
653          .dynamicStateCount = 9,
654          .pDynamicStates = (VkDynamicState[]) {
655             VK_DYNAMIC_STATE_VIEWPORT,
656             VK_DYNAMIC_STATE_SCISSOR,
657             VK_DYNAMIC_STATE_LINE_WIDTH,
658             VK_DYNAMIC_STATE_DEPTH_BIAS,
659             VK_DYNAMIC_STATE_BLEND_CONSTANTS,
660             VK_DYNAMIC_STATE_DEPTH_BOUNDS,
661             VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK,
662             VK_DYNAMIC_STATE_STENCIL_WRITE_MASK,
663             VK_DYNAMIC_STATE_STENCIL_REFERENCE,
664          },
665       },
666       .flags = 0,
667       .layout = device->meta_state.blit.pipeline_layout,
668       .renderPass = device->meta_state.blit.render_pass,
669       .subpass = 0,
670    };
671
672    const struct anv_graphics_pipeline_create_info anv_pipeline_info = {
673       .color_attachment_count = -1,
674       .use_repclear = false,
675       .disable_vs = true,
676       .use_rectlist = true
677    };
678
679    pipeline_shader_stages[1].module = anv_shader_module_to_handle(&fs_1d);
680    result = anv_graphics_pipeline_create(anv_device_to_handle(device),
681       VK_NULL_HANDLE,
682       &vk_pipeline_info, &anv_pipeline_info,
683       &device->meta_state.alloc, &device->meta_state.blit.pipeline_1d_src);
684    if (result != VK_SUCCESS)
685       goto fail_pipeline_layout;
686
687    pipeline_shader_stages[1].module = anv_shader_module_to_handle(&fs_2d);
688    result = anv_graphics_pipeline_create(anv_device_to_handle(device),
689       VK_NULL_HANDLE,
690       &vk_pipeline_info, &anv_pipeline_info,
691       &device->meta_state.alloc, &device->meta_state.blit.pipeline_2d_src);
692    if (result != VK_SUCCESS)
693       goto fail_pipeline_1d;
694
695    pipeline_shader_stages[1].module = anv_shader_module_to_handle(&fs_3d);
696    result = anv_graphics_pipeline_create(anv_device_to_handle(device),
697       VK_NULL_HANDLE,
698       &vk_pipeline_info, &anv_pipeline_info,
699       &device->meta_state.alloc, &device->meta_state.blit.pipeline_3d_src);
700    if (result != VK_SUCCESS)
701       goto fail_pipeline_2d;
702
703    ralloc_free(vs.nir);
704    ralloc_free(fs_1d.nir);
705    ralloc_free(fs_2d.nir);
706    ralloc_free(fs_3d.nir);
707
708    return VK_SUCCESS;
709
710  fail_pipeline_2d:
711    anv_DestroyPipeline(anv_device_to_handle(device),
712                        device->meta_state.blit.pipeline_2d_src,
713                        &device->meta_state.alloc);
714
715  fail_pipeline_1d:
716    anv_DestroyPipeline(anv_device_to_handle(device),
717                        device->meta_state.blit.pipeline_1d_src,
718                        &device->meta_state.alloc);
719
720  fail_pipeline_layout:
721    anv_DestroyPipelineLayout(anv_device_to_handle(device),
722                              device->meta_state.blit.pipeline_layout,
723                              &device->meta_state.alloc);
724  fail_descriptor_set_layout:
725    anv_DestroyDescriptorSetLayout(anv_device_to_handle(device),
726                                   device->meta_state.blit.ds_layout,
727                                   &device->meta_state.alloc);
728  fail_render_pass:
729    anv_DestroyRenderPass(anv_device_to_handle(device),
730                          device->meta_state.blit.render_pass,
731                          &device->meta_state.alloc);
732
733    ralloc_free(vs.nir);
734    ralloc_free(fs_1d.nir);
735    ralloc_free(fs_2d.nir);
736    ralloc_free(fs_3d.nir);
737  fail:
738    return result;
739 }