summaryrefslogtreecommitdiff
path: root/xc/lib/GL/mesa/src/drv/r128/r128_context.c
blob: e7be008538724d9996e523c00eadc6f8baa011d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/* $XFree86: xc/lib/GL/mesa/src/drv/r128/r128_context.c,v 1.3 2000/12/04 19:21:44 dawes Exp $ */
/**************************************************************************

Copyright 1999, 2000 ATI Technologies Inc. and Precision Insight, Inc.,
                                               Cedar Park, Texas.
All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
on the rights to use, copy, modify, merge, publish, distribute, sub
license, and/or sell copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice (including the next
paragraph) shall be included in all copies or substantial portions of the
Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
ATI, PRECISION INSIGHT AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.

**************************************************************************/

/*
 * Authors:
 *   Kevin E. Martin <martin@valinux.com>
 *   Gareth Hughes <gareth@valinux.com>
 *
 */

#include <stdlib.h>

#include "r128_context.h"
#include "r128_ioctl.h"
#include "r128_dd.h"
#include "r128_state.h"
#include "r128_span.h"
#include "r128_tex.h"
#include "r128_vb.h"
#include "r128_pipeline.h"

#include "context.h"
#include "simple_list.h"
#include "mem.h"

#ifndef R128_DEBUG
int R128_DEBUG = (0
/*		  | DEBUG_ALWAYS_SYNC */
/*		  | DEBUG_VERBOSE_API */
/*		  | DEBUG_VERBOSE_MSG */
/*		  | DEBUG_VERBOSE_LRU */
/*		  | DEBUG_VERBOSE_DRI */
/*		  | DEBUG_VERBOSE_IOCTL */
/*		  | DEBUG_VERBOSE_2D */
   );
#endif

/* Create the device specific context */
GLboolean r128CreateContext(Display *dpy, GLvisual *glVisual,
			    __DRIcontextPrivate *driContextPriv)
{
    r128ContextPtr  r128ctx;
    r128ScreenPtr   r128scrn;
    GLcontext      *ctx    = driContextPriv->mesaContext;
    int             i;
    char           *v;
    __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;

    r128ctx = (r128ContextPtr)Xcalloc(1, sizeof(*r128ctx));
    if (!r128ctx) return GL_FALSE;

    /* Initialize r128Context */
    r128ctx->glCtx       = ctx;
    r128ctx->display     = dpy;

    r128ctx->driContext  = driContextPriv;
    r128ctx->driScreen   = sPriv;
    r128ctx->driDrawable = NULL; /* Set by XMesaMakeCurrent */

    r128ctx->hHWContext  = driContextPriv->hHWContext;
    r128ctx->driFd       = sPriv->fd;
    r128ctx->driHwLock   = &sPriv->pSAREA->lock;

    r128scrn = r128ctx->r128Screen = (r128ScreenPtr)(sPriv->private);

    r128ctx->sarea = (R128SAREAPriv *)((char *)sPriv->pSAREA +
				       sizeof(XF86DRISAREARec));

    r128ctx->CurrentTexObj[0] = NULL;
    r128ctx->CurrentTexObj[1] = NULL;
    make_empty_list(&r128ctx->SwappedOut);
    for (i = 0; i < r128scrn->NRTexHeaps; i++) {
	make_empty_list(&r128ctx->TexObjList[i]);
	r128ctx->texHeap[i] = mmInit(0, r128scrn->texSize[i]);
	r128ctx->lastTexAge[i] = -1;
    }
    r128ctx->lastTexHeap = r128scrn->NRTexHeaps;

    r128ctx->BufferSize  = glVisual->DepthBits;
    r128ctx->DepthSize   = glVisual->DepthBits;
    r128ctx->StencilSize = glVisual->StencilBits;

    r128ctx->useFastPath = GL_FALSE;
    r128ctx->lod_bias = 0x3f;

    r128ctx->num_verts = 0;
    r128ctx->vert_buf = NULL;

    r128ctx->elt_buf = NULL;
    r128ctx->retained_buf = NULL;
    r128ctx->vert_heap = r128ctx->r128Screen->buffers->list->address;

#if 0
    r128ctx->CCEbuf= (CARD32 *)MALLOC(sizeof(*r128ctx->CCEbuf) *
				      r128scrn->ringEntries);
    r128ctx->CCEcount = 0;
#endif

    if ((v = getenv("LIBGL_CCE_TIMEOUT")))
	r128ctx->CCEtimeout   = strtoul(v, NULL, 10);
    else
	r128ctx->CCEtimeout   = (R128_DEFAULT_TOTAL_CCE_TIMEOUT /
				 R128_DEFAULT_CCE_TIMEOUT);
    if (r128ctx->CCEtimeout <= 0) r128ctx->CCEtimeout = 1;

    /* Initialize GLcontext */
    ctx->DriverCtx = (void *)r128ctx;

    r128DDInitExtensions(ctx);

    r128DDInitDriverFuncs(ctx);
    r128DDInitIoctlFuncs(ctx);
    r128DDInitStateFuncs(ctx);
    r128DDInitSpanFuncs(ctx);
    r128DDInitTextureFuncs(ctx);

    ctx->Driver.TriangleCaps = (DD_TRI_CULL
				| DD_TRI_LIGHT_TWOSIDE
				| DD_TRI_OFFSET);

    /* Ask Mesa to clip fog coordinates for us
     */
    ctx->TriangleCaps |= DD_CLIP_FOG_COORD;

    /* Reset Mesa's current 2D texture pointers to the driver's textures */
    ctx->Shared->DefaultD[2][0].DriverData = NULL;
    ctx->Shared->DefaultD[2][1].DriverData = NULL;

    /* KW: Set the maximum texture size small enough that we can
     * guarentee that both texture units can bind a maximal texture
     * and have them both in on-card memory at once.  (Kevin or
     * Gareth: Please check these numbers are OK)
     */
    if (r128scrn->texSize[0] < 2*1024*1024) {
       ctx->Const.MaxTextureLevels = 9;
       ctx->Const.MaxTextureSize = 1<<8;
    } else if (r128scrn->texSize[0] < 8*1024*1024) {
       ctx->Const.MaxTextureLevels = 10;
       ctx->Const.MaxTextureSize = 1<<9;
    } else {
       ctx->Const.MaxTextureLevels = 11;
       ctx->Const.MaxTextureSize = 1<<10;
    }

    ctx->Const.MaxTextureUnits = 2;

#if ENABLE_PERF_BOXES
    if (getenv("LIBGL_PERFORMANCE_BOXES"))
	r128ctx->boxes = 1;
    else
	r128ctx->boxes = 0;
#endif

    /* If Mesa has current a vertex buffer, make sure the driver's VB
       data is up to date */
    if (ctx->VB) r128DDRegisterVB(ctx->VB);

    /* Register the fast path */
    if (ctx->NrPipelineStages)
	ctx->NrPipelineStages =
	    r128DDRegisterPipelineStages(ctx->PipelineStage,
					 ctx->PipelineStage,
					 ctx->NrPipelineStages);

    r128DDInitState(r128ctx);

    driContextPriv->driverPrivate = (void *)r128ctx;

    return GL_TRUE;
}

/* Destroy the device specific context */
void r128DestroyContext(r128ContextPtr r128ctx)
{
    if (r128ctx) {
	r128TexObjPtr t, next_t;
	int           i;

#if 0
	FREE( r128ctx->CCEbuf );
#endif

	for (i = 0; i < r128ctx->r128Screen->NRTexHeaps; i++) {
	    foreach_s (t, next_t, &r128ctx->TexObjList[i])
		r128DestroyTexObj(r128ctx, t);
	}

	foreach_s (t, next_t, &r128ctx->SwappedOut)
	    r128DestroyTexObj(r128ctx, t);

	Xfree(r128ctx);
    }

#if 0
    glx_fini_prof();
#endif
}

/* Load the device specific context into the hardware.  The actual
   setting of the hardware state is done in the r128UpdateHWState(). */
r128ContextPtr r128MakeCurrent(r128ContextPtr oldCtx,
			       r128ContextPtr newCtx,
			       __DRIdrawablePrivate *dPriv)
{
    if (oldCtx) {
	if (!R128CCE_USE_RING_BUFFER(newCtx->r128Screen->CCEMode))
	    newCtx->dirty |= R128_REQUIRE_QUIESCENCE;
	if (oldCtx != newCtx) {
	    newCtx->new_state |= R128_NEW_CONTEXT;
	    newCtx->dirty = R128_UPLOAD_ALL;
	}
	if (oldCtx->driDrawable != dPriv) {
	    newCtx->new_state |= R128_NEW_WINDOW;
	}
    } else {
	newCtx->new_state |= R128_NEW_CONTEXT;
	newCtx->dirty = R128_UPLOAD_ALL;
    }

    newCtx->driDrawable = dPriv;

    return newCtx;
}