6a7845fd30f915b6149befdcaaaa8646b01164bf
[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 static void
133 meta_emit_blit2d(struct anv_cmd_buffer *cmd_buffer,
134 struct anv_image_view *src_iview,
135 VkOffset3D src_offset,
136 struct anv_image_view *dest_iview,
137 VkOffset3D dest_offset,
138 VkExtent3D extent)
139 {
140 struct anv_device *device = cmd_buffer->device;
141
142 struct blit_vb_data {
143 float pos[2];
144 float tex_coord[3];
145 } *vb_data;
146
147 unsigned vb_size = sizeof(struct anv_vue_header) + 3 * sizeof(*vb_data);
148
149 struct anv_state vb_state =
150 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, vb_size, 16);
151 memset(vb_state.map, 0, sizeof(struct anv_vue_header));
152 vb_data = vb_state.map + sizeof(struct anv_vue_header);
153
154 vb_data[0] = (struct blit_vb_data) {
155 .pos = {
156 dest_offset.x + extent.width,
157 dest_offset.y + extent.height,
158 },
159 .tex_coord = {
160 src_offset.x + extent.width,
161 src_offset.y + extent.height,
162 src_offset.z,
163 },
164 };
165
166 vb_data[1] = (struct blit_vb_data) {
167 .pos = {
168 dest_offset.x,
169 dest_offset.y + extent.height,
170 },
171 .tex_coord = {
172 src_offset.x,
173 src_offset.y + extent.height,
174 src_offset.z,
175 },
176 };
177
178 vb_data[2] = (struct blit_vb_data) {
179 .pos = {
180 dest_offset.x,
181 dest_offset.y,
182 },
183 .tex_coord = {
184 src_offset.x,
185 src_offset.y,
186 src_offset.z,
187 },
188 };
189
190 anv_state_clflush(vb_state);
191
192 struct anv_buffer vertex_buffer = {
193 .device = device,
194 .size = vb_size,
195 .bo = &device->dynamic_state_block_pool.bo,
196 .offset = vb_state.offset,
197 };
198
199 anv_CmdBindVertexBuffers(anv_cmd_buffer_to_handle(cmd_buffer), 0, 2,
200 (VkBuffer[]) {
201 anv_buffer_to_handle(&vertex_buffer),
202 anv_buffer_to_handle(&vertex_buffer)
203 },
204 (VkDeviceSize[]) {
205 0,
206 sizeof(struct anv_vue_header),
207 });
208
209 VkDescriptorPool desc_pool;
210 anv_CreateDescriptorPool(anv_device_to_handle(device),
211 &(const VkDescriptorPoolCreateInfo) {
212 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
213 .pNext = NULL,
214 .flags = 0,
215 .maxSets = 1,
216 .poolSizeCount = 1,
217 .pPoolSizes = (VkDescriptorPoolSize[]) {
218 {
219 .type = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
220 .descriptorCount = 1
221 },
222 }
223 }, &cmd_buffer->pool->alloc, &desc_pool);
224
225 VkDescriptorSet set;
226 anv_AllocateDescriptorSets(anv_device_to_handle(device),
227 &(VkDescriptorSetAllocateInfo) {
228 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
229 .descriptorPool = desc_pool,
230 .descriptorSetCount = 1,
231 .pSetLayouts = &device->meta_state.blit2d.ds_layout
232 }, &set);
233
234 anv_UpdateDescriptorSets(anv_device_to_handle(device),
235 1, /* writeCount */
236 (VkWriteDescriptorSet[]) {
237 {
238 .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
239 .dstSet = set,
240 .dstBinding = 0,
241 .dstArrayElement = 0,
242 .descriptorCount = 1,
243 .descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
244 .pImageInfo = (VkDescriptorImageInfo[]) {
245 {
246 .sampler = NULL,
247 .imageView = anv_image_view_to_handle(src_iview),
248 .imageLayout = VK_IMAGE_LAYOUT_GENERAL,
249 },
250 }
251 }
252 }, 0, NULL);
253
254 VkFramebuffer fb;
255 anv_CreateFramebuffer(anv_device_to_handle(device),
256 &(VkFramebufferCreateInfo) {
257 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
258 .attachmentCount = 1,
259 .pAttachments = (VkImageView[]) {
260 anv_image_view_to_handle(dest_iview),
261 },
262 .width = dest_iview->extent.width,
263 .height = dest_iview->extent.height,
264 .layers = 1
265 }, &cmd_buffer->pool->alloc, &fb);
266
267 ANV_CALL(CmdBeginRenderPass)(anv_cmd_buffer_to_handle(cmd_buffer),
268 &(VkRenderPassBeginInfo) {
269 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
270 .renderPass = device->meta_state.blit2d.render_pass,
271 .framebuffer = fb,
272 .renderArea = {
273 .offset = { dest_offset.x, dest_offset.y },
274 .extent = { extent.width, extent.height },
275 },
276 .clearValueCount = 0,
277 .pClearValues = NULL,
278 }, VK_SUBPASS_CONTENTS_INLINE);
279
280 VkPipeline pipeline = device->meta_state.blit2d.pipeline_2d_src;
281
282 if (cmd_buffer->state.pipeline != anv_pipeline_from_handle(pipeline)) {
283 anv_CmdBindPipeline(anv_cmd_buffer_to_handle(cmd_buffer),
284 VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
285 }
286
287 anv_CmdSetViewport(anv_cmd_buffer_to_handle(cmd_buffer), 0, 1,
288 &(VkViewport) {
289 .x = 0.0f,
290 .y = 0.0f,
291 .width = dest_iview->extent.width,
292 .height = dest_iview->extent.height,
293 .minDepth = 0.0f,
294 .maxDepth = 1.0f,
295 });
296
297 anv_CmdBindDescriptorSets(anv_cmd_buffer_to_handle(cmd_buffer),
298 VK_PIPELINE_BIND_POINT_GRAPHICS,
299 device->meta_state.blit2d.pipeline_layout, 0, 1,
300 &set, 0, NULL);
301
302 ANV_CALL(CmdDraw)(anv_cmd_buffer_to_handle(cmd_buffer), 3, 1, 0, 0);
303
304 ANV_CALL(CmdEndRenderPass)(anv_cmd_buffer_to_handle(cmd_buffer));
305
306 /* At the point where we emit the draw call, all data from the
307 * descriptor sets, etc. has been used. We are free to delete it.
308 */
309 anv_DestroyDescriptorPool(anv_device_to_handle(device),
310 desc_pool, &cmd_buffer->pool->alloc);
311 anv_DestroyFramebuffer(anv_device_to_handle(device), fb,
312 &cmd_buffer->pool->alloc);
313 }
314
315 void
316 anv_meta_end_blit2d(struct anv_cmd_buffer *cmd_buffer,
317 struct anv_meta_saved_state *save)
318 {
319 anv_meta_restore(save, cmd_buffer);
320 }
321
322 void
323 anv_meta_begin_blit2d(struct anv_cmd_buffer *cmd_buffer,
324 struct anv_meta_saved_state *save)
325 {
326 anv_meta_save(save, cmd_buffer,
327 (1 << VK_DYNAMIC_STATE_VIEWPORT));
328 }
329
330 void
331 anv_meta_blit2d(struct anv_cmd_buffer *cmd_buffer,
332 struct anv_meta_blit2d_surf *src,
333 struct anv_meta_blit2d_surf *dst,
334 unsigned num_rects,
335 struct anv_meta_blit2d_rect *rects)
336 {
337 VkDevice vk_device = anv_device_to_handle(cmd_buffer->device);
338 VkImageUsageFlags src_usage = VK_IMAGE_USAGE_SAMPLED_BIT;
339 VkImageUsageFlags dst_usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
340
341 for (unsigned r = 0; r < num_rects; ++r) {
342 VkImage src_img;
343 VkImage dst_img;
344 struct anv_image_view src_iview;
345 struct anv_image_view dst_iview;
346 create_iview(cmd_buffer, src, &rects[r], src_usage, &src_img, &src_iview);
347 create_iview(cmd_buffer, dst, &rects[r], dst_usage, &dst_img, &dst_iview);
348
349 /* Perform blit */
350 meta_emit_blit2d(cmd_buffer,
351 &src_iview,
352 (VkOffset3D){rects[r].src_x, rects[r].src_y, 0},
353 &dst_iview,
354 (VkOffset3D){rects[r].dst_x, rects[r].dst_y, 0},
355 (VkExtent3D){rects[r].width, rects[r].height, 1});
356
357 anv_DestroyImage(vk_device, src_img, &cmd_buffer->pool->alloc);
358 anv_DestroyImage(vk_device, dst_img, &cmd_buffer->pool->alloc);
359 }
360 }
361
362
363 static nir_shader *
364 build_nir_vertex_shader(void)
365 {
366 const struct glsl_type *vec4 = glsl_vec4_type();
367 nir_builder b;
368
369 nir_builder_init_simple_shader(&b, NULL, MESA_SHADER_VERTEX, NULL);
370 b.shader->info.name = ralloc_strdup(b.shader, "meta_blit_vs");
371
372 nir_variable *pos_in = nir_variable_create(b.shader, nir_var_shader_in,
373 vec4, "a_pos");
374 pos_in->data.location = VERT_ATTRIB_GENERIC0;
375 nir_variable *pos_out = nir_variable_create(b.shader, nir_var_shader_out,
376 vec4, "gl_Position");
377 pos_out->data.location = VARYING_SLOT_POS;
378 nir_copy_var(&b, pos_out, pos_in);
379
380 nir_variable *tex_pos_in = nir_variable_create(b.shader, nir_var_shader_in,
381 vec4, "a_tex_pos");
382 tex_pos_in->data.location = VERT_ATTRIB_GENERIC1;
383 nir_variable *tex_pos_out = nir_variable_create(b.shader, nir_var_shader_out,
384 vec4, "v_tex_pos");
385 tex_pos_out->data.location = VARYING_SLOT_VAR0;
386 tex_pos_out->data.interpolation = INTERP_QUALIFIER_SMOOTH;
387 nir_copy_var(&b, tex_pos_out, tex_pos_in);
388
389 return b.shader;
390 }
391
392 static nir_shader *
393 build_nir_copy_fragment_shader()
394 {
395 const struct glsl_type *vec4 = glsl_vec4_type();
396 const struct glsl_type *vec2 = glsl_vector_type(GLSL_TYPE_FLOAT, 2);
397 nir_builder b;
398
399 nir_builder_init_simple_shader(&b, NULL, MESA_SHADER_FRAGMENT, NULL);
400 b.shader->info.name = ralloc_strdup(b.shader, "meta_blit2d_fs");
401
402 nir_variable *tex_pos_in = nir_variable_create(b.shader, nir_var_shader_in,
403 vec2, "v_tex_pos");
404 tex_pos_in->data.location = VARYING_SLOT_VAR0;
405 nir_ssa_def *const tex_pos = nir_f2i(&b, nir_load_var(&b, tex_pos_in));
406
407 const struct glsl_type *sampler_type =
408 glsl_sampler_type(GLSL_SAMPLER_DIM_2D, false, false,
409 glsl_get_base_type(vec4));
410 nir_variable *sampler = nir_variable_create(b.shader, nir_var_uniform,
411 sampler_type, "s_tex");
412 sampler->data.descriptor_set = 0;
413 sampler->data.binding = 0;
414
415 nir_tex_instr *tex = nir_tex_instr_create(b.shader, 2);
416 tex->sampler_dim = GLSL_SAMPLER_DIM_2D;
417 tex->op = nir_texop_txf;
418 tex->src[0].src_type = nir_tex_src_coord;
419 tex->src[0].src = nir_src_for_ssa(tex_pos);
420 tex->src[1].src_type = nir_tex_src_lod;
421 tex->src[1].src = nir_src_for_ssa(nir_imm_int(&b, 0));
422 tex->dest_type = nir_type_float; /* TODO */
423 tex->is_array = false;
424 tex->coord_components = tex_pos->num_components;
425 tex->texture = nir_deref_var_create(tex, sampler);
426 tex->sampler = NULL;
427
428 nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, "tex");
429 nir_builder_instr_insert(&b, &tex->instr);
430
431 nir_variable *color_out = nir_variable_create(b.shader, nir_var_shader_out,
432 vec4, "f_color");
433 color_out->data.location = FRAG_RESULT_DATA0;
434 nir_store_var(&b, color_out, &tex->dest.ssa, 0xf);
435
436 return b.shader;
437 }
438
439 void
440 anv_device_finish_meta_blit2d_state(struct anv_device *device)
441 {
442 anv_DestroyRenderPass(anv_device_to_handle(device),
443 device->meta_state.blit2d.render_pass,
444 &device->meta_state.alloc);
445 anv_DestroyPipeline(anv_device_to_handle(device),
446 device->meta_state.blit2d.pipeline_2d_src,
447 &device->meta_state.alloc);
448 anv_DestroyPipelineLayout(anv_device_to_handle(device),
449 device->meta_state.blit2d.pipeline_layout,
450 &device->meta_state.alloc);
451 anv_DestroyDescriptorSetLayout(anv_device_to_handle(device),
452 device->meta_state.blit2d.ds_layout,
453 &device->meta_state.alloc);
454 }
455
456 VkResult
457 anv_device_init_meta_blit2d_state(struct anv_device *device)
458 {
459 VkResult result;
460
461 result = anv_CreateRenderPass(anv_device_to_handle(device),
462 &(VkRenderPassCreateInfo) {
463 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
464 .attachmentCount = 1,
465 .pAttachments = &(VkAttachmentDescription) {
466 .format = VK_FORMAT_UNDEFINED, /* Our shaders don't care */
467 .loadOp = VK_ATTACHMENT_LOAD_OP_LOAD,
468 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
469 .initialLayout = VK_IMAGE_LAYOUT_GENERAL,
470 .finalLayout = VK_IMAGE_LAYOUT_GENERAL,
471 },
472 .subpassCount = 1,
473 .pSubpasses = &(VkSubpassDescription) {
474 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
475 .inputAttachmentCount = 0,
476 .colorAttachmentCount = 1,
477 .pColorAttachments = &(VkAttachmentReference) {
478 .attachment = 0,
479 .layout = VK_IMAGE_LAYOUT_GENERAL,
480 },
481 .pResolveAttachments = NULL,
482 .pDepthStencilAttachment = &(VkAttachmentReference) {
483 .attachment = VK_ATTACHMENT_UNUSED,
484 .layout = VK_IMAGE_LAYOUT_GENERAL,
485 },
486 .preserveAttachmentCount = 1,
487 .pPreserveAttachments = (uint32_t[]) { 0 },
488 },
489 .dependencyCount = 0,
490 }, &device->meta_state.alloc, &device->meta_state.blit2d.render_pass);
491 if (result != VK_SUCCESS)
492 goto fail;
493
494 /* We don't use a vertex shader for blitting, but instead build and pass
495 * the VUEs directly to the rasterization backend. However, we do need
496 * to provide GLSL source for the vertex shader so that the compiler
497 * does not dead-code our inputs.
498 */
499 struct anv_shader_module vs = {
500 .nir = build_nir_vertex_shader(),
501 };
502
503 struct anv_shader_module fs_2d = {
504 .nir = build_nir_copy_fragment_shader(),
505 };
506
507 VkPipelineVertexInputStateCreateInfo vi_create_info = {
508 .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
509 .vertexBindingDescriptionCount = 2,
510 .pVertexBindingDescriptions = (VkVertexInputBindingDescription[]) {
511 {
512 .binding = 0,
513 .stride = 0,
514 .inputRate = VK_VERTEX_INPUT_RATE_INSTANCE
515 },
516 {
517 .binding = 1,
518 .stride = 5 * sizeof(float),
519 .inputRate = VK_VERTEX_INPUT_RATE_VERTEX
520 },
521 },
522 .vertexAttributeDescriptionCount = 3,
523 .pVertexAttributeDescriptions = (VkVertexInputAttributeDescription[]) {
524 {
525 /* VUE Header */
526 .location = 0,
527 .binding = 0,
528 .format = VK_FORMAT_R32G32B32A32_UINT,
529 .offset = 0
530 },
531 {
532 /* Position */
533 .location = 1,
534 .binding = 1,
535 .format = VK_FORMAT_R32G32_SFLOAT,
536 .offset = 0
537 },
538 {
539 /* Texture Coordinate */
540 .location = 2,
541 .binding = 1,
542 .format = VK_FORMAT_R32G32B32_SFLOAT,
543 .offset = 8
544 }
545 }
546 };
547
548 VkDescriptorSetLayoutCreateInfo ds_layout_info = {
549 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
550 .bindingCount = 1,
551 .pBindings = (VkDescriptorSetLayoutBinding[]) {
552 {
553 .binding = 0,
554 .descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
555 .descriptorCount = 1,
556 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
557 .pImmutableSamplers = NULL
558 },
559 }
560 };
561 result = anv_CreateDescriptorSetLayout(anv_device_to_handle(device),
562 &ds_layout_info,
563 &device->meta_state.alloc,
564 &device->meta_state.blit2d.ds_layout);
565 if (result != VK_SUCCESS)
566 goto fail_render_pass;
567
568 result = anv_CreatePipelineLayout(anv_device_to_handle(device),
569 &(VkPipelineLayoutCreateInfo) {
570 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
571 .setLayoutCount = 1,
572 .pSetLayouts = &device->meta_state.blit2d.ds_layout,
573 },
574 &device->meta_state.alloc, &device->meta_state.blit2d.pipeline_layout);
575 if (result != VK_SUCCESS)
576 goto fail_descriptor_set_layout;
577
578 VkPipelineShaderStageCreateInfo pipeline_shader_stages[] = {
579 {
580 .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
581 .stage = VK_SHADER_STAGE_VERTEX_BIT,
582 .module = anv_shader_module_to_handle(&vs),
583 .pName = "main",
584 .pSpecializationInfo = NULL
585 }, {
586 .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
587 .stage = VK_SHADER_STAGE_FRAGMENT_BIT,
588 .module = VK_NULL_HANDLE, /* TEMPLATE VALUE! FILL ME IN! */
589 .pName = "main",
590 .pSpecializationInfo = NULL
591 },
592 };
593
594 const VkGraphicsPipelineCreateInfo vk_pipeline_info = {
595 .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
596 .stageCount = ARRAY_SIZE(pipeline_shader_stages),
597 .pStages = pipeline_shader_stages,
598 .pVertexInputState = &vi_create_info,
599 .pInputAssemblyState = &(VkPipelineInputAssemblyStateCreateInfo) {
600 .sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
601 .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
602 .primitiveRestartEnable = false,
603 },
604 .pViewportState = &(VkPipelineViewportStateCreateInfo) {
605 .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
606 .viewportCount = 1,
607 .scissorCount = 1,
608 },
609 .pRasterizationState = &(VkPipelineRasterizationStateCreateInfo) {
610 .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
611 .rasterizerDiscardEnable = false,
612 .polygonMode = VK_POLYGON_MODE_FILL,
613 .cullMode = VK_CULL_MODE_NONE,
614 .frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE
615 },
616 .pMultisampleState = &(VkPipelineMultisampleStateCreateInfo) {
617 .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
618 .rasterizationSamples = 1,
619 .sampleShadingEnable = false,
620 .pSampleMask = (VkSampleMask[]) { UINT32_MAX },
621 },
622 .pColorBlendState = &(VkPipelineColorBlendStateCreateInfo) {
623 .sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
624 .attachmentCount = 1,
625 .pAttachments = (VkPipelineColorBlendAttachmentState []) {
626 { .colorWriteMask =
627 VK_COLOR_COMPONENT_A_BIT |
628 VK_COLOR_COMPONENT_R_BIT |
629 VK_COLOR_COMPONENT_G_BIT |
630 VK_COLOR_COMPONENT_B_BIT },
631 }
632 },
633 .pDynamicState = &(VkPipelineDynamicStateCreateInfo) {
634 .sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
635 .dynamicStateCount = 9,
636 .pDynamicStates = (VkDynamicState[]) {
637 VK_DYNAMIC_STATE_VIEWPORT,
638 VK_DYNAMIC_STATE_SCISSOR,
639 VK_DYNAMIC_STATE_LINE_WIDTH,
640 VK_DYNAMIC_STATE_DEPTH_BIAS,
641 VK_DYNAMIC_STATE_BLEND_CONSTANTS,
642 VK_DYNAMIC_STATE_DEPTH_BOUNDS,
643 VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK,
644 VK_DYNAMIC_STATE_STENCIL_WRITE_MASK,
645 VK_DYNAMIC_STATE_STENCIL_REFERENCE,
646 },
647 },
648 .flags = 0,
649 .layout = device->meta_state.blit2d.pipeline_layout,
650 .renderPass = device->meta_state.blit2d.render_pass,
651 .subpass = 0,
652 };
653
654 const struct anv_graphics_pipeline_create_info anv_pipeline_info = {
655 .color_attachment_count = -1,
656 .use_repclear = false,
657 .disable_viewport = true,
658 .disable_scissor = true,
659 .disable_vs = true,
660 .use_rectlist = true
661 };
662
663 pipeline_shader_stages[1].module = anv_shader_module_to_handle(&fs_2d);
664 result = anv_graphics_pipeline_create(anv_device_to_handle(device),
665 VK_NULL_HANDLE,
666 &vk_pipeline_info, &anv_pipeline_info,
667 &device->meta_state.alloc, &device->meta_state.blit2d.pipeline_2d_src);
668 if (result != VK_SUCCESS)
669 goto fail_pipeline_layout;
670
671 ralloc_free(vs.nir);
672 ralloc_free(fs_2d.nir);
673
674 return VK_SUCCESS;
675
676 fail_pipeline_layout:
677 anv_DestroyPipelineLayout(anv_device_to_handle(device),
678 device->meta_state.blit2d.pipeline_layout,
679 &device->meta_state.alloc);
680 fail_descriptor_set_layout:
681 anv_DestroyDescriptorSetLayout(anv_device_to_handle(device),
682 device->meta_state.blit2d.ds_layout,
683 &device->meta_state.alloc);
684 fail_render_pass:
685 anv_DestroyRenderPass(anv_device_to_handle(device),
686 device->meta_state.blit2d.render_pass,
687 &device->meta_state.alloc);
688
689 ralloc_free(vs.nir);
690 ralloc_free(fs_2d.nir);
691 fail:
692 return result;
693 }