summaryrefslogtreecommitdiff
path: root/src/shader_recompiler/backend/glsl
diff options
context:
space:
mode:
authorGravatar ameerj2021-06-22 23:09:22 -0400
committerGravatar ameerj2021-07-22 21:51:39 -0400
commit27ca8a0e13deeebb4185ec22619d2b78b5ad8b21 (patch)
tree627d3599a95edd45ff409fe5751fd059cdb0db9b /src/shader_recompiler/backend/glsl
parentshader: Remove IAbs64 (diff)
downloadyuzu-27ca8a0e13deeebb4185ec22619d2b78b5ad8b21.tar.gz
yuzu-27ca8a0e13deeebb4185ec22619d2b78b5ad8b21.tar.xz
yuzu-27ca8a0e13deeebb4185ec22619d2b78b5ad8b21.zip
glsl: Better IAdd Overflow CC fix
This ensures the original operand values are not overwritten when being used in the overflow detection.
Diffstat (limited to 'src/shader_recompiler/backend/glsl')
-rw-r--r--src/shader_recompiler/backend/glsl/emit_glsl.cpp2
-rw-r--r--src/shader_recompiler/backend/glsl/emit_glsl_integer.cpp22
2 files changed, 13 insertions, 11 deletions
diff --git a/src/shader_recompiler/backend/glsl/emit_glsl.cpp b/src/shader_recompiler/backend/glsl/emit_glsl.cpp
index 5867a04ab..32c4f1da2 100644
--- a/src/shader_recompiler/backend/glsl/emit_glsl.cpp
+++ b/src/shader_recompiler/backend/glsl/emit_glsl.cpp
@@ -227,7 +227,7 @@ std::string EmitGLSL(const Profile& profile, const RuntimeInfo& runtime_info, IR
227 ctx.header += "void main(){\n"; 227 ctx.header += "void main(){\n";
228 DefineVariables(ctx, ctx.header); 228 DefineVariables(ctx, ctx.header);
229 if (ctx.uses_cc_carry) { 229 if (ctx.uses_cc_carry) {
230 ctx.header += "uint carry;uint iadd_op_b;"; 230 ctx.header += "uint carry;";
231 } 231 }
232 if (program.info.uses_subgroup_shuffles) { 232 if (program.info.uses_subgroup_shuffles) {
233 ctx.header += "bool shfl_in_bounds;"; 233 ctx.header += "bool shfl_in_bounds;";
diff --git a/src/shader_recompiler/backend/glsl/emit_glsl_integer.cpp b/src/shader_recompiler/backend/glsl/emit_glsl_integer.cpp
index 40f453593..2892074e1 100644
--- a/src/shader_recompiler/backend/glsl/emit_glsl_integer.cpp
+++ b/src/shader_recompiler/backend/glsl/emit_glsl_integer.cpp
@@ -30,10 +30,21 @@ void SetSignFlag(EmitContext& ctx, IR::Inst& inst, std::string_view result) {
30} // Anonymous namespace 30} // Anonymous namespace
31 31
32void EmitIAdd32(EmitContext& ctx, IR::Inst& inst, std::string_view a, std::string_view b) { 32void EmitIAdd32(EmitContext& ctx, IR::Inst& inst, std::string_view a, std::string_view b) {
33 // Compute the overflow CC first as it requires the original operand values,
34 // which may be overwritten by the result of the addition
35 if (IR::Inst * overflow{inst.GetAssociatedPseudoOperation(IR::Opcode::GetOverflowFromOp)}) {
36 // https://stackoverflow.com/questions/55468823/how-to-detect-integer-overflow-in-c
37 constexpr u32 s32_max{static_cast<u32>(std::numeric_limits<s32>::max())};
38 const auto sub_a{fmt::format("{}u-{}", s32_max, a)};
39 const auto positive_result{fmt::format("int({})>int({})", b, sub_a)};
40 const auto negative_result{fmt::format("int({})<int({})", b, sub_a)};
41 ctx.AddU1("{}=int({})>=0?{}:{};", *overflow, a, positive_result, negative_result);
42 overflow->Invalidate();
43 }
33 const auto result{ctx.var_alloc.Define(inst, GlslVarType::U32)}; 44 const auto result{ctx.var_alloc.Define(inst, GlslVarType::U32)};
34 if (IR::Inst* const carry{inst.GetAssociatedPseudoOperation(IR::Opcode::GetCarryFromOp)}) { 45 if (IR::Inst* const carry{inst.GetAssociatedPseudoOperation(IR::Opcode::GetCarryFromOp)}) {
35 ctx.uses_cc_carry = true; 46 ctx.uses_cc_carry = true;
36 ctx.Add("iadd_op_b={};{}=uaddCarry({},{},carry);", b, result, a, b); 47 ctx.Add("{}=uaddCarry({},{},carry);", result, a, b);
37 ctx.AddU1("{}=carry!=0;", *carry); 48 ctx.AddU1("{}=carry!=0;", *carry);
38 carry->Invalidate(); 49 carry->Invalidate();
39 } else { 50 } else {
@@ -41,15 +52,6 @@ void EmitIAdd32(EmitContext& ctx, IR::Inst& inst, std::string_view a, std::strin
41 } 52 }
42 SetZeroFlag(ctx, inst, result); 53 SetZeroFlag(ctx, inst, result);
43 SetSignFlag(ctx, inst, result); 54 SetSignFlag(ctx, inst, result);
44 if (IR::Inst * overflow{inst.GetAssociatedPseudoOperation(IR::Opcode::GetOverflowFromOp)}) {
45 // https://stackoverflow.com/questions/55468823/how-to-detect-integer-overflow-in-c
46 constexpr u32 s32_max{static_cast<u32>(std::numeric_limits<s32>::max())};
47 const auto sub_a{fmt::format("{}u-{}", s32_max, a)};
48 const auto op_b{ctx.uses_cc_carry ? "iadd_op_b" : b};
49 ctx.AddU1("{}=int({})>=0?int({})>int({}):int({})<int({});", *overflow, a, op_b, sub_a, op_b,
50 sub_a);
51 overflow->Invalidate();
52 }
53} 55}
54 56
55void EmitIAdd64(EmitContext& ctx, IR::Inst& inst, std::string_view a, std::string_view b) { 57void EmitIAdd64(EmitContext& ctx, IR::Inst& inst, std::string_view a, std::string_view b) {