Merge pull request #2981 from lioncash/copy

gl_shader_decompiler: Minor cleanup-related changes
This commit is contained in:
Rodrigo Locatti 2019-10-15 21:07:25 -03:00 committed by GitHub
commit 14f3cebcd4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 34 deletions

View File

@ -1148,7 +1148,7 @@ private:
for (const auto& variant : extras) { for (const auto& variant : extras) {
if (const auto argument = std::get_if<TextureArgument>(&variant)) { if (const auto argument = std::get_if<TextureArgument>(&variant)) {
expr += GenerateTextureArgument(*argument); expr += GenerateTextureArgument(*argument);
} else if (std::get_if<TextureAoffi>(&variant)) { } else if (std::holds_alternative<TextureAoffi>(variant)) {
expr += GenerateTextureAoffi(meta->aoffi); expr += GenerateTextureAoffi(meta->aoffi);
} else { } else {
UNREACHABLE(); UNREACHABLE();
@ -1158,8 +1158,8 @@ private:
return expr + ')'; return expr + ')';
} }
std::string GenerateTextureArgument(TextureArgument argument) { std::string GenerateTextureArgument(const TextureArgument& argument) {
const auto [type, operand] = argument; const auto& [type, operand] = argument;
if (operand == nullptr) { if (operand == nullptr) {
return {}; return {};
} }
@ -1235,7 +1235,7 @@ private:
std::string BuildImageValues(Operation operation) { std::string BuildImageValues(Operation operation) {
constexpr std::array constructors{"uint", "uvec2", "uvec3", "uvec4"}; constexpr std::array constructors{"uint", "uvec2", "uvec3", "uvec4"};
const auto meta{std::get<MetaImage>(operation.GetMeta())}; const auto& meta{std::get<MetaImage>(operation.GetMeta())};
const std::size_t values_count{meta.values.size()}; const std::size_t values_count{meta.values.size()};
std::string expr = fmt::format("{}(", constructors.at(values_count - 1)); std::string expr = fmt::format("{}(", constructors.at(values_count - 1));
@ -1780,14 +1780,14 @@ private:
return {"0", Type::Int}; return {"0", Type::Int};
} }
const auto meta{std::get<MetaImage>(operation.GetMeta())}; const auto& meta{std::get<MetaImage>(operation.GetMeta())};
return {fmt::format("imageLoad({}, {}){}", GetImage(meta.image), return {fmt::format("imageLoad({}, {}){}", GetImage(meta.image),
BuildIntegerCoordinates(operation), GetSwizzle(meta.element)), BuildIntegerCoordinates(operation), GetSwizzle(meta.element)),
Type::Uint}; Type::Uint};
} }
Expression ImageStore(Operation operation) { Expression ImageStore(Operation operation) {
const auto meta{std::get<MetaImage>(operation.GetMeta())}; const auto& meta{std::get<MetaImage>(operation.GetMeta())};
code.AddLine("imageStore({}, {}, {});", GetImage(meta.image), code.AddLine("imageStore({}, {}, {});", GetImage(meta.image),
BuildIntegerCoordinates(operation), BuildImageValues(operation)); BuildIntegerCoordinates(operation), BuildImageValues(operation));
return {}; return {};
@ -1795,7 +1795,7 @@ private:
template <const std::string_view& opname> template <const std::string_view& opname>
Expression AtomicImage(Operation operation) { Expression AtomicImage(Operation operation) {
const auto meta{std::get<MetaImage>(operation.GetMeta())}; const auto& meta{std::get<MetaImage>(operation.GetMeta())};
ASSERT(meta.values.size() == 1); ASSERT(meta.values.size() == 1);
return {fmt::format("imageAtomic{}({}, {}, {})", opname, GetImage(meta.image), return {fmt::format("imageAtomic{}({}, {}, {})", opname, GetImage(meta.image),
@ -2246,7 +2246,7 @@ private:
code.AddLine("#ifdef SAMPLER_{}_IS_BUFFER", sampler.GetIndex()); code.AddLine("#ifdef SAMPLER_{}_IS_BUFFER", sampler.GetIndex());
} }
std::string GetDeclarationWithSuffix(u32 index, const std::string& name) const { std::string GetDeclarationWithSuffix(u32 index, std::string_view name) const {
return fmt::format("{}_{}_{}", name, index, suffix); return fmt::format("{}_{}_{}", name, index, suffix);
} }
@ -2271,17 +2271,15 @@ private:
ShaderWriter code; ShaderWriter code;
}; };
static constexpr std::string_view flow_var = "flow_var_";
std::string GetFlowVariable(u32 i) { std::string GetFlowVariable(u32 i) {
return fmt::format("{}{}", flow_var, i); return fmt::format("flow_var_{}", i);
} }
class ExprDecompiler { class ExprDecompiler {
public: public:
explicit ExprDecompiler(GLSLDecompiler& decomp) : decomp{decomp} {} explicit ExprDecompiler(GLSLDecompiler& decomp) : decomp{decomp} {}
void operator()(VideoCommon::Shader::ExprAnd& expr) { void operator()(const ExprAnd& expr) {
inner += "( "; inner += "( ";
std::visit(*this, *expr.operand1); std::visit(*this, *expr.operand1);
inner += " && "; inner += " && ";
@ -2289,7 +2287,7 @@ public:
inner += ')'; inner += ')';
} }
void operator()(VideoCommon::Shader::ExprOr& expr) { void operator()(const ExprOr& expr) {
inner += "( "; inner += "( ";
std::visit(*this, *expr.operand1); std::visit(*this, *expr.operand1);
inner += " || "; inner += " || ";
@ -2297,17 +2295,17 @@ public:
inner += ')'; inner += ')';
} }
void operator()(VideoCommon::Shader::ExprNot& expr) { void operator()(const ExprNot& expr) {
inner += '!'; inner += '!';
std::visit(*this, *expr.operand1); std::visit(*this, *expr.operand1);
} }
void operator()(VideoCommon::Shader::ExprPredicate& expr) { void operator()(const ExprPredicate& expr) {
const 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()(const ExprCondCode& expr) {
const Node cc = decomp.ir.GetConditionCode(expr.cc); const Node cc = decomp.ir.GetConditionCode(expr.cc);
std::string target; std::string target;
@ -2329,15 +2327,15 @@ public:
inner += target; inner += target;
} }
void operator()(VideoCommon::Shader::ExprVar& expr) { void operator()(const ExprVar& expr) {
inner += GetFlowVariable(expr.var_index); inner += GetFlowVariable(expr.var_index);
} }
void operator()(VideoCommon::Shader::ExprBoolean& expr) { void operator()(const ExprBoolean& expr) {
inner += expr.value ? "true" : "false"; inner += expr.value ? "true" : "false";
} }
std::string& GetResult() { const std::string& GetResult() const {
return inner; return inner;
} }
@ -2350,7 +2348,7 @@ class ASTDecompiler {
public: public:
explicit ASTDecompiler(GLSLDecompiler& decomp) : decomp{decomp} {} explicit ASTDecompiler(GLSLDecompiler& decomp) : decomp{decomp} {}
void operator()(VideoCommon::Shader::ASTProgram& ast) { void operator()(const ASTProgram& ast) {
ASTNode current = ast.nodes.GetFirst(); ASTNode current = ast.nodes.GetFirst();
while (current) { while (current) {
Visit(current); Visit(current);
@ -2358,7 +2356,7 @@ public:
} }
} }
void operator()(VideoCommon::Shader::ASTIfThen& ast) { void operator()(const ASTIfThen& ast) {
ExprDecompiler expr_parser{decomp}; ExprDecompiler expr_parser{decomp};
std::visit(expr_parser, *ast.condition); std::visit(expr_parser, *ast.condition);
decomp.code.AddLine("if ({}) {{", expr_parser.GetResult()); decomp.code.AddLine("if ({}) {{", expr_parser.GetResult());
@ -2372,7 +2370,7 @@ public:
decomp.code.AddLine("}}"); decomp.code.AddLine("}}");
} }
void operator()(VideoCommon::Shader::ASTIfElse& ast) { void operator()(const ASTIfElse& ast) {
decomp.code.AddLine("else {{"); decomp.code.AddLine("else {{");
decomp.code.scope++; decomp.code.scope++;
ASTNode current = ast.nodes.GetFirst(); ASTNode current = ast.nodes.GetFirst();
@ -2384,29 +2382,29 @@ public:
decomp.code.AddLine("}}"); decomp.code.AddLine("}}");
} }
void operator()(VideoCommon::Shader::ASTBlockEncoded& ast) { void operator()([[maybe_unused]] const ASTBlockEncoded& ast) {
UNREACHABLE(); UNREACHABLE();
} }
void operator()(VideoCommon::Shader::ASTBlockDecoded& ast) { void operator()(const ASTBlockDecoded& ast) {
decomp.VisitBlock(ast.nodes); decomp.VisitBlock(ast.nodes);
} }
void operator()(VideoCommon::Shader::ASTVarSet& ast) { void operator()(const ASTVarSet& ast) {
ExprDecompiler expr_parser{decomp}; ExprDecompiler expr_parser{decomp};
std::visit(expr_parser, *ast.condition); std::visit(expr_parser, *ast.condition);
decomp.code.AddLine("{} = {};", GetFlowVariable(ast.index), expr_parser.GetResult()); decomp.code.AddLine("{} = {};", GetFlowVariable(ast.index), expr_parser.GetResult());
} }
void operator()(VideoCommon::Shader::ASTLabel& ast) { void operator()(const ASTLabel& ast) {
decomp.code.AddLine("// Label_{}:", ast.index); decomp.code.AddLine("// Label_{}:", ast.index);
} }
void operator()(VideoCommon::Shader::ASTGoto& ast) { void operator()([[maybe_unused]] const ASTGoto& ast) {
UNREACHABLE(); UNREACHABLE();
} }
void operator()(VideoCommon::Shader::ASTDoWhile& ast) { void operator()(const ASTDoWhile& ast) {
ExprDecompiler expr_parser{decomp}; ExprDecompiler expr_parser{decomp};
std::visit(expr_parser, *ast.condition); std::visit(expr_parser, *ast.condition);
decomp.code.AddLine("do {{"); decomp.code.AddLine("do {{");
@ -2420,7 +2418,7 @@ public:
decomp.code.AddLine("}} while({});", expr_parser.GetResult()); decomp.code.AddLine("}} while({});", expr_parser.GetResult());
} }
void operator()(VideoCommon::Shader::ASTReturn& ast) { void operator()(const ASTReturn& ast) {
const 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};
@ -2440,7 +2438,7 @@ public:
} }
} }
void operator()(VideoCommon::Shader::ASTBreak& ast) { void operator()(const ASTBreak& ast) {
const 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};
@ -2455,7 +2453,7 @@ public:
} }
} }
void Visit(VideoCommon::Shader::ASTNode& node) { void Visit(const ASTNode& node) {
std::visit(*this, *node->GetInnerData()); std::visit(*this, *node->GetInnerData());
} }
@ -2468,9 +2466,9 @@ void GLSLDecompiler::DecompileAST() {
for (u32 i = 0; i < num_flow_variables; i++) { for (u32 i = 0; i < num_flow_variables; i++) {
code.AddLine("bool {} = false;", GetFlowVariable(i)); code.AddLine("bool {} = false;", GetFlowVariable(i));
} }
ASTDecompiler decompiler{*this}; ASTDecompiler decompiler{*this};
VideoCommon::Shader::ASTNode program = ir.GetASTProgram(); decompiler.Visit(ir.GetASTProgram());
decompiler.Visit(program);
} }
} // Anonymous namespace } // Anonymous namespace

View File

@ -410,7 +410,7 @@ public:
explicit OperationNode(OperationCode code) : OperationNode(code, Meta{}) {} explicit OperationNode(OperationCode code) : OperationNode(code, Meta{}) {}
explicit OperationNode(OperationCode code, Meta meta) explicit OperationNode(OperationCode code, Meta meta)
: OperationNode(code, meta, std::vector<Node>{}) {} : OperationNode(code, std::move(meta), std::vector<Node>{}) {}
explicit OperationNode(OperationCode code, std::vector<Node> operands) explicit OperationNode(OperationCode code, std::vector<Node> operands)
: OperationNode(code, Meta{}, std::move(operands)) {} : OperationNode(code, Meta{}, std::move(operands)) {}