1
/**************************************************************************
3
* Copyright 1998-1999 Precision Insight, Inc., Cedar Park, Texas.
6
* Permission is hereby granted, free of charge, to any person obtaining a
7
* copy of this software and associated documentation files (the
8
* "Software"), to deal in the Software without restriction, including
9
* without limitation the rights to use, copy, modify, merge, publish,
10
* distribute, sub license, and/or sell copies of the Software, and to
11
* permit persons to whom the Software is furnished to do so, subject to
12
* the following conditions:
14
* The above copyright notice and this permission notice (including the
15
* next paragraph) shall be included in all copies or substantial portions
18
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21
* IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
22
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
* **************************************************************************/
27
/* $XFree86: xc/lib/GL/mesa/src/drv/i830/i830_context.c,v 1.9 2003/02/06 04:18:00 dawes Exp $ */
31
* Jeff Hartmann <jhartmann@2d3d.com>
32
* Graeme Fisher <graeme@2d3d.co.za>
33
* Abraham vd Merwe <abraham@2d3d.co.za>
35
* Heavily Based on I810 driver written by:
36
* Keith Whitwell <keith@tungstengraphics.com>
42
#include "simple_list.h"
43
#include "extensions.h"
46
#include "swrast/swrast.h"
47
#include "swrast_setup/swrast_setup.h"
49
#include "array_cache/acache.h"
51
#include "tnl/t_pipeline.h"
53
#include "i830_screen.h"
56
#include "i830_state.h"
58
#include "i830_span.h"
59
#include "i830_tris.h"
61
#include "i830_ioctl.h"
63
#include <X11/Xlibint.h>
71
/***************************************
72
* Mesa's Driver Functions
73
***************************************/
75
#define DRIVER_DATE "20021115"
77
static const GLubyte *i830DDGetString( GLcontext *ctx, GLenum name )
79
switch (I830_CONTEXT(ctx)->i830Screen->deviceID) {
83
return (GLubyte *)"2d3D, Inc";
85
return (GLubyte *)"Mesa DRI Intel(R) 845G " DRIVER_DATE;
93
return (GLubyte *)"VA Linux, Inc";
95
return (GLubyte *)"Mesa DRI Intel(R) 830M " DRIVER_DATE;
100
case PCI_CHIP_I855_GM:
103
return (GLubyte *)"Tungsten Graphics, Inc";
105
return (GLubyte *)"Mesa DRI Intel(R) 852GM/855GM " DRIVER_DATE;
110
case PCI_CHIP_I865_G:
113
return (GLubyte *)"Tungsten Graphics, Inc";
115
return (GLubyte *)"Mesa DRI Intel(R) 865G " DRIVER_DATE;
123
return (GLubyte *)"Tungsten Graphics, Inc";
125
return (GLubyte *)"Mesa DRI Unknown Intel Chipset " DRIVER_DATE;
133
static void i830BufferSize(GLframebuffer *buffer,
134
GLuint *width, GLuint *height)
136
GET_CURRENT_CONTEXT(ctx);
137
i830ContextPtr imesa = I830_CONTEXT(ctx);
138
/* Need to lock to make sure the driDrawable is uptodate. This
139
* information is used to resize Mesa's software buffers, so it has
142
LOCK_HARDWARE(imesa);
143
*width = imesa->driDrawable->w;
144
*height = imesa->driDrawable->h;
145
UNLOCK_HARDWARE(imesa);
148
/* Enable all the extensions we need */
149
static void i830InitExtensions( GLcontext *ctx )
151
_mesa_enable_imaging_extensions( ctx );
152
_mesa_enable_extension( ctx, "GL_ARB_multitexture" );
153
_mesa_enable_extension( ctx, "GL_ARB_texture_env_add" );
154
_mesa_enable_extension( ctx, "GL_EXT_texture_env_add" );
155
_mesa_enable_extension( ctx, "GL_ARB_texture_env_combine" );
156
_mesa_enable_extension( ctx, "GL_EXT_texture_env_combine" );
157
_mesa_enable_extension( ctx, "GL_EXT_blend_color" );
158
_mesa_enable_extension( ctx, "GL_EXT_blend_minmax" );
159
_mesa_enable_extension( ctx, "GL_EXT_blend_subtract" );
160
_mesa_enable_extension( ctx, "GL_EXT_blend_func_separate" );
161
_mesa_enable_extension( ctx, "GL_EXT_texture_lod_bias" );
162
_mesa_enable_extension( ctx, "GL_EXT_secondary_color" );
163
_mesa_enable_extension( ctx, "GL_EXT_fog_coord" );
165
/* Leave this for later */
167
_mesa_enable_extension( ctx, "GL_EXT_stencil_wrap" );
171
extern const struct gl_pipeline_stage _i830_render_stage;
173
static const struct gl_pipeline_stage *i830_pipeline[] = {
174
&_tnl_vertex_transform_stage,
175
&_tnl_normal_transform_stage,
176
&_tnl_lighting_stage,
177
&_tnl_fog_coordinate_stage,
179
&_tnl_texture_transform_stage,
180
/* REMOVE: point attenuation stage */
182
&_i830_render_stage, /* ADD: unclipped rastersetup-to-dma */
190
static void add_debug_flags( const char *debug )
192
if (strstr(debug, "fall"))
193
I830_DEBUG |= DEBUG_FALLBACKS;
195
if (strstr(debug, "tex"))
196
I830_DEBUG |= DEBUG_TEXTURE;
198
if (strstr(debug, "ioctl"))
199
I830_DEBUG |= DEBUG_IOCTL;
201
if (strstr(debug, "prim"))
202
I830_DEBUG |= DEBUG_PRIMS;
204
if (strstr(debug, "vert"))
205
I830_DEBUG |= DEBUG_VERTS;
207
if (strstr(debug, "state"))
208
I830_DEBUG |= DEBUG_STATE;
210
if (strstr(debug, "verb"))
211
I830_DEBUG |= DEBUG_VERBOSE;
213
if (strstr(debug, "dri"))
214
I830_DEBUG |= DEBUG_DRI;
216
if (strstr(debug, "dma"))
217
I830_DEBUG |= DEBUG_DMA;
219
if (strstr(debug, "san"))
220
I830_DEBUG |= DEBUG_SANITY;
222
if (strstr(debug, "sync"))
223
I830_DEBUG |= DEBUG_SYNC;
225
if (strstr(debug, "sleep"))
226
I830_DEBUG |= DEBUG_SLEEP;
230
GLboolean i830CreateContext( Display *dpy, const __GLcontextModes *mesaVis,
231
__DRIcontextPrivate *driContextPriv,
232
void *sharedContextPrivate)
234
GLcontext *ctx , *shareCtx;
235
i830ContextPtr imesa;
236
__DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
237
i830ScreenPrivate *i830Screen = (i830ScreenPrivate *)sPriv->private;
238
I830SAREAPtr saPriv=(I830SAREAPtr)
239
(((GLubyte *)sPriv->pSAREA)+i830Screen->sarea_priv_offset);
241
/* Allocate i830 context */
242
imesa = (i830ContextPtr) CALLOC_STRUCT(i830_context_t);
243
if (!imesa) return GL_FALSE;
245
/* Allocate the Mesa context */
246
if (sharedContextPrivate)
247
shareCtx = ((i830ContextPtr) sharedContextPrivate)->glCtx;
251
imesa->glCtx = _mesa_create_context(mesaVis, shareCtx, imesa, GL_TRUE);
256
driContextPriv->driverPrivate = imesa;
258
/* Set the maximum texture size small enough that we can guarentee
259
* that both texture units can bind a maximal texture and have them
263
if (i830Screen->textureSize < 2*1024*1024) {
264
ctx->Const.MaxTextureLevels = 9;
265
} else if (i830Screen->textureSize < 8*1024*1024) {
266
ctx->Const.MaxTextureLevels = 10;
268
ctx->Const.MaxTextureLevels = 11;
270
ctx->Const.MaxTextureUnits = 2;
272
ctx->Const.MinLineWidth = 1.0;
273
ctx->Const.MinLineWidthAA = 1.0;
274
ctx->Const.MaxLineWidth = 3.0;
275
ctx->Const.MaxLineWidthAA = 3.0;
276
ctx->Const.LineWidthGranularity = 1.0;
278
ctx->Const.MinPointSize = 1.0;
279
ctx->Const.MinPointSizeAA = 1.0;
280
ctx->Const.MaxPointSize = 255.0;
281
ctx->Const.MaxPointSizeAA = 3.0;
282
ctx->Const.PointSizeGranularity = 1.0;
284
ctx->Driver.GetBufferSize = i830BufferSize;
285
ctx->Driver.ResizeBuffers = _swrast_alloc_buffers;
286
ctx->Driver.GetString = i830DDGetString;
289
ctx->DriverCtx = (void *) imesa;
292
/* Initialize the software rasterizer and helper modules. */
293
_swrast_CreateContext( ctx );
294
_ac_CreateContext( ctx );
295
_tnl_CreateContext( ctx );
296
_swsetup_CreateContext( ctx );
298
/* Install the customized pipeline: */
299
_tnl_destroy_pipeline( ctx );
300
_tnl_install_pipeline( ctx, i830_pipeline );
302
/* Configure swrast to match hardware characteristics: */
303
_swrast_allow_pixel_fog( ctx, GL_FALSE );
304
_swrast_allow_vertex_fog( ctx, GL_TRUE );
307
imesa->display = dpy;
308
imesa->hHWContext = driContextPriv->hHWContext;
309
imesa->driFd = sPriv->fd;
310
imesa->driHwLock = &sPriv->pSAREA->lock;
311
imesa->vertex_format = 0;
313
imesa->hw_stencil = mesaVis->stencilBits && mesaVis->depthBits == 24;
315
switch(mesaVis->depthBits) {
317
imesa->depth_scale = 1.0/0xffff;
318
imesa->depth_clear_mask = ~0;
319
imesa->ClearDepth = 0xffff;
322
imesa->depth_scale = 1.0/0xffffff;
323
imesa->depth_clear_mask = 0x00ffffff;
324
imesa->stencil_clear_mask = 0xff000000;
325
imesa->ClearDepth = 0x00ffffff;
327
case 32: /* Not supported */
331
/* Completely disable stenciling for now, there are some serious issues
335
imesa->hw_stencil = 0;
338
imesa->i830Screen = i830Screen;
339
imesa->driScreen = sPriv;
340
imesa->sarea = saPriv;
341
imesa->glBuffer = NULL;
343
imesa->texHeap = mmInit( 0, i830Screen->textureSize );
344
imesa->RenderIndex = ~0;
346
imesa->upload_cliprects = GL_TRUE;
348
make_empty_list(&imesa->TexObjList);
349
make_empty_list(&imesa->SwappedOut);
351
imesa->CurrentTexObj[0] = 0;
352
imesa->CurrentTexObj[1] = 0;
354
imesa->do_irqs = (imesa->i830Screen->irq_active &&
355
!getenv("I830_NO_IRQS"));
357
_math_matrix_ctr (&imesa->ViewportMatrix);
359
i830InitExtensions (ctx);
360
i830DDInitStateFuncs( ctx );
361
i830DDInitTextureFuncs( ctx );
362
i830InitTriFuncs (ctx);
363
i830DDInitSpanFuncs( ctx );
364
i830DDInitIoctlFuncs( ctx );
366
i830DDInitState (ctx);
369
if (getenv("INTEL_DEBUG"))
370
add_debug_flags( getenv("INTEL_DEBUG") );
371
if (getenv("I830_DEBUG"))
372
add_debug_flags( getenv("I830_DEBUG") );
375
if (getenv("I830_NO_RAST") ||
376
getenv("INTEL_NO_RAST")) {
377
fprintf(stderr, "disabling 3D rasterization\n");
378
FALLBACK(imesa, I830_FALLBACK_USER, 1);
385
void i830DestroyContext(__DRIcontextPrivate *driContextPriv)
387
i830ContextPtr imesa = (i830ContextPtr) driContextPriv->driverPrivate;
389
assert(imesa); /* should never be null */
391
_swsetup_DestroyContext (imesa->glCtx);
392
_tnl_DestroyContext (imesa->glCtx);
393
_ac_DestroyContext (imesa->glCtx);
394
_swrast_DestroyContext (imesa->glCtx);
396
i830FreeVB (imesa->glCtx);
398
/* free the Mesa context */
399
imesa->glCtx->DriverCtx = NULL;
400
_mesa_destroy_context(imesa->glCtx);
406
void i830XMesaSetFrontClipRects( i830ContextPtr imesa )
408
__DRIdrawablePrivate *dPriv = imesa->driDrawable;
410
imesa->numClipRects = dPriv->numClipRects;
411
imesa->pClipRects = dPriv->pClipRects;
412
imesa->drawX = dPriv->x;
413
imesa->drawY = dPriv->y;
415
i830EmitDrawingRectangle( imesa );
416
imesa->upload_cliprects = GL_TRUE;
419
void i830XMesaSetBackClipRects( i830ContextPtr imesa )
421
__DRIdrawablePrivate *dPriv = imesa->driDrawable;
423
if (imesa->sarea->pf_enabled == 0 && dPriv->numBackClipRects == 0) {
424
imesa->numClipRects = dPriv->numClipRects;
425
imesa->pClipRects = dPriv->pClipRects;
426
imesa->drawX = dPriv->x;
427
imesa->drawY = dPriv->y;
429
imesa->numClipRects = dPriv->numBackClipRects;
430
imesa->pClipRects = dPriv->pBackClipRects;
431
imesa->drawX = dPriv->backX;
432
imesa->drawY = dPriv->backY;
435
i830EmitDrawingRectangle( imesa );
436
imesa->upload_cliprects = GL_TRUE;
439
static void i830XMesaWindowMoved( i830ContextPtr imesa )
441
switch (imesa->glCtx->Color.DriverDrawBuffer) {
443
i830XMesaSetFrontClipRects( imesa );
446
i830XMesaSetBackClipRects( imesa );
453
GLboolean i830UnbindContext(__DRIcontextPrivate *driContextPriv)
455
i830ContextPtr imesa = (i830ContextPtr) driContextPriv->driverPrivate;
457
/* Might want to change this so texblend isn't always updated */
458
imesa->dirty |= (I830_UPLOAD_CTX |
459
I830_UPLOAD_BUFFERS |
460
I830_UPLOAD_STIPPLE |
461
I830_UPLOAD_TEXBLEND0 |
462
I830_UPLOAD_TEXBLEND1);
464
if (imesa->CurrentTexObj[0]) imesa->dirty |= I830_UPLOAD_TEX0;
465
if (imesa->CurrentTexObj[1]) imesa->dirty |= I830_UPLOAD_TEX1;
470
GLboolean i830MakeCurrent(__DRIcontextPrivate *driContextPriv,
471
__DRIdrawablePrivate *driDrawPriv,
472
__DRIdrawablePrivate *driReadPriv)
475
if (driContextPriv) {
476
i830ContextPtr imesa = (i830ContextPtr) driContextPriv->driverPrivate;
478
if ( imesa->driDrawable != driDrawPriv ) {
479
/* Shouldn't the readbuffer be stored also? */
480
imesa->driDrawable = driDrawPriv;
481
i830XMesaWindowMoved( imesa );
484
_mesa_make_current2(imesa->glCtx,
485
(GLframebuffer *) driDrawPriv->driverPrivate,
486
(GLframebuffer *) driReadPriv->driverPrivate);
488
if (!imesa->glCtx->Viewport.Width)
489
_mesa_set_viewport(imesa->glCtx, 0, 0,
490
driDrawPriv->w, driDrawPriv->h);
492
_mesa_make_current(0,0);
498
/* Turn on/off page flipping according to the flags in the sarea:
501
i830UpdatePageFlipping( i830ContextPtr imesa )
503
GLcontext *ctx = imesa->glCtx;
506
switch (ctx->Color.DriverDrawBuffer) {
517
if ( imesa->sarea->pf_current_page == 1 )
521
imesa->BufferSetup[I830_DESTREG_CBUFADDR] = imesa->i830Screen->fbOffset;
522
imesa->drawMap = (char *)imesa->driScreen->pFB;
523
imesa->readMap = (char *)imesa->driScreen->pFB;
525
imesa->BufferSetup[I830_DESTREG_CBUFADDR] = imesa->i830Screen->backOffset;
526
imesa->drawMap = imesa->i830Screen->back.map;
527
imesa->readMap = imesa->i830Screen->back.map;
530
imesa->dirty |= I830_UPLOAD_BUFFERS;
533
void i830GetLock( i830ContextPtr imesa, GLuint flags )
535
__DRIdrawablePrivate *dPriv = imesa->driDrawable;
536
__DRIscreenPrivate *sPriv = imesa->driScreen;
537
I830SAREAPtr sarea = imesa->sarea;
538
int me = imesa->hHWContext;
540
drmGetLock(imesa->driFd, imesa->hHWContext, flags);
542
/* If the window moved, may need to set a new cliprect now.
544
* NOTE: This releases and regains the hw lock, so all state
545
* checking must be done *after* this call:
547
DRI_VALIDATE_DRAWABLE_INFO(imesa->display, sPriv, dPriv);
549
/* If we lost context, need to dump all registers to hardware.
550
* Note that we don't care about 2d contexts, even if they perform
551
* accelerated commands, so the DRI locking in the X server is even
552
* more broken than usual.
555
if (sarea->ctxOwner != me) {
556
imesa->upload_cliprects = GL_TRUE;
557
imesa->dirty |= (I830_UPLOAD_CTX |
558
I830_UPLOAD_BUFFERS |
559
I830_UPLOAD_STIPPLE);
561
if(imesa->CurrentTexObj[0]) imesa->dirty |= I830_UPLOAD_TEX0;
562
if(imesa->CurrentTexObj[1]) imesa->dirty |= I830_UPLOAD_TEX1;
563
if(imesa->TexBlendWordsUsed[0]) imesa->dirty |= I830_UPLOAD_TEXBLEND0;
564
if(imesa->TexBlendWordsUsed[1]) imesa->dirty |= I830_UPLOAD_TEXBLEND1;
566
sarea->perf_boxes = imesa->perf_boxes | I830_BOX_LOST_CONTEXT;
567
sarea->ctxOwner = me;
570
/* Shared texture managment - if another client has played with
571
* texture space, figure out which if any of our textures have been
572
* ejected, and update our global LRU.
574
if (sarea->texAge != imesa->texAge) {
575
int sz = 1 << (imesa->i830Screen->logTextureGranularity);
578
/* Have to go right round from the back to ensure stuff ends up
579
* LRU in our local list...
581
for (idx = sarea->texList[I830_NR_TEX_REGIONS].prev ;
582
idx != I830_NR_TEX_REGIONS && nr < I830_NR_TEX_REGIONS ;
583
idx = sarea->texList[idx].prev, nr++) {
584
if (sarea->texList[idx].age > imesa->texAge)
585
i830TexturesGone(imesa, idx * sz, sz, sarea->texList[idx].in_use);
588
if (nr == I830_NR_TEX_REGIONS) {
589
i830TexturesGone(imesa, 0, imesa->i830Screen->textureSize, 0);
590
i830ResetGlobalLRU( imesa );
593
imesa->texAge = sarea->texAge;
596
if (imesa->lastStamp != dPriv->lastStamp) {
597
i830UpdatePageFlipping( imesa );
598
i830XMesaWindowMoved( imesa );
599
imesa->lastStamp = dPriv->lastStamp;
602
sarea->last_quiescent = -1; /* just kill it for now */
605
void i830SwapBuffers(Display *dpy, void *drawablePrivate)
607
__DRIdrawablePrivate *dPriv = (__DRIdrawablePrivate *) drawablePrivate;
609
if (dPriv->driContextPriv && dPriv->driContextPriv->driverPrivate) {
610
i830ContextPtr imesa;
612
imesa = (i830ContextPtr) dPriv->driContextPriv->driverPrivate;
614
if (ctx->Visual.doubleBufferMode) {
615
/* flush pending rendering comands */
616
_mesa_swapbuffers( ctx );
617
if ( imesa->sarea->pf_active &&
618
(dPriv->w * dPriv->h * imesa->i830Screen->cpp) > (300*300*4) ) {
619
i830PageFlip( dPriv );
621
i830CopyBuffer( dPriv );
625
/* XXX this shouldn't be an error but we can't handle it for now */
626
_mesa_problem(NULL, "i830SwapBuffers: drawable has no context!\n");