recompiler: deduplication cleanup

This commit is contained in:
nmzik
2026-08-17 04:02:33 +02:00
parent 0f3540b1ff
commit e7c8bcb3a4
8 changed files with 42 additions and 57 deletions
@@ -2,6 +2,7 @@
#include "graphics/shader/recompiler/backend/spirv/spirvEmitterInternal.h" #include "graphics/shader/recompiler/backend/spirv/spirvEmitterInternal.h"
#include "graphics/shader/recompiler/ir/ValueProgram.h" #include "graphics/shader/recompiler/ir/ValueProgram.h"
#include "graphics/shader/shader.h"
#include <algorithm> #include <algorithm>
#include <array> #include <array>
@@ -10,14 +11,9 @@
namespace Libs::Graphics::ShaderRecompiler::Spirv { namespace Libs::Graphics::ShaderRecompiler::Spirv {
namespace { using ShaderError::Fail;
bool Fail(std::string* error, const std::string& message) { namespace {
if (error != nullptr) {
*error = message;
}
return false;
}
bool ImageBinding(const IR::ImageResource& image, IR::DescriptorBindingKind& kind) { bool ImageBinding(const IR::ImageResource& image, IR::DescriptorBindingKind& kind) {
using Kind = IR::DescriptorBindingKind; using Kind = IR::DescriptorBindingKind;
@@ -1,4 +1,5 @@
#include "graphics/shader/recompiler/frontend/translate/Translator.h" #include "graphics/shader/recompiler/frontend/translate/Translator.h"
#include "graphics/shader/shader.h"
#include <algorithm> #include <algorithm>
#include <array> #include <array>
@@ -6,14 +7,10 @@
#include <utility> #include <utility>
namespace Libs::Graphics::ShaderRecompiler::Frontend { namespace Libs::Graphics::ShaderRecompiler::Frontend {
namespace Detail {
bool Fail(std::string* error, const std::string& message) { using ShaderError::Fail;
if (error != nullptr) {
*error = message; namespace Detail {
}
return false;
}
IR::Operand Translator::OffsetOperand(const IR::Operand& operand, uint32_t offset) { IR::Operand Translator::OffsetOperand(const IR::Operand& operand, uint32_t offset) {
if (offset == 0 || operand.kind != IR::OperandKind::Register) { if (offset == 0 || operand.kind != IR::OperandKind::Register) {
@@ -808,7 +805,7 @@ bool TranslateProgram(const IR::Program& source, IR::ValueProgram& result,
return block.id; return block.id;
})->id; })->id;
if (max_id == UINT32_MAX) { if (max_id == UINT32_MAX) {
return Detail::Fail(error, "cannot allocate a typed prologue block id"); return Fail(error, "cannot allocate a typed prologue block id");
} }
CFG::Terminator terminator; CFG::Terminator terminator;
terminator.kind = CFG::TerminatorKind::Branch; terminator.kind = CFG::TerminatorKind::Branch;
@@ -827,7 +824,7 @@ bool TranslateProgram(const IR::Program& source, IR::ValueProgram& result,
for (size_t index = 0; index < source.blocks.size(); index++) { for (size_t index = 0; index < source.blocks.size(); index++) {
for (const auto successor: source.blocks[index].successors) { for (const auto successor: source.blocks[index].successors) {
if (successor >= source.blocks.size()) { if (successor >= source.blocks.size()) {
return Detail::Fail(error, "value IR block successor is out of range"); return Fail(error, "value IR block successor is out of range");
} }
result.blocks[index + 1u]->AddBranch(result.blocks[successor + 1u]); result.blocks[index + 1u]->AddBranch(result.blocks[successor + 1u]);
} }
@@ -18,8 +18,6 @@ struct ScalarMemorySourceValues {
IR::U32 offset; IR::U32 offset;
}; };
bool Fail(std::string* error, const std::string& message);
class Translator { class Translator {
public: public:
Translator(IR::ValueProgram& program, IR::Block* block, uint32_t vector_limit, Translator(IR::ValueProgram& program, IR::Block* block, uint32_t vector_limit,
@@ -1,18 +1,16 @@
#include "graphics/shader/recompiler/ir/ValueProgram.h" #include "graphics/shader/recompiler/ir/ValueProgram.h"
#include "graphics/shader/shader.h"
#include <fmt/format.h> #include <fmt/format.h>
#include <map> #include <map>
#include <unordered_set> #include <unordered_set>
namespace Libs::Graphics::ShaderRecompiler::IR { namespace Libs::Graphics::ShaderRecompiler::IR {
namespace {
bool Fail(std::string* error, const std::string& message) { using ShaderError::Fail;
if (error != nullptr) {
*error = message; namespace {
}
return false;
}
bool IsRegisterGet(ValueOpcode opcode) { bool IsRegisterGet(ValueOpcode opcode) {
switch (opcode) { switch (opcode) {
@@ -2,6 +2,7 @@
#include "graphics/shader/recompiler/ir/ValueProgram.h" #include "graphics/shader/recompiler/ir/ValueProgram.h"
#include "graphics/shader/recompiler/ir/passes/SrtWalker.h" #include "graphics/shader/recompiler/ir/passes/SrtWalker.h"
#include "graphics/shader/shader.h"
#include <algorithm> #include <algorithm>
#include <fmt/format.h> #include <fmt/format.h>
@@ -215,11 +216,9 @@ private:
}; };
bool Fail(uint32_t pc, std::string* error, const std::string& reason) const { bool Fail(uint32_t pc, std::string* error, const std::string& reason) const {
if (error != nullptr) { return ShaderError::Fail(
*error = fmt::format("shader resource tracking: hash=0x{:016x} stage={} pc=0x{:08x} {}", error, fmt::format("shader resource tracking: hash=0x{:016x} stage={} pc=0x{:08x} {}",
m_program.shader_hash, StageName(m_program.stage), pc, reason); m_program.shader_hash, StageName(m_program.stage), pc, reason));
}
return false;
} }
struct AddressPart { struct AddressPart {
@@ -1,6 +1,7 @@
#include "graphics/shader/recompiler/ir/passes/SrtWalker.h" #include "graphics/shader/recompiler/ir/passes/SrtWalker.h"
#include "graphics/shader/recompiler/ir/ValueProgram.h" #include "graphics/shader/recompiler/ir/ValueProgram.h"
#include "graphics/shader/shader.h"
#include <algorithm> #include <algorithm>
#include <bit> #include <bit>
@@ -314,10 +315,7 @@ private:
}; };
bool Fail(uint32_t pc, std::string* error, const std::string& message) const { bool Fail(uint32_t pc, std::string* error, const std::string& message) const {
if (error != nullptr) { return ShaderError::Fail(error, Diagnostic(m_program, pc, message));
*error = Diagnostic(m_program, pc, message);
}
return false;
} }
bool Collect(Value value, uint32_t use_pc, std::string* error) { bool Collect(Value value, uint32_t use_pc, std::string* error) {
@@ -434,10 +432,7 @@ public:
private: private:
bool Fail(std::string* error, const std::string& message) const { bool Fail(std::string* error, const std::string& message) const {
if (error != nullptr) { return ShaderError::Fail(error, Diagnostic(m_program, m_use_pc, message));
*error = Diagnostic(m_program, m_use_pc, message);
}
return false;
} }
bool EvaluateWide(Value value, uint64_t& result, std::string* error) { bool EvaluateWide(Value value, uint64_t& result, std::string* error) {
+12
View File
@@ -10,10 +10,22 @@
#include <memory> #include <memory>
#include <span> #include <span>
#include <string> #include <string>
#include <string_view>
#include <vector> #include <vector>
namespace Libs::Graphics { namespace Libs::Graphics {
namespace ShaderError {
[[nodiscard]] inline bool Fail(std::string* error, std::string_view message) {
if (error != nullptr) {
error->assign(message.data(), message.size());
}
return false;
}
} // namespace ShaderError
namespace HW { namespace HW {
struct VertexShaderInfo; struct VertexShaderInfo;
struct PixelShaderInfo; struct PixelShaderInfo;
+9 -19
View File
@@ -5,21 +5,10 @@
namespace Libs::Graphics { namespace Libs::Graphics {
namespace {
bool Fail(std::string* error, const char* message) {
if (error != nullptr) {
*error = message;
}
return false;
}
} // namespace
bool ShaderReadVertexMetadata(const ShaderMappedData& data, uint32_t max_user_sgprs, bool ShaderReadVertexMetadata(const ShaderMappedData& data, uint32_t max_user_sgprs,
ShaderVertexMetadata& metadata, std::string* error) { ShaderVertexMetadata& metadata, std::string* error) {
if (data.user_data == nullptr) { if (data.user_data == nullptr) {
return Fail(error, "missing AGC user-data header"); return ShaderError::Fail(error, "missing AGC user-data header");
} }
ShaderUserData user_data {}; ShaderUserData user_data {};
@@ -28,7 +17,8 @@ bool ShaderReadVertexMetadata(const ShaderMappedData& data, uint32_t max_user_sg
constexpr uint32_t DirectResourceCount = constexpr uint32_t DirectResourceCount =
static_cast<uint32_t>(AgcDirectResourceType::Last) + 1u; static_cast<uint32_t>(AgcDirectResourceType::Last) + 1u;
if (user_data.direct_resource_count > DirectResourceCount) { if (user_data.direct_resource_count > DirectResourceCount) {
return Fail(error, "AGC direct-resource count exceeds the known resource domain"); return ShaderError::Fail(error,
"AGC direct-resource count exceeds the known resource domain");
} }
std::array<uint16_t, DirectResourceCount> direct_offsets {}; std::array<uint16_t, DirectResourceCount> direct_offsets {};
@@ -36,7 +26,7 @@ bool ShaderReadVertexMetadata(const ShaderMappedData& data, uint32_t max_user_sg
static_cast<uint64_t>(user_data.direct_resource_count) * sizeof(uint16_t); static_cast<uint64_t>(user_data.direct_resource_count) * sizeof(uint16_t);
if (direct_size != 0) { if (direct_size != 0) {
if (user_data.direct_resource_offset == nullptr) { if (user_data.direct_resource_offset == nullptr) {
return Fail(error, "missing AGC direct-resource offsets"); return ShaderError::Fail(error, "missing AGC direct-resource offsets");
} }
std::memcpy(direct_offsets.data(), user_data.direct_resource_offset, direct_size); std::memcpy(direct_offsets.data(), user_data.direct_resource_offset, direct_size);
} }
@@ -57,31 +47,31 @@ bool ShaderReadVertexMetadata(const ShaderMappedData& data, uint32_t max_user_sg
} }
if (next.vertex_attrib_reg >= 0 && next.vertex_buffer_reg < 0) { if (next.vertex_attrib_reg >= 0 && next.vertex_buffer_reg < 0) {
return Fail(error, "vertex attribute table requires a vertex buffer table"); return ShaderError::Fail(error, "vertex attribute table requires a vertex buffer table");
} }
if (next.vertex_buffer_reg < 0) { if (next.vertex_buffer_reg < 0) {
metadata = next; metadata = next;
return true; return true;
} }
if (static_cast<uint32_t>(next.vertex_buffer_reg) + 1u >= max_user_sgprs) { if (static_cast<uint32_t>(next.vertex_buffer_reg) + 1u >= max_user_sgprs) {
return Fail(error, "vertex table pointer exceeds the user-SGPR domain"); return ShaderError::Fail(error, "vertex table pointer exceeds the user-SGPR domain");
} }
if (next.vertex_attrib_reg < 0) { if (next.vertex_attrib_reg < 0) {
metadata = next; metadata = next;
return true; return true;
} }
if (static_cast<uint32_t>(next.vertex_attrib_reg) + 1u >= max_user_sgprs) { if (static_cast<uint32_t>(next.vertex_attrib_reg) + 1u >= max_user_sgprs) {
return Fail(error, "vertex table pointer exceeds the user-SGPR domain"); return ShaderError::Fail(error, "vertex table pointer exceeds the user-SGPR domain");
} }
if (data.num_input_semantics == 0 || if (data.num_input_semantics == 0 ||
data.num_input_semantics > ShaderVertexInputInfo::RES_MAX) { data.num_input_semantics > ShaderVertexInputInfo::RES_MAX) {
return Fail(error, "vertex semantic count is outside the supported domain"); return ShaderError::Fail(error, "vertex semantic count is outside the supported domain");
} }
const auto semantic_size = const auto semantic_size =
static_cast<uint64_t>(data.num_input_semantics) * sizeof(ShaderSemantic); static_cast<uint64_t>(data.num_input_semantics) * sizeof(ShaderSemantic);
if (data.input_semantics == nullptr) { if (data.input_semantics == nullptr) {
return Fail(error, "missing vertex input semantics"); return ShaderError::Fail(error, "missing vertex input semantics");
} }
std::memcpy(next.input_semantics.data(), data.input_semantics, semantic_size); std::memcpy(next.input_semantics.data(), data.input_semantics, semantic_size);
next.input_semantics_count = data.num_input_semantics; next.input_semantics_count = data.num_input_semantics;