diff options
| author | 2021-05-30 17:27:00 -0400 | |
|---|---|---|
| committer | 2021-07-22 21:51:37 -0400 | |
| commit | 1269a0cf8b3844c1a9bb06c843a7698b0a9643d5 (patch) | |
| tree | a0716589fa3952bdeb0f1d19b4bb455d9cdd86e5 /src/shader_recompiler/backend/glsl/emit_glsl.cpp | |
| parent | glsl: Fix ATOM and implement ATOMS (diff) | |
| download | yuzu-1269a0cf8b3844c1a9bb06c843a7698b0a9643d5.tar.gz yuzu-1269a0cf8b3844c1a9bb06c843a7698b0a9643d5.tar.xz yuzu-1269a0cf8b3844c1a9bb06c843a7698b0a9643d5.zip | |
glsl: Rework variable allocator to allow for variable reuse
Diffstat (limited to 'src/shader_recompiler/backend/glsl/emit_glsl.cpp')
| -rw-r--r-- | src/shader_recompiler/backend/glsl/emit_glsl.cpp | 29 |
1 files changed, 22 insertions, 7 deletions
diff --git a/src/shader_recompiler/backend/glsl/emit_glsl.cpp b/src/shader_recompiler/backend/glsl/emit_glsl.cpp index bac4b830d..4304ee4d5 100644 --- a/src/shader_recompiler/backend/glsl/emit_glsl.cpp +++ b/src/shader_recompiler/backend/glsl/emit_glsl.cpp | |||
| @@ -33,7 +33,7 @@ void SetDefinition(EmitContext& ctx, IR::Inst* inst, Args... args) { | |||
| 33 | template <typename ArgType> | 33 | template <typename ArgType> |
| 34 | auto Arg(EmitContext& ctx, const IR::Value& arg) { | 34 | auto Arg(EmitContext& ctx, const IR::Value& arg) { |
| 35 | if constexpr (std::is_same_v<ArgType, std::string_view>) { | 35 | if constexpr (std::is_same_v<ArgType, std::string_view>) { |
| 36 | return ctx.reg_alloc.Consume(arg); | 36 | return ctx.var_alloc.Consume(arg); |
| 37 | } else if constexpr (std::is_same_v<ArgType, const IR::Value&>) { | 37 | } else if constexpr (std::is_same_v<ArgType, const IR::Value&>) { |
| 38 | return arg; | 38 | return arg; |
| 39 | } else if constexpr (std::is_same_v<ArgType, u32>) { | 39 | } else if constexpr (std::is_same_v<ArgType, u32>) { |
| @@ -131,7 +131,7 @@ void EmitCode(EmitContext& ctx, const IR::Program& program) { | |||
| 131 | } | 131 | } |
| 132 | break; | 132 | break; |
| 133 | case IR::AbstractSyntaxNode::Type::If: | 133 | case IR::AbstractSyntaxNode::Type::If: |
| 134 | ctx.Add("if ({}){{", ctx.reg_alloc.Consume(node.data.if_node.cond)); | 134 | ctx.Add("if ({}){{", ctx.var_alloc.Consume(node.data.if_node.cond)); |
| 135 | break; | 135 | break; |
| 136 | case IR::AbstractSyntaxNode::Type::EndIf: | 136 | case IR::AbstractSyntaxNode::Type::EndIf: |
| 137 | ctx.Add("}}"); | 137 | ctx.Add("}}"); |
| @@ -142,7 +142,7 @@ void EmitCode(EmitContext& ctx, const IR::Program& program) { | |||
| 142 | ctx.Add("break;"); | 142 | ctx.Add("break;"); |
| 143 | } | 143 | } |
| 144 | } else { | 144 | } else { |
| 145 | ctx.Add("if({}){{break;}}", ctx.reg_alloc.Consume(node.data.break_node.cond)); | 145 | ctx.Add("if({}){{break;}}", ctx.var_alloc.Consume(node.data.break_node.cond)); |
| 146 | } | 146 | } |
| 147 | break; | 147 | break; |
| 148 | case IR::AbstractSyntaxNode::Type::Return: | 148 | case IR::AbstractSyntaxNode::Type::Return: |
| @@ -153,7 +153,7 @@ void EmitCode(EmitContext& ctx, const IR::Program& program) { | |||
| 153 | ctx.Add("for(;;){{"); | 153 | ctx.Add("for(;;){{"); |
| 154 | break; | 154 | break; |
| 155 | case IR::AbstractSyntaxNode::Type::Repeat: | 155 | case IR::AbstractSyntaxNode::Type::Repeat: |
| 156 | ctx.Add("if({}){{", ctx.reg_alloc.Consume(node.data.repeat.cond)); | 156 | ctx.Add("if({}){{", ctx.var_alloc.Consume(node.data.repeat.cond)); |
| 157 | ctx.Add("continue;\n}}else{{"); | 157 | ctx.Add("continue;\n}}else{{"); |
| 158 | ctx.Add("break;\n}}\n}}"); | 158 | ctx.Add("break;\n}}\n}}"); |
| 159 | break; | 159 | break; |
| @@ -171,6 +171,23 @@ std::string GlslVersionSpecifier(const EmitContext& ctx) { | |||
| 171 | } | 171 | } |
| 172 | return ""; | 172 | return ""; |
| 173 | } | 173 | } |
| 174 | |||
| 175 | void DefineVariables(const EmitContext& ctx, std::string& header) { | ||
| 176 | for (u32 i = 0; i < static_cast<u32>(GlslVarType::Void); ++i) { | ||
| 177 | const auto type{static_cast<GlslVarType>(i)}; | ||
| 178 | const auto& tracker{ctx.var_alloc.GetUseTracker(type)}; | ||
| 179 | const auto type_name{ctx.var_alloc.GetGlslType(type)}; | ||
| 180 | // Temps/return types that are never used are stored at index 0 | ||
| 181 | if (tracker.uses_temp) { | ||
| 182 | header += fmt::format("{}{}={}(0);", type_name, ctx.var_alloc.Representation(0, type), | ||
| 183 | type_name); | ||
| 184 | } | ||
| 185 | for (u32 index = 1; index <= tracker.num_used; ++index) { | ||
| 186 | header += fmt::format("{}{}={}(0);", type_name, | ||
| 187 | ctx.var_alloc.Representation(index, type), type_name); | ||
| 188 | } | ||
| 189 | } | ||
| 190 | } | ||
| 174 | } // Anonymous namespace | 191 | } // Anonymous namespace |
| 175 | 192 | ||
| 176 | std::string EmitGLSL(const Profile& profile, const RuntimeInfo& runtime_info, IR::Program& program, | 193 | std::string EmitGLSL(const Profile& profile, const RuntimeInfo& runtime_info, IR::Program& program, |
| @@ -190,9 +207,7 @@ std::string EmitGLSL(const Profile& profile, const RuntimeInfo& runtime_info, IR | |||
| 190 | if (program.stage == Stage::VertexA || program.stage == Stage::VertexB) { | 207 | if (program.stage == Stage::VertexA || program.stage == Stage::VertexB) { |
| 191 | ctx.header += "gl_Position = vec4(0.0f, 0.0f, 0.0f, 1.0f);"; | 208 | ctx.header += "gl_Position = vec4(0.0f, 0.0f, 0.0f, 1.0f);"; |
| 192 | } | 209 | } |
| 193 | for (size_t index = 0; index < ctx.reg_alloc.num_used_registers; ++index) { | 210 | DefineVariables(ctx, ctx.header); |
| 194 | ctx.header += fmt::format("{} R{};", ctx.reg_alloc.reg_types[index], index); | ||
| 195 | } | ||
| 196 | if (ctx.uses_cc_carry) { | 211 | if (ctx.uses_cc_carry) { |
| 197 | ctx.header += "uint carry;"; | 212 | ctx.header += "uint carry;"; |
| 198 | } | 213 | } |