| 1 |
/* |
| 2 |
* Copyright © 2022 Imagination Technologies Ltd. |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 |
* of this software and associated documentation files (the "Software"), to deal |
| 6 |
* in the Software without restriction, including without limitation the rights |
| 7 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 |
* copies of the Software, and to permit persons to whom the Software is |
| 9 |
* 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 THE |
| 18 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 |
* SOFTWARE. |
| 22 |
*/ |
| 23 |
|
| 24 |
#include "pvr_image.h" |
| 25 |
|
| 26 |
#include <assert.h> |
| 27 |
#include <stdbool.h> |
| 28 |
#include <stdint.h> |
| 29 |
#include <string.h> |
| 30 |
|
| 31 |
#include "drm-uapi/drm_fourcc.h" |
| 32 |
#include "pvr_buffer.h" |
| 33 |
#include "pvr_device.h" |
| 34 |
#include "pvr_device_info.h" |
| 35 |
#include "pvr_entrypoints.h" |
| 36 |
#include "pvr_formats.h" |
| 37 |
#include "pvr_macros.h" |
| 38 |
#include "pvr_physical_device.h" |
| 39 |
#include "pvr_tex_state.h" |
| 40 |
#include "util/macros.h" |
| 41 |
#include "util/u_math.h" |
| 42 |
#include "vk_format.h" |
| 43 |
#include "vk_image.h" |
| 44 |
#include "vk_log.h" |
| 45 |
#include "vk_object.h" |
| 46 |
#include "vk_util.h" |
| 47 |
#include "wsi_common.h" |
| 48 |
|
| 49 |
static void pvr_image_init_memlayout(struct pvr_image *image) |
| 50 |
{ |
| 51 |
switch (image->vk.tiling) { |
| 52 |
default: |
| 53 |
UNREACHABLE("bad VkImageTiling"); |
| 54 |
case VK_IMAGE_TILING_OPTIMAL: |
| 55 |
if (image->vk.wsi_legacy_scanout || |
| 56 |
vk_format_get_ycbcr_info(image->vk.format)) |
| 57 |
image->memlayout = PVR_MEMLAYOUT_LINEAR; |
| 58 |
else if (image->vk.image_type == VK_IMAGE_TYPE_3D) |
| 59 |
image->memlayout = PVR_MEMLAYOUT_3DTWIDDLED; |
| 60 |
else |
| 61 |
image->memlayout = PVR_MEMLAYOUT_TWIDDLED; |
| 62 |
break; |
| 63 |
case VK_IMAGE_TILING_LINEAR: |
| 64 |
image->memlayout = PVR_MEMLAYOUT_LINEAR; |
| 65 |
break; |
| 66 |
case VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT: |
| 67 |
/* Support only LINEAR now */ |
| 68 |
assert(image->vk.drm_format_mod == DRM_FORMAT_MOD_LINEAR); |
| 69 |
image->memlayout = PVR_MEMLAYOUT_LINEAR; |
| 70 |
break; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
static void pvr_image_plane_init_physical_extent( |
| 75 |
struct pvr_image *image, |
| 76 |
const VkImageCreateInfo *pCreateInfo, |
| 77 |
unsigned pbe_stride_align, |
| 78 |
const struct vk_format_ycbcr_info *ycbcr_info, |
| 79 |
uint8_t i) |
| 80 |
{ |
| 81 |
struct pvr_image_plane *plane = &image->planes[i]; |
| 82 |
/* clang-format off */ |
| 83 |
if (image->vk.mip_levels > 1 || |
| 84 |
image->memlayout == PVR_MEMLAYOUT_TWIDDLED || |
| 85 |
image->memlayout == PVR_MEMLAYOUT_3DTWIDDLED) { |
| 86 |
/* clang-format on */ |
| 87 |
plane->physical_extent.width = |
| 88 |
util_next_power_of_two(image->vk.extent.width); |
| 89 |
plane->physical_extent.height = |
| 90 |
util_next_power_of_two(image->vk.extent.height); |
| 91 |
plane->physical_extent.depth = |
| 92 |
util_next_power_of_two(image->vk.extent.depth); |
| 93 |
} else { |
| 94 |
assert(image->memlayout == PVR_MEMLAYOUT_LINEAR); |
| 95 |
plane->physical_extent = image->vk.extent; |
| 96 |
|
| 97 |
/* Align the image for being rendered to (written by the PBE). */ |
| 98 |
plane->physical_extent.width = |
| 99 |
align(plane->physical_extent.width, pbe_stride_align); |
| 100 |
|
| 101 |
if (image->vk.tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT) { |
| 102 |
const VkImageDrmFormatModifierExplicitCreateInfoEXT *explicit_mod = |
| 103 |
vk_find_struct_const( |
| 104 |
pCreateInfo->pNext, |
| 105 |
IMAGE_DRM_FORMAT_MODIFIER_EXPLICIT_CREATE_INFO_EXT); |
| 106 |
if (explicit_mod) { |
| 107 |
const uint32_t bpp = |
| 108 |
vk_format_get_blocksize(image->vk.format); |
| 109 |
plane->physical_extent.width = |
| 110 |
explicit_mod->pPlaneLayouts[i].rowPitch / bpp; |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
if (ycbcr_info) { |
| 116 |
plane->physical_extent.width /= |
| 117 |
ycbcr_info->planes[i].denominator_scales[0]; |
| 118 |
plane->physical_extent.height /= |
| 119 |
ycbcr_info->planes[i].denominator_scales[1]; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
static void pvr_image_init_physical_extent(struct pvr_image *image, |
| 124 |
const VkImageCreateInfo *pCreateInfo, |
| 125 |
unsigned pbe_stride_align) |
| 126 |
{ |
| 127 |
assert(image->memlayout != PVR_MEMLAYOUT_UNDEFINED); |
| 128 |
|
| 129 |
const struct vk_format_ycbcr_info *ycbcr_info = |
| 130 |
vk_format_get_ycbcr_info(image->vk.format); |
| 131 |
|
| 132 |
for (uint8_t plane = 0; plane < image->plane_count; plane++) { |
| 133 |
pvr_image_plane_init_physical_extent(image, |
| 134 |
pCreateInfo, |
| 135 |
pbe_stride_align, |
| 136 |
ycbcr_info, |
| 137 |
plane); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
static void pvr_image_plane_setup_mip_levels(struct pvr_image *image, uint8_t i) |
| 142 |
{ |
| 143 |
struct pvr_image_plane *plane = &image->planes[i]; |
| 144 |
VkFormat plane_format = vk_format_get_plane_format(image->vk.format, i); |
| 145 |
|
| 146 |
const uint32_t extent_alignment = |
| 147 |
image->vk.image_type == VK_IMAGE_TYPE_3D ? 4 : 1; |
| 148 |
const unsigned int cpp = vk_format_get_blocksize(plane_format); |
| 149 |
VkExtent3D extent = |
| 150 |
vk_image_extent_to_elements(&image->vk, plane->physical_extent); |
| 151 |
|
| 152 |
assert(image->vk.mip_levels <= ARRAY_SIZE(plane->mip_levels)); |
| 153 |
|
| 154 |
plane->layer_size = 0; |
| 155 |
|
| 156 |
for (uint32_t i = 0; i < image->vk.mip_levels; i++) { |
| 157 |
struct pvr_mip_level *mip_level = &plane->mip_levels[i]; |
| 158 |
|
| 159 |
mip_level->pitch = cpp * align(extent.width, extent_alignment); |
| 160 |
mip_level->height_pitch = align(extent.height, extent_alignment); |
| 161 |
mip_level->size = image->vk.samples * mip_level->pitch * |
| 162 |
mip_level->height_pitch * |
| 163 |
align(extent.depth, extent_alignment); |
| 164 |
mip_level->offset = plane->layer_size; |
| 165 |
|
| 166 |
plane->layer_size += mip_level->size; |
| 167 |
|
| 168 |
extent.height = u_minify(extent.height, 1); |
| 169 |
extent.width = u_minify(extent.width, 1); |
| 170 |
extent.depth = u_minify(extent.depth, 1); |
| 171 |
} |
| 172 |
|
| 173 |
if (image->vk.mip_levels > 1) { |
| 174 |
/* The hw calculates layer strides as if a full mip chain up until 1x1x1 |
| 175 |
* were present so we need to account for that in the `layer_size`. |
| 176 |
*/ |
| 177 |
while (extent.height != 1 || extent.width != 1 || extent.depth != 1) { |
| 178 |
const uint32_t height_pitch = align(extent.height, extent_alignment); |
| 179 |
const uint32_t pitch = cpp * align(extent.width, extent_alignment); |
| 180 |
|
| 181 |
plane->layer_size += image->vk.samples * pitch * height_pitch * |
| 182 |
align(extent.depth, extent_alignment); |
| 183 |
|
| 184 |
extent.height = u_minify(extent.height, 1); |
| 185 |
extent.width = u_minify(extent.width, 1); |
| 186 |
extent.depth = u_minify(extent.depth, 1); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
/* TODO: It might be useful to store the alignment in the image so it can be |
| 191 |
* checked (via an assert?) when setting |
| 192 |
* RGX_CR_TPU_TAG_CEM_4K_FACE_PACKING_EN, assuming this is where the |
| 193 |
* requirement comes from. |
| 194 |
*/ |
| 195 |
if (image->vk.array_layers > 1) |
| 196 |
plane->layer_size = align64(plane->layer_size, image->alignment); |
| 197 |
|
| 198 |
plane->size = plane->layer_size * image->vk.array_layers; |
| 199 |
} |
| 200 |
|
| 201 |
static void pvr_image_setup_mip_levels(struct pvr_image *image) |
| 202 |
{ |
| 203 |
VkDeviceSize offset = 0; |
| 204 |
for (uint8_t plane = 0; plane < image->plane_count; plane++) { |
| 205 |
pvr_image_plane_setup_mip_levels(image, plane); |
| 206 |
|
| 207 |
offset = align(offset, image->alignment); |
| 208 |
image->planes[plane].offset = offset; |
| 209 |
offset += image->planes[plane].size; |
| 210 |
} |
| 211 |
|
| 212 |
image->total_size = offset; |
| 213 |
} |
| 214 |
|
| 215 |
static unsigned get_pbe_stride_align(const struct pvr_device_info *dev_info); |
| 216 |
|
| 217 |
static VkResult pvr_pick_modifier(const VkImageCreateInfo *pCreateInfo, |
| 218 |
unsigned pbe_stride_align, |
| 219 |
uint64_t *modifier) |
| 220 |
{ |
| 221 |
const VkImageDrmFormatModifierListCreateInfoEXT *mod_list = |
| 222 |
vk_find_struct_const(pCreateInfo->pNext, |
| 223 |
IMAGE_DRM_FORMAT_MODIFIER_LIST_CREATE_INFO_EXT); |
| 224 |
|
| 225 |
const VkImageDrmFormatModifierExplicitCreateInfoEXT *explicit_mod = |
| 226 |
vk_find_struct_const( |
| 227 |
pCreateInfo->pNext, |
| 228 |
IMAGE_DRM_FORMAT_MODIFIER_EXPLICIT_CREATE_INFO_EXT); |
| 229 |
|
| 230 |
/* Only support LINEAR now */ |
| 231 |
*modifier = DRM_FORMAT_MOD_INVALID; |
| 232 |
|
| 233 |
if (mod_list) |
| 234 |
for (unsigned i = 0; i < mod_list->drmFormatModifierCount; i++) |
| 235 |
if (mod_list->pDrmFormatModifiers[i] == DRM_FORMAT_MOD_LINEAR) |
| 236 |
*modifier = DRM_FORMAT_MOD_LINEAR; |
| 237 |
|
| 238 |
if (explicit_mod) { |
| 239 |
const uint32_t bpp = vk_format_get_blocksize(pCreateInfo->format); |
| 240 |
assert(explicit_mod->drmFormatModifier == DRM_FORMAT_MOD_LINEAR && |
| 241 |
explicit_mod->drmFormatModifierPlaneCount == 1); |
| 242 |
*modifier = explicit_mod->drmFormatModifier; |
| 243 |
|
| 244 |
if (explicit_mod->pPlaneLayouts[0].offset != 0) |
| 245 |
return VK_ERROR_INVALID_DRM_FORMAT_MODIFIER_PLANE_LAYOUT_EXT; |
| 246 |
|
| 247 |
if (explicit_mod->pPlaneLayouts[0].rowPitch % |
| 248 |
(bpp * pbe_stride_align) != 0) { |
| 249 |
return VK_ERROR_INVALID_DRM_FORMAT_MODIFIER_PLANE_LAYOUT_EXT; |
| 250 |
} |
| 251 |
|
| 252 |
if (explicit_mod->pPlaneLayouts[0].rowPitch < |
| 253 |
pCreateInfo->extent.width * bpp) { |
| 254 |
return VK_ERROR_INVALID_DRM_FORMAT_MODIFIER_PLANE_LAYOUT_EXT; |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
return VK_SUCCESS; |
| 259 |
} |
| 260 |
|
| 261 |
VkResult pvr_CreateImage(VkDevice _device, |
| 262 |
const VkImageCreateInfo *pCreateInfo, |
| 263 |
const VkAllocationCallbacks *pAllocator, |
| 264 |
VkImage *pImage) |
| 265 |
{ |
| 266 |
VK_FROM_HANDLE(pvr_device, device, _device); |
| 267 |
struct pvr_image *image; |
| 268 |
|
| 269 |
if (wsi_common_is_swapchain_image(pCreateInfo)) { |
| 270 |
return wsi_common_create_swapchain_image(&device->pdevice->wsi_device, |
| 271 |
pCreateInfo, |
| 272 |
pImage); |
| 273 |
} |
| 274 |
|
| 275 |
image = |
| 276 |
vk_image_create(&device->vk, pCreateInfo, pAllocator, sizeof(*image)); |
| 277 |
if (!image) |
| 278 |
return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY); |
| 279 |
|
| 280 |
pvr_image_init(device, pCreateInfo, image); |
| 281 |
|
| 282 |
*pImage = pvr_image_to_handle(image); |
| 283 |
|
| 284 |
return VK_SUCCESS; |
| 285 |
} |
| 286 |
|
| 287 |
void pvr_DestroyImage(VkDevice _device, |
| 288 |
VkImage _image, |
| 289 |
const VkAllocationCallbacks *pAllocator) |
| 290 |
{ |
| 291 |
VK_FROM_HANDLE(pvr_device, device, _device); |
| 292 |
VK_FROM_HANDLE(pvr_image, image, _image); |
| 293 |
|
| 294 |
if (!image) |
| 295 |
return; |
| 296 |
|
| 297 |
pvr_image_fini(device, image); |
| 298 |
vk_image_destroy(&device->vk, pAllocator, &image->vk); |
| 299 |
} |
| 300 |
|
| 301 |
void pvr_image_init(struct pvr_device *device, |
| 302 |
const VkImageCreateInfo *pCreateInfo, |
| 303 |
struct pvr_image *image) |
| 304 |
{ |
| 305 |
unsigned pbe_stride_align = get_pbe_stride_align(&device->pdevice->dev_info); |
| 306 |
|
| 307 |
image->plane_count = vk_format_get_plane_count(image->vk.format); |
| 308 |
image->alignment = device->pdevice->ws->page_size; |
| 309 |
|
| 310 |
if (pCreateInfo->tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT) { |
| 311 |
VkResult res = pvr_pick_modifier(pCreateInfo, |
| 312 |
pbe_stride_align, |
| 313 |
&image->vk.drm_format_mod); |
| 314 |
if (res != VK_SUCCESS) |
| 315 |
assert(res == VK_SUCCESS); |
| 316 |
|
| 317 |
assert(image->vk.drm_format_mod == DRM_FORMAT_MOD_LINEAR); |
| 318 |
} |
| 319 |
|
| 320 |
pvr_image_init_memlayout(image); |
| 321 |
pvr_image_init_physical_extent(image, pCreateInfo, pbe_stride_align); |
| 322 |
pvr_image_setup_mip_levels(image); |
| 323 |
} |
| 324 |
|
| 325 |
void pvr_image_fini(struct pvr_device *device, struct pvr_image *image) |
| 326 |
{ |
| 327 |
if (image->vma) |
| 328 |
pvr_unbind_memory(device, image->vma); |
| 329 |
} |
| 330 |
|
| 331 |
/* clang-format off */ |
| 332 |
/* Consider a 4 page buffer object. |
| 333 |
* _________________________________________ |
| 334 |
* | | | | | |
| 335 |
* |_________|__________|_________|__________| |
| 336 |
* | |
| 337 |
* \__ offset (0.5 page size) |
| 338 |
* |
| 339 |
* |___size(2 pages)____| |
| 340 |
* |
| 341 |
* |__VMA size required (3 pages)__| |
| 342 |
* |
| 343 |
* | |
| 344 |
* \__ returned dev_addr = vma + offset % page_size |
| 345 |
* |
| 346 |
* VMA size = align(size + offset % page_size, page_size); |
| 347 |
* |
| 348 |
* Note: the above handling is currently divided between generic |
| 349 |
* driver code and winsys layer. Given are the details of how this is |
| 350 |
* being handled. |
| 351 |
* * As winsys vma allocation interface does not have offset information, |
| 352 |
* it can not calculate the extra size needed to adjust for the unaligned |
| 353 |
* offset. So generic code is responsible for allocating a VMA that has |
| 354 |
* extra space to deal with the above scenario. |
| 355 |
* * Remaining work of mapping the vma to bo is done by vma_map interface, |
| 356 |
* as it contains offset information, we don't need to do any adjustments |
| 357 |
* in the generic code for this part. |
| 358 |
* |
| 359 |
* TODO: Look into merging heap_alloc and vma_map into single interface. |
| 360 |
*/ |
| 361 |
/* clang-format on */ |
| 362 |
|
| 363 |
VkResult pvr_BindImageMemory2(VkDevice _device, |
| 364 |
uint32_t bindInfoCount, |
| 365 |
const VkBindImageMemoryInfo *pBindInfos) |
| 366 |
{ |
| 367 |
VK_FROM_HANDLE(pvr_device, device, _device); |
| 368 |
uint32_t i; |
| 369 |
|
| 370 |
for (i = 0; i < bindInfoCount; i++) { |
| 371 |
VK_FROM_HANDLE(pvr_device_memory, mem, pBindInfos[i].memory); |
| 372 |
VK_FROM_HANDLE(pvr_image, image, pBindInfos[i].image); |
| 373 |
VkDeviceSize offset = pBindInfos[i].memoryOffset; |
| 374 |
VkResult result; |
| 375 |
|
| 376 |
#if defined(PVR_USE_WSI_PLATFORM) |
| 377 |
const VkBindImageMemorySwapchainInfoKHR *swapchain_info = |
| 378 |
vk_find_struct_const(pBindInfos[i].pNext, |
| 379 |
BIND_IMAGE_MEMORY_SWAPCHAIN_INFO_KHR); |
| 380 |
|
| 381 |
if (swapchain_info && swapchain_info->swapchain != VK_NULL_HANDLE) { |
| 382 |
VkDeviceMemory _swapchain_memory = |
| 383 |
wsi_common_get_memory(swapchain_info->swapchain, |
| 384 |
swapchain_info->imageIndex); |
| 385 |
VK_FROM_HANDLE(pvr_device_memory, swapchain_memory, _swapchain_memory); |
| 386 |
|
| 387 |
mem = swapchain_memory; |
| 388 |
offset = 0; |
| 389 |
} |
| 390 |
#endif |
| 391 |
|
| 392 |
result = pvr_bind_memory(device, |
| 393 |
mem, |
| 394 |
offset, |
| 395 |
image->total_size, |
| 396 |
image->alignment, |
| 397 |
&image->vma, |
| 398 |
&image->dev_addr); |
| 399 |
if (result != VK_SUCCESS) { |
| 400 |
while (i--) { |
| 401 |
VK_FROM_HANDLE(pvr_image, image, pBindInfos[i].image); |
| 402 |
|
| 403 |
pvr_unbind_memory(device, image->vma); |
| 404 |
} |
| 405 |
|
| 406 |
return result; |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
return VK_SUCCESS; |
| 411 |
} |
| 412 |
|
| 413 |
void pvr_get_image_subresource_layout(const struct pvr_image *image, |
| 414 |
const VkImageSubresource *subresource, |
| 415 |
VkSubresourceLayout *layout) |
| 416 |
{ |
| 417 |
const struct pvr_image_plane *plane = |
| 418 |
pvr_plane_from_aspect_const(image, subresource->aspectMask); |
| 419 |
const struct pvr_mip_level *mip_level = |
| 420 |
&plane->mip_levels[subresource->mipLevel]; |
| 421 |
|
| 422 |
layout->offset = plane->offset + |
| 423 |
subresource->arrayLayer * plane->layer_size + |
| 424 |
mip_level->offset; |
| 425 |
layout->rowPitch = mip_level->pitch; |
| 426 |
layout->depthPitch = mip_level->pitch * mip_level->height_pitch; |
| 427 |
layout->arrayPitch = plane->layer_size; |
| 428 |
layout->size = mip_level->size; |
| 429 |
} |
| 430 |
|
| 431 |
void pvr_GetImageSubresourceLayout(VkDevice device, |
| 432 |
VkImage _image, |
| 433 |
const VkImageSubresource *subresource, |
| 434 |
VkSubresourceLayout *layout) |
| 435 |
{ |
| 436 |
VK_FROM_HANDLE(pvr_image, image, _image); |
| 437 |
|
| 438 |
pvr_get_image_subresource_layout(image, subresource, layout); |
| 439 |
} |
| 440 |
|
| 441 |
/* Leave this at the very end, to avoid leakage of HW-defs here */ |
| 442 |
#define PVR_BUILD_ARCH_ROGUE |
| 443 |
#include "pvr_csb.h" |
| 444 |
|
| 445 |
static unsigned get_pbe_stride_align(const struct pvr_device_info *dev_info) |
| 446 |
{ |
| 447 |
return PVR_HAS_FEATURE(dev_info, pbe_stride_align_1pixel) |
| 448 |
? 1 |
| 449 |
: ROGUE_PBESTATE_REG_WORD0_LINESTRIDE_UNIT_SIZE; |
| 450 |
} |