Real-Time Shader Techniques Using RenderMonkey by pptfiles


									Real-Time Shader
Techniques Using

               Natasha Tatarchuk
  3D Application Research Group
               ATI Research, Inc.
Presentation Overview
•   Problems that RenderMonkey solves for

•   Shader creation using RenderMonkey
    development environment

•   Efficient HLSL shaders: tips and tricks
     – Compiling issues
     – Optimization strategies
     – Code structure pointers
RenderMonkey Addresses the Needs
of Real-Time Effect Developers
• Shaders are more than just assembly code
• Encapsulating shaders can be complex
     – Cannot deliver shader-based effects using a
       standard mechanism
•   Solve problems for shader development
     – Designing software on emerging hardware is
     – Lack of currently existing tools for full shader
     – Need a collaboration tool for artists and
       programmers for shader creation
•   You’re using it! >100, 000 downloads so far! 2
Making Your Shader
Development More Effective
•   Simplifies shader creation
•   Fast prototyping and debugging of new
    graphics algorithms
•   Helps you share graphics effects with other
    developers and artists
•   Easy integration of effects into existing
•   Quickly create new components using our
    flexible framework
•   A communication tool between artists and
Program Your Shaders Using
Our Intuitive, Convenient IDE

Creating a Shader-based
Effect Using RenderMonkey
1.   Setup all your effect parameters
        1. Variables
        2. Stream mapping
        3. Models and textures
        4. Rendering states
2.   Create your vertex and pixel shaders

3.   Link RenderMonkey parameter nodes to
     your shaders as necessary

4.   Compile and explore!
RenderMonkey Run-Time
Database Overview
•   Encapsulate all effect data in a single text file

•   Each Effect Workspace consists of:
     – Effect Group(s)
         •   Effect(s)
              – Pass(es)
                  •   Render State
                  •   Pixel Shader
                  •   Vertex Shader
                  •   Geometry
                  •   Textures

     – Variables, notes and stream mapping nodes
RenderMonkey Database Uses
XML File Format
•   Allows easy data representation
     – Industry standard
     – User-extensible
     – Parsers are readily available
• Describes all effect-related information
• Import and export easily from your
  native formats
     – Use our parser and run-time format
     – Write an exporter / importer plug-in (Example:
       Our Microsoft .FX exporter plug-in)
 Workspace View: Your
 Window into Our Database
• All effect data organized in
  a hierarchical Workspace
  tree view
• Visually identify node data
  types by their icons:

• Quickly identify invalid data
• Easy perusal of data values
  via automatic tooltips
Effect Group Nodes
• Group related effects in one
• Provides mechanism for
  dealing with a lot of effects
• Facilitate fallback versions
  of same effect
• How you group effects is
  entirely up to you
    – Ex: Group shaders by
      type, pick the first one that

    Effect Nodes
•   Encompass all information
    needed to implement a
    real-time visual effect
•   Composed of one or more
•   Inherit data and states from a
    Default Effect
     – Used to set a known starting state
       for all effects

Data Scope and Traversal
•   Store common effect data in the default
     – All other effects inherit data from it
     – Easy to share data from a common point
• Effects don’t inherit data from other effects in
  the workspace
• Variable scope is similar to C-style variable
• Data validation occurs upward through
  passes in the effect then upward through
  passes in the default effect
Pass Nodes
• Every pass is a draw call
• Passes inherit data from previous passes
  within the effect
     – First pass inherits from default effect

•   A typical pass may contain:
     –   A vertex and a pixel shader
     –   Render state block
     –   Texture objects
     –   Geometry model reference (required)
     –   Stream mapping reference (required)
     –   Other nodes (variables, etc)
Use Different Geometry in Passes

Variable Nodes
•   Parameters to your shaders
     – More intuitive way of dealing with constant store
• Give shader constants meaningful names
  and types
• Interactively manipulate shader constants
  using convenient GUI widgets and see
  changes in real-time
• Supported variable types:
     - Matrices        - Vectors       - Booleans
     - Scalars         - Colors
     - Strings (Notes)   (not available as shader constants)
Pre-defined Variables Help You
Set Up Your Shaders Quickly
•   RenderMonkey IDE calculates their values at

•   Provide a set of commonly used parameters:
     –   View projection matrix
     –   View matrix
     –   Inverse view matrix
     –   Projection matrix
     –   View direction vector
     –   View position vector
     –   Time, time cycle period
     –   cos_time, sin_time, tan_time
            •   and more…

Edit Shader Parameters Using
GUI Widgets via Editor plug-ins
•   Utilize knowledge of the
    variable type

•   Edit variables with
    custom widgets: easy to
    create your own

Communicate Your Ideas With Notes

Simplified Stream Mapping
• Create a stream map
  node at any point in
  the workspace
• Share stream mapping
     – Multiple references to a single stream
       mapping node can be created throughout
       the workspace
•   Each pass must have its own stream
    mapping reference
Specialized Shader Editors
•   Tabbed view allows editing multiple passes
    in the effect
     – Easy switching between vertex and pixel shaders
       in the pass
     – Full support for standard Windows editor

•   Automatic syntax coloring of shader code
     – Separate syntax rules for each shader model

•   Type your code, compile and see instant
    Assembly Shader Editor
•   Link RenderMonkey
    variable nodes to
    constant store

•   Syntax coloring
•   Automatically
    maintains count of
    ALU and texture ops
    in your shader
•   Export shader code
    to text files
Linking Variable Nodes to
Constant Store Registers is Easy
•   Use constant store editor

•   Bind a constant storage register to a
    variable from the effect workspace

•   Preview the incoming values:

    HLSL Shader Editor
•   Simple interface links
    RenderMonkey nodes
    to HLSL variables and
     – Collapsible UI
       provides more text
       real estate
•   Syntax coloring
•   Control target and entry
    points for your shaders
•   Preview resulting
    assembly instruction

Easily View Disassembly
•   Disassembly output is
    updated every time a
    shader is compiled

•   Displays count for ALU
    and texture ops, as
    well as the limits for
    the selected target

•   Save resulting
    code into text file
Integrated Compile Time Error

•   Double-click the
    error to highlight
    offending line of

    Interactively Preview Your
    Effects in the Viewer Plug-in
•   DirectX® 9.0 preview
     – HAL / REF
     – Rendering on demand
       for REF
•   Customize the preview:
     –   Trackball mode selection
     –   Settings for camera
     –   Clear color
     –   Preset views
     –   Optionally fit a model into given viewport
     –   Display axes triad / bounding box for models
    Visualize Shader Interaction
•   View output of
    each individual
    pass in separate

•   Incrementally
    visualize the
    build-up of the
    final effect

•   View contents
    of renderable
Setting Up Rendering States
•   Modify any render state
    within a particular pass

•   Render states are
     – From previous passes
       in the effect
     – From the default effect

•   Great for exploring the
    results of changing a
    render state – a useful
    learning tool
    Using Textures
• Textures can be shared
  between different effects
• Support for standard
  texture types
• Renderable texture
• Texture objects belong to a
  pass node
     – Reference a texture variable to sample from
     – Store all related texture and sampler states
     – Maps directly to HLSL samplers

Texture and Sampler State Editing
•   Specify texture and
    sampler state values
    (filtering, clamping, etc)
    for each texture node
    within a pass

•   Texture and sampler
    states are bundled
    together in one editor

•   State changes modify
    rendered output in real

Rendering to a Texture
•   Direct output of one or more
    passes to a texture map

•   Special texture variable
     – Renderable Texture

•   Direct pass output to a texture
     – Use Render Target node
       to link to a Renderable Texture

•   Sample in other passes as any
    other texture object
Preview Contents of Renderable
Texture         Render

             Final Effect

Develop Shaders With Your
• Use the Artist Editor interface to explore
• View workspace using Art tab
     – Only view data relevant to the artists
     – Programmers can select which data is
•   Our widgets provide the look and feel of
    user interfaces artists are familiar with

Artist Editor Interface

Efficient HLSL shaders: tips
and tricks
•   Compiling issues

•   Optimization strategies

•   Code structure pointers

    Why use HLSL?
•    Instant readability of your shader code
•    Better code re-use and maintainability
•    Optimization
•    Faster, easier effect development
     – Iterate on your shader design quicker →
       better looking effects!
     – Conveniently manage shader permutations
•    Industry standard which will run on
     cards from any vendor

    Compile Targets
•    HLSL is independent of selected compile

•    But having an HLSL shader doesn’t mean it
     will automatically compile on any hardware!

•    Currently supported compile targets:
     – vs_1_1, vs_2_0 and vs_2_sw
     – ps_1_1/2/3/4, ps_2_0/a/sw

•    Compilation is done by a D3DX component

Compilation Failure
•   The obvious: program errors
    – bad syntax, etc

•   Compile target specific reasons – your
    shader is too complex for the selected
    – Not enough resources in the selected target
    – Lack of capability in the target

•   Compiler provides useful messages

    Use Disassembly for Hints

•   Helps to understand how compile
    targets affect code generation

•   Useful when compiling “down” to an
    older compile target

•   Figure out how HLSL instructions map to

Optimizing HLSL Shaders
•   Don’t forget you are running on a vector
•   Do your computations at the most
    efficient frequency
     – Don’t do something per-pixel that you can do
     – Don’t perform computation in a shader that you
       can precompute in the app

•   Use HLSL intrinsic functions
     – Helps hardware to optimize your shaders
     – Know your intrinsics and how they map to asm,
       especially asm modifiers

    Know What Hardware You
    Are Targeting
•    The HLSL code you write is not limited by
     the compile target you choose
•    You can always use loops, subroutines, if-
     else statements
     – If not natively supported in the selected compile
       target, the compiler will still try to generate
•    But! Code generation is dependent upon
     compile target
•    Use appropriate data types to improve
     instruction count
Using If Statement in HLSL
•   Can have large performance implications
     – Lack of branching support in most asm
     – Both sides of an ‘if’ statement will be
     – The output is chosen based on which side of
       the ‘if’ would have been taken

•   Optimization is different than in the CPU
    programming world

Example of Using If in Vs_1_1
If ( Threshold > 0.0 )
     Out.Position = Value1;
     Out.Position = Value2;

  generates following assembly output:
         // calculate lerp value based on Value > 0
         mov r1.w, c2.x
         slt r0.w, c3.x, r1.w

         // lerp between Value1 and Value2
         mov r7, -c1
         add r2, r7, c0
         mad oPos, r0.w, r2, c1

 Code Permutations Via Compilation
  static const
static const boolbool bAnimate = false;
                  bAnimate = false;             vs_1_1
  VS_OUTPUT vs_main( float4 Pos: POSITION,      dcl_position v0
                      float2 Tex: TEXCOORD0 )   dcl_texcoord v1
     VS_OUTPUT Out = (VS_OUTPUT) 0;
                                                mul r0, v0.y, c1
     Out.Pos = mul( view_proj_matrix, Pos );    mad r0, c0, v0.x, r0
     if ( bAnimate )                            mad r0, c2, v0.z, r0
          Out.Tex.x = Tex.x + time / 2;         mad oPos, c3, v0.w, r0
          Out.Tex.y = Tex.y - time / 2;         mov oT0.xy, v1
          Out.Tex = Tex;
     return Out;
                                                               5 instructions

  bool bAnimate false;
 const bool= bAnimate = false;                  vs_1_1
                                                def c6, 0.5, 0, 0, 0
  VS_OUTPUT vs_main( float4 Pos: POSITION,
                                                dcl_position v0

                                                                8 instructions
                      float2 Tex: TEXCOORD0 )   dcl_texcoord v1
  {                                             mul r0, v0.y, c1
     VS_OUTPUT Out = (VS_OUTPUT) 0;             mad r0, c0, v0.x, r0
     Out.Pos = mul( view_proj_matrix, Pos );    mov r1.w, c4.x
     if ( bAnimate )
                                                mul r1.x, r1.w, c6.x
          Out.Tex.x = Tex.x + time / 2;         mad r0, c2, v0.z, r0
          Out.Tex.y = Tex.y - time / 2;         mov r1.y, -r1.x
     }                                          mad oPos, c3, v0.w, r0
     else                                       mad oT0.xy, c5.x, r1, v1
          Out.Tex = Tex;
     return Out;

Scalar and Vector Data Types
•   Scalar data types are not all natively
    supported in hardware
    – Integers are emulated on float hardware
•   Not all targets have native half and
    none currently have double
•   Can apply swizzles to vector types
            float2 vec = pos.xy
    – But not all targets have fully flexible
    – Acquaint yourself with the native swizzles
Integer Data Type

•   Added to make relative addressing more

•   Using floats for addressing purposes
    without defined truncation rules can result
    in incorrect access to arrays.

•   All inputs used as integers should be
    explicitly defined as int in your shader

Example of Integer Data
Type Usage
•   Matrix palette indices for skinning
     – Declaring variable as an int is a ‘free’
       operation => no truncation occurs
     – Using a float and casting it to an int or using
       directly => truncation will happen
         Out.Position = mul( inPos, World[Index]);
// Index declared as float     // Index declared as int
frc r0.w, r1.w                 mul r0.w, c60.x, r1.w
add r2.w, -r0.w, r1.w          mova a0.x, r0.w
mul r9.w, r2.w, c61.x          m4x4 oPos, v0, c0[a0.x]
mova a0.x, r9.w
m4x4 oPos, v0, c0[a0.x]

      Code generated with float index vs integer index
Shader Examples and Optimizations

 •   Multi-tone car paint effect

 •   Translucent iridescent effect

 •   Classic überlight example

•   RenderMonkey IDE is a powerful, intuitive
    environment for developing shaders

•   Prototype your shaders quickly

•   Interactively explore all parameters for
    your shaders using convenient GUI

•   Develop shaders with your artists!

To top