summaryrefslogtreecommitdiffstats
path: root/src/shader_recompiler/ir_opt
diff options
context:
space:
mode:
authorReinUsesLisp <reinuseslisp@airmail.cc>2021-07-26 03:20:56 +0200
committerFernando Sahmkow <fsahmkow27@gmail.com>2021-11-16 22:11:28 +0100
commit01379c5e3cd1181c7c7d37a672364fbcad627fb0 (patch)
tree9dfc52e3e4793f24cdc1d0c1d071f21605cb2bac /src/shader_recompiler/ir_opt
parentshader: Add IsTextureScaled opcode (diff)
downloadyuzu-01379c5e3cd1181c7c7d37a672364fbcad627fb0.tar
yuzu-01379c5e3cd1181c7c7d37a672364fbcad627fb0.tar.gz
yuzu-01379c5e3cd1181c7c7d37a672364fbcad627fb0.tar.bz2
yuzu-01379c5e3cd1181c7c7d37a672364fbcad627fb0.tar.lz
yuzu-01379c5e3cd1181c7c7d37a672364fbcad627fb0.tar.xz
yuzu-01379c5e3cd1181c7c7d37a672364fbcad627fb0.tar.zst
yuzu-01379c5e3cd1181c7c7d37a672364fbcad627fb0.zip
Diffstat (limited to 'src/shader_recompiler/ir_opt')
-rw-r--r--src/shader_recompiler/ir_opt/rescaling_pass.cpp105
1 files changed, 101 insertions, 4 deletions
diff --git a/src/shader_recompiler/ir_opt/rescaling_pass.cpp b/src/shader_recompiler/ir_opt/rescaling_pass.cpp
index f8d04b6e3..d5b98ae6e 100644
--- a/src/shader_recompiler/ir_opt/rescaling_pass.cpp
+++ b/src/shader_recompiler/ir_opt/rescaling_pass.cpp
@@ -22,6 +22,105 @@ void PatchFragCoord(IR::Block& block, IR::Inst& inst) {
inst.ReplaceUsesWith(downscaled_frag_coord);
}
+[[nodiscard]] IR::U32 Scale(IR::IREmitter& ir, const IR::U1& is_scaled, const IR::U32& value) {
+ IR::U32 scaled_value{value};
+ bool changed{};
+ if (const u32 up_scale = Settings::values.resolution_info.up_scale; up_scale != 1) {
+ scaled_value = ir.IMul(value, ir.Imm32(up_scale));
+ changed = true;
+ }
+ if (const u32 down_shift = Settings::values.resolution_info.down_shift; down_shift != 0) {
+ scaled_value = ir.ShiftRightArithmetic(value, ir.Imm32(down_shift));
+ changed = true;
+ }
+ if (changed) {
+ return IR::U32{ir.Select(is_scaled, scaled_value, value)};
+ } else {
+ return value;
+ }
+}
+
+[[nodiscard]] IR::U32 DownScale(IR::IREmitter& ir, IR::U32 value) {
+ if (const u32 down_shift = Settings::values.resolution_info.down_shift; down_shift != 0) {
+ value = ir.ShiftLeftLogical(value, ir.Imm32(down_shift));
+ }
+ if (const u32 up_scale = Settings::values.resolution_info.up_scale; up_scale != 1) {
+ value = ir.IDiv(value, ir.Imm32(up_scale));
+ }
+ return value;
+}
+
+void PatchImageQueryDimensions(IR::Block& block, IR::Inst& inst) {
+ const auto it{IR::Block::InstructionList::s_iterator_to(inst)};
+ IR::IREmitter ir{block, IR::Block::InstructionList::s_iterator_to(inst)};
+ const auto info{inst.Flags<IR::TextureInstInfo>()};
+ switch (info.type) {
+ case TextureType::Color1D:
+ case TextureType::ColorArray1D: {
+ const IR::Value new_inst{&*block.PrependNewInst(it, inst)};
+ const IR::U32 width{DownScale(ir, IR::U32{ir.CompositeExtract(new_inst, 0)})};
+ const IR::Value replacement{ir.CompositeConstruct(width, ir.CompositeExtract(new_inst, 1),
+ ir.CompositeExtract(new_inst, 2),
+ ir.CompositeExtract(new_inst, 3))};
+ inst.ReplaceUsesWith(replacement);
+ break;
+ }
+ case TextureType::Color2D:
+ case TextureType::ColorArray2D: {
+ const IR::Value new_inst{&*block.PrependNewInst(it, inst)};
+ const IR::U32 width{DownScale(ir, IR::U32{ir.CompositeExtract(new_inst, 0)})};
+ const IR::U32 height{DownScale(ir, IR::U32{ir.CompositeExtract(new_inst, 1)})};
+ const IR::Value replacement{ir.CompositeConstruct(
+ width, height, ir.CompositeExtract(new_inst, 2), ir.CompositeExtract(new_inst, 3))};
+ inst.ReplaceUsesWith(replacement);
+ break;
+ }
+ case TextureType::Color3D:
+ case TextureType::ColorCube:
+ case TextureType::ColorArrayCube:
+ case TextureType::Buffer:
+ // Nothing to patch here
+ break;
+ }
+}
+
+void PatchImageFetch(IR::Block& block, IR::Inst& inst) {
+ IR::IREmitter ir{block, IR::Block::InstructionList::s_iterator_to(inst)};
+ const auto info{inst.Flags<IR::TextureInstInfo>()};
+ const IR::U1 is_scaled{ir.IsTextureScaled(ir.Imm32(info.descriptor_index))};
+ const IR::Value coord{inst.Arg(1)};
+ switch (info.type) {
+ case TextureType::Color1D:
+ inst.SetArg(1, Scale(ir, is_scaled, IR::U32{coord}));
+ break;
+ case TextureType::ColorArray1D: {
+ const IR::U32 x{Scale(ir, is_scaled, IR::U32{ir.CompositeExtract(coord, 0)})};
+ const IR::U32 y{ir.CompositeExtract(coord, 1)};
+ inst.SetArg(1, ir.CompositeConstruct(x, y));
+ break;
+ }
+ case TextureType::Color2D: {
+ const IR::U32 x{Scale(ir, is_scaled, IR::U32{ir.CompositeExtract(coord, 0)})};
+ const IR::U32 y{Scale(ir, is_scaled, IR::U32{ir.CompositeExtract(coord, 1)})};
+ inst.SetArg(1, ir.CompositeConstruct(x, y));
+ break;
+ }
+ case TextureType::ColorArray2D: {
+ const IR::U32 x{Scale(ir, is_scaled, IR::U32{ir.CompositeExtract(coord, 0)})};
+ const IR::U32 y{Scale(ir, is_scaled, IR::U32{ir.CompositeExtract(coord, 1)})};
+ const IR::U32 z{ir.CompositeExtract(coord, 2)};
+ inst.SetArg(1, ir.CompositeConstruct(x, y, z));
+ break;
+ }
+ case TextureType::Color3D:
+ case TextureType::ColorCube:
+ case TextureType::ColorArrayCube:
+ case TextureType::Buffer:
+ // Nothing to patch here
+ break;
+ }
+}
+
void Visit(const IR::Program& program, IR::Block& block, IR::Inst& inst) {
const bool is_fragment_shader{program.stage == Stage::Fragment};
switch (inst.GetOpcode()) {
@@ -40,12 +139,10 @@ void Visit(const IR::Program& program, IR::Block& block, IR::Inst& inst) {
break;
}
case IR::Opcode::ImageQueryDimensions:
+ PatchImageQueryDimensions(block, inst);
break;
case IR::Opcode::ImageFetch:
- break;
- case IR::Opcode::ImageRead:
- break;
- case IR::Opcode::ImageWrite:
+ PatchImageFetch(block, inst);
break;
default:
break;