• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2
3<head>
4<title>How to Create Khronos API Extensions</title>
5</head>
6
7<body>
8<h1>How to Create Khronos API Extensions</h1>
9
10<p> This document outlines the steps to create, define, and use
11    an extension for some of the APIs supported by Khronos. It
12    is currently focused on OpenGL, OpenGL ES, GLX, and EGL.
13    Some discussion of OpenVG and WGL is also included.
14
15<ul>
16    <li><a href="#spec">Specifying</a> an extension prior to coding it.
17    <li><a href="#implementation">Implementing</a> an extension (this
18	discusses only aspects of the implemention visible to users -
19	primarily determining if the extension is available and
20	defining its interface in header files.
21    <li><a href="#using">Using</a> an extension in an application (this
22	discusses the mechanics of using an extension if and only if
23	it's supported by an implementation).
24</ul>
25
26<p><hr>
27
28<a name="spec"></a>
29<h2>Specification</h2>
30
31<p> When initially creating an extension, take the following steps:
32
33<ul>
34    <li><a href="#spec_existing">Determine if an existing extension</a>
35	can be used, instead of writing a new one. This may involve
36	reuse of an existing specification, or
37	<a href="promoting.html">promotion</a> to multivendor or
38	Khronos-approved status. In either case, coordinate this use
39	with the vendor(s) defining and shipping the existing extension.
40    <li>If no suitable extension already exists, determine if you can
41	<a href="#spec_category">agree on a multivendor or Khronos
42	extension</a>, or if the extension must be specific to a single
43	vendor.
44    <li>Develop and <a href="#spec_writing">write an extension
45	specification</a>, following the <a href="#spec_naming">naming
46	conventions</a>.
47    <li>When you're ready to release the extension, freeze the
48	specification and <a href="#spec_registry">add it to the
49	registry</a> maintained by Khronos. At this time (but not
50	before), you can obtain permanent token (enumerant) assignments.
51    <li>When shipping an extension, make sure that its interfaces are
52	defined in header files accessible to ISVs. If the extension is
53	Khronos-approved, use the Khronos-provided headers.
54</ul>
55
56<a name="spec_existing"></a>
57<h3>Is there already an extension that does what I want?</h3>
58
59<p> Specifications for extensions that have already been developed can
60be obtained from the <a href="http://opengl.org/registry/"> Extension
61Registry</a> maintained by Khronos. Since we are just getting started
62with most of the Khronos APIs, the registry currently contains only
63OpenGL, GLX, and WGL extension specifications.
64
65<p> It's possible that additional extensions may have been submitted to
66the registry but not yet updated on the website, or that another
67licensee may be working on a similar extension but not yet have released
68the specification. So it's worth asking on the appropriate Working
69Group mailing list if anyone has defined related functionality already.
70
71<a name="spec_category"></a>
72<h3>Should the extension be Khronos-approved, multivendor, or specific
73    to a single vendor?</h3>
74
75<p> OpenGL's history has made clear that ISVs do <b>not</b> want to deal
76    with vendor-specific extensions if they can possibly avoid it. So if
77    the functionality being exposed is going to be available on multiple
78    platforms - as most will - it's a good idea to agree on a single
79    extension with other vendors providing that functionality. This
80    makes it easier for ISVs to justify using extensions.
81
82<p> If the functionality is well-understood, it may be appropriate to
83    define a <b>Khronos-approved extension</b>. This is the most
84    &quot;blessed&quot; category of extension; it goes through the
85    entire standards process, and is approved by the group, but remains
86    optional functionality. Many core features have been promoted
87    directly from existing Khronos-approved extensions.
88
89<p> If Khronos as a whole isn't ready to deal with the extension, but
90    other vendors are, then it should be defined as a <b>multivendor
91    extension</b>. The interested parties can develop the specification
92    entirely among themselves, outside the standards process; or they
93    may be able to use Khronos Working Groups as a forum to develop the
94    specification.
95
96<p> In some cases, vendors may share a common core of functionality,
97    with vendor-specific additional features. Here, it may make sense to
98    agree on a multivendor extension to access the core, with additional
99    vendor-specific extensions layered on the core exposing unique
100    features.
101
102<p> Finally, some extensions will probably have to remain proprietary.
103
104<a name="spec_writing"></a>
105<h3>How do I write a specification?</h3>
106
107<p> Start with the <a href="template.txt">template</a> for writing
108    extension specifications. There are different templates for
109    different APIs, but general comments apply:
110
111<ul>
112<li> It's important to think about all the different areas of the core
113    language specification(s) that are affected. How are queries for
114    state handled? What attribute group does new state belong in? How
115    are existing calls modified or expanded in scope? How are existing
116    objects affected by use of the extension? The template helps with
117    this by at least reminding you to consider each part of the core
118    language specification.
119<li> Extension specifications must be written against a specific version
120    of the core API specification. If possible, it's highly preferable
121    to write against the most recent public version. &quot;Written
122    against&quot; means that new language must be written as
123    well-defined modifications to the Specification being referenced. It
124    should be possible for someone not involved with the development of
125    an extension to sit down with a copy of the Specification and the
126    extension and produce a merged document identical to that you
127    intended.
128<li> Extension specifications may also be specified as Docbook XML
129    documents, although we're still using plain text for the most part.
130    Docbook is a very powerful structural representation of documents,
131    but we're still feeling our way into using it. Known <a
132    href="toolchain.html">Docbook tools and editors</a> are described
133    elsewhere.
134</ul>
135
136<p> One complete, shipping example to refer to is the
137    <a href="../specs/EXT/fog_coord.txt">Fog Coordinate</a> OpenGL extension
138    specification.
139<!-- (<a href="fog_coord.xml">Docbook source</a>). -->
140
141<a name="spec_naming"></a>
142<h3>Naming Conventions</h3>
143
144<p> API entry points and enumerants in an extension must be named
145according to the <a href="syntaxrules.txt">syntax rules</a> specific to
146that API. In particular, follow the sections &quot;Extension name
147rules&quot; and &quot;Shared extensions&quot;.
148
149<p> All extensions must be named and the name included in the extension
150specification. The extension name is of the form
151<tt>&quot;api_category_name&quot;</tt> where
152
153<ul>
154<li> <tt>&quot;api&quot;</tt> identifies the API
155    (<tt>&quot;GL&quot;</tt> for OpenGL and OpenGL ES;
156    <tt>&quot;EGL&quot;</tt>, <tt>&quot;GLX&quot;</tt>, and
157    <tt>&quot;WGL&quot;</tt> for the corresponding window-system
158    abstraction APIs EGL, GLX, and WGL; <tt>&quot;GLU&quot;</tt> for the
159    OpenGL Utility Library; and <tt>&quot;OVG&quot;</tt> for OpenVG.
160
161    (<b>Note</b>: OpenMAX, OpenSL, OpenML TBD).
162
163<li> <tt>&quot;category&quot;</tt> identifies the extension category.
164    For a vendor-specific extension, the category is a 2-3 letter string
165    identifying the vendor: <tt>&quot;NV&quot;</tt> for NVIDIA,
166    <tt>&quot;ATI&quot;</tt> for ATI, etc. For a multivendor extensions,
167    the category is <tt>&quot;EXT&quot;</tt>. For Khronos-approved
168    extensions, one of several API-specific categories is used
169    (<tt>&quot;ARB&quot;</tt> for OpenGL extensions,
170    <tt>&quot;OES&quot;</tt> for OpenGL ES extensions,
171    <tt>&quot;OML&quot;</tt> for OpenML extensions, and so on).
172
173<li> <tt>&quot;name&quot;</tt> is one or more words separated by
174    underscores, providing a short label for the extension.
175    Conventionally the name is entirely lower-case.
176</ul>
177
178<p> For example, the extension name
179    <tt>&quot;GL_EXT_framebuffer_object&quot;</tt> is used for a
180    multivendor OpenGL extension adding support for framebuffer objects.
181
182<p> Choose names that are
183
184<ul>
185<li> Short.
186<li> Meaningful.
187<li> Not prefixes of any existing extension name (disregarding the
188    category). For example, <tt>&quot;GL_ARB_fog&quot;</tt> is a prefix
189    of <tt>&quot;GL_EXT_fog_coord&quot;</tt>, so should not be chosen.
190<li> Not prefixed by any existing extension name (again, disregarding
191    the category).
192</ul>
193
194<p> The goal is for names to be clear, but not at the cost of confusion
195    or ambiguity.
196
197
198<h4> Advertising Extension Names to Applications </h4>
199
200<p> Each Khronos API provides a way of describing the supported
201    extensions at compile- and run-time. This is done by a combination
202    of preprocessor tokens in header files, and queryable extension
203    strings.
204
205<p> <b>OpenGL and OpenGL ES</b> #define a preprocessor token
206    corresponding to the extension name in <tt>&lt;GL/gl.h&gt;</tt> (or
207    an include file that <tt>gl.h</tt> includes, such as the
208    <tt>glext.h</tt> header provided in the registry). When this token
209    is defined, it indicates that the function prototypes and enumerant
210    definitions required to use the extension are available at compile
211    time.
212
213<p> If an OpenGL or OpenGL ES extension is supported at runtime, the
214    extension name must also be included in the string returned by
215    <tt>glGetString(GL_EXTENSIONS)</tt>.
216
217<p> <b>GLX</b> #defines a preprocessor token corresponding to the
218    extension name in <tt>&lt;GL/glx.h&gt;</tt> (or an include file that
219    <tt>glx.h</tt> includes, such as the <tt>glxext.h</tt> header
220    provided in the registry). When this token is defined, it indicates
221    that the function prototypes and enumerant definitions required to
222    use the extension are available at compile time.
223
224<p> If a GLX extension is supported at runtime, the extension name must
225    also be included in the strings returned by
226    <b>glXQueryExtensionsString</b>, <b>glXGetClientString</b>, and/or
227    <b>glXQueryServerString</b> (see below for a description of the
228    different routines).
229
230<p> <b>WGL</b> #defines a preprocessor token corresponding to the
231    extension name in the <tt>wglext.h</tt> header provided in the
232    registry (the wgl.h supplied with Microsoft Windows does not
233    #include <tt>wglext.h</tt>, or define any extensions itself). When
234    this token is defined, it indicates that the function prototypes and
235    enumerant definitions required to use the extension are available at
236    compile time.
237
238<p> If a WGL extension is supported at runtime, the extension name must
239    also be included in the string returned by
240    <b>wglGetExtensionsStringEXT</b>.
241
242<p> <b>OpenVG</b> extension conventions are <i>To Be Determined</i>.
243
244<p> Note that extensions can have both OpenGL components and windowing
245    system components. For example, the ARB multisampling extension
246    modifies both GLX and OpenGL. In this case there will be two tokens
247    associated with the extension (e.g., <tt>GL_ARB_multisample</tt> and
248    <tt>GLX_ARB_multisample</tt>) and the extension will be advertised
249    by both OpenGL and GLX.
250
251<a name="spec_registry"></a>
252<h3>Extension Registry</h3>
253
254<p> Khronos keeps a registry of extension specifications, enumerated
255    type values, GLX codes (vendor private opcodes, vendor private with
256    reply opcodes, new visual attribute type values, GLX error codes and
257    GLX event codes), OpenGL rendering codes for GLX, and OpenGL
258    rendering codes for GLS and extension numbers. Vendors shipping
259    extensions using any of these values must obtain them from Khronos.
260
261<p> If an extension defines new OpenGL enumerant names, values for those
262    names must be requested in one or more blocks of 16 values. If an
263    extension defines new OpenGL rendering commands then you need to
264    register GLS rendering codes for it. If you want the extensions to
265    work with the X windowing system (i.e., with GLX), then you must
266    request GLX opcodes and define GLX protocol for it.
267
268<p> There are detailed <a href="enums.html">enumerant allocation
269    policies</a> for OpenGL, GLX, and WGL enumerants.
270
271<p> All new extensions must have a number associated with them for
272    documentation purposes. If an extension depends on another
273    extension, the other extension must have a lower number. (Note that
274    when an extension is deprecated, the number associated with it is
275    not reassigned.) This number will also be assigned by Khronos when
276    you register the extension.
277
278<p> Include all new enumerated values, GLX codes, and the extension
279    number in the specification.
280
281<p> Once you have completed the extension, please make it available to
282    other Khronos members and application developers, by submitting the
283    extension specification to the Khronos Registrar for inclusion in
284    the public registry.
285
286<p><hr>
287
288<a name="implementation"></a>
289<h2>Implementing and Advertising Extensions</h2>
290
291<h4><u>Errors</u></h4>
292
293<p> Whenever possible, extensions should use existing errors instead of
294    defining new error returns. For GLX, if a new protocol error is
295    introduced, then an error number must be obtained from and
296    registered with Khronos.
297
298<p> Vendors may ship a single OpenGL library, containing extensions, for
299    a variety of platforms. It is possible that some of the extension
300    routines defined in the library may not be supported on some of the
301    platforms. If this is the case and an application calls a routine
302    that is not supported by the current OpenGL renderer then a
303    <tt>GL_INVALID_OPERATION</tt> error should be returned.
304
305<h4><u>OpenGL Extension Availability</u></h4>
306
307<p> OpenGL extensions must be advertised in the extension string
308    returned by <b>glGetString</b>. Note that in a client-server
309    environment, this call returns the set of extensions that can be
310    supported on the connection. GLX client libraries must send a
311    <b>glXClientInfo</b> request to the server at start up time (if the
312    client libarary is 1.1 or later) indicating the version of the
313    client library and the OpenGL extensions that it supports. Then,
314    when <b>glGetString</b> is called, the client issues a GetString
315    request. The server intersects the set of extensions that the client
316    supports with the set of extensions that it supports (if a
317    <b>glXClientInfo</b> request was never received then the server
318    assumes that the client supports no OpenGL extensions) and returns
319    the result to the client. The client library then appends any
320    client-side only extensions to the list and returns the result.
321
322<p> Extension names for all known OpenGL extensions are #defined in the
323    <tt>glext.h</tt> header included in the registry.
324
325<h4><u>EGL Extension Availability</u></h4>
326
327<p> EGL extensions must be advertised in the extension string returned
328    by <tt>eglQueryString(EGL_EXTENSIONS)</tt>. Extension names for all
329    known EGL extensions are #defined in the <tt>eglext.h</tt> header
330    included in the registry.
331
332<h4><u>GLX Extension Availability</u></h4>
333
334<p> GLX client-side extensions must be advertised in the extension
335    string returned by <b>glXGetClientString</b>; server-side extensions
336    must be advertised in the extension string returned by
337    <b>glXQueryServerString</b>.
338
339<p> <b>glXQueryExtensionsString</b> returns the list of extensions that
340    can be supported on the connection. The client then issues a
341    <b>glXQueryServerString</b> request, intersects the returned string
342    with the set of extensions it can support and then appends any
343    client-side only extensions to the list.
344
345<p> Extension names for all known GLX extensions are #defined in the
346    <tt>glxext.h</tt> header included in the registry.
347
348<h4><u>WGL Extension Availability</u></h4>
349
350<p> WGL initially had no mechanism for returning its own extensions
351    string. For this reason, WGL extension names were initially
352    advertised in the GL extensions string returned by
353    <b>glGetString</b>. With the creation of a more formal WGL extension
354    mechanism, all implementations offering WGL extensions should export
355    the <tt>WGL_EXT_extensions_string</tt> extension, and should
356    advertise WGL extensions in the extensions string returned by the
357    <b>wglGetExtensionsStringEXT</b> interface defined by
358    <tt>WGL_EXT_extensions_string</tt>, as well as via
359    <b>glGetString</b>, for compatibility with older programs.</p>
360
361<p> Extension names for all known WGL extensions are #defined in the
362    <tt>wglext.h</tt> header included in the registry.
363
364<p><hr>
365
366<a name="using"></a>
367<h2>Use of Extensions</h2>
368
369<h4><u>Using OpenGL Extensions</u></h4>
370
371<p> Programmers that wish to use a particular OpenGL extension should
372    check both compile-time defines (to ensure that the extension is
373    supported by the library they are compiling against) and the
374    extension string returned by <b>glGetString</b> (to ensure that the
375    renderer supports the extension). For Windows, extensions usually
376    are not defined at link time, and function pointers to extension
377    APIs should be obtained by calling <b>wglGetProcAddress</b>.
378
379<p> For example, the following code could be used to check whether the
380    renderer supports an OpenGL extension called
381    <tt>GL_EXT_new_extension</tt>. This code would need to be executed
382    after the context had been made current:
383
384<blockquote><pre><tt>
385    static GLboolean CheckExtension(char *extName, const char *extString)
386    {
387	/*
388	 ** Search for extName in the extensions string.  Use of strstr()
389	 ** is not sufficient because extension names can be prefixes of
390	 ** other extension names.  Could use strtok() but the constant
391	 ** string returned by glGetString can be in read-only memory.
392	 */
393	char *p = (char *)extString;
394	char *end;
395	int extNameLen;
396
397	extNameLen = strlen(extName);
398	end = p + strlen(p);
399
400	while (p < end) {
401	    int n = strcspn(p, " ");
402	    if ((extNameLen == n) && (strncmp(extName, p, n) == 0)) {
403		return GL_TRUE;
404	    }
405	    p += (n + 1);
406	}
407	return GL_FALSE;
408    }
409
410    const GLubyte *ext_string;
411    int new_ext_supported = GL_FALSE;
412
413    if (CheckExtension("GL_EXT_new_extension", glGetString(GL_EXTENSIONS)))
414	new_ext_supported = GL_TRUE;
415</tt></pre></blockquote>
416
417<p> If the renderer supports the extension, then it is safe to use it at
418    runtime. (Note that in a client-server environment,
419    <b>glGetString</b> will only return the set of extensions that can
420    be supported by the client and server.) However, compile time checks
421    must be made to ensure that the library that you are linked against
422    supports the extension. For example:
423
424<blockquote><pre><tt>
425    #ifdef GL_EXT_new_extension
426	if (new_ext_supported)
427	    glNewExtensionEXT()
428    #endif
429</tt></pre></blockquote>
430
431<p> For a Windows OpenGL implementation, extensions are usually
432    dynamically loaded from the device driver, rather than statically
433    linked. Function pointers to extension APIs are obtained from
434    <b>wglGetProcAddress</b> and use to invoke the extension. For
435    example:
436
437<blockquote><pre><tt>
438    typedef void (WINAPI *PFNGLNEWEXTENSIONEXTPROC)();
439    PFNGLNEWEXTENSIONEXTPROC *glNewExtensionEXT = NULL;
440
441    /* Do this once, after context creation */
442    #ifdef GL_EXT_new_extension
443	if (new_ext_supported)
444	    glNewExtensionEXT = (PFNGLNEWEXTENSIONEXTPROC)
445		wglGetProcAddress("glNewExtensionEXT");
446    #endif
447
448    /* Do this when calling the extension */
449    #ifdef GL_EXT_new_extension
450	if (new_ext_supported)
451	    (*glNewExtensionEXT)();
452    #endif
453</tt></pre></blockquote>
454
455<h4><u>Using EGL Extensions</u></h4>
456
457<p> Before using an EGL extension, check for the extension name in both
458    the compile-time #defines and the extension string returned by
459    <tt>eglGetString(EGL_EXTENSIONS)</tt>. For example, this code could
460    be used to check whether an extension called
461    <tt>EGL_OES_new_extension</tt> can be used.
462
463<blockquote><pre><tt>
464	EGLDisplay dpy; // Initialized elsewhere
465	int new_ext_supported = FALSE;
466
467	if (CheckExtension("EGL_OES_new_extension",
468			       eglGetString(display, EGL_EXTENSIONS));
469		new_ext_supported = TRUE;
470    #endif
471</tt></pre></blockquote>
472
473<p> If the extension is supported, then it is safe to use it at runtime.
474    However, compile time checks must be made to ensure that you can
475    call the extension. For example:
476
477<blockquote><pre><tt>
478    #ifdef EGL_OES_new_extension
479	if (new_ext_supported)
480	    eglNewExtensionEXT(...);
481    #endif
482</tt></pre></blockquote>
483
484<h4><u>Using GLX Extensions</u></h4>
485
486<p> Before using a GLX extension, programmers should check the compile
487    time defines and the extension string returned by
488    <b>glXQueryExtensionsString</b>.
489
490<p> The following code could be used to check whether an extension
491    called <tt>GLX_EXT_new_extension</tt> can be used on the connection.
492    This code would be executed after the connection had been opened and
493    the existence of the GLX extension had been established.
494
495<blockquote><pre><tt>
496	Display *dpy;
497	int new_ext_supported = GL_FALSE;
498	int major, minor, screen;
499
500	if (!glXQueryVersion(dpy, &major, &minor))
501	    exit(1);
502	screen = DefaultScreen(dpy);
503
504    #ifdef GLX_VERSION_1_1
505	if (minor > 0 || major > 1)
506	    if (CheckExtension("GLX_EXT_new_extension",
507			       glXQueryExtensionsString(dpy, screen)))
508		new_ext_supported = GL_TRUE;
509    #endif
510</tt></pre></blockquote>
511
512<p> If the extension is supported on the connection, then it is safe to
513    use it at runtime. However, compile time checks must be made to
514    ensure that the library that you are linked against supports the
515    extension. For example:
516
517<blockquote><pre><tt>
518    #ifdef GLX_EXT_new_extension
519	if (new_ext_supported)
520	    glXNewExtensionEXT(...);
521    #endif
522</tt></pre></blockquote>
523
524<h4><u>Using WGL Extensions</u></h4>
525
526<p> Before using a WGL extension, check for its presence in the WGL
527    extensions string. Note that the WGL extension string query is
528    itself an extension; if not supported, WGL extensions are also
529    advertised in the base GL extensions string.
530
531    <p> <i>Code snippet should go here</i>
532
533<p> <hr>
534
535<p> Last modified August 13, 2006 by Jon Leech
536
537</body>
538</html>
539