2021-10-12 22:35:31 +02:00
|
|
|
using Ryujinx.Graphics.Shader.Decoders;
|
|
|
|
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
|
|
|
|
using Ryujinx.Graphics.Shader.Translation;
|
|
|
|
|
|
|
|
using static Ryujinx.Graphics.Shader.Instructions.InstEmitHelper;
|
|
|
|
using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Shader.Instructions
|
|
|
|
{
|
|
|
|
static partial class InstEmit
|
|
|
|
{
|
|
|
|
public static void Al2p(EmitterContext context)
|
|
|
|
{
|
|
|
|
InstAl2p op = context.GetOp<InstAl2p>();
|
|
|
|
|
|
|
|
context.Copy(GetDest(op.Dest), context.IAdd(GetSrcReg(context, op.SrcA), Const(op.Imm11)));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Ald(EmitterContext context)
|
|
|
|
{
|
|
|
|
InstAld op = context.GetOp<InstAld>();
|
|
|
|
|
|
|
|
Operand primVertex = context.Copy(GetSrcReg(context, op.SrcB));
|
|
|
|
|
|
|
|
for (int index = 0; index < (int)op.AlSize + 1; index++)
|
|
|
|
{
|
|
|
|
Register rd = new Register(op.Dest + index, RegisterType.Gpr);
|
|
|
|
|
|
|
|
if (rd.IsRZ)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (op.Phys)
|
|
|
|
{
|
|
|
|
Operand userAttrOffset = context.ISubtract(GetSrcReg(context, op.SrcA), Const(AttributeConsts.UserAttributeBase));
|
|
|
|
Operand userAttrIndex = context.ShiftRightU32(userAttrOffset, Const(2));
|
|
|
|
|
|
|
|
context.Copy(Register(rd), context.LoadAttribute(Const(AttributeConsts.UserAttributeBase), userAttrIndex, primVertex));
|
|
|
|
|
|
|
|
context.Config.SetUsedFeature(FeatureFlags.IaIndexing);
|
|
|
|
}
|
2021-10-18 23:38:04 +02:00
|
|
|
else if (op.SrcB == RegisterConsts.RegisterZeroIndex || op.P)
|
2021-10-12 22:35:31 +02:00
|
|
|
{
|
2021-11-08 17:18:46 +01:00
|
|
|
int offset = FixedFuncToUserAttribute(context.Config, op.Imm11 + index * 4, op.O);
|
2021-10-12 22:35:31 +02:00
|
|
|
|
2021-10-18 23:38:04 +02:00
|
|
|
context.FlagAttributeRead(offset);
|
|
|
|
|
2022-10-01 07:35:52 +02:00
|
|
|
if (op.O && CanLoadOutput(offset))
|
2021-10-18 23:38:04 +02:00
|
|
|
{
|
|
|
|
offset |= AttributeConsts.LoadOutputMask;
|
|
|
|
}
|
|
|
|
|
2022-11-11 17:22:49 +01:00
|
|
|
Operand src = op.P ? AttributePerPatch(offset) : CreateInputAttribute(context, offset);
|
2021-10-12 22:35:31 +02:00
|
|
|
|
|
|
|
context.Copy(Register(rd), src);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-11-08 17:18:46 +01:00
|
|
|
int offset = FixedFuncToUserAttribute(context.Config, op.Imm11 + index * 4, op.O);
|
2021-10-18 23:38:04 +02:00
|
|
|
|
|
|
|
context.FlagAttributeRead(offset);
|
2021-10-12 22:35:31 +02:00
|
|
|
|
2022-10-01 07:35:52 +02:00
|
|
|
if (op.O && CanLoadOutput(offset))
|
2021-10-18 23:38:04 +02:00
|
|
|
{
|
|
|
|
offset |= AttributeConsts.LoadOutputMask;
|
|
|
|
}
|
|
|
|
|
|
|
|
Operand src = Const(offset);
|
2021-10-12 22:35:31 +02:00
|
|
|
|
|
|
|
context.Copy(Register(rd), context.LoadAttribute(src, Const(0), primVertex));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Ast(EmitterContext context)
|
|
|
|
{
|
|
|
|
InstAst op = context.GetOp<InstAst>();
|
|
|
|
|
|
|
|
for (int index = 0; index < (int)op.AlSize + 1; index++)
|
|
|
|
{
|
|
|
|
if (op.SrcB + index > RegisterConsts.RegisterZeroIndex)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Register rd = new Register(op.SrcB + index, RegisterType.Gpr);
|
|
|
|
|
|
|
|
if (op.Phys)
|
|
|
|
{
|
|
|
|
Operand userAttrOffset = context.ISubtract(GetSrcReg(context, op.SrcA), Const(AttributeConsts.UserAttributeBase));
|
|
|
|
Operand userAttrIndex = context.ShiftRightU32(userAttrOffset, Const(2));
|
|
|
|
|
|
|
|
context.StoreAttribute(Const(AttributeConsts.UserAttributeBase), userAttrIndex, Register(rd));
|
|
|
|
|
|
|
|
context.Config.SetUsedFeature(FeatureFlags.OaIndexing);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-10-18 23:38:04 +02:00
|
|
|
// TODO: Support indirect stores using Ra.
|
|
|
|
|
|
|
|
int offset = op.Imm11 + index * 4;
|
|
|
|
|
2021-11-08 17:18:46 +01:00
|
|
|
if (!context.Config.IsUsedOutputAttribute(offset))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset = FixedFuncToUserAttribute(context.Config, offset, isOutput: true);
|
|
|
|
|
2021-10-18 23:38:04 +02:00
|
|
|
context.FlagAttributeWritten(offset);
|
2021-10-12 22:35:31 +02:00
|
|
|
|
2021-10-18 23:38:04 +02:00
|
|
|
Operand dest = op.P ? AttributePerPatch(offset) : Attribute(offset);
|
2021-10-12 22:35:31 +02:00
|
|
|
|
|
|
|
context.Copy(dest, Register(rd));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Ipa(EmitterContext context)
|
|
|
|
{
|
|
|
|
InstIpa op = context.GetOp<InstIpa>();
|
|
|
|
|
|
|
|
context.FlagAttributeRead(op.Imm10);
|
|
|
|
|
|
|
|
Operand res;
|
|
|
|
|
2021-11-08 17:18:46 +01:00
|
|
|
bool isFixedFunc = false;
|
|
|
|
|
2021-10-12 22:35:31 +02:00
|
|
|
if (op.Idx)
|
|
|
|
{
|
|
|
|
Operand userAttrOffset = context.ISubtract(GetSrcReg(context, op.SrcA), Const(AttributeConsts.UserAttributeBase));
|
|
|
|
Operand userAttrIndex = context.ShiftRightU32(userAttrOffset, Const(2));
|
|
|
|
|
|
|
|
res = context.LoadAttribute(Const(AttributeConsts.UserAttributeBase), userAttrIndex, Const(0));
|
|
|
|
res = context.FPMultiply(res, Attribute(AttributeConsts.PositionW));
|
|
|
|
|
|
|
|
context.Config.SetUsedFeature(FeatureFlags.IaIndexing);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-11-08 17:18:46 +01:00
|
|
|
isFixedFunc = TryFixedFuncToUserAttributeIpa(context, op.Imm10, out res);
|
2021-10-12 22:35:31 +02:00
|
|
|
|
|
|
|
if (op.Imm10 >= AttributeConsts.UserAttributeBase && op.Imm10 < AttributeConsts.UserAttributeEnd)
|
|
|
|
{
|
|
|
|
int index = (op.Imm10 - AttributeConsts.UserAttributeBase) >> 4;
|
|
|
|
|
|
|
|
if (context.Config.ImapTypes[index].GetFirstUsedType() == PixelImap.Perspective)
|
|
|
|
{
|
|
|
|
res = context.FPMultiply(res, Attribute(AttributeConsts.PositionW));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-08 17:18:46 +01:00
|
|
|
if (op.IpaOp == IpaOp.Multiply && !isFixedFunc)
|
2021-10-12 22:35:31 +02:00
|
|
|
{
|
|
|
|
Operand srcB = GetSrcReg(context, op.SrcB);
|
|
|
|
|
|
|
|
res = context.FPMultiply(res, srcB);
|
|
|
|
}
|
|
|
|
|
|
|
|
res = context.FPSaturate(res, op.Sat);
|
|
|
|
|
|
|
|
context.Copy(GetDest(op.Dest), res);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Isberd(EmitterContext context)
|
|
|
|
{
|
|
|
|
InstIsberd op = context.GetOp<InstIsberd>();
|
|
|
|
|
2022-04-08 12:42:39 +02:00
|
|
|
// This instruction performs a load from ISBE (Internal Stage Buffer Entry) memory.
|
|
|
|
// Here, we just propagate the offset, as the result from this instruction is usually
|
|
|
|
// used with ALD to perform vertex load on geometry or tessellation shaders.
|
|
|
|
// The offset is calculated as (PrimitiveIndex * VerticesPerPrimitive) + VertexIndex.
|
|
|
|
// Since we hardcode PrimitiveIndex to zero, then the offset will be just VertexIndex.
|
2021-10-12 22:35:31 +02:00
|
|
|
context.Copy(GetDest(op.Dest), GetSrcReg(context, op.SrcA));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void OutR(EmitterContext context)
|
|
|
|
{
|
|
|
|
InstOutR op = context.GetOp<InstOutR>();
|
|
|
|
|
|
|
|
EmitOut(context, op.OutType.HasFlag(OutType.Emit), op.OutType.HasFlag(OutType.Cut));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void OutI(EmitterContext context)
|
|
|
|
{
|
|
|
|
InstOutI op = context.GetOp<InstOutI>();
|
|
|
|
|
|
|
|
EmitOut(context, op.OutType.HasFlag(OutType.Emit), op.OutType.HasFlag(OutType.Cut));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void OutC(EmitterContext context)
|
|
|
|
{
|
|
|
|
InstOutC op = context.GetOp<InstOutC>();
|
|
|
|
|
|
|
|
EmitOut(context, op.OutType.HasFlag(OutType.Emit), op.OutType.HasFlag(OutType.Cut));
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void EmitOut(EmitterContext context, bool emit, bool cut)
|
|
|
|
{
|
|
|
|
if (!(emit || cut))
|
|
|
|
{
|
|
|
|
context.Config.GpuAccessor.Log("Invalid OUT encoding.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (emit)
|
|
|
|
{
|
2022-05-12 15:47:13 +02:00
|
|
|
if (context.Config.LastInVertexPipeline)
|
|
|
|
{
|
|
|
|
context.PrepareForVertexReturn(out var tempXLocal, out var tempYLocal, out var tempZLocal);
|
|
|
|
|
|
|
|
context.EmitVertex();
|
|
|
|
|
|
|
|
// Restore output position value before transformation.
|
|
|
|
|
|
|
|
if (tempXLocal != null)
|
|
|
|
{
|
|
|
|
context.Copy(Attribute(AttributeConsts.PositionX), tempXLocal);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tempYLocal != null)
|
|
|
|
{
|
|
|
|
context.Copy(Attribute(AttributeConsts.PositionY), tempYLocal);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tempZLocal != null)
|
|
|
|
{
|
|
|
|
context.Copy(Attribute(AttributeConsts.PositionZ), tempZLocal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
context.EmitVertex();
|
|
|
|
}
|
2021-10-12 22:35:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cut)
|
|
|
|
{
|
|
|
|
context.EndPrimitive();
|
|
|
|
}
|
|
|
|
}
|
2021-11-08 17:18:46 +01:00
|
|
|
|
2022-10-01 07:35:52 +02:00
|
|
|
private static bool CanLoadOutput(int attr)
|
|
|
|
{
|
|
|
|
return attr != AttributeConsts.TessCoordX && attr != AttributeConsts.TessCoordY;
|
|
|
|
}
|
|
|
|
|
2021-11-08 17:18:46 +01:00
|
|
|
private static bool TryFixedFuncToUserAttributeIpa(EmitterContext context, int attr, out Operand selectedAttr)
|
|
|
|
{
|
|
|
|
if (attr >= AttributeConsts.FrontColorDiffuseR && attr < AttributeConsts.BackColorDiffuseR)
|
|
|
|
{
|
|
|
|
// TODO: If two sided rendering is enabled, then this should return
|
|
|
|
// FrontColor if the fragment is front facing, and back color otherwise.
|
|
|
|
int index = (attr - AttributeConsts.FrontColorDiffuseR) >> 4;
|
|
|
|
int userAttrIndex = context.Config.GetFreeUserAttribute(isOutput: false, index);
|
|
|
|
Operand frontAttr = Attribute(AttributeConsts.UserAttributeBase + userAttrIndex * 16 + (attr & 0xf));
|
|
|
|
|
|
|
|
context.Config.SetInputUserAttributeFixedFunc(userAttrIndex);
|
|
|
|
|
|
|
|
selectedAttr = frontAttr;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (attr >= AttributeConsts.BackColorDiffuseR && attr < AttributeConsts.ClipDistance0)
|
|
|
|
{
|
|
|
|
selectedAttr = ConstF(((attr >> 2) & 3) == 3 ? 1f : 0f);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (attr >= AttributeConsts.TexCoordBase && attr < AttributeConsts.TexCoordEnd)
|
|
|
|
{
|
|
|
|
selectedAttr = Attribute(FixedFuncToUserAttribute(context.Config, attr, AttributeConsts.TexCoordBase, 4, isOutput: false));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
selectedAttr = Attribute(attr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static int FixedFuncToUserAttribute(ShaderConfig config, int attr, bool isOutput)
|
|
|
|
{
|
|
|
|
if (attr >= AttributeConsts.FrontColorDiffuseR && attr < AttributeConsts.ClipDistance0)
|
|
|
|
{
|
|
|
|
attr = FixedFuncToUserAttribute(config, attr, AttributeConsts.FrontColorDiffuseR, 0, isOutput);
|
|
|
|
}
|
|
|
|
else if (attr >= AttributeConsts.TexCoordBase && attr < AttributeConsts.TexCoordEnd)
|
|
|
|
{
|
|
|
|
attr = FixedFuncToUserAttribute(config, attr, AttributeConsts.TexCoordBase, 4, isOutput);
|
|
|
|
}
|
|
|
|
|
|
|
|
return attr;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static int FixedFuncToUserAttribute(ShaderConfig config, int attr, int baseAttr, int baseIndex, bool isOutput)
|
|
|
|
{
|
|
|
|
int index = (attr - baseAttr) >> 4;
|
|
|
|
int userAttrIndex = config.GetFreeUserAttribute(isOutput, index);
|
|
|
|
|
|
|
|
if ((uint)userAttrIndex < Constants.MaxAttributes)
|
|
|
|
{
|
|
|
|
userAttrIndex += baseIndex;
|
|
|
|
attr = AttributeConsts.UserAttributeBase + userAttrIndex * 16 + (attr & 0xf);
|
|
|
|
|
|
|
|
if (isOutput)
|
|
|
|
{
|
|
|
|
config.SetOutputUserAttributeFixedFunc(userAttrIndex);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
config.SetInputUserAttributeFixedFunc(userAttrIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return attr;
|
|
|
|
}
|
2022-11-11 17:22:49 +01:00
|
|
|
|
|
|
|
private static Operand CreateInputAttribute(EmitterContext context, int attr)
|
|
|
|
{
|
|
|
|
if (context.Config.Options.TargetApi == TargetApi.Vulkan)
|
|
|
|
{
|
|
|
|
if (attr == AttributeConsts.InstanceId)
|
|
|
|
{
|
|
|
|
return context.ISubtract(Attribute(AttributeConsts.InstanceIndex), Attribute(AttributeConsts.BaseInstance));
|
|
|
|
}
|
|
|
|
else if (attr == AttributeConsts.VertexId)
|
|
|
|
{
|
|
|
|
return Attribute(AttributeConsts.VertexIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Attribute(attr);
|
|
|
|
}
|
2021-10-12 22:35:31 +02:00
|
|
|
}
|
|
|
|
}
|