diff options
Diffstat (limited to 'src/Ryujinx.Graphics.Shader/CodeGen')
13 files changed, 413 insertions, 338 deletions
diff --git a/src/Ryujinx.Graphics.Shader/CodeGen/CodeGenParameters.cs b/src/Ryujinx.Graphics.Shader/CodeGen/CodeGenParameters.cs new file mode 100644 index 00000000..f692c428 --- /dev/null +++ b/src/Ryujinx.Graphics.Shader/CodeGen/CodeGenParameters.cs @@ -0,0 +1,31 @@ +using Ryujinx.Graphics.Shader.StructuredIr; +using Ryujinx.Graphics.Shader.Translation; + +namespace Ryujinx.Graphics.Shader.CodeGen +{ + readonly struct CodeGenParameters + { + public readonly AttributeUsage AttributeUsage; + public readonly ShaderDefinitions Definitions; + public readonly ShaderProperties Properties; + public readonly HostCapabilities HostCapabilities; + public readonly ILogger Logger; + public readonly TargetApi TargetApi; + + public CodeGenParameters( + AttributeUsage attributeUsage, + ShaderDefinitions definitions, + ShaderProperties properties, + HostCapabilities hostCapabilities, + ILogger logger, + TargetApi targetApi) + { + AttributeUsage = attributeUsage; + Definitions = definitions; + Properties = properties; + HostCapabilities = hostCapabilities; + Logger = logger; + TargetApi = targetApi; + } + } +} diff --git a/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/CodeGenContext.cs b/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/CodeGenContext.cs index 551e5cef..7506f72a 100644 --- a/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/CodeGenContext.cs +++ b/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/CodeGenContext.cs @@ -12,7 +12,12 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl public StructuredProgramInfo Info { get; } - public ShaderConfig Config { get; } + public AttributeUsage AttributeUsage { get; } + public ShaderDefinitions Definitions { get; } + public ShaderProperties Properties { get; } + public HostCapabilities HostCapabilities { get; } + public ILogger Logger { get; } + public TargetApi TargetApi { get; } public OperandManager OperandManager { get; } @@ -22,10 +27,15 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl private string _indentation; - public CodeGenContext(StructuredProgramInfo info, ShaderConfig config) + public CodeGenContext(StructuredProgramInfo info, CodeGenParameters parameters) { Info = info; - Config = config; + AttributeUsage = parameters.AttributeUsage; + Definitions = parameters.Definitions; + Properties = parameters.Properties; + HostCapabilities = parameters.HostCapabilities; + Logger = parameters.Logger; + TargetApi = parameters.TargetApi; OperandManager = new OperandManager(); diff --git a/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/Declarations.cs b/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/Declarations.cs index 2a45e23d..e181ae98 100644 --- a/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/Declarations.cs +++ b/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/Declarations.cs @@ -1,4 +1,5 @@ using Ryujinx.Common; +using Ryujinx.Graphics.Shader.IntermediateRepresentation; using Ryujinx.Graphics.Shader.StructuredIr; using Ryujinx.Graphics.Shader.Translation; using System; @@ -13,10 +14,10 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl { public static void Declare(CodeGenContext context, StructuredProgramInfo info) { - context.AppendLine(context.Config.Options.TargetApi == TargetApi.Vulkan ? "#version 460 core" : "#version 450 core"); + context.AppendLine(context.TargetApi == TargetApi.Vulkan ? "#version 460 core" : "#version 450 core"); context.AppendLine("#extension GL_ARB_gpu_shader_int64 : enable"); - if (context.Config.GpuAccessor.QueryHostSupportsShaderBallot()) + if (context.HostCapabilities.SupportsShaderBallot) { context.AppendLine("#extension GL_ARB_shader_ballot : enable"); } @@ -30,24 +31,24 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl context.AppendLine("#extension GL_EXT_shader_image_load_formatted : enable"); context.AppendLine("#extension GL_EXT_texture_shadow_lod : enable"); - if (context.Config.Stage == ShaderStage.Compute) + if (context.Definitions.Stage == ShaderStage.Compute) { context.AppendLine("#extension GL_ARB_compute_shader : enable"); } - else if (context.Config.Stage == ShaderStage.Fragment) + else if (context.Definitions.Stage == ShaderStage.Fragment) { - if (context.Config.GpuAccessor.QueryHostSupportsFragmentShaderInterlock()) + if (context.HostCapabilities.SupportsFragmentShaderInterlock) { context.AppendLine("#extension GL_ARB_fragment_shader_interlock : enable"); } - else if (context.Config.GpuAccessor.QueryHostSupportsFragmentShaderOrderingIntel()) + else if (context.HostCapabilities.SupportsFragmentShaderOrderingIntel) { context.AppendLine("#extension GL_INTEL_fragment_shader_ordering : enable"); } } else { - if (context.Config.Stage == ShaderStage.Vertex) + if (context.Definitions.Stage == ShaderStage.Vertex) { context.AppendLine("#extension GL_ARB_shader_draw_parameters : enable"); } @@ -55,12 +56,12 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl context.AppendLine("#extension GL_ARB_shader_viewport_layer_array : enable"); } - if (context.Config.GpPassthrough && context.Config.GpuAccessor.QueryHostSupportsGeometryShaderPassthrough()) + if (context.Definitions.GpPassthrough && context.HostCapabilities.SupportsGeometryShaderPassthrough) { context.AppendLine("#extension GL_NV_geometry_shader_passthrough : enable"); } - if (context.Config.GpuAccessor.QueryHostSupportsViewportMask()) + if (context.HostCapabilities.SupportsViewportMask) { context.AppendLine("#extension GL_NV_viewport_array2 : enable"); } @@ -71,23 +72,22 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl context.AppendLine($"const int {DefaultNames.UndefinedName} = 0;"); context.AppendLine(); - DeclareConstantBuffers(context, context.Config.Properties.ConstantBuffers.Values); - DeclareStorageBuffers(context, context.Config.Properties.StorageBuffers.Values); - DeclareMemories(context, context.Config.Properties.LocalMemories.Values, isShared: false); - DeclareMemories(context, context.Config.Properties.SharedMemories.Values, isShared: true); - DeclareSamplers(context, context.Config.Properties.Textures.Values); - DeclareImages(context, context.Config.Properties.Images.Values); + DeclareConstantBuffers(context, context.Properties.ConstantBuffers.Values); + DeclareStorageBuffers(context, context.Properties.StorageBuffers.Values); + DeclareMemories(context, context.Properties.LocalMemories.Values, isShared: false); + DeclareMemories(context, context.Properties.SharedMemories.Values, isShared: true); + DeclareSamplers(context, context.Properties.Textures.Values); + DeclareImages(context, context.Properties.Images.Values); - if (context.Config.Stage != ShaderStage.Compute) + if (context.Definitions.Stage != ShaderStage.Compute) { - if (context.Config.Stage == ShaderStage.Geometry) + if (context.Definitions.Stage == ShaderStage.Geometry) { - InputTopology inputTopology = context.Config.GpuAccessor.QueryPrimitiveTopology(); - string inPrimitive = inputTopology.ToGlslString(); + string inPrimitive = context.Definitions.InputTopology.ToGlslString(); - context.AppendLine($"layout (invocations = {context.Config.ThreadsPerInputPrimitive}, {inPrimitive}) in;"); + context.AppendLine($"layout (invocations = {context.Definitions.ThreadsPerInputPrimitive}, {inPrimitive}) in;"); - if (context.Config.GpPassthrough && context.Config.GpuAccessor.QueryHostSupportsGeometryShaderPassthrough()) + if (context.Definitions.GpPassthrough && context.HostCapabilities.SupportsGeometryShaderPassthrough) { context.AppendLine($"layout (passthrough) in gl_PerVertex"); context.EnterScope(); @@ -98,87 +98,75 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl } else { - string outPrimitive = context.Config.OutputTopology.ToGlslString(); + string outPrimitive = context.Definitions.OutputTopology.ToGlslString(); - int maxOutputVertices = context.Config.GpPassthrough - ? inputTopology.ToInputVertices() - : context.Config.MaxOutputVertices; + int maxOutputVertices = context.Definitions.GpPassthrough + ? context.Definitions.InputTopology.ToInputVertices() + : context.Definitions.MaxOutputVertices; context.AppendLine($"layout ({outPrimitive}, max_vertices = {maxOutputVertices}) out;"); } context.AppendLine(); } - else if (context.Config.Stage == ShaderStage.TessellationControl) + else if (context.Definitions.Stage == ShaderStage.TessellationControl) { - int threadsPerInputPrimitive = context.Config.ThreadsPerInputPrimitive; + int threadsPerInputPrimitive = context.Definitions.ThreadsPerInputPrimitive; context.AppendLine($"layout (vertices = {threadsPerInputPrimitive}) out;"); context.AppendLine(); } - else if (context.Config.Stage == ShaderStage.TessellationEvaluation) + else if (context.Definitions.Stage == ShaderStage.TessellationEvaluation) { - bool tessCw = context.Config.GpuAccessor.QueryTessCw(); + bool tessCw = context.Definitions.TessCw; - if (context.Config.Options.TargetApi == TargetApi.Vulkan) + if (context.TargetApi == TargetApi.Vulkan) { // We invert the front face on Vulkan backend, so we need to do that here aswell. tessCw = !tessCw; } - string patchType = context.Config.GpuAccessor.QueryTessPatchType().ToGlsl(); - string spacing = context.Config.GpuAccessor.QueryTessSpacing().ToGlsl(); + string patchType = context.Definitions.TessPatchType.ToGlsl(); + string spacing = context.Definitions.TessSpacing.ToGlsl(); string windingOrder = tessCw ? "cw" : "ccw"; context.AppendLine($"layout ({patchType}, {spacing}, {windingOrder}) in;"); context.AppendLine(); } - if (context.Config.UsedInputAttributes != 0 || context.Config.GpPassthrough) + static bool IsUserDefined(IoDefinition ioDefinition, StorageKind storageKind) { - DeclareInputAttributes(context, info); - - context.AppendLine(); + return ioDefinition.StorageKind == storageKind && ioDefinition.IoVariable == IoVariable.UserDefined; } - if (context.Config.UsedOutputAttributes != 0 || context.Config.Stage != ShaderStage.Fragment) + static bool IsUserDefinedOutput(ShaderStage stage, IoDefinition ioDefinition) { - DeclareOutputAttributes(context, info); - - context.AppendLine(); + IoVariable ioVariable = stage == ShaderStage.Fragment ? IoVariable.FragmentOutputColor : IoVariable.UserDefined; + return ioDefinition.StorageKind == StorageKind.Output && ioDefinition.IoVariable == ioVariable; } - if (context.Config.UsedInputAttributesPerPatch.Count != 0) - { - DeclareInputAttributesPerPatch(context, context.Config.UsedInputAttributesPerPatch); - - context.AppendLine(); - } - - if (context.Config.UsedOutputAttributesPerPatch.Count != 0) - { - DeclareUsedOutputAttributesPerPatch(context, context.Config.UsedOutputAttributesPerPatch); - - context.AppendLine(); - } + DeclareInputAttributes(context, info.IoDefinitions.Where(x => IsUserDefined(x, StorageKind.Input))); + DeclareOutputAttributes(context, info.IoDefinitions.Where(x => IsUserDefinedOutput(context.Definitions.Stage, x))); + DeclareInputAttributesPerPatch(context, info.IoDefinitions.Where(x => IsUserDefined(x, StorageKind.InputPerPatch))); + DeclareOutputAttributesPerPatch(context, info.IoDefinitions.Where(x => IsUserDefined(x, StorageKind.OutputPerPatch))); - if (context.Config.TransformFeedbackEnabled && context.Config.LastInVertexPipeline) + if (context.Definitions.TransformFeedbackEnabled && context.Definitions.LastInVertexPipeline) { - var tfOutput = context.Config.GetTransformFeedbackOutput(AttributeConsts.PositionX); + var tfOutput = context.Definitions.GetTransformFeedbackOutput(AttributeConsts.PositionX); if (tfOutput.Valid) { context.AppendLine($"layout (xfb_buffer = {tfOutput.Buffer}, xfb_offset = {tfOutput.Offset}, xfb_stride = {tfOutput.Stride}) out gl_PerVertex"); context.EnterScope(); context.AppendLine("vec4 gl_Position;"); - context.LeaveScope(context.Config.Stage == ShaderStage.TessellationControl ? " gl_out[];" : ";"); + context.LeaveScope(context.Definitions.Stage == ShaderStage.TessellationControl ? " gl_out[];" : ";"); } } } else { - string localSizeX = NumberFormatter.FormatInt(context.Config.GpuAccessor.QueryComputeLocalSizeX()); - string localSizeY = NumberFormatter.FormatInt(context.Config.GpuAccessor.QueryComputeLocalSizeY()); - string localSizeZ = NumberFormatter.FormatInt(context.Config.GpuAccessor.QueryComputeLocalSizeZ()); + string localSizeX = NumberFormatter.FormatInt(context.Definitions.ComputeLocalSizeX); + string localSizeY = NumberFormatter.FormatInt(context.Definitions.ComputeLocalSizeY); + string localSizeZ = NumberFormatter.FormatInt(context.Definitions.ComputeLocalSizeZ); context.AppendLine( "layout (" + @@ -188,15 +176,15 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl context.AppendLine(); } - if (context.Config.Stage == ShaderStage.Fragment) + if (context.Definitions.Stage == ShaderStage.Fragment) { - if (context.Config.GpuAccessor.QueryEarlyZForce()) + if (context.Definitions.EarlyZForce) { context.AppendLine("layout (early_fragment_tests) in;"); context.AppendLine(); } - if (context.Config.Properties.OriginUpperLeft) + if (context.Definitions.OriginUpperLeft) { context.AppendLine("layout (origin_upper_left) in vec4 gl_FragCoord;"); context.AppendLine(); @@ -251,7 +239,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl public static string GetVarTypeName(CodeGenContext context, AggregateType type, bool precise = true) { - if (context.Config.GpuAccessor.QueryHostReducedPrecision()) + if (context.HostCapabilities.ReducedPrecision) { precise = false; } @@ -305,7 +293,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl string set = string.Empty; - if (context.Config.Options.TargetApi == TargetApi.Vulkan) + if (context.TargetApi == TargetApi.Vulkan) { set = $"set = {buffer.Set}, "; } @@ -390,7 +378,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl string layout = string.Empty; - if (context.Config.Options.TargetApi == TargetApi.Vulkan) + if (context.TargetApi == TargetApi.Vulkan) { layout = $", set = {definition.Set}"; } @@ -435,7 +423,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl layout = ", " + layout; } - if (context.Config.Options.TargetApi == TargetApi.Vulkan) + if (context.TargetApi == TargetApi.Vulkan) { layout = $", set = {definition.Set}{layout}"; } @@ -444,42 +432,50 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl } } - private static void DeclareInputAttributes(CodeGenContext context, StructuredProgramInfo info) + private static void DeclareInputAttributes(CodeGenContext context, IEnumerable<IoDefinition> inputs) { - if (context.Config.UsedFeatures.HasFlag(FeatureFlags.IaIndexing)) + if (context.Definitions.IaIndexing) { - string suffix = context.Config.Stage == ShaderStage.Geometry ? "[]" : string.Empty; + string suffix = context.Definitions.Stage == ShaderStage.Geometry ? "[]" : string.Empty; context.AppendLine($"layout (location = 0) in vec4 {DefaultNames.IAttributePrefix}{suffix}[{Constants.MaxAttributes}];"); + context.AppendLine(); } else { - int usedAttributes = context.Config.UsedInputAttributes | context.Config.PassthroughAttributes; - while (usedAttributes != 0) + foreach (var ioDefinition in inputs.OrderBy(x => x.Location)) { - int index = BitOperations.TrailingZeroCount(usedAttributes); - DeclareInputAttribute(context, info, index); - usedAttributes &= ~(1 << index); + DeclareInputAttribute(context, ioDefinition.Location, ioDefinition.Component); + } + + if (inputs.Any()) + { + context.AppendLine(); } } } - private static void DeclareInputAttributesPerPatch(CodeGenContext context, HashSet<int> attrs) + private static void DeclareInputAttributesPerPatch(CodeGenContext context, IEnumerable<IoDefinition> inputs) { - foreach (int attr in attrs.Order()) + foreach (var ioDefinition in inputs.OrderBy(x => x.Location)) { - DeclareInputAttributePerPatch(context, attr); + DeclareInputAttributePerPatch(context, ioDefinition.Location); + } + + if (inputs.Any()) + { + context.AppendLine(); } } - private static void DeclareInputAttribute(CodeGenContext context, StructuredProgramInfo info, int attr) + private static void DeclareInputAttribute(CodeGenContext context, int location, int component) { - string suffix = IsArrayAttributeGlsl(context.Config.Stage, isOutAttr: false) ? "[]" : string.Empty; + string suffix = IsArrayAttributeGlsl(context.Definitions.Stage, isOutAttr: false) ? "[]" : string.Empty; string iq = string.Empty; - if (context.Config.Stage == ShaderStage.Fragment) + if (context.Definitions.Stage == ShaderStage.Fragment) { - iq = context.Config.ImapTypes[attr].GetFirstUsedType() switch + iq = context.Definitions.ImapTypes[location].GetFirstUsedType() switch { PixelImap.Constant => "flat ", PixelImap.ScreenLinear => "noperspective ", @@ -487,14 +483,22 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl }; } - string name = $"{DefaultNames.IAttributePrefix}{attr}"; + string name = $"{DefaultNames.IAttributePrefix}{location}"; - if (context.Config.TransformFeedbackEnabled && context.Config.Stage == ShaderStage.Fragment) + if (context.Definitions.TransformFeedbackEnabled && context.Definitions.Stage == ShaderStage.Fragment) { - int components = context.Config.GetTransformFeedbackOutputComponents(attr, 0); + bool hasComponent = context.Definitions.HasPerLocationInputOrOutputComponent(IoVariable.UserDefined, location, component, isOutput: false); - if (components > 1) + if (hasComponent) { + char swzMask = "xyzw"[component]; + + context.AppendLine($"layout (location = {location}, component = {component}) {iq}in float {name}_{swzMask}{suffix};"); + } + else + { + int components = context.Definitions.GetTransformFeedbackOutputComponents(location, 0); + string type = components switch { 2 => "vec2", @@ -503,147 +507,152 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl _ => "float", }; - context.AppendLine($"layout (location = {attr}) in {type} {name};"); - } - - for (int c = components > 1 ? components : 0; c < 4; c++) - { - char swzMask = "xyzw"[c]; - - context.AppendLine($"layout (location = {attr}, component = {c}) {iq}in float {name}_{swzMask}{suffix};"); + context.AppendLine($"layout (location = {location}) in {type} {name};"); } } else { - bool passthrough = (context.Config.PassthroughAttributes & (1 << attr)) != 0; - string pass = passthrough && context.Config.GpuAccessor.QueryHostSupportsGeometryShaderPassthrough() ? "passthrough, " : string.Empty; - string type; + bool passthrough = (context.AttributeUsage.PassthroughAttributes & (1 << location)) != 0; + string pass = passthrough && context.HostCapabilities.SupportsGeometryShaderPassthrough ? "passthrough, " : string.Empty; + string type = GetVarTypeName(context, context.Definitions.GetUserDefinedType(location, isOutput: false), false); - if (context.Config.Stage == ShaderStage.Vertex) - { - type = context.Config.GpuAccessor.QueryAttributeType(attr).ToVec4Type(); - } - else - { - type = AttributeType.Float.ToVec4Type(); - } - - context.AppendLine($"layout ({pass}location = {attr}) {iq}in {type} {name}{suffix};"); + context.AppendLine($"layout ({pass}location = {location}) {iq}in {type} {name}{suffix};"); } } - private static void DeclareInputAttributePerPatch(CodeGenContext context, int attr) + private static void DeclareInputAttributePerPatch(CodeGenContext context, int patchLocation) { - int location = context.Config.GetPerPatchAttributeLocation(attr); - string name = $"{DefaultNames.PerPatchAttributePrefix}{attr}"; + int location = context.AttributeUsage.GetPerPatchAttributeLocation(patchLocation); + string name = $"{DefaultNames.PerPatchAttributePrefix}{patchLocation}"; context.AppendLine($"layout (location = {location}) patch in vec4 {name};"); } - private static void DeclareOutputAttributes(CodeGenContext context, StructuredProgramInfo info) + private static void DeclareOutputAttributes(CodeGenContext context, IEnumerable<IoDefinition> outputs) { - if (context.Config.UsedFeatures.HasFlag(FeatureFlags.OaIndexing)) + if (context.Definitions.OaIndexing) { context.AppendLine($"layout (location = 0) out vec4 {DefaultNames.OAttributePrefix}[{Constants.MaxAttributes}];"); + context.AppendLine(); } else { - int usedAttributes = context.Config.UsedOutputAttributes; + outputs = outputs.OrderBy(x => x.Location); - if (context.Config.Stage == ShaderStage.Fragment && context.Config.GpuAccessor.QueryDualSourceBlendEnable()) + if (context.Definitions.Stage == ShaderStage.Fragment && context.Definitions.DualSourceBlend) { - int firstOutput = BitOperations.TrailingZeroCount(usedAttributes); - int mask = 3 << firstOutput; + IoDefinition firstOutput = outputs.ElementAtOrDefault(0); + IoDefinition secondOutput = outputs.ElementAtOrDefault(1); - if ((usedAttributes & mask) == mask) + if (firstOutput.Location + 1 == secondOutput.Location) { - usedAttributes &= ~mask; - DeclareOutputDualSourceBlendAttribute(context, firstOutput); + DeclareOutputDualSourceBlendAttribute(context, firstOutput.Location); + outputs = outputs.Skip(2); } } - while (usedAttributes != 0) + foreach (var ioDefinition in outputs) { - int index = BitOperations.TrailingZeroCount(usedAttributes); - DeclareOutputAttribute(context, index); - usedAttributes &= ~(1 << index); + DeclareOutputAttribute(context, ioDefinition.Location, ioDefinition.Component); + } + + if (outputs.Any()) + { + context.AppendLine(); } } } - private static void DeclareOutputAttribute(CodeGenContext context, int attr) + private static void DeclareOutputAttribute(CodeGenContext context, int location, int component) { - string suffix = IsArrayAttributeGlsl(context.Config.Stage, isOutAttr: true) ? "[]" : string.Empty; - string name = $"{DefaultNames.OAttributePrefix}{attr}{suffix}"; + string suffix = IsArrayAttributeGlsl(context.Definitions.Stage, isOutAttr: true) ? "[]" : string.Empty; + string name = $"{DefaultNames.OAttributePrefix}{location}{suffix}"; - if (context.Config.TransformFeedbackEnabled && context.Config.LastInVertexPipeline) + if (context.Definitions.TransformFeedbackEnabled && context.Definitions.LastInVertexPipeline) { - int components = context.Config.GetTransformFeedbackOutputComponents(attr, 0); + bool hasComponent = context.Definitions.HasPerLocationInputOrOutputComponent(IoVariable.UserDefined, location, component, isOutput: true); - if (components > 1) + if (hasComponent) { - string type = components switch - { - 2 => "vec2", - 3 => "vec3", - 4 => "vec4", - _ => "float", - }; + char swzMask = "xyzw"[component]; string xfb = string.Empty; - var tfOutput = context.Config.GetTransformFeedbackOutput(attr, 0); + var tfOutput = context.Definitions.GetTransformFeedbackOutput(location, component); if (tfOutput.Valid) { xfb = $", xfb_buffer = {tfOutput.Buffer}, xfb_offset = {tfOutput.Offset}, xfb_stride = {tfOutput.Stride}"; } - context.AppendLine($"layout (location = {attr}{xfb}) out {type} {name};"); + context.AppendLine($"layout (location = {location}, component = {component}{xfb}) out float {name}_{swzMask};"); } - - for (int c = components > 1 ? components : 0; c < 4; c++) + else { - char swzMask = "xyzw"[c]; + int components = context.Definitions.GetTransformFeedbackOutputComponents(location, 0); + + string type = components switch + { + 2 => "vec2", + 3 => "vec3", + 4 => "vec4", + _ => "float", + }; string xfb = string.Empty; - var tfOutput = context.Config.GetTransformFeedbackOutput(attr, c); + var tfOutput = context.Definitions.GetTransformFeedbackOutput(location, 0); if (tfOutput.Valid) { xfb = $", xfb_buffer = {tfOutput.Buffer}, xfb_offset = {tfOutput.Offset}, xfb_stride = {tfOutput.Stride}"; } - context.AppendLine($"layout (location = {attr}, component = {c}{xfb}) out float {name}_{swzMask};"); + context.AppendLine($"layout (location = {location}{xfb}) out {type} {name};"); } } else { - string type = context.Config.Stage != ShaderStage.Fragment ? "vec4" : - context.Config.GpuAccessor.QueryFragmentOutputType(attr) switch - { - AttributeType.Sint => "ivec4", - AttributeType.Uint => "uvec4", - _ => "vec4", - }; + string type = context.Definitions.Stage != ShaderStage.Fragment ? "vec4" : + GetVarTypeName(context, context.Definitions.GetFragmentOutputColorType(location), false); - if (context.Config.GpuAccessor.QueryHostReducedPrecision() && context.Config.Stage == ShaderStage.Vertex && attr == 0) + if (context.HostCapabilities.ReducedPrecision && context.Definitions.Stage == ShaderStage.Vertex && location == 0) { - context.AppendLine($"layout (location = {attr}) invariant out {type} {name};"); + context.AppendLine($"layout (location = {location}) invariant out {type} {name};"); } else { - context.AppendLine($"layout (location = {attr}) out {type} {name};"); + context.AppendLine($"layout (location = {location}) out {type} {name};"); } } } - private static void DeclareOutputDualSourceBlendAttribute(CodeGenContext context, int attr) + private static void DeclareOutputDualSourceBlendAttribute(CodeGenContext context, int location) + { + string name = $"{DefaultNames.OAttributePrefix}{location}"; + string name2 = $"{DefaultNames.OAttributePrefix}{(location + 1)}"; + + context.AppendLine($"layout (location = {location}, index = 0) out vec4 {name};"); + context.AppendLine($"layout (location = {location}, index = 1) out vec4 {name2};"); + } + + private static void DeclareOutputAttributesPerPatch(CodeGenContext context, IEnumerable<IoDefinition> outputs) + { + foreach (var ioDefinition in outputs) + { + DeclareOutputAttributePerPatch(context, ioDefinition.Location); + } + + if (outputs.Any()) + { + context.AppendLine(); + } + } + + private static void DeclareOutputAttributePerPatch(CodeGenContext context, int patchLocation) { - string name = $"{DefaultNames.OAttributePrefix}{attr}"; - string name2 = $"{DefaultNames.OAttributePrefix}{(attr + 1)}"; + int location = context.AttributeUsage.GetPerPatchAttributeLocation(patchLocation); + string name = $"{DefaultNames.PerPatchAttributePrefix}{patchLocation}"; - context.AppendLine($"layout (location = {attr}, index = 0) out vec4 {name};"); - context.AppendLine($"layout (location = {attr}, index = 1) out vec4 {name2};"); + context.AppendLine($"layout (location = {location}) patch out vec4 {name};"); } private static bool IsArrayAttributeGlsl(ShaderStage stage, bool isOutAttr) @@ -660,29 +669,13 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl } } - private static void DeclareUsedOutputAttributesPerPatch(CodeGenContext context, HashSet<int> attrs) - { - foreach (int attr in attrs.Order()) - { - DeclareOutputAttributePerPatch(context, attr); - } - } - - private static void DeclareOutputAttributePerPatch(CodeGenContext context, int attr) - { - int location = context.Config.GetPerPatchAttributeLocation(attr); - string name = $"{DefaultNames.PerPatchAttributePrefix}{attr}"; - - context.AppendLine($"layout (location = {location}) patch out vec4 {name};"); - } - private static void AppendHelperFunction(CodeGenContext context, string filename) { string code = EmbeddedResources.ReadAllText(filename); code = code.Replace("\t", CodeGenContext.Tab); - if (context.Config.GpuAccessor.QueryHostSupportsShaderBallot()) + if (context.HostCapabilities.SupportsShaderBallot) { code = code.Replace("$SUBGROUP_INVOCATION$", "gl_SubGroupInvocationARB"); code = code.Replace("$SUBGROUP_BROADCAST$", "readInvocationARB"); diff --git a/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/GlslGenerator.cs b/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/GlslGenerator.cs index 0140c1b9..469c4f0a 100644 --- a/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/GlslGenerator.cs +++ b/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/GlslGenerator.cs @@ -11,9 +11,9 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl { private const string MainFunctionName = "main"; - public static string Generate(StructuredProgramInfo info, ShaderConfig config) + public static string Generate(StructuredProgramInfo info, CodeGenParameters parameters) { - CodeGenContext context = new(info, config); + CodeGenContext context = new(info, parameters); Declarations.Declare(context, info); @@ -113,7 +113,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl } }; - bool supportsBarrierDivergence = context.Config.GpuAccessor.QueryHostSupportsShaderBarrierDivergence(); + bool supportsBarrierDivergence = context.HostCapabilities.SupportsShaderBarrierDivergence; bool mayHaveReturned = false; foreach (IAstNode node in visitor.Visit()) @@ -128,7 +128,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl // so skip emitting the barrier for those cases. if (visitor.Block.Type != AstBlockType.Main || mayHaveReturned || !isMainFunction) { - context.Config.GpuAccessor.Log($"Shader has barrier on potentially divergent block, the barrier will be removed."); + context.Logger.Log("Shader has barrier on potentially divergent block, the barrier will be removed."); continue; } diff --git a/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenBallot.cs b/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenBallot.cs index 9a2bfef0..b44759c0 100644 --- a/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenBallot.cs +++ b/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenBallot.cs @@ -14,7 +14,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions string arg = GetSoureExpr(context, operation.GetSource(0), dstType); - if (context.Config.GpuAccessor.QueryHostSupportsShaderBallot()) + if (context.HostCapabilities.SupportsShaderBallot) { return $"unpackUint2x32(ballotARB({arg})).x"; } diff --git a/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenFSI.cs b/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenFSI.cs index a3d68028..1697aa47 100644 --- a/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenFSI.cs +++ b/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenFSI.cs @@ -4,11 +4,11 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions { public static string FSIBegin(CodeGenContext context) { - if (context.Config.GpuAccessor.QueryHostSupportsFragmentShaderInterlock()) + if (context.HostCapabilities.SupportsFragmentShaderInterlock) { return "beginInvocationInterlockARB()"; } - else if (context.Config.GpuAccessor.QueryHostSupportsFragmentShaderOrderingIntel()) + else if (context.HostCapabilities.SupportsFragmentShaderOrderingIntel) { return "beginFragmentShaderOrderingINTEL()"; } @@ -18,7 +18,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions public static string FSIEnd(CodeGenContext context) { - if (context.Config.GpuAccessor.QueryHostSupportsFragmentShaderInterlock()) + if (context.HostCapabilities.SupportsFragmentShaderInterlock) { return "endInvocationInterlockARB()"; } diff --git a/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenMemory.cs b/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenMemory.cs index 7e6d8bb5..a1f92d11 100644 --- a/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenMemory.cs +++ b/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenMemory.cs @@ -84,7 +84,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions indexExpr = Src(AggregateType.S32); } - string imageName = GetImageName(context.Config, texOp, indexExpr); + string imageName = GetImageName(context.Properties, texOp, indexExpr); texCallBuilder.Append('('); texCallBuilder.Append(imageName); @@ -216,7 +216,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions indexExpr = GetSoureExpr(context, texOp.GetSource(0), AggregateType.S32); } - string samplerName = GetSamplerName(context.Config, texOp, indexExpr); + string samplerName = GetSamplerName(context.Properties, texOp, indexExpr); int coordsIndex = isBindless || isIndexed ? 1 : 0; @@ -273,7 +273,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions // 2D Array and Cube shadow samplers with LOD level or bias requires an extension. // If the extension is not supported, just remove the LOD parameter. - if (isArray && isShadow && (is2D || isCube) && !context.Config.GpuAccessor.QueryHostSupportsTextureShadowLod()) + if (isArray && isShadow && (is2D || isCube) && !context.HostCapabilities.SupportsTextureShadowLod) { hasLodBias = false; hasLodLevel = false; @@ -281,7 +281,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions // Cube shadow samplers with LOD level requires an extension. // If the extension is not supported, just remove the LOD level parameter. - if (isShadow && isCube && !context.Config.GpuAccessor.QueryHostSupportsTextureShadowLod()) + if (isShadow && isCube && !context.HostCapabilities.SupportsTextureShadowLod) { hasLodLevel = false; } @@ -342,7 +342,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions indexExpr = Src(AggregateType.S32); } - string samplerName = GetSamplerName(context.Config, texOp, indexExpr); + string samplerName = GetSamplerName(context.Properties, texOp, indexExpr); texCall += "(" + samplerName; @@ -538,7 +538,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions indexExpr = GetSoureExpr(context, texOp.GetSource(0), AggregateType.S32); } - string samplerName = GetSamplerName(context.Config, texOp, indexExpr); + string samplerName = GetSamplerName(context.Properties, texOp, indexExpr); if (texOp.Index == 3) { @@ -546,7 +546,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions } else { - context.Config.Properties.Textures.TryGetValue(texOp.Binding, out TextureDefinition definition); + context.Properties.Textures.TryGetValue(texOp.Binding, out TextureDefinition definition); bool hasLod = !definition.Type.HasFlag(SamplerType.Multisample) && (definition.Type & SamplerType.Mask) != SamplerType.TextureBuffer; string texCall; @@ -593,8 +593,8 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions int binding = bindingIndex.Value; BufferDefinition buffer = storageKind == StorageKind.ConstantBuffer - ? context.Config.Properties.ConstantBuffers[binding] - : context.Config.Properties.StorageBuffers[binding]; + ? context.Properties.ConstantBuffers[binding] + : context.Properties.StorageBuffers[binding]; if (operation.GetSource(srcIndex++) is not AstOperand fieldIndex || fieldIndex.Type != OperandType.Constant) { @@ -614,8 +614,8 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions } MemoryDefinition memory = storageKind == StorageKind.LocalMemory - ? context.Config.Properties.LocalMemories[bindingId.Value] - : context.Config.Properties.SharedMemories[bindingId.Value]; + ? context.Properties.LocalMemories[bindingId.Value] + : context.Properties.SharedMemories[bindingId.Value]; varName = memory.Name; varType = memory.Type; @@ -636,7 +636,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions int location = -1; int component = 0; - if (context.Config.HasPerLocationInputOrOutput(ioVariable, isOutput)) + if (context.Definitions.HasPerLocationInputOrOutput(ioVariable, isOutput)) { if (operation.GetSource(srcIndex++) is not AstOperand vecIndex || vecIndex.Type != OperandType.Constant) { @@ -648,16 +648,23 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions if (operation.SourcesCount > srcIndex && operation.GetSource(srcIndex) is AstOperand elemIndex && elemIndex.Type == OperandType.Constant && - context.Config.HasPerLocationInputOrOutputComponent(ioVariable, location, elemIndex.Value, isOutput)) + context.Definitions.HasPerLocationInputOrOutputComponent(ioVariable, location, elemIndex.Value, isOutput)) { component = elemIndex.Value; srcIndex++; } } - (varName, varType) = IoMap.GetGlslVariable(context.Config, ioVariable, location, component, isOutput, isPerPatch); + (varName, varType) = IoMap.GetGlslVariable( + context.Definitions, + context.HostCapabilities, + ioVariable, + location, + component, + isOutput, + isPerPatch); - if (IoMap.IsPerVertexBuiltIn(context.Config.Stage, ioVariable, isOutput)) + if (IoMap.IsPerVertexBuiltIn(context.Definitions.Stage, ioVariable, isOutput)) { // Since those exist both as input and output on geometry and tessellation shaders, // we need the gl_in and gl_out prefixes to disambiguate. @@ -692,7 +699,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions { varName += "." + "xyzw"[elementIndex.Value & 3]; } - else if (srcIndex == firstSrcIndex && context.Config.Stage == ShaderStage.TessellationControl && storageKind == StorageKind.Output) + else if (srcIndex == firstSrcIndex && context.Definitions.Stage == ShaderStage.TessellationControl && storageKind == StorageKind.Output) { // GLSL requires that for tessellation control shader outputs, // that the index expression must be *exactly* "gl_InvocationID", @@ -715,9 +722,9 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions return varName; } - private static string GetSamplerName(ShaderConfig config, AstTextureOperation texOp, string indexExpr) + private static string GetSamplerName(ShaderProperties resourceDefinitions, AstTextureOperation texOp, string indexExpr) { - string name = config.Properties.Textures[texOp.Binding].Name; + string name = resourceDefinitions.Textures[texOp.Binding].Name; if (texOp.Type.HasFlag(SamplerType.Indexed)) { @@ -727,9 +734,9 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions return name; } - private static string GetImageName(ShaderConfig config, AstTextureOperation texOp, string indexExpr) + private static string GetImageName(ShaderProperties resourceDefinitions, AstTextureOperation texOp, string indexExpr) { - string name = config.Properties.Images[texOp.Binding].Name; + string name = resourceDefinitions.Images[texOp.Binding].Name; if (texOp.Type.HasFlag(SamplerType.Indexed)) { diff --git a/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/IoMap.cs b/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/IoMap.cs index 3f88d2b3..b5f453ae 100644 --- a/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/IoMap.cs +++ b/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/IoMap.cs @@ -7,7 +7,8 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions static class IoMap { public static (string, AggregateType) GetGlslVariable( - ShaderConfig config, + ShaderDefinitions definitions, + HostCapabilities hostCapabilities, IoVariable ioVariable, int location, int component, @@ -25,7 +26,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions IoVariable.DrawIndex => ("gl_DrawIDARB", AggregateType.S32), IoVariable.FogCoord => ("gl_FogFragCoord", AggregateType.FP32), // Deprecated. IoVariable.FragmentCoord => ("gl_FragCoord", AggregateType.Vector4 | AggregateType.FP32), - IoVariable.FragmentOutputColor => GetFragmentOutputColorVariableName(config, location), + IoVariable.FragmentOutputColor => GetFragmentOutputColorVariableName(definitions, location), IoVariable.FragmentOutputDepth => ("gl_FragDepth", AggregateType.FP32), IoVariable.FrontColorDiffuse => ("gl_FrontColor", AggregateType.Vector4 | AggregateType.FP32), // Deprecated. IoVariable.FrontColorSpecular => ("gl_FrontSecondaryColor", AggregateType.Vector4 | AggregateType.FP32), // Deprecated. @@ -38,20 +39,20 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions IoVariable.PointCoord => ("gl_PointCoord", AggregateType.Vector2 | AggregateType.FP32), IoVariable.PointSize => ("gl_PointSize", AggregateType.FP32), IoVariable.Position => ("gl_Position", AggregateType.Vector4 | AggregateType.FP32), - IoVariable.PrimitiveId => GetPrimitiveIdVariableName(config.Stage, isOutput), - IoVariable.SubgroupEqMask => GetSubgroupMaskVariableName(config, "Eq"), - IoVariable.SubgroupGeMask => GetSubgroupMaskVariableName(config, "Ge"), - IoVariable.SubgroupGtMask => GetSubgroupMaskVariableName(config, "Gt"), - IoVariable.SubgroupLaneId => GetSubgroupInvocationIdVariableName(config), - IoVariable.SubgroupLeMask => GetSubgroupMaskVariableName(config, "Le"), - IoVariable.SubgroupLtMask => GetSubgroupMaskVariableName(config, "Lt"), + IoVariable.PrimitiveId => GetPrimitiveIdVariableName(definitions.Stage, isOutput), + IoVariable.SubgroupEqMask => GetSubgroupMaskVariableName(hostCapabilities.SupportsShaderBallot, "Eq"), + IoVariable.SubgroupGeMask => GetSubgroupMaskVariableName(hostCapabilities.SupportsShaderBallot, "Ge"), + IoVariable.SubgroupGtMask => GetSubgroupMaskVariableName(hostCapabilities.SupportsShaderBallot, "Gt"), + IoVariable.SubgroupLaneId => GetSubgroupInvocationIdVariableName(hostCapabilities.SupportsShaderBallot), + IoVariable.SubgroupLeMask => GetSubgroupMaskVariableName(hostCapabilities.SupportsShaderBallot, "Le"), + IoVariable.SubgroupLtMask => GetSubgroupMaskVariableName(hostCapabilities.SupportsShaderBallot, "Lt"), IoVariable.TessellationCoord => ("gl_TessCoord", AggregateType.Vector3 | AggregateType.FP32), IoVariable.TessellationLevelInner => ("gl_TessLevelInner", AggregateType.Array | AggregateType.FP32), IoVariable.TessellationLevelOuter => ("gl_TessLevelOuter", AggregateType.Array | AggregateType.FP32), IoVariable.TextureCoord => ("gl_TexCoord", AggregateType.Array | AggregateType.Vector4 | AggregateType.FP32), // Deprecated. IoVariable.ThreadId => ("gl_LocalInvocationID", AggregateType.Vector3 | AggregateType.U32), IoVariable.ThreadKill => ("gl_HelperInvocation", AggregateType.Bool), - IoVariable.UserDefined => GetUserDefinedVariableName(config, location, component, isOutput, isPerPatch), + IoVariable.UserDefined => GetUserDefinedVariableName(definitions, location, component, isOutput, isPerPatch), IoVariable.VertexId => ("gl_VertexID", AggregateType.S32), IoVariable.VertexIndex => ("gl_VertexIndex", AggregateType.S32), IoVariable.ViewportIndex => ("gl_ViewportIndex", AggregateType.S32), @@ -86,16 +87,16 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions return false; } - private static (string, AggregateType) GetFragmentOutputColorVariableName(ShaderConfig config, int location) + private static (string, AggregateType) GetFragmentOutputColorVariableName(ShaderDefinitions definitions, int location) { if (location < 0) { - return (DefaultNames.OAttributePrefix, config.GetFragmentOutputColorType(0)); + return (DefaultNames.OAttributePrefix, definitions.GetFragmentOutputColorType(0)); } string name = DefaultNames.OAttributePrefix + location.ToString(CultureInfo.InvariantCulture); - return (name, config.GetFragmentOutputColorType(location)); + return (name, definitions.GetFragmentOutputColorType(location)); } private static (string, AggregateType) GetPrimitiveIdVariableName(ShaderStage stage, bool isOutput) @@ -104,21 +105,21 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions return (isOutput || stage != ShaderStage.Geometry ? "gl_PrimitiveID" : "gl_PrimitiveIDIn", AggregateType.S32); } - private static (string, AggregateType) GetSubgroupMaskVariableName(ShaderConfig config, string cc) + private static (string, AggregateType) GetSubgroupMaskVariableName(bool supportsShaderBallot, string cc) { - return config.GpuAccessor.QueryHostSupportsShaderBallot() + return supportsShaderBallot ? ($"unpackUint2x32(gl_SubGroup{cc}MaskARB)", AggregateType.Vector2 | AggregateType.U32) : ($"gl_Subgroup{cc}Mask", AggregateType.Vector4 | AggregateType.U32); } - private static (string, AggregateType) GetSubgroupInvocationIdVariableName(ShaderConfig config) + private static (string, AggregateType) GetSubgroupInvocationIdVariableName(bool supportsShaderBallot) { - return config.GpuAccessor.QueryHostSupportsShaderBallot() + return supportsShaderBallot ? ("gl_SubGroupInvocationARB", AggregateType.U32) : ("gl_SubgroupInvocationID", AggregateType.U32); } - private static (string, AggregateType) GetUserDefinedVariableName(ShaderConfig config, int location, int component, bool isOutput, bool isPerPatch) + private static (string, AggregateType) GetUserDefinedVariableName(ShaderDefinitions definitions, int location, int component, bool isOutput, bool isPerPatch) { string name = isPerPatch ? DefaultNames.PerPatchAttributePrefix @@ -126,17 +127,17 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions if (location < 0) { - return (name, config.GetUserDefinedType(0, isOutput)); + return (name, definitions.GetUserDefinedType(0, isOutput)); } name += location.ToString(CultureInfo.InvariantCulture); - if (config.HasPerLocationInputOrOutputComponent(IoVariable.UserDefined, location, component, isOutput)) + if (definitions.HasPerLocationInputOrOutputComponent(IoVariable.UserDefined, location, component, isOutput)) { name += "_" + "xyzw"[component & 3]; } - return (name, config.GetUserDefinedType(location, isOutput)); + return (name, definitions.GetUserDefinedType(location, isOutput)); } } } diff --git a/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/OperandManager.cs b/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/OperandManager.cs index 9346341f..53ecc453 100644 --- a/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/OperandManager.cs +++ b/src/Ryujinx.Graphics.Shader/CodeGen/Glsl/OperandManager.cs @@ -68,8 +68,8 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl } BufferDefinition buffer = operation.StorageKind == StorageKind.ConstantBuffer - ? context.Config.Properties.ConstantBuffers[bindingIndex.Value] - : context.Config.Properties.StorageBuffers[bindingIndex.Value]; + ? context.Properties.ConstantBuffers[bindingIndex.Value] + : context.Properties.StorageBuffers[bindingIndex.Value]; StructureField field = buffer.Type.Fields[fieldIndex.Value]; return field.Type & AggregateType.ElementTypeMask; @@ -82,8 +82,8 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl } MemoryDefinition memory = operation.StorageKind == StorageKind.LocalMemory - ? context.Config.Properties.LocalMemories[bindingId.Value] - : context.Config.Properties.SharedMemories[bindingId.Value]; + ? context.Properties.LocalMemories[bindingId.Value] + : context.Properties.SharedMemories[bindingId.Value]; return memory.Type & AggregateType.ElementTypeMask; @@ -102,7 +102,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl int location = 0; int component = 0; - if (context.Config.HasPerLocationInputOrOutput(ioVariable, isOutput)) + if (context.Definitions.HasPerLocationInputOrOutput(ioVariable, isOutput)) { if (operation.GetSource(1) is not AstOperand vecIndex || vecIndex.Type != OperandType.Constant) { @@ -114,13 +114,20 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl if (operation.SourcesCount > 2 && operation.GetSource(2) is AstOperand elemIndex && elemIndex.Type == OperandType.Constant && - context.Config.HasPerLocationInputOrOutputComponent(ioVariable, location, elemIndex.Value, isOutput)) + context.Definitions.HasPerLocationInputOrOutputComponent(ioVariable, location, elemIndex.Value, isOutput)) { component = elemIndex.Value; } } - (_, AggregateType varType) = IoMap.GetGlslVariable(context.Config, ioVariable, location, component, isOutput, isPerPatch); + (_, AggregateType varType) = IoMap.GetGlslVariable( + context.Definitions, + context.HostCapabilities, + ioVariable, + location, + component, + isOutput, + isPerPatch); return varType & AggregateType.ElementTypeMask; } diff --git a/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/CodeGenContext.cs b/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/CodeGenContext.cs index d4f49045..d385782a 100644 --- a/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/CodeGenContext.cs +++ b/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/CodeGenContext.cs @@ -20,21 +20,29 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv public StructuredProgramInfo Info { get; } - public ShaderConfig Config { get; } + public AttributeUsage AttributeUsage { get; } + public ShaderDefinitions Definitions { get; } + public ShaderProperties Properties { get; } + public HostCapabilities HostCapabilities { get; } + public ILogger Logger { get; } + public TargetApi TargetApi { get; } public int InputVertices { get; } - public Dictionary<int, Instruction> ConstantBuffers { get; } = new Dictionary<int, Instruction>(); - public Dictionary<int, Instruction> StorageBuffers { get; } = new Dictionary<int, Instruction>(); - public Dictionary<int, Instruction> LocalMemories { get; } = new Dictionary<int, Instruction>(); - public Dictionary<int, Instruction> SharedMemories { get; } = new Dictionary<int, Instruction>(); - public Dictionary<int, SamplerType> SamplersTypes { get; } = new Dictionary<int, SamplerType>(); - public Dictionary<int, (Instruction, Instruction, Instruction)> Samplers { get; } = new Dictionary<int, (Instruction, Instruction, Instruction)>(); - public Dictionary<int, (Instruction, Instruction)> Images { get; } = new Dictionary<int, (Instruction, Instruction)>(); - public Dictionary<IoDefinition, Instruction> Inputs { get; } = new Dictionary<IoDefinition, Instruction>(); - public Dictionary<IoDefinition, Instruction> Outputs { get; } = new Dictionary<IoDefinition, Instruction>(); - public Dictionary<IoDefinition, Instruction> InputsPerPatch { get; } = new Dictionary<IoDefinition, Instruction>(); - public Dictionary<IoDefinition, Instruction> OutputsPerPatch { get; } = new Dictionary<IoDefinition, Instruction>(); + public Dictionary<int, Instruction> ConstantBuffers { get; } = new(); + public Dictionary<int, Instruction> StorageBuffers { get; } = new(); + + public Dictionary<int, Instruction> LocalMemories { get; } = new(); + public Dictionary<int, Instruction> SharedMemories { get; } = new(); + + public Dictionary<int, SamplerType> SamplersTypes { get; } = new(); + public Dictionary<int, (Instruction, Instruction, Instruction)> Samplers { get; } = new(); + public Dictionary<int, (Instruction, Instruction)> Images { get; } = new(); + + public Dictionary<IoDefinition, Instruction> Inputs { get; } = new(); + public Dictionary<IoDefinition, Instruction> Outputs { get; } = new(); + public Dictionary<IoDefinition, Instruction> InputsPerPatch { get; } = new(); + public Dictionary<IoDefinition, Instruction> OutputsPerPatch { get; } = new(); public StructuredFunction CurrentFunction { get; set; } private readonly Dictionary<AstOperand, Instruction> _locals = new(); @@ -81,25 +89,28 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv public CodeGenContext( StructuredProgramInfo info, - ShaderConfig config, + CodeGenParameters parameters, GeneratorPool<Instruction> instPool, GeneratorPool<LiteralInteger> integerPool) : base(SpirvVersionPacked, instPool, integerPool) { Info = info; - Config = config; - - if (config.Stage == ShaderStage.Geometry) + AttributeUsage = parameters.AttributeUsage; + Definitions = parameters.Definitions; + Properties = parameters.Properties; + HostCapabilities = parameters.HostCapabilities; + Logger = parameters.Logger; + TargetApi = parameters.TargetApi; + + if (parameters.Definitions.Stage == ShaderStage.Geometry) { - InputTopology inPrimitive = config.GpuAccessor.QueryPrimitiveTopology(); - - InputVertices = inPrimitive switch + InputVertices = parameters.Definitions.InputTopology switch { InputTopology.Points => 1, InputTopology.Lines => 2, InputTopology.LinesAdjacency => 2, InputTopology.Triangles => 3, InputTopology.TrianglesAdjacency => 3, - _ => throw new InvalidOperationException($"Invalid input topology \"{inPrimitive}\"."), + _ => throw new InvalidOperationException($"Invalid input topology \"{parameters.Definitions.InputTopology}\"."), }; } diff --git a/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/Declarations.cs b/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/Declarations.cs index b14fead8..f3a083c3 100644 --- a/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/Declarations.cs +++ b/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/Declarations.cs @@ -5,7 +5,6 @@ using Spv.Generator; using System; using System.Collections.Generic; using System.Diagnostics; -using System.Numerics; using static Spv.Specification; using SpvInstruction = Spv.Generator.Instruction; @@ -66,12 +65,12 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv public static void DeclareAll(CodeGenContext context, StructuredProgramInfo info) { - DeclareConstantBuffers(context, context.Config.Properties.ConstantBuffers.Values); - DeclareStorageBuffers(context, context.Config.Properties.StorageBuffers.Values); - DeclareMemories(context, context.Config.Properties.LocalMemories, context.LocalMemories, StorageClass.Private); - DeclareMemories(context, context.Config.Properties.SharedMemories, context.SharedMemories, StorageClass.Workgroup); - DeclareSamplers(context, context.Config.Properties.Textures.Values); - DeclareImages(context, context.Config.Properties.Images.Values); + DeclareConstantBuffers(context, context.Properties.ConstantBuffers.Values); + DeclareStorageBuffers(context, context.Properties.StorageBuffers.Values); + DeclareMemories(context, context.Properties.LocalMemories, context.LocalMemories, StorageClass.Private); + DeclareMemories(context, context.Properties.SharedMemories, context.SharedMemories, StorageClass.Workgroup); + DeclareSamplers(context, context.Properties.Textures.Values); + DeclareImages(context, context.Properties.Images.Values); DeclareInputsAndOutputs(context, info); } @@ -108,7 +107,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv foreach (BufferDefinition buffer in buffers) { - int setIndex = context.Config.Options.TargetApi == TargetApi.Vulkan ? buffer.Set : 0; + int setIndex = context.TargetApi == TargetApi.Vulkan ? buffer.Set : 0; int alignment = buffer.Layout == BufferLayout.Std140 ? 16 : 4; int alignmentMask = alignment - 1; int offset = 0; @@ -181,7 +180,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv { foreach (var sampler in samplers) { - int setIndex = context.Config.Options.TargetApi == TargetApi.Vulkan ? sampler.Set : 0; + int setIndex = context.TargetApi == TargetApi.Vulkan ? sampler.Set : 0; var dim = (sampler.Type & SamplerType.Mask) switch { @@ -220,7 +219,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv { foreach (var image in images) { - int setIndex = context.Config.Options.TargetApi == TargetApi.Vulkan ? image.Set : 0; + int setIndex = context.TargetApi == TargetApi.Vulkan ? image.Set : 0; var dim = GetDim(image.Type); @@ -314,16 +313,29 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv private static void DeclareInputsAndOutputs(CodeGenContext context, StructuredProgramInfo info) { + int firstLocation = int.MaxValue; + + if (context.Definitions.Stage == ShaderStage.Fragment && context.Definitions.DualSourceBlend) + { + foreach (var ioDefinition in info.IoDefinitions) + { + if (ioDefinition.IoVariable == IoVariable.FragmentOutputColor && ioDefinition.Location < firstLocation) + { + firstLocation = ioDefinition.Location; + } + } + } + foreach (var ioDefinition in info.IoDefinitions) { PixelImap iq = PixelImap.Unused; - if (context.Config.Stage == ShaderStage.Fragment) + if (context.Definitions.Stage == ShaderStage.Fragment) { var ioVariable = ioDefinition.IoVariable; if (ioVariable == IoVariable.UserDefined) { - iq = context.Config.ImapTypes[ioDefinition.Location].GetFirstUsedType(); + iq = context.Definitions.ImapTypes[ioDefinition.Location].GetFirstUsedType(); } else { @@ -340,11 +352,17 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv bool isOutput = ioDefinition.StorageKind.IsOutput(); bool isPerPatch = ioDefinition.StorageKind.IsPerPatch(); - DeclareInputOrOutput(context, ioDefinition, isOutput, isPerPatch, iq); + DeclareInputOrOutput(context, ioDefinition, isOutput, isPerPatch, iq, firstLocation); } } - private static void DeclareInputOrOutput(CodeGenContext context, IoDefinition ioDefinition, bool isOutput, bool isPerPatch, PixelImap iq = PixelImap.Unused) + private static void DeclareInputOrOutput( + CodeGenContext context, + IoDefinition ioDefinition, + bool isOutput, + bool isPerPatch, + PixelImap iq = PixelImap.Unused, + int firstLocation = 0) { IoVariable ioVariable = ioDefinition.IoVariable; var storageClass = isOutput ? StorageClass.Output : StorageClass.Input; @@ -355,12 +373,12 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv if (ioVariable == IoVariable.UserDefined) { - varType = context.Config.GetUserDefinedType(ioDefinition.Location, isOutput); + varType = context.Definitions.GetUserDefinedType(ioDefinition.Location, isOutput); isBuiltIn = false; } else if (ioVariable == IoVariable.FragmentOutputColor) { - varType = context.Config.GetFragmentOutputColorType(ioDefinition.Location); + varType = context.Definitions.GetFragmentOutputColorType(ioDefinition.Location); isBuiltIn = false; } else @@ -374,16 +392,16 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv } } - bool hasComponent = context.Config.HasPerLocationInputOrOutputComponent(ioVariable, ioDefinition.Location, ioDefinition.Component, isOutput); + bool hasComponent = context.Definitions.HasPerLocationInputOrOutputComponent(ioVariable, ioDefinition.Location, ioDefinition.Component, isOutput); if (hasComponent) { varType &= AggregateType.ElementTypeMask; } - else if (ioVariable == IoVariable.UserDefined && context.Config.HasTransformFeedbackOutputs(isOutput)) + else if (ioVariable == IoVariable.UserDefined && context.Definitions.HasTransformFeedbackOutputs(isOutput)) { varType &= AggregateType.ElementTypeMask; - varType |= context.Config.GetTransformFeedbackOutputComponents(ioDefinition.Location, ioDefinition.Component) switch + varType |= context.Definitions.GetTransformFeedbackOutputComponents(ioDefinition.Location, ioDefinition.Component) switch { 2 => AggregateType.Vector2, 3 => AggregateType.Vector3, @@ -395,20 +413,20 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv var spvType = context.GetType(varType, IoMap.GetSpirvBuiltInArrayLength(ioVariable)); bool builtInPassthrough = false; - if (!isPerPatch && IoMap.IsPerVertex(ioVariable, context.Config.Stage, isOutput)) + if (!isPerPatch && IoMap.IsPerVertex(ioVariable, context.Definitions.Stage, isOutput)) { - int arraySize = context.Config.Stage == ShaderStage.Geometry ? context.InputVertices : 32; + int arraySize = context.Definitions.Stage == ShaderStage.Geometry ? context.InputVertices : 32; spvType = context.TypeArray(spvType, context.Constant(context.TypeU32(), arraySize)); - if (context.Config.GpPassthrough && context.Config.GpuAccessor.QueryHostSupportsGeometryShaderPassthrough()) + if (context.Definitions.GpPassthrough && context.HostCapabilities.SupportsGeometryShaderPassthrough) { builtInPassthrough = true; } } - if (context.Config.Stage == ShaderStage.TessellationControl && isOutput && !isPerPatch) + if (context.Definitions.Stage == ShaderStage.TessellationControl && isOutput && !isPerPatch) { - spvType = context.TypeArray(spvType, context.Constant(context.TypeU32(), context.Config.ThreadsPerInputPrimitive)); + spvType = context.TypeArray(spvType, context.Constant(context.TypeU32(), context.Definitions.ThreadsPerInputPrimitive)); } var spvPointerType = context.TypePointer(storageClass, spvType); @@ -426,7 +444,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv context.Decorate(spvVar, Decoration.Patch); } - if (context.Config.GpuAccessor.QueryHostReducedPrecision() && ioVariable == IoVariable.Position) + if (context.HostCapabilities.ReducedPrecision && ioVariable == IoVariable.Position) { context.Decorate(spvVar, Decoration.Invariant); } @@ -439,7 +457,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv if (ioVariable == IoVariable.UserDefined) { - int location = context.Config.GetPerPatchAttributeLocation(ioDefinition.Location); + int location = context.AttributeUsage.GetPerPatchAttributeLocation(ioDefinition.Location); context.Decorate(spvVar, Decoration.Location, (LiteralInteger)location); } @@ -455,8 +473,8 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv if (!isOutput && !isPerPatch && - (context.Config.PassthroughAttributes & (1 << ioDefinition.Location)) != 0 && - context.Config.GpuAccessor.QueryHostSupportsGeometryShaderPassthrough()) + (context.AttributeUsage.PassthroughAttributes & (1 << ioDefinition.Location)) != 0 && + context.HostCapabilities.SupportsGeometryShaderPassthrough) { context.Decorate(spvVar, Decoration.PassthroughNV); } @@ -465,13 +483,11 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv { int location = ioDefinition.Location; - if (context.Config.Stage == ShaderStage.Fragment && context.Config.GpuAccessor.QueryDualSourceBlendEnable()) + if (context.Definitions.Stage == ShaderStage.Fragment && context.Definitions.DualSourceBlend) { - int firstLocation = BitOperations.TrailingZeroCount(context.Config.UsedOutputAttributes); int index = location - firstLocation; - int mask = 3 << firstLocation; - if ((uint)index < 2 && (context.Config.UsedOutputAttributes & mask) == mask) + if ((uint)index < 2) { context.Decorate(spvVar, Decoration.Location, (LiteralInteger)firstLocation); context.Decorate(spvVar, Decoration.Index, (LiteralInteger)index); @@ -499,7 +515,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv break; } } - else if (context.Config.TryGetTransformFeedbackOutput( + else if (context.Definitions.TryGetTransformFeedbackOutput( ioVariable, ioDefinition.Location, ioDefinition.Component, diff --git a/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/Instructions.cs b/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/Instructions.cs index 9489397b..30ce7f8b 100644 --- a/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/Instructions.cs +++ b/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/Instructions.cs @@ -240,10 +240,10 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv { // Barrier on divergent control flow paths may cause the GPU to hang, // so skip emitting the barrier for those cases. - if (!context.Config.GpuAccessor.QueryHostSupportsShaderBarrierDivergence() && + if (!context.HostCapabilities.SupportsShaderBarrierDivergence && (context.CurrentBlock.Type != AstBlockType.Main || context.MayHaveReturned || !context.IsMainFunction)) { - context.Config.GpuAccessor.Log($"Shader has barrier on potentially divergent block, the barrier will be removed."); + context.Logger.Log("Shader has barrier on potentially divergent block, the barrier will be removed."); return OperationResult.Invalid; } @@ -546,7 +546,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv private static OperationResult GenerateFSIBegin(CodeGenContext context, AstOperation operation) { - if (context.Config.GpuAccessor.QueryHostSupportsFragmentShaderInterlock()) + if (context.HostCapabilities.SupportsFragmentShaderInterlock) { context.BeginInvocationInterlockEXT(); } @@ -556,7 +556,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv private static OperationResult GenerateFSIEnd(CodeGenContext context, AstOperation operation) { - if (context.Config.GpuAccessor.QueryHostSupportsFragmentShaderInterlock()) + if (context.HostCapabilities.SupportsFragmentShaderInterlock) { context.EndInvocationInterlockEXT(); } @@ -1446,7 +1446,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv lodBias = Src(AggregateType.FP32); } - if (!isGather && !intCoords && !isMultisample && !hasLodLevel && !hasDerivatives && context.Config.Stage != ShaderStage.Fragment) + if (!isGather && !intCoords && !isMultisample && !hasLodLevel && !hasDerivatives && context.Definitions.Stage != ShaderStage.Fragment) { // Implicit LOD is only valid on fragment. // Use the LOD bias as explicit LOD if available. @@ -1804,8 +1804,8 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv } BufferDefinition buffer = storageKind == StorageKind.ConstantBuffer - ? context.Config.Properties.ConstantBuffers[bindingIndex.Value] - : context.Config.Properties.StorageBuffers[bindingIndex.Value]; + ? context.Properties.ConstantBuffers[bindingIndex.Value] + : context.Properties.StorageBuffers[bindingIndex.Value]; StructureField field = buffer.Type.Fields[fieldIndex.Value]; storageClass = StorageClass.Uniform; @@ -1825,13 +1825,13 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv if (storageKind == StorageKind.LocalMemory) { storageClass = StorageClass.Private; - varType = context.Config.Properties.LocalMemories[bindingId.Value].Type & AggregateType.ElementTypeMask; + varType = context.Properties.LocalMemories[bindingId.Value].Type & AggregateType.ElementTypeMask; baseObj = context.LocalMemories[bindingId.Value]; } else { storageClass = StorageClass.Workgroup; - varType = context.Config.Properties.SharedMemories[bindingId.Value].Type & AggregateType.ElementTypeMask; + varType = context.Properties.SharedMemories[bindingId.Value].Type & AggregateType.ElementTypeMask; baseObj = context.SharedMemories[bindingId.Value]; } break; @@ -1851,7 +1851,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv int location = 0; int component = 0; - if (context.Config.HasPerLocationInputOrOutput(ioVariable, isOutput)) + if (context.Definitions.HasPerLocationInputOrOutput(ioVariable, isOutput)) { if (operation.GetSource(srcIndex++) is not AstOperand vecIndex || vecIndex.Type != OperandType.Constant) { @@ -1863,7 +1863,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv if (operation.SourcesCount > srcIndex && operation.GetSource(srcIndex) is AstOperand elemIndex && elemIndex.Type == OperandType.Constant && - context.Config.HasPerLocationInputOrOutputComponent(ioVariable, location, elemIndex.Value, isOutput)) + context.Definitions.HasPerLocationInputOrOutputComponent(ioVariable, location, elemIndex.Value, isOutput)) { component = elemIndex.Value; srcIndex++; @@ -1872,11 +1872,11 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv if (ioVariable == IoVariable.UserDefined) { - varType = context.Config.GetUserDefinedType(location, isOutput); + varType = context.Definitions.GetUserDefinedType(location, isOutput); } else if (ioVariable == IoVariable.FragmentOutputColor) { - varType = context.Config.GetFragmentOutputColorType(location); + varType = context.Definitions.GetFragmentOutputColorType(location); } else { @@ -2076,7 +2076,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv { var result = emitF(context.TypeFP64(), context.GetFP64(src1), context.GetFP64(src2)); - if (!context.Config.GpuAccessor.QueryHostReducedPrecision() || operation.ForcePrecise) + if (!context.HostCapabilities.ReducedPrecision || operation.ForcePrecise) { context.Decorate(result, Decoration.NoContraction); } @@ -2087,7 +2087,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv { var result = emitF(context.TypeFP32(), context.GetFP32(src1), context.GetFP32(src2)); - if (!context.Config.GpuAccessor.QueryHostReducedPrecision() || operation.ForcePrecise) + if (!context.HostCapabilities.ReducedPrecision || operation.ForcePrecise) { context.Decorate(result, Decoration.NoContraction); } @@ -2147,7 +2147,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv { var result = emitF(context.TypeFP64(), context.GetFP64(src1), context.GetFP64(src2), context.GetFP64(src3)); - if (!context.Config.GpuAccessor.QueryHostReducedPrecision() || operation.ForcePrecise) + if (!context.HostCapabilities.ReducedPrecision || operation.ForcePrecise) { context.Decorate(result, Decoration.NoContraction); } @@ -2158,7 +2158,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv { var result = emitF(context.TypeFP32(), context.GetFP32(src1), context.GetFP32(src2), context.GetFP32(src3)); - if (!context.Config.GpuAccessor.QueryHostReducedPrecision() || operation.ForcePrecise) + if (!context.HostCapabilities.ReducedPrecision || operation.ForcePrecise) { context.Decorate(result, Decoration.NoContraction); } diff --git a/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/SpirvGenerator.cs b/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/SpirvGenerator.cs index 21797975..5eee888e 100644 --- a/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/SpirvGenerator.cs +++ b/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/SpirvGenerator.cs @@ -35,7 +35,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv HelperFunctionsMask.ShuffleXor | HelperFunctionsMask.SwizzleAdd; - public static byte[] Generate(StructuredProgramInfo info, ShaderConfig config) + public static byte[] Generate(StructuredProgramInfo info, CodeGenParameters parameters) { SpvInstructionPool instPool; SpvLiteralIntegerPool integerPool; @@ -46,7 +46,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv integerPool = _integerPool.Allocate(); } - CodeGenContext context = new(info, config, instPool, integerPool); + CodeGenContext context = new(info, parameters, instPool, integerPool); context.AddCapability(Capability.GroupNonUniformBallot); context.AddCapability(Capability.GroupNonUniformShuffle); @@ -56,39 +56,40 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv context.AddCapability(Capability.ImageQuery); context.AddCapability(Capability.SampledBuffer); - if (config.TransformFeedbackEnabled && config.LastInVertexPipeline) + if (parameters.Definitions.TransformFeedbackEnabled && parameters.Definitions.LastInVertexPipeline) { context.AddCapability(Capability.TransformFeedback); } - if (config.Stage == ShaderStage.Fragment) + if (parameters.Definitions.Stage == ShaderStage.Fragment) { if (context.Info.IoDefinitions.Contains(new IoDefinition(StorageKind.Input, IoVariable.Layer))) { context.AddCapability(Capability.Geometry); } - if (context.Config.GpuAccessor.QueryHostSupportsFragmentShaderInterlock()) + if (context.HostCapabilities.SupportsFragmentShaderInterlock) { context.AddCapability(Capability.FragmentShaderPixelInterlockEXT); context.AddExtension("SPV_EXT_fragment_shader_interlock"); } } - else if (config.Stage == ShaderStage.Geometry) + else if (parameters.Definitions.Stage == ShaderStage.Geometry) { context.AddCapability(Capability.Geometry); - if (config.GpPassthrough && context.Config.GpuAccessor.QueryHostSupportsGeometryShaderPassthrough()) + if (parameters.Definitions.GpPassthrough && context.HostCapabilities.SupportsGeometryShaderPassthrough) { context.AddExtension("SPV_NV_geometry_shader_passthrough"); context.AddCapability(Capability.GeometryShaderPassthroughNV); } } - else if (config.Stage == ShaderStage.TessellationControl || config.Stage == ShaderStage.TessellationEvaluation) + else if (parameters.Definitions.Stage == ShaderStage.TessellationControl || + parameters.Definitions.Stage == ShaderStage.TessellationEvaluation) { context.AddCapability(Capability.Tessellation); } - else if (config.Stage == ShaderStage.Vertex) + else if (parameters.Definitions.Stage == ShaderStage.Vertex) { context.AddCapability(Capability.DrawParameters); } @@ -170,15 +171,15 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv if (funcIndex == 0) { - context.AddEntryPoint(context.Config.Stage.Convert(), spvFunc, "main", context.GetMainInterface()); + context.AddEntryPoint(context.Definitions.Stage.Convert(), spvFunc, "main", context.GetMainInterface()); - if (context.Config.Stage == ShaderStage.TessellationControl) + if (context.Definitions.Stage == ShaderStage.TessellationControl) { - context.AddExecutionMode(spvFunc, ExecutionMode.OutputVertices, (SpvLiteralInteger)context.Config.ThreadsPerInputPrimitive); + context.AddExecutionMode(spvFunc, ExecutionMode.OutputVertices, (SpvLiteralInteger)context.Definitions.ThreadsPerInputPrimitive); } - else if (context.Config.Stage == ShaderStage.TessellationEvaluation) + else if (context.Definitions.Stage == ShaderStage.TessellationEvaluation) { - switch (context.Config.GpuAccessor.QueryTessPatchType()) + switch (context.Definitions.TessPatchType) { case TessPatchType.Isolines: context.AddExecutionMode(spvFunc, ExecutionMode.Isolines); @@ -191,7 +192,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv break; } - switch (context.Config.GpuAccessor.QueryTessSpacing()) + switch (context.Definitions.TessSpacing) { case TessSpacing.EqualSpacing: context.AddExecutionMode(spvFunc, ExecutionMode.SpacingEqual); @@ -204,9 +205,9 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv break; } - bool tessCw = context.Config.GpuAccessor.QueryTessCw(); + bool tessCw = context.Definitions.TessCw; - if (context.Config.Options.TargetApi == TargetApi.Vulkan) + if (context.TargetApi == TargetApi.Vulkan) { // We invert the front face on Vulkan backend, so we need to do that here as well. tessCw = !tessCw; @@ -221,37 +222,35 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv context.AddExecutionMode(spvFunc, ExecutionMode.VertexOrderCcw); } } - else if (context.Config.Stage == ShaderStage.Geometry) + else if (context.Definitions.Stage == ShaderStage.Geometry) { - InputTopology inputTopology = context.Config.GpuAccessor.QueryPrimitiveTopology(); - - context.AddExecutionMode(spvFunc, inputTopology switch + context.AddExecutionMode(spvFunc, context.Definitions.InputTopology switch { InputTopology.Points => ExecutionMode.InputPoints, InputTopology.Lines => ExecutionMode.InputLines, InputTopology.LinesAdjacency => ExecutionMode.InputLinesAdjacency, InputTopology.Triangles => ExecutionMode.Triangles, InputTopology.TrianglesAdjacency => ExecutionMode.InputTrianglesAdjacency, - _ => throw new InvalidOperationException($"Invalid input topology \"{inputTopology}\"."), + _ => throw new InvalidOperationException($"Invalid input topology \"{context.Definitions.InputTopology}\"."), }); - context.AddExecutionMode(spvFunc, ExecutionMode.Invocations, (SpvLiteralInteger)context.Config.ThreadsPerInputPrimitive); + context.AddExecutionMode(spvFunc, ExecutionMode.Invocations, (SpvLiteralInteger)context.Definitions.ThreadsPerInputPrimitive); - context.AddExecutionMode(spvFunc, context.Config.OutputTopology switch + context.AddExecutionMode(spvFunc, context.Definitions.OutputTopology switch { OutputTopology.PointList => ExecutionMode.OutputPoints, OutputTopology.LineStrip => ExecutionMode.OutputLineStrip, OutputTopology.TriangleStrip => ExecutionMode.OutputTriangleStrip, - _ => throw new InvalidOperationException($"Invalid output topology \"{context.Config.OutputTopology}\"."), + _ => throw new InvalidOperationException($"Invalid output topology \"{context.Definitions.OutputTopology}\"."), }); - int maxOutputVertices = context.Config.GpPassthrough ? context.InputVertices : context.Config.MaxOutputVertices; + int maxOutputVertices = context.Definitions.GpPassthrough ? context.InputVertices : context.Definitions.MaxOutputVertices; context.AddExecutionMode(spvFunc, ExecutionMode.OutputVertices, (SpvLiteralInteger)maxOutputVertices); } - else if (context.Config.Stage == ShaderStage.Fragment) + else if (context.Definitions.Stage == ShaderStage.Fragment) { - context.AddExecutionMode(spvFunc, context.Config.Properties.OriginUpperLeft + context.AddExecutionMode(spvFunc, context.Definitions.OriginUpperLeft ? ExecutionMode.OriginUpperLeft : ExecutionMode.OriginLowerLeft); @@ -260,22 +259,22 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv context.AddExecutionMode(spvFunc, ExecutionMode.DepthReplacing); } - if (context.Config.GpuAccessor.QueryEarlyZForce()) + if (context.Definitions.EarlyZForce) { context.AddExecutionMode(spvFunc, ExecutionMode.EarlyFragmentTests); } if ((info.HelperFunctionsMask & HelperFunctionsMask.FSI) != 0 && - context.Config.GpuAccessor.QueryHostSupportsFragmentShaderInterlock()) + context.HostCapabilities.SupportsFragmentShaderInterlock) { context.AddExecutionMode(spvFunc, ExecutionMode.PixelInterlockOrderedEXT); } } - else if (context.Config.Stage == ShaderStage.Compute) + else if (context.Definitions.Stage == ShaderStage.Compute) { - var localSizeX = (SpvLiteralInteger)context.Config.GpuAccessor.QueryComputeLocalSizeX(); - var localSizeY = (SpvLiteralInteger)context.Config.GpuAccessor.QueryComputeLocalSizeY(); - var localSizeZ = (SpvLiteralInteger)context.Config.GpuAccessor.QueryComputeLocalSizeZ(); + var localSizeX = (SpvLiteralInteger)context.Definitions.ComputeLocalSizeX; + var localSizeY = (SpvLiteralInteger)context.Definitions.ComputeLocalSizeY; + var localSizeZ = (SpvLiteralInteger)context.Definitions.ComputeLocalSizeZ; context.AddExecutionMode( spvFunc, @@ -285,7 +284,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv localSizeZ); } - if (context.Config.TransformFeedbackEnabled && context.Config.LastInVertexPipeline) + if (context.Definitions.TransformFeedbackEnabled && context.Definitions.LastInVertexPipeline) { context.AddExecutionMode(spvFunc, ExecutionMode.Xfb); } |