From 8a352df3c676e347c267919f2d9a56d75daef348 Mon Sep 17 00:00:00 2001 From: gdkchan Date: Sat, 3 Jun 2023 00:43:00 -0300 Subject: [PATCH] Allow BGRA images on Vulkan (#5203) --- src/Ryujinx.Graphics.GAL/Format.cs | 1 + .../Effects/FsrScalingFilter.cs | 8 ++-- .../Effects/FxaaPostProcessingEffect.cs | 25 +----------- .../Effects/SmaaPostProcessingEffect.cs | 38 ++++--------------- src/Ryujinx.Graphics.Vulkan/FormatTable.cs | 10 +++++ 5 files changed, 24 insertions(+), 58 deletions(-) diff --git a/src/Ryujinx.Graphics.GAL/Format.cs b/src/Ryujinx.Graphics.GAL/Format.cs index 5e0274e5..7e0e07d4 100644 --- a/src/Ryujinx.Graphics.GAL/Format.cs +++ b/src/Ryujinx.Graphics.GAL/Format.cs @@ -383,6 +383,7 @@ namespace Ryujinx.Graphics.GAL case Format.R10G10B10A2Unorm: case Format.R10G10B10A2Uint: case Format.R11G11B10Float: + case Format.B8G8R8A8Unorm: return true; } diff --git a/src/Ryujinx.Graphics.Vulkan/Effects/FsrScalingFilter.cs b/src/Ryujinx.Graphics.Vulkan/Effects/FsrScalingFilter.cs index e9952126..7317b567 100644 --- a/src/Ryujinx.Graphics.Vulkan/Effects/FsrScalingFilter.cs +++ b/src/Ryujinx.Graphics.Vulkan/Effects/FsrScalingFilter.cs @@ -96,8 +96,6 @@ namespace Ryujinx.Graphics.Vulkan.Effects { var originalInfo = view.Info; - var swapRB = originalInfo.Format.IsBgr() && originalInfo.SwizzleR == SwizzleComponent.Red; - var info = new TextureCreateInfo( width, height, @@ -110,9 +108,9 @@ namespace Ryujinx.Graphics.Vulkan.Effects originalInfo.Format, originalInfo.DepthStencilMode, originalInfo.Target, - swapRB ? originalInfo.SwizzleB : originalInfo.SwizzleR, + originalInfo.SwizzleR, originalInfo.SwizzleG, - swapRB ? originalInfo.SwizzleR : originalInfo.SwizzleB, + originalInfo.SwizzleB, originalInfo.SwizzleA); _intermediaryTexture?.Dispose(); _intermediaryTexture = _renderer.CreateTexture(info, view.ScaleFactor) as TextureView; @@ -155,7 +153,7 @@ namespace Ryujinx.Graphics.Vulkan.Effects var bufferRanges = new BufferRange(bufferHandle, 0, rangeSize); _pipeline.SetUniformBuffers(stackalloc[] { new BufferAssignment(2, bufferRanges) }); - _pipeline.SetImage(0, _intermediaryTexture, GAL.Format.R8G8B8A8Unorm); + _pipeline.SetImage(0, _intermediaryTexture, FormatTable.ConvertRgba8SrgbToUnorm(view.Info.Format)); _pipeline.DispatchCompute(dispatchX, dispatchY, 1); _pipeline.ComputeBarrier(); diff --git a/src/Ryujinx.Graphics.Vulkan/Effects/FxaaPostProcessingEffect.cs b/src/Ryujinx.Graphics.Vulkan/Effects/FxaaPostProcessingEffect.cs index 9da003dd..3c3516bb 100644 --- a/src/Ryujinx.Graphics.Vulkan/Effects/FxaaPostProcessingEffect.cs +++ b/src/Ryujinx.Graphics.Vulkan/Effects/FxaaPostProcessingEffect.cs @@ -56,28 +56,7 @@ namespace Ryujinx.Graphics.Vulkan.Effects if (_texture == null || _texture.Width != view.Width || _texture.Height != view.Height) { _texture?.Dispose(); - - var info = view.Info; - - if (view.Info.Format.IsBgr()) - { - info = new TextureCreateInfo(info.Width, - info.Height, - info.Depth, - info.Levels, - info.Samples, - info.BlockWidth, - info.BlockHeight, - info.BytesPerPixel, - info.Format, - info.DepthStencilMode, - info.Target, - info.SwizzleB, - info.SwizzleG, - info.SwizzleR, - info.SwizzleA); - } - _texture = _renderer.CreateTexture(info, view.ScaleFactor) as TextureView; + _texture = _renderer.CreateTexture(view.Info, view.ScaleFactor) as TextureView; } _pipeline.SetCommandBuffer(cbs); @@ -96,7 +75,7 @@ namespace Ryujinx.Graphics.Vulkan.Effects var dispatchX = BitUtils.DivRoundUp(view.Width, IPostProcessingEffect.LocalGroupSize); var dispatchY = BitUtils.DivRoundUp(view.Height, IPostProcessingEffect.LocalGroupSize); - _pipeline.SetImage(0, _texture, GAL.Format.R8G8B8A8Unorm); + _pipeline.SetImage(0, _texture, FormatTable.ConvertRgba8SrgbToUnorm(view.Info.Format)); _pipeline.DispatchCompute(dispatchX, dispatchY, 1); _renderer.BufferManager.Delete(bufferHandle); diff --git a/src/Ryujinx.Graphics.Vulkan/Effects/SmaaPostProcessingEffect.cs b/src/Ryujinx.Graphics.Vulkan/Effects/SmaaPostProcessingEffect.cs index 0d392a65..f6de3ac2 100644 --- a/src/Ryujinx.Graphics.Vulkan/Effects/SmaaPostProcessingEffect.cs +++ b/src/Ryujinx.Graphics.Vulkan/Effects/SmaaPostProcessingEffect.cs @@ -4,7 +4,6 @@ using Ryujinx.Graphics.Shader; using Ryujinx.Graphics.Shader.Translation; using Silk.NET.Vulkan; using System; -using Format = Ryujinx.Graphics.GAL.Format; namespace Ryujinx.Graphics.Vulkan.Effects { @@ -149,7 +148,7 @@ namespace Ryujinx.Graphics.Vulkan.Effects 1, 1, 1, - Format.R8G8Unorm, + GAL.Format.R8G8Unorm, DepthStencilMode.Depth, Target.Texture2D, SwizzleComponent.Red, @@ -165,7 +164,7 @@ namespace Ryujinx.Graphics.Vulkan.Effects 1, 1, 1, - Format.R8Unorm, + GAL.Format.R8Unorm, DepthStencilMode.Depth, Target.Texture2D, SwizzleComponent.Red, @@ -192,30 +191,9 @@ namespace Ryujinx.Graphics.Vulkan.Effects _edgeOutputTexture?.Dispose(); _blendOutputTexture?.Dispose(); - var info = view.Info; - - if (view.Info.Format.IsBgr()) - { - info = new TextureCreateInfo(info.Width, - info.Height, - info.Depth, - info.Levels, - info.Samples, - info.BlockWidth, - info.BlockHeight, - info.BytesPerPixel, - info.Format, - info.DepthStencilMode, - info.Target, - info.SwizzleB, - info.SwizzleG, - info.SwizzleR, - info.SwizzleA); - } - - _outputTexture = _renderer.CreateTexture(info, view.ScaleFactor) as TextureView; - _edgeOutputTexture = _renderer.CreateTexture(info, view.ScaleFactor) as TextureView; - _blendOutputTexture = _renderer.CreateTexture(info, view.ScaleFactor) as TextureView; + _outputTexture = _renderer.CreateTexture(view.Info, view.ScaleFactor) as TextureView; + _edgeOutputTexture = _renderer.CreateTexture(view.Info, view.ScaleFactor) as TextureView; + _blendOutputTexture = _renderer.CreateTexture(view.Info, view.ScaleFactor) as TextureView; } _pipeline.SetCommandBuffer(cbs); @@ -240,7 +218,7 @@ namespace Ryujinx.Graphics.Vulkan.Effects _renderer.BufferManager.SetData(bufferHandle, 0, resolutionBuffer); var bufferRanges = new BufferRange(bufferHandle, 0, rangeSize); _pipeline.SetUniformBuffers(stackalloc[] { new BufferAssignment(2, bufferRanges) }); - _pipeline.SetImage(0, _edgeOutputTexture, GAL.Format.R8G8B8A8Unorm); + _pipeline.SetImage(0, _edgeOutputTexture, FormatTable.ConvertRgba8SrgbToUnorm(view.Info.Format)); _pipeline.DispatchCompute(dispatchX, dispatchY, 1); _pipeline.ComputeBarrier(); @@ -250,7 +228,7 @@ namespace Ryujinx.Graphics.Vulkan.Effects _pipeline.SetTextureAndSampler(ShaderStage.Compute, 1, _edgeOutputTexture, _samplerLinear); _pipeline.SetTextureAndSampler(ShaderStage.Compute, 3, _areaTexture, _samplerLinear); _pipeline.SetTextureAndSampler(ShaderStage.Compute, 4, _searchTexture, _samplerLinear); - _pipeline.SetImage(0, _blendOutputTexture, GAL.Format.R8G8B8A8Unorm); + _pipeline.SetImage(0, _blendOutputTexture, FormatTable.ConvertRgba8SrgbToUnorm(view.Info.Format)); _pipeline.DispatchCompute(dispatchX, dispatchY, 1); _pipeline.ComputeBarrier(); @@ -259,7 +237,7 @@ namespace Ryujinx.Graphics.Vulkan.Effects _pipeline.Specialize(_specConstants); _pipeline.SetTextureAndSampler(ShaderStage.Compute, 3, _blendOutputTexture, _samplerLinear); _pipeline.SetTextureAndSampler(ShaderStage.Compute, 1, view, _samplerLinear); - _pipeline.SetImage(0, _outputTexture, GAL.Format.R8G8B8A8Unorm); + _pipeline.SetImage(0, _outputTexture, FormatTable.ConvertRgba8SrgbToUnorm(view.Info.Format)); _pipeline.DispatchCompute(dispatchX, dispatchY, 1); _pipeline.ComputeBarrier(); diff --git a/src/Ryujinx.Graphics.Vulkan/FormatTable.cs b/src/Ryujinx.Graphics.Vulkan/FormatTable.cs index a030d8c8..3d70f6f2 100644 --- a/src/Ryujinx.Graphics.Vulkan/FormatTable.cs +++ b/src/Ryujinx.Graphics.Vulkan/FormatTable.cs @@ -169,6 +169,16 @@ namespace Ryujinx.Graphics.Vulkan return _table[(int)format]; } + public static Format ConvertRgba8SrgbToUnorm(Format format) + { + return format switch + { + Format.R8G8B8A8Srgb => Format.R8G8B8A8Unorm, + Format.B8G8R8A8Srgb => Format.B8G8R8A8Unorm, + _ => format + }; + } + public static int GetAttributeFormatSize(VkFormat format) { switch (format)