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"
43 #include "GL/internal/dri_interface.h"
45 /* Mach64 configuration
49 const char __driConfigOptions
[] =
51 DRI_CONF_SECTION_PERFORMANCE
52 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_0
)
55 DRI_CONF_SECTION_DEBUG
56 DRI_CONF_PERFORMANCE_BOXES(false)
61 static const GLuint __driNConfigOptions
= 2;
63 static const GLuint __driNConfigOptions
= 1;
66 #ifdef USE_NEW_INTERFACE
67 static PFNGLXCREATECONTEXTMODES create_context_modes
= NULL
;
68 #endif /* USE_NEW_INTERFACE */
70 #ifdef USE_NEW_INTERFACE
71 static __GLcontextModes
* fill_in_modes( __GLcontextModes
* modes
,
74 unsigned stencil_bits
,
75 const GLenum
* db_modes
,
76 unsigned num_db_modes
,
79 static const uint8_t bits
[2][4] = {
84 static const uint32_t masks
[2][4] = {
85 { 0x0000F800, 0x000007E0, 0x0000001F, 0x00000000 },
86 { 0x00FF0000, 0x0000FF00, 0x000000FF, 0x00000000 }
91 const unsigned index
= ((pixel_bits
+ 15) / 16) - 1;
93 for ( i
= 0 ; i
< num_db_modes
; i
++ ) {
94 for ( j
= 0 ; j
< 2 ; j
++ ) {
96 modes
->redBits
= bits
[index
][0];
97 modes
->greenBits
= bits
[index
][1];
98 modes
->blueBits
= bits
[index
][2];
99 modes
->alphaBits
= bits
[index
][3];
100 modes
->redMask
= masks
[index
][0];
101 modes
->greenMask
= masks
[index
][1];
102 modes
->blueMask
= masks
[index
][2];
103 modes
->alphaMask
= masks
[index
][3];
104 modes
->rgbBits
= modes
->redBits
+ modes
->greenBits
105 + modes
->blueBits
+ modes
->alphaBits
;
107 modes
->accumRedBits
= 16 * j
;
108 modes
->accumGreenBits
= 16 * j
;
109 modes
->accumBlueBits
= 16 * j
;
110 modes
->accumAlphaBits
= 0;
111 modes
->visualRating
= (j
== 0) ? GLX_NONE
: GLX_SLOW_CONFIG
;
112 modes
->drawableType
= GLX_WINDOW_BIT
| GLX_PIXMAP_BIT
;
113 modes
->stencilBits
= stencil_bits
;
114 modes
->depthBits
= depth_bits
;
116 modes
->visualType
= visType
;
117 modes
->renderType
= GLX_RGBA_BIT
;
118 modes
->rgbMode
= GL_TRUE
;
120 if ( db_modes
[i
] == GLX_NONE
) {
122 modes
->doubleBufferMode
= GL_FALSE
;
125 modes
->doubleBufferMode
= GL_TRUE
;
126 modes
->swapMethod
= db_modes
[i
];
135 #endif /* USE_NEW_INTERFACE */
138 #ifdef USE_NEW_INTERFACE
139 static __GLcontextModes
*
140 mach64FillInModes( unsigned pixel_bits
, unsigned depth_bits
,
141 unsigned stencil_bits
, GLboolean have_back_buffer
)
143 __GLcontextModes
* modes
;
144 __GLcontextModes
* m
;
146 unsigned depth_buffer_factor
;
147 unsigned back_buffer_factor
;
150 /* Right now GLX_SWAP_COPY_OML isn't supported, but it would be easy
151 * enough to add support. Basically, if a context is created with an
152 * fbconfig where the swap method is GLX_SWAP_COPY_OML, pageflipping
153 * will never be used.
155 static const GLenum back_buffer_modes
[] = {
156 GLX_NONE
, GLX_SWAP_UNDEFINED_OML
/*, GLX_SWAP_COPY_OML */
159 int depth_buffer_modes
[2][2];
162 depth_buffer_modes
[0][0] = depth_bits
;
163 depth_buffer_modes
[1][0] = depth_bits
;
165 /* Just like with the accumulation buffer, always provide some modes
166 * with a stencil buffer. It will be a sw fallback, but some apps won't
169 depth_buffer_modes
[0][1] = 0;
170 depth_buffer_modes
[1][1] = (stencil_bits
== 0) ? 8 : stencil_bits
;
172 depth_buffer_factor
= ((depth_bits
!= 0) || (stencil_bits
!= 0)) ? 2 : 1;
173 back_buffer_factor
= (have_back_buffer
) ? 2 : 1;
175 num_modes
= depth_buffer_factor
* back_buffer_factor
* 4;
177 modes
= (*create_context_modes
)( num_modes
, sizeof( __GLcontextModes
) );
179 for ( i
= 0 ; i
< depth_buffer_factor
; i
++ ) {
180 m
= fill_in_modes( m
, pixel_bits
,
181 depth_buffer_modes
[i
][0], depth_buffer_modes
[i
][1],
182 back_buffer_modes
, back_buffer_factor
,
186 for ( i
= 0 ; i
< depth_buffer_factor
; i
++ ) {
187 m
= fill_in_modes( m
, pixel_bits
,
188 depth_buffer_modes
[i
][0], depth_buffer_modes
[i
][1],
189 back_buffer_modes
, back_buffer_factor
,
193 /* Mark the visual as slow if there are "fake" stencil bits.
195 for ( m
= modes
; m
!= NULL
; m
= m
->next
) {
196 if ( (m
->stencilBits
!= 0) && (m
->stencilBits
!= stencil_bits
) ){
197 m
->visualRating
= GLX_SLOW_CONFIG
;
203 #endif /* USE_NEW_INTERFACE */
206 /* Create the device specific screen private data struct.
208 static mach64ScreenRec
*
209 mach64CreateScreen( __DRIscreenPrivate
*sPriv
)
211 mach64ScreenPtr mach64Screen
;
212 ATIDRIPtr serverInfo
= (ATIDRIPtr
)sPriv
->pDevPriv
;
214 if ( MACH64_DEBUG
& DEBUG_VERBOSE_DRI
)
215 fprintf( stderr
, "%s\n", __FUNCTION__
);
217 /* Allocate the private area */
218 mach64Screen
= (mach64ScreenPtr
) CALLOC( sizeof(*mach64Screen
) );
219 if ( !mach64Screen
) return NULL
;
221 /* parse information in __driConfigOptions */
222 driParseOptionInfo (&mach64Screen
->optionCache
,
223 __driConfigOptions
, __driNConfigOptions
);
225 mach64Screen
->IsPCI
= serverInfo
->IsPCI
;
228 drm_mach64_getparam_t gp
;
231 gp
.param
= MACH64_PARAM_IRQ_NR
;
232 gp
.value
= (void *) &mach64Screen
->irq
;
234 ret
= drmCommandWriteRead( sPriv
->fd
, DRM_MACH64_GETPARAM
,
237 fprintf(stderr
, "DRM_MACH64_GETPARAM (MACH64_PARAM_IRQ_NR): %d\n", ret
);
238 FREE( mach64Screen
);
243 mach64Screen
->mmio
.handle
= serverInfo
->regs
;
244 mach64Screen
->mmio
.size
= serverInfo
->regsSize
;
245 if ( drmMap( sPriv
->fd
,
246 mach64Screen
->mmio
.handle
,
247 mach64Screen
->mmio
.size
,
248 (drmAddressPtr
)&mach64Screen
->mmio
.map
) != 0 ) {
249 FREE( mach64Screen
);
253 mach64Screen
->buffers
= drmMapBufs( sPriv
->fd
);
254 if ( !mach64Screen
->buffers
) {
255 drmUnmap( (drmAddress
)mach64Screen
->mmio
.map
,
256 mach64Screen
->mmio
.size
);
257 FREE( mach64Screen
);
261 if ( !mach64Screen
->IsPCI
) {
262 mach64Screen
->agpTextures
.handle
= serverInfo
->agp
;
263 mach64Screen
->agpTextures
.size
= serverInfo
->agpSize
;
264 if ( drmMap( sPriv
->fd
,
265 mach64Screen
->agpTextures
.handle
,
266 mach64Screen
->agpTextures
.size
,
267 (drmAddressPtr
)&mach64Screen
->agpTextures
.map
) ) {
268 drmUnmapBufs( mach64Screen
->buffers
);
269 drmUnmap( (drmAddress
)mach64Screen
->mmio
.map
, mach64Screen
->mmio
.size
);
270 FREE( mach64Screen
);
275 mach64Screen
->AGPMode
= serverInfo
->AGPMode
;
277 mach64Screen
->chipset
= serverInfo
->chipset
;
278 mach64Screen
->width
= serverInfo
->width
;
279 mach64Screen
->height
= serverInfo
->height
;
280 mach64Screen
->mem
= serverInfo
->mem
;
281 mach64Screen
->cpp
= serverInfo
->cpp
;
283 mach64Screen
->frontOffset
= serverInfo
->frontOffset
;
284 mach64Screen
->frontPitch
= serverInfo
->frontPitch
;
285 mach64Screen
->backOffset
= serverInfo
->backOffset
;
286 mach64Screen
->backPitch
= serverInfo
->backPitch
;
287 mach64Screen
->depthOffset
= serverInfo
->depthOffset
;
288 mach64Screen
->depthPitch
= serverInfo
->depthPitch
;
290 mach64Screen
->texOffset
[MACH64_CARD_HEAP
] = serverInfo
->textureOffset
;
291 mach64Screen
->texSize
[MACH64_CARD_HEAP
] = serverInfo
->textureSize
;
292 mach64Screen
->logTexGranularity
[MACH64_CARD_HEAP
] =
293 serverInfo
->logTextureGranularity
;
295 if ( mach64Screen
->IsPCI
) {
296 mach64Screen
->numTexHeaps
= MACH64_NR_TEX_HEAPS
- 1;
297 mach64Screen
->firstTexHeap
= MACH64_CARD_HEAP
;
298 mach64Screen
->texOffset
[MACH64_AGP_HEAP
] = 0;
299 mach64Screen
->texSize
[MACH64_AGP_HEAP
] = 0;
300 mach64Screen
->logTexGranularity
[MACH64_AGP_HEAP
] = 0;
302 if (mach64Screen
->texSize
[MACH64_CARD_HEAP
] > 0) {
303 mach64Screen
->numTexHeaps
= MACH64_NR_TEX_HEAPS
;
304 mach64Screen
->firstTexHeap
= MACH64_CARD_HEAP
;
306 mach64Screen
->numTexHeaps
= MACH64_NR_TEX_HEAPS
- 1;
307 mach64Screen
->firstTexHeap
= MACH64_AGP_HEAP
;
309 mach64Screen
->texOffset
[MACH64_AGP_HEAP
] = serverInfo
->agpTextureOffset
;
310 mach64Screen
->texSize
[MACH64_AGP_HEAP
] = serverInfo
->agpSize
;
311 mach64Screen
->logTexGranularity
[MACH64_AGP_HEAP
] = serverInfo
->logAgpTextureGranularity
;
314 mach64Screen
->driScreen
= sPriv
;
315 if ( driCompareGLXAPIVersion( 20030813 ) >= 0 ) {
316 PFNGLXSCRENABLEEXTENSIONPROC glx_enable_extension
=
317 (PFNGLXSCRENABLEEXTENSIONPROC
) glXGetProcAddress( (const GLubyte
*) "__glXScrEnableExtension" );
318 void * const psc
= sPriv
->psc
->screenConfigs
;
320 if ( glx_enable_extension
!= NULL
) {
321 if ( mach64Screen
->irq
!= 0 ) {
322 (*glx_enable_extension
)( psc
, "GLX_SGI_swap_control" );
323 (*glx_enable_extension
)( psc
, "GLX_SGI_video_sync" );
324 (*glx_enable_extension
)( psc
, "GLX_MESA_swap_control" );
327 (*glx_enable_extension
)( psc
, "GLX_MESA_swap_frame_usage" );
333 /* Destroy the device specific screen private data struct.
336 mach64DestroyScreen( __DRIscreenPrivate
*driScreen
)
338 mach64ScreenRec
*mach64Screen
= (mach64ScreenRec
*) driScreen
->private;
343 if ( MACH64_DEBUG
& DEBUG_VERBOSE_DRI
)
344 fprintf( stderr
, "%s\n", __FUNCTION__
);
346 if ( !mach64Screen
->IsPCI
) {
347 drmUnmap( (drmAddress
)mach64Screen
->agpTextures
.map
,
348 mach64Screen
->agpTextures
.size
);
351 drmUnmapBufs( mach64Screen
->buffers
);
352 drmUnmap( (drmAddress
)mach64Screen
->mmio
.map
, mach64Screen
->mmio
.size
);
354 FREE( mach64Screen
);
355 driScreen
->private = NULL
;
358 /* Create and initialize the Mesa and driver specific pixmap buffer
362 mach64CreateBuffer( __DRIscreenPrivate
*driScrnPriv
,
363 __DRIdrawablePrivate
*driDrawPriv
,
364 const __GLcontextModes
*mesaVis
,
368 return GL_FALSE
; /* not implemented */
371 driDrawPriv
->driverPrivate
= (void *)
372 _mesa_create_framebuffer( mesaVis
,
373 GL_FALSE
, /* software depth buffer? */
374 mesaVis
->stencilBits
> 0,
375 mesaVis
->accumRedBits
> 0,
376 mesaVis
->alphaBits
> 0 );
377 return (driDrawPriv
->driverPrivate
!= NULL
);
383 mach64DestroyBuffer(__DRIdrawablePrivate
*driDrawPriv
)
385 _mesa_destroy_framebuffer((GLframebuffer
*) (driDrawPriv
->driverPrivate
));
389 /* Copy the back color buffer to the front color buffer */
391 mach64SwapBuffers(__DRIdrawablePrivate
*dPriv
)
393 if (dPriv
->driContextPriv
&& dPriv
->driContextPriv
->driverPrivate
) {
394 mach64ContextPtr mmesa
;
396 mmesa
= (mach64ContextPtr
) dPriv
->driContextPriv
->driverPrivate
;
398 if (ctx
->Visual
.doubleBufferMode
) {
399 _mesa_notifySwapBuffers( ctx
); /* flush pending rendering comands */
400 mach64CopyBuffer( dPriv
);
404 /* XXX this shouldn't be an error but we can't handle it for now */
405 _mesa_problem(NULL
, "%s: drawable has no context!", __FUNCTION__
);
410 /* Initialize the driver specific screen private data.
413 mach64InitDriver( __DRIscreenPrivate
*driScreen
)
415 driScreen
->private = (void *) mach64CreateScreen( driScreen
);
417 if ( !driScreen
->private ) {
418 mach64DestroyScreen( driScreen
);
426 static struct __DriverAPIRec mach64API
= {
427 .InitDriver
= mach64InitDriver
,
428 .DestroyScreen
= mach64DestroyScreen
,
429 .CreateContext
= mach64CreateContext
,
430 .DestroyContext
= mach64DestroyContext
,
431 .CreateBuffer
= mach64CreateBuffer
,
432 .DestroyBuffer
= mach64DestroyBuffer
,
433 .SwapBuffers
= mach64SwapBuffers
,
434 .MakeCurrent
= mach64MakeCurrent
,
435 .UnbindContext
= mach64UnbindContext
,
437 .GetMSC
= driGetMSC32
,
438 .WaitForMSC
= driWaitForMSC32
,
440 .SwapBuffersMSC
= NULL
445 * This is the bootstrap function for the driver.
446 * The __driCreateScreen name is the symbol that libGL.so fetches.
447 * Return: pointer to a __DRIscreenPrivate.
449 #if !defined(DRI_NEW_INTERFACE_ONLY)
450 void *__driCreateScreen(Display
*dpy
, int scrn
, __DRIscreen
*psc
,
451 int numConfigs
, __GLXvisualConfig
*config
)
453 __DRIscreenPrivate
*psp
;
454 psp
= __driUtilCreateScreen(dpy
, scrn
, psc
, numConfigs
, config
, &mach64API
);
457 #endif /* !defined(DRI_NEW_INTERFACE_ONLY) */
460 * This is the bootstrap function for the driver. libGL supplies all of the
461 * requisite information about the system, and the driver initializes itself.
462 * This routine also fills in the linked list pointed to by \c driver_modes
463 * with the \c __GLcontextModes that the driver can support for windows or
466 * \return A pointer to a \c __DRIscreenPrivate on success, or \c NULL on
469 #ifdef USE_NEW_INTERFACE
470 void * __driCreateNewScreen( __DRInativeDisplay
*dpy
, int scrn
, __DRIscreen
*psc
,
471 const __GLcontextModes
* modes
,
472 const __DRIversion
* ddx_version
,
473 const __DRIversion
* dri_version
,
474 const __DRIversion
* drm_version
,
475 const __DRIframebuffer
* frame_buffer
,
476 drmAddress pSAREA
, int fd
,
477 int internal_api_version
,
478 __GLcontextModes
** driver_modes
)
481 __DRIscreenPrivate
*psp
;
482 static const __DRIversion ddx_expected
= { 6, 4, 0 };
483 static const __DRIversion dri_expected
= { 4, 0, 0 };
484 static const __DRIversion drm_expected
= { 1, 0, 0 };
486 if ( ! driCheckDriDdxDrmVersions2( "Mach64",
487 dri_version
, & dri_expected
,
488 ddx_version
, & ddx_expected
,
489 drm_version
, & drm_expected
) ) {
493 psp
= __driUtilCreateNewScreen(dpy
, scrn
, psc
, NULL
,
494 ddx_version
, dri_version
, drm_version
,
495 frame_buffer
, pSAREA
, fd
,
496 internal_api_version
, &mach64API
);
498 create_context_modes
= (PFNGLXCREATECONTEXTMODES
)
499 glXGetProcAddress( (const GLubyte
*) "__glXCreateContextModes" );
500 if ( create_context_modes
!= NULL
) {
501 ATIDRIPtr dri_priv
= (ATIDRIPtr
) psp
->pDevPriv
;
502 *driver_modes
= mach64FillInModes( dri_priv
->cpp
* 8,
511 #endif /* USE_NEW_INTERFACE */