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