From 603c861532ed1a89254556ff84bbe121bd700765 Mon Sep 17 00:00:00 2001 From: Fernando Sahmkow Date: Sun, 5 Jan 2020 15:23:24 -0400 Subject: Shader_IR: Implement initial code for tracking indexed samplers. --- src/video_core/shader/track.cpp | 82 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) (limited to 'src/video_core/shader/track.cpp') diff --git a/src/video_core/shader/track.cpp b/src/video_core/shader/track.cpp index 165c79330..69a677394 100644 --- a/src/video_core/shader/track.cpp +++ b/src/video_core/shader/track.cpp @@ -8,6 +8,7 @@ #include "common/common_types.h" #include "video_core/shader/node.h" +#include "video_core/shader/node_helper.h" #include "video_core/shader/shader_ir.h" namespace VideoCommon::Shader { @@ -37,6 +38,87 @@ std::pair FindOperation(const NodeBlock& code, s64 cursor, } } // Anonymous namespace +std::optional> DecoupleIndirectRead(const OperationNode& operation) { + if (operation.GetCode() != OperationCode::UAdd) { + return std::nullopt; + } + Node gpr{}; + Node offset{}; + if (operation.GetOperandsCount() != 2) { + return std::nullopt; + } + for (std::size_t i = 0; i < operation.GetOperandsCount(); i++) { + Node operand = operation[i]; + if (std::holds_alternative(*operand)) { + offset = operation[i]; + } else if (std::holds_alternative(*operand)) { + gpr = operation[i]; + } + } + if (offset && gpr) { + return {std::make_pair(gpr, offset)}; + } + return std::nullopt; +} + +std::tuple ShaderIR::TrackSampler(Node tracked, const NodeBlock& code, + s64 cursor) const { + if (const auto cbuf = std::get_if(&*tracked)) { + // Constant buffer found, test if it's an immediate + const auto offset = cbuf->GetOffset(); + if (const auto immediate = std::get_if(&*offset)) { + auto track = + MakeTrackSampler(cbuf->GetIndex(), immediate->GetValue()); + return {tracked, track}; + } else if (const auto operation = std::get_if(&*offset)) { + auto bound_buffer = locker.ObtainBoundBuffer(); + if (!bound_buffer) { + return {}; + } + if (*bound_buffer != cbuf->GetIndex()) { + return {}; + } + auto pair = DecoupleIndirectRead(*operation); + if (!pair) { + return {}; + } + auto [gpr, base_offset] = *pair; + const auto offset_inm = std::get_if(&*base_offset); + // TODO Implement Bindless Index custom variable + auto track = + MakeTrackSampler(cbuf->GetIndex(), offset_inm->GetValue(), 0); + return {tracked, track}; + } + return {}; + } + if (const auto gpr = std::get_if(&*tracked)) { + if (gpr->GetIndex() == Tegra::Shader::Register::ZeroIndex) { + return {}; + } + // 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) { + return {}; + } + return TrackSampler(source, code, new_cursor); + } + if (const auto operation = std::get_if(&*tracked)) { + for (std::size_t i = operation->GetOperandsCount(); i > 0; --i) { + if (auto found = TrackSampler((*operation)[i - 1], code, cursor); std::get<0>(found)) { + // Cbuf found in operand. + return found; + } + } + return {}; + } + if (const auto conditional = std::get_if(&*tracked)) { + const auto& conditional_code = conditional->GetCode(); + return TrackSampler(tracked, conditional_code, static_cast(conditional_code.size())); + } + return {}; +} + std::tuple ShaderIR::TrackCbuf(Node tracked, const NodeBlock& code, s64 cursor) const { if (const auto cbuf = std::get_if(&*tracked)) { -- cgit v1.2.3 From 7c530e06661d760eb6366724d109468423363072 Mon Sep 17 00:00:00 2001 From: Fernando Sahmkow Date: Tue, 7 Jan 2020 17:45:12 -0400 Subject: Shader_IR: Propagate bindless index into the GL compiler. --- src/video_core/shader/track.cpp | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) (limited to 'src/video_core/shader/track.cpp') diff --git a/src/video_core/shader/track.cpp b/src/video_core/shader/track.cpp index 69a677394..d449b625e 100644 --- a/src/video_core/shader/track.cpp +++ b/src/video_core/shader/track.cpp @@ -61,8 +61,19 @@ std::optional> DecoupleIndirectRead(const OperationNode& o return std::nullopt; } +bool AmendNodeCv(std::size_t amend_index, Node node) { + if (const auto operation = std::get_if(&*node)) { + operation->SetAmendIndex(amend_index); + return true; + } else if (const auto conditional = std::get_if(&*node)) { + conditional->SetAmendIndex(amend_index); + return true; + } + return false; +} + std::tuple ShaderIR::TrackSampler(Node tracked, const NodeBlock& code, - s64 cursor) const { + s64 cursor) { if (const auto cbuf = std::get_if(&*tracked)) { // Constant buffer found, test if it's an immediate const auto offset = cbuf->GetOffset(); @@ -84,9 +95,21 @@ std::tuple ShaderIR::TrackSampler(Node tracked, const NodeBl } auto [gpr, base_offset] = *pair; const auto offset_inm = std::get_if(&*base_offset); + auto gpu_driver = locker.AccessGuestDriverProfile(); + if (gpu_driver == nullptr) { + return {}; + } + const u32 bindless_cv = NewCustomVariable(); + const Node op = Operation(OperationCode::UDiv, NO_PRECISE, gpr, + Immediate(gpu_driver->GetTextureHandlerSize())); + + const Node cv_node = GetCustomVariable(bindless_cv); + Node amend_op = Operation(OperationCode::Assign, cv_node, std::move(op)); + const std::size_t amend_index = DeclareAmend(amend_op); + AmendNodeCv(amend_index, code[cursor]); // TODO Implement Bindless Index custom variable - auto track = - MakeTrackSampler(cbuf->GetIndex(), offset_inm->GetValue(), 0); + auto track = MakeTrackSampler(cbuf->GetIndex(), + offset_inm->GetValue(), bindless_cv); return {tracked, track}; } return {}; -- cgit v1.2.3 From 806f5691430b86640d64d4c5ae77c5e1dac1625a Mon Sep 17 00:00:00 2001 From: Fernando Sahmkow Date: Wed, 8 Jan 2020 15:59:21 -0400 Subject: Shader_IR: Change name of TrackSampler function so it does not confuse with the type. --- src/video_core/shader/track.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'src/video_core/shader/track.cpp') diff --git a/src/video_core/shader/track.cpp b/src/video_core/shader/track.cpp index d449b625e..4db721f69 100644 --- a/src/video_core/shader/track.cpp +++ b/src/video_core/shader/track.cpp @@ -72,8 +72,8 @@ bool AmendNodeCv(std::size_t amend_index, Node node) { return false; } -std::tuple ShaderIR::TrackSampler(Node tracked, const NodeBlock& code, - s64 cursor) { +std::tuple ShaderIR::TrackBindlessSampler(Node tracked, const NodeBlock& code, + s64 cursor) { if (const auto cbuf = std::get_if(&*tracked)) { // Constant buffer found, test if it's an immediate const auto offset = cbuf->GetOffset(); @@ -124,11 +124,12 @@ std::tuple ShaderIR::TrackSampler(Node tracked, const NodeBl if (!source) { return {}; } - return TrackSampler(source, code, new_cursor); + return TrackBindlessSampler(source, code, new_cursor); } if (const auto operation = std::get_if(&*tracked)) { for (std::size_t i = operation->GetOperandsCount(); i > 0; --i) { - if (auto found = TrackSampler((*operation)[i - 1], code, cursor); std::get<0>(found)) { + if (auto found = TrackBindlessSampler((*operation)[i - 1], code, cursor); + std::get<0>(found)) { // Cbuf found in operand. return found; } @@ -137,7 +138,8 @@ std::tuple ShaderIR::TrackSampler(Node tracked, const NodeBl } if (const auto conditional = std::get_if(&*tracked)) { const auto& conditional_code = conditional->GetCode(); - return TrackSampler(tracked, conditional_code, static_cast(conditional_code.size())); + return TrackBindlessSampler(tracked, conditional_code, + static_cast(conditional_code.size())); } return {}; } -- cgit v1.2.3 From bb8eb15d392d69693f8cda0427669d011e23db97 Mon Sep 17 00:00:00 2001 From: Fernando Sahmkow Date: Fri, 24 Jan 2020 10:44:34 -0400 Subject: Shader_IR: Address feedback. --- src/video_core/shader/track.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'src/video_core/shader/track.cpp') diff --git a/src/video_core/shader/track.cpp b/src/video_core/shader/track.cpp index 4db721f69..ea39bca54 100644 --- a/src/video_core/shader/track.cpp +++ b/src/video_core/shader/track.cpp @@ -36,7 +36,6 @@ std::pair FindOperation(const NodeBlock& code, s64 cursor, } return {}; } -} // Anonymous namespace std::optional> DecoupleIndirectRead(const OperationNode& operation) { if (operation.GetCode() != OperationCode::UAdd) { @@ -44,9 +43,7 @@ std::optional> DecoupleIndirectRead(const OperationNode& o } Node gpr{}; Node offset{}; - if (operation.GetOperandsCount() != 2) { - return std::nullopt; - } + ASSERT(operation.GetOperandsCount() == 2); for (std::size_t i = 0; i < operation.GetOperandsCount(); i++) { Node operand = operation[i]; if (std::holds_alternative(*operand)) { @@ -56,7 +53,7 @@ std::optional> DecoupleIndirectRead(const OperationNode& o } } if (offset && gpr) { - return {std::make_pair(gpr, offset)}; + return std::make_pair(gpr, offset); } return std::nullopt; } @@ -72,6 +69,8 @@ bool AmendNodeCv(std::size_t amend_index, Node node) { return false; } +} // Anonymous namespace + std::tuple ShaderIR::TrackBindlessSampler(Node tracked, const NodeBlock& code, s64 cursor) { if (const auto cbuf = std::get_if(&*tracked)) { -- cgit v1.2.3