introduction to computer graphicscs5600/media/lecture 21 - hardware acceleration... · parallelism...

40
Introduction to Computer Graphics Hardware Acceleration Review

Upload: others

Post on 20-Oct-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

  • Introduction to Computer GraphicsHardware Acceleration Review

  • OpenGL Project Setup

    Create a command-line style project in Xcode 4

    Select the project file and click Build Phases tab

    Add OpenGL.framework and GLUT.framework to Link list

  • OpenGL Project Setup

    Create a command-line style project in target IDE (or not...)

    Install libglut3-dev and libglew-dev using a package manager

    When compiling reference OpenGL, GLUT, GLEW librarieseg. gcc main.c -o a.out -I/usr/include -L/usr/lib -lglut -lGLEW -lGL

  • OpenGL Project SetupCreate a command-line style project in Visual Studio 2010

    Download and install GLUT - http://user.xmission.com/~nate/glut.html

    glut.h: C:\Program Files\Microsoft Visual Studio 10.0\VC\include\GL\

    glut32.lib: C:\Program Files\Microsoft Visual Studio 10.0\VC\lib\

    glut32.dll: C:\Windows\System32\ (or C:\Windows\SysWOW64\ for 64-bit)

    Download and install GLEW - http://glew.sourceforge.net/

    glew.h, glxew.h, wglew.h: C:\Program Files\Microsoft Visual Studio 10.0\VC\include\GL\

    glew32.lib: C:\Program Files\Microsoft Visual Studio 10.0\VC\lib\

    glew32.dll, glew32s.dll: C:\Windows\System32\ (or C:\Windows\SysWOW64\)

    Add OpenGL, GLUT, and GLEW library files to project link stage (opengl32.lib, glut32.lib, glew32.lib)

    Also see http://openglbook.com/setting-up-opengl-glew-and-freeglut-in-visual-c/

  • Hardware Acceleration

  • Hardware Acceleration

  • Hardware Acceleration

    2 Processors

  • Hardware Acceleration

  • Hardware Acceleration192

    Processors

  • Data read from Scene and OBJ files Rasterizer

    Model View Matrix

    Projection Matrix

    Viewport Transform

    Homogeneous Divide

    Depth Test

    Back-face Culling

    Texturing Lighting Blending

    Fragments resulting from rasterization Raster

  • Data read from Scene and OBJ files Rasterizer

    Model View Matrix

    Projection Matrix

    Viewport Transform

    Homogeneous Divide

    Depth Test

    Back-face Culling

    Texturing Lighting Blending

    Fragments resulting from rasterization Raster

  • Data read from Scene and OBJ files

    OpenGL ES Primitive

    ProcessingVertex Shader

    OpenGL ES Rasterizer

    Fragment Shader

    OpenGL ES Fragment

    Processing

    Fragments resulting from rasterization Frame Buffer

  • OpenGL Shading LanguageDefines a C-like language that can be compiled into GPU instructions

    Floating-point, Integer, and Boolean data types

    Vectors of these types in 2, 3, 4 sizes

    Matrices of floats in 2x2, 3x3, 4x4

    1D-Arrays and Structures

    Special Texture types

    Operators on all these types

  • Variable storage qualifiers - attribute, uniform, varying

    Variable precision qualifiers - lowp, mediump, highp

    Control Statements - if, else

    Loops - for, while, do-while

    Jumps - break, continue, return

    Function Definition

    Pre-processor Directives

    OpenGL Shading Language

  • Built-in functions for basic mathematics, trigonometry, geometry, and texturing (eg. exp, tan, cross, texture2D)

    Built-in variables to represent inputs and outputs

    Vertex Shader

    gl_Position (vec4, out)

    Fragment Shader

    gl_FragCoord (vec2, in)

    gl_FragColor (vec4, out)

    OpenGL Shading Language

  • Host-GPU Data TransferGlobal values are sent to the graphics processing hardware by changing the values of uniform variables defined in the shaders using glGetUnifromLocation and glUniform* calls

    Geometry data is sent to the graphics processing hardware by using glVertexAttribPointer

    Textures are sent to the graphics processing hardware by calling glBindTexture and glTexImage2D

    Drawing is initiated by calling glDrawArrays

  • Parallelism Notes

    Be careful with the use of control structures, especially branches and loops, to preserve efficiency

    Code is executing in parallel, so processor groups must all terminate before starting a new batch of data

    Remember that this is a Single Instruction Multiple Data system (SIMD) with scattering & gathering restrictions

    Possible to do general computation using GLSL, but it is best to use parallel computation specific APIs like OpenCL or CUDA

  • Data read from Scene and OBJ files

    OpenGL ES Primitive

    ProcessingVertex Shader

    OpenGL ES Rasterizer

    Fragment Shader

    OpenGL ES Fragment

    Processing

    Fragments resulting from rasterization Frame Buffer

  • Vertex Shader

    Receives a vertex from OpenGL after minimal processing

    Modifies incoming vertex in some way

    Outputs the vertex

    May also output additional data for the fragment shader to use

  • attribute vec4 position;

    void main() { gl_Position = position; }

    Vertex Shader

  • attribute vec4 position;

    uniform mat4 modelView;

    void main() { gl_Position = modelView * position; }

    Vertex Shader

  • attribute vec4 position;

    uniform mat4 modelView; uniform mat4 projection;

    void main() { gl_Position = projection * modelView * position; }

    Vertex Shader

  • attribute vec4 position; attribute vec2 textureCoordinate;

    uniform mat4 modelView; uniform mat4 projection;

    varying lowp vec2 textureCoordinateVarying;

    void main() { gl_Position = projection * modelView * position; textureCoordinateVarying = textureCoordinate; }

    Vertex Shader

  • attribute vec4 position; attribute vec3 normal; attribute vec2 textureCoordinate;

    uniform mat4 modelView; uniform mat4 projection;

    varying lowp vec3 normalVarying; varying lowp vec2 textureCoordinateVarying;

    void main() { gl_Position = projection * modelView * position; normalVarying = vec3(normalize(modelView * vec4(normal, 0.0))); textureCoordinateVarying = textureCoordinate; }

    Vertex Shader

  • attribute vec4 position; attribute vec3 normal; attribute vec2 textureCoordinate;

    uniform mat4 modelView; uniform mat4 projection;

    uniform vec4 lightPosition; uniform vec4 lightAmbient; uniform vec4 lightDiffuse;

    varying lowp vec3 normalVarying; varying lowp vec2 textureCoordinateVarying; varying lowp vec4 diffuseVarying;

    void main() { gl_Position = projection * modelView * position; normalVarying = vec3(normalize(modelView * vec4(normal, 0.0))); textureCoordinateVarying = textureCoordinate; vec4 lightVector = normalize(lightPosition - gl_Position); float lightIncidence = dot(lightVector, vec4(normalVarying, 1.0)); diffuseVarying = lightDiffuse * vec4(max(lightIncidence, 0.0)); }

    Vertex Shader

  • Data read from Scene and OBJ files

    OpenGL ES Primitive

    ProcessingVertex Shader

    OpenGL ES Rasterizer

    Fragment Shader

    OpenGL ES Fragment

    Processing

    Fragments resulting from rasterization Frame Buffer

  • Fragment Shader

    Receives a fragment from OpenGL resulting from rasterizing a primitive

    Chooses a color for the fragment based on data given by vertex shader

    Outputs the fragment color

  • void main() { gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0); }

    Fragment Shader

  • uniform vec4 color; void main() { gl_FragColor = color; }

    Fragment Shader

  • varying vec4 color; void main() { gl_FragColor = color; }

    Fragment Shader

  • uniform sampler2D textureUnit; varying vec2 textureCoordinate; void main() { gl_FragColor = texture2D(textureUnit, textureCoordinate); }

    Fragment Shader

  • uniform vec4 color; uniform vec4 lightPositionEyeCoords;

    varying vec4 positionEyeCoords; varying vec3 normalEyeCoords; void main() { vec3 incident = vec3(normalize(lightPositionEyeCoords - positionEyeCoords)); vec3 normal = normalEyeCoords; float incidence = max(0.0, dot(incident, normal)); gl_FragColor = color * incidence; }

    Fragment Shader

  • Data read from Scene and OBJ files

    OpenGL ES Primitive

    ProcessingVertex Shader

    OpenGL ES Rasterizer

    Fragment Shader

    OpenGL ES Fragment

    Processing

    Fragments resulting from rasterization Frame Buffer

  • Short Example Program#include // Not needed on Mac

    #include // on Mac

    void draw();

    int main(int argc, char** argv) {! glutInit(&argc, argv);! glutInitWindowSize(800, 600);! glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);! glutCreateWindow("OpenGL");

    ! GLenum err = glewInit(); // Not needed on Mac! if (err != GLEW_OK)! ! ; // Output error message

    glUseProgram(0);! glutDisplayFunc(draw);! glutMainLoop();

    ! return 0; }

    void draw() {! glClearColor(0.8f, 0.0f, 0.8f, 1.0f);! glClear(GL_COLOR_BUFFER_BIT);! glutSwapBuffers();}

  • NoiseProcedural geometry needs “smooth” randomness

    Randomness should be repeatable based on inputs

    1D, 2D, 3D, etc. versions of this are often called “noise”

    Ken Perlin created a good basis for noise functionshttp://mrl.nyu.edu/~perlin/doc/oscar.html

    http://mrl.nyu.edu/~perlin/noise/

    http://www.dreamincode.net/forums/topic/66480-perlin-noise/

    http://devmag.org.za/2009/04/25/perlin-noise/

  • Noise

  • Noise

  • OpenGL ES Reference Card

    http://www.khronos.org/opengles/2_X/