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 "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_rect.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_context
*pipe
,
49 struct pipe_transfer
*pt
,
50 uint x
, uint y
, uint w
, uint h
,
51 void *dst
, int dst_stride
)
56 dst_stride
= util_format_get_stride(pt
->resource
->format
, w
);
58 if (u_clip_tile(x
, y
, &w
, &h
, &pt
->box
))
61 src
= pipe
->transfer_map(pipe
, pt
);
66 util_copy_rect(dst
, pt
->resource
->format
, dst_stride
, 0, 0, w
, h
, src
, pt
->stride
, x
, y
);
68 pipe
->transfer_unmap(pipe
, pt
);
73 * Move raw block of pixels from user memory to transfer object.
76 pipe_put_tile_raw(struct pipe_context
*pipe
,
77 struct pipe_transfer
*pt
,
78 uint x
, uint y
, uint w
, uint h
,
79 const void *src
, int src_stride
)
82 enum pipe_format format
= pt
->resource
->format
;
85 src_stride
= util_format_get_stride(format
, w
);
87 if (u_clip_tile(x
, y
, &w
, &h
, &pt
->box
))
90 dst
= pipe
->transfer_map(pipe
, pt
);
95 util_copy_rect(dst
, format
, pt
->stride
, x
, y
, w
, h
, src
, src_stride
, 0, 0);
97 pipe
->transfer_unmap(pipe
, pt
);
103 /** Convert short in [-32768,32767] to GLfloat in [-1.0,1.0] */
104 #define SHORT_TO_FLOAT(S) ((2.0F * (S) + 1.0F) * (1.0F/65535.0F))
106 #define UNCLAMPED_FLOAT_TO_SHORT(us, f) \
107 us = ( (short) ( CLAMP((f), -1.0, 1.0) * 32767.0F) )
111 /*** PIPE_FORMAT_Z16_UNORM ***/
114 * Return each Z value as four floats in [0,1].
117 z16_get_tile_rgba(const ushort
*src
,
118 unsigned w
, unsigned h
,
122 const float scale
= 1.0f
/ 65535.0f
;
125 for (i
= 0; i
< h
; i
++) {
127 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
131 pRow
[3] = *src
++ * scale
;
140 /*** PIPE_FORMAT_Z32_UNORM ***/
143 * Return each Z value as four floats in [0,1].
146 z32_get_tile_rgba(const unsigned *src
,
147 unsigned w
, unsigned h
,
151 const double scale
= 1.0 / (double) 0xffffffff;
154 for (i
= 0; i
< h
; i
++) {
156 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
160 pRow
[3] = (float) (*src
++ * scale
);
167 /*** PIPE_FORMAT_Z24_UNORM_S8_USCALED ***/
170 * Return Z component as four float in [0,1]. Stencil part ignored.
173 s8z24_get_tile_rgba(const unsigned *src
,
174 unsigned w
, unsigned h
,
178 const double scale
= 1.0 / ((1 << 24) - 1);
181 for (i
= 0; i
< h
; i
++) {
183 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
187 pRow
[3] = (float) (scale
* (*src
++ & 0xffffff));
194 /*** PIPE_FORMAT_S8_USCALED_Z24_UNORM ***/
197 * Return Z component as four float in [0,1]. Stencil part ignored.
200 z24s8_get_tile_rgba(const unsigned *src
,
201 unsigned w
, unsigned h
,
205 const double scale
= 1.0 / ((1 << 24) - 1);
208 for (i
= 0; i
< h
; i
++) {
210 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
214 pRow
[3] = (float) (scale
* (*src
++ >> 8));
220 /*** PIPE_FORMAT_S8X24_USCALED ***/
223 * Return S component as four uint32_t in [0..255]. Z part ignored.
226 s8x24_get_tile_rgba(const unsigned *src
,
227 unsigned w
, unsigned h
,
233 for (i
= 0; i
< h
; i
++) {
236 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
240 pRow
[3] = (float)((*src
++ >> 24) & 0xff);
247 /*** PIPE_FORMAT_X24S8_USCALED ***/
250 * Return S component as four uint32_t in [0..255]. Z part ignored.
253 x24s8_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) {
266 pRow
[3] = (float)(*src
++ & 0xff);
274 * Return S component as four uint32_t in [0..255]. Z part ignored.
277 s8_get_tile_rgba(const unsigned char *src
,
278 unsigned w
, unsigned h
,
284 for (i
= 0; i
< h
; i
++) {
286 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
290 pRow
[3] = (float)(*src
++ & 0xff);
296 /*** PIPE_FORMAT_Z32_FLOAT ***/
299 * Return each Z value as four floats in [0,1].
302 z32f_get_tile_rgba(const float *src
,
303 unsigned w
, unsigned h
,
309 for (i
= 0; i
< h
; i
++) {
311 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
321 /*** PIPE_FORMAT_Z32_FLOAT_S8X24_USCALED ***/
324 * Return each Z value as four floats in [0,1].
327 z32f_x24s8_get_tile_rgba(const float *src
,
328 unsigned w
, unsigned h
,
334 for (i
= 0; i
< h
; i
++) {
336 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
349 pipe_tile_raw_to_rgba(enum pipe_format format
,
352 float *dst
, unsigned dst_stride
)
355 case PIPE_FORMAT_Z16_UNORM
:
356 z16_get_tile_rgba((ushort
*) src
, w
, h
, dst
, dst_stride
);
358 case PIPE_FORMAT_Z32_UNORM
:
359 z32_get_tile_rgba((unsigned *) src
, w
, h
, dst
, dst_stride
);
361 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
362 case PIPE_FORMAT_Z24X8_UNORM
:
363 s8z24_get_tile_rgba((unsigned *) src
, w
, h
, dst
, dst_stride
);
365 case PIPE_FORMAT_S8_USCALED
:
366 s8_get_tile_rgba((unsigned char *) src
, w
, h
, dst
, dst_stride
);
368 case PIPE_FORMAT_X24S8_USCALED
:
369 s8x24_get_tile_rgba((unsigned *) src
, w
, h
, dst
, dst_stride
);
371 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
372 case PIPE_FORMAT_X8Z24_UNORM
:
373 z24s8_get_tile_rgba((unsigned *) src
, w
, h
, dst
, dst_stride
);
375 case PIPE_FORMAT_S8X24_USCALED
:
376 x24s8_get_tile_rgba((unsigned *) src
, w
, h
, dst
, dst_stride
);
378 case PIPE_FORMAT_Z32_FLOAT
:
379 z32f_get_tile_rgba((float *) src
, w
, h
, dst
, dst_stride
);
381 case PIPE_FORMAT_Z32_FLOAT_S8X24_USCALED
:
382 z32f_x24s8_get_tile_rgba((float *) src
, w
, h
, dst
, dst_stride
);
385 util_format_read_4f(format
,
386 dst
, dst_stride
* sizeof(float),
387 src
, util_format_get_stride(format
, w
),
393 pipe_tile_raw_to_unsigned(enum pipe_format format
,
396 unsigned *dst
, unsigned dst_stride
)
398 util_format_read_4ui(format
,
399 dst
, dst_stride
* sizeof(float),
400 src
, util_format_get_stride(format
, w
),
405 pipe_tile_raw_to_signed(enum pipe_format format
,
408 int *dst
, unsigned dst_stride
)
410 util_format_read_4i(format
,
411 dst
, dst_stride
* sizeof(float),
412 src
, util_format_get_stride(format
, w
),
417 pipe_get_tile_rgba(struct pipe_context
*pipe
,
418 struct pipe_transfer
*pt
,
419 uint x
, uint y
, uint w
, uint h
,
422 pipe_get_tile_rgba_format(pipe
, pt
, x
, y
, w
, h
, pt
->resource
->format
, p
);
427 pipe_get_tile_rgba_format(struct pipe_context
*pipe
,
428 struct pipe_transfer
*pt
,
429 uint x
, uint y
, uint w
, uint h
,
430 enum pipe_format format
,
433 unsigned dst_stride
= w
* 4;
436 if (u_clip_tile(x
, y
, &w
, &h
, &pt
->box
)) {
440 packed
= MALLOC(util_format_get_nblocks(format
, w
, h
) * util_format_get_blocksize(format
));
445 if (format
== PIPE_FORMAT_UYVY
|| format
== PIPE_FORMAT_YUYV
) {
446 assert((x
& 1) == 0);
449 pipe_get_tile_raw(pipe
, pt
, x
, y
, w
, h
, packed
, 0);
451 pipe_tile_raw_to_rgba(format
, packed
, w
, h
, p
, dst_stride
);
458 pipe_put_tile_rgba(struct pipe_context
*pipe
,
459 struct pipe_transfer
*pt
,
460 uint x
, uint y
, uint w
, uint h
,
463 pipe_put_tile_rgba_format(pipe
, pt
, x
, y
, w
, h
, pt
->resource
->format
, p
);
468 pipe_put_tile_rgba_format(struct pipe_context
*pipe
,
469 struct pipe_transfer
*pt
,
470 uint x
, uint y
, uint w
, uint h
,
471 enum pipe_format format
,
474 unsigned src_stride
= w
* 4;
477 if (u_clip_tile(x
, y
, &w
, &h
, &pt
->box
))
480 packed
= MALLOC(util_format_get_nblocks(format
, w
, h
) * util_format_get_blocksize(format
));
486 case PIPE_FORMAT_Z16_UNORM
:
487 /*z16_put_tile_rgba((ushort *) packed, w, h, p, src_stride);*/
489 case PIPE_FORMAT_Z32_UNORM
:
490 /*z32_put_tile_rgba((unsigned *) packed, w, h, p, src_stride);*/
492 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
493 case PIPE_FORMAT_Z24X8_UNORM
:
494 /*s8z24_put_tile_rgba((unsigned *) packed, w, h, p, src_stride);*/
496 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
497 case PIPE_FORMAT_X8Z24_UNORM
:
498 /*z24s8_put_tile_rgba((unsigned *) packed, w, h, p, src_stride);*/
500 case PIPE_FORMAT_Z32_FLOAT
:
501 /*z32f_put_tile_rgba((unsigned *) packed, w, h, p, src_stride);*/
503 case PIPE_FORMAT_Z32_FLOAT_S8X24_USCALED
:
504 /*z32f_s8x24_put_tile_rgba((unsigned *) packed, w, h, p, src_stride);*/
507 util_format_write_4f(format
,
508 p
, src_stride
* sizeof(float),
509 packed
, util_format_get_stride(format
, w
),
513 pipe_put_tile_raw(pipe
, pt
, x
, y
, w
, h
, packed
, 0);
519 pipe_put_tile_i_format(struct pipe_context
*pipe
,
520 struct pipe_transfer
*pt
,
521 uint x
, uint y
, uint w
, uint h
,
522 enum pipe_format format
,
525 unsigned src_stride
= w
* 4;
528 if (u_clip_tile(x
, y
, &w
, &h
, &pt
->box
))
531 packed
= MALLOC(util_format_get_nblocks(format
, w
, h
) * util_format_get_blocksize(format
));
536 util_format_write_4i(format
,
537 p
, src_stride
* sizeof(float),
538 packed
, util_format_get_stride(format
, w
),
541 pipe_put_tile_raw(pipe
, pt
, x
, y
, w
, h
, packed
, 0);
547 pipe_put_tile_ui_format(struct pipe_context
*pipe
,
548 struct pipe_transfer
*pt
,
549 uint x
, uint y
, uint w
, uint h
,
550 enum pipe_format format
,
551 const unsigned int *p
)
553 unsigned src_stride
= w
* 4;
556 if (u_clip_tile(x
, y
, &w
, &h
, &pt
->box
))
559 packed
= MALLOC(util_format_get_nblocks(format
, w
, h
) * util_format_get_blocksize(format
));
564 util_format_write_4ui(format
,
565 p
, src_stride
* sizeof(float),
566 packed
, util_format_get_stride(format
, w
),
569 pipe_put_tile_raw(pipe
, pt
, x
, y
, w
, h
, packed
, 0);
575 * Get a block of Z values, converted to 32-bit range.
578 pipe_get_tile_z(struct pipe_context
*pipe
,
579 struct pipe_transfer
*pt
,
580 uint x
, uint y
, uint w
, uint h
,
583 const uint dstStride
= w
;
587 enum pipe_format format
= pt
->resource
->format
;
589 if (u_clip_tile(x
, y
, &w
, &h
, &pt
->box
))
592 map
= (ubyte
*)pipe
->transfer_map(pipe
, pt
);
599 case PIPE_FORMAT_Z32_UNORM
:
602 = (const uint
*)(map
+ y
* pt
->stride
+ x
*4);
603 for (i
= 0; i
< h
; i
++) {
604 memcpy(pDest
, ptrc
, 4 * w
);
606 ptrc
+= pt
->stride
/4;
610 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
611 case PIPE_FORMAT_Z24X8_UNORM
:
614 = (const uint
*)(map
+ y
* pt
->stride
+ x
*4);
615 for (i
= 0; i
< h
; i
++) {
616 for (j
= 0; j
< w
; j
++) {
617 /* convert 24-bit Z to 32-bit Z */
618 pDest
[j
] = (ptrc
[j
] << 8) | ((ptrc
[j
] >> 16) & 0xff);
621 ptrc
+= pt
->stride
/4;
625 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
626 case PIPE_FORMAT_X8Z24_UNORM
:
629 = (const uint
*)(map
+ y
* pt
->stride
+ x
*4);
630 for (i
= 0; i
< h
; i
++) {
631 for (j
= 0; j
< w
; j
++) {
632 /* convert 24-bit Z to 32-bit Z */
633 pDest
[j
] = (ptrc
[j
] & 0xffffff00) | ((ptrc
[j
] >> 24) & 0xff);
636 ptrc
+= pt
->stride
/4;
640 case PIPE_FORMAT_Z16_UNORM
:
643 = (const ushort
*)(map
+ y
* pt
->stride
+ x
*2);
644 for (i
= 0; i
< h
; i
++) {
645 for (j
= 0; j
< w
; j
++) {
646 /* convert 16-bit Z to 32-bit Z */
647 pDest
[j
] = (ptrc
[j
] << 16) | ptrc
[j
];
650 ptrc
+= pt
->stride
/2;
658 pipe
->transfer_unmap(pipe
, pt
);
663 pipe_put_tile_z(struct pipe_context
*pipe
,
664 struct pipe_transfer
*pt
,
665 uint x
, uint y
, uint w
, uint h
,
668 const uint srcStride
= w
;
669 const uint
*ptrc
= zSrc
;
672 enum pipe_format format
= pt
->resource
->format
;
674 if (u_clip_tile(x
, y
, &w
, &h
, &pt
->box
))
677 map
= (ubyte
*)pipe
->transfer_map(pipe
, pt
);
684 case PIPE_FORMAT_Z32_UNORM
:
686 uint
*pDest
= (uint
*) (map
+ y
* pt
->stride
+ x
*4);
687 for (i
= 0; i
< h
; i
++) {
688 memcpy(pDest
, ptrc
, 4 * w
);
689 pDest
+= pt
->stride
/4;
694 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
696 uint
*pDest
= (uint
*) (map
+ y
* pt
->stride
+ x
*4);
697 /*assert((pt->usage & PIPE_TRANSFER_READ_WRITE) == PIPE_TRANSFER_READ_WRITE);*/
698 for (i
= 0; i
< h
; i
++) {
699 for (j
= 0; j
< w
; j
++) {
700 /* convert 32-bit Z to 24-bit Z, preserve stencil */
701 pDest
[j
] = (pDest
[j
] & 0xff000000) | ptrc
[j
] >> 8;
703 pDest
+= pt
->stride
/4;
708 case PIPE_FORMAT_Z24X8_UNORM
:
710 uint
*pDest
= (uint
*) (map
+ y
* pt
->stride
+ x
*4);
711 for (i
= 0; i
< h
; i
++) {
712 for (j
= 0; j
< w
; j
++) {
713 /* convert 32-bit Z to 24-bit Z (0 stencil) */
714 pDest
[j
] = ptrc
[j
] >> 8;
716 pDest
+= pt
->stride
/4;
721 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
723 uint
*pDest
= (uint
*) (map
+ y
* pt
->stride
+ x
*4);
724 /*assert((pt->usage & PIPE_TRANSFER_READ_WRITE) == PIPE_TRANSFER_READ_WRITE);*/
725 for (i
= 0; i
< h
; i
++) {
726 for (j
= 0; j
< w
; j
++) {
727 /* convert 32-bit Z to 24-bit Z, preserve stencil */
728 pDest
[j
] = (pDest
[j
] & 0xff) | (ptrc
[j
] & 0xffffff00);
730 pDest
+= pt
->stride
/4;
735 case PIPE_FORMAT_X8Z24_UNORM
:
737 uint
*pDest
= (uint
*) (map
+ y
* pt
->stride
+ x
*4);
738 for (i
= 0; i
< h
; i
++) {
739 for (j
= 0; j
< w
; j
++) {
740 /* convert 32-bit Z to 24-bit Z (0 stencil) */
741 pDest
[j
] = ptrc
[j
] & 0xffffff00;
743 pDest
+= pt
->stride
/4;
748 case PIPE_FORMAT_Z16_UNORM
:
750 ushort
*pDest
= (ushort
*) (map
+ y
* pt
->stride
+ x
*2);
751 for (i
= 0; i
< h
; i
++) {
752 for (j
= 0; j
< w
; j
++) {
753 /* convert 32-bit Z to 16-bit Z */
754 pDest
[j
] = ptrc
[j
] >> 16;
756 pDest
+= pt
->stride
/2;
765 pipe
->transfer_unmap(pipe
, pt
);
770 pipe_get_tile_ui_format(struct pipe_context
*pipe
,
771 struct pipe_transfer
*pt
,
772 uint x
, uint y
, uint w
, uint h
,
773 enum pipe_format format
,
776 unsigned dst_stride
= w
* 4;
779 if (u_clip_tile(x
, y
, &w
, &h
, &pt
->box
)) {
783 packed
= MALLOC(util_format_get_nblocks(format
, w
, h
) * util_format_get_blocksize(format
));
788 if (format
== PIPE_FORMAT_UYVY
|| format
== PIPE_FORMAT_YUYV
) {
789 assert((x
& 1) == 0);
792 pipe_get_tile_raw(pipe
, pt
, x
, y
, w
, h
, packed
, 0);
794 pipe_tile_raw_to_unsigned(format
, packed
, w
, h
, p
, dst_stride
);
801 pipe_get_tile_i_format(struct pipe_context
*pipe
,
802 struct pipe_transfer
*pt
,
803 uint x
, uint y
, uint w
, uint h
,
804 enum pipe_format format
,
807 unsigned dst_stride
= w
* 4;
810 if (u_clip_tile(x
, y
, &w
, &h
, &pt
->box
)) {
814 packed
= MALLOC(util_format_get_nblocks(format
, w
, h
) * util_format_get_blocksize(format
));
819 if (format
== PIPE_FORMAT_UYVY
|| format
== PIPE_FORMAT_YUYV
) {
820 assert((x
& 1) == 0);
823 pipe_get_tile_raw(pipe
, pt
, x
, y
, w
, h
, packed
, 0);
825 pipe_tile_raw_to_signed(format
, packed
, w
, h
, p
, dst_stride
);