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