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 Context-related functions */
68 /*@{*/
69
70 extern void
71 _mesa_initialize(const char *extensions_override);
72
73 extern GLboolean
74 _mesa_initialize_context( struct gl_context *ctx,
75 gl_api api,
76 bool no_error,
77 const struct gl_config *visual,
78 struct gl_context *share_list,
79 const struct dd_function_table *driverFunctions);
80
81 extern struct _glapi_table *
82 _mesa_alloc_dispatch_table(bool glthread);
83
84 extern void
85 _mesa_init_dispatch(struct gl_context *ctx);
86
87 extern bool
88 _mesa_alloc_dispatch_tables(gl_api api, struct gl_dispatch *d, bool glthread);
89
90 extern bool
91 _mesa_initialize_dispatch_tables(struct gl_context *ctx);
92
93 extern struct _glapi_table *
94 _mesa_new_nop_table(unsigned numEntries, bool glthread);
95
96 extern void
97 _mesa_free_context_data(struct gl_context *ctx, bool destroy_debug_output);
98
99 extern void
100 _mesa_copy_context(const struct gl_context *src, struct gl_context *dst, GLuint mask);
101
102 extern GLboolean
103 _mesa_make_current( struct gl_context *ctx, struct gl_framebuffer *drawBuffer,
104 struct gl_framebuffer *readBuffer );
105
106 extern GLboolean
107 _mesa_share_state(struct gl_context *ctx, struct gl_context *ctxToShare);
108
109 extern struct gl_context *
110 _mesa_get_current_context(void);
111
112 /*@}*/
113
114 extern void
115 _mesa_init_constants(struct gl_constants *consts, gl_api api);
116
117 extern void
118 _mesa_set_context_lost_dispatch(struct gl_context *ctx);
119
120
121
122 /** \name Miscellaneous */
123 /*@{*/
124
125 extern void
126 _mesa_flush(struct gl_context *ctx);
127
128 /*@}*/
129
130
131 /**
132 * Are we currently between glBegin and glEnd?
133 * During execution, not display list compilation.
134 */
135 static inline GLboolean
_mesa_inside_begin_end(const struct gl_context * ctx)136 _mesa_inside_begin_end(const struct gl_context *ctx)
137 {
138 return ctx->Driver.CurrentExecPrimitive != PRIM_OUTSIDE_BEGIN_END;
139 }
140
141
142 /**
143 * Are we currently between glBegin and glEnd in a display list?
144 */
145 static inline GLboolean
_mesa_inside_dlist_begin_end(const struct gl_context * ctx)146 _mesa_inside_dlist_begin_end(const struct gl_context *ctx)
147 {
148 return ctx->Driver.CurrentSavePrimitive <= PRIM_MAX;
149 }
150
151
152
153 /**
154 * \name Macros for flushing buffered rendering commands before state changes,
155 * checking if inside glBegin/glEnd, etc.
156 */
157 /*@{*/
158
159 /**
160 * Flush vertices.
161 *
162 * \param ctx GL context.
163 * \param newstate new state.
164 *
165 * Checks if dd_function_table::NeedFlush is marked to flush stored vertices,
166 * and calls dd_function_table::FlushVertices if so. Marks
167 * __struct gl_contextRec::NewState with \p newstate.
168 */
169 #define FLUSH_VERTICES(ctx, newstate, pop_attrib_mask) \
170 do { \
171 if (MESA_VERBOSE & VERBOSE_STATE) \
172 _mesa_debug(ctx, "FLUSH_VERTICES in %s\n", __func__); \
173 if (ctx->Driver.NeedFlush & FLUSH_STORED_VERTICES) \
174 vbo_exec_FlushVertices(ctx, FLUSH_STORED_VERTICES); \
175 ctx->NewState |= newstate; \
176 ctx->PopAttribState |= pop_attrib_mask; \
177 } while (0)
178
179 /**
180 * Flush current state.
181 *
182 * \param ctx GL context.
183 * \param newstate new state.
184 *
185 * Checks if dd_function_table::NeedFlush is marked to flush current state,
186 * and calls dd_function_table::FlushVertices if so. Marks
187 * __struct gl_contextRec::NewState with \p newstate.
188 */
189 #define FLUSH_CURRENT(ctx, newstate) \
190 do { \
191 if (MESA_VERBOSE & VERBOSE_STATE) \
192 _mesa_debug(ctx, "FLUSH_CURRENT in %s\n", __func__); \
193 if (ctx->Driver.NeedFlush & FLUSH_UPDATE_CURRENT) \
194 vbo_exec_FlushVertices(ctx, FLUSH_UPDATE_CURRENT); \
195 ctx->NewState |= newstate; \
196 } while (0)
197
198 /**
199 * Flush vertices.
200 *
201 * \param ctx GL context.
202 *
203 * Checks if dd_function_table::NeedFlush is marked to flush stored vertices
204 * or current state and calls dd_function_table::FlushVertices if so.
205 */
206 #define FLUSH_FOR_DRAW(ctx) \
207 do { \
208 if (MESA_VERBOSE & VERBOSE_STATE) \
209 _mesa_debug(ctx, "FLUSH_FOR_DRAW in %s\n", __func__); \
210 if (ctx->Driver.NeedFlush) { \
211 if (ctx->_AllowDrawOutOfOrder) { \
212 if (ctx->Driver.NeedFlush & FLUSH_UPDATE_CURRENT) \
213 vbo_exec_FlushVertices(ctx, FLUSH_UPDATE_CURRENT); \
214 } else { \
215 vbo_exec_FlushVertices(ctx, ctx->Driver.NeedFlush); \
216 } \
217 } \
218 } while (0)
219
220 /**
221 * Macro to assert that the API call was made outside the
222 * glBegin()/glEnd() pair, with return value.
223 *
224 * \param ctx GL context.
225 * \param retval value to return in case the assertion fails.
226 */
227 #define ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, retval) \
228 do { \
229 if (_mesa_inside_begin_end(ctx)) { \
230 _mesa_error(ctx, GL_INVALID_OPERATION, "Inside glBegin/glEnd"); \
231 return retval; \
232 } \
233 } while (0)
234
235 /**
236 * Macro to assert that the API call was made outside the
237 * glBegin()/glEnd() pair.
238 *
239 * \param ctx GL context.
240 */
241 #define ASSERT_OUTSIDE_BEGIN_END(ctx) \
242 do { \
243 if (_mesa_inside_begin_end(ctx)) { \
244 _mesa_error(ctx, GL_INVALID_OPERATION, "Inside glBegin/glEnd"); \
245 return; \
246 } \
247 } while (0)
248
249 /*@}*/
250
251
252 /**
253 * Checks if the context is for Desktop GL Compatibility
254 */
255 static inline bool
_mesa_is_desktop_gl_compat(const struct gl_context * ctx)256 _mesa_is_desktop_gl_compat(const struct gl_context *ctx)
257 {
258 #if HAVE_OPENGL
259 return ctx->API == API_OPENGL_COMPAT;
260 #else
261 return false;
262 #endif
263 }
264
265 /**
266 * Checks if the context is for Desktop GL Core
267 */
268 static inline bool
_mesa_is_desktop_gl_core(const struct gl_context * ctx)269 _mesa_is_desktop_gl_core(const struct gl_context *ctx)
270 {
271 #if HAVE_OPENGL
272 return ctx->API == API_OPENGL_CORE;
273 #else
274 return false;
275 #endif
276 }
277
278 /**
279 * Checks if the context is for Desktop GL (Compatibility or Core)
280 */
281 static inline bool
_mesa_is_desktop_gl(const struct gl_context * ctx)282 _mesa_is_desktop_gl(const struct gl_context *ctx)
283 {
284 return _mesa_is_desktop_gl_compat(ctx) || _mesa_is_desktop_gl_core(ctx);
285 }
286
287 /**
288 * Checks if the context is for GLES 1.0
289 */
290 static inline bool
_mesa_is_gles1(const struct gl_context * ctx)291 _mesa_is_gles1(const struct gl_context *ctx)
292 {
293 #if HAVE_OPENGL_ES_1
294 return ctx->API == API_OPENGLES;
295 #else
296 return false;
297 #endif
298 }
299
300 /**
301 * Checks if the context is for GLES 2.0 or later
302 */
303 static inline bool
_mesa_is_gles2(const struct gl_context * ctx)304 _mesa_is_gles2(const struct gl_context *ctx)
305 {
306 return _mesa_is_api_gles2(ctx->API);
307 }
308
309 /**
310 * Checks if the context is for any GLES version
311 */
312 static inline bool
_mesa_is_gles(const struct gl_context * ctx)313 _mesa_is_gles(const struct gl_context *ctx)
314 {
315 return _mesa_is_gles1(ctx) || _mesa_is_gles2(ctx);
316 }
317
318 /**
319 * Checks if the context is for GLES 3.0 or later
320 */
321 static inline bool
_mesa_is_gles3(const struct gl_context * ctx)322 _mesa_is_gles3(const struct gl_context *ctx)
323 {
324 return _mesa_is_gles2(ctx) && ctx->Version >= 30;
325 }
326
327
328 /**
329 * Checks if the context is for GLES 3.1 or later
330 */
331 static inline bool
_mesa_is_gles31(const struct gl_context * ctx)332 _mesa_is_gles31(const struct gl_context *ctx)
333 {
334 return _mesa_is_gles2(ctx) && ctx->Version >= 31;
335 }
336
337
338 /**
339 * Checks if the context is for GLES 3.2 or later
340 */
341 static inline bool
_mesa_is_gles32(const struct gl_context * ctx)342 _mesa_is_gles32(const struct gl_context *ctx)
343 {
344 return _mesa_is_gles2(ctx) && ctx->Version >= 32;
345 }
346
347
348 static inline bool
_mesa_is_no_error_enabled(const struct gl_context * ctx)349 _mesa_is_no_error_enabled(const struct gl_context *ctx)
350 {
351 return ctx->Const.ContextFlags & GL_CONTEXT_FLAG_NO_ERROR_BIT_KHR;
352 }
353
354
355 static inline bool
_mesa_has_integer_textures(const struct gl_context * ctx)356 _mesa_has_integer_textures(const struct gl_context *ctx)
357 {
358 return _mesa_has_EXT_texture_integer(ctx) || _mesa_is_gles3(ctx);
359 }
360
361 static inline bool
_mesa_has_half_float_textures(const struct gl_context * ctx)362 _mesa_has_half_float_textures(const struct gl_context *ctx)
363 {
364 return _mesa_has_ARB_texture_float(ctx) ||
365 _mesa_has_OES_texture_half_float(ctx);
366 }
367
368 static inline bool
_mesa_has_float_textures(const struct gl_context * ctx)369 _mesa_has_float_textures(const struct gl_context *ctx)
370 {
371 return _mesa_has_ARB_texture_float(ctx) ||
372 _mesa_has_OES_texture_float(ctx) || _mesa_is_gles3(ctx);
373 }
374
375 static inline bool
_mesa_has_texture_rgb10_a2ui(const struct gl_context * ctx)376 _mesa_has_texture_rgb10_a2ui(const struct gl_context *ctx)
377 {
378 return _mesa_has_ARB_texture_rgb10_a2ui(ctx) || _mesa_is_gles3(ctx);
379 }
380
381 static inline bool
_mesa_has_float_depth_buffer(const struct gl_context * ctx)382 _mesa_has_float_depth_buffer(const struct gl_context *ctx)
383 {
384 return _mesa_has_ARB_depth_buffer_float(ctx) || _mesa_is_gles3(ctx);
385 }
386
387 static inline bool
_mesa_has_packed_float(const struct gl_context * ctx)388 _mesa_has_packed_float(const struct gl_context *ctx)
389 {
390 return _mesa_has_EXT_packed_float(ctx) || _mesa_is_gles3(ctx);
391 }
392
393 static inline bool
_mesa_has_rg_textures(const struct gl_context * ctx)394 _mesa_has_rg_textures(const struct gl_context *ctx)
395 {
396 return _mesa_has_ARB_texture_rg(ctx) || _mesa_has_EXT_texture_rg(ctx) ||
397 _mesa_is_gles3(ctx);
398 }
399
400 static inline bool
_mesa_has_texture_shared_exponent(const struct gl_context * ctx)401 _mesa_has_texture_shared_exponent(const struct gl_context *ctx)
402 {
403 return _mesa_has_EXT_texture_shared_exponent(ctx) || _mesa_is_gles3(ctx);
404 }
405
406 static inline bool
_mesa_has_texture_type_2_10_10_10_REV(const struct gl_context * ctx)407 _mesa_has_texture_type_2_10_10_10_REV(const struct gl_context *ctx)
408 {
409 return _mesa_is_desktop_gl(ctx) ||
410 _mesa_has_EXT_texture_type_2_10_10_10_REV(ctx);
411 }
412
413 /**
414 * Checks if the context supports geometry shaders.
415 */
416 static inline bool
_mesa_has_geometry_shaders(const struct gl_context * ctx)417 _mesa_has_geometry_shaders(const struct gl_context *ctx)
418 {
419 return _mesa_has_OES_geometry_shader(ctx) ||
420 (_mesa_is_desktop_gl(ctx) && ctx->Version >= 32);
421 }
422
423
424 /**
425 * Checks if the context supports compute shaders.
426 */
427 static inline bool
_mesa_has_compute_shaders(const struct gl_context * ctx)428 _mesa_has_compute_shaders(const struct gl_context *ctx)
429 {
430 return _mesa_has_ARB_compute_shader(ctx) ||
431 _mesa_is_gles31(ctx);
432 }
433
434 /**
435 * Checks if the context supports tessellation.
436 */
437 static inline bool
_mesa_has_tessellation(const struct gl_context * ctx)438 _mesa_has_tessellation(const struct gl_context *ctx)
439 {
440 /* _mesa_has_EXT_tessellation_shader(ctx) is redundant with the OES
441 * check, so don't bother calling it.
442 */
443 return _mesa_has_OES_tessellation_shader(ctx) ||
444 _mesa_has_ARB_tessellation_shader(ctx);
445 }
446
447 static inline bool
_mesa_has_texture_cube_map_array(const struct gl_context * ctx)448 _mesa_has_texture_cube_map_array(const struct gl_context *ctx)
449 {
450 return _mesa_has_ARB_texture_cube_map_array(ctx) ||
451 _mesa_has_OES_texture_cube_map_array(ctx);
452 }
453
454 static inline bool
_mesa_has_texture_view(const struct gl_context * ctx)455 _mesa_has_texture_view(const struct gl_context *ctx)
456 {
457 return _mesa_has_ARB_texture_view(ctx) ||
458 _mesa_has_OES_texture_view(ctx);
459 }
460
461 static inline bool
_mesa_hw_select_enabled(const struct gl_context * ctx)462 _mesa_hw_select_enabled(const struct gl_context *ctx)
463 {
464 return ctx->RenderMode == GL_SELECT &&
465 ctx->Const.HardwareAcceleratedSelect;
466 }
467
468 static inline bool
_mesa_has_occlusion_query(const struct gl_context * ctx)469 _mesa_has_occlusion_query(const struct gl_context *ctx)
470 {
471 return _mesa_has_ARB_occlusion_query(ctx) ||
472 _mesa_has_ARB_occlusion_query2(ctx) ||
473 (_mesa_is_desktop_gl(ctx) && ctx->Version >= 15);
474 }
475
476 static inline bool
_mesa_has_occlusion_query_boolean(const struct gl_context * ctx)477 _mesa_has_occlusion_query_boolean(const struct gl_context *ctx)
478 {
479 return _mesa_has_ARB_occlusion_query2(ctx) ||
480 _mesa_has_EXT_occlusion_query_boolean(ctx) ||
481 (_mesa_is_desktop_gl(ctx) && ctx->Version >= 33);
482 }
483
484 static inline bool
_mesa_has_pipeline_statistics(const struct gl_context * ctx)485 _mesa_has_pipeline_statistics(const struct gl_context *ctx)
486 {
487 return _mesa_has_ARB_pipeline_statistics_query(ctx) ||
488 (_mesa_is_desktop_gl(ctx) && ctx->Version >= 46);
489 }
490
491 #ifdef __cplusplus
492 }
493 #endif
494
495
496 #endif /* CONTEXT_H */
497