Graphics Reference
In-Depth Information
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
vec3 shade( const in vec3 P, const in vec3 n) {
vec3 radiance
= vec3(0.0);
// Assume only one light
vec3 offset = lightPosition - P;
float distanceToLight = length(offset);
vec3 w_i
= offset / distanceToLight;
vec3 w_o
= -normalize(P);
if (visible(P, w_i, distanceToLight, shadowMap)) {
vec3 L_i = lightPower / (4 * PI * distanceToLight * distanceToLight);
// Scatter the light.
radiance +=
L_i *
evaluateFiniteScatteringDensity(w_i, w_o) *
max(0.0, dot(w_i, n));
}
return radiance;
}
void main() {
vec3 P = Pinterp;
radiance = shade(P, n);
}
Listing 15.34: Helper functions for the pixel shader.
1
2
3
# define PI 3.1415927
bool visible( const in vec3 P, const in vec3 w_i, const in float distanceToLight,
sampler2DShadow shadowMap) {
return true ;
4
5
6
7
8
9
10
11
12
13
14
}
/ ** Returns f(wi, wo). Same as BSDF::evaluateFiniteScatteringDensity
from the ray tracer. * /
vec3 evaluateFiniteScatteringDensity( const in vec3 w_i, const in vec3 w_o) {
vec3 w_h = normalize(w_i + w_o);
return (k_L +
k_G * ((s + 8.0) * pow(max(0.0, dot(w_h, n)), s) / 8.0)) / PI;
}
However, there is one exception. The software renderers iterated over all the
lights in the scene for each point to be shaded. The pixel shader is hardcoded
to accept a single light source. That is because processing a variable number
of arguments is challenging at the hardware level. For performance, the inputs
to shaders are typically passed through registers, not heap memory. Register
allocation is generally a major factor in optimization. Therefore, most shading
compilers require the number of registers consumed to be known at compile
time, which precludes passing variable length arrays. Programmers have devel-
oped three forward-rendering design patterns for working within this limitation.
These use a single framebuffer and thus limit the total space required by the
algorithm. A fourth and currently popular deferred-rendering method requires
additional space.
 
 
Search WWH ::




Custom Search