patch to import Jon Smirl's work from Bitkeeper
[mesa.git] / src / mesa / drivers / dri / mga / mgaspan.c
1 /*
2 * Copyright 2000-2001 VA Linux Systems, Inc.
3 * 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 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * 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 * VA LINUX SYSTEMS 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
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Keith Whitwell <keith@tungstengraphics.com>
26 */
27 /* $XFree86: xc/lib/GL/mesa/src/drv/mga/mgaspan.c,v 1.11 2002/10/30 12:51:36 alanh Exp $ */
28
29 #include "mtypes.h"
30 #include "mgadd.h"
31 #include "mgacontext.h"
32 #include "mgaspan.h"
33 #include "mgaioctl.h"
34 #include "swrast/swrast.h"
35
36 #define DBG 0
37
38
39 #define LOCAL_VARS \
40 __DRIdrawablePrivate *dPriv = mmesa->driDrawable; \
41 mgaScreenPrivate *mgaScreen = mmesa->mgaScreen; \
42 __DRIscreenPrivate *sPriv = mmesa->driScreen; \
43 GLuint pitch = mgaScreen->frontPitch; \
44 GLuint height = dPriv->h; \
45 char *read_buf = (char *)(sPriv->pFB + \
46 mmesa->readOffset + \
47 dPriv->x * mgaScreen->cpp + \
48 dPriv->y * pitch); \
49 char *buf = (char *)(sPriv->pFB + \
50 mmesa->drawOffset + \
51 dPriv->x * mgaScreen->cpp + \
52 dPriv->y * pitch); \
53 GLuint p; \
54 (void) read_buf; (void) buf; (void) p
55
56
57
58 #define LOCAL_DEPTH_VARS \
59 __DRIdrawablePrivate *dPriv = mmesa->driDrawable; \
60 mgaScreenPrivate *mgaScreen = mmesa->mgaScreen; \
61 __DRIscreenPrivate *sPriv = mmesa->driScreen; \
62 GLuint pitch = mgaScreen->frontPitch; \
63 GLuint height = dPriv->h; \
64 char *buf = (char *)(sPriv->pFB + \
65 mgaScreen->depthOffset + \
66 dPriv->x * mgaScreen->cpp + \
67 dPriv->y * pitch)
68
69 #define LOCAL_STENCIL_VARS LOCAL_DEPTH_VARS
70
71 #define CLIPPIXEL(_x,_y) (_x >= minx && _x < maxx && \
72 _y >= miny && _y < maxy)
73
74 #define CLIPSPAN( _x, _y, _n, _x1, _n1, _i ) \
75 if ( _y < miny || _y >= maxy ) { \
76 _n1 = 0, _x1 = x; \
77 } else { \
78 _n1 = _n; \
79 _x1 = _x; \
80 if ( _x1 < minx ) _i += (minx-_x1), n1 -= (minx-_x1), _x1 = minx; \
81 if ( _x1 + _n1 >= maxx ) n1 -= (_x1 + n1 - maxx); \
82 }
83
84
85 #define HW_LOCK() \
86 mgaContextPtr mmesa = MGA_CONTEXT(ctx); \
87 FLUSH_BATCH(mmesa); \
88 LOCK_HARDWARE_QUIESCENT(mmesa);
89
90
91 #define HW_CLIPLOOP() \
92 do { \
93 int _nc = mmesa->numClipRects; \
94 while (_nc--) { \
95 int minx = mmesa->pClipRects[_nc].x1 - mmesa->drawX; \
96 int miny = mmesa->pClipRects[_nc].y1 - mmesa->drawY; \
97 int maxx = mmesa->pClipRects[_nc].x2 - mmesa->drawX; \
98 int maxy = mmesa->pClipRects[_nc].y2 - mmesa->drawY;
99
100 #define HW_ENDCLIPLOOP() \
101 } \
102 } while (0)
103
104 #define HW_UNLOCK() \
105 UNLOCK_HARDWARE(mmesa);
106
107
108
109
110
111
112
113 /* 16 bit, 565 rgb color spanline and pixel functions
114 */
115 #define Y_FLIP(_y) (height - _y - 1)
116
117 #undef INIT_MONO_PIXEL
118 #define INIT_MONO_PIXEL(p, color) \
119 p = PACK_COLOR_565( color[0], color[1], color[2] )
120
121
122 #define WRITE_RGBA( _x, _y, r, g, b, a ) \
123 *(GLushort *)(buf + _x*2 + _y*pitch) = ( (((int)r & 0xf8) << 8) | \
124 (((int)g & 0xfc) << 3) | \
125 (((int)b & 0xf8) >> 3))
126
127 #define WRITE_PIXEL( _x, _y, p ) \
128 *(GLushort *)(buf + _x*2 + _y*pitch) = p
129
130 #define READ_RGBA( rgba, _x, _y ) \
131 do { \
132 GLushort p = *(GLushort *)(read_buf + _x*2 + _y*pitch); \
133 rgba[0] = (((p >> 11) & 0x1f) * 255) / 31; \
134 rgba[1] = (((p >> 5) & 0x3f) * 255) / 63; \
135 rgba[2] = (((p >> 0) & 0x1f) * 255) / 31; \
136 rgba[3] = 255; \
137 } while(0)
138
139 #define TAG(x) mga##x##_565
140 #include "spantmp.h"
141
142
143
144
145
146 /* 32 bit, 8888 argb color spanline and pixel functions
147 */
148
149 #undef INIT_MONO_PIXEL
150 #define INIT_MONO_PIXEL(p, color) \
151 p = PACK_COLOR_8888( color[3], color[0], color[1], color[2] )
152
153
154 #define WRITE_RGBA(_x, _y, r, g, b, a) \
155 *(GLuint *)(buf + _x*4 + _y*pitch) = ((r << 16) | \
156 (g << 8) | \
157 (b << 0) | \
158 (a << 24) )
159
160 #define WRITE_PIXEL(_x, _y, p) \
161 *(GLuint *)(buf + _x*4 + _y*pitch) = p
162
163 #define READ_RGBA(rgba, _x, _y) \
164 do { \
165 GLuint p = *(GLuint *)(read_buf + _x*4 + _y*pitch); \
166 rgba[0] = (p >> 16) & 0xff; \
167 rgba[1] = (p >> 8) & 0xff; \
168 rgba[2] = (p >> 0) & 0xff; \
169 rgba[3] = 0xff; \
170 } while (0)
171
172 #define TAG(x) mga##x##_8888
173 #include "spantmp.h"
174
175
176
177
178 /* 16 bit depthbuffer functions.
179 */
180 #define WRITE_DEPTH( _x, _y, d ) \
181 *(GLushort *)(buf + _x*2 + _y*pitch) = d;
182
183 #define READ_DEPTH( d, _x, _y ) \
184 d = *(GLushort *)(buf + _x*2 + _y*pitch);
185
186 #define TAG(x) mga##x##_16
187 #include "depthtmp.h"
188
189
190
191
192 /* 32 bit depthbuffer functions.
193 */
194 #define WRITE_DEPTH( _x, _y, d ) \
195 *(GLuint *)(buf + _x*4 + _y*pitch) = d;
196
197 #define READ_DEPTH( d, _x, _y ) \
198 d = *(GLuint *)(buf + _x*4 + _y*pitch);
199
200 #define TAG(x) mga##x##_32
201 #include "depthtmp.h"
202
203
204
205 /* 24/8 bit interleaved depth/stencil functions
206 */
207 #define WRITE_DEPTH( _x, _y, d ) { \
208 GLuint tmp = *(GLuint *)(buf + _x*4 + _y*pitch); \
209 tmp &= 0xff; \
210 tmp |= (d) << 8; \
211 *(GLuint *)(buf + _x*4 + _y*pitch) = tmp; \
212 }
213
214 #define READ_DEPTH( d, _x, _y ) { \
215 d = (*(GLuint *)(buf + _x*4 + _y*pitch) & ~0xff) >> 8; \
216 }
217
218 #define TAG(x) mga##x##_24_8
219 #include "depthtmp.h"
220
221 #define WRITE_STENCIL( _x, _y, d ) { \
222 GLuint tmp = *(GLuint *)(buf + _x*4 + _y*pitch); \
223 tmp &= 0xffffff00; \
224 tmp |= d & 0xff; \
225 *(GLuint *)(buf + _x*4 + _y*pitch) = tmp; \
226 }
227
228 #define READ_STENCIL( d, _x, _y ) \
229 d = *(GLuint *)(buf + _x*4 + _y*pitch) & 0xff;
230
231 #define TAG(x) mga##x##_24_8
232 #include "stenciltmp.h"
233
234
235
236 /*
237 * This function is called to specify which buffer to read and write
238 * for software rasterization (swrast) fallbacks. This doesn't necessarily
239 * correspond to glDrawBuffer() or glReadBuffer() calls.
240 */
241 static void mgaDDSetBuffer(GLcontext *ctx, GLframebuffer *buffer,
242 GLuint bufferBit)
243 {
244 mgaContextPtr mmesa = MGA_CONTEXT(ctx);
245
246 if (bufferBit == FRONT_LEFT_BIT)
247 {
248 mmesa->drawOffset = mmesa->mgaScreen->frontOffset;
249 mmesa->readOffset = mmesa->mgaScreen->frontOffset;
250 }
251 else if (bufferBit == BACK_LEFT_BIT)
252 {
253 mmesa->drawOffset = mmesa->mgaScreen->backOffset;
254 mmesa->readOffset = mmesa->mgaScreen->backOffset;
255 }
256 else {
257 assert(0);
258 }
259 }
260
261 void mgaDDInitSpanFuncs( GLcontext *ctx )
262 {
263 mgaContextPtr mmesa = MGA_CONTEXT(ctx);
264 struct swrast_device_driver *swdd = _swrast_GetDeviceDriverReference(ctx);
265
266 swdd->SetBuffer = mgaDDSetBuffer;
267
268 switch (mmesa->mgaScreen->cpp) {
269 case 2:
270 swdd->WriteRGBASpan = mgaWriteRGBASpan_565;
271 swdd->WriteRGBSpan = mgaWriteRGBSpan_565;
272 swdd->WriteMonoRGBASpan = mgaWriteMonoRGBASpan_565;
273 swdd->WriteRGBAPixels = mgaWriteRGBAPixels_565;
274 swdd->WriteMonoRGBAPixels = mgaWriteMonoRGBAPixels_565;
275 swdd->ReadRGBASpan = mgaReadRGBASpan_565;
276 swdd->ReadRGBAPixels = mgaReadRGBAPixels_565;
277
278 swdd->ReadDepthSpan = mgaReadDepthSpan_16;
279 swdd->WriteDepthSpan = mgaWriteDepthSpan_16;
280 swdd->ReadDepthPixels = mgaReadDepthPixels_16;
281 swdd->WriteDepthPixels = mgaWriteDepthPixels_16;
282 break;
283
284 case 4:
285 swdd->WriteRGBASpan = mgaWriteRGBASpan_8888;
286 swdd->WriteRGBSpan = mgaWriteRGBSpan_8888;
287 swdd->WriteMonoRGBASpan = mgaWriteMonoRGBASpan_8888;
288 swdd->WriteRGBAPixels = mgaWriteRGBAPixels_8888;
289 swdd->WriteMonoRGBAPixels = mgaWriteMonoRGBAPixels_8888;
290 swdd->ReadRGBASpan = mgaReadRGBASpan_8888;
291 swdd->ReadRGBAPixels = mgaReadRGBAPixels_8888;
292
293 if (!mmesa->hw_stencil) {
294 swdd->ReadDepthSpan = mgaReadDepthSpan_32;
295 swdd->WriteDepthSpan = mgaWriteDepthSpan_32;
296 swdd->ReadDepthPixels = mgaReadDepthPixels_32;
297 swdd->WriteDepthPixels = mgaWriteDepthPixels_32;
298 } else {
299 swdd->ReadDepthSpan = mgaReadDepthSpan_24_8;
300 swdd->WriteDepthSpan = mgaWriteDepthSpan_24_8;
301 swdd->ReadDepthPixels = mgaReadDepthPixels_24_8;
302 swdd->WriteDepthPixels = mgaWriteDepthPixels_24_8;
303
304 swdd->ReadStencilSpan = mgaReadStencilSpan_24_8;
305 swdd->WriteStencilSpan = mgaWriteStencilSpan_24_8;
306 swdd->ReadStencilPixels = mgaReadStencilPixels_24_8;
307 swdd->WriteStencilPixels = mgaWriteStencilPixels_24_8;
308 }
309 break;
310 }
311 }