Professional Documents
Culture Documents
L06-PERFORMANCE
Mohammad Shaker
mohammadshaker.com
@ZGTRShaker
2015
Performance
How can you up your OpenGL app performance?
• Different Techniques:
– Vertex buffer and vertex arrays
– Display Lists
– Frustum culling
– Other methods
Vertex Buffer Object (VBO)
Vertex Array Object (VAO)
Vertex and Array Buffers
• Look at this,
Vertex and Array Buffers
// RGBA colors
color4 vertex_colors[8] = {
color4(0.0, 0.0, 0.0, 1.0 ), // black
color4(1.0, 0.0, 0.0, 1.0 ), // red
color4(1.0, 1.0, 0.0, 1.0 ), // yellow
color4(0.0, 1.0, 0.0, 1.0 ), // green
color4(0.0, 0.0, 1.0, 1.0 ), // blue
color4(1.0, 0.0, 1.0, 1.0 ), // magenta
color4(1.0, 1.0, 1.0, 1.0 ), // white
color4(0.0, 1.0, 1.0, 1.0 ) // cyan
};
Initializing the Cube’s Data
// quad() generates two triangles for each face and assigns colors to the vertices
int Index = 0; // global variable indexing into VBO arrays
Per Vertex
Polynomial Operations &
Evaluator Primitive
Assembly
Texture
Memory
Pixel
Operations
OpenGL Architecture
Per Vertex
Polynomial Operations &
Evaluator Primitive
Assembly
Texture
Memory
Pixel
Operations
OpenGL Architecture
Per Vertex
Polynomial Operations &
Evaluator Primitive
Assembly
Texture
Memory
Pixel
Operations
Display Lists
• Not all OpenGL routines can be stored in display lists
• State changes persist, even after a display list is finished
• Display lists can call other display lists
• Display lists are not editable, but you can fake it
– make a list (A) which calls other lists (B, C, and D)
– delete and replace B, C, and D, as needed
Display Lists and Hierarchy
• Consider model of a car
– Create display list for chassis
– Create display list for wheel
glNewList(CAR, GL_COMPILE);
glCallList(CHASSIS);
glTranslatef(…);
glCallList(WHEEL);
glTranslatef(…);
glCallList(WHEEL);
…
glEndList();
Display List Example
for Terrain Drawing
DisplayList Code
Global scope:
GLvoid init()
//Set camera and projection
GLvoid DrawGLScene()
DisplayList Code
Global scope:
GLuint terrainListId;
GLvoid init()
//Set camera and projection
terrainListId = glGenLists(1);
GLvoid DrawGLScene()
DisplayList Code
Global scope:
GLuint terrainListId;
GLvoid init()
//Set camera and projection
terrainListId = glGenLists(1);
DrawTerrain();
GLvoid DrawGLScene()
glCallList(terrainListId);
DisplayList Code
Global scope:
GLuint terrainListId;
GLvoid init()
//Set camera and projection
terrainListId = glGenLists(1);
DrawTerrain();
GLvoid DrawGLScene()
glCallList(terrainListId);
DisplayList Code
Global scope:
GLuint terrainListId;
GLvoid init()
//Set camera and projection
terrainListId = glGenLists(1);
DrawTerrain();
GLvoid DrawGLScene()
glCallList(terrainListId);
DrawTerrain()
glNewList(terrainListId, GL_COMPILE);
//Terrain Code
glEndList();
Culling and Clipping
Frustum Culling
• Create a clipping plane
– float plane [] = {A, b, C, D};
• Create a clipping plane with it
– glClipPlane(GL_CLIP_PLANE0, plane);
• Enable the clipping plane
– glEnable(GL_CLIP_PLANE0);
Scissor Box
• Additional Clipping Test
glScissor(x, y, w, h )
• any fragments outside of box are clipped
• useful for updating a small section of a viewport
– affects glClear() operations
General Memory Management
http://docs.unity3d.com/Manual/UnderstandingAutomaticMemoryManagement.html
General Memory Management
• Avoid memory allocation in a loops
function ConcatExample(intArray: int[]) {
var line = intArray[0].ToString();
return line;
}
General Memory Management
• Avoid memory allocation in a loops
var scoreBoard: GUIText;
var score: int;
function Update() {
var scoreText: String = "Score: " + score.ToString();
scoreBoard.text = scoreText;
}
function Update() {
if (score != oldScore) {
scoreText = "Score: " + score.ToString();
scoreBoard.text = scoreText;
oldScore = score;
}
}
General Memory Management
• Avoid memory allocation in a loops
– Assign random variables to an array.
return result;
}