nvFX: A Scene and Material Effect Framework for OpenGL
Tristan Lorach - NVIDIA
What is an Effect ?
Application
Effect
runtime
Effect file
Shader func’s
Sampler-states
Parameters
Techniques
Passes
Render-states
Texture Samplers
Textures (D3D) GPU
Shaders
Render
states
… • Set params (1 by 1 )
• Validate passes (builds shaders)
• Bind textures/samplers
• Activate a Pass
Introduced in Cg (2002) and HLSLFx
nvFX features
Candidate to replace CgFx
Hosts many Shading Languages (GLSL, GLSLCompute, HLSL, Compute…)
Effect == container of Shader snippets
self sufficient Effects
Lower amount C++ specialized code for the effect
Intermediate resources (Render-Targets) can be defined
Flexible Shader-snippets linkage approach
Nesting of Effects
Example: Scene-Effects influences Material Effects
Add more features
New Pass-states; resources; Constant Buffers; namespace; GL Extensions…
Open-Source
Inside An nvFX Effect
nvFX file
Sampler-states Uniform
Parameters
Techniques
Passes New Pass-states
Texture Bind point
Render
state-groups
Shader Code Modules
GLSL
D3D
Open-CL
GLSL-Compute
DXCompute
Resources
(render targets)
Frame buffers
Constant
Buffers
Resources description
Compute
[shared] Memory
CUDA
Simple nvFX Example
#extension GL_ARB_separate_shader_objects : enable
GLSLShader {
#version 410 compatibility
…
}
uniform vec4 scaleBias : SCBIAS = {1,0,0,0};
Namespace Object {
GLSLShader VS {
layout(location=0) in vec4 Position;
void main() { … }
}
GLSLShader PS < myAnnot=“bar”; > {
layout(location=0) in vec3 v2fWorldNormal;
uniform sampler2D diffSampler;
Main() { … }
}
}
rasterization_state myRStates {
POLYGON_MODE = FILL;
…
}
sampler_state defaultSamplerState
{ TEXTURE_MIN_FILTER = LINEAR_MIPMAP_LINEAR;
TEXTURE_MAG_FILTER = LINEAR;
}
Resource2D diffTex {
samplerState = defaultSamplerState;
defaultFile = "gargoyleMossyDiffuse.dds";
}
Technique BasicTechnique {
Pass p1 < annot1=“Foo”; annot2=3; > {
rasterization_state = myRStates;
samplerResource(diffSampler) = { diffTex, 0 };
VertexProgram = Object::VS;
FragmentProgram = Object::PS;
FragmentProgram<“light”> = LightingImpl;
Uniform(attenuation) = 0.9;
CurrentTarget = backbuffer;
}
Technique mySubTechnique1 Off
Technique mySubTechnique2 On
}
nvFX Effect Integration
Application
nvFX
runtime
nvFX files Shader code
Techs/passes
…
Shader code
Techs/passes
nvFX files
Application
nvFX
runtime
Techs/passes
…
Techs/passes
Application
nvFX
runtime Pre-
compiled
shaders
C++
nvFX
Effects
nvFX files
Shader code Shader code
Techs/passes Techs/passes
…
Shader code Shader code
Pre-
compiled
shaders
Shader code
API Design
Front-End : parser (Bison)
Parses the effect
Does not parse the shader/compute
code that is inside !
Back-End : the library to build the
effect data
Used by the Front-End to create parsed
data
Used by the application to drive the
effects
Works on PC, Unix (OSX/Linux),
Android… even iOS
Front-End
(nvFX parser)
C++ Back-End
(nvFX runtime)
API
Application
Your
Front-End
Parser
OpenGL DX Compute
Your Gfx API layer
Custom Grammar
& Token
DEMOS
1
Example : HDR Rendering With Glow
Render
Skybox in HDR
Render The
Scene in HDR
Down-scale the
texture 2x, 4x
Blur texture
Horizontal
Then Vertical
Scene Effect
1
2
3
4
5
6
Material Effect 1
Metal Pass
Material Effect 2
Plastic pass
Scene Graph
Other ‘Effects’…
RGBA32F
Texture
(HDR)
Compositing:
tone-mapping
And radial Blur
7
RGBA
32F
Texture
Backbuffer
+
2 3
4 5 6 7 7
Example : Compute Post-Processing
Render
Skybox
Render The
Scene
Triggers
Compute
Kernel
Display result
As a fullscreen
Quad
Scene Effect passes
1
2
3
4
Material Effect 1
Metal Pass
Material Effect 2
Plastic pass
Scene Graph
Other ‘Effects’…
RGBA
Texture
Backbuffer
Fire (Navier-Stokes equations) Techniq
ue
Advect Color
Advect Velocity
Vorticity
Confinement
Emit (Gaussian ball)
Fire Up-force
Vel. divergence
Comp. pressure
Proj. Velocity
Proj. Vel. edges passes
Volume bound 1
Volume bound 2
Smoke Ray-cast
Water Ray-cast
Fire Ray-cast
Rasterize result
Simulation passes
Techniques
Volume depth Volume depth
Rasterize
4
1
2
3
Conclusion
Less code in Application
More flexibility
Consistency of Effect code. Helps for maintenance and creativity
Updated use of modern APIs. Good for performance
Open-Source approach
Easier to track bugs
Customize it… make it yours or get inspiration from it
First drop now available on https://github.com/tlorach/nvFX
Feedback Needed to improve it : [email protected]
More details from previous talks (GTC 2013, Siggraph NVIDIA talks)