summaryrefslogtreecommitdiff
path: root/src/gallium/auxiliary/util/u_pack_color.h
blob: 7de62d32281520ba48cdc2b51afbac3169f82cc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/**************************************************************************
 *
 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/

/**
 * @file
 * Functions to produce packed colors/Z from floats.
 */


#ifndef U_PACK_COLOR_H
#define U_PACK_COLOR_H


#include "pipe/p_compiler.h"
#include "pipe/p_format.h"
#include "util/u_format.h"
#include "util/u_math.h"



union util_color {
   ubyte ub;
   ushort us;
   uint ui;
   float f[4];
};

/**
 * Pack ubyte R,G,B,A into dest pixel.
 */
static INLINE void
util_pack_color_ub(ubyte r, ubyte g, ubyte b, ubyte a,
                   enum pipe_format format, union util_color *uc)
{
   switch (format) {
   case PIPE_FORMAT_R8G8B8A8_UNORM:
      {
         uc->ui = (r << 24) | (g << 16) | (b << 8) | a;
      }
      return;
   case PIPE_FORMAT_R8G8B8X8_UNORM:
      {
         uc->ui = (r << 24) | (g << 16) | (b << 8) | 0xff;
      }
      return;
   case PIPE_FORMAT_A8R8G8B8_UNORM:
      {
         uc->ui = (a << 24) | (r << 16) | (g << 8) | b;
      }
      return;
   case PIPE_FORMAT_X8R8G8B8_UNORM:
      {
         uc->ui = (0xff << 24) | (r << 16) | (g << 8) | b;
      }
      return;
   case PIPE_FORMAT_B8G8R8A8_UNORM:
      {
         uc->ui = (b << 24) | (g << 16) | (r << 8) | a;
      }
      return;
   case PIPE_FORMAT_B8G8R8X8_UNORM:
      {
         uc->ui = (b << 24) | (g << 16) | (r << 8) | 0xff;
      }
      return;
   case PIPE_FORMAT_R5G6B5_UNORM:
      {
         uc->us = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | (b >> 3);
      }
      return;
   case PIPE_FORMAT_A1R5G5B5_UNORM:
      {
         uc->us = ((a & 0x80) << 8) | ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | (b >> 3);
      }
      return;
   case PIPE_FORMAT_A4R4G4B4_UNORM:
      {
         uc->us = ((a & 0xf0) << 8) | ((r & 0xf0) << 4) | ((g & 0xf0) << 0) | (b >> 4);
      }
      return;
   case PIPE_FORMAT_A8_UNORM:
      {
         uc->ub = a;
      }
      return;
   case PIPE_FORMAT_L8_UNORM:
   case PIPE_FORMAT_I8_UNORM:
      {
         uc->ub = a;
      }
      return;
   case PIPE_FORMAT_R32G32B32A32_FLOAT:
      {
         uc->f[0] = (float)r / 255.0f;
         uc->f[1] = (float)g / 255.0f;
         uc->f[2] = (float)b / 255.0f;
         uc->f[3] = (float)a / 255.0f;
      }
      return;
   case PIPE_FORMAT_R32G32B32_FLOAT:
      {
         uc->f[0] = (float)r / 255.0f;
         uc->f[1] = (float)g / 255.0f;
         uc->f[2] = (float)b / 255.0f;
      }
      return;

   /* XXX lots more cases to add */
   default:
      uc->ui = 0; /* keep compiler happy */
      debug_print_format("gallium: unhandled format in util_pack_color_ub()", format);
      assert(0);
   }
}
 

/**
 * Unpack RGBA from a packed pixel, returning values as ubytes in [0,255].
 */
static INLINE void
util_unpack_color_ub(enum pipe_format format, union util_color *uc,
                     ubyte *r, ubyte *g, ubyte *b, ubyte *a)
{
   switch (format) {
   case PIPE_FORMAT_R8G8B8A8_UNORM:
      {
         uint p = uc->ui;
         *r = (ubyte) ((p >> 24) & 0xff);
         *g = (ubyte) ((p >> 16) & 0xff);
         *b = (ubyte) ((p >>  8) & 0xff);
         *a = (ubyte) ((p >>  0) & 0xff);
      }
      return;
   case PIPE_FORMAT_R8G8B8X8_UNORM:
      {
         uint p = uc->ui;
         *r = (ubyte) ((p >> 24) & 0xff);
         *g = (ubyte) ((p >> 16) & 0xff);
         *b = (ubyte) ((p >>  8) & 0xff);
         *a = (ubyte) 0xff;
      }
      return;
   case PIPE_FORMAT_A8R8G8B8_UNORM:
      {
         uint p = uc->ui;
         *r = (ubyte) ((p >> 16) & 0xff);
         *g = (ubyte) ((p >>  8) & 0xff);
         *b = (ubyte) ((p >>  0) & 0xff);
         *a = (ubyte) ((p >> 24) & 0xff);
      }
      return;
   case PIPE_FORMAT_X8R8G8B8_UNORM:
      {
         uint p = uc->ui;
         *r = (ubyte) ((p >> 16) & 0xff);
         *g = (ubyte) ((p >>  8) & 0xff);
         *b = (ubyte) ((p >>  0) & 0xff);
         *a = (ubyte) 0xff;
      }
      return;
   case PIPE_FORMAT_B8G8R8A8_UNORM:
      {
         uint p = uc->ui;
         *r = (ubyte) ((p >>  8) & 0xff);
         *g = (ubyte) ((p >> 16) & 0xff);
         *b = (ubyte) ((p >> 24) & 0xff);
         *a = (ubyte) ((p >>  0) & 0xff);
      }
      return;
   case PIPE_FORMAT_B8G8R8X8_UNORM:
      {
         uint p = uc->ui;
         *r = (ubyte) ((p >>  8) & 0xff);
         *g = (ubyte) ((p >> 16) & 0xff);
         *b = (ubyte) ((p >> 24) & 0xff);
         *a = (ubyte) 0xff;
      }
      return;
   case PIPE_FORMAT_R5G6B5_UNORM:
      {
         ushort p = uc->us;
         *r = (ubyte) (((p >> 8) & 0xf8) | ((p >> 13) & 0x7));
         *g = (ubyte) (((p >> 3) & 0xfc) | ((p >>  9) & 0x3));
         *b = (ubyte) (((p << 3) & 0xf8) | ((p >>  2) & 0x7));
         *a = (ubyte) 0xff;
      }
      return;
   case PIPE_FORMAT_A1R5G5B5_UNORM:
      {
         ushort p = uc->us;
         *r = (ubyte) (((p >>  7) & 0xf8) | ((p >> 12) & 0x7));
         *g = (ubyte) (((p >>  2) & 0xf8) | ((p >>  7) & 0x7));
         *b = (ubyte) (((p <<  3) & 0xf8) | ((p >>  2) & 0x7));
         *a = (ubyte) (0xff * (p >> 15));
      }
      return;
   case PIPE_FORMAT_A4R4G4B4_UNORM:
      {
         ushort p = uc->us;
         *r = (ubyte) (((p >> 4) & 0xf0) | ((p >>  8) & 0xf));
         *g = (ubyte) (((p >> 0) & 0xf0) | ((p >>  4) & 0xf));
         *b = (ubyte) (((p << 4) & 0xf0) | ((p >>  0) & 0xf));
         *a = (ubyte) (((p >> 8) & 0xf0) | ((p >> 12) & 0xf));
      }
      return;
   case PIPE_FORMAT_A8_UNORM:
      {
         ubyte p = uc->ub;
         *r = *g = *b = (ubyte) 0xff;
         *a = p;
      }
      return;
   case PIPE_FORMAT_L8_UNORM:
      {
         ubyte p = uc->ub;
         *r = *g = *b = p;
         *a = (ubyte) 0xff;
      }
      return;
   case PIPE_FORMAT_I8_UNORM:
      {
         ubyte p = uc->ub;
         *r = *g = *b = *a = p;
      }
      return;
   case PIPE_FORMAT_R32G32B32A32_FLOAT:
      {
         const float *p = &uc->f[0];
         *r = float_to_ubyte(p[0]);
         *g = float_to_ubyte(p[1]);
         *b = float_to_ubyte(p[2]);
         *a = float_to_ubyte(p[3]);
      }
      return;
   case PIPE_FORMAT_R32G32B32_FLOAT:
      {
         const float *p = &uc->f[0];
         *r = float_to_ubyte(p[0]);
         *g = float_to_ubyte(p[1]);
         *b = float_to_ubyte(p[2]);
         *a = (ubyte) 0xff;
      }
      return;

   case PIPE_FORMAT_R32G32_FLOAT:
      {
         const float *p = &uc->f[0];
         *r = float_to_ubyte(p[0]);
         *g = float_to_ubyte(p[1]);
         *b = *a = (ubyte) 0xff;
      }
      return;

   case PIPE_FORMAT_R32_FLOAT:
      {
         const float *p = &uc->f[0];
         *r = float_to_ubyte(p[0]);
         *g = *b = *a = (ubyte) 0xff;
      }
      return;

   /* XXX lots more cases to add */
   default:
      debug_print_format("gallium: unhandled format in util_unpack_color_ub()",
                         format);
      assert(0);
   }
}


/**
 * Note rgba outside [0,1] will be clamped for int pixel formats.
 */
static INLINE void
util_pack_color(const float rgba[4], enum pipe_format format, union util_color *uc)
{
   ubyte r = 0;
   ubyte g = 0;
   ubyte b = 0;
   ubyte a = 0;

   if (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_RGB, 0) <= 8) {
      /* format uses 8-bit components or less */
      r = float_to_ubyte(rgba[0]);
      g = float_to_ubyte(rgba[1]);
      b = float_to_ubyte(rgba[2]);
      a = float_to_ubyte(rgba[3]);
   }

   switch (format) {
   case PIPE_FORMAT_R8G8B8A8_UNORM:
      {
         uc->ui = (r << 24) | (g << 16) | (b << 8) | a;
      }
      return;
   case PIPE_FORMAT_R8G8B8X8_UNORM:
      {
         uc->ui = (r << 24) | (g << 16) | (b << 8) | 0xff;
      }
      return;
   case PIPE_FORMAT_A8R8G8B8_UNORM:
      {
         uc->ui = (a << 24) | (r << 16) | (g << 8) | b;
      }
      return;
   case PIPE_FORMAT_X8R8G8B8_UNORM:
      {
         uc->ui = (0xff << 24) | (r << 16) | (g << 8) | b;
      }
      return;
   case PIPE_FORMAT_B8G8R8A8_UNORM:
      {
         uc->ui = (b << 24) | (g << 16) | (r << 8) | a;
      }
      return;
   case PIPE_FORMAT_B8G8R8X8_UNORM:
      {
         uc->ui = (b << 24) | (g << 16) | (r << 8) | 0xff;
      }
      return;
   case PIPE_FORMAT_R5G6B5_UNORM:
      {
         uc->us = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | (b >> 3);
      }
      return;
   case PIPE_FORMAT_A1R5G5B5_UNORM:
      {
         uc->us = ((a & 0x80) << 8) | ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | (b >> 3);
      }
      return;
   case PIPE_FORMAT_A4R4G4B4_UNORM:
      {
         uc->ub = ((a & 0xf0) << 8) | ((r & 0xf0) << 4) | ((g & 0xf0) << 0) | (b >> 4);
      }
      return;
   case PIPE_FORMAT_A8_UNORM:
      {
         uc->ub = a;
      }
      return;
   case PIPE_FORMAT_L8_UNORM:
   case PIPE_FORMAT_I8_UNORM:
      {
         uc->ub = r;
      }
      return;
   case PIPE_FORMAT_R32G32B32A32_FLOAT:
      {
         uc->f[0] = rgba[0];
         uc->f[1] = rgba[1];
         uc->f[2] = rgba[2];
         uc->f[3] = rgba[3];
      }
      return;
   case PIPE_FORMAT_R32G32B32_FLOAT:
      {
         uc->f[0] = rgba[0];
         uc->f[1] = rgba[1];
         uc->f[2] = rgba[2];
      }
      return;
   /* XXX lots more cases to add */
   default:
      uc->ui = 0; /* keep compiler happy */
      debug_print_format("gallium: unhandled format in util_pack_color()", format);
      assert(0);
   }
}
 

/**
 * Note: it's assumed that z is in [0,1]
 */
static INLINE uint
util_pack_z(enum pipe_format format, double z)
{
   if (z == 0.0)
      return 0;

   switch (format) {
   case PIPE_FORMAT_Z16_UNORM:
      if (z == 1.0)
         return 0xffff;
      return (uint) (z * 0xffff);
   case PIPE_FORMAT_Z32_UNORM:
      /* special-case to avoid overflow */
      if (z == 1.0)
         return 0xffffffff;
      return (uint) (z * 0xffffffff);
   case PIPE_FORMAT_S8Z24_UNORM:
   case PIPE_FORMAT_X8Z24_UNORM:
      if (z == 1.0)
         return 0xffffff;
      return (uint) (z * 0xffffff);
   case PIPE_FORMAT_Z24S8_UNORM:
   case PIPE_FORMAT_Z24X8_UNORM:
      if (z == 1.0)
         return 0xffffff00;
      return ((uint) (z * 0xffffff)) << 8;
   case PIPE_FORMAT_S8_UNORM:
      /* this case can get it via util_pack_z_stencil() */
      return 0;
   default:
      debug_print_format("gallium: unhandled format in util_pack_z()", format);
      assert(0);
      return 0;
   }
}
 

/**
 * Pack Z and/or stencil values into a 32-bit value described by format.
 * Note: it's assumed that z is in [0,1] and s in [0,255]
 */
static INLINE uint
util_pack_z_stencil(enum pipe_format format, double z, uint s)
{
   unsigned packed = util_pack_z(format, z);

   switch (format) {
   case PIPE_FORMAT_S8Z24_UNORM:
      packed |= s << 24;
      break;
   case PIPE_FORMAT_Z24S8_UNORM:
      packed |= s;
      break;
   case PIPE_FORMAT_S8_UNORM:
      packed |= s;
      break;
   default:
      break;
   }

   return packed;
}


/**
 * Pack 4 ubytes into a 4-byte word
 */
static INLINE unsigned
pack_ub4(ubyte b0, ubyte b1, ubyte b2, ubyte b3)
{
   return ((((unsigned int)b0) << 0) |
	   (((unsigned int)b1) << 8) |
	   (((unsigned int)b2) << 16) |
	   (((unsigned int)b3) << 24));
}


/**
 * Pack/convert 4 floats into one 4-byte word.
 */
static INLINE unsigned
pack_ui32_float4(float a, float b, float c, float d)
{
   return pack_ub4( float_to_ubyte(a),
		    float_to_ubyte(b),
		    float_to_ubyte(c),
		    float_to_ubyte(d) );
}



#endif /* U_PACK_COLOR_H */