summaryrefslogtreecommitdiff
path: root/src/shader_recompiler/backend
diff options
context:
space:
mode:
authorGravatar ReinUsesLisp2021-06-21 01:07:10 -0300
committerGravatar ameerj2021-07-22 21:51:39 -0400
commit808ef97a086e7cc58a3ceded1de516ad6a6be5d3 (patch)
treeb79be02801ddadb44940d2c77fa0ae5c571dc557 /src/shader_recompiler/backend
parentgl_graphics_pipeline: Fix assembly shaders check for transform feedbacks (diff)
downloadyuzu-808ef97a086e7cc58a3ceded1de516ad6a6be5d3.tar.gz
yuzu-808ef97a086e7cc58a3ceded1de516ad6a6be5d3.tar.xz
yuzu-808ef97a086e7cc58a3ceded1de516ad6a6be5d3.zip
shader: Move loop safety tests to code emission
Diffstat (limited to 'src/shader_recompiler/backend')
-rw-r--r--src/shader_recompiler/backend/glasm/emit_context.h1
-rw-r--r--src/shader_recompiler/backend/glasm/emit_glasm.cpp17
-rw-r--r--src/shader_recompiler/backend/glasm/emit_glasm_instructions.h2
-rw-r--r--src/shader_recompiler/backend/glasm/emit_glasm_not_implemented.cpp8
-rw-r--r--src/shader_recompiler/backend/glsl/emit_context.h2
-rw-r--r--src/shader_recompiler/backend/glsl/emit_glsl.cpp11
-rw-r--r--src/shader_recompiler/backend/glsl/emit_glsl_instructions.h2
-rw-r--r--src/shader_recompiler/backend/glsl/emit_glsl_not_implemented.cpp8
-rw-r--r--src/shader_recompiler/backend/spirv/emit_spirv.cpp19
-rw-r--r--src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp8
-rw-r--r--src/shader_recompiler/backend/spirv/emit_spirv_instructions.h2
11 files changed, 48 insertions, 32 deletions
diff --git a/src/shader_recompiler/backend/glasm/emit_context.h b/src/shader_recompiler/backend/glasm/emit_context.h
index cd4213cb7..9f86e55d3 100644
--- a/src/shader_recompiler/backend/glasm/emit_context.h
+++ b/src/shader_recompiler/backend/glasm/emit_context.h
@@ -71,6 +71,7 @@ public:
71 std::string_view stage_name = "invalid"; 71 std::string_view stage_name = "invalid";
72 std::string_view attrib_name = "invalid"; 72 std::string_view attrib_name = "invalid";
73 73
74 u32 num_safety_loop_vars{};
74 bool uses_y_direction{}; 75 bool uses_y_direction{};
75}; 76};
76 77
diff --git a/src/shader_recompiler/backend/glasm/emit_glasm.cpp b/src/shader_recompiler/backend/glasm/emit_glasm.cpp
index f39b02f77..79314f130 100644
--- a/src/shader_recompiler/backend/glasm/emit_glasm.cpp
+++ b/src/shader_recompiler/backend/glasm/emit_glasm.cpp
@@ -6,6 +6,8 @@
6#include <string> 6#include <string>
7#include <tuple> 7#include <tuple>
8 8
9#include "common/div_ceil.h"
10#include "common/settings.h"
9#include "shader_recompiler/backend/bindings.h" 11#include "shader_recompiler/backend/bindings.h"
10#include "shader_recompiler/backend/glasm/emit_context.h" 12#include "shader_recompiler/backend/glasm/emit_context.h"
11#include "shader_recompiler/backend/glasm/emit_glasm.h" 13#include "shader_recompiler/backend/glasm/emit_glasm.h"
@@ -222,6 +224,14 @@ void EmitCode(EmitContext& ctx, const IR::Program& program) {
222 ctx.Add("REP;"); 224 ctx.Add("REP;");
223 break; 225 break;
224 case IR::AbstractSyntaxNode::Type::Repeat: 226 case IR::AbstractSyntaxNode::Type::Repeat:
227 if (!Settings::values.disable_shader_loop_safety_checks) {
228 const u32 loop_index{ctx.num_safety_loop_vars++};
229 const u32 vector_index{loop_index / 4};
230 const char component{"xyzw"[loop_index % 4]};
231 ctx.Add("SUB.S.CC loop{}.{},loop{}.{},1;"
232 "BRK(LT.{});",
233 vector_index, component, vector_index, component, component);
234 }
225 if (node.data.repeat.cond.IsImmediate()) { 235 if (node.data.repeat.cond.IsImmediate()) {
226 if (node.data.repeat.cond.U1()) { 236 if (node.data.repeat.cond.U1()) {
227 ctx.Add("ENDREP;"); 237 ctx.Add("ENDREP;");
@@ -425,6 +435,10 @@ std::string EmitGLASM(const Profile& profile, const RuntimeInfo& runtime_info, I
425 if (program.info.uses_fswzadd) { 435 if (program.info.uses_fswzadd) {
426 header += "FSWZA[4],FSWZB[4],"; 436 header += "FSWZA[4],FSWZB[4],";
427 } 437 }
438 const u32 num_safety_loop_vectors{Common::DivCeil(ctx.num_safety_loop_vars, 4u)};
439 for (u32 index = 0; index < num_safety_loop_vectors; ++index) {
440 header += fmt::format("loop{},", index);
441 }
428 header += "RC;" 442 header += "RC;"
429 "LONG TEMP "; 443 "LONG TEMP ";
430 for (size_t index = 0; index < ctx.reg_alloc.NumUsedLongRegisters(); ++index) { 444 for (size_t index = 0; index < ctx.reg_alloc.NumUsedLongRegisters(); ++index) {
@@ -441,6 +455,9 @@ std::string EmitGLASM(const Profile& profile, const RuntimeInfo& runtime_info, I
441 "MOV.F FSWZB[2],1;" 455 "MOV.F FSWZB[2],1;"
442 "MOV.F FSWZB[3],-1;"; 456 "MOV.F FSWZB[3],-1;";
443 } 457 }
458 for (u32 index = 0; index < num_safety_loop_vectors; ++index) {
459 header += fmt::format("MOV.S loop{},{{0x2000,0x2000,0x2000,0x2000}};", index);
460 }
444 if (ctx.uses_y_direction) { 461 if (ctx.uses_y_direction) {
445 header += "PARAM y_direction[1]={state.material.front.ambient};"; 462 header += "PARAM y_direction[1]={state.material.front.ambient};";
446 } 463 }
diff --git a/src/shader_recompiler/backend/glasm/emit_glasm_instructions.h b/src/shader_recompiler/backend/glasm/emit_glasm_instructions.h
index fef9ff9be..c9f4826ce 100644
--- a/src/shader_recompiler/backend/glasm/emit_glasm_instructions.h
+++ b/src/shader_recompiler/backend/glasm/emit_glasm_instructions.h
@@ -42,8 +42,6 @@ void EmitSetGotoVariable(EmitContext& ctx);
42void EmitGetGotoVariable(EmitContext& ctx); 42void EmitGetGotoVariable(EmitContext& ctx);
43void EmitSetIndirectBranchVariable(EmitContext& ctx); 43void EmitSetIndirectBranchVariable(EmitContext& ctx);
44void EmitGetIndirectBranchVariable(EmitContext& ctx); 44void EmitGetIndirectBranchVariable(EmitContext& ctx);
45void EmitSetLoopSafetyVariable(EmitContext& ctx);
46void EmitGetLoopSafetyVariable(EmitContext& ctx);
47void EmitGetCbufU8(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset); 45void EmitGetCbufU8(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset);
48void EmitGetCbufS8(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset); 46void EmitGetCbufS8(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset);
49void EmitGetCbufU16(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset); 47void EmitGetCbufU16(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset);
diff --git a/src/shader_recompiler/backend/glasm/emit_glasm_not_implemented.cpp b/src/shader_recompiler/backend/glasm/emit_glasm_not_implemented.cpp
index a487a0744..ff64c6924 100644
--- a/src/shader_recompiler/backend/glasm/emit_glasm_not_implemented.cpp
+++ b/src/shader_recompiler/backend/glasm/emit_glasm_not_implemented.cpp
@@ -153,14 +153,6 @@ void EmitGetIndirectBranchVariable(EmitContext& ctx) {
153 NotImplemented(); 153 NotImplemented();
154} 154}
155 155
156void EmitSetLoopSafetyVariable(EmitContext& ctx) {
157 NotImplemented();
158}
159
160void EmitGetLoopSafetyVariable(EmitContext& ctx) {
161 NotImplemented();
162}
163
164void EmitGetZFlag(EmitContext& ctx) { 156void EmitGetZFlag(EmitContext& ctx) {
165 NotImplemented(); 157 NotImplemented();
166} 158}
diff --git a/src/shader_recompiler/backend/glsl/emit_context.h b/src/shader_recompiler/backend/glsl/emit_context.h
index 8fa87c02c..ecdf6e5bc 100644
--- a/src/shader_recompiler/backend/glsl/emit_context.h
+++ b/src/shader_recompiler/backend/glsl/emit_context.h
@@ -153,6 +153,8 @@ public:
153 std::vector<TextureImageDefinition> images; 153 std::vector<TextureImageDefinition> images;
154 std::array<std::array<GenericElementInfo, 4>, 32> output_generics{}; 154 std::array<std::array<GenericElementInfo, 4>, 32> output_generics{};
155 155
156 u32 num_safety_loop_vars{};
157
156 bool uses_y_direction{}; 158 bool uses_y_direction{};
157 bool uses_cc_carry{}; 159 bool uses_cc_carry{};
158 160
diff --git a/src/shader_recompiler/backend/glsl/emit_glsl.cpp b/src/shader_recompiler/backend/glsl/emit_glsl.cpp
index ff869923f..32c4f1da2 100644
--- a/src/shader_recompiler/backend/glsl/emit_glsl.cpp
+++ b/src/shader_recompiler/backend/glsl/emit_glsl.cpp
@@ -6,6 +6,7 @@
6#include <string> 6#include <string>
7 7
8#include "common/alignment.h" 8#include "common/alignment.h"
9#include "common/settings.h"
9#include "shader_recompiler/backend/glsl/emit_context.h" 10#include "shader_recompiler/backend/glsl/emit_context.h"
10#include "shader_recompiler/backend/glsl/emit_glsl.h" 11#include "shader_recompiler/backend/glsl/emit_glsl.h"
11#include "shader_recompiler/backend/glsl/emit_glsl_instructions.h" 12#include "shader_recompiler/backend/glsl/emit_glsl_instructions.h"
@@ -156,7 +157,12 @@ void EmitCode(EmitContext& ctx, const IR::Program& program) {
156 ctx.Add("for(;;){{"); 157 ctx.Add("for(;;){{");
157 break; 158 break;
158 case IR::AbstractSyntaxNode::Type::Repeat: 159 case IR::AbstractSyntaxNode::Type::Repeat:
159 ctx.Add("if(!{}){{break;}}}}", ctx.var_alloc.Consume(node.data.repeat.cond)); 160 if (Settings::values.disable_shader_loop_safety_checks) {
161 ctx.Add("if(!{}){{break;}}}}", ctx.var_alloc.Consume(node.data.repeat.cond));
162 } else {
163 ctx.Add("if(--loop{}<0 || !{}){{break;}}}}", ctx.num_safety_loop_vars++,
164 ctx.var_alloc.Consume(node.data.repeat.cond));
165 }
160 break; 166 break;
161 default: 167 default:
162 throw NotImplementedException("AbstractSyntaxNode Type {}", node.type); 168 throw NotImplementedException("AbstractSyntaxNode Type {}", node.type);
@@ -198,6 +204,9 @@ void DefineVariables(const EmitContext& ctx, std::string& header) {
198 ctx.var_alloc.Representation(index, type), type_name); 204 ctx.var_alloc.Representation(index, type), type_name);
199 } 205 }
200 } 206 }
207 for (u32 i = 0; i < ctx.num_safety_loop_vars; ++i) {
208 header += fmt::format("int loop{}=0x2000;", i);
209 }
201} 210}
202} // Anonymous namespace 211} // Anonymous namespace
203 212
diff --git a/src/shader_recompiler/backend/glsl/emit_glsl_instructions.h b/src/shader_recompiler/backend/glsl/emit_glsl_instructions.h
index df28036e4..6a30785bb 100644
--- a/src/shader_recompiler/backend/glsl/emit_glsl_instructions.h
+++ b/src/shader_recompiler/backend/glsl/emit_glsl_instructions.h
@@ -44,8 +44,6 @@ void EmitSetGotoVariable(EmitContext& ctx);
44void EmitGetGotoVariable(EmitContext& ctx); 44void EmitGetGotoVariable(EmitContext& ctx);
45void EmitSetIndirectBranchVariable(EmitContext& ctx); 45void EmitSetIndirectBranchVariable(EmitContext& ctx);
46void EmitGetIndirectBranchVariable(EmitContext& ctx); 46void EmitGetIndirectBranchVariable(EmitContext& ctx);
47void EmitSetLoopSafetyVariable(EmitContext& ctx);
48void EmitGetLoopSafetyVariable(EmitContext& ctx);
49void EmitGetCbufU8(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, 47void EmitGetCbufU8(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
50 const IR::Value& offset); 48 const IR::Value& offset);
51void EmitGetCbufS8(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, 49void EmitGetCbufS8(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
diff --git a/src/shader_recompiler/backend/glsl/emit_glsl_not_implemented.cpp b/src/shader_recompiler/backend/glsl/emit_glsl_not_implemented.cpp
index 0a28a1ffc..f420fe388 100644
--- a/src/shader_recompiler/backend/glsl/emit_glsl_not_implemented.cpp
+++ b/src/shader_recompiler/backend/glsl/emit_glsl_not_implemented.cpp
@@ -46,14 +46,6 @@ void EmitGetIndirectBranchVariable(EmitContext& ctx) {
46 NotImplemented(); 46 NotImplemented();
47} 47}
48 48
49void EmitSetLoopSafetyVariable(EmitContext& ctx) {
50 NotImplemented();
51}
52
53void EmitGetLoopSafetyVariable(EmitContext& ctx) {
54 NotImplemented();
55}
56
57void EmitGetZFlag(EmitContext& ctx) { 49void EmitGetZFlag(EmitContext& ctx) {
58 NotImplemented(); 50 NotImplemented();
59} 51}
diff --git a/src/shader_recompiler/backend/spirv/emit_spirv.cpp b/src/shader_recompiler/backend/spirv/emit_spirv.cpp
index fd59b4d0a..278c262f8 100644
--- a/src/shader_recompiler/backend/spirv/emit_spirv.cpp
+++ b/src/shader_recompiler/backend/spirv/emit_spirv.cpp
@@ -8,6 +8,7 @@
8#include <utility> 8#include <utility>
9#include <vector> 9#include <vector>
10 10
11#include "common/settings.h"
11#include "shader_recompiler/backend/spirv/emit_spirv.h" 12#include "shader_recompiler/backend/spirv/emit_spirv.h"
12#include "shader_recompiler/backend/spirv/emit_spirv_instructions.h" 13#include "shader_recompiler/backend/spirv/emit_spirv_instructions.h"
13#include "shader_recompiler/frontend/ir/basic_block.h" 14#include "shader_recompiler/frontend/ir/basic_block.h"
@@ -151,9 +152,25 @@ void Traverse(EmitContext& ctx, IR::Program& program) {
151 } 152 }
152 break; 153 break;
153 case IR::AbstractSyntaxNode::Type::Repeat: { 154 case IR::AbstractSyntaxNode::Type::Repeat: {
155 Id cond{ctx.Def(node.data.repeat.cond)};
156 if (!Settings::values.disable_shader_loop_safety_checks) {
157 const Id pointer_type{ctx.TypePointer(spv::StorageClass::Private, ctx.U32[1])};
158 const Id safety_counter{ctx.AddGlobalVariable(
159 pointer_type, spv::StorageClass::Private, ctx.Const(0x2000u))};
160 if (ctx.profile.supported_spirv >= 0x00010400) {
161 ctx.interfaces.push_back(safety_counter);
162 }
163 const Id old_counter{ctx.OpLoad(ctx.U32[1], safety_counter)};
164 const Id new_counter{ctx.OpISub(ctx.U32[1], old_counter, ctx.Const(1u))};
165 ctx.OpStore(safety_counter, new_counter);
166
167 const Id safety_cond{
168 ctx.OpSGreaterThanEqual(ctx.U1, new_counter, ctx.u32_zero_value)};
169 cond = ctx.OpLogicalAnd(ctx.U1, cond, safety_cond);
170 }
154 const Id loop_header_label{node.data.repeat.loop_header->Definition<Id>()}; 171 const Id loop_header_label{node.data.repeat.loop_header->Definition<Id>()};
155 const Id merge_label{node.data.repeat.merge->Definition<Id>()}; 172 const Id merge_label{node.data.repeat.merge->Definition<Id>()};
156 ctx.OpBranchConditional(ctx.Def(node.data.repeat.cond), loop_header_label, merge_label); 173 ctx.OpBranchConditional(cond, loop_header_label, merge_label);
157 break; 174 break;
158 } 175 }
159 case IR::AbstractSyntaxNode::Type::Return: 176 case IR::AbstractSyntaxNode::Type::Return:
diff --git a/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp b/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp
index 2e364baec..85bd72389 100644
--- a/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp
+++ b/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp
@@ -198,14 +198,6 @@ void EmitGetIndirectBranchVariable(EmitContext&) {
198 throw LogicError("Unreachable instruction"); 198 throw LogicError("Unreachable instruction");
199} 199}
200 200
201void EmitSetLoopSafetyVariable(EmitContext&) {
202 throw LogicError("Unreachable instruction");
203}
204
205void EmitGetLoopSafetyVariable(EmitContext&) {
206 throw LogicError("Unreachable instruction");
207}
208
209Id EmitGetCbufU8(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) { 201Id EmitGetCbufU8(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) {
210 if (ctx.profile.support_descriptor_aliasing && ctx.profile.support_int8) { 202 if (ctx.profile.support_descriptor_aliasing && ctx.profile.support_int8) {
211 const Id load{GetCbuf(ctx, ctx.U8, &UniformDefinitions::U8, sizeof(u8), binding, offset)}; 203 const Id load{GetCbuf(ctx, ctx.U8, &UniformDefinitions::U8, sizeof(u8), binding, offset)};
diff --git a/src/shader_recompiler/backend/spirv/emit_spirv_instructions.h b/src/shader_recompiler/backend/spirv/emit_spirv_instructions.h
index e3e5b03fe..1181e7b4f 100644
--- a/src/shader_recompiler/backend/spirv/emit_spirv_instructions.h
+++ b/src/shader_recompiler/backend/spirv/emit_spirv_instructions.h
@@ -43,8 +43,6 @@ void EmitSetGotoVariable(EmitContext& ctx);
43void EmitGetGotoVariable(EmitContext& ctx); 43void EmitGetGotoVariable(EmitContext& ctx);
44void EmitSetIndirectBranchVariable(EmitContext& ctx); 44void EmitSetIndirectBranchVariable(EmitContext& ctx);
45void EmitGetIndirectBranchVariable(EmitContext& ctx); 45void EmitGetIndirectBranchVariable(EmitContext& ctx);
46void EmitSetLoopSafetyVariable(EmitContext& ctx);
47void EmitGetLoopSafetyVariable(EmitContext& ctx);
48Id EmitGetCbufU8(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset); 46Id EmitGetCbufU8(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset);
49Id EmitGetCbufS8(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset); 47Id EmitGetCbufS8(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset);
50Id EmitGetCbufU16(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset); 48Id EmitGetCbufU16(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset);