FBO Questions

Started by
9 comments, last by Hyunkel 9 years, 9 months ago

Hello,

I'm doing shadow mapping with exponential shadow maps (ESM) in OpenGL 4.3.

This requires blurring the shadow map, which I do with a normal 2-pass gaussian blur.

What I want to do is the following:

Shadow map -> (Vertical Blur Shader) -> Intermediate Texture -> (Horizontal Blur Shader) -> Shadow Map

The shadow map is a DepthComponent32f texture and the intermediate texture uses R32f.

The first pass works fine, but for the second pass, where I want to write back to the shadow map, I can't seem to use the shadow map as a FBO color attachment, so I'm unable to write back to it.

I've also noticed, completely by accident, that I can sample from a texture that I am currently writing to, without any ill results.
For example I can do:
Texture -> (Vertical Blur Shader) -> Texture
To recap:
  1. Is there a way to use a DepthComponent texture as a color attachment in a FBO?
  2. Why can I sample a texture that I'm currently writing to? Is this legal in OpenGL 4.3, or is the behavior undefined? What happens behind the scenes? Does it internally create a new texture to write to, and then discard the old one when the draw call finishes?

Cheers,
Hyu

Advertisement
@Question 1: I presume you want to reuse the memory of the shadow map? Because otherwise there is no reason to to use a DepthComponent texture as the final target.

@Question 2: The results of doing that are usually "undefined" which means anything can happen (including the intended) but the behavior can be different for different vendors, driver versions, GPU-loads, ... In short: don't do it.
The texture cache, through which you read, is not kept coherent with the video memory so writing a pixel does not effect the copy of that pixel in the texture cache, which is probably what you are seeing here. In most cases, you can read and write to the same texture, if you only read one pixel per thread and it's the very pixel you write, but in your case you are reading more than one pixel.

@Question 1: I presume you want to reuse the memory of the shadow map? Because otherwise there is no reason to to use a DepthComponent texture as the final target.

Yes, that is correct. I would like to avoid allocating an extra texture for each shadow map if possible.

@Question 2: The results of doing that are usually "undefined" which means anything can happen (including the intended) but the behavior can be different for different vendors, driver versions, GPU-loads, ... In short: don't do it.
The texture cache, through which you read, is not kept coherent with the video memory so writing a pixel does not effect the copy of that pixel in the texture cache, which is probably what you are seeing here. In most cases, you can read and write to the same texture, if you only read one pixel per thread and it's the very pixel you write, but in your case you are reading more than one pixel.

Makes perfect sense, thanks!


Why can I sample a texture that I'm currently writing to? Is this legal in OpenGL 4.3, or is the behavior undefined?

It is explicitly not disallowed, because there are useful operations in this area (i.e. updating a particle system stored in a texture).

As Ohforf says, reading different pixels than you are writing is asking for trouble.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]


Is there a way to use a DepthComponent texture as a color attachment in a FBO?
Nope. Just use GL_RED, GL_R32F with GL_FLOAT as type. Depth is just a value, write/read it as it were like any other texture (it is).

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

It is explicitly not disallowed, because there are useful operations in this area (i.e. updating a particle system stored in a texture).

As Ohforf says, reading different pixels than you are writing is asking for trouble.

Good to know, thanks!

Nope. Just use GL_RED, GL_R32F with GL_FLOAT as type. Depth is just a value, write/read it as it were like any other texture (it is).

But I can't use GL_32F as a depth attachment when generating the shadow map, can I?


But I can't use GL_32F as a depth attachment when generating the shadow map, can I?
You could write the depth values yourself onto that 32 bit float texture as a regular color attachment, then sample/modify them as you please, and just attaching a regular RBO to the depth attachment.

Now, I'm not sure if you could actually create a depth texture, attach it to the FBO as depth attachment, and also bind it to a texture image unit and sample from it.

Then again, if you're doing the writing in a final pass and you don't need to simultaneously sample from the depth buffer, you could always write what you need to the depth attachment directly in the fragment shader.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

You could write the depth values yourself onto that 32 bit float texture as a regular color attachment, then sample/modify them as you please, and just attaching a regular RBO to the depth attachment.

Now, I'm not sure if you could actually create a depth texture, attach it to the FBO as depth attachment, and also bind it to a texture image unit and sample from it.

Then again, if you're doing the writing in a final pass and you don't need to simultaneously sample from the depth buffer, you could always write what you need to the depth attachment directly in the fragment shader.

I've tried a few more different combinations, and it seems like it is not possible to create a texture that can be used both as a depth attachment and as a color attachment.

But as you've mentioned, I can either simply output to gl_FragDepth for the final pass, or for the shadow map generation, output the depth to a color attachment, using a shared RBO across all shadow maps.

Thank you for all the helpful comments!

Cheers,

Hyu

The hardware depth test has certain optimizations in place which can significantly speed up the rendering of occluded fragments. However, those optimizations require additional memory which is why depth attachments are more then a simple texture. You can't just use them as a regular render target, write to them, and still expect that additional memory for the optimizations to be consistent.
OpenGL actually has a method for aliasing the format of textures, called TextureView, and as far as I'm aware, even TextureViews can't change a depth texture into a regular one.

I think the best option is to allocate 3 buffers: The depth attachment, into which you render, the intermediate texture and the final texture. Note that the first 2 can be reused by other lights after the filtering, as long as the other shadowmaps have the same or a smaller size.


The hardware depth test has certain optimizations in place which can significantly speed up the rendering of occluded fragments.
This. Have in mind that writing directly to the depth buffer means no early Z rejection. The GPU can "preemptively" reject fragments before executing the fragment shader by just testing the resulting depth value from the vertex shader outputs.

If you write to the depth buffer directly, the GPU has to execute the fragment shader to know the actual depth value, thus no early Z rejection is possible.

If that worries you, you can always measure the differences between the possible methods.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

This topic is closed to new replies.

Advertisement