Skip to content

Commit

Permalink
Fixed vtx overlaps
Browse files Browse the repository at this point in the history
  • Loading branch information
KiritoDv committed Mar 4, 2024
1 parent 1682451 commit 1712d54
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/Companion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "factories/BlobFactory.h"
#include "factories/LightsFactory.h"
#include "factories/mk64/WaypointFactory.h"
#include "factories/DisplayListOverrides.h"
#include "spdlog/spdlog.h"
#include "hj/sha1.h"

Expand Down Expand Up @@ -434,6 +435,7 @@ void Companion::Process() {
root = YAML::LoadFile(yamlPath);
this->gConfig.segment.local.clear();
this->gFileHeader.clear();
GFXDOverride::ClearVtx();

if(root[":config"]) {
this->ParseCurrentFileConfig(root[":config"]);
Expand Down
30 changes: 22 additions & 8 deletions src/factories/DisplayListFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,22 +125,27 @@ void DListCodeExporter::Export(std::ostream &write, std::shared_ptr<IParsedData>

void DebugDisplayList(uint32_t w0, uint32_t w1){
uint32_t dlist[] = {w0, w1};
char out[0xFFFF] = {0};

gfxd_input_buffer(dlist, sizeof(dlist));
gfxd_output_fd(fileno(stdout));
gfxd_output_buffer(out, sizeof(out));
// gfxd_output_fd(fileno(stdout));
gfxd_endian(gfxd_endian_host, sizeof(uint32_t));
gfxd_macro_fn([](){
gfxd_puts("> ");
gfxd_macro_dflt();
gfxd_puts("\n");
return 0;
});
// gfxd_macro_fn([](){
// gfxd_puts("> ");
// gfxd_macro_dflt();
// gfxd_puts("\n");
// return 0;
// });
gfxd_vtx_callback(GFXDOverride::Vtx);
gfxd_timg_callback(GFXDOverride::Texture);
gfxd_dl_callback(GFXDOverride::DisplayList);
gfxd_tlut_callback(GFXDOverride::Palette);
//gfxd_light_callback(GFXDOverride::Light);
GFXDSetGBIVersion();
gfxd_execute();

SPDLOG_INFO("{}", out);
}

void DListBinaryExporter::Export(std::ostream &write, std::shared_ptr<IParsedData> raw, std::string& entryName, YAML::Node &node, std::string* replacement ) {
Expand Down Expand Up @@ -267,6 +272,7 @@ std::optional<std::shared_ptr<IParsedData>> DListFactory::parse(std::vector<uint
auto [_, segment] = Decompressor::AutoDecode(node, raw_buffer);
LUS::BinaryReader reader(segment.data, segment.size);
reader.SetEndianness(LUS::Endianness::Big);

// Validate this
// reader.Seek(offset, LUS::SeekOffsetType::Start);

Expand All @@ -279,6 +285,10 @@ std::optional<std::shared_ptr<IParsedData>> DListFactory::parse(std::vector<uint

uint8_t opcode = w0 >> 24;

if(true){
DebugDisplayList(w0, w1);
}

if(opcode == GBI(G_ENDDL)) {
processing = false;
}
Expand Down Expand Up @@ -392,7 +402,11 @@ std::optional<std::shared_ptr<IParsedData>> DListFactory::parse(std::vector<uint
break;
}

if(const auto decl = Companion::Instance->GetNodeByAddr(w1); !decl.has_value()){
const auto decl = Companion::Instance->GetNodeByAddr(w1);

bool overlaps = GFXDOverride::SearchVTXOverlaps(w1, !decl.has_value() ? nvtx : std::get<YAML::Node>(decl.value())["count"].as<uint32_t>());

if(!decl.has_value() && !overlaps){
SPDLOG_INFO("Addr to Vtx array at 0x{:X} not in yaml, autogenerating it", w1);

auto ptr = w1;
Expand Down
51 changes: 48 additions & 3 deletions src/factories/DisplayListOverrides.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace GFXDOverride {

std::unordered_map<uint32_t, std::pair<uint32_t, uint32_t>> gVtxOverlapMap;

void Triangle2(const Gfx* gfx) {
auto w0 = gfx->words.w0;
auto w1 = gfx->words.w1;
Expand Down Expand Up @@ -47,14 +49,26 @@ void Quadrangle(const Gfx* gfx) {
}

int Vtx(uint32_t ptr, int32_t num) {
auto dec = Companion::Instance->GetNodeByAddr(ptr);
auto overlap = VtxOverlapping(ptr);
auto isOverlapping = overlap.has_value();
SPDLOG_INFO("Vtx: 0x{:X} Num: {} Overlapping: {}", ptr, num, isOverlapping);
auto dec = Companion::Instance->GetNodeByAddr(isOverlapping ? overlap.value().first : ptr);

if(dec.has_value()){
auto node = std::get<1>(dec.value());
auto symbol = GetSafeNode<std::string>(node, "symbol");
SPDLOG_INFO("Found Vtx: 0x{:X} Symbol: {}", ptr, symbol);
gfxd_puts(symbol.c_str());
return 1;
if(isOverlapping){
gfxd_puts("&");
gfxd_puts(symbol.c_str());
gfxd_puts("[");
gfxd_puts(std::to_string(overlap.value().second).c_str());
gfxd_puts("]");
return 1;
} else {
gfxd_puts(symbol.c_str());
return 1;
}
}

SPDLOG_WARN("Could not find vtx at 0x{:X}", ptr);
Expand Down Expand Up @@ -120,4 +134,35 @@ int DisplayList(uint32_t ptr) {
SPDLOG_WARN("Could not find display list to override at 0x{:X}", ptr);
return 0;
}

bool SearchVTXOverlaps(uint32_t ptr, uint32_t num) {
for(auto& [key, value] : gVtxOverlapMap){
auto [addr, count] = value;
if(ptr > addr && ptr <= (addr + (count * sizeof(Vtx_t)))){
SPDLOG_INFO("Found Vtx Overlap: 0x{:X} Overlaps with 0x{:X} Count: {}", ptr, addr, count);
return true;
}
}

SPDLOG_INFO("Vtx Overlap Not Found: 0x{:X} Count: {}", ptr, num);

gVtxOverlapMap[ptr] = std::make_pair(ptr, num);
return false;
}

void ClearVtx() {
gVtxOverlapMap.clear();
}

std::optional<std::pair<uint32_t, uint32_t>> VtxOverlapping(uint32_t ptr) {
for(auto& [key, value] : gVtxOverlapMap){
auto [addr, count] = value;
if(ptr > addr && ptr < addr + (count * sizeof(Vtx_t))){
auto diff = ptr - addr;
return std::make_pair(addr, diff / sizeof(Vtx_t));
}
}

return std::nullopt;
}
}
3 changes: 3 additions & 0 deletions src/factories/DisplayListOverrides.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,7 @@ int Texture(uint32_t timg, int32_t fmt, int32_t siz, int32_t width, int32_t hei
int Palette(uint32_t tlut, int32_t idx, int32_t count);
int Light(uint32_t lightsn, int32_t count);
int DisplayList(uint32_t dl);
bool SearchVTXOverlaps(uint32_t ptr, uint32_t num);
void ClearVtx();
std::optional<std::pair<uint32_t, uint32_t>> VtxOverlapping(uint32_t ptr);
};

0 comments on commit 1712d54

Please sign in to comment.