[MESA]
[reactos.git] / reactos / dll / opengl / mesa / src / 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
79 #include "glheader.h"
80 #include "mfeatures.h"
81 #include "imports.h"
82 #include "accum.h"
83 #include "api_exec.h"
84 #include "arrayobj.h"
85 #include "attrib.h"
86 #include "blend.h"
87 #include "buffers.h"
88 #include "bufferobj.h"
89 #include "context.h"
90 #include "cpuinfo.h"
91 #include "depth.h"
92 #include "dlist.h"
93 #include "eval.h"
94 #include "extensions.h"
95 #include "feedback.h"
96 #include "fog.h"
97 #include "formats.h"
98 #include "framebuffer.h"
99 #include "hint.h"
100 #include "hash.h"
101 #include "light.h"
102 #include "lines.h"
103 #include "macros.h"
104 #include "matrix.h"
105 #include "multisample.h"
106 #include "pixel.h"
107 #include "pixelstore.h"
108 #include "points.h"
109 #include "polygon.h"
110 #include "rastpos.h"
111 #include "scissor.h"
112 #include "shared.h"
113 #include "shaderobj.h"
114 #include "simple_list.h"
115 #include "state.h"
116 #include "stencil.h"
117 #include "texstate.h"
118 #include "mtypes.h"
119 #include "varray.h"
120 #include "version.h"
121 #include "viewport.h"
122 #include "vtxfmt.h"
123 #include "program/program.h"
124 #include "program/prog_print.h"
125 #if _HAVE_FULL_GL
126 #include "math/m_matrix.h"
127 #endif
128 #include "main/dispatch.h" /* for _gloffset_COUNT */
129
130 #ifdef USE_SPARC_ASM
131 #include "sparc/sparc.h"
132 #endif
133
134 #include "glsl_parser_extras.h"
135 #include <stdbool.h>
136
137
138 #ifndef MESA_VERBOSE
139 int MESA_VERBOSE = 0;
140 #endif
141
142 #ifndef MESA_DEBUG_FLAGS
143 int MESA_DEBUG_FLAGS = 0;
144 #endif
145
146
147 /* ubyte -> float conversion */
148 GLfloat _mesa_ubyte_to_float_color_tab[256];
149
150
151
152 /**
153 * Swap buffers notification callback.
154 *
155 * \param ctx GL context.
156 *
157 * Called by window system just before swapping buffers.
158 * We have to finish any pending rendering.
159 */
160 void
161 _mesa_notifySwapBuffers(struct gl_context *ctx)
162 {
163 if (MESA_VERBOSE & VERBOSE_SWAPBUFFERS)
164 _mesa_debug(ctx, "SwapBuffers\n");
165 FLUSH_CURRENT( ctx, 0 );
166 if (ctx->Driver.Flush) {
167 ctx->Driver.Flush(ctx);
168 }
169 }
170
171
172 /**********************************************************************/
173 /** \name GL Visual allocation/destruction */
174 /**********************************************************************/
175 /*@{*/
176
177 /**
178 * Allocates a struct gl_config structure and initializes it via
179 * _mesa_initialize_visual().
180 *
181 * \param dbFlag double buffering
182 * \param stereoFlag stereo buffer
183 * \param depthBits requested bits per depth buffer value. Any value in [0, 32]
184 * is acceptable but the actual depth type will be GLushort or GLuint as
185 * needed.
186 * \param stencilBits requested minimum bits per stencil buffer value
187 * \param accumRedBits, accumGreenBits, accumBlueBits, accumAlphaBits number
188 * of bits per color component in accum buffer.
189 * \param indexBits number of bits per pixel if \p rgbFlag is GL_FALSE
190 * \param redBits number of bits per color component in frame buffer for RGB(A)
191 * mode. We always use 8 in core Mesa though.
192 * \param greenBits same as above.
193 * \param blueBits same as above.
194 * \param alphaBits same as above.
195 *
196 * \return pointer to new struct gl_config or NULL if requested parameters
197 * can't be met.
198 *
199 * \note Need to add params for level and numAuxBuffers (at least)
200 */
201 struct gl_config *
202 _mesa_create_visual( GLboolean dbFlag,
203 GLboolean stereoFlag,
204 GLint redBits,
205 GLint greenBits,
206 GLint blueBits,
207 GLint alphaBits,
208 GLint depthBits,
209 GLint stencilBits,
210 GLint accumRedBits,
211 GLint accumGreenBits,
212 GLint accumBlueBits,
213 GLint accumAlphaBits)
214 {
215 struct gl_config *vis = CALLOC_STRUCT(gl_config);
216 if (vis) {
217 if (!_mesa_initialize_visual(vis, dbFlag, stereoFlag,
218 redBits, greenBits, blueBits, alphaBits,
219 depthBits, stencilBits,
220 accumRedBits, accumGreenBits,
221 accumBlueBits, accumAlphaBits)) {
222 free(vis);
223 return NULL;
224 }
225 }
226 return vis;
227 }
228
229
230 /**
231 * Makes some sanity checks and fills in the fields of the struct
232 * gl_config object with the given parameters. If the caller needs to
233 * set additional fields, he should just probably init the whole
234 * gl_config object himself.
235 *
236 * \return GL_TRUE on success, or GL_FALSE on failure.
237 *
238 * \sa _mesa_create_visual() above for the parameter description.
239 */
240 GLboolean
241 _mesa_initialize_visual( struct gl_config *vis,
242 GLboolean dbFlag,
243 GLboolean stereoFlag,
244 GLint redBits,
245 GLint greenBits,
246 GLint blueBits,
247 GLint alphaBits,
248 GLint depthBits,
249 GLint stencilBits,
250 GLint accumRedBits,
251 GLint accumGreenBits,
252 GLint accumBlueBits,
253 GLint accumAlphaBits)
254 {
255 assert(vis);
256
257 if (depthBits < 0 || depthBits > 32) {
258 return GL_FALSE;
259 }
260 if (stencilBits < 0 || stencilBits > STENCIL_BITS) {
261 return GL_FALSE;
262 }
263 assert(accumRedBits >= 0);
264 assert(accumGreenBits >= 0);
265 assert(accumBlueBits >= 0);
266 assert(accumAlphaBits >= 0);
267
268 vis->rgbMode = GL_TRUE;
269 vis->doubleBufferMode = dbFlag;
270 vis->stereoMode = stereoFlag;
271
272 vis->redBits = redBits;
273 vis->greenBits = greenBits;
274 vis->blueBits = blueBits;
275 vis->alphaBits = alphaBits;
276 vis->rgbBits = redBits + greenBits + blueBits;
277
278 vis->indexBits = 0;
279 vis->depthBits = depthBits;
280 vis->stencilBits = stencilBits;
281
282 vis->accumRedBits = accumRedBits;
283 vis->accumGreenBits = accumGreenBits;
284 vis->accumBlueBits = accumBlueBits;
285 vis->accumAlphaBits = accumAlphaBits;
286
287 vis->haveAccumBuffer = accumRedBits > 0;
288 vis->haveDepthBuffer = depthBits > 0;
289 vis->haveStencilBuffer = stencilBits > 0;
290
291 vis->numAuxBuffers = 0;
292 vis->level = 0;
293
294 return GL_TRUE;
295 }
296
297
298 /**
299 * Destroy a visual and free its memory.
300 *
301 * \param vis visual.
302 *
303 * Frees the visual structure.
304 */
305 void
306 _mesa_destroy_visual( struct gl_config *vis )
307 {
308 free(vis);
309 }
310
311 /*@}*/
312
313
314 /**********************************************************************/
315 /** \name Context allocation, initialization, destroying
316 *
317 * The purpose of the most initialization functions here is to provide the
318 * default state values according to the OpenGL specification.
319 */
320 /**********************************************************************/
321 /*@{*/
322
323
324 /**
325 * This is lame. gdb only seems to recognize enum types that are
326 * actually used somewhere. We want to be able to print/use enum
327 * values such as TEXTURE_2D_INDEX in gdb. But we don't actually use
328 * the gl_texture_index type anywhere. Thus, this lame function.
329 */
330 static void
331 dummy_enum_func(void)
332 {
333 gl_buffer_index bi = BUFFER_FRONT_LEFT;
334 gl_face_index fi = FACE_POS_X;
335 gl_frag_attrib fa = FRAG_ATTRIB_WPOS;
336 gl_frag_result fr = FRAG_RESULT_DEPTH;
337 gl_vert_attrib va = VERT_ATTRIB_POS;
338 gl_vert_result vr = VERT_RESULT_HPOS;
339
340 (void) bi;
341 (void) fi;
342 (void) fa;
343 (void) fr;
344 (void) va;
345 (void) vr;
346 }
347
348
349 /**
350 * One-time initialization mutex lock.
351 *
352 * \sa Used by one_time_init().
353 */
354 _glthread_DECLARE_STATIC_MUTEX(OneTimeLock);
355
356
357
358 /**
359 * Calls all the various one-time-init functions in Mesa.
360 *
361 * While holding a global mutex lock, calls several initialization functions,
362 * and sets the glapi callbacks if the \c MESA_DEBUG environment variable is
363 * defined.
364 *
365 * \sa _math_init().
366 */
367 static void
368 one_time_init( struct gl_context *ctx )
369 {
370 static GLboolean api_init = GL_FALSE;
371
372 _glthread_LOCK_MUTEX(OneTimeLock);
373
374 /* truly one-time init */
375 if (!api_init) {
376 GLuint i;
377
378 /* do some implementation tests */
379 assert( sizeof(GLbyte) == 1 );
380 assert( sizeof(GLubyte) == 1 );
381 assert( sizeof(GLshort) == 2 );
382 assert( sizeof(GLushort) == 2 );
383 assert( sizeof(GLint) == 4 );
384 assert( sizeof(GLuint) == 4 );
385
386 _mesa_get_cpu_features();
387
388 _mesa_init_sqrt_table();
389
390 /* context dependence is never a one-time thing... */
391 _mesa_init_get_hash(ctx);
392
393 for (i = 0; i < 256; i++) {
394 _mesa_ubyte_to_float_color_tab[i] = (float) i / 255.0F;
395 }
396
397 #if defined(DEBUG) && defined(__DATE__) && defined(__TIME__)
398 if (MESA_VERBOSE != 0) {
399 _mesa_debug(ctx, "Mesa %s DEBUG build %s %s\n",
400 MESA_VERSION_STRING, __DATE__, __TIME__);
401 }
402 #endif
403
404 #ifdef DEBUG
405 _mesa_test_formats();
406 #endif
407 }
408
409 api_init = GL_TRUE;
410
411 _glthread_UNLOCK_MUTEX(OneTimeLock);
412
413 /* Hopefully atexit() is widely available. If not, we may need some
414 * #ifdef tests here.
415 */
416 atexit(_mesa_destroy_shader_compiler);
417
418 dummy_enum_func();
419 }
420
421
422 /**
423 * Initialize fields of gl_current_attrib (aka ctx->Current.*)
424 */
425 static void
426 _mesa_init_current(struct gl_context *ctx)
427 {
428 GLuint i;
429
430 /* Init all to (0,0,0,1) */
431 for (i = 0; i < Elements(ctx->Current.Attrib); i++) {
432 ASSIGN_4V( ctx->Current.Attrib[i], 0.0, 0.0, 0.0, 1.0 );
433 }
434
435 /* redo special cases: */
436 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_WEIGHT], 1.0, 0.0, 0.0, 0.0 );
437 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_NORMAL], 0.0, 0.0, 1.0, 1.0 );
438 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR0], 1.0, 1.0, 1.0, 1.0 );
439 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR1], 0.0, 0.0, 0.0, 1.0 );
440 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR_INDEX], 1.0, 0.0, 0.0, 1.0 );
441 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_EDGEFLAG], 1.0, 0.0, 0.0, 1.0 );
442 }
443
444
445 /**
446 * Init vertex/fragment/geometry program limits.
447 * Important: drivers should override these with actual limits.
448 */
449 static void
450 init_program_limits(GLenum type, struct gl_program_constants *prog)
451 {
452 prog->MaxInstructions = MAX_PROGRAM_INSTRUCTIONS;
453 prog->MaxAluInstructions = MAX_PROGRAM_INSTRUCTIONS;
454 prog->MaxTexInstructions = MAX_PROGRAM_INSTRUCTIONS;
455 prog->MaxTexIndirections = MAX_PROGRAM_INSTRUCTIONS;
456 prog->MaxTemps = MAX_PROGRAM_TEMPS;
457 prog->MaxEnvParams = MAX_PROGRAM_ENV_PARAMS;
458 prog->MaxLocalParams = MAX_PROGRAM_LOCAL_PARAMS;
459 prog->MaxAddressOffset = MAX_PROGRAM_LOCAL_PARAMS;
460
461 switch (type) {
462 case GL_VERTEX_PROGRAM_ARB:
463 prog->MaxParameters = MAX_VERTEX_PROGRAM_PARAMS;
464 prog->MaxAttribs = MAX_NV_VERTEX_PROGRAM_INPUTS;
465 prog->MaxAddressRegs = MAX_VERTEX_PROGRAM_ADDRESS_REGS;
466 prog->MaxUniformComponents = 4 * MAX_UNIFORMS;
467 break;
468 case GL_FRAGMENT_PROGRAM_ARB:
469 prog->MaxParameters = MAX_NV_FRAGMENT_PROGRAM_PARAMS;
470 prog->MaxAttribs = MAX_NV_FRAGMENT_PROGRAM_INPUTS;
471 prog->MaxAddressRegs = MAX_FRAGMENT_PROGRAM_ADDRESS_REGS;
472 prog->MaxUniformComponents = 4 * MAX_UNIFORMS;
473 break;
474 default:
475 assert(0 && "Bad program type in init_program_limits()");
476 }
477
478 /* Set the native limits to zero. This implies that there is no native
479 * support for shaders. Let the drivers fill in the actual values.
480 */
481 prog->MaxNativeInstructions = 0;
482 prog->MaxNativeAluInstructions = 0;
483 prog->MaxNativeTexInstructions = 0;
484 prog->MaxNativeTexIndirections = 0;
485 prog->MaxNativeAttribs = 0;
486 prog->MaxNativeTemps = 0;
487 prog->MaxNativeAddressRegs = 0;
488 prog->MaxNativeParameters = 0;
489 }
490
491
492 /**
493 * Initialize fields of gl_constants (aka ctx->Const.*).
494 * Use defaults from config.h. The device drivers will often override
495 * some of these values (such as number of texture units).
496 */
497 static void
498 _mesa_init_constants(struct gl_context *ctx)
499 {
500 assert(ctx);
501
502 /* Constants, may be overriden (usually only reduced) by device drivers */
503 ctx->Const.MaxTextureMbytes = MAX_TEXTURE_MBYTES;
504 ctx->Const.MaxTextureLevels = MAX_TEXTURE_LEVELS;
505 ctx->Const.Max3DTextureLevels = MAX_3D_TEXTURE_LEVELS;
506 ctx->Const.MaxCubeTextureLevels = MAX_CUBE_TEXTURE_LEVELS;
507 ctx->Const.MaxTextureCoordUnits = MAX_TEXTURE_COORD_UNITS;
508 ctx->Const.MaxTextureImageUnits = MAX_TEXTURE_IMAGE_UNITS;
509 ctx->Const.MaxTextureUnits = MIN2(ctx->Const.MaxTextureCoordUnits,
510 ctx->Const.MaxTextureImageUnits);
511 ctx->Const.MaxTextureMaxAnisotropy = MAX_TEXTURE_MAX_ANISOTROPY;
512 ctx->Const.MaxArrayLockSize = MAX_ARRAY_LOCK_SIZE;
513 ctx->Const.SubPixelBits = SUB_PIXEL_BITS;
514 ctx->Const.MinPointSize = MIN_POINT_SIZE;
515 ctx->Const.MaxPointSize = MAX_POINT_SIZE;
516 ctx->Const.MinPointSizeAA = MIN_POINT_SIZE;
517 ctx->Const.MaxPointSizeAA = MAX_POINT_SIZE;
518 ctx->Const.PointSizeGranularity = (GLfloat) POINT_SIZE_GRANULARITY;
519 ctx->Const.MinLineWidth = MIN_LINE_WIDTH;
520 ctx->Const.MaxLineWidth = MAX_LINE_WIDTH;
521 ctx->Const.MinLineWidthAA = MIN_LINE_WIDTH;
522 ctx->Const.MaxLineWidthAA = MAX_LINE_WIDTH;
523 ctx->Const.LineWidthGranularity = (GLfloat) LINE_WIDTH_GRANULARITY;
524 ctx->Const.MaxColorTableSize = MAX_COLOR_TABLE_SIZE;
525 ctx->Const.MaxClipPlanes = 6;
526 ctx->Const.MaxLights = MAX_LIGHTS;
527 ctx->Const.MaxShininess = 128.0;
528 ctx->Const.MaxSpotExponent = 128.0;
529 ctx->Const.MaxViewportWidth = MAX_WIDTH;
530 ctx->Const.MaxViewportHeight = MAX_HEIGHT;
531 #if FEATURE_ARB_vertex_program
532 init_program_limits(GL_VERTEX_PROGRAM_ARB, &ctx->Const.VertexProgram);
533 #endif
534 #if FEATURE_ARB_fragment_program
535 init_program_limits(GL_FRAGMENT_PROGRAM_ARB, &ctx->Const.FragmentProgram);
536 #endif
537 ctx->Const.MaxProgramMatrices = MAX_PROGRAM_MATRICES;
538 ctx->Const.MaxProgramMatrixStackDepth = MAX_PROGRAM_MATRIX_STACK_DEPTH;
539
540 /* CheckArrayBounds is overriden by drivers/x11 for X server */
541 ctx->Const.CheckArrayBounds = GL_FALSE;
542
543 #if FEATURE_ARB_vertex_shader
544 ctx->Const.MaxVertexTextureImageUnits = MAX_VERTEX_TEXTURE_IMAGE_UNITS;
545 ctx->Const.MaxCombinedTextureImageUnits = MAX_COMBINED_TEXTURE_IMAGE_UNITS;
546 ctx->Const.MaxVarying = MAX_VARYING;
547 #endif
548
549 ctx->Const.GLSLVersion = 120;
550 _mesa_override_glsl_version(ctx);
551
552 /* GL_ATI_envmap_bumpmap */
553 ctx->Const.SupportedBumpUnits = SUPPORTED_ATI_BUMP_UNITS;
554
555 /* GL 3.2: hard-coded for now: */
556 ctx->Const.ProfileMask = GL_CONTEXT_COMPATIBILITY_PROFILE_BIT;
557
558 /** GL_EXT_gpu_shader4 */
559 ctx->Const.MinProgramTexelOffset = -8;
560 ctx->Const.MaxProgramTexelOffset = 7;
561
562 /* GL_ARB_robustness */
563 ctx->Const.ResetStrategy = GL_NO_RESET_NOTIFICATION_ARB;
564 }
565
566
567 /**
568 * Do some sanity checks on the limits/constants for the given context.
569 * Only called the first time a context is bound.
570 */
571 static void
572 check_context_limits(struct gl_context *ctx)
573 {
574 /* check that we don't exceed the size of various bitfields */
575 assert(VERT_RESULT_MAX <=
576 (8 * sizeof(ctx->VertexProgram._Current->Base.OutputsWritten)));
577 assert(FRAG_ATTRIB_MAX <=
578 (8 * sizeof(ctx->FragmentProgram._Current->Base.InputsRead)));
579
580 assert(MAX_COMBINED_TEXTURE_IMAGE_UNITS <= 8 * sizeof(GLbitfield));
581
582 /* shader-related checks */
583 assert(ctx->Const.FragmentProgram.MaxLocalParams <= MAX_PROGRAM_LOCAL_PARAMS);
584 assert(ctx->Const.VertexProgram.MaxLocalParams <= MAX_PROGRAM_LOCAL_PARAMS);
585
586 assert(MAX_NV_FRAGMENT_PROGRAM_TEMPS <= MAX_PROGRAM_TEMPS);
587 assert(MAX_NV_VERTEX_PROGRAM_TEMPS <= MAX_PROGRAM_TEMPS);
588 assert(MAX_NV_VERTEX_PROGRAM_INPUTS <= VERT_ATTRIB_MAX);
589 assert(MAX_NV_VERTEX_PROGRAM_OUTPUTS <= VERT_RESULT_MAX);
590
591 /* Texture unit checks */
592 assert(ctx->Const.MaxTextureImageUnits > 0);
593 assert(ctx->Const.MaxTextureImageUnits <= MAX_TEXTURE_IMAGE_UNITS);
594 assert(ctx->Const.MaxTextureCoordUnits > 0);
595 assert(ctx->Const.MaxTextureCoordUnits <= MAX_TEXTURE_COORD_UNITS);
596 assert(ctx->Const.MaxTextureUnits > 0);
597 assert(ctx->Const.MaxTextureUnits <= MAX_TEXTURE_IMAGE_UNITS);
598 assert(ctx->Const.MaxTextureUnits <= MAX_TEXTURE_COORD_UNITS);
599 assert(ctx->Const.MaxTextureUnits == MIN2(ctx->Const.MaxTextureImageUnits,
600 ctx->Const.MaxTextureCoordUnits));
601 assert(ctx->Const.MaxCombinedTextureImageUnits > 0);
602 assert(ctx->Const.MaxCombinedTextureImageUnits <= MAX_COMBINED_TEXTURE_IMAGE_UNITS);
603 assert(ctx->Const.MaxTextureCoordUnits <= MAX_COMBINED_TEXTURE_IMAGE_UNITS);
604 /* number of coord units cannot be greater than number of image units */
605 assert(ctx->Const.MaxTextureCoordUnits <= ctx->Const.MaxTextureImageUnits);
606
607
608 /* Texture size checks */
609 assert(ctx->Const.MaxTextureLevels <= MAX_TEXTURE_LEVELS);
610 assert(ctx->Const.Max3DTextureLevels <= MAX_3D_TEXTURE_LEVELS);
611 assert(ctx->Const.MaxCubeTextureLevels <= MAX_CUBE_TEXTURE_LEVELS);
612
613 /* make sure largest texture image is <= MAX_WIDTH in size */
614 assert((1 << (ctx->Const.MaxTextureLevels - 1)) <= MAX_WIDTH);
615 assert((1 << (ctx->Const.MaxCubeTextureLevels - 1)) <= MAX_WIDTH);
616 assert((1 << (ctx->Const.Max3DTextureLevels - 1)) <= MAX_WIDTH);
617
618 /* Texture level checks */
619 assert(MAX_TEXTURE_LEVELS >= MAX_3D_TEXTURE_LEVELS);
620 assert(MAX_TEXTURE_LEVELS >= MAX_CUBE_TEXTURE_LEVELS);
621
622 /* Max texture size should be <= max viewport size (render to texture) */
623 assert((1 << (MAX_TEXTURE_LEVELS - 1)) <= MAX_WIDTH);
624
625 assert(ctx->Const.MaxViewportWidth <= MAX_WIDTH);
626 assert(ctx->Const.MaxViewportHeight <= MAX_WIDTH);
627 }
628
629
630 /**
631 * Initialize the attribute groups in a GL context.
632 *
633 * \param ctx GL context.
634 *
635 * Initializes all the attributes, calling the respective <tt>init*</tt>
636 * functions for the more complex data structures.
637 */
638 static GLboolean
639 init_attrib_groups(struct gl_context *ctx)
640 {
641 assert(ctx);
642
643 /* Constants */
644 _mesa_init_constants( ctx );
645
646 /* Extensions */
647 _mesa_init_extensions( ctx );
648
649 /* Attribute Groups */
650 _mesa_init_accum( ctx );
651 _mesa_init_attrib( ctx );
652 _mesa_init_buffer_objects( ctx );
653 _mesa_init_color( ctx );
654 _mesa_init_current( ctx );
655 _mesa_init_depth( ctx );
656 _mesa_init_display_list( ctx );
657 _mesa_init_eval( ctx );
658 _mesa_init_feedback( ctx );
659 _mesa_init_fog( ctx );
660 _mesa_init_hint( ctx );
661 _mesa_init_line( ctx );
662 _mesa_init_lighting( ctx );
663 _mesa_init_matrix( ctx );
664 _mesa_init_multisample( ctx );
665 _mesa_init_pixel( ctx );
666 _mesa_init_pixelstore( ctx );
667 _mesa_init_point( ctx );
668 _mesa_init_polygon( ctx );
669 _mesa_init_program( ctx );
670 _mesa_init_rastpos( ctx );
671 _mesa_init_scissor( ctx );
672 _mesa_init_shader_state( ctx );
673 _mesa_init_stencil( ctx );
674 _mesa_init_transform( ctx );
675 _mesa_init_varray( ctx );
676 _mesa_init_viewport( ctx );
677
678 if (!_mesa_init_texture( ctx ))
679 return GL_FALSE;
680
681 /* Miscellaneous */
682 ctx->NewState = _NEW_ALL;
683 ctx->ErrorValue = (GLenum) GL_NO_ERROR;
684 ctx->ResetStatus = (GLenum) GL_NO_ERROR;
685 ctx->varying_vp_inputs = VERT_BIT_ALL;
686
687 return GL_TRUE;
688 }
689
690
691 /**
692 * Update default objects in a GL context with respect to shared state.
693 *
694 * \param ctx GL context.
695 *
696 * Removes references to old default objects, (texture objects, program
697 * objects, etc.) and changes to reference those from the current shared
698 * state.
699 */
700 static GLboolean
701 update_default_objects(struct gl_context *ctx)
702 {
703 assert(ctx);
704
705 _mesa_update_default_objects_program(ctx);
706 _mesa_update_default_objects_texture(ctx);
707 _mesa_update_default_objects_buffer_objects(ctx);
708
709 return GL_TRUE;
710 }
711
712
713 /**
714 * This is the default function we plug into all dispatch table slots
715 * This helps prevents a segfault when someone calls a GL function without
716 * first checking if the extension's supported.
717 */
718 static int
719 generic_nop(void)
720 {
721 _mesa_warning(NULL, "User called no-op dispatch function (an unsupported extension function?)");
722 return 0;
723 }
724
725
726 /**
727 * Allocate and initialize a new dispatch table.
728 */
729 struct _glapi_table *
730 _mesa_alloc_dispatch_table(int size)
731 {
732 /* Find the larger of Mesa's dispatch table and libGL's dispatch table.
733 * In practice, this'll be the same for stand-alone Mesa. But for DRI
734 * Mesa we do this to accomodate different versions of libGL and various
735 * DRI drivers.
736 */
737 GLint numEntries = MAX2(_glapi_get_dispatch_table_size(), _gloffset_COUNT);
738 struct _glapi_table *table;
739
740 /* should never happen, but just in case */
741 numEntries = MAX2(numEntries, size);
742
743 table = (struct _glapi_table *) malloc(numEntries * sizeof(_glapi_proc));
744 if (table) {
745 _glapi_proc *entry = (_glapi_proc *) table;
746 GLint i;
747 for (i = 0; i < numEntries; i++) {
748 entry[i] = (_glapi_proc) generic_nop;
749 }
750 }
751 return table;
752 }
753
754
755 /**
756 * Initialize a struct gl_context struct (rendering context).
757 *
758 * This includes allocating all the other structs and arrays which hang off of
759 * the context by pointers.
760 * Note that the driver needs to pass in its dd_function_table here since
761 * we need to at least call driverFunctions->NewTextureObject to create the
762 * default texture objects.
763 *
764 * Called by _mesa_create_context().
765 *
766 * Performs the imports and exports callback tables initialization, and
767 * miscellaneous one-time initializations. If no shared context is supplied one
768 * is allocated, and increase its reference count. Setups the GL API dispatch
769 * tables. Initialize the TNL module. Sets the maximum Z buffer depth.
770 * Finally queries the \c MESA_DEBUG and \c MESA_VERBOSE environment variables
771 * for debug flags.
772 *
773 * \param ctx the context to initialize
774 * \param api the GL API type to create the context for
775 * \param visual describes the visual attributes for this context
776 * \param share_list points to context to share textures, display lists,
777 * etc with, or NULL
778 * \param driverFunctions table of device driver functions for this context
779 * to use
780 * \param driverContext pointer to driver-specific context data
781 */
782 GLboolean
783 _mesa_initialize_context(struct gl_context *ctx,
784 const struct gl_config *visual,
785 struct gl_context *share_list,
786 const struct dd_function_table *driverFunctions,
787 void *driverContext)
788 {
789 struct gl_shared_state *shared;
790
791 /*ASSERT(driverContext);*/
792 assert(driverFunctions->NewTextureObject);
793 assert(driverFunctions->FreeTextureImageBuffer);
794
795 ctx->Visual = *visual;
796 ctx->DrawBuffer = NULL;
797 ctx->ReadBuffer = NULL;
798 ctx->WinSysDrawBuffer = NULL;
799 ctx->WinSysReadBuffer = NULL;
800
801 /* misc one-time initializations */
802 one_time_init(ctx);
803
804 /* Plug in driver functions and context pointer here.
805 * This is important because when we call alloc_shared_state() below
806 * we'll call ctx->Driver.NewTextureObject() to create the default
807 * textures.
808 */
809 ctx->Driver = *driverFunctions;
810 ctx->DriverCtx = driverContext;
811
812 if (share_list) {
813 /* share state with another context */
814 shared = share_list->Shared;
815 }
816 else {
817 /* allocate new, unshared state */
818 shared = _mesa_alloc_shared_state(ctx);
819 if (!shared)
820 return GL_FALSE;
821 }
822
823 _mesa_reference_shared_state(ctx, &ctx->Shared, shared);
824
825 if (!init_attrib_groups( ctx )) {
826 _mesa_reference_shared_state(ctx, &ctx->Shared, NULL);
827 return GL_FALSE;
828 }
829
830 ctx->Exec = _mesa_create_exec_table();
831
832 if (!ctx->Exec) {
833 _mesa_reference_shared_state(ctx, &ctx->Shared, NULL);
834 return GL_FALSE;
835 }
836 ctx->CurrentDispatch = ctx->Exec;
837
838 /* Mesa core handles all the formats that mesa core knows about.
839 * Drivers will want to override this list with just the formats
840 * they can handle, and confirm that appropriate fallbacks exist in
841 * _mesa_choose_tex_format().
842 */
843 memset(&ctx->TextureFormatSupported, GL_TRUE,
844 sizeof(ctx->TextureFormatSupported));
845
846 ctx->Save = _mesa_create_save_table();
847 if (!ctx->Save) {
848 _mesa_reference_shared_state(ctx, &ctx->Shared, NULL);
849 free(ctx->Exec);
850 return GL_FALSE;
851 }
852
853 _mesa_install_save_vtxfmt( ctx, &ctx->ListState.ListVtxfmt );
854
855 ctx->FirstTimeCurrent = GL_TRUE;
856
857 return GL_TRUE;
858 }
859
860
861 /**
862 * Allocate and initialize a struct gl_context structure.
863 * Note that the driver needs to pass in its dd_function_table here since
864 * we need to at least call driverFunctions->NewTextureObject to initialize
865 * the rendering context.
866 *
867 * \param api the GL API type to create the context for
868 * \param visual a struct gl_config pointer (we copy the struct contents)
869 * \param share_list another context to share display lists with or NULL
870 * \param driverFunctions points to the dd_function_table into which the
871 * driver has plugged in all its special functions.
872 * \param driverContext points to the device driver's private context state
873 *
874 * \return pointer to a new __struct gl_contextRec or NULL if error.
875 */
876 struct gl_context *
877 _mesa_create_context(const struct gl_config *visual,
878 struct gl_context *share_list,
879 const struct dd_function_table *driverFunctions,
880 void *driverContext)
881 {
882 struct gl_context *ctx;
883
884 ASSERT(visual);
885 /*ASSERT(driverContext);*/
886
887 ctx = (struct gl_context *) calloc(1, sizeof(struct gl_context));
888 if (!ctx)
889 return NULL;
890
891 if (_mesa_initialize_context(ctx, visual, share_list,
892 driverFunctions, driverContext)) {
893 return ctx;
894 }
895 else {
896 free(ctx);
897 return NULL;
898 }
899 }
900
901
902 /**
903 * Free the data associated with the given context.
904 *
905 * But doesn't free the struct gl_context struct itself.
906 *
907 * \sa _mesa_initialize_context() and init_attrib_groups().
908 */
909 void
910 _mesa_free_context_data( struct gl_context *ctx )
911 {
912 if (!_mesa_get_current_context()){
913 /* No current context, but we may need one in order to delete
914 * texture objs, etc. So temporarily bind the context now.
915 */
916 _mesa_make_current(ctx, NULL, NULL);
917 }
918
919 /* unreference WinSysDraw/Read buffers */
920 _mesa_reference_framebuffer(&ctx->WinSysDrawBuffer, NULL);
921 _mesa_reference_framebuffer(&ctx->WinSysReadBuffer, NULL);
922 _mesa_reference_framebuffer(&ctx->DrawBuffer, NULL);
923 _mesa_reference_framebuffer(&ctx->ReadBuffer, NULL);
924
925 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current, NULL);
926 _mesa_reference_vertprog(ctx, &ctx->VertexProgram._Current, NULL);
927
928 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current, NULL);
929 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._Current, NULL);
930
931 _mesa_free_attrib_data(ctx);
932 _mesa_free_buffer_objects(ctx);
933 _mesa_free_lighting_data( ctx );
934 _mesa_free_eval_data( ctx );
935 _mesa_free_texture_data( ctx );
936 _mesa_free_matrix_data( ctx );
937 _mesa_free_viewport_data( ctx );
938 _mesa_free_program_data(ctx);
939 _mesa_free_shader_state(ctx);
940 _mesa_free_varray_data(ctx);
941
942 _mesa_delete_array_object(ctx, ctx->Array.DefaultArrayObj);
943
944 #if FEATURE_ARB_pixel_buffer_object
945 _mesa_reference_buffer_object(ctx, &ctx->Pack.BufferObj, NULL);
946 _mesa_reference_buffer_object(ctx, &ctx->Unpack.BufferObj, NULL);
947 _mesa_reference_buffer_object(ctx, &ctx->DefaultPacking.BufferObj, NULL);
948 #endif
949
950 _mesa_reference_buffer_object(ctx, &ctx->Array.ArrayBufferObj, NULL);
951
952 /* free dispatch tables */
953 free(ctx->Exec);
954 free(ctx->Save);
955
956 /* Shared context state (display lists, textures, etc) */
957 _mesa_reference_shared_state(ctx, &ctx->Shared, NULL);
958
959 /* needs to be after freeing shared state */
960 _mesa_free_display_list_data(ctx);
961
962 if (ctx->Extensions.String)
963 free((void *) ctx->Extensions.String);
964
965 if (ctx->VersionString)
966 free(ctx->VersionString);
967
968 /* unbind the context if it's currently bound */
969 if (ctx == _mesa_get_current_context()) {
970 _mesa_make_current(NULL, NULL, NULL);
971 }
972 }
973
974
975 /**
976 * Destroy a struct gl_context structure.
977 *
978 * \param ctx GL context.
979 *
980 * Calls _mesa_free_context_data() and frees the gl_context object itself.
981 */
982 void
983 _mesa_destroy_context( struct gl_context *ctx )
984 {
985 if (ctx) {
986 _mesa_free_context_data(ctx);
987 free( (void *) ctx );
988 }
989 }
990
991
992 #if _HAVE_FULL_GL
993 /**
994 * Copy attribute groups from one context to another.
995 *
996 * \param src source context
997 * \param dst destination context
998 * \param mask bitwise OR of GL_*_BIT flags
999 *
1000 * According to the bits specified in \p mask, copies the corresponding
1001 * attributes from \p src into \p dst. For many of the attributes a simple \c
1002 * memcpy is not enough due to the existence of internal pointers in their data
1003 * structures.
1004 */
1005 void
1006 _mesa_copy_context( const struct gl_context *src, struct gl_context *dst,
1007 GLuint mask )
1008 {
1009 if (mask & GL_ACCUM_BUFFER_BIT) {
1010 /* OK to memcpy */
1011 dst->Accum = src->Accum;
1012 }
1013 if (mask & GL_COLOR_BUFFER_BIT) {
1014 /* OK to memcpy */
1015 dst->Color = src->Color;
1016 }
1017 if (mask & GL_CURRENT_BIT) {
1018 /* OK to memcpy */
1019 dst->Current = src->Current;
1020 }
1021 if (mask & GL_DEPTH_BUFFER_BIT) {
1022 /* OK to memcpy */
1023 dst->Depth = src->Depth;
1024 }
1025 if (mask & GL_ENABLE_BIT) {
1026 /* no op */
1027 }
1028 if (mask & GL_EVAL_BIT) {
1029 /* OK to memcpy */
1030 dst->Eval = src->Eval;
1031 }
1032 if (mask & GL_FOG_BIT) {
1033 /* OK to memcpy */
1034 dst->Fog = src->Fog;
1035 }
1036 if (mask & GL_HINT_BIT) {
1037 /* OK to memcpy */
1038 dst->Hint = src->Hint;
1039 }
1040 if (mask & GL_LIGHTING_BIT) {
1041 GLuint i;
1042 /* begin with memcpy */
1043 dst->Light = src->Light;
1044 /* fixup linked lists to prevent pointer insanity */
1045 make_empty_list( &(dst->Light.EnabledList) );
1046 for (i = 0; i < MAX_LIGHTS; i++) {
1047 if (dst->Light.Light[i].Enabled) {
1048 insert_at_tail(&(dst->Light.EnabledList), &(dst->Light.Light[i]));
1049 }
1050 }
1051 }
1052 if (mask & GL_LINE_BIT) {
1053 /* OK to memcpy */
1054 dst->Line = src->Line;
1055 }
1056 if (mask & GL_LIST_BIT) {
1057 /* OK to memcpy */
1058 dst->List = src->List;
1059 }
1060 if (mask & GL_PIXEL_MODE_BIT) {
1061 /* OK to memcpy */
1062 dst->Pixel = src->Pixel;
1063 }
1064 if (mask & GL_POINT_BIT) {
1065 /* OK to memcpy */
1066 dst->Point = src->Point;
1067 }
1068 if (mask & GL_POLYGON_BIT) {
1069 /* OK to memcpy */
1070 dst->Polygon = src->Polygon;
1071 }
1072 if (mask & GL_POLYGON_STIPPLE_BIT) {
1073 /* Use loop instead of memcpy due to problem with Portland Group's
1074 * C compiler. Reported by John Stone.
1075 */
1076 GLuint i;
1077 for (i = 0; i < 32; i++) {
1078 dst->PolygonStipple[i] = src->PolygonStipple[i];
1079 }
1080 }
1081 if (mask & GL_SCISSOR_BIT) {
1082 /* OK to memcpy */
1083 dst->Scissor = src->Scissor;
1084 }
1085 if (mask & GL_STENCIL_BUFFER_BIT) {
1086 /* OK to memcpy */
1087 dst->Stencil = src->Stencil;
1088 }
1089 if (mask & GL_TEXTURE_BIT) {
1090 /* Cannot memcpy because of pointers */
1091 _mesa_copy_texture_state(src, dst);
1092 }
1093 if (mask & GL_TRANSFORM_BIT) {
1094 /* OK to memcpy */
1095 dst->Transform = src->Transform;
1096 }
1097 if (mask & GL_VIEWPORT_BIT) {
1098 /* Cannot use memcpy, because of pointers in GLmatrix _WindowMap */
1099 dst->Viewport.X = src->Viewport.X;
1100 dst->Viewport.Y = src->Viewport.Y;
1101 dst->Viewport.Width = src->Viewport.Width;
1102 dst->Viewport.Height = src->Viewport.Height;
1103 dst->Viewport.Near = src->Viewport.Near;
1104 dst->Viewport.Far = src->Viewport.Far;
1105 _math_matrix_copy(&dst->Viewport._WindowMap, &src->Viewport._WindowMap);
1106 }
1107
1108 /* XXX FIXME: Call callbacks?
1109 */
1110 dst->NewState = _NEW_ALL;
1111 }
1112 #endif
1113
1114
1115 /**
1116 * Check if the given context can render into the given framebuffer
1117 * by checking visual attributes.
1118 *
1119 * Most of these tests could go away because Mesa is now pretty flexible
1120 * in terms of mixing rendering contexts with framebuffers. As long
1121 * as RGB vs. CI mode agree, we're probably good.
1122 *
1123 * \return GL_TRUE if compatible, GL_FALSE otherwise.
1124 */
1125 static GLboolean
1126 check_compatible(const struct gl_context *ctx,
1127 const struct gl_framebuffer *buffer)
1128 {
1129 const struct gl_config *ctxvis = &ctx->Visual;
1130 const struct gl_config *bufvis = &buffer->Visual;
1131
1132 #if 0
1133 /* disabling this fixes the fgl_glxgears pbuffer demo */
1134 if (ctxvis->doubleBufferMode && !bufvis->doubleBufferMode)
1135 return GL_FALSE;
1136 #endif
1137 if (ctxvis->stereoMode && !bufvis->stereoMode)
1138 return GL_FALSE;
1139 if (ctxvis->haveAccumBuffer && !bufvis->haveAccumBuffer)
1140 return GL_FALSE;
1141 if (ctxvis->haveDepthBuffer && !bufvis->haveDepthBuffer)
1142 return GL_FALSE;
1143 if (ctxvis->haveStencilBuffer && !bufvis->haveStencilBuffer)
1144 return GL_FALSE;
1145 if (ctxvis->redMask && ctxvis->redMask != bufvis->redMask)
1146 return GL_FALSE;
1147 if (ctxvis->greenMask && ctxvis->greenMask != bufvis->greenMask)
1148 return GL_FALSE;
1149 if (ctxvis->blueMask && ctxvis->blueMask != bufvis->blueMask)
1150 return GL_FALSE;
1151 #if 0
1152 /* disabled (see bug 11161) */
1153 if (ctxvis->depthBits && ctxvis->depthBits != bufvis->depthBits)
1154 return GL_FALSE;
1155 #endif
1156 if (ctxvis->stencilBits && ctxvis->stencilBits != bufvis->stencilBits)
1157 return GL_FALSE;
1158
1159 return GL_TRUE;
1160 }
1161
1162
1163 /**
1164 * Do one-time initialization for the given framebuffer. Specifically,
1165 * ask the driver for the window's current size and update the framebuffer
1166 * object to match.
1167 * Really, the device driver should totally take care of this.
1168 */
1169 static void
1170 initialize_framebuffer_size(struct gl_context *ctx, struct gl_framebuffer *fb)
1171 {
1172 GLuint width, height;
1173 if (ctx->Driver.GetBufferSize) {
1174 ctx->Driver.GetBufferSize(fb, &width, &height);
1175 if (ctx->Driver.ResizeBuffers)
1176 ctx->Driver.ResizeBuffers(ctx, fb, width, height);
1177 fb->Initialized = GL_TRUE;
1178 }
1179 }
1180
1181
1182 /**
1183 * Check if the viewport/scissor size has not yet been initialized.
1184 * Initialize the size if the given width and height are non-zero.
1185 */
1186 void
1187 _mesa_check_init_viewport(struct gl_context *ctx, GLuint width, GLuint height)
1188 {
1189 if (!ctx->ViewportInitialized && width > 0 && height > 0) {
1190 /* Note: set flag here, before calling _mesa_set_viewport(), to prevent
1191 * potential infinite recursion.
1192 */
1193 ctx->ViewportInitialized = GL_TRUE;
1194 _mesa_set_viewport(ctx, 0, 0, width, height);
1195 _mesa_set_scissor(ctx, 0, 0, width, height);
1196 }
1197 }
1198
1199
1200 /**
1201 * Bind the given context to the given drawBuffer and readBuffer and
1202 * make it the current context for the calling thread.
1203 * We'll render into the drawBuffer and read pixels from the
1204 * readBuffer (i.e. glRead/CopyPixels, glCopyTexImage, etc).
1205 *
1206 * We check that the context's and framebuffer's visuals are compatible
1207 * and return immediately if they're not.
1208 *
1209 * \param newCtx the new GL context. If NULL then there will be no current GL
1210 * context.
1211 * \param drawBuffer the drawing framebuffer
1212 * \param readBuffer the reading framebuffer
1213 */
1214 GLboolean
1215 _mesa_make_current( struct gl_context *newCtx,
1216 struct gl_framebuffer *drawBuffer,
1217 struct gl_framebuffer *readBuffer )
1218 {
1219 GET_CURRENT_CONTEXT(curCtx);
1220
1221 if (MESA_VERBOSE & VERBOSE_API)
1222 _mesa_debug(newCtx, "_mesa_make_current()\n");
1223
1224 /* Check that the context's and framebuffer's visuals are compatible.
1225 */
1226 if (newCtx && drawBuffer && newCtx->WinSysDrawBuffer != drawBuffer) {
1227 if (!check_compatible(newCtx, drawBuffer)) {
1228 _mesa_warning(newCtx,
1229 "MakeCurrent: incompatible visuals for context and drawbuffer");
1230 return GL_FALSE;
1231 }
1232 }
1233 if (newCtx && readBuffer && newCtx->WinSysReadBuffer != readBuffer) {
1234 if (!check_compatible(newCtx, readBuffer)) {
1235 _mesa_warning(newCtx,
1236 "MakeCurrent: incompatible visuals for context and readbuffer");
1237 return GL_FALSE;
1238 }
1239 }
1240
1241 if (curCtx &&
1242 (curCtx->WinSysDrawBuffer || curCtx->WinSysReadBuffer) &&
1243 /* make sure this context is valid for flushing */
1244 curCtx != newCtx)
1245 _mesa_flush(curCtx);
1246
1247 /* We used to call _glapi_check_multithread() here. Now do it in drivers */
1248 _glapi_set_context((void *) newCtx);
1249 ASSERT(_mesa_get_current_context() == newCtx);
1250
1251 if (!newCtx) {
1252 _glapi_set_dispatch(NULL); /* none current */
1253 }
1254 else {
1255 _glapi_set_dispatch(newCtx->CurrentDispatch);
1256
1257 if (drawBuffer && readBuffer) {
1258 ASSERT(drawBuffer->Name == 0);
1259 ASSERT(readBuffer->Name == 0);
1260 _mesa_reference_framebuffer(&newCtx->WinSysDrawBuffer, drawBuffer);
1261 _mesa_reference_framebuffer(&newCtx->WinSysReadBuffer, readBuffer);
1262
1263 /*
1264 * Only set the context's Draw/ReadBuffer fields if they're NULL
1265 * or not bound to a user-created FBO.
1266 */
1267 _mesa_reference_framebuffer(&newCtx->DrawBuffer, drawBuffer);
1268 /* Update the FBO's list of drawbuffers/renderbuffers.
1269 * For winsys FBOs this comes from the GL state (which may have
1270 * changed since the last time this FBO was bound).
1271 */
1272 _mesa_update_draw_buffer(newCtx);
1273
1274 _mesa_reference_framebuffer(&newCtx->ReadBuffer, readBuffer);
1275
1276 /* XXX only set this flag if we're really changing the draw/read
1277 * framebuffer bindings.
1278 */
1279 newCtx->NewState |= _NEW_BUFFERS;
1280
1281 #if 1
1282 /* We want to get rid of these lines: */
1283
1284 #if _HAVE_FULL_GL
1285 if (!drawBuffer->Initialized) {
1286 initialize_framebuffer_size(newCtx, drawBuffer);
1287 }
1288 if (readBuffer != drawBuffer && !readBuffer->Initialized) {
1289 initialize_framebuffer_size(newCtx, readBuffer);
1290 }
1291
1292 _mesa_resizebuffers(newCtx);
1293 #endif
1294
1295 #else
1296 /* We want the drawBuffer and readBuffer to be initialized by
1297 * the driver.
1298 * This generally means the Width and Height match the actual
1299 * window size and the renderbuffers (both hardware and software
1300 * based) are allocated to match. The later can generally be
1301 * done with a call to _mesa_resize_framebuffer().
1302 *
1303 * It's theoretically possible for a buffer to have zero width
1304 * or height, but for now, assert check that the driver did what's
1305 * expected of it.
1306 */
1307 ASSERT(drawBuffer->Width > 0);
1308 ASSERT(drawBuffer->Height > 0);
1309 #endif
1310
1311 if (drawBuffer) {
1312 _mesa_check_init_viewport(newCtx,
1313 drawBuffer->Width, drawBuffer->Height);
1314 }
1315 }
1316
1317 if (newCtx->FirstTimeCurrent) {
1318 _mesa_compute_version(newCtx);
1319
1320 newCtx->Extensions.String = _mesa_make_extension_string(newCtx);
1321
1322 check_context_limits(newCtx);
1323
1324 newCtx->FirstTimeCurrent = GL_FALSE;
1325 }
1326 }
1327
1328 return GL_TRUE;
1329 }
1330
1331
1332 /**
1333 * Make context 'ctx' share the display lists, textures and programs
1334 * that are associated with 'ctxToShare'.
1335 * Any display lists, textures or programs associated with 'ctx' will
1336 * be deleted if nobody else is sharing them.
1337 */
1338 GLboolean
1339 _mesa_share_state(struct gl_context *ctx, struct gl_context *ctxToShare)
1340 {
1341 if (ctx && ctxToShare && ctx->Shared && ctxToShare->Shared) {
1342 struct gl_shared_state *oldShared = NULL;
1343
1344 /* save ref to old state to prevent it from being deleted immediately */
1345 _mesa_reference_shared_state(ctx, &oldShared, ctx->Shared);
1346
1347 /* update ctx's Shared pointer */
1348 _mesa_reference_shared_state(ctx, &ctx->Shared, ctxToShare->Shared);
1349
1350 update_default_objects(ctx);
1351
1352 /* release the old shared state */
1353 _mesa_reference_shared_state(ctx, &oldShared, NULL);
1354
1355 return GL_TRUE;
1356 }
1357 else {
1358 return GL_FALSE;
1359 }
1360 }
1361
1362
1363
1364 /**
1365 * \return pointer to the current GL context for this thread.
1366 *
1367 * Calls _glapi_get_context(). This isn't the fastest way to get the current
1368 * context. If you need speed, see the #GET_CURRENT_CONTEXT macro in
1369 * context.h.
1370 */
1371 struct gl_context *
1372 _mesa_get_current_context( void )
1373 {
1374 return (struct gl_context *) _glapi_get_context();
1375 }
1376
1377
1378 /**
1379 * Get context's current API dispatch table.
1380 *
1381 * It'll either be the immediate-mode execute dispatcher or the display list
1382 * compile dispatcher.
1383 *
1384 * \param ctx GL context.
1385 *
1386 * \return pointer to dispatch_table.
1387 *
1388 * Simply returns __struct gl_contextRec::CurrentDispatch.
1389 */
1390 struct _glapi_table *
1391 _mesa_get_dispatch(struct gl_context *ctx)
1392 {
1393 return ctx->CurrentDispatch;
1394 }
1395
1396 /*@}*/
1397
1398
1399 /**********************************************************************/
1400 /** \name Miscellaneous functions */
1401 /**********************************************************************/
1402 /*@{*/
1403
1404 /**
1405 * Record an error.
1406 *
1407 * \param ctx GL context.
1408 * \param error error code.
1409 *
1410 * Records the given error code and call the driver's dd_function_table::Error
1411 * function if defined.
1412 *
1413 * \sa
1414 * This is called via _mesa_error().
1415 */
1416 void
1417 _mesa_record_error(struct gl_context *ctx, GLenum error)
1418 {
1419 if (!ctx)
1420 return;
1421
1422 if (ctx->ErrorValue == GL_NO_ERROR) {
1423 ctx->ErrorValue = error;
1424 }
1425
1426 /* Call device driver's error handler, if any. This is used on the Mac. */
1427 if (ctx->Driver.Error) {
1428 ctx->Driver.Error(ctx);
1429 }
1430 }
1431
1432
1433 /**
1434 * Flush commands and wait for completion.
1435 */
1436 void
1437 _mesa_finish(struct gl_context *ctx)
1438 {
1439 FLUSH_CURRENT( ctx, 0 );
1440 if (ctx->Driver.Finish) {
1441 ctx->Driver.Finish(ctx);
1442 }
1443 }
1444
1445
1446 /**
1447 * Flush commands.
1448 */
1449 void
1450 _mesa_flush(struct gl_context *ctx)
1451 {
1452 FLUSH_CURRENT( ctx, 0 );
1453 if (ctx->Driver.Flush) {
1454 ctx->Driver.Flush(ctx);
1455 }
1456 }
1457
1458
1459
1460 /**
1461 * Execute glFinish().
1462 *
1463 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
1464 * dd_function_table::Finish driver callback, if not NULL.
1465 */
1466 void GLAPIENTRY
1467 _mesa_Finish(void)
1468 {
1469 GET_CURRENT_CONTEXT(ctx);
1470 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1471 _mesa_finish(ctx);
1472 }
1473
1474
1475 /**
1476 * Execute glFlush().
1477 *
1478 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
1479 * dd_function_table::Flush driver callback, if not NULL.
1480 */
1481 void GLAPIENTRY
1482 _mesa_Flush(void)
1483 {
1484 GET_CURRENT_CONTEXT(ctx);
1485 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1486 _mesa_flush(ctx);
1487 }
1488
1489
1490 /**
1491 * Set mvp_with_dp4 flag. If a driver has a preference for DP4 over
1492 * MUL/MAD, or vice versa, call this function to register that.
1493 * Otherwise we default to MUL/MAD.
1494 */
1495 void
1496 _mesa_set_mvp_with_dp4( struct gl_context *ctx,
1497 GLboolean flag )
1498 {
1499 ctx->mvp_with_dp4 = flag;
1500 }
1501
1502
1503
1504 /**
1505 * Prior to drawing anything with glBegin, glDrawArrays, etc. this function
1506 * is called to see if it's valid to render. This involves checking that
1507 * the current shader is valid and the framebuffer is complete.
1508 * If an error is detected it'll be recorded here.
1509 * \return GL_TRUE if OK to render, GL_FALSE if not
1510 */
1511 GLboolean
1512 _mesa_valid_to_render(struct gl_context *ctx, const char *where)
1513 {
1514 bool vert_from_glsl_shader = false;
1515 bool frag_from_glsl_shader = false;
1516
1517 /* This depends on having up to date derived state (shaders) */
1518 if (ctx->NewState)
1519 _mesa_update_state(ctx);
1520
1521 if (ctx->Shader.CurrentVertexProgram) {
1522 vert_from_glsl_shader = true;
1523
1524 if (!ctx->Shader.CurrentVertexProgram->LinkStatus) {
1525 _mesa_error(ctx, GL_INVALID_OPERATION,
1526 "%s(shader not linked)", where);
1527 return GL_FALSE;
1528 }
1529 #if 0 /* not normally enabled */
1530 {
1531 char errMsg[100];
1532 if (!_mesa_validate_shader_program(ctx,
1533 ctx->Shader.CurrentVertexProgram,
1534 errMsg)) {
1535 _mesa_warning(ctx, "Shader program %u is invalid: %s",
1536 ctx->Shader.CurrentVertexProgram->Name, errMsg);
1537 }
1538 }
1539 #endif
1540 }
1541
1542 if (ctx->Shader.CurrentFragmentProgram) {
1543 frag_from_glsl_shader = true;
1544
1545 if (!ctx->Shader.CurrentFragmentProgram->LinkStatus) {
1546 _mesa_error(ctx, GL_INVALID_OPERATION,
1547 "%s(shader not linked)", where);
1548 return GL_FALSE;
1549 }
1550 #if 0 /* not normally enabled */
1551 {
1552 char errMsg[100];
1553 if (!_mesa_validate_shader_program(ctx,
1554 ctx->Shader.CurrentFragmentProgram,
1555 errMsg)) {
1556 _mesa_warning(ctx, "Shader program %u is invalid: %s",
1557 ctx->Shader.CurrentFragmentProgram->Name, errMsg);
1558 }
1559 }
1560 #endif
1561 }
1562
1563 /* Any shader stages that are not supplied by the GLSL shader and have
1564 * assembly shaders enabled must now be validated.
1565 */
1566 if (!vert_from_glsl_shader
1567 && ctx->VertexProgram.Enabled && !ctx->VertexProgram._Enabled) {
1568 _mesa_error(ctx, GL_INVALID_OPERATION,
1569 "%s(vertex program not valid)", where);
1570 return GL_FALSE;
1571 }
1572
1573 if (!frag_from_glsl_shader) {
1574 if (ctx->FragmentProgram.Enabled && !ctx->FragmentProgram._Enabled) {
1575 _mesa_error(ctx, GL_INVALID_OPERATION,
1576 "%s(fragment program not valid)", where);
1577 return GL_FALSE;
1578 }
1579
1580 /* If drawing to integer-valued color buffers, there must be an
1581 * active fragment shader (GL_EXT_texture_integer).
1582 */
1583 if (ctx->DrawBuffer && ctx->DrawBuffer->_IntegerColor) {
1584 _mesa_error(ctx, GL_INVALID_OPERATION,
1585 "%s(integer format but no fragment shader)", where);
1586 return GL_FALSE;
1587 }
1588 }
1589
1590 #ifdef DEBUG
1591 if (ctx->Shader.Flags & GLSL_LOG) {
1592 struct gl_shader_program *shProg[MESA_SHADER_TYPES];
1593 gl_shader_type i;
1594
1595 shProg[MESA_SHADER_VERTEX] = ctx->Shader.CurrentVertexProgram;
1596 shProg[MESA_SHADER_FRAGMENT] = ctx->Shader.CurrentFragmentProgram;
1597
1598 for (i = 0; i < MESA_SHADER_TYPES; i++) {
1599 if (shProg[i] == NULL || shProg[i]->_Used
1600 || shProg[i]->_LinkedShaders[i] == NULL)
1601 continue;
1602
1603 /* This is the first time this shader is being used.
1604 * Append shader's constants/uniforms to log file.
1605 *
1606 * Only log data for the program target that matches the shader
1607 * target. It's possible to have a program bound to the vertex
1608 * shader target that also supplied a fragment shader. If that
1609 * program isn't also bound to the fragment shader target we don't
1610 * want to log its fragment data.
1611 */
1612 _mesa_append_uniforms_to_file(shProg[i]->_LinkedShaders[i]);
1613 }
1614
1615 for (i = 0; i < MESA_SHADER_TYPES; i++) {
1616 if (shProg[i] != NULL)
1617 shProg[i]->_Used = GL_TRUE;
1618 }
1619 }
1620 #endif
1621
1622 return GL_TRUE;
1623 }
1624
1625
1626 /*@}*/