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

Shader Programming CgFX, OpenGL 2.0

Shader Programming CgFX, OpenGL 2.0 Michael Haller 2003

Michael Haller

2003

Outline
Outline

What is CgFX? CgFX runtime Production pipeline with CgFX CgFX Tools set OpenGL 2.0

What is CgFX?
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
CgFX overview
CgFX overview
Typical Production Pipeline
Typical Production Pipeline
Programmers write assembly for different hardware ASM Shaders (HW1)
Programmers
write
assembly for
different
hardware
ASM
Shaders
(HW1)
Artists create models, textures, maps, … in DCC tool of choice DCC Image
Artists create models,
textures, maps, … in
DCC tool of choice
DCC Image
textures, maps, … in DCC tool of choice DCC Image Not the same! DCC tool (Maya,
textures, maps, … in DCC tool of choice DCC Image Not the same! DCC tool (Maya,

Not the same!

DCC tool (Maya, Max, SoftImage, …)

Digital Content Creation

Scene exporter plug-in

DCC tool (Maya, Max, SoftImage, …) Digital Content Creation Scene exporter plug-in
…) Digital Content Creation Scene exporter plug-in Models, Textur es, Maps, … Game Image Scene manager

Models, Textures, Maps, …

Game Image Scene manager ASM Shaders (HW2) Application (game, renderer, …) App Scene Manager hard-coded
Game Image
Scene manager
ASM
Shaders
(HW2)
Application (game, renderer, …)
App Scene Manager
hard-coded to choose
at run-time
the appropriate ASM
shaders + state
for the hardware
CgFX Production Pipeline
CgFX Production Pipeline
Programmers and/or artists write FX effects FX files
Programmers
and/or artists
write FX
effects
FX
files
Artists assign FX files to scene objects and tweak parameters for each object in real-time
Artists assign
FX files
to scene objects
and tweak parameters
for each object
in real-time

DCC tool (Maya, Max, SoftImage, …)

FX material plug-in

DCC tool (Maya, Max, SoftImage, …) FX material plug-in Scene exporter plug-in

Scene exporter plug-in

…) FX material plug-in Scene exporter plug-in Models, Textures, Maps, FX effects + parameters Same Image

Models, Textures, Maps, FX effects + parameters

Same Image
Same Image

Scene manager

Scene manager FX runtime Application (game, renderer, …)

FX runtime

Scene manager FX runtime Application (game, renderer, …)

Application (game, renderer, …)

For any FX, App Scene Manager chooses at run-time the appropriate technique for the hardware
For any FX, App Scene
Manager chooses
at run-time
the appropriate
technique
for the hardware
CgFX Example File - Structure
CgFX Example File - Structure

Global declarations Tweakable declarations Vertex shaders Fragment shaders Techniques to encapsulate shaders

Techniques
Techniques

effect myEffectName { technique PixelShaderVersion {…}; technique FixedFunctionVersion {…}; technique LowDetailVersion {…};

};

Passes
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
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
Variables and Semantics

Global and per-technique Cg-style variables (passed as uniform parameters):

bool AlphaBlending = false; float bumpHeight = 0.5f;

These variables can contain a user-defined semantic, which helps applications provide the correct data to the shader:

float4x4 myViewMatrix : ViewMatrix; texture2D someTexture : DiffuseMap;

Annotations
Annotations

Additionally, each variable can have an optional annotation. An annotation describes a user interface element for manipulating uniform variables:

user interface element for manipulating uniform variables: float bumpHeight < string gui = “slider”; float

float bumpHeight

<

string gui = “slider”; float uimin = 0.0f; float uimax = 1.0f; float uistep = 0.1f; > = 0.5f;

My first fx example (Blinn)
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
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
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
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) I
Results (Blinn) I
Results (Blinn) I
Results (Blinn) II
Results (Blinn) II
Results (Blinn) II
Production Pipeline with CgFX
Production Pipeline with CgFX
Production Pipeline with CgFX
CgFX Tools Set
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
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

them to .fx parameters On the fly editing of shaders and auto-update of .fx GUI MAXSCRIPT
Integration in 3D Studio Max
Integration in 3D Studio Max
Integration in 3D Studio Max
Integration in 3D Studio Max
CgFX Viewport Manager Intuitive artist controls (sliders, color pickers, etc.)
CgFX Viewport
Manager
Intuitive artist
controls
(sliders, color pickers, etc.)
Intuitive artist controls (sliders, color pickers, etc.) Multiple Techniques for fallbacks Supports .fx file

Multiple Techniques for fallbacks

Supports .fx file format

Dynamic, shader- specific GUI

Shader changed by selecting a new fx file Color and numeric values can be changed
Shader
changed by
selecting a
new fx file
Color and numeric
values can be
changed
a new fx file Color and numeric values can be changed Customizable Parameters are specific to

Customizable Parameters are specific to each effect

Bitmaps can be swapped

NVB Exporter for 3ds max
NVB Exporter for 3ds max

Based on Pierre Terdiman’s 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
CgFX Viewer

Scene graph GUI .fx parameters edition Error reporting for easy .fx file problem identification Runs OpenGL, DirectX8,

DirectX9

for easy .fx file problem identification Runs OpenGL, DirectX8, DirectX9 Switch between devices at any point

Switch between devices at any point

CgFX Viewer II
CgFX Viewer II
CgFX Viewer II Main Application Window Connection Editor Window The CgFX Viewer can be used as

Main Application Window

Connection

Editor

Window

The CgFX Viewer can be used as a production resource and a code example for implementing CgFX

How Does CgFX relate to Cg?
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 •
Hardware Shader Workflow • Designing Shaders and Using Existing Shaders • Artist-Configurable Parameters •
Hardware Shader Workflow
• Designing Shaders and
Using Existing Shaders
• Artist-Configurable
Parameters
• Editing Shader
Parameters
• Exporting Shader
Parameters to Game
Engine
• Artist-Configurable Parameters • Editing Shader Parameters • Exporting Shader Parameters to Game Engine
Multiple, narrowly-targeted shaders are more efficient/faster than large all-purpose shaders Several shaders may share

Multiple, narrowly-targeted shaders are more efficient/faster than large all-purpose shaders

Several shaders may share similar features and lighting models

Small Efficient Shaders

OpenGL: Where do you want to go today?

OpenGL: Where do you want to go today?

OpenGL: Where do you want to go today?
OpenGL 2.0 Goals
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
Status of Shading Language

Extensions to support the OpenGL Shading Language Language

approved as ARB extensions in June 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
Overview
Overview
Thanks!
Thanks!
Thanks!