Skip to content

Commit

Permalink
WIP all the rest
Browse files Browse the repository at this point in the history
  • Loading branch information
mardy committed Feb 1, 2025
1 parent 6f4e511 commit 1887c78
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 95 deletions.
5 changes: 5 additions & 0 deletions examples/sdl2/opengl20/cube_tex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,11 @@ int main(int argc, char **argv)
// Clear the screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

/* Add a clipping plane that rotates around the cube */
GLdouble clip_plane[4] = { sin(dt), cos(dt), 0.2, 0.1 };
glEnable(GL_CLIP_PLANE0);
glClipPlane(GL_CLIP_PLANE0, clip_plane);

// Use our shader
glUseProgram(programID);

Expand Down
8 changes: 3 additions & 5 deletions examples/sdl2/opengl20/opengx_shaders.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ static void cube_tex_setup_draw(GLuint program, const OgxDrawData *draw_data,
glGetUniformiv(program, data->tex_sampler_loc, &texture_unit);
ogx_shader_set_mvp_gl(m);

uint8_t tex_map = GX_TEXMAP0;
uint8_t tex_coord = GX_TEXCOORD0;
uint8_t tex_map = GX_TEXMAP0 + ogx_gpu_resources->texmap_first++;
uint8_t tex_coord = GX_TEXCOORD0 + ogx_gpu_resources->texcoord_first++;
uint8_t input_coordinates = GX_TG_TEX0;
uint8_t stage = GX_TEVSTAGE0;
uint8_t stage = GX_TEVSTAGE0 + ogx_gpu_resources->tevstage_first++;
GXTexObj *texture;
texture = ogx_shader_get_texobj(texture_unit);
GX_LoadTexObj(texture, tex_map);
Expand All @@ -102,9 +102,7 @@ static void cube_tex_setup_draw(GLuint program, const OgxDrawData *draw_data,
GX_TEVPREV);
GX_SetTexCoordGen(tex_coord, GX_TG_MTX2x4, input_coordinates, GX_IDENTITY);

GX_SetNumTevStages(1);
GX_SetTevOrder(stage, tex_coord, tex_map, GX_COLOR0A0);
GX_SetNumTexGens(1);
}

static bool shader_compile(GLuint shader)
Expand Down
21 changes: 16 additions & 5 deletions src/clip.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ static void load_clip_texture(u8 tex_map)

static bool setup_tev(u8 tex_map, int plane_index0, int plane_index1)
{
u8 stage = GX_TEVSTAGE0 + _ogx_gpu_resources->tevstage_first++;
u8 tex_coord = GX_TEXCOORD0 + _ogx_gpu_resources->texcoord_first++;
u8 tex_mtx = GX_TEXMTX0 + _ogx_gpu_resources->texmtx_first++ * 3;
u8 stage = GX_TEVSTAGE0 + ogx_gpu_resources->tevstage_first++;
u8 tex_coord = GX_TEXCOORD0 + ogx_gpu_resources->texcoord_first++;
u8 tex_mtx = GX_TEXMTX0 + ogx_gpu_resources->texmtx_first++ * 3;

debug(OGX_LOG_CLIPPING, "%d TEV stages, %d tex_coords, %d tex_maps",
stage, tex_coord, tex_map);
Expand Down Expand Up @@ -100,7 +100,18 @@ static bool setup_tev(u8 tex_map, int plane_index0, int plane_index1)
* than zero ensures that we end up in the right quadrant) */
set_gx_mtx_row(1, planes, 0.0f, 0.0f, 0.0f, 1.0f);
}
guMtxConcat(planes, glparamstate.modelview_matrix, m);
{
Mtx mv;
memcpy(&mv, &planes, sizeof(mv));
fprintf(stderr, "MV matrix: %f %f %f %f\n"
" %f %f %f %f\n"
" %f %f %f %f\n",
mv[0][0], mv[0][1], mv[0][2], mv[0][3],
mv[1][0], mv[1][1], mv[1][2], mv[1][3],
mv[2][0], mv[2][1], mv[2][2], mv[2][3]);
}

guMtxConcat(planes, *glparamstate.mv_ptr, m);
/* Our texture has coordinates [0,1]x[0,1] and is made of four texels. The
* centre of our texture is (0.5, 0.5), therefore we need to map the zero
* point to that. We do that by translating the texture coordinates by 0.5.
Expand Down Expand Up @@ -140,7 +151,7 @@ bool _ogx_clip_is_point_clipped(const guVector *p)
void _ogx_clip_setup_tev()
{
debug(OGX_LOG_CLIPPING, "setting up clip TEV");
u8 tex_map = GX_TEXMAP0 + _ogx_gpu_resources->texmap_first++;
u8 tex_map = GX_TEXMAP0 + ogx_gpu_resources->texmap_first++;
load_clip_texture(tex_map);

int plane_index0 = -1;
Expand Down
47 changes: 28 additions & 19 deletions src/gc_gl.c
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ void ogx_initialize()
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glparamstate.mv_ptr = &glparamstate.modelview_matrix;
glparamstate.proj_ptr = &glparamstate.projection_matrix;

glparamstate.scissor[0] = glparamstate.scissor[1] = 0;
/* Scissor width and height are initialized when a window is attached */
Expand Down Expand Up @@ -2268,13 +2270,32 @@ static void setup_fog()
GX_SetFog(mode, start, end, near, far, color);
}

static bool setup_common_stages()
{
if (glparamstate.stencil.enabled) {
bool should_draw = _ogx_stencil_setup_tev();
if (!should_draw) return false;
}

if (glparamstate.clip_plane_mask != 0) {
_ogx_clip_setup_tev();
}

/* Stages and texture coordinate slots must be enabled sequentially, so we
* know that the number of used resources is given by
* OgxGpuResources::{tevstage,texcoord}_first. */
GX_SetNumTevStages(ogx_gpu_resources->tevstage_first);
GX_SetNumTexGens(ogx_gpu_resources->texcoord_first);
return true;
}

bool _ogx_setup_render_stages()
{
if (!glparamstate.dirty.bits.dirty_tev) return true;

u8 raster_output, raster_reg_index;
if (glparamstate.texture_enabled) {
raster_reg_index = _ogx_gpu_resources->tevreg_first++;
raster_reg_index = ogx_gpu_resources->tevreg_first++;
raster_output = GX_TEVREG0 + raster_reg_index;
} else {
raster_output = GX_TEVPREV;
Expand All @@ -2286,7 +2307,7 @@ bool _ogx_setup_render_stages()
GXColor color_black = { 0, 0, 0, 255 };
GXColor color_gamb = gxcol_new_fv(glparamstate.lighting.globalambient);

_ogx_gpu_resources->tevstage_first += 2;
ogx_gpu_resources->tevstage_first += 2;
GX_SetNumChans(2);

unsigned char vert_color_src = GX_SRC_VTX;
Expand Down Expand Up @@ -2357,7 +2378,7 @@ bool _ogx_setup_render_stages()

// STAGE 0: ambient*vert_color -> cprev
// In data: d: Raster Color, a: emission color
u8 emission_reg = _ogx_gpu_resources->tevreg_first++;
u8 emission_reg = ogx_gpu_resources->tevreg_first++;
GX_SetTevColor(GX_TEVREG0 + emission_reg, ecol);
/* Multiply by two because there are alpha registers in between */
GX_SetTevColorIn(GX_TEVSTAGE0, GX_CC_C0 + emission_reg * 2,
Expand Down Expand Up @@ -2409,7 +2430,7 @@ bool _ogx_setup_render_stages()
_ogx_setup_texture_stages(raster_reg_index, GX_COLOR0A0);
} else {
// Use one stage only
_ogx_gpu_resources->tevstage_first += 1;
ogx_gpu_resources->tevstage_first += 1;
// In data: d: Raster Color
GX_SetTevColorIn(GX_TEVSTAGE0, GX_CC_ZERO, GX_CC_ZERO, GX_CC_ZERO, GX_CC_RASC);
GX_SetTevAlphaIn(GX_TEVSTAGE0, GX_CA_ZERO, GX_CA_ZERO, GX_CA_ZERO, GX_CA_RASA);
Expand All @@ -2421,22 +2442,9 @@ bool _ogx_setup_render_stages()
}
}

if (glparamstate.stencil.enabled) {
bool should_draw = _ogx_stencil_setup_tev();
if (!should_draw) return false;
}

if (glparamstate.clip_plane_mask != 0) {
_ogx_clip_setup_tev();
}

/* Stages and texture coordinate slots must be enabled sequentially, so we
* know that the number of used resources is given by
* OgxGpuResources::{tevstage,texcoord}_first. */
GX_SetNumTevStages(_ogx_gpu_resources->tevstage_first);
GX_SetNumTexGens(_ogx_gpu_resources->texcoord_first);
bool should_draw = setup_common_stages();
glparamstate.dirty.bits.dirty_tev = false;
return true;
return should_draw;
}

static inline void apply_state_fixed_pipeline()
Expand Down Expand Up @@ -2629,6 +2637,7 @@ static bool setup_draw(const OgxDrawData *draw_data)
if (!should_draw) return false;
} else {
_ogx_shader_setup_draw(draw_data);
if (!setup_common_stages()) return false;
}
_ogx_apply_state();
return true;
Expand Down
13 changes: 7 additions & 6 deletions src/gpu_resources.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/

#include "gpu_resources.h"
#include "opengx.h"

#include <assert.h>
#include <ogc/gx.h>
Expand All @@ -39,7 +40,7 @@ POSSIBILITY OF SUCH DAMAGE.
* value, if needed). */
#define GPU_RESOURCES_STACK_SIZE 3

OgxGpuResources *_ogx_gpu_resources = NULL;
OgxGpuResources *ogx_gpu_resources = NULL;
OgxGpuResources *s_gpu_resources = NULL;

static void resources_init(OgxGpuResources *resources)
Expand Down Expand Up @@ -79,17 +80,17 @@ void _ogx_gpu_resources_init()
s_gpu_resources =
malloc(sizeof(OgxGpuResources) * GPU_RESOURCES_STACK_SIZE);
resources_init(s_gpu_resources);
_ogx_gpu_resources = s_gpu_resources;
ogx_gpu_resources = s_gpu_resources;
}

void _ogx_gpu_resources_push()
{
OgxGpuResources *old = _ogx_gpu_resources;
memcpy(++_ogx_gpu_resources, old, sizeof(OgxGpuResources));
OgxGpuResources *old = ogx_gpu_resources;
memcpy(++ogx_gpu_resources, old, sizeof(OgxGpuResources));
}

void _ogx_gpu_resources_pop()
{
assert(_ogx_gpu_resources != s_gpu_resources);
_ogx_gpu_resources--;
assert(ogx_gpu_resources != s_gpu_resources);
ogx_gpu_resources--;
}
40 changes: 0 additions & 40 deletions src/gpu_resources.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,46 +36,6 @@ POSSIBILITY OF SUCH DAMAGE.
extern "C" {
#endif

typedef struct {
/* *_first: number of the first available resource
* *_end: number of the first *not* available resource
*
* The number of available resources is X_end - X_first. Each member
* specifies the number starting from zero, so that in order to get the ID
* of the desired resource, you need to add the base ID of the resource:
* for example, to get the actual stage number, you'd have to do
*
* stage = number + GX_TEVSTAGE0
*
* and, for matrix types,
*
* texmtx = number * 3 + GX_TEXMTX0
*
* Fields are named according to libogc's constants, to minimize confusion.
*/
uint8_t tevstage_first;
uint8_t tevstage_end;
uint8_t kcolor_first;
uint8_t kcolor_end;
uint8_t tevreg_first;
uint8_t tevreg_end;
uint8_t texcoord_first;
uint8_t texcoord_end;
uint8_t pnmtx_first;
uint8_t pnmtx_end;
uint8_t dttmtx_first;
uint8_t dttmtx_end;
uint8_t texmtx_first;
uint8_t texmtx_end;
uint8_t texmap_first;
uint8_t texmap_end;
/* We could add the VTXFMT here too, if we decided to reserve them for
* specific goals; for the time being, we only use GX_VTXFMT0 and set it up
* from scratch every time. */
} OgxGpuResources;

extern OgxGpuResources *_ogx_gpu_resources;

void _ogx_gpu_resources_init();
void _ogx_gpu_resources_push();
void _ogx_gpu_resources_pop();
Expand Down
40 changes: 40 additions & 0 deletions src/opengx.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,46 @@ void ogx_stencil_create(OgxStencilFlags flags);

/* Support for GLSL emulation */

typedef struct {
/* *_first: number of the first available resource
* *_end: number of the first *not* available resource
*
* The number of available resources is X_end - X_first. Each member
* specifies the number starting from zero, so that in order to get the ID
* of the desired resource, you need to add the base ID of the resource:
* for example, to get the actual stage number, you'd have to do
*
* stage = number + GX_TEVSTAGE0
*
* and, for matrix types,
*
* texmtx = number * 3 + GX_TEXMTX0
*
* Fields are named according to libogc's constants, to minimize confusion.
*/
uint8_t tevstage_first;
uint8_t tevstage_end;
uint8_t kcolor_first;
uint8_t kcolor_end;
uint8_t tevreg_first;
uint8_t tevreg_end;
uint8_t texcoord_first;
uint8_t texcoord_end;
uint8_t pnmtx_first;
uint8_t pnmtx_end;
uint8_t dttmtx_first;
uint8_t dttmtx_end;
uint8_t texmtx_first;
uint8_t texmtx_end;
uint8_t texmap_first;
uint8_t texmap_end;
/* We could add the VTXFMT here too, if we decided to reserve them for
* specific goals; for the time being, we only use GX_VTXFMT0 and set it up
* from scratch every time. */
} OgxGpuResources;

extern OgxGpuResources *ogx_gpu_resources;

typedef struct _OgxDrawMode {
uint8_t mode;
bool loop;
Expand Down
8 changes: 8 additions & 0 deletions src/shader.c
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,14 @@ void glUseProgram(GLuint program)
glparamstate.current_program = program;
glparamstate.dirty.bits.dirty_attributes = 1;

if (program != 0) {
glparamstate.mv_ptr = &_ogx_shader_state.mv_matrix;
glparamstate.proj_ptr = &_ogx_shader_state.proj_matrix;
} else {
glparamstate.mv_ptr = &glparamstate.modelview_matrix;
glparamstate.proj_ptr = &glparamstate.projection_matrix;
}

if (old && old->deletion_requested) {
glDeleteProgram(PROGRAM_TO_INT(old));
}
Expand Down
4 changes: 4 additions & 0 deletions src/shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ typedef struct {
* separate array so that we can use consecutive elements as matrix
* columns. */
Vec4f vertex_attrib_data[MAX_VERTEX_ATTRIBS];

/* Transformation matrices */
Mtx mv_matrix;
Mtx44 proj_matrix;
} OgxShaderState;

typedef struct _OgxVertexAttribState OgxVertexAttribState;
Expand Down
6 changes: 5 additions & 1 deletion src/shader_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,16 @@ static void scale_matrix(const GLfloat *matrix, float divisor, float *out)

void ogx_shader_set_projection_gx(const Mtx44 matrix)
{
memcpy(_ogx_shader_state.proj_matrix, matrix,
sizeof(_ogx_shader_state.proj_matrix));
_ogx_set_projection(matrix);
}

void ogx_shader_set_modelview_gx(const Mtx matrix)
{
GX_LoadPosMtxImm((void*)mv_matrix, GX_PNMTX0);
memcpy(_ogx_shader_state.mv_matrix, matrix,
sizeof(_ogx_shader_state.mv_matrix));
GX_LoadPosMtxImm(_ogx_shader_state.mv_matrix, GX_PNMTX0);
GX_SetCurrentMtx(GX_PNMTX0);
}

Expand Down
2 changes: 2 additions & 0 deletions src/state.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ typedef struct glparams_
Mtx44 projection_matrix;
Mtx modelview_stack[MAX_MODV_STACK];
Mtx44 projection_stack[MAX_PROJ_STACK];
Mtx *mv_ptr;
Mtx44 *proj_ptr;
ClipPlane clip_planes[MAX_CLIP_PLANES];
float raster_pos[4];
float pixel_zoom_x;
Expand Down
Loading

0 comments on commit 1887c78

Please sign in to comment.