1/**************************************************************************
2 *
3 * Copyright 2010 VMware, Inc.
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 SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
24 * of the Software.
25 *
26 **************************************************************************/
27
28
29#include "u_debug.h"
30#include "u_math.h"
31#include "u_format_zs.h"
32
33
34/*
35 * z32_unorm conversion functions
36 */
37
38static inline uint16_t
39z32_unorm_to_z16_unorm(uint32_t z)
40{
41   /* z * 0xffff / 0xffffffff */
42   return z >> 16;
43}
44
45static inline uint32_t
46z16_unorm_to_z32_unorm(uint16_t z)
47{
48   /* z * 0xffffffff / 0xffff */
49   return (z << 16) | z;
50}
51
52static inline uint32_t
53z32_unorm_to_z24_unorm(uint32_t z)
54{
55   /* z * 0xffffff / 0xffffffff */
56   return z >> 8;
57}
58
59static inline uint32_t
60z24_unorm_to_z32_unorm(uint32_t z)
61{
62   /* z * 0xffffffff / 0xffffff */
63   return (z << 8) | (z >> 16);
64}
65
66
67/*
68 * z32_float conversion functions
69 */
70
71static inline uint16_t
72z32_float_to_z16_unorm(float z)
73{
74   const float scale = 0xffff;
75   return (uint16_t)(z * scale + 0.5f);
76}
77
78static inline float
79z16_unorm_to_z32_float(uint16_t z)
80{
81   const float scale = 1.0 / 0xffff;
82   return (float)(z * scale);
83}
84
85static inline uint32_t
86z32_float_to_z24_unorm(float z)
87{
88   const double scale = 0xffffff;
89   return (uint32_t)(z * scale) & 0xffffff;
90}
91
92static inline float
93z24_unorm_to_z32_float(uint32_t z)
94{
95   const double scale = 1.0 / 0xffffff;
96   return (float)(z * scale);
97}
98
99static inline uint32_t
100z32_float_to_z32_unorm(float z)
101{
102   const double scale = 0xffffffff;
103   return (uint32_t)(z * scale);
104}
105
106static inline float
107z32_unorm_to_z32_float(uint32_t z)
108{
109   const double scale = 1.0 / 0xffffffff;
110   return (float)(z * scale);
111}
112
113
114void
115util_format_s8_uint_unpack_s_8uint(uint8_t *dst_row, unsigned dst_stride,
116                                         const uint8_t *src_row, unsigned src_stride,
117                                         unsigned width, unsigned height)
118{
119   unsigned y;
120   for(y = 0; y < height; ++y) {
121      memcpy(dst_row, src_row, width);
122      src_row += src_stride/sizeof(*src_row);
123      dst_row += dst_stride/sizeof(*dst_row);
124   }
125}
126
127void
128util_format_s8_uint_pack_s_8uint(uint8_t *dst_row, unsigned dst_stride,
129                                       const uint8_t *src_row, unsigned src_stride,
130                                       unsigned width, unsigned height)
131{
132   unsigned y;
133   for(y = 0; y < height; ++y) {
134      memcpy(dst_row, src_row, width);
135      src_row += src_stride/sizeof(*src_row);
136      dst_row += dst_stride/sizeof(*dst_row);
137   }
138}
139
140void
141util_format_z16_unorm_unpack_z_float(float *dst_row, unsigned dst_stride,
142                                     const uint8_t *src_row, unsigned src_stride,
143                                     unsigned width, unsigned height)
144{
145   unsigned x, y;
146   for(y = 0; y < height; ++y) {
147      float *dst = dst_row;
148      const uint16_t *src = (const uint16_t *)src_row;
149      for(x = 0; x < width; ++x) {
150         uint16_t value = util_cpu_to_le16(*src++);
151         *dst++ = z16_unorm_to_z32_float(value);
152      }
153      src_row += src_stride/sizeof(*src_row);
154      dst_row += dst_stride/sizeof(*dst_row);
155   }
156}
157
158void
159util_format_z16_unorm_pack_z_float(uint8_t *dst_row, unsigned dst_stride,
160                                   const float *src_row, unsigned src_stride,
161                                   unsigned width, unsigned height)
162{
163   unsigned x, y;
164   for(y = 0; y < height; ++y) {
165      const float *src = src_row;
166      uint16_t *dst = (uint16_t *)dst_row;
167      for(x = 0; x < width; ++x) {
168         uint16_t value;
169         value = z32_float_to_z16_unorm(*src++);
170         *dst++ = util_le16_to_cpu(value);
171      }
172      dst_row += dst_stride/sizeof(*dst_row);
173      src_row += src_stride/sizeof(*src_row);
174   }
175}
176
177void
178util_format_z16_unorm_unpack_z_32unorm(uint32_t *dst_row, unsigned dst_stride,
179                                       const uint8_t *src_row, unsigned src_stride,
180                                       unsigned width, unsigned height)
181{
182   unsigned x, y;
183   for(y = 0; y < height; ++y) {
184      uint32_t *dst = dst_row;
185      const uint16_t *src = (const uint16_t *)src_row;
186      for(x = 0; x < width; ++x) {
187         uint16_t value = util_cpu_to_le16(*src++);
188         *dst++ = z16_unorm_to_z32_unorm(value);
189      }
190      src_row += src_stride/sizeof(*src_row);
191      dst_row += dst_stride/sizeof(*dst_row);
192   }
193}
194
195void
196util_format_z16_unorm_pack_z_32unorm(uint8_t *dst_row, unsigned dst_stride,
197                                     const uint32_t *src_row, unsigned src_stride,
198                                     unsigned width, unsigned height)
199{
200   unsigned x, y;
201   for(y = 0; y < height; ++y) {
202      const uint32_t *src = src_row;
203      uint16_t *dst = (uint16_t *)dst_row;
204      for(x = 0; x < width; ++x) {
205         uint16_t value;
206         value = z32_unorm_to_z16_unorm(*src++);
207         *dst++ = util_le16_to_cpu(value);
208      }
209      dst_row += dst_stride/sizeof(*dst_row);
210      src_row += src_stride/sizeof(*src_row);
211   }
212}
213
214void
215util_format_z32_unorm_unpack_z_float(float *dst_row, unsigned dst_stride,
216                                     const uint8_t *src_row, unsigned src_stride,
217                                     unsigned width, unsigned height)
218{
219   unsigned x, y;
220   for(y = 0; y < height; ++y) {
221      float *dst = dst_row;
222      const uint32_t *src = (const uint32_t *)src_row;
223      for(x = 0; x < width; ++x) {
224         uint32_t value = util_cpu_to_le32(*src++);
225         *dst++ = z32_unorm_to_z32_float(value);
226      }
227      src_row += src_stride/sizeof(*src_row);
228      dst_row += dst_stride/sizeof(*dst_row);
229   }
230}
231
232void
233util_format_z32_unorm_pack_z_float(uint8_t *dst_row, unsigned dst_stride,
234                                   const float *src_row, unsigned src_stride,
235                                   unsigned width, unsigned height)
236{
237   unsigned x, y;
238   for(y = 0; y < height; ++y) {
239      const float *src = src_row;
240      uint32_t *dst = (uint32_t *)dst_row;
241      for(x = 0; x < width; ++x) {
242         uint32_t value;
243         value = z32_float_to_z32_unorm(*src++);
244         *dst++ = util_le32_to_cpu(value);
245      }
246      dst_row += dst_stride/sizeof(*dst_row);
247      src_row += src_stride/sizeof(*src_row);
248   }
249}
250
251void
252util_format_z32_unorm_unpack_z_32unorm(uint32_t *dst_row, unsigned dst_stride,
253                                       const uint8_t *src_row, unsigned src_stride,
254                                       unsigned width, unsigned height)
255{
256   unsigned y;
257   for(y = 0; y < height; ++y) {
258      memcpy(dst_row, src_row, width * 4);
259      src_row += src_stride/sizeof(*src_row);
260      dst_row += dst_stride/sizeof(*dst_row);
261   }
262}
263
264void
265util_format_z32_unorm_pack_z_32unorm(uint8_t *dst_row, unsigned dst_stride,
266                                     const uint32_t *src_row, unsigned src_stride,
267                                     unsigned width, unsigned height)
268{
269   unsigned y;
270   for(y = 0; y < height; ++y) {
271      memcpy(dst_row, src_row, width * 4);
272      src_row += src_stride/sizeof(*src_row);
273      dst_row += dst_stride/sizeof(*dst_row);
274   }
275}
276
277void
278util_format_z32_float_unpack_z_float(float *dst_row, unsigned dst_stride,
279                                     const uint8_t *src_row, unsigned src_stride,
280                                     unsigned width, unsigned height)
281{
282   unsigned y;
283   for(y = 0; y < height; ++y) {
284      memcpy(dst_row, src_row, width * 4);
285      src_row += src_stride/sizeof(*src_row);
286      dst_row += dst_stride/sizeof(*dst_row);
287   }
288}
289
290void
291util_format_z32_float_pack_z_float(uint8_t *dst_row, unsigned dst_stride,
292                                   const float *src_row, unsigned src_stride,
293                                   unsigned width, unsigned height)
294{
295   unsigned y;
296   for(y = 0; y < height; ++y) {
297      memcpy(dst_row, src_row, width * 4);
298      src_row += src_stride/sizeof(*src_row);
299      dst_row += dst_stride/sizeof(*dst_row);
300   }
301}
302
303void
304util_format_z32_float_unpack_z_32unorm(uint32_t *dst_row, unsigned dst_stride,
305                                       const uint8_t *src_row, unsigned src_stride,
306                                       unsigned width, unsigned height)
307{
308   unsigned x, y;
309   for(y = 0; y < height; ++y) {
310      uint32_t *dst = dst_row;
311      const float *src = (const float *)src_row;
312      for(x = 0; x < width; ++x) {
313         *dst++ = z32_float_to_z32_unorm(*src++);
314      }
315      src_row += src_stride/sizeof(*src_row);
316      dst_row += dst_stride/sizeof(*dst_row);
317   }
318}
319
320void
321util_format_z32_float_pack_z_32unorm(uint8_t *dst_row, unsigned dst_stride,
322                                     const uint32_t *src_row, unsigned src_stride,
323                                     unsigned width, unsigned height)
324{
325   unsigned x, y;
326   for(y = 0; y < height; ++y) {
327      const uint32_t *src = src_row;
328      float *dst = (float *)dst_row;
329      for(x = 0; x < width; ++x) {
330         *dst++ = z32_unorm_to_z32_float(*src++);
331      }
332      dst_row += dst_stride/sizeof(*dst_row);
333      src_row += src_stride/sizeof(*src_row);
334   }
335}
336
337void
338util_format_z24_unorm_s8_uint_unpack_z_float(float *dst_row, unsigned dst_stride,
339                                                const uint8_t *src_row, unsigned src_stride,
340                                                unsigned width, unsigned height)
341{
342   unsigned x, y;
343   for(y = 0; y < height; ++y) {
344      float *dst = dst_row;
345      const uint32_t *src = (const uint32_t *)src_row;
346      for(x = 0; x < width; ++x) {
347         uint32_t value =  util_cpu_to_le32(*src++);
348         *dst++ = z24_unorm_to_z32_float(value & 0xffffff);
349      }
350      src_row += src_stride/sizeof(*src_row);
351      dst_row += dst_stride/sizeof(*dst_row);
352   }
353}
354
355void
356util_format_z24_unorm_s8_uint_pack_z_float(uint8_t *dst_row, unsigned dst_stride,
357                                              const float *src_row, unsigned src_stride,
358                                              unsigned width, unsigned height)
359{
360   unsigned x, y;
361   for(y = 0; y < height; ++y) {
362      const float *src = src_row;
363      uint32_t *dst = (uint32_t *)dst_row;
364      for(x = 0; x < width; ++x) {
365         uint32_t value = util_le32_to_cpu(*dst);
366         value &= 0xff000000;
367         value |= z32_float_to_z24_unorm(*src++);
368         *dst++ = util_cpu_to_le32(value);
369      }
370      dst_row += dst_stride/sizeof(*dst_row);
371      src_row += src_stride/sizeof(*src_row);
372   }
373}
374
375void
376util_format_z24_unorm_s8_uint_unpack_z_32unorm(uint32_t *dst_row, unsigned dst_stride,
377                                                  const uint8_t *src_row, unsigned src_stride,
378                                                  unsigned width, unsigned height)
379{
380   unsigned x, y;
381   for(y = 0; y < height; ++y) {
382      uint32_t *dst = dst_row;
383      const uint32_t *src = (const uint32_t *)src_row;
384      for(x = 0; x < width; ++x) {
385         uint32_t value = util_cpu_to_le32(*src++);
386         *dst++ = z24_unorm_to_z32_unorm(value & 0xffffff);
387      }
388      src_row += src_stride/sizeof(*src_row);
389      dst_row += dst_stride/sizeof(*dst_row);
390   }
391}
392
393void
394util_format_z24_unorm_s8_uint_pack_z_32unorm(uint8_t *dst_row, unsigned dst_stride,
395                                                const uint32_t *src_row, unsigned src_stride,
396                                                unsigned width, unsigned height)
397{
398   unsigned x, y;
399   for(y = 0; y < height; ++y) {
400      const uint32_t *src = src_row;
401      uint32_t *dst = (uint32_t *)dst_row;
402      for(x = 0; x < width; ++x) {
403         uint32_t value = util_le32_to_cpu(*dst);
404         value &= 0xff000000;
405         value |= z32_unorm_to_z24_unorm(*src++);
406         *dst++ = util_cpu_to_le32(value);
407      }
408      dst_row += dst_stride/sizeof(*dst_row);
409      src_row += src_stride/sizeof(*src_row);
410   }
411}
412
413void
414util_format_z24_unorm_s8_uint_unpack_s_8uint(uint8_t *dst_row, unsigned dst_stride,
415                                                   const uint8_t *src_row, unsigned src_stride,
416                                                   unsigned width, unsigned height)
417{
418   unsigned x, y;
419   for(y = 0; y < height; ++y) {
420      uint8_t *dst = dst_row;
421      const uint32_t *src = (const uint32_t *)src_row;
422      for(x = 0; x < width; ++x) {
423         uint32_t value = util_cpu_to_le32(*src++);
424         *dst++ = value >> 24;
425      }
426      src_row += src_stride/sizeof(*src_row);
427      dst_row += dst_stride/sizeof(*dst_row);
428   }
429}
430
431void
432util_format_z24_unorm_s8_uint_pack_s_8uint(uint8_t *dst_row, unsigned dst_stride,
433                                                 const uint8_t *src_row, unsigned src_stride,
434                                                 unsigned width, unsigned height)
435{
436   unsigned x, y;
437   for(y = 0; y < height; ++y) {
438      const uint8_t *src = src_row;
439      uint32_t *dst = (uint32_t *)dst_row;
440      for(x = 0; x < width; ++x) {
441         uint32_t value = util_le32_to_cpu(*dst);
442         value &= 0x00ffffff;
443         value |= *src++ << 24;
444         *dst++ = util_cpu_to_le32(value);
445      }
446      dst_row += dst_stride/sizeof(*dst_row);
447      src_row += src_stride/sizeof(*src_row);
448   }
449}
450
451void
452util_format_s8_uint_z24_unorm_unpack_z_float(float *dst_row, unsigned dst_stride,
453                                                const uint8_t *src_row, unsigned src_stride,
454                                                unsigned width, unsigned height)
455{
456   unsigned x, y;
457   for(y = 0; y < height; ++y) {
458      float *dst = dst_row;
459      const uint32_t *src = (const uint32_t *)src_row;
460      for(x = 0; x < width; ++x) {
461         uint32_t value = util_cpu_to_le32(*src++);
462         *dst++ = z24_unorm_to_z32_float(value >> 8);
463      }
464      src_row += src_stride/sizeof(*src_row);
465      dst_row += dst_stride/sizeof(*dst_row);
466   }
467}
468
469void
470util_format_s8_uint_z24_unorm_pack_z_float(uint8_t *dst_row, unsigned dst_stride,
471                                              const float *src_row, unsigned src_stride,
472                                              unsigned width, unsigned height)
473{
474   unsigned x, y;
475   for(y = 0; y < height; ++y) {
476      const float *src = src_row;
477      uint32_t *dst = (uint32_t *)dst_row;
478      for(x = 0; x < width; ++x) {
479         uint32_t value = util_le32_to_cpu(*dst);
480         value &= 0x000000ff;
481         value |= z32_float_to_z24_unorm(*src++) << 8;
482         *dst++ = util_cpu_to_le32(value);
483      }
484      dst_row += dst_stride/sizeof(*dst_row);
485      src_row += src_stride/sizeof(*src_row);
486   }
487}
488
489void
490util_format_s8_uint_z24_unorm_unpack_z_32unorm(uint32_t *dst_row, unsigned dst_stride,
491                                                  const uint8_t *src_row, unsigned src_stride,
492                                                  unsigned width, unsigned height)
493{
494   unsigned x, y;
495   for(y = 0; y < height; ++y) {
496      uint32_t *dst = dst_row;
497      const uint32_t *src = (const uint32_t *)src_row;
498      for(x = 0; x < width; ++x) {
499         uint32_t value = util_cpu_to_le32(*src++);
500         *dst++ = z24_unorm_to_z32_unorm(value >> 8);
501      }
502      src_row += src_stride/sizeof(*src_row);
503      dst_row += dst_stride/sizeof(*dst_row);
504   }
505}
506
507void
508util_format_s8_uint_z24_unorm_pack_z_32unorm(uint8_t *dst_row, unsigned dst_stride,
509                                                const uint32_t *src_row, unsigned src_stride,
510                                                unsigned width, unsigned height)
511{
512   unsigned x, y;
513   for(y = 0; y < height; ++y) {
514      const uint32_t *src = src_row;
515      uint32_t *dst = (uint32_t *)dst_row;
516      for(x = 0; x < width; ++x) {
517         uint32_t value = util_le32_to_cpu(*dst);
518         value &= 0x000000ff;
519         value |= *src++ & 0xffffff00;
520         *dst++ = util_cpu_to_le32(value);
521      }
522      dst_row += dst_stride/sizeof(*dst_row);
523      src_row += src_stride/sizeof(*src_row);
524   }
525}
526
527void
528util_format_s8_uint_z24_unorm_unpack_s_8uint(uint8_t *dst_row, unsigned dst_stride,
529                                                   const uint8_t *src_row, unsigned src_stride,
530                                                   unsigned width, unsigned height)
531{
532   unsigned x, y;
533   for(y = 0; y < height; ++y) {
534      uint8_t *dst = dst_row;
535      const uint32_t *src = (const uint32_t *)src_row;
536      for(x = 0; x < width; ++x) {
537         uint32_t value = util_cpu_to_le32(*src++);
538         *dst++ = value & 0xff;
539      }
540      src_row += src_stride/sizeof(*src_row);
541      dst_row += dst_stride/sizeof(*dst_row);
542   }
543}
544
545void
546util_format_s8_uint_z24_unorm_pack_s_8uint(uint8_t *dst_row, unsigned dst_stride,
547                                                 const uint8_t *src_row, unsigned src_stride,
548                                                 unsigned width, unsigned height)
549{
550   unsigned x, y;
551   for(y = 0; y < height; ++y) {
552      const uint8_t *src = src_row;
553      uint32_t *dst = (uint32_t *)dst_row;
554      for(x = 0; x < width; ++x) {
555         uint32_t value = util_le32_to_cpu(*dst);
556         value &= 0xffffff00;
557         value |= *src++;
558         *dst++ = util_cpu_to_le32(value);
559      }
560      dst_row += dst_stride/sizeof(*dst_row);
561      src_row += src_stride/sizeof(*src_row);
562   }
563}
564
565void
566util_format_z24x8_unorm_unpack_z_float(float *dst_row, unsigned dst_stride,
567                                       const uint8_t *src_row, unsigned src_stride,
568                                       unsigned width, unsigned height)
569{
570   unsigned x, y;
571   for(y = 0; y < height; ++y) {
572      float *dst = dst_row;
573      const uint32_t *src = (const uint32_t *)src_row;
574      for(x = 0; x < width; ++x) {
575         uint32_t value = util_cpu_to_le32(*src++);
576         *dst++ = z24_unorm_to_z32_float(value & 0xffffff);
577      }
578      src_row += src_stride/sizeof(*src_row);
579      dst_row += dst_stride/sizeof(*dst_row);
580   }
581}
582
583void
584util_format_z24x8_unorm_pack_z_float(uint8_t *dst_row, unsigned dst_stride,
585                                     const float *src_row, unsigned src_stride,
586                                     unsigned width, unsigned height)
587{
588   unsigned x, y;
589   for(y = 0; y < height; ++y) {
590      const float *src = src_row;
591      uint32_t *dst = (uint32_t *)dst_row;
592      for(x = 0; x < width; ++x) {
593         uint32_t value;
594         value = z32_float_to_z24_unorm(*src++);
595         *dst++ = util_le32_to_cpu(value);
596      }
597      dst_row += dst_stride/sizeof(*dst_row);
598      src_row += src_stride/sizeof(*src_row);
599   }
600}
601
602void
603util_format_z24x8_unorm_unpack_z_32unorm(uint32_t *dst_row, unsigned dst_stride,
604                                         const uint8_t *src_row, unsigned src_stride,
605                                         unsigned width, unsigned height)
606{
607   unsigned x, y;
608   for(y = 0; y < height; ++y) {
609      uint32_t *dst = dst_row;
610      const uint32_t *src = (const uint32_t *)src_row;
611      for(x = 0; x < width; ++x) {
612         uint32_t value = util_cpu_to_le32(*src++);
613         *dst++ = z24_unorm_to_z32_unorm(value & 0xffffff);
614      }
615      src_row += src_stride/sizeof(*src_row);
616      dst_row += dst_stride/sizeof(*dst_row);
617   }
618}
619
620void
621util_format_z24x8_unorm_pack_z_32unorm(uint8_t *dst_row, unsigned dst_stride,
622                                       const uint32_t *src_row, unsigned src_stride,
623                                       unsigned width, unsigned height)
624{
625   unsigned x, y;
626   for(y = 0; y < height; ++y) {
627      const uint32_t *src = src_row;
628      uint32_t *dst = (uint32_t *)dst_row;
629      for(x = 0; x < width; ++x) {
630         uint32_t value;
631         value = z32_unorm_to_z24_unorm(*src++);
632         *dst++ = util_cpu_to_le32(value);
633      }
634      dst_row += dst_stride/sizeof(*dst_row);
635      src_row += src_stride/sizeof(*src_row);
636   }
637}
638
639void
640util_format_x8z24_unorm_unpack_z_float(float *dst_row, unsigned dst_stride,
641                                       const uint8_t *src_row, unsigned src_stride,
642                                       unsigned width, unsigned height)
643{
644   unsigned x, y;
645   for(y = 0; y < height; ++y) {
646      float *dst = dst_row;
647      const uint32_t *src = (uint32_t *)src_row;
648      for(x = 0; x < width; ++x) {
649         uint32_t value = util_cpu_to_le32(*src++);
650         *dst++ = z24_unorm_to_z32_float(value >> 8);
651      }
652      src_row += src_stride/sizeof(*src_row);
653      dst_row += dst_stride/sizeof(*dst_row);
654   }
655}
656
657void
658util_format_x8z24_unorm_pack_z_float(uint8_t *dst_row, unsigned dst_stride,
659                                     const float *src_row, unsigned src_stride,
660                                     unsigned width, unsigned height)
661{
662   unsigned x, y;
663   for(y = 0; y < height; ++y) {
664      const float *src = src_row;
665      uint32_t *dst = (uint32_t *)dst_row;
666      for(x = 0; x < width; ++x) {
667         uint32_t value;
668         value = z32_float_to_z24_unorm(*src++) << 8;
669         *dst++ = util_cpu_to_le32(value);
670      }
671      dst_row += dst_stride/sizeof(*dst_row);
672      src_row += src_stride/sizeof(*src_row);
673   }
674}
675
676void
677util_format_x8z24_unorm_unpack_z_32unorm(uint32_t *dst_row, unsigned dst_stride,
678                                         const uint8_t *src_row, unsigned src_stride,
679                                         unsigned width, unsigned height)
680{
681   unsigned x, y;
682   for(y = 0; y < height; ++y) {
683      uint32_t *dst = dst_row;
684      const uint32_t *src = (const uint32_t *)src_row;
685      for(x = 0; x < width; ++x) {
686         uint32_t value = util_cpu_to_le32(*src++);
687         *dst++ = z24_unorm_to_z32_unorm(value >> 8);
688      }
689      src_row += src_stride/sizeof(*src_row);
690      dst_row += dst_stride/sizeof(*dst_row);
691   }
692}
693
694void
695util_format_x8z24_unorm_pack_z_32unorm(uint8_t *dst_row, unsigned dst_stride,
696                                       const uint32_t *src_row, unsigned src_stride,
697                                       unsigned width, unsigned height)
698{
699   unsigned x, y;
700   for(y = 0; y < height; ++y) {
701      const uint32_t *src = src_row;
702      uint32_t *dst = (uint32_t *)dst_row;
703      for(x = 0; x < width; ++x) {
704         uint32_t value;
705         value = z32_unorm_to_z24_unorm(*src++) << 8;
706         *dst++ = util_cpu_to_le32(value);
707      }
708      dst_row += dst_stride/sizeof(*dst_row);
709      src_row += src_stride/sizeof(*src_row);
710   }
711}
712
713void
714util_format_z32_float_s8x24_uint_unpack_z_float(float *dst_row, unsigned dst_stride,
715                                                   const uint8_t *src_row, unsigned src_stride,
716                                                   unsigned width, unsigned height)
717{
718   unsigned x, y;
719   for(y = 0; y < height; ++y) {
720      float *dst = dst_row;
721      const float *src = (const float *)src_row;
722      for(x = 0; x < width; ++x) {
723         *dst = *src;
724         src += 2;
725         dst += 1;
726      }
727      src_row += src_stride/sizeof(*src_row);
728      dst_row += dst_stride/sizeof(*dst_row);
729   }
730}
731
732void
733util_format_z32_float_s8x24_uint_pack_z_float(uint8_t *dst_row, unsigned dst_stride,
734                                                 const float *src_row, unsigned src_stride,
735                                                 unsigned width, unsigned height)
736{
737   unsigned x, y;
738   for(y = 0; y < height; ++y) {
739      const float *src = src_row;
740      float *dst = (float *)dst_row;
741      for(x = 0; x < width; ++x) {
742         *dst = *src;
743         src += 1;
744         dst += 2;
745      }
746      dst_row += dst_stride/sizeof(*dst_row);
747      src_row += src_stride/sizeof(*src_row);
748   }
749}
750
751void
752util_format_z32_float_s8x24_uint_unpack_z_32unorm(uint32_t *dst_row, unsigned dst_stride,
753                                                     const uint8_t *src_row, unsigned src_stride,
754                                                     unsigned width, unsigned height)
755{
756   unsigned x, y;
757   for(y = 0; y < height; ++y) {
758      uint32_t *dst = dst_row;
759      const float *src = (const float *)src_row;
760      for(x = 0; x < width; ++x) {
761         *dst = z32_float_to_z32_unorm(*src);
762         src += 2;
763         dst += 1;
764      }
765      src_row += src_stride/sizeof(*src_row);
766      dst_row += dst_stride/sizeof(*dst_row);
767   }
768}
769
770void
771util_format_z32_float_s8x24_uint_pack_z_32unorm(uint8_t *dst_row, unsigned dst_stride,
772                                                   const uint32_t *src_row, unsigned src_stride,
773                                                   unsigned width, unsigned height)
774{
775   unsigned x, y;
776   for(y = 0; y < height; ++y) {
777      const uint32_t *src = src_row;
778      float *dst = (float *)dst_row;
779      for(x = 0; x < width; ++x) {
780         *dst++ = z32_unorm_to_z32_float(*src++);
781      }
782      dst_row += dst_stride/sizeof(*dst_row);
783      src_row += src_stride/sizeof(*src_row);
784   }
785}
786
787void
788util_format_z32_float_s8x24_uint_unpack_s_8uint(uint8_t *dst_row, unsigned dst_stride,
789                                                      const uint8_t *src_row, unsigned src_stride,
790                                                      unsigned width, unsigned height)
791{
792   unsigned x, y;
793   for(y = 0; y < height; ++y) {
794      uint8_t *dst = dst_row;
795      const uint8_t *src = src_row + 4;
796      for(x = 0; x < width; ++x) {
797         *dst = *src;
798         src += 8;
799         dst += 1;
800      }
801      src_row += src_stride/sizeof(*src_row);
802      dst_row += dst_stride/sizeof(*dst_row);
803   }
804}
805
806void
807util_format_z32_float_s8x24_uint_pack_s_8uint(uint8_t *dst_row, unsigned dst_stride,
808                                                    const uint8_t *src_row, unsigned src_stride,
809                                                    unsigned width, unsigned height)
810{
811   unsigned x, y;
812   for(y = 0; y < height; ++y) {
813      const uint8_t *src = src_row;
814      uint8_t *dst = dst_row + 4;
815      for(x = 0; x < width; ++x) {
816         *dst = *src;
817         src += 1;
818         dst += 8;
819      }
820      dst_row += dst_stride/sizeof(*dst_row);
821      src_row += src_stride/sizeof(*src_row);
822   }
823}
824
825
826void
827util_format_x24s8_uint_unpack_s_8uint(uint8_t *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
828{
829   util_format_z24_unorm_s8_uint_unpack_s_8uint(dst_row, dst_stride,
830						      src_row, src_stride,
831						      width, height);
832}
833
834void
835util_format_x24s8_uint_pack_s_8uint(uint8_t *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
836{
837   util_format_z24_unorm_s8_uint_pack_s_8uint(dst_row, dst_stride,
838						    src_row, src_stride,
839						    width, height);
840}
841
842void
843util_format_s8x24_uint_unpack_s_8uint(uint8_t *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
844{
845   util_format_s8_uint_z24_unorm_unpack_s_8uint(dst_row, dst_stride,
846						      src_row, src_stride,
847						      width, height);
848}
849
850void
851util_format_s8x24_uint_pack_s_8uint(uint8_t *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
852{
853   util_format_s8_uint_z24_unorm_pack_s_8uint(dst_row, dst_stride,
854						      src_row, src_stride,
855						      width, height);
856}
857
858void
859util_format_x32_s8x24_uint_unpack_s_8uint(uint8_t *dst_row, unsigned dst_stride,
860						const uint8_t *src_row, unsigned src_stride,
861						unsigned width, unsigned height)
862{
863   util_format_z32_float_s8x24_uint_unpack_s_8uint(dst_row, dst_stride,
864							 src_row, src_stride,
865							 width, height);
866
867}
868
869void
870util_format_x32_s8x24_uint_pack_s_8uint(uint8_t *dst_row, unsigned dst_stride,
871					      const uint8_t *src_row, unsigned src_stride,
872					      unsigned width, unsigned height)
873{
874   util_format_z32_float_s8x24_uint_pack_s_8uint(dst_row, dst_stride,
875                                                       src_row, src_stride,
876						       width, height);
877}
878