1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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 "util/u_inlines.h"
37 #include "util/u_format.h"
38 #include "util/u_math.h"
39 #include "util/u_memory.h"
40 #include "util/u_surface.h"
41 #include "util/u_tile.h"
45 * Move raw block of pixels from transfer object to user memory.
48 pipe_get_tile_raw(struct pipe_transfer
*pt
,
50 uint x
, uint y
, uint w
, uint h
,
51 void *dst
, int dst_stride
)
54 dst_stride
= util_format_get_stride(pt
->resource
->format
, w
);
56 if (u_clip_tile(x
, y
, &w
, &h
, &pt
->box
))
59 util_copy_rect(dst
, pt
->resource
->format
, dst_stride
, 0, 0, w
, h
, src
, pt
->stride
, x
, y
);
64 * Move raw block of pixels from user memory to transfer object.
67 pipe_put_tile_raw(struct pipe_transfer
*pt
,
69 uint x
, uint y
, uint w
, uint h
,
70 const void *src
, int src_stride
)
72 enum pipe_format format
= pt
->resource
->format
;
75 src_stride
= util_format_get_stride(format
, w
);
77 if (u_clip_tile(x
, y
, &w
, &h
, &pt
->box
))
80 util_copy_rect(dst
, format
, pt
->stride
, x
, y
, w
, h
, src
, src_stride
, 0, 0);
86 /** Convert short in [-32768,32767] to GLfloat in [-1.0,1.0] */
87 #define SHORT_TO_FLOAT(S) ((2.0F * (S) + 1.0F) * (1.0F/65535.0F))
89 #define UNCLAMPED_FLOAT_TO_SHORT(us, f) \
90 us = ( (short) ( CLAMP((f), -1.0, 1.0) * 32767.0F) )
94 /*** PIPE_FORMAT_Z16_UNORM ***/
97 * Return each Z value as four floats in [0,1].
100 z16_get_tile_rgba(const ushort
*src
,
101 unsigned w
, unsigned h
,
105 const float scale
= 1.0f
/ 65535.0f
;
108 for (i
= 0; i
< h
; i
++) {
110 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
114 pRow
[3] = *src
++ * scale
;
123 /*** PIPE_FORMAT_Z32_UNORM ***/
126 * Return each Z value as four floats in [0,1].
129 z32_get_tile_rgba(const unsigned *src
,
130 unsigned w
, unsigned h
,
134 const double scale
= 1.0 / (double) 0xffffffff;
137 for (i
= 0; i
< h
; i
++) {
139 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
143 pRow
[3] = (float) (*src
++ * scale
);
150 /*** PIPE_FORMAT_Z24_UNORM_S8_UINT ***/
153 * Return Z component as four float in [0,1]. Stencil part ignored.
156 s8z24_get_tile_rgba(const unsigned *src
,
157 unsigned w
, unsigned h
,
161 const double scale
= 1.0 / ((1 << 24) - 1);
164 for (i
= 0; i
< h
; i
++) {
166 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
170 pRow
[3] = (float) (scale
* (*src
++ & 0xffffff));
177 /*** PIPE_FORMAT_S8_UINT_Z24_UNORM ***/
180 * Return Z component as four float in [0,1]. Stencil part ignored.
183 z24s8_get_tile_rgba(const unsigned *src
,
184 unsigned w
, unsigned h
,
188 const double scale
= 1.0 / ((1 << 24) - 1);
191 for (i
= 0; i
< h
; i
++) {
193 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
197 pRow
[3] = (float) (scale
* (*src
++ >> 8));
203 /*** PIPE_FORMAT_S8X24_UINT ***/
206 * Return S component as four uint32_t in [0..255]. Z part ignored.
209 s8x24_get_tile_rgba(const unsigned *src
,
210 unsigned w
, unsigned h
,
216 for (i
= 0; i
< h
; i
++) {
217 uint32_t *pRow
= (uint32_t *)p
;
219 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
223 pRow
[3] = ((*src
++ >> 24) & 0xff);
230 /*** PIPE_FORMAT_X24S8_UINT ***/
233 * Return S component as four uint32_t in [0..255]. Z part ignored.
236 x24s8_get_tile_rgba(const unsigned *src
,
237 unsigned w
, unsigned h
,
243 for (i
= 0; i
< h
; i
++) {
244 uint32_t *pRow
= (uint32_t *)p
;
245 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
249 pRow
[3] = (*src
++ & 0xff);
257 * Return S component as four uint32_t in [0..255]. Z part ignored.
260 s8_get_tile_rgba(const unsigned char *src
,
261 unsigned w
, unsigned h
,
267 for (i
= 0; i
< h
; i
++) {
268 uint32_t *pRow
= (uint32_t *)p
;
269 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
273 pRow
[3] = (*src
++ & 0xff);
279 /*** PIPE_FORMAT_Z32_FLOAT ***/
282 * Return each Z value as four floats in [0,1].
285 z32f_get_tile_rgba(const float *src
,
286 unsigned w
, unsigned h
,
292 for (i
= 0; i
< h
; i
++) {
294 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
304 /*** PIPE_FORMAT_Z32_FLOAT_S8X24_UINT ***/
307 * Return each Z value as four floats in [0,1].
310 z32f_x24s8_get_tile_rgba(const float *src
,
311 unsigned w
, unsigned h
,
317 for (i
= 0; i
< h
; i
++) {
319 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
330 /*** PIPE_FORMAT_X32_S8X24_UINT ***/
333 * Return S component as four uint32_t in [0..255]. Z part ignored.
336 x32_s8_get_tile_rgba(const unsigned *src
,
337 unsigned w
, unsigned h
,
343 for (i
= 0; i
< h
; i
++) {
344 uint32_t *pRow
= (uint32_t *)p
;
345 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
350 pRow
[3] = (*src
++ & 0xff);
357 pipe_tile_raw_to_rgba(enum pipe_format format
,
360 float *dst
, unsigned dst_stride
)
363 case PIPE_FORMAT_Z16_UNORM
:
364 z16_get_tile_rgba((ushort
*) src
, w
, h
, dst
, dst_stride
);
366 case PIPE_FORMAT_Z32_UNORM
:
367 z32_get_tile_rgba((unsigned *) src
, w
, h
, dst
, dst_stride
);
369 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
370 case PIPE_FORMAT_Z24X8_UNORM
:
371 s8z24_get_tile_rgba((unsigned *) src
, w
, h
, dst
, dst_stride
);
373 case PIPE_FORMAT_S8_UINT
:
374 s8_get_tile_rgba((unsigned char *) src
, w
, h
, dst
, dst_stride
);
376 case PIPE_FORMAT_X24S8_UINT
:
377 s8x24_get_tile_rgba((unsigned *) src
, w
, h
, dst
, dst_stride
);
379 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
380 case PIPE_FORMAT_X8Z24_UNORM
:
381 z24s8_get_tile_rgba((unsigned *) src
, w
, h
, dst
, dst_stride
);
383 case PIPE_FORMAT_S8X24_UINT
:
384 x24s8_get_tile_rgba((unsigned *) src
, w
, h
, dst
, dst_stride
);
386 case PIPE_FORMAT_Z32_FLOAT
:
387 z32f_get_tile_rgba((float *) src
, w
, h
, dst
, dst_stride
);
389 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
390 z32f_x24s8_get_tile_rgba((float *) src
, w
, h
, dst
, dst_stride
);
392 case PIPE_FORMAT_X32_S8X24_UINT
:
393 x32_s8_get_tile_rgba((unsigned *) src
, w
, h
, dst
, dst_stride
);
396 util_format_read_4f(format
,
397 dst
, dst_stride
* sizeof(float),
398 src
, util_format_get_stride(format
, w
),
404 pipe_tile_raw_to_unsigned(enum pipe_format format
,
407 unsigned *dst
, unsigned dst_stride
)
409 util_format_read_4ui(format
,
410 dst
, dst_stride
* sizeof(float),
411 src
, util_format_get_stride(format
, w
),
416 pipe_tile_raw_to_signed(enum pipe_format format
,
419 int *dst
, unsigned dst_stride
)
421 util_format_read_4i(format
,
422 dst
, dst_stride
* sizeof(float),
423 src
, util_format_get_stride(format
, w
),
428 pipe_get_tile_rgba(struct pipe_transfer
*pt
,
430 uint x
, uint y
, uint w
, uint h
,
433 pipe_get_tile_rgba_format(pt
, src
, x
, y
, w
, h
, pt
->resource
->format
, p
);
438 pipe_get_tile_rgba_format(struct pipe_transfer
*pt
,
440 uint x
, uint y
, uint w
, uint h
,
441 enum pipe_format format
,
444 unsigned dst_stride
= w
* 4;
447 if (u_clip_tile(x
, y
, &w
, &h
, &pt
->box
)) {
451 packed
= MALLOC(util_format_get_nblocks(format
, w
, h
) * util_format_get_blocksize(format
));
456 if (format
== PIPE_FORMAT_UYVY
|| format
== PIPE_FORMAT_YUYV
) {
457 assert((x
& 1) == 0);
460 pipe_get_tile_raw(pt
, src
, x
, y
, w
, h
, packed
, 0);
462 pipe_tile_raw_to_rgba(format
, packed
, w
, h
, p
, dst_stride
);
469 pipe_put_tile_rgba(struct pipe_transfer
*pt
,
471 uint x
, uint y
, uint w
, uint h
,
474 pipe_put_tile_rgba_format(pt
, dst
, x
, y
, w
, h
, pt
->resource
->format
, p
);
479 pipe_put_tile_rgba_format(struct pipe_transfer
*pt
,
481 uint x
, uint y
, uint w
, uint h
,
482 enum pipe_format format
,
485 unsigned src_stride
= w
* 4;
488 if (u_clip_tile(x
, y
, &w
, &h
, &pt
->box
))
491 packed
= MALLOC(util_format_get_nblocks(format
, w
, h
) * util_format_get_blocksize(format
));
497 case PIPE_FORMAT_Z16_UNORM
:
498 /*z16_put_tile_rgba((ushort *) packed, w, h, p, src_stride);*/
500 case PIPE_FORMAT_Z32_UNORM
:
501 /*z32_put_tile_rgba((unsigned *) packed, w, h, p, src_stride);*/
503 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
504 case PIPE_FORMAT_Z24X8_UNORM
:
505 /*s8z24_put_tile_rgba((unsigned *) packed, w, h, p, src_stride);*/
507 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
508 case PIPE_FORMAT_X8Z24_UNORM
:
509 /*z24s8_put_tile_rgba((unsigned *) packed, w, h, p, src_stride);*/
511 case PIPE_FORMAT_Z32_FLOAT
:
512 /*z32f_put_tile_rgba((unsigned *) packed, w, h, p, src_stride);*/
514 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
515 /*z32f_s8x24_put_tile_rgba((unsigned *) packed, w, h, p, src_stride);*/
518 util_format_write_4f(format
,
519 p
, src_stride
* sizeof(float),
520 packed
, util_format_get_stride(format
, w
),
524 pipe_put_tile_raw(pt
, dst
, x
, y
, w
, h
, packed
, 0);
530 pipe_put_tile_i_format(struct pipe_transfer
*pt
,
532 uint x
, uint y
, uint w
, uint h
,
533 enum pipe_format format
,
536 unsigned src_stride
= w
* 4;
539 if (u_clip_tile(x
, y
, &w
, &h
, &pt
->box
))
542 packed
= MALLOC(util_format_get_nblocks(format
, w
, h
) * util_format_get_blocksize(format
));
547 util_format_write_4i(format
,
548 p
, src_stride
* sizeof(float),
549 packed
, util_format_get_stride(format
, w
),
552 pipe_put_tile_raw(pt
, dst
, x
, y
, w
, h
, packed
, 0);
558 pipe_put_tile_ui_format(struct pipe_transfer
*pt
,
560 uint x
, uint y
, uint w
, uint h
,
561 enum pipe_format format
,
562 const unsigned int *p
)
564 unsigned src_stride
= w
* 4;
567 if (u_clip_tile(x
, y
, &w
, &h
, &pt
->box
))
570 packed
= MALLOC(util_format_get_nblocks(format
, w
, h
) * util_format_get_blocksize(format
));
575 util_format_write_4ui(format
,
576 p
, src_stride
* sizeof(float),
577 packed
, util_format_get_stride(format
, w
),
580 pipe_put_tile_raw(pt
, dst
, x
, y
, w
, h
, packed
, 0);
586 * Get a block of Z values, converted to 32-bit range.
589 pipe_get_tile_z(struct pipe_transfer
*pt
,
591 uint x
, uint y
, uint w
, uint h
,
594 const uint dstStride
= w
;
595 const ubyte
*map
= src
;
598 enum pipe_format format
= pt
->resource
->format
;
600 if (u_clip_tile(x
, y
, &w
, &h
, &pt
->box
))
604 case PIPE_FORMAT_Z32_UNORM
:
607 = (const uint
*)(map
+ y
* pt
->stride
+ x
*4);
608 for (i
= 0; i
< h
; i
++) {
609 memcpy(pDest
, ptrc
, 4 * w
);
611 ptrc
+= pt
->stride
/4;
615 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
616 case PIPE_FORMAT_Z24X8_UNORM
:
619 = (const uint
*)(map
+ y
* pt
->stride
+ x
*4);
620 for (i
= 0; i
< h
; i
++) {
621 for (j
= 0; j
< w
; j
++) {
622 /* convert 24-bit Z to 32-bit Z */
623 pDest
[j
] = (ptrc
[j
] << 8) | ((ptrc
[j
] >> 16) & 0xff);
626 ptrc
+= pt
->stride
/4;
630 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
631 case PIPE_FORMAT_X8Z24_UNORM
:
634 = (const uint
*)(map
+ y
* pt
->stride
+ x
*4);
635 for (i
= 0; i
< h
; i
++) {
636 for (j
= 0; j
< w
; j
++) {
637 /* convert 24-bit Z to 32-bit Z */
638 pDest
[j
] = (ptrc
[j
] & 0xffffff00) | ((ptrc
[j
] >> 24) & 0xff);
641 ptrc
+= pt
->stride
/4;
645 case PIPE_FORMAT_Z16_UNORM
:
648 = (const ushort
*)(map
+ y
* pt
->stride
+ x
*2);
649 for (i
= 0; i
< h
; i
++) {
650 for (j
= 0; j
< w
; j
++) {
651 /* convert 16-bit Z to 32-bit Z */
652 pDest
[j
] = (ptrc
[j
] << 16) | ptrc
[j
];
655 ptrc
+= pt
->stride
/2;
659 case PIPE_FORMAT_Z32_FLOAT
:
661 const float *ptrc
= (const float *)(map
+ y
* pt
->stride
+ x
*4);
662 for (i
= 0; i
< h
; i
++) {
663 for (j
= 0; j
< w
; j
++) {
664 /* convert float Z to 32-bit Z */
665 if (ptrc
[j
] <= 0.0) {
668 else if (ptrc
[j
] >= 1.0) {
669 pDest
[j
] = 0xffffffff;
672 double z
= ptrc
[j
] * 0xffffffff;
677 ptrc
+= pt
->stride
/4;
681 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
683 const float *ptrc
= (const float *)(map
+ y
* pt
->stride
+ x
*8);
684 for (i
= 0; i
< h
; i
++) {
685 for (j
= 0; j
< w
; j
++) {
686 /* convert float Z to 32-bit Z */
687 if (ptrc
[j
] <= 0.0) {
690 else if (ptrc
[j
] >= 1.0) {
691 pDest
[j
*2] = 0xffffffff;
694 double z
= ptrc
[j
] * 0xffffffff;
695 pDest
[j
*2] = (uint
) z
;
699 ptrc
+= pt
->stride
/4;
710 pipe_put_tile_z(struct pipe_transfer
*pt
,
712 uint x
, uint y
, uint w
, uint h
,
715 const uint srcStride
= w
;
716 const uint
*ptrc
= zSrc
;
719 enum pipe_format format
= pt
->resource
->format
;
721 if (u_clip_tile(x
, y
, &w
, &h
, &pt
->box
))
725 case PIPE_FORMAT_Z32_UNORM
:
727 uint
*pDest
= (uint
*) (map
+ y
* pt
->stride
+ x
*4);
728 for (i
= 0; i
< h
; i
++) {
729 memcpy(pDest
, ptrc
, 4 * w
);
730 pDest
+= pt
->stride
/4;
735 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
737 uint
*pDest
= (uint
*) (map
+ y
* pt
->stride
+ x
*4);
738 /*assert((pt->usage & PIPE_TRANSFER_READ_WRITE) == PIPE_TRANSFER_READ_WRITE);*/
739 for (i
= 0; i
< h
; i
++) {
740 for (j
= 0; j
< w
; j
++) {
741 /* convert 32-bit Z to 24-bit Z, preserve stencil */
742 pDest
[j
] = (pDest
[j
] & 0xff000000) | ptrc
[j
] >> 8;
744 pDest
+= pt
->stride
/4;
749 case PIPE_FORMAT_Z24X8_UNORM
:
751 uint
*pDest
= (uint
*) (map
+ y
* pt
->stride
+ x
*4);
752 for (i
= 0; i
< h
; i
++) {
753 for (j
= 0; j
< w
; j
++) {
754 /* convert 32-bit Z to 24-bit Z (0 stencil) */
755 pDest
[j
] = ptrc
[j
] >> 8;
757 pDest
+= pt
->stride
/4;
762 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
764 uint
*pDest
= (uint
*) (map
+ y
* pt
->stride
+ x
*4);
765 /*assert((pt->usage & PIPE_TRANSFER_READ_WRITE) == PIPE_TRANSFER_READ_WRITE);*/
766 for (i
= 0; i
< h
; i
++) {
767 for (j
= 0; j
< w
; j
++) {
768 /* convert 32-bit Z to 24-bit Z, preserve stencil */
769 pDest
[j
] = (pDest
[j
] & 0xff) | (ptrc
[j
] & 0xffffff00);
771 pDest
+= pt
->stride
/4;
776 case PIPE_FORMAT_X8Z24_UNORM
:
778 uint
*pDest
= (uint
*) (map
+ y
* pt
->stride
+ x
*4);
779 for (i
= 0; i
< h
; i
++) {
780 for (j
= 0; j
< w
; j
++) {
781 /* convert 32-bit Z to 24-bit Z (0 stencil) */
782 pDest
[j
] = ptrc
[j
] & 0xffffff00;
784 pDest
+= pt
->stride
/4;
789 case PIPE_FORMAT_Z16_UNORM
:
791 ushort
*pDest
= (ushort
*) (map
+ y
* pt
->stride
+ x
*2);
792 for (i
= 0; i
< h
; i
++) {
793 for (j
= 0; j
< w
; j
++) {
794 /* convert 32-bit Z to 16-bit Z */
795 pDest
[j
] = ptrc
[j
] >> 16;
797 pDest
+= pt
->stride
/2;
802 case PIPE_FORMAT_Z32_FLOAT
:
804 float *pDest
= (float *) (map
+ y
* pt
->stride
+ x
*4);
805 for (i
= 0; i
< h
; i
++) {
806 for (j
= 0; j
< w
; j
++) {
807 /* convert 32-bit integer Z to float Z */
808 const double scale
= 1.0 / 0xffffffffU
;
809 pDest
[j
] = (float) (ptrc
[j
] * scale
);
811 pDest
+= pt
->stride
/4;
816 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
818 float *pDest
= (float *) (map
+ y
* pt
->stride
+ x
*8);
819 for (i
= 0; i
< h
; i
++) {
820 for (j
= 0; j
< w
; j
++) {
821 /* convert 32-bit integer Z to float Z */
822 const double scale
= 1.0 / 0xffffffffU
;
823 pDest
[j
*2] = (float) (ptrc
[j
] * scale
);
825 pDest
+= pt
->stride
/4;
837 pipe_get_tile_ui_format(struct pipe_transfer
*pt
,
839 uint x
, uint y
, uint w
, uint h
,
840 enum pipe_format format
,
843 unsigned dst_stride
= w
* 4;
846 if (u_clip_tile(x
, y
, &w
, &h
, &pt
->box
)) {
850 packed
= MALLOC(util_format_get_nblocks(format
, w
, h
) * util_format_get_blocksize(format
));
855 if (format
== PIPE_FORMAT_UYVY
|| format
== PIPE_FORMAT_YUYV
) {
856 assert((x
& 1) == 0);
859 pipe_get_tile_raw(pt
, src
, x
, y
, w
, h
, packed
, 0);
861 pipe_tile_raw_to_unsigned(format
, packed
, w
, h
, p
, dst_stride
);
868 pipe_get_tile_i_format(struct pipe_transfer
*pt
,
870 uint x
, uint y
, uint w
, uint h
,
871 enum pipe_format format
,
874 unsigned dst_stride
= w
* 4;
877 if (u_clip_tile(x
, y
, &w
, &h
, &pt
->box
)) {
881 packed
= MALLOC(util_format_get_nblocks(format
, w
, h
) * util_format_get_blocksize(format
));
886 if (format
== PIPE_FORMAT_UYVY
|| format
== PIPE_FORMAT_YUYV
) {
887 assert((x
& 1) == 0);
890 pipe_get_tile_raw(pt
, src
, x
, y
, w
, h
, packed
, 0);
892 pipe_tile_raw_to_signed(format
, packed
, w
, h
, p
, dst_stride
);