Merge branch 'nouveau-import'
[mesa.git] / src / mesa / drivers / dri / unichrome / via_context.c
1 /*
2 * Copyright 1998-2003 VIA Technologies, Inc. All Rights Reserved.
3 * Copyright 2001-2003 S3 Graphics, Inc. All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sub license,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
14 * of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * VIA, S3 GRAPHICS, AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file via_context.c
27 *
28 * \author John Sheng (presumably of either VIA Technologies or S3 Graphics)
29 * \author Others at VIA Technologies?
30 * \author Others at S3 Graphics?
31 */
32
33 #include "glheader.h"
34 #include "context.h"
35 #include "matrix.h"
36 #include "state.h"
37 #include "simple_list.h"
38 #include "extensions.h"
39 #include "framebuffer.h"
40 #include "renderbuffer.h"
41
42 #include "swrast/swrast.h"
43 #include "swrast_setup/swrast_setup.h"
44 #include "tnl/tnl.h"
45 #include "array_cache/acache.h"
46
47 #include "tnl/t_pipeline.h"
48
49 #include "drivers/common/driverfuncs.h"
50
51 #include "via_screen.h"
52 #include "via_dri.h"
53
54 #include "via_state.h"
55 #include "via_tex.h"
56 #include "via_span.h"
57 #include "via_tris.h"
58 #include "via_ioctl.h"
59 #include "via_fb.h"
60
61 #include <stdio.h>
62 #include "macros.h"
63 #include "drirenderbuffer.h"
64
65 #define need_GL_ARB_multisample
66 #define need_GL_ARB_point_parameters
67 #define need_GL_EXT_fog_coord
68 #define need_GL_EXT_secondary_color
69 #include "extension_helper.h"
70
71 #define DRIVER_DATE "20060710"
72
73 #include "vblank.h"
74 #include "utils.h"
75
76 GLuint VIA_DEBUG = 0;
77
78 /**
79 * Return various strings for \c glGetString.
80 *
81 * \sa glGetString
82 */
83 static const GLubyte *viaGetString(GLcontext *ctx, GLenum name)
84 {
85 static char buffer[128];
86 unsigned offset;
87
88
89 switch (name) {
90 case GL_VENDOR:
91 return (GLubyte *)"VIA Technology";
92
93 case GL_RENDERER: {
94 static const char * const chipset_names[] = {
95 "UniChrome",
96 "CastleRock (CLE266)",
97 "UniChrome (KM400)",
98 "UniChrome (K8M800)",
99 "UniChrome (PM8x0/CN400)",
100 };
101 struct via_context *vmesa = VIA_CONTEXT(ctx);
102 unsigned id = vmesa->viaScreen->deviceID;
103
104 offset = driGetRendererString( buffer,
105 chipset_names[(id > VIA_PM800) ? 0 : id],
106 DRIVER_DATE, 0 );
107 return (GLubyte *)buffer;
108 }
109
110 default:
111 return NULL;
112 }
113 }
114
115
116 /**
117 * Calculate a width that satisfies the hardware's alignment requirements.
118 * On the Unichrome hardware, each scanline must be aligned to a multiple of
119 * 16 pixels.
120 *
121 * \param width Minimum buffer width, in pixels.
122 *
123 * \returns A pixel width that meets the alignment requirements.
124 */
125 static __inline__ unsigned
126 buffer_align( unsigned width )
127 {
128 return (width + 0x0f) & ~0x0f;
129 }
130
131
132 static void
133 viaDeleteRenderbuffer(struct gl_renderbuffer *rb)
134 {
135 /* Don't free() since we're contained in via_context struct. */
136 }
137
138 static GLboolean
139 viaRenderbufferStorage(GLcontext *ctx, struct gl_renderbuffer *rb,
140 GLenum internalFormat, GLuint width, GLuint height)
141 {
142 rb->Width = width;
143 rb->Height = height;
144 rb->InternalFormat = internalFormat;
145 return GL_TRUE;
146 }
147
148
149 static void
150 viaInitRenderbuffer(struct via_renderbuffer *vrb, GLenum format,
151 __DRIdrawablePrivate *dPriv)
152 {
153 const GLuint name = 0;
154 struct gl_renderbuffer *rb = & vrb->Base;
155
156 vrb->dPriv = dPriv;
157 _mesa_init_renderbuffer(rb, name);
158
159 /* Make sure we're using a null-valued GetPointer routine */
160 assert(rb->GetPointer(NULL, rb, 0, 0) == NULL);
161
162 rb->InternalFormat = format;
163
164 if (format == GL_RGBA) {
165 /* Color */
166 rb->_BaseFormat = GL_RGBA;
167 rb->DataType = GL_UNSIGNED_BYTE;
168 }
169 else if (format == GL_DEPTH_COMPONENT16) {
170 /* Depth */
171 rb->_BaseFormat = GL_DEPTH_COMPONENT;
172 /* we always Get/Put 32-bit Z values */
173 rb->DataType = GL_UNSIGNED_INT;
174 }
175 else if (format == GL_DEPTH_COMPONENT24) {
176 /* Depth */
177 rb->_BaseFormat = GL_DEPTH_COMPONENT;
178 /* we always Get/Put 32-bit Z values */
179 rb->DataType = GL_UNSIGNED_INT;
180 }
181 else {
182 /* Stencil */
183 ASSERT(format == GL_STENCIL_INDEX8_EXT);
184 rb->_BaseFormat = GL_STENCIL_INDEX;
185 rb->DataType = GL_UNSIGNED_BYTE;
186 }
187
188 rb->Delete = viaDeleteRenderbuffer;
189 rb->AllocStorage = viaRenderbufferStorage;
190 }
191
192
193 /**
194 * Calculate the framebuffer parameters for all buffers (front, back, depth,
195 * and stencil) associated with the specified context.
196 *
197 * \warning
198 * This function also calls \c AllocateBuffer to actually allocate the
199 * buffers.
200 *
201 * \sa AllocateBuffer
202 */
203 static GLboolean
204 calculate_buffer_parameters(struct via_context *vmesa,
205 struct gl_framebuffer *fb,
206 __DRIdrawablePrivate *dPriv)
207 {
208 const unsigned shift = vmesa->viaScreen->bitsPerPixel / 16;
209 const unsigned extra = 32;
210 unsigned w;
211 unsigned h;
212
213 /* Normally, the renderbuffer would be added to the framebuffer just once
214 * when the framebuffer was created. The VIA driver is a bit funny
215 * though in that the front/back/depth renderbuffers are in the per-context
216 * state!
217 * That should be fixed someday.
218 */
219
220 if (!vmesa->front.Base.InternalFormat) {
221 /* do one-time init for the renderbuffers */
222 viaInitRenderbuffer(&vmesa->front, GL_RGBA, dPriv);
223 viaSetSpanFunctions(&vmesa->front, &fb->Visual);
224 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &vmesa->front.Base);
225
226 if (fb->Visual.doubleBufferMode) {
227 viaInitRenderbuffer(&vmesa->back, GL_RGBA, dPriv);
228 viaSetSpanFunctions(&vmesa->back, &fb->Visual);
229 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &vmesa->back.Base);
230 }
231
232 if (vmesa->glCtx->Visual.depthBits > 0) {
233 viaInitRenderbuffer(&vmesa->depth,
234 (vmesa->glCtx->Visual.depthBits == 16
235 ? GL_DEPTH_COMPONENT16 : GL_DEPTH_COMPONENT24),
236 dPriv);
237 viaSetSpanFunctions(&vmesa->depth, &fb->Visual);
238 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &vmesa->depth.Base);
239 }
240
241 if (vmesa->glCtx->Visual.stencilBits > 0) {
242 viaInitRenderbuffer(&vmesa->stencil, GL_STENCIL_INDEX8_EXT,
243 dPriv);
244 viaSetSpanFunctions(&vmesa->stencil, &fb->Visual);
245 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &vmesa->stencil.Base);
246 }
247 }
248
249 assert(vmesa->front.Base.InternalFormat);
250 assert(vmesa->front.Base.AllocStorage);
251 if (fb->Visual.doubleBufferMode) {
252 assert(vmesa->back.Base.AllocStorage);
253 }
254 if (fb->Visual.depthBits) {
255 assert(vmesa->depth.Base.AllocStorage);
256 }
257
258
259 /* Allocate front-buffer */
260 if (vmesa->drawType == GLX_PBUFFER_BIT) {
261 w = vmesa->driDrawable->w;
262 h = vmesa->driDrawable->h;
263
264 vmesa->front.bpp = vmesa->viaScreen->bitsPerPixel;
265 vmesa->front.pitch = buffer_align( w ) << shift; /* bytes, not pixels */
266 vmesa->front.size = vmesa->front.pitch * h;
267
268 if (vmesa->front.map)
269 via_free_draw_buffer(vmesa, &vmesa->front);
270 if (!via_alloc_draw_buffer(vmesa, &vmesa->front))
271 return GL_FALSE;
272
273 } else {
274 w = vmesa->viaScreen->width;
275 h = vmesa->viaScreen->height;
276
277 vmesa->front.bpp = vmesa->viaScreen->bitsPerPixel;
278 vmesa->front.pitch = buffer_align( w ) << shift; /* bytes, not pixels */
279 vmesa->front.size = vmesa->front.pitch * h;
280 if (getenv("ALTERNATE_SCREEN"))
281 vmesa->front.offset = vmesa->front.size;
282 else
283 vmesa->front.offset = 0;
284 vmesa->front.map = (char *) vmesa->driScreen->pFB;
285 }
286
287
288 /* Allocate back-buffer */
289 if (vmesa->hasBack) {
290 vmesa->back.bpp = vmesa->viaScreen->bitsPerPixel;
291 vmesa->back.pitch = (buffer_align( vmesa->driDrawable->w ) << shift);
292 vmesa->back.pitch += extra;
293 vmesa->back.pitch = MIN2(vmesa->back.pitch, vmesa->front.pitch);
294 vmesa->back.size = vmesa->back.pitch * vmesa->driDrawable->h;
295 if (vmesa->back.map)
296 via_free_draw_buffer(vmesa, &vmesa->back);
297 if (!via_alloc_draw_buffer(vmesa, &vmesa->back))
298 return GL_FALSE;
299 }
300 else {
301 if (vmesa->back.map)
302 via_free_draw_buffer(vmesa, &vmesa->back);
303 (void) memset( &vmesa->back, 0, sizeof( vmesa->back ) );
304 }
305
306
307 /* Allocate depth-buffer */
308 if ( vmesa->hasStencil || vmesa->hasDepth ) {
309 vmesa->depth.bpp = vmesa->depthBits;
310 if (vmesa->depth.bpp == 24)
311 vmesa->depth.bpp = 32;
312
313 vmesa->depth.pitch = (buffer_align( vmesa->driDrawable->w ) *
314 (vmesa->depth.bpp/8)) + extra;
315 vmesa->depth.size = vmesa->depth.pitch * vmesa->driDrawable->h;
316
317 if (vmesa->depth.map)
318 via_free_draw_buffer(vmesa, &vmesa->depth);
319 if (!via_alloc_draw_buffer(vmesa, &vmesa->depth)) {
320 return GL_FALSE;
321 }
322 }
323 else {
324 if (vmesa->depth.map)
325 via_free_draw_buffer(vmesa, &vmesa->depth);
326 (void) memset( & vmesa->depth, 0, sizeof( vmesa->depth ) );
327 }
328
329 /* stencil buffer is same as depth buffer */
330 vmesa->stencil.handle = vmesa->depth.handle;
331 vmesa->stencil.size = vmesa->depth.size;
332 vmesa->stencil.offset = vmesa->depth.offset;
333 vmesa->stencil.index = vmesa->depth.index;
334 vmesa->stencil.pitch = vmesa->depth.pitch;
335 vmesa->stencil.bpp = vmesa->depth.bpp;
336 vmesa->stencil.map = vmesa->depth.map;
337 vmesa->stencil.orig = vmesa->depth.orig;
338 vmesa->stencil.origMap = vmesa->depth.origMap;
339
340 if( vmesa->viaScreen->width == vmesa->driDrawable->w &&
341 vmesa->viaScreen->height == vmesa->driDrawable->h ) {
342 vmesa->doPageFlip = vmesa->allowPageFlip;
343 if (vmesa->hasBack) {
344 assert(vmesa->back.pitch == vmesa->front.pitch);
345 }
346 }
347 else
348 vmesa->doPageFlip = GL_FALSE;
349
350 return GL_TRUE;
351 }
352
353
354 void viaReAllocateBuffers(GLcontext *ctx, GLframebuffer *drawbuffer,
355 GLuint width, GLuint height)
356 {
357 struct via_context *vmesa = VIA_CONTEXT(ctx);
358
359 calculate_buffer_parameters(vmesa, drawbuffer, vmesa->driDrawable);
360
361 _mesa_resize_framebuffer(ctx, drawbuffer, width, height);
362 }
363
364 /* Extension strings exported by the Unichrome driver.
365 */
366 const struct dri_extension card_extensions[] =
367 {
368 { "GL_ARB_multisample", GL_ARB_multisample_functions },
369 { "GL_ARB_multitexture", NULL },
370 { "GL_ARB_point_parameters", GL_ARB_point_parameters_functions },
371 { "GL_ARB_texture_env_add", NULL },
372 { "GL_ARB_texture_env_combine", NULL },
373 /* { "GL_ARB_texture_env_dot3", NULL }, */
374 { "GL_ARB_texture_mirrored_repeat", NULL },
375 { "GL_EXT_fog_coord", GL_EXT_fog_coord_functions },
376 { "GL_EXT_secondary_color", GL_EXT_secondary_color_functions },
377 { "GL_EXT_stencil_wrap", NULL },
378 { "GL_EXT_texture_env_combine", NULL },
379 /* { "GL_EXT_texture_env_dot3", NULL }, */
380 { "GL_EXT_texture_lod_bias", NULL },
381 { "GL_NV_blend_square", NULL },
382 { NULL, NULL }
383 };
384
385 extern const struct tnl_pipeline_stage _via_fastrender_stage;
386 extern const struct tnl_pipeline_stage _via_render_stage;
387
388 static const struct tnl_pipeline_stage *via_pipeline[] = {
389 &_tnl_vertex_transform_stage,
390 &_tnl_normal_transform_stage,
391 &_tnl_lighting_stage,
392 &_tnl_fog_coordinate_stage,
393 &_tnl_texgen_stage,
394 &_tnl_texture_transform_stage,
395 /* REMOVE: point attenuation stage */
396 #if 1
397 &_via_fastrender_stage, /* ADD: unclipped rastersetup-to-dma */
398 #endif
399 &_tnl_render_stage,
400 0,
401 };
402
403
404 static const struct dri_debug_control debug_control[] =
405 {
406 { "fall", DEBUG_FALLBACKS },
407 { "tex", DEBUG_TEXTURE },
408 { "ioctl", DEBUG_IOCTL },
409 { "prim", DEBUG_PRIMS },
410 { "vert", DEBUG_VERTS },
411 { "state", DEBUG_STATE },
412 { "verb", DEBUG_VERBOSE },
413 { "dri", DEBUG_DRI },
414 { "dma", DEBUG_DMA },
415 { "san", DEBUG_SANITY },
416 { "sync", DEBUG_SYNC },
417 { "sleep", DEBUG_SLEEP },
418 { "pix", DEBUG_PIXEL },
419 { "2d", DEBUG_2D },
420 { NULL, 0 }
421 };
422
423
424 static GLboolean
425 AllocateDmaBuffer(struct via_context *vmesa)
426 {
427 if (vmesa->dma)
428 via_free_dma_buffer(vmesa);
429
430 if (!via_alloc_dma_buffer(vmesa))
431 return GL_FALSE;
432
433 vmesa->dmaLow = 0;
434 vmesa->dmaCliprectAddr = ~0;
435 return GL_TRUE;
436 }
437
438 static void
439 FreeBuffer(struct via_context *vmesa)
440 {
441 if (vmesa->front.map && vmesa->drawType == GLX_PBUFFER_BIT)
442 via_free_draw_buffer(vmesa, &vmesa->front);
443
444 if (vmesa->back.map)
445 via_free_draw_buffer(vmesa, &vmesa->back);
446
447 if (vmesa->depth.map)
448 via_free_draw_buffer(vmesa, &vmesa->depth);
449
450 if (vmesa->breadcrumb.map)
451 via_free_draw_buffer(vmesa, &vmesa->breadcrumb);
452
453 if (vmesa->dma)
454 via_free_dma_buffer(vmesa);
455 }
456
457
458 GLboolean
459 viaCreateContext(const __GLcontextModes *visual,
460 __DRIcontextPrivate *driContextPriv,
461 void *sharedContextPrivate)
462 {
463 GLcontext *ctx, *shareCtx;
464 struct via_context *vmesa;
465 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
466 viaScreenPrivate *viaScreen = (viaScreenPrivate *)sPriv->private;
467 drm_via_sarea_t *saPriv = (drm_via_sarea_t *)
468 (((GLubyte *)sPriv->pSAREA) + viaScreen->sareaPrivOffset);
469 struct dd_function_table functions;
470
471 /* Allocate via context */
472 vmesa = (struct via_context *) CALLOC_STRUCT(via_context);
473 if (!vmesa) {
474 return GL_FALSE;
475 }
476
477 /* Parse configuration files.
478 */
479 driParseConfigFiles (&vmesa->optionCache, &viaScreen->optionCache,
480 sPriv->myNum, "unichrome");
481
482 /* pick back buffer */
483 vmesa->hasBack = visual->doubleBufferMode;
484
485 switch(visual->depthBits) {
486 case 0:
487 vmesa->hasDepth = GL_FALSE;
488 vmesa->depthBits = 0;
489 vmesa->depth_max = 1.0;
490 break;
491 case 16:
492 vmesa->hasDepth = GL_TRUE;
493 vmesa->depthBits = visual->depthBits;
494 vmesa->have_hw_stencil = GL_FALSE;
495 vmesa->depth_max = (GLfloat)0xffff;
496 vmesa->depth_clear_mask = 0xf << 28;
497 vmesa->ClearDepth = 0xffff;
498 vmesa->polygon_offset_scale = 1.0 / vmesa->depth_max;
499 break;
500 case 24:
501 vmesa->hasDepth = GL_TRUE;
502 vmesa->depthBits = visual->depthBits;
503 vmesa->depth_max = (GLfloat) 0xffffff;
504 vmesa->depth_clear_mask = 0xe << 28;
505 vmesa->ClearDepth = 0xffffff00;
506
507 assert(visual->haveStencilBuffer);
508 assert(visual->stencilBits == 8);
509
510 vmesa->have_hw_stencil = GL_TRUE;
511 vmesa->stencilBits = visual->stencilBits;
512 vmesa->stencil_clear_mask = 0x1 << 28;
513 vmesa->polygon_offset_scale = 2.0 / vmesa->depth_max;
514 break;
515 case 32:
516 vmesa->hasDepth = GL_TRUE;
517 vmesa->depthBits = visual->depthBits;
518 assert(!visual->haveStencilBuffer);
519 vmesa->have_hw_stencil = GL_FALSE;
520 vmesa->depth_max = (GLfloat)0xffffffff;
521 vmesa->depth_clear_mask = 0xf << 28;
522 vmesa->ClearDepth = 0xffffffff;
523 vmesa->polygon_offset_scale = 2.0 / vmesa->depth_max;
524 break;
525 default:
526 assert(0);
527 break;
528 }
529
530 make_empty_list(&vmesa->freed_tex_buffers);
531 make_empty_list(&vmesa->tex_image_list[VIA_MEM_VIDEO]);
532 make_empty_list(&vmesa->tex_image_list[VIA_MEM_AGP]);
533 make_empty_list(&vmesa->tex_image_list[VIA_MEM_SYSTEM]);
534
535 _mesa_init_driver_functions(&functions);
536 viaInitTextureFuncs(&functions);
537
538 /* Allocate the Mesa context */
539 if (sharedContextPrivate)
540 shareCtx = ((struct via_context *) sharedContextPrivate)->glCtx;
541 else
542 shareCtx = NULL;
543
544 vmesa->glCtx = _mesa_create_context(visual, shareCtx, &functions,
545 (void*) vmesa);
546
547 vmesa->shareCtx = shareCtx;
548
549 if (!vmesa->glCtx) {
550 FREE(vmesa);
551 return GL_FALSE;
552 }
553 driContextPriv->driverPrivate = vmesa;
554
555 ctx = vmesa->glCtx;
556
557 if (driQueryOptionb(&vmesa->optionCache, "excess_mipmap"))
558 ctx->Const.MaxTextureLevels = 11;
559 else
560 ctx->Const.MaxTextureLevels = 10;
561
562 ctx->Const.MaxTextureUnits = 2;
563 ctx->Const.MaxTextureImageUnits = ctx->Const.MaxTextureUnits;
564 ctx->Const.MaxTextureCoordUnits = ctx->Const.MaxTextureUnits;
565
566 ctx->Const.MinLineWidth = 1.0;
567 ctx->Const.MinLineWidthAA = 1.0;
568 ctx->Const.MaxLineWidth = 1.0;
569 ctx->Const.MaxLineWidthAA = 1.0;
570 ctx->Const.LineWidthGranularity = 1.0;
571
572 ctx->Const.MinPointSize = 1.0;
573 ctx->Const.MinPointSizeAA = 1.0;
574 ctx->Const.MaxPointSize = 1.0;
575 ctx->Const.MaxPointSizeAA = 1.0;
576 ctx->Const.PointSizeGranularity = 1.0;
577
578 ctx->Driver.GetString = viaGetString;
579
580 ctx->DriverCtx = (void *)vmesa;
581 vmesa->glCtx = ctx;
582
583 /* Initialize the software rasterizer and helper modules.
584 */
585 _swrast_CreateContext(ctx);
586 _ac_CreateContext(ctx);
587 _tnl_CreateContext(ctx);
588 _swsetup_CreateContext(ctx);
589
590 /* Install the customized pipeline:
591 */
592 _tnl_destroy_pipeline(ctx);
593 _tnl_install_pipeline(ctx, via_pipeline);
594
595 /* Configure swrast and T&L to match hardware characteristics:
596 */
597 _swrast_allow_pixel_fog(ctx, GL_FALSE);
598 _swrast_allow_vertex_fog(ctx, GL_TRUE);
599 _tnl_allow_pixel_fog(ctx, GL_FALSE);
600 _tnl_allow_vertex_fog(ctx, GL_TRUE);
601
602 /* vmesa->display = dpy; */
603 vmesa->display = sPriv->display;
604
605 vmesa->hHWContext = driContextPriv->hHWContext;
606 vmesa->driFd = sPriv->fd;
607 vmesa->driHwLock = &sPriv->pSAREA->lock;
608
609 vmesa->viaScreen = viaScreen;
610 vmesa->driScreen = sPriv;
611 vmesa->sarea = saPriv;
612
613 vmesa->renderIndex = ~0;
614 vmesa->setupIndex = ~0;
615 vmesa->hwPrimitive = GL_POLYGON+1;
616
617 /* KW: Hardwire this. Was previously set bogusly in
618 * viaCreateBuffer. Needs work before PBUFFER can be used:
619 */
620 vmesa->drawType = GLX_WINDOW_BIT;
621
622
623 _math_matrix_ctr(&vmesa->ViewportMatrix);
624
625 /* Do this early, before VIA_FLUSH_DMA can be called:
626 */
627 if (!AllocateDmaBuffer(vmesa)) {
628 fprintf(stderr ,"AllocateDmaBuffer fail\n");
629 FreeBuffer(vmesa);
630 FREE(vmesa);
631 return GL_FALSE;
632 }
633
634 /* Allocate a small piece of fb memory for synchronization:
635 */
636 vmesa->breadcrumb.bpp = 32;
637 vmesa->breadcrumb.pitch = buffer_align( 64 ) << 2;
638 vmesa->breadcrumb.size = vmesa->breadcrumb.pitch;
639
640 if (!via_alloc_draw_buffer(vmesa, &vmesa->breadcrumb)) {
641 fprintf(stderr ,"AllocateDmaBuffer fail\n");
642 FreeBuffer(vmesa);
643 FREE(vmesa);
644 return GL_FALSE;
645 }
646
647 driInitExtensions( ctx, card_extensions, GL_TRUE );
648 viaInitStateFuncs(ctx);
649 viaInitTriFuncs(ctx);
650 viaInitSpanFuncs(ctx);
651 viaInitIoctlFuncs(ctx);
652 viaInitState(ctx);
653
654 if (getenv("VIA_DEBUG"))
655 VIA_DEBUG = driParseDebugString( getenv( "VIA_DEBUG" ),
656 debug_control );
657
658 if (getenv("VIA_NO_RAST") ||
659 driQueryOptionb(&vmesa->optionCache, "no_rast"))
660 FALLBACK(vmesa, VIA_FALLBACK_USER_DISABLE, 1);
661
662 vmesa->vblank_flags =
663 vmesa->viaScreen->irqEnabled ?
664 driGetDefaultVBlankFlags(&vmesa->optionCache) : VBLANK_FLAG_NO_IRQ;
665
666 if (getenv("VIA_PAGEFLIP"))
667 vmesa->allowPageFlip = 1;
668
669 (*dri_interface->getUST)( &vmesa->swap_ust );
670
671
672 vmesa->regMMIOBase = (GLuint *)((unsigned long)viaScreen->reg);
673 vmesa->pnGEMode = (GLuint *)((unsigned long)viaScreen->reg + 0x4);
674 vmesa->regEngineStatus = (GLuint *)((unsigned long)viaScreen->reg + 0x400);
675 vmesa->regTranSet = (GLuint *)((unsigned long)viaScreen->reg + 0x43C);
676 vmesa->regTranSpace = (GLuint *)((unsigned long)viaScreen->reg + 0x440);
677 vmesa->agpBase = viaScreen->agpBase;
678
679
680 return GL_TRUE;
681 }
682
683 void
684 viaDestroyContext(__DRIcontextPrivate *driContextPriv)
685 {
686 GET_CURRENT_CONTEXT(ctx);
687 struct via_context *vmesa =
688 (struct via_context *)driContextPriv->driverPrivate;
689 struct via_context *current = ctx ? VIA_CONTEXT(ctx) : NULL;
690 assert(vmesa); /* should never be null */
691
692 /* check if we're deleting the currently bound context */
693 if (vmesa == current) {
694 VIA_FLUSH_DMA(vmesa);
695 _mesa_make_current(NULL, NULL, NULL);
696 }
697
698 if (vmesa) {
699 viaWaitIdle(vmesa, GL_FALSE);
700 if (vmesa->doPageFlip) {
701 LOCK_HARDWARE(vmesa);
702 if (vmesa->pfCurrentOffset != 0) {
703 fprintf(stderr, "%s - reset pf\n", __FUNCTION__);
704 viaResetPageFlippingLocked(vmesa);
705 }
706 UNLOCK_HARDWARE(vmesa);
707 }
708
709 _swsetup_DestroyContext(vmesa->glCtx);
710 _tnl_DestroyContext(vmesa->glCtx);
711 _ac_DestroyContext(vmesa->glCtx);
712 _swrast_DestroyContext(vmesa->glCtx);
713 /* free the Mesa context */
714 _mesa_destroy_context(vmesa->glCtx);
715 /* release our data */
716 FreeBuffer(vmesa);
717
718 assert (is_empty_list(&vmesa->tex_image_list[VIA_MEM_AGP]));
719 assert (is_empty_list(&vmesa->tex_image_list[VIA_MEM_VIDEO]));
720 assert (is_empty_list(&vmesa->tex_image_list[VIA_MEM_SYSTEM]));
721 assert (is_empty_list(&vmesa->freed_tex_buffers));
722
723 driDestroyOptionCache(&vmesa->optionCache);
724
725 FREE(vmesa);
726 }
727 }
728
729
730 void viaXMesaWindowMoved(struct via_context *vmesa)
731 {
732 __DRIdrawablePrivate *const drawable = vmesa->driDrawable;
733 __DRIdrawablePrivate *const readable = vmesa->driReadable;
734 struct via_renderbuffer *const draw_buffer =
735 (struct via_renderbuffer *) drawable->driverPrivate;
736 struct via_renderbuffer *const read_buffer =
737 (struct via_renderbuffer *) readable->driverPrivate;
738 GLuint bytePerPixel = vmesa->viaScreen->bitsPerPixel >> 3;
739
740 if (!drawable)
741 return;
742
743 switch (vmesa->glCtx->DrawBuffer->_ColorDrawBufferMask[0]) {
744 case BUFFER_BIT_BACK_LEFT:
745 if (drawable->numBackClipRects == 0) {
746 vmesa->numClipRects = drawable->numClipRects;
747 vmesa->pClipRects = drawable->pClipRects;
748 }
749 else {
750 vmesa->numClipRects = drawable->numBackClipRects;
751 vmesa->pClipRects = drawable->pBackClipRects;
752 }
753 break;
754 case BUFFER_BIT_FRONT_LEFT:
755 vmesa->numClipRects = drawable->numClipRects;
756 vmesa->pClipRects = drawable->pClipRects;
757 break;
758 default:
759 vmesa->numClipRects = 0;
760 break;
761 }
762
763 if ((draw_buffer->drawW != drawable->w)
764 || (draw_buffer->drawH != drawable->h)) {
765 calculate_buffer_parameters(vmesa, vmesa->glCtx->DrawBuffer,
766 drawable);
767 }
768
769 draw_buffer->drawXoff = (GLuint)(((drawable->x * bytePerPixel) & 0x1f) /
770 bytePerPixel);
771 draw_buffer->drawX = drawable->x - draw_buffer->drawXoff;
772 draw_buffer->drawY = drawable->y;
773 draw_buffer->drawW = drawable->w;
774 draw_buffer->drawH = drawable->h;
775
776 if (drawable != readable) {
777 if ((read_buffer->drawW != readable->w)
778 || (read_buffer->drawH != readable->h)) {
779 calculate_buffer_parameters(vmesa, vmesa->glCtx->ReadBuffer,
780 readable);
781 }
782
783 read_buffer->drawXoff = (GLuint)(((readable->x * bytePerPixel) & 0x1f) /
784 bytePerPixel);
785 read_buffer->drawX = readable->x - read_buffer->drawXoff;
786 read_buffer->drawY = readable->y;
787 read_buffer->drawW = readable->w;
788 read_buffer->drawH = readable->h;
789 }
790
791 vmesa->front.orig = (vmesa->front.offset +
792 draw_buffer->drawY * vmesa->front.pitch +
793 draw_buffer->drawX * bytePerPixel);
794
795 vmesa->front.origMap = (vmesa->front.map +
796 draw_buffer->drawY * vmesa->front.pitch +
797 draw_buffer->drawX * bytePerPixel);
798
799 vmesa->back.orig = vmesa->back.offset;
800 vmesa->depth.orig = vmesa->depth.offset;
801 vmesa->back.origMap = vmesa->back.map;
802 vmesa->depth.origMap = vmesa->depth.map;
803
804 viaCalcViewport(vmesa->glCtx);
805 }
806
807 GLboolean
808 viaUnbindContext(__DRIcontextPrivate *driContextPriv)
809 {
810 return GL_TRUE;
811 }
812
813 GLboolean
814 viaMakeCurrent(__DRIcontextPrivate *driContextPriv,
815 __DRIdrawablePrivate *driDrawPriv,
816 __DRIdrawablePrivate *driReadPriv)
817 {
818 if (VIA_DEBUG & DEBUG_DRI) {
819 fprintf(stderr, "driContextPriv = %016lx\n", (unsigned long)driContextPriv);
820 fprintf(stderr, "driDrawPriv = %016lx\n", (unsigned long)driDrawPriv);
821 fprintf(stderr, "driReadPriv = %016lx\n", (unsigned long)driReadPriv);
822 }
823
824 if (driContextPriv) {
825 struct via_context *vmesa =
826 (struct via_context *)driContextPriv->driverPrivate;
827 GLcontext *ctx = vmesa->glCtx;
828 struct gl_framebuffer *drawBuffer, *readBuffer;
829
830 drawBuffer = (GLframebuffer *)driDrawPriv->driverPrivate;
831 readBuffer = (GLframebuffer *)driReadPriv->driverPrivate;
832
833 if (vmesa->driDrawable != driDrawPriv) {
834 driDrawableInitVBlank(driDrawPriv, vmesa->vblank_flags,
835 &vmesa->vbl_seq);
836 }
837
838 if ((vmesa->driDrawable != driDrawPriv)
839 || (vmesa->driReadable != driReadPriv)) {
840 vmesa->driDrawable = driDrawPriv;
841 vmesa->driReadable = driReadPriv;
842
843 if ((drawBuffer->Width != driDrawPriv->w)
844 || (drawBuffer->Height != driDrawPriv->h)) {
845 _mesa_resize_framebuffer(ctx, drawBuffer,
846 driDrawPriv->w, driDrawPriv->h);
847 drawBuffer->Initialized = GL_TRUE;
848 }
849
850 if (!calculate_buffer_parameters(vmesa, drawBuffer, driDrawPriv)) {
851 return GL_FALSE;
852 }
853
854 if (driDrawPriv != driReadPriv) {
855 if ((readBuffer->Width != driReadPriv->w)
856 || (readBuffer->Height != driReadPriv->h)) {
857 _mesa_resize_framebuffer(ctx, readBuffer,
858 driReadPriv->w, driReadPriv->h);
859 readBuffer->Initialized = GL_TRUE;
860 }
861
862 if (!calculate_buffer_parameters(vmesa, readBuffer, driReadPriv)) {
863 return GL_FALSE;
864 }
865 }
866 }
867
868 _mesa_make_current(vmesa->glCtx, drawBuffer, readBuffer);
869
870 ctx->Driver.DrawBuffer( ctx, ctx->Color.DrawBuffer[0] );
871
872 viaXMesaWindowMoved(vmesa);
873 ctx->Driver.Scissor(vmesa->glCtx,
874 vmesa->glCtx->Scissor.X,
875 vmesa->glCtx->Scissor.Y,
876 vmesa->glCtx->Scissor.Width,
877 vmesa->glCtx->Scissor.Height);
878 }
879 else {
880 _mesa_make_current(NULL, NULL, NULL);
881 }
882
883 return GL_TRUE;
884 }
885
886 void viaGetLock(struct via_context *vmesa, GLuint flags)
887 {
888 __DRIdrawablePrivate *dPriv = vmesa->driDrawable;
889 __DRIscreenPrivate *sPriv = vmesa->driScreen;
890
891 drmGetLock(vmesa->driFd, vmesa->hHWContext, flags);
892
893 DRI_VALIDATE_DRAWABLE_INFO(sPriv, dPriv);
894 if (dPriv != vmesa->driReadable) {
895 DRI_VALIDATE_DRAWABLE_INFO(sPriv, vmesa->driReadable);
896 }
897
898 if (vmesa->sarea->ctxOwner != vmesa->hHWContext) {
899 vmesa->sarea->ctxOwner = vmesa->hHWContext;
900 vmesa->newEmitState = ~0;
901 }
902
903 if (vmesa->lastStamp != dPriv->lastStamp) {
904 viaXMesaWindowMoved(vmesa);
905 driUpdateFramebufferSize(vmesa->glCtx, dPriv);
906 vmesa->newEmitState = ~0;
907 vmesa->lastStamp = dPriv->lastStamp;
908 }
909
910 if (vmesa->doPageFlip &&
911 vmesa->pfCurrentOffset != vmesa->sarea->pfCurrentOffset) {
912 fprintf(stderr, "%s - reset pf\n", __FUNCTION__);
913 viaResetPageFlippingLocked(vmesa);
914 }
915 }
916
917
918 void
919 viaSwapBuffers(__DRIdrawablePrivate *drawablePrivate)
920 {
921 __DRIdrawablePrivate *dPriv = (__DRIdrawablePrivate *)drawablePrivate;
922
923 if (dPriv &&
924 dPriv->driContextPriv &&
925 dPriv->driContextPriv->driverPrivate) {
926 struct via_context *vmesa =
927 (struct via_context *)dPriv->driContextPriv->driverPrivate;
928 GLcontext *ctx = vmesa->glCtx;
929
930 _mesa_notifySwapBuffers(ctx);
931
932 if (ctx->Visual.doubleBufferMode) {
933 if (vmesa->doPageFlip) {
934 viaPageFlip(dPriv);
935 }
936 else {
937 viaCopyBuffer(dPriv);
938 }
939 }
940 else
941 VIA_FLUSH_DMA(vmesa);
942 }
943 else {
944 _mesa_problem(NULL, "viaSwapBuffers: drawable has no context!\n");
945 }
946 }