opengl buffer transfers patrick cozzi university of pennsylvania cis 565 - spring 2012
TRANSCRIPT
![Page 1: OpenGL Buffer Transfers Patrick Cozzi University of Pennsylvania CIS 565 - Spring 2012](https://reader035.vdocument.in/reader035/viewer/2022070400/56649f125503460f94c25c5f/html5/thumbnails/1.jpg)
OpenGL Buffer Transfers
Patrick CozziUniversity of PennsylvaniaCIS 565 - Spring 2012
![Page 2: OpenGL Buffer Transfers Patrick Cozzi University of Pennsylvania CIS 565 - Spring 2012](https://reader035.vdocument.in/reader035/viewer/2022070400/56649f125503460f94c25c5f/html5/thumbnails/2.jpg)
Drawing
It doesn’t matter if we’re using:
Efficiently transferring data between the CPU and GPU is critical for performance.
![Page 3: OpenGL Buffer Transfers Patrick Cozzi University of Pennsylvania CIS 565 - Spring 2012](https://reader035.vdocument.in/reader035/viewer/2022070400/56649f125503460f94c25c5f/html5/thumbnails/3.jpg)
How many vertices per second do we
need?
![Page 4: OpenGL Buffer Transfers Patrick Cozzi University of Pennsylvania CIS 565 - Spring 2012](https://reader035.vdocument.in/reader035/viewer/2022070400/56649f125503460f94c25c5f/html5/thumbnails/4.jpg)
Drawing
Image from http://graphics.cs.uni-sb.de/MassiveRT/boeing777.html
Boeing 777 model: ~350 million polygons
![Page 5: OpenGL Buffer Transfers Patrick Cozzi University of Pennsylvania CIS 565 - Spring 2012](https://reader035.vdocument.in/reader035/viewer/2022070400/56649f125503460f94c25c5f/html5/thumbnails/5.jpg)
Drawing
Image from http://www.vision.ee.ethz.ch/~pmueller/wiki/CityEngine/Documents
Procedurally generated model of Pompeii: ~1.4 billion polygons
![Page 6: OpenGL Buffer Transfers Patrick Cozzi University of Pennsylvania CIS 565 - Spring 2012](https://reader035.vdocument.in/reader035/viewer/2022070400/56649f125503460f94c25c5f/html5/thumbnails/6.jpg)
Buffer Objects
Array buffers – store vertex attributes Element buffers – store indices Stored in driver-controlled memory, not an
array in our application Provide hints to the driver about how we
will use the buffer
![Page 7: OpenGL Buffer Transfers Patrick Cozzi University of Pennsylvania CIS 565 - Spring 2012](https://reader035.vdocument.in/reader035/viewer/2022070400/56649f125503460f94c25c5f/html5/thumbnails/7.jpg)
Buffer Objects
GLuint vbo;GLfloat* vertices = new GLfloat[3 * numberOfVertices];
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER_ARB, vbo);
glBufferData(GL_ARRAY_BUFFER_ARB, numberOfBytes, vertices, GL_STATIC_DRAW_ARB);// Also check out glBufferSubData
delete [] vertices;
glDeleteBuffers(1, &vbo);
![Page 8: OpenGL Buffer Transfers Patrick Cozzi University of Pennsylvania CIS 565 - Spring 2012](https://reader035.vdocument.in/reader035/viewer/2022070400/56649f125503460f94c25c5f/html5/thumbnails/8.jpg)
Buffer Objects
GLuint vbo;GLfloat* vertices = new GLfloat[3 * numberOfVertices];
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER_ARB, vbo);
glBufferData(GL_ARRAY_BUFFER_ARB, numberOfBytes, vertices, GL_STATIC_DRAW_ARB);// Also check out glBufferSubData
delete [] vertices;
glDeleteBuffers(1, &vbo);
![Page 9: OpenGL Buffer Transfers Patrick Cozzi University of Pennsylvania CIS 565 - Spring 2012](https://reader035.vdocument.in/reader035/viewer/2022070400/56649f125503460f94c25c5f/html5/thumbnails/9.jpg)
Buffer Objects
GLuint vbo;GLfloat* vertices = new GLfloat[3 * numberOfVertices];
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER_ARB, vbo);
glBufferData(GL_ARRAY_BUFFER_ARB, numberOfBytes, vertices, GL_STATIC_DRAW_ARB);// Also check out glBufferSubData
delete [] vertices;
glDeleteBuffers(1, &vbo);
![Page 10: OpenGL Buffer Transfers Patrick Cozzi University of Pennsylvania CIS 565 - Spring 2012](https://reader035.vdocument.in/reader035/viewer/2022070400/56649f125503460f94c25c5f/html5/thumbnails/10.jpg)
Buffer Objects
GLuint vbo;GLfloat* vertices = new GLfloat[3 * numberOfVertices];
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER_ARB, vbo);
glBufferData(GL_ARRAY_BUFFER_ARB, numberOfBytes, vertices, GL_STATIC_DRAW_ARB);// Also check out glBufferSubData
delete [] vertices;
glDeleteBuffers(1, &vbo); Copy from application to driver-controlled memory. GL_STATIC_DRAW should imply video memory.
![Page 11: OpenGL Buffer Transfers Patrick Cozzi University of Pennsylvania CIS 565 - Spring 2012](https://reader035.vdocument.in/reader035/viewer/2022070400/56649f125503460f94c25c5f/html5/thumbnails/11.jpg)
Buffer Objects
GLuint vbo;GLfloat* vertices = new GLfloat[3 * numberOfVertices];
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER_ARB, vbo);
glBufferData(GL_ARRAY_BUFFER_ARB, numberOfBytes, vertices, GL_STATIC_DRAW_ARB);// Also check out glBufferSubData
delete [] vertices;
glDeleteBuffers(1, &vbo); Does glBufferData block? Does glBufferSubData block?
![Page 12: OpenGL Buffer Transfers Patrick Cozzi University of Pennsylvania CIS 565 - Spring 2012](https://reader035.vdocument.in/reader035/viewer/2022070400/56649f125503460f94c25c5f/html5/thumbnails/12.jpg)
Buffer Objects
Usage HintStatic: 1-to-n update-to-draw ratioDynamic: n-to-m update to draw (n < m)Stream: 1-to-1 update to draw
It’s a hint. Do drivers take it into consideration?GL_ARB_debug_output tells us where the
buffer is stored
![Page 13: OpenGL Buffer Transfers Patrick Cozzi University of Pennsylvania CIS 565 - Spring 2012](https://reader035.vdocument.in/reader035/viewer/2022070400/56649f125503460f94c25c5f/html5/thumbnails/13.jpg)
Layouts
Images from www.virtualglobebook.com
Separate Buffers
Non-interleaved Buffer
Interleaved Buffer
![Page 14: OpenGL Buffer Transfers Patrick Cozzi University of Pennsylvania CIS 565 - Spring 2012](https://reader035.vdocument.in/reader035/viewer/2022070400/56649f125503460f94c25c5f/html5/thumbnails/14.jpg)
Layout Tradeoffs
Separate Buffers Flexibility, e.g.:
Combination of static and dynamic buffers Multiple objects share the same buffer
Non-interleaved Buffer How is the memory coherence?
Interleaved Buffer Faster for static buffers
Proportional to the number of attributes
Hybrid?
![Page 15: OpenGL Buffer Transfers Patrick Cozzi University of Pennsylvania CIS 565 - Spring 2012](https://reader035.vdocument.in/reader035/viewer/2022070400/56649f125503460f94c25c5f/html5/thumbnails/15.jpg)
Vertex Throughput Tips
Optimize for the Vertex Cache Use smaller vertices
Use less precision, e.g., half instead of floatPack, then unpack in vertex shaderDerive attributes or components from other
attributesHow many components do you need to store a
normal?
![Page 16: OpenGL Buffer Transfers Patrick Cozzi University of Pennsylvania CIS 565 - Spring 2012](https://reader035.vdocument.in/reader035/viewer/2022070400/56649f125503460f94c25c5f/html5/thumbnails/16.jpg)
Buffer Objects
Image from http://developer.nvidia.com/object/using_VBOs.html
Map a pointer to driver-controlled memory• Also map just a subset of the buffer
![Page 17: OpenGL Buffer Transfers Patrick Cozzi University of Pennsylvania CIS 565 - Spring 2012](https://reader035.vdocument.in/reader035/viewer/2022070400/56649f125503460f94c25c5f/html5/thumbnails/17.jpg)
DMA
Image from http://www.openglinsights.com/
DMA – Direct Memory Access Asynchronously transfer buffer between CPU and
GPU Asynchronous with respect to the CPU, not always
the GPU How many copies are made?
![Page 18: OpenGL Buffer Transfers Patrick Cozzi University of Pennsylvania CIS 565 - Spring 2012](https://reader035.vdocument.in/reader035/viewer/2022070400/56649f125503460f94c25c5f/html5/thumbnails/18.jpg)
Buffer Mapping
Use glMapBuffer, glUnmapBuffer, and friends to save a copy
Pointer returned by glMapBuffer is valid until glUnmapBuffer is called.
Image from http://developer.nvidia.com/object/using_VBOs.html
![Page 19: OpenGL Buffer Transfers Patrick Cozzi University of Pennsylvania CIS 565 - Spring 2012](https://reader035.vdocument.in/reader035/viewer/2022070400/56649f125503460f94c25c5f/html5/thumbnails/19.jpg)
Buffer Mapping
Image from http://developer.nvidia.com/object/using_VBOs.html
![Page 20: OpenGL Buffer Transfers Patrick Cozzi University of Pennsylvania CIS 565 - Spring 2012](https://reader035.vdocument.in/reader035/viewer/2022070400/56649f125503460f94c25c5f/html5/thumbnails/20.jpg)
Buffer Mapping
Use glMapBufferRange to map a subset of a buffer. Why?
![Page 21: OpenGL Buffer Transfers Patrick Cozzi University of Pennsylvania CIS 565 - Spring 2012](https://reader035.vdocument.in/reader035/viewer/2022070400/56649f125503460f94c25c5f/html5/thumbnails/21.jpg)
Buffer Mapping
Use glMapBufferRange to map a subset of a buffer. Why?Only upload the portion of a buffer that changedManual double buffering – use one half for
updating and the other for rendering
![Page 22: OpenGL Buffer Transfers Patrick Cozzi University of Pennsylvania CIS 565 - Spring 2012](https://reader035.vdocument.in/reader035/viewer/2022070400/56649f125503460f94c25c5f/html5/thumbnails/22.jpg)
Implicit Synchronization
Command queue Rendering may occur a frame or two later Helps hide latency However implicit synchronization can occur:
Image from http://developer.nvidia.com/object/using_VBOs.html
![Page 23: OpenGL Buffer Transfers Patrick Cozzi University of Pennsylvania CIS 565 - Spring 2012](https://reader035.vdocument.in/reader035/viewer/2022070400/56649f125503460f94c25c5f/html5/thumbnails/23.jpg)
Implicit Synchronization
Avoiding implicit synchronizationRound-robinOrphanManual synchronization
![Page 24: OpenGL Buffer Transfers Patrick Cozzi University of Pennsylvania CIS 565 - Spring 2012](https://reader035.vdocument.in/reader035/viewer/2022070400/56649f125503460f94c25c5f/html5/thumbnails/24.jpg)
Implicit Synchronization
Round-robin
Image from http://developer.nvidia.com/object/using_VBOs.html
![Page 25: OpenGL Buffer Transfers Patrick Cozzi University of Pennsylvania CIS 565 - Spring 2012](https://reader035.vdocument.in/reader035/viewer/2022070400/56649f125503460f94c25c5f/html5/thumbnails/25.jpg)
Implicit Synchronization
Orphan – round robin inside the driver?
Image from http://developer.nvidia.com/object/using_VBOs.html
![Page 26: OpenGL Buffer Transfers Patrick Cozzi University of Pennsylvania CIS 565 - Spring 2012](https://reader035.vdocument.in/reader035/viewer/2022070400/56649f125503460f94c25c5f/html5/thumbnails/26.jpg)
Implicit Synchronization
Use glMapBufferRange with GL_MAP_UNSYNCHRONIZED_BITManually sync with glClientWaitSync
Image from http://developer.nvidia.com/object/using_VBOs.html
![Page 27: OpenGL Buffer Transfers Patrick Cozzi University of Pennsylvania CIS 565 - Spring 2012](https://reader035.vdocument.in/reader035/viewer/2022070400/56649f125503460f94c25c5f/html5/thumbnails/27.jpg)
Implicit Synchronization
Image from http://developer.nvidia.com/object/using_VBOs.html
![Page 28: OpenGL Buffer Transfers Patrick Cozzi University of Pennsylvania CIS 565 - Spring 2012](https://reader035.vdocument.in/reader035/viewer/2022070400/56649f125503460f94c25c5f/html5/thumbnails/28.jpg)
Other Buffer Objects
Pixel Buffers Texture Buffers Uniform Buffers
These are not in OpenGL ES 2.