CSCE 441 Lecture 36

From Notes
Jump to navigation Jump to search

« previous | Monday, April 21, 2014 | next »


Graphics Processors

Massive, insane parallel processing machines!

Graphics Pipeline

  1. Vertices are input
  2. Transformation / Lighting
  3. Viewport Transformation
  4. Triangle Setup (If triangles are used as input, they come in here)
  5. Backface Culling
  6. Clipping
  7. Interpolation / Rasterization
  8. (data in pipeline is now pixel location, color, and depth)
  9. Visibility determination
  10. Frame Buffer


Programmable Shaders

Transformation / Lighting becomes Vertex Shader

Pixel Shader outputs color and depth, while interpolation/rasterization outputs

  • just pixel location to visibility determination
  • vertex data to the pixel shader


Shader Programming Langages

  • Assembly
  • OpenGL Shading Language (latest release)
  • Nvidia's CG
  • Microsoft's HLSL (looks a lot like Nvidia CG)

Different shader models:

  • vertex (and different versions therein)
  • shader (and different versions therein)

Different model capabilities (levels are nested)

  • register counts
  • instructions
  • maximum number of instructions


Vertex Shaders

Input: anything associated with vertices:

  • position
  • normal
  • texture coordinates

Output: transformed vertices; must output position


Example (HLSL

struct VS_OUTPUT
{
    float4 Pos : POSITION;      // position output goes here
}

VS_OUTPUT VS(
    float3 inPOS : POSITION     // vertex position in model space
)
{
    VS_OUTPUT Out = (VS_OUTPUT)0;   // set all output entries to 0

    // transform position
    float3 transformedPos = mul(float(InPos, 1), (float4x3)World);      // multiply vertex position by model-view matrix, specified with OpenGL
    Out.Pos = mul(float4(transformedPos, 1), ViewProjection);           // project vertex onto screen.

    return Out;
}


Pixel Shader

Input: (interpolated) Vertex data produced from vertex shader

Output: mandatory color; can also output depth, but cannot change location on screen

Example (HLSL)

float4 PS (VS_OUTPUT In) : COLOR
{
    // may perform texture lookup, depth effects, fog, etc...
    return float4(1,1,1,1);     // make everything white
}


Gouraud Shading Example

// vertex shader

struct VS_OUTPUT
{
    float4 Pos : POSITION;
    float4 Color : COLOR;
};

VS_OUTPUT VS(float3 InPos : POSITION, flaot3 InNormal : NORMAL)
{
    VS_OUTPUT Out = (VS_OUTPUT)0;

    // transform the position and normal
    float3 transformedPos = mul(float4(InPos, 1), (float4x3)World);
    Out.Pos = mul(float4(transformedPos, 1), ViewProjection);

    float3 transNormal = mul(InNormal, (float3x3)World); // this line of code is not generally correct, but the inverse transpose of a rotation matrix is the same matrix.

    Out.Color = float4(calcColor(normalize(lightPos - transformedPos), transNormal, normalize(eyePos - transformedPos)), 1);
    return Out;
}

float3 calcColor(float3 lightVec, float3 normal, float3 eyeToVertex)
{
    float3 color = 0;
    
    color += lightColor * MaterialAmbient; // component-wise multiplication
    color += lightColor * MaterialDiffuse * max(0, dot(normal, lightVec));
    float3 R = normalize(reflect(lightVec, normal));  // reflect is built-in!
    color += lightColor * MaterialSpecular * pow(max(0, dot(R, eyeToVertex)), MaterialSpecularPower);

    return color;
}


// pixel shader
float4 PS(VS_OUTPUT In) : COLOR
{
    return In.Color; // interpolation takes care of interpolating the color over the polygon.
}


Phong Shading Example

struct VS_OUTPUT
{
    float4 Pos : POSITION;
    float3 Normal : TEXCOORD0;          // normal stored as texture coordinate
    float3 TransformedPos : TEXCOORD1;  // need 3D position for lighting equation
};

// why not normal or color? (values clamped between 0 and 1)


VS_OUTPUT VS(float3 InPos : POSITION, float3 InNormal : NORMAL)
{
    VS_OUTPUT Out = (VS_OUTPUT) 0;

    // transform position and normal
    Out.TransformedPos = ...
}

float4 PS(VS_OUTPUT In) : COLOR
{
    float3 EyeToVertex = normalize(In.TransformedPos - EyePos);
    float3 normal = normalize(In.Normal);

    float4 color = calcColor(normalize(lightPos - In.TransformedPos), normal, EyeToVertex);

    return color;
}


Runtime is dependent on scene:

  • Pixel shader is like an inner loop: typically called multiple times per polygon
  • Vertex shader takes raw input; if vertex mesh is extremely dense, the vertex shader could be called more often


General Purpose GPU Programming

Originally, success was limited because problems had to be crammed into graphics pipeline.

At some point, people realized that this was stupid. Dr. Scott Schaefer

General purpose computation is now available:

  • Nvidia's CUDA
  • DirectX Compute
  • OpenCL