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