anv/meta: Minimize height of images used for copies
[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 void
123 anv_meta_begin_blit2d(struct anv_cmd_buffer *cmd_buffer,
124 struct anv_meta_saved_state *save)
125 {
126 meta_prepare_blit(cmd_buffer, save);
127 }
128
129
130 /* Returns the user-provided VkBufferImageCopy::imageOffset in units of
131 * elements rather than texels. One element equals one texel or one block
132 * if Image is uncompressed or compressed, respectively.
133 */
134 static struct VkOffset3D
135 meta_region_offset_el(const struct anv_image * image,
136 const struct VkOffset3D * offset)
137 {
138 const struct isl_format_layout * isl_layout = image->format->isl_layout;
139 return (VkOffset3D) {
140 .x = offset->x / isl_layout->bw,
141 .y = offset->y / isl_layout->bh,
142 .z = offset->z / isl_layout->bd,
143 };
144 }
145
146 /* Returns the user-provided VkBufferImageCopy::imageExtent in units of
147 * elements rather than texels. One element equals one texel or one block
148 * if Image is uncompressed or compressed, respectively.
149 */
150 static struct VkExtent3D
151 meta_region_extent_el(const VkFormat format,
152 const struct VkExtent3D * extent)
153 {
154 const struct isl_format_layout * isl_layout =
155 anv_format_for_vk_format(format)->isl_layout;
156 return (VkExtent3D) {
157 .width = DIV_ROUND_UP(extent->width , isl_layout->bw),
158 .height = DIV_ROUND_UP(extent->height, isl_layout->bh),
159 .depth = DIV_ROUND_UP(extent->depth , isl_layout->bd),
160 };
161 }
162
163 static void
164 meta_emit_blit(struct anv_cmd_buffer *cmd_buffer,
165 struct anv_image *src_image,
166 struct anv_image_view *src_iview,
167 VkOffset3D src_offset,
168 VkExtent3D src_extent,
169 struct anv_image *dest_image,
170 struct anv_image_view *dest_iview,
171 VkOffset3D dest_offset,
172 VkExtent3D dest_extent,
173 VkFilter blit_filter)
174 {
175 struct anv_device *device = cmd_buffer->device;
176
177 struct blit_vb_data {
178 float pos[2];
179 float tex_coord[3];
180 } *vb_data;
181
182 assert(src_image->samples == dest_image->samples);
183
184 unsigned vb_size = sizeof(struct anv_vue_header) + 3 * sizeof(*vb_data);
185
186 struct anv_state vb_state =
187 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, vb_size, 16);
188 memset(vb_state.map, 0, sizeof(struct anv_vue_header));
189 vb_data = vb_state.map + sizeof(struct anv_vue_header);
190
191 vb_data[0] = (struct blit_vb_data) {
192 .pos = {
193 dest_offset.x + dest_extent.width,
194 dest_offset.y + dest_extent.height,
195 },
196 .tex_coord = {
197 (float)(src_offset.x + src_extent.width) / (float)src_iview->extent.width,
198 (float)(src_offset.y + src_extent.height) / (float)src_iview->extent.height,
199 (float)src_offset.z / (float)src_iview->extent.depth,
200 },
201 };
202
203 vb_data[1] = (struct blit_vb_data) {
204 .pos = {
205 dest_offset.x,
206 dest_offset.y + dest_extent.height,
207 },
208 .tex_coord = {
209 (float)src_offset.x / (float)src_iview->extent.width,
210 (float)(src_offset.y + src_extent.height) / (float)src_iview->extent.height,
211 (float)src_offset.z / (float)src_iview->extent.depth,
212 },
213 };
214
215 vb_data[2] = (struct blit_vb_data) {
216 .pos = {
217 dest_offset.x,
218 dest_offset.y,
219 },
220 .tex_coord = {
221 (float)src_offset.x / (float)src_iview->extent.width,
222 (float)src_offset.y / (float)src_iview->extent.height,
223 (float)src_offset.z / (float)src_iview->extent.depth,
224 },
225 };
226
227 anv_state_clflush(vb_state);
228
229 struct anv_buffer vertex_buffer = {
230 .device = device,
231 .size = vb_size,
232 .bo = &device->dynamic_state_block_pool.bo,
233 .offset = vb_state.offset,
234 };
235
236 anv_CmdBindVertexBuffers(anv_cmd_buffer_to_handle(cmd_buffer), 0, 2,
237 (VkBuffer[]) {
238 anv_buffer_to_handle(&vertex_buffer),
239 anv_buffer_to_handle(&vertex_buffer)
240 },
241 (VkDeviceSize[]) {
242 0,
243 sizeof(struct anv_vue_header),
244 });
245
246 VkSampler sampler;
247 ANV_CALL(CreateSampler)(anv_device_to_handle(device),
248 &(VkSamplerCreateInfo) {
249 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
250 .magFilter = blit_filter,
251 .minFilter = blit_filter,
252 }, &cmd_buffer->pool->alloc, &sampler);
253
254 VkDescriptorPool desc_pool;
255 anv_CreateDescriptorPool(anv_device_to_handle(device),
256 &(const VkDescriptorPoolCreateInfo) {
257 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
258 .pNext = NULL,
259 .flags = 0,
260 .maxSets = 1,
261 .poolSizeCount = 1,
262 .pPoolSizes = (VkDescriptorPoolSize[]) {
263 {
264 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
265 .descriptorCount = 1
266 },
267 }
268 }, &cmd_buffer->pool->alloc, &desc_pool);
269
270 VkDescriptorSet set;
271 anv_AllocateDescriptorSets(anv_device_to_handle(device),
272 &(VkDescriptorSetAllocateInfo) {
273 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
274 .descriptorPool = desc_pool,
275 .descriptorSetCount = 1,
276 .pSetLayouts = &device->meta_state.blit.ds_layout
277 }, &set);
278
279 anv_UpdateDescriptorSets(anv_device_to_handle(device),
280 1, /* writeCount */
281 (VkWriteDescriptorSet[]) {
282 {
283 .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
284 .dstSet = set,
285 .dstBinding = 0,
286 .dstArrayElement = 0,
287 .descriptorCount = 1,
288 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
289 .pImageInfo = (VkDescriptorImageInfo[]) {
290 {
291 .sampler = sampler,
292 .imageView = anv_image_view_to_handle(src_iview),
293 .imageLayout = VK_IMAGE_LAYOUT_GENERAL,
294 },
295 }
296 }
297 }, 0, NULL);
298
299 VkFramebuffer fb;
300 anv_CreateFramebuffer(anv_device_to_handle(device),
301 &(VkFramebufferCreateInfo) {
302 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
303 .attachmentCount = 1,
304 .pAttachments = (VkImageView[]) {
305 anv_image_view_to_handle(dest_iview),
306 },
307 .width = dest_iview->extent.width,
308 .height = dest_iview->extent.height,
309 .layers = 1
310 }, &cmd_buffer->pool->alloc, &fb);
311
312 ANV_CALL(CmdBeginRenderPass)(anv_cmd_buffer_to_handle(cmd_buffer),
313 &(VkRenderPassBeginInfo) {
314 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
315 .renderPass = device->meta_state.blit.render_pass,
316 .framebuffer = fb,
317 .renderArea = {
318 .offset = { dest_offset.x, dest_offset.y },
319 .extent = { dest_extent.width, dest_extent.height },
320 },
321 .clearValueCount = 0,
322 .pClearValues = NULL,
323 }, VK_SUBPASS_CONTENTS_INLINE);
324
325 VkPipeline pipeline;
326
327 switch (src_image->type) {
328 case VK_IMAGE_TYPE_1D:
329 pipeline = device->meta_state.blit.pipeline_1d_src;
330 break;
331 case VK_IMAGE_TYPE_2D:
332 pipeline = device->meta_state.blit.pipeline_2d_src;
333 break;
334 case VK_IMAGE_TYPE_3D:
335 pipeline = device->meta_state.blit.pipeline_3d_src;
336 break;
337 default:
338 unreachable(!"bad VkImageType");
339 }
340
341 if (cmd_buffer->state.pipeline != anv_pipeline_from_handle(pipeline)) {
342 anv_CmdBindPipeline(anv_cmd_buffer_to_handle(cmd_buffer),
343 VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
344 }
345
346 anv_CmdSetViewport(anv_cmd_buffer_to_handle(cmd_buffer), 0, 1,
347 &(VkViewport) {
348 .x = 0.0f,
349 .y = 0.0f,
350 .width = dest_iview->extent.width,
351 .height = dest_iview->extent.height,
352 .minDepth = 0.0f,
353 .maxDepth = 1.0f,
354 });
355
356 anv_CmdBindDescriptorSets(anv_cmd_buffer_to_handle(cmd_buffer),
357 VK_PIPELINE_BIND_POINT_GRAPHICS,
358 device->meta_state.blit.pipeline_layout, 0, 1,
359 &set, 0, NULL);
360
361 ANV_CALL(CmdDraw)(anv_cmd_buffer_to_handle(cmd_buffer), 3, 1, 0, 0);
362
363 ANV_CALL(CmdEndRenderPass)(anv_cmd_buffer_to_handle(cmd_buffer));
364
365 /* At the point where we emit the draw call, all data from the
366 * descriptor sets, etc. has been used. We are free to delete it.
367 */
368 anv_DestroyDescriptorPool(anv_device_to_handle(device),
369 desc_pool, &cmd_buffer->pool->alloc);
370 anv_DestroySampler(anv_device_to_handle(device), sampler,
371 &cmd_buffer->pool->alloc);
372 anv_DestroyFramebuffer(anv_device_to_handle(device), fb,
373 &cmd_buffer->pool->alloc);
374 }
375
376 static void
377 meta_finish_blit(struct anv_cmd_buffer *cmd_buffer,
378 const struct anv_meta_saved_state *saved_state)
379 {
380 anv_meta_restore(saved_state, cmd_buffer);
381 }
382
383 void
384 anv_meta_end_blit2d(struct anv_cmd_buffer *cmd_buffer,
385 struct anv_meta_saved_state *save)
386 {
387 meta_finish_blit(cmd_buffer, save);
388 }
389
390 static VkFormat
391 vk_format_for_size(int bs)
392 {
393 /* The choice of UNORM and UINT formats is very intentional here. Most of
394 * the time, we want to use a UINT format to avoid any rounding error in
395 * the blit. For stencil blits, R8_UINT is required by the hardware.
396 * (It's the only format allowed in conjunction with W-tiling.) Also we
397 * intentionally use the 4-channel formats whenever we can. This is so
398 * that, when we do a RGB <-> RGBX copy, the two formats will line up even
399 * though one of them is 3/4 the size of the other. The choice of UNORM
400 * vs. UINT is also very intentional because Haswell doesn't handle 8 or
401 * 16-bit RGB UINT formats at all so we have to use UNORM there.
402 * Fortunately, the only time we should ever use two different formats in
403 * the table below is for RGB -> RGBA blits and so we will never have any
404 * UNORM/UINT mismatch.
405 */
406 switch (bs) {
407 case 1: return VK_FORMAT_R8_UINT;
408 case 2: return VK_FORMAT_R8G8_UINT;
409 case 3: return VK_FORMAT_R8G8B8_UNORM;
410 case 4: return VK_FORMAT_R8G8B8A8_UNORM;
411 case 6: return VK_FORMAT_R16G16B16_UNORM;
412 case 8: return VK_FORMAT_R16G16B16A16_UNORM;
413 case 12: return VK_FORMAT_R32G32B32_UINT;
414 case 16: return VK_FORMAT_R32G32B32A32_UINT;
415 default:
416 unreachable("Invalid format block size");
417 }
418 }
419
420 static struct anv_meta_blit2d_surf
421 blit_surf_for_image(const struct anv_image* image,
422 const struct isl_surf *img_isl_surf)
423 {
424 return (struct anv_meta_blit2d_surf) {
425 .bo = image->bo,
426 .tiling = img_isl_surf->tiling,
427 .base_offset = image->offset,
428 .bs = isl_format_get_layout(img_isl_surf->format)->bs,
429 .pitch = isl_surf_get_row_pitch(img_isl_surf),
430 };
431 }
432
433 void
434 anv_meta_blit2d(struct anv_cmd_buffer *cmd_buffer,
435 struct anv_meta_blit2d_surf *src,
436 struct anv_meta_blit2d_surf *dst,
437 unsigned num_rects,
438 struct anv_meta_blit2d_rect *rects)
439 {
440 VkDevice vk_device = anv_device_to_handle(cmd_buffer->device);
441 VkFormat src_format = vk_format_for_size(src->bs);
442 VkFormat dst_format = vk_format_for_size(dst->bs);
443
444 for (unsigned r = 0; r < num_rects; ++r) {
445
446 /* Create VkImages */
447 VkImageCreateInfo image_info = {
448 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
449 .imageType = VK_IMAGE_TYPE_2D,
450 .format = 0, /* TEMPLATE */
451 .extent = {
452 .width = 0, /* TEMPLATE */
453 .height = 0, /* TEMPLATE */
454 .depth = 1,
455 },
456 .mipLevels = 1,
457 .arrayLayers = 1,
458 .samples = 1,
459 .tiling = 0, /* TEMPLATE */
460 .usage = 0, /* TEMPLATE */
461 };
462 struct anv_image_create_info anv_image_info = {
463 .vk_info = &image_info,
464 .isl_tiling_flags = 0, /* TEMPLATE */
465 };
466
467 /* The image height is the rect height + src/dst y-offset from the
468 * tile-aligned base address.
469 */
470 struct isl_tile_info tile_info;
471
472 anv_image_info.isl_tiling_flags = 1 << src->tiling;
473 image_info.tiling = anv_image_info.isl_tiling_flags == ISL_TILING_LINEAR_BIT ?
474 VK_IMAGE_TILING_LINEAR : VK_IMAGE_TILING_OPTIMAL;
475 image_info.usage = VK_IMAGE_USAGE_SAMPLED_BIT;
476 image_info.format = src_format,
477 isl_tiling_get_info(&cmd_buffer->device->isl_dev, src->tiling, src->bs, &tile_info);
478 image_info.extent.height = rects[r].height +
479 rects[r].src_y % tile_info.height;
480 image_info.extent.width = src->pitch / src->bs;
481 VkImage src_image;
482 anv_image_create(vk_device, &anv_image_info,
483 &cmd_buffer->pool->alloc, &src_image);
484
485 anv_image_info.isl_tiling_flags = 1 << dst->tiling;
486 image_info.tiling = anv_image_info.isl_tiling_flags == ISL_TILING_LINEAR_BIT ?
487 VK_IMAGE_TILING_LINEAR : VK_IMAGE_TILING_OPTIMAL;
488 image_info.usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
489 image_info.format = dst_format,
490 isl_tiling_get_info(&cmd_buffer->device->isl_dev, dst->tiling, dst->bs, &tile_info);
491 image_info.extent.height = rects[r].height +
492 rects[r].dst_y % tile_info.height;
493 image_info.extent.width = dst->pitch / dst->bs;
494 VkImage dst_image;
495 anv_image_create(vk_device, &anv_image_info,
496 &cmd_buffer->pool->alloc, &dst_image);
497
498 /* We could use a vk call to bind memory, but that would require
499 * creating a dummy memory object etc. so there's really no point.
500 */
501 anv_image_from_handle(src_image)->bo = src->bo;
502 anv_image_from_handle(src_image)->offset = src->base_offset;
503 anv_image_from_handle(dst_image)->bo = dst->bo;
504 anv_image_from_handle(dst_image)->offset = dst->base_offset;
505
506 /* Create VkImageViews */
507 VkImageViewCreateInfo iview_info = {
508 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
509 .image = 0, /* TEMPLATE */
510 .viewType = VK_IMAGE_VIEW_TYPE_2D,
511 .format = 0, /* TEMPLATE */
512 .subresourceRange = {
513 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
514 .baseMipLevel = 0,
515 .levelCount = 1,
516 .baseArrayLayer = 0,
517 .layerCount = 1
518 },
519 };
520 uint32_t img_o = 0;
521
522 iview_info.image = src_image;
523 iview_info.format = src_format;
524 VkOffset3D src_offset_el = {0};
525 isl_surf_get_image_intratile_offset_el_xy(&cmd_buffer->device->isl_dev,
526 &anv_image_from_handle(src_image)->
527 color_surface.isl,
528 rects[r].src_x,
529 rects[r].src_y,
530 &img_o,
531 (uint32_t*)&src_offset_el.x,
532 (uint32_t*)&src_offset_el.y);
533
534 struct anv_image_view src_iview;
535 anv_image_view_init(&src_iview, cmd_buffer->device,
536 &iview_info, cmd_buffer, img_o, VK_IMAGE_USAGE_SAMPLED_BIT);
537
538 iview_info.image = dst_image;
539 iview_info.format = dst_format;
540 VkOffset3D dst_offset_el = {0};
541 isl_surf_get_image_intratile_offset_el_xy(&cmd_buffer->device->isl_dev,
542 &anv_image_from_handle(dst_image)->
543 color_surface.isl,
544 rects[r].dst_x,
545 rects[r].dst_y,
546 &img_o,
547 (uint32_t*)&dst_offset_el.x,
548 (uint32_t*)&dst_offset_el.y);
549 struct anv_image_view dst_iview;
550 anv_image_view_init(&dst_iview, cmd_buffer->device,
551 &iview_info, cmd_buffer, img_o, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
552
553 /* Perform blit */
554 meta_emit_blit(cmd_buffer,
555 anv_image_from_handle(src_image),
556 &src_iview,
557 src_offset_el,
558 (VkExtent3D){rects[r].width, rects[r].height, 1},
559 anv_image_from_handle(dst_image),
560 &dst_iview,
561 dst_offset_el,
562 (VkExtent3D){rects[r].width, rects[r].height, 1},
563 VK_FILTER_NEAREST);
564
565 anv_DestroyImage(vk_device, src_image, &cmd_buffer->pool->alloc);
566 anv_DestroyImage(vk_device, dst_image, &cmd_buffer->pool->alloc);
567 }
568 }
569
570 static void
571 do_buffer_copy(struct anv_cmd_buffer *cmd_buffer,
572 struct anv_bo *src, uint64_t src_offset,
573 struct anv_bo *dest, uint64_t dest_offset,
574 int width, int height, int bs)
575 {
576 struct anv_meta_blit2d_surf b_src = {
577 .bo = src,
578 .tiling = ISL_TILING_LINEAR,
579 .base_offset = src_offset,
580 .bs = bs,
581 .pitch = width * bs,
582 };
583 struct anv_meta_blit2d_surf b_dst = {
584 .bo = dest,
585 .tiling = ISL_TILING_LINEAR,
586 .base_offset = dest_offset,
587 .bs = bs,
588 .pitch = width * bs,
589 };
590 struct anv_meta_blit2d_rect rect = {
591 .width = width,
592 .height = height,
593 };
594 anv_meta_blit2d(cmd_buffer,
595 &b_src,
596 &b_dst,
597 1,
598 &rect);
599 }
600
601 void anv_CmdCopyBuffer(
602 VkCommandBuffer commandBuffer,
603 VkBuffer srcBuffer,
604 VkBuffer destBuffer,
605 uint32_t regionCount,
606 const VkBufferCopy* pRegions)
607 {
608 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
609 ANV_FROM_HANDLE(anv_buffer, src_buffer, srcBuffer);
610 ANV_FROM_HANDLE(anv_buffer, dest_buffer, destBuffer);
611
612 struct anv_meta_saved_state saved_state;
613
614 anv_meta_begin_blit2d(cmd_buffer, &saved_state);
615
616 for (unsigned r = 0; r < regionCount; r++) {
617 uint64_t src_offset = src_buffer->offset + pRegions[r].srcOffset;
618 uint64_t dest_offset = dest_buffer->offset + pRegions[r].dstOffset;
619 uint64_t copy_size = pRegions[r].size;
620
621 /* First, we compute the biggest format that can be used with the
622 * given offsets and size.
623 */
624 int bs = 16;
625
626 int fs = ffs(src_offset) - 1;
627 if (fs != -1)
628 bs = MIN2(bs, 1 << fs);
629 assert(src_offset % bs == 0);
630
631 fs = ffs(dest_offset) - 1;
632 if (fs != -1)
633 bs = MIN2(bs, 1 << fs);
634 assert(dest_offset % bs == 0);
635
636 fs = ffs(pRegions[r].size) - 1;
637 if (fs != -1)
638 bs = MIN2(bs, 1 << fs);
639 assert(pRegions[r].size % bs == 0);
640
641 /* This is maximum possible width/height our HW can handle */
642 uint64_t max_surface_dim = 1 << 14;
643
644 /* First, we make a bunch of max-sized copies */
645 uint64_t max_copy_size = max_surface_dim * max_surface_dim * bs;
646 while (copy_size >= max_copy_size) {
647 do_buffer_copy(cmd_buffer, src_buffer->bo, src_offset,
648 dest_buffer->bo, dest_offset,
649 max_surface_dim, max_surface_dim, bs);
650 copy_size -= max_copy_size;
651 src_offset += max_copy_size;
652 dest_offset += max_copy_size;
653 }
654
655 uint64_t height = copy_size / (max_surface_dim * bs);
656 assert(height < max_surface_dim);
657 if (height != 0) {
658 uint64_t rect_copy_size = height * max_surface_dim * bs;
659 do_buffer_copy(cmd_buffer, src_buffer->bo, src_offset,
660 dest_buffer->bo, dest_offset,
661 max_surface_dim, height, bs);
662 copy_size -= rect_copy_size;
663 src_offset += rect_copy_size;
664 dest_offset += rect_copy_size;
665 }
666
667 if (copy_size != 0) {
668 do_buffer_copy(cmd_buffer, src_buffer->bo, src_offset,
669 dest_buffer->bo, dest_offset,
670 copy_size / bs, 1, bs);
671 }
672 }
673
674 anv_meta_end_blit2d(cmd_buffer, &saved_state);
675 }
676
677 void anv_CmdUpdateBuffer(
678 VkCommandBuffer commandBuffer,
679 VkBuffer dstBuffer,
680 VkDeviceSize dstOffset,
681 VkDeviceSize dataSize,
682 const uint32_t* pData)
683 {
684 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
685 ANV_FROM_HANDLE(anv_buffer, dst_buffer, dstBuffer);
686 struct anv_meta_saved_state saved_state;
687
688 anv_meta_begin_blit2d(cmd_buffer, &saved_state);
689
690 /* We can't quite grab a full block because the state stream needs a
691 * little data at the top to build its linked list.
692 */
693 const uint32_t max_update_size =
694 cmd_buffer->device->dynamic_state_block_pool.block_size - 64;
695
696 assert(max_update_size < (1 << 14) * 4);
697
698 while (dataSize) {
699 const uint32_t copy_size = MIN2(dataSize, max_update_size);
700
701 struct anv_state tmp_data =
702 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, copy_size, 64);
703
704 memcpy(tmp_data.map, pData, copy_size);
705
706 int bs;
707 if ((copy_size & 15) == 0 && (dstOffset & 15) == 0) {
708 bs = 16;
709 } else if ((copy_size & 7) == 0 && (dstOffset & 7) == 0) {
710 bs = 8;
711 } else {
712 assert((copy_size & 3) == 0 && (dstOffset & 3) == 0);
713 bs = 4;
714 }
715
716 do_buffer_copy(cmd_buffer,
717 &cmd_buffer->device->dynamic_state_block_pool.bo,
718 tmp_data.offset,
719 dst_buffer->bo, dst_buffer->offset + dstOffset,
720 copy_size / bs, 1, bs);
721
722 dataSize -= copy_size;
723 dstOffset += copy_size;
724 pData = (void *)pData + copy_size;
725 }
726
727 anv_meta_end_blit2d(cmd_buffer, &saved_state);
728 }
729
730 void anv_CmdCopyImage(
731 VkCommandBuffer commandBuffer,
732 VkImage srcImage,
733 VkImageLayout srcImageLayout,
734 VkImage destImage,
735 VkImageLayout destImageLayout,
736 uint32_t regionCount,
737 const VkImageCopy* pRegions)
738 {
739 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
740 ANV_FROM_HANDLE(anv_image, src_image, srcImage);
741 ANV_FROM_HANDLE(anv_image, dest_image, destImage);
742 struct anv_meta_saved_state saved_state;
743
744 /* From the Vulkan 1.0 spec:
745 *
746 * vkCmdCopyImage can be used to copy image data between multisample
747 * images, but both images must have the same number of samples.
748 */
749 assert(src_image->samples == dest_image->samples);
750
751 anv_meta_begin_blit2d(cmd_buffer, &saved_state);
752
753 for (unsigned r = 0; r < regionCount; r++) {
754 assert(pRegions[r].srcSubresource.aspectMask ==
755 pRegions[r].dstSubresource.aspectMask);
756
757 VkImageAspectFlags aspect = pRegions[r].srcSubresource.aspectMask;
758
759 /* Create blit surfaces */
760 struct isl_surf *src_isl_surf =
761 &anv_image_get_surface_for_aspect_mask(src_image, aspect)->isl;
762 struct isl_surf *dst_isl_surf =
763 &anv_image_get_surface_for_aspect_mask(dest_image, aspect)->isl;
764 struct anv_meta_blit2d_surf b_src = blit_surf_for_image(src_image, src_isl_surf);
765 struct anv_meta_blit2d_surf b_dst = blit_surf_for_image(dest_image, dst_isl_surf);
766
767 /* Start creating blit rect */
768 const VkOffset3D dst_offset_el = meta_region_offset_el(dest_image, &pRegions[r].dstOffset);
769 const VkOffset3D src_offset_el = meta_region_offset_el(src_image, &pRegions[r].srcOffset);
770 const VkExtent3D img_extent_el = meta_region_extent_el(src_image->vk_format,
771 &pRegions[r].extent);
772 struct anv_meta_blit2d_rect rect = {
773 .width = img_extent_el.width,
774 .height = img_extent_el.height,
775 };
776
777 /* Loop through each 3D or array slice */
778 unsigned num_slices_3d = pRegions[r].extent.depth;
779 unsigned num_slices_array = pRegions[r].dstSubresource.layerCount;
780 unsigned slice_3d = 0;
781 unsigned slice_array = 0;
782 while (slice_3d < num_slices_3d && slice_array < num_slices_array) {
783
784 /* Finish creating blit rect */
785 isl_surf_get_image_offset_el(dst_isl_surf,
786 pRegions[r].dstSubresource.mipLevel,
787 pRegions[r].dstSubresource.baseArrayLayer + slice_array,
788 pRegions[r].dstOffset.z + slice_3d,
789 &rect.dst_x,
790 &rect.dst_y);
791 isl_surf_get_image_offset_el(src_isl_surf,
792 pRegions[r].srcSubresource.mipLevel,
793 pRegions[r].srcSubresource.baseArrayLayer + slice_array,
794 pRegions[r].srcOffset.z + slice_3d,
795 &rect.src_x,
796 &rect.src_y);
797 rect.dst_x += dst_offset_el.x;
798 rect.dst_y += dst_offset_el.y;
799 rect.src_x += src_offset_el.x;
800 rect.src_y += src_offset_el.y;
801
802 /* Perform Blit */
803 anv_meta_blit2d(cmd_buffer,
804 &b_src,
805 &b_dst,
806 1,
807 &rect);
808
809 if (dest_image->type == VK_IMAGE_TYPE_3D)
810 slice_3d++;
811 else
812 slice_array++;
813 }
814 }
815
816 anv_meta_end_blit2d(cmd_buffer, &saved_state);
817 }
818
819 void anv_CmdBlitImage(
820 VkCommandBuffer commandBuffer,
821 VkImage srcImage,
822 VkImageLayout srcImageLayout,
823 VkImage destImage,
824 VkImageLayout destImageLayout,
825 uint32_t regionCount,
826 const VkImageBlit* pRegions,
827 VkFilter filter)
828
829 {
830 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
831 ANV_FROM_HANDLE(anv_image, src_image, srcImage);
832 ANV_FROM_HANDLE(anv_image, dest_image, destImage);
833 struct anv_meta_saved_state saved_state;
834
835 /* From the Vulkan 1.0 spec:
836 *
837 * vkCmdBlitImage must not be used for multisampled source or
838 * destination images. Use vkCmdResolveImage for this purpose.
839 */
840 assert(src_image->samples == 1);
841 assert(dest_image->samples == 1);
842
843 anv_finishme("respect VkFilter");
844
845 meta_prepare_blit(cmd_buffer, &saved_state);
846
847 for (unsigned r = 0; r < regionCount; r++) {
848 struct anv_image_view src_iview;
849 anv_image_view_init(&src_iview, cmd_buffer->device,
850 &(VkImageViewCreateInfo) {
851 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
852 .image = srcImage,
853 .viewType = anv_meta_get_view_type(src_image),
854 .format = src_image->vk_format,
855 .subresourceRange = {
856 .aspectMask = pRegions[r].srcSubresource.aspectMask,
857 .baseMipLevel = pRegions[r].srcSubresource.mipLevel,
858 .levelCount = 1,
859 .baseArrayLayer = pRegions[r].srcSubresource.baseArrayLayer,
860 .layerCount = 1
861 },
862 },
863 cmd_buffer, 0, VK_IMAGE_USAGE_SAMPLED_BIT);
864
865 const VkOffset3D dest_offset = {
866 .x = pRegions[r].dstOffsets[0].x,
867 .y = pRegions[r].dstOffsets[0].y,
868 .z = 0,
869 };
870
871 if (pRegions[r].dstOffsets[1].x < pRegions[r].dstOffsets[0].x ||
872 pRegions[r].dstOffsets[1].y < pRegions[r].dstOffsets[0].y ||
873 pRegions[r].srcOffsets[1].x < pRegions[r].srcOffsets[0].x ||
874 pRegions[r].srcOffsets[1].y < pRegions[r].srcOffsets[0].y)
875 anv_finishme("FINISHME: Allow flipping in blits");
876
877 const VkExtent3D dest_extent = {
878 .width = pRegions[r].dstOffsets[1].x - pRegions[r].dstOffsets[0].x,
879 .height = pRegions[r].dstOffsets[1].y - pRegions[r].dstOffsets[0].y,
880 };
881
882 const VkExtent3D src_extent = {
883 .width = pRegions[r].srcOffsets[1].x - pRegions[r].srcOffsets[0].x,
884 .height = pRegions[r].srcOffsets[1].y - pRegions[r].srcOffsets[0].y,
885 };
886
887 const uint32_t dest_array_slice =
888 anv_meta_get_iview_layer(dest_image, &pRegions[r].dstSubresource,
889 &pRegions[r].dstOffsets[0]);
890
891 if (pRegions[r].srcSubresource.layerCount > 1)
892 anv_finishme("FINISHME: copy multiple array layers");
893
894 if (pRegions[r].srcOffsets[0].z + 1 != pRegions[r].srcOffsets[1].z ||
895 pRegions[r].dstOffsets[0].z + 1 != pRegions[r].dstOffsets[1].z)
896 anv_finishme("FINISHME: copy multiple depth layers");
897
898 struct anv_image_view dest_iview;
899 anv_image_view_init(&dest_iview, cmd_buffer->device,
900 &(VkImageViewCreateInfo) {
901 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
902 .image = destImage,
903 .viewType = anv_meta_get_view_type(dest_image),
904 .format = dest_image->vk_format,
905 .subresourceRange = {
906 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
907 .baseMipLevel = pRegions[r].dstSubresource.mipLevel,
908 .levelCount = 1,
909 .baseArrayLayer = dest_array_slice,
910 .layerCount = 1
911 },
912 },
913 cmd_buffer, 0, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
914
915 meta_emit_blit(cmd_buffer,
916 src_image, &src_iview,
917 pRegions[r].srcOffsets[0], src_extent,
918 dest_image, &dest_iview,
919 dest_offset, dest_extent,
920 filter);
921 }
922
923 meta_finish_blit(cmd_buffer, &saved_state);
924 }
925
926 static void
927 meta_copy_buffer_to_image(struct anv_cmd_buffer *cmd_buffer,
928 struct anv_buffer* buffer,
929 struct anv_image* image,
930 uint32_t regionCount,
931 const VkBufferImageCopy* pRegions,
932 bool forward)
933 {
934 struct anv_meta_saved_state saved_state;
935
936 /* The Vulkan 1.0 spec says "dstImage must have a sample count equal to
937 * VK_SAMPLE_COUNT_1_BIT."
938 */
939 assert(image->samples == 1);
940
941 anv_meta_begin_blit2d(cmd_buffer, &saved_state);
942
943 for (unsigned r = 0; r < regionCount; r++) {
944
945 /* Start creating blit rect */
946 const VkOffset3D img_offset_el = meta_region_offset_el(image, &pRegions[r].imageOffset);
947 const VkExtent3D bufferExtent = {
948 .width = pRegions[r].bufferRowLength,
949 .height = pRegions[r].bufferImageHeight,
950 };
951 const VkExtent3D buf_extent_el = meta_region_extent_el(image->vk_format, &bufferExtent);
952 const VkExtent3D img_extent_el = meta_region_extent_el(image->vk_format,
953 &pRegions[r].imageExtent);
954 struct anv_meta_blit2d_rect rect = {
955 .width = MAX2(buf_extent_el.width, img_extent_el.width),
956 .height = MAX2(buf_extent_el.height, img_extent_el.height),
957 };
958
959 /* Create blit surfaces */
960 VkImageAspectFlags aspect = pRegions[r].imageSubresource.aspectMask;
961 const struct isl_surf *img_isl_surf =
962 &anv_image_get_surface_for_aspect_mask(image, aspect)->isl;
963 struct anv_meta_blit2d_surf img_bsurf = blit_surf_for_image(image, img_isl_surf);
964 struct anv_meta_blit2d_surf buf_bsurf = {
965 .bo = buffer->bo,
966 .tiling = ISL_TILING_LINEAR,
967 .base_offset = buffer->offset + pRegions[r].bufferOffset,
968 .bs = forward ? image->format->isl_layout->bs : img_bsurf.bs,
969 .pitch = rect.width * buf_bsurf.bs,
970 };
971
972 /* Set direction-dependent variables */
973 struct anv_meta_blit2d_surf *dst_bsurf = forward ? &img_bsurf : &buf_bsurf;
974 struct anv_meta_blit2d_surf *src_bsurf = forward ? &buf_bsurf : &img_bsurf;
975 uint32_t *x_offset = forward ? &rect.dst_x : &rect.src_x;
976 uint32_t *y_offset = forward ? &rect.dst_y : &rect.src_y;
977
978 /* Loop through each 3D or array slice */
979 unsigned num_slices_3d = pRegions[r].imageExtent.depth;
980 unsigned num_slices_array = pRegions[r].imageSubresource.layerCount;
981 unsigned slice_3d = 0;
982 unsigned slice_array = 0;
983 while (slice_3d < num_slices_3d && slice_array < num_slices_array) {
984
985 /* Finish creating blit rect */
986 isl_surf_get_image_offset_el(img_isl_surf,
987 pRegions[r].imageSubresource.mipLevel,
988 pRegions[r].imageSubresource.baseArrayLayer + slice_array,
989 pRegions[r].imageOffset.z + slice_3d,
990 x_offset,
991 y_offset);
992 *x_offset += img_offset_el.x;
993 *y_offset += img_offset_el.y;
994
995 /* Perform Blit */
996 anv_meta_blit2d(cmd_buffer,
997 src_bsurf,
998 dst_bsurf,
999 1,
1000 &rect);
1001
1002 /* Once we've done the blit, all of the actual information about
1003 * the image is embedded in the command buffer so we can just
1004 * increment the offset directly in the image effectively
1005 * re-binding it to different backing memory.
1006 */
1007 buf_bsurf.base_offset += rect.width * rect.height * buf_bsurf.bs;
1008
1009 if (image->type == VK_IMAGE_TYPE_3D)
1010 slice_3d++;
1011 else
1012 slice_array++;
1013 }
1014 }
1015 anv_meta_end_blit2d(cmd_buffer, &saved_state);
1016 }
1017
1018 void anv_CmdCopyBufferToImage(
1019 VkCommandBuffer commandBuffer,
1020 VkBuffer srcBuffer,
1021 VkImage destImage,
1022 VkImageLayout destImageLayout,
1023 uint32_t regionCount,
1024 const VkBufferImageCopy* pRegions)
1025 {
1026 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
1027 ANV_FROM_HANDLE(anv_image, dest_image, destImage);
1028 ANV_FROM_HANDLE(anv_buffer, src_buffer, srcBuffer);
1029
1030 meta_copy_buffer_to_image(cmd_buffer, src_buffer, dest_image,
1031 regionCount, pRegions, true);
1032 }
1033
1034 void anv_CmdCopyImageToBuffer(
1035 VkCommandBuffer commandBuffer,
1036 VkImage srcImage,
1037 VkImageLayout srcImageLayout,
1038 VkBuffer destBuffer,
1039 uint32_t regionCount,
1040 const VkBufferImageCopy* pRegions)
1041 {
1042 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
1043 ANV_FROM_HANDLE(anv_image, src_image, srcImage);
1044 ANV_FROM_HANDLE(anv_buffer, dst_buffer, destBuffer);
1045
1046 meta_copy_buffer_to_image(cmd_buffer, dst_buffer, src_image,
1047 regionCount, pRegions, false);
1048 }
1049
1050 void
1051 anv_device_finish_meta_blit_state(struct anv_device *device)
1052 {
1053 anv_DestroyRenderPass(anv_device_to_handle(device),
1054 device->meta_state.blit.render_pass,
1055 &device->meta_state.alloc);
1056 anv_DestroyPipeline(anv_device_to_handle(device),
1057 device->meta_state.blit.pipeline_1d_src,
1058 &device->meta_state.alloc);
1059 anv_DestroyPipeline(anv_device_to_handle(device),
1060 device->meta_state.blit.pipeline_2d_src,
1061 &device->meta_state.alloc);
1062 anv_DestroyPipeline(anv_device_to_handle(device),
1063 device->meta_state.blit.pipeline_3d_src,
1064 &device->meta_state.alloc);
1065 anv_DestroyPipelineLayout(anv_device_to_handle(device),
1066 device->meta_state.blit.pipeline_layout,
1067 &device->meta_state.alloc);
1068 anv_DestroyDescriptorSetLayout(anv_device_to_handle(device),
1069 device->meta_state.blit.ds_layout,
1070 &device->meta_state.alloc);
1071 }
1072
1073 VkResult
1074 anv_device_init_meta_blit_state(struct anv_device *device)
1075 {
1076 VkResult result;
1077
1078 result = anv_CreateRenderPass(anv_device_to_handle(device),
1079 &(VkRenderPassCreateInfo) {
1080 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1081 .attachmentCount = 1,
1082 .pAttachments = &(VkAttachmentDescription) {
1083 .format = VK_FORMAT_UNDEFINED, /* Our shaders don't care */
1084 .loadOp = VK_ATTACHMENT_LOAD_OP_LOAD,
1085 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1086 .initialLayout = VK_IMAGE_LAYOUT_GENERAL,
1087 .finalLayout = VK_IMAGE_LAYOUT_GENERAL,
1088 },
1089 .subpassCount = 1,
1090 .pSubpasses = &(VkSubpassDescription) {
1091 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1092 .inputAttachmentCount = 0,
1093 .colorAttachmentCount = 1,
1094 .pColorAttachments = &(VkAttachmentReference) {
1095 .attachment = 0,
1096 .layout = VK_IMAGE_LAYOUT_GENERAL,
1097 },
1098 .pResolveAttachments = NULL,
1099 .pDepthStencilAttachment = &(VkAttachmentReference) {
1100 .attachment = VK_ATTACHMENT_UNUSED,
1101 .layout = VK_IMAGE_LAYOUT_GENERAL,
1102 },
1103 .preserveAttachmentCount = 1,
1104 .pPreserveAttachments = (uint32_t[]) { 0 },
1105 },
1106 .dependencyCount = 0,
1107 }, &device->meta_state.alloc, &device->meta_state.blit.render_pass);
1108 if (result != VK_SUCCESS)
1109 goto fail;
1110
1111 /* We don't use a vertex shader for blitting, but instead build and pass
1112 * the VUEs directly to the rasterization backend. However, we do need
1113 * to provide GLSL source for the vertex shader so that the compiler
1114 * does not dead-code our inputs.
1115 */
1116 struct anv_shader_module vs = {
1117 .nir = build_nir_vertex_shader(),
1118 };
1119
1120 struct anv_shader_module fs_1d = {
1121 .nir = build_nir_copy_fragment_shader(GLSL_SAMPLER_DIM_1D),
1122 };
1123
1124 struct anv_shader_module fs_2d = {
1125 .nir = build_nir_copy_fragment_shader(GLSL_SAMPLER_DIM_2D),
1126 };
1127
1128 struct anv_shader_module fs_3d = {
1129 .nir = build_nir_copy_fragment_shader(GLSL_SAMPLER_DIM_3D),
1130 };
1131
1132 VkPipelineVertexInputStateCreateInfo vi_create_info = {
1133 .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
1134 .vertexBindingDescriptionCount = 2,
1135 .pVertexBindingDescriptions = (VkVertexInputBindingDescription[]) {
1136 {
1137 .binding = 0,
1138 .stride = 0,
1139 .inputRate = VK_VERTEX_INPUT_RATE_VERTEX
1140 },
1141 {
1142 .binding = 1,
1143 .stride = 5 * sizeof(float),
1144 .inputRate = VK_VERTEX_INPUT_RATE_VERTEX
1145 },
1146 },
1147 .vertexAttributeDescriptionCount = 3,
1148 .pVertexAttributeDescriptions = (VkVertexInputAttributeDescription[]) {
1149 {
1150 /* VUE Header */
1151 .location = 0,
1152 .binding = 0,
1153 .format = VK_FORMAT_R32G32B32A32_UINT,
1154 .offset = 0
1155 },
1156 {
1157 /* Position */
1158 .location = 1,
1159 .binding = 1,
1160 .format = VK_FORMAT_R32G32_SFLOAT,
1161 .offset = 0
1162 },
1163 {
1164 /* Texture Coordinate */
1165 .location = 2,
1166 .binding = 1,
1167 .format = VK_FORMAT_R32G32B32_SFLOAT,
1168 .offset = 8
1169 }
1170 }
1171 };
1172
1173 VkDescriptorSetLayoutCreateInfo ds_layout_info = {
1174 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
1175 .bindingCount = 1,
1176 .pBindings = (VkDescriptorSetLayoutBinding[]) {
1177 {
1178 .binding = 0,
1179 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1180 .descriptorCount = 1,
1181 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1182 .pImmutableSamplers = NULL
1183 },
1184 }
1185 };
1186 result = anv_CreateDescriptorSetLayout(anv_device_to_handle(device),
1187 &ds_layout_info,
1188 &device->meta_state.alloc,
1189 &device->meta_state.blit.ds_layout);
1190 if (result != VK_SUCCESS)
1191 goto fail_render_pass;
1192
1193 result = anv_CreatePipelineLayout(anv_device_to_handle(device),
1194 &(VkPipelineLayoutCreateInfo) {
1195 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1196 .setLayoutCount = 1,
1197 .pSetLayouts = &device->meta_state.blit.ds_layout,
1198 },
1199 &device->meta_state.alloc, &device->meta_state.blit.pipeline_layout);
1200 if (result != VK_SUCCESS)
1201 goto fail_descriptor_set_layout;
1202
1203 VkPipelineShaderStageCreateInfo pipeline_shader_stages[] = {
1204 {
1205 .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
1206 .stage = VK_SHADER_STAGE_VERTEX_BIT,
1207 .module = anv_shader_module_to_handle(&vs),
1208 .pName = "main",
1209 .pSpecializationInfo = NULL
1210 }, {
1211 .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
1212 .stage = VK_SHADER_STAGE_FRAGMENT_BIT,
1213 .module = VK_NULL_HANDLE, /* TEMPLATE VALUE! FILL ME IN! */
1214 .pName = "main",
1215 .pSpecializationInfo = NULL
1216 },
1217 };
1218
1219 const VkGraphicsPipelineCreateInfo vk_pipeline_info = {
1220 .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
1221 .stageCount = ARRAY_SIZE(pipeline_shader_stages),
1222 .pStages = pipeline_shader_stages,
1223 .pVertexInputState = &vi_create_info,
1224 .pInputAssemblyState = &(VkPipelineInputAssemblyStateCreateInfo) {
1225 .sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
1226 .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
1227 .primitiveRestartEnable = false,
1228 },
1229 .pViewportState = &(VkPipelineViewportStateCreateInfo) {
1230 .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
1231 .viewportCount = 1,
1232 .scissorCount = 1,
1233 },
1234 .pRasterizationState = &(VkPipelineRasterizationStateCreateInfo) {
1235 .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
1236 .rasterizerDiscardEnable = false,
1237 .polygonMode = VK_POLYGON_MODE_FILL,
1238 .cullMode = VK_CULL_MODE_NONE,
1239 .frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE
1240 },
1241 .pMultisampleState = &(VkPipelineMultisampleStateCreateInfo) {
1242 .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
1243 .rasterizationSamples = 1,
1244 .sampleShadingEnable = false,
1245 .pSampleMask = (VkSampleMask[]) { UINT32_MAX },
1246 },
1247 .pColorBlendState = &(VkPipelineColorBlendStateCreateInfo) {
1248 .sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
1249 .attachmentCount = 1,
1250 .pAttachments = (VkPipelineColorBlendAttachmentState []) {
1251 { .colorWriteMask =
1252 VK_COLOR_COMPONENT_A_BIT |
1253 VK_COLOR_COMPONENT_R_BIT |
1254 VK_COLOR_COMPONENT_G_BIT |
1255 VK_COLOR_COMPONENT_B_BIT },
1256 }
1257 },
1258 .pDynamicState = &(VkPipelineDynamicStateCreateInfo) {
1259 .sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
1260 .dynamicStateCount = 9,
1261 .pDynamicStates = (VkDynamicState[]) {
1262 VK_DYNAMIC_STATE_VIEWPORT,
1263 VK_DYNAMIC_STATE_SCISSOR,
1264 VK_DYNAMIC_STATE_LINE_WIDTH,
1265 VK_DYNAMIC_STATE_DEPTH_BIAS,
1266 VK_DYNAMIC_STATE_BLEND_CONSTANTS,
1267 VK_DYNAMIC_STATE_DEPTH_BOUNDS,
1268 VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK,
1269 VK_DYNAMIC_STATE_STENCIL_WRITE_MASK,
1270 VK_DYNAMIC_STATE_STENCIL_REFERENCE,
1271 },
1272 },
1273 .flags = 0,
1274 .layout = device->meta_state.blit.pipeline_layout,
1275 .renderPass = device->meta_state.blit.render_pass,
1276 .subpass = 0,
1277 };
1278
1279 const struct anv_graphics_pipeline_create_info anv_pipeline_info = {
1280 .color_attachment_count = -1,
1281 .use_repclear = false,
1282 .disable_viewport = true,
1283 .disable_scissor = true,
1284 .disable_vs = true,
1285 .use_rectlist = true
1286 };
1287
1288 pipeline_shader_stages[1].module = anv_shader_module_to_handle(&fs_1d);
1289 result = anv_graphics_pipeline_create(anv_device_to_handle(device),
1290 VK_NULL_HANDLE,
1291 &vk_pipeline_info, &anv_pipeline_info,
1292 &device->meta_state.alloc, &device->meta_state.blit.pipeline_1d_src);
1293 if (result != VK_SUCCESS)
1294 goto fail_pipeline_layout;
1295
1296 pipeline_shader_stages[1].module = anv_shader_module_to_handle(&fs_2d);
1297 result = anv_graphics_pipeline_create(anv_device_to_handle(device),
1298 VK_NULL_HANDLE,
1299 &vk_pipeline_info, &anv_pipeline_info,
1300 &device->meta_state.alloc, &device->meta_state.blit.pipeline_2d_src);
1301 if (result != VK_SUCCESS)
1302 goto fail_pipeline_1d;
1303
1304 pipeline_shader_stages[1].module = anv_shader_module_to_handle(&fs_3d);
1305 result = anv_graphics_pipeline_create(anv_device_to_handle(device),
1306 VK_NULL_HANDLE,
1307 &vk_pipeline_info, &anv_pipeline_info,
1308 &device->meta_state.alloc, &device->meta_state.blit.pipeline_3d_src);
1309 if (result != VK_SUCCESS)
1310 goto fail_pipeline_2d;
1311
1312 ralloc_free(vs.nir);
1313 ralloc_free(fs_1d.nir);
1314 ralloc_free(fs_2d.nir);
1315 ralloc_free(fs_3d.nir);
1316
1317 return VK_SUCCESS;
1318
1319 fail_pipeline_2d:
1320 anv_DestroyPipeline(anv_device_to_handle(device),
1321 device->meta_state.blit.pipeline_2d_src,
1322 &device->meta_state.alloc);
1323
1324 fail_pipeline_1d:
1325 anv_DestroyPipeline(anv_device_to_handle(device),
1326 device->meta_state.blit.pipeline_1d_src,
1327 &device->meta_state.alloc);
1328
1329 fail_pipeline_layout:
1330 anv_DestroyPipelineLayout(anv_device_to_handle(device),
1331 device->meta_state.blit.pipeline_layout,
1332 &device->meta_state.alloc);
1333 fail_descriptor_set_layout:
1334 anv_DestroyDescriptorSetLayout(anv_device_to_handle(device),
1335 device->meta_state.blit.ds_layout,
1336 &device->meta_state.alloc);
1337 fail_render_pass:
1338 anv_DestroyRenderPass(anv_device_to_handle(device),
1339 device->meta_state.blit.render_pass,
1340 &device->meta_state.alloc);
1341
1342 ralloc_free(vs.nir);
1343 ralloc_free(fs_1d.nir);
1344 ralloc_free(fs_2d.nir);
1345 ralloc_free(fs_3d.nir);
1346 fail:
1347 return result;
1348 }