240 likes | 412 Views
Hardware Shaders. Paul Taylor 2009. What is a Hardware Shader. We send Verticies to the Video card and Textures / Colours
E N D
Hardware Shaders Paul Taylor 2009
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.
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
http://nehe.gamedev.net/data/articles/article.asp?article=21
GLSL 101 Data Types Vec2, Vec3, Vec4 // Floating Point Vectors Ivec2, Ivec3, Ivec4 // Integer Vectors Bvec2, Bvec3, Bvec4 // Boolean Vectors Mat2, Mat3, Mat4 // Floating Point Matrices
sampler1D, sampler2D, sampler3D // 1D, 2D and 3D texture samplerCube // Cube Map texture sampler1Dshadow, sampler2Dshadow // 1D and 2D depth-component texture // Aka Bump maps and Normal Maps
Inputs and Outputs Uniforms Attributes Varyings
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
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
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)
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
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.
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
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.
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);
Filling Data • Vectors and Matrices need to be filled on Construction Good: Vec2 filled = vec2(1.0, 0.0);
dot // a simple dot product cross // a simple cross product texture2D // used for sampling a texture normalize // normalize a vector clamp //clamping a vector to a minimum and a maximum
How the Vertex Shader normally works Void main() { gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; }
A Simple Pixel (Fragment) Shader Void main() { // Setting Each Pixel To Cyan gl_FragColor = vec4(0.0, 1.0, 1.0, 1.0); }
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); }
Pixel Shader void main() { // Sampling The Texture And Passing It To The Frame Buffer gl_FragColor = texture2D(my_color_texture, texture_coordinate); }
Utilising Shaders in OpenGL The 4 OpenGL Extensions: GL_ARB_shader_objects GL_ARB_shading_language_100 GL_ARB_vertex_shader GL_ARB_fragment_shader
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
References http://nehe.gamedev.net/data/articles/article.asp?article=21 http://msdn.microsoft.com/en-us/library/bb509656(VS.85).aspx