Merge branch 'mesa_7_7_branch'
[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 "main/mtypes.h"
26 #include "savagedd.h"
27 #include "savagespan.h"
28 #include "savageioctl.h"
29 #include "savage_3d_reg.h"
30 #include "swrast/swrast.h"
31
32 #define DBG 0
33
34 #define LOCAL_VARS \
35 driRenderbuffer *drb = (driRenderbuffer *) rb; \
36 __DRIdrawable *const dPriv = drb->dPriv; \
37 GLuint cpp = drb->cpp; \
38 GLuint pitch = drb->pitch; \
39 GLuint height = dPriv->h; \
40 GLubyte *buf = drb->Base.Data + dPriv->x * cpp + dPriv->y * pitch; \
41 GLuint p; \
42 (void) p
43
44 #define LOCAL_DEPTH_VARS \
45 driRenderbuffer *drb = (driRenderbuffer *) rb; \
46 __DRIdrawable *const dPriv = drb->dPriv; \
47 GLuint zpp = drb->cpp; \
48 GLuint pitch = drb->pitch; \
49 GLuint height = dPriv->h; \
50 GLubyte *buf = drb->Base.Data + dPriv->x * zpp + dPriv->y * pitch;
51
52 #define LOCAL_STENCIL_VARS LOCAL_DEPTH_VARS
53
54 #define Y_FLIP(_y) (height - _y - 1)
55
56 #define HW_LOCK()
57
58 #define HW_UNLOCK()
59
60 #define HW_WRITE_LOCK()
61
62 #define HW_READ_LOCK()
63
64
65 /* 16 bit, 565 rgb color spanline and pixel functions
66 */
67 #define SPANTMP_PIXEL_FMT GL_RGB
68 #define SPANTMP_PIXEL_TYPE GL_UNSIGNED_SHORT_5_6_5
69
70 #define TAG(x) savage##x##_565
71 #define TAG2(x,y) savage##x##_565##y
72 #include "spantmp2.h"
73
74
75 /* 32 bit, 8888 ARGB color spanline and pixel functions
76 */
77 #define SPANTMP_PIXEL_FMT GL_BGRA
78 #define SPANTMP_PIXEL_TYPE GL_UNSIGNED_INT_8_8_8_8_REV
79
80 #define TAG(x) savage##x##_8888
81 #define TAG2(x,y) savage##x##_8888##y
82 #include "spantmp2.h"
83
84
85 #undef HW_WRITE_LOCK
86 #define HW_WRITE_LOCK()
87 #undef HW_READ_LOCK
88 #define HW_READ_LOCK()
89
90
91
92 /* 16 bit integer depthbuffer functions
93 * Depth range is reversed. See also savageCalcViewport.
94 */
95 #define VALUE_TYPE GLushort
96
97 #define WRITE_DEPTH( _x, _y, d ) \
98 *(GLushort *)(buf + ((_x)<<1) + (_y)*pitch) = 0xFFFF - d
99
100 #define READ_DEPTH( d, _x, _y ) \
101 d = 0xFFFF - *(GLushort *)(buf + ((_x)<<1) + (_y)*pitch)
102
103 #define TAG(x) savage##x##_z16
104 #include "depthtmp.h"
105
106
107
108
109 /* 16 bit float depthbuffer functions
110 */
111 #define VALUE_TYPE GLushort
112
113 #define WRITE_DEPTH( _x, _y, d ) \
114 *(GLushort *)(buf + ((_x)<<1) + (_y)*pitch) = \
115 savageEncodeFloat16( 1.0 - (GLfloat)d/65535.0 )
116
117 #define READ_DEPTH( d, _x, _y ) \
118 d = 65535 - \
119 savageDecodeFloat16( *(GLushort *)(buf + ((_x)<<1) + (_y)*pitch) ) * \
120 65535.0
121
122 #define TAG(x) savage##x##_z16f
123 #include "depthtmp.h"
124
125
126
127
128 /* 8-bit stencil /24-bit integer depth depthbuffer functions.
129 * Depth range is reversed. See also savageCalcViewport.
130 */
131 #define VALUE_TYPE GLuint
132
133 #define WRITE_DEPTH( _x, _y, d ) do { \
134 GLuint tmp = *(GLuint *)(buf + ((_x)<<2) + (_y)*pitch); \
135 tmp &= 0xFF000000; \
136 tmp |= 0x00FFFFFF - d; \
137 *(GLuint *)(buf + (_x<<2) + _y*pitch) = tmp; \
138 } while(0)
139
140 #define READ_DEPTH( d, _x, _y ) \
141 d = 0x00FFFFFF - (*(GLuint *)(buf + ((_x)<<2) + (_y)*pitch) & 0x00FFFFFF)
142
143 #define TAG(x) savage##x##_s8_z24
144 #include "depthtmp.h"
145
146
147
148
149 /* 24 bit float depthbuffer functions
150 */
151 #define VALUE_TYPE GLuint
152
153 #define WRITE_DEPTH( _x, _y, d ) do { \
154 GLuint tmp = *(GLuint *)(buf + ((_x)<<2) + (_y)*pitch); \
155 tmp &= 0xFF000000; \
156 tmp |= savageEncodeFloat24( 1.0 - (GLfloat)d/16777215.0 ); \
157 *(GLuint *)(buf + (_x<<2) + _y*pitch) = tmp; \
158 } while(0)
159
160 #define READ_DEPTH( d, _x, _y ) \
161 d = 16777215 - savageDecodeFloat24( \
162 *(GLuint *)(buf + ((_x)<<2) + (_y)*pitch) & 0x00FFFFFF) \
163 * 16777215.0
164
165 #define TAG(x) savage##x##_s8_z24f
166 #include "depthtmp.h"
167
168
169 #define WRITE_STENCIL( _x, _y, d ) do { \
170 GLuint tmp = *(GLuint *)(buf + ((_x)<<2) + (_y)*pitch); \
171 tmp &= 0x00FFFFFF; \
172 tmp |= (((GLuint)d)<<24) & 0xFF000000; \
173 *(GLuint *)(buf + ((_x)<<2) + (_y)*pitch) = tmp; \
174 } while(0)
175
176 #define READ_STENCIL( d, _x, _y ) \
177 d = (GLstencil)((*(GLuint *)(buf + ((_x)<<2) + (_y)*pitch) & 0xFF000000) >> 24)
178
179 #define TAG(x) savage##x##_s8_z24
180 #include "stenciltmp.h"
181
182
183
184 /*
185 * Wrappers around _swrast_Copy/Draw/ReadPixels that make sure all
186 * primitives are flushed and the hardware is idle before accessing
187 * the frame buffer.
188 */
189 static void
190 savageCopyPixels( GLcontext *ctx,
191 GLint srcx, GLint srcy, GLsizei width, GLsizei height,
192 GLint destx, GLint desty,
193 GLenum type )
194 {
195 savageContextPtr imesa = SAVAGE_CONTEXT(ctx);
196 FLUSH_BATCH(imesa);
197 WAIT_IDLE_EMPTY(imesa);
198 _swrast_CopyPixels(ctx, srcx, srcy, width, height, destx, desty, type);
199 }
200 static void
201 savageDrawPixels( GLcontext *ctx,
202 GLint x, GLint y,
203 GLsizei width, GLsizei height,
204 GLenum format, GLenum type,
205 const struct gl_pixelstore_attrib *packing,
206 const GLvoid *pixels )
207 {
208 savageContextPtr imesa = SAVAGE_CONTEXT(ctx);
209 FLUSH_BATCH(imesa);
210 WAIT_IDLE_EMPTY(imesa);
211 _swrast_DrawPixels(ctx, x, y, width, height, format, type, packing, pixels);
212 }
213 static void
214 savageReadPixels( GLcontext *ctx,
215 GLint x, GLint y, GLsizei width, GLsizei height,
216 GLenum format, GLenum type,
217 const struct gl_pixelstore_attrib *packing,
218 GLvoid *pixels )
219 {
220 savageContextPtr imesa = SAVAGE_CONTEXT(ctx);
221 FLUSH_BATCH(imesa);
222 WAIT_IDLE_EMPTY(imesa);
223 _swrast_ReadPixels(ctx, x, y, width, height, format, type, packing, pixels);
224 }
225
226 /*
227 * Make sure the hardware is idle when span-rendering.
228 */
229 static void savageSpanRenderStart( GLcontext *ctx )
230 {
231 savageContextPtr imesa = SAVAGE_CONTEXT(ctx);
232 FLUSH_BATCH(imesa);
233 WAIT_IDLE_EMPTY(imesa);
234 }
235
236
237 void savageDDInitSpanFuncs( GLcontext *ctx )
238 {
239 struct swrast_device_driver *swdd = _swrast_GetDeviceDriverReference(ctx);
240 swdd->SpanRenderStart = savageSpanRenderStart;
241
242 /* XXX these should probably be plugged in elsewhere */
243 ctx->Driver.CopyPixels = savageCopyPixels;
244 ctx->Driver.DrawPixels = savageDrawPixels;
245 ctx->Driver.ReadPixels = savageReadPixels;
246 }
247
248
249
250 /**
251 * Plug in the Get/Put routines for the given driRenderbuffer.
252 */
253 void
254 savageSetSpanFunctions(driRenderbuffer *drb, const GLvisual *vis,
255 GLboolean float_depth)
256 {
257 if (drb->Base.Format == MESA_FORMAT_RGB565) {
258 savageInitPointers_565(&drb->Base);
259 }
260 else if (drb->Base.Format == MESA_FORMAT_ARGB8888) {
261 savageInitPointers_8888(&drb->Base);
262 }
263 else if (drb->Base.Format == MESA_FORMAT_Z16) {
264 if (float_depth) {
265 savageInitDepthPointers_z16f(&drb->Base);
266 }
267 else {
268 savageInitDepthPointers_z16(&drb->Base);
269 }
270 }
271 else if (drb->Base.Format == MESA_FORMAT_S8_Z24) {
272 if (float_depth) {
273 savageInitDepthPointers_s8_z24f(&drb->Base);
274 }
275 else {
276 savageInitDepthPointers_s8_z24(&drb->Base);
277 }
278 }
279 else if (drb->Base.Format == MESA_FORMAT_S8) {
280 savageInitStencilPointers_s8_z24(&drb->Base);
281 }
282 }