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