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