Skip to content

Commit

Permalink
GS: Pre-round/truncate STQ values based on hardware tests
Browse files Browse the repository at this point in the history
  • Loading branch information
refractionpcsx2 committed Nov 17, 2023
1 parent a26d7f6 commit d26a605
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
55 changes: 54 additions & 1 deletion pcsx2/GS/GSState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <cfloat>
#include <fstream>
#include <iomanip>
#include <bit>

int GSState::s_n = 0;
int GSState::s_last_transfer_draw_n = 0;
Expand Down Expand Up @@ -503,7 +504,7 @@ void GSState::DumpVertices(const std::string& filename)
file << uv_U << DEL << uv_V;
}
else
file << v.ST.S << DEL << v.ST.T << DEL << v.RGBAQ.Q;
file << v.ST.S << "(" << *(u32*)&v.ST.S << ")" << DEL << v.ST.T << "(" << *(u32*)&v.ST.T << ")" << DEL << v.RGBAQ.Q << "(" << *(u32*)&v.RGBAQ.Q << ")";

file << std::endl;
}
Expand Down Expand Up @@ -1593,6 +1594,21 @@ inline bool GSState::TestDrawChanged()
return false;
}

u32 GSState::CalcMask(int exp, int max_exp)
{
const int amount = 9 + (max_exp - exp);
u32 result = 0x1ff;

for (int i = 9; i < amount; i++)
{
result |= 1 << i;
}

result &= 0x7fffff;

return result;
}

void GSState::FlushPrim()
{
if (m_index.tail > 0)
Expand Down Expand Up @@ -1668,6 +1684,43 @@ void GSState::FlushPrim()

m_vt.Update(m_vertex.buff, m_index.buff, m_vertex.tail, m_index.tail, GSUtil::GetPrimClass(PRIM->PRIM));

// Texel coordinate rounding
// Helps Manhunt (lights shining through objects).
// Can help with some alignment issues when upscaling too, and is for both Software and Hardware renderers.
// Sometimes hardware doesn't get affected, likely due to the difference in how GPU's handle textures (Persona minimap).
if (m_env.PRIM.TME && (GSUtil::GetPrimClass(PRIM->PRIM) == GS_PRIM_CLASS::GS_SPRITE_CLASS || m_vt.m_eq.z))
{
if (!m_env.PRIM.FST) // STQ's
{
// ST's have the lowest 9 bits rounding down (from hardware tests)
for (u32 i = 0; i < m_vertex.tail; i++)
{
// Don't worry, I hate me too. If there's a modern way to do bit manipulation on floats, I'd love to hear it!
GSVertex* v = &m_vertex.buff[i];
int T = std::bit_cast<int>(v->ST.T);
int Q = std::bit_cast<int>(v->RGBAQ.Q);
int S = std::bit_cast<int>(v->ST.S);
const int expS = (S >> 23) & 0xff;
const int expT = (T >> 23) & 0xff;
const int expQ = (Q >> 23) & 0xff;
int max_exp = std::max(expS, expQ);

u32 mask = CalcMask(expS, max_exp);
S &= ~mask;
v->ST.S = std::bit_cast<float>(S);
max_exp = std::max(expT, expQ);
mask = CalcMask(expT, max_exp);
T &= ~mask;
v->ST.T = std::bit_cast<float>(T);
Q &= ~0xff;
v->RGBAQ.Q = std::bit_cast<float>(Q);

m_vt.m_min.t.x = std::min(m_vt.m_min.t.x, (v->ST.S / v->RGBAQ.Q) * (1 << m_context->TEX0.TW));
m_vt.m_min.t.y = std::min(m_vt.m_min.t.y, (v->ST.T / v->RGBAQ.Q) * (1 << m_context->TEX0.TH));
}
}
}

// Skip draw if Z test is enabled, but set to fail all pixels.
const bool skip_draw = (m_context->TEST.ZTE && m_context->TEST.ZTST == ZTST_NEVER);

Expand Down
1 change: 1 addition & 0 deletions pcsx2/GS/GSState.h
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ class GSState : public GSAlignedClass<32>
virtual void UpdateSettings(const Pcsx2Config::GSOptions& old_config);

void Flush(GSFlushReason reason);
u32 CalcMask(int exp, int max_exp);
void FlushPrim();
bool TestDrawChanged();
void FlushWrite();
Expand Down

0 comments on commit d26a605

Please sign in to comment.