mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-20 18:59:58 -04:00
Import the base tree (MIT, see LICENSE)
This commit is contained in:
35
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/AstAssignment.cs
vendored
Normal file
35
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/AstAssignment.cs
vendored
Normal file
@@ -0,0 +1,35 @@
|
||||
using static Ryujinx.Graphics.Shader.StructuredIr.AstHelper;
|
||||
|
||||
namespace Ryujinx.Graphics.Shader.StructuredIr
|
||||
{
|
||||
class AstAssignment : AstNode
|
||||
{
|
||||
public IAstNode Destination { get; }
|
||||
|
||||
private IAstNode _source;
|
||||
|
||||
public IAstNode Source
|
||||
{
|
||||
get
|
||||
{
|
||||
return _source;
|
||||
}
|
||||
set
|
||||
{
|
||||
RemoveUse(_source, this);
|
||||
|
||||
AddUse(value, this);
|
||||
|
||||
_source = value;
|
||||
}
|
||||
}
|
||||
|
||||
public AstAssignment(IAstNode destination, IAstNode source)
|
||||
{
|
||||
Destination = destination;
|
||||
Source = source;
|
||||
|
||||
AddDef(destination, this);
|
||||
}
|
||||
}
|
||||
}
|
||||
117
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/AstBlock.cs
vendored
Normal file
117
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/AstBlock.cs
vendored
Normal file
@@ -0,0 +1,117 @@
|
||||
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using static Ryujinx.Graphics.Shader.StructuredIr.AstHelper;
|
||||
|
||||
namespace Ryujinx.Graphics.Shader.StructuredIr
|
||||
{
|
||||
class AstBlock : AstNode, IEnumerable<IAstNode>
|
||||
{
|
||||
public AstBlockType Type { get; private set; }
|
||||
|
||||
private IAstNode _condition;
|
||||
|
||||
public IAstNode Condition
|
||||
{
|
||||
get
|
||||
{
|
||||
return _condition;
|
||||
}
|
||||
set
|
||||
{
|
||||
RemoveUse(_condition, this);
|
||||
|
||||
AddUse(value, this);
|
||||
|
||||
_condition = value;
|
||||
}
|
||||
}
|
||||
|
||||
private readonly LinkedList<IAstNode> _nodes;
|
||||
|
||||
public IAstNode First => _nodes.First?.Value;
|
||||
public IAstNode Last => _nodes.Last?.Value;
|
||||
|
||||
public int Count => _nodes.Count;
|
||||
|
||||
public AstBlock(AstBlockType type, IAstNode condition = null)
|
||||
{
|
||||
Type = type;
|
||||
Condition = condition;
|
||||
|
||||
_nodes = [];
|
||||
}
|
||||
|
||||
public void Add(IAstNode node)
|
||||
{
|
||||
Add(node, _nodes.AddLast(node));
|
||||
}
|
||||
|
||||
public void AddFirst(IAstNode node)
|
||||
{
|
||||
Add(node, _nodes.AddFirst(node));
|
||||
}
|
||||
|
||||
public void AddBefore(IAstNode next, IAstNode node)
|
||||
{
|
||||
Add(node, _nodes.AddBefore(next.LLNode, node));
|
||||
}
|
||||
|
||||
public void AddAfter(IAstNode prev, IAstNode node)
|
||||
{
|
||||
Add(node, _nodes.AddAfter(prev.LLNode, node));
|
||||
}
|
||||
|
||||
private void Add(IAstNode node, LinkedListNode<IAstNode> newNode)
|
||||
{
|
||||
if (node.Parent != null)
|
||||
{
|
||||
throw new ArgumentException("Node already belongs to a block.");
|
||||
}
|
||||
|
||||
node.Parent = this;
|
||||
node.LLNode = newNode;
|
||||
}
|
||||
|
||||
public void Remove(IAstNode node)
|
||||
{
|
||||
_nodes.Remove(node.LLNode);
|
||||
|
||||
node.Parent = null;
|
||||
node.LLNode = null;
|
||||
}
|
||||
|
||||
public void AndCondition(IAstNode cond)
|
||||
{
|
||||
Condition = new AstOperation(Instruction.LogicalAnd, Condition, cond);
|
||||
}
|
||||
|
||||
public void OrCondition(IAstNode cond)
|
||||
{
|
||||
Condition = new AstOperation(Instruction.LogicalOr, Condition, cond);
|
||||
}
|
||||
public void TurnIntoIf(IAstNode cond)
|
||||
{
|
||||
Condition = cond;
|
||||
|
||||
Type = AstBlockType.If;
|
||||
}
|
||||
|
||||
public void TurnIntoElseIf()
|
||||
{
|
||||
Type = AstBlockType.ElseIf;
|
||||
}
|
||||
|
||||
public IEnumerator<IAstNode> GetEnumerator()
|
||||
{
|
||||
return _nodes.GetEnumerator();
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
}
|
||||
}
|
||||
12
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/AstBlockType.cs
vendored
Normal file
12
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/AstBlockType.cs
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
namespace Ryujinx.Graphics.Shader.StructuredIr
|
||||
{
|
||||
enum AstBlockType
|
||||
{
|
||||
DoWhile,
|
||||
If,
|
||||
Else,
|
||||
ElseIf,
|
||||
Main,
|
||||
While,
|
||||
}
|
||||
}
|
||||
68
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/AstBlockVisitor.cs
vendored
Normal file
68
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/AstBlockVisitor.cs
vendored
Normal file
@@ -0,0 +1,68 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using static Ryujinx.Graphics.Shader.StructuredIr.AstHelper;
|
||||
|
||||
namespace Ryujinx.Graphics.Shader.StructuredIr
|
||||
{
|
||||
class AstBlockVisitor
|
||||
{
|
||||
public AstBlock Block { get; private set; }
|
||||
|
||||
public class BlockVisitationEventArgs : EventArgs
|
||||
{
|
||||
public AstBlock Block { get; }
|
||||
|
||||
public BlockVisitationEventArgs(AstBlock block)
|
||||
{
|
||||
Block = block;
|
||||
}
|
||||
}
|
||||
|
||||
public event EventHandler<BlockVisitationEventArgs> BlockEntered;
|
||||
public event EventHandler<BlockVisitationEventArgs> BlockLeft;
|
||||
|
||||
public AstBlockVisitor(AstBlock mainBlock)
|
||||
{
|
||||
Block = mainBlock;
|
||||
}
|
||||
|
||||
public IEnumerable<IAstNode> Visit()
|
||||
{
|
||||
IAstNode node = Block.First;
|
||||
|
||||
while (node != null)
|
||||
{
|
||||
// We reached a child block, visit the nodes inside.
|
||||
while (node is AstBlock childBlock)
|
||||
{
|
||||
Block = childBlock;
|
||||
|
||||
node = childBlock.First;
|
||||
|
||||
BlockEntered?.Invoke(this, new BlockVisitationEventArgs(Block));
|
||||
}
|
||||
|
||||
// Node may be null, if the block is empty.
|
||||
if (node != null)
|
||||
{
|
||||
IAstNode next = Next(node);
|
||||
|
||||
yield return node;
|
||||
|
||||
node = next;
|
||||
}
|
||||
|
||||
// We reached the end of the list, go up on tree to the parent blocks.
|
||||
while (node == null && Block.Type != AstBlockType.Main)
|
||||
{
|
||||
BlockLeft?.Invoke(this, new BlockVisitationEventArgs(Block));
|
||||
|
||||
node = Next(Block);
|
||||
|
||||
Block = Block.Parent;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
12
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/AstComment.cs
vendored
Normal file
12
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/AstComment.cs
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
namespace Ryujinx.Graphics.Shader.StructuredIr
|
||||
{
|
||||
class AstComment : AstNode
|
||||
{
|
||||
public string Comment { get; }
|
||||
|
||||
public AstComment(string comment)
|
||||
{
|
||||
Comment = comment;
|
||||
}
|
||||
}
|
||||
}
|
||||
75
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/AstHelper.cs
vendored
Normal file
75
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/AstHelper.cs
vendored
Normal file
@@ -0,0 +1,75 @@
|
||||
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
|
||||
using Ryujinx.Graphics.Shader.Translation;
|
||||
|
||||
namespace Ryujinx.Graphics.Shader.StructuredIr
|
||||
{
|
||||
static class AstHelper
|
||||
{
|
||||
public static void AddUse(IAstNode node, IAstNode parent)
|
||||
{
|
||||
if (node is AstOperand operand && operand.Type == OperandType.LocalVariable)
|
||||
{
|
||||
operand.Uses.Add(parent);
|
||||
}
|
||||
}
|
||||
|
||||
public static void AddDef(IAstNode node, IAstNode parent)
|
||||
{
|
||||
if (node is AstOperand operand && operand.Type == OperandType.LocalVariable)
|
||||
{
|
||||
operand.Defs.Add(parent);
|
||||
}
|
||||
}
|
||||
|
||||
public static void RemoveUse(IAstNode node, IAstNode parent)
|
||||
{
|
||||
if (node is AstOperand operand && operand.Type == OperandType.LocalVariable)
|
||||
{
|
||||
operand.Uses.Remove(parent);
|
||||
}
|
||||
}
|
||||
|
||||
public static void RemoveDef(IAstNode node, IAstNode parent)
|
||||
{
|
||||
if (node is AstOperand operand && operand.Type == OperandType.LocalVariable)
|
||||
{
|
||||
operand.Defs.Remove(parent);
|
||||
}
|
||||
}
|
||||
|
||||
public static AstAssignment Assign(IAstNode destination, IAstNode source)
|
||||
{
|
||||
return new AstAssignment(destination, source);
|
||||
}
|
||||
|
||||
public static AstOperand Const(int value)
|
||||
{
|
||||
return new AstOperand(OperandType.Constant, value);
|
||||
}
|
||||
|
||||
public static AstOperand Local(AggregateType type)
|
||||
{
|
||||
AstOperand local = new(OperandType.LocalVariable)
|
||||
{
|
||||
VarType = type,
|
||||
};
|
||||
|
||||
return local;
|
||||
}
|
||||
|
||||
public static IAstNode InverseCond(IAstNode cond)
|
||||
{
|
||||
return new AstOperation(Instruction.LogicalNot, cond);
|
||||
}
|
||||
|
||||
public static IAstNode Next(IAstNode node)
|
||||
{
|
||||
return node.LLNode.Next?.Value;
|
||||
}
|
||||
|
||||
public static IAstNode Previous(IAstNode node)
|
||||
{
|
||||
return node.LLNode.Previous?.Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/AstNode.cs
vendored
Normal file
11
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/AstNode.cs
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Ryujinx.Graphics.Shader.StructuredIr
|
||||
{
|
||||
class AstNode : IAstNode
|
||||
{
|
||||
public AstBlock Parent { get; set; }
|
||||
|
||||
public LinkedListNode<IAstNode> LLNode { get; set; }
|
||||
}
|
||||
}
|
||||
38
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/AstOperand.cs
vendored
Normal file
38
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/AstOperand.cs
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
|
||||
using Ryujinx.Graphics.Shader.Translation;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Ryujinx.Graphics.Shader.StructuredIr
|
||||
{
|
||||
class AstOperand : AstNode
|
||||
{
|
||||
public HashSet<IAstNode> Defs { get; }
|
||||
public HashSet<IAstNode> Uses { get; }
|
||||
|
||||
public OperandType Type { get; }
|
||||
|
||||
public AggregateType VarType { get; set; }
|
||||
|
||||
public int Value { get; }
|
||||
|
||||
private AstOperand()
|
||||
{
|
||||
Defs = [];
|
||||
Uses = [];
|
||||
|
||||
VarType = AggregateType.S32;
|
||||
}
|
||||
|
||||
public AstOperand(Operand operand) : this()
|
||||
{
|
||||
Type = operand.Type;
|
||||
Value = operand.Value;
|
||||
}
|
||||
|
||||
public AstOperand(OperandType type, int value = 0) : this()
|
||||
{
|
||||
Type = type;
|
||||
Value = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
94
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/AstOperation.cs
vendored
Normal file
94
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/AstOperation.cs
vendored
Normal file
@@ -0,0 +1,94 @@
|
||||
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
|
||||
using Ryujinx.Graphics.Shader.Translation;
|
||||
using System.Numerics;
|
||||
|
||||
using static Ryujinx.Graphics.Shader.StructuredIr.AstHelper;
|
||||
|
||||
namespace Ryujinx.Graphics.Shader.StructuredIr
|
||||
{
|
||||
class AstOperation : AstNode
|
||||
{
|
||||
public Instruction Inst { get; }
|
||||
public StorageKind StorageKind { get; }
|
||||
public bool ForcePrecise { get; }
|
||||
|
||||
public int Index { get; }
|
||||
|
||||
private readonly IAstNode[] _sources;
|
||||
|
||||
public int SourcesCount => _sources.Length;
|
||||
|
||||
public AstOperation(Instruction inst, StorageKind storageKind, bool forcePrecise, IAstNode[] sources, int sourcesCount)
|
||||
{
|
||||
Inst = inst;
|
||||
StorageKind = storageKind;
|
||||
ForcePrecise = forcePrecise;
|
||||
_sources = sources;
|
||||
|
||||
for (int index = 0; index < sources.Length; index++)
|
||||
{
|
||||
if (index < sourcesCount)
|
||||
{
|
||||
AddUse(sources[index], this);
|
||||
}
|
||||
else
|
||||
{
|
||||
AddDef(sources[index], this);
|
||||
}
|
||||
}
|
||||
|
||||
Index = 0;
|
||||
}
|
||||
|
||||
public AstOperation(
|
||||
Instruction inst,
|
||||
StorageKind storageKind,
|
||||
bool forcePrecise,
|
||||
int index,
|
||||
IAstNode[] sources,
|
||||
int sourcesCount) : this(inst, storageKind, forcePrecise, sources, sourcesCount)
|
||||
{
|
||||
Index = index;
|
||||
}
|
||||
|
||||
public AstOperation(Instruction inst, params IAstNode[] sources) : this(inst, StorageKind.None, false, sources, sources.Length)
|
||||
{
|
||||
}
|
||||
|
||||
public IAstNode GetSource(int index)
|
||||
{
|
||||
return _sources[index];
|
||||
}
|
||||
|
||||
public void SetSource(int index, IAstNode source)
|
||||
{
|
||||
RemoveUse(_sources[index], this);
|
||||
|
||||
AddUse(source, this);
|
||||
|
||||
_sources[index] = source;
|
||||
}
|
||||
|
||||
public AggregateType GetVectorType(AggregateType scalarType)
|
||||
{
|
||||
int componentsCount = BitOperations.PopCount((uint)Index);
|
||||
|
||||
AggregateType type = scalarType;
|
||||
|
||||
switch (componentsCount)
|
||||
{
|
||||
case 2:
|
||||
type |= AggregateType.Vector2;
|
||||
break;
|
||||
case 3:
|
||||
type |= AggregateType.Vector3;
|
||||
break;
|
||||
case 4:
|
||||
type |= AggregateType.Vector4;
|
||||
break;
|
||||
}
|
||||
|
||||
return type;
|
||||
}
|
||||
}
|
||||
}
|
||||
154
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/AstOptimizer.cs
vendored
Normal file
154
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/AstOptimizer.cs
vendored
Normal file
@@ -0,0 +1,154 @@
|
||||
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
using static Ryujinx.Graphics.Shader.StructuredIr.AstHelper;
|
||||
|
||||
namespace Ryujinx.Graphics.Shader.StructuredIr
|
||||
{
|
||||
static class AstOptimizer
|
||||
{
|
||||
public static void Optimize(StructuredProgramContext context)
|
||||
{
|
||||
AstBlock mainBlock = context.CurrentFunction.MainBlock;
|
||||
|
||||
// When debug mode is enabled, we disable expression propagation
|
||||
// (this makes comparison with the disassembly easier).
|
||||
if (!context.DebugMode)
|
||||
{
|
||||
AstBlockVisitor visitor = new(mainBlock);
|
||||
|
||||
foreach (IAstNode node in visitor.Visit())
|
||||
{
|
||||
if (node is AstAssignment assignment && assignment.Destination is AstOperand propVar)
|
||||
{
|
||||
bool isWorthPropagating = propVar.Uses.Count == 1 || IsWorthPropagating(assignment.Source);
|
||||
|
||||
if (propVar.Defs.Count == 1 && isWorthPropagating)
|
||||
{
|
||||
PropagateExpression(propVar, assignment.Source);
|
||||
}
|
||||
|
||||
if (propVar.Type == OperandType.LocalVariable && propVar.Uses.Count == 0)
|
||||
{
|
||||
visitor.Block.Remove(assignment);
|
||||
|
||||
context.CurrentFunction.Locals.Remove(propVar);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
RemoveEmptyBlocks(mainBlock);
|
||||
}
|
||||
|
||||
private static bool IsWorthPropagating(IAstNode source)
|
||||
{
|
||||
if (source is not AstOperation srcOp)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!InstructionInfo.IsUnary(srcOp.Inst))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return srcOp.GetSource(0) is AstOperand || srcOp.Inst == Instruction.Copy;
|
||||
}
|
||||
|
||||
private static void PropagateExpression(AstOperand propVar, IAstNode source)
|
||||
{
|
||||
IAstNode[] uses = propVar.Uses.ToArray();
|
||||
|
||||
foreach (IAstNode useNode in uses)
|
||||
{
|
||||
if (useNode is AstBlock useBlock)
|
||||
{
|
||||
useBlock.Condition = source;
|
||||
}
|
||||
else if (useNode is AstOperation useOperation)
|
||||
{
|
||||
for (int srcIndex = 0; srcIndex < useOperation.SourcesCount; srcIndex++)
|
||||
{
|
||||
if (useOperation.GetSource(srcIndex) == propVar)
|
||||
{
|
||||
useOperation.SetSource(srcIndex, source);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (useNode is AstAssignment useAssignment)
|
||||
{
|
||||
useAssignment.Source = source;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void RemoveEmptyBlocks(AstBlock mainBlock)
|
||||
{
|
||||
Queue<AstBlock> pending = new();
|
||||
|
||||
pending.Enqueue(mainBlock);
|
||||
|
||||
while (pending.TryDequeue(out AstBlock block))
|
||||
{
|
||||
foreach (IAstNode node in block)
|
||||
{
|
||||
if (node is AstBlock childBlock)
|
||||
{
|
||||
pending.Enqueue(childBlock);
|
||||
}
|
||||
}
|
||||
|
||||
AstBlock parent = block.Parent;
|
||||
|
||||
if (parent == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
AstBlock nextBlock = Next(block) as AstBlock;
|
||||
|
||||
bool hasElse = nextBlock != null && nextBlock.Type == AstBlockType.Else;
|
||||
|
||||
bool isIf = block.Type == AstBlockType.If;
|
||||
|
||||
if (block.Count == 0)
|
||||
{
|
||||
if (isIf)
|
||||
{
|
||||
if (hasElse)
|
||||
{
|
||||
nextBlock.TurnIntoIf(InverseCond(block.Condition));
|
||||
}
|
||||
|
||||
parent.Remove(block);
|
||||
}
|
||||
else if (block.Type == AstBlockType.Else)
|
||||
{
|
||||
parent.Remove(block);
|
||||
}
|
||||
}
|
||||
else if (isIf && parent.Type == AstBlockType.Else && parent.Count == (hasElse ? 2 : 1))
|
||||
{
|
||||
AstBlock parentOfParent = parent.Parent;
|
||||
|
||||
parent.Remove(block);
|
||||
|
||||
parentOfParent.AddAfter(parent, block);
|
||||
|
||||
if (hasElse)
|
||||
{
|
||||
parent.Remove(nextBlock);
|
||||
|
||||
parentOfParent.AddAfter(block, nextBlock);
|
||||
}
|
||||
|
||||
parentOfParent.Remove(parent);
|
||||
|
||||
block.TurnIntoElseIf();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
49
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/AstTextureOperation.cs
vendored
Normal file
49
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/AstTextureOperation.cs
vendored
Normal file
@@ -0,0 +1,49 @@
|
||||
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
|
||||
|
||||
namespace Ryujinx.Graphics.Shader.StructuredIr
|
||||
{
|
||||
class AstTextureOperation : AstOperation
|
||||
{
|
||||
public SamplerType Type { get; }
|
||||
public TextureFormat Format { get; }
|
||||
public TextureFlags Flags { get; }
|
||||
|
||||
public int Set { get; }
|
||||
public int Binding { get; }
|
||||
public int SamplerSet { get; }
|
||||
public int SamplerBinding { get; }
|
||||
|
||||
public bool IsSeparate => SamplerBinding >= 0;
|
||||
|
||||
public AstTextureOperation(
|
||||
Instruction inst,
|
||||
SamplerType type,
|
||||
TextureFormat format,
|
||||
TextureFlags flags,
|
||||
int set,
|
||||
int binding,
|
||||
int samplerSet,
|
||||
int samplerBinding,
|
||||
int index,
|
||||
params IAstNode[] sources) : base(inst, StorageKind.None, false, index, sources, sources.Length)
|
||||
{
|
||||
Type = type;
|
||||
Format = format;
|
||||
Flags = flags;
|
||||
Set = set;
|
||||
Binding = binding;
|
||||
SamplerSet = samplerSet;
|
||||
SamplerBinding = samplerBinding;
|
||||
}
|
||||
|
||||
public SetBindingPair GetTextureSetAndBinding()
|
||||
{
|
||||
return new SetBindingPair(Set, Binding);
|
||||
}
|
||||
|
||||
public SetBindingPair GetSamplerSetAndBinding()
|
||||
{
|
||||
return new SetBindingPair(SamplerSet, SamplerBinding);
|
||||
}
|
||||
}
|
||||
}
|
||||
20
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/BufferDefinition.cs
vendored
Normal file
20
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/BufferDefinition.cs
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
namespace Ryujinx.Graphics.Shader.StructuredIr
|
||||
{
|
||||
readonly struct BufferDefinition
|
||||
{
|
||||
public BufferLayout Layout { get; }
|
||||
public int Set { get; }
|
||||
public int Binding { get; }
|
||||
public string Name { get; }
|
||||
public StructureType Type { get; }
|
||||
|
||||
public BufferDefinition(BufferLayout layout, int set, int binding, string name, StructureType type)
|
||||
{
|
||||
Layout = layout;
|
||||
Set = set;
|
||||
Binding = binding;
|
||||
Name = name;
|
||||
Type = type;
|
||||
}
|
||||
}
|
||||
}
|
||||
8
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/BufferLayout.cs
vendored
Normal file
8
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/BufferLayout.cs
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace Ryujinx.Graphics.Shader.StructuredIr
|
||||
{
|
||||
enum BufferLayout
|
||||
{
|
||||
Std140,
|
||||
Std430,
|
||||
}
|
||||
}
|
||||
458
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/GotoElimination.cs
vendored
Normal file
458
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/GotoElimination.cs
vendored
Normal file
@@ -0,0 +1,458 @@
|
||||
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using static Ryujinx.Graphics.Shader.StructuredIr.AstHelper;
|
||||
|
||||
namespace Ryujinx.Graphics.Shader.StructuredIr
|
||||
{
|
||||
static class GotoElimination
|
||||
{
|
||||
// This is a modified version of the algorithm presented on the paper
|
||||
// "Taming Control Flow: A Structured Approach to Eliminating Goto Statements".
|
||||
public static void Eliminate(GotoStatement[] gotos)
|
||||
{
|
||||
for (int index = gotos.Length - 1; index >= 0; index--)
|
||||
{
|
||||
GotoStatement stmt = gotos[index];
|
||||
|
||||
AstBlock gBlock = ParentBlock(stmt.Goto);
|
||||
AstBlock lBlock = ParentBlock(stmt.Label);
|
||||
|
||||
int gLevel = Level(gBlock);
|
||||
int lLevel = Level(lBlock);
|
||||
|
||||
if (IndirectlyRelated(gBlock, lBlock, gLevel, lLevel))
|
||||
{
|
||||
AstBlock drBlock = gBlock;
|
||||
|
||||
int drLevel = gLevel;
|
||||
|
||||
do
|
||||
{
|
||||
drBlock = drBlock.Parent;
|
||||
|
||||
drLevel--;
|
||||
}
|
||||
while (!DirectlyRelated(drBlock, lBlock, drLevel, lLevel));
|
||||
|
||||
MoveOutward(stmt, gLevel, drLevel);
|
||||
|
||||
gBlock = drBlock;
|
||||
gLevel = drLevel;
|
||||
|
||||
if (Previous(stmt.Goto) is AstBlock elseBlock && elseBlock.Type == AstBlockType.Else)
|
||||
{
|
||||
// It's possible that the label was enclosed inside an else block,
|
||||
// in this case we need to update the block and level.
|
||||
// We also need to set the IsLoop for the case when the label is
|
||||
// now before the goto, due to the newly introduced else block.
|
||||
lBlock = ParentBlock(stmt.Label);
|
||||
|
||||
lLevel = Level(lBlock);
|
||||
|
||||
if (!IndirectlyRelated(elseBlock, lBlock, gLevel + 1, lLevel))
|
||||
{
|
||||
stmt.IsLoop = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (DirectlyRelated(gBlock, lBlock, gLevel, lLevel))
|
||||
{
|
||||
if (gLevel > lLevel)
|
||||
{
|
||||
MoveOutward(stmt, gLevel, lLevel);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (stmt.IsLoop)
|
||||
{
|
||||
Lift(stmt);
|
||||
}
|
||||
|
||||
MoveInward(stmt);
|
||||
}
|
||||
}
|
||||
|
||||
gBlock = ParentBlock(stmt.Goto);
|
||||
|
||||
if (stmt.IsLoop)
|
||||
{
|
||||
EncloseDoWhile(stmt, gBlock, stmt.Label);
|
||||
}
|
||||
else
|
||||
{
|
||||
Enclose(gBlock, AstBlockType.If, stmt.Condition, Next(stmt.Goto), stmt.Label);
|
||||
}
|
||||
|
||||
gBlock.Remove(stmt.Goto);
|
||||
}
|
||||
}
|
||||
|
||||
private static bool IndirectlyRelated(AstBlock lBlock, AstBlock rBlock, int lLevel, int rlevel)
|
||||
{
|
||||
return !(lBlock == rBlock || DirectlyRelated(lBlock, rBlock, lLevel, rlevel));
|
||||
}
|
||||
|
||||
private static bool DirectlyRelated(AstBlock lBlock, AstBlock rBlock, int lLevel, int rLevel)
|
||||
{
|
||||
// If the levels are equal, they can be either siblings or indirectly related.
|
||||
if (lLevel == rLevel)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
AstNode block;
|
||||
AstNode other;
|
||||
|
||||
int blockLvl, otherLvl;
|
||||
|
||||
if (lLevel > rLevel)
|
||||
{
|
||||
block = lBlock;
|
||||
blockLvl = lLevel;
|
||||
other = rBlock;
|
||||
otherLvl = rLevel;
|
||||
}
|
||||
else /* if (rLevel > lLevel) */
|
||||
{
|
||||
block = rBlock;
|
||||
blockLvl = rLevel;
|
||||
other = lBlock;
|
||||
otherLvl = lLevel;
|
||||
}
|
||||
|
||||
while (blockLvl >= otherLvl)
|
||||
{
|
||||
if (block == other)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
block = block.Parent;
|
||||
|
||||
blockLvl--;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static void Lift(GotoStatement stmt)
|
||||
{
|
||||
AstBlock block = ParentBlock(stmt.Goto);
|
||||
|
||||
AstBlock[] path = BackwardsPath(block, ParentBlock(stmt.Label));
|
||||
|
||||
AstBlock loopFirstStmt = path[^1];
|
||||
|
||||
if (loopFirstStmt.Type == AstBlockType.Else)
|
||||
{
|
||||
loopFirstStmt = Previous(loopFirstStmt) as AstBlock;
|
||||
|
||||
if (loopFirstStmt == null || loopFirstStmt.Type != AstBlockType.If)
|
||||
{
|
||||
throw new InvalidOperationException("Found an else without a matching if.");
|
||||
}
|
||||
}
|
||||
|
||||
AstBlock newBlock = EncloseDoWhile(stmt, block, loopFirstStmt);
|
||||
|
||||
block.Remove(stmt.Goto);
|
||||
|
||||
newBlock.AddFirst(stmt.Goto);
|
||||
|
||||
stmt.IsLoop = false;
|
||||
}
|
||||
|
||||
private static void MoveOutward(GotoStatement stmt, int gLevel, int lLevel)
|
||||
{
|
||||
AstBlock origin = ParentBlock(stmt.Goto);
|
||||
|
||||
AstBlock block = origin;
|
||||
|
||||
// Check if a loop is enclosing the goto, and the block that is
|
||||
// directly related to the label is above the loop block.
|
||||
// In that case, we need to introduce a break to get out of the loop.
|
||||
AstBlock loopBlock = origin;
|
||||
|
||||
int loopLevel = gLevel;
|
||||
|
||||
while (loopLevel > lLevel)
|
||||
{
|
||||
AstBlock child = loopBlock;
|
||||
|
||||
loopBlock = loopBlock.Parent;
|
||||
|
||||
loopLevel--;
|
||||
|
||||
if (child.Type == AstBlockType.DoWhile)
|
||||
{
|
||||
EncloseSingleInst(stmt, Instruction.LoopBreak);
|
||||
|
||||
block.Remove(stmt.Goto);
|
||||
|
||||
loopBlock.AddAfter(child, stmt.Goto);
|
||||
|
||||
block = loopBlock;
|
||||
gLevel = loopLevel;
|
||||
}
|
||||
}
|
||||
|
||||
// Insert ifs to skip the parts that shouldn't be executed due to the goto.
|
||||
bool tryInsertElse = stmt.IsUnconditional && origin.Type == AstBlockType.If;
|
||||
|
||||
while (gLevel > lLevel)
|
||||
{
|
||||
Enclose(block, AstBlockType.If, stmt.Condition, Next(stmt.Goto));
|
||||
|
||||
block.Remove(stmt.Goto);
|
||||
|
||||
AstBlock child = block;
|
||||
|
||||
// We can't move the goto in the middle of a if and a else block, in
|
||||
// this case we need to move it after the else.
|
||||
// IsLoop may need to be updated if the label is inside the else, as
|
||||
// introducing a loop is the only way to ensure the else will be executed.
|
||||
if (Next(child) is AstBlock elseBlock && elseBlock.Type == AstBlockType.Else)
|
||||
{
|
||||
child = elseBlock;
|
||||
}
|
||||
|
||||
block = block.Parent;
|
||||
|
||||
block.AddAfter(child, stmt.Goto);
|
||||
|
||||
gLevel--;
|
||||
|
||||
if (tryInsertElse && child == origin)
|
||||
{
|
||||
AstBlock lBlock = ParentBlock(stmt.Label);
|
||||
|
||||
IAstNode last = block == lBlock && !stmt.IsLoop ? stmt.Label : null;
|
||||
|
||||
AstBlock newBlock = Enclose(block, AstBlockType.Else, null, Next(stmt.Goto), last);
|
||||
|
||||
if (newBlock != null)
|
||||
{
|
||||
block.Remove(stmt.Goto);
|
||||
|
||||
block.AddAfter(newBlock, stmt.Goto);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void MoveInward(GotoStatement stmt)
|
||||
{
|
||||
AstBlock block = ParentBlock(stmt.Goto);
|
||||
|
||||
AstBlock[] path = BackwardsPath(block, ParentBlock(stmt.Label));
|
||||
|
||||
for (int index = path.Length - 1; index >= 0; index--)
|
||||
{
|
||||
AstBlock child = path[index];
|
||||
AstBlock last = child;
|
||||
|
||||
if (child.Type == AstBlockType.If)
|
||||
{
|
||||
// Modify the if condition to allow it to be entered by the goto.
|
||||
if (!ContainsCondComb(child.Condition, Instruction.LogicalOr, stmt.Condition))
|
||||
{
|
||||
child.OrCondition(stmt.Condition);
|
||||
}
|
||||
}
|
||||
else if (child.Type == AstBlockType.Else)
|
||||
{
|
||||
// Modify the matching if condition to force the else to be entered by the goto.
|
||||
if (Previous(child) is not AstBlock ifBlock || ifBlock.Type != AstBlockType.If)
|
||||
{
|
||||
throw new InvalidOperationException("Found an else without a matching if.");
|
||||
}
|
||||
|
||||
IAstNode cond = InverseCond(stmt.Condition);
|
||||
|
||||
if (!ContainsCondComb(ifBlock.Condition, Instruction.LogicalAnd, cond))
|
||||
{
|
||||
ifBlock.AndCondition(cond);
|
||||
}
|
||||
|
||||
last = ifBlock;
|
||||
}
|
||||
|
||||
Enclose(block, AstBlockType.If, stmt.Condition, Next(stmt.Goto), last);
|
||||
|
||||
block.Remove(stmt.Goto);
|
||||
|
||||
child.AddFirst(stmt.Goto);
|
||||
|
||||
block = child;
|
||||
}
|
||||
}
|
||||
|
||||
private static bool ContainsCondComb(IAstNode node, Instruction inst, IAstNode newCond)
|
||||
{
|
||||
while (node is AstOperation operation && operation.SourcesCount == 2)
|
||||
{
|
||||
if (operation.Inst == inst && IsSameCond(operation.GetSource(1), newCond))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
node = operation.GetSource(0);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static AstBlock EncloseDoWhile(GotoStatement stmt, AstBlock block, IAstNode first)
|
||||
{
|
||||
if (block.Type == AstBlockType.DoWhile && first == block.First)
|
||||
{
|
||||
// We only need to insert the continue if we're not at the end of the loop,
|
||||
// or if our condition is different from the loop condition.
|
||||
if (Next(stmt.Goto) != null || block.Condition != stmt.Condition)
|
||||
{
|
||||
EncloseSingleInst(stmt, Instruction.LoopContinue);
|
||||
}
|
||||
|
||||
// Modify the do-while condition to allow it to continue.
|
||||
if (!ContainsCondComb(block.Condition, Instruction.LogicalOr, stmt.Condition))
|
||||
{
|
||||
block.OrCondition(stmt.Condition);
|
||||
}
|
||||
|
||||
return block;
|
||||
}
|
||||
|
||||
return Enclose(block, AstBlockType.DoWhile, stmt.Condition, first, stmt.Goto);
|
||||
}
|
||||
|
||||
private static void EncloseSingleInst(GotoStatement stmt, Instruction inst)
|
||||
{
|
||||
AstBlock block = ParentBlock(stmt.Goto);
|
||||
|
||||
AstBlock newBlock = new(AstBlockType.If, stmt.Condition);
|
||||
|
||||
block.AddAfter(stmt.Goto, newBlock);
|
||||
|
||||
newBlock.AddFirst(new AstOperation(inst));
|
||||
}
|
||||
|
||||
private static AstBlock Enclose(
|
||||
AstBlock block,
|
||||
AstBlockType type,
|
||||
IAstNode cond,
|
||||
IAstNode first,
|
||||
IAstNode last = null)
|
||||
{
|
||||
if (first == last)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (type == AstBlockType.If)
|
||||
{
|
||||
cond = InverseCond(cond);
|
||||
}
|
||||
|
||||
// Do a quick check, if we are enclosing a single block,
|
||||
// and the block type/condition matches the one we're going
|
||||
// to create, then we don't need a new block, we can just
|
||||
// return the old one.
|
||||
bool hasSingleNode = Next(first) == last;
|
||||
|
||||
if (hasSingleNode && BlockMatches(first, type, cond))
|
||||
{
|
||||
return first as AstBlock;
|
||||
}
|
||||
|
||||
AstBlock newBlock = new(type, cond);
|
||||
|
||||
block.AddBefore(first, newBlock);
|
||||
|
||||
while (first != last)
|
||||
{
|
||||
IAstNode next = Next(first);
|
||||
|
||||
block.Remove(first);
|
||||
|
||||
newBlock.Add(first);
|
||||
|
||||
first = next;
|
||||
}
|
||||
|
||||
return newBlock;
|
||||
}
|
||||
|
||||
private static bool BlockMatches(IAstNode node, AstBlockType type, IAstNode cond)
|
||||
{
|
||||
if (node is not AstBlock block)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return block.Type == type && IsSameCond(block.Condition, cond);
|
||||
}
|
||||
|
||||
private static bool IsSameCond(IAstNode lCond, IAstNode rCond)
|
||||
{
|
||||
if (lCond is AstOperation lCondOp && lCondOp.Inst == Instruction.LogicalNot)
|
||||
{
|
||||
if (rCond is not AstOperation rCondOp || rCondOp.Inst != lCondOp.Inst)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
lCond = lCondOp.GetSource(0);
|
||||
rCond = rCondOp.GetSource(0);
|
||||
}
|
||||
|
||||
return lCond == rCond;
|
||||
}
|
||||
|
||||
private static AstBlock ParentBlock(IAstNode node)
|
||||
{
|
||||
if (node is AstBlock block)
|
||||
{
|
||||
return block.Parent;
|
||||
}
|
||||
|
||||
while (node is not AstBlock)
|
||||
{
|
||||
node = node.Parent;
|
||||
}
|
||||
|
||||
return node as AstBlock;
|
||||
}
|
||||
|
||||
private static AstBlock[] BackwardsPath(AstBlock top, AstBlock bottom)
|
||||
{
|
||||
AstBlock block = bottom;
|
||||
|
||||
List<AstBlock> path = [];
|
||||
|
||||
while (block != top)
|
||||
{
|
||||
path.Add(block);
|
||||
|
||||
block = block.Parent;
|
||||
}
|
||||
|
||||
return path.ToArray();
|
||||
}
|
||||
|
||||
private static int Level(AstNode node)
|
||||
{
|
||||
int level = 0;
|
||||
|
||||
while (node != null)
|
||||
{
|
||||
level++;
|
||||
|
||||
node = node.Parent;
|
||||
}
|
||||
|
||||
return level;
|
||||
}
|
||||
}
|
||||
}
|
||||
23
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/GotoStatement.cs
vendored
Normal file
23
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/GotoStatement.cs
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
|
||||
|
||||
namespace Ryujinx.Graphics.Shader.StructuredIr
|
||||
{
|
||||
class GotoStatement
|
||||
{
|
||||
public AstOperation Goto { get; }
|
||||
public AstAssignment Label { get; }
|
||||
|
||||
public IAstNode Condition => Label.Destination;
|
||||
|
||||
public bool IsLoop { get; set; }
|
||||
|
||||
public bool IsUnconditional => Goto.Inst == Instruction.Branch;
|
||||
|
||||
public GotoStatement(AstOperation branch, AstAssignment label, bool isLoop)
|
||||
{
|
||||
Goto = branch;
|
||||
Label = label;
|
||||
IsLoop = isLoop;
|
||||
}
|
||||
}
|
||||
}
|
||||
13
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/HelperFunctionsMask.cs
vendored
Normal file
13
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/HelperFunctionsMask.cs
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
|
||||
namespace Ryujinx.Graphics.Shader.StructuredIr
|
||||
{
|
||||
[Flags]
|
||||
enum HelperFunctionsMask
|
||||
{
|
||||
MultiplyHighS32 = 1 << 2,
|
||||
MultiplyHighU32 = 1 << 3,
|
||||
SwizzleAdd = 1 << 10,
|
||||
FSI = 1 << 11,
|
||||
}
|
||||
}
|
||||
11
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/IAstNode.cs
vendored
Normal file
11
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/IAstNode.cs
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Ryujinx.Graphics.Shader.StructuredIr
|
||||
{
|
||||
interface IAstNode
|
||||
{
|
||||
AstBlock Parent { get; set; }
|
||||
|
||||
LinkedListNode<IAstNode> LLNode { get; set; }
|
||||
}
|
||||
}
|
||||
208
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/InstructionInfo.cs
vendored
Normal file
208
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/InstructionInfo.cs
vendored
Normal file
@@ -0,0 +1,208 @@
|
||||
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
|
||||
using Ryujinx.Graphics.Shader.Translation;
|
||||
using System;
|
||||
|
||||
namespace Ryujinx.Graphics.Shader.StructuredIr
|
||||
{
|
||||
static class InstructionInfo
|
||||
{
|
||||
private readonly struct InstInfo
|
||||
{
|
||||
public AggregateType DestType { get; }
|
||||
|
||||
public AggregateType[] SrcTypes { get; }
|
||||
|
||||
public InstInfo(AggregateType destType, params AggregateType[] srcTypes)
|
||||
{
|
||||
DestType = destType;
|
||||
SrcTypes = srcTypes;
|
||||
}
|
||||
}
|
||||
|
||||
private static readonly InstInfo[] _infoTbl;
|
||||
|
||||
static InstructionInfo()
|
||||
{
|
||||
_infoTbl = new InstInfo[(int)Instruction.Count];
|
||||
|
||||
#pragma warning disable IDE0055 // Disable formatting
|
||||
// Inst Destination type Source 1 type Source 2 type Source 3 type Source 4 type
|
||||
Add(Instruction.AtomicAdd, AggregateType.U32, AggregateType.S32, AggregateType.S32, AggregateType.U32);
|
||||
Add(Instruction.AtomicAnd, AggregateType.U32, AggregateType.S32, AggregateType.S32, AggregateType.U32);
|
||||
Add(Instruction.AtomicCompareAndSwap, AggregateType.U32, AggregateType.S32, AggregateType.S32, AggregateType.U32, AggregateType.U32);
|
||||
Add(Instruction.AtomicMaxS32, AggregateType.S32, AggregateType.S32, AggregateType.S32, AggregateType.S32);
|
||||
Add(Instruction.AtomicMaxU32, AggregateType.U32, AggregateType.S32, AggregateType.S32, AggregateType.U32);
|
||||
Add(Instruction.AtomicMinS32, AggregateType.S32, AggregateType.S32, AggregateType.S32, AggregateType.S32);
|
||||
Add(Instruction.AtomicMinU32, AggregateType.U32, AggregateType.S32, AggregateType.S32, AggregateType.U32);
|
||||
Add(Instruction.AtomicOr, AggregateType.U32, AggregateType.S32, AggregateType.S32, AggregateType.U32);
|
||||
Add(Instruction.AtomicSwap, AggregateType.U32, AggregateType.S32, AggregateType.S32, AggregateType.U32);
|
||||
Add(Instruction.AtomicXor, AggregateType.U32, AggregateType.S32, AggregateType.S32, AggregateType.U32);
|
||||
Add(Instruction.Absolute, AggregateType.Scalar, AggregateType.Scalar);
|
||||
Add(Instruction.Add, AggregateType.Scalar, AggregateType.Scalar, AggregateType.Scalar);
|
||||
Add(Instruction.Ballot, AggregateType.U32, AggregateType.Bool);
|
||||
Add(Instruction.BitCount, AggregateType.S32, AggregateType.S32);
|
||||
Add(Instruction.BitfieldExtractS32, AggregateType.S32, AggregateType.S32, AggregateType.S32, AggregateType.S32);
|
||||
Add(Instruction.BitfieldExtractU32, AggregateType.U32, AggregateType.U32, AggregateType.S32, AggregateType.S32);
|
||||
Add(Instruction.BitfieldInsert, AggregateType.S32, AggregateType.S32, AggregateType.S32, AggregateType.S32, AggregateType.S32);
|
||||
Add(Instruction.BitfieldReverse, AggregateType.S32, AggregateType.S32);
|
||||
Add(Instruction.BitwiseAnd, AggregateType.S32, AggregateType.S32, AggregateType.S32);
|
||||
Add(Instruction.BitwiseExclusiveOr, AggregateType.S32, AggregateType.S32, AggregateType.S32);
|
||||
Add(Instruction.BitwiseNot, AggregateType.S32, AggregateType.S32);
|
||||
Add(Instruction.BitwiseOr, AggregateType.S32, AggregateType.S32, AggregateType.S32);
|
||||
Add(Instruction.BranchIfTrue, AggregateType.Void, AggregateType.Bool);
|
||||
Add(Instruction.BranchIfFalse, AggregateType.Void, AggregateType.Bool);
|
||||
Add(Instruction.Call, AggregateType.Scalar);
|
||||
Add(Instruction.Ceiling, AggregateType.Scalar, AggregateType.Scalar, AggregateType.Scalar);
|
||||
Add(Instruction.Clamp, AggregateType.Scalar, AggregateType.Scalar, AggregateType.Scalar, AggregateType.Scalar);
|
||||
Add(Instruction.ClampU32, AggregateType.U32, AggregateType.U32, AggregateType.U32, AggregateType.U32);
|
||||
Add(Instruction.CompareEqual, AggregateType.Bool, AggregateType.Scalar, AggregateType.Scalar);
|
||||
Add(Instruction.CompareGreater, AggregateType.Bool, AggregateType.Scalar, AggregateType.Scalar);
|
||||
Add(Instruction.CompareGreaterOrEqual, AggregateType.Bool, AggregateType.Scalar, AggregateType.Scalar);
|
||||
Add(Instruction.CompareGreaterOrEqualU32, AggregateType.Bool, AggregateType.U32, AggregateType.U32);
|
||||
Add(Instruction.CompareGreaterU32, AggregateType.Bool, AggregateType.U32, AggregateType.U32);
|
||||
Add(Instruction.CompareLess, AggregateType.Bool, AggregateType.Scalar, AggregateType.Scalar);
|
||||
Add(Instruction.CompareLessOrEqual, AggregateType.Bool, AggregateType.Scalar, AggregateType.Scalar);
|
||||
Add(Instruction.CompareLessOrEqualU32, AggregateType.Bool, AggregateType.U32, AggregateType.U32);
|
||||
Add(Instruction.CompareLessU32, AggregateType.Bool, AggregateType.U32, AggregateType.U32);
|
||||
Add(Instruction.CompareNotEqual, AggregateType.Bool, AggregateType.Scalar, AggregateType.Scalar);
|
||||
Add(Instruction.ConditionalSelect, AggregateType.Scalar, AggregateType.Bool, AggregateType.Scalar, AggregateType.Scalar);
|
||||
Add(Instruction.ConvertFP32ToFP64, AggregateType.FP64, AggregateType.FP32);
|
||||
Add(Instruction.ConvertFP64ToFP32, AggregateType.FP32, AggregateType.FP64);
|
||||
Add(Instruction.ConvertFP32ToS32, AggregateType.S32, AggregateType.FP32);
|
||||
Add(Instruction.ConvertFP32ToU32, AggregateType.U32, AggregateType.FP32);
|
||||
Add(Instruction.ConvertFP64ToS32, AggregateType.S32, AggregateType.FP64);
|
||||
Add(Instruction.ConvertFP64ToU32, AggregateType.U32, AggregateType.FP64);
|
||||
Add(Instruction.ConvertS32ToFP32, AggregateType.FP32, AggregateType.S32);
|
||||
Add(Instruction.ConvertS32ToFP64, AggregateType.FP64, AggregateType.S32);
|
||||
Add(Instruction.ConvertU32ToFP32, AggregateType.FP32, AggregateType.U32);
|
||||
Add(Instruction.ConvertU32ToFP64, AggregateType.FP64, AggregateType.U32);
|
||||
Add(Instruction.Cosine, AggregateType.Scalar, AggregateType.Scalar);
|
||||
Add(Instruction.Ddx, AggregateType.FP32, AggregateType.FP32);
|
||||
Add(Instruction.Ddy, AggregateType.FP32, AggregateType.FP32);
|
||||
Add(Instruction.Divide, AggregateType.Scalar, AggregateType.Scalar, AggregateType.Scalar);
|
||||
Add(Instruction.ExponentB2, AggregateType.Scalar, AggregateType.Scalar);
|
||||
Add(Instruction.FindLSB, AggregateType.S32, AggregateType.S32);
|
||||
Add(Instruction.FindMSBS32, AggregateType.S32, AggregateType.S32);
|
||||
Add(Instruction.FindMSBU32, AggregateType.S32, AggregateType.U32);
|
||||
Add(Instruction.Floor, AggregateType.Scalar, AggregateType.Scalar);
|
||||
Add(Instruction.FusedMultiplyAdd, AggregateType.Scalar, AggregateType.Scalar, AggregateType.Scalar, AggregateType.Scalar);
|
||||
Add(Instruction.ImageLoad, AggregateType.FP32);
|
||||
Add(Instruction.ImageStore, AggregateType.Void);
|
||||
Add(Instruction.ImageAtomic, AggregateType.S32);
|
||||
Add(Instruction.IsNan, AggregateType.Bool, AggregateType.Scalar);
|
||||
Add(Instruction.Load, AggregateType.FP32);
|
||||
Add(Instruction.Lod, AggregateType.FP32);
|
||||
Add(Instruction.LogarithmB2, AggregateType.Scalar, AggregateType.Scalar);
|
||||
Add(Instruction.LogicalAnd, AggregateType.Bool, AggregateType.Bool, AggregateType.Bool);
|
||||
Add(Instruction.LogicalExclusiveOr, AggregateType.Bool, AggregateType.Bool, AggregateType.Bool);
|
||||
Add(Instruction.LogicalNot, AggregateType.Bool, AggregateType.Bool);
|
||||
Add(Instruction.LogicalOr, AggregateType.Bool, AggregateType.Bool, AggregateType.Bool);
|
||||
Add(Instruction.Maximum, AggregateType.Scalar, AggregateType.Scalar, AggregateType.Scalar);
|
||||
Add(Instruction.MaximumU32, AggregateType.U32, AggregateType.U32, AggregateType.U32);
|
||||
Add(Instruction.Minimum, AggregateType.Scalar, AggregateType.Scalar, AggregateType.Scalar);
|
||||
Add(Instruction.MinimumU32, AggregateType.U32, AggregateType.U32, AggregateType.U32);
|
||||
Add(Instruction.Modulo, AggregateType.Scalar, AggregateType.Scalar, AggregateType.Scalar);
|
||||
Add(Instruction.Multiply, AggregateType.Scalar, AggregateType.Scalar, AggregateType.Scalar);
|
||||
Add(Instruction.MultiplyHighS32, AggregateType.S32, AggregateType.S32, AggregateType.S32);
|
||||
Add(Instruction.MultiplyHighU32, AggregateType.U32, AggregateType.U32, AggregateType.U32);
|
||||
Add(Instruction.Negate, AggregateType.Scalar, AggregateType.Scalar);
|
||||
Add(Instruction.PackDouble2x32, AggregateType.FP64, AggregateType.U32, AggregateType.U32);
|
||||
Add(Instruction.PackHalf2x16, AggregateType.U32, AggregateType.FP32, AggregateType.FP32);
|
||||
Add(Instruction.ReciprocalSquareRoot, AggregateType.Scalar, AggregateType.Scalar);
|
||||
Add(Instruction.Return, AggregateType.Void, AggregateType.U32);
|
||||
Add(Instruction.Round, AggregateType.Scalar, AggregateType.Scalar);
|
||||
Add(Instruction.ShiftLeft, AggregateType.S32, AggregateType.S32, AggregateType.S32);
|
||||
Add(Instruction.ShiftRightS32, AggregateType.S32, AggregateType.S32, AggregateType.S32);
|
||||
Add(Instruction.ShiftRightU32, AggregateType.U32, AggregateType.U32, AggregateType.S32);
|
||||
Add(Instruction.Shuffle, AggregateType.FP32, AggregateType.FP32, AggregateType.U32);
|
||||
Add(Instruction.ShuffleDown, AggregateType.FP32, AggregateType.FP32, AggregateType.U32);
|
||||
Add(Instruction.ShuffleUp, AggregateType.FP32, AggregateType.FP32, AggregateType.U32);
|
||||
Add(Instruction.ShuffleXor, AggregateType.FP32, AggregateType.FP32, AggregateType.U32);
|
||||
Add(Instruction.Sine, AggregateType.Scalar, AggregateType.Scalar);
|
||||
Add(Instruction.SquareRoot, AggregateType.Scalar, AggregateType.Scalar);
|
||||
Add(Instruction.Store, AggregateType.Void);
|
||||
Add(Instruction.Subtract, AggregateType.Scalar, AggregateType.Scalar, AggregateType.Scalar);
|
||||
Add(Instruction.SwizzleAdd, AggregateType.FP32, AggregateType.FP32, AggregateType.FP32, AggregateType.S32);
|
||||
Add(Instruction.TextureSample, AggregateType.FP32);
|
||||
Add(Instruction.TextureQuerySamples, AggregateType.S32, AggregateType.S32);
|
||||
Add(Instruction.TextureQuerySize, AggregateType.S32, AggregateType.S32, AggregateType.S32);
|
||||
Add(Instruction.Truncate, AggregateType.Scalar, AggregateType.Scalar);
|
||||
Add(Instruction.UnpackDouble2x32, AggregateType.U32, AggregateType.FP64);
|
||||
Add(Instruction.UnpackHalf2x16, AggregateType.FP32, AggregateType.U32);
|
||||
Add(Instruction.VectorExtract, AggregateType.Scalar, AggregateType.Vector4, AggregateType.S32);
|
||||
Add(Instruction.VoteAll, AggregateType.Bool, AggregateType.Bool);
|
||||
Add(Instruction.VoteAllEqual, AggregateType.Bool, AggregateType.Bool);
|
||||
Add(Instruction.VoteAny, AggregateType.Bool, AggregateType.Bool);
|
||||
#pragma warning restore IDE0055
|
||||
}
|
||||
|
||||
private static void Add(Instruction inst, AggregateType destType, params AggregateType[] srcTypes)
|
||||
{
|
||||
_infoTbl[(int)inst] = new InstInfo(destType, srcTypes);
|
||||
}
|
||||
|
||||
public static AggregateType GetDestVarType(Instruction inst)
|
||||
{
|
||||
return GetFinalVarType(_infoTbl[(int)(inst & Instruction.Mask)].DestType, inst);
|
||||
}
|
||||
|
||||
public static AggregateType GetSrcVarType(Instruction inst, int index)
|
||||
{
|
||||
// TODO: Return correct type depending on source index,
|
||||
// that can improve the decompiler output.
|
||||
if (inst is Instruction.ImageLoad or
|
||||
Instruction.ImageStore or
|
||||
Instruction.ImageAtomic or
|
||||
Instruction.Lod or
|
||||
Instruction.TextureSample)
|
||||
{
|
||||
return AggregateType.FP32;
|
||||
}
|
||||
else if (inst == Instruction.Call || inst == Instruction.Load || inst == Instruction.Store || inst.IsAtomic())
|
||||
{
|
||||
return AggregateType.S32;
|
||||
}
|
||||
|
||||
return GetFinalVarType(_infoTbl[(int)(inst & Instruction.Mask)].SrcTypes[index], inst);
|
||||
}
|
||||
|
||||
private static AggregateType GetFinalVarType(AggregateType type, Instruction inst)
|
||||
{
|
||||
if (type == AggregateType.Scalar)
|
||||
{
|
||||
if ((inst & Instruction.FP32) != 0)
|
||||
{
|
||||
return AggregateType.FP32;
|
||||
}
|
||||
else if ((inst & Instruction.FP64) != 0)
|
||||
{
|
||||
return AggregateType.FP64;
|
||||
}
|
||||
else
|
||||
{
|
||||
return AggregateType.S32;
|
||||
}
|
||||
}
|
||||
else if (type == AggregateType.Void)
|
||||
{
|
||||
throw new ArgumentException($"Invalid operand for instruction \"{inst}\".");
|
||||
}
|
||||
|
||||
return type;
|
||||
}
|
||||
|
||||
public static bool IsUnary(Instruction inst)
|
||||
{
|
||||
if (inst == Instruction.Copy)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else if (inst == Instruction.TextureSample)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return _infoTbl[(int)(inst & Instruction.Mask)].SrcTypes.Length == 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
44
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/IoDefinition.cs
vendored
Normal file
44
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/IoDefinition.cs
vendored
Normal file
@@ -0,0 +1,44 @@
|
||||
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
|
||||
using System;
|
||||
|
||||
namespace Ryujinx.Graphics.Shader.StructuredIr
|
||||
{
|
||||
readonly struct IoDefinition : IEquatable<IoDefinition>
|
||||
{
|
||||
public StorageKind StorageKind { get; }
|
||||
public IoVariable IoVariable { get; }
|
||||
public int Location { get; }
|
||||
public int Component { get; }
|
||||
|
||||
public IoDefinition(StorageKind storageKind, IoVariable ioVariable, int location = 0, int component = 0)
|
||||
{
|
||||
StorageKind = storageKind;
|
||||
IoVariable = ioVariable;
|
||||
Location = location;
|
||||
Component = component;
|
||||
}
|
||||
|
||||
public override bool Equals(object other)
|
||||
{
|
||||
return other is IoDefinition ioDefinition && Equals(ioDefinition);
|
||||
}
|
||||
|
||||
public bool Equals(IoDefinition other)
|
||||
{
|
||||
return StorageKind == other.StorageKind &&
|
||||
IoVariable == other.IoVariable &&
|
||||
Location == other.Location &&
|
||||
Component == other.Component;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return (int)StorageKind | ((int)IoVariable << 8) | (Location << 16) | (Component << 24);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{StorageKind}.{IoVariable}.{Location}.{Component}";
|
||||
}
|
||||
}
|
||||
}
|
||||
18
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/MemoryDefinition.cs
vendored
Normal file
18
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/MemoryDefinition.cs
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
using Ryujinx.Graphics.Shader.Translation;
|
||||
|
||||
namespace Ryujinx.Graphics.Shader.StructuredIr
|
||||
{
|
||||
readonly struct MemoryDefinition
|
||||
{
|
||||
public string Name { get; }
|
||||
public AggregateType Type { get; }
|
||||
public int ArrayLength { get; }
|
||||
|
||||
public MemoryDefinition(string name, AggregateType type, int arrayLength = 1)
|
||||
{
|
||||
Name = name;
|
||||
Type = type;
|
||||
ArrayLength = arrayLength;
|
||||
}
|
||||
}
|
||||
}
|
||||
32
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/OperandInfo.cs
vendored
Normal file
32
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/OperandInfo.cs
vendored
Normal file
@@ -0,0 +1,32 @@
|
||||
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
|
||||
using Ryujinx.Graphics.Shader.Translation;
|
||||
using System;
|
||||
|
||||
namespace Ryujinx.Graphics.Shader.StructuredIr
|
||||
{
|
||||
static class OperandInfo
|
||||
{
|
||||
public static AggregateType GetVarType(AstOperand operand)
|
||||
{
|
||||
if (operand.Type == OperandType.LocalVariable)
|
||||
{
|
||||
return operand.VarType;
|
||||
}
|
||||
else
|
||||
{
|
||||
return GetVarType(operand.Type);
|
||||
}
|
||||
}
|
||||
|
||||
public static AggregateType GetVarType(OperandType type)
|
||||
{
|
||||
return type switch
|
||||
{
|
||||
OperandType.Argument => AggregateType.S32,
|
||||
OperandType.Constant => AggregateType.S32,
|
||||
OperandType.Undefined => AggregateType.S32,
|
||||
_ => throw new ArgumentException($"Invalid operand type \"{type}\"."),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
49
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/PhiFunctions.cs
vendored
Normal file
49
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/PhiFunctions.cs
vendored
Normal file
@@ -0,0 +1,49 @@
|
||||
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Ryujinx.Graphics.Shader.StructuredIr
|
||||
{
|
||||
static class PhiFunctions
|
||||
{
|
||||
public static void Remove(BasicBlock[] blocks)
|
||||
{
|
||||
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;
|
||||
|
||||
if (node.Value is not PhiNode phi)
|
||||
{
|
||||
node = nextNode;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
Operand temp = OperandHelper.Local();
|
||||
|
||||
for (int index = 0; index < phi.SourcesCount; index++)
|
||||
{
|
||||
Operand src = phi.GetSource(index);
|
||||
BasicBlock srcBlock = phi.GetBlock(index);
|
||||
|
||||
Operation copyOp = new(Instruction.Copy, temp, src);
|
||||
|
||||
srcBlock.Append(copyOp);
|
||||
}
|
||||
|
||||
Operation copyOp2 = new(Instruction.Copy, phi.Dest, temp);
|
||||
|
||||
nextNode = block.Operations.AddAfter(node, copyOp2).Next;
|
||||
block.Operations.Remove(node);
|
||||
|
||||
node = nextNode;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
110
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/ShaderProperties.cs
vendored
Normal file
110
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/ShaderProperties.cs
vendored
Normal file
@@ -0,0 +1,110 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Ryujinx.Graphics.Shader.StructuredIr
|
||||
{
|
||||
class ShaderProperties
|
||||
{
|
||||
private readonly Dictionary<int, BufferDefinition> _constantBuffers;
|
||||
private readonly Dictionary<int, BufferDefinition> _storageBuffers;
|
||||
private readonly Dictionary<SetBindingPair, TextureDefinition> _textures;
|
||||
private readonly Dictionary<SetBindingPair, TextureDefinition> _images;
|
||||
private readonly Dictionary<int, MemoryDefinition> _localMemories;
|
||||
private readonly Dictionary<int, MemoryDefinition> _sharedMemories;
|
||||
|
||||
public IReadOnlyDictionary<int, BufferDefinition> ConstantBuffers => _constantBuffers;
|
||||
public IReadOnlyDictionary<int, BufferDefinition> StorageBuffers => _storageBuffers;
|
||||
public IReadOnlyDictionary<SetBindingPair, TextureDefinition> Textures => _textures;
|
||||
public IReadOnlyDictionary<SetBindingPair, TextureDefinition> Images => _images;
|
||||
public IReadOnlyDictionary<int, MemoryDefinition> LocalMemories => _localMemories;
|
||||
public IReadOnlyDictionary<int, MemoryDefinition> SharedMemories => _sharedMemories;
|
||||
|
||||
public ShaderProperties()
|
||||
{
|
||||
_constantBuffers = new Dictionary<int, BufferDefinition>();
|
||||
_storageBuffers = new Dictionary<int, BufferDefinition>();
|
||||
_textures = new Dictionary<SetBindingPair, TextureDefinition>();
|
||||
_images = new Dictionary<SetBindingPair, TextureDefinition>();
|
||||
_localMemories = new Dictionary<int, MemoryDefinition>();
|
||||
_sharedMemories = new Dictionary<int, MemoryDefinition>();
|
||||
}
|
||||
|
||||
public void AddOrUpdateConstantBuffer(BufferDefinition definition)
|
||||
{
|
||||
_constantBuffers[definition.Binding] = definition;
|
||||
}
|
||||
|
||||
public void AddOrUpdateStorageBuffer(BufferDefinition definition)
|
||||
{
|
||||
_storageBuffers[definition.Binding] = definition;
|
||||
}
|
||||
|
||||
public void AddOrUpdateTexture(TextureDefinition definition)
|
||||
{
|
||||
_textures[new(definition.Set, definition.Binding)] = definition;
|
||||
}
|
||||
|
||||
public void AddOrUpdateImage(TextureDefinition definition)
|
||||
{
|
||||
_images[new(definition.Set, definition.Binding)] = definition;
|
||||
}
|
||||
|
||||
public int AddLocalMemory(MemoryDefinition definition)
|
||||
{
|
||||
int id = _localMemories.Count;
|
||||
_localMemories.Add(id, definition);
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
public int AddSharedMemory(MemoryDefinition definition)
|
||||
{
|
||||
int id = _sharedMemories.Count;
|
||||
_sharedMemories.Add(id, definition);
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
public static TextureFormat GetTextureFormat(IGpuAccessor gpuAccessor, int handle, int cbufSlot = -1)
|
||||
{
|
||||
// When the formatted load extension is supported, we don't need to
|
||||
// specify a format, we can just declare it without a format and the GPU will handle it.
|
||||
if (gpuAccessor.QueryHostSupportsImageLoadFormatted())
|
||||
{
|
||||
return TextureFormat.Unknown;
|
||||
}
|
||||
|
||||
TextureFormat format = gpuAccessor.QueryTextureFormat(handle, cbufSlot);
|
||||
|
||||
if (format == TextureFormat.Unknown)
|
||||
{
|
||||
gpuAccessor.Log($"Unknown format for texture {handle}.");
|
||||
|
||||
format = TextureFormat.R8G8B8A8Unorm;
|
||||
}
|
||||
|
||||
return format;
|
||||
}
|
||||
|
||||
private static bool FormatSupportsAtomic(TextureFormat format)
|
||||
{
|
||||
return format is TextureFormat.R32Sint or TextureFormat.R32Uint;
|
||||
}
|
||||
|
||||
public static TextureFormat GetTextureFormatAtomic(IGpuAccessor gpuAccessor, int handle, int cbufSlot = -1)
|
||||
{
|
||||
// Atomic image instructions do not support GL_EXT_shader_image_load_formatted,
|
||||
// and must have a type specified. Default to R32Sint if not available.
|
||||
|
||||
TextureFormat format = gpuAccessor.QueryTextureFormat(handle, cbufSlot);
|
||||
|
||||
if (!FormatSupportsAtomic(format))
|
||||
{
|
||||
gpuAccessor.Log($"Unsupported format for texture {handle}: {format}.");
|
||||
|
||||
format = TextureFormat.R32Sint;
|
||||
}
|
||||
|
||||
return format;
|
||||
}
|
||||
}
|
||||
}
|
||||
28
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/StructureType.cs
vendored
Normal file
28
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/StructureType.cs
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
using Ryujinx.Graphics.Shader.Translation;
|
||||
|
||||
namespace Ryujinx.Graphics.Shader.StructuredIr
|
||||
{
|
||||
readonly struct StructureField
|
||||
{
|
||||
public AggregateType Type { get; }
|
||||
public string Name { get; }
|
||||
public int ArrayLength { get; }
|
||||
|
||||
public StructureField(AggregateType type, string name, int arrayLength = 1)
|
||||
{
|
||||
Type = type;
|
||||
Name = name;
|
||||
ArrayLength = arrayLength;
|
||||
}
|
||||
}
|
||||
|
||||
class StructureType
|
||||
{
|
||||
public StructureField[] Fields { get; }
|
||||
|
||||
public StructureType(StructureField[] fields)
|
||||
{
|
||||
Fields = fields;
|
||||
}
|
||||
}
|
||||
}
|
||||
42
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/StructuredFunction.cs
vendored
Normal file
42
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/StructuredFunction.cs
vendored
Normal file
@@ -0,0 +1,42 @@
|
||||
using Ryujinx.Graphics.Shader.Translation;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Ryujinx.Graphics.Shader.StructuredIr
|
||||
{
|
||||
class StructuredFunction
|
||||
{
|
||||
public AstBlock MainBlock { get; }
|
||||
|
||||
public string Name { get; }
|
||||
|
||||
public AggregateType ReturnType { get; }
|
||||
|
||||
public AggregateType[] InArguments { get; }
|
||||
public AggregateType[] OutArguments { get; }
|
||||
|
||||
public HashSet<AstOperand> Locals { get; }
|
||||
|
||||
public StructuredFunction(
|
||||
AstBlock mainBlock,
|
||||
string name,
|
||||
AggregateType returnType,
|
||||
AggregateType[] inArguments,
|
||||
AggregateType[] outArguments)
|
||||
{
|
||||
MainBlock = mainBlock;
|
||||
Name = name;
|
||||
ReturnType = returnType;
|
||||
InArguments = inArguments;
|
||||
OutArguments = outArguments;
|
||||
|
||||
Locals = [];
|
||||
}
|
||||
|
||||
public AggregateType GetArgumentType(int index)
|
||||
{
|
||||
return index >= InArguments.Length
|
||||
? OutArguments[index - InArguments.Length]
|
||||
: InArguments[index];
|
||||
}
|
||||
}
|
||||
}
|
||||
467
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/StructuredProgram.cs
vendored
Normal file
467
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/StructuredProgram.cs
vendored
Normal file
@@ -0,0 +1,467 @@
|
||||
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
|
||||
using Ryujinx.Graphics.Shader.Translation;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Numerics;
|
||||
|
||||
namespace Ryujinx.Graphics.Shader.StructuredIr
|
||||
{
|
||||
static class StructuredProgram
|
||||
{
|
||||
// TODO: Eventually it should be possible to specify the parameter types for the function instead of using S32 for everything.
|
||||
private const AggregateType FuncParameterType = AggregateType.S32;
|
||||
|
||||
public static StructuredProgramInfo MakeStructuredProgram(
|
||||
IReadOnlyList<Function> functions,
|
||||
AttributeUsage attributeUsage,
|
||||
ShaderDefinitions definitions,
|
||||
ResourceManager resourceManager,
|
||||
TargetLanguage targetLanguage,
|
||||
bool debugMode)
|
||||
{
|
||||
StructuredProgramContext context = new(attributeUsage, definitions, resourceManager, debugMode);
|
||||
|
||||
for (int funcIndex = 0; funcIndex < functions.Count; funcIndex++)
|
||||
{
|
||||
Function function = functions[funcIndex];
|
||||
|
||||
BasicBlock[] blocks = function.Blocks;
|
||||
|
||||
AggregateType returnType = function.ReturnsValue ? FuncParameterType : AggregateType.Void;
|
||||
|
||||
AggregateType[] inArguments = new AggregateType[function.InArgumentsCount];
|
||||
AggregateType[] outArguments = new AggregateType[function.OutArgumentsCount];
|
||||
|
||||
for (int i = 0; i < inArguments.Length; i++)
|
||||
{
|
||||
inArguments[i] = FuncParameterType;
|
||||
}
|
||||
|
||||
for (int i = 0; i < outArguments.Length; i++)
|
||||
{
|
||||
outArguments[i] = FuncParameterType;
|
||||
}
|
||||
|
||||
context.EnterFunction(blocks.Length, function.Name, returnType, inArguments, outArguments);
|
||||
|
||||
PhiFunctions.Remove(blocks);
|
||||
|
||||
for (int blkIndex = 0; blkIndex < blocks.Length; blkIndex++)
|
||||
{
|
||||
BasicBlock block = blocks[blkIndex];
|
||||
|
||||
context.EnterBlock(block);
|
||||
|
||||
for (LinkedListNode<INode> opNode = block.Operations.First; opNode != null; opNode = opNode.Next)
|
||||
{
|
||||
Operation operation = (Operation)opNode.Value;
|
||||
|
||||
if (IsBranchInst(operation.Inst))
|
||||
{
|
||||
context.LeaveBlock(block, operation);
|
||||
}
|
||||
else
|
||||
{
|
||||
AddOperation(context, operation, targetLanguage, functions);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
GotoElimination.Eliminate(context.GetGotos());
|
||||
|
||||
AstOptimizer.Optimize(context);
|
||||
|
||||
context.LeaveFunction();
|
||||
}
|
||||
|
||||
return context.Info;
|
||||
}
|
||||
|
||||
private static void AddOperation(StructuredProgramContext context, Operation operation, TargetLanguage targetLanguage, IReadOnlyList<Function> functions)
|
||||
{
|
||||
Instruction inst = operation.Inst;
|
||||
StorageKind storageKind = operation.StorageKind;
|
||||
|
||||
if (inst is Instruction.Load or Instruction.Store)
|
||||
{
|
||||
if (storageKind.IsInputOrOutput())
|
||||
{
|
||||
IoVariable ioVariable = (IoVariable)operation.GetSource(0).Value;
|
||||
bool isOutput = storageKind.IsOutput();
|
||||
int location = 0;
|
||||
int component = 0;
|
||||
|
||||
if (context.Definitions.HasPerLocationInputOrOutput(ioVariable, isOutput))
|
||||
{
|
||||
location = operation.GetSource(1).Value;
|
||||
|
||||
if (operation.SourcesCount > 2 &&
|
||||
operation.GetSource(2).Type == OperandType.Constant &&
|
||||
context.Definitions.HasPerLocationInputOrOutputComponent(ioVariable, location, operation.GetSource(2).Value, isOutput))
|
||||
{
|
||||
component = operation.GetSource(2).Value;
|
||||
}
|
||||
}
|
||||
|
||||
context.Info.IoDefinitions.Add(new IoDefinition(storageKind, ioVariable, location, component));
|
||||
}
|
||||
else if (storageKind == StorageKind.ConstantBuffer && operation.GetSource(0).Type == OperandType.Constant)
|
||||
{
|
||||
context.ResourceManager.SetUsedConstantBufferBinding(operation.GetSource(0).Value);
|
||||
}
|
||||
}
|
||||
|
||||
bool vectorDest = IsVectorDestInst(inst);
|
||||
|
||||
int sourcesCount = operation.SourcesCount;
|
||||
int outDestsCount = operation.DestsCount != 0 && !vectorDest ? operation.DestsCount - 1 : 0;
|
||||
|
||||
IAstNode[] sources = new IAstNode[sourcesCount + outDestsCount];
|
||||
|
||||
if (inst == Instruction.Call && targetLanguage == TargetLanguage.Spirv)
|
||||
{
|
||||
// SPIR-V requires that all function parameters are copied to a local variable before the call
|
||||
// (or at least that's what the Khronos compiler does).
|
||||
|
||||
// First one is the function index.
|
||||
Operand funcIndexOperand = operation.GetSource(0);
|
||||
Debug.Assert(funcIndexOperand.Type == OperandType.Constant);
|
||||
int funcIndex = funcIndexOperand.Value;
|
||||
|
||||
sources[0] = new AstOperand(OperandType.Constant, funcIndex);
|
||||
|
||||
int inArgsCount = functions[funcIndex].InArgumentsCount;
|
||||
|
||||
// Remaining ones are parameters, copy them to a temp local variable.
|
||||
for (int index = 1; index < operation.SourcesCount; index++)
|
||||
{
|
||||
IAstNode source = context.GetOperandOrCbLoad(operation.GetSource(index));
|
||||
|
||||
if (index - 1 < inArgsCount)
|
||||
{
|
||||
AstOperand argTemp = context.NewTemp(FuncParameterType);
|
||||
context.AddNode(new AstAssignment(argTemp, source));
|
||||
sources[index] = argTemp;
|
||||
}
|
||||
else
|
||||
{
|
||||
sources[index] = source;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int index = 0; index < operation.SourcesCount; index++)
|
||||
{
|
||||
sources[index] = context.GetOperandOrCbLoad(operation.GetSource(index));
|
||||
}
|
||||
}
|
||||
|
||||
for (int index = 0; index < outDestsCount; index++)
|
||||
{
|
||||
AstOperand oper = context.GetOperand(operation.GetDest(1 + index));
|
||||
|
||||
oper.VarType = InstructionInfo.GetSrcVarType(inst, sourcesCount + index);
|
||||
|
||||
sources[sourcesCount + index] = oper;
|
||||
}
|
||||
|
||||
AstTextureOperation GetAstTextureOperation(TextureOperation texOp)
|
||||
{
|
||||
return new AstTextureOperation(
|
||||
inst,
|
||||
texOp.Type,
|
||||
texOp.Format,
|
||||
texOp.Flags,
|
||||
texOp.Set,
|
||||
texOp.Binding,
|
||||
texOp.SamplerSet,
|
||||
texOp.SamplerBinding,
|
||||
texOp.Index,
|
||||
sources);
|
||||
}
|
||||
|
||||
int componentsCount = BitOperations.PopCount((uint)operation.Index);
|
||||
|
||||
if (vectorDest && componentsCount > 1)
|
||||
{
|
||||
AggregateType destType = InstructionInfo.GetDestVarType(inst);
|
||||
|
||||
IAstNode source;
|
||||
|
||||
if (operation is TextureOperation texOp)
|
||||
{
|
||||
if (texOp.Inst == Instruction.ImageLoad)
|
||||
{
|
||||
destType = texOp.Format.GetComponentType();
|
||||
}
|
||||
|
||||
source = GetAstTextureOperation(texOp);
|
||||
}
|
||||
else
|
||||
{
|
||||
source = new AstOperation(
|
||||
inst,
|
||||
operation.StorageKind,
|
||||
operation.ForcePrecise,
|
||||
operation.Index,
|
||||
sources,
|
||||
operation.SourcesCount);
|
||||
}
|
||||
|
||||
AggregateType destElemType = destType;
|
||||
|
||||
switch (componentsCount)
|
||||
{
|
||||
case 2:
|
||||
destType |= AggregateType.Vector2;
|
||||
break;
|
||||
case 3:
|
||||
destType |= AggregateType.Vector3;
|
||||
break;
|
||||
case 4:
|
||||
destType |= AggregateType.Vector4;
|
||||
break;
|
||||
}
|
||||
|
||||
AstOperand destVec = context.NewTemp(destType);
|
||||
|
||||
context.AddNode(new AstAssignment(destVec, source));
|
||||
|
||||
for (int i = 0; i < operation.DestsCount; i++)
|
||||
{
|
||||
AstOperand dest = context.GetOperand(operation.GetDest(i));
|
||||
AstOperand index = new(OperandType.Constant, i);
|
||||
|
||||
dest.VarType = destElemType;
|
||||
|
||||
context.AddNode(new AstAssignment(dest, new AstOperation(Instruction.VectorExtract, StorageKind.None, false,
|
||||
[destVec, index], 2)));
|
||||
}
|
||||
}
|
||||
else if (operation.Dest != null)
|
||||
{
|
||||
AstOperand dest = context.GetOperand(operation.Dest);
|
||||
|
||||
// If all the sources are bool, it's better to use short-circuiting
|
||||
// logical operations, rather than forcing a cast to int and doing
|
||||
// a bitwise operation with the value, as it is likely to be used as
|
||||
// a bool in the end.
|
||||
if (IsBitwiseInst(inst) && AreAllSourceTypesEqual(sources, AggregateType.Bool))
|
||||
{
|
||||
inst = GetLogicalFromBitwiseInst(inst);
|
||||
}
|
||||
|
||||
bool isCondSel = inst == Instruction.ConditionalSelect;
|
||||
bool isCopy = inst == Instruction.Copy;
|
||||
|
||||
if (isCondSel || isCopy)
|
||||
{
|
||||
AggregateType type = GetVarTypeFromUses(operation.Dest);
|
||||
|
||||
if (isCondSel && type == AggregateType.FP32)
|
||||
{
|
||||
inst |= Instruction.FP32;
|
||||
}
|
||||
|
||||
dest.VarType = type;
|
||||
}
|
||||
else
|
||||
{
|
||||
dest.VarType = InstructionInfo.GetDestVarType(inst);
|
||||
}
|
||||
|
||||
IAstNode source;
|
||||
|
||||
if (operation is TextureOperation texOp)
|
||||
{
|
||||
if (texOp.Inst == Instruction.ImageLoad)
|
||||
{
|
||||
dest.VarType = texOp.Format.GetComponentType();
|
||||
}
|
||||
|
||||
source = GetAstTextureOperation(texOp);
|
||||
}
|
||||
else if (!isCopy)
|
||||
{
|
||||
source = new AstOperation(
|
||||
inst,
|
||||
operation.StorageKind,
|
||||
operation.ForcePrecise,
|
||||
operation.Index,
|
||||
sources,
|
||||
operation.SourcesCount);
|
||||
}
|
||||
else
|
||||
{
|
||||
source = sources[0];
|
||||
}
|
||||
|
||||
context.AddNode(new AstAssignment(dest, source));
|
||||
}
|
||||
else if (operation.Inst == Instruction.Comment)
|
||||
{
|
||||
context.AddNode(new AstComment(((CommentNode)operation).Comment));
|
||||
}
|
||||
else if (operation is TextureOperation texOp)
|
||||
{
|
||||
AstTextureOperation astTexOp = GetAstTextureOperation(texOp);
|
||||
|
||||
context.AddNode(astTexOp);
|
||||
}
|
||||
else
|
||||
{
|
||||
context.AddNode(new AstOperation(
|
||||
inst,
|
||||
operation.StorageKind,
|
||||
operation.ForcePrecise,
|
||||
operation.Index,
|
||||
sources,
|
||||
operation.SourcesCount));
|
||||
}
|
||||
|
||||
// Those instructions needs to be emulated by using helper functions,
|
||||
// because they are NVIDIA specific. Those flags helps the backend to
|
||||
// decide which helper functions are needed on the final generated code.
|
||||
switch (operation.Inst)
|
||||
{
|
||||
case Instruction.MultiplyHighS32:
|
||||
context.Info.HelperFunctionsMask |= HelperFunctionsMask.MultiplyHighS32;
|
||||
break;
|
||||
case Instruction.MultiplyHighU32:
|
||||
context.Info.HelperFunctionsMask |= HelperFunctionsMask.MultiplyHighU32;
|
||||
break;
|
||||
case Instruction.SwizzleAdd:
|
||||
context.Info.HelperFunctionsMask |= HelperFunctionsMask.SwizzleAdd;
|
||||
break;
|
||||
case Instruction.FSIBegin:
|
||||
case Instruction.FSIEnd:
|
||||
context.Info.HelperFunctionsMask |= HelperFunctionsMask.FSI;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private static AggregateType GetVarTypeFromUses(Operand dest)
|
||||
{
|
||||
HashSet<Operand> visited = [];
|
||||
|
||||
Queue<Operand> pending = new();
|
||||
|
||||
bool Enqueue(Operand operand)
|
||||
{
|
||||
if (visited.Add(operand))
|
||||
{
|
||||
pending.Enqueue(operand);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
Enqueue(dest);
|
||||
|
||||
while (pending.TryDequeue(out Operand operand))
|
||||
{
|
||||
foreach (INode useNode in operand.UseOps)
|
||||
{
|
||||
if (useNode is not Operation operation)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (operation.Inst == Instruction.Copy)
|
||||
{
|
||||
if (operation.Dest.Type == OperandType.LocalVariable)
|
||||
{
|
||||
if (Enqueue(operation.Dest))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return OperandInfo.GetVarType(operation.Dest.Type);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int index = 0; index < operation.SourcesCount; index++)
|
||||
{
|
||||
if (operation.GetSource(index) == operand)
|
||||
{
|
||||
return InstructionInfo.GetSrcVarType(operation.Inst, index);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return AggregateType.S32;
|
||||
}
|
||||
|
||||
private static bool AreAllSourceTypesEqual(IAstNode[] sources, AggregateType type)
|
||||
{
|
||||
foreach (IAstNode node in sources)
|
||||
{
|
||||
if (node is not AstOperand operand)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (operand.VarType != type)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool IsVectorDestInst(Instruction inst)
|
||||
{
|
||||
return inst switch
|
||||
{
|
||||
Instruction.ImageLoad or
|
||||
Instruction.TextureSample => true,
|
||||
_ => false,
|
||||
};
|
||||
}
|
||||
|
||||
private static bool IsBranchInst(Instruction inst)
|
||||
{
|
||||
return inst switch
|
||||
{
|
||||
Instruction.Branch or
|
||||
Instruction.BranchIfFalse or
|
||||
Instruction.BranchIfTrue => true,
|
||||
_ => false,
|
||||
};
|
||||
}
|
||||
|
||||
private static bool IsBitwiseInst(Instruction inst)
|
||||
{
|
||||
return inst switch
|
||||
{
|
||||
Instruction.BitwiseAnd or
|
||||
Instruction.BitwiseExclusiveOr or
|
||||
Instruction.BitwiseNot or
|
||||
Instruction.BitwiseOr => true,
|
||||
_ => false,
|
||||
};
|
||||
}
|
||||
|
||||
private static Instruction GetLogicalFromBitwiseInst(Instruction inst)
|
||||
{
|
||||
return inst switch
|
||||
{
|
||||
Instruction.BitwiseAnd => Instruction.LogicalAnd,
|
||||
Instruction.BitwiseExclusiveOr => Instruction.LogicalExclusiveOr,
|
||||
Instruction.BitwiseNot => Instruction.LogicalNot,
|
||||
Instruction.BitwiseOr => Instruction.LogicalOr,
|
||||
_ => throw new ArgumentException($"Unexpected instruction \"{inst}\"."),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
354
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/StructuredProgramContext.cs
vendored
Normal file
354
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/StructuredProgramContext.cs
vendored
Normal file
@@ -0,0 +1,354 @@
|
||||
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
|
||||
using Ryujinx.Graphics.Shader.Translation;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using static Ryujinx.Graphics.Shader.StructuredIr.AstHelper;
|
||||
|
||||
namespace Ryujinx.Graphics.Shader.StructuredIr
|
||||
{
|
||||
class StructuredProgramContext
|
||||
{
|
||||
private HashSet<BasicBlock> _loopTails;
|
||||
|
||||
private Stack<(AstBlock Block, int CurrEndIndex, int LoopEndIndex)> _blockStack;
|
||||
|
||||
private Dictionary<Operand, AstOperand> _localsMap;
|
||||
|
||||
private Dictionary<int, AstAssignment> _gotoTempAsgs;
|
||||
|
||||
private List<GotoStatement> _gotos;
|
||||
|
||||
private AstBlock _currBlock;
|
||||
|
||||
private int _currEndIndex;
|
||||
private int _loopEndIndex;
|
||||
|
||||
public StructuredFunction CurrentFunction { get; private set; }
|
||||
|
||||
public StructuredProgramInfo Info { get; }
|
||||
|
||||
public ShaderDefinitions Definitions { get; }
|
||||
public ResourceManager ResourceManager { get; }
|
||||
public bool DebugMode { get; }
|
||||
|
||||
public StructuredProgramContext(
|
||||
AttributeUsage attributeUsage,
|
||||
ShaderDefinitions definitions,
|
||||
ResourceManager resourceManager,
|
||||
bool debugMode)
|
||||
{
|
||||
Info = new StructuredProgramInfo();
|
||||
|
||||
Definitions = definitions;
|
||||
ResourceManager = resourceManager;
|
||||
DebugMode = debugMode;
|
||||
|
||||
if (definitions.GpPassthrough)
|
||||
{
|
||||
int passthroughAttributes = attributeUsage.PassthroughAttributes;
|
||||
while (passthroughAttributes != 0)
|
||||
{
|
||||
int index = BitOperations.TrailingZeroCount(passthroughAttributes);
|
||||
|
||||
Info.IoDefinitions.Add(new IoDefinition(StorageKind.Input, IoVariable.UserDefined, index));
|
||||
|
||||
passthroughAttributes &= ~(1 << index);
|
||||
}
|
||||
|
||||
Info.IoDefinitions.Add(new IoDefinition(StorageKind.Input, IoVariable.Position));
|
||||
Info.IoDefinitions.Add(new IoDefinition(StorageKind.Input, IoVariable.PointSize));
|
||||
Info.IoDefinitions.Add(new IoDefinition(StorageKind.Input, IoVariable.ClipDistance));
|
||||
}
|
||||
}
|
||||
|
||||
public void EnterFunction(
|
||||
int blocksCount,
|
||||
string name,
|
||||
AggregateType returnType,
|
||||
AggregateType[] inArguments,
|
||||
AggregateType[] outArguments)
|
||||
{
|
||||
_loopTails = [];
|
||||
|
||||
_blockStack = new Stack<(AstBlock, int, int)>();
|
||||
|
||||
_localsMap = new Dictionary<Operand, AstOperand>();
|
||||
|
||||
_gotoTempAsgs = new Dictionary<int, AstAssignment>();
|
||||
|
||||
_gotos = [];
|
||||
|
||||
_currBlock = new AstBlock(AstBlockType.Main);
|
||||
|
||||
_currEndIndex = blocksCount;
|
||||
_loopEndIndex = blocksCount;
|
||||
|
||||
CurrentFunction = new StructuredFunction(_currBlock, name, returnType, inArguments, outArguments);
|
||||
}
|
||||
|
||||
public void LeaveFunction()
|
||||
{
|
||||
Info.Functions.Add(CurrentFunction);
|
||||
}
|
||||
|
||||
public void EnterBlock(BasicBlock block)
|
||||
{
|
||||
while (_currEndIndex == block.Index)
|
||||
{
|
||||
(_currBlock, _currEndIndex, _loopEndIndex) = _blockStack.Pop();
|
||||
}
|
||||
|
||||
if (_gotoTempAsgs.TryGetValue(block.Index, out AstAssignment gotoTempAsg))
|
||||
{
|
||||
AddGotoTempReset(block, gotoTempAsg);
|
||||
}
|
||||
|
||||
LookForDoWhileStatements(block);
|
||||
}
|
||||
|
||||
public void LeaveBlock(BasicBlock block, Operation branchOp)
|
||||
{
|
||||
LookForIfStatements(block, branchOp);
|
||||
}
|
||||
|
||||
private void LookForDoWhileStatements(BasicBlock block)
|
||||
{
|
||||
// Check if we have any predecessor whose index is greater than the
|
||||
// current block, this indicates a loop.
|
||||
bool done = false;
|
||||
|
||||
foreach (BasicBlock predecessor in block.Predecessors.OrderByDescending(x => x.Index))
|
||||
{
|
||||
// If not a loop, break.
|
||||
if (predecessor.Index < block.Index)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
// Check if we can create a do-while loop here (only possible if the loop end
|
||||
// falls inside the current scope), if not add a goto instead.
|
||||
if (predecessor.Index < _currEndIndex && !done)
|
||||
{
|
||||
// Create do-while loop block. We must avoid inserting a goto at the end
|
||||
// of the loop later, when the tail block is processed. So we add the predecessor
|
||||
// to a list of loop tails to prevent it from being processed later.
|
||||
Operation branchOp = (Operation)predecessor.GetLastOp();
|
||||
|
||||
NewBlock(AstBlockType.DoWhile, branchOp, predecessor.Index + 1);
|
||||
|
||||
_loopTails.Add(predecessor);
|
||||
|
||||
done = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Failed to create loop. Since this block is the loop head, we reset the
|
||||
// goto condition variable here. The variable is always reset on the jump
|
||||
// target, and this block is the jump target for some loop.
|
||||
AddGotoTempReset(block, GetGotoTempAsg(block.Index));
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void LookForIfStatements(BasicBlock block, Operation branchOp)
|
||||
{
|
||||
if (block.Branch == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// We can only enclose the "if" when the branch lands before
|
||||
// the end of the current block. If the current enclosing block
|
||||
// is not a loop, then we can also do so if the branch lands
|
||||
// right at the end of the current block. When it is a loop,
|
||||
// this is not valid as the loop condition would be evaluated,
|
||||
// and it could erroneously jump back to the start of the loop.
|
||||
bool inRange =
|
||||
block.Branch.Index < _currEndIndex ||
|
||||
(block.Branch.Index == _currEndIndex && block.Branch.Index < _loopEndIndex);
|
||||
|
||||
bool isLoop = block.Branch.Index <= block.Index;
|
||||
|
||||
if (inRange && !isLoop)
|
||||
{
|
||||
NewBlock(AstBlockType.If, branchOp, block.Branch.Index);
|
||||
}
|
||||
else if (!_loopTails.Contains(block))
|
||||
{
|
||||
AstAssignment gotoTempAsg = GetGotoTempAsg(block.Branch.Index);
|
||||
|
||||
// We use DoWhile type here, as the condition should be true for
|
||||
// unconditional branches, or it should jump if the condition is true otherwise.
|
||||
IAstNode cond = GetBranchCond(AstBlockType.DoWhile, branchOp);
|
||||
|
||||
AddNode(Assign(gotoTempAsg.Destination, cond));
|
||||
|
||||
AstOperation branch = new(branchOp.Inst);
|
||||
|
||||
AddNode(branch);
|
||||
|
||||
GotoStatement gotoStmt = new(branch, gotoTempAsg, isLoop);
|
||||
|
||||
_gotos.Add(gotoStmt);
|
||||
}
|
||||
}
|
||||
|
||||
private AstAssignment GetGotoTempAsg(int index)
|
||||
{
|
||||
if (_gotoTempAsgs.TryGetValue(index, out AstAssignment gotoTempAsg))
|
||||
{
|
||||
return gotoTempAsg;
|
||||
}
|
||||
|
||||
AstOperand gotoTemp = NewTemp(AggregateType.Bool);
|
||||
|
||||
gotoTempAsg = Assign(gotoTemp, Const(IrConsts.False));
|
||||
|
||||
_gotoTempAsgs.Add(index, gotoTempAsg);
|
||||
|
||||
return gotoTempAsg;
|
||||
}
|
||||
|
||||
private void AddGotoTempReset(BasicBlock block, AstAssignment gotoTempAsg)
|
||||
{
|
||||
// If it was already added, we don't need to add it again.
|
||||
if (gotoTempAsg.Parent != null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
AddNode(gotoTempAsg);
|
||||
|
||||
// For block 0, we don't need to add the extra "reset" at the beginning,
|
||||
// because it is already the first node to be executed on the shader,
|
||||
// so it is reset to false by the "local" assignment anyway.
|
||||
if (block.Index != 0)
|
||||
{
|
||||
CurrentFunction.MainBlock.AddFirst(Assign(gotoTempAsg.Destination, Const(IrConsts.False)));
|
||||
}
|
||||
}
|
||||
|
||||
private void NewBlock(AstBlockType type, Operation branchOp, int endIndex)
|
||||
{
|
||||
NewBlock(type, GetBranchCond(type, branchOp), endIndex);
|
||||
}
|
||||
|
||||
private void NewBlock(AstBlockType type, IAstNode cond, int endIndex)
|
||||
{
|
||||
AstBlock childBlock = new(type, cond);
|
||||
|
||||
AddNode(childBlock);
|
||||
|
||||
_blockStack.Push((_currBlock, _currEndIndex, _loopEndIndex));
|
||||
|
||||
_currBlock = childBlock;
|
||||
_currEndIndex = endIndex;
|
||||
|
||||
if (type == AstBlockType.DoWhile)
|
||||
{
|
||||
_loopEndIndex = endIndex;
|
||||
}
|
||||
}
|
||||
|
||||
private IAstNode GetBranchCond(AstBlockType type, Operation branchOp)
|
||||
{
|
||||
IAstNode cond;
|
||||
|
||||
if (branchOp.Inst == Instruction.Branch)
|
||||
{
|
||||
// If the branch is not conditional, the condition is a constant.
|
||||
// For if it's false (always jump over, if block never executed).
|
||||
// For loops it's always true (always loop).
|
||||
cond = Const(type == AstBlockType.If ? IrConsts.False : IrConsts.True);
|
||||
}
|
||||
else
|
||||
{
|
||||
cond = GetOperand(branchOp.GetSource(0));
|
||||
|
||||
Instruction invInst = type == AstBlockType.If
|
||||
? Instruction.BranchIfTrue
|
||||
: Instruction.BranchIfFalse;
|
||||
|
||||
if (branchOp.Inst == invInst)
|
||||
{
|
||||
cond = new AstOperation(Instruction.LogicalNot, cond);
|
||||
}
|
||||
}
|
||||
|
||||
return cond;
|
||||
}
|
||||
|
||||
public void AddNode(IAstNode node)
|
||||
{
|
||||
_currBlock.Add(node);
|
||||
}
|
||||
|
||||
public GotoStatement[] GetGotos()
|
||||
{
|
||||
return _gotos.ToArray();
|
||||
}
|
||||
|
||||
public AstOperand NewTemp(AggregateType type)
|
||||
{
|
||||
AstOperand newTemp = Local(type);
|
||||
|
||||
CurrentFunction.Locals.Add(newTemp);
|
||||
|
||||
return newTemp;
|
||||
}
|
||||
|
||||
public IAstNode GetOperandOrCbLoad(Operand operand)
|
||||
{
|
||||
if (operand.Type == OperandType.ConstantBuffer)
|
||||
{
|
||||
int cbufSlot = operand.GetCbufSlot();
|
||||
int cbufOffset = operand.GetCbufOffset();
|
||||
|
||||
int binding = ResourceManager.GetConstantBufferBinding(cbufSlot);
|
||||
int vecIndex = cbufOffset >> 2;
|
||||
int elemIndex = cbufOffset & 3;
|
||||
|
||||
ResourceManager.SetUsedConstantBufferBinding(binding);
|
||||
|
||||
IAstNode[] sources =
|
||||
[
|
||||
new AstOperand(OperandType.Constant, binding),
|
||||
new AstOperand(OperandType.Constant, 0),
|
||||
new AstOperand(OperandType.Constant, vecIndex),
|
||||
new AstOperand(OperandType.Constant, elemIndex)
|
||||
];
|
||||
|
||||
return new AstOperation(Instruction.Load, StorageKind.ConstantBuffer, false, sources, sources.Length);
|
||||
}
|
||||
|
||||
return GetOperand(operand);
|
||||
}
|
||||
|
||||
public AstOperand GetOperand(Operand operand)
|
||||
{
|
||||
if (operand == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (operand.Type != OperandType.LocalVariable)
|
||||
{
|
||||
return new AstOperand(operand);
|
||||
}
|
||||
|
||||
if (!_localsMap.TryGetValue(operand, out AstOperand astOperand))
|
||||
{
|
||||
astOperand = new AstOperand(operand);
|
||||
|
||||
_localsMap.Add(operand, astOperand);
|
||||
|
||||
CurrentFunction.Locals.Add(astOperand);
|
||||
}
|
||||
|
||||
return astOperand;
|
||||
}
|
||||
}
|
||||
}
|
||||
20
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/StructuredProgramInfo.cs
vendored
Normal file
20
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/StructuredProgramInfo.cs
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Ryujinx.Graphics.Shader.StructuredIr
|
||||
{
|
||||
class StructuredProgramInfo
|
||||
{
|
||||
public List<StructuredFunction> Functions { get; }
|
||||
|
||||
public HashSet<IoDefinition> IoDefinitions { get; }
|
||||
|
||||
public HelperFunctionsMask HelperFunctionsMask { get; set; }
|
||||
|
||||
public StructuredProgramInfo()
|
||||
{
|
||||
Functions = new List<StructuredFunction>();
|
||||
|
||||
IoDefinitions = new HashSet<IoDefinition>();
|
||||
}
|
||||
}
|
||||
}
|
||||
43
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/TextureDefinition.cs
vendored
Normal file
43
tools/vendor/ryujinx/src/Ryujinx.Graphics.Shader/StructuredIr/TextureDefinition.cs
vendored
Normal file
@@ -0,0 +1,43 @@
|
||||
namespace Ryujinx.Graphics.Shader
|
||||
{
|
||||
readonly struct TextureDefinition
|
||||
{
|
||||
public int Set { get; }
|
||||
public int Binding { get; }
|
||||
public int ArrayLength { get; }
|
||||
public bool Separate { get; }
|
||||
public string Name { get; }
|
||||
public SamplerType Type { get; }
|
||||
public TextureFormat Format { get; }
|
||||
public TextureUsageFlags Flags { get; }
|
||||
|
||||
public TextureDefinition(
|
||||
int set,
|
||||
int binding,
|
||||
int arrayLength,
|
||||
bool separate,
|
||||
string name,
|
||||
SamplerType type,
|
||||
TextureFormat format,
|
||||
TextureUsageFlags flags)
|
||||
{
|
||||
Set = set;
|
||||
Binding = binding;
|
||||
ArrayLength = arrayLength;
|
||||
Separate = separate;
|
||||
Name = name;
|
||||
Type = type;
|
||||
Format = format;
|
||||
Flags = flags;
|
||||
}
|
||||
|
||||
public TextureDefinition(int set, int binding, string name, SamplerType type) : this(set, binding, 1, false, name, type, TextureFormat.Unknown, TextureUsageFlags.None)
|
||||
{
|
||||
}
|
||||
|
||||
public TextureDefinition SetFlag(TextureUsageFlags flag)
|
||||
{
|
||||
return new TextureDefinition(Set, Binding, ArrayLength, Separate, Name, Type, Format, Flags | flag);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user