Fixed off by one errors in clipping.
[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[0] == 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 drm_clip_rect_t *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 drm_clip_rect_t *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 drm_clip_rect_t *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_ORDINARY_FB_DATA(ReadParamsp, type, x, y) \
576 (((type *)((ReadParamsp)->lfbPtr)) \
577 [(y) * ((ReadParamsp)->LFBStrideInElts) \
578 + (x)])
579 #define GET_WRAPPED_FB_DATA(ReadParamsp, type, x, y) \
580 (((type *)((ReadParamsp)->lfbWrapPtr)) \
581 [((y)) * ((ReadParamsp)->LFBStrideInElts) \
582 + ((x) - (ReadParamsp)->firstWrappedX)])
583 #define GET_FB_DATA(ReadParamsp, type, x, y) \
584 (((x) < (ReadParamsp)->firstWrappedX) \
585 ? GET_ORDINARY_FB_DATA(ReadParamsp, type, x, y) \
586 : GET_WRAPPED_FB_DATA(ReadParamsp, type, x, y))
587 #define PUT_ORDINARY_FB_DATA(ReadParamsp, type, x, y, value) \
588 (GET_ORDINARY_FB_DATA(ReadParamsp, type, x, y) = (type)(value))
589 #define PUT_WRAPPED_FB_DATA(ReadParamsp, type, x, y, value) \
590 (GET_WRAPPED_FB_DATA(ReadParamsp, type, x, y) = (type)(value))
591 #define PUT_FB_DATA(ReadParamsp, type, x, y, value) \
592 do { \
593 if ((x) < (ReadParamsp)->firstWrappedX) \
594 PUT_ORDINARY_FB_DATA(ReadParamsp, type, x, y, value); \
595 else \
596 PUT_WRAPPED_FB_DATA(ReadParamsp, type, x, y, value); \
597 } while (0)
598
599 static void
600 tdfxDDWriteDepthSpan(GLcontext * ctx,
601 GLuint n, GLint x, GLint y, const GLdepth depth[],
602 const GLubyte mask[])
603 {
604 tdfxContextPtr fxMesa = (tdfxContextPtr) ctx->DriverCtx;
605 GLint bottom = fxMesa->y_offset + fxMesa->height - 1;
606 GLuint depth_size = fxMesa->glCtx->Visual.depthBits;
607 GLuint stencil_size = fxMesa->glCtx->Visual.stencilBits;
608 GrLfbInfo_t info;
609 GLubyte visMask[MAX_WIDTH];
610
611 if (MESA_VERBOSE & VERBOSE_DRIVER) {
612 fprintf(stderr, "tdfxmesa: tdfxDDWriteDepthSpan(...)\n");
613 }
614
615 assert((depth_size == 16) || (depth_size == 24) || (depth_size == 32));
616 /*
617 * Convert x and y to screen coordinates.
618 */
619 x += fxMesa->x_offset;
620 y = bottom - y;
621 if (mask) {
622 GLint i;
623 GLushort d16;
624 GrLfbInfo_t backBufferInfo;
625
626 switch (depth_size) {
627 case 16:
628 GetBackBufferInfo(fxMesa, &backBufferInfo);
629 /*
630 * Note that the _LOCK macro adds a curly brace,
631 * and the UNLOCK macro removes it.
632 */
633 WRITE_FB_SPAN_LOCK(fxMesa, info, GR_BUFFER_AUXBUFFER,
634 GR_LFBWRITEMODE_ANY);
635 generate_vismask(fxMesa, x, y, n, visMask);
636 {
637 LFBParameters ReadParams;
638 int wrappedPartStart;
639 GetFbParams(fxMesa, &info, &backBufferInfo,
640 &ReadParams, sizeof(GLushort));
641 if (ReadParams.firstWrappedX <= x) {
642 wrappedPartStart = 0;
643 }
644 else if (n <= (ReadParams.firstWrappedX - x)) {
645 wrappedPartStart = n;
646 }
647 else {
648 wrappedPartStart = (ReadParams.firstWrappedX - x);
649 }
650 for (i = 0; i < wrappedPartStart; i++) {
651 if (mask[i] && visMask[i]) {
652 d16 = depth[i];
653 PUT_ORDINARY_FB_DATA(&ReadParams, GLushort, x + i, y, d16);
654 }
655 }
656 for (; i < n; i++) {
657 if (mask[i] && visMask[i]) {
658 d16 = depth[i];
659 PUT_WRAPPED_FB_DATA(&ReadParams, GLushort, x + i, y, d16);
660 }
661 }
662 }
663 WRITE_FB_SPAN_UNLOCK(fxMesa, GR_BUFFER_AUXBUFFER);
664 break;
665 case 24:
666 case 32:
667 GetBackBufferInfo(fxMesa, &backBufferInfo);
668 /*
669 * Note that the _LOCK macro adds a curly brace,
670 * and the UNLOCK macro removes it.
671 */
672 WRITE_FB_SPAN_LOCK(fxMesa, info, GR_BUFFER_AUXBUFFER,
673 GR_LFBWRITEMODE_ANY);
674 generate_vismask(fxMesa, x, y, n, visMask);
675 {
676 LFBParameters ReadParams;
677 int wrappedPartStart;
678 GetFbParams(fxMesa, &info, &backBufferInfo,
679 &ReadParams, sizeof(GLuint));
680 if (ReadParams.firstWrappedX <= x) {
681 wrappedPartStart = 0;
682 }
683 else if (n <= (ReadParams.firstWrappedX - x)) {
684 wrappedPartStart = n;
685 }
686 else {
687 wrappedPartStart = (ReadParams.firstWrappedX - x);
688 }
689 for (i = 0; i < wrappedPartStart; i++) {
690 GLuint d32;
691 if (mask[i] && visMask[i]) {
692 if (stencil_size > 0) {
693 d32 =
694 GET_ORDINARY_FB_DATA(&ReadParams, GLuint,
695 x + i, y);
696 d32 =
697 (d32 & 0xFF000000) | (depth[i] & 0x00FFFFFF);
698 }
699 else {
700 d32 = depth[i];
701 }
702 PUT_ORDINARY_FB_DATA(&ReadParams, GLuint, x + i, y, d32);
703 }
704 }
705 for (; i < n; i++) {
706 GLuint d32;
707 if (mask[i] && visMask[i]) {
708 if (stencil_size > 0) {
709 d32 =
710 GET_WRAPPED_FB_DATA(&ReadParams, GLuint,
711 x + i, y);
712 d32 =
713 (d32 & 0xFF000000) | (depth[i] & 0x00FFFFFF);
714 }
715 else {
716 d32 = depth[i];
717 }
718 PUT_WRAPPED_FB_DATA(&ReadParams, GLuint, x + i, y, d32);
719 }
720 }
721 }
722 WRITE_FB_SPAN_UNLOCK(fxMesa, GR_BUFFER_AUXBUFFER);
723 break;
724 }
725 }
726 else {
727 GLint i;
728 GLuint d32;
729 GLushort d16;
730 GrLfbInfo_t backBufferInfo;
731
732 switch (depth_size) {
733 case 16:
734 GetBackBufferInfo(fxMesa, &backBufferInfo);
735 /*
736 * Note that the _LOCK macro adds a curly brace,
737 * and the UNLOCK macro removes it.
738 */
739 WRITE_FB_SPAN_LOCK(fxMesa, info,
740 GR_BUFFER_AUXBUFFER, GR_LFBWRITEMODE_ANY);
741 generate_vismask(fxMesa, x, y, n, visMask);
742 {
743 LFBParameters ReadParams;
744 GLuint wrappedPartStart;
745 GetFbParams(fxMesa, &info, &backBufferInfo,
746 &ReadParams, sizeof(GLushort));
747 if (ReadParams.firstWrappedX <= x) {
748 wrappedPartStart = 0;
749 }
750 else if (n <= (ReadParams.firstWrappedX - x)) {
751 wrappedPartStart = n;
752 }
753 else {
754 wrappedPartStart = (ReadParams.firstWrappedX - x);
755 }
756 for (i = 0; i < wrappedPartStart; i++) {
757 if (visMask[i]) {
758 d16 = depth[i];
759 PUT_ORDINARY_FB_DATA(&ReadParams,
760 GLushort,
761 x + i, y,
762 d16);
763 }
764 }
765 for (; i < n; i++) {
766 if (visMask[i]) {
767 d16 = depth[i];
768 PUT_WRAPPED_FB_DATA(&ReadParams,
769 GLushort,
770 x + i, y,
771 d16);
772 }
773 }
774 }
775 WRITE_FB_SPAN_UNLOCK(fxMesa, GR_BUFFER_AUXBUFFER);
776 break;
777 case 24:
778 case 32:
779 GetBackBufferInfo(fxMesa, &backBufferInfo);
780 /*
781 * Note that the _LOCK macro adds a curly brace,
782 * and the UNLOCK macro removes it.
783 */
784 WRITE_FB_SPAN_LOCK(fxMesa, info,
785 GR_BUFFER_AUXBUFFER, GR_LFBWRITEMODE_ANY);
786 generate_vismask(fxMesa, x, y, n, visMask);
787 {
788 LFBParameters ReadParams;
789 GLuint wrappedPartStart;
790
791 GetFbParams(fxMesa, &info, &backBufferInfo,
792 &ReadParams, sizeof(GLuint));
793 if (ReadParams.firstWrappedX <= x) {
794 wrappedPartStart = 0;
795 }
796 else if (n <= (ReadParams.firstWrappedX - x)) {
797 wrappedPartStart = n;
798 }
799 else {
800 wrappedPartStart = (ReadParams.firstWrappedX - x);
801 }
802 for (i = 0; i < wrappedPartStart; i++) {
803 if (visMask[i]) {
804 if (stencil_size > 0) {
805 d32 = GET_ORDINARY_FB_DATA(&ReadParams, GLuint, x + i, y);
806 d32 =
807 (d32 & 0xFF000000) | (depth[i] & 0x00FFFFFF);
808 }
809 else {
810 d32 = depth[i];
811 }
812 PUT_ORDINARY_FB_DATA(&ReadParams, GLuint, x + i, y, d32);
813 }
814 }
815 for (; i < n; i++) {
816 if (visMask[i]) {
817 if (stencil_size > 0) {
818 d32 = GET_WRAPPED_FB_DATA(&ReadParams, GLuint, x + i, y);
819 d32 =
820 (d32 & 0xFF000000) | (depth[i] & 0x00FFFFFF);
821 }
822 else {
823 d32 = depth[i];
824 }
825 PUT_WRAPPED_FB_DATA(&ReadParams, GLuint, x + i, y, d32);
826 }
827 }
828 }
829 WRITE_FB_SPAN_UNLOCK(fxMesa, GR_BUFFER_AUXBUFFER);
830 break;
831 }
832 }
833 }
834
835 static void
836 tdfxDDReadDepthSpan(GLcontext * ctx,
837 GLuint n, GLint x, GLint y, GLdepth depth[])
838 {
839 tdfxContextPtr fxMesa = (tdfxContextPtr) ctx->DriverCtx;
840 GLint bottom = fxMesa->height + fxMesa->y_offset - 1;
841 GLuint i;
842 GLuint depth_size = fxMesa->glCtx->Visual.depthBits;
843 GrLfbInfo_t info;
844
845 if (MESA_VERBOSE & VERBOSE_DRIVER) {
846 fprintf(stderr, "tdfxmesa: tdfxDDReadDepthSpan(...)\n");
847 }
848
849 /*
850 * Convert to screen coordinates.
851 */
852 x += fxMesa->x_offset;
853 y = bottom - y;
854 switch (depth_size) {
855 case 16:
856 {
857 LFBParameters ReadParams;
858 GrLfbInfo_t backBufferInfo;
859 int wrappedPartStart;
860 GetBackBufferInfo(fxMesa, &backBufferInfo);
861 /*
862 * Note that the _LOCK macro adds a curly brace,
863 * and the UNLOCK macro removes it.
864 */
865 READ_FB_SPAN_LOCK(fxMesa, info, GR_BUFFER_AUXBUFFER);
866 GetFbParams(fxMesa, &info, &backBufferInfo,
867 &ReadParams, sizeof(GLushort));
868 if (ReadParams.firstWrappedX <= x) {
869 wrappedPartStart = 0;
870 }
871 else if (n <= (ReadParams.firstWrappedX - x)) {
872 wrappedPartStart = n;
873 }
874 else {
875 wrappedPartStart = (ReadParams.firstWrappedX - x);
876 }
877 /*
878 * Read the line.
879 */
880 for (i = 0; i < wrappedPartStart; i++) {
881 depth[i] =
882 GET_ORDINARY_FB_DATA(&ReadParams, GLushort, x + i, y);
883 }
884 for (; i < n; i++) {
885 depth[i] = GET_WRAPPED_FB_DATA(&ReadParams, GLushort,
886 x + i, y);
887 }
888 READ_FB_SPAN_UNLOCK(fxMesa, GR_BUFFER_AUXBUFFER);
889 break;
890 }
891 case 24:
892 case 32:
893 {
894 LFBParameters ReadParams;
895 GrLfbInfo_t backBufferInfo;
896 int wrappedPartStart;
897 GLuint stencil_size = fxMesa->glCtx->Visual.stencilBits;
898 GetBackBufferInfo(fxMesa, &backBufferInfo);
899 /*
900 * Note that the _LOCK macro adds a curly brace,
901 * and the UNLOCK macro removes it.
902 */
903 READ_FB_SPAN_LOCK(fxMesa, info, GR_BUFFER_AUXBUFFER);
904 GetFbParams(fxMesa, &info, &backBufferInfo,
905 &ReadParams, sizeof(GLuint));
906 if (ReadParams.firstWrappedX <= x) {
907 wrappedPartStart = 0;
908 }
909 else if (n <= (ReadParams.firstWrappedX - x)) {
910 wrappedPartStart = n;
911 }
912 else {
913 wrappedPartStart = (ReadParams.firstWrappedX - x);
914 }
915 /*
916 * Read the line.
917 */
918 for (i = 0; i < wrappedPartStart; i++) {
919 const GLuint mask =
920 (stencil_size > 0) ? 0x00FFFFFF : 0xFFFFFFFF;
921 depth[i] =
922 GET_ORDINARY_FB_DATA(&ReadParams, GLuint, x + i, y);
923 depth[i] &= mask;
924 }
925 for (; i < n; i++) {
926 const GLuint mask =
927 (stencil_size > 0) ? 0x00FFFFFF : 0xFFFFFFFF;
928 depth[i] = GET_WRAPPED_FB_DATA(&ReadParams, GLuint, x + i, y);
929 depth[i] &= mask;
930 }
931 READ_FB_SPAN_UNLOCK(fxMesa, GR_BUFFER_AUXBUFFER);
932 break;
933 }
934 }
935 }
936
937
938 static void
939 tdfxDDWriteDepthPixels(GLcontext * ctx,
940 GLuint n, const GLint x[], const GLint y[],
941 const GLdepth depth[], const GLubyte mask[])
942 {
943 tdfxContextPtr fxMesa = (tdfxContextPtr) ctx->DriverCtx;
944 GLint bottom = fxMesa->height + fxMesa->y_offset - 1;
945 GLuint i;
946 GLushort d16;
947 GLuint d32;
948 GLuint depth_size = fxMesa->glCtx->Visual.depthBits;
949 GLuint stencil_size = fxMesa->glCtx->Visual.stencilBits;
950 GrLfbInfo_t info;
951 int xpos;
952 int ypos;
953 GrLfbInfo_t backBufferInfo;
954
955 if (MESA_VERBOSE & VERBOSE_DRIVER) {
956 fprintf(stderr, "tdfxmesa: tdfxDDWriteDepthPixels(...)\n");
957 }
958
959 switch (depth_size) {
960 case 16:
961 GetBackBufferInfo(fxMesa, &backBufferInfo);
962 /*
963 * Note that the _LOCK macro adds a curly brace,
964 * and the UNLOCK macro removes it.
965 */
966 WRITE_FB_SPAN_LOCK(fxMesa, info,
967 GR_BUFFER_AUXBUFFER, GR_LFBWRITEMODE_ANY);
968 {
969 LFBParameters ReadParams;
970 GetFbParams(fxMesa, &info, &backBufferInfo,
971 &ReadParams, sizeof(GLushort));
972 for (i = 0; i < n; i++) {
973 if (mask[i] && visible_pixel(fxMesa, x[i], y[i])) {
974 xpos = x[i] + fxMesa->x_offset;
975 ypos = bottom - y[i];
976 d16 = depth[i];
977 PUT_FB_DATA(&ReadParams, GLushort, xpos, ypos, d16);
978 }
979 }
980 }
981 WRITE_FB_SPAN_UNLOCK(fxMesa, GR_BUFFER_AUXBUFFER);
982 break;
983 case 24:
984 case 32:
985 GetBackBufferInfo(fxMesa, &backBufferInfo);
986 /*
987 * Note that the _LOCK macro adds a curly brace,
988 * and the UNLOCK macro removes it.
989 */
990 WRITE_FB_SPAN_LOCK(fxMesa, info,
991 GR_BUFFER_AUXBUFFER, GR_LFBWRITEMODE_ANY);
992 {
993 LFBParameters ReadParams;
994 GetFbParams(fxMesa, &info, &backBufferInfo,
995 &ReadParams, sizeof(GLuint));
996 for (i = 0; i < n; i++) {
997 if (mask[i]) {
998 if (visible_pixel(fxMesa, x[i], y[i])) {
999 xpos = x[i] + fxMesa->x_offset;
1000 ypos = bottom - y[i];
1001 if (stencil_size > 0) {
1002 d32 =
1003 GET_FB_DATA(&ReadParams, GLuint, xpos, ypos);
1004 d32 = (d32 & 0xFF000000) | (depth[i] & 0xFFFFFF);
1005 }
1006 else {
1007 d32 = depth[i];
1008 }
1009 PUT_FB_DATA(&ReadParams, GLuint, xpos, ypos, d32);
1010 }
1011 }
1012 }
1013 }
1014 WRITE_FB_SPAN_UNLOCK(fxMesa, GR_BUFFER_AUXBUFFER);
1015 break;
1016 }
1017 }
1018
1019
1020 static void
1021 tdfxDDReadDepthPixels(GLcontext * ctx, GLuint n,
1022 const GLint x[], const GLint y[], GLdepth depth[])
1023 {
1024 tdfxContextPtr fxMesa = (tdfxContextPtr) ctx->DriverCtx;
1025 GLint bottom = fxMesa->height + fxMesa->y_offset - 1;
1026 GLuint i;
1027 GLuint depth_size = fxMesa->glCtx->Visual.depthBits;
1028 GLushort d16;
1029 int xpos;
1030 int ypos;
1031 GrLfbInfo_t info;
1032 GLuint stencil_size;
1033 GrLfbInfo_t backBufferInfo;
1034
1035 if (MESA_VERBOSE & VERBOSE_DRIVER) {
1036 fprintf(stderr, "tdfxmesa: tdfxDDReadDepthPixels(...)\n");
1037 }
1038
1039 assert((depth_size == 16) || (depth_size == 24) || (depth_size == 32));
1040 switch (depth_size) {
1041 case 16:
1042 GetBackBufferInfo(fxMesa, &backBufferInfo);
1043 /*
1044 * Note that the _LOCK macro adds a curly brace,
1045 * and the UNLOCK macro removes it.
1046 */
1047 READ_FB_SPAN_LOCK(fxMesa, info, GR_BUFFER_AUXBUFFER);
1048 {
1049 LFBParameters ReadParams;
1050 GetFbParams(fxMesa, &info, &backBufferInfo,
1051 &ReadParams, sizeof(GLushort));
1052 for (i = 0; i < n; i++) {
1053 /*
1054 * Convert to screen coordinates.
1055 */
1056 xpos = x[i] + fxMesa->x_offset;
1057 ypos = bottom - y[i];
1058 d16 = GET_FB_DATA(&ReadParams, GLushort, xpos, ypos);
1059 depth[i] = d16;
1060 }
1061 }
1062 READ_FB_SPAN_UNLOCK(fxMesa, GR_BUFFER_AUXBUFFER);
1063 break;
1064 case 24:
1065 case 32:
1066 GetBackBufferInfo(fxMesa, &backBufferInfo);
1067 /*
1068 * Note that the _LOCK macro adds a curly brace,
1069 * and the UNLOCK macro removes it.
1070 */
1071 READ_FB_SPAN_LOCK(fxMesa, info, GR_BUFFER_AUXBUFFER);
1072 stencil_size = fxMesa->glCtx->Visual.stencilBits;
1073 {
1074 LFBParameters ReadParams;
1075 GetFbParams(fxMesa, &info, &backBufferInfo,
1076 &ReadParams, sizeof(GLuint));
1077 for (i = 0; i < n; i++) {
1078 GLuint d32;
1079
1080 /*
1081 * Convert to screen coordinates.
1082 */
1083 xpos = x[i] + fxMesa->x_offset;
1084 ypos = bottom - y[i];
1085 d32 = GET_FB_DATA(&ReadParams, GLuint, xpos, ypos);
1086 if (stencil_size > 0) {
1087 d32 &= 0x00FFFFFF;
1088 }
1089 depth[i] = d32;
1090 }
1091 }
1092 READ_FB_SPAN_UNLOCK(fxMesa, GR_BUFFER_AUXBUFFER);
1093 break;
1094 default:
1095 assert(0);
1096 }
1097 }
1098
1099 /*
1100 * Stencil buffer read/write functions.
1101 */
1102 #define EXTRACT_S_FROM_ZS(zs) (((zs) >> 24) & 0xFF)
1103 #define EXTRACT_Z_FROM_ZS(zs) ((zs) & 0xffffff)
1104 #define BUILD_ZS(z, s) (((s) << 24) | (z))
1105
1106 static void
1107 write_stencil_span(GLcontext * ctx, GLuint n, GLint x, GLint y,
1108 const GLstencil stencil[], const GLubyte mask[])
1109 {
1110 tdfxContextPtr fxMesa = TDFX_CONTEXT(ctx);
1111 GrLfbInfo_t info;
1112 GrLfbInfo_t backBufferInfo;
1113
1114 GetBackBufferInfo(fxMesa, &backBufferInfo);
1115 /*
1116 * Note that the _LOCK macro adds a curly brace,
1117 * and the UNLOCK macro removes it.
1118 */
1119 WRITE_FB_SPAN_LOCK(fxMesa, info, GR_BUFFER_AUXBUFFER, GR_LFBWRITEMODE_ANY);
1120 {
1121 const GLint winY = fxMesa->y_offset + fxMesa->height - 1;
1122 const GLint winX = fxMesa->x_offset;
1123 const GLint scrX = winX + x;
1124 const GLint scrY = winY - y;
1125 LFBParameters ReadParams;
1126 GLubyte visMask[MAX_WIDTH];
1127 GLuint i;
1128 int wrappedPartStart;
1129
1130 GetFbParams(fxMesa, &info, &backBufferInfo, &ReadParams,
1131 sizeof(GLuint));
1132 if (ReadParams.firstWrappedX <= x) {
1133 wrappedPartStart = 0;
1134 }
1135 else if (n <= (ReadParams.firstWrappedX - x)) {
1136 wrappedPartStart = n;
1137 }
1138 else {
1139 wrappedPartStart = (ReadParams.firstWrappedX - x);
1140 }
1141 generate_vismask(fxMesa, scrX, scrY, n, visMask);
1142 for (i = 0; i < wrappedPartStart; i++) {
1143 if (visMask[i] && (!mask || mask[i])) {
1144 GLuint z = GET_ORDINARY_FB_DATA(&ReadParams, GLuint,
1145 scrX + i, scrY) & 0x00FFFFFF;
1146 z |= (stencil[i] & 0xFF) << 24;
1147 PUT_ORDINARY_FB_DATA(&ReadParams, GLuint, scrX + i, scrY, z);
1148 }
1149 }
1150 for (; i < n; i++) {
1151 if (visMask[i] && (!mask || mask[i])) {
1152 GLuint z = GET_WRAPPED_FB_DATA(&ReadParams, GLuint,
1153 scrX + i, scrY) & 0x00FFFFFF;
1154 z |= (stencil[i] & 0xFF) << 24;
1155 PUT_WRAPPED_FB_DATA(&ReadParams, GLuint, scrX + i, scrY, z);
1156 }
1157 }
1158 }
1159 WRITE_FB_SPAN_UNLOCK(fxMesa, GR_BUFFER_AUXBUFFER);
1160 }
1161
1162
1163 static void
1164 read_stencil_span(GLcontext * ctx, GLuint n, GLint x, GLint y,
1165 GLstencil stencil[])
1166 {
1167 tdfxContextPtr fxMesa = TDFX_CONTEXT(ctx);
1168 GrLfbInfo_t info;
1169 GrLfbInfo_t backBufferInfo;
1170
1171 GetBackBufferInfo(fxMesa, &backBufferInfo);
1172 /*
1173 * Note that the _LOCK macro adds a curly brace,
1174 * and the UNLOCK macro removes it.
1175 */
1176 READ_FB_SPAN_LOCK(fxMesa, info, GR_BUFFER_AUXBUFFER);
1177 {
1178 const GLint winY = fxMesa->y_offset + fxMesa->height - 1;
1179 const GLint winX = fxMesa->x_offset;
1180 GLuint i;
1181 LFBParameters ReadParams;
1182 int wrappedPartStart;
1183
1184 /*
1185 * Convert to screen coordinates.
1186 */
1187 x += winX;
1188 y = winY - y;
1189 GetFbParams(fxMesa, &info, &backBufferInfo, &ReadParams,
1190 sizeof(GLuint));
1191 if (ReadParams.firstWrappedX <= x) {
1192 wrappedPartStart = 0;
1193 }
1194 else if (n <= (ReadParams.firstWrappedX - x)) {
1195 wrappedPartStart = n;
1196 }
1197 else {
1198 wrappedPartStart = (ReadParams.firstWrappedX - x);
1199 }
1200 for (i = 0; i < wrappedPartStart; i++) {
1201 stencil[i] = (GET_ORDINARY_FB_DATA(&ReadParams, GLuint,
1202 x + i, y) >> 24) & 0xFF;
1203 }
1204 for (; i < n; i++) {
1205 stencil[i] = (GET_WRAPPED_FB_DATA(&ReadParams, GLuint,
1206 x + i, y) >> 24) & 0xFF;
1207 }
1208 }
1209 READ_FB_SPAN_UNLOCK(fxMesa, GR_BUFFER_AUXBUFFER);
1210 }
1211
1212
1213 static void
1214 write_stencil_pixels(GLcontext * ctx, GLuint n,
1215 const GLint x[], const GLint y[],
1216 const GLstencil stencil[], const GLubyte mask[])
1217 {
1218 tdfxContextPtr fxMesa = TDFX_CONTEXT(ctx);
1219 GrLfbInfo_t info;
1220 GrLfbInfo_t backBufferInfo;
1221
1222 GetBackBufferInfo(fxMesa, &backBufferInfo);
1223 /*
1224 * Note that the _LOCK macro adds a curly brace,
1225 * and the UNLOCK macro removes it.
1226 */
1227 WRITE_FB_SPAN_LOCK(fxMesa, info, GR_BUFFER_AUXBUFFER, GR_LFBWRITEMODE_ANY);
1228 {
1229 const GLint winY = fxMesa->y_offset + fxMesa->height - 1;
1230 const GLint winX = fxMesa->x_offset;
1231 LFBParameters ReadParams;
1232 GLuint i;
1233
1234 GetFbParams(fxMesa, &info, &backBufferInfo, &ReadParams,
1235 sizeof(GLuint));
1236 for (i = 0; i < n; i++) {
1237 const GLint scrX = winX + x[i];
1238 const GLint scrY = winY - y[i];
1239 if ((!mask || mask[i]) && visible_pixel(fxMesa, scrX, scrY)) {
1240 GLuint z =
1241 GET_FB_DATA(&ReadParams, GLuint, scrX, scrY) & 0x00FFFFFF;
1242 z |= (stencil[i] & 0xFF) << 24;
1243 PUT_FB_DATA(&ReadParams, GLuint, scrX, scrY, z);
1244 }
1245 }
1246 }
1247 WRITE_FB_SPAN_UNLOCK(fxMesa, GR_BUFFER_AUXBUFFER);
1248 }
1249
1250
1251 static void
1252 read_stencil_pixels(GLcontext * ctx, GLuint n, const GLint x[],
1253 const GLint y[], GLstencil stencil[])
1254 {
1255 tdfxContextPtr fxMesa = TDFX_CONTEXT(ctx);
1256 GrLfbInfo_t info;
1257 GrLfbInfo_t backBufferInfo;
1258
1259 GetBackBufferInfo(fxMesa, &backBufferInfo);
1260 /*
1261 * Note that the _LOCK macro adds a curly brace,
1262 * and the UNLOCK macro removes it.
1263 */
1264 READ_FB_SPAN_LOCK(fxMesa, info, GR_BUFFER_AUXBUFFER);
1265 {
1266 const GLint winY = fxMesa->y_offset + fxMesa->height - 1;
1267 const GLint winX = fxMesa->x_offset;
1268 GLuint i;
1269 LFBParameters ReadParams;
1270
1271 GetFbParams(fxMesa, &info, &backBufferInfo, &ReadParams,
1272 sizeof(GLuint));
1273 for (i = 0; i < n; i++) {
1274 const GLint scrX = winX + x[i];
1275 const GLint scrY = winY - y[i];
1276 stencil[i] =
1277 (GET_FB_DATA(&ReadParams, GLuint, scrX, scrY) >> 24) & 0xFF;
1278 }
1279 }
1280 READ_FB_SPAN_UNLOCK(fxMesa, GR_BUFFER_AUXBUFFER);
1281 }
1282
1283 #define VISUAL_EQUALS_RGBA(vis, r, g, b, a) \
1284 ((vis.redBits == r) && \
1285 (vis.greenBits == g) && \
1286 (vis.blueBits == b) && \
1287 (vis.alphaBits == a))
1288
1289
1290
1291
1292 /**********************************************************************/
1293 /* Locking for swrast */
1294 /**********************************************************************/
1295
1296
1297 static void tdfxSpanRenderStart( GLcontext *ctx )
1298 {
1299 tdfxContextPtr fxMesa = TDFX_CONTEXT(ctx);
1300 LOCK_HARDWARE(fxMesa);
1301 }
1302
1303 static void tdfxSpanRenderFinish( GLcontext *ctx )
1304 {
1305 tdfxContextPtr fxMesa = TDFX_CONTEXT(ctx);
1306 _swrast_flush( ctx );
1307 UNLOCK_HARDWARE(fxMesa);
1308 }
1309
1310 /* Set the buffer used for reading */
1311 static void tdfxDDSetBuffer( GLcontext *ctx,
1312 GLframebuffer *buffer, GLuint bufferBit )
1313 {
1314 tdfxContextPtr fxMesa = TDFX_CONTEXT(ctx);
1315 (void) buffer;
1316
1317 switch ( bufferBit ) {
1318 case DD_FRONT_LEFT_BIT:
1319 fxMesa->DrawBuffer = fxMesa->ReadBuffer = GR_BUFFER_FRONTBUFFER;
1320 break;
1321 case DD_BACK_LEFT_BIT:
1322 fxMesa->DrawBuffer = fxMesa->ReadBuffer = GR_BUFFER_BACKBUFFER;
1323 break;
1324 default:
1325 break;
1326 }
1327 }
1328
1329 /**********************************************************************/
1330 /* Initialize swrast device driver */
1331 /**********************************************************************/
1332
1333 void tdfxDDInitSpanFuncs( GLcontext *ctx )
1334 {
1335 tdfxContextPtr fxMesa = TDFX_CONTEXT(ctx);
1336 struct swrast_device_driver *swdd = _swrast_GetDeviceDriverReference( ctx );
1337
1338 swdd->SetBuffer = tdfxDDSetBuffer;
1339
1340 if ( VISUAL_EQUALS_RGBA(ctx->Visual, 5, 6, 5, 0) )
1341 {
1342 /* 16bpp mode */
1343 swdd->WriteRGBASpan = tdfxWriteRGBASpan_RGB565;
1344 swdd->WriteRGBSpan = tdfxWriteRGBSpan_RGB565;
1345 swdd->WriteMonoRGBASpan = tdfxWriteMonoRGBASpan_RGB565;
1346 swdd->WriteRGBAPixels = tdfxWriteRGBAPixels_RGB565;
1347 swdd->WriteMonoRGBAPixels = tdfxWriteMonoRGBAPixels_RGB565;
1348 swdd->ReadRGBASpan = tdfxReadRGBASpan_RGB565;
1349 swdd->ReadRGBAPixels = tdfxReadRGBAPixels_RGB565;
1350 }
1351 else if ( VISUAL_EQUALS_RGBA(ctx->Visual, 8, 8, 8, 0) )
1352 {
1353 /* 24bpp mode */
1354 swdd->WriteRGBASpan = tdfxWriteRGBASpan_RGB888;
1355 swdd->WriteRGBSpan = tdfxWriteRGBSpan_RGB888;
1356 swdd->WriteMonoRGBASpan = tdfxWriteMonoRGBASpan_RGB888;
1357 swdd->WriteRGBAPixels = tdfxWriteRGBAPixels_RGB888;
1358 swdd->WriteMonoRGBAPixels = tdfxWriteMonoRGBAPixels_RGB888;
1359 swdd->ReadRGBASpan = tdfxReadRGBASpan_RGB888;
1360 swdd->ReadRGBAPixels = tdfxReadRGBAPixels_RGB888;
1361 }
1362 else if ( VISUAL_EQUALS_RGBA(ctx->Visual, 8, 8, 8, 8) )
1363 {
1364 /* 32bpp mode */
1365 swdd->WriteRGBASpan = tdfxWriteRGBASpan_ARGB8888;
1366 swdd->WriteRGBSpan = tdfxWriteRGBSpan_ARGB8888;
1367 swdd->WriteMonoRGBASpan = tdfxWriteMonoRGBASpan_ARGB8888;
1368 swdd->WriteRGBAPixels = tdfxWriteRGBAPixels_ARGB8888;
1369 swdd->WriteMonoRGBAPixels = tdfxWriteMonoRGBAPixels_ARGB8888;
1370 swdd->ReadRGBAPixels = tdfxReadRGBAPixels_ARGB8888;
1371 swdd->ReadRGBASpan = tdfxReadRGBASpan_ARGB8888;
1372 }
1373 else
1374 {
1375 abort();
1376 }
1377
1378 if ( fxMesa->haveHwStencil ) {
1379 swdd->WriteStencilSpan = write_stencil_span;
1380 swdd->ReadStencilSpan = read_stencil_span;
1381 swdd->WriteStencilPixels = write_stencil_pixels;
1382 swdd->ReadStencilPixels = read_stencil_pixels;
1383 }
1384
1385 swdd->WriteDepthSpan = tdfxDDWriteDepthSpan;
1386 swdd->WriteDepthPixels = tdfxDDWriteDepthPixels;
1387 swdd->ReadDepthSpan = tdfxDDReadDepthSpan;
1388 swdd->ReadDepthPixels = tdfxDDReadDepthPixels;
1389
1390 swdd->WriteCI8Span = NULL;
1391 swdd->WriteCI32Span = NULL;
1392 swdd->WriteMonoCISpan = NULL;
1393 swdd->WriteCI32Pixels = NULL;
1394 swdd->WriteMonoCIPixels = NULL;
1395 swdd->ReadCI32Span = NULL;
1396 swdd->ReadCI32Pixels = NULL;
1397
1398 swdd->SpanRenderStart = tdfxSpanRenderStart;
1399 swdd->SpanRenderFinish = tdfxSpanRenderFinish;
1400 }