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