1 / 24

Hardware Shaders

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

Download Presentation

Hardware Shaders

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Hardware Shaders Paul Taylor 2009

  2. 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.

  3. 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

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

  5. 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

  6. 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

  7. Inputs and Outputs Uniforms Attributes Varyings

  8. 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

  9. 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

  10. 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)

  11. 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

  12. 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.

  13. 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

  14. 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.

  15. 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);

  16. Filling Data • Vectors and Matrices need to be filled on Construction Good: Vec2 filled = vec2(1.0, 0.0);

  17. 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

  18. How the Vertex Shader normally works Void main() { gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; }

  19. A Simple Pixel (Fragment) Shader Void main() { // Setting Each Pixel To Cyan gl_FragColor = vec4(0.0, 1.0, 1.0, 1.0); }

  20. 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); }

  21. Pixel Shader void main() { // Sampling The Texture And Passing It To The Frame Buffer gl_FragColor = texture2D(my_color_texture, texture_coordinate); }

  22. 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

  23. 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

  24. References http://nehe.gamedev.net/data/articles/article.asp?article=21 http://msdn.microsoft.com/en-us/library/bb509656(VS.85).aspx

More Related