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