View Single Post
Old 20th February 2018, 17:04   #11
Nitorami
Major Dude
 
Join Date: Mar 2008
Location: Erlangen
Posts: 856
To a lot of folks, shaders seem to be a mystery. And indeed shader programming is different to what one would naively expect when doing a image processing routine in a language like C or any other. We would start with loops
code:

for x:= 0 to 1279 do
for y := 0 to 1023 do
ret := Some_Colour_as_function_of (x,y)
end;
end;



In the shaders, the loop is implicit. You do not need to define it manually. It took me quite a long while to understand this principle:

The video card runs the exact same shader code for each pixel on the screen.
Then why isn't each pixel the same and we only see a uniform screen ?
Because the code uses variable uv, which is the screen position for each pixel.

The entire shader code is one big formula which does the same for each pixel, only depending on the screen position uv. If your code does not use uv, all pixels will inevitably be the same. Using uv is the ONLY way to produce some sort of picture.

For instance,

ret = tex2D(sampler_dancer, uv); - samples the content of texture sampler_dancer at each pixel.

ret = uv.y; - produces a vertical grayscale.

ret.g = sin (50*uv.x); - produces vertical green stripes.

ret = float3 (0.1, 0.5, 0.9); - produces a uniform blueish screen. As we did not use uv here, the screen can only have uniform colour.

Remember this. It helps.
Nitorami is offline   Reply With Quote