gallium: unify transfer functions
[mesa.git] / src / gallium / auxiliary / util / u_tile.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 /**
29 * RGBA/float tile get/put functions.
30 * Usable both by drivers and state trackers.
31 */
32
33
34 #include "pipe/p_defines.h"
35 #include "util/u_inlines.h"
36
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"
42
43
44 /**
45 * Move raw block of pixels from transfer object to user memory.
46 */
47 void
48 pipe_get_tile_raw(struct pipe_transfer *pt,
49 const void *src,
50 uint x, uint y, uint w, uint h,
51 void *dst, int dst_stride)
52 {
53 if (dst_stride == 0)
54 dst_stride = util_format_get_stride(pt->resource->format, w);
55
56 if (u_clip_tile(x, y, &w, &h, &pt->box))
57 return;
58
59 util_copy_rect(dst, pt->resource->format, dst_stride, 0, 0, w, h, src, pt->stride, x, y);
60 }
61
62
63 /**
64 * Move raw block of pixels from user memory to transfer object.
65 */
66 void
67 pipe_put_tile_raw(struct pipe_transfer *pt,
68 void *dst,
69 uint x, uint y, uint w, uint h,
70 const void *src, int src_stride)
71 {
72 enum pipe_format format = pt->resource->format;
73
74 if (src_stride == 0)
75 src_stride = util_format_get_stride(format, w);
76
77 if (u_clip_tile(x, y, &w, &h, &pt->box))
78 return;
79
80 util_copy_rect(dst, format, pt->stride, x, y, w, h, src, src_stride, 0, 0);
81 }
82
83
84
85
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))
88
89 #define UNCLAMPED_FLOAT_TO_SHORT(us, f) \
90 us = ( (short) ( CLAMP((f), -1.0, 1.0) * 32767.0F) )
91
92
93
94 /*** PIPE_FORMAT_Z16_UNORM ***/
95
96 /**
97 * Return each Z value as four floats in [0,1].
98 */
99 static void
100 z16_get_tile_rgba(const ushort *src,
101 unsigned w, unsigned h,
102 float *p,
103 unsigned dst_stride)
104 {
105 const float scale = 1.0f / 65535.0f;
106 unsigned i, j;
107
108 for (i = 0; i < h; i++) {
109 float *pRow = p;
110 for (j = 0; j < w; j++, pRow += 4) {
111 pRow[0] =
112 pRow[1] =
113 pRow[2] =
114 pRow[3] = *src++ * scale;
115 }
116 p += dst_stride;
117 }
118 }
119
120
121
122
123 /*** PIPE_FORMAT_Z32_UNORM ***/
124
125 /**
126 * Return each Z value as four floats in [0,1].
127 */
128 static void
129 z32_get_tile_rgba(const unsigned *src,
130 unsigned w, unsigned h,
131 float *p,
132 unsigned dst_stride)
133 {
134 const double scale = 1.0 / (double) 0xffffffff;
135 unsigned i, j;
136
137 for (i = 0; i < h; i++) {
138 float *pRow = p;
139 for (j = 0; j < w; j++, pRow += 4) {
140 pRow[0] =
141 pRow[1] =
142 pRow[2] =
143 pRow[3] = (float) (*src++ * scale);
144 }
145 p += dst_stride;
146 }
147 }
148
149
150 /*** PIPE_FORMAT_Z24_UNORM_S8_UINT ***/
151
152 /**
153 * Return Z component as four float in [0,1]. Stencil part ignored.
154 */
155 static void
156 s8z24_get_tile_rgba(const unsigned *src,
157 unsigned w, unsigned h,
158 float *p,
159 unsigned dst_stride)
160 {
161 const double scale = 1.0 / ((1 << 24) - 1);
162 unsigned i, j;
163
164 for (i = 0; i < h; i++) {
165 float *pRow = p;
166 for (j = 0; j < w; j++, pRow += 4) {
167 pRow[0] =
168 pRow[1] =
169 pRow[2] =
170 pRow[3] = (float) (scale * (*src++ & 0xffffff));
171 }
172 p += dst_stride;
173 }
174 }
175
176
177 /*** PIPE_FORMAT_S8_UINT_Z24_UNORM ***/
178
179 /**
180 * Return Z component as four float in [0,1]. Stencil part ignored.
181 */
182 static void
183 z24s8_get_tile_rgba(const unsigned *src,
184 unsigned w, unsigned h,
185 float *p,
186 unsigned dst_stride)
187 {
188 const double scale = 1.0 / ((1 << 24) - 1);
189 unsigned i, j;
190
191 for (i = 0; i < h; i++) {
192 float *pRow = p;
193 for (j = 0; j < w; j++, pRow += 4) {
194 pRow[0] =
195 pRow[1] =
196 pRow[2] =
197 pRow[3] = (float) (scale * (*src++ >> 8));
198 }
199 p += dst_stride;
200 }
201 }
202
203 /*** PIPE_FORMAT_S8X24_UINT ***/
204
205 /**
206 * Return S component as four uint32_t in [0..255]. Z part ignored.
207 */
208 static void
209 s8x24_get_tile_rgba(const unsigned *src,
210 unsigned w, unsigned h,
211 float *p,
212 unsigned dst_stride)
213 {
214 unsigned i, j;
215
216 for (i = 0; i < h; i++) {
217 float *pRow = p;
218
219 for (j = 0; j < w; j++, pRow += 4) {
220 pRow[0] =
221 pRow[1] =
222 pRow[2] =
223 pRow[3] = (float)((*src++ >> 24) & 0xff);
224 }
225
226 p += dst_stride;
227 }
228 }
229
230 /*** PIPE_FORMAT_X24S8_UINT ***/
231
232 /**
233 * Return S component as four uint32_t in [0..255]. Z part ignored.
234 */
235 static void
236 x24s8_get_tile_rgba(const unsigned *src,
237 unsigned w, unsigned h,
238 float *p,
239 unsigned dst_stride)
240 {
241 unsigned i, j;
242
243 for (i = 0; i < h; i++) {
244 float *pRow = p;
245 for (j = 0; j < w; j++, pRow += 4) {
246 pRow[0] =
247 pRow[1] =
248 pRow[2] =
249 pRow[3] = (float)(*src++ & 0xff);
250 }
251 p += dst_stride;
252 }
253 }
254
255
256 /**
257 * Return S component as four uint32_t in [0..255]. Z part ignored.
258 */
259 static void
260 s8_get_tile_rgba(const unsigned char *src,
261 unsigned w, unsigned h,
262 float *p,
263 unsigned dst_stride)
264 {
265 unsigned i, j;
266
267 for (i = 0; i < h; i++) {
268 float *pRow = p;
269 for (j = 0; j < w; j++, pRow += 4) {
270 pRow[0] =
271 pRow[1] =
272 pRow[2] =
273 pRow[3] = (float)(*src++ & 0xff);
274 }
275 p += dst_stride;
276 }
277 }
278
279 /*** PIPE_FORMAT_Z32_FLOAT ***/
280
281 /**
282 * Return each Z value as four floats in [0,1].
283 */
284 static void
285 z32f_get_tile_rgba(const float *src,
286 unsigned w, unsigned h,
287 float *p,
288 unsigned dst_stride)
289 {
290 unsigned i, j;
291
292 for (i = 0; i < h; i++) {
293 float *pRow = p;
294 for (j = 0; j < w; j++, pRow += 4) {
295 pRow[0] =
296 pRow[1] =
297 pRow[2] =
298 pRow[3] = *src++;
299 }
300 p += dst_stride;
301 }
302 }
303
304 /*** PIPE_FORMAT_Z32_FLOAT_S8X24_UINT ***/
305
306 /**
307 * Return each Z value as four floats in [0,1].
308 */
309 static void
310 z32f_x24s8_get_tile_rgba(const float *src,
311 unsigned w, unsigned h,
312 float *p,
313 unsigned dst_stride)
314 {
315 unsigned i, j;
316
317 for (i = 0; i < h; i++) {
318 float *pRow = p;
319 for (j = 0; j < w; j++, pRow += 4) {
320 pRow[0] =
321 pRow[1] =
322 pRow[2] =
323 pRow[3] = *src;
324 src += 2;
325 }
326 p += dst_stride;
327 }
328 }
329
330 /*** PIPE_FORMAT_X32_S8X24_UINT ***/
331
332 /**
333 * Return S component as four uint32_t in [0..255]. Z part ignored.
334 */
335 static void
336 x32_s8_get_tile_rgba(const unsigned *src,
337 unsigned w, unsigned h,
338 float *p,
339 unsigned dst_stride)
340 {
341 unsigned i, j;
342
343 for (i = 0; i < h; i++) {
344 float *pRow = p;
345 for (j = 0; j < w; j++, pRow += 4) {
346 src++;
347 pRow[0] =
348 pRow[1] =
349 pRow[2] =
350 pRow[3] = (float)(*src++ & 0xff);
351 }
352 p += dst_stride;
353 }
354 }
355
356 void
357 pipe_tile_raw_to_rgba(enum pipe_format format,
358 const void *src,
359 uint w, uint h,
360 float *dst, unsigned dst_stride)
361 {
362 switch (format) {
363 case PIPE_FORMAT_Z16_UNORM:
364 z16_get_tile_rgba((ushort *) src, w, h, dst, dst_stride);
365 break;
366 case PIPE_FORMAT_Z32_UNORM:
367 z32_get_tile_rgba((unsigned *) src, w, h, dst, dst_stride);
368 break;
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);
372 break;
373 case PIPE_FORMAT_S8_UINT:
374 s8_get_tile_rgba((unsigned char *) src, w, h, dst, dst_stride);
375 break;
376 case PIPE_FORMAT_X24S8_UINT:
377 s8x24_get_tile_rgba((unsigned *) src, w, h, dst, dst_stride);
378 break;
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);
382 break;
383 case PIPE_FORMAT_S8X24_UINT:
384 x24s8_get_tile_rgba((unsigned *) src, w, h, dst, dst_stride);
385 break;
386 case PIPE_FORMAT_Z32_FLOAT:
387 z32f_get_tile_rgba((float *) src, w, h, dst, dst_stride);
388 break;
389 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
390 z32f_x24s8_get_tile_rgba((float *) src, w, h, dst, dst_stride);
391 break;
392 case PIPE_FORMAT_X32_S8X24_UINT:
393 x32_s8_get_tile_rgba((unsigned *) src, w, h, dst, dst_stride);
394 break;
395 default:
396 util_format_read_4f(format,
397 dst, dst_stride * sizeof(float),
398 src, util_format_get_stride(format, w),
399 0, 0, w, h);
400 }
401 }
402
403 void
404 pipe_tile_raw_to_unsigned(enum pipe_format format,
405 const void *src,
406 uint w, uint h,
407 unsigned *dst, unsigned dst_stride)
408 {
409 util_format_read_4ui(format,
410 dst, dst_stride * sizeof(float),
411 src, util_format_get_stride(format, w),
412 0, 0, w, h);
413 }
414
415 void
416 pipe_tile_raw_to_signed(enum pipe_format format,
417 void *src,
418 uint w, uint h,
419 int *dst, unsigned dst_stride)
420 {
421 util_format_read_4i(format,
422 dst, dst_stride * sizeof(float),
423 src, util_format_get_stride(format, w),
424 0, 0, w, h);
425 }
426
427 void
428 pipe_get_tile_rgba(struct pipe_transfer *pt,
429 const void *src,
430 uint x, uint y, uint w, uint h,
431 float *p)
432 {
433 pipe_get_tile_rgba_format(pt, src, x, y, w, h, pt->resource->format, p);
434 }
435
436
437 void
438 pipe_get_tile_rgba_format(struct pipe_transfer *pt,
439 const void *src,
440 uint x, uint y, uint w, uint h,
441 enum pipe_format format,
442 float *p)
443 {
444 unsigned dst_stride = w * 4;
445 void *packed;
446
447 if (u_clip_tile(x, y, &w, &h, &pt->box)) {
448 return;
449 }
450
451 packed = MALLOC(util_format_get_nblocks(format, w, h) * util_format_get_blocksize(format));
452 if (!packed) {
453 return;
454 }
455
456 if (format == PIPE_FORMAT_UYVY || format == PIPE_FORMAT_YUYV) {
457 assert((x & 1) == 0);
458 }
459
460 pipe_get_tile_raw(pt, src, x, y, w, h, packed, 0);
461
462 pipe_tile_raw_to_rgba(format, packed, w, h, p, dst_stride);
463
464 FREE(packed);
465 }
466
467
468 void
469 pipe_put_tile_rgba(struct pipe_transfer *pt,
470 void *dst,
471 uint x, uint y, uint w, uint h,
472 const float *p)
473 {
474 pipe_put_tile_rgba_format(pt, dst, x, y, w, h, pt->resource->format, p);
475 }
476
477
478 void
479 pipe_put_tile_rgba_format(struct pipe_transfer *pt,
480 void *dst,
481 uint x, uint y, uint w, uint h,
482 enum pipe_format format,
483 const float *p)
484 {
485 unsigned src_stride = w * 4;
486 void *packed;
487
488 if (u_clip_tile(x, y, &w, &h, &pt->box))
489 return;
490
491 packed = MALLOC(util_format_get_nblocks(format, w, h) * util_format_get_blocksize(format));
492
493 if (!packed)
494 return;
495
496 switch (format) {
497 case PIPE_FORMAT_Z16_UNORM:
498 /*z16_put_tile_rgba((ushort *) packed, w, h, p, src_stride);*/
499 break;
500 case PIPE_FORMAT_Z32_UNORM:
501 /*z32_put_tile_rgba((unsigned *) packed, w, h, p, src_stride);*/
502 break;
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);*/
506 break;
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);*/
510 break;
511 case PIPE_FORMAT_Z32_FLOAT:
512 /*z32f_put_tile_rgba((unsigned *) packed, w, h, p, src_stride);*/
513 break;
514 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
515 /*z32f_s8x24_put_tile_rgba((unsigned *) packed, w, h, p, src_stride);*/
516 break;
517 default:
518 util_format_write_4f(format,
519 p, src_stride * sizeof(float),
520 packed, util_format_get_stride(format, w),
521 0, 0, w, h);
522 }
523
524 pipe_put_tile_raw(pt, dst, x, y, w, h, packed, 0);
525
526 FREE(packed);
527 }
528
529 void
530 pipe_put_tile_i_format(struct pipe_transfer *pt,
531 void *dst,
532 uint x, uint y, uint w, uint h,
533 enum pipe_format format,
534 const int *p)
535 {
536 unsigned src_stride = w * 4;
537 void *packed;
538
539 if (u_clip_tile(x, y, &w, &h, &pt->box))
540 return;
541
542 packed = MALLOC(util_format_get_nblocks(format, w, h) * util_format_get_blocksize(format));
543
544 if (!packed)
545 return;
546
547 util_format_write_4i(format,
548 p, src_stride * sizeof(float),
549 packed, util_format_get_stride(format, w),
550 0, 0, w, h);
551
552 pipe_put_tile_raw(pt, dst, x, y, w, h, packed, 0);
553
554 FREE(packed);
555 }
556
557 void
558 pipe_put_tile_ui_format(struct pipe_transfer *pt,
559 void *dst,
560 uint x, uint y, uint w, uint h,
561 enum pipe_format format,
562 const unsigned int *p)
563 {
564 unsigned src_stride = w * 4;
565 void *packed;
566
567 if (u_clip_tile(x, y, &w, &h, &pt->box))
568 return;
569
570 packed = MALLOC(util_format_get_nblocks(format, w, h) * util_format_get_blocksize(format));
571
572 if (!packed)
573 return;
574
575 util_format_write_4ui(format,
576 p, src_stride * sizeof(float),
577 packed, util_format_get_stride(format, w),
578 0, 0, w, h);
579
580 pipe_put_tile_raw(pt, dst, x, y, w, h, packed, 0);
581
582 FREE(packed);
583 }
584
585 /**
586 * Get a block of Z values, converted to 32-bit range.
587 */
588 void
589 pipe_get_tile_z(struct pipe_transfer *pt,
590 const void *src,
591 uint x, uint y, uint w, uint h,
592 uint *z)
593 {
594 const uint dstStride = w;
595 const ubyte *map = src;
596 uint *pDest = z;
597 uint i, j;
598 enum pipe_format format = pt->resource->format;
599
600 if (u_clip_tile(x, y, &w, &h, &pt->box))
601 return;
602
603 switch (format) {
604 case PIPE_FORMAT_Z32_UNORM:
605 {
606 const uint *ptrc
607 = (const uint *)(map + y * pt->stride + x*4);
608 for (i = 0; i < h; i++) {
609 memcpy(pDest, ptrc, 4 * w);
610 pDest += dstStride;
611 ptrc += pt->stride/4;
612 }
613 }
614 break;
615 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
616 case PIPE_FORMAT_Z24X8_UNORM:
617 {
618 const uint *ptrc
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);
624 }
625 pDest += dstStride;
626 ptrc += pt->stride/4;
627 }
628 }
629 break;
630 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
631 case PIPE_FORMAT_X8Z24_UNORM:
632 {
633 const uint *ptrc
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);
639 }
640 pDest += dstStride;
641 ptrc += pt->stride/4;
642 }
643 }
644 break;
645 case PIPE_FORMAT_Z16_UNORM:
646 {
647 const ushort *ptrc
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];
653 }
654 pDest += dstStride;
655 ptrc += pt->stride/2;
656 }
657 }
658 break;
659 case PIPE_FORMAT_Z32_FLOAT:
660 {
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) {
666 pDest[j] = 0;
667 }
668 else if (ptrc[j] >= 1.0) {
669 pDest[j] = 0xffffffff;
670 }
671 else {
672 double z = ptrc[j] * 0xffffffff;
673 pDest[j] = (uint) z;
674 }
675 }
676 pDest += dstStride;
677 ptrc += pt->stride/4;
678 }
679 }
680 break;
681 default:
682 assert(0);
683 }
684 }
685
686
687 void
688 pipe_put_tile_z(struct pipe_transfer *pt,
689 void *dst,
690 uint x, uint y, uint w, uint h,
691 const uint *zSrc)
692 {
693 const uint srcStride = w;
694 const uint *ptrc = zSrc;
695 ubyte *map = dst;
696 uint i, j;
697 enum pipe_format format = pt->resource->format;
698
699 if (u_clip_tile(x, y, &w, &h, &pt->box))
700 return;
701
702 switch (format) {
703 case PIPE_FORMAT_Z32_UNORM:
704 {
705 uint *pDest = (uint *) (map + y * pt->stride + x*4);
706 for (i = 0; i < h; i++) {
707 memcpy(pDest, ptrc, 4 * w);
708 pDest += pt->stride/4;
709 ptrc += srcStride;
710 }
711 }
712 break;
713 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
714 {
715 uint *pDest = (uint *) (map + y * pt->stride + x*4);
716 /*assert((pt->usage & PIPE_TRANSFER_READ_WRITE) == PIPE_TRANSFER_READ_WRITE);*/
717 for (i = 0; i < h; i++) {
718 for (j = 0; j < w; j++) {
719 /* convert 32-bit Z to 24-bit Z, preserve stencil */
720 pDest[j] = (pDest[j] & 0xff000000) | ptrc[j] >> 8;
721 }
722 pDest += pt->stride/4;
723 ptrc += srcStride;
724 }
725 }
726 break;
727 case PIPE_FORMAT_Z24X8_UNORM:
728 {
729 uint *pDest = (uint *) (map + y * pt->stride + x*4);
730 for (i = 0; i < h; i++) {
731 for (j = 0; j < w; j++) {
732 /* convert 32-bit Z to 24-bit Z (0 stencil) */
733 pDest[j] = ptrc[j] >> 8;
734 }
735 pDest += pt->stride/4;
736 ptrc += srcStride;
737 }
738 }
739 break;
740 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
741 {
742 uint *pDest = (uint *) (map + y * pt->stride + x*4);
743 /*assert((pt->usage & PIPE_TRANSFER_READ_WRITE) == PIPE_TRANSFER_READ_WRITE);*/
744 for (i = 0; i < h; i++) {
745 for (j = 0; j < w; j++) {
746 /* convert 32-bit Z to 24-bit Z, preserve stencil */
747 pDest[j] = (pDest[j] & 0xff) | (ptrc[j] & 0xffffff00);
748 }
749 pDest += pt->stride/4;
750 ptrc += srcStride;
751 }
752 }
753 break;
754 case PIPE_FORMAT_X8Z24_UNORM:
755 {
756 uint *pDest = (uint *) (map + y * pt->stride + x*4);
757 for (i = 0; i < h; i++) {
758 for (j = 0; j < w; j++) {
759 /* convert 32-bit Z to 24-bit Z (0 stencil) */
760 pDest[j] = ptrc[j] & 0xffffff00;
761 }
762 pDest += pt->stride/4;
763 ptrc += srcStride;
764 }
765 }
766 break;
767 case PIPE_FORMAT_Z16_UNORM:
768 {
769 ushort *pDest = (ushort *) (map + y * pt->stride + x*2);
770 for (i = 0; i < h; i++) {
771 for (j = 0; j < w; j++) {
772 /* convert 32-bit Z to 16-bit Z */
773 pDest[j] = ptrc[j] >> 16;
774 }
775 pDest += pt->stride/2;
776 ptrc += srcStride;
777 }
778 }
779 break;
780 case PIPE_FORMAT_Z32_FLOAT:
781 {
782 float *pDest = (float *) (map + y * pt->stride + x*2);
783 for (i = 0; i < h; i++) {
784 for (j = 0; j < w; j++) {
785 /* convert 32-bit integer Z to float Z */
786 const double scale = 1.0 / 0xffffffffU;
787 pDest[j] = ptrc[j] * scale;
788 }
789 pDest += pt->stride/4;
790 ptrc += srcStride;
791 }
792 }
793 break;
794 default:
795 assert(0);
796 }
797 }
798
799
800 void
801 pipe_get_tile_ui_format(struct pipe_transfer *pt,
802 const void *src,
803 uint x, uint y, uint w, uint h,
804 enum pipe_format format,
805 unsigned int *p)
806 {
807 unsigned dst_stride = w * 4;
808 void *packed;
809
810 if (u_clip_tile(x, y, &w, &h, &pt->box)) {
811 return;
812 }
813
814 packed = MALLOC(util_format_get_nblocks(format, w, h) * util_format_get_blocksize(format));
815 if (!packed) {
816 return;
817 }
818
819 if (format == PIPE_FORMAT_UYVY || format == PIPE_FORMAT_YUYV) {
820 assert((x & 1) == 0);
821 }
822
823 pipe_get_tile_raw(pt, src, x, y, w, h, packed, 0);
824
825 pipe_tile_raw_to_unsigned(format, packed, w, h, p, dst_stride);
826
827 FREE(packed);
828 }
829
830
831 void
832 pipe_get_tile_i_format(struct pipe_transfer *pt,
833 const void *src,
834 uint x, uint y, uint w, uint h,
835 enum pipe_format format,
836 int *p)
837 {
838 unsigned dst_stride = w * 4;
839 void *packed;
840
841 if (u_clip_tile(x, y, &w, &h, &pt->box)) {
842 return;
843 }
844
845 packed = MALLOC(util_format_get_nblocks(format, w, h) * util_format_get_blocksize(format));
846 if (!packed) {
847 return;
848 }
849
850 if (format == PIPE_FORMAT_UYVY || format == PIPE_FORMAT_YUYV) {
851 assert((x & 1) == 0);
852 }
853
854 pipe_get_tile_raw(pt, src, x, y, w, h, packed, 0);
855
856 pipe_tile_raw_to_signed(format, packed, w, h, p, dst_stride);
857
858 FREE(packed);
859 }