1 /* $XFree86$ */ /* -*- mode: c; c-basic-offset: 3 -*- */
3 * Copyright 2000 Gareth Hughes
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 (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * GARETH HUGHES BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 * Gareth Hughes <gareth@valinux.com>
28 * Leif Delgass <ldelgass@retinalburn.net>
29 * Jos�Fonseca <j_r_fonseca@yahoo.co.uk>
32 #include "mach64_context.h"
33 #include "mach64_ioctl.h"
34 #include "mach64_tris.h"
35 #include "mach64_vb.h"
44 #include "GL/internal/dri_interface.h"
47 /* Mach64 configuration
51 const char __driConfigOptions
[] =
53 DRI_CONF_SECTION_PERFORMANCE
54 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_0
)
57 DRI_CONF_SECTION_DEBUG
58 DRI_CONF_PERFORMANCE_BOXES(false)
63 static const GLuint __driNConfigOptions
= 2;
65 static const GLuint __driNConfigOptions
= 1;
68 #ifdef USE_NEW_INTERFACE
69 static PFNGLXCREATECONTEXTMODES create_context_modes
= NULL
;
70 #endif /* USE_NEW_INTERFACE */
72 #ifdef USE_NEW_INTERFACE
73 static __GLcontextModes
* fill_in_modes( __GLcontextModes
* modes
,
76 unsigned stencil_bits
,
77 const GLenum
* db_modes
,
78 unsigned num_db_modes
,
81 static const uint8_t bits
[2][4] = {
86 static const uint32_t masks
[2][4] = {
87 { 0x0000F800, 0x000007E0, 0x0000001F, 0x00000000 },
88 { 0x00FF0000, 0x0000FF00, 0x000000FF, 0x00000000 }
93 const unsigned index
= ((pixel_bits
+ 15) / 16) - 1;
95 for ( i
= 0 ; i
< num_db_modes
; i
++ ) {
96 for ( j
= 0 ; j
< 2 ; j
++ ) {
98 modes
->redBits
= bits
[index
][0];
99 modes
->greenBits
= bits
[index
][1];
100 modes
->blueBits
= bits
[index
][2];
101 modes
->alphaBits
= bits
[index
][3];
102 modes
->redMask
= masks
[index
][0];
103 modes
->greenMask
= masks
[index
][1];
104 modes
->blueMask
= masks
[index
][2];
105 modes
->alphaMask
= masks
[index
][3];
106 modes
->rgbBits
= modes
->redBits
+ modes
->greenBits
107 + modes
->blueBits
+ modes
->alphaBits
;
109 modes
->accumRedBits
= 16 * j
;
110 modes
->accumGreenBits
= 16 * j
;
111 modes
->accumBlueBits
= 16 * j
;
112 modes
->accumAlphaBits
= 0;
113 modes
->visualRating
= (j
== 0) ? GLX_NONE
: GLX_SLOW_CONFIG
;
114 modes
->drawableType
= GLX_WINDOW_BIT
| GLX_PIXMAP_BIT
;
115 modes
->stencilBits
= stencil_bits
;
116 modes
->depthBits
= depth_bits
;
118 modes
->visualType
= visType
;
119 modes
->renderType
= GLX_RGBA_BIT
;
120 modes
->rgbMode
= GL_TRUE
;
122 if ( db_modes
[i
] == GLX_NONE
) {
124 modes
->doubleBufferMode
= GL_FALSE
;
127 modes
->doubleBufferMode
= GL_TRUE
;
128 modes
->swapMethod
= db_modes
[i
];
137 #endif /* USE_NEW_INTERFACE */
140 #ifdef USE_NEW_INTERFACE
141 static __GLcontextModes
*
142 mach64FillInModes( unsigned pixel_bits
, unsigned depth_bits
,
143 unsigned stencil_bits
, GLboolean have_back_buffer
)
145 __GLcontextModes
* modes
;
146 __GLcontextModes
* m
;
148 unsigned depth_buffer_factor
;
149 unsigned back_buffer_factor
;
152 /* Right now GLX_SWAP_COPY_OML isn't supported, but it would be easy
153 * enough to add support. Basically, if a context is created with an
154 * fbconfig where the swap method is GLX_SWAP_COPY_OML, pageflipping
155 * will never be used.
157 static const GLenum back_buffer_modes
[] = {
158 GLX_NONE
, GLX_SWAP_UNDEFINED_OML
/*, GLX_SWAP_COPY_OML */
161 int depth_buffer_modes
[2][2];
164 depth_buffer_modes
[0][0] = depth_bits
;
165 depth_buffer_modes
[1][0] = depth_bits
;
167 /* Just like with the accumulation buffer, always provide some modes
168 * with a stencil buffer. It will be a sw fallback, but some apps won't
171 depth_buffer_modes
[0][1] = 0;
172 depth_buffer_modes
[1][1] = (stencil_bits
== 0) ? 8 : stencil_bits
;
174 depth_buffer_factor
= ((depth_bits
!= 0) || (stencil_bits
!= 0)) ? 2 : 1;
175 back_buffer_factor
= (have_back_buffer
) ? 2 : 1;
177 num_modes
= depth_buffer_factor
* back_buffer_factor
* 4;
179 modes
= (*create_context_modes
)( num_modes
, sizeof( __GLcontextModes
) );
181 for ( i
= 0 ; i
< depth_buffer_factor
; i
++ ) {
182 m
= fill_in_modes( m
, pixel_bits
,
183 depth_buffer_modes
[i
][0], depth_buffer_modes
[i
][1],
184 back_buffer_modes
, back_buffer_factor
,
188 for ( i
= 0 ; i
< depth_buffer_factor
; i
++ ) {
189 m
= fill_in_modes( m
, pixel_bits
,
190 depth_buffer_modes
[i
][0], depth_buffer_modes
[i
][1],
191 back_buffer_modes
, back_buffer_factor
,
195 /* Mark the visual as slow if there are "fake" stencil bits.
197 for ( m
= modes
; m
!= NULL
; m
= m
->next
) {
198 if ( (m
->stencilBits
!= 0) && (m
->stencilBits
!= stencil_bits
) ){
199 m
->visualRating
= GLX_SLOW_CONFIG
;
205 #endif /* USE_NEW_INTERFACE */
208 /* Create the device specific screen private data struct.
210 static mach64ScreenRec
*
211 mach64CreateScreen( __DRIscreenPrivate
*sPriv
)
213 mach64ScreenPtr mach64Screen
;
214 ATIDRIPtr serverInfo
= (ATIDRIPtr
)sPriv
->pDevPriv
;
216 if ( MACH64_DEBUG
& DEBUG_VERBOSE_DRI
)
217 fprintf( stderr
, "%s\n", __FUNCTION__
);
219 /* Allocate the private area */
220 mach64Screen
= (mach64ScreenPtr
) CALLOC( sizeof(*mach64Screen
) );
221 if ( !mach64Screen
) return NULL
;
223 /* parse information in __driConfigOptions */
224 driParseOptionInfo (&mach64Screen
->optionCache
,
225 __driConfigOptions
, __driNConfigOptions
);
227 mach64Screen
->IsPCI
= serverInfo
->IsPCI
;
230 drm_mach64_getparam_t gp
;
233 gp
.param
= MACH64_PARAM_IRQ_NR
;
234 gp
.value
= (void *) &mach64Screen
->irq
;
236 ret
= drmCommandWriteRead( sPriv
->fd
, DRM_MACH64_GETPARAM
,
239 fprintf(stderr
, "DRM_MACH64_GETPARAM (MACH64_PARAM_IRQ_NR): %d\n", ret
);
240 FREE( mach64Screen
);
245 mach64Screen
->mmio
.handle
= serverInfo
->regs
;
246 mach64Screen
->mmio
.size
= serverInfo
->regsSize
;
247 if ( drmMap( sPriv
->fd
,
248 mach64Screen
->mmio
.handle
,
249 mach64Screen
->mmio
.size
,
250 (drmAddressPtr
)&mach64Screen
->mmio
.map
) != 0 ) {
251 FREE( mach64Screen
);
255 mach64Screen
->buffers
= drmMapBufs( sPriv
->fd
);
256 if ( !mach64Screen
->buffers
) {
257 drmUnmap( (drmAddress
)mach64Screen
->mmio
.map
,
258 mach64Screen
->mmio
.size
);
259 FREE( mach64Screen
);
263 if ( !mach64Screen
->IsPCI
) {
264 mach64Screen
->agpTextures
.handle
= serverInfo
->agp
;
265 mach64Screen
->agpTextures
.size
= serverInfo
->agpSize
;
266 if ( drmMap( sPriv
->fd
,
267 mach64Screen
->agpTextures
.handle
,
268 mach64Screen
->agpTextures
.size
,
269 (drmAddressPtr
)&mach64Screen
->agpTextures
.map
) ) {
270 drmUnmapBufs( mach64Screen
->buffers
);
271 drmUnmap( (drmAddress
)mach64Screen
->mmio
.map
, mach64Screen
->mmio
.size
);
272 FREE( mach64Screen
);
277 mach64Screen
->AGPMode
= serverInfo
->AGPMode
;
279 mach64Screen
->chipset
= serverInfo
->chipset
;
280 mach64Screen
->width
= serverInfo
->width
;
281 mach64Screen
->height
= serverInfo
->height
;
282 mach64Screen
->mem
= serverInfo
->mem
;
283 mach64Screen
->cpp
= serverInfo
->cpp
;
285 mach64Screen
->frontOffset
= serverInfo
->frontOffset
;
286 mach64Screen
->frontPitch
= serverInfo
->frontPitch
;
287 mach64Screen
->backOffset
= serverInfo
->backOffset
;
288 mach64Screen
->backPitch
= serverInfo
->backPitch
;
289 mach64Screen
->depthOffset
= serverInfo
->depthOffset
;
290 mach64Screen
->depthPitch
= serverInfo
->depthPitch
;
292 mach64Screen
->texOffset
[MACH64_CARD_HEAP
] = serverInfo
->textureOffset
;
293 mach64Screen
->texSize
[MACH64_CARD_HEAP
] = serverInfo
->textureSize
;
294 mach64Screen
->logTexGranularity
[MACH64_CARD_HEAP
] =
295 serverInfo
->logTextureGranularity
;
297 if ( mach64Screen
->IsPCI
) {
298 mach64Screen
->numTexHeaps
= MACH64_NR_TEX_HEAPS
- 1;
299 mach64Screen
->firstTexHeap
= MACH64_CARD_HEAP
;
300 mach64Screen
->texOffset
[MACH64_AGP_HEAP
] = 0;
301 mach64Screen
->texSize
[MACH64_AGP_HEAP
] = 0;
302 mach64Screen
->logTexGranularity
[MACH64_AGP_HEAP
] = 0;
304 if (mach64Screen
->texSize
[MACH64_CARD_HEAP
] > 0) {
305 mach64Screen
->numTexHeaps
= MACH64_NR_TEX_HEAPS
;
306 mach64Screen
->firstTexHeap
= MACH64_CARD_HEAP
;
308 mach64Screen
->numTexHeaps
= MACH64_NR_TEX_HEAPS
- 1;
309 mach64Screen
->firstTexHeap
= MACH64_AGP_HEAP
;
311 mach64Screen
->texOffset
[MACH64_AGP_HEAP
] = serverInfo
->agpTextureOffset
;
312 mach64Screen
->texSize
[MACH64_AGP_HEAP
] = serverInfo
->agpSize
;
313 mach64Screen
->logTexGranularity
[MACH64_AGP_HEAP
] = serverInfo
->logAgpTextureGranularity
;
316 mach64Screen
->driScreen
= sPriv
;
318 if ( driCompareGLXAPIVersion( 20030813 ) >= 0 ) {
319 PFNGLXSCRENABLEEXTENSIONPROC glx_enable_extension
=
320 (PFNGLXSCRENABLEEXTENSIONPROC
) glXGetProcAddress( (const GLubyte
*) "__glXScrEnableExtension" );
321 void * const psc
= sPriv
->psc
->screenConfigs
;
323 if ( glx_enable_extension
!= NULL
) {
324 if ( mach64Screen
->irq
!= 0 ) {
325 (*glx_enable_extension
)( psc
, "GLX_SGI_swap_control" );
326 (*glx_enable_extension
)( psc
, "GLX_SGI_video_sync" );
327 (*glx_enable_extension
)( psc
, "GLX_MESA_swap_control" );
330 (*glx_enable_extension
)( psc
, "GLX_MESA_swap_frame_usage" );
337 /* Destroy the device specific screen private data struct.
340 mach64DestroyScreen( __DRIscreenPrivate
*driScreen
)
342 mach64ScreenRec
*mach64Screen
= (mach64ScreenRec
*) driScreen
->private;
347 if ( MACH64_DEBUG
& DEBUG_VERBOSE_DRI
)
348 fprintf( stderr
, "%s\n", __FUNCTION__
);
350 if ( !mach64Screen
->IsPCI
) {
351 drmUnmap( (drmAddress
)mach64Screen
->agpTextures
.map
,
352 mach64Screen
->agpTextures
.size
);
355 drmUnmapBufs( mach64Screen
->buffers
);
356 drmUnmap( (drmAddress
)mach64Screen
->mmio
.map
, mach64Screen
->mmio
.size
);
358 FREE( mach64Screen
);
359 driScreen
->private = NULL
;
362 /* Create and initialize the Mesa and driver specific pixmap buffer
366 mach64CreateBuffer( __DRIscreenPrivate
*driScrnPriv
,
367 __DRIdrawablePrivate
*driDrawPriv
,
368 const __GLcontextModes
*mesaVis
,
372 return GL_FALSE
; /* not implemented */
375 driDrawPriv
->driverPrivate
= (void *)
376 _mesa_create_framebuffer( mesaVis
,
377 GL_FALSE
, /* software depth buffer? */
378 mesaVis
->stencilBits
> 0,
379 mesaVis
->accumRedBits
> 0,
380 mesaVis
->alphaBits
> 0 );
381 return (driDrawPriv
->driverPrivate
!= NULL
);
387 mach64DestroyBuffer(__DRIdrawablePrivate
*driDrawPriv
)
389 _mesa_destroy_framebuffer((GLframebuffer
*) (driDrawPriv
->driverPrivate
));
393 /* Copy the back color buffer to the front color buffer */
395 mach64SwapBuffers(__DRIdrawablePrivate
*dPriv
)
397 if (dPriv
->driContextPriv
&& dPriv
->driContextPriv
->driverPrivate
) {
398 mach64ContextPtr mmesa
;
400 mmesa
= (mach64ContextPtr
) dPriv
->driContextPriv
->driverPrivate
;
402 if (ctx
->Visual
.doubleBufferMode
) {
403 _mesa_notifySwapBuffers( ctx
); /* flush pending rendering comands */
404 mach64CopyBuffer( dPriv
);
408 /* XXX this shouldn't be an error but we can't handle it for now */
409 _mesa_problem(NULL
, "%s: drawable has no context!", __FUNCTION__
);
414 /* Initialize the driver specific screen private data.
417 mach64InitDriver( __DRIscreenPrivate
*driScreen
)
419 driScreen
->private = (void *) mach64CreateScreen( driScreen
);
421 if ( !driScreen
->private ) {
422 mach64DestroyScreen( driScreen
);
430 static struct __DriverAPIRec mach64API
= {
431 .InitDriver
= mach64InitDriver
,
432 .DestroyScreen
= mach64DestroyScreen
,
433 .CreateContext
= mach64CreateContext
,
434 .DestroyContext
= mach64DestroyContext
,
435 .CreateBuffer
= mach64CreateBuffer
,
436 .DestroyBuffer
= mach64DestroyBuffer
,
437 .SwapBuffers
= mach64SwapBuffers
,
438 .MakeCurrent
= mach64MakeCurrent
,
439 .UnbindContext
= mach64UnbindContext
,
441 .GetMSC
= driGetMSC32
,
442 .WaitForMSC
= driWaitForMSC32
,
444 .SwapBuffersMSC
= NULL
449 * This is the bootstrap function for the driver.
450 * The __driCreateScreen name is the symbol that libGL.so fetches.
451 * Return: pointer to a __DRIscreenPrivate.
453 #if !defined(DRI_NEW_INTERFACE_ONLY)
455 void *__driCreateScreen(Display
*dpy
, int scrn
, __DRIscreen
*psc
,
456 int numConfigs
, __GLXvisualConfig
*config
)
458 __DRIscreenPrivate
*psp
;
459 psp
= __driUtilCreateScreen(dpy
, scrn
, psc
, numConfigs
, config
, &mach64API
);
463 void *__driCreateScreen(struct DRIDriverRec
*driver
,
464 struct DRIDriverContextRec
*driverContext
)
466 __DRIscreenPrivate
*psp
;
467 psp
= __driUtilCreateScreen(driver
, driverContext
, &mach64API
);
471 #endif /* !defined(DRI_NEW_INTERFACE_ONLY) */
474 * This is the bootstrap function for the driver. libGL supplies all of the
475 * requisite information about the system, and the driver initializes itself.
476 * This routine also fills in the linked list pointed to by \c driver_modes
477 * with the \c __GLcontextModes that the driver can support for windows or
480 * \return A pointer to a \c __DRIscreenPrivate on success, or \c NULL on
483 #ifdef USE_NEW_INTERFACE
484 void * __driCreateNewScreen( __DRInativeDisplay
*dpy
, int scrn
, __DRIscreen
*psc
,
485 const __GLcontextModes
* modes
,
486 const __DRIversion
* ddx_version
,
487 const __DRIversion
* dri_version
,
488 const __DRIversion
* drm_version
,
489 const __DRIframebuffer
* frame_buffer
,
490 drmAddress pSAREA
, int fd
,
491 int internal_api_version
,
492 __GLcontextModes
** driver_modes
)
495 __DRIscreenPrivate
*psp
;
496 static const __DRIversion ddx_expected
= { 4, 0, 0 };
497 static const __DRIversion dri_expected
= { 6, 4, 0 };
498 static const __DRIversion drm_expected
= { 1, 0, 0 };
500 if ( ! driCheckDriDdxDrmVersions2( "Mach64",
501 dri_version
, & dri_expected
,
502 ddx_version
, & ddx_expected
,
503 drm_version
, & drm_expected
) ) {
507 psp
= __driUtilCreateNewScreen(dpy
, scrn
, psc
, NULL
,
508 ddx_version
, dri_version
, drm_version
,
509 frame_buffer
, pSAREA
, fd
,
510 internal_api_version
, &mach64API
);
512 create_context_modes
= (PFNGLXCREATECONTEXTMODES
)
513 glXGetProcAddress( (const GLubyte
*) "__glXCreateContextModes" );
514 if ( create_context_modes
!= NULL
) {
515 ATIDRIPtr dri_priv
= (ATIDRIPtr
) psp
->pDevPriv
;
516 *driver_modes
= mach64FillInModes( dri_priv
->cpp
* 8,
525 #endif /* USE_NEW_INTERFACE */