Skip to content

Commit ec7cfb1

Browse files
committed
renderer: Fix 16-bit Z buffer writes
renderer: Improve Z format fetches Improves graphics on a bunch of games, Sonic Heroes and the Super Mario 64 port are the only ones I can remember from the top of my head
1 parent de3a2b6 commit ec7cfb1

File tree

1 file changed

+43
-21
lines changed

1 file changed

+43
-21
lines changed

src/gs/renderer/software_thread.cpp

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,8 @@ static inline uint32_t gs_read_dispfb(struct ps2_gs* gs, int x, int y, int dfb)
938938
return vram[psmct16_shift[idx] & 0xfffff];
939939
} break;
940940
default: {
941-
printf("Unsupported PSMT %02x for dispfb read\n", dfbpsm);
941+
fprintf(stderr, "Unsupported PSMT %02x for dispfb read\n", dfbpsm);
942+
942943
exit(1);
943944
} break;
944945
}
@@ -964,15 +965,15 @@ static inline uint32_t gs_read_zb(struct ps2_gs* gs, int x, int y) {
964965

965966
int idx = (x & 15) + ((y & 1) * 16);
966967

967-
return vram[psmct16_shift[idx] & 0xfffff];
968+
return vram[psmct16_shift[idx]];
968969
} break;
969970
case GS_PSMCT16S: {
970971
uint32_t addr = psmz16s_addr(gs->ctx->zbp >> 6, gs->ctx->fbw >> 6, x, y);
971972
uint16_t* vram = (uint16_t*)(&gs->vram[addr & 0xfffff]);
972973

973974
int idx = (x & 15) + ((y & 1) * 16);
974975

975-
return vram[psmct16_shift[idx] & 0xfffff];
976+
return vram[psmct16_shift[idx]];
976977
} break;
977978
default: {
978979
// printf("Unsupported PSMT %02x for zb read\n", gs->ctx->zbpsm);
@@ -1091,8 +1092,12 @@ static inline uint32_t gs_read_cb(struct ps2_gs* gs, int i) {
10911092
static inline uint32_t gs_to_rgba32(struct ps2_gs* gs, uint32_t c, int fmt) {
10921093
switch (fmt) {
10931094
case GS_PSMCT32:
1095+
case GS_PSMZ32: {
10941096
return c;
1095-
case GS_PSMCT24: {
1097+
}
1098+
1099+
case GS_PSMCT24:
1100+
case GS_PSMZ24: {
10961101
uint32_t a = 0;
10971102

10981103
if (gs->aem) {
@@ -1107,8 +1112,11 @@ static inline uint32_t gs_to_rgba32(struct ps2_gs* gs, uint32_t c, int fmt) {
11071112

11081113
return c | (a << 24);
11091114
}
1115+
11101116
case GS_PSMCT16:
1111-
case GS_PSMCT16S: {
1117+
case GS_PSMCT16S:
1118+
case GS_PSMZ16:
1119+
case GS_PSMZ16S: {
11121120
int ia = c & 0x8000;
11131121
uint32_t oa = 0;
11141122

@@ -1131,6 +1139,7 @@ static inline uint32_t gs_to_rgba32(struct ps2_gs* gs, uint32_t c, int fmt) {
11311139
((c & 0x7c00) << 9) |
11321140
(oa << 24);
11331141
} break;
1142+
11341143
case GS_PSMT8:
11351144
case GS_PSMT8H:
11361145
case GS_PSMT4:
@@ -1150,12 +1159,20 @@ static inline uint32_t gs_to_rgba32(struct ps2_gs* gs, uint32_t c, int fmt) {
11501159
static inline uint32_t gs_from_rgba32(struct ps2_gs* gs, uint32_t c, int fmt, int x = 0, int y = 0, int dither = 0) {
11511160
switch (fmt) {
11521161
case GS_PSMCT32:
1162+
case GS_PSMZ32: {
11531163
return c;
1164+
}
1165+
11541166
case GS_PSMCT24:
1167+
case GS_PSMZ24: {
11551168
// To-do: Use TEXA
11561169
return c & 0xffffff;
1170+
}
1171+
11571172
case GS_PSMCT16:
1158-
case GS_PSMCT16S: {
1173+
case GS_PSMCT16S:
1174+
case GS_PSMZ16:
1175+
case GS_PSMZ16S: {
11591176
if (dither) {
11601177
int dv = gs->dither[y & 3][x & 3];
11611178

@@ -1208,30 +1225,30 @@ static inline uint32_t gs_read_tb_impl(struct ps2_gs* gs, int u, int v) {
12081225
case GS_PSMCT32:
12091226
return gs->vram[psmct32_addr(gs->ctx->tbp0, gs->ctx->tbw, u, v) & 0xfffff];
12101227
case GS_PSMCT24:
1211-
return gs->vram[psmct32_addr(gs->ctx->tbp0, gs->ctx->tbw, u, v) & 0xfffff];
1228+
return gs->vram[psmct32_addr(gs->ctx->tbp0, gs->ctx->tbw, u, v) & 0xfffff] & 0xffffff;
12121229
case GS_PSMCT16: {
12131230
uint32_t addr = psmct16_addr(gs->ctx->tbp0, gs->ctx->tbw, u, v);
12141231
uint16_t* vram = (uint16_t*)(&gs->vram[addr & 0xfffff]);
12151232

12161233
int idx = (u & 15) + ((v & 1) * 16);
12171234

1218-
return vram[psmct16_shift[idx] & 0xfffff];
1235+
return vram[psmct16_shift[idx]];
12191236
} break;
12201237
case GS_PSMCT16S: {
12211238
uint32_t addr = psmct16s_addr(gs->ctx->tbp0, gs->ctx->tbw, u, v);
12221239
uint16_t* vram = (uint16_t*)(&gs->vram[addr & 0xfffff]);
12231240

12241241
int idx = (u & 15) + ((v & 1) * 16);
12251242

1226-
return vram[psmct16_shift[idx] & 0xfffff];
1243+
return vram[psmct16_shift[idx]];
12271244
} break;
12281245
case GS_PSMT8: {
12291246
uint32_t addr = psmt8_addr(gs->ctx->tbp0, gs->ctx->tbw, u, v);
12301247
uint8_t* vram = (uint8_t*)(&gs->vram[addr & 0xfffff]);
12311248

12321249
int idx = (u & 15) + ((v & 3) * 16);
12331250

1234-
return gs_read_cb(gs, vram[psmt8_shift[idx] & 0xfffff]);
1251+
return gs_read_cb(gs, vram[psmt8_shift[idx]]);
12351252
} break;
12361253
case GS_PSMT8H: {
12371254
uint32_t data = gs->vram[psmct32_addr(gs->ctx->tbp0, gs->ctx->tbw, u, v) & 0xfffff];
@@ -1457,7 +1474,7 @@ static inline void gs_write_zb(struct ps2_gs* gs, int x, int y, uint32_t z) {
14571474
int idx = (x & 15) + ((y & 1) * 16);
14581475

14591476
vram[psmct16_shift[idx]] = z;
1460-
}
1477+
} break;
14611478
case GS_ZSMZ16S: {
14621479
uint32_t addr = psmz16s_addr(gs->ctx->zbp >> 6, gs->ctx->fbw >> 6, x, y);
14631480
uint16_t* vram = (uint16_t*)(&gs->vram[addr & 0xfffff]);
@@ -2122,10 +2139,12 @@ void render_triangle(struct ps2_gs* gs, void* udata) {
21222139
area = EDGE(v0, v1, v2);
21232140
}
21242141

2125-
// printf("triangle: v0=(%04x,%04x) v1=(%04x,%04x) v2=(%04x,%04x)\n",
2142+
// printf("triangle: v0=(%04x,%04x) v1=(%04x,%04x) v2=(%04x,%04x) ate=%d date=%d zte=%d ztst=%d zbpsm=%02x zbp=%x fbp=%x\n",
21262143
// v0.x, v0.y,
21272144
// v1.x, v1.y,
2128-
// v2.x, v2.y
2145+
// v2.x, v2.y,
2146+
// gs->ctx->ate, gs->ctx->date, gs->ctx->zte, gs->ctx->ztst,
2147+
// gs->ctx->zbpsm, gs->ctx->zbp, gs->ctx->fbp
21292148
// );
21302149

21312150
v0.x -= gs->ctx->ofx;
@@ -2334,14 +2353,19 @@ void render_sprite(struct ps2_gs* gs, void* udata) {
23342353
// gs->ctx->scay1 << 4
23352354
// );
23362355

2337-
// printf("sprite: v0=(%04x,%04x) v1=(%04x,%04x) tnbpsm=%02x tbw=%x tbp=%x cbpsm=%d mode=%d\n",
2356+
// if (gs->tme)
2357+
// printf("sprite: v0=(%04x,%04x) v1=(%04x,%04x) tbpsm=%02x tbw=%x tbp=%x fbp=%x fbpsm=%02x zbp=%x zbpsm=%02x zte=%d zmsk=%d\n",
23382358
// v0.x, v0.y,
23392359
// v1.x, v1.y,
23402360
// gs->ctx->tbpsm,
23412361
// gs->ctx->tbw,
23422362
// gs->ctx->tbp0,
2343-
// gs->ctx->cbpsm,
2344-
// gs->ctx->csm
2363+
// gs->ctx->fbp >> 6,
2364+
// gs->ctx->fbpsm,
2365+
// gs->ctx->zbp >> 6,
2366+
// gs->ctx->zbpsm,
2367+
// gs->ctx->zte,
2368+
// gs->ctx->zbmsk
23452369
// );
23462370

23472371
// U and S values at the start of a row
@@ -3047,14 +3071,12 @@ void software_thread_begin_render(void* udata, SDL_GPUCommandBuffer* command_buf
30473071
dfbp = ctx->gs->dispfb2;
30483072
}
30493073

3050-
uint32_t* ptr = &ctx->gs->vram[dfbp];
3051-
3052-
// printf("fbp=%x\n", dfbp);
3053-
30543074
if (!ctx->tex_w) {
30553075
return;
30563076
}
30573077

3078+
// printf("dfbp2=%08x dfbp1=%08x dfbpsm2=%02x dfbpsm1=%02x dfb=%d fbp=%x\n", ctx->gs->dfbp2, ctx->gs->dfbp1, ctx->gs->dfbpsm2, ctx->gs->dfbpsm1, dfb, ctx->gs->ctx->fbp >> 6);
3079+
30583080
if (ctx->gs->smode2 & 1) {
30593081
// Need to deinterlace
30603082
if (ctx->gs->smode2 & 2) {
@@ -3066,7 +3088,7 @@ void software_thread_begin_render(void* udata, SDL_GPUCommandBuffer* command_buf
30663088
gs_blit_dispfb_no_deinterlace(ctx, dfb);
30673089
}
30683090

3069-
ptr = ctx->buf;
3091+
uint32_t* ptr = ctx->buf;
30703092

30713093
SDL_Rect size, rect;
30723094

0 commit comments

Comments
 (0)