summaryrefslogtreecommitdiff
path: root/src/factories/oot/DeferredVtx.cpp
blob: 9ea76d05568fd2f55e966e2cde1a5b232274bc99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "DeferredVtx.h"
#include "Companion.h"
#include "factories/DisplayListOverrides.h"
#include "n64/CommandMacros.h"
#include "spdlog/spdlog.h"
#include <algorithm>
#include <iomanip>
#include <sstream>

// N64 vertex size in bytes (matching N64Vtx_t: 3*int16 + uint16 + 2*int16 + 4*uchar = 16)
static constexpr uint32_t kVtxSize = 16;

// Deferred VTX consolidation state (ZAPD-style MergeConnectingVertexLists).
// ZAPD merges VTX per-DList (each DList has its own vertices map and merge pass).
// We collect VTX during each DList parse call and flush at the end of that parse.
namespace DeferredVtx {

bool sDeferred = false;
std::vector<PendingVtx> sPendingList;

void BeginDefer() {
    sDeferred = true;
    sPendingList.clear();
}

bool IsDeferred() {
    return sDeferred;
}

std::vector<PendingVtx> SaveAndClearPending() {
    auto saved = std::move(sPendingList);
    sPendingList.clear();
    return saved;
}

void RestorePending(std::vector<PendingVtx>& saved) {
    // Prepend saved items to current pending list (in case anything was added during the save)
    saved.insert(saved.end(), sPendingList.begin(), sPendingList.end());
    sPendingList = std::move(saved);
}

void AddPending(uint32_t addr, uint32_t count) {
    sPendingList.push_back({addr, count});
}

// Flush pending VTX for a single DList: merge adjacent arrays and register assets.
// Called at the end of each DList parse() to match ZAPD's per-DList merge scope.
void FlushDeferred(const std::string& baseName) {
    // Don't clear sDeferred here — it stays active for the entire room.
    // Each DList parse flushes its own collected VTX.
    auto pending = std::move(sPendingList);
    sPendingList.clear();

    if (pending.empty()) {
        return;
    }

    SPDLOG_INFO("VTX FlushDeferred: {} pending VTX for {}", pending.size(), baseName);

    // Sort by segment offset
    std::sort(pending.begin(), pending.end(),
        [](const PendingVtx& a, const PendingVtx& b) {
            return SEGMENT_OFFSET(a.addr) < SEGMENT_OFFSET(b.addr);
        });

    // Merge adjacent/overlapping VTX arrays (ZAPD's MergeConnectingVertexLists algorithm).
    // Two arrays merge if the first array's end >= the second's start.
    struct MergedVtx {
        uint32_t addr;      // segment address of start
        uint32_t endOff;    // segment offset of end (exclusive)
    };
    std::vector<MergedVtx> merged;

    for (auto& pv : pending) {
        uint32_t startOff = SEGMENT_OFFSET(pv.addr);
        uint32_t endOff = startOff + pv.count * kVtxSize;

        if (merged.empty() || startOff > merged.back().endOff) {
            // New group
            merged.push_back({pv.addr, endOff});
        } else {
            // Extend existing group
            if (endOff > merged.back().endOff) {
                merged.back().endOff = endOff;
            }
        }
    }

    // Register each merged VTX group as an asset
    for (auto& mg : merged) {
        uint32_t startOff = SEGMENT_OFFSET(mg.addr);
        uint32_t totalBytes = mg.endOff - startOff;
        uint32_t totalCount = totalBytes / kVtxSize;

        // Build proper symbol: baseName + "Vtx_" + 6-digit hex offset
        std::ostringstream ss;
        ss << baseName << "Vtx_" << std::uppercase << std::hex
           << std::setfill('0') << std::setw(6) << startOff;
        std::string symbol = ss.str();

        SPDLOG_INFO("VTX consolidation: {} at 0x{:X} count={}", symbol, mg.addr, totalCount);

        // Look up the pre-declared VTX in YAML (should exist with enrichment)
        auto registeredNode = Companion::Instance->GetNodeByAddr(mg.addr);
        if (!registeredNode.has_value()) {
            SPDLOG_WARN("Undeclared VTX at 0x{:X} — YAML enrichment incomplete", mg.addr);
        }

        // Register overlap mappings for all pending addresses within this group.
        if (registeredNode.has_value()) {
            auto [fullPath, vtxNode] = registeredNode.value();
            auto overlapTuple = std::make_tuple(symbol, vtxNode);
            for (auto& pv : pending) {
                uint32_t pvOff = SEGMENT_OFFSET(pv.addr);
                if (pvOff > startOff && pvOff < mg.endOff) {
                    GFXDOverride::RegisterVTXOverlap(pv.addr, overlapTuple);
                }
            }
        }
    }
}

void EndDefer() {
    sDeferred = false;
    sPendingList.clear();
}

} // namespace DeferredVtx