gl_shader_decompiler: Refactor and address feedback.

This commit is contained in:
Fernando Sahmkow 2019-09-22 12:33:13 -04:00 committed by FernandoS27
parent b3c46d6948
commit 189a50bc2a

View file

@ -355,7 +355,7 @@ public:
if (!ir.IsFlowStackDisabled()) { if (!ir.IsFlowStackDisabled()) {
for (const auto stack : std::array{MetaStackClass::Ssy, MetaStackClass::Pbk}) { for (const auto stack : std::array{MetaStackClass::Ssy, MetaStackClass::Pbk}) {
code.AddLine("uint {}[{}];", FlowStackName(stack), FLOW_STACK_SIZE); code.AddLine("uint {}[{}];", FlowStackName(stack), FLOW_STACK_SIZE);
code.AddLine("uint {} = 0u;", FlowStackTopName(stack)); code.AddLine("uint {} = 0U;", FlowStackTopName(stack));
} }
} }
@ -1837,10 +1837,9 @@ private:
return {}; return {};
} }
Expression WriteExit() { void PreExit() {
if (stage != ProgramType::Fragment) { if (stage != ProgramType::Fragment) {
code.AddLine("return;"); return;
return {};
} }
const auto& used_registers = ir.GetRegisters(); const auto& used_registers = ir.GetRegisters();
const auto SafeGetRegister = [&](u32 reg) -> Expression { const auto SafeGetRegister = [&](u32 reg) -> Expression {
@ -1872,13 +1871,12 @@ private:
// already contains one past the last color register. // already contains one past the last color register.
code.AddLine("gl_FragDepth = {};", SafeGetRegister(current_reg + 1).AsFloat()); code.AddLine("gl_FragDepth = {};", SafeGetRegister(current_reg + 1).AsFloat());
} }
code.AddLine("return;");
return {};
} }
Expression Exit(Operation operation) { Expression Exit(Operation operation) {
return WriteExit(); PreExit();
code.AddLine("return;");
return {};
} }
Expression Discard(Operation operation) { Expression Discard(Operation operation) {
@ -2277,7 +2275,7 @@ const std::string flow_var = "flow_var_";
class ExprDecompiler { class ExprDecompiler {
public: public:
ExprDecompiler(GLSLDecompiler& decomp) : decomp{decomp} {} explicit ExprDecompiler(GLSLDecompiler& decomp) : decomp{decomp} {}
void operator()(VideoCommon::Shader::ExprAnd& expr) { void operator()(VideoCommon::Shader::ExprAnd& expr) {
inner += "( "; inner += "( ";
@ -2301,12 +2299,12 @@ public:
} }
void operator()(VideoCommon::Shader::ExprPredicate& expr) { void operator()(VideoCommon::Shader::ExprPredicate& expr) {
auto pred = static_cast<Tegra::Shader::Pred>(expr.predicate); const auto pred = static_cast<Tegra::Shader::Pred>(expr.predicate);
inner += decomp.GetPredicate(pred); inner += decomp.GetPredicate(pred);
} }
void operator()(VideoCommon::Shader::ExprCondCode& expr) { void operator()(VideoCommon::Shader::ExprCondCode& expr) {
Node cc = decomp.ir.GetConditionCode(expr.cc); const Node cc = decomp.ir.GetConditionCode(expr.cc);
std::string target; std::string target;
if (const auto pred = std::get_if<PredicateNode>(&*cc)) { if (const auto pred = std::get_if<PredicateNode>(&*cc)) {
@ -2321,6 +2319,8 @@ public:
} }
} else if (const auto flag = std::get_if<InternalFlagNode>(&*cc)) { } else if (const auto flag = std::get_if<InternalFlagNode>(&*cc)) {
target = decomp.GetInternalFlag(flag->GetFlag()); target = decomp.GetInternalFlag(flag->GetFlag());
} else {
UNREACHABLE();
} }
inner += target; inner += target;
} }
@ -2338,13 +2338,13 @@ public:
} }
private: private:
std::string inner{}; std::string inner;
GLSLDecompiler& decomp; GLSLDecompiler& decomp;
}; };
class ASTDecompiler { class ASTDecompiler {
public: public:
ASTDecompiler(GLSLDecompiler& decomp) : decomp{decomp} {} explicit ASTDecompiler(GLSLDecompiler& decomp) : decomp{decomp} {}
void operator()(VideoCommon::Shader::ASTProgram& ast) { void operator()(VideoCommon::Shader::ASTProgram& ast) {
ASTNode current = ast.nodes.GetFirst(); ASTNode current = ast.nodes.GetFirst();
@ -2417,7 +2417,7 @@ public:
} }
void operator()(VideoCommon::Shader::ASTReturn& ast) { void operator()(VideoCommon::Shader::ASTReturn& ast) {
bool is_true = VideoCommon::Shader::ExprIsTrue(ast.condition); const bool is_true = VideoCommon::Shader::ExprIsTrue(ast.condition);
if (!is_true) { if (!is_true) {
ExprDecompiler expr_parser{decomp}; ExprDecompiler expr_parser{decomp};
std::visit(expr_parser, *ast.condition); std::visit(expr_parser, *ast.condition);
@ -2427,7 +2427,8 @@ public:
if (ast.kills) { if (ast.kills) {
decomp.code.AddLine("discard;"); decomp.code.AddLine("discard;");
} else { } else {
decomp.WriteExit(); decomp.PreExit();
decomp.code.AddLine("return;");
} }
if (!is_true) { if (!is_true) {
decomp.code.scope--; decomp.code.scope--;
@ -2436,7 +2437,7 @@ public:
} }
void operator()(VideoCommon::Shader::ASTBreak& ast) { void operator()(VideoCommon::Shader::ASTBreak& ast) {
bool is_true = VideoCommon::Shader::ExprIsTrue(ast.condition); const bool is_true = VideoCommon::Shader::ExprIsTrue(ast.condition);
if (!is_true) { if (!is_true) {
ExprDecompiler expr_parser{decomp}; ExprDecompiler expr_parser{decomp};
std::visit(expr_parser, *ast.condition); std::visit(expr_parser, *ast.condition);
@ -2459,7 +2460,7 @@ private:
}; };
void GLSLDecompiler::DecompileAST() { void GLSLDecompiler::DecompileAST() {
u32 num_flow_variables = ir.GetASTNumVariables(); const u32 num_flow_variables = ir.GetASTNumVariables();
for (u32 i = 0; i < num_flow_variables; i++) { for (u32 i = 0; i < num_flow_variables; i++) {
code.AddLine("bool {}{} = false;", flow_var, i); code.AddLine("bool {}{} = false;", flow_var, i);
} }