anv/meta: Move blit code to anv_meta_blit.c
[mesa.git] / src / 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 nir_builder b;
38
39 const struct glsl_type *vertex_type = glsl_vec4_type();
40
41 nir_builder_init_simple_shader(&b, NULL, MESA_SHADER_VERTEX, NULL);
42 b.shader->info.name = ralloc_strdup(b.shader, "meta_blit_vs");
43
44 nir_variable *pos_in = nir_variable_create(b.shader, nir_var_shader_in,
45 vertex_type, "a_pos");
46 pos_in->data.location = VERT_ATTRIB_GENERIC0;
47 nir_variable *pos_out = nir_variable_create(b.shader, nir_var_shader_out,
48 vertex_type, "gl_Position");
49 pos_out->data.location = VARYING_SLOT_POS;
50 nir_copy_var(&b, pos_out, pos_in);
51
52 /* Add one more pass-through attribute. For clear shaders, this is used
53 * to store the color and for blit shaders it's the texture coordinate.
54 */
55 const struct glsl_type *attr_type = glsl_vec4_type();
56 nir_variable *attr_in = nir_variable_create(b.shader, nir_var_shader_in,
57 attr_type, "a_attr");
58 attr_in->data.location = VERT_ATTRIB_GENERIC1;
59 nir_variable *attr_out = nir_variable_create(b.shader, nir_var_shader_out,
60 attr_type, "v_attr");
61 attr_out->data.location = VARYING_SLOT_VAR0;
62 attr_out->data.interpolation = INTERP_QUALIFIER_SMOOTH;
63 nir_copy_var(&b, attr_out, attr_in);
64
65 return b.shader;
66 }
67
68 static nir_shader *
69 build_nir_copy_fragment_shader(enum glsl_sampler_dim tex_dim)
70 {
71 nir_builder b;
72
73 nir_builder_init_simple_shader(&b, NULL, MESA_SHADER_FRAGMENT, NULL);
74 b.shader->info.name = ralloc_strdup(b.shader, "meta_blit_fs");
75
76 const struct glsl_type *color_type = glsl_vec4_type();
77
78 nir_variable *tex_pos_in = nir_variable_create(b.shader, nir_var_shader_in,
79 glsl_vec4_type(), "v_attr");
80 tex_pos_in->data.location = VARYING_SLOT_VAR0;
81
82 /* Swizzle the array index which comes in as Z coordinate into the right
83 * position.
84 */
85 unsigned swz[] = { 0, (tex_dim == GLSL_SAMPLER_DIM_1D ? 2 : 1), 2 };
86 nir_ssa_def *const tex_pos =
87 nir_swizzle(&b, nir_load_var(&b, tex_pos_in), swz,
88 (tex_dim == GLSL_SAMPLER_DIM_1D ? 2 : 3), false);
89
90 const struct glsl_type *sampler_type =
91 glsl_sampler_type(tex_dim, false, tex_dim != GLSL_SAMPLER_DIM_3D,
92 glsl_get_base_type(color_type));
93 nir_variable *sampler = nir_variable_create(b.shader, nir_var_uniform,
94 sampler_type, "s_tex");
95 sampler->data.descriptor_set = 0;
96 sampler->data.binding = 0;
97
98 nir_tex_instr *tex = nir_tex_instr_create(b.shader, 1);
99 tex->sampler_dim = tex_dim;
100 tex->op = nir_texop_tex;
101 tex->src[0].src_type = nir_tex_src_coord;
102 tex->src[0].src = nir_src_for_ssa(tex_pos);
103 tex->dest_type = nir_type_float; /* TODO */
104 tex->is_array = glsl_sampler_type_is_array(sampler_type);
105 tex->coord_components = tex_pos->num_components;
106 tex->texture = nir_deref_var_create(tex, sampler);
107 tex->sampler = nir_deref_var_create(tex, sampler);
108
109 nir_ssa_dest_init(&tex->instr, &tex->dest, 4, "tex");
110 nir_builder_instr_insert(&b, &tex->instr);
111
112 nir_variable *color_out = nir_variable_create(b.shader, nir_var_shader_out,
113 color_type, "f_color");
114 color_out->data.location = FRAG_RESULT_DATA0;
115 nir_store_var(&b, color_out, &tex->dest.ssa, 4);
116
117 return b.shader;
118 }
119
120 static void
121 meta_prepare_blit(struct anv_cmd_buffer *cmd_buffer,
122 struct anv_meta_saved_state *saved_state)
123 {
124 anv_meta_save(saved_state, cmd_buffer,
125 (1 << VK_DYNAMIC_STATE_VIEWPORT));
126 }
127
128 /* Returns the user-provided VkBufferImageCopy::imageOffset in units of
129 * elements rather than texels. One element equals one texel or one block
130 * if Image is uncompressed or compressed, respectively.
131 */
132 static struct VkOffset3D
133 meta_region_offset_el(const struct anv_image * image,
134 const struct VkOffset3D * offset)
135 {
136 const struct isl_format_layout * isl_layout = image->format->isl_layout;
137 return (VkOffset3D) {
138 .x = offset->x / isl_layout->bw,
139 .y = offset->y / isl_layout->bh,
140 .z = offset->z / isl_layout->bd,
141 };
142 }
143
144 /* Returns the user-provided VkBufferImageCopy::imageExtent in units of
145 * elements rather than texels. One element equals one texel or one block
146 * if Image is uncompressed or compressed, respectively.
147 */
148 static struct VkExtent3D
149 meta_region_extent_el(const VkFormat format,
150 const struct VkExtent3D * extent)
151 {
152 const struct isl_format_layout * isl_layout =
153 anv_format_for_vk_format(format)->isl_layout;
154 return (VkExtent3D) {
155 .width = DIV_ROUND_UP(extent->width , isl_layout->bw),
156 .height = DIV_ROUND_UP(extent->height, isl_layout->bh),
157 .depth = DIV_ROUND_UP(extent->depth , isl_layout->bd),
158 };
159 }
160
161 static void
162 meta_emit_blit(struct anv_cmd_buffer *cmd_buffer,
163 struct anv_image *src_image,
164 struct anv_image_view *src_iview,
165 VkOffset3D src_offset,
166 VkExtent3D src_extent,
167 struct anv_image *dest_image,
168 struct anv_image_view *dest_iview,
169 VkOffset3D dest_offset,
170 VkExtent3D dest_extent,
171 VkFilter blit_filter)
172 {
173 struct anv_device *device = cmd_buffer->device;
174 VkDescriptorPool dummy_desc_pool = (VkDescriptorPool)1;
175
176 struct blit_vb_data {
177 float pos[2];
178 float tex_coord[3];
179 } *vb_data;
180
181 assert(src_image->samples == dest_image->samples);
182
183 unsigned vb_size = sizeof(struct anv_vue_header) + 3 * sizeof(*vb_data);
184
185 struct anv_state vb_state =
186 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, vb_size, 16);
187 memset(vb_state.map, 0, sizeof(struct anv_vue_header));
188 vb_data = vb_state.map + sizeof(struct anv_vue_header);
189
190 vb_data[0] = (struct blit_vb_data) {
191 .pos = {
192 dest_offset.x + dest_extent.width,
193 dest_offset.y + dest_extent.height,
194 },
195 .tex_coord = {
196 (float)(src_offset.x + src_extent.width) / (float)src_iview->extent.width,
197 (float)(src_offset.y + src_extent.height) / (float)src_iview->extent.height,
198 (float)src_offset.z / (float)src_iview->extent.depth,
199 },
200 };
201
202 vb_data[1] = (struct blit_vb_data) {
203 .pos = {
204 dest_offset.x,
205 dest_offset.y + dest_extent.height,
206 },
207 .tex_coord = {
208 (float)src_offset.x / (float)src_iview->extent.width,
209 (float)(src_offset.y + src_extent.height) / (float)src_iview->extent.height,
210 (float)src_offset.z / (float)src_iview->extent.depth,
211 },
212 };
213
214 vb_data[2] = (struct blit_vb_data) {
215 .pos = {
216 dest_offset.x,
217 dest_offset.y,
218 },
219 .tex_coord = {
220 (float)src_offset.x / (float)src_iview->extent.width,
221 (float)src_offset.y / (float)src_iview->extent.height,
222 (float)src_offset.z / (float)src_iview->extent.depth,
223 },
224 };
225
226 anv_state_clflush(vb_state);
227
228 struct anv_buffer vertex_buffer = {
229 .device = device,
230 .size = vb_size,
231 .bo = &device->dynamic_state_block_pool.bo,
232 .offset = vb_state.offset,
233 };
234
235 anv_CmdBindVertexBuffers(anv_cmd_buffer_to_handle(cmd_buffer), 0, 2,
236 (VkBuffer[]) {
237 anv_buffer_to_handle(&vertex_buffer),
238 anv_buffer_to_handle(&vertex_buffer)
239 },
240 (VkDeviceSize[]) {
241 0,
242 sizeof(struct anv_vue_header),
243 });
244
245 VkSampler sampler;
246 ANV_CALL(CreateSampler)(anv_device_to_handle(device),
247 &(VkSamplerCreateInfo) {
248 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
249 .magFilter = blit_filter,
250 .minFilter = blit_filter,
251 }, &cmd_buffer->pool->alloc, &sampler);
252
253 VkDescriptorSet set;
254 anv_AllocateDescriptorSets(anv_device_to_handle(device),
255 &(VkDescriptorSetAllocateInfo) {
256 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
257 .descriptorPool = dummy_desc_pool,
258 .descriptorSetCount = 1,
259 .pSetLayouts = &device->meta_state.blit.ds_layout
260 }, &set);
261 anv_UpdateDescriptorSets(anv_device_to_handle(device),
262 1, /* writeCount */
263 (VkWriteDescriptorSet[]) {
264 {
265 .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
266 .dstSet = set,
267 .dstBinding = 0,
268 .dstArrayElement = 0,
269 .descriptorCount = 1,
270 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
271 .pImageInfo = (VkDescriptorImageInfo[]) {
272 {
273 .sampler = sampler,
274 .imageView = anv_image_view_to_handle(src_iview),
275 .imageLayout = VK_IMAGE_LAYOUT_GENERAL,
276 },
277 }
278 }
279 }, 0, NULL);
280
281 VkFramebuffer fb;
282 anv_CreateFramebuffer(anv_device_to_handle(device),
283 &(VkFramebufferCreateInfo) {
284 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
285 .attachmentCount = 1,
286 .pAttachments = (VkImageView[]) {
287 anv_image_view_to_handle(dest_iview),
288 },
289 .width = dest_iview->extent.width,
290 .height = dest_iview->extent.height,
291 .layers = 1
292 }, &cmd_buffer->pool->alloc, &fb);
293
294 ANV_CALL(CmdBeginRenderPass)(anv_cmd_buffer_to_handle(cmd_buffer),
295 &(VkRenderPassBeginInfo) {
296 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
297 .renderPass = device->meta_state.blit.render_pass,
298 .framebuffer = fb,
299 .renderArea = {
300 .offset = { dest_offset.x, dest_offset.y },
301 .extent = { dest_extent.width, dest_extent.height },
302 },
303 .clearValueCount = 0,
304 .pClearValues = NULL,
305 }, VK_SUBPASS_CONTENTS_INLINE);
306
307 VkPipeline pipeline;
308
309 switch (src_image->type) {
310 case VK_IMAGE_TYPE_1D:
311 pipeline = device->meta_state.blit.pipeline_1d_src;
312 break;
313 case VK_IMAGE_TYPE_2D:
314 pipeline = device->meta_state.blit.pipeline_2d_src;
315 break;
316 case VK_IMAGE_TYPE_3D:
317 pipeline = device->meta_state.blit.pipeline_3d_src;
318 break;
319 default:
320 unreachable(!"bad VkImageType");
321 }
322
323 if (cmd_buffer->state.pipeline != anv_pipeline_from_handle(pipeline)) {
324 anv_CmdBindPipeline(anv_cmd_buffer_to_handle(cmd_buffer),
325 VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
326 }
327
328 anv_CmdSetViewport(anv_cmd_buffer_to_handle(cmd_buffer), 0, 1,
329 &(VkViewport) {
330 .x = 0.0f,
331 .y = 0.0f,
332 .width = dest_iview->extent.width,
333 .height = dest_iview->extent.height,
334 .minDepth = 0.0f,
335 .maxDepth = 1.0f,
336 });
337
338 anv_CmdBindDescriptorSets(anv_cmd_buffer_to_handle(cmd_buffer),
339 VK_PIPELINE_BIND_POINT_GRAPHICS,
340 device->meta_state.blit.pipeline_layout, 0, 1,
341 &set, 0, NULL);
342
343 ANV_CALL(CmdDraw)(anv_cmd_buffer_to_handle(cmd_buffer), 3, 1, 0, 0);
344
345 ANV_CALL(CmdEndRenderPass)(anv_cmd_buffer_to_handle(cmd_buffer));
346
347 /* At the point where we emit the draw call, all data from the
348 * descriptor sets, etc. has been used. We are free to delete it.
349 */
350 anv_descriptor_set_destroy(device, anv_descriptor_set_from_handle(set));
351 anv_DestroySampler(anv_device_to_handle(device), sampler,
352 &cmd_buffer->pool->alloc);
353 anv_DestroyFramebuffer(anv_device_to_handle(device), fb,
354 &cmd_buffer->pool->alloc);
355 }
356
357 static void
358 meta_finish_blit(struct anv_cmd_buffer *cmd_buffer,
359 const struct anv_meta_saved_state *saved_state)
360 {
361 anv_meta_restore(saved_state, cmd_buffer);
362 }
363
364 static VkFormat
365 vk_format_for_size(int bs)
366 {
367 /* Note: We intentionally use the 4-channel formats whenever we can.
368 * This is so that, when we do a RGB <-> RGBX copy, the two formats will
369 * line up even though one of them is 3/4 the size of the other.
370 */
371 switch (bs) {
372 case 1: return VK_FORMAT_R8_UINT;
373 case 2: return VK_FORMAT_R8G8_UINT;
374 case 3: return VK_FORMAT_R8G8B8_UINT;
375 case 4: return VK_FORMAT_R8G8B8A8_UINT;
376 case 6: return VK_FORMAT_R16G16B16_UINT;
377 case 8: return VK_FORMAT_R16G16B16A16_UINT;
378 case 12: return VK_FORMAT_R32G32B32_UINT;
379 case 16: return VK_FORMAT_R32G32B32A32_UINT;
380 default:
381 unreachable("Invalid format block size");
382 }
383 }
384
385 static void
386 do_buffer_copy(struct anv_cmd_buffer *cmd_buffer,
387 struct anv_bo *src, uint64_t src_offset,
388 struct anv_bo *dest, uint64_t dest_offset,
389 int width, int height, VkFormat copy_format)
390 {
391 VkDevice vk_device = anv_device_to_handle(cmd_buffer->device);
392
393 VkImageCreateInfo image_info = {
394 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
395 .imageType = VK_IMAGE_TYPE_2D,
396 .format = copy_format,
397 .extent = {
398 .width = width,
399 .height = height,
400 .depth = 1,
401 },
402 .mipLevels = 1,
403 .arrayLayers = 1,
404 .samples = 1,
405 .tiling = VK_IMAGE_TILING_LINEAR,
406 .usage = 0,
407 .flags = 0,
408 };
409
410 VkImage src_image;
411 image_info.usage = VK_IMAGE_USAGE_SAMPLED_BIT;
412 anv_CreateImage(vk_device, &image_info,
413 &cmd_buffer->pool->alloc, &src_image);
414
415 VkImage dest_image;
416 image_info.usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
417 anv_CreateImage(vk_device, &image_info,
418 &cmd_buffer->pool->alloc, &dest_image);
419
420 /* We could use a vk call to bind memory, but that would require
421 * creating a dummy memory object etc. so there's really no point.
422 */
423 anv_image_from_handle(src_image)->bo = src;
424 anv_image_from_handle(src_image)->offset = src_offset;
425 anv_image_from_handle(dest_image)->bo = dest;
426 anv_image_from_handle(dest_image)->offset = dest_offset;
427
428 struct anv_image_view src_iview;
429 anv_image_view_init(&src_iview, cmd_buffer->device,
430 &(VkImageViewCreateInfo) {
431 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
432 .image = src_image,
433 .viewType = VK_IMAGE_VIEW_TYPE_2D,
434 .format = copy_format,
435 .subresourceRange = {
436 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
437 .baseMipLevel = 0,
438 .levelCount = 1,
439 .baseArrayLayer = 0,
440 .layerCount = 1
441 },
442 },
443 cmd_buffer, 0);
444
445 struct anv_image_view dest_iview;
446 anv_image_view_init(&dest_iview, cmd_buffer->device,
447 &(VkImageViewCreateInfo) {
448 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
449 .image = dest_image,
450 .viewType = VK_IMAGE_VIEW_TYPE_2D,
451 .format = copy_format,
452 .subresourceRange = {
453 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
454 .baseMipLevel = 0,
455 .levelCount = 1,
456 .baseArrayLayer = 0,
457 .layerCount = 1,
458 },
459 },
460 cmd_buffer, 0);
461
462 meta_emit_blit(cmd_buffer,
463 anv_image_from_handle(src_image),
464 &src_iview,
465 (VkOffset3D) { 0, 0, 0 },
466 (VkExtent3D) { width, height, 1 },
467 anv_image_from_handle(dest_image),
468 &dest_iview,
469 (VkOffset3D) { 0, 0, 0 },
470 (VkExtent3D) { width, height, 1 },
471 VK_FILTER_NEAREST);
472
473 anv_DestroyImage(vk_device, src_image, &cmd_buffer->pool->alloc);
474 anv_DestroyImage(vk_device, dest_image, &cmd_buffer->pool->alloc);
475 }
476
477 void anv_CmdCopyBuffer(
478 VkCommandBuffer commandBuffer,
479 VkBuffer srcBuffer,
480 VkBuffer destBuffer,
481 uint32_t regionCount,
482 const VkBufferCopy* pRegions)
483 {
484 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
485 ANV_FROM_HANDLE(anv_buffer, src_buffer, srcBuffer);
486 ANV_FROM_HANDLE(anv_buffer, dest_buffer, destBuffer);
487
488 struct anv_meta_saved_state saved_state;
489
490 meta_prepare_blit(cmd_buffer, &saved_state);
491
492 for (unsigned r = 0; r < regionCount; r++) {
493 uint64_t src_offset = src_buffer->offset + pRegions[r].srcOffset;
494 uint64_t dest_offset = dest_buffer->offset + pRegions[r].dstOffset;
495 uint64_t copy_size = pRegions[r].size;
496
497 /* First, we compute the biggest format that can be used with the
498 * given offsets and size.
499 */
500 int bs = 16;
501
502 int fs = ffs(src_offset) - 1;
503 if (fs != -1)
504 bs = MIN2(bs, 1 << fs);
505 assert(src_offset % bs == 0);
506
507 fs = ffs(dest_offset) - 1;
508 if (fs != -1)
509 bs = MIN2(bs, 1 << fs);
510 assert(dest_offset % bs == 0);
511
512 fs = ffs(pRegions[r].size) - 1;
513 if (fs != -1)
514 bs = MIN2(bs, 1 << fs);
515 assert(pRegions[r].size % bs == 0);
516
517 VkFormat copy_format = vk_format_for_size(bs);
518
519 /* This is maximum possible width/height our HW can handle */
520 uint64_t max_surface_dim = 1 << 14;
521
522 /* First, we make a bunch of max-sized copies */
523 uint64_t max_copy_size = max_surface_dim * max_surface_dim * bs;
524 while (copy_size >= max_copy_size) {
525 do_buffer_copy(cmd_buffer, src_buffer->bo, src_offset,
526 dest_buffer->bo, dest_offset,
527 max_surface_dim, max_surface_dim, copy_format);
528 copy_size -= max_copy_size;
529 src_offset += max_copy_size;
530 dest_offset += max_copy_size;
531 }
532
533 uint64_t height = copy_size / (max_surface_dim * bs);
534 assert(height < max_surface_dim);
535 if (height != 0) {
536 uint64_t rect_copy_size = height * max_surface_dim * bs;
537 do_buffer_copy(cmd_buffer, src_buffer->bo, src_offset,
538 dest_buffer->bo, dest_offset,
539 max_surface_dim, height, copy_format);
540 copy_size -= rect_copy_size;
541 src_offset += rect_copy_size;
542 dest_offset += rect_copy_size;
543 }
544
545 if (copy_size != 0) {
546 do_buffer_copy(cmd_buffer, src_buffer->bo, src_offset,
547 dest_buffer->bo, dest_offset,
548 copy_size / bs, 1, copy_format);
549 }
550 }
551
552 meta_finish_blit(cmd_buffer, &saved_state);
553 }
554
555 void anv_CmdUpdateBuffer(
556 VkCommandBuffer commandBuffer,
557 VkBuffer dstBuffer,
558 VkDeviceSize dstOffset,
559 VkDeviceSize dataSize,
560 const uint32_t* pData)
561 {
562 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
563 ANV_FROM_HANDLE(anv_buffer, dst_buffer, dstBuffer);
564 struct anv_meta_saved_state saved_state;
565
566 meta_prepare_blit(cmd_buffer, &saved_state);
567
568 /* We can't quite grab a full block because the state stream needs a
569 * little data at the top to build its linked list.
570 */
571 const uint32_t max_update_size =
572 cmd_buffer->device->dynamic_state_block_pool.block_size - 64;
573
574 assert(max_update_size < (1 << 14) * 4);
575
576 while (dataSize) {
577 const uint32_t copy_size = MIN2(dataSize, max_update_size);
578
579 struct anv_state tmp_data =
580 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, copy_size, 64);
581
582 memcpy(tmp_data.map, pData, copy_size);
583
584 VkFormat format;
585 int bs;
586 if ((copy_size & 15) == 0 && (dstOffset & 15) == 0) {
587 format = VK_FORMAT_R32G32B32A32_UINT;
588 bs = 16;
589 } else if ((copy_size & 7) == 0 && (dstOffset & 7) == 0) {
590 format = VK_FORMAT_R32G32_UINT;
591 bs = 8;
592 } else {
593 assert((copy_size & 3) == 0 && (dstOffset & 3) == 0);
594 format = VK_FORMAT_R32_UINT;
595 bs = 4;
596 }
597
598 do_buffer_copy(cmd_buffer,
599 &cmd_buffer->device->dynamic_state_block_pool.bo,
600 tmp_data.offset,
601 dst_buffer->bo, dst_buffer->offset + dstOffset,
602 copy_size / bs, 1, format);
603
604 dataSize -= copy_size;
605 dstOffset += copy_size;
606 pData = (void *)pData + copy_size;
607 }
608 }
609
610 static VkFormat
611 choose_iview_format(struct anv_image *image, VkImageAspectFlagBits aspect)
612 {
613 assert(__builtin_popcount(aspect) == 1);
614
615 struct isl_surf *surf =
616 &anv_image_get_surface_for_aspect_mask(image, aspect)->isl;
617
618 /* vkCmdCopyImage behaves like memcpy. Therefore we choose identical UINT
619 * formats for the source and destination image views.
620 *
621 * From the Vulkan spec (2015-12-30):
622 *
623 * vkCmdCopyImage performs image copies in a similar manner to a host
624 * memcpy. It does not perform general-purpose conversions such as
625 * scaling, resizing, blending, color-space conversion, or format
626 * conversions. Rather, it simply copies raw image data. vkCmdCopyImage
627 * can copy between images with different formats, provided the formats
628 * are compatible as defined below.
629 *
630 * [The spec later defines compatibility as having the same number of
631 * bytes per block].
632 */
633 return vk_format_for_size(isl_format_layouts[surf->format].bs);
634 }
635
636 static VkFormat
637 choose_buffer_format(VkFormat format, VkImageAspectFlagBits aspect)
638 {
639 assert(__builtin_popcount(aspect) == 1);
640
641 /* vkCmdCopy* commands behave like memcpy. Therefore we choose
642 * compatable UINT formats for the source and destination image views.
643 *
644 * For the buffer, we go back to the original image format and get a
645 * the format as if it were linear. This way, for RGB formats, we get
646 * an RGB format here even if the tiled image is RGBA. XXX: This doesn't
647 * work if the buffer is the destination.
648 */
649 enum isl_format linear_format = anv_get_isl_format(format, aspect,
650 VK_IMAGE_TILING_LINEAR,
651 NULL);
652
653 return vk_format_for_size(isl_format_layouts[linear_format].bs);
654 }
655
656 void anv_CmdCopyImage(
657 VkCommandBuffer commandBuffer,
658 VkImage srcImage,
659 VkImageLayout srcImageLayout,
660 VkImage destImage,
661 VkImageLayout destImageLayout,
662 uint32_t regionCount,
663 const VkImageCopy* pRegions)
664 {
665 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
666 ANV_FROM_HANDLE(anv_image, src_image, srcImage);
667 ANV_FROM_HANDLE(anv_image, dest_image, destImage);
668 struct anv_meta_saved_state saved_state;
669
670 /* From the Vulkan 1.0 spec:
671 *
672 * vkCmdCopyImage can be used to copy image data between multisample
673 * images, but both images must have the same number of samples.
674 */
675 assert(src_image->samples == dest_image->samples);
676
677 meta_prepare_blit(cmd_buffer, &saved_state);
678
679 for (unsigned r = 0; r < regionCount; r++) {
680 assert(pRegions[r].srcSubresource.aspectMask ==
681 pRegions[r].dstSubresource.aspectMask);
682
683 VkImageAspectFlags aspect = pRegions[r].srcSubresource.aspectMask;
684
685 VkFormat src_format = choose_iview_format(src_image, aspect);
686 VkFormat dst_format = choose_iview_format(dest_image, aspect);
687
688 struct anv_image_view src_iview;
689 anv_image_view_init(&src_iview, cmd_buffer->device,
690 &(VkImageViewCreateInfo) {
691 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
692 .image = srcImage,
693 .viewType = anv_meta_get_view_type(src_image),
694 .format = src_format,
695 .subresourceRange = {
696 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
697 .baseMipLevel = pRegions[r].srcSubresource.mipLevel,
698 .levelCount = 1,
699 .baseArrayLayer = pRegions[r].srcSubresource.baseArrayLayer,
700 .layerCount = pRegions[r].dstSubresource.layerCount,
701 },
702 },
703 cmd_buffer, 0);
704
705 const VkOffset3D dest_offset = {
706 .x = pRegions[r].dstOffset.x,
707 .y = pRegions[r].dstOffset.y,
708 .z = 0,
709 };
710
711 unsigned num_slices;
712 if (src_image->type == VK_IMAGE_TYPE_3D) {
713 assert(pRegions[r].srcSubresource.layerCount == 1 &&
714 pRegions[r].dstSubresource.layerCount == 1);
715 num_slices = pRegions[r].extent.depth;
716 } else {
717 assert(pRegions[r].srcSubresource.layerCount ==
718 pRegions[r].dstSubresource.layerCount);
719 assert(pRegions[r].extent.depth == 1);
720 num_slices = pRegions[r].dstSubresource.layerCount;
721 }
722
723 const uint32_t dest_base_array_slice =
724 anv_meta_get_iview_layer(dest_image, &pRegions[r].dstSubresource,
725 &pRegions[r].dstOffset);
726
727 for (unsigned slice = 0; slice < num_slices; slice++) {
728 VkOffset3D src_offset = pRegions[r].srcOffset;
729 src_offset.z += slice;
730
731 struct anv_image_view dest_iview;
732 anv_image_view_init(&dest_iview, cmd_buffer->device,
733 &(VkImageViewCreateInfo) {
734 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
735 .image = destImage,
736 .viewType = anv_meta_get_view_type(dest_image),
737 .format = dst_format,
738 .subresourceRange = {
739 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
740 .baseMipLevel = pRegions[r].dstSubresource.mipLevel,
741 .levelCount = 1,
742 .baseArrayLayer = dest_base_array_slice + slice,
743 .layerCount = 1
744 },
745 },
746 cmd_buffer, 0);
747
748 meta_emit_blit(cmd_buffer,
749 src_image, &src_iview,
750 src_offset,
751 pRegions[r].extent,
752 dest_image, &dest_iview,
753 dest_offset,
754 pRegions[r].extent,
755 VK_FILTER_NEAREST);
756 }
757 }
758
759 meta_finish_blit(cmd_buffer, &saved_state);
760 }
761
762 void anv_CmdBlitImage(
763 VkCommandBuffer commandBuffer,
764 VkImage srcImage,
765 VkImageLayout srcImageLayout,
766 VkImage destImage,
767 VkImageLayout destImageLayout,
768 uint32_t regionCount,
769 const VkImageBlit* pRegions,
770 VkFilter filter)
771
772 {
773 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
774 ANV_FROM_HANDLE(anv_image, src_image, srcImage);
775 ANV_FROM_HANDLE(anv_image, dest_image, destImage);
776 struct anv_meta_saved_state saved_state;
777
778 /* From the Vulkan 1.0 spec:
779 *
780 * vkCmdBlitImage must not be used for multisampled source or
781 * destination images. Use vkCmdResolveImage for this purpose.
782 */
783 assert(src_image->samples == 1);
784 assert(dest_image->samples == 1);
785
786 anv_finishme("respect VkFilter");
787
788 meta_prepare_blit(cmd_buffer, &saved_state);
789
790 for (unsigned r = 0; r < regionCount; r++) {
791 struct anv_image_view src_iview;
792 anv_image_view_init(&src_iview, cmd_buffer->device,
793 &(VkImageViewCreateInfo) {
794 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
795 .image = srcImage,
796 .viewType = anv_meta_get_view_type(src_image),
797 .format = src_image->vk_format,
798 .subresourceRange = {
799 .aspectMask = pRegions[r].srcSubresource.aspectMask,
800 .baseMipLevel = pRegions[r].srcSubresource.mipLevel,
801 .levelCount = 1,
802 .baseArrayLayer = pRegions[r].srcSubresource.baseArrayLayer,
803 .layerCount = 1
804 },
805 },
806 cmd_buffer, 0);
807
808 const VkOffset3D dest_offset = {
809 .x = pRegions[r].dstOffsets[0].x,
810 .y = pRegions[r].dstOffsets[0].y,
811 .z = 0,
812 };
813
814 if (pRegions[r].dstOffsets[1].x < pRegions[r].dstOffsets[0].x ||
815 pRegions[r].dstOffsets[1].y < pRegions[r].dstOffsets[0].y ||
816 pRegions[r].srcOffsets[1].x < pRegions[r].srcOffsets[0].x ||
817 pRegions[r].srcOffsets[1].y < pRegions[r].srcOffsets[0].y)
818 anv_finishme("FINISHME: Allow flipping in blits");
819
820 const VkExtent3D dest_extent = {
821 .width = pRegions[r].dstOffsets[1].x - pRegions[r].dstOffsets[0].x,
822 .height = pRegions[r].dstOffsets[1].y - pRegions[r].dstOffsets[0].y,
823 };
824
825 const VkExtent3D src_extent = {
826 .width = pRegions[r].srcOffsets[1].x - pRegions[r].srcOffsets[0].x,
827 .height = pRegions[r].srcOffsets[1].y - pRegions[r].srcOffsets[0].y,
828 };
829
830 const uint32_t dest_array_slice =
831 anv_meta_get_iview_layer(dest_image, &pRegions[r].dstSubresource,
832 &pRegions[r].dstOffsets[0]);
833
834 if (pRegions[r].srcSubresource.layerCount > 1)
835 anv_finishme("FINISHME: copy multiple array layers");
836
837 if (pRegions[r].srcOffsets[0].z + 1 != pRegions[r].srcOffsets[1].z ||
838 pRegions[r].dstOffsets[0].z + 1 != pRegions[r].dstOffsets[1].z)
839 anv_finishme("FINISHME: copy multiple depth layers");
840
841 struct anv_image_view dest_iview;
842 anv_image_view_init(&dest_iview, cmd_buffer->device,
843 &(VkImageViewCreateInfo) {
844 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
845 .image = destImage,
846 .viewType = anv_meta_get_view_type(dest_image),
847 .format = dest_image->vk_format,
848 .subresourceRange = {
849 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
850 .baseMipLevel = pRegions[r].dstSubresource.mipLevel,
851 .levelCount = 1,
852 .baseArrayLayer = dest_array_slice,
853 .layerCount = 1
854 },
855 },
856 cmd_buffer, 0);
857
858 meta_emit_blit(cmd_buffer,
859 src_image, &src_iview,
860 pRegions[r].srcOffsets[0], src_extent,
861 dest_image, &dest_iview,
862 dest_offset, dest_extent,
863 filter);
864 }
865
866 meta_finish_blit(cmd_buffer, &saved_state);
867 }
868
869 static struct anv_image *
870 make_image_for_buffer(VkDevice vk_device, VkBuffer vk_buffer, VkFormat format,
871 VkImageUsageFlags usage,
872 VkImageType image_type,
873 const VkAllocationCallbacks *alloc,
874 const VkBufferImageCopy *copy)
875 {
876 ANV_FROM_HANDLE(anv_buffer, buffer, vk_buffer);
877
878 VkExtent3D extent = copy->imageExtent;
879 if (copy->bufferRowLength)
880 extent.width = copy->bufferRowLength;
881 if (copy->bufferImageHeight)
882 extent.height = copy->bufferImageHeight;
883 extent.depth = 1;
884 extent = meta_region_extent_el(format, &extent);
885
886 VkImageAspectFlags aspect = copy->imageSubresource.aspectMask;
887 VkFormat buffer_format = choose_buffer_format(format, aspect);
888
889 VkImage vk_image;
890 VkResult result = anv_CreateImage(vk_device,
891 &(VkImageCreateInfo) {
892 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
893 .imageType = VK_IMAGE_TYPE_2D,
894 .format = buffer_format,
895 .extent = extent,
896 .mipLevels = 1,
897 .arrayLayers = 1,
898 .samples = 1,
899 .tiling = VK_IMAGE_TILING_LINEAR,
900 .usage = usage,
901 .flags = 0,
902 }, alloc, &vk_image);
903 assert(result == VK_SUCCESS);
904
905 ANV_FROM_HANDLE(anv_image, image, vk_image);
906
907 /* We could use a vk call to bind memory, but that would require
908 * creating a dummy memory object etc. so there's really no point.
909 */
910 image->bo = buffer->bo;
911 image->offset = buffer->offset + copy->bufferOffset;
912
913 return image;
914 }
915
916 void anv_CmdCopyBufferToImage(
917 VkCommandBuffer commandBuffer,
918 VkBuffer srcBuffer,
919 VkImage destImage,
920 VkImageLayout destImageLayout,
921 uint32_t regionCount,
922 const VkBufferImageCopy* pRegions)
923 {
924 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
925 ANV_FROM_HANDLE(anv_image, dest_image, destImage);
926 VkDevice vk_device = anv_device_to_handle(cmd_buffer->device);
927 struct anv_meta_saved_state saved_state;
928
929 /* The Vulkan 1.0 spec says "dstImage must have a sample count equal to
930 * VK_SAMPLE_COUNT_1_BIT."
931 */
932 assert(dest_image->samples == 1);
933
934 meta_prepare_blit(cmd_buffer, &saved_state);
935
936 for (unsigned r = 0; r < regionCount; r++) {
937 VkImageAspectFlags aspect = pRegions[r].imageSubresource.aspectMask;
938
939 VkFormat image_format = choose_iview_format(dest_image, aspect);
940
941 struct anv_image *src_image =
942 make_image_for_buffer(vk_device, srcBuffer, dest_image->vk_format,
943 VK_IMAGE_USAGE_SAMPLED_BIT,
944 dest_image->type, &cmd_buffer->pool->alloc,
945 &pRegions[r]);
946
947 const uint32_t dest_base_array_slice =
948 anv_meta_get_iview_layer(dest_image, &pRegions[r].imageSubresource,
949 &pRegions[r].imageOffset);
950
951 unsigned num_slices_3d = pRegions[r].imageExtent.depth;
952 unsigned num_slices_array = pRegions[r].imageSubresource.layerCount;
953 unsigned slice_3d = 0;
954 unsigned slice_array = 0;
955 while (slice_3d < num_slices_3d && slice_array < num_slices_array) {
956 struct anv_image_view src_iview;
957 anv_image_view_init(&src_iview, cmd_buffer->device,
958 &(VkImageViewCreateInfo) {
959 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
960 .image = anv_image_to_handle(src_image),
961 .viewType = VK_IMAGE_VIEW_TYPE_2D,
962 .format = src_image->vk_format,
963 .subresourceRange = {
964 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
965 .baseMipLevel = 0,
966 .levelCount = 1,
967 .baseArrayLayer = 0,
968 .layerCount = 1,
969 },
970 },
971 cmd_buffer, 0);
972
973 uint32_t img_x = 0;
974 uint32_t img_y = 0;
975 uint32_t img_o = 0;
976 if (isl_format_is_compressed(dest_image->format->isl_format))
977 isl_surf_get_image_intratile_offset_el(&cmd_buffer->device->isl_dev,
978 &dest_image->color_surface.isl,
979 pRegions[r].imageSubresource.mipLevel,
980 pRegions[r].imageSubresource.baseArrayLayer + slice_array,
981 pRegions[r].imageOffset.z + slice_3d,
982 &img_o, &img_x, &img_y);
983
984 VkOffset3D dest_offset_el = meta_region_offset_el(dest_image, & pRegions[r].imageOffset);
985 dest_offset_el.x += img_x;
986 dest_offset_el.y += img_y;
987 dest_offset_el.z = 0;
988
989 struct anv_image_view dest_iview;
990 anv_image_view_init(&dest_iview, cmd_buffer->device,
991 &(VkImageViewCreateInfo) {
992 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
993 .image = anv_image_to_handle(dest_image),
994 .viewType = anv_meta_get_view_type(dest_image),
995 .format = image_format,
996 .subresourceRange = {
997 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
998 .baseMipLevel = pRegions[r].imageSubresource.mipLevel,
999 .levelCount = 1,
1000 .baseArrayLayer = dest_base_array_slice +
1001 slice_array + slice_3d,
1002 .layerCount = 1
1003 },
1004 },
1005 cmd_buffer, img_o);
1006
1007 const VkExtent3D img_extent_el = meta_region_extent_el(dest_image->vk_format,
1008 &pRegions[r].imageExtent);
1009
1010 meta_emit_blit(cmd_buffer,
1011 src_image,
1012 &src_iview,
1013 (VkOffset3D){0, 0, 0},
1014 img_extent_el,
1015 dest_image,
1016 &dest_iview,
1017 dest_offset_el,
1018 img_extent_el,
1019 VK_FILTER_NEAREST);
1020
1021 /* Once we've done the blit, all of the actual information about
1022 * the image is embedded in the command buffer so we can just
1023 * increment the offset directly in the image effectively
1024 * re-binding it to different backing memory.
1025 */
1026 src_image->offset += src_image->extent.width *
1027 src_image->extent.height *
1028 src_image->format->isl_layout->bs;
1029
1030 if (dest_image->type == VK_IMAGE_TYPE_3D)
1031 slice_3d++;
1032 else
1033 slice_array++;
1034 }
1035
1036 anv_DestroyImage(vk_device, anv_image_to_handle(src_image),
1037 &cmd_buffer->pool->alloc);
1038 }
1039
1040 meta_finish_blit(cmd_buffer, &saved_state);
1041 }
1042
1043 void anv_CmdCopyImageToBuffer(
1044 VkCommandBuffer commandBuffer,
1045 VkImage srcImage,
1046 VkImageLayout srcImageLayout,
1047 VkBuffer destBuffer,
1048 uint32_t regionCount,
1049 const VkBufferImageCopy* pRegions)
1050 {
1051 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
1052 ANV_FROM_HANDLE(anv_image, src_image, srcImage);
1053 VkDevice vk_device = anv_device_to_handle(cmd_buffer->device);
1054 struct anv_meta_saved_state saved_state;
1055
1056
1057 /* The Vulkan 1.0 spec says "srcImage must have a sample count equal to
1058 * VK_SAMPLE_COUNT_1_BIT."
1059 */
1060 assert(src_image->samples == 1);
1061
1062 meta_prepare_blit(cmd_buffer, &saved_state);
1063
1064 for (unsigned r = 0; r < regionCount; r++) {
1065 VkImageAspectFlags aspect = pRegions[r].imageSubresource.aspectMask;
1066
1067 VkFormat image_format = choose_iview_format(src_image, aspect);
1068
1069 struct anv_image_view src_iview;
1070 anv_image_view_init(&src_iview, cmd_buffer->device,
1071 &(VkImageViewCreateInfo) {
1072 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
1073 .image = srcImage,
1074 .viewType = anv_meta_get_view_type(src_image),
1075 .format = image_format,
1076 .subresourceRange = {
1077 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
1078 .baseMipLevel = pRegions[r].imageSubresource.mipLevel,
1079 .levelCount = 1,
1080 .baseArrayLayer = pRegions[r].imageSubresource.baseArrayLayer,
1081 .layerCount = pRegions[r].imageSubresource.layerCount,
1082 },
1083 },
1084 cmd_buffer, 0);
1085
1086 struct anv_image *dest_image =
1087 make_image_for_buffer(vk_device, destBuffer, src_image->vk_format,
1088 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
1089 src_image->type, &cmd_buffer->pool->alloc,
1090 &pRegions[r]);
1091
1092 unsigned num_slices;
1093 if (src_image->type == VK_IMAGE_TYPE_3D) {
1094 assert(pRegions[r].imageSubresource.layerCount == 1);
1095 num_slices = pRegions[r].imageExtent.depth;
1096 } else {
1097 assert(pRegions[r].imageExtent.depth == 1);
1098 num_slices = pRegions[r].imageSubresource.layerCount;
1099 }
1100
1101 for (unsigned slice = 0; slice < num_slices; slice++) {
1102 VkOffset3D src_offset = pRegions[r].imageOffset;
1103 src_offset.z += slice;
1104
1105 struct anv_image_view dest_iview;
1106 anv_image_view_init(&dest_iview, cmd_buffer->device,
1107 &(VkImageViewCreateInfo) {
1108 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
1109 .image = anv_image_to_handle(dest_image),
1110 .viewType = VK_IMAGE_VIEW_TYPE_2D,
1111 .format = dest_image->vk_format,
1112 .subresourceRange = {
1113 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
1114 .baseMipLevel = 0,
1115 .levelCount = 1,
1116 .baseArrayLayer = 0,
1117 .layerCount = 1
1118 },
1119 },
1120 cmd_buffer, 0);
1121
1122 meta_emit_blit(cmd_buffer,
1123 anv_image_from_handle(srcImage),
1124 &src_iview,
1125 src_offset,
1126 pRegions[r].imageExtent,
1127 dest_image,
1128 &dest_iview,
1129 (VkOffset3D) { 0, 0, 0 },
1130 pRegions[r].imageExtent,
1131 VK_FILTER_NEAREST);
1132
1133 /* Once we've done the blit, all of the actual information about
1134 * the image is embedded in the command buffer so we can just
1135 * increment the offset directly in the image effectively
1136 * re-binding it to different backing memory.
1137 */
1138 dest_image->offset += dest_image->extent.width *
1139 dest_image->extent.height *
1140 src_image->format->isl_layout->bs;
1141 }
1142
1143 anv_DestroyImage(vk_device, anv_image_to_handle(dest_image),
1144 &cmd_buffer->pool->alloc);
1145 }
1146
1147 meta_finish_blit(cmd_buffer, &saved_state);
1148 }
1149
1150 void
1151 anv_device_finish_meta_blit_state(struct anv_device *device)
1152 {
1153 anv_DestroyRenderPass(anv_device_to_handle(device),
1154 device->meta_state.blit.render_pass,
1155 &device->meta_state.alloc);
1156 anv_DestroyPipeline(anv_device_to_handle(device),
1157 device->meta_state.blit.pipeline_1d_src,
1158 &device->meta_state.alloc);
1159 anv_DestroyPipeline(anv_device_to_handle(device),
1160 device->meta_state.blit.pipeline_2d_src,
1161 &device->meta_state.alloc);
1162 anv_DestroyPipeline(anv_device_to_handle(device),
1163 device->meta_state.blit.pipeline_3d_src,
1164 &device->meta_state.alloc);
1165 anv_DestroyPipelineLayout(anv_device_to_handle(device),
1166 device->meta_state.blit.pipeline_layout,
1167 &device->meta_state.alloc);
1168 anv_DestroyDescriptorSetLayout(anv_device_to_handle(device),
1169 device->meta_state.blit.ds_layout,
1170 &device->meta_state.alloc);
1171 }
1172
1173 VkResult
1174 anv_device_init_meta_blit_state(struct anv_device *device)
1175 {
1176 VkResult result;
1177
1178 result = anv_CreateRenderPass(anv_device_to_handle(device),
1179 &(VkRenderPassCreateInfo) {
1180 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1181 .attachmentCount = 1,
1182 .pAttachments = &(VkAttachmentDescription) {
1183 .format = VK_FORMAT_UNDEFINED, /* Our shaders don't care */
1184 .loadOp = VK_ATTACHMENT_LOAD_OP_LOAD,
1185 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1186 .initialLayout = VK_IMAGE_LAYOUT_GENERAL,
1187 .finalLayout = VK_IMAGE_LAYOUT_GENERAL,
1188 },
1189 .subpassCount = 1,
1190 .pSubpasses = &(VkSubpassDescription) {
1191 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1192 .inputAttachmentCount = 0,
1193 .colorAttachmentCount = 1,
1194 .pColorAttachments = &(VkAttachmentReference) {
1195 .attachment = 0,
1196 .layout = VK_IMAGE_LAYOUT_GENERAL,
1197 },
1198 .pResolveAttachments = NULL,
1199 .pDepthStencilAttachment = &(VkAttachmentReference) {
1200 .attachment = VK_ATTACHMENT_UNUSED,
1201 .layout = VK_IMAGE_LAYOUT_GENERAL,
1202 },
1203 .preserveAttachmentCount = 1,
1204 .pPreserveAttachments = (uint32_t[]) { 0 },
1205 },
1206 .dependencyCount = 0,
1207 }, &device->meta_state.alloc, &device->meta_state.blit.render_pass);
1208 if (result != VK_SUCCESS)
1209 goto fail;
1210
1211 /* We don't use a vertex shader for clearing, but instead build and pass
1212 * the VUEs directly to the rasterization backend. However, we do need
1213 * to provide GLSL source for the vertex shader so that the compiler
1214 * does not dead-code our inputs.
1215 */
1216 struct anv_shader_module vs = {
1217 .nir = build_nir_vertex_shader(),
1218 };
1219
1220 struct anv_shader_module fs_1d = {
1221 .nir = build_nir_copy_fragment_shader(GLSL_SAMPLER_DIM_1D),
1222 };
1223
1224 struct anv_shader_module fs_2d = {
1225 .nir = build_nir_copy_fragment_shader(GLSL_SAMPLER_DIM_2D),
1226 };
1227
1228 struct anv_shader_module fs_3d = {
1229 .nir = build_nir_copy_fragment_shader(GLSL_SAMPLER_DIM_3D),
1230 };
1231
1232 VkPipelineVertexInputStateCreateInfo vi_create_info = {
1233 .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
1234 .vertexBindingDescriptionCount = 2,
1235 .pVertexBindingDescriptions = (VkVertexInputBindingDescription[]) {
1236 {
1237 .binding = 0,
1238 .stride = 0,
1239 .inputRate = VK_VERTEX_INPUT_RATE_VERTEX
1240 },
1241 {
1242 .binding = 1,
1243 .stride = 5 * sizeof(float),
1244 .inputRate = VK_VERTEX_INPUT_RATE_VERTEX
1245 },
1246 },
1247 .vertexAttributeDescriptionCount = 3,
1248 .pVertexAttributeDescriptions = (VkVertexInputAttributeDescription[]) {
1249 {
1250 /* VUE Header */
1251 .location = 0,
1252 .binding = 0,
1253 .format = VK_FORMAT_R32G32B32A32_UINT,
1254 .offset = 0
1255 },
1256 {
1257 /* Position */
1258 .location = 1,
1259 .binding = 1,
1260 .format = VK_FORMAT_R32G32_SFLOAT,
1261 .offset = 0
1262 },
1263 {
1264 /* Texture Coordinate */
1265 .location = 2,
1266 .binding = 1,
1267 .format = VK_FORMAT_R32G32B32_SFLOAT,
1268 .offset = 8
1269 }
1270 }
1271 };
1272
1273 VkDescriptorSetLayoutCreateInfo ds_layout_info = {
1274 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
1275 .bindingCount = 1,
1276 .pBindings = (VkDescriptorSetLayoutBinding[]) {
1277 {
1278 .binding = 0,
1279 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1280 .descriptorCount = 1,
1281 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1282 .pImmutableSamplers = NULL
1283 },
1284 }
1285 };
1286 result = anv_CreateDescriptorSetLayout(anv_device_to_handle(device),
1287 &ds_layout_info,
1288 &device->meta_state.alloc,
1289 &device->meta_state.blit.ds_layout);
1290 if (result != VK_SUCCESS)
1291 goto fail_render_pass;
1292
1293 result = anv_CreatePipelineLayout(anv_device_to_handle(device),
1294 &(VkPipelineLayoutCreateInfo) {
1295 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1296 .setLayoutCount = 1,
1297 .pSetLayouts = &device->meta_state.blit.ds_layout,
1298 },
1299 &device->meta_state.alloc, &device->meta_state.blit.pipeline_layout);
1300 if (result != VK_SUCCESS)
1301 goto fail_descriptor_set_layout;
1302
1303 VkPipelineShaderStageCreateInfo pipeline_shader_stages[] = {
1304 {
1305 .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
1306 .stage = VK_SHADER_STAGE_VERTEX_BIT,
1307 .module = anv_shader_module_to_handle(&vs),
1308 .pName = "main",
1309 .pSpecializationInfo = NULL
1310 }, {
1311 .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
1312 .stage = VK_SHADER_STAGE_FRAGMENT_BIT,
1313 .module = VK_NULL_HANDLE, /* TEMPLATE VALUE! FILL ME IN! */
1314 .pName = "main",
1315 .pSpecializationInfo = NULL
1316 },
1317 };
1318
1319 const VkGraphicsPipelineCreateInfo vk_pipeline_info = {
1320 .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
1321 .stageCount = ARRAY_SIZE(pipeline_shader_stages),
1322 .pStages = pipeline_shader_stages,
1323 .pVertexInputState = &vi_create_info,
1324 .pInputAssemblyState = &(VkPipelineInputAssemblyStateCreateInfo) {
1325 .sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
1326 .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
1327 .primitiveRestartEnable = false,
1328 },
1329 .pViewportState = &(VkPipelineViewportStateCreateInfo) {
1330 .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
1331 .viewportCount = 1,
1332 .scissorCount = 1,
1333 },
1334 .pRasterizationState = &(VkPipelineRasterizationStateCreateInfo) {
1335 .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
1336 .rasterizerDiscardEnable = false,
1337 .polygonMode = VK_POLYGON_MODE_FILL,
1338 .cullMode = VK_CULL_MODE_NONE,
1339 .frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE
1340 },
1341 .pMultisampleState = &(VkPipelineMultisampleStateCreateInfo) {
1342 .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
1343 .rasterizationSamples = 1,
1344 .sampleShadingEnable = false,
1345 .pSampleMask = (VkSampleMask[]) { UINT32_MAX },
1346 },
1347 .pColorBlendState = &(VkPipelineColorBlendStateCreateInfo) {
1348 .sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
1349 .attachmentCount = 1,
1350 .pAttachments = (VkPipelineColorBlendAttachmentState []) {
1351 { .colorWriteMask =
1352 VK_COLOR_COMPONENT_A_BIT |
1353 VK_COLOR_COMPONENT_R_BIT |
1354 VK_COLOR_COMPONENT_G_BIT |
1355 VK_COLOR_COMPONENT_B_BIT },
1356 }
1357 },
1358 .pDynamicState = &(VkPipelineDynamicStateCreateInfo) {
1359 .sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
1360 .dynamicStateCount = 9,
1361 .pDynamicStates = (VkDynamicState[]) {
1362 VK_DYNAMIC_STATE_VIEWPORT,
1363 VK_DYNAMIC_STATE_SCISSOR,
1364 VK_DYNAMIC_STATE_LINE_WIDTH,
1365 VK_DYNAMIC_STATE_DEPTH_BIAS,
1366 VK_DYNAMIC_STATE_BLEND_CONSTANTS,
1367 VK_DYNAMIC_STATE_DEPTH_BOUNDS,
1368 VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK,
1369 VK_DYNAMIC_STATE_STENCIL_WRITE_MASK,
1370 VK_DYNAMIC_STATE_STENCIL_REFERENCE,
1371 },
1372 },
1373 .flags = 0,
1374 .layout = device->meta_state.blit.pipeline_layout,
1375 .renderPass = device->meta_state.blit.render_pass,
1376 .subpass = 0,
1377 };
1378
1379 const struct anv_graphics_pipeline_create_info anv_pipeline_info = {
1380 .color_attachment_count = -1,
1381 .use_repclear = false,
1382 .disable_viewport = true,
1383 .disable_scissor = true,
1384 .disable_vs = true,
1385 .use_rectlist = true
1386 };
1387
1388 pipeline_shader_stages[1].module = anv_shader_module_to_handle(&fs_1d);
1389 result = anv_graphics_pipeline_create(anv_device_to_handle(device),
1390 VK_NULL_HANDLE,
1391 &vk_pipeline_info, &anv_pipeline_info,
1392 &device->meta_state.alloc, &device->meta_state.blit.pipeline_1d_src);
1393 if (result != VK_SUCCESS)
1394 goto fail_pipeline_layout;
1395
1396 pipeline_shader_stages[1].module = anv_shader_module_to_handle(&fs_2d);
1397 result = anv_graphics_pipeline_create(anv_device_to_handle(device),
1398 VK_NULL_HANDLE,
1399 &vk_pipeline_info, &anv_pipeline_info,
1400 &device->meta_state.alloc, &device->meta_state.blit.pipeline_2d_src);
1401 if (result != VK_SUCCESS)
1402 goto fail_pipeline_1d;
1403
1404 pipeline_shader_stages[1].module = anv_shader_module_to_handle(&fs_3d);
1405 result = anv_graphics_pipeline_create(anv_device_to_handle(device),
1406 VK_NULL_HANDLE,
1407 &vk_pipeline_info, &anv_pipeline_info,
1408 &device->meta_state.alloc, &device->meta_state.blit.pipeline_3d_src);
1409 if (result != VK_SUCCESS)
1410 goto fail_pipeline_2d;
1411
1412 ralloc_free(vs.nir);
1413 ralloc_free(fs_1d.nir);
1414 ralloc_free(fs_2d.nir);
1415 ralloc_free(fs_3d.nir);
1416
1417 return VK_SUCCESS;
1418
1419 fail_pipeline_2d:
1420 anv_DestroyPipeline(anv_device_to_handle(device),
1421 device->meta_state.blit.pipeline_2d_src,
1422 &device->meta_state.alloc);
1423
1424 fail_pipeline_1d:
1425 anv_DestroyPipeline(anv_device_to_handle(device),
1426 device->meta_state.blit.pipeline_1d_src,
1427 &device->meta_state.alloc);
1428
1429 fail_pipeline_layout:
1430 anv_DestroyPipelineLayout(anv_device_to_handle(device),
1431 device->meta_state.blit.pipeline_layout,
1432 &device->meta_state.alloc);
1433 fail_descriptor_set_layout:
1434 anv_DestroyDescriptorSetLayout(anv_device_to_handle(device),
1435 device->meta_state.blit.ds_layout,
1436 &device->meta_state.alloc);
1437 fail_render_pass:
1438 anv_DestroyRenderPass(anv_device_to_handle(device),
1439 device->meta_state.blit.render_pass,
1440 &device->meta_state.alloc);
1441
1442 ralloc_free(vs.nir);
1443 ralloc_free(fs_1d.nir);
1444 ralloc_free(fs_2d.nir);
1445 ralloc_free(fs_3d.nir);
1446 fail:
1447 return result;
1448 }