Skip to content

Shader resources #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Mar 24, 2025
Merged

Shader resources #101

merged 20 commits into from
Mar 24, 2025

Conversation

mardy
Copy link
Collaborator

@mardy mardy commented Feb 3, 2025

To be merged after #100

Enable operation of clipping and stencil tests when using shaders, too. In order to do this, expose the OgxGpuResources structure to the client, to let them allocate stages dynamically. The clip and stencil stages need to be run after the other stages, and for this reason it's important for opengx to know how many stages are being used by the shader program.

The last commit fixes an unrelated old regression, discovered when testing these changes.

mardy added 20 commits March 15, 2025 13:39
The association of a VBO to an array is to be saved in the client state:
we should look up the VBO only when we draw some geometry. The error was
discovered running the cube_tex example (it will be added to the repo in
a later commit), which uses two different VBOs for the position and
texture coordinate attributes.
This example uses SDL2 and glm. At the moment this example does not work
on the Wii/GameCube, as the textured rendering using OpenGL 2.0 will
come in a later commit.

The "common" directory contains textures that get embedded into the
executable, so that examples can be written without access to external
files (which makes testing on the console -- or on the emulator --
very straightforward).
Colors are often specifies as a 3 or 4 long vector of floats, but GX
does not support specifying colors using floating point components.
Therefore, convert such values to GXColor.
The data here is just an integer number representing the texture unit
bound to the uniform.
This is all what is needed to let the client developer write a GX shader
capable of supporting textured geometries.
This is a bit primitive, because we are hardcoding the texture stage and
other TEV variables (we'll improve this later), but it does the job.
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.
Shader programs will be able to know which resources are available and
will not conflict with other core opengx functionality (like clipping
and stencil).
These enums are supported both by glGetBoolean() and glIsEnabled().
It is not very clear how this should work, but we replicate the
behaviour of desktop OpenGL.
Clipping and stenciling is not working properly currently (due to the
model-view matrix not being up-to-date when drawing with shaders), but
the stages do get applied.
Add a couple of pointers in the opengx state, to the currently active
model-view and projection matrices: these are not always those set via
the immediate-mode API, because with OpenGL 2.0+ these matrices can be
implicitly set by the shader programs.
This now works when using shaders, too.
The client can update the matrices in the setup_draw() callback as well,
but having a specialized callback makes it possible for opengx to
request the client to update the matrices without setting up the whole
TEV state.
This is required for supporting stencil drawing when using shaders:
since the drawing happens in two stages (first we draw to the stencil
buffer, and then to the scene) and only the latter stage uses the TEV as
setup by the client, it would be a waste to call setup_draw() twice, if
all what we need from the first invocation is just the updated
transformation matrices.
This is as simple as ensuring that the matrices being considered for the
stencil texture projection are those used by the shader pipeline
(glUseProgram() takes care of updating the matrix pointers).
This allows to reduce branching, since the function pointer only changes
when glUseProgram() is called. We should consider using the same
mechanism for the setup_draw() function.

We also clean up the dirty bit usage for the normal matrix, partially
reverting commit 836fd7e, because the
current condition for rebuilding the normal matrix is anyway satisfied
when the dirty_matrices flag is set, so we can just set it anytime a
matrix is changed. We still keep setting the dirty_tev flag to true when
the normal matrix gets changed, in order not to break the idea behind
commit 836fd7e.
Fixes a regression introduced by commit 836fd7e, because indeed
the _ogx_stencil_draw() function (and draw_op() in particular) alters
the TEV setup, which then needs to be restored when drawing to the
scene.

This fixes an issue in the dinoshade program, where some parts of the
reflected dinosaur could be seen outside of the floor.
@mardy mardy force-pushed the shader-resources branch from c53b9ca to 92b2d6b Compare March 15, 2025 10:41
@WinterMute WinterMute merged commit 92b2d6b into devkitPro:master Mar 24, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants