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>
36 #include "main/colormac.h"
37 #include "spantmp_common.h"
43 #ifndef HW_READ_CLIPLOOP
44 #define HW_READ_CLIPLOOP() HW_CLIPLOOP()
47 #ifndef HW_WRITE_CLIPLOOP
48 #define HW_WRITE_CLIPLOOP() HW_CLIPLOOP()
51 #if (SPANTMP_PIXEL_FMT == GL_RGB) && (SPANTMP_PIXEL_TYPE == GL_UNSIGNED_SHORT_5_6_5)
54 ** GL_RGB, GL_UNSIGNED_SHORT_5_6_5
59 #define GET_PTR(_x, _y) (buf + (_x) * 2 + (_y) * pitch)
62 #define GET_VALUE(_x, _y) *(volatile GLushort *)(GET_PTR(_x, _y))
63 #define PUT_VALUE(_x, _y, _v) *(volatile GLushort *)(GET_PTR(_x, _y)) = (_v)
64 #endif /* GET_VALUE */
66 #define INIT_MONO_PIXEL(p, color) \
67 p = PACK_COLOR_565( color[0], color[1], color[2] )
69 #define WRITE_RGBA( _x, _y, r, g, b, a ) \
70 PUT_VALUE(_x, _y, ((((int)r & 0xf8) << 8) | \
71 (((int)g & 0xfc) << 3) | \
72 (((int)b & 0xf8) >> 3))) \
74 #define WRITE_PIXEL( _x, _y, p ) PUT_VALUE(_x, _y, p)
76 #define READ_RGBA( rgba, _x, _y ) \
78 GLushort p = GET_VALUE(_x, _y); \
79 rgba[0] = ((p >> 8) & 0xf8) * 255 / 0xf8; \
80 rgba[1] = ((p >> 3) & 0xfc) * 255 / 0xfc; \
81 rgba[2] = ((p << 3) & 0xf8) * 255 / 0xf8; \
85 #elif (SPANTMP_PIXEL_FMT == GL_BGRA) && (SPANTMP_PIXEL_TYPE == GL_UNSIGNED_SHORT_4_4_4_4_REV)
88 ** GL_BGRA, GL_UNSIGNED_SHORT_4_4_4_4_REV
93 #define GET_PTR(_x, _y) (buf + (_x) * 2 + (_y) * pitch)
96 #define GET_VALUE(_x, _y) *(volatile GLushort *)(GET_PTR(_x, _y))
97 #define PUT_VALUE(_x, _y, _v) *(volatile GLushort *)(GET_PTR(_x, _y)) = (_v)
98 #endif /* GET_VALUE */
100 #define INIT_MONO_PIXEL(p, color) \
101 p = PACK_COLOR_4444(color[3], color[0], color[1], color[2])
103 #define WRITE_RGBA( _x, _y, r, g, b, a ) \
104 PUT_VALUE(_x, _y, PACK_COLOR_4444(a, r, g, b)) \
106 #define WRITE_PIXEL( _x, _y, p ) PUT_VALUE(_x, _y, p)
108 #define READ_RGBA( rgba, _x, _y ) \
110 GLushort p = GET_VALUE(_x, _y); \
111 rgba[0] = ((p >> 8) & 0xf) * 0x11; \
112 rgba[1] = ((p >> 4) & 0xf) * 0x11; \
113 rgba[2] = ((p >> 0) & 0xf) * 0x11; \
114 rgba[3] = ((p >> 12) & 0xf) * 0x11; \
118 #elif (SPANTMP_PIXEL_FMT == GL_BGRA) && (SPANTMP_PIXEL_TYPE == GL_UNSIGNED_SHORT_1_5_5_5_REV)
121 ** GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV
126 #define GET_PTR(_x, _y) (buf + (_x) * 2 + (_y) * pitch)
129 #define GET_VALUE(_x, _y) *(volatile GLushort *)(GET_PTR(_x, _y))
130 #define PUT_VALUE(_x, _y, _v) *(volatile GLushort *)(GET_PTR(_x, _y)) = (_v)
131 #endif /* GET_VALUE */
133 #define INIT_MONO_PIXEL(p, color) \
134 p = PACK_COLOR_1555(color[3], color[0], color[1], color[2])
136 #define WRITE_RGBA( _x, _y, r, g, b, a ) \
137 PUT_VALUE(_x, _y, PACK_COLOR_1555(a, r, g, b)) \
139 #define WRITE_PIXEL( _x, _y, p ) PUT_VALUE(_x, _y, p)
141 #define READ_RGBA( rgba, _x, _y ) \
143 GLushort p = GET_VALUE(_x, _y); \
144 rgba[0] = ((p >> 7) & 0xf8) * 255 / 0xf8; \
145 rgba[1] = ((p >> 2) & 0xf8) * 255 / 0xf8; \
146 rgba[2] = ((p << 3) & 0xf8) * 255 / 0xf8; \
147 rgba[3] = ((p >> 15) & 0x1) * 0xff; \
150 #elif (SPANTMP_PIXEL_FMT == GL_BGRA) && (SPANTMP_PIXEL_TYPE == GL_UNSIGNED_INT_8_8_8_8_REV)
153 ** GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV
158 #define GET_PTR(_x, _y) ( buf + (_x) * 4 + (_y) * pitch)
161 #define GET_VALUE(_x, _y) *(volatile GLuint *)(GET_PTR(_x, _y))
162 #define PUT_VALUE(_x, _y, _v) *(volatile GLuint *)(GET_PTR(_x, _y)) = (_v)
163 #endif /* GET_VALUE */
165 # define INIT_MONO_PIXEL(p, color) \
166 p = PACK_COLOR_8888(color[3], color[0], color[1], color[2])
168 # define WRITE_RGBA(_x, _y, r, g, b, a) \
169 PUT_VALUE(_x, _y, ((r << 16) | \
174 #define WRITE_PIXEL(_x, _y, p) PUT_VALUE(_x, _y, p)
176 # if defined( USE_X86_ASM )
177 # define READ_RGBA(rgba, _x, _y) \
179 GLuint p = GET_VALUE(_x, _y); \
180 __asm__ __volatile__( "bswap %0; rorl $8, %0" \
181 : "=r" (p) : "0" (p) ); \
182 ((GLuint *)rgba)[0] = p; \
184 # elif defined( MESA_BIG_ENDIAN )
185 /* On PowerPC with GCC 3.4.2 the shift madness below becomes a single
186 * rotlwi instruction. It also produces good code on SPARC.
188 # define READ_RGBA( rgba, _x, _y ) \
190 GLuint p = GET_VALUE(_x, _y); \
192 *((uint32_t *) rgba) = (t >> 24) | (p << 8); \
195 # define READ_RGBA( rgba, _x, _y ) \
197 GLuint p = GET_VALUE(_x, _y); \
198 rgba[0] = (p >> 16) & 0xff; \
199 rgba[1] = (p >> 8) & 0xff; \
200 rgba[2] = (p >> 0) & 0xff; \
201 rgba[3] = (p >> 24) & 0xff; \
206 #error SPANTMP_PIXEL_FMT must be set to a valid value!
212 ** Assembly routines.
215 #if defined( USE_MMX_ASM ) || defined( USE_SSE_ASM )
216 #include "x86/read_rgba_span_x86.h"
217 #include "x86/common_x86_asm.h"
220 static void TAG(WriteRGBASpan
)( GLcontext
*ctx
,
221 struct gl_renderbuffer
*rb
,
222 GLuint n
, GLint x
, GLint y
,
223 const void *values
, const GLubyte mask
[] )
227 const GLubyte (*rgba
)[4] = (const GLubyte (*)[4]) values
;
237 CLIPSPAN(x
,y
,n
,x1
,n1
,i
);
239 if (DBG
) fprintf(stderr
, "WriteRGBASpan %d..%d (x1 %d)\n",
240 (int)i
, (int)n1
, (int)x1
);
244 for (;n1
>0;i
++,x1
++,n1
--)
247 rgba
[i
][0], rgba
[i
][1],
248 rgba
[i
][2], rgba
[i
][3] );
252 for (;n1
>0;i
++,x1
++,n1
--)
254 rgba
[i
][0], rgba
[i
][1],
255 rgba
[i
][2], rgba
[i
][3] );
263 static void TAG(WriteRGBSpan
)( GLcontext
*ctx
,
264 struct gl_renderbuffer
*rb
,
265 GLuint n
, GLint x
, GLint y
,
266 const void *values
, const GLubyte mask
[] )
270 const GLubyte (*rgb
)[3] = (const GLubyte (*)[3]) values
;
280 CLIPSPAN(x
,y
,n
,x1
,n1
,i
);
282 if (DBG
) fprintf(stderr
, "WriteRGBSpan %d..%d (x1 %d)\n",
283 (int)i
, (int)n1
, (int)x1
);
287 for (;n1
>0;i
++,x1
++,n1
--)
289 WRITE_RGBA( x1
, y
, rgb
[i
][0], rgb
[i
][1], rgb
[i
][2], 255 );
293 for (;n1
>0;i
++,x1
++,n1
--)
294 WRITE_RGBA( x1
, y
, rgb
[i
][0], rgb
[i
][1], rgb
[i
][2], 255 );
302 static void TAG(WriteRGBAPixels
)( GLcontext
*ctx
,
303 struct gl_renderbuffer
*rb
,
304 GLuint n
, const GLint x
[], const GLint y
[],
305 const void *values
, const GLubyte mask
[] )
309 const GLubyte (*rgba
)[4] = (const GLubyte (*)[4]) values
;
313 if (DBG
) fprintf(stderr
, "WriteRGBAPixels\n");
322 const int fy
= Y_FLIP(y
[i
]);
323 if (CLIPPIXEL(x
[i
],fy
))
324 WRITE_RGBA( x
[i
], fy
,
325 rgba
[i
][0], rgba
[i
][1],
326 rgba
[i
][2], rgba
[i
][3] );
334 const int fy
= Y_FLIP(y
[i
]);
335 if (CLIPPIXEL(x
[i
],fy
))
336 WRITE_RGBA( x
[i
], fy
,
337 rgba
[i
][0], rgba
[i
][1],
338 rgba
[i
][2], rgba
[i
][3] );
348 static void TAG(WriteMonoRGBASpan
)( GLcontext
*ctx
,
349 struct gl_renderbuffer
*rb
,
350 GLuint n
, GLint x
, GLint y
,
351 const void *value
, const GLubyte mask
[] )
355 const GLubyte
*color
= (const GLubyte
*) value
;
359 INIT_MONO_PIXEL(p
, color
);
363 if (DBG
) fprintf(stderr
, "WriteMonoRGBASpan\n");
368 CLIPSPAN(x
,y
,n
,x1
,n1
,i
);
371 for (;n1
>0;i
++,x1
++,n1
--)
373 WRITE_PIXEL( x1
, y
, p
);
377 for (;n1
>0;i
++,x1
++,n1
--)
378 WRITE_PIXEL( x1
, y
, p
);
387 static void TAG(WriteMonoRGBAPixels
)( GLcontext
*ctx
,
388 struct gl_renderbuffer
*rb
,
390 const GLint x
[], const GLint y
[],
392 const GLubyte mask
[] )
396 const GLubyte
*color
= (const GLubyte
*) value
;
399 INIT_MONO_PIXEL(p
, color
);
401 if (DBG
) fprintf(stderr
, "WriteMonoRGBAPixels\n");
409 int fy
= Y_FLIP(y
[i
]);
410 if (CLIPPIXEL( x
[i
], fy
))
411 WRITE_PIXEL( x
[i
], fy
, p
);
417 int fy
= Y_FLIP(y
[i
]);
418 if (CLIPPIXEL( x
[i
], fy
))
419 WRITE_PIXEL( x
[i
], fy
, p
);
429 static void TAG(ReadRGBASpan
)( GLcontext
*ctx
,
430 struct gl_renderbuffer
*rb
,
431 GLuint n
, GLint x
, GLint y
, void *values
)
435 GLubyte (*rgba
)[4] = (GLubyte (*)[4]) values
;
441 if (DBG
) fprintf(stderr
, "ReadRGBASpan\n");
446 CLIPSPAN(x
,y
,n
,x1
,n1
,i
);
447 for (;n1
>0;i
++,x1
++,n1
--)
448 READ_RGBA( rgba
[i
], x1
, y
);
456 #if defined(GET_PTR) && \
457 defined(USE_MMX_ASM) && \
458 (((SPANTMP_PIXEL_FMT == GL_BGRA) && \
459 (SPANTMP_PIXEL_TYPE == GL_UNSIGNED_INT_8_8_8_8_REV)) || \
460 ((SPANTMP_PIXEL_FMT == GL_RGB) && \
461 (SPANTMP_PIXEL_TYPE == GL_UNSIGNED_SHORT_5_6_5)))
462 static void TAG2(ReadRGBASpan
,_MMX
)( GLcontext
*ctx
,
463 struct gl_renderbuffer
*rb
,
464 GLuint n
, GLint x
, GLint y
, void *values
)
466 #ifndef USE_INNER_EMMS
467 /* The EMMS instruction is directly in-lined here because using GCC's
468 * built-in _mm_empty function was found to utterly destroy performance.
470 __asm__
__volatile__( "emms" );
475 GLubyte (*rgba
)[4] = (GLubyte (*)[4]) values
;
481 if (DBG
) fprintf(stderr
, "ReadRGBASpan\n");
486 CLIPSPAN(x
,y
,n
,x1
,n1
,i
);
489 const void * src
= GET_PTR( x1
, y
);
490 #if (SPANTMP_PIXEL_FMT == GL_RGB) && \
491 (SPANTMP_PIXEL_TYPE == GL_UNSIGNED_SHORT_5_6_5)
492 _generic_read_RGBA_span_RGB565_MMX( src
, rgba
[i
], n1
);
494 _generic_read_RGBA_span_BGRA8888_REV_MMX( src
, rgba
[i
], n1
);
501 #ifndef USE_INNER_EMMS
502 __asm__
__volatile__( "emms" );
508 #if defined(GET_PTR) && \
509 defined(USE_SSE_ASM) && \
510 (SPANTMP_PIXEL_FMT == GL_BGRA) && \
511 (SPANTMP_PIXEL_TYPE == GL_UNSIGNED_INT_8_8_8_8_REV)
512 static void TAG2(ReadRGBASpan
,_SSE2
)( GLcontext
*ctx
,
513 struct gl_renderbuffer
*rb
,
514 GLuint n
, GLint x
, GLint y
,
519 GLubyte (*rgba
)[4] = (GLubyte (*)[4]) values
;
525 if (DBG
) fprintf(stderr
, "ReadRGBASpan\n");
530 CLIPSPAN(x
,y
,n
,x1
,n1
,i
);
533 const void * src
= GET_PTR( x1
, y
);
534 _generic_read_RGBA_span_BGRA8888_REV_SSE2( src
, rgba
[i
], n1
);
543 #if defined(GET_PTR) && \
544 defined(USE_SSE_ASM) && \
545 (SPANTMP_PIXEL_FMT == GL_BGRA) && \
546 (SPANTMP_PIXEL_TYPE == GL_UNSIGNED_INT_8_8_8_8_REV)
547 static void TAG2(ReadRGBASpan
,_SSE
)( GLcontext
*ctx
,
548 struct gl_renderbuffer
*rb
,
549 GLuint n
, GLint x
, GLint y
,
552 #ifndef USE_INNER_EMMS
553 /* The EMMS instruction is directly in-lined here because using GCC's
554 * built-in _mm_empty function was found to utterly destroy performance.
556 __asm__
__volatile__( "emms" );
561 GLubyte (*rgba
)[4] = (GLubyte (*)[4]) values
;
567 if (DBG
) fprintf(stderr
, "ReadRGBASpan\n");
572 CLIPSPAN(x
,y
,n
,x1
,n1
,i
);
575 const void * src
= GET_PTR( x1
, y
);
576 _generic_read_RGBA_span_BGRA8888_REV_SSE( src
, rgba
[i
], n1
);
582 #ifndef USE_INNER_EMMS
583 __asm__
__volatile__( "emms" );
589 static void TAG(ReadRGBAPixels
)( GLcontext
*ctx
,
590 struct gl_renderbuffer
*rb
,
591 GLuint n
, const GLint x
[], const GLint y
[],
596 GLubyte (*rgba
)[4] = (GLubyte (*)[4]) values
;
597 GLubyte
*mask
= NULL
; /* remove someday */
601 if (DBG
) fprintf(stderr
, "ReadRGBAPixels\n");
609 int fy
= Y_FLIP( y
[i
] );
610 if (CLIPPIXEL( x
[i
], fy
))
611 READ_RGBA( rgba
[i
], x
[i
], fy
);
617 int fy
= Y_FLIP( y
[i
] );
618 if (CLIPPIXEL( x
[i
], fy
))
619 READ_RGBA( rgba
[i
], x
[i
], fy
);
628 static void TAG(InitPointers
)(struct gl_renderbuffer
*rb
)
630 rb
->PutRow
= TAG(WriteRGBASpan
);
631 rb
->PutRowRGB
= TAG(WriteRGBSpan
);
632 rb
->PutMonoRow
= TAG(WriteMonoRGBASpan
);
633 rb
->PutValues
= TAG(WriteRGBAPixels
);
634 rb
->PutMonoValues
= TAG(WriteMonoRGBAPixels
);
635 rb
->GetValues
= TAG(ReadRGBAPixels
);
638 #if defined(USE_SSE_ASM) && \
639 (SPANTMP_PIXEL_FMT == GL_BGRA) && \
640 (SPANTMP_PIXEL_TYPE == GL_UNSIGNED_INT_8_8_8_8_REV)
641 if ( cpu_has_xmm2
) {
642 if (DBG
) fprintf( stderr
, "Using %s version of GetRow\n", "SSE2" );
643 rb
->GetRow
= TAG2(ReadRGBASpan
, _SSE2
);
647 #if defined(USE_SSE_ASM) && \
648 (SPANTMP_PIXEL_FMT == GL_BGRA) && \
649 (SPANTMP_PIXEL_TYPE == GL_UNSIGNED_INT_8_8_8_8_REV)
651 if (DBG
) fprintf( stderr
, "Using %s version of GetRow\n", "SSE" );
652 rb
->GetRow
= TAG2(ReadRGBASpan
, _SSE
);
656 #if defined(USE_MMX_ASM) && \
657 (((SPANTMP_PIXEL_FMT == GL_BGRA) && \
658 (SPANTMP_PIXEL_TYPE == GL_UNSIGNED_INT_8_8_8_8_REV)) || \
659 ((SPANTMP_PIXEL_FMT == GL_RGB) && \
660 (SPANTMP_PIXEL_TYPE == GL_UNSIGNED_SHORT_5_6_5)))
662 if (DBG
) fprintf( stderr
, "Using %s version of GetRow\n", "MMX" );
663 rb
->GetRow
= TAG2(ReadRGBASpan
, _MMX
);
669 if (DBG
) fprintf( stderr
, "Using %s version of GetRow\n", "C" );
670 rb
->GetRow
= TAG(ReadRGBASpan
);
676 #undef INIT_MONO_PIXEL
685 #undef SPANTMP_PIXEL_FMT
686 #undef SPANTMP_PIXEL_TYPE