new texture image routines work now
[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, const GLchan color[4])
132 {
133 fxMesaContext fxMesa=(fxMesaContext)ctx->DriverCtx;
134 GLubyte col[4];
135
136 if (MESA_VERBOSE&VERBOSE_DRIVER) {
137 fprintf(stderr,"fxmesa: fxDDClearColor(%d,%d,%d,%d)\n",
138 color[0], color[1], color[2], color[3]);
139 }
140
141 ASSIGN_4V( col, color[0], color[1], color[2], 255 );
142 fxMesa->clearC = FXCOLOR4( col );
143 fxMesa->clearA = color[3];
144 }
145
146
147 /* Clear the color and/or depth buffers */
148 static void fxDDClear(GLcontext *ctx, GLbitfield mask, GLboolean all,
149 GLint x, GLint y, GLint width, GLint height )
150 {
151 fxMesaContext fxMesa=(fxMesaContext)ctx->DriverCtx;
152 const GLuint colorMask = *((GLuint *) &ctx->Color.ColorMask);
153 const FxU16 clearD = (FxU16) (ctx->Depth.Clear * 0xffff);
154 GLbitfield softwareMask = mask & (DD_STENCIL_BIT | DD_ACCUM_BIT);
155
156 /* we can't clear stencil or accum buffers */
157 mask &= ~(DD_STENCIL_BIT | DD_ACCUM_BIT);
158
159 if (MESA_VERBOSE & VERBOSE_DRIVER) {
160 fprintf(stderr,"fxmesa: fxDDClear(%d,%d,%d,%d)\n", (int) x, (int) y,
161 (int) width, (int) height);
162 }
163
164 if (colorMask != 0xffffffff) {
165 /* do masked color buffer clears in software */
166 softwareMask |= (mask & (DD_FRONT_LEFT_BIT | DD_BACK_LEFT_BIT));
167 mask &= ~(DD_FRONT_LEFT_BIT | DD_BACK_LEFT_BIT);
168 }
169
170 /*
171 * This could probably be done fancier but doing each possible case
172 * explicitly is less error prone.
173 */
174 switch (mask) {
175 case DD_BACK_LEFT_BIT | DD_DEPTH_BIT:
176 /* back buffer & depth */
177 FX_grDepthMask(FXTRUE);
178 FX_grRenderBuffer(GR_BUFFER_BACKBUFFER);
179 FX_grBufferClear(fxMesa->clearC, fxMesa->clearA, clearD);
180 if (!ctx->Depth.Mask) {
181 FX_grDepthMask(FXFALSE);
182 }
183 break;
184 case DD_FRONT_LEFT_BIT | DD_DEPTH_BIT:
185 /* XXX it appears that the depth buffer isn't cleared when
186 * glRenderBuffer(GR_BUFFER_FRONTBUFFER) is set.
187 * This is a work-around/
188 */
189 /* clear depth */
190 FX_grDepthMask(FXTRUE);
191 FX_grRenderBuffer(GR_BUFFER_BACKBUFFER);
192 FX_grColorMask(FXFALSE,FXFALSE);
193 FX_grBufferClear(fxMesa->clearC, fxMesa->clearA, clearD);
194 /* clear front */
195 FX_grColorMask(FXTRUE, ctx->Color.ColorMask[ACOMP] && fxMesa->haveAlphaBuffer);
196 FX_grRenderBuffer(GR_BUFFER_FRONTBUFFER);
197 FX_grBufferClear(fxMesa->clearC, fxMesa->clearA, clearD);
198 break;
199 case DD_BACK_LEFT_BIT:
200 /* back buffer only */
201 FX_grDepthMask(FXFALSE);
202 FX_grRenderBuffer(GR_BUFFER_BACKBUFFER);
203 FX_grBufferClear(fxMesa->clearC, fxMesa->clearA, clearD);
204 if (ctx->Depth.Mask) {
205 FX_grDepthMask(FXTRUE);
206 }
207 break;
208 case DD_FRONT_LEFT_BIT:
209 /* front buffer only */
210 FX_grDepthMask(FXFALSE);
211 FX_grRenderBuffer(GR_BUFFER_FRONTBUFFER);
212 FX_grBufferClear(fxMesa->clearC, fxMesa->clearA, clearD);
213 if (ctx->Depth.Mask) {
214 FX_grDepthMask(FXTRUE);
215 }
216 break;
217 case DD_FRONT_LEFT_BIT | DD_BACK_LEFT_BIT:
218 /* front and back */
219 FX_grDepthMask(FXFALSE);
220 FX_grRenderBuffer(GR_BUFFER_BACKBUFFER);
221 FX_grBufferClear(fxMesa->clearC, fxMesa->clearA, clearD);
222 FX_grRenderBuffer(GR_BUFFER_FRONTBUFFER);
223 FX_grBufferClear(fxMesa->clearC, fxMesa->clearA, clearD);
224 if (ctx->Depth.Mask) {
225 FX_grDepthMask(FXTRUE);
226 }
227 break;
228 case DD_FRONT_LEFT_BIT | DD_BACK_LEFT_BIT | DD_DEPTH_BIT:
229 /* clear front */
230 FX_grDepthMask(FXFALSE);
231 FX_grRenderBuffer(GR_BUFFER_FRONTBUFFER);
232 FX_grBufferClear(fxMesa->clearC, fxMesa->clearA, clearD);
233 /* clear back and depth */
234 FX_grDepthMask(FXTRUE);
235 FX_grRenderBuffer(GR_BUFFER_BACKBUFFER);
236 FX_grBufferClear(fxMesa->clearC, fxMesa->clearA, clearD);
237 if (!ctx->Depth.Mask) {
238 FX_grDepthMask(FXFALSE);
239 }
240 break;
241 case DD_DEPTH_BIT:
242 /* just the depth buffer */
243 FX_grRenderBuffer(GR_BUFFER_BACKBUFFER);
244 FX_grColorMask(FXFALSE,FXFALSE);
245 FX_grDepthMask(FXTRUE);
246 FX_grBufferClear(fxMesa->clearC, fxMesa->clearA, clearD);
247 FX_grColorMask(FXTRUE, ctx->Color.ColorMask[ACOMP] && fxMesa->haveAlphaBuffer);
248 if (ctx->Color.DrawDestMask & FRONT_LEFT_BIT)
249 FX_grRenderBuffer(GR_BUFFER_FRONTBUFFER);
250 if (!ctx->Depth.Test || !ctx->Depth.Mask)
251 FX_grDepthMask(FXFALSE);
252 break;
253 default:
254 /* error */
255 ;
256 }
257
258 /* Clear any remaining buffers:
259 */
260 if (softwareMask)
261 _swrast_Clear( ctx, softwareMask, all, x, y, width, height );
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 void 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 {
342 _swrast_Bitmap( ctx, px, py, width, height, unpack, bitmap );
343 return;
344 }
345
346
347 if (ctx->Scissor.Enabled) {
348 /* This is a bit tricky, but by carefully adjusting the px, py,
349 * width, height, skipPixels and skipRows values we can do
350 * scissoring without special code in the rendering loop.
351 *
352 * KW: This code is never reached, see the test above.
353 */
354
355 /* we'll construct a new pixelstore struct */
356 finalUnpack = &scissoredUnpack;
357 scissoredUnpack = *unpack;
358 if (scissoredUnpack.RowLength == 0)
359 scissoredUnpack.RowLength = width;
360
361 /* clip left */
362 if (px < ctx->Scissor.X) {
363 scissoredUnpack.SkipPixels += (ctx->Scissor.X - px);
364 width -= (ctx->Scissor.X - px);
365 px = ctx->Scissor.X;
366 }
367 /* clip right */
368 if (px + width >= ctx->Scissor.X + ctx->Scissor.Width) {
369 width -= (px + width - (ctx->Scissor.X + ctx->Scissor.Width));
370 }
371 /* clip bottom */
372 if (py < ctx->Scissor.Y) {
373 scissoredUnpack.SkipRows += (ctx->Scissor.Y - py);
374 height -= (ctx->Scissor.Y - py);
375 py = ctx->Scissor.Y;
376 }
377 /* clip top */
378 if (py + height >= ctx->Scissor.Y + ctx->Scissor.Height) {
379 height -= (py + height - (ctx->Scissor.Y + ctx->Scissor.Height));
380 }
381
382 if (width <= 0 || height <= 0)
383 return;
384 }
385 else {
386 finalUnpack = unpack;
387 }
388
389 /* compute pixel value */
390 {
391 GLint r = (GLint) (ctx->Current.RasterColor[0] * 255.0f);
392 GLint g = (GLint) (ctx->Current.RasterColor[1] * 255.0f);
393 GLint b = (GLint) (ctx->Current.RasterColor[2] * 255.0f);
394 /*GLint a = (GLint)(ctx->Current.RasterColor[3]*255.0f);*/
395 if (fxMesa->bgrOrder)
396 color = (FxU16)
397 ( ((FxU16)0xf8 & b) << (11-3)) |
398 ( ((FxU16)0xfc & g) << (5-3+1)) |
399 ( ((FxU16)0xf8 & r) >> 3);
400 else
401 color = (FxU16)
402 ( ((FxU16)0xf8 & r) << (11-3)) |
403 ( ((FxU16)0xfc & g) << (5-3+1)) |
404 ( ((FxU16)0xf8 & b) >> 3);
405 }
406
407 info.size = sizeof(info);
408 if (!FX_grLfbLock(GR_LFB_WRITE_ONLY,
409 fxMesa->currentFB,
410 GR_LFBWRITEMODE_565,
411 GR_ORIGIN_UPPER_LEFT,
412 FXFALSE,
413 &info)) {
414 #ifndef FX_SILENT
415 fprintf(stderr,"fx Driver: error locking the linear frame buffer\n");
416 #endif
417 return;
418 }
419
420 {
421 const GLint winX = 0;
422 const GLint winY = fxMesa->height - 1;
423 /* The dest stride depends on the hardware and whether we're drawing
424 * to the front or back buffer. This compile-time test seems to do
425 * the job for now.
426 */
427 const GLint dstStride = info.strideInBytes / 2; /* stride in GLushorts */
428
429 GLint row;
430 /* compute dest address of bottom-left pixel in bitmap */
431 GLushort *dst = (GLushort *) info.lfbPtr
432 + (winY - py) * dstStride
433 + (winX + px);
434
435 for (row = 0; row < height; row++) {
436 const GLubyte *src = (const GLubyte *) _mesa_image_address( finalUnpack,
437 bitmap, width, height, GL_COLOR_INDEX, GL_BITMAP, 0, row, 0 );
438 if (finalUnpack->LsbFirst) {
439 /* least significan bit first */
440 GLubyte mask = 1U << (finalUnpack->SkipPixels & 0x7);
441 GLint col;
442 for (col=0; col<width; col++) {
443 if (*src & mask) {
444 dst[col] = color;
445 }
446 if (mask == 128U) {
447 src++;
448 mask = 1U;
449 }
450 else {
451 mask = mask << 1;
452 }
453 }
454 if (mask != 1)
455 src++;
456 }
457 else {
458 /* most significan bit first */
459 GLubyte mask = 128U >> (finalUnpack->SkipPixels & 0x7);
460 GLint col;
461 for (col=0; col<width; col++) {
462 if (*src & mask) {
463 dst[col] = color;
464 }
465 if (mask == 1U) {
466 src++;
467 mask = 128U;
468 }
469 else {
470 mask = mask >> 1;
471 }
472 }
473 if (mask != 128)
474 src++;
475 }
476 dst -= dstStride;
477 }
478 }
479
480 FX_grLfbUnlock(GR_LFB_WRITE_ONLY,fxMesa->currentFB);
481 }
482
483
484 static void fxDDReadPixels( GLcontext *ctx, GLint x, GLint y,
485 GLsizei width, GLsizei height,
486 GLenum format, GLenum type,
487 const struct gl_pixelstore_attrib *packing,
488 GLvoid *dstImage )
489 {
490 if (ctx->_ImageTransferState) {
491 _swrast_ReadPixels( ctx, x, y, width, height, format, type,
492 packing, dstImage );
493 return;
494 }
495 else {
496 fxMesaContext fxMesa=(fxMesaContext)ctx->DriverCtx;
497 GrLfbInfo_t info;
498
499 BEGIN_BOARD_LOCK();
500 if (grLfbLock(GR_LFB_READ_ONLY,
501 fxMesa->currentFB,
502 GR_LFBWRITEMODE_ANY,
503 GR_ORIGIN_UPPER_LEFT,
504 FXFALSE,
505 &info)) {
506 const GLint winX = 0;
507 const GLint winY = fxMesa->height - 1;
508 const GLint srcStride = info.strideInBytes / 2; /* stride in GLushorts */
509 const GLushort *src = (const GLushort *) info.lfbPtr
510 + (winY - y) * srcStride + (winX + x);
511 GLubyte *dst = (GLubyte *) _mesa_image_address(packing, dstImage,
512 width, height, format, type, 0, 0, 0);
513 GLint dstStride = _mesa_image_row_stride(packing, width, format, type);
514
515 if (format == GL_RGB && type == GL_UNSIGNED_BYTE) {
516 /* convert 5R6G5B into 8R8G8B */
517 GLint row, col;
518 const GLint halfWidth = width >> 1;
519 const GLint extraPixel = (width & 1);
520 for (row = 0; row < height; row++) {
521 GLubyte *d = dst;
522 for (col = 0; col < halfWidth; col++) {
523 const GLuint pixel = ((const GLuint *) src)[col];
524 const GLint pixel0 = pixel & 0xffff;
525 const GLint pixel1 = pixel >> 16;
526 *d++ = FX_PixelToR[pixel0];
527 *d++ = FX_PixelToG[pixel0];
528 *d++ = FX_PixelToB[pixel0];
529 *d++ = FX_PixelToR[pixel1];
530 *d++ = FX_PixelToG[pixel1];
531 *d++ = FX_PixelToB[pixel1];
532 }
533 if (extraPixel) {
534 GLushort pixel = src[width-1];
535 *d++ = FX_PixelToR[pixel];
536 *d++ = FX_PixelToG[pixel];
537 *d++ = FX_PixelToB[pixel];
538 }
539 dst += dstStride;
540 src -= srcStride;
541 }
542 }
543 else if (format == GL_RGBA && type == GL_UNSIGNED_BYTE) {
544 /* convert 5R6G5B into 8R8G8B8A */
545 GLint row, col;
546 const GLint halfWidth = width >> 1;
547 const GLint extraPixel = (width & 1);
548 for (row = 0; row < height; row++) {
549 GLubyte *d = dst;
550 for (col = 0; col < halfWidth; col++) {
551 const GLuint pixel = ((const GLuint *) src)[col];
552 const GLint pixel0 = pixel & 0xffff;
553 const GLint pixel1 = pixel >> 16;
554 *d++ = FX_PixelToR[pixel0];
555 *d++ = FX_PixelToG[pixel0];
556 *d++ = FX_PixelToB[pixel0];
557 *d++ = 255;
558 *d++ = FX_PixelToR[pixel1];
559 *d++ = FX_PixelToG[pixel1];
560 *d++ = FX_PixelToB[pixel1];
561 *d++ = 255;
562 }
563 if (extraPixel) {
564 const GLushort pixel = src[width-1];
565 *d++ = FX_PixelToR[pixel];
566 *d++ = FX_PixelToG[pixel];
567 *d++ = FX_PixelToB[pixel];
568 *d++ = 255;
569 }
570 dst += dstStride;
571 src -= srcStride;
572 }
573 }
574 else if (format == GL_RGB && type == GL_UNSIGNED_SHORT_5_6_5) {
575 /* directly memcpy 5R6G5B pixels into client's buffer */
576 const GLint widthInBytes = width * 2;
577 GLint row;
578 for (row = 0; row < height; row++) {
579 MEMCPY(dst, src, widthInBytes);
580 dst += dstStride;
581 src -= srcStride;
582 }
583 }
584 else {
585 grLfbUnlock(GR_LFB_READ_ONLY, fxMesa->currentFB);
586 END_BOARD_LOCK();
587 _swrast_ReadPixels( ctx, x, y, width, height, format, type,
588 packing, dstImage );
589 return;
590 }
591
592 grLfbUnlock(GR_LFB_READ_ONLY, fxMesa->currentFB);
593 }
594 END_BOARD_LOCK();
595 }
596 }
597
598
599
600 static void fxDDFinish(GLcontext *ctx)
601 {
602 FX_grFlush();
603 }
604
605
606
607
608
609 /* KW: Put the word Mesa in the render string because quakeworld
610 * checks for this rather than doing a glGet(GL_MAX_TEXTURE_SIZE).
611 * Why?
612 */
613 static const GLubyte *fxDDGetString(GLcontext *ctx, GLenum name)
614 {
615 switch (name) {
616 case GL_RENDERER:
617 {
618 static char buf[80];
619
620 if (glbHWConfig.SSTs[glbCurrentBoard].type==GR_SSTTYPE_VOODOO) {
621 GrVoodooConfig_t *vc =
622 &glbHWConfig.SSTs[glbCurrentBoard].sstBoard.VoodooConfig;
623
624 sprintf(buf,
625 "Mesa Glide v0.30 Voodoo_Graphics %d "
626 "CARD/%d FB/%d TM/%d TMU/%s",
627 glbCurrentBoard,
628 (vc->sliDetect ? (vc->fbRam*2) : vc->fbRam),
629 (vc->tmuConfig[GR_TMU0].tmuRam +
630 ((vc->nTexelfx>1) ? vc->tmuConfig[GR_TMU1].tmuRam : 0)),
631 vc->nTexelfx,
632 (vc->sliDetect ? "SLI" : "NOSLI"));
633 }
634 else if (glbHWConfig.SSTs[glbCurrentBoard].type==GR_SSTTYPE_SST96) {
635 GrSst96Config_t *sc =
636 &glbHWConfig.SSTs[glbCurrentBoard].sstBoard.SST96Config;
637
638 sprintf(buf,
639 "Glide v0.30 Voodoo_Rush %d "
640 "CARD/%d FB/%d TM/%d TMU/NOSLI",
641 glbCurrentBoard,
642 sc->fbRam,
643 sc->tmuConfig.tmuRam,
644 sc->nTexelfx);
645 }
646 else {
647 strcpy(buf, "Glide v0.30 UNKNOWN");
648 }
649 return (GLubyte *) buf;
650 }
651 default:
652 return NULL;
653 }
654 }
655
656 static const struct gl_pipeline_stage *fx_pipeline[] = {
657 &_tnl_update_material_stage, /* TODO: Add the fastpath here */
658 &_tnl_vertex_transform_stage,
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.TexImage2D = fxDDTexImage2D;
1107 ctx->Driver.TexSubImage2D = fxDDTexSubImage2D;
1108 ctx->Driver.TexEnv = fxDDTexEnv;
1109 ctx->Driver.TexParameter = fxDDTexParam;
1110 ctx->Driver.BindTexture = fxDDTexBind;
1111 ctx->Driver.DeleteTexture = fxDDTexDel;
1112 ctx->Driver.UpdateTexturePalette = fxDDTexPalette;
1113
1114 ctx->Driver.AlphaFunc=fxDDAlphaFunc;
1115 ctx->Driver.BlendFunc=fxDDBlendFunc;
1116 ctx->Driver.DepthFunc=fxDDDepthFunc;
1117 ctx->Driver.DepthMask=fxDDDepthMask;
1118 ctx->Driver.ColorMask=fxDDColorMask;
1119 ctx->Driver.Fogfv=fxDDFogfv;
1120 ctx->Driver.Scissor=fxDDScissor;
1121 ctx->Driver.FrontFace=fxDDFrontFace;
1122 ctx->Driver.CullFace=fxDDCullFace;
1123 ctx->Driver.ShadeModel=fxDDShadeModel;
1124 ctx->Driver.Enable=fxDDEnable;
1125
1126
1127
1128 fxSetupDDSpanPointers(ctx);
1129 fxDDUpdateDDPointers(ctx,~0);
1130 }
1131
1132
1133 #else
1134
1135
1136 /*
1137 * Need this to provide at least one external definition.
1138 */
1139
1140 extern int gl_fx_dummy_function_dd(void);
1141 int gl_fx_dummy_function_dd(void)
1142 {
1143 return 0;
1144 }
1145
1146 #endif /* FX */
1147