Merge branch 'mesa_7_5_branch'
[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 "main/glheader.h"
36 #include "main/imports.h"
37 #include "main/colormac.h"
38 #include "swrast/swrast.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 #define VALUE_TYPE GLushort
176
177 #define WRITE_DEPTH( _x, _y, d ) \
178 *(GLushort *)(buf + r200_mba_z16( drb, _x + xo, _y + yo )) = d;
179
180 #define READ_DEPTH( d, _x, _y ) \
181 d = *(GLushort *)(buf + r200_mba_z16( drb, _x + xo, _y + yo ));
182
183 #define TAG(x) r200##x##_z16
184 #include "depthtmp.h"
185
186
187 /* 24 bit depth, 8 bit stencil depthbuffer functions
188 */
189 #define VALUE_TYPE GLuint
190
191 #define WRITE_DEPTH( _x, _y, d ) \
192 do { \
193 GLuint offset = r200_mba_z32( drb, _x + xo, _y + yo ); \
194 GLuint tmp = *(GLuint *)(buf + offset); \
195 tmp &= 0xff000000; \
196 tmp |= ((d) & 0x00ffffff); \
197 *(GLuint *)(buf + offset) = tmp; \
198 } while (0)
199
200 #define READ_DEPTH( d, _x, _y ) \
201 d = *(GLuint *)(buf + r200_mba_z32( drb, _x + xo, \
202 _y + yo )) & 0x00ffffff;
203
204 #define TAG(x) r200##x##_z24_s8
205 #include "depthtmp.h"
206
207
208 /* ================================================================
209 * Stencil buffer
210 */
211
212 /* 24 bit depth, 8 bit stencil depthbuffer functions
213 */
214 #define WRITE_STENCIL( _x, _y, d ) \
215 do { \
216 GLuint offset = r200_mba_z32( drb, _x + xo, _y + yo ); \
217 GLuint tmp = *(GLuint *)(buf + offset); \
218 tmp &= 0x00ffffff; \
219 tmp |= (((d) & 0xff) << 24); \
220 *(GLuint *)(buf + offset) = tmp; \
221 } while (0)
222
223 #define READ_STENCIL( d, _x, _y ) \
224 do { \
225 GLuint offset = r200_mba_z32( drb, _x + xo, _y + yo ); \
226 GLuint tmp = *(GLuint *)(buf + offset); \
227 tmp &= 0xff000000; \
228 d = tmp >> 24; \
229 } while (0)
230
231 #define TAG(x) r200##x##_z24_s8
232 #include "stenciltmp.h"
233
234
235 /* Move locking out to get reasonable span performance (10x better
236 * than doing this in HW_LOCK above). WaitForIdle() is the main
237 * culprit.
238 */
239
240 static void r200SpanRenderStart( GLcontext *ctx )
241 {
242 r200ContextPtr rmesa = R200_CONTEXT( ctx );
243
244 R200_FIREVERTICES( rmesa );
245 LOCK_HARDWARE( rmesa );
246 r200WaitForIdleLocked( rmesa );
247
248 /* Read & rewrite the first pixel in the frame buffer. This should
249 * be a noop, right? In fact without this conform fails as reading
250 * from the framebuffer sometimes produces old results -- the
251 * on-card read cache gets mixed up and doesn't notice that the
252 * framebuffer has been updated.
253 *
254 * In the worst case this is buggy too as p might get the wrong
255 * value first time, so really need a hidden pixel somewhere for this.
256 */
257 {
258 int p;
259 driRenderbuffer *drb =
260 (driRenderbuffer *) ctx->WinSysDrawBuffer->_ColorDrawBuffers[0];
261 volatile int *buf =
262 (volatile int *)(rmesa->dri.screen->pFB + drb->offset);
263 p = *buf;
264 *buf = p;
265 }
266 }
267
268 static void r200SpanRenderFinish( GLcontext *ctx )
269 {
270 r200ContextPtr rmesa = R200_CONTEXT( ctx );
271 _swrast_flush( ctx );
272 UNLOCK_HARDWARE( rmesa );
273 }
274
275 void r200InitSpanFuncs( GLcontext *ctx )
276 {
277 struct swrast_device_driver *swdd = _swrast_GetDeviceDriverReference(ctx);
278 swdd->SpanRenderStart = r200SpanRenderStart;
279 swdd->SpanRenderFinish = r200SpanRenderFinish;
280 }
281
282
283
284 /**
285 * Plug in the Get/Put routines for the given driRenderbuffer.
286 */
287 void
288 radeonSetSpanFunctions(driRenderbuffer *drb, const GLvisual *vis)
289 {
290 if (drb->Base.InternalFormat == GL_RGBA) {
291 if (vis->redBits == 5 && vis->greenBits == 6 && vis->blueBits == 5) {
292 r200InitPointers_RGB565(&drb->Base);
293 }
294 else {
295 r200InitPointers_ARGB8888(&drb->Base);
296 }
297 }
298 else if (drb->Base.InternalFormat == GL_DEPTH_COMPONENT16) {
299 r200InitDepthPointers_z16(&drb->Base);
300 }
301 else if (drb->Base.InternalFormat == GL_DEPTH_COMPONENT24) {
302 r200InitDepthPointers_z24_s8(&drb->Base);
303 }
304 else if (drb->Base.InternalFormat == GL_STENCIL_INDEX8_EXT) {
305 r200InitStencilPointers_z24_s8(&drb->Base);
306 }
307 }