1 / 23

The OpenGL API

The OpenGL API. Patrick Cozzi University of Pennsylvania CIS 565 - Fall 2012. Agenda. Today: OpenGL shaders and uniforms Later: efficient buffer usage. OpenGL. Is a C-based API Is cross platform Is run by the ARB : Architecture Review Board Hides the device driver details

oakes
Download Presentation

The OpenGL API

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. The OpenGL API Patrick Cozzi University of Pennsylvania CIS 565 - Fall 2012

  2. Agenda • Today: OpenGL shaders and uniforms • Later: efficient buffer usage

  3. OpenGL • Is a C-based API • Is cross platform • Is run by the ARB: Architecture Review Board • Hides the device driver details • OpenGL vs. Direct3D

  4. Recall the fixed function light map Why was the alpha test remove? OpenGL • We are using GL 2 • No fixed function vertex and fragment shading • No legacy API calls: • glBegin() • glRotatef() • glTexEnvf() • AlphaFunc() • …

  5. Application OpenGL API Device Driver GPU OpenGL • Software stack:

  6. OpenGL • Major objects: Shader Programs Shader Objects Array Buffers Element Buffers Framebuffers Pixel Buffers Textures Fixed Function State Renderbuffers • We are not covering everything. Just surveying the most relevant parts for writing GLSL shaders

  7. Shaders • Shader object: an individual vertex, fragment, etc. shader • Are provided shader source code as a string • Are compiled • Shader program: Multiple shader objects linked together

  8. Shader Objects • Compile a shader object: const char *source = // ... GLint sourceLength = // ... GLuint v = glCreateShader(GL_VERTEX_SHADER); glShaderSource(v, 1, &source, &sourceLength); glCompileShader(v); GLint compiled; glGetShaderiv(v, GL_COMPILE_STATUS, &compiled); // success: compiled == GL_TRUE // ... glDeleteShader(v);

  9. OpenGL functions start with gl. Why? How would you design this in C++? • v is an opaque object • What is it under the hood? • How would you design this in C++? Shader Objects • Compile a shader object: const char *source = // ... GLint sourceLength = // ... GLuint v = glCreateShader(GL_VERTEX_SHADER); glShaderSource(v, 1, &source, &sourceLength); glCompileShader(v); GLint compiled; glGetShaderiv(v, GL_COMPILE_STATUS, &compiled); // success: compiled == GL_TRUE // ... glDeleteShader(v);

  10. Provide the shader’s source code Why can we pass more than one string? Shader Objects • Compile a shader object: const char *source = // ... GLint sourceLength = // ... GLuint v = glCreateShader(GL_VERTEX_SHADER); glShaderSource(v, 1, &source, &sourceLength); glCompileShader(v); GLint compiled; glGetShaderiv(v, GL_COMPILE_STATUS, &compiled); // success: compiled == GL_TRUE // ... glDeleteShader(v); Where should the source come from?

  11. Shader Objects • Compile a shader object: const char *source = // ... GLint sourceLength = // ... GLuint v = glCreateShader(GL_VERTEX_SHADER); glShaderSource(v, 1, &source, &sourceLength); glCompileShader(v); GLint compiled; glGetShaderiv(v, GL_COMPILE_STATUS, &compiled); // success: compiled == GL_TRUE // ... glDeleteShader(v); Compile, but what does the driver really do?

  12. Calling glGet* has performance implications. Why? Shader Objects • Compile a shader object: const char *source = // ... GLint sourceLength = // ... GLuint v = glCreateShader(GL_VERTEX_SHADER); glShaderSource(v, 1, &source, &sourceLength); glCompileShader(v); GLint compiled; glGetShaderiv(v, GL_COMPILE_STATUS, &compiled); // success: compiled == GL_TRUE // ... glDeleteShader(v); Good developers check for error. Again, how would you design this in C++?

  13. Shader Objects • Compile a shader object: const char *source = // ... GLint sourceLength = // ... GLuint v = glCreateShader(GL_VERTEX_SHADER); glShaderSource(v, 1, &source, &sourceLength); glCompileShader(v); GLint compiled; glGetShaderiv(v, GL_COMPILE_STATUS, &compiled); // success: compiled == GL_TRUE // ... glDeleteShader(v); Good developers also cleanup resources

  14. Shader Programs • Link a shader program: GLuint v = glCreateShader(GL_VERTEX_SHADER); GLuint f = glCreateShader(GL_FRAGMENT_SHADER); // ... GLuint p = glCreateProgram(); glAttachShader(p, v); glAttachShader(p, f); glLinkProgram(p); GLint linked; glGetShaderiv(p, GL_LINK_STATUS, &linked); // success: linked == GL_TRUE // ... glDeleteProgram(v);

  15. Shader Programs • Link a shader program: A program needs a vertex and fragment shader GLuint v = glCreateShader(GL_VERTEX_SHADER); GLuint f = glCreateShader(GL_FRAGMENT_SHADER); // ... GLuint p = glCreateProgram(); glAttachShader(p, v); glAttachShader(p, f); glLinkProgram(p); GLint linked; glGetShaderiv(p, GL_LINK_STATUS, &linked); // success: linked == GL_TRUE // ... glDeleteProgram(v);

  16. Shader Programs • Link a shader program: GLuint v = glCreateShader(GL_VERTEX_SHADER); GLuint f = glCreateShader(GL_FRAGMENT_SHADER); // ... GLuint p = glCreateProgram(); glAttachShader(p, v); glAttachShader(p, f); glLinkProgram(p); GLint linked; glGetShaderiv(p, GL_LINK_STATUS, &linked); // success: linked == GL_TRUE // ... glDeleteProgram(v);

  17. Shader Programs • Link a shader program: GLuint v = glCreateShader(GL_VERTEX_SHADER); GLuint f = glCreateShader(GL_FRAGMENT_SHADER); // ... GLuint p = glCreateProgram(); glAttachShader(p, v); glAttachShader(p, f); glLinkProgram(p); GLint linked; glGetShaderiv(p, GL_LINK_STATUS, &linked); // success: linked == GL_TRUE // ... glDeleteProgram(v); Be a good developer again

  18. Using Shader Programs GLuint p = glCreateProgram(); // ... glUseProgram(p); glDraw*(); // * because there are lots of draw functions • Part of the current state • How do you draw different objects with different shaders? • What is the cost of using multiple shaders? • How do we reduce the cost? • Hint: write more CPU code – really.

  19. Uniforms GLuint p = glCreateProgram(); // ... glLinkProgram(p); GLuintm = glGetUniformLocation(p, “u_modelViewMatrix”); GLuintl = glGetUniformLocation(p, “u_lightMap”); glUseProgram(p); mat4 matrix = // ... glUniformMatrix4fv(m, 1, GL_FALSE, &matrix[0][0]); glUniform1i(l, 0);

  20. Uniforms Each active uniform has an integer index location. GLuint p = glCreateProgram(); // ... glLinkProgram(p); GLuintm = glGetUniformLocation(p, “u_modelViewMatrix”); GLuintl = glGetUniformLocation(p, “u_lightMap”); glUseProgram(p); mat4 matrix = // ... glUniformMatrix4fv(m, 1, GL_FALSE, &matrix[0][0]); glUniform1i(l, 0);

  21. Uniforms GLuint p = glCreateProgram(); // ... glLinkProgram(p); GLuintm = glGetUniformLocation(p, “u_modelViewMatrix”); GLuintl = glGetUniformLocation(p, “u_lightMap”); glUseProgram(p); mat4 matrix = // ... glUniformMatrix4fv(m, 1, GL_FALSE, &matrix[0][0]); glUniform1i(l, 0); mat4 is part of the C++ GLM library GLM: http://www.g-truc.net/project-0016.html#menu

  22. Not transposing the matrix Uniforms GLuint p = glCreateProgram(); // ... glLinkProgram(p); GLuintm = glGetUniformLocation(p, “u_modelViewMatrix”); GLuintl = glGetUniformLocation(p, “u_lightMap”); glUseProgram(p); mat4 matrix = // ... glUniformMatrix4fv(m, 1, GL_FALSE, &matrix[0][0]); glUniform1i(l, 0); glUniform* for all sorts of datatypes Uniforms can be changed as often as needed, but are constant during a draw call

  23. Uniforms GLuint p = glCreateProgram(); // ... glLinkProgram(p); GLuintm = glGetUniformLocation(p, “u_modelViewMatrix”); GLuintl = glGetUniformLocation(p, “u_lightMap”); glUseProgram(p); mat4 matrix = // ... glUniformMatrix4fv(m, 1, GL_FALSE, &matrix[0][0]); glUniform1i(l, 0); Why not glUniform*(p, …)?

More Related