From 9003549de1beb0d57a3ab3705da6b056b912107e Mon Sep 17 00:00:00 2001 From: gdkchan Date: Sun, 1 Sep 2024 17:43:29 -0300 Subject: [PATCH] Replace shaderc.net with Silk.NET.Shaderc --- Directory.Packages.props | 2 +- .../Ryujinx.Graphics.Vulkan.csproj | 2 +- src/Ryujinx.Graphics.Vulkan/Shader.cs | 49 ++++++++----------- 3 files changed, 22 insertions(+), 31 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index 301024cf8a..30ab7df560 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -37,8 +37,8 @@ - + diff --git a/src/Ryujinx.Graphics.Vulkan/Ryujinx.Graphics.Vulkan.csproj b/src/Ryujinx.Graphics.Vulkan/Ryujinx.Graphics.Vulkan.csproj index f6a7be91e4..bee450a7e3 100644 --- a/src/Ryujinx.Graphics.Vulkan/Ryujinx.Graphics.Vulkan.csproj +++ b/src/Ryujinx.Graphics.Vulkan/Ryujinx.Graphics.Vulkan.csproj @@ -51,7 +51,7 @@ - + diff --git a/src/Ryujinx.Graphics.Vulkan/Shader.cs b/src/Ryujinx.Graphics.Vulkan/Shader.cs index 1c8caffd9a..ac072e8657 100644 --- a/src/Ryujinx.Graphics.Vulkan/Shader.cs +++ b/src/Ryujinx.Graphics.Vulkan/Shader.cs @@ -1,7 +1,7 @@ using Ryujinx.Common.Logging; using Ryujinx.Graphics.GAL; using Ryujinx.Graphics.Shader; -using shaderc; +using Silk.NET.Shaderc; using Silk.NET.Vulkan; using System; using System.Runtime.InteropServices; @@ -11,10 +11,6 @@ namespace Ryujinx.Graphics.Vulkan { class Shader : IDisposable { - // The shaderc.net dependency's Options constructor and dispose are not thread safe. - // Take this lock when using them. - private static readonly object _shaderOptionsLock = new(); - private static readonly IntPtr _ptrMainEntryPointName = Marshal.StringToHGlobalAnsi("main"); private readonly Vk _api; @@ -73,38 +69,33 @@ namespace Ryujinx.Graphics.Vulkan private unsafe static byte[] GlslToSpirv(string glsl, ShaderStage stage) { - Options options; + var api = Shaderc.GetApi(); + var compiler = api.CompilerInitialize(); + var options = api.CompileOptionsInitialize(); - lock (_shaderOptionsLock) + api.CompileOptionsSetSourceLanguage(options, SourceLanguage.Glsl); + api.CompileOptionsSetTargetSpirv(options, SpirvVersion.Shaderc15); + api.CompileOptionsSetTargetEnv(options, TargetEnv.Vulkan, Vk.Version12); + + var scr = api.CompileIntoSpv(compiler, glsl, (nuint)glsl.Length, GetShaderCShaderStage(stage), "Ryu", "main", options); + + var status = api.ResultGetCompilationStatus(scr); + + if (status != CompilationStatus.Success) { - options = new Options(false) - { - SourceLanguage = SourceLanguage.Glsl, - TargetSpirVVersion = new SpirVVersion(1, 5), - }; - } - - options.SetTargetEnvironment(TargetEnvironment.Vulkan, EnvironmentVersion.Vulkan_1_2); - Compiler compiler = new(options); - var scr = compiler.Compile(glsl, "Ryu", GetShaderCShaderStage(stage)); - - lock (_shaderOptionsLock) - { - options.Dispose(); - } - - if (scr.Status != Status.Success) - { - Logger.Error?.Print(LogClass.Gpu, $"Shader compilation error: {scr.Status} {scr.ErrorMessage}"); + Logger.Error?.Print(LogClass.Gpu, $"Shader compilation error: {status} {api.ResultGetErrorMessageS(scr)}"); return null; } - var spirvBytes = new Span((void*)scr.CodePointer, (int)scr.CodeLength); + var spirvBytes = new Span(api.ResultGetBytes(scr), (int)api.ResultGetLength(scr)); - byte[] code = new byte[(scr.CodeLength + 3) & ~3]; + byte[] code = new byte[(spirvBytes.Length + 3) & ~3]; - spirvBytes.CopyTo(code.AsSpan()[..(int)scr.CodeLength]); + spirvBytes.CopyTo(code.AsSpan()[..spirvBytes.Length]); + + api.CompilerRelease(compiler); + api.CompileOptionsRelease(options); return code; }