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