Import the base tree (MIT, see LICENSE)

This commit is contained in:
Valiantsin Dzerakh
2026-11-04 22:16:41 +03:00
committed by chrg6
commit 0478c13e45
734 changed files with 233953 additions and 0 deletions

View File

@@ -0,0 +1,59 @@
using System;
namespace Ryujinx.Graphics.Shader.Translation
{
[Flags]
enum AggregateType
{
Invalid,
Void,
Bool,
FP32,
FP64,
S32,
U32,
ElementTypeMask = 0xff,
ElementCountShift = 8,
ElementCountMask = 3 << ElementCountShift,
Scalar = 0 << ElementCountShift,
Vector2 = 1 << ElementCountShift,
Vector3 = 2 << ElementCountShift,
Vector4 = 3 << ElementCountShift,
Array = 1 << 10,
}
static class AggregateTypeExtensions
{
public static int GetSizeInBytes(this AggregateType type)
{
int elementSize = (type & AggregateType.ElementTypeMask) switch
{
AggregateType.Bool or
AggregateType.FP32 or
AggregateType.S32 or
AggregateType.U32 => 4,
AggregateType.FP64 => 8,
_ => 0,
};
switch (type & AggregateType.ElementCountMask)
{
case AggregateType.Vector2:
elementSize *= 2;
break;
case AggregateType.Vector3:
elementSize *= 3;
break;
case AggregateType.Vector4:
elementSize *= 4;
break;
}
return elementSize;
}
}
}

View File

@@ -0,0 +1,38 @@
namespace Ryujinx.Graphics.Shader.Translation
{
static class AttributeConsts
{
public const int PrimitiveId = 0x060;
public const int Layer = 0x064;
public const int ViewportIndex = 0x068;
public const int PositionX = 0x070;
public const int PositionY = 0x074;
public const int FrontColorDiffuseR = 0x280;
public const int BackColorDiffuseR = 0x2a0;
public const int ClipDistance0 = 0x2c0;
public const int ClipDistance1 = 0x2c4;
public const int ClipDistance2 = 0x2c8;
public const int ClipDistance3 = 0x2cc;
public const int ClipDistance4 = 0x2d0;
public const int ClipDistance5 = 0x2d4;
public const int ClipDistance6 = 0x2d8;
public const int ClipDistance7 = 0x2dc;
public const int FogCoord = 0x2e8;
public const int TessCoordX = 0x2f0;
public const int TessCoordY = 0x2f4;
public const int InstanceId = 0x2f8;
public const int VertexId = 0x2fc;
public const int TexCoordCount = 10;
public const int TexCoordBase = 0x300;
public const int TexCoordEnd = TexCoordBase + TexCoordCount * 16;
public const int ViewportMask = 0x3a0;
public const int FrontFacing = 0x3fc;
public const int UserAttributesCount = 32;
public const int UserAttributeBase = 0x80;
public const int UserAttributeEnd = UserAttributeBase + UserAttributesCount * 16;
public const int UserAttributePerPatchBase = 0x18;
public const int UserAttributePerPatchEnd = 0x200;
}
}

View File

@@ -0,0 +1,168 @@
using System;
using System.Collections.Generic;
using System.Numerics;
namespace Ryujinx.Graphics.Shader.Translation
{
class AttributeUsage
{
public bool NextUsesFixedFuncAttributes { get; private set; }
public int UsedInputAttributes { get; private set; }
public int UsedOutputAttributes { get; private set; }
public HashSet<int> UsedInputAttributesPerPatch { get; }
public HashSet<int> UsedOutputAttributesPerPatch { get; }
public HashSet<int> NextUsedInputAttributesPerPatch { get; private set; }
public int PassthroughAttributes { get; private set; }
private int _nextUsedInputAttributes;
private int _thisUsedInputAttributes;
private Dictionary<int, int> _perPatchAttributeLocations;
private readonly IGpuAccessor _gpuAccessor;
public UInt128 NextInputAttributesComponents { get; private set; }
public UInt128 ThisInputAttributesComponents { get; private set; }
public AttributeUsage(IGpuAccessor gpuAccessor)
{
_gpuAccessor = gpuAccessor;
UsedInputAttributesPerPatch = [];
UsedOutputAttributesPerPatch = [];
}
public void SetInputUserAttribute(int index, int component)
{
int mask = 1 << index;
UsedInputAttributes |= mask;
_thisUsedInputAttributes |= mask;
ThisInputAttributesComponents |= UInt128.One << (index * 4 + component);
}
public void SetInputUserAttributePerPatch(int index)
{
UsedInputAttributesPerPatch.Add(index);
}
public void SetOutputUserAttribute(int index)
{
UsedOutputAttributes |= 1 << index;
}
public void SetOutputUserAttributePerPatch(int index)
{
UsedOutputAttributesPerPatch.Add(index);
}
public void MergeFromtNextStage(bool gpPassthrough, bool nextUsesFixedFunctionAttributes, AttributeUsage nextStage)
{
NextInputAttributesComponents = nextStage.ThisInputAttributesComponents;
NextUsedInputAttributesPerPatch = nextStage.UsedInputAttributesPerPatch;
NextUsesFixedFuncAttributes = nextUsesFixedFunctionAttributes;
MergeOutputUserAttributes(gpPassthrough, nextStage.UsedInputAttributes, nextStage.UsedInputAttributesPerPatch);
if (UsedOutputAttributesPerPatch.Count != 0)
{
// Regular and per-patch input/output locations can't overlap,
// so we must assign on our location using unused regular input/output locations.
Dictionary<int, int> locationsMap = new();
int freeMask = ~UsedOutputAttributes;
foreach (int attr in UsedOutputAttributesPerPatch)
{
int location = BitOperations.TrailingZeroCount(freeMask);
if (location == 32)
{
_gpuAccessor.Log($"No enough free locations for patch input/output 0x{attr:X}.");
break;
}
locationsMap.Add(attr, location);
freeMask &= ~(1 << location);
}
// Both stages must agree on the locations, so use the same "map" for both.
_perPatchAttributeLocations = locationsMap;
nextStage._perPatchAttributeLocations = locationsMap;
}
}
private void MergeOutputUserAttributes(bool gpPassthrough, int mask, IEnumerable<int> perPatch)
{
_nextUsedInputAttributes = mask;
if (gpPassthrough)
{
PassthroughAttributes = mask & ~UsedOutputAttributes;
}
else
{
UsedOutputAttributes |= mask;
UsedOutputAttributesPerPatch.UnionWith(perPatch);
}
}
public int GetPerPatchAttributeLocation(int index)
{
if (_perPatchAttributeLocations == null || !_perPatchAttributeLocations.TryGetValue(index, out int location))
{
return index;
}
return location;
}
public bool IsUsedOutputAttribute(int attr)
{
// The check for fixed function attributes on the next stage is conservative,
// returning false if the output is just not used by the next stage is also valid.
if (NextUsesFixedFuncAttributes &&
attr >= AttributeConsts.UserAttributeBase &&
attr < AttributeConsts.UserAttributeEnd)
{
int index = (attr - AttributeConsts.UserAttributeBase) >> 4;
return (_nextUsedInputAttributes & (1 << index)) != 0;
}
return true;
}
public int GetFreeUserAttribute(bool isOutput, int index)
{
int useMask = isOutput ? _nextUsedInputAttributes : _thisUsedInputAttributes;
int bit = -1;
while (useMask != -1)
{
bit = BitOperations.TrailingZeroCount(~useMask);
if (bit == 32)
{
bit = -1;
break;
}
else if (index < 1)
{
break;
}
useMask |= 1 << bit;
index--;
}
return bit;
}
public void SetAllInputUserAttributes()
{
UsedInputAttributes |= Constants.AllAttributesMask;
ThisInputAttributesComponents |= ~UInt128.Zero >> (128 - Constants.MaxAttributes * 4);
}
public void SetAllOutputUserAttributes()
{
UsedOutputAttributes |= Constants.AllAttributesMask;
}
}
}

View File

@@ -0,0 +1,176 @@
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
using System.Collections.Generic;
namespace Ryujinx.Graphics.Shader.Translation
{
class ControlFlowGraph
{
public BasicBlock[] Blocks { get; }
public BasicBlock[] PostOrderBlocks { get; }
public int[] PostOrderMap { get; }
public ControlFlowGraph(BasicBlock[] blocks)
{
Blocks = blocks;
HashSet<BasicBlock> visited = [];
Stack<BasicBlock> blockStack = new();
List<BasicBlock> postOrderBlocks = new(blocks.Length);
PostOrderMap = new int[blocks.Length];
visited.Add(blocks[0]);
blockStack.Push(blocks[0]);
while (blockStack.TryPop(out BasicBlock block))
{
if (block.Next != null && visited.Add(block.Next))
{
blockStack.Push(block);
blockStack.Push(block.Next);
}
else if (block.Branch != null && visited.Add(block.Branch))
{
blockStack.Push(block);
blockStack.Push(block.Branch);
}
else
{
PostOrderMap[block.Index] = postOrderBlocks.Count;
postOrderBlocks.Add(block);
}
}
PostOrderBlocks = postOrderBlocks.ToArray();
}
public static ControlFlowGraph Create(Operation[] operations)
{
Dictionary<Operand, BasicBlock> labels = new();
List<BasicBlock> blocks = [];
BasicBlock currentBlock = null;
void NextBlock(BasicBlock nextBlock)
{
if (currentBlock != null && !EndsWithUnconditionalInst(currentBlock.GetLastOp()))
{
currentBlock.Next = nextBlock;
}
currentBlock = nextBlock;
}
void NewNextBlock()
{
BasicBlock block = new(blocks.Count);
blocks.Add(block);
NextBlock(block);
}
bool needsNewBlock = true;
for (int index = 0; index < operations.Length; index++)
{
Operation operation = operations[index];
if (operation.Inst == Instruction.MarkLabel)
{
Operand label = operation.Dest;
if (labels.TryGetValue(label, out BasicBlock nextBlock))
{
nextBlock.Index = blocks.Count;
blocks.Add(nextBlock);
NextBlock(nextBlock);
}
else
{
NewNextBlock();
labels.Add(label, currentBlock);
}
}
else
{
if (needsNewBlock)
{
NewNextBlock();
}
currentBlock.Operations.AddLast(operation);
}
needsNewBlock = operation.Inst is Instruction.Branch or
Instruction.BranchIfTrue or
Instruction.BranchIfFalse;
if (needsNewBlock)
{
Operand label = operation.Dest;
if (!labels.TryGetValue(label, out BasicBlock branchBlock))
{
branchBlock = new BasicBlock();
labels.Add(label, branchBlock);
}
currentBlock.Branch = branchBlock;
}
}
// Remove unreachable blocks.
bool hasUnreachable;
do
{
hasUnreachable = false;
for (int blkIndex = 1; blkIndex < blocks.Count; blkIndex++)
{
BasicBlock block = blocks[blkIndex];
if (block.Predecessors.Count == 0)
{
block.Next = null;
block.Branch = null;
blocks.RemoveAt(blkIndex--);
hasUnreachable = true;
}
else
{
block.Index = blkIndex;
}
}
} while (hasUnreachable);
return new ControlFlowGraph(blocks.ToArray());
}
private static bool EndsWithUnconditionalInst(INode node)
{
if (node is Operation operation)
{
switch (operation.Inst)
{
case Instruction.Branch:
case Instruction.Discard:
case Instruction.Return:
return true;
}
}
return false;
}
}
}

View File

@@ -0,0 +1,94 @@
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
namespace Ryujinx.Graphics.Shader.Translation
{
static class Dominance
{
// Those methods are an implementation of the algorithms on "A Simple, Fast Dominance Algorithm".
// https://www.cs.rice.edu/~keith/EMBED/dom.pdf
public static void FindDominators(ControlFlowGraph cfg)
{
BasicBlock Intersect(BasicBlock block1, BasicBlock block2)
{
while (block1 != block2)
{
while (cfg.PostOrderMap[block1.Index] < cfg.PostOrderMap[block2.Index])
{
block1 = block1.ImmediateDominator;
}
while (cfg.PostOrderMap[block2.Index] < cfg.PostOrderMap[block1.Index])
{
block2 = block2.ImmediateDominator;
}
}
return block1;
}
cfg.Blocks[0].ImmediateDominator = cfg.Blocks[0];
bool modified;
do
{
modified = false;
for (int blkIndex = cfg.PostOrderBlocks.Length - 2; blkIndex >= 0; blkIndex--)
{
BasicBlock block = cfg.PostOrderBlocks[blkIndex];
BasicBlock newIDom = null;
foreach (BasicBlock predecessor in block.Predecessors)
{
if (predecessor.ImmediateDominator != null)
{
if (newIDom != null)
{
newIDom = Intersect(predecessor, newIDom);
}
else
{
newIDom = predecessor;
}
}
}
if (block.ImmediateDominator != newIDom)
{
block.ImmediateDominator = newIDom;
modified = true;
}
}
}
while (modified);
}
public static void FindDominanceFrontiers(BasicBlock[] blocks)
{
for (int blkIndex = 0; blkIndex < blocks.Length; blkIndex++)
{
BasicBlock block = blocks[blkIndex];
if (block.Predecessors.Count < 2)
{
continue;
}
for (int pBlkIndex = 0; pBlkIndex < block.Predecessors.Count; pBlkIndex++)
{
BasicBlock current = block.Predecessors[pBlkIndex];
while (current != block.ImmediateDominator)
{
current.DominanceFrontiers.Add(block);
current = current.ImmediateDominator;
}
}
}
}
}
}

View File

@@ -0,0 +1,628 @@
using Ryujinx.Graphics.Shader.Decoders;
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using System.Runtime.CompilerServices;
using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
namespace Ryujinx.Graphics.Shader.Translation
{
class EmitterContext
{
public DecodedProgram Program { get; }
public TranslatorContext TranslatorContext { get; }
public ResourceManager ResourceManager { get; }
public bool VertexAsCompute { get; }
public bool IsNonMain { get; }
public Block CurrBlock { get; set; }
public InstOp CurrOp { get; set; }
public int OperationsCount => _operations.Count;
private readonly struct BrxTarget
{
public readonly Operand Selector;
public readonly int ExpectedValue;
public readonly ulong NextTargetAddress;
public BrxTarget(Operand selector, int expectedValue, ulong nextTargetAddress)
{
Selector = selector;
ExpectedValue = expectedValue;
NextTargetAddress = nextTargetAddress;
}
}
private class BlockLabel
{
public readonly Operand Label;
public BrxTarget BrxTarget;
public BlockLabel(Operand label)
{
Label = label;
}
}
private readonly List<Operation> _operations;
private readonly Dictionary<ulong, BlockLabel> _labels;
public EmitterContext()
{
_operations = [];
_labels = new Dictionary<ulong, BlockLabel>();
}
public EmitterContext(
TranslatorContext translatorContext,
ResourceManager resourceManager,
DecodedProgram program,
bool vertexAsCompute,
bool isNonMain) : this()
{
TranslatorContext = translatorContext;
ResourceManager = resourceManager;
Program = program;
VertexAsCompute = vertexAsCompute;
IsNonMain = isNonMain;
EmitStart();
}
private void EmitStart()
{
if (TranslatorContext.Options.Flags.HasFlag(TranslationFlags.VertexA))
{
return;
}
// Vulkan requires the point size to be always written on the shader if the primitive topology is points.
// OpenGL requires the point size to be always written on the shader if PROGRAM_POINT_SIZE is set.
if (TranslatorContext.Definitions.Stage == ShaderStage.Vertex)
{
this.Store(StorageKind.Output, IoVariable.PointSize, null, ConstF(TranslatorContext.Definitions.PointSize));
}
if (VertexAsCompute)
{
int vertexInfoCbBinding = ResourceManager.Reservations.VertexInfoConstantBufferBinding;
int countFieldIndex = TranslatorContext.Stage == ShaderStage.Vertex
? (int)VertexInfoBufferField.VertexCounts
: (int)VertexInfoBufferField.GeometryCounts;
Operand outputVertexOffset = this.Load(StorageKind.Input, IoVariable.GlobalId, Const(0));
Operand vertexCount = this.Load(StorageKind.ConstantBuffer, vertexInfoCbBinding, Const(countFieldIndex), Const(0));
Operand isVertexOob = this.ICompareGreaterOrEqualUnsigned(outputVertexOffset, vertexCount);
Operand lblVertexInBounds = Label();
this.BranchIfFalse(lblVertexInBounds, isVertexOob);
this.Return();
this.MarkLabel(lblVertexInBounds);
Operand outputInstanceOffset = this.Load(StorageKind.Input, IoVariable.GlobalId, Const(1));
Operand instanceCount = this.Load(StorageKind.ConstantBuffer, vertexInfoCbBinding, Const((int)VertexInfoBufferField.VertexCounts), Const(1));
Operand firstVertex = this.Load(StorageKind.ConstantBuffer, vertexInfoCbBinding, Const((int)VertexInfoBufferField.VertexCounts), Const(2));
Operand firstInstance = this.Load(StorageKind.ConstantBuffer, vertexInfoCbBinding, Const((int)VertexInfoBufferField.VertexCounts), Const(3));
Operand ibBaseOffset = this.Load(StorageKind.ConstantBuffer, vertexInfoCbBinding, Const((int)VertexInfoBufferField.GeometryCounts), Const(3));
Operand isInstanceOob = this.ICompareGreaterOrEqualUnsigned(outputInstanceOffset, instanceCount);
Operand lblInstanceInBounds = Label();
this.BranchIfFalse(lblInstanceInBounds, isInstanceOob);
this.Return();
this.MarkLabel(lblInstanceInBounds);
if (TranslatorContext.Stage == ShaderStage.Vertex)
{
Operand vertexIndexVr = Local();
this.TextureSample(
SamplerType.TextureBuffer,
TextureFlags.IntCoords,
ResourceManager.Reservations.GetIndexBufferTextureSetAndBinding(),
1,
[vertexIndexVr],
[this.IAdd(ibBaseOffset, outputVertexOffset)]);
this.Store(StorageKind.LocalMemory, ResourceManager.LocalVertexIndexVertexRateMemoryId, this.IAdd(firstVertex, vertexIndexVr));
this.Store(StorageKind.LocalMemory, ResourceManager.LocalVertexIndexInstanceRateMemoryId, this.IAdd(firstInstance, outputInstanceOffset));
}
else if (TranslatorContext.Stage == ShaderStage.Geometry)
{
int inputVertices = TranslatorContext.Definitions.InputTopology.InputVertexCount;
Operand baseVertex = this.IMultiply(outputVertexOffset, Const(inputVertices));
for (int index = 0; index < inputVertices; index++)
{
Operand vertexIndex = Local();
this.TextureSample(
SamplerType.TextureBuffer,
TextureFlags.IntCoords,
ResourceManager.Reservations.GetTopologyRemapBufferTextureSetAndBinding(),
1,
[vertexIndex],
[this.IAdd(baseVertex, Const(index))]);
this.Store(StorageKind.LocalMemory, ResourceManager.LocalTopologyRemapMemoryId, Const(index), vertexIndex);
}
this.Store(StorageKind.LocalMemory, ResourceManager.LocalGeometryOutputVertexCountMemoryId, Const(0));
this.Store(StorageKind.LocalMemory, ResourceManager.LocalGeometryOutputIndexCountMemoryId, Const(0));
}
}
}
public T GetOp<T>() where T : unmanaged
{
Debug.Assert(Unsafe.SizeOf<T>() == sizeof(ulong));
ulong op = CurrOp.RawOpCode;
return Unsafe.As<ulong, T>(ref op);
}
public Operand Add(Instruction inst, Operand dest = null, params Operand[] sources)
{
Operation operation = new(inst, dest, sources);
_operations.Add(operation);
return dest;
}
public Operand Add(Instruction inst, StorageKind storageKind, Operand dest = null, params Operand[] sources)
{
Operation operation = new(inst, storageKind, dest, sources);
_operations.Add(operation);
return dest;
}
public (Operand, Operand) Add(Instruction inst, (Operand, Operand) dest, params Operand[] sources)
{
Operand[] dests = [dest.Item1, dest.Item2];
Operation operation = new(inst, 0, dests, sources);
Add(operation);
return dest;
}
public void Add(Operation operation)
{
_operations.Add(operation);
}
public void MarkLabel(Operand label)
{
Add(Instruction.MarkLabel, label);
}
public Operand GetLabel(ulong address)
{
return EnsureBlockLabel(address).Label;
}
public void SetBrxTarget(ulong address, Operand selector, int targetValue, ulong nextTargetAddress)
{
BlockLabel blockLabel = EnsureBlockLabel(address);
Debug.Assert(blockLabel.BrxTarget.Selector == null);
blockLabel.BrxTarget = new BrxTarget(selector, targetValue, nextTargetAddress);
}
public void EnterBlock(ulong address)
{
BlockLabel blockLabel = EnsureBlockLabel(address);
MarkLabel(blockLabel.Label);
BrxTarget brxTarget = blockLabel.BrxTarget;
if (brxTarget.Selector != null)
{
this.BranchIfFalse(GetLabel(brxTarget.NextTargetAddress), this.ICompareEqual(brxTarget.Selector, Const(brxTarget.ExpectedValue)));
}
}
private BlockLabel EnsureBlockLabel(ulong address)
{
if (!_labels.TryGetValue(address, out BlockLabel blockLabel))
{
blockLabel = new BlockLabel(Label());
_labels.Add(address, blockLabel);
}
return blockLabel;
}
public void PrepareForVertexReturn()
{
// TODO: Support transform feedback emulation on stages other than vertex.
// Those stages might produce more primitives, so it needs a way to "compact" the output after it is written.
if (!TranslatorContext.GpuAccessor.QueryHostSupportsTransformFeedback() &&
TranslatorContext.GpuAccessor.QueryTransformFeedbackEnabled() &&
TranslatorContext.Stage == ShaderStage.Vertex)
{
Operand vertexCount = this.Load(StorageKind.ConstantBuffer, SupportBuffer.Binding, Const((int)SupportBufferField.TfeVertexCount));
for (int tfbIndex = 0; tfbIndex < ResourceReservations.TfeBuffersCount; tfbIndex++)
{
ReadOnlySpan<byte> locations = TranslatorContext.GpuAccessor.QueryTransformFeedbackVaryingLocations(tfbIndex);
int stride = TranslatorContext.GpuAccessor.QueryTransformFeedbackStride(tfbIndex);
Operand baseOffset = this.Load(StorageKind.ConstantBuffer, SupportBuffer.Binding, Const((int)SupportBufferField.TfeOffset), Const(tfbIndex));
Operand baseVertex = this.Load(StorageKind.Input, IoVariable.BaseVertex);
Operand baseInstance = this.Load(StorageKind.Input, IoVariable.BaseInstance);
Operand vertexIndex = this.Load(StorageKind.Input, IoVariable.VertexIndex);
Operand instanceIndex = this.Load(StorageKind.Input, IoVariable.InstanceIndex);
Operand outputVertexOffset = this.ISubtract(vertexIndex, baseVertex);
Operand outputInstanceOffset = this.ISubtract(instanceIndex, baseInstance);
Operand outputBaseVertex = this.IMultiply(outputInstanceOffset, vertexCount);
Operand vertexOffset = this.IMultiply(this.IAdd(outputBaseVertex, outputVertexOffset), Const(stride / 4));
baseOffset = this.IAdd(baseOffset, vertexOffset);
for (int j = 0; j < locations.Length; j++)
{
byte location = locations[j];
if (location == 0xff)
{
continue;
}
Operand offset = this.IAdd(baseOffset, Const(j));
Operand value = Instructions.AttributeMap.GenerateAttributeLoad(this, null, location * 4, isOutput: true, isPerPatch: false);
int binding = ResourceManager.Reservations.GetTfeBufferStorageBufferBinding(tfbIndex);
this.Store(StorageKind.StorageBuffer, binding, Const(0), offset, value);
}
}
}
if (TranslatorContext.Definitions.ViewportTransformDisable)
{
Operand x = this.Load(StorageKind.Output, IoVariable.Position, null, Const(0));
Operand y = this.Load(StorageKind.Output, IoVariable.Position, null, Const(1));
Operand xScale = this.Load(StorageKind.ConstantBuffer, SupportBuffer.Binding, Const((int)SupportBufferField.ViewportInverse), Const(0));
Operand yScale = this.Load(StorageKind.ConstantBuffer, SupportBuffer.Binding, Const((int)SupportBufferField.ViewportInverse), Const(1));
Operand negativeOne = ConstF(-1.0f);
this.Store(StorageKind.Output, IoVariable.Position, null, Const(0), this.FPFusedMultiplyAdd(x, xScale, negativeOne));
this.Store(StorageKind.Output, IoVariable.Position, null, Const(1), this.FPFusedMultiplyAdd(y, yScale, negativeOne));
}
if (TranslatorContext.Definitions.DepthMode && !TranslatorContext.GpuAccessor.QueryHostSupportsDepthClipControl())
{
Operand z = this.Load(StorageKind.Output, IoVariable.Position, null, Const(2));
Operand w = this.Load(StorageKind.Output, IoVariable.Position, null, Const(3));
Operand halfW = this.FPMultiply(w, ConstF(0.5f));
this.Store(StorageKind.Output, IoVariable.Position, null, Const(2), this.FPFusedMultiplyAdd(z, ConstF(0.5f), halfW));
}
}
public void PrepareForVertexReturn(out Operand oldXLocal, out Operand oldYLocal, out Operand oldZLocal)
{
if (TranslatorContext.Definitions.ViewportTransformDisable)
{
oldXLocal = Local();
this.Copy(oldXLocal, this.Load(StorageKind.Output, IoVariable.Position, null, Const(0)));
oldYLocal = Local();
this.Copy(oldYLocal, this.Load(StorageKind.Output, IoVariable.Position, null, Const(1)));
}
else
{
oldXLocal = null;
oldYLocal = null;
}
if (TranslatorContext.Definitions.DepthMode && !TranslatorContext.GpuAccessor.QueryHostSupportsDepthClipControl())
{
oldZLocal = Local();
this.Copy(oldZLocal, this.Load(StorageKind.Output, IoVariable.Position, null, Const(2)));
}
else
{
oldZLocal = null;
}
PrepareForVertexReturn();
}
public bool PrepareForReturn()
{
if (IsNonMain)
{
return true;
}
if (TranslatorContext.Definitions.LastInVertexPipeline &&
(TranslatorContext.Definitions.Stage == ShaderStage.Vertex || TranslatorContext.Definitions.Stage == ShaderStage.TessellationEvaluation) &&
(TranslatorContext.Options.Flags & TranslationFlags.VertexA) == 0)
{
PrepareForVertexReturn();
}
else if (TranslatorContext.Definitions.Stage == ShaderStage.Geometry)
{
void WritePositionOutput(int primIndex)
{
Operand x = this.Load(StorageKind.Input, IoVariable.Position, Const(primIndex), Const(0));
Operand y = this.Load(StorageKind.Input, IoVariable.Position, Const(primIndex), Const(1));
Operand z = this.Load(StorageKind.Input, IoVariable.Position, Const(primIndex), Const(2));
Operand w = this.Load(StorageKind.Input, IoVariable.Position, Const(primIndex), Const(3));
this.Store(StorageKind.Output, IoVariable.Position, null, Const(0), x);
this.Store(StorageKind.Output, IoVariable.Position, null, Const(1), y);
this.Store(StorageKind.Output, IoVariable.Position, null, Const(2), z);
this.Store(StorageKind.Output, IoVariable.Position, null, Const(3), w);
}
void WriteUserDefinedOutput(int index, int primIndex)
{
Operand x = this.Load(StorageKind.Input, IoVariable.UserDefined, Const(index), Const(primIndex), Const(0));
Operand y = this.Load(StorageKind.Input, IoVariable.UserDefined, Const(index), Const(primIndex), Const(1));
Operand z = this.Load(StorageKind.Input, IoVariable.UserDefined, Const(index), Const(primIndex), Const(2));
Operand w = this.Load(StorageKind.Input, IoVariable.UserDefined, Const(index), Const(primIndex), Const(3));
this.Store(StorageKind.Output, IoVariable.UserDefined, null, Const(index), Const(0), x);
this.Store(StorageKind.Output, IoVariable.UserDefined, null, Const(index), Const(1), y);
this.Store(StorageKind.Output, IoVariable.UserDefined, null, Const(index), Const(2), z);
this.Store(StorageKind.Output, IoVariable.UserDefined, null, Const(index), Const(3), w);
}
if (TranslatorContext.Definitions.GpPassthrough && !TranslatorContext.GpuAccessor.QueryHostSupportsGeometryShaderPassthrough())
{
int inputStart, inputEnd, inputStep;
InputTopology topology = TranslatorContext.Definitions.InputTopology;
if (topology == InputTopology.LinesAdjacency)
{
inputStart = 1;
inputEnd = 3;
inputStep = 1;
}
else if (topology == InputTopology.TrianglesAdjacency)
{
inputStart = 0;
inputEnd = 6;
inputStep = 2;
}
else
{
inputStart = 0;
inputEnd = topology.InputVertexCountNoAdjacency;
inputStep = 1;
}
for (int primIndex = inputStart; primIndex < inputEnd; primIndex += inputStep)
{
WritePositionOutput(primIndex);
int passthroughAttributes = TranslatorContext.AttributeUsage.PassthroughAttributes;
while (passthroughAttributes != 0)
{
int index = BitOperations.TrailingZeroCount(passthroughAttributes);
WriteUserDefinedOutput(index, primIndex);
passthroughAttributes &= ~(1 << index);
}
this.EmitVertex();
}
this.EndPrimitive();
}
}
else if (TranslatorContext.Definitions.Stage == ShaderStage.Fragment)
{
GenerateAlphaToCoverageDitherDiscard();
bool supportsBgra = TranslatorContext.GpuAccessor.QueryHostSupportsBgraFormat();
if (TranslatorContext.Definitions.OmapDepth)
{
Operand src = Register(TranslatorContext.GetDepthRegister(), RegisterType.Gpr);
this.Store(StorageKind.Output, IoVariable.FragmentOutputDepth, null, src);
}
AlphaTestOp alphaTestOp = TranslatorContext.Definitions.AlphaTestCompare;
if (alphaTestOp != AlphaTestOp.Always)
{
if (alphaTestOp == AlphaTestOp.Never)
{
this.Discard();
}
else if ((TranslatorContext.Definitions.OmapTargets & 8) != 0)
{
Instruction comparator = alphaTestOp switch
{
AlphaTestOp.Equal => Instruction.CompareEqual,
AlphaTestOp.Greater => Instruction.CompareGreater,
AlphaTestOp.GreaterOrEqual => Instruction.CompareGreaterOrEqual,
AlphaTestOp.Less => Instruction.CompareLess,
AlphaTestOp.LessOrEqual => Instruction.CompareLessOrEqual,
AlphaTestOp.NotEqual => Instruction.CompareNotEqual,
_ => 0,
};
Debug.Assert(comparator != 0, $"Invalid alpha test operation \"{alphaTestOp}\".");
Operand alpha = Register(3, RegisterType.Gpr);
Operand alphaRef = ConstF(TranslatorContext.Definitions.AlphaTestReference);
Operand alphaPass = Add(Instruction.FP32 | comparator, Local(), alpha, alphaRef);
Operand alphaPassLabel = Label();
this.BranchIfTrue(alphaPassLabel, alphaPass);
this.Discard();
this.MarkLabel(alphaPassLabel);
}
}
// We don't need to output anything if alpha test always fails.
if (alphaTestOp == AlphaTestOp.Never)
{
return false;
}
int regIndexBase = 0;
for (int rtIndex = 0; rtIndex < 8; rtIndex++)
{
for (int component = 0; component < 4; component++)
{
bool componentEnabled = (TranslatorContext.Definitions.OmapTargets & (1 << (rtIndex * 4 + component))) != 0;
if (!componentEnabled)
{
continue;
}
Operand src = Register(regIndexBase + component, RegisterType.Gpr);
// Perform B <-> R swap if needed, for BGRA formats (not supported on OpenGL).
if (!supportsBgra && (component == 0 || component == 2))
{
Operand isBgra = this.Load(StorageKind.ConstantBuffer, SupportBuffer.Binding, Const((int)SupportBufferField.FragmentIsBgra), Const(rtIndex));
Operand lblIsBgra = Label();
Operand lblEnd = Label();
this.BranchIfTrue(lblIsBgra, isBgra);
this.Store(StorageKind.Output, IoVariable.FragmentOutputColor, null, Const(rtIndex), Const(component), src);
this.Branch(lblEnd);
MarkLabel(lblIsBgra);
this.Store(StorageKind.Output, IoVariable.FragmentOutputColor, null, Const(rtIndex), Const(2 - component), src);
MarkLabel(lblEnd);
}
else
{
this.Store(StorageKind.Output, IoVariable.FragmentOutputColor, null, Const(rtIndex), Const(component), src);
}
}
bool targetEnabled = (TranslatorContext.Definitions.OmapTargets & (0xf << (rtIndex * 4))) != 0;
if (targetEnabled)
{
regIndexBase += 4;
}
}
}
if (VertexAsCompute)
{
if (TranslatorContext.Stage == ShaderStage.Vertex)
{
int vertexInfoCbBinding = ResourceManager.Reservations.VertexInfoConstantBufferBinding;
int vertexOutputSbBinding = ResourceManager.Reservations.VertexOutputStorageBufferBinding;
int stride = ResourceManager.Reservations.OutputSizePerInvocation;
Operand vertexCount = this.Load(StorageKind.ConstantBuffer, vertexInfoCbBinding, Const((int)VertexInfoBufferField.VertexCounts), Const(0));
Operand outputVertexOffset = this.Load(StorageKind.Input, IoVariable.GlobalId, Const(0));
Operand outputInstanceOffset = this.Load(StorageKind.Input, IoVariable.GlobalId, Const(1));
Operand outputBaseVertex = this.IMultiply(outputInstanceOffset, vertexCount);
Operand baseOffset = this.IMultiply(this.IAdd(outputBaseVertex, outputVertexOffset), Const(stride));
for (int offset = 0; offset < stride; offset++)
{
Operand vertexOffset = this.IAdd(baseOffset, Const(offset));
Operand value = this.Load(StorageKind.LocalMemory, ResourceManager.LocalVertexDataMemoryId, Const(offset));
this.Store(StorageKind.StorageBuffer, vertexOutputSbBinding, Const(0), vertexOffset, value);
}
}
else if (TranslatorContext.Stage == ShaderStage.Geometry)
{
Operand lblLoopHead = Label();
Operand lblExit = Label();
this.MarkLabel(lblLoopHead);
Operand writtenIndices = this.Load(StorageKind.LocalMemory, ResourceManager.LocalGeometryOutputIndexCountMemoryId);
int maxIndicesPerPrimitiveInvocation = TranslatorContext.Definitions.GetGeometryOutputIndexBufferStridePerInstance();
int maxIndicesPerPrimitive = maxIndicesPerPrimitiveInvocation * TranslatorContext.Definitions.ThreadsPerInputPrimitive;
this.BranchIfTrue(lblExit, this.ICompareGreaterOrEqualUnsigned(writtenIndices, Const(maxIndicesPerPrimitiveInvocation)));
int vertexInfoCbBinding = ResourceManager.Reservations.VertexInfoConstantBufferBinding;
Operand primitiveIndex = this.Load(StorageKind.Input, IoVariable.GlobalId, Const(0));
Operand instanceIndex = this.Load(StorageKind.Input, IoVariable.GlobalId, Const(1));
Operand invocationId = this.Load(StorageKind.Input, IoVariable.GlobalId, Const(2));
Operand vertexCount = this.Load(StorageKind.ConstantBuffer, vertexInfoCbBinding, Const((int)VertexInfoBufferField.VertexCounts), Const(0));
Operand primitiveId = this.IAdd(this.IMultiply(instanceIndex, vertexCount), primitiveIndex);
Operand ibOffset = this.IMultiply(primitiveId, Const(maxIndicesPerPrimitive));
ibOffset = this.IAdd(ibOffset, this.IMultiply(invocationId, Const(maxIndicesPerPrimitiveInvocation)));
ibOffset = this.IAdd(ibOffset, writtenIndices);
this.Store(StorageKind.StorageBuffer, ResourceManager.Reservations.GeometryIndexOutputStorageBufferBinding, Const(0), ibOffset, Const(-1));
this.Store(StorageKind.LocalMemory, ResourceManager.LocalGeometryOutputIndexCountMemoryId, this.IAdd(writtenIndices, Const(1)));
this.Branch(lblLoopHead);
this.MarkLabel(lblExit);
}
}
return true;
}
private void GenerateAlphaToCoverageDitherDiscard()
{
// If the feature is disabled, or alpha is not written, then we're done.
if (!TranslatorContext.Definitions.AlphaToCoverageDitherEnable || (TranslatorContext.Definitions.OmapTargets & 8) == 0)
{
return;
}
// 11 11 11 10 10 10 10 00
// 11 01 01 01 01 00 00 00
Operand ditherMask = Const(unchecked((int)0xfbb99110u));
Operand fragCoordX = this.Load(StorageKind.Input, IoVariable.FragmentCoord, null, Const(0));
Operand fragCoordY = this.Load(StorageKind.Input, IoVariable.FragmentCoord, null, Const(1));
Operand x = this.BitwiseAnd(this.FP32ConvertToU32(fragCoordX), Const(1));
Operand y = this.BitwiseAnd(this.FP32ConvertToU32(fragCoordY), Const(1));
Operand xy = this.BitwiseOr(x, this.ShiftLeft(y, Const(1)));
Operand alpha = Register(3, RegisterType.Gpr);
Operand scaledAlpha = this.FPMultiply(this.FPSaturate(alpha), ConstF(8));
Operand quantizedAlpha = this.IMinimumU32(this.FP32ConvertToU32(scaledAlpha), Const(7));
Operand shift = this.BitwiseOr(this.ShiftLeft(quantizedAlpha, Const(2)), xy);
Operand opaque = this.BitwiseAnd(this.ShiftRightU32(ditherMask, shift), Const(1));
Operand a2cDitherEndLabel = Label();
this.BranchIfTrue(a2cDitherEndLabel, opaque);
this.Discard();
this.MarkLabel(a2cDitherEndLabel);
}
public Operation[] GetOperations()
{
return _operations.ToArray();
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,30 @@
using System;
namespace Ryujinx.Graphics.Shader.Translation
{
/// <summary>
/// Features used by the shader that are important for the code generator to know in advance.
/// These typically change the declarations in the shader header.
/// </summary>
[Flags]
public enum FeatureFlags
{
None = 0,
// Affected by resolution scaling.
FragCoordXY = 1 << 1,
Bindless = 1 << 2,
InstanceId = 1 << 3,
DrawParameters = 1 << 4,
RtLayer = 1 << 5,
Shuffle = 1 << 6,
ViewportIndex = 1 << 7,
ViewportMask = 1 << 8,
FixedFuncAttr = 1 << 9,
LocalMemory = 1 << 10,
SharedMemory = 1 << 11,
Store = 1 << 12,
VtgAsCompute = 1 << 13,
}
}

View File

@@ -0,0 +1,865 @@
using Ryujinx.Graphics.Shader.Decoders;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace Ryujinx.Graphics.Shader.Translation
{
static class FunctionMatch
{
private static readonly IPatternTreeNode[] _fsiGetAddressTree = PatternTrees.GetFsiGetAddress();
private static readonly IPatternTreeNode[] _fsiGetAddressV2Tree = PatternTrees.GetFsiGetAddressV2();
private static readonly IPatternTreeNode[] _fsiIsLastWarpThreadPatternTree = PatternTrees.GetFsiIsLastWarpThread();
private static readonly IPatternTreeNode[] _fsiBeginPatternTree = PatternTrees.GetFsiBeginPattern();
private static readonly IPatternTreeNode[] _fsiEndPatternTree = PatternTrees.GetFsiEndPattern();
public static void RunPass(DecodedProgram program)
{
byte[] externalRegs = new byte[4];
bool hasGetAddress = false;
foreach (DecodedFunction function in program)
{
if (function == program.MainFunction)
{
continue;
}
int externalReg4 = 0;
TreeNode[] functionTree = BuildTree(function.Blocks);
if (Matches(_fsiGetAddressTree, functionTree))
{
externalRegs[1] = functionTree[0].GetRd();
externalRegs[2] = functionTree[2].GetRd();
externalRegs[3] = functionTree[1].GetRd();
externalReg4 = functionTree[3].GetRd();
}
else if (Matches(_fsiGetAddressV2Tree, functionTree))
{
externalRegs[1] = functionTree[2].GetRd();
externalRegs[2] = functionTree[1].GetRd();
externalRegs[3] = functionTree[0].GetRd();
externalReg4 = functionTree[3].GetRd();
}
// Ensure the register allocation is valid.
// If so, then we have a match.
if (externalRegs[1] != externalRegs[2] &&
externalRegs[2] != externalRegs[3] &&
externalRegs[1] != externalRegs[3] &&
externalRegs[1] + 1 != externalRegs[2] &&
externalRegs[1] + 1 != externalRegs[3] &&
externalRegs[1] + 1 == externalReg4 &&
externalRegs[2] != RegisterConsts.RegisterZeroIndex &&
externalRegs[3] != RegisterConsts.RegisterZeroIndex &&
externalReg4 != RegisterConsts.RegisterZeroIndex)
{
hasGetAddress = true;
function.Type = FunctionType.Unused;
break;
}
}
foreach (DecodedFunction function in program)
{
if (function.IsCompilerGenerated || function == program.MainFunction)
{
continue;
}
if (hasGetAddress)
{
TreeNode[] functionTree = BuildTree(function.Blocks);
if (MatchesFsi(_fsiBeginPatternTree, program, function, functionTree, externalRegs))
{
function.Type = FunctionType.BuiltInFSIBegin;
continue;
}
else if (MatchesFsi(_fsiEndPatternTree, program, function, functionTree, externalRegs))
{
function.Type = FunctionType.BuiltInFSIEnd;
continue;
}
}
}
}
private readonly struct TreeNodeUse
{
public TreeNode Node { get; }
public int Index { get; }
public bool Inverted { get; }
private TreeNodeUse(int index, bool inverted, TreeNode node)
{
Index = index;
Inverted = inverted;
Node = node;
}
public TreeNodeUse(int index, TreeNode node) : this(index, false, node)
{
}
public TreeNodeUse Flip()
{
return new TreeNodeUse(Index, !Inverted, Node);
}
}
private enum TreeNodeType : byte
{
Op,
Label,
}
private class TreeNode
{
public readonly InstOp Op;
public readonly List<TreeNodeUse> Uses;
public TreeNodeType Type { get; }
public byte Order { get; }
public TreeNode(byte order)
{
Type = TreeNodeType.Label;
Order = order;
}
public TreeNode(InstOp op, byte order)
{
Op = op;
Uses = [];
Type = TreeNodeType.Op;
Order = order;
}
public byte GetPd()
{
return (byte)((Op.RawOpCode >> 3) & 7);
}
public byte GetRd()
{
return (byte)Op.RawOpCode;
}
}
private static TreeNode[] BuildTree(Block[] blocks)
{
List<TreeNode> nodes = [];
Dictionary<ulong, TreeNode> labels = new();
TreeNodeUse[] predDefs = new TreeNodeUse[RegisterConsts.PredsCount];
TreeNodeUse[] gprDefs = new TreeNodeUse[RegisterConsts.GprsCount];
void DefPred(byte predIndex, int index, TreeNode node)
{
if (predIndex != RegisterConsts.PredicateTrueIndex)
{
predDefs[predIndex] = new TreeNodeUse(index, node);
}
}
void DefGpr(byte regIndex, int index, TreeNode node)
{
if (regIndex != RegisterConsts.RegisterZeroIndex)
{
gprDefs[regIndex] = new TreeNodeUse(index, node);
}
}
TreeNodeUse UsePred(byte predIndex, bool predInv)
{
if (predIndex != RegisterConsts.PredicateTrueIndex)
{
TreeNodeUse use = predDefs[predIndex];
if (use.Node != null)
{
nodes.Remove(use.Node);
}
else
{
use = new TreeNodeUse(-(predIndex + 2), null);
}
return predInv ? use.Flip() : use;
}
return new TreeNodeUse(-1, null);
}
TreeNodeUse UseGpr(byte regIndex)
{
if (regIndex != RegisterConsts.RegisterZeroIndex)
{
TreeNodeUse use = gprDefs[regIndex];
if (use.Node != null)
{
nodes.Remove(use.Node);
}
else
{
use = new TreeNodeUse(-(regIndex + 2), null);
}
return use;
}
return new TreeNodeUse(-1, null);
}
byte order = 0;
for (int index = 0; index < blocks.Length; index++)
{
Block block = blocks[index];
if (block.Predecessors.Count > 1)
{
TreeNode label = new(order++);
nodes.Add(label);
labels.Add(block.Address, label);
}
for (int opIndex = 0; opIndex < block.OpCodes.Count; opIndex++)
{
InstOp op = block.OpCodes[opIndex];
TreeNode node = new(op, IsOrderDependant(op.Name) ? order : (byte)0);
// Add uses.
if (!op.Props.HasFlag(InstProps.NoPred))
{
byte predIndex = (byte)((op.RawOpCode >> 16) & 7);
bool predInv = (op.RawOpCode & 0x80000) != 0;
node.Uses.Add(UsePred(predIndex, predInv));
}
if (op.Props.HasFlag(InstProps.Ps))
{
byte predIndex = (byte)((op.RawOpCode >> 39) & 7);
bool predInv = (op.RawOpCode & 0x40000000000) != 0;
node.Uses.Add(UsePred(predIndex, predInv));
}
if (op.Props.HasFlag(InstProps.Ra))
{
byte ra = (byte)(op.RawOpCode >> 8);
node.Uses.Add(UseGpr(ra));
}
if ((op.Props & (InstProps.Rb | InstProps.Rb2)) != 0)
{
byte rb = op.Props.HasFlag(InstProps.Rb2) ? (byte)op.RawOpCode : (byte)(op.RawOpCode >> 20);
node.Uses.Add(UseGpr(rb));
}
if (op.Props.HasFlag(InstProps.Rc))
{
byte rc = (byte)(op.RawOpCode >> 39);
node.Uses.Add(UseGpr(rc));
}
if (op.Name == InstName.Bra && labels.TryGetValue(op.GetAbsoluteAddress(), out TreeNode label))
{
node.Uses.Add(new TreeNodeUse(0, label));
}
// Make definitions.
int defIndex = 0;
InstProps pdType = op.Props & InstProps.PdMask;
if (pdType != 0)
{
int bit = pdType switch
{
InstProps.Pd => 3,
InstProps.LPd => 48,
InstProps.SPd => 30,
InstProps.TPd => 51,
InstProps.VPd => 45,
_ => throw new InvalidOperationException($"Table has unknown predicate destination {pdType}."),
};
byte predIndex = (byte)((op.RawOpCode >> bit) & 7);
DefPred(predIndex, defIndex++, node);
}
if (op.Props.HasFlag(InstProps.Rd))
{
byte rd = (byte)op.RawOpCode;
DefGpr(rd, defIndex++, node);
}
nodes.Add(node);
}
}
return nodes.ToArray();
}
private static bool IsOrderDependant(InstName name)
{
switch (name)
{
case InstName.Atom:
case InstName.AtomCas:
case InstName.Atoms:
case InstName.AtomsCas:
case InstName.Ld:
case InstName.Ldg:
case InstName.Ldl:
case InstName.Lds:
case InstName.Suatom:
case InstName.SuatomB:
case InstName.SuatomB2:
case InstName.SuatomCas:
case InstName.SuatomCasB:
case InstName.Suld:
case InstName.SuldB:
case InstName.SuldD:
case InstName.SuldDB:
return true;
}
return false;
}
private interface IPatternTreeNode
{
List<PatternTreeNodeUse> Uses { get; }
InstName Name { get; }
TreeNodeType Type { get; }
byte Order { get; }
bool IsImm { get; }
bool Matches(in InstOp opInfo);
}
private readonly struct PatternTreeNodeUse
{
public IPatternTreeNode Node { get; }
public int Index { get; }
public bool Inverted { get; }
public PatternTreeNodeUse Inv => new(Index, !Inverted, Node);
private PatternTreeNodeUse(int index, bool inverted, IPatternTreeNode node)
{
Index = index;
Inverted = inverted;
Node = node;
}
public PatternTreeNodeUse(int index, IPatternTreeNode node) : this(index, false, node)
{
}
}
private class PatternTreeNode<T> : IPatternTreeNode
{
public List<PatternTreeNodeUse> Uses { get; }
private readonly Func<T, bool> _match;
public InstName Name { get; }
public TreeNodeType Type { get; }
public byte Order { get; }
public bool IsImm { get; }
public PatternTreeNodeUse Out => new(0, this);
public PatternTreeNode(InstName name, Func<T, bool> match, TreeNodeType type = TreeNodeType.Op, byte order = 0, bool isImm = false)
{
Name = name;
_match = match;
Type = type;
Order = order;
IsImm = isImm;
Uses = [];
}
public PatternTreeNode<T> Use(PatternTreeNodeUse use)
{
Uses.Add(use);
return this;
}
public PatternTreeNodeUse OutAt(int index)
{
return new PatternTreeNodeUse(index, this);
}
public bool Matches(in InstOp opInfo)
{
if (opInfo.Name != Name)
{
return false;
}
ulong rawOp = opInfo.RawOpCode;
T op = Unsafe.As<ulong, T>(ref rawOp);
if (!_match(op))
{
return false;
}
return true;
}
}
private static bool MatchesFsi(
IPatternTreeNode[] pattern,
DecodedProgram program,
DecodedFunction function,
TreeNode[] functionTree,
byte[] externalRegs)
{
if (function.Blocks.Length == 0)
{
return false;
}
InstOp callOp = function.Blocks[0].GetLastOp();
if (callOp.Name != InstName.Cal)
{
return false;
}
DecodedFunction callTarget = program.GetFunctionByAddress(callOp.GetAbsoluteAddress());
TreeNode[] callTargetTree;
if (callTarget == null || !Matches(_fsiIsLastWarpThreadPatternTree, callTargetTree = BuildTree(callTarget.Blocks)))
{
return false;
}
externalRegs[0] = callTargetTree[0].GetPd();
if (Matches(pattern, functionTree, externalRegs))
{
callTarget.RemoveCaller(function);
return true;
}
return false;
}
private static bool Matches(IPatternTreeNode[] pTree, TreeNode[] cTree, byte[] externalRegs = null)
{
if (pTree.Length != cTree.Length)
{
return false;
}
for (int index = 0; index < pTree.Length; index++)
{
if (!Matches(pTree[index], cTree[index], externalRegs))
{
return false;
}
}
return true;
}
private static bool Matches(IPatternTreeNode pTreeNode, TreeNode cTreeNode, byte[] externalRegs)
{
if (!pTreeNode.Matches(in cTreeNode.Op) ||
pTreeNode.Type != cTreeNode.Type ||
pTreeNode.Order != cTreeNode.Order ||
pTreeNode.IsImm != cTreeNode.Op.Props.HasFlag(InstProps.Ib))
{
return false;
}
if (pTreeNode.Type == TreeNodeType.Op)
{
if (pTreeNode.Uses.Count != cTreeNode.Uses.Count)
{
return false;
}
for (int index = 0; index < pTreeNode.Uses.Count; index++)
{
PatternTreeNodeUse pUse = pTreeNode.Uses[index];
TreeNodeUse cUse = cTreeNode.Uses[index];
if (pUse.Index <= -2)
{
if (externalRegs[-pUse.Index - 2] != (-cUse.Index - 2))
{
return false;
}
}
else if (pUse.Index != cUse.Index)
{
return false;
}
if (pUse.Inverted != cUse.Inverted || (pUse.Node == null) != (cUse.Node == null))
{
return false;
}
if (pUse.Node != null && !Matches(pUse.Node, cUse.Node, externalRegs))
{
return false;
}
}
}
return true;
}
private static class PatternTrees
{
public static IPatternTreeNode[] GetFsiGetAddress()
{
PatternTreeNodeUse affinityValue = S2r(SReg.Affinity).Use(PT).Out;
PatternTreeNodeUse orderingTicketValue = S2r(SReg.OrderingTicket).Use(PT).Out;
return
[
Iscadd(cc: true, 2, 0, 404)
.Use(PT)
.Use(Iscadd(cc: false, 8)
.Use(PT)
.Use(Lop32i(LogicOp.And, 0xff)
.Use(PT)
.Use(affinityValue).Out)
.Use(Lop32i(LogicOp.And, 0xff)
.Use(PT)
.Use(orderingTicketValue).Out).Out),
ShrU32W(16)
.Use(PT)
.Use(orderingTicketValue),
Iadd32i(0x200)
.Use(PT)
.Use(Lop32i(LogicOp.And, 0xfe00)
.Use(PT)
.Use(orderingTicketValue).Out),
Iadd(x: true, 0, 405).Use(PT).Use(RZ),
Ret().Use(PT)
];
}
public static IPatternTreeNode[] GetFsiGetAddressV2()
{
PatternTreeNodeUse affinityValue = S2r(SReg.Affinity).Use(PT).Out;
PatternTreeNodeUse orderingTicketValue = S2r(SReg.OrderingTicket).Use(PT).Out;
return
[
ShrU32W(16)
.Use(PT)
.Use(orderingTicketValue),
Iadd32i(0x200)
.Use(PT)
.Use(Lop32i(LogicOp.And, 0xfe00)
.Use(PT)
.Use(orderingTicketValue).Out),
Iscadd(cc: true, 2, 0, 404)
.Use(PT)
.Use(Bfi(0x808)
.Use(PT)
.Use(affinityValue)
.Use(Lop32i(LogicOp.And, 0xff)
.Use(PT)
.Use(orderingTicketValue).Out).Out),
Iadd(x: true, 0, 405).Use(PT).Use(RZ),
Ret().Use(PT)
];
}
public static IPatternTreeNode[] GetFsiIsLastWarpThread()
{
PatternTreeNodeUse threadKillValue = S2r(SReg.ThreadKill).Use(PT).Out;
PatternTreeNodeUse laneIdValue = S2r(SReg.LaneId).Use(PT).Out;
return
[
IsetpU32(IComp.Eq)
.Use(PT)
.Use(PT)
.Use(FloU32()
.Use(PT)
.Use(Vote(VoteMode.Any)
.Use(PT)
.Use(IsetpU32(IComp.Ne)
.Use(PT)
.Use(PT)
.Use(Lop(negB: true, LogicOp.PassB)
.Use(PT)
.Use(RZ)
.Use(threadKillValue).OutAt(1))
.Use(RZ).Out).OutAt(1)).Out)
.Use(laneIdValue),
Ret().Use(PT)
];
}
public static IPatternTreeNode[] GetFsiBeginPattern()
{
PatternTreeNodeUse addressLowValue = CallArg(1);
static PatternTreeNodeUse HighU16Equals(PatternTreeNodeUse x)
{
PatternTreeNodeUse expectedValue = CallArg(3);
return IsetpU32(IComp.Eq)
.Use(PT)
.Use(PT)
.Use(ShrU32W(16).Use(PT).Use(x).Out)
.Use(expectedValue).Out;
}
PatternTreeNode<byte> label;
return
[
Cal(),
Ret().Use(CallArg(0).Inv),
Ret()
.Use(HighU16Equals(LdgE(CacheOpLd.Cg, LsSize.B32)
.Use(PT)
.Use(addressLowValue).Out)),
label = Label(),
Bra()
.Use(HighU16Equals(LdgE(CacheOpLd.Cg, LsSize.B32, 1)
.Use(PT)
.Use(addressLowValue).Out).Inv)
.Use(label.Out),
Ret().Use(PT)
];
}
public static IPatternTreeNode[] GetFsiEndPattern()
{
PatternTreeNodeUse voteResult = Vote(VoteMode.All).Use(PT).Use(PT).OutAt(1);
PatternTreeNodeUse popcResult = Popc().Use(PT).Use(voteResult).Out;
PatternTreeNodeUse threadKillValue = S2r(SReg.ThreadKill).Use(PT).Out;
PatternTreeNodeUse laneIdValue = S2r(SReg.LaneId).Use(PT).Out;
PatternTreeNodeUse addressLowValue = CallArg(1);
PatternTreeNodeUse incrementValue = CallArg(2);
return
[
Cal(),
Ret().Use(CallArg(0).Inv),
Membar(Decoders.Membar.Vc).Use(PT),
Ret().Use(IsetpU32(IComp.Ne)
.Use(PT)
.Use(PT)
.Use(threadKillValue)
.Use(RZ).Out),
RedE(RedOp.Add, AtomSize.U32)
.Use(IsetpU32(IComp.Eq)
.Use(PT)
.Use(PT)
.Use(FloU32()
.Use(PT)
.Use(voteResult).Out)
.Use(laneIdValue).Out)
.Use(addressLowValue)
.Use(Xmad(XmadCop.Cbcc, psl: true, hiloA: true, hiloB: true)
.Use(PT)
.Use(incrementValue)
.Use(Xmad(XmadCop.Cfull, mrg: true, hiloB: true)
.Use(PT)
.Use(incrementValue)
.Use(popcResult)
.Use(RZ).Out)
.Use(Xmad(XmadCop.Cfull)
.Use(PT)
.Use(incrementValue)
.Use(popcResult)
.Use(RZ).Out).Out),
Ret().Use(PT)
];
}
private static PatternTreeNode<InstBfiI> Bfi(int imm)
{
return new(InstName.Bfi, (op) => !op.WriteCC && op.Imm20 == imm, isImm: true);
}
private static PatternTreeNode<InstBra> Bra()
{
return new(InstName.Bra, (op) => op.Ccc == Ccc.T && !op.Ca);
}
private static PatternTreeNode<InstCal> Cal()
{
return new(InstName.Cal, (op) => !op.Ca && op.Inc);
}
private static PatternTreeNode<InstFloR> FloU32()
{
return new(InstName.Flo, (op) => !op.Signed && !op.Sh && !op.NegB && !op.WriteCC);
}
private static PatternTreeNode<InstIaddC> Iadd(bool x, int cbufSlot, int cbufOffset)
{
return new(InstName.Iadd, (op) =>
!op.Sat &&
!op.WriteCC &&
op.X == x &&
op.AvgMode == AvgMode.NoNeg &&
op.CbufSlot == cbufSlot &&
op.CbufOffset == cbufOffset);
}
private static PatternTreeNode<InstIadd32i> Iadd32i(int imm)
{
return new(InstName.Iadd32i, (op) => !op.Sat && !op.WriteCC && !op.X && op.AvgMode == AvgMode.NoNeg && op.Imm32 == imm);
}
private static PatternTreeNode<InstIscaddR> Iscadd(bool cc, int imm)
{
return new(InstName.Iscadd, (op) => op.WriteCC == cc && op.AvgMode == AvgMode.NoNeg && op.Imm5 == imm);
}
private static PatternTreeNode<InstIscaddC> Iscadd(bool cc, int imm, int cbufSlot, int cbufOffset)
{
return new(InstName.Iscadd, (op) =>
op.WriteCC == cc &&
op.AvgMode == AvgMode.NoNeg &&
op.Imm5 == imm &&
op.CbufSlot == cbufSlot &&
op.CbufOffset == cbufOffset);
}
private static PatternTreeNode<InstIsetpR> IsetpU32(IComp comp)
{
return new(InstName.Isetp, (op) => !op.Signed && op.IComp == comp && op.Bop == BoolOp.And);
}
private static PatternTreeNode<byte> Label()
{
return new(InstName.Invalid, (op) => true, type: TreeNodeType.Label);
}
private static PatternTreeNode<InstLopR> Lop(bool negB, LogicOp logicOp)
{
return new(InstName.Lop, (op) => !op.NegA && op.NegB == negB && !op.WriteCC && !op.X && op.Lop == logicOp && op.PredicateOp == PredicateOp.F);
}
private static PatternTreeNode<InstLop32i> Lop32i(LogicOp logicOp, int imm)
{
return new(InstName.Lop32i, (op) => !op.NegA && !op.NegB && !op.X && !op.WriteCC && op.LogicOp == logicOp && op.Imm32 == imm);
}
private static PatternTreeNode<InstMembar> Membar(Membar membar)
{
return new(InstName.Membar, (op) => op.Membar == membar);
}
private static PatternTreeNode<InstPopcR> Popc()
{
return new(InstName.Popc, (op) => !op.NegB);
}
private static PatternTreeNode<InstRet> Ret()
{
return new(InstName.Ret, (op) => op.Ccc == Ccc.T);
}
private static PatternTreeNode<InstS2r> S2r(SReg reg)
{
return new(InstName.S2r, (op) => op.SReg == reg);
}
private static PatternTreeNode<InstShrI> ShrU32W(int imm)
{
return new(InstName.Shr, (op) => !op.Signed && !op.Brev && op.M && op.XMode == 0 && op.Imm20 == imm, isImm: true);
}
private static PatternTreeNode<InstLdg> LdgE(CacheOpLd cacheOp, LsSize size, byte order = 0)
{
return new(InstName.Ldg, (op) => op.E && op.CacheOp == cacheOp && op.LsSize == size, order: order);
}
private static PatternTreeNode<InstRed> RedE(RedOp redOp, AtomSize size, byte order = 0)
{
return new(InstName.Red, (op) => op.E && op.RedOp == redOp && op.RedSize == size, order: order);
}
private static PatternTreeNode<InstVote> Vote(VoteMode mode)
{
return new(InstName.Vote, (op) => op.VoteMode == mode);
}
private static PatternTreeNode<InstXmadR> Xmad(XmadCop cop, bool psl = false, bool mrg = false, bool hiloA = false, bool hiloB = false)
{
return new(InstName.Xmad, (op) => op.XmadCop == cop && op.Psl == psl && op.Mrg == mrg && op.HiloA == hiloA && op.HiloB == hiloB);
}
private static PatternTreeNodeUse PT => PTOrRZ();
private static PatternTreeNodeUse RZ => PTOrRZ();
private static PatternTreeNodeUse CallArg(int index)
{
return new PatternTreeNodeUse(-(index + 2), null);
}
private static PatternTreeNodeUse PTOrRZ()
{
return new PatternTreeNodeUse(-1, null);
}
}
private static void PrintTreeNode(TreeNode node, string indentation)
{
Console.WriteLine($" {node.Op.Name}");
for (int i = 0; i < node.Uses.Count; i++)
{
TreeNodeUse use = node.Uses[i];
bool last = i == node.Uses.Count - 1;
char separator = last ? '`' : '|';
if (use.Node != null)
{
Console.Write($"{indentation} {separator}- ({(use.Inverted ? "INV " : string.Empty)}{use.Index})");
PrintTreeNode(use.Node, indentation + (last ? " " : " | "));
}
else
{
Console.WriteLine($"{indentation} {separator}- ({(use.Inverted ? "INV " : string.Empty)}{use.Index}) NULL");
}
}
}
private static void PrintTreeNode(IPatternTreeNode node, string indentation)
{
Console.WriteLine($" {node.Name}");
for (int i = 0; i < node.Uses.Count; i++)
{
PatternTreeNodeUse use = node.Uses[i];
bool last = i == node.Uses.Count - 1;
char separator = last ? '`' : '|';
if (use.Node != null)
{
Console.Write($"{indentation} {separator}- ({(use.Inverted ? "INV " : string.Empty)}{use.Index})");
PrintTreeNode(use.Node, indentation + (last ? " " : " | "));
}
else
{
Console.WriteLine($"{indentation} {separator}- ({(use.Inverted ? "INV " : string.Empty)}{use.Index}) NULL");
}
}
}
}
}

View File

@@ -0,0 +1,474 @@
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
using System;
using System.Collections.Generic;
using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
namespace Ryujinx.Graphics.Shader.Translation
{
class HelperFunctionManager
{
private readonly List<Function> _functionList;
private readonly Dictionary<int, int> _functionIds;
private readonly ShaderStage _stage;
public HelperFunctionManager(List<Function> functionList, ShaderStage stage)
{
_functionList = functionList;
_functionIds = new Dictionary<int, int>();
_stage = stage;
}
public int AddFunction(Function function)
{
int functionId = _functionList.Count;
_functionList.Add(function);
return functionId;
}
public int GetOrCreateFunctionId(HelperFunctionName functionName)
{
if (_functionIds.TryGetValue((int)functionName, out int functionId))
{
return functionId;
}
Function function = GenerateFunction(functionName);
functionId = AddFunction(function);
_functionIds.Add((int)functionName, functionId);
return functionId;
}
public int GetOrCreateFunctionId(HelperFunctionName functionName, int id)
{
int key = (int)functionName | (id << 16);
if (_functionIds.TryGetValue(key, out int functionId))
{
return functionId;
}
Function function = GenerateFunction(functionName, id);
functionId = AddFunction(function);
_functionIds.Add(key, functionId);
return functionId;
}
public int GetOrCreateShuffleFunctionId(HelperFunctionName functionName, int subgroupSize)
{
if (_functionIds.TryGetValue((int)functionName, out int functionId))
{
return functionId;
}
Function function = GenerateShuffleFunction(functionName, subgroupSize);
functionId = AddFunction(function);
_functionIds.Add((int)functionName, functionId);
return functionId;
}
private Function GenerateFunction(HelperFunctionName functionName)
{
return functionName switch
{
HelperFunctionName.ConvertDoubleToFloat => GenerateConvertDoubleToFloatFunction(),
HelperFunctionName.ConvertFloatToDouble => GenerateConvertFloatToDoubleFunction(),
HelperFunctionName.TexelFetchScale => GenerateTexelFetchScaleFunction(),
HelperFunctionName.TextureSizeUnscale => GenerateTextureSizeUnscaleFunction(),
_ => throw new ArgumentException($"Invalid function name {functionName}"),
};
}
private static Function GenerateConvertDoubleToFloatFunction()
{
EmitterContext context = new();
Operand valueLow = Argument(0);
Operand valueHigh = Argument(1);
Operand mantissaLow = context.BitwiseAnd(valueLow, Const(((1 << 22) - 1)));
Operand mantissa = context.ShiftRightU32(valueLow, Const(22));
mantissa = context.BitwiseOr(mantissa, context.ShiftLeft(context.BitwiseAnd(valueHigh, Const(0xfffff)), Const(10)));
mantissa = context.BitwiseOr(mantissa, context.ConditionalSelect(mantissaLow, Const(1), Const(0)));
Operand exp = context.BitwiseAnd(context.ShiftRightU32(valueHigh, Const(20)), Const(0x7ff));
Operand sign = context.ShiftRightS32(valueHigh, Const(31));
Operand resultSign = context.ShiftLeft(sign, Const(31));
Operand notZero = context.BitwiseOr(mantissa, exp);
Operand lblNotZero = Label();
context.BranchIfTrue(lblNotZero, notZero);
context.Return(resultSign);
context.MarkLabel(lblNotZero);
Operand notNaNOrInf = context.ICompareNotEqual(exp, Const(0x7ff));
mantissa = context.BitwiseOr(mantissa, Const(0x40000000));
exp = context.ISubtract(exp, Const(0x381));
// Note: Overflow cases are not handled here and might produce incorrect results.
Operand roundBits = context.BitwiseAnd(mantissa, Const(0x7f));
Operand roundBitsXor64 = context.BitwiseExclusiveOr(roundBits, Const(0x40));
mantissa = context.ShiftRightU32(context.IAdd(mantissa, Const(0x40)), Const(7));
mantissa = context.BitwiseAnd(mantissa, context.ConditionalSelect(roundBitsXor64, Const(~0), Const(~1)));
exp = context.ConditionalSelect(mantissa, exp, Const(0));
exp = context.ConditionalSelect(notNaNOrInf, exp, Const(0xff));
Operand result = context.IAdd(context.IAdd(mantissa, context.ShiftLeft(exp, Const(23))), resultSign);
context.Return(result);
return new Function(ControlFlowGraph.Create(context.GetOperations()).Blocks, "ConvertDoubleToFloat", true, 2, 0);
}
private static Function GenerateConvertFloatToDoubleFunction()
{
EmitterContext context = new();
Operand value = Argument(0);
Operand mantissa = context.BitwiseAnd(value, Const(0x7fffff));
Operand exp = context.BitwiseAnd(context.ShiftRightU32(value, Const(23)), Const(0xff));
Operand sign = context.ShiftRightS32(value, Const(31));
Operand notNaNOrInf = context.ICompareNotEqual(exp, Const(0xff));
Operand expNotZero = context.ICompareNotEqual(exp, Const(0));
Operand notDenorm = context.BitwiseOr(expNotZero, context.ICompareEqual(mantissa, Const(0)));
exp = context.IAdd(exp, Const(0x380));
Operand shiftDist = context.ISubtract(Const(32), context.FindMSBU32(mantissa));
Operand normExp = context.ISubtract(context.ISubtract(Const(1), shiftDist), Const(1));
Operand normMant = context.ShiftLeft(mantissa, shiftDist);
exp = context.ConditionalSelect(notNaNOrInf, exp, Const(0x7ff));
exp = context.ConditionalSelect(notDenorm, exp, normExp);
mantissa = context.ConditionalSelect(expNotZero, mantissa, normMant);
Operand resultLow = context.ShiftLeft(mantissa, Const(29));
Operand resultHigh = context.ShiftRightU32(mantissa, Const(3));
resultHigh = context.IAdd(resultHigh, context.ShiftLeft(exp, Const(20)));
resultHigh = context.IAdd(resultHigh, context.ShiftLeft(sign, Const(31)));
context.Copy(Argument(1), resultLow);
context.Copy(Argument(2), resultHigh);
context.Return();
return new Function(ControlFlowGraph.Create(context.GetOperations()).Blocks, "ConvertFloatToDouble", false, 1, 2);
}
private static Function GenerateFunction(HelperFunctionName functionName, int id)
{
return functionName switch
{
HelperFunctionName.SharedAtomicMaxS32 => GenerateSharedAtomicSigned(id, isMin: false),
HelperFunctionName.SharedAtomicMinS32 => GenerateSharedAtomicSigned(id, isMin: true),
HelperFunctionName.SharedStore8 => GenerateSharedStore8(id),
HelperFunctionName.SharedStore16 => GenerateSharedStore16(id),
_ => throw new ArgumentException($"Invalid function name {functionName}"),
};
}
private static Function GenerateSharedAtomicSigned(int id, bool isMin)
{
EmitterContext context = new();
Operand wordOffset = Argument(0);
Operand value = Argument(1);
Operand result = GenerateSharedAtomicCasLoop(context, wordOffset, id, (memValue) =>
{
return isMin
? context.IMinimumS32(memValue, value)
: context.IMaximumS32(memValue, value);
});
context.Return(result);
return new Function(ControlFlowGraph.Create(context.GetOperations()).Blocks, $"SharedAtomic{(isMin ? "Min" : "Max")}_{id}", true, 2, 0);
}
private static Function GenerateSharedStore8(int id)
{
return GenerateSharedStore(id, 8);
}
private static Function GenerateSharedStore16(int id)
{
return GenerateSharedStore(id, 16);
}
private static Function GenerateSharedStore(int id, int bitSize)
{
EmitterContext context = new();
Operand offset = Argument(0);
Operand value = Argument(1);
Operand wordOffset = context.ShiftRightU32(offset, Const(2));
Operand bitOffset = GetBitOffset(context, offset);
GenerateSharedAtomicCasLoop(context, wordOffset, id, (memValue) =>
{
return context.BitfieldInsert(memValue, value, bitOffset, Const(bitSize));
});
context.Return();
return new Function(ControlFlowGraph.Create(context.GetOperations()).Blocks, $"SharedStore{bitSize}_{id}", false, 2, 0);
}
private static Function GenerateShuffleFunction(HelperFunctionName functionName, int subgroupSize)
{
return functionName switch
{
HelperFunctionName.Shuffle => GenerateShuffle(subgroupSize),
HelperFunctionName.ShuffleDown => GenerateShuffleDown(subgroupSize),
HelperFunctionName.ShuffleUp => GenerateShuffleUp(subgroupSize),
HelperFunctionName.ShuffleXor => GenerateShuffleXor(subgroupSize),
_ => throw new ArgumentException($"Invalid function name {functionName}"),
};
}
private static Function GenerateShuffle(int subgroupSize)
{
EmitterContext context = new();
Operand value = Argument(0);
Operand index = Argument(1);
Operand mask = Argument(2);
Operand clamp = context.BitwiseAnd(mask, Const(0x1f));
Operand segMask = context.BitwiseAnd(context.ShiftRightU32(mask, Const(8)), Const(0x1f));
Operand minThreadId = context.BitwiseAnd(GenerateLoadSubgroupLaneId(context, subgroupSize), segMask);
Operand maxThreadId = context.BitwiseOr(context.BitwiseAnd(clamp, context.BitwiseNot(segMask)), minThreadId);
Operand srcThreadId = context.BitwiseOr(context.BitwiseAnd(index, context.BitwiseNot(segMask)), minThreadId);
Operand valid = context.ICompareLessOrEqualUnsigned(srcThreadId, maxThreadId);
context.Copy(Argument(3), valid);
Operand result = context.Shuffle(value, GenerateSubgroupShuffleIndex(context, srcThreadId, subgroupSize));
context.Return(context.ConditionalSelect(valid, result, value));
return new Function(ControlFlowGraph.Create(context.GetOperations()).Blocks, "Shuffle", true, 3, 1);
}
private static Function GenerateShuffleDown(int subgroupSize)
{
EmitterContext context = new();
Operand value = Argument(0);
Operand index = Argument(1);
Operand mask = Argument(2);
Operand clamp = context.BitwiseAnd(mask, Const(0x1f));
Operand segMask = context.BitwiseAnd(context.ShiftRightU32(mask, Const(8)), Const(0x1f));
Operand laneId = GenerateLoadSubgroupLaneId(context, subgroupSize);
Operand minThreadId = context.BitwiseAnd(laneId, segMask);
Operand maxThreadId = context.BitwiseOr(context.BitwiseAnd(clamp, context.BitwiseNot(segMask)), minThreadId);
Operand srcThreadId = context.IAdd(laneId, index);
Operand valid = context.ICompareLessOrEqualUnsigned(srcThreadId, maxThreadId);
context.Copy(Argument(3), valid);
Operand result = context.Shuffle(value, GenerateSubgroupShuffleIndex(context, srcThreadId, subgroupSize));
context.Return(context.ConditionalSelect(valid, result, value));
return new Function(ControlFlowGraph.Create(context.GetOperations()).Blocks, "ShuffleDown", true, 3, 1);
}
private static Function GenerateShuffleUp(int subgroupSize)
{
EmitterContext context = new();
Operand value = Argument(0);
Operand index = Argument(1);
Operand mask = Argument(2);
Operand segMask = context.BitwiseAnd(context.ShiftRightU32(mask, Const(8)), Const(0x1f));
Operand laneId = GenerateLoadSubgroupLaneId(context, subgroupSize);
Operand minThreadId = context.BitwiseAnd(laneId, segMask);
Operand srcThreadId = context.ISubtract(laneId, index);
Operand valid = context.ICompareGreaterOrEqual(srcThreadId, minThreadId);
context.Copy(Argument(3), valid);
Operand result = context.Shuffle(value, GenerateSubgroupShuffleIndex(context, srcThreadId, subgroupSize));
context.Return(context.ConditionalSelect(valid, result, value));
return new Function(ControlFlowGraph.Create(context.GetOperations()).Blocks, "ShuffleUp", true, 3, 1);
}
private static Function GenerateShuffleXor(int subgroupSize)
{
EmitterContext context = new();
Operand value = Argument(0);
Operand index = Argument(1);
Operand mask = Argument(2);
Operand clamp = context.BitwiseAnd(mask, Const(0x1f));
Operand segMask = context.BitwiseAnd(context.ShiftRightU32(mask, Const(8)), Const(0x1f));
Operand laneId = GenerateLoadSubgroupLaneId(context, subgroupSize);
Operand minThreadId = context.BitwiseAnd(laneId, segMask);
Operand maxThreadId = context.BitwiseOr(context.BitwiseAnd(clamp, context.BitwiseNot(segMask)), minThreadId);
Operand srcThreadId = context.BitwiseExclusiveOr(laneId, index);
Operand valid = context.ICompareLessOrEqualUnsigned(srcThreadId, maxThreadId);
context.Copy(Argument(3), valid);
Operand result = context.Shuffle(value, GenerateSubgroupShuffleIndex(context, srcThreadId, subgroupSize));
context.Return(context.ConditionalSelect(valid, result, value));
return new Function(ControlFlowGraph.Create(context.GetOperations()).Blocks, "ShuffleXor", true, 3, 1);
}
private static Operand GenerateLoadSubgroupLaneId(EmitterContext context, int subgroupSize)
{
if (subgroupSize <= 32)
{
return context.Load(StorageKind.Input, IoVariable.SubgroupLaneId);
}
return context.BitwiseAnd(context.Load(StorageKind.Input, IoVariable.SubgroupLaneId), Const(0x1f));
}
private static Operand GenerateSubgroupShuffleIndex(EmitterContext context, Operand srcThreadId, int subgroupSize)
{
if (subgroupSize <= 32)
{
return srcThreadId;
}
return context.BitwiseOr(
context.BitwiseAnd(context.Load(StorageKind.Input, IoVariable.SubgroupLaneId), Const(0x60)),
srcThreadId);
}
private Function GenerateTexelFetchScaleFunction()
{
EmitterContext context = new();
Operand input = Argument(0);
Operand samplerIndex = Argument(1);
Operand index = GetScaleIndex(context, samplerIndex);
Operand scale = context.Load(StorageKind.ConstantBuffer, 0, Const((int)SupportBufferField.RenderScale), index);
Operand scaleIsOne = context.FPCompareEqual(scale, ConstF(1f));
Operand lblScaleNotOne = Label();
context.BranchIfFalse(lblScaleNotOne, scaleIsOne);
context.Return(input);
context.MarkLabel(lblScaleNotOne);
int inArgumentsCount;
if (_stage == ShaderStage.Fragment)
{
Operand scaleIsLessThanZero = context.FPCompareLess(scale, ConstF(0f));
Operand lblScaleGreaterOrEqualZero = Label();
context.BranchIfFalse(lblScaleGreaterOrEqualZero, scaleIsLessThanZero);
Operand negScale = context.FPNegate(scale);
Operand inputScaled = context.FPMultiply(context.IConvertS32ToFP32(input), negScale);
Operand fragCoordX = context.Load(StorageKind.Input, IoVariable.FragmentCoord, null, Const(0));
Operand fragCoordY = context.Load(StorageKind.Input, IoVariable.FragmentCoord, null, Const(1));
Operand fragCoord = context.ConditionalSelect(Argument(2), fragCoordY, fragCoordX);
Operand inputBias = context.FPModulo(fragCoord, negScale);
Operand inputWithBias = context.FPAdd(inputScaled, inputBias);
context.Return(context.FP32ConvertToS32(inputWithBias));
context.MarkLabel(lblScaleGreaterOrEqualZero);
inArgumentsCount = 3;
}
else
{
inArgumentsCount = 2;
}
Operand inputScaled2 = context.FPMultiply(context.IConvertS32ToFP32(input), scale);
context.Return(context.FP32ConvertToS32(inputScaled2));
return new Function(ControlFlowGraph.Create(context.GetOperations()).Blocks, "TexelFetchScale", true, inArgumentsCount, 0);
}
private Function GenerateTextureSizeUnscaleFunction()
{
EmitterContext context = new();
Operand input = Argument(0);
Operand samplerIndex = Argument(1);
Operand index = GetScaleIndex(context, samplerIndex);
Operand scale = context.FPAbsolute(context.Load(StorageKind.ConstantBuffer, 0, Const((int)SupportBufferField.RenderScale), index));
Operand scaleIsOne = context.FPCompareEqual(scale, ConstF(1f));
Operand lblScaleNotOne = Label();
context.BranchIfFalse(lblScaleNotOne, scaleIsOne);
context.Return(input);
context.MarkLabel(lblScaleNotOne);
Operand inputUnscaled = context.FPDivide(context.IConvertS32ToFP32(input), scale);
context.Return(context.FP32ConvertToS32(inputUnscaled));
return new Function(ControlFlowGraph.Create(context.GetOperations()).Blocks, "TextureSizeUnscale", true, 2, 0);
}
private Operand GetScaleIndex(EmitterContext context, Operand index)
{
switch (_stage)
{
case ShaderStage.Vertex:
Operand fragScaleCount = context.Load(StorageKind.ConstantBuffer, 0, Const((int)SupportBufferField.FragmentRenderScaleCount));
return context.IAdd(Const(1), context.IAdd(index, fragScaleCount));
default:
return context.IAdd(Const(1), index);
}
}
public static Operand GetBitOffset(EmitterContext context, Operand offset)
{
return context.ShiftLeft(context.BitwiseAnd(offset, Const(3)), Const(3));
}
private static Operand GenerateSharedAtomicCasLoop(EmitterContext context, Operand wordOffset, int id, Func<Operand, Operand> opCallback)
{
Operand lblLoopHead = Label();
context.MarkLabel(lblLoopHead);
Operand oldValue = context.Load(StorageKind.SharedMemory, id, wordOffset);
Operand newValue = opCallback(oldValue);
Operand casResult = context.AtomicCompareAndSwap(StorageKind.SharedMemory, id, wordOffset, oldValue, newValue);
Operand casFail = context.ICompareNotEqual(casResult, oldValue);
context.BranchIfTrue(lblLoopHead, casFail);
return oldValue;
}
}
}

View File

@@ -0,0 +1,20 @@
namespace Ryujinx.Graphics.Shader.Translation
{
enum HelperFunctionName
{
Invalid,
ConvertDoubleToFloat,
ConvertFloatToDouble,
SharedAtomicMaxS32,
SharedAtomicMinS32,
SharedStore8,
SharedStore16,
Shuffle,
ShuffleDown,
ShuffleUp,
ShuffleXor,
TexelFetchScale,
TextureSizeUnscale,
}
}

View File

@@ -0,0 +1,40 @@
namespace Ryujinx.Graphics.Shader.Translation
{
class HostCapabilities
{
public readonly bool ReducedPrecision;
public readonly bool SupportsFragmentShaderInterlock;
public readonly bool SupportsFragmentShaderOrderingIntel;
public readonly bool SupportsGeometryShaderPassthrough;
public readonly bool SupportsShaderBallot;
public readonly bool SupportsShaderBarrierDivergence;
public readonly bool SupportsShaderFloat64;
public readonly bool SupportsShaderNonUniformIndexing;
public readonly bool SupportsTextureShadowLod;
public readonly bool SupportsViewportMask;
public HostCapabilities(
bool reducedPrecision,
bool supportsFragmentShaderInterlock,
bool supportsFragmentShaderOrderingIntel,
bool supportsGeometryShaderPassthrough,
bool supportsShaderBallot,
bool supportsShaderBarrierDivergence,
bool supportsShaderFloat64,
bool supportsShaderNonUniformIndexing,
bool supportsTextureShadowLod,
bool supportsViewportMask)
{
ReducedPrecision = reducedPrecision;
SupportsFragmentShaderInterlock = supportsFragmentShaderInterlock;
SupportsFragmentShaderOrderingIntel = supportsFragmentShaderOrderingIntel;
SupportsGeometryShaderPassthrough = supportsGeometryShaderPassthrough;
SupportsShaderBallot = supportsShaderBallot;
SupportsShaderBarrierDivergence = supportsShaderBarrierDivergence;
SupportsShaderFloat64 = supportsShaderFloat64;
SupportsShaderNonUniformIndexing = supportsShaderNonUniformIndexing;
SupportsTextureShadowLod = supportsTextureShadowLod;
SupportsViewportMask = supportsViewportMask;
}
}
}

View File

@@ -0,0 +1,28 @@
namespace Ryujinx.Graphics.Shader.Translation
{
readonly struct IoUsage
{
private readonly FeatureFlags _usedFeatures;
public readonly bool UsesRtLayer => _usedFeatures.HasFlag(FeatureFlags.RtLayer);
public readonly bool UsesViewportIndex => _usedFeatures.HasFlag(FeatureFlags.ViewportIndex);
public readonly bool UsesViewportMask => _usedFeatures.HasFlag(FeatureFlags.ViewportMask);
public readonly byte ClipDistancesWritten { get; }
public readonly int UserDefinedMap { get; }
public IoUsage(FeatureFlags usedFeatures, byte clipDistancesWritten, int userDefinedMap)
{
_usedFeatures = usedFeatures;
ClipDistancesWritten = clipDistancesWritten;
UserDefinedMap = userDefinedMap;
}
public readonly IoUsage Combine(IoUsage other)
{
return new IoUsage(
_usedFeatures | other._usedFeatures,
(byte)(ClipDistancesWritten | other.ClipDistancesWritten),
UserDefinedMap | other.UserDefinedMap);
}
}
}

View File

@@ -0,0 +1,494 @@
using Ryujinx.Graphics.Shader.Instructions;
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
using Ryujinx.Graphics.Shader.StructuredIr;
using System;
using System.Collections.Generic;
namespace Ryujinx.Graphics.Shader.Translation.Optimizations
{
class BindlessElimination
{
public static void RunPass(BasicBlock block, ResourceManager resourceManager, IGpuAccessor gpuAccessor)
{
// We can turn a bindless into regular access by recognizing the pattern
// produced by the compiler for separate texture and sampler.
// We check for the following conditions:
// - The handle is a constant buffer value.
// - The handle is the result of a bitwise OR logical operation.
// - Both sources of the OR operation comes from a constant buffer.
LinkedListNode<INode> nextNode;
for (LinkedListNode<INode> node = block.Operations.First; node != null; node = nextNode)
{
nextNode = node.Next;
if (node.Value is not TextureOperation texOp)
{
continue;
}
if ((texOp.Flags & TextureFlags.Bindless) == 0)
{
continue;
}
if (!TryConvertBindless(block, resourceManager, gpuAccessor, texOp) &&
!GenerateBindlessAccess(block, resourceManager, gpuAccessor, texOp, node))
{
// If we can't do bindless elimination, remove the texture operation.
// Set any destination variables to zero.
string typeName = texOp.Inst.IsImage()
? texOp.Type.GetGlslImageTypeName(texOp.Format.GetComponentType())
: texOp.Type.GlslTextureTypeName;
gpuAccessor.Log($"Failed to find handle source for bindless access of type \"{typeName}\".");
for (int destIndex = 0; destIndex < texOp.DestsCount; destIndex++)
{
block.Operations.AddBefore(node, new Operation(Instruction.Copy, texOp.GetDest(destIndex), OperandHelper.Const(0)));
}
Utils.DeleteNode(node, texOp);
}
}
}
private static bool GenerateBindlessAccess(
BasicBlock block,
ResourceManager resourceManager,
IGpuAccessor gpuAccessor,
TextureOperation texOp,
LinkedListNode<INode> node)
{
if (!gpuAccessor.QueryHostSupportsSeparateSampler())
{
// We depend on combining samplers and textures in the shader being supported for this.
return false;
}
Operand bindlessHandle = texOp.GetSource(0);
if (bindlessHandle.AsgOp is PhiNode phi)
{
for (int srcIndex = 0; srcIndex < phi.SourcesCount; srcIndex++)
{
Operand phiSource = phi.GetSource(srcIndex);
if (phiSource.AsgOp is not PhiNode && !IsBindlessAccessAllowed(phiSource))
{
return false;
}
}
}
else if (!IsBindlessAccessAllowed(bindlessHandle))
{
return false;
}
Operand textureHandle = OperandHelper.Local();
Operand samplerHandle = OperandHelper.Local();
Operand textureIndex = OperandHelper.Local();
block.Operations.AddBefore(node, new Operation(Instruction.BitwiseAnd, textureHandle, bindlessHandle, OperandHelper.Const(0xfffff)));
block.Operations.AddBefore(node, new Operation(Instruction.ShiftRightU32, samplerHandle, bindlessHandle, OperandHelper.Const(20)));
int texturePoolLength = Math.Max(BindlessToArray.MinimumArrayLength, gpuAccessor.QueryTextureArrayLengthFromPool());
block.Operations.AddBefore(node, new Operation(Instruction.MinimumU32, textureIndex, textureHandle, OperandHelper.Const(texturePoolLength - 1)));
texOp.SetSource(0, textureIndex);
bool hasSampler = !texOp.Inst.IsImage();
SetBindingPair textureSetAndBinding = resourceManager.GetTextureOrImageBinding(
texOp.Inst,
texOp.Type,
texOp.Format,
texOp.Flags & ~TextureFlags.Bindless,
0,
TextureHandle.PackOffsets(0, 0, TextureHandleType.Direct),
texturePoolLength,
hasSampler);
if (hasSampler)
{
Operand samplerIndex = OperandHelper.Local();
int samplerPoolLength = Math.Max(BindlessToArray.MinimumArrayLength, gpuAccessor.QuerySamplerArrayLengthFromPool());
block.Operations.AddBefore(node, new Operation(Instruction.MinimumU32, samplerIndex, samplerHandle, OperandHelper.Const(samplerPoolLength - 1)));
texOp.InsertSource(1, samplerIndex);
SetBindingPair samplerSetAndBinding = resourceManager.GetTextureOrImageBinding(
texOp.Inst,
SamplerType.None,
texOp.Format,
TextureFlags.None,
0,
TextureHandle.PackOffsets(0, 0, TextureHandleType.Direct),
samplerPoolLength);
texOp.TurnIntoArray(textureSetAndBinding, samplerSetAndBinding);
}
else
{
texOp.TurnIntoArray(textureSetAndBinding);
}
return true;
}
private static bool IsBindlessAccessAllowed(Operand bindlessHandle)
{
if (bindlessHandle.Type == OperandType.ConstantBuffer)
{
// Bindless access with handles from constant buffer is allowed.
return true;
}
if (bindlessHandle.AsgOp is not Operation handleOp ||
handleOp.Inst != Instruction.Load ||
(handleOp.StorageKind != StorageKind.Input && handleOp.StorageKind != StorageKind.StorageBuffer))
{
// Right now, we only allow bindless access when the handle comes from a shader input or storage buffer.
// This is an artificial limitation to prevent it from being used in cases where it
// would have a large performance impact of loading all textures in the pool.
// It might be removed in the future, if we can mitigate the performance impact.
return false;
}
return true;
}
private static bool TryConvertBindless(BasicBlock block, ResourceManager resourceManager, IGpuAccessor gpuAccessor, TextureOperation texOp)
{
if (texOp.Inst == Instruction.TextureSample || texOp.Inst.IsTextureQuery())
{
Operand bindlessHandle = texOp.GetSource(0);
// In some cases the compiler uses a shuffle operation to get the handle,
// for some textureGrad implementations. In those cases, we can skip the shuffle.
if (bindlessHandle.AsgOp is Operation shuffleOp && shuffleOp.Inst == Instruction.Shuffle)
{
bindlessHandle = shuffleOp.GetSource(0);
}
bindlessHandle = Utils.FindLastOperation(bindlessHandle, block);
// Some instructions do not encode an accurate sampler type:
// - Most instructions uses the same type for 1D and Buffer.
// - Query instructions may not have any type.
// For those cases, we need to try getting the type from current GPU state,
// as long bindless elimination is successful and we know where the texture descriptor is located.
bool rewriteSamplerType =
texOp.Type == SamplerType.TextureBuffer ||
texOp.Inst == Instruction.TextureQuerySamples ||
texOp.Inst == Instruction.TextureQuerySize;
if (bindlessHandle.Type == OperandType.ConstantBuffer)
{
SetHandle(
resourceManager,
gpuAccessor,
texOp,
bindlessHandle.GetCbufOffset(),
bindlessHandle.GetCbufSlot(),
rewriteSamplerType,
isImage: false);
return true;
}
if (!TryGetOperation(bindlessHandle.AsgOp, out Operation handleCombineOp))
{
return false;
}
if (handleCombineOp.Inst != Instruction.BitwiseOr)
{
return false;
}
Operand src0 = Utils.FindLastOperation(handleCombineOp.GetSource(0), block);
Operand src1 = Utils.FindLastOperation(handleCombineOp.GetSource(1), block);
// For cases where we have a constant, ensure that the constant is always
// the second operand.
// Since this is a commutative operation, both are fine,
// and having a "canonical" representation simplifies some checks below.
if (src0.Type == OperandType.Constant && src1.Type != OperandType.Constant)
{
(src0, src1) = (src1, src0);
}
TextureHandleType handleType = TextureHandleType.SeparateSamplerHandle;
// Try to match the following patterns:
// Masked pattern:
// - samplerHandle = samplerHandle & 0xFFF00000;
// - textureHandle = textureHandle & 0xFFFFF;
// - combinedHandle = samplerHandle | textureHandle;
// Where samplerHandle and textureHandle comes from a constant buffer.
// Shifted pattern:
// - samplerHandle = samplerId << 20;
// - combinedHandle = samplerHandle | textureHandle;
// Where samplerId and textureHandle comes from a constant buffer.
// Constant pattern:
// - combinedHandle = samplerHandleConstant | textureHandle;
// Where samplerHandleConstant is a constant value, and textureHandle comes from a constant buffer.
if (src0.AsgOp is Operation src0AsgOp)
{
if (src1.AsgOp is Operation src1AsgOp &&
src0AsgOp.Inst == Instruction.BitwiseAnd &&
src1AsgOp.Inst == Instruction.BitwiseAnd)
{
src0 = GetSourceForMaskedHandle(src0AsgOp, 0xFFFFF);
src1 = GetSourceForMaskedHandle(src1AsgOp, 0xFFF00000);
// The OR operation is commutative, so we can also try to swap the operands to get a match.
if (src0 == null || src1 == null)
{
src0 = GetSourceForMaskedHandle(src1AsgOp, 0xFFFFF);
src1 = GetSourceForMaskedHandle(src0AsgOp, 0xFFF00000);
}
if (src0 == null || src1 == null)
{
return false;
}
}
else if (src0AsgOp.Inst == Instruction.ShiftLeft)
{
Operand shift = src0AsgOp.GetSource(1);
if (shift.Type == OperandType.Constant && shift.Value == 20)
{
src0 = src1;
src1 = src0AsgOp.GetSource(0);
handleType = TextureHandleType.SeparateSamplerId;
}
}
}
else if (src1.AsgOp is Operation src1AsgOp && src1AsgOp.Inst == Instruction.ShiftLeft)
{
Operand shift = src1AsgOp.GetSource(1);
if (shift.Type == OperandType.Constant && shift.Value == 20)
{
src1 = src1AsgOp.GetSource(0);
handleType = TextureHandleType.SeparateSamplerId;
}
}
else if (src1.Type == OperandType.Constant && (src1.Value & 0xfffff) == 0)
{
handleType = TextureHandleType.SeparateConstantSamplerHandle;
}
if (src0.Type != OperandType.ConstantBuffer)
{
return false;
}
if (handleType == TextureHandleType.SeparateConstantSamplerHandle)
{
SetHandle(
resourceManager,
gpuAccessor,
texOp,
TextureHandle.PackOffsets(src0.GetCbufOffset(), (src1.Value >> 20) & 0xfff, handleType),
TextureHandle.PackSlots(src0.GetCbufSlot(), 0),
rewriteSamplerType,
isImage: false);
return true;
}
else if (src1.Type == OperandType.ConstantBuffer)
{
SetHandle(
resourceManager,
gpuAccessor,
texOp,
TextureHandle.PackOffsets(src0.GetCbufOffset(), src1.GetCbufOffset(), handleType),
TextureHandle.PackSlots(src0.GetCbufSlot(), src1.GetCbufSlot()),
rewriteSamplerType,
isImage: false);
return true;
}
}
else if (texOp.Inst.IsImage())
{
Operand src0 = Utils.FindLastOperation(texOp.GetSource(0), block);
if (src0.Type == OperandType.ConstantBuffer)
{
int cbufOffset = src0.GetCbufOffset();
int cbufSlot = src0.GetCbufSlot();
if (texOp.Format == TextureFormat.Unknown)
{
if (texOp.Inst == Instruction.ImageAtomic)
{
texOp.Format = ShaderProperties.GetTextureFormatAtomic(gpuAccessor, cbufOffset, cbufSlot);
}
else
{
texOp.Format = ShaderProperties.GetTextureFormat(gpuAccessor, cbufOffset, cbufSlot);
}
}
bool rewriteSamplerType = texOp.Type == SamplerType.TextureBuffer;
SetHandle(resourceManager, gpuAccessor, texOp, cbufOffset, cbufSlot, rewriteSamplerType, isImage: true);
return true;
}
}
return false;
}
private static bool TryGetOperation(INode asgOp, out Operation outOperation)
{
if (asgOp is PhiNode phi)
{
// If we have a phi, let's check if all inputs are effectively the same value.
// If so, we can "see through" the phi and pick any of the inputs (since they are all the same).
Operand firstSrc = phi.GetSource(0);
for (int index = 1; index < phi.SourcesCount; index++)
{
if (!IsSameOperand(firstSrc, phi.GetSource(index)))
{
outOperation = null;
return false;
}
}
asgOp = firstSrc.AsgOp;
}
if (asgOp is Operation operation)
{
outOperation = operation;
return true;
}
outOperation = null;
return false;
}
private static bool IsSameOperand(Operand x, Operand y)
{
if (x.Type == y.Type && x.Type == OperandType.LocalVariable)
{
return x.AsgOp is Operation xOp &&
y.AsgOp is Operation yOp &&
xOp.Inst == Instruction.BitwiseOr &&
yOp.Inst == Instruction.BitwiseOr &&
AreBothEqualConstantBuffers(xOp.GetSource(0), yOp.GetSource(0)) &&
AreBothEqualConstantBuffers(xOp.GetSource(1), yOp.GetSource(1));
}
return false;
}
private static bool AreBothEqualConstantBuffers(Operand x, Operand y)
{
return x.Type == y.Type && x.Value == y.Value && x.Type == OperandType.ConstantBuffer;
}
private static Operand GetSourceForMaskedHandle(Operation asgOp, uint mask)
{
// Assume it was already checked that the operation is bitwise AND.
Operand src0 = asgOp.GetSource(0);
Operand src1 = asgOp.GetSource(1);
if (src0.Type == OperandType.ConstantBuffer && src1.Type == OperandType.ConstantBuffer)
{
// We can't check if the mask matches here as both operands are from a constant buffer.
// Be optimistic and assume it matches. Avoid constant buffer 1 as official drivers
// uses this one to store compiler constants.
return src0.GetCbufSlot() == 1 ? src1 : src0;
}
else if (src0.Type == OperandType.ConstantBuffer && src1.Type == OperandType.Constant)
{
if ((uint)src1.Value == mask)
{
return src0;
}
}
else if (src0.Type == OperandType.Constant && src1.Type == OperandType.ConstantBuffer)
{
if ((uint)src0.Value == mask)
{
return src1;
}
}
return null;
}
private static void SetHandle(
ResourceManager resourceManager,
IGpuAccessor gpuAccessor,
TextureOperation texOp,
int cbufOffset,
int cbufSlot,
bool rewriteSamplerType,
bool isImage)
{
if (rewriteSamplerType)
{
SamplerType newType = gpuAccessor.QuerySamplerType(cbufOffset, cbufSlot);
if (texOp.Inst.IsTextureQuery())
{
texOp.Type = newType;
}
else if (texOp.Type == SamplerType.TextureBuffer && newType == SamplerType.Texture1D)
{
int coordsCount = 2;
if (InstEmit.Sample1DAs2D)
{
newType = SamplerType.Texture2D;
texOp.InsertSource(coordsCount++, OperandHelper.Const(0));
}
if (!isImage &&
(texOp.Flags & TextureFlags.IntCoords) != 0 &&
(texOp.Flags & TextureFlags.LodLevel) == 0)
{
// IntCoords textures must always have explicit LOD.
texOp.SetLodLevelFlag();
texOp.InsertSource(coordsCount, OperandHelper.Const(0));
}
texOp.Type = newType;
}
}
SetBindingPair setAndBinding = resourceManager.GetTextureOrImageBinding(
texOp.Inst,
texOp.Type,
texOp.Format,
texOp.Flags & ~TextureFlags.Bindless,
cbufSlot,
cbufOffset);
texOp.SetBinding(setAndBinding);
}
}
}

View File

@@ -0,0 +1,238 @@
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
using System;
using System.Collections.Generic;
using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
namespace Ryujinx.Graphics.Shader.Translation.Optimizations
{
static class BindlessToArray
{
private const int NvnTextureBufferIndex = 2;
private const int HardcodedArrayLengthOgl = 4;
// 1 and 0 elements are not considered arrays anymore.
public const int MinimumArrayLength = 2;
public static void RunPassOgl(BasicBlock block, ResourceManager resourceManager)
{
// We can turn a bindless texture access into a indexed access,
// as long the following conditions are true:
// - The handle is loaded using a LDC instruction.
// - The handle is loaded from the constant buffer with the handles (CB2 for NVN).
// - The load has a constant offset.
// The base offset of the array of handles on the constant buffer is the constant offset.
for (LinkedListNode<INode> node = block.Operations.First; node != null; node = node.Next)
{
if (node.Value is not TextureOperation texOp)
{
continue;
}
if ((texOp.Flags & TextureFlags.Bindless) == 0)
{
continue;
}
if (texOp.GetSource(0).AsgOp is not Operation handleAsgOp)
{
continue;
}
if (handleAsgOp.Inst != Instruction.Load ||
handleAsgOp.StorageKind != StorageKind.ConstantBuffer ||
handleAsgOp.SourcesCount != 4)
{
continue;
}
Operand ldcSrc0 = handleAsgOp.GetSource(0);
if (ldcSrc0.Type != OperandType.Constant ||
!resourceManager.TryGetConstantBufferSlot(ldcSrc0.Value, out int src0CbufSlot) ||
src0CbufSlot != NvnTextureBufferIndex)
{
continue;
}
Operand ldcSrc1 = handleAsgOp.GetSource(1);
// We expect field index 0 to be accessed.
if (ldcSrc1.Type != OperandType.Constant || ldcSrc1.Value != 0)
{
continue;
}
Operand ldcSrc2 = handleAsgOp.GetSource(2);
// FIXME: This is missing some checks, for example, a check to ensure that the shift value is 2.
// Might be not worth fixing since if that doesn't kick in, the result will be no texture
// to access anyway which is also wrong.
// Plus this whole transform is fundamentally flawed as-is since we have no way to know the array size.
// Eventually, this should be entirely removed in favor of a implementation that supports true bindless
// texture access.
if (ldcSrc2.AsgOp is not Operation shrOp || shrOp.Inst != Instruction.ShiftRightU32)
{
continue;
}
if (shrOp.GetSource(0).AsgOp is not Operation shrOp2 || shrOp2.Inst != Instruction.ShiftRightU32)
{
continue;
}
if (shrOp2.GetSource(0).AsgOp is not Operation addOp || addOp.Inst != Instruction.Add)
{
continue;
}
Operand addSrc1 = addOp.GetSource(1);
if (addSrc1.Type != OperandType.Constant)
{
continue;
}
TurnIntoArray(resourceManager, texOp, NvnTextureBufferIndex, addSrc1.Value / 4, HardcodedArrayLengthOgl);
Operand index = Local();
Operand source = addOp.GetSource(0);
Operation shrBy3 = new(Instruction.ShiftRightU32, index, source, Const(3));
block.Operations.AddBefore(node, shrBy3);
texOp.SetSource(0, index);
}
}
public static void RunPass(BasicBlock block, ResourceManager resourceManager, IGpuAccessor gpuAccessor)
{
// We can turn a bindless texture access into a indexed access,
// as long the following conditions are true:
// - The handle is loaded using a LDC instruction.
// - The handle is loaded from the constant buffer with the handles (CB2 for NVN).
// - The load has a constant offset.
// The base offset of the array of handles on the constant buffer is the constant offset.
for (LinkedListNode<INode> node = block.Operations.First; node != null; node = node.Next)
{
if (node.Value is not TextureOperation texOp)
{
continue;
}
if ((texOp.Flags & TextureFlags.Bindless) == 0)
{
continue;
}
Operand bindlessHandle = Utils.FindLastOperation(texOp.GetSource(0), block);
if (bindlessHandle.AsgOp is not Operation handleAsgOp)
{
continue;
}
int secondaryCbufSlot = 0;
int secondaryCbufOffset = 0;
bool hasSecondaryHandle = false;
if (handleAsgOp.Inst == Instruction.BitwiseOr)
{
Operand src0 = Utils.FindLastOperation(handleAsgOp.GetSource(0), block);
Operand src1 = Utils.FindLastOperation(handleAsgOp.GetSource(1), block);
if (src0.Type == OperandType.ConstantBuffer && src1.AsgOp is Operation)
{
handleAsgOp = src1.AsgOp as Operation;
secondaryCbufSlot = src0.GetCbufSlot();
secondaryCbufOffset = src0.GetCbufOffset();
hasSecondaryHandle = true;
}
else if (src0.AsgOp is Operation && src1.Type == OperandType.ConstantBuffer)
{
handleAsgOp = src0.AsgOp as Operation;
secondaryCbufSlot = src1.GetCbufSlot();
secondaryCbufOffset = src1.GetCbufOffset();
hasSecondaryHandle = true;
}
}
if (handleAsgOp.Inst != Instruction.Load ||
handleAsgOp.StorageKind != StorageKind.ConstantBuffer ||
handleAsgOp.SourcesCount != 4)
{
continue;
}
Operand ldcSrc0 = handleAsgOp.GetSource(0);
if (ldcSrc0.Type != OperandType.Constant ||
!resourceManager.TryGetConstantBufferSlot(ldcSrc0.Value, out int src0CbufSlot))
{
continue;
}
Operand ldcSrc1 = handleAsgOp.GetSource(1);
// We expect field index 0 to be accessed.
if (ldcSrc1.Type != OperandType.Constant || ldcSrc1.Value != 0)
{
continue;
}
Operand ldcVecIndex = handleAsgOp.GetSource(2);
Operand ldcElemIndex = handleAsgOp.GetSource(3);
if (ldcVecIndex.Type != OperandType.LocalVariable || ldcElemIndex.Type != OperandType.LocalVariable)
{
continue;
}
int cbufSlot;
int handleIndex;
if (hasSecondaryHandle)
{
cbufSlot = TextureHandle.PackSlots(src0CbufSlot, secondaryCbufSlot);
handleIndex = TextureHandle.PackOffsets(0, secondaryCbufOffset, TextureHandleType.SeparateSamplerHandle);
}
else
{
cbufSlot = src0CbufSlot;
handleIndex = 0;
}
int length = Math.Max(MinimumArrayLength, gpuAccessor.QueryTextureArrayLengthFromBuffer(src0CbufSlot));
TurnIntoArray(resourceManager, texOp, cbufSlot, handleIndex, length);
Operand vecIndex = Local();
Operand elemIndex = Local();
Operand index = Local();
Operand indexMin = Local();
block.Operations.AddBefore(node, new Operation(Instruction.ShiftLeft, vecIndex, ldcVecIndex, Const(1)));
block.Operations.AddBefore(node, new Operation(Instruction.ShiftRightU32, elemIndex, ldcElemIndex, Const(1)));
block.Operations.AddBefore(node, new Operation(Instruction.Add, index, vecIndex, elemIndex));
block.Operations.AddBefore(node, new Operation(Instruction.MinimumU32, indexMin, index, Const(length - 1)));
texOp.SetSource(0, indexMin);
}
}
private static void TurnIntoArray(ResourceManager resourceManager, TextureOperation texOp, int cbufSlot, int handleIndex, int length)
{
SetBindingPair setAndBinding = resourceManager.GetTextureOrImageBinding(
texOp.Inst,
texOp.Type,
texOp.Format,
texOp.Flags & ~TextureFlags.Bindless,
cbufSlot,
handleIndex,
length);
texOp.TurnIntoArray(setAndBinding);
}
}
}

View File

@@ -0,0 +1,60 @@
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
using System;
namespace Ryujinx.Graphics.Shader.Translation.Optimizations
{
static class BranchElimination
{
public static bool RunPass(BasicBlock block)
{
if (block.HasBranch && IsRedundantBranch((Operation)block.GetLastOp(), Next(block)))
{
block.Branch = null;
return true;
}
return false;
}
private static bool IsRedundantBranch(Operation current, BasicBlock nextBlock)
{
// Here we check that:
// - The current block ends with a branch.
// - The next block only contains a branch.
// - The branch on the next block is unconditional.
// - Both branches are jumping to the same location.
// In this case, the branch on the current block can be removed,
// as the next block is going to jump to the same place anyway.
if (nextBlock?.Operations.First?.Value is not Operation next)
{
return false;
}
if (next.Inst != Instruction.Branch)
{
return false;
}
return current.Dest == next.Dest;
}
private static BasicBlock Next(BasicBlock block)
{
block = block.Next;
while (block != null && block.Operations.Count == 0)
{
if (block.HasBranch)
{
throw new InvalidOperationException("Found a bogus empty block that \"ends with a branch\".");
}
block = block.Next;
}
return block;
}
}
}

View File

@@ -0,0 +1,352 @@
using Ryujinx.Common.Utilities;
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
using System;
using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
namespace Ryujinx.Graphics.Shader.Translation.Optimizations
{
static class ConstantFolding
{
public static void RunPass(ResourceManager resourceManager, Operation operation)
{
if (!AreAllSourcesConstant(operation))
{
return;
}
switch (operation.Inst)
{
case Instruction.Add:
EvaluateBinary(operation, (x, y) => x + y);
break;
case Instruction.BitCount:
EvaluateUnary(operation, (x) => BitCount(x));
break;
case Instruction.BitwiseAnd:
EvaluateBinary(operation, (x, y) => x & y);
break;
case Instruction.BitwiseExclusiveOr:
EvaluateBinary(operation, (x, y) => x ^ y);
break;
case Instruction.BitwiseNot:
EvaluateUnary(operation, (x) => ~x);
break;
case Instruction.BitwiseOr:
EvaluateBinary(operation, (x, y) => x | y);
break;
case Instruction.BitfieldExtractS32:
BitfieldExtractS32(operation);
break;
case Instruction.BitfieldExtractU32:
BitfieldExtractU32(operation);
break;
case Instruction.Clamp:
EvaluateTernary(operation, (x, y, z) => Math.Clamp(x, y, z));
break;
case Instruction.ClampU32:
EvaluateTernary(operation, (x, y, z) => (int)Math.Clamp((uint)x, (uint)y, (uint)z));
break;
case Instruction.CompareEqual:
EvaluateBinary(operation, (x, y) => x == y);
break;
case Instruction.CompareGreater:
EvaluateBinary(operation, (x, y) => x > y);
break;
case Instruction.CompareGreaterOrEqual:
EvaluateBinary(operation, (x, y) => x >= y);
break;
case Instruction.CompareGreaterOrEqualU32:
EvaluateBinary(operation, (x, y) => (uint)x >= (uint)y);
break;
case Instruction.CompareGreaterU32:
EvaluateBinary(operation, (x, y) => (uint)x > (uint)y);
break;
case Instruction.CompareLess:
EvaluateBinary(operation, (x, y) => x < y);
break;
case Instruction.CompareLessOrEqual:
EvaluateBinary(operation, (x, y) => x <= y);
break;
case Instruction.CompareLessOrEqualU32:
EvaluateBinary(operation, (x, y) => (uint)x <= (uint)y);
break;
case Instruction.CompareLessU32:
EvaluateBinary(operation, (x, y) => (uint)x < (uint)y);
break;
case Instruction.CompareNotEqual:
EvaluateBinary(operation, (x, y) => x != y);
break;
case Instruction.Divide:
EvaluateBinary(operation, (x, y) => y != 0 ? x / y : 0);
break;
case Instruction.FP32 | Instruction.Add:
EvaluateFPBinary(operation, (x, y) => x + y);
break;
case Instruction.FP32 | Instruction.Clamp:
EvaluateFPTernary(operation, (x, y, z) => Math.Clamp(x, y, z));
break;
case Instruction.FP32 | Instruction.CompareEqual:
EvaluateFPBinary(operation, (x, y) => x == y);
break;
case Instruction.FP32 | Instruction.CompareGreater:
EvaluateFPBinary(operation, (x, y) => x > y);
break;
case Instruction.FP32 | Instruction.CompareGreaterOrEqual:
EvaluateFPBinary(operation, (x, y) => x >= y);
break;
case Instruction.FP32 | Instruction.CompareLess:
EvaluateFPBinary(operation, (x, y) => x < y);
break;
case Instruction.FP32 | Instruction.CompareLessOrEqual:
EvaluateFPBinary(operation, (x, y) => x <= y);
break;
case Instruction.FP32 | Instruction.CompareNotEqual:
EvaluateFPBinary(operation, (x, y) => x != y);
break;
case Instruction.FP32 | Instruction.Divide:
EvaluateFPBinary(operation, (x, y) => x / y);
break;
case Instruction.FP32 | Instruction.Multiply:
EvaluateFPBinary(operation, (x, y) => x * y);
break;
case Instruction.FP32 | Instruction.Negate:
EvaluateFPUnary(operation, (x) => -x);
break;
case Instruction.FP32 | Instruction.Subtract:
EvaluateFPBinary(operation, (x, y) => x - y);
break;
case Instruction.IsNan:
EvaluateFPUnary(operation, (x) => float.IsNaN(x));
break;
case Instruction.Load:
if (operation.StorageKind == StorageKind.ConstantBuffer && operation.SourcesCount == 4)
{
int binding = operation.GetSource(0).Value;
int fieldIndex = operation.GetSource(1).Value;
if (resourceManager.TryGetConstantBufferSlot(binding, out int cbufSlot) && fieldIndex == 0)
{
int vecIndex = operation.GetSource(2).Value;
int elemIndex = operation.GetSource(3).Value;
int cbufOffset = vecIndex * 4 + elemIndex;
operation.TurnIntoCopy(Cbuf(cbufSlot, cbufOffset));
}
}
break;
case Instruction.Maximum:
EvaluateBinary(operation, (x, y) => Math.Max(x, y));
break;
case Instruction.MaximumU32:
EvaluateBinary(operation, (x, y) => (int)Math.Max((uint)x, (uint)y));
break;
case Instruction.Minimum:
EvaluateBinary(operation, (x, y) => Math.Min(x, y));
break;
case Instruction.MinimumU32:
EvaluateBinary(operation, (x, y) => (int)Math.Min((uint)x, (uint)y));
break;
case Instruction.Multiply:
EvaluateBinary(operation, (x, y) => x * y);
break;
case Instruction.Negate:
EvaluateUnary(operation, (x) => -x);
break;
case Instruction.ShiftLeft:
EvaluateBinary(operation, (x, y) => x << y);
break;
case Instruction.ShiftRightS32:
EvaluateBinary(operation, (x, y) => x >> y);
break;
case Instruction.ShiftRightU32:
EvaluateBinary(operation, (x, y) => (int)((uint)x >> y));
break;
case Instruction.Subtract:
EvaluateBinary(operation, (x, y) => x - y);
break;
case Instruction.UnpackHalf2x16:
UnpackHalf2x16(operation);
break;
}
}
private static bool AreAllSourcesConstant(Operation operation)
{
for (int index = 0; index < operation.SourcesCount; index++)
{
if (operation.GetSource(index).Type != OperandType.Constant)
{
return false;
}
}
return true;
}
private static int BitCount(int value)
{
int count = 0;
for (int bit = 0; bit < 32; bit++)
{
if (value.Extract(bit))
{
count++;
}
}
return count;
}
private static void BitfieldExtractS32(Operation operation)
{
int value = GetBitfieldExtractValue(operation);
int shift = 32 - operation.GetSource(2).Value;
value = (value << shift) >> shift;
operation.TurnIntoCopy(Const(value));
}
private static void BitfieldExtractU32(Operation operation)
{
operation.TurnIntoCopy(Const(GetBitfieldExtractValue(operation)));
}
private static int GetBitfieldExtractValue(Operation operation)
{
int value = operation.GetSource(0).Value;
int lsb = operation.GetSource(1).Value;
int length = operation.GetSource(2).Value;
return value.Extract(lsb, length);
}
private static void UnpackHalf2x16(Operation operation)
{
int value = operation.GetSource(0).Value;
value = (value >> operation.Index * 16) & 0xffff;
operation.TurnIntoCopy(ConstF((float)BitConverter.UInt16BitsToHalf((ushort)value)));
}
private static void EvaluateUnary(Operation operation, Func<int, int> op)
{
int x = operation.GetSource(0).Value;
operation.TurnIntoCopy(Const(op(x)));
}
private static void EvaluateFPUnary(Operation operation, Func<float, float> op)
{
float x = operation.GetSource(0).AsFloat();
operation.TurnIntoCopy(ConstF(op(x)));
}
private static void EvaluateFPUnary(Operation operation, Func<float, bool> op)
{
float x = operation.GetSource(0).AsFloat();
operation.TurnIntoCopy(Const(op(x) ? IrConsts.True : IrConsts.False));
}
private static void EvaluateBinary(Operation operation, Func<int, int, int> op)
{
int x = operation.GetSource(0).Value;
int y = operation.GetSource(1).Value;
operation.TurnIntoCopy(Const(op(x, y)));
}
private static void EvaluateBinary(Operation operation, Func<int, int, bool> op)
{
int x = operation.GetSource(0).Value;
int y = operation.GetSource(1).Value;
operation.TurnIntoCopy(Const(op(x, y) ? IrConsts.True : IrConsts.False));
}
private static void EvaluateFPBinary(Operation operation, Func<float, float, float> op)
{
float x = operation.GetSource(0).AsFloat();
float y = operation.GetSource(1).AsFloat();
operation.TurnIntoCopy(ConstF(op(x, y)));
}
private static void EvaluateFPBinary(Operation operation, Func<float, float, bool> op)
{
float x = operation.GetSource(0).AsFloat();
float y = operation.GetSource(1).AsFloat();
operation.TurnIntoCopy(Const(op(x, y) ? IrConsts.True : IrConsts.False));
}
private static void EvaluateTernary(Operation operation, Func<int, int, int, int> op)
{
int x = operation.GetSource(0).Value;
int y = operation.GetSource(1).Value;
int z = operation.GetSource(2).Value;
operation.TurnIntoCopy(Const(op(x, y, z)));
}
private static void EvaluateFPTernary(Operation operation, Func<float, float, float, float> op)
{
float x = operation.GetSource(0).AsFloat();
float y = operation.GetSource(1).AsFloat();
float z = operation.GetSource(2).AsFloat();
operation.TurnIntoCopy(ConstF(op(x, y, z)));
}
}
}

View File

@@ -0,0 +1,70 @@
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
using System.Collections.Generic;
using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
namespace Ryujinx.Graphics.Shader.Translation.Optimizations
{
static class DoubleToFloat
{
public static void RunPass(HelperFunctionManager hfm, BasicBlock block)
{
for (LinkedListNode<INode> node = block.Operations.First; node != null; node = node.Next)
{
if (node.Value is not Operation)
{
continue;
}
node = InsertSoftFloat64(hfm, node);
}
}
private static LinkedListNode<INode> InsertSoftFloat64(HelperFunctionManager hfm, LinkedListNode<INode> node)
{
Operation operation = (Operation)node.Value;
if (operation.Inst == Instruction.PackDouble2x32)
{
int functionId = hfm.GetOrCreateFunctionId(HelperFunctionName.ConvertDoubleToFloat);
Operand[] callArgs = [Const(functionId), operation.GetSource(0), operation.GetSource(1)];
Operand floatValue = operation.Dest;
operation.Dest = null;
LinkedListNode<INode> newNode = node.List.AddBefore(node, new Operation(Instruction.Call, 0, floatValue, callArgs));
Utils.DeleteNode(node, operation);
return newNode;
}
else if (operation.Inst == Instruction.UnpackDouble2x32)
{
int functionId = hfm.GetOrCreateFunctionId(HelperFunctionName.ConvertFloatToDouble);
// TODO: Allow UnpackDouble2x32 to produce two outputs and get rid of "operation.Index".
Operand resultLow = operation.Index == 0 ? operation.Dest : Local();
Operand resultHigh = operation.Index == 1 ? operation.Dest : Local();
operation.Dest = null;
Operand[] callArgs = [Const(functionId), operation.GetSource(0), resultLow, resultHigh];
LinkedListNode<INode> newNode = node.List.AddBefore(node, new Operation(Instruction.Call, 0, (Operand)null, callArgs));
Utils.DeleteNode(node, operation);
return newNode;
}
else
{
operation.TurnDoubleIntoFloat();
return node;
}
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,458 @@
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace Ryujinx.Graphics.Shader.Translation.Optimizations
{
static class Optimizer
{
public static void RunPass(TransformContext context)
{
for (int blkIndex = 0; blkIndex < context.Blocks.Length; blkIndex++)
{
XmadOptimizer.RunPass(context.Blocks[blkIndex]);
}
RunOptimizationPasses(context.Blocks, context.ResourceManager);
// TODO: Some of those are not optimizations and shouldn't be here.
GlobalToStorage.RunPass(context.Hfm, context.Blocks, context.ResourceManager, context.GpuAccessor, context.TargetLanguage);
bool hostSupportsShaderFloat64 = context.GpuAccessor.QueryHostSupportsShaderFloat64();
// Those passes are looking for specific patterns and only needs to run once.
for (int blkIndex = 0; blkIndex < context.Blocks.Length; blkIndex++)
{
if (context.TargetApi == TargetApi.OpenGL)
{
BindlessToArray.RunPassOgl(context.Blocks[blkIndex], context.ResourceManager);
}
else
{
BindlessToArray.RunPass(context.Blocks[blkIndex], context.ResourceManager, context.GpuAccessor);
}
BindlessElimination.RunPass(context.Blocks[blkIndex], context.ResourceManager, context.GpuAccessor);
// FragmentCoord only exists on fragment shaders, so we don't need to check other stages.
if (context.Stage == ShaderStage.Fragment)
{
EliminateMultiplyByFragmentCoordW(context.Blocks[blkIndex]);
}
// If the host does not support double operations, we need to turn them into float operations.
if (!hostSupportsShaderFloat64)
{
DoubleToFloat.RunPass(context.Hfm, context.Blocks[blkIndex]);
}
}
// Run optimizations one last time to remove any code that is now optimizable after above passes.
RunOptimizationPasses(context.Blocks, context.ResourceManager);
}
private static void RunOptimizationPasses(BasicBlock[] blocks, ResourceManager resourceManager)
{
bool modified;
do
{
modified = false;
for (int blkIndex = 0; blkIndex < blocks.Length; blkIndex++)
{
BasicBlock block = blocks[blkIndex];
LinkedListNode<INode> node = block.Operations.First;
while (node != null)
{
LinkedListNode<INode> nextNode = node.Next;
bool isUnused = IsUnused(node.Value);
if (node.Value is not Operation operation || isUnused)
{
if (node.Value is PhiNode phi && !isUnused)
{
isUnused = PropagatePhi(phi);
}
if (isUnused)
{
RemoveNode(block, node);
modified = true;
}
node = nextNode;
continue;
}
ConstantFolding.RunPass(resourceManager, operation);
Simplification.RunPass(operation);
if (DestIsLocalVar(operation))
{
if (operation.Inst == Instruction.Copy)
{
PropagateCopy(operation);
RemoveNode(block, node);
modified = true;
}
else if ((operation.Inst == Instruction.PackHalf2x16 && PropagatePack(operation)) ||
(operation.Inst == Instruction.ShuffleXor && MatchDdxOrDdy(operation)))
{
if (DestHasNoUses(operation))
{
RemoveNode(block, node);
}
modified = true;
}
}
node = nextNode;
}
if (BranchElimination.RunPass(block))
{
RemoveNode(block, block.Operations.Last);
modified = true;
}
}
}
while (modified);
}
private static void PropagateCopy(Operation copyOp)
{
// Propagate copy source operand to all uses of
// the destination operand.
Operand dest = copyOp.Dest;
Operand src = copyOp.GetSource(0);
INode[] uses = dest.UseOps.ToArray();
foreach (INode useNode in uses)
{
for (int index = 0; index < useNode.SourcesCount; index++)
{
if (useNode.GetSource(index) == dest)
{
useNode.SetSource(index, src);
}
}
}
}
private static bool PropagatePhi(PhiNode phi)
{
// If all phi sources are the same, we can propagate it and remove the phi.
if (!Utils.AreAllSourcesTheSameOperand(phi))
{
return false;
}
// All sources are equal, we can propagate the value.
Operand firstSrc = phi.GetSource(0);
Operand dest = phi.Dest;
INode[] uses = dest.UseOps.ToArray();
foreach (INode useNode in uses)
{
for (int index = 0; index < useNode.SourcesCount; index++)
{
if (useNode.GetSource(index) == dest)
{
useNode.SetSource(index, firstSrc);
}
}
}
return true;
}
private static bool PropagatePack(Operation packOp)
{
// Propagate pack source operands to uses by unpack
// instruction. The source depends on the unpack instruction.
bool modified = false;
Operand dest = packOp.Dest;
Operand src0 = packOp.GetSource(0);
Operand src1 = packOp.GetSource(1);
INode[] uses = dest.UseOps.ToArray();
foreach (INode useNode in uses)
{
if (useNode is not Operation operation || operation.Inst != Instruction.UnpackHalf2x16)
{
continue;
}
if (operation.GetSource(0) == dest)
{
operation.TurnIntoCopy(operation.Index == 1 ? src1 : src0);
modified = true;
}
}
return modified;
}
public static bool MatchDdxOrDdy(Operation operation)
{
// It's assumed that "operation.Inst" is ShuffleXor,
// that should be checked before calling this method.
Debug.Assert(operation.Inst == Instruction.ShuffleXor);
bool modified = false;
Operand src2 = operation.GetSource(1);
Operand src3 = operation.GetSource(2);
if (src2.Type != OperandType.Constant || (src2.Value != 1 && src2.Value != 2))
{
return false;
}
if (src3.Type != OperandType.Constant || src3.Value != 0x1c03)
{
return false;
}
bool isDdy = src2.Value == 2;
bool isDdx = !isDdy;
// We can replace any use by a FSWZADD with DDX/DDY, when
// the following conditions are true:
// - The mask should be 0b10100101 for DDY, or 0b10011001 for DDX.
// - The first source operand must be the shuffle output.
// - The second source operand must be the shuffle first source operand.
INode[] uses = operation.Dest.UseOps.ToArray();
foreach (INode use in uses)
{
if (use is not Operation test)
{
continue;
}
if (use is not Operation useOp || useOp.Inst != Instruction.SwizzleAdd)
{
continue;
}
Operand fswzaddSrc1 = useOp.GetSource(0);
Operand fswzaddSrc2 = useOp.GetSource(1);
Operand fswzaddSrc3 = useOp.GetSource(2);
if (fswzaddSrc1 != operation.Dest)
{
continue;
}
if (fswzaddSrc2 != operation.GetSource(0))
{
continue;
}
if (fswzaddSrc3.Type != OperandType.Constant)
{
continue;
}
int mask = fswzaddSrc3.Value;
if ((isDdx && mask != 0b10011001) ||
(isDdy && mask != 0b10100101))
{
continue;
}
useOp.TurnInto(isDdx ? Instruction.Ddx : Instruction.Ddy, fswzaddSrc2);
modified = true;
}
return modified;
}
private static void EliminateMultiplyByFragmentCoordW(BasicBlock block)
{
foreach (INode node in block.Operations)
{
if (node is Operation operation)
{
EliminateMultiplyByFragmentCoordW(operation);
}
}
}
private static void EliminateMultiplyByFragmentCoordW(Operation operation)
{
// We're looking for the pattern:
// y = x * gl_FragCoord.w
// v = y * (1.0 / gl_FragCoord.w)
// Then we transform it into:
// v = x
// This pattern is common on fragment shaders due to the way how perspective correction is done.
// We are expecting a multiplication by the reciprocal of gl_FragCoord.w.
if (operation.Inst != (Instruction.FP32 | Instruction.Multiply))
{
return;
}
Operand lhs = operation.GetSource(0);
Operand rhs = operation.GetSource(1);
// Check LHS of the main multiplication operation. We expect an input being multiplied by gl_FragCoord.w.
if (lhs.AsgOp is not Operation attrMulOp || attrMulOp.Inst != (Instruction.FP32 | Instruction.Multiply))
{
return;
}
Operand attrMulLhs = attrMulOp.GetSource(0);
Operand attrMulRhs = attrMulOp.GetSource(1);
// LHS should be any input, RHS should be exactly gl_FragCoord.w.
if (!Utils.IsInputLoad(attrMulLhs.AsgOp) || !Utils.IsInputLoad(attrMulRhs.AsgOp, IoVariable.FragmentCoord, 3))
{
return;
}
// RHS of the main multiplication should be a reciprocal operation (1.0 / x).
if (rhs.AsgOp is not Operation reciprocalOp || reciprocalOp.Inst != (Instruction.FP32 | Instruction.Divide))
{
return;
}
Operand reciprocalLhs = reciprocalOp.GetSource(0);
Operand reciprocalRhs = reciprocalOp.GetSource(1);
// Check if the divisor is a constant equal to 1.0.
if (reciprocalLhs.Type != OperandType.Constant || reciprocalLhs.AsFloat() != 1.0f)
{
return;
}
// Check if the dividend is gl_FragCoord.w.
if (!Utils.IsInputLoad(reciprocalRhs.AsgOp, IoVariable.FragmentCoord, 3))
{
return;
}
// If everything matches, we can replace the operation with the input load result.
operation.TurnIntoCopy(attrMulLhs);
}
public static void RemoveNode(BasicBlock block, LinkedListNode<INode> llNode)
{
// Remove a node from the nodes list, and also remove itself
// from all the use lists on the operands that this node uses.
block.Operations.Remove(llNode);
Queue<INode> nodes = new();
nodes.Enqueue(llNode.Value);
while (nodes.TryDequeue(out INode node))
{
for (int index = 0; index < node.SourcesCount; index++)
{
Operand src = node.GetSource(index);
if (src.Type != OperandType.LocalVariable)
{
continue;
}
if (src.UseOps.Remove(node) && src.UseOps.Count == 0)
{
Debug.Assert(src.AsgOp != null);
nodes.Enqueue(src.AsgOp);
}
}
}
}
private static bool IsUnused(INode node)
{
return !HasSideEffects(node) && DestIsLocalVar(node) && DestHasNoUses(node);
}
private static bool HasSideEffects(INode node)
{
if (node is Operation operation)
{
switch (operation.Inst & Instruction.Mask)
{
case Instruction.AtomicAdd:
case Instruction.AtomicAnd:
case Instruction.AtomicCompareAndSwap:
case Instruction.AtomicMaxS32:
case Instruction.AtomicMaxU32:
case Instruction.AtomicMinS32:
case Instruction.AtomicMinU32:
case Instruction.AtomicOr:
case Instruction.AtomicSwap:
case Instruction.AtomicXor:
case Instruction.Call:
case Instruction.ImageAtomic:
return true;
}
}
return false;
}
private static bool DestIsLocalVar(INode node)
{
if (node.DestsCount == 0)
{
return false;
}
for (int index = 0; index < node.DestsCount; index++)
{
Operand dest = node.GetDest(index);
if (dest != null && dest.Type != OperandType.LocalVariable)
{
return false;
}
}
return true;
}
private static bool DestHasNoUses(INode node)
{
for (int index = 0; index < node.DestsCount; index++)
{
Operand dest = node.GetDest(index);
if (dest != null && dest.UseOps.Count != 0)
{
return false;
}
}
return true;
}
}
}

View File

@@ -0,0 +1,236 @@
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
namespace Ryujinx.Graphics.Shader.Translation.Optimizations
{
static class Simplification
{
private const int AllOnes = ~0;
public static void RunPass(Operation operation)
{
switch (operation.Inst)
{
case Instruction.Add:
TryEliminateBinaryOpCommutative(operation, 0);
break;
case Instruction.BitwiseAnd:
TryEliminateBitwiseAnd(operation);
break;
case Instruction.BitwiseExclusiveOr:
if (!TryEliminateXorSwap(operation))
{
TryEliminateBinaryOpCommutative(operation, 0);
}
break;
case Instruction.BitwiseOr:
TryEliminateBitwiseOr(operation);
break;
case Instruction.CompareNotEqual:
TryEliminateCompareNotEqual(operation);
break;
case Instruction.ConditionalSelect:
TryEliminateConditionalSelect(operation);
break;
case Instruction.Divide:
TryEliminateBinaryOpY(operation, 1);
break;
case Instruction.Multiply:
TryEliminateBinaryOpCommutative(operation, 1);
break;
case Instruction.ShiftLeft:
case Instruction.ShiftRightS32:
case Instruction.ShiftRightU32:
case Instruction.Subtract:
TryEliminateBinaryOpY(operation, 0);
break;
}
}
private static void TryEliminateBitwiseAnd(Operation operation)
{
// Try to recognize and optimize those 3 patterns (in order):
// x & 0xFFFFFFFF == x, 0xFFFFFFFF & y == y,
// x & 0x00000000 == 0x00000000, 0x00000000 & y == 0x00000000
Operand x = operation.GetSource(0);
Operand y = operation.GetSource(1);
if (IsConstEqual(x, AllOnes))
{
operation.TurnIntoCopy(y);
}
else if (IsConstEqual(y, AllOnes))
{
operation.TurnIntoCopy(x);
}
else if (IsConstEqual(x, 0) || IsConstEqual(y, 0))
{
operation.TurnIntoCopy(Const(0));
}
}
private static bool TryEliminateXorSwap(Operation xCopyOp)
{
// Try to recognize XOR swap pattern:
// x = x ^ y
// y = x ^ y
// x = x ^ y
// Or, in SSA:
// x2 = x ^ y
// y2 = x2 ^ y
// x3 = x2 ^ y2
// Transform it into something more sane:
// temp = y
// y = x
// x = temp
// Note that because XOR is commutative, there are actually
// multiple possible combinations of this pattern, for
// simplicity this only catches one of them.
Operand x = xCopyOp.GetSource(0);
Operand y = xCopyOp.GetSource(1);
if (x.AsgOp is not Operation tCopyOp || tCopyOp.Inst != Instruction.BitwiseExclusiveOr ||
y.AsgOp is not Operation yCopyOp || yCopyOp.Inst != Instruction.BitwiseExclusiveOr)
{
return false;
}
if (tCopyOp == yCopyOp)
{
return false;
}
if (yCopyOp.GetSource(0) != x ||
yCopyOp.GetSource(1) != tCopyOp.GetSource(1) ||
x.UseOps.Count != 2)
{
return false;
}
x = tCopyOp.GetSource(0);
y = tCopyOp.GetSource(1);
tCopyOp.TurnIntoCopy(y); // Temp = Y
yCopyOp.TurnIntoCopy(x); // Y = X
xCopyOp.TurnIntoCopy(tCopyOp.Dest); // X = Temp
return true;
}
private static void TryEliminateBitwiseOr(Operation operation)
{
// Try to recognize and optimize those 3 patterns (in order):
// x | 0x00000000 == x, 0x00000000 | y == y,
// x | 0xFFFFFFFF == 0xFFFFFFFF, 0xFFFFFFFF | y == 0xFFFFFFFF
Operand x = operation.GetSource(0);
Operand y = operation.GetSource(1);
if (IsConstEqual(x, 0))
{
operation.TurnIntoCopy(y);
}
else if (IsConstEqual(y, 0))
{
operation.TurnIntoCopy(x);
}
else if (IsConstEqual(x, AllOnes) || IsConstEqual(y, AllOnes))
{
operation.TurnIntoCopy(Const(AllOnes));
}
}
private static void TryEliminateBinaryOpY(Operation operation, int comparand)
{
Operand x = operation.GetSource(0);
Operand y = operation.GetSource(1);
if (IsConstEqual(y, comparand))
{
operation.TurnIntoCopy(x);
}
}
private static void TryEliminateBinaryOpCommutative(Operation operation, int comparand)
{
Operand x = operation.GetSource(0);
Operand y = operation.GetSource(1);
if (IsConstEqual(x, comparand))
{
operation.TurnIntoCopy(y);
}
else if (IsConstEqual(y, comparand))
{
operation.TurnIntoCopy(x);
}
}
private static void TryEliminateCompareNotEqual(Operation operation)
{
// Comparison instruction returns 0 if the result is false, and -1 if true.
// Doing a not equal zero comparison on the result is redundant, so we can just copy the first result in this case.
Operand lhs = operation.GetSource(0);
Operand rhs = operation.GetSource(1);
if (lhs.Type == OperandType.Constant)
{
(lhs, rhs) = (rhs, lhs);
}
if (rhs.Type != OperandType.Constant || rhs.Value != 0)
{
return;
}
if (lhs.AsgOp is not Operation compareOp || !compareOp.Inst.IsComparison())
{
return;
}
operation.TurnIntoCopy(lhs);
}
private static void TryEliminateConditionalSelect(Operation operation)
{
Operand cond = operation.GetSource(0);
if (cond.Type != OperandType.Constant)
{
return;
}
// The condition is constant, we can turn it into a copy, and select
// the source based on the condition value.
int srcIndex = cond.Value != 0 ? 1 : 2;
Operand source = operation.GetSource(srcIndex);
operation.TurnIntoCopy(source);
}
private static bool IsConstEqual(Operand operand, int comparand)
{
if (operand.Type != OperandType.Constant)
{
return false;
}
return operand.Value == comparand;
}
}
}

View File

@@ -0,0 +1,202 @@
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
using System.Collections.Generic;
namespace Ryujinx.Graphics.Shader.Translation.Optimizations
{
static class Utils
{
public static bool IsInputLoad(INode node)
{
return (node is Operation operation) &&
operation.Inst == Instruction.Load &&
operation.StorageKind == StorageKind.Input;
}
public static bool IsInputLoad(INode node, IoVariable ioVariable, int elemIndex)
{
if (node is not Operation operation ||
operation.Inst != Instruction.Load ||
operation.StorageKind != StorageKind.Input ||
operation.SourcesCount != 2)
{
return false;
}
Operand ioVariableSrc = operation.GetSource(0);
if (ioVariableSrc.Type != OperandType.Constant || (IoVariable)ioVariableSrc.Value != ioVariable)
{
return false;
}
Operand elemIndexSrc = operation.GetSource(1);
return elemIndexSrc.Type == OperandType.Constant && elemIndexSrc.Value == elemIndex;
}
private static bool IsSameOperand(Operand x, Operand y)
{
if (x.Type != y.Type || x.Value != y.Value)
{
return false;
}
// TODO: Handle Load operations with the same storage and the same constant parameters.
return x == y || x.Type == OperandType.Constant || x.Type == OperandType.ConstantBuffer;
}
private static bool AreAllSourcesEqual(Operation node, Operation otherNode)
{
if (node.SourcesCount != otherNode.SourcesCount)
{
return false;
}
for (int index = 0; index < node.SourcesCount; index++)
{
if (!IsSameOperand(node.GetSource(index), otherNode.GetSource(index)))
{
return false;
}
}
return true;
}
public static bool AreAllSourcesTheSameOperand(INode node)
{
Operand firstSrc = node.GetSource(0);
for (int index = 1; index < node.SourcesCount; index++)
{
if (!IsSameOperand(firstSrc, node.GetSource(index)))
{
return false;
}
}
return true;
}
private static Operation FindBranchSource(BasicBlock block)
{
foreach (BasicBlock sourceBlock in block.Predecessors)
{
if (sourceBlock.Operations.Count > 0)
{
if (sourceBlock.GetLastOp() is Operation lastOp && IsConditionalBranch(lastOp.Inst) && sourceBlock.Next == block)
{
return lastOp;
}
}
}
return null;
}
private static bool IsConditionalBranch(Instruction inst)
{
return inst is Instruction.BranchIfFalse or Instruction.BranchIfTrue;
}
private static bool IsSameCondition(Operand currentCondition, Operand queryCondition)
{
if (currentCondition == queryCondition)
{
return true;
}
return currentCondition.AsgOp is Operation currentOperation &&
queryCondition.AsgOp is Operation queryOperation &&
currentOperation.Inst == queryOperation.Inst &&
AreAllSourcesEqual(currentOperation, queryOperation);
}
private static bool BlockConditionsMatch(BasicBlock currentBlock, BasicBlock queryBlock)
{
// Check if all the conditions for the query block are satisfied by the current block.
// Just checks the top-most conditional for now.
Operation currentBranch = FindBranchSource(currentBlock);
Operation queryBranch = FindBranchSource(queryBlock);
Operand currentCondition = currentBranch?.GetSource(0);
Operand queryCondition = queryBranch?.GetSource(0);
// The condition should be the same operand instance.
return currentBranch != null && queryBranch != null &&
currentBranch.Inst == queryBranch.Inst &&
IsSameCondition(currentCondition, queryCondition);
}
public static Operand FindLastOperation(Operand source, BasicBlock block, bool recurse = true)
{
if (source.AsgOp is PhiNode phiNode)
{
// This source can have a different value depending on a previous branch.
// Ensure that conditions met for that branch are also met for the current one.
// Prefer the latest sources for the phi node.
int undefCount = 0;
for (int i = phiNode.SourcesCount - 1; i >= 0; i--)
{
BasicBlock phiBlock = phiNode.GetBlock(i);
Operand phiSource = phiNode.GetSource(i);
if (BlockConditionsMatch(block, phiBlock))
{
return phiSource;
}
else if (recurse && phiSource.AsgOp is PhiNode)
{
// Phi source is another phi.
// Let's check if that phi has a block that matches our condition.
Operand match = FindLastOperation(phiSource, block, false);
if (match != phiSource)
{
return match;
}
}
else if (phiSource.Type == OperandType.Undefined)
{
undefCount++;
}
}
// If all sources but one are undefined, we can assume that the one
// that is not undefined is the right one.
if (undefCount == phiNode.SourcesCount - 1)
{
for (int i = phiNode.SourcesCount - 1; i >= 0; i--)
{
Operand phiSource = phiNode.GetSource(i);
if (phiSource.Type != OperandType.Undefined)
{
return phiSource;
}
}
}
}
return source;
}
public static void DeleteNode(LinkedListNode<INode> node, Operation operation)
{
node.List.Remove(node);
for (int srcIndex = 0; srcIndex < operation.SourcesCount; srcIndex++)
{
operation.SetSource(srcIndex, null);
}
operation.Dest = null;
}
}
}

View File

@@ -0,0 +1,342 @@
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
using System.Collections.Generic;
namespace Ryujinx.Graphics.Shader.Translation.Optimizations
{
static class XmadOptimizer
{
public static void RunPass(BasicBlock block)
{
for (LinkedListNode<INode> node = block.Operations.First; node != null; node = node.Next)
{
if (node.Value is not Operation operation)
{
continue;
}
if (TryMatchXmadPattern(operation, out Operand x, out Operand y, out Operand addend))
{
LinkedListNode<INode> nextNode;
if (addend != null)
{
Operand temp = OperandHelper.Local();
nextNode = block.Operations.AddAfter(node, new Operation(Instruction.Multiply, temp, x, y));
nextNode = block.Operations.AddAfter(nextNode, new Operation(Instruction.Add, operation.Dest, temp, addend));
}
else
{
nextNode = block.Operations.AddAfter(node, new Operation(Instruction.Multiply, operation.Dest, x, y));
}
Optimizer.RemoveNode(block, node);
node = nextNode;
}
}
}
private static bool TryMatchXmadPattern(Operation operation, out Operand x, out Operand y, out Operand addend)
{
return TryMatchXmad32x32Pattern(operation, out x, out y, out addend) ||
TryMatchXmad32x16Pattern(operation, out x, out y, out addend);
}
private static bool TryMatchXmad32x32Pattern(Operation operation, out Operand x, out Operand y, out Operand addend)
{
x = null;
y = null;
addend = null;
if (operation.Inst != Instruction.Add)
{
return false;
}
Operand src1 = operation.GetSource(0);
Operand src2 = operation.GetSource(1);
if (src2.AsgOp is not Operation addOp || addOp.Inst != Instruction.Add)
{
return false;
}
Operand lowTimesLowResult = GetCopySource(addOp.GetSource(0));
if (lowTimesLowResult.AsgOp is not Operation lowTimesLowOp)
{
return false;
}
if (!TryMatchLowTimesLow(lowTimesLowOp, out x, out y, out addend))
{
return false;
}
Operand lowTimesHighResult = GetCopySource(GetShifted16Source(addOp.GetSource(1), Instruction.ShiftLeft));
if (lowTimesHighResult.AsgOp is not Operation lowTimesHighOp)
{
return false;
}
if (!TryMatchLowTimesHigh(lowTimesHighOp, x, y))
{
return false;
}
if (src1.AsgOp is not Operation highTimesHighOp)
{
return false;
}
if (!TryMatchHighTimesHigh(highTimesHighOp, x, lowTimesHighResult))
{
return false;
}
return true;
}
private static bool TryMatchXmad32x16Pattern(Operation operation, out Operand x, out Operand y, out Operand addend)
{
x = null;
y = null;
addend = null;
if (operation.Inst != Instruction.Add)
{
return false;
}
Operand src1 = operation.GetSource(0);
Operand src2 = operation.GetSource(1);
Operand lowTimesLowResult = GetCopySource(src2);
if (lowTimesLowResult.AsgOp is not Operation lowTimesLowOp)
{
return false;
}
if (!TryMatchLowTimesLow(lowTimesLowOp, out x, out y, out addend))
{
return false;
}
Operand highTimesLowResult = src1;
if (highTimesLowResult.AsgOp is not Operation highTimesLowOp)
{
return false;
}
if (!TryMatchHighTimesLow(highTimesLowOp, x, y))
{
return false;
}
return y.Type == OperandType.Constant && (ushort)y.Value == y.Value;
}
private static bool TryMatchLowTimesLow(Operation operation, out Operand x, out Operand y, out Operand addend)
{
// x = x & 0xFFFF
// y = y & 0xFFFF
// lowTimesLow = x * y
x = null;
y = null;
addend = null;
if (operation.Inst == Instruction.Add)
{
if (operation.GetSource(0).AsgOp is not Operation mulOp)
{
return false;
}
addend = operation.GetSource(1);
operation = mulOp;
}
if (operation.Inst != Instruction.Multiply)
{
return false;
}
Operand src1 = GetMasked16Source(operation.GetSource(0));
Operand src2 = GetMasked16Source(operation.GetSource(1));
if (src1 == null || src2 == null)
{
return false;
}
x = src1;
y = src2;
return true;
}
private static bool TryMatchLowTimesHigh(Operation operation, Operand x, Operand y)
{
// xLow = x & 0xFFFF
// yHigh = y >> 16
// lowTimesHigh = xLow * yHigh
// result = (lowTimesHigh & 0xFFFF) | (y << 16)
if (operation.Inst != Instruction.BitwiseOr)
{
return false;
}
Operand mulResult = GetMasked16Source(operation.GetSource(0));
if (mulResult == null)
{
return false;
}
mulResult = GetCopySource(mulResult);
if (mulResult.AsgOp is not Operation mulOp || mulOp.Inst != Instruction.Multiply)
{
return false;
}
if (GetMasked16Source(mulOp.GetSource(0)) != x)
{
return false;
}
if (GetShifted16Source(mulOp.GetSource(1), Instruction.ShiftRightU32) != y)
{
return false;
}
if (GetShifted16Source(operation.GetSource(1), Instruction.ShiftLeft) != y)
{
return false;
}
return true;
}
private static bool TryMatchHighTimesLow(Operation operation, Operand x, Operand y)
{
// xHigh = x >> 16
// yLow = y & 0xFFFF
// highTimesLow = xHigh * yLow
// result = highTimesLow << 16
if (operation.Inst != Instruction.ShiftLeft || !IsConst(operation.GetSource(1), 16))
{
return false;
}
Operand mulResult = operation.GetSource(0);
if (mulResult.AsgOp is not Operation mulOp || mulOp.Inst != Instruction.Multiply)
{
return false;
}
if (GetShifted16Source(mulOp.GetSource(0), Instruction.ShiftRightU32) != x)
{
return false;
}
Operand src2 = GetMasked16Source(mulOp.GetSource(1));
if (src2.Type != y.Type || src2.Value != y.Value)
{
return false;
}
return true;
}
private static bool TryMatchHighTimesHigh(Operation operation, Operand x, Operand lowTimesHighResult)
{
// xHigh = x >> 16
// lowTimesHighResultHigh = lowTimesHighResult >> 16
// highTimesHigh = xHigh * lowTimesHighResultHigh
// result = highTimesHigh << 16
if (operation.Inst != Instruction.ShiftLeft || !IsConst(operation.GetSource(1), 16))
{
return false;
}
Operand mulResult = operation.GetSource(0);
if (mulResult.AsgOp is not Operation mulOp || mulOp.Inst != Instruction.Multiply)
{
return false;
}
if (GetShifted16Source(mulOp.GetSource(0), Instruction.ShiftRightU32) != x)
{
return false;
}
if (GetCopySource(GetShifted16Source(mulOp.GetSource(1), Instruction.ShiftRightU32)) != lowTimesHighResult)
{
return false;
}
return true;
}
private static Operand GetMasked16Source(Operand value)
{
if (value.AsgOp is not Operation maskOp)
{
return null;
}
if (maskOp.Inst != Instruction.BitwiseAnd || !IsConst(maskOp.GetSource(1), ushort.MaxValue))
{
return null;
}
return maskOp.GetSource(0);
}
private static Operand GetShifted16Source(Operand value, Instruction shiftInst)
{
if (value.AsgOp is not Operation shiftOp)
{
return null;
}
if (shiftOp.Inst != shiftInst || !IsConst(shiftOp.GetSource(1), 16))
{
return null;
}
return shiftOp.GetSource(0);
}
private static Operand GetCopySource(Operand value)
{
while (value.AsgOp is Operation operation && IsCopy(operation))
{
value = operation.GetSource(0);
}
return value;
}
private static bool IsCopy(Operation operation)
{
return operation.Inst == Instruction.Copy || (operation.Inst == Instruction.Add && IsConst(operation.GetSource(1), 0));
}
private static bool IsConst(Operand operand, int value)
{
return operand.Type == OperandType.Constant && operand.Value == value;
}
}
}

View File

@@ -0,0 +1,491 @@
using Ryujinx.Graphics.Shader.Decoders;
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
namespace Ryujinx.Graphics.Shader.Translation
{
static class RegisterUsage
{
private const int RegsCount = 256;
private const int RegsMask = RegsCount - 1;
private const int GprMasks = 4;
private const int PredMasks = 1;
private const int FlagMasks = 1;
private const int TotalMasks = GprMasks + PredMasks + FlagMasks;
private struct RegisterMask : IEquatable<RegisterMask>
{
public long GprMask0 { get; set; }
public long GprMask1 { get; set; }
public long GprMask2 { get; set; }
public long GprMask3 { get; set; }
public long PredMask { get; set; }
public long FlagMask { get; set; }
public RegisterMask(long gprMask0, long gprMask1, long gprMask2, long gprMask3, long predMask, long flagMask)
{
GprMask0 = gprMask0;
GprMask1 = gprMask1;
GprMask2 = gprMask2;
GprMask3 = gprMask3;
PredMask = predMask;
FlagMask = flagMask;
}
public readonly long GetMask(int index)
{
return index switch
{
0 => GprMask0,
1 => GprMask1,
2 => GprMask2,
3 => GprMask3,
4 => PredMask,
5 => FlagMask,
_ => throw new ArgumentOutOfRangeException(nameof(index)),
};
}
public static RegisterMask operator &(RegisterMask x, RegisterMask y)
{
return new RegisterMask(
x.GprMask0 & y.GprMask0,
x.GprMask1 & y.GprMask1,
x.GprMask2 & y.GprMask2,
x.GprMask3 & y.GprMask3,
x.PredMask & y.PredMask,
x.FlagMask & y.FlagMask);
}
public static RegisterMask operator |(RegisterMask x, RegisterMask y)
{
return new RegisterMask(
x.GprMask0 | y.GprMask0,
x.GprMask1 | y.GprMask1,
x.GprMask2 | y.GprMask2,
x.GprMask3 | y.GprMask3,
x.PredMask | y.PredMask,
x.FlagMask | y.FlagMask);
}
public static RegisterMask operator ~(RegisterMask x)
{
return new RegisterMask(
~x.GprMask0,
~x.GprMask1,
~x.GprMask2,
~x.GprMask3,
~x.PredMask,
~x.FlagMask);
}
public static bool operator ==(RegisterMask x, RegisterMask y)
{
return x.Equals(y);
}
public static bool operator !=(RegisterMask x, RegisterMask y)
{
return !x.Equals(y);
}
public readonly override bool Equals(object obj)
{
return obj is RegisterMask regMask && Equals(regMask);
}
public readonly bool Equals(RegisterMask other)
{
return GprMask0 == other.GprMask0 &&
GprMask1 == other.GprMask1 &&
GprMask2 == other.GprMask2 &&
GprMask3 == other.GprMask3 &&
PredMask == other.PredMask &&
FlagMask == other.FlagMask;
}
public readonly override int GetHashCode()
{
return HashCode.Combine(GprMask0, GprMask1, GprMask2, GprMask3, PredMask, FlagMask);
}
}
public readonly struct FunctionRegisterUsage
{
public Register[] InArguments { get; }
public Register[] OutArguments { get; }
public FunctionRegisterUsage(Register[] inArguments, Register[] outArguments)
{
InArguments = inArguments;
OutArguments = outArguments;
}
}
public static FunctionRegisterUsage RunPass(ControlFlowGraph cfg)
{
List<Register> inArguments = [];
List<Register> outArguments = [];
// Compute local register inputs and outputs used inside blocks.
RegisterMask[] localInputs = new RegisterMask[cfg.Blocks.Length];
RegisterMask[] localOutputs = new RegisterMask[cfg.Blocks.Length];
foreach (BasicBlock block in cfg.Blocks)
{
for (LinkedListNode<INode> node = block.Operations.First; node != null; node = node.Next)
{
Operation operation = node.Value as Operation;
for (int srcIndex = 0; srcIndex < operation.SourcesCount; srcIndex++)
{
Operand source = operation.GetSource(srcIndex);
if (source.Type != OperandType.Register)
{
continue;
}
Register register = source.GetRegister();
localInputs[block.Index] |= GetMask(register) & ~localOutputs[block.Index];
}
for (int dstIndex = 0; dstIndex < operation.DestsCount; dstIndex++)
{
Operand dest = operation.GetDest(dstIndex);
if (dest != null && dest.Type == OperandType.Register)
{
localOutputs[block.Index] |= GetMask(dest.GetRegister());
}
}
}
}
// Compute global register inputs and outputs used across blocks.
RegisterMask[] globalCmnOutputs = new RegisterMask[cfg.Blocks.Length];
RegisterMask[] globalInputs = new RegisterMask[cfg.Blocks.Length];
RegisterMask[] globalOutputs = new RegisterMask[cfg.Blocks.Length];
RegisterMask allOutputs = new();
RegisterMask allCmnOutputs = new(-1L, -1L, -1L, -1L, -1L, -1L);
bool modified;
bool firstPass = true;
do
{
modified = false;
// Compute register outputs.
for (int index = cfg.PostOrderBlocks.Length - 1; index >= 0; index--)
{
BasicBlock block = cfg.PostOrderBlocks[index];
if (block.Predecessors.Count != 0)
{
BasicBlock predecessor = block.Predecessors[0];
RegisterMask cmnOutputs = localOutputs[predecessor.Index] | globalCmnOutputs[predecessor.Index];
RegisterMask outputs = globalOutputs[predecessor.Index];
for (int pIndex = 1; pIndex < block.Predecessors.Count; pIndex++)
{
predecessor = block.Predecessors[pIndex];
cmnOutputs &= localOutputs[predecessor.Index] | globalCmnOutputs[predecessor.Index];
outputs |= globalOutputs[predecessor.Index];
}
globalInputs[block.Index] |= outputs & ~cmnOutputs;
if (!firstPass)
{
cmnOutputs &= globalCmnOutputs[block.Index];
}
if (EndsWithReturn(block))
{
allCmnOutputs &= cmnOutputs | localOutputs[block.Index];
}
if (Exchange(globalCmnOutputs, block.Index, cmnOutputs))
{
modified = true;
}
outputs |= localOutputs[block.Index];
if (Exchange(globalOutputs, block.Index, globalOutputs[block.Index] | outputs))
{
allOutputs |= outputs;
modified = true;
}
}
else if (Exchange(globalOutputs, block.Index, localOutputs[block.Index]))
{
allOutputs |= localOutputs[block.Index];
modified = true;
}
}
// Compute register inputs.
for (int index = 0; index < cfg.PostOrderBlocks.Length; index++)
{
BasicBlock block = cfg.PostOrderBlocks[index];
RegisterMask inputs = localInputs[block.Index];
if (block.Next != null)
{
inputs |= globalInputs[block.Next.Index];
}
if (block.Branch != null)
{
inputs |= globalInputs[block.Branch.Index];
}
inputs &= ~globalCmnOutputs[block.Index];
if (Exchange(globalInputs, block.Index, globalInputs[block.Index] | inputs))
{
modified = true;
}
}
firstPass = false;
}
while (modified);
// Insert load and store context instructions where needed.
foreach (BasicBlock block in cfg.Blocks)
{
// The only block without any predecessor should be the entry block.
// It always needs a context load as it is the first block to run.
if (block.Predecessors.Count == 0)
{
RegisterMask inputs = globalInputs[block.Index] | (allOutputs & ~allCmnOutputs);
LoadLocals(block, inputs, inArguments);
}
if (EndsWithReturn(block))
{
StoreLocals(block, allOutputs, inArguments.Count, outArguments);
}
}
return new FunctionRegisterUsage(inArguments.ToArray(), outArguments.ToArray());
}
public static void FixupCalls(BasicBlock[] blocks, FunctionRegisterUsage[] frus)
{
foreach (BasicBlock block in blocks)
{
for (LinkedListNode<INode> node = block.Operations.First; node != null; node = node.Next)
{
Operation operation = node.Value as Operation;
if (operation.Inst == Instruction.Call)
{
Operand funcId = operation.GetSource(0);
Debug.Assert(funcId.Type == OperandType.Constant);
FunctionRegisterUsage fru = frus[funcId.Value];
Operand[] inRegs = new Operand[fru.InArguments.Length];
for (int i = 0; i < fru.InArguments.Length; i++)
{
inRegs[i] = OperandHelper.Register(fru.InArguments[i]);
}
operation.AppendSources(inRegs);
Operand[] outRegs = new Operand[1 + fru.OutArguments.Length];
for (int i = 0; i < fru.OutArguments.Length; i++)
{
outRegs[1 + i] = OperandHelper.Register(fru.OutArguments[i]);
}
operation.AppendDests(outRegs);
}
}
}
}
private static bool StartsWith(BasicBlock block, Instruction inst)
{
if (block.Operations.Count == 0)
{
return false;
}
return block.Operations.First.Value is Operation operation && operation.Inst == inst;
}
private static bool EndsWith(BasicBlock block, Instruction inst)
{
if (block.Operations.Count == 0)
{
return false;
}
return block.Operations.Last.Value is Operation operation && operation.Inst == inst;
}
private static RegisterMask GetMask(Register register)
{
Span<long> gprMasks = stackalloc long[4];
long predMask = 0;
long flagMask = 0;
switch (register.Type)
{
case RegisterType.Gpr:
gprMasks[register.Index >> 6] = 1L << (register.Index & 0x3f);
break;
case RegisterType.Predicate:
predMask = 1L << register.Index;
break;
case RegisterType.Flag:
flagMask = 1L << register.Index;
break;
}
return new RegisterMask(gprMasks[0], gprMasks[1], gprMasks[2], gprMasks[3], predMask, flagMask);
}
private static bool Exchange(RegisterMask[] masks, int blkIndex, RegisterMask value)
{
RegisterMask oldValue = masks[blkIndex];
masks[blkIndex] = value;
return oldValue != value;
}
private static void LoadLocals(BasicBlock block, RegisterMask masks, List<Register> inArguments)
{
bool fillArgsList = inArguments.Count == 0;
LinkedListNode<INode> node = null;
int argIndex = 0;
for (int i = 0; i < TotalMasks; i++)
{
(RegisterType regType, int baseRegIndex) = GetRegTypeAndBaseIndex(i);
long mask = masks.GetMask(i);
while (mask != 0)
{
int bit = BitOperations.TrailingZeroCount(mask);
mask &= ~(1L << bit);
Register register = new(baseRegIndex + bit, regType);
if (fillArgsList)
{
inArguments.Add(register);
}
Operation copyOp = new(Instruction.Copy, OperandHelper.Register(register), OperandHelper.Argument(argIndex++));
if (node == null)
{
node = block.Operations.AddFirst(copyOp);
}
else
{
node = block.Operations.AddAfter(node, copyOp);
}
}
}
Debug.Assert(argIndex <= inArguments.Count);
}
private static void StoreLocals(BasicBlock block, RegisterMask masks, int inArgumentsCount, List<Register> outArguments)
{
LinkedListNode<INode> node = null;
int argIndex = inArgumentsCount;
bool fillArgsList = outArguments.Count == 0;
for (int i = 0; i < TotalMasks; i++)
{
(RegisterType regType, int baseRegIndex) = GetRegTypeAndBaseIndex(i);
long mask = masks.GetMask(i);
while (mask != 0)
{
int bit = BitOperations.TrailingZeroCount(mask);
mask &= ~(1L << bit);
Register register = new(baseRegIndex + bit, regType);
if (fillArgsList)
{
outArguments.Add(register);
}
Operation copyOp = new(Instruction.Copy, OperandHelper.Argument(argIndex++), OperandHelper.Register(register));
if (node == null)
{
node = block.Operations.AddBefore(block.Operations.Last, copyOp);
}
else
{
node = block.Operations.AddAfter(node, copyOp);
}
}
}
Debug.Assert(argIndex <= inArgumentsCount + outArguments.Count);
}
private static (RegisterType RegType, int BaseRegIndex) GetRegTypeAndBaseIndex(int i)
{
RegisterType regType = RegisterType.Gpr;
int baseRegIndex = 0;
if (i < GprMasks)
{
baseRegIndex = i * sizeof(long) * 8;
}
else if (i == GprMasks)
{
regType = RegisterType.Predicate;
}
else
{
regType = RegisterType.Flag;
}
return (regType, baseRegIndex);
}
private static bool EndsWithReturn(BasicBlock block)
{
if (block.GetLastOp() is not Operation operation)
{
return false;
}
return operation.Inst == Instruction.Return;
}
}
}

View File

@@ -0,0 +1,649 @@
using Ryujinx.Common;
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
using Ryujinx.Graphics.Shader.StructuredIr;
using System;
using System.Collections.Generic;
using System.Globalization;
namespace Ryujinx.Graphics.Shader.Translation
{
class ResourceManager
{
// Those values are used if the shader as local or shared memory access,
// but for some reason the supplied size was 0.
private const int DefaultLocalMemorySize = 128;
private const int DefaultSharedMemorySize = 4096;
private static readonly string[] _stagePrefixes = new string[] { "cp", "vp", "tcp", "tep", "gp", "fp" };
private readonly IGpuAccessor _gpuAccessor;
private readonly ShaderStage _stage;
private readonly string _stagePrefix;
private readonly SetBindingPair[] _cbSlotToBindingMap;
private readonly SetBindingPair[] _sbSlotToBindingMap;
private uint _sbSlotWritten;
private readonly Dictionary<int, int> _sbSlots;
private readonly Dictionary<int, int> _sbSlotsReverse;
private readonly HashSet<int> _usedConstantBufferBindings;
private readonly record struct TextureInfo(int CbufSlot, int Handle, int ArrayLength, bool Separate, SamplerType Type, TextureFormat Format);
private struct TextureMeta
{
public int Set;
public int Binding;
public bool AccurateType;
public SamplerType Type;
public TextureUsageFlags UsageFlags;
}
private readonly Dictionary<TextureInfo, TextureMeta> _usedTextures;
private readonly Dictionary<TextureInfo, TextureMeta> _usedImages;
public int LocalMemoryId { get; private set; }
public int SharedMemoryId { get; private set; }
public int LocalVertexDataMemoryId { get; private set; }
public int LocalTopologyRemapMemoryId { get; private set; }
public int LocalVertexIndexVertexRateMemoryId { get; private set; }
public int LocalVertexIndexInstanceRateMemoryId { get; private set; }
public int LocalGeometryOutputVertexCountMemoryId { get; private set; }
public int LocalGeometryOutputIndexCountMemoryId { get; private set; }
public ShaderProperties Properties { get; }
public ResourceReservations Reservations { get; }
public ResourceManager(ShaderStage stage, IGpuAccessor gpuAccessor, ResourceReservations reservations = null)
{
_gpuAccessor = gpuAccessor;
Properties = new();
Reservations = reservations;
_stage = stage;
_stagePrefix = GetShaderStagePrefix(stage);
_cbSlotToBindingMap = new SetBindingPair[18];
_sbSlotToBindingMap = new SetBindingPair[16];
_cbSlotToBindingMap.AsSpan().Fill(new(-1, -1));
_sbSlotToBindingMap.AsSpan().Fill(new(-1, -1));
_sbSlots = new();
_sbSlotsReverse = new();
_usedConstantBufferBindings = new();
_usedTextures = new();
_usedImages = new();
Properties.AddOrUpdateConstantBuffer(new(BufferLayout.Std140, 0, SupportBuffer.Binding, "support_buffer", SupportBuffer.GetStructureType()));
LocalMemoryId = -1;
SharedMemoryId = -1;
}
public void SetCurrentLocalMemory(int size, bool isUsed)
{
if (isUsed)
{
if (size <= 0)
{
size = DefaultLocalMemorySize;
}
MemoryDefinition lmem = new MemoryDefinition("local_memory", AggregateType.Array | AggregateType.U32, BitUtils.DivRoundUp(size, sizeof(uint)));
LocalMemoryId = Properties.AddLocalMemory(lmem);
}
else
{
LocalMemoryId = -1;
}
}
public void SetCurrentSharedMemory(int size, bool isUsed)
{
if (isUsed)
{
if (size <= 0)
{
size = DefaultSharedMemorySize;
}
MemoryDefinition smem = new MemoryDefinition("shared_memory", AggregateType.Array | AggregateType.U32, BitUtils.DivRoundUp(size, sizeof(uint)));
SharedMemoryId = Properties.AddSharedMemory(smem);
}
else
{
SharedMemoryId = -1;
}
}
public void SetVertexAsComputeLocalMemories(ShaderStage stage, InputTopology inputTopology)
{
LocalVertexDataMemoryId = AddMemoryDefinition("local_vertex_data", AggregateType.Array | AggregateType.FP32, Reservations.OutputSizePerInvocation);
if (stage == ShaderStage.Vertex)
{
LocalVertexIndexVertexRateMemoryId = AddMemoryDefinition("local_vertex_index_vr", AggregateType.U32);
LocalVertexIndexInstanceRateMemoryId = AddMemoryDefinition("local_vertex_index_ir", AggregateType.U32);
}
else if (stage == ShaderStage.Geometry)
{
LocalTopologyRemapMemoryId = AddMemoryDefinition("local_topology_remap", AggregateType.Array | AggregateType.U32, inputTopology.InputVertexCount);
LocalGeometryOutputVertexCountMemoryId = AddMemoryDefinition("local_geometry_output_vertex", AggregateType.U32);
LocalGeometryOutputIndexCountMemoryId = AddMemoryDefinition("local_geometry_output_index", AggregateType.U32);
}
}
private int AddMemoryDefinition(string name, AggregateType type, int arrayLength = 1)
{
return Properties.AddLocalMemory(new MemoryDefinition(name, type, arrayLength));
}
public int GetConstantBufferBinding(int slot)
{
SetBindingPair setAndBinding = _cbSlotToBindingMap[slot];
if (setAndBinding.Binding < 0)
{
setAndBinding = _gpuAccessor.CreateConstantBufferBinding(slot);
_cbSlotToBindingMap[slot] = setAndBinding;
string slotNumber = slot.ToString(CultureInfo.InvariantCulture);
AddNewConstantBuffer(setAndBinding.SetIndex, setAndBinding.Binding, $"{_stagePrefix}_c{slotNumber}");
}
return setAndBinding.Binding;
}
public bool TryGetStorageBufferBinding(int sbCbSlot, int sbCbOffset, bool write, out int binding)
{
if (!TryGetSbSlot((byte)sbCbSlot, (ushort)sbCbOffset, out int slot))
{
binding = 0;
return false;
}
SetBindingPair setAndBinding = _sbSlotToBindingMap[slot];
if (setAndBinding.Binding < 0)
{
setAndBinding = _gpuAccessor.CreateStorageBufferBinding(slot);
_sbSlotToBindingMap[slot] = setAndBinding;
string slotNumber = slot.ToString(CultureInfo.InvariantCulture);
AddNewStorageBuffer(setAndBinding.SetIndex, setAndBinding.Binding, $"{_stagePrefix}_s{slotNumber}");
}
if (write)
{
_sbSlotWritten |= 1u << slot;
}
binding = setAndBinding.Binding;
return true;
}
private bool TryGetSbSlot(byte sbCbSlot, ushort sbCbOffset, out int slot)
{
int key = PackSbCbInfo(sbCbSlot, sbCbOffset);
if (!_sbSlots.TryGetValue(key, out slot))
{
slot = _sbSlots.Count;
if (slot >= _sbSlotToBindingMap.Length)
{
return false;
}
_sbSlots.Add(key, slot);
_sbSlotsReverse.Add(slot, key);
}
return true;
}
public bool TryGetConstantBufferSlot(int binding, out int slot)
{
for (slot = 0; slot < _cbSlotToBindingMap.Length; slot++)
{
if (_cbSlotToBindingMap[slot].Binding == binding)
{
return true;
}
}
slot = 0;
return false;
}
public SetBindingPair GetTextureOrImageBinding(
Instruction inst,
SamplerType type,
TextureFormat format,
TextureFlags flags,
int cbufSlot,
int handle,
int arrayLength = 1,
bool separate = false)
{
inst &= Instruction.Mask;
bool isImage = inst.IsImage();
bool isWrite = inst.IsImageStore();
bool accurateType = !inst.IsTextureQuery();
bool intCoords = isImage || flags.HasFlag(TextureFlags.IntCoords) || inst == Instruction.TextureQuerySize;
bool coherent = flags.HasFlag(TextureFlags.Coherent);
if (!isImage)
{
format = TextureFormat.Unknown;
}
SetBindingPair setAndBinding = GetTextureOrImageBinding(
cbufSlot,
handle,
arrayLength,
type,
format,
isImage,
intCoords,
isWrite,
accurateType,
coherent,
separate);
_gpuAccessor.RegisterTexture(handle, cbufSlot);
return setAndBinding;
}
private SetBindingPair GetTextureOrImageBinding(
int cbufSlot,
int handle,
int arrayLength,
SamplerType type,
TextureFormat format,
bool isImage,
bool intCoords,
bool write,
bool accurateType,
bool coherent,
bool separate)
{
int dimensions = type == SamplerType.None ? 0 : type.Dimensions;
Dictionary<TextureInfo, TextureMeta> dict = isImage ? _usedImages : _usedTextures;
TextureUsageFlags usageFlags = TextureUsageFlags.None;
if (intCoords)
{
usageFlags |= TextureUsageFlags.NeedsScaleValue;
bool canScale = _stage.SupportsRenderScale && arrayLength == 1 && !write && dimensions == 2;
if (!canScale)
{
// Resolution scaling cannot be applied to this texture right now.
// Flag so that we know to blacklist scaling on related textures when binding them.
usageFlags |= TextureUsageFlags.ResScaleUnsupported;
}
}
if (write)
{
usageFlags |= TextureUsageFlags.ImageStore;
}
if (coherent)
{
usageFlags |= TextureUsageFlags.ImageCoherent;
}
// For array textures, we also want to use type as key,
// since we may have texture handles stores in the same buffer, but for textures with different types.
SamplerType keyType = arrayLength > 1 ? type : SamplerType.None;
TextureInfo info = new TextureInfo(cbufSlot, handle, arrayLength, separate, keyType, format);
TextureMeta meta = new TextureMeta()
{
AccurateType = accurateType,
Type = type,
UsageFlags = usageFlags,
};
int setIndex;
int binding;
if (dict.TryGetValue(info, out TextureMeta existingMeta))
{
dict[info] = MergeTextureMeta(meta, existingMeta);
setIndex = existingMeta.Set;
binding = existingMeta.Binding;
}
else
{
if (arrayLength > 1 && (setIndex = _gpuAccessor.CreateExtraSet()) >= 0)
{
// We reserved an "extra set" for the array.
// In this case the binding is always the first one (0).
// Using separate sets for array is better as we need to do less descriptor set updates.
binding = 0;
}
else
{
bool isBuffer = (type & SamplerType.Mask) == SamplerType.TextureBuffer;
SetBindingPair setAndBinding = isImage
? _gpuAccessor.CreateImageBinding(arrayLength, isBuffer)
: _gpuAccessor.CreateTextureBinding(arrayLength, isBuffer);
setIndex = setAndBinding.SetIndex;
binding = setAndBinding.Binding;
}
meta.Set = setIndex;
meta.Binding = binding;
dict.Add(info, meta);
}
string nameSuffix;
string prefix = isImage ? "i" : "t";
if (arrayLength != 1 && type != SamplerType.None)
{
prefix += type.ShortTypeName;
}
if (isImage)
{
nameSuffix = cbufSlot < 0
? $"{prefix}_tcb_{handle:X}_{format.ToGlslFormat()}"
: $"{prefix}_cb{cbufSlot}_{handle:X}_{format.ToGlslFormat()}";
}
else if (type == SamplerType.None)
{
nameSuffix = cbufSlot < 0 ? $"s_tcb_{handle:X}" : $"s_cb{cbufSlot}_{handle:X}";
}
else
{
nameSuffix = cbufSlot < 0 ? $"{prefix}_tcb_{handle:X}" : $"{prefix}_cb{cbufSlot}_{handle:X}";
}
TextureDefinition definition = new TextureDefinition(
setIndex,
binding,
arrayLength,
separate,
$"{_stagePrefix}_{nameSuffix}",
meta.Type,
info.Format,
meta.UsageFlags);
if (isImage)
{
Properties.AddOrUpdateImage(definition);
}
else
{
Properties.AddOrUpdateTexture(definition);
}
return new SetBindingPair(setIndex, binding);
}
private static TextureMeta MergeTextureMeta(TextureMeta meta, TextureMeta existingMeta)
{
meta.Set = existingMeta.Set;
meta.Binding = existingMeta.Binding;
meta.UsageFlags |= existingMeta.UsageFlags;
// If the texture we have has inaccurate type information, then
// we prefer the most accurate one.
if (existingMeta.AccurateType)
{
meta.AccurateType = true;
meta.Type = existingMeta.Type;
}
return meta;
}
public void SetUsageFlagsForTextureQuery(int binding, SamplerType type)
{
TextureInfo selectedInfo = default;
TextureMeta selectedMeta = default;
bool found = false;
foreach ((TextureInfo info, TextureMeta meta) in _usedTextures)
{
if (meta.Binding == binding)
{
selectedInfo = info;
selectedMeta = meta;
found = true;
break;
}
}
if (found)
{
selectedMeta.UsageFlags |= TextureUsageFlags.NeedsScaleValue;
int dimensions = type.Dimensions;
bool canScale = _stage.SupportsRenderScale && selectedInfo.ArrayLength == 1 && dimensions == 2;
if (!canScale)
{
// Resolution scaling cannot be applied to this texture right now.
// Flag so that we know to blacklist scaling on related textures when binding them.
selectedMeta.UsageFlags |= TextureUsageFlags.ResScaleUnsupported;
}
_usedTextures[selectedInfo] = selectedMeta;
}
}
public void SetUsedConstantBufferBinding(int binding)
{
_usedConstantBufferBindings.Add(binding);
}
public BufferDescriptor[] GetConstantBufferDescriptors()
{
BufferDescriptor[] descriptors = new BufferDescriptor[_usedConstantBufferBindings.Count];
int descriptorIndex = 0;
for (int slot = 0; slot < _cbSlotToBindingMap.Length; slot++)
{
SetBindingPair setAndBinding = _cbSlotToBindingMap[slot];
if (setAndBinding.Binding >= 0 && _usedConstantBufferBindings.Contains(setAndBinding.Binding))
{
descriptors[descriptorIndex++] = new BufferDescriptor(setAndBinding.SetIndex, setAndBinding.Binding, slot);
}
}
if (descriptors.Length != descriptorIndex)
{
Array.Resize(ref descriptors, descriptorIndex);
}
return descriptors;
}
public BufferDescriptor[] GetStorageBufferDescriptors()
{
BufferDescriptor[] descriptors = new BufferDescriptor[_sbSlots.Count];
int descriptorIndex = 0;
foreach ((int key, int slot) in _sbSlots)
{
SetBindingPair setAndBinding = _sbSlotToBindingMap[slot];
if (setAndBinding.Binding >= 0)
{
(int sbCbSlot, int sbCbOffset) = UnpackSbCbInfo(key);
BufferUsageFlags flags = (_sbSlotWritten & (1u << slot)) != 0 ? BufferUsageFlags.Write : BufferUsageFlags.None;
descriptors[descriptorIndex++] = new BufferDescriptor(setAndBinding.SetIndex, setAndBinding.Binding, slot, sbCbSlot, sbCbOffset, flags);
}
}
if (descriptors.Length != descriptorIndex)
{
Array.Resize(ref descriptors, descriptorIndex);
}
return descriptors;
}
public TextureDescriptor[] GetTextureDescriptors(bool includeArrays = true)
{
return GetDescriptors(_usedTextures, includeArrays);
}
public TextureDescriptor[] GetImageDescriptors(bool includeArrays = true)
{
return GetDescriptors(_usedImages, includeArrays);
}
private static TextureDescriptor[] GetDescriptors(IReadOnlyDictionary<TextureInfo, TextureMeta> usedResources, bool includeArrays)
{
List<TextureDescriptor> descriptors = new();
bool hasAnyArray = false;
foreach ((TextureInfo info, TextureMeta meta) in usedResources)
{
if (info.ArrayLength > 1)
{
hasAnyArray = true;
continue;
}
descriptors.Add(new TextureDescriptor(
meta.Set,
meta.Binding,
meta.Type,
info.Format,
info.CbufSlot,
info.Handle,
info.ArrayLength,
info.Separate,
meta.UsageFlags));
}
if (hasAnyArray && includeArrays)
{
foreach ((TextureInfo info, TextureMeta meta) in usedResources)
{
if (info.ArrayLength <= 1)
{
continue;
}
descriptors.Add(new TextureDescriptor(
meta.Set,
meta.Binding,
meta.Type,
info.Format,
info.CbufSlot,
info.Handle,
info.ArrayLength,
info.Separate,
meta.UsageFlags));
}
}
return descriptors.ToArray();
}
public bool TryGetCbufSlotAndHandleForTexture(int binding, out int cbufSlot, out int handle)
{
foreach ((TextureInfo info, TextureMeta meta) in _usedTextures)
{
if (meta.Binding == binding)
{
cbufSlot = info.CbufSlot;
handle = info.Handle;
return true;
}
}
cbufSlot = 0;
handle = 0;
return false;
}
private static int FindDescriptorIndex(TextureDescriptor[] array, int binding)
{
return Array.FindIndex(array, x => x.Binding == binding);
}
public int FindTextureDescriptorIndex(int binding)
{
return FindDescriptorIndex(GetTextureDescriptors(), binding);
}
public int FindImageDescriptorIndex(int binding)
{
return FindDescriptorIndex(GetImageDescriptors(), binding);
}
public bool IsArrayOfTexturesOrImages(int binding, bool isImage)
{
foreach ((TextureInfo info, TextureMeta meta) in isImage ? _usedImages : _usedTextures)
{
if (meta.Binding == binding)
{
return info.ArrayLength != 1;
}
}
return false;
}
private void AddNewConstantBuffer(int setIndex, int binding, string name)
{
StructureType type = new(new[]
{
new StructureField(AggregateType.Array | AggregateType.Vector4 | AggregateType.FP32, "data", Constants.ConstantBufferSize / 16),
});
Properties.AddOrUpdateConstantBuffer(new(BufferLayout.Std140, setIndex, binding, name, type));
}
private void AddNewStorageBuffer(int setIndex, int binding, string name)
{
StructureType type = new(new[]
{
new StructureField(AggregateType.Array | AggregateType.U32, "data", 0),
});
Properties.AddOrUpdateStorageBuffer(new(BufferLayout.Std430, setIndex, binding, name, type));
}
public static string GetShaderStagePrefix(ShaderStage stage)
{
uint index = (uint)stage;
return index >= _stagePrefixes.Length ? "invalid" : _stagePrefixes[index];
}
private static int PackSbCbInfo(int sbCbSlot, int sbCbOffset)
{
return sbCbOffset | (sbCbSlot << 16);
}
private static (int, int) UnpackSbCbInfo(int key)
{
return ((byte)(key >> 16), (ushort)key);
}
}
}

View File

@@ -0,0 +1,203 @@
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
using Ryujinx.Graphics.Shader.StructuredIr;
using System.Collections.Generic;
using System.Numerics;
namespace Ryujinx.Graphics.Shader.Translation
{
public class ResourceReservations
{
public const int TfeBuffersCount = 4;
public const int MaxVertexBufferTextures = 32;
private const int TextureSetIndex = 2; // TODO: Get from GPU accessor.
public int VertexInfoConstantBufferBinding { get; }
public int VertexOutputStorageBufferBinding { get; }
public int GeometryVertexOutputStorageBufferBinding { get; }
public int GeometryIndexOutputStorageBufferBinding { get; }
public int IndexBufferTextureBinding { get; }
public int TopologyRemapBufferTextureBinding { get; }
public int ReservedConstantBuffers { get; }
public int ReservedStorageBuffers { get; }
public int ReservedTextures { get; }
public int ReservedImages { get; }
public int InputSizePerInvocation { get; }
public int OutputSizePerInvocation { get; }
public int OutputSizeInBytesPerInvocation => OutputSizePerInvocation * sizeof(uint);
private readonly int _tfeBufferSbBaseBinding;
private readonly int _vertexBufferTextureBaseBinding;
private readonly Dictionary<IoDefinition, int> _offsets;
internal IReadOnlyDictionary<IoDefinition, int> Offsets => _offsets;
internal ResourceReservations(bool isTransformFeedbackEmulated, bool vertexAsCompute)
{
// All stages reserves the first constant buffer binding for the support buffer.
ReservedConstantBuffers = 1;
ReservedStorageBuffers = 0;
ReservedTextures = 0;
ReservedImages = 0;
if (isTransformFeedbackEmulated)
{
// Transform feedback emulation currently always uses 4 storage buffers.
_tfeBufferSbBaseBinding = ReservedStorageBuffers;
ReservedStorageBuffers = TfeBuffersCount;
}
if (vertexAsCompute)
{
// One constant buffer reserved for vertex related state.
VertexInfoConstantBufferBinding = ReservedConstantBuffers++;
// One storage buffer for the output vertex data.
VertexOutputStorageBufferBinding = ReservedStorageBuffers++;
// One storage buffer for the output geometry vertex data.
GeometryVertexOutputStorageBufferBinding = ReservedStorageBuffers++;
// One storage buffer for the output geometry index data.
GeometryIndexOutputStorageBufferBinding = ReservedStorageBuffers++;
// Enough textures reserved for all vertex attributes, plus the index buffer.
IndexBufferTextureBinding = ReservedTextures;
TopologyRemapBufferTextureBinding = ReservedTextures + 1;
_vertexBufferTextureBaseBinding = ReservedTextures + 2;
ReservedTextures += 2 + MaxVertexBufferTextures;
}
}
internal ResourceReservations(
IGpuAccessor gpuAccessor,
bool isTransformFeedbackEmulated,
bool vertexAsCompute,
IoUsage? vacInput,
IoUsage vacOutput) : this(isTransformFeedbackEmulated, vertexAsCompute)
{
if (vertexAsCompute)
{
_offsets = new();
if (vacInput.HasValue)
{
InputSizePerInvocation = FillIoOffsetMap(gpuAccessor, StorageKind.Input, vacInput.Value);
}
OutputSizePerInvocation = FillIoOffsetMap(gpuAccessor, StorageKind.Output, vacOutput);
}
}
private int FillIoOffsetMap(IGpuAccessor gpuAccessor, StorageKind storageKind, IoUsage vacUsage)
{
int offset = 0;
for (int c = 0; c < 4; c++)
{
_offsets.Add(new IoDefinition(storageKind, IoVariable.Position, 0, c), offset++);
}
_offsets.Add(new IoDefinition(storageKind, IoVariable.PointSize), offset++);
int clipDistancesWrittenMap = vacUsage.ClipDistancesWritten;
while (clipDistancesWrittenMap != 0)
{
int index = BitOperations.TrailingZeroCount(clipDistancesWrittenMap);
_offsets.Add(new IoDefinition(storageKind, IoVariable.ClipDistance, 0, index), offset++);
clipDistancesWrittenMap &= ~(1 << index);
}
if (vacUsage.UsesRtLayer)
{
_offsets.Add(new IoDefinition(storageKind, IoVariable.Layer), offset++);
}
if (vacUsage.UsesViewportIndex && gpuAccessor.QueryHostSupportsViewportIndexVertexTessellation())
{
_offsets.Add(new IoDefinition(storageKind, IoVariable.VertexIndex), offset++);
}
if (vacUsage.UsesViewportMask && gpuAccessor.QueryHostSupportsViewportMask())
{
_offsets.Add(new IoDefinition(storageKind, IoVariable.ViewportMask), offset++);
}
int usedDefinedMap = vacUsage.UserDefinedMap;
while (usedDefinedMap != 0)
{
int location = BitOperations.TrailingZeroCount(usedDefinedMap);
for (int c = 0; c < 4; c++)
{
_offsets.Add(new IoDefinition(storageKind, IoVariable.UserDefined, location, c), offset++);
}
usedDefinedMap &= ~(1 << location);
}
return offset;
}
internal static bool IsVectorOrArrayVariable(IoVariable variable)
{
return variable switch
{
IoVariable.ClipDistance or
IoVariable.Position => true,
_ => false,
};
}
public int GetTfeBufferStorageBufferBinding(int bufferIndex)
{
return _tfeBufferSbBaseBinding + bufferIndex;
}
public int GetVertexBufferTextureBinding(int vaLocation)
{
return _vertexBufferTextureBaseBinding + vaLocation;
}
public SetBindingPair GetVertexBufferTextureSetAndBinding(int vaLocation)
{
return new SetBindingPair(TextureSetIndex, GetVertexBufferTextureBinding(vaLocation));
}
public SetBindingPair GetIndexBufferTextureSetAndBinding()
{
return new SetBindingPair(TextureSetIndex, IndexBufferTextureBinding);
}
public SetBindingPair GetTopologyRemapBufferTextureSetAndBinding()
{
return new SetBindingPair(TextureSetIndex, TopologyRemapBufferTextureBinding);
}
internal bool TryGetOffset(StorageKind storageKind, int location, int component, out int offset)
{
return _offsets.TryGetValue(new IoDefinition(storageKind, IoVariable.UserDefined, location, component), out offset);
}
internal bool TryGetOffset(StorageKind storageKind, IoVariable ioVariable, int location, int component, out int offset)
{
return _offsets.TryGetValue(new IoDefinition(storageKind, ioVariable, location, component), out offset);
}
internal bool TryGetOffset(StorageKind storageKind, IoVariable ioVariable, int component, out int offset)
{
return _offsets.TryGetValue(new IoDefinition(storageKind, ioVariable, 0, component), out offset);
}
internal bool TryGetOffset(StorageKind storageKind, IoVariable ioVariable, out int offset)
{
return _offsets.TryGetValue(new IoDefinition(storageKind, ioVariable, 0, 0), out offset);
}
}
}

View File

@@ -0,0 +1,355 @@
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
using System;
using System.Collections.Generic;
using System.Numerics;
namespace Ryujinx.Graphics.Shader.Translation
{
class ShaderDefinitions
{
private readonly GpuGraphicsState _graphicsState;
public ShaderStage Stage { get; }
public int ComputeLocalSizeX { get; }
public int ComputeLocalSizeY { get; }
public int ComputeLocalSizeZ { get; }
public bool TessCw => _graphicsState.TessCw;
public TessPatchType TessPatchType => _graphicsState.TessPatchType;
public TessSpacing TessSpacing => _graphicsState.TessSpacing;
public bool AlphaToCoverageDitherEnable => _graphicsState.AlphaToCoverageEnable && _graphicsState.AlphaToCoverageDitherEnable;
public bool ViewportTransformDisable => _graphicsState.ViewportTransformDisable;
public bool DepthMode => _graphicsState.DepthMode;
public float PointSize => _graphicsState.PointSize;
public AlphaTestOp AlphaTestCompare => _graphicsState.AlphaTestCompare;
public float AlphaTestReference => _graphicsState.AlphaTestReference;
public bool GpPassthrough { get; }
public bool LastInVertexPipeline { get; set; }
public int ThreadsPerInputPrimitive { get; private set; }
public InputTopology InputTopology => _graphicsState.Topology;
public OutputTopology OutputTopology { get; }
public int MaxOutputVertices { get; }
public bool DualSourceBlend => _graphicsState.DualSourceBlendEnable;
public bool EarlyZForce => _graphicsState.EarlyZForce;
public bool YNegateEnabled => _graphicsState.YNegateEnabled;
public bool OriginUpperLeft => _graphicsState.OriginUpperLeft;
public bool HalvePrimitiveId => _graphicsState.HalvePrimitiveId;
public ImapPixelType[] ImapTypes { get; }
public bool IaIndexing { get; private set; }
public bool OaIndexing { get; private set; }
public int OmapTargets { get; }
public bool OmapSampleMask { get; }
public bool OmapDepth { get; }
public bool SupportsScaledVertexFormats { get; }
public bool TransformFeedbackEnabled { get; }
private readonly TransformFeedbackOutput[] _transformFeedbackOutputs;
readonly struct TransformFeedbackVariable : IEquatable<TransformFeedbackVariable>
{
public IoVariable IoVariable { get; }
public int Location { get; }
public int Component { get; }
public TransformFeedbackVariable(IoVariable ioVariable, int location = 0, int component = 0)
{
IoVariable = ioVariable;
Location = location;
Component = component;
}
public override bool Equals(object other)
{
return other is TransformFeedbackVariable tfbVar && Equals(tfbVar);
}
public bool Equals(TransformFeedbackVariable other)
{
return IoVariable == other.IoVariable &&
Location == other.Location &&
Component == other.Component;
}
public override int GetHashCode()
{
return (int)IoVariable | (Location << 8) | (Component << 16);
}
public override string ToString()
{
return $"{IoVariable}.{Location}.{Component}";
}
}
private readonly Dictionary<TransformFeedbackVariable, TransformFeedbackOutput> _transformFeedbackDefinitions;
public ShaderDefinitions(ShaderStage stage, ulong transformFeedbackVecMap, TransformFeedbackOutput[] transformFeedbackOutputs)
{
Stage = stage;
TransformFeedbackEnabled = transformFeedbackOutputs != null;
_transformFeedbackOutputs = transformFeedbackOutputs;
_transformFeedbackDefinitions = new();
PopulateTransformFeedbackDefinitions(transformFeedbackVecMap, transformFeedbackOutputs);
}
public ShaderDefinitions(
ShaderStage stage,
int computeLocalSizeX,
int computeLocalSizeY,
int computeLocalSizeZ)
{
Stage = stage;
ComputeLocalSizeX = computeLocalSizeX;
ComputeLocalSizeY = computeLocalSizeY;
ComputeLocalSizeZ = computeLocalSizeZ;
}
public ShaderDefinitions(
ShaderStage stage,
GpuGraphicsState graphicsState,
bool gpPassthrough,
int threadsPerInputPrimitive,
OutputTopology outputTopology,
int maxOutputVertices)
{
Stage = stage;
_graphicsState = graphicsState;
GpPassthrough = gpPassthrough;
ThreadsPerInputPrimitive = threadsPerInputPrimitive;
OutputTopology = outputTopology;
MaxOutputVertices = maxOutputVertices;
}
public ShaderDefinitions(
ShaderStage stage,
GpuGraphicsState graphicsState,
bool gpPassthrough,
int threadsPerInputPrimitive,
OutputTopology outputTopology,
int maxOutputVertices,
ImapPixelType[] imapTypes,
int omapTargets,
bool omapSampleMask,
bool omapDepth,
bool supportsScaledVertexFormats,
ulong transformFeedbackVecMap,
TransformFeedbackOutput[] transformFeedbackOutputs)
{
Stage = stage;
_graphicsState = graphicsState;
GpPassthrough = gpPassthrough;
ThreadsPerInputPrimitive = threadsPerInputPrimitive;
OutputTopology = outputTopology;
MaxOutputVertices = gpPassthrough ? graphicsState.Topology.InputVertexCountNoAdjacency : maxOutputVertices;
ImapTypes = imapTypes;
OmapTargets = omapTargets;
OmapSampleMask = omapSampleMask;
OmapDepth = omapDepth;
LastInVertexPipeline = stage < ShaderStage.Fragment;
SupportsScaledVertexFormats = supportsScaledVertexFormats;
TransformFeedbackEnabled = transformFeedbackOutputs != null;
_transformFeedbackOutputs = transformFeedbackOutputs;
_transformFeedbackDefinitions = new();
PopulateTransformFeedbackDefinitions(transformFeedbackVecMap, transformFeedbackOutputs);
}
private void PopulateTransformFeedbackDefinitions(ulong transformFeedbackVecMap, TransformFeedbackOutput[] transformFeedbackOutputs)
{
while (transformFeedbackVecMap != 0)
{
int vecIndex = BitOperations.TrailingZeroCount(transformFeedbackVecMap);
for (int subIndex = 0; subIndex < 4; subIndex++)
{
int wordOffset = vecIndex * 4 + subIndex;
int byteOffset = wordOffset * 4;
if (transformFeedbackOutputs[wordOffset].Valid)
{
IoVariable ioVariable = Instructions.AttributeMap.GetIoVariable(this, byteOffset, out int location);
int component = 0;
if (HasPerLocationInputOrOutputComponent(ioVariable, location, subIndex, isOutput: true))
{
component = subIndex;
}
TransformFeedbackVariable transformFeedbackVariable = new(ioVariable, location, component);
_transformFeedbackDefinitions.TryAdd(transformFeedbackVariable, transformFeedbackOutputs[wordOffset]);
}
}
transformFeedbackVecMap &= ~(1UL << vecIndex);
}
}
public void EnableInputIndexing()
{
IaIndexing = true;
}
public void EnableOutputIndexing()
{
OaIndexing = true;
}
public bool TryGetTransformFeedbackOutput(IoVariable ioVariable, int location, int component, out TransformFeedbackOutput transformFeedbackOutput)
{
if (!HasTransformFeedbackOutputs())
{
transformFeedbackOutput = default;
return false;
}
TransformFeedbackVariable transformFeedbackVariable = new(ioVariable, location, component);
return _transformFeedbackDefinitions.TryGetValue(transformFeedbackVariable, out transformFeedbackOutput);
}
private bool HasTransformFeedbackOutputs()
{
return TransformFeedbackEnabled && (LastInVertexPipeline || Stage == ShaderStage.Fragment);
}
public bool HasTransformFeedbackOutputs(bool isOutput)
{
return TransformFeedbackEnabled && ((isOutput && LastInVertexPipeline) || (!isOutput && Stage == ShaderStage.Fragment));
}
public bool HasPerLocationInputOrOutput(IoVariable ioVariable, bool isOutput)
{
if (ioVariable == IoVariable.UserDefined)
{
return (!isOutput && !IaIndexing) || (isOutput && !OaIndexing);
}
return ioVariable == IoVariable.FragmentOutputColor;
}
public bool HasPerLocationInputOrOutputComponent(IoVariable ioVariable, int location, int component, bool isOutput)
{
if (ioVariable != IoVariable.UserDefined || !HasTransformFeedbackOutputs(isOutput))
{
return false;
}
return GetTransformFeedbackOutputComponents(location, component) == 1;
}
public TransformFeedbackOutput GetTransformFeedbackOutput(int wordOffset)
{
return _transformFeedbackOutputs[wordOffset];
}
public TransformFeedbackOutput GetTransformFeedbackOutput(int location, int component)
{
return GetTransformFeedbackOutput((AttributeConsts.UserAttributeBase / 4) + location * 4 + component);
}
public int GetTransformFeedbackOutputComponents(int location, int component)
{
int baseIndex = (AttributeConsts.UserAttributeBase / 4) + location * 4;
int index = baseIndex + component;
int count = 1;
for (; count < 4; count++)
{
ref TransformFeedbackOutput prev = ref _transformFeedbackOutputs[baseIndex + count - 1];
ref TransformFeedbackOutput curr = ref _transformFeedbackOutputs[baseIndex + count];
int prevOffset = prev.Offset;
int currOffset = curr.Offset;
if (!prev.Valid || !curr.Valid || prevOffset + 4 != currOffset)
{
break;
}
}
if (baseIndex + count <= index)
{
return 1;
}
return count;
}
public AggregateType GetFragmentOutputColorType(int location)
{
return AggregateType.Vector4 | _graphicsState.FragmentOutputTypes[location].Aggregate;
}
public AggregateType GetUserDefinedType(int location, bool isOutput)
{
if ((!isOutput && IaIndexing) || (isOutput && OaIndexing))
{
return AggregateType.Array | AggregateType.Vector4 | AggregateType.FP32;
}
AggregateType type = AggregateType.Vector4;
if (Stage == ShaderStage.Vertex && !isOutput)
{
type |= _graphicsState.AttributeTypes[location].AsAggregate(SupportsScaledVertexFormats);
}
else
{
type |= AggregateType.FP32;
}
return type;
}
public AttributeType GetAttributeType(int location)
{
return _graphicsState.AttributeTypes[location];
}
public bool IsAttributeSint(int location)
{
return (_graphicsState.AttributeTypes[location] & ~AttributeType.AnyPacked) == AttributeType.Sint;
}
public bool IsAttributePacked(int location)
{
return _graphicsState.AttributeTypes[location].HasFlag(AttributeType.Packed);
}
public bool IsAttributePackedRgb10A2Signed(int location)
{
return _graphicsState.AttributeTypes[location].HasFlag(AttributeType.PackedRgb10A2Signed);
}
public int GetGeometryOutputIndexBufferStridePerInstance()
{
return MaxOutputVertices + OutputTopology switch
{
OutputTopology.LineStrip => MaxOutputVertices / 2,
OutputTopology.TriangleStrip => MaxOutputVertices / 3,
_ => MaxOutputVertices,
};
}
public int GetGeometryOutputIndexBufferStride()
{
return GetGeometryOutputIndexBufferStridePerInstance() * ThreadsPerInputPrimitive;
}
}
}

View File

@@ -0,0 +1,170 @@
using Ryujinx.Common.Utilities;
using System;
using System.Runtime.InteropServices;
namespace Ryujinx.Graphics.Shader.Translation
{
enum PixelImap
{
Unused = 0,
Constant = 1,
Perspective = 2,
ScreenLinear = 3,
}
readonly struct ImapPixelType
{
public PixelImap X { get; }
public PixelImap Y { get; }
public PixelImap Z { get; }
public PixelImap W { get; }
public ImapPixelType(PixelImap x, PixelImap y, PixelImap z, PixelImap w)
{
X = x;
Y = y;
Z = z;
W = w;
}
public PixelImap GetFirstUsedType()
{
if (X != PixelImap.Unused)
{
return X;
}
if (Y != PixelImap.Unused)
{
return Y;
}
if (Z != PixelImap.Unused)
{
return Z;
}
return W;
}
}
class ShaderHeader
{
public int SphType { get; }
public int Version { get; }
public ShaderStage Stage { get; }
public bool MrtEnable { get; }
public bool KillsPixels { get; }
public bool DoesGlobalStore { get; }
public int SassVersion { get; }
public bool GpPassthrough { get; }
public bool DoesLoadOrStore { get; }
public bool DoesFp64 { get; }
public int StreamOutMask { get; }
public int ShaderLocalMemoryLowSize { get; }
public int PerPatchAttributeCount { get; }
public int ShaderLocalMemoryHighSize { get; }
public int ThreadsPerInputPrimitive { get; }
public int ShaderLocalMemoryCrsSize { get; }
public OutputTopology OutputTopology { get; }
public int MaxOutputVertexCount { get; }
public int StoreReqStart { get; }
public int StoreReqEnd { get; }
public ImapPixelType[] ImapTypes { get; }
public int OmapTargets { get; }
public bool OmapSampleMask { get; }
public bool OmapDepth { get; }
public ShaderHeader(IGpuAccessor gpuAccessor, ulong address)
{
ReadOnlySpan<int> header = MemoryMarshal.Cast<ulong, int>(gpuAccessor.GetCode(address, 0x50));
int commonWord0 = header[0];
int commonWord1 = header[1];
int commonWord2 = header[2];
int commonWord3 = header[3];
int commonWord4 = header[4];
SphType = commonWord0.Extract(0, 5);
Version = commonWord0.Extract(5, 5);
Stage = (ShaderStage)commonWord0.Extract(10, 4);
// Invalid.
if (Stage == ShaderStage.Compute)
{
Stage = ShaderStage.Vertex;
}
MrtEnable = commonWord0.Extract(14);
KillsPixels = commonWord0.Extract(15);
DoesGlobalStore = commonWord0.Extract(16);
SassVersion = commonWord0.Extract(17, 4);
GpPassthrough = commonWord0.Extract(24);
DoesLoadOrStore = commonWord0.Extract(26);
DoesFp64 = commonWord0.Extract(27);
StreamOutMask = commonWord0.Extract(28, 4);
ShaderLocalMemoryLowSize = commonWord1.Extract(0, 24);
PerPatchAttributeCount = commonWord1.Extract(24, 8);
ShaderLocalMemoryHighSize = commonWord2.Extract(0, 24);
ThreadsPerInputPrimitive = commonWord2.Extract(24, 8);
ShaderLocalMemoryCrsSize = commonWord3.Extract(0, 24);
OutputTopology = (OutputTopology)commonWord3.Extract(24, 4);
MaxOutputVertexCount = commonWord4.Extract(0, 12);
StoreReqStart = commonWord4.Extract(12, 8);
StoreReqEnd = commonWord4.Extract(24, 8);
ImapTypes = new ImapPixelType[32];
for (int i = 0; i < 32; i++)
{
byte imap = (byte)(header[6 + (i >> 2)] >> ((i & 3) * 8));
ImapTypes[i] = new ImapPixelType(
(PixelImap)((imap >> 0) & 3),
(PixelImap)((imap >> 2) & 3),
(PixelImap)((imap >> 4) & 3),
(PixelImap)((imap >> 6) & 3));
}
int type2OmapTarget = header[18];
int type2Omap = header[19];
OmapTargets = type2OmapTarget;
OmapSampleMask = type2Omap.Extract(0);
OmapDepth = type2Omap.Extract(1);
}
}
}

View File

@@ -0,0 +1,375 @@
using Ryujinx.Graphics.Shader.Decoders;
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
using System.Collections.Generic;
using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
namespace Ryujinx.Graphics.Shader.Translation
{
static class Ssa
{
private const int GprsAndPredsCount = RegisterConsts.GprsCount + RegisterConsts.PredsCount;
private class DefMap
{
private readonly Dictionary<Register, Operand> _map;
private readonly long[] _phiMasks;
public DefMap()
{
_map = new Dictionary<Register, Operand>();
_phiMasks = new long[(RegisterConsts.TotalCount + 63) / 64];
}
public bool TryAddOperand(Register reg, Operand operand)
{
return _map.TryAdd(reg, operand);
}
public bool TryGetOperand(Register reg, out Operand operand)
{
return _map.TryGetValue(reg, out operand);
}
public bool AddPhi(Register reg)
{
int key = GetKeyFromRegister(reg);
int index = key / 64;
int bit = key & 63;
long mask = 1L << bit;
if ((_phiMasks[index] & mask) != 0)
{
return false;
}
_phiMasks[index] |= mask;
return true;
}
public bool HasPhi(Register reg)
{
int key = GetKeyFromRegister(reg);
int index = key / 64;
int bit = key & 63;
return (_phiMasks[index] & (1L << bit)) != 0;
}
}
private class LocalDefMap
{
private readonly Operand[] _map;
private readonly int[] _uses;
public int UseCount { get; private set; }
public LocalDefMap()
{
_map = new Operand[RegisterConsts.TotalCount];
_uses = new int[RegisterConsts.TotalCount];
}
public Operand Get(int key)
{
return _map[key];
}
public void Add(int key, Operand operand)
{
if (_map[key] == null)
{
_uses[UseCount++] = key;
}
_map[key] = operand;
}
public Operand GetUse(int index, out int key)
{
key = _uses[index];
return _map[key];
}
public void Clear()
{
for (int i = 0; i < UseCount; i++)
{
_map[_uses[i]] = null;
}
UseCount = 0;
}
}
private readonly struct Definition
{
public BasicBlock Block { get; }
public Operand Local { get; }
public Definition(BasicBlock block, Operand local)
{
Block = block;
Local = local;
}
}
public static void Rename(BasicBlock[] blocks)
{
DefMap[] globalDefs = new DefMap[blocks.Length];
LocalDefMap localDefs = new();
for (int blkIndex = 0; blkIndex < blocks.Length; blkIndex++)
{
globalDefs[blkIndex] = new DefMap();
}
Queue<BasicBlock> dfPhiBlocks = new();
// First pass, get all defs and locals uses.
for (int blkIndex = 0; blkIndex < blocks.Length; blkIndex++)
{
Operand RenameLocal(Operand operand)
{
if (operand != null && operand.Type == OperandType.Register)
{
Operand local = localDefs.Get(GetKeyFromRegister(operand.GetRegister()));
operand = local ?? operand;
}
return operand;
}
BasicBlock block = blocks[blkIndex];
LinkedListNode<INode> node = block.Operations.First;
while (node != null)
{
if (node.Value is Operation operation)
{
for (int index = 0; index < operation.SourcesCount; index++)
{
operation.SetSource(index, RenameLocal(operation.GetSource(index)));
}
for (int index = 0; index < operation.DestsCount; index++)
{
Operand dest = operation.GetDest(index);
if (dest != null && dest.Type == OperandType.Register)
{
Operand local = Local();
localDefs.Add(GetKeyFromRegister(dest.GetRegister()), local);
operation.SetDest(index, local);
}
}
}
node = node.Next;
}
int localUses = localDefs.UseCount;
for (int index = 0; index < localUses; index++)
{
Operand local = localDefs.GetUse(index, out int key);
Register reg = GetRegisterFromKey(key);
globalDefs[block.Index].TryAddOperand(reg, local);
dfPhiBlocks.Enqueue(block);
while (dfPhiBlocks.TryDequeue(out BasicBlock dfPhiBlock))
{
foreach (BasicBlock domFrontier in dfPhiBlock.DominanceFrontiers)
{
if (globalDefs[domFrontier.Index].AddPhi(reg))
{
dfPhiBlocks.Enqueue(domFrontier);
}
}
}
}
localDefs.Clear();
}
// Second pass, rename variables with definitions on different blocks.
for (int blkIndex = 0; blkIndex < blocks.Length; blkIndex++)
{
BasicBlock block = blocks[blkIndex];
Operand RenameGlobal(Operand operand)
{
if (operand != null && operand.Type == OperandType.Register)
{
int key = GetKeyFromRegister(operand.GetRegister());
Operand local = localDefs.Get(key);
if (local != null)
{
return local;
}
operand = FindDefinitionForCurr(globalDefs, block, operand.GetRegister());
localDefs.Add(key, operand);
}
return operand;
}
for (LinkedListNode<INode> node = block.Operations.First; node != null; node = node.Next)
{
if (node.Value is Operation operation)
{
for (int index = 0; index < operation.SourcesCount; index++)
{
operation.SetSource(index, RenameGlobal(operation.GetSource(index)));
}
}
}
if (blkIndex < blocks.Length - 1)
{
localDefs.Clear();
}
}
}
private static Operand FindDefinitionForCurr(DefMap[] globalDefs, BasicBlock current, Register reg)
{
if (globalDefs[current.Index].HasPhi(reg))
{
return InsertPhi(globalDefs, current, reg);
}
if (current != current.ImmediateDominator)
{
return FindDefinition(globalDefs, current.ImmediateDominator, reg).Local;
}
return Undef();
}
private static Definition FindDefinition(DefMap[] globalDefs, BasicBlock current, Register reg)
{
foreach (BasicBlock block in SelfAndImmediateDominators(current))
{
DefMap defMap = globalDefs[block.Index];
if (defMap.TryGetOperand(reg, out Operand lastDef))
{
return new Definition(block, lastDef);
}
if (defMap.HasPhi(reg))
{
return new Definition(block, InsertPhi(globalDefs, block, reg));
}
}
return new Definition(current, Undef());
}
private static IEnumerable<BasicBlock> SelfAndImmediateDominators(BasicBlock block)
{
while (block != block.ImmediateDominator)
{
yield return block;
block = block.ImmediateDominator;
}
yield return block;
}
private static Operand InsertPhi(DefMap[] globalDefs, BasicBlock block, Register reg)
{
// This block has a Phi that has not been materialized yet, but that
// would define a new version of the variable we're looking for. We need
// to materialize the Phi, add all the block/operand pairs into the Phi, and
// then use the definition from that Phi.
Operand local = Local();
PhiNode phi = new(local);
AddPhi(block, phi);
globalDefs[block.Index].TryAddOperand(reg, local);
foreach (BasicBlock predecessor in block.Predecessors)
{
Definition def = FindDefinition(globalDefs, predecessor, reg);
phi.AddSource(def.Block, def.Local);
}
return local;
}
private static void AddPhi(BasicBlock block, PhiNode phi)
{
LinkedListNode<INode> node = block.Operations.First;
if (node != null)
{
while (node.Next?.Value is PhiNode)
{
node = node.Next;
}
}
if (node?.Value is PhiNode)
{
block.Operations.AddAfter(node, phi);
}
else
{
block.Operations.AddFirst(phi);
}
}
private static int GetKeyFromRegister(Register reg)
{
if (reg.Type == RegisterType.Gpr)
{
return reg.Index;
}
else if (reg.Type == RegisterType.Predicate)
{
return RegisterConsts.GprsCount + reg.Index;
}
else /* if (reg.Type == RegisterType.Flag) */
{
return GprsAndPredsCount + reg.Index;
}
}
private static Register GetRegisterFromKey(int key)
{
if (key < RegisterConsts.GprsCount)
{
return new Register(key, RegisterType.Gpr);
}
else if (key < GprsAndPredsCount)
{
return new Register(key - RegisterConsts.GprsCount, RegisterType.Predicate);
}
else /* if (key < RegisterConsts.TotalCount) */
{
return new Register(key - GprsAndPredsCount, RegisterType.Flag);
}
}
}
}

View File

@@ -0,0 +1,8 @@
namespace Ryujinx.Graphics.Shader.Translation
{
public enum TargetApi
{
OpenGL,
Vulkan,
}
}

View File

@@ -0,0 +1,9 @@
namespace Ryujinx.Graphics.Shader.Translation
{
public enum TargetLanguage
{
Glsl,
Spirv,
Arb,
}
}

View File

@@ -0,0 +1,39 @@
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
namespace Ryujinx.Graphics.Shader.Translation
{
readonly ref struct TransformContext
{
public readonly HelperFunctionManager Hfm;
public readonly BasicBlock[] Blocks;
public readonly ShaderDefinitions Definitions;
public readonly ResourceManager ResourceManager;
public readonly IGpuAccessor GpuAccessor;
public readonly TargetApi TargetApi;
public readonly TargetLanguage TargetLanguage;
public readonly ShaderStage Stage;
public readonly ref FeatureFlags UsedFeatures;
public TransformContext(
HelperFunctionManager hfm,
BasicBlock[] blocks,
ShaderDefinitions definitions,
ResourceManager resourceManager,
IGpuAccessor gpuAccessor,
TargetApi targetApi,
TargetLanguage targetLanguage,
ShaderStage stage,
ref FeatureFlags usedFeatures)
{
Hfm = hfm;
Blocks = blocks;
Definitions = definitions;
ResourceManager = resourceManager;
GpuAccessor = gpuAccessor;
TargetApi = targetApi;
TargetLanguage = targetLanguage;
Stage = stage;
UsedFeatures = ref usedFeatures;
}
}
}

View File

@@ -0,0 +1,18 @@
namespace Ryujinx.Graphics.Shader.Translation
{
readonly struct TransformFeedbackOutput
{
public readonly bool Valid;
public readonly int Buffer;
public readonly int Offset;
public readonly int Stride;
public TransformFeedbackOutput(int buffer, int offset, int stride)
{
Valid = true;
Buffer = buffer;
Offset = offset;
Stride = stride;
}
}
}

View File

@@ -0,0 +1,93 @@
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
using System.Collections.Generic;
using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
namespace Ryujinx.Graphics.Shader.Translation.Transforms
{
class DrawParametersReplace : ITransformPass
{
public static bool IsEnabled(IGpuAccessor gpuAccessor, ShaderStage stage, TargetLanguage targetLanguage, FeatureFlags usedFeatures)
{
return stage == ShaderStage.Vertex;
}
public static LinkedListNode<INode> RunPass(TransformContext context, LinkedListNode<INode> node)
{
Operation operation = (Operation)node.Value;
if (context.GpuAccessor.QueryHasConstantBufferDrawParameters())
{
if (ReplaceConstantBufferWithDrawParameters(node, operation))
{
context.UsedFeatures |= FeatureFlags.DrawParameters;
}
}
else if (HasConstantBufferDrawParameters(operation))
{
context.UsedFeatures |= FeatureFlags.DrawParameters;
}
return node;
}
private static bool ReplaceConstantBufferWithDrawParameters(LinkedListNode<INode> node, Operation operation)
{
Operand GenerateLoad(IoVariable ioVariable)
{
Operand value = Local();
node.List.AddBefore(node, new Operation(Instruction.Load, StorageKind.Input, value, Const((int)ioVariable)));
return value;
}
bool modified = false;
for (int srcIndex = 0; srcIndex < operation.SourcesCount; srcIndex++)
{
Operand src = operation.GetSource(srcIndex);
if (src.Type == OperandType.ConstantBuffer && src.GetCbufSlot() == 0)
{
switch (src.GetCbufOffset())
{
case Constants.NvnBaseVertexByteOffset / 4:
operation.SetSource(srcIndex, GenerateLoad(IoVariable.BaseVertex));
modified = true;
break;
case Constants.NvnBaseInstanceByteOffset / 4:
operation.SetSource(srcIndex, GenerateLoad(IoVariable.BaseInstance));
modified = true;
break;
case Constants.NvnDrawIndexByteOffset / 4:
operation.SetSource(srcIndex, GenerateLoad(IoVariable.DrawIndex));
modified = true;
break;
}
}
}
return modified;
}
private static bool HasConstantBufferDrawParameters(Operation operation)
{
for (int srcIndex = 0; srcIndex < operation.SourcesCount; srcIndex++)
{
Operand src = operation.GetSource(srcIndex);
if (src.Type == OperandType.ConstantBuffer && src.GetCbufSlot() == 0)
{
switch (src.GetCbufOffset())
{
case Constants.NvnBaseVertexByteOffset / 4:
case Constants.NvnBaseInstanceByteOffset / 4:
case Constants.NvnDrawIndexByteOffset / 4:
return true;
}
}
}
return false;
}
}
}

View File

@@ -0,0 +1,36 @@
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
using System.Collections.Generic;
namespace Ryujinx.Graphics.Shader.Translation.Transforms
{
class ForcePreciseEnable : ITransformPass
{
public static bool IsEnabled(IGpuAccessor gpuAccessor, ShaderStage stage, TargetLanguage targetLanguage, FeatureFlags usedFeatures)
{
return stage == ShaderStage.Fragment && gpuAccessor.QueryHostReducedPrecision();
}
public static LinkedListNode<INode> RunPass(TransformContext context, LinkedListNode<INode> node)
{
// There are some cases where a small bias is added to values to prevent division by zero.
// When operating with reduced precision, it is possible for this bias to get rounded to 0
// and cause a division by zero.
// To prevent that, we force those operations to be precise even if the host wants
// imprecise operations for performance.
Operation operation = (Operation)node.Value;
if (operation.Inst == (Instruction.FP32 | Instruction.Divide) &&
operation.GetSource(0).Type == OperandType.Constant &&
operation.GetSource(0).AsFloat() == 1f &&
operation.GetSource(1).AsgOp is Operation addOp &&
addOp.Inst == (Instruction.FP32 | Instruction.Add) &&
addOp.GetSource(1).Type == OperandType.Constant)
{
addOp.ForcePrecise = true;
}
return node;
}
}
}

View File

@@ -0,0 +1,380 @@
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
using Ryujinx.Graphics.Shader.Translation.Optimizations;
using System.Collections.Generic;
using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
namespace Ryujinx.Graphics.Shader.Translation.Transforms
{
class GeometryToCompute : ITransformPass
{
public static bool IsEnabled(IGpuAccessor gpuAccessor, ShaderStage stage, TargetLanguage targetLanguage, FeatureFlags usedFeatures)
{
return usedFeatures.HasFlag(FeatureFlags.VtgAsCompute);
}
public static LinkedListNode<INode> RunPass(TransformContext context, LinkedListNode<INode> node)
{
if (context.Definitions.Stage != ShaderStage.Geometry)
{
return node;
}
Operation operation = (Operation)node.Value;
LinkedListNode<INode> newNode = node;
switch (operation.Inst)
{
case Instruction.EmitVertex:
newNode = GenerateEmitVertex(context.Definitions, context.ResourceManager, node);
break;
case Instruction.EndPrimitive:
newNode = GenerateEndPrimitive(context.Definitions, context.ResourceManager, node);
break;
case Instruction.Load:
if (operation.StorageKind == StorageKind.Input)
{
IoVariable ioVariable = (IoVariable)operation.GetSource(0).Value;
if (TryGetOffset(context.ResourceManager, operation, StorageKind.Input, out int inputOffset))
{
Operand primVertex = ioVariable == IoVariable.UserDefined
? operation.GetSource(2)
: operation.GetSource(1);
Operand vertexElemOffset = GenerateVertexOffset(context.ResourceManager, node, inputOffset, primVertex);
newNode = node.List.AddBefore(node, new Operation(
Instruction.Load,
StorageKind.StorageBuffer,
operation.Dest,
new[] { Const(context.ResourceManager.Reservations.VertexOutputStorageBufferBinding), Const(0), vertexElemOffset }));
}
else
{
switch (ioVariable)
{
case IoVariable.InvocationId:
newNode = GenerateInvocationId(node, operation.Dest);
break;
case IoVariable.PrimitiveId:
newNode = GeneratePrimitiveId(context.ResourceManager, node, operation.Dest);
break;
case IoVariable.GlobalId:
case IoVariable.SubgroupEqMask:
case IoVariable.SubgroupGeMask:
case IoVariable.SubgroupGtMask:
case IoVariable.SubgroupLaneId:
case IoVariable.SubgroupLeMask:
case IoVariable.SubgroupLtMask:
// Those are valid or expected for geometry shaders.
break;
default:
context.GpuAccessor.Log($"Invalid input \"{ioVariable}\".");
break;
}
}
}
else if (operation.StorageKind == StorageKind.Output)
{
if (TryGetOffset(context.ResourceManager, operation, StorageKind.Output, out int outputOffset))
{
newNode = node.List.AddBefore(node, new Operation(
Instruction.Load,
StorageKind.LocalMemory,
operation.Dest,
new[] { Const(context.ResourceManager.LocalVertexDataMemoryId), Const(outputOffset) }));
}
else
{
context.GpuAccessor.Log($"Invalid output \"{(IoVariable)operation.GetSource(0).Value}\".");
}
}
break;
case Instruction.Store:
if (operation.StorageKind == StorageKind.Output)
{
if (TryGetOffset(context.ResourceManager, operation, StorageKind.Output, out int outputOffset))
{
Operand value = operation.GetSource(operation.SourcesCount - 1);
newNode = node.List.AddBefore(node, new Operation(
Instruction.Store,
StorageKind.LocalMemory,
(Operand)null,
new[] { Const(context.ResourceManager.LocalVertexDataMemoryId), Const(outputOffset), value }));
}
else
{
context.GpuAccessor.Log($"Invalid output \"{(IoVariable)operation.GetSource(0).Value}\".");
}
}
break;
}
if (newNode != node)
{
Utils.DeleteNode(node, operation);
}
return newNode;
}
private static LinkedListNode<INode> GenerateEmitVertex(ShaderDefinitions definitions, ResourceManager resourceManager, LinkedListNode<INode> node)
{
int vbOutputBinding = resourceManager.Reservations.GeometryVertexOutputStorageBufferBinding;
int ibOutputBinding = resourceManager.Reservations.GeometryIndexOutputStorageBufferBinding;
int stride = resourceManager.Reservations.OutputSizePerInvocation;
Operand outputPrimVertex = IncrementLocalMemory(node, resourceManager.LocalGeometryOutputVertexCountMemoryId);
Operand baseVertexOffset = GenerateBaseOffset(
resourceManager,
node,
definitions.MaxOutputVertices * definitions.ThreadsPerInputPrimitive,
definitions.ThreadsPerInputPrimitive);
Operand outputBaseVertex = Local();
node.List.AddBefore(node, new Operation(Instruction.Add, outputBaseVertex, new[] { baseVertexOffset, outputPrimVertex }));
Operand outputPrimIndex = IncrementLocalMemory(node, resourceManager.LocalGeometryOutputIndexCountMemoryId);
Operand baseIndexOffset = GenerateBaseOffset(
resourceManager,
node,
definitions.GetGeometryOutputIndexBufferStride(),
definitions.ThreadsPerInputPrimitive);
Operand outputBaseIndex = Local();
node.List.AddBefore(node, new Operation(Instruction.Add, outputBaseIndex, new[] { baseIndexOffset, outputPrimIndex }));
node.List.AddBefore(node, new Operation(
Instruction.Store,
StorageKind.StorageBuffer,
null,
new[] { Const(ibOutputBinding), Const(0), outputBaseIndex, outputBaseVertex }));
Operand baseOffset = Local();
node.List.AddBefore(node, new Operation(Instruction.Multiply, baseOffset, new[] { outputBaseVertex, Const(stride) }));
LinkedListNode<INode> newNode = node;
for (int offset = 0; offset < stride; offset++)
{
Operand vertexOffset;
if (offset > 0)
{
vertexOffset = Local();
node.List.AddBefore(node, new Operation(Instruction.Add, vertexOffset, new[] { baseOffset, Const(offset) }));
}
else
{
vertexOffset = baseOffset;
}
Operand value = Local();
node.List.AddBefore(node, new Operation(
Instruction.Load,
StorageKind.LocalMemory,
value,
new[] { Const(resourceManager.LocalVertexDataMemoryId), Const(offset) }));
newNode = node.List.AddBefore(node, new Operation(
Instruction.Store,
StorageKind.StorageBuffer,
null,
new[] { Const(vbOutputBinding), Const(0), vertexOffset, value }));
}
return newNode;
}
private static LinkedListNode<INode> GenerateEndPrimitive(ShaderDefinitions definitions, ResourceManager resourceManager, LinkedListNode<INode> node)
{
int ibOutputBinding = resourceManager.Reservations.GeometryIndexOutputStorageBufferBinding;
Operand outputPrimIndex = IncrementLocalMemory(node, resourceManager.LocalGeometryOutputIndexCountMemoryId);
Operand baseIndexOffset = GenerateBaseOffset(
resourceManager,
node,
definitions.GetGeometryOutputIndexBufferStride(),
definitions.ThreadsPerInputPrimitive);
Operand outputBaseIndex = Local();
node.List.AddBefore(node, new Operation(Instruction.Add, outputBaseIndex, new[] { baseIndexOffset, outputPrimIndex }));
return node.List.AddBefore(node, new Operation(
Instruction.Store,
StorageKind.StorageBuffer,
null,
new[] { Const(ibOutputBinding), Const(0), outputBaseIndex, Const(-1) }));
}
private static Operand GenerateBaseOffset(ResourceManager resourceManager, LinkedListNode<INode> node, int stride, int threadsPerInputPrimitive)
{
Operand primitiveId = Local();
GeneratePrimitiveId(resourceManager, node, primitiveId);
Operand baseOffset = Local();
node.List.AddBefore(node, new Operation(Instruction.Multiply, baseOffset, new[] { primitiveId, Const(stride) }));
Operand invocationId = Local();
GenerateInvocationId(node, invocationId);
Operand invocationOffset = Local();
node.List.AddBefore(node, new Operation(Instruction.Multiply, invocationOffset, new[] { invocationId, Const(stride / threadsPerInputPrimitive) }));
Operand combinedOffset = Local();
node.List.AddBefore(node, new Operation(Instruction.Add, combinedOffset, new[] { baseOffset, invocationOffset }));
return combinedOffset;
}
private static Operand IncrementLocalMemory(LinkedListNode<INode> node, int memoryId)
{
Operand oldValue = Local();
node.List.AddBefore(node, new Operation(
Instruction.Load,
StorageKind.LocalMemory,
oldValue,
new[] { Const(memoryId) }));
Operand newValue = Local();
node.List.AddBefore(node, new Operation(Instruction.Add, newValue, new[] { oldValue, Const(1) }));
node.List.AddBefore(node, new Operation(Instruction.Store, StorageKind.LocalMemory, null, new[] { Const(memoryId), newValue }));
return oldValue;
}
private static Operand GenerateVertexOffset(
ResourceManager resourceManager,
LinkedListNode<INode> node,
int elementOffset,
Operand primVertex)
{
int vertexInfoCbBinding = resourceManager.Reservations.VertexInfoConstantBufferBinding;
Operand vertexCount = Local();
node.List.AddBefore(node, new Operation(
Instruction.Load,
StorageKind.ConstantBuffer,
vertexCount,
new[] { Const(vertexInfoCbBinding), Const((int)VertexInfoBufferField.VertexCounts), Const(0) }));
Operand primInputVertex = Local();
node.List.AddBefore(node, new Operation(
Instruction.Load,
StorageKind.LocalMemory,
primInputVertex,
new[] { Const(resourceManager.LocalTopologyRemapMemoryId), primVertex }));
Operand instanceIndex = Local();
node.List.AddBefore(node, new Operation(
Instruction.Load,
StorageKind.Input,
instanceIndex,
new[] { Const((int)IoVariable.GlobalId), Const(1) }));
Operand baseVertex = Local();
node.List.AddBefore(node, new Operation(Instruction.Multiply, baseVertex, new[] { instanceIndex, vertexCount }));
Operand vertexIndex = Local();
node.List.AddBefore(node, new Operation(Instruction.Add, vertexIndex, new[] { baseVertex, primInputVertex }));
Operand vertexBaseOffset = Local();
node.List.AddBefore(node, new Operation(
Instruction.Multiply,
vertexBaseOffset,
new[] { vertexIndex, Const(resourceManager.Reservations.InputSizePerInvocation) }));
Operand vertexElemOffset;
if (elementOffset != 0)
{
vertexElemOffset = Local();
node.List.AddBefore(node, new Operation(Instruction.Add, vertexElemOffset, new[] { vertexBaseOffset, Const(elementOffset) }));
}
else
{
vertexElemOffset = vertexBaseOffset;
}
return vertexElemOffset;
}
private static LinkedListNode<INode> GeneratePrimitiveId(ResourceManager resourceManager, LinkedListNode<INode> node, Operand dest)
{
int vertexInfoCbBinding = resourceManager.Reservations.VertexInfoConstantBufferBinding;
Operand vertexCount = Local();
node.List.AddBefore(node, new Operation(
Instruction.Load,
StorageKind.ConstantBuffer,
vertexCount,
new[] { Const(vertexInfoCbBinding), Const((int)VertexInfoBufferField.VertexCounts), Const(0) }));
Operand vertexIndex = Local();
node.List.AddBefore(node, new Operation(
Instruction.Load,
StorageKind.Input,
vertexIndex,
new[] { Const((int)IoVariable.GlobalId), Const(0) }));
Operand instanceIndex = Local();
node.List.AddBefore(node, new Operation(
Instruction.Load,
StorageKind.Input,
instanceIndex,
new[] { Const((int)IoVariable.GlobalId), Const(1) }));
Operand baseVertex = Local();
node.List.AddBefore(node, new Operation(Instruction.Multiply, baseVertex, new[] { instanceIndex, vertexCount }));
return node.List.AddBefore(node, new Operation(Instruction.Add, dest, new[] { baseVertex, vertexIndex }));
}
private static LinkedListNode<INode> GenerateInvocationId(LinkedListNode<INode> node, Operand dest)
{
return node.List.AddBefore(node, new Operation(
Instruction.Load,
StorageKind.Input,
dest,
new[] { Const((int)IoVariable.GlobalId), Const(2) }));
}
private static bool TryGetOffset(ResourceManager resourceManager, Operation operation, StorageKind storageKind, out int outputOffset)
{
bool isStore = operation.Inst == Instruction.Store;
IoVariable ioVariable = (IoVariable)operation.GetSource(0).Value;
bool isValidOutput;
if (ioVariable == IoVariable.UserDefined)
{
int lastIndex = operation.SourcesCount - (isStore ? 2 : 1);
int location = operation.GetSource(1).Value;
int component = operation.GetSource(lastIndex).Value;
isValidOutput = resourceManager.Reservations.TryGetOffset(storageKind, location, component, out outputOffset);
}
else
{
if (ResourceReservations.IsVectorOrArrayVariable(ioVariable))
{
int component = operation.GetSource(operation.SourcesCount - (isStore ? 2 : 1)).Value;
isValidOutput = resourceManager.Reservations.TryGetOffset(storageKind, ioVariable, component, out outputOffset);
}
else
{
isValidOutput = resourceManager.Reservations.TryGetOffset(storageKind, ioVariable, out outputOffset);
}
}
return isValidOutput;
}
}
}

View File

@@ -0,0 +1,11 @@
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
using System.Collections.Generic;
namespace Ryujinx.Graphics.Shader.Translation.Transforms
{
interface ITransformPass
{
abstract static bool IsEnabled(IGpuAccessor gpuAccessor, ShaderStage stage, TargetLanguage targetLanguage, FeatureFlags usedFeatures);
abstract static LinkedListNode<INode> RunPass(TransformContext context, LinkedListNode<INode> node);
}
}

View File

@@ -0,0 +1,58 @@
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
using Ryujinx.Graphics.Shader.Translation.Optimizations;
using System.Collections.Generic;
using System.Diagnostics;
using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
namespace Ryujinx.Graphics.Shader.Translation.Transforms
{
class SharedAtomicSignedCas : ITransformPass
{
public static bool IsEnabled(IGpuAccessor gpuAccessor, ShaderStage stage, TargetLanguage targetLanguage, FeatureFlags usedFeatures)
{
return targetLanguage != TargetLanguage.Spirv && stage == ShaderStage.Compute && usedFeatures.HasFlag(FeatureFlags.SharedMemory);
}
public static LinkedListNode<INode> RunPass(TransformContext context, LinkedListNode<INode> node)
{
Operation operation = (Operation)node.Value;
HelperFunctionName name;
if (operation.Inst == Instruction.AtomicMaxS32)
{
name = HelperFunctionName.SharedAtomicMaxS32;
}
else if (operation.Inst == Instruction.AtomicMinS32)
{
name = HelperFunctionName.SharedAtomicMinS32;
}
else
{
return node;
}
if (operation.StorageKind != StorageKind.SharedMemory)
{
return node;
}
Operand result = operation.Dest;
Operand memoryId = operation.GetSource(0);
Operand byteOffset = operation.GetSource(1);
Operand value = operation.GetSource(2);
Debug.Assert(memoryId.Type == OperandType.Constant);
int functionId = context.Hfm.GetOrCreateFunctionId(name, memoryId.Value);
Operand[] callArgs = [Const(functionId), byteOffset, value];
LinkedListNode<INode> newNode = node.List.AddBefore(node, new Operation(Instruction.Call, 0, result, callArgs));
Utils.DeleteNode(node, operation);
return newNode;
}
}
}

View File

@@ -0,0 +1,57 @@
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
using Ryujinx.Graphics.Shader.Translation.Optimizations;
using System.Collections.Generic;
using System.Diagnostics;
using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
namespace Ryujinx.Graphics.Shader.Translation.Transforms
{
class SharedStoreSmallIntCas : ITransformPass
{
public static bool IsEnabled(IGpuAccessor gpuAccessor, ShaderStage stage, TargetLanguage targetLanguage, FeatureFlags usedFeatures)
{
return stage == ShaderStage.Compute && usedFeatures.HasFlag(FeatureFlags.SharedMemory);
}
public static LinkedListNode<INode> RunPass(TransformContext context, LinkedListNode<INode> node)
{
Operation operation = (Operation)node.Value;
HelperFunctionName name;
if (operation.StorageKind == StorageKind.SharedMemory8)
{
name = HelperFunctionName.SharedStore8;
}
else if (operation.StorageKind == StorageKind.SharedMemory16)
{
name = HelperFunctionName.SharedStore16;
}
else
{
return node;
}
if (operation.Inst != Instruction.Store)
{
return node;
}
Operand memoryId = operation.GetSource(0);
Operand byteOffset = operation.GetSource(1);
Operand value = operation.GetSource(2);
Debug.Assert(memoryId.Type == OperandType.Constant);
int functionId = context.Hfm.GetOrCreateFunctionId(name, memoryId.Value);
Operand[] callArgs = [Const(functionId), byteOffset, value];
LinkedListNode<INode> newNode = node.List.AddBefore(node, new Operation(Instruction.Call, 0, (Operand)null, callArgs));
Utils.DeleteNode(node, operation);
return newNode;
}
}
}

View File

@@ -0,0 +1,52 @@
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
using Ryujinx.Graphics.Shader.Translation.Optimizations;
using System.Collections.Generic;
using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
namespace Ryujinx.Graphics.Shader.Translation.Transforms
{
class ShufflePass : ITransformPass
{
public static bool IsEnabled(IGpuAccessor gpuAccessor, ShaderStage stage, TargetLanguage targetLanguage, FeatureFlags usedFeatures)
{
return usedFeatures.HasFlag(FeatureFlags.Shuffle);
}
public static LinkedListNode<INode> RunPass(TransformContext context, LinkedListNode<INode> node)
{
Operation operation = (Operation)node.Value;
HelperFunctionName functionName = operation.Inst switch
{
Instruction.Shuffle => HelperFunctionName.Shuffle,
Instruction.ShuffleDown => HelperFunctionName.ShuffleDown,
Instruction.ShuffleUp => HelperFunctionName.ShuffleUp,
Instruction.ShuffleXor => HelperFunctionName.ShuffleXor,
_ => HelperFunctionName.Invalid,
};
if (functionName == HelperFunctionName.Invalid || operation.SourcesCount != 3 || operation.DestsCount != 2)
{
return node;
}
int functionId = context.Hfm.GetOrCreateShuffleFunctionId(functionName, context.GpuAccessor.QueryHostSubgroupSize());
Operand result = operation.GetDest(0);
Operand valid = operation.GetDest(1);
Operand value = operation.GetSource(0);
Operand index = operation.GetSource(1);
Operand mask = operation.GetSource(2);
operation.Dest = null;
Operand[] callArgs = [Const(functionId), value, index, mask, valid];
LinkedListNode<INode> newNode = node.List.AddBefore(node, new Operation(Instruction.Call, 0, result, callArgs));
Utils.DeleteNode(node, operation);
return newNode;
}
}
}

View File

@@ -0,0 +1,749 @@
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
using System.Collections.Generic;
using System.Linq;
using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
namespace Ryujinx.Graphics.Shader.Translation.Transforms
{
class TexturePass : ITransformPass
{
public static bool IsEnabled(IGpuAccessor gpuAccessor, ShaderStage stage, TargetLanguage targetLanguage, FeatureFlags usedFeatures)
{
return true;
}
public static LinkedListNode<INode> RunPass(TransformContext context, LinkedListNode<INode> node)
{
if (node.Value is TextureOperation texOp)
{
node = InsertTexelFetchScale(context.Hfm, node, context.ResourceManager, context.Stage);
node = InsertTextureSizeUnscale(context.Hfm, node, context.ResourceManager, context.Stage);
if (texOp.Inst == Instruction.TextureSample)
{
node = InsertCoordNormalization(context.Hfm, node, context.ResourceManager, context.GpuAccessor, context.Stage);
node = InsertCoordGatherBias(node, context.ResourceManager, context.GpuAccessor);
node = InsertConstOffsets(node, context.ResourceManager, context.GpuAccessor, context.Stage);
if (texOp.Type == SamplerType.TextureBuffer && !context.GpuAccessor.QueryHostSupportsSnormBufferTextureFormat())
{
node = InsertSnormNormalization(node, context.ResourceManager, context.GpuAccessor);
}
}
}
return node;
}
private static LinkedListNode<INode> InsertTexelFetchScale(
HelperFunctionManager hfm,
LinkedListNode<INode> node,
ResourceManager resourceManager,
ShaderStage stage)
{
TextureOperation texOp = (TextureOperation)node.Value;
bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
bool intCoords = (texOp.Flags & TextureFlags.IntCoords) != 0;
bool isImage = IsImageInstructionWithScale(texOp.Inst);
bool isIndexed = resourceManager.IsArrayOfTexturesOrImages(texOp.Binding, isImage);
if ((texOp.Inst == Instruction.TextureSample || isImage) &&
(intCoords || isImage) &&
!isBindless &&
!isIndexed &&
stage.SupportsRenderScale &&
TypeSupportsScale(texOp.Type))
{
int functionId = hfm.GetOrCreateFunctionId(HelperFunctionName.TexelFetchScale);
int samplerIndex = isImage
? resourceManager.GetTextureDescriptors(includeArrays: false).Length + resourceManager.FindImageDescriptorIndex(texOp.Binding)
: resourceManager.FindTextureDescriptorIndex(texOp.Binding);
int coordsCount = texOp.Type.Dimensions;
int coordsIndex = isBindless ? 1 : 0;
for (int index = 0; index < coordsCount; index++)
{
Operand scaledCoord = Local();
Operand[] callArgs;
if (stage == ShaderStage.Fragment)
{
callArgs = [Const(functionId), texOp.GetSource(coordsIndex + index), Const(samplerIndex), Const(index)
];
}
else
{
callArgs = [Const(functionId), texOp.GetSource(coordsIndex + index), Const(samplerIndex)];
}
node.List.AddBefore(node, new Operation(Instruction.Call, 0, scaledCoord, callArgs));
texOp.SetSource(coordsIndex + index, scaledCoord);
}
}
return node;
}
private static LinkedListNode<INode> InsertTextureSizeUnscale(
HelperFunctionManager hfm,
LinkedListNode<INode> node,
ResourceManager resourceManager,
ShaderStage stage)
{
TextureOperation texOp = (TextureOperation)node.Value;
bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
bool isIndexed = resourceManager.IsArrayOfTexturesOrImages(texOp.Binding, isImage: false);
if (texOp.Inst == Instruction.TextureQuerySize &&
texOp.Index < 2 &&
!isBindless &&
!isIndexed &&
stage.SupportsRenderScale &&
TypeSupportsScale(texOp.Type))
{
int functionId = hfm.GetOrCreateFunctionId(HelperFunctionName.TextureSizeUnscale);
int samplerIndex = resourceManager.FindTextureDescriptorIndex(texOp.Binding);
for (int index = texOp.DestsCount - 1; index >= 0; index--)
{
Operand dest = texOp.GetDest(index);
Operand unscaledSize = Local();
// Replace all uses with the unscaled size value.
// This must be done before the call is added, since it also is a use of the original size.
foreach (INode useOp in dest.UseOps)
{
for (int srcIndex = 0; srcIndex < useOp.SourcesCount; srcIndex++)
{
if (useOp.GetSource(srcIndex) == dest)
{
useOp.SetSource(srcIndex, unscaledSize);
}
}
}
Operand[] callArgs = [Const(functionId), dest, Const(samplerIndex)];
node.List.AddAfter(node, new Operation(Instruction.Call, 0, unscaledSize, callArgs));
}
}
return node;
}
private static LinkedListNode<INode> InsertCoordNormalization(
HelperFunctionManager hfm,
LinkedListNode<INode> node,
ResourceManager resourceManager,
IGpuAccessor gpuAccessor,
ShaderStage stage)
{
// Emulate non-normalized coordinates by normalizing the coordinates on the shader.
// Without normalization, the coordinates are expected to the in the [0, W or H] range,
// and otherwise, it is expected to be in the [0, 1] range.
// We normalize by dividing the coords by the texture size.
TextureOperation texOp = (TextureOperation)node.Value;
bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
bool isIndexed = resourceManager.IsArrayOfTexturesOrImages(texOp.Binding, isImage: false);
if (isBindless || isIndexed || !resourceManager.TryGetCbufSlotAndHandleForTexture(texOp.Binding, out int cbufSlot, out int handle))
{
return node;
}
bool intCoords = (texOp.Flags & TextureFlags.IntCoords) != 0;
bool isCoordNormalized = gpuAccessor.QueryTextureCoordNormalized(handle, cbufSlot);
if (isCoordNormalized || intCoords)
{
return node;
}
int coordsCount = texOp.Type.Dimensions;
int normCoordsCount = (texOp.Type & SamplerType.Mask) == SamplerType.TextureCube ? 2 : coordsCount;
for (int index = 0; index < normCoordsCount; index++)
{
Operand coordSize = Local();
Operand[] texSizeSources = [Const(0)];
LinkedListNode<INode> textureSizeNode = node.List.AddBefore(node, new TextureOperation(
Instruction.TextureQuerySize,
texOp.Type,
texOp.Format,
texOp.Flags,
texOp.Set,
texOp.Binding,
index,
[coordSize],
texSizeSources));
resourceManager.SetUsageFlagsForTextureQuery(texOp.Binding, texOp.Type);
Operand source = texOp.GetSource(index);
Operand coordNormalized = Local();
node.List.AddBefore(node, new Operation(Instruction.FP32 | Instruction.Divide, coordNormalized, source, GenerateI2f(node, coordSize)));
texOp.SetSource(index, coordNormalized);
InsertTextureSizeUnscale(hfm, textureSizeNode, resourceManager, stage);
}
return node;
}
private static LinkedListNode<INode> InsertCoordGatherBias(LinkedListNode<INode> node, ResourceManager resourceManager, IGpuAccessor gpuAccessor)
{
// The gather behavior when the coordinate sits right in the middle of two texels is not well defined.
// To ensure the correct texel is sampled, we add a small bias value to the coordinate.
// This value is calculated as the minimum value required to change the texel it will sample from,
// and is 0 if the host does not require the bias.
TextureOperation texOp = (TextureOperation)node.Value;
bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
bool isGather = (texOp.Flags & TextureFlags.Gather) != 0;
int gatherBiasPrecision = gpuAccessor.QueryHostGatherBiasPrecision();
if (!isGather || gatherBiasPrecision == 0)
{
return node;
}
bool isIndexed = resourceManager.IsArrayOfTexturesOrImages(texOp.Binding, isImage: false);
int coordsCount = texOp.Type.Dimensions;
int coordsIndex = isBindless || isIndexed ? 1 : 0;
int normCoordsCount = (texOp.Type & SamplerType.Mask) == SamplerType.TextureCube ? 2 : coordsCount;
for (int index = 0; index < normCoordsCount; index++)
{
Operand coordSize = Local();
Operand scaledSize = Local();
Operand bias = Local();
Operand[] texSizeSources;
if (isBindless || isIndexed)
{
texSizeSources = [texOp.GetSource(0), Const(0)];
}
else
{
texSizeSources = [Const(0)];
}
node.List.AddBefore(node, new TextureOperation(
Instruction.TextureQuerySize,
texOp.Type,
texOp.Format,
texOp.Flags,
texOp.Set,
texOp.Binding,
index,
[coordSize],
texSizeSources));
node.List.AddBefore(node, new Operation(
Instruction.FP32 | Instruction.Multiply,
scaledSize,
GenerateI2f(node, coordSize),
ConstF((float)(1 << (gatherBiasPrecision + 1)))));
node.List.AddBefore(node, new Operation(Instruction.FP32 | Instruction.Divide, bias, ConstF(1f), scaledSize));
Operand source = texOp.GetSource(coordsIndex + index);
Operand coordBiased = Local();
node.List.AddBefore(node, new Operation(Instruction.FP32 | Instruction.Add, coordBiased, source, bias));
texOp.SetSource(coordsIndex + index, coordBiased);
}
return node;
}
private static LinkedListNode<INode> InsertConstOffsets(LinkedListNode<INode> node, ResourceManager resourceManager, IGpuAccessor gpuAccessor, ShaderStage stage)
{
// Non-constant texture offsets are not allowed (according to the spec),
// however some GPUs does support that.
// For GPUs where it is not supported, we can replace the instruction with the following:
// For texture*Offset, we replace it by texture*, and add the offset to the P coords.
// The offset can be calculated as offset / textureSize(lod), where lod = textureQueryLod(coords).
// For texelFetchOffset, we replace it by texelFetch and add the offset to the P coords directly.
// For textureGatherOffset, we split the operation into up to 4 operations, one for each component
// that is accessed, where each textureGather operation has a different offset for each pixel.
TextureOperation texOp = (TextureOperation)node.Value;
bool hasOffset = (texOp.Flags & TextureFlags.Offset) != 0;
bool hasOffsets = (texOp.Flags & TextureFlags.Offsets) != 0;
bool needsOffsetsEmulation = hasOffsets && !gpuAccessor.QueryHostSupportsTextureGatherOffsets();
bool hasInvalidOffset = needsOffsetsEmulation || ((hasOffset || hasOffsets) && !gpuAccessor.QueryHostSupportsNonConstantTextureOffset());
bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
if (!hasInvalidOffset)
{
return node;
}
bool isGather = (texOp.Flags & TextureFlags.Gather) != 0;
bool hasDerivatives = (texOp.Flags & TextureFlags.Derivatives) != 0;
bool intCoords = (texOp.Flags & TextureFlags.IntCoords) != 0;
bool hasLodBias = (texOp.Flags & TextureFlags.LodBias) != 0;
bool hasLodLevel = (texOp.Flags & TextureFlags.LodLevel) != 0;
bool isArray = (texOp.Type & SamplerType.Array) != 0;
bool isMultisample = (texOp.Type & SamplerType.Multisample) != 0;
bool isShadow = (texOp.Type & SamplerType.Shadow) != 0;
int coordsCount = texOp.Type.Dimensions;
int offsetsCount;
if (hasOffsets)
{
offsetsCount = coordsCount * 4;
}
else if (hasOffset)
{
offsetsCount = coordsCount;
}
else
{
offsetsCount = 0;
}
bool isIndexed = resourceManager.IsArrayOfTexturesOrImages(texOp.Binding, isImage: false);
Operand[] offsets = new Operand[offsetsCount];
Operand[] sources = new Operand[texOp.SourcesCount - offsetsCount];
int copyCount = 0;
if (isBindless || isIndexed)
{
copyCount++;
}
Operand[] lodSources = new Operand[copyCount + coordsCount];
for (int index = 0; index < lodSources.Length; index++)
{
lodSources[index] = texOp.GetSource(index);
}
copyCount += coordsCount;
if (isArray)
{
copyCount++;
}
if (isShadow)
{
copyCount++;
}
if (hasDerivatives)
{
copyCount += coordsCount * 2;
}
if (isMultisample)
{
copyCount++;
}
else if (hasLodLevel)
{
copyCount++;
}
int srcIndex = 0;
int dstIndex = 0;
for (int index = 0; index < copyCount; index++)
{
sources[dstIndex++] = texOp.GetSource(srcIndex++);
}
bool areAllOffsetsConstant = true;
for (int index = 0; index < offsetsCount; index++)
{
Operand offset = texOp.GetSource(srcIndex++);
areAllOffsetsConstant &= offset.Type == OperandType.Constant;
offsets[index] = offset;
}
if (!needsOffsetsEmulation)
{
hasInvalidOffset &= !areAllOffsetsConstant;
if (!hasInvalidOffset)
{
return node;
}
}
if (hasLodBias)
{
sources[dstIndex++] = texOp.GetSource(srcIndex++);
}
if (isGather && !isShadow)
{
sources[dstIndex++] = texOp.GetSource(srcIndex++);
}
int coordsIndex = isBindless || isIndexed ? 1 : 0;
int componentIndex = texOp.Index;
Operand[] dests = new Operand[texOp.DestsCount];
for (int i = 0; i < texOp.DestsCount; i++)
{
dests[i] = texOp.GetDest(i);
}
Operand bindlessHandle = isBindless || isIndexed ? sources[0] : null;
LinkedListNode<INode> oldNode = node;
if (isGather && !isShadow && hasOffsets)
{
Operand[] newSources = new Operand[sources.Length];
sources.CopyTo(newSources, 0);
Operand[] texSizes = InsertTextureBaseSize(node, texOp, bindlessHandle, coordsCount);
int destIndex = 0;
for (int compIndex = 0; compIndex < 4; compIndex++)
{
if (((texOp.Index >> compIndex) & 1) == 0)
{
continue;
}
for (int index = 0; index < coordsCount; index++)
{
Operand offset = Local();
Operand intOffset = offsets[index + compIndex * coordsCount];
node.List.AddBefore(node, new Operation(
Instruction.FP32 | Instruction.Divide,
offset,
GenerateI2f(node, intOffset),
GenerateI2f(node, texSizes[index])));
Operand source = sources[coordsIndex + index];
Operand coordPlusOffset = Local();
node.List.AddBefore(node, new Operation(Instruction.FP32 | Instruction.Add, coordPlusOffset, source, offset));
newSources[coordsIndex + index] = coordPlusOffset;
}
TextureOperation newTexOp = new(
Instruction.TextureSample,
texOp.Type,
texOp.Format,
texOp.Flags & ~(TextureFlags.Offset | TextureFlags.Offsets),
texOp.Set,
texOp.Binding,
1 << 3, // W component: i=0, j=0
[dests[destIndex++]],
newSources);
node = node.List.AddBefore(node, newTexOp);
}
}
else
{
if (intCoords)
{
for (int index = 0; index < coordsCount; index++)
{
Operand source = sources[coordsIndex + index];
Operand coordPlusOffset = Local();
node.List.AddBefore(node, new Operation(Instruction.Add, coordPlusOffset, source, offsets[index]));
sources[coordsIndex + index] = coordPlusOffset;
}
}
else
{
Operand[] texSizes = isGather
? InsertTextureBaseSize(node, texOp, bindlessHandle, coordsCount)
: InsertTextureLod(node, texOp, lodSources, bindlessHandle, coordsCount, stage);
for (int index = 0; index < coordsCount; index++)
{
Operand offset = Local();
Operand intOffset = offsets[index];
node.List.AddBefore(node, new Operation(
Instruction.FP32 | Instruction.Divide,
offset,
GenerateI2f(node, intOffset),
GenerateI2f(node, texSizes[index])));
Operand source = sources[coordsIndex + index];
Operand coordPlusOffset = Local();
node.List.AddBefore(node, new Operation(Instruction.FP32 | Instruction.Add, coordPlusOffset, source, offset));
sources[coordsIndex + index] = coordPlusOffset;
}
}
TextureOperation newTexOp = new(
Instruction.TextureSample,
texOp.Type,
texOp.Format,
texOp.Flags & ~(TextureFlags.Offset | TextureFlags.Offsets),
texOp.Set,
texOp.Binding,
componentIndex,
dests,
sources);
node = node.List.AddBefore(node, newTexOp);
}
node.List.Remove(oldNode);
for (int index = 0; index < texOp.SourcesCount; index++)
{
texOp.SetSource(index, null);
}
return node;
}
private static Operand[] InsertTextureBaseSize(
LinkedListNode<INode> node,
TextureOperation texOp,
Operand bindlessHandle,
int coordsCount)
{
Operand[] texSizes = new Operand[coordsCount];
for (int index = 0; index < coordsCount; index++)
{
texSizes[index] = Local();
Operand[] texSizeSources;
if (bindlessHandle != null)
{
texSizeSources = [bindlessHandle, Const(0)];
}
else
{
texSizeSources = [Const(0)];
}
node.List.AddBefore(node, new TextureOperation(
Instruction.TextureQuerySize,
texOp.Type,
texOp.Format,
texOp.Flags,
texOp.Set,
texOp.Binding,
index,
[texSizes[index]],
texSizeSources));
}
return texSizes;
}
private static Operand[] InsertTextureLod(
LinkedListNode<INode> node,
TextureOperation texOp,
Operand[] lodSources,
Operand bindlessHandle,
int coordsCount,
ShaderStage stage)
{
Operand[] texSizes = new Operand[coordsCount];
Operand lod;
if (stage == ShaderStage.Fragment)
{
lod = Local();
node.List.AddBefore(node, new TextureOperation(
Instruction.Lod,
texOp.Type,
texOp.Format,
texOp.Flags,
texOp.Set,
texOp.Binding,
0,
[lod],
lodSources));
}
else
{
lod = Const(0);
}
for (int index = 0; index < coordsCount; index++)
{
texSizes[index] = Local();
Operand[] texSizeSources;
if (bindlessHandle != null)
{
texSizeSources = [bindlessHandle, GenerateF2i(node, lod)];
}
else
{
texSizeSources = [GenerateF2i(node, lod)];
}
node.List.AddBefore(node, new TextureOperation(
Instruction.TextureQuerySize,
texOp.Type,
texOp.Format,
texOp.Flags,
texOp.Set,
texOp.Binding,
index,
[texSizes[index]],
texSizeSources));
}
return texSizes;
}
private static LinkedListNode<INode> InsertSnormNormalization(LinkedListNode<INode> node, ResourceManager resourceManager, IGpuAccessor gpuAccessor)
{
TextureOperation texOp = (TextureOperation)node.Value;
// We can't query the format of a bindless texture,
// because the handle is unknown, it can have any format.
if (texOp.Flags.HasFlag(TextureFlags.Bindless) || !resourceManager.TryGetCbufSlotAndHandleForTexture(texOp.Binding, out int cbufSlot, out int handle))
{
return node;
}
TextureFormat format = gpuAccessor.QueryTextureFormat(handle, cbufSlot);
int maxPositive = format switch
{
TextureFormat.R8Snorm => sbyte.MaxValue,
TextureFormat.R8G8Snorm => sbyte.MaxValue,
TextureFormat.R8G8B8A8Snorm => sbyte.MaxValue,
TextureFormat.R16Snorm => short.MaxValue,
TextureFormat.R16G16Snorm => short.MaxValue,
TextureFormat.R16G16B16A16Snorm => short.MaxValue,
_ => 0,
};
// The value being 0 means that the format is not a SNORM format,
// so there's nothing to do here.
if (maxPositive == 0)
{
return node;
}
// Do normalization. We assume SINT formats are being used
// as replacement for SNORM (which is not supported).
for (int i = 0; i < texOp.DestsCount; i++)
{
Operand dest = texOp.GetDest(i);
INode[] uses = dest.UseOps.ToArray();
Operation convOp = new(Instruction.ConvertS32ToFP32, Local(), dest);
Operation normOp = new(Instruction.FP32 | Instruction.Multiply, Local(), convOp.Dest, ConstF(1f / maxPositive));
node = node.List.AddAfter(node, convOp);
node = node.List.AddAfter(node, normOp);
foreach (INode useOp in uses)
{
if (useOp is not Operation op)
{
continue;
}
// Replace all uses of the texture pixel value with the normalized value.
for (int index = 0; index < op.SourcesCount; index++)
{
if (op.GetSource(index) == dest)
{
op.SetSource(index, normOp.Dest);
}
}
}
}
return node;
}
private static Operand GenerateI2f(LinkedListNode<INode> node, Operand value)
{
Operand res = Local();
node.List.AddBefore(node, new Operation(Instruction.ConvertS32ToFP32, res, value));
return res;
}
private static Operand GenerateF2i(LinkedListNode<INode> node, Operand value)
{
Operand res = Local();
node.List.AddBefore(node, new Operation(Instruction.ConvertFP32ToS32, res, value));
return res;
}
private static bool IsImageInstructionWithScale(Instruction inst)
{
// Currently, we don't support scaling images that are modified,
// so we only need to care about the load instruction.
return inst == Instruction.ImageLoad;
}
private static bool TypeSupportsScale(SamplerType type)
{
return (type & SamplerType.Mask) == SamplerType.Texture2D;
}
}
}

View File

@@ -0,0 +1,44 @@
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
using System.Collections.Generic;
namespace Ryujinx.Graphics.Shader.Translation.Transforms
{
static class TransformPasses
{
public static void RunPass(TransformContext context)
{
RunPass<DrawParametersReplace>(context);
RunPass<ForcePreciseEnable>(context);
RunPass<VectorComponentSelect>(context);
RunPass<TexturePass>(context);
RunPass<SharedStoreSmallIntCas>(context);
RunPass<SharedAtomicSignedCas>(context);
RunPass<ShufflePass>(context);
RunPass<VertexToCompute>(context);
RunPass<GeometryToCompute>(context);
}
private static void RunPass<T>(TransformContext context) where T : ITransformPass
{
if (!T.IsEnabled(context.GpuAccessor, context.Stage, context.TargetLanguage, context.UsedFeatures))
{
return;
}
for (int blkIndex = 0; blkIndex < context.Blocks.Length; blkIndex++)
{
BasicBlock block = context.Blocks[blkIndex];
for (LinkedListNode<INode> node = block.Operations.First; node != null; node = node.Next)
{
if (node.Value is not Operation)
{
continue;
}
node = T.RunPass(context, node);
}
}
}
}
}

View File

@@ -0,0 +1,96 @@
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
using Ryujinx.Graphics.Shader.StructuredIr;
using System.Collections.Generic;
using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
namespace Ryujinx.Graphics.Shader.Translation.Transforms
{
class VectorComponentSelect : ITransformPass
{
public static bool IsEnabled(IGpuAccessor gpuAccessor, ShaderStage stage, TargetLanguage targetLanguage, FeatureFlags usedFeatures)
{
return gpuAccessor.QueryHostHasVectorIndexingBug();
}
public static LinkedListNode<INode> RunPass(TransformContext context, LinkedListNode<INode> node)
{
Operation operation = (Operation)node.Value;
if (operation.Inst != Instruction.Load ||
operation.StorageKind != StorageKind.ConstantBuffer ||
operation.SourcesCount < 3)
{
return node;
}
Operand bindingIndex = operation.GetSource(0);
Operand fieldIndex = operation.GetSource(1);
Operand elemIndex = operation.GetSource(operation.SourcesCount - 1);
if (bindingIndex.Type != OperandType.Constant ||
fieldIndex.Type != OperandType.Constant ||
elemIndex.Type == OperandType.Constant)
{
return node;
}
BufferDefinition buffer = context.ResourceManager.Properties.ConstantBuffers[bindingIndex.Value];
StructureField field = buffer.Type.Fields[fieldIndex.Value];
int elemCount = (field.Type & AggregateType.ElementCountMask) switch
{
AggregateType.Vector2 => 2,
AggregateType.Vector3 => 3,
AggregateType.Vector4 => 4,
_ => 1
};
if (elemCount == 1)
{
return node;
}
Operand result = null;
for (int i = 0; i < elemCount; i++)
{
Operand value = Local();
Operand[] inputs = new Operand[operation.SourcesCount];
for (int srcIndex = 0; srcIndex < inputs.Length - 1; srcIndex++)
{
inputs[srcIndex] = operation.GetSource(srcIndex);
}
inputs[^1] = Const(i);
Operation loadOp = new(Instruction.Load, StorageKind.ConstantBuffer, value, inputs);
node.List.AddBefore(node, loadOp);
if (i == 0)
{
result = value;
}
else
{
Operand isCurrentIndex = Local();
Operand selection = Local();
Operation compareOp = new(Instruction.CompareEqual, isCurrentIndex, new Operand[] { elemIndex, Const(i) });
Operation selectOp = new(Instruction.ConditionalSelect, selection, new Operand[] { isCurrentIndex, value, result });
node.List.AddBefore(node, compareOp);
node.List.AddBefore(node, selectOp);
result = selection;
}
}
operation.TurnIntoCopy(result);
return node;
}
}
}

View File

@@ -0,0 +1,369 @@
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
using Ryujinx.Graphics.Shader.Translation.Optimizations;
using System.Collections.Generic;
using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
namespace Ryujinx.Graphics.Shader.Translation.Transforms
{
class VertexToCompute : ITransformPass
{
public static bool IsEnabled(IGpuAccessor gpuAccessor, ShaderStage stage, TargetLanguage targetLanguage, FeatureFlags usedFeatures)
{
return usedFeatures.HasFlag(FeatureFlags.VtgAsCompute);
}
public static LinkedListNode<INode> RunPass(TransformContext context, LinkedListNode<INode> node)
{
if (context.Definitions.Stage != ShaderStage.Vertex)
{
return node;
}
Operation operation = (Operation)node.Value;
LinkedListNode<INode> newNode = node;
if (operation.Inst == Instruction.Load && operation.StorageKind == StorageKind.Input)
{
Operand dest = operation.Dest;
switch ((IoVariable)operation.GetSource(0).Value)
{
case IoVariable.BaseInstance:
newNode = GenerateBaseInstanceLoad(context.ResourceManager, node, dest);
break;
case IoVariable.BaseVertex:
newNode = GenerateBaseVertexLoad(context.ResourceManager, node, dest);
break;
case IoVariable.InstanceId:
newNode = GenerateInstanceIdLoad(node, dest);
break;
case IoVariable.InstanceIndex:
newNode = GenerateInstanceIndexLoad(context.ResourceManager, node, dest);
break;
case IoVariable.VertexId:
case IoVariable.VertexIndex:
newNode = GenerateVertexIndexLoad(context.ResourceManager, node, dest);
break;
case IoVariable.UserDefined:
int location = operation.GetSource(1).Value;
int component = operation.GetSource(2).Value;
if (context.Definitions.IsAttributePacked(location))
{
bool needsSextNorm = context.Definitions.IsAttributePackedRgb10A2Signed(location);
SetBindingPair setAndBinding = context.ResourceManager.Reservations.GetVertexBufferTextureSetAndBinding(location);
Operand temp = needsSextNorm ? Local() : dest;
Operand vertexElemOffset = GenerateVertexOffset(context.ResourceManager, node, location, 0);
newNode = node.List.AddBefore(node, new TextureOperation(
Instruction.TextureSample,
SamplerType.TextureBuffer,
TextureFormat.Unknown,
TextureFlags.IntCoords,
setAndBinding.SetIndex,
setAndBinding.Binding,
1 << component,
[temp],
[vertexElemOffset]));
if (needsSextNorm)
{
bool sint = context.Definitions.IsAttributeSint(location);
CopySignExtendedNormalized(node, component == 3 ? 2 : 10, !sint, dest, temp);
}
}
else
{
SetBindingPair setAndBinding = context.ResourceManager.Reservations.GetVertexBufferTextureSetAndBinding(location);
Operand temp = component > 0 ? Local() : dest;
Operand vertexElemOffset = GenerateVertexOffset(context.ResourceManager, node, location, component);
newNode = node.List.AddBefore(node, new TextureOperation(
Instruction.TextureSample,
SamplerType.TextureBuffer,
TextureFormat.Unknown,
TextureFlags.IntCoords,
setAndBinding.SetIndex,
setAndBinding.Binding,
1,
[temp],
[vertexElemOffset]));
if (component > 0)
{
newNode = CopyMasked(context.ResourceManager, newNode, location, component, dest, temp);
}
}
break;
case IoVariable.GlobalId:
case IoVariable.SubgroupEqMask:
case IoVariable.SubgroupGeMask:
case IoVariable.SubgroupGtMask:
case IoVariable.SubgroupLaneId:
case IoVariable.SubgroupLeMask:
case IoVariable.SubgroupLtMask:
// Those are valid or expected for vertex shaders.
break;
default:
context.GpuAccessor.Log($"Invalid input \"{(IoVariable)operation.GetSource(0).Value}\".");
break;
}
}
else if (operation.Inst == Instruction.Load && operation.StorageKind == StorageKind.Output)
{
if (TryGetOutputOffset(context.ResourceManager, operation, out int outputOffset))
{
newNode = node.List.AddBefore(node, new Operation(
Instruction.Load,
StorageKind.LocalMemory,
operation.Dest,
new[] { Const(context.ResourceManager.LocalVertexDataMemoryId), Const(outputOffset) }));
}
else
{
context.GpuAccessor.Log($"Invalid output \"{(IoVariable)operation.GetSource(0).Value}\".");
}
}
else if (operation.Inst == Instruction.Store && operation.StorageKind == StorageKind.Output)
{
if (TryGetOutputOffset(context.ResourceManager, operation, out int outputOffset))
{
Operand value = operation.GetSource(operation.SourcesCount - 1);
newNode = node.List.AddBefore(node, new Operation(
Instruction.Store,
StorageKind.LocalMemory,
(Operand)null,
new[] { Const(context.ResourceManager.LocalVertexDataMemoryId), Const(outputOffset), value }));
}
else
{
context.GpuAccessor.Log($"Invalid output \"{(IoVariable)operation.GetSource(0).Value}\".");
}
}
if (newNode != node)
{
Utils.DeleteNode(node, operation);
}
return newNode;
}
private static Operand GenerateVertexOffset(ResourceManager resourceManager, LinkedListNode<INode> node, int location, int component)
{
int vertexInfoCbBinding = resourceManager.Reservations.VertexInfoConstantBufferBinding;
Operand vertexIdVr = Local();
GenerateVertexIdVertexRateLoad(resourceManager, node, vertexIdVr);
Operand vertexIdIr = Local();
GenerateVertexIdInstanceRateLoad(resourceManager, node, vertexIdIr);
Operand attributeOffset = Local();
node.List.AddBefore(node, new Operation(
Instruction.Load,
StorageKind.ConstantBuffer,
attributeOffset,
new[] { Const(vertexInfoCbBinding), Const((int)VertexInfoBufferField.VertexOffsets), Const(location), Const(0) }));
Operand isInstanceRate = Local();
node.List.AddBefore(node, new Operation(
Instruction.Load,
StorageKind.ConstantBuffer,
isInstanceRate,
new[] { Const(vertexInfoCbBinding), Const((int)VertexInfoBufferField.VertexOffsets), Const(location), Const(1) }));
Operand vertexId = Local();
node.List.AddBefore(node, new Operation(
Instruction.ConditionalSelect,
vertexId,
new[] { isInstanceRate, vertexIdIr, vertexIdVr }));
Operand vertexStride = Local();
node.List.AddBefore(node, new Operation(
Instruction.Load,
StorageKind.ConstantBuffer,
vertexStride,
new[] { Const(vertexInfoCbBinding), Const((int)VertexInfoBufferField.VertexStrides), Const(location), Const(0) }));
Operand vertexBaseOffset = Local();
node.List.AddBefore(node, new Operation(Instruction.Multiply, vertexBaseOffset, new[] { vertexId, vertexStride }));
Operand vertexOffset = Local();
node.List.AddBefore(node, new Operation(Instruction.Add, vertexOffset, new[] { attributeOffset, vertexBaseOffset }));
Operand vertexElemOffset;
if (component != 0)
{
vertexElemOffset = Local();
node.List.AddBefore(node, new Operation(Instruction.Add, vertexElemOffset, new[] { vertexOffset, Const(component) }));
}
else
{
vertexElemOffset = vertexOffset;
}
return vertexElemOffset;
}
private static LinkedListNode<INode> CopySignExtendedNormalized(LinkedListNode<INode> node, int bits, bool normalize, Operand dest, Operand src)
{
Operand leftShifted = Local();
node = node.List.AddAfter(node, new Operation(
Instruction.ShiftLeft,
leftShifted,
new[] { src, Const(32 - bits) }));
Operand rightShifted = normalize ? Local() : dest;
node = node.List.AddAfter(node, new Operation(
Instruction.ShiftRightS32,
rightShifted,
new[] { leftShifted, Const(32 - bits) }));
if (normalize)
{
Operand asFloat = Local();
node = node.List.AddAfter(node, new Operation(Instruction.ConvertS32ToFP32, asFloat, new[] { rightShifted }));
node = node.List.AddAfter(node, new Operation(
Instruction.FP32 | Instruction.Multiply,
dest,
new[] { asFloat, ConstF(1f / (1 << (bits - 1))) }));
}
return node;
}
private static LinkedListNode<INode> CopyMasked(
ResourceManager resourceManager,
LinkedListNode<INode> node,
int location,
int component,
Operand dest,
Operand src)
{
Operand componentExists = Local();
int vertexInfoCbBinding = resourceManager.Reservations.VertexInfoConstantBufferBinding;
node = node.List.AddAfter(node, new Operation(
Instruction.Load,
StorageKind.ConstantBuffer,
componentExists,
new[] { Const(vertexInfoCbBinding), Const((int)VertexInfoBufferField.VertexStrides), Const(location), Const(component) }));
return node.List.AddAfter(node, new Operation(
Instruction.ConditionalSelect,
dest,
new[] { componentExists, src, ConstF(component == 3 ? 1f : 0f) }));
}
private static LinkedListNode<INode> GenerateBaseVertexLoad(ResourceManager resourceManager, LinkedListNode<INode> node, Operand dest)
{
int vertexInfoCbBinding = resourceManager.Reservations.VertexInfoConstantBufferBinding;
return node.List.AddBefore(node, new Operation(
Instruction.Load,
StorageKind.ConstantBuffer,
dest,
new[] { Const(vertexInfoCbBinding), Const((int)VertexInfoBufferField.VertexCounts), Const(2) }));
}
private static LinkedListNode<INode> GenerateBaseInstanceLoad(ResourceManager resourceManager, LinkedListNode<INode> node, Operand dest)
{
int vertexInfoCbBinding = resourceManager.Reservations.VertexInfoConstantBufferBinding;
return node.List.AddBefore(node, new Operation(
Instruction.Load,
StorageKind.ConstantBuffer,
dest,
new[] { Const(vertexInfoCbBinding), Const((int)VertexInfoBufferField.VertexCounts), Const(3) }));
}
private static LinkedListNode<INode> GenerateVertexIndexLoad(ResourceManager resourceManager, LinkedListNode<INode> node, Operand dest)
{
Operand baseVertex = Local();
Operand vertexId = Local();
GenerateBaseVertexLoad(resourceManager, node, baseVertex);
GenerateVertexIdVertexRateLoad(resourceManager, node, vertexId);
return node.List.AddBefore(node, new Operation(Instruction.Add, dest, new[] { baseVertex, vertexId }));
}
private static LinkedListNode<INode> GenerateInstanceIndexLoad(ResourceManager resourceManager, LinkedListNode<INode> node, Operand dest)
{
Operand baseInstance = Local();
Operand instanceId = Local();
GenerateBaseInstanceLoad(resourceManager, node, baseInstance);
node.List.AddBefore(node, new Operation(
Instruction.Load,
StorageKind.Input,
instanceId,
new[] { Const((int)IoVariable.GlobalId), Const(1) }));
return node.List.AddBefore(node, new Operation(Instruction.Add, dest, new[] { baseInstance, instanceId }));
}
private static LinkedListNode<INode> GenerateVertexIdVertexRateLoad(ResourceManager resourceManager, LinkedListNode<INode> node, Operand dest)
{
Operand[] sources = [Const(resourceManager.LocalVertexIndexVertexRateMemoryId)];
return node.List.AddBefore(node, new Operation(Instruction.Load, StorageKind.LocalMemory, dest, sources));
}
private static LinkedListNode<INode> GenerateVertexIdInstanceRateLoad(ResourceManager resourceManager, LinkedListNode<INode> node, Operand dest)
{
Operand[] sources = [Const(resourceManager.LocalVertexIndexInstanceRateMemoryId)];
return node.List.AddBefore(node, new Operation(Instruction.Load, StorageKind.LocalMemory, dest, sources));
}
private static LinkedListNode<INode> GenerateInstanceIdLoad(LinkedListNode<INode> node, Operand dest)
{
Operand[] sources = [Const((int)IoVariable.GlobalId), Const(1)];
return node.List.AddBefore(node, new Operation(Instruction.Load, StorageKind.Input, dest, sources));
}
private static bool TryGetOutputOffset(ResourceManager resourceManager, Operation operation, out int outputOffset)
{
bool isStore = operation.Inst == Instruction.Store;
IoVariable ioVariable = (IoVariable)operation.GetSource(0).Value;
bool isValidOutput;
if (ioVariable == IoVariable.UserDefined)
{
int lastIndex = operation.SourcesCount - (isStore ? 2 : 1);
int location = operation.GetSource(1).Value;
int component = operation.GetSource(lastIndex).Value;
isValidOutput = resourceManager.Reservations.TryGetOffset(StorageKind.Output, location, component, out outputOffset);
}
else
{
if (ResourceReservations.IsVectorOrArrayVariable(ioVariable))
{
int component = operation.GetSource(operation.SourcesCount - (isStore ? 2 : 1)).Value;
isValidOutput = resourceManager.Reservations.TryGetOffset(StorageKind.Output, ioVariable, component, out outputOffset);
}
else
{
isValidOutput = resourceManager.Reservations.TryGetOffset(StorageKind.Output, ioVariable, out outputOffset);
}
}
return isValidOutput;
}
}
}

View File

@@ -0,0 +1,14 @@
using System;
namespace Ryujinx.Graphics.Shader.Translation
{
[Flags]
public enum TranslationFlags
{
None = 0,
VertexA = 1 << 0,
Compute = 1 << 1,
DebugMode = 1 << 2,
}
}

View File

@@ -0,0 +1,16 @@
namespace Ryujinx.Graphics.Shader.Translation
{
public readonly struct TranslationOptions
{
public TargetLanguage TargetLanguage { get; }
public TargetApi TargetApi { get; }
public TranslationFlags Flags { get; }
public TranslationOptions(TargetLanguage targetLanguage, TargetApi targetApi, TranslationFlags flags)
{
TargetLanguage = targetLanguage;
TargetApi = targetApi;
Flags = flags;
}
}
}

View File

@@ -0,0 +1,397 @@
using Ryujinx.Graphics.Shader.Decoders;
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
using System;
using System.Linq;
using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
namespace Ryujinx.Graphics.Shader.Translation
{
public static class Translator
{
private const int ThreadsPerWarp = 32;
private const int HeaderSize = 0x50;
internal readonly struct FunctionCode
{
public Operation[] Code { get; }
public FunctionCode(Operation[] code)
{
Code = code;
}
}
public static TranslatorContext CreateContext(ulong address, IGpuAccessor gpuAccessor, TranslationOptions options)
{
return DecodeShader(address, gpuAccessor, options);
}
private static TranslatorContext DecodeShader(ulong address, IGpuAccessor gpuAccessor, TranslationOptions options)
{
int localMemorySize;
ShaderDefinitions definitions;
DecodedProgram program;
if (options.Flags.HasFlag(TranslationFlags.Compute))
{
definitions = CreateComputeDefinitions(gpuAccessor);
localMemorySize = gpuAccessor.QueryComputeLocalMemorySize();
program = Decoder.Decode(definitions, gpuAccessor, address);
}
else
{
ShaderHeader header = new(gpuAccessor, address);
definitions = CreateGraphicsDefinitions(gpuAccessor, header);
localMemorySize = GetLocalMemorySize(header);
program = Decoder.Decode(definitions, gpuAccessor, address + HeaderSize);
}
ulong maxEndAddress = 0;
foreach (DecodedFunction function in program)
{
foreach (Block block in function.Blocks)
{
if (maxEndAddress < block.EndAddress)
{
maxEndAddress = block.EndAddress;
}
}
}
int size = (int)maxEndAddress + (options.Flags.HasFlag(TranslationFlags.Compute) ? 0 : HeaderSize);
return new TranslatorContext(address, size, localMemorySize, definitions, gpuAccessor, options, program);
}
private static ShaderDefinitions CreateComputeDefinitions(IGpuAccessor gpuAccessor)
{
return new ShaderDefinitions(
ShaderStage.Compute,
gpuAccessor.QueryComputeLocalSizeX(),
gpuAccessor.QueryComputeLocalSizeY(),
gpuAccessor.QueryComputeLocalSizeZ());
}
private static ShaderDefinitions CreateGraphicsDefinitions(IGpuAccessor gpuAccessor, ShaderHeader header)
{
TransformFeedbackOutput[] transformFeedbackOutputs = GetTransformFeedbackOutputs(gpuAccessor, out ulong transformFeedbackVecMap);
return new ShaderDefinitions(
header.Stage,
gpuAccessor.QueryGraphicsState(),
header.Stage == ShaderStage.Geometry && header.GpPassthrough,
header.ThreadsPerInputPrimitive,
header.OutputTopology,
header.MaxOutputVertexCount,
header.ImapTypes,
header.OmapTargets,
header.OmapSampleMask,
header.OmapDepth,
gpuAccessor.QueryHostSupportsScaledVertexFormats(),
transformFeedbackVecMap,
transformFeedbackOutputs);
}
internal static TransformFeedbackOutput[] GetTransformFeedbackOutputs(IGpuAccessor gpuAccessor, out ulong transformFeedbackVecMap)
{
bool transformFeedbackEnabled =
gpuAccessor.QueryTransformFeedbackEnabled() &&
gpuAccessor.QueryHostSupportsTransformFeedback();
TransformFeedbackOutput[] transformFeedbackOutputs = null;
transformFeedbackVecMap = 0UL;
if (transformFeedbackEnabled)
{
transformFeedbackOutputs = new TransformFeedbackOutput[0xc0];
for (int tfbIndex = 0; tfbIndex < 4; tfbIndex++)
{
ReadOnlySpan<byte> locations = gpuAccessor.QueryTransformFeedbackVaryingLocations(tfbIndex);
int stride = gpuAccessor.QueryTransformFeedbackStride(tfbIndex);
for (int i = 0; i < locations.Length; i++)
{
byte wordOffset = locations[i];
if (wordOffset < 0xc0)
{
transformFeedbackOutputs[wordOffset] = new TransformFeedbackOutput(tfbIndex, i * 4, stride);
transformFeedbackVecMap |= 1UL << (wordOffset / 4);
}
}
}
}
return transformFeedbackOutputs;
}
private static int GetLocalMemorySize(ShaderHeader header)
{
return header.ShaderLocalMemoryLowSize + header.ShaderLocalMemoryHighSize + (header.ShaderLocalMemoryCrsSize / ThreadsPerWarp);
}
internal static FunctionCode[] EmitShader(
TranslatorContext translatorContext,
ResourceManager resourceManager,
DecodedProgram program,
bool vertexAsCompute,
bool initializeOutputs,
out int initializationOperations)
{
initializationOperations = 0;
FunctionMatch.RunPass(program);
foreach (DecodedFunction function in program.Where(x => !x.IsCompilerGenerated).OrderBy(x => x.Address))
{
program.AddFunctionAndSetId(function);
}
FunctionCode[] functions = new FunctionCode[program.FunctionsWithIdCount];
for (int index = 0; index < functions.Length; index++)
{
EmitterContext context = new(translatorContext, resourceManager, program, vertexAsCompute, index != 0);
if (initializeOutputs && index == 0)
{
EmitOutputsInitialization(context, translatorContext.AttributeUsage, translatorContext.GpuAccessor, translatorContext.Stage);
initializationOperations = context.OperationsCount;
}
DecodedFunction function = program.GetFunctionById(index);
foreach (Block block in function.Blocks)
{
context.CurrBlock = block;
context.EnterBlock(block.Address);
EmitOps(context, block);
}
functions[index] = new(context.GetOperations());
}
return functions;
}
private static void EmitOutputsInitialization(EmitterContext context, AttributeUsage attributeUsage, IGpuAccessor gpuAccessor, ShaderStage stage)
{
// Compute has no output attributes, so we
// don't need to initialize outputs on that stage.
if (stage == ShaderStage.Compute)
{
return;
}
if (stage == ShaderStage.Fragment)
{
// Fragment is the last stage, so we don't need to
// initialize outputs unless we're using DSB, in which
// we need to make sure the ouput has a valid value.
if (gpuAccessor.QueryGraphicsState().DualSourceBlendEnable)
{
for (int i = 0; i < 4; i++)
{
context.Store(StorageKind.Output, IoVariable.FragmentOutputColor, null, Const(1), Const(i), ConstF(0));
}
}
return;
}
if (stage == ShaderStage.Vertex)
{
InitializeVertexOutputs(context);
}
UInt128 usedAttributes = context.TranslatorContext.AttributeUsage.NextInputAttributesComponents;
while (usedAttributes != UInt128.Zero)
{
int index = (int)UInt128.TrailingZeroCount(usedAttributes);
int vecIndex = index / 4;
usedAttributes &= ~(UInt128.One << index);
// We don't need to initialize passthrough attributes.
if ((context.TranslatorContext.AttributeUsage.PassthroughAttributes & (1 << vecIndex)) != 0)
{
continue;
}
InitializeOutputComponent(context, vecIndex, index & 3, perPatch: false);
}
if (context.TranslatorContext.AttributeUsage.NextUsedInputAttributesPerPatch != null)
{
foreach (int vecIndex in context.TranslatorContext.AttributeUsage.NextUsedInputAttributesPerPatch.Order())
{
InitializeOutput(context, vecIndex, perPatch: true);
}
}
if (attributeUsage.NextUsesFixedFuncAttributes)
{
bool supportsLayerFromVertexOrTess = gpuAccessor.QueryHostSupportsLayerVertexTessellation();
int fixedStartAttr = supportsLayerFromVertexOrTess ? 0 : 1;
for (int i = fixedStartAttr; i < fixedStartAttr + 5 + AttributeConsts.TexCoordCount; i++)
{
int index = attributeUsage.GetFreeUserAttribute(isOutput: true, i);
if (index < 0)
{
break;
}
InitializeOutput(context, index, perPatch: false);
}
}
}
private static void InitializeVertexOutputs(EmitterContext context)
{
for (int c = 0; c < 4; c++)
{
context.Store(StorageKind.Output, IoVariable.Position, null, Const(c), ConstF(c == 3 ? 1f : 0f));
}
if (context.Program.ClipDistancesWritten != 0)
{
for (int i = 0; i < 8; i++)
{
context.Store(StorageKind.Output, IoVariable.ClipDistance, null, Const(i), ConstF(0f));
}
}
}
private static void InitializeOutput(EmitterContext context, int location, bool perPatch)
{
for (int c = 0; c < 4; c++)
{
InitializeOutputComponent(context, location, c, perPatch);
}
}
private static void InitializeOutputComponent(EmitterContext context, int location, int c, bool perPatch)
{
StorageKind storageKind = perPatch ? StorageKind.OutputPerPatch : StorageKind.Output;
if (context.TranslatorContext.Definitions.OaIndexing)
{
Operand invocationId = null;
if (context.TranslatorContext.Definitions.Stage == ShaderStage.TessellationControl && !perPatch)
{
invocationId = context.Load(StorageKind.Input, IoVariable.InvocationId);
}
int index = location * 4 + c;
context.Store(storageKind, IoVariable.UserDefined, invocationId, Const(index), ConstF(c == 3 ? 1f : 0f));
}
else
{
if (context.TranslatorContext.Definitions.Stage == ShaderStage.TessellationControl && !perPatch)
{
Operand invocationId = context.Load(StorageKind.Input, IoVariable.InvocationId);
context.Store(storageKind, IoVariable.UserDefined, Const(location), invocationId, Const(c), ConstF(c == 3 ? 1f : 0f));
}
else
{
context.Store(storageKind, IoVariable.UserDefined, null, Const(location), Const(c), ConstF(c == 3 ? 1f : 0f));
}
}
}
private static void EmitOps(EmitterContext context, Block block)
{
for (int opIndex = 0; opIndex < block.OpCodes.Count; opIndex++)
{
InstOp op = block.OpCodes[opIndex];
if (context.TranslatorContext.Options.Flags.HasFlag(TranslationFlags.DebugMode))
{
string instName;
if (op.Emitter != null)
{
instName = op.Name.ToString();
}
else
{
instName = "???";
context.TranslatorContext.GpuAccessor.Log($"Invalid instruction at 0x{op.Address:X6} (0x{op.RawOpCode:X16}).");
}
string dbgComment = $"0x{op.Address:X6}: 0x{op.RawOpCode:X16} {instName}";
context.Add(new CommentNode(dbgComment));
}
InstConditional opConditional = new(op.RawOpCode);
bool noPred = op.Props.HasFlag(InstProps.NoPred);
if (!noPred && opConditional.Pred == RegisterConsts.PredicateTrueIndex && opConditional.PredInv)
{
continue;
}
Operand predSkipLbl = null;
if (Decoder.IsPopBranch(op.Name))
{
// If the instruction is a SYNC or BRK instruction with only one
// possible target address, then the instruction is basically
// just a simple branch, we can generate code similar to branch
// instructions, with the condition check on the branch itself.
noPred = block.SyncTargets.Count <= 1;
}
else if (op.Name == InstName.Bra)
{
noPred = true;
}
if (!(opConditional.Pred == RegisterConsts.PredicateTrueIndex || noPred))
{
Operand label;
if (opIndex == block.OpCodes.Count - 1 && block.HasNext())
{
label = context.GetLabel(block.Successors[0].Address);
}
else
{
label = Label();
predSkipLbl = label;
}
Operand pred = Register(opConditional.Pred, RegisterType.Predicate);
if (opConditional.PredInv)
{
context.BranchIfTrue(label, pred);
}
else
{
context.BranchIfFalse(label, pred);
}
}
context.CurrOp = op;
op.Emitter?.Invoke(context);
if (predSkipLbl != null)
{
context.MarkLabel(predSkipLbl);
}
}
}
}
}

View File

@@ -0,0 +1,674 @@
using Ryujinx.Graphics.Shader.CodeGen;
using Ryujinx.Graphics.Shader.CodeGen.Glsl;
using Ryujinx.Graphics.Shader.CodeGen.Spirv;
using Ryujinx.Graphics.Shader.Decoders;
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
using Ryujinx.Graphics.Shader.StructuredIr;
using Ryujinx.Graphics.Shader.Translation.Optimizations;
using Ryujinx.Graphics.Shader.Translation.Transforms;
using System;
using System.Collections.Generic;
using System.Numerics;
using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
using static Ryujinx.Graphics.Shader.Translation.Translator;
namespace Ryujinx.Graphics.Shader.Translation
{
public class TranslatorContext
{
private readonly DecodedProgram _program;
private readonly int _localMemorySize;
private IoUsage _vertexOutput;
public ulong Address { get; }
public int Size { get; }
public int Cb1DataSize => _program.Cb1DataSize;
internal AttributeUsage AttributeUsage => _program.AttributeUsage;
internal ShaderDefinitions Definitions { get; }
public ShaderStage Stage => Definitions.Stage;
internal IGpuAccessor GpuAccessor { get; }
internal TranslationOptions Options { get; }
private bool IsTransformFeedbackEmulated => !GpuAccessor.QueryHostSupportsTransformFeedback() && GpuAccessor.QueryTransformFeedbackEnabled();
public bool HasStore => _program.UsedFeatures.HasFlag(FeatureFlags.Store) || (IsTransformFeedbackEmulated && Definitions.LastInVertexPipeline);
public bool LayerOutputWritten { get; private set; }
public int LayerOutputAttribute { get; private set; }
internal TranslatorContext(
ulong address,
int size,
int localMemorySize,
ShaderDefinitions definitions,
IGpuAccessor gpuAccessor,
TranslationOptions options,
DecodedProgram program)
{
Address = address;
Size = size;
_program = program;
_localMemorySize = localMemorySize;
_vertexOutput = new IoUsage(FeatureFlags.None, 0, -1);
Definitions = definitions;
GpuAccessor = gpuAccessor;
Options = options;
}
private static bool IsLoadUserDefined(Operation operation)
{
// TODO: Check if sources count match and all sources are constant.
return operation.Inst == Instruction.Load && (IoVariable)operation.GetSource(0).Value == IoVariable.UserDefined;
}
private static bool IsStoreUserDefined(Operation operation)
{
// TODO: Check if sources count match and all sources are constant.
return operation.Inst == Instruction.Store && (IoVariable)operation.GetSource(0).Value == IoVariable.UserDefined;
}
private static FunctionCode[] Combine(FunctionCode[] a, FunctionCode[] b, int aStart)
{
// Here we combine two shaders.
// For shader A:
// - All user attribute stores on shader A are turned into copies to a
// temporary variable. It's assumed that shader B will consume them.
// - All return instructions are turned into branch instructions, the
// branch target being the start of the shader B code.
// For shader B:
// - All user attribute loads on shader B are turned into copies from a
// temporary variable, as long that attribute is written by shader A.
FunctionCode[] output = new FunctionCode[a.Length + b.Length - 1];
List<Operation> ops = new(a.Length + b.Length);
Operand[] temps = new Operand[AttributeConsts.UserAttributesCount * 4];
Operand lblB = Label();
for (int index = aStart; index < a[0].Code.Length; index++)
{
Operation operation = a[0].Code[index];
if (IsStoreUserDefined(operation))
{
int tIndex = operation.GetSource(1).Value * 4 + operation.GetSource(2).Value;
Operand temp = temps[tIndex];
if (temp == null)
{
temp = Local();
temps[tIndex] = temp;
}
operation.Dest = temp;
operation.TurnIntoCopy(operation.GetSource(operation.SourcesCount - 1));
}
if (operation.Inst == Instruction.Return)
{
ops.Add(new Operation(Instruction.Branch, lblB));
}
else
{
ops.Add(operation);
}
}
ops.Add(new Operation(Instruction.MarkLabel, lblB));
for (int index = 0; index < b[0].Code.Length; index++)
{
Operation operation = b[0].Code[index];
if (IsLoadUserDefined(operation))
{
int tIndex = operation.GetSource(1).Value * 4 + operation.GetSource(2).Value;
Operand temp = temps[tIndex];
if (temp != null)
{
operation.TurnIntoCopy(temp);
}
}
ops.Add(operation);
}
output[0] = new FunctionCode(ops.ToArray());
for (int i = 1; i < a.Length; i++)
{
output[i] = a[i];
}
for (int i = 1; i < b.Length; i++)
{
output[a.Length + i - 1] = b[i];
}
return output;
}
internal int GetDepthRegister()
{
// The depth register is always two registers after the last color output.
return BitOperations.PopCount((uint)Definitions.OmapTargets) + 1;
}
public void SetLayerOutputAttribute(int attr)
{
LayerOutputWritten = true;
LayerOutputAttribute = attr;
}
public void SetLastInVertexPipeline()
{
Definitions.LastInVertexPipeline = true;
}
public void SetNextStage(TranslatorContext nextStage)
{
AttributeUsage.MergeFromtNextStage(
Definitions.GpPassthrough,
nextStage._program.UsedFeatures.HasFlag(FeatureFlags.FixedFuncAttr),
nextStage.AttributeUsage);
// We don't consider geometry shaders using the geometry shader passthrough feature
// as being the last because when this feature is used, it can't actually modify any of the outputs,
// so the stage that comes before it is the last one that can do modifications.
if (nextStage.Definitions.Stage != ShaderStage.Fragment &&
(nextStage.Definitions.Stage != ShaderStage.Geometry || !nextStage.Definitions.GpPassthrough))
{
Definitions.LastInVertexPipeline = false;
}
}
public ShaderProgram Translate(bool asCompute = false)
{
ResourceManager resourceManager = CreateResourceManager(asCompute);
bool usesLocalMemory = _program.UsedFeatures.HasFlag(FeatureFlags.LocalMemory);
resourceManager.SetCurrentLocalMemory(_localMemorySize, usesLocalMemory);
if (Stage == ShaderStage.Compute)
{
bool usesSharedMemory = _program.UsedFeatures.HasFlag(FeatureFlags.SharedMemory);
resourceManager.SetCurrentSharedMemory(GpuAccessor.QueryComputeSharedMemorySize(), usesSharedMemory);
}
FunctionCode[] code = EmitShader(this, resourceManager, _program, asCompute, initializeOutputs: true, out _);
return Translate(code, resourceManager, _program.UsedFeatures, _program.ClipDistancesWritten, asCompute);
}
public ShaderProgram Translate(TranslatorContext other, bool asCompute = false)
{
ResourceManager resourceManager = CreateResourceManager(asCompute);
bool usesLocalMemory = _program.UsedFeatures.HasFlag(FeatureFlags.LocalMemory);
resourceManager.SetCurrentLocalMemory(_localMemorySize, usesLocalMemory);
FunctionCode[] code = EmitShader(this, resourceManager, _program, asCompute, initializeOutputs: false, out _);
bool otherUsesLocalMemory = other._program.UsedFeatures.HasFlag(FeatureFlags.LocalMemory);
resourceManager.SetCurrentLocalMemory(other._localMemorySize, otherUsesLocalMemory);
FunctionCode[] otherCode = EmitShader(other, resourceManager, other._program, asCompute, initializeOutputs: true, out int aStart);
code = Combine(otherCode, code, aStart);
return Translate(
code,
resourceManager,
_program.UsedFeatures | other._program.UsedFeatures,
(byte)(_program.ClipDistancesWritten | other._program.ClipDistancesWritten),
asCompute);
}
private ShaderProgram Translate(FunctionCode[] functions, ResourceManager resourceManager, FeatureFlags usedFeatures, byte clipDistancesWritten, bool asCompute)
{
if (asCompute)
{
usedFeatures |= FeatureFlags.VtgAsCompute;
}
ControlFlowGraph[] cfgs = new ControlFlowGraph[functions.Length];
RegisterUsage.FunctionRegisterUsage[] frus = new RegisterUsage.FunctionRegisterUsage[functions.Length];
for (int i = 0; i < functions.Length; i++)
{
cfgs[i] = ControlFlowGraph.Create(functions[i].Code);
if (i != 0)
{
frus[i] = RegisterUsage.RunPass(cfgs[i]);
}
}
List<Function> funcs = new(functions.Length);
for (int i = 0; i < functions.Length; i++)
{
funcs.Add(null);
}
HelperFunctionManager hfm = new(funcs, Definitions.Stage);
for (int i = 0; i < functions.Length; i++)
{
ControlFlowGraph cfg = cfgs[i];
int inArgumentsCount = 0;
int outArgumentsCount = 0;
if (i != 0)
{
RegisterUsage.FunctionRegisterUsage fru = frus[i];
inArgumentsCount = fru.InArguments.Length;
outArgumentsCount = fru.OutArguments.Length;
}
if (cfg.Blocks.Length != 0)
{
RegisterUsage.FixupCalls(cfg.Blocks, frus);
Dominance.FindDominators(cfg);
Dominance.FindDominanceFrontiers(cfg.Blocks);
Ssa.Rename(cfg.Blocks);
TransformContext context = new(
hfm,
cfg.Blocks,
Definitions,
resourceManager,
GpuAccessor,
Options.TargetApi,
Options.TargetLanguage,
Definitions.Stage,
ref usedFeatures);
Optimizer.RunPass(context);
TransformPasses.RunPass(context);
}
funcs[i] = new Function(cfg.Blocks, $"fun{i}", false, inArgumentsCount, outArgumentsCount);
}
return Generate(
funcs,
AttributeUsage,
GetDefinitions(asCompute),
Definitions,
resourceManager,
usedFeatures,
clipDistancesWritten);
}
private ShaderProgram Generate(
IReadOnlyList<Function> funcs,
AttributeUsage attributeUsage,
ShaderDefinitions definitions,
ShaderDefinitions originalDefinitions,
ResourceManager resourceManager,
FeatureFlags usedFeatures,
byte clipDistancesWritten)
{
StructuredProgramInfo sInfo = StructuredProgram.MakeStructuredProgram(
funcs,
attributeUsage,
definitions,
resourceManager,
Options.TargetLanguage,
Options.Flags.HasFlag(TranslationFlags.DebugMode));
int geometryVerticesPerPrimitive = Definitions.OutputTopology switch
{
OutputTopology.LineStrip => 2,
OutputTopology.TriangleStrip => 3,
_ => 1
};
ShaderProgramInfo info = new ShaderProgramInfo(
resourceManager.GetConstantBufferDescriptors(),
resourceManager.GetStorageBufferDescriptors(),
resourceManager.GetTextureDescriptors(),
resourceManager.GetImageDescriptors(),
originalDefinitions.Stage,
geometryVerticesPerPrimitive,
originalDefinitions.MaxOutputVertices,
originalDefinitions.ThreadsPerInputPrimitive,
usedFeatures.HasFlag(FeatureFlags.FragCoordXY),
usedFeatures.HasFlag(FeatureFlags.InstanceId),
usedFeatures.HasFlag(FeatureFlags.DrawParameters),
usedFeatures.HasFlag(FeatureFlags.RtLayer),
clipDistancesWritten,
originalDefinitions.OmapTargets);
HostCapabilities hostCapabilities = new HostCapabilities(
GpuAccessor.QueryHostReducedPrecision(),
GpuAccessor.QueryHostSupportsFragmentShaderInterlock(),
GpuAccessor.QueryHostSupportsFragmentShaderOrderingIntel(),
GpuAccessor.QueryHostSupportsGeometryShaderPassthrough(),
GpuAccessor.QueryHostSupportsShaderBallot(),
GpuAccessor.QueryHostSupportsShaderBarrierDivergence(),
GpuAccessor.QueryHostSupportsShaderFloat64(),
GpuAccessor.QueryHostSupportsShaderNonUniformIndexing(),
GpuAccessor.QueryHostSupportsTextureShadowLod(),
GpuAccessor.QueryHostSupportsViewportMask());
CodeGenParameters parameters = new CodeGenParameters(attributeUsage, definitions, resourceManager.Properties, hostCapabilities, GpuAccessor, Options.TargetApi);
return Options.TargetLanguage switch
{
TargetLanguage.Glsl => new ShaderProgram(info, TargetLanguage.Glsl, GlslGenerator.Generate(sInfo, parameters)),
TargetLanguage.Spirv => new ShaderProgram(info, TargetLanguage.Spirv, SpirvGenerator.Generate(sInfo, parameters)),
_ => throw new NotImplementedException(Options.TargetLanguage.ToString()),
};
}
private ResourceManager CreateResourceManager(bool vertexAsCompute)
{
ResourceManager resourceManager = new(Definitions.Stage, GpuAccessor, GetResourceReservations());
if (IsTransformFeedbackEmulated)
{
StructureType tfeDataStruct = new(new StructureField[]
{
new(AggregateType.Array | AggregateType.U32, "data", 0)
});
for (int i = 0; i < ResourceReservations.TfeBuffersCount; i++)
{
int binding = resourceManager.Reservations.GetTfeBufferStorageBufferBinding(i);
BufferDefinition tfeDataBuffer = new(BufferLayout.Std430, 1, binding, $"tfe_data{i}", tfeDataStruct);
resourceManager.Properties.AddOrUpdateStorageBuffer(tfeDataBuffer);
}
}
if (vertexAsCompute)
{
int vertexInfoCbBinding = resourceManager.Reservations.VertexInfoConstantBufferBinding;
BufferDefinition vertexInfoBuffer = new(BufferLayout.Std140, 0, vertexInfoCbBinding, "vb_info", VertexInfoBuffer.GetStructureType());
resourceManager.Properties.AddOrUpdateConstantBuffer(vertexInfoBuffer);
StructureType vertexOutputStruct = new(new StructureField[]
{
new(AggregateType.Array | AggregateType.FP32, "data", 0)
});
int vertexOutputSbBinding = resourceManager.Reservations.VertexOutputStorageBufferBinding;
BufferDefinition vertexOutputBuffer = new(BufferLayout.Std430, 1, vertexOutputSbBinding, "vertex_output", vertexOutputStruct);
resourceManager.Properties.AddOrUpdateStorageBuffer(vertexOutputBuffer);
if (Stage == ShaderStage.Vertex)
{
SetBindingPair ibSetAndBinding = resourceManager.Reservations.GetIndexBufferTextureSetAndBinding();
TextureDefinition indexBuffer = new(ibSetAndBinding.SetIndex, ibSetAndBinding.Binding, "ib_data", SamplerType.TextureBuffer);
resourceManager.Properties.AddOrUpdateTexture(indexBuffer);
int inputMap = _program.AttributeUsage.UsedInputAttributes;
while (inputMap != 0)
{
int location = BitOperations.TrailingZeroCount(inputMap);
SetBindingPair setAndBinding = resourceManager.Reservations.GetVertexBufferTextureSetAndBinding(location);
TextureDefinition vaBuffer = new(setAndBinding.SetIndex, setAndBinding.Binding, $"vb_data{location}", SamplerType.TextureBuffer);
resourceManager.Properties.AddOrUpdateTexture(vaBuffer);
inputMap &= ~(1 << location);
}
}
else if (Stage == ShaderStage.Geometry)
{
SetBindingPair trbSetAndBinding = resourceManager.Reservations.GetTopologyRemapBufferTextureSetAndBinding();
TextureDefinition remapBuffer = new(trbSetAndBinding.SetIndex, trbSetAndBinding.Binding, "trb_data", SamplerType.TextureBuffer);
resourceManager.Properties.AddOrUpdateTexture(remapBuffer);
int geometryVbOutputSbBinding = resourceManager.Reservations.GeometryVertexOutputStorageBufferBinding;
BufferDefinition geometryVbOutputBuffer = new(BufferLayout.Std430, 1, geometryVbOutputSbBinding, "geometry_vb_output", vertexOutputStruct);
resourceManager.Properties.AddOrUpdateStorageBuffer(geometryVbOutputBuffer);
StructureType geometryIbOutputStruct = new(new StructureField[]
{
new(AggregateType.Array | AggregateType.U32, "data", 0)
});
int geometryIbOutputSbBinding = resourceManager.Reservations.GeometryIndexOutputStorageBufferBinding;
BufferDefinition geometryIbOutputBuffer = new(BufferLayout.Std430, 1, geometryIbOutputSbBinding, "geometry_ib_output", geometryIbOutputStruct);
resourceManager.Properties.AddOrUpdateStorageBuffer(geometryIbOutputBuffer);
}
resourceManager.SetVertexAsComputeLocalMemories(Definitions.Stage, Definitions.InputTopology);
}
return resourceManager;
}
private ShaderDefinitions GetDefinitions(bool vertexAsCompute)
{
if (vertexAsCompute)
{
return new ShaderDefinitions(ShaderStage.Compute, 32, 32, 1);
}
else
{
return Definitions;
}
}
public ResourceReservations GetResourceReservations()
{
IoUsage ioUsage = _program.GetIoUsage();
if (Definitions.GpPassthrough)
{
ioUsage = ioUsage.Combine(_vertexOutput);
}
return new ResourceReservations(GpuAccessor, IsTransformFeedbackEmulated, vertexAsCompute: true, _vertexOutput, ioUsage);
}
public void SetVertexOutputMapForGeometryAsCompute(TranslatorContext vertexContext)
{
_vertexOutput = vertexContext._program.GetIoUsage();
}
public ShaderProgram GenerateVertexPassthroughForCompute()
{
AttributeUsage attributeUsage = new AttributeUsage(GpuAccessor);
ResourceManager resourceManager = new ResourceManager(ShaderStage.Vertex, GpuAccessor);
ResourceReservations reservations = GetResourceReservations();
int vertexInfoCbBinding = reservations.VertexInfoConstantBufferBinding;
if (Stage == ShaderStage.Vertex)
{
BufferDefinition vertexInfoBuffer = new(BufferLayout.Std140, 0, vertexInfoCbBinding, "vb_info", VertexInfoBuffer.GetStructureType());
resourceManager.Properties.AddOrUpdateConstantBuffer(vertexInfoBuffer);
}
StructureType vertexInputStruct = new(new StructureField[]
{
new(AggregateType.Array | AggregateType.FP32, "data", 0)
});
int vertexDataSbBinding = reservations.VertexOutputStorageBufferBinding;
BufferDefinition vertexOutputBuffer = new(BufferLayout.Std430, 1, vertexDataSbBinding, "vb_input", vertexInputStruct);
resourceManager.Properties.AddOrUpdateStorageBuffer(vertexOutputBuffer);
EmitterContext context = new EmitterContext();
Operand vertexIndex = Options.TargetApi == TargetApi.OpenGL
? context.Load(StorageKind.Input, IoVariable.VertexId)
: context.Load(StorageKind.Input, IoVariable.VertexIndex);
if (Stage == ShaderStage.Vertex)
{
Operand vertexCount = context.Load(StorageKind.ConstantBuffer, vertexInfoCbBinding, Const((int)VertexInfoBufferField.VertexCounts), Const(0));
// Base instance will be always zero when this shader is used, so which one we use here doesn't really matter.
Operand instanceId = Options.TargetApi == TargetApi.OpenGL
? context.Load(StorageKind.Input, IoVariable.InstanceId)
: context.Load(StorageKind.Input, IoVariable.InstanceIndex);
vertexIndex = context.IAdd(context.IMultiply(instanceId, vertexCount), vertexIndex);
}
Operand baseOffset = context.IMultiply(vertexIndex, Const(reservations.OutputSizePerInvocation));
foreach ((IoDefinition ioDefinition, int inputOffset) in reservations.Offsets)
{
if (ioDefinition.StorageKind != StorageKind.Output)
{
continue;
}
Operand vertexOffset = inputOffset != 0 ? context.IAdd(baseOffset, Const(inputOffset)) : baseOffset;
Operand value = context.Load(StorageKind.StorageBuffer, vertexDataSbBinding, Const(0), vertexOffset);
if (ioDefinition.IoVariable == IoVariable.UserDefined)
{
context.Store(StorageKind.Output, ioDefinition.IoVariable, null, Const(ioDefinition.Location), Const(ioDefinition.Component), value);
attributeUsage.SetOutputUserAttribute(ioDefinition.Location);
}
else if (ResourceReservations.IsVectorOrArrayVariable(ioDefinition.IoVariable))
{
context.Store(StorageKind.Output, ioDefinition.IoVariable, null, Const(ioDefinition.Component), value);
}
else
{
context.Store(StorageKind.Output, ioDefinition.IoVariable, null, value);
}
}
Operation[] operations = context.GetOperations();
ControlFlowGraph cfg = ControlFlowGraph.Create(operations);
Function function = new Function(cfg.Blocks, "main", false, 0, 0);
TransformFeedbackOutput[] transformFeedbackOutputs = GetTransformFeedbackOutputs(GpuAccessor, out ulong transformFeedbackVecMap);
ShaderDefinitions definitions = new ShaderDefinitions(ShaderStage.Vertex, transformFeedbackVecMap, transformFeedbackOutputs)
{
LastInVertexPipeline = true
};
return Generate(
new[] { function },
attributeUsage,
definitions,
definitions,
resourceManager,
FeatureFlags.None,
0);
}
public ShaderProgram GenerateGeometryPassthrough()
{
int outputAttributesMask = AttributeUsage.UsedOutputAttributes;
int layerOutputAttr = LayerOutputAttribute;
if (LayerOutputWritten)
{
outputAttributesMask |= 1 << ((layerOutputAttr - AttributeConsts.UserAttributeBase) / 16);
}
OutputTopology outputTopology;
int maxOutputVertices;
switch (Definitions.InputTopology)
{
case InputTopology.Points:
outputTopology = OutputTopology.PointList;
maxOutputVertices = 1;
break;
case InputTopology.Lines:
case InputTopology.LinesAdjacency:
outputTopology = OutputTopology.LineStrip;
maxOutputVertices = 2;
break;
default:
outputTopology = OutputTopology.TriangleStrip;
maxOutputVertices = 3;
break;
}
AttributeUsage attributeUsage = new AttributeUsage(GpuAccessor);
ResourceManager resourceManager = new ResourceManager(ShaderStage.Geometry, GpuAccessor);
EmitterContext context = new EmitterContext();
for (int v = 0; v < maxOutputVertices; v++)
{
int outAttrsMask = outputAttributesMask;
while (outAttrsMask != 0)
{
int attrIndex = BitOperations.TrailingZeroCount(outAttrsMask);
outAttrsMask &= ~(1 << attrIndex);
for (int c = 0; c < 4; c++)
{
int attr = AttributeConsts.UserAttributeBase + attrIndex * 16 + c * 4;
Operand value = context.Load(StorageKind.Input, IoVariable.UserDefined, Const(attrIndex), Const(v), Const(c));
if (attr == layerOutputAttr)
{
context.Store(StorageKind.Output, IoVariable.Layer, null, value);
}
else
{
context.Store(StorageKind.Output, IoVariable.UserDefined, null, Const(attrIndex), Const(c), value);
}
}
}
for (int c = 0; c < 4; c++)
{
Operand value = context.Load(StorageKind.Input, IoVariable.Position, Const(v), Const(c));
context.Store(StorageKind.Output, IoVariable.Position, null, Const(c), value);
}
context.EmitVertex();
}
context.EndPrimitive();
Operation[] operations = context.GetOperations();
ControlFlowGraph cfg = ControlFlowGraph.Create(operations);
Function function = new Function(cfg.Blocks, "main", false, 0, 0);
ShaderDefinitions definitions = new ShaderDefinitions(
ShaderStage.Geometry,
GpuAccessor.QueryGraphicsState(),
false,
1,
outputTopology,
maxOutputVertices);
return Generate(
new[] { function },
attributeUsage,
definitions,
definitions,
resourceManager,
FeatureFlags.RtLayer,
0);
}
}
}