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
1 change: 1 addition & 0 deletions lib/gfx/texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ struct TextureRef {
uint32_t mipCount;
u32 gxFormat;
bool hasArbitraryMips = false;
bool isReplacement = false;

TextureRef(wgpu::Texture texture, wgpu::TextureView sampleTextureView, wgpu::TextureView attachmentTextureView,
wgpu::Extent3D size, wgpu::TextureFormat format, uint32_t mipCount, u32 gxFormat)
Expand Down
39 changes: 5 additions & 34 deletions lib/gfx/texture_replacement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ struct CachedReplacement {

struct ReplacementIndexEntry {
std::filesystem::path path;
bool hasMips;
};

absl::flat_hash_map<RuntimeTextureKey, ReplacementIndexEntry> s_replacementIndex;
Expand Down Expand Up @@ -284,7 +283,7 @@ std::string format_replacement_filename(const RuntimeTextureKey& key) {
return fmt::format("tex1_{}x{}_{:016x}_{}.dds", key.width, key.height, key.textureHash, key.format);
}

std::optional<std::pair<RuntimeTextureKey, bool>> parse_replacement_filename(std::string_view filename) noexcept {
std::optional<RuntimeTextureKey> parse_replacement_filename(std::string_view filename) noexcept {
const size_t dot = filename.rfind('.');
if (dot == std::string_view::npos) {
return std::nullopt;
Expand Down Expand Up @@ -323,9 +322,7 @@ std::optional<std::pair<RuntimeTextureKey, bool>> parse_replacement_filename(std
}

size_t index = 2;
bool hasMips = false;
if (parts[index] == "m") {
hasMips = true;
++index;
}

Expand Down Expand Up @@ -370,15 +367,14 @@ std::optional<std::pair<RuntimeTextureKey, bool>> parse_replacement_filename(std
}
}

RuntimeTextureKey key{
return RuntimeTextureKey{
.textureHash = textureHash,
.tlutHash = tlutHash,
.width = dimensions->first,
.height = dimensions->second,
.hasTlut = hasTlut,
.format = *format,
};
return {{key, hasMips}};
}

static std::optional<ConvertedTexture> load_texture_file(const std::filesystem::path& path) {
Expand Down Expand Up @@ -425,10 +421,6 @@ std::optional<ConvertedTexture> load_replacement(const ReplacementIndexEntry& en
return std::nullopt;
}

if (!entry.hasMips) {
return base;
}

std::vector<ConvertedTexture> more;
std::error_code ec;
for (uint32_t mipLevel = 1;; ++mipLevel) {
Expand All @@ -455,7 +447,7 @@ std::optional<ConvertedTexture> load_replacement(const ReplacementIndexEntry& en
}

if (more.empty()) {
return std::nullopt;
return base;
}

const uint32_t mips = 1u + static_cast<uint32_t>(more.size());
Expand Down Expand Up @@ -571,7 +563,7 @@ void build_index() noexcept {
continue;
}

s_replacementIndex.try_emplace(parsed->first, path, parsed->second);
s_replacementIndex.try_emplace(*parsed, path);
}

Log.info("Indexed {} texture replacements", s_replacementIndex.size());
Expand Down Expand Up @@ -642,6 +634,7 @@ gfx::TextureHandle load_replacement_texture(const RuntimeTextureKey& key, const
auto textureView = texture.CreateView(&textureViewDescriptor);
auto handle = std::make_shared<gfx::TextureRef>(std::move(texture), std::move(textureView), wgpu::TextureView{}, size,
replacement->format, replacement->mips, gfx::InvalidTextureFormat);
handle->isReplacement = true;
gfx::write_texture(*handle, replacement->data);
return handle;
}
Expand All @@ -656,15 +649,6 @@ void cache_replacement(const RuntimeTextureKey& key, const gfx::TextureHandle& h
evict_replacement_cache_if_needed();
}

void bind_replacement(GXTexObj_& obj, GXTexMapID id, const gfx::TextureHandle& handle) noexcept {
GXTexObj_ out = obj;
out.mWidth = handle->size.width;
out.mHeight = handle->size.height;
out.mFormat = GX_TF_RGBA8_PC;
g_gxState.textures[id] = gfx::TextureBind(out, handle);
g_gxState.stateDirty = true;
}

bool dump_editable_texture_dds(const RuntimeTextureKey& key, const GXTexObj_& obj) noexcept {
const ArrayRef<uint8_t> texData{static_cast<const uint8_t*>(obj.data), UINT32_MAX};
const uint32_t texWidth = obj.width();
Expand Down Expand Up @@ -759,19 +743,6 @@ void load_tlut(const GXTlutObj* obj, uint32_t idx) noexcept {
};
}

bool try_bind_replacement(GXTexObj_& obj, GXTexMapID id) noexcept {
if (!g_config.allowTextureReplacements) {
return false;
}

const auto handle = find_replacement(obj);
if (!handle.has_value()) {
return false;
}
bind_replacement(obj, id, *handle);
return true;
}

std::optional<TextureHandle> find_replacement(const GXTexObj_& obj) noexcept {
ZoneScoped;

Expand Down
1 change: 0 additions & 1 deletion lib/gfx/texture_replacement.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ void register_tlut(const GXTlutObj* obj, const void* data, GXTlutFmt format, uin
void load_tlut(const GXTlutObj* obj, uint32_t idx) noexcept;
std::optional<TextureHandle> find_replacement(const GXTexObj_& obj) noexcept;
std::string build_texture_replacement_name(const GXTexObj_& obj) noexcept;
bool try_bind_replacement(GXTexObj_& obj, GXTexMapID id) noexcept;
} // namespace aurora::gfx::texture_replacement
9 changes: 7 additions & 2 deletions lib/gx/gx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -985,11 +985,16 @@ static u16 wgpu_aniso(GXAnisotropy aniso) {
}

wgpu::SamplerDescriptor aurora::gfx::TextureBind::get_descriptor() const noexcept {
const auto [minFilter, mipFilter] = wgpu_filter_mode(texObj.min_filter());
auto [minFilter, mipFilter] = wgpu_filter_mode(texObj.min_filter());
const auto [magFilter, _] = wgpu_filter_mode(texObj.mag_filter());
float minLod = texObj.min_lod();
float maxLod = texObj.max_lod();
if (mipFilter == wgpu::MipmapFilterMode::Undefined) {
if (ref && ref->isReplacement) {
minFilter = wgpu::FilterMode::Linear;
mipFilter = wgpu::MipmapFilterMode::Linear;
minLod = 0.f;
maxLod = static_cast<float>(std::max(ref->mipCount, 1u) - 1u);
} else if (mipFilter == wgpu::MipmapFilterMode::Undefined) {
minLod = 0.f;
maxLod = 0.f;
}
Expand Down
1 change: 0 additions & 1 deletion tests/gx_test_stubs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,6 @@ u32 compute_texture_upload_size(const GXTexObj_& obj) noexcept { return 0; }
void register_tlut(const GXTlutObj*, const void*, GXTlutFmt, u16) noexcept {}
void load_tlut(const GXTlutObj*, u32) noexcept {}
std::optional<TextureHandle> find_replacement(const GXTexObj_&) noexcept { return std::nullopt; }
bool try_bind_replacement(GXTexObj_&, GXTexMapID) noexcept { return false; }
} // namespace aurora::gfx::texture_replacement

// --- Window stub ---
Expand Down
Loading