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