From 0e56aecda13040e17db4320fa5e774b97da69067 Mon Sep 17 00:00:00 2001 From: Alberto Mardegan Date: Sat, 1 Feb 2025 14:57:59 +0300 Subject: [PATCH] shaders: rename public APIs for matrix management 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. --- examples/sdl2/opengl20/opengx_shaders.c | 4 ++-- src/gc_gl.c | 4 ++-- src/opengx.h | 23 ++++++++++++++++------- src/shader_api.c | 19 +++++++++++++++---- src/utils.h | 2 ++ 5 files changed, 37 insertions(+), 15 deletions(-) diff --git a/examples/sdl2/opengl20/opengx_shaders.c b/examples/sdl2/opengl20/opengx_shaders.c index 296f3e5..93f4b2d 100644 --- a/examples/sdl2/opengl20/opengx_shaders.c +++ b/examples/sdl2/opengl20/opengx_shaders.c @@ -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); @@ -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; diff --git a/src/gc_gl.c b/src/gc_gl.c index 12b2bdd..ccd1b11 100644 --- a/src/gc_gl.c +++ b/src/gc_gl.c @@ -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, @@ -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() diff --git a/src/opengx.h b/src/opengx.h index ded06c1..90346e4 100644 --- a/src/opengx.h +++ b/src/opengx.h @@ -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) { @@ -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 diff --git a/src/shader_api.c b/src/shader_api.c index 5b7e0d2..507ca9c 100644 --- a/src/shader_api.c +++ b/src/shader_api.c @@ -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" @@ -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; @@ -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. */ diff --git a/src/utils.h b/src/utils.h index 8e1b0bd..e1cc3ff 100644 --- a/src/utils.h +++ b/src/utils.h @@ -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);