Skip to content

Commit

Permalink
shaders: rename public APIs for matrix management
Browse files Browse the repository at this point in the history
To make clear that these functions are to be called only when
implementing shaders in the client code, rename them to have a
"ogx_shader" prefix. Also use the "_gx" or "_gl" suffix to indicate
whether they expect the matrix to be in the GX or OpenGL format.
  • Loading branch information
mardy committed Feb 1, 2025
1 parent 2688641 commit 0e56aec
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 15 deletions.
4 changes: 2 additions & 2 deletions examples/sdl2/opengl20/opengx_shaders.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ static void gl2gears_setup_draw(GLuint program, const OgxDrawData *draw_data,
glGetUniformfv(program, data->mat_color_loc, colorf);
float light_dir[4];
glGetUniformfv(program, data->light_pos_loc, light_dir);
ogx_set_mvp_matrix(m);
ogx_shader_set_mvp_gl(m);

Mtx normalm;
ogx_matrix_gl_to_mtx(normal_matrix, normalm);
Expand Down Expand Up @@ -80,7 +80,7 @@ static void cube_tex_setup_draw(GLuint program, const OgxDrawData *draw_data,
glGetUniformfv(program, data->mvp_loc, m);
GLint texture_unit;
glGetUniformiv(program, data->tex_sampler_loc, &texture_unit);
ogx_set_mvp_matrix(m);
ogx_shader_set_mvp_gl(m);

uint8_t tex_map = GX_TEXMAP0;
uint8_t tex_coord = GX_TEXCOORD0;
Expand Down
4 changes: 2 additions & 2 deletions src/gc_gl.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ static void get_projection_info(const Mtx44 matrix, u8 *type, float *near, float
}
}

void ogx_set_projection_gx(const Mtx44 matrix)
void _ogx_set_projection(const Mtx44 matrix)
{
/* OpenGL's projection matrix transform the scene into a clip space where
* all the coordinates lie in the range [-1, 1]. Nintendo's GX, however,
Expand Down Expand Up @@ -154,7 +154,7 @@ void ogx_set_projection_gx(const Mtx44 matrix)

static inline void update_projection_matrix()
{
ogx_set_projection_gx(glparamstate.projection_matrix);
_ogx_set_projection(glparamstate.projection_matrix);
}

static inline void update_normal_matrix()
Expand Down
23 changes: 16 additions & 7 deletions src/opengx.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,19 +176,15 @@ void ogx_shader_setup_attribute_array(int index, uint8_t gx_attr,
const OgxDrawData *draw_data);
void *ogx_shader_get_data(GLuint shader);

void ogx_set_projection_gx(const Mtx44 matrix);
static inline void ogx_set_projection_gl(const GLfloat *matrix)
void ogx_shader_set_projection_gx(const Mtx44 matrix);
static inline void ogx_shader_set_projection_gl(const GLfloat *matrix)
{
Mtx44 m;
for (int i = 0; i < 16; i++) {
m[i % 4][i / 4] = matrix[i];
}
ogx_set_projection_gx(m);
ogx_shader_set_projection_gx(m);
}
/* Many OpenGL 2.0+ apps pass a uniform with the model-view-projection matrix
* to the vertex shader. This function decomposes it into MV and projection
* matrices, and uploads them separately to GX. */
void ogx_set_mvp_matrix(const GLfloat *matrix);

static inline void ogx_matrix_gl_to_mtx(const GLfloat *matrix, Mtx m)
{
Expand All @@ -199,6 +195,19 @@ static inline void ogx_matrix_gl_to_mtx(const GLfloat *matrix, Mtx m)
}
}

void ogx_shader_set_modelview_gx(const Mtx matrix);
static inline void ogx_shader_set_modelview_gl(const GLfloat *matrix)
{
Mtx m;
ogx_matrix_gl_to_mtx(matrix, m);
ogx_shader_set_modelview_gx(m);
}

/* Many OpenGL 2.0+ apps pass a uniform with the model-view-projection matrix
* to the vertex shader. This function decomposes it into MV and projection
* matrices, and uploads them separately to GX. */
void ogx_shader_set_mvp_gl(const GLfloat *matrix);

#ifdef __cplusplus
} // extern C
#endif
Expand Down
19 changes: 15 additions & 4 deletions src/shader_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/

#define BUILDING_SHADER_CODE
#include "debug.h"
#include "opengx.h"
#include "shader.h"
Expand All @@ -44,7 +45,18 @@ static void scale_matrix(const GLfloat *matrix, float divisor, float *out)
out[i] = matrix[i] / divisor;
}

void ogx_set_mvp_matrix(const GLfloat *matrix)
void ogx_shader_set_projection_gx(const Mtx44 matrix)
{
_ogx_set_projection(matrix);
}

void ogx_shader_set_modelview_gx(const Mtx matrix)
{
GX_LoadPosMtxImm((void*)matrix, GX_PNMTX0);
GX_SetCurrentMtx(GX_PNMTX0);
}

void ogx_shader_set_mvp_gl(const GLfloat *matrix)
{
Mtx44 proj;
Mtx mv;
Expand Down Expand Up @@ -95,9 +107,8 @@ void ogx_set_mvp_matrix(const GLfloat *matrix)
mv[row][col] = matrix[col * 4 + row];
}
}
ogx_set_projection_gx(proj);
GX_LoadPosMtxImm(mv, GX_PNMTX0);
GX_SetCurrentMtx(GX_PNMTX0);
ogx_shader_set_projection_gx(proj);
ogx_shader_set_modelview_gx(mv);

/* In the unlikely case that some fixed-pipeline drawing happens, we mark
* the matrices as dirty so that they'd be reconfigured when needed. */
Expand Down
2 changes: 2 additions & 0 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,8 @@ void _ogx_setup_2D_projection(void);
/* Set the OpenGL 3D modelview and projection matrices */
void _ogx_setup_3D_projection(void);

void _ogx_set_projection(const Mtx44 matrix);

bool _ogx_setup_render_stages(void);
void _ogx_update_vertex_array_readers(OgxDrawMode mode);

Expand Down

0 comments on commit 0e56aec

Please sign in to comment.