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->texture->format, w);
58 if (pipe_clip_tile(x, y, &w, &h, pt))
61 src = pipe->transfer_map(pipe, pt);
66 util_copy_rect(dst, pt->texture->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->texture->format;
85 src_stride = util_format_get_stride(format, w);
87 if (pipe_clip_tile(x, y, &w, &h, pt))
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));
221 /*** PIPE_FORMAT_Z32_FLOAT ***/
224 * Return each Z value as four floats in [0,1].
227 z32f_get_tile_rgba(const float *src,
228 unsigned w, unsigned h,
234 for (i = 0; i < h; i++) {
236 for (j = 0; j < w; j++, pRow += 4) {
248 pipe_tile_raw_to_rgba(enum pipe_format format,
251 float *dst, unsigned dst_stride)
254 case PIPE_FORMAT_Z16_UNORM:
255 z16_get_tile_rgba((ushort *) src, w, h, dst, dst_stride);
257 case PIPE_FORMAT_Z32_UNORM:
258 z32_get_tile_rgba((unsigned *) src, w, h, dst, dst_stride);
260 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
261 case PIPE_FORMAT_Z24X8_UNORM:
262 s8z24_get_tile_rgba((unsigned *) src, w, h, dst, dst_stride);
264 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
265 case PIPE_FORMAT_X8Z24_UNORM:
266 z24s8_get_tile_rgba((unsigned *) src, w, h, dst, dst_stride);
268 case PIPE_FORMAT_Z32_FLOAT:
269 z32f_get_tile_rgba((float *) src, w, h, dst, dst_stride);
272 util_format_read_4f(format,
273 dst, dst_stride * sizeof(float),
274 src, util_format_get_stride(format, w),
281 pipe_get_tile_rgba(struct pipe_context *pipe,
282 struct pipe_transfer *pt,
283 uint x, uint y, uint w, uint h,
286 unsigned dst_stride = w * 4;
288 enum pipe_format format = pt->texture->format;
290 if (pipe_clip_tile(x, y, &w, &h, pt))
293 packed = MALLOC(util_format_get_nblocks(format, w, h) * util_format_get_blocksize(format));
298 if(format == PIPE_FORMAT_UYVY || format == PIPE_FORMAT_YUYV)
299 assert((x & 1) == 0);
301 pipe_get_tile_raw(pipe, pt, x, y, w, h, packed, 0);
303 pipe_tile_raw_to_rgba(format, packed, w, h, p, dst_stride);
310 pipe_get_tile_swizzle(struct pipe_context *pipe,
311 struct pipe_transfer *pt,
320 enum pipe_format format,
323 unsigned dst_stride = w * 4;
328 if (pipe_clip_tile(x, y, &w, &h, pt)) {
332 packed = MALLOC(util_format_get_nblocks(format, w, h) * util_format_get_blocksize(format));
337 if (format == PIPE_FORMAT_UYVY || format == PIPE_FORMAT_YUYV) {
338 assert((x & 1) == 0);
341 pipe_get_tile_raw(pipe, pt, x, y, w, h, packed, 0);
343 pipe_tile_raw_to_rgba(format, packed, w, h, p, dst_stride);
347 if (swizzle_r == PIPE_SWIZZLE_RED &&
348 swizzle_g == PIPE_SWIZZLE_GREEN &&
349 swizzle_b == PIPE_SWIZZLE_BLUE &&
350 swizzle_a == PIPE_SWIZZLE_ALPHA) {
355 rgba01[PIPE_SWIZZLE_ZERO] = 0.0f;
356 rgba01[PIPE_SWIZZLE_ONE] = 1.0f;
358 for (iy = 0; iy < h; iy++) {
362 for (ix = 0; ix < w; ix++) {
363 rgba01[PIPE_SWIZZLE_RED] = row[0];
364 rgba01[PIPE_SWIZZLE_GREEN] = row[1];
365 rgba01[PIPE_SWIZZLE_BLUE] = row[2];
366 rgba01[PIPE_SWIZZLE_ALPHA] = row[3];
368 *row++ = rgba01[swizzle_r];
369 *row++ = rgba01[swizzle_g];
370 *row++ = rgba01[swizzle_b];
371 *row++ = rgba01[swizzle_a];
379 pipe_put_tile_rgba(struct pipe_context *pipe,
380 struct pipe_transfer *pt,
381 uint x, uint y, uint w, uint h,
384 unsigned src_stride = w * 4;
386 enum pipe_format format = pt->texture->format;
388 if (pipe_clip_tile(x, y, &w, &h, pt))
391 packed = MALLOC(util_format_get_nblocks(format, w, h) * util_format_get_blocksize(format));
397 case PIPE_FORMAT_Z16_UNORM:
398 /*z16_put_tile_rgba((ushort *) packed, w, h, p, src_stride);*/
400 case PIPE_FORMAT_Z32_UNORM:
401 /*z32_put_tile_rgba((unsigned *) packed, w, h, p, src_stride);*/
403 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
404 case PIPE_FORMAT_Z24X8_UNORM:
405 /*s8z24_put_tile_rgba((unsigned *) packed, w, h, p, src_stride);*/
407 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
408 case PIPE_FORMAT_X8Z24_UNORM:
409 /*z24s8_put_tile_rgba((unsigned *) packed, w, h, p, src_stride);*/
412 util_format_write_4f(format,
413 p, src_stride * sizeof(float),
414 packed, util_format_get_stride(format, w),
418 pipe_put_tile_raw(pipe, pt, x, y, w, h, packed, 0);
425 * Get a block of Z values, converted to 32-bit range.
428 pipe_get_tile_z(struct pipe_context *pipe,
429 struct pipe_transfer *pt,
430 uint x, uint y, uint w, uint h,
433 const uint dstStride = w;
437 enum pipe_format format = pt->texture->format;
439 if (pipe_clip_tile(x, y, &w, &h, pt))
442 map = (ubyte *)pipe->transfer_map(pipe, pt);
449 case PIPE_FORMAT_Z32_UNORM:
452 = (const uint *)(map + y * pt->stride + x*4);
453 for (i = 0; i < h; i++) {
454 memcpy(pDest, ptrc, 4 * w);
456 ptrc += pt->stride/4;
460 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
461 case PIPE_FORMAT_Z24X8_UNORM:
464 = (const uint *)(map + y * pt->stride + x*4);
465 for (i = 0; i < h; i++) {
466 for (j = 0; j < w; j++) {
467 /* convert 24-bit Z to 32-bit Z */
468 pDest[j] = (ptrc[j] << 8) | ((ptrc[j] >> 16) & 0xff);
471 ptrc += pt->stride/4;
475 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
476 case PIPE_FORMAT_X8Z24_UNORM:
479 = (const uint *)(map + y * pt->stride + x*4);
480 for (i = 0; i < h; i++) {
481 for (j = 0; j < w; j++) {
482 /* convert 24-bit Z to 32-bit Z */
483 pDest[j] = (ptrc[j] & 0xffffff00) | ((ptrc[j] >> 24) & 0xff);
486 ptrc += pt->stride/4;
490 case PIPE_FORMAT_Z16_UNORM:
493 = (const ushort *)(map + y * pt->stride + x*2);
494 for (i = 0; i < h; i++) {
495 for (j = 0; j < w; j++) {
496 /* convert 16-bit Z to 32-bit Z */
497 pDest[j] = (ptrc[j] << 16) | ptrc[j];
500 ptrc += pt->stride/2;
508 pipe->transfer_unmap(pipe, pt);
513 pipe_put_tile_z(struct pipe_context *pipe,
514 struct pipe_transfer *pt,
515 uint x, uint y, uint w, uint h,
518 const uint srcStride = w;
519 const uint *ptrc = zSrc;
522 enum pipe_format format = pt->texture->format;
524 if (pipe_clip_tile(x, y, &w, &h, pt))
527 map = (ubyte *)pipe->transfer_map(pipe, pt);
534 case PIPE_FORMAT_Z32_UNORM:
536 uint *pDest = (uint *) (map + y * pt->stride + x*4);
537 for (i = 0; i < h; i++) {
538 memcpy(pDest, ptrc, 4 * w);
539 pDest += pt->stride/4;
544 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
546 uint *pDest = (uint *) (map + y * pt->stride + x*4);
547 assert((pt->usage & PIPE_TRANSFER_READ_WRITE) == PIPE_TRANSFER_READ_WRITE);
548 for (i = 0; i < h; i++) {
549 for (j = 0; j < w; j++) {
550 /* convert 32-bit Z to 24-bit Z, preserve stencil */
551 pDest[j] = (pDest[j] & 0xff000000) | ptrc[j] >> 8;
553 pDest += pt->stride/4;
558 case PIPE_FORMAT_Z24X8_UNORM:
560 uint *pDest = (uint *) (map + y * pt->stride + x*4);
561 for (i = 0; i < h; i++) {
562 for (j = 0; j < w; j++) {
563 /* convert 32-bit Z to 24-bit Z (0 stencil) */
564 pDest[j] = ptrc[j] >> 8;
566 pDest += pt->stride/4;
571 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
573 uint *pDest = (uint *) (map + y * pt->stride + x*4);
574 assert((pt->usage & PIPE_TRANSFER_READ_WRITE) == PIPE_TRANSFER_READ_WRITE);
575 for (i = 0; i < h; i++) {
576 for (j = 0; j < w; j++) {
577 /* convert 32-bit Z to 24-bit Z, preserve stencil */
578 pDest[j] = (pDest[j] & 0xff) | (ptrc[j] & 0xffffff00);
580 pDest += pt->stride/4;
585 case PIPE_FORMAT_X8Z24_UNORM:
587 uint *pDest = (uint *) (map + y * pt->stride + x*4);
588 for (i = 0; i < h; i++) {
589 for (j = 0; j < w; j++) {
590 /* convert 32-bit Z to 24-bit Z (0 stencil) */
591 pDest[j] = ptrc[j] & 0xffffff00;
593 pDest += pt->stride/4;
598 case PIPE_FORMAT_Z16_UNORM:
600 ushort *pDest = (ushort *) (map + y * pt->stride + x*2);
601 for (i = 0; i < h; i++) {
602 for (j = 0; j < w; j++) {
603 /* convert 32-bit Z to 16-bit Z */
604 pDest[j] = ptrc[j] >> 16;
606 pDest += pt->stride/2;
615 pipe->transfer_unmap(pipe, pt);