Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 04 May 2006
Offline Last Active Yesterday, 04:40 PM

Posts I've Made

In Topic: Sampling the depth buffer in a shader in DX11

17 November 2014 - 01:30 AM

Thanks guys, this will do for now.


By the way: What you're doing with that clip() should be possible with the depth buffer directly, no need to bind it as SRV. Draw your full screen quad with a fixed z at that threshold value and use a depth comparison of greater or greater-equal. Or set z to 1 and the comparison to equal (if your depth was cleared to 1 initially).


True, but when I started I was not sure if I wanted to mask pixels out or only include them with a smaller alpha, so I thought this would be more flexible, though, perhaps not as efficient as your approach.

In Topic: Sampling the depth buffer in a shader in DX11

16 November 2014 - 04:03 PM

Nope. Are you calculating the tex coords for the sample correctly? I.e., converting the screen position (range -1 to 1) to tex coords in the range 0 to 1?


I am not doing that conversion, but do I really need to? The output does look like it should (apart from what I expected the depth value to be, but that is probably because of the non-linearity like Adam_42 said).


I am rendering the output to a texture by drawing a fullscreen quad and mapping the original back buffer and depth stencil shader resource view as shader input textures. Then I sample both the textures using the "normal" 0 to 1 UV range and mask off certain parts of the back buffer according to the depth stored in the depth stencil view. like this:

float4 BackgroundPS(BackgroundVSOutput input) : SV_TARGET
	float4 original = backBuffer.Sample(bilinearSampler, input.Tex).rgba;
	float depth = depthBuffer.Sample(bilinearSampler, input.Tex).r;

	clip(depth < 0.9999f ? -1 : 1);

	return original;

This gives me a texture where only far away objects (basically skyboxes and impostors, ie. things drawn without depth) are rendered. I should not need to do any UV conversions for this, right? I am not sure where the -1 to 1 range comes into play...

In Topic: Sampling the depth buffer in a shader in DX11

16 November 2014 - 01:36 PM

Thank you guys, especially TiagoCosta for your excellent explanation. It works great, but I seem to get something other than the normalized depth range out of the R component when sampling it in my shader. When drawing the resulting texture to screen I get white almost everywhere except when I move really close to an object, which suggests that the value in the component is actually a lot larger than one and only gets into the normalized range when moving really close to an object.


You said:


when you sample the Texture2D in the pixel shader the RED channel will contain "the floating point depth value between 0 and 1".


...but is it possible that I still have to scale the value into the normalized range myself?

In Topic: Best Way to Learn 3D Computer Graphics? Help!

16 November 2014 - 04:03 AM


this book: http://d3dcoder.net/d3d11.htm and working your way through the examples


Luna's book is an excellent resource. Note, however, that his projects are based on the (deprecated) DX SDK (June 2010). It's certainly a private choice, but, IMHO, a D3D11 beginner may as well just use the Windows SDK, perhaps using the book as a reference. Luna's projects were created with VS2010, and they don't necessary upgrade well to later versions of VS.



Using the Effect Framework: http://blogs.msdn.com/b/chuckw/archive/2012/10/24/effects-for-direct3d-11-update.aspx, almost all chapters in the book can be compiled and run on the newest SDKs. Luna also has samples for creating windows store apps and using shaders without the effect framework on his web site, here: http://www.d3dcoder.net/resources.htm


I suggested the book because it covers a lot of the essential math topics needed for graphics programming, regardless of APIs and SDK versions.

In Topic: Best Way to Learn 3D Computer Graphics? Help!

15 November 2014 - 03:30 AM

May I suggest getting this book: http://d3dcoder.net/d3d11.htm and working your way through the examples as you read it? Be sure to understand everything before moving to the next chapter. It will teach you the necessary math while focusing on the "fun stuff" ie. programming.