Description
Hello, I'm trying to build a 2d game with this library and I have to use an FBO to stock the depth value of each pixel.
Here is my process :
first, I create a renderer, and several images that can be drawn to using GPU_LoadTarget(), then each frame :
- I clear every FBO and the screen.
- I draw the opaque objects of my world in an image called opaque_world_render, using depth testing in the process.
- Then I copy opaque_world_render in an FBO and drawn on it all the transparent object of the scene, also using depth testing.
- I draw the final result in the screen using post-processing shader to add some bloom and blur.
This process works fine to combine depth testing and transparency. What I want to add is the possibility for the shader that draws a transparent object to access the depth-value for each pixel. This is to add a notion of depth to water, for exemple. I've read that it is impossible to access the depth buffer in a fragment shader. I've also tried to store this in the value of every pixel, but the only value left is the alpha value and I need more precision than a single byte (my depth values range from 0 to about 8000). And I read that SDL_GPU isn't made to change how this works.
So my last resort is to use an FBO to store the depth value of each pixel in the opaque_world_renderer. But the issue with this approach is that my game is made for rendering a very high amount of sprites (500k+) and making a second draw call for each sprite is going to destroy the perfomances. What I want to do is to have 2 output in my fragment shader :
layout (location = 0) out vec4 fragColor;
layout (location = 1) out vec4 depthColor;
But the frag shader doesn't draw in the FBO, here is the code I used to pass it to the shader :
// creating the fbo
RE.depth_fbo_image = GPU_CreateImage(RE.screen->w, RE.screen->h, GPU_FORMAT_RGBA);
GPU_SetAnchor(RE.depth_fbo_image, 0, 0);
RE.depth_fbo = GPU_LoadTarget(RE.depth_fbo_image);
// giving the fbo to the shader
GPU_SetShaderImage(depth_fbo_image, shader.get_location("depth_fbo"), 1);
If you know how to do this with SDL_GPU I would like to know.