Small optimization for big-endian (e.g., PowerPC) systems.
[mesa.git] / src / mesa / drivers / dri / savage / savagespan.c
1 /*
2 * Copyright 1998-2003 VIA Technologies, Inc. All Rights Reserved.
3 * Copyright 2001-2003 S3 Graphics, Inc. All Rights Reserved.
4 *
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:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
14 * of the Software.
15 *
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.
23 */
24
25 #include "mtypes.h"
26 #include "savagedd.h"
27 #include "savagespan.h"
28 #include "savageioctl.h"
29 #include "savage_bci.h"
30 #include "savage_3d_reg.h"
31 #include "swrast/swrast.h"
32
33 #define DBG 0
34
35 #define LOCAL_VARS \
36 __DRIdrawablePrivate *dPriv = imesa->mesa_drawable; \
37 savageScreenPrivate *savageScreen = imesa->savageScreen; \
38 GLuint cpp = savageScreen->cpp; \
39 GLuint pitch = imesa->aperturePitch; \
40 GLuint height = dPriv->h; \
41 char *buf = (char *)(imesa->drawMap + \
42 dPriv->x * cpp + \
43 dPriv->y * pitch); \
44 char *read_buf = (char *)(imesa->readMap + \
45 dPriv->x * cpp + \
46 dPriv->y * pitch); \
47 GLuint p = SAVAGE_CONTEXT( ctx )->MonoColor; \
48 (void) read_buf; (void) buf; (void) p
49
50 #define LOCAL_DEPTH_VARS \
51 __DRIdrawablePrivate *dPriv = imesa->mesa_drawable; \
52 savageScreenPrivate *savageScreen = imesa->savageScreen; \
53 GLuint zpp = savageScreen->zpp; \
54 GLuint pitch = imesa->aperturePitch; \
55 GLuint height = dPriv->h; \
56 char *buf = (char *)(imesa->apertureBase[TARGET_DEPTH] + \
57 dPriv->x * zpp + \
58 dPriv->y * pitch)
59
60 #define LOCAL_STENCIL_VARS LOCAL_DEPTH_VARS
61
62 #define INIT_MONO_PIXEL(p)
63
64 #define CLIPPIXEL(_x,_y) (_x >= minx && _x < maxx && \
65 _y >= miny && _y < maxy)
66
67
68 #define CLIPSPAN( _x, _y, _n, _x1, _n1, _i ) \
69 if ( _y < miny || _y >= maxy ) { \
70 _n1 = 0, _x1 = x; \
71 } else { \
72 _n1 = _n; \
73 _x1 = _x; \
74 if ( _x1 < minx ) _i += (minx-_x1), n1 -= (minx-_x1), _x1 = minx; \
75 if ( _x1 + _n1 >= maxx ) n1 -= (_x1 + n1 - maxx); \
76 }
77
78 #define Y_FLIP(_y) (height - _y - 1)
79
80 #define HW_LOCK() savageContextPtr imesa = SAVAGE_CONTEXT(ctx); \
81 WAIT_IDLE_EMPTY;\
82
83 #define HW_CLIPLOOP() \
84 do { \
85 __DRIdrawablePrivate *dPriv = imesa->driDrawable; \
86 int _nc = dPriv->numClipRects; \
87 while (_nc--) { \
88 int minx = dPriv->pClipRects[_nc].x1 - dPriv->x; \
89 int miny = dPriv->pClipRects[_nc].y1 - dPriv->y; \
90 int maxx = dPriv->pClipRects[_nc].x2 - dPriv->x; \
91 int maxy = dPriv->pClipRects[_nc].y2 - dPriv->y;
92
93
94 #define HW_ENDCLIPLOOP() \
95 } \
96 } while (0)
97
98 #if 0
99 #define HW_UNLOCK() \
100 UNLOCK_HARDWARE(imesa);
101 #endif
102 #define HW_UNLOCK() { }
103
104
105 /* 16 bit, 565 rgb color spanline and pixel functions
106 */
107 #undef INIT_MONO_PIXEL
108 #define INIT_MONO_PIXEL(p, color) \
109 p = SAVAGEPACKCOLOR565( color[0], color[1], color[2] )
110
111 #define WRITE_RGBA( _x, _y, r, g, b, a ) \
112 do{ \
113 *(GLushort *)(buf + (_x<<1) + _y*pitch) = ( (((int)r & 0xf8) << 8) |\
114 (((int)g & 0xfc) << 3) | \
115 (((int)b & 0xf8) >> 3)); \
116 }while(0)
117 #define WRITE_PIXEL( _x, _y, p ) \
118 do{ \
119 *(GLushort *)(buf + (_x<<1) + _y*pitch) = p; \
120 }while(0)
121
122 #define READ_RGBA( rgba, _x, _y ) \
123 do { \
124 GLushort p = *(GLushort *)(read_buf + (_x<<1) + _y*pitch); \
125 rgba[0] = (((p >> 11) & 0x1f) * 255) >>5; \
126 rgba[1] = (((p >> 5) & 0x3f) * 255) >>6; \
127 rgba[2] = (((p >> 0) & 0x1f) * 255) >>5; \
128 rgba[3] = 255; \
129 } while(0)
130
131 #define TAG(x) savage##x##_565
132 #include "spantmp.h"
133
134
135 /* 32 bit, 8888 ARGB color spanline and pixel functions
136 */
137 #undef INIT_MONO_PIXEL
138 #define INIT_MONO_PIXEL(p, color) \
139 p = SAVAGEPACKCOLOR8888( color[0], color[1], color[2], color[3] )
140
141 #define WRITE_RGBA( _x, _y, r, g, b, a ) \
142 *(GLuint *)(buf + (_x<<2) + _y*pitch) = ( ((GLuint)a << 24) | \
143 ((GLuint)r << 16) | \
144 ((GLuint)g << 8) | \
145 ((GLuint)b ))
146 #define WRITE_PIXEL( _x, _y, p ) \
147 *(GLuint *)(buf + (_x<<2) + _y*pitch) = p
148
149 #define READ_RGBA( rgba, _x, _y ) \
150 do { \
151 GLuint p = *(GLuint *)(read_buf + (_x<<2) + _y*pitch); \
152 rgba[0] = (p >> 16) & 0xFF; \
153 rgba[1] = (p >> 8) & 0xFF; \
154 rgba[2] = (p >> 0) & 0xFF; \
155 rgba[3] = 0xFF; \
156 } while(0)
157
158 #define TAG(x) savage##x##_8888
159 #include "spantmp.h"
160
161
162
163
164 /* 16 bit depthbuffer functions.
165 */
166 #define WRITE_DEPTH( _x, _y, d ) \
167 do{ \
168 *(GLushort *)(buf + (_x<<1) + _y*pitch) = d; \
169 }while(0)
170
171 #define READ_DEPTH( d, _x, _y ) \
172 do{ \
173 d = *(GLushort *)(buf + (_x<<1) + _y*pitch); \
174 }while(0)
175
176 /* d = 0xffff; */
177
178 #define TAG(x) savage##x##_16
179 #include "depthtmp.h"
180
181
182
183
184
185 /* 8-bit stencil /24-bit depth depthbuffer functions.
186 */
187 #define WRITE_DEPTH( _x, _y, d ) { \
188 GLuint tmp = *(GLuint *)(buf + (_x<<2) + _y*pitch); \
189 tmp &= 0xFF000000; \
190 tmp |= d; \
191 *(GLuint *)(buf + (_x<<2) + _y*pitch) = tmp; \
192 }
193
194 #define READ_DEPTH( d, _x, _y ) \
195 d = *(GLuint *)(buf + (_x<<2) + _y*pitch) & 0x00FFFFFF;
196
197 /* d = 0x00ffffff; */
198
199 #define TAG(x) savage##x##_8_24
200 #include "depthtmp.h"
201
202
203 #define WRITE_STENCIL( _x, _y, d ) { \
204 GLuint tmp = *(GLuint *)(buf + (_x<<2) + _y*pitch); \
205 tmp &= 0x00FFFFFF; \
206 tmp |= (((GLuint)d)<<24) & 0xFF000000; \
207 *(GLuint *)(buf + (_x<<2) + _y*pitch) = tmp; \
208 }
209
210 #define READ_STENCIL( d, _x, _y ) \
211 d = (GLstencil)((*(GLuint *)(buf + (_x<<2) + _y*pitch) & 0xFF000000) >> 24);
212
213 #define TAG(x) savage##x##_8_24
214 #include "stenciltmp.h"
215
216
217 /*
218 * This function is called to specify which buffer to read and write
219 * for software rasterization (swrast) fallbacks. This doesn't necessarily
220 * correspond to glDrawBuffer() or glReadBuffer() calls.
221 */
222 static void savageDDSetBuffer(GLcontext *ctx, GLframebuffer *buffer,
223 GLuint bufferBit)
224 {
225 savageContextPtr imesa = SAVAGE_CONTEXT(ctx);
226 char *map;
227
228 assert((bufferBit == DD_FRONT_LEFT_BIT) || (bufferBit == DD_BACK_LEFT_BIT));
229
230 map = (bufferBit == DD_FRONT_LEFT_BIT)
231 ? (char*)imesa->apertureBase[TARGET_FRONT]
232 : (char*)imesa->apertureBase[TARGET_BACK];
233
234 imesa->drawMap = map;
235 imesa->readMap = map;
236
237 assert( (buffer == imesa->driDrawable->driverPrivate)
238 || (buffer == imesa->driReadable->driverPrivate) );
239
240 imesa->mesa_drawable = (buffer == imesa->driDrawable->driverPrivate)
241 ? imesa->driDrawable : imesa->driReadable;
242 }
243
244
245 void savageDDInitSpanFuncs( GLcontext *ctx )
246 {
247 savageContextPtr imesa = SAVAGE_CONTEXT(ctx);
248 struct swrast_device_driver *swdd = _swrast_GetDeviceDriverReference(ctx);
249
250 swdd->SetBuffer = savageDDSetBuffer;
251
252 switch (imesa->savageScreen->cpp)
253 {
254 case 2:
255 swdd->WriteRGBASpan = savageWriteRGBASpan_565;
256 swdd->WriteRGBSpan = savageWriteRGBSpan_565;
257 swdd->WriteMonoRGBASpan = savageWriteMonoRGBASpan_565;
258 swdd->WriteRGBAPixels = savageWriteRGBAPixels_565;
259 swdd->WriteMonoRGBAPixels = savageWriteMonoRGBAPixels_565;
260 swdd->ReadRGBASpan = savageReadRGBASpan_565;
261 swdd->ReadRGBAPixels = savageReadRGBAPixels_565;
262
263 break;
264
265 case 4:
266 swdd->WriteRGBASpan = savageWriteRGBASpan_8888;
267 swdd->WriteRGBSpan = savageWriteRGBSpan_8888;
268 swdd->WriteMonoRGBASpan = savageWriteMonoRGBASpan_8888;
269 swdd->WriteRGBAPixels = savageWriteRGBAPixels_8888;
270 swdd->WriteMonoRGBAPixels = savageWriteMonoRGBAPixels_8888;
271 swdd->ReadRGBASpan = savageReadRGBASpan_8888;
272 swdd->ReadRGBAPixels = savageReadRGBAPixels_8888;
273 }
274
275 switch (imesa->savageScreen->zpp)
276 {
277 case 2:
278 swdd->ReadDepthSpan = savageReadDepthSpan_16;
279 swdd->WriteDepthSpan = savageWriteDepthSpan_16;
280 swdd->ReadDepthPixels = savageReadDepthPixels_16;
281 swdd->WriteDepthPixels = savageWriteDepthPixels_16;
282
283 break;
284 case 4:
285 swdd->ReadDepthSpan = savageReadDepthSpan_8_24;
286 swdd->WriteDepthSpan = savageWriteDepthSpan_8_24;
287 swdd->ReadDepthPixels = savageReadDepthPixels_8_24;
288 swdd->WriteDepthPixels = savageWriteDepthPixels_8_24;
289 swdd->ReadStencilSpan = savageReadStencilSpan_8_24;
290 swdd->WriteStencilSpan = savageWriteStencilSpan_8_24;
291 swdd->ReadStencilPixels = savageReadStencilPixels_8_24;
292 swdd->WriteStencilPixels = savageWriteStencilPixels_8_24;
293 break;
294
295 }
296 swdd->WriteCI8Span =NULL;
297 swdd->WriteCI32Span =NULL;
298 swdd->WriteMonoCISpan =NULL;
299 swdd->WriteCI32Pixels =NULL;
300 swdd->WriteMonoCIPixels =NULL;
301 swdd->ReadCI32Span =NULL;
302 swdd->ReadCI32Pixels =NULL;
303
304 /* Pixel path fallbacks.
305 */
306 ctx->Driver.Accum = _swrast_Accum;
307 ctx->Driver.Bitmap = _swrast_Bitmap;
308 ctx->Driver.CopyPixels = _swrast_CopyPixels;
309 ctx->Driver.DrawPixels = _swrast_DrawPixels;
310 ctx->Driver.ReadPixels = _swrast_ReadPixels;
311 }