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"
36 #include "mach64_span.h"
40 #include "framebuffer.h"
41 #include "renderbuffer.h"
46 #include "GL/internal/dri_interface.h"
48 /* Mach64 configuration
52 PUBLIC
const char __driConfigOptions
[] =
54 DRI_CONF_SECTION_PERFORMANCE
55 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_0
)
57 DRI_CONF_SECTION_DEBUG
58 DRI_CONF_NO_RAST(false)
60 DRI_CONF_PERFORMANCE_BOXES(false)
65 static const GLuint __driNConfigOptions
= 3;
67 static const GLuint __driNConfigOptions
= 2;
70 extern const struct dri_extension card_extensions
[];
72 static __GLcontextModes
* fill_in_modes( __GLcontextModes
* modes
,
75 unsigned stencil_bits
,
76 const GLenum
* db_modes
,
77 unsigned num_db_modes
,
80 static const u_int8_t bits
[2][4] = {
85 static const u_int32_t masks
[2][4] = {
86 { 0x0000F800, 0x000007E0, 0x0000001F, 0x00000000 },
87 { 0x00FF0000, 0x0000FF00, 0x000000FF, 0x00000000 }
92 const unsigned index
= ((pixel_bits
+ 15) / 16) - 1;
94 for ( i
= 0 ; i
< num_db_modes
; i
++ ) {
95 for ( j
= 0 ; j
< 2 ; j
++ ) {
97 modes
->redBits
= bits
[index
][0];
98 modes
->greenBits
= bits
[index
][1];
99 modes
->blueBits
= bits
[index
][2];
100 modes
->alphaBits
= bits
[index
][3];
101 modes
->redMask
= masks
[index
][0];
102 modes
->greenMask
= masks
[index
][1];
103 modes
->blueMask
= masks
[index
][2];
104 modes
->alphaMask
= masks
[index
][3];
105 modes
->rgbBits
= modes
->redBits
+ modes
->greenBits
106 + modes
->blueBits
+ modes
->alphaBits
;
108 modes
->accumRedBits
= 16 * j
;
109 modes
->accumGreenBits
= 16 * j
;
110 modes
->accumBlueBits
= 16 * j
;
111 modes
->accumAlphaBits
= 0;
112 modes
->visualRating
= (j
== 0) ? GLX_NONE
: GLX_SLOW_CONFIG
;
113 modes
->drawableType
= GLX_WINDOW_BIT
| GLX_PIXMAP_BIT
;
114 modes
->stencilBits
= stencil_bits
;
115 modes
->depthBits
= depth_bits
;
117 modes
->visualType
= visType
;
118 modes
->renderType
= GLX_RGBA_BIT
;
119 modes
->rgbMode
= GL_TRUE
;
121 if ( db_modes
[i
] == GLX_NONE
) {
123 modes
->doubleBufferMode
= GL_FALSE
;
126 modes
->doubleBufferMode
= GL_TRUE
;
127 modes
->swapMethod
= db_modes
[i
];
138 static __GLcontextModes
*
139 mach64FillInModes( unsigned pixel_bits
, unsigned depth_bits
,
140 unsigned stencil_bits
, GLboolean have_back_buffer
)
142 __GLcontextModes
* modes
;
143 __GLcontextModes
* m
;
145 unsigned depth_buffer_factor
;
146 unsigned back_buffer_factor
;
149 /* Right now GLX_SWAP_COPY_OML isn't supported, but it would be easy
150 * enough to add support. Basically, if a context is created with an
151 * fbconfig where the swap method is GLX_SWAP_COPY_OML, pageflipping
152 * will never be used.
154 static const GLenum back_buffer_modes
[] = {
155 GLX_NONE
, GLX_SWAP_UNDEFINED_OML
/*, GLX_SWAP_COPY_OML */
158 int depth_buffer_modes
[2][2];
161 depth_buffer_modes
[0][0] = depth_bits
;
162 depth_buffer_modes
[1][0] = depth_bits
;
164 /* Just like with the accumulation buffer, always provide some modes
165 * with a stencil buffer. It will be a sw fallback, but some apps won't
168 depth_buffer_modes
[0][1] = 0;
169 depth_buffer_modes
[1][1] = (stencil_bits
== 0) ? 8 : stencil_bits
;
171 depth_buffer_factor
= ((depth_bits
!= 0) || (stencil_bits
!= 0)) ? 2 : 1;
172 back_buffer_factor
= (have_back_buffer
) ? 2 : 1;
174 num_modes
= depth_buffer_factor
* back_buffer_factor
* 4;
176 modes
= (*dri_interface
->createContextModes
)( num_modes
, sizeof( __GLcontextModes
) );
178 for ( i
= 0 ; i
< depth_buffer_factor
; i
++ ) {
179 m
= fill_in_modes( m
, pixel_bits
,
180 depth_buffer_modes
[i
][0], depth_buffer_modes
[i
][1],
181 back_buffer_modes
, back_buffer_factor
,
185 for ( i
= 0 ; i
< depth_buffer_factor
; i
++ ) {
186 m
= fill_in_modes( m
, pixel_bits
,
187 depth_buffer_modes
[i
][0], depth_buffer_modes
[i
][1],
188 back_buffer_modes
, back_buffer_factor
,
192 /* Mark the visual as slow if there are "fake" stencil bits.
194 for ( m
= modes
; m
!= NULL
; m
= m
->next
) {
195 if ( (m
->stencilBits
!= 0) && (m
->stencilBits
!= stencil_bits
) ){
196 m
->visualRating
= GLX_SLOW_CONFIG
;
204 /* Create the device specific screen private data struct.
206 static mach64ScreenRec
*
207 mach64CreateScreen( __DRIscreenPrivate
*sPriv
)
209 mach64ScreenPtr mach64Screen
;
210 ATIDRIPtr serverInfo
= (ATIDRIPtr
)sPriv
->pDevPriv
;
211 PFNGLXSCRENABLEEXTENSIONPROC glx_enable_extension
=
212 (PFNGLXSCRENABLEEXTENSIONPROC
) (*dri_interface
->getProcAddress("glxEnableExtension"));
213 void * const psc
= sPriv
->psc
->screenConfigs
;
215 if (sPriv
->devPrivSize
!= sizeof(ATIDRIRec
)) {
216 fprintf(stderr
,"\nERROR! sizeof(ATIDRIRec) does not match passed size from device driver\n");
220 if ( MACH64_DEBUG
& DEBUG_VERBOSE_DRI
)
221 fprintf( stderr
, "%s\n", __FUNCTION__
);
223 /* Allocate the private area */
224 mach64Screen
= (mach64ScreenPtr
) CALLOC( sizeof(*mach64Screen
) );
225 if ( !mach64Screen
) return NULL
;
227 /* parse information in __driConfigOptions */
228 driParseOptionInfo (&mach64Screen
->optionCache
,
229 __driConfigOptions
, __driNConfigOptions
);
231 mach64Screen
->IsPCI
= serverInfo
->IsPCI
;
234 drm_mach64_getparam_t gp
;
237 gp
.param
= MACH64_PARAM_IRQ_NR
;
238 gp
.value
= (void *) &mach64Screen
->irq
;
240 ret
= drmCommandWriteRead( sPriv
->fd
, DRM_MACH64_GETPARAM
,
243 fprintf(stderr
, "DRM_MACH64_GETPARAM (MACH64_PARAM_IRQ_NR): %d\n", ret
);
244 FREE( mach64Screen
);
249 mach64Screen
->mmio
.handle
= serverInfo
->regs
;
250 mach64Screen
->mmio
.size
= serverInfo
->regsSize
;
251 if ( drmMap( sPriv
->fd
,
252 mach64Screen
->mmio
.handle
,
253 mach64Screen
->mmio
.size
,
254 (drmAddressPtr
)&mach64Screen
->mmio
.map
) != 0 ) {
255 FREE( mach64Screen
);
259 mach64Screen
->buffers
= drmMapBufs( sPriv
->fd
);
260 if ( !mach64Screen
->buffers
) {
261 drmUnmap( (drmAddress
)mach64Screen
->mmio
.map
,
262 mach64Screen
->mmio
.size
);
263 FREE( mach64Screen
);
267 if ( !mach64Screen
->IsPCI
) {
268 mach64Screen
->agpTextures
.handle
= serverInfo
->agp
;
269 mach64Screen
->agpTextures
.size
= serverInfo
->agpSize
;
270 if ( drmMap( sPriv
->fd
,
271 mach64Screen
->agpTextures
.handle
,
272 mach64Screen
->agpTextures
.size
,
273 (drmAddressPtr
)&mach64Screen
->agpTextures
.map
) ) {
274 drmUnmapBufs( mach64Screen
->buffers
);
275 drmUnmap( (drmAddress
)mach64Screen
->mmio
.map
, mach64Screen
->mmio
.size
);
276 FREE( mach64Screen
);
281 mach64Screen
->AGPMode
= serverInfo
->AGPMode
;
283 mach64Screen
->chipset
= serverInfo
->chipset
;
284 mach64Screen
->width
= serverInfo
->width
;
285 mach64Screen
->height
= serverInfo
->height
;
286 mach64Screen
->mem
= serverInfo
->mem
;
287 mach64Screen
->cpp
= serverInfo
->cpp
;
289 mach64Screen
->frontOffset
= serverInfo
->frontOffset
;
290 mach64Screen
->frontPitch
= serverInfo
->frontPitch
;
291 mach64Screen
->backOffset
= serverInfo
->backOffset
;
292 mach64Screen
->backPitch
= serverInfo
->backPitch
;
293 mach64Screen
->depthOffset
= serverInfo
->depthOffset
;
294 mach64Screen
->depthPitch
= serverInfo
->depthPitch
;
296 mach64Screen
->texOffset
[MACH64_CARD_HEAP
] = serverInfo
->textureOffset
;
297 mach64Screen
->texSize
[MACH64_CARD_HEAP
] = serverInfo
->textureSize
;
298 mach64Screen
->logTexGranularity
[MACH64_CARD_HEAP
] =
299 serverInfo
->logTextureGranularity
;
301 if ( mach64Screen
->IsPCI
) {
302 mach64Screen
->numTexHeaps
= MACH64_NR_TEX_HEAPS
- 1;
303 mach64Screen
->firstTexHeap
= MACH64_CARD_HEAP
;
304 mach64Screen
->texOffset
[MACH64_AGP_HEAP
] = 0;
305 mach64Screen
->texSize
[MACH64_AGP_HEAP
] = 0;
306 mach64Screen
->logTexGranularity
[MACH64_AGP_HEAP
] = 0;
308 if (serverInfo
->textureSize
> 0) {
309 mach64Screen
->numTexHeaps
= MACH64_NR_TEX_HEAPS
;
310 mach64Screen
->firstTexHeap
= MACH64_CARD_HEAP
;
312 mach64Screen
->numTexHeaps
= MACH64_NR_TEX_HEAPS
- 1;
313 mach64Screen
->firstTexHeap
= MACH64_AGP_HEAP
;
315 mach64Screen
->texOffset
[MACH64_AGP_HEAP
] = serverInfo
->agpTextureOffset
;
316 mach64Screen
->texSize
[MACH64_AGP_HEAP
] = serverInfo
->agpSize
;
317 mach64Screen
->logTexGranularity
[MACH64_AGP_HEAP
] = serverInfo
->logAgpTextureGranularity
;
320 mach64Screen
->driScreen
= sPriv
;
322 if ( glx_enable_extension
!= NULL
) {
323 if ( mach64Screen
->irq
!= 0 ) {
324 (*glx_enable_extension
)( psc
, "GLX_SGI_swap_control" );
325 (*glx_enable_extension
)( psc
, "GLX_SGI_video_sync" );
326 (*glx_enable_extension
)( psc
, "GLX_MESA_swap_control" );
329 (*glx_enable_extension
)( psc
, "GLX_MESA_swap_frame_usage" );
335 /* Destroy the device specific screen private data struct.
338 mach64DestroyScreen( __DRIscreenPrivate
*driScreen
)
340 mach64ScreenRec
*mach64Screen
= (mach64ScreenRec
*) driScreen
->private;
345 if ( MACH64_DEBUG
& DEBUG_VERBOSE_DRI
)
346 fprintf( stderr
, "%s\n", __FUNCTION__
);
348 if ( !mach64Screen
->IsPCI
) {
349 drmUnmap( (drmAddress
)mach64Screen
->agpTextures
.map
,
350 mach64Screen
->agpTextures
.size
);
353 drmUnmapBufs( mach64Screen
->buffers
);
354 drmUnmap( (drmAddress
)mach64Screen
->mmio
.map
, mach64Screen
->mmio
.size
);
356 FREE( mach64Screen
);
357 driScreen
->private = NULL
;
361 /* Create and initialize the Mesa and driver specific pixmap buffer
365 mach64CreateBuffer( __DRIscreenPrivate
*driScrnPriv
,
366 __DRIdrawablePrivate
*driDrawPriv
,
367 const __GLcontextModes
*mesaVis
,
370 mach64ScreenPtr screen
= (mach64ScreenPtr
) driScrnPriv
->private;
373 return GL_FALSE
; /* not implemented */
376 struct gl_framebuffer
*fb
= _mesa_create_framebuffer(mesaVis
);
379 driRenderbuffer
*frontRb
380 = driNewRenderbuffer(GL_RGBA
,
383 screen
->frontOffset
, screen
->frontPitch
,
385 mach64SetSpanFunctions(frontRb
, mesaVis
);
386 _mesa_add_renderbuffer(fb
, BUFFER_FRONT_LEFT
, &frontRb
->Base
);
389 if (mesaVis
->doubleBufferMode
) {
390 driRenderbuffer
*backRb
391 = driNewRenderbuffer(GL_RGBA
,
394 screen
->backOffset
, screen
->backPitch
,
396 mach64SetSpanFunctions(backRb
, mesaVis
);
397 _mesa_add_renderbuffer(fb
, BUFFER_BACK_LEFT
, &backRb
->Base
);
400 if (mesaVis
->depthBits
== 16) {
401 driRenderbuffer
*depthRb
402 = driNewRenderbuffer(GL_DEPTH_COMPONENT16
,
404 screen
->depthOffset
, screen
->depthPitch
,
406 mach64SetSpanFunctions(depthRb
, mesaVis
);
407 _mesa_add_renderbuffer(fb
, BUFFER_DEPTH
, &depthRb
->Base
);
409 else if (mesaVis
->depthBits
== 24) {
410 /* XXX I don't think 24-bit Z is supported - so this isn't used */
411 driRenderbuffer
*depthRb
412 = driNewRenderbuffer(GL_DEPTH_COMPONENT24
,
415 screen
->depthOffset
, screen
->depthPitch
,
417 mach64SetSpanFunctions(depthRb
, mesaVis
);
418 _mesa_add_renderbuffer(fb
, BUFFER_DEPTH
, &depthRb
->Base
);
421 _mesa_add_soft_renderbuffers(fb
,
422 GL_FALSE
, /* color */
423 GL_FALSE
, /* depth */
424 mesaVis
->stencilBits
> 0,
425 mesaVis
->accumRedBits
> 0,
426 GL_FALSE
, /* alpha */
428 driDrawPriv
->driverPrivate
= (void *) fb
;
430 return (driDrawPriv
->driverPrivate
!= NULL
);
436 mach64DestroyBuffer(__DRIdrawablePrivate
*driDrawPriv
)
438 _mesa_unreference_framebuffer((GLframebuffer
**)(&(driDrawPriv
->driverPrivate
)));
442 /* Copy the back color buffer to the front color buffer */
444 mach64SwapBuffers(__DRIdrawablePrivate
*dPriv
)
446 if (dPriv
->driContextPriv
&& dPriv
->driContextPriv
->driverPrivate
) {
447 mach64ContextPtr mmesa
;
449 mmesa
= (mach64ContextPtr
) dPriv
->driContextPriv
->driverPrivate
;
451 if (ctx
->Visual
.doubleBufferMode
) {
452 _mesa_notifySwapBuffers( ctx
); /* flush pending rendering comands */
453 mach64CopyBuffer( dPriv
);
457 /* XXX this shouldn't be an error but we can't handle it for now */
458 _mesa_problem(NULL
, "%s: drawable has no context!", __FUNCTION__
);
463 /* Initialize the driver specific screen private data.
466 mach64InitDriver( __DRIscreenPrivate
*driScreen
)
468 driScreen
->private = (void *) mach64CreateScreen( driScreen
);
470 if ( !driScreen
->private ) {
471 mach64DestroyScreen( driScreen
);
479 static struct __DriverAPIRec mach64API
= {
480 .InitDriver
= mach64InitDriver
,
481 .DestroyScreen
= mach64DestroyScreen
,
482 .CreateContext
= mach64CreateContext
,
483 .DestroyContext
= mach64DestroyContext
,
484 .CreateBuffer
= mach64CreateBuffer
,
485 .DestroyBuffer
= mach64DestroyBuffer
,
486 .SwapBuffers
= mach64SwapBuffers
,
487 .MakeCurrent
= mach64MakeCurrent
,
488 .UnbindContext
= mach64UnbindContext
,
490 .GetMSC
= driGetMSC32
,
491 .WaitForMSC
= driWaitForMSC32
,
493 .SwapBuffersMSC
= NULL
498 * This is the bootstrap function for the driver. libGL supplies all of the
499 * requisite information about the system, and the driver initializes itself.
500 * This routine also fills in the linked list pointed to by \c driver_modes
501 * with the \c __GLcontextModes that the driver can support for windows or
504 * \return A pointer to a \c __DRIscreenPrivate on success, or \c NULL on
508 void * __driCreateNewScreen_20050727( __DRInativeDisplay
*dpy
, int scrn
, __DRIscreen
*psc
,
509 const __GLcontextModes
* modes
,
510 const __DRIversion
* ddx_version
,
511 const __DRIversion
* dri_version
,
512 const __DRIversion
* drm_version
,
513 const __DRIframebuffer
* frame_buffer
,
514 drmAddress pSAREA
, int fd
,
515 int internal_api_version
,
516 const __DRIinterfaceMethods
* interface
,
517 __GLcontextModes
** driver_modes
)
520 __DRIscreenPrivate
*psp
;
521 static const __DRIversion ddx_expected
= { 6, 4, 0 };
522 static const __DRIversion dri_expected
= { 4, 0, 0 };
523 static const __DRIversion drm_expected
= { 2, 0, 0 };
525 dri_interface
= interface
;
527 if ( ! driCheckDriDdxDrmVersions2( "Mach64",
528 dri_version
, & dri_expected
,
529 ddx_version
, & ddx_expected
,
530 drm_version
, & drm_expected
) ) {
534 psp
= __driUtilCreateNewScreen(dpy
, scrn
, psc
, NULL
,
535 ddx_version
, dri_version
, drm_version
,
536 frame_buffer
, pSAREA
, fd
,
537 internal_api_version
, &mach64API
);
539 ATIDRIPtr dri_priv
= (ATIDRIPtr
) psp
->pDevPriv
;
540 *driver_modes
= mach64FillInModes( dri_priv
->cpp
* 8,
545 /* Calling driInitExtensions here, with a NULL context pointer, does not actually
546 * enable the extensions. It just makes sure that all the dispatch offsets for all
547 * the extensions that *might* be enables are known. This is needed because the
548 * dispatch offsets need to be known when _mesa_context_create is called, but we can't
549 * enable the extensions until we have a context pointer.
551 * Hello chicken. Hello egg. How are you two today?
553 driInitExtensions( NULL
, card_extensions
, GL_FALSE
);