1848b8605Smrg/*
2848b8605Smrg * Mesa 3-D graphics library
3848b8605Smrg *
4848b8605Smrg * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
5848b8605Smrg * Copyright (C) 2008  VMware, Inc.  All Rights Reserved.
6848b8605Smrg *
7848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
8848b8605Smrg * copy of this software and associated documentation files (the "Software"),
9848b8605Smrg * to deal in the Software without restriction, including without limitation
10848b8605Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11848b8605Smrg * and/or sell copies of the Software, and to permit persons to whom the
12848b8605Smrg * Software is furnished to do so, subject to the following conditions:
13848b8605Smrg *
14848b8605Smrg * The above copyright notice and this permission notice shall be included
15848b8605Smrg * in all copies or substantial portions of the Software.
16848b8605Smrg *
17848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18848b8605Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19848b8605Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20848b8605Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21848b8605Smrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22848b8605Smrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23848b8605Smrg * OTHER DEALINGS IN THE SOFTWARE.
24848b8605Smrg */
25848b8605Smrg
26848b8605Smrg/**
27848b8605Smrg * \file context.c
28848b8605Smrg * Mesa context/visual/framebuffer management functions.
29848b8605Smrg * \author Brian Paul
30848b8605Smrg */
31848b8605Smrg
32848b8605Smrg/**
33848b8605Smrg * \mainpage Mesa Main Module
34848b8605Smrg *
35848b8605Smrg * \section MainIntroduction Introduction
36848b8605Smrg *
37848b8605Smrg * The Mesa Main module consists of all the files in the main/ directory.
38848b8605Smrg * Among the features of this module are:
39848b8605Smrg * <UL>
40848b8605Smrg * <LI> Structures to represent most GL state </LI>
41848b8605Smrg * <LI> State set/get functions </LI>
42848b8605Smrg * <LI> Display lists </LI>
43848b8605Smrg * <LI> Texture unit, object and image handling </LI>
44848b8605Smrg * <LI> Matrix and attribute stacks </LI>
45848b8605Smrg * </UL>
46848b8605Smrg *
47848b8605Smrg * Other modules are responsible for API dispatch, vertex transformation,
48848b8605Smrg * point/line/triangle setup, rasterization, vertex array caching,
49848b8605Smrg * vertex/fragment programs/shaders, etc.
50848b8605Smrg *
51848b8605Smrg *
52848b8605Smrg * \section AboutDoxygen About Doxygen
53848b8605Smrg *
54848b8605Smrg * If you're viewing this information as Doxygen-generated HTML you'll
55848b8605Smrg * see the documentation index at the top of this page.
56848b8605Smrg *
57848b8605Smrg * The first line lists the Mesa source code modules.
58848b8605Smrg * The second line lists the indexes available for viewing the documentation
59848b8605Smrg * for each module.
60848b8605Smrg *
61848b8605Smrg * Selecting the <b>Main page</b> link will display a summary of the module
62848b8605Smrg * (this page).
63848b8605Smrg *
64848b8605Smrg * Selecting <b>Data Structures</b> will list all C structures.
65848b8605Smrg *
66848b8605Smrg * Selecting the <b>File List</b> link will list all the source files in
67848b8605Smrg * the module.
68848b8605Smrg * Selecting a filename will show a list of all functions defined in that file.
69848b8605Smrg *
70848b8605Smrg * Selecting the <b>Data Fields</b> link will display a list of all
71848b8605Smrg * documented structure members.
72848b8605Smrg *
73848b8605Smrg * Selecting the <b>Globals</b> link will display a list
74848b8605Smrg * of all functions, structures, global variables and macros in the module.
75848b8605Smrg *
76848b8605Smrg */
77848b8605Smrg
78848b8605Smrg
79848b8605Smrg#include "glheader.h"
80848b8605Smrg#include "imports.h"
81848b8605Smrg#include "accum.h"
82848b8605Smrg#include "api_exec.h"
83848b8605Smrg#include "api_loopback.h"
84848b8605Smrg#include "arrayobj.h"
85848b8605Smrg#include "attrib.h"
86b8e80941Smrg#include "bbox.h"
87848b8605Smrg#include "blend.h"
88848b8605Smrg#include "buffers.h"
89848b8605Smrg#include "bufferobj.h"
90b8e80941Smrg#include "conservativeraster.h"
91848b8605Smrg#include "context.h"
92848b8605Smrg#include "cpuinfo.h"
93848b8605Smrg#include "debug.h"
94b8e80941Smrg#include "debug_output.h"
95848b8605Smrg#include "depth.h"
96848b8605Smrg#include "dlist.h"
97848b8605Smrg#include "eval.h"
98848b8605Smrg#include "extensions.h"
99848b8605Smrg#include "fbobject.h"
100848b8605Smrg#include "feedback.h"
101848b8605Smrg#include "fog.h"
102848b8605Smrg#include "formats.h"
103848b8605Smrg#include "framebuffer.h"
104b8e80941Smrg#include "glthread.h"
105848b8605Smrg#include "hint.h"
106848b8605Smrg#include "hash.h"
107848b8605Smrg#include "light.h"
108848b8605Smrg#include "lines.h"
109848b8605Smrg#include "macros.h"
110848b8605Smrg#include "matrix.h"
111848b8605Smrg#include "multisample.h"
112848b8605Smrg#include "performance_monitor.h"
113b8e80941Smrg#include "performance_query.h"
114848b8605Smrg#include "pipelineobj.h"
115848b8605Smrg#include "pixel.h"
116848b8605Smrg#include "pixelstore.h"
117848b8605Smrg#include "points.h"
118848b8605Smrg#include "polygon.h"
119848b8605Smrg#include "queryobj.h"
120848b8605Smrg#include "syncobj.h"
121848b8605Smrg#include "rastpos.h"
122848b8605Smrg#include "remap.h"
123848b8605Smrg#include "scissor.h"
124848b8605Smrg#include "shared.h"
125848b8605Smrg#include "shaderobj.h"
126b8e80941Smrg#include "shaderimage.h"
127848b8605Smrg#include "state.h"
128b8e80941Smrg#include "util/debug.h"
129b8e80941Smrg#include "util/disk_cache.h"
130b8e80941Smrg#include "util/strtod.h"
131848b8605Smrg#include "stencil.h"
132b8e80941Smrg#include "shaderimage.h"
133848b8605Smrg#include "texcompress_s3tc.h"
134848b8605Smrg#include "texstate.h"
135848b8605Smrg#include "transformfeedback.h"
136848b8605Smrg#include "mtypes.h"
137848b8605Smrg#include "varray.h"
138848b8605Smrg#include "version.h"
139848b8605Smrg#include "viewport.h"
140b8e80941Smrg#include "texturebindless.h"
141848b8605Smrg#include "program/program.h"
142848b8605Smrg#include "math/m_matrix.h"
143848b8605Smrg#include "main/dispatch.h" /* for _gloffset_COUNT */
144b8e80941Smrg#include "macros.h"
145b8e80941Smrg#include "git_sha1.h"
146848b8605Smrg
147848b8605Smrg#ifdef USE_SPARC_ASM
148848b8605Smrg#include "sparc/sparc.h"
149848b8605Smrg#endif
150848b8605Smrg
151b8e80941Smrg#include "compiler/glsl_types.h"
152b8e80941Smrg#include "compiler/glsl/glsl_parser_extras.h"
153848b8605Smrg#include <stdbool.h>
154848b8605Smrg
155848b8605Smrg
156848b8605Smrg#ifndef MESA_VERBOSE
157848b8605Smrgint MESA_VERBOSE = 0;
158848b8605Smrg#endif
159848b8605Smrg
160848b8605Smrg#ifndef MESA_DEBUG_FLAGS
161848b8605Smrgint MESA_DEBUG_FLAGS = 0;
162848b8605Smrg#endif
163848b8605Smrg
164848b8605Smrg
165848b8605Smrg/* ubyte -> float conversion */
166848b8605SmrgGLfloat _mesa_ubyte_to_float_color_tab[256];
167848b8605Smrg
168848b8605Smrg
169848b8605Smrg
170848b8605Smrg/**
171848b8605Smrg * Swap buffers notification callback.
172b8e80941Smrg *
173848b8605Smrg * \param ctx GL context.
174848b8605Smrg *
175848b8605Smrg * Called by window system just before swapping buffers.
176848b8605Smrg * We have to finish any pending rendering.
177848b8605Smrg */
178848b8605Smrgvoid
179848b8605Smrg_mesa_notifySwapBuffers(struct gl_context *ctx)
180848b8605Smrg{
181848b8605Smrg   if (MESA_VERBOSE & VERBOSE_SWAPBUFFERS)
182848b8605Smrg      _mesa_debug(ctx, "SwapBuffers\n");
183848b8605Smrg   FLUSH_CURRENT( ctx, 0 );
184848b8605Smrg   if (ctx->Driver.Flush) {
185848b8605Smrg      ctx->Driver.Flush(ctx);
186848b8605Smrg   }
187848b8605Smrg}
188848b8605Smrg
189848b8605Smrg
190848b8605Smrg/**********************************************************************/
191848b8605Smrg/** \name GL Visual allocation/destruction                            */
192848b8605Smrg/**********************************************************************/
193848b8605Smrg/*@{*/
194848b8605Smrg
195848b8605Smrg/**
196848b8605Smrg * Allocates a struct gl_config structure and initializes it via
197848b8605Smrg * _mesa_initialize_visual().
198b8e80941Smrg *
199848b8605Smrg * \param dbFlag double buffering
200848b8605Smrg * \param stereoFlag stereo buffer
201848b8605Smrg * \param depthBits requested bits per depth buffer value. Any value in [0, 32]
202848b8605Smrg * is acceptable but the actual depth type will be GLushort or GLuint as
203848b8605Smrg * needed.
204848b8605Smrg * \param stencilBits requested minimum bits per stencil buffer value
205848b8605Smrg * \param accumRedBits, accumGreenBits, accumBlueBits, accumAlphaBits number
206848b8605Smrg * of bits per color component in accum buffer.
207848b8605Smrg * \param indexBits number of bits per pixel if \p rgbFlag is GL_FALSE
208848b8605Smrg * \param redBits number of bits per color component in frame buffer for RGB(A)
209848b8605Smrg * mode.  We always use 8 in core Mesa though.
210848b8605Smrg * \param greenBits same as above.
211848b8605Smrg * \param blueBits same as above.
212848b8605Smrg * \param alphaBits same as above.
213848b8605Smrg * \param numSamples not really used.
214b8e80941Smrg *
215848b8605Smrg * \return pointer to new struct gl_config or NULL if requested parameters
216848b8605Smrg * can't be met.
217848b8605Smrg *
218848b8605Smrg * \note Need to add params for level and numAuxBuffers (at least)
219848b8605Smrg */
220848b8605Smrgstruct gl_config *
221848b8605Smrg_mesa_create_visual( GLboolean dbFlag,
222848b8605Smrg                     GLboolean stereoFlag,
223848b8605Smrg                     GLint redBits,
224848b8605Smrg                     GLint greenBits,
225848b8605Smrg                     GLint blueBits,
226848b8605Smrg                     GLint alphaBits,
227848b8605Smrg                     GLint depthBits,
228848b8605Smrg                     GLint stencilBits,
229848b8605Smrg                     GLint accumRedBits,
230848b8605Smrg                     GLint accumGreenBits,
231848b8605Smrg                     GLint accumBlueBits,
232848b8605Smrg                     GLint accumAlphaBits,
233b8e80941Smrg                     GLuint numSamples )
234848b8605Smrg{
235848b8605Smrg   struct gl_config *vis = CALLOC_STRUCT(gl_config);
236848b8605Smrg   if (vis) {
237848b8605Smrg      if (!_mesa_initialize_visual(vis, dbFlag, stereoFlag,
238848b8605Smrg                                   redBits, greenBits, blueBits, alphaBits,
239848b8605Smrg                                   depthBits, stencilBits,
240848b8605Smrg                                   accumRedBits, accumGreenBits,
241848b8605Smrg                                   accumBlueBits, accumAlphaBits,
242848b8605Smrg                                   numSamples)) {
243848b8605Smrg         free(vis);
244848b8605Smrg         return NULL;
245848b8605Smrg      }
246848b8605Smrg   }
247848b8605Smrg   return vis;
248848b8605Smrg}
249848b8605Smrg
250848b8605Smrg
251848b8605Smrg/**
252848b8605Smrg * Makes some sanity checks and fills in the fields of the struct
253848b8605Smrg * gl_config object with the given parameters.  If the caller needs to
254848b8605Smrg * set additional fields, he should just probably init the whole
255848b8605Smrg * gl_config object himself.
256848b8605Smrg *
257848b8605Smrg * \return GL_TRUE on success, or GL_FALSE on failure.
258848b8605Smrg *
259848b8605Smrg * \sa _mesa_create_visual() above for the parameter description.
260848b8605Smrg */
261848b8605SmrgGLboolean
262848b8605Smrg_mesa_initialize_visual( struct gl_config *vis,
263848b8605Smrg                         GLboolean dbFlag,
264848b8605Smrg                         GLboolean stereoFlag,
265848b8605Smrg                         GLint redBits,
266848b8605Smrg                         GLint greenBits,
267848b8605Smrg                         GLint blueBits,
268848b8605Smrg                         GLint alphaBits,
269848b8605Smrg                         GLint depthBits,
270848b8605Smrg                         GLint stencilBits,
271848b8605Smrg                         GLint accumRedBits,
272848b8605Smrg                         GLint accumGreenBits,
273848b8605Smrg                         GLint accumBlueBits,
274848b8605Smrg                         GLint accumAlphaBits,
275b8e80941Smrg                         GLuint numSamples )
276848b8605Smrg{
277848b8605Smrg   assert(vis);
278848b8605Smrg
279848b8605Smrg   if (depthBits < 0 || depthBits > 32) {
280848b8605Smrg      return GL_FALSE;
281848b8605Smrg   }
282848b8605Smrg   if (stencilBits < 0 || stencilBits > 8) {
283848b8605Smrg      return GL_FALSE;
284848b8605Smrg   }
285848b8605Smrg   assert(accumRedBits >= 0);
286848b8605Smrg   assert(accumGreenBits >= 0);
287848b8605Smrg   assert(accumBlueBits >= 0);
288848b8605Smrg   assert(accumAlphaBits >= 0);
289848b8605Smrg
290848b8605Smrg   vis->rgbMode          = GL_TRUE;
291848b8605Smrg   vis->doubleBufferMode = dbFlag;
292848b8605Smrg   vis->stereoMode       = stereoFlag;
293848b8605Smrg
294848b8605Smrg   vis->redBits          = redBits;
295848b8605Smrg   vis->greenBits        = greenBits;
296848b8605Smrg   vis->blueBits         = blueBits;
297848b8605Smrg   vis->alphaBits        = alphaBits;
298848b8605Smrg   vis->rgbBits          = redBits + greenBits + blueBits;
299848b8605Smrg
300848b8605Smrg   vis->indexBits      = 0;
301848b8605Smrg   vis->depthBits      = depthBits;
302848b8605Smrg   vis->stencilBits    = stencilBits;
303848b8605Smrg
304848b8605Smrg   vis->accumRedBits   = accumRedBits;
305848b8605Smrg   vis->accumGreenBits = accumGreenBits;
306848b8605Smrg   vis->accumBlueBits  = accumBlueBits;
307848b8605Smrg   vis->accumAlphaBits = accumAlphaBits;
308848b8605Smrg
309848b8605Smrg   vis->haveAccumBuffer   = accumRedBits > 0;
310848b8605Smrg   vis->haveDepthBuffer   = depthBits > 0;
311848b8605Smrg   vis->haveStencilBuffer = stencilBits > 0;
312848b8605Smrg
313848b8605Smrg   vis->numAuxBuffers = 0;
314848b8605Smrg   vis->level = 0;
315848b8605Smrg   vis->sampleBuffers = numSamples > 0 ? 1 : 0;
316848b8605Smrg   vis->samples = numSamples;
317848b8605Smrg
318848b8605Smrg   return GL_TRUE;
319848b8605Smrg}
320848b8605Smrg
321848b8605Smrg
322848b8605Smrg/**
323848b8605Smrg * Destroy a visual and free its memory.
324848b8605Smrg *
325848b8605Smrg * \param vis visual.
326b8e80941Smrg *
327848b8605Smrg * Frees the visual structure.
328848b8605Smrg */
329848b8605Smrgvoid
330848b8605Smrg_mesa_destroy_visual( struct gl_config *vis )
331848b8605Smrg{
332848b8605Smrg   free(vis);
333848b8605Smrg}
334848b8605Smrg
335848b8605Smrg/*@}*/
336848b8605Smrg
337848b8605Smrg
338848b8605Smrg/**********************************************************************/
339848b8605Smrg/** \name Context allocation, initialization, destroying
340848b8605Smrg *
341848b8605Smrg * The purpose of the most initialization functions here is to provide the
342848b8605Smrg * default state values according to the OpenGL specification.
343848b8605Smrg */
344848b8605Smrg/**********************************************************************/
345848b8605Smrg/*@{*/
346848b8605Smrg
347848b8605Smrg
348848b8605Smrg/**
349848b8605Smrg * One-time initialization mutex lock.
350848b8605Smrg *
351848b8605Smrg * \sa Used by one_time_init().
352848b8605Smrg */
353848b8605Smrgmtx_t OneTimeLock = _MTX_INITIALIZER_NP;
354848b8605Smrg
355848b8605Smrg
356b8e80941Smrg/**
357b8e80941Smrg * Calls all the various one-time-fini functions in Mesa
358b8e80941Smrg */
359b8e80941Smrg
360b8e80941Smrgstatic GLbitfield api_init_mask = 0x0;
361b8e80941Smrgstatic void __attribute__((__destructor__))
362b8e80941Smrgone_time_fini(void)
363b8e80941Smrg{
364b8e80941Smrg   if (api_init_mask) {
365b8e80941Smrg      _mesa_destroy_shader_compiler();
366b8e80941Smrg      _mesa_destroy_shader_compiler_types();
367b8e80941Smrg      _mesa_locale_fini();
368b8e80941Smrg   }
369b8e80941Smrg}
370848b8605Smrg
371848b8605Smrg/**
372848b8605Smrg * Calls all the various one-time-init functions in Mesa.
373848b8605Smrg *
374848b8605Smrg * While holding a global mutex lock, calls several initialization functions,
375848b8605Smrg * and sets the glapi callbacks if the \c MESA_DEBUG environment variable is
376848b8605Smrg * defined.
377848b8605Smrg *
378848b8605Smrg * \sa _math_init().
379848b8605Smrg */
380848b8605Smrgstatic void
381848b8605Smrgone_time_init( struct gl_context *ctx )
382848b8605Smrg{
383848b8605Smrg
384848b8605Smrg   mtx_lock(&OneTimeLock);
385848b8605Smrg
386848b8605Smrg   /* truly one-time init */
387848b8605Smrg   if (!api_init_mask) {
388848b8605Smrg      GLuint i;
389848b8605Smrg
390b8e80941Smrg      STATIC_ASSERT(sizeof(GLbyte) == 1);
391b8e80941Smrg      STATIC_ASSERT(sizeof(GLubyte) == 1);
392b8e80941Smrg      STATIC_ASSERT(sizeof(GLshort) == 2);
393b8e80941Smrg      STATIC_ASSERT(sizeof(GLushort) == 2);
394b8e80941Smrg      STATIC_ASSERT(sizeof(GLint) == 4);
395b8e80941Smrg      STATIC_ASSERT(sizeof(GLuint) == 4);
396b8e80941Smrg
397b8e80941Smrg      _mesa_locale_init();
398b8e80941Smrg
399b8e80941Smrg      _mesa_init_shader_compiler_types();
400848b8605Smrg
401b8e80941Smrg      _mesa_one_time_init_extension_overrides(ctx);
402848b8605Smrg
403848b8605Smrg      _mesa_get_cpu_features();
404848b8605Smrg
405848b8605Smrg      for (i = 0; i < 256; i++) {
406848b8605Smrg         _mesa_ubyte_to_float_color_tab[i] = (float) i / 255.0F;
407848b8605Smrg      }
408848b8605Smrg
409b8e80941Smrg#if defined(DEBUG)
410848b8605Smrg      if (MESA_VERBOSE != 0) {
411b8e80941Smrg         _mesa_debug(ctx, "Mesa " PACKAGE_VERSION " DEBUG build" MESA_GIT_SHA1 "\n");
412848b8605Smrg      }
413848b8605Smrg#endif
414848b8605Smrg   }
415848b8605Smrg
416848b8605Smrg   /* per-API one-time init */
417848b8605Smrg   if (!(api_init_mask & (1 << ctx->API))) {
418848b8605Smrg      _mesa_init_remap_table();
419848b8605Smrg   }
420848b8605Smrg
421848b8605Smrg   api_init_mask |= 1 << ctx->API;
422848b8605Smrg
423848b8605Smrg   mtx_unlock(&OneTimeLock);
424848b8605Smrg}
425848b8605Smrg
426848b8605Smrg
427848b8605Smrg/**
428848b8605Smrg * Initialize fields of gl_current_attrib (aka ctx->Current.*)
429848b8605Smrg */
430848b8605Smrgstatic void
431848b8605Smrg_mesa_init_current(struct gl_context *ctx)
432848b8605Smrg{
433848b8605Smrg   GLuint i;
434848b8605Smrg
435848b8605Smrg   /* Init all to (0,0,0,1) */
436b8e80941Smrg   for (i = 0; i < ARRAY_SIZE(ctx->Current.Attrib); i++) {
437848b8605Smrg      ASSIGN_4V( ctx->Current.Attrib[i], 0.0, 0.0, 0.0, 1.0 );
438848b8605Smrg   }
439848b8605Smrg
440848b8605Smrg   /* redo special cases: */
441848b8605Smrg   ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_NORMAL], 0.0, 0.0, 1.0, 1.0 );
442848b8605Smrg   ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR0], 1.0, 1.0, 1.0, 1.0 );
443848b8605Smrg   ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR1], 0.0, 0.0, 0.0, 1.0 );
444848b8605Smrg   ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_COLOR_INDEX], 1.0, 0.0, 0.0, 1.0 );
445848b8605Smrg   ASSIGN_4V( ctx->Current.Attrib[VERT_ATTRIB_EDGEFLAG], 1.0, 0.0, 0.0, 1.0 );
446848b8605Smrg}
447848b8605Smrg
448848b8605Smrg
449848b8605Smrg/**
450848b8605Smrg * Init vertex/fragment/geometry program limits.
451848b8605Smrg * Important: drivers should override these with actual limits.
452848b8605Smrg */
453848b8605Smrgstatic void
454848b8605Smrginit_program_limits(struct gl_constants *consts, gl_shader_stage stage,
455848b8605Smrg                    struct gl_program_constants *prog)
456848b8605Smrg{
457848b8605Smrg   prog->MaxInstructions = MAX_PROGRAM_INSTRUCTIONS;
458848b8605Smrg   prog->MaxAluInstructions = MAX_PROGRAM_INSTRUCTIONS;
459848b8605Smrg   prog->MaxTexInstructions = MAX_PROGRAM_INSTRUCTIONS;
460848b8605Smrg   prog->MaxTexIndirections = MAX_PROGRAM_INSTRUCTIONS;
461848b8605Smrg   prog->MaxTemps = MAX_PROGRAM_TEMPS;
462848b8605Smrg   prog->MaxEnvParams = MAX_PROGRAM_ENV_PARAMS;
463848b8605Smrg   prog->MaxLocalParams = MAX_PROGRAM_LOCAL_PARAMS;
464848b8605Smrg   prog->MaxAddressOffset = MAX_PROGRAM_LOCAL_PARAMS;
465848b8605Smrg
466848b8605Smrg   switch (stage) {
467848b8605Smrg   case MESA_SHADER_VERTEX:
468848b8605Smrg      prog->MaxParameters = MAX_VERTEX_PROGRAM_PARAMS;
469848b8605Smrg      prog->MaxAttribs = MAX_VERTEX_GENERIC_ATTRIBS;
470848b8605Smrg      prog->MaxAddressRegs = MAX_VERTEX_PROGRAM_ADDRESS_REGS;
471848b8605Smrg      prog->MaxUniformComponents = 4 * MAX_UNIFORMS;
472848b8605Smrg      prog->MaxInputComponents = 0; /* value not used */
473848b8605Smrg      prog->MaxOutputComponents = 16 * 4; /* old limit not to break tnl and swrast */
474848b8605Smrg      break;
475848b8605Smrg   case MESA_SHADER_FRAGMENT:
476b8e80941Smrg      prog->MaxParameters = MAX_FRAGMENT_PROGRAM_PARAMS;
477b8e80941Smrg      prog->MaxAttribs = MAX_FRAGMENT_PROGRAM_INPUTS;
478848b8605Smrg      prog->MaxAddressRegs = MAX_FRAGMENT_PROGRAM_ADDRESS_REGS;
479848b8605Smrg      prog->MaxUniformComponents = 4 * MAX_UNIFORMS;
480848b8605Smrg      prog->MaxInputComponents = 16 * 4; /* old limit not to break tnl and swrast */
481848b8605Smrg      prog->MaxOutputComponents = 0; /* value not used */
482848b8605Smrg      break;
483b8e80941Smrg   case MESA_SHADER_TESS_CTRL:
484b8e80941Smrg   case MESA_SHADER_TESS_EVAL:
485848b8605Smrg   case MESA_SHADER_GEOMETRY:
486848b8605Smrg      prog->MaxParameters = MAX_VERTEX_PROGRAM_PARAMS;
487848b8605Smrg      prog->MaxAttribs = MAX_VERTEX_GENERIC_ATTRIBS;
488848b8605Smrg      prog->MaxAddressRegs = MAX_VERTEX_PROGRAM_ADDRESS_REGS;
489848b8605Smrg      prog->MaxUniformComponents = 4 * MAX_UNIFORMS;
490848b8605Smrg      prog->MaxInputComponents = 16 * 4; /* old limit not to break tnl and swrast */
491848b8605Smrg      prog->MaxOutputComponents = 16 * 4; /* old limit not to break tnl and swrast */
492848b8605Smrg      break;
493848b8605Smrg   case MESA_SHADER_COMPUTE:
494848b8605Smrg      prog->MaxParameters = 0; /* not meaningful for compute shaders */
495848b8605Smrg      prog->MaxAttribs = 0; /* not meaningful for compute shaders */
496848b8605Smrg      prog->MaxAddressRegs = 0; /* not meaningful for compute shaders */
497848b8605Smrg      prog->MaxUniformComponents = 4 * MAX_UNIFORMS;
498848b8605Smrg      prog->MaxInputComponents = 0; /* not meaningful for compute shaders */
499848b8605Smrg      prog->MaxOutputComponents = 0; /* not meaningful for compute shaders */
500848b8605Smrg      break;
501848b8605Smrg   default:
502848b8605Smrg      assert(0 && "Bad shader stage in init_program_limits()");
503848b8605Smrg   }
504848b8605Smrg
505848b8605Smrg   /* Set the native limits to zero.  This implies that there is no native
506848b8605Smrg    * support for shaders.  Let the drivers fill in the actual values.
507848b8605Smrg    */
508848b8605Smrg   prog->MaxNativeInstructions = 0;
509848b8605Smrg   prog->MaxNativeAluInstructions = 0;
510848b8605Smrg   prog->MaxNativeTexInstructions = 0;
511848b8605Smrg   prog->MaxNativeTexIndirections = 0;
512848b8605Smrg   prog->MaxNativeAttribs = 0;
513848b8605Smrg   prog->MaxNativeTemps = 0;
514848b8605Smrg   prog->MaxNativeAddressRegs = 0;
515848b8605Smrg   prog->MaxNativeParameters = 0;
516848b8605Smrg
517848b8605Smrg   /* Set GLSL datatype range/precision info assuming IEEE float values.
518848b8605Smrg    * Drivers should override these defaults as needed.
519848b8605Smrg    */
520848b8605Smrg   prog->MediumFloat.RangeMin = 127;
521848b8605Smrg   prog->MediumFloat.RangeMax = 127;
522848b8605Smrg   prog->MediumFloat.Precision = 23;
523848b8605Smrg   prog->LowFloat = prog->HighFloat = prog->MediumFloat;
524848b8605Smrg
525848b8605Smrg   /* Assume ints are stored as floats for now, since this is the least-common
526848b8605Smrg    * denominator.  The OpenGL ES spec implies (page 132) that the precision
527848b8605Smrg    * of integer types should be 0.  Practically speaking, IEEE
528848b8605Smrg    * single-precision floating point values can only store integers in the
529848b8605Smrg    * range [-0x01000000, 0x01000000] without loss of precision.
530848b8605Smrg    */
531848b8605Smrg   prog->MediumInt.RangeMin = 24;
532848b8605Smrg   prog->MediumInt.RangeMax = 24;
533848b8605Smrg   prog->MediumInt.Precision = 0;
534848b8605Smrg   prog->LowInt = prog->HighInt = prog->MediumInt;
535848b8605Smrg
536848b8605Smrg   prog->MaxUniformBlocks = 12;
537848b8605Smrg   prog->MaxCombinedUniformComponents = (prog->MaxUniformComponents +
538848b8605Smrg                                         consts->MaxUniformBlockSize / 4 *
539848b8605Smrg                                         prog->MaxUniformBlocks);
540848b8605Smrg
541848b8605Smrg   prog->MaxAtomicBuffers = 0;
542848b8605Smrg   prog->MaxAtomicCounters = 0;
543b8e80941Smrg
544b8e80941Smrg   prog->MaxShaderStorageBlocks = 8;
545848b8605Smrg}
546848b8605Smrg
547848b8605Smrg
548848b8605Smrg/**
549848b8605Smrg * Initialize fields of gl_constants (aka ctx->Const.*).
550848b8605Smrg * Use defaults from config.h.  The device drivers will often override
551848b8605Smrg * some of these values (such as number of texture units).
552848b8605Smrg */
553848b8605Smrgvoid
554848b8605Smrg_mesa_init_constants(struct gl_constants *consts, gl_api api)
555848b8605Smrg{
556848b8605Smrg   int i;
557848b8605Smrg   assert(consts);
558848b8605Smrg
559848b8605Smrg   /* Constants, may be overriden (usually only reduced) by device drivers */
560848b8605Smrg   consts->MaxTextureMbytes = MAX_TEXTURE_MBYTES;
561848b8605Smrg   consts->MaxTextureLevels = MAX_TEXTURE_LEVELS;
562848b8605Smrg   consts->Max3DTextureLevels = MAX_3D_TEXTURE_LEVELS;
563848b8605Smrg   consts->MaxCubeTextureLevels = MAX_CUBE_TEXTURE_LEVELS;
564848b8605Smrg   consts->MaxTextureRectSize = MAX_TEXTURE_RECT_SIZE;
565848b8605Smrg   consts->MaxArrayTextureLayers = MAX_ARRAY_TEXTURE_LAYERS;
566848b8605Smrg   consts->MaxTextureCoordUnits = MAX_TEXTURE_COORD_UNITS;
567848b8605Smrg   consts->Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = MAX_TEXTURE_IMAGE_UNITS;
568848b8605Smrg   consts->MaxTextureUnits = MIN2(consts->MaxTextureCoordUnits,
569848b8605Smrg                                     consts->Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits);
570848b8605Smrg   consts->MaxTextureMaxAnisotropy = MAX_TEXTURE_MAX_ANISOTROPY;
571848b8605Smrg   consts->MaxTextureLodBias = MAX_TEXTURE_LOD_BIAS;
572848b8605Smrg   consts->MaxTextureBufferSize = 65536;
573848b8605Smrg   consts->TextureBufferOffsetAlignment = 1;
574848b8605Smrg   consts->MaxArrayLockSize = MAX_ARRAY_LOCK_SIZE;
575848b8605Smrg   consts->SubPixelBits = SUB_PIXEL_BITS;
576848b8605Smrg   consts->MinPointSize = MIN_POINT_SIZE;
577848b8605Smrg   consts->MaxPointSize = MAX_POINT_SIZE;
578848b8605Smrg   consts->MinPointSizeAA = MIN_POINT_SIZE;
579848b8605Smrg   consts->MaxPointSizeAA = MAX_POINT_SIZE;
580848b8605Smrg   consts->PointSizeGranularity = (GLfloat) POINT_SIZE_GRANULARITY;
581848b8605Smrg   consts->MinLineWidth = MIN_LINE_WIDTH;
582848b8605Smrg   consts->MaxLineWidth = MAX_LINE_WIDTH;
583848b8605Smrg   consts->MinLineWidthAA = MIN_LINE_WIDTH;
584848b8605Smrg   consts->MaxLineWidthAA = MAX_LINE_WIDTH;
585848b8605Smrg   consts->LineWidthGranularity = (GLfloat) LINE_WIDTH_GRANULARITY;
586848b8605Smrg   consts->MaxClipPlanes = 6;
587848b8605Smrg   consts->MaxLights = MAX_LIGHTS;
588848b8605Smrg   consts->MaxShininess = 128.0;
589848b8605Smrg   consts->MaxSpotExponent = 128.0;
590b8e80941Smrg   consts->MaxViewportWidth = 16384;
591b8e80941Smrg   consts->MaxViewportHeight = 16384;
592848b8605Smrg   consts->MinMapBufferAlignment = 64;
593848b8605Smrg
594848b8605Smrg   /* Driver must override these values if ARB_viewport_array is supported. */
595848b8605Smrg   consts->MaxViewports = 1;
596848b8605Smrg   consts->ViewportSubpixelBits = 0;
597848b8605Smrg   consts->ViewportBounds.Min = 0;
598848b8605Smrg   consts->ViewportBounds.Max = 0;
599848b8605Smrg
600848b8605Smrg   /** GL_ARB_uniform_buffer_object */
601848b8605Smrg   consts->MaxCombinedUniformBlocks = 36;
602848b8605Smrg   consts->MaxUniformBufferBindings = 36;
603848b8605Smrg   consts->MaxUniformBlockSize = 16384;
604848b8605Smrg   consts->UniformBufferOffsetAlignment = 1;
605848b8605Smrg
606b8e80941Smrg   /** GL_ARB_shader_storage_buffer_object */
607b8e80941Smrg   consts->MaxCombinedShaderStorageBlocks = 8;
608b8e80941Smrg   consts->MaxShaderStorageBufferBindings = 8;
609b8e80941Smrg   consts->MaxShaderStorageBlockSize = 128 * 1024 * 1024; /* 2^27 */
610b8e80941Smrg   consts->ShaderStorageBufferOffsetAlignment = 256;
611b8e80941Smrg
612848b8605Smrg   /* GL_ARB_explicit_uniform_location, GL_MAX_UNIFORM_LOCATIONS */
613848b8605Smrg   consts->MaxUserAssignableUniformLocations =
614848b8605Smrg      4 * MESA_SHADER_STAGES * MAX_UNIFORMS;
615848b8605Smrg
616848b8605Smrg   for (i = 0; i < MESA_SHADER_STAGES; i++)
617848b8605Smrg      init_program_limits(consts, i, &consts->Program[i]);
618848b8605Smrg
619848b8605Smrg   consts->MaxProgramMatrices = MAX_PROGRAM_MATRICES;
620848b8605Smrg   consts->MaxProgramMatrixStackDepth = MAX_PROGRAM_MATRIX_STACK_DEPTH;
621848b8605Smrg
622b8e80941Smrg   /* Set the absolute minimum possible GLSL version.  API_OPENGL_CORE can
623b8e80941Smrg    * mean an OpenGL 3.0 forward-compatible context, so that implies a minimum
624b8e80941Smrg    * possible version of 1.30.  Otherwise, the minimum possible version 1.20.
625b8e80941Smrg    * Since Mesa unconditionally advertises GL_ARB_shading_language_100 and
626b8e80941Smrg    * GL_ARB_shader_objects, every driver has GLSL 1.20... even if they don't
627b8e80941Smrg    * advertise any extensions to enable any shader stages (e.g.,
628b8e80941Smrg    * GL_ARB_vertex_shader).
629b8e80941Smrg    */
630b8e80941Smrg   consts->GLSLVersion = api == API_OPENGL_CORE ? 130 : 120;
631b8e80941Smrg   consts->GLSLVersionCompat = consts->GLSLVersion;
632b8e80941Smrg
633848b8605Smrg   /* Assume that if GLSL 1.30+ (or GLSL ES 3.00+) is supported that
634848b8605Smrg    * gl_VertexID is implemented using a native hardware register with OpenGL
635848b8605Smrg    * semantics.
636848b8605Smrg    */
637848b8605Smrg   consts->VertexID_is_zero_based = false;
638848b8605Smrg
639848b8605Smrg   /* GL_ARB_draw_buffers */
640848b8605Smrg   consts->MaxDrawBuffers = MAX_DRAW_BUFFERS;
641848b8605Smrg
642848b8605Smrg   consts->MaxColorAttachments = MAX_COLOR_ATTACHMENTS;
643848b8605Smrg   consts->MaxRenderbufferSize = MAX_RENDERBUFFER_SIZE;
644848b8605Smrg
645848b8605Smrg   consts->Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = MAX_TEXTURE_IMAGE_UNITS;
646848b8605Smrg   consts->MaxCombinedTextureImageUnits = MAX_COMBINED_TEXTURE_IMAGE_UNITS;
647848b8605Smrg   consts->MaxVarying = 16; /* old limit not to break tnl and swrast */
648848b8605Smrg   consts->Program[MESA_SHADER_GEOMETRY].MaxTextureImageUnits = MAX_TEXTURE_IMAGE_UNITS;
649848b8605Smrg   consts->MaxGeometryOutputVertices = MAX_GEOMETRY_OUTPUT_VERTICES;
650848b8605Smrg   consts->MaxGeometryTotalOutputComponents = MAX_GEOMETRY_TOTAL_OUTPUT_COMPONENTS;
651b8e80941Smrg   consts->MaxGeometryShaderInvocations = MAX_GEOMETRY_SHADER_INVOCATIONS;
652848b8605Smrg
653b8e80941Smrg#ifdef DEBUG
654b8e80941Smrg   consts->GenerateTemporaryNames = true;
655b8e80941Smrg#else
656b8e80941Smrg   consts->GenerateTemporaryNames = false;
657b8e80941Smrg#endif
658848b8605Smrg
659848b8605Smrg   /* GL_ARB_framebuffer_object */
660848b8605Smrg   consts->MaxSamples = 0;
661848b8605Smrg
662848b8605Smrg   /* GLSL default if NativeIntegers == FALSE */
663b8e80941Smrg   consts->UniformBooleanTrue = FLOAT_AS_UNION(1.0f).u;
664848b8605Smrg
665848b8605Smrg   /* GL_ARB_sync */
666b8e80941Smrg   consts->MaxServerWaitTimeout = 0x7fffffff7fffffffULL;
667848b8605Smrg
668848b8605Smrg   /* GL_EXT_provoking_vertex */
669848b8605Smrg   consts->QuadsFollowProvokingVertexConvention = GL_TRUE;
670848b8605Smrg
671b8e80941Smrg   /** GL_ARB_viewport_array */
672b8e80941Smrg   consts->LayerAndVPIndexProvokingVertex = GL_UNDEFINED_VERTEX;
673b8e80941Smrg
674848b8605Smrg   /* GL_EXT_transform_feedback */
675848b8605Smrg   consts->MaxTransformFeedbackBuffers = MAX_FEEDBACK_BUFFERS;
676848b8605Smrg   consts->MaxTransformFeedbackSeparateComponents = 4 * MAX_FEEDBACK_ATTRIBS;
677848b8605Smrg   consts->MaxTransformFeedbackInterleavedComponents = 4 * MAX_FEEDBACK_ATTRIBS;
678848b8605Smrg   consts->MaxVertexStreams = 1;
679848b8605Smrg
680848b8605Smrg   /* GL 3.2  */
681848b8605Smrg   consts->ProfileMask = api == API_OPENGL_CORE
682848b8605Smrg                          ? GL_CONTEXT_CORE_PROFILE_BIT
683848b8605Smrg                          : GL_CONTEXT_COMPATIBILITY_PROFILE_BIT;
684848b8605Smrg
685b8e80941Smrg   /* GL 4.4 */
686b8e80941Smrg   consts->MaxVertexAttribStride = 2048;
687b8e80941Smrg
688848b8605Smrg   /** GL_EXT_gpu_shader4 */
689848b8605Smrg   consts->MinProgramTexelOffset = -8;
690848b8605Smrg   consts->MaxProgramTexelOffset = 7;
691848b8605Smrg
692848b8605Smrg   /* GL_ARB_texture_gather */
693848b8605Smrg   consts->MinProgramTextureGatherOffset = -8;
694848b8605Smrg   consts->MaxProgramTextureGatherOffset = 7;
695848b8605Smrg
696848b8605Smrg   /* GL_ARB_robustness */
697848b8605Smrg   consts->ResetStrategy = GL_NO_RESET_NOTIFICATION_ARB;
698848b8605Smrg
699b8e80941Smrg   /* GL_KHR_robustness */
700b8e80941Smrg   consts->RobustAccess = GL_FALSE;
701848b8605Smrg
702848b8605Smrg   /* ES 3.0 or ARB_ES3_compatibility */
703848b8605Smrg   consts->MaxElementIndex = 0xffffffffu;
704848b8605Smrg
705848b8605Smrg   /* GL_ARB_texture_multisample */
706848b8605Smrg   consts->MaxColorTextureSamples = 1;
707848b8605Smrg   consts->MaxDepthTextureSamples = 1;
708848b8605Smrg   consts->MaxIntegerSamples = 1;
709848b8605Smrg
710848b8605Smrg   /* GL_ARB_shader_atomic_counters */
711848b8605Smrg   consts->MaxAtomicBufferBindings = MAX_COMBINED_ATOMIC_BUFFERS;
712848b8605Smrg   consts->MaxAtomicBufferSize = MAX_ATOMIC_COUNTERS * ATOMIC_COUNTER_SIZE;
713848b8605Smrg   consts->MaxCombinedAtomicBuffers = MAX_COMBINED_ATOMIC_BUFFERS;
714848b8605Smrg   consts->MaxCombinedAtomicCounters = MAX_ATOMIC_COUNTERS;
715848b8605Smrg
716848b8605Smrg   /* GL_ARB_vertex_attrib_binding */
717848b8605Smrg   consts->MaxVertexAttribRelativeOffset = 2047;
718848b8605Smrg   consts->MaxVertexAttribBindings = MAX_VERTEX_GENERIC_ATTRIBS;
719848b8605Smrg
720848b8605Smrg   /* GL_ARB_compute_shader */
721848b8605Smrg   consts->MaxComputeWorkGroupCount[0] = 65535;
722848b8605Smrg   consts->MaxComputeWorkGroupCount[1] = 65535;
723848b8605Smrg   consts->MaxComputeWorkGroupCount[2] = 65535;
724848b8605Smrg   consts->MaxComputeWorkGroupSize[0] = 1024;
725848b8605Smrg   consts->MaxComputeWorkGroupSize[1] = 1024;
726848b8605Smrg   consts->MaxComputeWorkGroupSize[2] = 64;
727b8e80941Smrg   /* Enables compute support for GLES 3.1 if >= 128 */
728b8e80941Smrg   consts->MaxComputeWorkGroupInvocations = 0;
729848b8605Smrg
730848b8605Smrg   /** GL_ARB_gpu_shader5 */
731848b8605Smrg   consts->MinFragmentInterpolationOffset = MIN_FRAGMENT_INTERPOLATION_OFFSET;
732848b8605Smrg   consts->MaxFragmentInterpolationOffset = MAX_FRAGMENT_INTERPOLATION_OFFSET;
733b8e80941Smrg
734b8e80941Smrg   /** GL_KHR_context_flush_control */
735b8e80941Smrg   consts->ContextReleaseBehavior = GL_CONTEXT_RELEASE_BEHAVIOR_FLUSH;
736b8e80941Smrg
737b8e80941Smrg   /** GL_ARB_tessellation_shader */
738b8e80941Smrg   consts->MaxTessGenLevel = MAX_TESS_GEN_LEVEL;
739b8e80941Smrg   consts->MaxPatchVertices = MAX_PATCH_VERTICES;
740b8e80941Smrg   consts->Program[MESA_SHADER_TESS_CTRL].MaxTextureImageUnits = MAX_TEXTURE_IMAGE_UNITS;
741b8e80941Smrg   consts->Program[MESA_SHADER_TESS_EVAL].MaxTextureImageUnits = MAX_TEXTURE_IMAGE_UNITS;
742b8e80941Smrg   consts->MaxTessPatchComponents = MAX_TESS_PATCH_COMPONENTS;
743b8e80941Smrg   consts->MaxTessControlTotalOutputComponents = MAX_TESS_CONTROL_TOTAL_OUTPUT_COMPONENTS;
744b8e80941Smrg   consts->PrimitiveRestartForPatches = false;
745b8e80941Smrg
746b8e80941Smrg   /** GL_ARB_compute_variable_group_size */
747b8e80941Smrg   consts->MaxComputeVariableGroupSize[0] = 512;
748b8e80941Smrg   consts->MaxComputeVariableGroupSize[1] = 512;
749b8e80941Smrg   consts->MaxComputeVariableGroupSize[2] = 64;
750b8e80941Smrg   consts->MaxComputeVariableGroupInvocations = 512;
751b8e80941Smrg
752b8e80941Smrg   /** GL_NV_conservative_raster */
753b8e80941Smrg   consts->MaxSubpixelPrecisionBiasBits = 0;
754b8e80941Smrg
755b8e80941Smrg   /** GL_NV_conservative_raster_dilate */
756b8e80941Smrg   consts->ConservativeRasterDilateRange[0] = 0.0;
757b8e80941Smrg   consts->ConservativeRasterDilateRange[1] = 0.0;
758b8e80941Smrg   consts->ConservativeRasterDilateGranularity = 0.0;
759848b8605Smrg}
760848b8605Smrg
761848b8605Smrg
762848b8605Smrg/**
763848b8605Smrg * Do some sanity checks on the limits/constants for the given context.
764848b8605Smrg * Only called the first time a context is bound.
765848b8605Smrg */
766848b8605Smrgstatic void
767848b8605Smrgcheck_context_limits(struct gl_context *ctx)
768848b8605Smrg{
769b8e80941Smrg   (void) ctx;
770b8e80941Smrg
771848b8605Smrg   /* check that we don't exceed the size of various bitfields */
772848b8605Smrg   assert(VARYING_SLOT_MAX <=
773b8e80941Smrg          (8 * sizeof(ctx->VertexProgram._Current->info.outputs_written)));
774848b8605Smrg   assert(VARYING_SLOT_MAX <=
775b8e80941Smrg          (8 * sizeof(ctx->FragmentProgram._Current->info.inputs_read)));
776848b8605Smrg
777848b8605Smrg   /* shader-related checks */
778848b8605Smrg   assert(ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxLocalParams <= MAX_PROGRAM_LOCAL_PARAMS);
779848b8605Smrg   assert(ctx->Const.Program[MESA_SHADER_VERTEX].MaxLocalParams <= MAX_PROGRAM_LOCAL_PARAMS);
780848b8605Smrg
781848b8605Smrg   /* Texture unit checks */
782848b8605Smrg   assert(ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits > 0);
783848b8605Smrg   assert(ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits <= MAX_TEXTURE_IMAGE_UNITS);
784848b8605Smrg   assert(ctx->Const.MaxTextureCoordUnits > 0);
785848b8605Smrg   assert(ctx->Const.MaxTextureCoordUnits <= MAX_TEXTURE_COORD_UNITS);
786848b8605Smrg   assert(ctx->Const.MaxTextureUnits > 0);
787848b8605Smrg   assert(ctx->Const.MaxTextureUnits <= MAX_TEXTURE_IMAGE_UNITS);
788848b8605Smrg   assert(ctx->Const.MaxTextureUnits <= MAX_TEXTURE_COORD_UNITS);
789848b8605Smrg   assert(ctx->Const.MaxTextureUnits == MIN2(ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits,
790848b8605Smrg                                             ctx->Const.MaxTextureCoordUnits));
791848b8605Smrg   assert(ctx->Const.MaxCombinedTextureImageUnits > 0);
792848b8605Smrg   assert(ctx->Const.MaxCombinedTextureImageUnits <= MAX_COMBINED_TEXTURE_IMAGE_UNITS);
793848b8605Smrg   assert(ctx->Const.MaxTextureCoordUnits <= MAX_COMBINED_TEXTURE_IMAGE_UNITS);
794848b8605Smrg   /* number of coord units cannot be greater than number of image units */
795848b8605Smrg   assert(ctx->Const.MaxTextureCoordUnits <= ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits);
796848b8605Smrg
797848b8605Smrg
798848b8605Smrg   /* Texture size checks */
799848b8605Smrg   assert(ctx->Const.MaxTextureLevels <= MAX_TEXTURE_LEVELS);
800848b8605Smrg   assert(ctx->Const.Max3DTextureLevels <= MAX_3D_TEXTURE_LEVELS);
801848b8605Smrg   assert(ctx->Const.MaxCubeTextureLevels <= MAX_CUBE_TEXTURE_LEVELS);
802848b8605Smrg   assert(ctx->Const.MaxTextureRectSize <= MAX_TEXTURE_RECT_SIZE);
803848b8605Smrg
804848b8605Smrg   /* Texture level checks */
805848b8605Smrg   assert(MAX_TEXTURE_LEVELS >= MAX_3D_TEXTURE_LEVELS);
806848b8605Smrg   assert(MAX_TEXTURE_LEVELS >= MAX_CUBE_TEXTURE_LEVELS);
807848b8605Smrg
808848b8605Smrg   /* Max texture size should be <= max viewport size (render to texture) */
809848b8605Smrg   assert((1U << (ctx->Const.MaxTextureLevels - 1))
810848b8605Smrg          <= ctx->Const.MaxViewportWidth);
811848b8605Smrg   assert((1U << (ctx->Const.MaxTextureLevels - 1))
812848b8605Smrg          <= ctx->Const.MaxViewportHeight);
813848b8605Smrg
814848b8605Smrg   assert(ctx->Const.MaxDrawBuffers <= MAX_DRAW_BUFFERS);
815848b8605Smrg
816848b8605Smrg   /* if this fails, add more enum values to gl_buffer_index */
817848b8605Smrg   assert(BUFFER_COLOR0 + MAX_DRAW_BUFFERS <= BUFFER_COUNT);
818848b8605Smrg
819848b8605Smrg   /* XXX probably add more tests */
820848b8605Smrg}
821848b8605Smrg
822848b8605Smrg
823848b8605Smrg/**
824848b8605Smrg * Initialize the attribute groups in a GL context.
825848b8605Smrg *
826848b8605Smrg * \param ctx GL context.
827848b8605Smrg *
828848b8605Smrg * Initializes all the attributes, calling the respective <tt>init*</tt>
829848b8605Smrg * functions for the more complex data structures.
830848b8605Smrg */
831848b8605Smrgstatic GLboolean
832848b8605Smrginit_attrib_groups(struct gl_context *ctx)
833848b8605Smrg{
834848b8605Smrg   assert(ctx);
835848b8605Smrg
836848b8605Smrg   /* Constants */
837848b8605Smrg   _mesa_init_constants(&ctx->Const, ctx->API);
838848b8605Smrg
839848b8605Smrg   /* Extensions */
840848b8605Smrg   _mesa_init_extensions(&ctx->Extensions);
841848b8605Smrg
842848b8605Smrg   /* Attribute Groups */
843848b8605Smrg   _mesa_init_accum( ctx );
844848b8605Smrg   _mesa_init_attrib( ctx );
845b8e80941Smrg   _mesa_init_bbox( ctx );
846848b8605Smrg   _mesa_init_buffer_objects( ctx );
847848b8605Smrg   _mesa_init_color( ctx );
848b8e80941Smrg   _mesa_init_conservative_raster( ctx );
849848b8605Smrg   _mesa_init_current( ctx );
850848b8605Smrg   _mesa_init_depth( ctx );
851848b8605Smrg   _mesa_init_debug( ctx );
852b8e80941Smrg   _mesa_init_debug_output( ctx );
853848b8605Smrg   _mesa_init_display_list( ctx );
854848b8605Smrg   _mesa_init_eval( ctx );
855848b8605Smrg   _mesa_init_fbobjects( ctx );
856848b8605Smrg   _mesa_init_feedback( ctx );
857848b8605Smrg   _mesa_init_fog( ctx );
858848b8605Smrg   _mesa_init_hint( ctx );
859b8e80941Smrg   _mesa_init_image_units( ctx );
860848b8605Smrg   _mesa_init_line( ctx );
861848b8605Smrg   _mesa_init_lighting( ctx );
862848b8605Smrg   _mesa_init_matrix( ctx );
863848b8605Smrg   _mesa_init_multisample( ctx );
864848b8605Smrg   _mesa_init_performance_monitors( ctx );
865b8e80941Smrg   _mesa_init_performance_queries( ctx );
866848b8605Smrg   _mesa_init_pipeline( ctx );
867848b8605Smrg   _mesa_init_pixel( ctx );
868848b8605Smrg   _mesa_init_pixelstore( ctx );
869848b8605Smrg   _mesa_init_point( ctx );
870848b8605Smrg   _mesa_init_polygon( ctx );
871848b8605Smrg   _mesa_init_program( ctx );
872848b8605Smrg   _mesa_init_queryobj( ctx );
873848b8605Smrg   _mesa_init_sync( ctx );
874848b8605Smrg   _mesa_init_rastpos( ctx );
875848b8605Smrg   _mesa_init_scissor( ctx );
876848b8605Smrg   _mesa_init_shader_state( ctx );
877848b8605Smrg   _mesa_init_stencil( ctx );
878848b8605Smrg   _mesa_init_transform( ctx );
879848b8605Smrg   _mesa_init_transform_feedback( ctx );
880848b8605Smrg   _mesa_init_varray( ctx );
881848b8605Smrg   _mesa_init_viewport( ctx );
882b8e80941Smrg   _mesa_init_resident_handles( ctx );
883848b8605Smrg
884848b8605Smrg   if (!_mesa_init_texture( ctx ))
885848b8605Smrg      return GL_FALSE;
886848b8605Smrg
887848b8605Smrg   /* Miscellaneous */
888b8e80941Smrg   ctx->TileRasterOrderIncreasingX = GL_TRUE;
889b8e80941Smrg   ctx->TileRasterOrderIncreasingY = GL_TRUE;
890848b8605Smrg   ctx->NewState = _NEW_ALL;
891848b8605Smrg   ctx->NewDriverState = ~0;
892848b8605Smrg   ctx->ErrorValue = GL_NO_ERROR;
893848b8605Smrg   ctx->ShareGroupReset = false;
894848b8605Smrg   ctx->varying_vp_inputs = VERT_BIT_ALL;
895848b8605Smrg
896848b8605Smrg   return GL_TRUE;
897848b8605Smrg}
898848b8605Smrg
899848b8605Smrg
900848b8605Smrg/**
901848b8605Smrg * Update default objects in a GL context with respect to shared state.
902848b8605Smrg *
903848b8605Smrg * \param ctx GL context.
904848b8605Smrg *
905848b8605Smrg * Removes references to old default objects, (texture objects, program
906848b8605Smrg * objects, etc.) and changes to reference those from the current shared
907848b8605Smrg * state.
908848b8605Smrg */
909848b8605Smrgstatic GLboolean
910848b8605Smrgupdate_default_objects(struct gl_context *ctx)
911848b8605Smrg{
912848b8605Smrg   assert(ctx);
913848b8605Smrg
914848b8605Smrg   _mesa_update_default_objects_program(ctx);
915848b8605Smrg   _mesa_update_default_objects_texture(ctx);
916848b8605Smrg   _mesa_update_default_objects_buffer_objects(ctx);
917848b8605Smrg
918848b8605Smrg   return GL_TRUE;
919848b8605Smrg}
920848b8605Smrg
921848b8605Smrg
922b8e80941Smrg/* XXX this is temporary and should be removed at some point in the
923b8e80941Smrg * future when there's a reasonable expectation that the libGL library
924b8e80941Smrg * contains the _glapi_new_nop_table() and _glapi_set_nop_handler()
925b8e80941Smrg * functions which were added in Mesa 10.6.
926b8e80941Smrg */
927b8e80941Smrg#if !defined(_WIN32)
928b8e80941Smrg/* Avoid libGL / driver ABI break */
929b8e80941Smrg#define USE_GLAPI_NOP_FEATURES 0
930b8e80941Smrg#else
931b8e80941Smrg#define USE_GLAPI_NOP_FEATURES 1
932b8e80941Smrg#endif
933b8e80941Smrg
934b8e80941Smrg
935848b8605Smrg/**
936b8e80941Smrg * This function is called by the glapi no-op functions.  For each OpenGL
937b8e80941Smrg * function/entrypoint there's a simple no-op function.  These "no-op"
938b8e80941Smrg * functions call this function.
939b8e80941Smrg *
940b8e80941Smrg * If there's a current OpenGL context for the calling thread, we record a
941b8e80941Smrg * GL_INVALID_OPERATION error.  This can happen either because the app's
942b8e80941Smrg * calling an unsupported extension function, or calling an illegal function
943b8e80941Smrg * (such as glClear between glBegin/glEnd).
944b8e80941Smrg *
945b8e80941Smrg * If there's no current OpenGL context for the calling thread, we can
946b8e80941Smrg * print a message to stderr.
947b8e80941Smrg *
948b8e80941Smrg * \param name  the name of the OpenGL function
949848b8605Smrg */
950b8e80941Smrg#if USE_GLAPI_NOP_FEATURES
951b8e80941Smrgstatic void
952b8e80941Smrgnop_handler(const char *name)
953848b8605Smrg{
954848b8605Smrg   GET_CURRENT_CONTEXT(ctx);
955b8e80941Smrg   if (ctx) {
956b8e80941Smrg      _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid call)", name);
957b8e80941Smrg   }
958b8e80941Smrg#if defined(DEBUG)
959b8e80941Smrg   else if (getenv("MESA_DEBUG") || getenv("LIBGL_DEBUG")) {
960b8e80941Smrg      fprintf(stderr,
961b8e80941Smrg              "GL User Error: gl%s called without a rendering context\n",
962b8e80941Smrg              name);
963b8e80941Smrg      fflush(stderr);
964b8e80941Smrg   }
965b8e80941Smrg#endif
966848b8605Smrg}
967b8e80941Smrg#endif
968848b8605Smrg
969848b8605Smrg
970848b8605Smrg/**
971848b8605Smrg * Special no-op glFlush, see below.
972848b8605Smrg */
973848b8605Smrg#if defined(_WIN32)
974848b8605Smrgstatic void GLAPIENTRY
975848b8605Smrgnop_glFlush(void)
976848b8605Smrg{
977b8e80941Smrg   /* don't record an error like we do in nop_handler() */
978b8e80941Smrg}
979b8e80941Smrg#endif
980b8e80941Smrg
981b8e80941Smrg
982b8e80941Smrg#if !USE_GLAPI_NOP_FEATURES
983b8e80941Smrgstatic int
984b8e80941Smrggeneric_nop(void)
985b8e80941Smrg{
986b8e80941Smrg   GET_CURRENT_CONTEXT(ctx);
987b8e80941Smrg   _mesa_error(ctx, GL_INVALID_OPERATION,
988b8e80941Smrg               "unsupported function called "
989b8e80941Smrg               "(unsupported extension or deprecated function?)");
990b8e80941Smrg   return 0;
991848b8605Smrg}
992848b8605Smrg#endif
993848b8605Smrg
994848b8605Smrg
995848b8605Smrg/**
996b8e80941Smrg * Create a new API dispatch table in which all entries point to the
997b8e80941Smrg * generic_nop() function.  This will not work on Windows because of
998b8e80941Smrg * the __stdcall convention which requires the callee to clean up the
999b8e80941Smrg * call stack.  That's impossible with one generic no-op function.
1000848b8605Smrg */
1001848b8605Smrgstruct _glapi_table *
1002b8e80941Smrg_mesa_new_nop_table(unsigned numEntries)
1003848b8605Smrg{
1004848b8605Smrg   struct _glapi_table *table;
1005848b8605Smrg
1006b8e80941Smrg#if !USE_GLAPI_NOP_FEATURES
1007848b8605Smrg   table = malloc(numEntries * sizeof(_glapi_proc));
1008848b8605Smrg   if (table) {
1009848b8605Smrg      _glapi_proc *entry = (_glapi_proc *) table;
1010b8e80941Smrg      unsigned i;
1011848b8605Smrg      for (i = 0; i < numEntries; i++) {
1012b8e80941Smrg         entry[i] = (_glapi_proc) generic_nop;
1013848b8605Smrg      }
1014b8e80941Smrg   }
1015b8e80941Smrg#else
1016b8e80941Smrg   table = _glapi_new_nop_table(numEntries);
1017b8e80941Smrg#endif
1018b8e80941Smrg   return table;
1019b8e80941Smrg}
1020b8e80941Smrg
1021b8e80941Smrg
1022b8e80941Smrg/**
1023b8e80941Smrg * Allocate and initialize a new dispatch table.  The table will be
1024b8e80941Smrg * populated with pointers to "no-op" functions.  In turn, the no-op
1025b8e80941Smrg * functions will call nop_handler() above.
1026b8e80941Smrg */
1027b8e80941Smrgstruct _glapi_table *
1028b8e80941Smrg_mesa_alloc_dispatch_table(void)
1029b8e80941Smrg{
1030b8e80941Smrg   /* Find the larger of Mesa's dispatch table and libGL's dispatch table.
1031b8e80941Smrg    * In practice, this'll be the same for stand-alone Mesa.  But for DRI
1032b8e80941Smrg    * Mesa we do this to accommodate different versions of libGL and various
1033b8e80941Smrg    * DRI drivers.
1034b8e80941Smrg    */
1035b8e80941Smrg   int numEntries = MAX2(_glapi_get_dispatch_table_size(), _gloffset_COUNT);
1036b8e80941Smrg
1037b8e80941Smrg   struct _glapi_table *table = _mesa_new_nop_table(numEntries);
1038848b8605Smrg
1039848b8605Smrg#if defined(_WIN32)
1040b8e80941Smrg   if (table) {
1041848b8605Smrg      /* This is a special case for Windows in the event that
1042848b8605Smrg       * wglGetProcAddress is called between glBegin/End().
1043848b8605Smrg       *
1044848b8605Smrg       * The MS opengl32.dll library apparently calls glFlush from
1045848b8605Smrg       * wglGetProcAddress().  If we're inside glBegin/End(), glFlush
1046848b8605Smrg       * will dispatch to _mesa_generic_nop() and we'll generate a
1047848b8605Smrg       * GL_INVALID_OPERATION error.
1048848b8605Smrg       *
1049848b8605Smrg       * The specific case which hits this is piglit's primitive-restart
1050848b8605Smrg       * test which calls glPrimitiveRestartNV() inside glBegin/End.  The
1051848b8605Smrg       * first time we call glPrimitiveRestartNV() Piglit's API dispatch
1052848b8605Smrg       * code will try to resolve the function by calling wglGetProcAddress.
1053848b8605Smrg       * This raises GL_INVALID_OPERATION and an assert(glGetError()==0)
1054848b8605Smrg       * will fail causing the test to fail.  By suppressing the error, the
1055848b8605Smrg       * assertion passes and the test continues.
1056848b8605Smrg       */
1057848b8605Smrg      SET_Flush(table, nop_glFlush);
1058848b8605Smrg   }
1059b8e80941Smrg#endif
1060b8e80941Smrg
1061b8e80941Smrg#if USE_GLAPI_NOP_FEATURES
1062b8e80941Smrg   _glapi_set_nop_handler(nop_handler);
1063b8e80941Smrg#endif
1064b8e80941Smrg
1065848b8605Smrg   return table;
1066848b8605Smrg}
1067848b8605Smrg
1068848b8605Smrg/**
1069848b8605Smrg * Creates a minimal dispatch table for use within glBegin()/glEnd().
1070848b8605Smrg *
1071848b8605Smrg * This ensures that we generate GL_INVALID_OPERATION errors from most
1072848b8605Smrg * functions, since the set of functions that are valid within Begin/End is
1073848b8605Smrg * very small.
1074848b8605Smrg *
1075848b8605Smrg * From the GL 1.0 specification section 2.6.3, "GL Commands within
1076848b8605Smrg * Begin/End"
1077848b8605Smrg *
1078848b8605Smrg *     "The only GL commands that are allowed within any Begin/End pairs are
1079848b8605Smrg *      the commands for specifying vertex coordinates, vertex color, normal
1080848b8605Smrg *      coordinates, and texture coordinates (Vertex, Color, Index, Normal,
1081848b8605Smrg *      TexCoord), EvalCoord and EvalPoint commands (see section 5.1),
1082848b8605Smrg *      commands for specifying lighting material parameters (Material
1083848b8605Smrg *      commands see section 2.12.2), display list invocation commands
1084848b8605Smrg *      (CallList and CallLists see section 5.4), and the EdgeFlag
1085848b8605Smrg *      command. Executing Begin after Begin has already been executed but
1086848b8605Smrg *      before an End is issued generates the INVALID OPERATION error, as does
1087848b8605Smrg *      executing End without a previous corresponding Begin. Executing any
1088848b8605Smrg *      other GL command within Begin/End results in the error INVALID
1089848b8605Smrg *      OPERATION."
1090848b8605Smrg *
1091848b8605Smrg * The table entries for specifying vertex attributes are set up by
1092848b8605Smrg * install_vtxfmt() and _mesa_loopback_init_api_table(), and End() and dlists
1093848b8605Smrg * are set by install_vtxfmt() as well.
1094848b8605Smrg */
1095848b8605Smrgstatic struct _glapi_table *
1096848b8605Smrgcreate_beginend_table(const struct gl_context *ctx)
1097848b8605Smrg{
1098848b8605Smrg   struct _glapi_table *table;
1099848b8605Smrg
1100848b8605Smrg   table = _mesa_alloc_dispatch_table();
1101848b8605Smrg   if (!table)
1102848b8605Smrg      return NULL;
1103848b8605Smrg
1104848b8605Smrg   /* Fill in functions which return a value, since they should return some
1105848b8605Smrg    * specific value even if they emit a GL_INVALID_OPERATION error from them
1106848b8605Smrg    * being called within glBegin()/glEnd().
1107848b8605Smrg    */
1108848b8605Smrg#define COPY_DISPATCH(func) SET_##func(table, GET_##func(ctx->Exec))
1109848b8605Smrg
1110848b8605Smrg   COPY_DISPATCH(GenLists);
1111848b8605Smrg   COPY_DISPATCH(IsProgram);
1112848b8605Smrg   COPY_DISPATCH(IsVertexArray);
1113848b8605Smrg   COPY_DISPATCH(IsBuffer);
1114848b8605Smrg   COPY_DISPATCH(IsEnabled);
1115848b8605Smrg   COPY_DISPATCH(IsEnabledi);
1116848b8605Smrg   COPY_DISPATCH(IsRenderbuffer);
1117848b8605Smrg   COPY_DISPATCH(IsFramebuffer);
1118848b8605Smrg   COPY_DISPATCH(CheckFramebufferStatus);
1119848b8605Smrg   COPY_DISPATCH(RenderMode);
1120848b8605Smrg   COPY_DISPATCH(GetString);
1121848b8605Smrg   COPY_DISPATCH(GetStringi);
1122848b8605Smrg   COPY_DISPATCH(GetPointerv);
1123848b8605Smrg   COPY_DISPATCH(IsQuery);
1124848b8605Smrg   COPY_DISPATCH(IsSampler);
1125848b8605Smrg   COPY_DISPATCH(IsSync);
1126848b8605Smrg   COPY_DISPATCH(IsTexture);
1127848b8605Smrg   COPY_DISPATCH(IsTransformFeedback);
1128848b8605Smrg   COPY_DISPATCH(DeleteQueries);
1129848b8605Smrg   COPY_DISPATCH(AreTexturesResident);
1130848b8605Smrg   COPY_DISPATCH(FenceSync);
1131848b8605Smrg   COPY_DISPATCH(ClientWaitSync);
1132848b8605Smrg   COPY_DISPATCH(MapBuffer);
1133848b8605Smrg   COPY_DISPATCH(UnmapBuffer);
1134848b8605Smrg   COPY_DISPATCH(MapBufferRange);
1135848b8605Smrg   COPY_DISPATCH(ObjectPurgeableAPPLE);
1136848b8605Smrg   COPY_DISPATCH(ObjectUnpurgeableAPPLE);
1137848b8605Smrg
1138848b8605Smrg   _mesa_loopback_init_api_table(ctx, table);
1139848b8605Smrg
1140848b8605Smrg   return table;
1141848b8605Smrg}
1142848b8605Smrg
1143848b8605Smrgvoid
1144848b8605Smrg_mesa_initialize_dispatch_tables(struct gl_context *ctx)
1145848b8605Smrg{
1146848b8605Smrg   /* Do the code-generated setup of the exec table in api_exec.c. */
1147848b8605Smrg   _mesa_initialize_exec_table(ctx);
1148848b8605Smrg
1149848b8605Smrg   if (ctx->Save)
1150848b8605Smrg      _mesa_initialize_save_table(ctx);
1151848b8605Smrg}
1152848b8605Smrg
1153848b8605Smrg/**
1154848b8605Smrg * Initialize a struct gl_context struct (rendering context).
1155848b8605Smrg *
1156848b8605Smrg * This includes allocating all the other structs and arrays which hang off of
1157848b8605Smrg * the context by pointers.
1158848b8605Smrg * Note that the driver needs to pass in its dd_function_table here since
1159848b8605Smrg * we need to at least call driverFunctions->NewTextureObject to create the
1160848b8605Smrg * default texture objects.
1161b8e80941Smrg *
1162848b8605Smrg * Called by _mesa_create_context().
1163848b8605Smrg *
1164848b8605Smrg * Performs the imports and exports callback tables initialization, and
1165848b8605Smrg * miscellaneous one-time initializations. If no shared context is supplied one
1166848b8605Smrg * is allocated, and increase its reference count.  Setups the GL API dispatch
1167848b8605Smrg * tables.  Initialize the TNL module. Sets the maximum Z buffer depth.
1168848b8605Smrg * Finally queries the \c MESA_DEBUG and \c MESA_VERBOSE environment variables
1169848b8605Smrg * for debug flags.
1170848b8605Smrg *
1171848b8605Smrg * \param ctx the context to initialize
1172848b8605Smrg * \param api the GL API type to create the context for
1173848b8605Smrg * \param visual describes the visual attributes for this context or NULL to
1174848b8605Smrg *               create a configless context
1175848b8605Smrg * \param share_list points to context to share textures, display lists,
1176848b8605Smrg *        etc with, or NULL
1177848b8605Smrg * \param driverFunctions table of device driver functions for this context
1178848b8605Smrg *        to use
1179848b8605Smrg */
1180848b8605SmrgGLboolean
1181848b8605Smrg_mesa_initialize_context(struct gl_context *ctx,
1182848b8605Smrg                         gl_api api,
1183848b8605Smrg                         const struct gl_config *visual,
1184848b8605Smrg                         struct gl_context *share_list,
1185848b8605Smrg                         const struct dd_function_table *driverFunctions)
1186848b8605Smrg{
1187848b8605Smrg   struct gl_shared_state *shared;
1188848b8605Smrg   int i;
1189848b8605Smrg
1190848b8605Smrg   assert(driverFunctions->NewTextureObject);
1191848b8605Smrg   assert(driverFunctions->FreeTextureImageBuffer);
1192848b8605Smrg
1193848b8605Smrg   ctx->API = api;
1194848b8605Smrg   ctx->DrawBuffer = NULL;
1195848b8605Smrg   ctx->ReadBuffer = NULL;
1196848b8605Smrg   ctx->WinSysDrawBuffer = NULL;
1197848b8605Smrg   ctx->WinSysReadBuffer = NULL;
1198848b8605Smrg
1199848b8605Smrg   if (visual) {
1200848b8605Smrg      ctx->Visual = *visual;
1201848b8605Smrg      ctx->HasConfig = GL_TRUE;
1202848b8605Smrg   }
1203848b8605Smrg   else {
1204848b8605Smrg      memset(&ctx->Visual, 0, sizeof ctx->Visual);
1205848b8605Smrg      ctx->HasConfig = GL_FALSE;
1206848b8605Smrg   }
1207848b8605Smrg
1208b8e80941Smrg   _mesa_override_gl_version(ctx);
1209848b8605Smrg
1210848b8605Smrg   /* misc one-time initializations */
1211848b8605Smrg   one_time_init(ctx);
1212848b8605Smrg
1213b8e80941Smrg   _mesa_init_shader_compiler_types();
1214b8e80941Smrg
1215848b8605Smrg   /* Plug in driver functions and context pointer here.
1216848b8605Smrg    * This is important because when we call alloc_shared_state() below
1217848b8605Smrg    * we'll call ctx->Driver.NewTextureObject() to create the default
1218848b8605Smrg    * textures.
1219848b8605Smrg    */
1220848b8605Smrg   ctx->Driver = *driverFunctions;
1221848b8605Smrg
1222848b8605Smrg   if (share_list) {
1223848b8605Smrg      /* share state with another context */
1224848b8605Smrg      shared = share_list->Shared;
1225848b8605Smrg   }
1226848b8605Smrg   else {
1227848b8605Smrg      /* allocate new, unshared state */
1228848b8605Smrg      shared = _mesa_alloc_shared_state(ctx);
1229848b8605Smrg      if (!shared)
1230848b8605Smrg         return GL_FALSE;
1231848b8605Smrg   }
1232848b8605Smrg
1233848b8605Smrg   _mesa_reference_shared_state(ctx, &ctx->Shared, shared);
1234848b8605Smrg
1235848b8605Smrg   if (!init_attrib_groups( ctx ))
1236848b8605Smrg      goto fail;
1237848b8605Smrg
1238b8e80941Smrg   /* KHR_no_error is likely to crash, overflow memory, etc if an application
1239b8e80941Smrg    * has errors so don't enable it for setuid processes.
1240b8e80941Smrg    */
1241b8e80941Smrg   if (env_var_as_boolean("MESA_NO_ERROR", false)) {
1242b8e80941Smrg#if !defined(_WIN32)
1243b8e80941Smrg      if (!issetugid())
1244b8e80941Smrg#endif
1245b8e80941Smrg         ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_NO_ERROR_BIT_KHR;
1246b8e80941Smrg   }
1247b8e80941Smrg
1248848b8605Smrg   /* setup the API dispatch tables with all nop functions */
1249848b8605Smrg   ctx->OutsideBeginEnd = _mesa_alloc_dispatch_table();
1250848b8605Smrg   if (!ctx->OutsideBeginEnd)
1251848b8605Smrg      goto fail;
1252848b8605Smrg   ctx->Exec = ctx->OutsideBeginEnd;
1253b8e80941Smrg   ctx->CurrentClientDispatch = ctx->CurrentServerDispatch = ctx->OutsideBeginEnd;
1254848b8605Smrg
1255848b8605Smrg   ctx->FragmentProgram._MaintainTexEnvProgram
1256b8e80941Smrg      = (getenv("MESA_TEX_PROG") != NULL);
1257848b8605Smrg
1258848b8605Smrg   ctx->VertexProgram._MaintainTnlProgram
1259b8e80941Smrg      = (getenv("MESA_TNL_PROG") != NULL);
1260848b8605Smrg   if (ctx->VertexProgram._MaintainTnlProgram) {
1261848b8605Smrg      /* this is required... */
1262848b8605Smrg      ctx->FragmentProgram._MaintainTexEnvProgram = GL_TRUE;
1263848b8605Smrg   }
1264848b8605Smrg
1265848b8605Smrg   /* Mesa core handles all the formats that mesa core knows about.
1266848b8605Smrg    * Drivers will want to override this list with just the formats
1267848b8605Smrg    * they can handle, and confirm that appropriate fallbacks exist in
1268848b8605Smrg    * _mesa_choose_tex_format().
1269848b8605Smrg    */
1270848b8605Smrg   memset(&ctx->TextureFormatSupported, GL_TRUE,
1271b8e80941Smrg          sizeof(ctx->TextureFormatSupported));
1272848b8605Smrg
1273848b8605Smrg   switch (ctx->API) {
1274848b8605Smrg   case API_OPENGL_COMPAT:
1275848b8605Smrg      ctx->BeginEnd = create_beginend_table(ctx);
1276848b8605Smrg      ctx->Save = _mesa_alloc_dispatch_table();
1277848b8605Smrg      if (!ctx->BeginEnd || !ctx->Save)
1278848b8605Smrg         goto fail;
1279848b8605Smrg
1280848b8605Smrg      /* fall-through */
1281848b8605Smrg   case API_OPENGL_CORE:
1282848b8605Smrg      break;
1283848b8605Smrg   case API_OPENGLES:
1284848b8605Smrg      /**
1285848b8605Smrg       * GL_OES_texture_cube_map says
1286848b8605Smrg       * "Initially all texture generation modes are set to REFLECTION_MAP_OES"
1287848b8605Smrg       */
1288b8e80941Smrg      for (i = 0; i < ARRAY_SIZE(ctx->Texture.FixedFuncUnit); i++) {
1289b8e80941Smrg         struct gl_fixedfunc_texture_unit *texUnit =
1290b8e80941Smrg            &ctx->Texture.FixedFuncUnit[i];
1291b8e80941Smrg
1292b8e80941Smrg         texUnit->GenS.Mode = GL_REFLECTION_MAP_NV;
1293b8e80941Smrg         texUnit->GenT.Mode = GL_REFLECTION_MAP_NV;
1294b8e80941Smrg         texUnit->GenR.Mode = GL_REFLECTION_MAP_NV;
1295b8e80941Smrg         texUnit->GenS._ModeBit = TEXGEN_REFLECTION_MAP_NV;
1296b8e80941Smrg         texUnit->GenT._ModeBit = TEXGEN_REFLECTION_MAP_NV;
1297b8e80941Smrg         texUnit->GenR._ModeBit = TEXGEN_REFLECTION_MAP_NV;
1298848b8605Smrg      }
1299848b8605Smrg      break;
1300848b8605Smrg   case API_OPENGLES2:
1301848b8605Smrg      ctx->FragmentProgram._MaintainTexEnvProgram = GL_TRUE;
1302848b8605Smrg      ctx->VertexProgram._MaintainTnlProgram = GL_TRUE;
1303848b8605Smrg      break;
1304848b8605Smrg   }
1305848b8605Smrg
1306848b8605Smrg   ctx->FirstTimeCurrent = GL_TRUE;
1307848b8605Smrg
1308848b8605Smrg   return GL_TRUE;
1309848b8605Smrg
1310848b8605Smrgfail:
1311848b8605Smrg   _mesa_reference_shared_state(ctx, &ctx->Shared, NULL);
1312848b8605Smrg   free(ctx->BeginEnd);
1313848b8605Smrg   free(ctx->OutsideBeginEnd);
1314848b8605Smrg   free(ctx->Save);
1315848b8605Smrg   return GL_FALSE;
1316848b8605Smrg}
1317848b8605Smrg
1318848b8605Smrg
1319848b8605Smrg/**
1320848b8605Smrg * Free the data associated with the given context.
1321b8e80941Smrg *
1322848b8605Smrg * But doesn't free the struct gl_context struct itself.
1323848b8605Smrg *
1324848b8605Smrg * \sa _mesa_initialize_context() and init_attrib_groups().
1325848b8605Smrg */
1326848b8605Smrgvoid
1327b8e80941Smrg_mesa_free_context_data(struct gl_context *ctx, bool destroy_compiler_types)
1328848b8605Smrg{
1329848b8605Smrg   if (!_mesa_get_current_context()){
1330848b8605Smrg      /* No current context, but we may need one in order to delete
1331848b8605Smrg       * texture objs, etc.  So temporarily bind the context now.
1332848b8605Smrg       */
1333848b8605Smrg      _mesa_make_current(ctx, NULL, NULL);
1334848b8605Smrg   }
1335848b8605Smrg
1336848b8605Smrg   /* unreference WinSysDraw/Read buffers */
1337848b8605Smrg   _mesa_reference_framebuffer(&ctx->WinSysDrawBuffer, NULL);
1338848b8605Smrg   _mesa_reference_framebuffer(&ctx->WinSysReadBuffer, NULL);
1339848b8605Smrg   _mesa_reference_framebuffer(&ctx->DrawBuffer, NULL);
1340848b8605Smrg   _mesa_reference_framebuffer(&ctx->ReadBuffer, NULL);
1341848b8605Smrg
1342b8e80941Smrg   _mesa_reference_program(ctx, &ctx->VertexProgram.Current, NULL);
1343b8e80941Smrg   _mesa_reference_program(ctx, &ctx->VertexProgram._Current, NULL);
1344b8e80941Smrg   _mesa_reference_program(ctx, &ctx->VertexProgram._TnlProgram, NULL);
1345848b8605Smrg
1346b8e80941Smrg   _mesa_reference_program(ctx, &ctx->TessCtrlProgram._Current, NULL);
1347b8e80941Smrg   _mesa_reference_program(ctx, &ctx->TessEvalProgram._Current, NULL);
1348b8e80941Smrg   _mesa_reference_program(ctx, &ctx->GeometryProgram._Current, NULL);
1349848b8605Smrg
1350b8e80941Smrg   _mesa_reference_program(ctx, &ctx->FragmentProgram.Current, NULL);
1351b8e80941Smrg   _mesa_reference_program(ctx, &ctx->FragmentProgram._Current, NULL);
1352b8e80941Smrg   _mesa_reference_program(ctx, &ctx->FragmentProgram._TexEnvProgram, NULL);
1353b8e80941Smrg
1354b8e80941Smrg   _mesa_reference_program(ctx, &ctx->ComputeProgram._Current, NULL);
1355848b8605Smrg
1356848b8605Smrg   _mesa_reference_vao(ctx, &ctx->Array.VAO, NULL);
1357848b8605Smrg   _mesa_reference_vao(ctx, &ctx->Array.DefaultVAO, NULL);
1358b8e80941Smrg   _mesa_reference_vao(ctx, &ctx->Array._EmptyVAO, NULL);
1359b8e80941Smrg   _mesa_reference_vao(ctx, &ctx->Array._DrawVAO, NULL);
1360848b8605Smrg
1361848b8605Smrg   _mesa_free_attrib_data(ctx);
1362848b8605Smrg   _mesa_free_buffer_objects(ctx);
1363848b8605Smrg   _mesa_free_eval_data( ctx );
1364848b8605Smrg   _mesa_free_texture_data( ctx );
1365b8e80941Smrg   _mesa_free_image_textures(ctx);
1366848b8605Smrg   _mesa_free_matrix_data( ctx );
1367848b8605Smrg   _mesa_free_pipeline_data(ctx);
1368848b8605Smrg   _mesa_free_program_data(ctx);
1369848b8605Smrg   _mesa_free_shader_state(ctx);
1370848b8605Smrg   _mesa_free_queryobj_data(ctx);
1371848b8605Smrg   _mesa_free_sync_data(ctx);
1372848b8605Smrg   _mesa_free_varray_data(ctx);
1373848b8605Smrg   _mesa_free_transform_feedback(ctx);
1374848b8605Smrg   _mesa_free_performance_monitors(ctx);
1375b8e80941Smrg   _mesa_free_performance_queries(ctx);
1376b8e80941Smrg   _mesa_free_resident_handles(ctx);
1377848b8605Smrg
1378848b8605Smrg   _mesa_reference_buffer_object(ctx, &ctx->Pack.BufferObj, NULL);
1379848b8605Smrg   _mesa_reference_buffer_object(ctx, &ctx->Unpack.BufferObj, NULL);
1380848b8605Smrg   _mesa_reference_buffer_object(ctx, &ctx->DefaultPacking.BufferObj, NULL);
1381848b8605Smrg   _mesa_reference_buffer_object(ctx, &ctx->Array.ArrayBufferObj, NULL);
1382848b8605Smrg
1383848b8605Smrg   /* free dispatch tables */
1384848b8605Smrg   free(ctx->BeginEnd);
1385848b8605Smrg   free(ctx->OutsideBeginEnd);
1386848b8605Smrg   free(ctx->Save);
1387b8e80941Smrg   free(ctx->ContextLost);
1388b8e80941Smrg   free(ctx->MarshalExec);
1389848b8605Smrg
1390848b8605Smrg   /* Shared context state (display lists, textures, etc) */
1391848b8605Smrg   _mesa_reference_shared_state(ctx, &ctx->Shared, NULL);
1392848b8605Smrg
1393848b8605Smrg   /* needs to be after freeing shared state */
1394848b8605Smrg   _mesa_free_display_list_data(ctx);
1395848b8605Smrg
1396848b8605Smrg   _mesa_free_errors_data(ctx);
1397848b8605Smrg
1398848b8605Smrg   free((void *)ctx->Extensions.String);
1399848b8605Smrg
1400848b8605Smrg   free(ctx->VersionString);
1401848b8605Smrg
1402b8e80941Smrg   if (destroy_compiler_types)
1403b8e80941Smrg      _mesa_destroy_shader_compiler_types();
1404b8e80941Smrg
1405848b8605Smrg   /* unbind the context if it's currently bound */
1406848b8605Smrg   if (ctx == _mesa_get_current_context()) {
1407848b8605Smrg      _mesa_make_current(NULL, NULL, NULL);
1408848b8605Smrg   }
1409848b8605Smrg}
1410848b8605Smrg
1411848b8605Smrg
1412848b8605Smrg/**
1413848b8605Smrg * Destroy a struct gl_context structure.
1414848b8605Smrg *
1415848b8605Smrg * \param ctx GL context.
1416b8e80941Smrg *
1417848b8605Smrg * Calls _mesa_free_context_data() and frees the gl_context object itself.
1418848b8605Smrg */
1419848b8605Smrgvoid
1420848b8605Smrg_mesa_destroy_context( struct gl_context *ctx )
1421848b8605Smrg{
1422848b8605Smrg   if (ctx) {
1423b8e80941Smrg      _mesa_free_context_data(ctx, true);
1424848b8605Smrg      free( (void *) ctx );
1425848b8605Smrg   }
1426848b8605Smrg}
1427848b8605Smrg
1428848b8605Smrg
1429848b8605Smrg/**
1430848b8605Smrg * Copy attribute groups from one context to another.
1431b8e80941Smrg *
1432848b8605Smrg * \param src source context
1433848b8605Smrg * \param dst destination context
1434848b8605Smrg * \param mask bitwise OR of GL_*_BIT flags
1435848b8605Smrg *
1436848b8605Smrg * According to the bits specified in \p mask, copies the corresponding
1437848b8605Smrg * attributes from \p src into \p dst.  For many of the attributes a simple \c
1438848b8605Smrg * memcpy is not enough due to the existence of internal pointers in their data
1439848b8605Smrg * structures.
1440848b8605Smrg */
1441848b8605Smrgvoid
1442848b8605Smrg_mesa_copy_context( const struct gl_context *src, struct gl_context *dst,
1443848b8605Smrg                    GLuint mask )
1444848b8605Smrg{
1445848b8605Smrg   if (mask & GL_ACCUM_BUFFER_BIT) {
1446848b8605Smrg      /* OK to memcpy */
1447848b8605Smrg      dst->Accum = src->Accum;
1448848b8605Smrg   }
1449848b8605Smrg   if (mask & GL_COLOR_BUFFER_BIT) {
1450848b8605Smrg      /* OK to memcpy */
1451848b8605Smrg      dst->Color = src->Color;
1452848b8605Smrg   }
1453848b8605Smrg   if (mask & GL_CURRENT_BIT) {
1454848b8605Smrg      /* OK to memcpy */
1455848b8605Smrg      dst->Current = src->Current;
1456848b8605Smrg   }
1457848b8605Smrg   if (mask & GL_DEPTH_BUFFER_BIT) {
1458848b8605Smrg      /* OK to memcpy */
1459848b8605Smrg      dst->Depth = src->Depth;
1460848b8605Smrg   }
1461848b8605Smrg   if (mask & GL_ENABLE_BIT) {
1462848b8605Smrg      /* no op */
1463848b8605Smrg   }
1464848b8605Smrg   if (mask & GL_EVAL_BIT) {
1465848b8605Smrg      /* OK to memcpy */
1466848b8605Smrg      dst->Eval = src->Eval;
1467848b8605Smrg   }
1468848b8605Smrg   if (mask & GL_FOG_BIT) {
1469848b8605Smrg      /* OK to memcpy */
1470848b8605Smrg      dst->Fog = src->Fog;
1471848b8605Smrg   }
1472848b8605Smrg   if (mask & GL_HINT_BIT) {
1473848b8605Smrg      /* OK to memcpy */
1474848b8605Smrg      dst->Hint = src->Hint;
1475848b8605Smrg   }
1476848b8605Smrg   if (mask & GL_LIGHTING_BIT) {
1477b8e80941Smrg      /* OK to memcpy */
1478848b8605Smrg      dst->Light = src->Light;
1479848b8605Smrg   }
1480848b8605Smrg   if (mask & GL_LINE_BIT) {
1481848b8605Smrg      /* OK to memcpy */
1482848b8605Smrg      dst->Line = src->Line;
1483848b8605Smrg   }
1484848b8605Smrg   if (mask & GL_LIST_BIT) {
1485848b8605Smrg      /* OK to memcpy */
1486848b8605Smrg      dst->List = src->List;
1487848b8605Smrg   }
1488848b8605Smrg   if (mask & GL_PIXEL_MODE_BIT) {
1489848b8605Smrg      /* OK to memcpy */
1490848b8605Smrg      dst->Pixel = src->Pixel;
1491848b8605Smrg   }
1492848b8605Smrg   if (mask & GL_POINT_BIT) {
1493848b8605Smrg      /* OK to memcpy */
1494848b8605Smrg      dst->Point = src->Point;
1495848b8605Smrg   }
1496848b8605Smrg   if (mask & GL_POLYGON_BIT) {
1497848b8605Smrg      /* OK to memcpy */
1498848b8605Smrg      dst->Polygon = src->Polygon;
1499848b8605Smrg   }
1500848b8605Smrg   if (mask & GL_POLYGON_STIPPLE_BIT) {
1501848b8605Smrg      /* Use loop instead of memcpy due to problem with Portland Group's
1502848b8605Smrg       * C compiler.  Reported by John Stone.
1503848b8605Smrg       */
1504848b8605Smrg      GLuint i;
1505848b8605Smrg      for (i = 0; i < 32; i++) {
1506848b8605Smrg         dst->PolygonStipple[i] = src->PolygonStipple[i];
1507848b8605Smrg      }
1508848b8605Smrg   }
1509848b8605Smrg   if (mask & GL_SCISSOR_BIT) {
1510848b8605Smrg      /* OK to memcpy */
1511848b8605Smrg      dst->Scissor = src->Scissor;
1512848b8605Smrg   }
1513848b8605Smrg   if (mask & GL_STENCIL_BUFFER_BIT) {
1514848b8605Smrg      /* OK to memcpy */
1515848b8605Smrg      dst->Stencil = src->Stencil;
1516848b8605Smrg   }
1517848b8605Smrg   if (mask & GL_TEXTURE_BIT) {
1518848b8605Smrg      /* Cannot memcpy because of pointers */
1519848b8605Smrg      _mesa_copy_texture_state(src, dst);
1520848b8605Smrg   }
1521848b8605Smrg   if (mask & GL_TRANSFORM_BIT) {
1522848b8605Smrg      /* OK to memcpy */
1523848b8605Smrg      dst->Transform = src->Transform;
1524848b8605Smrg   }
1525848b8605Smrg   if (mask & GL_VIEWPORT_BIT) {
1526848b8605Smrg      unsigned i;
1527848b8605Smrg      for (i = 0; i < src->Const.MaxViewports; i++) {
1528b8e80941Smrg         /* OK to memcpy */
1529b8e80941Smrg         dst->ViewportArray[i] = src->ViewportArray[i];
1530848b8605Smrg      }
1531848b8605Smrg   }
1532848b8605Smrg
1533848b8605Smrg   /* XXX FIXME:  Call callbacks?
1534848b8605Smrg    */
1535848b8605Smrg   dst->NewState = _NEW_ALL;
1536848b8605Smrg   dst->NewDriverState = ~0;
1537848b8605Smrg}
1538848b8605Smrg
1539848b8605Smrg
1540848b8605Smrg/**
1541848b8605Smrg * Check if the given context can render into the given framebuffer
1542848b8605Smrg * by checking visual attributes.
1543848b8605Smrg *
1544848b8605Smrg * \return GL_TRUE if compatible, GL_FALSE otherwise.
1545848b8605Smrg */
1546b8e80941Smrgstatic GLboolean
1547848b8605Smrgcheck_compatible(const struct gl_context *ctx,
1548848b8605Smrg                 const struct gl_framebuffer *buffer)
1549848b8605Smrg{
1550848b8605Smrg   const struct gl_config *ctxvis = &ctx->Visual;
1551848b8605Smrg   const struct gl_config *bufvis = &buffer->Visual;
1552848b8605Smrg
1553848b8605Smrg   if (buffer == _mesa_get_incomplete_framebuffer())
1554848b8605Smrg      return GL_TRUE;
1555848b8605Smrg
1556b8e80941Smrg#define check_component(foo)           \
1557b8e80941Smrg   if (ctxvis->foo && bufvis->foo &&   \
1558b8e80941Smrg       ctxvis->foo != bufvis->foo)     \
1559b8e80941Smrg      return GL_FALSE
1560b8e80941Smrg
1561b8e80941Smrg   check_component(redMask);
1562b8e80941Smrg   check_component(greenMask);
1563b8e80941Smrg   check_component(blueMask);
1564b8e80941Smrg   check_component(depthBits);
1565b8e80941Smrg   check_component(stencilBits);
1566b8e80941Smrg
1567b8e80941Smrg#undef check_component
1568848b8605Smrg
1569848b8605Smrg   return GL_TRUE;
1570848b8605Smrg}
1571848b8605Smrg
1572848b8605Smrg
1573848b8605Smrg/**
1574848b8605Smrg * Check if the viewport/scissor size has not yet been initialized.
1575848b8605Smrg * Initialize the size if the given width and height are non-zero.
1576848b8605Smrg */
1577b8e80941Smrgstatic void
1578b8e80941Smrgcheck_init_viewport(struct gl_context *ctx, GLuint width, GLuint height)
1579848b8605Smrg{
1580848b8605Smrg   if (!ctx->ViewportInitialized && width > 0 && height > 0) {
1581848b8605Smrg      unsigned i;
1582848b8605Smrg
1583848b8605Smrg      /* Note: set flag here, before calling _mesa_set_viewport(), to prevent
1584848b8605Smrg       * potential infinite recursion.
1585848b8605Smrg       */
1586848b8605Smrg      ctx->ViewportInitialized = GL_TRUE;
1587848b8605Smrg
1588848b8605Smrg      /* Note: ctx->Const.MaxViewports may not have been set by the driver
1589848b8605Smrg       * yet, so just initialize all of them.
1590848b8605Smrg       */
1591848b8605Smrg      for (i = 0; i < MAX_VIEWPORTS; i++) {
1592848b8605Smrg         _mesa_set_viewport(ctx, i, 0, 0, width, height);
1593848b8605Smrg         _mesa_set_scissor(ctx, i, 0, 0, width, height);
1594848b8605Smrg      }
1595848b8605Smrg   }
1596848b8605Smrg}
1597848b8605Smrg
1598b8e80941Smrg
1599848b8605Smrgstatic void
1600848b8605Smrghandle_first_current(struct gl_context *ctx)
1601848b8605Smrg{
1602b8e80941Smrg   if (ctx->Version == 0 || !ctx->DrawBuffer) {
1603b8e80941Smrg      /* probably in the process of tearing down the context */
1604b8e80941Smrg      return;
1605b8e80941Smrg   }
1606848b8605Smrg
1607848b8605Smrg   check_context_limits(ctx);
1608848b8605Smrg
1609b8e80941Smrg   _mesa_update_vertex_processing_mode(ctx);
1610b8e80941Smrg
1611848b8605Smrg   /* According to GL_MESA_configless_context the default value of
1612848b8605Smrg    * glDrawBuffers depends on the config of the first surface it is bound to.
1613b8e80941Smrg    * For GLES it is always GL_BACK which has a magic interpretation.
1614b8e80941Smrg    */
1615848b8605Smrg   if (!ctx->HasConfig && _mesa_is_desktop_gl(ctx)) {
1616848b8605Smrg      if (ctx->DrawBuffer != _mesa_get_incomplete_framebuffer()) {
1617b8e80941Smrg         GLenum16 buffer;
1618b8e80941Smrg
1619848b8605Smrg         if (ctx->DrawBuffer->Visual.doubleBufferMode)
1620848b8605Smrg            buffer = GL_BACK;
1621848b8605Smrg         else
1622848b8605Smrg            buffer = GL_FRONT;
1623848b8605Smrg
1624b8e80941Smrg         _mesa_drawbuffers(ctx, ctx->DrawBuffer, 1, &buffer,
1625b8e80941Smrg                           NULL /* destMask */);
1626848b8605Smrg      }
1627848b8605Smrg
1628848b8605Smrg      if (ctx->ReadBuffer != _mesa_get_incomplete_framebuffer()) {
1629b8e80941Smrg         gl_buffer_index bufferIndex;
1630b8e80941Smrg         GLenum buffer;
1631b8e80941Smrg
1632848b8605Smrg         if (ctx->ReadBuffer->Visual.doubleBufferMode) {
1633848b8605Smrg            buffer = GL_BACK;
1634848b8605Smrg            bufferIndex = BUFFER_BACK_LEFT;
1635848b8605Smrg         }
1636848b8605Smrg         else {
1637848b8605Smrg            buffer = GL_FRONT;
1638848b8605Smrg            bufferIndex = BUFFER_FRONT_LEFT;
1639848b8605Smrg         }
1640848b8605Smrg
1641b8e80941Smrg         _mesa_readbuffer(ctx, ctx->ReadBuffer, buffer, bufferIndex);
1642848b8605Smrg      }
1643848b8605Smrg   }
1644848b8605Smrg
1645b8e80941Smrg   /* Determine if generic vertex attribute 0 aliases the conventional
1646b8e80941Smrg    * glVertex position.
1647b8e80941Smrg    */
1648b8e80941Smrg   {
1649b8e80941Smrg      const bool is_forward_compatible_context =
1650b8e80941Smrg         ctx->Const.ContextFlags & GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT;
1651b8e80941Smrg
1652b8e80941Smrg      /* In OpenGL 3.1 attribute 0 becomes non-magic, just like in OpenGL ES
1653b8e80941Smrg       * 2.0.  Note that we cannot just check for API_OPENGL_COMPAT here because
1654b8e80941Smrg       * that will erroneously allow this usage in a 3.0 forward-compatible
1655b8e80941Smrg       * context too.
1656b8e80941Smrg       */
1657b8e80941Smrg      ctx->_AttribZeroAliasesVertex = (ctx->API == API_OPENGLES
1658b8e80941Smrg                                       || (ctx->API == API_OPENGL_COMPAT
1659b8e80941Smrg                                           && !is_forward_compatible_context));
1660b8e80941Smrg   }
1661b8e80941Smrg
1662848b8605Smrg   /* We can use this to help debug user's problems.  Tell them to set
1663848b8605Smrg    * the MESA_INFO env variable before running their app.  Then the
1664848b8605Smrg    * first time each context is made current we'll print some useful
1665848b8605Smrg    * information.
1666848b8605Smrg    */
1667b8e80941Smrg   if (getenv("MESA_INFO")) {
1668848b8605Smrg      _mesa_print_info(ctx);
1669848b8605Smrg   }
1670848b8605Smrg}
1671848b8605Smrg
1672848b8605Smrg/**
1673848b8605Smrg * Bind the given context to the given drawBuffer and readBuffer and
1674848b8605Smrg * make it the current context for the calling thread.
1675848b8605Smrg * We'll render into the drawBuffer and read pixels from the
1676848b8605Smrg * readBuffer (i.e. glRead/CopyPixels, glCopyTexImage, etc).
1677848b8605Smrg *
1678848b8605Smrg * We check that the context's and framebuffer's visuals are compatible
1679848b8605Smrg * and return immediately if they're not.
1680848b8605Smrg *
1681848b8605Smrg * \param newCtx  the new GL context. If NULL then there will be no current GL
1682848b8605Smrg *                context.
1683848b8605Smrg * \param drawBuffer  the drawing framebuffer
1684848b8605Smrg * \param readBuffer  the reading framebuffer
1685848b8605Smrg */
1686848b8605SmrgGLboolean
1687848b8605Smrg_mesa_make_current( struct gl_context *newCtx,
1688848b8605Smrg                    struct gl_framebuffer *drawBuffer,
1689848b8605Smrg                    struct gl_framebuffer *readBuffer )
1690848b8605Smrg{
1691848b8605Smrg   GET_CURRENT_CONTEXT(curCtx);
1692848b8605Smrg
1693848b8605Smrg   if (MESA_VERBOSE & VERBOSE_API)
1694848b8605Smrg      _mesa_debug(newCtx, "_mesa_make_current()\n");
1695848b8605Smrg
1696848b8605Smrg   /* Check that the context's and framebuffer's visuals are compatible.
1697848b8605Smrg    */
1698848b8605Smrg   if (newCtx && drawBuffer && newCtx->WinSysDrawBuffer != drawBuffer) {
1699848b8605Smrg      if (!check_compatible(newCtx, drawBuffer)) {
1700848b8605Smrg         _mesa_warning(newCtx,
1701848b8605Smrg              "MakeCurrent: incompatible visuals for context and drawbuffer");
1702848b8605Smrg         return GL_FALSE;
1703848b8605Smrg      }
1704848b8605Smrg   }
1705848b8605Smrg   if (newCtx && readBuffer && newCtx->WinSysReadBuffer != readBuffer) {
1706848b8605Smrg      if (!check_compatible(newCtx, readBuffer)) {
1707848b8605Smrg         _mesa_warning(newCtx,
1708848b8605Smrg              "MakeCurrent: incompatible visuals for context and readbuffer");
1709848b8605Smrg         return GL_FALSE;
1710848b8605Smrg      }
1711848b8605Smrg   }
1712848b8605Smrg
1713b8e80941Smrg   if (curCtx &&
1714b8e80941Smrg       (curCtx->WinSysDrawBuffer || curCtx->WinSysReadBuffer) &&
1715848b8605Smrg       /* make sure this context is valid for flushing */
1716b8e80941Smrg       curCtx != newCtx &&
1717b8e80941Smrg       curCtx->Const.ContextReleaseBehavior ==
1718b8e80941Smrg       GL_CONTEXT_RELEASE_BEHAVIOR_FLUSH) {
1719848b8605Smrg      _mesa_flush(curCtx);
1720b8e80941Smrg   }
1721848b8605Smrg
1722b8e80941Smrg   /* Call this periodically to detect when the user has begun using
1723b8e80941Smrg    * GL rendering from multiple threads.
1724b8e80941Smrg    */
1725b8e80941Smrg   _glapi_check_multithread();
1726848b8605Smrg
1727848b8605Smrg   if (!newCtx) {
1728848b8605Smrg      _glapi_set_dispatch(NULL);  /* none current */
1729b8e80941Smrg      /* We need old ctx to correctly release Draw/ReadBuffer
1730b8e80941Smrg       * and avoid a surface leak in st_renderbuffer_delete.
1731b8e80941Smrg       * Therefore, first drop buffers then set new ctx to NULL.
1732b8e80941Smrg       */
1733b8e80941Smrg      if (curCtx) {
1734b8e80941Smrg         _mesa_reference_framebuffer(&curCtx->WinSysDrawBuffer, NULL);
1735b8e80941Smrg         _mesa_reference_framebuffer(&curCtx->WinSysReadBuffer, NULL);
1736b8e80941Smrg      }
1737b8e80941Smrg      _glapi_set_context(NULL);
1738b8e80941Smrg      assert(_mesa_get_current_context() == NULL);
1739848b8605Smrg   }
1740848b8605Smrg   else {
1741b8e80941Smrg      _glapi_set_context((void *) newCtx);
1742b8e80941Smrg      assert(_mesa_get_current_context() == newCtx);
1743b8e80941Smrg      _glapi_set_dispatch(newCtx->CurrentClientDispatch);
1744848b8605Smrg
1745848b8605Smrg      if (drawBuffer && readBuffer) {
1746b8e80941Smrg         assert(_mesa_is_winsys_fbo(drawBuffer));
1747b8e80941Smrg         assert(_mesa_is_winsys_fbo(readBuffer));
1748848b8605Smrg         _mesa_reference_framebuffer(&newCtx->WinSysDrawBuffer, drawBuffer);
1749848b8605Smrg         _mesa_reference_framebuffer(&newCtx->WinSysReadBuffer, readBuffer);
1750848b8605Smrg
1751848b8605Smrg         /*
1752848b8605Smrg          * Only set the context's Draw/ReadBuffer fields if they're NULL
1753848b8605Smrg          * or not bound to a user-created FBO.
1754848b8605Smrg          */
1755848b8605Smrg         if (!newCtx->DrawBuffer || _mesa_is_winsys_fbo(newCtx->DrawBuffer)) {
1756848b8605Smrg            _mesa_reference_framebuffer(&newCtx->DrawBuffer, drawBuffer);
1757848b8605Smrg            /* Update the FBO's list of drawbuffers/renderbuffers.
1758848b8605Smrg             * For winsys FBOs this comes from the GL state (which may have
1759848b8605Smrg             * changed since the last time this FBO was bound).
1760848b8605Smrg             */
1761848b8605Smrg            _mesa_update_draw_buffers(newCtx);
1762848b8605Smrg         }
1763848b8605Smrg         if (!newCtx->ReadBuffer || _mesa_is_winsys_fbo(newCtx->ReadBuffer)) {
1764848b8605Smrg            _mesa_reference_framebuffer(&newCtx->ReadBuffer, readBuffer);
1765b8e80941Smrg            /* In _mesa_initialize_window_framebuffer, for single-buffered
1766b8e80941Smrg             * visuals, the ColorReadBuffer is set to be GL_FRONT, even with
1767b8e80941Smrg             * GLES contexts. When calling read_buffer, we verify we are reading
1768b8e80941Smrg             * from GL_BACK in is_legal_es3_readbuffer_enum.  But the default is
1769b8e80941Smrg             * incorrect, and certain dEQP tests check this.  So fix it here.
1770b8e80941Smrg             */
1771b8e80941Smrg            if (_mesa_is_gles(newCtx) &&
1772b8e80941Smrg               !newCtx->ReadBuffer->Visual.doubleBufferMode)
1773b8e80941Smrg               if (newCtx->ReadBuffer->ColorReadBuffer == GL_FRONT)
1774b8e80941Smrg                  newCtx->ReadBuffer->ColorReadBuffer = GL_BACK;
1775848b8605Smrg         }
1776848b8605Smrg
1777848b8605Smrg         /* XXX only set this flag if we're really changing the draw/read
1778848b8605Smrg          * framebuffer bindings.
1779848b8605Smrg          */
1780b8e80941Smrg         newCtx->NewState |= _NEW_BUFFERS;
1781848b8605Smrg
1782b8e80941Smrg         check_init_viewport(newCtx, drawBuffer->Width, drawBuffer->Height);
1783848b8605Smrg      }
1784848b8605Smrg
1785848b8605Smrg      if (newCtx->FirstTimeCurrent) {
1786848b8605Smrg         handle_first_current(newCtx);
1787b8e80941Smrg         newCtx->FirstTimeCurrent = GL_FALSE;
1788848b8605Smrg      }
1789848b8605Smrg   }
1790b8e80941Smrg
1791848b8605Smrg   return GL_TRUE;
1792848b8605Smrg}
1793848b8605Smrg
1794848b8605Smrg
1795848b8605Smrg/**
1796848b8605Smrg * Make context 'ctx' share the display lists, textures and programs
1797848b8605Smrg * that are associated with 'ctxToShare'.
1798848b8605Smrg * Any display lists, textures or programs associated with 'ctx' will
1799848b8605Smrg * be deleted if nobody else is sharing them.
1800848b8605Smrg */
1801848b8605SmrgGLboolean
1802848b8605Smrg_mesa_share_state(struct gl_context *ctx, struct gl_context *ctxToShare)
1803848b8605Smrg{
1804848b8605Smrg   if (ctx && ctxToShare && ctx->Shared && ctxToShare->Shared) {
1805848b8605Smrg      struct gl_shared_state *oldShared = NULL;
1806848b8605Smrg
1807848b8605Smrg      /* save ref to old state to prevent it from being deleted immediately */
1808848b8605Smrg      _mesa_reference_shared_state(ctx, &oldShared, ctx->Shared);
1809848b8605Smrg
1810848b8605Smrg      /* update ctx's Shared pointer */
1811848b8605Smrg      _mesa_reference_shared_state(ctx, &ctx->Shared, ctxToShare->Shared);
1812848b8605Smrg
1813848b8605Smrg      update_default_objects(ctx);
1814848b8605Smrg
1815848b8605Smrg      /* release the old shared state */
1816848b8605Smrg      _mesa_reference_shared_state(ctx, &oldShared, NULL);
1817848b8605Smrg
1818848b8605Smrg      return GL_TRUE;
1819848b8605Smrg   }
1820848b8605Smrg   else {
1821848b8605Smrg      return GL_FALSE;
1822848b8605Smrg   }
1823848b8605Smrg}
1824848b8605Smrg
1825848b8605Smrg
1826848b8605Smrg
1827848b8605Smrg/**
1828848b8605Smrg * \return pointer to the current GL context for this thread.
1829b8e80941Smrg *
1830848b8605Smrg * Calls _glapi_get_context(). This isn't the fastest way to get the current
1831848b8605Smrg * context.  If you need speed, see the #GET_CURRENT_CONTEXT macro in
1832848b8605Smrg * context.h.
1833848b8605Smrg */
1834848b8605Smrgstruct gl_context *
1835848b8605Smrg_mesa_get_current_context( void )
1836848b8605Smrg{
1837848b8605Smrg   return (struct gl_context *) _glapi_get_context();
1838848b8605Smrg}
1839848b8605Smrg
1840848b8605Smrg
1841848b8605Smrg/**
1842848b8605Smrg * Get context's current API dispatch table.
1843848b8605Smrg *
1844b8e80941Smrg * It'll either be the immediate-mode execute dispatcher, the display list
1845b8e80941Smrg * compile dispatcher, or the thread marshalling dispatcher.
1846b8e80941Smrg *
1847848b8605Smrg * \param ctx GL context.
1848848b8605Smrg *
1849848b8605Smrg * \return pointer to dispatch_table.
1850848b8605Smrg *
1851b8e80941Smrg * Simply returns __struct gl_contextRec::CurrentClientDispatch.
1852848b8605Smrg */
1853848b8605Smrgstruct _glapi_table *
1854848b8605Smrg_mesa_get_dispatch(struct gl_context *ctx)
1855848b8605Smrg{
1856b8e80941Smrg   return ctx->CurrentClientDispatch;
1857848b8605Smrg}
1858848b8605Smrg
1859848b8605Smrg/*@}*/
1860848b8605Smrg
1861848b8605Smrg
1862848b8605Smrg/**********************************************************************/
1863848b8605Smrg/** \name Miscellaneous functions                                     */
1864848b8605Smrg/**********************************************************************/
1865848b8605Smrg/*@{*/
1866848b8605Smrg/**
1867848b8605Smrg * Flush commands.
1868848b8605Smrg */
1869848b8605Smrgvoid
1870848b8605Smrg_mesa_flush(struct gl_context *ctx)
1871848b8605Smrg{
1872848b8605Smrg   FLUSH_VERTICES( ctx, 0 );
1873848b8605Smrg   FLUSH_CURRENT( ctx, 0 );
1874848b8605Smrg   if (ctx->Driver.Flush) {
1875848b8605Smrg      ctx->Driver.Flush(ctx);
1876848b8605Smrg   }
1877848b8605Smrg}
1878848b8605Smrg
1879848b8605Smrg
1880848b8605Smrg
1881848b8605Smrg/**
1882b8e80941Smrg * Flush commands and wait for completion.
1883848b8605Smrg *
1884848b8605Smrg * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
1885848b8605Smrg * dd_function_table::Finish driver callback, if not NULL.
1886848b8605Smrg */
1887848b8605Smrgvoid GLAPIENTRY
1888848b8605Smrg_mesa_Finish(void)
1889848b8605Smrg{
1890848b8605Smrg   GET_CURRENT_CONTEXT(ctx);
1891848b8605Smrg   ASSERT_OUTSIDE_BEGIN_END(ctx);
1892b8e80941Smrg
1893b8e80941Smrg   FLUSH_VERTICES(ctx, 0);
1894b8e80941Smrg   FLUSH_CURRENT(ctx, 0);
1895b8e80941Smrg
1896b8e80941Smrg   if (ctx->Driver.Finish) {
1897b8e80941Smrg      ctx->Driver.Finish(ctx);
1898b8e80941Smrg   }
1899848b8605Smrg}
1900848b8605Smrg
1901848b8605Smrg
1902848b8605Smrg/**
1903848b8605Smrg * Execute glFlush().
1904848b8605Smrg *
1905848b8605Smrg * Calls the #ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH macro and the
1906848b8605Smrg * dd_function_table::Flush driver callback, if not NULL.
1907848b8605Smrg */
1908848b8605Smrgvoid GLAPIENTRY
1909848b8605Smrg_mesa_Flush(void)
1910848b8605Smrg{
1911848b8605Smrg   GET_CURRENT_CONTEXT(ctx);
1912848b8605Smrg   ASSERT_OUTSIDE_BEGIN_END(ctx);
1913848b8605Smrg   _mesa_flush(ctx);
1914848b8605Smrg}
1915848b8605Smrg
1916848b8605Smrg
1917848b8605Smrg/*@}*/
1918