diff options
Diffstat (limited to 'src/shader_recompiler/ir_opt/position_pass.cpp')
| -rw-r--r-- | src/shader_recompiler/ir_opt/position_pass.cpp | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/src/shader_recompiler/ir_opt/position_pass.cpp b/src/shader_recompiler/ir_opt/position_pass.cpp new file mode 100644 index 000000000..3c20b7189 --- /dev/null +++ b/src/shader_recompiler/ir_opt/position_pass.cpp | |||
| @@ -0,0 +1,77 @@ | |||
| 1 | // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project | ||
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | ||
| 3 | |||
| 4 | #include <boost/container/small_vector.hpp> | ||
| 5 | |||
| 6 | #include "shader_recompiler/frontend/ir/basic_block.h" | ||
| 7 | #include "shader_recompiler/frontend/ir/ir_emitter.h" | ||
| 8 | #include "shader_recompiler/frontend/ir/value.h" | ||
| 9 | #include "shader_recompiler/ir_opt/passes.h" | ||
| 10 | |||
| 11 | namespace Shader::Optimization { | ||
| 12 | |||
| 13 | namespace { | ||
| 14 | struct PositionInst { | ||
| 15 | IR::Inst* inst; | ||
| 16 | IR::Block* block; | ||
| 17 | IR::Attribute attr; | ||
| 18 | }; | ||
| 19 | using PositionInstVector = boost::container::small_vector<PositionInst, 24>; | ||
| 20 | } // Anonymous namespace | ||
| 21 | |||
| 22 | void PositionPass(Environment& env, IR::Program& program) { | ||
| 23 | if (env.ShaderStage() != Stage::VertexB || env.ReadViewportTransformState()) { | ||
| 24 | return; | ||
| 25 | } | ||
| 26 | |||
| 27 | Info& info{program.info}; | ||
| 28 | info.uses_render_area = true; | ||
| 29 | |||
| 30 | PositionInstVector to_replace; | ||
| 31 | for (IR::Block* const block : program.post_order_blocks) { | ||
| 32 | for (IR::Inst& inst : block->Instructions()) { | ||
| 33 | switch (inst.GetOpcode()) { | ||
| 34 | case IR::Opcode::SetAttribute: { | ||
| 35 | const IR::Attribute attr{inst.Arg(0).Attribute()}; | ||
| 36 | switch (attr) { | ||
| 37 | case IR::Attribute::PositionX: | ||
| 38 | case IR::Attribute::PositionY: { | ||
| 39 | to_replace.push_back(PositionInst{.inst = &inst, .block = block, .attr = attr}); | ||
| 40 | break; | ||
| 41 | } | ||
| 42 | default: | ||
| 43 | break; | ||
| 44 | } | ||
| 45 | break; | ||
| 46 | } | ||
| 47 | default: | ||
| 48 | break; | ||
| 49 | } | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | for (PositionInst& position_inst : to_replace) { | ||
| 54 | IR::IREmitter ir{*position_inst.block, | ||
| 55 | IR::Block::InstructionList::s_iterator_to(*position_inst.inst)}; | ||
| 56 | const IR::F32 value(position_inst.inst->Arg(1)); | ||
| 57 | const IR::F32F64 scale(ir.Imm32(2.f)); | ||
| 58 | const IR::F32 negative_one{ir.Imm32(-1.f)}; | ||
| 59 | switch (position_inst.attr) { | ||
| 60 | case IR::Attribute::PositionX: { | ||
| 61 | position_inst.inst->SetArg( | ||
| 62 | 1, | ||
| 63 | ir.FPFma(value, ir.FPMul(ir.FPRecip(ir.RenderAreaWidth()), scale), negative_one)); | ||
| 64 | break; | ||
| 65 | } | ||
| 66 | case IR::Attribute::PositionY: { | ||
| 67 | position_inst.inst->SetArg( | ||
| 68 | 1, | ||
| 69 | ir.FPFma(value, ir.FPMul(ir.FPRecip(ir.RenderAreaHeight()), scale), negative_one)); | ||
| 70 | break; | ||
| 71 | } | ||
| 72 | default: | ||
| 73 | break; | ||
| 74 | } | ||
| 75 | } | ||
| 76 | } | ||
| 77 | } // namespace Shader::Optimization | ||