OpenGL spec says that `GLint` must be 32 bits wide, but gl.xml naively defines it as `int` instead of `int32_t`. Why?

182 views Asked by At

OpenGL specification (glspec45.core.pdf) defines exact bit widths for all GL... types:

2.2.1 Data Conversion For State-Setting Commands
...
An implementation must use exactly the number of bits indicated in the table to represent a GL type.
...

But so called OpenGL registry (gl.xml - official parsable OpenGL API specification), naively defines those types as aliases to corresponding C types.

...
<type>typedef short <name>GLshort</name>;</type>
<type>typedef int <name>GLint</name>;</type>
<type>typedef int <name>GLclampx</name>;</type>
...

It is potentially unsafe.

Why fixed-width types weren't used instead?

Why no preprocessor logic to determine correct types? Why no compile-time checks of type sizes?

And what's more important, when making an OpenGL functions/extensions loader, how should I define those types?

2

There are 2 answers

4
Nicol Bolas On BEST ANSWER

It is potentially unsafe.

Yes, it is.

So?

As long as it is not actually unsafe, as long as you do not try to run this code on an odd-ball platform/compiler that doesn't provide the expected sizes for these types, it's fine. It works for GCC, Clang, VC++, and virtually every other major compiler. These definitions work on Windows, MacOS, Linux, Android, iOS, and plenty of other systems and ABIs.

I understand the desire to more strongly adhere to the C and C++ standards. But the reality is that these definitions are fine for most real systems in use. And for systems where they are not fine, it's up to the individual OpenGL providers for that system to provide alternative definitions for these.

Why fixed-width types weren't used instead?

Because OpenGL existed before such types did. Remember: they date from C99/C++11; OpenGL dates to 1992.

Why no compile-time checks of type sizes?

Back in 1992, C++ wasn't even standardized then. What "compile-time checks" could have existed back then? Also, OpenGL is defined in terms of C, and C's compile-time computation abilities consist primarily of extreme macro programming techniques.

And what's more important, when making an OpenGL functions/extensions loader, how should I define those types?

Unless you have a specific need to do otherwise, exactly as gl.xml says to define them.

0
Stefan On

I know this question is already answered, but I want to add in the glew.h file some of the OpenGL types are conditionally defined depending on compiler versions or operating system.

For example:

#if defined(_MSC_VER) && _MSC_VER < 1400
typedef __int64 GLint64EXT;
typedef unsigned __int64 GLuint64EXT;
#elif defined(_MSC_VER) || defined(__BORLANDC__)
typedef signed long long GLint64EXT;
typedef unsigned long long GLuint64EXT;
#else
#  if defined(__MINGW32__) || defined(__CYGWIN__)
#include <inttypes.h>
#  endif
typedef int64_t GLint64EXT;
typedef uint64_t GLuint64EXT;
#endif