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