radv: Cleanup unused prime blit path.
[mesa.git] / src / amd / vulkan / radv_meta_copy.c
1 /*
2 * Copyright © 2016 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 "radv_meta.h"
25 #include "vk_format.h"
26
27 static VkExtent3D
28 meta_image_block_size(const struct radv_image *image)
29 {
30 const struct vk_format_description *desc = vk_format_description(image->vk_format);
31 return (VkExtent3D) { desc->block.width, desc->block.height, 1 };
32 }
33
34 /* Returns the user-provided VkBufferImageCopy::imageExtent in units of
35 * elements rather than texels. One element equals one texel or one block
36 * if Image is uncompressed or compressed, respectively.
37 */
38 static struct VkExtent3D
39 meta_region_extent_el(const struct radv_image *image,
40 const VkImageType imageType,
41 const struct VkExtent3D *extent)
42 {
43 const VkExtent3D block = meta_image_block_size(image);
44 return radv_sanitize_image_extent(imageType, (VkExtent3D) {
45 .width = DIV_ROUND_UP(extent->width , block.width),
46 .height = DIV_ROUND_UP(extent->height, block.height),
47 .depth = DIV_ROUND_UP(extent->depth , block.depth),
48 });
49 }
50
51 /* Returns the user-provided VkBufferImageCopy::imageOffset in units of
52 * elements rather than texels. One element equals one texel or one block
53 * if Image is uncompressed or compressed, respectively.
54 */
55 static struct VkOffset3D
56 meta_region_offset_el(const struct radv_image *image,
57 const struct VkOffset3D *offset)
58 {
59 const VkExtent3D block = meta_image_block_size(image);
60 return radv_sanitize_image_offset(image->type, (VkOffset3D) {
61 .x = offset->x / block.width,
62 .y = offset->y / block.height,
63 .z = offset->z / block.depth,
64 });
65 }
66
67 static VkFormat
68 vk_format_for_size(int bs)
69 {
70 switch (bs) {
71 case 1: return VK_FORMAT_R8_UINT;
72 case 2: return VK_FORMAT_R8G8_UINT;
73 case 4: return VK_FORMAT_R8G8B8A8_UINT;
74 case 8: return VK_FORMAT_R16G16B16A16_UINT;
75 case 12: return VK_FORMAT_R32G32B32_UINT;
76 case 16: return VK_FORMAT_R32G32B32A32_UINT;
77 default:
78 unreachable("Invalid format block size");
79 }
80 }
81
82 static struct radv_meta_blit2d_surf
83 blit_surf_for_image_level_layer(struct radv_image *image,
84 VkImageLayout layout,
85 const VkImageSubresourceLayers *subres)
86 {
87 VkFormat format = image->vk_format;
88 if (subres->aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT)
89 format = vk_format_depth_only(format);
90 else if (subres->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT)
91 format = vk_format_stencil_only(format);
92
93 if (!radv_image_has_dcc(image) &&
94 !(radv_image_is_tc_compat_htile(image)))
95 format = vk_format_for_size(vk_format_get_blocksize(format));
96
97 format = vk_format_no_srgb(format);
98
99 return (struct radv_meta_blit2d_surf) {
100 .format = format,
101 .bs = vk_format_get_blocksize(format),
102 .level = subres->mipLevel,
103 .layer = subres->baseArrayLayer,
104 .image = image,
105 .aspect_mask = subres->aspectMask,
106 .current_layout = layout,
107 };
108 }
109
110 static void
111 meta_copy_buffer_to_image(struct radv_cmd_buffer *cmd_buffer,
112 struct radv_buffer* buffer,
113 struct radv_image* image,
114 VkImageLayout layout,
115 uint32_t regionCount,
116 const VkBufferImageCopy* pRegions)
117 {
118 bool cs = cmd_buffer->queue_family_index == RADV_QUEUE_COMPUTE;
119 struct radv_meta_saved_state saved_state;
120
121 /* The Vulkan 1.0 spec says "dstImage must have a sample count equal to
122 * VK_SAMPLE_COUNT_1_BIT."
123 */
124 assert(image->info.samples == 1);
125
126 radv_meta_save(&saved_state, cmd_buffer,
127 (cs ? RADV_META_SAVE_COMPUTE_PIPELINE :
128 RADV_META_SAVE_GRAPHICS_PIPELINE) |
129 RADV_META_SAVE_CONSTANTS |
130 RADV_META_SAVE_DESCRIPTORS);
131
132 for (unsigned r = 0; r < regionCount; r++) {
133
134 /**
135 * From the Vulkan 1.0.6 spec: 18.3 Copying Data Between Images
136 * extent is the size in texels of the source image to copy in width,
137 * height and depth. 1D images use only x and width. 2D images use x, y,
138 * width and height. 3D images use x, y, z, width, height and depth.
139 *
140 *
141 * Also, convert the offsets and extent from units of texels to units of
142 * blocks - which is the highest resolution accessible in this command.
143 */
144 const VkOffset3D img_offset_el =
145 meta_region_offset_el(image, &pRegions[r].imageOffset);
146 const VkExtent3D bufferExtent = {
147 .width = pRegions[r].bufferRowLength ?
148 pRegions[r].bufferRowLength : pRegions[r].imageExtent.width,
149 .height = pRegions[r].bufferImageHeight ?
150 pRegions[r].bufferImageHeight : pRegions[r].imageExtent.height,
151 };
152 const VkExtent3D buf_extent_el =
153 meta_region_extent_el(image, image->type, &bufferExtent);
154
155 /* Start creating blit rect */
156 const VkExtent3D img_extent_el =
157 meta_region_extent_el(image, image->type, &pRegions[r].imageExtent);
158 struct radv_meta_blit2d_rect rect = {
159 .width = img_extent_el.width,
160 .height = img_extent_el.height,
161 };
162
163 /* Create blit surfaces */
164 struct radv_meta_blit2d_surf img_bsurf =
165 blit_surf_for_image_level_layer(image,
166 layout,
167 &pRegions[r].imageSubresource);
168
169 struct radv_meta_blit2d_buffer buf_bsurf = {
170 .bs = img_bsurf.bs,
171 .format = img_bsurf.format,
172 .buffer = buffer,
173 .offset = pRegions[r].bufferOffset,
174 .pitch = buf_extent_el.width,
175 };
176
177 if (image->type == VK_IMAGE_TYPE_3D)
178 img_bsurf.layer = img_offset_el.z;
179 /* Loop through each 3D or array slice */
180 unsigned num_slices_3d = img_extent_el.depth;
181 unsigned num_slices_array = pRegions[r].imageSubresource.layerCount;
182 unsigned slice_3d = 0;
183 unsigned slice_array = 0;
184 while (slice_3d < num_slices_3d && slice_array < num_slices_array) {
185
186 rect.dst_x = img_offset_el.x;
187 rect.dst_y = img_offset_el.y;
188
189
190 /* Perform Blit */
191 if (cs)
192 radv_meta_buffer_to_image_cs(cmd_buffer, &buf_bsurf, &img_bsurf, 1, &rect);
193 else
194 radv_meta_blit2d(cmd_buffer, NULL, &buf_bsurf, &img_bsurf, 1, &rect);
195
196 /* Once we've done the blit, all of the actual information about
197 * the image is embedded in the command buffer so we can just
198 * increment the offset directly in the image effectively
199 * re-binding it to different backing memory.
200 */
201 buf_bsurf.offset += buf_extent_el.width *
202 buf_extent_el.height * buf_bsurf.bs;
203 img_bsurf.layer++;
204 if (image->type == VK_IMAGE_TYPE_3D)
205 slice_3d++;
206 else
207 slice_array++;
208 }
209 }
210
211 radv_meta_restore(&saved_state, cmd_buffer);
212 }
213
214 void radv_CmdCopyBufferToImage(
215 VkCommandBuffer commandBuffer,
216 VkBuffer srcBuffer,
217 VkImage destImage,
218 VkImageLayout destImageLayout,
219 uint32_t regionCount,
220 const VkBufferImageCopy* pRegions)
221 {
222 RADV_FROM_HANDLE(radv_cmd_buffer, cmd_buffer, commandBuffer);
223 RADV_FROM_HANDLE(radv_image, dest_image, destImage);
224 RADV_FROM_HANDLE(radv_buffer, src_buffer, srcBuffer);
225
226 meta_copy_buffer_to_image(cmd_buffer, src_buffer, dest_image, destImageLayout,
227 regionCount, pRegions);
228 }
229
230 static void
231 meta_copy_image_to_buffer(struct radv_cmd_buffer *cmd_buffer,
232 struct radv_buffer* buffer,
233 struct radv_image* image,
234 VkImageLayout layout,
235 uint32_t regionCount,
236 const VkBufferImageCopy* pRegions)
237 {
238 struct radv_meta_saved_state saved_state;
239
240 radv_meta_save(&saved_state, cmd_buffer,
241 RADV_META_SAVE_COMPUTE_PIPELINE |
242 RADV_META_SAVE_CONSTANTS |
243 RADV_META_SAVE_DESCRIPTORS);
244
245 for (unsigned r = 0; r < regionCount; r++) {
246
247 /**
248 * From the Vulkan 1.0.6 spec: 18.3 Copying Data Between Images
249 * extent is the size in texels of the source image to copy in width,
250 * height and depth. 1D images use only x and width. 2D images use x, y,
251 * width and height. 3D images use x, y, z, width, height and depth.
252 *
253 *
254 * Also, convert the offsets and extent from units of texels to units of
255 * blocks - which is the highest resolution accessible in this command.
256 */
257 const VkOffset3D img_offset_el =
258 meta_region_offset_el(image, &pRegions[r].imageOffset);
259 const VkExtent3D bufferExtent = {
260 .width = pRegions[r].bufferRowLength ?
261 pRegions[r].bufferRowLength : pRegions[r].imageExtent.width,
262 .height = pRegions[r].bufferImageHeight ?
263 pRegions[r].bufferImageHeight : pRegions[r].imageExtent.height,
264 };
265 const VkExtent3D buf_extent_el =
266 meta_region_extent_el(image, image->type, &bufferExtent);
267
268 /* Start creating blit rect */
269 const VkExtent3D img_extent_el =
270 meta_region_extent_el(image, image->type, &pRegions[r].imageExtent);
271 struct radv_meta_blit2d_rect rect = {
272 .width = img_extent_el.width,
273 .height = img_extent_el.height,
274 };
275
276 /* Create blit surfaces */
277 struct radv_meta_blit2d_surf img_info =
278 blit_surf_for_image_level_layer(image,
279 layout,
280 &pRegions[r].imageSubresource);
281
282 struct radv_meta_blit2d_buffer buf_info = {
283 .bs = img_info.bs,
284 .format = img_info.format,
285 .buffer = buffer,
286 .offset = pRegions[r].bufferOffset,
287 .pitch = buf_extent_el.width,
288 };
289
290 if (image->type == VK_IMAGE_TYPE_3D)
291 img_info.layer = img_offset_el.z;
292 /* Loop through each 3D or array slice */
293 unsigned num_slices_3d = img_extent_el.depth;
294 unsigned num_slices_array = pRegions[r].imageSubresource.layerCount;
295 unsigned slice_3d = 0;
296 unsigned slice_array = 0;
297 while (slice_3d < num_slices_3d && slice_array < num_slices_array) {
298
299 rect.src_x = img_offset_el.x;
300 rect.src_y = img_offset_el.y;
301
302
303 /* Perform Blit */
304 radv_meta_image_to_buffer(cmd_buffer, &img_info, &buf_info, 1, &rect);
305
306 buf_info.offset += buf_extent_el.width *
307 buf_extent_el.height * buf_info.bs;
308 img_info.layer++;
309 if (image->type == VK_IMAGE_TYPE_3D)
310 slice_3d++;
311 else
312 slice_array++;
313 }
314 }
315
316 radv_meta_restore(&saved_state, cmd_buffer);
317 }
318
319 void radv_CmdCopyImageToBuffer(
320 VkCommandBuffer commandBuffer,
321 VkImage srcImage,
322 VkImageLayout srcImageLayout,
323 VkBuffer destBuffer,
324 uint32_t regionCount,
325 const VkBufferImageCopy* pRegions)
326 {
327 RADV_FROM_HANDLE(radv_cmd_buffer, cmd_buffer, commandBuffer);
328 RADV_FROM_HANDLE(radv_image, src_image, srcImage);
329 RADV_FROM_HANDLE(radv_buffer, dst_buffer, destBuffer);
330
331 meta_copy_image_to_buffer(cmd_buffer, dst_buffer, src_image,
332 srcImageLayout,
333 regionCount, pRegions);
334 }
335
336 static void
337 meta_copy_image(struct radv_cmd_buffer *cmd_buffer,
338 struct radv_image *src_image,
339 VkImageLayout src_image_layout,
340 struct radv_image *dest_image,
341 VkImageLayout dest_image_layout,
342 uint32_t regionCount,
343 const VkImageCopy *pRegions)
344 {
345 bool cs = cmd_buffer->queue_family_index == RADV_QUEUE_COMPUTE;
346 struct radv_meta_saved_state saved_state;
347
348 /* From the Vulkan 1.0 spec:
349 *
350 * vkCmdCopyImage can be used to copy image data between multisample
351 * images, but both images must have the same number of samples.
352 */
353 assert(src_image->info.samples == dest_image->info.samples);
354
355 radv_meta_save(&saved_state, cmd_buffer,
356 (cs ? RADV_META_SAVE_COMPUTE_PIPELINE :
357 RADV_META_SAVE_GRAPHICS_PIPELINE) |
358 RADV_META_SAVE_CONSTANTS |
359 RADV_META_SAVE_DESCRIPTORS);
360
361 for (unsigned r = 0; r < regionCount; r++) {
362 assert(pRegions[r].srcSubresource.aspectMask ==
363 pRegions[r].dstSubresource.aspectMask);
364
365 /* Create blit surfaces */
366 struct radv_meta_blit2d_surf b_src =
367 blit_surf_for_image_level_layer(src_image,
368 src_image_layout,
369 &pRegions[r].srcSubresource);
370
371 struct radv_meta_blit2d_surf b_dst =
372 blit_surf_for_image_level_layer(dest_image,
373 dest_image_layout,
374 &pRegions[r].dstSubresource);
375
376 uint32_t dst_queue_mask = radv_image_queue_family_mask(dest_image,
377 cmd_buffer->queue_family_index,
378 cmd_buffer->queue_family_index);
379 bool dst_compressed = radv_layout_dcc_compressed(dest_image, dest_image_layout, dst_queue_mask);
380 uint32_t src_queue_mask = radv_image_queue_family_mask(src_image,
381 cmd_buffer->queue_family_index,
382 cmd_buffer->queue_family_index);
383 bool src_compressed = radv_layout_dcc_compressed(src_image, src_image_layout, src_queue_mask);
384
385 if (!src_compressed || radv_dcc_formats_compatible(b_src.format, b_dst.format)) {
386 b_src.format = b_dst.format;
387 } else if (!dst_compressed) {
388 b_dst.format = b_src.format;
389 } else {
390 radv_decompress_dcc(cmd_buffer, dest_image, &(VkImageSubresourceRange) {
391 .aspectMask = pRegions[r].dstSubresource.aspectMask,
392 .baseMipLevel = pRegions[r].dstSubresource.mipLevel,
393 .levelCount = 1,
394 .baseArrayLayer = pRegions[r].dstSubresource.baseArrayLayer,
395 .layerCount = pRegions[r].dstSubresource.layerCount,
396 });
397 b_dst.format = b_src.format;
398 b_dst.current_layout = VK_IMAGE_LAYOUT_GENERAL;
399 }
400
401
402 /**
403 * From the Vulkan 1.0.6 spec: 18.4 Copying Data Between Buffers and Images
404 * imageExtent is the size in texels of the image to copy in width, height
405 * and depth. 1D images use only x and width. 2D images use x, y, width
406 * and height. 3D images use x, y, z, width, height and depth.
407 *
408 * Also, convert the offsets and extent from units of texels to units of
409 * blocks - which is the highest resolution accessible in this command.
410 */
411 const VkOffset3D dst_offset_el =
412 meta_region_offset_el(dest_image, &pRegions[r].dstOffset);
413 const VkOffset3D src_offset_el =
414 meta_region_offset_el(src_image, &pRegions[r].srcOffset);
415
416 /*
417 * From Vulkan 1.0.68, "Copying Data Between Images":
418 * "When copying between compressed and uncompressed formats
419 * the extent members represent the texel dimensions of the
420 * source image and not the destination."
421 * However, we must use the destination image type to avoid
422 * clamping depth when copying multiple layers of a 2D image to
423 * a 3D image.
424 */
425 const VkExtent3D img_extent_el =
426 meta_region_extent_el(src_image, dest_image->type, &pRegions[r].extent);
427
428 /* Start creating blit rect */
429 struct radv_meta_blit2d_rect rect = {
430 .width = img_extent_el.width,
431 .height = img_extent_el.height,
432 };
433
434 if (src_image->type == VK_IMAGE_TYPE_3D)
435 b_src.layer = src_offset_el.z;
436
437 if (dest_image->type == VK_IMAGE_TYPE_3D)
438 b_dst.layer = dst_offset_el.z;
439
440 /* Loop through each 3D or array slice */
441 unsigned num_slices_3d = img_extent_el.depth;
442 unsigned num_slices_array = pRegions[r].dstSubresource.layerCount;
443 unsigned slice_3d = 0;
444 unsigned slice_array = 0;
445 while (slice_3d < num_slices_3d && slice_array < num_slices_array) {
446
447 /* Finish creating blit rect */
448 rect.dst_x = dst_offset_el.x;
449 rect.dst_y = dst_offset_el.y;
450 rect.src_x = src_offset_el.x;
451 rect.src_y = src_offset_el.y;
452
453 /* Perform Blit */
454 if (cs)
455 radv_meta_image_to_image_cs(cmd_buffer, &b_src, &b_dst, 1, &rect);
456 else
457 radv_meta_blit2d(cmd_buffer, &b_src, NULL, &b_dst, 1, &rect);
458
459 b_src.layer++;
460 b_dst.layer++;
461 if (dest_image->type == VK_IMAGE_TYPE_3D)
462 slice_3d++;
463 else
464 slice_array++;
465 }
466 }
467
468 radv_meta_restore(&saved_state, cmd_buffer);
469 }
470
471 void radv_CmdCopyImage(
472 VkCommandBuffer commandBuffer,
473 VkImage srcImage,
474 VkImageLayout srcImageLayout,
475 VkImage destImage,
476 VkImageLayout destImageLayout,
477 uint32_t regionCount,
478 const VkImageCopy* pRegions)
479 {
480 RADV_FROM_HANDLE(radv_cmd_buffer, cmd_buffer, commandBuffer);
481 RADV_FROM_HANDLE(radv_image, src_image, srcImage);
482 RADV_FROM_HANDLE(radv_image, dest_image, destImage);
483
484 meta_copy_image(cmd_buffer,
485 src_image, srcImageLayout,
486 dest_image, destImageLayout,
487 regionCount, pRegions);
488 }