2021-01-09 06:30:07 +00:00
|
|
|
// Copyright 2021 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <memory>
|
2021-02-11 19:39:06 +00:00
|
|
|
#include <vector>
|
2021-01-09 06:30:07 +00:00
|
|
|
|
2021-02-06 02:11:23 +00:00
|
|
|
#include "shader_recompiler/frontend/ir/basic_block.h"
|
2021-02-14 23:15:42 +00:00
|
|
|
#include "shader_recompiler/frontend/ir/post_order.h"
|
2021-01-09 06:30:07 +00:00
|
|
|
#include "shader_recompiler/frontend/maxwell/program.h"
|
2021-03-14 06:41:05 +00:00
|
|
|
#include "shader_recompiler/frontend/maxwell/structured_control_flow.h"
|
2021-01-09 06:30:07 +00:00
|
|
|
#include "shader_recompiler/frontend/maxwell/translate/translate.h"
|
2021-02-03 00:07:00 +00:00
|
|
|
#include "shader_recompiler/ir_opt/passes.h"
|
2021-01-09 06:30:07 +00:00
|
|
|
|
|
|
|
namespace Shader::Maxwell {
|
2021-04-04 08:18:09 +00:00
|
|
|
namespace {
|
|
|
|
void RemoveUnreachableBlocks(IR::Program& program) {
|
2021-03-14 06:41:05 +00:00
|
|
|
// Some blocks might be unreachable if a function call exists unconditionally
|
|
|
|
// If this happens the number of blocks and post order blocks will mismatch
|
|
|
|
if (program.blocks.size() == program.post_order_blocks.size()) {
|
|
|
|
return;
|
|
|
|
}
|
2021-04-04 08:18:09 +00:00
|
|
|
const auto begin{std::next(program.blocks.begin())};
|
|
|
|
const auto end{program.blocks.end()};
|
|
|
|
const auto pred{[](IR::Block* block) { return block->ImmediatePredecessors().empty(); }};
|
|
|
|
program.blocks.erase(std::remove_if(begin, end, pred), end);
|
2021-02-03 00:07:00 +00:00
|
|
|
}
|
2021-01-09 06:30:07 +00:00
|
|
|
|
2021-04-04 08:18:09 +00:00
|
|
|
void CollectInterpolationInfo(Environment& env, IR::Program& program) {
|
2021-03-27 07:59:58 +00:00
|
|
|
if (program.stage != Stage::Fragment) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const ProgramHeader& sph{env.SPH()};
|
|
|
|
for (size_t index = 0; index < program.info.input_generics.size(); ++index) {
|
|
|
|
std::optional<PixelImap> imap;
|
|
|
|
for (const PixelImap value : sph.ps.GenericInputMap(static_cast<u32>(index))) {
|
|
|
|
if (value == PixelImap::Unused) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (imap && imap != value) {
|
|
|
|
throw NotImplementedException("Per component interpolation");
|
|
|
|
}
|
|
|
|
imap = value;
|
|
|
|
}
|
|
|
|
if (!imap) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
program.info.input_generics[index].interpolation = [&] {
|
|
|
|
switch (*imap) {
|
|
|
|
case PixelImap::Unused:
|
|
|
|
case PixelImap::Perspective:
|
|
|
|
return Interpolation::Smooth;
|
|
|
|
case PixelImap::Constant:
|
|
|
|
return Interpolation::Flat;
|
|
|
|
case PixelImap::ScreenLinear:
|
|
|
|
return Interpolation::NoPerspective;
|
|
|
|
}
|
|
|
|
throw NotImplementedException("Unknown interpolation {}", *imap);
|
|
|
|
}();
|
|
|
|
}
|
|
|
|
}
|
2021-04-04 08:18:09 +00:00
|
|
|
} // Anonymous namespace
|
2021-03-27 07:59:58 +00:00
|
|
|
|
2021-02-06 02:11:23 +00:00
|
|
|
IR::Program TranslateProgram(ObjectPool<IR::Inst>& inst_pool, ObjectPool<IR::Block>& block_pool,
|
2021-02-11 19:39:06 +00:00
|
|
|
Environment& env, Flow::CFG& cfg) {
|
2021-02-06 02:11:23 +00:00
|
|
|
IR::Program program;
|
2021-03-14 06:41:05 +00:00
|
|
|
program.blocks = VisitAST(inst_pool, block_pool, env, cfg);
|
|
|
|
program.post_order_blocks = PostOrder(program.blocks);
|
2021-03-19 22:28:31 +00:00
|
|
|
program.stage = env.ShaderStage();
|
2021-03-28 22:53:34 +00:00
|
|
|
program.local_memory_size = env.LocalMemorySize();
|
2021-03-27 06:08:31 +00:00
|
|
|
if (program.stage == Stage::Compute) {
|
|
|
|
program.workgroup_size = env.WorkgroupSize();
|
2021-03-28 22:53:34 +00:00
|
|
|
program.shared_memory_size = env.SharedMemorySize();
|
2021-03-27 06:08:31 +00:00
|
|
|
}
|
2021-03-14 06:41:05 +00:00
|
|
|
RemoveUnreachableBlocks(program);
|
|
|
|
|
|
|
|
// Replace instructions before the SSA rewrite
|
2021-02-19 21:10:18 +00:00
|
|
|
Optimization::LowerFp16ToFp32(program);
|
2021-03-14 06:41:05 +00:00
|
|
|
|
|
|
|
Optimization::SsaRewritePass(program);
|
|
|
|
|
2021-02-16 07:10:22 +00:00
|
|
|
Optimization::GlobalMemoryToStorageBufferPass(program);
|
2021-03-08 21:31:53 +00:00
|
|
|
Optimization::TexturePass(env, program);
|
2021-03-14 06:41:05 +00:00
|
|
|
|
|
|
|
Optimization::ConstantPropagationPass(program);
|
|
|
|
Optimization::DeadCodeEliminationPass(program);
|
|
|
|
Optimization::IdentityRemovalPass(program);
|
|
|
|
Optimization::VerificationPass(program);
|
2021-02-16 07:10:22 +00:00
|
|
|
Optimization::CollectShaderInfoPass(program);
|
2021-03-27 07:59:58 +00:00
|
|
|
CollectInterpolationInfo(env, program);
|
2021-02-06 02:11:23 +00:00
|
|
|
return program;
|
2021-01-09 06:30:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Shader::Maxwell
|