Sync with trunk r63637.
[reactos.git] / 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_COLOR], 1.0, 1.0, 1.0, 1.0 );
377 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR_INDEX], 1.0, 0.0, 0.0, 1.0 );
378 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_EDGEFLAG], 1.0, 0.0, 0.0, 1.0 );
379 }
380
381 /**
382 * Initialize fields of gl_constants (aka ctx->Const.*).
383 * Use defaults from config.h. The device drivers will often override
384 * some of these values (such as number of texture units).
385 */
386 static void
387 _mesa_init_constants(struct gl_context *ctx)
388 {
389 assert(ctx);
390
391 /* Constants, may be overriden (usually only reduced) by device drivers */
392 ctx->Const.MaxTextureMbytes = MAX_TEXTURE_MBYTES;
393 ctx->Const.MaxTextureLevels = MAX_TEXTURE_LEVELS;
394 ctx->Const.MaxCubeTextureLevels = MAX_CUBE_TEXTURE_LEVELS;
395 ctx->Const.MaxTextureMaxAnisotropy = MAX_TEXTURE_MAX_ANISOTROPY;
396 ctx->Const.MaxArrayLockSize = MAX_ARRAY_LOCK_SIZE;
397 ctx->Const.SubPixelBits = SUB_PIXEL_BITS;
398 ctx->Const.MinPointSize = MIN_POINT_SIZE;
399 ctx->Const.MaxPointSize = MAX_POINT_SIZE;
400 ctx->Const.MinPointSizeAA = MIN_POINT_SIZE;
401 ctx->Const.MaxPointSizeAA = MAX_POINT_SIZE;
402 ctx->Const.PointSizeGranularity = (GLfloat) POINT_SIZE_GRANULARITY;
403 ctx->Const.MinLineWidth = MIN_LINE_WIDTH;
404 ctx->Const.MaxLineWidth = MAX_LINE_WIDTH;
405 ctx->Const.MinLineWidthAA = MIN_LINE_WIDTH;
406 ctx->Const.MaxLineWidthAA = MAX_LINE_WIDTH;
407 ctx->Const.LineWidthGranularity = (GLfloat) LINE_WIDTH_GRANULARITY;
408 ctx->Const.MaxClipPlanes = 6;
409 ctx->Const.MaxLights = MAX_LIGHTS;
410 ctx->Const.MaxShininess = 128.0;
411 ctx->Const.MaxSpotExponent = 128.0;
412 ctx->Const.MaxViewportWidth = MAX_WIDTH;
413 ctx->Const.MaxViewportHeight = MAX_HEIGHT;
414
415 /* CheckArrayBounds is overriden by drivers/x11 for X server */
416 ctx->Const.CheckArrayBounds = GL_FALSE;
417
418 /* GL 3.2: hard-coded for now: */
419 ctx->Const.ProfileMask = GL_CONTEXT_COMPATIBILITY_PROFILE_BIT;
420 }
421
422
423 /**
424 * Do some sanity checks on the limits/constants for the given context.
425 * Only called the first time a context is bound.
426 */
427 static void
428 check_context_limits(struct gl_context *ctx)
429 {
430 /* Texture size checks */
431 assert(ctx->Const.MaxTextureLevels <= MAX_TEXTURE_LEVELS);
432 assert(ctx->Const.Max3DTextureLevels <= MAX_3D_TEXTURE_LEVELS);
433 assert(ctx->Const.MaxCubeTextureLevels <= MAX_CUBE_TEXTURE_LEVELS);
434
435 /* make sure largest texture image is <= MAX_WIDTH in size */
436 assert((1 << (ctx->Const.MaxTextureLevels - 1)) <= MAX_WIDTH);
437 assert((1 << (ctx->Const.MaxCubeTextureLevels - 1)) <= MAX_WIDTH);
438 assert((1 << (ctx->Const.Max3DTextureLevels - 1)) <= MAX_WIDTH);
439
440 /* Texture level checks */
441 assert(MAX_TEXTURE_LEVELS >= MAX_3D_TEXTURE_LEVELS);
442 assert(MAX_TEXTURE_LEVELS >= MAX_CUBE_TEXTURE_LEVELS);
443
444 /* Max texture size should be <= max viewport size (render to texture) */
445 assert((1 << (MAX_TEXTURE_LEVELS - 1)) <= MAX_WIDTH);
446
447 assert(ctx->Const.MaxViewportWidth <= MAX_WIDTH);
448 assert(ctx->Const.MaxViewportHeight <= MAX_WIDTH);
449 }
450
451
452 /**
453 * Initialize the attribute groups in a GL context.
454 *
455 * \param ctx GL context.
456 *
457 * Initializes all the attributes, calling the respective <tt>init*</tt>
458 * functions for the more complex data structures.
459 */
460 static GLboolean
461 init_attrib_groups(struct gl_context *ctx)
462 {
463 assert(ctx);
464
465 /* Constants */
466 _mesa_init_constants( ctx );
467
468 /* Extensions */
469 _mesa_init_extensions( ctx );
470
471 /* Attribute Groups */
472 _mesa_init_accum( ctx );
473 _mesa_init_attrib( ctx );
474 _mesa_init_buffer_objects( ctx );
475 _mesa_init_color( ctx );
476 _mesa_init_current( ctx );
477 _mesa_init_depth( ctx );
478 _mesa_init_display_list( ctx );
479 _mesa_init_eval( ctx );
480 _mesa_init_feedback( ctx );
481 _mesa_init_fog( ctx );
482 _mesa_init_hint( ctx );
483 _mesa_init_line( ctx );
484 _mesa_init_lighting( ctx );
485 _mesa_init_matrix( ctx );
486 _mesa_init_multisample( ctx );
487 _mesa_init_pixel( ctx );
488 _mesa_init_pixelstore( ctx );
489 _mesa_init_point( ctx );
490 _mesa_init_polygon( ctx );
491 _mesa_init_rastpos( ctx );
492 _mesa_init_scissor( ctx );
493 _mesa_init_stencil( ctx );
494 _mesa_init_transform( ctx );
495 _mesa_init_varray(ctx, &ctx->Array);
496 _mesa_init_viewport( ctx );
497
498 if (!_mesa_init_texture( ctx ))
499 return GL_FALSE;
500
501 /* Miscellaneous */
502 ctx->NewState = _NEW_ALL;
503 ctx->ErrorValue = (GLenum) GL_NO_ERROR;
504
505 return GL_TRUE;
506 }
507
508
509 /**
510 * Update default objects in a GL context with respect to shared state.
511 *
512 * \param ctx GL context.
513 *
514 * Removes references to old default objects, (texture objects, program
515 * objects, etc.) and changes to reference those from the current shared
516 * state.
517 */
518 static GLboolean
519 update_default_objects(struct gl_context *ctx)
520 {
521 assert(ctx);
522
523 _mesa_update_default_objects_texture(ctx);
524 _mesa_update_default_objects_buffer_objects(ctx);
525
526 return GL_TRUE;
527 }
528
529
530 /**
531 * This is the default function we plug into all dispatch table slots
532 * This helps prevents a segfault when someone calls a GL function without
533 * first checking if the extension's supported.
534 */
535 static int
536 generic_nop(void)
537 {
538 _mesa_warning(NULL, "User called no-op dispatch function (an unsupported extension function?)");
539 return 0;
540 }
541
542
543 /**
544 * Allocate and initialize a new dispatch table.
545 */
546 struct _glapi_table *
547 _mesa_alloc_dispatch_table(int size)
548 {
549 /* Find the larger of Mesa's dispatch table and libGL's dispatch table.
550 * In practice, this'll be the same for stand-alone Mesa. But for DRI
551 * Mesa we do this to accomodate different versions of libGL and various
552 * DRI drivers.
553 */
554 GLint numEntries = MAX2(_glapi_get_dispatch_table_size(), _gloffset_COUNT);
555 struct _glapi_table *table;
556
557 /* should never happen, but just in case */
558 numEntries = MAX2(numEntries, size);
559
560 table = (struct _glapi_table *) malloc(numEntries * sizeof(_glapi_proc));
561 if (table) {
562 _glapi_proc *entry = (_glapi_proc *) table;
563 GLint i;
564 for (i = 0; i < numEntries; i++) {
565 entry[i] = (_glapi_proc) generic_nop;
566 }
567 }
568 return table;
569 }
570
571
572 /**
573 * Initialize a struct gl_context struct (rendering context).
574 *
575 * This includes allocating all the other structs and arrays which hang off of
576 * the context by pointers.
577 * Note that the driver needs to pass in its dd_function_table here since
578 * we need to at least call driverFunctions->NewTextureObject to create the
579 * default texture objects.
580 *
581 * Called by _mesa_create_context().
582 *
583 * Performs the imports and exports callback tables initialization, and
584 * miscellaneous one-time initializations. If no shared context is supplied one
585 * is allocated, and increase its reference count. Setups the GL API dispatch
586 * tables. Initialize the TNL module. Sets the maximum Z buffer depth.
587 * Finally queries the \c MESA_DEBUG and \c MESA_VERBOSE environment variables
588 * for debug flags.
589 *
590 * \param ctx the context to initialize
591 * \param api the GL API type to create the context for
592 * \param visual describes the visual attributes for this context
593 * \param share_list points to context to share textures, display lists,
594 * etc with, or NULL
595 * \param driverFunctions table of device driver functions for this context
596 * to use
597 * \param driverContext pointer to driver-specific context data
598 */
599 GLboolean
600 _mesa_initialize_context(struct gl_context *ctx,
601 const struct gl_config *visual,
602 struct gl_context *share_list,
603 const struct dd_function_table *driverFunctions,
604 void *driverContext)
605 {
606 struct gl_shared_state *shared;
607
608 /*ASSERT(driverContext);*/
609 assert(driverFunctions->NewTextureObject);
610 assert(driverFunctions->FreeTextureImageBuffer);
611
612 ctx->Visual = *visual;
613 ctx->DrawBuffer = NULL;
614 ctx->ReadBuffer = NULL;
615 ctx->WinSysDrawBuffer = NULL;
616 ctx->WinSysReadBuffer = NULL;
617
618 /* misc one-time initializations */
619 one_time_init(ctx);
620
621 /* Plug in driver functions and context pointer here.
622 * This is important because when we call alloc_shared_state() below
623 * we'll call ctx->Driver.NewTextureObject() to create the default
624 * textures.
625 */
626 ctx->Driver = *driverFunctions;
627 ctx->DriverCtx = driverContext;
628
629 if (share_list) {
630 /* share state with another context */
631 shared = share_list->Shared;
632 }
633 else {
634 /* allocate new, unshared state */
635 shared = _mesa_alloc_shared_state(ctx);
636 if (!shared)
637 return GL_FALSE;
638 }
639
640 _mesa_reference_shared_state(ctx, &ctx->Shared, shared);
641
642 if (!init_attrib_groups( ctx )) {
643 _mesa_reference_shared_state(ctx, &ctx->Shared, NULL);
644 return GL_FALSE;
645 }
646
647 ctx->Exec = _mesa_create_exec_table();
648
649 if (!ctx->Exec) {
650 _mesa_reference_shared_state(ctx, &ctx->Shared, NULL);
651 return GL_FALSE;
652 }
653 ctx->CurrentDispatch = ctx->Exec;
654
655 /* Mesa core handles all the formats that mesa core knows about.
656 * Drivers will want to override this list with just the formats
657 * they can handle, and confirm that appropriate fallbacks exist in
658 * _mesa_choose_tex_format().
659 */
660 memset(&ctx->TextureFormatSupported, GL_TRUE,
661 sizeof(ctx->TextureFormatSupported));
662
663 ctx->Save = _mesa_create_save_table();
664 if (!ctx->Save) {
665 _mesa_reference_shared_state(ctx, &ctx->Shared, NULL);
666 free(ctx->Exec);
667 return GL_FALSE;
668 }
669
670 _mesa_install_save_vtxfmt( ctx, &ctx->ListState.ListVtxfmt );
671
672 ctx->FirstTimeCurrent = GL_TRUE;
673
674 return GL_TRUE;
675 }
676
677
678 /**
679 * Allocate and initialize a struct gl_context structure.
680 * Note that the driver needs to pass in its dd_function_table here since
681 * we need to at least call driverFunctions->NewTextureObject to initialize
682 * the rendering context.
683 *
684 * \param api the GL API type to create the context for
685 * \param visual a struct gl_config pointer (we copy the struct contents)
686 * \param share_list another context to share display lists with or NULL
687 * \param driverFunctions points to the dd_function_table into which the
688 * driver has plugged in all its special functions.
689 * \param driverContext points to the device driver's private context state
690 *
691 * \return pointer to a new __struct gl_contextRec or NULL if error.
692 */
693 struct gl_context *
694 _mesa_create_context(const struct gl_config *visual,
695 struct gl_context *share_list,
696 const struct dd_function_table *driverFunctions,
697 void *driverContext)
698 {
699 struct gl_context *ctx;
700
701 ASSERT(visual);
702 /*ASSERT(driverContext);*/
703
704 ctx = (struct gl_context *) calloc(1, sizeof(struct gl_context));
705 if (!ctx)
706 return NULL;
707
708 if (_mesa_initialize_context(ctx, visual, share_list,
709 driverFunctions, driverContext)) {
710 return ctx;
711 }
712 else {
713 free(ctx);
714 return NULL;
715 }
716 }
717
718
719 /**
720 * Free the data associated with the given context.
721 *
722 * But doesn't free the struct gl_context struct itself.
723 *
724 * \sa _mesa_initialize_context() and init_attrib_groups().
725 */
726 void
727 _mesa_free_context_data( struct gl_context *ctx )
728 {
729 if (!_mesa_get_current_context()){
730 /* No current context, but we may need one in order to delete
731 * texture objs, etc. So temporarily bind the context now.
732 */
733 _mesa_make_current(ctx, NULL, NULL);
734 }
735
736 /* unreference WinSysDraw/Read buffers */
737 _mesa_reference_framebuffer(&ctx->WinSysDrawBuffer, NULL);
738 _mesa_reference_framebuffer(&ctx->WinSysReadBuffer, NULL);
739 _mesa_reference_framebuffer(&ctx->DrawBuffer, NULL);
740 _mesa_reference_framebuffer(&ctx->ReadBuffer, NULL);
741
742 _mesa_free_attrib_data(ctx);
743 _mesa_free_lighting_data( ctx );
744 _mesa_free_eval_data( ctx );
745 _mesa_free_texture_data( ctx );
746 _mesa_free_matrix_data( ctx );
747 _mesa_free_viewport_data( ctx );
748 _mesa_free_varray_data(ctx, &ctx->Array);
749
750 /* free dispatch tables */
751 free(ctx->Exec);
752 free(ctx->Save);
753
754 /* Shared context state (display lists, textures, etc) */
755 _mesa_reference_shared_state(ctx, &ctx->Shared, NULL);
756
757 /* needs to be after freeing shared state */
758 _mesa_free_display_list_data(ctx);
759
760 if (ctx->Extensions.String)
761 free((void *) ctx->Extensions.String);
762
763 if (ctx->VersionString)
764 free(ctx->VersionString);
765
766 /* unbind the context if it's currently bound */
767 if (ctx == _mesa_get_current_context()) {
768 _mesa_make_current(NULL, NULL, NULL);
769 }
770 }
771
772
773 /**
774 * Destroy a struct gl_context structure.
775 *
776 * \param ctx GL context.
777 *
778 * Calls _mesa_free_context_data() and frees the gl_context object itself.
779 */
780 void
781 _mesa_destroy_context( struct gl_context *ctx )
782 {
783 if (ctx) {
784 _mesa_free_context_data(ctx);
785 free( (void *) ctx );
786 }
787 }
788
789
790 #if _HAVE_FULL_GL
791 /**
792 * Copy attribute groups from one context to another.
793 *
794 * \param src source context
795 * \param dst destination context
796 * \param mask bitwise OR of GL_*_BIT flags
797 *
798 * According to the bits specified in \p mask, copies the corresponding
799 * attributes from \p src into \p dst. For many of the attributes a simple \c
800 * memcpy is not enough due to the existence of internal pointers in their data
801 * structures.
802 */
803 void
804 _mesa_copy_context( const struct gl_context *src, struct gl_context *dst,
805 GLuint mask )
806 {
807 if (mask & GL_ACCUM_BUFFER_BIT) {
808 /* OK to memcpy */
809 dst->Accum = src->Accum;
810 }
811 if (mask & GL_COLOR_BUFFER_BIT) {
812 /* OK to memcpy */
813 dst->Color = src->Color;
814 }
815 if (mask & GL_CURRENT_BIT) {
816 /* OK to memcpy */
817 dst->Current = src->Current;
818 }
819 if (mask & GL_DEPTH_BUFFER_BIT) {
820 /* OK to memcpy */
821 dst->Depth = src->Depth;
822 }
823 if (mask & GL_ENABLE_BIT) {
824 /* no op */
825 }
826 if (mask & GL_EVAL_BIT) {
827 /* OK to memcpy */
828 dst->Eval = src->Eval;
829 }
830 if (mask & GL_FOG_BIT) {
831 /* OK to memcpy */
832 dst->Fog = src->Fog;
833 }
834 if (mask & GL_HINT_BIT) {
835 /* OK to memcpy */
836 dst->Hint = src->Hint;
837 }
838 if (mask & GL_LIGHTING_BIT) {
839 GLuint i;
840 /* begin with memcpy */
841 dst->Light = src->Light;
842 /* fixup linked lists to prevent pointer insanity */
843 make_empty_list( &(dst->Light.EnabledList) );
844 for (i = 0; i < MAX_LIGHTS; i++) {
845 if (dst->Light.Light[i].Enabled) {
846 insert_at_tail(&(dst->Light.EnabledList), &(dst->Light.Light[i]));
847 }
848 }
849 }
850 if (mask & GL_LINE_BIT) {
851 /* OK to memcpy */
852 dst->Line = src->Line;
853 }
854 if (mask & GL_LIST_BIT) {
855 /* OK to memcpy */
856 dst->List = src->List;
857 }
858 if (mask & GL_PIXEL_MODE_BIT) {
859 /* OK to memcpy */
860 dst->Pixel = src->Pixel;
861 }
862 if (mask & GL_POINT_BIT) {
863 /* OK to memcpy */
864 dst->Point = src->Point;
865 }
866 if (mask & GL_POLYGON_BIT) {
867 /* OK to memcpy */
868 dst->Polygon = src->Polygon;
869 }
870 if (mask & GL_POLYGON_STIPPLE_BIT) {
871 /* Use loop instead of memcpy due to problem with Portland Group's
872 * C compiler. Reported by John Stone.
873 */
874 GLuint i;
875 for (i = 0; i < 32; i++) {
876 dst->PolygonStipple[i] = src->PolygonStipple[i];
877 }
878 }
879 if (mask & GL_SCISSOR_BIT) {
880 /* OK to memcpy */
881 dst->Scissor = src->Scissor;
882 }
883 if (mask & GL_STENCIL_BUFFER_BIT) {
884 /* OK to memcpy */
885 dst->Stencil = src->Stencil;
886 }
887 if (mask & GL_TEXTURE_BIT) {
888 /* Cannot memcpy because of pointers */
889 _mesa_copy_texture_state(src, dst);
890 }
891 if (mask & GL_TRANSFORM_BIT) {
892 /* OK to memcpy */
893 dst->Transform = src->Transform;
894 }
895 if (mask & GL_VIEWPORT_BIT) {
896 /* Cannot use memcpy, because of pointers in GLmatrix _WindowMap */
897 dst->Viewport.X = src->Viewport.X;
898 dst->Viewport.Y = src->Viewport.Y;
899 dst->Viewport.Width = src->Viewport.Width;
900 dst->Viewport.Height = src->Viewport.Height;
901 dst->Viewport.Near = src->Viewport.Near;
902 dst->Viewport.Far = src->Viewport.Far;
903 _math_matrix_copy(&dst->Viewport._WindowMap, &src->Viewport._WindowMap);
904 }
905
906 /* XXX FIXME: Call callbacks?
907 */
908 dst->NewState = _NEW_ALL;
909 }
910 #endif
911
912
913 /**
914 * Check if the given context can render into the given framebuffer
915 * by checking visual attributes.
916 *
917 * Most of these tests could go away because Mesa is now pretty flexible
918 * in terms of mixing rendering contexts with framebuffers. As long
919 * as RGB vs. CI mode agree, we're probably good.
920 *
921 * \return GL_TRUE if compatible, GL_FALSE otherwise.
922 */
923 static GLboolean
924 check_compatible(const struct gl_context *ctx,
925 const struct gl_framebuffer *buffer)
926 {
927 const struct gl_config *ctxvis = &ctx->Visual;
928 const struct gl_config *bufvis = &buffer->Visual;
929
930 #if 0
931 /* disabling this fixes the fgl_glxgears pbuffer demo */
932 if (ctxvis->doubleBufferMode && !bufvis->doubleBufferMode)
933 return GL_FALSE;
934 #endif
935 if (ctxvis->stereoMode && !bufvis->stereoMode)
936 return GL_FALSE;
937 if (ctxvis->haveAccumBuffer && !bufvis->haveAccumBuffer)
938 return GL_FALSE;
939 if (ctxvis->haveDepthBuffer && !bufvis->haveDepthBuffer)
940 return GL_FALSE;
941 if (ctxvis->haveStencilBuffer && !bufvis->haveStencilBuffer)
942 return GL_FALSE;
943 if (ctxvis->redMask && ctxvis->redMask != bufvis->redMask)
944 return GL_FALSE;
945 if (ctxvis->greenMask && ctxvis->greenMask != bufvis->greenMask)
946 return GL_FALSE;
947 if (ctxvis->blueMask && ctxvis->blueMask != bufvis->blueMask)
948 return GL_FALSE;
949 #if 0
950 /* disabled (see bug 11161) */
951 if (ctxvis->depthBits && ctxvis->depthBits != bufvis->depthBits)
952 return GL_FALSE;
953 #endif
954 if (ctxvis->stencilBits && ctxvis->stencilBits != bufvis->stencilBits)
955 return GL_FALSE;
956
957 return GL_TRUE;
958 }
959
960
961 /**
962 * Do one-time initialization for the given framebuffer. Specifically,
963 * ask the driver for the window's current size and update the framebuffer
964 * object to match.
965 * Really, the device driver should totally take care of this.
966 */
967 static void
968 initialize_framebuffer_size(struct gl_context *ctx, struct gl_framebuffer *fb)
969 {
970 GLuint width, height;
971 if (ctx->Driver.GetBufferSize) {
972 ctx->Driver.GetBufferSize(fb, &width, &height);
973 if (ctx->Driver.ResizeBuffers)
974 ctx->Driver.ResizeBuffers(ctx, fb, width, height);
975 fb->Initialized = GL_TRUE;
976 }
977 }
978
979
980 /**
981 * Check if the viewport/scissor size has not yet been initialized.
982 * Initialize the size if the given width and height are non-zero.
983 */
984 void
985 _mesa_check_init_viewport(struct gl_context *ctx, GLuint width, GLuint height)
986 {
987 if (!ctx->ViewportInitialized && width > 0 && height > 0) {
988 /* Note: set flag here, before calling _mesa_set_viewport(), to prevent
989 * potential infinite recursion.
990 */
991 ctx->ViewportInitialized = GL_TRUE;
992 _mesa_set_viewport(ctx, 0, 0, width, height);
993 _mesa_set_scissor(ctx, 0, 0, width, height);
994 }
995 }
996
997
998 /**
999 * Bind the given context to the given drawBuffer and readBuffer and
1000 * make it the current context for the calling thread.
1001 * We'll render into the drawBuffer and read pixels from the
1002 * readBuffer (i.e. glRead/CopyPixels, glCopyTexImage, etc).
1003 *
1004 * We check that the context's and framebuffer's visuals are compatible
1005 * and return immediately if they're not.
1006 *
1007 * \param newCtx the new GL context. If NULL then there will be no current GL
1008 * context.
1009 * \param drawBuffer the drawing framebuffer
1010 * \param readBuffer the reading framebuffer
1011 */
1012 GLboolean
1013 _mesa_make_current( struct gl_context *newCtx,
1014 struct gl_framebuffer *drawBuffer,
1015 struct gl_framebuffer *readBuffer )
1016 {
1017 GET_CURRENT_CONTEXT(curCtx);
1018
1019 if (MESA_VERBOSE & VERBOSE_API)
1020 _mesa_debug(newCtx, "_mesa_make_current()\n");
1021
1022 /* Check that the context's and framebuffer's visuals are compatible.
1023 */
1024 if (newCtx && drawBuffer && newCtx->WinSysDrawBuffer != drawBuffer) {
1025 if (!check_compatible(newCtx, drawBuffer)) {
1026 _mesa_warning(newCtx,
1027 "MakeCurrent: incompatible visuals for context and drawbuffer");
1028 return GL_FALSE;
1029 }
1030 }
1031 if (newCtx && readBuffer && newCtx->WinSysReadBuffer != readBuffer) {
1032 if (!check_compatible(newCtx, readBuffer)) {
1033 _mesa_warning(newCtx,
1034 "MakeCurrent: incompatible visuals for context and readbuffer");
1035 return GL_FALSE;
1036 }
1037 }
1038
1039 if (curCtx &&
1040 (curCtx->WinSysDrawBuffer || curCtx->WinSysReadBuffer) &&
1041 /* make sure this context is valid for flushing */
1042 curCtx != newCtx)
1043 _mesa_flush(curCtx);
1044
1045 /* We used to call _glapi_check_multithread() here. Now do it in drivers */
1046 _glapi_set_context((void *) newCtx);
1047 ASSERT(_mesa_get_current_context() == newCtx);
1048
1049 if (!newCtx) {
1050 _glapi_set_dispatch(NULL); /* none current */
1051 }
1052 else {
1053 _glapi_set_dispatch(newCtx->CurrentDispatch);
1054
1055 if (drawBuffer && readBuffer) {
1056 ASSERT(drawBuffer->Name == 0);
1057 ASSERT(readBuffer->Name == 0);
1058 _mesa_reference_framebuffer(&newCtx->WinSysDrawBuffer, drawBuffer);
1059 _mesa_reference_framebuffer(&newCtx->WinSysReadBuffer, readBuffer);
1060
1061 /*
1062 * Only set the context's Draw/ReadBuffer fields if they're NULL
1063 * or not bound to a user-created FBO.
1064 */
1065 _mesa_reference_framebuffer(&newCtx->DrawBuffer, drawBuffer);
1066 /* Update the FBO's list of drawbuffers/renderbuffers.
1067 * For winsys FBOs this comes from the GL state (which may have
1068 * changed since the last time this FBO was bound).
1069 */
1070 _mesa_update_draw_buffer(newCtx);
1071
1072 _mesa_reference_framebuffer(&newCtx->ReadBuffer, readBuffer);
1073
1074 /* XXX only set this flag if we're really changing the draw/read
1075 * framebuffer bindings.
1076 */
1077 newCtx->NewState |= _NEW_BUFFERS;
1078
1079 #if 1
1080 /* We want to get rid of these lines: */
1081
1082 #if _HAVE_FULL_GL
1083 if (!drawBuffer->Initialized) {
1084 initialize_framebuffer_size(newCtx, drawBuffer);
1085 }
1086 if (readBuffer != drawBuffer && !readBuffer->Initialized) {
1087 initialize_framebuffer_size(newCtx, readBuffer);
1088 }
1089
1090 _mesa_resizebuffers(newCtx);
1091 #endif
1092
1093 #else
1094 /* We want the drawBuffer and readBuffer to be initialized by
1095 * the driver.
1096 * This generally means the Width and Height match the actual
1097 * window size and the renderbuffers (both hardware and software
1098 * based) are allocated to match. The later can generally be
1099 * done with a call to _mesa_resize_framebuffer().
1100 *
1101 * It's theoretically possible for a buffer to have zero width
1102 * or height, but for now, assert check that the driver did what's
1103 * expected of it.
1104 */
1105 ASSERT(drawBuffer->Width > 0);
1106 ASSERT(drawBuffer->Height > 0);
1107 #endif
1108
1109 if (drawBuffer) {
1110 _mesa_check_init_viewport(newCtx,
1111 drawBuffer->Width, drawBuffer->Height);
1112 }
1113 }
1114
1115 if (newCtx->FirstTimeCurrent) {
1116 _mesa_compute_version(newCtx);
1117
1118 newCtx->Extensions.String = _mesa_make_extension_string(newCtx);
1119
1120 check_context_limits(newCtx);
1121
1122 newCtx->FirstTimeCurrent = GL_FALSE;
1123 }
1124 }
1125
1126 return GL_TRUE;
1127 }
1128
1129
1130 /**
1131 * Make context 'ctx' share the display lists, textures and programs
1132 * that are associated with 'ctxToShare'.
1133 * Any display lists, textures or programs associated with 'ctx' will
1134 * be deleted if nobody else is sharing them.
1135 */
1136 GLboolean
1137 _mesa_share_state(struct gl_context *ctx, struct gl_context *ctxToShare)
1138 {
1139 if (ctx && ctxToShare && ctx->Shared && ctxToShare->Shared) {
1140 struct gl_shared_state *oldShared = NULL;
1141
1142 /* save ref to old state to prevent it from being deleted immediately */
1143 _mesa_reference_shared_state(ctx, &oldShared, ctx->Shared);
1144
1145 /* update ctx's Shared pointer */
1146 _mesa_reference_shared_state(ctx, &ctx->Shared, ctxToShare->Shared);
1147
1148 update_default_objects(ctx);
1149
1150 /* release the old shared state */
1151 _mesa_reference_shared_state(ctx, &oldShared, NULL);
1152
1153 return GL_TRUE;
1154 }
1155 else {
1156 return GL_FALSE;
1157 }
1158 }
1159
1160
1161
1162 /**
1163 * \return pointer to the current GL context for this thread.
1164 *
1165 * Calls _glapi_get_context(). This isn't the fastest way to get the current
1166 * context. If you need speed, see the #GET_CURRENT_CONTEXT macro in
1167 * context.h.
1168 */
1169 struct gl_context *
1170 _mesa_get_current_context( void )
1171 {
1172 return (struct gl_context *) _glapi_get_context();
1173 }
1174
1175
1176 /**
1177 * Get context's current API dispatch table.
1178 *
1179 * It'll either be the immediate-mode execute dispatcher or the display list
1180 * compile dispatcher.
1181 *
1182 * \param ctx GL context.
1183 *
1184 * \return pointer to dispatch_table.
1185 *
1186 * Simply returns __struct gl_contextRec::CurrentDispatch.
1187 */
1188 struct _glapi_table *
1189 _mesa_get_dispatch(struct gl_context *ctx)
1190 {
1191 return ctx->CurrentDispatch;
1192 }
1193
1194 /*@}*/
1195
1196
1197 /**********************************************************************/
1198 /** \name Miscellaneous functions */
1199 /**********************************************************************/
1200 /*@{*/
1201
1202 /**
1203 * Record an error.
1204 *
1205 * \param ctx GL context.
1206 * \param error error code.
1207 *
1208 * Records the given error code and call the driver's dd_function_table::Error
1209 * function if defined.
1210 *
1211 * \sa
1212 * This is called via _mesa_error().
1213 */
1214 void
1215 _mesa_record_error(struct gl_context *ctx, GLenum error)
1216 {
1217 if (!ctx)
1218 return;
1219
1220 if (ctx->ErrorValue == GL_NO_ERROR) {
1221 ctx->ErrorValue = error;
1222 }
1223
1224 /* Call device driver's error handler, if any. This is used on the Mac. */
1225 if (ctx->Driver.Error) {
1226 ctx->Driver.Error(ctx);
1227 }
1228 }
1229
1230
1231 /**
1232 * Flush commands and wait for completion.
1233 */
1234 void
1235 _mesa_finish(struct gl_context *ctx)
1236 {
1237 FLUSH_CURRENT( ctx, 0 );
1238 if (ctx->Driver.Finish) {
1239 ctx->Driver.Finish(ctx);
1240 }
1241 }
1242
1243
1244 /**
1245 * Flush commands.
1246 */
1247 void
1248 _mesa_flush(struct gl_context *ctx)
1249 {
1250 FLUSH_CURRENT( ctx, 0 );
1251 if (ctx->Driver.Flush) {
1252 ctx->Driver.Flush(ctx);
1253 }
1254 }
1255
1256
1257
1258 /**
1259 * Execute glFinish().
1260 *
1261 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
1262 * dd_function_table::Finish driver callback, if not NULL.
1263 */
1264 void GLAPIENTRY
1265 _mesa_Finish(void)
1266 {
1267 GET_CURRENT_CONTEXT(ctx);
1268 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1269 _mesa_finish(ctx);
1270 }
1271
1272
1273 /**
1274 * Execute glFlush().
1275 *
1276 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
1277 * dd_function_table::Flush driver callback, if not NULL.
1278 */
1279 void GLAPIENTRY
1280 _mesa_Flush(void)
1281 {
1282 GET_CURRENT_CONTEXT(ctx);
1283 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1284 _mesa_flush(ctx);
1285 }
1286
1287
1288 /**
1289 * Set mvp_with_dp4 flag. If a driver has a preference for DP4 over
1290 * MUL/MAD, or vice versa, call this function to register that.
1291 * Otherwise we default to MUL/MAD.
1292 */
1293 void
1294 _mesa_set_mvp_with_dp4( struct gl_context *ctx,
1295 GLboolean flag )
1296 {
1297 ctx->mvp_with_dp4 = flag;
1298 }
1299
1300
1301
1302 /**
1303 * Prior to drawing anything with glBegin, glDrawArrays, etc. this function
1304 * is called to see if it's valid to render. This involves checking that
1305 * the current shader is valid and the framebuffer is complete.
1306 * If an error is detected it'll be recorded here.
1307 * \return GL_TRUE if OK to render, GL_FALSE if not
1308 */
1309 GLboolean
1310 _mesa_valid_to_render(struct gl_context *ctx, const char *where)
1311 {
1312 /* This depends on having up to date derived state (shaders) */
1313 if (ctx->NewState)
1314 _mesa_update_state(ctx);
1315
1316
1317 return GL_TRUE;
1318 }
1319
1320
1321 /*@}*/