A HLSL Primer for Developers By Wolfgang F. Engel June 13th, 2003.

Slides:



Advertisements
Similar presentations
An Optimized Soft Shadow Volume Algorithm with Real-Time Performance Ulf Assarsson 1, Michael Dougherty 2, Michael Mounier 2, and Tomas Akenine-Möller.
Advertisements

Perspective aperture ygyg yryr n zgzg y s = y g (n/z g ) ysys y s = y r (n/z r ) zrzr.
Normal Map Compression with ATI 3Dc™ Jonathan Zarge ATI Research Inc.
GLSL Basics Discussion Lecture for CS 418 Spring 2015 TA: Zhicheng Yan, Sushma S Kini, Mary Pietrowicz.
Understanding the graphics pipeline Lecture 2 Original Slides by: Suresh Venkatasubramanian Updates by Joseph Kider.
Graphics Pipeline.
Status – Week 257 Victor Moya. Summary GPU interface. GPU interface. GPU state. GPU state. API/Driver State. API/Driver State. Driver/CPU Proxy. Driver/CPU.
Texture Mapping. Texturing  process that modifies the appearance of each point on a surface using an image or function  any aspect of appearance can.
CS-378: Game Technology Lecture #9: More Mapping Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic, Jessica.
Real-Time Rendering TEXTURING Lecture 02 Marina Gavrilova.
9/25/2001CS 638, Fall 2001 Today Shadow Volume Algorithms Vertex and Pixel Shaders.
The Programmable Graphics Hardware Pipeline Doug James Asst. Professor CS & Robotics.
Morphing and Animation GPU Graphics Gary J. Katz University of Pennsylvania CIS 665 Adapted from articles taken from ShaderX 3, 4 and 5 And GPU Gems 1.
Introduction to Shader Programming
GLSL I May 28, 2007 (Adapted from Ed Angel’s lecture slides)
Skin Rendering GPU Graphics Gary J. Katz University of Pennsylvania CIS 665 Adapted from David Gosselin’s Power Point and article, Real-time skin rendering,
Cg: C for Graphics Jon Moon Based on slides by Eugene Lee.
The programmable pipeline Lecture 10 Slide Courtesy to Dr. Suresh Venkatasubramanian.
Mohan Sridharan Based on slides created by Edward Angel GLSL I 1 CS4395: Computer Graphics.
Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides Shaders Feb 18, 2011 Creative Commons Attribution 3.0 (Except copyrighted.
GPU Graphics Processing Unit. Graphics Pipeline Scene Transformations Lighting & Shading ViewingTransformations Rasterization GPUs evolved as hardware.
Cg Kevin Bjorke GDC NVIDIA CONFIDENTIAL A Whole New World with Cg Graphics Program Written in Cg “C” for Graphics Compiled & Optimized Low Level,
GAM532 DPS932 – Week 1 Rendering Pipeline and Shaders.
Cg – C for Graphics Eric Vidal CS 280. History General graphics processing languages – Renderman shading language (1988) Assembly languages for graphics.
REAL-TIME VOLUME GRAPHICS Christof Rezk Salama Computer Graphics and Multimedia Group, University of Siegen, Germany Eurographics 2006 Real-Time Volume.
GPU Programming Robert Hero Quick Overview (The Old Way) Graphics cards process Triangles Graphics cards process Triangles Quads.
Programmable Pipelines. Objectives Introduce programmable pipelines ­Vertex shaders ­Fragment shaders Introduce shading languages ­Needed to describe.
Real-time Graphical Shader Programming with Cg (HLSL)
Geometric Objects and Transformations. Coordinate systems rial.html.
OpenGL Shading Language (Advanced Computer Graphics) Ernest Tatum.
GPU Shading and Rendering Shading Technology 8:30 Introduction (:30–Olano) 9:00 Direct3D 10 (:45–Blythe) Languages, Systems and Demos 10:30 RapidMind.
Programmable Pipelines. 2 Objectives Introduce programmable pipelines ­Vertex shaders ­Fragment shaders Introduce shading languages ­Needed to describe.
UW EXTENSION CERTIFICATE PROGRAM IN GAME DEVELOPMENT 2 ND QUARTER: ADVANCED GRAPHICS Textures.
A Crash Course in HLSL Matt Christian.
09/09/03CS679 - Fall Copyright Univ. of Wisconsin Last Time Event management Lag Group assignment has happened, like it or not.
Shading CMSC 435/634. RenderMan Light Displacement Surface Volume Imager.
1 Dr. Scott Schaefer Programmable Shaders. 2/30 Graphics Cards Performance Nvidia Geforce 6800 GTX 1  6.4 billion pixels/sec Nvidia Geforce 7900 GTX.
The programmable pipeline Lecture 3.
Computer Graphics The Rendering Pipeline - Review CO2409 Computer Graphics Week 15.
GAM532 DPS932 – Week 2 Vertex Shaders. The Shader Pipeline Vertex Processing Primitive Assembly / Processing Rasterization Fragment Process Pixel Output.
Shadow Mapping Chun-Fa Chang National Taiwan Normal University.
Shader Study 이동현. Vision engine   Games Helldorado The Show Warlord.
Xbox MB system memory IBM 3-way symmetric core processor ATI GPU with embedded EDRAM 12x DVD Optional Hard disk.
09/16/03CS679 - Fall Copyright Univ. of Wisconsin Last Time Environment mapping Light mapping Project Goals for Stage 1.
Computer Graphics 3 Lecture 6: Other Hardware-Based Extensions Benjamin Mora 1 University of Wales Swansea Dr. Benjamin Mora.
Week 3 Lecture 4: Part 2: GLSL I Based on Interactive Computer Graphics (Angel) - Chapter 9.
 Learn some important functions and process in OpenGL ES  Draw some triangles on the screen  Do some transformation on each triangle in each frame.
09/25/03CS679 - Fall Copyright Univ. of Wisconsin Last Time Shadows Stage 2 outline.
What are shaders? In the field of computer graphics, a shader is a computer program that runs on the graphics processing unit(GPU) and is used to do shading.
Mesh Skinning Sébastien Dominé. Agenda Introduction to Mesh Skinning 2 matrix skinning 4 matrix skinning with lighting Complex skinning for character.
OpenGL Shading Language
Advanced D3D10 Shader Authoring Presentation/Presenter Title Slide.
GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global.
An Introduction to the Cg Shading Language Marco Leon Brandeis University Computer Science Department.
Computer Science – Game DesignUC Santa Cruz Tile Engine.
COMP 175 | COMPUTER GRAPHICS Remco Chang1/XX13 – GLSL Lecture 13: OpenGL Shading Language (GLSL) COMP 175: Computer Graphics April 12, 2016.
Ying Zhu Georgia State University
Programmable Shaders Dr. Scott Schaefer.
Graphics Processing Unit
Normal Map Compression with ATI 3Dc™
Chapter 6 GPU, Shaders, and Shading Languages
The Graphics Rendering Pipeline
UMBC Graphics for Games
Chapter VI OpenGL ES and Shader
Computer Graphics Introduction to Shaders
CIS 441/541: Introduction to Computer Graphics Lecture 15: shaders
03 | Creating, Texturing and Moving Objects
CS 480/680 Computer Graphics GLSL Overview.
Language Definitions Chap. 3 of Orange Book.
CS 480/680 Fall 2011 Dr. Frederick C Harris, Jr. Computer Graphics
Presentation transcript:

A HLSL Primer for Developers By Wolfgang F. Engel June 13th, 2003

Agenda HLSL by example: projective texturing Two tricks –Simulating blending operations on floating point render targets –Normal Map Compression

Introduction High-Level Shader Language (HLSL) is a language for programming GPU‘s Looks like C Example vertex and pixel shader for projective texturing (texture should appear to be projected onto the scene, as if from a slide projector) struct VS_OUTPUTPROJTEX// output structure { float4 Pos : POSITION; float4 Tex : TEXCOORD0; }; VS_OUTPUTPROJTEX VSProjTexture(float4 Pos : POSITION, float3 Normal : NORMAL) { VS_OUTPUTPROJTEX Out = (VS_OUTPUTPROJTEX)0; Out.Pos = mul(Pos, matWorldViewProj);// transform Position Out.Tex = mul(ProjTextureMatrix, Pos);// project texture coordinates return Out; } float4 PSProjTexture(float4 Tex: TEXCOORD0) : COLOR { return tex2Dproj(ProjTexMapSampler, Tex); }

Vertex Shader Explanation struct VS_OUTPUTPROJTEX// output structure { float4 Pos : POSITION; float4 Tex : TEXCOORD0; }; Output structure: vertex shader outputs a position value and a texture coordinate to the pixel shader Semantics (f.e. : POSITION or : TEXCOORD0) –capital keyword + preceded by a colon (:) –Links shader input to the output of the previous stage of the graphics pipeline –In other words: helps the HLSL compiler to bind the data to the right hardware registers –Different Input and output semantics for vertex and pixel shaders Data types: –bool, float, float2, float3, float4, float4x4 (matrix) etc. –int (32-bit integer), half (16-bit float), double (64-bit float) (emulated if not supported) –No string type

Vertex Shader Explanation II VS_OUTPUTPROJTEX VSProjTexture(float4 Pos : POSITION) { VS_OUTPUTPROJTEX Out = (VS_OUTPUTPROJTEX)0; Out.Pos = mul(Pos, matWorldViewProj);// transform Position Out.Tex = mul(ProjTextureMatrix, Pos);// project texture coordinates return Out; } Math Intrinsic functions (converted to instructions by the HLSL compiler) –normalize(v)returns normalized vector v/length(v) –pow(x, y)returns x y –saturate(x)clamps x to 0..1 –mul(a, b)multiplies row-vector * matrix or matrix * column-vector (might help to save a transpose) –and many more –Math intrinsic functions work in vertex and pixel shaders

Pixel Shader float4 PSProjTexture(float4 Tex: TEXCOORD0) : COLOR { return tex2Dproj(ProjTexMapSampler, Tex); } Texture sampling intrinsics: 16 tex* for 1D, 2D, 3D and cube map textures tex2Dproj(s, t): 2D projective texture lookup by dividing t (4D texture coordinate) through its last component Output value: float4 : COLOR Rendering into a multiple render target: struct PS_OUTPUTPROJTEX { float4 Col : COLOR; float4 Col1 : COLOR1; }; PS_OUTPUTPROJTEX PSProjTexture(float4 Tex: TEXCOORD0) { PS_OUTPUTPROJTEX Out = (PS_OUTPUTPROJTEX) 0; Out.Col = tex2Dproj(ProjTexMapSampler, Tex); Out.Col1 = tex2Dproj(ProjTexMapSampler, Tex); return Out; }

Projective Texturing Issues There are two notable issues with projective texturing –Back-projection artifacts: projects texture on surfaces behind the projector Reason: undefined negative results from texture interpolators Solution: Tex.w < 0.0 ? 0.0 : tex2Dproj(ProjTexMapSampler, Tex) –projective texture is applied to every triangle (front and back) that is within the projectors frustrum Reason: No occlusion checks Solution #1: Restrict projection to front facing triangles by comparing a dot product between the projectors direction and the normal to 0.0. VS_OUTPUTPROJTEX VSProjTexture(float4 Pos : POSITION, float3 Normal : NORMAL) { VS_OUTPUTPROJTEX Out = (VS_OUTPUTPROJTEX)0; Out.Pos = mul(Pos, matWorldViewProj);// transform Position Out.Tex = mul(ProjTextureMatrix, Pos);// project texture coordinates float4 PosWorld = normalize(mul(Pos, matWorld));// vertex in world space Out.Norm = normalize(mul(Normal, matWorld));// normal in world space Out.Proj = PosWorld - normalize(vecProjDir); // projection vector in world space return Out; } float4 PSProjTexture(float4 Norm : TEXCOORD0, float4 Tex: TEXCOORD1, float4 Proj : TEXCOORD2) : COLOR { if (dot(Proj, Norm) <= 0.0) return Tex.w < 0.0 ? 0.0 : tex2Dproj(ProjTexMapSampler, Tex); else return 1.0; } –Solution #2: Depth Map to clamp projection

Demo

HLSL Features from C HLSL provides structures and arrays incl. Multidimensional arrays Comments (//, /* */) All C‘s arithmetic operators (+, *, /, etc.) bool type with bolean and relational operators (||, &&, !, etc.) Increment/decrement (++, --) Conditional expression (? :) Assigment expressions (+=, etc.) C comma operator User defined functions (not: recursive functions) A subset of C‘s flow constructs (do, while, for, if, break, continue) Not: goto, switch #define, #ifdef etc.

Additional HLSL Features not in C Built-in constructors float4 vec = float4(4.0, -2.0, 5.0, 3.0); Swizzling float4 vec1 = float4(4.0, -2.0, 5.0, 3.0); float2 vec2 = vec1.yx; // vec2 = (-2.0, 4.0) float scalar = vec1.w; // scalar = 3.0 float3 vec3 = scalar.xxx;// vec3 = (3.0, 3.0, 3.0) Write mask vec1.xw = vec3; // vec1 = (3.0, -2.0, 5.0, 3.0) Matrices can be stored as row_major or column_major row_major half1x4 fh1By4; column_major half1x4 fh4By1; row_major half3x2 fh3By2; column_major half3x2 fh2By3; Or as compiler pragmas #pragma PACK_MATRIX (ROW_MAJOR) #pragma PACK_MATRIX (COLUMN_MAJOR) The keyword register can be used to specify a specific register float4 vDisplace : register (c0); // puts vDisplace constant into C0 Restrict the usage of specific registers on specific versions float4 vDisplace : register(ps_2_0, c10) // puts vDisplace into C10 for ps_2_0

C Features not Supported in HLSL No pointers/bitwise operations No unions and function variables No classes, templates, operator overloading, exception handling, and namespaces No string processing (no string type), file i/o, memory allocation Not the scope of HLSL

What‘s required for Compilation Entry point: name of vertex shader or pixel shader Compile target: vertex or pixel shader version Version Inst. Slots Constant Count ====== ======== =========== vs_1_1 128 at least 96 cap'd (4) vs_2_0 256 cap'd (4) vs_2_x 256 cap'd (4) vs_2_sw unlimited 8192 vs_3_0 cap'd (1) cap'd (4) ps_1_1 - ps_1_ ps_1_4 28 (in two phases) 8 ps_2_ ps_2_x cap'd (2) 32 ps_2_swunlimited 8192 ps_3_0 cap'd (3) 224 ps_3_sw unlimited 8192 (1) D3DCAPS9.MaxVertexShader30InstructionSlots (2) D3DCAPS9.D3DPSHADERCAPS2_0.NumInstructionSlots (3) D3DCAPS9.MaxPixelShader30InstructionSlots (4) D3DCAPS9.MaxVertexShaderConst Example command line: fxc.exe /T ps_2_0 /E PSProjTexture /Fc test.txt $(InputName).fx HLSL compilercompile TargetEntrypointoutput assembly filename of input file Effect file framework helps managing shaders and helps to provide fallback pathes: Recommended !

Trick 1: Blending on Float RTs Trick by Francesco Carucci, „Simulating Blending Operations on Floating Point Render Targets“, ShaderX 2 – Shader Tips & Tricks, August 2003, Wordware Ltd. Simulating blending operations on floating point render targets How does it work: –Pass 1: Setup floating point texture as render target and then render into it –Pass 2: send vertex position in camera space from the vertex shader through the interpolators to the pixel shader transform vertex position into screen space in the pixel shader Blend fp texture with the result of this pass Reading in the input texture in the pixel shader (Pass 2) –send vertex position in camera space from the vertex shader through the interpolators to the pixel shader VS_OUTPUTPROJTEX VSProjTexture(float4 Pos : POSITION, float3 Normal : NORMAL) { VS_OUTPUTPROJTEX Out = (VS_OUTPUTPROJTEX)0; float4 Position = mul(Pos, matWorldViewProj);// transform Position Out.Tex = mul(ProjTextureMatrix, Pos);// project texture coordinates Out.Pos = Position; Out.Tex2 = Position;// provide vertex position in camera space return Out; }

Trick: Blending on Float RTs II –transform vertex position into screen space in the pixel shader float4 tex2DRect(sampler2D s, float4 position)// user-defined function { float2 tc; tc.x = (position.x / position.w) * ; tc.y = (-position.y / position.w) * ; return tex2D(s, tc); } –Blend values: // // Pixel Shader (input channels):output channel // float4 PSProjTexture(float4 Tex: TEXCOORD0, float4 Position : TEXCOORD1) : COLOR { float4 TexProj = tex2Dproj(ProjTexMapSampler, Tex); float4 TexRenderMap = tex2DRect(RenderMapSampler, Position); return TexRenderMap * TexProj;// blend }

Trick 1: Blending on Float RTs III Does not support blending modes with a destination color computed in the same pass (f.e. particle system) Watch out for color saturation -> map to 0..1 with a logarithmic function Fillrate: 64-bit or 128-bit render targets burn fillrate.

Trick 2: Normal Map Compression Trick by Jakub Klarowicz, „Normal Map Compression “, ShaderX 2 – Shader Tips & Tricks, August 2003, Wordware Ltd. Geometry details in bump maps (ATI Normal Mapper or NVIDIA Melody) need high resolution maps -> big chunk of memory How does it work: –Use all four color channels of DXT5 format –One channel of the normal map (R, G or B) is copied into alpha channel A and then it is cleared (filled with zero values) (f.e. with Adobe Photoshop) –The alpha channel is copied back into the color channel in the pixel shader Why does it work: –Alpha channel is quantized separately from the RGB channels –Alpha channel is quantized with an increased accuracy –Clearing one color channel leads to a higher precision in quantization of the color channels Pixel Shader source: … float4 bumpNormal = 2 * (tex2D(BumpMapSampler, Tex) - 0.5); // bump map bumpNormal.g = bumpNormal.a;// move alpha into green color channel …

Trick 2: Normal Map Compression II Does not work well for model space normal maps as for tangent space maps -> vectors in model space normal maps vary much Rough normal maps still look bad when compressed

Trick 2: Normal Map Compression III Figure: Left DXT5 compressed (2049 kb) | Right: uncompressed (8183 kb)

Wrap Up HLSL ~ C with modifications for Graphics HLSL compiler optimizes for hardware from different vendors Use it … don‘t invest time to learn assembly

Further Reading Jason L. Mitchell, Craig Peeper, „Introduction to HLSL“, ShaderX 2 – Shader Introduction & Tutorial, August 2003, Wordware Cass Everitt, „Projective Texturing“, NVIDIA developer web-site.

Thank you