Instead of calling _mesa_ResizeBuffersMESA() in the Viewport function,
[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 "20050526"
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 gl_renderbuffer *rb, GLenum format)
151 {
152 const GLuint name = 0;
153
154 _mesa_init_renderbuffer(rb, name);
155
156 /* Make sure we're using a null-valued GetPointer routine */
157 assert(rb->GetPointer(NULL, rb, 0, 0) == NULL);
158
159 rb->InternalFormat = format;
160
161 if (format == GL_RGBA) {
162 /* Color */
163 rb->_BaseFormat = GL_RGBA;
164 rb->DataType = GL_UNSIGNED_BYTE;
165 }
166 else if (format == GL_DEPTH_COMPONENT16) {
167 /* Depth */
168 rb->_BaseFormat = GL_DEPTH_COMPONENT;
169 /* we always Get/Put 32-bit Z values */
170 rb->DataType = GL_UNSIGNED_INT;
171 }
172 else if (format == GL_DEPTH_COMPONENT24) {
173 /* Depth */
174 rb->_BaseFormat = GL_DEPTH_COMPONENT;
175 /* we always Get/Put 32-bit Z values */
176 rb->DataType = GL_UNSIGNED_INT;
177 }
178 else {
179 /* Stencil */
180 ASSERT(format == GL_STENCIL_INDEX8);
181 rb->_BaseFormat = GL_STENCIL_INDEX;
182 rb->DataType = GL_UNSIGNED_BYTE;
183 }
184
185 rb->Delete = viaDeleteRenderbuffer;
186 rb->AllocStorage = viaRenderbufferStorage;
187 }
188
189
190 /**
191 * Calculate the framebuffer parameters for all buffers (front, back, depth,
192 * and stencil) associated with the specified context.
193 *
194 * \warning
195 * This function also calls \c AllocateBuffer to actually allocate the
196 * buffers.
197 *
198 * \sa AllocateBuffer
199 */
200 static GLboolean
201 calculate_buffer_parameters( struct via_context *vmesa,
202 struct gl_framebuffer *fb )
203 {
204 const unsigned shift = vmesa->viaScreen->bitsPerPixel / 16;
205 const unsigned extra = 32;
206 unsigned w;
207 unsigned h;
208
209 /* Normally, the renderbuffer would be added to the framebuffer just once
210 * when the framebuffer was created. The VIA driver is a bit funny
211 * though in that the front/back/depth renderbuffers are in the per-context
212 * state!
213 * That should be fixed someday.
214 */
215
216 if (!vmesa->front.Base.InternalFormat) {
217 /* do one-time init for the renderbuffers */
218 viaInitRenderbuffer(&vmesa->front.Base, GL_RGBA);
219 viaSetSpanFunctions(&vmesa->front, &fb->Visual);
220 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &vmesa->front.Base);
221
222 if (fb->Visual.doubleBufferMode) {
223 viaInitRenderbuffer(&vmesa->back.Base, GL_RGBA);
224 viaSetSpanFunctions(&vmesa->back, &fb->Visual);
225 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &vmesa->back.Base);
226 }
227
228 if (vmesa->glCtx->Visual.depthBits > 0) {
229 viaInitRenderbuffer(&vmesa->depth.Base,
230 (vmesa->glCtx->Visual.depthBits == 16
231 ? GL_DEPTH_COMPONENT16 : GL_DEPTH_COMPONENT24));
232 viaSetSpanFunctions(&vmesa->depth, &fb->Visual);
233 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &vmesa->depth.Base);
234 }
235
236 if (vmesa->glCtx->Visual.stencilBits > 0) {
237 viaInitRenderbuffer(&vmesa->stencil.Base, GL_STENCIL_INDEX8_EXT);
238 viaSetSpanFunctions(&vmesa->stencil, &fb->Visual);
239 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &vmesa->stencil.Base);
240 }
241 }
242
243 assert(vmesa->front.Base.InternalFormat);
244 assert(vmesa->front.Base.AllocStorage);
245 if (fb->Visual.doubleBufferMode) {
246 assert(fb->Attachment[BUFFER_BACK_LEFT].Renderbuffer);
247 assert(vmesa->front.Base.AllocStorage);
248 }
249 if (fb->Visual.depthBits) {
250 assert(fb->Attachment[BUFFER_DEPTH].Renderbuffer);
251 assert(vmesa->depth.Base.AllocStorage);
252 }
253
254
255 /* Allocate front-buffer */
256 if (vmesa->drawType == GLX_PBUFFER_BIT) {
257 w = vmesa->driDrawable->w;
258 h = vmesa->driDrawable->h;
259
260 vmesa->front.bpp = vmesa->viaScreen->bitsPerPixel;
261 vmesa->front.pitch = buffer_align( w ) << shift; /* bytes, not pixels */
262 vmesa->front.size = vmesa->front.pitch * h;
263
264 if (vmesa->front.map)
265 via_free_draw_buffer(vmesa, &vmesa->front);
266 if (!via_alloc_draw_buffer(vmesa, &vmesa->front))
267 return GL_FALSE;
268
269 } else {
270 w = vmesa->viaScreen->width;
271 h = vmesa->viaScreen->height;
272
273 vmesa->front.bpp = vmesa->viaScreen->bitsPerPixel;
274 vmesa->front.pitch = buffer_align( w ) << shift; /* bytes, not pixels */
275 vmesa->front.size = vmesa->front.pitch * h;
276 if (getenv("ALTERNATE_SCREEN"))
277 vmesa->front.offset = vmesa->front.size;
278 else
279 vmesa->front.offset = 0;
280 vmesa->front.map = (char *) vmesa->driScreen->pFB;
281 }
282
283
284 /* Allocate back-buffer */
285 if (vmesa->hasBack) {
286 vmesa->back.bpp = vmesa->viaScreen->bitsPerPixel;
287 vmesa->back.pitch = (buffer_align( vmesa->driDrawable->w ) << shift);
288 vmesa->back.pitch += extra;
289 vmesa->back.pitch = MIN2(vmesa->back.pitch, vmesa->front.pitch);
290 vmesa->back.size = vmesa->back.pitch * vmesa->driDrawable->h;
291 if (vmesa->back.map)
292 via_free_draw_buffer(vmesa, &vmesa->back);
293 if (!via_alloc_draw_buffer(vmesa, &vmesa->back))
294 return GL_FALSE;
295 }
296 else {
297 if (vmesa->back.map)
298 via_free_draw_buffer(vmesa, &vmesa->back);
299 (void) memset( &vmesa->back, 0, sizeof( vmesa->back ) );
300 }
301
302
303 /* Allocate depth-buffer */
304 if ( vmesa->hasStencil || vmesa->hasDepth ) {
305 vmesa->depth.bpp = vmesa->depthBits;
306 if (vmesa->depth.bpp == 24)
307 vmesa->depth.bpp = 32;
308
309 vmesa->depth.pitch = (buffer_align( vmesa->driDrawable->w ) *
310 (vmesa->depth.bpp/8)) + extra;
311 vmesa->depth.size = vmesa->depth.pitch * vmesa->driDrawable->h;
312
313 if (vmesa->depth.map)
314 via_free_draw_buffer(vmesa, &vmesa->depth);
315 if (!via_alloc_draw_buffer(vmesa, &vmesa->depth)) {
316 return GL_FALSE;
317 }
318 }
319 else {
320 if (vmesa->depth.map)
321 via_free_draw_buffer(vmesa, &vmesa->depth);
322 (void) memset( & vmesa->depth, 0, sizeof( vmesa->depth ) );
323 }
324
325 /* stencil buffer is same as depth buffer */
326 vmesa->stencil.handle = vmesa->depth.handle;
327 vmesa->stencil.size = vmesa->depth.size;
328 vmesa->stencil.offset = vmesa->depth.offset;
329 vmesa->stencil.index = vmesa->depth.index;
330 vmesa->stencil.pitch = vmesa->depth.pitch;
331 vmesa->stencil.bpp = vmesa->depth.bpp;
332 vmesa->stencil.map = vmesa->depth.map;
333 vmesa->stencil.orig = vmesa->depth.orig;
334 vmesa->stencil.origMap = vmesa->depth.origMap;
335
336 if( vmesa->viaScreen->width == vmesa->driDrawable->w &&
337 vmesa->viaScreen->height == vmesa->driDrawable->h ) {
338 vmesa->doPageFlip = vmesa->allowPageFlip;
339 assert(vmesa->back.pitch == vmesa->front.pitch);
340 }
341 else
342 vmesa->doPageFlip = GL_FALSE;
343
344 return GL_TRUE;
345 }
346
347
348 void viaReAllocateBuffers(GLcontext *ctx, GLframebuffer *drawbuffer,
349 GLuint width, GLuint height)
350 {
351 struct via_context *vmesa = VIA_CONTEXT(ctx);
352
353 calculate_buffer_parameters( vmesa, drawbuffer );
354
355 _mesa_resize_framebuffer(ctx, drawbuffer, width, height);
356 }
357
358 static void viaBufferSize(GLframebuffer *buffer, GLuint *width, GLuint *height)
359 {
360 GET_CURRENT_CONTEXT(ctx);
361 struct via_context *vmesa = VIA_CONTEXT(ctx);
362 *width = vmesa->driDrawable->w;
363 *height = vmesa->driDrawable->h;
364 }
365
366 /* Extension strings exported by the Unichrome driver.
367 */
368 const struct dri_extension card_extensions[] =
369 {
370 { "GL_ARB_multisample", GL_ARB_multisample_functions },
371 { "GL_ARB_multitexture", NULL },
372 { "GL_ARB_point_parameters", GL_ARB_point_parameters_functions },
373 { "GL_ARB_texture_env_add", NULL },
374 { "GL_ARB_texture_env_combine", NULL },
375 /* { "GL_ARB_texture_env_dot3", NULL }, */
376 { "GL_ARB_texture_mirrored_repeat", NULL },
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, "via");
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 ctx->Const.MaxTextureLevels = 10;
560 ctx->Const.MaxTextureUnits = 2;
561 ctx->Const.MaxTextureImageUnits = ctx->Const.MaxTextureUnits;
562 ctx->Const.MaxTextureCoordUnits = ctx->Const.MaxTextureUnits;
563
564 ctx->Const.MinLineWidth = 1.0;
565 ctx->Const.MinLineWidthAA = 1.0;
566 ctx->Const.MaxLineWidth = 1.0;
567 ctx->Const.MaxLineWidthAA = 1.0;
568 ctx->Const.LineWidthGranularity = 1.0;
569
570 ctx->Const.MinPointSize = 1.0;
571 ctx->Const.MinPointSizeAA = 1.0;
572 ctx->Const.MaxPointSize = 1.0;
573 ctx->Const.MaxPointSizeAA = 1.0;
574 ctx->Const.PointSizeGranularity = 1.0;
575
576 ctx->Driver.GetBufferSize = viaBufferSize;
577 /* ctx->Driver.ResizeBuffers = _swrast_alloc_buffers; *//* FIXME ?? */
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 FALLBACK(vmesa, VIA_FALLBACK_USER_DISABLE, 1);
660
661 /* I don't understand why this isn't working:
662 */
663 vmesa->vblank_flags =
664 vmesa->viaScreen->irqEnabled ?
665 driGetDefaultVBlankFlags(&vmesa->optionCache) : VBLANK_FLAG_NO_IRQ;
666
667 /* Hack this up in its place:
668 */
669 vmesa->vblank_flags = (getenv("VIA_VSYNC") ?
670 VBLANK_FLAG_SYNC : VBLANK_FLAG_NO_IRQ);
671
672 if (getenv("VIA_PAGEFLIP"))
673 vmesa->allowPageFlip = 1;
674
675 (*dri_interface->getUST)( &vmesa->swap_ust );
676
677
678 vmesa->regMMIOBase = (GLuint *)((GLuint)viaScreen->reg);
679 vmesa->pnGEMode = (GLuint *)((GLuint)viaScreen->reg + 0x4);
680 vmesa->regEngineStatus = (GLuint *)((GLuint)viaScreen->reg + 0x400);
681 vmesa->regTranSet = (GLuint *)((GLuint)viaScreen->reg + 0x43C);
682 vmesa->regTranSpace = (GLuint *)((GLuint)viaScreen->reg + 0x440);
683 vmesa->agpBase = viaScreen->agpBase;
684
685 return GL_TRUE;
686 }
687
688 void
689 viaDestroyContext(__DRIcontextPrivate *driContextPriv)
690 {
691 GET_CURRENT_CONTEXT(ctx);
692 struct via_context *vmesa =
693 (struct via_context *)driContextPriv->driverPrivate;
694 struct via_context *current = ctx ? VIA_CONTEXT(ctx) : NULL;
695 assert(vmesa); /* should never be null */
696
697 /* check if we're deleting the currently bound context */
698 if (vmesa == current) {
699 VIA_FLUSH_DMA(vmesa);
700 _mesa_make_current(NULL, NULL, NULL);
701 }
702
703 if (vmesa) {
704 viaWaitIdle(vmesa);
705 if (vmesa->doPageFlip) {
706 LOCK_HARDWARE(vmesa);
707 if (vmesa->pfCurrentOffset != 0) {
708 fprintf(stderr, "%s - reset pf\n", __FUNCTION__);
709 viaResetPageFlippingLocked(vmesa);
710 }
711 UNLOCK_HARDWARE(vmesa);
712 }
713
714 _swsetup_DestroyContext(vmesa->glCtx);
715 _tnl_DestroyContext(vmesa->glCtx);
716 _ac_DestroyContext(vmesa->glCtx);
717 _swrast_DestroyContext(vmesa->glCtx);
718 /* free the Mesa context */
719 _mesa_destroy_context(vmesa->glCtx);
720 /* release our data */
721 FreeBuffer(vmesa);
722
723 assert (is_empty_list(&vmesa->tex_image_list[VIA_MEM_AGP]));
724 assert (is_empty_list(&vmesa->tex_image_list[VIA_MEM_VIDEO]));
725 assert (is_empty_list(&vmesa->tex_image_list[VIA_MEM_SYSTEM]));
726 assert (is_empty_list(&vmesa->freed_tex_buffers));
727
728 FREE(vmesa);
729 }
730 }
731
732
733 void viaXMesaWindowMoved(struct via_context *vmesa)
734 {
735 __DRIdrawablePrivate *dPriv = vmesa->driDrawable;
736 GLuint bytePerPixel = vmesa->viaScreen->bitsPerPixel >> 3;
737
738 if (!dPriv)
739 return;
740
741 switch (vmesa->glCtx->DrawBuffer->_ColorDrawBufferMask[0]) {
742 case BUFFER_BIT_BACK_LEFT:
743 if (dPriv->numBackClipRects == 0) {
744 vmesa->numClipRects = dPriv->numClipRects;
745 vmesa->pClipRects = dPriv->pClipRects;
746 }
747 else {
748 vmesa->numClipRects = dPriv->numBackClipRects;
749 vmesa->pClipRects = dPriv->pBackClipRects;
750 }
751 break;
752 case BUFFER_BIT_FRONT_LEFT:
753 vmesa->numClipRects = dPriv->numClipRects;
754 vmesa->pClipRects = dPriv->pClipRects;
755 break;
756 default:
757 vmesa->numClipRects = 0;
758 break;
759 }
760
761 if (vmesa->drawW != dPriv->w ||
762 vmesa->drawH != dPriv->h)
763 calculate_buffer_parameters( vmesa, vmesa->glCtx->DrawBuffer );
764
765 vmesa->drawXoff = (GLuint)(((dPriv->x * bytePerPixel) & 0x1f) /
766 bytePerPixel);
767 vmesa->drawX = dPriv->x - vmesa->drawXoff;
768 vmesa->drawY = dPriv->y;
769 vmesa->drawW = dPriv->w;
770 vmesa->drawH = dPriv->h;
771
772 vmesa->front.orig = (vmesa->front.offset +
773 vmesa->drawY * vmesa->front.pitch +
774 vmesa->drawX * bytePerPixel);
775
776 vmesa->front.origMap = (vmesa->front.map +
777 vmesa->drawY * vmesa->front.pitch +
778 vmesa->drawX * bytePerPixel);
779
780 vmesa->back.orig = vmesa->back.offset;
781 vmesa->depth.orig = vmesa->depth.offset;
782 vmesa->back.origMap = vmesa->back.map;
783 vmesa->depth.origMap = vmesa->depth.map;
784
785 viaCalcViewport(vmesa->glCtx);
786 }
787
788 GLboolean
789 viaUnbindContext(__DRIcontextPrivate *driContextPriv)
790 {
791 return GL_TRUE;
792 }
793
794 GLboolean
795 viaMakeCurrent(__DRIcontextPrivate *driContextPriv,
796 __DRIdrawablePrivate *driDrawPriv,
797 __DRIdrawablePrivate *driReadPriv)
798 {
799 if (VIA_DEBUG & DEBUG_DRI) {
800 fprintf(stderr, "driContextPriv = %08x\n", (GLuint)driContextPriv);
801 fprintf(stderr, "driDrawPriv = %08x\n", (GLuint)driDrawPriv);
802 fprintf(stderr, "driReadPriv = %08x\n", (GLuint)driReadPriv);
803 }
804
805 if (driContextPriv) {
806 struct via_context *vmesa =
807 (struct via_context *)driContextPriv->driverPrivate;
808 GLcontext *ctx = vmesa->glCtx;
809 struct gl_framebuffer *drawBuffer, *readBuffer;
810
811 drawBuffer = (GLframebuffer *)driDrawPriv->driverPrivate;
812 readBuffer = (GLframebuffer *)driReadPriv->driverPrivate;
813
814 if ( vmesa->driDrawable != driDrawPriv ) {
815 driDrawableInitVBlank( driDrawPriv, vmesa->vblank_flags );
816 vmesa->driDrawable = driDrawPriv;
817 if ( ! calculate_buffer_parameters( vmesa, drawBuffer ) ) {
818 return GL_FALSE;
819 }
820 }
821
822 _mesa_make_current(vmesa->glCtx, drawBuffer, readBuffer);
823
824 ctx->Driver.DrawBuffer( ctx, ctx->Color.DrawBuffer[0] );
825
826 viaXMesaWindowMoved(vmesa);
827 ctx->Driver.Scissor(vmesa->glCtx,
828 vmesa->glCtx->Scissor.X,
829 vmesa->glCtx->Scissor.Y,
830 vmesa->glCtx->Scissor.Width,
831 vmesa->glCtx->Scissor.Height);
832 }
833 else {
834 _mesa_make_current(NULL, NULL, NULL);
835 }
836
837 return GL_TRUE;
838 }
839
840 void viaGetLock(struct via_context *vmesa, GLuint flags)
841 {
842 __DRIdrawablePrivate *dPriv = vmesa->driDrawable;
843 __DRIscreenPrivate *sPriv = vmesa->driScreen;
844
845 drmGetLock(vmesa->driFd, vmesa->hHWContext, flags);
846
847 DRI_VALIDATE_DRAWABLE_INFO( sPriv, dPriv );
848
849 if (vmesa->sarea->ctxOwner != vmesa->hHWContext) {
850 vmesa->sarea->ctxOwner = vmesa->hHWContext;
851 vmesa->newEmitState = ~0;
852 }
853
854 if (vmesa->lastStamp != dPriv->lastStamp) {
855 viaXMesaWindowMoved(vmesa);
856 driUpdateFramebufferSize(vmesa->glCtx, dPriv);
857 vmesa->newEmitState = ~0;
858 vmesa->lastStamp = dPriv->lastStamp;
859 }
860
861 if (vmesa->doPageFlip &&
862 vmesa->pfCurrentOffset != vmesa->sarea->pfCurrentOffset) {
863 fprintf(stderr, "%s - reset pf\n", __FUNCTION__);
864 viaResetPageFlippingLocked(vmesa);
865 }
866 }
867
868
869 void
870 viaSwapBuffers(__DRIdrawablePrivate *drawablePrivate)
871 {
872 __DRIdrawablePrivate *dPriv = (__DRIdrawablePrivate *)drawablePrivate;
873
874 if (dPriv &&
875 dPriv->driContextPriv &&
876 dPriv->driContextPriv->driverPrivate) {
877 struct via_context *vmesa =
878 (struct via_context *)dPriv->driContextPriv->driverPrivate;
879 GLcontext *ctx = vmesa->glCtx;
880
881 _mesa_notifySwapBuffers(ctx);
882
883 if (ctx->Visual.doubleBufferMode) {
884 if (vmesa->doPageFlip) {
885 viaPageFlip(dPriv);
886 }
887 else {
888 viaCopyBuffer(dPriv);
889 }
890 }
891 else
892 VIA_FLUSH_DMA(vmesa);
893 }
894 else {
895 _mesa_problem(NULL, "viaSwapBuffers: drawable has no context!\n");
896 }
897 }