2018-05-17 14:25:42 -04:00
|
|
|
using System;
|
|
|
|
|
2018-04-08 15:17:35 -04:00
|
|
|
using static Ryujinx.Graphics.Gal.Shader.ShaderDecodeHelper;
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Gal.Shader
|
|
|
|
{
|
|
|
|
static partial class ShaderDecode
|
|
|
|
{
|
2018-07-03 19:06:13 -04:00
|
|
|
private const int ____ = 0x0;
|
|
|
|
private const int R___ = 0x1;
|
|
|
|
private const int _G__ = 0x2;
|
|
|
|
private const int RG__ = 0x3;
|
|
|
|
private const int __B_ = 0x4;
|
|
|
|
private const int RGB_ = 0x7;
|
|
|
|
private const int ___A = 0x8;
|
|
|
|
private const int R__A = 0x9;
|
|
|
|
private const int _G_A = 0xa;
|
|
|
|
private const int RG_A = 0xb;
|
|
|
|
private const int __BA = 0xc;
|
|
|
|
private const int R_BA = 0xd;
|
|
|
|
private const int _GBA = 0xe;
|
|
|
|
private const int RGBA = 0xf;
|
|
|
|
|
|
|
|
private static int[,] MaskLut = new int[,]
|
|
|
|
{
|
|
|
|
{ ____, ____, ____, ____, ____, ____, ____, ____ },
|
|
|
|
{ R___, _G__, __B_, ___A, RG__, R__A, _G_A, __BA },
|
2018-08-13 18:46:36 -04:00
|
|
|
{ R___, _G__, __B_, ___A, RG__, ____, ____, ____ },
|
2018-07-03 19:06:13 -04:00
|
|
|
{ RGB_, RG_A, R_BA, _GBA, RGBA, ____, ____, ____ }
|
|
|
|
};
|
|
|
|
|
2018-09-18 00:30:35 -04:00
|
|
|
public static void Ld_A(ShaderIrBlock Block, long OpCode, int Position)
|
2018-04-08 15:17:35 -04:00
|
|
|
{
|
2018-09-08 13:51:50 -04:00
|
|
|
ShaderIrNode[] Opers = OpCode.Abuf20();
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2018-07-19 01:33:27 -04:00
|
|
|
//Used by GS
|
2018-09-08 13:51:50 -04:00
|
|
|
ShaderIrOperGpr Vertex = OpCode.Gpr39();
|
2018-07-19 01:33:27 -04:00
|
|
|
|
2018-04-08 15:17:35 -04:00
|
|
|
int Index = 0;
|
|
|
|
|
|
|
|
foreach (ShaderIrNode OperA in Opers)
|
|
|
|
{
|
2018-09-08 13:51:50 -04:00
|
|
|
ShaderIrOperGpr OperD = OpCode.Gpr0();
|
2018-04-08 15:17:35 -04:00
|
|
|
|
|
|
|
OperD.Index += Index++;
|
|
|
|
|
2018-09-08 13:51:50 -04:00
|
|
|
Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OperD, OperA)));
|
2018-04-08 15:17:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-18 00:30:35 -04:00
|
|
|
public static void Ld_C(ShaderIrBlock Block, long OpCode, int Position)
|
2018-05-17 14:25:42 -04:00
|
|
|
{
|
2018-09-08 13:51:50 -04:00
|
|
|
int CbufPos = OpCode.Read(22, 0x3fff);
|
|
|
|
int CbufIndex = OpCode.Read(36, 0x1f);
|
|
|
|
int Type = OpCode.Read(48, 7);
|
2018-05-17 14:25:42 -04:00
|
|
|
|
|
|
|
if (Type > 5)
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
}
|
|
|
|
|
2018-08-20 22:31:10 -04:00
|
|
|
ShaderIrOperGpr Temp = ShaderIrOperGpr.MakeTemporary();
|
|
|
|
|
2018-09-08 13:51:50 -04:00
|
|
|
Block.AddNode(new ShaderIrAsg(Temp, OpCode.Gpr8()));
|
2018-08-20 22:31:10 -04:00
|
|
|
|
2018-05-17 14:25:42 -04:00
|
|
|
int Count = Type == 5 ? 2 : 1;
|
|
|
|
|
|
|
|
for (int Index = 0; Index < Count; Index++)
|
|
|
|
{
|
2018-08-20 22:31:10 -04:00
|
|
|
ShaderIrOperCbuf OperA = new ShaderIrOperCbuf(CbufIndex, CbufPos, Temp);
|
|
|
|
|
2018-09-08 13:51:50 -04:00
|
|
|
ShaderIrOperGpr OperD = OpCode.Gpr0();
|
2018-05-17 14:25:42 -04:00
|
|
|
|
|
|
|
OperA.Pos += Index;
|
|
|
|
OperD.Index += Index;
|
|
|
|
|
2018-08-20 22:31:10 -04:00
|
|
|
if (!OperD.IsValidRegister)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-05-17 14:25:42 -04:00
|
|
|
ShaderIrNode Node = OperA;
|
|
|
|
|
|
|
|
if (Type < 4)
|
|
|
|
{
|
|
|
|
//This is a 8 or 16 bits type.
|
|
|
|
bool Signed = (Type & 1) != 0;
|
|
|
|
|
|
|
|
int Size = 8 << (Type >> 1);
|
|
|
|
|
|
|
|
Node = ExtendTo32(Node, Signed, Size);
|
|
|
|
}
|
|
|
|
|
2018-09-08 13:51:50 -04:00
|
|
|
Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OperD, Node)));
|
2018-05-17 14:25:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-18 00:30:35 -04:00
|
|
|
public static void St_A(ShaderIrBlock Block, long OpCode, int Position)
|
2018-04-08 15:17:35 -04:00
|
|
|
{
|
2018-09-08 13:51:50 -04:00
|
|
|
ShaderIrNode[] Opers = OpCode.Abuf20();
|
2018-04-08 15:17:35 -04:00
|
|
|
|
|
|
|
int Index = 0;
|
|
|
|
|
|
|
|
foreach (ShaderIrNode OperA in Opers)
|
|
|
|
{
|
2018-09-08 13:51:50 -04:00
|
|
|
ShaderIrOperGpr OperD = OpCode.Gpr0();
|
2018-04-08 15:17:35 -04:00
|
|
|
|
|
|
|
OperD.Index += Index++;
|
|
|
|
|
2018-09-08 13:51:50 -04:00
|
|
|
Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OperA, OperD)));
|
2018-04-08 15:17:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-18 00:30:35 -04:00
|
|
|
public static void Texq(ShaderIrBlock Block, long OpCode, int Position)
|
2018-04-10 15:50:32 -04:00
|
|
|
{
|
2018-09-08 13:51:50 -04:00
|
|
|
ShaderIrNode OperD = OpCode.Gpr0();
|
|
|
|
ShaderIrNode OperA = OpCode.Gpr8();
|
2018-04-10 15:50:32 -04:00
|
|
|
|
2018-09-08 13:51:50 -04:00
|
|
|
ShaderTexqInfo Info = (ShaderTexqInfo)(OpCode.Read(22, 0x1f));
|
2018-04-10 15:50:32 -04:00
|
|
|
|
|
|
|
ShaderIrMetaTexq Meta0 = new ShaderIrMetaTexq(Info, 0);
|
|
|
|
ShaderIrMetaTexq Meta1 = new ShaderIrMetaTexq(Info, 1);
|
|
|
|
|
2018-09-08 13:51:50 -04:00
|
|
|
ShaderIrNode OperC = OpCode.Imm13_36();
|
2018-04-10 15:50:32 -04:00
|
|
|
|
|
|
|
ShaderIrOp Op0 = new ShaderIrOp(ShaderIrInst.Texq, OperA, null, OperC, Meta0);
|
|
|
|
ShaderIrOp Op1 = new ShaderIrOp(ShaderIrInst.Texq, OperA, null, OperC, Meta1);
|
|
|
|
|
2018-09-08 13:51:50 -04:00
|
|
|
Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OperD, Op0)));
|
|
|
|
Block.AddNode(OpCode.PredNode(new ShaderIrAsg(OperA, Op1))); //Is this right?
|
2018-04-10 15:50:32 -04:00
|
|
|
}
|
|
|
|
|
2018-09-18 00:30:35 -04:00
|
|
|
public static void Tex(ShaderIrBlock Block, long OpCode, int Position)
|
2018-08-13 17:22:09 -04:00
|
|
|
{
|
|
|
|
EmitTex(Block, OpCode, GprHandle: false);
|
|
|
|
}
|
|
|
|
|
2018-09-18 00:30:35 -04:00
|
|
|
public static void Tex_B(ShaderIrBlock Block, long OpCode, int Position)
|
2018-08-13 17:22:09 -04:00
|
|
|
{
|
|
|
|
EmitTex(Block, OpCode, GprHandle: true);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void EmitTex(ShaderIrBlock Block, long OpCode, bool GprHandle)
|
2018-06-01 23:50:56 -04:00
|
|
|
{
|
|
|
|
//TODO: Support other formats.
|
|
|
|
ShaderIrOperGpr[] Coords = new ShaderIrOperGpr[2];
|
|
|
|
|
|
|
|
for (int Index = 0; Index < Coords.Length; Index++)
|
|
|
|
{
|
2019-01-31 07:43:24 -05:00
|
|
|
ShaderIrOperGpr CoordReg = OpCode.Gpr8();
|
2018-06-01 23:50:56 -04:00
|
|
|
|
2019-01-31 07:43:24 -05:00
|
|
|
CoordReg.Index += Index;
|
2018-06-01 23:50:56 -04:00
|
|
|
|
2019-01-31 07:43:24 -05:00
|
|
|
if (!CoordReg.IsValidRegister)
|
2018-06-01 23:50:56 -04:00
|
|
|
{
|
2019-01-31 07:43:24 -05:00
|
|
|
CoordReg.Index = ShaderIrOperGpr.ZRIndex;
|
2018-06-01 23:50:56 -04:00
|
|
|
}
|
2019-01-31 07:43:24 -05:00
|
|
|
|
|
|
|
Coords[Index] = ShaderIrOperGpr.MakeTemporary(Index);
|
|
|
|
|
|
|
|
Block.AddNode(new ShaderIrAsg(Coords[Index], CoordReg));
|
2018-06-01 23:50:56 -04:00
|
|
|
}
|
|
|
|
|
2018-09-08 13:51:50 -04:00
|
|
|
int ChMask = OpCode.Read(31, 0xf);
|
2018-06-01 23:50:56 -04:00
|
|
|
|
2018-08-13 17:22:09 -04:00
|
|
|
ShaderIrNode OperC = GprHandle
|
2018-09-08 13:51:50 -04:00
|
|
|
? (ShaderIrNode)OpCode.Gpr20()
|
|
|
|
: (ShaderIrNode)OpCode.Imm13_36();
|
2018-08-13 17:22:09 -04:00
|
|
|
|
|
|
|
ShaderIrInst Inst = GprHandle ? ShaderIrInst.Texb : ShaderIrInst.Texs;
|
2018-06-01 23:50:56 -04:00
|
|
|
|
|
|
|
int RegInc = 0;
|
|
|
|
|
|
|
|
for (int Ch = 0; Ch < 4; Ch++)
|
|
|
|
{
|
|
|
|
if (!IsChannelUsed(ChMask, Ch))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-09-08 13:51:50 -04:00
|
|
|
ShaderIrOperGpr Dst = OpCode.Gpr0();
|
2018-06-01 23:50:56 -04:00
|
|
|
|
|
|
|
Dst.Index += RegInc++;
|
|
|
|
|
2019-01-31 07:43:24 -05:00
|
|
|
if (!Dst.IsValidRegister || Dst.IsConst)
|
2018-06-01 23:50:56 -04:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-01-31 07:43:24 -05:00
|
|
|
ShaderIrMetaTex Meta = new ShaderIrMetaTex(Ch);
|
|
|
|
|
|
|
|
ShaderIrOp Op = new ShaderIrOp(Inst, Coords[0], Coords[1], OperC, Meta);
|
|
|
|
|
|
|
|
Block.AddNode(OpCode.PredNode(new ShaderIrAsg(Dst, Op)));
|
2018-06-01 23:50:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-18 00:30:35 -04:00
|
|
|
public static void Texs(ShaderIrBlock Block, long OpCode, int Position)
|
2018-04-10 15:50:32 -04:00
|
|
|
{
|
2018-08-13 17:22:09 -04:00
|
|
|
EmitTexs(Block, OpCode, ShaderIrInst.Texs);
|
2018-04-10 15:50:32 -04:00
|
|
|
}
|
|
|
|
|
2018-09-18 00:30:35 -04:00
|
|
|
public static void Tlds(ShaderIrBlock Block, long OpCode, int Position)
|
2018-04-10 15:50:32 -04:00
|
|
|
{
|
2018-08-13 17:22:09 -04:00
|
|
|
EmitTexs(Block, OpCode, ShaderIrInst.Txlf);
|
2018-04-10 15:50:32 -04:00
|
|
|
}
|
|
|
|
|
2018-08-13 17:22:09 -04:00
|
|
|
private static void EmitTexs(ShaderIrBlock Block, long OpCode, ShaderIrInst Inst)
|
2018-04-08 15:17:35 -04:00
|
|
|
{
|
|
|
|
//TODO: Support other formats.
|
2018-07-03 19:06:13 -04:00
|
|
|
int LutIndex;
|
2018-06-01 23:50:56 -04:00
|
|
|
|
2019-01-31 07:43:24 -05:00
|
|
|
LutIndex = !OpCode.Gpr0().IsConst ? 1 : 0;
|
|
|
|
LutIndex |= !OpCode.Gpr28().IsConst ? 2 : 0;
|
2018-06-01 23:50:56 -04:00
|
|
|
|
2018-08-13 18:46:36 -04:00
|
|
|
if (LutIndex == 0)
|
|
|
|
{
|
2019-01-31 07:43:24 -05:00
|
|
|
//Both destination registers are RZ, do nothing.
|
2018-08-13 18:46:36 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-31 07:43:24 -05:00
|
|
|
bool Fp16 = !OpCode.Read(59);
|
2018-04-13 14:12:58 -04:00
|
|
|
|
2019-01-31 07:43:24 -05:00
|
|
|
int DstIncrement = 0;
|
2018-06-01 23:50:56 -04:00
|
|
|
|
2018-08-13 18:46:36 -04:00
|
|
|
ShaderIrOperGpr GetDst()
|
|
|
|
{
|
|
|
|
ShaderIrOperGpr Dst;
|
|
|
|
|
2019-01-31 07:43:24 -05:00
|
|
|
if (Fp16)
|
2018-08-13 18:46:36 -04:00
|
|
|
{
|
2019-01-31 07:43:24 -05:00
|
|
|
//FP16 mode, two components are packed on the two
|
|
|
|
//halfs of a 32-bits register, as two half-float values.
|
|
|
|
int HalfPart = DstIncrement & 1;
|
|
|
|
|
|
|
|
switch (LutIndex)
|
|
|
|
{
|
|
|
|
case 1: Dst = OpCode.GprHalf0(HalfPart); break;
|
|
|
|
case 2: Dst = OpCode.GprHalf28(HalfPart); break;
|
|
|
|
case 3: Dst = (DstIncrement >> 1) != 0
|
|
|
|
? OpCode.GprHalf28(HalfPart)
|
|
|
|
: OpCode.GprHalf0(HalfPart); break;
|
|
|
|
|
|
|
|
default: throw new InvalidOperationException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//32-bits mode, each component uses one register.
|
|
|
|
//Two components uses two consecutive registers.
|
|
|
|
switch (LutIndex)
|
|
|
|
{
|
|
|
|
case 1: Dst = OpCode.Gpr0(); break;
|
|
|
|
case 2: Dst = OpCode.Gpr28(); break;
|
|
|
|
case 3: Dst = (DstIncrement >> 1) != 0
|
|
|
|
? OpCode.Gpr28()
|
|
|
|
: OpCode.Gpr0(); break;
|
|
|
|
|
|
|
|
default: throw new InvalidOperationException();
|
|
|
|
}
|
|
|
|
|
|
|
|
Dst.Index += DstIncrement & 1;
|
2018-08-13 18:46:36 -04:00
|
|
|
}
|
|
|
|
|
2019-01-31 07:43:24 -05:00
|
|
|
DstIncrement++;
|
2018-08-13 18:46:36 -04:00
|
|
|
|
|
|
|
return Dst;
|
|
|
|
}
|
|
|
|
|
2019-01-31 07:43:24 -05:00
|
|
|
int ChMask = MaskLut[LutIndex, OpCode.Read(50, 7)];
|
|
|
|
|
|
|
|
if (ChMask == 0)
|
|
|
|
{
|
|
|
|
//All channels are disabled, do nothing.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ShaderIrNode OperC = OpCode.Imm13_36();
|
|
|
|
|
|
|
|
ShaderIrOperGpr Coord0 = ShaderIrOperGpr.MakeTemporary(0);
|
|
|
|
ShaderIrOperGpr Coord1 = ShaderIrOperGpr.MakeTemporary(1);
|
|
|
|
|
|
|
|
Block.AddNode(new ShaderIrAsg(Coord0, OpCode.Gpr8()));
|
|
|
|
Block.AddNode(new ShaderIrAsg(Coord1, OpCode.Gpr20()));
|
|
|
|
|
2018-04-13 14:12:58 -04:00
|
|
|
for (int Ch = 0; Ch < 4; Ch++)
|
|
|
|
{
|
2018-06-01 23:50:56 -04:00
|
|
|
if (!IsChannelUsed(ChMask, Ch))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-01-31 07:43:24 -05:00
|
|
|
ShaderIrMetaTex Meta = new ShaderIrMetaTex(Ch);
|
|
|
|
|
|
|
|
ShaderIrOp Op = new ShaderIrOp(Inst, Coord0, Coord1, OperC, Meta);
|
2018-04-13 14:12:58 -04:00
|
|
|
|
2018-08-13 18:46:36 -04:00
|
|
|
ShaderIrOperGpr Dst = GetDst();
|
2018-04-13 14:12:58 -04:00
|
|
|
|
2019-01-31 07:43:24 -05:00
|
|
|
if (Dst.IsValidRegister && !Dst.IsConst)
|
2018-04-30 13:32:54 -04:00
|
|
|
{
|
2019-01-31 07:43:24 -05:00
|
|
|
Block.AddNode(OpCode.PredNode(new ShaderIrAsg(Dst, Op)));
|
2018-04-30 13:32:54 -04:00
|
|
|
}
|
2018-04-13 14:12:58 -04:00
|
|
|
}
|
2018-04-08 15:17:35 -04:00
|
|
|
}
|
2018-06-01 23:50:56 -04:00
|
|
|
|
|
|
|
private static bool IsChannelUsed(int ChMask, int Ch)
|
|
|
|
{
|
|
|
|
return (ChMask & (1 << Ch)) != 0;
|
|
|
|
}
|
2018-04-08 15:17:35 -04:00
|
|
|
}
|
|
|
|
}
|