open gl (ebook) shader programming - cgfx opengl 2.0.pdf

Upload: luissustaita

Post on 02-Jun-2018

245 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/11/2019 Open Gl (Ebook) Shader Programming - Cgfx Opengl 2.0.pdf

    1/35

    Shader ProgrammingCgFX, OpenGL 2.0

    Michael Haller

    2003

  • 8/11/2019 Open Gl (Ebook) Shader Programming - Cgfx Opengl 2.0.pdf

    2/35

    Outline

    What is CgFX? CgFX runtime

    Production pipeline with CgFX

    CgFX Tools set

    OpenGL 2.0

  • 8/11/2019 Open Gl (Ebook) Shader Programming - Cgfx Opengl 2.0.pdf

    3/35

    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

  • 8/11/2019 Open Gl (Ebook) Shader Programming - Cgfx Opengl 2.0.pdf

    4/35

    CgFX overview

  • 8/11/2019 Open Gl (Ebook) Shader Programming - Cgfx Opengl 2.0.pdf

    5/35

    Game Image

    Application (game, renderer, )

    Typical Production Pipeline

    DCC tool (Maya, Max, Soft Image, )

    Digital Content Creation

    Scene exporter plug-in

    Scene manager

    App Scene Managerhard-coded to choose

    at run-time

    the appropriate ASM

    shaders + state

    for the hardware

    Artists create models,

    textures, maps, in

    DCC tool of choice

    ASMShaders

    (HW1)

    Programmers

    write

    assembly for

    different

    hardware

    ASM

    Shaders

    (HW2)

    Not the same!

    DCC Image

    Models, Textures, Maps,

  • 8/11/2019 Open Gl (Ebook) Shader Programming - Cgfx Opengl 2.0.pdf

    6/35

    DCC tool (Maya, Max, Soft Image, )

    Scene exporter plug-in

    FX material plug-in

    Application (game, renderer, )

    FX runtime

    Scene manager

    CgFX Production Pipeline

    For any FX, App SceneManager chooses

    at run-time

    the appropriate

    technique

    for the hardware

    Artists assign

    FX files

    to scene objects

    and tweak parameters

    for each objectin real-time

    FX

    files

    Programmers

    and/or artists

    wri te FX

    effects

    Models, Textures, Maps,FX effects + parameters

    Same Image

  • 8/11/2019 Open Gl (Ebook) Shader Programming - Cgfx Opengl 2.0.pdf

    7/35

    CgFX Example File - Structure

    Global declarations

    Tweakable declarations

    Vertex shaders

    Fragment shaders Techniques to encapsulate shaders

  • 8/11/2019 Open Gl (Ebook) Shader Programming - Cgfx Opengl 2.0.pdf

    8/35

    Techniques

    effect myEffectName {technique PixelShaderVersion

    {};

    technique FixedFunctionVersion{};

    technique LowDetailVersion

    {};

    };

  • 8/11/2019 Open Gl (Ebook) Shader Programming - Cgfx Opengl 2.0.pdf

    9/35

    Passes

    Each technique contains one or more passes

    Each pass may contain a vertex program, afragment 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.

  • 8/11/2019 Open Gl (Ebook) Shader Programming - Cgfx Opengl 2.0.pdf

    10/35

    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;

    };

    };

  • 8/11/2019 Open Gl (Ebook) Shader Programming - Cgfx Opengl 2.0.pdf

    11/35

    Variables and Semantics

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

    These variables can contain a user-definedsemantic, which helps applications provide the

    correct data to the shader:

    bool AlphaBlending = false;

    float bumpHeight = 0.5f;

    float4x4 myViewMatrix : ViewMatrix;

    texture2D someTexture : DiffuseMap;

  • 8/11/2019 Open Gl (Ebook) Shader Programming - Cgfx Opengl 2.0.pdf

    12/35

    Annotations

    Additionally, each variable can have an optional annotation.

    An annotation describes a user interface element formanipulating uniform variables:

    float bumpHeight

    = 0.5f;

  • 8/11/2019 Open Gl (Ebook) Shader Programming - Cgfx Opengl 2.0.pdf

    13/35

    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;

    };

  • 8/11/2019 Open Gl (Ebook) Shader Programming - Cgfx Opengl 2.0.pdf

    14/35

    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

    = {100.0f, 100.0f, 100.0f, 0.0f};

  • 8/11/2019 Open Gl (Ebook) Shader Programming - Cgfx Opengl 2.0.pdf

    15/35

    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;

    }

  • 8/11/2019 Open Gl (Ebook) Shader Programming - Cgfx Opengl 2.0.pdf

    16/35

    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);

    }

    }

  • 8/11/2019 Open Gl (Ebook) Shader Programming - Cgfx Opengl 2.0.pdf

    17/35

    Results (Blinn) I

  • 8/11/2019 Open Gl (Ebook) Shader Programming - Cgfx Opengl 2.0.pdf

    18/35

    Results (Blinn) II

  • 8/11/2019 Open Gl (Ebook) Shader Programming - Cgfx Opengl 2.0.pdf

    19/35

    Production Pipeline with CgFX

  • 8/11/2019 Open Gl (Ebook) Shader Programming - Cgfx Opengl 2.0.pdf

    20/35

    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

  • 8/11/2019 Open Gl (Ebook) Shader Programming - Cgfx Opengl 2.0.pdf

    21/35

    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 .fxGUI

    MAXSCRIPT support

    Source Code

  • 8/11/2019 Open Gl (Ebook) Shader Programming - Cgfx Opengl 2.0.pdf

    22/35

    Integration in 3D Studio Max

  • 8/11/2019 Open Gl (Ebook) Shader Programming - Cgfx Opengl 2.0.pdf

    23/35

    CgFX Viewport

    Manager

    Intuitive artist

    controls(sliders, color pickers, etc.)

    Supports .fxfile format

    Multiple

    Techniques

    for fallbacks

    Dynamic, shader-

    specific GUI

  • 8/11/2019 Open Gl (Ebook) Shader Programming - Cgfx Opengl 2.0.pdf

    24/35

    Customizable Parameters are

    specific to each effectBitmaps can be

    swapped

    Color and numericvalues can be

    changed

    Shader

    changed by

    selecting anew fx file

  • 8/11/2019 Open Gl (Ebook) Shader Programming - Cgfx Opengl 2.0.pdf

    25/35

    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

  • 8/11/2019 Open Gl (Ebook) Shader Programming - Cgfx Opengl 2.0.pdf

    26/35

    CgFX Viewer

    Scene graph GUI

    .fx parameters edition Error reporting for easy

    .fx

    file problemidentification

    Runs OpenGL, DirectX8,DirectX9

    Switch between devices

    at any point

  • 8/11/2019 Open Gl (Ebook) Shader Programming - Cgfx Opengl 2.0.pdf

    27/35

    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

  • 8/11/2019 Open Gl (Ebook) Shader Programming - Cgfx Opengl 2.0.pdf

    28/35

    How Does CgFX relate to Cg?

    CgFX describes an entire effect Cg

    implements a particular function required byan effect

    CgFX describes all the parameters (and their

    meaning or semantics) that the app has toprovide automatic parameter discovery

    CgFX can describe complex multi-pass effects

    CgFX can handle multiple techniques

  • 8/11/2019 Open Gl (Ebook) Shader Programming - Cgfx Opengl 2.0.pdf

    29/35

    Hardware Shader Workflow

    Designing Shaders and

    Using Existing Shaders

    Artist-Configurable

    Parameters

    Editing Shader

    Parameters

    Exporting Shader

    Parameters to Game

    Engine

  • 8/11/2019 Open Gl (Ebook) Shader Programming - Cgfx Opengl 2.0.pdf

    30/35

    Small Efficient Shaders

    Multiple, narrowly-targeted shaders are more

    efficient/faster than large all-purpose shaders

    Several

    shaders may

    share similarfeatures and

    lighting

    models

  • 8/11/2019 Open Gl (Ebook) Shader Programming - Cgfx Opengl 2.0.pdf

    31/35

    OpenGL: Where do you want to go today?

  • 8/11/2019 Open Gl (Ebook) Shader Programming - Cgfx Opengl 2.0.pdf

    32/35

    OpenGL 2.0 Goals

    Reduce the need for existing and future

    extensions to OpenGL by replacing complexitywith programmability.

    Backward compatibility to OpenGL 1.x

    Address the needs of dynamic mediaauthoring and playback applications neededby OpenML.

  • 8/11/2019 Open Gl (Ebook) Shader Programming - Cgfx Opengl 2.0.pdf

    33/35

    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 intoOpenGL 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

  • 8/11/2019 Open Gl (Ebook) Shader Programming - Cgfx Opengl 2.0.pdf

    34/35

    Overview

    h k !

  • 8/11/2019 Open Gl (Ebook) Shader Programming - Cgfx Opengl 2.0.pdf

    35/35

    hanks!