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