aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Graphics.Shader/Instructions
diff options
context:
space:
mode:
authorgdkchan <gab.dark.100@gmail.com>2023-04-25 19:51:07 -0300
committerGitHub <noreply@github.com>2023-04-25 19:51:07 -0300
commit9f12e50a546b15533778ed0d8290202af91c10a2 (patch)
treef0e77a7b7c605face5ef29270b4248af2682301a /Ryujinx.Graphics.Shader/Instructions
parent097562bc6c227c42f803ce1078fcb4adf06cd20c (diff)
Refactor attribute handling on the shader generator (#4565)
* Refactor attribute handling on the shader generator * Implement gl_ViewportMask[] * Add back the Intel FrontFacing bug workaround * Fix GLSL transform feedback outputs mistmatch with fragment stage * Shader cache version bump * Fix geometry shader recognition * PR feedback * Delete GetOperandDef and GetOperandUse * Remove replacements that are no longer needed on GLSL compilation on Vulkan * Fix incorrect load for per-patch outputs * Fix build
Diffstat (limited to 'Ryujinx.Graphics.Shader/Instructions')
-rw-r--r--Ryujinx.Graphics.Shader/Instructions/AttributeMap.cs351
-rw-r--r--Ryujinx.Graphics.Shader/Instructions/InstEmitAttribute.cs150
-rw-r--r--Ryujinx.Graphics.Shader/Instructions/InstEmitMemory.cs24
-rw-r--r--Ryujinx.Graphics.Shader/Instructions/InstEmitMove.cs36
4 files changed, 477 insertions, 84 deletions
diff --git a/Ryujinx.Graphics.Shader/Instructions/AttributeMap.cs b/Ryujinx.Graphics.Shader/Instructions/AttributeMap.cs
new file mode 100644
index 00000000..562fb8d5
--- /dev/null
+++ b/Ryujinx.Graphics.Shader/Instructions/AttributeMap.cs
@@ -0,0 +1,351 @@
+using Ryujinx.Graphics.Shader.IntermediateRepresentation;
+using Ryujinx.Graphics.Shader.Translation;
+using System.Collections.Generic;
+
+using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
+
+namespace Ryujinx.Graphics.Shader.Instructions
+{
+ static class AttributeMap
+ {
+ private enum StagesMask : byte
+ {
+ None = 0,
+ Compute = 1 << (int)ShaderStage.Compute,
+ Vertex = 1 << (int)ShaderStage.Vertex,
+ TessellationControl = 1 << (int)ShaderStage.TessellationControl,
+ TessellationEvaluation = 1 << (int)ShaderStage.TessellationEvaluation,
+ Geometry = 1 << (int)ShaderStage.Geometry,
+ Fragment = 1 << (int)ShaderStage.Fragment,
+
+ Tessellation = TessellationControl | TessellationEvaluation,
+ VertexTessellationGeometry = Vertex | Tessellation | Geometry,
+ TessellationGeometryFragment = Tessellation | Geometry | Fragment,
+ AllGraphics = Vertex | Tessellation | Geometry | Fragment
+ }
+
+ private struct AttributeEntry
+ {
+ public int BaseOffset { get; }
+ public AggregateType Type { get; }
+ public IoVariable IoVariable { get; }
+ public StagesMask InputMask { get; }
+ public StagesMask OutputMask { get; }
+
+ public AttributeEntry(
+ int baseOffset,
+ AggregateType type,
+ IoVariable ioVariable,
+ StagesMask inputMask,
+ StagesMask outputMask)
+ {
+ BaseOffset = baseOffset;
+ Type = type;
+ IoVariable = ioVariable;
+ InputMask = inputMask;
+ OutputMask = outputMask;
+ }
+ }
+
+ private static readonly IReadOnlyDictionary<int, AttributeEntry> _attributes;
+ private static readonly IReadOnlyDictionary<int, AttributeEntry> _attributesPerPatch;
+
+ static AttributeMap()
+ {
+ _attributes = CreateMap();
+ _attributesPerPatch = CreatePerPatchMap();
+ }
+
+ private static IReadOnlyDictionary<int, AttributeEntry> CreateMap()
+ {
+ var map = new Dictionary<int, AttributeEntry>();
+
+ Add(map, 0x060, AggregateType.S32, IoVariable.PrimitiveId, StagesMask.TessellationGeometryFragment, StagesMask.Geometry);
+ Add(map, 0x064, AggregateType.S32, IoVariable.Layer, StagesMask.Fragment, StagesMask.VertexTessellationGeometry);
+ Add(map, 0x068, AggregateType.S32, IoVariable.ViewportIndex, StagesMask.Fragment, StagesMask.VertexTessellationGeometry);
+ Add(map, 0x06c, AggregateType.FP32, IoVariable.PointSize, StagesMask.None, StagesMask.VertexTessellationGeometry);
+ Add(map, 0x070, AggregateType.Vector4 | AggregateType.FP32, IoVariable.Position, StagesMask.TessellationGeometryFragment, StagesMask.VertexTessellationGeometry);
+ Add(map, 0x080, AggregateType.Vector4 | AggregateType.FP32, IoVariable.UserDefined, StagesMask.AllGraphics, StagesMask.VertexTessellationGeometry, 32);
+ Add(map, 0x280, AggregateType.Vector4 | AggregateType.FP32, IoVariable.FrontColorDiffuse, StagesMask.TessellationGeometryFragment, StagesMask.VertexTessellationGeometry);
+ Add(map, 0x290, AggregateType.Vector4 | AggregateType.FP32, IoVariable.FrontColorSpecular, StagesMask.TessellationGeometryFragment, StagesMask.VertexTessellationGeometry);
+ Add(map, 0x2a0, AggregateType.Vector4 | AggregateType.FP32, IoVariable.BackColorDiffuse, StagesMask.TessellationGeometryFragment, StagesMask.VertexTessellationGeometry);
+ Add(map, 0x2b0, AggregateType.Vector4 | AggregateType.FP32, IoVariable.BackColorSpecular, StagesMask.TessellationGeometryFragment, StagesMask.VertexTessellationGeometry);
+ Add(map, 0x2c0, AggregateType.Array | AggregateType.FP32, IoVariable.ClipDistance, StagesMask.TessellationGeometryFragment, StagesMask.VertexTessellationGeometry, 8);
+ Add(map, 0x2e0, AggregateType.Vector2 | AggregateType.FP32, IoVariable.PointCoord, StagesMask.Fragment, StagesMask.None);
+ Add(map, 0x2e8, AggregateType.FP32, IoVariable.FogCoord, StagesMask.TessellationGeometryFragment, StagesMask.VertexTessellationGeometry);
+ Add(map, 0x2f0, AggregateType.Vector2 | AggregateType.FP32, IoVariable.TessellationCoord, StagesMask.TessellationEvaluation, StagesMask.None);
+ Add(map, 0x2f8, AggregateType.S32, IoVariable.InstanceId, StagesMask.Vertex, StagesMask.None);
+ Add(map, 0x2fc, AggregateType.S32, IoVariable.VertexId, StagesMask.Vertex, StagesMask.None);
+ Add(map, 0x300, AggregateType.Vector4 | AggregateType.FP32, IoVariable.TextureCoord, StagesMask.TessellationGeometryFragment, StagesMask.VertexTessellationGeometry);
+ Add(map, 0x3a0, AggregateType.Array | AggregateType.S32, IoVariable.ViewportMask, StagesMask.Fragment, StagesMask.VertexTessellationGeometry);
+ Add(map, 0x3fc, AggregateType.Bool, IoVariable.FrontFacing, StagesMask.Fragment, StagesMask.None);
+
+ return map;
+ }
+
+ private static IReadOnlyDictionary<int, AttributeEntry> CreatePerPatchMap()
+ {
+ var map = new Dictionary<int, AttributeEntry>();
+
+ Add(map, 0x000, AggregateType.Vector4 | AggregateType.FP32, IoVariable.TessellationLevelOuter, StagesMask.TessellationEvaluation, StagesMask.TessellationControl);
+ Add(map, 0x010, AggregateType.Vector2 | AggregateType.FP32, IoVariable.TessellationLevelInner, StagesMask.TessellationEvaluation, StagesMask.TessellationControl);
+ Add(map, 0x018, AggregateType.Vector4 | AggregateType.FP32, IoVariable.UserDefined, StagesMask.TessellationEvaluation, StagesMask.TessellationControl, 31, 0x200);
+
+ return map;
+ }
+
+ private static void Add(
+ Dictionary<int, AttributeEntry> attributes,
+ int offset,
+ AggregateType type,
+ IoVariable ioVariable,
+ StagesMask inputMask,
+ StagesMask outputMask,
+ int count = 1,
+ int upperBound = 0x400)
+ {
+ int baseOffset = offset;
+
+ int elementsCount = GetElementCount(type);
+
+ for (int index = 0; index < count; index++)
+ {
+ for (int elementIndex = 0; elementIndex < elementsCount; elementIndex++)
+ {
+ attributes.Add(offset, new AttributeEntry(baseOffset, type, ioVariable, inputMask, outputMask));
+
+ offset += 4;
+
+ if (offset >= upperBound)
+ {
+ return;
+ }
+ }
+ }
+ }
+
+ public static Operand GenerateAttributeLoad(EmitterContext context, Operand primVertex, int offset, bool isOutput, bool isPerPatch)
+ {
+ if (!(isPerPatch ? _attributesPerPatch : _attributes).TryGetValue(offset, out AttributeEntry entry))
+ {
+ context.Config.GpuAccessor.Log($"Attribute offset 0x{offset:X} is not valid.");
+ return Const(0);
+ }
+
+ StagesMask validUseMask = isOutput ? entry.OutputMask : entry.InputMask;
+
+ if (((StagesMask)(1 << (int)context.Config.Stage) & validUseMask) == StagesMask.None)
+ {
+ context.Config.GpuAccessor.Log($"Attribute offset 0x{offset:X} ({entry.IoVariable}) is not valid for stage {context.Config.Stage}.");
+ return Const(0);
+ }
+
+ if (!IsSupportedByHost(context.Config.GpuAccessor, context.Config.Stage, entry.IoVariable))
+ {
+ context.Config.GpuAccessor.Log($"Attribute offset 0x{offset:X} ({entry.IoVariable}) is not supported by the host for stage {context.Config.Stage}.");
+ return Const(0);
+ }
+
+ if (HasInvocationId(context.Config.Stage, isOutput) && !isPerPatch)
+ {
+ primVertex = context.Load(StorageKind.Input, IoVariable.InvocationId);
+ }
+
+ int innerOffset = offset - entry.BaseOffset;
+ int innerIndex = innerOffset / 4;
+
+ StorageKind storageKind = isPerPatch
+ ? (isOutput ? StorageKind.OutputPerPatch : StorageKind.InputPerPatch)
+ : (isOutput ? StorageKind.Output : StorageKind.Input);
+ IoVariable ioVariable = GetIoVariable(context.Config.Stage, in entry);
+ AggregateType type = GetType(context.Config, isOutput, innerIndex, in entry);
+ int elementCount = GetElementCount(type);
+
+ bool isArray = type.HasFlag(AggregateType.Array);
+ bool hasArrayIndex = isArray || context.Config.HasPerLocationInputOrOutput(ioVariable, isOutput);
+
+ bool hasElementIndex = elementCount > 1;
+
+ if (hasArrayIndex && hasElementIndex)
+ {
+ int arrayIndex = innerIndex / elementCount;
+ int elementIndex = innerIndex - (arrayIndex * elementCount);
+
+ return primVertex == null || isArray
+ ? context.Load(storageKind, ioVariable, primVertex, Const(arrayIndex), Const(elementIndex))
+ : context.Load(storageKind, ioVariable, Const(arrayIndex), primVertex, Const(elementIndex));
+ }
+ else if (hasArrayIndex || hasElementIndex)
+ {
+ return primVertex == null || isArray || !hasArrayIndex
+ ? context.Load(storageKind, ioVariable, primVertex, Const(innerIndex))
+ : context.Load(storageKind, ioVariable, Const(innerIndex), primVertex);
+ }
+ else
+ {
+ return context.Load(storageKind, ioVariable, primVertex);
+ }
+ }
+
+ public static void GenerateAttributeStore(EmitterContext context, int offset, bool isPerPatch, Operand value)
+ {
+ if (!(isPerPatch ? _attributesPerPatch : _attributes).TryGetValue(offset, out AttributeEntry entry))
+ {
+ context.Config.GpuAccessor.Log($"Attribute offset 0x{offset:X} is not valid.");
+ return;
+ }
+
+ if (((StagesMask)(1 << (int)context.Config.Stage) & entry.OutputMask) == StagesMask.None)
+ {
+ context.Config.GpuAccessor.Log($"Attribute offset 0x{offset:X} ({entry.IoVariable}) is not valid for stage {context.Config.Stage}.");
+ return;
+ }
+
+ if (!IsSupportedByHost(context.Config.GpuAccessor, context.Config.Stage, entry.IoVariable))
+ {
+ context.Config.GpuAccessor.Log($"Attribute offset 0x{offset:X} ({entry.IoVariable}) is not supported by the host for stage {context.Config.Stage}.");
+ return;
+ }
+
+ Operand invocationId = null;
+
+ if (HasInvocationId(context.Config.Stage, isOutput: true) && !isPerPatch)
+ {
+ invocationId = context.Load(StorageKind.Input, IoVariable.InvocationId);
+ }
+
+ int innerOffset = offset - entry.BaseOffset;
+ int innerIndex = innerOffset / 4;
+
+ StorageKind storageKind = isPerPatch ? StorageKind.OutputPerPatch : StorageKind.Output;
+ IoVariable ioVariable = GetIoVariable(context.Config.Stage, in entry);
+ AggregateType type = GetType(context.Config, isOutput: true, innerIndex, in entry);
+ int elementCount = GetElementCount(type);
+
+ bool isArray = type.HasFlag(AggregateType.Array);
+ bool hasArrayIndex = isArray || context.Config.HasPerLocationInputOrOutput(ioVariable, isOutput: true);
+
+ bool hasElementIndex = elementCount > 1;
+
+ if (hasArrayIndex && hasElementIndex)
+ {
+ int arrayIndex = innerIndex / elementCount;
+ int elementIndex = innerIndex - (arrayIndex * elementCount);
+
+ if (invocationId == null || isArray)
+ {
+ context.Store(storageKind, ioVariable, invocationId, Const(arrayIndex), Const(elementIndex), value);
+ }
+ else
+ {
+ context.Store(storageKind, ioVariable, Const(arrayIndex), invocationId, Const(elementIndex), value);
+ }
+ }
+ else if (hasArrayIndex || hasElementIndex)
+ {
+ if (invocationId == null || isArray || !hasArrayIndex)
+ {
+ context.Store(storageKind, ioVariable, invocationId, Const(innerIndex), value);
+ }
+ else
+ {
+ context.Store(storageKind, ioVariable, Const(innerIndex), invocationId, value);
+ }
+ }
+ else
+ {
+ context.Store(storageKind, ioVariable, invocationId, value);
+ }
+ }
+
+ private static bool IsSupportedByHost(IGpuAccessor gpuAccessor, ShaderStage stage, IoVariable ioVariable)
+ {
+ if (ioVariable == IoVariable.ViewportIndex && stage != ShaderStage.Geometry && stage != ShaderStage.Fragment)
+ {
+ return gpuAccessor.QueryHostSupportsViewportIndexVertexTessellation();
+ }
+ else if (ioVariable == IoVariable.ViewportMask)
+ {
+ return gpuAccessor.QueryHostSupportsViewportMask();
+ }
+
+ return true;
+ }
+
+ public static IoVariable GetIoVariable(ShaderConfig config, int offset, out int location)
+ {
+ location = 0;
+
+ if (!_attributes.TryGetValue(offset, out AttributeEntry entry))
+ {
+ return IoVariable.Invalid;
+ }
+
+ if (((StagesMask)(1 << (int)config.Stage) & entry.OutputMask) == StagesMask.None)
+ {
+ return IoVariable.Invalid;
+ }
+
+ if (config.HasPerLocationInputOrOutput(entry.IoVariable, isOutput: true))
+ {
+ location = (offset - entry.BaseOffset) / 16;
+ }
+
+ return GetIoVariable(config.Stage, in entry);
+ }
+
+ private static IoVariable GetIoVariable(ShaderStage stage, in AttributeEntry entry)
+ {
+ if (entry.IoVariable == IoVariable.Position && stage == ShaderStage.Fragment)
+ {
+ return IoVariable.FragmentCoord;
+ }
+
+ return entry.IoVariable;
+ }
+
+ private static AggregateType GetType(ShaderConfig config, bool isOutput, int innerIndex, in AttributeEntry entry)
+ {
+ AggregateType type = entry.Type;
+
+ if (entry.IoVariable == IoVariable.UserDefined)
+ {
+ type = config.GetUserDefinedType(innerIndex / 4, isOutput);
+ }
+ else if (entry.IoVariable == IoVariable.FragmentOutputColor)
+ {
+ type = config.GetFragmentOutputColorType(innerIndex / 4);
+ }
+
+ return type;
+ }
+
+ public static bool HasPrimitiveVertex(ShaderStage stage, bool isOutput)
+ {
+ if (isOutput)
+ {
+ return false;
+ }
+
+ return stage == ShaderStage.TessellationControl ||
+ stage == ShaderStage.TessellationEvaluation ||
+ stage == ShaderStage.Geometry;
+ }
+
+ public static bool HasInvocationId(ShaderStage stage, bool isOutput)
+ {
+ return isOutput && stage == ShaderStage.TessellationControl;
+ }
+
+ private static int GetElementCount(AggregateType type)
+ {
+ return (type & AggregateType.ElementCountMask) switch
+ {
+ AggregateType.Vector2 => 2,
+ AggregateType.Vector3 => 3,
+ AggregateType.Vector4 => 4,
+ _ => 1
+ };
+ }
+ }
+} \ No newline at end of file
diff --git a/Ryujinx.Graphics.Shader/Instructions/InstEmitAttribute.cs b/Ryujinx.Graphics.Shader/Instructions/InstEmitAttribute.cs
index 9f9ac141..1df38761 100644
--- a/Ryujinx.Graphics.Shader/Instructions/InstEmitAttribute.cs
+++ b/Ryujinx.Graphics.Shader/Instructions/InstEmitAttribute.cs
@@ -20,7 +20,16 @@ namespace Ryujinx.Graphics.Shader.Instructions
{
InstAld op = context.GetOp<InstAld>();
- Operand primVertex = context.Copy(GetSrcReg(context, op.SrcB));
+ // Some of those attributes are per invocation,
+ // so we should ignore any primitive vertex indexing for those.
+ bool hasPrimitiveVertex = AttributeMap.HasPrimitiveVertex(context.Config.Stage, op.O) && !op.P;
+
+ if (!op.Phys)
+ {
+ hasPrimitiveVertex &= HasPrimitiveVertex(op.Imm11);
+ }
+
+ Operand primVertex = hasPrimitiveVertex ? context.Copy(GetSrcReg(context, op.SrcB)) : null;
for (int index = 0; index < (int)op.AlSize + 1; index++)
{
@@ -33,12 +42,13 @@ namespace Ryujinx.Graphics.Shader.Instructions
if (op.Phys)
{
- Operand userAttrOffset = context.ISubtract(GetSrcReg(context, op.SrcA), Const(AttributeConsts.UserAttributeBase));
- Operand userAttrIndex = context.ShiftRightU32(userAttrOffset, Const(2));
+ Operand offset = context.ISubtract(GetSrcReg(context, op.SrcA), Const(AttributeConsts.UserAttributeBase));
+ Operand vecIndex = context.ShiftRightU32(offset, Const(4));
+ Operand elemIndex = context.BitwiseAnd(context.ShiftRightU32(offset, Const(2)), Const(3));
- context.Copy(Register(rd), context.LoadAttribute(Const(AttributeConsts.UserAttributeBase), userAttrIndex, primVertex));
+ StorageKind storageKind = op.O ? StorageKind.Output : StorageKind.Input;
- context.Config.SetUsedFeature(FeatureFlags.IaIndexing);
+ context.Copy(Register(rd), context.Load(storageKind, IoVariable.UserDefined, primVertex, vecIndex, elemIndex));
}
else if (op.SrcB == RegisterConsts.RegisterZeroIndex || op.P)
{
@@ -46,14 +56,16 @@ namespace Ryujinx.Graphics.Shader.Instructions
context.FlagAttributeRead(offset);
- if (op.O && CanLoadOutput(offset))
+ bool isOutput = op.O && CanLoadOutput(offset);
+
+ if (!op.P && !isOutput && TryConvertIdToIndexForVulkan(context, offset, out Operand value))
{
- offset |= AttributeConsts.LoadOutputMask;
+ context.Copy(Register(rd), value);
+ }
+ else
+ {
+ context.Copy(Register(rd), AttributeMap.GenerateAttributeLoad(context, primVertex, offset, isOutput, op.P));
}
-
- Operand src = op.P ? AttributePerPatch(offset) : CreateInputAttribute(context, offset);
-
- context.Copy(Register(rd), src);
}
else
{
@@ -61,14 +73,9 @@ namespace Ryujinx.Graphics.Shader.Instructions
context.FlagAttributeRead(offset);
- if (op.O && CanLoadOutput(offset))
- {
- offset |= AttributeConsts.LoadOutputMask;
- }
-
- Operand src = Const(offset);
+ bool isOutput = op.O && CanLoadOutput(offset);
- context.Copy(Register(rd), context.LoadAttribute(src, Const(0), primVertex));
+ context.Copy(Register(rd), AttributeMap.GenerateAttributeLoad(context, primVertex, offset, isOutput, false));
}
}
}
@@ -88,12 +95,14 @@ namespace Ryujinx.Graphics.Shader.Instructions
if (op.Phys)
{
- Operand userAttrOffset = context.ISubtract(GetSrcReg(context, op.SrcA), Const(AttributeConsts.UserAttributeBase));
- Operand userAttrIndex = context.ShiftRightU32(userAttrOffset, Const(2));
-
- context.StoreAttribute(Const(AttributeConsts.UserAttributeBase), userAttrIndex, Register(rd));
-
- context.Config.SetUsedFeature(FeatureFlags.OaIndexing);
+ Operand offset = context.ISubtract(GetSrcReg(context, op.SrcA), Const(AttributeConsts.UserAttributeBase));
+ Operand vecIndex = context.ShiftRightU32(offset, Const(4));
+ Operand elemIndex = context.BitwiseAnd(context.ShiftRightU32(offset, Const(2)), Const(3));
+ Operand invocationId = AttributeMap.HasInvocationId(context.Config.Stage, isOutput: true)
+ ? context.Load(StorageKind.Input, IoVariable.InvocationId)
+ : null;
+
+ context.Store(StorageKind.Output, IoVariable.UserDefined, invocationId, vecIndex, elemIndex, Register(rd));
}
else
{
@@ -110,9 +119,7 @@ namespace Ryujinx.Graphics.Shader.Instructions
context.FlagAttributeWritten(offset);
- Operand dest = op.P ? AttributePerPatch(offset) : Attribute(offset);
-
- context.Copy(dest, Register(rd));
+ AttributeMap.GenerateAttributeStore(context, offset, op.P, Register(rd));
}
}
}
@@ -129,13 +136,12 @@ namespace Ryujinx.Graphics.Shader.Instructions
if (op.Idx)
{
- Operand userAttrOffset = context.ISubtract(GetSrcReg(context, op.SrcA), Const(AttributeConsts.UserAttributeBase));
- Operand userAttrIndex = context.ShiftRightU32(userAttrOffset, Const(2));
+ Operand offset = context.ISubtract(GetSrcReg(context, op.SrcA), Const(AttributeConsts.UserAttributeBase));
+ Operand vecIndex = context.ShiftRightU32(offset, Const(4));
+ Operand elemIndex = context.BitwiseAnd(context.ShiftRightU32(offset, Const(2)), Const(3));
- res = context.LoadAttribute(Const(AttributeConsts.UserAttributeBase), userAttrIndex, Const(0));
- res = context.FPMultiply(res, Attribute(AttributeConsts.PositionW));
-
- context.Config.SetUsedFeature(FeatureFlags.IaIndexing);
+ res = context.Load(StorageKind.Input, IoVariable.UserDefined, null, vecIndex, elemIndex);
+ res = context.FPMultiply(res, context.Load(StorageKind.Input, IoVariable.FragmentCoord, null, Const(3)));
}
else
{
@@ -147,9 +153,21 @@ namespace Ryujinx.Graphics.Shader.Instructions
if (context.Config.ImapTypes[index].GetFirstUsedType() == PixelImap.Perspective)
{
- res = context.FPMultiply(res, Attribute(AttributeConsts.PositionW));
+ res = context.FPMultiply(res, context.Load(StorageKind.Input, IoVariable.FragmentCoord, null, Const(3)));
}
}
+ else if (op.Imm10 == AttributeConsts.PositionX || op.Imm10 == AttributeConsts.PositionY)
+ {
+ // FragCoord X/Y must be divided by the render target scale, if resolution scaling is active,
+ // because the shader code is not expecting scaled values.
+ res = context.FPDivide(res, context.Load(StorageKind.Input, IoVariable.SupportBlockRenderScale, null, Const(0)));
+ }
+ else if (op.Imm10 == AttributeConsts.FrontFacing && context.Config.GpuAccessor.QueryHostHasFrontFacingBug())
+ {
+ // gl_FrontFacing sometimes has incorrect (flipped) values depending how it is accessed on Intel GPUs.
+ // This weird trick makes it behave.
+ res = context.ICompareLess(context.INegate(context.IConvertS32ToFP32(res)), Const(0));
+ }
}
if (op.IpaOp == IpaOp.Multiply && !isFixedFunc)
@@ -216,17 +234,17 @@ namespace Ryujinx.Graphics.Shader.Instructions
if (tempXLocal != null)
{
- context.Copy(Attribute(AttributeConsts.PositionX), tempXLocal);
+ context.Copy(context.Load(StorageKind.Input, IoVariable.Position, null, Const(0)), tempXLocal);
}
if (tempYLocal != null)
{
- context.Copy(Attribute(AttributeConsts.PositionY), tempYLocal);
+ context.Copy(context.Load(StorageKind.Input, IoVariable.Position, null, Const(1)), tempYLocal);
}
if (tempZLocal != null)
{
- context.Copy(Attribute(AttributeConsts.PositionZ), tempZLocal);
+ context.Copy(context.Load(StorageKind.Input, IoVariable.Position, null, Const(2)), tempZLocal);
}
}
else
@@ -241,6 +259,13 @@ namespace Ryujinx.Graphics.Shader.Instructions
}
}
+ private static bool HasPrimitiveVertex(int attr)
+ {
+ return attr != AttributeConsts.PrimitiveId &&
+ attr != AttributeConsts.TessCoordX &&
+ attr != AttributeConsts.TessCoordY;
+ }
+
private static bool CanLoadOutput(int attr)
{
return attr != AttributeConsts.TessCoordX && attr != AttributeConsts.TessCoordY;
@@ -252,13 +277,13 @@ namespace Ryujinx.Graphics.Shader.Instructions
{
// TODO: If two sided rendering is enabled, then this should return
// FrontColor if the fragment is front facing, and back color otherwise.
- int index = (attr - AttributeConsts.FrontColorDiffuseR) >> 4;
- int userAttrIndex = context.Config.GetFreeUserAttribute(isOutput: false, index);
- Operand frontAttr = Attribute(AttributeConsts.UserAttributeBase + userAttrIndex * 16 + (attr & 0xf));
-
- context.Config.SetInputUserAttributeFixedFunc(userAttrIndex);
-
- selectedAttr = frontAttr;
+ selectedAttr = GenerateIpaLoad(context, FixedFuncToUserAttribute(context.Config, attr, isOutput: false));
+ return true;
+ }
+ else if (attr == AttributeConsts.FogCoord)
+ {
+ // TODO: We likely need to emulate the fixed-function functionality for FogCoord here.
+ selectedAttr = GenerateIpaLoad(context, FixedFuncToUserAttribute(context.Config, attr, isOutput: false));
return true;
}
else if (attr >= AttributeConsts.BackColorDiffuseR && attr < AttributeConsts.ClipDistance0)
@@ -268,14 +293,19 @@ namespace Ryujinx.Graphics.Shader.Instructions
}
else if (attr >= AttributeConsts.TexCoordBase && attr < AttributeConsts.TexCoordEnd)
{
- selectedAttr = Attribute(FixedFuncToUserAttribute(context.Config, attr, AttributeConsts.TexCoordBase, 4, isOutput: false));
+ selectedAttr = GenerateIpaLoad(context, FixedFuncToUserAttribute(context.Config, attr, isOutput: false));
return true;
}
- selectedAttr = Attribute(attr);
+ selectedAttr = GenerateIpaLoad(context, attr);
return false;
}
+ private static Operand GenerateIpaLoad(EmitterContext context, int offset)
+ {
+ return AttributeMap.GenerateAttributeLoad(context, null, offset, isOutput: false, isPerPatch: false);
+ }
+
private static int FixedFuncToUserAttribute(ShaderConfig config, int attr, bool isOutput)
{
bool supportsLayerFromVertexOrTess = config.GpuAccessor.QueryHostSupportsLayerVertexTessellation();
@@ -286,13 +316,17 @@ namespace Ryujinx.Graphics.Shader.Instructions
attr = FixedFuncToUserAttribute(config, attr, AttributeConsts.Layer, 0, isOutput);
config.SetLayerOutputAttribute(attr);
}
+ else if (attr == AttributeConsts.FogCoord)
+ {
+ attr = FixedFuncToUserAttribute(config, attr, AttributeConsts.FogCoord, fixedStartAttr, isOutput);
+ }
else if (attr >= AttributeConsts.FrontColorDiffuseR && attr < AttributeConsts.ClipDistance0)
{
- attr = FixedFuncToUserAttribute(config, attr, AttributeConsts.FrontColorDiffuseR, fixedStartAttr, isOutput);
+ attr = FixedFuncToUserAttribute(config, attr, AttributeConsts.FrontColorDiffuseR, fixedStartAttr + 1, isOutput);
}
else if (attr >= AttributeConsts.TexCoordBase && attr < AttributeConsts.TexCoordEnd)
{
- attr = FixedFuncToUserAttribute(config, attr, AttributeConsts.TexCoordBase, fixedStartAttr + 4, isOutput);
+ attr = FixedFuncToUserAttribute(config, attr, AttributeConsts.TexCoordBase, fixedStartAttr + 5, isOutput);
}
return attr;
@@ -301,11 +335,10 @@ namespace Ryujinx.Graphics.Shader.Instructions
private static int FixedFuncToUserAttribute(ShaderConfig config, int attr, int baseAttr, int baseIndex, bool isOutput)
{
int index = (attr - baseAttr) >> 4;
- int userAttrIndex = config.GetFreeUserAttribute(isOutput, index);
+ int userAttrIndex = config.GetFreeUserAttribute(isOutput, baseIndex + index);
if ((uint)userAttrIndex < Constants.MaxAttributes)
{
- userAttrIndex += baseIndex;
attr = AttributeConsts.UserAttributeBase + userAttrIndex * 16 + (attr & 0xf);
if (isOutput)
@@ -317,25 +350,34 @@ namespace Ryujinx.Graphics.Shader.Instructions
config.SetInputUserAttributeFixedFunc(userAttrIndex);
}
}
+ else
+ {
+ config.GpuAccessor.Log($"No enough user attributes for fixed attribute offset 0x{attr:X}.");
+ }
return attr;
}
- private static Operand CreateInputAttribute(EmitterContext context, int attr)
+ private static bool TryConvertIdToIndexForVulkan(EmitterContext context, int attr, out Operand value)
{
if (context.Config.Options.TargetApi == TargetApi.Vulkan)
{
if (attr == AttributeConsts.InstanceId)
{
- return context.ISubtract(Attribute(AttributeConsts.InstanceIndex), Attribute(AttributeConsts.BaseInstance));
+ value = context.ISubtract(
+ context.Load(StorageKind.Input, IoVariable.InstanceIndex),
+ context.Load(StorageKind.Input, IoVariable.BaseInstance));
+ return true;
}
else if (attr == AttributeConsts.VertexId)
{
- return Attribute(AttributeConsts.VertexIndex);
+ value = context.Load(StorageKind.Input, IoVariable.VertexIndex);
+ return true;
}
}
- return Attribute(attr);
+ value = null;
+ return false;
}
}
} \ No newline at end of file
diff --git a/Ryujinx.Graphics.Shader/Instructions/InstEmitMemory.cs b/Ryujinx.Graphics.Shader/Instructions/InstEmitMemory.cs
index ceb76de1..c73c6b2a 100644
--- a/Ryujinx.Graphics.Shader/Instructions/InstEmitMemory.cs
+++ b/Ryujinx.Graphics.Shader/Instructions/InstEmitMemory.cs
@@ -25,7 +25,7 @@ namespace Ryujinx.Graphics.Shader.Instructions
Operand value = GetSrcReg(context, op.SrcB);
- Operand res = EmitAtomicOp(context, Instruction.MrGlobal, op.Op, op.Size, addrLow, addrHigh, value);
+ Operand res = EmitAtomicOp(context, StorageKind.GlobalMemory, op.Op, op.Size, addrLow, addrHigh, value);
context.Copy(GetDest(op.Dest), res);
}
@@ -50,7 +50,7 @@ namespace Ryujinx.Graphics.Shader.Instructions
_ => AtomSize.U32
};
- Operand res = EmitAtomicOp(context, Instruction.MrShared, op.AtomOp, size, offset, Const(0), value);
+ Operand res = EmitAtomicOp(context, StorageKind.SharedMemory, op.AtomOp, size, offset, Const(0), value);
context.Copy(GetDest(op.Dest), res);
}
@@ -130,7 +130,7 @@ namespace Ryujinx.Graphics.Shader.Instructions
(Operand addrLow, Operand addrHigh) = Get40BitsAddress(context, new Register(op.SrcA, RegisterType.Gpr), op.E, op.Imm20);
- EmitAtomicOp(context, Instruction.MrGlobal, (AtomOp)op.RedOp, op.RedSize, addrLow, addrHigh, GetDest(op.SrcB));
+ EmitAtomicOp(context, StorageKind.GlobalMemory, (AtomOp)op.RedOp, op.RedSize, addrLow, addrHigh, GetDest(op.SrcB));
}
public static void Stg(EmitterContext context)
@@ -156,7 +156,7 @@ namespace Ryujinx.Graphics.Shader.Instructions
private static Operand EmitAtomicOp(
EmitterContext context,
- Instruction mr,
+ StorageKind storageKind,
AtomOp op,
AtomSize type,
Operand addrLow,
@@ -170,7 +170,7 @@ namespace Ryujinx.Graphics.Shader.Instructions
case AtomOp.Add:
if (type == AtomSize.S32 || type == AtomSize.U32)
{
- res = context.AtomicAdd(mr, addrLow, addrHigh, value);
+ res = context.AtomicAdd(storageKind, addrLow, addrHigh, value);
}
else
{
@@ -180,7 +180,7 @@ namespace Ryujinx.Graphics.Shader.Instructions
case AtomOp.And:
if (type == AtomSize.S32 || type == AtomSize.U32)
{
- res = context.AtomicAnd(mr, addrLow, addrHigh, value);
+ res = context.AtomicAnd(storageKind, addrLow, addrHigh, value);
}
else
{
@@ -190,7 +190,7 @@ namespace Ryujinx.Graphics.Shader.Instructions
case AtomOp.Xor:
if (type == AtomSize.S32 || type == AtomSize.U32)
{
- res = context.AtomicXor(mr, addrLow, addrHigh, value);
+ res = context.AtomicXor(storageKind, addrLow, addrHigh, value);
}
else
{
@@ -200,7 +200,7 @@ namespace Ryujinx.Graphics.Shader.Instructions
case AtomOp.Or:
if (type == AtomSize.S32 || type == AtomSize.U32)
{
- res = context.AtomicOr(mr, addrLow, addrHigh, value);
+ res = context.AtomicOr(storageKind, addrLow, addrHigh, value);
}
else
{
@@ -210,11 +210,11 @@ namespace Ryujinx.Graphics.Shader.Instructions
case AtomOp.Max:
if (type == AtomSize.S32)
{
- res = context.AtomicMaxS32(mr, addrLow, addrHigh, value);
+ res = context.AtomicMaxS32(storageKind, addrLow, addrHigh, value);
}
else if (type == AtomSize.U32)
{
- res = context.AtomicMaxU32(mr, addrLow, addrHigh, value);
+ res = context.AtomicMaxU32(storageKind, addrLow, addrHigh, value);
}
else
{
@@ -224,11 +224,11 @@ namespace Ryujinx.Graphics.Shader.Instructions
case AtomOp.Min:
if (type == AtomSize.S32)
{
- res = context.AtomicMinS32(mr, addrLow, addrHigh, value);
+ res = context.AtomicMinS32(storageKind, addrLow, addrHigh, value);
}
else if (type == AtomSize.U32)
{
- res = context.AtomicMinU32(mr, addrLow, addrHigh, value);
+ res = context.AtomicMinU32(storageKind, addrLow, addrHigh, value);
}
else
{
diff --git a/Ryujinx.Graphics.Shader/Instructions/InstEmitMove.cs b/Ryujinx.Graphics.Shader/Instructions/InstEmitMove.cs
index 16b02f97..9992ac37 100644
--- a/Ryujinx.Graphics.Shader/Instructions/InstEmitMove.cs
+++ b/Ryujinx.Graphics.Shader/Instructions/InstEmitMove.cs
@@ -76,11 +76,11 @@ namespace Ryujinx.Graphics.Shader.Instructions
switch (op.SReg)
{
case SReg.LaneId:
- src = Attribute(AttributeConsts.LaneId);
+ src = context.Load(StorageKind.Input, IoVariable.SubgroupLaneId);
break;
case SReg.InvocationId:
- src = Attribute(AttributeConsts.InvocationId);
+ src = context.Load(StorageKind.Input, IoVariable.InvocationId);
break;
case SReg.YDirection:
@@ -88,7 +88,7 @@ namespace Ryujinx.Graphics.Shader.Instructions
break;
case SReg.ThreadKill:
- src = context.Config.Stage == ShaderStage.Fragment ? Attribute(AttributeConsts.ThreadKill) : Const(0);
+ src = context.Config.Stage == ShaderStage.Fragment ? context.Load(StorageKind.Input, IoVariable.ThreadKill) : Const(0);
break;
case SReg.InvocationInfo:
@@ -101,7 +101,7 @@ namespace Ryujinx.Graphics.Shader.Instructions
if (context.Config.Stage == ShaderStage.TessellationControl ||
context.Config.Stage == ShaderStage.TessellationEvaluation)
{
- src = context.ShiftLeft(Attribute(AttributeConsts.PatchVerticesIn), Const(16));
+ src = context.ShiftLeft(context.Load(StorageKind.Input, IoVariable.PatchVertices), Const(16));
}
else
{
@@ -115,9 +115,9 @@ namespace Ryujinx.Graphics.Shader.Instructions
break;
case SReg.TId:
- Operand tidX = Attribute(AttributeConsts.ThreadIdX);
- Operand tidY = Attribute(AttributeConsts.ThreadIdY);
- Operand tidZ = Attribute(AttributeConsts.ThreadIdZ);
+ Operand tidX = context.Load(StorageKind.Input, IoVariable.ThreadId, null, Const(0));
+ Operand tidY = context.Load(StorageKind.Input, IoVariable.ThreadId, null, Const(1));
+ Operand tidZ = context.Load(StorageKind.Input, IoVariable.ThreadId, null, Const(2));
tidY = context.ShiftLeft(tidY, Const(16));
tidZ = context.ShiftLeft(tidZ, Const(26));
@@ -126,39 +126,39 @@ namespace Ryujinx.Graphics.Shader.Instructions
break;
case SReg.TIdX:
- src = Attribute(AttributeConsts.ThreadIdX);
+ src = context.Load(StorageKind.Input, IoVariable.ThreadId, null, Const(0));
break;
case SReg.TIdY:
- src = Attribute(AttributeConsts.ThreadIdY);
+ src = context.Load(StorageKind.Input, IoVariable.ThreadId, null, Const(1));
break;
case SReg.TIdZ:
- src = Attribute(AttributeConsts.ThreadIdZ);
+ src = context.Load(StorageKind.Input, IoVariable.ThreadId, null, Const(2));
break;
case SReg.CtaIdX:
- src = Attribute(AttributeConsts.CtaIdX);
+ src = context.Load(StorageKind.Input, IoVariable.CtaId, null, Const(0));
break;
case SReg.CtaIdY:
- src = Attribute(AttributeConsts.CtaIdY);
+ src = context.Load(StorageKind.Input, IoVariable.CtaId, null, Const(1));
break;
case SReg.CtaIdZ:
- src = Attribute(AttributeConsts.CtaIdZ);
+ src = context.Load(StorageKind.Input, IoVariable.CtaId, null, Const(2));
break;
case SReg.EqMask:
- src = Attribute(AttributeConsts.EqMask);
+ src = context.Load(StorageKind.Input, IoVariable.SubgroupEqMask, null, Const(0));
break;
case SReg.LtMask:
- src = Attribute(AttributeConsts.LtMask);
+ src = context.Load(StorageKind.Input, IoVariable.SubgroupLtMask, null, Const(0));
break;
case SReg.LeMask:
- src = Attribute(AttributeConsts.LeMask);
+ src = context.Load(StorageKind.Input, IoVariable.SubgroupLeMask, null, Const(0));
break;
case SReg.GtMask:
- src = Attribute(AttributeConsts.GtMask);
+ src = context.Load(StorageKind.Input, IoVariable.SubgroupGtMask, null, Const(0));
break;
case SReg.GeMask:
- src = Attribute(AttributeConsts.GeMask);
+ src = context.Load(StorageKind.Input, IoVariable.SubgroupGeMask, null, Const(0));
break;
default: