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/format/u_format.h"
38 #include "util/format/u_format_bptc.h"
39 #include "util/u_math.h"
40 #include "util/u_memory.h"
41 #include "util/u_surface.h"
42 #include "util/u_tile.h"
46 * Move raw block of pixels from transfer object to user memory.
49 pipe_get_tile_raw(struct pipe_transfer
*pt
,
51 uint x
, uint y
, uint w
, uint h
,
52 void *dst
, int dst_stride
)
55 dst_stride
= util_format_get_stride(pt
->resource
->format
, w
);
57 if (u_clip_tile(x
, y
, &w
, &h
, &pt
->box
))
60 util_copy_rect(dst
, pt
->resource
->format
, dst_stride
, 0, 0, w
, h
, src
, pt
->stride
, x
, y
);
65 * Move raw block of pixels from user memory to transfer object.
68 pipe_put_tile_raw(struct pipe_transfer
*pt
,
70 uint x
, uint y
, uint w
, uint h
,
71 const void *src
, int src_stride
)
73 enum pipe_format format
= pt
->resource
->format
;
76 src_stride
= util_format_get_stride(format
, w
);
78 if (u_clip_tile(x
, y
, &w
, &h
, &pt
->box
))
81 util_copy_rect(dst
, format
, pt
->stride
, x
, y
, w
, h
, src
, src_stride
, 0, 0);
87 /** Convert short in [-32768,32767] to GLfloat in [-1.0,1.0] */
88 #define SHORT_TO_FLOAT(S) ((2.0F * (S) + 1.0F) * (1.0F/65535.0F))
90 #define UNCLAMPED_FLOAT_TO_SHORT(us, f) \
91 us = ( (short) ( CLAMP((f), -1.0, 1.0) * 32767.0F) )
95 /*** PIPE_FORMAT_Z16_UNORM ***/
98 * Return each Z value as four floats in [0,1].
101 z16_get_tile_rgba(const ushort
*src
,
102 unsigned w
, unsigned h
,
106 const float scale
= 1.0f
/ 65535.0f
;
109 for (i
= 0; i
< h
; i
++) {
111 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
115 pRow
[3] = *src
++ * scale
;
124 /*** PIPE_FORMAT_Z32_UNORM ***/
127 * Return each Z value as four floats in [0,1].
130 z32_get_tile_rgba(const unsigned *src
,
131 unsigned w
, unsigned h
,
135 const double scale
= 1.0 / (double) 0xffffffff;
138 for (i
= 0; i
< h
; i
++) {
140 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
144 pRow
[3] = (float) (*src
++ * scale
);
151 /*** PIPE_FORMAT_Z24_UNORM_S8_UINT ***/
154 * Return Z component as four float in [0,1]. Stencil part ignored.
157 s8z24_get_tile_rgba(const unsigned *src
,
158 unsigned w
, unsigned h
,
162 const double scale
= 1.0 / ((1 << 24) - 1);
165 for (i
= 0; i
< h
; i
++) {
167 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
171 pRow
[3] = (float) (scale
* (*src
++ & 0xffffff));
178 /*** PIPE_FORMAT_S8_UINT_Z24_UNORM ***/
181 * Return Z component as four float in [0,1]. Stencil part ignored.
184 z24s8_get_tile_rgba(const unsigned *src
,
185 unsigned w
, unsigned h
,
189 const double scale
= 1.0 / ((1 << 24) - 1);
192 for (i
= 0; i
< h
; i
++) {
194 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
198 pRow
[3] = (float) (scale
* (*src
++ >> 8));
204 /*** PIPE_FORMAT_S8X24_UINT ***/
207 * Return S component as four uint32_t in [0..255]. Z part ignored.
210 s8x24_get_tile_rgba(const unsigned *src
,
211 unsigned w
, unsigned h
,
217 for (i
= 0; i
< h
; i
++) {
218 uint32_t *pRow
= (uint32_t *)p
;
220 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
224 pRow
[3] = ((*src
++ >> 24) & 0xff);
231 /*** PIPE_FORMAT_X24S8_UINT ***/
234 * Return S component as four uint32_t in [0..255]. Z part ignored.
237 x24s8_get_tile_rgba(const unsigned *src
,
238 unsigned w
, unsigned h
,
244 for (i
= 0; i
< h
; i
++) {
245 uint32_t *pRow
= (uint32_t *)p
;
246 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
250 pRow
[3] = (*src
++ & 0xff);
258 * Return S component as four uint32_t in [0..255]. Z part ignored.
261 s8_get_tile_rgba(const unsigned char *src
,
262 unsigned w
, unsigned h
,
268 for (i
= 0; i
< h
; i
++) {
269 uint32_t *pRow
= (uint32_t *)p
;
270 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
274 pRow
[3] = (*src
++ & 0xff);
280 /*** PIPE_FORMAT_Z32_FLOAT ***/
283 * Return each Z value as four floats in [0,1].
286 z32f_get_tile_rgba(const float *src
,
287 unsigned w
, unsigned h
,
293 for (i
= 0; i
< h
; i
++) {
295 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
305 /*** PIPE_FORMAT_Z32_FLOAT_S8X24_UINT ***/
308 * Return each Z value as four floats in [0,1].
311 z32f_x24s8_get_tile_rgba(const float *src
,
312 unsigned w
, unsigned h
,
318 for (i
= 0; i
< h
; i
++) {
320 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
331 /*** PIPE_FORMAT_X32_S8X24_UINT ***/
334 * Return S component as four uint32_t in [0..255]. Z part ignored.
337 x32_s8_get_tile_rgba(const unsigned *src
,
338 unsigned w
, unsigned h
,
344 for (i
= 0; i
< h
; i
++) {
345 uint32_t *pRow
= (uint32_t *)p
;
346 for (j
= 0; j
< w
; j
++, pRow
+= 4) {
351 pRow
[3] = (*src
++ & 0xff);
358 pipe_put_tile_rgba(struct pipe_transfer
*pt
,
360 uint x
, uint y
, uint w
, uint h
,
361 enum pipe_format format
, const void *p
)
363 unsigned src_stride
= w
* 4;
365 if (u_clip_tile(x
, y
, &w
, &h
, &pt
->box
))
368 /* softpipe's S8_UINT texture cache fetch needs to take the rgba_format
369 * path, not ui (since there's no ui unpack for s8, but it's technically
372 if (util_format_is_pure_uint(format
)) {
373 util_format_write_4ui(format
,
374 p
, src_stride
* sizeof(float),
377 } else if (util_format_is_pure_sint(format
)) {
378 util_format_write_4i(format
,
379 p
, src_stride
* sizeof(float),
384 case PIPE_FORMAT_Z16_UNORM
:
385 /*z16_put_tile_rgba((ushort *) dst, w, h, p, src_stride);*/
387 case PIPE_FORMAT_Z32_UNORM
:
388 /*z32_put_tile_rgba((unsigned *) dst, w, h, p, src_stride);*/
390 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
391 case PIPE_FORMAT_Z24X8_UNORM
:
392 /*s8z24_put_tile_rgba((unsigned *) dst, w, h, p, src_stride);*/
394 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
395 case PIPE_FORMAT_X8Z24_UNORM
:
396 /*z24s8_put_tile_rgba((unsigned *) dst, w, h, p, src_stride);*/
398 case PIPE_FORMAT_Z32_FLOAT
:
399 /*z32f_put_tile_rgba((unsigned *) dst, w, h, p, src_stride);*/
401 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
402 /*z32f_s8x24_put_tile_rgba((unsigned *) dst, w, h, p, src_stride);*/
405 util_format_write_4f(format
,
406 p
, src_stride
* sizeof(float),
414 pipe_get_tile_rgba(struct pipe_transfer
*pt
,
416 uint x
, uint y
, uint w
, uint h
,
417 enum pipe_format format
,
420 unsigned dst_stride
= w
* 4;
423 if (u_clip_tile(x
, y
, &w
, &h
, &pt
->box
)) {
427 packed
= MALLOC(util_format_get_nblocks(format
, w
, h
) * util_format_get_blocksize(format
));
432 if (format
== PIPE_FORMAT_UYVY
|| format
== PIPE_FORMAT_YUYV
) {
433 assert((x
& 1) == 0);
436 pipe_get_tile_raw(pt
, src
, x
, y
, w
, h
, packed
, 0);
438 if (util_format_is_pure_uint(format
) &&
439 !util_format_is_depth_or_stencil(format
)) {
440 util_format_read_4ui(format
,
441 dst
, dst_stride
* sizeof(float),
442 packed
, util_format_get_stride(format
, w
),
444 } else if (util_format_is_pure_sint(format
)) {
445 util_format_read_4i(format
,
446 dst
, dst_stride
* sizeof(float),
447 packed
, util_format_get_stride(format
, w
),
451 case PIPE_FORMAT_Z16_UNORM
:
452 z16_get_tile_rgba((ushort
*) packed
, w
, h
, dst
, dst_stride
);
454 case PIPE_FORMAT_Z32_UNORM
:
455 z32_get_tile_rgba((unsigned *) packed
, w
, h
, dst
, dst_stride
);
457 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
458 case PIPE_FORMAT_Z24X8_UNORM
:
459 s8z24_get_tile_rgba((unsigned *) packed
, w
, h
, dst
, dst_stride
);
461 case PIPE_FORMAT_S8_UINT
:
462 s8_get_tile_rgba((unsigned char *) packed
, w
, h
, dst
, dst_stride
);
464 case PIPE_FORMAT_X24S8_UINT
:
465 s8x24_get_tile_rgba((unsigned *) packed
, w
, h
, dst
, dst_stride
);
467 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
468 case PIPE_FORMAT_X8Z24_UNORM
:
469 z24s8_get_tile_rgba((unsigned *) packed
, w
, h
, dst
, dst_stride
);
471 case PIPE_FORMAT_S8X24_UINT
:
472 x24s8_get_tile_rgba((unsigned *) packed
, w
, h
, dst
, dst_stride
);
474 case PIPE_FORMAT_Z32_FLOAT
:
475 z32f_get_tile_rgba((float *) packed
, w
, h
, dst
, dst_stride
);
477 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
478 z32f_x24s8_get_tile_rgba((float *) packed
, w
, h
, dst
, dst_stride
);
480 case PIPE_FORMAT_X32_S8X24_UINT
:
481 x32_s8_get_tile_rgba((unsigned *) packed
, w
, h
, dst
, dst_stride
);
484 util_format_read_4f(format
,
485 dst
, dst_stride
* sizeof(float),
486 packed
, util_format_get_stride(format
, w
),