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_R8G8B8A8_UNORM ***/
253 r8g8b8a8_get_tile_rgba(const unsigned *src
,
254 unsigned w
, unsigned h
,
260 for (i
= 0; i
< h
; i
++) {
262 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
263 const unsigned pixel
= *src
++;
264 pRow
[0] = ubyte_to_float((pixel
>> 24) & 0xff);
265 pRow
[1] = ubyte_to_float((pixel
>> 16) & 0xff);
266 pRow
[2] = ubyte_to_float((pixel
>> 8) & 0xff);
267 pRow
[3] = ubyte_to_float((pixel
>> 0) & 0xff);
275 r8g8b8a8_put_tile_rgba(unsigned *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 *dst
++ = (r
<< 24) | (g
<< 16) | (b
<< 8) | a
;
297 /*** PIPE_FORMAT_A1R5G5B5_UNORM ***/
300 a1r5g5b5_get_tile_rgba(const ushort
*src
,
301 unsigned w
, unsigned h
,
307 for (i
= 0; i
< h
; i
++) {
309 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
310 const ushort pixel
= *src
++;
311 pRow
[0] = ((pixel
>> 10) & 0x1f) * (1.0f
/ 31.0f
);
312 pRow
[1] = ((pixel
>> 5) & 0x1f) * (1.0f
/ 31.0f
);
313 pRow
[2] = ((pixel
) & 0x1f) * (1.0f
/ 31.0f
);
314 pRow
[3] = ((pixel
>> 15) ) * 1.0f
;
322 a1r5g5b5_put_tile_rgba(ushort
*dst
,
323 unsigned w
, unsigned h
,
329 for (i
= 0; i
< h
; i
++) {
330 const float *pRow
= p
;
331 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
333 r
= float_to_ubyte(pRow
[0]);
334 g
= float_to_ubyte(pRow
[1]);
335 b
= float_to_ubyte(pRow
[2]);
336 a
= float_to_ubyte(pRow
[3]);
337 r
= r
>> 3; /* 5 bits */
338 g
= g
>> 3; /* 5 bits */
339 b
= b
>> 3; /* 5 bits */
340 a
= a
>> 7; /* 1 bit */
341 *dst
++ = (a
<< 15) | (r
<< 10) | (g
<< 5) | b
;
348 /*** PIPE_FORMAT_A4R4G4B4_UNORM ***/
351 a4r4g4b4_get_tile_rgba(const ushort
*src
,
352 unsigned w
, unsigned h
,
358 for (i
= 0; i
< h
; i
++) {
360 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
361 const ushort pixel
= *src
++;
362 pRow
[0] = ((pixel
>> 8) & 0xf) * (1.0f
/ 15.0f
);
363 pRow
[1] = ((pixel
>> 4) & 0xf) * (1.0f
/ 15.0f
);
364 pRow
[2] = ((pixel
) & 0xf) * (1.0f
/ 15.0f
);
365 pRow
[3] = ((pixel
>> 12) ) * (1.0f
/ 15.0f
);
373 a4r4g4b4_put_tile_rgba(ushort
*dst
,
374 unsigned w
, unsigned h
,
380 for (i
= 0; i
< h
; i
++) {
381 const float *pRow
= p
;
382 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
384 r
= float_to_ubyte(pRow
[0]);
385 g
= float_to_ubyte(pRow
[1]);
386 b
= float_to_ubyte(pRow
[2]);
387 a
= float_to_ubyte(pRow
[3]);
392 *dst
++ = (a
<< 12) | (r
<< 16) | (g
<< 4) | b
;
399 /*** PIPE_FORMAT_R5G6B5_UNORM ***/
402 r5g6b5_get_tile_rgba(const ushort
*src
,
403 unsigned w
, unsigned h
,
409 for (i
= 0; i
< h
; i
++) {
411 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
412 const ushort pixel
= *src
++;
413 pRow
[0] = ((pixel
>> 11) & 0x1f) * (1.0f
/ 31.0f
);
414 pRow
[1] = ((pixel
>> 5) & 0x3f) * (1.0f
/ 63.0f
);
415 pRow
[2] = ((pixel
) & 0x1f) * (1.0f
/ 31.0f
);
424 r5g6b5_put_tile_rgba(ushort
*dst
,
425 unsigned w
, unsigned h
,
431 for (i
= 0; i
< h
; i
++) {
432 const float *pRow
= p
;
433 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
434 uint r
= (uint
) (CLAMP(pRow
[0], 0.0, 1.0) * 31.0);
435 uint g
= (uint
) (CLAMP(pRow
[1], 0.0, 1.0) * 63.0);
436 uint b
= (uint
) (CLAMP(pRow
[2], 0.0, 1.0) * 31.0);
437 *dst
++ = (r
<< 11) | (g
<< 5) | (b
);
445 /*** PIPE_FORMAT_R8G8B8_UNORM ***/
448 r8g8b8_get_tile_rgba(const ubyte
*src
,
449 unsigned w
, unsigned h
,
455 for (i
= 0; i
< h
; i
++) {
457 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
458 pRow
[0] = ubyte_to_float(src
[0]);
459 pRow
[1] = ubyte_to_float(src
[1]);
460 pRow
[2] = ubyte_to_float(src
[2]);
470 r8g8b8_put_tile_rgba(ubyte
*dst
,
471 unsigned w
, unsigned h
,
477 for (i
= 0; i
< h
; i
++) {
478 const float *pRow
= p
;
479 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
480 dst
[0] = float_to_ubyte(pRow
[0]);
481 dst
[1] = float_to_ubyte(pRow
[1]);
482 dst
[2] = float_to_ubyte(pRow
[2]);
491 /*** PIPE_FORMAT_Z16_UNORM ***/
494 * Return each Z value as four floats in [0,1].
497 z16_get_tile_rgba(const ushort
*src
,
498 unsigned w
, unsigned h
,
502 const float scale
= 1.0f
/ 65535.0f
;
505 for (i
= 0; i
< h
; i
++) {
507 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
511 pRow
[3] = *src
++ * scale
;
520 /*** PIPE_FORMAT_L8_UNORM ***/
523 l8_get_tile_rgba(const ubyte
*src
,
524 unsigned w
, unsigned h
,
530 for (i
= 0; i
< h
; i
++) {
532 for (j
= 0; j
< w
; j
++, src
++, pRow
+= 4) {
535 pRow
[2] = ubyte_to_float(*src
);
544 l8_put_tile_rgba(ubyte
*dst
,
545 unsigned w
, unsigned h
,
551 for (i
= 0; i
< h
; i
++) {
552 const float *pRow
= p
;
553 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
555 r
= float_to_ubyte(pRow
[0]);
564 /*** PIPE_FORMAT_A8_UNORM ***/
567 a8_get_tile_rgba(const ubyte
*src
,
568 unsigned w
, unsigned h
,
574 for (i
= 0; i
< h
; i
++) {
576 for (j
= 0; j
< w
; j
++, src
++, pRow
+= 4) {
580 pRow
[3] = ubyte_to_float(*src
);
588 a8_put_tile_rgba(ubyte
*dst
,
589 unsigned w
, unsigned h
,
595 for (i
= 0; i
< h
; i
++) {
596 const float *pRow
= p
;
597 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
599 a
= float_to_ubyte(pRow
[3]);
608 /*** PIPE_FORMAT_R16_SNORM ***/
611 r16_get_tile_rgba(const short *src
,
612 unsigned w
, unsigned h
,
618 for (i
= 0; i
< h
; i
++) {
620 for (j
= 0; j
< w
; j
++, src
++, pRow
+= 4) {
621 pRow
[0] = SHORT_TO_FLOAT(src
[0]);
632 r16_put_tile_rgba(short *dst
,
633 unsigned w
, unsigned h
,
639 for (i
= 0; i
< h
; i
++) {
640 const float *pRow
= p
;
641 for (j
= 0; j
< w
; j
++, dst
++, pRow
+= 4) {
642 UNCLAMPED_FLOAT_TO_SHORT(dst
[0], pRow
[0]);
649 /*** PIPE_FORMAT_R16G16B16A16_SNORM ***/
652 r16g16b16a16_get_tile_rgba(const short *src
,
653 unsigned w
, unsigned h
,
659 for (i
= 0; i
< h
; i
++) {
661 for (j
= 0; j
< w
; j
++, src
+= 4, pRow
+= 4) {
662 pRow
[0] = SHORT_TO_FLOAT(src
[0]);
663 pRow
[1] = SHORT_TO_FLOAT(src
[1]);
664 pRow
[2] = SHORT_TO_FLOAT(src
[2]);
665 pRow
[3] = SHORT_TO_FLOAT(src
[3]);
673 r16g16b16a16_put_tile_rgba(short *dst
,
674 unsigned w
, unsigned h
,
680 for (i
= 0; i
< h
; i
++) {
681 const float *pRow
= p
;
682 for (j
= 0; j
< w
; j
++, dst
+= 4, pRow
+= 4) {
683 UNCLAMPED_FLOAT_TO_SHORT(dst
[0], pRow
[0]);
684 UNCLAMPED_FLOAT_TO_SHORT(dst
[1], pRow
[1]);
685 UNCLAMPED_FLOAT_TO_SHORT(dst
[2], pRow
[2]);
686 UNCLAMPED_FLOAT_TO_SHORT(dst
[3], pRow
[3]);
693 /*** PIPE_FORMAT_R8G8B8A8_SRGB ***/
696 * Convert an 8-bit sRGB value from non-linear space to a
697 * linear RGB value in [0, 1].
698 * Implemented with a 256-entry lookup table.
701 srgb_to_linear(ubyte cs8
)
703 static float table
[256];
704 static boolean tableReady
= FALSE
;
706 /* compute lookup table now */
708 for (i
= 0; i
< 256; i
++) {
709 const float cs
= ubyte_to_float(i
);
711 table
[i
] = cs
/ 12.92f
;
714 table
[i
] = (float) powf((cs
+ 0.055) / 1.055, 2.4);
724 * Convert linear float in [0,1] to an srgb ubyte value in [0,255].
725 * XXX this hasn't been tested (render to srgb surface).
726 * XXX this needs optimization.
729 linear_to_srgb(float cl
)
733 else if (cl
>= 0.0031308F
)
734 return float_to_ubyte(1.055F
* powf(cl
, 0.41666F
) - 0.055F
);
736 return float_to_ubyte(12.92F
* cl
);
743 a8r8g8b8_srgb_get_tile_rgba(const unsigned *src
,
744 unsigned w
, unsigned h
,
750 for (i
= 0; i
< h
; i
++) {
752 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
753 const unsigned pixel
= *src
++;
754 pRow
[0] = srgb_to_linear((pixel
>> 16) & 0xff);
755 pRow
[1] = srgb_to_linear((pixel
>> 8) & 0xff);
756 pRow
[2] = srgb_to_linear((pixel
>> 0) & 0xff);
757 pRow
[3] = ubyte_to_float((pixel
>> 24) & 0xff);
764 a8r8g8b8_srgb_put_tile_rgba(unsigned *dst
,
765 unsigned w
, unsigned h
,
771 for (i
= 0; i
< h
; i
++) {
772 const float *pRow
= p
;
773 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
775 r
= linear_to_srgb(pRow
[0]);
776 g
= linear_to_srgb(pRow
[1]);
777 b
= linear_to_srgb(pRow
[2]);
778 a
= float_to_ubyte(pRow
[3]);
779 *dst
++ = (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
786 /*** PIPE_FORMAT_A8L8_SRGB ***/
789 a8l8_srgb_get_tile_rgba(const ushort
*src
,
790 unsigned w
, unsigned h
,
796 for (i
= 0; i
< h
; i
++) {
798 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
802 pRow
[2] = srgb_to_linear(p
& 0xff);
803 pRow
[3] = ubyte_to_float(p
>> 8);
810 a8l8_srgb_put_tile_rgba(ushort
*dst
,
811 unsigned w
, unsigned h
,
817 for (i
= 0; i
< h
; i
++) {
818 const float *pRow
= p
;
819 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
821 r
= linear_to_srgb(pRow
[0]);
822 a
= float_to_ubyte(pRow
[3]);
823 *dst
++ = (a
<< 8) | r
;
830 /*** PIPE_FORMAT_L8_SRGB ***/
833 l8_srgb_get_tile_rgba(const ubyte
*src
,
834 unsigned w
, unsigned h
,
840 for (i
= 0; i
< h
; i
++) {
842 for (j
= 0; j
< w
; j
++, src
++, pRow
+= 4) {
845 pRow
[2] = srgb_to_linear(*src
);
853 l8_srgb_put_tile_rgba(ubyte
*dst
,
854 unsigned w
, unsigned h
,
860 for (i
= 0; i
< h
; i
++) {
861 const float *pRow
= p
;
862 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
864 r
= linear_to_srgb(pRow
[0]);
872 /*** PIPE_FORMAT_I8_UNORM ***/
875 i8_get_tile_rgba(const ubyte
*src
,
876 unsigned w
, unsigned h
,
882 for (i
= 0; i
< h
; i
++) {
884 for (j
= 0; j
< w
; j
++, src
++, pRow
+= 4) {
888 pRow
[3] = ubyte_to_float(*src
);
896 i8_put_tile_rgba(ubyte
*dst
,
897 unsigned w
, unsigned h
,
903 for (i
= 0; i
< h
; i
++) {
904 const float *pRow
= p
;
905 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
907 r
= float_to_ubyte(pRow
[0]);
915 /*** PIPE_FORMAT_A8L8_UNORM ***/
918 a8l8_get_tile_rgba(const ushort
*src
,
919 unsigned w
, unsigned h
,
925 for (i
= 0; i
< h
; i
++) {
927 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
931 pRow
[2] = ubyte_to_float(p
& 0xff);
932 pRow
[3] = ubyte_to_float(p
>> 8);
940 a8l8_put_tile_rgba(ushort
*dst
,
941 unsigned w
, unsigned h
,
947 for (i
= 0; i
< h
; i
++) {
948 const float *pRow
= p
;
949 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
951 r
= float_to_ubyte(pRow
[0]);
952 a
= float_to_ubyte(pRow
[3]);
953 *dst
++ = (a
<< 8) | r
;
962 /*** PIPE_FORMAT_Z32_UNORM ***/
965 * Return each Z value as four floats in [0,1].
968 z32_get_tile_rgba(const unsigned *src
,
969 unsigned w
, unsigned h
,
973 const double scale
= 1.0 / (double) 0xffffffff;
976 for (i
= 0; i
< h
; i
++) {
978 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
982 pRow
[3] = (float) (*src
++ * scale
);
989 /*** PIPE_FORMAT_S8Z24_UNORM ***/
992 * Return Z component as four float in [0,1]. Stencil part ignored.
995 s8z24_get_tile_rgba(const unsigned *src
,
996 unsigned w
, unsigned h
,
1000 const double scale
= 1.0 / ((1 << 24) - 1);
1003 for (i
= 0; i
< h
; i
++) {
1005 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
1009 pRow
[3] = (float) (scale
* (*src
++ & 0xffffff));
1016 /*** PIPE_FORMAT_Z24S8_UNORM ***/
1019 * Return Z component as four float in [0,1]. Stencil part ignored.
1022 z24s8_get_tile_rgba(const unsigned *src
,
1023 unsigned w
, unsigned h
,
1025 unsigned dst_stride
)
1027 const double scale
= 1.0 / ((1 << 24) - 1);
1030 for (i
= 0; i
< h
; i
++) {
1032 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
1036 pRow
[3] = (float) (scale
* (*src
++ >> 8));
1043 /*** PIPE_FORMAT_Z32_FLOAT ***/
1046 * Return each Z value as four floats in [0,1].
1049 z32f_get_tile_rgba(const float *src
,
1050 unsigned w
, unsigned h
,
1052 unsigned dst_stride
)
1056 for (i
= 0; i
< h
; i
++) {
1058 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
1069 /*** PIPE_FORMAT_YCBCR / PIPE_FORMAT_YCBCR_REV ***/
1072 * Convert YCbCr (or YCrCb) to RGBA.
1075 ycbcr_get_tile_rgba(const ushort
*src
,
1076 unsigned w
, unsigned h
,
1078 unsigned dst_stride
,
1081 const float scale
= 1.0f
/ 255.0f
;
1084 for (i
= 0; i
< h
; i
++) {
1086 /* do two texels at a time */
1087 for (j
= 0; j
< (w
& ~1); j
+= 2, src
+= 2) {
1088 const ushort t0
= src
[0];
1089 const ushort t1
= src
[1];
1090 const ubyte y0
= (t0
>> 8) & 0xff; /* luminance */
1091 const ubyte y1
= (t1
>> 8) & 0xff; /* luminance */
1096 cb
= t1
& 0xff; /* chroma U */
1097 cr
= t0
& 0xff; /* chroma V */
1100 cb
= t0
& 0xff; /* chroma U */
1101 cr
= t1
& 0xff; /* chroma V */
1104 /* even pixel: y0,cr,cb */
1105 r
= 1.164f
* (y0
-16) + 1.596f
* (cr
-128);
1106 g
= 1.164f
* (y0
-16) - 0.813f
* (cr
-128) - 0.391f
* (cb
-128);
1107 b
= 1.164f
* (y0
-16) + 2.018f
* (cb
-128);
1108 pRow
[0] = r
* scale
;
1109 pRow
[1] = g
* scale
;
1110 pRow
[2] = b
* scale
;
1114 /* odd pixel: use y1,cr,cb */
1115 r
= 1.164f
* (y1
-16) + 1.596f
* (cr
-128);
1116 g
= 1.164f
* (y1
-16) - 0.813f
* (cr
-128) - 0.391f
* (cb
-128);
1117 b
= 1.164f
* (y1
-16) + 2.018f
* (cb
-128);
1118 pRow
[0] = r
* scale
;
1119 pRow
[1] = g
* scale
;
1120 pRow
[2] = b
* scale
;
1125 /* do the last texel */
1127 const ushort t0
= src
[0];
1128 const ushort t1
= src
[1];
1129 const ubyte y0
= (t0
>> 8) & 0xff; /* luminance */
1134 cb
= t1
& 0xff; /* chroma U */
1135 cr
= t0
& 0xff; /* chroma V */
1138 cb
= t0
& 0xff; /* chroma U */
1139 cr
= t1
& 0xff; /* chroma V */
1142 /* even pixel: y0,cr,cb */
1143 r
= 1.164f
* (y0
-16) + 1.596f
* (cr
-128);
1144 g
= 1.164f
* (y0
-16) - 0.813f
* (cr
-128) - 0.391f
* (cb
-128);
1145 b
= 1.164f
* (y0
-16) + 2.018f
* (cb
-128);
1146 pRow
[0] = r
* scale
;
1147 pRow
[1] = g
* scale
;
1148 pRow
[2] = b
* scale
;
1158 fake_get_tile_rgba(const ushort
*src
,
1159 unsigned w
, unsigned h
,
1161 unsigned dst_stride
)
1165 for (i
= 0; i
< h
; i
++) {
1167 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
1171 pRow
[3] = (i
^ j
) & 1 ? 1.0f
: 0.0f
;
1179 pipe_tile_raw_to_rgba(enum pipe_format format
,
1182 float *dst
, unsigned dst_stride
)
1185 case PIPE_FORMAT_A8R8G8B8_UNORM
:
1186 a8r8g8b8_get_tile_rgba((unsigned *) src
, w
, h
, dst
, dst_stride
);
1188 case PIPE_FORMAT_X8R8G8B8_UNORM
:
1189 x8r8g8b8_get_tile_rgba((unsigned *) src
, w
, h
, dst
, dst_stride
);
1191 case PIPE_FORMAT_B8G8R8A8_UNORM
:
1192 b8g8r8a8_get_tile_rgba((unsigned *) src
, w
, h
, dst
, dst_stride
);
1194 case PIPE_FORMAT_R8G8B8A8_UNORM
:
1195 r8g8b8a8_get_tile_rgba((unsigned *) src
, w
, h
, dst
, dst_stride
);
1197 case PIPE_FORMAT_A1R5G5B5_UNORM
:
1198 a1r5g5b5_get_tile_rgba((ushort
*) src
, w
, h
, dst
, dst_stride
);
1200 case PIPE_FORMAT_A4R4G4B4_UNORM
:
1201 a4r4g4b4_get_tile_rgba((ushort
*) src
, w
, h
, dst
, dst_stride
);
1203 case PIPE_FORMAT_R5G6B5_UNORM
:
1204 r5g6b5_get_tile_rgba((ushort
*) src
, w
, h
, dst
, dst_stride
);
1206 case PIPE_FORMAT_R8G8B8_UNORM
:
1207 r8g8b8_get_tile_rgba((ubyte
*) src
, w
, h
, dst
, dst_stride
);
1209 case PIPE_FORMAT_L8_UNORM
:
1210 l8_get_tile_rgba((ubyte
*) src
, w
, h
, dst
, dst_stride
);
1212 case PIPE_FORMAT_A8_UNORM
:
1213 a8_get_tile_rgba((ubyte
*) src
, w
, h
, dst
, dst_stride
);
1215 case PIPE_FORMAT_I8_UNORM
:
1216 i8_get_tile_rgba((ubyte
*) src
, w
, h
, dst
, dst_stride
);
1218 case PIPE_FORMAT_A8L8_UNORM
:
1219 a8l8_get_tile_rgba((ushort
*) src
, w
, h
, dst
, dst_stride
);
1221 case PIPE_FORMAT_R16_SNORM
:
1222 r16_get_tile_rgba((short *) src
, w
, h
, dst
, dst_stride
);
1224 case PIPE_FORMAT_R16G16B16A16_SNORM
:
1225 r16g16b16a16_get_tile_rgba((short *) src
, w
, h
, dst
, dst_stride
);
1227 case PIPE_FORMAT_A8R8G8B8_SRGB
:
1228 a8r8g8b8_srgb_get_tile_rgba((unsigned *) src
, w
, h
, dst
, dst_stride
);
1230 case PIPE_FORMAT_A8L8_SRGB
:
1231 a8l8_srgb_get_tile_rgba((ushort
*) src
, w
, h
, dst
, dst_stride
);
1233 case PIPE_FORMAT_L8_SRGB
:
1234 l8_srgb_get_tile_rgba((ubyte
*) src
, w
, h
, dst
, dst_stride
);
1236 case PIPE_FORMAT_Z16_UNORM
:
1237 z16_get_tile_rgba((ushort
*) src
, w
, h
, dst
, dst_stride
);
1239 case PIPE_FORMAT_Z32_UNORM
:
1240 z32_get_tile_rgba((unsigned *) src
, w
, h
, dst
, dst_stride
);
1242 case PIPE_FORMAT_S8Z24_UNORM
:
1243 case PIPE_FORMAT_X8Z24_UNORM
:
1244 s8z24_get_tile_rgba((unsigned *) src
, w
, h
, dst
, dst_stride
);
1246 case PIPE_FORMAT_Z24S8_UNORM
:
1247 case PIPE_FORMAT_Z24X8_UNORM
:
1248 z24s8_get_tile_rgba((unsigned *) src
, w
, h
, dst
, dst_stride
);
1250 case PIPE_FORMAT_Z32_FLOAT
:
1251 z32f_get_tile_rgba((float *) src
, w
, h
, dst
, dst_stride
);
1253 case PIPE_FORMAT_YCBCR
:
1254 ycbcr_get_tile_rgba((ushort
*) src
, w
, h
, dst
, dst_stride
, FALSE
);
1256 case PIPE_FORMAT_YCBCR_REV
:
1257 ycbcr_get_tile_rgba((ushort
*) src
, w
, h
, dst
, dst_stride
, TRUE
);
1260 debug_printf("%s: unsupported format %s\n", __FUNCTION__
, pf_name(format
));
1261 fake_get_tile_rgba(src
, w
, h
, dst
, dst_stride
);
1267 pipe_get_tile_rgba(struct pipe_transfer
*pt
,
1268 uint x
, uint y
, uint w
, uint h
,
1271 unsigned dst_stride
= w
* 4;
1273 enum pipe_format format
= pt
->texture
->format
;
1275 if (pipe_clip_tile(x
, y
, &w
, &h
, pt
))
1278 packed
= MALLOC(pf_get_nblocks(format
, w
, h
) * pf_get_blocksize(format
));
1283 if(format
== PIPE_FORMAT_YCBCR
|| format
== PIPE_FORMAT_YCBCR_REV
)
1284 assert((x
& 1) == 0);
1286 pipe_get_tile_raw(pt
, x
, y
, w
, h
, packed
, 0);
1288 pipe_tile_raw_to_rgba(format
, packed
, w
, h
, p
, dst_stride
);
1295 pipe_put_tile_rgba(struct pipe_transfer
*pt
,
1296 uint x
, uint y
, uint w
, uint h
,
1299 unsigned src_stride
= w
* 4;
1301 enum pipe_format format
= pt
->texture
->format
;
1303 if (pipe_clip_tile(x
, y
, &w
, &h
, pt
))
1306 packed
= MALLOC(pf_get_nblocks(format
, w
, h
) * pf_get_blocksize(format
));
1312 case PIPE_FORMAT_A8R8G8B8_UNORM
:
1313 a8r8g8b8_put_tile_rgba((unsigned *) packed
, w
, h
, p
, src_stride
);
1315 case PIPE_FORMAT_X8R8G8B8_UNORM
:
1316 x8r8g8b8_put_tile_rgba((unsigned *) packed
, w
, h
, p
, src_stride
);
1318 case PIPE_FORMAT_B8G8R8A8_UNORM
:
1319 b8g8r8a8_put_tile_rgba((unsigned *) packed
, w
, h
, p
, src_stride
);
1321 case PIPE_FORMAT_R8G8B8A8_UNORM
:
1322 r8g8b8a8_put_tile_rgba((unsigned *) packed
, w
, h
, p
, src_stride
);
1324 case PIPE_FORMAT_A1R5G5B5_UNORM
:
1325 a1r5g5b5_put_tile_rgba((ushort
*) packed
, w
, h
, p
, src_stride
);
1327 case PIPE_FORMAT_R5G6B5_UNORM
:
1328 r5g6b5_put_tile_rgba((ushort
*) packed
, w
, h
, p
, src_stride
);
1330 case PIPE_FORMAT_R8G8B8_UNORM
:
1331 r8g8b8_put_tile_rgba((ubyte
*) packed
, w
, h
, p
, src_stride
);
1333 case PIPE_FORMAT_A4R4G4B4_UNORM
:
1334 a4r4g4b4_put_tile_rgba((ushort
*) packed
, w
, h
, p
, src_stride
);
1336 case PIPE_FORMAT_L8_UNORM
:
1337 l8_put_tile_rgba((ubyte
*) packed
, w
, h
, p
, src_stride
);
1339 case PIPE_FORMAT_A8_UNORM
:
1340 a8_put_tile_rgba((ubyte
*) packed
, w
, h
, p
, src_stride
);
1342 case PIPE_FORMAT_I8_UNORM
:
1343 i8_put_tile_rgba((ubyte
*) packed
, w
, h
, p
, src_stride
);
1345 case PIPE_FORMAT_A8L8_UNORM
:
1346 a8l8_put_tile_rgba((ushort
*) packed
, w
, h
, p
, src_stride
);
1348 case PIPE_FORMAT_R16_SNORM
:
1349 r16_put_tile_rgba((short *) packed
, w
, h
, p
, src_stride
);
1351 case PIPE_FORMAT_R16G16B16A16_SNORM
:
1352 r16g16b16a16_put_tile_rgba((short *) packed
, w
, h
, p
, src_stride
);
1354 case PIPE_FORMAT_A8R8G8B8_SRGB
:
1355 a8r8g8b8_srgb_put_tile_rgba((unsigned *) packed
, w
, h
, p
, src_stride
);
1357 case PIPE_FORMAT_A8L8_SRGB
:
1358 a8l8_srgb_put_tile_rgba((ushort
*) packed
, w
, h
, p
, src_stride
);
1360 case PIPE_FORMAT_L8_SRGB
:
1361 l8_srgb_put_tile_rgba((ubyte
*) packed
, w
, h
, p
, src_stride
);
1363 case PIPE_FORMAT_Z16_UNORM
:
1364 /*z16_put_tile_rgba((ushort *) packed, w, h, p, src_stride);*/
1366 case PIPE_FORMAT_Z32_UNORM
:
1367 /*z32_put_tile_rgba((unsigned *) packed, w, h, p, src_stride);*/
1369 case PIPE_FORMAT_S8Z24_UNORM
:
1370 case PIPE_FORMAT_X8Z24_UNORM
:
1371 /*s8z24_put_tile_rgba((unsigned *) packed, w, h, p, src_stride);*/
1373 case PIPE_FORMAT_Z24S8_UNORM
:
1374 case PIPE_FORMAT_Z24X8_UNORM
:
1375 /*z24s8_put_tile_rgba((unsigned *) packed, w, h, p, src_stride);*/
1378 debug_printf("%s: unsupported format %s\n", __FUNCTION__
, pf_name(format
));
1381 pipe_put_tile_raw(pt
, x
, y
, w
, h
, packed
, 0);
1388 * Get a block of Z values, converted to 32-bit range.
1391 pipe_get_tile_z(struct pipe_transfer
*pt
,
1392 uint x
, uint y
, uint w
, uint h
,
1395 struct pipe_screen
*screen
= pt
->texture
->screen
;
1396 const uint dstStride
= w
;
1400 enum pipe_format format
= pt
->texture
->format
;
1402 if (pipe_clip_tile(x
, y
, &w
, &h
, pt
))
1405 map
= (ubyte
*)screen
->transfer_map(screen
, pt
);
1412 case PIPE_FORMAT_Z32_UNORM
:
1415 = (const uint
*)(map
+ y
* pt
->stride
+ x
*4);
1416 for (i
= 0; i
< h
; i
++) {
1417 memcpy(pDest
, ptrc
, 4 * w
);
1419 ptrc
+= pt
->stride
/4;
1423 case PIPE_FORMAT_S8Z24_UNORM
:
1424 case PIPE_FORMAT_X8Z24_UNORM
:
1427 = (const uint
*)(map
+ y
* pt
->stride
+ x
*4);
1428 for (i
= 0; i
< h
; i
++) {
1429 for (j
= 0; j
< w
; j
++) {
1430 /* convert 24-bit Z to 32-bit Z */
1431 pDest
[j
] = (ptrc
[j
] << 8) | ((ptrc
[j
] >> 16) & 0xff);
1434 ptrc
+= pt
->stride
/4;
1438 case PIPE_FORMAT_Z24S8_UNORM
:
1439 case PIPE_FORMAT_Z24X8_UNORM
:
1442 = (const uint
*)(map
+ y
* pt
->stride
+ x
*4);
1443 for (i
= 0; i
< h
; i
++) {
1444 for (j
= 0; j
< w
; j
++) {
1445 /* convert 24-bit Z to 32-bit Z */
1446 pDest
[j
] = (ptrc
[j
] & 0xffffff00) | ((ptrc
[j
] >> 24) & 0xff);
1449 ptrc
+= pt
->stride
/4;
1453 case PIPE_FORMAT_Z16_UNORM
:
1456 = (const ushort
*)(map
+ y
* pt
->stride
+ x
*2);
1457 for (i
= 0; i
< h
; i
++) {
1458 for (j
= 0; j
< w
; j
++) {
1459 /* convert 16-bit Z to 32-bit Z */
1460 pDest
[j
] = (ptrc
[j
] << 16) | ptrc
[j
];
1463 ptrc
+= pt
->stride
/2;
1471 screen
->transfer_unmap(screen
, pt
);
1476 pipe_put_tile_z(struct pipe_transfer
*pt
,
1477 uint x
, uint y
, uint w
, uint h
,
1480 struct pipe_screen
*screen
= pt
->texture
->screen
;
1481 const uint srcStride
= w
;
1482 const uint
*ptrc
= zSrc
;
1485 enum pipe_format format
= pt
->texture
->format
;
1487 if (pipe_clip_tile(x
, y
, &w
, &h
, pt
))
1490 map
= (ubyte
*)screen
->transfer_map(screen
, pt
);
1497 case PIPE_FORMAT_Z32_UNORM
:
1499 uint
*pDest
= (uint
*) (map
+ y
* pt
->stride
+ x
*4);
1500 for (i
= 0; i
< h
; i
++) {
1501 memcpy(pDest
, ptrc
, 4 * w
);
1502 pDest
+= pt
->stride
/4;
1507 case PIPE_FORMAT_S8Z24_UNORM
:
1509 uint
*pDest
= (uint
*) (map
+ y
* pt
->stride
+ x
*4);
1510 assert((pt
->usage
& PIPE_TRANSFER_READ_WRITE
) == PIPE_TRANSFER_READ_WRITE
);
1511 for (i
= 0; i
< h
; i
++) {
1512 for (j
= 0; j
< w
; j
++) {
1513 /* convert 32-bit Z to 24-bit Z, preserve stencil */
1514 pDest
[j
] = (pDest
[j
] & 0xff000000) | ptrc
[j
] >> 8;
1516 pDest
+= pt
->stride
/4;
1521 case PIPE_FORMAT_X8Z24_UNORM
:
1523 uint
*pDest
= (uint
*) (map
+ y
* pt
->stride
+ x
*4);
1524 for (i
= 0; i
< h
; i
++) {
1525 for (j
= 0; j
< w
; j
++) {
1526 /* convert 32-bit Z to 24-bit Z (0 stencil) */
1527 pDest
[j
] = ptrc
[j
] >> 8;
1529 pDest
+= pt
->stride
/4;
1534 case PIPE_FORMAT_Z24S8_UNORM
:
1536 uint
*pDest
= (uint
*) (map
+ y
* pt
->stride
+ x
*4);
1537 assert((pt
->usage
& PIPE_TRANSFER_READ_WRITE
) == PIPE_TRANSFER_READ_WRITE
);
1538 for (i
= 0; i
< h
; i
++) {
1539 for (j
= 0; j
< w
; j
++) {
1540 /* convert 32-bit Z to 24-bit Z, preserve stencil */
1541 pDest
[j
] = (pDest
[j
] & 0xff) | (ptrc
[j
] & 0xffffff00);
1543 pDest
+= pt
->stride
/4;
1548 case PIPE_FORMAT_Z24X8_UNORM
:
1550 uint
*pDest
= (uint
*) (map
+ y
* pt
->stride
+ x
*4);
1551 for (i
= 0; i
< h
; i
++) {
1552 for (j
= 0; j
< w
; j
++) {
1553 /* convert 32-bit Z to 24-bit Z (0 stencil) */
1554 pDest
[j
] = ptrc
[j
] & 0xffffff00;
1556 pDest
+= pt
->stride
/4;
1561 case PIPE_FORMAT_Z16_UNORM
:
1563 ushort
*pDest
= (ushort
*) (map
+ y
* pt
->stride
+ x
*2);
1564 for (i
= 0; i
< h
; i
++) {
1565 for (j
= 0; j
< w
; j
++) {
1566 /* convert 32-bit Z to 16-bit Z */
1567 pDest
[j
] = ptrc
[j
] >> 16;
1569 pDest
+= pt
->stride
/2;
1578 screen
->transfer_unmap(screen
, pt
);