2023-02-25 10:39:51 +00:00
|
|
|
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
|
|
|
|
using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Shader.Translation
|
|
|
|
{
|
|
|
|
static class ShaderIdentifier
|
|
|
|
{
|
|
|
|
public static ShaderIdentification Identify(Function[] functions, ShaderConfig config)
|
|
|
|
{
|
|
|
|
if (config.Stage == ShaderStage.Geometry &&
|
|
|
|
config.GpuAccessor.QueryPrimitiveTopology() == InputTopology.Triangles &&
|
|
|
|
!config.GpuAccessor.QueryHostSupportsGeometryShader() &&
|
|
|
|
IsLayerPassthroughGeometryShader(functions, out int layerInputAttr))
|
|
|
|
{
|
|
|
|
config.SetGeometryShaderLayerInputAttribute(layerInputAttr);
|
|
|
|
|
|
|
|
return ShaderIdentification.GeometryLayerPassthrough;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ShaderIdentification.None;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static bool IsLayerPassthroughGeometryShader(Function[] functions, out int layerInputAttr)
|
|
|
|
{
|
|
|
|
bool writesLayer = false;
|
|
|
|
layerInputAttr = 0;
|
|
|
|
|
|
|
|
if (functions.Length != 1)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int verticesCount = 0;
|
|
|
|
int totalVerticesCount = 0;
|
|
|
|
|
|
|
|
foreach (BasicBlock block in functions[0].Blocks)
|
|
|
|
{
|
|
|
|
// We are not expecting loops or any complex control flow here, so fail in those cases.
|
|
|
|
if (block.Branch != null && block.Branch.Index <= block.Index)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (INode node in block.Operations)
|
|
|
|
{
|
|
|
|
if (!(node is Operation operation))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsResourceWrite(operation.Inst))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-04-25 23:51:07 +01:00
|
|
|
if (operation.Inst == Instruction.Store && operation.StorageKind == StorageKind.Output)
|
2023-02-25 10:39:51 +00:00
|
|
|
{
|
2023-04-25 23:51:07 +01:00
|
|
|
Operand src = operation.GetSource(operation.SourcesCount - 1);
|
|
|
|
Operation srcAttributeAsgOp = null;
|
2023-02-25 10:39:51 +00:00
|
|
|
|
2023-04-25 23:51:07 +01:00
|
|
|
if (src.Type == OperandType.LocalVariable &&
|
|
|
|
src.AsgOp is Operation asgOp &&
|
|
|
|
asgOp.Inst == Instruction.Load &&
|
|
|
|
asgOp.StorageKind.IsInputOrOutput())
|
2023-02-25 10:39:51 +00:00
|
|
|
{
|
2023-04-25 23:51:07 +01:00
|
|
|
if (asgOp.StorageKind != StorageKind.Input)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
srcAttributeAsgOp = asgOp;
|
2023-02-25 10:39:51 +00:00
|
|
|
}
|
|
|
|
|
2023-04-25 23:51:07 +01:00
|
|
|
if (srcAttributeAsgOp != null)
|
2023-02-25 10:39:51 +00:00
|
|
|
{
|
2023-04-25 23:51:07 +01:00
|
|
|
IoVariable dstAttribute = (IoVariable)operation.GetSource(0).Value;
|
|
|
|
IoVariable srcAttribute = (IoVariable)srcAttributeAsgOp.GetSource(0).Value;
|
|
|
|
|
|
|
|
if (dstAttribute == IoVariable.Layer && srcAttribute == IoVariable.UserDefined)
|
2023-02-25 10:39:51 +00:00
|
|
|
{
|
2023-04-25 23:51:07 +01:00
|
|
|
if (srcAttributeAsgOp.SourcesCount != 4)
|
2023-02-25 10:39:51 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
writesLayer = true;
|
2023-04-25 23:51:07 +01:00
|
|
|
layerInputAttr = srcAttributeAsgOp.GetSource(1).Value * 4 + srcAttributeAsgOp.GetSource(3).Value;;
|
2023-02-25 10:39:51 +00:00
|
|
|
}
|
2023-04-25 23:51:07 +01:00
|
|
|
else
|
2023-02-25 10:39:51 +00:00
|
|
|
{
|
2023-04-25 23:51:07 +01:00
|
|
|
if (dstAttribute != srcAttribute)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int inputsCount = operation.SourcesCount - 2;
|
|
|
|
|
|
|
|
if (dstAttribute == IoVariable.UserDefined)
|
|
|
|
{
|
|
|
|
if (operation.GetSource(1).Value != srcAttributeAsgOp.GetSource(1).Value)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
inputsCount--;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < inputsCount; i++)
|
|
|
|
{
|
|
|
|
int dstIndex = operation.SourcesCount - 2 - i;
|
|
|
|
int srcIndex = srcAttributeAsgOp.SourcesCount - 1 - i;
|
|
|
|
|
|
|
|
if ((dstIndex | srcIndex) < 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (operation.GetSource(dstIndex).Type != OperandType.Constant ||
|
|
|
|
srcAttributeAsgOp.GetSource(srcIndex).Type != OperandType.Constant ||
|
|
|
|
operation.GetSource(dstIndex).Value != srcAttributeAsgOp.GetSource(srcIndex).Value)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2023-02-25 10:39:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (src.Type == OperandType.Constant)
|
|
|
|
{
|
2023-04-25 23:51:07 +01:00
|
|
|
int dstComponent = operation.GetSource(operation.SourcesCount - 2).Value;
|
2023-02-25 10:39:51 +00:00
|
|
|
float expectedValue = dstComponent == 3 ? 1f : 0f;
|
|
|
|
|
|
|
|
if (src.AsFloat() != expectedValue)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (operation.Inst == Instruction.EmitVertex)
|
|
|
|
{
|
|
|
|
verticesCount++;
|
|
|
|
}
|
|
|
|
else if (operation.Inst == Instruction.EndPrimitive)
|
|
|
|
{
|
|
|
|
totalVerticesCount += verticesCount;
|
|
|
|
verticesCount = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return totalVerticesCount + verticesCount == 3 && writesLayer;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static bool IsResourceWrite(Instruction inst)
|
|
|
|
{
|
|
|
|
switch (inst)
|
|
|
|
{
|
|
|
|
case Instruction.AtomicAdd:
|
|
|
|
case Instruction.AtomicAnd:
|
|
|
|
case Instruction.AtomicCompareAndSwap:
|
|
|
|
case Instruction.AtomicMaxS32:
|
|
|
|
case Instruction.AtomicMaxU32:
|
|
|
|
case Instruction.AtomicMinS32:
|
|
|
|
case Instruction.AtomicMinU32:
|
|
|
|
case Instruction.AtomicOr:
|
|
|
|
case Instruction.AtomicSwap:
|
|
|
|
case Instruction.AtomicXor:
|
|
|
|
case Instruction.ImageAtomic:
|
|
|
|
case Instruction.ImageStore:
|
|
|
|
case Instruction.StoreGlobal:
|
|
|
|
case Instruction.StoreGlobal16:
|
|
|
|
case Instruction.StoreGlobal8:
|
|
|
|
case Instruction.StoreStorage:
|
|
|
|
case Instruction.StoreStorage16:
|
|
|
|
case Instruction.StoreStorage8:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|