higer order filters as b-splines - stanford...
TRANSCRIPT
![Page 1: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/1.jpg)
Tim Foley
Shading Languages
![Page 2: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/2.jpg)
2
Why Shading Languages? DSLs?
ProductivityPerformance
![Page 3: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/3.jpg)
3
Why Shading Languages? DSLs?
ProductivityPerformance
![Page 4: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/4.jpg)
4
► Productivity
► Build shaders from re-usable components
► Performance
► Specialize code to data
► Exploit specialized hardware
![Page 5: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/5.jpg)
5
► Productivity
► Build shaders from re-usable components
► Based on model of problem domain
► Performance
► Specialize code to data
► Exploit specialized hardware
► Based on model of solution domain
![Page 6: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/6.jpg)
6
► Productivity
► Build shaders from re-usable components
► Based on model of problem domain
► Performance
► Specialize code to data
► Exploit specialized hardware
► Based on model of solution domain
Shader Graphs
Rates of Computation
![Page 7: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/7.jpg)
7
Building Shaders from Components
![Page 8: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/8.jpg)
8
► What kinds of components are needed?
► What form do components take?
► How do we combine components?
![Page 9: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/9.jpg)
9
RenderMan Shader Types
![Page 10: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/10.jpg)
10
Shader Components in a Modern Game
► Materials (pattern generation / BSDFs)
► Lights / Shadows
► Volumes (e.g., fog)
► Animation
► Geometry (e.g, tessellation, displacement)
► “Camera” (rendering mode)
► 2D/cubemap/stereo, color/depth output
![Page 11: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/11.jpg)
11
► What kinds of components are needed?
► What form do components take?
► How do we combine components?
![Page 12: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/12.jpg)
12
What form do shader components take?
► Function/procedure?
► Dataflow graph?
► Class?
![Page 13: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/13.jpg)
13
► Make a shader look like a procedure
► Represent with a dataflow graph IR (shader graph)
► Compose and specialize using class-like concepts
![Page 14: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/14.jpg)
14
Shade Trees[Cook 1984]
![Page 15: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/15.jpg)
15
![Page 16: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/16.jpg)
16
Shade Trees
float a = 0.5, s = 0.5;float roughness = 0.1;float intensity;color metal_color = (1,1,1);intensity = a*ambient() +
s*specular(normal,viewer,roughness);final_color = intensity * metal_color;
Key:
typeconstant
![Page 17: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/17.jpg)
17
Shade Trees
float a = 0.5, s = 0.5;float roughness = 0.1;float intensity;color metal_color = (1,1,1);intensity = a*ambient() +
s*specular(normal,viewer,roughness);final_color = intensity * metal_color;
Key:
typeconstant
final_color
a
smetal_color
ambient()specular()
viewer
roughness
normal
intensity
![Page 18: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/18.jpg)
18
Shade Trees
float a = 0.5, s = 0.5;float roughness = 0.1;float intensity;color metal_color = (1,1,1);intensity = a*ambient() +
s*specular(normal,viewer,roughness);final_color = intensity * metal_color;
Key:
typeconstant
final_color
a
smetal_color
ambient()specular()
viewer
roughness
normal
+intensity
*
*
![Page 19: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/19.jpg)
19
Point Light
Shader Graphs are Composable
DisplacementDiffuse Material
...
Li
N
L
P
P
NdotLNfinal_color
Lidelta
L
lightPos
intensity
![Page 20: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/20.jpg)
20
Point Light
Shader Graphs are Composable
DisplacementDiffuse Material
...
Li
N
L
P
P
NdotLNfinal_color
Lidelta
L
lightPos
intensity
![Page 21: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/21.jpg)
21
Shader Graphs are Composable
DisplacementDiffuse Material
Point Light
P
NdotLNfinal_color
...
Lidelta
L
lightPos
intensity
![Page 22: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/22.jpg)
22
Exploiting Specialized Hardware
Specializing Code to Data
![Page 23: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/23.jpg)
23
RenderMan Shading Language[Hanrahan and Lawson 1990]
![Page 24: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/24.jpg)
24
uniform vector L;varying vector N;
...
L = normalize(L);
...
N = normalize(N);varying float NdotL = N . L;
Key:
typerate
RenderMan Shading Language
![Page 25: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/25.jpg)
25
uniform vector L;varying vector N;
...
L = normalize(L);
...
N = normalize(N);varying float NdotL = N . L;
computed per-batch
computed per-sample
Key:
typerate
RenderMan Shading Language
![Page 26: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/26.jpg)
26
Split shader into uniform and varying parts
uniformcode
varyingcode
shader
![Page 27: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/27.jpg)
27
Create an instance of the shader class
shader
uniform
args
instance
uniformcode
varyingcode
![Page 28: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/28.jpg)
28
Create an instance of the shader class
shader
uniform
args
instance
uniformcode
varyingcode
shader parameters are instance variables
shader class has multiple methods
![Page 29: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/29.jpg)
29
Specialize as more information becomes known
shader
uniform
args
instance
geometry
boundinstance
vertex
args
elaboratedinstance
uniformcode
varyingcode
![Page 30: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/30.jpg)
30
Specialize as more information becomes known
shader
uniform
args
instance
geometry
boundinstance
vertex
args
elaboratedinstance
uniformcode
varyingcode
find parameter values using prototype chain
![Page 31: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/31.jpg)
31
Intermission:
Let’s Talk About Staging
![Page 32: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/32.jpg)
32
Staging Transformations
► Given a function of two parameters f(x,y)
► Where x might represent information known “before” y
► Compute functions f1(x) and f2(t,y)
► Such that f2(f1(x),y) = f(x,y)
► Can generalize to N stages
[Jørring and Scherlis 1986]
![Page 33: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/33.jpg)
33
Examples of f(x,y)
► Regular expression matching
► x is regular expression, y is string to match against
► RenderMan Shading Language
► x is uniform parameters, y is varying parameters
![Page 34: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/34.jpg)
34
Examples of f(x,y)
► Regular expression matching
► x is regular expression, y is string to match against
► RenderMan Shading Language
► x is uniform parameters, y is varying parameters
want to
compute
f(x, a)
f(x, b)
f(x, c)
…
![Page 35: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/35.jpg)
35
Goal
► Try to do “as much as possible” in f1
![Page 36: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/36.jpg)
36
A Trivial Solution
function f(x,y)...
end
function f1(x)return x
end
function f2(t, y)return f(t, y);
end
![Page 37: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/37.jpg)
37
A Trivial Solution
function f(x,y)...
end
function f1(x)return x
end
function f2(t, y)return f(t, y);
end
this isn’t “as much as possible”
![Page 38: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/38.jpg)
38
Another Trivial Solution
function f(x,y)...
end
function f1(x)return function(y)
return f(x,y)end
end
function f2(t, y)return t(y);
end
![Page 39: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/39.jpg)
39
Another Trivial Solution
function f(x,y)...
end
function f1(x)return function(y)
return f(x,y)end
end
function f2(t, y)return t(y);
end
first step returns a closure
second step applies it
![Page 40: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/40.jpg)
40
A Terra Solution
terra f(x,y)...
end
function f1(x)return terra(y)
return f(x,y)end
end
function f2(t, y)return t(y);
end
![Page 41: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/41.jpg)
41
A Terra Solution
terra f(x,y)...
end
function f1(x)return terra(y)
return f(x,y)end
end
function f2(t, y)return t(y);
end
compiler might do inlining,
constant folding, etc.
![Page 42: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/42.jpg)
42
More Idiomatic Terra
function f_staged(x,y)...
end
function f1(x)return terra(y)
return [f_staged(x,y)]end
end
function f2(t, y)return t(y);
end
![Page 43: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/43.jpg)
43
More Idiomatic Terra
function pow_staged(n,y)if n == 0 then return `1.0else return `(y * [pow_staged(n-1,y)]) end
end
function make_pow(n)return terra(y)
return [pow_staged(n,y)]end
end
function f2(t, y)return t(y);
end
![Page 44: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/44.jpg)
44
Explicit Staging Annotations
function pow_staged(n,y)if n == 0 then return `1.0else return `(y * [pow_staged(n-1,y)]) end
end
![Page 45: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/45.jpg)
45
Staged vs. Unstaged
function pow (n,y)if n == 0 then return 1.0else return (y * pow (n-1,y) ) end
end
![Page 46: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/46.jpg)
46
Staged Programmingbut not
Staged Metaprogramming
![Page 47: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/47.jpg)
47
Old Goal
► Try to do “as much as possible” in f1
![Page 48: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/48.jpg)
48
Revised Goals
► Try to do “as little as possible” in f2
► Then, try to do “as little as possible” in f1
► Then, try to do “as much as possible” when generating f1, f2
![Page 49: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/49.jpg)
49
Explicit Staging Annotations
► Quote and splice are one option
► Delay and force is another
► Rate qualifiers are yet another
► uniform and varying
► Appear to be related to “world” type in modal type theories
delay(exp) <-> function() return exp endforce(exp) <-> exp()
[“Modal Types for Mobile Code” Muphy 2008]
![Page 50: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/50.jpg)
50
Real-Time Shading Language[Proudfoot et al. 2001]
![Page 51: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/51.jpg)
51
surface shader float4 Simple( ... ){
constant float3 L_world = normalize({1, 1, 1});
primitivegroup matrix4 viewProj = view * proj;
vertex float4 P_proj = P_world * viewProj;vertex float NdotL = max(dot(N_world, L_world), 0);
fragment float4 diffuse = texture(diffuseTex, uv);fragment float4 color = diffuse * NdotL;
return color;}
Key:
keywordtypeconstantrate
![Page 52: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/52.jpg)
52
surface shader float4 Simple( ... ){
constant float3 L_world = normalize({1, 1, 1});
primitivegroup matrix4 viewProj = view * proj;
vertex float4 P_proj = P_world * viewProj;vertex float NdotL = max(dot(N_world, L_world), 0);
fragment float4 diffuse = texture(diffuseTex, uv);fragment float4 color = diffuse * NdotL;
return color;}
Key:
keywordtypeconstantrate
![Page 53: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/53.jpg)
53
surface shader float4 Simple( ... ){
constant float3 L_world = normalize({1, 1, 1});
primitivegroup matrix4 viewProj = view * proj;
vertex float4 P_proj = P_world * viewProj;vertex float NdotL = max(dot(N_world, L_world), 0);
fragment float4 diffuse = texture(diffuseTex, uv);fragment float4 color = diffuse * NdotL;
return color;}
Key:
keywordtypeconstantrate
![Page 54: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/54.jpg)
54
surface shader float4 Simple( ... ){
constant float3 L_world = normalize({1, 1, 1});
primitivegroup matrix4 viewProj = view * proj;
vertex float4 P_proj = P_world * viewProj;vertex float NdotL = max(dot(N_world, L_world), 0);
fragment float4 diffuse = texture(diffuseTex, uv);fragment float4 color = diffuse * NdotL;
return color;}
Key:
keywordtypeconstantrate
![Page 55: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/55.jpg)
55
surface shader float4 Simple( ... ){
constant float3 L_world = normalize({1, 1, 1});
primitivegroup matrix4 viewProj = view * proj;
vertex float4 P_proj = P_world * viewProj;vertex float NdotL = max(dot(N_world, L_world), 0);
fragment float4 diffuse = texture(diffuseTex, uv);fragment float4 color = diffuse * NdotL;
return color;}
Key:
keywordtypeconstantrate
![Page 56: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/56.jpg)
56
Map to Shader Graph
color
diffuse
NdotL
P_proj
viewProj
view
projN_world
L_world
diffuseTex
uv
P_world
![Page 57: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/57.jpg)
57
Color by Rates
color
diffuse
NdotL
P_proj
viewProj
view
projN_world
L_world
diffuseTex
uv
P_world
![Page 58: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/58.jpg)
58
Color by Rates
constant
color
diffuse
NdotL
P_proj
viewProj
view
projN_world
L_world
diffuseTex
uv
P_world
![Page 59: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/59.jpg)
59
Color by Rates
constant primitive group
color
diffuse
NdotL
P_proj
viewProj
view
projN_world
L_world
diffuseTex
uv
P_world
![Page 60: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/60.jpg)
60
Color by Rates
constant vertexprimitive group
color
diffuse
NdotL
P_proj
viewProj
view
projN_world
L_world
diffuseTex
uv
P_world
![Page 61: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/61.jpg)
61
Color by Rates
constant fragmentvertexprimitive group
color
diffuse
NdotL
P_proj
viewProj
view
projN_world
L_world
diffuseTex
uv
P_world
![Page 62: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/62.jpg)
62
Partition
constant fragmentvertexprimitive group
color
diffuse
NdotL
P_proj
viewProj
view
projN_world
L_world
diffuseTex
uv
P_world
![Page 63: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/63.jpg)
63
Partition
constant fragmentvertexprimitive group
color
diffuse
NdotL
P_proj
viewProj
view
projN_world
L_world
diffuseTex
uv
P_world
![Page 64: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/64.jpg)
64
Spark[Foley and Hanrahan 2011]
![Page 65: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/65.jpg)
65
Shader Components in a Modern Game
► Materials (pattern generation / BSDFs)
► Lights / Shadows
► Volumes (e.g., fog)
► Animation
► Geometry (e.g, tessellation, displacement)
► “Camera” (rendering mode)
► 2D/cubemap/stereo, color/depth output
![Page 66: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/66.jpg)
66
define shader graphs as classes
compose with inheritance
![Page 67: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/67.jpg)
67
Define Shader Graphs as Classes
abstract mixin shader class SimpleDiffuse : D3D11DrawPass{
input @Uniform float3 L_world;abstract @FineVertex float3 N_world;virtual @Fragment float4 diffuse = float4(1.0f);
@Fragment float NdotL = max(dot(L_world, N_world), 0.0f);@Fragment float4 color = diffuse * NdotL;
output @Pixel float4 target = color;}
Key:
keywordtypeconstantrate
![Page 68: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/68.jpg)
68
Define Shader Graphs as Classes
abstract mixin shader class SimpleDiffuse : D3D11DrawPass{
input @Uniform float3 L_world;abstract @FineVertex float3 N_world;virtual @Fragment float4 diffuse = float4(1.0f);
@Fragment float NdotL = max(dot(L_world, N_world), 0.0f);@Fragment float4 color = diffuse * NdotL;
output @Pixel float4 target = color;}
N_world
diffuse
NdotL
color
L_world
![Page 69: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/69.jpg)
69
Define Shader Graphs as Classes
shader class SimpleDiffuse{
...}
shader class CubicGregoryACC { ... } shader class MyTextureMapping { ... }shader class ScalarDisplacement { ... }...
![Page 70: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/70.jpg)
70
Compose With Inheritance
shader class Composedextends SimpleDiffuse
{}
![Page 71: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/71.jpg)
71
Compose With Inheritance
shader class Composedextends SimpleDiffuse,
CubicGregoryACC
{}
![Page 72: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/72.jpg)
72
Compose With Inheritance
shader class Composedextends SimpleDiffuse,
CubicGregoryACC,TextureMapping
{}
![Page 73: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/73.jpg)
73
Compose With Inheritance
shader class Composedextends SimpleDiffuse,
CubicGregoryACC,TextureMapping,ScalarDisplacement
{}
![Page 74: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/74.jpg)
74
Cg, HLSL, GLSL, etc.[Mark et al. 2003]
[Microsoft]
[OpenGL ARB]
[Sony, Apple, …]
![Page 75: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/75.jpg)
75
Assumption so far…
► Decompose program according to problem domain
► Material, lights, animation, etc.
► Compiler uses rates/staging to map to solution domain
► Specialization
► SIMD, GPU
![Page 76: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/76.jpg)
76
New assumption
► Decompose program according to solution domain
► Programmable stages of the GPU graphics pipeline
► Programmer must use ??? to align with problem domain
► Procedural abstraction?
► Object-oriented programming?
► Preprocessor?
![Page 77: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/77.jpg)
77
New assumption
► Decompose program according to solution domain
► Programmable stages of the GPU graphics pipeline
► Programmer must use ??? to align with problem domain
► Procedural abstraction?
► Object-oriented programming?
► Preprocessor?
![Page 78: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/78.jpg)
78
Looking Ahead
![Page 79: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/79.jpg)
79
“Just write shaders in C++”
![Page 80: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/80.jpg)
80
“Just write shaders in C++”
the same language as your application
![Page 81: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/81.jpg)
81
Most shader code is “just code”
![Page 82: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/82.jpg)
82
Most shader code is “just code”
Works for CPU, GPU compute, graphics
![Page 83: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/83.jpg)
83
Write “just code” part in language X
Write shader-specific parts in EDSL,implemented in language X
![Page 84: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/84.jpg)
84
Write “just code” part in Terra
Write shader-specific parts in EDSL,implemented in Terra
![Page 85: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/85.jpg)
85
Conclusion
► Productivity
► Decompose program according to problem domain
► Performance
► Use rates (staging) to guide code generation
► Generality
► Embed shaders into applications languages as EDSLs
![Page 87: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/87.jpg)
87
ReferencesShade Trees
[Robert L. Cook 1984]
A Language for Shading and Lighting Calculations
[Pat Hanrahan and Jim Lawson 190]
Compilers and Staging Transformations
[Ulrik Jørring and William L. Scherlis 1986]
A Real-Time Procedural Shading System for Programmable Graphics Hardware
[Kekoa Proudfoot, William R. Mark, Svetoslav Tvetkov, and Pat Hanrahan 2001]
Spark: Modular, Composable Shaders for Graphics Hardware
[Tim Foley and Pat Hanrahan 2011]
Cg: A System for Programming Graphics Hardware in a C-like Language
[William R. Mark, R. Steven Glanville, Kurt Akeley, and Mark J. Kilgard 2003]
Mobile Types for Mobile Code
[Tom Murphy VII 2008]
![Page 88: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/88.jpg)
88
![Page 89: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/89.jpg)
89
Backup
![Page 90: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/90.jpg)
90
Cg, HLSL, GLSL
![Page 91: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/91.jpg)
91
Output
Merger
The Direct3D 11 Pipeline
Fragment
ShaderRasterizer
Geometry
Shader
Domain
ShaderTessellator
Hull
Shader
Vertex
Shader
Input
Assembler
![Page 92: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/92.jpg)
92
Programmable Stages
Fragment
Shader
Geometry
Shader
Domain
Shader
Hull
Shader
Vertex
Shader
![Page 93: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/93.jpg)
93
Programmable Stages
Fragment ShaderGeometry ShaderDomain ShaderHull ShaderVertex Shader
![Page 94: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/94.jpg)
94
Problem-Domain Components
Fragment ShaderGeometry ShaderDomain ShaderHull ShaderVertex Shader
![Page 95: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/95.jpg)
95
Problem-Domain Components
Fragment ShaderGeometry ShaderDomain ShaderHull ShaderVertex Shader
SimpleDiffuse
CubicGregoryACC
ScalarDisplacement
TextureMapping
![Page 96: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/96.jpg)
96
Problem-Domain Components
Fragment ShaderGeometry ShaderDomain ShaderHull ShaderVertex Shader
SimpleDiffuse
CubicGregoryACC
ScalarDisplacement
TextureMapping
PointLightRenderToCubeMap
![Page 97: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/97.jpg)
97
Cross-Cutting Concerns
Fragment ShaderGeometry ShaderDomain ShaderHull ShaderVertex Shader
SimpleDiffuse
CubicGregoryACC
ScalarDisplacement
TextureMapping
PointLightRenderToCubeMapCubicGregoryACC
TextureMappingTextureMapping TextureMapping TextureMapping
![Page 98: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/98.jpg)
98
Coupling
Fragment ShaderGeometry ShaderDomain ShaderHull ShaderVertex Shader
SimpleDiffuse
CubicGregoryACC
ScalarDisplacement
TextureMapping
PointLightRenderToCubeMapCubicGregoryACC
TextureMappingTextureMapping TextureMapping TextureMapping
![Page 99: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/99.jpg)
99
Combinatorial Explosion
Fragment ShaderGeometry ShaderDomain ShaderHull ShaderVertex Shader
SimpleDiffuse
CubicGregoryACC
ScalarDisplacement
TextureMapping
PointLightRenderToCubeMapCubicGregoryACC
TextureMappingTextureMapping TextureMapping TextureMapping
![Page 100: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/100.jpg)
100
Terra-Integrated Shaders
![Page 101: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/101.jpg)
101
local pipeline ToyPipeline {uniform Uniforms {
mvp : mat4;}input P_model : vec3;input N_model : vec3;output C : vec4;
varying N = N_model;
vertex codegl_Position = mvp * make_vec4(P_model, 1.0);
end
fragment codeC= make_vec4(normalize(N), 1.0);
end}
Key:
keywordtypeconstantrate
![Page 102: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/102.jpg)
102
local pipeline ToyPipeline {...
}
terra init()...GL.glShaderSource(vertexShader, 1, [ToyPipeline.vertex.glsl], nil);...
end
terra render()GL.glVertexAttribPointer([ToyPipeline.P_model.__location], ...);GL.glBindBufferBase(
GL.GL_UNIFORM_BUFFER,[ToyPipeline.Uniforms.__binding],uniformBuffer);
end
Key:
keywordtypeconstantrate
![Page 103: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/103.jpg)
103
local pipeline ToyPipeline {...
}
terra init()...toyPipeline = ToyPipeline.new();toyPipeline.P_model.set( vertexData, ... );
end
terra render()toyPipeline.Uniforms.mvp.set( camera.modelViewProj );Gfx.push(toyPipeline);Gfx.draw();
end
Key:
keywordtypeconstantrate
![Page 104: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/104.jpg)
104
local pipeline Camera { ... }local pipeline SkeletalAnimation { ... }local pipeline PhongMaterial { ... }local pipeline PointLight { ... }...
terra render()for m = 0,materialCount do
var mat = &materials[m];Gfx.push(mat.pipeline);for n = 0,mat.meshCount do
Gfx.push(mat.meshes[n].pipeline);Gfx.draw();Gfx.pop();
endGfx.pop();
endend
Key:
keywordtypeconstantrate
![Page 105: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/105.jpg)
105
Staging Isn’t Always For Performance
![Page 106: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/106.jpg)
106
Shade Trees
DisplacementDiffuse Material
Point Light
P
NdotLNfinal_color
...
Lidelta
L
lightPos
intensity
![Page 107: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/107.jpg)
107
Point Light
Lidelta
L
lightPos
intensity
What if we have multiple lights?
DisplacementDiffuse Material
Point Light
P
NdotLNfinal_color
...
Lidelta
L
lightPos
intensity
![Page 108: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/108.jpg)
108
Displacement
Point Light
Lidelta
L
lightPos
intensity
Diffuse Material
What if we have multiple lights?
Point Light
P
Nfinal_color
...
Lidelta
L
lightPos
intensity
NdotL+
NdotL
![Page 109: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/109.jpg)
109
RenderMan Shading Language
![Page 110: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/110.jpg)
110
Surface and Light Shaders
![Page 111: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/111.jpg)
111
Linkage via control-flow constructs
color C = 0;illuminance( P, N, Pi/2 ) {
L = normalize(L);C += Kd * Cd * Cl * length(L ^ T);
}
Key:
keywordtypeconstant
illuminate( P, N, beamangle ) {Cl = (intensity*lightcolor)/(L . L);
}
surface shader light shader(s)
illuminate( P, N, beamangle ) {...
}
![Page 112: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/112.jpg)
112
Linkage via control-flow constructs
color C = 0;illuminance( P, N, Pi/2 ) {
L = normalize(L);C += Kd * Cd * Cl * length(L ^ T);
}
Key:
keywordtypeconstant
illuminate( P, N, beamangle ) {Cl = (intensity*lightcolor)/(L . L);
}
surface shader light shader(s)
illuminate( P, N, beamangle ) {...
}
![Page 113: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/113.jpg)
113
Linkage via control-flow constructs
color C = 0;illuminance( P, N, Pi/2 ) {
L = normalize(L);C += Kd * Cd * Cl * length(L ^ T);
}
Key:
keywordtypeconstant
illuminate( P, N, beamangle ) {Cl = (intensity*lightcolor)/(L . L);
}
surface shader light shader(s)
illuminate( P, N, beamangle ) {...
}
![Page 114: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/114.jpg)
114
Linkage via control-flow constructs
color C = 0;illuminance( P, N, Pi/2 ) {
L = normalize(L);C += Kd * Cd * Cl * length(L ^ T);
}
Key:
keywordtypeconstant
illuminate( P, N, beamangle ) {Cl = (intensity*lightcolor)/(L . L);
}
surface shader light shader(s)
illuminate( P, N, beamangle ) {...
}
![Page 115: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/115.jpg)
115
Linkage via control-flow constructs
color C = 0;illuminance( P, N, Pi/2 ) {
L = normalize(L);C += Kd * Cd * Cl * length(L ^ T);
}
Key:
keywordtypeconstant
illuminate( P, N, beamangle ) {Cl = (intensity*lightcolor)/(L . L);
}
surface shader light shader(s)
illuminate( P, N, beamangle ) {...
}
![Page 116: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/116.jpg)
116
Could re-cast as higher-order functions
color C = 0;illuminance( P, N, Pi/2 ) {
L = normalize(L);C += Kd * Cd * Cl * length(L ^ T);
}
Key:
keywordtypeconstant
illuminate( P, N, beamangle ) {Cl = (intensity*lightcolor)/(L . L);
}
surface shader light shader(s)
![Page 117: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/117.jpg)
117
Could re-cast as higher-order functions
color C = 0;illuminance( P, N, Pi/2, function(L, Cl) {
L = normalize(L);C += Kd * Cd * Cl * length(L ^ T);
});
Key:
keywordtypeconstant
illuminate( P, N, beamangle, function(L) {Cl = (intensity*lightcolor)/(L . L);
});
surface shader light shader(s)
![Page 118: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/118.jpg)
118
Could re-cast as higher-order functions
color C = 0;illuminance( P, N, Pi/2, function(L, Cl) {
L = normalize(L);C += Kd * Cd * Cl * length(L ^ T);
});
Key:
keywordtypeconstant
illuminate( P, N, beamangle, function(L) {Cl = (intensity*lightcolor)/(L . L);
});
surface shader light shader(s)
closure to apply to each illumination sample
![Page 119: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/119.jpg)
119
RTSL perlight rate
► Computations that depend on both surface and light
► System instantiates this sub-graph for each light
► Sums results when converting per-light to fragment
► In Spark, can implement @Light in user space
![Page 120: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/120.jpg)
120
Modern renderers need different decomposition
► Physically-based rendering
► Want to guarantee energy conservation, etc. of BSDFs
► Ray tracing
► Renderer wants to control sampling, integration, scheduling of rays
![Page 121: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/121.jpg)
121
Decompose surface shader into
► Pattern generation
► May be authored by artists
► Might not even need a language
► BSDF evaluation, integration, etc.
► Authored by programmers, or technical artists
► Typically only need a few (diffuse, dielectric, skin, …)
![Page 122: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/122.jpg)
122
Unity Standard Shader
► Artist only sets textures, colors
► Covers most use cases
![Page 123: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/123.jpg)
123
Unreal Engine Material Editor
► Graphical DSL
► Also used for
► Animation
► Scripting
► Audio mixing
► …
![Page 124: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/124.jpg)
124
Open Shading Language (OSL)
surface Glass(color Kd = 0.8,float ior = 1.45,output closure color bsdf = 0)
{float fr = FresnelDieletric(I, N, ior);bsdf = Kd * (fr*reflection(N) + (1-fr)*refraction(N, ior));
}
![Page 125: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/125.jpg)
125
Open Shading Language (OSL)
surface Glass(color Kd = 0.8,float ior = 1.45,output closure color bsdf = 0)
{float fr = FresnelDieletric(I, N, ior);bsdf = Kd * (fr*reflection(N) + (1-fr)*refraction(N, ior));
}
shader outputs a “radiance closure,”
to be scheduled by renderer
closures created with built-in functions,
then combined with operators like +
![Page 126: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/126.jpg)
126
Two-Stage Material Shading Pipeline
BSDF
EvaluationIntegrator
Pattern
Generation
![Page 127: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/127.jpg)
127
Automatic Rate Placement
![Page 128: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/128.jpg)
128
Rates are a way to express scheduling
![Page 129: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/129.jpg)
129
Decouple algorithm from schedule?
Automatically generate a good schedule?
![Page 130: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/130.jpg)
130
A System for Rapid, AutomaticShader Level-of-Detail
[He, Foley, Tatarchuk, Fatahalian 2015]
![Page 131: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/131.jpg)
131
Shader Simplification
1.7ms/frame 0.8ms/frame
![Page 132: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/132.jpg)
132
Observation:
The best simplifications tend to come from reducing the rate at which a term is computed
![Page 133: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/133.jpg)
133
Move fragment code to vertex shader
Move vertex code to “parameter” shader
![Page 134: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/134.jpg)
134
Project started with vertex+fragment shaders
Next step is “rate-less” pipeline shaders
![Page 135: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/135.jpg)
135
Encoding algorithm choice
![Page 136: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/136.jpg)
136
expensive = computeExpensiveBRDF(N, L, p1, p2, ...)
color = expensive
![Page 137: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/137.jpg)
137
expensive = computeExpensiveBRDF(N, L, p1, p2, ...)cheap = computeCheapBRDF(N, L, param)color = [choice(`cheap, `expensive)]
![Page 138: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/138.jpg)
138
expensive = computeExpensiveBRDF(N, L, p1, p2, ...)
color = [choice(`expensive, moveToVertex(`expensive))]
![Page 139: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/139.jpg)
139
expensive = computeExpensiveBRDF(N, L, p1, p2, ...)cheap = computeCheapBRDF(N, L, [fitParameter(`expensive)])color = [choice(`cheap, `expensive)]
![Page 140: Higer order filters as B-splines - Stanford Universitycs448h.stanford.edu/shading-languages-with-backup.pdf · 2015-12-02 · 6 Productivity Build shaders from re-usable components](https://reader034.vdocuments.net/reader034/viewer/2022042023/5e7ab15efcfdc827a116646d/html5/thumbnails/140.jpg)
140
Explicit choices can makeauto-tuning tractable