2 * Copyright 2000-2001 VA Linux Systems, Inc.
3 * (C) Copyright IBM Corporation 2004
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:
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
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.
29 * Template file of span read / write functions.
31 * \author Keith Whitwell <keithw@tungstengraphics.com>
32 * \author Gareth Hughes <gareth@nvidia.com>
33 * \author Ian Romanick <idr@us.ibm.com>
43 #define HW_WRITE_LOCK() HW_LOCK()
46 #ifndef HW_WRITE_UNLOCK
47 #define HW_WRITE_UNLOCK() HW_UNLOCK()
51 #define HW_READ_LOCK() HW_LOCK()
54 #ifndef HW_READ_UNLOCK
55 #define HW_READ_UNLOCK() HW_UNLOCK()
58 #ifndef HW_READ_CLIPLOOP
59 #define HW_READ_CLIPLOOP() HW_CLIPLOOP()
62 #ifndef HW_WRITE_CLIPLOOP
63 #define HW_WRITE_CLIPLOOP() HW_CLIPLOOP()
66 #if (SPANTMP_PIXEL_FMT == GL_RGB) && (SPANTMP_PIXEL_TYPE == GL_UNSIGNED_SHORT_5_6_5)
68 #define INIT_MONO_PIXEL(p, color) \
69 p = PACK_COLOR_565( color[0], color[1], color[2] )
71 #define WRITE_RGBA( _x, _y, r, g, b, a ) \
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)); \
78 #define WRITE_PIXEL( _x, _y, p ) \
80 GLushort * _p = (GLushort *) GET_DST_PTR(_x, _y); \
84 #define READ_RGBA( rgba, _x, _y ) \
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; \
93 #elif (SPANTMP_PIXEL_FMT == GL_BGRA) && (SPANTMP_PIXEL_TYPE == GL_UNSIGNED_INT_8_8_8_8_REV)
95 # define INIT_MONO_PIXEL(p, color) \
96 p = PACK_COLOR_8888(color[3], color[0], color[1], color[2])
98 # define WRITE_RGBA(_x, _y, r, g, b, a) \
100 GLuint * _p = (GLuint *) GET_DST_PTR(_x, _y); \
101 _p[0] = ((r << 16) | (g << 8) | (b << 0) | (a << 24)); \
104 #define WRITE_PIXEL(_x, _y, p) \
106 GLuint * _p = (GLuint *) GET_DST_PTR(_x, _y); \
110 # if defined( USE_X86_ASM )
111 # define READ_RGBA(rgba, _x, _y) \
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; \
119 # define READ_RGBA( rgba, _x, _y ) \
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; \
130 #error SPANTMP_PIXEL_FMT must be set to a valid value!
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"
138 static void TAG(WriteRGBASpan
)( const GLcontext
*ctx
,
139 GLuint n
, GLint x
, GLint y
,
140 const GLubyte rgba
[][4],
141 const GLubyte mask
[] )
154 CLIPSPAN(x
,y
,n
,x1
,n1
,i
);
156 if (DBG
) fprintf(stderr
, "WriteRGBASpan %d..%d (x1 %d)\n",
157 (int)i
, (int)n1
, (int)x1
);
161 for (;n1
>0;i
++,x1
++,n1
--)
164 rgba
[i
][0], rgba
[i
][1],
165 rgba
[i
][2], rgba
[i
][3] );
169 for (;n1
>0;i
++,x1
++,n1
--)
171 rgba
[i
][0], rgba
[i
][1],
172 rgba
[i
][2], rgba
[i
][3] );
180 static void TAG(WriteRGBSpan
)( const GLcontext
*ctx
,
181 GLuint n
, GLint x
, GLint y
,
182 const GLubyte rgb
[][3],
183 const GLubyte mask
[] )
196 CLIPSPAN(x
,y
,n
,x1
,n1
,i
);
198 if (DBG
) fprintf(stderr
, "WriteRGBSpan %d..%d (x1 %d)\n",
199 (int)i
, (int)n1
, (int)x1
);
203 for (;n1
>0;i
++,x1
++,n1
--)
205 WRITE_RGBA( x1
, y
, rgb
[i
][0], rgb
[i
][1], rgb
[i
][2], 255 );
209 for (;n1
>0;i
++,x1
++,n1
--)
210 WRITE_RGBA( x1
, y
, rgb
[i
][0], rgb
[i
][1], rgb
[i
][2], 255 );
218 static void TAG(WriteRGBAPixels
)( const GLcontext
*ctx
,
222 const GLubyte rgba
[][4],
223 const GLubyte mask
[] )
230 if (DBG
) fprintf(stderr
, "WriteRGBAPixels\n");
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] );
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] );
265 static void TAG(WriteMonoRGBASpan
)( const GLcontext
*ctx
,
266 GLuint n
, GLint x
, GLint y
,
267 const GLchan color
[4],
268 const GLubyte mask
[] )
275 INIT_MONO_PIXEL(p
, color
);
279 if (DBG
) fprintf(stderr
, "WriteMonoRGBASpan\n");
284 CLIPSPAN(x
,y
,n
,x1
,n1
,i
);
287 for (;n1
>0;i
++,x1
++,n1
--)
289 WRITE_PIXEL( x1
, y
, p
);
293 for (;n1
>0;i
++,x1
++,n1
--)
294 WRITE_PIXEL( x1
, y
, p
);
303 static void TAG(WriteMonoRGBAPixels
)( const GLcontext
*ctx
,
305 const GLint x
[], const GLint y
[],
306 const GLchan color
[],
307 const GLubyte mask
[] )
313 INIT_MONO_PIXEL(p
, color
);
315 if (DBG
) fprintf(stderr
, "WriteMonoRGBAPixels\n");
323 int fy
= Y_FLIP(y
[i
]);
324 if (CLIPPIXEL( x
[i
], fy
))
325 WRITE_PIXEL( x
[i
], fy
, p
);
331 int fy
= Y_FLIP(y
[i
]);
332 if (CLIPPIXEL( x
[i
], fy
))
333 WRITE_PIXEL( x
[i
], fy
, p
);
343 static void TAG(ReadRGBASpan
)( const GLcontext
*ctx
,
344 GLuint n
, GLint x
, GLint y
,
354 if (DBG
) fprintf(stderr
, "ReadRGBASpan\n");
359 CLIPSPAN(x
,y
,n
,x1
,n1
,i
);
360 for (;n1
>0;i
++,x1
++,n1
--)
361 READ_RGBA( rgba
[i
], x1
, y
);
369 #if defined(USE_MMX_ASM) && \
370 (SPANTMP_PIXEL_FMT == GL_BGRA) && \
371 (SPANTMP_PIXEL_TYPE == GL_UNSIGNED_INT_8_8_8_8_REV)
372 static void TAG2(ReadRGBASpan
,_MMX
)( const GLcontext
*ctx
,
373 GLuint n
, GLint x
, GLint y
,
376 #ifndef USE_INNER_EMMS
377 /* The EMMS instruction is directly in-lined here because using GCC's
378 * built-in _mm_empty function was found to utterly destroy performance.
380 __asm__
__volatile__( "emms" );
390 if (DBG
) fprintf(stderr
, "ReadRGBASpan\n");
395 CLIPSPAN(x
,y
,n
,x1
,n1
,i
);
398 const char * src
= GET_SRC_PTR( x1
, y
);
399 _generic_read_RGBA_span_BGRA8888_REV_MMX( src
, rgba
[i
], n1
);
405 #ifndef USE_INNER_EMMS
406 __asm__
__volatile__( "emms" );
412 #if defined(USE_SSE_ASM) && \
413 (SPANTMP_PIXEL_FMT == GL_BGRA) && \
414 (SPANTMP_PIXEL_TYPE == GL_UNSIGNED_INT_8_8_8_8_REV)
415 static void TAG2(ReadRGBASpan
,_SSE2
)( const GLcontext
*ctx
,
416 GLuint n
, GLint x
, GLint y
,
426 if (DBG
) fprintf(stderr
, "ReadRGBASpan\n");
431 CLIPSPAN(x
,y
,n
,x1
,n1
,i
);
434 const char * src
= GET_SRC_PTR( x1
, y
);
435 _generic_read_RGBA_span_BGRA8888_REV_SSE2( src
, rgba
[i
], n1
);
444 #if defined(USE_SSE_ASM) && \
445 (SPANTMP_PIXEL_FMT == GL_BGRA) && \
446 (SPANTMP_PIXEL_TYPE == GL_UNSIGNED_INT_8_8_8_8_REV)
447 static void TAG2(ReadRGBASpan
,_SSE
)( const GLcontext
*ctx
,
448 GLuint n
, GLint x
, GLint y
,
451 #ifndef USE_INNER_EMMS
452 /* The EMMS instruction is directly in-lined here because using GCC's
453 * built-in _mm_empty function was found to utterly destroy performance.
455 __asm__
__volatile__( "emms" );
465 if (DBG
) fprintf(stderr
, "ReadRGBASpan\n");
470 CLIPSPAN(x
,y
,n
,x1
,n1
,i
);
473 const char * src
= GET_SRC_PTR( x1
, y
);
474 _generic_read_RGBA_span_BGRA8888_REV_SSE( src
, rgba
[i
], n1
);
480 #ifndef USE_INNER_EMMS
481 __asm__
__volatile__( "emms" );
487 static void TAG(ReadRGBAPixels
)( const GLcontext
*ctx
,
488 GLuint n
, const GLint x
[], const GLint y
[],
489 GLubyte rgba
[][4], const GLubyte mask
[] )
496 if (DBG
) fprintf(stderr
, "ReadRGBAPixels\n");
504 int fy
= Y_FLIP( y
[i
] );
505 if (CLIPPIXEL( x
[i
], fy
))
506 READ_RGBA( rgba
[i
], x
[i
], fy
);
512 int fy
= Y_FLIP( y
[i
] );
513 if (CLIPPIXEL( x
[i
], fy
))
514 READ_RGBA( rgba
[i
], x
[i
], fy
);
523 static void TAG(InitPointers
)(struct swrast_device_driver
*swdd
)
525 swdd
->WriteRGBASpan
= TAG(WriteRGBASpan
);
526 swdd
->WriteRGBSpan
= TAG(WriteRGBSpan
);
527 swdd
->WriteMonoRGBASpan
= TAG(WriteMonoRGBASpan
);
528 swdd
->WriteRGBAPixels
= TAG(WriteRGBAPixels
);
529 swdd
->WriteMonoRGBAPixels
= TAG(WriteMonoRGBAPixels
);
530 swdd
->ReadRGBAPixels
= TAG(ReadRGBAPixels
);
532 #if (SPANTMP_PIXEL_FMT == GL_BGRA) && \
533 (SPANTMP_PIXEL_TYPE == GL_UNSIGNED_INT_8_8_8_8_REV)
534 #if defined(USE_SSE_ASM)
535 if ( cpu_has_xmm2
) {
536 if (DBG
) fprintf( stderr
, "Using %s version of ReadRGBASpan\n", "SSE2" );
537 swdd
->ReadRGBASpan
= TAG2(ReadRGBASpan
, _SSE2
);
541 #if defined(USE_SSE_ASM)
543 if (DBG
) fprintf( stderr
, "Using %s version of ReadRGBASpan\n", "SSE" );
544 swdd
->ReadRGBASpan
= TAG2(ReadRGBASpan
, _SSE
);
548 #if defined(USE_MMX_ASM)
550 if (DBG
) fprintf( stderr
, "Using %s version of ReadRGBASpan\n", "MMX" );
551 swdd
->ReadRGBASpan
= TAG2(ReadRGBASpan
, _MMX
);
557 if (DBG
) fprintf( stderr
, "Using %s version of ReadRGBASpan\n", "C" );
558 swdd
->ReadRGBASpan
= TAG(ReadRGBASpan
);
564 #undef INIT_MONO_PIXEL
572 #undef SPANTMP_PIXEL_FMT
573 #undef SPANTMP_PIXEL_TYPE