Path: blob/21.2-virgl/src/broadcom/vulkan/v3dv_image.c
4560 views
/*1* Copyright © 2019 Raspberry Pi2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#include "v3dv_private.h"2425#include "drm-uapi/drm_fourcc.h"26#include "util/format/u_format.h"27#include "util/u_math.h"28#include "vk_format_info.h"29#include "vk_util.h"30#include "vulkan/wsi/wsi_common.h"3132/**33* Computes the HW's UIFblock padding for a given height/cpp.34*35* The goal of the padding is to keep pages of the same color (bank number) at36* least half a page away from each other vertically when crossing between37* columns of UIF blocks.38*/39static uint32_t40v3d_get_ub_pad(uint32_t cpp, uint32_t height)41{42uint32_t utile_h = v3d_utile_height(cpp);43uint32_t uif_block_h = utile_h * 2;44uint32_t height_ub = height / uif_block_h;4546uint32_t height_offset_in_pc = height_ub % PAGE_CACHE_UB_ROWS;4748/* For the perfectly-aligned-for-UIF-XOR case, don't add any pad. */49if (height_offset_in_pc == 0)50return 0;5152/* Try padding up to where we're offset by at least half a page. */53if (height_offset_in_pc < PAGE_UB_ROWS_TIMES_1_5) {54/* If we fit entirely in the page cache, don't pad. */55if (height_ub < PAGE_CACHE_UB_ROWS)56return 0;57else58return PAGE_UB_ROWS_TIMES_1_5 - height_offset_in_pc;59}6061/* If we're close to being aligned to page cache size, then round up62* and rely on XOR.63*/64if (height_offset_in_pc > PAGE_CACHE_MINUS_1_5_UB_ROWS)65return PAGE_CACHE_UB_ROWS - height_offset_in_pc;6667/* Otherwise, we're far enough away (top and bottom) to not need any68* padding.69*/70return 0;71}7273static void74v3d_setup_slices(struct v3dv_image *image)75{76assert(image->cpp > 0);7778uint32_t width = image->extent.width;79uint32_t height = image->extent.height;80uint32_t depth = image->extent.depth;8182/* Note that power-of-two padding is based on level 1. These are not83* equivalent to just util_next_power_of_two(dimension), because at a84* level 0 dimension of 9, the level 1 power-of-two padded value is 4,85* not 8.86*/87uint32_t pot_width = 2 * util_next_power_of_two(u_minify(width, 1));88uint32_t pot_height = 2 * util_next_power_of_two(u_minify(height, 1));89uint32_t pot_depth = 2 * util_next_power_of_two(u_minify(depth, 1));9091uint32_t utile_w = v3d_utile_width(image->cpp);92uint32_t utile_h = v3d_utile_height(image->cpp);93uint32_t uif_block_w = utile_w * 2;94uint32_t uif_block_h = utile_h * 2;9596uint32_t block_width = vk_format_get_blockwidth(image->vk_format);97uint32_t block_height = vk_format_get_blockheight(image->vk_format);9899assert(image->samples == VK_SAMPLE_COUNT_1_BIT ||100image->samples == VK_SAMPLE_COUNT_4_BIT);101bool msaa = image->samples != VK_SAMPLE_COUNT_1_BIT;102103bool uif_top = msaa;104105assert(image->array_size > 0);106assert(depth > 0);107assert(image->levels >= 1);108109uint32_t offset = 0;110for (int32_t i = image->levels - 1; i >= 0; i--) {111struct v3d_resource_slice *slice = &image->slices[i];112113uint32_t level_width, level_height, level_depth;114if (i < 2) {115level_width = u_minify(width, i);116level_height = u_minify(height, i);117} else {118level_width = u_minify(pot_width, i);119level_height = u_minify(pot_height, i);120}121122if (i < 1)123level_depth = u_minify(depth, i);124else125level_depth = u_minify(pot_depth, i);126127if (msaa) {128level_width *= 2;129level_height *= 2;130}131132level_width = DIV_ROUND_UP(level_width, block_width);133level_height = DIV_ROUND_UP(level_height, block_height);134135if (!image->tiled) {136slice->tiling = V3D_TILING_RASTER;137if (image->type == VK_IMAGE_TYPE_1D)138level_width = align(level_width, 64 / image->cpp);139} else {140if ((i != 0 || !uif_top) &&141(level_width <= utile_w || level_height <= utile_h)) {142slice->tiling = V3D_TILING_LINEARTILE;143level_width = align(level_width, utile_w);144level_height = align(level_height, utile_h);145} else if ((i != 0 || !uif_top) && level_width <= uif_block_w) {146slice->tiling = V3D_TILING_UBLINEAR_1_COLUMN;147level_width = align(level_width, uif_block_w);148level_height = align(level_height, uif_block_h);149} else if ((i != 0 || !uif_top) && level_width <= 2 * uif_block_w) {150slice->tiling = V3D_TILING_UBLINEAR_2_COLUMN;151level_width = align(level_width, 2 * uif_block_w);152level_height = align(level_height, uif_block_h);153} else {154/* We align the width to a 4-block column of UIF blocks, but we155* only align height to UIF blocks.156*/157level_width = align(level_width, 4 * uif_block_w);158level_height = align(level_height, uif_block_h);159160slice->ub_pad = v3d_get_ub_pad(image->cpp, level_height);161level_height += slice->ub_pad * uif_block_h;162163/* If the padding set us to to be aligned to the page cache size,164* then the HW will use the XOR bit on odd columns to get us165* perfectly misaligned.166*/167if ((level_height / uif_block_h) %168(V3D_PAGE_CACHE_SIZE / V3D_UIFBLOCK_ROW_SIZE) == 0) {169slice->tiling = V3D_TILING_UIF_XOR;170} else {171slice->tiling = V3D_TILING_UIF_NO_XOR;172}173}174}175176slice->offset = offset;177slice->stride = level_width * image->cpp;178slice->padded_height = level_height;179if (slice->tiling == V3D_TILING_UIF_NO_XOR ||180slice->tiling == V3D_TILING_UIF_XOR) {181slice->padded_height_of_output_image_in_uif_blocks =182slice->padded_height / (2 * v3d_utile_height(image->cpp));183}184185slice->size = level_height * slice->stride;186uint32_t slice_total_size = slice->size * level_depth;187188/* The HW aligns level 1's base to a page if any of level 1 or189* below could be UIF XOR. The lower levels then inherit the190* alignment for as long as necesary, thanks to being power of191* two aligned.192*/193if (i == 1 &&194level_width > 4 * uif_block_w &&195level_height > PAGE_CACHE_MINUS_1_5_UB_ROWS * uif_block_h) {196slice_total_size = align(slice_total_size, V3D_UIFCFG_PAGE_SIZE);197}198199offset += slice_total_size;200}201202image->size = offset;203204/* UIF/UBLINEAR levels need to be aligned to UIF-blocks, and LT only205* needs to be aligned to utile boundaries. Since tiles are laid out206* from small to big in memory, we need to align the later UIF slices207* to UIF blocks, if they were preceded by non-UIF-block-aligned LT208* slices.209*210* We additionally align to 4k, which improves UIF XOR performance.211*/212image->alignment =213image->tiling == VK_IMAGE_TILING_LINEAR ? image->cpp : 4096;214uint32_t align_offset =215align(image->slices[0].offset, image->alignment) - image->slices[0].offset;216if (align_offset) {217image->size += align_offset;218for (int i = 0; i < image->levels; i++)219image->slices[i].offset += align_offset;220}221222/* Arrays and cube textures have a stride which is the distance from223* one full mipmap tree to the next (64b aligned). For 3D textures,224* we need to program the stride between slices of miplevel 0.225*/226if (image->type != VK_IMAGE_TYPE_3D) {227image->cube_map_stride =228align(image->slices[0].offset + image->slices[0].size, 64);229image->size += image->cube_map_stride * (image->array_size - 1);230} else {231image->cube_map_stride = image->slices[0].size;232}233}234235uint32_t236v3dv_layer_offset(const struct v3dv_image *image, uint32_t level, uint32_t layer)237{238const struct v3d_resource_slice *slice = &image->slices[level];239240if (image->type == VK_IMAGE_TYPE_3D)241return image->mem_offset + slice->offset + layer * slice->size;242else243return image->mem_offset + slice->offset + layer * image->cube_map_stride;244}245246static VkResult247create_image(struct v3dv_device *device,248const VkImageCreateInfo *pCreateInfo,249const VkAllocationCallbacks *pAllocator,250VkImage *pImage)251{252struct v3dv_image *image = NULL;253254assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO);255256v3dv_assert(pCreateInfo->mipLevels > 0);257v3dv_assert(pCreateInfo->arrayLayers > 0);258v3dv_assert(pCreateInfo->samples > 0);259v3dv_assert(pCreateInfo->extent.width > 0);260v3dv_assert(pCreateInfo->extent.height > 0);261v3dv_assert(pCreateInfo->extent.depth > 0);262263/* When using the simulator the WSI common code will see that our264* driver wsi device doesn't match the display device and because of that265* it will not attempt to present directly from the swapchain images,266* instead it will use the prime blit path (use_prime_blit flag in267* struct wsi_swapchain), where it copies the contents of the swapchain268* images to a linear buffer with appropriate row stride for presentation.269* As a result, on that path, swapchain images do not have any special270* requirements and are not created with the pNext structs below.271*/272uint64_t modifier = DRM_FORMAT_MOD_INVALID;273if (pCreateInfo->tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT) {274const VkImageDrmFormatModifierListCreateInfoEXT *mod_info =275vk_find_struct_const(pCreateInfo->pNext,276IMAGE_DRM_FORMAT_MODIFIER_LIST_CREATE_INFO_EXT);277assert(mod_info);278for (uint32_t i = 0; i < mod_info->drmFormatModifierCount; i++) {279switch (mod_info->pDrmFormatModifiers[i]) {280case DRM_FORMAT_MOD_LINEAR:281if (modifier == DRM_FORMAT_MOD_INVALID)282modifier = DRM_FORMAT_MOD_LINEAR;283break;284case DRM_FORMAT_MOD_BROADCOM_UIF:285modifier = DRM_FORMAT_MOD_BROADCOM_UIF;286break;287}288}289} else {290const struct wsi_image_create_info *wsi_info =291vk_find_struct_const(pCreateInfo->pNext, WSI_IMAGE_CREATE_INFO_MESA);292if (wsi_info && wsi_info->scanout)293modifier = DRM_FORMAT_MOD_LINEAR;294}295296const VkExternalMemoryImageCreateInfo *external_info =297vk_find_struct_const(pCreateInfo->pNext, EXTERNAL_MEMORY_IMAGE_CREATE_INFO);298299/* 1D and 1D_ARRAY textures are always raster-order */300VkImageTiling tiling;301if (pCreateInfo->imageType == VK_IMAGE_TYPE_1D)302tiling = VK_IMAGE_TILING_LINEAR;303else if (modifier == DRM_FORMAT_MOD_INVALID)304tiling = pCreateInfo->tiling;305else if (modifier == DRM_FORMAT_MOD_BROADCOM_UIF)306tiling = VK_IMAGE_TILING_OPTIMAL;307else308tiling = VK_IMAGE_TILING_LINEAR;309310const struct v3dv_format *format = v3dv_X(device, get_format)(pCreateInfo->format);311v3dv_assert(format != NULL && format->supported);312313image = vk_object_zalloc(&device->vk, pAllocator, sizeof(*image),314VK_OBJECT_TYPE_IMAGE);315if (!image)316return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);317318assert(pCreateInfo->samples == VK_SAMPLE_COUNT_1_BIT ||319pCreateInfo->samples == VK_SAMPLE_COUNT_4_BIT);320321image->type = pCreateInfo->imageType;322image->extent = pCreateInfo->extent;323image->vk_format = pCreateInfo->format;324image->format = format;325image->aspects = vk_format_aspects(image->vk_format);326image->levels = pCreateInfo->mipLevels;327image->array_size = pCreateInfo->arrayLayers;328image->samples = pCreateInfo->samples;329image->usage = pCreateInfo->usage;330image->flags = pCreateInfo->flags;331332image->drm_format_mod = modifier;333image->tiling = tiling;334image->tiled = tiling == VK_IMAGE_TILING_OPTIMAL;335image->external = external_info != NULL;336337image->cpp = vk_format_get_blocksize(image->vk_format);338339v3d_setup_slices(image);340341*pImage = v3dv_image_to_handle(image);342343return VK_SUCCESS;344}345346static VkResult347create_image_from_swapchain(struct v3dv_device *device,348const VkImageCreateInfo *pCreateInfo,349const VkImageSwapchainCreateInfoKHR *swapchain_info,350const VkAllocationCallbacks *pAllocator,351VkImage *pImage)352{353struct v3dv_image *swapchain_image =354v3dv_wsi_get_image_from_swapchain(swapchain_info->swapchain, 0);355assert(swapchain_image);356357VkImageCreateInfo local_create_info = *pCreateInfo;358local_create_info.pNext = NULL;359360/* Added by wsi code. */361local_create_info.usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;362363/* The spec requires TILING_OPTIMAL as input, but the swapchain image may364* privately use a different tiling. See spec anchor365* #swapchain-wsi-image-create-info .366*/367assert(local_create_info.tiling == VK_IMAGE_TILING_OPTIMAL);368local_create_info.tiling = swapchain_image->tiling;369370VkImageDrmFormatModifierListCreateInfoEXT local_modifier_info = {371.sType = VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_LIST_CREATE_INFO_EXT,372.drmFormatModifierCount = 1,373.pDrmFormatModifiers = &swapchain_image->drm_format_mod,374};375376if (swapchain_image->drm_format_mod != DRM_FORMAT_MOD_INVALID)377__vk_append_struct(&local_create_info, &local_modifier_info);378379assert(swapchain_image->type == local_create_info.imageType);380assert(swapchain_image->vk_format == local_create_info.format);381assert(swapchain_image->extent.width == local_create_info.extent.width);382assert(swapchain_image->extent.height == local_create_info.extent.height);383assert(swapchain_image->extent.depth == local_create_info.extent.depth);384assert(swapchain_image->array_size == local_create_info.arrayLayers);385assert(swapchain_image->samples == local_create_info.samples);386assert(swapchain_image->tiling == local_create_info.tiling);387assert((swapchain_image->usage & local_create_info.usage) ==388local_create_info.usage);389390return create_image(device, &local_create_info, pAllocator, pImage);391}392393VKAPI_ATTR VkResult VKAPI_CALL394v3dv_CreateImage(VkDevice _device,395const VkImageCreateInfo *pCreateInfo,396const VkAllocationCallbacks *pAllocator,397VkImage *pImage)398{399V3DV_FROM_HANDLE(v3dv_device, device, _device);400401const VkImageSwapchainCreateInfoKHR *swapchain_info =402vk_find_struct_const(pCreateInfo->pNext, IMAGE_SWAPCHAIN_CREATE_INFO_KHR);403if (swapchain_info && swapchain_info->swapchain != VK_NULL_HANDLE)404return create_image_from_swapchain(device, pCreateInfo, swapchain_info,405pAllocator, pImage);406407return create_image(device, pCreateInfo, pAllocator, pImage);408}409410VKAPI_ATTR void VKAPI_CALL411v3dv_GetImageSubresourceLayout(VkDevice device,412VkImage _image,413const VkImageSubresource *subresource,414VkSubresourceLayout *layout)415{416V3DV_FROM_HANDLE(v3dv_image, image, _image);417418const struct v3d_resource_slice *slice =419&image->slices[subresource->mipLevel];420layout->offset =421v3dv_layer_offset(image, subresource->mipLevel, subresource->arrayLayer);422layout->rowPitch = slice->stride;423layout->depthPitch = image->cube_map_stride;424layout->arrayPitch = image->cube_map_stride;425426if (image->type != VK_IMAGE_TYPE_3D) {427layout->size = slice->size;428} else {429/* For 3D images, the size of the slice represents the size of a 2D slice430* in the 3D image, so we have to multiply by the depth extent of the431* miplevel. For levels other than the first, we just compute the size432* as the distance between consecutive levels (notice that mip levels are433* arranged in memory from last to first).434*/435if (subresource->mipLevel == 0) {436layout->size = slice->size * image->extent.depth;437} else {438const struct v3d_resource_slice *prev_slice =439&image->slices[subresource->mipLevel - 1];440layout->size = prev_slice->offset - slice->offset;441}442}443}444445VKAPI_ATTR VkResult VKAPI_CALL446v3dv_GetImageDrmFormatModifierPropertiesEXT(447VkDevice device,448VkImage _image,449VkImageDrmFormatModifierPropertiesEXT *pProperties)450{451V3DV_FROM_HANDLE(v3dv_image, image, _image);452453assert(pProperties->sType ==454VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_PROPERTIES_EXT);455456pProperties->drmFormatModifier = image->drm_format_mod;457458return VK_SUCCESS;459}460461VKAPI_ATTR void VKAPI_CALL462v3dv_DestroyImage(VkDevice _device,463VkImage _image,464const VkAllocationCallbacks* pAllocator)465{466V3DV_FROM_HANDLE(v3dv_device, device, _device);467V3DV_FROM_HANDLE(v3dv_image, image, _image);468469if (image == NULL)470return;471472vk_object_free(&device->vk, pAllocator, image);473}474475VkImageViewType476v3dv_image_type_to_view_type(VkImageType type)477{478switch (type) {479case VK_IMAGE_TYPE_1D: return VK_IMAGE_VIEW_TYPE_1D;480case VK_IMAGE_TYPE_2D: return VK_IMAGE_VIEW_TYPE_2D;481case VK_IMAGE_TYPE_3D: return VK_IMAGE_VIEW_TYPE_3D;482default:483unreachable("Invalid image type");484}485}486487static enum pipe_swizzle488vk_component_mapping_to_pipe_swizzle(VkComponentSwizzle comp,489VkComponentSwizzle swz)490{491if (swz == VK_COMPONENT_SWIZZLE_IDENTITY)492swz = comp;493494switch (swz) {495case VK_COMPONENT_SWIZZLE_ZERO:496return PIPE_SWIZZLE_0;497case VK_COMPONENT_SWIZZLE_ONE:498return PIPE_SWIZZLE_1;499case VK_COMPONENT_SWIZZLE_R:500return PIPE_SWIZZLE_X;501case VK_COMPONENT_SWIZZLE_G:502return PIPE_SWIZZLE_Y;503case VK_COMPONENT_SWIZZLE_B:504return PIPE_SWIZZLE_Z;505case VK_COMPONENT_SWIZZLE_A:506return PIPE_SWIZZLE_W;507default:508unreachable("Unknown VkComponentSwizzle");509};510}511512VKAPI_ATTR VkResult VKAPI_CALL513v3dv_CreateImageView(VkDevice _device,514const VkImageViewCreateInfo *pCreateInfo,515const VkAllocationCallbacks *pAllocator,516VkImageView *pView)517{518V3DV_FROM_HANDLE(v3dv_device, device, _device);519V3DV_FROM_HANDLE(v3dv_image, image, pCreateInfo->image);520struct v3dv_image_view *iview;521522iview = vk_object_zalloc(&device->vk, pAllocator, sizeof(*iview),523VK_OBJECT_TYPE_IMAGE_VIEW);524if (iview == NULL)525return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);526527const VkImageSubresourceRange *range = &pCreateInfo->subresourceRange;528529assert(range->layerCount > 0);530assert(range->baseMipLevel < image->levels);531532#ifdef DEBUG533switch (image->type) {534case VK_IMAGE_TYPE_1D:535case VK_IMAGE_TYPE_2D:536assert(range->baseArrayLayer + v3dv_layer_count(image, range) - 1 <=537image->array_size);538break;539case VK_IMAGE_TYPE_3D:540assert(range->baseArrayLayer + v3dv_layer_count(image, range) - 1541<= u_minify(image->extent.depth, range->baseMipLevel));542/* VK_KHR_maintenance1 */543assert(pCreateInfo->viewType != VK_IMAGE_VIEW_TYPE_2D ||544((image->flags & VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT) &&545range->levelCount == 1 && range->layerCount == 1));546assert(pCreateInfo->viewType != VK_IMAGE_VIEW_TYPE_2D_ARRAY ||547((image->flags & VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT) &&548range->levelCount == 1));549break;550default:551unreachable("bad VkImageType");552}553#endif554555iview->image = image;556iview->aspects = range->aspectMask;557iview->type = pCreateInfo->viewType;558559iview->base_level = range->baseMipLevel;560iview->max_level = iview->base_level + v3dv_level_count(image, range) - 1;561iview->extent = (VkExtent3D) {562.width = u_minify(image->extent.width , iview->base_level),563.height = u_minify(image->extent.height, iview->base_level),564.depth = u_minify(image->extent.depth , iview->base_level),565};566567iview->first_layer = range->baseArrayLayer;568iview->last_layer = range->baseArrayLayer +569v3dv_layer_count(image, range) - 1;570iview->offset =571v3dv_layer_offset(image, iview->base_level, iview->first_layer);572573/* If we have D24S8 format but the view only selects the stencil aspect574* we want to re-interpret the format as RGBA8_UINT, then map our stencil575* data reads to the R component and ignore the GBA channels that contain576* the depth aspect data.577*/578VkFormat format;579uint8_t image_view_swizzle[4];580if (pCreateInfo->format == VK_FORMAT_D24_UNORM_S8_UINT &&581range->aspectMask == VK_IMAGE_ASPECT_STENCIL_BIT) {582format = VK_FORMAT_R8G8B8A8_UINT;583image_view_swizzle[0] = PIPE_SWIZZLE_X;584image_view_swizzle[1] = PIPE_SWIZZLE_0;585image_view_swizzle[2] = PIPE_SWIZZLE_0;586image_view_swizzle[3] = PIPE_SWIZZLE_1;587} else {588format = pCreateInfo->format;589590/* FIXME: we are doing this vk to pipe swizzle mapping just to call591* util_format_compose_swizzles. Would be good to check if it would be592* better to reimplement the latter using vk component593*/594image_view_swizzle[0] =595vk_component_mapping_to_pipe_swizzle(VK_COMPONENT_SWIZZLE_R,596pCreateInfo->components.r);597image_view_swizzle[1] =598vk_component_mapping_to_pipe_swizzle(VK_COMPONENT_SWIZZLE_G,599pCreateInfo->components.g);600image_view_swizzle[2] =601vk_component_mapping_to_pipe_swizzle(VK_COMPONENT_SWIZZLE_B,602pCreateInfo->components.b);603image_view_swizzle[3] =604vk_component_mapping_to_pipe_swizzle(VK_COMPONENT_SWIZZLE_A,605pCreateInfo->components.a);606}607608iview->vk_format = format;609iview->format = v3dv_X(device, get_format)(format);610assert(iview->format && iview->format->supported);611612if (vk_format_is_depth_or_stencil(iview->vk_format)) {613iview->internal_type = v3dv_X(device, get_internal_depth_type)(iview->vk_format);614} else {615v3dv_X(device, get_internal_type_bpp_for_output_format)616(iview->format->rt_type, &iview->internal_type, &iview->internal_bpp);617}618619const uint8_t *format_swizzle = v3dv_get_format_swizzle(device, format);620util_format_compose_swizzles(format_swizzle, image_view_swizzle,621iview->swizzle);622iview->swap_rb = iview->swizzle[0] == PIPE_SWIZZLE_Z;623624v3dv_X(device, pack_texture_shader_state)(device, iview);625626*pView = v3dv_image_view_to_handle(iview);627628return VK_SUCCESS;629}630631VKAPI_ATTR void VKAPI_CALL632v3dv_DestroyImageView(VkDevice _device,633VkImageView imageView,634const VkAllocationCallbacks* pAllocator)635{636V3DV_FROM_HANDLE(v3dv_device, device, _device);637V3DV_FROM_HANDLE(v3dv_image_view, image_view, imageView);638639if (image_view == NULL)640return;641642vk_object_free(&device->vk, pAllocator, image_view);643}644645VKAPI_ATTR VkResult VKAPI_CALL646v3dv_CreateBufferView(VkDevice _device,647const VkBufferViewCreateInfo *pCreateInfo,648const VkAllocationCallbacks *pAllocator,649VkBufferView *pView)650{651V3DV_FROM_HANDLE(v3dv_device, device, _device);652653struct v3dv_buffer *buffer =654v3dv_buffer_from_handle(pCreateInfo->buffer);655656struct v3dv_buffer_view *view =657vk_object_zalloc(&device->vk, pAllocator, sizeof(*view),658VK_OBJECT_TYPE_BUFFER_VIEW);659if (!view)660return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);661662uint32_t range;663if (pCreateInfo->range == VK_WHOLE_SIZE)664range = buffer->size - pCreateInfo->offset;665else666range = pCreateInfo->range;667668enum pipe_format pipe_format = vk_format_to_pipe_format(pCreateInfo->format);669uint32_t num_elements = range / util_format_get_blocksize(pipe_format);670671view->buffer = buffer;672view->offset = pCreateInfo->offset;673view->size = view->offset + range;674view->num_elements = num_elements;675view->vk_format = pCreateInfo->format;676view->format = v3dv_X(device, get_format)(view->vk_format);677678v3dv_X(device, get_internal_type_bpp_for_output_format)679(view->format->rt_type, &view->internal_type, &view->internal_bpp);680681if (buffer->usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT ||682buffer->usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT)683v3dv_X(device, pack_texture_shader_state_from_buffer_view)(device, view);684685*pView = v3dv_buffer_view_to_handle(view);686687return VK_SUCCESS;688}689690VKAPI_ATTR void VKAPI_CALL691v3dv_DestroyBufferView(VkDevice _device,692VkBufferView bufferView,693const VkAllocationCallbacks *pAllocator)694{695V3DV_FROM_HANDLE(v3dv_device, device, _device);696V3DV_FROM_HANDLE(v3dv_buffer_view, buffer_view, bufferView);697698if (buffer_view == NULL)699return;700701vk_object_free(&device->vk, pAllocator, buffer_view);702}703704705