7424a00b7f2d87b34902676e8e5160153c92c11e
[mesa.git] / src / intel / vulkan / anv_meta_blit2d.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 "anv_meta.h"
25 #include "nir/nir_builder.h"
26
27 static VkFormat
28 vk_format_for_size(int bs)
29 {
30 /* The choice of UNORM and UINT formats is very intentional here. Most of
31 * the time, we want to use a UINT format to avoid any rounding error in
32 * the blit. For stencil blits, R8_UINT is required by the hardware.
33 * (It's the only format allowed in conjunction with W-tiling.) Also we
34 * intentionally use the 4-channel formats whenever we can. This is so
35 * that, when we do a RGB <-> RGBX copy, the two formats will line up even
36 * though one of them is 3/4 the size of the other. The choice of UNORM
37 * vs. UINT is also very intentional because Haswell doesn't handle 8 or
38 * 16-bit RGB UINT formats at all so we have to use UNORM there.
39 * Fortunately, the only time we should ever use two different formats in
40 * the table below is for RGB -> RGBA blits and so we will never have any
41 * UNORM/UINT mismatch.
42 */
43 switch (bs) {
44 case 1: return VK_FORMAT_R8_UINT;
45 case 2: return VK_FORMAT_R8G8_UINT;
46 case 3: return VK_FORMAT_R8G8B8_UNORM;
47 case 4: return VK_FORMAT_R8G8B8A8_UNORM;
48 case 6: return VK_FORMAT_R16G16B16_UNORM;
49 case 8: return VK_FORMAT_R16G16B16A16_UNORM;
50 case 12: return VK_FORMAT_R32G32B32_UINT;
51 case 16: return VK_FORMAT_R32G32B32A32_UINT;
52 default:
53 unreachable("Invalid format block size");
54 }
55 }
56
57 static void
58 create_iview(struct anv_cmd_buffer *cmd_buffer,
59 struct anv_meta_blit2d_surf *surf,
60 struct anv_meta_blit2d_rect *rect,
61 VkImageUsageFlags usage,
62 VkImage *img,
63 struct anv_image_view *iview)
64 {
65 struct isl_tile_info tile_info;
66 isl_tiling_get_info(&cmd_buffer->device->isl_dev,
67 surf->tiling, surf->bs, &tile_info);
68 const unsigned tile_width_px = tile_info.width > surf->bs ?
69 tile_info.width / surf->bs : 1;
70 uint32_t *rect_y = (usage == VK_IMAGE_USAGE_SAMPLED_BIT) ?
71 &rect->src_y : &rect->dst_y;
72 uint32_t *rect_x = (usage == VK_IMAGE_USAGE_SAMPLED_BIT) ?
73 &rect->src_x : &rect->dst_x;
74
75 /* Define the shared state among all created image views */
76 const VkImageCreateInfo image_info = {
77 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
78 .imageType = VK_IMAGE_TYPE_2D,
79 .format = vk_format_for_size(surf->bs),
80 .extent = {
81 .width = rect->width + (*rect_x) % tile_width_px,
82 .height = rect->height + (*rect_y) % tile_info.height,
83 .depth = 1,
84 },
85 .mipLevels = 1,
86 .arrayLayers = 1,
87 .samples = 1,
88 .tiling = surf->tiling == ISL_TILING_LINEAR ?
89 VK_IMAGE_TILING_LINEAR : VK_IMAGE_TILING_OPTIMAL,
90 .usage = usage,
91 };
92
93 /* Create the VkImage that is bound to the surface's memory. */
94 anv_image_create(anv_device_to_handle(cmd_buffer->device),
95 &(struct anv_image_create_info) {
96 .vk_info = &image_info,
97 .isl_tiling_flags = 1 << surf->tiling,
98 .stride = surf->pitch,
99 }, &cmd_buffer->pool->alloc, img);
100
101 /* We could use a vk call to bind memory, but that would require
102 * creating a dummy memory object etc. so there's really no point.
103 */
104 anv_image_from_handle(*img)->bo = surf->bo;
105 anv_image_from_handle(*img)->offset = surf->base_offset;
106
107 /* Create a VkImageView that starts at the tile aligned offset closest
108 * to the provided x/y offset into the surface.
109 */
110 uint32_t img_o = 0;
111 isl_surf_get_image_intratile_offset_el_xy(&cmd_buffer->device->isl_dev,
112 &anv_image_from_handle(*img)->
113 color_surface.isl,
114 *rect_x, *rect_y,
115 &img_o, rect_x, rect_y);
116 anv_image_view_init(iview, cmd_buffer->device,
117 &(VkImageViewCreateInfo) {
118 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
119 .image = *img,
120 .viewType = VK_IMAGE_VIEW_TYPE_2D,
121 .format = image_info.format,
122 .subresourceRange = {
123 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
124 .baseMipLevel = 0,
125 .levelCount = 1,
126 .baseArrayLayer = 0,
127 .layerCount = 1
128 },
129 }, cmd_buffer, img_o, usage);
130 }
131
132 struct blit2d_src_temps {
133 VkImage image;
134 struct anv_image_view iview;
135 VkDescriptorPool desc_pool;
136 VkDescriptorSet set;
137 };
138
139 static void
140 blit2d_bind_src(struct anv_cmd_buffer *cmd_buffer,
141 struct anv_meta_blit2d_surf *src,
142 struct anv_meta_blit2d_rect *rect,
143 struct blit2d_src_temps *tmp)
144 {
145 struct anv_device *device = cmd_buffer->device;
146 VkDevice vk_device = anv_device_to_handle(cmd_buffer->device);
147
148 create_iview(cmd_buffer, src, rect, VK_IMAGE_USAGE_SAMPLED_BIT,
149 &tmp->image, &tmp->iview);
150
151 anv_CreateDescriptorPool(vk_device,
152 &(const VkDescriptorPoolCreateInfo) {
153 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
154 .pNext = NULL,
155 .flags = 0,
156 .maxSets = 1,
157 .poolSizeCount = 1,
158 .pPoolSizes = (VkDescriptorPoolSize[]) {
159 {
160 .type = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
161 .descriptorCount = 1
162 },
163 }
164 }, &cmd_buffer->pool->alloc, &tmp->desc_pool);
165
166 anv_AllocateDescriptorSets(vk_device,
167 &(VkDescriptorSetAllocateInfo) {
168 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
169 .descriptorPool = tmp->desc_pool,
170 .descriptorSetCount = 1,
171 .pSetLayouts = &device->meta_state.blit2d.ds_layout
172 }, &tmp->set);
173
174 anv_UpdateDescriptorSets(vk_device,
175 1, /* writeCount */
176 (VkWriteDescriptorSet[]) {
177 {
178 .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
179 .dstSet = tmp->set,
180 .dstBinding = 0,
181 .dstArrayElement = 0,
182 .descriptorCount = 1,
183 .descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
184 .pImageInfo = (VkDescriptorImageInfo[]) {
185 {
186 .sampler = NULL,
187 .imageView = anv_image_view_to_handle(&tmp->iview),
188 .imageLayout = VK_IMAGE_LAYOUT_GENERAL,
189 },
190 }
191 }
192 }, 0, NULL);
193
194 anv_CmdBindDescriptorSets(anv_cmd_buffer_to_handle(cmd_buffer),
195 VK_PIPELINE_BIND_POINT_GRAPHICS,
196 device->meta_state.blit2d.pipeline_layout, 0, 1,
197 &tmp->set, 0, NULL);
198 }
199
200 static void
201 blit2d_unbind_src(struct anv_cmd_buffer *cmd_buffer,
202 struct blit2d_src_temps *tmp)
203 {
204 anv_DestroyDescriptorPool(anv_device_to_handle(cmd_buffer->device),
205 tmp->desc_pool, &cmd_buffer->pool->alloc);
206 anv_DestroyImage(anv_device_to_handle(cmd_buffer->device),
207 tmp->image, &cmd_buffer->pool->alloc);
208 }
209
210 void
211 anv_meta_end_blit2d(struct anv_cmd_buffer *cmd_buffer,
212 struct anv_meta_saved_state *save)
213 {
214 anv_meta_restore(save, cmd_buffer);
215 }
216
217 void
218 anv_meta_begin_blit2d(struct anv_cmd_buffer *cmd_buffer,
219 struct anv_meta_saved_state *save)
220 {
221 anv_meta_save(save, cmd_buffer,
222 (1 << VK_DYNAMIC_STATE_VIEWPORT));
223 }
224
225 void
226 anv_meta_blit2d(struct anv_cmd_buffer *cmd_buffer,
227 struct anv_meta_blit2d_surf *src,
228 struct anv_meta_blit2d_surf *dst,
229 unsigned num_rects,
230 struct anv_meta_blit2d_rect *rects)
231 {
232 struct anv_device *device = cmd_buffer->device;
233 VkDevice vk_device = anv_device_to_handle(cmd_buffer->device);
234 VkImageUsageFlags dst_usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
235
236 for (unsigned r = 0; r < num_rects; ++r) {
237 struct blit2d_src_temps src_temps;
238 blit2d_bind_src(cmd_buffer, src, &rects[r], &src_temps);
239
240 VkImage dst_img;
241 struct anv_image_view dst_iview;
242 create_iview(cmd_buffer, dst, &rects[r], dst_usage, &dst_img, &dst_iview);
243
244 struct blit_vb_data {
245 float pos[2];
246 float tex_coord[3];
247 } *vb_data;
248
249 unsigned vb_size = sizeof(struct anv_vue_header) + 3 * sizeof(*vb_data);
250
251 struct anv_state vb_state =
252 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, vb_size, 16);
253 memset(vb_state.map, 0, sizeof(struct anv_vue_header));
254 vb_data = vb_state.map + sizeof(struct anv_vue_header);
255
256 vb_data[0] = (struct blit_vb_data) {
257 .pos = {
258 rects[r].dst_x + rects[r].width,
259 rects[r].dst_y + rects[r].height,
260 },
261 .tex_coord = {
262 rects[r].src_x + rects[r].width,
263 rects[r].src_y + rects[r].height,
264 src->pitch,
265 },
266 };
267
268 vb_data[1] = (struct blit_vb_data) {
269 .pos = {
270 rects[r].dst_x,
271 rects[r].dst_y + rects[r].height,
272 },
273 .tex_coord = {
274 rects[r].src_x,
275 rects[r].src_y + rects[r].height,
276 src->pitch,
277 },
278 };
279
280 vb_data[2] = (struct blit_vb_data) {
281 .pos = {
282 rects[r].dst_x,
283 rects[r].dst_y,
284 },
285 .tex_coord = {
286 rects[r].src_x,
287 rects[r].src_y,
288 src->pitch,
289 },
290 };
291
292 anv_state_clflush(vb_state);
293
294 struct anv_buffer vertex_buffer = {
295 .device = device,
296 .size = vb_size,
297 .bo = &device->dynamic_state_block_pool.bo,
298 .offset = vb_state.offset,
299 };
300
301 anv_CmdBindVertexBuffers(anv_cmd_buffer_to_handle(cmd_buffer), 0, 2,
302 (VkBuffer[]) {
303 anv_buffer_to_handle(&vertex_buffer),
304 anv_buffer_to_handle(&vertex_buffer)
305 },
306 (VkDeviceSize[]) {
307 0,
308 sizeof(struct anv_vue_header),
309 });
310
311 VkFramebuffer fb;
312 anv_CreateFramebuffer(vk_device,
313 &(VkFramebufferCreateInfo) {
314 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
315 .attachmentCount = 1,
316 .pAttachments = (VkImageView[]) {
317 anv_image_view_to_handle(&dst_iview),
318 },
319 .width = dst_iview.extent.width,
320 .height = dst_iview.extent.height,
321 .layers = 1
322 }, &cmd_buffer->pool->alloc, &fb);
323
324 ANV_CALL(CmdBeginRenderPass)(anv_cmd_buffer_to_handle(cmd_buffer),
325 &(VkRenderPassBeginInfo) {
326 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
327 .renderPass = device->meta_state.blit2d.render_pass,
328 .framebuffer = fb,
329 .renderArea = {
330 .offset = { rects[r].dst_x, rects[r].dst_y, },
331 .extent = { rects[r].width, rects[r].height },
332 },
333 .clearValueCount = 0,
334 .pClearValues = NULL,
335 }, VK_SUBPASS_CONTENTS_INLINE);
336
337 VkPipeline pipeline = device->meta_state.blit2d.pipeline_2d_src;
338
339 if (cmd_buffer->state.pipeline != anv_pipeline_from_handle(pipeline)) {
340 anv_CmdBindPipeline(anv_cmd_buffer_to_handle(cmd_buffer),
341 VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
342 }
343
344 anv_CmdSetViewport(anv_cmd_buffer_to_handle(cmd_buffer), 0, 1,
345 &(VkViewport) {
346 .x = 0.0f,
347 .y = 0.0f,
348 .width = dst_iview.extent.width,
349 .height = dst_iview.extent.height,
350 .minDepth = 0.0f,
351 .maxDepth = 1.0f,
352 });
353
354 ANV_CALL(CmdDraw)(anv_cmd_buffer_to_handle(cmd_buffer), 3, 1, 0, 0);
355
356 ANV_CALL(CmdEndRenderPass)(anv_cmd_buffer_to_handle(cmd_buffer));
357
358 /* At the point where we emit the draw call, all data from the
359 * descriptor sets, etc. has been used. We are free to delete it.
360 */
361 blit2d_unbind_src(cmd_buffer, &src_temps);
362 anv_DestroyFramebuffer(vk_device, fb, &cmd_buffer->pool->alloc);
363 anv_DestroyImage(vk_device, dst_img, &cmd_buffer->pool->alloc);
364 }
365 }
366
367
368 static nir_shader *
369 build_nir_vertex_shader(void)
370 {
371 const struct glsl_type *vec4 = glsl_vec4_type();
372 nir_builder b;
373
374 nir_builder_init_simple_shader(&b, NULL, MESA_SHADER_VERTEX, NULL);
375 b.shader->info.name = ralloc_strdup(b.shader, "meta_blit_vs");
376
377 nir_variable *pos_in = nir_variable_create(b.shader, nir_var_shader_in,
378 vec4, "a_pos");
379 pos_in->data.location = VERT_ATTRIB_GENERIC0;
380 nir_variable *pos_out = nir_variable_create(b.shader, nir_var_shader_out,
381 vec4, "gl_Position");
382 pos_out->data.location = VARYING_SLOT_POS;
383 nir_copy_var(&b, pos_out, pos_in);
384
385 nir_variable *tex_pos_in = nir_variable_create(b.shader, nir_var_shader_in,
386 vec4, "a_tex_pos");
387 tex_pos_in->data.location = VERT_ATTRIB_GENERIC1;
388 nir_variable *tex_pos_out = nir_variable_create(b.shader, nir_var_shader_out,
389 vec4, "v_tex_pos");
390 tex_pos_out->data.location = VARYING_SLOT_VAR0;
391 tex_pos_out->data.interpolation = INTERP_QUALIFIER_SMOOTH;
392 nir_copy_var(&b, tex_pos_out, tex_pos_in);
393
394 return b.shader;
395 }
396
397 typedef nir_ssa_def* (*texel_fetch_build_func)(struct nir_builder *,
398 struct anv_device *,
399 nir_ssa_def *, nir_ssa_def *);
400
401 static nir_ssa_def *
402 build_nir_texel_fetch(struct nir_builder *b, struct anv_device *device,
403 nir_ssa_def *tex_pos, nir_ssa_def *tex_pitch)
404 {
405 const struct glsl_type *sampler_type =
406 glsl_sampler_type(GLSL_SAMPLER_DIM_2D, false, false, GLSL_TYPE_FLOAT);
407 nir_variable *sampler = nir_variable_create(b->shader, nir_var_uniform,
408 sampler_type, "s_tex");
409 sampler->data.descriptor_set = 0;
410 sampler->data.binding = 0;
411
412 nir_tex_instr *tex = nir_tex_instr_create(b->shader, 2);
413 tex->sampler_dim = GLSL_SAMPLER_DIM_2D;
414 tex->op = nir_texop_txf;
415 tex->src[0].src_type = nir_tex_src_coord;
416 tex->src[0].src = nir_src_for_ssa(tex_pos);
417 tex->src[1].src_type = nir_tex_src_lod;
418 tex->src[1].src = nir_src_for_ssa(nir_imm_int(b, 0));
419 tex->dest_type = nir_type_float; /* TODO */
420 tex->is_array = false;
421 tex->coord_components = 2;
422 tex->texture = nir_deref_var_create(tex, sampler);
423 tex->sampler = NULL;
424
425 nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, "tex");
426 nir_builder_instr_insert(b, &tex->instr);
427
428 return &tex->dest.ssa;
429 }
430
431 static nir_shader *
432 build_nir_copy_fragment_shader(struct anv_device *device,
433 texel_fetch_build_func txf_func)
434 {
435 const struct glsl_type *vec4 = glsl_vec4_type();
436 const struct glsl_type *vec3 = glsl_vector_type(GLSL_TYPE_FLOAT, 3);
437 nir_builder b;
438
439 nir_builder_init_simple_shader(&b, NULL, MESA_SHADER_FRAGMENT, NULL);
440 b.shader->info.name = ralloc_strdup(b.shader, "meta_blit2d_fs");
441
442 nir_variable *tex_pos_in = nir_variable_create(b.shader, nir_var_shader_in,
443 vec3, "v_tex_pos");
444 tex_pos_in->data.location = VARYING_SLOT_VAR0;
445
446 nir_variable *color_out = nir_variable_create(b.shader, nir_var_shader_out,
447 vec4, "f_color");
448 color_out->data.location = FRAG_RESULT_DATA0;
449
450 nir_ssa_def *pos_int = nir_f2i(&b, nir_load_var(&b, tex_pos_in));
451 unsigned swiz[4] = { 0, 1 };
452 nir_ssa_def *tex_pos = nir_swizzle(&b, pos_int, swiz, 2, false);
453 nir_ssa_def *tex_pitch = nir_channel(&b, pos_int, 2);
454
455 nir_ssa_def *color = txf_func(&b, device, tex_pos, tex_pitch);
456 nir_store_var(&b, color_out, color, 0xf);
457
458 return b.shader;
459 }
460
461 void
462 anv_device_finish_meta_blit2d_state(struct anv_device *device)
463 {
464 anv_DestroyRenderPass(anv_device_to_handle(device),
465 device->meta_state.blit2d.render_pass,
466 &device->meta_state.alloc);
467 anv_DestroyPipeline(anv_device_to_handle(device),
468 device->meta_state.blit2d.pipeline_2d_src,
469 &device->meta_state.alloc);
470 anv_DestroyPipelineLayout(anv_device_to_handle(device),
471 device->meta_state.blit2d.pipeline_layout,
472 &device->meta_state.alloc);
473 anv_DestroyDescriptorSetLayout(anv_device_to_handle(device),
474 device->meta_state.blit2d.ds_layout,
475 &device->meta_state.alloc);
476 }
477
478 VkResult
479 anv_device_init_meta_blit2d_state(struct anv_device *device)
480 {
481 VkResult result;
482
483 result = anv_CreateRenderPass(anv_device_to_handle(device),
484 &(VkRenderPassCreateInfo) {
485 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
486 .attachmentCount = 1,
487 .pAttachments = &(VkAttachmentDescription) {
488 .format = VK_FORMAT_UNDEFINED, /* Our shaders don't care */
489 .loadOp = VK_ATTACHMENT_LOAD_OP_LOAD,
490 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
491 .initialLayout = VK_IMAGE_LAYOUT_GENERAL,
492 .finalLayout = VK_IMAGE_LAYOUT_GENERAL,
493 },
494 .subpassCount = 1,
495 .pSubpasses = &(VkSubpassDescription) {
496 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
497 .inputAttachmentCount = 0,
498 .colorAttachmentCount = 1,
499 .pColorAttachments = &(VkAttachmentReference) {
500 .attachment = 0,
501 .layout = VK_IMAGE_LAYOUT_GENERAL,
502 },
503 .pResolveAttachments = NULL,
504 .pDepthStencilAttachment = &(VkAttachmentReference) {
505 .attachment = VK_ATTACHMENT_UNUSED,
506 .layout = VK_IMAGE_LAYOUT_GENERAL,
507 },
508 .preserveAttachmentCount = 1,
509 .pPreserveAttachments = (uint32_t[]) { 0 },
510 },
511 .dependencyCount = 0,
512 }, &device->meta_state.alloc, &device->meta_state.blit2d.render_pass);
513 if (result != VK_SUCCESS)
514 goto fail;
515
516 /* We don't use a vertex shader for blitting, but instead build and pass
517 * the VUEs directly to the rasterization backend. However, we do need
518 * to provide GLSL source for the vertex shader so that the compiler
519 * does not dead-code our inputs.
520 */
521 struct anv_shader_module vs = {
522 .nir = build_nir_vertex_shader(),
523 };
524
525 struct anv_shader_module fs_2d = {
526 .nir = build_nir_copy_fragment_shader(device, build_nir_texel_fetch),
527 };
528
529 VkPipelineVertexInputStateCreateInfo vi_create_info = {
530 .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
531 .vertexBindingDescriptionCount = 2,
532 .pVertexBindingDescriptions = (VkVertexInputBindingDescription[]) {
533 {
534 .binding = 0,
535 .stride = 0,
536 .inputRate = VK_VERTEX_INPUT_RATE_INSTANCE
537 },
538 {
539 .binding = 1,
540 .stride = 5 * sizeof(float),
541 .inputRate = VK_VERTEX_INPUT_RATE_VERTEX
542 },
543 },
544 .vertexAttributeDescriptionCount = 3,
545 .pVertexAttributeDescriptions = (VkVertexInputAttributeDescription[]) {
546 {
547 /* VUE Header */
548 .location = 0,
549 .binding = 0,
550 .format = VK_FORMAT_R32G32B32A32_UINT,
551 .offset = 0
552 },
553 {
554 /* Position */
555 .location = 1,
556 .binding = 1,
557 .format = VK_FORMAT_R32G32_SFLOAT,
558 .offset = 0
559 },
560 {
561 /* Texture Coordinate */
562 .location = 2,
563 .binding = 1,
564 .format = VK_FORMAT_R32G32B32_SFLOAT,
565 .offset = 8
566 }
567 }
568 };
569
570 VkDescriptorSetLayoutCreateInfo ds_layout_info = {
571 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
572 .bindingCount = 1,
573 .pBindings = (VkDescriptorSetLayoutBinding[]) {
574 {
575 .binding = 0,
576 .descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
577 .descriptorCount = 1,
578 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
579 .pImmutableSamplers = NULL
580 },
581 }
582 };
583 result = anv_CreateDescriptorSetLayout(anv_device_to_handle(device),
584 &ds_layout_info,
585 &device->meta_state.alloc,
586 &device->meta_state.blit2d.ds_layout);
587 if (result != VK_SUCCESS)
588 goto fail_render_pass;
589
590 result = anv_CreatePipelineLayout(anv_device_to_handle(device),
591 &(VkPipelineLayoutCreateInfo) {
592 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
593 .setLayoutCount = 1,
594 .pSetLayouts = &device->meta_state.blit2d.ds_layout,
595 },
596 &device->meta_state.alloc, &device->meta_state.blit2d.pipeline_layout);
597 if (result != VK_SUCCESS)
598 goto fail_descriptor_set_layout;
599
600 VkPipelineShaderStageCreateInfo pipeline_shader_stages[] = {
601 {
602 .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
603 .stage = VK_SHADER_STAGE_VERTEX_BIT,
604 .module = anv_shader_module_to_handle(&vs),
605 .pName = "main",
606 .pSpecializationInfo = NULL
607 }, {
608 .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
609 .stage = VK_SHADER_STAGE_FRAGMENT_BIT,
610 .module = VK_NULL_HANDLE, /* TEMPLATE VALUE! FILL ME IN! */
611 .pName = "main",
612 .pSpecializationInfo = NULL
613 },
614 };
615
616 const VkGraphicsPipelineCreateInfo vk_pipeline_info = {
617 .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
618 .stageCount = ARRAY_SIZE(pipeline_shader_stages),
619 .pStages = pipeline_shader_stages,
620 .pVertexInputState = &vi_create_info,
621 .pInputAssemblyState = &(VkPipelineInputAssemblyStateCreateInfo) {
622 .sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
623 .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
624 .primitiveRestartEnable = false,
625 },
626 .pViewportState = &(VkPipelineViewportStateCreateInfo) {
627 .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
628 .viewportCount = 1,
629 .scissorCount = 1,
630 },
631 .pRasterizationState = &(VkPipelineRasterizationStateCreateInfo) {
632 .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
633 .rasterizerDiscardEnable = false,
634 .polygonMode = VK_POLYGON_MODE_FILL,
635 .cullMode = VK_CULL_MODE_NONE,
636 .frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE
637 },
638 .pMultisampleState = &(VkPipelineMultisampleStateCreateInfo) {
639 .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
640 .rasterizationSamples = 1,
641 .sampleShadingEnable = false,
642 .pSampleMask = (VkSampleMask[]) { UINT32_MAX },
643 },
644 .pColorBlendState = &(VkPipelineColorBlendStateCreateInfo) {
645 .sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
646 .attachmentCount = 1,
647 .pAttachments = (VkPipelineColorBlendAttachmentState []) {
648 { .colorWriteMask =
649 VK_COLOR_COMPONENT_A_BIT |
650 VK_COLOR_COMPONENT_R_BIT |
651 VK_COLOR_COMPONENT_G_BIT |
652 VK_COLOR_COMPONENT_B_BIT },
653 }
654 },
655 .pDynamicState = &(VkPipelineDynamicStateCreateInfo) {
656 .sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
657 .dynamicStateCount = 9,
658 .pDynamicStates = (VkDynamicState[]) {
659 VK_DYNAMIC_STATE_VIEWPORT,
660 VK_DYNAMIC_STATE_SCISSOR,
661 VK_DYNAMIC_STATE_LINE_WIDTH,
662 VK_DYNAMIC_STATE_DEPTH_BIAS,
663 VK_DYNAMIC_STATE_BLEND_CONSTANTS,
664 VK_DYNAMIC_STATE_DEPTH_BOUNDS,
665 VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK,
666 VK_DYNAMIC_STATE_STENCIL_WRITE_MASK,
667 VK_DYNAMIC_STATE_STENCIL_REFERENCE,
668 },
669 },
670 .flags = 0,
671 .layout = device->meta_state.blit2d.pipeline_layout,
672 .renderPass = device->meta_state.blit2d.render_pass,
673 .subpass = 0,
674 };
675
676 const struct anv_graphics_pipeline_create_info anv_pipeline_info = {
677 .color_attachment_count = -1,
678 .use_repclear = false,
679 .disable_viewport = true,
680 .disable_scissor = true,
681 .disable_vs = true,
682 .use_rectlist = true
683 };
684
685 pipeline_shader_stages[1].module = anv_shader_module_to_handle(&fs_2d);
686 result = anv_graphics_pipeline_create(anv_device_to_handle(device),
687 VK_NULL_HANDLE,
688 &vk_pipeline_info, &anv_pipeline_info,
689 &device->meta_state.alloc, &device->meta_state.blit2d.pipeline_2d_src);
690 if (result != VK_SUCCESS)
691 goto fail_pipeline_layout;
692
693 ralloc_free(vs.nir);
694 ralloc_free(fs_2d.nir);
695
696 return VK_SUCCESS;
697
698 fail_pipeline_layout:
699 anv_DestroyPipelineLayout(anv_device_to_handle(device),
700 device->meta_state.blit2d.pipeline_layout,
701 &device->meta_state.alloc);
702 fail_descriptor_set_layout:
703 anv_DestroyDescriptorSetLayout(anv_device_to_handle(device),
704 device->meta_state.blit2d.ds_layout,
705 &device->meta_state.alloc);
706 fail_render_pass:
707 anv_DestroyRenderPass(anv_device_to_handle(device),
708 device->meta_state.blit2d.render_pass,
709 &device->meta_state.alloc);
710
711 ralloc_free(vs.nir);
712 ralloc_free(fs_2d.nir);
713 fail:
714 return result;
715 }