Fix several conformance problems. Hack solution to line stipple problem.
[mesa.git] / src / mesa / drivers / glide / fxdd.c
1
2 /*
3 * Mesa 3-D graphics library
4 * Version: 3.3
5 *
6 * Copyright (C) 1999-2000 Brian Paul All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 *
26 * Original Mesa / 3Dfx device driver (C) 1999 David Bucciarelli, by the
27 * terms stated above.
28 *
29 * Thank you for your contribution, David!
30 *
31 * Please make note of the above copyright/license statement. If you
32 * contributed code or bug fixes to this code under the previous (GNU
33 * Library) license and object to the new license, your code will be
34 * removed at your request. Please see the Mesa docs/COPYRIGHT file
35 * for more information.
36 *
37 * Additional Mesa/3Dfx driver developers:
38 * Daryll Strauss <daryll@precisioninsight.com>
39 * Keith Whitwell <keith@precisioninsight.com>
40 *
41 * See fxapi.h for more revision/author details.
42 */
43
44
45 /* fxdd.c - 3Dfx VooDoo Mesa device driver functions */
46
47
48 #ifdef HAVE_CONFIG_H
49 #include "conf.h"
50 #endif
51
52 #if defined(FX)
53
54 #include "image.h"
55 #include "mtypes.h"
56 #include "fxdrv.h"
57 #include "enums.h"
58 #include "extensions.h"
59 #include "swrast/swrast.h"
60 #include "swrast_setup/swrast_setup.h"
61 #include "tnl/tnl.h"
62 #include "array_cache/acache.h"
63
64 #include "tnl/t_pipeline.h"
65
66
67 float gl_ubyte_to_float_255_color_tab[256];
68
69 /* These lookup table are used to extract RGB values in [0,255] from
70 * 16-bit pixel values.
71 */
72 GLubyte FX_PixelToR[0x10000];
73 GLubyte FX_PixelToG[0x10000];
74 GLubyte FX_PixelToB[0x10000];
75
76
77 /*
78 * Initialize the FX_PixelTo{RGB} arrays.
79 * Input: bgrOrder - if TRUE, pixels are in BGR order, else RGB order.
80 */
81 void fxInitPixelTables(fxMesaContext fxMesa, GLboolean bgrOrder)
82 {
83 GLuint pixel;
84
85 fxMesa->bgrOrder=bgrOrder;
86 for (pixel = 0; pixel <= 0xffff; pixel++) {
87 GLuint r, g, b;
88 if (bgrOrder) {
89 r = (pixel & 0x001F) << 3;
90 g = (pixel & 0x07E0) >> 3;
91 b = (pixel & 0xF800) >> 8;
92 }
93 else {
94 r = (pixel & 0xF800) >> 8;
95 g = (pixel & 0x07E0) >> 3;
96 b = (pixel & 0x001F) << 3;
97 }
98 r = r * 255 / 0xF8; /* fill in low-order bits */
99 g = g * 255 / 0xFC;
100 b = b * 255 / 0xF8;
101 FX_PixelToR[pixel] = r;
102 FX_PixelToG[pixel] = g;
103 FX_PixelToB[pixel] = b;
104 }
105 }
106
107
108 /**********************************************************************/
109 /***** Miscellaneous functions *****/
110 /**********************************************************************/
111
112 /* Return buffer size information */
113 static void fxDDBufferSize(GLcontext *ctx, GLuint *width, GLuint *height)
114 {
115 fxMesaContext fxMesa=(fxMesaContext)ctx->DriverCtx;
116
117 if (MESA_VERBOSE&VERBOSE_DRIVER) {
118 fprintf(stderr,"fxmesa: fxDDBufferSize(...) Start\n");
119 }
120
121 *width=fxMesa->width;
122 *height=fxMesa->height;
123
124 if (MESA_VERBOSE&VERBOSE_DRIVER) {
125 fprintf(stderr,"fxmesa: fxDDBufferSize(...) End\n");
126 }
127 }
128
129
130 /* Implements glClearColor() */
131 static void fxDDClearColor(GLcontext *ctx, GLubyte red, GLubyte green,
132 GLubyte blue, GLubyte alpha )
133 {
134 fxMesaContext fxMesa=(fxMesaContext)ctx->DriverCtx;
135 GLubyte col[4];
136
137
138
139 ASSIGN_4V( col, red, green, blue, 255 );
140
141 if (MESA_VERBOSE&VERBOSE_DRIVER) {
142 fprintf(stderr,"fxmesa: fxDDClearColor(%d,%d,%d,%d)\n",red,green,blue,alpha);
143 }
144
145 fxMesa->clearC=FXCOLOR4( col );
146 fxMesa->clearA=alpha;
147 }
148
149
150 /* Clear the color and/or depth buffers */
151 static GLbitfield fxDDClear(GLcontext *ctx, GLbitfield mask, GLboolean all,
152 GLint x, GLint y, GLint width, GLint height )
153 {
154 fxMesaContext fxMesa=(fxMesaContext)ctx->DriverCtx;
155 const GLuint colorMask = *((GLuint *) &ctx->Color.ColorMask);
156 const FxU16 clearD = (FxU16) (ctx->Depth.Clear * 0xffff);
157 GLbitfield softwareMask = mask & (DD_STENCIL_BIT | DD_ACCUM_BIT);
158
159 /* we can't clear stencil or accum buffers */
160 mask &= ~(DD_STENCIL_BIT | DD_ACCUM_BIT);
161
162 if (MESA_VERBOSE & VERBOSE_DRIVER) {
163 fprintf(stderr,"fxmesa: fxDDClear(%d,%d,%d,%d)\n", (int) x, (int) y,
164 (int) width, (int) height);
165 }
166
167 if (colorMask != 0xffffffff) {
168 /* do masked color buffer clears in software */
169 softwareMask |= (mask & (DD_FRONT_LEFT_BIT | DD_BACK_LEFT_BIT));
170 mask &= ~(DD_FRONT_LEFT_BIT | DD_BACK_LEFT_BIT);
171 }
172
173 /*
174 * This could probably be done fancier but doing each possible case
175 * explicitly is less error prone.
176 */
177 switch (mask) {
178 case DD_BACK_LEFT_BIT | DD_DEPTH_BIT:
179 /* back buffer & depth */
180 FX_grDepthMask(FXTRUE);
181 FX_grRenderBuffer(GR_BUFFER_BACKBUFFER);
182 FX_grBufferClear(fxMesa->clearC, fxMesa->clearA, clearD);
183 if (!ctx->Depth.Mask) {
184 FX_grDepthMask(FXFALSE);
185 }
186 break;
187 case DD_FRONT_LEFT_BIT | DD_DEPTH_BIT:
188 /* XXX it appears that the depth buffer isn't cleared when
189 * glRenderBuffer(GR_BUFFER_FRONTBUFFER) is set.
190 * This is a work-around/
191 */
192 /* clear depth */
193 FX_grDepthMask(FXTRUE);
194 FX_grRenderBuffer(GR_BUFFER_BACKBUFFER);
195 FX_grColorMask(FXFALSE,FXFALSE);
196 FX_grBufferClear(fxMesa->clearC, fxMesa->clearA, clearD);
197 /* clear front */
198 FX_grColorMask(FXTRUE, ctx->Color.ColorMask[ACOMP] && fxMesa->haveAlphaBuffer);
199 FX_grRenderBuffer(GR_BUFFER_FRONTBUFFER);
200 FX_grBufferClear(fxMesa->clearC, fxMesa->clearA, clearD);
201 break;
202 case DD_BACK_LEFT_BIT:
203 /* back buffer only */
204 FX_grDepthMask(FXFALSE);
205 FX_grRenderBuffer(GR_BUFFER_BACKBUFFER);
206 FX_grBufferClear(fxMesa->clearC, fxMesa->clearA, clearD);
207 if (ctx->Depth.Mask) {
208 FX_grDepthMask(FXTRUE);
209 }
210 break;
211 case DD_FRONT_LEFT_BIT:
212 /* front buffer only */
213 FX_grDepthMask(FXFALSE);
214 FX_grRenderBuffer(GR_BUFFER_FRONTBUFFER);
215 FX_grBufferClear(fxMesa->clearC, fxMesa->clearA, clearD);
216 if (ctx->Depth.Mask) {
217 FX_grDepthMask(FXTRUE);
218 }
219 break;
220 case DD_FRONT_LEFT_BIT | DD_BACK_LEFT_BIT:
221 /* front and back */
222 FX_grDepthMask(FXFALSE);
223 FX_grRenderBuffer(GR_BUFFER_BACKBUFFER);
224 FX_grBufferClear(fxMesa->clearC, fxMesa->clearA, clearD);
225 FX_grRenderBuffer(GR_BUFFER_FRONTBUFFER);
226 FX_grBufferClear(fxMesa->clearC, fxMesa->clearA, clearD);
227 if (ctx->Depth.Mask) {
228 FX_grDepthMask(FXTRUE);
229 }
230 break;
231 case DD_FRONT_LEFT_BIT | DD_BACK_LEFT_BIT | DD_DEPTH_BIT:
232 /* clear front */
233 FX_grDepthMask(FXFALSE);
234 FX_grRenderBuffer(GR_BUFFER_FRONTBUFFER);
235 FX_grBufferClear(fxMesa->clearC, fxMesa->clearA, clearD);
236 /* clear back and depth */
237 FX_grDepthMask(FXTRUE);
238 FX_grRenderBuffer(GR_BUFFER_BACKBUFFER);
239 FX_grBufferClear(fxMesa->clearC, fxMesa->clearA, clearD);
240 if (!ctx->Depth.Mask) {
241 FX_grDepthMask(FXFALSE);
242 }
243 break;
244 case DD_DEPTH_BIT:
245 /* just the depth buffer */
246 FX_grRenderBuffer(GR_BUFFER_BACKBUFFER);
247 FX_grColorMask(FXFALSE,FXFALSE);
248 FX_grDepthMask(FXTRUE);
249 FX_grBufferClear(fxMesa->clearC, fxMesa->clearA, clearD);
250 FX_grColorMask(FXTRUE, ctx->Color.ColorMask[ACOMP] && fxMesa->haveAlphaBuffer);
251 if (ctx->Color.DrawDestMask & FRONT_LEFT_BIT)
252 FX_grRenderBuffer(GR_BUFFER_FRONTBUFFER);
253 if (!ctx->Depth.Test || !ctx->Depth.Mask)
254 FX_grDepthMask(FXFALSE);
255 break;
256 default:
257 /* error */
258 ;
259 }
260
261 return softwareMask;
262 }
263
264
265 /* Set the buffer used for drawing */
266 /* XXX support for separate read/draw buffers hasn't been tested */
267 static GLboolean fxDDSetDrawBuffer(GLcontext *ctx, GLenum mode)
268 {
269 fxMesaContext fxMesa=(fxMesaContext)ctx->DriverCtx;
270
271 if (MESA_VERBOSE&VERBOSE_DRIVER) {
272 fprintf(stderr,"fxmesa: fxDDSetBuffer(%x)\n", (int) mode);
273 }
274
275 if (mode == GL_FRONT_LEFT) {
276 fxMesa->currentFB = GR_BUFFER_FRONTBUFFER;
277 FX_grRenderBuffer(fxMesa->currentFB);
278 return GL_TRUE;
279 }
280 else if (mode == GL_BACK_LEFT) {
281 fxMesa->currentFB = GR_BUFFER_BACKBUFFER;
282 FX_grRenderBuffer(fxMesa->currentFB);
283 return GL_TRUE;
284 }
285 else if (mode == GL_NONE) {
286 FX_grColorMask(FXFALSE,FXFALSE);
287 return GL_TRUE;
288 }
289 else {
290 return GL_FALSE;
291 }
292 }
293
294
295 /* Set the buffer used for reading */
296 /* XXX support for separate read/draw buffers hasn't been tested */
297 static void fxDDSetReadBuffer(GLcontext *ctx, GLframebuffer *buffer,
298 GLenum mode )
299 {
300 fxMesaContext fxMesa=(fxMesaContext)ctx->DriverCtx;
301 (void) buffer;
302
303 if (MESA_VERBOSE&VERBOSE_DRIVER) {
304 fprintf(stderr,"fxmesa: fxDDSetBuffer(%x)\n", (int) mode);
305 }
306
307 if (mode == GL_FRONT_LEFT) {
308 fxMesa->currentFB = GR_BUFFER_FRONTBUFFER;
309 FX_grRenderBuffer(fxMesa->currentFB);
310 }
311 else if (mode == GL_BACK_LEFT) {
312 fxMesa->currentFB = GR_BUFFER_BACKBUFFER;
313 FX_grRenderBuffer(fxMesa->currentFB);
314 }
315 }
316
317
318
319 static GLboolean fxDDDrawBitmap(GLcontext *ctx, GLint px, GLint py,
320 GLsizei width, GLsizei height,
321 const struct gl_pixelstore_attrib *unpack,
322 const GLubyte *bitmap)
323 {
324 fxMesaContext fxMesa=(fxMesaContext)ctx->DriverCtx;
325 GrLfbInfo_t info;
326 FxU16 color;
327 const struct gl_pixelstore_attrib *finalUnpack;
328 struct gl_pixelstore_attrib scissoredUnpack;
329
330 /* check if there's any raster operations enabled which we can't handle */
331 if (ctx->Color.AlphaEnabled ||
332 ctx->Color.BlendEnabled ||
333 ctx->Depth.Test ||
334 ctx->Fog.Enabled ||
335 ctx->Color.ColorLogicOpEnabled ||
336 ctx->Stencil.Enabled ||
337 ctx->Scissor.Enabled ||
338 ( ctx->DrawBuffer->UseSoftwareAlphaBuffers &&
339 ctx->Color.ColorMask[ACOMP]) ||
340 ctx->Color.MultiDrawBuffer)
341 return GL_FALSE;
342
343
344 if (ctx->Scissor.Enabled) {
345 /* This is a bit tricky, but by carefully adjusting the px, py,
346 * width, height, skipPixels and skipRows values we can do
347 * scissoring without special code in the rendering loop.
348 *
349 * KW: This code is never reached, see the test above.
350 */
351
352 /* we'll construct a new pixelstore struct */
353 finalUnpack = &scissoredUnpack;
354 scissoredUnpack = *unpack;
355 if (scissoredUnpack.RowLength == 0)
356 scissoredUnpack.RowLength = width;
357
358 /* clip left */
359 if (px < ctx->Scissor.X) {
360 scissoredUnpack.SkipPixels += (ctx->Scissor.X - px);
361 width -= (ctx->Scissor.X - px);
362 px = ctx->Scissor.X;
363 }
364 /* clip right */
365 if (px + width >= ctx->Scissor.X + ctx->Scissor.Width) {
366 width -= (px + width - (ctx->Scissor.X + ctx->Scissor.Width));
367 }
368 /* clip bottom */
369 if (py < ctx->Scissor.Y) {
370 scissoredUnpack.SkipRows += (ctx->Scissor.Y - py);
371 height -= (ctx->Scissor.Y - py);
372 py = ctx->Scissor.Y;
373 }
374 /* clip top */
375 if (py + height >= ctx->Scissor.Y + ctx->Scissor.Height) {
376 height -= (py + height - (ctx->Scissor.Y + ctx->Scissor.Height));
377 }
378
379 if (width <= 0 || height <= 0)
380 return GL_TRUE; /* totally scissored away */
381 }
382 else {
383 finalUnpack = unpack;
384 }
385
386 /* compute pixel value */
387 {
388 GLint r = (GLint) (ctx->Current.RasterColor[0] * 255.0f);
389 GLint g = (GLint) (ctx->Current.RasterColor[1] * 255.0f);
390 GLint b = (GLint) (ctx->Current.RasterColor[2] * 255.0f);
391 /*GLint a = (GLint)(ctx->Current.RasterColor[3]*255.0f);*/
392 if (fxMesa->bgrOrder)
393 color = (FxU16)
394 ( ((FxU16)0xf8 & b) << (11-3)) |
395 ( ((FxU16)0xfc & g) << (5-3+1)) |
396 ( ((FxU16)0xf8 & r) >> 3);
397 else
398 color = (FxU16)
399 ( ((FxU16)0xf8 & r) << (11-3)) |
400 ( ((FxU16)0xfc & g) << (5-3+1)) |
401 ( ((FxU16)0xf8 & b) >> 3);
402 }
403
404 info.size = sizeof(info);
405 if (!FX_grLfbLock(GR_LFB_WRITE_ONLY,
406 fxMesa->currentFB,
407 GR_LFBWRITEMODE_565,
408 GR_ORIGIN_UPPER_LEFT,
409 FXFALSE,
410 &info)) {
411 #ifndef FX_SILENT
412 fprintf(stderr,"fx Driver: error locking the linear frame buffer\n");
413 #endif
414 return GL_TRUE;
415 }
416
417 {
418 const GLint winX = 0;
419 const GLint winY = fxMesa->height - 1;
420 /* The dest stride depends on the hardware and whether we're drawing
421 * to the front or back buffer. This compile-time test seems to do
422 * the job for now.
423 */
424 const GLint dstStride = info.strideInBytes / 2; /* stride in GLushorts */
425
426 GLint row;
427 /* compute dest address of bottom-left pixel in bitmap */
428 GLushort *dst = (GLushort *) info.lfbPtr
429 + (winY - py) * dstStride
430 + (winX + px);
431
432 for (row = 0; row < height; row++) {
433 const GLubyte *src = (const GLubyte *) _mesa_image_address( finalUnpack,
434 bitmap, width, height, GL_COLOR_INDEX, GL_BITMAP, 0, row, 0 );
435 if (finalUnpack->LsbFirst) {
436 /* least significan bit first */
437 GLubyte mask = 1U << (finalUnpack->SkipPixels & 0x7);
438 GLint col;
439 for (col=0; col<width; col++) {
440 if (*src & mask) {
441 dst[col] = color;
442 }
443 if (mask == 128U) {
444 src++;
445 mask = 1U;
446 }
447 else {
448 mask = mask << 1;
449 }
450 }
451 if (mask != 1)
452 src++;
453 }
454 else {
455 /* most significan bit first */
456 GLubyte mask = 128U >> (finalUnpack->SkipPixels & 0x7);
457 GLint col;
458 for (col=0; col<width; col++) {
459 if (*src & mask) {
460 dst[col] = color;
461 }
462 if (mask == 1U) {
463 src++;
464 mask = 128U;
465 }
466 else {
467 mask = mask >> 1;
468 }
469 }
470 if (mask != 128)
471 src++;
472 }
473 dst -= dstStride;
474 }
475 }
476
477 FX_grLfbUnlock(GR_LFB_WRITE_ONLY,fxMesa->currentFB);
478 return GL_TRUE;
479 }
480
481
482 static GLboolean fxDDReadPixels( GLcontext *ctx, GLint x, GLint y,
483 GLsizei width, GLsizei height,
484 GLenum format, GLenum type,
485 const struct gl_pixelstore_attrib *packing,
486 GLvoid *dstImage )
487 {
488 if (ctx->_ImageTransferState) {
489 return GL_FALSE; /* can't do this */
490 }
491 else {
492 fxMesaContext fxMesa=(fxMesaContext)ctx->DriverCtx;
493 GrLfbInfo_t info;
494 GLboolean result = GL_FALSE;
495
496 BEGIN_BOARD_LOCK();
497 if (grLfbLock(GR_LFB_READ_ONLY,
498 fxMesa->currentFB,
499 GR_LFBWRITEMODE_ANY,
500 GR_ORIGIN_UPPER_LEFT,
501 FXFALSE,
502 &info)) {
503 const GLint winX = 0;
504 const GLint winY = fxMesa->height - 1;
505 const GLint srcStride = info.strideInBytes / 2; /* stride in GLushorts */
506 const GLushort *src = (const GLushort *) info.lfbPtr
507 + (winY - y) * srcStride + (winX + x);
508 GLubyte *dst = (GLubyte *) _mesa_image_address(packing, dstImage,
509 width, height, format, type, 0, 0, 0);
510 GLint dstStride = _mesa_image_row_stride(packing, width, format, type);
511
512 if (format == GL_RGB && type == GL_UNSIGNED_BYTE) {
513 /* convert 5R6G5B into 8R8G8B */
514 GLint row, col;
515 const GLint halfWidth = width >> 1;
516 const GLint extraPixel = (width & 1);
517 for (row = 0; row < height; row++) {
518 GLubyte *d = dst;
519 for (col = 0; col < halfWidth; col++) {
520 const GLuint pixel = ((const GLuint *) src)[col];
521 const GLint pixel0 = pixel & 0xffff;
522 const GLint pixel1 = pixel >> 16;
523 *d++ = FX_PixelToR[pixel0];
524 *d++ = FX_PixelToG[pixel0];
525 *d++ = FX_PixelToB[pixel0];
526 *d++ = FX_PixelToR[pixel1];
527 *d++ = FX_PixelToG[pixel1];
528 *d++ = FX_PixelToB[pixel1];
529 }
530 if (extraPixel) {
531 GLushort pixel = src[width-1];
532 *d++ = FX_PixelToR[pixel];
533 *d++ = FX_PixelToG[pixel];
534 *d++ = FX_PixelToB[pixel];
535 }
536 dst += dstStride;
537 src -= srcStride;
538 }
539 result = GL_TRUE;
540 }
541 else if (format == GL_RGBA && type == GL_UNSIGNED_BYTE) {
542 /* convert 5R6G5B into 8R8G8B8A */
543 GLint row, col;
544 const GLint halfWidth = width >> 1;
545 const GLint extraPixel = (width & 1);
546 for (row = 0; row < height; row++) {
547 GLubyte *d = dst;
548 for (col = 0; col < halfWidth; col++) {
549 const GLuint pixel = ((const GLuint *) src)[col];
550 const GLint pixel0 = pixel & 0xffff;
551 const GLint pixel1 = pixel >> 16;
552 *d++ = FX_PixelToR[pixel0];
553 *d++ = FX_PixelToG[pixel0];
554 *d++ = FX_PixelToB[pixel0];
555 *d++ = 255;
556 *d++ = FX_PixelToR[pixel1];
557 *d++ = FX_PixelToG[pixel1];
558 *d++ = FX_PixelToB[pixel1];
559 *d++ = 255;
560 }
561 if (extraPixel) {
562 const GLushort pixel = src[width-1];
563 *d++ = FX_PixelToR[pixel];
564 *d++ = FX_PixelToG[pixel];
565 *d++ = FX_PixelToB[pixel];
566 *d++ = 255;
567 }
568 dst += dstStride;
569 src -= srcStride;
570 }
571 result = GL_TRUE;
572 }
573 else if (format == GL_RGB && type == GL_UNSIGNED_SHORT_5_6_5) {
574 /* directly memcpy 5R6G5B pixels into client's buffer */
575 const GLint widthInBytes = width * 2;
576 GLint row;
577 for (row = 0; row < height; row++) {
578 MEMCPY(dst, src, widthInBytes);
579 dst += dstStride;
580 src -= srcStride;
581 }
582 result = GL_TRUE;
583 }
584 else {
585 result = GL_FALSE;
586 }
587
588 grLfbUnlock(GR_LFB_READ_ONLY, fxMesa->currentFB);
589 }
590 END_BOARD_LOCK();
591 return result;
592 }
593 }
594
595
596
597 static void fxDDFinish(GLcontext *ctx)
598 {
599 FX_grFlush();
600 }
601
602
603
604
605
606 /* KW: Put the word Mesa in the render string because quakeworld
607 * checks for this rather than doing a glGet(GL_MAX_TEXTURE_SIZE).
608 * Why?
609 */
610 static const GLubyte *fxDDGetString(GLcontext *ctx, GLenum name)
611 {
612 switch (name) {
613 case GL_RENDERER:
614 {
615 static char buf[80];
616
617 if (glbHWConfig.SSTs[glbCurrentBoard].type==GR_SSTTYPE_VOODOO) {
618 GrVoodooConfig_t *vc =
619 &glbHWConfig.SSTs[glbCurrentBoard].sstBoard.VoodooConfig;
620
621 sprintf(buf,
622 "Mesa Glide v0.30 Voodoo_Graphics %d "
623 "CARD/%d FB/%d TM/%d TMU/%s",
624 glbCurrentBoard,
625 (vc->sliDetect ? (vc->fbRam*2) : vc->fbRam),
626 (vc->tmuConfig[GR_TMU0].tmuRam +
627 ((vc->nTexelfx>1) ? vc->tmuConfig[GR_TMU1].tmuRam : 0)),
628 vc->nTexelfx,
629 (vc->sliDetect ? "SLI" : "NOSLI"));
630 }
631 else if (glbHWConfig.SSTs[glbCurrentBoard].type==GR_SSTTYPE_SST96) {
632 GrSst96Config_t *sc =
633 &glbHWConfig.SSTs[glbCurrentBoard].sstBoard.SST96Config;
634
635 sprintf(buf,
636 "Glide v0.30 Voodoo_Rush %d "
637 "CARD/%d FB/%d TM/%d TMU/NOSLI",
638 glbCurrentBoard,
639 sc->fbRam,
640 sc->tmuConfig.tmuRam,
641 sc->nTexelfx);
642 }
643 else {
644 strcpy(buf, "Glide v0.30 UNKNOWN");
645 }
646 return (GLubyte *) buf;
647 }
648 default:
649 return NULL;
650 }
651 }
652
653 static const struct gl_pipeline_stage *fx_pipeline[] = {
654 &_tnl_update_material_stage, /* TODO: Add the fastpath here */
655 &_tnl_vertex_transform_stage,
656 &_tnl_normal_transform_stage,
657 &_tnl_lighting_stage,
658 &_tnl_fog_coordinate_stage, /* TODO: Omit fog stage */
659 &_tnl_texgen_stage,
660 &_tnl_texture_transform_stage,
661 &_tnl_point_attenuation_stage,
662 &_tnl_render_stage,
663 0,
664 };
665
666
667
668
669 int fxDDInitFxMesaContext( fxMesaContext fxMesa )
670 {
671 int i;
672 static int firsttime = 1;
673
674 for (i = 0 ; i < 256 ; i++) {
675 gl_ubyte_to_float_255_color_tab[i] = (float) i;
676 }
677
678 if (firsttime) {
679 fxDDSetupInit();
680 fxDDTrifuncInit();
681 firsttime = 0;
682 }
683
684 FX_setupGrVertexLayout();
685
686 if (getenv("FX_EMULATE_SINGLE_TMU"))
687 fxMesa->haveTwoTMUs = GL_FALSE;
688
689 fxMesa->emulateTwoTMUs = fxMesa->haveTwoTMUs;
690
691 if (!getenv("FX_DONT_FAKE_MULTITEX"))
692 fxMesa->emulateTwoTMUs = GL_TRUE;
693
694 if(getenv("FX_GLIDE_SWAPINTERVAL"))
695 fxMesa->swapInterval=atoi(getenv("FX_GLIDE_SWAPINTERVAL"));
696 else
697 fxMesa->swapInterval=1;
698
699 if(getenv("MESA_FX_SWAP_PENDING"))
700 fxMesa->maxPendingSwapBuffers=atoi(getenv("MESA_FX_SWAP_PENDING"));
701 else
702 fxMesa->maxPendingSwapBuffers=2;
703
704 if(getenv("MESA_FX_INFO"))
705 fxMesa->verbose=GL_TRUE;
706 else
707 fxMesa->verbose=GL_FALSE;
708
709 fxMesa->color=0xffffffff;
710 fxMesa->clearC=0;
711 fxMesa->clearA=0;
712
713 fxMesa->stats.swapBuffer=0;
714 fxMesa->stats.reqTexUpload=0;
715 fxMesa->stats.texUpload=0;
716 fxMesa->stats.memTexUpload=0;
717
718 fxMesa->tmuSrc=FX_TMU_NONE;
719 fxMesa->lastUnitsMode=FX_UM_NONE;
720 fxTMInit(fxMesa);
721
722 /* FX units setup */
723
724 fxMesa->unitsState.alphaTestEnabled=GL_FALSE;
725 fxMesa->unitsState.alphaTestFunc=GR_CMP_ALWAYS;
726 fxMesa->unitsState.alphaTestRefValue=0;
727
728 fxMesa->unitsState.blendEnabled=GL_FALSE;
729 fxMesa->unitsState.blendSrcFuncRGB=GR_BLEND_ONE;
730 fxMesa->unitsState.blendDstFuncRGB=GR_BLEND_ZERO;
731 fxMesa->unitsState.blendSrcFuncAlpha=GR_BLEND_ONE;
732 fxMesa->unitsState.blendDstFuncAlpha=GR_BLEND_ZERO;
733
734 fxMesa->unitsState.depthTestEnabled =GL_FALSE;
735 fxMesa->unitsState.depthMask =GL_TRUE;
736 fxMesa->unitsState.depthTestFunc =GR_CMP_LESS;
737
738 FX_grColorMask(FXTRUE, fxMesa->haveAlphaBuffer ? FXTRUE : FXFALSE);
739 if(fxMesa->haveDoubleBuffer) {
740 fxMesa->currentFB=GR_BUFFER_BACKBUFFER;
741 FX_grRenderBuffer(GR_BUFFER_BACKBUFFER);
742 } else {
743 fxMesa->currentFB=GR_BUFFER_FRONTBUFFER;
744 FX_grRenderBuffer(GR_BUFFER_FRONTBUFFER);
745 }
746
747 fxMesa->state = malloc(FX_grGetInteger(FX_GLIDE_STATE_SIZE));
748 fxMesa->fogTable = malloc(FX_grGetInteger(FX_FOG_TABLE_ENTRIES) *
749 sizeof(GrFog_t));
750
751 if (!fxMesa->state || !fxMesa->fogTable) {
752 if (fxMesa->state) free(fxMesa->state);
753 if (fxMesa->fogTable) free(fxMesa->fogTable);
754 return 0;
755 }
756
757 if(fxMesa->haveZBuffer)
758 FX_grDepthBufferMode(GR_DEPTHBUFFER_ZBUFFER);
759
760 #if (!FXMESA_USE_ARGB)
761 FX_grLfbWriteColorFormat(GR_COLORFORMAT_ABGR); /* Not every Glide has this */
762 #endif
763
764 fxMesa->textureAlign=FX_grGetInteger(FX_TEXTURE_ALIGN);
765 fxMesa->glCtx->Const.MaxTextureLevels=9;
766 fxMesa->glCtx->Const.MaxTextureSize=256;
767 fxMesa->glCtx->Const.MaxTextureUnits=fxMesa->emulateTwoTMUs ? 2 : 1;
768 fxMesa->new_state = _NEW_ALL;
769
770 /* Initialize the software rasterizer and helper modules.
771 */
772 _swrast_CreateContext( fxMesa->glCtx );
773 _ac_CreateContext( fxMesa->glCtx );
774 _tnl_CreateContext( fxMesa->glCtx );
775 _swsetup_CreateContext( fxMesa->glCtx );
776
777 _tnl_destroy_pipeline( fxMesa->glCtx );
778 _tnl_install_pipeline( fxMesa->glCtx, fx_pipeline );
779
780 fxAllocVB( fxMesa->glCtx );
781
782 fxSetupDDPointers(fxMesa->glCtx);
783
784 /* Tell the software rasterizer to use pixel fog always.
785 */
786 _swrast_allow_vertex_fog( fxMesa->glCtx, GL_FALSE );
787 _swrast_allow_pixel_fog( fxMesa->glCtx, GL_TRUE );
788
789 /* Tell tnl not to calculate or use vertex fog factors. (Needed to
790 * tell render stage not to clip fog coords).
791 */
792 /* _tnl_calculate_vertex_fog( fxMesa->glCtx, GL_FALSE ); */
793
794 fxDDInitExtensions(fxMesa->glCtx);
795
796 #ifdef FXVTXFMT
797 fxDDInitVtxfmt(fxMesa->glCtx);
798 #endif
799
800 FX_grGlideGetState((GrState*)fxMesa->state);
801
802 /* Run the config file */
803 _mesa_context_initialize( fxMesa->glCtx );
804
805 return 1;
806 }
807
808 /* Undo the above.
809 */
810 void fxDDDestroyFxMesaContext( fxMesaContext fxMesa )
811 {
812 _swsetup_DestroyContext( fxMesa->glCtx );
813 _tnl_DestroyContext( fxMesa->glCtx );
814 _ac_DestroyContext( fxMesa->glCtx );
815 _swrast_DestroyContext( fxMesa->glCtx );
816
817 if (fxMesa->state)
818 free(fxMesa->state);
819 if (fxMesa->fogTable)
820 free(fxMesa->fogTable);
821 fxTMClose(fxMesa);
822 fxFreeVB(fxMesa->glCtx);
823 }
824
825
826
827
828 void fxDDInitExtensions( GLcontext *ctx )
829 {
830 fxMesaContext fxMesa=(fxMesaContext)ctx->DriverCtx;
831
832 gl_extensions_disable(ctx, "GL_EXT_blend_logic_op");
833 gl_extensions_disable(ctx, "GL_EXT_blend_minmax");
834 gl_extensions_disable(ctx, "GL_EXT_blend_subtract");
835 gl_extensions_disable(ctx, "GL_EXT_blend_color");
836 gl_extensions_disable(ctx, "GL_EXT_fog_coord");
837
838 if (1)
839 gl_extensions_disable(ctx, "GL_EXT_point_parameters");
840
841 gl_extensions_add(ctx, GL_TRUE, "3DFX_set_global_palette", 0);
842
843 if (!fxMesa->haveTwoTMUs)
844 gl_extensions_disable(ctx, "GL_EXT_texture_env_add");
845
846 if (!fxMesa->emulateTwoTMUs)
847 gl_extensions_disable(ctx, "GL_ARB_multitexture");
848 }
849
850
851 /************************************************************************/
852 /************************************************************************/
853 /************************************************************************/
854
855 /* Check if the hardware supports the current context
856 *
857 * Performs similar work to fxDDChooseRenderState() - should be merged.
858 */
859 static GLboolean fxIsInHardware(GLcontext *ctx)
860 {
861 fxMesaContext fxMesa=(fxMesaContext)ctx->DriverCtx;
862
863 if (ctx->RenderMode != GL_RENDER)
864 return GL_FALSE;
865
866 if (!ctx->Hint.AllowDrawMem)
867 return GL_TRUE; /* you'll take it and like it */
868
869 if (ctx->Stencil.Enabled ||
870 ctx->Color.MultiDrawBuffer ||
871 ((ctx->Color.BlendEnabled) && (ctx->Color.BlendEquation!=GL_FUNC_ADD_EXT)) ||
872 ((ctx->Color.ColorLogicOpEnabled) && (ctx->Color.LogicOp!=GL_COPY)) ||
873 (ctx->Light.Model.ColorControl==GL_SEPARATE_SPECULAR_COLOR) ||
874 (!((ctx->Color.ColorMask[RCOMP]==ctx->Color.ColorMask[GCOMP]) &&
875 (ctx->Color.ColorMask[GCOMP]==ctx->Color.ColorMask[BCOMP]) &&
876 (ctx->Color.ColorMask[ACOMP]==ctx->Color.ColorMask[ACOMP])))
877 )
878 {
879 return GL_FALSE;
880 }
881 /* Unsupported texture/multitexture cases */
882
883 if(fxMesa->emulateTwoTMUs) {
884 if((ctx->_Enabled & (TEXTURE0_3D | TEXTURE1_3D)) ||
885 /* Not very well written ... */
886 ((ctx->_Enabled & (TEXTURE0_1D | TEXTURE1_1D)) &&
887 ((ctx->_Enabled & (TEXTURE0_2D | TEXTURE1_2D))!=(TEXTURE0_2D | TEXTURE1_2D)))
888 ) {
889 return GL_FALSE;
890 }
891
892 if (ctx->Texture._ReallyEnabled & TEXTURE0_2D) {
893 if (ctx->Texture.Unit[0].EnvMode == GL_BLEND &&
894 (ctx->Texture._ReallyEnabled & TEXTURE1_2D ||
895 ctx->Texture.Unit[0].EnvColor[0] != 0 ||
896 ctx->Texture.Unit[0].EnvColor[1] != 0 ||
897 ctx->Texture.Unit[0].EnvColor[2] != 0 ||
898 ctx->Texture.Unit[0].EnvColor[3] != 1)) {
899 return GL_FALSE;
900 }
901 if (ctx->Texture.Unit[0]._Current->Image[0]->Border > 0)
902 return GL_FALSE;
903 }
904
905 if (ctx->Texture._ReallyEnabled & TEXTURE1_2D) {
906 if (ctx->Texture.Unit[1].EnvMode == GL_BLEND)
907 return GL_FALSE;
908 if (ctx->Texture.Unit[0]._Current->Image[0]->Border > 0)
909 return GL_FALSE;
910 }
911
912 if (MESA_VERBOSE & (VERBOSE_DRIVER|VERBOSE_TEXTURE))
913 fprintf(stderr, "fxMesa: fxIsInHardware, envmode is %s/%s\n",
914 gl_lookup_enum_by_nr(ctx->Texture.Unit[0].EnvMode),
915 gl_lookup_enum_by_nr(ctx->Texture.Unit[1].EnvMode));
916
917 /* KW: This was wrong (I think) and I changed it... which doesn't mean
918 * it is now correct...
919 */
920 if((ctx->_Enabled & (TEXTURE0_1D | TEXTURE0_2D | TEXTURE0_3D)) &&
921 (ctx->_Enabled & (TEXTURE1_1D | TEXTURE1_2D | TEXTURE1_3D)))
922 {
923 /* Can't use multipass to blend a multitextured triangle - fall
924 * back to software.
925 */
926 if (!fxMesa->haveTwoTMUs && ctx->Color.BlendEnabled) {
927 return GL_FALSE;
928 }
929
930 if ((ctx->Texture.Unit[0].EnvMode!=ctx->Texture.Unit[1].EnvMode) &&
931 (ctx->Texture.Unit[0].EnvMode!=GL_MODULATE) &&
932 (ctx->Texture.Unit[0].EnvMode!=GL_REPLACE)) /* q2, seems ok... */
933 {
934 if (MESA_VERBOSE&VERBOSE_DRIVER)
935 fprintf(stderr, "fxMesa: unsupported multitex env mode\n");
936 return GL_FALSE;
937 }
938 }
939 } else {
940 if((ctx->_Enabled & (TEXTURE1_1D | TEXTURE1_2D | TEXTURE1_3D)) ||
941 /* Not very well written ... */
942 ((ctx->_Enabled & TEXTURE0_1D) &&
943 (!(ctx->_Enabled & TEXTURE0_2D)))
944 ) {
945 return GL_FALSE;
946 }
947
948
949 if((ctx->Texture._ReallyEnabled & TEXTURE0_2D) &&
950 (ctx->Texture.Unit[0].EnvMode==GL_BLEND)) {
951 return GL_FALSE;
952 }
953 }
954
955 return GL_TRUE;
956 }
957
958 static void update_texture_scales( GLcontext *ctx )
959 {
960 fxMesaContext fxMesa = FX_CONTEXT(ctx);
961 struct gl_texture_unit *t0 = &ctx->Texture.Unit[fxMesa->tmu_source[0]];
962 struct gl_texture_unit *t1 = &ctx->Texture.Unit[fxMesa->tmu_source[1]];
963
964 if (t0 && t0->_Current && FX_TEXTURE_DATA(t0)) {
965 fxMesa->s0scale = FX_TEXTURE_DATA(t0)->sScale;
966 fxMesa->t0scale = FX_TEXTURE_DATA(t0)->tScale;
967 fxMesa->inv_s0scale = 1.0 / fxMesa->s0scale;
968 fxMesa->inv_t0scale = 1.0 / fxMesa->t0scale;
969 }
970
971 if (t1 && t1->_Current && FX_TEXTURE_DATA(t1)) {
972 fxMesa->s1scale = FX_TEXTURE_DATA(t1)->sScale;
973 fxMesa->t1scale = FX_TEXTURE_DATA(t1)->tScale;
974 fxMesa->inv_s1scale = 1.0 / fxMesa->s1scale;
975 fxMesa->inv_t1scale = 1.0 / fxMesa->t1scale;
976 }
977 }
978
979 static void fxDDUpdateDDPointers(GLcontext *ctx, GLuint new_state)
980 {
981 fxMesaContext fxMesa = FX_CONTEXT(ctx);
982
983 _swrast_InvalidateState( ctx, new_state );
984 _ac_InvalidateState( ctx, new_state );
985 _tnl_InvalidateState( ctx, new_state );
986 _swsetup_InvalidateState( ctx, new_state );
987
988 /* Recalculate fog table on projection matrix changes. This used to
989 * be triggered by the NearFar callback.
990 */
991 if (new_state & _NEW_PROJECTION)
992 fxMesa->new_state |= FX_NEW_FOG;
993
994 if (new_state & (_FX_NEW_IS_IN_HARDWARE |
995 _FX_NEW_RENDERSTATE |
996 _FX_NEW_SETUP_FUNCTION |
997 _NEW_TEXTURE))
998 {
999 fxMesaContext fxMesa=(fxMesaContext)ctx->DriverCtx;
1000
1001 if (new_state & _FX_NEW_IS_IN_HARDWARE)
1002 fxMesa->is_in_hardware = fxIsInHardware(ctx);
1003
1004 if (fxMesa->new_state)
1005 fxSetupFXUnits(ctx);
1006
1007 if (new_state & _FX_NEW_RENDERSTATE)
1008 fxDDChooseRenderState( ctx );
1009
1010 if (new_state & _FX_NEW_SETUP_FUNCTION)
1011 ctx->Driver.BuildProjectedVertices = fx_validate_BuildProjVerts;
1012
1013 if (new_state & _NEW_TEXTURE)
1014 update_texture_scales( ctx );
1015
1016 }
1017
1018 #ifdef FXVTXFMT
1019 if (fxMesa->allow_vfmt) {
1020 if (new_state & _NEW_LIGHT)
1021 fx_update_lighting( ctx );
1022
1023 if (new_state & _FX_NEW_VTXFMT)
1024 fxDDCheckVtxfmt( ctx );
1025 }
1026 #endif
1027 }
1028
1029 static void fxDDRenderPrimitive( GLcontext *ctx, GLenum mode )
1030 {
1031 fxMesaContext fxMesa=(fxMesaContext)ctx->DriverCtx;
1032
1033 if (!fxMesa->is_in_hardware) {
1034 _swsetup_RenderPrimitive( ctx, mode );
1035 }
1036 else {
1037 fxMesa->render_prim = mode;
1038 }
1039 }
1040
1041
1042 static void fxDDRenderStart( GLcontext *ctx )
1043 {
1044 fxMesaContext fxMesa=(fxMesaContext)ctx->DriverCtx;
1045
1046 if (!fxMesa->is_in_hardware) {
1047 _swsetup_RenderStart( ctx );
1048 }
1049 else if (fxMesa->new_state) {
1050 fxSetupFXUnits( ctx );
1051 }
1052 }
1053
1054 static void fxDDRenderFinish( GLcontext *ctx )
1055 {
1056 fxMesaContext fxMesa=(fxMesaContext)ctx->DriverCtx;
1057
1058 if (!fxMesa->is_in_hardware) {
1059 _swsetup_RenderFinish( ctx );
1060 }
1061 }
1062
1063
1064
1065 void fxSetupDDPointers(GLcontext *ctx)
1066 {
1067 if (MESA_VERBOSE&VERBOSE_DRIVER) {
1068 fprintf(stderr,"fxmesa: fxSetupDDPointers()\n");
1069 }
1070
1071 ctx->Driver.UpdateState=fxDDUpdateDDPointers;
1072
1073 ctx->Driver.WriteDepthSpan=fxDDWriteDepthSpan;
1074 ctx->Driver.WriteDepthPixels=fxDDWriteDepthPixels;
1075 ctx->Driver.ReadDepthSpan=fxDDReadDepthSpan;
1076 ctx->Driver.ReadDepthPixels=fxDDReadDepthPixels;
1077
1078 ctx->Driver.GetString=fxDDGetString;
1079
1080 ctx->Driver.ClearIndex=NULL;
1081 ctx->Driver.ClearColor=fxDDClearColor;
1082 ctx->Driver.Clear=fxDDClear;
1083
1084 ctx->Driver.SetDrawBuffer=fxDDSetDrawBuffer;
1085 ctx->Driver.SetReadBuffer=fxDDSetReadBuffer;
1086 ctx->Driver.GetBufferSize=fxDDBufferSize;
1087
1088 ctx->Driver.Bitmap=fxDDDrawBitmap;
1089 ctx->Driver.DrawPixels=NULL;
1090 ctx->Driver.ReadPixels=fxDDReadPixels;
1091
1092 ctx->Driver.Finish=fxDDFinish;
1093 ctx->Driver.Flush=NULL;
1094
1095 ctx->Driver.RenderStart=fxDDRenderStart;
1096 ctx->Driver.RenderFinish=fxDDRenderFinish;
1097 ctx->Driver.ResetLineStipple=_swrast_ResetLineStipple;
1098 ctx->Driver.RenderPrimitive=fxDDRenderPrimitive;
1099
1100 ctx->Driver.TexImage2D = fxDDTexImage2D;
1101 ctx->Driver.TexSubImage2D = fxDDTexSubImage2D;
1102 ctx->Driver.GetTexImage = fxDDGetTexImage;
1103 ctx->Driver.TexEnv=fxDDTexEnv;
1104 ctx->Driver.TexParameter=fxDDTexParam;
1105 ctx->Driver.BindTexture=fxDDTexBind;
1106 ctx->Driver.DeleteTexture=fxDDTexDel;
1107 ctx->Driver.UpdateTexturePalette=fxDDTexPalette;
1108
1109 ctx->Driver.AlphaFunc=fxDDAlphaFunc;
1110 ctx->Driver.BlendFunc=fxDDBlendFunc;
1111 ctx->Driver.DepthFunc=fxDDDepthFunc;
1112 ctx->Driver.DepthMask=fxDDDepthMask;
1113 ctx->Driver.ColorMask=fxDDColorMask;
1114 ctx->Driver.Fogfv=fxDDFogfv;
1115 ctx->Driver.Scissor=fxDDScissor;
1116 ctx->Driver.FrontFace=fxDDFrontFace;
1117 ctx->Driver.CullFace=fxDDCullFace;
1118 ctx->Driver.ShadeModel=fxDDShadeModel;
1119 ctx->Driver.Enable=fxDDEnable;
1120
1121
1122
1123 fxSetupDDSpanPointers(ctx);
1124 fxDDUpdateDDPointers(ctx,~0);
1125 }
1126
1127
1128 #else
1129
1130
1131 /*
1132 * Need this to provide at least one external definition.
1133 */
1134
1135 extern int gl_fx_dummy_function_dd(void);
1136 int gl_fx_dummy_function_dd(void)
1137 {
1138 return 0;
1139 }
1140
1141 #endif /* FX */
1142