hardware shaders

24
Hardware Shaders Paul Taylor 2009

Upload: amethyst-farrell

Post on 02-Jan-2016

60 views

Category:

Documents


1 download

DESCRIPTION

Hardware Shaders. Paul Taylor 2009. What is a Hardware Shader. We send Verticies to the Video card and Textures / Colours - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Hardware Shaders

Hardware Shaders

Paul Taylor 2009

Page 2: Hardware Shaders

What is a Hardware Shader

• We send Verticies to the Video card and Textures / Colours

• The Video Card applies the ModelView translations and Matrix Translations to each vertex, then passes them on to get the Pixels (Fragments) rendered to the screen buffer

• These are the 2 default Shaders that video cards have used for decades.

Page 3: Hardware Shaders

Types of Shaders

• Vertex• Pixel (Fragment)• Common Shader Core (HLSL 4.0)– A combination of Pixel and Vertex Shading

allowing multi-pass rendering on the GPU

Page 4: Hardware Shaders

http://nehe.gamedev.net/data/articles/article.asp?article=21

Page 5: Hardware Shaders

GLSL 101

Data TypesVec2, Vec3, Vec4 // Floating Point VectorsIvec2, Ivec3, Ivec4 // Integer VectorsBvec2, Bvec3, Bvec4 // Boolean VectorsMat2, Mat3, Mat4 // Floating Point Matrices

Page 6: Hardware Shaders

sampler1D, sampler2D, sampler3D // 1D, 2D and 3D texture samplerCube // Cube Map texturesampler1Dshadow, sampler2Dshadow // 1D and 2D depth-component texture// Aka Bump maps and Normal Maps

Page 7: Hardware Shaders

Inputs and Outputs

Uniforms AttributesVaryings

Page 8: Hardware Shaders

Uniforms

• Uniforms are read-only• They do not change during a render• Uniforms apply to both Vertex and Pixel

Shaders• Eg light values, light colour,

currentWindVelocity

Page 9: Hardware Shaders

Attributes

• Attributes apply to the Vertex Shader only• The Value of an attribute can change on a per-

vertex frequency• An Attribute is a input value which is read only

to the Vertex Shader• Eg: Vertex position, normal vector

Page 10: Hardware Shaders

Varyings

• These carry values from the Vertex Shader to the Pixel Shader

• Varyings are read OR write to the Vertex Shader• Varyings are read only to the Pixel Shader– If you read a Varying in the Vertex Shader you cannot write it!

• To use a varying you must declare it in both your Vertex and Pixel Shader code

• Varyings are Interpolated across the Primitive in a Perspective-Correct form (Linear on the Projection Plane)

Page 11: Hardware Shaders

Built In Data Attributes (Vertex Shader)

gl_Vertex // 4D vector of the vertex position gl_Normal // 3D vector of the vertex normal gl_Color // 4D vector of the vertex colour gl_MultiTexCoordX // 4D vector of the Texture

Coordinate of textureX

Page 12: Hardware Shaders

Built In Data Uniforms (Vertex / Pixel Shader)

gl_ModelViewMatrix 4x4 model-view matrix. gl_ModelViewProjectionMatrix 4x4 model-view-

projection matrix. gl_NormalMatrix 3x3 inverse transpose model-

view matrix. This matrix is used for normal transformation.

Page 13: Hardware Shaders

Built In Data Varyings (Vertex / Pixel Shader)

gl_FrontColor 4D vector of the primitives front colour

gl_BackColor 4D vector of the primitives back colour

gl_TexCoord[X] 4D vector of the Xth texture coordinate

Page 14: Hardware Shaders

Built In Data Outputs

Vertex Shader• l_Position 4D vector representing the final

processed vertex position.Pixel Shader• gl_FragColor 4D vector representing the final

color which is written in the frame buffer. (Pixel Shader)

• gl_FragDepth float representing the depth which is written in the depth buffer.

Page 15: Hardware Shaders

Declaring Data

uniform sampler2D awesomeTexture; varying vec3 momentumDirection; attribute vec3 previousNormal;Float thisIsAFloat = 2.0f;Cast using constructors!Int a = 2;Float b = float(a);

Page 16: Hardware Shaders

Filling Data

• Vectors and Matrices need to be filled on Construction

Good:Vec2 filled = vec2(1.0, 0.0);

Page 17: Hardware Shaders

dot // a simple dot product cross // a simple cross product texture2D // used for sampling a texturenormalize // normalize a vectorclamp //clamping a vector to a minimum and a

maximum

Page 18: Hardware Shaders

How the Vertex Shader normally works

Void main(){gl_Position = gl_ModelViewProjectionMatrix *

gl_Vertex;}

Page 19: Hardware Shaders

A Simple Pixel (Fragment) Shader

Void main() {

// Setting Each Pixel To Cyangl_FragColor = vec4(0.0, 1.0, 1.0, 1.0);

}

Page 20: Hardware Shaders

A simple Texture Shader

Vertex Shader:void main() { // Transforming The Vertex gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; // Passing The Texture Coordinate Of Texture Unit 0 To

The Fragment Shader texture_coordinate = vec2(gl_MultiTexCoord0); }

Page 21: Hardware Shaders

Pixel Shader

void main() { // Sampling The Texture And Passing It To The

Frame Buffer gl_FragColor = texture2D(my_color_texture,

texture_coordinate); }

Page 22: Hardware Shaders

Utilising Shaders in OpenGL

The 4 OpenGL Extensions:GL_ARB_shader_objectsGL_ARB_shading_language_100GL_ARB_vertex_shaderGL_ARB_fragment_shader

Page 23: Hardware Shaders

Next Week

• Loading Extensions in Windows C++• glext.h is your friend• Passing the shader source to a shader object• Compiling the shader source• Linking shaders to one program object