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