Skip to content

Commit fc1d69d

Browse files
committed
hardware: Implement correct scissor detection
1 parent 25e82a2 commit fc1d69d

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

src/gs/renderer/hardware.cpp

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,50 @@ void hardware_init(void* udata, struct ps2_gs* gs, SDL_Window* window, SDL_GPUDe
144144
void hardware_destroy(void* udata) {
145145
hardware_state* ctx = (hardware_state*)udata;
146146
}
147-
void hardware_set_size(void* udata, int width, int height) {}
147+
void hardware_set_size(void* udata, int width, int height) {
148+
hardware_state* ctx = (hardware_state*)udata;
149+
150+
int en1 = ctx->gs->pmode & 1;
151+
int en2 = (ctx->gs->pmode >> 1) & 1;
152+
153+
uint64_t display = 0, dispfb = 0;
154+
155+
if (en1) {
156+
display = ctx->gs->display1;
157+
dispfb = ctx->gs->dispfb1;
158+
} else if (en2) {
159+
display = ctx->gs->display2;
160+
dispfb = ctx->gs->dispfb2;
161+
}
162+
163+
uint32_t tex_fmt = (dispfb >> 15) & 0x1f;
164+
165+
int magh = ((display >> 23) & 0xf) + 1;
166+
int magv = ((display >> 27) & 3) + 1;
167+
168+
if (((display >> 32) & 0xfff) == 0) {
169+
ctx->tex_w = 0;
170+
ctx->tex_h = 0;
171+
172+
return;
173+
}
174+
175+
if (((display >> 44) & 0x7ff) == 0) {
176+
ctx->tex_w = 0;
177+
ctx->tex_h = 0;
178+
179+
return;
180+
}
181+
182+
uint32_t tex_w = (((display >> 32) & 0xfff) / magh) + 1;
183+
uint32_t tex_h = (((display >> 44) & 0x7ff) / magv) + 1;
184+
185+
ctx->tex_w = tex_w;
186+
ctx->tex_h = tex_h;
187+
188+
if ((ctx->gs->smode2 & 3) == 3)
189+
ctx->tex_h /= 2;
190+
}
148191
void hardware_set_scale(void* udata, float scale) {}
149192
void hardware_set_aspect_mode(void* udata, int aspect_mode) {}
150193
void hardware_set_integer_scaling(void* udata, bool integer_scaling) {}
@@ -176,6 +219,8 @@ extern "C" void hardware_render_triangle(struct ps2_gs* gs, void* udata) {
176219
// gs->vq[0].z, gs->vq[1].z, gs->vq[2].z
177220
// );
178221

222+
int interlace = ((gs->smode2 & 3) == 3) ? 2 : 1;
223+
179224
for (int i = 0; i < 3; i++) {
180225
hw_vertex hw_v;
181226

@@ -186,7 +231,7 @@ extern "C" void hardware_render_triangle(struct ps2_gs* gs, void* udata) {
186231
hw_v.u = gs->vq[i].u;
187232
hw_v.v = gs->vq[i].v;
188233
hw_v.x = gs->vq[i].x - ctx->gs->ctx->ofx;
189-
hw_v.y = (224 - (gs->vq[i].y - ctx->gs->ctx->ofy)) * 2;
234+
hw_v.y = (ctx->tex_h - (gs->vq[i].y - ctx->gs->ctx->ofy)) * interlace;
190235
hw_v.z = gs->vq[i].z;
191236
hw_v.s = gs->vq[i].s;
192237
hw_v.t = gs->vq[i].t;
@@ -201,10 +246,12 @@ extern "C" void hardware_render_sprite(struct ps2_gs* gs, void* udata) {
201246
struct gs_vertex v0 = gs->vq[0];
202247
struct gs_vertex v1 = gs->vq[1];
203248

249+
int interlace = ((gs->smode2 & 3) == 3) ? 2 : 1;
250+
204251
uint32_t v0_x = v0.x - gs->ctx->ofx;
205-
uint32_t v0_y = (224 - (v0.y - gs->ctx->ofy)) * 2.0;
252+
uint32_t v0_y = (ctx->tex_h - (v0.y - gs->ctx->ofy)) * interlace;
206253
uint32_t v1_x = v1.x - gs->ctx->ofx;
207-
uint32_t v1_y = (224 - (v1.y - gs->ctx->ofy)) * 2.0;
254+
uint32_t v1_y = (ctx->tex_h - (v1.y - gs->ctx->ofy)) * interlace;
208255

209256
hw_vertex hw_v[6];
210257

src/gs/renderer/hardware.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ struct hardware_state {
3131
struct ps2_gs* gs;
3232

3333
std::vector <hw_vertex> vertices;
34+
35+
int tex_w, tex_h;
3436
};
3537

3638
void hardware_init(void* ctx, struct ps2_gs* gs, SDL_Window* window, SDL_GPUDevice* device);

0 commit comments

Comments
 (0)