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>
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()
52 #if (SPANTMP_PIXEL_FMT == GL_RGB) && (SPANTMP_PIXEL_TYPE == GL_UNSIGNED_SHORT_5_6_5)
55 ** GL_RGB, GL_UNSIGNED_SHORT_5_6_5
59 #define GET_PTR(_x, _y) (buf + (_x) * 2 + (_y) * pitch)
62 #define INIT_MONO_PIXEL(p, color) \
63 p = PACK_COLOR_565( color[0], color[1], color[2] )
65 #define WRITE_RGBA( _x, _y, r, g, b, a ) \
67 GLshort * _p = (GLshort *) GET_PTR(_x, _y); \
68 _p[0] = ((((int)r & 0xf8) << 8) | (((int)g & 0xfc) << 3) | \
69 (((int)b & 0xf8) >> 3)); \
72 #define WRITE_PIXEL( _x, _y, p ) \
74 GLushort * _p = (GLushort *) GET_PTR(_x, _y); \
78 #define READ_RGBA( rgba, _x, _y ) \
80 GLushort p = *(volatile GLshort *) GET_PTR(_x, _y); \
81 rgba[0] = ((p >> 8) & 0xf8) * 255 / 0xf8; \
82 rgba[1] = ((p >> 3) & 0xfc) * 255 / 0xfc; \
83 rgba[2] = ((p << 3) & 0xf8) * 255 / 0xf8; \
87 #elif (SPANTMP_PIXEL_FMT == GL_BGRA) && (SPANTMP_PIXEL_TYPE == GL_UNSIGNED_INT_8_8_8_8_REV)
90 ** GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV
94 #define GET_PTR(_x, _y) ( buf + (_x) * 4 + (_y) * pitch)
97 # define INIT_MONO_PIXEL(p, color) \
98 p = PACK_COLOR_8888(color[3], color[0], color[1], color[2])
100 # define WRITE_RGBA(_x, _y, r, g, b, a) \
102 GLuint * _p = (GLuint *) GET_PTR(_x, _y); \
103 _p[0] = ((r << 16) | (g << 8) | (b << 0) | (a << 24)); \
106 #define WRITE_PIXEL(_x, _y, p) \
108 GLuint * _p = (GLuint *) GET_PTR(_x, _y); \
112 # if defined( USE_X86_ASM )
113 # define READ_RGBA(rgba, _x, _y) \
115 GLuint p = *(volatile GLuint *) GET_PTR(_x, _y); \
116 __asm__ __volatile__( "bswap %0; rorl $8, %0" \
117 : "=r" (p) : "0" (p) ); \
118 ((GLuint *)rgba)[0] = p; \
120 # elif defined( MESA_BIG_ENDIAN )
121 /* On PowerPC with GCC 3.4.2 the shift madness below becomes a single
122 * rotlwi instruction. It also produces good code on SPARC.
124 # define READ_RGBA( rgba, _x, _y ) \
126 GLuint p = *(volatile GLuint *) GET_PTR(_x, _y); \
128 *((uint32_t *) rgba) = (t >> 24) | (p << 8); \
131 # define READ_RGBA( rgba, _x, _y ) \
133 GLuint p = *(volatile GLuint *) GET_PTR(_x, _y); \
134 rgba[0] = (p >> 16) & 0xff; \
135 rgba[1] = (p >> 8) & 0xff; \
136 rgba[2] = (p >> 0) & 0xff; \
137 rgba[3] = (p >> 24) & 0xff; \
142 #error SPANTMP_PIXEL_FMT must be set to a valid value!
148 ** Assembly routines.
151 #if defined( USE_MMX_ASM ) || defined( USE_SSE_ASM )
152 #include "x86/read_rgba_span_x86.h"
153 #include "x86/common_x86_asm.h"
156 static void TAG(WriteRGBASpan
)( GLcontext
*ctx
,
157 struct gl_renderbuffer
*rb
,
158 GLuint n
, GLint x
, GLint y
,
159 const void *values
, const GLubyte mask
[] )
163 const GLubyte (*rgba
)[4] = (const GLubyte (*)[4]) values
;
173 CLIPSPAN(x
,y
,n
,x1
,n1
,i
);
175 if (DBG
) fprintf(stderr
, "WriteRGBASpan %d..%d (x1 %d)\n",
176 (int)i
, (int)n1
, (int)x1
);
180 for (;n1
>0;i
++,x1
++,n1
--)
183 rgba
[i
][0], rgba
[i
][1],
184 rgba
[i
][2], rgba
[i
][3] );
188 for (;n1
>0;i
++,x1
++,n1
--)
190 rgba
[i
][0], rgba
[i
][1],
191 rgba
[i
][2], rgba
[i
][3] );
199 static void TAG(WriteRGBSpan
)( GLcontext
*ctx
,
200 struct gl_renderbuffer
*rb
,
201 GLuint n
, GLint x
, GLint y
,
202 const void *values
, const GLubyte mask
[] )
206 const GLubyte (*rgb
)[3] = (const GLubyte (*)[3]) values
;
216 CLIPSPAN(x
,y
,n
,x1
,n1
,i
);
218 if (DBG
) fprintf(stderr
, "WriteRGBSpan %d..%d (x1 %d)\n",
219 (int)i
, (int)n1
, (int)x1
);
223 for (;n1
>0;i
++,x1
++,n1
--)
225 WRITE_RGBA( x1
, y
, rgb
[i
][0], rgb
[i
][1], rgb
[i
][2], 255 );
229 for (;n1
>0;i
++,x1
++,n1
--)
230 WRITE_RGBA( x1
, y
, rgb
[i
][0], rgb
[i
][1], rgb
[i
][2], 255 );
238 static void TAG(WriteRGBAPixels
)( GLcontext
*ctx
,
239 struct gl_renderbuffer
*rb
,
240 GLuint n
, const GLint x
[], const GLint y
[],
241 const void *values
, const GLubyte mask
[] )
245 const GLubyte (*rgba
)[4] = (const GLubyte (*)[4]) values
;
249 if (DBG
) fprintf(stderr
, "WriteRGBAPixels\n");
258 const int fy
= Y_FLIP(y
[i
]);
259 if (CLIPPIXEL(x
[i
],fy
))
260 WRITE_RGBA( x
[i
], fy
,
261 rgba
[i
][0], rgba
[i
][1],
262 rgba
[i
][2], rgba
[i
][3] );
270 const int fy
= Y_FLIP(y
[i
]);
271 if (CLIPPIXEL(x
[i
],fy
))
272 WRITE_RGBA( x
[i
], fy
,
273 rgba
[i
][0], rgba
[i
][1],
274 rgba
[i
][2], rgba
[i
][3] );
284 static void TAG(WriteMonoRGBASpan
)( GLcontext
*ctx
,
285 struct gl_renderbuffer
*rb
,
286 GLuint n
, GLint x
, GLint y
,
287 const void *value
, const GLubyte mask
[] )
291 const GLubyte
*color
= (const GLubyte
*) value
;
295 INIT_MONO_PIXEL(p
, color
);
299 if (DBG
) fprintf(stderr
, "WriteMonoRGBASpan\n");
304 CLIPSPAN(x
,y
,n
,x1
,n1
,i
);
307 for (;n1
>0;i
++,x1
++,n1
--)
309 WRITE_PIXEL( x1
, y
, p
);
313 for (;n1
>0;i
++,x1
++,n1
--)
314 WRITE_PIXEL( x1
, y
, p
);
323 static void TAG(WriteMonoRGBAPixels
)( GLcontext
*ctx
,
324 struct gl_renderbuffer
*rb
,
326 const GLint x
[], const GLint y
[],
328 const GLubyte mask
[] )
332 const GLubyte
*color
= (const GLubyte
*) value
;
335 INIT_MONO_PIXEL(p
, color
);
337 if (DBG
) fprintf(stderr
, "WriteMonoRGBAPixels\n");
345 int fy
= Y_FLIP(y
[i
]);
346 if (CLIPPIXEL( x
[i
], fy
))
347 WRITE_PIXEL( x
[i
], fy
, p
);
353 int fy
= Y_FLIP(y
[i
]);
354 if (CLIPPIXEL( x
[i
], fy
))
355 WRITE_PIXEL( x
[i
], fy
, p
);
365 static void TAG(ReadRGBASpan
)( GLcontext
*ctx
,
366 struct gl_renderbuffer
*rb
,
367 GLuint n
, GLint x
, GLint y
, void *values
)
371 GLubyte (*rgba
)[4] = (GLubyte (*)[4]) values
;
377 if (DBG
) fprintf(stderr
, "ReadRGBASpan\n");
382 CLIPSPAN(x
,y
,n
,x1
,n1
,i
);
383 for (;n1
>0;i
++,x1
++,n1
--)
384 READ_RGBA( rgba
[i
], x1
, y
);
392 #if defined(USE_MMX_ASM) && \
393 (((SPANTMP_PIXEL_FMT == GL_BGRA) && \
394 (SPANTMP_PIXEL_TYPE == GL_UNSIGNED_INT_8_8_8_8_REV)) || \
395 ((SPANTMP_PIXEL_FMT == GL_RGB) && \
396 (SPANTMP_PIXEL_TYPE == GL_UNSIGNED_SHORT_5_6_5)))
397 static void TAG2(ReadRGBASpan
,_MMX
)( GLcontext
*ctx
,
398 struct gl_renderbuffer
*rb
,
399 GLuint n
, GLint x
, GLint y
, void *values
)
401 #ifndef USE_INNER_EMMS
402 /* The EMMS instruction is directly in-lined here because using GCC's
403 * built-in _mm_empty function was found to utterly destroy performance.
405 __asm__
__volatile__( "emms" );
410 GLubyte (*rgba
)[4] = (GLubyte (*)[4]) values
;
416 if (DBG
) fprintf(stderr
, "ReadRGBASpan\n");
421 CLIPSPAN(x
,y
,n
,x1
,n1
,i
);
424 const void * src
= GET_PTR( x1
, y
);
425 #if (SPANTMP_PIXEL_FMT == GL_RGB) && \
426 (SPANTMP_PIXEL_TYPE == GL_UNSIGNED_SHORT_5_6_5)
427 _generic_read_RGBA_span_RGB565_MMX( src
, rgba
[i
], n1
);
429 _generic_read_RGBA_span_BGRA8888_REV_MMX( src
, rgba
[i
], n1
);
436 #ifndef USE_INNER_EMMS
437 __asm__
__volatile__( "emms" );
443 #if defined(USE_SSE_ASM) && \
444 (SPANTMP_PIXEL_FMT == GL_BGRA) && \
445 (SPANTMP_PIXEL_TYPE == GL_UNSIGNED_INT_8_8_8_8_REV)
446 static void TAG2(ReadRGBASpan
,_SSE2
)( GLcontext
*ctx
,
447 struct gl_renderbuffer
*rb
,
448 GLuint n
, GLint x
, GLint y
,
453 GLubyte (*rgba
)[4] = (GLubyte (*)[4]) values
;
459 if (DBG
) fprintf(stderr
, "ReadRGBASpan\n");
464 CLIPSPAN(x
,y
,n
,x1
,n1
,i
);
467 const void * src
= GET_PTR( x1
, y
);
468 _generic_read_RGBA_span_BGRA8888_REV_SSE2( src
, rgba
[i
], n1
);
477 #if defined(USE_SSE_ASM) && \
478 (SPANTMP_PIXEL_FMT == GL_BGRA) && \
479 (SPANTMP_PIXEL_TYPE == GL_UNSIGNED_INT_8_8_8_8_REV)
480 static void TAG2(ReadRGBASpan
,_SSE
)( GLcontext
*ctx
,
481 struct gl_renderbuffer
*rb
,
482 GLuint n
, GLint x
, GLint y
,
485 #ifndef USE_INNER_EMMS
486 /* The EMMS instruction is directly in-lined here because using GCC's
487 * built-in _mm_empty function was found to utterly destroy performance.
489 __asm__
__volatile__( "emms" );
494 GLubyte (*rgba
)[4] = (GLubyte (*)[4]) values
;
500 if (DBG
) fprintf(stderr
, "ReadRGBASpan\n");
505 CLIPSPAN(x
,y
,n
,x1
,n1
,i
);
508 const void * src
= GET_PTR( x1
, y
);
509 _generic_read_RGBA_span_BGRA8888_REV_SSE( src
, rgba
[i
], n1
);
515 #ifndef USE_INNER_EMMS
516 __asm__
__volatile__( "emms" );
522 static void TAG(ReadRGBAPixels
)( GLcontext
*ctx
,
523 struct gl_renderbuffer
*rb
,
524 GLuint n
, const GLint x
[], const GLint y
[],
529 GLubyte (*rgba
)[4] = (GLubyte (*)[4]) values
;
530 GLubyte
*mask
= NULL
; /* remove someday */
534 if (DBG
) fprintf(stderr
, "ReadRGBAPixels\n");
542 int fy
= Y_FLIP( y
[i
] );
543 if (CLIPPIXEL( x
[i
], fy
))
544 READ_RGBA( rgba
[i
], x
[i
], fy
);
550 int fy
= Y_FLIP( y
[i
] );
551 if (CLIPPIXEL( x
[i
], fy
))
552 READ_RGBA( rgba
[i
], x
[i
], fy
);
561 static void TAG(InitPointers
)(struct gl_renderbuffer
*rb
)
563 rb
->PutRow
= TAG(WriteRGBASpan
);
564 rb
->PutRowRGB
= TAG(WriteRGBSpan
);
565 rb
->PutMonoRow
= TAG(WriteMonoRGBASpan
);
566 rb
->PutValues
= TAG(WriteRGBAPixels
);
567 rb
->PutMonoValues
= TAG(WriteMonoRGBAPixels
);
568 rb
->GetValues
= TAG(ReadRGBAPixels
);
570 #if defined(USE_SSE_ASM) && \
571 (SPANTMP_PIXEL_FMT == GL_BGRA) && \
572 (SPANTMP_PIXEL_TYPE == GL_UNSIGNED_INT_8_8_8_8_REV)
573 if ( cpu_has_xmm2
) {
574 if (DBG
) fprintf( stderr
, "Using %s version of GetRow\n", "SSE2" );
575 rb
->GetRow
= TAG2(ReadRGBASpan
, _SSE2
);
579 #if defined(USE_SSE_ASM) && \
580 (SPANTMP_PIXEL_FMT == GL_BGRA) && \
581 (SPANTMP_PIXEL_TYPE == GL_UNSIGNED_INT_8_8_8_8_REV)
583 if (DBG
) fprintf( stderr
, "Using %s version of GetRow\n", "SSE" );
584 rb
->GetRow
= TAG2(ReadRGBASpan
, _SSE
);
588 #if defined(USE_MMX_ASM) && \
589 (((SPANTMP_PIXEL_FMT == GL_BGRA) && \
590 (SPANTMP_PIXEL_TYPE == GL_UNSIGNED_INT_8_8_8_8_REV)) || \
591 ((SPANTMP_PIXEL_FMT == GL_RGB) && \
592 (SPANTMP_PIXEL_TYPE == GL_UNSIGNED_SHORT_5_6_5)))
594 if (DBG
) fprintf( stderr
, "Using %s version of GetRow\n", "MMX" );
595 rb
->GetRow
= TAG2(ReadRGBASpan
, _MMX
);
600 if (DBG
) fprintf( stderr
, "Using %s version of GetRow\n", "C" );
601 rb
->GetRow
= TAG(ReadRGBASpan
);
607 #undef INIT_MONO_PIXEL
614 #undef SPANTMP_PIXEL_FMT
615 #undef SPANTMP_PIXEL_TYPE