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; \
118 # elif defined( MESA_BIG_ENDIAN )
119 /* On PowerPC with GCC 3.4.2 the shift madness below becomes a single
120 * rotlwi instruction. It also produces good code on SPARC.
122 # define READ_RGBA( rgba, _x, _y ) \
124 GLuint p = *(volatile GLuint *) GET_SRC_PTR(_x, _y); \
126 *((uint32_t *) rgba) = (t >> 24) | (p << 8); \
129 # define READ_RGBA( rgba, _x, _y ) \
131 GLuint p = *(volatile GLuint *) GET_SRC_PTR(_x, _y); \
132 rgba[0] = (p >> 16) & 0xff; \
133 rgba[1] = (p >> 8) & 0xff; \
134 rgba[2] = (p >> 0) & 0xff; \
135 rgba[3] = (p >> 24) & 0xff; \
140 #error SPANTMP_PIXEL_FMT must be set to a valid value!
143 #if defined( USE_MMX_ASM ) || defined( USE_SSE_ASM )
144 #include "x86/read_rgba_span_x86.h"
145 #include "x86/common_x86_asm.h"
148 static void TAG(WriteRGBASpan
)( GLcontext
*ctx
,
149 struct gl_renderbuffer
*rb
,
150 GLuint n
, GLint x
, GLint y
,
151 const void *values
, const GLubyte mask
[] )
155 const GLubyte (*rgba
)[4] = (const GLubyte (*)[4]) values
;
165 CLIPSPAN(x
,y
,n
,x1
,n1
,i
);
167 if (DBG
) fprintf(stderr
, "WriteRGBASpan %d..%d (x1 %d)\n",
168 (int)i
, (int)n1
, (int)x1
);
172 for (;n1
>0;i
++,x1
++,n1
--)
175 rgba
[i
][0], rgba
[i
][1],
176 rgba
[i
][2], rgba
[i
][3] );
180 for (;n1
>0;i
++,x1
++,n1
--)
182 rgba
[i
][0], rgba
[i
][1],
183 rgba
[i
][2], rgba
[i
][3] );
191 static void TAG(WriteRGBSpan
)( GLcontext
*ctx
,
192 struct gl_renderbuffer
*rb
,
193 GLuint n
, GLint x
, GLint y
,
194 const void *values
, const GLubyte mask
[] )
198 const GLubyte (*rgb
)[3] = (const GLubyte (*)[3]) values
;
208 CLIPSPAN(x
,y
,n
,x1
,n1
,i
);
210 if (DBG
) fprintf(stderr
, "WriteRGBSpan %d..%d (x1 %d)\n",
211 (int)i
, (int)n1
, (int)x1
);
215 for (;n1
>0;i
++,x1
++,n1
--)
217 WRITE_RGBA( x1
, y
, rgb
[i
][0], rgb
[i
][1], rgb
[i
][2], 255 );
221 for (;n1
>0;i
++,x1
++,n1
--)
222 WRITE_RGBA( x1
, y
, rgb
[i
][0], rgb
[i
][1], rgb
[i
][2], 255 );
230 static void TAG(WriteRGBAPixels
)( GLcontext
*ctx
,
231 struct gl_renderbuffer
*rb
,
232 GLuint n
, const GLint x
[], const GLint y
[],
233 const void *values
, const GLubyte mask
[] )
237 const GLubyte (*rgba
)[4] = (const GLubyte (*)[4]) values
;
241 if (DBG
) fprintf(stderr
, "WriteRGBAPixels\n");
250 const int fy
= Y_FLIP(y
[i
]);
251 if (CLIPPIXEL(x
[i
],fy
))
252 WRITE_RGBA( x
[i
], fy
,
253 rgba
[i
][0], rgba
[i
][1],
254 rgba
[i
][2], rgba
[i
][3] );
262 const int fy
= Y_FLIP(y
[i
]);
263 if (CLIPPIXEL(x
[i
],fy
))
264 WRITE_RGBA( x
[i
], fy
,
265 rgba
[i
][0], rgba
[i
][1],
266 rgba
[i
][2], rgba
[i
][3] );
276 static void TAG(WriteMonoRGBASpan
)( GLcontext
*ctx
,
277 struct gl_renderbuffer
*rb
,
278 GLuint n
, GLint x
, GLint y
,
279 const void *value
, const GLubyte mask
[] )
283 const GLubyte
*color
= (const GLubyte
*) value
;
287 INIT_MONO_PIXEL(p
, color
);
291 if (DBG
) fprintf(stderr
, "WriteMonoRGBASpan\n");
296 CLIPSPAN(x
,y
,n
,x1
,n1
,i
);
299 for (;n1
>0;i
++,x1
++,n1
--)
301 WRITE_PIXEL( x1
, y
, p
);
305 for (;n1
>0;i
++,x1
++,n1
--)
306 WRITE_PIXEL( x1
, y
, p
);
315 static void TAG(WriteMonoRGBAPixels
)( GLcontext
*ctx
,
316 struct gl_renderbuffer
*rb
,
318 const GLint x
[], const GLint y
[],
320 const GLubyte mask
[] )
324 const GLubyte
*color
= (const GLubyte
*) value
;
327 INIT_MONO_PIXEL(p
, color
);
329 if (DBG
) fprintf(stderr
, "WriteMonoRGBAPixels\n");
337 int fy
= Y_FLIP(y
[i
]);
338 if (CLIPPIXEL( x
[i
], fy
))
339 WRITE_PIXEL( x
[i
], fy
, p
);
345 int fy
= Y_FLIP(y
[i
]);
346 if (CLIPPIXEL( x
[i
], fy
))
347 WRITE_PIXEL( x
[i
], fy
, p
);
357 static void TAG(ReadRGBASpan
)( GLcontext
*ctx
,
358 struct gl_renderbuffer
*rb
,
359 GLuint n
, GLint x
, GLint y
, void *values
)
363 GLubyte (*rgba
)[4] = (GLubyte (*)[4]) values
;
369 if (DBG
) fprintf(stderr
, "ReadRGBASpan\n");
374 CLIPSPAN(x
,y
,n
,x1
,n1
,i
);
375 for (;n1
>0;i
++,x1
++,n1
--)
376 READ_RGBA( rgba
[i
], x1
, y
);
384 #if defined(USE_MMX_ASM) && \
385 (((SPANTMP_PIXEL_FMT == GL_BGRA) && \
386 (SPANTMP_PIXEL_TYPE == GL_UNSIGNED_INT_8_8_8_8_REV)) || \
387 ((SPANTMP_PIXEL_FMT == GL_RGB) && \
388 (SPANTMP_PIXEL_TYPE == GL_UNSIGNED_SHORT_5_6_5)))
389 static void TAG2(ReadRGBASpan
,_MMX
)( GLcontext
*ctx
,
390 struct gl_renderbuffer
*rb
,
391 GLuint n
, GLint x
, GLint y
, void *values
)
393 #ifndef USE_INNER_EMMS
394 /* The EMMS instruction is directly in-lined here because using GCC's
395 * built-in _mm_empty function was found to utterly destroy performance.
397 __asm__
__volatile__( "emms" );
402 GLubyte (*rgba
)[4] = (GLubyte (*)[4]) values
;
408 if (DBG
) fprintf(stderr
, "ReadRGBASpan\n");
413 CLIPSPAN(x
,y
,n
,x1
,n1
,i
);
416 const char * src
= GET_SRC_PTR( x1
, y
);
417 #if (SPANTMP_PIXEL_FMT == GL_RGB) && \
418 (SPANTMP_PIXEL_TYPE == GL_UNSIGNED_SHORT_5_6_5)
419 _generic_read_RGBA_span_RGB565_MMX( src
, rgba
[i
], n1
);
421 _generic_read_RGBA_span_BGRA8888_REV_MMX( src
, rgba
[i
], n1
);
428 #ifndef USE_INNER_EMMS
429 __asm__
__volatile__( "emms" );
435 #if defined(USE_SSE_ASM) && \
436 (SPANTMP_PIXEL_FMT == GL_BGRA) && \
437 (SPANTMP_PIXEL_TYPE == GL_UNSIGNED_INT_8_8_8_8_REV)
438 static void TAG2(ReadRGBASpan
,_SSE2
)( GLcontext
*ctx
,
439 struct gl_renderbuffer
*rb
,
440 GLuint n
, GLint x
, GLint y
,
445 GLubyte (*rgba
)[4] = (GLubyte (*)[4]) values
;
451 if (DBG
) fprintf(stderr
, "ReadRGBASpan\n");
456 CLIPSPAN(x
,y
,n
,x1
,n1
,i
);
459 const char * src
= GET_SRC_PTR( x1
, y
);
460 _generic_read_RGBA_span_BGRA8888_REV_SSE2( src
, rgba
[i
], n1
);
469 #if defined(USE_SSE_ASM) && \
470 (SPANTMP_PIXEL_FMT == GL_BGRA) && \
471 (SPANTMP_PIXEL_TYPE == GL_UNSIGNED_INT_8_8_8_8_REV)
472 static void TAG2(ReadRGBASpan
,_SSE
)( GLcontext
*ctx
,
473 struct gl_renderbuffer
*rb
,
474 GLuint n
, GLint x
, GLint y
,
477 #ifndef USE_INNER_EMMS
478 /* The EMMS instruction is directly in-lined here because using GCC's
479 * built-in _mm_empty function was found to utterly destroy performance.
481 __asm__
__volatile__( "emms" );
486 GLubyte (*rgba
)[4] = (GLubyte (*)[4]) values
;
492 if (DBG
) fprintf(stderr
, "ReadRGBASpan\n");
497 CLIPSPAN(x
,y
,n
,x1
,n1
,i
);
500 const char * src
= GET_SRC_PTR( x1
, y
);
501 _generic_read_RGBA_span_BGRA8888_REV_SSE( src
, rgba
[i
], n1
);
507 #ifndef USE_INNER_EMMS
508 __asm__
__volatile__( "emms" );
514 static void TAG(ReadRGBAPixels
)( GLcontext
*ctx
,
515 struct gl_renderbuffer
*rb
,
516 GLuint n
, const GLint x
[], const GLint y
[],
521 GLubyte (*rgba
)[4] = (GLubyte (*)[4]) values
;
522 GLubyte
*mask
= NULL
; /* remove someday */
526 if (DBG
) fprintf(stderr
, "ReadRGBAPixels\n");
534 int fy
= Y_FLIP( y
[i
] );
535 if (CLIPPIXEL( x
[i
], fy
))
536 READ_RGBA( rgba
[i
], x
[i
], fy
);
542 int fy
= Y_FLIP( y
[i
] );
543 if (CLIPPIXEL( x
[i
], fy
))
544 READ_RGBA( rgba
[i
], x
[i
], fy
);
553 static void TAG(InitPointers
)(struct gl_renderbuffer
*rb
)
555 rb
->PutRow
= TAG(WriteRGBASpan
);
556 rb
->PutRowRGB
= TAG(WriteRGBSpan
);
557 rb
->PutMonoRow
= TAG(WriteMonoRGBASpan
);
558 rb
->PutValues
= TAG(WriteRGBAPixels
);
559 rb
->PutMonoValues
= TAG(WriteMonoRGBAPixels
);
560 rb
->GetValues
= TAG(ReadRGBAPixels
);
562 #if defined(USE_SSE_ASM) && \
563 (SPANTMP_PIXEL_FMT == GL_BGRA) && \
564 (SPANTMP_PIXEL_TYPE == GL_UNSIGNED_INT_8_8_8_8_REV)
565 if ( cpu_has_xmm2
) {
566 if (DBG
) fprintf( stderr
, "Using %s version of GetRow\n", "SSE2" );
567 rb
->GetRow
= TAG2(ReadRGBASpan
, _SSE2
);
571 #if defined(USE_SSE_ASM) && \
572 (SPANTMP_PIXEL_FMT == GL_BGRA) && \
573 (SPANTMP_PIXEL_TYPE == GL_UNSIGNED_INT_8_8_8_8_REV)
575 if (DBG
) fprintf( stderr
, "Using %s version of GetRow\n", "SSE" );
576 rb
->GetRow
= TAG2(ReadRGBASpan
, _SSE
);
580 #if defined(USE_MMX_ASM) && \
581 (((SPANTMP_PIXEL_FMT == GL_BGRA) && \
582 (SPANTMP_PIXEL_TYPE == GL_UNSIGNED_INT_8_8_8_8_REV)) || \
583 ((SPANTMP_PIXEL_FMT == GL_RGB) && \
584 (SPANTMP_PIXEL_TYPE == GL_UNSIGNED_SHORT_5_6_5)))
586 if (DBG
) fprintf( stderr
, "Using %s version of GetRow\n", "MMX" );
587 rb
->GetRow
= TAG2(ReadRGBASpan
, _MMX
);
592 if (DBG
) fprintf( stderr
, "Using %s version of GetRow\n", "C" );
593 rb
->GetRow
= TAG(ReadRGBASpan
);
599 #undef INIT_MONO_PIXEL
607 #undef SPANTMP_PIXEL_FMT
608 #undef SPANTMP_PIXEL_TYPE