Title: GPU Shading and Rendering: OpenGL Shading Language
1(No Transcript)
2GPU Shading and RenderingOpenGL Shading Language
3OpenGL Shading
- High level language
- OpenGL Shading Language GLslang GLSL
- Integrated into OpenGL API (no extra run-time)
4Organization
- API
- Vertex Shading
- Fragment Shading
- Lots of demos.
- 2-year old Apple PowerBook G4/1.5GHz
- ATI Mobility Radeon 9700
5API-integrated
- Compiler built into driver
- Presumably they know your card best
- IHVs must produce (good) compilers
- Use built-in parameters (glColor, glNormal, )
- Add your own
- Other options can still produce low-level code
- Cg, ASHLI, RapidMind,
- With loss of integration
6Using High-level Code
- Create shader objectS glCreateShader(GL_VERTEX_
SHADER)S glCreateShaderObjectARB(GL_VERTEX_SHAD
ER_ARB) - Vertex or Fragment
- Load shader into objectglShaderSource(S, n,
shaderArray, lenArray)glShaderSourceARB(S, n,
shaderArray, lenArray) - Array of strings
- Compile objectglCompileShader(S)glCompileShaderA
RB(S)
7Loading Shaders
- glShaderSource(S, n, shaderArray, lenArray)
- One string containing entire mmapd file
- Strings as includes
- Varying variables between vertex and fragment
- Strings as lines
- Null-terminated if lenArray is Null or length-1
8Using High-level Code (2)
- Create program objectP glCreateProgram()P
glCreateProgramObjectARB() - Attach all shader objectsglAttachShader(P,
S)glAttachObjectARB(P, S) - Vertex, Fragment or both
- Link togetherglLinkProgram(P)glLinkProgramARB(P)
- UseglUseProgramObject(P)glUseProgramObjectARB(P)
9Using High-level Code (3)
- Where is my attributes/uniforms
parameter?iglGetAttribLocation(P,myAttrib)ig
lGetUniformLocation(P,myAttrib) - Set themglVertexAttrib1f(i,value)glVertexAttribP
ointer(i,)glUniform1f(i,value)
10Using Low-level Code
- Load shaderglProgramStringARB(GL_VERTEX_PROGRAM_A
RB, GL_PROGRAM_FORMAT_ASCII_ARB, length, shader) - Vertex or fragment
- Single string (vs. array)
- EnableglEnable(GL_VERTEX_PROGRAM_ARB)
11Useful Tools
- Shader debugger
- Immediate updates
- Choose model/texture
- Tweak parameters
- Examine/dump frames
- Several available
- Not hard to build
- OpenGL debugger
- Trace of calls made
- Examine resources
- Breakpoints/actions
- Graph performance
- A couple of choices
12gDEBugger A Professional OpenGL Debugger and
Profiler
- Provides graphic pipeline information needed to
find bugs and to optimize application
performance - Shortens debugging and profiling time
- Improves application quality
- Optimizes application performance
13Free gDEBugger License for Academic Users!
- OpenGL ARB and Graphic Remedy Academic Program
- Annual program for all OpenGL Academic users
- License of the full feature version for one year
- Includes all software updates
- A limited number of free licenses available
fornon-commercial developers who are not in
academia - More details http//academic.gremedy.com
14Non-windows OS
- Linux
- gDEBugger in progress
- Apple OpenGL Profiler and Driver Monitor
- Free part of OS / Developer tools
15Vertex DemoBlend Positions
16High-level Code
void main() float Kin gl_Color.r
// key input // screen position from vertex
and texture vec4 Vp ftransform() vec4
Tp vec4(gl_MultiTexCoord0.xy1.8-.9, 0.,1.)
// interpolate between Vp and Tp
gl_Position mix(Tp,Vp,pow(1.-Kin,8.)) //
copy to output gl_TexCoord0
gl_MultiTexCoord0 gl_TexCoord1 Vp
gl_TexCoord3 vec4(Kin)
17Main Function
void main() float Kin gl_Color.r
// key input // screen position from vertex
and texture vec4 Vp ftransform() vec4
Tp vec4(gl_MultiTexCoord0.xy1.8-.9, 0.,1.)
// interpolate between Vp and Tp
gl_Position mix(Tp,Vp,pow(1.-Kin,8.)) //
copy to output gl_TexCoord0
gl_MultiTexCoord0 gl_TexCoord1 Vp
gl_TexCoord3 vec4(Kin)
18Use Standard OpenGL State
void main() float Kin gl_Color.r
// key input // screen position from vertex
and texture vec4 Vp ftransform() vec4
Tp vec4(gl_MultiTexCoord0.xy1.8-.9, 0.,1.)
// interpolate between Vp and Tp
gl_Position mix(Tp,Vp,pow(1.-Kin,8.)) //
copy to output gl_TexCoord0
gl_MultiTexCoord0 gl_TexCoord1 Vp
gl_TexCoord3 vec4(Kin)
19Built-in Types
void main() float Kin gl_Color.r
// key input // screen position from vertex
and texture vec4 Vp ftransform() vec4
Tp vec4(gl_MultiTexCoord0.xy1.8-.9, 0.,1.)
// interpolate between Vp and Tp
gl_Position mix(Tp,Vp,pow(1.-Kin,8.)) //
copy to output gl_TexCoord0
gl_MultiTexCoord0 gl_TexCoord1 Vp
gl_TexCoord3 vec4(Kin)
20Swizzle / Channel Selection
void main() float Kin gl_Color.r
// key input // screen position from vertex
and texture vec4 Vp ftransform() vec4
Tp vec4(gl_MultiTexCoord0.xy1.8-.9, 0.,1.)
// interpolate between Vp and Tp
gl_Position mix(Tp,Vp,pow(1.-Kin,8.)) //
copy to output gl_TexCoord0
gl_MultiTexCoord0 gl_TexCoord1 Vp
gl_TexCoord3 vec4(Kin)
21Vector Construction
void main() float Kin gl_Color.r
// key input // screen position from vertex
and texture vec4 Vp ftransform() vec4
Tp vec4(gl_MultiTexCoord0.xy1.8-.9, 0.,1.)
// interpolate between Vp and Tp
gl_Position mix(Tp,Vp,pow(1.-Kin,8.)) //
copy to output gl_TexCoord0
gl_MultiTexCoord0 gl_TexCoord1 Vp
gl_TexCoord3 vec4(Kin)
22Built-in Functions
void main() float Kin gl_Color.r
// key input // screen position from vertex
and texture vec4 Vp ftransform() vec4
Tp vec4(gl_MultiTexCoord0.xy1.8-.9, 0.,1.)
// interpolate between Vp and Tp
gl_Position mix(Tp,Vp,pow(1.-Kin,8.)) //
copy to output gl_TexCoord0
gl_MultiTexCoord0 gl_TexCoord1 Vp
gl_TexCoord3 vec4(Kin)
23Vertex Fragment DemoFresnel Environment Map
24Trick 1 Where is the Eye
ObjectSpace
ModelViewMatrix
EyeSpace
Projection Matrix
ClipSpace
- Where is the Eye in Eye Space?
- (0,0,0)? Not necessarily!
- Know where it is in Clip Space
- (0,0,-1,0), looking in the (0,0,1,0) direction
- Assuming GL_LESS depth test
- Invert projection to find the eye!
- Works for any eye position, or even parallel
projection.
25Trick 2 Subtract Homogeneous Points
- Homogeneous point vec4(V.xyz, V.w)
- 3D equivalent V.xyz/V.w
- Defers division, makes perspective, translation,
and many things happy - Vector subtraction VE
- V.xyz/V.w E.xyz/E.w
- (V.xyzE.w E.xyzV.w)/(V.wE.w)
26Trick 3 Skip Division for Normalize
- normalize(V.xyz/V.w) normalize(V.xyz)
- If V.w isnt negative
- Put it all together
- normalize(V-E)
- normalize(V.xyzE.w - E.xyzV.w)
27OpenGL State DemoVertex Lighting
28Lighting Vectors in Eye Space
void main() // convert shading-related
vectors to eye space vec4 P
gl_ModelViewMatrixgl_Vertex vec4 E
gl_ProjectionMatrixInversevec4(0,0,-1,0) vec3
V normalize(E.xyzP.w-P.xyzE.w) vec3 N
normalize(gl_NormalMatrixgl_Normal)
29Accumulate Each Light
// accumulate contribution from each light
gl_FrontColor vec4(0) for(int i0
iltgl_MaxLights i) vec3 L
normalize(gl_LightSourcei.position.xyzP.w
- P.xyzgl_LightSourcei.position.w)
vec3 H normalize(LV) float diff
dot(N,L) gl_FrontColor
gl_LightSourcei.ambient if (diff gt 0.)
gl_FrontColor
gl_LightSourcei.diffuse diff
gl_FrontColor gl_LightSourcei.specul
ar max(pow(dot(N,H), gl_FrontMaterialShinines
s),0.)
30Standard Vertex Shader Stuff
// standard texture coordinate and position
stuff gl_TexCoord0 gl_TextureMatrix0gl_Mu
ltiTexCoord0 gl_Position ftransform()
31Noise
- Controlled, repeatable randomness
- Still spotty implementation
- Can use texture or compute
32Noise Characteristics
- Repeatable
- Locally continuous but distant points
uncorrolated - values -1,1, average 0
- 1/2 1 cycle per unit
- Versions for n-D input
33Noise Subtleties
- Many noise functions based on a lattice
- Like a spline between integer coordinates
- Hash of integer coordinates ? control points
- Interpolating values easy but poor
- Even with higher-order interpolation
- Perlins noise
- Passes through 0 at each integer
- Hash gives gradient
34Modified Noise Olano 2005
- Three relatively independent modifications
- New computable hash
- Change gradient computation
- Reorder computation
- Variety of computation/texture options
- Can just store in a texture
- Can compute with some texture accesses
- Can compute with no texture accesses
35Computable Hash
- Normal hash chains access to permutation texture
- Want totally computable hash
- mod(kx2, m)
- Still chain for higher-D
- hash(floor(P.x) hash(floor(P.y)))
- Not quite as good, but cheap computable
- Noise usually not used alone
36Gradient
- 3D Gradient (fract(P.x), fract(P.y))
- Each sign from one bit of hash
- Made slightly more difficult without bitwise ops
- Allows noise(x) noise(x,0)
- If 2D noise is stored in a texture
- Can share the same texture for 1D noise as well
- Not normally true!
37Reordered Computation
- Refactor to be able to build n-D noise from two
shifted calls to n-1 D noise - If 2D noise is stored in a texture
- Can build 3D noise from 2 texture accesses
- Can build 4D noise from 4 texture accesses
38Shader Design Strategies
- Learn and adapt from RenderMan
- Noise
- Layers
- Multiple Passes
- Baked computation
39(No Transcript)