316d8280338e0ba6188d790ff74cc366f972130c
[mesa.git] / src / mesa / main / textureview.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2013 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Courtney Goeltzenleuchter <courtney@lunarg.com>
26 */
27
28
29 /**
30 * \file textureview.c
31 * GL_ARB_texture_view functions
32 */
33
34 #include "glheader.h"
35 #include "context.h"
36 #include "enums.h"
37 #include "imports.h"
38 #include "macros.h"
39 #include "teximage.h"
40 #include "texobj.h"
41 #include "mipmap.h"
42 #include "texstorage.h"
43 #include "textureview.h"
44 #include "stdbool.h"
45 #include "mtypes.h"
46
47 /* Table 3.X.2 (Compatible internal formats for TextureView)
48 ---------------------------------------------------------------------------
49 | Class | Internal formats |
50 ---------------------------------------------------------------------------
51 | VIEW_CLASS_128_BITS | RGBA32F, RGBA32UI, RGBA32I |
52 ---------------------------------------------------------------------------
53 | VIEW_CLASS_96_BITS | RGB32F, RGB32UI, RGB32I |
54 ---------------------------------------------------------------------------
55 | VIEW_CLASS_64_BITS | RGBA16F, RG32F, RGBA16UI, RG32UI, RGBA16I, |
56 | | RG32I, RGBA16, RGBA16_SNORM |
57 ---------------------------------------------------------------------------
58 | VIEW_CLASS_48_BITS | RGB16, RGB16_SNORM, RGB16F, RGB16UI, RGB16I |
59 ---------------------------------------------------------------------------
60 | VIEW_CLASS_32_BITS | RG16F, R11F_G11F_B10F, R32F, |
61 | | RGB10_A2UI, RGBA8UI, RG16UI, R32UI, |
62 | | RGBA8I, RG16I, R32I, RGB10_A2, RGBA8, RG16, |
63 | | RGBA8_SNORM, RG16_SNORM, SRGB8_ALPHA8, RGB9_E5 |
64 ---------------------------------------------------------------------------
65 | VIEW_CLASS_24_BITS | RGB8, RGB8_SNORM, SRGB8, RGB8UI, RGB8I |
66 ---------------------------------------------------------------------------
67 | VIEW_CLASS_16_BITS | R16F, RG8UI, R16UI, RG8I, R16I, RG8, R16, |
68 | | RG8_SNORM, R16_SNORM |
69 ---------------------------------------------------------------------------
70 | VIEW_CLASS_8_BITS | R8UI, R8I, R8, R8_SNORM |
71 ---------------------------------------------------------------------------
72 | VIEW_CLASS_RGTC1_RED | COMPRESSED_RED_RGTC1, |
73 | | COMPRESSED_SIGNED_RED_RGTC1 |
74 ---------------------------------------------------------------------------
75 | VIEW_CLASS_RGTC2_RG | COMPRESSED_RG_RGTC2, |
76 | | COMPRESSED_SIGNED_RG_RGTC2 |
77 ---------------------------------------------------------------------------
78 | VIEW_CLASS_BPTC_UNORM | COMPRESSED_RGBA_BPTC_UNORM, |
79 | | COMPRESSED_SRGB_ALPHA_BPTC_UNORM |
80 ---------------------------------------------------------------------------
81 | VIEW_CLASS_BPTC_FLOAT | COMPRESSED_RGB_BPTC_SIGNED_FLOAT, |
82 | | COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT |
83 ---------------------------------------------------------------------------
84 */
85 struct internal_format_class_info {
86 GLenum view_class;
87 GLenum internal_format;
88 };
89 static const struct internal_format_class_info compatible_internal_formats[] = {
90 {GL_VIEW_CLASS_128_BITS, GL_RGBA32F},
91 {GL_VIEW_CLASS_128_BITS, GL_RGBA32UI},
92 {GL_VIEW_CLASS_128_BITS, GL_RGBA32I},
93 {GL_VIEW_CLASS_96_BITS, GL_RGB32F},
94 {GL_VIEW_CLASS_96_BITS, GL_RGB32UI},
95 {GL_VIEW_CLASS_96_BITS, GL_RGB32I},
96 {GL_VIEW_CLASS_64_BITS, GL_RGBA16F},
97 {GL_VIEW_CLASS_64_BITS, GL_RG32F},
98 {GL_VIEW_CLASS_64_BITS, GL_RGBA16UI},
99 {GL_VIEW_CLASS_64_BITS, GL_RG32UI},
100 {GL_VIEW_CLASS_64_BITS, GL_RGBA16I},
101 {GL_VIEW_CLASS_64_BITS, GL_RG32I},
102 {GL_VIEW_CLASS_64_BITS, GL_RGBA16},
103 {GL_VIEW_CLASS_64_BITS, GL_RGBA16_SNORM},
104 {GL_VIEW_CLASS_48_BITS, GL_RGB16},
105 {GL_VIEW_CLASS_48_BITS, GL_RGB16_SNORM},
106 {GL_VIEW_CLASS_48_BITS, GL_RGB16F},
107 {GL_VIEW_CLASS_48_BITS, GL_RGB16UI},
108 {GL_VIEW_CLASS_48_BITS, GL_RGB16I},
109 {GL_VIEW_CLASS_32_BITS, GL_RG16F},
110 {GL_VIEW_CLASS_32_BITS, GL_R11F_G11F_B10F},
111 {GL_VIEW_CLASS_32_BITS, GL_R32F},
112 {GL_VIEW_CLASS_32_BITS, GL_RGB10_A2UI},
113 {GL_VIEW_CLASS_32_BITS, GL_RGBA8UI},
114 {GL_VIEW_CLASS_32_BITS, GL_RG16UI},
115 {GL_VIEW_CLASS_32_BITS, GL_R32UI},
116 {GL_VIEW_CLASS_32_BITS, GL_RGBA8I},
117 {GL_VIEW_CLASS_32_BITS, GL_RG16I},
118 {GL_VIEW_CLASS_32_BITS, GL_R32I},
119 {GL_VIEW_CLASS_32_BITS, GL_RGB10_A2},
120 {GL_VIEW_CLASS_32_BITS, GL_RGBA8},
121 {GL_VIEW_CLASS_32_BITS, GL_RG16},
122 {GL_VIEW_CLASS_32_BITS, GL_RGBA8_SNORM},
123 {GL_VIEW_CLASS_32_BITS, GL_RG16_SNORM},
124 {GL_VIEW_CLASS_32_BITS, GL_SRGB8_ALPHA8},
125 {GL_VIEW_CLASS_32_BITS, GL_RGB9_E5},
126 {GL_VIEW_CLASS_24_BITS, GL_RGB8},
127 {GL_VIEW_CLASS_24_BITS, GL_RGB8_SNORM},
128 {GL_VIEW_CLASS_24_BITS, GL_SRGB8},
129 {GL_VIEW_CLASS_24_BITS, GL_RGB8UI},
130 {GL_VIEW_CLASS_24_BITS, GL_RGB8I},
131 {GL_VIEW_CLASS_16_BITS, GL_R16F},
132 {GL_VIEW_CLASS_16_BITS, GL_RG8UI},
133 {GL_VIEW_CLASS_16_BITS, GL_R16UI},
134 {GL_VIEW_CLASS_16_BITS, GL_RG8I},
135 {GL_VIEW_CLASS_16_BITS, GL_R16I},
136 {GL_VIEW_CLASS_16_BITS, GL_RG8},
137 {GL_VIEW_CLASS_16_BITS, GL_R16},
138 {GL_VIEW_CLASS_16_BITS, GL_RG8_SNORM},
139 {GL_VIEW_CLASS_16_BITS, GL_R16_SNORM},
140 {GL_VIEW_CLASS_8_BITS, GL_R8UI},
141 {GL_VIEW_CLASS_8_BITS, GL_R8I},
142 {GL_VIEW_CLASS_8_BITS, GL_R8},
143 {GL_VIEW_CLASS_8_BITS, GL_R8_SNORM},
144 {GL_VIEW_CLASS_RGTC1_RED, GL_COMPRESSED_RED_RGTC1},
145 {GL_VIEW_CLASS_RGTC1_RED, GL_COMPRESSED_SIGNED_RED_RGTC1},
146 {GL_VIEW_CLASS_RGTC2_RG, GL_COMPRESSED_RG_RGTC2},
147 {GL_VIEW_CLASS_RGTC2_RG, GL_COMPRESSED_SIGNED_RG_RGTC2},
148 {GL_VIEW_CLASS_BPTC_UNORM, GL_COMPRESSED_RGBA_BPTC_UNORM_ARB},
149 {GL_VIEW_CLASS_BPTC_UNORM, GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB},
150 {GL_VIEW_CLASS_BPTC_FLOAT, GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB},
151 {GL_VIEW_CLASS_BPTC_FLOAT, GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB},
152 };
153
154 static const struct internal_format_class_info s3tc_compatible_internal_formats[] = {
155 {GL_VIEW_CLASS_S3TC_DXT1_RGB, GL_COMPRESSED_RGB_S3TC_DXT1_EXT},
156 {GL_VIEW_CLASS_S3TC_DXT1_RGB, GL_COMPRESSED_SRGB_S3TC_DXT1_EXT},
157 {GL_VIEW_CLASS_S3TC_DXT1_RGBA, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT},
158 {GL_VIEW_CLASS_S3TC_DXT1_RGBA, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT},
159 {GL_VIEW_CLASS_S3TC_DXT3_RGBA, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT},
160 {GL_VIEW_CLASS_S3TC_DXT3_RGBA, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT},
161 {GL_VIEW_CLASS_S3TC_DXT5_RGBA, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT},
162 {GL_VIEW_CLASS_S3TC_DXT5_RGBA, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT},
163 };
164
165 /**
166 * Lookup format view class based on internalformat
167 * \return VIEW_CLASS if internalformat found in table, false otherwise.
168 */
169 static GLenum
170 lookup_view_class(const struct gl_context *ctx, GLenum internalformat)
171 {
172 GLuint i;
173
174 for (i = 0; i < ARRAY_SIZE(compatible_internal_formats); i++) {
175 if (compatible_internal_formats[i].internal_format == internalformat)
176 return compatible_internal_formats[i].view_class;
177 }
178
179 if (ctx->Extensions.EXT_texture_compression_s3tc &&
180 ctx->Extensions.EXT_texture_sRGB) {
181 for (i = 0; i < ARRAY_SIZE(s3tc_compatible_internal_formats); i++) {
182 if (s3tc_compatible_internal_formats[i].internal_format
183 == internalformat)
184 return s3tc_compatible_internal_formats[i].view_class;
185 }
186 }
187 return GL_FALSE;
188 }
189
190 /**
191 * Initialize new texture's gl_texture_image structures. Will not call driver
192 * to allocate new space, simply record relevant layer, face, format, etc.
193 * \return GL_FALSE if any error, GL_TRUE otherwise.
194 */
195 static GLboolean
196 initialize_texture_fields(struct gl_context *ctx,
197 GLenum target,
198 struct gl_texture_object *texObj,
199 GLint levels,
200 GLsizei width, GLsizei height, GLsizei depth,
201 GLenum internalFormat, mesa_format texFormat)
202 {
203 const GLuint numFaces = _mesa_num_tex_faces(target);
204 GLint level, levelWidth = width, levelHeight = height, levelDepth = depth;
205 GLuint face;
206
207 /* Pretend we are bound to initialize the gl_texture_image structs */
208 texObj->Target = target;
209
210 /* Set up all the texture object's gl_texture_images */
211 for (level = 0; level < levels; level++) {
212 for (face = 0; face < numFaces; face++) {
213 struct gl_texture_image *texImage;
214 const GLenum faceTarget = _mesa_cube_face_target(target, face);
215
216 texImage = _mesa_get_tex_image(ctx, texObj, faceTarget, level);
217
218 if (!texImage) {
219 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexStorage");
220 return GL_FALSE;
221 }
222
223 _mesa_init_teximage_fields(ctx, texImage,
224 levelWidth, levelHeight, levelDepth,
225 0, internalFormat, texFormat);
226 }
227
228 _mesa_next_mipmap_level_size(target, 0,
229 levelWidth, levelHeight, levelDepth,
230 &levelWidth, &levelHeight, &levelDepth);
231 }
232
233 /* "unbind" */
234 texObj->Target = 0;
235
236 return GL_TRUE;
237 }
238
239 #define RETURN_IF_SUPPORTED(t) do { \
240 if (newTarget == GL_ ## t) \
241 return true; \
242 } while (0)
243
244 /**
245 * Check for compatible target
246 * If an error is found, record it with _mesa_error()
247 * \return false if any error, true otherwise.
248 */
249 static bool
250 target_valid(struct gl_context *ctx, GLenum origTarget, GLenum newTarget)
251 {
252 /*
253 * From ARB_texture_view spec:
254 ---------------------------------------------------------------------------------------------------------
255 | Original target | Valid new targets |
256 ---------------------------------------------------------------------------------------------------------
257 | TEXTURE_1D | TEXTURE_1D, TEXTURE_1D_ARRAY |
258 | ------------------------------------------------------------------------------------------------------- |
259 | TEXTURE_2D | TEXTURE_2D, TEXTURE_2D_ARRAY |
260 | ------------------------------------------------------------------------------------------------------- |
261 | TEXTURE_3D | TEXTURE_3D |
262 | ------------------------------------------------------------------------------------------------------- |
263 | TEXTURE_CUBE_MAP | TEXTURE_CUBE_MAP, TEXTURE_2D, TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP_ARRAY |
264 | ------------------------------------------------------------------------------------------------------- |
265 | TEXTURE_RECTANGLE | TEXTURE_RECTANGLE |
266 | ------------------------------------------------------------------------------------------------------- |
267 | TEXTURE_BUFFER | <none> |
268 | ------------------------------------------------------------------------------------------------------- |
269 | TEXTURE_1D_ARRAY | TEXTURE_1D_ARRAY, TEXTURE_1D |
270 | ------------------------------------------------------------------------------------------------------- |
271 | TEXTURE_2D_ARRAY | TEXTURE_2D_ARRAY, TEXTURE_2D, TEXTURE_CUBE_MAP, TEXTURE_CUBE_MAP_ARRAY |
272 | ------------------------------------------------------------------------------------------------------- |
273 | TEXTURE_CUBE_MAP_ARRAY | TEXTURE_CUBE_MAP_ARRAY, TEXTURE_2D_ARRAY, TEXTURE_2D, TEXTURE_CUBE_MAP |
274 | ------------------------------------------------------------------------------------------------------- |
275 | TEXTURE_2D_MULTISAMPLE | TEXTURE_2D_MULTISAMPLE, TEXTURE_2D_MULTISAMPLE_ARRAY |
276 | ------------------------------------------------------------------------------------------------------- |
277 | TEXTURE_2D_MULTISAMPLE_ARRAY | TEXTURE_2D_MULTISAMPLE, TEXTURE_2D_MULTISAMPLE_ARRAY |
278 ---------------------------------------------------------------------------------------------------------
279 */
280
281 switch (origTarget) {
282 case GL_TEXTURE_1D:
283 case GL_TEXTURE_1D_ARRAY:
284 RETURN_IF_SUPPORTED(TEXTURE_1D);
285 RETURN_IF_SUPPORTED(TEXTURE_1D_ARRAY);
286 break;
287 case GL_TEXTURE_2D:
288 RETURN_IF_SUPPORTED(TEXTURE_2D);
289 RETURN_IF_SUPPORTED(TEXTURE_2D_ARRAY);
290 break;
291 case GL_TEXTURE_3D:
292 RETURN_IF_SUPPORTED(TEXTURE_3D);
293 break;
294 case GL_TEXTURE_RECTANGLE:
295 RETURN_IF_SUPPORTED(TEXTURE_RECTANGLE);
296 break;
297 case GL_TEXTURE_CUBE_MAP:
298 case GL_TEXTURE_2D_ARRAY:
299 case GL_TEXTURE_CUBE_MAP_ARRAY:
300 RETURN_IF_SUPPORTED(TEXTURE_2D);
301 RETURN_IF_SUPPORTED(TEXTURE_2D_ARRAY);
302 RETURN_IF_SUPPORTED(TEXTURE_CUBE_MAP);
303 RETURN_IF_SUPPORTED(TEXTURE_CUBE_MAP_ARRAY);
304 break;
305 case GL_TEXTURE_2D_MULTISAMPLE:
306 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
307 RETURN_IF_SUPPORTED(TEXTURE_2D_MULTISAMPLE);
308 RETURN_IF_SUPPORTED(TEXTURE_2D_MULTISAMPLE_ARRAY);
309 break;
310 }
311 _mesa_error(ctx, GL_INVALID_OPERATION,
312 "glTextureView(illegal target=%s)",
313 _mesa_enum_to_string(newTarget));
314 return false;
315 }
316 #undef RETURN_IF_SUPPORTED
317
318 /**
319 * Check for compatible format
320 * If an error is found, record it with _mesa_error()
321 * \return false if any error, true otherwise.
322 */
323 bool
324 _mesa_texture_view_compatible_format(const struct gl_context *ctx,
325 GLenum origInternalFormat,
326 GLenum newInternalFormat)
327 {
328 unsigned int origViewClass, newViewClass;
329
330 /* The two textures' internal formats must be compatible according to
331 * Table 3.X.2 (Compatible internal formats for TextureView)
332 * if the internal format exists in that table the view class must match.
333 * The internal formats must be identical if not in that table,
334 * or an INVALID_OPERATION error is generated.
335 */
336 if (origInternalFormat == newInternalFormat)
337 return true;
338
339 origViewClass = lookup_view_class(ctx, origInternalFormat);
340 newViewClass = lookup_view_class(ctx, newInternalFormat);
341 if ((origViewClass == newViewClass) && origViewClass != false)
342 return true;
343
344 return false;
345 }
346
347 /**
348 * Helper function for TexStorage and teximagemultisample to set immutable
349 * texture state needed by ARB_texture_view.
350 */
351 void
352 _mesa_set_texture_view_state(struct gl_context *ctx,
353 struct gl_texture_object *texObj,
354 GLenum target, GLuint levels)
355 {
356 struct gl_texture_image *texImage;
357
358 /* Get a reference to what will become this View's base level */
359 texImage = _mesa_select_tex_image(texObj, target, 0);
360
361 /* When an immutable texture is created via glTexStorage or
362 * glTexImageMultisample,
363 * TEXTURE_IMMUTABLE_FORMAT becomes TRUE.
364 * TEXTURE_IMMUTABLE_LEVELS and TEXTURE_VIEW_NUM_LEVELS become levels.
365 * If the texture target is TEXTURE_1D_ARRAY then
366 * TEXTURE_VIEW_NUM_LAYERS becomes height.
367 * If the texture target is TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP_ARRAY,
368 * or TEXTURE_2D_MULTISAMPLE_ARRAY then TEXTURE_VIEW_NUM_LAYERS becomes
369 * depth.
370 * If the texture target is TEXTURE_CUBE_MAP, then
371 * TEXTURE_VIEW_NUM_LAYERS becomes 6.
372 * For any other texture target, TEXTURE_VIEW_NUM_LAYERS becomes 1.
373 *
374 * ARB_texture_multisample: Multisample textures do
375 * not have multiple image levels.
376 */
377
378 texObj->Immutable = GL_TRUE;
379 texObj->ImmutableLevels = levels;
380 texObj->MinLevel = 0;
381 texObj->NumLevels = levels;
382 texObj->MinLayer = 0;
383 texObj->NumLayers = 1;
384 switch (target) {
385 case GL_TEXTURE_1D_ARRAY:
386 texObj->NumLayers = texImage->Height;
387 break;
388
389 case GL_TEXTURE_2D_MULTISAMPLE:
390 texObj->NumLevels = 1;
391 texObj->ImmutableLevels = 1;
392 break;
393
394 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
395 texObj->NumLevels = 1;
396 texObj->ImmutableLevels = 1;
397 /* fall through to set NumLayers */
398
399 case GL_TEXTURE_2D_ARRAY:
400 case GL_TEXTURE_CUBE_MAP_ARRAY:
401 texObj->NumLayers = texImage->Depth;
402 break;
403
404 case GL_TEXTURE_CUBE_MAP:
405 texObj->NumLayers = 6;
406 break;
407 }
408 }
409
410 /**
411 * glTextureView (ARB_texture_view)
412 * If an error is found, record it with _mesa_error()
413 * \return none.
414 */
415 void GLAPIENTRY
416 _mesa_TextureView(GLuint texture, GLenum target, GLuint origtexture,
417 GLenum internalformat,
418 GLuint minlevel, GLuint numlevels,
419 GLuint minlayer, GLuint numlayers)
420 {
421 struct gl_texture_object *texObj;
422 struct gl_texture_object *origTexObj;
423 struct gl_texture_image *origTexImage;
424 GLuint newViewMinLevel, newViewMinLayer;
425 GLuint newViewNumLevels, newViewNumLayers;
426 GLsizei width, height, depth;
427 mesa_format texFormat;
428 GLboolean sizeOK, dimensionsOK;
429 GLenum faceTarget;
430
431 GET_CURRENT_CONTEXT(ctx);
432
433 if (MESA_VERBOSE & (VERBOSE_API | VERBOSE_TEXTURE))
434 _mesa_debug(ctx, "glTextureView %d %s %d %s %d %d %d %d\n",
435 texture, _mesa_enum_to_string(target), origtexture,
436 _mesa_enum_to_string(internalformat),
437 minlevel, numlevels, minlayer, numlayers);
438
439 if (origtexture == 0) {
440 _mesa_error(ctx, GL_INVALID_VALUE, "glTextureView(origtexture = %u)",
441 origtexture);
442 return;
443 }
444
445 /* Need original texture information to validate arguments */
446 origTexObj = _mesa_lookup_texture(ctx, origtexture);
447
448 /* If <origtexture> is not the name of a texture, INVALID_VALUE
449 * is generated.
450 */
451 if (!origTexObj) {
452 _mesa_error(ctx, GL_INVALID_VALUE, "glTextureView(origtexture = %u)",
453 origtexture);
454 return;
455 }
456
457 /* If <origtexture>'s TEXTURE_IMMUTABLE_FORMAT value is not TRUE,
458 * INVALID_OPERATION is generated.
459 */
460 if (!origTexObj->Immutable) {
461 _mesa_error(ctx, GL_INVALID_OPERATION,
462 "glTextureView(origtexture not immutable)");
463 return;
464 }
465
466 /* If <texture> is 0, INVALID_VALUE is generated. */
467 if (texture == 0) {
468 _mesa_error(ctx, GL_INVALID_VALUE, "glTextureView(texture = 0)");
469 return;
470 }
471
472 /* If <texture> is not a valid name returned by GenTextures,
473 * the error INVALID_OPERATION is generated.
474 */
475 texObj = _mesa_lookup_texture(ctx, texture);
476 if (texObj == NULL) {
477 _mesa_error(ctx, GL_INVALID_OPERATION,
478 "glTextureView(texture = %u non-gen name)", texture);
479 return;
480 }
481
482 /* If <texture> has already been bound and given a target, then
483 * the error INVALID_OPERATION is generated.
484 */
485 if (texObj->Target) {
486 _mesa_error(ctx, GL_INVALID_OPERATION,
487 "glTextureView(texture = %u already bound)", texture);
488 return;
489 }
490
491 /* Check for compatible target */
492 if (!target_valid(ctx, origTexObj->Target, target)) {
493 return; /* error was recorded */
494 }
495
496 /* minlevel and minlayer are relative to the view of origtexture.
497 * If minlevel or minlayer is greater than level or layer, respectively,
498 * return INVALID_VALUE.
499 */
500 newViewMinLevel = origTexObj->MinLevel + minlevel;
501 newViewMinLayer = origTexObj->MinLayer + minlayer;
502 if (newViewMinLevel >= (origTexObj->MinLevel + origTexObj->NumLevels)) {
503 _mesa_error(ctx, GL_INVALID_VALUE,
504 "glTextureView(new minlevel (%d) > orig minlevel (%d)"
505 " + orig numlevels (%d))",
506 newViewMinLevel, origTexObj->MinLevel, origTexObj->NumLevels);
507 return;
508 }
509
510 if (newViewMinLayer >= (origTexObj->MinLayer + origTexObj->NumLayers)) {
511 _mesa_error(ctx, GL_INVALID_VALUE,
512 "glTextureView(new minlayer (%d) > orig minlayer (%d)"
513 " + orig numlayers (%d))",
514 newViewMinLayer, origTexObj->MinLayer, origTexObj->NumLayers);
515 return;
516 }
517
518 if (!_mesa_texture_view_compatible_format(ctx,
519 origTexObj->Image[0][0]->InternalFormat,
520 internalformat)) {
521 _mesa_error(ctx, GL_INVALID_OPERATION,
522 "glTextureView(internalformat %s not compatible with origtexture %s)",
523 _mesa_enum_to_string(internalformat),
524 _mesa_enum_to_string(origTexObj->Image[0][0]->InternalFormat));
525 return;
526 }
527
528 texFormat = _mesa_choose_texture_format(ctx, texObj, target, 0,
529 internalformat, GL_NONE, GL_NONE);
530 assert(texFormat != MESA_FORMAT_NONE);
531 if (texFormat == MESA_FORMAT_NONE) return;
532
533 newViewNumLevels = MIN2(numlevels, origTexObj->NumLevels - minlevel);
534 newViewNumLayers = MIN2(numlayers, origTexObj->NumLayers - minlayer);
535
536 faceTarget = _mesa_cube_face_target(origTexObj->Target, minlayer);
537
538 /* Get a reference to what will become this View's base level */
539 origTexImage = _mesa_select_tex_image(origTexObj, faceTarget, minlevel);
540 width = origTexImage->Width;
541 height = origTexImage->Height;
542 depth = origTexImage->Depth;
543
544 /* Adjust width, height, depth to be appropriate for new target */
545 switch (target) {
546 case GL_TEXTURE_1D:
547 height = 1;
548 break;
549
550 case GL_TEXTURE_3D:
551 break;
552
553 case GL_TEXTURE_1D_ARRAY:
554 height = (GLsizei) newViewNumLayers;
555 break;
556
557 case GL_TEXTURE_2D:
558 case GL_TEXTURE_2D_MULTISAMPLE:
559 case GL_TEXTURE_RECTANGLE:
560 case GL_TEXTURE_CUBE_MAP:
561 depth = 1;
562 break;
563
564 case GL_TEXTURE_2D_ARRAY:
565 case GL_TEXTURE_CUBE_MAP_ARRAY:
566 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
567 depth = newViewNumLayers;
568 break;
569 }
570
571 /* If the dimensions of the original texture are larger than the maximum
572 * supported dimensions of the new target, the error INVALID_OPERATION is
573 * generated. For example, if the original texture has a TEXTURE_2D_ARRAY
574 * target and its width is greater than MAX_CUBE_MAP_TEXTURE_SIZE, an error
575 * will be generated if TextureView is called to create a TEXTURE_CUBE_MAP
576 * view.
577 */
578 dimensionsOK = _mesa_legal_texture_dimensions(ctx, target, 0,
579 width, height, depth, 0);
580 if (!dimensionsOK) {
581 _mesa_error(ctx, GL_INVALID_OPERATION,
582 "glTextureView(invalid width or height or depth)");
583 return;
584 }
585
586 sizeOK = ctx->Driver.TestProxyTexImage(ctx, target, 0, texFormat,
587 width, height, depth, 0);
588 if (!sizeOK) {
589 _mesa_error(ctx, GL_INVALID_OPERATION,
590 "glTextureView(invalid texture size)");
591 return;
592 }
593
594 /* If <target> is TEXTURE_1D, TEXTURE_2D, TEXTURE_3D, TEXTURE_RECTANGLE,
595 * or TEXTURE_2D_MULTISAMPLE and <numlayers> does not equal 1, the error
596 * INVALID_VALUE is generated.
597 */
598 switch (target) {
599 case GL_TEXTURE_1D:
600 case GL_TEXTURE_2D:
601 case GL_TEXTURE_3D:
602 case GL_TEXTURE_RECTANGLE:
603 case GL_TEXTURE_2D_MULTISAMPLE:
604 if (numlayers != 1) {
605 _mesa_error(ctx, GL_INVALID_VALUE, "glTextureView(numlayers %d != 1)",
606 numlayers);
607 return;
608 }
609 break;
610
611 case GL_TEXTURE_CUBE_MAP:
612 /* If the new texture's target is TEXTURE_CUBE_MAP, the clamped
613 * <numlayers> must be equal to 6.
614 */
615 if (newViewNumLayers != 6) {
616 _mesa_error(ctx, GL_INVALID_VALUE,
617 "glTextureView(clamped numlayers %d != 6)",
618 newViewNumLayers);
619 return;
620 }
621 break;
622
623 case GL_TEXTURE_CUBE_MAP_ARRAY:
624 /* If the new texture's target is TEXTURE_CUBE_MAP_ARRAY,
625 * then <numlayers> counts layer-faces rather than layers,
626 * and the clamped <numlayers> must be a multiple of 6.
627 * Otherwise, the error INVALID_VALUE is generated.
628 */
629 if ((newViewNumLayers % 6) != 0) {
630 _mesa_error(ctx, GL_INVALID_VALUE,
631 "glTextureView(clamped numlayers %d is not"
632 " a multiple of 6)",
633 newViewNumLayers);
634 return;
635 }
636 break;
637 }
638
639 /* If the new texture's target is TEXTURE_CUBE_MAP or
640 * TEXTURE_CUBE_MAP_ARRAY, the width and height of the original texture's
641 * levels must be equal otherwise the error INVALID_OPERATION is generated.
642 */
643 if ((target == GL_TEXTURE_CUBE_MAP || target == GL_TEXTURE_CUBE_MAP_ARRAY) &&
644 (origTexImage->Width != origTexImage->Height)) {
645 _mesa_error(ctx, GL_INVALID_OPERATION,
646 "glTextureView(origtexture width (%d) != height (%d))",
647 origTexImage->Width, origTexImage->Height);
648 return;
649 }
650
651 /* When the original texture's target is TEXTURE_CUBE_MAP, the layer
652 * parameters are interpreted in the same order as if it were a
653 * TEXTURE_CUBE_MAP_ARRAY with 6 layer-faces.
654 */
655
656 /* If the internal format does not exactly match the internal format of the
657 * original texture, the contents of the memory are reinterpreted in the
658 * same manner as for image bindings described in
659 * section 3.9.20 (Texture Image Loads and Stores).
660 */
661
662 /* TEXTURE_BASE_LEVEL and TEXTURE_MAX_LEVEL are interpreted
663 * relative to the view and not relative to the original data store.
664 */
665
666 if (!initialize_texture_fields(ctx, target, texObj, newViewNumLevels,
667 width, height, depth,
668 internalformat, texFormat)) {
669 return; /* Already recorded error */
670 }
671
672 texObj->MinLevel = newViewMinLevel;
673 texObj->MinLayer = newViewMinLayer;
674 texObj->NumLevels = newViewNumLevels;
675 texObj->NumLayers = newViewNumLayers;
676 texObj->Immutable = GL_TRUE;
677 texObj->ImmutableLevels = origTexObj->ImmutableLevels;
678 texObj->Target = target;
679 texObj->TargetIndex = _mesa_tex_target_to_index(ctx, target);
680 assert(texObj->TargetIndex < NUM_TEXTURE_TARGETS);
681
682 if (ctx->Driver.TextureView != NULL &&
683 !ctx->Driver.TextureView(ctx, texObj, origTexObj)) {
684 return; /* driver recorded error */
685 }
686 }