Skip to content

Commit

Permalink
VideoCommon: add ability to overwrite texture filtering or wrap modes…
Browse files Browse the repository at this point in the history
… on individual cache entries
  • Loading branch information
iwubcode committed Oct 30, 2022
1 parent a27c919 commit 297ebb8
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@

#pragma once

#include <optional>
#include <string_view>

#include "Common/CommonTypes.h"
#include "Common/Matrix.h"

#include "VideoCommon/RenderState.h"

namespace GraphicsModActionData
{
struct DrawStarted
Expand All @@ -31,5 +34,10 @@ struct Projection
struct TextureLoad
{
std::string_view texture_name;
std::optional<FilterMode>* min_filter;
std::optional<FilterMode>* mag_filter;
std::optional<FilterMode>* mipmap_filter;
std::optional<WrapMode>* wrap_u;
std::optional<WrapMode>* wrap_v;
};
} // namespace GraphicsModActionData
48 changes: 45 additions & 3 deletions Source/Core/VideoCommon/TextureCacheBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,11 @@ static bool IsAnisostropicEnhancementSafe(const TexMode0& tm0)
}

static void SetSamplerState(u32 index, float custom_tex_scale, bool custom_tex,
bool has_arbitrary_mips)
bool has_arbitrary_mips, std::optional<FilterMode> custom_min_filter,
std::optional<FilterMode> custom_mag_filter,
std::optional<FilterMode> custom_mipmap_filter,
std::optional<WrapMode> custom_wrap_u,
std::optional<WrapMode> custom_wrap_v)
{
const TexMode0& tm0 = bpmem.tex.GetUnit(index).texMode0;

Expand Down Expand Up @@ -1046,6 +1050,40 @@ static void SetSamplerState(u32 index, float custom_tex_scale, bool custom_tex,
state.tm0.anisotropic_filtering = false;
}

if (custom_min_filter)
{
state.tm0.min_filter = *custom_min_filter;

// Overriding our settings with a custom value also disables ansiotropic filtering if enabled
state.tm0.anisotropic_filtering = false;
}

if (custom_mag_filter)
{
state.tm0.mag_filter = *custom_mag_filter;

// Overriding our settings with a custom value also disables ansiotropic filtering if enabled
state.tm0.anisotropic_filtering = false;
}

if (custom_mipmap_filter)
{
state.tm0.mipmap_filter = *custom_mipmap_filter;

// Overriding our settings with a custom value also disables ansiotropic filtering if enabled
state.tm0.anisotropic_filtering = false;
}

if (custom_wrap_u)
{
state.tm0.wrap_u = *custom_wrap_u;
}

if (custom_wrap_v)
{
state.tm0.wrap_v = *custom_wrap_v;
}

g_renderer->SetSamplerState(index, state);
PixelShaderManager::SetSamplerState(index, state.tm0.hex, state.tm1.hex);
}
Expand All @@ -1061,7 +1099,9 @@ void TextureCacheBase::BindTextures(BitSet32 used_textures)
PixelShaderManager::SetTexDims(i, tentry->native_width, tentry->native_height);

const float custom_tex_scale = tentry->GetWidth() / float(tentry->native_width);
SetSamplerState(i, custom_tex_scale, tentry->is_custom_tex, tentry->has_arbitrary_mips);
SetSamplerState(i, custom_tex_scale, tentry->is_custom_tex, tentry->has_arbitrary_mips,
tentry->custom_min_filter, tentry->custom_mag_filter,
tentry->custom_mipmap_filter, tentry->custom_wrap_u, tentry->custom_wrap_v);
}
}

Expand Down Expand Up @@ -1244,7 +1284,9 @@ TextureCacheBase::TCacheEntry* TextureCacheBase::Load(const TextureInfo& texture
{
entry->texture_info_name = texture_info.CalculateTextureName().GetFullName();

GraphicsModActionData::TextureLoad texture_load{entry->texture_info_name};
GraphicsModActionData::TextureLoad texture_load{
entry->texture_info_name, &entry->custom_min_filter, &entry->custom_mag_filter,
&entry->custom_mipmap_filter, &entry->custom_wrap_u, &entry->custom_wrap_v};
for (const auto action :
g_renderer->GetGraphicsModManager().GetTextureLoadActions(entry->texture_info_name))
{
Expand Down
7 changes: 7 additions & 0 deletions Source/Core/VideoCommon/TextureCacheBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "Common/MathUtil.h"
#include "VideoCommon/AbstractTexture.h"
#include "VideoCommon/BPMemory.h"
#include "VideoCommon/RenderState.h"
#include "VideoCommon/TextureConfig.h"
#include "VideoCommon/TextureDecoder.h"
#include "VideoCommon/TextureInfo.h"
Expand Down Expand Up @@ -158,6 +159,12 @@ class TextureCacheBase

std::string texture_info_name = "";

std::optional<FilterMode> custom_min_filter;
std::optional<FilterMode> custom_mag_filter;
std::optional<FilterMode> custom_mipmap_filter;
std::optional<WrapMode> custom_wrap_u;
std::optional<WrapMode> custom_wrap_v;

explicit TCacheEntry(std::unique_ptr<AbstractTexture> tex,
std::unique_ptr<AbstractFramebuffer> fb);

Expand Down

0 comments on commit 297ebb8

Please sign in to comment.