Miscellaneous
This topic contains miscellaneous OpenGL ES programming tips.
Avoid reading back the framebuffer contents (M1)
Reading back the framebuffer flushes the GL pipeline and limits the amount CPU/GPU
parallelism. Reading frequently or in the middle of a frame stalls the GPU and limits the
throughput with lower frame rate as a result. If the buffer contents must be read back
(perhaps for picking 3D objects in a complex scene), it should be done minimally and
scheduled at the beginning of the next frame. In the special case that the application is
reading back into a sub-rectangle of pixels in client memory, the
pack_subimage
extension (if supported) is very useful. Setting the
PACK_ROW_LENGTH
pixel store parameter will reduce the loop overhead that
will otherwise be necessary (T9).
Avoid clearing buffers needlessly (M2)
If the application always covers the entire color buffer for each frame, then bandwidth can
be saved by not clearing it. It is a common mistake to call
Clear(GL_COLOR_BUFFER_BIT)
when it is not necessary. If only part of the
color buffer is modified, then constrain pixel operations to that region by enabling scissor
testing and define a minimal scissor box for the region. The same applies to depth and
stencil buffers if full screen testing is not needed.
Disable blending when it is not needed (M3)
Most blending operations require a read and a write to the framebuffer.
Memory bandwidth is often doubled when rendering with blending is enabled. The number of blended fragments should be kept to a minimum—it can drastically speed up the GL application.
Minimize memory fragmentation (M4)
Buffer objects and glTexImage*
functions are effectively graphics memory
allocations. Reusing existing buffer objects and texture objects will reduce memory
fragmentation. If geometry or textures are generated dynamically, the application should
allocate a minimal pool of objects for this purpose during application initialization. It
may be that two buffers or textures used in a round-robin fashion are optimal for reducing
the risk that the GPU is waiting on the resource. Also, recall that sampling a texture that
is being rendered to, at the same time, is undefined. This can be another reason to
alternate between objects. For more information, see Memory Fragmentation in this appendix.