31604e3e1567c1c1c5dafe0e8dbe46d0a840677a
[mesa.git] / src / mesa / drivers / dri / tdfx / tdfx_dd.c
1 /* -*- mode: c; c-basic-offset: 3 -*-
2 *
3 * Copyright 2000 VA Linux Systems Inc., Fremont, California.
4 *
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * VA LINUX SYSTEMS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
23 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26 /* $XFree86: xc/lib/GL/mesa/src/drv/tdfx/tdfx_dd.c,v 1.10 2002/10/30 12:52:00 alanh Exp $ */
27
28 /*
29 * Original rewrite:
30 * Gareth Hughes <gareth@valinux.com>, 29 Sep - 1 Oct 2000
31 *
32 * Authors:
33 * Gareth Hughes <gareth@valinux.com>
34 * Brian Paul <brianp@valinux.com>
35 *
36 */
37
38 #include "tdfx_context.h"
39 #include "tdfx_dd.h"
40 #include "tdfx_lock.h"
41 #include "tdfx_vb.h"
42 #include "tdfx_pixels.h"
43
44 #include "context.h"
45 #include "enums.h"
46 #include "swrast/swrast.h"
47 #if defined(USE_X86_ASM)
48 #include "X86/common_x86_asm.h"
49 #endif
50
51
52 #define TDFX_DATE "20021125"
53
54
55 /* These are used in calls to FX_grColorMaskv() */
56 const GLboolean false4[4] = { GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE };
57 const GLboolean true4[4] = { GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE };
58
59
60
61 /* KW: Put the word Mesa in the render string because quakeworld
62 * checks for this rather than doing a glGet(GL_MAX_TEXTURE_SIZE).
63 * Why?
64 */
65 static const GLubyte *tdfxDDGetString( GLcontext *ctx, GLenum name )
66 {
67 tdfxContextPtr fxMesa = (tdfxContextPtr) ctx->DriverCtx;
68
69 switch ( name ) {
70 case GL_RENDERER:
71 {
72 /* The renderer string must be per-context state to handle
73 * multihead correctly.
74 */
75 char *buffer = fxMesa->rendererString;
76 char hardware[100];
77
78 LOCK_HARDWARE(fxMesa);
79 strcpy( hardware, fxMesa->Glide.grGetString(GR_HARDWARE) );
80 UNLOCK_HARDWARE(fxMesa);
81
82 strcpy( buffer, "Mesa DRI " );
83 strcat( buffer, TDFX_DATE );
84 strcat( buffer, " " );
85
86 if ( strcmp( hardware, "Voodoo3 (tm)" ) == 0 ) {
87 strcat( buffer, "Voodoo3" );
88 }
89 else if ( strcmp( hardware, "Voodoo Banshee (tm)" ) == 0 ) {
90 strcat( buffer, "VoodooBanshee" );
91 }
92 else if ( strcmp( hardware, "Voodoo4 (tm)" ) == 0 ) {
93 strcat( buffer, "Voodoo4" );
94 }
95 else if ( strcmp( hardware, "Voodoo5 (tm)" ) == 0 ) {
96 strcat( buffer, "Voodoo5" );
97 }
98 else {
99 /* unexpected result: replace spaces with hyphens */
100 int i;
101 for ( i = 0 ; hardware[i] && i < 60 ; i++ ) {
102 if ( hardware[i] == ' ' || hardware[i] == '\t' )
103 hardware[i] = '-';
104 }
105 strcat( buffer, hardware );
106 }
107
108 /* Append any CPU-specific information.
109 */
110 #ifdef USE_X86_ASM
111 if ( _mesa_x86_cpu_features ) {
112 strncat( buffer, " x86", 4 );
113 }
114 #endif
115 #ifdef USE_MMX_ASM
116 if ( cpu_has_mmx ) {
117 strncat( buffer, "/MMX", 4 );
118 }
119 #endif
120 #ifdef USE_3DNOW_ASM
121 if ( cpu_has_3dnow ) {
122 strncat( buffer, "/3DNow!", 7 );
123 }
124 #endif
125 #ifdef USE_SSE_ASM
126 if ( cpu_has_xmm ) {
127 strncat( buffer, "/SSE", 4 );
128 }
129 #endif
130 return (const GLubyte *) buffer;
131 }
132 case GL_VENDOR:
133 return (const GLubyte *)"VA Linux Systems, Inc.";
134 default:
135 return NULL;
136 }
137 }
138
139
140 /* Return uptodate buffer size information.
141 */
142 static void tdfxDDGetBufferSize( GLframebuffer *buffer,
143 GLuint *width, GLuint *height )
144 {
145 GET_CURRENT_CONTEXT(ctx);
146 tdfxContextPtr fxMesa = TDFX_CONTEXT(ctx);
147
148 LOCK_HARDWARE( fxMesa );
149 *width = fxMesa->width;
150 *height = fxMesa->height;
151 UNLOCK_HARDWARE( fxMesa );
152 }
153
154
155
156 /*
157 * Return the current value of the occlusion test flag and
158 * reset the flag (hardware counters) to false.
159 */
160 static GLboolean get_occlusion_result( GLcontext *ctx )
161 {
162 tdfxContextPtr fxMesa = TDFX_CONTEXT(ctx);
163 GLboolean result;
164
165 LOCK_HARDWARE( fxMesa );
166 fxMesa->Glide.grFinish(); /* required to flush the FIFO - FB 21-01-2002 */
167
168 if (ctx->Depth.OcclusionTest) {
169 if (ctx->OcclusionResult) {
170 result = GL_TRUE; /* result of software rendering */
171 }
172 else {
173 FxI32 zfail, in;
174 fxMesa->Glide.grGet(GR_STATS_PIXELS_DEPTHFUNC_FAIL, 4, &zfail);
175 fxMesa->Glide.grGet(GR_STATS_PIXELS_IN, 4, &in);
176 /* Geometry is occluded if there is no input (in == 0) */
177 /* or if all pixels failed the depth test (zfail == in) */
178 /* The < 1 is there because I have empirically seen cases where */
179 /* zfail > in.... go figure. FB - 21-01-2002. */
180 result = ((in - zfail) < 1 || in == 0) ? GL_FALSE : GL_TRUE;
181 }
182 }
183 else {
184 result = ctx->OcclusionResultSaved;
185 }
186
187 /* reset results now */
188 fxMesa->Glide.grReset(GR_STATS_PIXELS);
189 ctx->OcclusionResult = GL_FALSE;
190 ctx->OcclusionResultSaved = GL_FALSE;
191
192 UNLOCK_HARDWARE( fxMesa );
193
194 return result;
195 }
196
197
198 /*
199 * We're only implementing this function to handle the
200 * GL_OCCLUSTION_TEST_RESULT_HP case. It's special because it
201 * has a side-effect: resetting the occlustion result flag.
202 */
203 static GLboolean tdfxDDGetBooleanv( GLcontext *ctx, GLenum pname,
204 GLboolean *result )
205 {
206 if ( pname == GL_OCCLUSION_TEST_RESULT_HP ) {
207 *result = get_occlusion_result( ctx );
208 return GL_TRUE;
209 }
210 return GL_FALSE;
211 }
212
213 static GLboolean tdfxDDGetDoublev( GLcontext *ctx, GLenum pname,
214 GLdouble *result )
215 {
216 if ( pname == GL_OCCLUSION_TEST_RESULT_HP ) {
217 *result = (GLdouble) get_occlusion_result( ctx );
218 return GL_TRUE;
219 }
220 return GL_FALSE;
221 }
222
223 static GLboolean tdfxDDGetFloatv( GLcontext *ctx, GLenum pname,
224 GLfloat *result )
225 {
226 if ( pname == GL_OCCLUSION_TEST_RESULT_HP ) {
227 *result = (GLfloat) get_occlusion_result( ctx );
228 return GL_TRUE;
229 }
230 return GL_FALSE;
231 }
232
233 static GLboolean tdfxDDGetIntegerv( GLcontext *ctx, GLenum pname,
234 GLint *result )
235 {
236 if ( pname == GL_OCCLUSION_TEST_RESULT_HP ) {
237 *result = (GLint) get_occlusion_result( ctx );
238 return GL_TRUE;
239 }
240 return GL_FALSE;
241 }
242
243
244
245 #define VISUAL_EQUALS_RGBA(vis, r, g, b, a) \
246 ((vis.redBits == r) && \
247 (vis.greenBits == g) && \
248 (vis.blueBits == b) && \
249 (vis.alphaBits == a))
250
251 void tdfxDDInitDriverFuncs( GLcontext *ctx )
252 {
253 if ( MESA_VERBOSE & VERBOSE_DRIVER ) {
254 fprintf( stderr, "tdfx: %s()\n", __FUNCTION__ );
255 }
256
257 ctx->Driver.GetString = tdfxDDGetString;
258 ctx->Driver.GetBufferSize = tdfxDDGetBufferSize;
259 ctx->Driver.ResizeBuffers = _swrast_alloc_buffers;
260 ctx->Driver.Error = NULL;
261
262 /* Pixel path fallbacks.
263 */
264 ctx->Driver.Accum = _swrast_Accum;
265 ctx->Driver.Bitmap = _swrast_Bitmap;
266 ctx->Driver.CopyPixels = _swrast_CopyPixels;
267 ctx->Driver.DrawPixels = _swrast_DrawPixels;
268 ctx->Driver.ReadPixels = _swrast_ReadPixels;
269
270 /* Accelerated paths
271 */
272 if ( VISUAL_EQUALS_RGBA(ctx->Visual, 8, 8, 8, 8) )
273 {
274 ctx->Driver.DrawPixels = tdfx_drawpixels_R8G8B8A8;
275 ctx->Driver.ReadPixels = tdfx_readpixels_R8G8B8A8;
276 }
277 else if ( VISUAL_EQUALS_RGBA(ctx->Visual, 5, 6, 5, 0) )
278 {
279 ctx->Driver.ReadPixels = tdfx_readpixels_R5G6B5;
280 }
281
282 ctx->Driver.GetBooleanv = tdfxDDGetBooleanv;
283 ctx->Driver.GetDoublev = tdfxDDGetDoublev;
284 ctx->Driver.GetFloatv = tdfxDDGetFloatv;
285 ctx->Driver.GetIntegerv = tdfxDDGetIntegerv;
286 ctx->Driver.GetPointerv = NULL;
287 }
288
289
290 /*
291 * These are here for lack of a better place.
292 */
293
294 void
295 FX_grColorMaskv(GLcontext *ctx, const GLboolean rgba[4])
296 {
297 tdfxContextPtr fxMesa = TDFX_CONTEXT(ctx);
298 LOCK_HARDWARE(fxMesa);
299 if (ctx->Visual.redBits == 8) {
300 /* 32bpp mode */
301 ASSERT( fxMesa->Glide.grColorMaskExt );
302 fxMesa->Glide.grColorMaskExt(rgba[RCOMP], rgba[GCOMP],
303 rgba[BCOMP], rgba[ACOMP]);
304 }
305 else {
306 /* 16 bpp mode */
307 /* we never have an alpha buffer */
308 fxMesa->Glide.grColorMask(rgba[RCOMP] || rgba[GCOMP] || rgba[BCOMP],
309 GL_FALSE);
310 }
311 UNLOCK_HARDWARE(fxMesa);
312 }
313
314 void
315 FX_grColorMaskv_NoLock(GLcontext *ctx, const GLboolean rgba[4])
316 {
317 tdfxContextPtr fxMesa = TDFX_CONTEXT(ctx);
318 if (ctx->Visual.redBits == 8) {
319 /* 32bpp mode */
320 ASSERT( fxMesa->Glide.grColorMaskExt );
321 fxMesa->Glide.grColorMaskExt(rgba[RCOMP], rgba[GCOMP],
322 rgba[BCOMP], rgba[ACOMP]);
323 }
324 else {
325 /* 16 bpp mode */
326 /* we never have an alpha buffer */
327 fxMesa->Glide.grColorMask(rgba[RCOMP] || rgba[GCOMP] || rgba[BCOMP],
328 GL_FALSE);
329 }
330 }