The required DDX and DRI version numbers were switched. This went unnoticed
[mesa.git] / src / mesa / drivers / dri / tdfx / tdfx_screen.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_screen.c,v 1.3 2002/02/22 21:45:03 dawes 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 *
35 */
36
37 #include "tdfx_dri.h"
38 #include "tdfx_context.h"
39 #include "tdfx_lock.h"
40 #include "tdfx_vb.h"
41 #include "tdfx_tris.h"
42
43
44 #ifdef DEBUG_LOCKING
45 char *prevLockFile = 0;
46 int prevLockLine = 0;
47 #endif
48
49 #ifndef TDFX_DEBUG
50 int TDFX_DEBUG = (0
51 /* | DEBUG_ALWAYS_SYNC */
52 /* | DEBUG_VERBOSE_API */
53 /* | DEBUG_VERBOSE_MSG */
54 /* | DEBUG_VERBOSE_LRU */
55 /* | DEBUG_VERBOSE_DRI */
56 /* | DEBUG_VERBOSE_IOCTL */
57 /* | DEBUG_VERBOSE_2D */
58 );
59 #endif
60
61
62
63 static GLboolean
64 tdfxCreateScreen( __DRIscreenPrivate *sPriv )
65 {
66 tdfxScreenPrivate *fxScreen;
67 TDFXDRIPtr fxDRIPriv = (TDFXDRIPtr) sPriv->pDevPriv;
68
69 /* Allocate the private area */
70 fxScreen = (tdfxScreenPrivate *) CALLOC( sizeof(tdfxScreenPrivate) );
71 if ( !fxScreen )
72 return GL_FALSE;
73
74 fxScreen->driScrnPriv = sPriv;
75 sPriv->private = (void *) fxScreen;
76
77 fxScreen->regs.handle = fxDRIPriv->regs;
78 fxScreen->regs.size = fxDRIPriv->regsSize;
79 fxScreen->deviceID = fxDRIPriv->deviceID;
80 fxScreen->width = fxDRIPriv->width;
81 fxScreen->height = fxDRIPriv->height;
82 fxScreen->mem = fxDRIPriv->mem;
83 fxScreen->cpp = fxDRIPriv->cpp;
84 fxScreen->stride = fxDRIPriv->stride;
85 fxScreen->fifoOffset = fxDRIPriv->fifoOffset;
86 fxScreen->fifoSize = fxDRIPriv->fifoSize;
87 fxScreen->fbOffset = fxDRIPriv->fbOffset;
88 fxScreen->backOffset = fxDRIPriv->backOffset;
89 fxScreen->depthOffset = fxDRIPriv->depthOffset;
90 fxScreen->textureOffset = fxDRIPriv->textureOffset;
91 fxScreen->textureSize = fxDRIPriv->textureSize;
92 fxScreen->sarea_priv_offset = fxDRIPriv->sarea_priv_offset;
93
94 if ( drmMap( sPriv->fd, fxScreen->regs.handle,
95 fxScreen->regs.size, &fxScreen->regs.map ) ) {
96 return GL_FALSE;
97 }
98
99 return GL_TRUE;
100 }
101
102
103 static void
104 tdfxDestroyScreen( __DRIscreenPrivate *sPriv )
105 {
106 tdfxScreenPrivate *fxScreen = (tdfxScreenPrivate *) sPriv->private;
107
108 if ( fxScreen ) {
109 drmUnmap( fxScreen->regs.map, fxScreen->regs.size );
110
111 FREE( fxScreen );
112 sPriv->private = NULL;
113 }
114 }
115
116
117 static GLboolean
118 tdfxInitDriver( __DRIscreenPrivate *sPriv )
119 {
120 if ( TDFX_DEBUG & DEBUG_VERBOSE_DRI ) {
121 fprintf( stderr, "%s( %p )\n", __FUNCTION__, (void *)sPriv );
122 }
123
124 if ( !tdfxCreateScreen( sPriv ) ) {
125 tdfxDestroyScreen( sPriv );
126 return GL_FALSE;
127 }
128
129 return GL_TRUE;
130 }
131
132
133 static GLboolean
134 tdfxCreateBuffer( __DRIscreenPrivate *driScrnPriv,
135 __DRIdrawablePrivate *driDrawPriv,
136 const __GLcontextModes *mesaVis,
137 GLboolean isPixmap )
138 {
139 if (isPixmap) {
140 return GL_FALSE; /* not implemented */
141 }
142 else {
143 driDrawPriv->driverPrivate = (void *)
144 _mesa_create_framebuffer( mesaVis,
145 GL_FALSE, /* software depth buffer? */
146 mesaVis->stencilBits > 0,
147 mesaVis->accumRedBits > 0,
148 GL_FALSE /* software alpha channel? */ );
149 return (driDrawPriv->driverPrivate != NULL);
150 }
151 }
152
153
154 static void
155 tdfxDestroyBuffer(__DRIdrawablePrivate *driDrawPriv)
156 {
157 _mesa_destroy_framebuffer((GLframebuffer *) (driDrawPriv->driverPrivate));
158 }
159
160
161 static void
162 tdfxSwapBuffers( __DRIdrawablePrivate *driDrawPriv )
163
164 {
165 GET_CURRENT_CONTEXT(ctx);
166 tdfxContextPtr fxMesa = 0;
167 GLframebuffer *mesaBuffer;
168
169 if ( TDFX_DEBUG & DEBUG_VERBOSE_DRI ) {
170 fprintf( stderr, "%s( %p )\n", __FUNCTION__, (void *)driDrawPriv );
171 }
172
173 mesaBuffer = (GLframebuffer *) driDrawPriv->driverPrivate;
174 if ( !mesaBuffer->Visual.doubleBufferMode )
175 return; /* can't swap a single-buffered window */
176
177 /* If the current context's drawable matches the given drawable
178 * we have to do a glFinish (per the GLX spec).
179 */
180 if ( ctx ) {
181 __DRIdrawablePrivate *curDrawPriv;
182 fxMesa = TDFX_CONTEXT(ctx);
183 curDrawPriv = fxMesa->driContext->driDrawablePriv;
184
185 if ( curDrawPriv == driDrawPriv ) {
186 /* swapping window bound to current context, flush first */
187 _mesa_notifySwapBuffers( ctx );
188 LOCK_HARDWARE( fxMesa );
189 }
190 else {
191 /* find the fxMesa context previously bound to the window */
192 fxMesa = (tdfxContextPtr) driDrawPriv->driContextPriv->driverPrivate;
193 if (!fxMesa)
194 return;
195 LOCK_HARDWARE( fxMesa );
196 fxMesa->Glide.grSstSelect( fxMesa->Glide.Board );
197 printf("SwapBuf SetState 1\n");
198 fxMesa->Glide.grGlideSetState(fxMesa->Glide.State );
199 }
200 }
201
202 #ifdef STATS
203 {
204 int stalls;
205 static int prevStalls = 0;
206
207 stalls = fxMesa->Glide.grFifoGetStalls();
208
209 fprintf( stderr, "%s:\n", __FUNCTION__ );
210 if ( stalls != prevStalls ) {
211 fprintf( stderr, " %d stalls occurred\n",
212 stalls - prevStalls );
213 prevStalls = stalls;
214 }
215 if ( fxMesa && fxMesa->texSwaps ) {
216 fprintf( stderr, " %d texture swaps occurred\n",
217 fxMesa->texSwaps );
218 fxMesa->texSwaps = 0;
219 }
220 }
221 #endif
222
223 if (fxMesa->scissoredClipRects) {
224 /* restore clip rects without scissor box */
225 fxMesa->Glide.grDRIPosition( driDrawPriv->x, driDrawPriv->y,
226 driDrawPriv->w, driDrawPriv->h,
227 driDrawPriv->numClipRects,
228 driDrawPriv->pClipRects );
229 }
230
231 fxMesa->Glide.grDRIBufferSwap( fxMesa->Glide.SwapInterval );
232
233 if (fxMesa->scissoredClipRects) {
234 /* restore clip rects WITH scissor box */
235 fxMesa->Glide.grDRIPosition( driDrawPriv->x, driDrawPriv->y,
236 driDrawPriv->w, driDrawPriv->h,
237 fxMesa->numClipRects, fxMesa->pClipRects );
238 }
239
240
241 #if 0
242 {
243 FxI32 result;
244 do {
245 FxI32 result;
246 fxMesa->Glide.grGet(GR_PENDING_BUFFERSWAPS, 4, &result);
247 } while ( result > fxMesa->maxPendingSwapBuffers );
248 }
249 #endif
250
251 fxMesa->stats.swapBuffer++;
252
253 if (ctx) {
254 if (ctx->DriverCtx != fxMesa) {
255 fxMesa = TDFX_CONTEXT(ctx);
256 fxMesa->Glide.grSstSelect( fxMesa->Glide.Board );
257 printf("SwapBuf SetState 2\n");
258 fxMesa->Glide.grGlideSetState(fxMesa->Glide.State );
259 }
260 UNLOCK_HARDWARE( fxMesa );
261 }
262 }
263
264
265 static const struct __DriverAPIRec tdfxAPI = {
266 .InitDriver = tdfxInitDriver,
267 .DestroyScreen = tdfxDestroyScreen,
268 .CreateContext = tdfxCreateContext,
269 .DestroyContext = tdfxDestroyContext,
270 .CreateBuffer = tdfxCreateBuffer,
271 .DestroyBuffer = tdfxDestroyBuffer,
272 .SwapBuffers = tdfxSwapBuffers,
273 .MakeCurrent = tdfxMakeCurrent,
274 .UnbindContext = tdfxUnbindContext,
275 .GetSwapInfo = NULL,
276 .GetMSC = NULL,
277 .WaitForMSC = NULL,
278 .WaitForSBC = NULL,
279 .SwapBuffersMSC = NULL
280 };
281
282 #ifdef USE_NEW_INTERFACE
283 /*
284 * new interface code, derived from radeon_screen.c
285 * XXX this may still be wrong
286 */
287 static PFNGLXCREATECONTEXTMODES create_context_modes = NULL;
288
289 static __GLcontextModes *tdfxFillInModes(unsigned pixel_bits,
290 unsigned depth_bits,
291 unsigned stencil_bits,
292 GLboolean have_back_buffer)
293 {
294 __GLcontextModes *modes;
295 __GLcontextModes *m;
296 unsigned num_modes;
297 unsigned vis[2] = { GLX_TRUE_COLOR, GLX_DIRECT_COLOR };
298 unsigned deep = (depth_bits > 17);
299 unsigned i, db, depth, accum, stencil;
300
301 /* Right now GLX_SWAP_COPY_OML isn't supported, but it would be easy
302 * enough to add support. Basically, if a context is created with an
303 * fbconfig where the swap method is GLX_SWAP_COPY_OML, pageflipping
304 * will never be used.
305 */
306
307 num_modes = (depth_bits == 16) ? 32 : 16;
308
309 modes = (*create_context_modes)(num_modes, sizeof(__GLcontextModes));
310 m = modes;
311
312 for (i = 0; i <= 1; i++) {
313 for (db = 0; db <= 1; db++) {
314 for (depth = 0; depth <= 1; depth++) {
315 for (accum = 0; accum <= 1; accum++) {
316 for (stencil = 0; stencil <= !deep; stencil++) {
317 if (deep) stencil = depth;
318 m->redBits = deep ? 8 : 5;
319 m->greenBits = deep ? 8 : 6;
320 m->blueBits = deep ? 8 : 5;
321 m->alphaBits = deep ? 8 : 0;
322 m->redMask = deep ?0xFF000000 :0x0000F800;
323 m->greenMask = deep ?0x00FF0000 :0x000007E0;
324 m->blueMask = deep ?0x0000FF00 :0x0000001F;
325 m->alphaMask = deep ? 0x000000FF : 0;
326 m->rgbBits = m->redBits + m->greenBits +
327 m->blueBits + m->alphaBits;
328 m->accumRedBits = accum ? 16 : 0;
329 m->accumGreenBits = accum ? 16 : 0;
330 m->accumBlueBits = accum ? 16 : 0;
331 m->accumAlphaBits = accum ? 16 : 0;
332 m->stencilBits = stencil ? 8 : 0;
333 m->depthBits = deep
334 ? (depth ? 24 : 0)
335 : (depth ? 0 : depth_bits);
336 m->visualType = i ? GLX_TRUE_COLOR
337 : GLX_DIRECT_COLOR;
338 m->renderType = GLX_RGBA_BIT;
339 m->drawableType = GLX_WINDOW_BIT;
340 m->rgbMode = GL_TRUE;
341 m->doubleBufferMode = db ? GL_TRUE : GL_FALSE;
342 if (db)
343 m->swapMethod = GLX_SWAP_UNDEFINED_OML;
344 m->visualRating = ((stencil && !deep) || accum)
345 ? GLX_SLOW_CONFIG
346 : GLX_NONE;
347 m = m->next;
348 if (deep) stencil = 0;
349 }
350 }
351 }
352 }
353 }
354
355 return modes;
356 }
357
358 /**
359 * This is the bootstrap function for the driver. libGL supplies all of the
360 * requisite information about the system, and the driver initializes itself.
361 * This routine also fills in the linked list pointed to by \c driver_modes
362 * with the \c __GLcontextModes that the driver can support for windows or
363 * pbuffers.
364 *
365 * \return A pointer to a \c __DRIscreenPrivate on success, or \c NULL on
366 * failure.
367 */
368 void * __driCreateNewScreen( __DRInativeDisplay *dpy, int scrn, __DRIscreen *psc,
369 const __GLcontextModes * modes,
370 const __DRIversion * ddx_version,
371 const __DRIversion * dri_version,
372 const __DRIversion * drm_version,
373 const __DRIframebuffer * frame_buffer,
374 drmAddress pSAREA, int fd,
375 int internal_api_version,
376 __GLcontextModes ** driver_modes )
377 {
378 __DRIscreenPrivate *psp;
379 static const __DRIversion ddx_expected = { 1, 0, 0 };
380 static const __DRIversion dri_expected = { 4, 0, 0 };
381 static const __DRIversion drm_expected = { 1, 0, 0 };
382
383 if ( ! driCheckDriDdxDrmVersions2( "tdfx",
384 dri_version, & dri_expected,
385 ddx_version, & ddx_expected,
386 drm_version, & drm_expected ) ) {
387 return NULL;
388 }
389
390 psp = __driUtilCreateNewScreen(dpy, scrn, psc, NULL,
391 ddx_version, dri_version, drm_version,
392 frame_buffer, pSAREA, fd,
393 internal_api_version, &tdfxAPI);
394
395 create_context_modes = (PFNGLXCREATECONTEXTMODES)
396 glXGetProcAddress((const GLubyte *)"__glXCreateContextModes");
397
398 if (create_context_modes != NULL) {
399 /* divined from tdfx_dri.c, sketchy */
400 TDFXDRIPtr dri_priv = (TDFXDRIPtr) psp->pDevPriv;
401 int bpp = (dri_priv->cpp > 2) ? 24 : 16;
402
403 /* XXX i wish it was like this */
404 /* bpp = dri_priv->bpp */
405
406 *driver_modes = tdfxFillInModes(bpp, (bpp == 16) ? 16 : 24,
407 (bpp == 16) ? 0 : 8,
408 (dri_priv->backOffset!=dri_priv->depthOffset));
409 }
410
411 return (void *)psp;
412 }
413 #endif /* USE_NEW_INTERFACE */
414
415
416 /*
417 * This is the bootstrap function for the driver.
418 * The __driCreateScreen name is the symbol that libGL.so fetches.
419 * Return: pointer to a __DRIscreenPrivate.
420 */
421 #if !defined(DRI_NEW_INTERFACE_ONLY)
422 #ifndef _SOLO
423 void *__driCreateScreen(Display *dpy, int scrn, __DRIscreen *psc,
424 int numConfigs, __GLXvisualConfig *config)
425 {
426 __DRIscreenPrivate *psp;
427 psp = __driUtilCreateScreen(dpy, scrn, psc, numConfigs, config, &tdfxAPI);
428 return (void *) psp;
429 }
430 #else
431 void *__driCreateScreen(struct DRIDriverRec *driver,
432 struct DRIDriverContextRec *driverContext)
433 {
434 __DRIscreenPrivate *psp;
435 psp = __driUtilCreateScreen(driver, driverContext, &tdfxAPI);
436 return (void *) psp;
437 }
438 #endif
439 #endif /* !defined(DRI_NEW_INTERFACE_ONLY) */