Вы находитесь на странице: 1из 63

GLSL Shading with

OpenSceneGraph

Mike Weiblen
July 31, 2005
Los Angeles

Rev B
1
Summary of this talk
y Overview of GLSL pipeline & syntax
y OSG support for GLSL
y Tips, Tricks, Gotchas, and Tools
y Demos

y What we’re not covering


ƒ Our focus will be the OSG API, not OpenGL API
ƒ Not lots of detail on the GLSL language itself

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 2


Our dependencies
y OpenGL 2.0
y OpenGL Shading Language 1.10 rev 59
ƒ Specs on opengl.org and CDROM
y OpenSceneGraph 0.9.9
ƒ Note: GLSL support continues to evolve in CVS

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 3


GLSL / OSG timeline
y Fall 2001: 3Dlabs “GL2.0” whitepapers,
shading language is the centerpiece
y July 2003: ARB approves GL1.5, GLSL as
ARB extension
y Fall 2003: osgGL2 Nodekit
y Sep 2004: ARB approves GL2.0, GLSL in
the core.
y Spring 2005: 2nd generation GLSL support
integrated into OSG core
ƒ Supports both OpenGL 2.0 and 1.5 extensions

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 4


Overview of GLSL

5
The OpenGL 2.0 Pipeline

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 6


GLSL pipeline architecture
y GLSL exposes programmability at two
points in the OpenGL 2.0 pipeline
ƒ Vertex processor
ƒ Fragment processor

y Compiled code to run on a particular


processor is a glShader
y A linked executable unit to be activated on
the pipeline is a glProgram

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 7


GLSL compilation model
y Similar to familiar C build model
y glShader = “object file”
ƒ Contains shader source code
ƒ Compiled to become an “.obj file”
ƒ Must be recompiled when source changes
y glProgram = “executable file”
ƒ Contains a list of shaders
ƒ Linked to become an “.exe file”
ƒ Must be relinked when set of shaders changes

y As with C, a glShader “.obj” can be


shared across multiple glPrograms “.exe”
Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 8
Vertex Processor Overview
Standard
OpenGL Generic
attributes attributes
gl_color 0, 1, 2, …
gl_normal
etc.

Vertex User-defined uniforms:


epsilon, myLightPos, surfColor, etc.

Texture Maps Processor Built-in uniforms:


gl_FogColor, gl_ModelViewMatrix, etc.

Standard Special User-defined


Varying Variables Varying
gl_FrontColor gl_Position normal
gl_BackColor gl_ClipVertex refraction
etc. gl_PointSize etc.

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 9


Fragment Processor Overview
Standard Special User-defined
Varying Variables Varying
gl_Color gl_FragCoord normal
gl_SecondaryColor gl_FrontFacing refraction
etc. etc.

Fragment User-defined uniforms:


epsilon, myLightPos, surfColor, etc.

Texture Maps Processor Built-in uniforms:


gl_FogColor, gl_ModelViewMatrix, etc.

Special
Variables
gl_FragColor
gl_FragDepth
gl_FragData[n]

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 10


GLSL language design
y Based on syntax of ANSI C
y Includes preprocessor
y Additions for graphics functionality
y Additions from C++
y Some refactoring for cleaner design
y Designed for parallelization on SIMD array

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 11


Language enhanced for graphics
y Added Vector and Matrix types
y Added Sampler type for textures
y Qualifiers: attribute, uniform, varying
y Built-in variables to access GL state
y Built-in functions
y Vector component notation (swizzling)
y discard keyword to cease fragment
processing

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 12


Additions from C++
y Function overloading
y Variables declared when needed
y struct definition performs typedef
y bool datatype

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 13


Differences from C/C++
y No automatic type conversion
y Constructor notation rather than type cast
ƒ int x = int(5.0);
y Function parameters passed by value-
return
y No pointers or strings

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 14


GLSL compiler
y The GLSL compiler is in the GL driver and
part of the core OpenGL API
y No external compiler tools required.
y So the compiler is always available at
runtime
ƒ Compile shaders whenever convenient
y Tight integration allows every vendor to
exploit their architecture for best possible
performance

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 15


Comparing architectures

Cg GLSL
Cg
Cg Code
Code GLSL
GLSL Code
Code

Cg
Cg Compiler
Compiler

Intermediate
Intermediate Lang
Lang Too
(e.g.
(e.g. ARB
ARB vp/fp)
vp/fp) far
apart

IL
ILTranslator
Translator
Compiler
Compiler
Driver Tightly
Driver Driver
Driver coupled
Graphics
Graphics HW
HW Graphics
Graphics HW
HW

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 16


GLSL datatypes
y Scalars: float, int, bool
y Vectors: float, int, bool
y Matrices: float
y Samplers
y Arrays
y Structs

y Note
ƒ int and bool types are semantic, not expected to
be supported natively
ƒ int at least as 16 bits plus sign bit
Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 17
GLSL datatype qualifiers
y uniform
ƒ Relatively constant data from app or OpenGL
ƒ Input to both vertex and fragment shaders
y attribute
ƒ Per-vertex data from app or OpenGL
ƒ Input to vertex shader only
y varying
ƒ Perspective-correct interpolated value
ƒ Output from vertex, input to fragment
y const
y in, out, inout (for function parameters)

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 18


OpenGL state tracking
y Common OpenGL state is available to
GLSL via built-in variables
ƒ Built-in variables do not need declaration
ƒ All begin with reserved prefix “gl_”
ƒ Includes uniforms, attributes, and varyings
ƒ Makes it easier to interface w/ legacy app code

y However, recommend just defining your


own variables for semantic clarity; resist
temptation to overload built-ins
ƒ FYI OpenGL/ES will have no or few built-ins

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 19


uniform variables
y Input to vertex and fragment shaders
y Values from OpenGL or app
ƒ e.g.: gl_ModelViewProjectionMatrix
y Changes relatively infrequently
ƒ Typically constant over several primitives
y Queriable limit on number of floats
y App sets values with glUniform*() API

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 20


attribute variables
y Input to vertex shader only
y Values from OpenGL or app
ƒ e.g.: gl_Color, gl_Normal
y Can change per-vertex
ƒ But doesn’t have to
y Queriable limit on the # of vec4 slots
ƒ Scalars/vectors take a slot
ƒ Matrices take a slot per column
y Apps sends with per-vertex API or vertex
arrays

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 21


varying variables
y Output from vertex, input to fragment
y Name & type must match across shaders
y Values from vertex stage are perspective-
corrected, interpolated, sent to fragment
stage
y Queriable limit on number of floats
y Usually defined by GLSL code
ƒ although GLSL defines some built-ins; e.g.:
gl_FrontColor (necessary when combining GLSL
with fixed-functionality)

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 22


Defining variables in GLSL
y Uniform, varying, attribute must be global
to a glShader
y Over-declaring variables in GLSL code
doesn’t cost
y Only those variables actually used in the
code (the “active” variables) consume
resources
y After linking, the app queries uniforms
and attributes from glProgram
y Runtime introspection; useful e.g. for
building a GUI on the fly
Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 23
Texture access
y GLSL supports texture access in both
vertex and fragment stages
y However, some hardware may not yet
support texture access in vertex
ƒ Vertex texturing is available when
GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS > 0
y Mipmap LOD is handled differently
between Vertex and Fragment stages

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 24


Shader Configurations
y May have more than 1 glShader per stage
attached to a glProgram
y But there must be exactly one main() per
stage

y Useful for a library of shared GLSL code


to be reused across several glPrograms

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 25


Program Configurations
y GLSL permits mixing a fixed-functionality
stage with a programmable stage
ƒ Prog Vertex + Prog Fragment
ƒ Prog Vertex + FF Fragment
ƒ FF Vertex + Prog Fragment

y GLSL Built-in varyings are key when


mixing programmable stages w/ FF

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 26


GLSL Versioning & Extensions
y #version min_version_number
ƒ Default is “#version 110”
ƒ Good idea to always declare expected version
y #extension name : behavior
ƒ Default is “#extension all : disable”

y Extension names/capabilies defined in


usual GL extensions specifications
y Special name “all” indicates all
extensions supported by a compiler
y Details in GLSL 1.10 spec pp11-12
Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 27
Using GLSL extensions
y Recommended approach

#ifdef ARB_texture_rectangle
#extension ARB_texture_rectangle : require
#endif

uniform sampler2DRect mysampler;

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 28


GLSL Future
y Expect GLSL to evolve
y Possible new language features
ƒ More built-in functions, datatypes
ƒ Interfaces
ƒ Shader trees
y Possible new programmable stages in
pipeline
ƒ Geometry
ƒ Blending

y Use #version and #extension


Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 29
OSG support for
GLSL

30
OSG GLSL design goals
y Continue OSG’s straightforward mapping
of classes to the underlying GL concepts
y Leverage OSG’s state stack to apply GLSL
state with proper scoping
ƒ App specifies where/what GLSL will do.
ƒ OSG determines when/how to apply, restoring to
previous state afterwards.
y Let OSG deal with the tedious GL stuff
ƒ Management of contexts, constructors, indices,
compile/link, etc.

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 31


Mapping GL API to OSG API

y glShader object -> osg::Shader


y glProgram object -> osg::Program
y glUniform*() -> osg::Uniform

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 32


OSG GLSL benefits over GL
y Decouples shaders from GL contexts
y Handles multiple instancing when multiple
GL contexts
y osg::Uniform values correctly applied via
OSG’s state update mechanism at the
appropriate time
y Compilation and linking automatically
handled when osg::Shader/osg::Program
are dirtied by modification

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 33


osg::Shader
y Derived from osg::Object
y Stores the shader’s source code text and
manages its compilation
y Attach osg::Shader to osg::Program
y osg::Shader be attached to more than one
osg::Program
y More than one osg::Shader may be
attached to an osg::Program
y Encapsulates per-context glShaders

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 34


osg::Shader API subset
y Shader Type
ƒ VERTEX or FRAGMENT
y Sourcecode text management
ƒ setShaderSource() / getShaderSource()
ƒ loadShaderSourceFromFile()
ƒ readShaderFile()
y Queries
ƒ getType()
ƒ getGlShaderInfoLog()

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 35


osg::Program
y Derived from osg::StateAttribute
y Defines a set of osg::Shaders, manages
their linkage, and activates for rendering
y osg::Programs may be attached anywhere
in the scenegraph
y An “empty” osg::Program (i.e.: no
attached osg::Shaders) indicates fixed-
functionality
y Automatically performs relink if attached
osg::Shaders are modified
y Encapsulates per-context glPrograms
Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 36
osg::Program API subset
y Shader management
ƒ addShader()
ƒ removeShader()
ƒ getNumShaders()
ƒ getShader()
y Attribute binding management
ƒ addBindAttribLocation()
ƒ removeBindAttribLocation()
ƒ getAttribBindingList()
y Queries
ƒ getActiveUniforms()
ƒ getActiveAttribs()
ƒ getGlProgramInfoLog()
Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 37
osg::Uniform
y Derived from osg::Object
y Attaches to osg::StateSet
y May be attached anywhere in the
scenegraph, not just near osg::Program
ƒ e.g.: set default values at root of scenegraph
y Their effect inherits/overrides through the
scenegraph, like osg::StateAttributes
y OSG handles the uniform index
management automatically

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 38


osg::Uniform API subset
y Uniform Types
ƒ All defined GLSL types (float, vec, mat, etc)
y Value management
ƒ Many convenient constructors
ƒ Many get()/set() methods
y Callback support
ƒ setUpdateCallback() / getUpdateCallback()
ƒ setEventCallback() / getEventCallback()

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 39


Simple source example
y Putting it all together…

osg::Program* pgm = new osg::Program;


pgm->setName( "simple" );
pgm->addShader(new osg::Shader( osg::Shader::VERTEX, vsrc ));
pgm->addShader(new osg::Shader( osg::Shader::FRAGMENT, fsrc ));

osg::StateSet* ss = getOrCreateStateSet();
ss->setAttributeAndModes( pgm, osg::StateAttribute::ON );
ss->addUniform( new osg::Uniform( "color", osg::Vec3(1.0f, 0.0f, 0.0f) ));
ss->addUniform( new osg::Uniform( "val1", 0.0f ));

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 40


Attributes & osg::Program
y GL supports both explicit and automatic
attribute binding
ƒ GLSL will dynamically assign attribute indices if
not otherwise specified
y However, OSG currently supports only
explicit binding, so app must assign
indices
ƒ Automatic binding makes display lists dependent
on osg::Program, and has impact on DL sharing
y GLSL specifies much freedom in selecting
attribute indices, but some current drivers
impose restrictions
Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 41
Using textures
y In the OSG app code
ƒ Construct an osg::Texture
ƒ Load image data, set filtering, wrap modes
ƒ Attach Texture to StateSet on any texunit
ƒ Create an int osg::Uniform with the texunit ID,
attach to StateSet
y In GLSL code
ƒ Declare a uniform sampler*D foo;
ƒ Access the texture with texture*D( foo,
coord );

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 42


OSG preset uniforms
y “Always available” values, like OpenGL
built-in uniforms
y In osgUtil::SceneView
ƒ int osg_FrameNumber;
ƒ float osg_FrameTime;
ƒ float osg_DeltaFrameTime;
ƒ mat4 osg_ViewMatrix;
ƒ mat4 osg_InverseViewMatrix;
y Automatically updated once per frame by
SceneView
y Bitmask to disable updating if desired
Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 43
OSG file formats & GLSL
y .osg & .ive formats have full read/write
support for GLSL objects
y OSG formats can serve as a GLSL effect
file.
y Today’s demos consist simply of a .osg
file
ƒ no runtime app other than osgviewer required

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 44


Tips, Tricks,
Gotchas
Gotchas,, and Tools

45
Tips for Shader Debugging
y Name your osg::Shaders/osg::Programs
y Review the infologs displayed at notify
osg::INFO level.
y Assign internal vecs to color
y Use discard like assert
y Verify your code for conformance
y Try glsl_dataflag.osg to see values inside
your scene
y New in CVS: glValidateProgram() support

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 46


GLSL performance tips
y Put algorithm in the right stage
ƒ Don’t compute in fragment if could be passed
from vertex stage or app
y Don’t interpolate more than necessary
ƒ If your texture coord is a vec2, don’t pass as vec4
y Try passing data as attributes rather than
uniforms
ƒ Changing uniforms sometimes have a setup cost
y Use built-in functions and types
y Review the infologs
ƒ Driver may give hints on non-optimal code

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 47


GLSL language gotchas
y Comparing float values
ƒ Use an epsilon
y Varyings are interpolated
ƒ Interpolating from A to A may not exactly == A
y int is semantic (usually float internally)

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 48


GLSL implementation gotchas
y Drivers are new, there’s room for improvement
y Don’t learn GLSL empirically on your driver
y Example portability issues
ƒ “Any extended behavior must first be enabled.” (p11)
ƒ “There are no implicit conversions between types.” (p16)
ƒ Writes to read-only variables
ƒ Additional resource constraints (attribute slots, texture units)
ƒ Loops forced to constant number of iterations
y Review your driver’s release notes, take heed
y Note the driver’s GLSL version string
y Depend on the GL and GLSL specs
y Be vigilant now for compatibility later

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 49


On the CDROM
y Documentation
ƒ OpenGL 2.0, GLSL 1.10 specifications
ƒ GLSL Overview whitepaper & Quick Reference
ƒ OpenGL manpages (HTML & VS.net help)
y Open-source tools from 3Dlabs website
ƒ GLSL Demo
ƒ GLSL Parser Test
ƒ GLSL Validate
ƒ ShaderGen
ƒ GLSL Compiler Front-end

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 50


GLSL Validate
y Open source, including commercial use
y Uses the 3Dlabs GLSL compiler front-end
to check the validity of a shader
y Contains both command line and GUI
interface
y Does NOT require a GLSL-capable driver

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 51


GLSL Parse Test
y Open source, including commercial use
y Suite of over 140 GLSL shader tests
y Includes both known-good and known-
bad test cases
y Compiles each shader, compares to
expected results
y Results are summarized, written to HTML
y Info logs can be examined
y It tests a driver’s GLSL compiler, so a
GLSL-capable driver required (duh)

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 52


GLSL Compiler Front-End
Front-End
y Open source, including commercial use
y Part of 3Dlabs’ production compiler
y Compiles on Windows and Linux
y Performs:
ƒ Preprocessing
ƒ Lexical analysis
ƒ Syntactic analysis
ƒ Semantic analysis
ƒ Constructs a high-level parse tree.

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 53


osgToy::GlslLint
osgToy::GlslLint
y Proof-of-concept GLSLvalidate-like
functionality integrated with OSG
y Uses the 3Dlabs GLSL compiler front-end
y No GLSL driver or hardware necessary
y Currently part of the osgToy collection
ƒ http://sourceforge.net/projects/osgtoy/

y Accessible from C++ as a NodeVisitor:


ƒ osgToy::GlslLintVisitor
y Or from cmdline as a pseudoloader:
ƒ osgviewer myScene.osg.glsllint
Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 54
Demos!

55
osgshaders example
y The original OSG/GLSL example in C++
y Demonstrates multiple osg::Programs,
time-varying uniforms, multi-texture

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 56


glsl _simple.osg
glsl_simple.osg
y The first GLSL scene in a .osg file
y Block colors are uniforms distributed
around the scenegraph

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 57


glsl _confetti.osg
glsl_confetti.osg
y Demonstrates generic vertex attributes
and particle animation in a vertex shader

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 58


compactdisc .osg
compactdisc.osg
y A vertex-only shader using generic vertex
attributes

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 59


glsl _dataflag.osg
glsl_dataflag.osg
y Displays GLSL-internal values as ASCII
strings
y Drawn in one pass; no render-to-texture

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 60


3dataflags
y Multiple dataflag instances can show
different data

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 61


For more info

y http://openscenegraph.org/
y http://developer.3Dlabs.com/
y http://mew.cx/osg/
y http://sourceforge.net/projects/osgtoy/

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 62


Thank you!

Copyright © 2005, 3Dlabs, Inc. Ltd July 31, 2005 63

Вам также может понравиться