Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring #56

Merged
merged 6 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 72 additions & 68 deletions src/gc_gl.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ char _ogx_log_level = 0;
static GXTexObj s_zbuffer_texture;
static uint8_t s_zbuffer_texels[2 * 32] ATTRIBUTE_ALIGN(32);

static void draw_arrays_general(int first, int count, int ne,
int color_provide, int texen, bool loop);
static void draw_arrays_general(uint8_t gxmode, int first, int count, int ne,
int color_provide, int texen);

#define MODELVIEW_UPDATE \
{ \
Expand Down Expand Up @@ -115,13 +115,6 @@ static void draw_arrays_general(int first, int count, int ne,
GX_LoadNrmMtxImm(normalm, GX_PNMTX3); \
}

static inline void model_view_matrix_to_gx(Mtx mv)
{
for (int i = 0; i < 3; i++)
for (int j = 0; j < 4; j++)
mv[i][j] = glparamstate.modelview_matrix[j][i];
}

/* Deduce the projection type (perspective vs orthogonal) and the values of the
* near and far clipping plane from the projection matrix. */
static void get_projection_info(u8 *type, float *near, float *far)
Expand Down Expand Up @@ -171,21 +164,6 @@ static void setup_cull_mode()
}
}

static inline uint8_t gx_compare_from_gl(GLenum func)
{
switch (func) {
case GL_NEVER: return GX_NEVER;
case GL_LESS: return GX_LESS;
case GL_EQUAL: return GX_EQUAL;
case GL_LEQUAL: return GX_LEQUAL;
case GL_GREATER: return GX_GREATER;
case GL_NOTEQUAL: return GX_NEQUAL;
case GL_GEQUAL: return GX_GEQUAL;
case GL_ALWAYS: return GX_ALWAYS;
default: return 0xff;
}
}

int ogx_prepare_swap_buffers()
{
return glparamstate.render_mode == GL_RENDER ? 0 : -1;
Expand Down Expand Up @@ -221,6 +199,7 @@ void ogx_initialize()
glparamstate.glcullmode = GL_BACK;
glparamstate.render_mode = GL_RENDER;
glparamstate.cullenabled = 0;
glparamstate.color_update = true;
glparamstate.alpha_func = GX_ALWAYS;
glparamstate.alpha_ref = 0;
glparamstate.alphatest_enabled = 0;
Expand Down Expand Up @@ -1159,7 +1138,11 @@ void glClearColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
}
void glClearDepth(GLclampd depth)
{
glparamstate.clearz = clampf_01(depth);
float clearz = clampf_01(depth);
if (clearz != glparamstate.clearz) {
glparamstate.clearz = clearz;
glparamstate.dirty.bits.dirty_clearz = 1;
}
}

// Clearing is simulated by rendering a big square with the depth value
Expand All @@ -1177,12 +1160,17 @@ void glClear(GLbitfield mask)
GX_SetNumTexGens(1);

/* Create a 1x1 Z-texture to set the desired depth */
/* Our z-buffer depth is 24 bits */
uint32_t depth = glparamstate.clearz * ((1 << 24) - 1);
s_zbuffer_texels[0] = 0xff; // ignored
s_zbuffer_texels[1] = (depth >> 16) & 0xff;
s_zbuffer_texels[32] = (depth >> 8) & 0xff;
s_zbuffer_texels[33] = depth & 0xff;
if (glparamstate.dirty.bits.dirty_clearz) {
/* Our z-buffer depth is 24 bits */
uint32_t depth = glparamstate.clearz * ((1 << 24) - 1);
s_zbuffer_texels[0] = 0xff; // ignored
s_zbuffer_texels[1] = (depth >> 16) & 0xff;
s_zbuffer_texels[32] = (depth >> 8) & 0xff;
s_zbuffer_texels[33] = depth & 0xff;
DCStoreRange(s_zbuffer_texels, sizeof(s_zbuffer_texels));
GX_InvalidateTexAll();
glparamstate.dirty.bits.dirty_clearz = 0;
}
GX_LoadTexObj(&s_zbuffer_texture, GX_TEXMAP0);
GX_SetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD0, GX_TEXMAP0, GX_COLOR0A0);
} else {
Expand Down Expand Up @@ -1239,7 +1227,14 @@ void glClear(GLbitfield mask)
GX_End();

GX_SetZTexture(GX_ZT_DISABLE, GX_TF_Z24X8, 0);
glparamstate.dirty.all = ~0;

glparamstate.dirty.bits.dirty_alphatest = 1;
glparamstate.dirty.bits.dirty_blend = 1;
glparamstate.dirty.bits.dirty_z = 1;
glparamstate.dirty.bits.dirty_color_update = 1;
glparamstate.dirty.bits.dirty_matrices = 1;
glparamstate.dirty.bits.dirty_cull = 1;
glparamstate.dirty.bits.dirty_texture_gen = 1;
}

void glDepthFunc(GLenum func)
Expand Down Expand Up @@ -1398,10 +1393,8 @@ void glLineWidth(GLfloat width)

void glColorMask(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha)
{
if ((red | green | blue | alpha) != 0)
GX_SetColorUpdate(GX_TRUE);
else
GX_SetColorUpdate(GX_FALSE);
glparamstate.color_update = (red | green | blue | alpha) != 0;
glparamstate.dirty.bits.dirty_color_update = 1;
}

/*
Expand Down Expand Up @@ -1972,6 +1965,7 @@ static void setup_texture_stage(u8 stage, u8 raster_color, u8 raster_alpha,
GX_SetNumTexGens(1);
if (glparamstate.dirty.bits.dirty_texture_gen) {
setup_texture_gen();
glparamstate.dirty.bits.dirty_texture_gen = 0;
}
}

Expand Down Expand Up @@ -2139,6 +2133,10 @@ void _ogx_apply_state()
if (glparamstate.dirty.bits.dirty_z)
GX_SetZMode(glparamstate.ztest, glparamstate.zfunc, glparamstate.zwrite & glparamstate.ztest);

if (glparamstate.dirty.bits.dirty_color_update) {
GX_SetColorUpdate(glparamstate.color_update ? GX_TRUE : GX_FALSE);
}

if (glparamstate.dirty.bits.dirty_blend) {
if (glparamstate.blendenabled)
GX_SetBlendMode(GX_BM_BLEND, glparamstate.srcblend, glparamstate.dstblend, GX_LO_CLEAR);
Expand Down Expand Up @@ -2170,8 +2168,15 @@ void _ogx_apply_state()
NORMAL_UPDATE
}

// All the state has been transferred, no need to update it again next time
glparamstate.dirty.all = 0;
/* Reset the updated bits to 0. We don't unconditionally reset everything
* to 0 because some states might still be dirty. */
glparamstate.dirty.bits.dirty_cull = 0;
glparamstate.dirty.bits.dirty_lighting = 0;
glparamstate.dirty.bits.dirty_matrices = 0;
glparamstate.dirty.bits.dirty_alphatest = 0;
glparamstate.dirty.bits.dirty_blend = 0;
glparamstate.dirty.bits.dirty_color_update = 0;
glparamstate.dirty.bits.dirty_z = 0;
}

void glDrawArrays(GLenum mode, GLint first, GLsizei count)
Expand Down Expand Up @@ -2201,34 +2206,8 @@ void glDrawArrays(GLenum mode, GLint first, GLsizei count)
color_provide = 1;
}

// Not using indices
GX_ClearVtxDesc();
if (glparamstate.cs.vertex_enabled)
GX_SetVtxDesc(GX_VA_POS, GX_DIRECT);
if (glparamstate.cs.normal_enabled)
GX_SetVtxDesc(GX_VA_NRM, GX_DIRECT);
if (color_provide)
GX_SetVtxDesc(GX_VA_CLR0, GX_DIRECT);
if (color_provide == 2)
GX_SetVtxDesc(GX_VA_CLR1, GX_DIRECT);
if (texen)
GX_SetVtxDesc(GX_VA_TEX0, GX_DIRECT);

// Using floats
GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_F32, 0);
GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_NRM, GX_NRM_XYZ, GX_F32, 0);
GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_TEX0, GX_TEX_ST, GX_F32, 0);
GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGBA, GX_RGBA8, 0);
GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_CLR1, GX_CLR_RGBA, GX_RGBA8, 0);

// Invalidate vertex data as may have been modified by the user
GX_InvVtxCache();

bool loop = (mode == GL_LINE_LOOP);
GX_Begin(gxmode, GX_VTXFMT0, count + loop);
draw_arrays_general(first, count, glparamstate.cs.normal_enabled,
color_provide, texen, loop);
GX_End();
draw_arrays_general(gxmode, first, count, glparamstate.cs.normal_enabled,
color_provide, texen);
}

void glDrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices)
Expand Down Expand Up @@ -2313,10 +2292,34 @@ void glDrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indic
GX_End();
}

static void draw_arrays_general(int first, int count, int ne,
int color_provide, int texen, bool loop)
static void draw_arrays_general(uint8_t gxmode, int first, int count, int ne,
int color_provide, int texen)
{
// Not using indices
GX_ClearVtxDesc();
if (glparamstate.cs.vertex_enabled)
GX_SetVtxDesc(GX_VA_POS, GX_DIRECT);
if (ne)
GX_SetVtxDesc(GX_VA_NRM, GX_DIRECT);
if (color_provide)
GX_SetVtxDesc(GX_VA_CLR0, GX_DIRECT);
if (color_provide == 2)
GX_SetVtxDesc(GX_VA_CLR1, GX_DIRECT);
if (texen)
GX_SetVtxDesc(GX_VA_TEX0, GX_DIRECT);

// Using floats
GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_F32, 0);
GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_NRM, GX_NRM_XYZ, GX_F32, 0);
GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_TEX0, GX_TEX_ST, GX_F32, 0);
GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGBA, GX_RGBA8, 0);
GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_CLR1, GX_CLR_RGBA, GX_RGBA8, 0);

// Invalidate vertex data as may have been modified by the user
GX_InvVtxCache();

bool loop = (gxmode == GL_LINE_LOOP);
GX_Begin(gxmode, GX_VTXFMT0, count + loop);
int i;
for (i = 0; i < count + loop; i++) {
int j = i % count + first;
Expand Down Expand Up @@ -2344,6 +2347,7 @@ static void draw_arrays_general(int first, int count, int ne,
GX_TexCoord2f32(value[0], value[1]);
}
}
GX_End();
}

void glFrustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top,
Expand Down
3 changes: 3 additions & 0 deletions src/state.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ typedef struct glparams_
unsigned char zwrite, ztest, zfunc;
unsigned char matrixmode;
unsigned char frontcw, cullenabled;
bool color_update;
uint8_t alpha_func, alpha_ref, alphatest_enabled;
uint16_t texture_env_mode;
/* There should be 4 of these (for S, T, R, Q) but GX uses a single
Expand Down Expand Up @@ -149,6 +150,8 @@ typedef struct glparams_
unsigned dirty_alphatest : 1;
unsigned dirty_blend : 1;
unsigned dirty_z : 1;
unsigned dirty_clearz : 1;
unsigned dirty_color_update : 1;
unsigned dirty_matrices : 1;
unsigned dirty_lighting : 1;
unsigned dirty_material : 1;
Expand Down
15 changes: 15 additions & 0 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,21 @@ static inline void gl_matrix_to_gx44(const GLfloat *source, Mtx44 mv)
}
}

static inline uint8_t gx_compare_from_gl(GLenum func)
{
switch (func) {
case GL_NEVER: return GX_NEVER;
case GL_LESS: return GX_LESS;
case GL_EQUAL: return GX_EQUAL;
case GL_LEQUAL: return GX_LEQUAL;
case GL_GREATER: return GX_GREATER;
case GL_NOTEQUAL: return GX_NEQUAL;
case GL_GEQUAL: return GX_GEQUAL;
case GL_ALWAYS: return GX_ALWAYS;
default: return 0xff;
}
}

/* Set up the matrices for 2D pixel-perfect drawing */
void _ogx_setup_2D_projection(void);

Expand Down