How to pass scene depth buffer to fragment shader as a texture? (DEF-2699)


I want to use the depth buffer values of a scene for post processing effects, such as doing SSAO or scene fog.

Like this kind of thing!Advanced-Lighting/SSAO


1 Like


I should be able to do this?

render.enable_texture(0, self.effect_render_target, render.BUFFER_COLOR_BIT)
render.enable_texture(1, self.effect_render_target, render.BUFFER_DEPTH_BIT)

And then this should work?

uniform lowp sampler2D DIFFUSE_TEXTURE;
uniform lowp sampler2D DEPTH_BUFFER;

Right now I’m getting

Render target does not have a texture for the specified buffer type.
stack traceback:
	[C]: in function 'enable_texture'

When trying

render.enable_texture(1, self.effect_render_target, render.BUFFER_DEPTH_BIT)

I do have a texture setup as a sampler in the material the render target is using.

Is there a sample render script I could look at to see how it’s done correctly?

Here is render target being setup. What’s missing?

	local color_params = { format = render.FORMAT_RGBA,
	                       width = render.get_window_width(),
	                       height = render.get_window_height(),
	                       min_filter = render.FILTER_LINEAR,
	                       mag_filter = render.FILTER_LINEAR,
	                       u_wrap = render.WRAP_CLAMP_TO_EDGE,
	                       v_wrap = render.WRAP_CLAMP_TO_EDGE }
   	local depth_params = { format = render.FORMAT_DEPTH,
	                       width = render.get_window_width(),
	                       height = render.get_window_height(),
	                       u_wrap = render.WRAP_CLAMP_TO_EDGE,
	                       v_wrap = render.WRAP_CLAMP_TO_EDGE }    	
    self.effect_render_target = render.render_target("effect_render_target", {[render.BUFFER_COLOR_BIT] = color_params, [render.BUFFER_DEPTH_BIT] = depth_params})


Sorry, you’re right, we only support color buffers at the moment. I filed an issue for this, DEF-2699

1 Like


Is that implemented yet? Can we access depth buffer as a texture now?



No, sorry, not many are asking for it so other things keep taking precedence.



People just don’t know what possibilities it opens up, probably.
I want to make Depth of Field effect with a simple blur instead of the bokeh for starters.
Without that feature I can still render to another render target to imitate the depth buffer, right?

1 Like


Yes, of course, you can use render.enable_material to switch to a shader which draws the depth as a gray-scale or similar.



Looks like only two textures are allowed per material? I tried passing original texture, depth texture and lut texture to my postprocessing shader and color grading no longer works…
Is it hard to support more than two textures per material?



That should not be the case. Could you try something safer/simpler to verify that the third texture is actually ignored? The only limiting factor should be the number of texturing units.

1 Like


Can current emphasis on 3D features get this issue solved too? It’s needed for a variety of shader effects.



What you need to do is to bind a texture to the depth buffer. This is not supported by the core GLES2.0 functionality, which we guarantee to support. You can however achieve this using the OES_depth_texture extension.
We are currently focusing heavily on both 3D-rendering functionality and performance and part of that is to be able to use OpenGL extensions and functionality to check support etc.

It will be worked on over the next few sprints, I can’t however give you a more precis estimate at the very moment, more than to say it does have very high priority given how much it will bring in to the 3D-support of Defold.
But, one will need other functionality in place for this to be useful, something that is taken into account and worked on simultaneously.




Looks like this is still an issue?



Yeah unfortunately there’s no extension support available in the render scripts that could enable this functionality. I know it has been up for discussion at some point, but I don’t know if was ever a design drafted for it. Perhaps @Mathias_Westerdahl or @sven has some input to it? I don’t think we can stick to the ES2.0 vanilla spec forever if we want to move forward with better 3D support, but it’s not up to me to decide :slight_smile: