2020-11-12 18:15:34 -05:00
|
|
|
|
using Ryujinx.Graphics.Shader.Decoders;
|
|
|
|
|
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
|
|
|
|
|
using static Ryujinx.Graphics.Shader.Translation.Translator;
|
|
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Shader.Translation
|
|
|
|
|
{
|
|
|
|
|
public class TranslatorContext
|
|
|
|
|
{
|
2021-10-28 18:53:12 -04:00
|
|
|
|
private readonly DecodedProgram _program;
|
2020-11-12 18:15:34 -05:00
|
|
|
|
private ShaderConfig _config;
|
|
|
|
|
|
|
|
|
|
public ulong Address { get; }
|
|
|
|
|
|
|
|
|
|
public ShaderStage Stage => _config.Stage;
|
|
|
|
|
public int Size => _config.Size;
|
|
|
|
|
|
2021-03-19 15:07:37 -04:00
|
|
|
|
public FeatureFlags UsedFeatures => _config.UsedFeatures;
|
|
|
|
|
|
2020-11-12 18:15:34 -05:00
|
|
|
|
public HashSet<int> TextureHandlesForCache => _config.TextureHandlesForCache;
|
|
|
|
|
|
|
|
|
|
public IGpuAccessor GpuAccessor => _config.GpuAccessor;
|
|
|
|
|
|
2021-10-28 18:53:12 -04:00
|
|
|
|
internal TranslatorContext(ulong address, DecodedProgram program, ShaderConfig config)
|
2020-11-12 18:15:34 -05:00
|
|
|
|
{
|
2021-02-07 18:42:17 -05:00
|
|
|
|
Address = address;
|
2021-10-28 18:53:12 -04:00
|
|
|
|
_program = program;
|
2021-02-07 18:42:17 -05:00
|
|
|
|
_config = config;
|
2020-11-12 18:15:34 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static bool IsUserAttribute(Operand operand)
|
|
|
|
|
{
|
2021-10-18 17:38:04 -04:00
|
|
|
|
if (operand != null && operand.Type.IsAttribute())
|
|
|
|
|
{
|
|
|
|
|
int value = operand.Value & AttributeConsts.Mask;
|
|
|
|
|
return value >= AttributeConsts.UserAttributeBase && value < AttributeConsts.UserAttributeEnd;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
2020-11-12 18:15:34 -05:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-11 16:27:00 -04:00
|
|
|
|
private static FunctionCode[] Combine(FunctionCode[] a, FunctionCode[] b, int aStart)
|
2020-11-12 18:15:34 -05:00
|
|
|
|
{
|
|
|
|
|
// Here we combine two shaders.
|
|
|
|
|
// For shader A:
|
|
|
|
|
// - All user attribute stores on shader A are turned into copies to a
|
|
|
|
|
// temporary variable. It's assumed that shader B will consume them.
|
|
|
|
|
// - All return instructions are turned into branch instructions, the
|
|
|
|
|
// branch target being the start of the shader B code.
|
|
|
|
|
// For shader B:
|
|
|
|
|
// - All user attribute loads on shader B are turned into copies from a
|
|
|
|
|
// temporary variable, as long that attribute is written by shader A.
|
|
|
|
|
FunctionCode[] output = new FunctionCode[a.Length + b.Length - 1];
|
|
|
|
|
|
|
|
|
|
List<Operation> ops = new List<Operation>(a.Length + b.Length);
|
|
|
|
|
|
|
|
|
|
Operand[] temps = new Operand[AttributeConsts.UserAttributesCount * 4];
|
|
|
|
|
|
|
|
|
|
Operand lblB = Label();
|
|
|
|
|
|
2021-08-11 16:27:00 -04:00
|
|
|
|
for (int index = aStart; index < a[0].Code.Length; index++)
|
2020-11-12 18:15:34 -05:00
|
|
|
|
{
|
|
|
|
|
Operation operation = a[0].Code[index];
|
|
|
|
|
|
|
|
|
|
if (IsUserAttribute(operation.Dest))
|
|
|
|
|
{
|
|
|
|
|
int tIndex = (operation.Dest.Value - AttributeConsts.UserAttributeBase) / 4;
|
|
|
|
|
|
|
|
|
|
Operand temp = temps[tIndex];
|
|
|
|
|
|
|
|
|
|
if (temp == null)
|
|
|
|
|
{
|
|
|
|
|
temp = Local();
|
|
|
|
|
|
|
|
|
|
temps[tIndex] = temp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
operation.Dest = temp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (operation.Inst == Instruction.Return)
|
|
|
|
|
{
|
|
|
|
|
ops.Add(new Operation(Instruction.Branch, lblB));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ops.Add(operation);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ops.Add(new Operation(Instruction.MarkLabel, lblB));
|
|
|
|
|
|
|
|
|
|
for (int index = 0; index < b[0].Code.Length; index++)
|
|
|
|
|
{
|
|
|
|
|
Operation operation = b[0].Code[index];
|
|
|
|
|
|
|
|
|
|
for (int srcIndex = 0; srcIndex < operation.SourcesCount; srcIndex++)
|
|
|
|
|
{
|
|
|
|
|
Operand src = operation.GetSource(srcIndex);
|
|
|
|
|
|
|
|
|
|
if (IsUserAttribute(src))
|
|
|
|
|
{
|
|
|
|
|
Operand temp = temps[(src.Value - AttributeConsts.UserAttributeBase) / 4];
|
|
|
|
|
|
|
|
|
|
if (temp != null)
|
|
|
|
|
{
|
2021-08-26 19:44:47 -04:00
|
|
|
|
operation.SetSource(srcIndex, temp);
|
2020-11-12 18:15:34 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ops.Add(operation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
output[0] = new FunctionCode(ops.ToArray());
|
|
|
|
|
|
|
|
|
|
for (int i = 1; i < a.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
output[i] = a[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 1; i < b.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
output[a.Length + i - 1] = b[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-11 16:27:00 -04:00
|
|
|
|
public ShaderProgram Translate(
|
|
|
|
|
out ShaderProgramInfo shaderProgramInfo,
|
|
|
|
|
TranslatorContext nextStage = null,
|
|
|
|
|
TranslatorContext other = null)
|
2020-11-12 18:15:34 -05:00
|
|
|
|
{
|
2021-08-11 16:27:00 -04:00
|
|
|
|
if (nextStage != null)
|
|
|
|
|
{
|
2021-11-08 11:18:46 -05:00
|
|
|
|
_config.MergeFromtNextStage(nextStage._config);
|
2021-08-11 16:27:00 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-28 18:53:12 -04:00
|
|
|
|
FunctionCode[] code = EmitShader(_program, _config, initializeOutputs: other == null, out _);
|
2020-11-12 18:15:34 -05:00
|
|
|
|
|
2021-02-07 18:42:17 -05:00
|
|
|
|
if (other != null)
|
2020-11-12 18:15:34 -05:00
|
|
|
|
{
|
2021-10-18 17:38:04 -04:00
|
|
|
|
other._config.MergeOutputUserAttributes(_config.UsedOutputAttributes, 0);
|
2021-08-11 16:27:00 -04:00
|
|
|
|
|
2021-10-28 18:53:12 -04:00
|
|
|
|
FunctionCode[] otherCode = EmitShader(other._program, other._config, initializeOutputs: true, out int aStart);
|
2021-08-11 16:27:00 -04:00
|
|
|
|
|
|
|
|
|
code = Combine(otherCode, code, aStart);
|
2021-05-20 15:43:23 -04:00
|
|
|
|
|
|
|
|
|
_config.InheritFrom(other._config);
|
2020-11-12 18:15:34 -05:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-07 18:42:17 -05:00
|
|
|
|
return Translator.Translate(code, _config, out shaderProgramInfo);
|
2020-11-12 18:15:34 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|