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