vk/meta: Add support for copying arbitrary size buffers
[mesa.git] / src / vulkan / 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 "private.h"
31 #include "glsl_helpers.h"
32
33 static void
34 anv_device_init_meta_clear_state(struct anv_device *device)
35 {
36 VkPipelineIaStateCreateInfo ia_create_info = {
37 .sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO,
38 .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
39 .disableVertexReuse = false,
40 .primitiveRestartEnable = false,
41 .primitiveRestartIndex = 0
42 };
43
44 /* We don't use a vertex shader for clearing, but instead build and pass
45 * the VUEs directly to the rasterization backend.
46 */
47 VkShader fs = GLSL_VK_SHADER(device, FRAGMENT,
48 out vec4 f_color;
49 flat in vec4 v_color;
50 void main()
51 {
52 f_color = v_color;
53 }
54 );
55
56 VkPipelineShaderStageCreateInfo fs_create_info = {
57 .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
58 .pNext = &ia_create_info,
59 .shader = {
60 .stage = VK_SHADER_STAGE_FRAGMENT,
61 .shader = fs,
62 .linkConstBufferCount = 0,
63 .pLinkConstBufferInfo = NULL,
64 .pSpecializationInfo = NULL
65 }
66 };
67
68 /* We use instanced rendering to clear multiple render targets. We have two
69 * vertex buffers: the first vertex buffer holds per-vertex data and
70 * provides the vertices for the clear rectangle. The second one holds
71 * per-instance data, which consists of the VUE header (which selects the
72 * layer) and the color (Vulkan supports per-RT clear colors).
73 */
74 VkPipelineVertexInputCreateInfo vi_create_info = {
75 .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO,
76 .pNext = &fs_create_info,
77 .bindingCount = 2,
78 .pVertexBindingDescriptions = (VkVertexInputBindingDescription[]) {
79 {
80 .binding = 0,
81 .strideInBytes = 8,
82 .stepRate = VK_VERTEX_INPUT_STEP_RATE_VERTEX
83 },
84 {
85 .binding = 1,
86 .strideInBytes = 32,
87 .stepRate = VK_VERTEX_INPUT_STEP_RATE_INSTANCE
88 },
89 },
90 .attributeCount = 3,
91 .pVertexAttributeDescriptions = (VkVertexInputAttributeDescription[]) {
92 {
93 /* VUE Header */
94 .location = 0,
95 .binding = 1,
96 .format = VK_FORMAT_R32G32B32A32_UINT,
97 .offsetInBytes = 0
98 },
99 {
100 /* Position */
101 .location = 1,
102 .binding = 0,
103 .format = VK_FORMAT_R32G32_SFLOAT,
104 .offsetInBytes = 0
105 },
106 {
107 /* Color */
108 .location = 2,
109 .binding = 1,
110 .format = VK_FORMAT_R32G32B32A32_SFLOAT,
111 .offsetInBytes = 16
112 }
113 }
114 };
115
116 VkPipelineRsStateCreateInfo rs_create_info = {
117 .sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO,
118 .pNext = &vi_create_info,
119 .depthClipEnable = true,
120 .rasterizerDiscardEnable = false,
121 .fillMode = VK_FILL_MODE_SOLID,
122 .cullMode = VK_CULL_MODE_NONE,
123 .frontFace = VK_FRONT_FACE_CCW
124 };
125
126 anv_pipeline_create((VkDevice) device,
127 &(VkGraphicsPipelineCreateInfo) {
128 .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
129 .pNext = &rs_create_info,
130 .flags = 0,
131 .layout = 0
132 },
133 &(struct anv_pipeline_create_info) {
134 .use_repclear = true,
135 .disable_viewport = true,
136 .use_rectlist = true
137 },
138 &device->clear_state.pipeline);
139
140 anv_DestroyObject((VkDevice) device, VK_OBJECT_TYPE_SHADER, fs);
141
142 anv_CreateDynamicRasterState((VkDevice) device,
143 &(VkDynamicRsStateCreateInfo) {
144 .sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO,
145 },
146 &device->clear_state.rs_state);
147 }
148
149 #define NUM_VB_USED 2
150 struct anv_saved_state {
151 struct anv_bindings bindings;
152 struct anv_bindings *old_bindings;
153 struct anv_pipeline *old_pipeline;
154 };
155
156 static void
157 anv_cmd_buffer_save(struct anv_cmd_buffer *cmd_buffer,
158 struct anv_saved_state *state)
159 {
160 state->old_bindings = cmd_buffer->bindings;
161 cmd_buffer->bindings = &state->bindings;
162 state->old_pipeline = cmd_buffer->pipeline;
163 }
164
165 static void
166 anv_cmd_buffer_restore(struct anv_cmd_buffer *cmd_buffer,
167 const struct anv_saved_state *state)
168 {
169 cmd_buffer->bindings = state->old_bindings;
170 cmd_buffer->pipeline = state->old_pipeline;
171
172 cmd_buffer->vb_dirty |= (1 << NUM_VB_USED) - 1;
173 cmd_buffer->dirty |= ANV_CMD_BUFFER_PIPELINE_DIRTY |
174 ANV_CMD_BUFFER_DESCRIPTOR_SET_DIRTY;
175 }
176
177 struct vue_header {
178 uint32_t Reserved;
179 uint32_t RTAIndex;
180 uint32_t ViewportIndex;
181 float PointWidth;
182 };
183
184 void
185 anv_cmd_buffer_clear(struct anv_cmd_buffer *cmd_buffer,
186 struct anv_render_pass *pass)
187 {
188 struct anv_device *device = cmd_buffer->device;
189 struct anv_framebuffer *fb = cmd_buffer->framebuffer;
190 struct anv_saved_state saved_state;
191 struct anv_state state;
192 uint32_t size;
193
194 struct instance_data {
195 struct vue_header vue_header;
196 float color[4];
197 } *instance_data;
198
199 if (pass->num_clear_layers == 0)
200 return;
201
202 const float vertex_data[] = {
203 /* Rect-list coordinates */
204 0.0, 0.0,
205 fb->width, 0.0,
206 fb->width, fb->height,
207
208 /* Align to 16 bytes */
209 0.0, 0.0,
210 };
211
212 size = sizeof(vertex_data) + pass->num_clear_layers * sizeof(instance_data[0]);
213 state = anv_state_stream_alloc(&cmd_buffer->surface_state_stream, size, 16);
214
215 memcpy(state.map, vertex_data, sizeof(vertex_data));
216 instance_data = state.map + sizeof(vertex_data);
217
218 for (uint32_t i = 0; i < pass->num_layers; i++) {
219 if (pass->layers[i].color_load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) {
220 *instance_data++ = (struct instance_data) {
221 .vue_header = {
222 .RTAIndex = i,
223 .ViewportIndex = 0,
224 .PointWidth = 0.0
225 },
226 .color = {
227 pass->layers[i].clear_color.color.floatColor[0],
228 pass->layers[i].clear_color.color.floatColor[1],
229 pass->layers[i].clear_color.color.floatColor[2],
230 pass->layers[i].clear_color.color.floatColor[3],
231 }
232 };
233 }
234 }
235
236 struct anv_buffer vertex_buffer = {
237 .device = cmd_buffer->device,
238 .size = size,
239 .bo = &device->surface_state_block_pool.bo,
240 .offset = state.offset
241 };
242
243 anv_cmd_buffer_save(cmd_buffer, &saved_state);
244
245 /* Initialize render targets for the meta bindings. */
246 anv_cmd_buffer_fill_render_targets(cmd_buffer);
247
248 anv_CmdBindVertexBuffers((VkCmdBuffer) cmd_buffer, 0, 2,
249 (VkBuffer[]) {
250 (VkBuffer) &vertex_buffer,
251 (VkBuffer) &vertex_buffer
252 },
253 (VkDeviceSize[]) {
254 0,
255 sizeof(vertex_data)
256 });
257
258 if ((VkPipeline) cmd_buffer->pipeline != device->clear_state.pipeline)
259 anv_CmdBindPipeline((VkCmdBuffer) cmd_buffer,
260 VK_PIPELINE_BIND_POINT_GRAPHICS, device->clear_state.pipeline);
261
262 /* We don't need anything here, only set if not already set. */
263 if (cmd_buffer->rs_state == NULL)
264 anv_CmdBindDynamicStateObject((VkCmdBuffer) cmd_buffer,
265 VK_STATE_BIND_POINT_RASTER,
266 device->clear_state.rs_state);
267
268 if (cmd_buffer->vp_state == NULL)
269 anv_CmdBindDynamicStateObject((VkCmdBuffer) cmd_buffer,
270 VK_STATE_BIND_POINT_VIEWPORT,
271 cmd_buffer->framebuffer->vp_state);
272
273 anv_CmdDraw((VkCmdBuffer) cmd_buffer, 0, 3, 0, pass->num_clear_layers);
274
275 /* Restore API state */
276 anv_cmd_buffer_restore(cmd_buffer, &saved_state);
277
278 }
279
280 static void
281 anv_device_init_meta_blit_state(struct anv_device *device)
282 {
283 VkPipelineIaStateCreateInfo ia_create_info = {
284 .sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO,
285 .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
286 .disableVertexReuse = false,
287 .primitiveRestartEnable = false,
288 .primitiveRestartIndex = 0
289 };
290
291 /* We don't use a vertex shader for clearing, but instead build and pass
292 * the VUEs directly to the rasterization backend. However, we do need
293 * to provide GLSL source for the vertex shader so that the compiler
294 * does not dead-code our inputs.
295 */
296 VkShader vs = GLSL_VK_SHADER(device, VERTEX,
297 in vec2 a_pos;
298 in vec2 a_tex_coord;
299 out vec4 v_tex_coord;
300 void main()
301 {
302 v_tex_coord = vec4(a_tex_coord, 0, 1);
303 gl_Position = vec4(a_pos, 0, 1);
304 }
305 );
306
307 VkShader fs = GLSL_VK_SHADER(device, FRAGMENT,
308 out vec4 f_color;
309 in vec4 v_tex_coord;
310 layout(set = 0, binding = 0) uniform sampler2D u_tex;
311 void main()
312 {
313 f_color = texture(u_tex, v_tex_coord.xy);
314 }
315 );
316
317 VkPipelineShaderStageCreateInfo vs_create_info = {
318 .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
319 .pNext = &ia_create_info,
320 .shader = {
321 .stage = VK_SHADER_STAGE_VERTEX,
322 .shader = vs,
323 .linkConstBufferCount = 0,
324 .pLinkConstBufferInfo = NULL,
325 .pSpecializationInfo = NULL
326 }
327 };
328
329 VkPipelineShaderStageCreateInfo fs_create_info = {
330 .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
331 .pNext = &vs_create_info,
332 .shader = {
333 .stage = VK_SHADER_STAGE_FRAGMENT,
334 .shader = fs,
335 .linkConstBufferCount = 0,
336 .pLinkConstBufferInfo = NULL,
337 .pSpecializationInfo = NULL
338 }
339 };
340
341 VkPipelineVertexInputCreateInfo vi_create_info = {
342 .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO,
343 .pNext = &fs_create_info,
344 .bindingCount = 2,
345 .pVertexBindingDescriptions = (VkVertexInputBindingDescription[]) {
346 {
347 .binding = 0,
348 .strideInBytes = 0,
349 .stepRate = VK_VERTEX_INPUT_STEP_RATE_VERTEX
350 },
351 {
352 .binding = 1,
353 .strideInBytes = 16,
354 .stepRate = VK_VERTEX_INPUT_STEP_RATE_VERTEX
355 },
356 },
357 .attributeCount = 3,
358 .pVertexAttributeDescriptions = (VkVertexInputAttributeDescription[]) {
359 {
360 /* VUE Header */
361 .location = 0,
362 .binding = 0,
363 .format = VK_FORMAT_R32G32B32A32_UINT,
364 .offsetInBytes = 0
365 },
366 {
367 /* Position */
368 .location = 1,
369 .binding = 1,
370 .format = VK_FORMAT_R32G32_SFLOAT,
371 .offsetInBytes = 0
372 },
373 {
374 /* Texture Coordinate */
375 .location = 2,
376 .binding = 1,
377 .format = VK_FORMAT_R32G32_SFLOAT,
378 .offsetInBytes = 8
379 }
380 }
381 };
382
383 VkDescriptorSetLayoutCreateInfo ds_layout_info = {
384 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
385 .count = 1,
386 .pBinding = (VkDescriptorSetLayoutBinding[]) {
387 {
388 .descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
389 .count = 1,
390 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
391 .pImmutableSamplers = NULL
392 },
393 }
394 };
395 anv_CreateDescriptorSetLayout((VkDevice) device, &ds_layout_info,
396 &device->blit_state.ds_layout);
397
398 VkPipelineLayoutCreateInfo pipeline_layout_info = {
399 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
400 .descriptorSetCount = 1,
401 .pSetLayouts = &device->blit_state.ds_layout,
402 };
403
404 VkPipelineLayout pipeline_layout;
405 anv_CreatePipelineLayout((VkDevice) device, &pipeline_layout_info,
406 &pipeline_layout);
407
408 VkPipelineRsStateCreateInfo rs_create_info = {
409 .sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO,
410 .pNext = &vi_create_info,
411 .depthClipEnable = true,
412 .rasterizerDiscardEnable = false,
413 .fillMode = VK_FILL_MODE_SOLID,
414 .cullMode = VK_CULL_MODE_NONE,
415 .frontFace = VK_FRONT_FACE_CCW
416 };
417
418 VkGraphicsPipelineCreateInfo pipeline_info = {
419 .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
420 .pNext = &rs_create_info,
421 .flags = 0,
422 .layout = pipeline_layout,
423 };
424
425 anv_pipeline_create((VkDevice) device, &pipeline_info,
426 &(struct anv_pipeline_create_info) {
427 .use_repclear = false,
428 .disable_viewport = true,
429 .disable_scissor = true,
430 .disable_vs = true,
431 .use_rectlist = true
432 },
433 &device->blit_state.pipeline);
434
435 anv_DestroyObject((VkDevice) device, VK_OBJECT_TYPE_SHADER, vs);
436 anv_DestroyObject((VkDevice) device, VK_OBJECT_TYPE_SHADER, fs);
437
438 anv_CreateDynamicRasterState((VkDevice) device,
439 &(VkDynamicRsStateCreateInfo) {
440 .sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO,
441 },
442 &device->blit_state.rs_state);
443 }
444
445 static void
446 meta_prepare_blit(struct anv_cmd_buffer *cmd_buffer,
447 struct anv_saved_state *saved_state)
448 {
449 struct anv_device *device = cmd_buffer->device;
450
451 anv_cmd_buffer_save(cmd_buffer, saved_state);
452
453 if ((VkPipeline) cmd_buffer->pipeline != device->blit_state.pipeline)
454 anv_CmdBindPipeline((VkCmdBuffer) cmd_buffer,
455 VK_PIPELINE_BIND_POINT_GRAPHICS,
456 device->blit_state.pipeline);
457
458 /* We don't need anything here, only set if not already set. */
459 if (cmd_buffer->rs_state == NULL)
460 anv_CmdBindDynamicStateObject((VkCmdBuffer) cmd_buffer,
461 VK_STATE_BIND_POINT_RASTER,
462 device->blit_state.rs_state);
463 }
464
465 struct blit_region {
466 VkOffset3D src_offset;
467 VkExtent3D src_extent;
468 VkOffset3D dest_offset;
469 VkExtent3D dest_extent;
470 };
471
472 static void
473 meta_emit_blit(struct anv_cmd_buffer *cmd_buffer,
474 struct anv_surface_view *src,
475 VkOffset3D src_offset,
476 VkExtent3D src_extent,
477 struct anv_surface_view *dest,
478 VkOffset3D dest_offset,
479 VkExtent3D dest_extent)
480 {
481 struct anv_device *device = cmd_buffer->device;
482
483 struct blit_vb_data {
484 float pos[2];
485 float tex_coord[2];
486 } *vb_data;
487
488 unsigned vb_size = sizeof(struct vue_header) + 3 * sizeof(*vb_data);
489
490 struct anv_state vb_state =
491 anv_state_stream_alloc(&cmd_buffer->surface_state_stream, vb_size, 16);
492 memset(vb_state.map, 0, sizeof(struct vue_header));
493 vb_data = vb_state.map + sizeof(struct vue_header);
494
495 vb_data[0] = (struct blit_vb_data) {
496 .pos = {
497 dest_offset.x + dest_extent.width,
498 dest_offset.y + dest_extent.height,
499 },
500 .tex_coord = {
501 (float)(src_offset.x + src_extent.width) / (float)src->extent.width,
502 (float)(src_offset.y + src_extent.height) / (float)src->extent.height,
503 },
504 };
505
506 vb_data[1] = (struct blit_vb_data) {
507 .pos = {
508 dest_offset.x,
509 dest_offset.y + dest_extent.height,
510 },
511 .tex_coord = {
512 (float)src_offset.x / (float)src->extent.width,
513 (float)(src_offset.y + src_extent.height) / (float)src->extent.height,
514 },
515 };
516
517 vb_data[2] = (struct blit_vb_data) {
518 .pos = {
519 dest_offset.x,
520 dest_offset.y,
521 },
522 .tex_coord = {
523 (float)src_offset.x / (float)src->extent.width,
524 (float)src_offset.y / (float)src->extent.height,
525 },
526 };
527
528 struct anv_buffer vertex_buffer = {
529 .device = device,
530 .size = vb_size,
531 .bo = &device->surface_state_block_pool.bo,
532 .offset = vb_state.offset,
533 };
534
535 anv_CmdBindVertexBuffers((VkCmdBuffer) cmd_buffer, 0, 2,
536 (VkBuffer[]) {
537 (VkBuffer) &vertex_buffer,
538 (VkBuffer) &vertex_buffer
539 },
540 (VkDeviceSize[]) {
541 0,
542 sizeof(struct vue_header),
543 });
544
545 uint32_t count;
546 VkDescriptorSet set;
547 anv_AllocDescriptorSets((VkDevice) device, 0 /* pool */,
548 VK_DESCRIPTOR_SET_USAGE_ONE_SHOT,
549 1, &device->blit_state.ds_layout, &set, &count);
550 anv_UpdateDescriptors((VkDevice) device, set, 1,
551 (const void * []) {
552 &(VkUpdateImages) {
553 .sType = VK_STRUCTURE_TYPE_UPDATE_IMAGES,
554 .descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
555 .binding = 0,
556 .count = 1,
557 .pImageViews = (VkImageViewAttachInfo[]) {
558 {
559 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO,
560 .view = (VkImageView) src,
561 .layout = VK_IMAGE_LAYOUT_GENERAL,
562 }
563 }
564 }
565 });
566
567 struct anv_framebuffer *fb;
568 anv_CreateFramebuffer((VkDevice) device,
569 &(VkFramebufferCreateInfo) {
570 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
571 .colorAttachmentCount = 1,
572 .pColorAttachments = (VkColorAttachmentBindInfo[]) {
573 {
574 .view = (VkColorAttachmentView) dest,
575 .layout = VK_IMAGE_LAYOUT_GENERAL
576 }
577 },
578 .pDepthStencilAttachment = NULL,
579 .sampleCount = 1,
580 .width = dest->extent.width,
581 .height = dest->extent.height,
582 .layers = 1
583 }, (VkFramebuffer *)&fb);
584
585
586 VkRenderPass pass;
587 anv_CreateRenderPass((VkDevice )device,
588 &(VkRenderPassCreateInfo) {
589 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
590 .renderArea = { { 0, 0 }, { dest->extent.width, dest->extent.height } },
591 .colorAttachmentCount = 1,
592 .extent = { 0, },
593 .sampleCount = 1,
594 .layers = 1,
595 .pColorFormats = (VkFormat[]) { dest->format },
596 .pColorLayouts = (VkImageLayout[]) { VK_IMAGE_LAYOUT_GENERAL },
597 .pColorLoadOps = (VkAttachmentLoadOp[]) { VK_ATTACHMENT_LOAD_OP_LOAD },
598 .pColorStoreOps = (VkAttachmentStoreOp[]) { VK_ATTACHMENT_STORE_OP_STORE },
599 .pColorLoadClearValues = (VkClearColor[]) {
600 { .color = { .floatColor = { 1.0, 0.0, 0.0, 1.0 } }, .useRawValue = false }
601 },
602 .depthStencilFormat = VK_FORMAT_UNDEFINED,
603 }, &pass);
604
605 anv_CmdBeginRenderPass((VkCmdBuffer) cmd_buffer,
606 &(VkRenderPassBegin) {
607 .renderPass = pass,
608 .framebuffer = (VkFramebuffer) fb,
609 });
610
611 anv_CmdBindDynamicStateObject((VkCmdBuffer) cmd_buffer,
612 VK_STATE_BIND_POINT_VIEWPORT, fb->vp_state);
613
614 anv_CmdBindDescriptorSets((VkCmdBuffer) cmd_buffer,
615 VK_PIPELINE_BIND_POINT_GRAPHICS, 0, 1,
616 &set, 0, NULL);
617
618 anv_CmdDraw((VkCmdBuffer) cmd_buffer, 0, 3, 0, 1);
619
620 anv_CmdEndRenderPass((VkCmdBuffer) cmd_buffer, pass);
621 }
622
623 static void
624 meta_finish_blit(struct anv_cmd_buffer *cmd_buffer,
625 const struct anv_saved_state *saved_state)
626 {
627 anv_cmd_buffer_restore(cmd_buffer, saved_state);
628 }
629
630 static VkFormat
631 vk_format_for_cpp(int cpp)
632 {
633 switch (cpp) {
634 case 1: return VK_FORMAT_R8_UINT;
635 case 2: return VK_FORMAT_R8G8_UINT;
636 case 3: return VK_FORMAT_R8G8B8_UINT;
637 case 4: return VK_FORMAT_R8G8B8A8_UINT;
638 case 6: return VK_FORMAT_R16G16B16_UINT;
639 case 8: return VK_FORMAT_R16G16B16A16_UINT;
640 case 12: return VK_FORMAT_R32G32B32_UINT;
641 case 16: return VK_FORMAT_R32G32B32A32_UINT;
642 default:
643 unreachable("Invalid format cpp");
644 }
645 }
646
647 static void
648 do_buffer_copy(struct anv_cmd_buffer *cmd_buffer,
649 struct anv_bo *src, uint64_t src_offset,
650 struct anv_bo *dest, uint64_t dest_offset,
651 int width, int height, VkFormat copy_format)
652 {
653 VkDevice vk_device = (VkDevice)cmd_buffer->device;
654
655 VkImageCreateInfo image_info = {
656 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
657 .imageType = VK_IMAGE_TYPE_2D,
658 .format = copy_format,
659 .extent = {
660 .width = width,
661 .height = height,
662 .depth = 1,
663 },
664 .mipLevels = 1,
665 .arraySize = 1,
666 .samples = 1,
667 .tiling = VK_IMAGE_TILING_LINEAR,
668 .usage = VK_IMAGE_USAGE_SAMPLED_BIT,
669 .flags = 0,
670 };
671
672 struct anv_image *src_image, *dest_image;
673 anv_CreateImage(vk_device, &image_info, (VkImage *)&src_image);
674 anv_CreateImage(vk_device, &image_info, (VkImage *)&dest_image);
675
676 /* We could use a vk call to bind memory, but that would require
677 * creating a dummy memory object etc. so there's really no point.
678 */
679 src_image->bo = src;
680 src_image->offset = src_offset;
681 dest_image->bo = dest;
682 dest_image->offset = dest_offset;
683
684 struct anv_surface_view src_view;
685 anv_image_view_init(&src_view, cmd_buffer->device,
686 &(VkImageViewCreateInfo) {
687 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
688 .image = (VkImage)src_image,
689 .viewType = VK_IMAGE_VIEW_TYPE_2D,
690 .format = copy_format,
691 .channels = {
692 VK_CHANNEL_SWIZZLE_R,
693 VK_CHANNEL_SWIZZLE_G,
694 VK_CHANNEL_SWIZZLE_B,
695 VK_CHANNEL_SWIZZLE_A
696 },
697 .subresourceRange = {
698 .aspect = VK_IMAGE_ASPECT_COLOR,
699 .baseMipLevel = 0,
700 .mipLevels = 1,
701 .baseArraySlice = 0,
702 .arraySize = 1
703 },
704 .minLod = 0
705 },
706 cmd_buffer);
707
708 struct anv_surface_view dest_view;
709 anv_color_attachment_view_init(&dest_view, cmd_buffer->device,
710 &(VkColorAttachmentViewCreateInfo) {
711 .sType = VK_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
712 .image = (VkImage)dest_image,
713 .format = copy_format,
714 .mipLevel = 0,
715 .baseArraySlice = 0,
716 .arraySize = 1,
717 },
718 cmd_buffer);
719
720 meta_emit_blit(cmd_buffer,
721 &src_view,
722 (VkOffset3D) { 0, 0, 0 },
723 (VkExtent3D) { width, height, 1 },
724 &dest_view,
725 (VkOffset3D) { 0, 0, 0 },
726 (VkExtent3D) { width, height, 1 });
727 }
728
729 void anv_CmdCopyBuffer(
730 VkCmdBuffer cmdBuffer,
731 VkBuffer srcBuffer,
732 VkBuffer destBuffer,
733 uint32_t regionCount,
734 const VkBufferCopy* pRegions)
735 {
736 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *)cmdBuffer;
737 struct anv_buffer *src_buffer = (struct anv_buffer *)srcBuffer;
738 struct anv_buffer *dest_buffer = (struct anv_buffer *)destBuffer;
739 struct anv_saved_state saved_state;
740
741 meta_prepare_blit(cmd_buffer, &saved_state);
742
743 for (unsigned r = 0; r < regionCount; r++) {
744 uint64_t src_offset = src_buffer->offset + pRegions[r].srcOffset;
745 uint64_t dest_offset = dest_buffer->offset + pRegions[r].destOffset;
746 uint64_t copy_size = pRegions[r].copySize;
747
748 /* First, we compute the biggest format that can be used with the
749 * given offsets and size.
750 */
751 int cpp = 16;
752
753 int fs = ffs(src_offset) - 1;
754 if (fs != -1)
755 cpp = MIN2(cpp, 1 << fs);
756 assert(src_offset % cpp == 0);
757
758 fs = ffs(dest_offset) - 1;
759 if (fs != -1)
760 cpp = MIN2(cpp, 1 << fs);
761 assert(dest_offset % cpp == 0);
762
763 fs = ffs(pRegions[r].copySize) - 1;
764 if (fs != -1)
765 cpp = MIN2(cpp, 1 << fs);
766 assert(pRegions[r].copySize % cpp == 0);
767
768 VkFormat copy_format = vk_format_for_cpp(cpp);
769
770 /* This is maximum possible width/height our HW can handle */
771 uint64_t max_surface_dim = 1 << 14;
772
773 /* First, we make a bunch of max-sized copies */
774 uint64_t max_copy_size = max_surface_dim * max_surface_dim * cpp;
775 while (copy_size > max_copy_size) {
776 do_buffer_copy(cmd_buffer, src_buffer->bo, src_offset,
777 dest_buffer->bo, dest_offset,
778 max_surface_dim, max_surface_dim, copy_format);
779 copy_size -= max_copy_size;
780 src_offset += max_copy_size;
781 dest_offset += max_copy_size;
782 }
783
784 uint64_t height = copy_size / (max_surface_dim * cpp);
785 assert(height < max_surface_dim);
786 if (height != 0) {
787 uint64_t rect_copy_size = height * max_surface_dim * cpp;
788 do_buffer_copy(cmd_buffer, src_buffer->bo, src_offset,
789 dest_buffer->bo, dest_offset,
790 max_surface_dim, height, copy_format);
791 copy_size -= rect_copy_size;
792 src_offset += rect_copy_size;
793 dest_offset += rect_copy_size;
794 }
795
796 if (copy_size != 0) {
797 do_buffer_copy(cmd_buffer, src_buffer->bo, src_offset,
798 dest_buffer->bo, dest_offset,
799 copy_size / cpp, 1, copy_format);
800 }
801 }
802
803 meta_finish_blit(cmd_buffer, &saved_state);
804 }
805
806 void anv_CmdCopyImage(
807 VkCmdBuffer cmdBuffer,
808 VkImage srcImage,
809 VkImageLayout srcImageLayout,
810 VkImage destImage,
811 VkImageLayout destImageLayout,
812 uint32_t regionCount,
813 const VkImageCopy* pRegions)
814 {
815 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *)cmdBuffer;
816 struct anv_image *src_image = (struct anv_image *)srcImage;
817 struct anv_saved_state saved_state;
818
819 meta_prepare_blit(cmd_buffer, &saved_state);
820
821 for (unsigned r = 0; r < regionCount; r++) {
822 struct anv_surface_view src_view;
823 anv_image_view_init(&src_view, cmd_buffer->device,
824 &(VkImageViewCreateInfo) {
825 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
826 .image = srcImage,
827 .viewType = VK_IMAGE_VIEW_TYPE_2D,
828 .format = src_image->format,
829 .channels = {
830 VK_CHANNEL_SWIZZLE_R,
831 VK_CHANNEL_SWIZZLE_G,
832 VK_CHANNEL_SWIZZLE_B,
833 VK_CHANNEL_SWIZZLE_A
834 },
835 .subresourceRange = {
836 .aspect = pRegions[r].srcSubresource.aspect,
837 .baseMipLevel = pRegions[r].srcSubresource.mipLevel,
838 .mipLevels = 1,
839 .baseArraySlice = pRegions[r].srcSubresource.arraySlice,
840 .arraySize = 1
841 },
842 .minLod = 0
843 },
844 cmd_buffer);
845
846 struct anv_surface_view dest_view;
847 anv_color_attachment_view_init(&dest_view, cmd_buffer->device,
848 &(VkColorAttachmentViewCreateInfo) {
849 .sType = VK_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
850 .image = destImage,
851 .format = src_image->format,
852 .mipLevel = pRegions[r].destSubresource.mipLevel,
853 .baseArraySlice = pRegions[r].destSubresource.arraySlice,
854 .arraySize = 1,
855 },
856 cmd_buffer);
857
858 meta_emit_blit(cmd_buffer,
859 &src_view,
860 pRegions[r].srcOffset,
861 pRegions[r].extent,
862 &dest_view,
863 pRegions[r].destOffset,
864 pRegions[r].extent);
865 }
866
867 meta_finish_blit(cmd_buffer, &saved_state);
868 }
869
870 void anv_CmdBlitImage(
871 VkCmdBuffer cmdBuffer,
872 VkImage srcImage,
873 VkImageLayout srcImageLayout,
874 VkImage destImage,
875 VkImageLayout destImageLayout,
876 uint32_t regionCount,
877 const VkImageBlit* pRegions)
878 {
879 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *)cmdBuffer;
880 struct anv_image *src_image = (struct anv_image *)srcImage;
881 struct anv_image *dest_image = (struct anv_image *)destImage;
882 struct anv_saved_state saved_state;
883
884 meta_prepare_blit(cmd_buffer, &saved_state);
885
886 for (unsigned r = 0; r < regionCount; r++) {
887 struct anv_surface_view src_view;
888 anv_image_view_init(&src_view, cmd_buffer->device,
889 &(VkImageViewCreateInfo) {
890 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
891 .image = srcImage,
892 .viewType = VK_IMAGE_VIEW_TYPE_2D,
893 .format = src_image->format,
894 .channels = {
895 VK_CHANNEL_SWIZZLE_R,
896 VK_CHANNEL_SWIZZLE_G,
897 VK_CHANNEL_SWIZZLE_B,
898 VK_CHANNEL_SWIZZLE_A
899 },
900 .subresourceRange = {
901 .aspect = pRegions[r].srcSubresource.aspect,
902 .baseMipLevel = pRegions[r].srcSubresource.mipLevel,
903 .mipLevels = 1,
904 .baseArraySlice = pRegions[r].srcSubresource.arraySlice,
905 .arraySize = 1
906 },
907 .minLod = 0
908 },
909 cmd_buffer);
910
911 struct anv_surface_view dest_view;
912 anv_color_attachment_view_init(&dest_view, cmd_buffer->device,
913 &(VkColorAttachmentViewCreateInfo) {
914 .sType = VK_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
915 .image = destImage,
916 .format = dest_image->format,
917 .mipLevel = pRegions[r].destSubresource.mipLevel,
918 .baseArraySlice = pRegions[r].destSubresource.arraySlice,
919 .arraySize = 1,
920 },
921 cmd_buffer);
922
923 meta_emit_blit(cmd_buffer,
924 &src_view,
925 pRegions[r].srcOffset,
926 pRegions[r].srcExtent,
927 &dest_view,
928 pRegions[r].destOffset,
929 pRegions[r].destExtent);
930 }
931
932 meta_finish_blit(cmd_buffer, &saved_state);
933 }
934
935 void anv_CmdCopyBufferToImage(
936 VkCmdBuffer cmdBuffer,
937 VkBuffer srcBuffer,
938 VkImage destImage,
939 VkImageLayout destImageLayout,
940 uint32_t regionCount,
941 const VkBufferImageCopy* pRegions)
942 {
943 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *)cmdBuffer;
944 VkDevice vk_device = (VkDevice) cmd_buffer->device;
945 struct anv_buffer *src_buffer = (struct anv_buffer *)srcBuffer;
946 struct anv_image *dest_image = (struct anv_image *)destImage;
947 struct anv_saved_state saved_state;
948
949 meta_prepare_blit(cmd_buffer, &saved_state);
950
951 for (unsigned r = 0; r < regionCount; r++) {
952 struct anv_image *src_image;
953 anv_CreateImage(vk_device,
954 &(VkImageCreateInfo) {
955 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
956 .imageType = VK_IMAGE_TYPE_2D,
957 .format = dest_image->format,
958 .extent = {
959 .width = pRegions[r].imageExtent.width,
960 .height = pRegions[r].imageExtent.height,
961 .depth = 1,
962 },
963 .mipLevels = 1,
964 .arraySize = 1,
965 .samples = 1,
966 .tiling = VK_IMAGE_TILING_LINEAR,
967 .usage = VK_IMAGE_USAGE_SAMPLED_BIT,
968 .flags = 0,
969 }, (VkImage *)&src_image);
970
971 /* We could use a vk call to bind memory, but that would require
972 * creating a dummy memory object etc. so there's really no point.
973 */
974 src_image->bo = src_buffer->bo;
975 src_image->offset = src_buffer->offset + pRegions[r].bufferOffset;
976
977 struct anv_surface_view src_view;
978 anv_image_view_init(&src_view, cmd_buffer->device,
979 &(VkImageViewCreateInfo) {
980 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
981 .image = (VkImage)src_image,
982 .viewType = VK_IMAGE_VIEW_TYPE_2D,
983 .format = dest_image->format,
984 .channels = {
985 VK_CHANNEL_SWIZZLE_R,
986 VK_CHANNEL_SWIZZLE_G,
987 VK_CHANNEL_SWIZZLE_B,
988 VK_CHANNEL_SWIZZLE_A
989 },
990 .subresourceRange = {
991 .aspect = pRegions[r].imageSubresource.aspect,
992 .baseMipLevel = 0,
993 .mipLevels = 1,
994 .baseArraySlice = 0,
995 .arraySize = 1
996 },
997 .minLod = 0
998 },
999 cmd_buffer);
1000
1001 struct anv_surface_view dest_view;
1002 anv_color_attachment_view_init(&dest_view, cmd_buffer->device,
1003 &(VkColorAttachmentViewCreateInfo) {
1004 .sType = VK_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
1005 .image = (VkImage)dest_image,
1006 .format = dest_image->format,
1007 .mipLevel = pRegions[r].imageSubresource.mipLevel,
1008 .baseArraySlice = pRegions[r].imageSubresource.arraySlice,
1009 .arraySize = 1,
1010 },
1011 cmd_buffer);
1012
1013 meta_emit_blit(cmd_buffer,
1014 &src_view,
1015 (VkOffset3D) { 0, 0, 0 },
1016 pRegions[r].imageExtent,
1017 &dest_view,
1018 pRegions[r].imageOffset,
1019 pRegions[r].imageExtent);
1020 }
1021
1022 meta_finish_blit(cmd_buffer, &saved_state);
1023 }
1024
1025 void anv_CmdCopyImageToBuffer(
1026 VkCmdBuffer cmdBuffer,
1027 VkImage srcImage,
1028 VkImageLayout srcImageLayout,
1029 VkBuffer destBuffer,
1030 uint32_t regionCount,
1031 const VkBufferImageCopy* pRegions)
1032 {
1033 struct anv_cmd_buffer *cmd_buffer = (struct anv_cmd_buffer *)cmdBuffer;
1034 VkDevice vk_device = (VkDevice) cmd_buffer->device;
1035 struct anv_image *src_image = (struct anv_image *)srcImage;
1036 struct anv_buffer *dest_buffer = (struct anv_buffer *)destBuffer;
1037 struct anv_saved_state saved_state;
1038
1039 meta_prepare_blit(cmd_buffer, &saved_state);
1040
1041 for (unsigned r = 0; r < regionCount; r++) {
1042 struct anv_surface_view src_view;
1043 anv_image_view_init(&src_view, cmd_buffer->device,
1044 &(VkImageViewCreateInfo) {
1045 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
1046 .image = srcImage,
1047 .viewType = VK_IMAGE_VIEW_TYPE_2D,
1048 .format = src_image->format,
1049 .channels = {
1050 VK_CHANNEL_SWIZZLE_R,
1051 VK_CHANNEL_SWIZZLE_G,
1052 VK_CHANNEL_SWIZZLE_B,
1053 VK_CHANNEL_SWIZZLE_A
1054 },
1055 .subresourceRange = {
1056 .aspect = pRegions[r].imageSubresource.aspect,
1057 .baseMipLevel = pRegions[r].imageSubresource.mipLevel,
1058 .mipLevels = 1,
1059 .baseArraySlice = pRegions[r].imageSubresource.arraySlice,
1060 .arraySize = 1
1061 },
1062 .minLod = 0
1063 },
1064 cmd_buffer);
1065
1066 struct anv_image *dest_image;
1067 anv_CreateImage(vk_device,
1068 &(VkImageCreateInfo) {
1069 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
1070 .imageType = VK_IMAGE_TYPE_2D,
1071 .format = src_image->format,
1072 .extent = {
1073 .width = pRegions[r].imageExtent.width,
1074 .height = pRegions[r].imageExtent.height,
1075 .depth = 1,
1076 },
1077 .mipLevels = 1,
1078 .arraySize = 1,
1079 .samples = 1,
1080 .tiling = VK_IMAGE_TILING_LINEAR,
1081 .usage = VK_IMAGE_USAGE_SAMPLED_BIT,
1082 .flags = 0,
1083 }, (VkImage *)&dest_image);
1084
1085 /* We could use a vk call to bind memory, but that would require
1086 * creating a dummy memory object etc. so there's really no point.
1087 */
1088 dest_image->bo = dest_buffer->bo;
1089 dest_image->offset = dest_buffer->offset + pRegions[r].bufferOffset;
1090
1091 struct anv_surface_view dest_view;
1092 anv_color_attachment_view_init(&dest_view, cmd_buffer->device,
1093 &(VkColorAttachmentViewCreateInfo) {
1094 .sType = VK_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
1095 .image = (VkImage)dest_image,
1096 .format = src_image->format,
1097 .mipLevel = 0,
1098 .baseArraySlice = 0,
1099 .arraySize = 1,
1100 },
1101 cmd_buffer);
1102
1103 meta_emit_blit(cmd_buffer,
1104 &src_view,
1105 pRegions[r].imageOffset,
1106 pRegions[r].imageExtent,
1107 &dest_view,
1108 (VkOffset3D) { 0, 0, 0 },
1109 pRegions[r].imageExtent);
1110 }
1111
1112 meta_finish_blit(cmd_buffer, &saved_state);
1113 }
1114
1115 void anv_CmdCloneImageData(
1116 VkCmdBuffer cmdBuffer,
1117 VkImage srcImage,
1118 VkImageLayout srcImageLayout,
1119 VkImage destImage,
1120 VkImageLayout destImageLayout)
1121 {
1122 stub();
1123 }
1124
1125 void anv_CmdUpdateBuffer(
1126 VkCmdBuffer cmdBuffer,
1127 VkBuffer destBuffer,
1128 VkDeviceSize destOffset,
1129 VkDeviceSize dataSize,
1130 const uint32_t* pData)
1131 {
1132 stub();
1133 }
1134
1135 void anv_CmdFillBuffer(
1136 VkCmdBuffer cmdBuffer,
1137 VkBuffer destBuffer,
1138 VkDeviceSize destOffset,
1139 VkDeviceSize fillSize,
1140 uint32_t data)
1141 {
1142 stub();
1143 }
1144
1145 void anv_CmdClearColorImage(
1146 VkCmdBuffer cmdBuffer,
1147 VkImage image,
1148 VkImageLayout imageLayout,
1149 const VkClearColor* color,
1150 uint32_t rangeCount,
1151 const VkImageSubresourceRange* pRanges)
1152 {
1153 stub();
1154 }
1155
1156 void anv_CmdClearDepthStencil(
1157 VkCmdBuffer cmdBuffer,
1158 VkImage image,
1159 VkImageLayout imageLayout,
1160 float depth,
1161 uint32_t stencil,
1162 uint32_t rangeCount,
1163 const VkImageSubresourceRange* pRanges)
1164 {
1165 stub();
1166 }
1167
1168 void anv_CmdResolveImage(
1169 VkCmdBuffer cmdBuffer,
1170 VkImage srcImage,
1171 VkImageLayout srcImageLayout,
1172 VkImage destImage,
1173 VkImageLayout destImageLayout,
1174 uint32_t regionCount,
1175 const VkImageResolve* pRegions)
1176 {
1177 stub();
1178 }
1179
1180 void
1181 anv_device_init_meta(struct anv_device *device)
1182 {
1183 anv_device_init_meta_clear_state(device);
1184 anv_device_init_meta_blit_state(device);
1185 }