Skip to content

Commit 1887c78

Browse files
committed
WIP all the rest
1 parent 6f4e511 commit 1887c78

File tree

13 files changed

+137
-95
lines changed

13 files changed

+137
-95
lines changed

examples/sdl2/opengl20/cube_tex.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,11 @@ int main(int argc, char **argv)
309309
// Clear the screen
310310
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
311311

312+
/* Add a clipping plane that rotates around the cube */
313+
GLdouble clip_plane[4] = { sin(dt), cos(dt), 0.2, 0.1 };
314+
glEnable(GL_CLIP_PLANE0);
315+
glClipPlane(GL_CLIP_PLANE0, clip_plane);
316+
312317
// Use our shader
313318
glUseProgram(programID);
314319

examples/sdl2/opengl20/opengx_shaders.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,10 @@ static void cube_tex_setup_draw(GLuint program, const OgxDrawData *draw_data,
8282
glGetUniformiv(program, data->tex_sampler_loc, &texture_unit);
8383
ogx_shader_set_mvp_gl(m);
8484

85-
uint8_t tex_map = GX_TEXMAP0;
86-
uint8_t tex_coord = GX_TEXCOORD0;
85+
uint8_t tex_map = GX_TEXMAP0 + ogx_gpu_resources->texmap_first++;
86+
uint8_t tex_coord = GX_TEXCOORD0 + ogx_gpu_resources->texcoord_first++;
8787
uint8_t input_coordinates = GX_TG_TEX0;
88-
uint8_t stage = GX_TEVSTAGE0;
88+
uint8_t stage = GX_TEVSTAGE0 + ogx_gpu_resources->tevstage_first++;
8989
GXTexObj *texture;
9090
texture = ogx_shader_get_texobj(texture_unit);
9191
GX_LoadTexObj(texture, tex_map);
@@ -102,9 +102,7 @@ static void cube_tex_setup_draw(GLuint program, const OgxDrawData *draw_data,
102102
GX_TEVPREV);
103103
GX_SetTexCoordGen(tex_coord, GX_TG_MTX2x4, input_coordinates, GX_IDENTITY);
104104

105-
GX_SetNumTevStages(1);
106105
GX_SetTevOrder(stage, tex_coord, tex_map, GX_COLOR0A0);
107-
GX_SetNumTexGens(1);
108106
}
109107

110108
static bool shader_compile(GLuint shader)

src/clip.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ static void load_clip_texture(u8 tex_map)
6868

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

7575
debug(OGX_LOG_CLIPPING, "%d TEV stages, %d tex_coords, %d tex_maps",
7676
stage, tex_coord, tex_map);
@@ -100,7 +100,18 @@ static bool setup_tev(u8 tex_map, int plane_index0, int plane_index1)
100100
* than zero ensures that we end up in the right quadrant) */
101101
set_gx_mtx_row(1, planes, 0.0f, 0.0f, 0.0f, 1.0f);
102102
}
103-
guMtxConcat(planes, glparamstate.modelview_matrix, m);
103+
{
104+
Mtx mv;
105+
memcpy(&mv, &planes, sizeof(mv));
106+
fprintf(stderr, "MV matrix: %f %f %f %f\n"
107+
" %f %f %f %f\n"
108+
" %f %f %f %f\n",
109+
mv[0][0], mv[0][1], mv[0][2], mv[0][3],
110+
mv[1][0], mv[1][1], mv[1][2], mv[1][3],
111+
mv[2][0], mv[2][1], mv[2][2], mv[2][3]);
112+
}
113+
114+
guMtxConcat(planes, *glparamstate.mv_ptr, m);
104115
/* Our texture has coordinates [0,1]x[0,1] and is made of four texels. The
105116
* centre of our texture is (0.5, 0.5), therefore we need to map the zero
106117
* point to that. We do that by translating the texture coordinates by 0.5.
@@ -140,7 +151,7 @@ bool _ogx_clip_is_point_clipped(const guVector *p)
140151
void _ogx_clip_setup_tev()
141152
{
142153
debug(OGX_LOG_CLIPPING, "setting up clip TEV");
143-
u8 tex_map = GX_TEXMAP0 + _ogx_gpu_resources->texmap_first++;
154+
u8 tex_map = GX_TEXMAP0 + ogx_gpu_resources->texmap_first++;
144155
load_clip_texture(tex_map);
145156

146157
int plane_index0 = -1;

src/gc_gl.c

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,8 @@ void ogx_initialize()
310310
glLoadIdentity();
311311
glMatrixMode(GL_MODELVIEW);
312312
glLoadIdentity();
313+
glparamstate.mv_ptr = &glparamstate.modelview_matrix;
314+
glparamstate.proj_ptr = &glparamstate.projection_matrix;
313315

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

2273+
static bool setup_common_stages()
2274+
{
2275+
if (glparamstate.stencil.enabled) {
2276+
bool should_draw = _ogx_stencil_setup_tev();
2277+
if (!should_draw) return false;
2278+
}
2279+
2280+
if (glparamstate.clip_plane_mask != 0) {
2281+
_ogx_clip_setup_tev();
2282+
}
2283+
2284+
/* Stages and texture coordinate slots must be enabled sequentially, so we
2285+
* know that the number of used resources is given by
2286+
* OgxGpuResources::{tevstage,texcoord}_first. */
2287+
GX_SetNumTevStages(ogx_gpu_resources->tevstage_first);
2288+
GX_SetNumTexGens(ogx_gpu_resources->texcoord_first);
2289+
return true;
2290+
}
2291+
22712292
bool _ogx_setup_render_stages()
22722293
{
22732294
if (!glparamstate.dirty.bits.dirty_tev) return true;
22742295

22752296
u8 raster_output, raster_reg_index;
22762297
if (glparamstate.texture_enabled) {
2277-
raster_reg_index = _ogx_gpu_resources->tevreg_first++;
2298+
raster_reg_index = ogx_gpu_resources->tevreg_first++;
22782299
raster_output = GX_TEVREG0 + raster_reg_index;
22792300
} else {
22802301
raster_output = GX_TEVPREV;
@@ -2286,7 +2307,7 @@ bool _ogx_setup_render_stages()
22862307
GXColor color_black = { 0, 0, 0, 255 };
22872308
GXColor color_gamb = gxcol_new_fv(glparamstate.lighting.globalambient);
22882309

2289-
_ogx_gpu_resources->tevstage_first += 2;
2310+
ogx_gpu_resources->tevstage_first += 2;
22902311
GX_SetNumChans(2);
22912312

22922313
unsigned char vert_color_src = GX_SRC_VTX;
@@ -2357,7 +2378,7 @@ bool _ogx_setup_render_stages()
23572378

23582379
// STAGE 0: ambient*vert_color -> cprev
23592380
// In data: d: Raster Color, a: emission color
2360-
u8 emission_reg = _ogx_gpu_resources->tevreg_first++;
2381+
u8 emission_reg = ogx_gpu_resources->tevreg_first++;
23612382
GX_SetTevColor(GX_TEVREG0 + emission_reg, ecol);
23622383
/* Multiply by two because there are alpha registers in between */
23632384
GX_SetTevColorIn(GX_TEVSTAGE0, GX_CC_C0 + emission_reg * 2,
@@ -2409,7 +2430,7 @@ bool _ogx_setup_render_stages()
24092430
_ogx_setup_texture_stages(raster_reg_index, GX_COLOR0A0);
24102431
} else {
24112432
// Use one stage only
2412-
_ogx_gpu_resources->tevstage_first += 1;
2433+
ogx_gpu_resources->tevstage_first += 1;
24132434
// In data: d: Raster Color
24142435
GX_SetTevColorIn(GX_TEVSTAGE0, GX_CC_ZERO, GX_CC_ZERO, GX_CC_ZERO, GX_CC_RASC);
24152436
GX_SetTevAlphaIn(GX_TEVSTAGE0, GX_CA_ZERO, GX_CA_ZERO, GX_CA_ZERO, GX_CA_RASA);
@@ -2421,22 +2442,9 @@ bool _ogx_setup_render_stages()
24212442
}
24222443
}
24232444

2424-
if (glparamstate.stencil.enabled) {
2425-
bool should_draw = _ogx_stencil_setup_tev();
2426-
if (!should_draw) return false;
2427-
}
2428-
2429-
if (glparamstate.clip_plane_mask != 0) {
2430-
_ogx_clip_setup_tev();
2431-
}
2432-
2433-
/* Stages and texture coordinate slots must be enabled sequentially, so we
2434-
* know that the number of used resources is given by
2435-
* OgxGpuResources::{tevstage,texcoord}_first. */
2436-
GX_SetNumTevStages(_ogx_gpu_resources->tevstage_first);
2437-
GX_SetNumTexGens(_ogx_gpu_resources->texcoord_first);
2445+
bool should_draw = setup_common_stages();
24382446
glparamstate.dirty.bits.dirty_tev = false;
2439-
return true;
2447+
return should_draw;
24402448
}
24412449

24422450
static inline void apply_state_fixed_pipeline()
@@ -2629,6 +2637,7 @@ static bool setup_draw(const OgxDrawData *draw_data)
26292637
if (!should_draw) return false;
26302638
} else {
26312639
_ogx_shader_setup_draw(draw_data);
2640+
if (!setup_common_stages()) return false;
26322641
}
26332642
_ogx_apply_state();
26342643
return true;

src/gpu_resources.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ POSSIBILITY OF SUCH DAMAGE.
2828
*****************************************************************************/
2929

3030
#include "gpu_resources.h"
31+
#include "opengx.h"
3132

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

42-
OgxGpuResources *_ogx_gpu_resources = NULL;
43+
OgxGpuResources *ogx_gpu_resources = NULL;
4344
OgxGpuResources *s_gpu_resources = NULL;
4445

4546
static void resources_init(OgxGpuResources *resources)
@@ -79,17 +80,17 @@ void _ogx_gpu_resources_init()
7980
s_gpu_resources =
8081
malloc(sizeof(OgxGpuResources) * GPU_RESOURCES_STACK_SIZE);
8182
resources_init(s_gpu_resources);
82-
_ogx_gpu_resources = s_gpu_resources;
83+
ogx_gpu_resources = s_gpu_resources;
8384
}
8485

8586
void _ogx_gpu_resources_push()
8687
{
87-
OgxGpuResources *old = _ogx_gpu_resources;
88-
memcpy(++_ogx_gpu_resources, old, sizeof(OgxGpuResources));
88+
OgxGpuResources *old = ogx_gpu_resources;
89+
memcpy(++ogx_gpu_resources, old, sizeof(OgxGpuResources));
8990
}
9091

9192
void _ogx_gpu_resources_pop()
9293
{
93-
assert(_ogx_gpu_resources != s_gpu_resources);
94-
_ogx_gpu_resources--;
94+
assert(ogx_gpu_resources != s_gpu_resources);
95+
ogx_gpu_resources--;
9596
}

src/gpu_resources.h

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -36,46 +36,6 @@ POSSIBILITY OF SUCH DAMAGE.
3636
extern "C" {
3737
#endif
3838

39-
typedef struct {
40-
/* *_first: number of the first available resource
41-
* *_end: number of the first *not* available resource
42-
*
43-
* The number of available resources is X_end - X_first. Each member
44-
* specifies the number starting from zero, so that in order to get the ID
45-
* of the desired resource, you need to add the base ID of the resource:
46-
* for example, to get the actual stage number, you'd have to do
47-
*
48-
* stage = number + GX_TEVSTAGE0
49-
*
50-
* and, for matrix types,
51-
*
52-
* texmtx = number * 3 + GX_TEXMTX0
53-
*
54-
* Fields are named according to libogc's constants, to minimize confusion.
55-
*/
56-
uint8_t tevstage_first;
57-
uint8_t tevstage_end;
58-
uint8_t kcolor_first;
59-
uint8_t kcolor_end;
60-
uint8_t tevreg_first;
61-
uint8_t tevreg_end;
62-
uint8_t texcoord_first;
63-
uint8_t texcoord_end;
64-
uint8_t pnmtx_first;
65-
uint8_t pnmtx_end;
66-
uint8_t dttmtx_first;
67-
uint8_t dttmtx_end;
68-
uint8_t texmtx_first;
69-
uint8_t texmtx_end;
70-
uint8_t texmap_first;
71-
uint8_t texmap_end;
72-
/* We could add the VTXFMT here too, if we decided to reserve them for
73-
* specific goals; for the time being, we only use GX_VTXFMT0 and set it up
74-
* from scratch every time. */
75-
} OgxGpuResources;
76-
77-
extern OgxGpuResources *_ogx_gpu_resources;
78-
7939
void _ogx_gpu_resources_init();
8040
void _ogx_gpu_resources_push();
8141
void _ogx_gpu_resources_pop();

src/opengx.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,46 @@ void ogx_stencil_create(OgxStencilFlags flags);
105105

106106
/* Support for GLSL emulation */
107107

108+
typedef struct {
109+
/* *_first: number of the first available resource
110+
* *_end: number of the first *not* available resource
111+
*
112+
* The number of available resources is X_end - X_first. Each member
113+
* specifies the number starting from zero, so that in order to get the ID
114+
* of the desired resource, you need to add the base ID of the resource:
115+
* for example, to get the actual stage number, you'd have to do
116+
*
117+
* stage = number + GX_TEVSTAGE0
118+
*
119+
* and, for matrix types,
120+
*
121+
* texmtx = number * 3 + GX_TEXMTX0
122+
*
123+
* Fields are named according to libogc's constants, to minimize confusion.
124+
*/
125+
uint8_t tevstage_first;
126+
uint8_t tevstage_end;
127+
uint8_t kcolor_first;
128+
uint8_t kcolor_end;
129+
uint8_t tevreg_first;
130+
uint8_t tevreg_end;
131+
uint8_t texcoord_first;
132+
uint8_t texcoord_end;
133+
uint8_t pnmtx_first;
134+
uint8_t pnmtx_end;
135+
uint8_t dttmtx_first;
136+
uint8_t dttmtx_end;
137+
uint8_t texmtx_first;
138+
uint8_t texmtx_end;
139+
uint8_t texmap_first;
140+
uint8_t texmap_end;
141+
/* We could add the VTXFMT here too, if we decided to reserve them for
142+
* specific goals; for the time being, we only use GX_VTXFMT0 and set it up
143+
* from scratch every time. */
144+
} OgxGpuResources;
145+
146+
extern OgxGpuResources *ogx_gpu_resources;
147+
108148
typedef struct _OgxDrawMode {
109149
uint8_t mode;
110150
bool loop;

src/shader.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,14 @@ void glUseProgram(GLuint program)
893893
glparamstate.current_program = program;
894894
glparamstate.dirty.bits.dirty_attributes = 1;
895895

896+
if (program != 0) {
897+
glparamstate.mv_ptr = &_ogx_shader_state.mv_matrix;
898+
glparamstate.proj_ptr = &_ogx_shader_state.proj_matrix;
899+
} else {
900+
glparamstate.mv_ptr = &glparamstate.modelview_matrix;
901+
glparamstate.proj_ptr = &glparamstate.projection_matrix;
902+
}
903+
896904
if (old && old->deletion_requested) {
897905
glDeleteProgram(PROGRAM_TO_INT(old));
898906
}

src/shader.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ typedef struct {
149149
* separate array so that we can use consecutive elements as matrix
150150
* columns. */
151151
Vec4f vertex_attrib_data[MAX_VERTEX_ATTRIBS];
152+
153+
/* Transformation matrices */
154+
Mtx mv_matrix;
155+
Mtx44 proj_matrix;
152156
} OgxShaderState;
153157

154158
typedef struct _OgxVertexAttribState OgxVertexAttribState;

src/shader_api.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,16 @@ static void scale_matrix(const GLfloat *matrix, float divisor, float *out)
4747

4848
void ogx_shader_set_projection_gx(const Mtx44 matrix)
4949
{
50+
memcpy(_ogx_shader_state.proj_matrix, matrix,
51+
sizeof(_ogx_shader_state.proj_matrix));
5052
_ogx_set_projection(matrix);
5153
}
5254

5355
void ogx_shader_set_modelview_gx(const Mtx matrix)
5456
{
55-
GX_LoadPosMtxImm((void*)mv_matrix, GX_PNMTX0);
57+
memcpy(_ogx_shader_state.mv_matrix, matrix,
58+
sizeof(_ogx_shader_state.mv_matrix));
59+
GX_LoadPosMtxImm(_ogx_shader_state.mv_matrix, GX_PNMTX0);
5660
GX_SetCurrentMtx(GX_PNMTX0);
5761
}
5862

0 commit comments

Comments
 (0)