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