summaryrefslogtreecommitdiff
path: root/src/shader_recompiler/backend
diff options
context:
space:
mode:
Diffstat (limited to 'src/shader_recompiler/backend')
-rw-r--r--src/shader_recompiler/backend/glasm/emit_glasm_context_get_set.cpp24
-rw-r--r--src/shader_recompiler/backend/glasm/emit_glasm_instructions.h1
-rw-r--r--src/shader_recompiler/backend/glsl/emit_glsl.cpp2
-rw-r--r--src/shader_recompiler/backend/glsl/emit_glsl_bitwise_conversion.cpp4
-rw-r--r--src/shader_recompiler/backend/glsl/emit_glsl_context_get_set.cpp115
-rw-r--r--src/shader_recompiler/backend/glsl/emit_glsl_floating_point.cpp4
-rw-r--r--src/shader_recompiler/backend/glsl/emit_glsl_instructions.h2
-rw-r--r--src/shader_recompiler/backend/glsl/emit_glsl_integer.cpp4
-rw-r--r--src/shader_recompiler/backend/glsl/emit_glsl_special.cpp4
-rw-r--r--src/shader_recompiler/backend/glsl/glsl_emit_context.cpp45
-rw-r--r--src/shader_recompiler/backend/spirv/emit_spirv.cpp9
-rw-r--r--src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp56
-rw-r--r--src/shader_recompiler/backend/spirv/emit_spirv_instructions.h1
-rw-r--r--src/shader_recompiler/backend/spirv/spirv_emit_context.cpp143
-rw-r--r--src/shader_recompiler/backend/spirv/spirv_emit_context.h15
15 files changed, 110 insertions, 319 deletions
diff --git a/src/shader_recompiler/backend/glasm/emit_glasm_context_get_set.cpp b/src/shader_recompiler/backend/glasm/emit_glasm_context_get_set.cpp
index 081b2c8e0..7434a1f92 100644
--- a/src/shader_recompiler/backend/glasm/emit_glasm_context_get_set.cpp
+++ b/src/shader_recompiler/backend/glasm/emit_glasm_context_get_set.cpp
@@ -86,7 +86,7 @@ void EmitGetAttribute(EmitContext& ctx, IR::Inst& inst, IR::Attribute attr, Scal
86 } 86 }
87 switch (attr) { 87 switch (attr) {
88 case IR::Attribute::PrimitiveId: 88 case IR::Attribute::PrimitiveId:
89 ctx.Add("MOV.S {}.x,primitive.id;", inst); 89 ctx.Add("MOV.F {}.x,primitive.id;", inst);
90 break; 90 break;
91 case IR::Attribute::PositionX: 91 case IR::Attribute::PositionX:
92 case IR::Attribute::PositionY: 92 case IR::Attribute::PositionY:
@@ -113,19 +113,35 @@ void EmitGetAttribute(EmitContext& ctx, IR::Inst& inst, IR::Attribute attr, Scal
113 ctx.Add("MOV.F {}.x,vertex.tesscoord.{};", inst, swizzle); 113 ctx.Add("MOV.F {}.x,vertex.tesscoord.{};", inst, swizzle);
114 break; 114 break;
115 case IR::Attribute::InstanceId: 115 case IR::Attribute::InstanceId:
116 ctx.Add("MOV.S {}.x,{}.instance;", inst, ctx.attrib_name); 116 ctx.Add("MOV.F {}.x,{}.instance;", inst, ctx.attrib_name);
117 break; 117 break;
118 case IR::Attribute::VertexId: 118 case IR::Attribute::VertexId:
119 ctx.Add("MOV.S {}.x,{}.id;", inst, ctx.attrib_name); 119 ctx.Add("MOV.F {}.x,{}.id;", inst, ctx.attrib_name);
120 break; 120 break;
121 case IR::Attribute::FrontFace: 121 case IR::Attribute::FrontFace:
122 ctx.Add("CMP.S {}.x,{}.facing.x,0,-1;", inst, ctx.attrib_name); 122 ctx.Add("CMP.F {}.x,{}.facing.x,0,-1;", inst, ctx.attrib_name);
123 break; 123 break;
124 default: 124 default:
125 throw NotImplementedException("Get attribute {}", attr); 125 throw NotImplementedException("Get attribute {}", attr);
126 } 126 }
127} 127}
128 128
129void EmitGetAttributeU32(EmitContext& ctx, IR::Inst& inst, IR::Attribute attr, ScalarU32) {
130 switch (attr) {
131 case IR::Attribute::PrimitiveId:
132 ctx.Add("MOV.S {}.x,primitive.id;", inst);
133 break;
134 case IR::Attribute::InstanceId:
135 ctx.Add("MOV.S {}.x,{}.instance;", inst, ctx.attrib_name);
136 break;
137 case IR::Attribute::VertexId:
138 ctx.Add("MOV.S {}.x,{}.id;", inst, ctx.attrib_name);
139 break;
140 default:
141 throw NotImplementedException("Get U32 attribute {}", attr);
142 }
143}
144
129void EmitSetAttribute(EmitContext& ctx, IR::Attribute attr, ScalarF32 value, 145void EmitSetAttribute(EmitContext& ctx, IR::Attribute attr, ScalarF32 value,
130 [[maybe_unused]] ScalarU32 vertex) { 146 [[maybe_unused]] ScalarU32 vertex) {
131 const u32 element{static_cast<u32>(attr) % 4}; 147 const u32 element{static_cast<u32>(attr) % 4};
diff --git a/src/shader_recompiler/backend/glasm/emit_glasm_instructions.h b/src/shader_recompiler/backend/glasm/emit_glasm_instructions.h
index 1f343bff5..b48007856 100644
--- a/src/shader_recompiler/backend/glasm/emit_glasm_instructions.h
+++ b/src/shader_recompiler/backend/glasm/emit_glasm_instructions.h
@@ -50,6 +50,7 @@ void EmitGetCbufU32(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
50void EmitGetCbufF32(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset); 50void EmitGetCbufF32(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset);
51void EmitGetCbufU32x2(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset); 51void EmitGetCbufU32x2(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, ScalarU32 offset);
52void EmitGetAttribute(EmitContext& ctx, IR::Inst& inst, IR::Attribute attr, ScalarU32 vertex); 52void EmitGetAttribute(EmitContext& ctx, IR::Inst& inst, IR::Attribute attr, ScalarU32 vertex);
53void EmitGetAttributeU32(EmitContext& ctx, IR::Inst& inst, IR::Attribute attr, ScalarU32 vertex);
53void EmitSetAttribute(EmitContext& ctx, IR::Attribute attr, ScalarF32 value, ScalarU32 vertex); 54void EmitSetAttribute(EmitContext& ctx, IR::Attribute attr, ScalarF32 value, ScalarU32 vertex);
54void EmitGetAttributeIndexed(EmitContext& ctx, IR::Inst& inst, ScalarS32 offset, ScalarU32 vertex); 55void EmitGetAttributeIndexed(EmitContext& ctx, IR::Inst& inst, ScalarS32 offset, ScalarU32 vertex);
55void EmitSetAttributeIndexed(EmitContext& ctx, ScalarU32 offset, ScalarF32 value, ScalarU32 vertex); 56void EmitSetAttributeIndexed(EmitContext& ctx, ScalarU32 offset, ScalarF32 value, ScalarU32 vertex);
diff --git a/src/shader_recompiler/backend/glsl/emit_glsl.cpp b/src/shader_recompiler/backend/glsl/emit_glsl.cpp
index 78b2eeaa2..b6b17a330 100644
--- a/src/shader_recompiler/backend/glsl/emit_glsl.cpp
+++ b/src/shader_recompiler/backend/glsl/emit_glsl.cpp
@@ -176,7 +176,7 @@ void EmitCode(EmitContext& ctx, const IR::Program& program) {
176} 176}
177 177
178std::string GlslVersionSpecifier(const EmitContext& ctx) { 178std::string GlslVersionSpecifier(const EmitContext& ctx) {
179 if (ctx.uses_y_direction || ctx.info.stores.Legacy() || ctx.info.loads.Legacy()) { 179 if (ctx.uses_y_direction) {
180 return " compatibility"; 180 return " compatibility";
181 } 181 }
182 return ""; 182 return "";
diff --git a/src/shader_recompiler/backend/glsl/emit_glsl_bitwise_conversion.cpp b/src/shader_recompiler/backend/glsl/emit_glsl_bitwise_conversion.cpp
index 0f2668d9e..e0ead7a53 100644
--- a/src/shader_recompiler/backend/glsl/emit_glsl_bitwise_conversion.cpp
+++ b/src/shader_recompiler/backend/glsl/emit_glsl_bitwise_conversion.cpp
@@ -7,6 +7,7 @@
7#include "shader_recompiler/backend/glsl/emit_glsl_instructions.h" 7#include "shader_recompiler/backend/glsl/emit_glsl_instructions.h"
8#include "shader_recompiler/backend/glsl/glsl_emit_context.h" 8#include "shader_recompiler/backend/glsl/glsl_emit_context.h"
9#include "shader_recompiler/frontend/ir/value.h" 9#include "shader_recompiler/frontend/ir/value.h"
10#include "shader_recompiler/profile.h"
10 11
11namespace Shader::Backend::GLSL { 12namespace Shader::Backend::GLSL {
12namespace { 13namespace {
@@ -30,8 +31,9 @@ void EmitConditionRef(EmitContext& ctx, IR::Inst& inst, const IR::Value& value)
30 inst.DestructiveAddUsage(1); 31 inst.DestructiveAddUsage(1);
31 const auto ret{ctx.var_alloc.Define(inst, GlslVarType::U1)}; 32 const auto ret{ctx.var_alloc.Define(inst, GlslVarType::U1)};
32 const auto input{ctx.var_alloc.Consume(value)}; 33 const auto input{ctx.var_alloc.Consume(value)};
34 const auto suffix{ctx.profile.has_gl_bool_ref_bug ? "?true:false" : ""};
33 if (ret != input) { 35 if (ret != input) {
34 ctx.Add("{}={};", ret, input); 36 ctx.Add("{}={}{};", ret, input, suffix);
35 } 37 }
36} 38}
37 39
diff --git a/src/shader_recompiler/backend/glsl/emit_glsl_context_get_set.cpp b/src/shader_recompiler/backend/glsl/emit_glsl_context_get_set.cpp
index 1920047f4..0c1fbc7b1 100644
--- a/src/shader_recompiler/backend/glsl/emit_glsl_context_get_set.cpp
+++ b/src/shader_recompiler/backend/glsl/emit_glsl_context_get_set.cpp
@@ -98,47 +98,50 @@ void GetCbuf16(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, const
98 GetCbuf(ctx, ret, binding, offset, 16, cast, bit_offset); 98 GetCbuf(ctx, ret, binding, offset, 16, cast, bit_offset);
99 } 99 }
100} 100}
101
102u32 TexCoordIndex(IR::Attribute attr) {
103 return (static_cast<u32>(attr) - static_cast<u32>(IR::Attribute::FixedFncTexture0S)) / 4;
104}
105} // Anonymous namespace 101} // Anonymous namespace
106 102
107void EmitGetCbufU8(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, 103void EmitGetCbufU8(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
108 const IR::Value& offset) { 104 const IR::Value& offset) {
109 GetCbuf8(ctx, inst, binding, offset, "ftou"); 105 const auto cast{ctx.profile.has_gl_cbuf_ftou_bug ? "" : "ftou"};
106 GetCbuf8(ctx, inst, binding, offset, cast);
110} 107}
111 108
112void EmitGetCbufS8(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, 109void EmitGetCbufS8(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
113 const IR::Value& offset) { 110 const IR::Value& offset) {
114 GetCbuf8(ctx, inst, binding, offset, "ftoi"); 111 const auto cast{ctx.profile.has_gl_cbuf_ftou_bug ? "int" : "ftoi"};
112 GetCbuf8(ctx, inst, binding, offset, cast);
115} 113}
116 114
117void EmitGetCbufU16(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, 115void EmitGetCbufU16(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
118 const IR::Value& offset) { 116 const IR::Value& offset) {
119 GetCbuf16(ctx, inst, binding, offset, "ftou"); 117 const auto cast{ctx.profile.has_gl_cbuf_ftou_bug ? "" : "ftou"};
118 GetCbuf16(ctx, inst, binding, offset, cast);
120} 119}
121 120
122void EmitGetCbufS16(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, 121void EmitGetCbufS16(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
123 const IR::Value& offset) { 122 const IR::Value& offset) {
124 GetCbuf16(ctx, inst, binding, offset, "ftoi"); 123 const auto cast{ctx.profile.has_gl_cbuf_ftou_bug ? "int" : "ftoi"};
124 GetCbuf16(ctx, inst, binding, offset, cast);
125} 125}
126 126
127void EmitGetCbufU32(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, 127void EmitGetCbufU32(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
128 const IR::Value& offset) { 128 const IR::Value& offset) {
129 const auto ret{ctx.var_alloc.Define(inst, GlslVarType::U32)}; 129 const auto ret{ctx.var_alloc.Define(inst, GlslVarType::U32)};
130 GetCbuf(ctx, ret, binding, offset, 32, "ftou"); 130 const auto cast{ctx.profile.has_gl_cbuf_ftou_bug ? "" : "ftou"};
131 GetCbuf(ctx, ret, binding, offset, 32, cast);
131} 132}
132 133
133void EmitGetCbufF32(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, 134void EmitGetCbufF32(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
134 const IR::Value& offset) { 135 const IR::Value& offset) {
135 const auto ret{ctx.var_alloc.Define(inst, GlslVarType::F32)}; 136 const auto ret{ctx.var_alloc.Define(inst, GlslVarType::F32)};
136 GetCbuf(ctx, ret, binding, offset, 32); 137 const auto cast{ctx.profile.has_gl_cbuf_ftou_bug ? "utof" : ""};
138 GetCbuf(ctx, ret, binding, offset, 32, cast);
137} 139}
138 140
139void EmitGetCbufU32x2(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding, 141void EmitGetCbufU32x2(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding,
140 const IR::Value& offset) { 142 const IR::Value& offset) {
141 const auto cbuf{fmt::format("{}_cbuf{}", ctx.stage_name, binding.U32())}; 143 const auto cbuf{fmt::format("{}_cbuf{}", ctx.stage_name, binding.U32())};
144 const auto cast{ctx.profile.has_gl_cbuf_ftou_bug ? "" : "ftou"};
142 if (offset.IsImmediate()) { 145 if (offset.IsImmediate()) {
143 static constexpr u32 cbuf_size{0x10000}; 146 static constexpr u32 cbuf_size{0x10000};
144 const u32 u32_offset{offset.U32()}; 147 const u32 u32_offset{offset.U32()};
@@ -149,26 +152,26 @@ void EmitGetCbufU32x2(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding
149 return; 152 return;
150 } 153 }
151 if (u32_offset % 2 == 0) { 154 if (u32_offset % 2 == 0) {
152 ctx.AddU32x2("{}=ftou({}[{}].{}{});", inst, cbuf, u32_offset / 16, 155 ctx.AddU32x2("{}={}({}[{}].{}{});", inst, cast, cbuf, u32_offset / 16,
153 OffsetSwizzle(u32_offset), OffsetSwizzle(u32_offset + 4)); 156 OffsetSwizzle(u32_offset), OffsetSwizzle(u32_offset + 4));
154 } else { 157 } else {
155 ctx.AddU32x2("{}=uvec2(ftou({}[{}].{}),ftou({}[{}].{}));", inst, cbuf, u32_offset / 16, 158 ctx.AddU32x2("{}=uvec2({}({}[{}].{}),{}({}[{}].{}));", inst, cast, cbuf,
156 OffsetSwizzle(u32_offset), cbuf, (u32_offset + 4) / 16, 159 u32_offset / 16, OffsetSwizzle(u32_offset), cast, cbuf,
157 OffsetSwizzle(u32_offset + 4)); 160 (u32_offset + 4) / 16, OffsetSwizzle(u32_offset + 4));
158 } 161 }
159 return; 162 return;
160 } 163 }
161 const auto offset_var{ctx.var_alloc.Consume(offset)}; 164 const auto offset_var{ctx.var_alloc.Consume(offset)};
162 if (!ctx.profile.has_gl_component_indexing_bug) { 165 if (!ctx.profile.has_gl_component_indexing_bug) {
163 ctx.AddU32x2("{}=uvec2(ftou({}[{}>>4][({}>>2)%4]),ftou({}[({}+4)>>4][(({}+4)>>2)%4]));", 166 ctx.AddU32x2("{}=uvec2({}({}[{}>>4][({}>>2)%4]),{}({}[({}+4)>>4][(({}+4)>>2)%4]));", inst,
164 inst, cbuf, offset_var, offset_var, cbuf, offset_var, offset_var); 167 cast, cbuf, offset_var, offset_var, cast, cbuf, offset_var, offset_var);
165 return; 168 return;
166 } 169 }
167 const auto ret{ctx.var_alloc.Define(inst, GlslVarType::U32x2)}; 170 const auto ret{ctx.var_alloc.Define(inst, GlslVarType::U32x2)};
168 const auto cbuf_offset{fmt::format("{}>>2", offset_var)}; 171 const auto cbuf_offset{fmt::format("{}>>2", offset_var)};
169 for (u32 swizzle = 0; swizzle < 4; ++swizzle) { 172 for (u32 swizzle = 0; swizzle < 4; ++swizzle) {
170 ctx.Add("if(({}&3)=={}){}=uvec2(ftou({}[{}>>4].{}),ftou({}[({}+4)>>4].{}));", cbuf_offset, 173 ctx.Add("if(({}&3)=={}){}=uvec2({}({}[{}>>4].{}),{}({}[({}+4)>>4].{}));", cbuf_offset,
171 swizzle, ret, cbuf, offset_var, "xyzw"[swizzle], cbuf, offset_var, 174 swizzle, ret, cast, cbuf, offset_var, "xyzw"[swizzle], cast, cbuf, offset_var,
172 "xyzw"[(swizzle + 1) % 4]); 175 "xyzw"[(swizzle + 1) % 4]);
173 } 176 }
174} 177}
@@ -190,18 +193,6 @@ void EmitGetAttribute(EmitContext& ctx, IR::Inst& inst, IR::Attribute attr,
190 ctx.AddF32("{}=in_attr{}{}.{};", inst, index, InputVertexIndex(ctx, vertex), swizzle); 193 ctx.AddF32("{}=in_attr{}{}.{};", inst, index, InputVertexIndex(ctx, vertex), swizzle);
191 return; 194 return;
192 } 195 }
193 // GLSL only exposes 8 legacy texcoords
194 if (attr >= IR::Attribute::FixedFncTexture8S && attr <= IR::Attribute::FixedFncTexture9Q) {
195 LOG_WARNING(Shader_GLSL, "GLSL does not allow access to gl_TexCoord[{}]",
196 TexCoordIndex(attr));
197 ctx.AddF32("{}=0.f;", inst);
198 return;
199 }
200 if (attr >= IR::Attribute::FixedFncTexture0S && attr <= IR::Attribute::FixedFncTexture7Q) {
201 const u32 index{TexCoordIndex(attr)};
202 ctx.AddF32("{}=gl_TexCoord[{}].{};", inst, index, swizzle);
203 return;
204 }
205 switch (attr) { 196 switch (attr) {
206 case IR::Attribute::PrimitiveId: 197 case IR::Attribute::PrimitiveId:
207 ctx.AddF32("{}=itof(gl_PrimitiveID);", inst); 198 ctx.AddF32("{}=itof(gl_PrimitiveID);", inst);
@@ -215,16 +206,6 @@ void EmitGetAttribute(EmitContext& ctx, IR::Inst& inst, IR::Attribute attr,
215 ctx.AddF32("{}={}{}.{};", inst, input_decorator, ctx.position_name, swizzle); 206 ctx.AddF32("{}={}{}.{};", inst, input_decorator, ctx.position_name, swizzle);
216 break; 207 break;
217 } 208 }
218 case IR::Attribute::ColorFrontDiffuseR:
219 case IR::Attribute::ColorFrontDiffuseG:
220 case IR::Attribute::ColorFrontDiffuseB:
221 case IR::Attribute::ColorFrontDiffuseA:
222 if (ctx.stage == Stage::Fragment) {
223 ctx.AddF32("{}=gl_Color.{};", inst, swizzle);
224 } else {
225 ctx.AddF32("{}=gl_FrontColor.{};", inst, swizzle);
226 }
227 break;
228 case IR::Attribute::PointSpriteS: 209 case IR::Attribute::PointSpriteS:
229 case IR::Attribute::PointSpriteT: 210 case IR::Attribute::PointSpriteT:
230 ctx.AddF32("{}=gl_PointCoord.{};", inst, swizzle); 211 ctx.AddF32("{}=gl_PointCoord.{};", inst, swizzle);
@@ -247,6 +228,22 @@ void EmitGetAttribute(EmitContext& ctx, IR::Inst& inst, IR::Attribute attr,
247 } 228 }
248} 229}
249 230
231void EmitGetAttributeU32(EmitContext& ctx, IR::Inst& inst, IR::Attribute attr, std::string_view) {
232 switch (attr) {
233 case IR::Attribute::PrimitiveId:
234 ctx.AddU32("{}=uint(gl_PrimitiveID);", inst);
235 break;
236 case IR::Attribute::InstanceId:
237 ctx.AddU32("{}=uint(gl_InstanceID);", inst);
238 break;
239 case IR::Attribute::VertexId:
240 ctx.AddU32("{}=uint(gl_VertexID);", inst);
241 break;
242 default:
243 throw NotImplementedException("Get U32 attribute {}", attr);
244 }
245}
246
250void EmitSetAttribute(EmitContext& ctx, IR::Attribute attr, std::string_view value, 247void EmitSetAttribute(EmitContext& ctx, IR::Attribute attr, std::string_view value,
251 [[maybe_unused]] std::string_view vertex) { 248 [[maybe_unused]] std::string_view vertex) {
252 if (IR::IsGeneric(attr)) { 249 if (IR::IsGeneric(attr)) {
@@ -264,17 +261,6 @@ void EmitSetAttribute(EmitContext& ctx, IR::Attribute attr, std::string_view val
264 } 261 }
265 const u32 element{static_cast<u32>(attr) % 4}; 262 const u32 element{static_cast<u32>(attr) % 4};
266 const char swizzle{"xyzw"[element]}; 263 const char swizzle{"xyzw"[element]};
267 // GLSL only exposes 8 legacy texcoords
268 if (attr >= IR::Attribute::FixedFncTexture8S && attr <= IR::Attribute::FixedFncTexture9Q) {
269 LOG_WARNING(Shader_GLSL, "GLSL does not allow access to gl_TexCoord[{}]",
270 TexCoordIndex(attr));
271 return;
272 }
273 if (attr >= IR::Attribute::FixedFncTexture0S && attr <= IR::Attribute::FixedFncTexture7Q) {
274 const u32 index{TexCoordIndex(attr)};
275 ctx.Add("gl_TexCoord[{}].{}={};", index, swizzle, value);
276 return;
277 }
278 switch (attr) { 264 switch (attr) {
279 case IR::Attribute::Layer: 265 case IR::Attribute::Layer:
280 if (ctx.stage != Stage::Geometry && 266 if (ctx.stage != Stage::Geometry &&
@@ -312,33 +298,6 @@ void EmitSetAttribute(EmitContext& ctx, IR::Attribute attr, std::string_view val
312 case IR::Attribute::PositionW: 298 case IR::Attribute::PositionW:
313 ctx.Add("gl_Position.{}={};", swizzle, value); 299 ctx.Add("gl_Position.{}={};", swizzle, value);
314 break; 300 break;
315 case IR::Attribute::ColorFrontDiffuseR:
316 case IR::Attribute::ColorFrontDiffuseG:
317 case IR::Attribute::ColorFrontDiffuseB:
318 case IR::Attribute::ColorFrontDiffuseA:
319 ctx.Add("gl_FrontColor.{}={};", swizzle, value);
320 break;
321 case IR::Attribute::ColorFrontSpecularR:
322 case IR::Attribute::ColorFrontSpecularG:
323 case IR::Attribute::ColorFrontSpecularB:
324 case IR::Attribute::ColorFrontSpecularA:
325 ctx.Add("gl_FrontSecondaryColor.{}={};", swizzle, value);
326 break;
327 case IR::Attribute::ColorBackDiffuseR:
328 case IR::Attribute::ColorBackDiffuseG:
329 case IR::Attribute::ColorBackDiffuseB:
330 case IR::Attribute::ColorBackDiffuseA:
331 ctx.Add("gl_BackColor.{}={};", swizzle, value);
332 break;
333 case IR::Attribute::ColorBackSpecularR:
334 case IR::Attribute::ColorBackSpecularG:
335 case IR::Attribute::ColorBackSpecularB:
336 case IR::Attribute::ColorBackSpecularA:
337 ctx.Add("gl_BackSecondaryColor.{}={};", swizzle, value);
338 break;
339 case IR::Attribute::FogCoordinate:
340 ctx.Add("gl_FogFragCoord={};", value);
341 break;
342 case IR::Attribute::ClipDistance0: 301 case IR::Attribute::ClipDistance0:
343 case IR::Attribute::ClipDistance1: 302 case IR::Attribute::ClipDistance1:
344 case IR::Attribute::ClipDistance2: 303 case IR::Attribute::ClipDistance2:
diff --git a/src/shader_recompiler/backend/glsl/emit_glsl_floating_point.cpp b/src/shader_recompiler/backend/glsl/emit_glsl_floating_point.cpp
index b765a251b..474189d87 100644
--- a/src/shader_recompiler/backend/glsl/emit_glsl_floating_point.cpp
+++ b/src/shader_recompiler/backend/glsl/emit_glsl_floating_point.cpp
@@ -125,11 +125,11 @@ void EmitFPNeg16([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] IR::Inst& i
125} 125}
126 126
127void EmitFPNeg32(EmitContext& ctx, IR::Inst& inst, std::string_view value) { 127void EmitFPNeg32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
128 ctx.AddF32("{}=-({});", inst, value); 128 ctx.AddF32("{}=0.f-({});", inst, value);
129} 129}
130 130
131void EmitFPNeg64(EmitContext& ctx, IR::Inst& inst, std::string_view value) { 131void EmitFPNeg64(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
132 ctx.AddF64("{}=-({});", inst, value); 132 ctx.AddF64("{}=double(0.)-({});", inst, value);
133} 133}
134 134
135void EmitFPSin(EmitContext& ctx, IR::Inst& inst, std::string_view value) { 135void EmitFPSin(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
diff --git a/src/shader_recompiler/backend/glsl/emit_glsl_instructions.h b/src/shader_recompiler/backend/glsl/emit_glsl_instructions.h
index f86502e4c..6cabbc717 100644
--- a/src/shader_recompiler/backend/glsl/emit_glsl_instructions.h
+++ b/src/shader_recompiler/backend/glsl/emit_glsl_instructions.h
@@ -60,6 +60,8 @@ void EmitGetCbufU32x2(EmitContext& ctx, IR::Inst& inst, const IR::Value& binding
60 const IR::Value& offset); 60 const IR::Value& offset);
61void EmitGetAttribute(EmitContext& ctx, IR::Inst& inst, IR::Attribute attr, 61void EmitGetAttribute(EmitContext& ctx, IR::Inst& inst, IR::Attribute attr,
62 std::string_view vertex); 62 std::string_view vertex);
63void EmitGetAttributeU32(EmitContext& ctx, IR::Inst& inst, IR::Attribute attr,
64 std::string_view vertex);
63void EmitSetAttribute(EmitContext& ctx, IR::Attribute attr, std::string_view value, 65void EmitSetAttribute(EmitContext& ctx, IR::Attribute attr, std::string_view value,
64 std::string_view vertex); 66 std::string_view vertex);
65void EmitGetAttributeIndexed(EmitContext& ctx, IR::Inst& inst, std::string_view offset, 67void EmitGetAttributeIndexed(EmitContext& ctx, IR::Inst& inst, std::string_view offset,
diff --git a/src/shader_recompiler/backend/glsl/emit_glsl_integer.cpp b/src/shader_recompiler/backend/glsl/emit_glsl_integer.cpp
index 44060df33..b0d85be99 100644
--- a/src/shader_recompiler/backend/glsl/emit_glsl_integer.cpp
+++ b/src/shader_recompiler/backend/glsl/emit_glsl_integer.cpp
@@ -87,11 +87,11 @@ void EmitUDiv32(EmitContext& ctx, IR::Inst& inst, std::string_view a, std::strin
87} 87}
88 88
89void EmitINeg32(EmitContext& ctx, IR::Inst& inst, std::string_view value) { 89void EmitINeg32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
90 ctx.AddU32("{}=uint(-({}));", inst, value); 90 ctx.AddU32("{}=uint(int(0)-int({}));", inst, value);
91} 91}
92 92
93void EmitINeg64(EmitContext& ctx, IR::Inst& inst, std::string_view value) { 93void EmitINeg64(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
94 ctx.AddU64("{}=-({});", inst, value); 94 ctx.AddU64("{}=uint64_t(int64_t(0)-int64_t({}));", inst, value);
95} 95}
96 96
97void EmitIAbs32(EmitContext& ctx, IR::Inst& inst, std::string_view value) { 97void EmitIAbs32(EmitContext& ctx, IR::Inst& inst, std::string_view value) {
diff --git a/src/shader_recompiler/backend/glsl/emit_glsl_special.cpp b/src/shader_recompiler/backend/glsl/emit_glsl_special.cpp
index b8ddafe48..fcf620b79 100644
--- a/src/shader_recompiler/backend/glsl/emit_glsl_special.cpp
+++ b/src/shader_recompiler/backend/glsl/emit_glsl_special.cpp
@@ -90,7 +90,9 @@ void EmitPhiMove(EmitContext& ctx, const IR::Value& phi_value, const IR::Value&
90 if (phi_reg == val_reg) { 90 if (phi_reg == val_reg) {
91 return; 91 return;
92 } 92 }
93 ctx.Add("{}={};", phi_reg, val_reg); 93 const bool needs_workaround{ctx.profile.has_gl_bool_ref_bug && phi_type == IR::Type::U1};
94 const auto suffix{needs_workaround ? "?true:false" : ""};
95 ctx.Add("{}={}{};", phi_reg, val_reg, suffix);
94} 96}
95 97
96void EmitPrologue(EmitContext& ctx) { 98void EmitPrologue(EmitContext& ctx) {
diff --git a/src/shader_recompiler/backend/glsl/glsl_emit_context.cpp b/src/shader_recompiler/backend/glsl/glsl_emit_context.cpp
index 1de017e76..bb7f1a0fd 100644
--- a/src/shader_recompiler/backend/glsl/glsl_emit_context.cpp
+++ b/src/shader_recompiler/backend/glsl/glsl_emit_context.cpp
@@ -211,27 +211,6 @@ std::string_view OutputPrimitive(OutputTopology topology) {
211 throw InvalidArgument("Invalid output topology {}", topology); 211 throw InvalidArgument("Invalid output topology {}", topology);
212} 212}
213 213
214void SetupLegacyOutPerVertex(EmitContext& ctx, std::string& header) {
215 if (!ctx.info.stores.Legacy()) {
216 return;
217 }
218 if (ctx.info.stores.FixedFunctionTexture()) {
219 header += "vec4 gl_TexCoord[8];";
220 }
221 if (ctx.info.stores.AnyComponent(IR::Attribute::ColorFrontDiffuseR)) {
222 header += "vec4 gl_FrontColor;";
223 }
224 if (ctx.info.stores.AnyComponent(IR::Attribute::ColorFrontSpecularR)) {
225 header += "vec4 gl_FrontSecondaryColor;";
226 }
227 if (ctx.info.stores.AnyComponent(IR::Attribute::ColorBackDiffuseR)) {
228 header += "vec4 gl_BackColor;";
229 }
230 if (ctx.info.stores.AnyComponent(IR::Attribute::ColorBackSpecularR)) {
231 header += "vec4 gl_BackSecondaryColor;";
232 }
233}
234
235void SetupOutPerVertex(EmitContext& ctx, std::string& header) { 214void SetupOutPerVertex(EmitContext& ctx, std::string& header) {
236 if (!StoresPerVertexAttributes(ctx.stage)) { 215 if (!StoresPerVertexAttributes(ctx.stage)) {
237 return; 216 return;
@@ -250,7 +229,6 @@ void SetupOutPerVertex(EmitContext& ctx, std::string& header) {
250 ctx.profile.support_viewport_index_layer_non_geometry && ctx.stage != Stage::Geometry) { 229 ctx.profile.support_viewport_index_layer_non_geometry && ctx.stage != Stage::Geometry) {
251 header += "int gl_ViewportIndex;"; 230 header += "int gl_ViewportIndex;";
252 } 231 }
253 SetupLegacyOutPerVertex(ctx, header);
254 header += "};"; 232 header += "};";
255 if (ctx.info.stores[IR::Attribute::ViewportIndex] && ctx.stage == Stage::Geometry) { 233 if (ctx.info.stores[IR::Attribute::ViewportIndex] && ctx.stage == Stage::Geometry) {
256 header += "out int gl_ViewportIndex;"; 234 header += "out int gl_ViewportIndex;";
@@ -282,21 +260,6 @@ void SetupInPerVertex(EmitContext& ctx, std::string& header) {
282 } 260 }
283 header += "}gl_in[gl_MaxPatchVertices];"; 261 header += "}gl_in[gl_MaxPatchVertices];";
284} 262}
285
286void SetupLegacyInPerFragment(EmitContext& ctx, std::string& header) {
287 if (!ctx.info.loads.Legacy()) {
288 return;
289 }
290 header += "in gl_PerFragment{";
291 if (ctx.info.loads.FixedFunctionTexture()) {
292 header += "vec4 gl_TexCoord[8];";
293 }
294 if (ctx.info.loads.AnyComponent(IR::Attribute::ColorFrontDiffuseR)) {
295 header += "vec4 gl_Color;";
296 }
297 header += "};";
298}
299
300} // Anonymous namespace 263} // Anonymous namespace
301 264
302EmitContext::EmitContext(IR::Program& program, Bindings& bindings, const Profile& profile_, 265EmitContext::EmitContext(IR::Program& program, Bindings& bindings, const Profile& profile_,
@@ -361,7 +324,6 @@ EmitContext::EmitContext(IR::Program& program, Bindings& bindings, const Profile
361 } 324 }
362 SetupOutPerVertex(*this, header); 325 SetupOutPerVertex(*this, header);
363 SetupInPerVertex(*this, header); 326 SetupInPerVertex(*this, header);
364 SetupLegacyInPerFragment(*this, header);
365 327
366 for (size_t index = 0; index < IR::NUM_GENERICS; ++index) { 328 for (size_t index = 0; index < IR::NUM_GENERICS; ++index) {
367 if (!info.loads.Generic(index) || !runtime_info.previous_stage_stores.Generic(index)) { 329 if (!info.loads.Generic(index) || !runtime_info.previous_stage_stores.Generic(index)) {
@@ -466,9 +428,10 @@ void EmitContext::DefineConstantBuffers(Bindings& bindings) {
466 return; 428 return;
467 } 429 }
468 for (const auto& desc : info.constant_buffer_descriptors) { 430 for (const auto& desc : info.constant_buffer_descriptors) {
469 header += fmt::format( 431 const auto cbuf_type{profile.has_gl_cbuf_ftou_bug ? "uvec4" : "vec4"};
470 "layout(std140,binding={}) uniform {}_cbuf_{}{{vec4 {}_cbuf{}[{}];}};", 432 header += fmt::format("layout(std140,binding={}) uniform {}_cbuf_{}{{{} {}_cbuf{}[{}];}};",
471 bindings.uniform_buffer, stage_name, desc.index, stage_name, desc.index, 4 * 1024); 433 bindings.uniform_buffer, stage_name, desc.index, cbuf_type,
434 stage_name, desc.index, 4 * 1024);
472 bindings.uniform_buffer += desc.count; 435 bindings.uniform_buffer += desc.count;
473 } 436 }
474} 437}
diff --git a/src/shader_recompiler/backend/spirv/emit_spirv.cpp b/src/shader_recompiler/backend/spirv/emit_spirv.cpp
index 6ce7ed12a..50918317f 100644
--- a/src/shader_recompiler/backend/spirv/emit_spirv.cpp
+++ b/src/shader_recompiler/backend/spirv/emit_spirv.cpp
@@ -30,11 +30,20 @@ struct FuncTraits<ReturnType_ (*)(Args...)> {
30 using ArgType = std::tuple_element_t<I, std::tuple<Args...>>; 30 using ArgType = std::tuple_element_t<I, std::tuple<Args...>>;
31}; 31};
32 32
33#ifdef _MSC_VER
34#pragma warning(push)
35#pragma warning(disable : 4702) // Ignore unreachable code warning
36#endif
37
33template <auto func, typename... Args> 38template <auto func, typename... Args>
34void SetDefinition(EmitContext& ctx, IR::Inst* inst, Args... args) { 39void SetDefinition(EmitContext& ctx, IR::Inst* inst, Args... args) {
35 inst->SetDefinition<Id>(func(ctx, std::forward<Args>(args)...)); 40 inst->SetDefinition<Id>(func(ctx, std::forward<Args>(args)...));
36} 41}
37 42
43#ifdef _MSC_VER
44#pragma warning(pop)
45#endif
46
38template <typename ArgType> 47template <typename ArgType>
39ArgType Arg(EmitContext& ctx, const IR::Value& arg) { 48ArgType Arg(EmitContext& ctx, const IR::Value& arg) {
40 if constexpr (std::is_same_v<ArgType, Id>) { 49 if constexpr (std::is_same_v<ArgType, Id>) {
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 ad84966b5..8ea730c80 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
@@ -44,14 +44,6 @@ Id AttrPointer(EmitContext& ctx, Id pointer_type, Id vertex, Id base, Args&&...
44 } 44 }
45} 45}
46 46
47bool IsLegacyAttribute(IR::Attribute attribute) {
48 return (attribute >= IR::Attribute::ColorFrontDiffuseR &&
49 attribute <= IR::Attribute::ColorBackSpecularA) ||
50 attribute == IR::Attribute::FogCoordinate ||
51 (attribute >= IR::Attribute::FixedFncTexture0S &&
52 attribute <= IR::Attribute::FixedFncTexture9Q);
53}
54
55template <typename... Args> 47template <typename... Args>
56Id OutputAccessChain(EmitContext& ctx, Id result_type, Id base, Args&&... args) { 48Id OutputAccessChain(EmitContext& ctx, Id result_type, Id base, Args&&... args) {
57 if (ctx.stage == Stage::TessellationControl) { 49 if (ctx.stage == Stage::TessellationControl) {
@@ -83,17 +75,6 @@ std::optional<OutAttr> OutputAttrPointer(EmitContext& ctx, IR::Attribute attr) {
83 return OutputAccessChain(ctx, ctx.output_f32, info.id, index_id); 75 return OutputAccessChain(ctx, ctx.output_f32, info.id, index_id);
84 } 76 }
85 } 77 }
86 if (IsLegacyAttribute(attr)) {
87 if (attr == IR::Attribute::FogCoordinate) {
88 return OutputAccessChain(ctx, ctx.output_f32, ctx.OutputLegacyAttribute(attr),
89 ctx.Const(0u));
90 } else {
91 const u32 element{static_cast<u32>(attr) % 4};
92 const Id element_id{ctx.Const(element)};
93 return OutputAccessChain(ctx, ctx.output_f32, ctx.OutputLegacyAttribute(attr),
94 element_id);
95 }
96 }
97 switch (attr) { 78 switch (attr) {
98 case IR::Attribute::PointSize: 79 case IR::Attribute::PointSize:
99 return ctx.output_point_size; 80 return ctx.output_point_size;
@@ -327,18 +308,6 @@ Id EmitGetAttribute(EmitContext& ctx, IR::Attribute attr, Id vertex) {
327 const Id value{ctx.OpLoad(type->id, pointer)}; 308 const Id value{ctx.OpLoad(type->id, pointer)};
328 return type->needs_cast ? ctx.OpBitcast(ctx.F32[1], value) : value; 309 return type->needs_cast ? ctx.OpBitcast(ctx.F32[1], value) : value;
329 } 310 }
330 if (IsLegacyAttribute(attr)) {
331 if (attr == IR::Attribute::FogCoordinate) {
332 const Id attr_ptr{AttrPointer(ctx, ctx.input_f32, vertex,
333 ctx.InputLegacyAttribute(attr), ctx.Const(0u))};
334 return ctx.OpLoad(ctx.F32[1], attr_ptr);
335 } else {
336 const Id element_id{ctx.Const(element)};
337 const Id attr_ptr{AttrPointer(ctx, ctx.input_f32, vertex,
338 ctx.InputLegacyAttribute(attr), element_id)};
339 return ctx.OpLoad(ctx.F32[1], attr_ptr);
340 }
341 }
342 switch (attr) { 311 switch (attr) {
343 case IR::Attribute::PrimitiveId: 312 case IR::Attribute::PrimitiveId:
344 return ctx.OpBitcast(ctx.F32[1], ctx.OpLoad(ctx.U32[1], ctx.primitive_id)); 313 return ctx.OpBitcast(ctx.F32[1], ctx.OpLoad(ctx.U32[1], ctx.primitive_id));
@@ -386,6 +355,31 @@ Id EmitGetAttribute(EmitContext& ctx, IR::Attribute attr, Id vertex) {
386 } 355 }
387} 356}
388 357
358Id EmitGetAttributeU32(EmitContext& ctx, IR::Attribute attr, Id) {
359 switch (attr) {
360 case IR::Attribute::PrimitiveId:
361 return ctx.OpLoad(ctx.U32[1], ctx.primitive_id);
362 case IR::Attribute::InstanceId:
363 if (ctx.profile.support_vertex_instance_id) {
364 return ctx.OpLoad(ctx.U32[1], ctx.instance_id);
365 } else {
366 const Id index{ctx.OpLoad(ctx.U32[1], ctx.instance_index)};
367 const Id base{ctx.OpLoad(ctx.U32[1], ctx.base_instance)};
368 return ctx.OpISub(ctx.U32[1], index, base);
369 }
370 case IR::Attribute::VertexId:
371 if (ctx.profile.support_vertex_instance_id) {
372 return ctx.OpLoad(ctx.U32[1], ctx.vertex_id);
373 } else {
374 const Id index{ctx.OpLoad(ctx.U32[1], ctx.vertex_index)};
375 const Id base{ctx.OpLoad(ctx.U32[1], ctx.base_vertex)};
376 return ctx.OpISub(ctx.U32[1], index, base);
377 }
378 default:
379 throw NotImplementedException("Read U32 attribute {}", attr);
380 }
381}
382
389void EmitSetAttribute(EmitContext& ctx, IR::Attribute attr, Id value, [[maybe_unused]] Id vertex) { 383void EmitSetAttribute(EmitContext& ctx, IR::Attribute attr, Id value, [[maybe_unused]] Id vertex) {
390 const std::optional<OutAttr> output{OutputAttrPointer(ctx, attr)}; 384 const std::optional<OutAttr> output{OutputAttrPointer(ctx, attr)};
391 if (!output) { 385 if (!output) {
diff --git a/src/shader_recompiler/backend/spirv/emit_spirv_instructions.h b/src/shader_recompiler/backend/spirv/emit_spirv_instructions.h
index 6cd22dd3e..887112deb 100644
--- a/src/shader_recompiler/backend/spirv/emit_spirv_instructions.h
+++ b/src/shader_recompiler/backend/spirv/emit_spirv_instructions.h
@@ -53,6 +53,7 @@ Id EmitGetCbufU32(EmitContext& ctx, const IR::Value& binding, const IR::Value& o
53Id EmitGetCbufF32(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset); 53Id EmitGetCbufF32(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset);
54Id EmitGetCbufU32x2(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset); 54Id EmitGetCbufU32x2(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset);
55Id EmitGetAttribute(EmitContext& ctx, IR::Attribute attr, Id vertex); 55Id EmitGetAttribute(EmitContext& ctx, IR::Attribute attr, Id vertex);
56Id EmitGetAttributeU32(EmitContext& ctx, IR::Attribute attr, Id vertex);
56void EmitSetAttribute(EmitContext& ctx, IR::Attribute attr, Id value, Id vertex); 57void EmitSetAttribute(EmitContext& ctx, IR::Attribute attr, Id value, Id vertex);
57Id EmitGetAttributeIndexed(EmitContext& ctx, Id offset, Id vertex); 58Id EmitGetAttributeIndexed(EmitContext& ctx, Id offset, Id vertex);
58void EmitSetAttributeIndexed(EmitContext& ctx, Id offset, Id value, Id vertex); 59void EmitSetAttributeIndexed(EmitContext& ctx, Id offset, Id value, Id vertex);
diff --git a/src/shader_recompiler/backend/spirv/spirv_emit_context.cpp b/src/shader_recompiler/backend/spirv/spirv_emit_context.cpp
index 4b6f792bf..d3ba66569 100644
--- a/src/shader_recompiler/backend/spirv/spirv_emit_context.cpp
+++ b/src/shader_recompiler/backend/spirv/spirv_emit_context.cpp
@@ -18,8 +18,6 @@
18 18
19namespace Shader::Backend::SPIRV { 19namespace Shader::Backend::SPIRV {
20namespace { 20namespace {
21constexpr size_t NUM_FIXEDFNCTEXTURE = 10;
22
23enum class Operation { 21enum class Operation {
24 Increment, 22 Increment,
25 Decrement, 23 Decrement,
@@ -432,34 +430,6 @@ Id DescType(EmitContext& ctx, Id sampled_type, Id pointer_type, u32 count) {
432 return pointer_type; 430 return pointer_type;
433 } 431 }
434} 432}
435
436size_t FindAndSetNextUnusedLocation(std::bitset<IR::NUM_GENERICS>& used_locations,
437 size_t& start_offset) {
438 for (size_t location = start_offset; location < used_locations.size(); ++location) {
439 if (!used_locations.test(location)) {
440 start_offset = location;
441 used_locations.set(location);
442 return location;
443 }
444 }
445 throw RuntimeError("Unable to get an unused location for legacy attribute");
446}
447
448Id DefineLegacyInput(EmitContext& ctx, std::bitset<IR::NUM_GENERICS>& used_locations,
449 size_t& start_offset) {
450 const Id id{DefineInput(ctx, ctx.F32[4], true)};
451 const size_t location = FindAndSetNextUnusedLocation(used_locations, start_offset);
452 ctx.Decorate(id, spv::Decoration::Location, location);
453 return id;
454}
455
456Id DefineLegacyOutput(EmitContext& ctx, std::bitset<IR::NUM_GENERICS>& used_locations,
457 size_t& start_offset, std::optional<u32> invocations) {
458 const Id id{DefineOutput(ctx, ctx.F32[4], invocations)};
459 const size_t location = FindAndSetNextUnusedLocation(used_locations, start_offset);
460 ctx.Decorate(id, spv::Decoration::Location, location);
461 return id;
462}
463} // Anonymous namespace 433} // Anonymous namespace
464 434
465void VectorTypes::Define(Sirit::Module& sirit_ctx, Id base_type, std::string_view name) { 435void VectorTypes::Define(Sirit::Module& sirit_ctx, Id base_type, std::string_view name) {
@@ -543,64 +513,6 @@ Id EmitContext::BitOffset16(const IR::Value& offset) {
543 return OpBitwiseAnd(U32[1], OpShiftLeftLogical(U32[1], Def(offset), Const(3u)), Const(16u)); 513 return OpBitwiseAnd(U32[1], OpShiftLeftLogical(U32[1], Def(offset), Const(3u)), Const(16u));
544} 514}
545 515
546Id EmitContext::InputLegacyAttribute(IR::Attribute attribute) {
547 if (attribute >= IR::Attribute::ColorFrontDiffuseR &&
548 attribute <= IR::Attribute::ColorFrontDiffuseA) {
549 return input_front_color;
550 }
551 if (attribute >= IR::Attribute::ColorFrontSpecularR &&
552 attribute <= IR::Attribute::ColorFrontSpecularA) {
553 return input_front_secondary_color;
554 }
555 if (attribute >= IR::Attribute::ColorBackDiffuseR &&
556 attribute <= IR::Attribute::ColorBackDiffuseA) {
557 return input_back_color;
558 }
559 if (attribute >= IR::Attribute::ColorBackSpecularR &&
560 attribute <= IR::Attribute::ColorBackSpecularA) {
561 return input_back_secondary_color;
562 }
563 if (attribute == IR::Attribute::FogCoordinate) {
564 return input_fog_frag_coord;
565 }
566 if (attribute >= IR::Attribute::FixedFncTexture0S &&
567 attribute <= IR::Attribute::FixedFncTexture9Q) {
568 u32 index =
569 (static_cast<u32>(attribute) - static_cast<u32>(IR::Attribute::FixedFncTexture0S)) / 4;
570 return input_fixed_fnc_textures[index];
571 }
572 throw InvalidArgument("Attribute is not legacy attribute {}", attribute);
573}
574
575Id EmitContext::OutputLegacyAttribute(IR::Attribute attribute) {
576 if (attribute >= IR::Attribute::ColorFrontDiffuseR &&
577 attribute <= IR::Attribute::ColorFrontDiffuseA) {
578 return output_front_color;
579 }
580 if (attribute >= IR::Attribute::ColorFrontSpecularR &&
581 attribute <= IR::Attribute::ColorFrontSpecularA) {
582 return output_front_secondary_color;
583 }
584 if (attribute >= IR::Attribute::ColorBackDiffuseR &&
585 attribute <= IR::Attribute::ColorBackDiffuseA) {
586 return output_back_color;
587 }
588 if (attribute >= IR::Attribute::ColorBackSpecularR &&
589 attribute <= IR::Attribute::ColorBackSpecularA) {
590 return output_back_secondary_color;
591 }
592 if (attribute == IR::Attribute::FogCoordinate) {
593 return output_fog_frag_coord;
594 }
595 if (attribute >= IR::Attribute::FixedFncTexture0S &&
596 attribute <= IR::Attribute::FixedFncTexture9Q) {
597 u32 index =
598 (static_cast<u32>(attribute) - static_cast<u32>(IR::Attribute::FixedFncTexture0S)) / 4;
599 return output_fixed_fnc_textures[index];
600 }
601 throw InvalidArgument("Attribute is not legacy attribute {}", attribute);
602}
603
604void EmitContext::DefineCommonTypes(const Info& info) { 516void EmitContext::DefineCommonTypes(const Info& info) {
605 void_id = TypeVoid(); 517 void_id = TypeVoid();
606 518
@@ -1389,7 +1301,6 @@ void EmitContext::DefineInputs(const IR::Program& program) {
1389 loads[IR::Attribute::TessellationEvaluationPointV]) { 1301 loads[IR::Attribute::TessellationEvaluationPointV]) {
1390 tess_coord = DefineInput(*this, F32[3], false, spv::BuiltIn::TessCoord); 1302 tess_coord = DefineInput(*this, F32[3], false, spv::BuiltIn::TessCoord);
1391 } 1303 }
1392 std::bitset<IR::NUM_GENERICS> used_locations{};
1393 for (size_t index = 0; index < IR::NUM_GENERICS; ++index) { 1304 for (size_t index = 0; index < IR::NUM_GENERICS; ++index) {
1394 const AttributeType input_type{runtime_info.generic_input_types[index]}; 1305 const AttributeType input_type{runtime_info.generic_input_types[index]};
1395 if (!runtime_info.previous_stage_stores.Generic(index)) { 1306 if (!runtime_info.previous_stage_stores.Generic(index)) {
@@ -1401,7 +1312,6 @@ void EmitContext::DefineInputs(const IR::Program& program) {
1401 if (input_type == AttributeType::Disabled) { 1312 if (input_type == AttributeType::Disabled) {
1402 continue; 1313 continue;
1403 } 1314 }
1404 used_locations.set(index);
1405 const Id type{GetAttributeType(*this, input_type)}; 1315 const Id type{GetAttributeType(*this, input_type)};
1406 const Id id{DefineInput(*this, type, true)}; 1316 const Id id{DefineInput(*this, type, true)};
1407 Decorate(id, spv::Decoration::Location, static_cast<u32>(index)); 1317 Decorate(id, spv::Decoration::Location, static_cast<u32>(index));
@@ -1427,30 +1337,6 @@ void EmitContext::DefineInputs(const IR::Program& program) {
1427 break; 1337 break;
1428 } 1338 }
1429 } 1339 }
1430 size_t previous_unused_location = 0;
1431 if (loads.AnyComponent(IR::Attribute::ColorFrontDiffuseR)) {
1432 input_front_color = DefineLegacyInput(*this, used_locations, previous_unused_location);
1433 }
1434 if (loads.AnyComponent(IR::Attribute::ColorFrontSpecularR)) {
1435 input_front_secondary_color =
1436 DefineLegacyInput(*this, used_locations, previous_unused_location);
1437 }
1438 if (loads.AnyComponent(IR::Attribute::ColorBackDiffuseR)) {
1439 input_back_color = DefineLegacyInput(*this, used_locations, previous_unused_location);
1440 }
1441 if (loads.AnyComponent(IR::Attribute::ColorBackSpecularR)) {
1442 input_back_secondary_color =
1443 DefineLegacyInput(*this, used_locations, previous_unused_location);
1444 }
1445 if (loads.AnyComponent(IR::Attribute::FogCoordinate)) {
1446 input_fog_frag_coord = DefineLegacyInput(*this, used_locations, previous_unused_location);
1447 }
1448 for (size_t index = 0; index < NUM_FIXEDFNCTEXTURE; ++index) {
1449 if (loads.AnyComponent(IR::Attribute::FixedFncTexture0S + index * 4)) {
1450 input_fixed_fnc_textures[index] =
1451 DefineLegacyInput(*this, used_locations, previous_unused_location);
1452 }
1453 }
1454 if (stage == Stage::TessellationEval) { 1340 if (stage == Stage::TessellationEval) {
1455 for (size_t index = 0; index < info.uses_patches.size(); ++index) { 1341 for (size_t index = 0; index < info.uses_patches.size(); ++index) {
1456 if (!info.uses_patches[index]) { 1342 if (!info.uses_patches[index]) {
@@ -1501,38 +1387,9 @@ void EmitContext::DefineOutputs(const IR::Program& program) {
1501 viewport_mask = DefineOutput(*this, TypeArray(U32[1], Const(1u)), std::nullopt, 1387 viewport_mask = DefineOutput(*this, TypeArray(U32[1], Const(1u)), std::nullopt,
1502 spv::BuiltIn::ViewportMaskNV); 1388 spv::BuiltIn::ViewportMaskNV);
1503 } 1389 }
1504 std::bitset<IR::NUM_GENERICS> used_locations{};
1505 for (size_t index = 0; index < IR::NUM_GENERICS; ++index) { 1390 for (size_t index = 0; index < IR::NUM_GENERICS; ++index) {
1506 if (info.stores.Generic(index)) { 1391 if (info.stores.Generic(index)) {
1507 DefineGenericOutput(*this, index, invocations); 1392 DefineGenericOutput(*this, index, invocations);
1508 used_locations.set(index);
1509 }
1510 }
1511 size_t previous_unused_location = 0;
1512 if (info.stores.AnyComponent(IR::Attribute::ColorFrontDiffuseR)) {
1513 output_front_color =
1514 DefineLegacyOutput(*this, used_locations, previous_unused_location, invocations);
1515 }
1516 if (info.stores.AnyComponent(IR::Attribute::ColorFrontSpecularR)) {
1517 output_front_secondary_color =
1518 DefineLegacyOutput(*this, used_locations, previous_unused_location, invocations);
1519 }
1520 if (info.stores.AnyComponent(IR::Attribute::ColorBackDiffuseR)) {
1521 output_back_color =
1522 DefineLegacyOutput(*this, used_locations, previous_unused_location, invocations);
1523 }
1524 if (info.stores.AnyComponent(IR::Attribute::ColorBackSpecularR)) {
1525 output_back_secondary_color =
1526 DefineLegacyOutput(*this, used_locations, previous_unused_location, invocations);
1527 }
1528 if (info.stores.AnyComponent(IR::Attribute::FogCoordinate)) {
1529 output_fog_frag_coord =
1530 DefineLegacyOutput(*this, used_locations, previous_unused_location, invocations);
1531 }
1532 for (size_t index = 0; index < NUM_FIXEDFNCTEXTURE; ++index) {
1533 if (info.stores.AnyComponent(IR::Attribute::FixedFncTexture0S + index * 4)) {
1534 output_fixed_fnc_textures[index] =
1535 DefineLegacyOutput(*this, used_locations, previous_unused_location, invocations);
1536 } 1393 }
1537 } 1394 }
1538 switch (stage) { 1395 switch (stage) {
diff --git a/src/shader_recompiler/backend/spirv/spirv_emit_context.h b/src/shader_recompiler/backend/spirv/spirv_emit_context.h
index 63f8185d9..f87138f7e 100644
--- a/src/shader_recompiler/backend/spirv/spirv_emit_context.h
+++ b/src/shader_recompiler/backend/spirv/spirv_emit_context.h
@@ -113,9 +113,6 @@ public:
113 [[nodiscard]] Id BitOffset8(const IR::Value& offset); 113 [[nodiscard]] Id BitOffset8(const IR::Value& offset);
114 [[nodiscard]] Id BitOffset16(const IR::Value& offset); 114 [[nodiscard]] Id BitOffset16(const IR::Value& offset);
115 115
116 Id InputLegacyAttribute(IR::Attribute attribute);
117 Id OutputLegacyAttribute(IR::Attribute attribute);
118
119 Id Const(u32 value) { 116 Id Const(u32 value) {
120 return Constant(U32[1], value); 117 return Constant(U32[1], value);
121 } 118 }
@@ -281,22 +278,10 @@ public:
281 Id write_global_func_u32x4{}; 278 Id write_global_func_u32x4{};
282 279
283 Id input_position{}; 280 Id input_position{};
284 Id input_front_color{};
285 Id input_front_secondary_color{};
286 Id input_back_color{};
287 Id input_back_secondary_color{};
288 Id input_fog_frag_coord{};
289 std::array<Id, 10> input_fixed_fnc_textures{};
290 std::array<Id, 32> input_generics{}; 281 std::array<Id, 32> input_generics{};
291 282
292 Id output_point_size{}; 283 Id output_point_size{};
293 Id output_position{}; 284 Id output_position{};
294 Id output_front_color{};
295 Id output_front_secondary_color{};
296 Id output_back_color{};
297 Id output_back_secondary_color{};
298 Id output_fog_frag_coord{};
299 std::array<Id, 10> output_fixed_fnc_textures{};
300 std::array<std::array<GenericElementInfo, 4>, 32> output_generics{}; 285 std::array<std::array<GenericElementInfo, 4>, 32> output_generics{};
301 286
302 Id output_tess_level_outer{}; 287 Id output_tess_level_outer{};