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