Skip to content
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

Multiple outputs for fragment shader doesn't work #246

Open
MonsieurBleu opened this issue Jan 25, 2023 · 0 comments
Open

Multiple outputs for fragment shader doesn't work #246

MonsieurBleu opened this issue Jan 25, 2023 · 0 comments

Comments

@MonsieurBleu
Copy link

MonsieurBleu commented Jan 25, 2023

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 :

  1. I clear every FBO and the screen.
  2. I draw the opaque objects of my world in an image called opaque_world_render, using depth testing in the process.
  3. Then I copy opaque_world_render in an FBO and drawn on it all the transparent object of the scene, also using depth testing.
  4. 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.

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

No branches or pull requests

1 participant