1 /*
2 * Copyright © 2019 Raspberry Pi Ltd
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 "v3dv_private.h"
25
26 #include "drm-uapi/drm_fourcc.h"
27 #include "util/format/u_format.h"
28 #include "util/u_math.h"
29 #include "vk_util.h"
30 #include "vulkan/wsi/wsi_common.h"
31 #include "vk_android.h"
32
33 /**
34 * Computes the HW's UIFblock padding for a given height/cpp.
35 *
36 * The goal of the padding is to keep pages of the same color (bank number) at
37 * least half a page away from each other vertically when crossing between
38 * columns of UIF blocks.
39 */
40 static uint32_t
v3d_get_ub_pad(uint32_t cpp,uint32_t height)41 v3d_get_ub_pad(uint32_t cpp, uint32_t height)
42 {
43 uint32_t utile_h = v3d_utile_height(cpp);
44 uint32_t uif_block_h = utile_h * 2;
45 uint32_t height_ub = height / uif_block_h;
46
47 uint32_t height_offset_in_pc = height_ub % PAGE_CACHE_UB_ROWS;
48
49 /* For the perfectly-aligned-for-UIF-XOR case, don't add any pad. */
50 if (height_offset_in_pc == 0)
51 return 0;
52
53 /* Try padding up to where we're offset by at least half a page. */
54 if (height_offset_in_pc < PAGE_UB_ROWS_TIMES_1_5) {
55 /* If we fit entirely in the page cache, don't pad. */
56 if (height_ub < PAGE_CACHE_UB_ROWS)
57 return 0;
58 else
59 return PAGE_UB_ROWS_TIMES_1_5 - height_offset_in_pc;
60 }
61
62 /* If we're close to being aligned to page cache size, then round up
63 * and rely on XOR.
64 */
65 if (height_offset_in_pc > PAGE_CACHE_MINUS_1_5_UB_ROWS)
66 return PAGE_CACHE_UB_ROWS - height_offset_in_pc;
67
68 /* Otherwise, we're far enough away (top and bottom) to not need any
69 * padding.
70 */
71 return 0;
72 }
73
74 /**
75 * Computes the dimension with required padding for mip levels.
76 *
77 * This padding is required for width and height dimensions when the mip
78 * level is greater than 1, and for the depth dimension when the mip level
79 * is greater than 0. This function expects to be passed a mip level >= 1.
80 *
81 * Note: Hardware documentation seems to suggest that the third argument
82 * should be the utile dimensions, but through testing it was found that
83 * the block dimension should be used instead.
84 */
85 static uint32_t
v3d_get_dimension_mpad(uint32_t dimension,uint32_t level,uint32_t block_dimension)86 v3d_get_dimension_mpad(uint32_t dimension, uint32_t level, uint32_t block_dimension)
87 {
88 assert(level >= 1);
89 uint32_t pot_dim = u_minify(dimension, 1);
90 pot_dim = util_next_power_of_two(DIV_ROUND_UP(pot_dim, block_dimension));
91 uint32_t padded_dim = block_dimension * pot_dim;
92 return u_minify(padded_dim, level - 1);
93 }
94
95 static bool
v3d_setup_plane_slices(struct v3dv_image * image,uint8_t plane,uint32_t plane_offset,const VkSubresourceLayout * plane_layouts)96 v3d_setup_plane_slices(struct v3dv_image *image, uint8_t plane,
97 uint32_t plane_offset,
98 const VkSubresourceLayout *plane_layouts)
99 {
100 assert(image->planes[plane].cpp > 0);
101
102 uint32_t width = image->planes[plane].width;
103 uint32_t height = image->planes[plane].height;
104 uint32_t depth = image->vk.extent.depth;
105
106 uint32_t utile_w = v3d_utile_width(image->planes[plane].cpp);
107 uint32_t utile_h = v3d_utile_height(image->planes[plane].cpp);
108 uint32_t uif_block_w = utile_w * 2;
109 uint32_t uif_block_h = utile_h * 2;
110
111 uint32_t block_width = vk_format_get_blockwidth(image->vk.format);
112 uint32_t block_height = vk_format_get_blockheight(image->vk.format);
113
114 /* Note that power-of-two padding is based on level 1. These are not
115 * equivalent to just util_next_power_of_two(dimension), because at a
116 * level 0 dimension of 9, the level 1 power-of-two padded value is 4,
117 * not 8. Additionally the pot padding is based on the block size.
118 */
119 uint32_t pot_width = 2 * v3d_get_dimension_mpad(width,
120 1,
121 block_width);
122 uint32_t pot_height = 2 * v3d_get_dimension_mpad(height,
123 1,
124 block_height);
125 uint32_t pot_depth = 2 * v3d_get_dimension_mpad(depth,
126 1,
127 1);
128
129 assert(image->vk.samples == VK_SAMPLE_COUNT_1_BIT ||
130 image->vk.samples == VK_SAMPLE_COUNT_4_BIT);
131 bool msaa = image->vk.samples != VK_SAMPLE_COUNT_1_BIT;
132
133 bool uif_top = msaa;
134
135 assert(image->vk.array_layers > 0);
136 assert(depth > 0);
137 assert(image->vk.mip_levels >= 1);
138
139 /* Texture Base Address needs to be 64-byte aligned. If we have an explicit
140 * plane layout we will return false to fail image creation with appropriate
141 * error code.
142 */
143 uint32_t offset;
144 if (plane_layouts) {
145 offset = plane_layouts[plane].offset;
146 if (offset % 64 != 0)
147 return false;
148 } else {
149 offset = plane_offset;
150 }
151 assert(plane_offset % 64 == 0);
152
153 for (int32_t i = image->vk.mip_levels - 1; i >= 0; i--) {
154 struct v3d_resource_slice *slice = &image->planes[plane].slices[i];
155
156 slice->width = u_minify(width, i);
157 slice->height = u_minify(height, i);
158
159 uint32_t level_width, level_height, level_depth;
160 if (i < 2) {
161 level_width = slice->width;
162 level_height = slice->height;
163 } else {
164 level_width = u_minify(pot_width, i);
165 level_height = u_minify(pot_height, i);
166 }
167
168 if (i < 1)
169 level_depth = u_minify(depth, i);
170 else
171 level_depth = u_minify(pot_depth, i);
172
173 if (msaa) {
174 level_width *= 2;
175 level_height *= 2;
176 }
177
178 level_width = DIV_ROUND_UP(level_width, block_width);
179 level_height = DIV_ROUND_UP(level_height, block_height);
180
181 if (!image->tiled) {
182 slice->tiling = V3D_TILING_RASTER;
183 if (image->vk.image_type == VK_IMAGE_TYPE_1D)
184 level_width = align(level_width, 64 / image->planes[plane].cpp);
185 } else {
186 if ((i != 0 || !uif_top) &&
187 (level_width <= utile_w || level_height <= utile_h)) {
188 slice->tiling = V3D_TILING_LINEARTILE;
189 level_width = align(level_width, utile_w);
190 level_height = align(level_height, utile_h);
191 } else if ((i != 0 || !uif_top) && level_width <= uif_block_w) {
192 slice->tiling = V3D_TILING_UBLINEAR_1_COLUMN;
193 level_width = align(level_width, uif_block_w);
194 level_height = align(level_height, uif_block_h);
195 } else if ((i != 0 || !uif_top) && level_width <= 2 * uif_block_w) {
196 slice->tiling = V3D_TILING_UBLINEAR_2_COLUMN;
197 level_width = align(level_width, 2 * uif_block_w);
198 level_height = align(level_height, uif_block_h);
199 } else {
200 /* We align the width to a 4-block column of UIF blocks, but we
201 * only align height to UIF blocks.
202 */
203 level_width = align(level_width, 4 * uif_block_w);
204 level_height = align(level_height, uif_block_h);
205
206 slice->ub_pad = v3d_get_ub_pad(image->planes[plane].cpp,
207 level_height);
208 level_height += slice->ub_pad * uif_block_h;
209
210 /* If the padding set us to to be aligned to the page cache size,
211 * then the HW will use the XOR bit on odd columns to get us
212 * perfectly misaligned.
213 */
214 if ((level_height / uif_block_h) %
215 (V3D_PAGE_CACHE_SIZE / V3D_UIFBLOCK_ROW_SIZE) == 0) {
216 slice->tiling = V3D_TILING_UIF_XOR;
217 } else {
218 slice->tiling = V3D_TILING_UIF_NO_XOR;
219 }
220 }
221 }
222
223 slice->offset = offset;
224 slice->stride = level_width * image->planes[plane].cpp;
225
226 /* We assume that rowPitch in the plane layout refers to level 0 */
227 if (plane_layouts && i == 0) {
228 if (plane_layouts[plane].rowPitch < slice->stride)
229 return false;
230 if (plane_layouts[plane].rowPitch % image->planes[plane].cpp)
231 return false;
232 if (image->tiled && (plane_layouts[plane].rowPitch % (4 * uif_block_w)))
233 return false;
234 slice->stride = plane_layouts[plane].rowPitch;
235 }
236
237 slice->padded_height = level_height;
238 if (slice->tiling == V3D_TILING_UIF_NO_XOR ||
239 slice->tiling == V3D_TILING_UIF_XOR) {
240 slice->padded_height_of_output_image_in_uif_blocks =
241 slice->padded_height /
242 (2 * v3d_utile_height(image->planes[plane].cpp));
243 }
244
245 slice->size = level_height * slice->stride;
246 uint32_t slice_total_size = slice->size * level_depth;
247
248 /* The HW aligns level 1's base to a page if any of level 1 or
249 * below could be UIF XOR. The lower levels then inherit the
250 * alignment for as long as necessary, thanks to being power of
251 * two aligned.
252 */
253 if (i == 1 &&
254 level_width > 4 * uif_block_w &&
255 level_height > PAGE_CACHE_MINUS_1_5_UB_ROWS * uif_block_h) {
256 slice_total_size = align(slice_total_size, V3D_UIFCFG_PAGE_SIZE);
257 }
258
259 offset += slice_total_size;
260 }
261
262 image->planes[plane].size = offset - plane_offset;
263
264 /* UIF/UBLINEAR levels need to be aligned to UIF-blocks, and LT only
265 * needs to be aligned to utile boundaries. Since tiles are laid out
266 * from small to big in memory, we need to align the later UIF slices
267 * to UIF blocks, if they were preceded by non-UIF-block-aligned LT
268 * slices.
269 *
270 * We additionally align to 4k, which improves UIF XOR performance.
271 *
272 * Finally, because the Texture Base Address field must be 64-byte aligned,
273 * we also need to align linear images to 64 if the image is going to be
274 * used for transfer.
275 */
276 if (image->tiled) {
277 image->planes[plane].alignment = 4096;
278 } else {
279 image->planes[plane].alignment =
280 (image->vk.usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) ?
281 64 : image->planes[plane].cpp;
282 }
283
284 uint32_t align_offset =
285 align(image->planes[plane].slices[0].offset,
286 image->planes[plane].alignment) -
287 image->planes[plane].slices[0].offset;
288 if (align_offset) {
289 image->planes[plane].size += align_offset;
290 for (int i = 0; i < image->vk.mip_levels; i++)
291 image->planes[plane].slices[i].offset += align_offset;
292 }
293
294 /* Arrays and cube textures have a stride which is the distance from
295 * one full mipmap tree to the next (64b aligned). For 3D textures,
296 * we need to program the stride between slices of miplevel 0.
297 */
298 if (image->vk.image_type != VK_IMAGE_TYPE_3D) {
299 image->planes[plane].cube_map_stride =
300 align(image->planes[plane].slices[0].offset +
301 image->planes[plane].slices[0].size, 64);
302
303 if (plane_layouts && image->vk.array_layers > 1) {
304 if (plane_layouts[plane].arrayPitch % 64 != 0)
305 return false;
306 if (plane_layouts[plane].arrayPitch <
307 image->planes[plane].cube_map_stride) {
308 return false;
309 }
310 image->planes[plane].cube_map_stride = plane_layouts[plane].arrayPitch;
311 }
312
313 image->planes[plane].size += image->planes[plane].cube_map_stride *
314 (image->vk.array_layers - 1);
315 } else {
316 image->planes[plane].cube_map_stride = image->planes[plane].slices[0].size;
317 if (plane_layouts) {
318 /* We assume that depthPitch in the plane layout refers to level 0 */
319 if (plane_layouts[plane].depthPitch !=
320 image->planes[plane].slices[0].size) {
321 return false;
322 }
323 }
324 }
325
326 return true;
327 }
328
329 static VkResult
v3d_setup_slices(struct v3dv_image * image,bool disjoint,const VkSubresourceLayout * plane_layouts)330 v3d_setup_slices(struct v3dv_image *image, bool disjoint,
331 const VkSubresourceLayout *plane_layouts)
332 {
333 if (disjoint && image->plane_count == 1)
334 disjoint = false;
335
336 uint64_t offset = 0;
337 for (uint8_t plane = 0; plane < image->plane_count; plane++) {
338 offset = disjoint ? 0 : offset;
339 if (!v3d_setup_plane_slices(image, plane, offset, plane_layouts)) {
340 assert(plane_layouts);
341 return VK_ERROR_INVALID_DRM_FORMAT_MODIFIER_PLANE_LAYOUT_EXT;
342 }
343 offset += align64(image->planes[plane].size, 64);
344 }
345
346 /* From the Vulkan spec:
347 *
348 * "If the size of the resultant image would exceed maxResourceSize, then
349 * vkCreateImage must fail and return VK_ERROR_OUT_OF_DEVICE_MEMORY. This
350 * failure may occur even when all image creation parameters satisfy their
351 * valid usage requirements."
352 */
353 if (offset > 0xffffffff)
354 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
355
356 image->non_disjoint_size = disjoint ? 0 : offset;
357 return VK_SUCCESS;
358 }
359
360 uint32_t
v3dv_layer_offset(const struct v3dv_image * image,uint32_t level,uint32_t layer,uint8_t plane)361 v3dv_layer_offset(const struct v3dv_image *image, uint32_t level, uint32_t layer,
362 uint8_t plane)
363 {
364 const struct v3d_resource_slice *slice = &image->planes[plane].slices[level];
365
366 if (image->vk.image_type == VK_IMAGE_TYPE_3D)
367 return image->planes[plane].mem_offset + slice->offset + layer * slice->size;
368 else
369 return image->planes[plane].mem_offset + slice->offset +
370 layer * image->planes[plane].cube_map_stride;
371 }
372
373 VkResult
v3dv_update_image_layout(struct v3dv_device * device,struct v3dv_image * image,uint64_t modifier,bool disjoint,const VkImageDrmFormatModifierExplicitCreateInfoEXT * explicit_mod_info)374 v3dv_update_image_layout(struct v3dv_device *device,
375 struct v3dv_image *image,
376 uint64_t modifier,
377 bool disjoint,
378 const VkImageDrmFormatModifierExplicitCreateInfoEXT *explicit_mod_info)
379 {
380 assert(!explicit_mod_info ||
381 image->plane_count == explicit_mod_info->drmFormatModifierPlaneCount);
382
383 assert(!explicit_mod_info ||
384 modifier == explicit_mod_info->drmFormatModifier);
385
386 image->tiled = modifier != DRM_FORMAT_MOD_LINEAR;
387
388 image->vk.drm_format_mod = modifier;
389
390 return v3d_setup_slices(image, disjoint,
391 explicit_mod_info ? explicit_mod_info->pPlaneLayouts :
392 NULL);
393 }
394
395 VkResult
v3dv_image_init(struct v3dv_device * device,const VkImageCreateInfo * pCreateInfo,const VkAllocationCallbacks * pAllocator,struct v3dv_image * image)396 v3dv_image_init(struct v3dv_device *device,
397 const VkImageCreateInfo *pCreateInfo,
398 const VkAllocationCallbacks *pAllocator,
399 struct v3dv_image *image)
400 {
401 /* When using the simulator the WSI common code will see that our
402 * driver wsi device doesn't match the display device and because of that
403 * it will not attempt to present directly from the swapchain images,
404 * instead it will use the prime blit path (use_buffer_blit flag in
405 * struct wsi_swapchain), where it copies the contents of the swapchain
406 * images to a linear buffer with appropriate row stride for presentation.
407 * As a result, on that path, swapchain images do not have any special
408 * requirements and are not created with the pNext structs below.
409 */
410 VkImageTiling tiling = pCreateInfo->tiling;
411 uint64_t modifier = DRM_FORMAT_MOD_INVALID;
412 const VkImageDrmFormatModifierListCreateInfoEXT *mod_info = NULL;
413 const VkImageDrmFormatModifierExplicitCreateInfoEXT *explicit_mod_info = NULL;
414
415 /* This section is removed by the optimizer for non-ANDROID builds */
416 VkImageDrmFormatModifierExplicitCreateInfoEXT eci;
417 VkSubresourceLayout a_plane_layouts[V3DV_MAX_PLANE_COUNT];
418 if (vk_image_is_android_native_buffer(&image->vk)) {
419 VkResult result = vk_android_get_anb_layout(
420 pCreateInfo, &eci, a_plane_layouts, V3DV_MAX_PLANE_COUNT);
421 if (result != VK_SUCCESS)
422 return result;
423
424 explicit_mod_info = &eci;
425 modifier = eci.drmFormatModifier;
426 }
427
428 if (tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT) {
429 mod_info =
430 vk_find_struct_const(pCreateInfo->pNext,
431 IMAGE_DRM_FORMAT_MODIFIER_LIST_CREATE_INFO_EXT);
432 explicit_mod_info =
433 vk_find_struct_const(pCreateInfo->pNext,
434 IMAGE_DRM_FORMAT_MODIFIER_EXPLICIT_CREATE_INFO_EXT);
435 assert(mod_info || explicit_mod_info);
436
437 if (mod_info) {
438 for (uint32_t i = 0; i < mod_info->drmFormatModifierCount; i++) {
439 switch (mod_info->pDrmFormatModifiers[i]) {
440 case DRM_FORMAT_MOD_LINEAR:
441 if (modifier == DRM_FORMAT_MOD_INVALID)
442 modifier = DRM_FORMAT_MOD_LINEAR;
443 break;
444 case DRM_FORMAT_MOD_BROADCOM_UIF:
445 modifier = DRM_FORMAT_MOD_BROADCOM_UIF;
446 break;
447 }
448 }
449 } else {
450 modifier = explicit_mod_info->drmFormatModifier;
451 }
452 assert(modifier == DRM_FORMAT_MOD_LINEAR ||
453 modifier == DRM_FORMAT_MOD_BROADCOM_UIF);
454 } else if (pCreateInfo->imageType == VK_IMAGE_TYPE_1D ||
455 image->vk.wsi_legacy_scanout) {
456 tiling = VK_IMAGE_TILING_LINEAR;
457 }
458
459 if (modifier == DRM_FORMAT_MOD_INVALID)
460 modifier = (tiling == VK_IMAGE_TILING_OPTIMAL) ? DRM_FORMAT_MOD_BROADCOM_UIF
461 : DRM_FORMAT_MOD_LINEAR;
462
463 const struct v3dv_format *format =
464 v3dv_X(device, get_format)(image->vk.format);
465 v3dv_assert(format != NULL && format->plane_count);
466
467 assert(pCreateInfo->samples == VK_SAMPLE_COUNT_1_BIT ||
468 pCreateInfo->samples == VK_SAMPLE_COUNT_4_BIT);
469
470 image->format = format;
471
472 image->plane_count = vk_format_get_plane_count(image->vk.format);
473
474 const struct vk_format_ycbcr_info *ycbcr_info =
475 vk_format_get_ycbcr_info(image->vk.format);
476
477 for (uint8_t plane = 0; plane < image->plane_count; plane++) {
478 VkFormat plane_format =
479 vk_format_get_plane_format(image->vk.format, plane);
480 image->planes[plane].cpp =
481 vk_format_get_blocksize(plane_format);
482 image->planes[plane].vk_format = plane_format;
483
484 image->planes[plane].width = image->vk.extent.width;
485 image->planes[plane].height = image->vk.extent.height;
486
487 if (ycbcr_info) {
488 image->planes[plane].width /=
489 ycbcr_info->planes[plane].denominator_scales[0];
490
491 image->planes[plane].height /=
492 ycbcr_info->planes[plane].denominator_scales[1];
493 }
494 }
495
496 /* Our meta paths can create image views with compatible formats for any
497 * image, so always set this flag to keep the common Vulkan image code
498 * happy.
499 */
500 image->vk.create_flags |= VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT;
501
502 /* At this time, an AHB handle is not yet provided.
503 * Image layout will be filled up during vkBindImageMemory2
504 * This section is removed by the optimizer for non-ANDROID builds
505 */
506 if (vk_image_is_android_hardware_buffer(&image->vk))
507 return VK_SUCCESS;
508
509 bool disjoint = image->vk.create_flags & VK_IMAGE_CREATE_DISJOINT_BIT;
510
511 return v3dv_update_image_layout(device, image, modifier, disjoint,
512 explicit_mod_info);
513 }
514
515 static VkResult
516 create_image_from_swapchain(struct v3dv_device *device,
517 const VkImageCreateInfo *pCreateInfo,
518 const VkImageSwapchainCreateInfoKHR *swapchain_info,
519 const VkAllocationCallbacks *pAllocator,
520 VkImage *pImage);
521
522 static VkResult
create_image(struct v3dv_device * device,const VkImageCreateInfo * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkImage * pImage)523 create_image(struct v3dv_device *device,
524 const VkImageCreateInfo *pCreateInfo,
525 const VkAllocationCallbacks *pAllocator,
526 VkImage *pImage)
527 {
528 #if DETECT_OS_ANDROID
529 /* VkImageSwapchainCreateInfoKHR is not useful at all */
530 const VkImageSwapchainCreateInfoKHR *swapchain_info = NULL;
531 #else
532 const VkImageSwapchainCreateInfoKHR *swapchain_info =
533 vk_find_struct_const(pCreateInfo->pNext, IMAGE_SWAPCHAIN_CREATE_INFO_KHR);
534 #endif
535
536 if (swapchain_info && swapchain_info->swapchain != VK_NULL_HANDLE)
537 return create_image_from_swapchain(device, pCreateInfo, swapchain_info,
538 pAllocator, pImage);
539
540 VkResult result;
541 struct v3dv_image *image = NULL;
542
543 image = vk_image_create(&device->vk, pCreateInfo, pAllocator, sizeof(*image));
544 if (image == NULL)
545 return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
546
547 result = v3dv_image_init(device, pCreateInfo, pAllocator, image);
548 if (result != VK_SUCCESS)
549 goto fail;
550
551 /* This section is removed by the optimizer for non-ANDROID builds */
552 if (vk_image_is_android_native_buffer(&image->vk)) {
553 result = vk_android_import_anb(&device->vk, pCreateInfo, pAllocator,
554 &image->vk);
555 if (result != VK_SUCCESS)
556 goto fail;
557 }
558
559 *pImage = v3dv_image_to_handle(image);
560
561 return VK_SUCCESS;
562
563 fail:
564 vk_image_destroy(&device->vk, pAllocator, &image->vk);
565 return result;
566 }
567
568 static VkResult
create_image_from_swapchain(struct v3dv_device * device,const VkImageCreateInfo * pCreateInfo,const VkImageSwapchainCreateInfoKHR * swapchain_info,const VkAllocationCallbacks * pAllocator,VkImage * pImage)569 create_image_from_swapchain(struct v3dv_device *device,
570 const VkImageCreateInfo *pCreateInfo,
571 const VkImageSwapchainCreateInfoKHR *swapchain_info,
572 const VkAllocationCallbacks *pAllocator,
573 VkImage *pImage)
574 {
575 struct v3dv_image *swapchain_image =
576 v3dv_wsi_get_image_from_swapchain(swapchain_info->swapchain, 0);
577 assert(swapchain_image);
578
579 VkImageCreateInfo local_create_info = *pCreateInfo;
580 local_create_info.pNext = NULL;
581
582 /* Added by wsi code. */
583 local_create_info.usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
584
585 /* The spec requires TILING_OPTIMAL as input, but the swapchain image may
586 * privately use a different tiling. See spec anchor
587 * #swapchain-wsi-image-create-info .
588 */
589 assert(local_create_info.tiling == VK_IMAGE_TILING_OPTIMAL);
590 local_create_info.tiling = swapchain_image->vk.tiling;
591
592 VkImageDrmFormatModifierListCreateInfoEXT local_modifier_info = {
593 .sType = VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_LIST_CREATE_INFO_EXT,
594 .drmFormatModifierCount = 1,
595 .pDrmFormatModifiers = &swapchain_image->vk.drm_format_mod,
596 };
597
598 if (swapchain_image->vk.drm_format_mod != DRM_FORMAT_MOD_INVALID)
599 __vk_append_struct(&local_create_info, &local_modifier_info);
600
601 assert(swapchain_image->vk.image_type == local_create_info.imageType);
602 assert(swapchain_image->vk.format == local_create_info.format);
603 assert(swapchain_image->vk.extent.width == local_create_info.extent.width);
604 assert(swapchain_image->vk.extent.height == local_create_info.extent.height);
605 assert(swapchain_image->vk.extent.depth == local_create_info.extent.depth);
606 assert(swapchain_image->vk.array_layers == local_create_info.arrayLayers);
607 assert(swapchain_image->vk.samples == local_create_info.samples);
608 assert(swapchain_image->vk.tiling == local_create_info.tiling);
609 assert((swapchain_image->vk.usage & local_create_info.usage) ==
610 local_create_info.usage);
611
612 return create_image(device, &local_create_info, pAllocator, pImage);
613 }
614
615 VKAPI_ATTR VkResult VKAPI_CALL
v3dv_CreateImage(VkDevice _device,const VkImageCreateInfo * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkImage * pImage)616 v3dv_CreateImage(VkDevice _device,
617 const VkImageCreateInfo *pCreateInfo,
618 const VkAllocationCallbacks *pAllocator,
619 VkImage *pImage)
620 {
621 V3DV_FROM_HANDLE(v3dv_device, device, _device);
622 return create_image(device, pCreateInfo, pAllocator, pImage);
623 }
624
625 static void
get_image_subresource_layout(struct v3dv_device * device,struct v3dv_image * image,const VkImageSubresource2KHR * subresource2,VkSubresourceLayout2KHR * layout2)626 get_image_subresource_layout(struct v3dv_device *device,
627 struct v3dv_image *image,
628 const VkImageSubresource2KHR *subresource2,
629 VkSubresourceLayout2KHR *layout2)
630 {
631 const VkImageSubresource *subresource = &subresource2->imageSubresource;
632 VkSubresourceLayout *layout = &layout2->subresourceLayout;
633
634 uint8_t plane = v3dv_plane_from_aspect(subresource->aspectMask);
635 const struct v3d_resource_slice *slice =
636 &image->planes[plane].slices[subresource->mipLevel];
637
638 /* About why the offset below works for both disjoint and non-disjoint
639 * cases, from the Vulkan spec:
640 *
641 * "If the image is disjoint, then the offset is relative to the base
642 * address of the plane."
643 *
644 * "If the image is non-disjoint, then the offset is relative to the base
645 * address of the image."
646 *
647 * In our case, the per-plane mem_offset for non-disjoint images is the
648 * same for all planes and matches the base address of the image.
649 */
650 layout->offset =
651 v3dv_layer_offset(image, subresource->mipLevel, subresource->arrayLayer,
652 plane) - image->planes[plane].mem_offset;
653 layout->rowPitch = slice->stride;
654 layout->depthPitch = image->vk.image_type == VK_IMAGE_TYPE_3D ?
655 image->planes[plane].cube_map_stride : 0;
656 layout->arrayPitch = image->vk.array_layers > 1 ?
657 image->planes[plane].cube_map_stride : 0;
658
659 if (image->vk.image_type != VK_IMAGE_TYPE_3D) {
660 layout->size = slice->size;
661 } else {
662 /* For 3D images, the size of the slice represents the size of a 2D slice
663 * in the 3D image, so we have to multiply by the depth extent of the
664 * miplevel. For levels other than the first, we just compute the size
665 * as the distance between consecutive levels (notice that mip levels are
666 * arranged in memory from last to first).
667 */
668 if (subresource->mipLevel == 0) {
669 layout->size = slice->size * image->vk.extent.depth;
670 } else {
671 const struct v3d_resource_slice *prev_slice =
672 &image->planes[plane].slices[subresource->mipLevel - 1];
673 layout->size = prev_slice->offset - slice->offset;
674 }
675 }
676 }
677
678 VKAPI_ATTR void VKAPI_CALL
v3dv_GetImageSubresourceLayout2KHR(VkDevice _device,VkImage _image,const VkImageSubresource2KHR * subresource2,VkSubresourceLayout2KHR * layout2)679 v3dv_GetImageSubresourceLayout2KHR(VkDevice _device,
680 VkImage _image,
681 const VkImageSubresource2KHR *subresource2,
682 VkSubresourceLayout2KHR *layout2)
683 {
684 V3DV_FROM_HANDLE(v3dv_device, device, _device);
685 V3DV_FROM_HANDLE(v3dv_image, image, _image);
686 get_image_subresource_layout(device, image, subresource2, layout2);
687 }
688
689 VKAPI_ATTR void VKAPI_CALL
v3dv_GetDeviceImageSubresourceLayoutKHR(VkDevice vk_device,const VkDeviceImageSubresourceInfoKHR * pInfo,VkSubresourceLayout2KHR * pLayout)690 v3dv_GetDeviceImageSubresourceLayoutKHR(VkDevice vk_device,
691 const VkDeviceImageSubresourceInfoKHR *pInfo,
692 VkSubresourceLayout2KHR *pLayout)
693 {
694 V3DV_FROM_HANDLE(v3dv_device, device, vk_device);
695
696 memset(&pLayout->subresourceLayout, 0, sizeof(pLayout->subresourceLayout));
697
698 VkImage vk_image = VK_NULL_HANDLE;
699 VkResult result = create_image(device, pInfo->pCreateInfo, NULL, &vk_image);
700 if (result != VK_SUCCESS)
701 return;
702
703 struct v3dv_image *image = v3dv_image_from_handle(vk_image);
704 get_image_subresource_layout(device, image, pInfo->pSubresource, pLayout);
705
706 v3dv_DestroyImage(vk_device, vk_image, NULL);
707 }
708
709 VKAPI_ATTR void VKAPI_CALL
v3dv_DestroyImage(VkDevice _device,VkImage _image,const VkAllocationCallbacks * pAllocator)710 v3dv_DestroyImage(VkDevice _device,
711 VkImage _image,
712 const VkAllocationCallbacks* pAllocator)
713 {
714 V3DV_FROM_HANDLE(v3dv_device, device, _device);
715 V3DV_FROM_HANDLE(v3dv_image, image, _image);
716
717 if (image == NULL)
718 return;
719
720 /* If we have created a shadow tiled image for this image we must also free
721 * it (along with its memory allocation).
722 */
723 if (image->shadow) {
724 bool disjoint = image->vk.create_flags & VK_IMAGE_CREATE_DISJOINT_BIT;
725 for (int i = 0; i < (disjoint ? image->plane_count : 1); i++) {
726 if (image->shadow->planes[i].mem) {
727 v3dv_FreeMemory(_device,
728 v3dv_device_memory_to_handle(image->shadow->planes[i].mem),
729 pAllocator);
730 }
731 }
732 v3dv_DestroyImage(_device, v3dv_image_to_handle(image->shadow),
733 pAllocator);
734 image->shadow = NULL;
735 }
736
737 vk_image_destroy(&device->vk, pAllocator, &image->vk);
738 }
739
740 VkImageViewType
v3dv_image_type_to_view_type(VkImageType type)741 v3dv_image_type_to_view_type(VkImageType type)
742 {
743 switch (type) {
744 case VK_IMAGE_TYPE_1D: return VK_IMAGE_VIEW_TYPE_1D;
745 case VK_IMAGE_TYPE_2D: return VK_IMAGE_VIEW_TYPE_2D;
746 case VK_IMAGE_TYPE_3D: return VK_IMAGE_VIEW_TYPE_3D;
747 default:
748 unreachable("Invalid image type");
749 }
750 }
751
752 static VkResult
create_image_view(struct v3dv_device * device,bool driver_internal,const VkImageViewCreateInfo * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkImageView * pView)753 create_image_view(struct v3dv_device *device,
754 bool driver_internal,
755 const VkImageViewCreateInfo *pCreateInfo,
756 const VkAllocationCallbacks *pAllocator,
757 VkImageView *pView)
758 {
759 V3DV_FROM_HANDLE(v3dv_image, image, pCreateInfo->image);
760 struct v3dv_image_view *iview;
761
762 iview = vk_image_view_create(&device->vk, driver_internal, pCreateInfo,
763 pAllocator, sizeof(*iview));
764 if (iview == NULL)
765 return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
766
767 const VkImageAspectFlagBits any_plane_aspect =
768 VK_IMAGE_ASPECT_PLANE_0_BIT |
769 VK_IMAGE_ASPECT_PLANE_1_BIT |
770 VK_IMAGE_ASPECT_PLANE_2_BIT;
771
772 if (image->vk.aspects & any_plane_aspect) {
773 assert((image->vk.aspects & ~any_plane_aspect) == 0);
774 iview->plane_count = 0;
775 static const VkImageAspectFlagBits plane_aspects[]= {
776 VK_IMAGE_ASPECT_PLANE_0_BIT,
777 VK_IMAGE_ASPECT_PLANE_1_BIT,
778 VK_IMAGE_ASPECT_PLANE_2_BIT
779 };
780 for (uint8_t plane = 0; plane < V3DV_MAX_PLANE_COUNT; plane++) {
781 if (iview->vk.aspects & plane_aspects[plane])
782 iview->planes[iview->plane_count++].image_plane = plane;
783 }
784 } else {
785 iview->plane_count = 1;
786 iview->planes[0].image_plane = 0;
787 }
788 /* At this point we should have at least one plane */
789 assert(iview->plane_count > 0);
790
791 const VkImageSubresourceRange *range = &pCreateInfo->subresourceRange;
792
793 /* If we have D24S8 format but the view only selects the stencil aspect
794 * we want to re-interpret the format as RGBA8_UINT, then map our stencil
795 * data reads to the R component and ignore the GBA channels that contain
796 * the depth aspect data.
797 *
798 * FIXME: thwe code belows calls vk_component_mapping_to_pipe_swizzle
799 * only so it can then call util_format_compose_swizzles later. Maybe it
800 * makes sense to implement swizzle composition using VkSwizzle directly.
801 */
802 VkFormat format;
803 if (image->vk.format == VK_FORMAT_D24_UNORM_S8_UINT &&
804 range->aspectMask == VK_IMAGE_ASPECT_STENCIL_BIT) {
805 format = VK_FORMAT_R8G8B8A8_UINT;
806 uint8_t stencil_aspect_swizzle[4] = {
807 PIPE_SWIZZLE_X, PIPE_SWIZZLE_0, PIPE_SWIZZLE_0, PIPE_SWIZZLE_1,
808 };
809 uint8_t view_swizzle[4];
810 vk_component_mapping_to_pipe_swizzle(iview->vk.swizzle, view_swizzle);
811
812 util_format_compose_swizzles(stencil_aspect_swizzle, view_swizzle,
813 iview->view_swizzle);
814 } else {
815 format = iview->vk.format;
816 vk_component_mapping_to_pipe_swizzle(iview->vk.swizzle,
817 iview->view_swizzle);
818 }
819
820 iview->vk.view_format = format;
821 iview->format = v3dv_X(device, get_format)(format);
822 assert(iview->format && iview->format->plane_count);
823
824 for (uint8_t plane = 0; plane < iview->plane_count; plane++) {
825 iview->planes[plane].offset = v3dv_layer_offset(image,
826 iview->vk.base_mip_level,
827 iview->vk.base_array_layer,
828 plane);
829
830 if (vk_format_is_depth_or_stencil(iview->vk.view_format)) {
831 iview->planes[plane].internal_type =
832 v3dv_X(device, get_internal_depth_type)(iview->vk.view_format);
833 } else {
834 v3dv_X(device, get_internal_type_bpp_for_output_format)
835 (iview->format->planes[plane].rt_type,
836 &iview->planes[plane].internal_type,
837 &iview->planes[plane].internal_bpp);
838 }
839
840 const uint8_t *format_swizzle =
841 v3dv_get_format_swizzle(device, format, plane);
842 util_format_compose_swizzles(format_swizzle, iview->view_swizzle,
843 iview->planes[plane].swizzle);
844
845 iview->planes[plane].swap_rb = v3dv_format_swizzle_needs_rb_swap(format_swizzle);
846 iview->planes[plane].channel_reverse = v3dv_format_swizzle_needs_reverse(format_swizzle);
847 }
848
849 v3dv_X(device, pack_texture_shader_state)(device, iview);
850
851 *pView = v3dv_image_view_to_handle(iview);
852
853 return VK_SUCCESS;
854 }
855
856 VkResult
v3dv_create_image_view(struct v3dv_device * device,const VkImageViewCreateInfo * pCreateInfo,VkImageView * pView)857 v3dv_create_image_view(struct v3dv_device *device,
858 const VkImageViewCreateInfo *pCreateInfo,
859 VkImageView *pView)
860 {
861 return create_image_view(device, true, pCreateInfo, NULL, pView);
862 }
863
864 VKAPI_ATTR VkResult VKAPI_CALL
v3dv_CreateImageView(VkDevice _device,const VkImageViewCreateInfo * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkImageView * pView)865 v3dv_CreateImageView(VkDevice _device,
866 const VkImageViewCreateInfo *pCreateInfo,
867 const VkAllocationCallbacks *pAllocator,
868 VkImageView *pView)
869 {
870 V3DV_FROM_HANDLE(v3dv_device, device, _device);
871
872 return create_image_view(device, false, pCreateInfo, pAllocator, pView);
873 }
874
875 VKAPI_ATTR void VKAPI_CALL
v3dv_DestroyImageView(VkDevice _device,VkImageView imageView,const VkAllocationCallbacks * pAllocator)876 v3dv_DestroyImageView(VkDevice _device,
877 VkImageView imageView,
878 const VkAllocationCallbacks* pAllocator)
879 {
880 V3DV_FROM_HANDLE(v3dv_device, device, _device);
881 V3DV_FROM_HANDLE(v3dv_image_view, image_view, imageView);
882
883 if (image_view == NULL)
884 return;
885
886 if (image_view->shadow) {
887 v3dv_DestroyImageView(_device,
888 v3dv_image_view_to_handle(image_view->shadow),
889 pAllocator);
890 image_view->shadow = NULL;
891 }
892
893 vk_image_view_destroy(&device->vk, pAllocator, &image_view->vk);
894 }
895
896 VKAPI_ATTR VkResult VKAPI_CALL
v3dv_CreateBufferView(VkDevice _device,const VkBufferViewCreateInfo * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkBufferView * pView)897 v3dv_CreateBufferView(VkDevice _device,
898 const VkBufferViewCreateInfo *pCreateInfo,
899 const VkAllocationCallbacks *pAllocator,
900 VkBufferView *pView)
901 {
902 V3DV_FROM_HANDLE(v3dv_device, device, _device);
903
904 struct v3dv_buffer *buffer =
905 v3dv_buffer_from_handle(pCreateInfo->buffer);
906
907 struct v3dv_buffer_view *view =
908 vk_object_zalloc(&device->vk, pAllocator, sizeof(*view),
909 VK_OBJECT_TYPE_BUFFER_VIEW);
910 if (!view)
911 return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
912
913 uint32_t range;
914 if (pCreateInfo->range == VK_WHOLE_SIZE)
915 range = buffer->size - pCreateInfo->offset;
916 else
917 range = pCreateInfo->range;
918
919 enum pipe_format pipe_format = vk_format_to_pipe_format(pCreateInfo->format);
920 uint32_t num_elements = range / util_format_get_blocksize(pipe_format);
921
922 view->buffer = buffer;
923 view->offset = pCreateInfo->offset;
924 view->size = view->offset + range;
925 view->num_elements = num_elements;
926 view->vk_format = pCreateInfo->format;
927 view->format = v3dv_X(device, get_format)(view->vk_format);
928
929 /* We don't support multi-plane formats for buffer views */
930 assert(view->format->plane_count == 1);
931 v3dv_X(device, get_internal_type_bpp_for_output_format)
932 (view->format->planes[0].rt_type, &view->internal_type, &view->internal_bpp);
933
934 const VkBufferUsageFlags2CreateInfoKHR *flags2 =
935 vk_find_struct_const(pCreateInfo->pNext,
936 BUFFER_USAGE_FLAGS_2_CREATE_INFO_KHR);
937
938 VkBufferUsageFlags2KHR usage;
939 if (flags2)
940 usage = flags2->usage;
941 else
942 usage = buffer->usage;
943
944 if (usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT ||
945 usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT)
946 v3dv_X(device, pack_texture_shader_state_from_buffer_view)(device, view);
947
948 *pView = v3dv_buffer_view_to_handle(view);
949
950 return VK_SUCCESS;
951 }
952
953 VKAPI_ATTR void VKAPI_CALL
v3dv_DestroyBufferView(VkDevice _device,VkBufferView bufferView,const VkAllocationCallbacks * pAllocator)954 v3dv_DestroyBufferView(VkDevice _device,
955 VkBufferView bufferView,
956 const VkAllocationCallbacks *pAllocator)
957 {
958 V3DV_FROM_HANDLE(v3dv_device, device, _device);
959 V3DV_FROM_HANDLE(v3dv_buffer_view, buffer_view, bufferView);
960
961 if (buffer_view == NULL)
962 return;
963
964 vk_object_free(&device->vk, pAllocator, buffer_view);
965 }
966