Skip to content

Instantly share code, notes, and snippets.

@unknownbrackets
Forked from Orphis/gist:6048978
Last active December 20, 2015 01:29
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/6049007 to your computer and use it in GitHub Desktop.
Save unknownbrackets/6049007 to your computer and use it in GitHub Desktop.
PSP clut test
/*
* PSP Software Development Kit - http://www.pspdev.org
* -----------------------------------------------------------------------
* Licensed under the BSD license, see LICENSE in PSPSDK root for details.
*
* Copyright (c) 2005 Jesper Svennevid
*/
#include <common.h>
#include <pspkernel.h>
#include <pspdisplay.h>
#include <pspdebug.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <pspgu.h>
static unsigned int __attribute__((aligned(16))) list[262144];
struct Vertex
{
float u,v;
float x,y,z;
};
#define BUF_WIDTH (512)
#define SCR_WIDTH (480)
#define SCR_HEIGHT (272)
#define PIXEL_SIZE (4) /* change this if you change to another screenmode */
#define FRAME_SIZE (BUF_WIDTH * SCR_HEIGHT * PIXEL_SIZE)
#define ZBUF_SIZE (BUF_WIDTH SCR_HEIGHT * 2) /* zbuffer seems to be 16-bit? */
#define PAD1 0,
#define PAD2 PAD1 PAD1
#define PAD4 PAD2 PAD2
#define PAD8 PAD4 PAD4
#define PAD12 PAD8 PAD4
#define PAD14 PAD8 PAD4 PAD2
#define PAD15 PAD8 PAD4 PAD2 PAD1
unsigned __attribute__((aligned(16))) char tex5650[] = {
0x1F, 0x00, 0xE0, 0x07, PAD12
0x00, 0xF8, 0xFF, 0xFF, PAD12
};
unsigned __attribute__((aligned(16))) char tex5551[] = {
0x1F, 0x80, 0xE0, 0x83, PAD12
0x00, 0xFC, 0xFF, 0xFF, PAD12
};
unsigned __attribute__((aligned(16))) char tex4444[] = {
0x0F, 0xF0, 0xF0, 0xF0, PAD12
0x00, 0xFF, 0xFF, 0xFF, PAD12
};
unsigned __attribute__((aligned(16))) char tex8888[] = {
0xFF, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, PAD8
0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, PAD8
};
unsigned __attribute__((aligned(16))) char texclut4[] = {
0x10, PAD15
0x32, PAD15
};
unsigned __attribute__((aligned(16))) char texclut8[] = {
0x00, 0x01, PAD14
0x02, 0x03, PAD14
};
unsigned __attribute__((aligned(16))) char texclut16[] = {
0x00, 0x00, 0x01, 0x00, PAD12
0x02, 0x00, 0x03, 0x00, PAD12
};
unsigned __attribute__((aligned(16))) char texclut32[] = {
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, PAD8
0x02, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, PAD8
};
unsigned __attribute__((aligned(16))) char clut5650[] = {
0x1F, 0x00, 0xE0, 0x07,
0x00, 0xF8, 0xFF, 0xFF,
};
unsigned __attribute__((aligned(16))) char clut5551[] = {
0x1F, 0x80, 0xE0, 0x83,
0x00, 0xFC, 0xFF, 0xFF,
};
unsigned __attribute__((aligned(16))) char clut4444[] = {
0x0F, 0xF0, 0xF0, 0xF0,
0x00, 0xFF, 0xFF, 0xFF,
};
unsigned __attribute__((aligned(16))) char clut8888[] = {
0xFF, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
};
unsigned int __attribute__((aligned(16))) clut256[256];
unsigned char __attribute__((aligned(16))) tex256[256*256];
void drawsprite(int texfmt, int tbw, int clutfmt, unsigned char* tex, unsigned char* clut, int n) {
int i = n % 4;
int j = n / 4;
int sh = SCR_HEIGHT / 5;
int sw = SCR_WIDTH / 4;
if(clutfmt != -1) {
sceGuClutMode(clutfmt,0,0xff,0);
sceGuClutLoad((256/8),clut); // upload 32*8 entries (256)
}
sceGuTexMode(texfmt,0,0,0);
sceGuTexImage(0,2,2,tbw,tex);
sceGuTexFunc(GU_TFX_REPLACE,GU_TCC_RGB);
sceGuTexFilter(GU_NEAREST,GU_NEAREST);
sceGuTexScale(1.0f,1.0f);
sceGuTexOffset(0.0f,0.0f);
sceGuAmbientColor(0xffffffff);
sceGuColor(0xffffffff);
struct Vertex* vertices = (struct Vertex*)sceGuGetMemory(2 * sizeof(struct Vertex));
vertices[0].u = 0; vertices[0].v = 0;
vertices[0].x = sw * i; vertices[0].y = sh * j; vertices[0].z = 0;
vertices[1].u = 2; vertices[1].v = 2;
vertices[1].x = sw * (i + 1); vertices[1].y = sh * (j + 1); vertices[1].z = 0;
sceGuDrawArray(GU_SPRITES,GU_TEXTURE_32BITF|GU_VERTEX_32BITF|GU_TRANSFORM_2D,2,0,vertices);
}
int main(int argc, char* argv[])
{
unsigned int i,j;
// initialize texture
for (j = 0; j < 256; ++j)
{
for (i = 0; i < 256; ++i)
{
tex256[i + j * 256] = j ^ i;
}
}
sceKernelDcacheWritebackAll();
// setup GU
sceGuInit();
sceGuStart(GU_DIRECT,list);
sceGuDrawBuffer(GU_PSM_8888,(void*)0,BUF_WIDTH);
sceGuDispBuffer(SCR_WIDTH,SCR_HEIGHT,(void*)FRAME_SIZE,BUF_WIDTH);
sceGuDepthBuffer((void*)(FRAME_SIZE*2),BUF_WIDTH);
sceGuOffset(2048 - (SCR_WIDTH/2),2048 - (SCR_HEIGHT/2));
sceGuViewport(2048,2048,SCR_WIDTH,SCR_HEIGHT);
sceGuDepthRange(0xc350,0x2710);
sceGuScissor(0,0,SCR_WIDTH,SCR_HEIGHT);
sceGuEnable(GU_SCISSOR_TEST);
sceGuFrontFace(GU_CW);
sceGuEnable(GU_TEXTURE_2D);
sceGuClear(GU_COLOR_BUFFER_BIT|GU_DEPTH_BUFFER_BIT);
sceGuFinish();
sceGuSync(0,0);
sceDisplayWaitVblankStart();
sceGuDisplay(GU_TRUE);
// run sample
int offset = 0;
while (offset < 100)
{
sceGuStart(GU_DIRECT,list);
// clear screen
sceGuClearColor(0x808080);
sceGuClear(GU_COLOR_BUFFER_BIT);
// setup CLUT texture
/*
//sceGuClutMode(GU_PSM_8888,0,0xff,0); // 32-bit palette
//sceGuClutLoad((256/8),clut256); // upload 32*8 entries (256)
sceGuTexMode(GU_PSM_8888,0,0,0); // 8-bit image
sceGuTexImage(0,2,2,2,tex8888);
sceGuTexFunc(GU_TFX_REPLACE,GU_TCC_RGB);
sceGuTexFilter(GU_NEAREST,GU_NEAREST);
sceGuTexScale(1.0f,1.0f);
sceGuTexOffset(0.0f,0.0f);
sceGuAmbientColor(0xffffffff);
*/
// render sprite
int n = 0;
drawsprite(GU_PSM_5650, 8, -1, tex5650, NULL, n++);
drawsprite(GU_PSM_5551, 8, -1, tex5551, NULL, n++);
drawsprite(GU_PSM_4444, 8, -1, tex4444, NULL, n++);
drawsprite(GU_PSM_8888, 4, -1, tex8888, NULL, n++);
drawsprite(GU_PSM_T4, 32, GU_PSM_5650, texclut4, clut5650, n++);
drawsprite(GU_PSM_T4, 32, GU_PSM_5551, texclut4, clut5551, n++);
drawsprite(GU_PSM_T4, 32, GU_PSM_4444, texclut4, clut4444, n++);
drawsprite(GU_PSM_T4, 32, GU_PSM_8888, texclut4, clut8888, n++);
drawsprite(GU_PSM_T8, 16, GU_PSM_5650, texclut8, clut5650, n++);
drawsprite(GU_PSM_T8, 16, GU_PSM_5551, texclut8, clut5551, n++);
drawsprite(GU_PSM_T8, 16, GU_PSM_4444, texclut8, clut4444, n++);
drawsprite(GU_PSM_T8, 16, GU_PSM_8888, texclut8, clut8888, n++);
drawsprite(GU_PSM_T16, 8, GU_PSM_5650, texclut16, clut5650, n++);
drawsprite(GU_PSM_T16, 8, GU_PSM_5551, texclut16, clut5551, n++);
drawsprite(GU_PSM_T16, 8, GU_PSM_4444, texclut16, clut4444, n++);
drawsprite(GU_PSM_T16, 8, GU_PSM_8888, texclut16, clut8888, n++);
drawsprite(GU_PSM_T32, 4, GU_PSM_5650, texclut32, clut5650, n++);
drawsprite(GU_PSM_T32, 4, GU_PSM_5551, texclut32, clut5551, n++);
drawsprite(GU_PSM_T32, 4, GU_PSM_4444, texclut32, clut4444, n++);
drawsprite(GU_PSM_T32, 4, GU_PSM_8888, texclut32, clut8888, n++);
// wait for next frame
sceGuFinish();
sceGuSync(0,0);
sceDisplayWaitVblankStart();
sceGuSwapBuffers();
offset++;
if (offset == 5) {
emulatorEmitScreenshot();
}
}
sceGuTerm();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment