1 /* $XFree86: xc/lib/GL/mesa/src/drv/ffb/ffb_xmesa.c,v 1.4 2002/02/22 21:32:59 dawes Exp $
3 * GLX Hardware Device Driver for Sun Creator/Creator3D
4 * Copyright (C) 2000, 2001 David S. Miller
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * DAVID MILLER, OR ANY OTHER CONTRIBUTORS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
22 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 * David S. Miller <davem@redhat.com>
28 #ifdef GLX_DIRECT_RENDERING
30 #include "ffb_xmesa.h"
33 #include "simple_list.h"
36 #include "swrast/swrast.h"
37 #include "swrast_setup/swrast_setup.h"
39 #include "tnl/t_pipeline.h"
40 #include "array_cache/acache.h"
41 #include "drivers/common/driverfuncs.h"
43 #include "ffb_context.h"
46 #include "ffb_depth.h"
47 #include "ffb_stencil.h"
48 #include "ffb_clear.h"
51 #include "ffb_lines.h"
52 #include "ffb_points.h"
53 #include "ffb_state.h"
56 #include "ffb_vtxfmt.h"
57 #include "ffb_bitmap.h"
59 #include "drm_sarea.h"
62 ffbInitDriver(__DRIscreenPrivate
*sPriv
)
64 ffbScreenPrivate
*ffbScreen
;
65 FFBDRIPtr gDRIPriv
= (FFBDRIPtr
) sPriv
->pDevPriv
;
67 if (getenv("LIBGL_FORCE_XSERVER"))
70 /* Allocate the private area. */
71 ffbScreen
= (ffbScreenPrivate
*) MALLOC(sizeof(ffbScreenPrivate
));
75 /* Map FBC registers. */
79 &gDRIPriv
->mFbcRegs
)) {
83 ffbScreen
->regs
= (ffb_fbcPtr
) gDRIPriv
->mFbcRegs
;
85 /* Map ramdac registers. */
89 &gDRIPriv
->mDacRegs
)) {
90 drmUnmap(gDRIPriv
->mFbcRegs
, gDRIPriv
->sFbcRegs
);
94 ffbScreen
->dac
= (ffb_dacPtr
) gDRIPriv
->mDacRegs
;
96 /* Map "Smart" framebuffer views. */
100 &gDRIPriv
->mSfb8r
)) {
101 drmUnmap(gDRIPriv
->mFbcRegs
, gDRIPriv
->sFbcRegs
);
102 drmUnmap(gDRIPriv
->mDacRegs
, gDRIPriv
->sDacRegs
);
106 ffbScreen
->sfb8r
= (volatile char *) gDRIPriv
->mSfb8r
;
108 if (drmMap(sPriv
->fd
,
111 &gDRIPriv
->mSfb32
)) {
112 drmUnmap(gDRIPriv
->mFbcRegs
, gDRIPriv
->sFbcRegs
);
113 drmUnmap(gDRIPriv
->mDacRegs
, gDRIPriv
->sDacRegs
);
114 drmUnmap(gDRIPriv
->mSfb8r
, gDRIPriv
->sSfb8r
);
118 ffbScreen
->sfb32
= (volatile char *) gDRIPriv
->mSfb32
;
120 if (drmMap(sPriv
->fd
,
123 &gDRIPriv
->mSfb64
)) {
124 drmUnmap(gDRIPriv
->mFbcRegs
, gDRIPriv
->sFbcRegs
);
125 drmUnmap(gDRIPriv
->mDacRegs
, gDRIPriv
->sDacRegs
);
126 drmUnmap(gDRIPriv
->mSfb8r
, gDRIPriv
->sSfb8r
);
127 drmUnmap(gDRIPriv
->mSfb32
, gDRIPriv
->sSfb32
);
131 ffbScreen
->sfb64
= (volatile char *) gDRIPriv
->mSfb64
;
133 ffbScreen
->fifo_cache
= 0;
134 ffbScreen
->rp_active
= 0;
136 ffbScreen
->sPriv
= sPriv
;
137 sPriv
->private = (void *) ffbScreen
;
140 ffbDDPointfuncInit();
147 ffbDestroyScreen(__DRIscreenPrivate
*sPriv
)
149 ffbScreenPrivate
*ffbScreen
= sPriv
->private;
150 FFBDRIPtr gDRIPriv
= (FFBDRIPtr
) sPriv
->pDevPriv
;
152 drmUnmap(gDRIPriv
->mFbcRegs
, gDRIPriv
->sFbcRegs
);
153 drmUnmap(gDRIPriv
->mDacRegs
, gDRIPriv
->sDacRegs
);
154 drmUnmap(gDRIPriv
->mSfb8r
, gDRIPriv
->sSfb8r
);
155 drmUnmap(gDRIPriv
->mSfb32
, gDRIPriv
->sSfb32
);
156 drmUnmap(gDRIPriv
->mSfb64
, gDRIPriv
->sSfb64
);
161 static const struct tnl_pipeline_stage
*ffb_pipeline
[] = {
162 &_tnl_vertex_transform_stage
,
163 &_tnl_normal_transform_stage
,
164 &_tnl_lighting_stage
,
165 /* REMOVE: fog coord stage */
167 &_tnl_texture_transform_stage
,
168 /* REMOVE: point attenuation stage */
173 /* Create and initialize the Mesa and driver specific context data */
175 ffbCreateContext(const __GLcontextModes
*mesaVis
,
176 __DRIcontextPrivate
*driContextPriv
,
177 void *sharedContextPrivate
)
180 GLcontext
*ctx
, *shareCtx
;
181 __DRIscreenPrivate
*sPriv
;
182 ffbScreenPrivate
*ffbScreen
;
184 struct dd_function_table functions
;
186 /* Allocate ffb context */
187 fmesa
= (ffbContextPtr
) CALLOC(sizeof(ffbContextRec
));
191 _mesa_init_driver_functions(&functions
);
193 /* Allocate Mesa context */
194 if (sharedContextPrivate
)
195 shareCtx
= ((ffbContextPtr
) sharedContextPrivate
)->glCtx
;
198 fmesa
->glCtx
= _mesa_create_context(mesaVis
, shareCtx
,
204 driContextPriv
->driverPrivate
= fmesa
;
207 sPriv
= driContextPriv
->driScreenPriv
;
208 ffbScreen
= (ffbScreenPrivate
*) sPriv
->private;
211 fmesa
->hHWContext
= driContextPriv
->hHWContext
;
212 fmesa
->driFd
= sPriv
->fd
;
213 fmesa
->driHwLock
= &sPriv
->pSAREA
->lock
;
215 fmesa
->ffbScreen
= ffbScreen
;
216 fmesa
->driScreen
= sPriv
;
217 fmesa
->ffb_sarea
= FFB_DRISHARE(sPriv
->pSAREA
);
219 /* Register and framebuffer hw pointers. */
220 fmesa
->regs
= ffbScreen
->regs
;
221 fmesa
->sfb32
= ffbScreen
->sfb32
;
223 ffbDDInitContextHwState(ctx
);
225 /* Default clear and depth colors. */
227 GLubyte r
= (GLint
) (ctx
->Color
.ClearColor
[0] * 255.0F
);
228 GLubyte g
= (GLint
) (ctx
->Color
.ClearColor
[1] * 255.0F
);
229 GLubyte b
= (GLint
) (ctx
->Color
.ClearColor
[2] * 255.0F
);
231 fmesa
->clear_pixel
= ((r
<< 0) |
235 fmesa
->clear_depth
= Z_FROM_MESA(ctx
->Depth
.Clear
* 4294967295.0f
);
236 fmesa
->clear_stencil
= ctx
->Stencil
.Clear
& 0xf;
238 /* No wide points. */
239 ctx
->Const
.MinPointSize
= 1.0;
240 ctx
->Const
.MinPointSizeAA
= 1.0;
241 ctx
->Const
.MaxPointSize
= 1.0;
242 ctx
->Const
.MaxPointSizeAA
= 1.0;
244 /* Disable wide lines as we can't antialias them correctly in
247 ctx
->Const
.MinLineWidth
= 1.0;
248 ctx
->Const
.MinLineWidthAA
= 1.0;
249 ctx
->Const
.MaxLineWidth
= 1.0;
250 ctx
->Const
.MaxLineWidthAA
= 1.0;
251 ctx
->Const
.LineWidthGranularity
= 1.0;
253 /* Instead of having GCC emit these constants a zillion times
254 * everywhere in the driver, put them here.
256 fmesa
->ffb_2_30_fixed_scale
= __FFB_2_30_FIXED_SCALE
;
257 fmesa
->ffb_one_over_2_30_fixed_scale
= (1.0 / __FFB_2_30_FIXED_SCALE
);
258 fmesa
->ffb_16_16_fixed_scale
= __FFB_16_16_FIXED_SCALE
;
259 fmesa
->ffb_one_over_16_16_fixed_scale
= (1.0 / __FFB_16_16_FIXED_SCALE
);
260 fmesa
->ffb_ubyte_color_scale
= 255.0f
;
261 fmesa
->ffb_zero
= 0.0f
;
263 fmesa
->debugFallbacks
= GL_FALSE
;
264 debug
= getenv("LIBGL_DEBUG");
265 if (debug
&& strstr(debug
, "fallbacks"))
266 fmesa
->debugFallbacks
= GL_TRUE
;
268 /* Initialize the software rasterizer and helper modules. */
269 _swrast_CreateContext( ctx
);
270 _ac_CreateContext( ctx
);
271 _tnl_CreateContext( ctx
);
272 _swsetup_CreateContext( ctx
);
274 /* All of this need only be done once for a new context. */
275 /* XXX these should be moved right after the
276 * _mesa_init_driver_functions() call above.
278 ffbDDExtensionsInit(ctx
);
279 ffbDDInitDriverFuncs(ctx
);
280 ffbDDInitStateFuncs(ctx
);
281 ffbDDInitSpanFuncs(ctx
);
282 ffbDDInitDepthFuncs(ctx
);
283 ffbDDInitStencilFuncs(ctx
);
284 ffbDDInitRenderFuncs(ctx
);
285 /*ffbDDInitTexFuncs(ctx); not needed */
286 ffbDDInitBitmapFuncs(ctx
);
290 ffbInitTnlModule(ctx
);
293 _tnl_destroy_pipeline(ctx
);
294 _tnl_install_pipeline(ctx
, ffb_pipeline
);
300 ffbDestroyContext(__DRIcontextPrivate
*driContextPriv
)
302 ffbContextPtr fmesa
= (ffbContextPtr
) driContextPriv
->driverPrivate
;
305 ffbFreeVB(fmesa
->glCtx
);
307 _swsetup_DestroyContext( fmesa
->glCtx
);
308 _tnl_DestroyContext( fmesa
->glCtx
);
309 _ac_DestroyContext( fmesa
->glCtx
);
310 _swrast_DestroyContext( fmesa
->glCtx
);
312 /* free the Mesa context */
313 fmesa
->glCtx
->DriverCtx
= NULL
;
314 _mesa_destroy_context(fmesa
->glCtx
);
320 /* Create and initialize the Mesa and driver specific pixmap buffer data */
322 ffbCreateBuffer(__DRIscreenPrivate
*driScrnPriv
,
323 __DRIdrawablePrivate
*driDrawPriv
,
324 const __GLcontextModes
*mesaVis
,
328 return GL_FALSE
; /* not implemented */
331 driDrawPriv
->driverPrivate
= (void *)
332 _mesa_create_framebuffer(mesaVis
,
333 GL_FALSE
, /* software depth buffer? */
334 mesaVis
->stencilBits
> 0,
335 mesaVis
->accumRedBits
> 0,
336 mesaVis
->alphaBits
> 0);
337 return (driDrawPriv
->driverPrivate
!= NULL
);
343 ffbDestroyBuffer(__DRIdrawablePrivate
*driDrawPriv
)
345 _mesa_destroy_framebuffer((GLframebuffer
*) (driDrawPriv
->driverPrivate
));
349 #define USE_FAST_SWAP
352 ffbSwapBuffers( __DRIdrawablePrivate
*dPriv
)
354 ffbContextPtr fmesa
= (ffbContextPtr
) dPriv
->driContextPriv
->driverPrivate
;
355 unsigned int fbc
, wid
, wid_reg_val
, dac_db_bit
;
356 unsigned int shadow_dac_addr
, active_dac_addr
;
361 fmesa
->glCtx
->Visual
.doubleBufferMode
== 0)
364 /* Flush pending rendering commands */
365 _mesa_notifySwapBuffers(fmesa
->glCtx
);
368 dac
= fmesa
->ffbScreen
->dac
;
373 /* Swap the buffer we render into and read pixels from. */
374 fmesa
->back_buffer
^= 1;
376 /* If we are writing into both buffers, don't mess with
379 if ((fbc
& FFB_FBC_WB_AB
) != FFB_FBC_WB_AB
) {
380 if ((fbc
& FFB_FBC_WB_A
) != 0)
381 fbc
= (fbc
& ~FFB_FBC_WB_A
) | FFB_FBC_WB_B
;
383 fbc
= (fbc
& ~FFB_FBC_WB_B
) | FFB_FBC_WB_A
;
386 /* But either way, we must flip the read buffer setting. */
387 if ((fbc
& FFB_FBC_RB_A
) != 0)
388 fbc
= (fbc
& ~FFB_FBC_RB_A
) | FFB_FBC_RB_B
;
390 fbc
= (fbc
& ~FFB_FBC_RB_B
) | FFB_FBC_RB_A
;
392 LOCK_HARDWARE(fmesa
);
394 if (fmesa
->fbc
!= fbc
) {
396 ffb
->fbc
= fmesa
->fbc
= fbc
;
397 fmesa
->ffbScreen
->rp_active
= 1;
400 /* And swap the buffer displayed in the WID. */
401 if (fmesa
->ffb_sarea
->flags
& FFB_DRI_PAC1
) {
402 shadow_dac_addr
= FFBDAC_PAC1_SPWLUT(wid
);
403 active_dac_addr
= FFBDAC_PAC1_APWLUT(wid
);
404 dac_db_bit
= FFBDAC_PAC1_WLUT_DB
;
406 shadow_dac_addr
= FFBDAC_PAC2_SPWLUT(wid
);
407 active_dac_addr
= FFBDAC_PAC2_APWLUT(wid
);
408 dac_db_bit
= FFBDAC_PAC2_WLUT_DB
;
413 wid_reg_val
= DACCFG_READ(dac
, active_dac_addr
);
414 if (fmesa
->back_buffer
== 0)
415 wid_reg_val
|= dac_db_bit
;
417 wid_reg_val
&= ~dac_db_bit
;
419 DACCFG_WRITE(dac
, active_dac_addr
, wid_reg_val
);
421 DACCFG_WRITE(dac
, shadow_dac_addr
, wid_reg_val
);
423 /* Schedule the window transfer. */
424 DACCFG_WRITE(dac
, FFBDAC_CFG_WTCTRL
,
425 (FFBDAC_CFG_WTCTRL_TCMD
| FFBDAC_CFG_WTCTRL_TE
));
430 unsigned int wtctrl
= DACCFG_READ(dac
, FFBDAC_CFG_WTCTRL
);
432 if ((wtctrl
& FFBDAC_CFG_WTCTRL_DS
) == 0)
438 UNLOCK_HARDWARE(fmesa
);
441 static void ffb_init_wid(ffbContextPtr fmesa
, unsigned int wid
)
443 ffb_dacPtr dac
= fmesa
->ffbScreen
->dac
;
444 unsigned int wid_reg_val
, dac_db_bit
, active_dac_addr
;
445 unsigned int shadow_dac_addr
;
447 if (fmesa
->ffb_sarea
->flags
& FFB_DRI_PAC1
) {
448 shadow_dac_addr
= FFBDAC_PAC1_SPWLUT(wid
);
449 active_dac_addr
= FFBDAC_PAC1_APWLUT(wid
);
450 dac_db_bit
= FFBDAC_PAC1_WLUT_DB
;
452 shadow_dac_addr
= FFBDAC_PAC2_SPWLUT(wid
);
453 active_dac_addr
= FFBDAC_PAC2_APWLUT(wid
);
454 dac_db_bit
= FFBDAC_PAC2_WLUT_DB
;
457 wid_reg_val
= DACCFG_READ(dac
, active_dac_addr
);
458 wid_reg_val
&= ~dac_db_bit
;
460 DACCFG_WRITE(dac
, active_dac_addr
, wid_reg_val
);
462 DACCFG_WRITE(dac
, shadow_dac_addr
, wid_reg_val
);
464 /* Schedule the window transfer. */
465 DACCFG_WRITE(dac
, FFBDAC_CFG_WTCTRL
,
466 (FFBDAC_CFG_WTCTRL_TCMD
| FFBDAC_CFG_WTCTRL_TE
));
471 unsigned int wtctrl
= DACCFG_READ(dac
, FFBDAC_CFG_WTCTRL
);
473 if ((wtctrl
& FFBDAC_CFG_WTCTRL_DS
) == 0)
480 /* Force the context `c' to be the current context and associate with it
483 ffbMakeCurrent(__DRIcontextPrivate
*driContextPriv
,
484 __DRIdrawablePrivate
*driDrawPriv
,
485 __DRIdrawablePrivate
*driReadPriv
)
487 if (driContextPriv
) {
488 ffbContextPtr fmesa
= (ffbContextPtr
) driContextPriv
->driverPrivate
;
491 fmesa
->driDrawable
= driDrawPriv
;
493 _mesa_make_current2(fmesa
->glCtx
,
494 (GLframebuffer
*) driDrawPriv
->driverPrivate
,
495 (GLframebuffer
*) driReadPriv
->driverPrivate
);
498 if (fmesa
->wid
== ~0) {
500 if (getenv("LIBGL_SOFTWARE_RENDERING"))
501 FALLBACK( fmesa
->glCtx
, FFB_BADATTR_SWONLY
, GL_TRUE
);
504 LOCK_HARDWARE(fmesa
);
506 fmesa
->wid
= fmesa
->ffb_sarea
->wid_table
[driDrawPriv
->index
];
507 ffb_init_wid(fmesa
, fmesa
->wid
);
510 fmesa
->state_dirty
|= FFB_STATE_ALL
;
511 fmesa
->state_fifo_ents
= fmesa
->state_all_fifo_ents
;
512 ffbSyncHardware(fmesa
);
513 UNLOCK_HARDWARE(fmesa
);
516 /* Also, at the first switch to a new context,
517 * we need to clear all the hw buffers.
519 ffbDDClear(fmesa
->glCtx
,
520 (DD_FRONT_LEFT_BIT
| DD_BACK_LEFT_BIT
|
521 DD_DEPTH_BIT
| DD_STENCIL_BIT
),
525 _mesa_make_current(NULL
, NULL
);
531 /* Force the context `c' to be unbound from its buffer */
533 ffbUnbindContext(__DRIcontextPrivate
*driContextPriv
)
538 void ffbXMesaUpdateState(ffbContextPtr fmesa
)
540 __DRIdrawablePrivate
*dPriv
= fmesa
->driDrawable
;
541 __DRIscreenPrivate
*sPriv
= fmesa
->driScreen
;
542 int stamp
= dPriv
->lastStamp
;
544 DRI_VALIDATE_DRAWABLE_INFO(sPriv
, dPriv
);
546 if (dPriv
->lastStamp
!= stamp
) {
547 GLcontext
*ctx
= fmesa
->glCtx
;
549 ffbCalcViewport(ctx
);
550 if (ctx
->Polygon
.StippleFlag
)
551 ffbXformAreaPattern(fmesa
,
552 (const GLubyte
*)ctx
->PolygonStipple
);
557 static struct __DriverAPIRec ffbAPI
= {
572 * This is the bootstrap function for the driver.
573 * The __driCreateScreen name is the symbol that libGL.so fetches.
574 * Return: pointer to a __DRIscreenPrivate.
576 void *__driCreateScreen(Display
*dpy
, int scrn
, __DRIscreen
*psc
,
577 int numConfigs
, __GLXvisualConfig
*config
)
579 __DRIscreenPrivate
*psp
;
580 psp
= __driUtilCreateScreen(dpy
, scrn
, psc
, numConfigs
, config
, &ffbAPI
);
585 #endif /* GLX_DIRECT_RENDERING */