mirror of
https://github.com/open-goal/jak-project
synced 2026-08-06 18:03:30 -04:00
124 lines
3.4 KiB
C++
124 lines
3.4 KiB
C++
#pragma once
|
|
|
|
#include <map>
|
|
#include <optional>
|
|
#include <set>
|
|
#include <string>
|
|
#include <unordered_map>
|
|
#include <vector>
|
|
|
|
#include "common/common_types.h"
|
|
#include "common/custom_data/Tfrag3Data.h"
|
|
#include "common/util/FileUtil.h"
|
|
|
|
namespace decompiler {
|
|
struct ResolvedTextureData {
|
|
u16 w;
|
|
u16 h;
|
|
std::vector<u32> rgba;
|
|
};
|
|
|
|
struct TextureLRUCache {
|
|
using Entry = std::pair<u32, ResolvedTextureData>;
|
|
using List = std::list<Entry>;
|
|
using Iterator = List::iterator;
|
|
|
|
size_t capacity;
|
|
List entries; // front = most recently used
|
|
std::unordered_map<u32, Iterator> lookup;
|
|
|
|
TextureLRUCache(size_t capacity) : capacity(capacity) {}
|
|
|
|
ResolvedTextureData* get(u32 id) {
|
|
auto it = lookup.find(id);
|
|
if (it == lookup.end()) {
|
|
return nullptr;
|
|
}
|
|
entries.splice(entries.begin(), entries, it->second);
|
|
return &it->second->second;
|
|
}
|
|
|
|
void put(u32 id, ResolvedTextureData data) {
|
|
auto it = lookup.find(id);
|
|
if (it != lookup.end()) {
|
|
it->second->second = std::move(data);
|
|
entries.splice(entries.begin(), entries, it->second);
|
|
return;
|
|
}
|
|
|
|
entries.emplace_front(id, std::move(data));
|
|
lookup[id] = entries.begin();
|
|
|
|
if (lookup.size() > capacity) {
|
|
auto last = std::prev(entries.end());
|
|
lookup.erase(last->first);
|
|
entries.pop_back();
|
|
}
|
|
}
|
|
};
|
|
|
|
struct TextureDB {
|
|
TextureDB();
|
|
struct TextureData {
|
|
u16 w, h;
|
|
std::string name;
|
|
u32 page;
|
|
u32 dest = -1;
|
|
std::vector<u32> rgba_bytes;
|
|
u32 num_mips = -1;
|
|
};
|
|
|
|
std::map<u32, TextureData> textures;
|
|
std::unordered_map<u32, std::string> tpage_names;
|
|
std::unordered_map<std::string, std::set<u32>> texture_ids_per_level;
|
|
std::optional<fs::path> merge_texture_dir;
|
|
std::optional<fs::path> replace_texture_dir;
|
|
TextureLRUCache replacement_cache;
|
|
|
|
// special textures for animation.
|
|
std::map<u32, tfrag3::IndexTexture> index_textures_by_combo_id;
|
|
|
|
std::unordered_map<std::string, u32> animated_tex_output_to_anim_slot;
|
|
|
|
ResolvedTextureData resolve_texture(u32 id);
|
|
|
|
static constexpr int kPlaceholderWhiteTexturePage = INT16_MAX;
|
|
static constexpr int kPlaceholderWhiteTextureId = 0;
|
|
|
|
void add_texture(u32 tpage,
|
|
u32 texid,
|
|
const std::vector<u32>& data,
|
|
u16 w,
|
|
u16 h,
|
|
const std::string& tex_name,
|
|
const std::string& tpage_name,
|
|
const std::vector<std::string>& level_names,
|
|
u32 num_mips,
|
|
u32 dest);
|
|
|
|
void add_index_texture(u32 tpage,
|
|
u32 texid,
|
|
const std::vector<u8>& index_data,
|
|
const std::array<math::Vector4<u8>, 256>& clut,
|
|
u16 w,
|
|
u16 h,
|
|
const std::string& tex_name,
|
|
const std::string& tpage_name,
|
|
const std::vector<std::string>& level_names);
|
|
|
|
void merge_textures(const fs::path& base_path);
|
|
void replace_textures(const fs::path& path);
|
|
void merge_texture(u32 id, std::vector<u32>& rgba) const;
|
|
std::optional<ResolvedTextureData> replace_texture(u32 id);
|
|
|
|
std::string generate_texture_dest_adjustment_table() const;
|
|
};
|
|
|
|
// used by decompiler for texture macros
|
|
struct TexInfo {
|
|
std::string name;
|
|
std::string tpage_name;
|
|
u32 idx;
|
|
};
|
|
} // namespace decompiler
|