From 5f1c880805c4d6b8ecf9c49fe867e5c16a6d3b0f Mon Sep 17 00:00:00 2001 From: Isaac Marovitz Date: Wed, 8 May 2024 23:08:42 -0400 Subject: [PATCH] Image Subclasses --- .../Image/ITextureInfo.cs | 4 +- .../Image/TextureCopy.cs | 8 +- .../Image/TextureCopyIncompatible.cs | 66 +++++++-------- .../Image/TextureCopyMS.cs | 80 ++++++++++--------- .../Image/TextureStorage.cs | 78 +++++++++--------- 5 files changed, 121 insertions(+), 115 deletions(-) diff --git a/src/Ryujinx.Graphics.OpenGL/Image/ITextureInfo.cs b/src/Ryujinx.Graphics.OpenGL/Image/ITextureInfo.cs index 464d005bf..d422942e8 100644 --- a/src/Ryujinx.Graphics.OpenGL/Image/ITextureInfo.cs +++ b/src/Ryujinx.Graphics.OpenGL/Image/ITextureInfo.cs @@ -6,8 +6,8 @@ namespace Ryujinx.Graphics.OpenGL.Image { ITextureInfo Storage { get; } uint Handle { get; } - int FirstLayer => 0; - int FirstLevel => 0; + uint FirstLayer => 0; + uint FirstLevel => 0; TextureCreateInfo Info { get; } } diff --git a/src/Ryujinx.Graphics.OpenGL/Image/TextureCopy.cs b/src/Ryujinx.Graphics.OpenGL/Image/TextureCopy.cs index 5ddf023bc..3f2bc7334 100644 --- a/src/Ryujinx.Graphics.OpenGL/Image/TextureCopy.cs +++ b/src/Ryujinx.Graphics.OpenGL/Image/TextureCopy.cs @@ -245,16 +245,16 @@ namespace Ryujinx.Graphics.OpenGL.Image _api.CopyImageSubData( src.Storage.Handle, src.Storage.Info.Target.ConvertToImageTarget(), - src.FirstLevel + srcLevel + level, + (int)src.FirstLevel + srcLevel + level, 0, 0, - src.FirstLayer + srcLayer, + (int)src.FirstLayer + srcLayer, dst.Storage.Handle, dst.Storage.Info.Target.ConvertToImageTarget(), - dst.FirstLevel + dstLevel + level, + (int)dst.FirstLevel + dstLevel + level, 0, 0, - dst.FirstLayer + dstLayer, + (int)dst.FirstLayer + dstLayer, (uint)copyWidth, (uint)copyHeight, (uint)depth); diff --git a/src/Ryujinx.Graphics.OpenGL/Image/TextureCopyIncompatible.cs b/src/Ryujinx.Graphics.OpenGL/Image/TextureCopyIncompatible.cs index af6f7b32f..271f66ff5 100644 --- a/src/Ryujinx.Graphics.OpenGL/Image/TextureCopyIncompatible.cs +++ b/src/Ryujinx.Graphics.OpenGL/Image/TextureCopyIncompatible.cs @@ -67,15 +67,17 @@ void main() imageStore(dst, ivec2(coords), uvec4(r, g, b, a)); }"; + private readonly GL _api; private readonly OpenGLRenderer _renderer; - private readonly Dictionary _shorteningProgramHandles; - private readonly Dictionary _wideningProgramHandles; + private readonly Dictionary _shorteningProgramHandles; + private readonly Dictionary _wideningProgramHandles; - public TextureCopyIncompatible(OpenGLRenderer renderer) + public TextureCopyIncompatible(GL api, OpenGLRenderer renderer) { + _api = api; _renderer = renderer; - _shorteningProgramHandles = new Dictionary(); - _wideningProgramHandles = new Dictionary(); + _shorteningProgramHandles = new Dictionary(); + _wideningProgramHandles = new Dictionary(); } public void CopyIncompatibleFormats(ITextureInfo src, ITextureInfo dst, int srcLayer, int dstLayer, int srcLevel, int dstLevel, int depth, int levels) @@ -91,10 +93,10 @@ void main() int srcComponentsCount = srcBpp / componentSize; int dstComponentsCount = dstBpp / componentSize; - var srcFormat = GetFormat(componentSize, srcComponentsCount); - var dstFormat = GetFormat(componentSize, dstComponentsCount); + var srcFormat = (InternalFormat)GetFormat(componentSize, srcComponentsCount); + var dstFormat = (InternalFormat)GetFormat(componentSize, dstComponentsCount); - GL.UseProgram(srcBpp < dstBpp + _api.UseProgram(srcBpp < dstBpp ? GetWideningShader(componentSize, srcComponentsCount, dstComponentsCount) : GetShorteningShader(componentSize, srcComponentsCount, dstComponentsCount)); @@ -106,15 +108,15 @@ void main() int dstWidth = Math.Max(1, dst.Info.Width >> l); int dstHeight = Math.Max(1, dst.Info.Height >> l); - int width = Math.Min(srcWidth, dstWidth); - int height = Math.Min(srcHeight, dstHeight); + uint width = (uint)Math.Min(srcWidth, dstWidth); + uint height = (uint)Math.Min(srcHeight, dstHeight); for (int z = 0; z < depth; z++) { - GL.BindImageTexture(0, src.Handle, srcLevel + l, false, srcLayer + z, BufferAccessARB.ReadOnly, srcFormat); - GL.BindImageTexture(1, dst.Handle, dstLevel + l, false, dstLayer + z, BufferAccessARB.WriteOnly, dstFormat); + _api.BindImageTexture(0, src.Handle, srcLevel + l, false, srcLayer + z, BufferAccessARB.ReadOnly, srcFormat); + _api.BindImageTexture(1, dst.Handle, dstLevel + l, false, dstLayer + z, BufferAccessARB.WriteOnly, dstFormat); - GL.DispatchCompute((width + 31) / 32, (height + 31) / 32, 1); + _api.DispatchCompute((width + 31) / 32, (height + 31) / 32, 1); } } @@ -162,19 +164,19 @@ void main() } } - private int GetShorteningShader(int componentSize, int srcComponentsCount, int dstComponentsCount) + private uint GetShorteningShader(int componentSize, int srcComponentsCount, int dstComponentsCount) { return GetShader(ComputeShaderShortening, _shorteningProgramHandles, componentSize, srcComponentsCount, dstComponentsCount); } - private int GetWideningShader(int componentSize, int srcComponentsCount, int dstComponentsCount) + private uint GetWideningShader(int componentSize, int srcComponentsCount, int dstComponentsCount) { return GetShader(ComputeShaderWidening, _wideningProgramHandles, componentSize, srcComponentsCount, dstComponentsCount); } - private static int GetShader( + private uint GetShader( string code, - Dictionary programHandles, + Dictionary programHandles, int componentSize, int srcComponentsCount, int dstComponentsCount) @@ -186,9 +188,9 @@ void main() int key = srcIndex | (dstIndex << 8); - if (!programHandles.TryGetValue(key, out int programHandle)) + if (!programHandles.TryGetValue(key, out uint programHandle)) { - int csHandle = GL.CreateShader(ShaderType.ComputeShader); + uint csHandle = _api.CreateShader(ShaderType.ComputeShader); string[] formatTable = new[] { "r8ui", "r16ui", "r32ui", "rg8ui", "rg16ui", "rg32ui", "rgba8ui", "rgba16ui", "rgba32ui" }; @@ -201,25 +203,25 @@ void main() int ratio = srcBpp < dstBpp ? dstBpp / srcBpp : srcBpp / dstBpp; int ratioLog2 = BitOperations.Log2((uint)ratio); - GL.ShaderSource(csHandle, code + _api.ShaderSource(csHandle, code .Replace("$SRC_FORMAT$", srcFormat) .Replace("$DST_FORMAT$", dstFormat) .Replace("$RATIO_LOG2$", ratioLog2.ToString(CultureInfo.InvariantCulture))); - GL.CompileShader(csHandle); + _api.CompileShader(csHandle); - programHandle = GL.CreateProgram(); + programHandle = _api.CreateProgram(); - GL.AttachShader(programHandle, csHandle); - GL.LinkProgram(programHandle); - GL.DetachShader(programHandle, csHandle); - GL.DeleteShader(csHandle); + _api.AttachShader(programHandle, csHandle); + _api.LinkProgram(programHandle); + _api.DetachShader(programHandle, csHandle); + _api.DeleteShader(csHandle); - GL.GetProgram(programHandle, GetProgramParameterName.LinkStatus, out int status); + _api.GetProgram(programHandle, ProgramPropertyARB.LinkStatus, out int status); if (status == 0) { - throw new Exception(GL.GetProgramInfoLog(programHandle)); + throw new Exception(_api.GetProgramInfoLog(programHandle)); } programHandles.Add(key, programHandle); @@ -230,16 +232,16 @@ void main() public void Dispose() { - foreach (int handle in _shorteningProgramHandles.Values) + foreach (uint handle in _shorteningProgramHandles.Values) { - GL.DeleteProgram(handle); + _api.DeleteProgram(handle); } _shorteningProgramHandles.Clear(); - foreach (int handle in _wideningProgramHandles.Values) + foreach (uint handle in _wideningProgramHandles.Values) { - GL.DeleteProgram(handle); + _api.DeleteProgram(handle); } _wideningProgramHandles.Clear(); diff --git a/src/Ryujinx.Graphics.OpenGL/Image/TextureCopyMS.cs b/src/Ryujinx.Graphics.OpenGL/Image/TextureCopyMS.cs index 0764382a1..473fd44bd 100644 --- a/src/Ryujinx.Graphics.OpenGL/Image/TextureCopyMS.cs +++ b/src/Ryujinx.Graphics.OpenGL/Image/TextureCopyMS.cs @@ -93,12 +93,14 @@ void main() imageStore(imgOut, ivec2(int(coords.x) >> samplesInXLog2, int(coords.y) >> samplesInYLog2), sampleIdx, value); }"; + private readonly GL _api; private readonly OpenGLRenderer _renderer; private readonly uint[] _msToNonMSProgramHandles; private readonly uint[] _nonMSToMSProgramHandles; - public TextureCopyMS(OpenGLRenderer renderer) + public TextureCopyMS(GL api, OpenGLRenderer renderer) { + _api = api; _renderer = renderer; _msToNonMSProgramHandles = new uint[5]; _nonMSToMSProgramHandles = new uint[5]; @@ -109,20 +111,20 @@ void main() TextureCreateInfo srcInfo = src.Info; TextureCreateInfo dstInfo = dst.Info; - int srcHandle = CreateViewIfNeeded(src); - int dstHandle = CreateViewIfNeeded(dst); + uint srcHandle = CreateViewIfNeeded(src); + uint dstHandle = CreateViewIfNeeded(dst); - int dstWidth = dstInfo.Width; - int dstHeight = dstInfo.Height; + uint dstWidth = (uint)dstInfo.Width; + uint dstHeight = (uint)dstInfo.Height; - GL.UseProgram(GetMSToNonMSShader(srcInfo.BytesPerPixel)); + _api.UseProgram(GetMSToNonMSShader(srcInfo.BytesPerPixel)); for (int z = 0; z < depth; z++) { - GL.BindImageTexture(0, srcHandle, 0, false, srcLayer + z, BufferAccessARB.ReadOnly, GetFormat(srcInfo.BytesPerPixel)); - GL.BindImageTexture(1, dstHandle, 0, false, dstLayer + z, BufferAccessARB.WriteOnly, GetFormat(dstInfo.BytesPerPixel)); + _api.BindImageTexture(0, srcHandle, 0, false, srcLayer + z, BufferAccessARB.ReadOnly, (InternalFormat)GetFormat(srcInfo.BytesPerPixel)); + _api.BindImageTexture(1, dstHandle, 0, false, dstLayer + z, BufferAccessARB.WriteOnly, (InternalFormat)GetFormat(dstInfo.BytesPerPixel)); - GL.DispatchCompute((dstWidth + 31) / 32, (dstHeight + 31) / 32, 1); + _api.DispatchCompute((dstWidth + 31) / 32, (dstHeight + 31) / 32, 1); } Pipeline pipeline = (Pipeline)_renderer.Pipeline; @@ -139,20 +141,20 @@ void main() TextureCreateInfo srcInfo = src.Info; TextureCreateInfo dstInfo = dst.Info; - int srcHandle = CreateViewIfNeeded(src); - int dstHandle = CreateViewIfNeeded(dst); + uint srcHandle = CreateViewIfNeeded(src); + uint dstHandle = CreateViewIfNeeded(dst); - int srcWidth = srcInfo.Width; - int srcHeight = srcInfo.Height; + uint srcWidth = (uint)srcInfo.Width; + uint srcHeight = (uint)srcInfo.Height; - GL.UseProgram(GetNonMSToMSShader(srcInfo.BytesPerPixel)); + _api.UseProgram(GetNonMSToMSShader(srcInfo.BytesPerPixel)); for (int z = 0; z < depth; z++) { - GL.BindImageTexture(0, srcHandle, 0, false, srcLayer + z, BufferAccessARB.ReadOnly, GetFormat(srcInfo.BytesPerPixel)); - GL.BindImageTexture(1, dstHandle, 0, false, dstLayer + z, BufferAccessARB.WriteOnly, GetFormat(dstInfo.BytesPerPixel)); + _api.BindImageTexture(0, srcHandle, 0, false, srcLayer + z, BufferAccessARB.ReadOnly, (InternalFormat)GetFormat(srcInfo.BytesPerPixel)); + _api.BindImageTexture(1, dstHandle, 0, false, dstLayer + z, BufferAccessARB.WriteOnly, (InternalFormat)GetFormat(dstInfo.BytesPerPixel)); - GL.DispatchCompute((srcWidth + 31) / 32, (srcHeight + 31) / 32, 1); + _api.DispatchCompute((srcWidth + 31) / 32, (srcHeight + 31) / 32, 1); } Pipeline pipeline = (Pipeline)_renderer.Pipeline; @@ -177,23 +179,23 @@ void main() }; } - private static uint CreateViewIfNeeded(ITextureInfo texture) + private uint CreateViewIfNeeded(ITextureInfo texture) { // Binding sRGB textures as images doesn't work on NVIDIA, // we need to create and bind a RGBA view for it to work. if (texture.Info.Format == Format.R8G8B8A8Srgb) { - int handle = GL.GenTexture(); + uint handle = _api.GenTexture(); - GL.TextureView( + _api.TextureView( handle, texture.Info.Target.Convert(), texture.Storage.Handle, - InternalFormat.Rgba8, + SizedInternalFormat.Rgba8, texture.FirstLevel, 1, texture.FirstLayer, - texture.Info.GetLayers()); + (uint)texture.Info.GetLayers()); return handle; } @@ -201,49 +203,49 @@ void main() return texture.Handle; } - private static void DestroyViewIfNeeded(ITextureInfo info, int handle) + private void DestroyViewIfNeeded(ITextureInfo info, uint handle) { if (info.Handle != handle) { - GL.DeleteTexture(handle); + _api.DeleteTexture(handle); } } - private int GetMSToNonMSShader(int bytesPerPixel) + private uint GetMSToNonMSShader(int bytesPerPixel) { return GetShader(ComputeShaderMSToNonMS, _msToNonMSProgramHandles, bytesPerPixel); } - private int GetNonMSToMSShader(int bytesPerPixel) + private uint GetNonMSToMSShader(int bytesPerPixel) { return GetShader(ComputeShaderNonMSToMS, _nonMSToMSProgramHandles, bytesPerPixel); } - private static int GetShader(string code, int[] programHandles, int bytesPerPixel) + private uint GetShader(string code, uint[] programHandles, int bytesPerPixel) { int index = BitOperations.Log2((uint)bytesPerPixel); if (programHandles[index] == 0) { - int csHandle = GL.CreateShader(ShaderType.ComputeShader); + uint csHandle = _api.CreateShader(ShaderType.ComputeShader); string format = new[] { "r8ui", "r16ui", "r32ui", "rg32ui", "rgba32ui" }[index]; - GL.ShaderSource(csHandle, code.Replace("$FORMAT$", format)); - GL.CompileShader(csHandle); + _api.ShaderSource(csHandle, code.Replace("$FORMAT$", format)); + _api.CompileShader(csHandle); - int programHandle = GL.CreateProgram(); + uint programHandle = _api.CreateProgram(); - GL.AttachShader(programHandle, csHandle); - GL.LinkProgram(programHandle); - GL.DetachShader(programHandle, csHandle); - GL.DeleteShader(csHandle); + _api.AttachShader(programHandle, csHandle); + _api.LinkProgram(programHandle); + _api.DetachShader(programHandle, csHandle); + _api.DeleteShader(csHandle); - GL.GetProgram(programHandle, GetProgramParameterName.LinkStatus, out int status); + _api.GetProgram(programHandle, ProgramPropertyARB.LinkStatus, out int status); if (status == 0) { - throw new Exception(GL.GetProgramInfoLog(programHandle)); + throw new Exception(_api.GetProgramInfoLog(programHandle)); } programHandles[index] = programHandle; @@ -258,7 +260,7 @@ void main() { if (_msToNonMSProgramHandles[i] != 0) { - GL.DeleteProgram(_msToNonMSProgramHandles[i]); + _api.DeleteProgram(_msToNonMSProgramHandles[i]); _msToNonMSProgramHandles[i] = 0; } } @@ -267,7 +269,7 @@ void main() { if (_nonMSToMSProgramHandles[i] != 0) { - GL.DeleteProgram(_nonMSToMSProgramHandles[i]); + _api.DeleteProgram(_nonMSToMSProgramHandles[i]); _nonMSToMSProgramHandles[i] = 0; } } diff --git a/src/Ryujinx.Graphics.OpenGL/Image/TextureStorage.cs b/src/Ryujinx.Graphics.OpenGL/Image/TextureStorage.cs index 5294fc212..8c0be4b7d 100644 --- a/src/Ryujinx.Graphics.OpenGL/Image/TextureStorage.cs +++ b/src/Ryujinx.Graphics.OpenGL/Image/TextureStorage.cs @@ -12,17 +12,19 @@ namespace Ryujinx.Graphics.OpenGL.Image public TextureCreateInfo Info { get; } private readonly OpenGLRenderer _renderer; + private readonly GL _api; private int _viewsCount; internal ITexture DefaultView { get; private set; } - public TextureStorage(OpenGLRenderer renderer, TextureCreateInfo info) + public TextureStorage(GL api, OpenGLRenderer renderer, TextureCreateInfo info) { + _api = api; _renderer = renderer; Info = info; - Handle = GL.GenTexture(); + Handle = _api.GenTexture(); CreateImmutableStorage(); } @@ -31,9 +33,9 @@ namespace Ryujinx.Graphics.OpenGL.Image { TextureTarget target = Info.Target.Convert(); - GL.ActiveTexture(TextureUnit.Texture0); + _api.ActiveTexture(TextureUnit.Texture0); - GL.BindTexture(target, Handle); + _api.BindTexture(target, Handle); FormatInfo format = FormatTable.GetFormatInfo(Info.Format); @@ -48,94 +50,94 @@ namespace Ryujinx.Graphics.OpenGL.Image internalFormat = (SizedInternalFormat)format.InternalFormat; } - int levels = Info.GetLevelsClamped(); + uint levels = (uint)Info.GetLevelsClamped(); switch (Info.Target) { case Target.Texture1D: - GL.TexStorage1D( + _api.TexStorage1D( TextureTarget.Texture1D, levels, internalFormat, - Info.Width); + (uint)Info.Width); break; case Target.Texture1DArray: - GL.TexStorage2D( + _api.TexStorage2D( TextureTarget.Texture1DArray, levels, internalFormat, - Info.Width, - Info.Height); + (uint)Info.Width, + (uint)Info.Height); break; case Target.Texture2D: - GL.TexStorage2D( + _api.TexStorage2D( TextureTarget.Texture2D, levels, internalFormat, - Info.Width, - Info.Height); + (uint)Info.Width, + (uint)Info.Height); break; case Target.Texture2DArray: - GL.TexStorage3D( + _api.TexStorage3D( TextureTarget.Texture2DArray, levels, internalFormat, - Info.Width, - Info.Height, - Info.Depth); + (uint)Info.Width, + (uint)Info.Height, + (uint)Info.Depth); break; case Target.Texture2DMultisample: - GL.TexStorage2DMultisample( + _api.TexStorage2DMultisample( TextureTarget.Texture2DMultisample, - Info.Samples, + (uint)Info.Samples, internalFormat, - Info.Width, - Info.Height, + (uint)Info.Width, + (uint)Info.Height, true); break; case Target.Texture2DMultisampleArray: - GL.TexStorage3DMultisample( + _api.TexStorage3DMultisample( TextureTarget.Texture2DMultisampleArray, - Info.Samples, + (uint)Info.Samples, internalFormat, - Info.Width, - Info.Height, - Info.Depth, + (uint)Info.Width, + (uint)Info.Height, + (uint)Info.Depth, true); break; case Target.Texture3D: - GL.TexStorage3D( + _api.TexStorage3D( TextureTarget.Texture3D, levels, internalFormat, - Info.Width, - Info.Height, - Info.Depth); + (uint)Info.Width, + (uint)Info.Height, + (uint)Info.Depth); break; case Target.Cubemap: - GL.TexStorage2D( + _api.TexStorage2D( TextureTarget.TextureCubeMap, levels, internalFormat, - Info.Width, - Info.Height); + (uint)Info.Width, + (uint)Info.Height); break; case Target.CubemapArray: - GL.TexStorage3D( + _api.TexStorage3D( TextureTarget.TextureCubeMapArray, levels, internalFormat, - Info.Width, - Info.Height, - Info.Depth); + (uint)Info.Width, + (uint)Info.Height, + (uint)Info.Depth); break; default: @@ -201,7 +203,7 @@ namespace Ryujinx.Graphics.OpenGL.Image if (Handle != 0) { - GL.DeleteTexture(Handle); + _api.DeleteTexture(Handle); Handle = 0; }