diff options
Diffstat (limited to 'src/shader_recompiler/backend')
4 files changed, 51 insertions, 19 deletions
diff --git a/src/shader_recompiler/backend/glsl/emit_context.cpp b/src/shader_recompiler/backend/glsl/emit_context.cpp index 0e8fe017d..d224c4d84 100644 --- a/src/shader_recompiler/backend/glsl/emit_context.cpp +++ b/src/shader_recompiler/backend/glsl/emit_context.cpp | |||
| @@ -148,6 +148,16 @@ std::string_view ImageFormatString(ImageFormat format) { | |||
| 148 | } | 148 | } |
| 149 | } | 149 | } |
| 150 | 150 | ||
| 151 | std::string_view ImageAccessQualifier(bool is_written, bool is_read) { | ||
| 152 | if (is_written && !is_read) { | ||
| 153 | return "writeonly "; | ||
| 154 | } | ||
| 155 | if (is_read && !is_written) { | ||
| 156 | return "readonly "; | ||
| 157 | } | ||
| 158 | return ""; | ||
| 159 | } | ||
| 160 | |||
| 151 | std::string_view GetTessMode(TessPrimitive primitive) { | 161 | std::string_view GetTessMode(TessPrimitive primitive) { |
| 152 | switch (primitive) { | 162 | switch (primitive) { |
| 153 | case TessPrimitive::Triangles: | 163 | case TessPrimitive::Triangles: |
| @@ -262,7 +272,9 @@ void SetupLegacyInPerFragment(EmitContext& ctx, std::string& header) { | |||
| 262 | EmitContext::EmitContext(IR::Program& program, Bindings& bindings, const Profile& profile_, | 272 | EmitContext::EmitContext(IR::Program& program, Bindings& bindings, const Profile& profile_, |
| 263 | const RuntimeInfo& runtime_info_) | 273 | const RuntimeInfo& runtime_info_) |
| 264 | : info{program.info}, profile{profile_}, runtime_info{runtime_info_} { | 274 | : info{program.info}, profile{profile_}, runtime_info{runtime_info_} { |
| 265 | header += "#pragma optionNV(fastmath off)\n"; | 275 | if (profile.need_fastmath_off) { |
| 276 | header += "#pragma optionNV(fastmath off)\n"; | ||
| 277 | } | ||
| 266 | SetupExtensions(); | 278 | SetupExtensions(); |
| 267 | stage = program.stage; | 279 | stage = program.stage; |
| 268 | switch (program.stage) { | 280 | switch (program.stage) { |
| @@ -335,7 +347,7 @@ EmitContext::EmitContext(IR::Program& program, Bindings& bindings, const Profile | |||
| 335 | } | 347 | } |
| 336 | for (size_t index = 0; index < info.stores_generics.size(); ++index) { | 348 | for (size_t index = 0; index < info.stores_generics.size(); ++index) { |
| 337 | // TODO: Properly resolve attribute issues | 349 | // TODO: Properly resolve attribute issues |
| 338 | if (info.stores_generics[index] || stage == Stage::VertexA || stage == Stage::VertexB) { | 350 | if (info.stores_generics[index] || StageInitializesVaryings()) { |
| 339 | DefineGenericOutput(index, program.invocations); | 351 | DefineGenericOutput(index, program.invocations); |
| 340 | } | 352 | } |
| 341 | } | 353 | } |
| @@ -347,6 +359,17 @@ EmitContext::EmitContext(IR::Program& program, Bindings& bindings, const Profile | |||
| 347 | DefineConstants(); | 359 | DefineConstants(); |
| 348 | } | 360 | } |
| 349 | 361 | ||
| 362 | bool EmitContext::StageInitializesVaryings() const noexcept { | ||
| 363 | switch (stage) { | ||
| 364 | case Stage::VertexA: | ||
| 365 | case Stage::VertexB: | ||
| 366 | case Stage::Geometry: | ||
| 367 | return true; | ||
| 368 | default: | ||
| 369 | return false; | ||
| 370 | } | ||
| 371 | } | ||
| 372 | |||
| 350 | void EmitContext::SetupExtensions() { | 373 | void EmitContext::SetupExtensions() { |
| 351 | if (info.uses_shadow_lod && profile.support_gl_texture_shadow_lod) { | 374 | if (info.uses_shadow_lod && profile.support_gl_texture_shadow_lod) { |
| 352 | header += "#extension GL_EXT_texture_shadow_lod : enable\n"; | 375 | header += "#extension GL_EXT_texture_shadow_lod : enable\n"; |
| @@ -361,7 +384,7 @@ void EmitContext::SetupExtensions() { | |||
| 361 | header += "#extension GL_NV_shader_atomic_float : enable\n"; | 384 | header += "#extension GL_NV_shader_atomic_float : enable\n"; |
| 362 | } | 385 | } |
| 363 | if (info.uses_atomic_f16x2_add || info.uses_atomic_f16x2_min || info.uses_atomic_f16x2_max) { | 386 | if (info.uses_atomic_f16x2_add || info.uses_atomic_f16x2_min || info.uses_atomic_f16x2_max) { |
| 364 | header += "#extension NV_shader_atomic_fp16_vector : enable\n"; | 387 | header += "#extension GL_NV_shader_atomic_fp16_vector : enable\n"; |
| 365 | } | 388 | } |
| 366 | if (info.uses_fp16) { | 389 | if (info.uses_fp16) { |
| 367 | if (profile.support_gl_nv_gpu_shader_5) { | 390 | if (profile.support_gl_nv_gpu_shader_5) { |
| @@ -392,7 +415,7 @@ void EmitContext::SetupExtensions() { | |||
| 392 | if (info.stores_viewport_mask && profile.support_viewport_mask) { | 415 | if (info.stores_viewport_mask && profile.support_viewport_mask) { |
| 393 | header += "#extension GL_NV_viewport_array2 : enable\n"; | 416 | header += "#extension GL_NV_viewport_array2 : enable\n"; |
| 394 | } | 417 | } |
| 395 | if (info.uses_typeless_image_reads || info.uses_typeless_image_writes) { | 418 | if (info.uses_typeless_image_reads) { |
| 396 | header += "#extension GL_EXT_shader_image_load_formatted : enable\n"; | 419 | header += "#extension GL_EXT_shader_image_load_formatted : enable\n"; |
| 397 | } | 420 | } |
| 398 | if (info.uses_derivatives && profile.support_gl_derivative_control) { | 421 | if (info.uses_derivatives && profile.support_gl_derivative_control) { |
| @@ -593,9 +616,9 @@ std::string EmitContext::DefineGlobalMemoryFunctions() { | |||
| 593 | "return uvec4({0}[uint(addr-{1})>>2],{0}[uint(addr-{1}+4)>>2],{0}[" | 616 | "return uvec4({0}[uint(addr-{1})>>2],{0}[uint(addr-{1}+4)>>2],{0}[" |
| 594 | "uint(addr-{1}+8)>>2],{0}[uint(addr-{1}+12)>>2]);}}"); | 617 | "uint(addr-{1}+8)>>2],{0}[uint(addr-{1}+12)>>2]);}}"); |
| 595 | } | 618 | } |
| 596 | write_func += "}"; | 619 | write_func += '}'; |
| 597 | write_func_64 += "}"; | 620 | write_func_64 += '}'; |
| 598 | write_func_128 += "}"; | 621 | write_func_128 += '}'; |
| 599 | load_func += "return 0u;}"; | 622 | load_func += "return 0u;}"; |
| 600 | load_func_64 += "return uvec2(0);}"; | 623 | load_func_64 += "return uvec2(0);}"; |
| 601 | load_func_128 += "return uvec4(0);}"; | 624 | load_func_128 += "return uvec4(0);}"; |
| @@ -607,9 +630,10 @@ void EmitContext::SetupImages(Bindings& bindings) { | |||
| 607 | for (const auto& desc : info.image_buffer_descriptors) { | 630 | for (const auto& desc : info.image_buffer_descriptors) { |
| 608 | image_buffers.push_back({bindings.image, desc.count}); | 631 | image_buffers.push_back({bindings.image, desc.count}); |
| 609 | const auto format{ImageFormatString(desc.format)}; | 632 | const auto format{ImageFormatString(desc.format)}; |
| 633 | const auto qualifier{ImageAccessQualifier(desc.is_written, desc.is_read)}; | ||
| 610 | const auto array_decorator{desc.count > 1 ? fmt::format("[{}]", desc.count) : ""}; | 634 | const auto array_decorator{desc.count > 1 ? fmt::format("[{}]", desc.count) : ""}; |
| 611 | header += fmt::format("layout(binding={}{}) uniform uimageBuffer img{}{};", bindings.image, | 635 | header += fmt::format("layout(binding={}{}) uniform {}uimageBuffer img{}{};", |
| 612 | format, bindings.image, array_decorator); | 636 | bindings.image, format, qualifier, bindings.image, array_decorator); |
| 613 | bindings.image += desc.count; | 637 | bindings.image += desc.count; |
| 614 | } | 638 | } |
| 615 | images.reserve(info.image_descriptors.size()); | 639 | images.reserve(info.image_descriptors.size()); |
| @@ -617,7 +641,7 @@ void EmitContext::SetupImages(Bindings& bindings) { | |||
| 617 | images.push_back({bindings.image, desc.count}); | 641 | images.push_back({bindings.image, desc.count}); |
| 618 | const auto format{ImageFormatString(desc.format)}; | 642 | const auto format{ImageFormatString(desc.format)}; |
| 619 | const auto image_type{ImageType(desc.type)}; | 643 | const auto image_type{ImageType(desc.type)}; |
| 620 | const auto qualifier{desc.is_written ? "" : "readonly "}; | 644 | const auto qualifier{ImageAccessQualifier(desc.is_written, desc.is_read)}; |
| 621 | const auto array_decorator{desc.count > 1 ? fmt::format("[{}]", desc.count) : ""}; | 645 | const auto array_decorator{desc.count > 1 ? fmt::format("[{}]", desc.count) : ""}; |
| 622 | header += fmt::format("layout(binding={}{})uniform {}{} img{}{};", bindings.image, format, | 646 | header += fmt::format("layout(binding={}{})uniform {}{} img{}{};", bindings.image, format, |
| 623 | qualifier, image_type, bindings.image, array_decorator); | 647 | qualifier, image_type, bindings.image, array_decorator); |
diff --git a/src/shader_recompiler/backend/glsl/emit_context.h b/src/shader_recompiler/backend/glsl/emit_context.h index 8fa87c02c..4a50556e1 100644 --- a/src/shader_recompiler/backend/glsl/emit_context.h +++ b/src/shader_recompiler/backend/glsl/emit_context.h | |||
| @@ -136,6 +136,8 @@ public: | |||
| 136 | code += '\n'; | 136 | code += '\n'; |
| 137 | } | 137 | } |
| 138 | 138 | ||
| 139 | [[nodiscard]] bool StageInitializesVaryings() const noexcept; | ||
| 140 | |||
| 139 | std::string header; | 141 | std::string header; |
| 140 | std::string code; | 142 | std::string code; |
| 141 | VarAlloc var_alloc; | 143 | VarAlloc var_alloc; |
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 edeecc26e..a241d18fe 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 | |||
| @@ -329,7 +329,7 @@ void EmitSetAttribute(EmitContext& ctx, IR::Attribute attr, std::string_view val | |||
| 329 | ctx.Add("gl_BackSecondaryColor.{}={};", swizzle, value); | 329 | ctx.Add("gl_BackSecondaryColor.{}={};", swizzle, value); |
| 330 | break; | 330 | break; |
| 331 | case IR::Attribute::FogCoordinate: | 331 | case IR::Attribute::FogCoordinate: |
| 332 | ctx.Add("gl_FogFragCoord.x={};", value); | 332 | ctx.Add("gl_FogFragCoord={};", value); |
| 333 | break; | 333 | break; |
| 334 | case IR::Attribute::ClipDistance0: | 334 | case IR::Attribute::ClipDistance0: |
| 335 | case IR::Attribute::ClipDistance1: | 335 | case IR::Attribute::ClipDistance1: |
diff --git a/src/shader_recompiler/backend/glsl/emit_glsl_special.cpp b/src/shader_recompiler/backend/glsl/emit_glsl_special.cpp index cfef58d79..59ca52f07 100644 --- a/src/shader_recompiler/backend/glsl/emit_glsl_special.cpp +++ b/src/shader_recompiler/backend/glsl/emit_glsl_special.cpp | |||
| @@ -10,6 +10,17 @@ | |||
| 10 | #include "shader_recompiler/frontend/ir/value.h" | 10 | #include "shader_recompiler/frontend/ir/value.h" |
| 11 | 11 | ||
| 12 | namespace Shader::Backend::GLSL { | 12 | namespace Shader::Backend::GLSL { |
| 13 | namespace { | ||
| 14 | void InitializeVaryings(EmitContext& ctx) { | ||
| 15 | ctx.Add("gl_Position=vec4(0,0,0,1);"); | ||
| 16 | // TODO: Properly resolve attribute issues | ||
| 17 | for (size_t index = 0; index < ctx.info.stores_generics.size() / 2; ++index) { | ||
| 18 | if (!ctx.info.stores_generics[index]) { | ||
| 19 | ctx.Add("out_attr{}=vec4(0,0,0,1);", index); | ||
| 20 | } | ||
| 21 | } | ||
| 22 | } | ||
| 23 | } // Anonymous namespace | ||
| 13 | 24 | ||
| 14 | void EmitPhi(EmitContext& ctx, IR::Inst& phi) { | 25 | void EmitPhi(EmitContext& ctx, IR::Inst& phi) { |
| 15 | const size_t num_args{phi.NumArgs()}; | 26 | const size_t num_args{phi.NumArgs()}; |
| @@ -44,14 +55,8 @@ void EmitPhiMove(EmitContext& ctx, const IR::Value& phi_value, const IR::Value& | |||
| 44 | } | 55 | } |
| 45 | 56 | ||
| 46 | void EmitPrologue(EmitContext& ctx) { | 57 | void EmitPrologue(EmitContext& ctx) { |
| 47 | if (ctx.stage == Stage::VertexA || ctx.stage == Stage::VertexB) { | 58 | if (ctx.StageInitializesVaryings()) { |
| 48 | ctx.Add("gl_Position=vec4(0.0f, 0.0f, 0.0f, 1.0f);"); | 59 | InitializeVaryings(ctx); |
| 49 | // TODO: Properly resolve attribute issues | ||
| 50 | for (size_t index = 0; index < ctx.info.stores_generics.size() / 2; ++index) { | ||
| 51 | if (!ctx.info.stores_generics[index]) { | ||
| 52 | ctx.Add("out_attr{}=vec4(0,0,0,1);", index); | ||
| 53 | } | ||
| 54 | } | ||
| 55 | } | 60 | } |
| 56 | } | 61 | } |
| 57 | 62 | ||
| @@ -59,6 +64,7 @@ void EmitEpilogue(EmitContext&) {} | |||
| 59 | 64 | ||
| 60 | void EmitEmitVertex(EmitContext& ctx, const IR::Value& stream) { | 65 | void EmitEmitVertex(EmitContext& ctx, const IR::Value& stream) { |
| 61 | ctx.Add("EmitStreamVertex(int({}));", ctx.var_alloc.Consume(stream)); | 66 | ctx.Add("EmitStreamVertex(int({}));", ctx.var_alloc.Consume(stream)); |
| 67 | InitializeVaryings(ctx); | ||
| 62 | } | 68 | } |
| 63 | 69 | ||
| 64 | void EmitEndPrimitive(EmitContext& ctx, const IR::Value& stream) { | 70 | void EmitEndPrimitive(EmitContext& ctx, const IR::Value& stream) { |