Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 48 additions & 6 deletions rpcs3/Emu/RSX/Program/CgBinaryProgram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,34 +77,62 @@ std::string CgBinaryDisasm::GetCgParamValue(u32 offset, u32 end_offset) const
return fmt::format("num %d ", num) + offsets;
}

void CgBinaryDisasm::ConvertToLE(CgBinaryProgram& prog)
bool CgBinaryDisasm::ConvertToLE(CgBinaryProgram& prog)
{
// BE payload, requires that data be swapped
const auto be_profile = prog.profile;

auto swap_be32 = [&](u32 start_offset, size_t size_bytes)
{
if (static_cast<usz>(start_offset) > m_buffer.size()
|| size_bytes > m_buffer.size() - start_offset
|| start_offset % alignof(u32)
|| size_bytes % sizeof(u32))
{
return false;
}

auto start = reinterpret_cast<u32*>(m_buffer.data() + start_offset);
auto end = reinterpret_cast<u32*>(m_buffer.data() + start_offset + size_bytes);

for (auto data = start; data < end; ++data)
{
*data = std::bit_cast<be_t<u32>>(*data);
}

return true;
};

// 1. Swap the header
swap_be32(0, sizeof(CgBinaryProgram));
if (!swap_be32(0, sizeof(CgBinaryProgram)))
{
return false;
}

// 2. Swap parameters
swap_be32(prog.parameterArray, sizeof(CgBinaryParameter) * prog.parameterCount);
if (static_cast<usz>(prog.parameterCount) > static_cast<usz>(umax) / sizeof(CgBinaryParameter)
|| !swap_be32(prog.parameterArray, sizeof(CgBinaryParameter) * prog.parameterCount))
{
return false;
}

// 3. Swap the ucode
swap_be32(prog.ucode, m_buffer.size() - prog.ucode);
if (static_cast<usz>(prog.ucode) > m_buffer.size()
|| !swap_be32(prog.ucode, m_buffer.size() - prog.ucode))
{
return false;
}

// 4. Swap the domain header
if (be_profile == 7004u)
{
if (static_cast<usz>(prog.program) > m_buffer.size()
|| sizeof(CgBinaryFragmentProgram) > m_buffer.size() - prog.program
|| prog.program % alignof(u32))
{
return false;
}

// Need to swap each field individually
auto& fprog = GetCgRef<CgBinaryFragmentProgram>(prog.program);
fprog.instructionCount = std::bit_cast<be_t<u32>>(fprog.instructionCount);
Expand All @@ -117,20 +145,34 @@ void CgBinaryDisasm::ConvertToLE(CgBinaryProgram& prog)
else
{
// Swap entire header block as all fields are u32
swap_be32(prog.program, sizeof(CgBinaryVertexProgram));
if (!swap_be32(prog.program, sizeof(CgBinaryVertexProgram)))
{
return false;
}
}

return true;
}

void CgBinaryDisasm::BuildShaderBody(bool include_glsl)
{
ParamArray param_array;

if (m_buffer.size() < sizeof(CgBinaryProgram))
{
return;
}

auto& prog = GetCgRef<CgBinaryProgram>(0);

if (const u32 be_profile = std::bit_cast<be_t<u32>>(prog.profile);
be_profile == 7003u || be_profile == 7004u)
{
ConvertToLE(prog);
if (!ConvertToLE(prog))
{
return;
}

ensure(be_profile == prog.profile);
}

Expand Down
2 changes: 1 addition & 1 deletion rpcs3/Emu/RSX/Program/CgBinaryProgram.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ class CgBinaryDisasm
std::string GetCgParamSemantic(u32 offset) const;
std::string GetCgParamValue(u32 offset, u32 end_offset) const;

void ConvertToLE(CgBinaryProgram& prog);
bool ConvertToLE(CgBinaryProgram& prog);
void BuildShaderBody(bool include_glsl = true);

static u32 GetData(const u32 d) { return d << 16 | d >> 16; }
Expand Down
Loading