VERY experimental Nintendo Switch 2 emulator
C++
TextureCache: Simplify blitting of D24S8 formats and fix bugs.
Fernando Sahmkow09a236dbbf44
diff --git a/src/video_core/host_shaders/CMakeLists.txt b/src/video_core/host_shaders/CMakeLists.txtindex 1c91999d7e..fd3e414349 100644--- a/src/video_core/host_shaders/CMakeLists.txt+++ b/src/video_core/host_shaders/CMakeLists.txt@@ -11,13 +11,9 @@ set(SHADER_FILESblock_linear_unswizzle_2d.compblock_linear_unswizzle_3d.compconvert_abgr8_to_d24s8.frag- convert_b10g11r11_to_d24s8.fragconvert_d24s8_to_abgr8.frag- convert_d24s8_to_b10g11r11.frag- convert_d24s8_to_r16g16.fragconvert_depth_to_float.fragconvert_float_to_depth.frag- convert_r16g16_to_d24s8.fragfull_screen_triangle.vertfxaa.fragfxaa.vertdiff --git a/src/video_core/host_shaders/convert_b10g11r11_to_d24s8.frag b/src/video_core/host_shaders/convert_b10g11r11_to_d24s8.fragdeleted file mode 100644index 11bdd861db..0000000000--- a/src/video_core/host_shaders/convert_b10g11r11_to_d24s8.frag+++ /dev/null@@ -1,27 +0,0 @@-// Copyright 2021 yuzu Emulator Project-// Licensed under GPLv2 or any later version-// Refer to the license.txt file included.--#version 450-#extension GL_ARB_shader_stencil_export : require--layout(binding = 0) uniform sampler2D color_texture;--uint conv_from_float(float value_f, uint mantissa_bits) {- uint value = floatBitsToInt(value_f);- uint exp = (value >> 23) & 0x1Fu;- uint mantissa_shift = 32u - mantissa_bits;- uint mantissa = (value << 9u) >> mantissa_shift;- return (exp << mantissa_bits) | mantissa;-}--void main() {- ivec2 coord = ivec2(gl_FragCoord.xy);- vec4 color = texelFetch(color_texture, coord, 0).rgba;- uint depth_stencil_unorm = (conv_from_float(color.r, 6u) << 21)- | (conv_from_float(color.g, 6u) << 10)- | conv_from_float(color.b, 5u);-- gl_FragDepth = float(depth_stencil_unorm & 0x00FFFFFFu) / (exp2(24.0) - 1.0f);- gl_FragStencilRefARB = int(depth_stencil_unorm >> 24);-}diff --git a/src/video_core/host_shaders/convert_d24s8_to_b10g11r11.frag b/src/video_core/host_shaders/convert_d24s8_to_b10g11r11.fragdeleted file mode 100644index c2d935fcd9..0000000000--- a/src/video_core/host_shaders/convert_d24s8_to_b10g11r11.frag+++ /dev/null@@ -1,32 +0,0 @@-// Copyright 2021 yuzu Emulator Project-// Licensed under GPLv2 or any later version-// Refer to the license.txt file included.--#version 450--layout(binding = 0) uniform sampler2D depth_tex;-layout(binding = 1) uniform isampler2D stencil_tex;--layout(location = 0) out vec4 color;--float conv_to_float(uint value, uint mantissa_bits) {- uint exp = (value >> mantissa_bits) & 0x1Fu;- uint mantissa_shift = 32u - mantissa_bits;- uint mantissa = (value << mantissa_shift) >> mantissa_shift;- return uintBitsToFloat((exp << 23) | (mantissa << (23 - mantissa_bits)));-}--void main() {- ivec2 coord = ivec2(gl_FragCoord.xy);- uint depth = uint(textureLod(depth_tex, coord, 0).r * (exp2(32.0) - 1.0f));- uint stencil = uint(textureLod(stencil_tex, coord, 0).r);- uint depth_stencil = (stencil << 24) | (depth >> 8);- uint red_int = (depth_stencil >> 21) & 0x07FF;- uint green_int = (depth_stencil >> 10) & 0x07FF;- uint blue_int = depth_stencil & 0x03FF;-- color.r = conv_to_float(red_int, 6u);- color.g = conv_to_float(green_int, 6u);- color.b = conv_to_float(blue_int, 5u);- color.a = 1.0f;-}diff --git a/src/video_core/host_shaders/convert_d24s8_to_r16g16.frag b/src/video_core/host_shaders/convert_d24s8_to_r16g16.fragdeleted file mode 100644index c48a7ac660..0000000000--- a/src/video_core/host_shaders/convert_d24s8_to_r16g16.frag+++ /dev/null@@ -1,22 +0,0 @@-// Copyright 2021 yuzu Emulator Project-// Licensed under GPLv2 or any later version-// Refer to the license.txt file included.--#version 450--layout(binding = 0) uniform sampler2D depth_tex;-layout(binding = 1) uniform isampler2D stencil_tex;--layout(location = 0) out vec4 color;--void main() {- ivec2 coord = ivec2(gl_FragCoord.xy);- uint depth = uint(textureLod(depth_tex, coord, 0).r * (exp2(32.0) - 1.0f));- uint stencil = uint(textureLod(stencil_tex, coord, 0).r);- uint depth_stencil = (stencil << 24) | (depth >> 8);-- color.r = float(depth_stencil & 0x0000FFFFu) / (exp2(16) - 1.0);- color.g = float(depth_stencil >> 16) / (exp2(16) - 1.0);- color.b = 0.0f;- color.a = 1.0f;-}diff --git a/src/video_core/host_shaders/convert_r16g16_to_d24s8.frag b/src/video_core/host_shaders/convert_r16g16_to_d24s8.fragdeleted file mode 100644index beb2d12848..0000000000--- a/src/video_core/host_shaders/convert_r16g16_to_d24s8.frag+++ /dev/null@@ -1,19 +0,0 @@-// Copyright 2021 yuzu Emulator Project-// Licensed under GPLv2 or any later version-// Refer to the license.txt file included.--#version 450-#extension GL_ARB_shader_stencil_export : require--layout(binding = 0) uniform sampler2D color_texture;--void main() {- ivec2 coord = ivec2(gl_FragCoord.xy);- vec4 color = texelFetch(color_texture, coord, 0).rgba;- uvec2 bytes = uvec2(color.rg * (exp2(16) - 1.0f)) << uvec2(0, 16);- uint depth_stencil_unorm =- uint(color.r * (exp2(16) - 1.0f)) | (uint(color.g * (exp2(16) - 1.0f)) << 16);-- gl_FragDepth = float(depth_stencil_unorm & 0x00FFFFFFu) / (exp2(24.0) - 1.0f);- gl_FragStencilRefARB = int(depth_stencil_unorm >> 24);-}diff --git a/src/video_core/renderer_vulkan/blit_image.cpp b/src/video_core/renderer_vulkan/blit_image.cppindex 28b631f73f..2e69e270fb 100644--- a/src/video_core/renderer_vulkan/blit_image.cpp+++ b/src/video_core/renderer_vulkan/blit_image.cpp@@ -5,13 +5,9 @@#include <algorithm>#include "video_core/host_shaders/convert_abgr8_to_d24s8_frag_spv.h"-#include "video_core/host_shaders/convert_b10g11r11_to_d24s8_frag_spv.h"#include "video_core/host_shaders/convert_d24s8_to_abgr8_frag_spv.h"-#include "video_core/host_shaders/convert_d24s8_to_b10g11r11_frag_spv.h"-#include "video_core/host_shaders/convert_d24s8_to_r16g16_frag_spv.h"#include "video_core/host_shaders/convert_depth_to_float_frag_spv.h"#include "video_core/host_shaders/convert_float_to_depth_frag_spv.h"-#include "video_core/host_shaders/convert_r16g16_to_d24s8_frag_spv.h"#include "video_core/host_shaders/full_screen_triangle_vert_spv.h"#include "video_core/host_shaders/vulkan_blit_color_float_frag_spv.h"#include "video_core/host_shaders/vulkan_blit_depth_stencil_frag_spv.h"@@ -361,11 +357,7 @@ BlitImageHelper::BlitImageHelper(const Device& device_, VKScheduler& scheduler_,convert_depth_to_float_frag(BuildShader(device, CONVERT_DEPTH_TO_FLOAT_FRAG_SPV)),convert_float_to_depth_frag(BuildShader(device, CONVERT_FLOAT_TO_DEPTH_FRAG_SPV)),convert_abgr8_to_d24s8_frag(BuildShader(device, CONVERT_ABGR8_TO_D24S8_FRAG_SPV)),- convert_b10g11r11_to_d24s8_frag(BuildShader(device, CONVERT_B10G11R11_TO_D24S8_FRAG_SPV)),- convert_r16g16_to_d24s8_frag(BuildShader(device, CONVERT_R16G16_TO_D24S8_FRAG_SPV)),convert_d24s8_to_abgr8_frag(BuildShader(device, CONVERT_D24S8_TO_ABGR8_FRAG_SPV)),- convert_d24s8_to_b10g11r11_frag(BuildShader(device, CONVERT_D24S8_TO_B10G11R11_FRAG_SPV)),- convert_d24s8_to_r16g16_frag(BuildShader(device, CONVERT_D24S8_TO_R16G16_FRAG_SPV)),linear_sampler(device.GetLogical().CreateSampler(SAMPLER_CREATE_INFO<VK_FILTER_LINEAR>)),nearest_sampler(device.GetLogical().CreateSampler(SAMPLER_CREATE_INFO<VK_FILTER_NEAREST>)) {if (device.IsExtShaderStencilExportSupported()) {@@ -461,30 +453,11 @@ void BlitImageHelper::ConvertR16ToD16(const Framebuffer* dst_framebuffer,}void BlitImageHelper::ConvertABGR8ToD24S8(const Framebuffer* dst_framebuffer,- const ImageView& src_image_view, u32 up_scale,- u32 down_shift) {+ ImageView& src_image_view, u32 up_scale, u32 down_shift) {ConvertPipelineDepthTargetEx(convert_abgr8_to_d24s8_pipeline, dst_framebuffer->RenderPass(),convert_abgr8_to_d24s8_frag, true);- Convert(*convert_abgr8_to_d24s8_pipeline, dst_framebuffer, src_image_view, up_scale,- down_shift);-}--void BlitImageHelper::ConvertB10G11R11ToD24S8(const Framebuffer* dst_framebuffer,- const ImageView& src_image_view, u32 up_scale,- u32 down_shift) {- ConvertPipelineDepthTargetEx(convert_b10g11r11_to_d24s8_pipeline, dst_framebuffer->RenderPass(),- convert_b10g11r11_to_d24s8_frag, true);- Convert(*convert_b10g11r11_to_d24s8_pipeline, dst_framebuffer, src_image_view, up_scale,- down_shift);-}--void BlitImageHelper::ConvertR16G16ToD24S8(const Framebuffer* dst_framebuffer,- const ImageView& src_image_view, u32 up_scale,- u32 down_shift) {- ConvertPipelineDepthTargetEx(convert_r16g16_to_d24s8_pipeline, dst_framebuffer->RenderPass(),- convert_r16g16_to_d24s8_frag, true);- Convert(*convert_r16g16_to_d24s8_pipeline, dst_framebuffer, src_image_view, up_scale,- down_shift);+ ConvertColor(*convert_abgr8_to_d24s8_pipeline, dst_framebuffer, src_image_view, up_scale,+ down_shift);}void BlitImageHelper::ConvertD24S8ToABGR8(const Framebuffer* dst_framebuffer,@@ -495,24 +468,6 @@ void BlitImageHelper::ConvertD24S8ToABGR8(const Framebuffer* dst_framebuffer,down_shift);}-void BlitImageHelper::ConvertD24S8ToB10G11R11(const Framebuffer* dst_framebuffer,- ImageView& src_image_view, u32 up_scale,- u32 down_shift) {- ConvertPipelineColorTargetEx(convert_d24s8_to_b10g11r11_pipeline, dst_framebuffer->RenderPass(),- convert_d24s8_to_b10g11r11_frag, false);- ConvertDepthStencil(*convert_d24s8_to_b10g11r11_pipeline, dst_framebuffer, src_image_view,- up_scale, down_shift);-}--void BlitImageHelper::ConvertD24S8ToR16G16(const Framebuffer* dst_framebuffer,- ImageView& src_image_view, u32 up_scale,- u32 down_shift) {- ConvertPipelineColorTargetEx(convert_d24s8_to_r16g16_pipeline, dst_framebuffer->RenderPass(),- convert_d24s8_to_r16g16_frag, false);- ConvertDepthStencil(*convert_d24s8_to_r16g16_pipeline, dst_framebuffer, src_image_view,- up_scale, down_shift);-}-void BlitImageHelper::Convert(VkPipeline pipeline, const Framebuffer* dst_framebuffer,const ImageView& src_image_view, u32 up_scale, u32 down_shift) {const VkPipelineLayout layout = *one_texture_pipeline_layout;@@ -560,6 +515,53 @@ void BlitImageHelper::Convert(VkPipeline pipeline, const Framebuffer* dst_framebscheduler.InvalidateState();}+void BlitImageHelper::ConvertColor(VkPipeline pipeline, const Framebuffer* dst_framebuffer,+ ImageView& src_image_view, u32 up_scale, u32 down_shift) {+ const VkPipelineLayout layout = *one_texture_pipeline_layout;+ const VkImageView src_view = src_image_view.ColorView();+ const VkSampler sampler = *nearest_sampler;+ const VkExtent2D extent{+ .width = std::max((src_image_view.size.width * up_scale) >> down_shift, 1U),+ .height = std::max((src_image_view.size.height * up_scale) >> down_shift, 1U),+ };+ scheduler.RequestRenderpass(dst_framebuffer);+ scheduler.Record([pipeline, layout, sampler, src_view, extent, up_scale, down_shift,+ this](vk::CommandBuffer cmdbuf) {+ const VkOffset2D offset{+ .x = 0,+ .y = 0,+ };+ const VkViewport viewport{+ .x = 0.0f,+ .y = 0.0f,+ .width = static_cast<float>(extent.width),+ .height = static_cast<float>(extent.height),+ .minDepth = 0.0f,+ .maxDepth = 0.0f,+ };+ const VkRect2D scissor{+ .offset = offset,+ .extent = extent,+ };+ const PushConstants push_constants{+ .tex_scale = {viewport.width, viewport.height},+ .tex_offset = {0.0f, 0.0f},+ };+ const VkDescriptorSet descriptor_set = one_texture_descriptor_allocator.Commit();+ UpdateOneTextureDescriptorSet(device, descriptor_set, sampler, src_view);++ // TODO: Barriers+ cmdbuf.BindPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);+ cmdbuf.BindDescriptorSets(VK_PIPELINE_BIND_POINT_GRAPHICS, layout, 0, descriptor_set,+ nullptr);+ cmdbuf.SetViewport(0, viewport);+ cmdbuf.SetScissor(0, scissor);+ cmdbuf.PushConstants(layout, VK_SHADER_STAGE_VERTEX_BIT, push_constants);+ cmdbuf.Draw(3, 1, 0, 0);+ });+ scheduler.InvalidateState();+}+void BlitImageHelper::ConvertDepthStencil(VkPipeline pipeline, const Framebuffer* dst_framebuffer,ImageView& src_image_view, u32 up_scale, u32 down_shift) {const VkPipelineLayout layout = *two_textures_pipeline_layout;diff --git a/src/video_core/renderer_vulkan/blit_image.h b/src/video_core/renderer_vulkan/blit_image.hindex cec0953413..0b73cf444e 100644--- a/src/video_core/renderer_vulkan/blit_image.h+++ b/src/video_core/renderer_vulkan/blit_image.h@@ -56,28 +56,19 @@ public:void ConvertR16ToD16(const Framebuffer* dst_framebuffer, const ImageView& src_image_view,u32 up_scale, u32 down_shift);- void ConvertABGR8ToD24S8(const Framebuffer* dst_framebuffer, const ImageView& src_image_view,+ void ConvertABGR8ToD24S8(const Framebuffer* dst_framebuffer, ImageView& src_image_view,u32 up_scale, u32 down_shift);- void ConvertB10G11R11ToD24S8(const Framebuffer* dst_framebuffer,- const ImageView& src_image_view, u32 up_scale, u32 down_shift);-- void ConvertR16G16ToD24S8(const Framebuffer* dst_framebuffer, const ImageView& src_image_view,- u32 up_scale, u32 down_shift);-void ConvertD24S8ToABGR8(const Framebuffer* dst_framebuffer, ImageView& src_image_view,u32 up_scale, u32 down_shift);- void ConvertD24S8ToB10G11R11(const Framebuffer* dst_framebuffer, ImageView& src_image_view,- u32 up_scale, u32 down_shift);-- void ConvertD24S8ToR16G16(const Framebuffer* dst_framebuffer, ImageView& src_image_view,- u32 up_scale, u32 down_shift);-private:void Convert(VkPipeline pipeline, const Framebuffer* dst_framebuffer,const ImageView& src_image_view, u32 up_scale, u32 down_shift);+ void ConvertColor(VkPipeline pipeline, const Framebuffer* dst_framebuffer,+ ImageView& src_image_view, u32 up_scale, u32 down_shift);+void ConvertDepthStencil(VkPipeline pipeline, const Framebuffer* dst_framebuffer,ImageView& src_image_view, u32 up_scale, u32 down_shift);@@ -111,11 +102,7 @@ private:vk::ShaderModule convert_depth_to_float_frag;vk::ShaderModule convert_float_to_depth_frag;vk::ShaderModule convert_abgr8_to_d24s8_frag;- vk::ShaderModule convert_b10g11r11_to_d24s8_frag;- vk::ShaderModule convert_r16g16_to_d24s8_frag;vk::ShaderModule convert_d24s8_to_abgr8_frag;- vk::ShaderModule convert_d24s8_to_b10g11r11_frag;- vk::ShaderModule convert_d24s8_to_r16g16_frag;vk::Sampler linear_sampler;vk::Sampler nearest_sampler;@@ -128,11 +115,7 @@ private:vk::Pipeline convert_d16_to_r16_pipeline;vk::Pipeline convert_r16_to_d16_pipeline;vk::Pipeline convert_abgr8_to_d24s8_pipeline;- vk::Pipeline convert_b10g11r11_to_d24s8_pipeline;- vk::Pipeline convert_r16g16_to_d24s8_pipeline;vk::Pipeline convert_d24s8_to_abgr8_pipeline;- vk::Pipeline convert_d24s8_to_b10g11r11_pipeline;- vk::Pipeline convert_d24s8_to_r16g16_pipeline;};} // namespace Vulkandiff --git a/src/video_core/renderer_vulkan/vk_texture_cache.cpp b/src/video_core/renderer_vulkan/vk_texture_cache.cppindex e1ba1bdaf2..ef8ae6cb60 100644--- a/src/video_core/renderer_vulkan/vk_texture_cache.cpp+++ b/src/video_core/renderer_vulkan/vk_texture_cache.cpp@@ -1063,21 +1063,10 @@ void TextureCacheRuntime::ConvertImage(Framebuffer* dst, ImageView& dst_view, Im}break;case PixelFormat::A8B8G8R8_UNORM:- case PixelFormat::B8G8R8A8_UNORM:if (src_view.format == PixelFormat::S8_UINT_D24_UNORM) {return blit_image_helper.ConvertD24S8ToABGR8(dst, src_view, up_scale, down_shift);}break;- case PixelFormat::B10G11R11_FLOAT:- if (src_view.format == PixelFormat::S8_UINT_D24_UNORM) {- return blit_image_helper.ConvertD24S8ToB10G11R11(dst, src_view, up_scale, down_shift);- }- break;- case PixelFormat::R16G16_UNORM:- if (src_view.format == PixelFormat::S8_UINT_D24_UNORM) {- return blit_image_helper.ConvertD24S8ToR16G16(dst, src_view, up_scale, down_shift);- }- break;case PixelFormat::R32_FLOAT:if (src_view.format == PixelFormat::D32_FLOAT) {return blit_image_helper.ConvertD32ToR32(dst, src_view, up_scale, down_shift);@@ -1089,16 +1078,7 @@ void TextureCacheRuntime::ConvertImage(Framebuffer* dst, ImageView& dst_view, Im}break;case PixelFormat::S8_UINT_D24_UNORM:- if (src_view.format == PixelFormat::A8B8G8R8_UNORM ||- src_view.format == PixelFormat::B8G8R8A8_UNORM) {- return blit_image_helper.ConvertABGR8ToD24S8(dst, src_view, up_scale, down_shift);- }- if (src_view.format == PixelFormat::B10G11R11_FLOAT) {- return blit_image_helper.ConvertB10G11R11ToD24S8(dst, src_view, up_scale, down_shift);- }- if (src_view.format == PixelFormat::R16G16_UNORM) {- return blit_image_helper.ConvertR16G16ToD24S8(dst, src_view, up_scale, down_shift);- }+ return blit_image_helper.ConvertABGR8ToD24S8(dst, src_view, up_scale, down_shift);break;case PixelFormat::D32_FLOAT:if (src_view.format == PixelFormat::R32_FLOAT) {@@ -1595,6 +1575,14 @@ VkImageView ImageView::StencilView() {return *stencil_view;}+VkImageView ImageView::ColorView() {+ if (color_view) {+ return *color_view;+ }+ color_view = MakeView(VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_ASPECT_COLOR_BIT);+ return *color_view;+}+VkImageView ImageView::StorageView(Shader::TextureType texture_type,Shader::ImageFormat image_format) {if (image_format == Shader::ImageFormat::Typeless) {diff --git a/src/video_core/renderer_vulkan/vk_texture_cache.h b/src/video_core/renderer_vulkan/vk_texture_cache.hindex 44e9dcee42..753e3e8a1e 100644--- a/src/video_core/renderer_vulkan/vk_texture_cache.h+++ b/src/video_core/renderer_vulkan/vk_texture_cache.h@@ -184,6 +184,8 @@ public:[[nodiscard]] VkImageView StencilView();+ [[nodiscard]] VkImageView ColorView();+[[nodiscard]] VkImageView StorageView(Shader::TextureType texture_type,Shader::ImageFormat image_format);@@ -224,6 +226,7 @@ private:std::unique_ptr<StorageViews> storage_views;vk::ImageView depth_view;vk::ImageView stencil_view;+ vk::ImageView color_view;VkImage image_handle = VK_NULL_HANDLE;VkImageView render_target = VK_NULL_HANDLE;VkSampleCountFlagBits samples = VK_SAMPLE_COUNT_1_BIT;diff --git a/src/video_core/texture_cache/texture_cache.h b/src/video_core/texture_cache/texture_cache.hindex 0e4907c53a..9548abec8a 100644--- a/src/video_core/texture_cache/texture_cache.h+++ b/src/video_core/texture_cache/texture_cache.h@@ -1781,7 +1781,13 @@ void TextureCache<P>::CopyImage(ImageId dst_id, ImageId src_id, std::vector<Imagconst SubresourceExtent src_extent{.levels = 1, .layers = 1};const SubresourceRange dst_range{.base = dst_base, .extent = dst_extent};const SubresourceRange src_range{.base = src_base, .extent = src_extent};- const ImageViewInfo dst_view_info(ImageViewType::e2D, dst.info.format, dst_range);+ PixelFormat dst_format = dst.info.format;+ if (GetFormatType(src.info.format) == SurfaceType::DepthStencil &&+ GetFormatType(dst_format) == SurfaceType::ColorTexture &&+ BytesPerBlock(dst_format) == 4) {+ dst_format = PixelFormat::A8B8G8R8_UNORM;+ }+ const ImageViewInfo dst_view_info(ImageViewType::e2D, dst_format, dst_range);const ImageViewInfo src_view_info(ImageViewType::e2D, src.info.format, src_range);const auto [dst_framebuffer_id, dst_view_id] = RenderTargetFromImage(dst_id, dst_view_info);Framebuffer* const dst_framebuffer = &slot_framebuffers[dst_framebuffer_id];