Added support for floating point depth buffers on Savage4-based
[mesa.git] / src / mesa / drivers / dri / savage / savagespan.c
1 /*
2 * Copyright 1998-2003 VIA Technologies, Inc. All Rights Reserved.
3 * Copyright 2001-2003 S3 Graphics, Inc. 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 * the rights to use, copy, modify, merge, publish, distribute, sub license,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
14 * of the 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 * VIA, S3 GRAPHICS, 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 OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 #include "mtypes.h"
26 #include "savagedd.h"
27 #include "savagespan.h"
28 #include "savageioctl.h"
29 #include "savage_bci.h"
30 #include "savage_3d_reg.h"
31 #include "swrast/swrast.h"
32
33 #define DBG 0
34
35 #define LOCAL_VARS \
36 savageContextPtr imesa = SAVAGE_CONTEXT(ctx); \
37 __DRIdrawablePrivate *dPriv = imesa->mesa_drawable; \
38 savageScreenPrivate *savageScreen = imesa->savageScreen; \
39 GLuint cpp = savageScreen->cpp; \
40 GLuint pitch = imesa->aperturePitch; \
41 GLuint height = dPriv->h; \
42 char *buf = (char *)(imesa->drawMap + \
43 dPriv->x * cpp + \
44 dPriv->y * pitch); \
45 char *read_buf = (char *)(imesa->readMap + \
46 dPriv->x * cpp + \
47 dPriv->y * pitch); \
48 GLuint p = SAVAGE_CONTEXT( ctx )->MonoColor; \
49 (void) read_buf; (void) buf; (void) p
50
51 #define LOCAL_DEPTH_VARS \
52 savageContextPtr imesa = SAVAGE_CONTEXT(ctx); \
53 __DRIdrawablePrivate *dPriv = imesa->mesa_drawable; \
54 savageScreenPrivate *savageScreen = imesa->savageScreen; \
55 GLuint zpp = savageScreen->zpp; \
56 GLuint pitch = imesa->aperturePitch; \
57 GLuint height = dPriv->h; \
58 char *buf = (char *)(imesa->apertureBase[TARGET_DEPTH] + \
59 dPriv->x * zpp + \
60 dPriv->y * pitch)
61
62 #define LOCAL_STENCIL_VARS LOCAL_DEPTH_VARS
63
64 #define INIT_MONO_PIXEL(p)
65
66 #define CLIPPIXEL(_x,_y) (_x >= minx && _x < maxx && \
67 _y >= miny && _y < maxy)
68
69
70 #define CLIPSPAN( _x, _y, _n, _x1, _n1, _i ) \
71 if ( _y < miny || _y >= maxy ) { \
72 _n1 = 0, _x1 = x; \
73 } else { \
74 _n1 = _n; \
75 _x1 = _x; \
76 if ( _x1 < minx ) _i += (minx-_x1), n1 -= (minx-_x1), _x1 = minx; \
77 if ( _x1 + _n1 >= maxx ) n1 -= (_x1 + n1 - maxx); \
78 }
79
80 #define Y_FLIP(_y) (height - _y - 1)
81
82 #define HW_LOCK()
83
84 #define HW_CLIPLOOP() \
85 do { \
86 __DRIdrawablePrivate *dPriv = imesa->driDrawable; \
87 int _nc = dPriv->numClipRects; \
88 while (_nc--) { \
89 int minx = dPriv->pClipRects[_nc].x1 - dPriv->x; \
90 int miny = dPriv->pClipRects[_nc].y1 - dPriv->y; \
91 int maxx = dPriv->pClipRects[_nc].x2 - dPriv->x; \
92 int maxy = dPriv->pClipRects[_nc].y2 - dPriv->y;
93
94
95 #define HW_ENDCLIPLOOP() \
96 } \
97 } while (0)
98
99 #define HW_UNLOCK()
100
101
102 /* 16 bit, 565 rgb color spanline and pixel functions
103 */
104 #undef INIT_MONO_PIXEL
105 #define INIT_MONO_PIXEL(p, color) \
106 p = SAVAGEPACKCOLOR565( color[0], color[1], color[2] )
107
108 #define WRITE_RGBA( _x, _y, r, g, b, a ) \
109 do{ \
110 *(GLushort *)(buf + (_x<<1) + _y*pitch) = ( (((int)r & 0xf8) << 8) |\
111 (((int)g & 0xfc) << 3) | \
112 (((int)b & 0xf8) >> 3)); \
113 }while(0)
114 #define WRITE_PIXEL( _x, _y, p ) \
115 do{ \
116 *(GLushort *)(buf + (_x<<1) + _y*pitch) = p; \
117 }while(0)
118
119 #define READ_RGBA( rgba, _x, _y ) \
120 do { \
121 GLushort p = *(GLushort *)(read_buf + (_x<<1) + _y*pitch); \
122 rgba[0] = (((p >> 11) & 0x1f) * 255) >>5; \
123 rgba[1] = (((p >> 5) & 0x3f) * 255) >>6; \
124 rgba[2] = (((p >> 0) & 0x1f) * 255) >>5; \
125 rgba[3] = 255; \
126 } while(0)
127
128 #define TAG(x) savage##x##_565
129 #include "spantmp.h"
130
131
132 /* 32 bit, 8888 ARGB color spanline and pixel functions
133 */
134 #undef INIT_MONO_PIXEL
135 #define INIT_MONO_PIXEL(p, color) \
136 p = SAVAGEPACKCOLOR8888( color[0], color[1], color[2], color[3] )
137
138 #define WRITE_RGBA( _x, _y, r, g, b, a ) \
139 *(GLuint *)(buf + (_x<<2) + _y*pitch) = ( ((GLuint)a << 24) | \
140 ((GLuint)r << 16) | \
141 ((GLuint)g << 8) | \
142 ((GLuint)b ))
143 #define WRITE_PIXEL( _x, _y, p ) \
144 *(GLuint *)(buf + (_x<<2) + _y*pitch) = p
145
146 #define READ_RGBA( rgba, _x, _y ) \
147 do { \
148 GLuint p = *(GLuint *)(read_buf + (_x<<2) + _y*pitch); \
149 rgba[0] = (p >> 16) & 0xFF; \
150 rgba[1] = (p >> 8) & 0xFF; \
151 rgba[2] = (p >> 0) & 0xFF; \
152 rgba[3] = 0xFF; \
153 } while(0)
154
155 #define TAG(x) savage##x##_8888
156 #include "spantmp.h"
157
158
159
160
161 /* 16 bit integer depthbuffer functions
162 * Depth range is reversed. See also savageCalcViewport.
163 */
164 #define WRITE_DEPTH( _x, _y, d ) \
165 *(GLushort *)(buf + ((_x)<<1) + (_y)*pitch) = 0xFFFF - d
166
167 #define READ_DEPTH( d, _x, _y ) \
168 d = 0xFFFF - *(GLushort *)(buf + ((_x)<<1) + (_y)*pitch)
169
170 #define TAG(x) savage##x##_16
171 #include "depthtmp.h"
172
173
174
175
176
177 /* 16 bit float depthbuffer functions
178 */
179 #define WRITE_DEPTH( _x, _y, d ) \
180 *(GLushort *)(buf + ((_x)<<1) + (_y)*pitch) = \
181 savageEncodeFloat16( 1.0 - (GLfloat)d/65535.0 )
182
183 #define READ_DEPTH( d, _x, _y ) \
184 d = 65535 - \
185 savageDecodeFloat16( *(GLushort *)(buf + ((_x)<<1) + (_y)*pitch) ) * \
186 65535.0
187
188 #define TAG(x) savage##x##_16f
189 #include "depthtmp.h"
190
191
192
193
194
195 /* 8-bit stencil /24-bit integer depth depthbuffer functions.
196 * Depth range is reversed. See also savageCalcViewport.
197 */
198 #define WRITE_DEPTH( _x, _y, d ) do { \
199 GLuint tmp = *(GLuint *)(buf + ((_x)<<2) + (_y)*pitch); \
200 tmp &= 0xFF000000; \
201 tmp |= 0x00FFFFFF - d; \
202 *(GLuint *)(buf + (_x<<2) + _y*pitch) = tmp; \
203 } while(0)
204
205 #define READ_DEPTH( d, _x, _y ) \
206 d = 0x00FFFFFF - (*(GLuint *)(buf + ((_x)<<2) + (_y)*pitch) & 0x00FFFFFF)
207
208 #define TAG(x) savage##x##_8_24
209 #include "depthtmp.h"
210
211
212
213
214
215 /* 24 bit float depthbuffer functions
216 */
217 #define WRITE_DEPTH( _x, _y, d ) do { \
218 GLuint tmp = *(GLuint *)(buf + ((_x)<<2) + (_y)*pitch); \
219 tmp &= 0xFF000000; \
220 tmp |= savageEncodeFloat24( 1.0 - (GLfloat)d/16777215.0 ); \
221 *(GLuint *)(buf + (_x<<2) + _y*pitch) = tmp; \
222 } while(0)
223
224 #define READ_DEPTH( d, _x, _y ) \
225 d = 16777215 - savageDecodeFloat24( \
226 *(GLuint *)(buf + ((_x)<<2) + (_y)*pitch) & 0x00FFFFFF) \
227 * 16777215.0
228
229 #define TAG(x) savage##x##_8_24f
230 #include "depthtmp.h"
231
232
233 #define WRITE_STENCIL( _x, _y, d ) do { \
234 GLuint tmp = *(GLuint *)(buf + ((_x)<<2) + (_y)*pitch); \
235 tmp &= 0x00FFFFFF; \
236 tmp |= (((GLuint)d)<<24) & 0xFF000000; \
237 *(GLuint *)(buf + ((_x)<<2) + (_y)*pitch) = tmp; \
238 } while(0)
239
240 #define READ_STENCIL( d, _x, _y ) \
241 d = (GLstencil)((*(GLuint *)(buf + ((_x)<<2) + (_y)*pitch) & 0xFF000000) >> 24)
242
243 #define TAG(x) savage##x##_8_24
244 #include "stenciltmp.h"
245
246
247 /*
248 * This function is called to specify which buffer to read and write
249 * for software rasterization (swrast) fallbacks. This doesn't necessarily
250 * correspond to glDrawBuffer() or glReadBuffer() calls.
251 */
252 static void savageDDSetBuffer(GLcontext *ctx, GLframebuffer *buffer,
253 GLuint bufferBit)
254 {
255 savageContextPtr imesa = SAVAGE_CONTEXT(ctx);
256 char *map;
257
258 assert((bufferBit == DD_FRONT_LEFT_BIT) || (bufferBit == DD_BACK_LEFT_BIT));
259
260 map = (bufferBit == DD_FRONT_LEFT_BIT)
261 ? (char*)imesa->apertureBase[TARGET_FRONT]
262 : (char*)imesa->apertureBase[TARGET_BACK];
263
264 imesa->drawMap = map;
265 imesa->readMap = map;
266
267 assert( (buffer == imesa->driDrawable->driverPrivate)
268 || (buffer == imesa->driReadable->driverPrivate) );
269
270 imesa->mesa_drawable = (buffer == imesa->driDrawable->driverPrivate)
271 ? imesa->driDrawable : imesa->driReadable;
272 }
273
274 /*
275 * Wrappers around _swrast_Copy/Draw/ReadPixels that make sure all
276 * primitives are flushed and the hardware is idle before accessing
277 * the frame buffer.
278 */
279 static void
280 savageCopyPixels( GLcontext *ctx,
281 GLint srcx, GLint srcy, GLsizei width, GLsizei height,
282 GLint destx, GLint desty,
283 GLenum type )
284 {
285 savageContextPtr imesa = SAVAGE_CONTEXT(ctx);
286 FLUSH_BATCH(imesa);
287 WAIT_IDLE_EMPTY;
288 _swrast_CopyPixels(ctx, srcx, srcy, width, height, destx, desty, type);
289 }
290 static void
291 savageDrawPixels( GLcontext *ctx,
292 GLint x, GLint y,
293 GLsizei width, GLsizei height,
294 GLenum format, GLenum type,
295 const struct gl_pixelstore_attrib *packing,
296 const GLvoid *pixels )
297 {
298 savageContextPtr imesa = SAVAGE_CONTEXT(ctx);
299 FLUSH_BATCH(imesa);
300 WAIT_IDLE_EMPTY;
301 _swrast_DrawPixels(ctx, x, y, width, height, format, type, packing, pixels);
302 }
303 static void
304 savageReadPixels( GLcontext *ctx,
305 GLint x, GLint y, GLsizei width, GLsizei height,
306 GLenum format, GLenum type,
307 const struct gl_pixelstore_attrib *packing,
308 GLvoid *pixels )
309 {
310 savageContextPtr imesa = SAVAGE_CONTEXT(ctx);
311 FLUSH_BATCH(imesa);
312 WAIT_IDLE_EMPTY;
313 _swrast_ReadPixels(ctx, x, y, width, height, format, type, packing, pixels);
314 }
315
316 /*
317 * Make sure the hardware is idle when span-rendering.
318 */
319 static void savageSpanRenderStart( GLcontext *ctx )
320 {
321 savageContextPtr imesa = SAVAGE_CONTEXT(ctx);
322 FLUSH_BATCH(imesa);
323 WAIT_IDLE_EMPTY;
324 }
325
326
327 void savageDDInitSpanFuncs( GLcontext *ctx )
328 {
329 savageContextPtr imesa = SAVAGE_CONTEXT(ctx);
330 struct swrast_device_driver *swdd = _swrast_GetDeviceDriverReference(ctx);
331
332 swdd->SetBuffer = savageDDSetBuffer;
333
334 switch (imesa->savageScreen->cpp)
335 {
336 case 2:
337 swdd->WriteRGBASpan = savageWriteRGBASpan_565;
338 swdd->WriteRGBSpan = savageWriteRGBSpan_565;
339 swdd->WriteMonoRGBASpan = savageWriteMonoRGBASpan_565;
340 swdd->WriteRGBAPixels = savageWriteRGBAPixels_565;
341 swdd->WriteMonoRGBAPixels = savageWriteMonoRGBAPixels_565;
342 swdd->ReadRGBASpan = savageReadRGBASpan_565;
343 swdd->ReadRGBAPixels = savageReadRGBAPixels_565;
344
345 break;
346
347 case 4:
348 swdd->WriteRGBASpan = savageWriteRGBASpan_8888;
349 swdd->WriteRGBSpan = savageWriteRGBSpan_8888;
350 swdd->WriteMonoRGBASpan = savageWriteMonoRGBASpan_8888;
351 swdd->WriteRGBAPixels = savageWriteRGBAPixels_8888;
352 swdd->WriteMonoRGBAPixels = savageWriteMonoRGBAPixels_8888;
353 swdd->ReadRGBASpan = savageReadRGBASpan_8888;
354 swdd->ReadRGBAPixels = savageReadRGBAPixels_8888;
355 }
356
357 switch (imesa->savageScreen->zpp)
358 {
359 case 2:
360 if (imesa->float_depth) {
361 swdd->ReadDepthSpan = savageReadDepthSpan_16f;
362 swdd->WriteDepthSpan = savageWriteDepthSpan_16f;
363 swdd->WriteMonoDepthSpan = savageWriteMonoDepthSpan_16f;
364 swdd->ReadDepthPixels = savageReadDepthPixels_16f;
365 swdd->WriteDepthPixels = savageWriteDepthPixels_16f;
366 } else {
367 swdd->ReadDepthSpan = savageReadDepthSpan_16;
368 swdd->WriteDepthSpan = savageWriteDepthSpan_16;
369 swdd->WriteMonoDepthSpan = savageWriteMonoDepthSpan_16;
370 swdd->ReadDepthPixels = savageReadDepthPixels_16;
371 swdd->WriteDepthPixels = savageWriteDepthPixels_16;
372 }
373
374 break;
375 case 4:
376 if (imesa->float_depth) {
377 swdd->ReadDepthSpan = savageReadDepthSpan_8_24f;
378 swdd->WriteDepthSpan = savageWriteDepthSpan_8_24f;
379 swdd->WriteMonoDepthSpan = savageWriteMonoDepthSpan_8_24f;
380 swdd->ReadDepthPixels = savageReadDepthPixels_8_24f;
381 swdd->WriteDepthPixels = savageWriteDepthPixels_8_24f;
382 } else {
383 swdd->ReadDepthSpan = savageReadDepthSpan_8_24;
384 swdd->WriteDepthSpan = savageWriteDepthSpan_8_24;
385 swdd->WriteMonoDepthSpan = savageWriteMonoDepthSpan_8_24;
386 swdd->ReadDepthPixels = savageReadDepthPixels_8_24;
387 swdd->WriteDepthPixels = savageWriteDepthPixels_8_24;
388 }
389 swdd->ReadStencilSpan = savageReadStencilSpan_8_24;
390 swdd->WriteStencilSpan = savageWriteStencilSpan_8_24;
391 swdd->ReadStencilPixels = savageReadStencilPixels_8_24;
392 swdd->WriteStencilPixels = savageWriteStencilPixels_8_24;
393 break;
394
395 }
396 swdd->WriteCI8Span =NULL;
397 swdd->WriteCI32Span =NULL;
398 swdd->WriteMonoCISpan =NULL;
399 swdd->WriteCI32Pixels =NULL;
400 swdd->WriteMonoCIPixels =NULL;
401 swdd->ReadCI32Span =NULL;
402 swdd->ReadCI32Pixels =NULL;
403
404 swdd->SpanRenderStart = savageSpanRenderStart;
405
406 /* Pixel path fallbacks.
407 */
408 ctx->Driver.Accum = _swrast_Accum;
409 ctx->Driver.Bitmap = _swrast_Bitmap;
410 ctx->Driver.CopyPixels = savageCopyPixels;
411 ctx->Driver.DrawPixels = savageDrawPixels;
412 ctx->Driver.ReadPixels = savageReadPixels;
413 }