textureview.c 30.2 KB
Newer Older
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
/*
 * Mesa 3-D graphics library
 *
 * Copyright (C) 2013 LunarG, 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
 * 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.
 *
 * Authors:
 *    Courtney Goeltzenleuchter <courtney@lunarg.com>
 */


/**
 * \file textureview.c
 * GL_ARB_texture_view functions
 */

#include "glheader.h"
#include "context.h"
#include "enums.h"
#include "imports.h"
#include "macros.h"
#include "teximage.h"
#include "texobj.h"
41
#include "mipmap.h"
42
43
#include "texstorage.h"
#include "textureview.h"
44
#include "stdbool.h"
45
46
#include "mtypes.h"

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
/* Table 3.X.2 (Compatible internal formats for TextureView)
    ---------------------------------------------------------------------------
    | Class                 | Internal formats                                |
    ---------------------------------------------------------------------------
    | VIEW_CLASS_128_BITS   | RGBA32F, RGBA32UI, RGBA32I                      |
    ---------------------------------------------------------------------------
    | VIEW_CLASS_96_BITS    | RGB32F, RGB32UI, RGB32I                         |
    ---------------------------------------------------------------------------
    | VIEW_CLASS_64_BITS    | RGBA16F, RG32F, RGBA16UI, RG32UI, RGBA16I,      |
    |                       | RG32I, RGBA16, RGBA16_SNORM                     |
    ---------------------------------------------------------------------------
    | VIEW_CLASS_48_BITS    | RGB16, RGB16_SNORM, RGB16F, RGB16UI, RGB16I     |
    ---------------------------------------------------------------------------
    | VIEW_CLASS_32_BITS    | RG16F, R11F_G11F_B10F, R32F,                    |
    |                       | RGB10_A2UI, RGBA8UI, RG16UI, R32UI,             |
    |                       | RGBA8I, RG16I, R32I, RGB10_A2, RGBA8, RG16,     |
    |                       | RGBA8_SNORM, RG16_SNORM, SRGB8_ALPHA8, RGB9_E5  |
    ---------------------------------------------------------------------------
    | VIEW_CLASS_24_BITS    | RGB8, RGB8_SNORM, SRGB8, RGB8UI, RGB8I          |
    ---------------------------------------------------------------------------
    | VIEW_CLASS_16_BITS    | R16F, RG8UI, R16UI, RG8I, R16I, RG8, R16,       |
    |                       | RG8_SNORM, R16_SNORM                            |
    ---------------------------------------------------------------------------
    | VIEW_CLASS_8_BITS     | R8UI, R8I, R8, R8_SNORM                         |
    ---------------------------------------------------------------------------
    | VIEW_CLASS_RGTC1_RED  | COMPRESSED_RED_RGTC1,                           |
    |                       | COMPRESSED_SIGNED_RED_RGTC1                     |
    ---------------------------------------------------------------------------
    | VIEW_CLASS_RGTC2_RG   | COMPRESSED_RG_RGTC2,                            |
    |                       | COMPRESSED_SIGNED_RG_RGTC2                      |
    ---------------------------------------------------------------------------
    | VIEW_CLASS_BPTC_UNORM | COMPRESSED_RGBA_BPTC_UNORM,                     |
    |                       | COMPRESSED_SRGB_ALPHA_BPTC_UNORM                |
    ---------------------------------------------------------------------------
    | VIEW_CLASS_BPTC_FLOAT | COMPRESSED_RGB_BPTC_SIGNED_FLOAT,               |
    |                       | COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT              |
    ---------------------------------------------------------------------------
 */
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

#define VIEW_CLASS_GLES(x)             (GL_VIEW_CLASS_BPTC_FLOAT + 1 + x)
#define VIEW_CLASS_EAC_R11             VIEW_CLASS_GLES(0)
#define VIEW_CLASS_EAC_RG11            VIEW_CLASS_GLES(1)
#define VIEW_CLASS_ETC2_RGB            VIEW_CLASS_GLES(2)
#define VIEW_CLASS_ETC2_RGBA           VIEW_CLASS_GLES(3)
#define VIEW_CLASS_ETC2_EAC_RGBA       VIEW_CLASS_GLES(4)
#define VIEW_CLASS_ASTC_4x4_RGBA       VIEW_CLASS_GLES(5)
#define VIEW_CLASS_ASTC_5x4_RGBA       VIEW_CLASS_GLES(6)
#define VIEW_CLASS_ASTC_5x5_RGBA       VIEW_CLASS_GLES(7)
#define VIEW_CLASS_ASTC_6x5_RGBA       VIEW_CLASS_GLES(8)
#define VIEW_CLASS_ASTC_6x6_RGBA       VIEW_CLASS_GLES(9)
#define VIEW_CLASS_ASTC_8x5_RGBA       VIEW_CLASS_GLES(10)
#define VIEW_CLASS_ASTC_8x6_RGBA       VIEW_CLASS_GLES(11)
#define VIEW_CLASS_ASTC_8x8_RGBA       VIEW_CLASS_GLES(12)
#define VIEW_CLASS_ASTC_10x5_RGBA      VIEW_CLASS_GLES(13)
#define VIEW_CLASS_ASTC_10x6_RGBA      VIEW_CLASS_GLES(14)
#define VIEW_CLASS_ASTC_10x8_RGBA      VIEW_CLASS_GLES(15)
#define VIEW_CLASS_ASTC_10x10_RGBA     VIEW_CLASS_GLES(16)
#define VIEW_CLASS_ASTC_12x10_RGBA     VIEW_CLASS_GLES(17)
#define VIEW_CLASS_ASTC_12x12_RGBA     VIEW_CLASS_GLES(18)
#define VIEW_CLASS_ASTC_3x3x3_RGBA     VIEW_CLASS_GLES(19)
#define VIEW_CLASS_ASTC_4x3x3_RGBA     VIEW_CLASS_GLES(20)
#define VIEW_CLASS_ASTC_4x4x3_RGBA     VIEW_CLASS_GLES(21)
#define VIEW_CLASS_ASTC_4x4x4_RGBA     VIEW_CLASS_GLES(22)
#define VIEW_CLASS_ASTC_5x4x4_RGBA     VIEW_CLASS_GLES(23)
#define VIEW_CLASS_ASTC_5x5x4_RGBA     VIEW_CLASS_GLES(24)
#define VIEW_CLASS_ASTC_5x5x5_RGBA     VIEW_CLASS_GLES(25)
#define VIEW_CLASS_ASTC_6x5x5_RGBA     VIEW_CLASS_GLES(26)
#define VIEW_CLASS_ASTC_6x6x5_RGBA     VIEW_CLASS_GLES(27)
#define VIEW_CLASS_ASTC_6x6x6_RGBA     VIEW_CLASS_GLES(28)


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
struct internal_format_class_info {
   GLenum view_class;
   GLenum internal_format;
};
static const struct internal_format_class_info compatible_internal_formats[] = {
   {GL_VIEW_CLASS_128_BITS, GL_RGBA32F},
   {GL_VIEW_CLASS_128_BITS, GL_RGBA32UI},
   {GL_VIEW_CLASS_128_BITS, GL_RGBA32I},
   {GL_VIEW_CLASS_96_BITS, GL_RGB32F},
   {GL_VIEW_CLASS_96_BITS, GL_RGB32UI},
   {GL_VIEW_CLASS_96_BITS, GL_RGB32I},
   {GL_VIEW_CLASS_64_BITS, GL_RGBA16F},
   {GL_VIEW_CLASS_64_BITS, GL_RG32F},
   {GL_VIEW_CLASS_64_BITS, GL_RGBA16UI},
   {GL_VIEW_CLASS_64_BITS, GL_RG32UI},
   {GL_VIEW_CLASS_64_BITS, GL_RGBA16I},
   {GL_VIEW_CLASS_64_BITS, GL_RG32I},
   {GL_VIEW_CLASS_64_BITS, GL_RGBA16},
   {GL_VIEW_CLASS_64_BITS, GL_RGBA16_SNORM},
   {GL_VIEW_CLASS_48_BITS, GL_RGB16},
   {GL_VIEW_CLASS_48_BITS, GL_RGB16_SNORM},
   {GL_VIEW_CLASS_48_BITS, GL_RGB16F},
   {GL_VIEW_CLASS_48_BITS, GL_RGB16UI},
   {GL_VIEW_CLASS_48_BITS, GL_RGB16I},
   {GL_VIEW_CLASS_32_BITS, GL_RG16F},
   {GL_VIEW_CLASS_32_BITS, GL_R11F_G11F_B10F},
   {GL_VIEW_CLASS_32_BITS, GL_R32F},
   {GL_VIEW_CLASS_32_BITS, GL_RGB10_A2UI},
   {GL_VIEW_CLASS_32_BITS, GL_RGBA8UI},
   {GL_VIEW_CLASS_32_BITS, GL_RG16UI},
   {GL_VIEW_CLASS_32_BITS, GL_R32UI},
   {GL_VIEW_CLASS_32_BITS, GL_RGBA8I},
   {GL_VIEW_CLASS_32_BITS, GL_RG16I},
   {GL_VIEW_CLASS_32_BITS, GL_R32I},
   {GL_VIEW_CLASS_32_BITS, GL_RGB10_A2},
   {GL_VIEW_CLASS_32_BITS, GL_RGBA8},
   {GL_VIEW_CLASS_32_BITS, GL_RG16},
   {GL_VIEW_CLASS_32_BITS, GL_RGBA8_SNORM},
   {GL_VIEW_CLASS_32_BITS, GL_RG16_SNORM},
   {GL_VIEW_CLASS_32_BITS, GL_SRGB8_ALPHA8},
   {GL_VIEW_CLASS_32_BITS, GL_RGB9_E5},
   {GL_VIEW_CLASS_24_BITS, GL_RGB8},
   {GL_VIEW_CLASS_24_BITS, GL_RGB8_SNORM},
   {GL_VIEW_CLASS_24_BITS, GL_SRGB8},
   {GL_VIEW_CLASS_24_BITS, GL_RGB8UI},
   {GL_VIEW_CLASS_24_BITS, GL_RGB8I},
   {GL_VIEW_CLASS_16_BITS, GL_R16F},
   {GL_VIEW_CLASS_16_BITS, GL_RG8UI},
   {GL_VIEW_CLASS_16_BITS, GL_R16UI},
   {GL_VIEW_CLASS_16_BITS, GL_RG8I},
   {GL_VIEW_CLASS_16_BITS, GL_R16I},
   {GL_VIEW_CLASS_16_BITS, GL_RG8},
   {GL_VIEW_CLASS_16_BITS, GL_R16},
   {GL_VIEW_CLASS_16_BITS, GL_RG8_SNORM},
   {GL_VIEW_CLASS_16_BITS, GL_R16_SNORM},
   {GL_VIEW_CLASS_8_BITS, GL_R8UI},
   {GL_VIEW_CLASS_8_BITS, GL_R8I},
   {GL_VIEW_CLASS_8_BITS, GL_R8},
   {GL_VIEW_CLASS_8_BITS, GL_R8_SNORM},
   {GL_VIEW_CLASS_RGTC1_RED, GL_COMPRESSED_RED_RGTC1},
   {GL_VIEW_CLASS_RGTC1_RED, GL_COMPRESSED_SIGNED_RED_RGTC1},
   {GL_VIEW_CLASS_RGTC2_RG, GL_COMPRESSED_RG_RGTC2},
   {GL_VIEW_CLASS_RGTC2_RG, GL_COMPRESSED_SIGNED_RG_RGTC2},
   {GL_VIEW_CLASS_BPTC_UNORM, GL_COMPRESSED_RGBA_BPTC_UNORM_ARB},
   {GL_VIEW_CLASS_BPTC_UNORM, GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB},
   {GL_VIEW_CLASS_BPTC_FLOAT, GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB},
   {GL_VIEW_CLASS_BPTC_FLOAT, GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB},
};

static const struct internal_format_class_info s3tc_compatible_internal_formats[] = {
   {GL_VIEW_CLASS_S3TC_DXT1_RGB, GL_COMPRESSED_RGB_S3TC_DXT1_EXT},
   {GL_VIEW_CLASS_S3TC_DXT1_RGB, GL_COMPRESSED_SRGB_S3TC_DXT1_EXT},
   {GL_VIEW_CLASS_S3TC_DXT1_RGBA, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT},
   {GL_VIEW_CLASS_S3TC_DXT1_RGBA, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT},
   {GL_VIEW_CLASS_S3TC_DXT3_RGBA, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT},
   {GL_VIEW_CLASS_S3TC_DXT3_RGBA, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT},
   {GL_VIEW_CLASS_S3TC_DXT5_RGBA, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT},
   {GL_VIEW_CLASS_S3TC_DXT5_RGBA, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT},
};

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
static const struct internal_format_class_info gles_etc2_compatible_internal_formats[] = {
   {VIEW_CLASS_EAC_R11, GL_COMPRESSED_R11_EAC},
   {VIEW_CLASS_EAC_R11, GL_COMPRESSED_SIGNED_R11_EAC},
   {VIEW_CLASS_EAC_RG11, GL_COMPRESSED_RG11_EAC},
   {VIEW_CLASS_EAC_RG11, GL_COMPRESSED_SIGNED_RG11_EAC},
   {VIEW_CLASS_ETC2_RGB, GL_COMPRESSED_RGB8_ETC2},
   {VIEW_CLASS_ETC2_RGB, GL_COMPRESSED_SRGB8_ETC2},
   {VIEW_CLASS_ETC2_RGBA, GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2},
   {VIEW_CLASS_ETC2_RGBA, GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2},
   {VIEW_CLASS_ETC2_EAC_RGBA, GL_COMPRESSED_RGBA8_ETC2_EAC},
   {VIEW_CLASS_ETC2_EAC_RGBA, GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC},
};

static const struct internal_format_class_info gles_astc_compatible_internal_formats[] = {
#define ASTC_FMT(size) \
   {VIEW_CLASS_ASTC_##size## _RGBA, GL_COMPRESSED_RGBA_ASTC_##size##_KHR}, \
   {VIEW_CLASS_ASTC_##size##_RGBA, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_##size##_KHR}

   ASTC_FMT(4x4),
   ASTC_FMT(5x4),
   ASTC_FMT(5x5),
   ASTC_FMT(6x5),
   ASTC_FMT(6x6),
   ASTC_FMT(8x5),
   ASTC_FMT(8x6),
   ASTC_FMT(8x8),
   ASTC_FMT(10x5),
   ASTC_FMT(10x6),
   ASTC_FMT(10x8),
   ASTC_FMT(10x10),
   ASTC_FMT(12x10),
   ASTC_FMT(12x12),
#undef ASTC_FMT
};

233
234
GLenum
_mesa_texture_view_lookup_view_class(const struct gl_context *ctx, GLenum internalformat)
235
{
236
   GLuint i;
237
238
239
240
241
242

   for (i = 0; i < ARRAY_SIZE(compatible_internal_formats); i++) {
      if (compatible_internal_formats[i].internal_format == internalformat)
         return compatible_internal_formats[i].view_class;
   }

243
244
   if (ctx->Extensions.EXT_texture_compression_s3tc &&
       ctx->Extensions.EXT_texture_sRGB) {
245
      for (i = 0; i < ARRAY_SIZE(s3tc_compatible_internal_formats); i++) {
246
247
         if (s3tc_compatible_internal_formats[i].internal_format
             == internalformat)
248
249
250
            return s3tc_compatible_internal_formats[i].view_class;
      }
   }
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268

   if (_mesa_is_gles3(ctx)) {
      for (i = 0; i < ARRAY_SIZE(gles_etc2_compatible_internal_formats); i++) {
         if (gles_etc2_compatible_internal_formats[i].internal_format
             == internalformat)
            return gles_etc2_compatible_internal_formats[i].view_class;
      }

      if (ctx->Extensions.KHR_texture_compression_astc_ldr) {
         for (i = 0; i < ARRAY_SIZE(gles_astc_compatible_internal_formats); i++) {
            if (gles_astc_compatible_internal_formats[i].internal_format
                == internalformat)
               return gles_astc_compatible_internal_formats[i].view_class;
         }
      }

      /* FINISHME: Add 3D OES formats when supported */
   }
269
270
271
272
273
274
275
276
277
278
279
280
281
282
   return GL_FALSE;
}

/**
 * Initialize new texture's gl_texture_image structures. Will not call driver
 * to allocate new space, simply record relevant layer, face, format, etc.
 * \return GL_FALSE if any error, GL_TRUE otherwise.
 */
static GLboolean
initialize_texture_fields(struct gl_context *ctx,
                          GLenum target,
                          struct gl_texture_object *texObj,
                          GLint levels,
                          GLsizei width, GLsizei height, GLsizei depth,
283
                          GLenum internalFormat, mesa_format texFormat)
284
285
286
287
288
289
290
291
292
293
294
{
   const GLuint numFaces = _mesa_num_tex_faces(target);
   GLint level, levelWidth = width, levelHeight = height, levelDepth = depth;
   GLuint face;

   /* Pretend we are bound to initialize the gl_texture_image structs */
   texObj->Target = target;

   /* Set up all the texture object's gl_texture_images */
   for (level = 0; level < levels; level++) {
      for (face = 0; face < numFaces; face++) {
295
         struct gl_texture_image *texImage;
296
         const GLenum faceTarget = _mesa_cube_face_target(target, face);
297
298

         texImage = _mesa_get_tex_image(ctx, texObj, faceTarget, level);
299
300
301
302
303
304
305
306
307
308
309

         if (!texImage) {
            _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexStorage");
            return GL_FALSE;
         }

         _mesa_init_teximage_fields(ctx, texImage,
                                    levelWidth, levelHeight, levelDepth,
                                    0, internalFormat, texFormat);
      }

310
311
      _mesa_next_mipmap_level_size(target, 0,
                                   levelWidth, levelHeight, levelDepth,
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
                                   &levelWidth, &levelHeight, &levelDepth);
   }

   /* "unbind" */
   texObj->Target = 0;

   return GL_TRUE;
}

#define RETURN_IF_SUPPORTED(t) do {		\
   if (newTarget == GL_ ## t)                   \
      return true;				\
} while (0)

/**
 * Check for compatible target
 * If an error is found, record it with _mesa_error()
 * \return false if any error, true otherwise.
 */
static bool
target_valid(struct gl_context *ctx, GLenum origTarget, GLenum newTarget)
{
   /*
    * From ARB_texture_view spec:
   ---------------------------------------------------------------------------------------------------------
   | Original target              | Valid new targets |
   ---------------------------------------------------------------------------------------------------------
   | TEXTURE_1D                   | TEXTURE_1D, TEXTURE_1D_ARRAY |
   | ------------------------------------------------------------------------------------------------------- |
   | TEXTURE_2D                   | TEXTURE_2D, TEXTURE_2D_ARRAY |
   | ------------------------------------------------------------------------------------------------------- |
   | TEXTURE_3D                   | TEXTURE_3D |
   | ------------------------------------------------------------------------------------------------------- |
   | TEXTURE_CUBE_MAP             | TEXTURE_CUBE_MAP, TEXTURE_2D, TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP_ARRAY |
   | ------------------------------------------------------------------------------------------------------- |
   | TEXTURE_RECTANGLE            | TEXTURE_RECTANGLE |
   | ------------------------------------------------------------------------------------------------------- |
   | TEXTURE_BUFFER               | <none> |
   | ------------------------------------------------------------------------------------------------------- |
   | TEXTURE_1D_ARRAY             | TEXTURE_1D_ARRAY, TEXTURE_1D |
   | ------------------------------------------------------------------------------------------------------- |
   | TEXTURE_2D_ARRAY             | TEXTURE_2D_ARRAY, TEXTURE_2D, TEXTURE_CUBE_MAP, TEXTURE_CUBE_MAP_ARRAY |
   | ------------------------------------------------------------------------------------------------------- |
   | TEXTURE_CUBE_MAP_ARRAY       | TEXTURE_CUBE_MAP_ARRAY, TEXTURE_2D_ARRAY, TEXTURE_2D, TEXTURE_CUBE_MAP |
   | ------------------------------------------------------------------------------------------------------- |
   | TEXTURE_2D_MULTISAMPLE       | TEXTURE_2D_MULTISAMPLE, TEXTURE_2D_MULTISAMPLE_ARRAY |
   | ------------------------------------------------------------------------------------------------------- |
   | TEXTURE_2D_MULTISAMPLE_ARRAY | TEXTURE_2D_MULTISAMPLE, TEXTURE_2D_MULTISAMPLE_ARRAY |
   ---------------------------------------------------------------------------------------------------------
    */

   switch (origTarget) {
   case GL_TEXTURE_1D:
   case GL_TEXTURE_1D_ARRAY:
      RETURN_IF_SUPPORTED(TEXTURE_1D);
      RETURN_IF_SUPPORTED(TEXTURE_1D_ARRAY);
      break;
   case GL_TEXTURE_2D:
      RETURN_IF_SUPPORTED(TEXTURE_2D);
      RETURN_IF_SUPPORTED(TEXTURE_2D_ARRAY);
      break;
   case GL_TEXTURE_3D:
      RETURN_IF_SUPPORTED(TEXTURE_3D);
      break;
   case GL_TEXTURE_RECTANGLE:
      RETURN_IF_SUPPORTED(TEXTURE_RECTANGLE);
      break;
   case GL_TEXTURE_CUBE_MAP:
   case GL_TEXTURE_2D_ARRAY:
   case GL_TEXTURE_CUBE_MAP_ARRAY:
      RETURN_IF_SUPPORTED(TEXTURE_2D);
      RETURN_IF_SUPPORTED(TEXTURE_2D_ARRAY);
      RETURN_IF_SUPPORTED(TEXTURE_CUBE_MAP);
      RETURN_IF_SUPPORTED(TEXTURE_CUBE_MAP_ARRAY);
      break;
   case GL_TEXTURE_2D_MULTISAMPLE:
   case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
      RETURN_IF_SUPPORTED(TEXTURE_2D_MULTISAMPLE);
      RETURN_IF_SUPPORTED(TEXTURE_2D_MULTISAMPLE_ARRAY);
      break;
   }
   _mesa_error(ctx, GL_INVALID_OPERATION,
               "glTextureView(illegal target=%s)",
395
               _mesa_enum_to_string(newTarget));
396
397
398
399
400
401
402
403
404
   return false;
}
#undef RETURN_IF_SUPPORTED

/**
 * Check for compatible format
 * If an error is found, record it with _mesa_error()
 * \return false if any error, true otherwise.
 */
405
406
bool
_mesa_texture_view_compatible_format(const struct gl_context *ctx,
407
408
                                     GLenum origInternalFormat,
                                     GLenum newInternalFormat)
409
410
411
412
413
414
415
416
417
{
   unsigned int origViewClass, newViewClass;

   /* The two textures' internal formats must be compatible according to
    * Table 3.X.2 (Compatible internal formats for TextureView)
    * if the internal format exists in that table the view class must match.
    * The internal formats must be identical if not in that table,
    * or an INVALID_OPERATION error is generated.
    */
418
   if (origInternalFormat == newInternalFormat)
419
      return true;
420

421
422
   origViewClass = _mesa_texture_view_lookup_view_class(ctx, origInternalFormat);
   newViewClass = _mesa_texture_view_lookup_view_class(ctx, newInternalFormat);
423
   if ((origViewClass == newViewClass) && origViewClass != false)
424
      return true;
425

426
   return false;
427
}
428

429
430
431
432
433
434
435
436
437
438
439
440
/**
 * Helper function for TexStorage and teximagemultisample to set immutable
 * texture state needed by ARB_texture_view.
 */
void
_mesa_set_texture_view_state(struct gl_context *ctx,
                             struct gl_texture_object *texObj,
                             GLenum target, GLuint levels)
{
   struct gl_texture_image *texImage;

   /* Get a reference to what will become this View's base level */
441
   texImage = _mesa_select_tex_image(texObj, target, 0);
442

443
444
   /* When an immutable texture is created via glTexStorage or
    * glTexImageMultisample,
445
446
447
448
449
    * TEXTURE_IMMUTABLE_FORMAT becomes TRUE.
    * TEXTURE_IMMUTABLE_LEVELS and TEXTURE_VIEW_NUM_LEVELS become levels.
    * If the texture target is TEXTURE_1D_ARRAY then
    * TEXTURE_VIEW_NUM_LAYERS becomes height.
    * If the texture target is TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP_ARRAY,
450
451
    * or TEXTURE_2D_MULTISAMPLE_ARRAY then TEXTURE_VIEW_NUM_LAYERS becomes
    * depth.
452
453
454
    * If the texture target is TEXTURE_CUBE_MAP, then
    * TEXTURE_VIEW_NUM_LAYERS becomes 6.
    * For any other texture target, TEXTURE_VIEW_NUM_LAYERS becomes 1.
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
490
    * ARB_texture_multisample: Multisample textures do
    * not have multiple image levels.
    */

   texObj->Immutable = GL_TRUE;
   texObj->ImmutableLevels = levels;
   texObj->MinLevel = 0;
   texObj->NumLevels = levels;
   texObj->MinLayer = 0;
   texObj->NumLayers = 1;
   switch (target) {
   case GL_TEXTURE_1D_ARRAY:
      texObj->NumLayers = texImage->Height;
      break;

   case GL_TEXTURE_2D_MULTISAMPLE:
      texObj->NumLevels = 1;
      texObj->ImmutableLevels = 1;
      break;

   case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
      texObj->NumLevels = 1;
      texObj->ImmutableLevels = 1;
      /* fall through to set NumLayers */

   case GL_TEXTURE_2D_ARRAY:
   case GL_TEXTURE_CUBE_MAP_ARRAY:
      texObj->NumLayers = texImage->Depth;
      break;

   case GL_TEXTURE_CUBE_MAP:
      texObj->NumLayers = 6;
      break;
   }
}
491

492
493
494
495
496
497
498
499
500
501
502
/**
 * glTextureView (ARB_texture_view)
 * If an error is found, record it with _mesa_error()
 * \return none.
 */
void GLAPIENTRY
_mesa_TextureView(GLuint texture, GLenum target, GLuint origtexture,
                  GLenum internalformat,
                  GLuint minlevel, GLuint numlevels,
                  GLuint minlayer, GLuint numlayers)
{
503
504
505
506
507
508
   struct gl_texture_object *texObj;
   struct gl_texture_object *origTexObj;
   struct gl_texture_image *origTexImage;
   GLuint newViewMinLevel, newViewMinLayer;
   GLuint newViewNumLevels, newViewNumLayers;
   GLsizei width, height, depth;
509
   mesa_format texFormat;
510
511
512
   GLboolean sizeOK, dimensionsOK;
   GLenum faceTarget;

513
514
515
   GET_CURRENT_CONTEXT(ctx);

   if (MESA_VERBOSE & (VERBOSE_API | VERBOSE_TEXTURE))
516
      _mesa_debug(ctx, "glTextureView %d %s %d %s %d %d %d %d\n",
517
518
                  texture, _mesa_enum_to_string(target), origtexture,
                  _mesa_enum_to_string(internalformat),
519
520
                  minlevel, numlevels, minlayer, numlayers);

521
   if (origtexture == 0) {
522
523
      _mesa_error(ctx, GL_INVALID_VALUE, "glTextureView(origtexture = %u)",
                  origtexture);
524
525
526
527
528
529
      return;
   }

   /* Need original texture information to validate arguments */
   origTexObj = _mesa_lookup_texture(ctx, origtexture);

530
531
532
   /* If <origtexture> is not the name of a texture, INVALID_VALUE
    * is generated.
    */
533
   if (!origTexObj) {
534
535
      _mesa_error(ctx, GL_INVALID_VALUE, "glTextureView(origtexture = %u)",
                  origtexture);
536
537
538
539
540
541
542
      return;
   }

   /* If <origtexture>'s TEXTURE_IMMUTABLE_FORMAT value is not TRUE,
    * INVALID_OPERATION is generated.
    */
   if (!origTexObj->Immutable) {
543
544
      _mesa_error(ctx, GL_INVALID_OPERATION,
                  "glTextureView(origtexture not immutable)");
545
546
547
548
549
550
551
552
553
554
555
556
557
558
      return;
   }

   /* If <texture> is 0, INVALID_VALUE is generated. */
   if (texture == 0) {
      _mesa_error(ctx, GL_INVALID_VALUE, "glTextureView(texture = 0)");
      return;
   }

   /* If <texture> is not a valid name returned by GenTextures,
    * the error INVALID_OPERATION is generated.
    */
   texObj = _mesa_lookup_texture(ctx, texture);
   if (texObj == NULL) {
559
560
      _mesa_error(ctx, GL_INVALID_OPERATION,
                  "glTextureView(texture = %u non-gen name)", texture);
561
562
563
564
565
566
567
      return;
   }

   /* If <texture> has already been bound and given a target, then
    * the error INVALID_OPERATION is generated.
    */
   if (texObj->Target) {
568
569
      _mesa_error(ctx, GL_INVALID_OPERATION,
                  "glTextureView(texture = %u already bound)", texture);
570
571
572
573
574
575
576
577
      return;
   }

   /* Check for compatible target */
   if (!target_valid(ctx, origTexObj->Target, target)) {
      return; /* error was recorded */
   }

578
   /* minlevel and minlayer are relative to the view of origtexture.
579
    * If minlevel or minlayer is greater than level or layer, respectively,
580
    * return INVALID_VALUE.
581
582
583
584
585
    */
   newViewMinLevel = origTexObj->MinLevel + minlevel;
   newViewMinLayer = origTexObj->MinLayer + minlayer;
   if (newViewMinLevel >= (origTexObj->MinLevel + origTexObj->NumLevels)) {
      _mesa_error(ctx, GL_INVALID_VALUE,
586
587
                  "glTextureView(new minlevel (%d) > orig minlevel (%d)"
                  " + orig numlevels (%d))",
588
589
590
591
592
593
                  newViewMinLevel, origTexObj->MinLevel, origTexObj->NumLevels);
      return;
   }

   if (newViewMinLayer >= (origTexObj->MinLayer + origTexObj->NumLayers)) {
      _mesa_error(ctx, GL_INVALID_VALUE,
594
595
                  "glTextureView(new minlayer (%d) > orig minlayer (%d)"
                  " + orig numlayers (%d))",
596
597
598
599
                  newViewMinLayer, origTexObj->MinLayer, origTexObj->NumLayers);
      return;
   }

600
   if (!_mesa_texture_view_compatible_format(ctx,
601
602
                                   origTexObj->Image[0][0]->InternalFormat,
                                   internalformat)) {
603
      _mesa_error(ctx, GL_INVALID_OPERATION,
604
          "glTextureView(internalformat %s not compatible with origtexture %s)",
605
606
          _mesa_enum_to_string(internalformat),
          _mesa_enum_to_string(origTexObj->Image[0][0]->InternalFormat));
607
      return;
608
609
610
611
612
613
614
615
616
617
   }

   texFormat = _mesa_choose_texture_format(ctx, texObj, target, 0,
                                           internalformat, GL_NONE, GL_NONE);
   assert(texFormat != MESA_FORMAT_NONE);
   if (texFormat == MESA_FORMAT_NONE) return;

   newViewNumLevels = MIN2(numlevels, origTexObj->NumLevels - minlevel);
   newViewNumLayers = MIN2(numlayers, origTexObj->NumLayers - minlayer);

618
   faceTarget = _mesa_cube_face_target(origTexObj->Target, minlayer);
619
620

   /* Get a reference to what will become this View's base level */
621
   origTexImage = _mesa_select_tex_image(origTexObj, faceTarget, minlevel);
622
623
624
625
626
627
628
   width = origTexImage->Width;
   height = origTexImage->Height;
   depth = origTexImage->Depth;

   /* Adjust width, height, depth to be appropriate for new target */
   switch (target) {
   case GL_TEXTURE_1D:
629
630
631
      height = 1;
      break;

632
633
634
635
636
637
638
639
640
641
   case GL_TEXTURE_3D:
      break;

   case GL_TEXTURE_1D_ARRAY:
      height = (GLsizei) newViewNumLayers;
      break;

   case GL_TEXTURE_2D:
   case GL_TEXTURE_2D_MULTISAMPLE:
   case GL_TEXTURE_RECTANGLE:
642
643
      depth = 1;
      break;
644
   case GL_TEXTURE_CUBE_MAP:
645
646
647
648
649
650
651
652
653
      /* If the new texture's target is TEXTURE_CUBE_MAP, the clamped
       * <numlayers> must be equal to 6.
       */
      if (newViewNumLayers != 6) {
         _mesa_error(ctx, GL_INVALID_VALUE,
                     "glTextureView(clamped numlayers %d != 6)",
                     newViewNumLayers);
         return;
      }
654
655
656
657
658
659
660
      depth = 1;
      break;

   case GL_TEXTURE_2D_ARRAY:
   case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
      depth = newViewNumLayers;
      break;
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
   case GL_TEXTURE_CUBE_MAP_ARRAY:
      /* If the new texture's target is TEXTURE_CUBE_MAP_ARRAY,
       * then <numlayers> counts layer-faces rather than layers,
       * and the clamped <numlayers> must be a multiple of 6.
       * Otherwise, the error INVALID_VALUE is generated.
       */
      if ((newViewNumLayers % 6) != 0) {
         _mesa_error(ctx, GL_INVALID_VALUE,
                     "glTextureView(clamped numlayers %d is not"
                     " a multiple of 6)",
                     newViewNumLayers);
         return;
      }
      depth = newViewNumLayers;
      break;
676
677
678
679
680
681
682
683
684
685
686
687
   }

   /* If the dimensions of the original texture are larger than the maximum
    * supported dimensions of the new target, the error INVALID_OPERATION is
    * generated. For example, if the original texture has a TEXTURE_2D_ARRAY
    * target and its width is greater than MAX_CUBE_MAP_TEXTURE_SIZE, an error
    * will be generated if TextureView is called to create a TEXTURE_CUBE_MAP
    * view.
    */
   dimensionsOK = _mesa_legal_texture_dimensions(ctx, target, 0,
                                                 width, height, depth, 0);
   if (!dimensionsOK) {
688
689
      _mesa_error(ctx, GL_INVALID_OPERATION,
                  "glTextureView(invalid width or height or depth)");
690
691
692
693
694
695
      return;
   }

   sizeOK = ctx->Driver.TestProxyTexImage(ctx, target, 0, texFormat,
                                          width, height, depth, 0);
   if (!sizeOK) {
696
697
      _mesa_error(ctx, GL_INVALID_OPERATION,
                  "glTextureView(invalid texture size)");
698
699
700
701
702
703
704
705
706
707
708
709
710
711
      return;
   }

   /* If <target> is TEXTURE_1D, TEXTURE_2D, TEXTURE_3D, TEXTURE_RECTANGLE,
    * or TEXTURE_2D_MULTISAMPLE and <numlayers> does not equal 1, the error
    * INVALID_VALUE is generated.
    */
   switch (target) {
   case GL_TEXTURE_1D:
   case GL_TEXTURE_2D:
   case GL_TEXTURE_3D:
   case GL_TEXTURE_RECTANGLE:
   case GL_TEXTURE_2D_MULTISAMPLE:
      if (numlayers != 1) {
712
713
         _mesa_error(ctx, GL_INVALID_VALUE, "glTextureView(numlayers %d != 1)",
                     numlayers);
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
         return;
      }
      break;
   case GL_TEXTURE_CUBE_MAP:
      break;
   case GL_TEXTURE_CUBE_MAP_ARRAY:
      break;
   }

   /* If the new texture's target is TEXTURE_CUBE_MAP or
    * TEXTURE_CUBE_MAP_ARRAY, the width and height of the original texture's
    * levels must be equal otherwise the error INVALID_OPERATION is generated.
    */
   if ((target == GL_TEXTURE_CUBE_MAP || target == GL_TEXTURE_CUBE_MAP_ARRAY) &&
       (origTexImage->Width != origTexImage->Height)) {
729
730
      _mesa_error(ctx, GL_INVALID_OPERATION,
                  "glTextureView(origtexture width (%d) != height (%d))",
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
                  origTexImage->Width, origTexImage->Height);
      return;
   }

   /* When the original texture's target is TEXTURE_CUBE_MAP, the layer
    * parameters are interpreted in the same order as if it were a
    * TEXTURE_CUBE_MAP_ARRAY with 6 layer-faces.
    */

   /* If the internal format does not exactly match the internal format of the
    * original texture, the contents of the memory are reinterpreted in the
    * same manner as for image bindings described in
    * section 3.9.20 (Texture Image Loads and Stores).
    */

   /* TEXTURE_BASE_LEVEL and TEXTURE_MAX_LEVEL are interpreted
    * relative to the view and not relative to the original data store.
    */

   if (!initialize_texture_fields(ctx, target, texObj, newViewNumLevels,
                                  width, height, depth,
                                  internalformat, texFormat)) {
      return; /* Already recorded error */
   }

   texObj->MinLevel = newViewMinLevel;
   texObj->MinLayer = newViewMinLayer;
   texObj->NumLevels = newViewNumLevels;
   texObj->NumLayers = newViewNumLayers;
   texObj->Immutable = GL_TRUE;
   texObj->ImmutableLevels = origTexObj->ImmutableLevels;
   texObj->Target = target;
763
764
   texObj->TargetIndex = _mesa_tex_target_to_index(ctx, target);
   assert(texObj->TargetIndex < NUM_TEXTURE_TARGETS);
765

766
767
   if (ctx->Driver.TextureView != NULL &&
       !ctx->Driver.TextureView(ctx, texObj, origTexObj)) {
768
769
      return; /* driver recorded error */
   }
770
}