13365f6d8a8c29dc74d268070f80c18a911b566a
[mesa.git] / src / mesa / drivers / dri / tdfx / tdfx_screen.c
1 /* -*- mode: c; c-basic-offset: 3 -*-
2 *
3 * Copyright 2000 VA Linux Systems Inc., Fremont, California.
4 *
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * VA LINUX SYSTEMS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
23 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26 /* $XFree86: xc/lib/GL/mesa/src/drv/tdfx/tdfx_screen.c,v 1.3 2002/02/22 21:45:03 dawes Exp $ */
27
28 /*
29 * Original rewrite:
30 * Gareth Hughes <gareth@valinux.com>, 29 Sep - 1 Oct 2000
31 *
32 * Authors:
33 * Gareth Hughes <gareth@valinux.com>
34 *
35 */
36
37 #include "tdfx_dri.h"
38 #include "tdfx_context.h"
39 #include "tdfx_lock.h"
40 #include "tdfx_vb.h"
41 #include "tdfx_span.h"
42 #include "tdfx_tris.h"
43
44 #include "main/framebuffer.h"
45 #include "main/renderbuffer.h"
46 #include "xmlpool.h"
47
48 #include "utils.h"
49
50 #ifdef DEBUG_LOCKING
51 char *prevLockFile = 0;
52 int prevLockLine = 0;
53 #endif
54
55 #ifndef TDFX_DEBUG
56 int TDFX_DEBUG = 0;
57 #endif
58
59 PUBLIC const char __driConfigOptions[] =
60 DRI_CONF_BEGIN
61 DRI_CONF_SECTION_DEBUG
62 DRI_CONF_NO_RAST(false)
63 DRI_CONF_SECTION_END
64 DRI_CONF_END;
65
66 static const __DRIextension *tdfxExtensions[] = {
67 &driReadDrawableExtension,
68 NULL
69 };
70
71 static const GLuint __driNConfigOptions = 1;
72
73 extern const struct dri_extension card_extensions[];
74 extern const struct dri_extension napalm_extensions[];
75
76 static GLboolean
77 tdfxCreateScreen( __DRIscreenPrivate *sPriv )
78 {
79 tdfxScreenPrivate *fxScreen;
80 TDFXDRIPtr fxDRIPriv = (TDFXDRIPtr) sPriv->pDevPriv;
81
82 if (sPriv->devPrivSize != sizeof(TDFXDRIRec)) {
83 fprintf(stderr,"\nERROR! sizeof(TDFXDRIRec) does not match passed size from device driver\n");
84 return GL_FALSE;
85 }
86
87 /* Allocate the private area */
88 fxScreen = (tdfxScreenPrivate *) CALLOC( sizeof(tdfxScreenPrivate) );
89 if ( !fxScreen )
90 return GL_FALSE;
91
92 /* parse information in __driConfigOptions */
93 driParseOptionInfo (&fxScreen->optionCache,
94 __driConfigOptions, __driNConfigOptions);
95
96 fxScreen->driScrnPriv = sPriv;
97 sPriv->private = (void *) fxScreen;
98
99 fxScreen->regs.handle = fxDRIPriv->regs;
100 fxScreen->regs.size = fxDRIPriv->regsSize;
101 fxScreen->deviceID = fxDRIPriv->deviceID;
102 fxScreen->width = fxDRIPriv->width;
103 fxScreen->height = fxDRIPriv->height;
104 fxScreen->mem = fxDRIPriv->mem;
105 fxScreen->cpp = fxDRIPriv->cpp;
106 fxScreen->stride = fxDRIPriv->stride;
107 fxScreen->fifoOffset = fxDRIPriv->fifoOffset;
108 fxScreen->fifoSize = fxDRIPriv->fifoSize;
109 fxScreen->fbOffset = fxDRIPriv->fbOffset;
110 fxScreen->backOffset = fxDRIPriv->backOffset;
111 fxScreen->depthOffset = fxDRIPriv->depthOffset;
112 fxScreen->textureOffset = fxDRIPriv->textureOffset;
113 fxScreen->textureSize = fxDRIPriv->textureSize;
114 fxScreen->sarea_priv_offset = fxDRIPriv->sarea_priv_offset;
115
116 if ( drmMap( sPriv->fd, fxScreen->regs.handle,
117 fxScreen->regs.size, &fxScreen->regs.map ) ) {
118 return GL_FALSE;
119 }
120
121 sPriv->extensions = tdfxExtensions;
122
123 return GL_TRUE;
124 }
125
126
127 static void
128 tdfxDestroyScreen( __DRIscreenPrivate *sPriv )
129 {
130 tdfxScreenPrivate *fxScreen = (tdfxScreenPrivate *) sPriv->private;
131
132 if (!fxScreen)
133 return;
134
135 drmUnmap( fxScreen->regs.map, fxScreen->regs.size );
136
137 /* free all option information */
138 driDestroyOptionInfo (&fxScreen->optionCache);
139
140 FREE( fxScreen );
141 sPriv->private = NULL;
142 }
143
144
145 static GLboolean
146 tdfxInitDriver( __DRIscreenPrivate *sPriv )
147 {
148 if ( TDFX_DEBUG & DEBUG_VERBOSE_DRI ) {
149 fprintf( stderr, "%s( %p )\n", __FUNCTION__, (void *)sPriv );
150 }
151
152 if ( !tdfxCreateScreen( sPriv ) ) {
153 tdfxDestroyScreen( sPriv );
154 return GL_FALSE;
155 }
156
157 return GL_TRUE;
158 }
159
160
161 static GLboolean
162 tdfxCreateBuffer( __DRIscreenPrivate *driScrnPriv,
163 __DRIdrawablePrivate *driDrawPriv,
164 const __GLcontextModes *mesaVis,
165 GLboolean isPixmap )
166 {
167 tdfxScreenPrivate *screen = (tdfxScreenPrivate *) driScrnPriv->private;
168
169 if (isPixmap) {
170 return GL_FALSE; /* not implemented */
171 }
172 else {
173 struct gl_framebuffer *fb = _mesa_create_framebuffer(mesaVis);
174
175 {
176 driRenderbuffer *frontRb
177 = driNewRenderbuffer(GL_RGBA, NULL, screen->cpp,
178 screen->fbOffset, screen->width, driDrawPriv);
179 tdfxSetSpanFunctions(frontRb, mesaVis);
180 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &frontRb->Base);
181 }
182
183 if (mesaVis->doubleBufferMode) {
184 driRenderbuffer *backRb
185 = driNewRenderbuffer(GL_RGBA, NULL, screen->cpp,
186 screen->backOffset, screen->width,
187 driDrawPriv);
188 tdfxSetSpanFunctions(backRb, mesaVis);
189 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &backRb->Base);
190 backRb->backBuffer = GL_TRUE;
191 }
192
193 if (mesaVis->depthBits == 16) {
194 driRenderbuffer *depthRb
195 = driNewRenderbuffer(GL_DEPTH_COMPONENT16, NULL, screen->cpp,
196 screen->depthOffset, screen->width,
197 driDrawPriv);
198 tdfxSetSpanFunctions(depthRb, mesaVis);
199 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
200 }
201 else if (mesaVis->depthBits == 24) {
202 driRenderbuffer *depthRb
203 = driNewRenderbuffer(GL_DEPTH_COMPONENT24, NULL, screen->cpp,
204 screen->depthOffset, screen->width,
205 driDrawPriv);
206 tdfxSetSpanFunctions(depthRb, mesaVis);
207 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
208 }
209
210 if (mesaVis->stencilBits > 0) {
211 driRenderbuffer *stencilRb
212 = driNewRenderbuffer(GL_STENCIL_INDEX8_EXT, NULL, screen->cpp,
213 screen->depthOffset, screen->width,
214 driDrawPriv);
215 tdfxSetSpanFunctions(stencilRb, mesaVis);
216 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &stencilRb->Base);
217 }
218
219 _mesa_add_soft_renderbuffers(fb,
220 GL_FALSE, /* color */
221 GL_FALSE, /* depth */
222 GL_FALSE, /*swStencil,*/
223 mesaVis->accumRedBits > 0,
224 GL_FALSE, /* alpha */
225 GL_FALSE /* aux */);
226 driDrawPriv->driverPrivate = (void *) fb;
227
228 return (driDrawPriv->driverPrivate != NULL);
229 }
230 }
231
232
233 static void
234 tdfxDestroyBuffer(__DRIdrawablePrivate *driDrawPriv)
235 {
236 _mesa_unreference_framebuffer((GLframebuffer **)(&(driDrawPriv->driverPrivate)));
237 }
238
239
240 static void
241 tdfxSwapBuffers( __DRIdrawablePrivate *driDrawPriv )
242
243 {
244 GET_CURRENT_CONTEXT(ctx);
245 tdfxContextPtr fxMesa = 0;
246 GLframebuffer *mesaBuffer;
247
248 if ( TDFX_DEBUG & DEBUG_VERBOSE_DRI ) {
249 fprintf( stderr, "%s( %p )\n", __FUNCTION__, (void *)driDrawPriv );
250 }
251
252 mesaBuffer = (GLframebuffer *) driDrawPriv->driverPrivate;
253 if ( !mesaBuffer->Visual.doubleBufferMode )
254 return; /* can't swap a single-buffered window */
255
256 /* If the current context's drawable matches the given drawable
257 * we have to do a glFinish (per the GLX spec).
258 */
259 if ( ctx ) {
260 __DRIdrawablePrivate *curDrawPriv;
261 fxMesa = TDFX_CONTEXT(ctx);
262 curDrawPriv = fxMesa->driContext->driDrawablePriv;
263
264 if ( curDrawPriv == driDrawPriv ) {
265 /* swapping window bound to current context, flush first */
266 _mesa_notifySwapBuffers( ctx );
267 LOCK_HARDWARE( fxMesa );
268 }
269 else {
270 /* find the fxMesa context previously bound to the window */
271 fxMesa = (tdfxContextPtr) driDrawPriv->driContextPriv->driverPrivate;
272 if (!fxMesa)
273 return;
274 LOCK_HARDWARE( fxMesa );
275 fxMesa->Glide.grSstSelect( fxMesa->Glide.Board );
276 #ifdef DEBUG
277 printf("SwapBuf SetState 1\n");
278 #endif
279 fxMesa->Glide.grGlideSetState(fxMesa->Glide.State );
280 }
281 }
282
283 #ifdef STATS
284 {
285 int stalls;
286 static int prevStalls = 0;
287
288 stalls = fxMesa->Glide.grFifoGetStalls();
289
290 fprintf( stderr, "%s:\n", __FUNCTION__ );
291 if ( stalls != prevStalls ) {
292 fprintf( stderr, " %d stalls occurred\n",
293 stalls - prevStalls );
294 prevStalls = stalls;
295 }
296 if ( fxMesa && fxMesa->texSwaps ) {
297 fprintf( stderr, " %d texture swaps occurred\n",
298 fxMesa->texSwaps );
299 fxMesa->texSwaps = 0;
300 }
301 }
302 #endif
303
304 if (fxMesa->scissoredClipRects) {
305 /* restore clip rects without scissor box */
306 fxMesa->Glide.grDRIPosition( driDrawPriv->x, driDrawPriv->y,
307 driDrawPriv->w, driDrawPriv->h,
308 driDrawPriv->numClipRects,
309 driDrawPriv->pClipRects );
310 }
311
312 fxMesa->Glide.grDRIBufferSwap( fxMesa->Glide.SwapInterval );
313
314 if (fxMesa->scissoredClipRects) {
315 /* restore clip rects WITH scissor box */
316 fxMesa->Glide.grDRIPosition( driDrawPriv->x, driDrawPriv->y,
317 driDrawPriv->w, driDrawPriv->h,
318 fxMesa->numClipRects, fxMesa->pClipRects );
319 }
320
321
322 #if 0
323 {
324 FxI32 result;
325 do {
326 FxI32 result;
327 fxMesa->Glide.grGet(GR_PENDING_BUFFERSWAPS, 4, &result);
328 } while ( result > fxMesa->maxPendingSwapBuffers );
329 }
330 #endif
331
332 fxMesa->stats.swapBuffer++;
333
334 if (ctx) {
335 if (ctx->DriverCtx != fxMesa) {
336 fxMesa = TDFX_CONTEXT(ctx);
337 fxMesa->Glide.grSstSelect( fxMesa->Glide.Board );
338 #ifdef DEBUG
339 printf("SwapBuf SetState 2\n");
340 #endif
341 fxMesa->Glide.grGlideSetState(fxMesa->Glide.State );
342 }
343 UNLOCK_HARDWARE( fxMesa );
344 }
345 }
346
347 static const __DRIconfig **
348 tdfxFillInModes(__DRIscreenPrivate *psp,
349 unsigned pixel_bits,
350 unsigned depth_bits,
351 unsigned stencil_bits,
352 GLboolean have_back_buffer)
353 {
354 unsigned deep = (depth_bits > 17);
355
356 /* Right now GLX_SWAP_COPY_OML isn't supported, but it would be easy
357 * enough to add support. Basically, if a context is created with an
358 * fbconfig where the swap method is GLX_SWAP_COPY_OML, pageflipping
359 * will never be used.
360 */
361
362 static const GLenum db_modes[2] = { GLX_NONE, GLX_SWAP_UNDEFINED_OML };
363 uint8_t depth_bits_array[4];
364 uint8_t stencil_bits_array[4];
365 if(deep) {
366 depth_bits_array[0] = 0;
367 depth_bits_array[1] = 24;
368 stencil_bits_array[0] = 0;
369 stencil_bits_array[1] = 8;
370 } else {
371 depth_bits_array[0] = depth_bits;
372 depth_bits_array[1] = 0;
373 depth_bits_array[2] = depth_bits;
374 depth_bits_array[3] = 0;
375 stencil_bits_array[0] = 0;
376 stencil_bits_array[1] = 0;
377 stencil_bits_array[2] = 8;
378 stencil_bits_array[3] = 8;
379 }
380
381 return driCreateConfigs(
382 deep ? GL_RGBA : GL_RGB,
383 deep ? GL_UNSIGNED_INT_8_8_8_8 : GL_UNSIGNED_SHORT_5_6_5,
384 depth_bits_array,
385 stencil_bits_array,
386 deep ? 2 : 4,
387 db_modes, 2);
388 }
389
390 /**
391 * This is the driver specific part of the createNewScreen entry point.
392 *
393 * \todo maybe fold this into intelInitDriver
394 *
395 * \return the __GLcontextModes supported by this driver
396 */
397 static const __DRIconfig **
398 tdfxInitScreen(__DRIscreen *psp)
399 {
400 static const __DRIversion ddx_expected = { 1, 1, 0 };
401 static const __DRIversion dri_expected = { 4, 0, 0 };
402 static const __DRIversion drm_expected = { 1, 0, 0 };
403
404 /* divined from tdfx_dri.c, sketchy */
405 TDFXDRIPtr dri_priv = (TDFXDRIPtr) psp->pDevPriv;
406
407 /* XXX i wish it was like this */
408 /* bpp = dri_priv->bpp */
409 int bpp = (dri_priv->cpp > 2) ? 24 : 16;
410
411 if ( ! driCheckDriDdxDrmVersions2( "tdfx",
412 &psp->dri_version, & dri_expected,
413 &psp->ddx_version, & ddx_expected,
414 &psp->drm_version, & drm_expected ) )
415 return NULL;
416
417 /* Calling driInitExtensions here, with a NULL context pointer,
418 * does not actually enable the extensions. It just makes sure
419 * that all the dispatch offsets for all the extensions that
420 * *might* be enables are known. This is needed because the
421 * dispatch offsets need to be known when _mesa_context_create is
422 * called, but we can't enable the extensions until we have a
423 * context pointer.
424 *
425 * Hello chicken. Hello egg. How are you two today?
426 */
427 driInitExtensions( NULL, card_extensions, GL_FALSE );
428 driInitExtensions( NULL, napalm_extensions, GL_FALSE );
429
430 if (!tdfxInitDriver(psp))
431 return NULL;
432
433 return tdfxFillInModes(psp,
434 bpp, (bpp == 16) ? 16 : 24,
435 (bpp == 16) ? 0 : 8,
436 (dri_priv->backOffset!=dri_priv->depthOffset));
437 }
438
439 const struct __DriverAPIRec driDriverAPI = {
440 .InitScreen = tdfxInitScreen,
441 .DestroyScreen = tdfxDestroyScreen,
442 .CreateContext = tdfxCreateContext,
443 .DestroyContext = tdfxDestroyContext,
444 .CreateBuffer = tdfxCreateBuffer,
445 .DestroyBuffer = tdfxDestroyBuffer,
446 .SwapBuffers = tdfxSwapBuffers,
447 .MakeCurrent = tdfxMakeCurrent,
448 .UnbindContext = tdfxUnbindContext,
449 .GetSwapInfo = NULL,
450 .GetDrawableMSC = NULL,
451 .WaitForMSC = NULL,
452 .WaitForSBC = NULL,
453 .SwapBuffersMSC = NULL
454 };