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_initialize_exec_table(struct gl_context *ctx);
86 
87 extern void
88 _mesa_initialize_dispatch_tables(struct gl_context *ctx);
89 
90 extern struct _glapi_table *
91 _mesa_new_nop_table(unsigned numEntries, bool glthread);
92 
93 extern void
94 _mesa_free_context_data(struct gl_context *ctx, bool destroy_debug_output);
95 
96 extern void
97 _mesa_copy_context(const struct gl_context *src, struct gl_context *dst, GLuint mask);
98 
99 extern GLboolean
100 _mesa_make_current( struct gl_context *ctx, struct gl_framebuffer *drawBuffer,
101                     struct gl_framebuffer *readBuffer );
102 
103 extern GLboolean
104 _mesa_share_state(struct gl_context *ctx, struct gl_context *ctxToShare);
105 
106 extern struct gl_context *
107 _mesa_get_current_context(void);
108 
109 /*@}*/
110 
111 extern void
112 _mesa_init_constants(struct gl_constants *consts, gl_api api);
113 
114 extern struct _glapi_table *
115 _mesa_get_dispatch(struct gl_context *ctx);
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 or Core)
254  */
255 static inline bool
_mesa_is_desktop_gl(const struct gl_context * ctx)256 _mesa_is_desktop_gl(const struct gl_context *ctx)
257 {
258    return ctx->API == API_OPENGL_COMPAT || ctx->API == API_OPENGL_CORE;
259 }
260 
261 
262 /**
263  * Checks if the context is for any GLES version
264  */
265 static inline bool
_mesa_is_gles(const struct gl_context * ctx)266 _mesa_is_gles(const struct gl_context *ctx)
267 {
268    return ctx->API == API_OPENGLES || ctx->API == API_OPENGLES2;
269 }
270 
271 
272 /**
273  * Checks if the context is for GLES 3.0 or later
274  */
275 static inline bool
_mesa_is_gles3(const struct gl_context * ctx)276 _mesa_is_gles3(const struct gl_context *ctx)
277 {
278    return ctx->API == API_OPENGLES2 && ctx->Version >= 30;
279 }
280 
281 
282 /**
283  * Checks if the context is for GLES 3.1 or later
284  */
285 static inline bool
_mesa_is_gles31(const struct gl_context * ctx)286 _mesa_is_gles31(const struct gl_context *ctx)
287 {
288    return ctx->API == API_OPENGLES2 && ctx->Version >= 31;
289 }
290 
291 
292 /**
293  * Checks if the context is for GLES 3.2 or later
294  */
295 static inline bool
_mesa_is_gles32(const struct gl_context * ctx)296 _mesa_is_gles32(const struct gl_context *ctx)
297 {
298    return ctx->API == API_OPENGLES2 && ctx->Version >= 32;
299 }
300 
301 
302 static inline bool
_mesa_is_no_error_enabled(const struct gl_context * ctx)303 _mesa_is_no_error_enabled(const struct gl_context *ctx)
304 {
305    return ctx->Const.ContextFlags & GL_CONTEXT_FLAG_NO_ERROR_BIT_KHR;
306 }
307 
308 
309 static inline bool
_mesa_has_integer_textures(const struct gl_context * ctx)310 _mesa_has_integer_textures(const struct gl_context *ctx)
311 {
312    return _mesa_has_EXT_texture_integer(ctx) || _mesa_is_gles3(ctx);
313 }
314 
315 static inline bool
_mesa_has_half_float_textures(const struct gl_context * ctx)316 _mesa_has_half_float_textures(const struct gl_context *ctx)
317 {
318    return _mesa_has_ARB_texture_float(ctx) ||
319           _mesa_has_OES_texture_half_float(ctx);
320 }
321 
322 static inline bool
_mesa_has_float_textures(const struct gl_context * ctx)323 _mesa_has_float_textures(const struct gl_context *ctx)
324 {
325    return _mesa_has_ARB_texture_float(ctx) ||
326           _mesa_has_OES_texture_float(ctx) || _mesa_is_gles3(ctx);
327  }
328 
329 static inline bool
_mesa_has_texture_rgb10_a2ui(const struct gl_context * ctx)330 _mesa_has_texture_rgb10_a2ui(const struct gl_context *ctx)
331 {
332    return _mesa_has_ARB_texture_rgb10_a2ui(ctx) || _mesa_is_gles3(ctx);
333 }
334 
335 static inline bool
_mesa_has_float_depth_buffer(const struct gl_context * ctx)336 _mesa_has_float_depth_buffer(const struct gl_context *ctx)
337 {
338    return _mesa_has_ARB_depth_buffer_float(ctx) || _mesa_is_gles3(ctx);
339 }
340 
341 static inline bool
_mesa_has_packed_float(const struct gl_context * ctx)342 _mesa_has_packed_float(const struct gl_context *ctx)
343 {
344    return _mesa_has_EXT_packed_float(ctx) || _mesa_is_gles3(ctx);
345 }
346 
347 static inline bool
_mesa_has_rg_textures(const struct gl_context * ctx)348 _mesa_has_rg_textures(const struct gl_context *ctx)
349 {
350    return _mesa_has_ARB_texture_rg(ctx) || _mesa_has_EXT_texture_rg(ctx) ||
351           _mesa_is_gles3(ctx);
352 }
353 
354 static inline bool
_mesa_has_texture_shared_exponent(const struct gl_context * ctx)355 _mesa_has_texture_shared_exponent(const struct gl_context *ctx)
356 {
357    return _mesa_has_EXT_texture_shared_exponent(ctx) || _mesa_is_gles3(ctx);
358 }
359 
360 static inline bool
_mesa_has_texture_type_2_10_10_10_REV(const struct gl_context * ctx)361 _mesa_has_texture_type_2_10_10_10_REV(const struct gl_context *ctx)
362 {
363    return _mesa_is_desktop_gl(ctx) ||
364           _mesa_has_EXT_texture_type_2_10_10_10_REV(ctx);
365 }
366 
367 /**
368  * Checks if the context supports geometry shaders.
369  */
370 static inline bool
_mesa_has_geometry_shaders(const struct gl_context * ctx)371 _mesa_has_geometry_shaders(const struct gl_context *ctx)
372 {
373    return _mesa_has_OES_geometry_shader(ctx) ||
374           (_mesa_is_desktop_gl(ctx) && ctx->Version >= 32);
375 }
376 
377 
378 /**
379  * Checks if the context supports compute shaders.
380  */
381 static inline bool
_mesa_has_compute_shaders(const struct gl_context * ctx)382 _mesa_has_compute_shaders(const struct gl_context *ctx)
383 {
384    return _mesa_has_ARB_compute_shader(ctx) ||
385       (ctx->API == API_OPENGLES2 && ctx->Version >= 31);
386 }
387 
388 /**
389  * Checks if the context supports tessellation.
390  */
391 static inline bool
_mesa_has_tessellation(const struct gl_context * ctx)392 _mesa_has_tessellation(const struct gl_context *ctx)
393 {
394    /* _mesa_has_EXT_tessellation_shader(ctx) is redundant with the OES
395     * check, so don't bother calling it.
396     */
397    return _mesa_has_OES_tessellation_shader(ctx) ||
398           _mesa_has_ARB_tessellation_shader(ctx);
399 }
400 
401 static inline bool
_mesa_has_texture_cube_map_array(const struct gl_context * ctx)402 _mesa_has_texture_cube_map_array(const struct gl_context *ctx)
403 {
404    return _mesa_has_ARB_texture_cube_map_array(ctx) ||
405           _mesa_has_OES_texture_cube_map_array(ctx);
406 }
407 
408 static inline bool
_mesa_has_texture_view(const struct gl_context * ctx)409 _mesa_has_texture_view(const struct gl_context *ctx)
410 {
411    return _mesa_has_ARB_texture_view(ctx) ||
412           _mesa_has_OES_texture_view(ctx);
413 }
414 
415 #ifdef __cplusplus
416 }
417 #endif
418 
419 
420 #endif /* CONTEXT_H */
421