1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
5 * Copyright (C) 2008 VMware, Inc. All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * 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 "util/glheader.h"
80
81 #include "accum.h"
82 #include "arrayobj.h"
83 #include "attrib.h"
84 #include "bbox.h"
85 #include "blend.h"
86 #include "buffers.h"
87 #include "bufferobj.h"
88 #include "conservativeraster.h"
89 #include "context.h"
90 #include "debug.h"
91 #include "debug_output.h"
92 #include "depth.h"
93 #include "dlist.h"
94 #include "draw_validate.h"
95 #include "eval.h"
96 #include "extensions.h"
97 #include "fbobject.h"
98 #include "feedback.h"
99 #include "fog.h"
100 #include "formats.h"
101 #include "framebuffer.h"
102 #include "glthread.h"
103 #include "hint.h"
104 #include "hash.h"
105 #include "light.h"
106 #include "lines.h"
107 #include "macros.h"
108 #include "matrix.h"
109 #include "multisample.h"
110 #include "performance_monitor.h"
111 #include "performance_query.h"
112 #include "pipelineobj.h"
113 #include "pixel.h"
114 #include "pixelstore.h"
115 #include "points.h"
116 #include "polygon.h"
117 #include "queryobj.h"
118 #include "syncobj.h"
119 #include "rastpos.h"
120 #include "remap.h"
121 #include "scissor.h"
122 #include "shared.h"
123 #include "shaderobj.h"
124 #include "shaderimage.h"
125 #include "state.h"
126 #include "util/u_debug.h"
127 #include "util/disk_cache.h"
128 #include "util/strtod.h"
129 #include "util/u_call_once.h"
130 #include "stencil.h"
131 #include "shaderimage.h"
132 #include "texcompress_s3tc.h"
133 #include "texstate.h"
134 #include "transformfeedback.h"
135 #include "mtypes.h"
136 #include "varray.h"
137 #include "version.h"
138 #include "viewport.h"
139 #include "texturebindless.h"
140 #include "program/program.h"
141 #include "math/m_matrix.h"
142 #include "main/dispatch.h" /* for _gloffset_COUNT */
143 #include "macros.h"
144 #include "git_sha1.h"
145
146 #include "compiler/glsl_types.h"
147 #include "compiler/glsl/builtin_functions.h"
148 #include "compiler/glsl/glsl_parser_extras.h"
149 #include <stdbool.h>
150 #include "util/u_memory.h"
151 #include "api_exec_decl.h"
152
153 #include "state_tracker/st_cb_texture.h"
154 #include "state_tracker/st_cb_flush.h"
155
156 #ifndef MESA_VERBOSE
157 int MESA_VERBOSE = 0;
158 #endif
159
160 #ifndef MESA_DEBUG_FLAGS
161 int MESA_DEBUG_FLAGS = 0;
162 #endif
163
164
165 /* ubyte -> float conversion */
166 GLfloat _mesa_ubyte_to_float_color_tab[256];
167
168
169 /**********************************************************************/
170 /** \name Context allocation, initialization, destroying
171 *
172 * The purpose of the most initialization functions here is to provide the
173 * default state values according to the OpenGL specification.
174 */
175 /**********************************************************************/
176 /*@{*/
177
178
179 /**
180 * Calls all the various one-time-fini functions in Mesa
181 */
182
183 static void
one_time_fini(void)184 one_time_fini(void)
185 {
186 glsl_type_singleton_decref();
187 }
188
189 /**
190 * Calls all the various one-time-init functions in Mesa
191 */
192
193 static void
one_time_init(const char * extensions_override)194 one_time_init(const char *extensions_override)
195 {
196 GLuint i;
197
198 STATIC_ASSERT(sizeof(GLbyte) == 1);
199 STATIC_ASSERT(sizeof(GLubyte) == 1);
200 STATIC_ASSERT(sizeof(GLshort) == 2);
201 STATIC_ASSERT(sizeof(GLushort) == 2);
202 STATIC_ASSERT(sizeof(GLint) == 4);
203 STATIC_ASSERT(sizeof(GLuint) == 4);
204
205 const char *env_const = os_get_option("MESA_EXTENSION_OVERRIDE");
206 if (env_const) {
207 if (extensions_override &&
208 strcmp(extensions_override, env_const)) {
209 printf("Warning: MESA_EXTENSION_OVERRIDE used instead of driconf setting\n");
210 }
211 extensions_override = env_const;
212 }
213
214 _mesa_one_time_init_extension_overrides(extensions_override);
215
216
217 for (i = 0; i < 256; i++) {
218 _mesa_ubyte_to_float_color_tab[i] = (float) i / 255.0F;
219 }
220
221 atexit(one_time_fini);
222
223 #if MESA_DEBUG
224 if (MESA_VERBOSE != 0) {
225 _mesa_debug(NULL, "Mesa " PACKAGE_VERSION " DEBUG build" MESA_GIT_SHA1 "\n");
226 }
227 #endif
228
229 /* Take a glsl type reference for the duration of libGL's life to avoid
230 * unecessary creation/destruction of glsl types.
231 */
232 glsl_type_singleton_init_or_ref();
233
234 _mesa_init_remap_table();
235 }
236
237 /**
238 * Calls all the various one-time-init functions in Mesa.
239 *
240 * While holding a global mutex lock, calls several initialization functions,
241 * and sets the glapi callbacks if the \c MESA_DEBUG environment variable is
242 * defined.
243 */
244 void
_mesa_initialize(const char * extensions_override)245 _mesa_initialize(const char *extensions_override)
246 {
247 static util_once_flag once = UTIL_ONCE_FLAG_INIT;
248 util_call_once_data(&once,
249 (util_call_once_data_func)one_time_init, extensions_override);
250 }
251
252
253 /**
254 * Initialize fields of gl_current_attrib (aka ctx->Current.*)
255 */
256 static void
_mesa_init_current(struct gl_context * ctx)257 _mesa_init_current(struct gl_context *ctx)
258 {
259 GLuint i;
260
261 /* Init all to (0,0,0,1) */
262 for (i = 0; i < ARRAY_SIZE(ctx->Current.Attrib); i++) {
263 ASSIGN_4V( ctx->Current.Attrib[i], 0.0, 0.0, 0.0, 1.0 );
264 }
265
266 /* redo special cases: */
267 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_NORMAL], 0.0, 0.0, 1.0, 1.0 );
268 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR0], 1.0, 1.0, 1.0, 1.0 );
269 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR1], 0.0, 0.0, 0.0, 1.0 );
270 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR_INDEX], 1.0, 0.0, 0.0, 1.0 );
271 ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_EDGEFLAG], 1.0, 0.0, 0.0, 1.0 );
272 }
273
274
275 /**
276 * Init vertex/fragment/geometry program limits.
277 * Important: drivers should override these with actual limits.
278 */
279 static void
init_program_limits(struct gl_constants * consts,gl_shader_stage stage,struct gl_program_constants * prog)280 init_program_limits(struct gl_constants *consts, gl_shader_stage stage,
281 struct gl_program_constants *prog)
282 {
283 prog->MaxInstructions = MAX_PROGRAM_INSTRUCTIONS;
284 prog->MaxAluInstructions = MAX_PROGRAM_INSTRUCTIONS;
285 prog->MaxTexInstructions = MAX_PROGRAM_INSTRUCTIONS;
286 prog->MaxTexIndirections = MAX_PROGRAM_INSTRUCTIONS;
287 prog->MaxTemps = MAX_PROGRAM_TEMPS;
288 prog->MaxEnvParams = MAX_PROGRAM_ENV_PARAMS;
289 prog->MaxLocalParams = MAX_PROGRAM_LOCAL_PARAMS;
290 prog->MaxAddressOffset = MAX_PROGRAM_LOCAL_PARAMS;
291 prog->MaxAddressRegs = 0; /* only meaningful for vertex/fragment shaders */
292
293 switch (stage) {
294 case MESA_SHADER_VERTEX:
295 prog->MaxParameters = MAX_VERTEX_PROGRAM_PARAMS;
296 prog->MaxAttribs = MAX_VERTEX_GENERIC_ATTRIBS;
297 prog->MaxAddressRegs = MAX_VERTEX_PROGRAM_ADDRESS_REGS;
298 prog->MaxUniformComponents = 4 * MAX_UNIFORMS;
299 prog->MaxInputComponents = 0; /* value not used */
300 prog->MaxOutputComponents = 16 * 4; /* old limit not to break tnl and swrast */
301 break;
302 case MESA_SHADER_FRAGMENT:
303 prog->MaxParameters = MAX_FRAGMENT_PROGRAM_PARAMS;
304 prog->MaxAttribs = MAX_FRAGMENT_PROGRAM_INPUTS;
305 prog->MaxAddressRegs = MAX_FRAGMENT_PROGRAM_ADDRESS_REGS;
306 prog->MaxUniformComponents = 4 * MAX_UNIFORMS;
307 prog->MaxInputComponents = 16 * 4; /* old limit not to break tnl and swrast */
308 prog->MaxOutputComponents = 0; /* value not used */
309 break;
310 case MESA_SHADER_TESS_CTRL:
311 case MESA_SHADER_TESS_EVAL:
312 case MESA_SHADER_GEOMETRY:
313 prog->MaxParameters = MAX_VERTEX_PROGRAM_PARAMS;
314 prog->MaxAttribs = MAX_VERTEX_GENERIC_ATTRIBS;
315 prog->MaxUniformComponents = 4 * MAX_UNIFORMS;
316 prog->MaxInputComponents = 16 * 4; /* old limit not to break tnl and swrast */
317 prog->MaxOutputComponents = 16 * 4; /* old limit not to break tnl and swrast */
318 break;
319 case MESA_SHADER_COMPUTE:
320 prog->MaxParameters = 0; /* not meaningful for compute shaders */
321 prog->MaxAttribs = 0; /* not meaningful for compute shaders */
322 prog->MaxUniformComponents = 4 * MAX_UNIFORMS;
323 prog->MaxInputComponents = 0; /* not meaningful for compute shaders */
324 prog->MaxOutputComponents = 0; /* not meaningful for compute shaders */
325 break;
326 default:
327 assert(0 && "Bad shader stage in init_program_limits()");
328 }
329
330 /* Set GLSL datatype range/precision info assuming IEEE float values.
331 * Drivers should override these defaults as needed.
332 */
333 prog->MediumFloat.RangeMin = 127;
334 prog->MediumFloat.RangeMax = 127;
335 prog->MediumFloat.Precision = 23;
336 prog->LowFloat = prog->HighFloat = prog->MediumFloat;
337
338 /* Assume ints are stored as floats for now, since this is the least-common
339 * denominator. The OpenGL ES spec implies (page 132) that the precision
340 * of integer types should be 0. Practically speaking, IEEE
341 * single-precision floating point values can only store integers in the
342 * range [-0x01000000, 0x01000000] without loss of precision.
343 */
344 prog->MediumInt.RangeMin = 24;
345 prog->MediumInt.RangeMax = 24;
346 prog->MediumInt.Precision = 0;
347 prog->LowInt = prog->HighInt = prog->MediumInt;
348
349 prog->MaxUniformBlocks = 12;
350 prog->MaxCombinedUniformComponents = (prog->MaxUniformComponents +
351 consts->MaxUniformBlockSize / 4 *
352 prog->MaxUniformBlocks);
353
354 prog->MaxAtomicBuffers = 0;
355 prog->MaxAtomicCounters = 0;
356
357 prog->MaxShaderStorageBlocks = 8;
358 }
359
360
361 /**
362 * Initialize fields of gl_constants (aka ctx->Const.*).
363 * Use defaults from config.h. The device drivers will often override
364 * some of these values (such as number of texture units).
365 */
366 void
_mesa_init_constants(struct gl_constants * consts,gl_api api)367 _mesa_init_constants(struct gl_constants *consts, gl_api api)
368 {
369 int i;
370 assert(consts);
371
372 /* Constants, may be overriden (usually only reduced) by device drivers */
373 consts->MaxTextureMbytes = MAX_TEXTURE_MBYTES;
374 consts->MaxTextureSize = 1 << (MAX_TEXTURE_LEVELS - 1);
375 consts->Max3DTextureLevels = MAX_TEXTURE_LEVELS;
376 consts->MaxCubeTextureLevels = MAX_TEXTURE_LEVELS;
377 consts->MaxTextureRectSize = MAX_TEXTURE_RECT_SIZE;
378 consts->MaxArrayTextureLayers = MAX_ARRAY_TEXTURE_LAYERS;
379 consts->MaxTextureCoordUnits = MAX_TEXTURE_COORD_UNITS;
380 consts->Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = MAX_TEXTURE_IMAGE_UNITS;
381 consts->MaxTextureUnits = MIN2(consts->MaxTextureCoordUnits,
382 consts->Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits);
383 consts->MaxTextureMaxAnisotropy = MAX_TEXTURE_MAX_ANISOTROPY;
384 consts->MaxTextureLodBias = MAX_TEXTURE_LOD_BIAS;
385 consts->MaxTextureBufferSize = 65536;
386 consts->TextureBufferOffsetAlignment = 1;
387 consts->MaxArrayLockSize = MAX_ARRAY_LOCK_SIZE;
388 consts->SubPixelBits = SUB_PIXEL_BITS;
389 consts->MinPointSize = MIN_POINT_SIZE;
390 consts->MaxPointSize = MAX_POINT_SIZE;
391 consts->MinPointSizeAA = MIN_POINT_SIZE;
392 consts->MaxPointSizeAA = MAX_POINT_SIZE;
393 consts->PointSizeGranularity = (GLfloat) POINT_SIZE_GRANULARITY;
394 consts->MinLineWidth = MIN_LINE_WIDTH;
395 consts->MaxLineWidth = MAX_LINE_WIDTH;
396 consts->MinLineWidthAA = MIN_LINE_WIDTH;
397 consts->MaxLineWidthAA = MAX_LINE_WIDTH;
398 consts->LineWidthGranularity = (GLfloat) LINE_WIDTH_GRANULARITY;
399 consts->MaxClipPlanes = 6;
400 consts->MaxLights = MAX_LIGHTS;
401 consts->MaxShininess = 128.0;
402 consts->MaxSpotExponent = 128.0;
403 consts->MaxViewportWidth = 16384;
404 consts->MaxViewportHeight = 16384;
405 consts->MinMapBufferAlignment = 64;
406
407 /* Driver must override these values if ARB_viewport_array is supported. */
408 consts->MaxViewports = 1;
409 consts->ViewportSubpixelBits = 0;
410 consts->ViewportBounds.Min = 0;
411 consts->ViewportBounds.Max = 0;
412
413 /** GL_ARB_uniform_buffer_object */
414 consts->MaxCombinedUniformBlocks = 36;
415 consts->MaxUniformBufferBindings = 36;
416 consts->MaxUniformBlockSize = 16384;
417 consts->UniformBufferOffsetAlignment = 1;
418
419 /** GL_ARB_shader_storage_buffer_object */
420 consts->MaxCombinedShaderStorageBlocks = 8;
421 consts->MaxShaderStorageBufferBindings = 8;
422 consts->MaxShaderStorageBlockSize = 128 * 1024 * 1024; /* 2^27 */
423 consts->ShaderStorageBufferOffsetAlignment = 256;
424
425 /* GL_ARB_explicit_uniform_location, GL_MAX_UNIFORM_LOCATIONS */
426 consts->MaxUserAssignableUniformLocations =
427 4 * MESA_SHADER_STAGES * MAX_UNIFORMS;
428
429 for (i = 0; i < MESA_SHADER_STAGES; i++)
430 init_program_limits(consts, i, &consts->Program[i]);
431
432 consts->MaxProgramMatrices = MAX_PROGRAM_MATRICES;
433 consts->MaxProgramMatrixStackDepth = MAX_PROGRAM_MATRIX_STACK_DEPTH;
434
435 /* Set the absolute minimum possible GLSL version. API_OPENGL_CORE can
436 * mean an OpenGL 3.0 forward-compatible context, so that implies a minimum
437 * possible version of 1.30. Otherwise, the minimum possible version 1.20.
438 * Since Mesa unconditionally advertises GL_ARB_shading_language_100 and
439 * GL_ARB_shader_objects, every driver has GLSL 1.20... even if they don't
440 * advertise any extensions to enable any shader stages (e.g.,
441 * GL_ARB_vertex_shader).
442 */
443 consts->GLSLVersion = api == API_OPENGL_CORE ? 130 : 120;
444 consts->GLSLVersionCompat = consts->GLSLVersion;
445
446 consts->GLSLLowerConstArrays = true;
447
448 /* GL_ARB_draw_buffers */
449 consts->MaxDrawBuffers = MAX_DRAW_BUFFERS;
450
451 consts->MaxColorAttachments = MAX_COLOR_ATTACHMENTS;
452 consts->MaxRenderbufferSize = MAX_RENDERBUFFER_SIZE;
453
454 consts->Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = MAX_TEXTURE_IMAGE_UNITS;
455 consts->MaxCombinedTextureImageUnits = MAX_COMBINED_TEXTURE_IMAGE_UNITS;
456 consts->MaxVarying = 16; /* old limit not to break tnl and swrast */
457 consts->Program[MESA_SHADER_GEOMETRY].MaxTextureImageUnits = MAX_TEXTURE_IMAGE_UNITS;
458 consts->MaxGeometryOutputVertices = MAX_GEOMETRY_OUTPUT_VERTICES;
459 consts->MaxGeometryTotalOutputComponents = MAX_GEOMETRY_TOTAL_OUTPUT_COMPONENTS;
460 consts->MaxGeometryShaderInvocations = MAX_GEOMETRY_SHADER_INVOCATIONS;
461
462 #if MESA_DEBUG
463 consts->GenerateTemporaryNames = true;
464 #else
465 consts->GenerateTemporaryNames = false;
466 #endif
467
468 /* GL_ARB_framebuffer_object */
469 consts->MaxSamples = 0;
470
471 /* GLSL default if NativeIntegers == FALSE */
472 consts->UniformBooleanTrue = FLOAT_AS_UNION(1.0f).u;
473
474 /* GL_ARB_sync */
475 consts->MaxServerWaitTimeout = 0x7fffffff7fffffffULL;
476
477 /* GL_EXT_provoking_vertex */
478 consts->QuadsFollowProvokingVertexConvention = GL_TRUE;
479
480 /** GL_ARB_viewport_array */
481 consts->LayerAndVPIndexProvokingVertex = GL_UNDEFINED_VERTEX;
482
483 /* GL_EXT_transform_feedback */
484 consts->MaxTransformFeedbackBuffers = MAX_FEEDBACK_BUFFERS;
485 consts->MaxTransformFeedbackSeparateComponents = 4 * MAX_FEEDBACK_ATTRIBS;
486 consts->MaxTransformFeedbackInterleavedComponents = 4 * MAX_FEEDBACK_ATTRIBS;
487 consts->MaxVertexStreams = 1;
488
489 /* GL 3.2 */
490 consts->ProfileMask = api == API_OPENGL_CORE
491 ? GL_CONTEXT_CORE_PROFILE_BIT
492 : GL_CONTEXT_COMPATIBILITY_PROFILE_BIT;
493
494 /* GL 4.4 */
495 consts->MaxVertexAttribStride = 2048;
496
497 /** GL_EXT_gpu_shader4 */
498 consts->MinProgramTexelOffset = -8;
499 consts->MaxProgramTexelOffset = 7;
500
501 /* GL_ARB_texture_gather */
502 consts->MinProgramTextureGatherOffset = -8;
503 consts->MaxProgramTextureGatherOffset = 7;
504
505 /* GL_ARB_robustness */
506 consts->ResetStrategy = GL_NO_RESET_NOTIFICATION_ARB;
507
508 /* GL_KHR_robustness */
509 consts->RobustAccess = GL_FALSE;
510
511 /* ES 3.0 or ARB_ES3_compatibility */
512 consts->MaxElementIndex = 0xffffffffu;
513
514 /* GL_ARB_texture_multisample */
515 consts->MaxColorTextureSamples = 1;
516 consts->MaxDepthTextureSamples = 1;
517 consts->MaxIntegerSamples = 1;
518
519 /* GL_ARB_shader_atomic_counters */
520 consts->MaxAtomicBufferBindings = MAX_COMBINED_ATOMIC_BUFFERS;
521 consts->MaxAtomicBufferSize = MAX_ATOMIC_COUNTERS * ATOMIC_COUNTER_SIZE;
522 consts->MaxCombinedAtomicBuffers = MAX_COMBINED_ATOMIC_BUFFERS;
523 consts->MaxCombinedAtomicCounters = MAX_ATOMIC_COUNTERS;
524
525 /* GL_ARB_vertex_attrib_binding */
526 consts->MaxVertexAttribRelativeOffset = 2047;
527 consts->MaxVertexAttribBindings = MAX_VERTEX_GENERIC_ATTRIBS;
528
529 /* GL_ARB_compute_shader */
530 consts->MaxComputeWorkGroupCount[0] = 65535;
531 consts->MaxComputeWorkGroupCount[1] = 65535;
532 consts->MaxComputeWorkGroupCount[2] = 65535;
533 consts->MaxComputeWorkGroupSize[0] = 1024;
534 consts->MaxComputeWorkGroupSize[1] = 1024;
535 consts->MaxComputeWorkGroupSize[2] = 64;
536 /* Enables compute support for GLES 3.1 if >= 128 */
537 consts->MaxComputeWorkGroupInvocations = 0;
538
539 /** GL_ARB_gpu_shader5 */
540 consts->MinFragmentInterpolationOffset = MIN_FRAGMENT_INTERPOLATION_OFFSET;
541 consts->MaxFragmentInterpolationOffset = MAX_FRAGMENT_INTERPOLATION_OFFSET;
542
543 /** GL_KHR_context_flush_control */
544 consts->ContextReleaseBehavior = GL_CONTEXT_RELEASE_BEHAVIOR_FLUSH;
545
546 /** GL_ARB_tessellation_shader */
547 consts->MaxTessGenLevel = MAX_TESS_GEN_LEVEL;
548 consts->MaxPatchVertices = MAX_PATCH_VERTICES;
549 consts->Program[MESA_SHADER_TESS_CTRL].MaxTextureImageUnits = MAX_TEXTURE_IMAGE_UNITS;
550 consts->Program[MESA_SHADER_TESS_EVAL].MaxTextureImageUnits = MAX_TEXTURE_IMAGE_UNITS;
551 consts->MaxTessPatchComponents = MAX_TESS_PATCH_COMPONENTS;
552 consts->MaxTessControlTotalOutputComponents = MAX_TESS_CONTROL_TOTAL_OUTPUT_COMPONENTS;
553 consts->PrimitiveRestartForPatches = false;
554
555 /** GL_ARB_compute_variable_group_size */
556 consts->MaxComputeVariableGroupSize[0] = 512;
557 consts->MaxComputeVariableGroupSize[1] = 512;
558 consts->MaxComputeVariableGroupSize[2] = 64;
559 consts->MaxComputeVariableGroupInvocations = 512;
560
561 /** GL_NV_conservative_raster */
562 consts->MaxSubpixelPrecisionBiasBits = 0;
563
564 /** GL_NV_conservative_raster_dilate */
565 consts->ConservativeRasterDilateRange[0] = 0.0;
566 consts->ConservativeRasterDilateRange[1] = 0.0;
567 consts->ConservativeRasterDilateGranularity = 0.0;
568
569 consts->glBeginEndBufferSize = 512 * 1024;
570 }
571
572
573 /**
574 * Do some sanity checks on the limits/constants for the given context.
575 * Only called the first time a context is bound.
576 */
577 static void
check_context_limits(struct gl_context * ctx)578 check_context_limits(struct gl_context *ctx)
579 {
580 (void) ctx;
581
582 /* check that we don't exceed the size of various bitfields */
583 assert(VARYING_SLOT_MAX <=
584 (8 * sizeof(ctx->VertexProgram._Current->info.outputs_written)));
585 assert(VARYING_SLOT_MAX <=
586 (8 * sizeof(ctx->FragmentProgram._Current->info.inputs_read)));
587
588 /* shader-related checks */
589 assert(ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxLocalParams <= MAX_PROGRAM_LOCAL_PARAMS);
590 assert(ctx->Const.Program[MESA_SHADER_VERTEX].MaxLocalParams <= MAX_PROGRAM_LOCAL_PARAMS);
591
592 /* Texture unit checks */
593 assert(ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits > 0);
594 assert(ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits <= MAX_TEXTURE_IMAGE_UNITS);
595 assert(ctx->Const.MaxTextureCoordUnits > 0);
596 assert(ctx->Const.MaxTextureCoordUnits <= MAX_TEXTURE_COORD_UNITS);
597 assert(ctx->Const.MaxTextureUnits > 0);
598 assert(ctx->Const.MaxTextureUnits <= MAX_TEXTURE_IMAGE_UNITS);
599 assert(ctx->Const.MaxTextureUnits <= MAX_TEXTURE_COORD_UNITS);
600 assert(ctx->Const.MaxTextureUnits == MIN2(ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits,
601 ctx->Const.MaxTextureCoordUnits));
602 assert(ctx->Const.MaxCombinedTextureImageUnits > 0);
603 assert(ctx->Const.MaxCombinedTextureImageUnits <= MAX_COMBINED_TEXTURE_IMAGE_UNITS);
604 assert(ctx->Const.MaxTextureCoordUnits <= MAX_COMBINED_TEXTURE_IMAGE_UNITS);
605 /* number of coord units cannot be greater than number of image units */
606 assert(ctx->Const.MaxTextureCoordUnits <= ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits);
607
608
609 /* Texture size checks */
610 assert(ctx->Const.MaxTextureSize <= (1 << (MAX_TEXTURE_LEVELS - 1)));
611 assert(ctx->Const.Max3DTextureLevels <= MAX_TEXTURE_LEVELS);
612 assert(ctx->Const.MaxCubeTextureLevels <= MAX_TEXTURE_LEVELS);
613 assert(ctx->Const.MaxTextureRectSize <= MAX_TEXTURE_RECT_SIZE);
614
615 /* Max texture size should be <= max viewport size (render to texture) */
616 assert(ctx->Const.MaxTextureSize <= ctx->Const.MaxViewportWidth);
617 assert(ctx->Const.MaxTextureSize <= ctx->Const.MaxViewportHeight);
618
619 assert(ctx->Const.MaxDrawBuffers <= MAX_DRAW_BUFFERS);
620
621 /* if this fails, add more enum values to gl_buffer_index */
622 assert(BUFFER_COLOR0 + MAX_DRAW_BUFFERS <= BUFFER_COUNT);
623
624 /* XXX probably add more tests */
625 }
626
627
628 /**
629 * Initialize the attribute groups in a GL context.
630 *
631 * \param ctx GL context.
632 *
633 * Initializes all the attributes, calling the respective <tt>init*</tt>
634 * functions for the more complex data structures.
635 */
636 static GLboolean
init_attrib_groups(struct gl_context * ctx)637 init_attrib_groups(struct gl_context *ctx)
638 {
639 assert(ctx);
640
641 /* Constants */
642 _mesa_init_constants(&ctx->Const, ctx->API);
643
644 /* Extensions */
645 _mesa_init_extensions(&ctx->Extensions);
646
647 /* Attribute Groups */
648 _mesa_init_accum( ctx );
649 _mesa_init_attrib( ctx );
650 _mesa_init_bbox( ctx );
651 _mesa_init_buffer_objects( ctx );
652 _mesa_init_color( ctx );
653 _mesa_init_conservative_raster( ctx );
654 _mesa_init_current( ctx );
655 _mesa_init_depth( ctx );
656 _mesa_init_debug( ctx );
657 _mesa_init_debug_output( ctx );
658 _mesa_init_display_list( ctx );
659 _mesa_init_eval( ctx );
660 _mesa_init_feedback( ctx );
661 _mesa_init_fog( ctx );
662 _mesa_init_hint( ctx );
663 _mesa_init_image_units( ctx );
664 _mesa_init_line( ctx );
665 _mesa_init_lighting( ctx );
666 _mesa_init_matrix( ctx );
667 _mesa_init_multisample( ctx );
668 _mesa_init_performance_monitors( ctx );
669 _mesa_init_performance_queries( ctx );
670 _mesa_init_pipeline( ctx );
671 _mesa_init_pixel( ctx );
672 _mesa_init_pixelstore( ctx );
673 _mesa_init_point( ctx );
674 _mesa_init_polygon( ctx );
675 _mesa_init_varray( ctx ); /* should be before _mesa_init_program */
676 _mesa_init_program( ctx );
677 _mesa_init_queryobj( ctx );
678 _mesa_init_sync( ctx );
679 _mesa_init_rastpos( ctx );
680 _mesa_init_scissor( ctx );
681 _mesa_init_shader_state( ctx );
682 _mesa_init_stencil( ctx );
683 _mesa_init_transform( ctx );
684 _mesa_init_transform_feedback( ctx );
685 _mesa_init_viewport( ctx );
686 _mesa_init_resident_handles( ctx );
687
688 if (!_mesa_init_texture( ctx ))
689 return GL_FALSE;
690
691 /* Miscellaneous */
692 ctx->TileRasterOrderIncreasingX = GL_TRUE;
693 ctx->TileRasterOrderIncreasingY = GL_TRUE;
694 ctx->NewState = _NEW_ALL;
695 ctx->NewDriverState = ST_ALL_STATES_MASK;
696 ctx->ErrorValue = GL_NO_ERROR;
697 ctx->ShareGroupReset = false;
698 ctx->IntelBlackholeRender = debug_get_bool_option("INTEL_BLACKHOLE_DEFAULT", false);
699
700 return GL_TRUE;
701 }
702
703
704 /**
705 * Update default objects in a GL context with respect to shared state.
706 *
707 * \param ctx GL context.
708 *
709 * Removes references to old default objects, (texture objects, program
710 * objects, etc.) and changes to reference those from the current shared
711 * state.
712 */
713 static GLboolean
update_default_objects(struct gl_context * ctx)714 update_default_objects(struct gl_context *ctx)
715 {
716 assert(ctx);
717
718 _mesa_update_default_objects_program(ctx);
719 _mesa_update_default_objects_texture(ctx);
720 _mesa_update_default_objects_buffer_objects(ctx);
721
722 return GL_TRUE;
723 }
724
725
726 /* XXX this is temporary and should be removed at some point in the
727 * future when there's a reasonable expectation that the libGL library
728 * contains the _glapi_new_nop_table() and _glapi_set_nop_handler()
729 * functions which were added in Mesa 10.6.
730 */
731 #if !defined(_WIN32)
732 /* Avoid libGL / driver ABI break */
733 #define USE_GLAPI_NOP_FEATURES 0
734 #else
735 #define USE_GLAPI_NOP_FEATURES 1
736 #endif
737
738
739 /**
740 * This function is called by the glapi no-op functions. For each OpenGL
741 * function/entrypoint there's a simple no-op function. These "no-op"
742 * functions call this function.
743 *
744 * If there's a current OpenGL context for the calling thread, we record a
745 * GL_INVALID_OPERATION error. This can happen either because the app's
746 * calling an unsupported extension function, or calling an illegal function
747 * (such as glClear between glBegin/glEnd).
748 *
749 * If there's no current OpenGL context for the calling thread, we can
750 * print a message to stderr.
751 *
752 * \param name the name of the OpenGL function
753 */
754 #if USE_GLAPI_NOP_FEATURES
755 static void
nop_handler(const char * name)756 nop_handler(const char *name)
757 {
758 GET_CURRENT_CONTEXT(ctx);
759 if (ctx) {
760 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid call)", name);
761 }
762 #ifndef NDEBUG
763 else if (getenv("MESA_DEBUG") || getenv("LIBGL_DEBUG")) {
764 fprintf(stderr,
765 "GL User Error: gl%s called without a rendering context\n",
766 name);
767 fflush(stderr);
768 }
769 #endif
770 }
771 #endif
772
773
774 /**
775 * Special no-op glFlush, see below.
776 */
777 #if defined(_WIN32)
778 static void GLAPIENTRY
nop_glFlush(void)779 nop_glFlush(void)
780 {
781 /* don't record an error like we do in nop_handler() */
782 }
783 #endif
784
785
786 #if !USE_GLAPI_NOP_FEATURES
787 static int
generic_nop(void)788 generic_nop(void)
789 {
790 GET_CURRENT_CONTEXT(ctx);
791 _mesa_error(ctx, GL_INVALID_OPERATION,
792 "unsupported function called "
793 "(unsupported extension or deprecated function?)");
794 return 0;
795 }
796 #endif
797
798
799 static int
glthread_nop(void)800 glthread_nop(void)
801 {
802 /* This writes the error into the glthread command buffer if glthread is
803 * enabled.
804 */
805 CALL_InternalSetError(GET_DISPATCH(), (GL_INVALID_OPERATION));
806 return 0;
807 }
808
809
810 /**
811 * Create a new API dispatch table in which all entries point to the
812 * generic_nop() function. This will not work on Windows because of
813 * the __stdcall convention which requires the callee to clean up the
814 * call stack. That's impossible with one generic no-op function.
815 */
816 struct _glapi_table *
_mesa_new_nop_table(unsigned numEntries,bool glthread)817 _mesa_new_nop_table(unsigned numEntries, bool glthread)
818 {
819 struct _glapi_table *table;
820
821 #if !USE_GLAPI_NOP_FEATURES
822 table = malloc(numEntries * sizeof(_glapi_proc));
823 if (table) {
824 _glapi_proc *entry = (_glapi_proc *) table;
825 unsigned i;
826 for (i = 0; i < numEntries; i++) {
827 entry[i] = (_glapi_proc) generic_nop;
828 }
829 }
830 #else
831 table = _glapi_new_nop_table(numEntries);
832 #endif
833
834 if (glthread) {
835 _glapi_proc *entry = (_glapi_proc *) table;
836 for (unsigned i = 0; i < numEntries; i++)
837 entry[i] = (_glapi_proc)glthread_nop;
838 }
839
840 return table;
841 }
842
843
844 /**
845 * Allocate and initialize a new dispatch table. The table will be
846 * populated with pointers to "no-op" functions. In turn, the no-op
847 * functions will call nop_handler() above.
848 */
849 struct _glapi_table *
_mesa_alloc_dispatch_table(bool glthread)850 _mesa_alloc_dispatch_table(bool glthread)
851 {
852 /* Find the larger of Mesa's dispatch table and libGL's dispatch table.
853 * In practice, this'll be the same for stand-alone Mesa. But for DRI
854 * Mesa we do this to accommodate different versions of libGL and various
855 * DRI drivers.
856 */
857 int numEntries = MAX2(_glapi_get_dispatch_table_size(), _gloffset_COUNT);
858
859 struct _glapi_table *table = _mesa_new_nop_table(numEntries, glthread);
860
861 #if defined(_WIN32)
862 if (table) {
863 /* This is a special case for Windows in the event that
864 * wglGetProcAddress is called between glBegin/End().
865 *
866 * The MS opengl32.dll library apparently calls glFlush from
867 * wglGetProcAddress(). If we're inside glBegin/End(), glFlush
868 * will dispatch to _mesa_generic_nop() and we'll generate a
869 * GL_INVALID_OPERATION error.
870 *
871 * The specific case which hits this is piglit's primitive-restart
872 * test which calls glPrimitiveRestartNV() inside glBegin/End. The
873 * first time we call glPrimitiveRestartNV() Piglit's API dispatch
874 * code will try to resolve the function by calling wglGetProcAddress.
875 * This raises GL_INVALID_OPERATION and an assert(glGetError()==0)
876 * will fail causing the test to fail. By suppressing the error, the
877 * assertion passes and the test continues.
878 */
879 SET_Flush(table, nop_glFlush);
880 }
881 #endif
882
883 #if USE_GLAPI_NOP_FEATURES
884 _glapi_set_nop_handler(nop_handler);
885 #endif
886
887 return table;
888 }
889
890 /**
891 * Allocate dispatch tables and set all functions to nop.
892 * It also makes the OutsideBeginEnd dispatch table current within gl_dispatch.
893 *
894 * \param glthread Whether to set nop dispatch for glthread or regular dispatch
895 */
896 bool
_mesa_alloc_dispatch_tables(gl_api api,struct gl_dispatch * d,bool glthread)897 _mesa_alloc_dispatch_tables(gl_api api, struct gl_dispatch *d, bool glthread)
898 {
899 d->OutsideBeginEnd = _mesa_alloc_dispatch_table(glthread);
900 if (!d->OutsideBeginEnd)
901 return false;
902
903 if (api == API_OPENGL_COMPAT) {
904 d->BeginEnd = _mesa_alloc_dispatch_table(glthread);
905 d->Save = _mesa_alloc_dispatch_table(glthread);
906 if (!d->BeginEnd || !d->Save)
907 return false;
908 }
909
910 d->Current = d->Exec = d->OutsideBeginEnd;
911 return true;
912 }
913
914 static void
_mesa_free_dispatch_tables(struct gl_dispatch * d)915 _mesa_free_dispatch_tables(struct gl_dispatch *d)
916 {
917 free(d->OutsideBeginEnd);
918 free(d->BeginEnd);
919 free(d->HWSelectModeBeginEnd);
920 free(d->Save);
921 free(d->ContextLost);
922 }
923
924 bool
_mesa_initialize_dispatch_tables(struct gl_context * ctx)925 _mesa_initialize_dispatch_tables(struct gl_context *ctx)
926 {
927 if (!_mesa_alloc_dispatch_tables(ctx->API, &ctx->Dispatch, false))
928 return false;
929
930 /* Do the code-generated initialization of dispatch tables. */
931 _mesa_init_dispatch(ctx);
932 vbo_init_dispatch_begin_end(ctx);
933
934 if (_mesa_is_desktop_gl_compat(ctx)) {
935 _mesa_init_dispatch_save(ctx);
936 _mesa_init_dispatch_save_begin_end(ctx);
937 }
938
939 /* This binds the dispatch table to the context, but MakeCurrent will
940 * bind it for the user. If glthread is enabled, it will override it.
941 */
942 ctx->GLApi = ctx->Dispatch.Current;
943 return true;
944 }
945
946 /**
947 * Initialize a struct gl_context struct (rendering context).
948 *
949 * This includes allocating all the other structs and arrays which hang off of
950 * the context by pointers.
951 * Note that the driver needs to pass in its dd_function_table here since
952 * we need to at least call st_NewTextureObject to create the
953 * default texture objects.
954 *
955 * Called by _mesa_create_context().
956 *
957 * Performs the imports and exports callback tables initialization, and
958 * miscellaneous one-time initializations. If no shared context is supplied one
959 * is allocated, and increase its reference count. Setups the GL API dispatch
960 * tables. Initialize the TNL module. Sets the maximum Z buffer depth.
961 * Finally queries the \c MESA_DEBUG and \c MESA_VERBOSE environment variables
962 * for debug flags.
963 *
964 * \param ctx the context to initialize
965 * \param api the GL API type to create the context for
966 * \param visual describes the visual attributes for this context or NULL to
967 * create a configless context
968 * \param share_list points to context to share textures, display lists,
969 * etc with, or NULL
970 * \param driverFunctions table of device driver functions for this context
971 * to use
972 */
973 GLboolean
_mesa_initialize_context(struct gl_context * ctx,gl_api api,bool no_error,const struct gl_config * visual,struct gl_context * share_list,const struct dd_function_table * driverFunctions,const struct st_config_options * options)974 _mesa_initialize_context(struct gl_context *ctx,
975 gl_api api,
976 bool no_error,
977 const struct gl_config *visual,
978 struct gl_context *share_list,
979 const struct dd_function_table *driverFunctions,
980 const struct st_config_options *options)
981 {
982 struct gl_shared_state *shared;
983 int i;
984
985 switch (api) {
986 case API_OPENGL_COMPAT:
987 case API_OPENGL_CORE:
988 if (!HAVE_OPENGL)
989 return GL_FALSE;
990 break;
991 case API_OPENGLES2:
992 if (!HAVE_OPENGL_ES_2)
993 return GL_FALSE;
994 break;
995 case API_OPENGLES:
996 if (!HAVE_OPENGL_ES_1)
997 return GL_FALSE;
998 break;
999 default:
1000 return GL_FALSE;
1001 }
1002
1003 ctx->API = api;
1004 ctx->DrawBuffer = NULL;
1005 ctx->ReadBuffer = NULL;
1006 ctx->WinSysDrawBuffer = NULL;
1007 ctx->WinSysReadBuffer = NULL;
1008
1009 if (visual) {
1010 ctx->Visual = *visual;
1011 ctx->HasConfig = GL_TRUE;
1012 }
1013 else {
1014 memset(&ctx->Visual, 0, sizeof ctx->Visual);
1015 ctx->HasConfig = GL_FALSE;
1016 }
1017
1018 _mesa_override_gl_version(ctx);
1019
1020 /* misc one-time initializations */
1021 _mesa_initialize(NULL);
1022
1023 /* Plug in driver functions and context pointer here.
1024 * This is important because when we call alloc_shared_state() below
1025 * we'll call ctx->Driver.NewTextureObject() to create the default
1026 * textures.
1027 */
1028 ctx->Driver = *driverFunctions;
1029
1030 if (share_list) {
1031 /* share state with another context */
1032 shared = share_list->Shared;
1033 }
1034 else {
1035 /* allocate new, unshared state */
1036 shared = _mesa_alloc_shared_state(ctx, options);
1037 if (!shared)
1038 return GL_FALSE;
1039 }
1040
1041 /* all supported by default */
1042 ctx->Const.DriverSupportedPrimMask = 0xffffffff;
1043
1044 _mesa_reference_shared_state(ctx, &ctx->Shared, shared);
1045
1046 if (!init_attrib_groups( ctx ))
1047 goto fail;
1048
1049 if (no_error)
1050 ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_NO_ERROR_BIT_KHR;
1051
1052 _mesa_reset_vertex_processing_mode(ctx);
1053
1054 /* Mesa core handles all the formats that mesa core knows about.
1055 * Drivers will want to override this list with just the formats
1056 * they can handle.
1057 */
1058 memset(&ctx->TextureFormatSupported, GL_TRUE,
1059 sizeof(ctx->TextureFormatSupported));
1060
1061 switch (ctx->API) {
1062 case API_OPENGL_COMPAT:
1063 case API_OPENGL_CORE:
1064 case API_OPENGLES2:
1065 break;
1066 case API_OPENGLES:
1067 /**
1068 * GL_OES_texture_cube_map says
1069 * "Initially all texture generation modes are set to REFLECTION_MAP_OES"
1070 */
1071 for (i = 0; i < ARRAY_SIZE(ctx->Texture.FixedFuncUnit); i++) {
1072 struct gl_fixedfunc_texture_unit *texUnit =
1073 &ctx->Texture.FixedFuncUnit[i];
1074
1075 texUnit->GenS.Mode = GL_REFLECTION_MAP_NV;
1076 texUnit->GenT.Mode = GL_REFLECTION_MAP_NV;
1077 texUnit->GenR.Mode = GL_REFLECTION_MAP_NV;
1078 texUnit->GenS._ModeBit = TEXGEN_REFLECTION_MAP_NV;
1079 texUnit->GenT._ModeBit = TEXGEN_REFLECTION_MAP_NV;
1080 texUnit->GenR._ModeBit = TEXGEN_REFLECTION_MAP_NV;
1081 }
1082 break;
1083 }
1084 ctx->VertexProgram.PointSizeEnabled = _mesa_is_gles2(ctx);
1085 ctx->PointSizeIsSet = GL_TRUE;
1086
1087 ctx->FirstTimeCurrent = GL_TRUE;
1088
1089 return GL_TRUE;
1090
1091 fail:
1092 _mesa_reference_shared_state(ctx, &ctx->Shared, NULL);
1093 return GL_FALSE;
1094 }
1095
1096
1097 /**
1098 * Free the data associated with the given context.
1099 *
1100 * But doesn't free the struct gl_context struct itself.
1101 *
1102 * \sa _mesa_initialize_context() and init_attrib_groups().
1103 */
1104 void
_mesa_free_context_data(struct gl_context * ctx,bool destroy_debug_output)1105 _mesa_free_context_data(struct gl_context *ctx, bool destroy_debug_output)
1106 {
1107 if (!_mesa_get_current_context()){
1108 /* No current context, but we may need one in order to delete
1109 * texture objs, etc. So temporarily bind the context now.
1110 */
1111 _mesa_make_current(ctx, NULL, NULL);
1112 }
1113
1114 /* unreference WinSysDraw/Read buffers */
1115 _mesa_reference_framebuffer(&ctx->WinSysDrawBuffer, NULL);
1116 _mesa_reference_framebuffer(&ctx->WinSysReadBuffer, NULL);
1117 _mesa_reference_framebuffer(&ctx->DrawBuffer, NULL);
1118 _mesa_reference_framebuffer(&ctx->ReadBuffer, NULL);
1119
1120 _mesa_reference_program(ctx, &ctx->VertexProgram.Current, NULL);
1121 _mesa_reference_program(ctx, &ctx->VertexProgram._Current, NULL);
1122 _mesa_reference_program(ctx, &ctx->VertexProgram._TnlProgram, NULL);
1123
1124 _mesa_reference_program(ctx, &ctx->TessCtrlProgram._Current, NULL);
1125 _mesa_reference_program(ctx, &ctx->TessEvalProgram._Current, NULL);
1126 _mesa_reference_program(ctx, &ctx->GeometryProgram._Current, NULL);
1127
1128 _mesa_reference_program(ctx, &ctx->FragmentProgram.Current, NULL);
1129 _mesa_reference_program(ctx, &ctx->FragmentProgram._Current, NULL);
1130 _mesa_reference_program(ctx, &ctx->FragmentProgram._TexEnvProgram, NULL);
1131
1132 _mesa_reference_program(ctx, &ctx->ComputeProgram._Current, NULL);
1133
1134 _mesa_reference_vao(ctx, &ctx->Array.VAO, NULL);
1135 _mesa_reference_vao(ctx, &ctx->Array.DefaultVAO, NULL);
1136 _mesa_reference_vao(ctx, &ctx->Array._DrawVAO, NULL);
1137
1138 _mesa_free_attrib_data(ctx);
1139 _mesa_free_eval_data( ctx );
1140 _mesa_free_feedback(ctx);
1141 _mesa_free_texture_data( ctx );
1142 _mesa_free_image_textures(ctx);
1143 _mesa_free_matrix_data( ctx );
1144 _mesa_free_pipeline_data(ctx);
1145 _mesa_free_program_data(ctx);
1146 _mesa_free_shader_state(ctx);
1147 _mesa_free_queryobj_data(ctx);
1148 _mesa_free_sync_data(ctx);
1149 _mesa_free_varray_data(ctx);
1150 _mesa_free_transform_feedback(ctx);
1151 _mesa_free_performance_monitors(ctx);
1152 _mesa_free_performance_queries(ctx);
1153 _mesa_free_perfomance_monitor_groups(ctx);
1154 _mesa_free_resident_handles(ctx);
1155
1156 _mesa_reference_buffer_object(ctx, &ctx->Pack.BufferObj, NULL);
1157 _mesa_reference_buffer_object(ctx, &ctx->Unpack.BufferObj, NULL);
1158 _mesa_reference_buffer_object(ctx, &ctx->DefaultPacking.BufferObj, NULL);
1159 _mesa_reference_buffer_object(ctx, &ctx->Array.ArrayBufferObj, NULL);
1160
1161 /* This must be called after all buffers are unbound because global buffer
1162 * references that this context holds will be removed.
1163 */
1164 _mesa_free_buffer_objects(ctx);
1165
1166 /* free dispatch tables */
1167 _mesa_free_dispatch_tables(&ctx->Dispatch);
1168 free(ctx->MarshalExec);
1169
1170 /* Shared context state (display lists, textures, etc) */
1171 _mesa_reference_shared_state(ctx, &ctx->Shared, NULL);
1172
1173 if (destroy_debug_output)
1174 _mesa_destroy_debug_output(ctx);
1175
1176 free((void *)ctx->Extensions.String);
1177
1178 free(ctx->VersionString);
1179
1180 ralloc_free(ctx->SoftFP64);
1181
1182 /* unbind the context if it's currently bound */
1183 if (ctx == _mesa_get_current_context()) {
1184 _mesa_make_current(NULL, NULL, NULL);
1185 }
1186
1187 /* Do this after unbinding context to ensure any thread is finished. */
1188 if (ctx->shader_builtin_ref) {
1189 _mesa_glsl_builtin_functions_decref();
1190 ctx->shader_builtin_ref = false;
1191 }
1192
1193 free(ctx->Const.SpirVExtensions);
1194 free(ctx->tmp_draws);
1195 }
1196
1197
1198 /**
1199 * Copy attribute groups from one context to another.
1200 *
1201 * \param src source context
1202 * \param dst destination context
1203 * \param mask bitwise OR of GL_*_BIT flags
1204 *
1205 * According to the bits specified in \p mask, copies the corresponding
1206 * attributes from \p src into \p dst. For many of the attributes a simple \c
1207 * memcpy is not enough due to the existence of internal pointers in their data
1208 * structures.
1209 */
1210 void
_mesa_copy_context(const struct gl_context * src,struct gl_context * dst,GLuint mask)1211 _mesa_copy_context( const struct gl_context *src, struct gl_context *dst,
1212 GLuint mask )
1213 {
1214 if (mask & GL_ACCUM_BUFFER_BIT) {
1215 /* OK to memcpy */
1216 dst->Accum = src->Accum;
1217 }
1218 if (mask & GL_COLOR_BUFFER_BIT) {
1219 /* OK to memcpy */
1220 dst->Color = src->Color;
1221 }
1222 if (mask & GL_CURRENT_BIT) {
1223 /* OK to memcpy */
1224 dst->Current = src->Current;
1225 }
1226 if (mask & GL_DEPTH_BUFFER_BIT) {
1227 /* OK to memcpy */
1228 dst->Depth = src->Depth;
1229 }
1230 if (mask & GL_ENABLE_BIT) {
1231 /* no op */
1232 }
1233 if (mask & GL_EVAL_BIT) {
1234 /* OK to memcpy */
1235 dst->Eval = src->Eval;
1236 }
1237 if (mask & GL_FOG_BIT) {
1238 /* OK to memcpy */
1239 dst->Fog = src->Fog;
1240 }
1241 if (mask & GL_HINT_BIT) {
1242 /* OK to memcpy */
1243 dst->Hint = src->Hint;
1244 }
1245 if (mask & GL_LIGHTING_BIT) {
1246 /* OK to memcpy */
1247 dst->Light = src->Light;
1248 }
1249 if (mask & GL_LINE_BIT) {
1250 /* OK to memcpy */
1251 dst->Line = src->Line;
1252 }
1253 if (mask & GL_LIST_BIT) {
1254 /* OK to memcpy */
1255 dst->List = src->List;
1256 }
1257 if (mask & GL_PIXEL_MODE_BIT) {
1258 /* OK to memcpy */
1259 dst->Pixel = src->Pixel;
1260 }
1261 if (mask & GL_POINT_BIT) {
1262 /* OK to memcpy */
1263 dst->Point = src->Point;
1264 }
1265 if (mask & GL_POLYGON_BIT) {
1266 /* OK to memcpy */
1267 dst->Polygon = src->Polygon;
1268 }
1269 if (mask & GL_POLYGON_STIPPLE_BIT) {
1270 /* Use loop instead of memcpy due to problem with Portland Group's
1271 * C compiler. Reported by John Stone.
1272 */
1273 GLuint i;
1274 for (i = 0; i < 32; i++) {
1275 dst->PolygonStipple[i] = src->PolygonStipple[i];
1276 }
1277 }
1278 if (mask & GL_SCISSOR_BIT) {
1279 /* OK to memcpy */
1280 dst->Scissor = src->Scissor;
1281 }
1282 if (mask & GL_STENCIL_BUFFER_BIT) {
1283 /* OK to memcpy */
1284 dst->Stencil = src->Stencil;
1285 }
1286 if (mask & GL_TEXTURE_BIT) {
1287 /* Cannot memcpy because of pointers */
1288 _mesa_copy_texture_state(src, dst);
1289 }
1290 if (mask & GL_TRANSFORM_BIT) {
1291 /* OK to memcpy */
1292 dst->Transform = src->Transform;
1293 }
1294 if (mask & GL_VIEWPORT_BIT) {
1295 unsigned i;
1296 for (i = 0; i < src->Const.MaxViewports; i++) {
1297 /* OK to memcpy */
1298 dst->ViewportArray[i] = src->ViewportArray[i];
1299 }
1300 }
1301
1302 /* XXX FIXME: Call callbacks?
1303 */
1304 dst->NewState = _NEW_ALL;
1305 dst->NewDriverState = ST_ALL_STATES_MASK;
1306 }
1307
1308
1309 /**
1310 * Check if the given context can render into the given framebuffer
1311 * by checking visual attributes.
1312 *
1313 * \return GL_TRUE if compatible, GL_FALSE otherwise.
1314 */
1315 static GLboolean
check_compatible(const struct gl_context * ctx,const struct gl_framebuffer * buffer)1316 check_compatible(const struct gl_context *ctx,
1317 const struct gl_framebuffer *buffer)
1318 {
1319 const struct gl_config *ctxvis = &ctx->Visual;
1320 const struct gl_config *bufvis = &buffer->Visual;
1321
1322 if (buffer == _mesa_get_incomplete_framebuffer())
1323 return GL_TRUE;
1324
1325 #define check_component(foo) \
1326 if (ctxvis->foo && bufvis->foo && \
1327 ctxvis->foo != bufvis->foo) \
1328 return GL_FALSE
1329
1330 check_component(redShift);
1331 check_component(greenShift);
1332 check_component(blueShift);
1333 check_component(redBits);
1334 check_component(greenBits);
1335 check_component(blueBits);
1336 check_component(depthBits);
1337 check_component(stencilBits);
1338
1339 #undef check_component
1340
1341 return GL_TRUE;
1342 }
1343
1344
1345 /**
1346 * Check if the viewport/scissor size has not yet been initialized.
1347 * Initialize the size if the given width and height are non-zero.
1348 */
1349 static void
check_init_viewport(struct gl_context * ctx,GLuint width,GLuint height)1350 check_init_viewport(struct gl_context *ctx, GLuint width, GLuint height)
1351 {
1352 if (!ctx->ViewportInitialized && width > 0 && height > 0) {
1353 unsigned i;
1354
1355 /* Note: set flag here, before calling _mesa_set_viewport(), to prevent
1356 * potential infinite recursion.
1357 */
1358 ctx->ViewportInitialized = GL_TRUE;
1359
1360 /* Note: ctx->Const.MaxViewports may not have been set by the driver
1361 * yet, so just initialize all of them.
1362 */
1363 for (i = 0; i < MAX_VIEWPORTS; i++) {
1364 _mesa_set_viewport(ctx, i, 0, 0, width, height);
1365 _mesa_set_scissor(ctx, i, 0, 0, width, height);
1366 }
1367 }
1368 }
1369
1370
1371 static void
handle_first_current(struct gl_context * ctx)1372 handle_first_current(struct gl_context *ctx)
1373 {
1374 if (ctx->Version == 0 || !ctx->DrawBuffer) {
1375 /* probably in the process of tearing down the context */
1376 return;
1377 }
1378
1379 check_context_limits(ctx);
1380
1381 _mesa_update_vertex_processing_mode(ctx);
1382
1383 /* According to GL_MESA_configless_context the default value of
1384 * glDrawBuffers depends on the config of the first surface it is bound to.
1385 * For GLES it is always GL_BACK which has a magic interpretation.
1386 */
1387 if (!ctx->HasConfig && _mesa_is_desktop_gl(ctx)) {
1388 if (ctx->DrawBuffer != _mesa_get_incomplete_framebuffer()) {
1389 GLenum16 buffer;
1390
1391 if (ctx->DrawBuffer->Visual.doubleBufferMode)
1392 buffer = GL_BACK;
1393 else
1394 buffer = GL_FRONT;
1395
1396 _mesa_drawbuffers(ctx, ctx->DrawBuffer, 1, &buffer,
1397 NULL /* destMask */);
1398 }
1399
1400 if (ctx->ReadBuffer != _mesa_get_incomplete_framebuffer()) {
1401 gl_buffer_index bufferIndex;
1402 GLenum buffer;
1403
1404 if (ctx->ReadBuffer->Visual.doubleBufferMode) {
1405 buffer = GL_BACK;
1406 bufferIndex = BUFFER_BACK_LEFT;
1407 }
1408 else {
1409 buffer = GL_FRONT;
1410 bufferIndex = BUFFER_FRONT_LEFT;
1411 }
1412
1413 _mesa_readbuffer(ctx, ctx->ReadBuffer, buffer, bufferIndex);
1414 }
1415 }
1416
1417 /* Determine if generic vertex attribute 0 aliases the conventional
1418 * glVertex position.
1419 */
1420 {
1421 const bool is_forward_compatible_context =
1422 ctx->Const.ContextFlags & GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT;
1423
1424 /* In OpenGL 3.1 attribute 0 becomes non-magic, just like in OpenGL ES
1425 * 2.0. Note that we cannot just check for API_OPENGL_COMPAT here because
1426 * that will erroneously allow this usage in a 3.0 forward-compatible
1427 * context too.
1428 */
1429 ctx->_AttribZeroAliasesVertex = (_mesa_is_gles1(ctx)
1430 || (_mesa_is_desktop_gl_compat(ctx)
1431 && !is_forward_compatible_context));
1432 }
1433
1434 /* We can use this to help debug user's problems. Tell them to set
1435 * the MESA_INFO env variable before running their app. Then the
1436 * first time each context is made current we'll print some useful
1437 * information.
1438 */
1439 if (getenv("MESA_INFO")) {
1440 _mesa_print_info(ctx);
1441 }
1442 }
1443
1444 /**
1445 * Bind the given context to the given drawBuffer and readBuffer and
1446 * make it the current context for the calling thread.
1447 * We'll render into the drawBuffer and read pixels from the
1448 * readBuffer (i.e. glRead/CopyPixels, glCopyTexImage, etc).
1449 *
1450 * We check that the context's and framebuffer's visuals are compatible
1451 * and return immediately if they're not.
1452 *
1453 * \param newCtx the new GL context. If NULL then there will be no current GL
1454 * context.
1455 * \param drawBuffer the drawing framebuffer
1456 * \param readBuffer the reading framebuffer
1457 */
1458 GLboolean
_mesa_make_current(struct gl_context * newCtx,struct gl_framebuffer * drawBuffer,struct gl_framebuffer * readBuffer)1459 _mesa_make_current( struct gl_context *newCtx,
1460 struct gl_framebuffer *drawBuffer,
1461 struct gl_framebuffer *readBuffer )
1462 {
1463 GET_CURRENT_CONTEXT(curCtx);
1464
1465 if (MESA_VERBOSE & VERBOSE_API)
1466 _mesa_debug(newCtx, "_mesa_make_current()\n");
1467
1468 /* Check that the context's and framebuffer's visuals are compatible.
1469 */
1470 if (newCtx && drawBuffer && newCtx->WinSysDrawBuffer != drawBuffer) {
1471 if (!check_compatible(newCtx, drawBuffer)) {
1472 _mesa_warning(newCtx,
1473 "MakeCurrent: incompatible visuals for context and drawbuffer");
1474 return GL_FALSE;
1475 }
1476 }
1477 if (newCtx && readBuffer && newCtx->WinSysReadBuffer != readBuffer) {
1478 if (!check_compatible(newCtx, readBuffer)) {
1479 _mesa_warning(newCtx,
1480 "MakeCurrent: incompatible visuals for context and readbuffer");
1481 return GL_FALSE;
1482 }
1483 }
1484
1485 if (curCtx &&
1486 /* make sure this context is valid for flushing */
1487 curCtx != newCtx &&
1488 curCtx->Const.ContextReleaseBehavior ==
1489 GL_CONTEXT_RELEASE_BEHAVIOR_FLUSH) {
1490 FLUSH_VERTICES(curCtx, 0, 0);
1491 if (curCtx->st){
1492 st_glFlush(curCtx, 0);
1493 }
1494 }
1495
1496 if (!newCtx) {
1497 _glapi_set_dispatch(NULL); /* none current */
1498 /* We need old ctx to correctly release Draw/ReadBuffer
1499 * and avoid a surface leak in st_renderbuffer_delete.
1500 * Therefore, first drop buffers then set new ctx to NULL.
1501 */
1502 if (curCtx) {
1503 _mesa_reference_framebuffer(&curCtx->WinSysDrawBuffer, NULL);
1504 _mesa_reference_framebuffer(&curCtx->WinSysReadBuffer, NULL);
1505 }
1506 _glapi_set_context(NULL);
1507 assert(_mesa_get_current_context() == NULL);
1508 }
1509 else {
1510 _glapi_set_context((void *) newCtx);
1511 assert(_mesa_get_current_context() == newCtx);
1512 _glapi_set_dispatch(newCtx->GLApi);
1513
1514 if (drawBuffer && readBuffer) {
1515 assert(_mesa_is_winsys_fbo(drawBuffer));
1516 assert(_mesa_is_winsys_fbo(readBuffer));
1517 _mesa_reference_framebuffer(&newCtx->WinSysDrawBuffer, drawBuffer);
1518 _mesa_reference_framebuffer(&newCtx->WinSysReadBuffer, readBuffer);
1519
1520 /*
1521 * Only set the context's Draw/ReadBuffer fields if they're NULL
1522 * or not bound to a user-created FBO.
1523 */
1524 if (!newCtx->DrawBuffer || _mesa_is_winsys_fbo(newCtx->DrawBuffer)) {
1525 _mesa_reference_framebuffer(&newCtx->DrawBuffer, drawBuffer);
1526 /* Update the FBO's list of drawbuffers/renderbuffers.
1527 * For winsys FBOs this comes from the GL state (which may have
1528 * changed since the last time this FBO was bound).
1529 */
1530 _mesa_update_draw_buffers(newCtx);
1531 _mesa_update_allow_draw_out_of_order(newCtx);
1532 _mesa_update_valid_to_render_state(newCtx);
1533 }
1534 if (!newCtx->ReadBuffer || _mesa_is_winsys_fbo(newCtx->ReadBuffer)) {
1535 _mesa_reference_framebuffer(&newCtx->ReadBuffer, readBuffer);
1536 /* In _mesa_initialize_window_framebuffer, for single-buffered
1537 * visuals, the ColorReadBuffer is set to be GL_FRONT, even with
1538 * GLES contexts. When calling read_buffer, we verify we are reading
1539 * from GL_BACK in is_legal_es3_readbuffer_enum. But the default is
1540 * incorrect, and certain dEQP tests check this. So fix it here.
1541 */
1542 if (_mesa_is_gles(newCtx) &&
1543 !newCtx->ReadBuffer->Visual.doubleBufferMode)
1544 if (newCtx->ReadBuffer->ColorReadBuffer == GL_FRONT)
1545 newCtx->ReadBuffer->ColorReadBuffer = GL_BACK;
1546 }
1547
1548 /* XXX only set this flag if we're really changing the draw/read
1549 * framebuffer bindings.
1550 */
1551 newCtx->NewState |= _NEW_BUFFERS;
1552
1553 check_init_viewport(newCtx, drawBuffer->Width, drawBuffer->Height);
1554 }
1555
1556 if (newCtx->FirstTimeCurrent) {
1557 handle_first_current(newCtx);
1558 newCtx->FirstTimeCurrent = GL_FALSE;
1559 }
1560 }
1561
1562 return GL_TRUE;
1563 }
1564
1565
1566 /**
1567 * Make context 'ctx' share the display lists, textures and programs
1568 * that are associated with 'ctxToShare'.
1569 * Any display lists, textures or programs associated with 'ctx' will
1570 * be deleted if nobody else is sharing them.
1571 */
1572 GLboolean
_mesa_share_state(struct gl_context * ctx,struct gl_context * ctxToShare)1573 _mesa_share_state(struct gl_context *ctx, struct gl_context *ctxToShare)
1574 {
1575 if (ctx && ctxToShare && ctx->Shared && ctxToShare->Shared) {
1576 struct gl_shared_state *oldShared = NULL;
1577
1578 /* save ref to old state to prevent it from being deleted immediately */
1579 _mesa_reference_shared_state(ctx, &oldShared, ctx->Shared);
1580
1581 /* update ctx's Shared pointer */
1582 _mesa_reference_shared_state(ctx, &ctx->Shared, ctxToShare->Shared);
1583
1584 update_default_objects(ctx);
1585
1586 /* release the old shared state */
1587 _mesa_reference_shared_state(ctx, &oldShared, NULL);
1588
1589 return GL_TRUE;
1590 }
1591 else {
1592 return GL_FALSE;
1593 }
1594 }
1595
1596
1597
1598 /**
1599 * \return pointer to the current GL context for this thread.
1600 *
1601 * Calls _glapi_get_context(). This isn't the fastest way to get the current
1602 * context. If you need speed, see the #GET_CURRENT_CONTEXT macro in
1603 * context.h.
1604 */
1605 struct gl_context *
_mesa_get_current_context(void)1606 _mesa_get_current_context( void )
1607 {
1608 return (struct gl_context *) _glapi_get_context();
1609 }
1610
1611 /*@}*/
1612
1613
1614 /**********************************************************************/
1615 /** \name Miscellaneous functions */
1616 /**********************************************************************/
1617 /*@{*/
1618 /**
1619 * Flush commands.
1620 */
1621 void
_mesa_flush(struct gl_context * ctx)1622 _mesa_flush(struct gl_context *ctx)
1623 {
1624 bool async = !ctx->Shared->HasExternallySharedImages;
1625 FLUSH_VERTICES(ctx, 0, 0);
1626
1627 st_glFlush(ctx, async ? PIPE_FLUSH_ASYNC : 0);
1628 }
1629
1630
1631
1632 /**
1633 * Flush commands and wait for completion.
1634 *
1635 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
1636 * dd_function_table::Finish driver callback, if not NULL.
1637 */
1638 void GLAPIENTRY
_mesa_Finish(void)1639 _mesa_Finish(void)
1640 {
1641 GET_CURRENT_CONTEXT(ctx);
1642 ASSERT_OUTSIDE_BEGIN_END(ctx);
1643
1644 FLUSH_VERTICES(ctx, 0, 0);
1645
1646 st_glFinish(ctx);
1647 }
1648
1649
1650 /**
1651 * Execute glFlush().
1652 *
1653 * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
1654 * dd_function_table::Flush driver callback, if not NULL.
1655 */
1656 void GLAPIENTRY
_mesa_Flush(void)1657 _mesa_Flush(void)
1658 {
1659 GET_CURRENT_CONTEXT(ctx);
1660 ASSERT_OUTSIDE_BEGIN_END(ctx);
1661 _mesa_flush(ctx);
1662 }
1663
1664
1665 /*@}*/
1666