Merge remote-tracking branch 'origin/master' into vulkan
[mesa.git] / src / intel / vulkan / anv_meta_blit.c
1 /*
2 * Copyright © 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "anv_meta.h"
25 #include "nir/nir_builder.h"
26
27 struct blit_region {
28 VkOffset3D src_offset;
29 VkExtent3D src_extent;
30 VkOffset3D dest_offset;
31 VkExtent3D dest_extent;
32 };
33
34 static nir_shader *
35 build_nir_vertex_shader(void)
36 {
37 const struct glsl_type *vec4 = glsl_vec4_type();
38 nir_builder b;
39
40 nir_builder_init_simple_shader(&b, NULL, MESA_SHADER_VERTEX, NULL);
41 b.shader->info.name = ralloc_strdup(b.shader, "meta_blit_vs");
42
43 nir_variable *pos_in = nir_variable_create(b.shader, nir_var_shader_in,
44 vec4, "a_pos");
45 pos_in->data.location = VERT_ATTRIB_GENERIC0;
46 nir_variable *pos_out = nir_variable_create(b.shader, nir_var_shader_out,
47 vec4, "gl_Position");
48 pos_out->data.location = VARYING_SLOT_POS;
49 nir_copy_var(&b, pos_out, pos_in);
50
51 nir_variable *tex_pos_in = nir_variable_create(b.shader, nir_var_shader_in,
52 vec4, "a_tex_pos");
53 tex_pos_in->data.location = VERT_ATTRIB_GENERIC1;
54 nir_variable *tex_pos_out = nir_variable_create(b.shader, nir_var_shader_out,
55 vec4, "v_tex_pos");
56 tex_pos_out->data.location = VARYING_SLOT_VAR0;
57 tex_pos_out->data.interpolation = INTERP_QUALIFIER_SMOOTH;
58 nir_copy_var(&b, tex_pos_out, tex_pos_in);
59
60 return b.shader;
61 }
62
63 static nir_shader *
64 build_nir_copy_fragment_shader(enum glsl_sampler_dim tex_dim)
65 {
66 const struct glsl_type *vec4 = glsl_vec4_type();
67 nir_builder b;
68
69 nir_builder_init_simple_shader(&b, NULL, MESA_SHADER_FRAGMENT, NULL);
70 b.shader->info.name = ralloc_strdup(b.shader, "meta_blit_fs");
71
72 nir_variable *tex_pos_in = nir_variable_create(b.shader, nir_var_shader_in,
73 vec4, "v_tex_pos");
74 tex_pos_in->data.location = VARYING_SLOT_VAR0;
75
76 /* Swizzle the array index which comes in as Z coordinate into the right
77 * position.
78 */
79 unsigned swz[] = { 0, (tex_dim == GLSL_SAMPLER_DIM_1D ? 2 : 1), 2 };
80 nir_ssa_def *const tex_pos =
81 nir_swizzle(&b, nir_load_var(&b, tex_pos_in), swz,
82 (tex_dim == GLSL_SAMPLER_DIM_1D ? 2 : 3), false);
83
84 const struct glsl_type *sampler_type =
85 glsl_sampler_type(tex_dim, false, tex_dim != GLSL_SAMPLER_DIM_3D,
86 glsl_get_base_type(vec4));
87 nir_variable *sampler = nir_variable_create(b.shader, nir_var_uniform,
88 sampler_type, "s_tex");
89 sampler->data.descriptor_set = 0;
90 sampler->data.binding = 0;
91
92 nir_tex_instr *tex = nir_tex_instr_create(b.shader, 1);
93 tex->sampler_dim = tex_dim;
94 tex->op = nir_texop_tex;
95 tex->src[0].src_type = nir_tex_src_coord;
96 tex->src[0].src = nir_src_for_ssa(tex_pos);
97 tex->dest_type = nir_type_float; /* TODO */
98 tex->is_array = glsl_sampler_type_is_array(sampler_type);
99 tex->coord_components = tex_pos->num_components;
100 tex->texture = nir_deref_var_create(tex, sampler);
101 tex->sampler = nir_deref_var_create(tex, sampler);
102
103 nir_ssa_dest_init(&tex->instr, &tex->dest, 4, "tex");
104 nir_builder_instr_insert(&b, &tex->instr);
105
106 nir_variable *color_out = nir_variable_create(b.shader, nir_var_shader_out,
107 vec4, "f_color");
108 color_out->data.location = FRAG_RESULT_DATA0;
109 nir_store_var(&b, color_out, &tex->dest.ssa, 4);
110
111 return b.shader;
112 }
113
114 static void
115 meta_prepare_blit(struct anv_cmd_buffer *cmd_buffer,
116 struct anv_meta_saved_state *saved_state)
117 {
118 anv_meta_save(saved_state, cmd_buffer,
119 (1 << VK_DYNAMIC_STATE_VIEWPORT));
120 }
121
122 static void
123 meta_emit_blit(struct anv_cmd_buffer *cmd_buffer,
124 struct anv_image *src_image,
125 struct anv_image_view *src_iview,
126 VkOffset3D src_offset,
127 VkExtent3D src_extent,
128 struct anv_image *dest_image,
129 struct anv_image_view *dest_iview,
130 VkOffset3D dest_offset,
131 VkExtent3D dest_extent,
132 VkFilter blit_filter)
133 {
134 struct anv_device *device = cmd_buffer->device;
135
136 struct blit_vb_data {
137 float pos[2];
138 float tex_coord[3];
139 } *vb_data;
140
141 assert(src_image->samples == dest_image->samples);
142
143 unsigned vb_size = sizeof(struct anv_vue_header) + 3 * sizeof(*vb_data);
144
145 struct anv_state vb_state =
146 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, vb_size, 16);
147 memset(vb_state.map, 0, sizeof(struct anv_vue_header));
148 vb_data = vb_state.map + sizeof(struct anv_vue_header);
149
150 vb_data[0] = (struct blit_vb_data) {
151 .pos = {
152 dest_offset.x + dest_extent.width,
153 dest_offset.y + dest_extent.height,
154 },
155 .tex_coord = {
156 (float)(src_offset.x + src_extent.width)
157 / (float)src_iview->extent.width,
158 (float)(src_offset.y + src_extent.height)
159 / (float)src_iview->extent.height,
160 (float)src_offset.z / (float)src_iview->extent.depth,
161 },
162 };
163
164 vb_data[1] = (struct blit_vb_data) {
165 .pos = {
166 dest_offset.x,
167 dest_offset.y + dest_extent.height,
168 },
169 .tex_coord = {
170 (float)src_offset.x / (float)src_iview->extent.width,
171 (float)(src_offset.y + src_extent.height) /
172 (float)src_iview->extent.height,
173 (float)src_offset.z / (float)src_iview->extent.depth,
174 },
175 };
176
177 vb_data[2] = (struct blit_vb_data) {
178 .pos = {
179 dest_offset.x,
180 dest_offset.y,
181 },
182 .tex_coord = {
183 (float)src_offset.x / (float)src_iview->extent.width,
184 (float)src_offset.y / (float)src_iview->extent.height,
185 (float)src_offset.z / (float)src_iview->extent.depth,
186 },
187 };
188
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 }, &cmd_buffer->pool->alloc, &sampler);
215
216 VkDescriptorPool desc_pool;
217 anv_CreateDescriptorPool(anv_device_to_handle(device),
218 &(const VkDescriptorPoolCreateInfo) {
219 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
220 .pNext = NULL,
221 .flags = 0,
222 .maxSets = 1,
223 .poolSizeCount = 1,
224 .pPoolSizes = (VkDescriptorPoolSize[]) {
225 {
226 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
227 .descriptorCount = 1
228 },
229 }
230 }, &cmd_buffer->pool->alloc, &desc_pool);
231
232 VkDescriptorSet set;
233 anv_AllocateDescriptorSets(anv_device_to_handle(device),
234 &(VkDescriptorSetAllocateInfo) {
235 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
236 .descriptorPool = desc_pool,
237 .descriptorSetCount = 1,
238 .pSetLayouts = &device->meta_state.blit.ds_layout
239 }, &set);
240
241 anv_UpdateDescriptorSets(anv_device_to_handle(device),
242 1, /* writeCount */
243 (VkWriteDescriptorSet[]) {
244 {
245 .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
246 .dstSet = set,
247 .dstBinding = 0,
248 .dstArrayElement = 0,
249 .descriptorCount = 1,
250 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
251 .pImageInfo = (VkDescriptorImageInfo[]) {
252 {
253 .sampler = sampler,
254 .imageView = anv_image_view_to_handle(src_iview),
255 .imageLayout = VK_IMAGE_LAYOUT_GENERAL,
256 },
257 }
258 }
259 }, 0, NULL);
260
261 VkFramebuffer fb;
262 anv_CreateFramebuffer(anv_device_to_handle(device),
263 &(VkFramebufferCreateInfo) {
264 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
265 .attachmentCount = 1,
266 .pAttachments = (VkImageView[]) {
267 anv_image_view_to_handle(dest_iview),
268 },
269 .width = dest_iview->extent.width,
270 .height = dest_iview->extent.height,
271 .layers = 1
272 }, &cmd_buffer->pool->alloc, &fb);
273
274 ANV_CALL(CmdBeginRenderPass)(anv_cmd_buffer_to_handle(cmd_buffer),
275 &(VkRenderPassBeginInfo) {
276 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
277 .renderPass = device->meta_state.blit.render_pass,
278 .framebuffer = fb,
279 .renderArea = {
280 .offset = { dest_offset.x, dest_offset.y },
281 .extent = { dest_extent.width, dest_extent.height },
282 },
283 .clearValueCount = 0,
284 .pClearValues = NULL,
285 }, VK_SUBPASS_CONTENTS_INLINE);
286
287 VkPipeline pipeline;
288
289 switch (src_image->type) {
290 case VK_IMAGE_TYPE_1D:
291 pipeline = device->meta_state.blit.pipeline_1d_src;
292 break;
293 case VK_IMAGE_TYPE_2D:
294 pipeline = device->meta_state.blit.pipeline_2d_src;
295 break;
296 case VK_IMAGE_TYPE_3D:
297 pipeline = device->meta_state.blit.pipeline_3d_src;
298 break;
299 default:
300 unreachable(!"bad VkImageType");
301 }
302
303 if (cmd_buffer->state.pipeline != anv_pipeline_from_handle(pipeline)) {
304 anv_CmdBindPipeline(anv_cmd_buffer_to_handle(cmd_buffer),
305 VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
306 }
307
308 anv_CmdSetViewport(anv_cmd_buffer_to_handle(cmd_buffer), 0, 1,
309 &(VkViewport) {
310 .x = 0.0f,
311 .y = 0.0f,
312 .width = dest_iview->extent.width,
313 .height = dest_iview->extent.height,
314 .minDepth = 0.0f,
315 .maxDepth = 1.0f,
316 });
317
318 anv_CmdBindDescriptorSets(anv_cmd_buffer_to_handle(cmd_buffer),
319 VK_PIPELINE_BIND_POINT_GRAPHICS,
320 device->meta_state.blit.pipeline_layout, 0, 1,
321 &set, 0, NULL);
322
323 ANV_CALL(CmdDraw)(anv_cmd_buffer_to_handle(cmd_buffer), 3, 1, 0, 0);
324
325 ANV_CALL(CmdEndRenderPass)(anv_cmd_buffer_to_handle(cmd_buffer));
326
327 /* At the point where we emit the draw call, all data from the
328 * descriptor sets, etc. has been used. We are free to delete it.
329 */
330 anv_DestroyDescriptorPool(anv_device_to_handle(device),
331 desc_pool, &cmd_buffer->pool->alloc);
332 anv_DestroySampler(anv_device_to_handle(device), sampler,
333 &cmd_buffer->pool->alloc);
334 anv_DestroyFramebuffer(anv_device_to_handle(device), fb,
335 &cmd_buffer->pool->alloc);
336 }
337
338 static void
339 meta_finish_blit(struct anv_cmd_buffer *cmd_buffer,
340 const struct anv_meta_saved_state *saved_state)
341 {
342 anv_meta_restore(saved_state, cmd_buffer);
343 }
344
345 void anv_CmdBlitImage(
346 VkCommandBuffer commandBuffer,
347 VkImage srcImage,
348 VkImageLayout srcImageLayout,
349 VkImage destImage,
350 VkImageLayout destImageLayout,
351 uint32_t regionCount,
352 const VkImageBlit* pRegions,
353 VkFilter filter)
354
355 {
356 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
357 ANV_FROM_HANDLE(anv_image, src_image, srcImage);
358 ANV_FROM_HANDLE(anv_image, dest_image, destImage);
359 struct anv_meta_saved_state saved_state;
360
361 /* From the Vulkan 1.0 spec:
362 *
363 * vkCmdBlitImage must not be used for multisampled source or
364 * destination images. Use vkCmdResolveImage for this purpose.
365 */
366 assert(src_image->samples == 1);
367 assert(dest_image->samples == 1);
368
369 meta_prepare_blit(cmd_buffer, &saved_state);
370
371 for (unsigned r = 0; r < regionCount; r++) {
372 struct anv_image_view src_iview;
373 anv_image_view_init(&src_iview, cmd_buffer->device,
374 &(VkImageViewCreateInfo) {
375 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
376 .image = srcImage,
377 .viewType = anv_meta_get_view_type(src_image),
378 .format = src_image->vk_format,
379 .subresourceRange = {
380 .aspectMask = pRegions[r].srcSubresource.aspectMask,
381 .baseMipLevel = pRegions[r].srcSubresource.mipLevel,
382 .levelCount = 1,
383 .baseArrayLayer = pRegions[r].srcSubresource.baseArrayLayer,
384 .layerCount = 1
385 },
386 },
387 cmd_buffer, 0, VK_IMAGE_USAGE_SAMPLED_BIT);
388
389 const VkOffset3D dest_offset = {
390 .x = pRegions[r].dstOffsets[0].x,
391 .y = pRegions[r].dstOffsets[0].y,
392 .z = 0,
393 };
394
395 if (pRegions[r].dstOffsets[1].x < pRegions[r].dstOffsets[0].x ||
396 pRegions[r].dstOffsets[1].y < pRegions[r].dstOffsets[0].y ||
397 pRegions[r].srcOffsets[1].x < pRegions[r].srcOffsets[0].x ||
398 pRegions[r].srcOffsets[1].y < pRegions[r].srcOffsets[0].y)
399 anv_finishme("FINISHME: Allow flipping in blits");
400
401 const VkExtent3D dest_extent = {
402 .width = pRegions[r].dstOffsets[1].x - pRegions[r].dstOffsets[0].x,
403 .height = pRegions[r].dstOffsets[1].y - pRegions[r].dstOffsets[0].y,
404 };
405
406 const VkExtent3D src_extent = {
407 .width = pRegions[r].srcOffsets[1].x - pRegions[r].srcOffsets[0].x,
408 .height = pRegions[r].srcOffsets[1].y - pRegions[r].srcOffsets[0].y,
409 };
410
411 const uint32_t dest_array_slice =
412 anv_meta_get_iview_layer(dest_image, &pRegions[r].dstSubresource,
413 &pRegions[r].dstOffsets[0]);
414
415 if (pRegions[r].srcSubresource.layerCount > 1)
416 anv_finishme("FINISHME: copy multiple array layers");
417
418 if (pRegions[r].srcOffsets[0].z + 1 != pRegions[r].srcOffsets[1].z ||
419 pRegions[r].dstOffsets[0].z + 1 != pRegions[r].dstOffsets[1].z)
420 anv_finishme("FINISHME: copy multiple depth layers");
421
422 struct anv_image_view dest_iview;
423 anv_image_view_init(&dest_iview, cmd_buffer->device,
424 &(VkImageViewCreateInfo) {
425 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
426 .image = destImage,
427 .viewType = anv_meta_get_view_type(dest_image),
428 .format = dest_image->vk_format,
429 .subresourceRange = {
430 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
431 .baseMipLevel = pRegions[r].dstSubresource.mipLevel,
432 .levelCount = 1,
433 .baseArrayLayer = dest_array_slice,
434 .layerCount = 1
435 },
436 },
437 cmd_buffer, 0, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
438
439 meta_emit_blit(cmd_buffer,
440 src_image, &src_iview,
441 pRegions[r].srcOffsets[0], src_extent,
442 dest_image, &dest_iview,
443 dest_offset, dest_extent,
444 filter);
445 }
446
447 meta_finish_blit(cmd_buffer, &saved_state);
448 }
449
450 void
451 anv_device_finish_meta_blit_state(struct anv_device *device)
452 {
453 anv_DestroyRenderPass(anv_device_to_handle(device),
454 device->meta_state.blit.render_pass,
455 &device->meta_state.alloc);
456 anv_DestroyPipeline(anv_device_to_handle(device),
457 device->meta_state.blit.pipeline_1d_src,
458 &device->meta_state.alloc);
459 anv_DestroyPipeline(anv_device_to_handle(device),
460 device->meta_state.blit.pipeline_2d_src,
461 &device->meta_state.alloc);
462 anv_DestroyPipeline(anv_device_to_handle(device),
463 device->meta_state.blit.pipeline_3d_src,
464 &device->meta_state.alloc);
465 anv_DestroyPipelineLayout(anv_device_to_handle(device),
466 device->meta_state.blit.pipeline_layout,
467 &device->meta_state.alloc);
468 anv_DestroyDescriptorSetLayout(anv_device_to_handle(device),
469 device->meta_state.blit.ds_layout,
470 &device->meta_state.alloc);
471 }
472
473 VkResult
474 anv_device_init_meta_blit_state(struct anv_device *device)
475 {
476 VkResult result;
477
478 result = anv_CreateRenderPass(anv_device_to_handle(device),
479 &(VkRenderPassCreateInfo) {
480 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
481 .attachmentCount = 1,
482 .pAttachments = &(VkAttachmentDescription) {
483 .format = VK_FORMAT_UNDEFINED, /* Our shaders don't care */
484 .loadOp = VK_ATTACHMENT_LOAD_OP_LOAD,
485 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
486 .initialLayout = VK_IMAGE_LAYOUT_GENERAL,
487 .finalLayout = VK_IMAGE_LAYOUT_GENERAL,
488 },
489 .subpassCount = 1,
490 .pSubpasses = &(VkSubpassDescription) {
491 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
492 .inputAttachmentCount = 0,
493 .colorAttachmentCount = 1,
494 .pColorAttachments = &(VkAttachmentReference) {
495 .attachment = 0,
496 .layout = VK_IMAGE_LAYOUT_GENERAL,
497 },
498 .pResolveAttachments = NULL,
499 .pDepthStencilAttachment = &(VkAttachmentReference) {
500 .attachment = VK_ATTACHMENT_UNUSED,
501 .layout = VK_IMAGE_LAYOUT_GENERAL,
502 },
503 .preserveAttachmentCount = 1,
504 .pPreserveAttachments = (uint32_t[]) { 0 },
505 },
506 .dependencyCount = 0,
507 }, &device->meta_state.alloc, &device->meta_state.blit.render_pass);
508 if (result != VK_SUCCESS)
509 goto fail;
510
511 /* We don't use a vertex shader for blitting, but instead build and pass
512 * the VUEs directly to the rasterization backend. However, we do need
513 * to provide GLSL source for the vertex shader so that the compiler
514 * does not dead-code our inputs.
515 */
516 struct anv_shader_module vs = {
517 .nir = build_nir_vertex_shader(),
518 };
519
520 struct anv_shader_module fs_1d = {
521 .nir = build_nir_copy_fragment_shader(GLSL_SAMPLER_DIM_1D),
522 };
523
524 struct anv_shader_module fs_2d = {
525 .nir = build_nir_copy_fragment_shader(GLSL_SAMPLER_DIM_2D),
526 };
527
528 struct anv_shader_module fs_3d = {
529 .nir = build_nir_copy_fragment_shader(GLSL_SAMPLER_DIM_3D),
530 };
531
532 VkPipelineVertexInputStateCreateInfo vi_create_info = {
533 .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
534 .vertexBindingDescriptionCount = 2,
535 .pVertexBindingDescriptions = (VkVertexInputBindingDescription[]) {
536 {
537 .binding = 0,
538 .stride = 0,
539 .inputRate = VK_VERTEX_INPUT_RATE_INSTANCE
540 },
541 {
542 .binding = 1,
543 .stride = 5 * sizeof(float),
544 .inputRate = VK_VERTEX_INPUT_RATE_VERTEX
545 },
546 },
547 .vertexAttributeDescriptionCount = 3,
548 .pVertexAttributeDescriptions = (VkVertexInputAttributeDescription[]) {
549 {
550 /* VUE Header */
551 .location = 0,
552 .binding = 0,
553 .format = VK_FORMAT_R32G32B32A32_UINT,
554 .offset = 0
555 },
556 {
557 /* Position */
558 .location = 1,
559 .binding = 1,
560 .format = VK_FORMAT_R32G32_SFLOAT,
561 .offset = 0
562 },
563 {
564 /* Texture Coordinate */
565 .location = 2,
566 .binding = 1,
567 .format = VK_FORMAT_R32G32B32_SFLOAT,
568 .offset = 8
569 }
570 }
571 };
572
573 VkDescriptorSetLayoutCreateInfo ds_layout_info = {
574 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
575 .bindingCount = 1,
576 .pBindings = (VkDescriptorSetLayoutBinding[]) {
577 {
578 .binding = 0,
579 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
580 .descriptorCount = 1,
581 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
582 .pImmutableSamplers = NULL
583 },
584 }
585 };
586 result = anv_CreateDescriptorSetLayout(anv_device_to_handle(device),
587 &ds_layout_info,
588 &device->meta_state.alloc,
589 &device->meta_state.blit.ds_layout);
590 if (result != VK_SUCCESS)
591 goto fail_render_pass;
592
593 result = anv_CreatePipelineLayout(anv_device_to_handle(device),
594 &(VkPipelineLayoutCreateInfo) {
595 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
596 .setLayoutCount = 1,
597 .pSetLayouts = &device->meta_state.blit.ds_layout,
598 },
599 &device->meta_state.alloc, &device->meta_state.blit.pipeline_layout);
600 if (result != VK_SUCCESS)
601 goto fail_descriptor_set_layout;
602
603 VkPipelineShaderStageCreateInfo pipeline_shader_stages[] = {
604 {
605 .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
606 .stage = VK_SHADER_STAGE_VERTEX_BIT,
607 .module = anv_shader_module_to_handle(&vs),
608 .pName = "main",
609 .pSpecializationInfo = NULL
610 }, {
611 .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
612 .stage = VK_SHADER_STAGE_FRAGMENT_BIT,
613 .module = VK_NULL_HANDLE, /* TEMPLATE VALUE! FILL ME IN! */
614 .pName = "main",
615 .pSpecializationInfo = NULL
616 },
617 };
618
619 const VkGraphicsPipelineCreateInfo vk_pipeline_info = {
620 .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
621 .stageCount = ARRAY_SIZE(pipeline_shader_stages),
622 .pStages = pipeline_shader_stages,
623 .pVertexInputState = &vi_create_info,
624 .pInputAssemblyState = &(VkPipelineInputAssemblyStateCreateInfo) {
625 .sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
626 .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
627 .primitiveRestartEnable = false,
628 },
629 .pViewportState = &(VkPipelineViewportStateCreateInfo) {
630 .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
631 .viewportCount = 1,
632 .scissorCount = 1,
633 },
634 .pRasterizationState = &(VkPipelineRasterizationStateCreateInfo) {
635 .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
636 .rasterizerDiscardEnable = false,
637 .polygonMode = VK_POLYGON_MODE_FILL,
638 .cullMode = VK_CULL_MODE_NONE,
639 .frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE
640 },
641 .pMultisampleState = &(VkPipelineMultisampleStateCreateInfo) {
642 .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
643 .rasterizationSamples = 1,
644 .sampleShadingEnable = false,
645 .pSampleMask = (VkSampleMask[]) { UINT32_MAX },
646 },
647 .pColorBlendState = &(VkPipelineColorBlendStateCreateInfo) {
648 .sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
649 .attachmentCount = 1,
650 .pAttachments = (VkPipelineColorBlendAttachmentState []) {
651 { .colorWriteMask =
652 VK_COLOR_COMPONENT_A_BIT |
653 VK_COLOR_COMPONENT_R_BIT |
654 VK_COLOR_COMPONENT_G_BIT |
655 VK_COLOR_COMPONENT_B_BIT },
656 }
657 },
658 .pDynamicState = &(VkPipelineDynamicStateCreateInfo) {
659 .sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
660 .dynamicStateCount = 9,
661 .pDynamicStates = (VkDynamicState[]) {
662 VK_DYNAMIC_STATE_VIEWPORT,
663 VK_DYNAMIC_STATE_SCISSOR,
664 VK_DYNAMIC_STATE_LINE_WIDTH,
665 VK_DYNAMIC_STATE_DEPTH_BIAS,
666 VK_DYNAMIC_STATE_BLEND_CONSTANTS,
667 VK_DYNAMIC_STATE_DEPTH_BOUNDS,
668 VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK,
669 VK_DYNAMIC_STATE_STENCIL_WRITE_MASK,
670 VK_DYNAMIC_STATE_STENCIL_REFERENCE,
671 },
672 },
673 .flags = 0,
674 .layout = device->meta_state.blit.pipeline_layout,
675 .renderPass = device->meta_state.blit.render_pass,
676 .subpass = 0,
677 };
678
679 const struct anv_graphics_pipeline_create_info anv_pipeline_info = {
680 .color_attachment_count = -1,
681 .use_repclear = false,
682 .disable_viewport = true,
683 .disable_scissor = true,
684 .disable_vs = true,
685 .use_rectlist = true
686 };
687
688 pipeline_shader_stages[1].module = anv_shader_module_to_handle(&fs_1d);
689 result = anv_graphics_pipeline_create(anv_device_to_handle(device),
690 VK_NULL_HANDLE,
691 &vk_pipeline_info, &anv_pipeline_info,
692 &device->meta_state.alloc, &device->meta_state.blit.pipeline_1d_src);
693 if (result != VK_SUCCESS)
694 goto fail_pipeline_layout;
695
696 pipeline_shader_stages[1].module = anv_shader_module_to_handle(&fs_2d);
697 result = anv_graphics_pipeline_create(anv_device_to_handle(device),
698 VK_NULL_HANDLE,
699 &vk_pipeline_info, &anv_pipeline_info,
700 &device->meta_state.alloc, &device->meta_state.blit.pipeline_2d_src);
701 if (result != VK_SUCCESS)
702 goto fail_pipeline_1d;
703
704 pipeline_shader_stages[1].module = anv_shader_module_to_handle(&fs_3d);
705 result = anv_graphics_pipeline_create(anv_device_to_handle(device),
706 VK_NULL_HANDLE,
707 &vk_pipeline_info, &anv_pipeline_info,
708 &device->meta_state.alloc, &device->meta_state.blit.pipeline_3d_src);
709 if (result != VK_SUCCESS)
710 goto fail_pipeline_2d;
711
712 ralloc_free(vs.nir);
713 ralloc_free(fs_1d.nir);
714 ralloc_free(fs_2d.nir);
715 ralloc_free(fs_3d.nir);
716
717 return VK_SUCCESS;
718
719 fail_pipeline_2d:
720 anv_DestroyPipeline(anv_device_to_handle(device),
721 device->meta_state.blit.pipeline_2d_src,
722 &device->meta_state.alloc);
723
724 fail_pipeline_1d:
725 anv_DestroyPipeline(anv_device_to_handle(device),
726 device->meta_state.blit.pipeline_1d_src,
727 &device->meta_state.alloc);
728
729 fail_pipeline_layout:
730 anv_DestroyPipelineLayout(anv_device_to_handle(device),
731 device->meta_state.blit.pipeline_layout,
732 &device->meta_state.alloc);
733 fail_descriptor_set_layout:
734 anv_DestroyDescriptorSetLayout(anv_device_to_handle(device),
735 device->meta_state.blit.ds_layout,
736 &device->meta_state.alloc);
737 fail_render_pass:
738 anv_DestroyRenderPass(anv_device_to_handle(device),
739 device->meta_state.blit.render_pass,
740 &device->meta_state.alloc);
741
742 ralloc_free(vs.nir);
743 ralloc_free(fs_1d.nir);
744 ralloc_free(fs_2d.nir);
745 ralloc_free(fs_3d.nir);
746 fail:
747 return result;
748 }