mesa: Enable remap table in core.
[mesa.git] / src / mesa / drivers / dri / mach64 / mach64_screen.c
1 /* -*- mode: c; c-basic-offset: 3 -*- */
2 /*
3 * Copyright 2000 Gareth Hughes
4 * All Rights Reserved.
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 (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
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.
23 */
24
25 /*
26 * Authors:
27 * Gareth Hughes <gareth@valinux.com>
28 * Leif Delgass <ldelgass@retinalburn.net>
29 * Jos�Fonseca <j_r_fonseca@yahoo.co.uk>
30 */
31
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"
37
38 #include "main/context.h"
39 #include "main/imports.h"
40 #include "main/framebuffer.h"
41 #include "main/renderbuffer.h"
42
43 #include "utils.h"
44 #include "vblank.h"
45
46 #include "GL/internal/dri_interface.h"
47
48 /* Mach64 configuration
49 */
50 #include "xmlpool.h"
51
52 PUBLIC const char __driConfigOptions[] =
53 DRI_CONF_BEGIN
54 DRI_CONF_SECTION_PERFORMANCE
55 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_0)
56 DRI_CONF_SECTION_END
57 DRI_CONF_SECTION_DEBUG
58 DRI_CONF_NO_RAST(false)
59 #if ENABLE_PERF_BOXES
60 DRI_CONF_PERFORMANCE_BOXES(false)
61 #endif
62 DRI_CONF_SECTION_END
63 DRI_CONF_END;
64 #if ENABLE_PERF_BOXES
65 static const GLuint __driNConfigOptions = 3;
66 #else
67 static const GLuint __driNConfigOptions = 2;
68 #endif
69
70 static const __DRIconfig **
71 mach64FillInModes( __DRIscreenPrivate *psp,
72 unsigned pixel_bits, unsigned depth_bits,
73 unsigned stencil_bits, GLboolean have_back_buffer )
74 {
75 __DRIconfig **configs;
76 __GLcontextModes * m;
77 GLenum fb_format;
78 GLenum fb_type;
79 unsigned depth_buffer_factor;
80 unsigned back_buffer_factor;
81 unsigned i;
82
83 /* Right now GLX_SWAP_COPY_OML isn't supported, but it would be easy
84 * enough to add support. Basically, if a context is created with an
85 * fbconfig where the swap method is GLX_SWAP_COPY_OML, pageflipping
86 * will never be used.
87 */
88 static const GLenum back_buffer_modes[] = {
89 GLX_NONE, GLX_SWAP_UNDEFINED_OML /*, GLX_SWAP_COPY_OML */
90 };
91
92 uint8_t depth_bits_array[2];
93 uint8_t stencil_bits_array[2];
94 uint8_t msaa_samples_array[1];
95
96 depth_bits_array[0] = depth_bits;
97 depth_bits_array[1] = depth_bits;
98
99 /* Just like with the accumulation buffer, always provide some modes
100 * with a stencil buffer. It will be a sw fallback, but some apps won't
101 * care about that.
102 */
103 stencil_bits_array[0] = 0;
104 stencil_bits_array[1] = (stencil_bits == 0) ? 8 : stencil_bits;
105
106 msaa_samples_array[0] = 0;
107
108 depth_buffer_factor = ((depth_bits != 0) || (stencil_bits != 0)) ? 2 : 1;
109 back_buffer_factor = (have_back_buffer) ? 2 : 1;
110
111 if (pixel_bits == 16) {
112 fb_format = GL_RGB;
113 fb_type = GL_UNSIGNED_SHORT_5_6_5;
114 }
115 else {
116 fb_format = GL_BGRA;
117 fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
118 }
119
120 configs = driCreateConfigs(fb_format, fb_type,
121 depth_bits_array, stencil_bits_array,
122 depth_buffer_factor, back_buffer_modes,
123 back_buffer_factor,
124 msaa_samples_array, 1);
125 if (configs == NULL) {
126 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n",
127 __func__, __LINE__);
128 return NULL;
129 }
130
131 /* Mark the visual as slow if there are "fake" stencil bits.
132 */
133 for (i = 0; configs[i]; i++) {
134 m = &configs[i]->modes;
135 if ((m->stencilBits != 0) && (m->stencilBits != stencil_bits)) {
136 m->visualRating = GLX_SLOW_CONFIG;
137 }
138 }
139
140 return (const __DRIconfig **) configs;
141 }
142
143
144 /* Create the device specific screen private data struct.
145 */
146 static mach64ScreenRec *
147 mach64CreateScreen( __DRIscreenPrivate *sPriv )
148 {
149 mach64ScreenPtr mach64Screen;
150 ATIDRIPtr serverInfo = (ATIDRIPtr)sPriv->pDevPriv;
151 int i;
152
153 if (sPriv->devPrivSize != sizeof(ATIDRIRec)) {
154 fprintf(stderr,"\nERROR! sizeof(ATIDRIRec) does not match passed size from device driver\n");
155 return GL_FALSE;
156 }
157
158 if ( MACH64_DEBUG & DEBUG_VERBOSE_DRI )
159 fprintf( stderr, "%s\n", __FUNCTION__ );
160
161 /* Allocate the private area */
162 mach64Screen = (mach64ScreenPtr) CALLOC( sizeof(*mach64Screen) );
163 if ( !mach64Screen ) return NULL;
164
165 /* parse information in __driConfigOptions */
166 driParseOptionInfo (&mach64Screen->optionCache,
167 __driConfigOptions, __driNConfigOptions);
168
169 mach64Screen->IsPCI = serverInfo->IsPCI;
170
171 {
172 drm_mach64_getparam_t gp;
173 int ret;
174
175 gp.param = MACH64_PARAM_IRQ_NR;
176 gp.value = (void *) &mach64Screen->irq;
177
178 ret = drmCommandWriteRead( sPriv->fd, DRM_MACH64_GETPARAM,
179 &gp, sizeof(gp));
180 if (ret) {
181 fprintf(stderr, "DRM_MACH64_GETPARAM (MACH64_PARAM_IRQ_NR): %d\n", ret);
182 FREE( mach64Screen );
183 return NULL;
184 }
185 }
186
187 mach64Screen->mmio.handle = serverInfo->regs;
188 mach64Screen->mmio.size = serverInfo->regsSize;
189 if ( drmMap( sPriv->fd,
190 mach64Screen->mmio.handle,
191 mach64Screen->mmio.size,
192 (drmAddressPtr)&mach64Screen->mmio.map ) != 0 ) {
193 FREE( mach64Screen );
194 return NULL;
195 }
196
197 mach64Screen->buffers = drmMapBufs( sPriv->fd );
198 if ( !mach64Screen->buffers ) {
199 drmUnmap( (drmAddress)mach64Screen->mmio.map,
200 mach64Screen->mmio.size );
201 FREE( mach64Screen );
202 return NULL;
203 }
204
205 if ( !mach64Screen->IsPCI ) {
206 mach64Screen->agpTextures.handle = serverInfo->agp;
207 mach64Screen->agpTextures.size = serverInfo->agpSize;
208 if ( drmMap( sPriv->fd,
209 mach64Screen->agpTextures.handle,
210 mach64Screen->agpTextures.size,
211 (drmAddressPtr)&mach64Screen->agpTextures.map ) ) {
212 drmUnmapBufs( mach64Screen->buffers );
213 drmUnmap( (drmAddress)mach64Screen->mmio.map, mach64Screen->mmio.size );
214 FREE( mach64Screen );
215 return NULL;
216 }
217 }
218
219 mach64Screen->AGPMode = serverInfo->AGPMode;
220
221 mach64Screen->chipset = serverInfo->chipset;
222 mach64Screen->width = serverInfo->width;
223 mach64Screen->height = serverInfo->height;
224 mach64Screen->mem = serverInfo->mem;
225 mach64Screen->cpp = serverInfo->cpp;
226
227 mach64Screen->frontOffset = serverInfo->frontOffset;
228 mach64Screen->frontPitch = serverInfo->frontPitch;
229 mach64Screen->backOffset = serverInfo->backOffset;
230 mach64Screen->backPitch = serverInfo->backPitch;
231 mach64Screen->depthOffset = serverInfo->depthOffset;
232 mach64Screen->depthPitch = serverInfo->depthPitch;
233
234 mach64Screen->texOffset[MACH64_CARD_HEAP] = serverInfo->textureOffset;
235 mach64Screen->texSize[MACH64_CARD_HEAP] = serverInfo->textureSize;
236 mach64Screen->logTexGranularity[MACH64_CARD_HEAP] =
237 serverInfo->logTextureGranularity;
238
239 if ( mach64Screen->IsPCI ) {
240 mach64Screen->numTexHeaps = MACH64_NR_TEX_HEAPS - 1;
241 mach64Screen->firstTexHeap = MACH64_CARD_HEAP;
242 mach64Screen->texOffset[MACH64_AGP_HEAP] = 0;
243 mach64Screen->texSize[MACH64_AGP_HEAP] = 0;
244 mach64Screen->logTexGranularity[MACH64_AGP_HEAP] = 0;
245 } else {
246 if (serverInfo->textureSize > 0) {
247 mach64Screen->numTexHeaps = MACH64_NR_TEX_HEAPS;
248 mach64Screen->firstTexHeap = MACH64_CARD_HEAP;
249 } else {
250 mach64Screen->numTexHeaps = MACH64_NR_TEX_HEAPS - 1;
251 mach64Screen->firstTexHeap = MACH64_AGP_HEAP;
252 }
253 mach64Screen->texOffset[MACH64_AGP_HEAP] = serverInfo->agpTextureOffset;
254 mach64Screen->texSize[MACH64_AGP_HEAP] = serverInfo->agpSize;
255 mach64Screen->logTexGranularity[MACH64_AGP_HEAP] = serverInfo->logAgpTextureGranularity;
256 }
257
258 mach64Screen->driScreen = sPriv;
259
260 i = 0;
261 mach64Screen->extensions[i++] = &driFrameTrackingExtension.base;
262 if ( mach64Screen->irq != 0 ) {
263 mach64Screen->extensions[i++] = &driSwapControlExtension.base;
264 mach64Screen->extensions[i++] = &driMediaStreamCounterExtension.base;
265 }
266 mach64Screen->extensions[i++] = NULL;
267 sPriv->extensions = mach64Screen->extensions;
268
269 return mach64Screen;
270 }
271
272 /* Destroy the device specific screen private data struct.
273 */
274 static void
275 mach64DestroyScreen( __DRIscreenPrivate *driScreen )
276 {
277 mach64ScreenRec *mach64Screen = (mach64ScreenRec *) driScreen->private;
278
279 if ( !mach64Screen )
280 return;
281
282 if ( MACH64_DEBUG & DEBUG_VERBOSE_DRI )
283 fprintf( stderr, "%s\n", __FUNCTION__ );
284
285 if ( !mach64Screen->IsPCI ) {
286 drmUnmap( (drmAddress)mach64Screen->agpTextures.map,
287 mach64Screen->agpTextures.size );
288 }
289
290 drmUnmapBufs( mach64Screen->buffers );
291 drmUnmap( (drmAddress)mach64Screen->mmio.map, mach64Screen->mmio.size );
292
293 FREE( mach64Screen );
294 driScreen->private = NULL;
295 }
296
297
298 /* Create and initialize the Mesa and driver specific pixmap buffer
299 * data.
300 */
301 static GLboolean
302 mach64CreateBuffer( __DRIscreenPrivate *driScrnPriv,
303 __DRIdrawablePrivate *driDrawPriv,
304 const __GLcontextModes *mesaVis,
305 GLboolean isPixmap )
306 {
307 mach64ScreenPtr screen = (mach64ScreenPtr) driScrnPriv->private;
308
309 if (isPixmap) {
310 return GL_FALSE; /* not implemented */
311 }
312 else {
313 struct gl_framebuffer *fb = _mesa_create_framebuffer(mesaVis);
314
315 {
316 driRenderbuffer *frontRb
317 = driNewRenderbuffer(GL_RGBA,
318 NULL,
319 screen->cpp,
320 screen->frontOffset, screen->frontPitch,
321 driDrawPriv);
322 mach64SetSpanFunctions(frontRb, mesaVis);
323 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &frontRb->Base);
324 }
325
326 if (mesaVis->doubleBufferMode) {
327 driRenderbuffer *backRb
328 = driNewRenderbuffer(GL_RGBA,
329 NULL,
330 screen->cpp,
331 screen->backOffset, screen->backPitch,
332 driDrawPriv);
333 mach64SetSpanFunctions(backRb, mesaVis);
334 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &backRb->Base);
335 }
336
337 if (mesaVis->depthBits == 16) {
338 driRenderbuffer *depthRb
339 = driNewRenderbuffer(GL_DEPTH_COMPONENT16,
340 NULL, screen->cpp,
341 screen->depthOffset, screen->depthPitch,
342 driDrawPriv);
343 mach64SetSpanFunctions(depthRb, mesaVis);
344 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
345 }
346 else if (mesaVis->depthBits == 24) {
347 /* XXX I don't think 24-bit Z is supported - so this isn't used */
348 driRenderbuffer *depthRb
349 = driNewRenderbuffer(GL_DEPTH_COMPONENT24,
350 NULL,
351 screen->cpp,
352 screen->depthOffset, screen->depthPitch,
353 driDrawPriv);
354 mach64SetSpanFunctions(depthRb, mesaVis);
355 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
356 }
357
358 _mesa_add_soft_renderbuffers(fb,
359 GL_FALSE, /* color */
360 GL_FALSE, /* depth */
361 mesaVis->stencilBits > 0,
362 mesaVis->accumRedBits > 0,
363 GL_FALSE, /* alpha */
364 GL_FALSE /* aux */);
365 driDrawPriv->driverPrivate = (void *) fb;
366
367 return (driDrawPriv->driverPrivate != NULL);
368 }
369 }
370
371
372 static void
373 mach64DestroyBuffer(__DRIdrawablePrivate *driDrawPriv)
374 {
375 _mesa_reference_framebuffer((GLframebuffer **)(&(driDrawPriv->driverPrivate)), NULL);
376 }
377
378
379 /* Copy the back color buffer to the front color buffer */
380 static void
381 mach64SwapBuffers(__DRIdrawablePrivate *dPriv)
382 {
383 if (dPriv->driContextPriv && dPriv->driContextPriv->driverPrivate) {
384 mach64ContextPtr mmesa;
385 GLcontext *ctx;
386 mmesa = (mach64ContextPtr) dPriv->driContextPriv->driverPrivate;
387 ctx = mmesa->glCtx;
388 if (ctx->Visual.doubleBufferMode) {
389 _mesa_notifySwapBuffers( ctx ); /* flush pending rendering comands */
390 mach64CopyBuffer( dPriv );
391 }
392 }
393 else {
394 /* XXX this shouldn't be an error but we can't handle it for now */
395 _mesa_problem(NULL, "%s: drawable has no context!", __FUNCTION__);
396 }
397 }
398
399
400 /* Initialize the driver specific screen private data.
401 */
402 static GLboolean
403 mach64InitDriver( __DRIscreenPrivate *driScreen )
404 {
405 driScreen->private = (void *) mach64CreateScreen( driScreen );
406
407 if ( !driScreen->private ) {
408 mach64DestroyScreen( driScreen );
409 return GL_FALSE;
410 }
411
412 return GL_TRUE;
413 }
414
415 /**
416 * This is the driver specific part of the createNewScreen entry point.
417 *
418 * \todo maybe fold this into intelInitDriver
419 *
420 * \return the __GLcontextModes supported by this driver
421 */
422 static const __DRIconfig **
423 mach64InitScreen(__DRIscreenPrivate *psp)
424 {
425 static const __DRIversion ddx_expected = { 6, 4, 0 };
426 static const __DRIversion dri_expected = { 4, 0, 0 };
427 static const __DRIversion drm_expected = { 2, 0, 0 };
428 ATIDRIPtr dri_priv = (ATIDRIPtr) psp->pDevPriv;
429
430 if ( ! driCheckDriDdxDrmVersions2( "Mach64",
431 &psp->dri_version, & dri_expected,
432 &psp->ddx_version, & ddx_expected,
433 &psp->drm_version, & drm_expected ) ) {
434 return NULL;
435 }
436
437 if (!mach64InitDriver(psp))
438 return NULL;
439
440 return mach64FillInModes( psp, dri_priv->cpp * 8, 16, 0, 1);
441 }
442
443 const struct __DriverAPIRec driDriverAPI = {
444 .InitScreen = mach64InitScreen,
445 .DestroyScreen = mach64DestroyScreen,
446 .CreateContext = mach64CreateContext,
447 .DestroyContext = mach64DestroyContext,
448 .CreateBuffer = mach64CreateBuffer,
449 .DestroyBuffer = mach64DestroyBuffer,
450 .SwapBuffers = mach64SwapBuffers,
451 .MakeCurrent = mach64MakeCurrent,
452 .UnbindContext = mach64UnbindContext,
453 .GetSwapInfo = NULL,
454 .GetDrawableMSC = driDrawableGetMSC32,
455 .WaitForMSC = driWaitForMSC32,
456 .WaitForSBC = NULL,
457 .SwapBuffersMSC = NULL
458 };
459