include texobj.h to silence warnings
[mesa.git] / src / mesa / drivers / dri / tdfx / tdfx_span.c
1 /* -*- mode: c; c-basic-offset: 3 -*-
2 *
3 * Copyright 2000 VA Linux Systems Inc., Fremont, California.
4 *
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * VA LINUX SYSTEMS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
23 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26 /* $XFree86: xc/lib/GL/mesa/src/drv/tdfx/tdfx_span.c,v 1.7 2002/10/30 12:52:00 alanh Exp $ */
27
28 /*
29 * Original rewrite:
30 * Gareth Hughes <gareth@valinux.com>, 29 Sep - 1 Oct 2000
31 *
32 * Authors:
33 * Gareth Hughes <gareth@valinux.com>
34 * Brian Paul <brianp@valinux.com>
35 * Keith Whitwell <keith@tungstengraphics.com>
36 *
37 */
38
39 #include "tdfx_context.h"
40 #include "tdfx_lock.h"
41 #include "tdfx_span.h"
42 #include "tdfx_render.h"
43 #include "swrast/swrast.h"
44
45
46 #define DBG 0
47
48
49 #define LOCAL_VARS \
50 __DRIdrawablePrivate *dPriv = fxMesa->driDrawable; \
51 tdfxScreenPrivate *fxPriv = fxMesa->fxScreen; \
52 GLuint pitch = (fxMesa->glCtx->Color.DrawBuffer == GL_FRONT) \
53 ? (fxMesa->screen_width * BYTESPERPIXEL) : \
54 (info.strideInBytes); \
55 GLuint height = fxMesa->height; \
56 char *buf = (char *)((char *)info.lfbPtr + \
57 dPriv->x * fxPriv->cpp + \
58 dPriv->y * pitch); \
59 GLuint p; \
60 (void) buf; (void) p;
61
62
63 #define CLIPPIXEL( _x, _y ) ( _x >= minx && _x < maxx && \
64 _y >= miny && _y < maxy )
65
66 #define CLIPSPAN( _x, _y, _n, _x1, _n1, _i ) \
67 if ( _y < miny || _y >= maxy ) { \
68 _n1 = 0, _x1 = x; \
69 } else { \
70 _n1 = _n; \
71 _x1 = _x; \
72 if ( _x1 < minx ) _i += (minx-_x1), n1 -= (minx-_x1), _x1 = minx; \
73 if ( _x1 + _n1 >= maxx ) n1 -= (_x1 + n1 - maxx); \
74 }
75
76 #define Y_FLIP(_y) (height - _y - 1)
77
78
79 #define HW_WRITE_LOCK() \
80 tdfxContextPtr fxMesa = TDFX_CONTEXT(ctx); \
81 GrLfbInfo_t info; \
82 FLUSH_BATCH( fxMesa ); \
83 UNLOCK_HARDWARE( fxMesa ); \
84 LOCK_HARDWARE( fxMesa ); \
85 info.size = sizeof(GrLfbInfo_t); \
86 if ( fxMesa->Glide.grLfbLock( GR_LFB_WRITE_ONLY, \
87 fxMesa->DrawBuffer, LFB_MODE, \
88 GR_ORIGIN_UPPER_LEFT, FXFALSE, &info ) ) \
89 {
90
91 #define HW_WRITE_UNLOCK() \
92 fxMesa->Glide.grLfbUnlock( GR_LFB_WRITE_ONLY, fxMesa->DrawBuffer );\
93 }
94
95
96 #define HW_READ_LOCK() \
97 tdfxContextPtr fxMesa = TDFX_CONTEXT(ctx); \
98 GrLfbInfo_t info; \
99 FLUSH_BATCH( fxMesa ); \
100 UNLOCK_HARDWARE( fxMesa ); \
101 LOCK_HARDWARE( fxMesa ); \
102 info.size = sizeof(GrLfbInfo_t); \
103 if ( fxMesa->Glide.grLfbLock( GR_LFB_READ_ONLY, fxMesa->ReadBuffer, \
104 LFB_MODE, GR_ORIGIN_UPPER_LEFT, FXFALSE, &info ) ) \
105 {
106
107 #define HW_READ_UNLOCK() \
108 fxMesa->Glide.grLfbUnlock( GR_LFB_READ_ONLY, fxMesa->ReadBuffer );\
109 }
110
111
112 #define HW_WRITE_CLIPLOOP() \
113 do { \
114 int _nc = fxMesa->numClipRects; \
115 while (_nc--) { \
116 int minx = fxMesa->pClipRects[_nc].x1 - fxMesa->x_offset; \
117 int miny = fxMesa->pClipRects[_nc].y1 - fxMesa->y_offset; \
118 int maxx = fxMesa->pClipRects[_nc].x2 - fxMesa->x_offset; \
119 int maxy = fxMesa->pClipRects[_nc].y2 - fxMesa->y_offset;
120
121 #define HW_READ_CLIPLOOP() \
122 do { \
123 const __DRIdrawablePrivate *dPriv = fxMesa->driDrawable; \
124 XF86DRIClipRectPtr rect = dPriv->pClipRects; \
125 int _nc = dPriv->numClipRects; \
126 while (_nc--) { \
127 const int minx = rect->x1 - fxMesa->x_offset; \
128 const int miny = rect->y1 - fxMesa->y_offset; \
129 const int maxx = rect->x2 - fxMesa->x_offset; \
130 const int maxy = rect->y2 - fxMesa->y_offset; \
131 rect++;
132
133 #define HW_ENDCLIPLOOP() \
134 } \
135 } while (0)
136
137
138
139 #define LFB_MODE GR_LFBWRITEMODE_565
140
141
142 /* 16 bit, RGB565 color spanline and pixel functions */ \
143
144 #undef INIT_MONO_PIXEL
145 #define INIT_MONO_PIXEL(p, color) \
146 p = TDFXPACKCOLOR565( color[0], color[1], color[2] )
147
148
149 #define WRITE_RGBA( _x, _y, r, g, b, a ) \
150 *(GLushort *)(buf + _x*2 + _y*pitch) = ((((int)r & 0xf8) << 8) | \
151 (((int)g & 0xfc) << 3) | \
152 (((int)b & 0xf8) >> 3))
153
154 #define WRITE_PIXEL( _x, _y, p ) \
155 *(GLushort *)(buf + _x*2 + _y*pitch) = p
156
157 #define READ_RGBA( rgba, _x, _y ) \
158 do { \
159 GLushort p = *(GLushort *)(buf + _x*2 + _y*pitch); \
160 rgba[0] = (((p >> 11) & 0x1f) * 255) / 31; \
161 rgba[1] = (((p >> 5) & 0x3f) * 255) / 63; \
162 rgba[2] = (((p >> 0) & 0x1f) * 255) / 31; \
163 rgba[3] = 0xff; \
164 } while (0)
165
166 #define TAG(x) tdfx##x##_RGB565
167 #define BYTESPERPIXEL 2
168 #include "spantmp.h"
169 #undef BYTESPERPIXEL
170
171
172 /* 16 bit, BGR565 color spanline and pixel functions */ \
173 #if 0
174
175 #define WRITE_RGBA( _x, _y, r, g, b, a ) \
176 *(GLushort *)(buf + _x*2 + _y*pitch) = ((((int)b & 0xf8) << 8) | \
177 (((int)g & 0xfc) << 3) | \
178 (((int)r & 0xf8) >> 3))
179
180 #define WRITE_PIXEL( _x, _y, p ) \
181 *(GLushort *)(buf + _x*2 + _y*pitch) = p
182
183 #define READ_RGBA( rgba, _x, _y ) \
184 do { \
185 GLushort p = *(GLushort *)(buf + _x*2 + _y*pitch); \
186 rgba[0] = (p << 3) & 0xf8; \
187 rgba[1] = (p >> 3) & 0xfc; \
188 rgba[2] = (p >> 8) & 0xf8; \
189 rgba[3] = 0xff; \
190 } while (0)
191
192 #define TAG(x) tdfx##x##_BGR565
193 #define BYTESPERPIXEL 2
194 #include "spantmp.h"
195 #undef BYTESPERPIXEL
196 #endif
197
198
199 #undef LFB_MODE
200 #define LFB_MODE GR_LFBWRITEMODE_888
201
202
203 /* 24 bit, RGB888 color spanline and pixel functions */
204 #undef INIT_MONO_PIXEL
205 #define INIT_MONO_PIXEL(p, color) \
206 p = TDFXPACKCOLOR888( color[0], color[1], color[2] )
207
208 #define WRITE_RGBA( _x, _y, r, g, b, a ) \
209 *(GLuint *)(buf + _x*3 + _y*pitch) = ((b << 0) | \
210 (g << 8) | \
211 (r << 16))
212
213 #define WRITE_PIXEL( _x, _y, p ) \
214 *(GLuint *)(buf + _x*3 + _y*pitch) = p
215
216 #define READ_RGBA( rgba, _x, _y ) \
217 do { \
218 GLuint p = *(GLuint *)(buf + _x*3 + _y*pitch); \
219 rgba[0] = (p >> 16) & 0xff; \
220 rgba[1] = (p >> 8) & 0xff; \
221 rgba[2] = (p >> 0) & 0xff; \
222 rgba[3] = 0xff; \
223 } while (0)
224
225 #define TAG(x) tdfx##x##_RGB888
226 #define BYTESPERPIXEL 4
227 #include "spantmp.h"
228 #undef BYTESPERPIXEL
229
230
231 #undef LFB_MODE
232 #define LFB_MODE GR_LFBWRITEMODE_8888
233
234
235 /* 32 bit, ARGB8888 color spanline and pixel functions */
236 #undef INIT_MONO_PIXEL
237 #define INIT_MONO_PIXEL(p, color) \
238 p = TDFXPACKCOLOR8888( color[0], color[1], color[2], color[3] )
239
240 #define WRITE_RGBA( _x, _y, r, g, b, a ) \
241 *(GLuint *)(buf + _x*4 + _y*pitch) = ((b << 0) | \
242 (g << 8) | \
243 (r << 16) | \
244 (a << 24) )
245
246 #define WRITE_PIXEL( _x, _y, p ) \
247 *(GLuint *)(buf + _x*4 + _y*pitch) = p
248
249 #define READ_RGBA( rgba, _x, _y ) \
250 do { \
251 GLuint p = *(GLuint *)(buf + _x*4 + _y*pitch); \
252 rgba[0] = (p >> 16) & 0xff; \
253 rgba[1] = (p >> 8) & 0xff; \
254 rgba[2] = (p >> 0) & 0xff; \
255 rgba[3] = (p >> 24) & 0xff; \
256 } while (0)
257
258 #define TAG(x) tdfx##x##_ARGB8888
259 #define BYTESPERPIXEL 4
260 #include "spantmp.h"
261 #undef BYTESPERPIXEL
262
263
264
265 /* ================================================================
266 * Old span functions below...
267 */
268
269
270 /*
271 * Examine the cliprects to generate an array of flags to indicate
272 * which pixels in a span are visible. Note: (x,y) is a screen
273 * coordinate.
274 */
275 static void
276 generate_vismask(const tdfxContextPtr fxMesa, GLint x, GLint y, GLint n,
277 GLubyte vismask[])
278 {
279 GLboolean initialized = GL_FALSE;
280 GLint i, j;
281
282 /* Ensure we clear the visual mask */
283 MEMSET(vismask, 0, n);
284
285 /* turn on flags for all visible pixels */
286 for (i = 0; i < fxMesa->numClipRects; i++) {
287 const XF86DRIClipRectPtr rect = &fxMesa->pClipRects[i];
288
289 if (y >= rect->y1 && y < rect->y2) {
290 if (x >= rect->x1 && x + n <= rect->x2) {
291 /* common case, whole span inside cliprect */
292 MEMSET(vismask, 1, n);
293 return;
294 }
295 if (x < rect->x2 && x + n >= rect->x1) {
296 /* some of the span is inside the rect */
297 GLint start, end;
298 if (!initialized) {
299 MEMSET(vismask, 0, n);
300 initialized = GL_TRUE;
301 }
302 if (x < rect->x1)
303 start = rect->x1 - x;
304 else
305 start = 0;
306 if (x + n > rect->x2)
307 end = rect->x2 - x;
308 else
309 end = n;
310 assert(start >= 0);
311 assert(end <= n);
312 for (j = start; j < end; j++)
313 vismask[j] = 1;
314 }
315 }
316 }
317 }
318
319 /*
320 * Examine cliprects and determine if the given screen pixel is visible.
321 */
322 static GLboolean
323 visible_pixel(const tdfxContextPtr fxMesa, int scrX, int scrY)
324 {
325 int i;
326 for (i = 0; i < fxMesa->numClipRects; i++) {
327 const XF86DRIClipRectPtr rect = &fxMesa->pClipRects[i];
328 if (scrX >= rect->x1 &&
329 scrX < rect->x2 &&
330 scrY >= rect->y1 && scrY < rect->y2) return GL_TRUE;
331 }
332 return GL_FALSE;
333 }
334
335
336
337 /*
338 * Depth buffer read/write functions.
339 */
340 /*
341 * To read the frame buffer, we need to lock and unlock it. The
342 * four macros {READ,WRITE}_FB_SPAN_{LOCK,UNLOCK}
343 * do this for us.
344 *
345 * Note that the lock must be matched with an unlock. These
346 * macros include a spare curly brace, so they must
347 * be syntactically matched.
348 *
349 * Note, also, that you can't lock a buffer twice with different
350 * modes. That is to say, you can't lock a buffer in both read
351 * and write modes. The strideInBytes and LFB pointer will be
352 * the same with read and write locks, so you can use either.
353 * o The HW has different state for reads and writes, so
354 * locking it twice may give screwy results.
355 * o The DRM won't let you lock twice. It hangs. This is probably
356 * because of the LOCK_HARDWARE IN THE *_FB_SPAN_LOCK macros,
357 * and could be eliminated with nonlocking lock routines. But
358 * what's the point after all.
359 */
360 #define READ_FB_SPAN_LOCK(fxMesa, info, target_buffer) \
361 UNLOCK_HARDWARE(fxMesa); \
362 LOCK_HARDWARE(fxMesa); \
363 (info).size=sizeof(info); \
364 if (fxMesa->Glide.grLfbLock(GR_LFB_READ_ONLY, \
365 target_buffer, \
366 GR_LFBWRITEMODE_ANY, \
367 GR_ORIGIN_UPPER_LEFT, \
368 FXFALSE, \
369 &(info))) {
370
371 #define READ_FB_SPAN_UNLOCK(fxMesa, target_buffer) \
372 fxMesa->Glide.grLfbUnlock(GR_LFB_READ_ONLY, target_buffer); \
373 } else { \
374 fprintf(stderr, "tdfxDriver: Can't get %s (%d) read lock\n", \
375 (target_buffer == GR_BUFFER_BACKBUFFER) \
376 ? "back buffer" \
377 : ((target_buffer == GR_BUFFER_AUXBUFFER) \
378 ? "depth buffer" \
379 : "unknown buffer"), \
380 target_buffer); \
381 }
382
383 #define WRITE_FB_SPAN_LOCK(fxMesa, info, target_buffer, write_mode) \
384 UNLOCK_HARDWARE(fxMesa); \
385 LOCK_HARDWARE(fxMesa); \
386 info.size=sizeof(info); \
387 if (fxMesa->Glide.grLfbLock(GR_LFB_WRITE_ONLY, \
388 target_buffer, \
389 write_mode, \
390 GR_ORIGIN_UPPER_LEFT, \
391 FXFALSE, \
392 &info)) {
393
394 #define WRITE_FB_SPAN_UNLOCK(fxMesa, target_buffer) \
395 fxMesa->Glide.grLfbUnlock(GR_LFB_WRITE_ONLY, target_buffer); \
396 } else { \
397 fprintf(stderr, "tdfxDriver: Can't get %s (%d) write lock\n", \
398 (target_buffer == GR_BUFFER_BACKBUFFER) \
399 ? "back buffer" \
400 : ((target_buffer == GR_BUFFER_AUXBUFFER) \
401 ? "depth buffer" \
402 : "unknown buffer"), \
403 target_buffer); \
404 }
405
406 /*
407 * Because the Linear Frame Buffer is not necessarily aligned
408 * with the depth buffer, we have to do some fiddling
409 * around to get the right addresses.
410 *
411 * Perhaps a picture is in order. The Linear Frame Buffer
412 * looks like this:
413 *
414 * |<----------------------info.strideInBytes------------->|
415 * |<-----physicalStrideInBytes------->|
416 * +-----------------------------------+xxxxxxxxxxxxxxxxxxx+
417 * | | |
418 * | Legal Memory | Forbidden Zone |
419 * | | |
420 * +-----------------------------------+xxxxxxxxxxxxxxxxxxx+
421 *
422 * You can only reliably read and write legal locations. Reads
423 * and writes from the Forbidden Zone will return undefined values,
424 * and may cause segmentation faults.
425 *
426 * Now, the depth buffer may not end up in a location such each
427 * scan line is an LFB line. For example, the depth buffer may
428 * look like this:
429 *
430 * wrapped ordinary.
431 * +-----------------------------------+xxxxxxxxxxxxxxxxxxx+
432 * |0000000000000000000000 | | back
433 * |1111111111111111111111 | | buffer
434 * |2222222222222222222222 | |
435 * |4096b align. padxx00000000000000000| Forbidden Zone | depth
436 * |0000 11111111111111111| | buffer
437 * |1111 22222222222222222| |
438 * |2222 | |
439 * +-----------------------------------+xxxxxxxxxxxxxxxxxxx+
440 * where each number is the scan line number. We know it will
441 * be aligned on 128 byte boundaries, at least. Aligning this
442 * on a scanline boundary causes the back and depth buffers to
443 * thrash in the SST1 cache. (Note that the back buffer is always
444 * allocated at the beginning of LFB memory, and so it is always
445 * properly aligned with the LFB stride.)
446 *
447 * We call the beginning of the line (which is the rightmost
448 * part of the depth line in the picture above) the *ordinary* part
449 * of the scanline, and the end of the line (which is the
450 * leftmost part, one line below) the *wrapped* part of the scanline.
451 * a.) We need to know what x value to subtract from the screen
452 * x coordinate to index into the wrapped part.
453 * b.) We also need to figure out if we need to read from the ordinary
454 * part scan line, or from the wrapped part of the scan line.
455 *
456 * [ad a]
457 * The first wrapped x coordinate is that coordinate such that
458 * depthBufferOffset&(info.strideInBytes) + x*elmentSize {*}
459 * > physicalStrideInBytes
460 * where depthBufferOffset is the LFB distance in bytes
461 * from the back buffer to the depth buffer. The expression
462 * depthBufferOffset&(info.strideInBytes)
463 * is then the offset (in bytes) from the beginining of (any)
464 * depth buffer line to first element in the line.
465 * Simplifying inequation {*} above we see that x is the smallest
466 * value such that
467 * x*elementSize > physicalStrideInBytes {**}
468 * - depthBufferOffset&(info.strideInBytes)
469 * Now, we know that both the summands on the right are multiples of
470 * 128, and elementSize <= 4, so if equality holds in {**}, x would
471 * be a multiple of 32. Thus we can set x to
472 * xwrapped = (physicalStrideInBytes
473 * - depthBufferOffset&(info.strideInBytes))/elementSize
474 * + 1
475 *
476 * [ad b]
477 * Question b is now simple. We read from the wrapped scan line if
478 * x is greater than xwrapped.
479 */
480 #define TILE_WIDTH_IN_BYTES 128
481 #define TILE_WIDTH_IN_ZOXELS(bpz) (TILE_WIDTH_IN_BYTES/(bpz))
482 #define TILE_HEIGHT_IN_LINES 32
483 typedef struct
484 {
485 void *lfbPtr;
486 void *lfbWrapPtr;
487 FxU32 LFBStrideInElts;
488 GLint firstWrappedX;
489 }
490 LFBParameters;
491
492 /*
493 * We need information about the back buffer. Note that
494 * this function *cannot be called* while the aux buffer
495 * is locked, or the caller will hang.
496 *
497 * Only Glide knows the LFB address of the back and depth
498 * offsets. The upper levels of Mesa know the depth offset,
499 * but that is not in LFB space, it is tiled memory space,
500 * and is not useable for us.
501 */
502 static void
503 GetBackBufferInfo(tdfxContextPtr fxMesa, GrLfbInfo_t * backBufferInfo)
504 {
505 READ_FB_SPAN_LOCK(fxMesa, *backBufferInfo, GR_BUFFER_BACKBUFFER);
506 READ_FB_SPAN_UNLOCK(fxMesa, GR_BUFFER_BACKBUFFER);
507 }
508
509 static void
510 GetFbParams(tdfxContextPtr fxMesa,
511 GrLfbInfo_t * info,
512 GrLfbInfo_t * backBufferInfo,
513 LFBParameters * ReadParamsp, FxU32 elementSize)
514 {
515 FxU32 physicalStrideInBytes, bufferOffset;
516 FxU32 strideInBytes = info->strideInBytes;
517 char *lfbPtr = (char *) (info->lfbPtr); /* For arithmetic, use char * */
518
519 /*
520 * These two come directly from the info structure.
521 */
522 ReadParamsp->lfbPtr = (void *) lfbPtr;
523 ReadParamsp->LFBStrideInElts = strideInBytes / elementSize;
524 /*
525 * Now, calculate the value of firstWrappedX.
526 *
527 * The physical stride is the screen width in bytes rounded up to
528 * the next highest multiple of 128 bytes. Note that this fails
529 * when TILE_WIDTH_IN_BYTES is not a power of two.
530 *
531 * The buffer Offset is the distance between the beginning of
532 * the LFB space, which is the beginning of the back buffer,
533 * and the buffer we are gathering information about.
534 * We want to make this routine usable for operations on the
535 * back buffer, though we don't actually use it on the back
536 * buffer. Note, then, that if bufferOffset == 0, the firstWrappedX
537 * is in the forbidden zone, and is therefore never reached.
538 *
539 * Note that if
540 * physicalStrideInBytes
541 * < bufferOffset&(info->strideInBytes-1)
542 * the buffer begins in the forbidden zone. We assert for this.
543 */
544 bufferOffset = (FxU32)(lfbPtr - (char *) backBufferInfo->lfbPtr);
545 physicalStrideInBytes
546 = (fxMesa->screen_width * elementSize + TILE_WIDTH_IN_BYTES - 1)
547 & ~(TILE_WIDTH_IN_BYTES - 1);
548 assert(physicalStrideInBytes > (bufferOffset & (strideInBytes - 1)));
549 ReadParamsp->firstWrappedX
550 = (physicalStrideInBytes
551 - (bufferOffset & (strideInBytes - 1))) / elementSize;
552 /*
553 * This is the address of the next physical line.
554 */
555 ReadParamsp->lfbWrapPtr
556 = (void *) ((char *) backBufferInfo->lfbPtr
557 + (bufferOffset & ~(strideInBytes - 1))
558 + (TILE_HEIGHT_IN_LINES) * strideInBytes);
559 }
560
561 /*
562 * These macros fetch data from the frame buffer. The type is
563 * the type of data we want to fetch. It should match the type
564 * whose size was used with GetFbParams to fill in the structure
565 * in *ReadParamsp. We have a macro to read the ordinary
566 * part, a second macro to read the wrapped part, and one which
567 * will do either. When we are reading a span, we will know
568 * when the ordinary part ends, so there's no need to test for
569 * it. However, when reading and writing pixels, we don't
570 * necessarily know. I suppose it's a matter of taste whether
571 * it's better in the macro or in the call.
572 *
573 * Recall that x and y are screen coordinates.
574 */
575 #define GET_FB_DATA(ReadParamsp, type, x, y) \
576 (((x) < (ReadParamsp)->firstWrappedX) \
577 ? (((type *)((ReadParamsp)->lfbPtr)) \
578 [(y) * ((ReadParamsp)->LFBStrideInElts) \
579 + (x)]) \
580 : (((type *)((ReadParamsp)->lfbWrapPtr)) \
581 [((y)) * ((ReadParamsp)->LFBStrideInElts) \
582 + ((x) - (ReadParamsp)->firstWrappedX)]))
583 #define GET_ORDINARY_FB_DATA(ReadParamsp, type, x, y) \
584 (((type *)((ReadParamsp)->lfbPtr)) \
585 [(y) * ((ReadParamsp)->LFBStrideInElts) \
586 + (x)])
587 #define GET_WRAPPED_FB_DATA(ReadParamsp, type, x, y) \
588 (((type *)((ReadParamsp)->lfbWrapPtr)) \
589 [((y)) * ((ReadParamsp)->LFBStrideInElts) \
590 + ((x) - (ReadParamsp)->firstWrappedX)])
591 #define PUT_FB_DATA(ReadParamsp, type, x, y, value) \
592 (GET_FB_DATA(ReadParamsp, type, x, y) = (type)(value))
593 #define PUT_ORDINARY_FB_DATA(ReadParamsp, type, x, y, value) \
594 (GET_ORDINARY_FB_DATA(ReadParamsp, type, x, y) = (type)(value))
595 #define PUT_WRAPPED_FB_DATA(ReadParamsp, type, x, y, value) \
596 (GET_WRAPPED_FB_DATA(ReadParamsp, type, x, y) = (type)(value))
597
598 static void
599 tdfxDDWriteDepthSpan(GLcontext * ctx,
600 GLuint n, GLint x, GLint y, const GLdepth depth[],
601 const GLubyte mask[])
602 {
603 tdfxContextPtr fxMesa = (tdfxContextPtr) ctx->DriverCtx;
604 GLint bottom = fxMesa->y_offset + fxMesa->height - 1;
605 GLuint depth_size = fxMesa->glCtx->Visual.depthBits;
606 GLuint stencil_size = fxMesa->glCtx->Visual.stencilBits;
607 GrLfbInfo_t info;
608 GLubyte visMask[MAX_WIDTH];
609
610 if (MESA_VERBOSE & VERBOSE_DRIVER) {
611 fprintf(stderr, "tdfxmesa: tdfxDDWriteDepthSpan(...)\n");
612 }
613
614 assert((depth_size == 16) || (depth_size == 24) || (depth_size == 32));
615 /*
616 * Convert x and y to screen coordinates.
617 */
618 x += fxMesa->x_offset;
619 y = bottom - y;
620 if (mask) {
621 GLint i;
622 GLushort d16;
623 GrLfbInfo_t backBufferInfo;
624
625 switch (depth_size) {
626 case 16:
627 GetBackBufferInfo(fxMesa, &backBufferInfo);
628 /*
629 * Note that the _LOCK macro adds a curly brace,
630 * and the UNLOCK macro removes it.
631 */
632 WRITE_FB_SPAN_LOCK(fxMesa, info, GR_BUFFER_AUXBUFFER,
633 GR_LFBWRITEMODE_ANY);
634 generate_vismask(fxMesa, x, y, n, visMask);
635 {
636 LFBParameters ReadParams;
637 int wrappedPartStart;
638 GetFbParams(fxMesa, &info, &backBufferInfo,
639 &ReadParams, sizeof(GLushort));
640 if (ReadParams.firstWrappedX <= x) {
641 wrappedPartStart = 0;
642 }
643 else if (n <= (ReadParams.firstWrappedX - x)) {
644 wrappedPartStart = n;
645 }
646 else {
647 wrappedPartStart = (ReadParams.firstWrappedX - x);
648 }
649 for (i = 0; i < wrappedPartStart; i++) {
650 if (mask[i] && visMask[i]) {
651 d16 = depth[i];
652 PUT_ORDINARY_FB_DATA(&ReadParams, GLushort, x + i, y, d16);
653 }
654 }
655 for (; i < n; i++) {
656 if (mask[i] && visMask[i]) {
657 d16 = depth[i];
658 PUT_WRAPPED_FB_DATA(&ReadParams, GLushort, x + i, y, d16);
659 }
660 }
661 }
662 WRITE_FB_SPAN_UNLOCK(fxMesa, GR_BUFFER_AUXBUFFER);
663 break;
664 case 24:
665 case 32:
666 GetBackBufferInfo(fxMesa, &backBufferInfo);
667 /*
668 * Note that the _LOCK macro adds a curly brace,
669 * and the UNLOCK macro removes it.
670 */
671 WRITE_FB_SPAN_LOCK(fxMesa, info, GR_BUFFER_AUXBUFFER,
672 GR_LFBWRITEMODE_ANY);
673 generate_vismask(fxMesa, x, y, n, visMask);
674 {
675 LFBParameters ReadParams;
676 int wrappedPartStart;
677 GetFbParams(fxMesa, &info, &backBufferInfo,
678 &ReadParams, sizeof(GLuint));
679 if (ReadParams.firstWrappedX <= x) {
680 wrappedPartStart = 0;
681 }
682 else if (n <= (ReadParams.firstWrappedX - x)) {
683 wrappedPartStart = n;
684 }
685 else {
686 wrappedPartStart = (ReadParams.firstWrappedX - x);
687 }
688 for (i = 0; i < wrappedPartStart; i++) {
689 GLuint d32;
690 if (mask[i] && visMask[i]) {
691 if (stencil_size > 0) {
692 d32 =
693 GET_ORDINARY_FB_DATA(&ReadParams, GLuint,
694 x + i, y);
695 d32 =
696 (d32 & 0xFF000000) | (depth[i] & 0x00FFFFFF);
697 }
698 else {
699 d32 = depth[i];
700 }
701 PUT_ORDINARY_FB_DATA(&ReadParams, GLuint, x + i, y, d32);
702 }
703 }
704 for (; i < n; i++) {
705 GLuint d32;
706 if (mask[i] && visMask[i]) {
707 if (stencil_size > 0) {
708 d32 =
709 GET_WRAPPED_FB_DATA(&ReadParams, GLuint,
710 x + i, y);
711 d32 =
712 (d32 & 0xFF000000) | (depth[i] & 0x00FFFFFF);
713 }
714 else {
715 d32 = depth[i];
716 }
717 PUT_WRAPPED_FB_DATA(&ReadParams, GLuint, x + i, y, d32);
718 }
719 }
720 }
721 WRITE_FB_SPAN_UNLOCK(fxMesa, GR_BUFFER_AUXBUFFER);
722 break;
723 }
724 }
725 else {
726 GLint i;
727 GLuint d32;
728 GLushort d16;
729 GrLfbInfo_t backBufferInfo;
730
731 switch (depth_size) {
732 case 16:
733 GetBackBufferInfo(fxMesa, &backBufferInfo);
734 /*
735 * Note that the _LOCK macro adds a curly brace,
736 * and the UNLOCK macro removes it.
737 */
738 WRITE_FB_SPAN_LOCK(fxMesa, info,
739 GR_BUFFER_AUXBUFFER, GR_LFBWRITEMODE_ANY);
740 generate_vismask(fxMesa, x, y, n, visMask);
741 {
742 LFBParameters ReadParams;
743 GLuint wrappedPartStart;
744 GetFbParams(fxMesa, &info, &backBufferInfo,
745 &ReadParams, sizeof(GLushort));
746 if (ReadParams.firstWrappedX <= x) {
747 wrappedPartStart = 0;
748 }
749 else if (n <= (ReadParams.firstWrappedX - x)) {
750 wrappedPartStart = n;
751 }
752 else {
753 wrappedPartStart = (ReadParams.firstWrappedX - x);
754 }
755 for (i = 0; i < wrappedPartStart; i++) {
756 if (visMask[i]) {
757 d16 = depth[i];
758 PUT_ORDINARY_FB_DATA(&ReadParams,
759 GLushort,
760 x + i, y,
761 d16);
762 }
763 }
764 for (; i < n; i++) {
765 if (visMask[i]) {
766 d16 = depth[i];
767 PUT_WRAPPED_FB_DATA(&ReadParams,
768 GLushort,
769 x + i, y,
770 d16);
771 }
772 }
773 }
774 WRITE_FB_SPAN_UNLOCK(fxMesa, GR_BUFFER_AUXBUFFER);
775 break;
776 case 24:
777 case 32:
778 GetBackBufferInfo(fxMesa, &backBufferInfo);
779 /*
780 * Note that the _LOCK macro adds a curly brace,
781 * and the UNLOCK macro removes it.
782 */
783 WRITE_FB_SPAN_LOCK(fxMesa, info,
784 GR_BUFFER_AUXBUFFER, GR_LFBWRITEMODE_ANY);
785 generate_vismask(fxMesa, x, y, n, visMask);
786 {
787 LFBParameters ReadParams;
788 GLuint wrappedPartStart;
789
790 GetFbParams(fxMesa, &info, &backBufferInfo,
791 &ReadParams, sizeof(GLuint));
792 if (ReadParams.firstWrappedX <= x) {
793 wrappedPartStart = 0;
794 }
795 else if (n <= (ReadParams.firstWrappedX - x)) {
796 wrappedPartStart = n;
797 }
798 else {
799 wrappedPartStart = (ReadParams.firstWrappedX - x);
800 }
801 for (i = 0; i < wrappedPartStart; i++) {
802 if (visMask[i]) {
803 if (stencil_size > 0) {
804 d32 = GET_ORDINARY_FB_DATA(&ReadParams, GLuint, x + i, y);
805 d32 =
806 (d32 & 0xFF000000) | (depth[i] & 0x00FFFFFF);
807 }
808 else {
809 d32 = depth[i];
810 }
811 PUT_ORDINARY_FB_DATA(&ReadParams, GLuint, x + i, y, d32);
812 }
813 }
814 for (; i < n; i++) {
815 if (visMask[i]) {
816 if (stencil_size > 0) {
817 d32 = GET_WRAPPED_FB_DATA(&ReadParams, GLuint, x + i, y);
818 d32 =
819 (d32 & 0xFF000000) | (depth[i] & 0x00FFFFFF);
820 }
821 else {
822 d32 = depth[i];
823 }
824 PUT_WRAPPED_FB_DATA(&ReadParams, GLuint, x + i, y, d32);
825 }
826 }
827 }
828 WRITE_FB_SPAN_UNLOCK(fxMesa, GR_BUFFER_AUXBUFFER);
829 break;
830 }
831 }
832 }
833
834 static void
835 tdfxDDReadDepthSpan(GLcontext * ctx,
836 GLuint n, GLint x, GLint y, GLdepth depth[])
837 {
838 tdfxContextPtr fxMesa = (tdfxContextPtr) ctx->DriverCtx;
839 GLint bottom = fxMesa->height + fxMesa->y_offset - 1;
840 GLuint i;
841 GLuint depth_size = fxMesa->glCtx->Visual.depthBits;
842 GrLfbInfo_t info;
843
844 if (MESA_VERBOSE & VERBOSE_DRIVER) {
845 fprintf(stderr, "tdfxmesa: tdfxDDReadDepthSpan(...)\n");
846 }
847
848 /*
849 * Convert to screen coordinates.
850 */
851 x += fxMesa->x_offset;
852 y = bottom - y;
853 switch (depth_size) {
854 case 16:
855 {
856 LFBParameters ReadParams;
857 GrLfbInfo_t backBufferInfo;
858 int wrappedPartStart;
859 GetBackBufferInfo(fxMesa, &backBufferInfo);
860 /*
861 * Note that the _LOCK macro adds a curly brace,
862 * and the UNLOCK macro removes it.
863 */
864 READ_FB_SPAN_LOCK(fxMesa, info, GR_BUFFER_AUXBUFFER);
865 GetFbParams(fxMesa, &info, &backBufferInfo,
866 &ReadParams, sizeof(GLushort));
867 if (ReadParams.firstWrappedX <= x) {
868 wrappedPartStart = 0;
869 }
870 else if (n <= (ReadParams.firstWrappedX - x)) {
871 wrappedPartStart = n;
872 }
873 else {
874 wrappedPartStart = (ReadParams.firstWrappedX - x);
875 }
876 /*
877 * Read the line.
878 */
879 for (i = 0; i < wrappedPartStart; i++) {
880 depth[i] =
881 GET_ORDINARY_FB_DATA(&ReadParams, GLushort, x + i, y);
882 }
883 for (; i < n; i++) {
884 depth[i] = GET_WRAPPED_FB_DATA(&ReadParams, GLushort,
885 x + i, y);
886 }
887 READ_FB_SPAN_UNLOCK(fxMesa, GR_BUFFER_AUXBUFFER);
888 break;
889 }
890 case 24:
891 case 32:
892 {
893 LFBParameters ReadParams;
894 GrLfbInfo_t backBufferInfo;
895 int wrappedPartStart;
896 GLuint stencil_size = fxMesa->glCtx->Visual.stencilBits;
897 GetBackBufferInfo(fxMesa, &backBufferInfo);
898 /*
899 * Note that the _LOCK macro adds a curly brace,
900 * and the UNLOCK macro removes it.
901 */
902 READ_FB_SPAN_LOCK(fxMesa, info, GR_BUFFER_AUXBUFFER);
903 GetFbParams(fxMesa, &info, &backBufferInfo,
904 &ReadParams, sizeof(GLuint));
905 if (ReadParams.firstWrappedX <= x) {
906 wrappedPartStart = 0;
907 }
908 else if (n <= (ReadParams.firstWrappedX - x)) {
909 wrappedPartStart = n;
910 }
911 else {
912 wrappedPartStart = (ReadParams.firstWrappedX - x);
913 }
914 /*
915 * Read the line.
916 */
917 for (i = 0; i < wrappedPartStart; i++) {
918 const GLuint mask =
919 (stencil_size > 0) ? 0x00FFFFFF : 0xFFFFFFFF;
920 depth[i] =
921 GET_ORDINARY_FB_DATA(&ReadParams, GLuint, x + i, y);
922 depth[i] &= mask;
923 }
924 for (; i < n; i++) {
925 const GLuint mask =
926 (stencil_size > 0) ? 0x00FFFFFF : 0xFFFFFFFF;
927 depth[i] = GET_WRAPPED_FB_DATA(&ReadParams, GLuint, x + i, y);
928 depth[i] &= mask;
929 }
930 READ_FB_SPAN_UNLOCK(fxMesa, GR_BUFFER_AUXBUFFER);
931 break;
932 }
933 }
934 }
935
936
937 static void
938 tdfxDDWriteDepthPixels(GLcontext * ctx,
939 GLuint n, const GLint x[], const GLint y[],
940 const GLdepth depth[], const GLubyte mask[])
941 {
942 tdfxContextPtr fxMesa = (tdfxContextPtr) ctx->DriverCtx;
943 GLint bottom = fxMesa->height + fxMesa->y_offset - 1;
944 GLuint i;
945 GLushort d16;
946 GLuint d32;
947 GLuint depth_size = fxMesa->glCtx->Visual.depthBits;
948 GLuint stencil_size = fxMesa->glCtx->Visual.stencilBits;
949 GrLfbInfo_t info;
950 int xpos;
951 int ypos;
952 GrLfbInfo_t backBufferInfo;
953
954 if (MESA_VERBOSE & VERBOSE_DRIVER) {
955 fprintf(stderr, "tdfxmesa: tdfxDDWriteDepthPixels(...)\n");
956 }
957
958 switch (depth_size) {
959 case 16:
960 GetBackBufferInfo(fxMesa, &backBufferInfo);
961 /*
962 * Note that the _LOCK macro adds a curly brace,
963 * and the UNLOCK macro removes it.
964 */
965 WRITE_FB_SPAN_LOCK(fxMesa, info,
966 GR_BUFFER_AUXBUFFER, GR_LFBWRITEMODE_ANY);
967 {
968 LFBParameters ReadParams;
969 GetFbParams(fxMesa, &info, &backBufferInfo,
970 &ReadParams, sizeof(GLushort));
971 for (i = 0; i < n; i++) {
972 if (mask[i] && visible_pixel(fxMesa, x[i], y[i])) {
973 xpos = x[i] + fxMesa->x_offset;
974 ypos = bottom - y[i];
975 d16 = depth[i];
976 PUT_FB_DATA(&ReadParams, GLushort, xpos, ypos, d16);
977 }
978 }
979 }
980 WRITE_FB_SPAN_UNLOCK(fxMesa, GR_BUFFER_AUXBUFFER);
981 break;
982 case 24:
983 case 32:
984 GetBackBufferInfo(fxMesa, &backBufferInfo);
985 /*
986 * Note that the _LOCK macro adds a curly brace,
987 * and the UNLOCK macro removes it.
988 */
989 WRITE_FB_SPAN_LOCK(fxMesa, info,
990 GR_BUFFER_AUXBUFFER, GR_LFBWRITEMODE_ANY);
991 {
992 LFBParameters ReadParams;
993 GetFbParams(fxMesa, &info, &backBufferInfo,
994 &ReadParams, sizeof(GLuint));
995 for (i = 0; i < n; i++) {
996 if (mask[i]) {
997 if (visible_pixel(fxMesa, x[i], y[i])) {
998 xpos = x[i] + fxMesa->x_offset;
999 ypos = bottom - y[i];
1000 if (stencil_size > 0) {
1001 d32 =
1002 GET_FB_DATA(&ReadParams, GLuint, xpos, ypos);
1003 d32 = (d32 & 0xFF000000) | (depth[i] & 0xFFFFFF);
1004 }
1005 else {
1006 d32 = depth[i];
1007 }
1008 PUT_FB_DATA(&ReadParams, GLuint, xpos, ypos, d32);
1009 }
1010 }
1011 }
1012 }
1013 WRITE_FB_SPAN_UNLOCK(fxMesa, GR_BUFFER_AUXBUFFER);
1014 break;
1015 }
1016 }
1017
1018
1019 static void
1020 tdfxDDReadDepthPixels(GLcontext * ctx, GLuint n,
1021 const GLint x[], const GLint y[], GLdepth depth[])
1022 {
1023 tdfxContextPtr fxMesa = (tdfxContextPtr) ctx->DriverCtx;
1024 GLint bottom = fxMesa->height + fxMesa->y_offset - 1;
1025 GLuint i;
1026 GLuint depth_size = fxMesa->glCtx->Visual.depthBits;
1027 GLushort d16;
1028 int xpos;
1029 int ypos;
1030 GrLfbInfo_t info;
1031 GLuint stencil_size;
1032 GrLfbInfo_t backBufferInfo;
1033
1034 if (MESA_VERBOSE & VERBOSE_DRIVER) {
1035 fprintf(stderr, "tdfxmesa: tdfxDDReadDepthPixels(...)\n");
1036 }
1037
1038 assert((depth_size == 16) || (depth_size == 24) || (depth_size == 32));
1039 switch (depth_size) {
1040 case 16:
1041 GetBackBufferInfo(fxMesa, &backBufferInfo);
1042 /*
1043 * Note that the _LOCK macro adds a curly brace,
1044 * and the UNLOCK macro removes it.
1045 */
1046 READ_FB_SPAN_LOCK(fxMesa, info, GR_BUFFER_AUXBUFFER);
1047 {
1048 LFBParameters ReadParams;
1049 GetFbParams(fxMesa, &info, &backBufferInfo,
1050 &ReadParams, sizeof(GLushort));
1051 for (i = 0; i < n; i++) {
1052 /*
1053 * Convert to screen coordinates.
1054 */
1055 xpos = x[i] + fxMesa->x_offset;
1056 ypos = bottom - y[i];
1057 d16 = GET_FB_DATA(&ReadParams, GLushort, xpos, ypos);
1058 depth[i] = d16;
1059 }
1060 }
1061 READ_FB_SPAN_UNLOCK(fxMesa, GR_BUFFER_AUXBUFFER);
1062 break;
1063 case 24:
1064 case 32:
1065 GetBackBufferInfo(fxMesa, &backBufferInfo);
1066 /*
1067 * Note that the _LOCK macro adds a curly brace,
1068 * and the UNLOCK macro removes it.
1069 */
1070 READ_FB_SPAN_LOCK(fxMesa, info, GR_BUFFER_AUXBUFFER);
1071 stencil_size = fxMesa->glCtx->Visual.stencilBits;
1072 {
1073 LFBParameters ReadParams;
1074 GetFbParams(fxMesa, &info, &backBufferInfo,
1075 &ReadParams, sizeof(GLuint));
1076 for (i = 0; i < n; i++) {
1077 GLuint d32;
1078
1079 /*
1080 * Convert to screen coordinates.
1081 */
1082 xpos = x[i] + fxMesa->x_offset;
1083 ypos = bottom - y[i];
1084 d32 = GET_FB_DATA(&ReadParams, GLuint, xpos, ypos);
1085 if (stencil_size > 0) {
1086 d32 &= 0x00FFFFFF;
1087 }
1088 depth[i] = d32;
1089 }
1090 }
1091 READ_FB_SPAN_UNLOCK(fxMesa, GR_BUFFER_AUXBUFFER);
1092 break;
1093 default:
1094 assert(0);
1095 }
1096 }
1097
1098 /*
1099 * Stencil buffer read/write functions.
1100 */
1101 #define EXTRACT_S_FROM_ZS(zs) (((zs) >> 24) & 0xFF)
1102 #define EXTRACT_Z_FROM_ZS(zs) ((zs) & 0xffffff)
1103 #define BUILD_ZS(z, s) (((s) << 24) | (z))
1104
1105 static void
1106 write_stencil_span(GLcontext * ctx, GLuint n, GLint x, GLint y,
1107 const GLstencil stencil[], const GLubyte mask[])
1108 {
1109 tdfxContextPtr fxMesa = TDFX_CONTEXT(ctx);
1110 GrLfbInfo_t info;
1111 GrLfbInfo_t backBufferInfo;
1112
1113 GetBackBufferInfo(fxMesa, &backBufferInfo);
1114 /*
1115 * Note that the _LOCK macro adds a curly brace,
1116 * and the UNLOCK macro removes it.
1117 */
1118 WRITE_FB_SPAN_LOCK(fxMesa, info, GR_BUFFER_AUXBUFFER, GR_LFBWRITEMODE_ANY);
1119 {
1120 const GLint winY = fxMesa->y_offset + fxMesa->height - 1;
1121 const GLint winX = fxMesa->x_offset;
1122 const GLint scrX = winX + x;
1123 const GLint scrY = winY - y;
1124 LFBParameters ReadParams;
1125 GLubyte visMask[MAX_WIDTH];
1126 GLuint i;
1127 int wrappedPartStart;
1128
1129 GetFbParams(fxMesa, &info, &backBufferInfo, &ReadParams,
1130 sizeof(GLuint));
1131 if (ReadParams.firstWrappedX <= x) {
1132 wrappedPartStart = 0;
1133 }
1134 else if (n <= (ReadParams.firstWrappedX - x)) {
1135 wrappedPartStart = n;
1136 }
1137 else {
1138 wrappedPartStart = (ReadParams.firstWrappedX - x);
1139 }
1140 generate_vismask(fxMesa, scrX, scrY, n, visMask);
1141 for (i = 0; i < wrappedPartStart; i++) {
1142 if (visMask[i] && (!mask || mask[i])) {
1143 GLuint z = GET_ORDINARY_FB_DATA(&ReadParams, GLuint,
1144 scrX + i, scrY) & 0x00FFFFFF;
1145 z |= (stencil[i] & 0xFF) << 24;
1146 PUT_ORDINARY_FB_DATA(&ReadParams, GLuint, scrX + i, scrY, z);
1147 }
1148 }
1149 for (; i < n; i++) {
1150 if (visMask[i] && (!mask || mask[i])) {
1151 GLuint z = GET_WRAPPED_FB_DATA(&ReadParams, GLuint,
1152 scrX + i, scrY) & 0x00FFFFFF;
1153 z |= (stencil[i] & 0xFF) << 24;
1154 PUT_WRAPPED_FB_DATA(&ReadParams, GLuint, scrX + i, scrY, z);
1155 }
1156 }
1157 }
1158 WRITE_FB_SPAN_UNLOCK(fxMesa, GR_BUFFER_AUXBUFFER);
1159 }
1160
1161
1162 static void
1163 read_stencil_span(GLcontext * ctx, GLuint n, GLint x, GLint y,
1164 GLstencil stencil[])
1165 {
1166 tdfxContextPtr fxMesa = TDFX_CONTEXT(ctx);
1167 GrLfbInfo_t info;
1168 GrLfbInfo_t backBufferInfo;
1169
1170 GetBackBufferInfo(fxMesa, &backBufferInfo);
1171 /*
1172 * Note that the _LOCK macro adds a curly brace,
1173 * and the UNLOCK macro removes it.
1174 */
1175 READ_FB_SPAN_LOCK(fxMesa, info, GR_BUFFER_AUXBUFFER);
1176 {
1177 const GLint winY = fxMesa->y_offset + fxMesa->height - 1;
1178 const GLint winX = fxMesa->x_offset;
1179 GLuint i;
1180 LFBParameters ReadParams;
1181 int wrappedPartStart;
1182
1183 /*
1184 * Convert to screen coordinates.
1185 */
1186 x += winX;
1187 y = winY - y;
1188 GetFbParams(fxMesa, &info, &backBufferInfo, &ReadParams,
1189 sizeof(GLuint));
1190 if (ReadParams.firstWrappedX <= x) {
1191 wrappedPartStart = 0;
1192 }
1193 else if (n <= (ReadParams.firstWrappedX - x)) {
1194 wrappedPartStart = n;
1195 }
1196 else {
1197 wrappedPartStart = (ReadParams.firstWrappedX - x);
1198 }
1199 for (i = 0; i < wrappedPartStart; i++) {
1200 stencil[i] = (GET_ORDINARY_FB_DATA(&ReadParams, GLuint,
1201 x + i, y) >> 24) & 0xFF;
1202 }
1203 for (; i < n; i++) {
1204 stencil[i] = (GET_WRAPPED_FB_DATA(&ReadParams, GLuint,
1205 x + i, y) >> 24) & 0xFF;
1206 }
1207 }
1208 READ_FB_SPAN_UNLOCK(fxMesa, GR_BUFFER_AUXBUFFER);
1209 }
1210
1211
1212 static void
1213 write_stencil_pixels(GLcontext * ctx, GLuint n,
1214 const GLint x[], const GLint y[],
1215 const GLstencil stencil[], const GLubyte mask[])
1216 {
1217 tdfxContextPtr fxMesa = TDFX_CONTEXT(ctx);
1218 GrLfbInfo_t info;
1219 GrLfbInfo_t backBufferInfo;
1220
1221 GetBackBufferInfo(fxMesa, &backBufferInfo);
1222 /*
1223 * Note that the _LOCK macro adds a curly brace,
1224 * and the UNLOCK macro removes it.
1225 */
1226 WRITE_FB_SPAN_LOCK(fxMesa, info, GR_BUFFER_AUXBUFFER, GR_LFBWRITEMODE_ANY);
1227 {
1228 const GLint winY = fxMesa->y_offset + fxMesa->height - 1;
1229 const GLint winX = fxMesa->x_offset;
1230 LFBParameters ReadParams;
1231 GLuint i;
1232
1233 GetFbParams(fxMesa, &info, &backBufferInfo, &ReadParams,
1234 sizeof(GLuint));
1235 for (i = 0; i < n; i++) {
1236 const GLint scrX = winX + x[i];
1237 const GLint scrY = winY - y[i];
1238 if ((!mask || mask[i]) && visible_pixel(fxMesa, scrX, scrY)) {
1239 GLuint z =
1240 GET_FB_DATA(&ReadParams, GLuint, scrX, scrY) & 0x00FFFFFF;
1241 z |= (stencil[i] & 0xFF) << 24;
1242 PUT_FB_DATA(&ReadParams, GLuint, scrX, scrY, z);
1243 }
1244 }
1245 }
1246 WRITE_FB_SPAN_UNLOCK(fxMesa, GR_BUFFER_AUXBUFFER);
1247 }
1248
1249
1250 static void
1251 read_stencil_pixels(GLcontext * ctx, GLuint n, const GLint x[],
1252 const GLint y[], GLstencil stencil[])
1253 {
1254 tdfxContextPtr fxMesa = TDFX_CONTEXT(ctx);
1255 GrLfbInfo_t info;
1256 GrLfbInfo_t backBufferInfo;
1257
1258 GetBackBufferInfo(fxMesa, &backBufferInfo);
1259 /*
1260 * Note that the _LOCK macro adds a curly brace,
1261 * and the UNLOCK macro removes it.
1262 */
1263 READ_FB_SPAN_LOCK(fxMesa, info, GR_BUFFER_AUXBUFFER);
1264 {
1265 const GLint winY = fxMesa->y_offset + fxMesa->height - 1;
1266 const GLint winX = fxMesa->x_offset;
1267 GLuint i;
1268 LFBParameters ReadParams;
1269
1270 GetFbParams(fxMesa, &info, &backBufferInfo, &ReadParams,
1271 sizeof(GLuint));
1272 for (i = 0; i < n; i++) {
1273 const GLint scrX = winX + x[i];
1274 const GLint scrY = winY - y[i];
1275 stencil[i] =
1276 (GET_FB_DATA(&ReadParams, GLuint, scrX, scrY) >> 24) & 0xFF;
1277 }
1278 }
1279 READ_FB_SPAN_UNLOCK(fxMesa, GR_BUFFER_AUXBUFFER);
1280 }
1281
1282 #define VISUAL_EQUALS_RGBA(vis, r, g, b, a) \
1283 ((vis.redBits == r) && \
1284 (vis.greenBits == g) && \
1285 (vis.blueBits == b) && \
1286 (vis.alphaBits == a))
1287
1288
1289
1290
1291 /**********************************************************************/
1292 /* Locking for swrast */
1293 /**********************************************************************/
1294
1295
1296 static void tdfxSpanRenderStart( GLcontext *ctx )
1297 {
1298 tdfxContextPtr fxMesa = TDFX_CONTEXT(ctx);
1299 LOCK_HARDWARE(fxMesa);
1300 }
1301
1302 static void tdfxSpanRenderFinish( GLcontext *ctx )
1303 {
1304 tdfxContextPtr fxMesa = TDFX_CONTEXT(ctx);
1305 _swrast_flush( ctx );
1306 UNLOCK_HARDWARE(fxMesa);
1307 }
1308
1309 /* Set the buffer used for reading */
1310 static void tdfxDDSetBuffer( GLcontext *ctx,
1311 GLframebuffer *buffer, GLuint bufferBit )
1312 {
1313 tdfxContextPtr fxMesa = TDFX_CONTEXT(ctx);
1314 (void) buffer;
1315
1316 switch ( bufferBit ) {
1317 case FRONT_LEFT_BIT:
1318 fxMesa->DrawBuffer = fxMesa->ReadBuffer = GR_BUFFER_FRONTBUFFER;
1319 break;
1320 case BACK_LEFT_BIT:
1321 fxMesa->DrawBuffer = fxMesa->ReadBuffer = GR_BUFFER_BACKBUFFER;
1322 break;
1323 default:
1324 break;
1325 }
1326 }
1327
1328 /**********************************************************************/
1329 /* Initialize swrast device driver */
1330 /**********************************************************************/
1331
1332 void tdfxDDInitSpanFuncs( GLcontext *ctx )
1333 {
1334 tdfxContextPtr fxMesa = TDFX_CONTEXT(ctx);
1335 struct swrast_device_driver *swdd = _swrast_GetDeviceDriverReference( ctx );
1336
1337 swdd->SetBuffer = tdfxDDSetBuffer;
1338
1339 if ( VISUAL_EQUALS_RGBA(ctx->Visual, 5, 6, 5, 0) )
1340 {
1341 /* 16bpp mode */
1342 swdd->WriteRGBASpan = tdfxWriteRGBASpan_RGB565;
1343 swdd->WriteRGBSpan = tdfxWriteRGBSpan_RGB565;
1344 swdd->WriteMonoRGBASpan = tdfxWriteMonoRGBASpan_RGB565;
1345 swdd->WriteRGBAPixels = tdfxWriteRGBAPixels_RGB565;
1346 swdd->WriteMonoRGBAPixels = tdfxWriteMonoRGBAPixels_RGB565;
1347 swdd->ReadRGBASpan = tdfxReadRGBASpan_RGB565;
1348 swdd->ReadRGBAPixels = tdfxReadRGBAPixels_RGB565;
1349 }
1350 else if ( VISUAL_EQUALS_RGBA(ctx->Visual, 8, 8, 8, 0) )
1351 {
1352 /* 24bpp mode */
1353 swdd->WriteRGBASpan = tdfxWriteRGBASpan_RGB888;
1354 swdd->WriteRGBSpan = tdfxWriteRGBSpan_RGB888;
1355 swdd->WriteMonoRGBASpan = tdfxWriteMonoRGBASpan_RGB888;
1356 swdd->WriteRGBAPixels = tdfxWriteRGBAPixels_RGB888;
1357 swdd->WriteMonoRGBAPixels = tdfxWriteMonoRGBAPixels_RGB888;
1358 swdd->ReadRGBASpan = tdfxReadRGBASpan_RGB888;
1359 swdd->ReadRGBAPixels = tdfxReadRGBAPixels_RGB888;
1360 }
1361 else if ( VISUAL_EQUALS_RGBA(ctx->Visual, 8, 8, 8, 8) )
1362 {
1363 /* 32bpp mode */
1364 swdd->WriteRGBASpan = tdfxWriteRGBASpan_ARGB8888;
1365 swdd->WriteRGBSpan = tdfxWriteRGBSpan_ARGB8888;
1366 swdd->WriteMonoRGBASpan = tdfxWriteMonoRGBASpan_ARGB8888;
1367 swdd->WriteRGBAPixels = tdfxWriteRGBAPixels_ARGB8888;
1368 swdd->WriteMonoRGBAPixels = tdfxWriteMonoRGBAPixels_ARGB8888;
1369 swdd->ReadRGBAPixels = tdfxReadRGBAPixels_ARGB8888;
1370 swdd->ReadRGBASpan = tdfxReadRGBASpan_ARGB8888;
1371 }
1372 else
1373 {
1374 abort();
1375 }
1376
1377 if ( fxMesa->haveHwStencil ) {
1378 swdd->WriteStencilSpan = write_stencil_span;
1379 swdd->ReadStencilSpan = read_stencil_span;
1380 swdd->WriteStencilPixels = write_stencil_pixels;
1381 swdd->ReadStencilPixels = read_stencil_pixels;
1382 }
1383
1384 swdd->WriteDepthSpan = tdfxDDWriteDepthSpan;
1385 swdd->WriteDepthPixels = tdfxDDWriteDepthPixels;
1386 swdd->ReadDepthSpan = tdfxDDReadDepthSpan;
1387 swdd->ReadDepthPixels = tdfxDDReadDepthPixels;
1388
1389 swdd->WriteCI8Span = NULL;
1390 swdd->WriteCI32Span = NULL;
1391 swdd->WriteMonoCISpan = NULL;
1392 swdd->WriteCI32Pixels = NULL;
1393 swdd->WriteMonoCIPixels = NULL;
1394 swdd->ReadCI32Span = NULL;
1395 swdd->ReadCI32Pixels = NULL;
1396
1397 swdd->SpanRenderStart = tdfxSpanRenderStart;
1398 swdd->SpanRenderFinish = tdfxSpanRenderFinish;
1399 }