Skip to content

Commit

Permalink
all the rest
Browse files Browse the repository at this point in the history
  • Loading branch information
mardy committed Jan 7, 2025
1 parent 7f57d48 commit 65f191f
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 27 deletions.
3 changes: 1 addition & 2 deletions src/functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@ POSSIBILITY OF SUCH DAMAGE.

#define GL_GLEXT_PROTOTYPES 1
#include "opengx.h"
#include "shader.h"
#include "types.h"

#include <stdlib.h>
#include <string.h>

int _ogx_functions_c = 0; /* referenced by gc_gl.c, see the comment in there */

OgxFunctions _ogx_shader_functions __attribute__((weak)) = { 0, NULL };

#define PROC(name) { #name, name }
static const OgxProcMap s_proc_map[] = {
PROC(glAccum),
Expand Down
47 changes: 24 additions & 23 deletions src/gc_gl.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "gpu_resources.h"
#include "opengx.h"
#include "selection.h"
#include "shader.h"
#include "state.h"
#include "stencil.h"
#include "texture_unit.h"
Expand Down Expand Up @@ -91,18 +92,6 @@ void *_ogx_force_proctable = &_ogx_functions_c;

typedef void (*ProcessVertex)(int index);

/* This weak symbol should never be reached: the strong symbol is defined in
* shader.c, along with the other shader support functions. That code is also
* responsible of setting glparamstate.current_program to a nonzero value, and
* since this condition is the trigger to call _ogx_shader_setup_draw(), when
* the condition is satisfied we should also have the strong definition of this
* symbol.
*/
void __attribute__((weak)) _ogx_shader_setup_draw(const OgxDrawData *)
{
warning("Rendering via shaders is not enabled");
}

static inline void update_modelview_matrix()
{
GX_LoadPosMtxImm(glparamstate.modelview_matrix, GX_PNMTX0);
Expand Down Expand Up @@ -460,6 +449,8 @@ void ogx_initialize()

glparamstate.active_buffer = GL_BACK;

glparamstate.current_program = 0;

glparamstate.error = GL_NO_ERROR;
glparamstate.draw_count = 0;

Expand Down Expand Up @@ -2328,6 +2319,20 @@ bool _ogx_setup_render_stages()
return true;
}

static inline void apply_state_fixed_pipeline()
{
// Matrix stuff
if (glparamstate.dirty.bits.dirty_matrices) {
update_modelview_matrix();
update_projection_matrix();
}
if (glparamstate.dirty.bits.dirty_matrices | glparamstate.dirty.bits.dirty_tev) {
update_normal_matrix();
}

glparamstate.dirty.bits.dirty_matrices = 0;
}

void _ogx_apply_state()
{
// Set up the OGL state to GX state
Expand Down Expand Up @@ -2368,15 +2373,8 @@ void _ogx_apply_state()
setup_cull_mode();
}

// Matrix stuff
if (glparamstate.dirty.bits.dirty_matrices) {
update_modelview_matrix();
update_projection_matrix();
}
if (glparamstate.dirty.bits.dirty_matrices | glparamstate.dirty.bits.dirty_tev) {
update_normal_matrix();
}

/* Though glFog*() functions can be replaced by shaders, the OpenGL spec
* allows them to be used even when shaders are active. */
if (glparamstate.dirty.bits.dirty_fog) {
setup_fog();
glparamstate.dirty.bits.dirty_fog = 0;
Expand All @@ -2386,11 +2384,14 @@ void _ogx_apply_state()
update_scissor();
}

if (!glparamstate.current_program) {
apply_state_fixed_pipeline();
}

/* Reset the updated bits to 0. We don't unconditionally reset everything
* to 0 because some states might still be dirty: for example, the stencil
* checks alters the texture coordinate generation. */
glparamstate.dirty.bits.dirty_cull = 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;
Expand Down Expand Up @@ -2493,7 +2494,7 @@ static bool setup_draw(const OgxDrawData *draw_data)
{
_ogx_efb_set_content_type(OGX_EFB_SCENE);

if (glparamstate.current_program == 0) {
if (!glparamstate.current_program) {
uint8_t texen = glparamstate.texture_enabled;
uint8_t color_provide = 0;
if (glparamstate.cs.color_enabled &&
Expand Down
7 changes: 6 additions & 1 deletion src/opengx.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ typedef enum {

void ogx_stencil_create(OgxStencilFlags flags);

/* Support for GLSL emulation */

typedef struct _OgxDrawMode {
uint8_t mode;
bool loop;
Expand Down Expand Up @@ -147,7 +149,10 @@ typedef enum {
void ogx_shader_add_uniforms(GLuint shader, int count, ...);
void ogx_shader_add_attributes(GLuint shader, int count, ...);

void ogx_shader_set_data(GLuint shader, void *data); // TODO: set_execution_callback
typedef void (*OgxSetupDrawCb)(GLuint shader, const OgxDrawData *draw_data,
void *user_data);
void ogx_shader_set_setup_draw_cb(GLuint shader,
OgxSetupDrawCb setup_draw, void *data);
void *ogx_shader_get_data(GLuint shader);

#ifdef __cplusplus
Expand Down
6 changes: 5 additions & 1 deletion src/shader.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ POSSIBILITY OF SUCH DAMAGE.

#include "debug.h"
#include "opengx.h"
#define BUILDING_SHADER_CODE
#include "shader.h"
#include "state.h"
#include "utils.h"
Expand Down Expand Up @@ -436,6 +437,7 @@ void glUseProgram(GLuint program)
/* TODO: applications are free to modify the shaders after this call, and
* we should make it so that these changes do not affect the rendering,
* unless glLinkProgram() is called. */
fprintf(stderr, "activating program %x\n", program);
glparamstate.current_program = program;
}

Expand Down Expand Up @@ -566,9 +568,11 @@ void ogx_shader_add_attributes(GLuint shader, int count, ...)
va_end(args);
}

void ogx_shader_set_data(GLuint shader, void *data)
void ogx_shader_set_setup_draw_cb(GLuint shader, OgxSetupDrawCb setup_draw,
void *data)
{
OgxShader *s = SHADER_FROM_INT(shader);
s->setup_draw = setup_draw;
s->user_data = data;
}

Expand Down
28 changes: 28 additions & 0 deletions src/shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ POSSIBILITY OF SUCH DAMAGE.
#ifndef OPENGX_SHADER_H
#define OPENGX_SHADER_H

#include "debug.h"
#include "types.h"

#ifdef BUILDING_SHADER_CODE

typedef struct {
/* Use these to navigate shaders and programs as a list */
OgxShader *shaders;
Expand Down Expand Up @@ -65,4 +68,29 @@ extern OgxFunctions _ogx_shader_functions;

void _ogx_shader_setup_draw(const OgxDrawData *draw_data);

#else /* BUILDING_SHADER_CODE not defined */

/* Define all needed symbols as weak */

/* This weak symbol should never be reached: the strong symbol is defined in
* shader.c, along with the other shader support functions. That code is also
* responsible of setting glparamstate.current_program to a nonzero value, and
* since this condition is the trigger to call _ogx_shader_setup_draw(), when
* the condition is satisfied we should also have the strong definition of this
* symbol.
*/
void __attribute__((weak)) _ogx_shader_setup_draw(const OgxDrawData *)
{
warning("Rendering via shaders is not enabled");
}

OgxFunctions _ogx_shader_functions __attribute__((weak)) = { 0, NULL };

#endif /* BUILDING_SHADER_CODE */

static inline bool _ogx_has_shaders()
{
return _ogx_shader_functions.num_functions > 0;
}

#endif /* OPENGX_SHADER_H */
2 changes: 2 additions & 0 deletions src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ typedef union {

typedef struct _OgxProgram OgxProgram;
typedef struct _OgxShader OgxShader;
typedef struct _OgxDrawData OgxDrawData;
typedef struct _OgxDrawMode OgxDrawMode;

struct _OgxShader {
OgxShader *next;
Expand Down

0 comments on commit 65f191f

Please sign in to comment.