remove CVS/XFree86 keywords
[mesa.git] / src / mesa / drivers / dri / r200 / r200_span.c
1 /*
2 Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
3
4 The Weather Channel (TM) funded Tungsten Graphics to develop the
5 initial release of the Radeon 8500 driver under the XFree86 license.
6 This notice must be preserved.
7
8 Permission is hereby granted, free of charge, to any person obtaining
9 a copy of this software and associated documentation files (the
10 "Software"), to deal in the Software without restriction, including
11 without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense, and/or sell copies of the Software, and to
13 permit persons to whom the Software is furnished to do so, subject to
14 the following conditions:
15
16 The above copyright notice and this permission notice (including the
17 next paragraph) shall be included in all copies or substantial
18 portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
24 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
28 **************************************************************************/
29
30 /*
31 * Authors:
32 * Keith Whitwell <keith@tungstengraphics.com>
33 */
34
35 #include "glheader.h"
36 #include "imports.h"
37 #include "swrast/swrast.h"
38 #include "colormac.h"
39
40 #include "r200_context.h"
41 #include "r200_ioctl.h"
42 #include "r200_state.h"
43 #include "r200_span.h"
44 #include "r200_tex.h"
45
46 #define DBG 0
47
48 /*
49 * Note that all information needed to access pixels in a renderbuffer
50 * should be obtained through the gl_renderbuffer parameter, not per-context
51 * information.
52 */
53 #define LOCAL_VARS \
54 driRenderbuffer *drb = (driRenderbuffer *) rb; \
55 const __DRIdrawablePrivate *dPriv = drb->dPriv; \
56 const GLuint bottom = dPriv->h - 1; \
57 GLubyte *buf = (GLubyte *) drb->flippedData \
58 + (dPriv->y * drb->flippedPitch + dPriv->x) * drb->cpp; \
59 GLuint p; \
60 (void) p;
61
62 #define LOCAL_DEPTH_VARS \
63 driRenderbuffer *drb = (driRenderbuffer *) rb; \
64 const __DRIdrawablePrivate *dPriv = drb->dPriv; \
65 const GLuint bottom = dPriv->h - 1; \
66 GLuint xo = dPriv->x; \
67 GLuint yo = dPriv->y; \
68 GLubyte *buf = (GLubyte *) drb->Base.Data;
69
70 #define LOCAL_STENCIL_VARS LOCAL_DEPTH_VARS
71
72 #define Y_FLIP(Y) (bottom - (Y))
73
74 #define HW_LOCK()
75
76 #define HW_UNLOCK()
77
78
79
80 /* ================================================================
81 * Color buffer
82 */
83
84 /* 16 bit, RGB565 color spanline and pixel functions
85 */
86 #define SPANTMP_PIXEL_FMT GL_RGB
87 #define SPANTMP_PIXEL_TYPE GL_UNSIGNED_SHORT_5_6_5
88
89 #define TAG(x) r200##x##_RGB565
90 #define TAG2(x,y) r200##x##_RGB565##y
91 #define GET_PTR(X,Y) (buf + ((Y) * drb->flippedPitch + (X)) * 2)
92 #include "spantmp2.h"
93
94 /* 32 bit, ARGB8888 color spanline and pixel functions
95 */
96 #define SPANTMP_PIXEL_FMT GL_BGRA
97 #define SPANTMP_PIXEL_TYPE GL_UNSIGNED_INT_8_8_8_8_REV
98
99 #define TAG(x) r200##x##_ARGB8888
100 #define TAG2(x,y) r200##x##_ARGB8888##y
101 #define GET_PTR(X,Y) (buf + ((Y) * drb->flippedPitch + (X)) * 4)
102 #include "spantmp2.h"
103
104
105 /* ================================================================
106 * Depth buffer
107 */
108
109 /* The Radeon family has depth tiling on all the time, so we have to convert
110 * the x,y coordinates into the memory bus address (mba) in the same
111 * manner as the engine. In each case, the linear block address (ba)
112 * is calculated, and then wired with x and y to produce the final
113 * memory address.
114 * The chip will do address translation on its own if the surface registers
115 * are set up correctly. It is not quite enough to get it working with hyperz too...
116 */
117
118 /* extract bit 'b' of x, result is zero or one */
119 #define BIT(x,b) ((x & (1<<b))>>b)
120
121 static GLuint
122 r200_mba_z32( driRenderbuffer *drb, GLint x, GLint y )
123 {
124 GLuint pitch = drb->pitch;
125 if (drb->depthHasSurface) {
126 return 4 * (x + y * pitch);
127 }
128 else {
129 GLuint b = ((y & 0x7FF) >> 4) * ((pitch & 0xFFF) >> 5) + ((x & 0x7FF) >> 5);
130 GLuint a =
131 (BIT(x,0) << 2) |
132 (BIT(y,0) << 3) |
133 (BIT(x,1) << 4) |
134 (BIT(y,1) << 5) |
135 (BIT(x,3) << 6) |
136 (BIT(x,4) << 7) |
137 (BIT(x,2) << 8) |
138 (BIT(y,2) << 9) |
139 (BIT(y,3) << 10) |
140 (((pitch & 0x20) ? (b & 0x01) : ((b & 0x01) ^ (BIT(y,4)))) << 11) |
141 ((b >> 1) << 12);
142 return a;
143 }
144 }
145
146 static GLuint
147 r200_mba_z16( driRenderbuffer *drb, GLint x, GLint y )
148 {
149 GLuint pitch = drb->pitch;
150 if (drb->depthHasSurface) {
151 return 2 * (x + y * pitch);
152 }
153 else {
154 GLuint b = ((y & 0x7FF) >> 4) * ((pitch & 0xFFF) >> 6) + ((x & 0x7FF) >> 6);
155 GLuint a =
156 (BIT(x,0) << 1) |
157 (BIT(y,0) << 2) |
158 (BIT(x,1) << 3) |
159 (BIT(y,1) << 4) |
160 (BIT(x,2) << 5) |
161 (BIT(x,4) << 6) |
162 (BIT(x,5) << 7) |
163 (BIT(x,3) << 8) |
164 (BIT(y,2) << 9) |
165 (BIT(y,3) << 10) |
166 (((pitch & 0x40) ? (b & 0x01) : ((b & 0x01) ^ (BIT(y,4)))) << 11) |
167 ((b >> 1) << 12);
168 return a;
169 }
170 }
171
172
173 /* 16-bit depth buffer functions
174 */
175
176 #define WRITE_DEPTH( _x, _y, d ) \
177 *(GLushort *)(buf + r200_mba_z16( drb, _x + xo, _y + yo )) = d;
178
179 #define READ_DEPTH( d, _x, _y ) \
180 d = *(GLushort *)(buf + r200_mba_z16( drb, _x + xo, _y + yo ));
181
182 #define TAG(x) r200##x##_z16
183 #include "depthtmp.h"
184
185
186 /* 24 bit depth, 8 bit stencil depthbuffer functions
187 */
188
189 #define WRITE_DEPTH( _x, _y, d ) \
190 do { \
191 GLuint offset = r200_mba_z32( drb, _x + xo, _y + yo ); \
192 GLuint tmp = *(GLuint *)(buf + offset); \
193 tmp &= 0xff000000; \
194 tmp |= ((d) & 0x00ffffff); \
195 *(GLuint *)(buf + offset) = tmp; \
196 } while (0)
197
198 #define READ_DEPTH( d, _x, _y ) \
199 d = *(GLuint *)(buf + r200_mba_z32( drb, _x + xo, \
200 _y + yo )) & 0x00ffffff;
201
202 #define TAG(x) r200##x##_z24_s8
203 #include "depthtmp.h"
204
205
206 /* ================================================================
207 * Stencil buffer
208 */
209
210 /* 24 bit depth, 8 bit stencil depthbuffer functions
211 */
212 #define WRITE_STENCIL( _x, _y, d ) \
213 do { \
214 GLuint offset = r200_mba_z32( drb, _x + xo, _y + yo ); \
215 GLuint tmp = *(GLuint *)(buf + offset); \
216 tmp &= 0x00ffffff; \
217 tmp |= (((d) & 0xff) << 24); \
218 *(GLuint *)(buf + offset) = tmp; \
219 } while (0)
220
221 #define READ_STENCIL( d, _x, _y ) \
222 do { \
223 GLuint offset = r200_mba_z32( drb, _x + xo, _y + yo ); \
224 GLuint tmp = *(GLuint *)(buf + offset); \
225 tmp &= 0xff000000; \
226 d = tmp >> 24; \
227 } while (0)
228
229 #define TAG(x) r200##x##_z24_s8
230 #include "stenciltmp.h"
231
232
233 /* Move locking out to get reasonable span performance (10x better
234 * than doing this in HW_LOCK above). WaitForIdle() is the main
235 * culprit.
236 */
237
238 static void r200SpanRenderStart( GLcontext *ctx )
239 {
240 r200ContextPtr rmesa = R200_CONTEXT( ctx );
241
242 R200_FIREVERTICES( rmesa );
243 LOCK_HARDWARE( rmesa );
244 r200WaitForIdleLocked( rmesa );
245
246 /* Read & rewrite the first pixel in the frame buffer. This should
247 * be a noop, right? In fact without this conform fails as reading
248 * from the framebuffer sometimes produces old results -- the
249 * on-card read cache gets mixed up and doesn't notice that the
250 * framebuffer has been updated.
251 *
252 * In the worst case this is buggy too as p might get the wrong
253 * value first time, so really need a hidden pixel somewhere for this.
254 */
255 {
256 int p;
257 driRenderbuffer *drb =
258 (driRenderbuffer *) ctx->WinSysDrawBuffer->_ColorDrawBuffers[0][0];
259 volatile int *buf =
260 (volatile int *)(rmesa->dri.screen->pFB + drb->offset);
261 p = *buf;
262 *buf = p;
263 }
264 }
265
266 static void r200SpanRenderFinish( GLcontext *ctx )
267 {
268 r200ContextPtr rmesa = R200_CONTEXT( ctx );
269 _swrast_flush( ctx );
270 UNLOCK_HARDWARE( rmesa );
271 }
272
273 void r200InitSpanFuncs( GLcontext *ctx )
274 {
275 struct swrast_device_driver *swdd = _swrast_GetDeviceDriverReference(ctx);
276 swdd->SpanRenderStart = r200SpanRenderStart;
277 swdd->SpanRenderFinish = r200SpanRenderFinish;
278 }
279
280
281
282 /**
283 * Plug in the Get/Put routines for the given driRenderbuffer.
284 */
285 void
286 radeonSetSpanFunctions(driRenderbuffer *drb, const GLvisual *vis)
287 {
288 if (drb->Base.InternalFormat == GL_RGBA) {
289 if (vis->redBits == 5 && vis->greenBits == 6 && vis->blueBits == 5) {
290 r200InitPointers_RGB565(&drb->Base);
291 }
292 else {
293 r200InitPointers_ARGB8888(&drb->Base);
294 }
295 }
296 else if (drb->Base.InternalFormat == GL_DEPTH_COMPONENT16) {
297 r200InitDepthPointers_z16(&drb->Base);
298 }
299 else if (drb->Base.InternalFormat == GL_DEPTH_COMPONENT24) {
300 r200InitDepthPointers_z24_s8(&drb->Base);
301 }
302 else if (drb->Base.InternalFormat == GL_STENCIL_INDEX8_EXT) {
303 r200InitStencilPointers_z24_s8(&drb->Base);
304 }
305 }