2023-08-04 03:21:22 +00:00
|
|
|
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
|
|
|
|
using Ryujinx.Graphics.Shader.StructuredIr;
|
|
|
|
using Ryujinx.Graphics.Shader.Translation;
|
|
|
|
using System;
|
2024-06-21 09:31:21 +00:00
|
|
|
using System.Text;
|
2024-06-21 15:58:58 +00:00
|
|
|
using static Ryujinx.Graphics.Shader.CodeGen.Msl.Instructions.InstGenBallot;
|
2024-06-22 13:38:09 +00:00
|
|
|
using static Ryujinx.Graphics.Shader.CodeGen.Msl.Instructions.InstGenBarrier;
|
2023-08-15 13:17:00 +00:00
|
|
|
using static Ryujinx.Graphics.Shader.CodeGen.Msl.Instructions.InstGenCall;
|
2023-08-04 03:21:22 +00:00
|
|
|
using static Ryujinx.Graphics.Shader.CodeGen.Msl.Instructions.InstGenHelper;
|
2023-08-15 13:17:00 +00:00
|
|
|
using static Ryujinx.Graphics.Shader.CodeGen.Msl.Instructions.InstGenMemory;
|
|
|
|
using static Ryujinx.Graphics.Shader.CodeGen.Msl.Instructions.InstGenVector;
|
2023-08-04 03:21:22 +00:00
|
|
|
using static Ryujinx.Graphics.Shader.StructuredIr.InstructionInfo;
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Shader.CodeGen.Msl.Instructions
|
|
|
|
{
|
|
|
|
static class InstGen
|
|
|
|
{
|
|
|
|
public static string GetExpression(CodeGenContext context, IAstNode node)
|
|
|
|
{
|
|
|
|
if (node is AstOperation operation)
|
|
|
|
{
|
|
|
|
return GetExpression(context, operation);
|
|
|
|
}
|
|
|
|
else if (node is AstOperand operand)
|
|
|
|
{
|
|
|
|
return context.OperandManager.GetExpression(context, operand);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new ArgumentException($"Invalid node type \"{node?.GetType().Name ?? "null"}\".");
|
|
|
|
}
|
|
|
|
|
|
|
|
private static string GetExpression(CodeGenContext context, AstOperation operation)
|
|
|
|
{
|
|
|
|
Instruction inst = operation.Inst;
|
|
|
|
|
|
|
|
InstInfo info = GetInstructionInfo(inst);
|
|
|
|
|
|
|
|
if ((info.Type & InstType.Call) != 0)
|
|
|
|
{
|
|
|
|
bool atomic = (info.Type & InstType.Atomic) != 0;
|
|
|
|
|
|
|
|
int arity = (int)(info.Type & InstType.ArityMask);
|
|
|
|
|
2024-06-21 09:31:21 +00:00
|
|
|
StringBuilder builder = new();
|
2023-08-04 03:21:22 +00:00
|
|
|
|
2024-06-21 09:31:21 +00:00
|
|
|
if (atomic && (operation.StorageKind == StorageKind.StorageBuffer || operation.StorageKind == StorageKind.SharedMemory))
|
2023-08-04 03:21:22 +00:00
|
|
|
{
|
2024-06-21 09:31:21 +00:00
|
|
|
AggregateType dstType = operation.Inst == Instruction.AtomicMaxS32 || operation.Inst == Instruction.AtomicMinS32
|
|
|
|
? AggregateType.S32
|
|
|
|
: AggregateType.U32;
|
|
|
|
|
2024-06-25 13:25:31 +00:00
|
|
|
builder.Append($"(device {Declarations.GetVarTypeName(dstType, true)}*)&{GenerateLoadOrStore(context, operation, isStore: false)}");
|
2024-06-21 15:58:58 +00:00
|
|
|
|
2024-06-21 09:31:21 +00:00
|
|
|
for (int argIndex = operation.SourcesCount - arity + 2; argIndex < operation.SourcesCount; argIndex++)
|
|
|
|
{
|
2024-06-21 15:58:58 +00:00
|
|
|
builder.Append($", {GetSourceExpr(context, operation.GetSource(argIndex), dstType)}, memory_order_relaxed");
|
2024-06-21 09:31:21 +00:00
|
|
|
}
|
2023-08-04 03:21:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (int argIndex = 0; argIndex < arity; argIndex++)
|
|
|
|
{
|
|
|
|
if (argIndex != 0)
|
|
|
|
{
|
2024-06-21 09:31:21 +00:00
|
|
|
builder.Append(", ");
|
2023-08-04 03:21:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AggregateType dstType = GetSrcVarType(inst, argIndex);
|
|
|
|
|
2024-06-21 09:31:21 +00:00
|
|
|
builder.Append(GetSourceExpr(context, operation.GetSource(argIndex), dstType));
|
2023-08-04 03:21:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-21 09:31:21 +00:00
|
|
|
return $"{info.OpName}({builder})";
|
2023-08-04 03:21:22 +00:00
|
|
|
}
|
|
|
|
else if ((info.Type & InstType.Op) != 0)
|
|
|
|
{
|
|
|
|
string op = info.OpName;
|
|
|
|
|
|
|
|
if (inst == Instruction.Return && operation.SourcesCount != 0)
|
|
|
|
{
|
|
|
|
return $"{op} {GetSourceExpr(context, operation.GetSource(0), context.CurrentFunction.ReturnType)}";
|
|
|
|
}
|
2023-10-11 00:00:56 +00:00
|
|
|
if (inst == Instruction.Return && context.Definitions.Stage is ShaderStage.Vertex or ShaderStage.Fragment)
|
2023-10-09 15:33:28 +00:00
|
|
|
{
|
|
|
|
return $"{op} out";
|
|
|
|
}
|
2023-08-04 03:21:22 +00:00
|
|
|
|
|
|
|
int arity = (int)(info.Type & InstType.ArityMask);
|
|
|
|
|
|
|
|
string[] expr = new string[arity];
|
|
|
|
|
|
|
|
for (int index = 0; index < arity; index++)
|
|
|
|
{
|
|
|
|
IAstNode src = operation.GetSource(index);
|
|
|
|
|
|
|
|
string srcExpr = GetSourceExpr(context, src, GetSrcVarType(inst, index));
|
|
|
|
|
|
|
|
bool isLhs = arity == 2 && index == 0;
|
|
|
|
|
|
|
|
expr[index] = Enclose(srcExpr, src, inst, info, isLhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (arity)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
return op;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
return op + expr[0];
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
return $"{expr[0]} {op} {expr[1]}";
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
return $"{expr[0]} {op[0]} {expr[1]} {op[1]} {expr[2]}";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ((info.Type & InstType.Special) != 0)
|
|
|
|
{
|
|
|
|
switch (inst & Instruction.Mask)
|
|
|
|
{
|
2024-06-21 15:58:58 +00:00
|
|
|
case Instruction.Ballot:
|
|
|
|
return Ballot(context, operation);
|
2023-08-04 03:21:22 +00:00
|
|
|
case Instruction.Barrier:
|
2024-06-22 13:38:09 +00:00
|
|
|
return Barrier(context, operation);
|
2023-08-04 03:21:22 +00:00
|
|
|
case Instruction.Call:
|
2023-08-15 13:17:00 +00:00
|
|
|
return Call(context, operation);
|
2023-08-04 03:21:22 +00:00
|
|
|
case Instruction.FSIBegin:
|
|
|
|
return "|| FSI BEGIN ||";
|
|
|
|
case Instruction.FSIEnd:
|
|
|
|
return "|| FSI END ||";
|
|
|
|
case Instruction.GroupMemoryBarrier:
|
|
|
|
return "|| FIND GROUP MEMORY BARRIER ||";
|
|
|
|
case Instruction.ImageLoad:
|
|
|
|
return "|| IMAGE LOAD ||";
|
|
|
|
case Instruction.ImageStore:
|
|
|
|
return "|| IMAGE STORE ||";
|
|
|
|
case Instruction.ImageAtomic:
|
|
|
|
return "|| IMAGE ATOMIC ||";
|
|
|
|
case Instruction.Load:
|
2023-08-15 13:17:00 +00:00
|
|
|
return Load(context, operation);
|
2023-08-04 03:21:22 +00:00
|
|
|
case Instruction.Lod:
|
2024-05-29 15:21:59 +00:00
|
|
|
return Lod(context, operation);
|
2023-08-04 03:21:22 +00:00
|
|
|
case Instruction.MemoryBarrier:
|
|
|
|
return "|| MEMORY BARRIER ||";
|
|
|
|
case Instruction.Store:
|
2023-08-15 13:17:00 +00:00
|
|
|
return Store(context, operation);
|
2024-06-22 13:38:09 +00:00
|
|
|
case Instruction.SwizzleAdd:
|
|
|
|
return "|| SWIZZLE ADD ||";
|
2023-08-04 03:21:22 +00:00
|
|
|
case Instruction.TextureSample:
|
2023-08-15 13:17:00 +00:00
|
|
|
return TextureSample(context, operation);
|
2023-10-09 15:12:04 +00:00
|
|
|
case Instruction.TextureQuerySamples:
|
2023-10-09 15:16:33 +00:00
|
|
|
return TextureQuerySamples(context, operation);
|
2023-10-09 15:12:04 +00:00
|
|
|
case Instruction.TextureQuerySize:
|
|
|
|
return TextureQuerySize(context, operation);
|
2024-05-30 01:14:56 +00:00
|
|
|
case Instruction.PackHalf2x16:
|
|
|
|
return PackHalf2x16(context, operation);
|
|
|
|
case Instruction.UnpackHalf2x16:
|
|
|
|
return UnpackHalf2x16(context, operation);
|
2023-08-04 03:21:22 +00:00
|
|
|
case Instruction.VectorExtract:
|
2023-08-15 13:17:00 +00:00
|
|
|
return VectorExtract(context, operation);
|
2023-08-04 03:21:22 +00:00
|
|
|
case Instruction.VoteAllEqual:
|
2024-06-22 13:38:09 +00:00
|
|
|
return VoteAllEqual(context, operation);
|
2023-08-04 03:21:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-15 13:17:00 +00:00
|
|
|
// TODO: Return this to being an error
|
|
|
|
return $"Unexpected instruction type \"{info.Type}\".";
|
2023-08-04 03:21:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|