Dx8 Pixel Shaders Sim Dietrich NVIDIA Corporation

Slides:



Advertisements
Similar presentations
Fragment level programmability in OpenGL Evan Hart
Advertisements

16.1 Si23_03 SI23 Introduction to Computer Graphics Lecture 16 – Some Special Rendering Effects.
Exploration of advanced lighting and shading techniques
POST-PROCESSING SET09115 Intro Graphics Programming.
An Optimized Soft Shadow Volume Algorithm with Real-Time Performance Ulf Assarsson 1, Michael Dougherty 2, Michael Mounier 2, and Tomas Akenine-Möller.
Target Code Generation
DirectX 8 and GeForce3 Christian Schär & Sacha Saxer.
Virtual Realism TEXTURE MAPPING. The Quest for Visual Realism.
Normal Map Compression with ATI 3Dc™ Jonathan Zarge ATI Research Inc.
Michael I. Gold NVIDIA Corporation
Frame Buffer Postprocessing Effects in DOUBLE-S.T.E.A.L (Wreckless)
Ray tracing. New Concepts The recursive ray tracing algorithm Generating eye rays Non Real-time rendering.
Exploration of bump, parallax, relief and displacement mapping
Bump Mapping CSE 781 Roger Crawfis.
Understanding the graphics pipeline Lecture 2 Original Slides by: Suresh Venkatasubramanian Updates by Joseph Kider.
Graphics Pipeline.
Texture Mapping. Texturing  process that modifies the appearance of each point on a surface using an image or function  any aspect of appearance can.
Week 7 - Monday.  What did we talk about last time?  Specular shading  Aliasing and antialiasing.
Real-Time Rendering TEXTURING Lecture 02 Marina Gavrilova.
CHAPTER 10 Alpha Blending and Fog © 2008 Cengage Learning EMEA.
9/25/2001CS 638, Fall 2001 Today Shadow Volume Algorithms Vertex and Pixel Shaders.
SE 313 – Computer Graphics Lecture 13: Lighting and Materials Practice Lecturer: Gazihan Alankuş 1.
Shading Languages GeForce3, DirectX 8 Michael Oswald.
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,
Status – Week 277 Victor Moya.
ARB Fragment Program in GPULib. Summary Fragment program arquitecture New instructions.  Emulating instructions not supported directly New Required GL.
University of Texas at Austin CS 378 – Game Technology Don Fussell CS 378: Computer Game Technology Beyond Meshes Spring 2012.
Computer Graphics: Programming, Problem Solving, and Visual Communication Steve Cunningham California State University Stanislaus and Grinnell College.
SET09115 Intro Graphics Programming
Shadows Computer Graphics. Shadows Shadows Extended light sources produce penumbras In real-time, we only use point light sources –Extended light sources.
Maths and Technologies for Games Water Rendering CO3303 Week 22.
1 Computer Graphics Week13 –Shading Models. Shading Models Flat Shading Model: In this technique, each surface is assumed to have one normal vector (usually.
Computer Graphics: Programming, Problem Solving, and Visual Communication Steve Cunningham California State University Stanislaus and Grinnell College.
REAL-TIME VOLUME GRAPHICS Christof Rezk Salama Computer Graphics and Multimedia Group, University of Siegen, Germany Eurographics 2006 Real-Time Volume.
University of Illinois at Chicago Electronic Visualization Laboratory (EVL) CS 426 Intro to 3D Computer Graphics © 2003, 2004, 2005 Jason Leigh Electronic.
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.
Graphics Graphics Korea University cgvr.korea.ac.kr 1 Using Vertex Shader in DirectX 8.1 강 신 진
Programmable Pipelines. 2 Objectives Introduce programmable pipelines ­Vertex shaders ­Fragment shaders Introduce shading languages ­Needed to describe.
Week 2 - Wednesday CS361.
Chris Kerkhoff Matthew Sullivan 10/16/2009.  Shaders are simple programs that describe the traits of either a vertex or a pixel.  Shaders replace a.
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.
Computer Graphics The Rendering Pipeline - Review CO2409 Computer Graphics Week 15.
Taku KomuraComputer Graphics Local Illumination and Shading Computer Graphics – Lecture 10 Taku Komura Institute for Perception, Action.
Advanced Computer Graphics Advanced Shaders CO2409 Computer Graphics Week 16.
Shader Study 이동현. Vision engine   Games Helldorado The Show Warlord.
CHAPTER 8 Color and Texture Mapping © 2008 Cengage Learning EMEA.
Realtime NPR Toon and Pencil Shading Joel Jorgensen May 4, 2010.
09/16/03CS679 - Fall Copyright Univ. of Wisconsin Last Time Environment mapping Light mapping Project Goals for Stage 1.
Advanced Computer Graphics Spring 2014 K. H. Ko School of Mechatronics Gwangju Institute of Science and Technology.
Lab: Vertex Shading Chris Wynn Ken Hurley. Objective Hands-On vertex shader programming Start with simple programs … Part 1: Textured-Lit Teapot.
11/5/2002 (c) University of Wisconsin, CS 559 Last Time Local Shading –Diffuse term –Specular term –All together –OpenGL brief overview.
COMPUTER GRAPHICS CS 482 – FALL 2015 SEPTEMBER 29, 2015 RENDERING RASTERIZATION RAY CASTING PROGRAMMABLE SHADERS.
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.
Pixel Shader  Based on nVIDIA’s GF3/4 architecture  Texture shader + register combiner texture unit 0texture program texture unit 1texture program texture.
An Introduction to the Cg Shading Language Marco Leon Brandeis University Computer Science Department.
- Introduction - Graphics Pipeline
Ying Zhu Georgia State University
Week 7 - Monday CS361.
Deferred Lighting.
Normal Map Compression with ATI 3Dc™
CS451Real-time Rendering Pipeline
Introduction to Programmable Hardware
Chapter XIV Normal Mapping
Computer Graphics Introduction to Shaders
CIS 441/541: Introduction to Computer Graphics Lecture 15: shaders
Presentation transcript:

Dx8 Pixel Shaders Sim Dietrich NVIDIA Corporation

Two Quads Lit Per-Pixel

Dx8 Pixel Shading Topics Dx7 Pixel Pipeline Dx8 Pixel Pipeline What is a Pixel Shader? Instruction overview

DX7 Texture Stage Pipeline Texture 0 Texture 1 Triangle Rasterizer 2 Texture Stages Specular / fog Computed Alpha Blending

Dx7 Texture Stages On Dx7-class hardware, you cant use the Pixel Shader API You can use the old dx6 & dx7-style Texture Stage API pDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE); But, there are two new new color ops available D3DTOP_MULTIPLYADD D3DTOP_LERP

Dx7-Style Multi-pass effects Most interesting effects are enabled through multi-pass techniques DOT3 is often multi-pass under D3D Full texture blending functionality of hardware is sometimes hidden for the sake of API compatibility, so more passes You typically end up burning a stage or two trying to control the look of the DOT3 effect anyway For instance, adding ambient or per-vertex colors

DX8 Pixel Shading Pipeline TexAddrOp 0 TexAddrOp 1 TexAddrOp 2 TexAddrOp 3 Triangle Rasterizer 8 Texture Blend Ops Specular / Fog Computed Alpha Blending Dx8 Pixel Shaders

What is a Pixel Shader? A Pixel Shader is a byte stream of instructions compiled from a text file You can compile the pixel shader at runtime during development, and then change to pre-compiled byte- streams for release mode Assemble a Pixel shader like so : D3DXAssembleShader( strParsedProgram.c_str(), // source strParsedProgram.size() - 1, //size 0, // no flags &pConstants, // constant floats pCode, // where to put code &pCompileErrors); // get errors

Dx8 Pixel Shaders Now ask D3D for a handle to the compiled pixel shader : pD3DDev->CreatePixelShader( pCode GetBufferPointer(), &m_dwMyHandle ); Now select this pixel shader program like so: pD3DDev->SetPixelShader( m_dwMyHandle ); Be sure to delete it pD3DDev->SetPixelShader( 0 ); pD3DDev->DeletePixelShader( m_dwMyHandle );

DX8 Pixel Shaders Three types of Instructions Constant Definitions Similar to Setting the TFACTOR Texture Address Ops Fetching Texels Floating Point Math Texture Blending Ops Combining texels, constant colors and iterated colors to produce SrcColor and SrcAlpha

Dx8 Pixel Shader Versions Version 1.1 – GeForce3 Up to 8 instructions Texture registers can be read from and written to EMBM takes only a single texture address slot Version 1.0 – Unknown Hardware Up to 4 instructions Texture registers are read-only EMBM takes two texture address slots

Pixel Shader Parts Using the Pixel Shader API, there are two parts to each program Up to 4 Texture Address Ops – Essentially here is where you say what each set of 4 texture coordinates are doing This controls HOW the texels are fetched Up to 8 Texture Blending Ops – Similar to TextureStageStates This is AFTER the texels are fetched and filtered There is no loopback to the Texture Address Ops

Setting Constants def c#,x,y,z,w Sets the Constant, from 0 to 7, with the appropriate floating point value : def c0, 1.0f, 4.0f, -10.0f, 1.0f Constants are clamped in the range [-1..1] A single color/alpha instruction pair can only reference two constants

Texture Address Ops Each Texture Address Op represents the use of a particular set of texture coordinates Texture Address Ops can be used either to : Look up a filtered texel color Use as a vector Use as the part of a matrix

Simple Texture Lookup Could be projective, or a cubemap, or a volume texture Just fetches a filtered texel color tex t0 mov r0, t0// just output color

Bump Environment Map texbem tDest, tSrc0 U += 2x2 matrix( dU ) V += 2x2 matrix( dV ) Then Sample at ( U, V ) tex t0// sample offset map texbem t1, t0// perform offset & sample mov r0, t1// output perturbed value

Bump Environment Map 2x2 Matrix The 2x2 Matrix modifies the direction and scale of the dU and dV terms from the bump map The 2x2 Matrix is set via SetTextureStageState calls D3DTSS_BUMPENVMAT00 D3DTSS_BUMPENVMAT01 D3DTSS_BUMPENVMAT10 D3DTSS_BUMPENVMAT11

Bump Environment Map Luminance texbeml tDest, tSrc0 U += 2x2 matrix( dU ) V += 2x2 matrix( dV ) Then Sample at ( U, V ) & Apply Luminance tex t0// sample offset map texbeml t1, t0// perform offset & sample // Also apply luminance & offset mov r0, t1// output perturbed & scaled value

texbeml - continued The Luminance is set via a SetTextureStage State call D3DTSS_BUMPENVLSCALE This is the amount to scale the fetched color by D3DTSS_BUMPENVLOFFSET This is the amount to add to fetched color

texbem & texbeml These instructions implement Environmental Bump Mapping ( EMBM ) EMBM is great for planar surfaces, but breaks down on anything convex or complicated EMBM uses an implicit 2D tangent basis, whereas you really need a 3D tangent basis to handle all objects and orientations That said, for water and planar surfaces, its the way to go

texcoord Clamps the texture coordinates to the range [0.0, 1.0] and output as a color texcoord tDest texcoord t0// pass in texture coordinates as a color tex t1 // sample a regular texture mov r0, t1 mul r0, r0, t0// modulate color and texture

texcoord Useful for passing in vectors without having to use a cubemap or iterated color texcoord t0// grab L vector in tangent space tex t1 // grab N vector in tangent space mov r1, t1_bx2 dp3_sat r0, r1, t0_bx2// Compute Clamp( L dot N ) Can be used for 1 – d*d distance calculation for attenuation as well texcoord t0// turn into color dp3 r1, t0_bx2, t0_bx2// compute d^2 mov r0, 1 - r1// compute 1-d^2

Texture Kill ( Clip Plane ) texkill tDest Kill the pixel if at least one of s,t,r,q is < 0 tex t0// sample a normal texture tex t1// sample a normal texture texcoord t2// clip out per-pixel based on // s,t,r & q mov r1, t1 mul r0, r1, t0// this will get skipped if the // pixel is killed

texm3x2pad Texm3x2pad t1, t0 padding instruction as part of the texm3x2tex instruction – performs a dot product of t0s color with these texture coordinates S coordinate of next stages texture = t1 DOT t0

texm3x2tex : Dependent Texture Take previous dot product from pad instruction as the S coordinate Perform dot product of t0s color with this texture coordinate and use as T Sample from a 2D texture using ( S, T ) tex t0 // sample normal map texm3x2pad t1, t0_bx2 // t2.s = t1.texcoord dot t0.rgb texm3x2tex t2, t0_bx2 // t2.t = t2.texcoord dot t0.rgb mov r0, t2 // output result of lookup

texm3x2tex This is possibly the most useful new instruction You pass in two vectors as texture coordinates ( usually L and H ), and sample another vector ( usually N ) The 3 rd texture is sampled using ( L dot N ) as the S texture coordinate ( H dot N ) as the T texture coordinate This gives you BRDF-like anisotropic lighting on a per-pixel basis Great for velvet, brushed metal or any material that has intensity or hue shifts that vary with angle Of course, if your surface is not bumpy, then just do this per-vertex instead

texm3x2tex Here is a N.L N.H texture used for Anisotropic Lighting

texm3x2tex You can also use it for toon shading. Rather than H dot N for the vertical dimension, perform E dot N instead. This allows the silhouette detection to pick up even the silhouette edges of bump maps

Simple 2D Dependent Texture texreg2ar tDest, tSrc Sample from ( tSrc.A, tSrc.R ) texreg2gb tDest, tSrc Sample from ( tSrc.G, tSrc.B ) tex t0 // sample regular texture texreg2ar t1, t0 // t1.S = t0.Alpha, t1.T = t0.Red texreg2gb t2, t0 // t2.S = t0.Green, t2.T = t0.Blue dp3_sat r0, t1_bx2, t2_bx2

Game Of Life Using texreg2gb

3x3 Texture Address Ops Texm3x3pad Padding for 3x3 matrix operation Uses the 3D texture coordinate as a row of the matrix Texm3x3spec Compute Non-Local Viewer Specular reflection about Normal from Normal Map tex t0 ; Normal Map texm3x3pad t1, t0; 1 st row of matrix texm3x3pad t2, t0; 2 nd row of matrix texm3x3spec t3, t0, c0 ; 3 rd row, reflect & sample mov r0, t3

Local Viewer Reflection Texm3x3vspec Compute Local Viewer Specular reflection about Normal from Normal Map Eye vector comes from q coordinates of the 3 sets of 4D textures tex t0 ; Normal Map texm3x3pad t1, t0; 1 st matrix row, x of eyevector texm3x3pad t2, t0; 2 nd matrix row, y of eyevector texm3x3vspec t3, t0 ; 3 rd row & eye z, reflect & sample mov r0, t3

texm3x3vspec This instruction is the one to use for bumpy reflective surfaces It is the most complex, and slowest instruction, but arguably also the most visually stunning

Procedural Normal Maps using tex3x3vspec

3x3 Per-Pixel Vector Rotation texm3x3tex Rotate vector through 3x3 matrix, then sample a CubeMap or 3D texture tex t0 ; Normal Map texm3x3pad t1, t0; 1 st matrix row texm3x3pad t2, t0; 2 nd matrix row texm3x3tex t3, t0 ; 3 rd matrix row & sample mov r0, t3

Texture Blending Ops After all Texture Address Ops, you can have up to 8 texture blending instruction slots Each slot can hold a color and an alpha operation to be executed simultaneously These are analogous to the old TextureStageState COLOROP and ALPHAOPs

Texture Blending Ops adddest, src1, src2 dest = src1 + sr2 sub dest, src1, src2 dest = src1 – src2 lrpdest, factor, src1, src2 dest = (factor)src1 + ( 1-factor)src2 dp3dest, src1, src2 dest = ( src1.x * src2.x + src1.y * src2.y …)

dp3 This is the workhorse instruction It is used for all per-pixel lighting calculations in the texture blending unit Typically you want to _sat your dot3 in lighting calculations to prevent lights behind a surface from showing up, so mostly you will use something like : dp3_sat r0, t0_bx2, r1 The bx2 modifier is there to take an 8 bit unsigned value and expand it to 1.8 signed format Note that the registers keep their sign bit, so be sure to use _bx2 only once after storing the signed value

Texture Blending Ops muldest, src0, src1 dest = src0 * src1 mad dest, src0, src1, src2 dest = ( src0 * src1 + src2 ) mov dest, src dest = src cnd dest, r0.a, src1, src2 if ( r0.a > 0.5 ) { dest = src1; } else { dest = src2; }

Argument Modifiers Alpha Replicate r0.a Invert 1 – r0 Negate -r0 Bias – subtract 0.5 r0_bias Signed Scale – 2 * ( x – 0.5f ) r0_bx2

Instruction Modifiers _x2// double result _x4 // quadruple result _d2 // halve result _sat // clamp 1 to 1 You can use _sat together with scaling : For instance : add_x2_sat r0, r1, t2 Common Example : dp3_sat r1, r0_bx2, t0_bx2

Simultaneous Color/Alpha You can dual issue color and alpha instructions Only applies for blending ops, not for address ops Use the + plus sign to indicate simultaneous execution mul r0.rgb, c1.rgb, c2.rgb + add r1.a, t0.a, t1.a

Example Pixel Shader ps.1.1; DirectX8 Version tex t0 ; sample normal map texm3x2pad t1, t0_bx2 ; N dot L texm3x2tex t2, t0_bx2 ; N dot H and sample add r0, t2, c0; add in ambient mov r0.a, t0.a; normal map alpha into r0

Additional Pixel Shader Notes The result of a pixel shader is always r0 SRCCOLOR = r0.rgb SRCALPHA = r0.a You can combine rgb & alpha operations to occur concurrently with + add r0.rgb, t1.rgb t0.rgb + mul r0.a, r1.a, t1.a You are in charge of your own specular add, SPECULAR_ENABLE is ignored Fog is still automatic

Vertex & Pixel Shaders It is common to write Vertex Shaders that use the legacy TextureStageState pipeline to do the texture blending However, almost all Pixel Shaders have a Vertex Shader to set things up properly Calculating & Packing L and H vectors Passing in Texture Space vectors Calculating and/or setting up Attenuation

Questions… ? Sim Dietrich