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