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) {
323 pipe_tile_raw_to_rgba(enum pipe_format format,
326 float *dst, unsigned dst_stride)
329 case PIPE_FORMAT_Z16_UNORM:
330 z16_get_tile_rgba((ushort *) src, w, h, dst, dst_stride);
332 case PIPE_FORMAT_Z32_UNORM:
333 z32_get_tile_rgba((unsigned *) src, w, h, dst, dst_stride);
335 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
336 case PIPE_FORMAT_Z24X8_UNORM:
337 s8z24_get_tile_rgba((unsigned *) src, w, h, dst, dst_stride);
339 case PIPE_FORMAT_S8_USCALED:
340 s8_get_tile_rgba((unsigned char *) src, w, h, dst, dst_stride);
342 case PIPE_FORMAT_X24S8_USCALED:
343 s8x24_get_tile_rgba((unsigned *) src, w, h, dst, dst_stride);
345 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
346 case PIPE_FORMAT_X8Z24_UNORM:
347 z24s8_get_tile_rgba((unsigned *) src, w, h, dst, dst_stride);
349 case PIPE_FORMAT_S8X24_USCALED:
350 x24s8_get_tile_rgba((unsigned *) src, w, h, dst, dst_stride);
352 case PIPE_FORMAT_Z32_FLOAT:
353 z32f_get_tile_rgba((float *) src, w, h, dst, dst_stride);
356 util_format_read_4f(format,
357 dst, dst_stride * sizeof(float),
358 src, util_format_get_stride(format, w),
365 pipe_get_tile_rgba(struct pipe_context *pipe,
366 struct pipe_transfer *pt,
367 uint x, uint y, uint w, uint h,
370 pipe_get_tile_rgba_format(pipe, pt, x, y, w, h, pt->resource->format, p);
375 pipe_get_tile_rgba_format(struct pipe_context *pipe,
376 struct pipe_transfer *pt,
377 uint x, uint y, uint w, uint h,
378 enum pipe_format format,
381 unsigned dst_stride = w * 4;
384 if (u_clip_tile(x, y, &w, &h, &pt->box)) {
388 packed = MALLOC(util_format_get_nblocks(format, w, h) * util_format_get_blocksize(format));
393 if (format == PIPE_FORMAT_UYVY || format == PIPE_FORMAT_YUYV) {
394 assert((x & 1) == 0);
397 pipe_get_tile_raw(pipe, pt, x, y, w, h, packed, 0);
399 pipe_tile_raw_to_rgba(format, packed, w, h, p, dst_stride);
406 pipe_put_tile_rgba(struct pipe_context *pipe,
407 struct pipe_transfer *pt,
408 uint x, uint y, uint w, uint h,
411 pipe_put_tile_rgba_format(pipe, pt, x, y, w, h, pt->resource->format, p);
416 pipe_put_tile_rgba_format(struct pipe_context *pipe,
417 struct pipe_transfer *pt,
418 uint x, uint y, uint w, uint h,
419 enum pipe_format format,
422 unsigned src_stride = w * 4;
425 if (u_clip_tile(x, y, &w, &h, &pt->box))
428 packed = MALLOC(util_format_get_nblocks(format, w, h) * util_format_get_blocksize(format));
434 case PIPE_FORMAT_Z16_UNORM:
435 /*z16_put_tile_rgba((ushort *) packed, w, h, p, src_stride);*/
437 case PIPE_FORMAT_Z32_UNORM:
438 /*z32_put_tile_rgba((unsigned *) packed, w, h, p, src_stride);*/
440 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
441 case PIPE_FORMAT_Z24X8_UNORM:
442 /*s8z24_put_tile_rgba((unsigned *) packed, w, h, p, src_stride);*/
444 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
445 case PIPE_FORMAT_X8Z24_UNORM:
446 /*z24s8_put_tile_rgba((unsigned *) packed, w, h, p, src_stride);*/
449 util_format_write_4f(format,
450 p, src_stride * sizeof(float),
451 packed, util_format_get_stride(format, w),
455 pipe_put_tile_raw(pipe, pt, x, y, w, h, packed, 0);
462 * Get a block of Z values, converted to 32-bit range.
465 pipe_get_tile_z(struct pipe_context *pipe,
466 struct pipe_transfer *pt,
467 uint x, uint y, uint w, uint h,
470 const uint dstStride = w;
474 enum pipe_format format = pt->resource->format;
476 if (u_clip_tile(x, y, &w, &h, &pt->box))
479 map = (ubyte *)pipe->transfer_map(pipe, pt);
486 case PIPE_FORMAT_Z32_UNORM:
489 = (const uint *)(map + y * pt->stride + x*4);
490 for (i = 0; i < h; i++) {
491 memcpy(pDest, ptrc, 4 * w);
493 ptrc += pt->stride/4;
497 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
498 case PIPE_FORMAT_Z24X8_UNORM:
501 = (const uint *)(map + y * pt->stride + x*4);
502 for (i = 0; i < h; i++) {
503 for (j = 0; j < w; j++) {
504 /* convert 24-bit Z to 32-bit Z */
505 pDest[j] = (ptrc[j] << 8) | ((ptrc[j] >> 16) & 0xff);
508 ptrc += pt->stride/4;
512 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
513 case PIPE_FORMAT_X8Z24_UNORM:
516 = (const uint *)(map + y * pt->stride + x*4);
517 for (i = 0; i < h; i++) {
518 for (j = 0; j < w; j++) {
519 /* convert 24-bit Z to 32-bit Z */
520 pDest[j] = (ptrc[j] & 0xffffff00) | ((ptrc[j] >> 24) & 0xff);
523 ptrc += pt->stride/4;
527 case PIPE_FORMAT_Z16_UNORM:
530 = (const ushort *)(map + y * pt->stride + x*2);
531 for (i = 0; i < h; i++) {
532 for (j = 0; j < w; j++) {
533 /* convert 16-bit Z to 32-bit Z */
534 pDest[j] = (ptrc[j] << 16) | ptrc[j];
537 ptrc += pt->stride/2;
545 pipe->transfer_unmap(pipe, pt);
550 pipe_put_tile_z(struct pipe_context *pipe,
551 struct pipe_transfer *pt,
552 uint x, uint y, uint w, uint h,
555 const uint srcStride = w;
556 const uint *ptrc = zSrc;
559 enum pipe_format format = pt->resource->format;
561 if (u_clip_tile(x, y, &w, &h, &pt->box))
564 map = (ubyte *)pipe->transfer_map(pipe, pt);
571 case PIPE_FORMAT_Z32_UNORM:
573 uint *pDest = (uint *) (map + y * pt->stride + x*4);
574 for (i = 0; i < h; i++) {
575 memcpy(pDest, ptrc, 4 * w);
576 pDest += pt->stride/4;
581 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
583 uint *pDest = (uint *) (map + y * pt->stride + x*4);
584 /*assert((pt->usage & PIPE_TRANSFER_READ_WRITE) == PIPE_TRANSFER_READ_WRITE);*/
585 for (i = 0; i < h; i++) {
586 for (j = 0; j < w; j++) {
587 /* convert 32-bit Z to 24-bit Z, preserve stencil */
588 pDest[j] = (pDest[j] & 0xff000000) | ptrc[j] >> 8;
590 pDest += pt->stride/4;
595 case PIPE_FORMAT_Z24X8_UNORM:
597 uint *pDest = (uint *) (map + y * pt->stride + x*4);
598 for (i = 0; i < h; i++) {
599 for (j = 0; j < w; j++) {
600 /* convert 32-bit Z to 24-bit Z (0 stencil) */
601 pDest[j] = ptrc[j] >> 8;
603 pDest += pt->stride/4;
608 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
610 uint *pDest = (uint *) (map + y * pt->stride + x*4);
611 /*assert((pt->usage & PIPE_TRANSFER_READ_WRITE) == PIPE_TRANSFER_READ_WRITE);*/
612 for (i = 0; i < h; i++) {
613 for (j = 0; j < w; j++) {
614 /* convert 32-bit Z to 24-bit Z, preserve stencil */
615 pDest[j] = (pDest[j] & 0xff) | (ptrc[j] & 0xffffff00);
617 pDest += pt->stride/4;
622 case PIPE_FORMAT_X8Z24_UNORM:
624 uint *pDest = (uint *) (map + y * pt->stride + x*4);
625 for (i = 0; i < h; i++) {
626 for (j = 0; j < w; j++) {
627 /* convert 32-bit Z to 24-bit Z (0 stencil) */
628 pDest[j] = ptrc[j] & 0xffffff00;
630 pDest += pt->stride/4;
635 case PIPE_FORMAT_Z16_UNORM:
637 ushort *pDest = (ushort *) (map + y * pt->stride + x*2);
638 for (i = 0; i < h; i++) {
639 for (j = 0; j < w; j++) {
640 /* convert 32-bit Z to 16-bit Z */
641 pDest[j] = ptrc[j] >> 16;
643 pDest += pt->stride/2;
652 pipe->transfer_unmap(pipe, pt);