Debian Codegraph @

debian/external_deps/fetk/sg/src/gl/context.c

raw
1 /* $Id: context.c,v 1.2 2000/10/27 15:21:40 mholst Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 2.2
6 * Copyright (C) 1995-1997 Brian Paul
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the Free
20 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 /*
24 * If multi-threading is enabled (-DTHREADS) then each thread has it's
25 * own rendering context. A thread obtains the pointer to its GLcontext
26 * with the gl_get_thread_context() function. Otherwise, the global
27 * pointer, CC, points to the current context used by all threads in
28 * the address space.
29 */
30
31
32
33 #include <assert.h>
34 #include <math.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include "accum.h"
39 #include "alphabuf.h"
40 #include "context.h"
41 #include "depth.h"
42 #include "draw.h"
43 #include "eval.h"
44 #include "light.h"
45 #include "lines.h"
46 #include "dlist.h"
47 #include "macros.h"
48 #include "pb.h"
49 #include "points.h"
50 #include "pointers.h"
51 #include "stencil.h"
52 #include "triangle.h"
53 #include "teximage.h"
54 #include "texobj.h"
55 #include "texture.h"
56 #include "types.h"
57 #include "vb.h"
58 #include "vertex.h"
59
60
61
62 /**********************************************************************/
63 /***** Context and Thread management *****/
64 /**********************************************************************/
65
66
67 #ifdef THREADS
68
69 /* A different context may be bound to each thread */
70
71 #include <sys/types.h>
72 #include <unistd.h>
73
74 #define MAX_THREADS 20
75 static pid_t ThreadID[MAX_THREADS];
76 static GLcontext *ThreadContext[MAX_THREADS];
77 static int NumThreads = 0;
78
79 GLcontext *gl_get_thread_context( void )
80 {
81 pid_t id;
82 int i;
83
84 id = getpid();
85
86 for (i=0;i<MAX_THREADS;i++) {
87 if (ThreadID[i]==id) {
88 return ThreadContext[i];
89 }
90 }
91
92 /* No context for this thread */
93 return NULL;
94 }
95
96 static void set_thread_context( GLcontext *ctx )
97 {
98 pid_t id = getpid();
99 int i;
100
101 for (i=0;i<NumThreads;i++) {
102 if (ThreadID[i]==id) {
103 ThreadContext[i] = c;
104 return;
105 }
106 }
107 if (i<MAX_THREADS) {
108 ThreadID[i] = getpid();
109 ThreadContext[i] = c;
110 NumThreads++;
111 }
112 }
113
114 #else
115
116 /* One Current Context pointer for all threads in the address space */
117 GLcontext *CC = NULL;
118
119 #endif /*THREADS*/
120
121
122
123
124 /**********************************************************************/
125 /***** Profiling functions *****/
126 /**********************************************************************/
127
128 #ifdef PROFILE
129
130 #include <sys/times.h>
131 #include <sys/param.h>
132
133
134 /*
135 * Return system time in seconds.
136 * NOTE: this implementation may not be very portable!
137 */
138 GLdouble gl_time( void )
139 {
140 static GLdouble prev_time = 0.0;
141 static GLdouble time;
142 struct tms tm;
143 clock_t clk;
144
145 clk = times(&tm);
146
147 #ifdef CLK_TCK
148 time = (double)clk / (double)CLK_TCK;
149 #else
150 time = (double)clk / (double)HZ;
151 #endif
152
153 if (time>prev_time) {
154 prev_time = time;
155 return time;
156 }
157 else {
158 return prev_time;
159 }
160 }
161
162
163
164 /*
165 * Reset the timing/profiling counters
166 */
167 static void init_timings( GLcontext *ctx )
168 {
169 ctx->BeginEndCount = 0;
170 ctx->BeginEndTime = 0.0;
171 ctx->VertexCount = 0;
172 ctx->VertexTime = 0.0;
173 ctx->PointCount = 0;
174 ctx->PointTime = 0.0;
175 ctx->LineCount = 0;
176 ctx->LineTime = 0.0;
177 ctx->PolygonCount = 0;
178 ctx->PolygonTime = 0.0;
179 ctx->ClearCount = 0;
180 ctx->ClearTime = 0.0;
181 ctx->SwapCount = 0;
182 ctx->SwapTime = 0.0;
183 }
184
185
186
187 /*
188 * Print the accumulated timing/profiling data.
189 */
190 static void print_timings( GLcontext *ctx )
191 {
192 GLdouble beginendrate;
193 GLdouble vertexrate;
194 GLdouble pointrate;
195 GLdouble linerate;
196 GLdouble polygonrate;
197 GLdouble overhead;
198 GLdouble clearrate;
199 GLdouble swaprate;
200 GLdouble avgvertices;
201
202 if (ctx->BeginEndTime>0.0) {
203 beginendrate = ctx->BeginEndCount / ctx->BeginEndTime;
204 }
205 else {
206 beginendrate = 0.0;
207 }
208 if (ctx->VertexTime>0.0) {
209 vertexrate = ctx->VertexCount / ctx->VertexTime;
210 }
211 else {
212 vertexrate = 0.0;
213 }
214 if (ctx->PointTime>0.0) {
215 pointrate = ctx->PointCount / ctx->PointTime;
216 }
217 else {
218 pointrate = 0.0;
219 }
220 if (ctx->LineTime>0.0) {
221 linerate = ctx->LineCount / ctx->LineTime;
222 }
223 else {
224 linerate = 0.0;
225 }
226 if (ctx->PolygonTime>0.0) {
227 polygonrate = ctx->PolygonCount / ctx->PolygonTime;
228 }
229 else {
230 polygonrate = 0.0;
231 }
232 if (ctx->ClearTime>0.0) {
233 clearrate = ctx->ClearCount / ctx->ClearTime;
234 }
235 else {
236 clearrate = 0.0;
237 }
238 if (ctx->SwapTime>0.0) {
239 swaprate = ctx->SwapCount / ctx->SwapTime;
240 }
241 else {
242 swaprate = 0.0;
243 }
244
245 if (ctx->BeginEndCount>0) {
246 avgvertices = (GLdouble) ctx->VertexCount / (GLdouble) ctx->BeginEndCount;
247 }
248 else {
249 avgvertices = 0.0;
250 }
251
252 overhead = ctx->BeginEndTime - ctx->VertexTime - ctx->PointTime
253 - ctx->LineTime - ctx->PolygonTime;
254
255
256 printf(" Count Time (s) Rate (/s) \n");
257 printf("--------------------------------------------------------\n");
258 printf("glBegin/glEnd %7d %8.3f %10.3f\n",
259 ctx->BeginEndCount, ctx->BeginEndTime, beginendrate);
260 printf(" vertexes transformed %7d %8.3f %10.3f\n",
261 ctx->VertexCount, ctx->VertexTime, vertexrate );
262 printf(" points rasterized %7d %8.3f %10.3f\n",
263 ctx->PointCount, ctx->PointTime, pointrate );
264 printf(" lines rasterized %7d %8.3f %10.3f\n",
265 ctx->LineCount, ctx->LineTime, linerate );
266 printf(" polygons rasterized %7d %8.3f %10.3f\n",
267 ctx->PolygonCount, ctx->PolygonTime, polygonrate );
268 printf(" overhead %8.3f\n", overhead );
269 printf("glClear %7d %8.3f %10.3f\n",
270 ctx->ClearCount, ctx->ClearTime, clearrate );
271 printf("SwapBuffers %7d %8.3f %10.3f\n",
272 ctx->SwapCount, ctx->SwapTime, swaprate );
273 printf("\n");
274
275 printf("Average number of vertices per begin/end: %8.3f\n", avgvertices );
276 }
277 #endif
278
279
280
281
282
283 /**********************************************************************/
284 /***** Context allocation, initialization, destroying *****/
285 /**********************************************************************/
286
287
288 /*
289 * Allocate and initialize a shared context state structure.
290 */
291 static struct gl_shared_state *alloc_shared_state( void )
292 {
293 struct gl_shared_state *ss;
294 struct gl_texture_object *t1, *t2, *t3;
295 GLuint i;
296
297 ss = (struct gl_shared_state*) malloc( sizeof(struct gl_shared_state) );
298 if (!ss)
299 return NULL;
300 ss->RefCount = 0;
301
302 /* Display lists */
303 for (i=0;i<MAX_DISPLAYLISTS;i++) {
304 ss->List[i] = NULL;
305 }
306
307 /* Texture objects
308 * There are two default textures, one for the GL_TEXTURE_1D target and
309 * one for the GL_TEXTURE_2D target. Both are named zero!
310 */
311 t1 = gl_alloc_texture_object();
312 if (!t1) {
313 free(ss);
314 return NULL;
315 }
316 t1->Dimensions = 1;
317
318 t2 = gl_alloc_texture_object();
319 if (!t2) {
320 free(ss);
321 free(t1);
322 return NULL;
323 }
324 t2->Dimensions = 2;
325
326 t3 = gl_alloc_texture_object();
327 if (!t3) {
328 free(ss);
329 free(t1);
330 free(t2);
331 return NULL;
332 }
333 t3->Dimensions = 3;
334
335 ss->TexObjectList = t1;
336 ss->TexObjectList->Next = t2;
337 ss->TexObjectList->Next->Next = t3;
338
339 return ss;
340 }
341
342
343 /*
344 * Deallocate a shared state context and all children structures.
345 */
346 static void free_shared_state( struct gl_shared_state *ss )
347 {
348 struct gl_texture_object *t, *tnext;
349
350 /* Free the linked list of texture objects */
351 t = ss->TexObjectList;
352 while (t) {
353 tnext = t->Next;
354 gl_free_texture_object(t);
355 t = tnext;
356 }
357 free(ss);
358 }
359
360
361
362
363 /*
364 * Initialize the nth light. Note that the defaults for light 0 are
365 * different than the other lights.
366 */
367 static void init_light( struct gl_light *l, GLuint n )
368 {
369 ASSIGN_4V( l->Ambient, 0.0, 0.0, 0.0, 1.0 );
370 if (n==0) {
371 ASSIGN_4V( l->Diffuse, 1.0, 1.0, 1.0, 1.0 );
372 ASSIGN_4V( l->Specular, 1.0, 1.0, 1.0, 1.0 );
373 }
374 else {
375 ASSIGN_4V( l->Diffuse, 0.0, 0.0, 0.0, 1.0 );
376 ASSIGN_4V( l->Specular, 0.0, 0.0, 0.0, 1.0 );
377 }
378 ASSIGN_4V( l->Position, 0.0, 0.0, 1.0, 0.0 );
379 ASSIGN_3V( l->Direction, 0.0, 0.0, -1.0 );
380 l->SpotExponent = 0.0;
381 gl_compute_spot_exp_table( l );
382 l->SpotCutoff = 180.0;
383 l->CosCutoff = -1.0;
384 l->ConstantAttenuation = 1.0;
385 l->LinearAttenuation = 0.0;
386 l->QuadraticAttenuation = 0.0;
387 l->Enabled = GL_FALSE;
388 }
389
390
391
392 static void init_lightmodel( struct gl_lightmodel *lm )
393 {
394 ASSIGN_4V( lm->Ambient, 0.2, 0.2, 0.2, 1.0 );
395 lm->LocalViewer = GL_FALSE;
396 lm->TwoSide = GL_FALSE;
397 }
398
399
400 static void init_material( struct gl_material *m )
401 {
402 ASSIGN_4V( m->Ambient, 0.2, 0.2, 0.2, 1.0 );
403 ASSIGN_4V( m->Diffuse, 0.8, 0.8, 0.8, 1.0 );
404 ASSIGN_4V( m->Specular, 0.0, 0.0, 0.0, 1.0 );
405 ASSIGN_4V( m->Emission, 0.0, 0.0, 0.0, 1.0 );
406 m->Shininess = 0.0;
407 m->AmbientIndex = 0;
408 m->DiffuseIndex = 1;
409 m->SpecularIndex = 1;
410 gl_compute_material_shine_table( m );
411 }
412
413
414
415 /*
416 * Initialize a gl_context structure to default values.
417 */
418 static void initialize_context( GLcontext *ctx )
419 {
420 static GLfloat identity[16] = {
421 1.0, 0.0, 0.0, 0.0,
422 0.0, 1.0, 0.0, 0.0,
423 0.0, 0.0, 1.0, 0.0,
424 0.0, 0.0, 0.0, 1.0
425 };
426 GLuint i;
427
428 if (ctx) {
429 /* Transformation matrices and stacks */
430 ctx->ModelViewStackDepth = 0;
431 MEMCPY( ctx->ModelViewMatrix, identity, 16*sizeof(GLfloat) );
432 MEMCPY( ctx->ModelViewInv, identity, 16*sizeof(GLfloat) );
433 ctx->ModelViewInvValid = GL_TRUE;
434
435 ctx->ProjectionStackDepth = 0;
436 MEMCPY( ctx->ProjectionMatrix, identity, 16*sizeof(GLfloat) );
437
438 ctx->TextureStackDepth = 0;
439 MEMCPY( ctx->TextureMatrix, identity, 16*sizeof(GLfloat) );
440 ctx->IdentityTexMat = GL_TRUE;
441
442 /* Accumulate buffer group */
443 ASSIGN_4V( ctx->Accum.ClearColor, 0.0, 0.0, 0.0, 0.0 );
444
445 /* Color buffer group */
446 ctx->Color.IndexMask = 0xffffffff;
447 ctx->Color.ColorMask = 0xf;
448 ctx->Color.SWmasking = GL_FALSE;
449 ctx->Color.ClearIndex = 0;
450 ASSIGN_4V( ctx->Color.ClearColor, 0.0, 0.0, 0.0, 0.0 );
451 ctx->Color.DrawBuffer = GL_FRONT;
452 ctx->Color.AlphaEnabled = GL_FALSE;
453 ctx->Color.AlphaFunc = GL_ALWAYS;
454 ctx->Color.AlphaRef = 0.0;
455 ctx->Color.AlphaRefUbyte = 0;
456 ctx->Color.BlendEnabled = GL_FALSE;
457 ctx->Color.BlendSrc = GL_ONE;
458 ctx->Color.BlendDst = GL_ZERO;
459 ctx->Color.BlendEquation = GL_FUNC_ADD_EXT;
460 ASSIGN_4V( ctx->Color.BlendColor, 0.0, 0.0, 0.0, 0.0 );
461 ctx->Color.IndexLogicOpEnabled = GL_FALSE;
462 ctx->Color.ColorLogicOpEnabled = GL_FALSE;
463 ctx->Color.SWLogicOpEnabled = GL_FALSE;
464 ctx->Color.LogicOp = GL_COPY;
465 ctx->Color.DitherFlag = GL_TRUE;
466
467 /* Current group */
468 ctx->Current.Index = 1;
469 ASSIGN_3V( ctx->Current.Normal, 0.0, 0.0, 1.0 );
470 ctx->Current.IntColor[0] = (GLint) ctx->Visual->RedScale;
471 ctx->Current.IntColor[1] = (GLint) ctx->Visual->GreenScale;
472 ctx->Current.IntColor[2] = (GLint) ctx->Visual->BlueScale;
473 ctx->Current.IntColor[3] = (GLint) ctx->Visual->AlphaScale;
474 ASSIGN_4V( ctx->Current.RasterPos, 0.0, 0.0, 0.0, 1.0 );
475 ctx->Current.RasterPosValid = GL_TRUE;
476 ctx->Current.RasterIndex = 1;
477 ASSIGN_4V( ctx->Current.TexCoord, 0.0, 0.0, 0.0, 1.0 );
478 ASSIGN_4V( ctx->Current.RasterColor, 1.0, 1.0, 1.0, 1.0 );
479 ctx->Current.EdgeFlag = GL_TRUE;
480
481 /* Depth buffer group */
482 ctx->Depth.Test = GL_FALSE;
483 ctx->Depth.Clear = 1.0;
484 ctx->Depth.Func = GL_LESS;
485 ctx->Depth.Mask = GL_TRUE;
486
487 /* Evaluators group */
488 ctx->Eval.Map1Color4 = GL_FALSE;
489 ctx->Eval.Map1Index = GL_FALSE;
490 ctx->Eval.Map1Normal = GL_FALSE;
491 ctx->Eval.Map1TextureCoord1 = GL_FALSE;
492 ctx->Eval.Map1TextureCoord2 = GL_FALSE;
493 ctx->Eval.Map1TextureCoord3 = GL_FALSE;
494 ctx->Eval.Map1TextureCoord4 = GL_FALSE;
495 ctx->Eval.Map1Vertex3 = GL_FALSE;
496 ctx->Eval.Map1Vertex4 = GL_FALSE;
497 ctx->Eval.Map2Color4 = GL_FALSE;
498 ctx->Eval.Map2Index = GL_FALSE;
499 ctx->Eval.Map2Normal = GL_FALSE;
500 ctx->Eval.Map2TextureCoord1 = GL_FALSE;
501 ctx->Eval.Map2TextureCoord2 = GL_FALSE;
502 ctx->Eval.Map2TextureCoord3 = GL_FALSE;
503 ctx->Eval.Map2TextureCoord4 = GL_FALSE;
504 ctx->Eval.Map2Vertex3 = GL_FALSE;
505 ctx->Eval.Map2Vertex4 = GL_FALSE;
506 ctx->Eval.AutoNormal = GL_FALSE;
507 ctx->Eval.MapGrid1un = 1;
508 ctx->Eval.MapGrid1u1 = 0.0;
509 ctx->Eval.MapGrid1u2 = 1.0;
510 ctx->Eval.MapGrid2un = 1;
511 ctx->Eval.MapGrid2vn = 1;
512 ctx->Eval.MapGrid2u1 = 0.0;
513 ctx->Eval.MapGrid2u2 = 1.0;
514 ctx->Eval.MapGrid2v1 = 0.0;
515 ctx->Eval.MapGrid2v2 = 1.0;
516
517 /* Fog group */
518 ctx->Fog.Enabled = GL_FALSE;
519 ctx->Fog.Mode = GL_EXP;
520 ASSIGN_4V( ctx->Fog.Color, 0.0, 0.0, 0.0, 0.0 );
521 ctx->Fog.Index = 0.0;
522 ctx->Fog.Density = 1.0;
523 ctx->Fog.Start = 0.0;
524 ctx->Fog.End = 1.0;
525
526 /* Hint group */
527 ctx->Hint.PerspectiveCorrection = GL_DONT_CARE;
528 ctx->Hint.PointSmooth = GL_DONT_CARE;
529 ctx->Hint.LineSmooth = GL_DONT_CARE;
530 ctx->Hint.PolygonSmooth = GL_DONT_CARE;
531 ctx->Hint.Fog = GL_DONT_CARE;
532
533 /* Lighting group */
534 for (i=0;i<MAX_LIGHTS;i++) {
535 init_light( &ctx->Light.Light[i], i );
536 }
537 init_lightmodel( &ctx->Light.Model );
538 init_material( &ctx->Light.Material[0] );
539 init_material( &ctx->Light.Material[1] );
540 ctx->Light.ShadeModel = GL_SMOOTH;
541 ctx->Light.Enabled = GL_FALSE;
542 ctx->Light.ColorMaterialFace = GL_FRONT_AND_BACK;
543 ctx->Light.ColorMaterialMode = GL_AMBIENT_AND_DIFFUSE;
544 ctx->Light.ColorMaterialBitmask
545 = gl_material_bitmask( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE );
546
547 ctx->Light.ColorMaterialEnabled = GL_FALSE;
548
549 /* Line group */
550 ctx->Line.SmoothFlag = GL_FALSE;
551 ctx->Line.StippleFlag = GL_FALSE;
552 ctx->Line.Width = 1.0;
553 ctx->Line.StipplePattern = 0xffff;
554 ctx->Line.StippleFactor = 1;
555
556 /* Display List group */
557 ctx->List.ListBase = 0;
558
559 /* Pixel group */
560 ctx->Pixel.RedBias = 0.0;
561 ctx->Pixel.RedScale = 1.0;
562 ctx->Pixel.GreenBias = 0.0;
563 ctx->Pixel.GreenScale = 1.0;
564 ctx->Pixel.BlueBias = 0.0;
565 ctx->Pixel.BlueScale = 1.0;
566 ctx->Pixel.AlphaBias = 0.0;
567 ctx->Pixel.AlphaScale = 1.0;
568 ctx->Pixel.DepthBias = 0.0;
569 ctx->Pixel.DepthScale = 1.0;
570 ctx->Pixel.IndexOffset = 0;
571 ctx->Pixel.IndexShift = 0;
572 ctx->Pixel.ZoomX = 1.0;
573 ctx->Pixel.ZoomY = 1.0;
574 ctx->Pixel.MapColorFlag = GL_FALSE;
575 ctx->Pixel.MapStencilFlag = GL_FALSE;
576 ctx->Pixel.MapStoSsize = 1;
577 ctx->Pixel.MapItoIsize = 1;
578 ctx->Pixel.MapItoRsize = 1;
579 ctx->Pixel.MapItoGsize = 1;
580 ctx->Pixel.MapItoBsize = 1;
581 ctx->Pixel.MapItoAsize = 1;
582 ctx->Pixel.MapRtoRsize = 1;
583 ctx->Pixel.MapGtoGsize = 1;
584 ctx->Pixel.MapBtoBsize = 1;
585 ctx->Pixel.MapAtoAsize = 1;
586 ctx->Pixel.MapStoS[0] = 0;
587 ctx->Pixel.MapItoI[0] = 0;
588 ctx->Pixel.MapItoR[0] = 0.0;
589 ctx->Pixel.MapItoG[0] = 0.0;
590 ctx->Pixel.MapItoB[0] = 0.0;
591 ctx->Pixel.MapItoA[0] = 0.0;
592 ctx->Pixel.MapRtoR[0] = 0.0;
593 ctx->Pixel.MapGtoG[0] = 0.0;
594 ctx->Pixel.MapBtoB[0] = 0.0;
595 ctx->Pixel.MapAtoA[0] = 0.0;
596
597 /* Point group */
598 ctx->Point.SmoothFlag = GL_FALSE;
599 ctx->Point.Size = 1.0;
600
601 /* Polygon group */
602 ctx->Polygon.CullFlag = GL_FALSE;
603 ctx->Polygon.CullFaceMode = GL_BACK;
604 ctx->Polygon.FrontFace = GL_CCW;
605 ctx->Polygon.FrontMode = GL_FILL;
606 ctx->Polygon.BackMode = GL_FILL;
607 ctx->Polygon.Unfilled = GL_FALSE;
608 ctx->Polygon.SmoothFlag = GL_FALSE;
609 ctx->Polygon.StippleFlag = GL_FALSE;
610 ctx->Polygon.OffsetFactor = 0.0F;
611 ctx->Polygon.OffsetUnits = 0.0F;
612 ctx->Polygon.OffsetPoint = GL_FALSE;
613 ctx->Polygon.OffsetLine = GL_FALSE;
614 ctx->Polygon.OffsetFill = GL_FALSE;
615 ctx->Polygon.OffsetAny = GL_FALSE;
616
617 /* Polygon Stipple group */
618 MEMSET( ctx->PolygonStipple, 0xff, 32*sizeof(GLuint) );
619
620 /* Scissor group */
621 ctx->Scissor.Enabled = GL_FALSE;
622 ctx->Scissor.X = 0;
623 ctx->Scissor.Y = 0;
624 ctx->Scissor.Width = 0;
625 ctx->Scissor.Height = 0;
626
627 /* Stencil group */
628 ctx->Stencil.Enabled = GL_FALSE;
629 ctx->Stencil.Function = GL_ALWAYS;
630 ctx->Stencil.FailFunc = GL_KEEP;
631 ctx->Stencil.ZPassFunc = GL_KEEP;
632 ctx->Stencil.ZFailFunc = GL_KEEP;
633 ctx->Stencil.Ref = 0;
634 ctx->Stencil.ValueMask = 0xff;
635 ctx->Stencil.Clear = 0;
636 ctx->Stencil.WriteMask = 0xff;
637
638 /* Texture group */
639 ctx->Texture.Enabled = 0;
640 ctx->Texture.EnvMode = GL_MODULATE;
641 ASSIGN_4V( ctx->Texture.EnvColor, 0.0, 0.0, 0.0, 0.0 );
642 ctx->Texture.TexGenEnabled = 0;
643 ctx->Texture.GenModeS = GL_EYE_LINEAR;
644 ctx->Texture.GenModeT = GL_EYE_LINEAR;
645 ctx->Texture.GenModeR = GL_EYE_LINEAR;
646 ctx->Texture.GenModeQ = GL_EYE_LINEAR;
647 ASSIGN_4V( ctx->Texture.ObjectPlaneS, 1.0, 0.0, 0.0, 0.0 );
648 ASSIGN_4V( ctx->Texture.ObjectPlaneT, 0.0, 1.0, 0.0, 0.0 );
649 ASSIGN_4V( ctx->Texture.ObjectPlaneR, 0.0, 0.0, 0.0, 0.0 );
650 ASSIGN_4V( ctx->Texture.ObjectPlaneQ, 0.0, 0.0, 0.0, 0.0 );
651 ASSIGN_4V( ctx->Texture.EyePlaneS, 1.0, 0.0, 0.0, 0.0 );
652 ASSIGN_4V( ctx->Texture.EyePlaneT, 0.0, 1.0, 0.0, 0.0 );
653 ASSIGN_4V( ctx->Texture.EyePlaneR, 0.0, 0.0, 0.0, 0.0 );
654 ASSIGN_4V( ctx->Texture.EyePlaneQ, 0.0, 0.0, 0.0, 0.0 );
655
656 assert( ctx->Shared->TexObjectList );
657 assert( ctx->Shared->TexObjectList->Next );
658 ctx->Texture.Current1D = ctx->Shared->TexObjectList;
659 ctx->Texture.Current2D = ctx->Shared->TexObjectList->Next;
660 ctx->Texture.Current3D = ctx->Shared->TexObjectList->Next->Next;
661
662 /* Transformation group */
663 ctx->Transform.MatrixMode = GL_MODELVIEW;
664 ctx->Transform.Normalize = GL_FALSE;
665 for (i=0;i<MAX_CLIP_PLANES;i++) {
666 ctx->Transform.ClipEnabled[i] = GL_FALSE;
667 ASSIGN_4V( ctx->Transform.ClipEquation[i], 0.0, 0.0, 0.0, 0.0 );
668 }
669 ctx->Transform.AnyClip = GL_FALSE;
670
671 /* Viewport group */
672 ctx->Viewport.X = 0;
673 ctx->Viewport.Y = 0;
674 ctx->Viewport.Width = 0;
675 ctx->Viewport.Height = 0;
676 ctx->Viewport.Near = 0.0;
677 ctx->Viewport.Far = 1.0;
678 ctx->Viewport.Sx = 0.0; /* Sx, Tx, Sy, Ty are computed later */
679 ctx->Viewport.Tx = 0.0;
680 ctx->Viewport.Sy = 0.0;
681 ctx->Viewport.Ty = 0.0;
682 ctx->Viewport.Sz = 0.5 * DEPTH_SCALE;
683 ctx->Viewport.Tz = 0.5 * DEPTH_SCALE;
684
685 /* Pixel transfer */
686 ctx->Pack.Alignment = 4;
687 ctx->Pack.RowLength = 0;
688 ctx->Pack.SkipPixels = 0;
689 ctx->Pack.SkipRows = 0;
690 ctx->Pack.SwapBytes = GL_FALSE;
691 ctx->Pack.LsbFirst = GL_FALSE;
692 ctx->Unpack.Alignment = 4;
693 ctx->Unpack.RowLength = 0;
694 ctx->Unpack.SkipPixels = 0;
695 ctx->Unpack.SkipRows = 0;
696 ctx->Unpack.SwapBytes = GL_FALSE;
697 ctx->Unpack.LsbFirst = GL_FALSE;
698
699 /* Feedback */
700 ctx->Feedback.Type = GL_2D; /* TODO: verify */
701 ctx->Feedback.Buffer = NULL;
702 ctx->Feedback.BufferSize = 0;
703 ctx->Feedback.Count = 0;
704
705 /* Selection/picking */
706 ctx->Select.Buffer = NULL;
707 ctx->Select.BufferSize = 0;
708 ctx->Select.BufferCount = 0;
709 ctx->Select.Hits = 0;
710 ctx->Select.NameStackDepth = 0;
711
712 /* Renderer and client attribute stacks */
713 ctx->AttribStackDepth = 0;
714 ctx->ClientAttribStackDepth = 0;
715
716 /*** Miscellaneous ***/
717 ctx->NewState = NEW_ALL;
718 ctx->RenderMode = GL_RENDER;
719 ctx->Primitive = GL_BITMAP;
720
721 ctx->StippleCounter = 0;
722 ctx->NeedNormals = GL_FALSE;
723
724 if ( ctx->Visual->RedScale==255.0F
725 && ctx->Visual->GreenScale==255.0F
726 && ctx->Visual->BlueScale==255.0F
727 && ctx->Visual->AlphaScale==255.0F) {
728 ctx->Visual->EightBitColor = GL_TRUE;
729 }
730 else {
731 ctx->Visual->EightBitColor = GL_FALSE;
732 }
733 ctx->FastDrawPixels = ctx->Visual->RGBAflag && ctx->Visual->EightBitColor;
734
735 ctx->PointsFunc = NULL;
736 ctx->LineFunc = NULL;
737 ctx->TriangleFunc = NULL;
738
739 ctx->CallDepth = 0;
740 ctx->ExecuteFlag = GL_TRUE;
741 ctx->CompileFlag = GL_FALSE;
742
743 ctx->ErrorValue = GL_NO_ERROR;
744
745 /* For debug/development only */
746 ctx->NoRaster = getenv("MESA_NO_RASTER") ? GL_TRUE : GL_FALSE;
747 }
748 }
749
750
751
752 /*
753 * Allocate a new GLvisual object.
754 * Input: rgb_flag - GL_TRUE=RGB(A) mode, GL_FALSE=Color Index mode
755 * alpha_flag - alloc software alpha buffers?
756 * db_flag - double buffering?
757 * depth_bits - requested minimum bits per depth buffer value
758 * stencil_bits - requested minimum bits per stencil buffer value
759 * accum_bits - requested minimum bits per accum buffer component
760 * index_bits - number of bits per pixel if rgb_flag==GL_FALSE
761 * red, green, blue_scale - float-to-int color scaling
762 * alpha_scale - ignored if alpha_flag is true
763 */
764 GLvisual *gl_create_visual( GLboolean rgb_flag,
765 GLboolean alpha_flag,
766 GLboolean db_flag,
767 GLint depth_bits,
768 GLint stencil_bits,
769 GLint accum_bits,
770 GLint index_bits,
771 GLfloat red_scale,
772 GLfloat green_scale,
773 GLfloat blue_scale,
774 GLfloat alpha_scale )
775 {
776 GLvisual *vis;
777
778 /* Can't do better than 8-bit/channel color at this time */
779 assert( red_scale<=255.0 );
780 assert( green_scale<=255.0 );
781 assert( blue_scale<=255.0 );
782 assert( alpha_scale<=255.0 );
783
784 if (depth_bits > 8*sizeof(GLdepth)) {
785 /* can't meet depth buffer requirements */
786 return NULL;
787 }
788 if (stencil_bits > 8*sizeof(GLstencil)) {
789 /* can't meet stencil buffer requirements */
790 return NULL;
791 }
792 if (accum_bits > 8*sizeof(GLaccum)) {
793 /* can't meet accum buffer requirements */
794 return NULL;
795 }
796
797 vis = (GLvisual *) calloc( 1, sizeof(GLvisual) );
798 if (!vis) {
799 return NULL;
800 }
801
802 vis->RGBAflag = rgb_flag;
803 vis->DBflag = db_flag;
804 vis->RedScale = red_scale;
805 vis->GreenScale = green_scale;
806 vis->BlueScale = blue_scale;
807 vis->AlphaScale = alpha_scale;
808 if (red_scale) {
809 vis->InvRedScale = 1.0F / red_scale;
810 }
811 if (green_scale) {
812 vis->InvGreenScale = 1.0F / green_scale;
813 }
814 if (blue_scale) {
815 vis->InvBlueScale = 1.0F / blue_scale;
816 }
817 if (alpha_scale) {
818 vis->InvAlphaScale = 1.0F / alpha_scale;
819 }
820 vis->IndexBits = index_bits;
821 vis->DepthBits = depth_bits;
822 vis->AccumBits = accum_bits;
823 vis->StencilBits = stencil_bits;
824
825 if (red_scale==255.0F && green_scale==255.0F
826 && blue_scale==255.0F && alpha_scale==255.0F) {
827 vis->EightBitColor = GL_TRUE;
828 }
829 else {
830 vis->EightBitColor = GL_FALSE;
831 }
832
833 /* software alpha buffers */
834 if (alpha_flag) {
835 vis->FrontAlphaEnabled = GL_TRUE;
836 if (db_flag) {
837 vis->BackAlphaEnabled = GL_TRUE;
838 }
839 }
840
841 return vis;
842 }
843
844
845
846
847 void gl_destroy_visual( GLvisual *vis )
848 {
849 free( vis );
850 }
851
852
853
854 /*
855 * Allocate the proxy textures. If we run out of memory part way through
856 * the allocations clean up and return GL_FALSE.
857 * Return: GL_TRUE=success, GL_FALSE=failure
858 */
859 static GLboolean alloc_proxy_textures( GLcontext *ctx )
860 {
861 GLboolean out_of_memory;
862 GLint i;
863
864 ctx->Texture.Proxy1D = gl_alloc_texture_object();
865 if (!ctx->Texture.Proxy1D) {
866 return GL_FALSE;
867 }
868
869 ctx->Texture.Proxy2D = gl_alloc_texture_object();
870 if (!ctx->Texture.Proxy2D) {
871 gl_free_texture_object(ctx->Texture.Proxy1D);
872 return GL_FALSE;
873 }
874
875 ctx->Texture.Proxy2D = gl_alloc_texture_object();
876 if (!ctx->Texture.Proxy2D) {
877 gl_free_texture_object(ctx->Texture.Proxy1D);
878 return GL_FALSE;
879 }
880
881 ctx->Texture.Proxy3D = gl_alloc_texture_object();
882 if (!ctx->Texture.Proxy3D) {
883 gl_free_texture_object(ctx->Texture.Proxy1D);
884 gl_free_texture_object(ctx->Texture.Proxy2D);
885 return GL_FALSE;
886 }
887
888 out_of_memory = GL_FALSE;
889 for (i=0;i<MAX_TEXTURE_LEVELS;i++) {
890 ctx->Texture.Proxy1D->Image[i] = gl_alloc_texture_image();
891 ctx->Texture.Proxy2D->Image[i] = gl_alloc_texture_image();
892 ctx->Texture.Proxy3D->Image[i] = gl_alloc_texture_image();
893 if (!ctx->Texture.Proxy1D->Image[i]
894 || !ctx->Texture.Proxy2D->Image[i]
895 || !ctx->Texture.Proxy3D->Image[i]) {
896 out_of_memory = GL_TRUE;
897 }
898 }
899 if (out_of_memory) {
900 for (i=0;i<MAX_TEXTURE_LEVELS;i++) {
901 if (ctx->Texture.Proxy1D->Image[i]) {
902 gl_free_texture_image(ctx->Texture.Proxy1D->Image[i]);
903 }
904 if (ctx->Texture.Proxy2D->Image[i]) {
905 gl_free_texture_image(ctx->Texture.Proxy2D->Image[i]);
906 }
907 if (ctx->Texture.Proxy3D->Image[i]) {
908 gl_free_texture_image(ctx->Texture.Proxy3D->Image[i]);
909 }
910 }
911 gl_free_texture_object(ctx->Texture.Proxy1D);
912 gl_free_texture_object(ctx->Texture.Proxy2D);
913 gl_free_texture_object(ctx->Texture.Proxy3D);
914 return GL_FALSE;
915 }
916 else {
917 return GL_TRUE;
918 }
919 }
920
921
922
923 /*
924 * Allocate and initialize a GLcontext structure.
925 * Input: visual - a GLvisual pointer
926 * sharelist - another context to share display lists with or NULL
927 * driver_ctx - pointer to device driver's context state struct
928 * Return: pointer to a new gl_context struct or NULL if error.
929 */
930 GLcontext *gl_create_context( GLvisual *visual,
931 GLcontext *share_list,
932 void *driver_ctx )
933 {
934 GLcontext *ctx;
935
936 /* do some implementation tests */
937 assert( sizeof(GLbyte) == 1 );
938 assert( sizeof(GLshort) >= 2 );
939 assert( sizeof(GLint) >= 4 );
940 assert( sizeof(GLubyte) == 1 );
941 assert( sizeof(GLushort) >= 2 );
942 assert( sizeof(GLuint) >= 4 );
943
944 ctx = (GLcontext *) calloc( 1, sizeof(GLcontext) );
945 if (!ctx) {
946 return NULL;
947 }
948
949 ctx->DriverCtx = driver_ctx;
950
951 ctx->Visual = visual;
952 ctx->Buffer = NULL;
953
954 gl_init_lists();
955 gl_init_eval(ctx);
956
957 ctx->VB = (struct vertex_buffer *) malloc(sizeof(struct vertex_buffer));
958 if (!ctx->VB) {
959 free( ctx );
960 return NULL;
961 }
962 gl_init_vb( ctx->VB );
963
964 ctx->PB = (struct pixel_buffer *) malloc(sizeof(struct pixel_buffer));
965 if (!ctx->PB) {
966 free( ctx->VB );
967 free( ctx );
968 return NULL;
969 }
970 PB_INIT( ctx->PB, GL_BITMAP );
971
972 if (share_list) {
973 /* share the group of display lists of another context */
974 ctx->Shared = share_list->Shared;
975 }
976 else {
977 /* allocate new group of display lists */
978 ctx->Shared = alloc_shared_state();
979 if (!ctx->Shared) {
980 free(ctx->VB);
981 free(ctx->PB);
982 free(ctx);
983 return NULL;
984 }
985 }
986 ctx->Shared->RefCount++;
987
988 initialize_context( ctx );
989
990 if (visual->DBflag) {
991 ctx->Color.DrawBuffer = GL_BACK;
992 ctx->Pixel.ReadBuffer = GL_BACK;
993 }
994 else {
995 ctx->Color.DrawBuffer = GL_FRONT;
996 ctx->Pixel.ReadBuffer = GL_FRONT;
997 }
998
999 #ifdef PROFILE
1000 init_timings( ctx );
1001 #endif
1002
1003 #ifdef GL_VERSION_1_1
1004 if (!alloc_proxy_textures(ctx)) {
1005 free_shared_state(ctx->Shared);
1006 free(ctx->VB);
1007 free(ctx->PB);
1008 free(ctx);
1009 return NULL;
1010 }
1011 #endif
1012
1013 gl_init_api_function_pointers( ctx );
1014 ctx->API = ctx->Exec; /* GL_EXECUTE is default */
1015
1016 return ctx;
1017 }
1018
1019
1020
1021
1022 /*
1023 * Destroy a gl_context structure.
1024 */
1025 void gl_destroy_context( GLcontext *ctx )
1026 {
1027 if (ctx) {
1028 GLint i;
1029
1030 #ifdef PROFILE
1031 if (getenv("MESA_PROFILE")) {
1032 print_timings( ctx );
1033 }
1034 #endif
1035
1036 free( ctx->PB );
1037 free( ctx->VB );
1038
1039 ctx->Shared->RefCount--;
1040 assert(ctx->Shared->RefCount>=0);
1041 if (ctx->Shared->RefCount==0) {
1042 /* free shared state */
1043 free_shared_state( ctx->Shared );
1044 }
1045
1046 /* Free proxy texture info */
1047 for (i=0;i<MAX_TEXTURE_LEVELS;i++) {
1048 free( ctx->Texture.Proxy1D->Image[i] );
1049 free( ctx->Texture.Proxy2D->Image[i] );
1050 free( ctx->Texture.Proxy3D->Image[i] );
1051 }
1052 free( ctx->Texture.Proxy1D );
1053 free( ctx->Texture.Proxy2D );
1054 free( ctx->Texture.Proxy3D );
1055
1056 free( (void *) ctx );
1057
1058 #ifndef THREADS
1059 if (ctx==CC) {
1060 CC = NULL;
1061 }
1062 #endif
1063
1064 }
1065 }
1066
1067
1068
1069
1070 /*
1071 * Create a new framebuffer. A GLframebuffer is a struct which
1072 * encapsulates the depth, stencil and accum buffers and related
1073 * parameters.
1074 * Input: visual - a GLvisual pointer
1075 * Return: pointer to new GLframebuffer struct or NULL if error.
1076 */
1077 GLframebuffer *gl_create_framebuffer( GLvisual *visual )
1078 {
1079 GLframebuffer *buffer;
1080
1081 buffer = (GLframebuffer *) calloc( 1, sizeof(GLframebuffer) );
1082 if (!buffer) {
1083 return NULL;
1084 }
1085
1086 buffer->Visual = visual;
1087
1088 return buffer;
1089 }
1090
1091
1092
1093 /*
1094 * Free a framebuffer struct and its buffers.
1095 */
1096 void gl_destroy_framebuffer( GLframebuffer *buffer )
1097 {
1098 if (buffer) {
1099 if (buffer->Depth) {
1100 free( buffer->Depth );
1101 }
1102 if (buffer->Accum) {
1103 free( buffer->Accum );
1104 }
1105 if (buffer->Stencil) {
1106 free( buffer->Stencil );
1107 }
1108 if (buffer->FrontAlpha) {
1109 free( buffer->FrontAlpha );
1110 }
1111 if (buffer->BackAlpha) {
1112 free( buffer->BackAlpha );
1113 }
1114 free(buffer);
1115 }
1116 }
1117
1118
1119
1120
1121
1122 /*
1123 * Set the current context, binding the given frame buffer to the context.
1124 */
1125 void gl_make_current( GLcontext *ctx, GLframebuffer *buffer )
1126 {
1127 #ifdef THREADS
1128 /* TODO: unbind old buffer from context? */
1129 set_thread_context( ctx );
1130 #else
1131 if (CC && CC->Buffer) {
1132 /* unbind frame buffer from context */
1133 CC->Buffer = NULL;
1134 }
1135 CC = ctx;
1136 #endif
1137
1138 if (ctx && buffer) {
1139 /* TODO: check if ctx and buffer's visual match??? */
1140 ctx->Buffer = buffer; /* Bind the frame buffer to the context */
1141 ctx->NewState = NEW_ALL; /* just to be safe */
1142 gl_update_state( ctx );
1143 }
1144 }
1145
1146
1147
1148 /**********************************************************************/
1149 /***** Miscellaneous functions *****/
1150 /**********************************************************************/
1151
1152
1153 /*
1154 * Copy attribute groups from one context to another.
1155 * Input: src - source context
1156 * dst - destination context
1157 * mask - bitwise OR of GL_*_BIT flags
1158 */
1159 void gl_copy_context( GLcontext *src, GLcontext *dst, GLuint mask )
1160 {
1161 if (mask & GL_ACCUM_BUFFER_BIT) {
1162 MEMCPY( &dst->Accum, &src->Accum, sizeof(struct gl_accum_attrib) );
1163 }
1164 if (mask & GL_COLOR_BUFFER_BIT) {
1165 MEMCPY( &dst->Color, &src->Color, sizeof(struct gl_colorbuffer_attrib) );
1166 }
1167 if (mask & GL_CURRENT_BIT) {
1168 MEMCPY( &dst->Current, &src->Current, sizeof(struct gl_current_attrib) );
1169 }
1170 if (mask & GL_DEPTH_BUFFER_BIT) {
1171 MEMCPY( &dst->Depth, &src->Depth, sizeof(struct gl_depthbuffer_attrib) );
1172 }
1173 if (mask & GL_ENABLE_BIT) {
1174 /* no op */
1175 }
1176 if (mask & GL_EVAL_BIT) {
1177 MEMCPY( &dst->Eval, &src->Eval, sizeof(struct gl_eval_attrib) );
1178 }
1179 if (mask & GL_FOG_BIT) {
1180 MEMCPY( &dst->Fog, &src->Fog, sizeof(struct gl_fog_attrib) );
1181 }
1182 if (mask & GL_HINT_BIT) {
1183 MEMCPY( &dst->Hint, &src->Hint, sizeof(struct gl_hint_attrib) );
1184 }
1185 if (mask & GL_LIGHTING_BIT) {
1186 MEMCPY( &dst->Light, &src->Light, sizeof(struct gl_light_attrib) );
1187 }
1188 if (mask & GL_LINE_BIT) {
1189 MEMCPY( &dst->Line, &src->Line, sizeof(struct gl_line_attrib) );
1190 }
1191 if (mask & GL_LIST_BIT) {
1192 MEMCPY( &dst->List, &src->List, sizeof(struct gl_list_attrib) );
1193 }
1194 if (mask & GL_PIXEL_MODE_BIT) {
1195 MEMCPY( &dst->Pixel, &src->Pixel, sizeof(struct gl_pixel_attrib) );
1196 }
1197 if (mask & GL_POINT_BIT) {
1198 MEMCPY( &dst->Point, &src->Point, sizeof(struct gl_point_attrib) );
1199 }
1200 if (mask & GL_POLYGON_BIT) {
1201 MEMCPY( &dst->Polygon, &src->Polygon, sizeof(struct gl_polygon_attrib) );
1202 }
1203 if (mask & GL_POLYGON_STIPPLE_BIT) {
1204 MEMCPY( &dst->PolygonStipple, &src->PolygonStipple, 32*sizeof(GLuint) );
1205 }
1206 if (mask & GL_SCISSOR_BIT) {
1207 MEMCPY( &dst->Scissor, &src->Scissor, sizeof(struct gl_scissor_attrib) );
1208 }
1209 if (mask & GL_STENCIL_BUFFER_BIT) {
1210 MEMCPY( &dst->Stencil, &src->Stencil, sizeof(struct gl_stencil_attrib) );
1211 }
1212 if (mask & GL_TEXTURE_BIT) {
1213 MEMCPY( &dst->Texture, &src->Texture, sizeof(struct gl_texture_attrib) );
1214 }
1215 if (mask & GL_TRANSFORM_BIT) {
1216 MEMCPY( &dst->Transform, &src->Transform, sizeof(struct gl_transform_attrib) );
1217 }
1218 if (mask & GL_VIEWPORT_BIT) {
1219 MEMCPY( &dst->Viewport, &src->Viewport, sizeof(struct gl_viewport_attrib) );
1220 }
1221 }
1222
1223
1224
1225 /*
1226 * This function is called when the Mesa user has stumbled into a code
1227 * path which may not be implemented fully or correctly.
1228 */
1229 void gl_problem( GLcontext *ctx, const char *s )
1230 {
1231 fprintf( stderr, "Mesa implementation error: %s\n", s );
1232 fprintf( stderr, "Report to Mesa author.\n" );
1233 }
1234
1235
1236
1237 /*
1238 * This is called to inform the user that he or she has tried to do
1239 * something illogical or if there's likely a bug in their program
1240 * (like enabled depth testing without a depth buffer).
1241 */
1242 void gl_warning( GLcontext *ctx, const char *s )
1243 {
1244 GLboolean debug;
1245 #ifdef DEBUG
1246 debug = GL_TRUE;
1247 #else
1248 if (getenv("MESA_DEBUG")) {
1249 debug = GL_TRUE;
1250 }
1251 else {
1252 debug = GL_FALSE;
1253 }
1254 #endif
1255 if (debug) {
1256 fprintf( stderr, "Mesa warning: %s\n", s );
1257 }
1258 }
1259
1260
1261
1262 /*
1263 * This is Mesa's error handler. Normally, all that's done is the updating
1264 * of the current error value. If Mesa is compiled with -DDEBUG or if the
1265 * environment variable "MESA_DEBUG" is defined then a real error message
1266 * is printed to stderr.
1267 * Input: error - the error value
1268 * s - a diagnostic string
1269 */
1270 void gl_error( GLcontext *ctx, GLenum error, const char *s )
1271 {
1272 GLboolean debug;
1273
1274 #ifdef DEBUG
1275 debug = GL_TRUE;
1276 #else
1277 if (getenv("MESA_DEBUG")) {
1278 debug = GL_TRUE;
1279 }
1280 else {
1281 debug = GL_FALSE;
1282 }
1283 #endif
1284
1285 if (debug) {
1286 char errstr[1000];
1287
1288 switch (error) {
1289 case GL_NO_ERROR:
1290 strcpy( errstr, "GL_NO_ERROR" );
1291 break;
1292 case GL_INVALID_VALUE:
1293 strcpy( errstr, "GL_INVALID_VALUE" );
1294 break;
1295 case GL_INVALID_ENUM:
1296 strcpy( errstr, "GL_INVALID_ENUM" );
1297 break;
1298 case GL_INVALID_OPERATION:
1299 strcpy( errstr, "GL_INVALID_OPERATION" );
1300 break;
1301 case GL_STACK_OVERFLOW:
1302 strcpy( errstr, "GL_STACK_OVERFLOW" );
1303 break;
1304 case GL_STACK_UNDERFLOW:
1305 strcpy( errstr, "GL_STACK_UNDERFLOW" );
1306 break;
1307 case GL_OUT_OF_MEMORY:
1308 strcpy( errstr, "GL_OUT_OF_MEMORY" );
1309 break;
1310 default:
1311 strcpy( errstr, "unknown" );
1312 break;
1313 }
1314 fprintf( stderr, "Mesa user error: %s in %s\n", errstr, s );
1315 }
1316
1317 if (ctx->ErrorValue==GL_NO_ERROR) {
1318 ctx->ErrorValue = error;
1319 }
1320
1321 /* Call device driver's error handler, if any. This is used on the Mac. */
1322 if (ctx->Driver.Error) {
1323 (*ctx->Driver.Error)( ctx );
1324 }
1325 }
1326
1327
1328
1329
1330 GLenum gl_GetError( GLcontext *ctx )
1331 {
1332 GLenum e;
1333
1334 if (INSIDE_BEGIN_END(ctx)) {
1335 gl_error( ctx, GL_INVALID_OPERATION, "glGetError" );
1336 return GL_INVALID_OPERATION;
1337 }
1338
1339 e = ctx->ErrorValue;
1340 ctx->ErrorValue = GL_NO_ERROR;
1341 return e;
1342 }
1343
1344
1345
1346 void gl_ResizeBuffersMESA( GLcontext *ctx )
1347 {
1348 GLint newsize;
1349 GLuint buf_width, buf_height;
1350
1351 /* ask device driver for size of output buffer */
1352 (*ctx->Driver.GetBufferSize)( ctx, &buf_width, &buf_height );
1353
1354 /* see if size of device driver's color buffer (window) has changed */
1355 newsize = ctx->Buffer->Width!=buf_width || ctx->Buffer->Height!=buf_height;
1356
1357 /* save buffer size */
1358 ctx->Buffer->Width = buf_width;
1359 ctx->Buffer->Height = buf_height;
1360
1361 /* Reallocate other buffers if needed. */
1362 if (newsize && ctx->Visual->DepthBits>0) {
1363 /* reallocate depth buffer */
1364 (*ctx->Driver.AllocDepthBuffer)( ctx );
1365 (*ctx->Driver.ClearDepthBuffer)( ctx );
1366 }
1367 if (newsize && ctx->Visual->StencilBits>0) {
1368 /* reallocate stencil buffer */
1369 gl_alloc_stencil_buffer( ctx );
1370 }
1371 if (newsize && ctx->Visual->AccumBits>0) {
1372 /* reallocate accum buffer */
1373 gl_alloc_accum_buffer( ctx );
1374 }
1375 if (newsize
1376 && (ctx->Visual->FrontAlphaEnabled || ctx->Visual->BackAlphaEnabled)) {
1377 gl_alloc_alpha_buffers( ctx );
1378 }
1379 }
1380
1381
1382
1383
1384 /**********************************************************************/
1385 /***** State update logic *****/
1386 /**********************************************************************/
1387
1388
1389 /*
1390 * Since the device driver may or may not support pixel logic ops we
1391 * have to make some extensive tests to determine whether or not
1392 * software-implemented logic operations have to be used.
1393 */
1394 static void update_pixel_logic( GLcontext *ctx )
1395 {
1396 if (ctx->Visual->RGBAflag) {
1397 /* RGBA mode blending w/ Logic Op */
1398 if (ctx->Color.ColorLogicOpEnabled) {
1399 if (ctx->Driver.LogicOp
1400 && (*ctx->Driver.LogicOp)( ctx, ctx->Color.LogicOp )) {
1401 /* Device driver can do logic, don't have to do it in software */
1402 ctx->Color.SWLogicOpEnabled = GL_FALSE;
1403 }
1404 else {
1405 /* Device driver can't do logic op so we do it in software */
1406 ctx->Color.SWLogicOpEnabled = GL_TRUE;
1407 }
1408 }
1409 else {
1410 /* no logic op */
1411 if (ctx->Driver.LogicOp) {
1412 (void) (*ctx->Driver.LogicOp)( ctx, GL_COPY );
1413 }
1414 ctx->Color.SWLogicOpEnabled = GL_FALSE;
1415 }
1416 }
1417 else {
1418 /* CI mode Logic Op */
1419 if (ctx->Color.IndexLogicOpEnabled) {
1420 if (ctx->Driver.LogicOp
1421 && (*ctx->Driver.LogicOp)( ctx, ctx->Color.LogicOp )) {
1422 /* Device driver can do logic, don't have to do it in software */
1423 ctx->Color.SWLogicOpEnabled = GL_FALSE;
1424 }
1425 else {
1426 /* Device driver can't do logic op so we do it in software */
1427 ctx->Color.SWLogicOpEnabled = GL_TRUE;
1428 }
1429 }
1430 else {
1431 /* no logic op */
1432 if (ctx->Driver.LogicOp) {
1433 (void) (*ctx->Driver.LogicOp)( ctx, GL_COPY );
1434 }
1435 ctx->Color.SWLogicOpEnabled = GL_FALSE;
1436 }
1437 }
1438 }
1439
1440
1441
1442 /*
1443 * Check if software implemented RGBA or Color Index masking is needed.
1444 */
1445 static void update_pixel_masking( GLcontext *ctx )
1446 {
1447 if (ctx->Visual->RGBAflag) {
1448 if (ctx->Color.ColorMask==0xf) {
1449 /* disable masking */
1450 if (ctx->Driver.ColorMask) {
1451 (void) (*ctx->Driver.ColorMask)( ctx, GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
1452 }
1453 ctx->Color.SWmasking = GL_FALSE;
1454 }
1455 else {
1456 /* Ask driver to do color masking, if it can't then
1457 * do it in software
1458 */
1459 GLboolean red = (ctx->Color.ColorMask & 8) ? GL_TRUE : GL_FALSE;
1460 GLboolean green = (ctx->Color.ColorMask & 4) ? GL_TRUE : GL_FALSE;
1461 GLboolean blue = (ctx->Color.ColorMask & 2) ? GL_TRUE : GL_FALSE;
1462 GLboolean alpha = (ctx->Color.ColorMask & 1) ? GL_TRUE : GL_FALSE;
1463 if (ctx->Driver.ColorMask
1464 && (*ctx->Driver.ColorMask)( ctx, red, green, blue, alpha )) {
1465 ctx->Color.SWmasking = GL_FALSE;
1466 }
1467 else {
1468 ctx->Color.SWmasking = GL_TRUE;
1469 }
1470 }
1471 }
1472 else {
1473 if (ctx->Color.IndexMask==0xffffffff) {
1474 /* disable masking */
1475 if (ctx->Driver.IndexMask) {
1476 (void) (*ctx->Driver.IndexMask)( ctx, 0xffffffff );
1477 }
1478 ctx->Color.SWmasking = GL_FALSE;
1479 }
1480 else {
1481 /* Ask driver to do index masking, if it can't then
1482 * do it in software
1483 */
1484 if (ctx->Driver.IndexMask
1485 && (*ctx->Driver.IndexMask)( ctx, ctx->Color.IndexMask )) {
1486 ctx->Color.SWmasking = GL_FALSE;
1487 }
1488 else {
1489 ctx->Color.SWmasking = GL_TRUE;
1490 }
1491 }
1492 }
1493 }
1494
1495
1496
1497 /*
1498 * Recompute the value of ctx->RasterMask, ctx->ClipMask, etc. according to
1499 * the current context.
1500 */
1501 static void update_rasterflags( GLcontext *ctx )
1502 {
1503 ctx->RasterMask = 0;
1504
1505 if (ctx->Color.AlphaEnabled) ctx->RasterMask |= ALPHATEST_BIT;
1506 if (ctx->Color.BlendEnabled) ctx->RasterMask |= BLEND_BIT;
1507 if (ctx->Depth.Test) ctx->RasterMask |= DEPTH_BIT;
1508 if (ctx->Fog.Enabled) ctx->RasterMask |= FOG_BIT;
1509 if (ctx->Color.SWLogicOpEnabled) ctx->RasterMask |= LOGIC_OP_BIT;
1510 if (ctx->Scissor.Enabled) ctx->RasterMask |= SCISSOR_BIT;
1511 if (ctx->Stencil.Enabled) ctx->RasterMask |= STENCIL_BIT;
1512 if (ctx->Color.SWmasking) ctx->RasterMask |= MASKING_BIT;
1513 if (ctx->Visual->FrontAlphaEnabled) ctx->RasterMask |= ALPHABUF_BIT;
1514 if (ctx->Visual->BackAlphaEnabled) ctx->RasterMask |= ALPHABUF_BIT;
1515
1516 if ( ctx->Viewport.X<0
1517 || ctx->Viewport.X + ctx->Viewport.Width > ctx->Buffer->Width
1518 || ctx->Viewport.Y<0
1519 || ctx->Viewport.Y + ctx->Viewport.Height > ctx->Buffer->Height) {
1520 ctx->RasterMask |= WINCLIP_BIT;
1521 }
1522
1523 /* check if drawing to front and back buffers */
1524 if (ctx->Color.DrawBuffer==GL_FRONT_AND_BACK) {
1525 ctx->RasterMask |= FRONT_AND_BACK_BIT;
1526 }
1527
1528 /* check if writing to color buffer(s) is disabled */
1529 if (ctx->Color.DrawBuffer==GL_NONE) {
1530 ctx->RasterMask |= NO_DRAW_BIT;
1531 }
1532 else if (ctx->Visual->RGBAflag && ctx->Color.ColorMask==0) {
1533 ctx->RasterMask |= NO_DRAW_BIT;
1534 }
1535 else if (!ctx->Visual->RGBAflag && ctx->Color.IndexMask==0) {
1536 ctx->RasterMask |= NO_DRAW_BIT;
1537 }
1538
1539 /* Recompute ClipMask (what has to be interpolated when clipping) */
1540 ctx->ClipMask = 0;
1541 if (ctx->Texture.Enabled) {
1542 ctx->ClipMask |= CLIP_TEXTURE_BIT;
1543 }
1544 if (ctx->Light.ShadeModel==GL_SMOOTH) {
1545 if (ctx->Visual->RGBAflag) {
1546 ctx->ClipMask |= CLIP_FCOLOR_BIT;
1547 if (ctx->Light.Model.TwoSide) {
1548 ctx->ClipMask |= CLIP_BCOLOR_BIT;
1549 }
1550 }
1551 else {
1552 ctx->ClipMask |= CLIP_FINDEX_BIT;
1553 if (ctx->Light.Model.TwoSide) {
1554 ctx->ClipMask |= CLIP_BINDEX_BIT;
1555 }
1556 }
1557 }
1558 }
1559
1560
1561
1562 /*
1563 * If ctx->NewState is non-zero then this function MUST be called before
1564 * rendering any primitive. Basically, function pointers and miscellaneous
1565 * flags are updated to reflect the current state of the state machine.
1566 */
1567 void gl_update_state( GLcontext *ctx )
1568 {
1569 if (ctx->NewState & NEW_RASTER_OPS) {
1570 update_pixel_logic(ctx);
1571 update_pixel_masking(ctx);
1572 update_rasterflags(ctx);
1573 }
1574
1575 if (ctx->NewState & NEW_LIGHTING) {
1576 gl_update_lighting(ctx);
1577 if (ctx->Light.ColorMaterialEnabled) {
1578 ctx->Exec.Color4f = gl_ColorMat4f;
1579 ctx->Exec.Color4ub = gl_ColorMat4ub;
1580 }
1581 else {
1582 ctx->Exec.Color4f = gl_Color4f;
1583 if (ctx->Visual->EightBitColor) {
1584 ctx->Exec.Color4ub = gl_Color4ub8bit;
1585 }
1586 else {
1587 ctx->Exec.Color4ub = gl_Color4ub;
1588 }
1589 }
1590 if (!ctx->CompileFlag) {
1591 ctx->API.Color4f = ctx->Exec.Color4f;
1592 ctx->API.Color4ub = ctx->Exec.Color4ub;
1593 }
1594 }
1595
1596 if (ctx->NewState & NEW_TEXTURING) {
1597 gl_update_texture_state(ctx);
1598 }
1599
1600 if (ctx->NewState & (NEW_LIGHTING | NEW_TEXTURING)) {
1601 /* Check if normal vectors are needed */
1602 if (ctx->Light.Enabled
1603 || (ctx->Texture.GenModeS==GL_SPHERE_MAP
1604 && (ctx->Texture.TexGenEnabled & S_BIT))
1605 || (ctx->Texture.GenModeT==GL_SPHERE_MAP
1606 && (ctx->Texture.TexGenEnabled & T_BIT))) {
1607 ctx->NeedNormals = GL_TRUE;
1608 }
1609 else {
1610 ctx->NeedNormals = GL_FALSE;
1611 }
1612 }
1613
1614 if (ctx->NewState & NEW_RASTER_OPS) {
1615 /* Check if incoming colors can be modified during rasterization */
1616 if (ctx->Fog.Enabled ||
1617 ctx->Texture.Enabled ||
1618 ctx->Color.BlendEnabled ||
1619 ctx->Color.SWmasking ||
1620 ctx->Color.SWLogicOpEnabled) {
1621 ctx->MutablePixels = GL_TRUE;
1622 }
1623 else {
1624 ctx->MutablePixels = GL_FALSE;
1625 }
1626 }
1627
1628 if (ctx->NewState & (NEW_RASTER_OPS | NEW_LIGHTING)) {
1629 /* Check if all pixels generated are likely to be the same color */
1630 if (ctx->Light.ShadeModel==GL_SMOOTH ||
1631 ctx->Light.Enabled ||
1632 ctx->Fog.Enabled ||
1633 ctx->Texture.Enabled ||
1634 ctx->Color.BlendEnabled ||
1635 ctx->Color.SWmasking ||
1636 ctx->Color.SWLogicOpEnabled) {
1637 ctx->MonoPixels = GL_FALSE; /* pixels probably multicolored */
1638 }
1639 else {
1640 /* pixels will all be same color,
1641 * only glColor() can invalidate this.
1642 */
1643 ctx->MonoPixels = GL_TRUE;
1644 }
1645 }
1646
1647 if (ctx->NewState & NEW_RASTER_OPS) {
1648 /* Setup CullBits bitmask */
1649 ctx->Polygon.CullBits = 0;
1650 if (ctx->Polygon.CullFlag) {
1651 if (ctx->Polygon.CullFaceMode==GL_FRONT ||
1652 ctx->Polygon.CullFaceMode==GL_FRONT_AND_BACK) {
1653 ctx->Polygon.CullBits |= 1;
1654 }
1655 if (ctx->Polygon.CullFaceMode==GL_BACK ||
1656 ctx->Polygon.CullFaceMode==GL_FRONT_AND_BACK) {
1657 ctx->Polygon.CullBits |= 2;
1658 }
1659 }
1660 /* Any Polygon offsets enabled? */
1661 ctx->Polygon.OffsetAny = ctx->Polygon.OffsetPoint ||
1662 ctx->Polygon.OffsetLine ||
1663 ctx->Polygon.OffsetFill;
1664 /* reset Z offsets now */
1665 ctx->PointZoffset = 0.0;
1666 ctx->LineZoffset = 0.0;
1667 ctx->PolygonZoffset = 0.0;
1668 }
1669
1670 if (ctx->NewState & (NEW_RASTER_OPS | NEW_LIGHTING)) {
1671 /* Determine if we can directly call the triangle rasterizer */
1672 if ( ctx->Polygon.Unfilled
1673 || ctx->Polygon.OffsetAny
1674 || ctx->Polygon.CullFlag
1675 || ctx->Light.Model.TwoSide
1676 || ctx->RenderMode!=GL_RENDER) {
1677 ctx->DirectTriangles = GL_FALSE;
1678 }
1679 else {
1680 ctx->DirectTriangles = GL_TRUE;
1681 }
1682 }
1683
1684 /* update scissor region */
1685 ctx->Buffer->Xmin = 0;
1686 ctx->Buffer->Ymin = 0;
1687 ctx->Buffer->Xmax = ctx->Buffer->Width-1;
1688 ctx->Buffer->Ymax = ctx->Buffer->Height-1;
1689 if (ctx->Scissor.Enabled) {
1690 if (ctx->Scissor.X > ctx->Buffer->Xmin) {
1691 ctx->Buffer->Xmin = ctx->Scissor.X;
1692 }
1693 if (ctx->Scissor.Y > ctx->Buffer->Ymin) {
1694 ctx->Buffer->Ymin = ctx->Scissor.Y;
1695 }
1696 if (ctx->Scissor.X + ctx->Scissor.Width - 1 < ctx->Buffer->Xmax) {
1697 ctx->Buffer->Xmax = ctx->Scissor.X + ctx->Scissor.Width - 1;
1698 }
1699 if (ctx->Scissor.Y + ctx->Scissor.Height - 1 < ctx->Buffer->Ymax) {
1700 ctx->Buffer->Ymax = ctx->Scissor.Y + ctx->Scissor.Height - 1;
1701 }
1702 }
1703
1704 /*
1705 * Update Device Driver interface
1706 */
1707 if (ctx->NewState & NEW_RASTER_OPS) {
1708 ctx->Driver.AllocDepthBuffer = gl_alloc_depth_buffer;
1709 ctx->Driver.ClearDepthBuffer = gl_clear_depth_buffer;
1710 if (ctx->Depth.Mask) {
1711 switch (ctx->Depth.Func) {
1712 case GL_LESS:
1713 ctx->Driver.DepthTestSpan = gl_depth_test_span_less;
1714 ctx->Driver.DepthTestPixels = gl_depth_test_pixels_less;
1715 break;
1716 case GL_GREATER:
1717 ctx->Driver.DepthTestSpan = gl_depth_test_span_greater;
1718 ctx->Driver.DepthTestPixels = gl_depth_test_pixels_greater;
1719 break;
1720 default:
1721 ctx->Driver.DepthTestSpan = gl_depth_test_span_generic;
1722 ctx->Driver.DepthTestPixels = gl_depth_test_pixels_generic;
1723 }
1724 }
1725 else {
1726 ctx->Driver.DepthTestSpan = gl_depth_test_span_generic;
1727 ctx->Driver.DepthTestPixels = gl_depth_test_pixels_generic;
1728 }
1729 ctx->Driver.ReadDepthSpanFloat = gl_read_depth_span_float;
1730 ctx->Driver.ReadDepthSpanInt = gl_read_depth_span_int;
1731 }
1732 if (ctx->Driver.UpdateState) {
1733 (*ctx->Driver.UpdateState)(ctx);
1734 }
1735
1736 gl_set_point_function(ctx);
1737 gl_set_line_function(ctx);
1738 gl_set_triangle_function(ctx);
1739
1740 gl_set_vertex_function(ctx);
1741
1742 ctx->NewState = 0;
1743 }
1744