anv: set maxFragmentDualSrcAttachments to 1
[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 struct VkExtent3D *extent)
41 {
42 const VkExtent3D block = meta_image_block_size(image);
43 return radv_sanitize_image_extent(image->type, (VkExtent3D) {
44 .width = DIV_ROUND_UP(extent->width , block.width),
45 .height = DIV_ROUND_UP(extent->height, block.height),
46 .depth = DIV_ROUND_UP(extent->depth , block.depth),
47 });
48 }
49
50 /* Returns the user-provided VkBufferImageCopy::imageOffset in units of
51 * elements rather than texels. One element equals one texel or one block
52 * if Image is uncompressed or compressed, respectively.
53 */
54 static struct VkOffset3D
55 meta_region_offset_el(const struct radv_image *image,
56 const struct VkOffset3D *offset)
57 {
58 const VkExtent3D block = meta_image_block_size(image);
59 return radv_sanitize_image_offset(image->type, (VkOffset3D) {
60 .x = offset->x / block.width,
61 .y = offset->y / block.height,
62 .z = offset->z / block.depth,
63 });
64 }
65
66 static VkFormat
67 vk_format_for_size(int bs)
68 {
69 switch (bs) {
70 case 1: return VK_FORMAT_R8_UINT;
71 case 2: return VK_FORMAT_R8G8_UINT;
72 case 4: return VK_FORMAT_R8G8B8A8_UINT;
73 case 8: return VK_FORMAT_R16G16B16A16_UINT;
74 case 16: return VK_FORMAT_R32G32B32A32_UINT;
75 default:
76 unreachable("Invalid format block size");
77 }
78 }
79
80 static struct radv_meta_blit2d_surf
81 blit_surf_for_image_level_layer(struct radv_image* image, VkImageAspectFlags aspectMask,
82 int level, int layer)
83 {
84 VkFormat format = image->vk_format;
85 if (aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT)
86 format = vk_format_depth_only(format);
87 else if (aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT)
88 format = vk_format_stencil_only(format);
89
90 if (!image->surface.dcc_size)
91 format = vk_format_for_size(vk_format_get_blocksize(format));
92
93 return (struct radv_meta_blit2d_surf) {
94 .format = format,
95 .bs = vk_format_get_blocksize(format),
96 .level = level,
97 .layer = layer,
98 .image = image,
99 .aspect_mask = aspectMask,
100 };
101 }
102
103 static void
104 meta_copy_buffer_to_image(struct radv_cmd_buffer *cmd_buffer,
105 struct radv_buffer* buffer,
106 struct radv_image* image,
107 uint32_t regionCount,
108 const VkBufferImageCopy* pRegions)
109 {
110 struct radv_meta_saved_state saved_state;
111
112 /* The Vulkan 1.0 spec says "dstImage must have a sample count equal to
113 * VK_SAMPLE_COUNT_1_BIT."
114 */
115 assert(image->samples == 1);
116
117 radv_meta_save_graphics_reset_vport_scissor(&saved_state, cmd_buffer);
118
119 for (unsigned r = 0; r < regionCount; r++) {
120
121 /**
122 * From the Vulkan 1.0.6 spec: 18.3 Copying Data Between Images
123 * extent is the size in texels of the source image to copy in width,
124 * height and depth. 1D images use only x and width. 2D images use x, y,
125 * width and height. 3D images use x, y, z, width, height and depth.
126 *
127 *
128 * Also, convert the offsets and extent from units of texels to units of
129 * blocks - which is the highest resolution accessible in this command.
130 */
131 const VkOffset3D img_offset_el =
132 meta_region_offset_el(image, &pRegions[r].imageOffset);
133 const VkExtent3D bufferExtent = {
134 .width = pRegions[r].bufferRowLength ?
135 pRegions[r].bufferRowLength : pRegions[r].imageExtent.width,
136 .height = pRegions[r].bufferImageHeight ?
137 pRegions[r].bufferImageHeight : pRegions[r].imageExtent.height,
138 };
139 const VkExtent3D buf_extent_el =
140 meta_region_extent_el(image, &bufferExtent);
141
142 /* Start creating blit rect */
143 const VkExtent3D img_extent_el =
144 meta_region_extent_el(image, &pRegions[r].imageExtent);
145 struct radv_meta_blit2d_rect rect = {
146 .width = img_extent_el.width,
147 .height = img_extent_el.height,
148 };
149
150 /* Create blit surfaces */
151 struct radv_meta_blit2d_surf img_bsurf =
152 blit_surf_for_image_level_layer(image,
153 pRegions[r].imageSubresource.aspectMask,
154 pRegions[r].imageSubresource.mipLevel,
155 pRegions[r].imageSubresource.baseArrayLayer);
156
157 struct radv_meta_blit2d_buffer buf_bsurf = {
158 .bs = img_bsurf.bs,
159 .format = img_bsurf.format,
160 .buffer = buffer,
161 .offset = pRegions[r].bufferOffset,
162 .pitch = buf_extent_el.width,
163 };
164
165 /* Loop through each 3D or array slice */
166 unsigned num_slices_3d = img_extent_el.depth;
167 unsigned num_slices_array = pRegions[r].imageSubresource.layerCount;
168 unsigned slice_3d = 0;
169 unsigned slice_array = 0;
170 while (slice_3d < num_slices_3d && slice_array < num_slices_array) {
171
172 rect.dst_x = img_offset_el.x;
173 rect.dst_y = img_offset_el.y;
174
175
176 /* Perform Blit */
177 radv_meta_blit2d(cmd_buffer, NULL, &buf_bsurf, &img_bsurf, 1, &rect);
178
179 /* Once we've done the blit, all of the actual information about
180 * the image is embedded in the command buffer so we can just
181 * increment the offset directly in the image effectively
182 * re-binding it to different backing memory.
183 */
184 buf_bsurf.offset += buf_extent_el.width *
185 buf_extent_el.height * buf_bsurf.bs;
186 img_bsurf.layer++;
187 if (image->type == VK_IMAGE_TYPE_3D)
188 slice_3d++;
189 else
190 slice_array++;
191 }
192 }
193 radv_meta_restore(&saved_state, cmd_buffer);
194 }
195
196 void radv_CmdCopyBufferToImage(
197 VkCommandBuffer commandBuffer,
198 VkBuffer srcBuffer,
199 VkImage destImage,
200 VkImageLayout destImageLayout,
201 uint32_t regionCount,
202 const VkBufferImageCopy* pRegions)
203 {
204 RADV_FROM_HANDLE(radv_cmd_buffer, cmd_buffer, commandBuffer);
205 RADV_FROM_HANDLE(radv_image, dest_image, destImage);
206 RADV_FROM_HANDLE(radv_buffer, src_buffer, srcBuffer);
207
208 meta_copy_buffer_to_image(cmd_buffer, src_buffer, dest_image,
209 regionCount, pRegions);
210 }
211
212 static void
213 meta_copy_image_to_buffer(struct radv_cmd_buffer *cmd_buffer,
214 struct radv_buffer* buffer,
215 struct radv_image* image,
216 uint32_t regionCount,
217 const VkBufferImageCopy* pRegions)
218 {
219 struct radv_meta_saved_compute_state saved_state;
220
221 radv_meta_begin_bufimage(cmd_buffer, &saved_state);
222 for (unsigned r = 0; r < regionCount; r++) {
223
224 /**
225 * From the Vulkan 1.0.6 spec: 18.3 Copying Data Between Images
226 * extent is the size in texels of the source image to copy in width,
227 * height and depth. 1D images use only x and width. 2D images use x, y,
228 * width and height. 3D images use x, y, z, width, height and depth.
229 *
230 *
231 * Also, convert the offsets and extent from units of texels to units of
232 * blocks - which is the highest resolution accessible in this command.
233 */
234 const VkOffset3D img_offset_el =
235 meta_region_offset_el(image, &pRegions[r].imageOffset);
236 const VkExtent3D bufferExtent = {
237 .width = pRegions[r].bufferRowLength ?
238 pRegions[r].bufferRowLength : pRegions[r].imageExtent.width,
239 .height = pRegions[r].bufferImageHeight ?
240 pRegions[r].bufferImageHeight : pRegions[r].imageExtent.height,
241 };
242 const VkExtent3D buf_extent_el =
243 meta_region_extent_el(image, &bufferExtent);
244
245 /* Start creating blit rect */
246 const VkExtent3D img_extent_el =
247 meta_region_extent_el(image, &pRegions[r].imageExtent);
248 struct radv_meta_blit2d_rect rect = {
249 .width = img_extent_el.width,
250 .height = img_extent_el.height,
251 };
252
253 /* Create blit surfaces */
254 struct radv_meta_blit2d_surf img_info =
255 blit_surf_for_image_level_layer(image,
256 pRegions[r].imageSubresource.aspectMask,
257 pRegions[r].imageSubresource.mipLevel,
258 pRegions[r].imageSubresource.baseArrayLayer);
259 struct radv_meta_blit2d_buffer buf_info = {
260 .bs = img_info.bs,
261 .format = img_info.format,
262 .buffer = buffer,
263 .offset = pRegions[r].bufferOffset,
264 .pitch = buf_extent_el.width,
265 };
266
267 /* Loop through each 3D or array slice */
268 unsigned num_slices_3d = img_extent_el.depth;
269 unsigned num_slices_array = pRegions[r].imageSubresource.layerCount;
270 unsigned slice_3d = 0;
271 unsigned slice_array = 0;
272 while (slice_3d < num_slices_3d && slice_array < num_slices_array) {
273
274 rect.src_x = img_offset_el.x;
275 rect.src_y = img_offset_el.y;
276
277
278 /* Perform Blit */
279 radv_meta_image_to_buffer(cmd_buffer, &img_info, &buf_info, 1, &rect);
280
281 buf_info.offset += buf_extent_el.width *
282 buf_extent_el.height * buf_info.bs;
283 img_info.layer++;
284 if (image->type == VK_IMAGE_TYPE_3D)
285 slice_3d++;
286 else
287 slice_array++;
288 }
289 }
290 radv_meta_end_bufimage(cmd_buffer, &saved_state);
291 }
292
293 void radv_CmdCopyImageToBuffer(
294 VkCommandBuffer commandBuffer,
295 VkImage srcImage,
296 VkImageLayout srcImageLayout,
297 VkBuffer destBuffer,
298 uint32_t regionCount,
299 const VkBufferImageCopy* pRegions)
300 {
301 RADV_FROM_HANDLE(radv_cmd_buffer, cmd_buffer, commandBuffer);
302 RADV_FROM_HANDLE(radv_image, src_image, srcImage);
303 RADV_FROM_HANDLE(radv_buffer, dst_buffer, destBuffer);
304
305 meta_copy_image_to_buffer(cmd_buffer, dst_buffer, src_image,
306 regionCount, pRegions);
307 }
308
309 void radv_CmdCopyImage(
310 VkCommandBuffer commandBuffer,
311 VkImage srcImage,
312 VkImageLayout srcImageLayout,
313 VkImage destImage,
314 VkImageLayout destImageLayout,
315 uint32_t regionCount,
316 const VkImageCopy* pRegions)
317 {
318 RADV_FROM_HANDLE(radv_cmd_buffer, cmd_buffer, commandBuffer);
319 RADV_FROM_HANDLE(radv_image, src_image, srcImage);
320 RADV_FROM_HANDLE(radv_image, dest_image, destImage);
321 struct radv_meta_saved_state saved_state;
322
323 /* From the Vulkan 1.0 spec:
324 *
325 * vkCmdCopyImage can be used to copy image data between multisample
326 * images, but both images must have the same number of samples.
327 */
328 assert(src_image->samples == dest_image->samples);
329
330 radv_meta_save_graphics_reset_vport_scissor(&saved_state, cmd_buffer);
331
332 for (unsigned r = 0; r < regionCount; r++) {
333 assert(pRegions[r].srcSubresource.aspectMask ==
334 pRegions[r].dstSubresource.aspectMask);
335
336 /* Create blit surfaces */
337 struct radv_meta_blit2d_surf b_src =
338 blit_surf_for_image_level_layer(src_image,
339 pRegions[r].srcSubresource.aspectMask,
340 pRegions[r].srcSubresource.mipLevel,
341 pRegions[r].srcSubresource.baseArrayLayer);
342 struct radv_meta_blit2d_surf b_dst =
343 blit_surf_for_image_level_layer(dest_image,
344 pRegions[r].dstSubresource.aspectMask,
345 pRegions[r].dstSubresource.mipLevel,
346 pRegions[r].dstSubresource.baseArrayLayer);
347
348 /* for DCC */
349 b_src.format = b_dst.format;
350
351 /**
352 * From the Vulkan 1.0.6 spec: 18.4 Copying Data Between Buffers and Images
353 * imageExtent is the size in texels of the image to copy in width, height
354 * and depth. 1D images use only x and width. 2D images use x, y, width
355 * and height. 3D images use x, y, z, width, height and depth.
356 *
357 * Also, convert the offsets and extent from units of texels to units of
358 * blocks - which is the highest resolution accessible in this command.
359 */
360 const VkOffset3D dst_offset_el =
361 meta_region_offset_el(dest_image, &pRegions[r].dstOffset);
362 const VkOffset3D src_offset_el =
363 meta_region_offset_el(src_image, &pRegions[r].srcOffset);
364 const VkExtent3D img_extent_el =
365 meta_region_extent_el(src_image, &pRegions[r].extent);
366
367 /* Start creating blit rect */
368 struct radv_meta_blit2d_rect rect = {
369 .width = img_extent_el.width,
370 .height = img_extent_el.height,
371 };
372
373 /* Loop through each 3D or array slice */
374 unsigned num_slices_3d = img_extent_el.depth;
375 unsigned num_slices_array = pRegions[r].dstSubresource.layerCount;
376 unsigned slice_3d = 0;
377 unsigned slice_array = 0;
378 while (slice_3d < num_slices_3d && slice_array < num_slices_array) {
379
380 /* Finish creating blit rect */
381 rect.dst_x = dst_offset_el.x;
382 rect.dst_y = dst_offset_el.y;
383 rect.src_x = src_offset_el.x;
384 rect.src_y = src_offset_el.y;
385
386 /* Perform Blit */
387 radv_meta_blit2d(cmd_buffer, &b_src, NULL, &b_dst, 1, &rect);
388
389 b_src.layer++;
390 b_dst.layer++;
391 if (dest_image->type == VK_IMAGE_TYPE_3D)
392 slice_3d++;
393 else
394 slice_array++;
395 }
396 }
397
398 radv_meta_restore(&saved_state, cmd_buffer);
399 }