diff options
| author | 2021-07-25 11:39:04 -0700 | |
|---|---|---|
| committer | 2021-07-25 11:39:04 -0700 | |
| commit | 98b26b6e126d4775fdf3f773fe8a8ac808a8ff8f (patch) | |
| tree | 816faa96c2c4d291825063433331a8ea4b3d08f1 /src/shader_recompiler/backend/spirv/emit_spirv_special.cpp | |
| parent | Merge pull request #6699 from lat9nq/common-threads (diff) | |
| parent | shader: Support out of bound local memory reads and immediate writes (diff) | |
| download | yuzu-98b26b6e126d4775fdf3f773fe8a8ac808a8ff8f.tar.gz yuzu-98b26b6e126d4775fdf3f773fe8a8ac808a8ff8f.tar.xz yuzu-98b26b6e126d4775fdf3f773fe8a8ac808a8ff8f.zip | |
Merge pull request #6585 from ameerj/hades
Shader Decompiler Rewrite
Diffstat (limited to 'src/shader_recompiler/backend/spirv/emit_spirv_special.cpp')
| -rw-r--r-- | src/shader_recompiler/backend/spirv/emit_spirv_special.cpp | 150 |
1 files changed, 150 insertions, 0 deletions
diff --git a/src/shader_recompiler/backend/spirv/emit_spirv_special.cpp b/src/shader_recompiler/backend/spirv/emit_spirv_special.cpp new file mode 100644 index 000000000..9e7eb3cb1 --- /dev/null +++ b/src/shader_recompiler/backend/spirv/emit_spirv_special.cpp | |||
| @@ -0,0 +1,150 @@ | |||
| 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 "shader_recompiler/backend/spirv/emit_spirv.h" | ||
| 6 | #include "shader_recompiler/backend/spirv/emit_spirv_instructions.h" | ||
| 7 | |||
| 8 | namespace Shader::Backend::SPIRV { | ||
| 9 | namespace { | ||
| 10 | void ConvertDepthMode(EmitContext& ctx) { | ||
| 11 | const Id type{ctx.F32[1]}; | ||
| 12 | const Id position{ctx.OpLoad(ctx.F32[4], ctx.output_position)}; | ||
| 13 | const Id z{ctx.OpCompositeExtract(type, position, 2u)}; | ||
| 14 | const Id w{ctx.OpCompositeExtract(type, position, 3u)}; | ||
| 15 | const Id screen_depth{ctx.OpFMul(type, ctx.OpFAdd(type, z, w), ctx.Constant(type, 0.5f))}; | ||
| 16 | const Id vector{ctx.OpCompositeInsert(ctx.F32[4], screen_depth, position, 2u)}; | ||
| 17 | ctx.OpStore(ctx.output_position, vector); | ||
| 18 | } | ||
| 19 | |||
| 20 | void SetFixedPipelinePointSize(EmitContext& ctx) { | ||
| 21 | if (ctx.runtime_info.fixed_state_point_size) { | ||
| 22 | const float point_size{*ctx.runtime_info.fixed_state_point_size}; | ||
| 23 | ctx.OpStore(ctx.output_point_size, ctx.Const(point_size)); | ||
| 24 | } | ||
| 25 | } | ||
| 26 | |||
| 27 | Id DefaultVarying(EmitContext& ctx, u32 num_components, u32 element, Id zero, Id one, | ||
| 28 | Id default_vector) { | ||
| 29 | switch (num_components) { | ||
| 30 | case 1: | ||
| 31 | return element == 3 ? one : zero; | ||
| 32 | case 2: | ||
| 33 | return ctx.ConstantComposite(ctx.F32[2], zero, element + 1 == 3 ? one : zero); | ||
| 34 | case 3: | ||
| 35 | return ctx.ConstantComposite(ctx.F32[3], zero, zero, element + 2 == 3 ? one : zero); | ||
| 36 | case 4: | ||
| 37 | return default_vector; | ||
| 38 | } | ||
| 39 | throw InvalidArgument("Bad element"); | ||
| 40 | } | ||
| 41 | |||
| 42 | Id ComparisonFunction(EmitContext& ctx, CompareFunction comparison, Id operand_1, Id operand_2) { | ||
| 43 | switch (comparison) { | ||
| 44 | case CompareFunction::Never: | ||
| 45 | return ctx.false_value; | ||
| 46 | case CompareFunction::Less: | ||
| 47 | return ctx.OpFOrdLessThan(ctx.U1, operand_1, operand_2); | ||
| 48 | case CompareFunction::Equal: | ||
| 49 | return ctx.OpFOrdEqual(ctx.U1, operand_1, operand_2); | ||
| 50 | case CompareFunction::LessThanEqual: | ||
| 51 | return ctx.OpFOrdLessThanEqual(ctx.U1, operand_1, operand_2); | ||
| 52 | case CompareFunction::Greater: | ||
| 53 | return ctx.OpFOrdGreaterThan(ctx.U1, operand_1, operand_2); | ||
| 54 | case CompareFunction::NotEqual: | ||
| 55 | return ctx.OpFOrdNotEqual(ctx.U1, operand_1, operand_2); | ||
| 56 | case CompareFunction::GreaterThanEqual: | ||
| 57 | return ctx.OpFOrdGreaterThanEqual(ctx.U1, operand_1, operand_2); | ||
| 58 | case CompareFunction::Always: | ||
| 59 | return ctx.true_value; | ||
| 60 | } | ||
| 61 | throw InvalidArgument("Comparison function {}", comparison); | ||
| 62 | } | ||
| 63 | |||
| 64 | void AlphaTest(EmitContext& ctx) { | ||
| 65 | if (!ctx.runtime_info.alpha_test_func) { | ||
| 66 | return; | ||
| 67 | } | ||
| 68 | const auto comparison{*ctx.runtime_info.alpha_test_func}; | ||
| 69 | if (comparison == CompareFunction::Always) { | ||
| 70 | return; | ||
| 71 | } | ||
| 72 | if (!Sirit::ValidId(ctx.frag_color[0])) { | ||
| 73 | return; | ||
| 74 | } | ||
| 75 | |||
| 76 | const Id type{ctx.F32[1]}; | ||
| 77 | const Id rt0_color{ctx.OpLoad(ctx.F32[4], ctx.frag_color[0])}; | ||
| 78 | const Id alpha{ctx.OpCompositeExtract(type, rt0_color, 3u)}; | ||
| 79 | |||
| 80 | const Id true_label{ctx.OpLabel()}; | ||
| 81 | const Id discard_label{ctx.OpLabel()}; | ||
| 82 | const Id alpha_reference{ctx.Const(ctx.runtime_info.alpha_test_reference)}; | ||
| 83 | const Id condition{ComparisonFunction(ctx, comparison, alpha, alpha_reference)}; | ||
| 84 | |||
| 85 | ctx.OpSelectionMerge(true_label, spv::SelectionControlMask::MaskNone); | ||
| 86 | ctx.OpBranchConditional(condition, true_label, discard_label); | ||
| 87 | ctx.AddLabel(discard_label); | ||
| 88 | ctx.OpKill(); | ||
| 89 | ctx.AddLabel(true_label); | ||
| 90 | } | ||
| 91 | } // Anonymous namespace | ||
| 92 | |||
| 93 | void EmitPrologue(EmitContext& ctx) { | ||
| 94 | if (ctx.stage == Stage::VertexB) { | ||
| 95 | const Id zero{ctx.Const(0.0f)}; | ||
| 96 | const Id one{ctx.Const(1.0f)}; | ||
| 97 | const Id default_vector{ctx.ConstantComposite(ctx.F32[4], zero, zero, zero, one)}; | ||
| 98 | ctx.OpStore(ctx.output_position, default_vector); | ||
| 99 | for (const auto& info : ctx.output_generics) { | ||
| 100 | if (info[0].num_components == 0) { | ||
| 101 | continue; | ||
| 102 | } | ||
| 103 | u32 element{0}; | ||
| 104 | while (element < 4) { | ||
| 105 | const auto& element_info{info[element]}; | ||
| 106 | const u32 num{element_info.num_components}; | ||
| 107 | const Id value{DefaultVarying(ctx, num, element, zero, one, default_vector)}; | ||
| 108 | ctx.OpStore(element_info.id, value); | ||
| 109 | element += num; | ||
| 110 | } | ||
| 111 | } | ||
| 112 | } | ||
| 113 | if (ctx.stage == Stage::VertexB || ctx.stage == Stage::Geometry) { | ||
| 114 | SetFixedPipelinePointSize(ctx); | ||
| 115 | } | ||
| 116 | } | ||
| 117 | |||
| 118 | void EmitEpilogue(EmitContext& ctx) { | ||
| 119 | if (ctx.stage == Stage::VertexB && ctx.runtime_info.convert_depth_mode) { | ||
| 120 | ConvertDepthMode(ctx); | ||
| 121 | } | ||
| 122 | if (ctx.stage == Stage::Fragment) { | ||
| 123 | AlphaTest(ctx); | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | void EmitEmitVertex(EmitContext& ctx, const IR::Value& stream) { | ||
| 128 | if (ctx.runtime_info.convert_depth_mode) { | ||
| 129 | ConvertDepthMode(ctx); | ||
| 130 | } | ||
| 131 | if (stream.IsImmediate()) { | ||
| 132 | ctx.OpEmitStreamVertex(ctx.Def(stream)); | ||
| 133 | } else { | ||
| 134 | LOG_WARNING(Shader_SPIRV, "Stream is not immediate"); | ||
| 135 | ctx.OpEmitStreamVertex(ctx.u32_zero_value); | ||
| 136 | } | ||
| 137 | // Restore fixed pipeline point size after emitting the vertex | ||
| 138 | SetFixedPipelinePointSize(ctx); | ||
| 139 | } | ||
| 140 | |||
| 141 | void EmitEndPrimitive(EmitContext& ctx, const IR::Value& stream) { | ||
| 142 | if (stream.IsImmediate()) { | ||
| 143 | ctx.OpEndStreamPrimitive(ctx.Def(stream)); | ||
| 144 | } else { | ||
| 145 | LOG_WARNING(Shader_SPIRV, "Stream is not immediate"); | ||
| 146 | ctx.OpEndStreamPrimitive(ctx.u32_zero_value); | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | } // namespace Shader::Backend::SPIRV | ||