From 511db833db8087e1b87d088d92df330effc0d28a Mon Sep 17 00:00:00 2001 From: Isaac Marovitz Date: Tue, 10 Oct 2023 20:00:56 -0400 Subject: [PATCH] Fix fragment shaders (and fuck everything up) --- src/Ryujinx.Graphics.Metal/Program.cs | 3 ++- .../CodeGen/Msl/Declarations.cs | 21 +++++++++++++++---- .../CodeGen/Msl/Instructions/InstGen.cs | 2 +- .../CodeGen/Msl/MslGenerator.cs | 1 + 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/Ryujinx.Graphics.Metal/Program.cs b/src/Ryujinx.Graphics.Metal/Program.cs index 7a7eb58361..ee3ff493fb 100644 --- a/src/Ryujinx.Graphics.Metal/Program.cs +++ b/src/Ryujinx.Graphics.Metal/Program.cs @@ -20,8 +20,9 @@ namespace Ryujinx.Graphics.Metal { for (int index = 0; index < shaders.Length; index++) { - var libraryError = new NSError(IntPtr.Zero); ShaderSource shader = shaders[index]; + + var libraryError = new NSError(IntPtr.Zero); var shaderLibrary = device.NewLibrary(StringHelper.NSString(shader.Code), new MTLCompileOptions(IntPtr.Zero), ref libraryError); if (libraryError != IntPtr.Zero) { diff --git a/src/Ryujinx.Graphics.Shader/CodeGen/Msl/Declarations.cs b/src/Ryujinx.Graphics.Shader/CodeGen/Msl/Declarations.cs index 8feba561be..956483a453 100644 --- a/src/Ryujinx.Graphics.Shader/CodeGen/Msl/Declarations.cs +++ b/src/Ryujinx.Graphics.Shader/CodeGen/Msl/Declarations.cs @@ -67,6 +67,10 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Msl { context.AppendLine("VertexOutput out;"); } + else if (stage == ShaderStage.Fragment) + { + context.AppendLine("FragmentOutput out;"); + } foreach (AstOperand decl in function.Locals) { @@ -133,8 +137,9 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Msl { string type = GetVarTypeName(context, context.Definitions.GetUserDefinedType(ioDefinition.Location, isOutput: false)); string name = $"{DefaultNames.IAttributePrefix}{ioDefinition.Location}"; + string suffix = context.Definitions.Stage == ShaderStage.Vertex ? $" [[attribute({ioDefinition.Location})]]" : ""; - context.AppendLine($"{type} {name} [[attribute({ioDefinition.Location})]];"); + context.AppendLine($"{type} {name}{suffix};"); } context.LeaveScope(";"); @@ -173,9 +178,17 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Msl foreach (var ioDefinition in inputs.OrderBy(x => x.Location)) { string type = GetVarTypeName(context, context.Definitions.GetUserDefinedType(ioDefinition.Location, isOutput: true)); - string name = $"{DefaultNames.OAttributePrefix}{ioDefinition.Location}"; - name = ioDefinition.IoVariable == IoVariable.Position ? "position" : name; - string suffix = ioDefinition.IoVariable == IoVariable.Position ? " [[position]]" : ""; + string name = ioDefinition.IoVariable switch + { + IoVariable.Position => "position", + IoVariable.FragmentOutputColor => "color", + _ => $"{DefaultNames.OAttributePrefix}{ioDefinition.Location}" + }; + string suffix = ioDefinition.IoVariable switch + { + IoVariable.Position => " [[position]]", + _ => "" + }; context.AppendLine($"{type} {name}{suffix};"); } diff --git a/src/Ryujinx.Graphics.Shader/CodeGen/Msl/Instructions/InstGen.cs b/src/Ryujinx.Graphics.Shader/CodeGen/Msl/Instructions/InstGen.cs index 5113c58c85..f18b345971 100644 --- a/src/Ryujinx.Graphics.Shader/CodeGen/Msl/Instructions/InstGen.cs +++ b/src/Ryujinx.Graphics.Shader/CodeGen/Msl/Instructions/InstGen.cs @@ -70,7 +70,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Msl.Instructions { return $"{op} {GetSourceExpr(context, operation.GetSource(0), context.CurrentFunction.ReturnType)}"; } - else if (inst == Instruction.Return && context.Definitions.Stage == ShaderStage.Vertex) + if (inst == Instruction.Return && context.Definitions.Stage is ShaderStage.Vertex or ShaderStage.Fragment) { return $"{op} out"; } diff --git a/src/Ryujinx.Graphics.Shader/CodeGen/Msl/MslGenerator.cs b/src/Ryujinx.Graphics.Shader/CodeGen/Msl/MslGenerator.cs index bd03aeeb58..0196164e8d 100644 --- a/src/Ryujinx.Graphics.Shader/CodeGen/Msl/MslGenerator.cs +++ b/src/Ryujinx.Graphics.Shader/CodeGen/Msl/MslGenerator.cs @@ -91,6 +91,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Msl { funcKeyword = "fragment"; funcName = "fragmentMain"; + returnType = "FragmentOutput"; } else if (stage == ShaderStage.Compute) {