Skip to content

Commit

Permalink
Code for UV clipping hack.
Browse files Browse the repository at this point in the history
  • Loading branch information
TJ committed Dec 27, 2024
1 parent 6a0f811 commit ff4d8cc
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
42 changes: 41 additions & 1 deletion pcsx2/GS/Renderers/SW/GSDrawScanline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <fstream>

// Comment to disable all dynamic code generation.
#define ENABLE_JIT_RASTERIZER
// #define ENABLE_JIT_RASTERIZER // disabled to use C scanline renderer for testing

#if MULTI_ISA_COMPILE_ONCE
// Lack of a better home
Expand Down Expand Up @@ -627,6 +627,14 @@ __ri void GSDrawScanline::CDrawScanline(int pixels, int left, int top, const GSV
}
}

// Save the min/max values of UV that are passed from the renderer class.
// This will probably fail if the texture coordinate mode is ST intead of UV (FIXME).
// This could be computed in the renderer class itself to save some time (TODO).
GSVector4i u_min = GSVector4i(global.t_bbox.xxxx().ceil());
GSVector4i v_min = GSVector4i(global.t_bbox.yyyy().ceil());
GSVector4i u_max = GSVector4i(global.t_bbox.zzzz().floor());
GSVector4i v_max = GSVector4i(global.t_bbox.wwww().floor());

while (1)
{
do
Expand Down Expand Up @@ -978,11 +986,27 @@ __ri void GSDrawScanline::CDrawScanline(int pixels, int left, int top, const GSV
uv1 = clamp.blend8(repeat, VectorI::broadcast128(global.t.mask));
}

if (sel.fst)
{
// Clip UV coordinate to min/max UV of the primitive
GSVector4i u0_clip = uv0.upl16().max_u32(u_min).min_u32(u_max);
GSVector4i v0_clip = uv0.uph16().max_u32(v_min).min_u32(v_max);
uv0 = u0_clip.ps32(v0_clip);
}

VectorI y0 = uv0.uph16() << (sel.tw + 3);
VectorI x0 = uv0.upl16();

if (sel.ltf)
{
if (sel.fst)
{
// Clip UV coordinate to min/max UV of the primitive
GSVector4i u1_clip = uv1.upl16().max_u32(u_min).min_u32(u_max);
GSVector4i v1_clip = uv1.uph16().max_u32(v_min).min_u32(v_max);
uv1 = u1_clip.ps32(v1_clip);
}

VectorI y1 = uv1.uph16() << (sel.tw + 3);
VectorI x1 = uv1.upl16();

Expand Down Expand Up @@ -1119,11 +1143,27 @@ __ri void GSDrawScanline::CDrawScanline(int pixels, int left, int top, const GSV
uv1 = clamp.blend8(repeat, VectorI::broadcast128(global.t.mask));
}

if (sel.fst)
{
// Clip UV coordinate to min/max UV of the primitive
GSVector4i u0_clip = uv0.upl16().max_u32(u_min).min_u32(u_max);
GSVector4i v0_clip = uv0.uph16().max_u32(v_min).min_u32(v_max);
uv0 = u0_clip.ps32(v0_clip);
}

VectorI y0 = uv0.uph16() << (sel.tw + 3);
VectorI x0 = uv0.upl16();

if (sel.ltf)
{
if (sel.fst)
{
// Clip UV coordinate to min/max UV of the primitive
GSVector4i u1_clip = uv1.upl16().max_u32(u_min).min_u32(u_max);
GSVector4i v1_clip = uv1.uph16().max_u32(v_min).min_u32(v_max);
uv1 = u1_clip.ps32(v1_clip);
}

VectorI y1 = uv1.uph16() << (sel.tw + 3);
VectorI x1 = uv1.upl16();

Expand Down
2 changes: 2 additions & 0 deletions pcsx2/GS/Renderers/SW/GSRendererSW.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ void GSRendererSW::Draw()
m_mem.SaveBMP(s, m_context->ZBUF.Block(), m_context->FRAME.FBW, m_context->ZBUF.PSM, r.z, r.w);
}

data.get()->global.t_bbox = m_vt.m_min.t.xyxy(m_vt.m_max.t); // Set min/max UV values for scanline function to clip
Queue(data);

Sync(3);
Expand Down Expand Up @@ -509,6 +510,7 @@ void GSRendererSW::Draw()
}
else
{
data.get()->global.t_bbox = m_vt.m_min.t.xyxy(m_vt.m_max.t); // Set min/max UV values for scanline function to clip
Queue(data);
}

Expand Down
2 changes: 2 additions & 0 deletions pcsx2/GS/Renderers/SW/GSScanlineEnvironment.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ struct alignas(32) GSScanlineGlobalData // per batch variables, this is like a p
GSVector4i afix;
struct { GSVector4i min, max, minmax, mask, invmask; } t; // [u] x 4 [v] x 4

GSVector4 t_bbox = GSVector4(0.0f, 0.0f, 10000.0f, 10000.0f); // Min/max values of UV in verts.

#if _M_SSE >= 0x501

u32 fm, zm;
Expand Down

0 comments on commit ff4d8cc

Please sign in to comment.