Skip to content

Commit

Permalink
GS: Code for new UV rounding method to grab enough of texture.
Browse files Browse the repository at this point in the history
  • Loading branch information
TJ committed Dec 29, 2024
1 parent 6a0f811 commit 6f79d62
Showing 1 changed file with 58 additions and 24 deletions.
82 changes: 58 additions & 24 deletions pcsx2/GS/GSState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3842,21 +3842,51 @@ GSState::TextureMinMaxResult GSState::GetTextureMinMax(GIFRegTEX0 TEX0, GIFRegCL
{
// Optimisation aims to reduce the amount of texture loaded to only the bit which will be read
GSVector4 st = m_vt.m_min.t.xyxy(m_vt.m_max.t);
if (linear)
{
st += GSVector4(-0.5f, 0.5f).xxyy();

// If it's the start of the texture and our little adjustment is all that pushed it over, clamp it to 0.
// This stops the border check failing when using repeat but needed less than the full texture
// since this was making it take the full texture even though it wasn't needed.
if (!clamp_to_tsize && ((m_vt.m_min.t.floor() == GSVector4::zero()).mask() & 0x3) == 0x3)
st = st.max(GSVector4::zero());
}
// FIXME: Commenting this out for now to try a different approach
//if (linear)
//{
// st += GSVector4(-0.5f, 0.5f).xxyy();

// // If it's the start of the texture and our little adjustment is all that pushed it over, clamp it to 0.
// // This stops the border check failing when using repeat but needed less than the full texture
// // since this was making it take the full texture even though it wasn't needed.
// if (!clamp_to_tsize && ((m_vt.m_min.t.floor() == GSVector4::zero()).mask() & 0x3) == 0x3)
// st = st.max(GSVector4::zero());
//}

// draw will get scissored, adjust UVs to suit

const GSVector2 pos_range(std::max(m_vt.m_max.p.x - m_vt.m_min.p.x, 1.0f), std::max(m_vt.m_max.p.y - m_vt.m_min.p.y, 1.0f));
const GSVector2 uv_range(m_vt.m_max.t.x - m_vt.m_min.t.x, m_vt.m_max.t.y - m_vt.m_min.t.y);

// Find the top-left and bottom-right pixels contained in the bounding box
// Format: (x_min, y_min, x_max, y_max)
GSVector4 p_int = m_vt.m_min.p.ceil().xyxy(m_vt.m_max.p.floor());

// FIXME: I don't know how to do this with SIMD...
// We want exclusive range for maximum because right/bottom edges are not drawn by GS rules
if (p_int.z == m_vt.m_max.p.x)
{
p_int.z -= 1.0f;
}
if (p_int.w == m_vt.m_max.p.y)
{
p_int.w -= 1.0f;
}

const GSVector2 grad(uv_range / pos_range);

// Linear interpolation the UV coordinates at the minimum/maximum pixel centers in the bounding box
// FIXME: This assume that the minmum U corresponds to the minimnum X and same for V/Y =/
GSVector4 t_interp = m_vt.m_min.t.xyxy() + (p_int - m_vt.m_min.p.xyxy()) * GSVector4(grad.x, grad.y, grad.x, grad.y);

if (linear)
{
// Get the top-left interpolation UV for the min values and the bottom-right for the max values
t_interp = (t_interp - 0.5).floor();
t_interp = t_interp.xyzw(t_interp + 1);
}

// Adjust texture range when sprites get scissor clipped. Since we linearly interpolate, this
// optimization doesn't work when perspective correction is enabled.
if (m_vt.m_primclass == GS_SPRITE_CLASS && PRIM->FST == 1 && m_primitive_covers_without_gaps != NoGapsType::GapsFound)
Expand All @@ -3877,6 +3907,8 @@ GSState::TextureMinMaxResult GSState::GetTextureMinMax(GIFRegTEX0 TEX0, GIFRegCL
const bool x_forward = vert_first->XYZ.X < vert_second->XYZ.X;
const bool swap_x = u_forward != x_forward;

// FIXME: I believe that it is incorrect to use the floored/ceiled values for interpolation
// because the gradient is calculated differently. Better to use the actual values of X, Y in the register above.
if (int_rc.left < scissored_rc.left)
{
if (!swap_x)
Expand Down Expand Up @@ -3924,35 +3956,34 @@ GSState::TextureMinMaxResult GSState::GetTextureMinMax(GIFRegTEX0 TEX0, GIFRegCL
}
}

const GSVector4i uv = GSVector4i(st.floor());
const GSVector4i uv(t_interp);

uses_border = GSVector4::cast((uv < vr).blend32<0xc>(uv >= vr)).mask();

// Need to make sure we don't oversample, this can cause trouble in grabbing textures.
// This may be inaccurate depending on the draw, but adding 1 all the time is wrong too.
const int inclusive_x_req = ((m_vt.m_primclass < GS_TRIANGLE_CLASS) || (grad.x < 1.0f || (grad.x == 1.0f && m_vt.m_max.p.x != floor(m_vt.m_max.p.x)))) ? 1 : 0;
const int inclusive_y_req = ((m_vt.m_primclass < GS_TRIANGLE_CLASS) || (grad.y < 1.0f || (grad.y == 1.0f && m_vt.m_max.p.y != floor(m_vt.m_max.p.y)))) ? 1 : 0;

// Roughly cut out the min/max of the read (Clamp)
switch (wms)
{
case CLAMP_REPEAT:
if ((uv.x & ~tw_mask) == (uv.z & ~tw_mask))
{
vr.x = std::max(vr.x, uv.x & tw_mask);
vr.z = std::min(vr.z, (uv.z & tw_mask) + inclusive_x_req);
vr.x = std::max(vr.x, (uv.x & tw_mask));
vr.z = std::min(vr.z, (uv.z & tw_mask) + 1); // +1 to convert inclusive to exclusive
}
break;
case CLAMP_CLAMP:
case CLAMP_REGION_CLAMP:
if (vr.x < uv.x)
vr.x = std::min(uv.x, vr.z - 1);
vr.x = std::min(vr.z - 1, uv.x);
if (vr.z > (uv.z + 1))
vr.z = std::max(uv.z, vr.x) + inclusive_x_req;
vr.z = std::max(vr.x + 1, uv.z + 1); // +1 to convert inclusive to exclusive
break;
// Note that UsesRegionRepeat already adds +1 to max result to make it exclusive
case CLAMP_REGION_REPEAT:
if (UsesRegionRepeat(maxu, minu, uv.x, uv.z, &vr.x, &vr.z) || maxu >= tw)
uses_border |= TextureMinMaxResult::USES_BOUNDARY_U;
break;
default:
ASSUME(0);
}

switch (wmt)
Expand All @@ -3961,20 +3992,23 @@ GSState::TextureMinMaxResult GSState::GetTextureMinMax(GIFRegTEX0 TEX0, GIFRegCL
if ((uv.y & ~th_mask) == (uv.w & ~th_mask))
{
vr.y = std::max(vr.y, uv.y & th_mask);
vr.w = std::min(vr.w, (uv.w & th_mask) + inclusive_y_req);
vr.w = std::min(vr.w, (uv.w & th_mask) + 1); // +1 to convert inclusive to exclusive
}
break;
case CLAMP_CLAMP:
case CLAMP_REGION_CLAMP:
if (vr.y < uv.y)
vr.y = std::min(uv.y, vr.w - 1);
vr.y = std::min(vr.w - 1, uv.y);
if (vr.w > (uv.w + 1))
vr.w = std::max(uv.w, vr.y) + inclusive_y_req;
vr.w = std::max(vr.y + 1, uv.w + 1); // +1 to convert inclusive to exclusive
break;
case CLAMP_REGION_REPEAT:
// Note that UsesRegionRepeat already adds +1 to max result to make it exclusive
if (UsesRegionRepeat(maxv, minv, uv.y, uv.w, &vr.y, &vr.w) || maxv >= th)
uses_border |= TextureMinMaxResult::USES_BOUNDARY_V;
break;
default:
ASSUME(0);
}
}

Expand Down Expand Up @@ -4006,7 +4040,7 @@ GSState::TextureMinMaxResult GSState::GetTextureMinMax(GIFRegTEX0 TEX0, GIFRegCL
vr = (vr + GSVector4i(0, inc_y ? 0 : -1, 0, inc_y ? 1 : 0)).rintersect(tr);
}

return { vr, uses_border };
return {vr, uses_border};
}

void GSState::CalcAlphaMinMax(const int tex_alpha_min, const int tex_alpha_max)
Expand Down

0 comments on commit 6f79d62

Please sign in to comment.