Major check-in of changes for GL_EXT_framebuffer_object extension.
[mesa.git] / src / mesa / drivers / dri / ffb / ffb_xmesa.c
1 /* $XFree86: xc/lib/GL/mesa/src/drv/ffb/ffb_xmesa.c,v 1.4 2002/02/22 21:32:59 dawes Exp $
2 *
3 * GLX Hardware Device Driver for Sun Creator/Creator3D
4 * Copyright (C) 2000, 2001 David S. Miller
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
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.
23 *
24 *
25 * David S. Miller <davem@redhat.com>
26 */
27
28 #include "ffb_xmesa.h"
29 #include "context.h"
30 #include "matrix.h"
31 #include "simple_list.h"
32 #include "imports.h"
33
34 #include "swrast/swrast.h"
35 #include "swrast_setup/swrast_setup.h"
36 #include "tnl/tnl.h"
37 #include "tnl/t_pipeline.h"
38 #include "array_cache/acache.h"
39 #include "drivers/common/driverfuncs.h"
40
41 #include "ffb_context.h"
42 #include "ffb_dd.h"
43 #include "ffb_span.h"
44 #include "ffb_depth.h"
45 #include "ffb_stencil.h"
46 #include "ffb_clear.h"
47 #include "ffb_vb.h"
48 #include "ffb_tris.h"
49 #include "ffb_lines.h"
50 #include "ffb_points.h"
51 #include "ffb_state.h"
52 #include "ffb_tex.h"
53 #include "ffb_lock.h"
54 #include "ffb_vtxfmt.h"
55 #include "ffb_bitmap.h"
56
57 #include "drm_sarea.h"
58
59 static GLboolean
60 ffbInitDriver(__DRIscreenPrivate *sPriv)
61 {
62 ffbScreenPrivate *ffbScreen;
63 FFBDRIPtr gDRIPriv = (FFBDRIPtr) sPriv->pDevPriv;
64
65 if (getenv("LIBGL_FORCE_XSERVER"))
66 return GL_FALSE;
67
68 /* Allocate the private area. */
69 ffbScreen = (ffbScreenPrivate *) MALLOC(sizeof(ffbScreenPrivate));
70 if (!ffbScreen)
71 return GL_FALSE;
72
73 /* Map FBC registers. */
74 if (drmMap(sPriv->fd,
75 gDRIPriv->hFbcRegs,
76 gDRIPriv->sFbcRegs,
77 &gDRIPriv->mFbcRegs)) {
78 FREE(ffbScreen);
79 return GL_FALSE;
80 }
81 ffbScreen->regs = (ffb_fbcPtr) gDRIPriv->mFbcRegs;
82
83 /* Map ramdac registers. */
84 if (drmMap(sPriv->fd,
85 gDRIPriv->hDacRegs,
86 gDRIPriv->sDacRegs,
87 &gDRIPriv->mDacRegs)) {
88 drmUnmap(gDRIPriv->mFbcRegs, gDRIPriv->sFbcRegs);
89 FREE(ffbScreen);
90 return GL_FALSE;
91 }
92 ffbScreen->dac = (ffb_dacPtr) gDRIPriv->mDacRegs;
93
94 /* Map "Smart" framebuffer views. */
95 if (drmMap(sPriv->fd,
96 gDRIPriv->hSfb8r,
97 gDRIPriv->sSfb8r,
98 &gDRIPriv->mSfb8r)) {
99 drmUnmap(gDRIPriv->mFbcRegs, gDRIPriv->sFbcRegs);
100 drmUnmap(gDRIPriv->mDacRegs, gDRIPriv->sDacRegs);
101 FREE(ffbScreen);
102 return GL_FALSE;
103 }
104 ffbScreen->sfb8r = (volatile char *) gDRIPriv->mSfb8r;
105
106 if (drmMap(sPriv->fd,
107 gDRIPriv->hSfb32,
108 gDRIPriv->sSfb32,
109 &gDRIPriv->mSfb32)) {
110 drmUnmap(gDRIPriv->mFbcRegs, gDRIPriv->sFbcRegs);
111 drmUnmap(gDRIPriv->mDacRegs, gDRIPriv->sDacRegs);
112 drmUnmap(gDRIPriv->mSfb8r, gDRIPriv->sSfb8r);
113 FREE(ffbScreen);
114 return GL_FALSE;
115 }
116 ffbScreen->sfb32 = (volatile char *) gDRIPriv->mSfb32;
117
118 if (drmMap(sPriv->fd,
119 gDRIPriv->hSfb64,
120 gDRIPriv->sSfb64,
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);
126 FREE(ffbScreen);
127 return GL_FALSE;
128 }
129 ffbScreen->sfb64 = (volatile char *) gDRIPriv->mSfb64;
130
131 ffbScreen->fifo_cache = 0;
132 ffbScreen->rp_active = 0;
133
134 ffbScreen->sPriv = sPriv;
135 sPriv->private = (void *) ffbScreen;
136
137 ffbDDLinefuncInit();
138 ffbDDPointfuncInit();
139
140 return GL_TRUE;
141 }
142
143
144 static void
145 ffbDestroyScreen(__DRIscreenPrivate *sPriv)
146 {
147 ffbScreenPrivate *ffbScreen = sPriv->private;
148 FFBDRIPtr gDRIPriv = (FFBDRIPtr) sPriv->pDevPriv;
149
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);
155
156 FREE(ffbScreen);
157 }
158
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 */
164 &_tnl_texgen_stage,
165 &_tnl_texture_transform_stage,
166 /* REMOVE: point attenuation stage */
167 &_tnl_render_stage,
168 0,
169 };
170
171 /* Create and initialize the Mesa and driver specific context data */
172 static GLboolean
173 ffbCreateContext(const __GLcontextModes *mesaVis,
174 __DRIcontextPrivate *driContextPriv,
175 void *sharedContextPrivate)
176 {
177 ffbContextPtr fmesa;
178 GLcontext *ctx, *shareCtx;
179 __DRIscreenPrivate *sPriv;
180 ffbScreenPrivate *ffbScreen;
181 char *debug;
182 struct dd_function_table functions;
183
184 /* Allocate ffb context */
185 fmesa = (ffbContextPtr) CALLOC(sizeof(ffbContextRec));
186 if (!fmesa)
187 return GL_FALSE;
188
189 _mesa_init_driver_functions(&functions);
190
191 /* Allocate Mesa context */
192 if (sharedContextPrivate)
193 shareCtx = ((ffbContextPtr) sharedContextPrivate)->glCtx;
194 else
195 shareCtx = NULL;
196 fmesa->glCtx = _mesa_create_context(mesaVis, shareCtx,
197 &functions, fmesa);
198 if (!fmesa->glCtx) {
199 FREE(fmesa);
200 return GL_FALSE;
201 }
202 driContextPriv->driverPrivate = fmesa;
203 ctx = fmesa->glCtx;
204
205 sPriv = driContextPriv->driScreenPriv;
206 ffbScreen = (ffbScreenPrivate *) sPriv->private;
207
208 /* Dri stuff. */
209 fmesa->hHWContext = driContextPriv->hHWContext;
210 fmesa->driFd = sPriv->fd;
211 fmesa->driHwLock = &sPriv->pSAREA->lock;
212
213 fmesa->ffbScreen = ffbScreen;
214 fmesa->driScreen = sPriv;
215 fmesa->ffb_sarea = FFB_DRISHARE(sPriv->pSAREA);
216
217 /* Register and framebuffer hw pointers. */
218 fmesa->regs = ffbScreen->regs;
219 fmesa->sfb32 = ffbScreen->sfb32;
220
221 ffbDDInitContextHwState(ctx);
222
223 /* Default clear and depth colors. */
224 {
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);
228
229 fmesa->clear_pixel = ((r << 0) |
230 (g << 8) |
231 (b << 16));
232 }
233 fmesa->clear_depth = Z_FROM_MESA(ctx->Depth.Clear * 4294967295.0f);
234 fmesa->clear_stencil = ctx->Stencil.Clear & 0xf;
235
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;
241
242 /* Disable wide lines as we can't antialias them correctly in
243 * hardware.
244 */
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;
250
251 /* Instead of having GCC emit these constants a zillion times
252 * everywhere in the driver, put them here.
253 */
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;
260
261 fmesa->debugFallbacks = GL_FALSE;
262 debug = getenv("LIBGL_DEBUG");
263 if (debug && strstr(debug, "fallbacks"))
264 fmesa->debugFallbacks = GL_TRUE;
265
266 /* Initialize the software rasterizer and helper modules. */
267 _swrast_CreateContext( ctx );
268 _ac_CreateContext( ctx );
269 _tnl_CreateContext( ctx );
270 _swsetup_CreateContext( ctx );
271
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.
275 */
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);
285 ffbInitVB(ctx);
286
287 #if 0
288 ffbInitTnlModule(ctx);
289 #endif
290
291 _tnl_destroy_pipeline(ctx);
292 _tnl_install_pipeline(ctx, ffb_pipeline);
293
294 return GL_TRUE;
295 }
296
297 static void
298 ffbDestroyContext(__DRIcontextPrivate *driContextPriv)
299 {
300 ffbContextPtr fmesa = (ffbContextPtr) driContextPriv->driverPrivate;
301
302 if (fmesa) {
303 ffbFreeVB(fmesa->glCtx);
304
305 _swsetup_DestroyContext( fmesa->glCtx );
306 _tnl_DestroyContext( fmesa->glCtx );
307 _ac_DestroyContext( fmesa->glCtx );
308 _swrast_DestroyContext( fmesa->glCtx );
309
310 /* free the Mesa context */
311 fmesa->glCtx->DriverCtx = NULL;
312 _mesa_destroy_context(fmesa->glCtx);
313
314 FREE(fmesa);
315 }
316 }
317
318 /* Create and initialize the Mesa and driver specific pixmap buffer data */
319 static GLboolean
320 ffbCreateBuffer(__DRIscreenPrivate *driScrnPriv,
321 __DRIdrawablePrivate *driDrawPriv,
322 const __GLcontextModes *mesaVis,
323 GLboolean isPixmap )
324 {
325 if (isPixmap) {
326 return GL_FALSE; /* not implemented */
327 }
328 else {
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);
336 }
337 }
338
339
340 static void
341 ffbDestroyBuffer(__DRIdrawablePrivate *driDrawPriv)
342 {
343 _mesa_destroy_framebuffer((GLframebuffer *) (driDrawPriv->driverPrivate));
344 }
345
346
347 #define USE_FAST_SWAP
348
349 static void
350 ffbSwapBuffers( __DRIdrawablePrivate *dPriv )
351 {
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;
355 ffb_fbcPtr ffb;
356 ffb_dacPtr dac;
357
358 if (fmesa == NULL ||
359 fmesa->glCtx->Visual.doubleBufferMode == 0)
360 return;
361
362 /* Flush pending rendering commands */
363 _mesa_notifySwapBuffers(fmesa->glCtx);
364
365 ffb = fmesa->regs;
366 dac = fmesa->ffbScreen->dac;
367
368 fbc = fmesa->fbc;
369 wid = fmesa->wid;
370
371 /* Swap the buffer we render into and read pixels from. */
372 fmesa->back_buffer ^= 1;
373
374 /* If we are writing into both buffers, don't mess with
375 * the WB setting.
376 */
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;
380 else
381 fbc = (fbc & ~FFB_FBC_WB_B) | FFB_FBC_WB_A;
382 }
383
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;
387 else
388 fbc = (fbc & ~FFB_FBC_RB_B) | FFB_FBC_RB_A;
389
390 LOCK_HARDWARE(fmesa);
391
392 if (fmesa->fbc != fbc) {
393 FFBFifo(fmesa, 1);
394 ffb->fbc = fmesa->fbc = fbc;
395 fmesa->ffbScreen->rp_active = 1;
396 }
397
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;
403 } else {
404 shadow_dac_addr = FFBDAC_PAC2_SPWLUT(wid);
405 active_dac_addr = FFBDAC_PAC2_APWLUT(wid);
406 dac_db_bit = FFBDAC_PAC2_WLUT_DB;
407 }
408
409 FFBWait(fmesa, ffb);
410
411 wid_reg_val = DACCFG_READ(dac, active_dac_addr);
412 if (fmesa->back_buffer == 0)
413 wid_reg_val |= dac_db_bit;
414 else
415 wid_reg_val &= ~dac_db_bit;
416 #ifdef USE_FAST_SWAP
417 DACCFG_WRITE(dac, active_dac_addr, wid_reg_val);
418 #else
419 DACCFG_WRITE(dac, shadow_dac_addr, wid_reg_val);
420
421 /* Schedule the window transfer. */
422 DACCFG_WRITE(dac, FFBDAC_CFG_WTCTRL,
423 (FFBDAC_CFG_WTCTRL_TCMD | FFBDAC_CFG_WTCTRL_TE));
424
425 {
426 int limit = 1000000;
427 while (limit--) {
428 unsigned int wtctrl = DACCFG_READ(dac, FFBDAC_CFG_WTCTRL);
429
430 if ((wtctrl & FFBDAC_CFG_WTCTRL_DS) == 0)
431 break;
432 }
433 }
434 #endif
435
436 UNLOCK_HARDWARE(fmesa);
437 }
438
439 static void ffb_init_wid(ffbContextPtr fmesa, unsigned int wid)
440 {
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;
444
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;
449 } else {
450 shadow_dac_addr = FFBDAC_PAC2_SPWLUT(wid);
451 active_dac_addr = FFBDAC_PAC2_APWLUT(wid);
452 dac_db_bit = FFBDAC_PAC2_WLUT_DB;
453 }
454
455 wid_reg_val = DACCFG_READ(dac, active_dac_addr);
456 wid_reg_val &= ~dac_db_bit;
457 #ifdef USE_FAST_SWAP
458 DACCFG_WRITE(dac, active_dac_addr, wid_reg_val);
459 #else
460 DACCFG_WRITE(dac, shadow_dac_addr, wid_reg_val);
461
462 /* Schedule the window transfer. */
463 DACCFG_WRITE(dac, FFBDAC_CFG_WTCTRL,
464 (FFBDAC_CFG_WTCTRL_TCMD | FFBDAC_CFG_WTCTRL_TE));
465
466 {
467 int limit = 1000000;
468 while (limit--) {
469 unsigned int wtctrl = DACCFG_READ(dac, FFBDAC_CFG_WTCTRL);
470
471 if ((wtctrl & FFBDAC_CFG_WTCTRL_DS) == 0)
472 break;
473 }
474 }
475 #endif
476 }
477
478 /* Force the context `c' to be the current context and associate with it
479 buffer `b' */
480 static GLboolean
481 ffbMakeCurrent(__DRIcontextPrivate *driContextPriv,
482 __DRIdrawablePrivate *driDrawPriv,
483 __DRIdrawablePrivate *driReadPriv)
484 {
485 if (driContextPriv) {
486 ffbContextPtr fmesa = (ffbContextPtr) driContextPriv->driverPrivate;
487 int first_time;
488
489 fmesa->driDrawable = driDrawPriv;
490
491 _mesa_make_current2(fmesa->glCtx,
492 (GLframebuffer *) driDrawPriv->driverPrivate,
493 (GLframebuffer *) driReadPriv->driverPrivate);
494
495 first_time = 0;
496 if (fmesa->wid == ~0) {
497 first_time = 1;
498 if (getenv("LIBGL_SOFTWARE_RENDERING"))
499 FALLBACK( fmesa->glCtx, FFB_BADATTR_SWONLY, GL_TRUE );
500 }
501
502 LOCK_HARDWARE(fmesa);
503 if (first_time) {
504 fmesa->wid = fmesa->ffb_sarea->wid_table[driDrawPriv->index];
505 ffb_init_wid(fmesa, fmesa->wid);
506 }
507
508 fmesa->state_dirty |= FFB_STATE_ALL;
509 fmesa->state_fifo_ents = fmesa->state_all_fifo_ents;
510 ffbSyncHardware(fmesa);
511 UNLOCK_HARDWARE(fmesa);
512
513 if (first_time) {
514 /* Also, at the first switch to a new context,
515 * we need to clear all the hw buffers.
516 */
517 ffbDDClear(fmesa->glCtx,
518 (BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_BACK_LEFT |
519 BUFFER_BIT_DEPTH | BUFFER_BIT_STENCIL),
520 1, 0, 0, 0, 0);
521 }
522 } else {
523 _mesa_make_current(NULL, NULL);
524 }
525
526 return GL_TRUE;
527 }
528
529 /* Force the context `c' to be unbound from its buffer */
530 static GLboolean
531 ffbUnbindContext(__DRIcontextPrivate *driContextPriv)
532 {
533 return GL_TRUE;
534 }
535
536 void ffbXMesaUpdateState(ffbContextPtr fmesa)
537 {
538 __DRIdrawablePrivate *dPriv = fmesa->driDrawable;
539 __DRIscreenPrivate *sPriv = fmesa->driScreen;
540 int stamp = dPriv->lastStamp;
541
542 DRI_VALIDATE_DRAWABLE_INFO(sPriv, dPriv);
543
544 if (dPriv->lastStamp != stamp) {
545 GLcontext *ctx = fmesa->glCtx;
546
547 ffbCalcViewport(ctx);
548 if (ctx->Polygon.StippleFlag)
549 ffbXformAreaPattern(fmesa,
550 (const GLubyte *)ctx->PolygonStipple);
551 }
552 }
553
554
555 static struct __DriverAPIRec ffbAPI = {
556 ffbInitDriver,
557 ffbDestroyScreen,
558 ffbCreateContext,
559 ffbDestroyContext,
560 ffbCreateBuffer,
561 ffbDestroyBuffer,
562 ffbSwapBuffers,
563 ffbMakeCurrent,
564 ffbUnbindContext
565 };
566
567
568
569 /*
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.
573 */
574 void *__driCreateScreen(Display *dpy, int scrn, __DRIscreen *psc,
575 int numConfigs, __GLXvisualConfig *config)
576 {
577 __DRIscreenPrivate *psp;
578 psp = __driUtilCreateScreen(dpy, scrn, psc, numConfigs, config, &ffbAPI);
579 return (void *) psp;
580 }