Skip to content

Commit

Permalink
[EE] Some code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFakeMontyOnTheRun committed Dec 17, 2023
1 parent 9226255 commit 1c6a40f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 66 deletions.
50 changes: 25 additions & 25 deletions ee_frontend/base3d/src/gx/GXRenderer.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,25 @@ int snapshotSignal = '.';
int needsToRedrawHUD = TRUE;
int enable3DRendering = TRUE;

#define DEFAULT_FIFO_SIZE (256*1024)
#define DEFAULT_FIFO_SIZE (256*1024)

static void *frameBuffer[2] = { NULL, NULL};
static void *frameBuffer[2] = {NULL, NULL};
GXRModeObj *rmode;
f32 yscale;
u32 xfbHeight;
Mtx view;
Mtx44 perspective;
Mtx model, modelview;
u32 fb = 0; // initial framebuffer index
u32 fb = 0;
GXColor background = {0, 0, 0, 0xff};

u32 first_frame = 1;
guVector cubeAxis = {1,1,1};
guVector cubeAxis = {1, 1, 1};
f32 rquad = 0.0f;
f32 zt = 0;
void *gpfifo = NULL;

uint8_t whiteTextureData[ 32 * 32 * 4];
uint8_t whiteTextureData[32 * 32 * 4];
GXTexObj whiteTextureObj;

void graphicsInit() {
Expand All @@ -69,8 +69,8 @@ void graphicsInit() {

rmode = VIDEO_GetPreferredMode(NULL);

gpfifo = memalign(32,DEFAULT_FIFO_SIZE);
memset(gpfifo,0,DEFAULT_FIFO_SIZE);
gpfifo = memalign(32, DEFAULT_FIFO_SIZE);
memset(gpfifo, 0, DEFAULT_FIFO_SIZE);

frameBuffer[0] = SYS_AllocateFramebuffer(rmode);
frameBuffer[1] = SYS_AllocateFramebuffer(rmode);
Expand All @@ -79,22 +79,22 @@ void graphicsInit() {
VIDEO_SetNextFramebuffer(frameBuffer[fb]);
VIDEO_Flush();
VIDEO_WaitVSync();
if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();
if (rmode->viTVMode & VI_NON_INTERLACE) VIDEO_WaitVSync();

fb ^= 1;

GX_Init(gpfifo,DEFAULT_FIFO_SIZE);
GX_Init(gpfifo, DEFAULT_FIFO_SIZE);

GX_SetCopyClear(background, 0x00ffffff);

GX_SetViewport(0,0,rmode->fbWidth,rmode->efbHeight,0,1);
yscale = GX_GetYScaleFactor(rmode->efbHeight,rmode->xfbHeight);
GX_SetViewport(0, 0, rmode->fbWidth, rmode->efbHeight, 0, 1);
yscale = GX_GetYScaleFactor(rmode->efbHeight, rmode->xfbHeight);
xfbHeight = GX_SetDispCopyYScale(yscale);
GX_SetScissor(0,0,rmode->fbWidth,rmode->efbHeight);
GX_SetDispCopySrc(0,0,rmode->fbWidth,rmode->efbHeight);
GX_SetDispCopyDst(rmode->fbWidth,xfbHeight);
GX_SetCopyFilter(rmode->aa,rmode->sample_pattern,GX_TRUE,rmode->vfilter);
GX_SetFieldMode(rmode->field_rendering,((rmode->viHeight==2*rmode->xfbHeight)?GX_ENABLE:GX_DISABLE));
GX_SetScissor(0, 0, rmode->fbWidth, rmode->efbHeight);
GX_SetDispCopySrc(0, 0, rmode->fbWidth, rmode->efbHeight);
GX_SetDispCopyDst(rmode->fbWidth, xfbHeight);
GX_SetCopyFilter(rmode->aa, rmode->sample_pattern, GX_TRUE, rmode->vfilter);
GX_SetFieldMode(rmode->field_rendering, ((rmode->viHeight == 2 * rmode->xfbHeight) ? GX_ENABLE : GX_DISABLE));

if (rmode->aa) {
GX_SetPixelFmt(GX_PF_RGB565_Z16, GX_ZC_LINEAR);
Expand All @@ -103,7 +103,7 @@ void graphicsInit() {
}

GX_SetCullMode(GX_CULL_NONE);
GX_CopyDisp(frameBuffer[fb],GX_TRUE);
GX_CopyDisp(frameBuffer[fb], GX_TRUE);
GX_SetDispCopyGamma(GX_GM_1_0);

GX_ClearVtxDesc();
Expand All @@ -117,7 +117,7 @@ void graphicsInit() {

GX_SetTexCoordGen(GX_TEXCOORD0, GX_TG_MTX2x4, GX_TG_TEX0, GX_IDENTITY);

GX_SetTevOp(GX_TEVSTAGE0,GX_MODULATE);
GX_SetTevOp(GX_TEVSTAGE0, GX_MODULATE);
GX_SetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD0, GX_TEXMAP0, GX_COLOR0A0);

GX_SetNumTexGens(1);
Expand All @@ -126,7 +126,7 @@ void graphicsInit() {
GX_InvalidateTexAll();

GX_SetAlphaUpdate(GX_TRUE);
GX_SetAlphaCompare(GX_GREATER,0,GX_AOP_AND,GX_ALWAYS,0);
GX_SetAlphaCompare(GX_GREATER, 0, GX_AOP_AND, GX_ALWAYS, 0);
GX_SetZCompLoc(GX_FALSE);

memset(&whiteTextureData[0], 0xFF, 32 * 32 * 4);
Expand Down Expand Up @@ -211,22 +211,22 @@ void graphicsShutdown() {
}

void flipRenderer() {
GX_SetAlphaCompare(GX_GREATER,0,GX_AOP_AND,GX_ALWAYS,0);
GX_SetAlphaCompare(GX_GREATER, 0, GX_AOP_AND, GX_ALWAYS, 0);
guMtxIdentity(model);
guMtxTransApply(model, model, 0.0f,0.0f,-0.8f);
guMtxConcat(view,model,modelview);
// load the modelview matrix into matrix memory
guMtxTransApply(model, model, 0.0f, 0.0f, -0.8f);
guMtxConcat(view, model, modelview);

GX_LoadPosMtxImm(modelview, GX_PNMTX3);
GX_SetCurrentMtx(GX_PNMTX3);

GX_SetZMode(GX_TRUE, GX_LEQUAL, GX_TRUE);
GX_SetColorUpdate(GX_TRUE);
GX_CopyDisp(frameBuffer[fb],GX_TRUE);
GX_CopyDisp(frameBuffer[fb], GX_TRUE);

GX_DrawDone();

VIDEO_SetNextFramebuffer(frameBuffer[fb]);
if(first_frame) {
if (first_frame) {
first_frame = 0;
VIDEO_SetBlack(FALSE);
}
Expand Down
23 changes: 12 additions & 11 deletions ee_frontend/base3d/src/gx/LoadBitmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,23 @@ void bindTexture(struct Bitmap *bitmap) {
int submitBitmapToGPU(struct Bitmap *bitmap) {
bitmap->nativeBuffer = allocMem(sizeof(GXTexObj), TEXTURE_MEMORY, 1);
DCFlushRange(bitmap->data, bitmap->width * bitmap->height * 4);
GX_InitTexObj(bitmap->nativeBuffer, bitmap->data, bitmap->width, bitmap->height, GX_TF_RGBA8, GX_REPEAT, GX_REPEAT, GX_FALSE);
GX_InitTexObj(bitmap->nativeBuffer, bitmap->data, bitmap->width, bitmap->height, GX_TF_RGBA8, GX_REPEAT, GX_REPEAT,
GX_FALSE);
bitmap->uploadId = 1;

return 0;
}

/* Based on code from GRRLIB - https://github.com/GRRLIB/GRRLIB */
void SetPixelToTexImg (const int x, const int y,
struct Bitmap *tex, const uint32_t color) {
uint32_t offs;
uint8_t* bp = (uint8_t*)tex->data;
void SetPixelToTexImg(const int x, const int y,
struct Bitmap *tex, const uint32_t color) {
uint32_t offs;
uint8_t *bp = (uint8_t *) tex->data;

offs = (((y&(~3))<<2)*tex->width) + ((x&(~3))<<4) + ((((y&3)<<2) + (x&3)) <<1);
offs = (((y & (~3)) << 2) * tex->width) + ((x & (~3)) << 4) + ((((y & 3) << 2) + (x & 3)) << 1);

*((uint16_t*)(bp+offs )) = (uint16_t)((color <<8) | (color >>24));
*((uint16_t*)(bp+offs+32)) = (uint16_t) (color >>8);
*((uint16_t *) (bp + offs)) = (uint16_t) ((color << 8) | (color >> 24));
*((uint16_t *) (bp + offs + 32)) = (uint16_t) (color >> 8);
}

struct Bitmap *loadBitmap(const char *filename) {
Expand Down Expand Up @@ -82,7 +83,7 @@ struct Bitmap *loadBitmap(const char *filename) {

memCopyToFrom(buffer, ptr, sizeInDisk);

toReturn->data = (TexturePixelFormat *) memalign( 32, size);
toReturn->data = (TexturePixelFormat *) memalign(32, size);
uint32_t *imgBuffer = allocMem(size, GENERAL_MEMORY, 0);

uint8_t repetitions;
Expand All @@ -109,8 +110,8 @@ struct Bitmap *loadBitmap(const char *filename) {
}
}

for (int y = 0; y < toReturn->height; ++y ) {
for (int x = 0; x < toReturn->width; ++x ) {
for (int y = 0; y < toReturn->height; ++y) {
for (int x = 0; x < toReturn->width; ++x) {
SetPixelToTexImg(x, y, toReturn, imgBuffer[y * toReturn->width + x]);
}
}
Expand Down
21 changes: 4 additions & 17 deletions ee_frontend/base3d/src/gx/Renderer.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,8 @@ extern GXRModeObj *rmode;
extern Mtx model, modelview;
extern Mtx view;
extern Mtx44 perspective;
guVector Yaxis = {0,1,0};
guVector Xaxis = {1,0,0};

guVector Yaxis = {0, 1, 0};
guVector Xaxis = {1, 0, 0};


void drawTriangle(const struct Vec3 pos1,
Expand All @@ -85,17 +84,11 @@ void enter2D(void) {
up = {0.0F, 1.0F, 0.0F},
look = {0.0F, 0.0F, -1.0F};


// setup our camera at the origin
// looking down the -z axis with y up
guLookAt(view, &cam, &up, &look);

// setup our projection matrix
// this creates a perspective matrix with a view angle of 90,
// and aspect ratio based on the display resolution
f32 w = rmode->viWidth;
f32 h = rmode->viHeight;
guPerspective(perspective, 45, (f32)w/h, 0.1F, 1024.0F);
guPerspective(perspective, 45, (f32) w / h, 0.1F, 1024.0F);
GX_LoadProjectionMtx(perspective, GX_PERSPECTIVE);
}

Expand Down Expand Up @@ -140,17 +133,11 @@ void enter3D(void) {
up = {0.0F, 1.0F, 0.0F},
look = {0.0F, 0.0F, -1.0F};


// setup our camera at the origin
// looking down the -z axis with y up
guLookAt(view, &cam, &up, &look);

// setup our projection matrix
// this creates a perspective matrix with a view angle of 90,
// and aspect ratio based on the display resolution
f32 w = rmode->viWidth;
f32 h = rmode->viHeight;
guPerspective(perspective, 90, (f32)w/h, 0.1F, 256.0F);
guPerspective(perspective, 90, (f32) w / h, 0.1F, 256.0F);
GX_LoadProjectionMtx(perspective, GX_PERSPECTIVE);
}

Expand Down
26 changes: 13 additions & 13 deletions ee_frontend/base3d/src/gx/Renderer_Rasterization.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void fillRect(
const FramebufferPixelFormat pixel,
const uint8_t stipple) {

drawBitmapRegion(_x, _y, _dx, _dy, pixel, NULL, 0, 0, 0, 1, 1);
drawBitmapRegion(_x, _y, _dx, _dy, pixel, NULL, 0, 0, 0, 1, 1);
}

void drawBitmapRegion(const int _x,
Expand Down Expand Up @@ -109,25 +109,25 @@ void drawBitmapRegion(const int _x,
}


GX_Begin(GX_QUADS, GX_VTXFMT0, 4); // Draw A Quad
GX_Begin(GX_QUADS, GX_VTXFMT0, 4);

GX_Position3f32(x, y + dy, -0.125); // Top Left
GX_Color3f32(r ,g ,b);
GX_Position3f32(x, y + dy, -0.125);
GX_Color3f32(r, g, b);
GX_TexCoord2f32(u0, v1);

GX_Position3f32( x + dx, y + dy, -0.125); // Top Right
GX_Color3f32(r ,g ,b);
GX_Position3f32(x + dx, y + dy, -0.125);
GX_Color3f32(r, g, b);
GX_TexCoord2f32(u1, v1);

GX_Position3f32( x +dx, y, -0.125); // Bottom Right
GX_Color3f32(r ,g ,b);
GX_TexCoord2f32(u1,v0);
GX_Position3f32(x + dx, y, -0.125);
GX_Color3f32(r, g, b);
GX_TexCoord2f32(u1, v0);

GX_Position3f32(x, y, -0.125); // Bottom Left
GX_Color3f32(r ,g ,b);
GX_TexCoord2f32(u0,v0);
GX_Position3f32(x, y, -0.125);
GX_Color3f32(r, g, b);
GX_TexCoord2f32(u0, v0);

GX_End(); // Done Drawing The Quad
GX_End();
}

void drawBitmap(const int _x,
Expand Down

0 comments on commit 1c6a40f

Please sign in to comment.