obsolete in favor of regular Makefiles
[mesa.git] / src / mesa / drivers / dri / r200 / r200_span.c
1 /* $XFree86: xc/lib/GL/mesa/src/drv/r200/r200_span.c,v 1.1 2002/10/30 12:51:52 alanh Exp $ */
2 /*
3 Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
4
5 The Weather Channel (TM) funded Tungsten Graphics to develop the
6 initial release of the Radeon 8500 driver under the XFree86 license.
7 This notice must be preserved.
8
9 Permission is hereby granted, free of charge, to any person obtaining
10 a copy of this software and associated documentation files (the
11 "Software"), to deal in the Software without restriction, including
12 without limitation the rights to use, copy, modify, merge, publish,
13 distribute, sublicense, and/or sell copies of the Software, and to
14 permit persons to whom the Software is furnished to do so, subject to
15 the following conditions:
16
17 The above copyright notice and this permission notice (including the
18 next paragraph) shall be included in all copies or substantial
19 portions of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
25 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28
29 **************************************************************************/
30
31 /*
32 * Authors:
33 * Keith Whitwell <keith@tungstengraphics.com>
34 */
35
36 #include "glheader.h"
37 #include "imports.h"
38 #include "swrast/swrast.h"
39 #include "colormac.h"
40
41 #include "r200_context.h"
42 #include "r200_ioctl.h"
43 #include "r200_state.h"
44 #include "r200_span.h"
45 #include "r200_tex.h"
46
47 #define DBG 0
48
49 #define LOCAL_VARS \
50 r200ContextPtr rmesa = R200_CONTEXT(ctx); \
51 r200ScreenPtr r200Screen = rmesa->r200Screen; \
52 __DRIscreenPrivate *sPriv = rmesa->dri.screen; \
53 __DRIdrawablePrivate *dPriv = rmesa->dri.drawable; \
54 GLuint pitch = r200Screen->frontPitch * r200Screen->cpp; \
55 GLuint height = dPriv->h; \
56 char *buf = (char *)(sPriv->pFB + \
57 rmesa->state.color.drawOffset + \
58 (dPriv->x * r200Screen->cpp) + \
59 (dPriv->y * pitch)); \
60 char *read_buf = (char *)(sPriv->pFB + \
61 rmesa->state.pixel.readOffset + \
62 (dPriv->x * r200Screen->cpp) + \
63 (dPriv->y * pitch)); \
64 GLuint p; \
65 (void) read_buf; (void) buf; (void) p
66
67 #define LOCAL_DEPTH_VARS \
68 r200ContextPtr rmesa = R200_CONTEXT(ctx); \
69 r200ScreenPtr r200Screen = rmesa->r200Screen; \
70 __DRIscreenPrivate *sPriv = rmesa->dri.screen; \
71 __DRIdrawablePrivate *dPriv = rmesa->dri.drawable; \
72 GLuint height = dPriv->h; \
73 GLuint xo = dPriv->x; \
74 GLuint yo = dPriv->y; \
75 char *buf = (char *)(sPriv->pFB + r200Screen->depthOffset); \
76 (void) buf
77
78 #define LOCAL_STENCIL_VARS LOCAL_DEPTH_VARS
79
80
81 #define CLIPPIXEL( _x, _y ) \
82 ((_x >= minx) && (_x < maxx) && (_y >= miny) && (_y < maxy))
83
84
85 #define CLIPSPAN( _x, _y, _n, _x1, _n1, _i ) \
86 if ( _y < miny || _y >= maxy ) { \
87 _n1 = 0, _x1 = x; \
88 } else { \
89 _n1 = _n; \
90 _x1 = _x; \
91 if ( _x1 < minx ) _i += (minx-_x1), n1 -= (minx-_x1), _x1 = minx; \
92 if ( _x1 + _n1 >= maxx ) n1 -= (_x1 + n1 - maxx); \
93 }
94
95 #define Y_FLIP( _y ) (height - _y - 1)
96
97
98 #define HW_LOCK()
99
100 #define HW_CLIPLOOP() \
101 do { \
102 __DRIdrawablePrivate *dPriv = rmesa->dri.drawable; \
103 int _nc = dPriv->numClipRects; \
104 \
105 while ( _nc-- ) { \
106 int minx = dPriv->pClipRects[_nc].x1 - dPriv->x; \
107 int miny = dPriv->pClipRects[_nc].y1 - dPriv->y; \
108 int maxx = dPriv->pClipRects[_nc].x2 - dPriv->x; \
109 int maxy = dPriv->pClipRects[_nc].y2 - dPriv->y;
110
111 #define HW_ENDCLIPLOOP() \
112 } \
113 } while (0)
114
115 #define HW_UNLOCK()
116
117
118
119 /* ================================================================
120 * Color buffer
121 */
122
123 /* 16 bit, RGB565 color spanline and pixel functions
124 */
125 #define INIT_MONO_PIXEL(p, color) \
126 p = PACK_COLOR_565( color[0], color[1], color[2] )
127
128 #define WRITE_RGBA( _x, _y, r, g, b, a ) \
129 *(GLushort *)(buf + _x*2 + _y*pitch) = ((((int)r & 0xf8) << 8) | \
130 (((int)g & 0xfc) << 3) | \
131 (((int)b & 0xf8) >> 3))
132
133 #define WRITE_PIXEL( _x, _y, p ) \
134 *(GLushort *)(buf + _x*2 + _y*pitch) = p
135
136 #define READ_RGBA( rgba, _x, _y ) \
137 do { \
138 GLushort p = *(GLushort *)(read_buf + _x*2 + _y*pitch); \
139 rgba[0] = ((p >> 8) & 0xf8) * 255 / 0xf8; \
140 rgba[1] = ((p >> 3) & 0xfc) * 255 / 0xfc; \
141 rgba[2] = ((p << 3) & 0xf8) * 255 / 0xf8; \
142 rgba[3] = 0xff; \
143 } while (0)
144
145 #define TAG(x) r200##x##_RGB565
146 #include "spantmp.h"
147
148 /* 32 bit, ARGB8888 color spanline and pixel functions
149 */
150 #undef INIT_MONO_PIXEL
151 #define INIT_MONO_PIXEL(p, color) \
152 p = PACK_COLOR_8888( color[3], color[0], color[1], color[2] )
153
154 #define WRITE_RGBA( _x, _y, r, g, b, a ) \
155 do { \
156 *(GLuint *)(buf + _x*4 + _y*pitch) = ((b << 0) | \
157 (g << 8) | \
158 (r << 16) | \
159 (a << 24) ); \
160 } while (0)
161
162 #define WRITE_PIXEL( _x, _y, p ) \
163 do { \
164 *(GLuint *)(buf + _x*4 + _y*pitch) = p; \
165 } while (0)
166
167 #define READ_RGBA( rgba, _x, _y ) \
168 do { \
169 volatile GLuint *ptr = (volatile GLuint *)(read_buf + _x*4 + _y*pitch); \
170 GLuint p = *ptr; \
171 rgba[0] = (p >> 16) & 0xff; \
172 rgba[1] = (p >> 8) & 0xff; \
173 rgba[2] = (p >> 0) & 0xff; \
174 rgba[3] = (p >> 24) & 0xff; \
175 } while (0)
176
177 #define TAG(x) r200##x##_ARGB8888
178 #include "spantmp.h"
179
180
181
182 /* ================================================================
183 * Depth buffer
184 */
185
186 /* The Radeon family has depth tiling on all the time, so we have to convert
187 * the x,y coordinates into the memory bus address (mba) in the same
188 * manner as the engine. In each case, the linear block address (ba)
189 * is calculated, and then wired with x and y to produce the final
190 * memory address.
191 */
192
193 #define BIT(x,b) ((x & (1<<b))>>b)
194 static GLuint r200_mba_z32( r200ContextPtr rmesa,
195 GLint x, GLint y )
196 {
197 GLuint pitch = rmesa->r200Screen->frontPitch;
198 GLuint b = ((y & 0x3FF) >> 4) * ((pitch & 0xFFF) >> 5) + ((x & 0x3FF) >> 5);
199 GLuint a =
200 (BIT(x,0) << 2) |
201 (BIT(y,0) << 3) |
202 (BIT(x,1) << 4) |
203 (BIT(y,1) << 5) |
204 (BIT(x,3) << 6) |
205 (BIT(x,4) << 7) |
206 (BIT(x,2) << 8) |
207 (BIT(y,2) << 9) |
208 (BIT(y,3) << 10) |
209 (((pitch & 0x20) ? (b & 0x01) : ((b & 0x01) ^ (BIT(y,4)))) << 11) |
210 ((b >> 1) << 12);
211 return a;
212 }
213
214 static GLuint r200_mba_z16( r200ContextPtr rmesa, GLint x, GLint y )
215 {
216 GLuint pitch = rmesa->r200Screen->frontPitch;
217 GLuint b = ((y & 0x3FF) >> 4) * ((pitch & 0xFFF) >> 6) + ((x & 0x3FF) >> 6);
218 GLuint a =
219 (BIT(x,0) << 1) |
220 (BIT(y,0) << 2) |
221 (BIT(x,1) << 3) |
222 (BIT(y,1) << 4) |
223 (BIT(x,2) << 5) |
224 (BIT(x,4) << 6) |
225 (BIT(x,5) << 7) |
226 (BIT(x,3) << 8) |
227 (BIT(y,2) << 9) |
228 (BIT(y,3) << 10) |
229 (((pitch & 0x40) ? (b & 0x01) : ((b & 0x01) ^ (BIT(y,4)))) << 11) |
230 ((b >> 1) << 12);
231 return a;
232 }
233
234
235 /* 16-bit depth buffer functions
236 */
237 #define WRITE_DEPTH( _x, _y, d ) \
238 *(GLushort *)(buf + r200_mba_z16( rmesa, _x + xo, _y + yo )) = d;
239
240 #define READ_DEPTH( d, _x, _y ) \
241 d = *(GLushort *)(buf + r200_mba_z16( rmesa, _x + xo, _y + yo ));
242
243 #define TAG(x) r200##x##_16
244 #include "depthtmp.h"
245
246 /* 24 bit depth, 8 bit stencil depthbuffer functions
247 */
248 #define WRITE_DEPTH( _x, _y, d ) \
249 do { \
250 GLuint offset = r200_mba_z32( rmesa, _x + xo, _y + yo ); \
251 GLuint tmp = *(GLuint *)(buf + offset); \
252 tmp &= 0xff000000; \
253 tmp |= ((d) & 0x00ffffff); \
254 *(GLuint *)(buf + offset) = tmp; \
255 } while (0)
256
257 #define READ_DEPTH( d, _x, _y ) \
258 d = *(GLuint *)(buf + r200_mba_z32( rmesa, _x + xo, \
259 _y + yo )) & 0x00ffffff;
260
261 #define TAG(x) r200##x##_24_8
262 #include "depthtmp.h"
263
264
265 /* ================================================================
266 * Stencil buffer
267 */
268
269 /* 24 bit depth, 8 bit stencil depthbuffer functions
270 */
271 #define WRITE_STENCIL( _x, _y, d ) \
272 do { \
273 GLuint offset = r200_mba_z32( rmesa, _x + xo, _y + yo ); \
274 GLuint tmp = *(GLuint *)(buf + offset); \
275 tmp &= 0x00ffffff; \
276 tmp |= (((d) & 0xff) << 24); \
277 *(GLuint *)(buf + offset) = tmp; \
278 } while (0)
279
280 #define READ_STENCIL( d, _x, _y ) \
281 do { \
282 GLuint offset = r200_mba_z32( rmesa, _x + xo, _y + yo ); \
283 GLuint tmp = *(GLuint *)(buf + offset); \
284 tmp &= 0xff000000; \
285 d = tmp >> 24; \
286 } while (0)
287
288 #define TAG(x) r200##x##_24_8
289 #include "stenciltmp.h"
290
291
292 /*
293 * This function is called to specify which buffer to read and write
294 * for software rasterization (swrast) fallbacks. This doesn't necessarily
295 * correspond to glDrawBuffer() or glReadBuffer() calls.
296 */
297 static void r200SetBuffer( GLcontext *ctx,
298 GLframebuffer *colorBuffer,
299 GLuint bufferBit )
300 {
301 r200ContextPtr rmesa = R200_CONTEXT(ctx);
302
303 switch ( bufferBit ) {
304 case DD_FRONT_LEFT_BIT:
305 if ( rmesa->doPageFlip && rmesa->sarea->pfCurrentPage == 1 ) {
306 rmesa->state.pixel.readOffset = rmesa->r200Screen->backOffset;
307 rmesa->state.pixel.readPitch = rmesa->r200Screen->backPitch;
308 rmesa->state.color.drawOffset = rmesa->r200Screen->backOffset;
309 rmesa->state.color.drawPitch = rmesa->r200Screen->backPitch;
310 } else {
311 rmesa->state.pixel.readOffset = rmesa->r200Screen->frontOffset;
312 rmesa->state.pixel.readPitch = rmesa->r200Screen->frontPitch;
313 rmesa->state.color.drawOffset = rmesa->r200Screen->frontOffset;
314 rmesa->state.color.drawPitch = rmesa->r200Screen->frontPitch;
315 }
316 break;
317 case DD_BACK_LEFT_BIT:
318 if ( rmesa->doPageFlip && rmesa->sarea->pfCurrentPage == 1 ) {
319 rmesa->state.pixel.readOffset = rmesa->r200Screen->frontOffset;
320 rmesa->state.pixel.readPitch = rmesa->r200Screen->frontPitch;
321 rmesa->state.color.drawOffset = rmesa->r200Screen->frontOffset;
322 rmesa->state.color.drawPitch = rmesa->r200Screen->frontPitch;
323 } else {
324 rmesa->state.pixel.readOffset = rmesa->r200Screen->backOffset;
325 rmesa->state.pixel.readPitch = rmesa->r200Screen->backPitch;
326 rmesa->state.color.drawOffset = rmesa->r200Screen->backOffset;
327 rmesa->state.color.drawPitch = rmesa->r200Screen->backPitch;
328 }
329 break;
330 default:
331 _mesa_problem(ctx, "Bad bufferBit in %s", __FUNCTION__);
332 break;
333 }
334 }
335
336 /* Move locking out to get reasonable span performance (10x better
337 * than doing this in HW_LOCK above). WaitForIdle() is the main
338 * culprit.
339 */
340
341 static void r200SpanRenderStart( GLcontext *ctx )
342 {
343 r200ContextPtr rmesa = R200_CONTEXT( ctx );
344
345 R200_FIREVERTICES( rmesa );
346 LOCK_HARDWARE( rmesa );
347 r200WaitForIdleLocked( rmesa );
348
349 /* Read & rewrite the first pixel in the frame buffer. This should
350 * be a noop, right? In fact without this conform fails as reading
351 * from the framebuffer sometimes produces old results -- the
352 * on-card read cache gets mixed up and doesn't notice that the
353 * framebuffer has been updated.
354 *
355 * In the worst case this is buggy too as p might get the wrong
356 * value first time, so really need a hidden pixel somewhere for this.
357 */
358 {
359 int p;
360 volatile int *read_buf = (volatile int *)(rmesa->dri.screen->pFB +
361 rmesa->state.pixel.readOffset);
362 p = *read_buf;
363 *read_buf = p;
364 }
365 }
366
367 static void r200SpanRenderFinish( GLcontext *ctx )
368 {
369 r200ContextPtr rmesa = R200_CONTEXT( ctx );
370 _swrast_flush( ctx );
371 UNLOCK_HARDWARE( rmesa );
372 }
373
374 void r200InitSpanFuncs( GLcontext *ctx )
375 {
376 r200ContextPtr rmesa = R200_CONTEXT(ctx);
377 struct swrast_device_driver *swdd = _swrast_GetDeviceDriverReference(ctx);
378
379 swdd->SetBuffer = r200SetBuffer;
380
381 switch ( rmesa->r200Screen->cpp ) {
382 case 2:
383 swdd->WriteRGBASpan = r200WriteRGBASpan_RGB565;
384 swdd->WriteRGBSpan = r200WriteRGBSpan_RGB565;
385 swdd->WriteMonoRGBASpan = r200WriteMonoRGBASpan_RGB565;
386 swdd->WriteRGBAPixels = r200WriteRGBAPixels_RGB565;
387 swdd->WriteMonoRGBAPixels = r200WriteMonoRGBAPixels_RGB565;
388 swdd->ReadRGBASpan = r200ReadRGBASpan_RGB565;
389 swdd->ReadRGBAPixels = r200ReadRGBAPixels_RGB565;
390 break;
391
392 case 4:
393 swdd->WriteRGBASpan = r200WriteRGBASpan_ARGB8888;
394 swdd->WriteRGBSpan = r200WriteRGBSpan_ARGB8888;
395 swdd->WriteMonoRGBASpan = r200WriteMonoRGBASpan_ARGB8888;
396 swdd->WriteRGBAPixels = r200WriteRGBAPixels_ARGB8888;
397 swdd->WriteMonoRGBAPixels = r200WriteMonoRGBAPixels_ARGB8888;
398 swdd->ReadRGBASpan = r200ReadRGBASpan_ARGB8888;
399 swdd->ReadRGBAPixels = r200ReadRGBAPixels_ARGB8888;
400 break;
401
402 default:
403 break;
404 }
405
406 switch ( rmesa->glCtx->Visual.depthBits ) {
407 case 16:
408 swdd->ReadDepthSpan = r200ReadDepthSpan_16;
409 swdd->WriteDepthSpan = r200WriteDepthSpan_16;
410 swdd->ReadDepthPixels = r200ReadDepthPixels_16;
411 swdd->WriteDepthPixels = r200WriteDepthPixels_16;
412 break;
413
414 case 24:
415 swdd->ReadDepthSpan = r200ReadDepthSpan_24_8;
416 swdd->WriteDepthSpan = r200WriteDepthSpan_24_8;
417 swdd->ReadDepthPixels = r200ReadDepthPixels_24_8;
418 swdd->WriteDepthPixels = r200WriteDepthPixels_24_8;
419
420 swdd->ReadStencilSpan = r200ReadStencilSpan_24_8;
421 swdd->WriteStencilSpan = r200WriteStencilSpan_24_8;
422 swdd->ReadStencilPixels = r200ReadStencilPixels_24_8;
423 swdd->WriteStencilPixels = r200WriteStencilPixels_24_8;
424 break;
425
426 default:
427 break;
428 }
429
430 swdd->SpanRenderStart = r200SpanRenderStart;
431 swdd->SpanRenderFinish = r200SpanRenderFinish;
432 }