Skip to content

Commit

Permalink
animated-triangle: double buffering test
Browse files Browse the repository at this point in the history
  • Loading branch information
JaCzekanski committed Dec 22, 2020
1 parent 6eb49a4 commit 760c497
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 1 deletion.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ IMAGES = common \
dma/chopping \
dma/dpcr \
dma/otc-test \
gpu/animated-triangle \
gpu/bandwidth \
gpu/benchmark \
gpu/clipping \
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ otc-test | DMA Channel 6 (OTC aka Ordering Table clear) unit tes

Name | Description
-------------------------|------------
animated-triangle | Double buffered rotating triangle
bandwidth | Measure GPU/VRAM bandwidth
benchmark | GPU test to benchmark rasterizer for various commands
clut-cache | CLUT (Palette) cache behavior test
Expand Down
2 changes: 1 addition & 1 deletion common-test.mk
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ include $(BASEDIR)/common.mk

INCLUDE += -I$(realpath $(BASEDIR)/common)
LIBDIRS += -L$(realpath $(BASEDIR)/common)
LIBS += -lcommon -lpsxgpu -lpsxetc -lpsxapi -lc
LIBS += -lcommon -lpsxgpu -lpsxetc -lpsxsio -lpsxapi -lpsxgte -lc

TARGET_EXE := $(TARGET:.elf=.exe)

Expand Down
3 changes: 3 additions & 0 deletions gpu/animated-triangle/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
TARGET = animated-triangle.elf

include ../../common-test.mk
132 changes: 132 additions & 0 deletions gpu/animated-triangle/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#include <common.h>
#include <stdio.h>
#include <stdlib.h>
#include <psxgpu.h>
#include <psxgte.h>
#include <psxpad.h>

int SCR_W = 320;
int SCR_H = 240;
#define OT_LEN 2

static DISPENV disp[2];
static DRAWENV draw[2];

char pribuff[2][2048]; /* Primitive packet buffers */
unsigned int ot[2][OT_LEN]; /* Ordering tables */
char *nextpri; /* Pointer to next packet buffer offset */
int dbActive;

int angle = 0;

// Init function
void init(void) {
ResetGraph(0);
SetVideoMode(MODE_NTSC);

SetDefDispEnv(&disp[0], 0, 0, SCR_W, SCR_H);
SetDefDrawEnv(&draw[0], SCR_W, 0, SCR_W, SCR_H);

SetDefDrawEnv(&draw[1], 0, 0, SCR_W, SCR_H);
SetDefDispEnv(&disp[1], SCR_W, 0, SCR_W, SCR_H);

for (int i = 0; i<2; i++){
setRGB0(&draw[i], 255, 255, 255);
draw[i].isbg = 1;
}

// Clear double buffer counter
dbActive = 0;

// Apply the GPU environments
PutDispEnv(&disp[dbActive]);
PutDrawEnv(&draw[dbActive]);

ClearOTagR( ot[0], OT_LEN );
ClearOTagR( ot[1], OT_LEN );
nextpri = pribuff[dbActive];
}

// Display function
void display(void)
{
DrawSync(0);
VSync(0);

dbActive = !dbActive;
nextpri = pribuff[dbActive];
ClearOTagR( ot[dbActive], OT_LEN );

PutDrawEnv(&draw[dbActive]);
PutDispEnv(&disp[dbActive]);

SetDispMask(1);
DrawOTag(ot[1-dbActive] + OT_LEN-1);
}

struct Vertex {
int x, y;
};

void translate(struct Vertex* v, struct Vertex tr) {
v->x += tr.x;
v->y += tr.y;
}

void rot2d(struct Vertex* v, int angle) {
#define M_PI 3.14
int x = v->x;
int y = v->y;

int c = icos(angle);
int s = isin(angle);

v->x = (x * c - y * s )>>12;
v->y = (x * s + y * c )>>12;
}

void drawTriangle(int cx, int cy, int size)
{
struct Vertex v[3];
v[0].x = -0.866f * size; v[0].y = -0.5f * size;
v[1].x = 0.866f * size; v[1].y = -0.5f * size;
v[2].x = 0; v[2].y = size;

struct Vertex tr = {
.x = cx,
.y = cy
};
for (int i = 0; i<3; i++) {
rot2d(&v[i], angle);
translate(&v[i], tr);
}

POLY_G3* p = (POLY_G3*)nextpri;
setPolyG3(p);
setRGB0(p, 255, 0, 0);
setRGB1(p, 0, 255, 0);
setRGB2(p, 0, 0, 255);
setXY3(p,
v[0].x, v[0].y,
v[1].x, v[1].y,
v[2].x, v[2].y
);
addPrim(ot[dbActive] + (OT_LEN-1), p);

nextpri += sizeof(POLY_G3);

angle += 4096/360;
if (angle >=4096) angle = 0;
}

int main(){
init();
printf("\ngpu/animated-triangle\n");

for (;;) {
drawTriangle(SCR_W/2, SCR_H/2, SCR_H/2);

display();
}
return 0;
}

0 comments on commit 760c497

Please sign in to comment.