Question

As far as I understand Pixel Shader operates on per-pixel basis. But there are functions like ddx and ddy that calculates derivates. But how can one calculate derivatives from just one pixel coordinates?? Can someone help me on these? These also raises questions as in

tex.Sample(s0, t0);

Does it mean Sample function is calculated per pixel basis?? I thought Sampler Instruction operates on per subspan basis.

Example:

If I have the following 16 pixels:

* * * *
* * * *   
* * * *
* * * *

And my pixel shader looks like this:

float4 PS(PS_INPUT input) : SV_Target{
    float2 derivX = ddx_fine(input.tex);
    float2 derivY = ddx_fine(input.tex);

    return tex.SampleGrad(s0, t0, derivX, derivY);
}

How many times the above code will be called in a 4 x 4 grid of pixel coordinates? Thanks.

Was it helpful?

Solution

A pixelshader is the program for exactly one fragment. But, all pixels are shaded within a 2x2 block. The pixelshaders are running simultaneously for this 4 pixels, so there can be computed dervatives for mipmapping etc. If you're calling tex.Sample it's called for all pixels in the respective block, so the gradient can be determined. This is the reason why gradient functions cannot be used in branching ifs or loops, because the program would differ within the fragment-quad.

There is a good description how mipmapping for example works: http://www.arcsynthesis.org/gltut/Texturing/Tut15%20How%20Mipmapping%20Works.html

@ Your edit, which should be answered by my link, but anyway: I'm not sure how it's implemented, but in the best case your shader would be runned 16 times. The pixels are grouped to 2x2 quads. There are cases at edges where pixels will be computed and discarded, but maybe only with skewed edges.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top