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