O'Reilly logo

OpenGL Insights by Christophe Riccio, Patrick Cozzi

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Tips
OpenGL glCreateShaderProgram may provide faster build per-
formance than a sequence of glCompilerShader and
glLinkProgram. However, it only creates a single shader stage
program.
OpenGL
WebGL
OpenGL ES
Not all shader objects need a main() function. Multiple shader
objects can be linked together in the same program to allow shar-
ing the same code between different programs.
OpenGL
WebGL
OpenGL ES
Build all GLSL shaders and programs first, and then query the
results to hide build and query latency.
OpenGL
WebGL
OpenGL ES
Call glDeleteShader after attaching a shader to a program to
simplify cleanup later.
OpenGL Five OpenGL 4.2 functions generate info logs:
glCompileShader
glCreateShaderProgram
glLinkProgram
glValidateProgram
glValidateProgramPipeline
OpenGL
OpenGL ES
Functions like glGenTextures do not create an object, they re-
turn a name for use with a new object. Objects are typically cre-
ated with glBind* unless they are b ased on direct state access, in
which case any other function may actually create the object.
xxvii
xxviii Tips
OpenGL
WebGL
OpenGL ES
glGenerateMipmap may execute on the CPU, and therefore
may be especially slow. Generate mipmaps offline or profile this
function.
OpenGL
WebGL
OpenGL ES
When using the default texture scanline alignment,
GL
PACK ALIGNMENT,offourbytes, withglTexImage2D
or glTexSubImage2D, the end of each row of pixel data may
need to be padded to the next multiple of the alignment.
OpenGL Texture rectangle, texture multisample, and buffer textures cant
have mipmaps.
OpenGL Integer textures, GL EXT texture integer, do not support fil-
tering.
OpenGL A buffer texture is a 1D texture with a buffer object as storage
which can only be fetched, not sampled.
OpenGL
WebGL
OpenGL ES
Unmap buffers as soon as possible to allow the driver to start the
transfer or to schedule the transfer.
OpenGL
WebGL
OpenGL ES
Use buffer usage flags appropriately: COPY,GLtoGL;DRAW,APP
to GL; READ, GL to APP; STREAM, update always, DYNAMIC,up-
date often, STATIC, update rarely.
OpenGL
WebGL
OpenGL ES
Set a GLSL sampler uniform to the texture unit number, not the
OpenGL texture ID.
OpenGL
WebGL
OpenGL ES
glGetUniformLocation returns 1 but doesnt generate an er-
ror if the uniform name does not correspond to an active uniform.
All declared uniforms are not active; uniforms that do not con-
tribute to the shader’s output can be optimized out by the com-
piler.
OpenGL An OpenGL context must always be current for the duration of
OpenGL/compute interoperability.
OpenGL An OpenGL object should not be accessed by OpenGL while it is
mapped for usage within the compute portion.
OpenGL
WebGL
OpenGL ES
Avoid extraneous glBindFramebuffer calls. Use multiple at-
tachments to a FBO rather than managing multiple FBOs.
Tips xxix
OpenGL
WebGL
OpenGL ES
FBOs must always be validated before use to ensure that the se-
lected format is renderable.
OpenGL Only one OpenGL query per quer y type, e.g., timer or occlusion,
can be active at a time.
OpenGL For occlusion queries, using GL ANY SAMPLES PASSED may be
more effective than GL
SAMPLES PASSED,asarenderingdoesnt
have to continue as soon as one fragment passed.
OpenGL
WebGL
OpenGL ES
For image-space rendering on GPUs with a large clipping guard
band clipping, e.g., GeForce, Radeon, and PowerVR series 6 use a
large clipped triangle instead of a quad. Measure both if in doubt.
OpenGL
WebGL
OpenGL ES
To test vertex throughput, do not render to a 1 × 1viewportbe-
cause parallelism is lost; instead, render outside of the view fr us-
tum.
OpenGL
WebGL
OpenGL ES
glGetError is par ticularly slow, especially in multi-process
WebGL architectures. Only use it in debug builds or instead use
GL
ARB debug output when available.
OpenGL Geometry shaders are usually output bound so spending ALU
time to reduce the amount of data output is a performance win.
WebGL In addition to #defining GL OES standard derivatives
before using dFdx, dFdy,andfwidth, also remember to call
context.getExtension("OES
standard derivatives")
in JavaScript.
OpenGL
WebGL
OpenGL ES
To accurately compute the length of a gradient, avoid
fwidth(v);insteadusesqrt(dFdx(v) * dFdx(v) +
dFdy(v) * dFdy(v)).
WebGL
OpenGL ES
highp is only available in fragment shaders if
GL
FRAGMENT PRECISION HIGH is #defined. Beware of
the performance implications of using highp in vertex or
fragment shaders.
xxx Tips
OpenGL In OpenGL, precision qualifiers were reserved in GLSL 1.20
and OpenGL 2.1 but actually introduced with GLSL 1.30
and OpenGL 3.0. From GLSL 1.40 a nd OpenGL 3.1,
and for the purpose of convergence with OpenGL ES 2.0,
GL
FRAGMENT PRECISION HIGH is defined as 1 in a fragment
shader.
OpenGL By default, precision for vertex, tessellation, and geometry shader
stages is highp for int types, and mediump for the fragment
shader stage int types. This may lead to warnings on some im-
plementations. float is always highp by default.
WebGL Given a WebGL context gl, gl.TRUE is undefined. When port-
ing OpenGL or OpenGL ES code, do not change GL
TRUE to
gl.TRUE because it will silently evaluate to false.
OpenGL
WebGL
OpenGL ES
Depth writes only occur if GL DEPTH TEST is enabled.
OpenGL
WebGL
OpenGL ES
The noise functions are still unimplemented in GLSL. Chapter 7
fixes this.
OpenGL gl VertexID get values in [first, first+count-1] when
generated from a DrawArray* command, and not in [0,
count-1]. Especially useful when using a zero input attributes
vertex shader.
OpenGL There are two ways to work with point size: glPointSize
in the client-side code or gl
PointSize in the GLSL code if
PROGRAM
POINT SIZE is enabled.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required