Merge branch 'drm-gem'
[mesa.git] / src / mesa / drivers / dri / gamma / gamma_span.c
1 /* $XFree86: xc/lib/GL/mesa/src/drv/gamma/gamma_span.c,v 1.4 2002/11/05 17:46:07 tsi Exp $ */
2
3 #include "gamma_context.h"
4 #include "gamma_lock.h"
5 #include "colormac.h"
6
7 #include "swrast/swrast.h"
8
9 #define DBG 0
10
11 #define LOCAL_VARS \
12 gammaContextPtr gmesa = GAMMA_CONTEXT(ctx); \
13 gammaScreenPtr gammascrn = gmesa->gammaScreen; \
14 __DRIscreenPrivate *sPriv = gmesa->driScreen; \
15 __DRIdrawablePrivate *dPriv = gmesa->driDrawable; \
16 GLuint pitch = sPriv->fbWidth * gammascrn->cpp; \
17 GLuint height = dPriv->h; \
18 char *buf = (char *)(sPriv->pFB + \
19 gmesa->drawOffset + \
20 (dPriv->x * gammascrn->cpp) + \
21 (dPriv->y * pitch)); \
22 GLuint p; \
23 (void) buf; (void) p
24
25 /* FIXME! Depth/Stencil read/writes don't work ! */
26 #define LOCAL_DEPTH_VARS \
27 gammaScreenPtr gammascrn = gmesa->gammaScreen; \
28 __DRIdrawablePrivate *dPriv = gmesa->driDrawable; \
29 __DRIscreenPrivate *sPriv = gmesa->driScreen; \
30 GLuint pitch = gammascrn->depthPitch; \
31 GLuint height = dPriv->h; \
32 char *buf = (char *)(sPriv->pFB + \
33 gammascrn->depthOffset + \
34 dPriv->x * gammascrn->cpp + \
35 dPriv->y * pitch)
36
37 #define LOCAL_STENCIL_VARS LOCAL_DEPTH_VARS
38
39 #define Y_FLIP( _y ) (height - _y - 1)
40
41 #define HW_LOCK() \
42 gammaContextPtr gmesa = GAMMA_CONTEXT(ctx); \
43 FLUSH_DMA_BUFFER(gmesa); \
44 gammaGetLock( gmesa, DRM_LOCK_FLUSH | DRM_LOCK_QUIESCENT ); \
45 GAMMAHW_LOCK( gmesa );
46
47 #define HW_UNLOCK() GAMMAHW_UNLOCK( gmesa )
48
49
50
51 /* ================================================================
52 * Color buffer
53 */
54
55 /* 16 bit, RGB565 color spanline and pixel functions
56 */
57 #define INIT_MONO_PIXEL(p, color) \
58 p = PACK_COLOR_565( color[0], color[1], color[2] )
59
60 #define WRITE_RGBA( _x, _y, r, g, b, a ) \
61 *(GLushort *)(buf + _x*2 + _y*pitch) = ((((int)r & 0xf8) << 8) | \
62 (((int)g & 0xfc) << 3) | \
63 (((int)b & 0xf8) >> 3))
64
65 #define WRITE_PIXEL( _x, _y, p ) \
66 *(GLushort *)(buf + _x*2 + _y*pitch) = p
67
68 #define READ_RGBA( rgba, _x, _y ) \
69 do { \
70 GLushort p = *(GLushort *)(buf + _x*2 + _y*pitch); \
71 rgba[0] = (p >> 8) & 0xf8; \
72 rgba[1] = (p >> 3) & 0xfc; \
73 rgba[2] = (p << 3) & 0xf8; \
74 rgba[3] = 0xff; \
75 if ( rgba[0] & 0x08 ) rgba[0] |= 0x07; \
76 if ( rgba[1] & 0x04 ) rgba[1] |= 0x03; \
77 if ( rgba[2] & 0x08 ) rgba[2] |= 0x07; \
78 } while (0)
79
80 #define TAG(x) gamma##x##_RGB565
81 #include "spantmp.h"
82
83
84 /* 32 bit, ARGB8888 color spanline and pixel functions
85 */
86
87 #undef INIT_MONO_PIXEL
88 #define INIT_MONO_PIXEL(p, color) \
89 p = PACK_COLOR_8888( color[3], color[0], color[1], color[2] )
90
91 #define WRITE_RGBA( _x, _y, r, g, b, a ) \
92 *(GLuint *)(buf + _x*4 + _y*pitch) = ((b << 0) | \
93 (g << 8) | \
94 (r << 16) | \
95 (a << 24) )
96
97 #define WRITE_PIXEL( _x, _y, p ) \
98 *(GLuint *)(buf + _x*4 + _y*pitch) = p
99
100 #define READ_RGBA( rgba, _x, _y ) \
101 do { \
102 GLuint p = *(GLuint *)(buf + _x*4 + _y*pitch); \
103 rgba[0] = (p >> 16) & 0xff; \
104 rgba[1] = (p >> 8) & 0xff; \
105 rgba[2] = (p >> 0) & 0xff; \
106 rgba[3] = (p >> 24) & 0xff; \
107 } while (0)
108
109 #define TAG(x) gamma##x##_ARGB8888
110 #include "spantmp.h"
111
112
113 /* 16 bit depthbuffer functions.
114 */
115 #define VALUE_TYPE GLushort
116
117 #define WRITE_DEPTH( _x, _y, d ) \
118 *(GLushort *)(buf + (_x)*2 + (_y)*pitch) = d;
119
120 #define READ_DEPTH( d, _x, _y ) \
121 d = *(GLushort *)(buf + (_x)*2 + (_y)*pitch);
122
123 #define TAG(x) gamma##x##_16
124 #include "depthtmp.h"
125
126
127 #if 0 /* Unused */
128 /* 32 bit depthbuffer functions.
129 */
130 #define VALUE_TYPE GLuint
131
132 #define WRITE_DEPTH( _x, _y, d ) \
133 *(GLuint *)(buf + (_x)*4 + (_y)*pitch) = d;
134
135 #define READ_DEPTH( d, _x, _y ) \
136 d = *(GLuint *)(buf + (_x)*4 + (_y)*pitch);
137
138 #define TAG(x) gamma##x##_32
139 #include "depthtmp.h"
140 #endif
141
142
143 /* 24/8 bit interleaved depth/stencil functions
144 */
145 #define VALUE_TYPE GLuint
146
147 #define WRITE_DEPTH( _x, _y, d ) { \
148 GLuint tmp = *(GLuint *)(buf + (_x)*4 + (_y)*pitch); \
149 tmp &= 0xff; \
150 tmp |= (d) & 0xffffff00; \
151 *(GLuint *)(buf + (_x)*4 + (_y)*pitch) = tmp; \
152 }
153
154 #define READ_DEPTH( d, _x, _y ) \
155 d = *(GLuint *)(buf + (_x)*4 + (_y)*pitch) & ~0xff;
156
157
158 #define TAG(x) gamma##x##_24_8
159 #include "depthtmp.h"
160
161 #if 0
162 #define WRITE_STENCIL( _x, _y, d ) { \
163 GLuint tmp = *(GLuint *)(buf + _x*4 + _y*pitch); \
164 tmp &= 0xffffff00; \
165 tmp |= d & 0xff; \
166 *(GLuint *)(buf + _x*4 + _y*pitch) = tmp; \
167 }
168
169 #define READ_STENCIL( d, _x, _y ) \
170 d = *(GLuint *)(buf + _x*4 + _y*pitch) & 0xff;
171
172 #define TAG(x) gamma##x##_24_8
173 #include "stenciltmp.h"
174
175 static void gammaReadRGBASpan8888( const GLcontext *ctx,
176 GLuint n, GLint x, GLint y,
177 GLubyte rgba[][4])
178 {
179 gammaContextPtr gmesa = GAMMA_CONTEXT(ctx);
180 gammaScreenPtr gammascrn = gmesa->gammaScreen;
181 u_int32_t dwords1, dwords2, i = 0;
182 char *src = (char *)rgba[0];
183 GLuint read = n * gammascrn->cpp; /* Number of bytes we are expecting */
184 u_int32_t data;
185
186 FLUSH_DMA_BUFFER(gmesa);
187 CHECK_DMA_BUFFER(gmesa, 16);
188 WRITE(gmesa->buf, LBReadMode, gmesa->LBReadMode & ~(LBReadSrcEnable | LBReadDstEnable));
189 WRITE(gmesa->buf, ColorDDAMode, ColorDDAEnable);
190 WRITE(gmesa->buf, LBWriteMode, LBWriteModeDisable);
191 WRITE(gmesa->buf, FBReadMode, (gmesa->FBReadMode & ~FBReadSrcEnable) | FBReadDstEnable | FBDataTypeColor);
192 WRITE(gmesa->buf, FilterMode, 0x200); /* Pass FBColorData */
193 WRITE(gmesa->buf, FBWriteMode, FBW_UploadColorData | FBWriteModeDisable);
194 WRITE(gmesa->buf, StartXSub, (x+n)<<16);
195 WRITE(gmesa->buf, StartXDom, x<<16);
196 WRITE(gmesa->buf, StartY, y<<16);
197 WRITE(gmesa->buf, GLINTCount, 1);
198 WRITE(gmesa->buf, dXDom, 0<<16);
199 WRITE(gmesa->buf, dXSub, 0<<16);
200 WRITE(gmesa->buf, dY, 1<<16);
201 WRITE(gmesa->buf, Render, PrimitiveTrapezoid);
202 FLUSH_DMA_BUFFER(gmesa);
203
204 moredata:
205
206 dwords1 = *(volatile u_int32_t*)(void *)(((u_int8_t*)gammascrn->regions[0].map) + (GlintOutFIFOWords));
207 dwords2 = *(volatile u_int32_t*)(void *)(((u_int8_t*)gammascrn->regions[2].map) + (GlintOutFIFOWords));
208
209 if (dwords1) {
210 memcpy(src, (char*)gammascrn->regions[1].map + 0x1000, dwords1 << 2);
211 src += dwords1 << 2;
212 read -= dwords1 << 2;
213 }
214 if (dwords2) {
215 memcpy(src, (char*)gammascrn->regions[3].map + 0x1000, dwords2 << 2);
216 src += dwords2 << 2;
217 read -= dwords2 << 2;
218 }
219
220 if (read)
221 goto moredata;
222
223 done:
224
225 CHECK_DMA_BUFFER(gmesa, 6);
226 WRITE(gmesa->buf, ColorDDAMode, gmesa->ColorDDAMode);
227 WRITE(gmesa->buf, LBWriteMode, LBWriteModeEnable);
228 WRITE(gmesa->buf, LBReadMode, gmesa->LBReadMode);
229 WRITE(gmesa->buf, FBReadMode, gmesa->FBReadMode);
230 WRITE(gmesa->buf, FBWriteMode, FBWriteModeEnable);
231 WRITE(gmesa->buf, FilterMode, 0x400);
232 }
233 #endif
234
235 static void gammaSetBuffer( GLcontext *ctx,
236 GLframebuffer *colorBuffer,
237 GLuint bufferBit )
238 {
239 gammaContextPtr gmesa = GAMMA_CONTEXT(ctx);
240
241 switch ( bufferBit ) {
242 case BUFFER_BIT_FRONT_LEFT:
243 gmesa->readOffset = 0;
244 break;
245 case BUFFER_BIT_BACK_LEFT:
246 gmesa->readOffset = gmesa->driScreen->fbHeight * gmesa->driScreen->fbWidth * gmesa->gammaScreen->cpp;
247 break;
248 default:
249 _mesa_problem(ctx, "Unexpected buffer 0x%x in gammaSetBuffer()", bufferBit);
250 }
251 }
252
253
254 void gammaDDInitSpanFuncs( GLcontext *ctx )
255 {
256 gammaContextPtr gmesa = GAMMA_CONTEXT(ctx);
257 struct swrast_device_driver *swdd = _swrast_GetDeviceDriverReference(ctx);
258
259 swdd->SetBuffer = gammaSetBuffer;
260
261 switch ( gmesa->gammaScreen->cpp ) {
262 case 2:
263 swdd->WriteRGBASpan = gammaWriteRGBASpan_RGB565;
264 swdd->WriteRGBSpan = gammaWriteRGBSpan_RGB565;
265 swdd->WriteMonoRGBASpan = gammaWriteMonoRGBASpan_RGB565;
266 swdd->WriteRGBAPixels = gammaWriteRGBAPixels_RGB565;
267 swdd->WriteMonoRGBAPixels = gammaWriteMonoRGBAPixels_RGB565;
268 swdd->ReadRGBASpan = gammaReadRGBASpan_RGB565;
269 swdd->ReadRGBAPixels = gammaReadRGBAPixels_RGB565;
270 break;
271
272 case 4:
273 swdd->WriteRGBASpan = gammaWriteRGBASpan_ARGB8888;
274 swdd->WriteRGBSpan = gammaWriteRGBSpan_ARGB8888;
275 swdd->WriteMonoRGBASpan = gammaWriteMonoRGBASpan_ARGB8888;
276 swdd->WriteRGBAPixels = gammaWriteRGBAPixels_ARGB8888;
277 swdd->WriteMonoRGBAPixels = gammaWriteMonoRGBAPixels_ARGB8888;
278 #if 1
279 swdd->ReadRGBASpan = gammaReadRGBASpan_ARGB8888;
280 #else
281 swdd->ReadRGBASpan = gammaReadRGBASpan8888;
282 #endif
283 swdd->ReadRGBAPixels = gammaReadRGBAPixels_ARGB8888;
284 break;
285
286 default:
287 break;
288 }
289
290 switch ( gmesa->glCtx->Visual.depthBits ) {
291 case 16:
292 swdd->ReadDepthSpan = gammaReadDepthSpan_16;
293 swdd->WriteDepthSpan = gammaWriteDepthSpan_16;
294 swdd->ReadDepthPixels = gammaReadDepthPixels_16;
295 swdd->WriteDepthPixels = gammaWriteDepthPixels_16;
296 break;
297
298 case 24:
299 swdd->ReadDepthSpan = gammaReadDepthSpan_24_8;
300 swdd->WriteDepthSpan = gammaWriteDepthSpan_24_8;
301 swdd->ReadDepthPixels = gammaReadDepthPixels_24_8;
302 swdd->WriteDepthPixels = gammaWriteDepthPixels_24_8;
303
304 #if 0
305 swdd->ReadStencilSpan = gammaReadStencilSpan_24_8;
306 swdd->WriteStencilSpan = gammaWriteStencilSpan_24_8;
307 swdd->ReadStencilPixels = gammaReadStencilPixels_24_8;
308 swdd->WriteStencilPixels = gammaWriteStencilPixels_24_8;
309 #endif
310 break;
311
312 default:
313 break;
314 }
315 }