Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


FBO Questions


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 Hyunkel   Members   -  Reputation: 383

Like
0Likes
Like

Posted 04 July 2014 - 07:09 AM

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



Sponsor:

#2 Ohforf sake   Members   -  Reputation: 1832

Like
3Likes
Like

Posted 04 July 2014 - 07:58 AM

@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.

#3 Hyunkel   Members   -  Reputation: 383

Like
0Likes
Like

Posted 04 July 2014 - 08:11 AM

 

 

@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!



#4 swiftcoder   Senior Moderators   -  Reputation: 10426

Like
1Likes
Like

Posted 04 July 2014 - 08:21 AM


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 - Software Engineer @Amazon - [swiftcoding]


#5 TheChubu   Crossbones+   -  Reputation: 4794

Like
1Likes
Like

Posted 04 July 2014 - 09:51 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


#6 Hyunkel   Members   -  Reputation: 383

Like
0Likes
Like

Posted 04 July 2014 - 10:31 AM

 

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?



#7 TheChubu   Crossbones+   -  Reputation: 4794

Like
1Likes
Like

Posted 04 July 2014 - 01:46 PM


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


#8 Hyunkel   Members   -  Reputation: 383

Like
0Likes
Like

Posted 04 July 2014 - 03:22 PM

 

 

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



#9 Ohforf sake   Members   -  Reputation: 1832

Like
2Likes
Like

Posted 05 July 2014 - 02:30 AM

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.

#10 TheChubu   Crossbones+   -  Reputation: 4794

Like
1Likes
Like

Posted 05 July 2014 - 05:08 AM


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


#11 Hyunkel   Members   -  Reputation: 383

Like
0Likes
Like

Posted 05 July 2014 - 05:34 AM

 

 

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.

 

 

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.

 

The reason I initially wanted to do this is because my shadow maps for directional lights have 4 cascades, and I tried generating them simultaneously by quadrupling the geometry using  the geometry buffer. If I rendered only to the depth buffer I was able to avoid allocating an additional 4 R32f 2048x2048 color attachment slices.

After some profiling I noticed that this isn't really any faster than generating the cascades in individual passes though, provided that I use decent frustum culling for the individual cascades.

 

 

 

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. 

This is precisely what I am doing now. It is also much easier to have either a linear or exponential shadow map this way, which is needed for ESM.

 

Thanks again!

Hyu






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS