diff options
Diffstat (limited to 'src/shader_recompiler/backend/glsl/emit_glsl_special.cpp')
| -rw-r--r-- | src/shader_recompiler/backend/glsl/emit_glsl_special.cpp | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/src/shader_recompiler/backend/glsl/emit_glsl_special.cpp b/src/shader_recompiler/backend/glsl/emit_glsl_special.cpp new file mode 100644 index 000000000..9b866f889 --- /dev/null +++ b/src/shader_recompiler/backend/glsl/emit_glsl_special.cpp | |||
| @@ -0,0 +1,111 @@ | |||
| 1 | // Copyright 2021 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <string_view> | ||
| 6 | |||
| 7 | #include "shader_recompiler/backend/glsl/emit_context.h" | ||
| 8 | #include "shader_recompiler/backend/glsl/emit_glsl_instructions.h" | ||
| 9 | #include "shader_recompiler/frontend/ir/program.h" | ||
| 10 | #include "shader_recompiler/frontend/ir/value.h" | ||
| 11 | #include "shader_recompiler/profile.h" | ||
| 12 | |||
| 13 | namespace Shader::Backend::GLSL { | ||
| 14 | namespace { | ||
| 15 | std::string_view OutputVertexIndex(EmitContext& ctx) { | ||
| 16 | return ctx.stage == Stage::TessellationControl ? "[gl_InvocationID]" : ""; | ||
| 17 | } | ||
| 18 | |||
| 19 | void InitializeOutputVaryings(EmitContext& ctx) { | ||
| 20 | if (ctx.uses_geometry_passthrough) { | ||
| 21 | return; | ||
| 22 | } | ||
| 23 | if (ctx.stage == Stage::VertexB || ctx.stage == Stage::Geometry) { | ||
| 24 | ctx.Add("gl_Position=vec4(0,0,0,1);"); | ||
| 25 | } | ||
| 26 | for (size_t index = 0; index < IR::NUM_GENERICS; ++index) { | ||
| 27 | if (!ctx.info.stores.Generic(index)) { | ||
| 28 | continue; | ||
| 29 | } | ||
| 30 | const auto& info_array{ctx.output_generics.at(index)}; | ||
| 31 | const auto output_decorator{OutputVertexIndex(ctx)}; | ||
| 32 | size_t element{}; | ||
| 33 | while (element < info_array.size()) { | ||
| 34 | const auto& info{info_array.at(element)}; | ||
| 35 | const auto varying_name{fmt::format("{}{}", info.name, output_decorator)}; | ||
| 36 | switch (info.num_components) { | ||
| 37 | case 1: { | ||
| 38 | const char value{element == 3 ? '1' : '0'}; | ||
| 39 | ctx.Add("{}={}.f;", varying_name, value); | ||
| 40 | break; | ||
| 41 | } | ||
| 42 | case 2: | ||
| 43 | case 3: | ||
| 44 | if (element + info.num_components < 4) { | ||
| 45 | ctx.Add("{}=vec{}(0);", varying_name, info.num_components); | ||
| 46 | } else { | ||
| 47 | // last element is the w component, must be initialized to 1 | ||
| 48 | const auto zeros{info.num_components == 3 ? "0,0," : "0,"}; | ||
| 49 | ctx.Add("{}=vec{}({}1);", varying_name, info.num_components, zeros); | ||
| 50 | } | ||
| 51 | break; | ||
| 52 | case 4: | ||
| 53 | ctx.Add("{}=vec4(0,0,0,1);", varying_name); | ||
| 54 | break; | ||
| 55 | default: | ||
| 56 | break; | ||
| 57 | } | ||
| 58 | element += info.num_components; | ||
| 59 | } | ||
| 60 | } | ||
| 61 | } | ||
| 62 | } // Anonymous namespace | ||
| 63 | |||
| 64 | void EmitPhi(EmitContext& ctx, IR::Inst& phi) { | ||
| 65 | const size_t num_args{phi.NumArgs()}; | ||
| 66 | for (size_t i = 0; i < num_args; ++i) { | ||
| 67 | ctx.var_alloc.Consume(phi.Arg(i)); | ||
| 68 | } | ||
| 69 | if (!phi.Definition<Id>().is_valid) { | ||
| 70 | // The phi node wasn't forward defined | ||
| 71 | ctx.var_alloc.PhiDefine(phi, phi.Arg(0).Type()); | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | void EmitVoid(EmitContext&) {} | ||
| 76 | |||
| 77 | void EmitReference(EmitContext& ctx, const IR::Value& value) { | ||
| 78 | ctx.var_alloc.Consume(value); | ||
| 79 | } | ||
| 80 | |||
| 81 | void EmitPhiMove(EmitContext& ctx, const IR::Value& phi_value, const IR::Value& value) { | ||
| 82 | IR::Inst& phi{*phi_value.InstRecursive()}; | ||
| 83 | const auto phi_type{phi.Arg(0).Type()}; | ||
| 84 | if (!phi.Definition<Id>().is_valid) { | ||
| 85 | // The phi node wasn't forward defined | ||
| 86 | ctx.var_alloc.PhiDefine(phi, phi_type); | ||
| 87 | } | ||
| 88 | const auto phi_reg{ctx.var_alloc.Consume(IR::Value{&phi})}; | ||
| 89 | const auto val_reg{ctx.var_alloc.Consume(value)}; | ||
| 90 | if (phi_reg == val_reg) { | ||
| 91 | return; | ||
| 92 | } | ||
| 93 | ctx.Add("{}={};", phi_reg, val_reg); | ||
| 94 | } | ||
| 95 | |||
| 96 | void EmitPrologue(EmitContext& ctx) { | ||
| 97 | InitializeOutputVaryings(ctx); | ||
| 98 | } | ||
| 99 | |||
| 100 | void EmitEpilogue(EmitContext&) {} | ||
| 101 | |||
| 102 | void EmitEmitVertex(EmitContext& ctx, const IR::Value& stream) { | ||
| 103 | ctx.Add("EmitStreamVertex(int({}));", ctx.var_alloc.Consume(stream)); | ||
| 104 | InitializeOutputVaryings(ctx); | ||
| 105 | } | ||
| 106 | |||
| 107 | void EmitEndPrimitive(EmitContext& ctx, const IR::Value& stream) { | ||
| 108 | ctx.Add("EndStreamPrimitive(int({}));", ctx.var_alloc.Consume(stream)); | ||
| 109 | } | ||
| 110 | |||
| 111 | } // namespace Shader::Backend::GLSL | ||