1 /*
2  * Mesa 3-D graphics library
3  *
4  * Copyright (C) 1999-2006  Brian Paul   All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 
26 /**
27  * \file context.h
28  * Mesa context and visual-related functions.
29  *
30  * There are three large Mesa data types/classes which are meant to be
31  * used by device drivers:
32  * - struct gl_context: this contains the Mesa rendering state
33  * - struct gl_config:  this describes the color buffer (RGB vs. ci), whether
34  *   or not there's a depth buffer, stencil buffer, etc.
35  * - struct gl_framebuffer:  contains pointers to the depth buffer, stencil
36  *   buffer, accum buffer and alpha buffers.
37  *
38  * These types should be encapsulated by corresponding device driver
39  * data types.  See xmesa.h and xmesaP.h for an example.
40  *
41  * In OOP terms, struct gl_context, struct gl_config, and struct gl_framebuffer
42  * are base classes which the device driver must derive from.
43  *
44  * The following functions create and destroy these data types.
45  */
46 
47 
48 #ifndef CONTEXT_H
49 #define CONTEXT_H
50 
51 
52 #include "errors.h"
53 
54 #include "extensions.h"
55 #include "mtypes.h"
56 #include "vbo/vbo.h"
57 
58 
59 #ifdef __cplusplus
60 extern "C" {
61 #endif
62 
63 
64 struct _glapi_table;
65 
66 
67 /** \name Visual-related functions */
68 /*@{*/
69 
70 extern struct gl_config *
71 _mesa_create_visual( GLboolean dbFlag,
72                      GLboolean stereoFlag,
73                      GLint redBits,
74                      GLint greenBits,
75                      GLint blueBits,
76                      GLint alphaBits,
77                      GLint depthBits,
78                      GLint stencilBits,
79                      GLint accumRedBits,
80                      GLint accumGreenBits,
81                      GLint accumBlueBits,
82                      GLint accumAlphaBits,
83                      GLuint numSamples );
84 
85 extern GLboolean
86 _mesa_initialize_visual( struct gl_config *v,
87                          GLboolean dbFlag,
88                          GLboolean stereoFlag,
89                          GLint redBits,
90                          GLint greenBits,
91                          GLint blueBits,
92                          GLint alphaBits,
93                          GLint depthBits,
94                          GLint stencilBits,
95                          GLint accumRedBits,
96                          GLint accumGreenBits,
97                          GLint accumBlueBits,
98                          GLint accumAlphaBits,
99                          GLuint numSamples );
100 
101 extern void
102 _mesa_destroy_visual( struct gl_config *vis );
103 
104 /*@}*/
105 
106 
107 /** \name Context-related functions */
108 /*@{*/
109 
110 extern void
111 _mesa_initialize(void);
112 
113 extern GLboolean
114 _mesa_initialize_context( struct gl_context *ctx,
115                           gl_api api,
116                           const struct gl_config *visual,
117                           struct gl_context *share_list,
118                           const struct dd_function_table *driverFunctions);
119 
120 extern void
121 _mesa_free_context_data(struct gl_context *ctx);
122 
123 extern void
124 _mesa_destroy_context( struct gl_context *ctx );
125 
126 
127 extern void
128 _mesa_copy_context(const struct gl_context *src, struct gl_context *dst, GLuint mask);
129 
130 extern GLboolean
131 _mesa_make_current( struct gl_context *ctx, struct gl_framebuffer *drawBuffer,
132                     struct gl_framebuffer *readBuffer );
133 
134 extern GLboolean
135 _mesa_share_state(struct gl_context *ctx, struct gl_context *ctxToShare);
136 
137 extern struct gl_context *
138 _mesa_get_current_context(void);
139 
140 /*@}*/
141 
142 extern void
143 _mesa_init_constants(struct gl_constants *consts, gl_api api);
144 
145 extern void
146 _mesa_notifySwapBuffers(struct gl_context *gc);
147 
148 
149 extern struct _glapi_table *
150 _mesa_get_dispatch(struct gl_context *ctx);
151 
152 extern void
153 _mesa_set_context_lost_dispatch(struct gl_context *ctx);
154 
155 
156 
157 /** \name Miscellaneous */
158 /*@{*/
159 
160 extern void
161 _mesa_flush(struct gl_context *ctx);
162 
163 extern void GLAPIENTRY
164 _mesa_Finish( void );
165 
166 extern void GLAPIENTRY
167 _mesa_Flush( void );
168 
169 /*@}*/
170 
171 
172 /**
173  * Are we currently between glBegin and glEnd?
174  * During execution, not display list compilation.
175  */
176 static inline GLboolean
_mesa_inside_begin_end(const struct gl_context * ctx)177 _mesa_inside_begin_end(const struct gl_context *ctx)
178 {
179    return ctx->Driver.CurrentExecPrimitive != PRIM_OUTSIDE_BEGIN_END;
180 }
181 
182 
183 /**
184  * Are we currently between glBegin and glEnd in a display list?
185  */
186 static inline GLboolean
_mesa_inside_dlist_begin_end(const struct gl_context * ctx)187 _mesa_inside_dlist_begin_end(const struct gl_context *ctx)
188 {
189    return ctx->Driver.CurrentSavePrimitive <= PRIM_MAX;
190 }
191 
192 
193 
194 /**
195  * \name Macros for flushing buffered rendering commands before state changes,
196  * checking if inside glBegin/glEnd, etc.
197  */
198 /*@{*/
199 
200 /**
201  * Flush vertices.
202  *
203  * \param ctx GL context.
204  * \param newstate new state.
205  *
206  * Checks if dd_function_table::NeedFlush is marked to flush stored vertices,
207  * and calls dd_function_table::FlushVertices if so. Marks
208  * __struct gl_contextRec::NewState with \p newstate.
209  */
210 #define FLUSH_VERTICES(ctx, newstate)				\
211 do {								\
212    if (MESA_VERBOSE & VERBOSE_STATE)				\
213       _mesa_debug(ctx, "FLUSH_VERTICES in %s\n", __func__);	\
214    if (ctx->Driver.NeedFlush & FLUSH_STORED_VERTICES)		\
215       vbo_exec_FlushVertices(ctx, FLUSH_STORED_VERTICES);	\
216    ctx->NewState |= newstate;					\
217 } while (0)
218 
219 /**
220  * Flush current state.
221  *
222  * \param ctx GL context.
223  * \param newstate new state.
224  *
225  * Checks if dd_function_table::NeedFlush is marked to flush current state,
226  * and calls dd_function_table::FlushVertices if so. Marks
227  * __struct gl_contextRec::NewState with \p newstate.
228  */
229 #define FLUSH_CURRENT(ctx, newstate)				\
230 do {								\
231    if (MESA_VERBOSE & VERBOSE_STATE)				\
232       _mesa_debug(ctx, "FLUSH_CURRENT in %s\n", __func__);	\
233    if (ctx->Driver.NeedFlush & FLUSH_UPDATE_CURRENT)		\
234       vbo_exec_FlushVertices(ctx, FLUSH_UPDATE_CURRENT);	\
235    ctx->NewState |= newstate;					\
236 } while (0)
237 
238 /**
239  * Flush vertices.
240  *
241  * \param ctx GL context.
242  *
243  * Checks if dd_function_table::NeedFlush is marked to flush stored vertices
244  * or current state and calls dd_function_table::FlushVertices if so.
245  */
246 #define FLUSH_FOR_DRAW(ctx)                                     \
247 do {                                                            \
248    if (MESA_VERBOSE & VERBOSE_STATE)                            \
249       _mesa_debug(ctx, "FLUSH_FOR_DRAW in %s\n", __func__);     \
250    if (ctx->Driver.NeedFlush) {                                 \
251       if (ctx->_AllowDrawOutOfOrder) {                          \
252           if (ctx->Driver.NeedFlush & FLUSH_UPDATE_CURRENT)     \
253              vbo_exec_FlushVertices(ctx, FLUSH_UPDATE_CURRENT); \
254       } else {                                                  \
255          vbo_exec_FlushVertices(ctx, ctx->Driver.NeedFlush);    \
256       }                                                         \
257    }                                                            \
258 } while (0)
259 
260 /**
261  * Macro to assert that the API call was made outside the
262  * glBegin()/glEnd() pair, with return value.
263  *
264  * \param ctx GL context.
265  * \param retval value to return in case the assertion fails.
266  */
267 #define ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, retval)		\
268 do {									\
269    if (_mesa_inside_begin_end(ctx)) {					\
270       _mesa_error(ctx, GL_INVALID_OPERATION, "Inside glBegin/glEnd");	\
271       return retval;							\
272    }									\
273 } while (0)
274 
275 /**
276  * Macro to assert that the API call was made outside the
277  * glBegin()/glEnd() pair.
278  *
279  * \param ctx GL context.
280  */
281 #define ASSERT_OUTSIDE_BEGIN_END(ctx)					\
282 do {									\
283    if (_mesa_inside_begin_end(ctx)) {					\
284       _mesa_error(ctx, GL_INVALID_OPERATION, "Inside glBegin/glEnd");	\
285       return;								\
286    }									\
287 } while (0)
288 
289 /*@}*/
290 
291 
292 /**
293  * Checks if the context is for Desktop GL (Compatibility or Core)
294  */
295 static inline bool
_mesa_is_desktop_gl(const struct gl_context * ctx)296 _mesa_is_desktop_gl(const struct gl_context *ctx)
297 {
298    return ctx->API == API_OPENGL_COMPAT || ctx->API == API_OPENGL_CORE;
299 }
300 
301 
302 /**
303  * Checks if the context is for any GLES version
304  */
305 static inline bool
_mesa_is_gles(const struct gl_context * ctx)306 _mesa_is_gles(const struct gl_context *ctx)
307 {
308    return ctx->API == API_OPENGLES || ctx->API == API_OPENGLES2;
309 }
310 
311 
312 /**
313  * Checks if the context is for GLES 3.0 or later
314  */
315 static inline bool
_mesa_is_gles3(const struct gl_context * ctx)316 _mesa_is_gles3(const struct gl_context *ctx)
317 {
318    return ctx->API == API_OPENGLES2 && ctx->Version >= 30;
319 }
320 
321 
322 /**
323  * Checks if the context is for GLES 3.1 or later
324  */
325 static inline bool
_mesa_is_gles31(const struct gl_context * ctx)326 _mesa_is_gles31(const struct gl_context *ctx)
327 {
328    return ctx->API == API_OPENGLES2 && ctx->Version >= 31;
329 }
330 
331 
332 /**
333  * Checks if the context is for GLES 3.2 or later
334  */
335 static inline bool
_mesa_is_gles32(const struct gl_context * ctx)336 _mesa_is_gles32(const struct gl_context *ctx)
337 {
338    return ctx->API == API_OPENGLES2 && ctx->Version >= 32;
339 }
340 
341 
342 static inline bool
_mesa_is_no_error_enabled(const struct gl_context * ctx)343 _mesa_is_no_error_enabled(const struct gl_context *ctx)
344 {
345    return ctx->Const.ContextFlags & GL_CONTEXT_FLAG_NO_ERROR_BIT_KHR;
346 }
347 
348 
349 static inline bool
_mesa_has_integer_textures(const struct gl_context * ctx)350 _mesa_has_integer_textures(const struct gl_context *ctx)
351 {
352    return _mesa_has_EXT_texture_integer(ctx) || _mesa_is_gles3(ctx);
353 }
354 
355 static inline bool
_mesa_has_half_float_textures(const struct gl_context * ctx)356 _mesa_has_half_float_textures(const struct gl_context *ctx)
357 {
358    return _mesa_has_ARB_texture_float(ctx) ||
359           _mesa_has_OES_texture_half_float(ctx) || _mesa_is_gles3(ctx);
360 }
361 
362 static inline bool
_mesa_has_float_textures(const struct gl_context * ctx)363 _mesa_has_float_textures(const struct gl_context *ctx)
364 {
365    return _mesa_has_ARB_texture_float(ctx) ||
366           _mesa_has_OES_texture_float(ctx) || _mesa_is_gles3(ctx);
367  }
368 
369 static inline bool
_mesa_has_texture_rgb10_a2ui(const struct gl_context * ctx)370 _mesa_has_texture_rgb10_a2ui(const struct gl_context *ctx)
371 {
372    return _mesa_has_ARB_texture_rgb10_a2ui(ctx) || _mesa_is_gles3(ctx);
373 }
374 
375 static inline bool
_mesa_has_float_depth_buffer(const struct gl_context * ctx)376 _mesa_has_float_depth_buffer(const struct gl_context *ctx)
377 {
378    return _mesa_has_ARB_depth_buffer_float(ctx) || _mesa_is_gles3(ctx);
379 }
380 
381 static inline bool
_mesa_has_packed_float(const struct gl_context * ctx)382 _mesa_has_packed_float(const struct gl_context *ctx)
383 {
384    return _mesa_has_EXT_packed_float(ctx) || _mesa_is_gles3(ctx);
385 }
386 
387 static inline bool
_mesa_has_rg_textures(const struct gl_context * ctx)388 _mesa_has_rg_textures(const struct gl_context *ctx)
389 {
390    return _mesa_has_ARB_texture_rg(ctx) || _mesa_has_EXT_texture_rg(ctx) ||
391           _mesa_is_gles3(ctx);
392 }
393 
394 static inline bool
_mesa_has_texture_shared_exponent(const struct gl_context * ctx)395 _mesa_has_texture_shared_exponent(const struct gl_context *ctx)
396 {
397    return _mesa_has_EXT_texture_shared_exponent(ctx) || _mesa_is_gles3(ctx);
398 }
399 
400 static inline bool
_mesa_has_texture_type_2_10_10_10_REV(const struct gl_context * ctx)401 _mesa_has_texture_type_2_10_10_10_REV(const struct gl_context *ctx)
402 {
403    return _mesa_is_desktop_gl(ctx) ||
404           _mesa_has_EXT_texture_type_2_10_10_10_REV(ctx);
405 }
406 
407 /**
408  * Checks if the context supports geometry shaders.
409  */
410 static inline bool
_mesa_has_geometry_shaders(const struct gl_context * ctx)411 _mesa_has_geometry_shaders(const struct gl_context *ctx)
412 {
413    return _mesa_has_OES_geometry_shader(ctx) ||
414           (_mesa_is_desktop_gl(ctx) && ctx->Version >= 32);
415 }
416 
417 
418 /**
419  * Checks if the context supports compute shaders.
420  */
421 static inline bool
_mesa_has_compute_shaders(const struct gl_context * ctx)422 _mesa_has_compute_shaders(const struct gl_context *ctx)
423 {
424    return _mesa_has_ARB_compute_shader(ctx) ||
425       (ctx->API == API_OPENGLES2 && ctx->Version >= 31);
426 }
427 
428 /**
429  * Checks if the context supports tessellation.
430  */
431 static inline bool
_mesa_has_tessellation(const struct gl_context * ctx)432 _mesa_has_tessellation(const struct gl_context *ctx)
433 {
434    /* _mesa_has_EXT_tessellation_shader(ctx) is redundant with the OES
435     * check, so don't bother calling it.
436     */
437    return _mesa_has_OES_tessellation_shader(ctx) ||
438           _mesa_has_ARB_tessellation_shader(ctx);
439 }
440 
441 static inline bool
_mesa_has_texture_cube_map_array(const struct gl_context * ctx)442 _mesa_has_texture_cube_map_array(const struct gl_context *ctx)
443 {
444    return _mesa_has_ARB_texture_cube_map_array(ctx) ||
445           _mesa_has_OES_texture_cube_map_array(ctx);
446 }
447 
448 static inline bool
_mesa_has_texture_view(const struct gl_context * ctx)449 _mesa_has_texture_view(const struct gl_context *ctx)
450 {
451    return _mesa_has_ARB_texture_view(ctx) ||
452           _mesa_has_OES_texture_view(ctx);
453 }
454 
455 #ifdef __cplusplus
456 }
457 #endif
458 
459 
460 #endif /* CONTEXT_H */
461