Skip to content

Instantly share code, notes, and snippets.

@unknownbrackets
Created August 17, 2013 16:37
Show Gist options
  • Save unknownbrackets/6257715 to your computer and use it in GitHub Desktop.
Save unknownbrackets/6257715 to your computer and use it in GitHub Desktop.
PSP alphatest test
#include <pspdisplay.h>
#include <pspgu.h>
#include <pspgum.h>
#include <common.h>
#include <pspkernel.h>
#include "commands.h"
#define BUF_WIDTH 512
#define SCR_WIDTH 480
#define SCR_HEIGHT 272
typedef u32 color_t;
typedef color_t __attribute__((aligned(16))) colorline_t[16];
typedef colorline_t __attribute__((aligned(16))) colorsquare_t[16];
unsigned int __attribute__((aligned(16))) list[262144];
unsigned int __attribute__((aligned(16))) clut[] = { 0xaaaaaaaa, 0x33ffffff, 0x00000000 };
unsigned char __attribute__((aligned(16))) bgData[] = {
2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
unsigned char __attribute__((aligned(16))) imageData[] = {
0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
colorsquare_t boxImages[] = {
{
{0x80808080, 0x80808080, 0x80808080, 0x80808080},
{0x80808080, 0x80808080, 0x80808080, 0x80808080},
{0x80808080, 0x80808080, 0x80808080, 0x80808080},
{0x80808080, 0x80808080, 0x80808080, 0x80808080},
},
{
{0x80808080, 0x30808080, 0x80808080, 0x80808080},
{0x30808080, 0x80808080, 0x80808080, 0x80808080},
{0x80808080, 0x30808080, 0x80808080, 0x80808080},
{0x30808080, 0x80808080, 0x80808080, 0x80808080},
},
};
typedef struct __attribute__((aligned(16)))
{
unsigned short a, b;
unsigned long color;
unsigned short x, y, z;
} VertexColor;
VertexColor bg[2] = { {0, 0, 0x33333333, 0, 0, 0}, {4, 4, 0x33333333, 479, 272, 0} };
VertexColor colorVertices[2 * 100];
int boxNum = 0;
void nextBox(int *x, int *y, u32 c)
{
colorsquare_t *boxImage = &boxImages[boxNum];
VertexColor *vtx = &colorVertices[boxNum * 2];
boxNum++;
vtx[0].a = 0;
vtx[0].b = 0;
vtx[0].color = c;
vtx[0].x = *x;
vtx[0].y = *y;
vtx[0].a = 4;
vtx[0].b = 4;
vtx[1].color = c;
vtx[1].x = *x + 40;
vtx[1].y = *y + 20;
*x += 47;
if (*x >= 470) {
*x = 10;
*y += 26;
}
sceKernelDcacheWritebackRange(vtx, sizeof(VertexColor) * 2);
sceGuBlendFunc(GU_ADD, GU_SRC_ALPHA, GU_ONE_MINUS_SRC_ALPHA, 0, 0);
sceGuEnable(GU_TEXTURE_2D);
sceGuTexMode(GU_PSM_8888, 0, 0, GU_FALSE);
sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGBA);
sceGuTexImage(0, 4, 4, 16, boxImage);
sceGuDrawArray(GU_SPRITES, GU_COLOR_8888 | GU_TEXTURE_16BIT | GU_VERTEX_16BIT | GU_TRANSFORM_2D, 2, NULL, vtx);
}
void drawBG()
{
sceGuEnable(GU_TEXTURE_2D);
sceGuTexMode(GU_PSM_T8, 0, 0, GU_FALSE);
sceGuTexFunc(GU_TFX_DECAL, GU_TCC_RGBA);
sceGuTexImage(0, 4, 4, 16, bgData);
sceGuDrawArray(GU_SPRITES, GU_COLOR_8888 | GU_TEXTURE_16BIT | GU_VERTEX_16BIT | GU_TRANSFORM_2D, 2, NULL, bg);
}
void draw()
{
int x = 10, y = 10;
sceDisplaySetMode(0, SCR_WIDTH, SCR_HEIGHT);
sceGuStart(GU_DIRECT, list);
sceGuClear(GU_COLOR_BUFFER_BIT | GU_STENCIL_BUFFER_BIT);
sceGuClutMode(GU_PSM_8888, 0, 0xFF, 0);
sceGuClutLoad(1, clut);
drawBG();
boxNum = 0;
// Reset things.
sceGuSendCommandi(GE_CMD_ALPHATESTENABLE, 0);
sceGuSendCommandi(GE_CMD_ALPHATEST, 0);
//nextBox(&x, &y, 0x80FFFFFF);
//nextBox(&x, &y, 0x80FF0000);
//nextBox(&x, &y, 0xFFFF0000);
nextBox(&x, &y, 0xFF000000);
nextBox(&x, &y, 0x00FF0000);
nextBox(&x, &y, 0x0000FF00);
nextBox(&x, &y, 0x000000FF);
nextBox(&x, &y, 0x80000000);
nextBox(&x, &y, 0x00800000);
nextBox(&x, &y, 0x00008000);
nextBox(&x, &y, 0x00000080);
sceGuFinish();
sceGuSync(GU_SYNC_LIST, GU_SYNC_WHAT_DONE);
sceGuSync(0, 0);
sceDisplayWaitVblankStart();
}
void init()
{
void *fbp0 = 0;
sceGuInit();
sceGuStart(GU_DIRECT, list);
sceGuDrawBuffer(GU_PSM_8888, fbp0, BUF_WIDTH);
sceGuDispBuffer(SCR_WIDTH, SCR_HEIGHT, fbp0, BUF_WIDTH);
sceGuScissor(0, 0, SCR_WIDTH, SCR_HEIGHT);
sceGuEnable(GU_SCISSOR_TEST);
sceGuFinish();
sceGuSync(0, 0);
sceDisplayWaitVblankStart();
sceGuDisplay(1);
}
int main(int argc, char *argv[])
{
int i;
init();
for (i = 0; i < 120; ++i) {
draw();
}
emulatorEmitScreenshot();
sceGuTerm();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment