Add support for optimized versions of the code underlying ReadPixels
[mesa.git] / src / mesa / drivers / dri / common / spantmp2.h
1 /*
2 * Copyright 2000-2001 VA Linux Systems, Inc.
3 * (C) Copyright IBM Corporation 2004
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * on the rights to use, copy, modify, merge, publish, distribute, sub
10 * license, and/or sell copies of the Software, and to permit persons to whom
11 * the Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
20 * VA LINUX SYSTEM, IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
23 * USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 /**
27 * \file spantmp2.h
28 *
29 * Template file of span read / write functions.
30 *
31 * \author Keith Whitwell <keithw@tungstengraphics.com>
32 * \author Gareth Hughes <gareth@nvidia.com>
33 * \author Ian Romanick <idr@us.ibm.com>
34 */
35
36 #include "colormac.h"
37
38 #ifndef DBG
39 #define DBG 0
40 #endif
41
42 #ifndef HW_WRITE_LOCK
43 #define HW_WRITE_LOCK() HW_LOCK()
44 #endif
45
46 #ifndef HW_WRITE_UNLOCK
47 #define HW_WRITE_UNLOCK() HW_UNLOCK()
48 #endif
49
50 #ifndef HW_READ_LOCK
51 #define HW_READ_LOCK() HW_LOCK()
52 #endif
53
54 #ifndef HW_READ_UNLOCK
55 #define HW_READ_UNLOCK() HW_UNLOCK()
56 #endif
57
58 #ifndef HW_READ_CLIPLOOP
59 #define HW_READ_CLIPLOOP() HW_CLIPLOOP()
60 #endif
61
62 #ifndef HW_WRITE_CLIPLOOP
63 #define HW_WRITE_CLIPLOOP() HW_CLIPLOOP()
64 #endif
65
66 #if (SPANTMP_PIXEL_FMT == GL_RGB) && (SPANTMP_PIXEL_TYPE == GL_UNSIGNED_SHORT_5_6_5)
67
68 #define INIT_MONO_PIXEL(p, color) \
69 p = PACK_COLOR_565( color[0], color[1], color[2] )
70
71 #define WRITE_RGBA( _x, _y, r, g, b, a ) \
72 do { \
73 GLshort * _p = (GLshort *) GET_DST_PTR(_x, _y); \
74 _p[0] = ((((int)r & 0xf8) << 8) | (((int)g & 0xfc) << 3) | \
75 (((int)b & 0xf8) >> 3)); \
76 } while(0)
77
78 #define WRITE_PIXEL( _x, _y, p ) \
79 do { \
80 GLushort * _p = (GLushort *) GET_DST_PTR(_x, _y); \
81 _p[0] = p; \
82 } while(0)
83
84 #define READ_RGBA( rgba, _x, _y ) \
85 do { \
86 GLushort p = *(volatile GLshort *) GET_SRC_PTR(_x, _y); \
87 rgba[0] = ((p >> 8) & 0xf8) * 255 / 0xf8; \
88 rgba[1] = ((p >> 3) & 0xfc) * 255 / 0xfc; \
89 rgba[2] = ((p << 3) & 0xf8) * 255 / 0xf8; \
90 rgba[3] = 0xff; \
91 } while (0)
92
93 #elif (SPANTMP_PIXEL_FMT == GL_BGRA) && (SPANTMP_PIXEL_TYPE == GL_UNSIGNED_INT_8_8_8_8_REV)
94
95 # define INIT_MONO_PIXEL(p, color) \
96 p = PACK_COLOR_8888(color[3], color[0], color[1], color[2])
97
98 # define WRITE_RGBA(_x, _y, r, g, b, a) \
99 do { \
100 GLuint * _p = (GLuint *) GET_DST_PTR(_x, _y); \
101 _p[0] = ((r << 16) | (g << 8) | (b << 0) | (a << 24)); \
102 } while(0)
103
104 #define WRITE_PIXEL(_x, _y, p) \
105 do { \
106 GLuint * _p = (GLuint *) GET_DST_PTR(_x, _y); \
107 _p[0] = p; \
108 } while(0)
109
110 # if defined( USE_X86_ASM )
111 # define READ_RGBA(rgba, _x, _y) \
112 do { \
113 GLuint p = *(volatile GLuint *) GET_SRC_PTR(_x, _y); \
114 __asm__ __volatile__( "bswap %0; rorl $8, %0" \
115 : "=r" (p) : "r" (p) ); \
116 ((GLuint *)rgba)[0] = p; \
117 } while (0)
118 # else
119 # define READ_RGBA( rgba, _x, _y ) \
120 do { \
121 GLuint p = *(volatile GLuint *) GET_SRC_PTR(_x, _y); \
122 rgba[0] = (p >> 16) & 0xff; \
123 rgba[1] = (p >> 8) & 0xff; \
124 rgba[2] = (p >> 0) & 0xff; \
125 rgba[3] = (p >> 24) & 0xff; \
126 } while (0)
127 # endif
128
129 #else
130 #error SPANTMP_PIXEL_FMT must be set to a valid value!
131 #endif
132
133 #if defined( USE_MMX_ASM ) || defined( USE_SSE_ASM )
134 #include "x86/read_rgba_span_x86.h"
135 #include "x86/common_x86_asm.h"
136 #endif
137
138 static void TAG(WriteRGBASpan)( const GLcontext *ctx,
139 GLuint n, GLint x, GLint y,
140 const GLubyte rgba[][4],
141 const GLubyte mask[] )
142 {
143 HW_WRITE_LOCK()
144 {
145 GLint x1;
146 GLint n1;
147 LOCAL_VARS;
148
149 y = Y_FLIP(y);
150
151 HW_WRITE_CLIPLOOP()
152 {
153 GLint i = 0;
154 CLIPSPAN(x,y,n,x1,n1,i);
155
156 if (DBG) fprintf(stderr, "WriteRGBASpan %d..%d (x1 %d)\n",
157 (int)i, (int)n1, (int)x1);
158
159 if (mask)
160 {
161 for (;n1>0;i++,x1++,n1--)
162 if (mask[i])
163 WRITE_RGBA( x1, y,
164 rgba[i][0], rgba[i][1],
165 rgba[i][2], rgba[i][3] );
166 }
167 else
168 {
169 for (;n1>0;i++,x1++,n1--)
170 WRITE_RGBA( x1, y,
171 rgba[i][0], rgba[i][1],
172 rgba[i][2], rgba[i][3] );
173 }
174 }
175 HW_ENDCLIPLOOP();
176 }
177 HW_WRITE_UNLOCK();
178 }
179
180 static void TAG(WriteRGBSpan)( const GLcontext *ctx,
181 GLuint n, GLint x, GLint y,
182 const GLubyte rgb[][3],
183 const GLubyte mask[] )
184 {
185 HW_WRITE_LOCK()
186 {
187 GLint x1;
188 GLint n1;
189 LOCAL_VARS;
190
191 y = Y_FLIP(y);
192
193 HW_WRITE_CLIPLOOP()
194 {
195 GLint i = 0;
196 CLIPSPAN(x,y,n,x1,n1,i);
197
198 if (DBG) fprintf(stderr, "WriteRGBSpan %d..%d (x1 %d)\n",
199 (int)i, (int)n1, (int)x1);
200
201 if (mask)
202 {
203 for (;n1>0;i++,x1++,n1--)
204 if (mask[i])
205 WRITE_RGBA( x1, y, rgb[i][0], rgb[i][1], rgb[i][2], 255 );
206 }
207 else
208 {
209 for (;n1>0;i++,x1++,n1--)
210 WRITE_RGBA( x1, y, rgb[i][0], rgb[i][1], rgb[i][2], 255 );
211 }
212 }
213 HW_ENDCLIPLOOP();
214 }
215 HW_WRITE_UNLOCK();
216 }
217
218 static void TAG(WriteRGBAPixels)( const GLcontext *ctx,
219 GLuint n,
220 const GLint x[],
221 const GLint y[],
222 const GLubyte rgba[][4],
223 const GLubyte mask[] )
224 {
225 HW_WRITE_LOCK()
226 {
227 GLint i;
228 LOCAL_VARS;
229
230 if (DBG) fprintf(stderr, "WriteRGBAPixels\n");
231
232 HW_WRITE_CLIPLOOP()
233 {
234 if (mask)
235 {
236 for (i=0;i<n;i++)
237 {
238 if (mask[i]) {
239 const int fy = Y_FLIP(y[i]);
240 if (CLIPPIXEL(x[i],fy))
241 WRITE_RGBA( x[i], fy,
242 rgba[i][0], rgba[i][1],
243 rgba[i][2], rgba[i][3] );
244 }
245 }
246 }
247 else
248 {
249 for (i=0;i<n;i++)
250 {
251 const int fy = Y_FLIP(y[i]);
252 if (CLIPPIXEL(x[i],fy))
253 WRITE_RGBA( x[i], fy,
254 rgba[i][0], rgba[i][1],
255 rgba[i][2], rgba[i][3] );
256 }
257 }
258 }
259 HW_ENDCLIPLOOP();
260 }
261 HW_WRITE_UNLOCK();
262 }
263
264
265 static void TAG(WriteMonoRGBASpan)( const GLcontext *ctx,
266 GLuint n, GLint x, GLint y,
267 const GLchan color[4],
268 const GLubyte mask[] )
269 {
270 HW_WRITE_LOCK()
271 {
272 GLint x1;
273 GLint n1;
274 LOCAL_VARS;
275 INIT_MONO_PIXEL(p, color);
276
277 y = Y_FLIP( y );
278
279 if (DBG) fprintf(stderr, "WriteMonoRGBASpan\n");
280
281 HW_WRITE_CLIPLOOP()
282 {
283 GLint i = 0;
284 CLIPSPAN(x,y,n,x1,n1,i);
285 if (mask)
286 {
287 for (;n1>0;i++,x1++,n1--)
288 if (mask[i])
289 WRITE_PIXEL( x1, y, p );
290 }
291 else
292 {
293 for (;n1>0;i++,x1++,n1--)
294 WRITE_PIXEL( x1, y, p );
295 }
296 }
297 HW_ENDCLIPLOOP();
298 }
299 HW_WRITE_UNLOCK();
300 }
301
302
303 static void TAG(WriteMonoRGBAPixels)( const GLcontext *ctx,
304 GLuint n,
305 const GLint x[], const GLint y[],
306 const GLchan color[],
307 const GLubyte mask[] )
308 {
309 HW_WRITE_LOCK()
310 {
311 GLint i;
312 LOCAL_VARS;
313 INIT_MONO_PIXEL(p, color);
314
315 if (DBG) fprintf(stderr, "WriteMonoRGBAPixels\n");
316
317 HW_WRITE_CLIPLOOP()
318 {
319 if (mask)
320 {
321 for (i=0;i<n;i++)
322 if (mask[i]) {
323 int fy = Y_FLIP(y[i]);
324 if (CLIPPIXEL( x[i], fy ))
325 WRITE_PIXEL( x[i], fy, p );
326 }
327 }
328 else
329 {
330 for (i=0;i<n;i++) {
331 int fy = Y_FLIP(y[i]);
332 if (CLIPPIXEL( x[i], fy ))
333 WRITE_PIXEL( x[i], fy, p );
334 }
335 }
336 }
337 HW_ENDCLIPLOOP();
338 }
339 HW_WRITE_UNLOCK();
340 }
341
342
343 static void TAG(ReadRGBASpan)( const GLcontext *ctx,
344 GLuint n, GLint x, GLint y,
345 GLubyte rgba[][4])
346 {
347 HW_READ_LOCK()
348 {
349 GLint x1,n1;
350 LOCAL_VARS;
351
352 y = Y_FLIP(y);
353
354 if (DBG) fprintf(stderr, "ReadRGBASpan\n");
355
356 HW_READ_CLIPLOOP()
357 {
358 GLint i = 0;
359 CLIPSPAN(x,y,n,x1,n1,i);
360 for (;n1>0;i++,x1++,n1--)
361 READ_RGBA( rgba[i], x1, y );
362 }
363 HW_ENDCLIPLOOP();
364 }
365 HW_READ_UNLOCK();
366 }
367
368
369 #if defined(USE_MMX_ASM)
370 static void TAG2(ReadRGBASpan,_MMX)( const GLcontext *ctx,
371 GLuint n, GLint x, GLint y,
372 GLubyte rgba[][4])
373 {
374 #ifndef USE_INNER_EMMS
375 /* The EMMS instruction is directly in-lined here because using GCC's
376 * built-in _mm_empty function was found to utterly destroy performance.
377 */
378 __asm__ __volatile__( "emms" );
379 #endif
380
381 HW_LOCK()
382 {
383 GLint x1,n1;
384 LOCAL_VARS;
385
386 y = Y_FLIP(y);
387
388 if (DBG) fprintf(stderr, "ReadRGBASpan\n");
389
390 HW_READ_CLIPLOOP()
391 {
392 GLint i = 0;
393 CLIPSPAN(x,y,n,x1,n1,i);
394
395 {
396 const char * src = (read_buf + x1*4 + y*pitch);
397 _generic_read_RGBA_span_BGRA8888_REV_MMX( src, rgba[i], n1 );
398 }
399 }
400 HW_ENDCLIPLOOP();
401 }
402 HW_UNLOCK();
403 #ifndef USE_INNER_EMMS
404 __asm__ __volatile__( "emms" );
405 #endif
406 }
407 #endif
408
409
410 #if defined(USE_SSE_ASM)
411 static void TAG2(ReadRGBASpan,_SSE2)( const GLcontext *ctx,
412 GLuint n, GLint x, GLint y,
413 GLubyte rgba[][4])
414 {
415 HW_LOCK()
416 {
417 GLint x1,n1;
418 LOCAL_VARS;
419
420 y = Y_FLIP(y);
421
422 if (DBG) fprintf(stderr, "ReadRGBASpan\n");
423
424 HW_READ_CLIPLOOP()
425 {
426 GLint i = 0;
427 CLIPSPAN(x,y,n,x1,n1,i);
428
429 {
430 const char * src = (read_buf + x1*4 + y*pitch);
431 _generic_read_RGBA_span_BGRA8888_REV_SSE2( src, rgba[i], n1 );
432 }
433 }
434 HW_ENDCLIPLOOP();
435 }
436 HW_UNLOCK();
437 }
438 #endif
439
440 #if defined(USE_SSE_ASM)
441 static void TAG2(ReadRGBASpan,_SSE)( const GLcontext *ctx,
442 GLuint n, GLint x, GLint y,
443 GLubyte rgba[][4])
444 {
445 #ifndef USE_INNER_EMMS
446 /* The EMMS instruction is directly in-lined here because using GCC's
447 * built-in _mm_empty function was found to utterly destroy performance.
448 */
449 __asm__ __volatile__( "emms" );
450 #endif
451
452 HW_LOCK()
453 {
454 GLint x1,n1;
455 LOCAL_VARS;
456
457 y = Y_FLIP(y);
458
459 if (DBG) fprintf(stderr, "ReadRGBASpan\n");
460
461 HW_READ_CLIPLOOP()
462 {
463 GLint i = 0;
464 CLIPSPAN(x,y,n,x1,n1,i);
465
466 {
467 const char * src = (read_buf + x1*4 + y*pitch);
468 _generic_read_RGBA_span_BGRA8888_REV_SSE( src, rgba[i], n1 );
469 }
470 }
471 HW_ENDCLIPLOOP();
472 }
473 HW_UNLOCK();
474 #ifndef USE_INNER_EMMS
475 __asm__ __volatile__( "emms" );
476 #endif
477 }
478 #endif
479
480
481 static void TAG(ReadRGBAPixels)( const GLcontext *ctx,
482 GLuint n, const GLint x[], const GLint y[],
483 GLubyte rgba[][4], const GLubyte mask[] )
484 {
485 HW_READ_LOCK()
486 {
487 GLint i;
488 LOCAL_VARS;
489
490 if (DBG) fprintf(stderr, "ReadRGBAPixels\n");
491
492 HW_READ_CLIPLOOP()
493 {
494 if (mask)
495 {
496 for (i=0;i<n;i++)
497 if (mask[i]) {
498 int fy = Y_FLIP( y[i] );
499 if (CLIPPIXEL( x[i], fy ))
500 READ_RGBA( rgba[i], x[i], fy );
501 }
502 }
503 else
504 {
505 for (i=0;i<n;i++) {
506 int fy = Y_FLIP( y[i] );
507 if (CLIPPIXEL( x[i], fy ))
508 READ_RGBA( rgba[i], x[i], fy );
509 }
510 }
511 }
512 HW_ENDCLIPLOOP();
513 }
514 HW_READ_UNLOCK();
515 }
516
517 static void TAG(InitPointers)(struct swrast_device_driver *swdd)
518 {
519 swdd->WriteRGBASpan = TAG(WriteRGBASpan);
520 swdd->WriteRGBSpan = TAG(WriteRGBSpan);
521 swdd->WriteMonoRGBASpan = TAG(WriteMonoRGBASpan);
522 swdd->WriteRGBAPixels = TAG(WriteRGBAPixels);
523 swdd->WriteMonoRGBAPixels = TAG(WriteMonoRGBAPixels);
524 swdd->ReadRGBAPixels = TAG(ReadRGBAPixels);
525
526 #if defined(USE_SSE_ASM)
527 if ( cpu_has_xmm2 ) {
528 if (DBG) fprintf( stderr, "Using %s version of ReadRGBASpan\n", "SSE2" );
529 swdd->ReadRGBASpan = TAG2(ReadRGBASpan, _SSE2);
530 }
531 else
532 #endif
533 #if defined(USE_SSE_ASM)
534 if ( cpu_has_xmm ) {
535 if (DBG) fprintf( stderr, "Using %s version of ReadRGBASpan\n", "SSE" );
536 swdd->ReadRGBASpan = TAG2(ReadRGBASpan, _SSE);
537 }
538 else
539 #endif
540 #if defined(USE_MMX_ASM)
541 if ( cpu_has_mmx ) {
542 if (DBG) fprintf( stderr, "Using %s version of ReadRGBASpan\n", "MMX" );
543 swdd->ReadRGBASpan = TAG2(ReadRGBASpan, _MMX);
544 }
545 else
546 #endif
547 {
548 if (DBG) fprintf( stderr, "Using %s version of ReadRGBASpan\n", "C" );
549 swdd->ReadRGBASpan = TAG(ReadRGBASpan);
550 }
551
552 }
553
554
555 #undef INIT_MONO_PIXEL
556 #undef WRITE_PIXEL
557 #undef WRITE_RGBA
558 #undef READ_RGBA
559 #undef TAG
560 #undef TAG2
561 #undef GET_SRC_PTR
562 #undef GET_DST_PTR
563 #undef SPANTMP_PIXEL_FMT
564 #undef SPANTMP_PIXEL_TYPE