2018-12-29 00:44:54 -05:00
|
|
|
// Copyright 2018 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <utility>
|
|
|
|
#include <variant>
|
|
|
|
|
2019-03-29 17:36:07 -04:00
|
|
|
#include "common/common_types.h"
|
2019-10-31 23:34:38 -04:00
|
|
|
#include "video_core/shader/node.h"
|
2018-12-29 00:44:54 -05:00
|
|
|
#include "video_core/shader/shader_ir.h"
|
|
|
|
|
|
|
|
namespace VideoCommon::Shader {
|
|
|
|
|
|
|
|
namespace {
|
2019-01-30 00:09:40 -05:00
|
|
|
std::pair<Node, s64> FindOperation(const NodeBlock& code, s64 cursor,
|
2018-12-29 00:44:54 -05:00
|
|
|
OperationCode operation_code) {
|
|
|
|
for (; cursor >= 0; --cursor) {
|
2019-07-16 10:37:11 -04:00
|
|
|
Node node = code.at(cursor);
|
|
|
|
|
2019-06-04 21:44:06 -04:00
|
|
|
if (const auto operation = std::get_if<OperationNode>(&*node)) {
|
2019-05-19 04:10:32 -04:00
|
|
|
if (operation->GetCode() == operation_code) {
|
2019-07-16 10:37:11 -04:00
|
|
|
return {std::move(node), cursor};
|
2019-05-19 04:10:32 -04:00
|
|
|
}
|
2018-12-29 00:44:54 -05:00
|
|
|
}
|
2019-07-16 10:37:11 -04:00
|
|
|
|
2019-06-04 21:44:06 -04:00
|
|
|
if (const auto conditional = std::get_if<ConditionalNode>(&*node)) {
|
2019-02-25 09:10:59 -05:00
|
|
|
const auto& conditional_code = conditional->GetCode();
|
2019-07-16 10:37:11 -04:00
|
|
|
auto [found, internal_cursor] = FindOperation(
|
2019-02-25 09:10:59 -05:00
|
|
|
conditional_code, static_cast<s64>(conditional_code.size() - 1), operation_code);
|
2019-05-19 04:10:32 -04:00
|
|
|
if (found) {
|
2019-07-16 10:37:11 -04:00
|
|
|
return {std::move(found), cursor};
|
2019-05-19 04:10:32 -04:00
|
|
|
}
|
2019-01-30 00:20:05 -05:00
|
|
|
}
|
2018-12-29 00:44:54 -05:00
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
2019-05-31 18:14:34 -04:00
|
|
|
} // Anonymous namespace
|
2018-12-29 00:44:54 -05:00
|
|
|
|
2019-05-31 18:14:34 -04:00
|
|
|
std::tuple<Node, u32, u32> ShaderIR::TrackCbuf(Node tracked, const NodeBlock& code,
|
|
|
|
s64 cursor) const {
|
2019-06-04 21:44:06 -04:00
|
|
|
if (const auto cbuf = std::get_if<CbufNode>(&*tracked)) {
|
2019-05-31 18:14:34 -04:00
|
|
|
// Constant buffer found, test if it's an immediate
|
|
|
|
const auto offset = cbuf->GetOffset();
|
|
|
|
if (const auto immediate = std::get_if<ImmediateNode>(&*offset)) {
|
|
|
|
return {tracked, cbuf->GetIndex(), immediate->GetValue()};
|
|
|
|
}
|
|
|
|
return {};
|
2018-12-29 00:44:54 -05:00
|
|
|
}
|
2019-06-04 21:44:06 -04:00
|
|
|
if (const auto gpr = std::get_if<GprNode>(&*tracked)) {
|
2018-12-29 00:44:54 -05:00
|
|
|
if (gpr->GetIndex() == Tegra::Shader::Register::ZeroIndex) {
|
2019-05-31 18:14:34 -04:00
|
|
|
return {};
|
2018-12-29 00:44:54 -05:00
|
|
|
}
|
|
|
|
// Reduce the cursor in one to avoid infinite loops when the instruction sets the same
|
|
|
|
// register that it uses as operand
|
|
|
|
const auto [source, new_cursor] = TrackRegister(gpr, code, cursor - 1);
|
|
|
|
if (!source) {
|
2019-05-31 18:14:34 -04:00
|
|
|
return {};
|
2018-12-29 00:44:54 -05:00
|
|
|
}
|
|
|
|
return TrackCbuf(source, code, new_cursor);
|
|
|
|
}
|
2019-06-04 21:44:06 -04:00
|
|
|
if (const auto operation = std::get_if<OperationNode>(&*tracked)) {
|
2019-07-15 15:46:35 -04:00
|
|
|
for (std::size_t i = operation->GetOperandsCount(); i > 0; --i) {
|
|
|
|
if (auto found = TrackCbuf((*operation)[i - 1], code, cursor); std::get<0>(found)) {
|
2019-05-31 18:14:34 -04:00
|
|
|
// Cbuf found in operand.
|
2018-12-29 00:44:54 -05:00
|
|
|
return found;
|
|
|
|
}
|
|
|
|
}
|
2019-05-31 18:14:34 -04:00
|
|
|
return {};
|
2018-12-29 00:44:54 -05:00
|
|
|
}
|
2019-06-04 21:44:06 -04:00
|
|
|
if (const auto conditional = std::get_if<ConditionalNode>(&*tracked)) {
|
2019-02-25 09:10:59 -05:00
|
|
|
const auto& conditional_code = conditional->GetCode();
|
|
|
|
return TrackCbuf(tracked, conditional_code, static_cast<s64>(conditional_code.size()));
|
2019-01-30 00:20:05 -05:00
|
|
|
}
|
2019-05-31 18:14:34 -04:00
|
|
|
return {};
|
2018-12-29 00:44:54 -05:00
|
|
|
}
|
|
|
|
|
2019-05-19 04:10:32 -04:00
|
|
|
std::optional<u32> ShaderIR::TrackImmediate(Node tracked, const NodeBlock& code, s64 cursor) const {
|
2019-03-29 17:36:07 -04:00
|
|
|
// Reduce the cursor in one to avoid infinite loops when the instruction sets the same register
|
|
|
|
// that it uses as operand
|
|
|
|
const auto [found, found_cursor] =
|
|
|
|
TrackRegister(&std::get<GprNode>(*tracked), code, cursor - 1);
|
|
|
|
if (!found) {
|
|
|
|
return {};
|
|
|
|
}
|
2019-06-04 21:44:06 -04:00
|
|
|
if (const auto immediate = std::get_if<ImmediateNode>(&*found)) {
|
2019-03-29 17:36:07 -04:00
|
|
|
return immediate->GetValue();
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-01-30 00:09:40 -05:00
|
|
|
std::pair<Node, s64> ShaderIR::TrackRegister(const GprNode* tracked, const NodeBlock& code,
|
2019-05-19 04:10:32 -04:00
|
|
|
s64 cursor) const {
|
2018-12-29 00:44:54 -05:00
|
|
|
for (; cursor >= 0; --cursor) {
|
|
|
|
const auto [found_node, new_cursor] = FindOperation(code, cursor, OperationCode::Assign);
|
|
|
|
if (!found_node) {
|
|
|
|
return {};
|
|
|
|
}
|
2019-06-04 21:44:06 -04:00
|
|
|
const auto operation = std::get_if<OperationNode>(&*found_node);
|
2018-12-29 00:44:54 -05:00
|
|
|
ASSERT(operation);
|
|
|
|
|
|
|
|
const auto& target = (*operation)[0];
|
2019-06-04 21:44:06 -04:00
|
|
|
if (const auto gpr_target = std::get_if<GprNode>(&*target)) {
|
2018-12-29 00:44:54 -05:00
|
|
|
if (gpr_target->GetIndex() == tracked->GetIndex()) {
|
|
|
|
return {(*operation)[1], new_cursor};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace VideoCommon::Shader
|