start removing old (pre-renderbuffer) span code
[mesa.git] / src / mesa / drivers / dri / r128 / r128_span.c
1 /* $XFree86: xc/lib/GL/mesa/src/drv/r128/r128_span.c,v 1.8 2002/10/30 12:51:39 alanh Exp $ */
2 /**************************************************************************
3
4 Copyright 1999, 2000 ATI Technologies Inc. and Precision Insight, Inc.,
5 Cedar Park, Texas.
6 All Rights Reserved.
7
8 Permission is hereby granted, free of charge, to any person obtaining a
9 copy of this software and associated documentation files (the "Software"),
10 to deal in the Software without restriction, including without limitation
11 on the rights to use, copy, modify, merge, publish, distribute, sub
12 license, and/or sell copies of the Software, and to permit persons to whom
13 the Software is furnished to do so, subject to the following conditions:
14
15 The above copyright notice and this permission notice (including the next
16 paragraph) shall be included in all copies or substantial portions of the
17 Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22 ATI, PRECISION INSIGHT AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
23 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25 USE OR OTHER DEALINGS IN THE SOFTWARE.
26
27 **************************************************************************/
28
29 /*
30 * Authors:
31 * Gareth Hughes <gareth@valinux.com>
32 * Keith Whitwell <keith@tungstengraphics.com>
33 * Kevin E. Martin <martin@valinux.com>
34 *
35 */
36
37 #include "r128_context.h"
38 #include "r128_ioctl.h"
39 #include "r128_state.h"
40 #include "r128_span.h"
41 #include "r128_tex.h"
42
43 #include "swrast/swrast.h"
44
45 #define DBG 0
46
47 #define HAVE_HW_DEPTH_SPANS 1
48 #define HAVE_HW_DEPTH_PIXELS 1
49
50 #define LOCAL_VARS \
51 r128ContextPtr rmesa = R128_CONTEXT(ctx); \
52 r128ScreenPtr r128scrn = rmesa->r128Screen; \
53 __DRIscreenPrivate *sPriv = rmesa->driScreen; \
54 __DRIdrawablePrivate *dPriv = rmesa->driDrawable; \
55 GLuint pitch = r128scrn->frontPitch * r128scrn->cpp; \
56 GLuint height = dPriv->h; \
57 char *buf = (char *)(sPriv->pFB + \
58 rmesa->drawOffset + \
59 (dPriv->x * r128scrn->cpp) + \
60 (dPriv->y * pitch)); \
61 char *read_buf = (char *)(sPriv->pFB + \
62 rmesa->readOffset + \
63 (dPriv->x * r128scrn->cpp) + \
64 (dPriv->y * pitch)); \
65 GLuint p; \
66 (void) read_buf; (void) buf; (void) p
67
68 #define LOCAL_DEPTH_VARS \
69 r128ContextPtr rmesa = R128_CONTEXT(ctx); \
70 r128ScreenPtr r128scrn = rmesa->r128Screen; \
71 __DRIscreenPrivate *sPriv = rmesa->driScreen; \
72 __DRIdrawablePrivate *dPriv = rmesa->driDrawable; \
73 GLuint height = dPriv->h; \
74 (void) r128scrn; (void) sPriv; (void) height
75
76 #define LOCAL_STENCIL_VARS LOCAL_DEPTH_VARS
77
78
79 #define CLIPPIXEL( _x, _y ) \
80 ((_x >= minx) && (_x < maxx) && (_y >= miny) && (_y < maxy))
81
82
83 #define CLIPSPAN( _x, _y, _n, _x1, _n1, _i ) \
84 if ( _y < miny || _y >= maxy ) { \
85 _n1 = 0, _x1 = x; \
86 } else { \
87 _n1 = _n; \
88 _x1 = _x; \
89 if ( _x1 < minx ) _i += (minx-_x1), n1 -= (minx-_x1), _x1 = minx; \
90 if ( _x1 + _n1 >= maxx ) n1 -= (_x1 + n1 - maxx); \
91 }
92
93 #define Y_FLIP( _y ) (height - _y - 1)
94
95
96 #define HW_LOCK()
97
98 #define HW_CLIPLOOP() \
99 do { \
100 __DRIdrawablePrivate *dPriv = rmesa->driDrawable; \
101 int _nc = dPriv->numClipRects; \
102 \
103 while ( _nc-- ) { \
104 int minx = dPriv->pClipRects[_nc].x1 - dPriv->x; \
105 int miny = dPriv->pClipRects[_nc].y1 - dPriv->y; \
106 int maxx = dPriv->pClipRects[_nc].x2 - dPriv->x; \
107 int maxy = dPriv->pClipRects[_nc].y2 - dPriv->y;
108
109 #define HW_ENDCLIPLOOP() \
110 } \
111 } while (0)
112
113 #define HW_UNLOCK()
114
115
116
117 /* ================================================================
118 * Color buffer
119 */
120
121 /* 16 bit, RGB565 color spanline and pixel functions
122 */
123 #define GET_SRC_PTR(_x, _y) (read_buf + _x * 2 + _y * pitch)
124 #define GET_DST_PTR(_x, _y) ( buf + _x * 2 + _y * pitch)
125 #define SPANTMP_PIXEL_FMT GL_RGB
126 #define SPANTMP_PIXEL_TYPE GL_UNSIGNED_SHORT_5_6_5
127
128 #define TAG(x) r128##x##_RGB565
129 #define TAG2(x,y) r128##x##_RGB565##y
130 #include "spantmp2.h"
131
132
133 /* 32 bit, ARGB8888 color spanline and pixel functions
134 */
135 #define GET_SRC_PTR(_x, _y) (read_buf + _x * 4 + _y * pitch)
136 #define GET_DST_PTR(_x, _y) ( buf + _x * 4 + _y * pitch)
137 #define SPANTMP_PIXEL_FMT GL_BGRA
138 #define SPANTMP_PIXEL_TYPE GL_UNSIGNED_INT_8_8_8_8_REV
139
140 #define TAG(x) r128##x##_ARGB8888
141 #define TAG2(x,y) r128##x##_ARGB8888##y
142 #include "spantmp2.h"
143
144
145 /* ================================================================
146 * Depth buffer
147 */
148
149 /* 16-bit depth buffer functions
150 */
151
152 #define WRITE_DEPTH_SPAN() \
153 r128WriteDepthSpanLocked( rmesa, n, \
154 x + dPriv->x, \
155 y + dPriv->y, \
156 depth, mask );
157
158 #define WRITE_DEPTH_PIXELS() \
159 do { \
160 GLint ox[MAX_WIDTH]; \
161 GLint oy[MAX_WIDTH]; \
162 for ( i = 0 ; i < n ; i++ ) { \
163 ox[i] = x[i] + dPriv->x; \
164 oy[i] = Y_FLIP( y[i] ) + dPriv->y; \
165 } \
166 r128WriteDepthPixelsLocked( rmesa, n, ox, oy, depth, mask ); \
167 } while (0)
168
169 #define READ_DEPTH_SPAN() \
170 do { \
171 GLushort *buf = (GLushort *)((GLubyte *)sPriv->pFB + \
172 r128scrn->spanOffset); \
173 GLint i; \
174 \
175 r128ReadDepthSpanLocked( rmesa, n, \
176 x + dPriv->x, \
177 y + dPriv->y ); \
178 r128WaitForIdleLocked( rmesa ); \
179 \
180 for ( i = 0 ; i < n ; i++ ) { \
181 depth[i] = buf[i]; \
182 } \
183 } while (0)
184
185 #define READ_DEPTH_PIXELS() \
186 do { \
187 GLushort *buf = (GLushort *)((GLubyte *)sPriv->pFB + \
188 r128scrn->spanOffset); \
189 GLint i, remaining = n; \
190 \
191 while ( remaining > 0 ) { \
192 GLint ox[128]; \
193 GLint oy[128]; \
194 GLint count; \
195 \
196 if ( remaining <= 128 ) { \
197 count = remaining; \
198 } else { \
199 count = 128; \
200 } \
201 for ( i = 0 ; i < count ; i++ ) { \
202 ox[i] = x[i] + dPriv->x; \
203 oy[i] = Y_FLIP( y[i] ) + dPriv->y; \
204 } \
205 \
206 r128ReadDepthPixelsLocked( rmesa, count, ox, oy ); \
207 r128WaitForIdleLocked( rmesa ); \
208 \
209 for ( i = 0 ; i < count ; i++ ) { \
210 depth[i] = buf[i]; \
211 } \
212 depth += count; \
213 x += count; \
214 y += count; \
215 remaining -= count; \
216 } \
217 } while (0)
218
219 #define TAG(x) r128##x##_16
220 #include "depthtmp.h"
221
222
223 /* 24-bit depth, 8-bit stencil buffer functions
224 */
225 #define WRITE_DEPTH_SPAN() \
226 r128WriteDepthSpanLocked( rmesa, n, \
227 x + dPriv->x, \
228 y + dPriv->y, \
229 depth, mask );
230
231 #define WRITE_DEPTH_PIXELS() \
232 do { \
233 GLint ox[MAX_WIDTH]; \
234 GLint oy[MAX_WIDTH]; \
235 for ( i = 0 ; i < n ; i++ ) { \
236 ox[i] = x[i] + dPriv->x; \
237 oy[i] = Y_FLIP( y[i] ) + dPriv->y; \
238 } \
239 r128WriteDepthPixelsLocked( rmesa, n, ox, oy, depth, mask ); \
240 } while (0)
241
242 #define READ_DEPTH_SPAN() \
243 do { \
244 GLuint *buf = (GLuint *)((GLubyte *)sPriv->pFB + \
245 r128scrn->spanOffset); \
246 GLint i; \
247 \
248 r128ReadDepthSpanLocked( rmesa, n, \
249 x + dPriv->x, \
250 y + dPriv->y ); \
251 r128WaitForIdleLocked( rmesa ); \
252 \
253 for ( i = 0 ; i < n ; i++ ) { \
254 depth[i] = buf[i] & 0x00ffffff; \
255 } \
256 } while (0)
257
258 #define READ_DEPTH_PIXELS() \
259 do { \
260 GLuint *buf = (GLuint *)((GLubyte *)sPriv->pFB + \
261 r128scrn->spanOffset); \
262 GLint i, remaining = n; \
263 \
264 while ( remaining > 0 ) { \
265 GLint ox[128]; \
266 GLint oy[128]; \
267 GLint count; \
268 \
269 if ( remaining <= 128 ) { \
270 count = remaining; \
271 } else { \
272 count = 128; \
273 } \
274 for ( i = 0 ; i < count ; i++ ) { \
275 ox[i] = x[i] + dPriv->x; \
276 oy[i] = Y_FLIP( y[i] ) + dPriv->y; \
277 } \
278 \
279 r128ReadDepthPixelsLocked( rmesa, count, ox, oy ); \
280 r128WaitForIdleLocked( rmesa ); \
281 \
282 for ( i = 0 ; i < count ; i++ ) { \
283 depth[i] = buf[i] & 0x00ffffff; \
284 } \
285 depth += count; \
286 x += count; \
287 y += count; \
288 remaining -= count; \
289 } \
290 } while (0)
291
292 #define TAG(x) r128##x##_24_8
293 #include "depthtmp.h"
294
295
296
297 /* ================================================================
298 * Stencil buffer
299 */
300
301 /* FIXME: Add support for hardware stencil buffers.
302 */
303
304 /*
305 * This function is called to specify which buffer to read and write
306 * for software rasterization (swrast) fallbacks. This doesn't necessarily
307 * correspond to glDrawBuffer() or glReadBuffer() calls.
308 */
309 static void r128DDSetBuffer( GLcontext *ctx,
310 GLframebuffer *colorBuffer,
311 GLuint bufferBit )
312 {
313 r128ContextPtr rmesa = R128_CONTEXT(ctx);
314
315 switch ( bufferBit ) {
316 case BUFFER_BIT_FRONT_LEFT:
317 if ( rmesa->sarea->pfCurrentPage == 1 ) {
318 rmesa->drawOffset = rmesa->readOffset = rmesa->r128Screen->backOffset;
319 rmesa->drawPitch = rmesa->readPitch = rmesa->r128Screen->backPitch;
320 } else {
321 rmesa->drawOffset = rmesa->readOffset = rmesa->r128Screen->frontOffset;
322 rmesa->drawPitch = rmesa->readPitch = rmesa->r128Screen->frontPitch;
323 }
324 break;
325 case BUFFER_BIT_BACK_LEFT:
326 if ( rmesa->sarea->pfCurrentPage == 1 ) {
327 rmesa->drawOffset = rmesa->readOffset = rmesa->r128Screen->frontOffset;
328 rmesa->drawPitch = rmesa->readPitch = rmesa->r128Screen->frontPitch;
329 } else {
330 rmesa->drawOffset = rmesa->readOffset = rmesa->r128Screen->backOffset;
331 rmesa->drawPitch = rmesa->readPitch = rmesa->r128Screen->backPitch;
332 }
333 break;
334 default:
335 break;
336 }
337 }
338
339 void r128SpanRenderStart( GLcontext *ctx )
340 {
341 r128ContextPtr rmesa = R128_CONTEXT(ctx);
342 FLUSH_BATCH(rmesa);
343 LOCK_HARDWARE(rmesa);
344 r128WaitForIdleLocked( rmesa );
345 }
346
347 void r128SpanRenderFinish( GLcontext *ctx )
348 {
349 r128ContextPtr rmesa = R128_CONTEXT(ctx);
350 _swrast_flush( ctx );
351 UNLOCK_HARDWARE( rmesa );
352 }
353
354 void r128DDInitSpanFuncs( GLcontext *ctx )
355 {
356 r128ContextPtr rmesa = R128_CONTEXT(ctx);
357 struct swrast_device_driver *swdd = _swrast_GetDeviceDriverReference(ctx);
358
359 swdd->SetBuffer = r128DDSetBuffer;
360 swdd->SpanRenderStart = r128SpanRenderStart;
361 swdd->SpanRenderFinish = r128SpanRenderFinish;
362 }
363
364
365 /**
366 * Plug in the Get/Put routines for the given driRenderbuffer.
367 */
368 void
369 r128SetSpanFunctions(driRenderbuffer *drb, const GLvisual *vis)
370 {
371 if (drb->Base.InternalFormat == GL_RGBA) {
372 if (vis->redBits == 5 && vis->greenBits == 6 && vis->blueBits == 5) {
373 r128InitPointers_RGB565(&drb->Base);
374 }
375 else {
376 r128InitPointers_ARGB8888(&drb->Base);
377 }
378 }
379 else if (drb->Base.InternalFormat == GL_DEPTH_COMPONENT16) {
380 drb->Base.GetRow = r128ReadDepthSpan_16;
381 drb->Base.GetValues = r128ReadDepthPixels_16;
382 drb->Base.PutRow = r128WriteDepthSpan_16;
383 drb->Base.PutMonoRow = r128WriteMonoDepthSpan_16;
384 drb->Base.PutValues = r128WriteDepthPixels_16;
385 drb->Base.PutMonoValues = NULL;
386 }
387 else if (drb->Base.InternalFormat == GL_DEPTH_COMPONENT24) {
388 drb->Base.GetRow = r128ReadDepthSpan_24_8;
389 drb->Base.GetValues = r128ReadDepthPixels_24_8;
390 drb->Base.PutRow = r128WriteDepthSpan_24_8;
391 drb->Base.PutMonoRow = r128WriteMonoDepthSpan_24_8;
392 drb->Base.PutValues = r128WriteDepthPixels_24_8;
393 drb->Base.PutMonoValues = NULL;
394 }
395 else if (drb->Base.InternalFormat == GL_STENCIL_INDEX8_EXT) {
396 drb->Base.GetRow = NULL;
397 drb->Base.GetValues = NULL;
398 drb->Base.PutRow = NULL;
399 drb->Base.PutMonoRow = NULL;
400 drb->Base.PutValues = NULL;
401 drb->Base.PutMonoValues = NULL;
402 }
403 }