Skip to content

Instantly share code, notes, and snippets.

@unknownbrackets
Last active December 28, 2015 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unknownbrackets/7530598 to your computer and use it in GitHub Desktop.
Save unknownbrackets/7530598 to your computer and use it in GitHub Desktop.
Messy hacked together depth test
#include <common.h>
#include <pspdisplay.h>
#include <pspgu.h>
#include <pspgum.h>
#include <float.h>
#include <pspkernel.h>
#include "commands.h"
u8 *fbp0 = 0;
u8 *dbp0 = fbp0 + 512 * 272 * sizeof(u32);
unsigned int __attribute__((aligned(16))) list[262144];
typedef struct {
u32 color;
s16 x, y;
u16 z;
} VertexColorU16;
typedef struct {
u32 color;
u8 x, y, z;
} VertexColorU8;
typedef struct {
u32 color;
float x, y, z;
} VertexColorF32;
__attribute__((aligned(16))) VertexColorU16 vertices[256];
VertexColorU8 *vertices_u8 = (VertexColorU8 *) &vertices[0];
VertexColorU16 *vertices_u16 = (VertexColorU16 *) &vertices[0];
VertexColorF32 *vertices_f32 = (VertexColorF32 *) &vertices[0];
inline VertexColorU8 makeVertex8(u32 c, u16 x, u16 y, u16 z) {
VertexColorU8 v;
v.color = c;
v.x = x & 0xFF;
v.y = y & 0xFF;
v.z = z > 255 ? 255 : z;
return v;
}
inline VertexColorU16 makeVertex16(u32 c, u16 x, u16 y, u16 z) {
VertexColorU16 v;
v.color = c;
v.x = x;
v.y = y;
v.z = z;
return v;
}
inline VertexColorF32 makeVertex32(u32 c, float x, float y, float z) {
VertexColorF32 v;
v.color = c;
v.x = x;
v.y = y;
v.z = z;
return v;
}
void displayBuffer(const char *reason) {
unsigned short *db = (unsigned short *)((u8 *)sceGeEdramGetAddr() + (intptr_t)dbp0);
schedf("%s: DEPTH= %04x %04x %04x %04x\n", reason, db[0], db[1], db[2], db[3]);
// Reset.
memset(((u8 *)sceGeEdramGetAddr() + (intptr_t)dbp0), 0x99, 512 * 272 * sizeof(u16));
}
void drawBoxCommands(u32 c, int z, bool throughmode, u32 posType) {
int i = 0;
switch (posType) {
case GU_VERTEX_8BIT:
// TODO: Doesn't work in throughmode.
vertices_u8[i++] = makeVertex8(c, -127, -127, z);
vertices_u8[i++] = makeVertex8(c, 127, 127, z);
break;
case GU_VERTEX_16BIT:
vertices_u16[i++] = makeVertex16(c, -32767, -32767, z);
vertices_u16[i++] = makeVertex16(c, 32767, 32767, z);
break;
case GU_VERTEX_32BITF:
// TODO: Only works in throughmode.
vertices_f32[i++] = makeVertex32(c, -32767, -32767, z);
vertices_f32[i++] = makeVertex32(c, 32767, 32767, z);
break;
}
sceGuColor(c);
// Clearing cache is fun. Let's do it all the time.
sceKernelDcacheWritebackInvalidateAll();
sceGuDrawArray(GU_SPRITES, GU_COLOR_8888 | posType | (throughmode ? GU_TRANSFORM_2D : GU_TRANSFORM_3D), 2, NULL, vertices);
}
void init() {
sceGuInit();
sceGuStart(GU_DIRECT, list);
sceGuDrawBuffer(GU_PSM_8888, fbp0, 512);
sceGuDispBuffer(480, 272, fbp0, 512);
sceGuDepthBuffer(dbp0, 512);
sceGuOffset(2048 - (480 / 2), 2048 - (272 / 2));
sceGuViewport(2048, 2048, 480, 272);
sceGuDepthRange(65535, 0);
sceGuDepthMask(0);
sceGuScissor(0, 0, 480, 272);
sceGuEnable(GU_SCISSOR_TEST);
sceGuFrontFace(GU_CW);
sceGuShadeModel(GU_SMOOTH);
sceGuDisable(GU_TEXTURE_2D);
ScePspFMatrix4 ones = {
{1, 0, 0, 0},
{0, 1, 0, 0},
{0, 0, 1, 0},
{0, 0, 0, 1},
};
sceGuSetMatrix(GU_MODEL, &ones);
sceGuSetMatrix(GU_VIEW, &ones);
sceGuSetMatrix(GU_PROJECTION, &ones);
sceGuFinish();
sceGuSync(0, 0);
sceDisplayWaitVblankStart();
sceGuDisplay(1);
memset(sceGeEdramGetAddr(), 0x99, 512 * 272 * sizeof(u32));
memset(((u8 *)sceGeEdramGetAddr() + (intptr_t)dbp0), 0x99, 512 * 272 * sizeof(u16));
displayBuffer("Initial");
}
void drawBoxWithDepthFunc(const char *title, int func, int ref, u32 posType, bool throughmode) {
sceGuStart(GU_DIRECT, list);
sceGuEnable(GU_STENCIL_TEST);
sceGuStencilFunc(GU_ALWAYS, 0, 0);
sceGuStencilOp(GU_ZERO, GU_DECR, GU_INCR);
sceGuEnable(GU_DEPTH_TEST);
sceGuDepthFunc(func);
drawBoxCommands(0xFFFFFF00, ref, throughmode, posType);
sceGuDisable(GU_DEPTH_TEST);
sceGuDisable(GU_STENCIL_TEST);
sceGuFinish();
sceGuSync(GU_SYNC_WAIT, GU_SYNC_WHAT_DONE);
displayBuffer(title);
}
void testDepthFuncs(const char *title, u32 posType, bool throughmode) {
checkpointNext("Depth funcs:");
checkpoint(title);
drawBoxWithDepthFunc("NEVER", GU_NEVER, 0x0C0C, posType, throughmode);
drawBoxWithDepthFunc("ALWAYS", GU_ALWAYS, 0x0C0C, posType, throughmode);
drawBoxWithDepthFunc("EQUAL - pass", GU_EQUAL, 0x9999, posType, throughmode);
drawBoxWithDepthFunc("EQUAL - fail", GU_EQUAL, 0x9A9A, posType, throughmode);
drawBoxWithDepthFunc("NOTEQUAL - pass", GU_NOTEQUAL, 0x9A9A, posType, throughmode);
drawBoxWithDepthFunc("NOTEQUAL - fail", GU_NOTEQUAL, 0x9999, posType, throughmode);
drawBoxWithDepthFunc("LESS - pass", GU_LESS, 0x9998, posType, throughmode);
drawBoxWithDepthFunc("LESS - fail", GU_LESS, 0x9999, posType, throughmode);
drawBoxWithDepthFunc("LEQUAL - pass", GU_LEQUAL, 0x9999, posType, throughmode);
drawBoxWithDepthFunc("LEQUAL - fail", GU_LEQUAL, 0x999A, posType, throughmode);
drawBoxWithDepthFunc("GREATER - pass", GU_GREATER, 0x999A, posType, throughmode);
drawBoxWithDepthFunc("GREATER - fail", GU_GREATER, 0x9999, posType, throughmode);
drawBoxWithDepthFunc("GEQUAL - pass", GU_GEQUAL, 0x9999, posType, throughmode);
drawBoxWithDepthFunc("GEQUAL - fail", GU_GEQUAL, 0x9998, posType, throughmode);
}
extern "C" int main(int argc, char *argv[]) {
init();
schedf("framebuf: %08x\n", sceDisplaySetFrameBuf(sceGeEdramGetAddr(), 512, GU_PSM_8888, PSP_DISPLAY_SETBUF_IMMEDIATE));
schedf("dispmode: %08x\n", sceDisplaySetMode(0, 480, 272));
testDepthFuncs("u8 - through", GU_VERTEX_8BIT, true);
testDepthFuncs("u16 - through", GU_VERTEX_16BIT, true);
testDepthFuncs("float - through", GU_VERTEX_32BITF, true);
testDepthFuncs("u8", GU_VERTEX_8BIT, false);
testDepthFuncs("u16", GU_VERTEX_16BIT, false);
testDepthFuncs("float", GU_VERTEX_32BITF, false);
for (int i = 0; i < 30; i++) {
sceDisplayWaitVblankStart();
}
sceGuTerm();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment