1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
29 * RGBA/float tile get/put functions.
30 * Usable both by drivers and state trackers.
34 #include "pipe/p_defines.h"
35 #include "pipe/p_inlines.h"
37 #include "util/u_math.h"
38 #include "util/u_memory.h"
39 #include "util/u_rect.h"
40 #include "util/u_tile.h"
44 * Move raw block of pixels from transfer object to user memory.
47 pipe_get_tile_raw(struct pipe_transfer
*pt
,
48 uint x
, uint y
, uint w
, uint h
,
49 void *dst
, int dst_stride
)
51 struct pipe_screen
*screen
= pt
->texture
->screen
;
55 dst_stride
= pf_get_stride(pt
->texture
->format
, w
);
57 if (pipe_clip_tile(x
, y
, &w
, &h
, pt
))
60 src
= screen
->transfer_map(screen
, pt
);
65 util_copy_rect(dst
, pt
->texture
->format
, dst_stride
, 0, 0, w
, h
, src
, pt
->stride
, x
, y
);
67 screen
->transfer_unmap(screen
, pt
);
72 * Move raw block of pixels from user memory to transfer object.
75 pipe_put_tile_raw(struct pipe_transfer
*pt
,
76 uint x
, uint y
, uint w
, uint h
,
77 const void *src
, int src_stride
)
79 struct pipe_screen
*screen
= pt
->texture
->screen
;
81 enum pipe_format format
= pt
->texture
->format
;
84 src_stride
= pf_get_stride(format
, w
);
86 if (pipe_clip_tile(x
, y
, &w
, &h
, pt
))
89 dst
= screen
->transfer_map(screen
, pt
);
94 util_copy_rect(dst
, format
, pt
->stride
, x
, y
, w
, h
, src
, src_stride
, 0, 0);
96 screen
->transfer_unmap(screen
, pt
);
102 /** Convert short in [-32768,32767] to GLfloat in [-1.0,1.0] */
103 #define SHORT_TO_FLOAT(S) ((2.0F * (S) + 1.0F) * (1.0F/65535.0F))
105 #define UNCLAMPED_FLOAT_TO_SHORT(us, f) \
106 us = ( (short) ( CLAMP((f), -1.0, 1.0) * 32767.0F) )
110 /*** PIPE_FORMAT_A8R8G8B8_UNORM ***/
113 a8r8g8b8_get_tile_rgba(const unsigned *src
,
114 unsigned w
, unsigned h
,
120 for (i
= 0; i
< h
; i
++) {
122 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
123 const unsigned pixel
= *src
++;
124 pRow
[0] = ubyte_to_float((pixel
>> 16) & 0xff);
125 pRow
[1] = ubyte_to_float((pixel
>> 8) & 0xff);
126 pRow
[2] = ubyte_to_float((pixel
>> 0) & 0xff);
127 pRow
[3] = ubyte_to_float((pixel
>> 24) & 0xff);
135 a8r8g8b8_put_tile_rgba(unsigned *dst
,
136 unsigned w
, unsigned h
,
142 for (i
= 0; i
< h
; i
++) {
143 const float *pRow
= p
;
144 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
146 r
= float_to_ubyte(pRow
[0]);
147 g
= float_to_ubyte(pRow
[1]);
148 b
= float_to_ubyte(pRow
[2]);
149 a
= float_to_ubyte(pRow
[3]);
150 *dst
++ = (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
157 /*** PIPE_FORMAT_X8R8G8B8_UNORM ***/
160 x8r8g8b8_get_tile_rgba(const unsigned *src
,
161 unsigned w
, unsigned h
,
167 for (i
= 0; i
< h
; i
++) {
169 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
170 const unsigned pixel
= *src
++;
171 pRow
[0] = ubyte_to_float((pixel
>> 16) & 0xff);
172 pRow
[1] = ubyte_to_float((pixel
>> 8) & 0xff);
173 pRow
[2] = ubyte_to_float((pixel
>> 0) & 0xff);
182 x8r8g8b8_put_tile_rgba(unsigned *dst
,
183 unsigned w
, unsigned h
,
189 for (i
= 0; i
< h
; i
++) {
190 const float *pRow
= p
;
191 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
193 r
= float_to_ubyte(pRow
[0]);
194 g
= float_to_ubyte(pRow
[1]);
195 b
= float_to_ubyte(pRow
[2]);
196 *dst
++ = (0xff << 24) | (r
<< 16) | (g
<< 8) | b
;
203 /*** PIPE_FORMAT_B8G8R8A8_UNORM ***/
206 b8g8r8a8_get_tile_rgba(const unsigned *src
,
207 unsigned w
, unsigned h
,
213 for (i
= 0; i
< h
; i
++) {
215 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
216 const unsigned pixel
= *src
++;
217 pRow
[0] = ubyte_to_float((pixel
>> 8) & 0xff);
218 pRow
[1] = ubyte_to_float((pixel
>> 16) & 0xff);
219 pRow
[2] = ubyte_to_float((pixel
>> 24) & 0xff);
220 pRow
[3] = ubyte_to_float((pixel
>> 0) & 0xff);
228 b8g8r8a8_put_tile_rgba(unsigned *dst
,
229 unsigned w
, unsigned h
,
235 for (i
= 0; i
< h
; i
++) {
236 const float *pRow
= p
;
237 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
239 r
= float_to_ubyte(pRow
[0]);
240 g
= float_to_ubyte(pRow
[1]);
241 b
= float_to_ubyte(pRow
[2]);
242 a
= float_to_ubyte(pRow
[3]);
243 *dst
++ = (b
<< 24) | (g
<< 16) | (r
<< 8) | a
;
250 /*** PIPE_FORMAT_A1R5G5B5_UNORM ***/
253 a1r5g5b5_get_tile_rgba(const ushort
*src
,
254 unsigned w
, unsigned h
,
260 for (i
= 0; i
< h
; i
++) {
262 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
263 const ushort pixel
= *src
++;
264 pRow
[0] = ((pixel
>> 10) & 0x1f) * (1.0f
/ 31.0f
);
265 pRow
[1] = ((pixel
>> 5) & 0x1f) * (1.0f
/ 31.0f
);
266 pRow
[2] = ((pixel
) & 0x1f) * (1.0f
/ 31.0f
);
267 pRow
[3] = ((pixel
>> 15) ) * 1.0f
;
275 a1r5g5b5_put_tile_rgba(ushort
*dst
,
276 unsigned w
, unsigned h
,
282 for (i
= 0; i
< h
; i
++) {
283 const float *pRow
= p
;
284 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
286 r
= float_to_ubyte(pRow
[0]);
287 g
= float_to_ubyte(pRow
[1]);
288 b
= float_to_ubyte(pRow
[2]);
289 a
= float_to_ubyte(pRow
[3]);
290 r
= r
>> 3; /* 5 bits */
291 g
= g
>> 3; /* 5 bits */
292 b
= b
>> 3; /* 5 bits */
293 a
= a
>> 7; /* 1 bit */
294 *dst
++ = (a
<< 15) | (r
<< 10) | (g
<< 5) | b
;
301 /*** PIPE_FORMAT_A4R4G4B4_UNORM ***/
304 a4r4g4b4_get_tile_rgba(const ushort
*src
,
305 unsigned w
, unsigned h
,
311 for (i
= 0; i
< h
; i
++) {
313 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
314 const ushort pixel
= *src
++;
315 pRow
[0] = ((pixel
>> 8) & 0xf) * (1.0f
/ 15.0f
);
316 pRow
[1] = ((pixel
>> 4) & 0xf) * (1.0f
/ 15.0f
);
317 pRow
[2] = ((pixel
) & 0xf) * (1.0f
/ 15.0f
);
318 pRow
[3] = ((pixel
>> 12) ) * (1.0f
/ 15.0f
);
326 a4r4g4b4_put_tile_rgba(ushort
*dst
,
327 unsigned w
, unsigned h
,
333 for (i
= 0; i
< h
; i
++) {
334 const float *pRow
= p
;
335 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
337 r
= float_to_ubyte(pRow
[0]);
338 g
= float_to_ubyte(pRow
[1]);
339 b
= float_to_ubyte(pRow
[2]);
340 a
= float_to_ubyte(pRow
[3]);
345 *dst
++ = (a
<< 12) | (r
<< 16) | (g
<< 4) | b
;
352 /*** PIPE_FORMAT_R5G6B5_UNORM ***/
355 r5g6b5_get_tile_rgba(const ushort
*src
,
356 unsigned w
, unsigned h
,
362 for (i
= 0; i
< h
; i
++) {
364 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
365 const ushort pixel
= *src
++;
366 pRow
[0] = ((pixel
>> 11) & 0x1f) * (1.0f
/ 31.0f
);
367 pRow
[1] = ((pixel
>> 5) & 0x3f) * (1.0f
/ 63.0f
);
368 pRow
[2] = ((pixel
) & 0x1f) * (1.0f
/ 31.0f
);
377 r5g6b5_put_tile_rgba(ushort
*dst
,
378 unsigned w
, unsigned h
,
384 for (i
= 0; i
< h
; i
++) {
385 const float *pRow
= p
;
386 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
387 uint r
= (uint
) (CLAMP(pRow
[0], 0.0, 1.0) * 31.0);
388 uint g
= (uint
) (CLAMP(pRow
[1], 0.0, 1.0) * 63.0);
389 uint b
= (uint
) (CLAMP(pRow
[2], 0.0, 1.0) * 31.0);
390 *dst
++ = (r
<< 11) | (g
<< 5) | (b
);
398 /*** PIPE_FORMAT_R8G8B8_UNORM ***/
401 r8g8b8_get_tile_rgba(const ubyte
*src
,
402 unsigned w
, unsigned h
,
408 for (i
= 0; i
< h
; i
++) {
410 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
411 pRow
[0] = ubyte_to_float(src
[0]);
412 pRow
[1] = ubyte_to_float(src
[1]);
413 pRow
[2] = ubyte_to_float(src
[2]);
423 r8g8b8_put_tile_rgba(ubyte
*dst
,
424 unsigned w
, unsigned h
,
430 for (i
= 0; i
< h
; i
++) {
431 const float *pRow
= p
;
432 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
433 dst
[0] = float_to_ubyte(pRow
[0]);
434 dst
[1] = float_to_ubyte(pRow
[1]);
435 dst
[2] = float_to_ubyte(pRow
[2]);
444 /*** PIPE_FORMAT_Z16_UNORM ***/
447 * Return each Z value as four floats in [0,1].
450 z16_get_tile_rgba(const ushort
*src
,
451 unsigned w
, unsigned h
,
455 const float scale
= 1.0f
/ 65535.0f
;
458 for (i
= 0; i
< h
; i
++) {
460 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
464 pRow
[3] = *src
++ * scale
;
473 /*** PIPE_FORMAT_L8_UNORM ***/
476 l8_get_tile_rgba(const ubyte
*src
,
477 unsigned w
, unsigned h
,
483 for (i
= 0; i
< h
; i
++) {
485 for (j
= 0; j
< w
; j
++, src
++, pRow
+= 4) {
488 pRow
[2] = ubyte_to_float(*src
);
497 l8_put_tile_rgba(ubyte
*dst
,
498 unsigned w
, unsigned h
,
504 for (i
= 0; i
< h
; i
++) {
505 const float *pRow
= p
;
506 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
508 r
= float_to_ubyte(pRow
[0]);
517 /*** PIPE_FORMAT_A8_UNORM ***/
520 a8_get_tile_rgba(const ubyte
*src
,
521 unsigned w
, unsigned h
,
527 for (i
= 0; i
< h
; i
++) {
529 for (j
= 0; j
< w
; j
++, src
++, pRow
+= 4) {
533 pRow
[3] = ubyte_to_float(*src
);
541 a8_put_tile_rgba(ubyte
*dst
,
542 unsigned w
, unsigned h
,
548 for (i
= 0; i
< h
; i
++) {
549 const float *pRow
= p
;
550 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
552 a
= float_to_ubyte(pRow
[3]);
561 /*** PIPE_FORMAT_R16_SNORM ***/
564 r16_get_tile_rgba(const short *src
,
565 unsigned w
, unsigned h
,
571 for (i
= 0; i
< h
; i
++) {
573 for (j
= 0; j
< w
; j
++, src
++, pRow
+= 4) {
574 pRow
[0] = SHORT_TO_FLOAT(src
[0]);
585 r16_put_tile_rgba(short *dst
,
586 unsigned w
, unsigned h
,
592 for (i
= 0; i
< h
; i
++) {
593 const float *pRow
= p
;
594 for (j
= 0; j
< w
; j
++, dst
++, pRow
+= 4) {
595 UNCLAMPED_FLOAT_TO_SHORT(dst
[0], pRow
[0]);
602 /*** PIPE_FORMAT_R16G16B16A16_SNORM ***/
605 r16g16b16a16_get_tile_rgba(const short *src
,
606 unsigned w
, unsigned h
,
612 for (i
= 0; i
< h
; i
++) {
614 for (j
= 0; j
< w
; j
++, src
+= 4, pRow
+= 4) {
615 pRow
[0] = SHORT_TO_FLOAT(src
[0]);
616 pRow
[1] = SHORT_TO_FLOAT(src
[1]);
617 pRow
[2] = SHORT_TO_FLOAT(src
[2]);
618 pRow
[3] = SHORT_TO_FLOAT(src
[3]);
626 r16g16b16a16_put_tile_rgba(short *dst
,
627 unsigned w
, unsigned h
,
633 for (i
= 0; i
< h
; i
++) {
634 const float *pRow
= p
;
635 for (j
= 0; j
< w
; j
++, dst
+= 4, pRow
+= 4) {
636 UNCLAMPED_FLOAT_TO_SHORT(dst
[0], pRow
[0]);
637 UNCLAMPED_FLOAT_TO_SHORT(dst
[1], pRow
[1]);
638 UNCLAMPED_FLOAT_TO_SHORT(dst
[2], pRow
[2]);
639 UNCLAMPED_FLOAT_TO_SHORT(dst
[3], pRow
[3]);
646 /*** PIPE_FORMAT_R8G8B8A8_SRGB ***/
649 * Convert an 8-bit sRGB value from non-linear space to a
650 * linear RGB value in [0, 1].
651 * Implemented with a 256-entry lookup table.
654 srgb_to_linear(ubyte cs8
)
656 static float table
[256];
657 static boolean tableReady
= FALSE
;
659 /* compute lookup table now */
661 for (i
= 0; i
< 256; i
++) {
662 const float cs
= ubyte_to_float(i
);
664 table
[i
] = cs
/ 12.92f
;
667 table
[i
] = (float) powf((cs
+ 0.055) / 1.055, 2.4);
677 * Convert linear float in [0,1] to an srgb ubyte value in [0,255].
678 * XXX this hasn't been tested (render to srgb surface).
679 * XXX this needs optimization.
682 linear_to_srgb(float cl
)
686 else if (cl
>= 0.0031308F
)
687 return float_to_ubyte(1.055F
* powf(cl
, 0.41666F
) - 0.055F
);
689 return float_to_ubyte(12.92F
* cl
);
696 a8r8g8b8_srgb_get_tile_rgba(const unsigned *src
,
697 unsigned w
, unsigned h
,
703 for (i
= 0; i
< h
; i
++) {
705 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
706 const unsigned pixel
= *src
++;
707 pRow
[0] = srgb_to_linear((pixel
>> 16) & 0xff);
708 pRow
[1] = srgb_to_linear((pixel
>> 8) & 0xff);
709 pRow
[2] = srgb_to_linear((pixel
>> 0) & 0xff);
710 pRow
[3] = ubyte_to_float((pixel
>> 24) & 0xff);
717 a8r8g8b8_srgb_put_tile_rgba(unsigned *dst
,
718 unsigned w
, unsigned h
,
724 for (i
= 0; i
< h
; i
++) {
725 const float *pRow
= p
;
726 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
728 r
= linear_to_srgb(pRow
[0]);
729 g
= linear_to_srgb(pRow
[1]);
730 b
= linear_to_srgb(pRow
[2]);
731 a
= float_to_ubyte(pRow
[3]);
732 *dst
++ = (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
739 /*** PIPE_FORMAT_A8L8_SRGB ***/
742 a8l8_srgb_get_tile_rgba(const ushort
*src
,
743 unsigned w
, unsigned h
,
749 for (i
= 0; i
< h
; i
++) {
751 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
755 pRow
[2] = srgb_to_linear(p
& 0xff);
756 pRow
[3] = ubyte_to_float(p
>> 8);
763 a8l8_srgb_put_tile_rgba(ushort
*dst
,
764 unsigned w
, unsigned h
,
770 for (i
= 0; i
< h
; i
++) {
771 const float *pRow
= p
;
772 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
774 r
= linear_to_srgb(pRow
[0]);
775 a
= float_to_ubyte(pRow
[3]);
776 *dst
++ = (a
<< 8) | r
;
783 /*** PIPE_FORMAT_L8_SRGB ***/
786 l8_srgb_get_tile_rgba(const ubyte
*src
,
787 unsigned w
, unsigned h
,
793 for (i
= 0; i
< h
; i
++) {
795 for (j
= 0; j
< w
; j
++, src
++, pRow
+= 4) {
798 pRow
[2] = srgb_to_linear(*src
);
806 l8_srgb_put_tile_rgba(ubyte
*dst
,
807 unsigned w
, unsigned h
,
813 for (i
= 0; i
< h
; i
++) {
814 const float *pRow
= p
;
815 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
817 r
= linear_to_srgb(pRow
[0]);
825 /*** PIPE_FORMAT_I8_UNORM ***/
828 i8_get_tile_rgba(const ubyte
*src
,
829 unsigned w
, unsigned h
,
835 for (i
= 0; i
< h
; i
++) {
837 for (j
= 0; j
< w
; j
++, src
++, pRow
+= 4) {
841 pRow
[3] = ubyte_to_float(*src
);
849 i8_put_tile_rgba(ubyte
*dst
,
850 unsigned w
, unsigned h
,
856 for (i
= 0; i
< h
; i
++) {
857 const float *pRow
= p
;
858 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
860 r
= float_to_ubyte(pRow
[0]);
868 /*** PIPE_FORMAT_A8L8_UNORM ***/
871 a8l8_get_tile_rgba(const ushort
*src
,
872 unsigned w
, unsigned h
,
878 for (i
= 0; i
< h
; i
++) {
880 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
884 pRow
[2] = ubyte_to_float(p
& 0xff);
885 pRow
[3] = ubyte_to_float(p
>> 8);
893 a8l8_put_tile_rgba(ushort
*dst
,
894 unsigned w
, unsigned h
,
900 for (i
= 0; i
< h
; i
++) {
901 const float *pRow
= p
;
902 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
904 r
= float_to_ubyte(pRow
[0]);
905 a
= float_to_ubyte(pRow
[3]);
906 *dst
++ = (a
<< 8) | r
;
915 /*** PIPE_FORMAT_Z32_UNORM ***/
918 * Return each Z value as four floats in [0,1].
921 z32_get_tile_rgba(const unsigned *src
,
922 unsigned w
, unsigned h
,
926 const double scale
= 1.0 / (double) 0xffffffff;
929 for (i
= 0; i
< h
; i
++) {
931 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
935 pRow
[3] = (float) (*src
++ * scale
);
942 /*** PIPE_FORMAT_S8Z24_UNORM ***/
945 * Return Z component as four float in [0,1]. Stencil part ignored.
948 s8z24_get_tile_rgba(const unsigned *src
,
949 unsigned w
, unsigned h
,
953 const double scale
= 1.0 / ((1 << 24) - 1);
956 for (i
= 0; i
< h
; i
++) {
958 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
962 pRow
[3] = (float) (scale
* (*src
++ & 0xffffff));
969 /*** PIPE_FORMAT_Z24S8_UNORM ***/
972 * Return Z component as four float in [0,1]. Stencil part ignored.
975 z24s8_get_tile_rgba(const unsigned *src
,
976 unsigned w
, unsigned h
,
980 const double scale
= 1.0 / ((1 << 24) - 1);
983 for (i
= 0; i
< h
; i
++) {
985 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
989 pRow
[3] = (float) (scale
* (*src
++ >> 8));
996 /*** PIPE_FORMAT_Z32_FLOAT ***/
999 * Return each Z value as four floats in [0,1].
1002 z32f_get_tile_rgba(const float *src
,
1003 unsigned w
, unsigned h
,
1005 unsigned dst_stride
)
1009 for (i
= 0; i
< h
; i
++) {
1011 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
1022 /*** PIPE_FORMAT_YCBCR / PIPE_FORMAT_YCBCR_REV ***/
1025 * Convert YCbCr (or YCrCb) to RGBA.
1028 ycbcr_get_tile_rgba(const ushort
*src
,
1029 unsigned w
, unsigned h
,
1031 unsigned dst_stride
,
1034 const float scale
= 1.0f
/ 255.0f
;
1037 for (i
= 0; i
< h
; i
++) {
1039 /* do two texels at a time */
1040 for (j
= 0; j
< (w
& ~1); j
+= 2, src
+= 2) {
1041 const ushort t0
= src
[0];
1042 const ushort t1
= src
[1];
1043 const ubyte y0
= (t0
>> 8) & 0xff; /* luminance */
1044 const ubyte y1
= (t1
>> 8) & 0xff; /* luminance */
1049 cb
= t1
& 0xff; /* chroma U */
1050 cr
= t0
& 0xff; /* chroma V */
1053 cb
= t0
& 0xff; /* chroma U */
1054 cr
= t1
& 0xff; /* chroma V */
1057 /* even pixel: y0,cr,cb */
1058 r
= 1.164f
* (y0
-16) + 1.596f
* (cr
-128);
1059 g
= 1.164f
* (y0
-16) - 0.813f
* (cr
-128) - 0.391f
* (cb
-128);
1060 b
= 1.164f
* (y0
-16) + 2.018f
* (cb
-128);
1061 pRow
[0] = r
* scale
;
1062 pRow
[1] = g
* scale
;
1063 pRow
[2] = b
* scale
;
1067 /* odd pixel: use y1,cr,cb */
1068 r
= 1.164f
* (y1
-16) + 1.596f
* (cr
-128);
1069 g
= 1.164f
* (y1
-16) - 0.813f
* (cr
-128) - 0.391f
* (cb
-128);
1070 b
= 1.164f
* (y1
-16) + 2.018f
* (cb
-128);
1071 pRow
[0] = r
* scale
;
1072 pRow
[1] = g
* scale
;
1073 pRow
[2] = b
* scale
;
1078 /* do the last texel */
1080 const ushort t0
= src
[0];
1081 const ushort t1
= src
[1];
1082 const ubyte y0
= (t0
>> 8) & 0xff; /* luminance */
1087 cb
= t1
& 0xff; /* chroma U */
1088 cr
= t0
& 0xff; /* chroma V */
1091 cb
= t0
& 0xff; /* chroma U */
1092 cr
= t1
& 0xff; /* chroma V */
1095 /* even pixel: y0,cr,cb */
1096 r
= 1.164f
* (y0
-16) + 1.596f
* (cr
-128);
1097 g
= 1.164f
* (y0
-16) - 0.813f
* (cr
-128) - 0.391f
* (cb
-128);
1098 b
= 1.164f
* (y0
-16) + 2.018f
* (cb
-128);
1099 pRow
[0] = r
* scale
;
1100 pRow
[1] = g
* scale
;
1101 pRow
[2] = b
* scale
;
1111 fake_get_tile_rgba(const ushort
*src
,
1112 unsigned w
, unsigned h
,
1114 unsigned dst_stride
)
1118 for (i
= 0; i
< h
; i
++) {
1120 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
1124 pRow
[3] = (i
^ j
) & 1 ? 1.0f
: 0.0f
;
1132 pipe_tile_raw_to_rgba(enum pipe_format format
,
1135 float *dst
, unsigned dst_stride
)
1138 case PIPE_FORMAT_A8R8G8B8_UNORM
:
1139 a8r8g8b8_get_tile_rgba((unsigned *) src
, w
, h
, dst
, dst_stride
);
1141 case PIPE_FORMAT_X8R8G8B8_UNORM
:
1142 x8r8g8b8_get_tile_rgba((unsigned *) src
, w
, h
, dst
, dst_stride
);
1144 case PIPE_FORMAT_B8G8R8A8_UNORM
:
1145 b8g8r8a8_get_tile_rgba((unsigned *) src
, w
, h
, dst
, dst_stride
);
1147 case PIPE_FORMAT_A1R5G5B5_UNORM
:
1148 a1r5g5b5_get_tile_rgba((ushort
*) src
, w
, h
, dst
, dst_stride
);
1150 case PIPE_FORMAT_A4R4G4B4_UNORM
:
1151 a4r4g4b4_get_tile_rgba((ushort
*) src
, w
, h
, dst
, dst_stride
);
1153 case PIPE_FORMAT_R5G6B5_UNORM
:
1154 r5g6b5_get_tile_rgba((ushort
*) src
, w
, h
, dst
, dst_stride
);
1156 case PIPE_FORMAT_R8G8B8_UNORM
:
1157 r8g8b8_get_tile_rgba((ubyte
*) src
, w
, h
, dst
, dst_stride
);
1159 case PIPE_FORMAT_L8_UNORM
:
1160 l8_get_tile_rgba((ubyte
*) src
, w
, h
, dst
, dst_stride
);
1162 case PIPE_FORMAT_A8_UNORM
:
1163 a8_get_tile_rgba((ubyte
*) src
, w
, h
, dst
, dst_stride
);
1165 case PIPE_FORMAT_I8_UNORM
:
1166 i8_get_tile_rgba((ubyte
*) src
, w
, h
, dst
, dst_stride
);
1168 case PIPE_FORMAT_A8L8_UNORM
:
1169 a8l8_get_tile_rgba((ushort
*) src
, w
, h
, dst
, dst_stride
);
1171 case PIPE_FORMAT_R16_SNORM
:
1172 r16_get_tile_rgba((short *) src
, w
, h
, dst
, dst_stride
);
1174 case PIPE_FORMAT_R16G16B16A16_SNORM
:
1175 r16g16b16a16_get_tile_rgba((short *) src
, w
, h
, dst
, dst_stride
);
1177 case PIPE_FORMAT_A8R8G8B8_SRGB
:
1178 a8r8g8b8_srgb_get_tile_rgba((unsigned *) src
, w
, h
, dst
, dst_stride
);
1180 case PIPE_FORMAT_A8L8_SRGB
:
1181 a8l8_srgb_get_tile_rgba((ushort
*) src
, w
, h
, dst
, dst_stride
);
1183 case PIPE_FORMAT_L8_SRGB
:
1184 l8_srgb_get_tile_rgba((ubyte
*) src
, w
, h
, dst
, dst_stride
);
1186 case PIPE_FORMAT_Z16_UNORM
:
1187 z16_get_tile_rgba((ushort
*) src
, w
, h
, dst
, dst_stride
);
1189 case PIPE_FORMAT_Z32_UNORM
:
1190 z32_get_tile_rgba((unsigned *) src
, w
, h
, dst
, dst_stride
);
1192 case PIPE_FORMAT_S8Z24_UNORM
:
1193 case PIPE_FORMAT_X8Z24_UNORM
:
1194 s8z24_get_tile_rgba((unsigned *) src
, w
, h
, dst
, dst_stride
);
1196 case PIPE_FORMAT_Z24S8_UNORM
:
1197 case PIPE_FORMAT_Z24X8_UNORM
:
1198 z24s8_get_tile_rgba((unsigned *) src
, w
, h
, dst
, dst_stride
);
1200 case PIPE_FORMAT_Z32_FLOAT
:
1201 z32f_get_tile_rgba((float *) src
, w
, h
, dst
, dst_stride
);
1203 case PIPE_FORMAT_YCBCR
:
1204 ycbcr_get_tile_rgba((ushort
*) src
, w
, h
, dst
, dst_stride
, FALSE
);
1206 case PIPE_FORMAT_YCBCR_REV
:
1207 ycbcr_get_tile_rgba((ushort
*) src
, w
, h
, dst
, dst_stride
, TRUE
);
1210 debug_printf("%s: unsupported format %s\n", __FUNCTION__
, pf_name(format
));
1211 fake_get_tile_rgba(src
, w
, h
, dst
, dst_stride
);
1217 pipe_get_tile_rgba(struct pipe_transfer
*pt
,
1218 uint x
, uint y
, uint w
, uint h
,
1221 unsigned dst_stride
= w
* 4;
1223 enum pipe_format format
= pt
->texture
->format
;
1225 if (pipe_clip_tile(x
, y
, &w
, &h
, pt
))
1228 packed
= MALLOC(pf_get_nblocks(format
, w
, h
) * pf_get_blocksize(format
));
1233 if(format
== PIPE_FORMAT_YCBCR
|| format
== PIPE_FORMAT_YCBCR_REV
)
1234 assert((x
& 1) == 0);
1236 pipe_get_tile_raw(pt
, x
, y
, w
, h
, packed
, 0);
1238 pipe_tile_raw_to_rgba(format
, packed
, w
, h
, p
, dst_stride
);
1245 pipe_put_tile_rgba(struct pipe_transfer
*pt
,
1246 uint x
, uint y
, uint w
, uint h
,
1249 unsigned src_stride
= w
* 4;
1251 enum pipe_format format
= pt
->texture
->format
;
1253 if (pipe_clip_tile(x
, y
, &w
, &h
, pt
))
1256 packed
= MALLOC(pf_get_nblocks(format
, w
, h
) * pf_get_blocksize(format
));
1262 case PIPE_FORMAT_A8R8G8B8_UNORM
:
1263 a8r8g8b8_put_tile_rgba((unsigned *) packed
, w
, h
, p
, src_stride
);
1265 case PIPE_FORMAT_X8R8G8B8_UNORM
:
1266 x8r8g8b8_put_tile_rgba((unsigned *) packed
, w
, h
, p
, src_stride
);
1268 case PIPE_FORMAT_B8G8R8A8_UNORM
:
1269 b8g8r8a8_put_tile_rgba((unsigned *) packed
, w
, h
, p
, src_stride
);
1271 case PIPE_FORMAT_A1R5G5B5_UNORM
:
1272 a1r5g5b5_put_tile_rgba((ushort
*) packed
, w
, h
, p
, src_stride
);
1274 case PIPE_FORMAT_R5G6B5_UNORM
:
1275 r5g6b5_put_tile_rgba((ushort
*) packed
, w
, h
, p
, src_stride
);
1277 case PIPE_FORMAT_R8G8B8_UNORM
:
1278 r8g8b8_put_tile_rgba((ubyte
*) packed
, w
, h
, p
, src_stride
);
1280 case PIPE_FORMAT_R8G8B8A8_UNORM
:
1283 case PIPE_FORMAT_A4R4G4B4_UNORM
:
1284 a4r4g4b4_put_tile_rgba((ushort
*) packed
, w
, h
, p
, src_stride
);
1286 case PIPE_FORMAT_L8_UNORM
:
1287 l8_put_tile_rgba((ubyte
*) packed
, w
, h
, p
, src_stride
);
1289 case PIPE_FORMAT_A8_UNORM
:
1290 a8_put_tile_rgba((ubyte
*) packed
, w
, h
, p
, src_stride
);
1292 case PIPE_FORMAT_I8_UNORM
:
1293 i8_put_tile_rgba((ubyte
*) packed
, w
, h
, p
, src_stride
);
1295 case PIPE_FORMAT_A8L8_UNORM
:
1296 a8l8_put_tile_rgba((ushort
*) packed
, w
, h
, p
, src_stride
);
1298 case PIPE_FORMAT_R16_SNORM
:
1299 r16_put_tile_rgba((short *) packed
, w
, h
, p
, src_stride
);
1301 case PIPE_FORMAT_R16G16B16A16_SNORM
:
1302 r16g16b16a16_put_tile_rgba((short *) packed
, w
, h
, p
, src_stride
);
1304 case PIPE_FORMAT_A8R8G8B8_SRGB
:
1305 a8r8g8b8_srgb_put_tile_rgba((unsigned *) packed
, w
, h
, p
, src_stride
);
1307 case PIPE_FORMAT_A8L8_SRGB
:
1308 a8l8_srgb_put_tile_rgba((ushort
*) packed
, w
, h
, p
, src_stride
);
1310 case PIPE_FORMAT_L8_SRGB
:
1311 l8_srgb_put_tile_rgba((ubyte
*) packed
, w
, h
, p
, src_stride
);
1313 case PIPE_FORMAT_Z16_UNORM
:
1314 /*z16_put_tile_rgba((ushort *) packed, w, h, p, src_stride);*/
1316 case PIPE_FORMAT_Z32_UNORM
:
1317 /*z32_put_tile_rgba((unsigned *) packed, w, h, p, src_stride);*/
1319 case PIPE_FORMAT_S8Z24_UNORM
:
1320 case PIPE_FORMAT_X8Z24_UNORM
:
1321 /*s8z24_put_tile_rgba((unsigned *) packed, w, h, p, src_stride);*/
1323 case PIPE_FORMAT_Z24S8_UNORM
:
1324 case PIPE_FORMAT_Z24X8_UNORM
:
1325 /*z24s8_put_tile_rgba((unsigned *) packed, w, h, p, src_stride);*/
1328 debug_printf("%s: unsupported format %s\n", __FUNCTION__
, pf_name(format
));
1331 pipe_put_tile_raw(pt
, x
, y
, w
, h
, packed
, 0);
1338 * Get a block of Z values, converted to 32-bit range.
1341 pipe_get_tile_z(struct pipe_transfer
*pt
,
1342 uint x
, uint y
, uint w
, uint h
,
1345 struct pipe_screen
*screen
= pt
->texture
->screen
;
1346 const uint dstStride
= w
;
1350 enum pipe_format format
= pt
->texture
->format
;
1352 if (pipe_clip_tile(x
, y
, &w
, &h
, pt
))
1355 map
= (ubyte
*)screen
->transfer_map(screen
, pt
);
1362 case PIPE_FORMAT_Z32_UNORM
:
1365 = (const uint
*)(map
+ y
* pt
->stride
+ x
*4);
1366 for (i
= 0; i
< h
; i
++) {
1367 memcpy(pDest
, ptrc
, 4 * w
);
1369 ptrc
+= pt
->stride
/4;
1373 case PIPE_FORMAT_S8Z24_UNORM
:
1374 case PIPE_FORMAT_X8Z24_UNORM
:
1377 = (const uint
*)(map
+ y
* pt
->stride
+ x
*4);
1378 for (i
= 0; i
< h
; i
++) {
1379 for (j
= 0; j
< w
; j
++) {
1380 /* convert 24-bit Z to 32-bit Z */
1381 pDest
[j
] = (ptrc
[j
] << 8) | ((ptrc
[j
] >> 16) & 0xff);
1384 ptrc
+= pt
->stride
/4;
1388 case PIPE_FORMAT_Z24S8_UNORM
:
1389 case PIPE_FORMAT_Z24X8_UNORM
:
1392 = (const uint
*)(map
+ y
* pt
->stride
+ x
*4);
1393 for (i
= 0; i
< h
; i
++) {
1394 for (j
= 0; j
< w
; j
++) {
1395 /* convert 24-bit Z to 32-bit Z */
1396 pDest
[j
] = (ptrc
[j
] & 0xffffff00) | ((ptrc
[j
] >> 24) & 0xff);
1399 ptrc
+= pt
->stride
/4;
1403 case PIPE_FORMAT_Z16_UNORM
:
1406 = (const ushort
*)(map
+ y
* pt
->stride
+ x
*2);
1407 for (i
= 0; i
< h
; i
++) {
1408 for (j
= 0; j
< w
; j
++) {
1409 /* convert 16-bit Z to 32-bit Z */
1410 pDest
[j
] = (ptrc
[j
] << 16) | ptrc
[j
];
1413 ptrc
+= pt
->stride
/2;
1421 screen
->transfer_unmap(screen
, pt
);
1426 pipe_put_tile_z(struct pipe_transfer
*pt
,
1427 uint x
, uint y
, uint w
, uint h
,
1430 struct pipe_screen
*screen
= pt
->texture
->screen
;
1431 const uint srcStride
= w
;
1432 const uint
*ptrc
= zSrc
;
1435 enum pipe_format format
= pt
->texture
->format
;
1437 if (pipe_clip_tile(x
, y
, &w
, &h
, pt
))
1440 map
= (ubyte
*)screen
->transfer_map(screen
, pt
);
1447 case PIPE_FORMAT_Z32_UNORM
:
1449 uint
*pDest
= (uint
*) (map
+ y
* pt
->stride
+ x
*4);
1450 for (i
= 0; i
< h
; i
++) {
1451 memcpy(pDest
, ptrc
, 4 * w
);
1452 pDest
+= pt
->stride
/4;
1457 case PIPE_FORMAT_S8Z24_UNORM
:
1459 uint
*pDest
= (uint
*) (map
+ y
* pt
->stride
+ x
*4);
1460 assert((pt
->usage
& PIPE_TRANSFER_READ_WRITE
) == PIPE_TRANSFER_READ_WRITE
);
1461 for (i
= 0; i
< h
; i
++) {
1462 for (j
= 0; j
< w
; j
++) {
1463 /* convert 32-bit Z to 24-bit Z, preserve stencil */
1464 pDest
[j
] = (pDest
[j
] & 0xff000000) | ptrc
[j
] >> 8;
1466 pDest
+= pt
->stride
/4;
1471 case PIPE_FORMAT_X8Z24_UNORM
:
1473 uint
*pDest
= (uint
*) (map
+ y
* pt
->stride
+ x
*4);
1474 for (i
= 0; i
< h
; i
++) {
1475 for (j
= 0; j
< w
; j
++) {
1476 /* convert 32-bit Z to 24-bit Z (0 stencil) */
1477 pDest
[j
] = ptrc
[j
] >> 8;
1479 pDest
+= pt
->stride
/4;
1484 case PIPE_FORMAT_Z24S8_UNORM
:
1486 uint
*pDest
= (uint
*) (map
+ y
* pt
->stride
+ x
*4);
1487 assert((pt
->usage
& PIPE_TRANSFER_READ_WRITE
) == PIPE_TRANSFER_READ_WRITE
);
1488 for (i
= 0; i
< h
; i
++) {
1489 for (j
= 0; j
< w
; j
++) {
1490 /* convert 32-bit Z to 24-bit Z, preserve stencil */
1491 pDest
[j
] = (pDest
[j
] & 0xff) | (ptrc
[j
] & 0xffffff00);
1493 pDest
+= pt
->stride
/4;
1498 case PIPE_FORMAT_Z24X8_UNORM
:
1500 uint
*pDest
= (uint
*) (map
+ y
* pt
->stride
+ x
*4);
1501 for (i
= 0; i
< h
; i
++) {
1502 for (j
= 0; j
< w
; j
++) {
1503 /* convert 32-bit Z to 24-bit Z (0 stencil) */
1504 pDest
[j
] = ptrc
[j
] & 0xffffff00;
1506 pDest
+= pt
->stride
/4;
1511 case PIPE_FORMAT_Z16_UNORM
:
1513 ushort
*pDest
= (ushort
*) (map
+ y
* pt
->stride
+ x
*2);
1514 for (i
= 0; i
< h
; i
++) {
1515 for (j
= 0; j
< w
; j
++) {
1516 /* convert 32-bit Z to 16-bit Z */
1517 pDest
[j
] = ptrc
[j
] >> 16;
1519 pDest
+= pt
->stride
/2;
1528 screen
->transfer_unmap(screen
, pt
);