Title: RealTime Shader Techniques Using RenderMonkeyTM
1Real-Time Shader Techniques Using RenderMonkeyTM
- Natasha Tatarchuk
- 3D Application Research Group
- ATI Research, Inc.
2Presentation 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
3RenderMonkey 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 - Youre using it! gt100, 000 downloads so far!
2
4Making 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
5Program Your Shaders Using Our Intuitive,
Convenient IDE
4
6Creating a Shader-based Effect Using RenderMonkey
- Setup all your effect parameters
- Variables
- Stream mapping
- Models and textures
- Rendering states
- Create your vertex and pixel shaders
- Link RenderMonkey parameter nodes to your shaders
as necessary - Compile and explore!
5
7RenderMonkey 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
8RenderMonkey 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
9Workspace View Your Window into Our Database
- All effect data organized in a hierarchical
Workspacetree view - Visually identify node data types by their
icons - Quickly identify invalid data
- Easy perusal of data valuesvia automatic tooltips
8
10Effect 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
11Effect 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
12Data 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 dont 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
13Pass 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
14Use Different Geometry in Passes
13
15Variable 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
16Pre-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
17Edit 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
18Communicate Your Ideas With Notes
17
19Simplified 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
20Specialized Shader Editors
- Tabbed view allows editing multiple passesin 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
21Assembly 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
22Linking 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
23HLSL 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
24Easily View Disassembly Output
- Disassembly output is updated every time a
shader is compiled - Displays count for ALUand texture ops, as well
as the limits forthe selected target - Save resulting assembly code into text file
23
25Integrated Compile Time Error Reporting
- Double-click the error to highlight offending
line of code
24
26Interactively 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
27Visualize 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
28Setting 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
29Using Textures
- Textures can be shared between different effects
- Support for standard texture types
- Renderable texture support
- Texture objects belong to apass node
- Reference a texture variable to sample from
- Store all related texture and sampler states
- Maps directly to HLSL samplers
28
30Texture and Sampler State Editing
- Specify texture and sampler state values
(filtering, clamping, etc) for each texture
node within a pass - Texture and samplerstates are bundled together
in one editor - State changes modify rendered output in real time
29
31Rendering 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 anyother texture object
30
32Preview Contents of Renderable Texture
Render Target Contents
Final Effect
31
33Develop 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
34Artist Editor Interface
33
35Efficient HLSL shaders tips and tricks
- Compiling issues
- Optimization strategies
- Code structure pointers
34
36Why 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
37Compile Targets
- HLSL is independent of selected compile target
- But having an HLSL shader doesnt 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
38Compilation 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
39Use 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
40Optimizing HLSL Shaders
- Dont forget you are running on a vector
processor - Do your computations at the most efficient
frequency - Dont do something per-pixel that you can do
per-vertex - Dont 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
41Know 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
42Using 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
43Example of Using If in Vs_1_1
If ( Threshold gt 0.0 ) Out.Position
Value1else Out.Position Value2
generates following assembly output
// calculate lerp value based on Value gt 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
44Code Permutations Via Compilation
static const bool bAnimate false
static const bool bAnimate false VS_OUTPUT
vs_main( float4 Pos POSITION,
float2 Tex TEXCOORD0 ) VS_OUTPUT Out
(VS_OUTPUT) 0 Out.Pos mul(
view_proj_matrix, Pos ) if ( bAnimate )
Out.Tex.x Tex.x time / 2
Out.Tex.y Tex.y - time / 2 else
Out.Tex Tex return Out
vs_1_1 dcl_position v0 dcl_texcoord v1 mul r0,
v0.y, c1 mad r0, c0, v0.x, r0 mad r0, c2, v0.z,
r0 mad oPos, c3, v0.w, r0 mov oT0.xy, v1
5 instructions
bool bAnimate false VS_OUTPUT vs_main(
float4 Pos POSITION, float2
Tex TEXCOORD0 ) VS_OUTPUT Out (VS_OUTPUT)
0 Out.Pos mul( view_proj_matrix, Pos )
if ( bAnimate ) Out.Tex.x Tex.x
time / 2 Out.Tex.y Tex.y - time / 2
else Out.Tex Tex return Out
vs_1_1 def c6, 0.5, 0, 0, 0dcl_position v0
dcl_texcoord v1 mul r0, v0.y, c1 mad r0, c0,
v0.x, r0 mov r1.w, c4.x mul r1.x, r1.w, c6.x mad
r0, c2, v0.z, r0 mov r1.y, -r1.x mad oPos, c3,
v0.w, r0 mad oT0.xy, c5.x, r1, v1
const bool bAnimate false
8 instructions
43
45Scalar 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
46Integer 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
47Example of Integer Data Type Usage
- Matrix palette indices for skinning
- Declaring variable as an int is a free
operation gt no truncation occurs - Using a float and casting it to an int or using
directly gt truncation will happen
46
48Shader Examples and Optimizations
- Multi-tone car paint effect
- Translucent iridescent effect
- Classic überlight example
- http//www.ati.com/developer/gdce/Tatarchuk-GDCE2
003.pps
47
49Conclusion
- 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