aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Graphics.Vulkan/Shader.cs
diff options
context:
space:
mode:
authorTSR Berry <20988865+TSRBerry@users.noreply.github.com>2023-04-08 01:22:00 +0200
committerMary <thog@protonmail.com>2023-04-27 23:51:14 +0200
commitcee712105850ac3385cd0091a923438167433f9f (patch)
tree4a5274b21d8b7f938c0d0ce18736d3f2993b11b1 /Ryujinx.Graphics.Vulkan/Shader.cs
parentcd124bda587ef09668a971fa1cac1c3f0cfc9f21 (diff)
Move solution and projects to src
Diffstat (limited to 'Ryujinx.Graphics.Vulkan/Shader.cs')
-rw-r--r--Ryujinx.Graphics.Vulkan/Shader.cs163
1 files changed, 0 insertions, 163 deletions
diff --git a/Ryujinx.Graphics.Vulkan/Shader.cs b/Ryujinx.Graphics.Vulkan/Shader.cs
deleted file mode 100644
index ca99ebf0..00000000
--- a/Ryujinx.Graphics.Vulkan/Shader.cs
+++ /dev/null
@@ -1,163 +0,0 @@
-using Ryujinx.Common.Logging;
-using Ryujinx.Graphics.GAL;
-using Ryujinx.Graphics.Shader;
-using shaderc;
-using Silk.NET.Vulkan;
-using System;
-using System.Runtime.InteropServices;
-using System.Threading.Tasks;
-
-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 object _shaderOptionsLock = new object();
-
- private static readonly IntPtr _ptrMainEntryPointName = Marshal.StringToHGlobalAnsi("main");
-
- private readonly Vk _api;
- private readonly Device _device;
- private readonly ShaderStageFlags _stage;
-
- private bool _disposed;
- private ShaderModule _module;
-
- public ShaderStageFlags StageFlags => _stage;
-
- public ShaderBindings Bindings { get; }
-
- public ProgramLinkStatus CompileStatus { private set; get; }
-
- public readonly Task CompileTask;
-
- public unsafe Shader(Vk api, Device device, ShaderSource shaderSource)
- {
- _api = api;
- _device = device;
- Bindings = shaderSource.Bindings;
-
- CompileStatus = ProgramLinkStatus.Incomplete;
-
- _stage = shaderSource.Stage.Convert();
-
- CompileTask = Task.Run(() =>
- {
- byte[] spirv = shaderSource.BinaryCode;
-
- if (spirv == null)
- {
- spirv = GlslToSpirv(shaderSource.Code, shaderSource.Stage);
-
- if (spirv == null)
- {
- CompileStatus = ProgramLinkStatus.Failure;
-
- return;
- }
- }
-
- fixed (byte* pCode = spirv)
- {
- var shaderModuleCreateInfo = new ShaderModuleCreateInfo()
- {
- SType = StructureType.ShaderModuleCreateInfo,
- CodeSize = (uint)spirv.Length,
- PCode = (uint*)pCode
- };
-
- api.CreateShaderModule(device, shaderModuleCreateInfo, null, out _module).ThrowOnError();
- }
-
- CompileStatus = ProgramLinkStatus.Success;
- });
- }
-
- private unsafe static byte[] GlslToSpirv(string glsl, ShaderStage stage)
- {
- Options options;
-
- lock (_shaderOptionsLock)
- {
- options = new Options(false)
- {
- SourceLanguage = SourceLanguage.Glsl,
- TargetSpirVVersion = new SpirVVersion(1, 5)
- };
- }
-
- options.SetTargetEnvironment(TargetEnvironment.Vulkan, EnvironmentVersion.Vulkan_1_2);
- Compiler compiler = new Compiler(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}");
-
- return null;
- }
-
- var spirvBytes = new Span<byte>((void*)scr.CodePointer, (int)scr.CodeLength);
-
- byte[] code = new byte[(scr.CodeLength + 3) & ~3];
-
- spirvBytes.CopyTo(code.AsSpan().Slice(0, (int)scr.CodeLength));
-
- return code;
- }
-
- private static ShaderKind GetShaderCShaderStage(ShaderStage stage)
- {
- switch (stage)
- {
- case ShaderStage.Vertex:
- return ShaderKind.GlslVertexShader;
- case ShaderStage.Geometry:
- return ShaderKind.GlslGeometryShader;
- case ShaderStage.TessellationControl:
- return ShaderKind.GlslTessControlShader;
- case ShaderStage.TessellationEvaluation:
- return ShaderKind.GlslTessEvaluationShader;
- case ShaderStage.Fragment:
- return ShaderKind.GlslFragmentShader;
- case ShaderStage.Compute:
- return ShaderKind.GlslComputeShader;
- }
-
- Logger.Debug?.Print(LogClass.Gpu, $"Invalid {nameof(ShaderStage)} enum value: {stage}.");
-
- return ShaderKind.GlslVertexShader;
- }
-
- public unsafe PipelineShaderStageCreateInfo GetInfo()
- {
- return new PipelineShaderStageCreateInfo()
- {
- SType = StructureType.PipelineShaderStageCreateInfo,
- Stage = _stage,
- Module = _module,
- PName = (byte*)_ptrMainEntryPointName
- };
- }
-
- public void WaitForCompile()
- {
- CompileTask.Wait();
- }
-
- public unsafe void Dispose()
- {
- if (!_disposed)
- {
- _api.DestroyShaderModule(_device, _module, null);
- _disposed = true;
- }
- }
- }
-}