1 /* -*- mode: c; c-basic-offset: 3 -*-
3 * Copyright 2000 VA Linux Systems Inc., Fremont, California.
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:
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
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
26 /* $XFree86: xc/lib/GL/mesa/src/drv/tdfx/tdfx_screen.c,v 1.3 2002/02/22 21:45:03 dawes Exp $ */
30 * Gareth Hughes <gareth@valinux.com>, 29 Sep - 1 Oct 2000
33 * Gareth Hughes <gareth@valinux.com>
38 #include "tdfx_context.h"
39 #include "tdfx_lock.h"
41 #include "tdfx_span.h"
42 #include "tdfx_tris.h"
44 #include "framebuffer.h"
45 #include "renderbuffer.h"
51 char *prevLockFile
= 0;
59 PUBLIC
const char __driConfigOptions
[] =
61 DRI_CONF_SECTION_DEBUG
62 DRI_CONF_NO_RAST(false)
66 static const __DRIextension
*tdfxExtensions
[] = {
67 &driReadDrawableExtension
,
70 static const GLuint __driNConfigOptions
= 1;
72 extern const struct dri_extension card_extensions
[];
73 extern const struct dri_extension napalm_extensions
[];
76 tdfxCreateScreen( __DRIscreenPrivate
*sPriv
)
78 tdfxScreenPrivate
*fxScreen
;
79 TDFXDRIPtr fxDRIPriv
= (TDFXDRIPtr
) sPriv
->pDevPriv
;
81 if (sPriv
->devPrivSize
!= sizeof(TDFXDRIRec
)) {
82 fprintf(stderr
,"\nERROR! sizeof(TDFXDRIRec) does not match passed size from device driver\n");
86 /* Allocate the private area */
87 fxScreen
= (tdfxScreenPrivate
*) CALLOC( sizeof(tdfxScreenPrivate
) );
91 /* parse information in __driConfigOptions */
92 driParseOptionInfo (&fxScreen
->optionCache
,
93 __driConfigOptions
, __driNConfigOptions
);
95 fxScreen
->driScrnPriv
= sPriv
;
96 sPriv
->private = (void *) fxScreen
;
98 fxScreen
->regs
.handle
= fxDRIPriv
->regs
;
99 fxScreen
->regs
.size
= fxDRIPriv
->regsSize
;
100 fxScreen
->deviceID
= fxDRIPriv
->deviceID
;
101 fxScreen
->width
= fxDRIPriv
->width
;
102 fxScreen
->height
= fxDRIPriv
->height
;
103 fxScreen
->mem
= fxDRIPriv
->mem
;
104 fxScreen
->cpp
= fxDRIPriv
->cpp
;
105 fxScreen
->stride
= fxDRIPriv
->stride
;
106 fxScreen
->fifoOffset
= fxDRIPriv
->fifoOffset
;
107 fxScreen
->fifoSize
= fxDRIPriv
->fifoSize
;
108 fxScreen
->fbOffset
= fxDRIPriv
->fbOffset
;
109 fxScreen
->backOffset
= fxDRIPriv
->backOffset
;
110 fxScreen
->depthOffset
= fxDRIPriv
->depthOffset
;
111 fxScreen
->textureOffset
= fxDRIPriv
->textureOffset
;
112 fxScreen
->textureSize
= fxDRIPriv
->textureSize
;
113 fxScreen
->sarea_priv_offset
= fxDRIPriv
->sarea_priv_offset
;
115 if ( drmMap( sPriv
->fd
, fxScreen
->regs
.handle
,
116 fxScreen
->regs
.size
, &fxScreen
->regs
.map
) ) {
120 sPriv
->extensions
= tdfxExtensions
;
127 tdfxDestroyScreen( __DRIscreenPrivate
*sPriv
)
129 tdfxScreenPrivate
*fxScreen
= (tdfxScreenPrivate
*) sPriv
->private;
134 drmUnmap( fxScreen
->regs
.map
, fxScreen
->regs
.size
);
136 /* free all option information */
137 driDestroyOptionInfo (&fxScreen
->optionCache
);
140 sPriv
->private = NULL
;
145 tdfxInitDriver( __DRIscreenPrivate
*sPriv
)
147 if ( TDFX_DEBUG
& DEBUG_VERBOSE_DRI
) {
148 fprintf( stderr
, "%s( %p )\n", __FUNCTION__
, (void *)sPriv
);
151 if ( !tdfxCreateScreen( sPriv
) ) {
152 tdfxDestroyScreen( sPriv
);
161 tdfxCreateBuffer( __DRIscreenPrivate
*driScrnPriv
,
162 __DRIdrawablePrivate
*driDrawPriv
,
163 const __GLcontextModes
*mesaVis
,
166 tdfxScreenPrivate
*screen
= (tdfxScreenPrivate
*) driScrnPriv
->private;
169 return GL_FALSE
; /* not implemented */
172 struct gl_framebuffer
*fb
= _mesa_create_framebuffer(mesaVis
);
175 driRenderbuffer
*frontRb
176 = driNewRenderbuffer(GL_RGBA
, NULL
, screen
->cpp
,
177 screen
->fbOffset
, screen
->width
, driDrawPriv
);
178 tdfxSetSpanFunctions(frontRb
, mesaVis
);
179 _mesa_add_renderbuffer(fb
, BUFFER_FRONT_LEFT
, &frontRb
->Base
);
182 if (mesaVis
->doubleBufferMode
) {
183 driRenderbuffer
*backRb
184 = driNewRenderbuffer(GL_RGBA
, NULL
, screen
->cpp
,
185 screen
->backOffset
, screen
->width
,
187 tdfxSetSpanFunctions(backRb
, mesaVis
);
188 _mesa_add_renderbuffer(fb
, BUFFER_BACK_LEFT
, &backRb
->Base
);
189 backRb
->backBuffer
= GL_TRUE
;
192 if (mesaVis
->depthBits
== 16) {
193 driRenderbuffer
*depthRb
194 = driNewRenderbuffer(GL_DEPTH_COMPONENT16
, NULL
, screen
->cpp
,
195 screen
->depthOffset
, screen
->width
,
197 tdfxSetSpanFunctions(depthRb
, mesaVis
);
198 _mesa_add_renderbuffer(fb
, BUFFER_DEPTH
, &depthRb
->Base
);
200 else if (mesaVis
->depthBits
== 24) {
201 driRenderbuffer
*depthRb
202 = driNewRenderbuffer(GL_DEPTH_COMPONENT24
, NULL
, screen
->cpp
,
203 screen
->depthOffset
, screen
->width
,
205 tdfxSetSpanFunctions(depthRb
, mesaVis
);
206 _mesa_add_renderbuffer(fb
, BUFFER_DEPTH
, &depthRb
->Base
);
209 if (mesaVis
->stencilBits
> 0) {
210 driRenderbuffer
*stencilRb
211 = driNewRenderbuffer(GL_STENCIL_INDEX8_EXT
, NULL
, screen
->cpp
,
212 screen
->depthOffset
, screen
->width
,
214 tdfxSetSpanFunctions(stencilRb
, mesaVis
);
215 _mesa_add_renderbuffer(fb
, BUFFER_STENCIL
, &stencilRb
->Base
);
218 _mesa_add_soft_renderbuffers(fb
,
219 GL_FALSE
, /* color */
220 GL_FALSE
, /* depth */
221 GL_FALSE
, /*swStencil,*/
222 mesaVis
->accumRedBits
> 0,
223 GL_FALSE
, /* alpha */
225 driDrawPriv
->driverPrivate
= (void *) fb
;
227 return (driDrawPriv
->driverPrivate
!= NULL
);
233 tdfxDestroyBuffer(__DRIdrawablePrivate
*driDrawPriv
)
235 _mesa_unreference_framebuffer((GLframebuffer
**)(&(driDrawPriv
->driverPrivate
)));
240 tdfxSwapBuffers( __DRIdrawablePrivate
*driDrawPriv
)
243 GET_CURRENT_CONTEXT(ctx
);
244 tdfxContextPtr fxMesa
= 0;
245 GLframebuffer
*mesaBuffer
;
247 if ( TDFX_DEBUG
& DEBUG_VERBOSE_DRI
) {
248 fprintf( stderr
, "%s( %p )\n", __FUNCTION__
, (void *)driDrawPriv
);
251 mesaBuffer
= (GLframebuffer
*) driDrawPriv
->driverPrivate
;
252 if ( !mesaBuffer
->Visual
.doubleBufferMode
)
253 return; /* can't swap a single-buffered window */
255 /* If the current context's drawable matches the given drawable
256 * we have to do a glFinish (per the GLX spec).
259 __DRIdrawablePrivate
*curDrawPriv
;
260 fxMesa
= TDFX_CONTEXT(ctx
);
261 curDrawPriv
= fxMesa
->driContext
->driDrawablePriv
;
263 if ( curDrawPriv
== driDrawPriv
) {
264 /* swapping window bound to current context, flush first */
265 _mesa_notifySwapBuffers( ctx
);
266 LOCK_HARDWARE( fxMesa
);
269 /* find the fxMesa context previously bound to the window */
270 fxMesa
= (tdfxContextPtr
) driDrawPriv
->driContextPriv
->driverPrivate
;
273 LOCK_HARDWARE( fxMesa
);
274 fxMesa
->Glide
.grSstSelect( fxMesa
->Glide
.Board
);
276 printf("SwapBuf SetState 1\n");
278 fxMesa
->Glide
.grGlideSetState(fxMesa
->Glide
.State
);
285 static int prevStalls
= 0;
287 stalls
= fxMesa
->Glide
.grFifoGetStalls();
289 fprintf( stderr
, "%s:\n", __FUNCTION__
);
290 if ( stalls
!= prevStalls
) {
291 fprintf( stderr
, " %d stalls occurred\n",
292 stalls
- prevStalls
);
295 if ( fxMesa
&& fxMesa
->texSwaps
) {
296 fprintf( stderr
, " %d texture swaps occurred\n",
298 fxMesa
->texSwaps
= 0;
303 if (fxMesa
->scissoredClipRects
) {
304 /* restore clip rects without scissor box */
305 fxMesa
->Glide
.grDRIPosition( driDrawPriv
->x
, driDrawPriv
->y
,
306 driDrawPriv
->w
, driDrawPriv
->h
,
307 driDrawPriv
->numClipRects
,
308 driDrawPriv
->pClipRects
);
311 fxMesa
->Glide
.grDRIBufferSwap( fxMesa
->Glide
.SwapInterval
);
313 if (fxMesa
->scissoredClipRects
) {
314 /* restore clip rects WITH scissor box */
315 fxMesa
->Glide
.grDRIPosition( driDrawPriv
->x
, driDrawPriv
->y
,
316 driDrawPriv
->w
, driDrawPriv
->h
,
317 fxMesa
->numClipRects
, fxMesa
->pClipRects
);
326 fxMesa
->Glide
.grGet(GR_PENDING_BUFFERSWAPS
, 4, &result
);
327 } while ( result
> fxMesa
->maxPendingSwapBuffers
);
331 fxMesa
->stats
.swapBuffer
++;
334 if (ctx
->DriverCtx
!= fxMesa
) {
335 fxMesa
= TDFX_CONTEXT(ctx
);
336 fxMesa
->Glide
.grSstSelect( fxMesa
->Glide
.Board
);
338 printf("SwapBuf SetState 2\n");
340 fxMesa
->Glide
.grGlideSetState(fxMesa
->Glide
.State
);
342 UNLOCK_HARDWARE( fxMesa
);
347 static const struct __DriverAPIRec tdfxAPI
= {
348 .DestroyScreen
= tdfxDestroyScreen
,
349 .CreateContext
= tdfxCreateContext
,
350 .DestroyContext
= tdfxDestroyContext
,
351 .CreateBuffer
= tdfxCreateBuffer
,
352 .DestroyBuffer
= tdfxDestroyBuffer
,
353 .SwapBuffers
= tdfxSwapBuffers
,
354 .MakeCurrent
= tdfxMakeCurrent
,
355 .UnbindContext
= tdfxUnbindContext
,
357 .GetDrawableMSC
= NULL
,
360 .SwapBuffersMSC
= NULL
364 static __GLcontextModes
*tdfxFillInModes(unsigned pixel_bits
,
366 unsigned stencil_bits
,
367 GLboolean have_back_buffer
)
369 __GLcontextModes
*modes
;
372 unsigned vis
[2] = { GLX_TRUE_COLOR
, GLX_DIRECT_COLOR
};
373 unsigned deep
= (depth_bits
> 17);
374 unsigned i
, db
, depth
, accum
, stencil
;
376 /* Right now GLX_SWAP_COPY_OML isn't supported, but it would be easy
377 * enough to add support. Basically, if a context is created with an
378 * fbconfig where the swap method is GLX_SWAP_COPY_OML, pageflipping
379 * will never be used.
382 num_modes
= (depth_bits
== 16) ? 32 : 16;
384 modes
= (*dri_interface
->createContextModes
)(num_modes
, sizeof(__GLcontextModes
));
387 for (i
= 0; i
<= 1; i
++) {
388 for (db
= 0; db
<= 1; db
++) {
389 for (depth
= 0; depth
<= 1; depth
++) {
390 for (accum
= 0; accum
<= 1; accum
++) {
391 for (stencil
= 0; stencil
<= !deep
; stencil
++) {
392 if (deep
) stencil
= depth
;
393 m
->redBits
= deep
? 8 : 5;
394 m
->greenBits
= deep
? 8 : 6;
395 m
->blueBits
= deep
? 8 : 5;
396 m
->alphaBits
= deep
? 8 : 0;
397 m
->redMask
= deep
?0xFF000000 :0x0000F800;
398 m
->greenMask
= deep
?0x00FF0000 :0x000007E0;
399 m
->blueMask
= deep
?0x0000FF00 :0x0000001F;
400 m
->alphaMask
= deep
? 0x000000FF : 0;
401 m
->rgbBits
= m
->redBits
+ m
->greenBits
+
402 m
->blueBits
+ m
->alphaBits
;
403 m
->accumRedBits
= accum
? 16 : 0;
404 m
->accumGreenBits
= accum
? 16 : 0;
405 m
->accumBlueBits
= accum
? 16 : 0;
406 m
->accumAlphaBits
= (accum
&& deep
) ? 16 : 0;
407 m
->stencilBits
= stencil
? 8 : 0;
410 : (depth
? 0 : depth_bits
);
411 m
->visualType
= vis
[i
];
412 m
->renderType
= GLX_RGBA_BIT
;
413 m
->drawableType
= GLX_WINDOW_BIT
;
414 m
->rgbMode
= GL_TRUE
;
415 m
->doubleBufferMode
= db
? GL_TRUE
: GL_FALSE
;
417 m
->swapMethod
= GLX_SWAP_UNDEFINED_OML
;
418 m
->visualRating
= ((stencil
&& !deep
) || accum
)
422 if (deep
) stencil
= 0;
433 * This is the driver specific part of the createNewScreen entry point.
435 * \todo maybe fold this into intelInitDriver
437 * \return the __GLcontextModes supported by this driver
439 __GLcontextModes
*__driDriverInitScreen(__DRIscreenPrivate
*psp
)
441 static const __DRIversion ddx_expected
= { 1, 1, 0 };
442 static const __DRIversion dri_expected
= { 4, 0, 0 };
443 static const __DRIversion drm_expected
= { 1, 0, 0 };
445 /* divined from tdfx_dri.c, sketchy */
446 TDFXDRIPtr dri_priv
= (TDFXDRIPtr
) psp
->pDevPriv
;
448 /* XXX i wish it was like this */
449 /* bpp = dri_priv->bpp */
450 int bpp
= (dri_priv
->cpp
> 2) ? 24 : 16;
452 if ( ! driCheckDriDdxDrmVersions2( "tdfx",
453 &psp
->dri_version
, & dri_expected
,
454 &psp
->ddx_version
, & ddx_expected
,
455 &psp
->drm_version
, & drm_expected
) )
458 psp
->DriverAPI
= tdfxAPI
;
460 /* Calling driInitExtensions here, with a NULL context pointer,
461 * does not actually enable the extensions. It just makes sure
462 * that all the dispatch offsets for all the extensions that
463 * *might* be enables are known. This is needed because the
464 * dispatch offsets need to be known when _mesa_context_create is
465 * called, but we can't enable the extensions until we have a
468 * Hello chicken. Hello egg. How are you two today?
470 driInitExtensions( NULL
, card_extensions
, GL_FALSE
);
471 driInitExtensions( NULL
, napalm_extensions
, GL_FALSE
);
473 if (!tdfxInitDriver(psp
))
476 return tdfxFillInModes(bpp
, (bpp
== 16) ? 16 : 24,
478 (dri_priv
->backOffset
!=dri_priv
->depthOffset
));