ryujinx/Ryujinx.Graphics.Shader/Translation/Optimizations/ConstantFolding.cs

342 lines
11 KiB
C#
Raw Normal View History

New shader translator implementation (#654) * Start implementing a new shader translator * Fix shift instructions and a typo * Small refactoring on StructuredProgram, move RemovePhis method to a separate class * Initial geometry shader support * Implement TLD4 * Fix -- There's no negation on FMUL32I * Add constant folding and algebraic simplification optimizations, nits * Some leftovers from constant folding * Avoid cast for constant assignments * Add a branch elimination pass, and misc small fixes * Remove redundant branches, add expression propagation and other improvements on the code * Small leftovers -- add missing break and continue, remove unused properties, other improvements * Add null check to handle empty block cases on block visitor * Add HADD2 and HMUL2 half float shader instructions * Optimize pack/unpack sequences, some fixes related to half float instructions * Add TXQ, TLD, TLDS and TLD4S shader texture instructions, and some support for bindless textures, some refactoring on codegen * Fix copy paste mistake that caused RZ to be ignored on the AST instruction * Add workaround for conditional exit, and fix half float instruction with constant buffer * Add missing 0.0 source for TLDS.LZ variants * Simplify the switch for TLDS.LZ * Texture instructions related fixes * Implement the HFMA instruction, and some misc. fixes * Enable constant folding on UnpackHalf2x16 instructions * Refactor HFMA to use OpCode* for opcode decoding rather than on the helper methods * Remove the old shader translator * Remove ShaderDeclInfo and other unused things * Add dual vertex shader support * Add ShaderConfig, used to pass shader type and maximum cbuffer size * Move and rename some instruction enums * Move texture instructions into a separate file * Move operand GetExpression and locals management to OperandManager * Optimize opcode decoding using a simple list and binary search * Add missing condition for do-while on goto elimination * Misc. fixes on texture instructions * Simplify TLDS switch * Address PR feedback, and a nit
2019-04-17 23:57:08 +00:00
using Ryujinx.Graphics.Shader.Decoders;
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
using System;
using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
namespace Ryujinx.Graphics.Shader.Translation.Optimizations
{
static class ConstantFolding
{
2019-10-13 06:02:07 +00:00
public static void RunPass(Operation operation)
New shader translator implementation (#654) * Start implementing a new shader translator * Fix shift instructions and a typo * Small refactoring on StructuredProgram, move RemovePhis method to a separate class * Initial geometry shader support * Implement TLD4 * Fix -- There's no negation on FMUL32I * Add constant folding and algebraic simplification optimizations, nits * Some leftovers from constant folding * Avoid cast for constant assignments * Add a branch elimination pass, and misc small fixes * Remove redundant branches, add expression propagation and other improvements on the code * Small leftovers -- add missing break and continue, remove unused properties, other improvements * Add null check to handle empty block cases on block visitor * Add HADD2 and HMUL2 half float shader instructions * Optimize pack/unpack sequences, some fixes related to half float instructions * Add TXQ, TLD, TLDS and TLD4S shader texture instructions, and some support for bindless textures, some refactoring on codegen * Fix copy paste mistake that caused RZ to be ignored on the AST instruction * Add workaround for conditional exit, and fix half float instruction with constant buffer * Add missing 0.0 source for TLDS.LZ variants * Simplify the switch for TLDS.LZ * Texture instructions related fixes * Implement the HFMA instruction, and some misc. fixes * Enable constant folding on UnpackHalf2x16 instructions * Refactor HFMA to use OpCode* for opcode decoding rather than on the helper methods * Remove the old shader translator * Remove ShaderDeclInfo and other unused things * Add dual vertex shader support * Add ShaderConfig, used to pass shader type and maximum cbuffer size * Move and rename some instruction enums * Move texture instructions into a separate file * Move operand GetExpression and locals management to OperandManager * Optimize opcode decoding using a simple list and binary search * Add missing condition for do-while on goto elimination * Misc. fixes on texture instructions * Simplify TLDS switch * Address PR feedback, and a nit
2019-04-17 23:57:08 +00:00
{
if (!AreAllSourcesConstant(operation))
{
return;
}
switch (operation.Inst)
{
case Instruction.Add:
EvaluateBinary(operation, (x, y) => x + y);
break;
case Instruction.BitCount:
EvaluateUnary(operation, (x) => BitCount(x));
break;
New shader translator implementation (#654) * Start implementing a new shader translator * Fix shift instructions and a typo * Small refactoring on StructuredProgram, move RemovePhis method to a separate class * Initial geometry shader support * Implement TLD4 * Fix -- There's no negation on FMUL32I * Add constant folding and algebraic simplification optimizations, nits * Some leftovers from constant folding * Avoid cast for constant assignments * Add a branch elimination pass, and misc small fixes * Remove redundant branches, add expression propagation and other improvements on the code * Small leftovers -- add missing break and continue, remove unused properties, other improvements * Add null check to handle empty block cases on block visitor * Add HADD2 and HMUL2 half float shader instructions * Optimize pack/unpack sequences, some fixes related to half float instructions * Add TXQ, TLD, TLDS and TLD4S shader texture instructions, and some support for bindless textures, some refactoring on codegen * Fix copy paste mistake that caused RZ to be ignored on the AST instruction * Add workaround for conditional exit, and fix half float instruction with constant buffer * Add missing 0.0 source for TLDS.LZ variants * Simplify the switch for TLDS.LZ * Texture instructions related fixes * Implement the HFMA instruction, and some misc. fixes * Enable constant folding on UnpackHalf2x16 instructions * Refactor HFMA to use OpCode* for opcode decoding rather than on the helper methods * Remove the old shader translator * Remove ShaderDeclInfo and other unused things * Add dual vertex shader support * Add ShaderConfig, used to pass shader type and maximum cbuffer size * Move and rename some instruction enums * Move texture instructions into a separate file * Move operand GetExpression and locals management to OperandManager * Optimize opcode decoding using a simple list and binary search * Add missing condition for do-while on goto elimination * Misc. fixes on texture instructions * Simplify TLDS switch * Address PR feedback, and a nit
2019-04-17 23:57:08 +00:00
case Instruction.BitwiseAnd:
EvaluateBinary(operation, (x, y) => x & y);
break;
case Instruction.BitwiseExclusiveOr:
EvaluateBinary(operation, (x, y) => x ^ y);
break;
case Instruction.BitwiseNot:
EvaluateUnary(operation, (x) => ~x);
break;
case Instruction.BitwiseOr:
EvaluateBinary(operation, (x, y) => x | y);
break;
case Instruction.BitfieldExtractS32:
BitfieldExtractS32(operation);
break;
case Instruction.BitfieldExtractU32:
BitfieldExtractU32(operation);
break;
case Instruction.Clamp:
EvaluateTernary(operation, (x, y, z) => Math.Clamp(x, y, z));
break;
case Instruction.ClampU32:
EvaluateTernary(operation, (x, y, z) => (int)Math.Clamp((uint)x, (uint)y, (uint)z));
break;
case Instruction.CompareEqual:
EvaluateBinary(operation, (x, y) => x == y);
break;
case Instruction.CompareGreater:
EvaluateBinary(operation, (x, y) => x > y);
break;
case Instruction.CompareGreaterOrEqual:
EvaluateBinary(operation, (x, y) => x >= y);
break;
case Instruction.CompareGreaterOrEqualU32:
EvaluateBinary(operation, (x, y) => (uint)x >= (uint)y);
break;
case Instruction.CompareGreaterU32:
EvaluateBinary(operation, (x, y) => (uint)x > (uint)y);
break;
case Instruction.CompareLess:
EvaluateBinary(operation, (x, y) => x < y);
break;
case Instruction.CompareLessOrEqual:
EvaluateBinary(operation, (x, y) => x <= y);
break;
case Instruction.CompareLessOrEqualU32:
EvaluateBinary(operation, (x, y) => (uint)x <= (uint)y);
break;
case Instruction.CompareLessU32:
EvaluateBinary(operation, (x, y) => (uint)x < (uint)y);
break;
case Instruction.CompareNotEqual:
EvaluateBinary(operation, (x, y) => x != y);
break;
case Instruction.Divide:
EvaluateBinary(operation, (x, y) => y != 0 ? x / y : 0);
break;
case Instruction.FP | Instruction.Add:
EvaluateFPBinary(operation, (x, y) => x + y);
break;
case Instruction.FP | Instruction.Clamp:
EvaluateFPTernary(operation, (x, y, z) => Math.Clamp(x, y, z));
break;
case Instruction.FP | Instruction.CompareEqual:
EvaluateFPBinary(operation, (x, y) => x == y);
break;
case Instruction.FP | Instruction.CompareGreater:
EvaluateFPBinary(operation, (x, y) => x > y);
break;
case Instruction.FP | Instruction.CompareGreaterOrEqual:
EvaluateFPBinary(operation, (x, y) => x >= y);
break;
case Instruction.FP | Instruction.CompareLess:
EvaluateFPBinary(operation, (x, y) => x < y);
break;
case Instruction.FP | Instruction.CompareLessOrEqual:
EvaluateFPBinary(operation, (x, y) => x <= y);
break;
case Instruction.FP | Instruction.CompareNotEqual:
EvaluateFPBinary(operation, (x, y) => x != y);
break;
case Instruction.FP | Instruction.Divide:
EvaluateFPBinary(operation, (x, y) => x / y);
break;
case Instruction.FP | Instruction.Multiply:
EvaluateFPBinary(operation, (x, y) => x * y);
break;
case Instruction.FP | Instruction.Negate:
EvaluateFPUnary(operation, (x) => -x);
break;
case Instruction.FP | Instruction.Subtract:
EvaluateFPBinary(operation, (x, y) => x - y);
break;
case Instruction.IsNan:
EvaluateFPUnary(operation, (x) => float.IsNaN(x));
break;
case Instruction.Maximum:
EvaluateBinary(operation, (x, y) => Math.Max(x, y));
break;
case Instruction.MaximumU32:
EvaluateBinary(operation, (x, y) => (int)Math.Max((uint)x, (uint)y));
break;
case Instruction.Minimum:
EvaluateBinary(operation, (x, y) => Math.Min(x, y));
break;
case Instruction.MinimumU32:
EvaluateBinary(operation, (x, y) => (int)Math.Min((uint)x, (uint)y));
break;
case Instruction.Multiply:
EvaluateBinary(operation, (x, y) => x * y);
break;
case Instruction.Negate:
EvaluateUnary(operation, (x) => -x);
break;
case Instruction.ShiftLeft:
EvaluateBinary(operation, (x, y) => x << y);
break;
case Instruction.ShiftRightS32:
EvaluateBinary(operation, (x, y) => x >> y);
break;
case Instruction.ShiftRightU32:
EvaluateBinary(operation, (x, y) => (int)((uint)x >> y));
break;
case Instruction.Subtract:
EvaluateBinary(operation, (x, y) => x - y);
break;
case Instruction.UnpackHalf2x16:
UnpackHalf2x16(operation);
break;
}
}
private static bool AreAllSourcesConstant(Operation operation)
{
for (int index = 0; index < operation.SourcesCount; index++)
{
if (operation.GetSource(index).Type != OperandType.Constant)
{
return false;
}
}
return true;
}
private static int BitCount(int value)
{
int count = 0;
for (int bit = 0; bit < 32; bit++)
{
if (value.Extract(bit))
{
count++;
}
}
return count;
}
New shader translator implementation (#654) * Start implementing a new shader translator * Fix shift instructions and a typo * Small refactoring on StructuredProgram, move RemovePhis method to a separate class * Initial geometry shader support * Implement TLD4 * Fix -- There's no negation on FMUL32I * Add constant folding and algebraic simplification optimizations, nits * Some leftovers from constant folding * Avoid cast for constant assignments * Add a branch elimination pass, and misc small fixes * Remove redundant branches, add expression propagation and other improvements on the code * Small leftovers -- add missing break and continue, remove unused properties, other improvements * Add null check to handle empty block cases on block visitor * Add HADD2 and HMUL2 half float shader instructions * Optimize pack/unpack sequences, some fixes related to half float instructions * Add TXQ, TLD, TLDS and TLD4S shader texture instructions, and some support for bindless textures, some refactoring on codegen * Fix copy paste mistake that caused RZ to be ignored on the AST instruction * Add workaround for conditional exit, and fix half float instruction with constant buffer * Add missing 0.0 source for TLDS.LZ variants * Simplify the switch for TLDS.LZ * Texture instructions related fixes * Implement the HFMA instruction, and some misc. fixes * Enable constant folding on UnpackHalf2x16 instructions * Refactor HFMA to use OpCode* for opcode decoding rather than on the helper methods * Remove the old shader translator * Remove ShaderDeclInfo and other unused things * Add dual vertex shader support * Add ShaderConfig, used to pass shader type and maximum cbuffer size * Move and rename some instruction enums * Move texture instructions into a separate file * Move operand GetExpression and locals management to OperandManager * Optimize opcode decoding using a simple list and binary search * Add missing condition for do-while on goto elimination * Misc. fixes on texture instructions * Simplify TLDS switch * Address PR feedback, and a nit
2019-04-17 23:57:08 +00:00
private static void BitfieldExtractS32(Operation operation)
{
int value = GetBitfieldExtractValue(operation);
int shift = 32 - operation.GetSource(2).Value;
value = (value << shift) >> shift;
operation.TurnIntoCopy(Const(value));
}
private static void BitfieldExtractU32(Operation operation)
{
operation.TurnIntoCopy(Const(GetBitfieldExtractValue(operation)));
}
private static int GetBitfieldExtractValue(Operation operation)
{
int value = operation.GetSource(0).Value;
int lsb = operation.GetSource(1).Value;
int length = operation.GetSource(2).Value;
return value.Extract(lsb, length);
}
private static void UnpackHalf2x16(Operation operation)
{
int value = operation.GetSource(0).Value;
value = (value >> operation.ComponentIndex * 16) & 0xffff;
operation.TurnIntoCopy(ConstF(HalfConversion.HalfToSingle(value)));
}
private static void FPNegate(Operation operation)
{
float value = operation.GetSource(0).AsFloat();
operation.TurnIntoCopy(ConstF(-value));
}
private static void EvaluateUnary(Operation operation, Func<int, int> op)
{
int x = operation.GetSource(0).Value;
operation.TurnIntoCopy(Const(op(x)));
}
private static void EvaluateFPUnary(Operation operation, Func<float, float> op)
{
float x = operation.GetSource(0).AsFloat();
operation.TurnIntoCopy(ConstF(op(x)));
}
private static void EvaluateFPUnary(Operation operation, Func<float, bool> op)
{
float x = operation.GetSource(0).AsFloat();
operation.TurnIntoCopy(Const(op(x) ? IrConsts.True : IrConsts.False));
}
private static void EvaluateBinary(Operation operation, Func<int, int, int> op)
{
int x = operation.GetSource(0).Value;
int y = operation.GetSource(1).Value;
operation.TurnIntoCopy(Const(op(x, y)));
}
private static void EvaluateBinary(Operation operation, Func<int, int, bool> op)
{
int x = operation.GetSource(0).Value;
int y = operation.GetSource(1).Value;
operation.TurnIntoCopy(Const(op(x, y) ? IrConsts.True : IrConsts.False));
}
private static void EvaluateFPBinary(Operation operation, Func<float, float, float> op)
{
float x = operation.GetSource(0).AsFloat();
float y = operation.GetSource(1).AsFloat();
operation.TurnIntoCopy(ConstF(op(x, y)));
}
private static void EvaluateFPBinary(Operation operation, Func<float, float, bool> op)
{
float x = operation.GetSource(0).AsFloat();
float y = operation.GetSource(1).AsFloat();
operation.TurnIntoCopy(Const(op(x, y) ? IrConsts.True : IrConsts.False));
}
private static void EvaluateTernary(Operation operation, Func<int, int, int, int> op)
{
int x = operation.GetSource(0).Value;
int y = operation.GetSource(1).Value;
int z = operation.GetSource(2).Value;
operation.TurnIntoCopy(Const(op(x, y, z)));
}
private static void EvaluateFPTernary(Operation operation, Func<float, float, float, float> op)
{
float x = operation.GetSource(0).AsFloat();
float y = operation.GetSource(1).AsFloat();
float z = operation.GetSource(2).AsFloat();
operation.TurnIntoCopy(ConstF(op(x, y, z)));
}
}
}