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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
|
/* $XFree86$ */
/**************************************************************************
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 <kevin@precisioninsight.com>
*
*/
#include "r128_init.h"
#include "r128_mesa.h"
#include "r128_xmesa.h"
#include "r128_context.h"
#include "r128_lock.h"
#include "r128_reg.h"
#include "r128_cce.h"
#include "r128_state.h"
#include "r128_vb.h"
#include "stages.h"
#define TEX0 \
do { \
v->v.tu0 = tc0[i][0]; \
v->v.tv0 = tc0[i][1]; \
} while (0)
#define TEX1 \
do { \
v->v.tu1 = tc1[i][0]; \
v->v.tv1 = tc1[i][1]; \
} while (0)
#define SPC \
do { \
GLubyte *spec = &(VB->Spec[0][i][0]); \
v->v.spec_frgb.r = spec[0]; \
v->v.spec_frgb.g = spec[1]; \
v->v.spec_frgb.b = spec[2]; \
} while (0)
#define FOG \
do { \
GLubyte *spec = &(VB->Spec[0][i][0]); \
v->v.spec_frgb.a = spec[3]; \
} while (0)
#define COL \
do { \
GLubyte *col = &(VB->Color[0]->data[i][0]); \
v->v.dif_argb.a = col[3]; \
v->v.dif_argb.r = col[0]; \
v->v.dif_argb.g = col[1]; \
v->v.dif_argb.b = col[2]; \
} while (0)
#if 1
/* FIXME: These are handled by the Rage 128 */
#define TEX0_4
#define TEX1_4
#else
#define TEX0_4 \
do { \
if (VB->TexCoordPtr[0]->size == 4) { \
GLfloat (*tc)[4] = VB->TexCoordPtr[0]->data; \
v = &(R128_DRIVER_DATA(VB)->verts[start]); \
for (i = start; i < end; i++, v++) { \
float oow = 1.0 / tc[i][3]; \
v->v.rhw *= tc[i][3]; \
v->v.tu0 *= oow; \
v->v.tv0 *= oow; \
} \
} \
} while (0)
#if USE_RHW2
#define TEX1_4 \
do { \
if (VB->TexCoordPtr[1]->size == 4) { \
GLfloat (*tc)[4] = VB->TexCoordPtr[1]->data; \
v = &(R128_DRIVER_DATA(VB)->verts[start]); \
for (i = start; i < end; i++, v++) { \
float oow = 1.0 / tc[i][3]; \
v->v.rhw2 *= tc[i][3]; \
v->v.tu1 *= oow; \
v->v.tv1 *= oow; \
} \
} \
} while (0)
#else
#define TEX1_4
#endif
#endif
#if USE_RHW2
#define COORD \
do { \
GLfloat *win = VB->Win.data[i]; \
v->v.x = win[0]; \
v->v.y = r128height - win[1]; \
v->v.z = (1.0/0x10000) * win[2]; \
v->v.rhw = v->v.rhw2 = win[3]; \
} while (0)
#else
#define COORD \
do { \
GLfloat *win = VB->Win.data[i]; \
v->v.x = win[0]; \
v->v.y = r128height - win[1]; \
v->v.z = (1.0/0x10000) * win[2]; \
v->v.rhw = win[3]; \
} while (0)
#endif
#define NOP
/* Setup the r128 vertex buffer entries */
#define SETUPFUNC(name,win,col,tex0,tex1,tex0_4,tex1_4,spec,fog) \
static void name(struct vertex_buffer *VB, GLuint start, GLuint end) \
{ \
r128ContextPtr r128ctx = R128_CONTEXT(VB->ctx); \
__DRIdrawablePrivate *dPriv = r128ctx->driDrawable; \
r128VertexPtr v; \
GLfloat (*tc0)[4]; \
GLfloat (*tc1)[4]; \
GLfloat r128height = dPriv->h; \
int i; \
\
(void) r128height; (void) r128ctx; \
\
gl_import_client_data(VB, VB->ctx->RenderFlags, \
(VB->ClipOrMask \
? VEC_WRITABLE | VEC_GOOD_STRIDE \
: VEC_GOOD_STRIDE)); \
\
tc0 = VB->TexCoordPtr[0]->data; \
tc1 = VB->TexCoordPtr[1]->data; \
\
v = &(R128_DRIVER_DATA(VB)->verts[start]); \
\
if (VB->ClipOrMask == 0) \
for (i = start; i < end; i++, v++) { \
win; \
col; \
spec; \
fog; \
tex0; \
tex1; \
} \
else \
for (i = start; i < end; i++, v++) { \
if (VB->ClipMask[i] == 0) { \
win; \
spec; \
fog; \
tex0; \
tex1; \
} \
col; \
} \
tex0_4; \
tex1_4; \
}
SETUPFUNC(rs_wt0, COORD, NOP, TEX0, NOP, TEX0_4, NOP, NOP, NOP)
SETUPFUNC(rs_wt1, COORD, NOP, NOP, TEX1, NOP, TEX1_4, NOP, NOP)
SETUPFUNC(rs_wt0t1, COORD, NOP, TEX0, TEX1, TEX0_4, TEX1_4, NOP, NOP)
SETUPFUNC(rs_wft0, COORD, NOP, TEX0, NOP, TEX0_4, NOP, NOP, FOG)
SETUPFUNC(rs_wft1, COORD, NOP, NOP, TEX1, NOP, TEX1_4, NOP, FOG)
SETUPFUNC(rs_wft0t1, COORD, NOP, TEX0, TEX1, TEX0_4, TEX1_4, NOP, FOG)
SETUPFUNC(rs_wg, COORD, COL, NOP, NOP, NOP, NOP, NOP, NOP)
SETUPFUNC(rs_wgs, COORD, COL, NOP, NOP, NOP, NOP, SPC, NOP)
SETUPFUNC(rs_wgt0, COORD, COL, TEX0, NOP, TEX0_4, NOP, NOP, NOP)
SETUPFUNC(rs_wgt1, COORD, COL, NOP, TEX1, NOP, TEX1_4, NOP, NOP)
SETUPFUNC(rs_wgt0t1, COORD, COL, TEX0, TEX1, TEX0_4, TEX1_4, NOP, NOP)
SETUPFUNC(rs_wgst0, COORD, COL, TEX0, NOP, TEX0_4, NOP, SPC, NOP)
SETUPFUNC(rs_wgst1, COORD, COL, NOP, TEX1, NOP, TEX1_4, SPC, NOP)
SETUPFUNC(rs_wgst0t1, COORD, COL, TEX0, TEX1, TEX0_4, TEX1_4, SPC, NOP)
SETUPFUNC(rs_wgf, COORD, COL, NOP, NOP, NOP, NOP, NOP, FOG)
SETUPFUNC(rs_wgfs, COORD, COL, NOP, NOP, NOP, NOP, SPC, FOG)
SETUPFUNC(rs_wgft0, COORD, COL, TEX0, NOP, TEX0_4, NOP, NOP, FOG)
SETUPFUNC(rs_wgft1, COORD, COL, NOP, TEX1, NOP, TEX1_4, NOP, FOG)
SETUPFUNC(rs_wgft0t1, COORD, COL, TEX0, TEX1, TEX0_4, TEX1_4, NOP, FOG)
SETUPFUNC(rs_wgfst0, COORD, COL, TEX0, NOP, TEX0_4, NOP, SPC, FOG)
SETUPFUNC(rs_wgfst1 , COORD, COL, NOP, TEX1, NOP, TEX1_4, SPC, FOG)
SETUPFUNC(rs_wgfst0t1, COORD, COL, TEX0, TEX1, TEX0_4, TEX1_4, SPC, FOG)
SETUPFUNC(rs_t0, NOP, NOP, TEX0, NOP, TEX0_4, NOP, NOP, NOP)
SETUPFUNC(rs_t1, NOP, NOP, NOP, TEX1, NOP, TEX1_4, NOP, NOP)
SETUPFUNC(rs_t0t1, NOP, NOP, TEX0, TEX1, TEX0_4, TEX1_4, NOP, NOP)
SETUPFUNC(rs_f, NOP, NOP, NOP, NOP, NOP, NOP, NOP, FOG)
SETUPFUNC(rs_ft0, NOP, NOP, TEX0, NOP, TEX0_4, NOP, NOP, FOG)
SETUPFUNC(rs_ft1, NOP, NOP, NOP, TEX1, NOP, TEX1_4, NOP, FOG)
SETUPFUNC(rs_ft0t1, NOP, NOP, TEX0, TEX1, TEX0_4, TEX1_4, NOP, FOG)
SETUPFUNC(rs_g, NOP, COL, NOP, NOP, NOP, NOP, NOP, NOP)
SETUPFUNC(rs_gs, NOP, COL, NOP, NOP, NOP, NOP, SPC, NOP)
SETUPFUNC(rs_gt0, NOP, COL, TEX0, NOP, TEX0_4, NOP, NOP, NOP)
SETUPFUNC(rs_gt1, NOP, COL, NOP, TEX1, NOP, TEX1_4, NOP, NOP)
SETUPFUNC(rs_gt0t1, NOP, COL, TEX0, TEX1, TEX0_4, TEX1_4, NOP, NOP)
SETUPFUNC(rs_gst0, NOP, COL, TEX0, NOP, TEX0_4, NOP, SPC, NOP)
SETUPFUNC(rs_gst1, NOP, COL, NOP, TEX1, NOP, TEX1_4, SPC, NOP)
SETUPFUNC(rs_gst0t1, NOP, COL, TEX0, TEX1, TEX0_4, TEX1_4, SPC, NOP)
SETUPFUNC(rs_gf, NOP, COL, NOP, NOP, NOP, NOP, NOP, FOG)
SETUPFUNC(rs_gfs, NOP, COL, NOP, NOP, NOP, NOP, SPC, FOG)
SETUPFUNC(rs_gft0, NOP, COL, TEX0, NOP, TEX0_4, NOP, NOP, FOG)
SETUPFUNC(rs_gft1, NOP, COL, NOP, TEX1, NOP, TEX1_4, NOP, FOG)
SETUPFUNC(rs_gft0t1, NOP, COL, TEX0, TEX1, TEX0_4, TEX1_4, NOP, FOG)
SETUPFUNC(rs_gfst0, NOP, COL, TEX0, NOP, TEX0_4, NOP, SPC, FOG)
SETUPFUNC(rs_gfst1, NOP, COL, NOP, TEX1, NOP, TEX1_4, SPC, FOG)
SETUPFUNC(rs_gfst0t1, NOP, COL, TEX0, TEX1, TEX0_4, TEX1_4, SPC, FOG)
static void rs_invalid(struct vertex_buffer *VB, GLuint start, GLuint end)
{
R128_DEBUG(("r128RasterSetup(): invalid setup function\n"));
}
typedef void (*setupFunc)(struct vertex_buffer *, GLuint, GLuint);
static setupFunc setup_func[0x80];
/* Initialize the table of vertex buffer setup functions */
void r128SetupInit(void)
{
int i;
for (i = 0; i < 0x80; i++) setup_func[i] = rs_invalid;
/* Funcs to build vertices from scratch */
setup_func[R128_WIN_BIT|R128_TEX0_BIT] = rs_wt0;
setup_func[R128_WIN_BIT|R128_TEX1_BIT] = rs_wt1;
setup_func[R128_WIN_BIT|R128_TEX0_BIT|R128_TEX1_BIT] = rs_wt0t1;
setup_func[R128_WIN_BIT|R128_FOG_BIT|R128_TEX0_BIT] = rs_wft0;
setup_func[R128_WIN_BIT|R128_FOG_BIT|R128_TEX1_BIT] = rs_wft1;
setup_func[R128_WIN_BIT|R128_FOG_BIT|R128_TEX0_BIT|R128_TEX1_BIT] = rs_wft0t1;
setup_func[R128_WIN_BIT|R128_RGBA_BIT] = rs_wg;
setup_func[R128_WIN_BIT|R128_RGBA_BIT|R128_SPEC_BIT] = rs_wgs;
setup_func[R128_WIN_BIT|R128_RGBA_BIT|R128_TEX0_BIT] = rs_wgt0;
setup_func[R128_WIN_BIT|R128_RGBA_BIT|R128_TEX1_BIT] = rs_wgt1;
setup_func[R128_WIN_BIT|R128_RGBA_BIT|R128_TEX0_BIT|R128_TEX1_BIT] = rs_wgt0t1;
setup_func[R128_WIN_BIT|R128_RGBA_BIT|R128_SPEC_BIT|R128_TEX0_BIT] = rs_wgst0;
setup_func[R128_WIN_BIT|R128_RGBA_BIT|R128_SPEC_BIT|R128_TEX1_BIT] = rs_wgst1;
setup_func[R128_WIN_BIT|R128_RGBA_BIT|R128_SPEC_BIT|R128_TEX0_BIT|R128_TEX1_BIT] = rs_wgst0t1;
setup_func[R128_WIN_BIT|R128_RGBA_BIT|R128_FOG_BIT] = rs_wgf;
setup_func[R128_WIN_BIT|R128_RGBA_BIT|R128_FOG_BIT|R128_SPEC_BIT] = rs_wgfs;
setup_func[R128_WIN_BIT|R128_RGBA_BIT|R128_FOG_BIT|R128_TEX0_BIT] = rs_wgft0;
setup_func[R128_WIN_BIT|R128_RGBA_BIT|R128_FOG_BIT|R128_TEX1_BIT] = rs_wgft1;
setup_func[R128_WIN_BIT|R128_RGBA_BIT|R128_FOG_BIT|R128_TEX0_BIT|R128_TEX1_BIT] = rs_wgft0t1;
setup_func[R128_WIN_BIT|R128_RGBA_BIT|R128_FOG_BIT|R128_SPEC_BIT|R128_TEX0_BIT] = rs_wgfst0;
setup_func[R128_WIN_BIT|R128_RGBA_BIT|R128_FOG_BIT|R128_SPEC_BIT|R128_TEX1_BIT] = rs_wgfst1;
setup_func[R128_WIN_BIT|R128_RGBA_BIT|R128_FOG_BIT|R128_SPEC_BIT|R128_TEX0_BIT|R128_TEX1_BIT] = rs_wgfst0t1;
/* Funcs to repair vertices */
setup_func[R128_TEX0_BIT] = rs_t0;
setup_func[R128_TEX1_BIT] = rs_t1;
setup_func[R128_TEX0_BIT|R128_TEX1_BIT] = rs_t0t1;
setup_func[R128_FOG_BIT] = rs_f;
setup_func[R128_FOG_BIT|R128_TEX0_BIT] = rs_ft0;
setup_func[R128_FOG_BIT|R128_TEX1_BIT] = rs_ft1;
setup_func[R128_FOG_BIT|R128_TEX0_BIT|R128_TEX1_BIT] = rs_ft0t1;
setup_func[R128_RGBA_BIT] = rs_g;
setup_func[R128_RGBA_BIT|R128_SPEC_BIT] = rs_gs;
setup_func[R128_RGBA_BIT|R128_TEX0_BIT] = rs_gt0;
setup_func[R128_RGBA_BIT|R128_TEX1_BIT] = rs_gt1;
setup_func[R128_RGBA_BIT|R128_TEX0_BIT|R128_TEX1_BIT] = rs_gt0t1;
setup_func[R128_RGBA_BIT|R128_SPEC_BIT|R128_TEX0_BIT] = rs_gst0;
setup_func[R128_RGBA_BIT|R128_SPEC_BIT|R128_TEX1_BIT] = rs_gst1;
setup_func[R128_RGBA_BIT|R128_SPEC_BIT|R128_TEX0_BIT|R128_TEX1_BIT] = rs_gst0t1;
setup_func[R128_RGBA_BIT|R128_FOG_BIT] = rs_gf;
setup_func[R128_RGBA_BIT|R128_FOG_BIT|R128_SPEC_BIT] = rs_gfs;
setup_func[R128_RGBA_BIT|R128_FOG_BIT|R128_TEX0_BIT] = rs_gft0;
setup_func[R128_RGBA_BIT|R128_FOG_BIT|R128_TEX1_BIT] = rs_gft1;
setup_func[R128_RGBA_BIT|R128_FOG_BIT|R128_TEX0_BIT|R128_TEX1_BIT] = rs_gft0t1;
setup_func[R128_RGBA_BIT|R128_FOG_BIT|R128_SPEC_BIT|R128_TEX0_BIT] = rs_gfst0;
setup_func[R128_RGBA_BIT|R128_FOG_BIT|R128_SPEC_BIT|R128_TEX1_BIT] = rs_gfst1;
setup_func[R128_RGBA_BIT|R128_FOG_BIT|R128_SPEC_BIT|R128_TEX0_BIT|R128_TEX1_BIT] = rs_gfst0t1;
}
/* Initialize the vertex buffer setup functions based on the current
rendering state */
void r128ChooseRasterSetupFunc(GLcontext *ctx)
{
r128ContextPtr r128ctx = R128_CONTEXT(ctx);
int funcIndex = R128_WIN_BIT | R128_RGBA_BIT;
if (ctx->Texture.Enabled & 0xf) {
if (ctx->Texture.Unit[0].EnvMode == GL_REPLACE)
funcIndex &= ~R128_RGBA_BIT;
funcIndex |= R128_TEX0_BIT;
}
if (ctx->Texture.Enabled & 0xf0)
funcIndex |= R128_TEX1_BIT;
/* FIXME: Verify this works properly */
if (ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR)
funcIndex |= R128_SPEC_BIT;
if (ctx->FogMode == FOG_FRAGMENT)
funcIndex |= R128_FOG_BIT;
r128ctx->SetupIndex = funcIndex;
ctx->Driver.RasterSetup = setup_func[funcIndex];
}
/* Check to see if any updates of the vertex buffer entries are needed */
void r128CheckPartialRasterSetup(GLcontext *ctx,
struct gl_pipeline_stage *s)
{
r128ContextPtr r128ctx = R128_CONTEXT(ctx);
int tmp = r128ctx->SetupDone;
s->type = 0;
r128ctx->SetupDone = GL_FALSE;
if ((ctx->Array.Summary & VERT_OBJ_ANY) == 0) return;
if (ctx->IndirectTriangles) return;
r128ctx->SetupDone = tmp;
}
/* Update the vertex buffer entries, if necessary */
void r128PartialRasterSetup(struct vertex_buffer *VB)
{
r128ContextPtr r128ctx = R128_CONTEXT(VB->ctx);
int new = VB->pipeline->new_outputs;
int available = VB->pipeline->outputs;
int index = 0;
if (new & VERT_WIN) {
new = available;
index |= R128_WIN_BIT | R128_FOG_BIT;
}
if (new & VERT_RGBA) index |= R128_RGBA_BIT | R128_SPEC_BIT;
if (new & VERT_TEX0_ANY) index |= R128_TEX0_BIT;
if (new & VERT_TEX1_ANY) index |= R128_TEX1_BIT;
if (new & VERT_FOG_COORD) index |= R128_FOG_BIT;
r128ctx->SetupDone &= ~index;
index &= r128ctx->SetupIndex;
r128ctx->SetupDone |= index;
if (index) setup_func[index & ~R128_ALPHA_BIT](VB, VB->Start, VB->Count);
}
/* Perform the raster setup for the fast path, if using CVA */
void r128DoRasterSetup(struct vertex_buffer *VB)
{
GLcontext *ctx = VB->ctx;
if (VB->Type == VB_CVA_PRECALC) r128PartialRasterSetup(VB);
else if (ctx->Driver.RasterSetup) ctx->Driver.RasterSetup(VB,
VB->CopyStart,
VB->Count);
}
/* Resize an existing vertex buffer */
void r128ResizeVB(struct vertex_buffer *VB, GLuint size)
{
r128VertexBufferPtr r128vb = R128_DRIVER_DATA(VB);
while (r128vb->size < size)
r128vb->size *= 2;
free(r128vb->vert_store);
r128vb->vert_store = malloc(sizeof(r128Vertex) * r128vb->size + 31);
if (!r128vb->vert_store) {
fprintf(stderr, "Cannot allocate vertex store! Exiting...\n");
exit(1);
}
r128vb->verts = (r128VertexPtr)(((CARD32)r128vb->vert_store + 31) & ~31);
gl_vector1ui_free(&r128vb->clipped_elements);
gl_vector1ui_alloc(&r128vb->clipped_elements,
VEC_WRITABLE, r128vb->size, 32);
if (!r128vb->clipped_elements.start) {
fprintf(stderr, "Cannot allocate clipped elements! Exiting...\n");
exit(1);
}
free(VB->ClipMask);
VB->ClipMask = (GLubyte *)malloc(sizeof(GLubyte) * r128vb->size);
if (!VB->ClipMask) {
fprintf(stderr, "Cannot allocate clipmask! Exiting...\n");
exit(1);
}
}
/* Create a new device-dependent vertex buffer */
void r128DDRegisterVB(struct vertex_buffer *VB)
{
r128VertexBufferPtr r128vb;
r128vb = (r128VertexBufferPtr)calloc(1, sizeof(*r128vb));
r128vb->size = VB->Size * 2;
r128vb->vert_store = malloc(sizeof(r128Vertex) * r128vb->size + 31);
if (!r128vb->vert_store) {
fprintf(stderr, "Cannot allocate vertex store! Exiting...\n");
exit(1);
}
r128vb->verts = (r128VertexPtr)(((CARD32)r128vb->vert_store + 31) & ~31);
gl_vector1ui_alloc(&r128vb->clipped_elements,
VEC_WRITABLE, r128vb->size, 32);
if (!r128vb->clipped_elements.start) {
fprintf(stderr, "Cannot allocate clipped elements! Exiting...\n");
exit(1);
}
free(VB->ClipMask);
VB->ClipMask = (GLubyte *)malloc(sizeof(GLubyte) * r128vb->size);
if (!VB->ClipMask) {
fprintf(stderr, "Cannot allocate clipmask! Exiting...\n");
exit(1);
}
VB->driver_data = r128vb;
}
/* Destroy a device-dependent vertex buffer */
void r128DDUnregisterVB(struct vertex_buffer *VB)
{
r128VertexBufferPtr r128vb = R128_DRIVER_DATA(VB);
if (r128vb) {
if (r128vb->vert_store) free(r128vb->vert_store);
gl_vector1ui_free(&r128vb->clipped_elements);
free(r128vb);
VB->driver_data = 0;
}
}
/* Get a new VB from the pool of vertex buffers in AGP space */
static r128CCEVertBufPtr r128CCEGetNewVB(r128ContextPtr r128ctx)
{
#define R128_VB_SIZE 4096
/* FIXME: This only works for a single client right now!!! */
if (r128ctx->vb) {
r128ctx->vb->start += R128_VB_SIZE;
if (r128ctx->vb->start >
r128ctx->r128Screen->vbStart + r128ctx->r128Screen->vbSize)
r128ctx->vb->start = r128ctx->r128Screen->vbStart;
} else {
r128ctx->vb = (r128CCEVertBufPtr)malloc(sizeof(*r128ctx->vb));
r128ctx->vb->start = r128ctx->r128Screen->vbStart;
r128ctx->vb->size = R128_VB_SIZE/sizeof(r128_vertex);
}
r128ctx->vb->count = 0;
r128ctx->vb->done = GL_FALSE;
return r128ctx->vb;
}
/* Release a VB back to the pool of vertex buffers in AGP space */
static void r128CCEReleaseVB(r128ContextPtr r128ctx)
{
r128ctx->vb->done = GL_TRUE;
}
/* Reserve space in a VB to store `verts' vertices. If adding these
vertices to the VB would exceed the size of the VB, then flush the
current VB, get a new one from the pool of VBs, and reserve space in
the new VB for the `verts' vertices. */
r128_vertex *r128CCEAllocVBSpace(r128ContextPtr r128ctx, int verts)
{
r128_vertex *vbptr;
r128CCEVertBufPtr vb = r128ctx->vb;
if (!vb || vb->done) vb = r128CCEGetNewVB(r128ctx);
if (vb->count + verts > vb->size) {
r128CCEFlushVB(r128ctx);
vb = r128CCEGetNewVB(r128ctx);
}
vbptr = (r128_vertex *)(r128ctx->r128Screen->agp + vb->start) + vb->count;
vb->count += verts;
return vbptr;
}
/* Send a VB to the Rage 128 to be processed */
void r128CCEFlushVB(r128ContextPtr r128ctx)
{
unsigned char *R128MMIO = r128ctx->r128Screen->mmio;
if (!r128ctx->vb ||
!r128ctx->vb->count ||
r128ctx->vb->done) return;
/* Make sure vertices are in memory, and not stuck in the WC cache */
R128FlushWCMemory();
/* FIXME: The textures referenced in this VB cannot be removed until
after this buffer has been processed. ATI suggests loading new
textures via HOSTDATA command packets to solve this problem. */
/* FIXME: We need a way to detect if the VB is free or if it is
still in use. By waiting for idle, we know that the currently
processing buffer is complete, but waiting is slow. A better
solution is to use a counter that is stored in one of the
GUI_SCRATCH_REGs, which gets incremented after each VB is
processed. Also, we will need to associate the count value with
each VB in the pool of VBs -- this will need to be stored in the
SAREA. */
#if 1
/* FIXME: Is this _really_ needed? */
R128CCEWaitForIdle(r128ctx->r128Screen);
#endif
BEGIN_CLIP_LOOP(r128ctx);
R128CCE_WAIT_LOCKED(5);
R128CCE3(R128_CCE_PACKET3_3D_RNDR_GEN_INDX_PRIM, 3);
R128CCE(r128ctx->vb->start);
R128CCE(r128ctx->vb->count);
R128CCE(R128_FULL_VERTEX_FORMAT);
R128CCE(R128_CCE_VC_CNTL_PRIM_TYPE_TRI_LIST |
R128_CCE_VC_CNTL_PRIM_WALK_LIST |
(r128ctx->vb->count << R128_CCE_VC_CNTL_NUM_SHIFT));
R128CCE_END_LOCKED();
END_CLIP_LOOP(r128ctx);
r128CCEReleaseVB(r128ctx);
}
|