You are on page 1of 35

Shader Programming

CgFX, OpenGL 2.0


Michael Haller
2003
Outline
What is CgFX?
CgFX runtime
Production pipeline with CgFX
CgFX Tools set
OpenGL 2.0
What is CgFX?
CgFX (C for Graphics Effekt File)
Supports Microsoft .fx files
Cg plus:
Multi-pass
Hardware fallbacks (techniques)
Complete Hardware states
Tweakables
MS .fx (HLSL) plus:
DirectX8 and OpenGL
MAC and Linux
CgFX overview
Game I mage
Application (game, renderer, )
Typical Production Pipeline
DCC tool (Maya, Max, SoftImage, )
Digital Content Creation
Scene exporter plug-in
Scene manager
App Scene Manager
hard-coded to choose
at run-time
the appropriate ASM
shaders + state
for the hardware
Artists create models,
textures, maps, in
DCC tool of choice
ASM
Shaders
(HW1)
Programmers
write
assembly for
different
hardware
ASM
Shaders
(HW2)
Not the same!
DCC I mage
Models, Textures, Maps,
DCC tool (Maya, Max, SoftImage, )
Scene exporter plug-in
FX material plug-in
Application (game, renderer, )
FX runtime
Scene manager
CgFX Production Pipeline
For any FX, App Scene
Manager chooses
at run-time
the appropriate
technique
for the hardware
Artists assign
FX files
to scene objects
and tweak parameters
for each object
in real-time
FX
files
Programmers
and/or artists
write FX
effects
Models, Textures, Maps,
FX effects + parameters
Same Image
CgFX Example File - Structure
Global declarations
Tweakable declarations
Vertex shaders
Fragment shaders
Techniques to encapsulate shaders
Techniques
effect myEffectName {
technique PixelShaderVersion
{};
technique FixedFunctionVersion
{};
technique LowDetailVersion
{};
};
Passes
Each technique contains one or more passes
Each pass may contain a vertex program, a
fragment program, or both.
E.g.
Pass 0: Fixed-function pixel processing to output the
ambient color.
Pass 1: ps_1_1 fragment program
Pass 2: ps_2_0 fragment program
Typically, all passes of a technique use Cg or
assembly programs.
Render States
pass firstPass {
DepthWriteEnable = true;
AlphaBlendEnable = false;
MinFilter[ 0 ] = Linear;
MagFilter[ 0 ] = Linear;
MipFilter[ 0 ] = Linear;
// Pixel shader written in assembly
PixelShader = asm {
ps.1.1
tex t0;
mov r0, t0;
};
};
Variables and Semantics
Global and per-technique Cg-style variables
(passed as uniform parameters):
These variables can contain a user-defined
semantic, which helps applications provide the
correct data to the shader:
bool AlphaBlending = false;
float bumpHeight = 0.5f;
float4x4 myViewMatrix : ViewMatrix;
texture2D someTexture : DiffuseMap;
Annotations
Additionally, each variable can have an optional annotation.
An annotation describes a user interface element for
manipulating uniform variables:
float bumpHeight
<
string gui = slider;
float uimin = 0.0f;
float uimax = 1.0f;
float uistep = 0.1f;
> = 0.5f;
My first fx example (Blinn)
struct appdata {
float4 vPosition : POSITION;
float4 vNormal : NORMAL;
float4 vTexCoords : TEXCOORD0;
};
struct vpconn {
float4 vTexCoord0 : TEXCOORD0;
float4 vDiffuse : COLOR0;
float4 vPosition : POSITION;
float4 vSpecular : COLOR1;
};
My first fx example (Blinn) II
// un-tweakables
float4x4 worldView : WorldView;
float4x4 worldViewIT: WorldViewIT;
float4x4 worldViewProjection : WorldViewProjection;
[...]
// tweakables
float4 diffuse : DIFFUSE
= { 0.1f, 0.1f, 0.5f, 1.0f };
[...]
float4 lightPos : Position
<
string Object = "PointLight";
string Space = "World";
> = {100.0f, 100.0f, 100.0f, 0.0f};
My first fx example (Blinn) III
vpconn vs_blinnShading(appdata IN,
uniform float4x4 ModelViewProj,
uniform float4x4 ModelView,
uniform float4x4 ModelViewIT,
uniform float4x4 ViewIT,
uniform float4x4 View,
uniform float4 lightPos,
uniform float4 diffuse,
uniform float4 specular,
uniform float4 ambient) {
vpconn OUT;
[]
OUT.vDiffuse = diff_term;
OUT.vPosition = mul(ModelViewProj,IN.vPosition);
return OUT;
}
My first fx example (Blinn) IV
technique Blinn {
pass p0 {
Zenable = true;
ZWriteEnable = true;
CullMode = None;
VertexShader = compile vs_1_1 vs_blinnShading(
worldViewProjection, worldView,
worldViewIT, viewIT, view,
lightPos, diffuse,
specular, ambient);
}
}
Results (Blinn) I
Results (Blinn) II
Production Pipeline with CgFX
CgFX Tools Set
Integrated authoring in DCC apps:
3ds MAX 5.1
MAYA 4.5
XSI (CgFX Coming soon...)
NVB Exporter
CgFX Viewer
OpenGL ARB, DirectX8, DirectX9
3D Studio Max 5.1
stdmaterial <-> CgFX
Ability to select MAX scene lights and connect them to
.fx parameters
On the fly editing of shaders and auto-update of .fx
GUI
MAXSCRIPT support
Source Code
Integration in 3D Studio Max
CgFX Viewport
Manager
Intuitive artist
controls
(sliders, color pickers, etc.)
Supports .fx
file format
Multiple
Techniques
for fallbacks
Dynamic, shader-
specific GUI
Customizable Parameters are
specific to each effect
Bitmaps can be
swapped
Color and numeric
values can be
changed
Shader
changed by
selecting a
new fx file
NVB Exporter for 3ds max
Based on Pierre Terdimans 3ds max exporter
http://www.codercorner.com/Flexporter.htm
Exports Scene data
Mesh, materials, lights, camera, skinning, etc...
Exports CgFX materials
ICgFXDataBridge interface
Source code
CgFX Viewer
Scene graph GUI
.fx parameters edition
Error reporting for easy
.fx
file problem
identification
Runs OpenGL, DirectX8,
DirectX9
Switch between devices
at any point
The CgFX Viewer can be used as a production resource
and a code example for implementing CgFX
Main Application
Window
Connection
Editor
Window
CgFX Viewer II
How Does CgFX relate to Cg?
CgFX describes an entire effect Cg
implements a particular function required by
an effect
CgFX describes all the parameters (and their
meaning or semantics) that the app has to
provide automatic parameter discovery
CgFX can describe complex multi-pass effects
CgFX can handle multiple techniques
Hardware Shader Workflow
Designing Shaders and
Using Existing Shaders
Artist-Configurable
Parameters
Editing Shader
Parameters
Exporting Shader
Parameters to Game
Engine
Small Efficient Shaders
Multiple, narrowly-targeted shaders are more
efficient/faster than large all-purpose shaders
Several
shaders may
share similar
features and
lighting
models
OpenGL: Where do you want to go today?
OpenGL 2.0 Goals
Reduce the need for existing and future
extensions to OpenGL by replacing complexity
with programmability.
Backward compatibility to OpenGL 1.x
Address the needs of dynamic media
authoring and playback applications needed
by OpenML.
Status of Shading Language
Extensions to support the OpenGL Shading Language
Language
approved as ARB extensions in J une 2003
Language and extensions expected to be rolled into
OpenGL in 6 rolled into OpenGL in 6-12 months 12
months
That version of OpenGL will be called OpenGL 2.0
It will still be backwards compatible with 1.0-1.5
3Dlabs is shipping a preliminary implementation
Overview
Thanks!

You might also like