From 760c4972dc4d8ad062e7c21265223fde3847a849 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Czekan=CC=81ski?= Date: Wed, 23 Dec 2020 00:12:41 +0100 Subject: [PATCH] animated-triangle: double buffering test --- Makefile | 1 + README.md | 1 + common-test.mk | 2 +- gpu/animated-triangle/Makefile | 3 + gpu/animated-triangle/main.c | 132 +++++++++++++++++++++++++++++++++ 5 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 gpu/animated-triangle/Makefile create mode 100644 gpu/animated-triangle/main.c diff --git a/Makefile b/Makefile index d1ebc35..aaf7544 100644 --- a/Makefile +++ b/Makefile @@ -11,6 +11,7 @@ IMAGES = common \ dma/chopping \ dma/dpcr \ dma/otc-test \ + gpu/animated-triangle \ gpu/bandwidth \ gpu/benchmark \ gpu/clipping \ diff --git a/README.md b/README.md index 1a0b6c0..bdbcf56 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/common-test.mk b/common-test.mk index cf7b6d4..1de8668 100644 --- a/common-test.mk +++ b/common-test.mk @@ -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) diff --git a/gpu/animated-triangle/Makefile b/gpu/animated-triangle/Makefile new file mode 100644 index 0000000..e546223 --- /dev/null +++ b/gpu/animated-triangle/Makefile @@ -0,0 +1,3 @@ +TARGET = animated-triangle.elf + +include ../../common-test.mk diff --git a/gpu/animated-triangle/main.c b/gpu/animated-triangle/main.c new file mode 100644 index 0000000..8930976 --- /dev/null +++ b/gpu/animated-triangle/main.c @@ -0,0 +1,132 @@ +#include +#include +#include +#include +#include +#include + +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; +}