formats.c 64 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
 * Mesa 3-D graphics library
 *
 * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
 * Copyright (c) 2008-2009  VMware, Inc.
 *
 * 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, sublicense,
 * 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 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 NONINFRINGEMENT.  IN NO EVENT SHALL
20
21
22
23
 * THE AUTHORS OR COPYRIGHT HOLDERS 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.
24
25
26
 */


27
#include "imports.h"
28
#include "formats.h"
29
#include "macros.h"
30
#include "glformats.h"
31
32
#include "c11/threads.h"
#include "util/hash_table.h"
33

34
35
36
37
38
/**
 * Information about texture formats.
 */
struct gl_format_info
{
39
   mesa_format Name;
40

41
42
43
   /** text name for debugging */
   const char *StrName;

44
45
   enum mesa_format_layout Layout;

46
   /**
47
48
    * Base format is one of GL_RED, GL_RG, GL_RGB, GL_RGBA, GL_ALPHA,
    * GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_INTENSITY, GL_YCBCR_MESA,
49
    * GL_DEPTH_COMPONENT, GL_STENCIL_INDEX, GL_DEPTH_STENCIL.
50
51
52
53
    */
   GLenum BaseFormat;

   /**
54
    * Logical data type: one of  GL_UNSIGNED_NORMALIZED, GL_SIGNED_NORMALIZED,
55
    * GL_UNSIGNED_INT, GL_INT, GL_FLOAT.
56
57
58
59
60
61
62
63
64
65
66
67
    */
   GLenum DataType;

   GLubyte RedBits;
   GLubyte GreenBits;
   GLubyte BlueBits;
   GLubyte AlphaBits;
   GLubyte LuminanceBits;
   GLubyte IntensityBits;
   GLubyte DepthBits;
   GLubyte StencilBits;

68
69
   bool IsSRGBFormat;

70
   /**
71
    * To describe compressed formats.  If not compressed, Width=Height=Depth=1.
72
    */
73
   GLubyte BlockWidth, BlockHeight, BlockDepth;
74
   GLubyte BytesPerBlock;
75
76

   uint8_t Swizzle[4];
77
   mesa_array_format ArrayFormat;
78
79
};

80
#include "format_info.h"
81
82

static const struct gl_format_info *
83
_mesa_get_format_info(mesa_format format)
84
85
{
   const struct gl_format_info *info = &format_info[format];
86
   STATIC_ASSERT(ARRAY_SIZE(format_info) == MESA_FORMAT_COUNT);
87
88
89
90
91
   assert(info->Name == format);
   return info;
}


92
93
/** Return string name of format (for debugging) */
const char *
94
_mesa_get_format_name(mesa_format format)
95
96
97
98
99
100
101
{
   const struct gl_format_info *info = _mesa_get_format_info(format);
   return info->StrName;
}



102
103
104
105
/**
 * Return bytes needed to store a block of pixels in the given format.
 * Normally, a block is 1x1 (a single pixel).  But for compressed formats
 * a block may be 4x4 or 8x4, etc.
106
107
 *
 * Note: not GLuint, so as not to coerce math to unsigned. cf. fdo #37351
108
 */
109
GLint
110
_mesa_get_format_bytes(mesa_format format)
111
112
{
   const struct gl_format_info *info = _mesa_get_format_info(format);
113
114
   assert(info->BytesPerBlock);
   assert(info->BytesPerBlock <= MAX_PIXEL_BYTES ||
Brian Paul's avatar
Brian Paul committed
115
          _mesa_is_format_compressed(format));
116
117
118
119
   return info->BytesPerBlock;
}


120
121
122
123
124
/**
 * Return bits per component for the given format.
 * \param format  one of MESA_FORMAT_x
 * \param pname  the component, such as GL_RED_BITS, GL_TEXTURE_BLUE_BITS, etc.
 */
Brian Paul's avatar
Brian Paul committed
125
GLint
126
_mesa_get_format_bits(mesa_format format, GLenum pname)
Brian Paul's avatar
Brian Paul committed
127
128
129
130
{
   const struct gl_format_info *info = _mesa_get_format_info(format);

   switch (pname) {
131
   case GL_RED_BITS:
Brian Paul's avatar
Brian Paul committed
132
   case GL_TEXTURE_RED_SIZE:
133
134
   case GL_RENDERBUFFER_RED_SIZE_EXT:
   case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
135
   case GL_INTERNALFORMAT_RED_SIZE:
Brian Paul's avatar
Brian Paul committed
136
      return info->RedBits;
137
   case GL_GREEN_BITS:
Brian Paul's avatar
Brian Paul committed
138
   case GL_TEXTURE_GREEN_SIZE:
139
140
   case GL_RENDERBUFFER_GREEN_SIZE_EXT:
   case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
141
   case GL_INTERNALFORMAT_GREEN_SIZE:
Brian Paul's avatar
Brian Paul committed
142
      return info->GreenBits;
143
   case GL_BLUE_BITS:
Brian Paul's avatar
Brian Paul committed
144
   case GL_TEXTURE_BLUE_SIZE:
145
146
   case GL_RENDERBUFFER_BLUE_SIZE_EXT:
   case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
147
   case GL_INTERNALFORMAT_BLUE_SIZE:
Brian Paul's avatar
Brian Paul committed
148
      return info->BlueBits;
149
   case GL_ALPHA_BITS:
Brian Paul's avatar
Brian Paul committed
150
   case GL_TEXTURE_ALPHA_SIZE:
151
152
   case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
   case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
153
   case GL_INTERNALFORMAT_ALPHA_SIZE:
Brian Paul's avatar
Brian Paul committed
154
155
156
157
158
      return info->AlphaBits;
   case GL_TEXTURE_INTENSITY_SIZE:
      return info->IntensityBits;
   case GL_TEXTURE_LUMINANCE_SIZE:
      return info->LuminanceBits;
159
   case GL_INDEX_BITS:
160
      return 0;
161
   case GL_DEPTH_BITS:
Brian Paul's avatar
Brian Paul committed
162
   case GL_TEXTURE_DEPTH_SIZE_ARB:
163
164
   case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
   case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
165
   case GL_INTERNALFORMAT_DEPTH_SIZE:
Brian Paul's avatar
Brian Paul committed
166
      return info->DepthBits;
167
   case GL_STENCIL_BITS:
Brian Paul's avatar
Brian Paul committed
168
   case GL_TEXTURE_STENCIL_SIZE_EXT:
169
170
   case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
   case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
171
   case GL_INTERNALFORMAT_STENCIL_SIZE:
Brian Paul's avatar
Brian Paul committed
172
173
174
175
176
177
178
179
      return info->StencilBits;
   default:
      _mesa_problem(NULL, "bad pname in _mesa_get_format_bits()");
      return 0;
   }
}


180
GLuint
181
_mesa_get_format_max_bits(mesa_format format)
182
183
184
185
186
187
188
189
190
191
192
193
194
{
   const struct gl_format_info *info = _mesa_get_format_info(format);
   GLuint max = MAX2(info->RedBits, info->GreenBits);
   max = MAX2(max, info->BlueBits);
   max = MAX2(max, info->AlphaBits);
   max = MAX2(max, info->LuminanceBits);
   max = MAX2(max, info->IntensityBits);
   max = MAX2(max, info->DepthBits);
   max = MAX2(max, info->StencilBits);
   return max;
}


195
196
197
198
199
200
201
202
203
204
205
/**
 * Return the layout type of the given format.
 */
extern enum mesa_format_layout
_mesa_get_format_layout(mesa_format format)
{
   const struct gl_format_info *info = _mesa_get_format_info(format);
   return info->Layout;
}


206
207
208
209
210
211
212
/**
 * Return the data type (or more specifically, the data representation)
 * for the given format.
 * The return value will be one of:
 *    GL_UNSIGNED_NORMALIZED = unsigned int representing [0,1]
 *    GL_SIGNED_NORMALIZED = signed int representing [-1, 1]
 *    GL_UNSIGNED_INT = an ordinary unsigned integer
213
 *    GL_INT = an ordinary signed integer
214
215
 *    GL_FLOAT = an ordinary float
 */
Brian Paul's avatar
Brian Paul committed
216
GLenum
217
_mesa_get_format_datatype(mesa_format format)
Brian Paul's avatar
Brian Paul committed
218
219
220
221
222
{
   const struct gl_format_info *info = _mesa_get_format_info(format);
   return info->DataType;
}

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
static GLenum
get_base_format_for_array_format(mesa_array_format format)
{
   uint8_t swizzle[4];
   int num_channels;

   _mesa_array_format_get_swizzle(format, swizzle);
   num_channels = _mesa_array_format_get_num_channels(format);

   switch (num_channels) {
   case 4:
      /* FIXME: RGBX formats have 4 channels, but their base format is GL_RGB.
       * This is not really a problem for now because we only create array
       * formats from GL format/type combinations, and these cannot specify
       * RGBX formats.
       */
      return GL_RGBA;
   case 3:
      return GL_RGB;
   case 2:
      if (swizzle[0] == 0 &&
          swizzle[1] == 0 &&
          swizzle[2] == 0 &&
          swizzle[3] == 1)
         return GL_LUMINANCE_ALPHA;
      if (swizzle[0] == 1 &&
          swizzle[1] == 1 &&
          swizzle[2] == 1 &&
          swizzle[3] == 0)
         return GL_LUMINANCE_ALPHA;
      if (swizzle[0] == 0 &&
          swizzle[1] == 1 &&
          swizzle[2] == 4 &&
          swizzle[3] == 5)
         return GL_RG;
      if (swizzle[0] == 1 &&
          swizzle[1] == 0 &&
          swizzle[2] == 4 &&
          swizzle[3] == 5)
         return GL_RG;
      break;
   case 1:
      if (swizzle[0] == 0 &&
          swizzle[1] == 0 &&
          swizzle[2] == 0 &&
          swizzle[3] == 5)
         return GL_LUMINANCE;
      if (swizzle[0] == 0 &&
          swizzle[1] == 0 &&
          swizzle[2] == 0 &&
          swizzle[3] == 0)
         return GL_INTENSITY;
      if (swizzle[0] <= MESA_FORMAT_SWIZZLE_W)
         return GL_RED;
      if (swizzle[1] <= MESA_FORMAT_SWIZZLE_W)
         return GL_GREEN;
      if (swizzle[2] <= MESA_FORMAT_SWIZZLE_W)
         return GL_BLUE;
      if (swizzle[3] <= MESA_FORMAT_SWIZZLE_W)
         return GL_ALPHA;
      break;
   }

   unreachable("Unsupported format");
}
Brian Paul's avatar
Brian Paul committed
288

289
/**
290
291
292
 * Return the basic format for the given type.  The result will be one of
 * GL_RGB, GL_RGBA, GL_ALPHA, GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_INTENSITY,
 * GL_YCBCR_MESA, GL_DEPTH_COMPONENT, GL_STENCIL_INDEX, GL_DEPTH_STENCIL.
293
 * This functions accepts a mesa_format or a mesa_array_format.
294
 */
295
GLenum
296
_mesa_get_format_base_format(uint32_t format)
297
{
298
299
300
301
302
303
   if (!_mesa_format_is_mesa_array_format(format)) {
      const struct gl_format_info *info = _mesa_get_format_info(format);
      return info->BaseFormat;
   } else {
      return get_base_format_for_array_format(format);
   }
304
305
306
}


307
308
309
310
311
312
313
314
/**
 * Return the block size (in pixels) for the given format.  Normally
 * the block size is 1x1.  But compressed formats will have block sizes
 * of 4x4 or 8x4 pixels, etc.
 * \param bw  returns block width in pixels
 * \param bh  returns block height in pixels
 */
void
315
_mesa_get_format_block_size(mesa_format format, GLuint *bw, GLuint *bh)
316
317
{
   const struct gl_format_info *info = _mesa_get_format_info(format);
318
319
320
   /* Use _mesa_get_format_block_size_3d() for 3D blocks. */
   assert(info->BlockDepth == 1);

321
322
323
324
325
   *bw = info->BlockWidth;
   *bh = info->BlockHeight;
}


326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/**
 * Return the block size (in pixels) for the given format. Normally
 * the block size is 1x1x1. But compressed formats will have block
 * sizes of 4x4x4, 3x3x3 pixels, etc.
 * \param bw  returns block width in pixels
 * \param bh  returns block height in pixels
 * \param bd  returns block depth in pixels
 */
void
_mesa_get_format_block_size_3d(mesa_format format,
                               GLuint *bw,
                               GLuint *bh,
                               GLuint *bd)
{
   const struct gl_format_info *info = _mesa_get_format_info(format);
   *bw = info->BlockWidth;
   *bh = info->BlockHeight;
   *bd = info->BlockDepth;
}


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
/**
 * Returns the an array of four numbers representing the transformation
 * from the RGBA or SZ colorspace to the given format.  For array formats,
 * the i'th RGBA component is given by:
 *
 * if (swizzle[i] <= MESA_FORMAT_SWIZZLE_W)
 *    comp = data[swizzle[i]];
 * else if (swizzle[i] == MESA_FORMAT_SWIZZLE_ZERO)
 *    comp = 0;
 * else if (swizzle[i] == MESA_FORMAT_SWIZZLE_ONE)
 *    comp = 1;
 * else if (swizzle[i] == MESA_FORMAT_SWIZZLE_NONE)
 *    // data does not contain a channel of this format
 *
 * For packed formats, the swizzle gives the number of components left of
 * the least significant bit.
 *
 * Compressed formats have no swizzle.
 */
void
_mesa_get_format_swizzle(mesa_format format, uint8_t swizzle_out[4])
{
   const struct gl_format_info *info = _mesa_get_format_info(format);
   memcpy(swizzle_out, info->Swizzle, sizeof(info->Swizzle));
}

373
374
375
376
377
378
379
380
381
382
383
384
385
mesa_array_format
_mesa_array_format_flip_channels(mesa_array_format format)
{
   int num_channels;
   uint8_t swizzle[4];

   num_channels = _mesa_array_format_get_num_channels(format);
   _mesa_array_format_get_swizzle(format, swizzle);

   if (num_channels == 1)
      return format;

   if (num_channels == 2) {
386
387
388
389
390
391
392
393
      /* Assert that the swizzle makes sense for 2 channels */
      for (unsigned i = 0; i < 4; i++)
         assert(swizzle[i] != 2 && swizzle[i] != 3);

      static const uint8_t flip_xy[6] = { 1, 0, 2, 3, 4, 5 };
      _mesa_array_format_set_swizzle(&format,
                                     flip_xy[swizzle[0]], flip_xy[swizzle[1]],
                                     flip_xy[swizzle[2]], flip_xy[swizzle[3]]);
394
395
396
397
      return format;
   }

   if (num_channels == 4) {
398
399
400
401
      static const uint8_t flip[6] = { 3, 2, 1, 0, 4, 5 };
      _mesa_array_format_set_swizzle(&format,
                                     flip[swizzle[0]], flip[swizzle[1]],
                                     flip[swizzle[2]], flip[swizzle[3]]);
402
403
404
405
406
407
408
409
410
411
      return format;
   }

   unreachable("Invalid array format");
}

uint32_t
_mesa_format_to_array_format(mesa_format format)
{
   const struct gl_format_info *info = _mesa_get_format_info(format);
412
413
   if (info->ArrayFormat && !_mesa_little_endian() &&
       info->Layout == MESA_FORMAT_LAYOUT_PACKED)
414
      return _mesa_array_format_flip_channels(info->ArrayFormat);
415
416
   else
      return info->ArrayFormat;
417
418
}

419
420
421
422
423
424
425
426
427
428
static struct hash_table *format_array_format_table;
static once_flag format_array_format_table_exists = ONCE_FLAG_INIT;

static bool
array_formats_equal(const void *a, const void *b)
{
   return (intptr_t)a == (intptr_t)b;
}

static void
429
format_array_format_table_init(void)
430
431
432
433
434
435
436
437
{
   const struct gl_format_info *info;
   mesa_array_format array_format;
   unsigned f;

   format_array_format_table = _mesa_hash_table_create(NULL, NULL,
                                                       array_formats_equal);

438
439
440
441
442
   if (!format_array_format_table) {
      _mesa_error_no_memory(__func__);
      return;
   }

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
   for (f = 1; f < MESA_FORMAT_COUNT; ++f) {
      info = _mesa_get_format_info(f);
      if (!info->ArrayFormat)
         continue;

      if (_mesa_little_endian()) {
         array_format = info->ArrayFormat;
      } else {
         array_format = _mesa_array_format_flip_channels(info->ArrayFormat);
      }

      /* This can happen and does for some of the BGR formats.  Let's take
       * the first one in the list.
       */
      if (_mesa_hash_table_search_pre_hashed(format_array_format_table,
                                             array_format,
                                             (void *)(intptr_t)array_format))
         continue;

      _mesa_hash_table_insert_pre_hashed(format_array_format_table,
                                         array_format,
                                         (void *)(intptr_t)array_format,
                                         (void *)(intptr_t)f);
   }
}

469
470
471
mesa_format
_mesa_format_from_array_format(uint32_t array_format)
{
472
   struct hash_entry *entry;
473
474
475

   assert(_mesa_format_is_mesa_array_format(array_format));

476
   call_once(&format_array_format_table_exists, format_array_format_table_init);
477

478
   if (!format_array_format_table) {
479
480
      static const once_flag once_flag_init = ONCE_FLAG_INIT;
      format_array_format_table_exists = once_flag_init;
481
482
483
      return MESA_FORMAT_NONE;
   }

484
485
486
487
488
489
490
   entry = _mesa_hash_table_search_pre_hashed(format_array_format_table,
                                              array_format,
                                              (void *)(intptr_t)array_format);
   if (entry)
      return (intptr_t)entry->data;
   else
      return MESA_FORMAT_NONE;
491
}
492

493
/** Is the given format a compressed format? */
494
GLboolean
495
_mesa_is_format_compressed(mesa_format format)
496
497
498
499
500
501
{
   const struct gl_format_info *info = _mesa_get_format_info(format);
   return info->BlockWidth > 1 || info->BlockHeight > 1;
}


502
503
504
505
/**
 * Determine if the given format represents a packed depth/stencil buffer.
 */
GLboolean
506
_mesa_is_format_packed_depth_stencil(mesa_format format)
507
{
508
   const struct gl_format_info *info = _mesa_get_format_info(format);
509

510
   return info->BaseFormat == GL_DEPTH_STENCIL;
511
512
513
}


514
/**
515
 * Is the given format a signed/unsigned integer color format?
516
517
 */
GLboolean
518
_mesa_is_format_integer_color(mesa_format format)
519
520
{
   const struct gl_format_info *info = _mesa_get_format_info(format);
521
522
523
524
   return (info->DataType == GL_INT || info->DataType == GL_UNSIGNED_INT) &&
      info->BaseFormat != GL_DEPTH_COMPONENT &&
      info->BaseFormat != GL_DEPTH_STENCIL &&
      info->BaseFormat != GL_STENCIL_INDEX;
525
526
527
}


528
529
530
531
/**
 * Is the given format an unsigned integer format?
 */
GLboolean
532
_mesa_is_format_unsigned(mesa_format format)
533
534
535
536
537
538
{
   const struct gl_format_info *info = _mesa_get_format_info(format);
   return _mesa_is_type_unsigned(info->DataType);
}


539
540
541
542
/**
 * Does the given format store signed values?
 */
GLboolean
543
_mesa_is_format_signed(mesa_format format)
544
{
545
546
   if (format == MESA_FORMAT_R11G11B10_FLOAT || 
       format == MESA_FORMAT_R9G9B9E5_FLOAT) {
547
      /* these packed float formats only store unsigned values */
548
549
550
551
552
553
554
555
556
557
      return GL_FALSE;
   }
   else {
      const struct gl_format_info *info = _mesa_get_format_info(format);
      return (info->DataType == GL_SIGNED_NORMALIZED ||
              info->DataType == GL_INT ||
              info->DataType == GL_FLOAT);
   }
}

558
559
560
561
562
563
564
565
566
/**
 * Is the given format an integer format?
 */
GLboolean
_mesa_is_format_integer(mesa_format format)
{
   const struct gl_format_info *info = _mesa_get_format_info(format);
   return (info->DataType == GL_INT || info->DataType == GL_UNSIGNED_INT);
}
567

568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586

/**
 * Return true if the given format is a color format.
 */
GLenum
_mesa_is_format_color_format(mesa_format format)
{
   const struct gl_format_info *info = _mesa_get_format_info(format);
   switch (info->BaseFormat) {
   case GL_DEPTH_COMPONENT:
   case GL_STENCIL_INDEX:
   case GL_DEPTH_STENCIL:
      return false;
   default:
      return true;
   }
}


587
588
589
590
591
/**
 * Return color encoding for given format.
 * \return GL_LINEAR or GL_SRGB
 */
GLenum
592
_mesa_get_format_color_encoding(mesa_format format)
593
{
594
595
   const struct gl_format_info *info = _mesa_get_format_info(format);
   return info->IsSRGBFormat ? GL_SRGB : GL_LINEAR;
596
597
}

598

599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
/**
 * Return TRUE if format is an ETC2 compressed format specified
 * by GL_ARB_ES3_compatibility.
 */
bool
_mesa_is_format_etc2(mesa_format format)
{
   switch (format) {
   case MESA_FORMAT_ETC2_RGB8:
   case MESA_FORMAT_ETC2_SRGB8:
   case MESA_FORMAT_ETC2_RGBA8_EAC:
   case MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC:
   case MESA_FORMAT_ETC2_R11_EAC:
   case MESA_FORMAT_ETC2_RG11_EAC:
   case MESA_FORMAT_ETC2_SIGNED_R11_EAC:
   case MESA_FORMAT_ETC2_SIGNED_RG11_EAC:
   case MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1:
   case MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1:
      return GL_TRUE;
   default:
      return GL_FALSE;
   }
}


624
625
626
627
/**
 * For an sRGB format, return the corresponding linear color space format.
 * For non-sRGB formats, return the format as-is.
 */
628
629
mesa_format
_mesa_get_srgb_format_linear(mesa_format format)
630
631
{
   switch (format) {
632
633
   case MESA_FORMAT_BGR_SRGB8:
      format = MESA_FORMAT_BGR_UNORM8;
634
      break;
635
   case MESA_FORMAT_A8B8G8R8_SRGB:
636
      format = MESA_FORMAT_A8B8G8R8_UNORM;
637
      break;
638
   case MESA_FORMAT_B8G8R8A8_SRGB:
639
      format = MESA_FORMAT_B8G8R8A8_UNORM;
640
      break;
641
642
643
   case MESA_FORMAT_A8R8G8B8_SRGB:
      format = MESA_FORMAT_A8R8G8B8_UNORM;
      break;
644
645
646
   case MESA_FORMAT_R8G8B8A8_SRGB:
      format = MESA_FORMAT_R8G8B8A8_UNORM;
      break;
647
648
   case MESA_FORMAT_L_SRGB8:
      format = MESA_FORMAT_L_UNORM8;
649
      break;
650
651
   case MESA_FORMAT_L8A8_SRGB:
      format = MESA_FORMAT_L8A8_UNORM;
652
      break;
653
654
655
   case MESA_FORMAT_A8L8_SRGB:
      format = MESA_FORMAT_A8L8_UNORM;
      break;
656
657
658
659
660
661
662
663
664
665
666
667
   case MESA_FORMAT_SRGB_DXT1:
      format = MESA_FORMAT_RGB_DXT1;
      break;
   case MESA_FORMAT_SRGBA_DXT1:
      format = MESA_FORMAT_RGBA_DXT1;
      break;
   case MESA_FORMAT_SRGBA_DXT3:
      format = MESA_FORMAT_RGBA_DXT3;
      break;
   case MESA_FORMAT_SRGBA_DXT5:
      format = MESA_FORMAT_RGBA_DXT5;
      break;
668
   case MESA_FORMAT_R8G8B8X8_SRGB:
669
      format = MESA_FORMAT_R8G8B8X8_UNORM;
670
      break;
671
672
673
   case MESA_FORMAT_X8B8G8R8_SRGB:
      format = MESA_FORMAT_X8B8G8R8_UNORM;
      break;
674
675
676
677
678
679
680
681
682
   case MESA_FORMAT_ETC2_SRGB8:
      format = MESA_FORMAT_ETC2_RGB8;
      break;
   case MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC:
      format = MESA_FORMAT_ETC2_RGBA8_EAC;
      break;
   case MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1:
      format = MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1;
      break;
683
684
685
   case MESA_FORMAT_BPTC_SRGB_ALPHA_UNORM:
      format = MESA_FORMAT_BPTC_RGBA_UNORM;
      break;
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
   case MESA_FORMAT_SRGB8_ALPHA8_ASTC_4x4:
      format = MESA_FORMAT_RGBA_ASTC_4x4;
      break;
   case MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x4:
      format = MESA_FORMAT_RGBA_ASTC_5x4;
      break;
   case MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x5:
      format = MESA_FORMAT_RGBA_ASTC_5x5;
      break;
   case MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x5:
      format = MESA_FORMAT_RGBA_ASTC_6x5;
      break;
   case MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x6:
      format = MESA_FORMAT_RGBA_ASTC_6x6;
      break;
   case MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x5:
      format = MESA_FORMAT_RGBA_ASTC_8x5;
      break;
   case MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x6:
      format = MESA_FORMAT_RGBA_ASTC_8x6;
      break;
   case MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x8:
      format = MESA_FORMAT_RGBA_ASTC_8x8;
      break;
   case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x5:
      format = MESA_FORMAT_RGBA_ASTC_10x5;
      break;
   case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x6:
      format = MESA_FORMAT_RGBA_ASTC_10x6;
      break;
   case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x8:
      format = MESA_FORMAT_RGBA_ASTC_10x8;
      break;
   case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x10:
      format = MESA_FORMAT_RGBA_ASTC_10x10;
      break;
   case MESA_FORMAT_SRGB8_ALPHA8_ASTC_12x10:
      format = MESA_FORMAT_RGBA_ASTC_12x10;
      break;
   case MESA_FORMAT_SRGB8_ALPHA8_ASTC_12x12:
      format = MESA_FORMAT_RGBA_ASTC_12x12;
      break;
728
729
730
   case MESA_FORMAT_B8G8R8X8_SRGB:
      format = MESA_FORMAT_B8G8R8X8_UNORM;
      break;
731
732
733
   case MESA_FORMAT_X8R8G8B8_SRGB:
      format = MESA_FORMAT_X8R8G8B8_UNORM;
      break;
734
735
736
737
738
739
   default:
      break;
   }
   return format;
}

740

741
742
743
744
/**
 * If the given format is a compressed format, return a corresponding
 * uncompressed format.
 */
745
746
mesa_format
_mesa_get_uncompressed_format(mesa_format format)
747
748
749
{
   switch (format) {
   case MESA_FORMAT_RGB_FXT1:
750
      return MESA_FORMAT_BGR_UNORM8;
751
   case MESA_FORMAT_RGBA_FXT1:
752
      return MESA_FORMAT_A8B8G8R8_UNORM;
753
754
   case MESA_FORMAT_RGB_DXT1:
   case MESA_FORMAT_SRGB_DXT1:
755
      return MESA_FORMAT_BGR_UNORM8;
756
757
   case MESA_FORMAT_RGBA_DXT1:
   case MESA_FORMAT_SRGBA_DXT1:
758
      return MESA_FORMAT_A8B8G8R8_UNORM;
759
760
   case MESA_FORMAT_RGBA_DXT3:
   case MESA_FORMAT_SRGBA_DXT3:
761
      return MESA_FORMAT_A8B8G8R8_UNORM;
762
763
   case MESA_FORMAT_RGBA_DXT5:
   case MESA_FORMAT_SRGBA_DXT5:
764
      return MESA_FORMAT_A8B8G8R8_UNORM;
765
   case MESA_FORMAT_R_RGTC1_UNORM:
766
      return MESA_FORMAT_R_UNORM8;
767
   case MESA_FORMAT_R_RGTC1_SNORM:
768
      return MESA_FORMAT_R_SNORM8;
769
   case MESA_FORMAT_RG_RGTC2_UNORM:
770
      return MESA_FORMAT_R8G8_UNORM;
771
   case MESA_FORMAT_RG_RGTC2_SNORM:
772
      return MESA_FORMAT_R8G8_SNORM;
773
   case MESA_FORMAT_L_LATC1_UNORM:
774
      return MESA_FORMAT_L_UNORM8;
775
   case MESA_FORMAT_L_LATC1_SNORM:
776
      return MESA_FORMAT_L_SNORM8;
777
   case MESA_FORMAT_LA_LATC2_UNORM:
778
      return MESA_FORMAT_L8A8_UNORM;
779
   case MESA_FORMAT_LA_LATC2_SNORM:
780
      return MESA_FORMAT_L8A8_SNORM;
781
   case MESA_FORMAT_ETC1_RGB8:
782
783
   case MESA_FORMAT_ETC2_RGB8:
   case MESA_FORMAT_ETC2_SRGB8:
784
      return MESA_FORMAT_BGR_UNORM8;
785
786
787
788
   case MESA_FORMAT_ETC2_RGBA8_EAC:
   case MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC:
   case MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1:
   case MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1:
789
      return MESA_FORMAT_A8B8G8R8_UNORM;
790
791
   case MESA_FORMAT_ETC2_R11_EAC:
   case MESA_FORMAT_ETC2_SIGNED_R11_EAC:
792
      return MESA_FORMAT_R_UNORM16;
793
794
   case MESA_FORMAT_ETC2_RG11_EAC:
   case MESA_FORMAT_ETC2_SIGNED_RG11_EAC:
795
      return MESA_FORMAT_R16G16_UNORM;
796
797
798
799
800
801
   case MESA_FORMAT_BPTC_RGBA_UNORM:
   case MESA_FORMAT_BPTC_SRGB_ALPHA_UNORM:
      return MESA_FORMAT_A8B8G8R8_UNORM;
   case MESA_FORMAT_BPTC_RGB_UNSIGNED_FLOAT:
   case MESA_FORMAT_BPTC_RGB_SIGNED_FLOAT:
      return MESA_FORMAT_RGB_FLOAT32;
802
803
804
805
806
807
808
809
810
811
   default:
#ifdef DEBUG
      assert(!_mesa_is_format_compressed(format));
#endif
      return format;
   }
}


GLuint
812
_mesa_format_num_components(mesa_format format)
813
814
815
816
817
818
819
820
821
822
823
824
825
{
   const struct gl_format_info *info = _mesa_get_format_info(format);
   return ((info->RedBits > 0) +
           (info->GreenBits > 0) +
           (info->BlueBits > 0) +
           (info->AlphaBits > 0) +
           (info->LuminanceBits > 0) +
           (info->IntensityBits > 0) +
           (info->DepthBits > 0) +
           (info->StencilBits > 0));
}


826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
/**
 * Returns true if a color format has data stored in the R/G/B/A channels,
 * given an index from 0 to 3.
 */
bool
_mesa_format_has_color_component(mesa_format format, int component)
{
   const struct gl_format_info *info = _mesa_get_format_info(format);

   assert(info->BaseFormat != GL_DEPTH_COMPONENT &&
          info->BaseFormat != GL_DEPTH_STENCIL &&
          info->BaseFormat != GL_STENCIL_INDEX);

   switch (component) {
   case 0:
      return (info->RedBits + info->IntensityBits + info->LuminanceBits) > 0;
   case 1:
      return (info->GreenBits + info->IntensityBits + info->LuminanceBits) > 0;
   case 2:
      return (info->BlueBits + info->IntensityBits + info->LuminanceBits) > 0;
   case 3:
      return (info->AlphaBits + info->IntensityBits) > 0;
   default:
      assert(!"Invalid color component: must be 0..3");
      return false;
   }
}


855
856
857
858
859
/**
 * Return number of bytes needed to store an image of the given size
 * in the given format.
 */
GLuint
860
_mesa_format_image_size(mesa_format format, GLsizei width,
861
862
863
                        GLsizei height, GLsizei depth)
{
   const struct gl_format_info *info = _mesa_get_format_info(format);
864
   GLuint sz;
865
   /* Strictly speaking, a conditional isn't needed here */
866
   if (info->BlockWidth > 1 || info->BlockHeight > 1 || info->BlockDepth > 1) {
867
      /* compressed format (2D only for now) */
868
869
870
      const GLuint bw = info->BlockWidth;
      const GLuint bh = info->BlockHeight;
      const GLuint bd = info->BlockDepth;
871
872
      const GLuint wblocks = (width + bw - 1) / bw;
      const GLuint hblocks = (height + bh - 1) / bh;
873
874
875
      const GLuint dblocks = (depth + bd - 1) / bd;
      sz = wblocks * hblocks * dblocks * info->BytesPerBlock;
   } else
876
      /* non-compressed */
877
878
879
      sz = width * height * depth * info->BytesPerBlock;

   return sz;
880
881
882
}


883
884
/**
 * Same as _mesa_format_image_size() but returns a 64-bit value to
Zoë Blade's avatar
Zoë Blade committed
885
 * accommodate very large textures.
886
887
 */
uint64_t
888
_mesa_format_image_size64(mesa_format format, GLsizei width,
889
890
891
                          GLsizei height, GLsizei depth)
{
   const struct gl_format_info *info = _mesa_get_format_info(format);
892
   uint64_t sz;
893
   /* Strictly speaking, a conditional isn't needed here */
894
   if (info->BlockWidth > 1 || info->BlockHeight > 1 || info->BlockDepth > 1) {
895
      /* compressed format (2D only for now) */
896
897
898
      const uint64_t bw = info->BlockWidth;
      const uint64_t bh = info->BlockHeight;
      const uint64_t bd = info->BlockDepth;
899
900
      const uint64_t wblocks = (width + bw - 1) / bw;
      const uint64_t hblocks = (height + bh - 1) / bh;
901
902
903
      const uint64_t dblocks = (depth + bd - 1) / bd;
      sz = wblocks * hblocks * dblocks * info->BytesPerBlock;
   } else
904
      /* non-compressed */
905
906
907
908
      sz = ((uint64_t) width * (uint64_t) height *
            (uint64_t) depth * info->BytesPerBlock);

   return sz;
909
910
911
}


912

913
GLint
914
_mesa_format_row_stride(mesa_format format, GLsizei width)
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
{
   const struct gl_format_info *info = _mesa_get_format_info(format);
   /* Strictly speaking, a conditional isn't needed here */
   if (info->BlockWidth > 1 || info->BlockHeight > 1) {
      /* compressed format */
      const GLuint bw = info->BlockWidth;
      const GLuint wblocks = (width + bw - 1) / bw;
      const GLint stride = wblocks * info->BytesPerBlock;
      return stride;
   }
   else {
      const GLint stride = width * info->BytesPerBlock;
      return stride;
   }
}


932

933
/**
934
935
 * Return datatype and number of components per texel for the given
 * uncompressed mesa_format. Only used for mipmap generation code.
936
937
 */
void
938
_mesa_uncompressed_format_to_type_and_comps(mesa_format format,
939
                               GLenum *datatype, GLuint *comps)
940
{
941
   switch (format) {
942
943
944
945
946
947
948
949
   case MESA_FORMAT_A8B8G8R8_UNORM:
   case MESA_FORMAT_R8G8B8A8_UNORM:
   case MESA_FORMAT_B8G8R8A8_UNORM:
   case MESA_FORMAT_A8R8G8B8_UNORM:
   case MESA_FORMAT_X8B8G8R8_UNORM:
   case MESA_FORMAT_R8G8B8X8_UNORM:
   case MESA_FORMAT_B8G8R8X8_UNORM:
   case MESA_FORMAT_X8R8G8B8_UNORM:
950
951
952
953
   case MESA_FORMAT_A8B8G8R8_UINT:
   case MESA_FORMAT_R8G8B8A8_UINT:
   case MESA_FORMAT_B8G8R8A8_UINT:
   case MESA_FORMAT_A8R8G8B8_UINT:
954
955
956
      *datatype = GL_UNSIGNED_BYTE;
      *comps = 4;
      return;
957
958
   case MESA_FORMAT_BGR_UNORM8:
   case MESA_FORMAT_RGB_UNORM8:
959
      *datatype = GL_UNSIGNED_BYTE;
960
      *comps = 3;
961
      return;
962
963
   case MESA_FORMAT_B5G6R5_UNORM:
   case MESA_FORMAT_R5G6B5_UNORM:
964
965
   case MESA_FORMAT_B5G6R5_UINT:
   case MESA_FORMAT_R5G6B5_UINT:
966
967
968
969
      *datatype = GL_UNSIGNED_SHORT_5_6_5;
      *comps = 3;
      return;

970
971
972
   case MESA_FORMAT_B4G4R4A4_UNORM:
   case MESA_FORMAT_A4R4G4B4_UNORM:
   case MESA_FORMAT_B4G4R4X4_UNORM:
973
974
   case MESA_FORMAT_B4G4R4A4_UINT:
   case MESA_FORMAT_A4R4G4B4_UINT:
975
976
977
978
      *datatype = GL_UNSIGNED_SHORT_4_4_4_4;
      *comps = 4;
      return;

979
980
981
   case MESA_FORMAT_B5G5R5A1_UNORM:
   case MESA_FORMAT_A1R5G5B5_UNORM:
   case MESA_FORMAT_B5G5R5X1_UNORM:
982
983
   case MESA_FORMAT_B5G5R5A1_UINT:
   case MESA_FORMAT_A1R5G5B5_UINT:
984
985
986
987
      *datatype = GL_UNSIGNED_SHORT_1_5_5_5_REV;
      *comps = 4;
      return;

988
   case MESA_FORMAT_B10G10R10A2_UNORM:
989
990
991
992
      *datatype = GL_UNSIGNED_INT_2_10_10_10_REV;
      *comps = 4;
      return;

993
   case MESA_FORMAT_A1B5G5R5_UNORM:
994
   case MESA_FORMAT_A1B5G5R5_UINT:
995
996
997
998
      *datatype = GL_UNSIGNED_SHORT_5_5_5_1;
      *comps = 4;
      return;

999
   case MESA_FORMAT_L4A4_UNORM:
1000
1001
1002
1003
      *datatype = MESA_UNSIGNED_BYTE_4_4;
      *comps = 2;
      return;

1004
1005
1006
1007
   case MESA_FORMAT_L8A8_UNORM:
   case MESA_FORMAT_A8L8_UNORM:
   case MESA_FORMAT_R8G8_UNORM:
   case MESA_FORMAT_G8R8_UNORM:
1008
1009
1010
      *datatype = GL_UNSIGNED_BYTE;
      *comps = 2;
      return;
1011

1012
1013
1014
1015
   case MESA_FORMAT_L16A16_UNORM:
   case MESA_FORMAT_A16L16_UNORM:
   case MESA_FORMAT_R16G16_UNORM:
   case MESA_FORMAT_G16R16_UNORM:
1016
1017
1018
1019
      *datatype = GL_UNSIGNED_SHORT;
      *comps = 2;
      return;

1020
1021
1022
1023
   case MESA_FORMAT_R_UNORM16:
   case MESA_FORMAT_A_UNORM16:
   case MESA_FORMAT_L_UNORM16:
   case MESA_FORMAT_I_UNORM16:
1024
      *datatype = GL_UNSIGNED_SHORT;
1025
      *comps = 1;
1026
1027
      return;

1028
   case MESA_FORMAT_R3G3B2_UNORM:
1029
   case MESA_FORMAT_R3G3B2_UINT:
1030
1031
1032
1033
      *datatype = GL_UNSIGNED_BYTE_2_3_3_REV;
      *comps = 3;
      return;
   case MESA_FORMAT_A4B4G4R4_UNORM:
1034
   case MESA_FORMAT_A4B4G4R4_UINT:
1035
1036
1037
1038
      *datatype = GL_UNSIGNED_SHORT_4_4_4_4;
      *comps = 4;
      return;

1039
   case MESA_FORMAT_R4G4B4A4_UNORM:
1040
   case MESA_FORMAT_R4G4B4A4_UINT:
1041
1042
1043
1044
      *datatype = GL_UNSIGNED_SHORT_4_4_4_4;
      *comps = 4;
      return;
   case MESA_FORMAT_R5G5B5A1_UNORM:
1045
   case MESA_FORMAT_R5G5B5A1_UINT:
1046
1047
1048
1049
1050
      *datatype = GL_UNSIGNED_SHORT_1_5_5_5_REV;
      *comps = 4;
      return;
   case MESA_FORMAT_A2B10G10R10_UNORM:
   case MESA_FORMAT_A2B10G10R10_UINT:
1051
1052
1053
      *datatype = GL_UNSIGNED_INT_10_10_10_2;
      *comps = 4;
      return;
1054
1055
1056
1057
1058
1059
   case MESA_FORMAT_A2R10G10B10_UNORM:
   case MESA_FORMAT_A2R10G10B10_UINT:
      *datatype = GL_UNSIGNED_INT_10_10_10_2;
      *comps = 4;
      return;

1060
   case MESA_FORMAT_B2G3R3_UNORM:
1061
   case MESA_FORMAT_B2G3R3_UINT:
1062
1063
1064
1065
      *datatype = GL_UNSIGNED_BYTE_3_3_2;
      *comps = 3;
      return;

1066
1067
1068
1069
1070
   case MESA_FORMAT_A_UNORM8:
   case MESA_FORMAT_L_UNORM8:
   case MESA_FORMAT_I_UNORM8:
   case MESA_FORMAT_R_UNORM8:
   case MESA_FORMAT_S_UINT8:
1071
1072
      *datatype = GL_UNSIGNED_BYTE;
      *comps = 1;
1073
1074
1075
1076
1077
      return;

   case MESA_FORMAT_YCBCR:
   case MESA_FORMAT_YCBCR_REV:
      *datatype = GL_UNSIGNED_SHORT;
1078
      *comps = 2;
1079
1080
      return;

1081
   case MESA_FORMAT_S8_UINT_Z24_UNORM:
1082
1083
      *datatype = GL_UNSIGNED_INT_24_8_MESA;
      *comps = 2;
1084
1085
      return;

1086
   case MESA_FORMAT_Z24_UNORM_S8_UINT:
1087
1088
      *datatype = GL_UNSIGNED_INT_8_24_REV_MESA;
      *comps = 2;
1089
1090
      return;

1091
   case MESA_FORMAT_Z_UNORM16:
1092
      *datatype = GL_UNSIGNED_SHORT;
1093
1094
1095
      *comps = 1;
      return;

1096
   case MESA_FORMAT_Z24_UNORM_X8_UINT:
1097
1098
1099
1100
      *datatype = GL_UNSIGNED_INT;
      *comps = 1;
      return;

1101
   case MESA_FORMAT_X8_UINT_Z24_UNORM:
1102
1103
1104
1105
      *datatype = GL_UNSIGNED_INT;
      *comps = 1;
      return;

1106
   case MESA_FORMAT_Z_UNORM32:
1107
1108
1109
1110
      *datatype = GL_UNSIGNED_INT;
      *comps = 1;
      return;

1111
   case MESA_FORMAT_Z_FLOAT32:
1112
1113
1114
1115
      *datatype = GL_FLOAT;
      *comps = 1;
      return;

1116
   case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
1117
1118
1119
1120
      *datatype = GL_FLOAT_32_UNSIGNED_INT_24_8_REV;
      *comps = 1;
      return;

1121
1122
1123
1124
   case MESA_FORMAT_R_SNORM8:
   case MESA_FORMAT_A_SNORM8:
   case MESA_FORMAT_L_SNORM8:
   case MESA_FORMAT_I_SNORM8:
1125
1126
1127
      *datatype = GL_BYTE;
      *comps = 1;
      return;
1128
1129
   case MESA_FORMAT_R8G8_SNORM:
   case MESA_FORMAT_L8A8_SNORM:
1130
   case MESA_FORMAT_A8L8_SNORM:
1131
1132
1133
      *datatype = GL_BYTE;
      *comps = 2;
      return;
1134
1135
1136
   case MESA_FORMAT_A8B8G8R8_SNORM:
   case MESA_FORMAT_R8G8B8A8_SNORM:
   case MESA_FORMAT_X8B8G8R8_SNORM:
1137
      *datatype = GL_BYTE;
1138
      *comps = 4;
1139
      return;
1140

1141
   case MESA_FORMAT_RGBA_UNORM16:
1142
1143
1144
1145
      *datatype = GL_UNSIGNED_SHORT;
      *comps = 4;
      return;

1146
1147
1148
1149
   case MESA_FORMAT_R_SNORM16:
   case MESA_FORMAT_A_SNORM16:
   case MESA_FORMAT_L_SNORM16:
   case MESA_FORMAT_I_SNORM16:
1150
1151
1152
      *datatype = GL_SHORT;
      *comps = 1;
      return;
1153
1154
   case MESA_FORMAT_R16G16_SNORM:
   case MESA_FORMAT_LA_SNORM16:
1155
1156
1157
      *datatype = GL_SHORT;
      *comps = 2;
      return;
1158
   case MESA_FORMAT_RGB_SNORM16:
1159
1160
1161
      *datatype = GL_SHORT;
      *comps = 3;
      return;
1162
   case MESA_FORMAT_RGBA_SNORM16:
1163
      *datatype = GL_SHORT;
1164
      *comps = 4;
1165
      return;
1166

1167
   case MESA_FORMAT_BGR_SRGB8:
1168
1169
1170
      *datatype = GL_UNSIGNED_BYTE;
      *comps = 3;
      return;
1171
1172
   case MESA_FORMAT_A8B8G8R8_SRGB:
   case MESA_FORMAT_B8G8R8A8_SRGB:
1173
   case MESA_FORMAT_A8R8G8B8_SRGB:
1174
   case MESA_FORMAT_R8G8B8A8_SRGB:
1175
1176
1177
      *datatype = GL_UNSIGNED_BYTE;
      *comps = 4;
      return;
1178
   case MESA_FORMAT_L_SRGB8:
1179
1180
1181
      *datatype = GL_UNSIGNED_BYTE;
      *comps = 1;
      return;
1182
   case MESA_FORMAT_L8A8_SRGB:
1183
   case MESA_FORMAT_A8L8_SRGB:
1184
      *datatype = GL_UNSIGNED_BYTE;
1185
      *comps = 2;
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
      return;

   case MESA_FORMAT_RGBA_FLOAT32:
      *datatype = GL_FLOAT;
      *comps = 4;
      return;
   case MESA_FORMAT_RGBA_FLOAT16:
      *datatype = GL_HALF_FLOAT_ARB;
      *comps = 4;
      return;
   case MESA_FORMAT_RGB_FLOAT32:
      *datatype = GL_FLOAT;
      *comps = 3;
      return;
   case MESA_FORMAT_RGB_FLOAT16:
      *datatype = GL_HALF_FLOAT_ARB;
      *comps = 3;
      return;
1204
   case MESA_FORMAT_LA_FLOAT32:
1205
   case MESA_FORMAT_RG_FLOAT32:
1206
1207
1208
      *datatype = GL_FLOAT;
      *comps = 2;
      return;
1209
   case MESA_FORMAT_LA_FLOAT16:
1210
   case MESA_FORMAT_RG_FLOAT16:
1211
1212
1213
      *datatype = GL_HALF_FLOAT_ARB;
      *comps = 2;
      return;
1214
1215
1216
   case MESA_FORMAT_A_FLOAT32:
   case MESA_FORMAT_L_FLOAT32:
   case MESA_FORMAT_I_FLOAT32:
1217
   case MESA_FORMAT_R_FLOAT32:
1218
1219
1220
      *datatype = GL_FLOAT;
      *comps = 1;
      return;
1221
1222
1223
   case MESA_FORMAT_A_FLOAT16:
   case MESA_FORMAT_L_FLOAT16:
   case MESA_FORMAT_I_FLOAT16:
1224
   case MESA_FORMAT_R_FLOAT16:
1225
1226
1227
1228
      *datatype = GL_HALF_FLOAT_ARB;
      *comps = 1;
      return;

1229
1230
1231
   case MESA_FORMAT_A_UINT8:
   case MESA_FORMAT_L_UINT8:
   case MESA_FORMAT_I_UINT8:
1232
1233
1234
      *datatype = GL_UNSIGNED_BYTE;
      *comps = 1;
      return;
1235
   case MESA_FORMAT_LA_UINT8:
1236
      *datatype = GL_UNSIGNED_BYTE;
1237
      *comps = 2;
1238
1239
      return;

1240
1241
1242
   case MESA_FORMAT_A_UINT16:
   case MESA_FORMAT_L_UINT16:
   case MESA_FORMAT_I_UINT16:
1243
1244
1245
      *datatype = GL_UNSIGNED_SHORT;
      *comps = 1;
      return;
1246
   case MESA_FORMAT_LA_UINT16:
1247
      *datatype = GL_UNSIGNED_SHORT;