Real-Time Shader Techniques Using RenderMonkey by pptfiles

VIEWS: 17 PAGES: 49

									Real-Time Shader
Techniques Using
RenderMonkeyTM

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

•   Shader creation using RenderMonkey
    development environment

•   Efficient HLSL shaders: tips and tricks
     – Compiling issues
     – Optimization strategies
     – Code structure pointers
                                              1
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
       difficult
     – Lack of currently existing tools for full shader
       development
     – 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
    applications
•   Quickly create new components using our
    flexible framework
•   A communication tool between artists and
    developers
                                                  3
Program Your Shaders Using
Our Intuitive, Convenient IDE




                                4
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!
                                            5
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
                                                    6
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)
                                                        7
 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
                                  8
Effect Group Nodes
• Group related effects in one
  container
• 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
      validates

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


                                            10
Data Scope and Traversal
•   Store common effect data in the default
    effect:
     – 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
  scope
• Data validation occurs upward through
  passes in the effect then upward through
  passes in the default effect
                                                     11
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)
                                                 12
Use Different Geometry in Passes




                               13
Variable Nodes
•   Parameters to your shaders
     – More intuitive way of dealing with constant store
       registers
• 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)
                                                               14
Pre-defined Variables Help You
Set Up Your Shaders Quickly
•   RenderMonkey IDE calculates their values at
    run-time

•   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…


                                                  15
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




                                  16
Communicate Your Ideas With Notes




                               17
Simplified Stream Mapping
• Create a stream map
  node at any point in
  the workspace
• Share stream mapping
  nodes
     – Multiple references to a single stream
       mapping node can be created throughout
       the workspace
•   Each pass must have its own stream
    mapping reference
                                                18
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
       functionality

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

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

•   Syntax coloring
•   Automatically
    maintains count of
    ALU and texture ops
    in your shader
•   Export shader code
    to text files
                             20
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:




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

                               22
Easily View Disassembly
Output
•   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
    assembly
    code into text file
                             23
Integrated Compile Time Error
Reporting

•   Double-click the
    error to highlight
    offending line of
    code




                                24
    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
                                                        25
    Visualize Shader Interaction
•   View output of
    each individual
    pass in separate
    viewports

•   Incrementally
    visualize the
    build-up of the
    final effect

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

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


•   Great for exploring the
    results of changing a
    render state – a useful
    learning tool
                                 27
    Using Textures
• Textures can be shared
  between different effects
• Support for standard
  texture types
• Renderable texture
  support
• 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

                                                      28
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
    time

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

•   Special texture variable
    type:
     – 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
                                         30
Preview Contents of Renderable
Texture         Render
                    Target
                   Contents

             Final Effect




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

                                                32
Artist Editor Interface




                          33
Efficient HLSL shaders: tips
and tricks
•   Compiling issues

•   Optimization strategies

•   Code structure pointers




                               34
    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


                                                 35
    Compile Targets
•    HLSL is independent of selected compile
     target

•    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

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

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

•   Compiler provides useful messages


                                                37
    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
    assembly


                                         38
Optimizing HLSL Shaders
•   Don’t forget you are running on a vector
    processor
•   Do your computations at the most
    efficient frequency
     – Don’t do something per-pixel that you can do
       per-vertex
     – 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

                                                        39
    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
       code.
•    But! Code generation is dependent upon
     compile target
•    Use appropriate data types to improve
     instruction count
                                                           40
Using If Statement in HLSL
•   Can have large performance implications
     – Lack of branching support in most asm
       models
     – Both sides of an ‘if’ statement will be
       executed
     – The output is chosen based on which side of
       the ‘if’ would have been taken

•   Optimization is different than in the CPU
    programming world

                                                     41
Example of Using If in Vs_1_1
If ( Threshold > 0.0 )
     Out.Position = Value1;
else
     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




                                                      42
 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
     }
     else
          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;
  }

                                                                            43
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
      swizzles
    – Acquaint yourself with the native swizzles
                                                   44
Integer Data Type

•   Added to make relative addressing more
    efficient

•   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

                                                  45
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
                                                          46
Shader Examples and Optimizations

 •   Multi-tone car paint effect


 •   Translucent iridescent effect


 •   Classic überlight example

     http://www.ati.com/developer/gdce/Tatarc
     huk-GDCE2003.pps
                                                47
Conclusion

•   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!
                                                48

								
To top