Remove the #if 0-ed out old span code.
[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
126 #define GET_SRC_PTR(_x, _y) (read_buf + _x * 2 + _y * pitch)
127 #define GET_DST_PTR(_x, _y) ( buf + _x * 2 + _y * pitch)
128 #define SPANTMP_PIXEL_FMT GL_RGB
129 #define SPANTMP_PIXEL_TYPE GL_UNSIGNED_SHORT_5_6_5
130
131 #define TAG(x) r200##x##_RGB565
132 #define TAG2(x,y) r200##x##_RGB565##y
133 #include "spantmp2.h"
134
135 /* 32 bit, ARGB8888 color spanline and pixel functions
136 */
137
138 #define GET_SRC_PTR(_x, _y) (read_buf + _x * 4 + _y * pitch)
139 #define GET_DST_PTR(_x, _y) ( buf + _x * 4 + _y * pitch)
140 #define SPANTMP_PIXEL_FMT GL_BGRA
141 #define SPANTMP_PIXEL_TYPE GL_UNSIGNED_INT_8_8_8_8_REV
142
143 #define TAG(x) r200##x##_ARGB8888
144 #define TAG2(x,y) r200##x##_ARGB8888##y
145 #include "spantmp2.h"
146
147
148 /* ================================================================
149 * Depth buffer
150 */
151
152 /* The Radeon family has depth tiling on all the time, so we have to convert
153 * the x,y coordinates into the memory bus address (mba) in the same
154 * manner as the engine. In each case, the linear block address (ba)
155 * is calculated, and then wired with x and y to produce the final
156 * memory address.
157 * The chip will do address translation on its own if the surface registers
158 * are set up correctly. It is not quite enough to get it working with hyperz too...
159 */
160
161 #define BIT(x,b) ((x & (1<<b))>>b)
162 static GLuint r200_mba_z32( r200ContextPtr rmesa,
163 GLint x, GLint y )
164 {
165 GLuint pitch = rmesa->r200Screen->frontPitch;
166 if (rmesa->r200Screen->depthHasSurface) {
167 return 4*(x + y*pitch);
168 }
169 else {
170 GLuint b = ((y & 0x7FF) >> 4) * ((pitch & 0xFFF) >> 5) + ((x & 0x7FF) >> 5);
171 GLuint a =
172 (BIT(x,0) << 2) |
173 (BIT(y,0) << 3) |
174 (BIT(x,1) << 4) |
175 (BIT(y,1) << 5) |
176 (BIT(x,3) << 6) |
177 (BIT(x,4) << 7) |
178 (BIT(x,2) << 8) |
179 (BIT(y,2) << 9) |
180 (BIT(y,3) << 10) |
181 (((pitch & 0x20) ? (b & 0x01) : ((b & 0x01) ^ (BIT(y,4)))) << 11) |
182 ((b >> 1) << 12);
183 return a;
184 }
185 }
186
187 static GLuint r200_mba_z16( r200ContextPtr rmesa, GLint x, GLint y )
188 {
189 GLuint pitch = rmesa->r200Screen->frontPitch;
190 if (rmesa->r200Screen->depthHasSurface) {
191 return 2*(x + y*pitch);
192 }
193 else {
194 GLuint b = ((y & 0x7FF) >> 4) * ((pitch & 0xFFF) >> 6) + ((x & 0x7FF) >> 6);
195 GLuint a =
196 (BIT(x,0) << 1) |
197 (BIT(y,0) << 2) |
198 (BIT(x,1) << 3) |
199 (BIT(y,1) << 4) |
200 (BIT(x,2) << 5) |
201 (BIT(x,4) << 6) |
202 (BIT(x,5) << 7) |
203 (BIT(x,3) << 8) |
204 (BIT(y,2) << 9) |
205 (BIT(y,3) << 10) |
206 (((pitch & 0x40) ? (b & 0x01) : ((b & 0x01) ^ (BIT(y,4)))) << 11) |
207 ((b >> 1) << 12);
208 return a;
209 }
210 }
211
212
213 /* 16-bit depth buffer functions
214 */
215
216 #define WRITE_DEPTH( _x, _y, d ) \
217 *(GLushort *)(buf + r200_mba_z16( rmesa, _x + xo, _y + yo )) = d;
218
219 #define READ_DEPTH( d, _x, _y ) \
220 d = *(GLushort *)(buf + r200_mba_z16( rmesa, _x + xo, _y + yo ));
221
222 #define TAG(x) r200##x##_16
223 #include "depthtmp.h"
224
225
226 /* 24 bit depth, 8 bit stencil depthbuffer functions
227 */
228
229 #define WRITE_DEPTH( _x, _y, d ) \
230 do { \
231 GLuint offset = r200_mba_z32( rmesa, _x + xo, _y + yo ); \
232 GLuint tmp = *(GLuint *)(buf + offset); \
233 tmp &= 0xff000000; \
234 tmp |= ((d) & 0x00ffffff); \
235 *(GLuint *)(buf + offset) = tmp; \
236 } while (0)
237
238 #define READ_DEPTH( d, _x, _y ) \
239 d = *(GLuint *)(buf + r200_mba_z32( rmesa, _x + xo, \
240 _y + yo )) & 0x00ffffff;
241
242 #define TAG(x) r200##x##_24_8
243 #include "depthtmp.h"
244
245
246 /* ================================================================
247 * Stencil buffer
248 */
249
250 /* 24 bit depth, 8 bit stencil depthbuffer functions
251 */
252 #define WRITE_STENCIL( _x, _y, d ) \
253 do { \
254 GLuint offset = r200_mba_z32( rmesa, _x + xo, _y + yo ); \
255 GLuint tmp = *(GLuint *)(buf + offset); \
256 tmp &= 0x00ffffff; \
257 tmp |= (((d) & 0xff) << 24); \
258 *(GLuint *)(buf + offset) = tmp; \
259 } while (0)
260
261 #define READ_STENCIL( d, _x, _y ) \
262 do { \
263 GLuint offset = r200_mba_z32( rmesa, _x + xo, _y + yo ); \
264 GLuint tmp = *(GLuint *)(buf + offset); \
265 tmp &= 0xff000000; \
266 d = tmp >> 24; \
267 } while (0)
268
269 #define TAG(x) r200##x##_24_8
270 #include "stenciltmp.h"
271
272
273 /*
274 * This function is called to specify which buffer to read and write
275 * for software rasterization (swrast) fallbacks. This doesn't necessarily
276 * correspond to glDrawBuffer() or glReadBuffer() calls.
277 */
278 static void r200SetBuffer( GLcontext *ctx,
279 GLframebuffer *colorBuffer,
280 GLuint bufferBit )
281 {
282 r200ContextPtr rmesa = R200_CONTEXT(ctx);
283
284 switch ( bufferBit ) {
285 case BUFFER_BIT_FRONT_LEFT:
286 if ( rmesa->doPageFlip && rmesa->sarea->pfCurrentPage == 1 ) {
287 rmesa->state.pixel.readOffset = rmesa->r200Screen->backOffset;
288 rmesa->state.pixel.readPitch = rmesa->r200Screen->backPitch;
289 rmesa->state.color.drawOffset = rmesa->r200Screen->backOffset;
290 rmesa->state.color.drawPitch = rmesa->r200Screen->backPitch;
291 } else {
292 rmesa->state.pixel.readOffset = rmesa->r200Screen->frontOffset;
293 rmesa->state.pixel.readPitch = rmesa->r200Screen->frontPitch;
294 rmesa->state.color.drawOffset = rmesa->r200Screen->frontOffset;
295 rmesa->state.color.drawPitch = rmesa->r200Screen->frontPitch;
296 }
297 break;
298 case BUFFER_BIT_BACK_LEFT:
299 if ( rmesa->doPageFlip && rmesa->sarea->pfCurrentPage == 1 ) {
300 rmesa->state.pixel.readOffset = rmesa->r200Screen->frontOffset;
301 rmesa->state.pixel.readPitch = rmesa->r200Screen->frontPitch;
302 rmesa->state.color.drawOffset = rmesa->r200Screen->frontOffset;
303 rmesa->state.color.drawPitch = rmesa->r200Screen->frontPitch;
304 } else {
305 rmesa->state.pixel.readOffset = rmesa->r200Screen->backOffset;
306 rmesa->state.pixel.readPitch = rmesa->r200Screen->backPitch;
307 rmesa->state.color.drawOffset = rmesa->r200Screen->backOffset;
308 rmesa->state.color.drawPitch = rmesa->r200Screen->backPitch;
309 }
310 break;
311 default:
312 _mesa_problem(ctx, "Bad bufferBit in %s", __FUNCTION__);
313 break;
314 }
315 }
316
317 /* Move locking out to get reasonable span performance (10x better
318 * than doing this in HW_LOCK above). WaitForIdle() is the main
319 * culprit.
320 */
321
322 static void r200SpanRenderStart( GLcontext *ctx )
323 {
324 r200ContextPtr rmesa = R200_CONTEXT( ctx );
325
326 R200_FIREVERTICES( rmesa );
327 LOCK_HARDWARE( rmesa );
328 r200WaitForIdleLocked( rmesa );
329
330 /* Read & rewrite the first pixel in the frame buffer. This should
331 * be a noop, right? In fact without this conform fails as reading
332 * from the framebuffer sometimes produces old results -- the
333 * on-card read cache gets mixed up and doesn't notice that the
334 * framebuffer has been updated.
335 *
336 * In the worst case this is buggy too as p might get the wrong
337 * value first time, so really need a hidden pixel somewhere for this.
338 */
339 {
340 int p;
341 volatile int *read_buf = (volatile int *)(rmesa->dri.screen->pFB +
342 rmesa->state.pixel.readOffset);
343 p = *read_buf;
344 *read_buf = p;
345 }
346 }
347
348 static void r200SpanRenderFinish( GLcontext *ctx )
349 {
350 r200ContextPtr rmesa = R200_CONTEXT( ctx );
351 _swrast_flush( ctx );
352 UNLOCK_HARDWARE( rmesa );
353 }
354
355 void r200InitSpanFuncs( GLcontext *ctx )
356 {
357 r200ContextPtr rmesa = R200_CONTEXT(ctx);
358 struct swrast_device_driver *swdd = _swrast_GetDeviceDriverReference(ctx);
359
360 swdd->SetBuffer = r200SetBuffer;
361 swdd->SpanRenderStart = r200SpanRenderStart;
362 swdd->SpanRenderFinish = r200SpanRenderFinish;
363 }
364
365
366
367 /**
368 * Plug in the Get/Put routines for the given driRenderbuffer.
369 */
370 void
371 r200SetSpanFunctions(driRenderbuffer *drb, const GLvisual *vis)
372 {
373 if (drb->Base.InternalFormat == GL_RGBA) {
374 if (vis->redBits == 5 && vis->greenBits == 6 && vis->blueBits == 5) {
375 r200InitPointers_RGB565(&drb->Base);
376 }
377 else {
378 r200InitPointers_ARGB8888(&drb->Base);
379 }
380 }
381 else if (drb->Base.InternalFormat == GL_DEPTH_COMPONENT16) {
382 drb->Base.GetRow = r200ReadDepthSpan_16;
383 drb->Base.GetValues = r200ReadDepthPixels_16;
384 drb->Base.PutRow = r200WriteDepthSpan_16;
385 drb->Base.PutMonoRow = r200WriteMonoDepthSpan_16;
386 drb->Base.PutValues = r200WriteDepthPixels_16;
387 drb->Base.PutMonoValues = NULL;
388 }
389 else if (drb->Base.InternalFormat == GL_DEPTH_COMPONENT24) {
390 drb->Base.GetRow = r200ReadDepthSpan_24_8;
391 drb->Base.GetValues = r200ReadDepthPixels_24_8;
392 drb->Base.PutRow = r200WriteDepthSpan_24_8;
393 drb->Base.PutMonoRow = r200WriteMonoDepthSpan_24_8;
394 drb->Base.PutValues = r200WriteDepthPixels_24_8;
395 drb->Base.PutMonoValues = NULL;
396 }
397 else if (drb->Base.InternalFormat == GL_STENCIL_INDEX8_EXT) {
398 drb->Base.GetRow = r200ReadStencilSpan_24_8;
399 drb->Base.GetValues = r200ReadStencilPixels_24_8;
400 drb->Base.PutRow = r200WriteStencilSpan_24_8;
401 drb->Base.PutMonoRow = r200WriteMonoStencilSpan_24_8;
402 drb->Base.PutValues = r200WriteStencilPixels_24_8;
403 drb->Base.PutMonoValues = NULL;
404 }
405 }