51
Pixel Shader Based on nVIDIA’s GF3/4 architecture Texture shader + register combiner texture unit 0 texture program texture unit 1 texture program texture unit 2 texture program texture unit 3 texture program fragment color input register combiner fragment color output texture shader

Pixel Shader

  • Upload
    bazyli

  • View
    76

  • Download
    1

Embed Size (px)

DESCRIPTION

Pixel Shader. Based on nVIDIA’s GF3/4 architecture Texture shader + register combiner. texture shader. fragment color input. texture unit 0. texture program. texture unit 1. texture program. texture unit 2. texture program. texture unit 3. texture program. register combiner. - PowerPoint PPT Presentation

Citation preview

Page 1: Pixel Shader

Pixel Shader Based on nVIDIA’s GF3/4 architecture Texture shader + register combiner

texture unit 0 texture program

texture unit 1 texture program

texture unit 2 texture program

texture unit 3 texture program

fragment color input

register combinerfragment color output

texture shader

Page 2: Pixel Shader

Texture Shader “Bridge”

(s0,t0,r0,q0)

(s1,t1,r1,q1)

(s2,t2,r2,q2)

(s3,t3,r3,q3)

(R0,G0,B0,A0)

(R1,G1,B1,A1)

(R2,G2,B2,A2)

(R3,G3,B3,A3)Interpolated texture

coordinate sets32-bit IEEE floating-point

Per-component

RGBA colors8-bit [0,1] or [-1,1)

fixed-pointPer-component

Texture shader and Texture fetch units

State

Page 3: Pixel Shader

Texture Shaders

Provides a superset of conventional OpenGL texture addressing

Five main categories of shader operations– Conventional textures

• 1D, 2D, texture rectangle, cube map– Special modes

• none, pass through, cull fragment– Direct dependent textures

• dependent AR, dependent GB, offset, offset scaled– Dot product (DP) dependent textures

• DP 2D, DP texture rectangle, DP cube map, DP reflect cube map, DP diffuse cube map

– Depth replace operations

Page 4: Pixel Shader

Conventional Textures

TexTex##

Texture Coords(S,T,R,Q)

ShaderOperations

TextureFetch

Texture 2D

2DAny Format

( , )

Bound Texture Target/Format

Qi

Si

Qi

Ti(Si,Ti,Ri,Qi)ii

OutputColor

(R,G,B,A)

Texture program: 2D texture mapping

Page 5: Pixel Shader

Texture Rectangle

TexTex##

Texture Coords(S,T,R,Q)

ShaderOperations

TextureFetch

Texture Rectangle

Texture RectangleAny Format

( , )

Bound Texture Target/Format

Qi

Si

Qi

Ti(Si,Ti,Ri,Qi)ii

OutputColor

(R,G,B,A)

Texture program: No need to have power of 2 texture

Page 6: Pixel Shader

Texture Cube Map

TexTex##

Texture Coords(S,T,R,Q)

ShaderOperations

TextureFetch

Texture Cube Map

U=(Si, Ti, Ri)

Bound Texture Target/Format

(Si,Ti,Ri)ii

Cube Map Any Format

U

OutputColor

(R,G,B,A)

Page 7: Pixel Shader

Special Modes

Texture program: pass through

ShaderOperations

TextureFetch

OutputColor

(Si,Ti,Ri,Qi) None

Bound Texture Target/Format

(R,G,B,A)None

R = Clamp0to1(Si)

G = Clamp0to1(Ti)

B = Clamp0to1(Ri)

A = Clamp0to1(Qi)

Page 8: Pixel Shader

Special Modes

TexTex##

Texture Coords(S,T,R,Q)

ShaderOperations

TextureFetch

Bound Texture Target/Format

Ignoredii NoneNone

OutputColor

(R,G,B,A)

R = 0

G = 0

B = 0

A = 0

Texture program: none

Page 9: Pixel Shader

Texture program: Cull Fragment

• Cull the fragment based upon sign of texture coords– each tex coord (STRQ) has its own settable condition– each of the 4 conditions is set to one of the following:

• GL_GEQUAL (tex coord ≥ 0) – pass iff positive or zero• GL_LESS (tex coord < 0) – pass iff negative

– all four tex coords are tested– if any of the four fail, the fragment is rejected

• Texture output for passing fragments is (0,0,0,0)

Special Modes

Page 10: Pixel Shader

Cull FragmentApplications

• Per-fragment clip planes– Up to 4 clip planes per texture unit– 16 clip planes maximum

• Non-planar clipping approaches also possible– Vertex programs can compute a distance to a

point or line and use that interpolated distance for clipping

Page 11: Pixel Shader

Cull FragmentExamples

Clipping a model to two texture shader clip planes

Clipping a 3D grid of cubes based on distance from a point

Page 12: Pixel Shader

Dependent Texture Shaders

• Take results of one texture, use them for addressing subsequent texture

• Simple dependent textures (single stage)– Dependent alpha-red– Dependent green blue– Offset texture 2D– Offset texture 2D scaled

Page 13: Pixel Shader

Dependent Alpha-Red Texturing

00

TexTex##

Texture Coords(S,T,R,Q)

ShaderOperations

TextureFetch

OutputColor

App specific Texture specific

Any type Unsigned RGBA

R0G0B0A0

11 (A0,R0)

Texture specific

2DRGBA

Bound Texture Target/Format

R1G1B1A1Ignored None

Page 14: Pixel Shader

Dependent Green-Blue Texturing

00

TexTex##

Texture Coords(S,T,R,Q)

ShaderOperations

TextureFetch

OutputColor

App specific Texture specific

Any type Unsigned RGB[A]

R0G0B0A0

11 (G0,B0)

Texture specific

2DRGBA

Bound Texture Target/Format

R1G1B1A1Ignored None

Page 15: Pixel Shader

Offset Texture 2D

• Use previous lookup (a signed 2D offset) to perturb the texture coordinates of a subsequent (non-projective) 2D texture lookup

• Signed 2D offset is transformed by user-defined 2x2 matrix (shown in the following diagrams as constants k0-k3)

• This 2x2 constant matrix allows for arbitrary rotation/scaling of offset vector

• Offset defined in DS/DT texture

Page 16: Pixel Shader

Offset Texture 2D

00

TexTex##

Texture Coords(S,T,R,Q)

ShaderOperations

TextureFetch

OutputColor

(S0,T0,R0,Q0)Texture

2D

2D DSDT

11

( , )

Bound Texture Target/Format

(0,0,0,0)Q0

S0

Q0

T0

(S1,T1) (S1’, T1’ )S1’ = S1+ k0*ds + k2*dtT1’ = T1+ k1*ds + k3*dt

(ds,dt)

R1G1B1A1

2D Any Format

k0, k1, k2 and k3 define a constant 2x2 floating-point matrix set by glTexEnv

Page 17: Pixel Shader

Offset Texture 2D Scale

• Same as Offset Texture 2D, except that subsequent (non-projective) 2D texture RGB output is scaled

• Scaling factor is the MAG component (from previous texture) scaled/biased by user-defined constants (kscale and kbias in the following diagrams)

• Alpha component is NOT scaled

Page 18: Pixel Shader

(R*M,

G*M,

B*M,A)

Offset Texture 2D Scale

00

TexTex##

Texture Coords(S,T,R,Q)

ShaderOperations

TextureFetch

OutputColor

(S0,T0,R0,Q0)Texture

2D

2DDSDT_Mag

11

( , )

Bound Texture Target/Format

(0,0,0,0)Q0

S0

Q0

T0

(S1,T1) (S1’, T1’ )S1’ = S1+ k0*ds + k2*dtT1’ = T1+ k1*ds + k3*dt

(ds,dt,mag)

2D RGBA

M = kscale* mag + kbias

k0, k1, k2 and k3 define a constant 2x2 floating-point matrix set by glTexEnv kscale and kbias define constant floating-point scale/bias set by glTexEnv

Page 19: Pixel Shader

Dot ProductDependent Texture Shaders

• Take results of one texture, perform 2 or 3 dot products with it and incoming texture coordinates, then use results for addressing subsequent texture(s)

• Multiple contiguous stages, not including source texture

Page 20: Pixel Shader

Dot Product Texture 2D

00

TexTex##

Texture Coords(S,T,R,Q)

ShaderOperations

TextureFetch

OutputColor

App specific Texture specific

Any type Signed RGB[A]

R0G0B0

11 None (0,0,0,0)None

22 (Ux, Uy)

Texture specific

2D RGBA

Uy =[S2, T2, R2] • [R0,G0,B0]

Ux =[S1,T1,R1] • [R0,G0,B0]

Bound Texture Target/Format

(S1, T1, R1)

(S2, T2, R2) R2G2B2A2

Page 21: Pixel Shader

Dot Product Texture 2D Application

• High-quality bump-mapping– 2D HILO texture stores normals

• Per-fragment tangent-space normal, N– Vertex programs supplies tangent-space light (L)

and half-angle (H) vectors in (s,t,r) texture coordinates

– Two dot products compute• Diffuse L dot N• Specular H dot N

– Illumination stored in 2D texture accessed byL dot N and H dot N

• Excellent specular appearance

Page 22: Pixel Shader

HILO Normal Map Dot Product Texture 2D Bump

Mapping

Bump mapping the Holy Grail

Page 23: Pixel Shader

Dot Product Texture 3D

00

TexTex##

Texture Coords(S,T,R,Q)

ShaderOperations

TextureFetch

OutputColor

App specific Texture specific

Any type Signed RGB[A]

R0G0B0

11 None (0,0,0,0)None

22 None

33 R3G3B3A3

Texture specific

(0,0,0,0)None

3D RGBA

T =[S2,T2,R2] • [R0,G0,B0]

S =[S1,T1,R1] • [R0,G0,B0]

Bound Texture Target/Format

(S3, T3, R3)

(S2, T2, R2)

(S1, T1, R1)

R =[S3,T3,R3] • [R0,G0,B0] (S,T,R)

Page 24: Pixel Shader

Texture Shader Precision

• Interpolated texture coordinates are IEEE 32-bit floating-point values

• Texture projections, dot products, texture offset, post-texture offset scaling, reflection vector, and depth replace division computations are performed in IEEE 32-bit floating-point

• HILO texture components are filtered as 16-bit values

• DSDT, MAG, intensity, and color components are filtered as 8-bit values

Page 25: Pixel Shader

Register Combiner

GeForce 2 (only 2 general combiner stages)

Spare 0

Fragment Color

TextureFetching

GeneralCombiner

0

4 RGB Inputs

Texture 0

Texture 1

Fog Color/Factor

Reg

iste

r S

etR

egis

ter

Set

6 RGB Inputs

Specular Color

4 Alpha Inputs

3 RGB Outputs

3 Alpha Outputs

GeneralCombiner

1

4 RGB Inputs

4 Alpha Inputs

3 RGB Outputs

3 Alpha Outputs

FinalCombiner

1 Alpha Input

Specular Color

Page 26: Pixel Shader

Register-based programming– All textures and colors available for each and every

texture blending stage– 8 Stages of blending in hardware, plus specular and

fog• Note that GeForce3 has 8 combiners, and 4

textures.– Signed color arithmetic

Register Combiner (cont’d)

Page 27: Pixel Shader

Diagram of a General Combiner

Input RGB, Alpha Registers

Input Alpha, Blue Registers

InputMappings

InputMappings

A

B

C

D

A op1 B

C op2 D

AB op3 CD

RGB Function

A

B

C

D

AB

CD

AB op4 CD

Alpha Function

RGBScale/Bias

AlphaScale/Bias

Next Combiner’sRGB Registers

Next Combiner’sAlpha Registers

RGBPortion

AlphaPortion

Page 28: Pixel Shader

General Combiner Input Registers

Input RGB, Alpha Registers

Input Alpha, Blue Registers

InputMappings

InputMappings

A

B

C

D

A op1 B

C op2 D

AB op3 CD

RGB Function

A

B

C

D

AB

CD

AB op4 CD

Alpha Function

RGBScale/Bias

AlphaScale/Bias

Next Combiner’sRGB Registers

Next Combiner’sAlpha Registers

RGBPortion

AlphaPortion

Page 29: Pixel Shader

The Register Set

Primary (diffuse) color• initialized to RGBA of fragment’s primary color

Secondary (specular) color• initialized to RGB of fragment’s secondary/specular color• alpha not initialized

Texture 0 and Texture 1 colors• initialized to fragment’s filtered RGBA texel from numbered texture unit• not initialized if numbered texture unit is disabled or non-existent

Spare 0 and Spare 1• Alpha of Spare 0 is initialized to alpha of Texture 0 color (if enabled)• RGB of Spare 0 and all of Spare 1 is not initialized

Fog• RGB is current fog color• alpha is fragment’s fog factor (only available in final combiner)• read-only

Constant color 0 and Constant color 1• initialized to user-defined RGBA value• read-only

Zero• constant, read-only value of zero

Page 30: Pixel Shader

General Combiner Input Mappings

Input RGB, Alpha Registers

Input Alpha, Blue Registers

InputMappings

InputMappings

A

B

C

D

A op1 B

C op2 D

AB op3 CD

RGB Function

A

B

C

D

AB

CD

AB op4 CD

Alpha Function

RGBScale/Bias

AlphaScale/Bias

Next Combiner’sRGB Registers

Next Combiner’sAlpha Registers

RGBPortion

AlphaPortion

Page 31: Pixel Shader

General Combiner Input Mappings

Signed Identity

f(x) = x

[-1, 1] [-1, 1]

Unsigned Identity

f(x) = max(0, x)

[0, 1] [0, 1]

Expand Normal

f(x) = 2 * max(0, x) - 1

[0, 1] [-1, 1]

Half Bias Normal

f(x) = max(0, x) – ½

[0, 1] [-½, ½]

Signed Negate

f(x) = -x

[-1, 1] [1, -1]

Unsigned Invert

f(x) = 1-min(max(0,x),1)

[0, 1] [1, 0]

Expand Negate

f(x) = -2 * max(0, x) + 1

[0, 1] [1, -1]

Half Bias Negate

f(x) = -max(0, x) + ½

[0, 1] [½, -½]

-1

0

1

-1 0 1

-1

0

1

-1 0 1

-1

0

1

-1 0 1

-1

0

1

-1 0 1

-1

0

1

-1 0 1

-1

0

1

-1 0 1

-1

0

1

-1 0 1

-1

0

1

-1 0 1

Page 32: Pixel Shader

General Combiner RGB Function

Input RGB, Alpha Registers

Input Alpha, Blue Registers

InputMappings

InputMappings

A

B

C

D

A op1 B

C op2 D

AB op3 CD

RGB Function

A

B

C

D

AB

CD

AB op4 CD

Alpha Function

RGBScale/Bias

AlphaScale/Bias

Next Combiner’sRGB Registers

Next Combiner’sAlpha Registers

RGBPortion

AlphaPortion

Page 33: Pixel Shader

General Combiner RGB Functions

A

B

C

D

AB

CD

AB + CD

A

B

C

D

AB

CD

mux(AB, CD)

A

B

C

D

A • B

C • D

Dot / Dot / Discard

A

B

C

D

A • B

CD

A

B

C

D

AB

C • D

Dot / Mult / Discard Mult / Dot / Discard

Mult / Mult / SumMult / Mult / Mux

mux(AB, CD) = (Spare0[Alpha] ½) ? AB : CD

Dot products on RGB registers: A • B = (A[red] * B[red] + A[green] * B[green] + A[blue] * B[blue],

A[red] * B[red] + A[green] * B[green] + A[blue] * B[blue],A[red] * B[red] + A[green] * B[green] + A[blue] * B[blue])

Multiplication on RGB registers: AB = (A[red] * B[red], A[green] * B[green], A[blue] * B[blue])

Page 34: Pixel Shader

General Combiner Alpha Function

Input RGB, Alpha Registers

Input Alpha, Blue Registers

InputMappings

InputMappings

A

B

C

D

A op1 B

C op2 D

AB op3 CD

RGB Function

A

B

C

D

AB

CD

AB op4 CD

Alpha Function

RGBScale/Bias

AlphaScale/Bias

Next Combiner’sRGB Registers

Next Combiner’sAlpha Registers

RGBPortion

AlphaPortion

Page 35: Pixel Shader

General Combiner Alpha Functions

A

B

C

D

AB

CD

AB + CD

A

B

C

D

AB

CD

mux(AB, CD)

Mult / Mult / SumMult / Mult / Mux

mux(AB, CD) = (Spare0[alpha] ½) ? AB : CD

Page 36: Pixel Shader

General Combiner Scale and Bias

Input RGB, Alpha Registers

Input Alpha, Blue Registers

InputMappings

InputMappings

A

B

C

D

A op1 B

C op2 D

AB op3 CD

RGB Function

A

B

C

D

AB

CD

AB op4 CD

Alpha Function

RGBScale/Bias

AlphaScale/Bias

Next Combiner’sRGB Registers

Next Combiner’sAlpha Registers

RGBPortion

AlphaPortion

Page 37: Pixel Shader

General Combiner Scale and Bias Options

f(x) = x/2 f(x) = x f(x) = 2x f(x) = 4x

Not supported

f(x) = x – ½ f(x) = 2(x – ½)

Not supported

-101

-1 0.5

-101

-1 0.5

-101

-1 0.5

-101

-1 0.5

-101

-1 0.5

-101

-1 0 1

Bias by –½

No bias

Scale by ½ No scale Scale by 2 Scale by 4

Scale and bias operation is defined as:ClampNegativeOneToOne( Scale * (x + Bias) )

OR max(min(Scale * (x + Bias), 1), -1)

Page 38: Pixel Shader

General Combiner Output Registers

Input RGB, Alpha Registers

Input Alpha, Blue Registers

InputMappings

InputMappings

A

B

C

D

A op1 B

C op2 D

AB op3 CD

RGB Function

A

B

C

D

AB

CD

AB op4 CD

Alpha Function

RGBScale/Bias

AlphaScale/Bias

Next Combiner’sRGB Registers

Next Combiner’sAlpha Registers

RGBPortion

AlphaPortion

Page 39: Pixel Shader

Up to six outputs can be specified per general combiner:• three RGB outputs (A op1 B, C op2 D, AB op3 CD) written to RGB portion of writable registers• three Alpha outputs (AB, CD, AB op4 CD) written to Alpha portion of writable registers

RGB outputs must be written to distinct registers (that is, two outputs cannot be written to one register)

Alpha outputs must be written to distinct registers

Any output can be discarded

Those RGB functions performing dot products must discard the third result (Dot/Dot/Discard, Dot/Mult/Discard, Mult/Dot/Discard)

General Combiner Output Registers

Page 40: Pixel Shader

Diagram of the Final Combiner (OpenGL only)

Input RGB, Alpha Registers

Input Alpha, Blue Registers

InputMappings

InputMapping

A

B

C

D

AB + (1-A)C + D

RGB Function

RGBPortion

AlphaPortion

RGBOut

AvailableRGB Inputs

AlphaOut

E

F

EF

Spare0

2nd-aryColor

Sum

Clamp to [0, 1]

InputMappings

Color Sum Unit

Multiplier

Page 41: Pixel Shader

Final Combiner Input Registers

Input RGB, Alpha Registers

Input Alpha, Blue Registers

InputMappings

InputMapping

A

B

C

D

AB + (1-A)C + D

RGB Function

RGBPortion

AlphaPortion

RGBOut

AvailableRGB Inputs

AlphaOut

E

F

EF

Spare0

2nd-aryColor

Sum

Clamp to [0, 1]

InputMappings

Color Sum Unit

Multiplier

Page 42: Pixel Shader

Final Combiner Input Mappings

Input RGB, Alpha Registers

Input Alpha, Blue Registers

InputMappings

InputMapping

A

B

C

D

AB + (1-A)C + D

RGB Function

RGBPortion

AlphaPortion

RGBOut

AvailableRGB Inputs

AlphaOut

E

F

EF

Spare0

2nd-aryColor

Sum

Clamp to [0, 1]

InputMappings

Color Sum Unit

Multiplier

Page 43: Pixel Shader

Final Combiner Input Mappings

Unsigned Identity

f(x) = max(0, x)

[0, 1] [0, 1]

Unsigned Invert

f(x) = 1-min(max(0,x),1)

[0, 1] [1, 0]

-101

-1 0.5

-101

-1 0.5

Page 44: Pixel Shader

Final Combiner EF Multiplier

Input RGB, Alpha Registers

Input Alpha, Blue Registers

InputMappings

InputMapping

A

B

C

D

AB + (1-A)C + D

RGB Function

RGBPortion

AlphaPortion

RGBOut

AvailableRGB Inputs

AlphaOut

E

F

EF

Spare0

2nd-aryColor

Sum

Clamp to [0, 1]

InputMappings

Color Sum Unit

Multiplier

Page 45: Pixel Shader

Final Combiner EF Multiplier

E

F

E F

Multiplication on RGB registers: E F = (E[red] * F[red], E[green] * F[green], E[blue] * F[blue])

Page 46: Pixel Shader

Final Combiner Color Sum and Optional Clamp

Input RGB, Alpha Registers

Input Alpha, Blue Registers

InputMappings

InputMapping

A

B

C

D

AB + (1-A)C + D

RGB Function

RGBPortion

AlphaPortion

RGBOut

AvailableRGB Inputs

AlphaOut

E

F

EF

Spare0

2nd-aryColor

Sum

Clamp to [0, 1]

InputMappings

Color Sum Unit

Multiplier

Page 47: Pixel Shader

Final Combiner Color Sum and Optional Clamp

Spare0

SecondaryColor

Clamp(Spare0) +Clamp(SecondaryColor)

Clamp to [0, 1]

Color Sum Unit

Inputs to this unit are hardwired to Spare0 and SecondaryColor registers

Each input to the color sum unit undergoes an unsigned identity mapping before addition

Unsigned identity = max(0, x), clamping negative input values to 0

Addition on RGB registers: Spare0 + SecondaryColor =(Spare0[red] + SecondaryColor[red], Spare0[green] + SecondaryColor[green], Spare0[blue] + SecondaryColor[blue])

Output range for sum is [0, 2]

Optional clamping unit clamps the sum to [0, 1]

Page 48: Pixel Shader

Final Combiner RGB Input Set

Input RGB, Alpha Registers

Input Alpha, Blue Registers

InputMappings

InputMapping

A

B

C

D

AB + (1-A)C + D

RGB Function

RGBPortion

AlphaPortion

RGBOut

AvailableRGB Inputs

AlphaOut

E

F

EF

Spare0

2nd-aryColor

Sum

Clamp to [0, 1]

InputMappings

Color Sum Unit

Multiplier

Page 49: Pixel Shader

Final Combiner RGB Input Set

All registers available to all RGB function inputs (A, B, C and D)

Result of EF multiplier also available to all RGB function inputs

Result of (possibly clamped) color sum available to B, C and D function inputs (but not A)

Neither the result of the EF multiplier nor the color sum is available to the Alpha portion

Page 50: Pixel Shader

Final Combiner RGB Function

Input RGB, Alpha Registers

Input Alpha, Blue Registers

InputMappings

InputMapping

A

B

C

D

AB + (1-A)C + D

RGB Function

RGBPortion

AlphaPortion

RGBOut

AvailableRGB Inputs

AlphaOut

E

F

EF

Spare0

2nd-aryColor

Sum

Clamp to [0, 1]

InputMappings

Color Sum Unit

Multiplier

Page 51: Pixel Shader

Final Combiner RGB Function

A

B

C

D

AB + (1-A)C + D RGB Out

Unlike the general combiner, only one function is supported in the final combiner

Addition/multiplication on RGB registers as defined previously

Output range for RGB function is [0, 2]

RGB output combined with Alpha output to form a single RGBA fragment

RGBA fragment continues with subsequent processing (Z-buffering, blending, ...)