14e64c555f327ef480899585a55e3b69fba33ce8
[reactos.git] / reactos / dll / opengl / mesa / main / context.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.3
4 *
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 * Copyright (C) 2008 VMware, Inc. All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 /**
27 * \file context.c
28 * Mesa context/visual/framebuffer management functions.
29 * \author Brian Paul
30 */
31
32 /**
33 * \mainpage Mesa Main Module
34 *
35 * \section MainIntroduction Introduction
36 *
37 * The Mesa Main module consists of all the files in the main/ directory.
38 * Among the features of this module are:
39 * <UL>
40 * <LI> Structures to represent most GL state </LI>
41 * <LI> State set/get functions </LI>
42 * <LI> Display lists </LI>
43 * <LI> Texture unit, object and image handling </LI>
44 * <LI> Matrix and attribute stacks </LI>
45 * </UL>
46 *
47 * Other modules are responsible for API dispatch, vertex transformation,
48 * point/line/triangle setup, rasterization, vertex array caching,
49 * vertex/fragment programs/shaders, etc.
50 *
51 *
52 * \section AboutDoxygen About Doxygen
53 *
54 * If you're viewing this information as Doxygen-generated HTML you'll
55 * see the documentation index at the top of this page.
56 *
57 * The first line lists the Mesa source code modules.
58 * The second line lists the indexes available for viewing the documentation
59 * for each module.
60 *
61 * Selecting the <b>Main page</b> link will display a summary of the module
62 * (this page).
63 *
64 * Selecting <b>Data Structures</b> will list all C structures.
65 *
66 * Selecting the <b>File List</b> link will list all the source files in
67 * the module.
68 * Selecting a filename will show a list of all functions defined in that file.
69 *
70 * Selecting the <b>Data Fields</b> link will display a list of all
71 * documented structure members.
72 *
73 * Selecting the <b>Globals</b> link will display a list
74 * of all functions, structures, global variables and macros in the module.
75 *
76 */
77
78 #include <precomp.h>
79
80 #ifdef USE_SPARC_ASM
81 #include "sparc/sparc.h"
82 #endif
83
84
85 #ifndef MESA_VERBOSE
86 int MESA_VERBOSE = 0;
87 #endif
88
89 #ifndef MESA_DEBUG_FLAGS
90 int MESA_DEBUG_FLAGS = 0;
91 #endif
92
93
94 /* ubyte -> float conversion */
95 GLfloat _mesa_ubyte_to_float_color_tab[256];
96
97
98
99 /**
100 * Swap buffers notification callback.
101 *
102 * \param ctx GL context.
103 *
104 * Called by window system just before swapping buffers.
105 * We have to finish any pending rendering.
106 */
107 void
108 _mesa_notifySwapBuffers(struct gl_context *ctx)
109 {
110 if (MESA_VERBOSE & VERBOSE_SWAPBUFFERS)
111 _mesa_debug(ctx, "SwapBuffers\n");
112 FLUSH_CURRENT( ctx, 0 );
113 if (ctx->Driver.Flush) {
114 ctx->Driver.Flush(ctx);
115 }
116 }
117
118
119 /**********************************************************************/
120 /** \name GL Visual allocation/destruction */
121 /**********************************************************************/
122 /*@{*/
123
124 /**
125 * Allocates a struct gl_config structure and initializes it via
126 * _mesa_initialize_visual().
127 *
128 * \param dbFlag double buffering
129 * \param stereoFlag stereo buffer
130 * \param depthBits requested bits per depth buffer value. Any value in [0, 32]
131 * is acceptable but the actual depth type will be GLushort or GLuint as
132 * needed.
133 * \param stencilBits requested minimum bits per stencil buffer value
134 * \param accumRedBits, accumGreenBits, accumBlueBits, accumAlphaBits number
135 * of bits per color component in accum buffer.
136 * \param indexBits number of bits per pixel if \p rgbFlag is GL_FALSE
137 * \param redBits number of bits per color component in frame buffer for RGB(A)
138 * mode. We always use 8 in core Mesa though.
139 * \param greenBits same as above.
140 * \param blueBits same as above.
141 * \param alphaBits same as above.
142 *
143 * \return pointer to new struct gl_config or NULL if requested parameters
144 * can't be met.
145 *
146 * \note Need to add params for level and numAuxBuffers (at least)
147 */
148 struct gl_config *
149 _mesa_create_visual( GLboolean dbFlag,
150 GLboolean stereoFlag,
151 GLint redBits,
152 GLint greenBits,
153 GLint blueBits,
154 GLint alphaBits,
155 GLint depthBits,
156 GLint stencilBits,
157 GLint accumRedBits,
158 GLint accumGreenBits,
159 GLint accumBlueBits,
160 GLint accumAlphaBits)
161 {
162 struct gl_config *vis = CALLOC_STRUCT(gl_config);
163 if (vis) {
164 if (!_mesa_initialize_visual(vis, dbFlag, stereoFlag,
165 redBits, greenBits, blueBits, alphaBits,
166 depthBits, stencilBits,
167 accumRedBits, accumGreenBits,
168 accumBlueBits, accumAlphaBits)) {
169 free(vis);
170 return NULL;
171 }
172 }
173 return vis;
174 }
175
176
177 /**
178 * Makes some sanity checks and fills in the fields of the struct
179 * gl_config object with the given parameters. If the caller needs to
180 * set additional fields, he should just probably init the whole
181 * gl_config object himself.
182 *
183 * \return GL_TRUE on success, or GL_FALSE on failure.
184 *
185 * \sa _mesa_create_visual() above for the parameter description.
186 */
187 GLboolean
188 _mesa_initialize_visual( struct gl_config *vis,
189 GLboolean dbFlag,
190 GLboolean stereoFlag,
191 GLint redBits,
192 GLint greenBits,
193 GLint blueBits,
194 GLint alphaBits,
195 GLint depthBits,
196 GLint stencilBits,
197 GLint accumRedBits,
198 GLint accumGreenBits,
199 GLint accumBlueBits,
200 GLint accumAlphaBits)
201 {
202 assert(vis);
203
204 if (depthBits < 0 || depthBits > 32) {
205 return GL_FALSE;
206 }
207 if (stencilBits < 0 || stencilBits > STENCIL_BITS) {
208 return GL_FALSE;
209 }
210 assert(accumRedBits >= 0);
211 assert(accumGreenBits >= 0);
212 assert(accumBlueBits >= 0);
213 assert(accumAlphaBits >= 0);
214
215 vis->rgbMode = GL_TRUE;
216 vis->doubleBufferMode = dbFlag;
217 vis->stereoMode = stereoFlag;
218
219 vis->redBits = redBits;
220 vis->greenBits = greenBits;
221 vis->blueBits = blueBits;
222 vis->alphaBits = alphaBits;
223 vis->rgbBits = redBits + greenBits + blueBits;
224
225 vis->indexBits = 0;
226 vis->depthBits = depthBits;
227 vis->stencilBits = stencilBits;
228
229 vis->accumRedBits = accumRedBits;
230 vis->accumGreenBits = accumGreenBits;
231 vis->accumBlueBits = accumBlueBits;
232 vis->accumAlphaBits = accumAlphaBits;
233
234 vis->haveAccumBuffer = accumRedBits > 0;
235 vis->haveDepthBuffer = depthBits > 0;
236 vis->haveStencilBuffer = stencilBits > 0;
237
238 vis->numAuxBuffers = 0;
239 vis->level = 0;
240
241 return GL_TRUE;
242 }
243
244
245 /**
246 * Destroy a visual and free its memory.
247 *
248 * \param vis visual.
249 *
250 * Frees the visual structure.
251 */
252 void
253 _mesa_destroy_visual( struct gl_config *vis )
254 {
255 free(vis);
256 }
257
258 /*@}*/
259
260
261 /**********************************************************************/
262 /** \name Context allocation, initialization, destroying
263 *
264 * The purpose of the most initialization functions here is to provide the
265 * default state values according to the OpenGL specification.
266 */
267 /**********************************************************************/
268 /*@{*/
269
270
271 /**
272 * This is lame. gdb only seems to recognize enum types that are
273 * actually used somewhere. We want to be able to print/use enum
274 * values such as TEXTURE_2D_INDEX in gdb. But we don't actually use
275 * the gl_texture_index type anywhere. Thus, this lame function.
276 */
277 static void
278 dummy_enum_func(void)
279 {
280 gl_buffer_index bi = BUFFER_FRONT_LEFT;
281 gl_face_index fi = FACE_POS_X;
282 gl_frag_attrib fa = FRAG_ATTRIB_WPOS;
283 gl_vert_attrib va = VERT_ATTRIB_POS;
284
285 (void) bi;
286 (void) fi;
287 (void) fa;
288 (void) va;
289 }
290
291
292 /**
293 * One-time initialization mutex lock.
294 *
295 * \sa Used by one_time_init().
296 */
297 _glthread_DECLARE_STATIC_MUTEX(OneTimeLock);
298
299
300
301 /**
302 * Calls all the various one-time-init functions in Mesa.
303 *
304 * While holding a global mutex lock, calls several initialization functions,
305 * and sets the glapi callbacks if the \c MESA_DEBUG environment variable is
306 * defined.
307 *
308 * \sa _math_init().
309 */
310 static void
311 one_time_init( struct gl_context *ctx )
312 {
313 static GLboolean api_init = GL_FALSE;
314
315 _glthread_LOCK_MUTEX(OneTimeLock);
316
317 /* truly one-time init */
318 if (!api_init) {
319 GLuint i;
320
321 /* do some implementation tests */
322 assert( sizeof(GLbyte) == 1 );
323 assert( sizeof(GLubyte) == 1 );
324 assert( sizeof(GLshort) == 2 );
325 assert( sizeof(GLushort) == 2 );
326 assert( sizeof(GLint) == 4 );
327 assert( sizeof(GLuint) == 4 );
328
329 _mesa_get_cpu_features();
330
331 _mesa_init_sqrt_table();
332
333 /* context dependence is never a one-time thing... */
334 _mesa_init_get_hash(ctx);
335
336 for (i = 0; i < 256; i++) {
337 _mesa_ubyte_to_float_color_tab[i] = (float) i / 255.0F;
338 }
339
340 #if defined(DEBUG) && defined(__DATE__) && defined(__TIME__)
341 if (MESA_VERBOSE != 0) {
342 _mesa_debug(ctx, "Mesa %s DEBUG build %s %s\n",
343 MESA_VERSION_STRING, __DATE__, __TIME__);
344 }
345 #endif
346
347 #ifdef DEBUG
348 _mesa_test_formats();
349 #endif
350 }
351
352 api_init = GL_TRUE;
353
354 _glthread_UNLOCK_MUTEX(OneTimeLock);
355
356 dummy_enum_func();
357 }
358
359
360 /**
361 * Initialize fields of gl_current_attrib (aka ctx->Current.*)
362 */
363 static void
364 _mesa_init_current(struct gl_context *ctx)
365 {
366 GLuint i;
367
368 /* Init all to (0,0,0,1) */
369 for (i = 0; i < Elements(ctx->Current.Attrib); i++) {
370 ASSIGN_4V( ctx->Current.Attrib[i], 0.0, 0.0, 0.0, 1.0 );
371 }
372
373 /* redo special cases: */
374 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_WEIGHT], 1.0, 0.0, 0.0, 0.0 );
375 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_NORMAL], 0.0, 0.0, 1.0, 1.0 );
376 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR0], 1.0, 1.0, 1.0, 1.0 );
377 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR1], 0.0, 0.0, 0.0, 1.0 );
378 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR_INDEX], 1.0, 0.0, 0.0, 1.0 );
379 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_EDGEFLAG], 1.0, 0.0, 0.0, 1.0 );
380 }
381
382 /**
383 * Initialize fields of gl_constants (aka ctx->Const.*).
384 * Use defaults from config.h. The device drivers will often override
385 * some of these values (such as number of texture units).
386 */
387 static void
388 _mesa_init_constants(struct gl_context *ctx)
389 {
390 assert(ctx);
391
392 /* Constants, may be overriden (usually only reduced) by device drivers */
393 ctx->Const.MaxTextureMbytes = MAX_TEXTURE_MBYTES;
394 ctx->Const.MaxTextureLevels = MAX_TEXTURE_LEVELS;
395 ctx->Const.MaxCubeTextureLevels = MAX_CUBE_TEXTURE_LEVELS;
396 ctx->Const.MaxTextureMaxAnisotropy = MAX_TEXTURE_MAX_ANISOTROPY;
397 ctx->Const.MaxArrayLockSize = MAX_ARRAY_LOCK_SIZE;
398 ctx->Const.SubPixelBits = SUB_PIXEL_BITS;
399 ctx->Const.MinPointSize = MIN_POINT_SIZE;
400 ctx->Const.MaxPointSize = MAX_POINT_SIZE;
401 ctx->Const.MinPointSizeAA = MIN_POINT_SIZE;
402 ctx->Const.MaxPointSizeAA = MAX_POINT_SIZE;
403 ctx->Const.PointSizeGranularity = (GLfloat) POINT_SIZE_GRANULARITY;
404 ctx->Const.MinLineWidth = MIN_LINE_WIDTH;
405 ctx->Const.MaxLineWidth = MAX_LINE_WIDTH;
406 ctx->Const.MinLineWidthAA = MIN_LINE_WIDTH;
407 ctx->Const.MaxLineWidthAA = MAX_LINE_WIDTH;
408 ctx->Const.LineWidthGranularity = (GLfloat) LINE_WIDTH_GRANULARITY;
409 ctx->Const.MaxColorTableSize = MAX_COLOR_TABLE_SIZE;
410 ctx->Const.MaxClipPlanes = 6;
411 ctx->Const.MaxLights = MAX_LIGHTS;
412 ctx->Const.MaxShininess = 128.0;
413 ctx->Const.MaxSpotExponent = 128.0;
414 ctx->Const.MaxViewportWidth = MAX_WIDTH;
415 ctx->Const.MaxViewportHeight = MAX_HEIGHT;
416
417 /* CheckArrayBounds is overriden by drivers/x11 for X server */
418 ctx->Const.CheckArrayBounds = GL_FALSE;
419
420 /* GL 3.2: hard-coded for now: */
421 ctx->Const.ProfileMask = GL_CONTEXT_COMPATIBILITY_PROFILE_BIT;
422 }
423
424
425 /**
426 * Do some sanity checks on the limits/constants for the given context.
427 * Only called the first time a context is bound.
428 */
429 static void
430 check_context_limits(struct gl_context *ctx)
431 {
432 /* Texture size checks */
433 assert(ctx->Const.MaxTextureLevels <= MAX_TEXTURE_LEVELS);
434 assert(ctx->Const.Max3DTextureLevels <= MAX_3D_TEXTURE_LEVELS);
435 assert(ctx->Const.MaxCubeTextureLevels <= MAX_CUBE_TEXTURE_LEVELS);
436
437 /* make sure largest texture image is <= MAX_WIDTH in size */
438 assert((1 << (ctx->Const.MaxTextureLevels - 1)) <= MAX_WIDTH);
439 assert((1 << (ctx->Const.MaxCubeTextureLevels - 1)) <= MAX_WIDTH);
440 assert((1 << (ctx->Const.Max3DTextureLevels - 1)) <= MAX_WIDTH);
441
442 /* Texture level checks */
443 assert(MAX_TEXTURE_LEVELS >= MAX_3D_TEXTURE_LEVELS);
444 assert(MAX_TEXTURE_LEVELS >= MAX_CUBE_TEXTURE_LEVELS);
445
446 /* Max texture size should be <= max viewport size (render to texture) */
447 assert((1 << (MAX_TEXTURE_LEVELS - 1)) <= MAX_WIDTH);
448
449 assert(ctx->Const.MaxViewportWidth <= MAX_WIDTH);
450 assert(ctx->Const.MaxViewportHeight <= MAX_WIDTH);
451 }
452
453
454 /**
455 * Initialize the attribute groups in a GL context.
456 *
457 * \param ctx GL context.
458 *
459 * Initializes all the attributes, calling the respective <tt>init*</tt>
460 * functions for the more complex data structures.
461 */
462 static GLboolean
463 init_attrib_groups(struct gl_context *ctx)
464 {
465 assert(ctx);
466
467 /* Constants */
468 _mesa_init_constants( ctx );
469
470 /* Extensions */
471 _mesa_init_extensions( ctx );
472
473 /* Attribute Groups */
474 _mesa_init_accum( ctx );
475 _mesa_init_attrib( ctx );
476 _mesa_init_buffer_objects( ctx );
477 _mesa_init_color( ctx );
478 _mesa_init_current( ctx );
479 _mesa_init_depth( ctx );
480 _mesa_init_display_list( ctx );
481 _mesa_init_eval( ctx );
482 _mesa_init_feedback( ctx );
483 _mesa_init_fog( ctx );
484 _mesa_init_hint( ctx );
485 _mesa_init_line( ctx );
486 _mesa_init_lighting( ctx );
487 _mesa_init_matrix( ctx );
488 _mesa_init_multisample( ctx );
489 _mesa_init_pixel( ctx );
490 _mesa_init_pixelstore( ctx );
491 _mesa_init_point( ctx );
492 _mesa_init_polygon( ctx );
493 _mesa_init_rastpos( ctx );
494 _mesa_init_scissor( ctx );
495 _mesa_init_stencil( ctx );
496 _mesa_init_transform( ctx );
497 _mesa_init_varray( ctx );
498 _mesa_init_viewport( ctx );
499
500 if (!_mesa_init_texture( ctx ))
501 return GL_FALSE;
502
503 /* Miscellaneous */
504 ctx->NewState = _NEW_ALL;
505 ctx->ErrorValue = (GLenum) GL_NO_ERROR;
506
507 return GL_TRUE;
508 }
509
510
511 /**
512 * Update default objects in a GL context with respect to shared state.
513 *
514 * \param ctx GL context.
515 *
516 * Removes references to old default objects, (texture objects, program
517 * objects, etc.) and changes to reference those from the current shared
518 * state.
519 */
520 static GLboolean
521 update_default_objects(struct gl_context *ctx)
522 {
523 assert(ctx);
524
525 _mesa_update_default_objects_texture(ctx);
526 _mesa_update_default_objects_buffer_objects(ctx);
527
528 return GL_TRUE;
529 }
530
531
532 /**
533 * This is the default function we plug into all dispatch table slots
534 * This helps prevents a segfault when someone calls a GL function without
535 * first checking if the extension's supported.
536 */
537 static int
538 generic_nop(void)
539 {
540 _mesa_warning(NULL, "User called no-op dispatch function (an unsupported extension function?)");
541 return 0;
542 }
543
544
545 /**
546 * Allocate and initialize a new dispatch table.
547 */
548 struct _glapi_table *
549 _mesa_alloc_dispatch_table(int size)
550 {
551 /* Find the larger of Mesa's dispatch table and libGL's dispatch table.
552 * In practice, this'll be the same for stand-alone Mesa. But for DRI
553 * Mesa we do this to accomodate different versions of libGL and various
554 * DRI drivers.
555 */
556 GLint numEntries = MAX2(_glapi_get_dispatch_table_size(), _gloffset_COUNT);
557 struct _glapi_table *table;
558
559 /* should never happen, but just in case */
560 numEntries = MAX2(numEntries, size);
561
562 table = (struct _glapi_table *) malloc(numEntries * sizeof(_glapi_proc));
563 if (table) {
564 _glapi_proc *entry = (_glapi_proc *) table;
565 GLint i;
566 for (i = 0; i < numEntries; i++) {
567 entry[i] = (_glapi_proc) generic_nop;
568 }
569 }
570 return table;
571 }
572
573
574 /**
575 * Initialize a struct gl_context struct (rendering context).
576 *
577 * This includes allocating all the other structs and arrays which hang off of
578 * the context by pointers.
579 * Note that the driver needs to pass in its dd_function_table here since
580 * we need to at least call driverFunctions->NewTextureObject to create the
581 * default texture objects.
582 *
583 * Called by _mesa_create_context().
584 *
585 * Performs the imports and exports callback tables initialization, and
586 * miscellaneous one-time initializations. If no shared context is supplied one
587 * is allocated, and increase its reference count. Setups the GL API dispatch
588 * tables. Initialize the TNL module. Sets the maximum Z buffer depth.
589 * Finally queries the \c MESA_DEBUG and \c MESA_VERBOSE environment variables
590 * for debug flags.
591 *
592 * \param ctx the context to initialize
593 * \param api the GL API type to create the context for
594 * \param visual describes the visual attributes for this context
595 * \param share_list points to context to share textures, display lists,
596 * etc with, or NULL
597 * \param driverFunctions table of device driver functions for this context
598 * to use
599 * \param driverContext pointer to driver-specific context data
600 */
601 GLboolean
602 _mesa_initialize_context(struct gl_context *ctx,
603 const struct gl_config *visual,
604 struct gl_context *share_list,
605 const struct dd_function_table *driverFunctions,
606 void *driverContext)
607 {
608 struct gl_shared_state *shared;
609
610 /*ASSERT(driverContext);*/
611 assert(driverFunctions->NewTextureObject);
612 assert(driverFunctions->FreeTextureImageBuffer);
613
614 ctx->Visual = *visual;
615 ctx->DrawBuffer = NULL;
616 ctx->ReadBuffer = NULL;
617 ctx->WinSysDrawBuffer = NULL;
618 ctx->WinSysReadBuffer = NULL;
619
620 /* misc one-time initializations */
621 one_time_init(ctx);
622
623 /* Plug in driver functions and context pointer here.
624 * This is important because when we call alloc_shared_state() below
625 * we'll call ctx->Driver.NewTextureObject() to create the default
626 * textures.
627 */
628 ctx->Driver = *driverFunctions;
629 ctx->DriverCtx = driverContext;
630
631 if (share_list) {
632 /* share state with another context */
633 shared = share_list->Shared;
634 }
635 else {
636 /* allocate new, unshared state */
637 shared = _mesa_alloc_shared_state(ctx);
638 if (!shared)
639 return GL_FALSE;
640 }
641
642 _mesa_reference_shared_state(ctx, &ctx->Shared, shared);
643
644 if (!init_attrib_groups( ctx )) {
645 _mesa_reference_shared_state(ctx, &ctx->Shared, NULL);
646 return GL_FALSE;
647 }
648
649 ctx->Exec = _mesa_create_exec_table();
650
651 if (!ctx->Exec) {
652 _mesa_reference_shared_state(ctx, &ctx->Shared, NULL);
653 return GL_FALSE;
654 }
655 ctx->CurrentDispatch = ctx->Exec;
656
657 /* Mesa core handles all the formats that mesa core knows about.
658 * Drivers will want to override this list with just the formats
659 * they can handle, and confirm that appropriate fallbacks exist in
660 * _mesa_choose_tex_format().
661 */
662 memset(&ctx->TextureFormatSupported, GL_TRUE,
663 sizeof(ctx->TextureFormatSupported));
664
665 ctx->Save = _mesa_create_save_table();
666 if (!ctx->Save) {
667 _mesa_reference_shared_state(ctx, &ctx->Shared, NULL);
668 free(ctx->Exec);
669 return GL_FALSE;
670 }
671
672 _mesa_install_save_vtxfmt( ctx, &ctx->ListState.ListVtxfmt );
673
674 ctx->FirstTimeCurrent = GL_TRUE;
675
676 return GL_TRUE;
677 }
678
679
680 /**
681 * Allocate and initialize a struct gl_context structure.
682 * Note that the driver needs to pass in its dd_function_table here since
683 * we need to at least call driverFunctions->NewTextureObject to initialize
684 * the rendering context.
685 *
686 * \param api the GL API type to create the context for
687 * \param visual a struct gl_config pointer (we copy the struct contents)
688 * \param share_list another context to share display lists with or NULL
689 * \param driverFunctions points to the dd_function_table into which the
690 * driver has plugged in all its special functions.
691 * \param driverContext points to the device driver's private context state
692 *
693 * \return pointer to a new __struct gl_contextRec or NULL if error.
694 */
695 struct gl_context *
696 _mesa_create_context(const struct gl_config *visual,
697 struct gl_context *share_list,
698 const struct dd_function_table *driverFunctions,
699 void *driverContext)
700 {
701 struct gl_context *ctx;
702
703 ASSERT(visual);
704 /*ASSERT(driverContext);*/
705
706 ctx = (struct gl_context *) calloc(1, sizeof(struct gl_context));
707 if (!ctx)
708 return NULL;
709
710 if (_mesa_initialize_context(ctx, visual, share_list,
711 driverFunctions, driverContext)) {
712 return ctx;
713 }
714 else {
715 free(ctx);
716 return NULL;
717 }
718 }
719
720
721 /**
722 * Free the data associated with the given context.
723 *
724 * But doesn't free the struct gl_context struct itself.
725 *
726 * \sa _mesa_initialize_context() and init_attrib_groups().
727 */
728 void
729 _mesa_free_context_data( struct gl_context *ctx )
730 {
731 if (!_mesa_get_current_context()){
732 /* No current context, but we may need one in order to delete
733 * texture objs, etc. So temporarily bind the context now.
734 */
735 _mesa_make_current(ctx, NULL, NULL);
736 }
737
738 /* unreference WinSysDraw/Read buffers */
739 _mesa_reference_framebuffer(&ctx->WinSysDrawBuffer, NULL);
740 _mesa_reference_framebuffer(&ctx->WinSysReadBuffer, NULL);
741 _mesa_reference_framebuffer(&ctx->DrawBuffer, NULL);
742 _mesa_reference_framebuffer(&ctx->ReadBuffer, NULL);
743
744 _mesa_free_attrib_data(ctx);
745 _mesa_free_buffer_objects(ctx);
746 _mesa_free_lighting_data( ctx );
747 _mesa_free_eval_data( ctx );
748 _mesa_free_texture_data( ctx );
749 _mesa_free_matrix_data( ctx );
750 _mesa_free_viewport_data( ctx );
751 _mesa_free_varray_data(ctx);
752
753 _mesa_delete_array_object(ctx, ctx->Array.DefaultArrayObj);
754
755 _mesa_reference_buffer_object(ctx, &ctx->Array.ArrayBufferObj, NULL);
756
757 /* free dispatch tables */
758 free(ctx->Exec);
759 free(ctx->Save);
760
761 /* Shared context state (display lists, textures, etc) */
762 _mesa_reference_shared_state(ctx, &ctx->Shared, NULL);
763
764 /* needs to be after freeing shared state */
765 _mesa_free_display_list_data(ctx);
766
767 if (ctx->Extensions.String)
768 free((void *) ctx->Extensions.String);
769
770 if (ctx->VersionString)
771 free(ctx->VersionString);
772
773 /* unbind the context if it's currently bound */
774 if (ctx == _mesa_get_current_context()) {
775 _mesa_make_current(NULL, NULL, NULL);
776 }
777 }
778
779
780 /**
781 * Destroy a struct gl_context structure.
782 *
783 * \param ctx GL context.
784 *
785 * Calls _mesa_free_context_data() and frees the gl_context object itself.
786 */
787 void
788 _mesa_destroy_context( struct gl_context *ctx )
789 {
790 if (ctx) {
791 _mesa_free_context_data(ctx);
792 free( (void *) ctx );
793 }
794 }
795
796
797 #if _HAVE_FULL_GL
798 /**
799 * Copy attribute groups from one context to another.
800 *
801 * \param src source context
802 * \param dst destination context
803 * \param mask bitwise OR of GL_*_BIT flags
804 *
805 * According to the bits specified in \p mask, copies the corresponding
806 * attributes from \p src into \p dst. For many of the attributes a simple \c
807 * memcpy is not enough due to the existence of internal pointers in their data
808 * structures.
809 */
810 void
811 _mesa_copy_context( const struct gl_context *src, struct gl_context *dst,
812 GLuint mask )
813 {
814 if (mask & GL_ACCUM_BUFFER_BIT) {
815 /* OK to memcpy */
816 dst->Accum = src->Accum;
817 }
818 if (mask & GL_COLOR_BUFFER_BIT) {
819 /* OK to memcpy */
820 dst->Color = src->Color;
821 }
822 if (mask & GL_CURRENT_BIT) {
823 /* OK to memcpy */
824 dst->Current = src->Current;
825 }
826 if (mask & GL_DEPTH_BUFFER_BIT) {
827 /* OK to memcpy */
828 dst->Depth = src->Depth;
829 }
830 if (mask & GL_ENABLE_BIT) {
831 /* no op */
832 }
833 if (mask & GL_EVAL_BIT) {
834 /* OK to memcpy */
835 dst->Eval = src->Eval;
836 }
837 if (mask & GL_FOG_BIT) {
838 /* OK to memcpy */
839 dst->Fog = src->Fog;
840 }
841 if (mask & GL_HINT_BIT) {
842 /* OK to memcpy */
843 dst->Hint = src->Hint;
844 }
845 if (mask & GL_LIGHTING_BIT) {
846 GLuint i;
847 /* begin with memcpy */
848 dst->Light = src->Light;
849 /* fixup linked lists to prevent pointer insanity */
850 make_empty_list( &(dst->Light.EnabledList) );
851 for (i = 0; i < MAX_LIGHTS; i++) {
852 if (dst->Light.Light[i].Enabled) {
853 insert_at_tail(&(dst->Light.EnabledList), &(dst->Light.Light[i]));
854 }
855 }
856 }
857 if (mask & GL_LINE_BIT) {
858 /* OK to memcpy */
859 dst->Line = src->Line;
860 }
861 if (mask & GL_LIST_BIT) {
862 /* OK to memcpy */
863 dst->List = src->List;
864 }
865 if (mask & GL_PIXEL_MODE_BIT) {
866 /* OK to memcpy */
867 dst->Pixel = src->Pixel;
868 }
869 if (mask & GL_POINT_BIT) {
870 /* OK to memcpy */
871 dst->Point = src->Point;
872 }
873 if (mask & GL_POLYGON_BIT) {
874 /* OK to memcpy */
875 dst->Polygon = src->Polygon;
876 }
877 if (mask & GL_POLYGON_STIPPLE_BIT) {
878 /* Use loop instead of memcpy due to problem with Portland Group's
879 * C compiler. Reported by John Stone.
880 */
881 GLuint i;
882 for (i = 0; i < 32; i++) {
883 dst->PolygonStipple[i] = src->PolygonStipple[i];
884 }
885 }
886 if (mask & GL_SCISSOR_BIT) {
887 /* OK to memcpy */
888 dst->Scissor = src->Scissor;
889 }
890 if (mask & GL_STENCIL_BUFFER_BIT) {
891 /* OK to memcpy */
892 dst->Stencil = src->Stencil;
893 }
894 if (mask & GL_TEXTURE_BIT) {
895 /* Cannot memcpy because of pointers */
896 _mesa_copy_texture_state(src, dst);
897 }
898 if (mask & GL_TRANSFORM_BIT) {
899 /* OK to memcpy */
900 dst->Transform = src->Transform;
901 }
902 if (mask & GL_VIEWPORT_BIT) {
903 /* Cannot use memcpy, because of pointers in GLmatrix _WindowMap */
904 dst->Viewport.X = src->Viewport.X;
905 dst->Viewport.Y = src->Viewport.Y;
906 dst->Viewport.Width = src->Viewport.Width;
907 dst->Viewport.Height = src->Viewport.Height;
908 dst->Viewport.Near = src->Viewport.Near;
909 dst->Viewport.Far = src->Viewport.Far;
910 _math_matrix_copy(&dst->Viewport._WindowMap, &src->Viewport._WindowMap);
911 }
912
913 /* XXX FIXME: Call callbacks?
914 */
915 dst->NewState = _NEW_ALL;
916 }
917 #endif
918
919
920 /**
921 * Check if the given context can render into the given framebuffer
922 * by checking visual attributes.
923 *
924 * Most of these tests could go away because Mesa is now pretty flexible
925 * in terms of mixing rendering contexts with framebuffers. As long
926 * as RGB vs. CI mode agree, we're probably good.
927 *
928 * \return GL_TRUE if compatible, GL_FALSE otherwise.
929 */
930 static GLboolean
931 check_compatible(const struct gl_context *ctx,
932 const struct gl_framebuffer *buffer)
933 {
934 const struct gl_config *ctxvis = &ctx->Visual;
935 const struct gl_config *bufvis = &buffer->Visual;
936
937 #if 0
938 /* disabling this fixes the fgl_glxgears pbuffer demo */
939 if (ctxvis->doubleBufferMode && !bufvis->doubleBufferMode)
940 return GL_FALSE;
941 #endif
942 if (ctxvis->stereoMode && !bufvis->stereoMode)
943 return GL_FALSE;
944 if (ctxvis->haveAccumBuffer && !bufvis->haveAccumBuffer)
945 return GL_FALSE;
946 if (ctxvis->haveDepthBuffer && !bufvis->haveDepthBuffer)
947 return GL_FALSE;
948 if (ctxvis->haveStencilBuffer && !bufvis->haveStencilBuffer)
949 return GL_FALSE;
950 if (ctxvis->redMask && ctxvis->redMask != bufvis->redMask)
951 return GL_FALSE;
952 if (ctxvis->greenMask && ctxvis->greenMask != bufvis->greenMask)
953 return GL_FALSE;
954 if (ctxvis->blueMask && ctxvis->blueMask != bufvis->blueMask)
955 return GL_FALSE;
956 #if 0
957 /* disabled (see bug 11161) */
958 if (ctxvis->depthBits && ctxvis->depthBits != bufvis->depthBits)
959 return GL_FALSE;
960 #endif
961 if (ctxvis->stencilBits && ctxvis->stencilBits != bufvis->stencilBits)
962 return GL_FALSE;
963
964 return GL_TRUE;
965 }
966
967
968 /**
969 * Do one-time initialization for the given framebuffer. Specifically,
970 * ask the driver for the window's current size and update the framebuffer
971 * object to match.
972 * Really, the device driver should totally take care of this.
973 */
974 static void
975 initialize_framebuffer_size(struct gl_context *ctx, struct gl_framebuffer *fb)
976 {
977 GLuint width, height;
978 if (ctx->Driver.GetBufferSize) {
979 ctx->Driver.GetBufferSize(fb, &width, &height);
980 if (ctx->Driver.ResizeBuffers)
981 ctx->Driver.ResizeBuffers(ctx, fb, width, height);
982 fb->Initialized = GL_TRUE;
983 }
984 }
985
986
987 /**
988 * Check if the viewport/scissor size has not yet been initialized.
989 * Initialize the size if the given width and height are non-zero.
990 */
991 void
992 _mesa_check_init_viewport(struct gl_context *ctx, GLuint width, GLuint height)
993 {
994 if (!ctx->ViewportInitialized && width > 0 && height > 0) {
995 /* Note: set flag here, before calling _mesa_set_viewport(), to prevent
996 * potential infinite recursion.
997 */
998 ctx->ViewportInitialized = GL_TRUE;
999 _mesa_set_viewport(ctx, 0, 0, width, height);
1000 _mesa_set_scissor(ctx, 0, 0, width, height);
1001 }
1002 }
1003
1004
1005 /**
1006 * Bind the given context to the given drawBuffer and readBuffer and
1007 * make it the current context for the calling thread.
1008 * We'll render into the drawBuffer and read pixels from the
1009 * readBuffer (i.e. glRead/CopyPixels, glCopyTexImage, etc).
1010 *
1011 * We check that the context's and framebuffer's visuals are compatible
1012 * and return immediately if they're not.
1013 *
1014 * \param newCtx the new GL context. If NULL then there will be no current GL
1015 * context.
1016 * \param drawBuffer the drawing framebuffer
1017 * \param readBuffer the reading framebuffer
1018 */
1019 GLboolean
1020 _mesa_make_current( struct gl_context *newCtx,
1021 struct gl_framebuffer *drawBuffer,
1022 struct gl_framebuffer *readBuffer )
1023 {
1024 GET_CURRENT_CONTEXT(curCtx);
1025
1026 if (MESA_VERBOSE & VERBOSE_API)
1027 _mesa_debug(newCtx, "_mesa_make_current()\n");
1028
1029 /* Check that the context's and framebuffer's visuals are compatible.
1030 */
1031 if (newCtx && drawBuffer && newCtx->WinSysDrawBuffer != drawBuffer) {
1032 if (!check_compatible(newCtx, drawBuffer)) {
1033 _mesa_warning(newCtx,
1034 "MakeCurrent: incompatible visuals for context and drawbuffer");
1035 return GL_FALSE;
1036 }
1037 }
1038 if (newCtx && readBuffer && newCtx->WinSysReadBuffer != readBuffer) {
1039 if (!check_compatible(newCtx, readBuffer)) {
1040 _mesa_warning(newCtx,
1041 "MakeCurrent: incompatible visuals for context and readbuffer");
1042 return GL_FALSE;
1043 }
1044 }
1045
1046 if (curCtx &&
1047 (curCtx->WinSysDrawBuffer || curCtx->WinSysReadBuffer) &&
1048 /* make sure this context is valid for flushing */
1049 curCtx != newCtx)
1050 _mesa_flush(curCtx);
1051
1052 /* We used to call _glapi_check_multithread() here. Now do it in drivers */
1053 _glapi_set_context((void *) newCtx);
1054 ASSERT(_mesa_get_current_context() == newCtx);
1055
1056 if (!newCtx) {
1057 _glapi_set_dispatch(NULL); /* none current */
1058 }
1059 else {
1060 _glapi_set_dispatch(newCtx->CurrentDispatch);
1061
1062 if (drawBuffer && readBuffer) {
1063 ASSERT(drawBuffer->Name == 0);
1064 ASSERT(readBuffer->Name == 0);
1065 _mesa_reference_framebuffer(&newCtx->WinSysDrawBuffer, drawBuffer);
1066 _mesa_reference_framebuffer(&newCtx->WinSysReadBuffer, readBuffer);
1067
1068 /*
1069 * Only set the context's Draw/ReadBuffer fields if they're NULL
1070 * or not bound to a user-created FBO.
1071 */
1072 _mesa_reference_framebuffer(&newCtx->DrawBuffer, drawBuffer);
1073 /* Update the FBO's list of drawbuffers/renderbuffers.
1074 * For winsys FBOs this comes from the GL state (which may have
1075 * changed since the last time this FBO was bound).
1076 */
1077 _mesa_update_draw_buffer(newCtx);
1078
1079 _mesa_reference_framebuffer(&newCtx->ReadBuffer, readBuffer);
1080
1081 /* XXX only set this flag if we're really changing the draw/read
1082 * framebuffer bindings.
1083 */
1084 newCtx->NewState |= _NEW_BUFFERS;
1085
1086 #if 1
1087 /* We want to get rid of these lines: */
1088
1089 #if _HAVE_FULL_GL
1090 if (!drawBuffer->Initialized) {
1091 initialize_framebuffer_size(newCtx, drawBuffer);
1092 }
1093 if (readBuffer != drawBuffer && !readBuffer->Initialized) {
1094 initialize_framebuffer_size(newCtx, readBuffer);
1095 }
1096
1097 _mesa_resizebuffers(newCtx);
1098 #endif
1099
1100 #else
1101 /* We want the drawBuffer and readBuffer to be initialized by
1102 * the driver.
1103 * This generally means the Width and Height match the actual
1104 * window size and the renderbuffers (both hardware and software
1105 * based) are allocated to match. The later can generally be
1106 * done with a call to _mesa_resize_framebuffer().
1107 *
1108 * It's theoretically possible for a buffer to have zero width
1109 * or height, but for now, assert check that the driver did what's
1110 * expected of it.
1111 */
1112 ASSERT(drawBuffer->Width > 0);
1113 ASSERT(drawBuffer->Height > 0);
1114 #endif
1115
1116 if (drawBuffer) {
1117 _mesa_check_init_viewport(newCtx,
1118 drawBuffer->Width, drawBuffer->Height);
1119 }
1120 }
1121
1122 if (newCtx->FirstTimeCurrent) {
1123 _mesa_compute_version(newCtx);
1124
1125 newCtx->Extensions.String = _mesa_make_extension_string(newCtx);
1126
1127 check_context_limits(newCtx);
1128
1129 newCtx->FirstTimeCurrent = GL_FALSE;
1130 }
1131 }
1132
1133 return GL_TRUE;
1134 }
1135
1136
1137 /**
1138 * Make context 'ctx' share the display lists, textures and programs
1139 * that are associated with 'ctxToShare'.
1140 * Any display lists, textures or programs associated with 'ctx' will
1141 * be deleted if nobody else is sharing them.
1142 */
1143 GLboolean
1144 _mesa_share_state(struct gl_context *ctx, struct gl_context *ctxToShare)
1145 {
1146 if (ctx && ctxToShare && ctx->Shared && ctxToShare->Shared) {
1147 struct gl_shared_state *oldShared = NULL;
1148
1149 /* save ref to old state to prevent it from being deleted immediately */
1150 _mesa_reference_shared_state(ctx, &oldShared, ctx->Shared);
1151
1152 /* update ctx's Shared pointer */
1153 _mesa_reference_shared_state(ctx, &ctx->Shared, ctxToShare->Shared);
1154
1155 update_default_objects(ctx);
1156
1157 /* release the old shared state */
1158 _mesa_reference_shared_state(ctx, &oldShared, NULL);
1159
1160 return GL_TRUE;
1161 }
1162 else {
1163 return GL_FALSE;
1164 }
1165 }
1166
1167
1168
1169 /**
1170 * \return pointer to the current GL context for this thread.
1171 *
1172 * Calls _glapi_get_context(). This isn't the fastest way to get the current
1173 * context. If you need speed, see the #GET_CURRENT_CONTEXT macro in
1174 * context.h.
1175 */
1176 struct gl_context *
1177 _mesa_get_current_context( void )
1178 {
1179 return (struct gl_context *) _glapi_get_context();
1180 }
1181
1182
1183 /**
1184 * Get context's current API dispatch table.
1185 *
1186 * It'll either be the immediate-mode execute dispatcher or the display list
1187 * compile dispatcher.
1188 *
1189 * \param ctx GL context.
1190 *
1191 * \return pointer to dispatch_table.
1192 *
1193 * Simply returns __struct gl_contextRec::CurrentDispatch.
1194 */
1195 struct _glapi_table *
1196 _mesa_get_dispatch(struct gl_context *ctx)
1197 {
1198 return ctx->CurrentDispatch;
1199 }
1200
1201 /*@}*/
1202
1203
1204 /**********************************************************************/
1205 /** \name Miscellaneous functions */
1206 /**********************************************************************/
1207 /*@{*/
1208
1209 /**
1210 * Record an error.
1211 *
1212 * \param ctx GL context.
1213 * \param error error code.
1214 *
1215 * Records the given error code and call the driver's dd_function_table::Error
1216 * function if defined.
1217 *
1218 * \sa
1219 * This is called via _mesa_error().
1220 */
1221 void
1222 _mesa_record_error(struct gl_context *ctx, GLenum error)
1223 {
1224 if (!ctx)
1225 return;
1226
1227 if (ctx->ErrorValue == GL_NO_ERROR) {
1228 ctx->ErrorValue = error;
1229 }
1230
1231 /* Call device driver's error handler, if any. This is used on the Mac. */
1232 if (ctx->Driver.Error) {
1233 ctx->Driver.Error(ctx);
1234 }
1235 }
1236
1237
1238 /**
1239 * Flush commands and wait for completion.
1240 */
1241 void
1242 _mesa_finish(struct gl_context *ctx)
1243 {
1244 FLUSH_CURRENT( ctx, 0 );
1245 if (ctx->Driver.Finish) {
1246 ctx->Driver.Finish(ctx);
1247 }
1248 }
1249
1250
1251 /**
1252 * Flush commands.
1253 */
1254 void
1255 _mesa_flush(struct gl_context *ctx)
1256 {
1257 FLUSH_CURRENT( ctx, 0 );
1258 if (ctx->Driver.Flush) {
1259 ctx->Driver.Flush(ctx);
1260 }
1261 }
1262
1263
1264
1265 /**
1266 * Execute glFinish().
1267 *
1268 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
1269 * dd_function_table::Finish driver callback, if not NULL.
1270 */
1271 void GLAPIENTRY
1272 _mesa_Finish(void)
1273 {
1274 GET_CURRENT_CONTEXT(ctx);
1275 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1276 _mesa_finish(ctx);
1277 }
1278
1279
1280 /**
1281 * Execute glFlush().
1282 *
1283 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
1284 * dd_function_table::Flush driver callback, if not NULL.
1285 */
1286 void GLAPIENTRY
1287 _mesa_Flush(void)
1288 {
1289 GET_CURRENT_CONTEXT(ctx);
1290 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1291 _mesa_flush(ctx);
1292 }
1293
1294
1295 /**
1296 * Set mvp_with_dp4 flag. If a driver has a preference for DP4 over
1297 * MUL/MAD, or vice versa, call this function to register that.
1298 * Otherwise we default to MUL/MAD.
1299 */
1300 void
1301 _mesa_set_mvp_with_dp4( struct gl_context *ctx,
1302 GLboolean flag )
1303 {
1304 ctx->mvp_with_dp4 = flag;
1305 }
1306
1307
1308
1309 /**
1310 * Prior to drawing anything with glBegin, glDrawArrays, etc. this function
1311 * is called to see if it's valid to render. This involves checking that
1312 * the current shader is valid and the framebuffer is complete.
1313 * If an error is detected it'll be recorded here.
1314 * \return GL_TRUE if OK to render, GL_FALSE if not
1315 */
1316 GLboolean
1317 _mesa_valid_to_render(struct gl_context *ctx, const char *where)
1318 {
1319 /* This depends on having up to date derived state (shaders) */
1320 if (ctx->NewState)
1321 _mesa_update_state(ctx);
1322
1323
1324 return GL_TRUE;
1325 }
1326
1327
1328 /*@}*/