Designing a portable shader library for current and future api s
Download
1 / 51

link - PowerPoint PPT Presentation


  • 355 Views
  • Updated On :

Designing a Portable Shader Library for Current and Future API's David Gosselin 3D Application Research Group Outline Introduction & Motivation Demos Case Study: ATI’s demo shader format Design Goals Artist’s Interface Texture Definitions Vertex and Index Buffers Sub-shaders and Passes

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'link' - bernad


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Designing a portable shader library for current and future api s l.jpg

Designing a Portable Shader Library for Current and Future API's

David Gosselin

3D Application Research Group


Outline l.jpg
Outline API's

  • Introduction & Motivation

  • Demos

  • Case Study: ATI’s demo shader format

    • Design Goals

    • Artist’s Interface

    • Texture Definitions

    • Vertex and Index Buffers

    • Sub-shaders and Passes

    • Vertex and Pixel Shaders

  • Converting from D3D ASM to HLSL

  • Example


Motivation l.jpg
Motivation API's

  • Share the lessons learned from our shader library

    • Shaders in general

    • Moving from D3D ASM to HLSL

  • Show some advantages to being shader-centric

  • Show ways to include fallback paths

  • Show cross platform generalizations

  • Would like to see more games use shaders and not just target the least common denominator for graphics


Why shaders l.jpg
Why Shaders? API's

  • Direction of the Industry

    • Hardware very shader driven

    • Will continue down this path

    • Modern engines need to be shader aware

  • High level shader languages (HLSL and OpenGL) make shaders easier to write

  • Visual flexibility

  • Less engine churn


What is a shader library l.jpg
What is a Shader Library? API's

  • Library in the sense of a linkable .lib file or collection of source code

    • Not a collection of shaders

  • Abstracts Graphics API

    • Render States

    • Constant Binding

    • Shader Binding

    • Etc.

  • Manages Shaders

  • Integrates with preprocessing/export


Engine block diagram l.jpg

Exporter API's

Material

Plug-ins

Shader/State Management

Preprocessor

OGL

D3D

Runtime Engine

Engine Block Diagram

Maya

File IO

3DS Max

Parsing

Shader Files

Shader Library


What is in a shader file l.jpg
What is in a Shader File? API's

  • Contains all the state and definitions needed to render a piece of geometry.

  • Includes

    • Artist Instructions

    • Preprocessing Directives

    • Texture Definitions

    • Constant / Variable Definitions

    • Stream Definitions (Vertex/Index buffers)

    • Fixed Function States (Alpha, Stencil, Z, etc.)

    • Vertex Shader

    • Pixel Shader

  • Potentially multiple LOD/# light variants



Shader file design goals l.jpg
Shader File Design Goals API's

  • Cross platform / cross API

  • Reduce the frequency of redesigning graphics engine

  • Expandable to future shader languages and API’s

  • Drive preprocessing (optimal VB/IB)

  • Fallback shaders

  • LOD shaders

  • Ability to target a wide range of graphics hardware

  • Artist interaction to runtime execution


Artists point of view l.jpg
Artists Point-of-View API's

  • To the RIGHT are our Maya and 3DS MAX plug-ins

  • Artists associate a shader with a material within the art tool

  • Art tool plug-in pulls instructions from the file for the artists


Artist notes l.jpg
Artist Notes API's

StartArtNotes

Supports:

- 3 Fast RT lights

- 3 Object Ambient Lights

Requirements:

- Base = RGB texture

- Bump = RGB normal map

EndArtNotes


Slide12 l.jpg

Art Notes API's

  • Describe maximum number of lights supported

  • Describe textures needed

  • Any special instructions

    • Artist editable variables

    • Special scene geometry

    • Etc.


Texture placement l.jpg
Texture Placement API's

Texture tBase 2D DXT1("Base", RGB, Box)

DefTexture tBase Trilinear

Texture tBump 2D RGBX("Bump", RGB, Box)

DefTexture tBump Trilinear


Texture declaration l.jpg
Texture Declaration API's

  • How to preprocess textures

    • Output format

    • Input Format

    • Mipmap Generation

    • Separate RGB and alpha textures

Texture tBase 2D DXT1 ("Base", RGB, KaiserGamma)

Texture tFurShellTexture 2D RGBA (“T1", RGB, Kaiser,

“T2", GRAY,Kaiser)

Texture tBump 2D DXT5 ("Bump", HEIGHT,Box,

“Opacity",GRAY,Box)

Texture tAnisoLookup 2D RGBA (“T5", RGB, Box,

“T6", GRAY,Box)

Texture tNormCube CM RGBX ("Base3", RGB, Box)

Texture tEnvMap CMAuto // Engine generated cubemap

Texture tWater Renderable("WaterReflection")


Artist editable variables l.jpg
Artist Editable Variables API's

Vector vColor(.7, .7, .7, 0.0) Editable(color)

Vector vReflectionColor (1, 1, 1, 1.0) Editable(Color)

Float vSpecularExp (16.0) Editable(Slider, 0, 512)


Variables l.jpg
Variables API's

  • Standard types: Float, Vector, Matrix

  • Can be bound to render state

  • Can be bound to engine state

  • Constant

  • Can be artist editable (from within tool)

Matrix wvp(MATRIX_WVP)

Vector osCamPos(CAMERA_POSITION, OBJECT_SPACE)

Vector osLightPos(LIGHT_POSITION,OBJECT_SPACE, 0)

Vector time(0.0, 0.0, 0.0, 0.0) AppUpdate(“Time”)

Vector furFadeScaleBias(0.5, 0.5, 0.0, 0.0)

Float furHeight(4.0) EDITABLE


Vertex and index buffers l.jpg
Vertex and Index Buffers API's

  • How to preprocess vertex/index buffers

  • One IB per unique stream map

StartStream sPosNorm (Normal)

float3 POSITION0 Position

float3 NORMAL0 VertexNormal

EndStream

StartStream sTexCoords (Normal)

float2 TEX0 UV0 // BaseTexU, BaseTexV

float3 TEX1 Tangent0

EndStream

StartStream sFurFins (FurFins) // Different geometry than above 2 streams

float3 POSITION0 Position

float3 NORMAL0 VertexNormal

float4 TEX0 FinFaceData0 // FinTexU, FinTexV, BaseTexUVDist, RandOffset

float3 TEX2 FaceNormal

EndStream

StreamMap smBasePass (sPosNorm, sTexCoords)

StreamMap smShellPass (sPosNorm, sTexCoords)

StreamMap smFinsPass (sFurFins)


Sub shaders l.jpg
Sub-Shaders API's

  • Single shader file with multiple sub-shaders

    • Fallbacks

    • LOD

    • Split-screens

    • Different number of lights

  • Controlled via a property string

  • First in file (top to bottom) with unique properties that validates

  • Can contain multiple passes


Sub shader example l.jpg
Sub-Shader Example API's

//-----------------------------------------------------

StartShader//For vs.2.0 and ps.2.0 minimum

Property “Normal” // Can be anything “Wire frame”,

// “Invincible”,“One Light”, etc.

StartPass

EndPass

EndShader

//-----------------------------------------------------

StartShader//For vs.1.1 and ps.1.4

Property “Normal”

StartPass

EndPass

StartPass

EndPass

EndShader


Falling back l.jpg
Falling Back API's

  • Determine which shaders validate before loading vertex buffers, index buffers and textures.

  • Vertex/Index Buffers

    • May need single stream vertex buffers for older hardware

    • All potential vertex streams defined in shader

    • Load only those required for shaders which validate

    • Extra data stored on disk, but optimal for runtime

  • Textures

    • Many fallback shaders won’t require all the defined textures

    • Only load textures required for validated shaders


Slide21 l.jpg

Each pass has a Stream Mapping, unique set of render state and textures, and vertex and pixel shader code.

Within a sub-shader, state is sticky between passes.

Pass

StartPass

//Set Stream Map

//Set Textures

//Set Render State

//Set Vertex Shader Constants

//Vertex Shader Code

//Set Pixel Shader Constants

//Pixel Shader Code

EndPass


Common graphics concepts l.jpg
Common Graphics Concepts and textures, and vertex and pixel shader code.

  • Graphics hardware is very similar despite differences in API’s

  • Hardware is functionally identical:

    • Setting texture state

    • Vertex/index buffers, stream maps

    • Draw calls

    • Alpha blending & testing

    • Shader constant setup

    • Z state

    • Stencil state

    • Renderable textures

    • etc.


Common state keywords l.jpg
Common State Keywords and textures, and vertex and pixel shader code.

Clipping TRUE

Cull CW

FillMode Solid

ShadeMode Gouraud

SetTexture 0 NULL CoordIndex(0) Transform(0) Linear LODBias(0.0)

Clamp(www) Border(0x00000000)

SetBlender 0 Color(SelectArg1, Diffuse, Diffuse) Alpha(SelectArg1,

Diffuse, Diffuse)

Fog FALSE Table(None) Vertex(None) Color(0) Start(0.0) End(1.0)

Density(1.0)

AlphaTest FALSE

Blend FALSE Src(One) Dest(Zero) Op(Add)

ColorWriteEnable (R, G, B, A)

MultiSampleAntiAlias TRUE

MultiSampleMask 0xffffffff

DitherEnable FALSE

Z TRUE Write(TRUE) Func(LessEqual) Bias(0.0) SlopeScale(0.0)

Stencil FALSE Pass(Keep) Fail(Keep) ZFail(Keep) Func(Always)

Ref(0xFFFFFFFF)

StencilCCW FALSE Pass(Keep) Fail(Keep) ZFail(Keep) Func(Always)


Shader languages l.jpg
Shader Languages and textures, and vertex and pixel shader code.

  • DirectX fixed function

  • DirectX assembly shaders

  • DirectX HLSL

  • OpenGL fixed function

  • OpenGL assembly shaders

    (ARB_vertex_program, ARB_fragment_program)

  • The OpenGL Shading Language (ARB_shading_language_100 )

  • GameCube

  • PS2


D3d asm shaders l.jpg
D3D ASM Shaders and textures, and vertex and pixel shader code.

  • VS/PS code embedded

  • Can also come from an external file.

VsConst 0 mWvp // Matrix takes up 4 constants

VsConst 4 vTimeConst

VsConst 5 (0.0, 0.1, 2.0, 5.0)

StartVertexShader

vs.1.1

dcl_position v0

dcl_color0 v5

dcl_texcoord0 v7

m4x4 oPos, v0, c0 // Transform position

mov oT0, v7 // Base texture coordinates

mov oD0, v5 // Pass vertex light to PS

EndVertexShader


Hlsl support design goals l.jpg
HLSL Support Design Goals and textures, and vertex and pixel shader code.

  • Build on our existing framework

  • Avoid explicit constant declarations

  • Allow HLSL include files to reference common functions

    • Includes can contain their own variables and textures

  • Hidden from outside the shader library


Basic hlsl shader l.jpg

Matrix and textures, and vertex and pixel shader code.mWvp(MATRIX_WVP)

Vector vTime AppUpdate(“Time”)

StartVertexShader(HLSL)

float4x4 mWvp;

float4 vTimeConst;

struct VS_OUTPUT

{

float4 Pos : POSITION;

float4 Diffuse : COLOR0;

float2 TCoord0 : TEXCOORD0;

};

VS_OUTPUT main (float4 aPosition : POSITION,

float4 aDiffuse : COLOR0, float2 aTC0 : TEXCOORD0)

{

VS_OUTPUT outV = (VS_OUTPUT) 0;

outV.Pos = mul (mWvp, aPosition); // Transform position

outV.TCoord0 = vTC0; // Pass texture coordinates

outV.Diffuse = vDiffuse; // Pass vertex light

return outV;

}

EndVertexShaderHLSL

New HLSL token

Constants by name

Basic HLSL Shader

VsConst0 mWvp //Takes up 4 constants

VsConst 4 vTime

VsConst5 (0.0, 0.1, 2.0, 5.0)

StartVertexShader

vs.1.1

dcl_position v0

dcl_color0 v5

dcl_texcoord0 v7

m4x4 oPos, v0, c0//Transform

mov oT0, v7//Base tex coords

mov oD0, v5 //Vertex light

EndVertexShader


Hlsl pixel shader l.jpg
HLSL Pixel Shader and textures, and vertex and pixel shader code.

Texture tBaseTexture 2D DXT1("Base", RGB, KaiserGamma)

StartPixelShader(HLSL)

sampler tBaseTexture;

struct PsInput

{

float2 texCoord : TEXCOORD0;

float3 vertexLight : COLOR0;

};

float4 main (PsInput i) : COLOR

{

// Sample base texture

float3 cBase = tex2D (tBaseTexture, i.texCoord);

float4 o;

o.rgb = cBase * i.vertexLight; //Final lighting

o.a = 1.0f;

return o;

}

EndPixelShader


Matching names l.jpg
Matching Names and textures, and vertex and pixel shader code.

  • D3DXCompileShader() returns a constant table when a shader is compiled.

  • ID3DXConstantTable has a member function GetConstantDesc() which allows you to get:

    • Name

    • Register Index

    • Type/Size

  • Our shader library matches names to registers to send to SetPixelShaderConstantF() and SetVertexShaderConstantF()


Handling hlsl includes l.jpg
Handling HLSL Includes and textures, and vertex and pixel shader code.

  • Didn’t use HLSL’s #include

  • Variables and textures in includes need to be interpreted by shader library

  • Our parser concatenates the include files with the embedded shader code

  • Shader author needs no knowledge about the contents of the include file other than the function declaration


Using an include file l.jpg
Using an Include file and textures, and vertex and pixel shader code.

StartHLSL

#define SI_SKINNING_MAX_BONES 40

EndHLSL

VsInclude this

VsInclude "SiSkinning.shl"

StartVertexShader(HLSL)

float4x4 mVP;

struct VsInput

{

float4 pos : POSITION0;

float4 weights : BLENDWEIGHT0;

int4 indices : BLENDINDICES0;

float3 normal : NORMAL0;

};

VsOutput main (VsInput i)

{

// Skin position

float4 pos = SiSkin4x4 (i.pos, i.weights, i.indices);

o.pos = mul (pos, mVP);

}

EndVertexShader


Hlsl include example l.jpg
HLSL Include Example and textures, and vertex and pixel shader code.

#replicate ($i, 0, 100, 1)

Matrix mSiWorld$i AppUpdate(world$imat)

#endreplicate

StartHLSL

float4x4 mSiWorld[SI_SKINNING_MAX_BONES];

float4 SiSkin4x4 (float4 aVec, float4 aWeights,

float4 aIndices)

{

float4 vec = (float4)0;

for(int bone = 0; bone < 4; bone++)

vec += (aWeights[bone] * (mul (aVec,

mSiWorld[aIndices[bone]]));

return vec;

}

...

EndHLSL


Concatenation of files l.jpg
Concatenation of Files and textures, and vertex and pixel shader code.

D3DX Compiler

Shader Library

#replicate ($i, 0, 100, 1)

Matrix mSiWorld$i AppUpdate(world$imat)

#endreplicate

StartHLSL

#define SI_SKINNING_MAX_BONES 40

EndHLSL

StartHLSL

float4x4 mSiWorld[SI_SKINNING_MAX_BONES];

float4 SiSkin4x4 (float4 aVec, float4 aWeights,

float4 aIndices)

{

float4 vec = (float4)0;

for(int bone = 0; bone < 4; bone++)

vec += (aWeights[bone] * (mul (aVec,

mSiWorld[aIndices[bone]]));

return vec;

}

Matrix mVP mWvp(MATRIX_WVP)

VsInclude "SiSkinning.shl"

StartVertexShader(HLSL)

float4x4 mVP;

struct VsInput

{

float4 pos : POSITION0;

float4 weights : BLENDWEIGHT0;

int4 indices : BLENDINDICES0;

float3 normal : NORMAL0;

};

VsOutput main (VsInput i)

{

float4 pos = SiSkin4x4 (i.pos, i.weights, i.indices);

o.pos = mul (pos, mVP);

}

EndHLSL

EndVertexShader


Debugging l.jpg
Debugging and textures, and vertex and pixel shader code.

  • Concatenated files means line numbers will not be accurate

  • Added a special tag to dump out the concatenated code:

  • Outputs the concatenated file to the given file name

StartPixelShader(HLSL) HLSLDebugOutput(“dbg.hlsl”)


Textures in hlsl includes l.jpg
Textures in HLSL Includes and textures, and vertex and pixel shader code.

  • Including an HLSL pixel shader also requires implicitly binding textures to texture stages

  • In non-HLSL pixel shaders, we had:

    SetTexture 0 tBaseTexture Trilinear

  • Since we can’t specify the stage to bind the texture to explicitly:

    DefTexture tBaseTexture Trilinear

  • HLSL compiler returns table for matching our DefTextures names with stages


Texture lookup in an include l.jpg
Texture Lookup in an Include and textures, and vertex and pixel shader code.

StartArtNotes

* Anisotropic Strand Lighting map on T2 (24 bit)

EndArtNotes

Texture tSiStrandLighting 2D DXT1("T2", RGB, Box)

DefTexture tSiStrandLighting Linear Clamp(cc)

StartHLSL

sampler tSiStrandLighting;

struct SiStrandPair

{

float diffuse;

float specular;

};

// Compute Wolfgang Heidrich's Anisotropic lighting

SiStrandPair SiComputeStrandLight (float3 normal,

float3 light, float3 view, float3 dirAniso)

{

SiStrandPair sPair;


Texture lookup continued l.jpg
Texture Lookup Continued and textures, and vertex and pixel shader code.

float LdA = dot(light, dirAniso);

float VdA = dot(view, dirAniso);

float2 fnLookup = tex2D(tSiStrandLighting,

(float2(LdA, VdA) * 0.5) + (float2)0.5);

float spec = fnLookup.y * fnLookup.y;

float diff = fnLookup.x;

float selfShadow = saturate(dot(normal,

light));

sPair.diffuse = diff * selfShadow;

sPair.specular = spec * selfShadow;

return(sPair);

}

EndHLSL


Default shader l.jpg
Default Shader and textures, and vertex and pixel shader code.

  • Defines all default state for your engine within a shader file.

  • You don’t need to rely on D3D’s or OpenGL’s default state since you override it with what is most useful to your app. This also helps reduce overall state change.

  • Necessary for cross API development since different API’s have different default state

  • Your shaders ultimately have less redundancy since you can rely on the defaults you set.


A full example environment mapped bumps l.jpg
A Full Example: and textures, and vertex and pixel shader code.Environment Mapped Bumps

StartArtNotes

Supports:

- 3 Fast RT lights

- 3 Object Ambient Lights

- Per-Pixel Specular Exponent

- Gloss Map

- Environment map cube map

- Bump map

Requirements:

- Color = Set Editable Color (vBaseColor)

- Bump = RGB normal map

- Gloss = GRAY Texture (Gloss Map)

- SpecularExp = GRAY Texture (Specular Exponent Map)

- T2 = RGB Cube Map (Environment Map)

EndArtNotes

StartMisc

Animation Skinned(40)

NumFastRTLights 3

EndMisc


Example continued l.jpg
Example Continued and textures, and vertex and pixel shader code.

// TEXTURES

Texture tGloss 2D GRAY("Gloss", GRAY, Box)

Texture tSpec 2D GRAY("SpecularExp", GRAY, Box)

Texture tEnv CM RGB("T2", RGB, Box)

Texture tBump 2D RGB("Bump", RGB, Box)

DefTexture tBump Trilinear

DefTexture tSpec Trilinear

DefTexture tGloss Trilinear

DefTexture tEnv Trilinear

// VARIABLES

Matrix mVP(VP)

Vector worldCamPos(CameraPosition, WorldSpace) Vector vBaseColor(.8, .8, .8, 0) Editable(color)


Example continued41 l.jpg
Example Continued and textures, and vertex and pixel shader code.

// STREAMS

StartStream s1 Normal

float3 POSITION Position

float4 BLENDWEIGHT BlendWeight

ubyte4 BLENDINDICES BlendIndex

float3 NORMAL Normal

float3 TANGENT0 Tangent("Bump")

float3 BINORMAL0 Binormal("Bump")

float2 TEX0 UV("Bump")

EndStream

StreamMap sm1(s1)

// Global HLSL block

StartHLSL

#define SI_SKINNING_MAX_BONES 40

#define NUM_OBJECT_AMBIENT_LIGHTS 3

#define SPECULAR_K_MIN 16

#define SPECULAR_K_MAX 256

EndHLSL


Example continued42 l.jpg
Example Continued and textures, and vertex and pixel shader code.

StartShader "NormalFastRT1"

Property "Normal"

Property "FastRT1"

StartPass "Pass1"

SetStreamMap sm1

VsInclude this

VsInclude "SiRTLight.shl"(VS)

VsInclude "SiObjAmbLight.shl"

VsInclude "SiSkinning.shl"

VsInclude "SiMath.shl"(Misc)

StartVertexShader(HLSL)

float4x4 mVP;

float3 worldCamPos;


Example continued43 l.jpg
Example Continued and textures, and vertex and pixel shader code.

struct VsInput

{

float4 pos : POSITION0;

float4 weights : BLENDWEIGHT0;

float4 indices : BLENDINDICES0;

float3 normal : NORMAL0;

float3 tangent : TANGENT0;

float3 binormal : BINORMAL0;

float2 texCoord : TEXCOORD0;

};

struct VsOutput

{

float4 pos : POSITION0;

float2 texCoord : TEXCOORD0;

float3 lightVec0TS : TEXCOORD1;

float3 lightSpacePos0 : TEXCOORD2;

float3 viewVecTS : TEXCOORD3;

float3 invNormal : TEXCOORD4;

float3 invTangent : TEXCOORD5;

float3 invBinormal : TEXCOORD6;

float3 vertexLight : COLOR0;

};


Example continued44 l.jpg
Example Continued and textures, and vertex and pixel shader code.

VsOutput main (VsInput i)

{

VsOutput o;

o.texCoord = i.texCoord;

float3x3 mTangent = 0;

// Skin

float4 pos = SiSkin4x4 (i.pos, i.weights, i.indices);

o.pos = mul (pos, mVP);

mTangent[0] = SiSkin3x3 (i.tangent, i.weights, i.indices);

mTangent[1] = SiSkin3x3 (i.binormal, i.weights, i.indices);

mTangent[2] = SiSkin3x3 (i.normal, i.weights, i.indices);

// Invert tangent space

float3x3 mInvTangent = transpose(mTangent);

o.invTangent = mInvTangent[0];

o.invBinormal = mInvTangent[1];

o.invNormal = mInvTangent[2];

// Compute View Vector

float3 viewVec = worldCamPos - i.pos;

viewVec = normalize (viewVec);

o.viewVecTS = mul (mTangent, viewVec);


Example continued45 l.jpg
Example Continued and textures, and vertex and pixel shader code.

// Compute ambient lighting

float3 vertexLight = 0.0f;

for (int idx = 0; idx < NUM_OBJECT_AMBIENT_LIGHTS; idx++)

{

vertexLight += SiComputeObjectAmbientLight (pos,

mTangent[2],

idx);

}

o.vertexLight = vertexLight;

// Compute runtime light vectors for RT1

o.lightSpacePos0 = SiComputeRTLightSpacePosition (pos, 0);

float3 lightVec0 = SiComputeRTLightVectorNormalized (pos,0);

o.lightVec0TS = mul (mTangent, lightVec0);

return o;

}

EndVertexShader


Example continued46 l.jpg
Example Continued and textures, and vertex and pixel shader code.

PsInclude this

PsInclude "SiRTLight.shl"(PS)

PsInclude "SiMath.shl"(Misc)

StartPixelShader(HLSL)

sampler tBump;

sampler tGloss;

sampler tEnv;

sampler tSpec;

float4 vBaseColor;

struct PsInput

{

float2 texCoord : TEXCOORD0;

float3 lightVec0TS : TEXCOORD1;

float3 lightSpacePos0 : TEXCOORD2;

float3 viewVecTS : TEXCOORD3;

float3 invNormal : TEXCOORD4;

float3 invTangent : TEXCOORD5;

float3 invBinormal : TEXCOORD6;

float3 vertexLight : COLOR0;

};


Example continued47 l.jpg
Example Continued and textures, and vertex and pixel shader code.

float4 main (PsInput i) : COLOR

{

// Create arrays of light vectors and positions

#define NUM_RT_LIGHTS 1

float3 vLightVec[NUM_RT_LIGHTS] = {i.lightVec0TS};

float3 vLightPos[NUM_RT_LIGHTS] = {i.lightSpacePos0};

// Sample normal map

float3 vNormal = tex2D (tBump, i.texCoord);

vNormal = SiConvertColorToVector (vNormal);

// Compute reflection vector

float3 reflectionVec = SiReflect (i.viewVecTS, vNormal);

// Sample Exponent and Gloss Map

float exponent = tex2D (tSpec, i.texCoord);

float gloss = tex2D (tGloss, i.texCoord);


Example continued48 l.jpg
Example Continued and textures, and vertex and pixel shader code.

// Loop over runtime lights computing light contributions

float3 diffuse = i.vertexLight * vNormal.z;

float3 specular = 0;

for (int idx = 0; idx < NUM_RT_LIGHTS; idx++)

{

float3 colorIntensity = SiComputeRTLightColorIntensity

(vLightPos[idx], lightIdx);

float diffuseNdotL = SiDot3Clamp (vNormal,

vLightVec[idx]);

diffuse += colorIntensity * diffuseNdotL;

float specularRdotL = SiComputeSpecular (reflectionVec,

vLightVec[idx], exponent,

SPECULAR_K_MIN, SPECULAR_K_MAX);

specular += colorIntensity * specularRdotL;

}


Example continued49 l.jpg
Example Continued and textures, and vertex and pixel shader code.

// Rotate reflection vector to object space

float3x3 mInvTangent = {i.invTangent, i.invBinormal, i.invNormal};

float3 reflectionVecOS = mul (mInvTangent, reflectionVec);

// sample environment map

float3 cEnv = texCUBE (tEnv, reflectionVecOS);

// Scale env map by fresnel and add to specular contribution

float fresnel = SiComputeFresnelApprox (vNormal, i.viewVecTS);

float specularEnv = cEnv * fresnel * gloss;

specular *= gloss;

// Compute final color

float4 o;

o.rgb = (vBaseColor * diffuse)+ (specular + specularEnv);

o.a = 0.0;

return o;

}

EndPixelShader

EndPass

EndShader


Summary l.jpg
Summary and textures, and vertex and pixel shader code.

  • Why a shader library is needed

  • Goals of designing a shader library

  • Case study: ATI’s demo shader format

  • Modifications for HLSL (it wasn’t that painful)


Questions l.jpg
Questions? and textures, and vertex and pixel shader code.

[email protected]


ad