Small optimization for big-endian (e.g., PowerPC) systems.
[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->mesa_drawable; \
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->mesa_drawable; \
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 #define Y_FLIP(_y) (height - _y - 1)
113
114 /* 16 bit, RGB565 color spanline and pixel functions
115 */
116
117 #define GET_SRC_PTR(_x, _y) (read_buf + _x * 2 + _y * pitch)
118 #define GET_DST_PTR(_x, _y) ( buf + _x * 2 + _y * pitch)
119 #define SPANTMP_PIXEL_FMT GL_RGB
120 #define SPANTMP_PIXEL_TYPE GL_UNSIGNED_SHORT_5_6_5
121
122 #define TAG(x) mga##x##_565
123 #define TAG2(x,y) mga##x##_565##y
124 #include "spantmp2.h"
125
126 /* 32 bit, ARGB8888 color spanline and pixel functions
127 */
128
129 #define GET_SRC_PTR(_x, _y) (read_buf + _x * 4 + _y * pitch)
130 #define GET_DST_PTR(_x, _y) ( buf + _x * 4 + _y * pitch)
131 #define SPANTMP_PIXEL_FMT GL_BGRA
132 #define SPANTMP_PIXEL_TYPE GL_UNSIGNED_INT_8_8_8_8_REV
133
134 #define TAG(x) mga##x##_8888
135 #define TAG2(x,y) mga##x##_8888##y
136 #include "spantmp2.h"
137
138
139 /* 16 bit depthbuffer functions.
140 */
141 #define WRITE_DEPTH( _x, _y, d ) \
142 *(GLushort *)(buf + _x*2 + _y*pitch) = d;
143
144 #define READ_DEPTH( d, _x, _y ) \
145 d = *(GLushort *)(buf + _x*2 + _y*pitch);
146
147 #define TAG(x) mga##x##_16
148 #include "depthtmp.h"
149
150
151
152
153 /* 32 bit depthbuffer functions.
154 */
155 #define WRITE_DEPTH( _x, _y, d ) \
156 *(GLuint *)(buf + _x*4 + _y*pitch) = d;
157
158 #define READ_DEPTH( d, _x, _y ) \
159 d = *(GLuint *)(buf + _x*4 + _y*pitch);
160
161 #define TAG(x) mga##x##_32
162 #include "depthtmp.h"
163
164
165
166 /* 24/8 bit interleaved depth/stencil functions
167 */
168 #define WRITE_DEPTH( _x, _y, d ) { \
169 GLuint tmp = *(GLuint *)(buf + _x*4 + _y*pitch); \
170 tmp &= 0xff; \
171 tmp |= (d) << 8; \
172 *(GLuint *)(buf + _x*4 + _y*pitch) = tmp; \
173 }
174
175 #define READ_DEPTH( d, _x, _y ) { \
176 d = (*(GLuint *)(buf + _x*4 + _y*pitch) & ~0xff) >> 8; \
177 }
178
179 #define TAG(x) mga##x##_24_8
180 #include "depthtmp.h"
181
182 #define WRITE_STENCIL( _x, _y, d ) { \
183 GLuint tmp = *(GLuint *)(buf + _x*4 + _y*pitch); \
184 tmp &= 0xffffff00; \
185 tmp |= d & 0xff; \
186 *(GLuint *)(buf + _x*4 + _y*pitch) = tmp; \
187 }
188
189 #define READ_STENCIL( d, _x, _y ) \
190 d = *(GLuint *)(buf + _x*4 + _y*pitch) & 0xff;
191
192 #define TAG(x) mga##x##_24_8
193 #include "stenciltmp.h"
194
195
196
197 /*
198 * This function is called to specify which buffer to read and write
199 * for software rasterization (swrast) fallbacks. This doesn't necessarily
200 * correspond to glDrawBuffer() or glReadBuffer() calls.
201 */
202 static void mgaDDSetBuffer(GLcontext *ctx, GLframebuffer *buffer,
203 GLuint bufferBit)
204 {
205 mgaContextPtr mmesa = MGA_CONTEXT(ctx);
206 unsigned int offset;
207
208 assert((bufferBit == DD_FRONT_LEFT_BIT) || (bufferBit == DD_BACK_LEFT_BIT));
209
210 offset = (bufferBit == DD_FRONT_LEFT_BIT)
211 ? mmesa->mgaScreen->frontOffset
212 : mmesa->mgaScreen->backOffset;
213
214 mmesa->drawOffset = offset;
215 mmesa->readOffset = offset;
216
217 assert( (buffer == mmesa->driDrawable->driverPrivate)
218 || (buffer == mmesa->driReadable->driverPrivate) );
219
220 mmesa->mesa_drawable = (buffer == mmesa->driDrawable->driverPrivate)
221 ? mmesa->driDrawable : mmesa->driReadable;
222 }
223
224 /**
225 * Initialize the driver callbacks for the read / write span functions.
226 *
227 * \bug
228 * To really support RGB888 and RGBA8888 visuals, we need separate read and
229 * write routines for 888 and 8888. We also need to determine whether or not
230 * the visual has destination alpha.
231 */
232 void mgaDDInitSpanFuncs( GLcontext *ctx )
233 {
234 mgaContextPtr mmesa = MGA_CONTEXT(ctx);
235 struct swrast_device_driver *swdd = _swrast_GetDeviceDriverReference(ctx);
236
237 swdd->SetBuffer = mgaDDSetBuffer;
238
239 switch (mmesa->mgaScreen->cpp) {
240 case 2:
241 mgaInitPointers_565( swdd );
242
243 swdd->ReadDepthSpan = mgaReadDepthSpan_16;
244 swdd->WriteDepthSpan = mgaWriteDepthSpan_16;
245 swdd->ReadDepthPixels = mgaReadDepthPixels_16;
246 swdd->WriteDepthPixels = mgaWriteDepthPixels_16;
247 break;
248
249 case 4:
250 mgaInitPointers_8888( swdd );
251
252 if (!mmesa->hw_stencil) {
253 swdd->ReadDepthSpan = mgaReadDepthSpan_32;
254 swdd->WriteDepthSpan = mgaWriteDepthSpan_32;
255 swdd->ReadDepthPixels = mgaReadDepthPixels_32;
256 swdd->WriteDepthPixels = mgaWriteDepthPixels_32;
257 } else {
258 swdd->ReadDepthSpan = mgaReadDepthSpan_24_8;
259 swdd->WriteDepthSpan = mgaWriteDepthSpan_24_8;
260 swdd->ReadDepthPixels = mgaReadDepthPixels_24_8;
261 swdd->WriteDepthPixels = mgaWriteDepthPixels_24_8;
262
263 swdd->ReadStencilSpan = mgaReadStencilSpan_24_8;
264 swdd->WriteStencilSpan = mgaWriteStencilSpan_24_8;
265 swdd->ReadStencilPixels = mgaReadStencilPixels_24_8;
266 swdd->WriteStencilPixels = mgaWriteStencilPixels_24_8;
267 }
268 break;
269 }
270 }