glsl: Stop being clever with pointer arithmetic when fetching types.
authorKenneth Graunke <kenneth@whitecape.org>
Tue, 18 Jun 2013 11:22:31 +0000 (04:22 -0700)
committerKenneth Graunke <kenneth@whitecape.org>
Wed, 26 Jun 2013 18:25:12 +0000 (11:25 -0700)
commit4530ed4f26bb887413a4ed4a5b6df7bf9ff9fc9e
treeb263f00b849f465a781d854c4de9430458e2e62b
parentd367a1cbdb816bcc50fb1fd10121de6eba53f7ab
glsl: Stop being clever with pointer arithmetic when fetching types.

Currently, vector types are linked together closely: the glsl_type
objects for float, vec2, vec3, and vec4 are all elements of the same
array, in that exact order.  This makes it possible to obtain vector
types via pointer arithmetic on the scalar type's convenience pointer.
For example, float_type + (3 - 1) = vec3.

However, relying on this is extremely fragile.  There's no particular
reason the underlying type objects need to be stored in an array.  They
could be individual class members, possibly with padding between them.
Then the pointer arithmetic would break, and we'd get bad pointers to
non-heap allocated data, causing subtle breakage that can't be detected
by valgrind.  Cue insanity.

Or someone could simply reorder the type variables, causing us to get
the wrong type entirely.  Also cue insanity.

Writing this explicitly is much safer.  With the new helper functions,
it's a bit less code even.

Signed-off-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Eric Anholt <eric@anholt.net>
src/glsl/glsl_types.cpp