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 #include "ffb_xmesa.h"
31 #include "simple_list.h"
34 #include "swrast/swrast.h"
35 #include "swrast_setup/swrast_setup.h"
37 #include "tnl/t_pipeline.h"
38 #include "array_cache/acache.h"
39 #include "drivers/common/driverfuncs.h"
41 #include "ffb_context.h"
44 #include "ffb_depth.h"
45 #include "ffb_stencil.h"
46 #include "ffb_clear.h"
49 #include "ffb_lines.h"
50 #include "ffb_points.h"
51 #include "ffb_state.h"
54 #include "ffb_vtxfmt.h"
55 #include "ffb_bitmap.h"
57 #include "drm_sarea.h"
60 ffbInitDriver(__DRIscreenPrivate
*sPriv
)
62 ffbScreenPrivate
*ffbScreen
;
63 FFBDRIPtr gDRIPriv
= (FFBDRIPtr
) sPriv
->pDevPriv
;
65 if (getenv("LIBGL_FORCE_XSERVER"))
68 /* Allocate the private area. */
69 ffbScreen
= (ffbScreenPrivate
*) MALLOC(sizeof(ffbScreenPrivate
));
73 /* Map FBC registers. */
77 &gDRIPriv
->mFbcRegs
)) {
81 ffbScreen
->regs
= (ffb_fbcPtr
) gDRIPriv
->mFbcRegs
;
83 /* Map ramdac registers. */
87 &gDRIPriv
->mDacRegs
)) {
88 drmUnmap(gDRIPriv
->mFbcRegs
, gDRIPriv
->sFbcRegs
);
92 ffbScreen
->dac
= (ffb_dacPtr
) gDRIPriv
->mDacRegs
;
94 /* Map "Smart" framebuffer views. */
99 drmUnmap(gDRIPriv
->mFbcRegs
, gDRIPriv
->sFbcRegs
);
100 drmUnmap(gDRIPriv
->mDacRegs
, gDRIPriv
->sDacRegs
);
104 ffbScreen
->sfb8r
= (volatile char *) gDRIPriv
->mSfb8r
;
106 if (drmMap(sPriv
->fd
,
109 &gDRIPriv
->mSfb32
)) {
110 drmUnmap(gDRIPriv
->mFbcRegs
, gDRIPriv
->sFbcRegs
);
111 drmUnmap(gDRIPriv
->mDacRegs
, gDRIPriv
->sDacRegs
);
112 drmUnmap(gDRIPriv
->mSfb8r
, gDRIPriv
->sSfb8r
);
116 ffbScreen
->sfb32
= (volatile char *) gDRIPriv
->mSfb32
;
118 if (drmMap(sPriv
->fd
,
121 &gDRIPriv
->mSfb64
)) {
122 drmUnmap(gDRIPriv
->mFbcRegs
, gDRIPriv
->sFbcRegs
);
123 drmUnmap(gDRIPriv
->mDacRegs
, gDRIPriv
->sDacRegs
);
124 drmUnmap(gDRIPriv
->mSfb8r
, gDRIPriv
->sSfb8r
);
125 drmUnmap(gDRIPriv
->mSfb32
, gDRIPriv
->sSfb32
);
129 ffbScreen
->sfb64
= (volatile char *) gDRIPriv
->mSfb64
;
131 ffbScreen
->fifo_cache
= 0;
132 ffbScreen
->rp_active
= 0;
134 ffbScreen
->sPriv
= sPriv
;
135 sPriv
->private = (void *) ffbScreen
;
138 ffbDDPointfuncInit();
145 ffbDestroyScreen(__DRIscreenPrivate
*sPriv
)
147 ffbScreenPrivate
*ffbScreen
= sPriv
->private;
148 FFBDRIPtr gDRIPriv
= (FFBDRIPtr
) sPriv
->pDevPriv
;
150 drmUnmap(gDRIPriv
->mFbcRegs
, gDRIPriv
->sFbcRegs
);
151 drmUnmap(gDRIPriv
->mDacRegs
, gDRIPriv
->sDacRegs
);
152 drmUnmap(gDRIPriv
->mSfb8r
, gDRIPriv
->sSfb8r
);
153 drmUnmap(gDRIPriv
->mSfb32
, gDRIPriv
->sSfb32
);
154 drmUnmap(gDRIPriv
->mSfb64
, gDRIPriv
->sSfb64
);
159 static const struct tnl_pipeline_stage
*ffb_pipeline
[] = {
160 &_tnl_vertex_transform_stage
,
161 &_tnl_normal_transform_stage
,
162 &_tnl_lighting_stage
,
163 /* REMOVE: fog coord stage */
165 &_tnl_texture_transform_stage
,
166 /* REMOVE: point attenuation stage */
171 /* Create and initialize the Mesa and driver specific context data */
173 ffbCreateContext(const __GLcontextModes
*mesaVis
,
174 __DRIcontextPrivate
*driContextPriv
,
175 void *sharedContextPrivate
)
178 GLcontext
*ctx
, *shareCtx
;
179 __DRIscreenPrivate
*sPriv
;
180 ffbScreenPrivate
*ffbScreen
;
182 struct dd_function_table functions
;
184 /* Allocate ffb context */
185 fmesa
= (ffbContextPtr
) CALLOC(sizeof(ffbContextRec
));
189 _mesa_init_driver_functions(&functions
);
191 /* Allocate Mesa context */
192 if (sharedContextPrivate
)
193 shareCtx
= ((ffbContextPtr
) sharedContextPrivate
)->glCtx
;
196 fmesa
->glCtx
= _mesa_create_context(mesaVis
, shareCtx
,
202 driContextPriv
->driverPrivate
= fmesa
;
205 sPriv
= driContextPriv
->driScreenPriv
;
206 ffbScreen
= (ffbScreenPrivate
*) sPriv
->private;
209 fmesa
->hHWContext
= driContextPriv
->hHWContext
;
210 fmesa
->driFd
= sPriv
->fd
;
211 fmesa
->driHwLock
= &sPriv
->pSAREA
->lock
;
213 fmesa
->ffbScreen
= ffbScreen
;
214 fmesa
->driScreen
= sPriv
;
215 fmesa
->ffb_sarea
= FFB_DRISHARE(sPriv
->pSAREA
);
217 /* Register and framebuffer hw pointers. */
218 fmesa
->regs
= ffbScreen
->regs
;
219 fmesa
->sfb32
= ffbScreen
->sfb32
;
221 ffbDDInitContextHwState(ctx
);
223 /* Default clear and depth colors. */
225 GLubyte r
= (GLint
) (ctx
->Color
.ClearColor
[0] * 255.0F
);
226 GLubyte g
= (GLint
) (ctx
->Color
.ClearColor
[1] * 255.0F
);
227 GLubyte b
= (GLint
) (ctx
->Color
.ClearColor
[2] * 255.0F
);
229 fmesa
->clear_pixel
= ((r
<< 0) |
233 fmesa
->clear_depth
= Z_FROM_MESA(ctx
->Depth
.Clear
* 4294967295.0f
);
234 fmesa
->clear_stencil
= ctx
->Stencil
.Clear
& 0xf;
236 /* No wide points. */
237 ctx
->Const
.MinPointSize
= 1.0;
238 ctx
->Const
.MinPointSizeAA
= 1.0;
239 ctx
->Const
.MaxPointSize
= 1.0;
240 ctx
->Const
.MaxPointSizeAA
= 1.0;
242 /* Disable wide lines as we can't antialias them correctly in
245 ctx
->Const
.MinLineWidth
= 1.0;
246 ctx
->Const
.MinLineWidthAA
= 1.0;
247 ctx
->Const
.MaxLineWidth
= 1.0;
248 ctx
->Const
.MaxLineWidthAA
= 1.0;
249 ctx
->Const
.LineWidthGranularity
= 1.0;
251 /* Instead of having GCC emit these constants a zillion times
252 * everywhere in the driver, put them here.
254 fmesa
->ffb_2_30_fixed_scale
= __FFB_2_30_FIXED_SCALE
;
255 fmesa
->ffb_one_over_2_30_fixed_scale
= (1.0 / __FFB_2_30_FIXED_SCALE
);
256 fmesa
->ffb_16_16_fixed_scale
= __FFB_16_16_FIXED_SCALE
;
257 fmesa
->ffb_one_over_16_16_fixed_scale
= (1.0 / __FFB_16_16_FIXED_SCALE
);
258 fmesa
->ffb_ubyte_color_scale
= 255.0f
;
259 fmesa
->ffb_zero
= 0.0f
;
261 fmesa
->debugFallbacks
= GL_FALSE
;
262 debug
= getenv("LIBGL_DEBUG");
263 if (debug
&& strstr(debug
, "fallbacks"))
264 fmesa
->debugFallbacks
= GL_TRUE
;
266 /* Initialize the software rasterizer and helper modules. */
267 _swrast_CreateContext( ctx
);
268 _ac_CreateContext( ctx
);
269 _tnl_CreateContext( ctx
);
270 _swsetup_CreateContext( ctx
);
272 /* All of this need only be done once for a new context. */
273 /* XXX these should be moved right after the
274 * _mesa_init_driver_functions() call above.
276 ffbDDExtensionsInit(ctx
);
277 ffbDDInitDriverFuncs(ctx
);
278 ffbDDInitStateFuncs(ctx
);
279 ffbDDInitSpanFuncs(ctx
);
280 ffbDDInitDepthFuncs(ctx
);
281 ffbDDInitStencilFuncs(ctx
);
282 ffbDDInitRenderFuncs(ctx
);
283 /*ffbDDInitTexFuncs(ctx); not needed */
284 ffbDDInitBitmapFuncs(ctx
);
288 ffbInitTnlModule(ctx
);
291 _tnl_destroy_pipeline(ctx
);
292 _tnl_install_pipeline(ctx
, ffb_pipeline
);
298 ffbDestroyContext(__DRIcontextPrivate
*driContextPriv
)
300 ffbContextPtr fmesa
= (ffbContextPtr
) driContextPriv
->driverPrivate
;
303 ffbFreeVB(fmesa
->glCtx
);
305 _swsetup_DestroyContext( fmesa
->glCtx
);
306 _tnl_DestroyContext( fmesa
->glCtx
);
307 _ac_DestroyContext( fmesa
->glCtx
);
308 _swrast_DestroyContext( fmesa
->glCtx
);
310 /* free the Mesa context */
311 fmesa
->glCtx
->DriverCtx
= NULL
;
312 _mesa_destroy_context(fmesa
->glCtx
);
318 /* Create and initialize the Mesa and driver specific pixmap buffer data */
320 ffbCreateBuffer(__DRIscreenPrivate
*driScrnPriv
,
321 __DRIdrawablePrivate
*driDrawPriv
,
322 const __GLcontextModes
*mesaVis
,
326 return GL_FALSE
; /* not implemented */
329 driDrawPriv
->driverPrivate
= (void *)
330 _mesa_create_framebuffer(mesaVis
,
331 GL_FALSE
, /* software depth buffer? */
332 mesaVis
->stencilBits
> 0,
333 mesaVis
->accumRedBits
> 0,
334 mesaVis
->alphaBits
> 0);
335 return (driDrawPriv
->driverPrivate
!= NULL
);
341 ffbDestroyBuffer(__DRIdrawablePrivate
*driDrawPriv
)
343 _mesa_destroy_framebuffer((GLframebuffer
*) (driDrawPriv
->driverPrivate
));
347 #define USE_FAST_SWAP
350 ffbSwapBuffers( __DRIdrawablePrivate
*dPriv
)
352 ffbContextPtr fmesa
= (ffbContextPtr
) dPriv
->driContextPriv
->driverPrivate
;
353 unsigned int fbc
, wid
, wid_reg_val
, dac_db_bit
;
354 unsigned int shadow_dac_addr
, active_dac_addr
;
359 fmesa
->glCtx
->Visual
.doubleBufferMode
== 0)
362 /* Flush pending rendering commands */
363 _mesa_notifySwapBuffers(fmesa
->glCtx
);
366 dac
= fmesa
->ffbScreen
->dac
;
371 /* Swap the buffer we render into and read pixels from. */
372 fmesa
->back_buffer
^= 1;
374 /* If we are writing into both buffers, don't mess with
377 if ((fbc
& FFB_FBC_WB_AB
) != FFB_FBC_WB_AB
) {
378 if ((fbc
& FFB_FBC_WB_A
) != 0)
379 fbc
= (fbc
& ~FFB_FBC_WB_A
) | FFB_FBC_WB_B
;
381 fbc
= (fbc
& ~FFB_FBC_WB_B
) | FFB_FBC_WB_A
;
384 /* But either way, we must flip the read buffer setting. */
385 if ((fbc
& FFB_FBC_RB_A
) != 0)
386 fbc
= (fbc
& ~FFB_FBC_RB_A
) | FFB_FBC_RB_B
;
388 fbc
= (fbc
& ~FFB_FBC_RB_B
) | FFB_FBC_RB_A
;
390 LOCK_HARDWARE(fmesa
);
392 if (fmesa
->fbc
!= fbc
) {
394 ffb
->fbc
= fmesa
->fbc
= fbc
;
395 fmesa
->ffbScreen
->rp_active
= 1;
398 /* And swap the buffer displayed in the WID. */
399 if (fmesa
->ffb_sarea
->flags
& FFB_DRI_PAC1
) {
400 shadow_dac_addr
= FFBDAC_PAC1_SPWLUT(wid
);
401 active_dac_addr
= FFBDAC_PAC1_APWLUT(wid
);
402 dac_db_bit
= FFBDAC_PAC1_WLUT_DB
;
404 shadow_dac_addr
= FFBDAC_PAC2_SPWLUT(wid
);
405 active_dac_addr
= FFBDAC_PAC2_APWLUT(wid
);
406 dac_db_bit
= FFBDAC_PAC2_WLUT_DB
;
411 wid_reg_val
= DACCFG_READ(dac
, active_dac_addr
);
412 if (fmesa
->back_buffer
== 0)
413 wid_reg_val
|= dac_db_bit
;
415 wid_reg_val
&= ~dac_db_bit
;
417 DACCFG_WRITE(dac
, active_dac_addr
, wid_reg_val
);
419 DACCFG_WRITE(dac
, shadow_dac_addr
, wid_reg_val
);
421 /* Schedule the window transfer. */
422 DACCFG_WRITE(dac
, FFBDAC_CFG_WTCTRL
,
423 (FFBDAC_CFG_WTCTRL_TCMD
| FFBDAC_CFG_WTCTRL_TE
));
428 unsigned int wtctrl
= DACCFG_READ(dac
, FFBDAC_CFG_WTCTRL
);
430 if ((wtctrl
& FFBDAC_CFG_WTCTRL_DS
) == 0)
436 UNLOCK_HARDWARE(fmesa
);
439 static void ffb_init_wid(ffbContextPtr fmesa
, unsigned int wid
)
441 ffb_dacPtr dac
= fmesa
->ffbScreen
->dac
;
442 unsigned int wid_reg_val
, dac_db_bit
, active_dac_addr
;
443 unsigned int shadow_dac_addr
;
445 if (fmesa
->ffb_sarea
->flags
& FFB_DRI_PAC1
) {
446 shadow_dac_addr
= FFBDAC_PAC1_SPWLUT(wid
);
447 active_dac_addr
= FFBDAC_PAC1_APWLUT(wid
);
448 dac_db_bit
= FFBDAC_PAC1_WLUT_DB
;
450 shadow_dac_addr
= FFBDAC_PAC2_SPWLUT(wid
);
451 active_dac_addr
= FFBDAC_PAC2_APWLUT(wid
);
452 dac_db_bit
= FFBDAC_PAC2_WLUT_DB
;
455 wid_reg_val
= DACCFG_READ(dac
, active_dac_addr
);
456 wid_reg_val
&= ~dac_db_bit
;
458 DACCFG_WRITE(dac
, active_dac_addr
, wid_reg_val
);
460 DACCFG_WRITE(dac
, shadow_dac_addr
, wid_reg_val
);
462 /* Schedule the window transfer. */
463 DACCFG_WRITE(dac
, FFBDAC_CFG_WTCTRL
,
464 (FFBDAC_CFG_WTCTRL_TCMD
| FFBDAC_CFG_WTCTRL_TE
));
469 unsigned int wtctrl
= DACCFG_READ(dac
, FFBDAC_CFG_WTCTRL
);
471 if ((wtctrl
& FFBDAC_CFG_WTCTRL_DS
) == 0)
478 /* Force the context `c' to be the current context and associate with it
481 ffbMakeCurrent(__DRIcontextPrivate
*driContextPriv
,
482 __DRIdrawablePrivate
*driDrawPriv
,
483 __DRIdrawablePrivate
*driReadPriv
)
485 if (driContextPriv
) {
486 ffbContextPtr fmesa
= (ffbContextPtr
) driContextPriv
->driverPrivate
;
489 fmesa
->driDrawable
= driDrawPriv
;
491 _mesa_make_current2(fmesa
->glCtx
,
492 (GLframebuffer
*) driDrawPriv
->driverPrivate
,
493 (GLframebuffer
*) driReadPriv
->driverPrivate
);
496 if (fmesa
->wid
== ~0) {
498 if (getenv("LIBGL_SOFTWARE_RENDERING"))
499 FALLBACK( fmesa
->glCtx
, FFB_BADATTR_SWONLY
, GL_TRUE
);
502 LOCK_HARDWARE(fmesa
);
504 fmesa
->wid
= fmesa
->ffb_sarea
->wid_table
[driDrawPriv
->index
];
505 ffb_init_wid(fmesa
, fmesa
->wid
);
508 fmesa
->state_dirty
|= FFB_STATE_ALL
;
509 fmesa
->state_fifo_ents
= fmesa
->state_all_fifo_ents
;
510 ffbSyncHardware(fmesa
);
511 UNLOCK_HARDWARE(fmesa
);
514 /* Also, at the first switch to a new context,
515 * we need to clear all the hw buffers.
517 ffbDDClear(fmesa
->glCtx
,
518 (BUFFER_BIT_FRONT_LEFT
| BUFFER_BIT_BACK_LEFT
|
519 BUFFER_BIT_DEPTH
| BUFFER_BIT_STENCIL
),
523 _mesa_make_current(NULL
, NULL
);
529 /* Force the context `c' to be unbound from its buffer */
531 ffbUnbindContext(__DRIcontextPrivate
*driContextPriv
)
536 void ffbXMesaUpdateState(ffbContextPtr fmesa
)
538 __DRIdrawablePrivate
*dPriv
= fmesa
->driDrawable
;
539 __DRIscreenPrivate
*sPriv
= fmesa
->driScreen
;
540 int stamp
= dPriv
->lastStamp
;
542 DRI_VALIDATE_DRAWABLE_INFO(sPriv
, dPriv
);
544 if (dPriv
->lastStamp
!= stamp
) {
545 GLcontext
*ctx
= fmesa
->glCtx
;
547 ffbCalcViewport(ctx
);
548 if (ctx
->Polygon
.StippleFlag
)
549 ffbXformAreaPattern(fmesa
,
550 (const GLubyte
*)ctx
->PolygonStipple
);
555 static struct __DriverAPIRec ffbAPI
= {
570 * This is the bootstrap function for the driver.
571 * The __driCreateScreen name is the symbol that libGL.so fetches.
572 * Return: pointer to a __DRIscreenPrivate.
574 void *__driCreateScreen(Display
*dpy
, int scrn
, __DRIscreen
*psc
,
575 int numConfigs
, __GLXvisualConfig
*config
)
577 __DRIscreenPrivate
*psp
;
578 psp
= __driUtilCreateScreen(dpy
, scrn
, psc
, numConfigs
, config
, &ffbAPI
);