Download presentation
Presentation is loading. Please wait.
Published byAldous McKenzie Modified over 8 years ago
1
COMP 175 | COMPUTER GRAPHICS Remco Chang1/XX13 – GLSL Lecture 13: OpenGL Shading Language (GLSL) COMP 175: Computer Graphics April 12, 2016
2
COMP 175 | COMPUTER GRAPHICS Remco Chang2/XX13 – GLSL Last week, we discussed the many of the new “tricks” in Graphics require low-level access to the Graphics pipeline For example, using textures, which is a pain in regular OpenGL. Motivation
3
COMP 175 | COMPUTER GRAPHICS Remco Chang3/XX13 – GLSL Graphics History Software Rendering Old-fashioned way (Use ray casting) Fixed-Function Immediate Mode (What we have used) Programmable Pipeline (~2001) GL Shading Language (What we will learn) CUDA/OpenCL (GPGPU) (~2008) General Purpose GPU Programming
4
COMP 175 | COMPUTER GRAPHICS Remco Chang4/XX13 – GLSL Ray Casting (Software Rendering) Wolfenstein Quake 1
5
COMP 175 | COMPUTER GRAPHICS Remco Chang5/XX13 – GLSL Fixed-Function Quake 3 Half-Life
6
COMP 175 | COMPUTER GRAPHICS Remco Chang6/XX13 – GLSL Programmable Pipeline (Shaders) Doom 3 Far Cry
7
COMP 175 | COMPUTER GRAPHICS Remco Chang7/XX13 – GLSL GPGPU General Purpose Graphics Processing Unit Utilize graphics card for computation Do work other than graphics
8
COMP 175 | COMPUTER GRAPHICS Remco Chang8/XX13 – GLSL Old Fixed-Function
9
COMP 175 | COMPUTER GRAPHICS Remco Chang9/XX13 – GLSL Programmable Pipeline
10
COMP 175 | COMPUTER GRAPHICS Remco Chang10/XX13 – GLSL Accessing Programmable Pipeline Introducing Shaders! 4/15/2014 10
11
COMP 175 | COMPUTER GRAPHICS Remco Chang11/XX13 – GLSL CPU: Typical C++ code GPU: OpenGL Shading Language (GLSL) CPU passes data to GPU Some processing happens on the CPU: Get mouse movement Load and parse data from disk Some processing happens on the GPU: Transform point from Object to World space Solve lighting equation Render CPU + GPU
12
COMP 175 | COMPUTER GRAPHICS Remco Chang12/XX13 – GLSL Most common: Vertex Fragment (Pixel) Newer: Geometry Tessellation Compute Types of Shaders
13
COMP 175 | COMPUTER GRAPHICS Remco Chang13/XX13 – GLSL OpenGL Shading Language (GLSL) Syntax Sequence and processes Vertex Shader Pixel Shader Communication between CPU and GPU What type of information can be passed How to pass information Note that you also need to know how to load a shader program, but that’s “magic incantation” that (mostly) stays the same every time. Two Things to Learn:
14
COMP 175 | COMPUTER GRAPHICS Remco Chang14/XX13 – GLSL
15
COMP 175 | COMPUTER GRAPHICS Remco Chang15/XX13 – GLSL Interfacing with the GPU Modern OpenGL Mobile/Web Programming Direct3D Compute Languages Stream, OpenCL, CUDA Future
16
COMP 175 | COMPUTER GRAPHICS Remco Chang16/XX13 – GLSL Client/Server Architecture Think of like a network Client = the CPU Server = the GPU The less they talk, the better Less waiting for memory to transfer Bandwidth is almost always the bottleneck Send data over once, and have it ready to go
17
COMP 175 | COMPUTER GRAPHICS Remco Chang17/XX13 – GLSL Optimizations Available Vertex Array Display Lists Vertex Buffer Objects Pixel Buffers Frame Buffer Objects Michael Shah
18
COMP 175 | COMPUTER GRAPHICS Remco Chang18/XX13 – GLSL Immediate Mode Every ‘gl’ command is being sent to the GPU every iteration. Very inefficient Think of it like having an interpreter versus a compiler for C++ Michael Shah
19
COMP 175 | COMPUTER GRAPHICS Remco Chang19/XX13 – GLSL Vertex Array No more glBegin/glEnd Store Vertices, indices, normals, colors, texture coordinates in an array Minimizes OpenGL calls on Client (CPU) Data still being passed from Client to Server every frame A little better because it’s all at once Stylistically, much cleaner code as well Michael Shah
20
COMP 175 | COMPUTER GRAPHICS Remco Chang20/XX13 – GLSL Compiled geometry Send it to the Server(GPU) once, and it is stored in memory on Server Geometry cannot be updated, without deleting display list. someGeometryDisplayList = glGenLists(1); glNewList(someGeometryDisplayList,GL_COMPILE); drawShape(); glEndList(); Display List 4/22/2014 Michael Shah 20
21
COMP 175 | COMPUTER GRAPHICS Remco Chang21/XX13 – GLSL Vertex Buffer Object (VBO) Improves upon Vertex Array We can store data on the Server (GPU) Just like display lists! We get a pointer into the Server (GPU) so that we can update it! As good as display lists! Because we have a pointer to the data, multiple clients can access data. Multiple instances of a program can access data 4/22/2014 Michael Shah 21
22
COMP 175 | COMPUTER GRAPHICS Remco Chang22/XX13 – GLSL Modifiers to Variables Uniform/Varying/Attribute Uniform – Read only data passed into vertex/fragment shaders Attributes – Input value which change every vertex. Read only – Used only in vertex shader Depending on GLSL version: GLSL <3.0: Varying – Pass data from vertex to fragment shader. GLSL >=3.0: in / out – in is the variable being passed in, out is the variable being passed out Read-only in fragment shader. Read/Write in Vertex Shader
23
COMP 175 | COMPUTER GRAPHICS Remco Chang23/XX13 – GLSL OpenGL Shading Language
24
COMP 175 | COMPUTER GRAPHICS Remco Chang24/XX13 – GLSL GLSL C-Like Language Made for GPU Architecture Because GPU’s are different than CPU’s Still Cross Platform Debugging – Still as difficult Domain Specific Language Compiled at runtime
25
COMP 175 | COMPUTER GRAPHICS Remco Chang25/XX13 – GLSL GLSL Primitives float, int, bool Example: float f = 10.0; or float f = float(10); (Casting not allowed, choose your type wisely!)
26
COMP 175 | COMPUTER GRAPHICS Remco Chang26/XX13 – GLSL GLSL Types Vector: vec2, vec3, vec4 Overloaded to take in each type Example: vec4 temp= vec4(1.0, 1.0, 1.0, 1.0) Matrix: mat2, mat3, mat4 Example: glModelViewMatrix is a mat4 type Access first column with: mat[0] Access second column with: mat[1] Example: mat[1] = vec3(1.0, 1.0, 1.0); Access individual element: mat[2][1];
27
COMP 175 | COMPUTER GRAPHICS Remco Chang27/XX13 – GLSL Some more examples Pass in vec3 into a vec4 vec3 color = vec3(1.0,0.5,0.25) vec4 temp = vec4(color, 1.0) Get components of vector color.x = 1.0; or color[0] = 1.0 color.y = 1.0; or color[1] = 1.0;
28
COMP 175 | COMPUTER GRAPHICS Remco Chang28/XX13 – GLSL Swizzling [1][1] Swizzling is the ability to rearrange a vectors components vec2 pos = temp.xy; // retrieve x and y component, or any of xyzw vec2 pos = temp.yz; vec4 = color.agbr; // opposite of rgba
29
COMP 175 | COMPUTER GRAPHICS Remco Chang29/XX13 – GLSL Note that some of these default keywords might not be there depending on GLSL version number (for example: gl_ModelViewMatrix isn’t there any more) More GLSL Keywords [1][2]12
30
COMP 175 | COMPUTER GRAPHICS Remco Chang30/XX13 – GLSL Write a Simple Shader Basic Vertex and Fragment Shader
31
COMP 175 | COMPUTER GRAPHICS Remco Chang31/XX13 – GLSL Writing a Shader Writing a shader, compiling a shader, and loading a shader Compile at runtime So we do not need to recompile entire source, if we only modify shader.
32
COMP 175 | COMPUTER GRAPHICS Remco Chang32/XX13 – GLSL
33
COMP 175 | COMPUTER GRAPHICS Remco Chang33/XX13 – GLSL Simple Vertex Shader
34
COMP 175 | COMPUTER GRAPHICS Remco Chang34/XX13 – GLSL Simple Fragment Shader
35
COMP 175 | COMPUTER GRAPHICS Remco Chang35/XX13 – GLSL The cpp file
36
COMP 175 | COMPUTER GRAPHICS Remco Chang36/XX13 – GLSL Drawing with immediate mode for now
37
COMP 175 | COMPUTER GRAPHICS Remco Chang37/XX13 – GLSL Final Image
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.