2 * Copyright 1998-2003 VIA Technologies, Inc. All Rights Reserved.
3 * Copyright 2001-2003 S3 Graphics, Inc. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sub license,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * VIA, S3 GRAPHICS, AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
27 #include "savagespan.h"
28 #include "savageioctl.h"
29 #include "savage_bci.h"
30 #include "savage_3d_reg.h"
31 #include "swrast/swrast.h"
36 savageContextPtr imesa = SAVAGE_CONTEXT(ctx); \
37 __DRIdrawablePrivate *dPriv = imesa->mesa_drawable; \
38 savageScreenPrivate *savageScreen = imesa->savageScreen; \
39 GLuint cpp = savageScreen->cpp; \
40 GLuint pitch = imesa->aperturePitch; \
41 GLuint height = dPriv->h; \
42 char *buf = (char *)(imesa->drawMap + \
45 char *read_buf = (char *)(imesa->readMap + \
48 GLuint p = SAVAGE_CONTEXT( ctx )->MonoColor; \
49 (void) read_buf; (void) buf; (void) p
51 #define LOCAL_DEPTH_VARS \
52 savageContextPtr imesa = SAVAGE_CONTEXT(ctx); \
53 __DRIdrawablePrivate *dPriv = imesa->mesa_drawable; \
54 savageScreenPrivate *savageScreen = imesa->savageScreen; \
55 GLuint zpp = savageScreen->zpp; \
56 GLuint pitch = imesa->aperturePitch; \
57 GLuint height = dPriv->h; \
58 char *buf = (char *)(imesa->apertureBase[TARGET_DEPTH] + \
62 #define LOCAL_STENCIL_VARS LOCAL_DEPTH_VARS
64 #define INIT_MONO_PIXEL(p)
66 #define CLIPPIXEL(_x,_y) (_x >= minx && _x < maxx && \
67 _y >= miny && _y < maxy)
70 #define CLIPSPAN( _x, _y, _n, _x1, _n1, _i ) \
71 if ( _y < miny || _y >= maxy ) { \
76 if ( _x1 < minx ) _i += (minx-_x1), n1 -= (minx-_x1), _x1 = minx; \
77 if ( _x1 + _n1 >= maxx ) n1 -= (_x1 + n1 - maxx); \
80 #define Y_FLIP(_y) (height - _y - 1)
84 #define HW_CLIPLOOP() \
86 __DRIdrawablePrivate *dPriv = imesa->driDrawable; \
87 int _nc = dPriv->numClipRects; \
89 int minx = dPriv->pClipRects[_nc].x1 - dPriv->x; \
90 int miny = dPriv->pClipRects[_nc].y1 - dPriv->y; \
91 int maxx = dPriv->pClipRects[_nc].x2 - dPriv->x; \
92 int maxy = dPriv->pClipRects[_nc].y2 - dPriv->y;
95 #define HW_ENDCLIPLOOP() \
102 /* 16 bit, 565 rgb color spanline and pixel functions
104 #undef INIT_MONO_PIXEL
105 #define INIT_MONO_PIXEL(p, color) \
106 p = SAVAGEPACKCOLOR565( color[0], color[1], color[2] )
108 #define WRITE_RGBA( _x, _y, r, g, b, a ) \
110 *(GLushort *)(buf + (_x<<1) + _y*pitch) = ( (((int)r & 0xf8) << 8) |\
111 (((int)g & 0xfc) << 3) | \
112 (((int)b & 0xf8) >> 3)); \
114 #define WRITE_PIXEL( _x, _y, p ) \
116 *(GLushort *)(buf + (_x<<1) + _y*pitch) = p; \
119 #define READ_RGBA( rgba, _x, _y ) \
121 GLushort p = *(GLushort *)(read_buf + (_x<<1) + _y*pitch); \
122 rgba[0] = (((p >> 11) & 0x1f) * 255) >>5; \
123 rgba[1] = (((p >> 5) & 0x3f) * 255) >>6; \
124 rgba[2] = (((p >> 0) & 0x1f) * 255) >>5; \
128 #define TAG(x) savage##x##_565
132 /* 32 bit, 8888 ARGB color spanline and pixel functions
134 #undef INIT_MONO_PIXEL
135 #define INIT_MONO_PIXEL(p, color) \
136 p = SAVAGEPACKCOLOR8888( color[0], color[1], color[2], color[3] )
138 #define WRITE_RGBA( _x, _y, r, g, b, a ) \
139 *(GLuint *)(buf + (_x<<2) + _y*pitch) = ( ((GLuint)a << 24) | \
140 ((GLuint)r << 16) | \
143 #define WRITE_PIXEL( _x, _y, p ) \
144 *(GLuint *)(buf + (_x<<2) + _y*pitch) = p
146 #define READ_RGBA( rgba, _x, _y ) \
148 GLuint p = *(GLuint *)(read_buf + (_x<<2) + _y*pitch); \
149 rgba[0] = (p >> 16) & 0xFF; \
150 rgba[1] = (p >> 8) & 0xFF; \
151 rgba[2] = (p >> 0) & 0xFF; \
155 #define TAG(x) savage##x##_8888
161 /* 16 bit integer depthbuffer functions
162 * Depth range is reversed. See also savageCalcViewport.
164 #define WRITE_DEPTH( _x, _y, d ) \
165 *(GLushort *)(buf + ((_x)<<1) + (_y)*pitch) = 0xFFFF - d
167 #define READ_DEPTH( d, _x, _y ) \
168 d = 0xFFFF - *(GLushort *)(buf + ((_x)<<1) + (_y)*pitch)
170 #define TAG(x) savage##x##_16
171 #include "depthtmp.h"
177 /* 16 bit float depthbuffer functions
179 #define WRITE_DEPTH( _x, _y, d ) \
180 *(GLushort *)(buf + ((_x)<<1) + (_y)*pitch) = \
181 savageEncodeFloat16( 1.0 - (GLfloat)d/65535.0 )
183 #define READ_DEPTH( d, _x, _y ) \
185 savageDecodeFloat16( *(GLushort *)(buf + ((_x)<<1) + (_y)*pitch) ) * \
188 #define TAG(x) savage##x##_16f
189 #include "depthtmp.h"
195 /* 8-bit stencil /24-bit integer depth depthbuffer functions.
196 * Depth range is reversed. See also savageCalcViewport.
198 #define WRITE_DEPTH( _x, _y, d ) do { \
199 GLuint tmp = *(GLuint *)(buf + ((_x)<<2) + (_y)*pitch); \
201 tmp |= 0x00FFFFFF - d; \
202 *(GLuint *)(buf + (_x<<2) + _y*pitch) = tmp; \
205 #define READ_DEPTH( d, _x, _y ) \
206 d = 0x00FFFFFF - (*(GLuint *)(buf + ((_x)<<2) + (_y)*pitch) & 0x00FFFFFF)
208 #define TAG(x) savage##x##_8_24
209 #include "depthtmp.h"
215 /* 24 bit float depthbuffer functions
217 #define WRITE_DEPTH( _x, _y, d ) do { \
218 GLuint tmp = *(GLuint *)(buf + ((_x)<<2) + (_y)*pitch); \
220 tmp |= savageEncodeFloat24( 1.0 - (GLfloat)d/16777215.0 ); \
221 *(GLuint *)(buf + (_x<<2) + _y*pitch) = tmp; \
224 #define READ_DEPTH( d, _x, _y ) \
225 d = 16777215 - savageDecodeFloat24( \
226 *(GLuint *)(buf + ((_x)<<2) + (_y)*pitch) & 0x00FFFFFF) \
229 #define TAG(x) savage##x##_8_24f
230 #include "depthtmp.h"
233 #define WRITE_STENCIL( _x, _y, d ) do { \
234 GLuint tmp = *(GLuint *)(buf + ((_x)<<2) + (_y)*pitch); \
236 tmp |= (((GLuint)d)<<24) & 0xFF000000; \
237 *(GLuint *)(buf + ((_x)<<2) + (_y)*pitch) = tmp; \
240 #define READ_STENCIL( d, _x, _y ) \
241 d = (GLstencil)((*(GLuint *)(buf + ((_x)<<2) + (_y)*pitch) & 0xFF000000) >> 24)
243 #define TAG(x) savage##x##_8_24
244 #include "stenciltmp.h"
248 * This function is called to specify which buffer to read and write
249 * for software rasterization (swrast) fallbacks. This doesn't necessarily
250 * correspond to glDrawBuffer() or glReadBuffer() calls.
252 static void savageDDSetBuffer(GLcontext
*ctx
, GLframebuffer
*buffer
,
255 savageContextPtr imesa
= SAVAGE_CONTEXT(ctx
);
258 assert((bufferBit
== DD_FRONT_LEFT_BIT
) || (bufferBit
== DD_BACK_LEFT_BIT
));
260 map
= (bufferBit
== DD_FRONT_LEFT_BIT
)
261 ? (char*)imesa
->apertureBase
[TARGET_FRONT
]
262 : (char*)imesa
->apertureBase
[TARGET_BACK
];
264 imesa
->drawMap
= map
;
265 imesa
->readMap
= map
;
267 assert( (buffer
== imesa
->driDrawable
->driverPrivate
)
268 || (buffer
== imesa
->driReadable
->driverPrivate
) );
270 imesa
->mesa_drawable
= (buffer
== imesa
->driDrawable
->driverPrivate
)
271 ? imesa
->driDrawable
: imesa
->driReadable
;
275 * Wrappers around _swrast_Copy/Draw/ReadPixels that make sure all
276 * primitives are flushed and the hardware is idle before accessing
280 savageCopyPixels( GLcontext
*ctx
,
281 GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
282 GLint destx
, GLint desty
,
285 savageContextPtr imesa
= SAVAGE_CONTEXT(ctx
);
288 _swrast_CopyPixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
, type
);
291 savageDrawPixels( GLcontext
*ctx
,
293 GLsizei width
, GLsizei height
,
294 GLenum format
, GLenum type
,
295 const struct gl_pixelstore_attrib
*packing
,
296 const GLvoid
*pixels
)
298 savageContextPtr imesa
= SAVAGE_CONTEXT(ctx
);
301 _swrast_DrawPixels(ctx
, x
, y
, width
, height
, format
, type
, packing
, pixels
);
304 savageReadPixels( GLcontext
*ctx
,
305 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
306 GLenum format
, GLenum type
,
307 const struct gl_pixelstore_attrib
*packing
,
310 savageContextPtr imesa
= SAVAGE_CONTEXT(ctx
);
313 _swrast_ReadPixels(ctx
, x
, y
, width
, height
, format
, type
, packing
, pixels
);
317 * Make sure the hardware is idle when span-rendering.
319 static void savageSpanRenderStart( GLcontext
*ctx
)
321 savageContextPtr imesa
= SAVAGE_CONTEXT(ctx
);
327 void savageDDInitSpanFuncs( GLcontext
*ctx
)
329 savageContextPtr imesa
= SAVAGE_CONTEXT(ctx
);
330 struct swrast_device_driver
*swdd
= _swrast_GetDeviceDriverReference(ctx
);
332 swdd
->SetBuffer
= savageDDSetBuffer
;
334 switch (imesa
->savageScreen
->cpp
)
337 swdd
->WriteRGBASpan
= savageWriteRGBASpan_565
;
338 swdd
->WriteRGBSpan
= savageWriteRGBSpan_565
;
339 swdd
->WriteMonoRGBASpan
= savageWriteMonoRGBASpan_565
;
340 swdd
->WriteRGBAPixels
= savageWriteRGBAPixels_565
;
341 swdd
->WriteMonoRGBAPixels
= savageWriteMonoRGBAPixels_565
;
342 swdd
->ReadRGBASpan
= savageReadRGBASpan_565
;
343 swdd
->ReadRGBAPixels
= savageReadRGBAPixels_565
;
348 swdd
->WriteRGBASpan
= savageWriteRGBASpan_8888
;
349 swdd
->WriteRGBSpan
= savageWriteRGBSpan_8888
;
350 swdd
->WriteMonoRGBASpan
= savageWriteMonoRGBASpan_8888
;
351 swdd
->WriteRGBAPixels
= savageWriteRGBAPixels_8888
;
352 swdd
->WriteMonoRGBAPixels
= savageWriteMonoRGBAPixels_8888
;
353 swdd
->ReadRGBASpan
= savageReadRGBASpan_8888
;
354 swdd
->ReadRGBAPixels
= savageReadRGBAPixels_8888
;
357 switch (imesa
->savageScreen
->zpp
)
360 if (imesa
->float_depth
) {
361 swdd
->ReadDepthSpan
= savageReadDepthSpan_16f
;
362 swdd
->WriteDepthSpan
= savageWriteDepthSpan_16f
;
363 swdd
->WriteMonoDepthSpan
= savageWriteMonoDepthSpan_16f
;
364 swdd
->ReadDepthPixels
= savageReadDepthPixels_16f
;
365 swdd
->WriteDepthPixels
= savageWriteDepthPixels_16f
;
367 swdd
->ReadDepthSpan
= savageReadDepthSpan_16
;
368 swdd
->WriteDepthSpan
= savageWriteDepthSpan_16
;
369 swdd
->WriteMonoDepthSpan
= savageWriteMonoDepthSpan_16
;
370 swdd
->ReadDepthPixels
= savageReadDepthPixels_16
;
371 swdd
->WriteDepthPixels
= savageWriteDepthPixels_16
;
376 if (imesa
->float_depth
) {
377 swdd
->ReadDepthSpan
= savageReadDepthSpan_8_24f
;
378 swdd
->WriteDepthSpan
= savageWriteDepthSpan_8_24f
;
379 swdd
->WriteMonoDepthSpan
= savageWriteMonoDepthSpan_8_24f
;
380 swdd
->ReadDepthPixels
= savageReadDepthPixels_8_24f
;
381 swdd
->WriteDepthPixels
= savageWriteDepthPixels_8_24f
;
383 swdd
->ReadDepthSpan
= savageReadDepthSpan_8_24
;
384 swdd
->WriteDepthSpan
= savageWriteDepthSpan_8_24
;
385 swdd
->WriteMonoDepthSpan
= savageWriteMonoDepthSpan_8_24
;
386 swdd
->ReadDepthPixels
= savageReadDepthPixels_8_24
;
387 swdd
->WriteDepthPixels
= savageWriteDepthPixels_8_24
;
389 swdd
->ReadStencilSpan
= savageReadStencilSpan_8_24
;
390 swdd
->WriteStencilSpan
= savageWriteStencilSpan_8_24
;
391 swdd
->ReadStencilPixels
= savageReadStencilPixels_8_24
;
392 swdd
->WriteStencilPixels
= savageWriteStencilPixels_8_24
;
396 swdd
->WriteCI8Span
=NULL
;
397 swdd
->WriteCI32Span
=NULL
;
398 swdd
->WriteMonoCISpan
=NULL
;
399 swdd
->WriteCI32Pixels
=NULL
;
400 swdd
->WriteMonoCIPixels
=NULL
;
401 swdd
->ReadCI32Span
=NULL
;
402 swdd
->ReadCI32Pixels
=NULL
;
404 swdd
->SpanRenderStart
= savageSpanRenderStart
;
406 /* Pixel path fallbacks.
408 ctx
->Driver
.Accum
= _swrast_Accum
;
409 ctx
->Driver
.Bitmap
= _swrast_Bitmap
;
410 ctx
->Driver
.CopyPixels
= savageCopyPixels
;
411 ctx
->Driver
.DrawPixels
= savageDrawPixels
;
412 ctx
->Driver
.ReadPixels
= savageReadPixels
;