Path: blob/21.2-virgl/src/amd/vulkan/radv_meta_copy.c
7233 views
/*1* Copyright © 2016 Intel Corporation2*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 "radv_meta.h"24#include "vk_format.h"2526static VkExtent3D27meta_image_block_size(const struct radv_image *image)28{29const struct util_format_description *desc = vk_format_description(image->vk_format);30return (VkExtent3D){desc->block.width, desc->block.height, 1};31}3233/* Returns the user-provided VkBufferImageCopy::imageExtent in units of34* elements rather than texels. One element equals one texel or one block35* if Image is uncompressed or compressed, respectively.36*/37static struct VkExtent3D38meta_region_extent_el(const struct radv_image *image, const VkImageType imageType,39const struct VkExtent3D *extent)40{41const VkExtent3D block = meta_image_block_size(image);42return radv_sanitize_image_extent(imageType,43(VkExtent3D){44.width = DIV_ROUND_UP(extent->width, block.width),45.height = DIV_ROUND_UP(extent->height, block.height),46.depth = DIV_ROUND_UP(extent->depth, block.depth),47});48}4950/* Returns the user-provided VkBufferImageCopy::imageOffset in units of51* elements rather than texels. One element equals one texel or one block52* if Image is uncompressed or compressed, respectively.53*/54static struct VkOffset3D55meta_region_offset_el(const struct radv_image *image, const struct VkOffset3D *offset)56{57const VkExtent3D block = meta_image_block_size(image);58return radv_sanitize_image_offset(image->type, (VkOffset3D){59.x = offset->x / block.width,60.y = offset->y / block.height,61.z = offset->z / block.depth,62});63}6465static VkFormat66vk_format_for_size(int bs)67{68switch (bs) {69case 1:70return VK_FORMAT_R8_UINT;71case 2:72return VK_FORMAT_R8G8_UINT;73case 4:74return VK_FORMAT_R8G8B8A8_UINT;75case 8:76return VK_FORMAT_R16G16B16A16_UINT;77case 12:78return VK_FORMAT_R32G32B32_UINT;79case 16:80return VK_FORMAT_R32G32B32A32_UINT;81default:82unreachable("Invalid format block size");83}84}8586static struct radv_meta_blit2d_surf87blit_surf_for_image_level_layer(struct radv_image *image, VkImageLayout layout,88const VkImageSubresourceLayers *subres,89VkImageAspectFlags aspect_mask)90{91VkFormat format = radv_get_aspect_format(image, aspect_mask);9293if (!radv_dcc_enabled(image, subres->mipLevel) && !(radv_image_is_tc_compat_htile(image)))94format = vk_format_for_size(vk_format_get_blocksize(format));9596format = vk_format_no_srgb(format);9798return (struct radv_meta_blit2d_surf){99.format = format,100.bs = vk_format_get_blocksize(format),101.level = subres->mipLevel,102.layer = subres->baseArrayLayer,103.image = image,104.aspect_mask = aspect_mask,105.current_layout = layout,106};107}108109bool110radv_image_is_renderable(struct radv_device *device, struct radv_image *image)111{112if (image->vk_format == VK_FORMAT_R32G32B32_UINT ||113image->vk_format == VK_FORMAT_R32G32B32_SINT ||114image->vk_format == VK_FORMAT_R32G32B32_SFLOAT)115return false;116117if (device->physical_device->rad_info.chip_class >= GFX9 && image->type == VK_IMAGE_TYPE_3D &&118vk_format_get_blocksizebits(image->vk_format) == 128 &&119vk_format_is_compressed(image->vk_format))120return false;121return true;122}123124static void125copy_buffer_to_image(struct radv_cmd_buffer *cmd_buffer, struct radv_buffer *buffer,126struct radv_image *image, VkImageLayout layout,127const VkBufferImageCopy2KHR *region)128{129struct radv_meta_saved_state saved_state;130bool old_predicating;131bool cs;132133/* The Vulkan 1.0 spec says "dstImage must have a sample count equal to134* VK_SAMPLE_COUNT_1_BIT."135*/136assert(image->info.samples == 1);137138cs = cmd_buffer->queue_family_index == RADV_QUEUE_COMPUTE ||139!radv_image_is_renderable(cmd_buffer->device, image);140141radv_meta_save(&saved_state, cmd_buffer,142(cs ? RADV_META_SAVE_COMPUTE_PIPELINE : RADV_META_SAVE_GRAPHICS_PIPELINE) |143RADV_META_SAVE_CONSTANTS | RADV_META_SAVE_DESCRIPTORS);144145/* VK_EXT_conditional_rendering says that copy commands should not be146* affected by conditional rendering.147*/148old_predicating = cmd_buffer->state.predicating;149cmd_buffer->state.predicating = false;150151/**152* From the Vulkan 1.0.6 spec: 18.3 Copying Data Between Images153* extent is the size in texels of the source image to copy in width,154* height and depth. 1D images use only x and width. 2D images use x, y,155* width and height. 3D images use x, y, z, width, height and depth.156*157*158* Also, convert the offsets and extent from units of texels to units of159* blocks - which is the highest resolution accessible in this command.160*/161const VkOffset3D img_offset_el = meta_region_offset_el(image, ®ion->imageOffset);162const VkExtent3D bufferExtent = {163.width = region->bufferRowLength ? region->bufferRowLength : region->imageExtent.width,164.height = region->bufferImageHeight ? region->bufferImageHeight : region->imageExtent.height,165};166const VkExtent3D buf_extent_el = meta_region_extent_el(image, image->type, &bufferExtent);167168/* Start creating blit rect */169const VkExtent3D img_extent_el = meta_region_extent_el(image, image->type, ®ion->imageExtent);170struct radv_meta_blit2d_rect rect = {171.width = img_extent_el.width,172.height = img_extent_el.height,173};174175/* Create blit surfaces */176struct radv_meta_blit2d_surf img_bsurf = blit_surf_for_image_level_layer(177image, layout, ®ion->imageSubresource, region->imageSubresource.aspectMask);178179if (!radv_is_buffer_format_supported(img_bsurf.format, NULL)) {180uint32_t queue_mask = radv_image_queue_family_mask(image, cmd_buffer->queue_family_index,181cmd_buffer->queue_family_index);182bool compressed =183radv_layout_dcc_compressed(cmd_buffer->device, image, region->imageSubresource.mipLevel,184layout, false, queue_mask);185if (compressed) {186radv_decompress_dcc(cmd_buffer, image,187&(VkImageSubresourceRange){188.aspectMask = region->imageSubresource.aspectMask,189.baseMipLevel = region->imageSubresource.mipLevel,190.levelCount = 1,191.baseArrayLayer = region->imageSubresource.baseArrayLayer,192.layerCount = region->imageSubresource.layerCount,193});194img_bsurf.disable_compression = true;195}196img_bsurf.format = vk_format_for_size(vk_format_get_blocksize(img_bsurf.format));197}198199struct radv_meta_blit2d_buffer buf_bsurf = {200.bs = img_bsurf.bs,201.format = img_bsurf.format,202.buffer = buffer,203.offset = region->bufferOffset,204.pitch = buf_extent_el.width,205};206207if (image->type == VK_IMAGE_TYPE_3D)208img_bsurf.layer = img_offset_el.z;209/* Loop through each 3D or array slice */210unsigned num_slices_3d = img_extent_el.depth;211unsigned num_slices_array = region->imageSubresource.layerCount;212unsigned slice_3d = 0;213unsigned slice_array = 0;214while (slice_3d < num_slices_3d && slice_array < num_slices_array) {215216rect.dst_x = img_offset_el.x;217rect.dst_y = img_offset_el.y;218219/* Perform Blit */220if (cs) {221radv_meta_buffer_to_image_cs(cmd_buffer, &buf_bsurf, &img_bsurf, 1, &rect);222} else {223radv_meta_blit2d(cmd_buffer, NULL, &buf_bsurf, &img_bsurf, 1, &rect);224}225226/* Once we've done the blit, all of the actual information about227* the image is embedded in the command buffer so we can just228* increment the offset directly in the image effectively229* re-binding it to different backing memory.230*/231buf_bsurf.offset += buf_extent_el.width * buf_extent_el.height * buf_bsurf.bs;232img_bsurf.layer++;233if (image->type == VK_IMAGE_TYPE_3D)234slice_3d++;235else236slice_array++;237}238239/* Restore conditional rendering. */240cmd_buffer->state.predicating = old_predicating;241242radv_meta_restore(&saved_state, cmd_buffer);243}244245void246radv_CmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,247const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo)248{249RADV_FROM_HANDLE(radv_cmd_buffer, cmd_buffer, commandBuffer);250RADV_FROM_HANDLE(radv_buffer, src_buffer, pCopyBufferToImageInfo->srcBuffer);251RADV_FROM_HANDLE(radv_image, dst_image, pCopyBufferToImageInfo->dstImage);252253for (unsigned r = 0; r < pCopyBufferToImageInfo->regionCount; r++) {254copy_buffer_to_image(cmd_buffer, src_buffer, dst_image,255pCopyBufferToImageInfo->dstImageLayout,256&pCopyBufferToImageInfo->pRegions[r]);257}258}259260static void261copy_image_to_buffer(struct radv_cmd_buffer *cmd_buffer, struct radv_buffer *buffer,262struct radv_image *image, VkImageLayout layout,263const VkBufferImageCopy2KHR *region)264{265struct radv_meta_saved_state saved_state;266bool old_predicating;267268radv_meta_save(269&saved_state, cmd_buffer,270RADV_META_SAVE_COMPUTE_PIPELINE | RADV_META_SAVE_CONSTANTS | RADV_META_SAVE_DESCRIPTORS);271272/* VK_EXT_conditional_rendering says that copy commands should not be273* affected by conditional rendering.274*/275old_predicating = cmd_buffer->state.predicating;276cmd_buffer->state.predicating = false;277278/**279* From the Vulkan 1.0.6 spec: 18.3 Copying Data Between Images280* extent is the size in texels of the source image to copy in width,281* height and depth. 1D images use only x and width. 2D images use x, y,282* width and height. 3D images use x, y, z, width, height and depth.283*284*285* Also, convert the offsets and extent from units of texels to units of286* blocks - which is the highest resolution accessible in this command.287*/288const VkOffset3D img_offset_el = meta_region_offset_el(image, ®ion->imageOffset);289const VkExtent3D bufferExtent = {290.width = region->bufferRowLength ? region->bufferRowLength : region->imageExtent.width,291.height = region->bufferImageHeight ? region->bufferImageHeight : region->imageExtent.height,292};293const VkExtent3D buf_extent_el = meta_region_extent_el(image, image->type, &bufferExtent);294295/* Start creating blit rect */296const VkExtent3D img_extent_el = meta_region_extent_el(image, image->type, ®ion->imageExtent);297struct radv_meta_blit2d_rect rect = {298.width = img_extent_el.width,299.height = img_extent_el.height,300};301302/* Create blit surfaces */303struct radv_meta_blit2d_surf img_info = blit_surf_for_image_level_layer(304image, layout, ®ion->imageSubresource, region->imageSubresource.aspectMask);305306if (!radv_is_buffer_format_supported(img_info.format, NULL)) {307uint32_t queue_mask = radv_image_queue_family_mask(image, cmd_buffer->queue_family_index,308cmd_buffer->queue_family_index);309bool compressed =310radv_layout_dcc_compressed(cmd_buffer->device, image, region->imageSubresource.mipLevel,311layout, false, queue_mask);312if (compressed) {313radv_decompress_dcc(cmd_buffer, image,314&(VkImageSubresourceRange){315.aspectMask = region->imageSubresource.aspectMask,316.baseMipLevel = region->imageSubresource.mipLevel,317.levelCount = 1,318.baseArrayLayer = region->imageSubresource.baseArrayLayer,319.layerCount = region->imageSubresource.layerCount,320});321img_info.disable_compression = true;322}323img_info.format = vk_format_for_size(vk_format_get_blocksize(img_info.format));324}325326struct radv_meta_blit2d_buffer buf_info = {327.bs = img_info.bs,328.format = img_info.format,329.buffer = buffer,330.offset = region->bufferOffset,331.pitch = buf_extent_el.width,332};333334if (image->type == VK_IMAGE_TYPE_3D)335img_info.layer = img_offset_el.z;336/* Loop through each 3D or array slice */337unsigned num_slices_3d = img_extent_el.depth;338unsigned num_slices_array = region->imageSubresource.layerCount;339unsigned slice_3d = 0;340unsigned slice_array = 0;341while (slice_3d < num_slices_3d && slice_array < num_slices_array) {342343rect.src_x = img_offset_el.x;344rect.src_y = img_offset_el.y;345346/* Perform Blit */347radv_meta_image_to_buffer(cmd_buffer, &img_info, &buf_info, 1, &rect);348349buf_info.offset += buf_extent_el.width * buf_extent_el.height * buf_info.bs;350img_info.layer++;351if (image->type == VK_IMAGE_TYPE_3D)352slice_3d++;353else354slice_array++;355}356357/* Restore conditional rendering. */358cmd_buffer->state.predicating = old_predicating;359360radv_meta_restore(&saved_state, cmd_buffer);361}362363void364radv_CmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,365const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo)366{367RADV_FROM_HANDLE(radv_cmd_buffer, cmd_buffer, commandBuffer);368RADV_FROM_HANDLE(radv_image, src_image, pCopyImageToBufferInfo->srcImage);369RADV_FROM_HANDLE(radv_buffer, dst_buffer, pCopyImageToBufferInfo->dstBuffer);370371for (unsigned r = 0; r < pCopyImageToBufferInfo->regionCount; r++) {372copy_image_to_buffer(cmd_buffer, dst_buffer, src_image,373pCopyImageToBufferInfo->srcImageLayout,374&pCopyImageToBufferInfo->pRegions[r]);375}376}377378static void379copy_image(struct radv_cmd_buffer *cmd_buffer, struct radv_image *src_image,380VkImageLayout src_image_layout, struct radv_image *dst_image,381VkImageLayout dst_image_layout, const VkImageCopy2KHR *region)382{383struct radv_meta_saved_state saved_state;384bool old_predicating;385bool cs;386387/* From the Vulkan 1.0 spec:388*389* vkCmdCopyImage can be used to copy image data between multisample390* images, but both images must have the same number of samples.391*/392assert(src_image->info.samples == dst_image->info.samples);393394cs = cmd_buffer->queue_family_index == RADV_QUEUE_COMPUTE ||395!radv_image_is_renderable(cmd_buffer->device, dst_image);396397radv_meta_save(&saved_state, cmd_buffer,398(cs ? RADV_META_SAVE_COMPUTE_PIPELINE : RADV_META_SAVE_GRAPHICS_PIPELINE) |399RADV_META_SAVE_CONSTANTS | RADV_META_SAVE_DESCRIPTORS);400401/* VK_EXT_conditional_rendering says that copy commands should not be402* affected by conditional rendering.403*/404old_predicating = cmd_buffer->state.predicating;405cmd_buffer->state.predicating = false;406407VkImageAspectFlags src_aspects[3] = {VK_IMAGE_ASPECT_PLANE_0_BIT, VK_IMAGE_ASPECT_PLANE_1_BIT,408VK_IMAGE_ASPECT_PLANE_2_BIT};409VkImageAspectFlags dst_aspects[3] = {VK_IMAGE_ASPECT_PLANE_0_BIT, VK_IMAGE_ASPECT_PLANE_1_BIT,410VK_IMAGE_ASPECT_PLANE_2_BIT};411unsigned aspect_count =412region->srcSubresource.aspectMask == VK_IMAGE_ASPECT_COLOR_BIT ? src_image->plane_count : 1;413if (region->srcSubresource.aspectMask != VK_IMAGE_ASPECT_COLOR_BIT)414src_aspects[0] = region->srcSubresource.aspectMask;415if (region->dstSubresource.aspectMask != VK_IMAGE_ASPECT_COLOR_BIT)416dst_aspects[0] = region->dstSubresource.aspectMask;417418for (unsigned a = 0; a < aspect_count; ++a) {419/* Create blit surfaces */420struct radv_meta_blit2d_surf b_src = blit_surf_for_image_level_layer(421src_image, src_image_layout, ®ion->srcSubresource, src_aspects[a]);422423struct radv_meta_blit2d_surf b_dst = blit_surf_for_image_level_layer(424dst_image, dst_image_layout, ®ion->dstSubresource, dst_aspects[a]);425426uint32_t dst_queue_mask = radv_image_queue_family_mask(427dst_image, cmd_buffer->queue_family_index, cmd_buffer->queue_family_index);428bool dst_compressed = radv_layout_dcc_compressed(cmd_buffer->device, dst_image,429region->dstSubresource.mipLevel,430dst_image_layout, false, dst_queue_mask);431uint32_t src_queue_mask = radv_image_queue_family_mask(432src_image, cmd_buffer->queue_family_index, cmd_buffer->queue_family_index);433bool src_compressed = radv_layout_dcc_compressed(cmd_buffer->device, src_image,434region->srcSubresource.mipLevel,435src_image_layout, false, src_queue_mask);436437if (!src_compressed || radv_dcc_formats_compatible(b_src.format, b_dst.format)) {438b_src.format = b_dst.format;439} else if (!dst_compressed) {440b_dst.format = b_src.format;441} else {442radv_decompress_dcc(cmd_buffer, dst_image,443&(VkImageSubresourceRange){444.aspectMask = dst_aspects[a],445.baseMipLevel = region->dstSubresource.mipLevel,446.levelCount = 1,447.baseArrayLayer = region->dstSubresource.baseArrayLayer,448.layerCount = region->dstSubresource.layerCount,449});450b_dst.format = b_src.format;451b_dst.disable_compression = true;452}453454/**455* From the Vulkan 1.0.6 spec: 18.4 Copying Data Between Buffers and Images456* imageExtent is the size in texels of the image to copy in width, height457* and depth. 1D images use only x and width. 2D images use x, y, width458* and height. 3D images use x, y, z, width, height and depth.459*460* Also, convert the offsets and extent from units of texels to units of461* blocks - which is the highest resolution accessible in this command.462*/463const VkOffset3D dst_offset_el = meta_region_offset_el(dst_image, ®ion->dstOffset);464const VkOffset3D src_offset_el = meta_region_offset_el(src_image, ®ion->srcOffset);465466/*467* From Vulkan 1.0.68, "Copying Data Between Images":468* "When copying between compressed and uncompressed formats469* the extent members represent the texel dimensions of the470* source image and not the destination."471* However, we must use the destination image type to avoid472* clamping depth when copying multiple layers of a 2D image to473* a 3D image.474*/475const VkExtent3D img_extent_el =476meta_region_extent_el(src_image, dst_image->type, ®ion->extent);477478/* Start creating blit rect */479struct radv_meta_blit2d_rect rect = {480.width = img_extent_el.width,481.height = img_extent_el.height,482};483484if (src_image->type == VK_IMAGE_TYPE_3D)485b_src.layer = src_offset_el.z;486487if (dst_image->type == VK_IMAGE_TYPE_3D)488b_dst.layer = dst_offset_el.z;489490/* Loop through each 3D or array slice */491unsigned num_slices_3d = img_extent_el.depth;492unsigned num_slices_array = region->dstSubresource.layerCount;493unsigned slice_3d = 0;494unsigned slice_array = 0;495while (slice_3d < num_slices_3d && slice_array < num_slices_array) {496497/* Finish creating blit rect */498rect.dst_x = dst_offset_el.x;499rect.dst_y = dst_offset_el.y;500rect.src_x = src_offset_el.x;501rect.src_y = src_offset_el.y;502503/* Perform Blit */504if (cs) {505radv_meta_image_to_image_cs(cmd_buffer, &b_src, &b_dst, 1, &rect);506} else {507radv_meta_blit2d(cmd_buffer, &b_src, NULL, &b_dst, 1, &rect);508}509510b_src.layer++;511b_dst.layer++;512if (dst_image->type == VK_IMAGE_TYPE_3D)513slice_3d++;514else515slice_array++;516}517}518519/* Restore conditional rendering. */520cmd_buffer->state.predicating = old_predicating;521522radv_meta_restore(&saved_state, cmd_buffer);523}524525void526radv_CmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo)527{528RADV_FROM_HANDLE(radv_cmd_buffer, cmd_buffer, commandBuffer);529RADV_FROM_HANDLE(radv_image, src_image, pCopyImageInfo->srcImage);530RADV_FROM_HANDLE(radv_image, dst_image, pCopyImageInfo->dstImage);531532for (unsigned r = 0; r < pCopyImageInfo->regionCount; r++) {533copy_image(cmd_buffer, src_image, pCopyImageInfo->srcImageLayout, dst_image,534pCopyImageInfo->dstImageLayout, &pCopyImageInfo->pRegions[r]);535}536}537538539