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