Jump to content
  • Advertisement
Sign in to follow this  

Load vs SampleGrad

This topic is 2119 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can someone explain to me the difference between Load and SampleGrad, and when each should be used.  I have used Load when dealing with the depth buffer before, and now i am looking at a sample app that uses SampleGrad also when dealing with a shadow map. 


I am not sure which or when to use either.


Thanks in advance

Share this post

Link to post
Share on other sites

Load does not perform sampling, which means it doesn't do any address conversions or texture filtering. You simply say "give me the texel value at position (X, Y)", and you get the value of that single texel. It's often used in full-screen shaders where each output pixel directly corresponds to a single input pixel. For instance if you were writing a tone-mapping shader, in the pixel shader you might say "I'm writing out to pixel (345, 270) so give me the texel at (245, 270) from my input texture so that I can apply tone-mapping".

SampleGrad, like all of the other "Sample*" functions on the Texture object, will perform sampling. This means that...

  • You provide coordinates with [0, 1] floating-point UV coordinates as opposed to the integer used for Load
  • The UV coordinates will be wrapped, mirrored, or clamped based on the address mode specified for the SamplerState that you pass in
  • Texture filtering (including mipmap sampling and interpolation) will be applied according to the filtering mode specified for the SamplerState that you pass in

Now SampleGrad in particular is a variant of Sample that lets you pass in the gradients (partial derivatives of the U and V coordinates with respect to screen space X and Y). The normal Sample function will automatically calculate the gradients for you, which is done using the ddx/ddy partial derivative instructions that are available to pixel shaders. These gradients affect which mipmap(s) get used for sampling, as well as how many samples are used for anisotropic filtering. There are 3 reasons why you might not want to use the regular Sample function:

  1. You're not working in a pixel shader, so you can't use Sample
  2. You're trying to sample inside of a dynamic branch or dynamic loop construct, in which case gradients aren't available
  3. You're doing something special and you don't want to use the auto-calculated gradients

#2 is pretty common, so you might see code that calculates the gradients outside of a branch using ddx and ddy, and then passing them into SampleGrad. The code you saw was probably for some variant of Variance Shadow Mapping, since normal shadow maps are not filterable thus you wouldn't care about the gradients (if you don't have mipmaps or don't care about them, it's common to use SampleLevel and pass 0 as the mip level). I would guess that the code you saw was either doing it in a deferred rendering scenario (where you can't calculate the gradients during the lighting pass), or it was doing it inside of a dynamic branch.

Edited by MJP

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!