Performant, scalar processing using C-Style dialect called GLSL OpenGL Shader Language Advanced blending, rasterizing and visibility testing for “free” OpenGL ES 2.0 “like” API to increase portability Thursday, January 24, 13
Geometry Shaders Vertex Shaders Fragment Shaders Vertex Buffers Yes No Yes No * Yes No * ARB No ARB Yes ARB Yes ARB Yes * Can be emulated using shaders Thursday, January 24, 13
Example: glPushStack(); glPopStack(); No native transformation / projection User managed GPU memory Factory calls GPU elements are references, not objects Thursday, January 24, 13
Uniforms: Run-time constants (includes samplers) Attributes: Per-vertex data Varyings: Interpolated data generated by the rasterizer Thursday, January 24, 13
Varyings and gl_Position Fragment: Transform Varyings into gl_FragColor Fragments may also be “discard”ed, preventing merge stage Thursday, January 24, 13
uniform mat4 mvProj; attribute vec3 aPosition; // Vertex position attribute vec3 aColor; // Vertex color attribute vec2 aTexture; // Vertex UV Coordinates varying vec3 color; // Fragment color varying vec2 texture; // Fragment UV coords void main() { // Simply forward colors and textures // NOTE: This is where you would do lighting color = aColor; texture = aTexture; // Position our vertex in screen space gl_Position = vec4(aPosition, 1.0) * mvProj; } FRAGMENT SHADER // the 2D texture used as a texture map uniform sampler2D mytexture; // The interpolated values for our fragment (pixel) varying vec3 color; varying vec2 texture; void main() { // Multiply (ref: photoshop effect) two colors gl_Color = vec3(color, 1.0) * texture2D(mytexture, texture); } Thursday, January 24, 13
Numeric: float, int, bool Vector Types: vec[2-4], int[2-4], bool[2-4] Matrix Types: mat[2-4] Samplers provide linkage for both 2D and 3D (cube mapping) textures Shaders see textures all as the same size with unit colors Vector math is always performed “component wise” Exceptions: dot, length, distance, cross Thursday, January 24, 13
these are just an array of floating point vectors (mostly) mat2 a = vec2 a[2]; Access entire column: a[x] = vec4(1.0); Access individual element: a[x][y] = 1.0; Thursday, January 24, 13
and fragment shaders Loops and Conditionals are Death to performance “Shared Program Cursor / Program Memory” Don’t calculate constants in shaders ModelView * Projection = No no A stalled GPU is a stalled CPU Matrices and Arrays are flattened when uploading to the GPU Thursday, January 24, 13
Upload textures (Textures are loaded into “texture units”) Create program Assign uniforms Bind buffers to attributes Call drawElements / drawArrays Thursday, January 24, 13
WebGL middleware https://github.com/evanw/lightgl.js/ glMatrix: High performance matrix math for javascript http://glmatrix.net/ Learning WebGL http://learningwebgl.com/blog/?page_id=1217 Thursday, January 24, 13