Merge branch 'dri2'
[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 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 WRITE_DEPTH( _x, _y, d ) \
97 *(GLushort *)(buf + ((_x)<<1) + (_y)*pitch) = 0xFFFF - d
98
99 #define READ_DEPTH( d, _x, _y ) \
100 d = 0xFFFF - *(GLushort *)(buf + ((_x)<<1) + (_y)*pitch)
101
102 #define TAG(x) savage##x##_z16
103 #include "depthtmp.h"
104
105
106
107
108 /* 16 bit float depthbuffer functions
109 */
110 #define WRITE_DEPTH( _x, _y, d ) \
111 *(GLushort *)(buf + ((_x)<<1) + (_y)*pitch) = \
112 savageEncodeFloat16( 1.0 - (GLfloat)d/65535.0 )
113
114 #define READ_DEPTH( d, _x, _y ) \
115 d = 65535 - \
116 savageDecodeFloat16( *(GLushort *)(buf + ((_x)<<1) + (_y)*pitch) ) * \
117 65535.0
118
119 #define TAG(x) savage##x##_z16f
120 #include "depthtmp.h"
121
122
123
124
125 /* 8-bit stencil /24-bit integer depth depthbuffer functions.
126 * Depth range is reversed. See also savageCalcViewport.
127 */
128 #define WRITE_DEPTH( _x, _y, d ) do { \
129 GLuint tmp = *(GLuint *)(buf + ((_x)<<2) + (_y)*pitch); \
130 tmp &= 0xFF000000; \
131 tmp |= 0x00FFFFFF - d; \
132 *(GLuint *)(buf + (_x<<2) + _y*pitch) = tmp; \
133 } while(0)
134
135 #define READ_DEPTH( d, _x, _y ) \
136 d = 0x00FFFFFF - (*(GLuint *)(buf + ((_x)<<2) + (_y)*pitch) & 0x00FFFFFF)
137
138 #define TAG(x) savage##x##_s8_z24
139 #include "depthtmp.h"
140
141
142
143
144 /* 24 bit float depthbuffer functions
145 */
146 #define WRITE_DEPTH( _x, _y, d ) do { \
147 GLuint tmp = *(GLuint *)(buf + ((_x)<<2) + (_y)*pitch); \
148 tmp &= 0xFF000000; \
149 tmp |= savageEncodeFloat24( 1.0 - (GLfloat)d/16777215.0 ); \
150 *(GLuint *)(buf + (_x<<2) + _y*pitch) = tmp; \
151 } while(0)
152
153 #define READ_DEPTH( d, _x, _y ) \
154 d = 16777215 - savageDecodeFloat24( \
155 *(GLuint *)(buf + ((_x)<<2) + (_y)*pitch) & 0x00FFFFFF) \
156 * 16777215.0
157
158 #define TAG(x) savage##x##_s8_z24f
159 #include "depthtmp.h"
160
161
162 #define WRITE_STENCIL( _x, _y, d ) do { \
163 GLuint tmp = *(GLuint *)(buf + ((_x)<<2) + (_y)*pitch); \
164 tmp &= 0x00FFFFFF; \
165 tmp |= (((GLuint)d)<<24) & 0xFF000000; \
166 *(GLuint *)(buf + ((_x)<<2) + (_y)*pitch) = tmp; \
167 } while(0)
168
169 #define READ_STENCIL( d, _x, _y ) \
170 d = (GLstencil)((*(GLuint *)(buf + ((_x)<<2) + (_y)*pitch) & 0xFF000000) >> 24)
171
172 #define TAG(x) savage##x##_s8_z24
173 #include "stenciltmp.h"
174
175
176
177 /*
178 * Wrappers around _swrast_Copy/Draw/ReadPixels that make sure all
179 * primitives are flushed and the hardware is idle before accessing
180 * the frame buffer.
181 */
182 static void
183 savageCopyPixels( GLcontext *ctx,
184 GLint srcx, GLint srcy, GLsizei width, GLsizei height,
185 GLint destx, GLint desty,
186 GLenum type )
187 {
188 savageContextPtr imesa = SAVAGE_CONTEXT(ctx);
189 FLUSH_BATCH(imesa);
190 WAIT_IDLE_EMPTY(imesa);
191 _swrast_CopyPixels(ctx, srcx, srcy, width, height, destx, desty, type);
192 }
193 static void
194 savageDrawPixels( GLcontext *ctx,
195 GLint x, GLint y,
196 GLsizei width, GLsizei height,
197 GLenum format, GLenum type,
198 const struct gl_pixelstore_attrib *packing,
199 const GLvoid *pixels )
200 {
201 savageContextPtr imesa = SAVAGE_CONTEXT(ctx);
202 FLUSH_BATCH(imesa);
203 WAIT_IDLE_EMPTY(imesa);
204 _swrast_DrawPixels(ctx, x, y, width, height, format, type, packing, pixels);
205 }
206 static void
207 savageReadPixels( GLcontext *ctx,
208 GLint x, GLint y, GLsizei width, GLsizei height,
209 GLenum format, GLenum type,
210 const struct gl_pixelstore_attrib *packing,
211 GLvoid *pixels )
212 {
213 savageContextPtr imesa = SAVAGE_CONTEXT(ctx);
214 FLUSH_BATCH(imesa);
215 WAIT_IDLE_EMPTY(imesa);
216 _swrast_ReadPixels(ctx, x, y, width, height, format, type, packing, pixels);
217 }
218
219 /*
220 * Make sure the hardware is idle when span-rendering.
221 */
222 static void savageSpanRenderStart( GLcontext *ctx )
223 {
224 savageContextPtr imesa = SAVAGE_CONTEXT(ctx);
225 FLUSH_BATCH(imesa);
226 WAIT_IDLE_EMPTY(imesa);
227 }
228
229
230 void savageDDInitSpanFuncs( GLcontext *ctx )
231 {
232 struct swrast_device_driver *swdd = _swrast_GetDeviceDriverReference(ctx);
233 swdd->SpanRenderStart = savageSpanRenderStart;
234
235 /* XXX these should probably be plugged in elsewhere */
236 ctx->Driver.CopyPixels = savageCopyPixels;
237 ctx->Driver.DrawPixels = savageDrawPixels;
238 ctx->Driver.ReadPixels = savageReadPixels;
239 }
240
241
242
243 /**
244 * Plug in the Get/Put routines for the given driRenderbuffer.
245 */
246 void
247 savageSetSpanFunctions(driRenderbuffer *drb, const GLvisual *vis,
248 GLboolean float_depth)
249 {
250 if (drb->Base.InternalFormat == GL_RGBA) {
251 if (vis->redBits == 5 && vis->greenBits == 6 && vis->blueBits == 5) {
252 savageInitPointers_565(&drb->Base);
253 }
254 else {
255 savageInitPointers_8888(&drb->Base);
256 }
257 }
258 else if (drb->Base.InternalFormat == GL_DEPTH_COMPONENT16) {
259 if (float_depth) {
260 savageInitDepthPointers_z16f(&drb->Base);
261 }
262 else {
263 savageInitDepthPointers_z16(&drb->Base);
264 }
265 }
266 else if (drb->Base.InternalFormat == GL_DEPTH_COMPONENT24) {
267 if (float_depth) {
268 savageInitDepthPointers_s8_z24f(&drb->Base);
269 }
270 else {
271 savageInitDepthPointers_s8_z24(&drb->Base);
272 }
273 }
274 else if (drb->Base.InternalFormat == GL_STENCIL_INDEX8_EXT) {
275 savageInitStencilPointers_s8_z24(&drb->Base);
276 }
277 }