mesa: code clean-ups in textureview.[ch]
[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 GLenum faceTarget = target;
215
216 if (target == GL_TEXTURE_CUBE_MAP)
217 faceTarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + face;
218
219 texImage = _mesa_get_tex_image(ctx, texObj, faceTarget, level);
220
221 if (!texImage) {
222 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexStorage");
223 return GL_FALSE;
224 }
225
226 _mesa_init_teximage_fields(ctx, texImage,
227 levelWidth, levelHeight, levelDepth,
228 0, internalFormat, texFormat);
229 }
230
231 _mesa_next_mipmap_level_size(target, 0,
232 levelWidth, levelHeight, levelDepth,
233 &levelWidth, &levelHeight, &levelDepth);
234 }
235
236 /* "unbind" */
237 texObj->Target = 0;
238
239 return GL_TRUE;
240 }
241
242 #define RETURN_IF_SUPPORTED(t) do { \
243 if (newTarget == GL_ ## t) \
244 return true; \
245 } while (0)
246
247 /**
248 * Check for compatible target
249 * If an error is found, record it with _mesa_error()
250 * \return false if any error, true otherwise.
251 */
252 static bool
253 target_valid(struct gl_context *ctx, GLenum origTarget, GLenum newTarget)
254 {
255 /*
256 * From ARB_texture_view spec:
257 ---------------------------------------------------------------------------------------------------------
258 | Original target | Valid new targets |
259 ---------------------------------------------------------------------------------------------------------
260 | TEXTURE_1D | TEXTURE_1D, TEXTURE_1D_ARRAY |
261 | ------------------------------------------------------------------------------------------------------- |
262 | TEXTURE_2D | TEXTURE_2D, TEXTURE_2D_ARRAY |
263 | ------------------------------------------------------------------------------------------------------- |
264 | TEXTURE_3D | TEXTURE_3D |
265 | ------------------------------------------------------------------------------------------------------- |
266 | TEXTURE_CUBE_MAP | TEXTURE_CUBE_MAP, TEXTURE_2D, TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP_ARRAY |
267 | ------------------------------------------------------------------------------------------------------- |
268 | TEXTURE_RECTANGLE | TEXTURE_RECTANGLE |
269 | ------------------------------------------------------------------------------------------------------- |
270 | TEXTURE_BUFFER | <none> |
271 | ------------------------------------------------------------------------------------------------------- |
272 | TEXTURE_1D_ARRAY | TEXTURE_1D_ARRAY, TEXTURE_1D |
273 | ------------------------------------------------------------------------------------------------------- |
274 | TEXTURE_2D_ARRAY | TEXTURE_2D_ARRAY, TEXTURE_2D, TEXTURE_CUBE_MAP, TEXTURE_CUBE_MAP_ARRAY |
275 | ------------------------------------------------------------------------------------------------------- |
276 | TEXTURE_CUBE_MAP_ARRAY | TEXTURE_CUBE_MAP_ARRAY, TEXTURE_2D_ARRAY, TEXTURE_2D, TEXTURE_CUBE_MAP |
277 | ------------------------------------------------------------------------------------------------------- |
278 | TEXTURE_2D_MULTISAMPLE | TEXTURE_2D_MULTISAMPLE, TEXTURE_2D_MULTISAMPLE_ARRAY |
279 | ------------------------------------------------------------------------------------------------------- |
280 | TEXTURE_2D_MULTISAMPLE_ARRAY | TEXTURE_2D_MULTISAMPLE, TEXTURE_2D_MULTISAMPLE_ARRAY |
281 ---------------------------------------------------------------------------------------------------------
282 */
283
284 switch (origTarget) {
285 case GL_TEXTURE_1D:
286 case GL_TEXTURE_1D_ARRAY:
287 RETURN_IF_SUPPORTED(TEXTURE_1D);
288 RETURN_IF_SUPPORTED(TEXTURE_1D_ARRAY);
289 break;
290 case GL_TEXTURE_2D:
291 RETURN_IF_SUPPORTED(TEXTURE_2D);
292 RETURN_IF_SUPPORTED(TEXTURE_2D_ARRAY);
293 break;
294 case GL_TEXTURE_3D:
295 RETURN_IF_SUPPORTED(TEXTURE_3D);
296 break;
297 case GL_TEXTURE_RECTANGLE:
298 RETURN_IF_SUPPORTED(TEXTURE_RECTANGLE);
299 break;
300 case GL_TEXTURE_CUBE_MAP:
301 case GL_TEXTURE_2D_ARRAY:
302 case GL_TEXTURE_CUBE_MAP_ARRAY:
303 RETURN_IF_SUPPORTED(TEXTURE_2D);
304 RETURN_IF_SUPPORTED(TEXTURE_2D_ARRAY);
305 RETURN_IF_SUPPORTED(TEXTURE_CUBE_MAP);
306 RETURN_IF_SUPPORTED(TEXTURE_CUBE_MAP_ARRAY);
307 break;
308 case GL_TEXTURE_2D_MULTISAMPLE:
309 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
310 RETURN_IF_SUPPORTED(TEXTURE_2D_MULTISAMPLE);
311 RETURN_IF_SUPPORTED(TEXTURE_2D_MULTISAMPLE_ARRAY);
312 break;
313 }
314 _mesa_error(ctx, GL_INVALID_OPERATION,
315 "glTextureView(illegal target=%s)",
316 _mesa_lookup_enum_by_nr(newTarget));
317 return false;
318 }
319 #undef RETURN_IF_SUPPORTED
320
321 /**
322 * Check for compatible format
323 * If an error is found, record it with _mesa_error()
324 * \return false if any error, true otherwise.
325 */
326 bool
327 _mesa_texture_view_compatible_format(const struct gl_context *ctx,
328 GLenum origInternalFormat,
329 GLenum newInternalFormat)
330 {
331 unsigned int origViewClass, newViewClass;
332
333 /* The two textures' internal formats must be compatible according to
334 * Table 3.X.2 (Compatible internal formats for TextureView)
335 * if the internal format exists in that table the view class must match.
336 * The internal formats must be identical if not in that table,
337 * or an INVALID_OPERATION error is generated.
338 */
339 if (origInternalFormat == newInternalFormat)
340 return true;
341
342 origViewClass = lookup_view_class(ctx, origInternalFormat);
343 newViewClass = lookup_view_class(ctx, newInternalFormat);
344 if ((origViewClass == newViewClass) && origViewClass != false)
345 return true;
346
347 return false;
348 }
349
350 /**
351 * Helper function for TexStorage and teximagemultisample to set immutable
352 * texture state needed by ARB_texture_view.
353 */
354 void
355 _mesa_set_texture_view_state(struct gl_context *ctx,
356 struct gl_texture_object *texObj,
357 GLenum target, GLuint levels)
358 {
359 struct gl_texture_image *texImage;
360
361 /* Get a reference to what will become this View's base level */
362 texImage = _mesa_select_tex_image(texObj, target, 0);
363
364 /* When an immutable texture is created via glTexStorage or
365 * glTexImageMultisample,
366 * TEXTURE_IMMUTABLE_FORMAT becomes TRUE.
367 * TEXTURE_IMMUTABLE_LEVELS and TEXTURE_VIEW_NUM_LEVELS become levels.
368 * If the texture target is TEXTURE_1D_ARRAY then
369 * TEXTURE_VIEW_NUM_LAYERS becomes height.
370 * If the texture target is TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP_ARRAY,
371 * or TEXTURE_2D_MULTISAMPLE_ARRAY then TEXTURE_VIEW_NUM_LAYERS becomes
372 * depth.
373 * If the texture target is TEXTURE_CUBE_MAP, then
374 * TEXTURE_VIEW_NUM_LAYERS becomes 6.
375 * For any other texture target, TEXTURE_VIEW_NUM_LAYERS becomes 1.
376 *
377 * ARB_texture_multisample: Multisample textures do
378 * not have multiple image levels.
379 */
380
381 texObj->Immutable = GL_TRUE;
382 texObj->ImmutableLevels = levels;
383 texObj->MinLevel = 0;
384 texObj->NumLevels = levels;
385 texObj->MinLayer = 0;
386 texObj->NumLayers = 1;
387 switch (target) {
388 case GL_TEXTURE_1D_ARRAY:
389 texObj->NumLayers = texImage->Height;
390 break;
391
392 case GL_TEXTURE_2D_MULTISAMPLE:
393 texObj->NumLevels = 1;
394 texObj->ImmutableLevels = 1;
395 break;
396
397 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
398 texObj->NumLevels = 1;
399 texObj->ImmutableLevels = 1;
400 /* fall through to set NumLayers */
401
402 case GL_TEXTURE_2D_ARRAY:
403 case GL_TEXTURE_CUBE_MAP_ARRAY:
404 texObj->NumLayers = texImage->Depth;
405 break;
406
407 case GL_TEXTURE_CUBE_MAP:
408 texObj->NumLayers = 6;
409 break;
410 }
411 }
412
413 /**
414 * glTextureView (ARB_texture_view)
415 * If an error is found, record it with _mesa_error()
416 * \return none.
417 */
418 void GLAPIENTRY
419 _mesa_TextureView(GLuint texture, GLenum target, GLuint origtexture,
420 GLenum internalformat,
421 GLuint minlevel, GLuint numlevels,
422 GLuint minlayer, GLuint numlayers)
423 {
424 struct gl_texture_object *texObj;
425 struct gl_texture_object *origTexObj;
426 struct gl_texture_image *origTexImage;
427 GLuint newViewMinLevel, newViewMinLayer;
428 GLuint newViewNumLevels, newViewNumLayers;
429 GLsizei width, height, depth;
430 mesa_format texFormat;
431 GLboolean sizeOK, dimensionsOK;
432 GLenum faceTarget;
433
434 GET_CURRENT_CONTEXT(ctx);
435
436 if (MESA_VERBOSE & (VERBOSE_API | VERBOSE_TEXTURE))
437 _mesa_debug(ctx, "glTextureView %d %s %d %s %d %d %d %d\n",
438 texture, _mesa_lookup_enum_by_nr(target), origtexture,
439 _mesa_lookup_enum_by_nr(internalformat),
440 minlevel, numlevels, minlayer, numlayers);
441
442 if (origtexture == 0) {
443 _mesa_error(ctx, GL_INVALID_VALUE, "glTextureView(origtexture = %u)",
444 origtexture);
445 return;
446 }
447
448 /* Need original texture information to validate arguments */
449 origTexObj = _mesa_lookup_texture(ctx, origtexture);
450
451 /* If <origtexture> is not the name of a texture, INVALID_VALUE
452 * is generated.
453 */
454 if (!origTexObj) {
455 _mesa_error(ctx, GL_INVALID_VALUE, "glTextureView(origtexture = %u)",
456 origtexture);
457 return;
458 }
459
460 /* If <origtexture>'s TEXTURE_IMMUTABLE_FORMAT value is not TRUE,
461 * INVALID_OPERATION is generated.
462 */
463 if (!origTexObj->Immutable) {
464 _mesa_error(ctx, GL_INVALID_OPERATION,
465 "glTextureView(origtexture not immutable)");
466 return;
467 }
468
469 /* If <texture> is 0, INVALID_VALUE is generated. */
470 if (texture == 0) {
471 _mesa_error(ctx, GL_INVALID_VALUE, "glTextureView(texture = 0)");
472 return;
473 }
474
475 /* If <texture> is not a valid name returned by GenTextures,
476 * the error INVALID_OPERATION is generated.
477 */
478 texObj = _mesa_lookup_texture(ctx, texture);
479 if (texObj == NULL) {
480 _mesa_error(ctx, GL_INVALID_OPERATION,
481 "glTextureView(texture = %u non-gen name)", texture);
482 return;
483 }
484
485 /* If <texture> has already been bound and given a target, then
486 * the error INVALID_OPERATION is generated.
487 */
488 if (texObj->Target) {
489 _mesa_error(ctx, GL_INVALID_OPERATION,
490 "glTextureView(texture = %u already bound)", texture);
491 return;
492 }
493
494 /* Check for compatible target */
495 if (!target_valid(ctx, origTexObj->Target, target)) {
496 return; /* error was recorded */
497 }
498
499 /* minlevel and minlayer are relative to the view of origtexture.
500 * If minlevel or minlayer is greater than level or layer, respectively,
501 * return INVALID_VALUE.
502 */
503 newViewMinLevel = origTexObj->MinLevel + minlevel;
504 newViewMinLayer = origTexObj->MinLayer + minlayer;
505 if (newViewMinLevel >= (origTexObj->MinLevel + origTexObj->NumLevels)) {
506 _mesa_error(ctx, GL_INVALID_VALUE,
507 "glTextureView(new minlevel (%d) > orig minlevel (%d)"
508 " + orig numlevels (%d))",
509 newViewMinLevel, origTexObj->MinLevel, origTexObj->NumLevels);
510 return;
511 }
512
513 if (newViewMinLayer >= (origTexObj->MinLayer + origTexObj->NumLayers)) {
514 _mesa_error(ctx, GL_INVALID_VALUE,
515 "glTextureView(new minlayer (%d) > orig minlayer (%d)"
516 " + orig numlayers (%d))",
517 newViewMinLayer, origTexObj->MinLayer, origTexObj->NumLayers);
518 return;
519 }
520
521 if (!_mesa_texture_view_compatible_format(ctx,
522 origTexObj->Image[0][0]->InternalFormat,
523 internalformat)) {
524 _mesa_error(ctx, GL_INVALID_OPERATION,
525 "glTextureView(internalformat %s not compatible with origtexture %s)",
526 _mesa_lookup_enum_by_nr(internalformat),
527 _mesa_lookup_enum_by_nr(origTexObj->Image[0][0]->InternalFormat));
528 return;
529 }
530
531 texFormat = _mesa_choose_texture_format(ctx, texObj, target, 0,
532 internalformat, GL_NONE, GL_NONE);
533 assert(texFormat != MESA_FORMAT_NONE);
534 if (texFormat == MESA_FORMAT_NONE) return;
535
536 newViewNumLevels = MIN2(numlevels, origTexObj->NumLevels - minlevel);
537 newViewNumLayers = MIN2(numlayers, origTexObj->NumLayers - minlayer);
538
539 faceTarget = origTexObj->Target;
540 if (faceTarget == GL_TEXTURE_CUBE_MAP)
541 faceTarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + minlayer;
542
543 /* Get a reference to what will become this View's base level */
544 origTexImage = _mesa_select_tex_image(origTexObj, faceTarget, minlevel);
545 width = origTexImage->Width;
546 height = origTexImage->Height;
547 depth = origTexImage->Depth;
548
549 /* Adjust width, height, depth to be appropriate for new target */
550 switch (target) {
551 case GL_TEXTURE_1D:
552 height = 1;
553 break;
554
555 case GL_TEXTURE_3D:
556 break;
557
558 case GL_TEXTURE_1D_ARRAY:
559 height = (GLsizei) newViewNumLayers;
560 break;
561
562 case GL_TEXTURE_2D:
563 case GL_TEXTURE_2D_MULTISAMPLE:
564 case GL_TEXTURE_RECTANGLE:
565 case GL_TEXTURE_CUBE_MAP:
566 depth = 1;
567 break;
568
569 case GL_TEXTURE_2D_ARRAY:
570 case GL_TEXTURE_CUBE_MAP_ARRAY:
571 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
572 depth = newViewNumLayers;
573 break;
574 }
575
576 /* If the dimensions of the original texture are larger than the maximum
577 * supported dimensions of the new target, the error INVALID_OPERATION is
578 * generated. For example, if the original texture has a TEXTURE_2D_ARRAY
579 * target and its width is greater than MAX_CUBE_MAP_TEXTURE_SIZE, an error
580 * will be generated if TextureView is called to create a TEXTURE_CUBE_MAP
581 * view.
582 */
583 dimensionsOK = _mesa_legal_texture_dimensions(ctx, target, 0,
584 width, height, depth, 0);
585 if (!dimensionsOK) {
586 _mesa_error(ctx, GL_INVALID_OPERATION,
587 "glTextureView(invalid width or height or depth)");
588 return;
589 }
590
591 sizeOK = ctx->Driver.TestProxyTexImage(ctx, target, 0, texFormat,
592 width, height, depth, 0);
593 if (!sizeOK) {
594 _mesa_error(ctx, GL_INVALID_OPERATION,
595 "glTextureView(invalid texture size)");
596 return;
597 }
598
599 /* If <target> is TEXTURE_1D, TEXTURE_2D, TEXTURE_3D, TEXTURE_RECTANGLE,
600 * or TEXTURE_2D_MULTISAMPLE and <numlayers> does not equal 1, the error
601 * INVALID_VALUE is generated.
602 */
603 switch (target) {
604 case GL_TEXTURE_1D:
605 case GL_TEXTURE_2D:
606 case GL_TEXTURE_3D:
607 case GL_TEXTURE_RECTANGLE:
608 case GL_TEXTURE_2D_MULTISAMPLE:
609 if (numlayers != 1) {
610 _mesa_error(ctx, GL_INVALID_VALUE, "glTextureView(numlayers %d != 1)",
611 numlayers);
612 return;
613 }
614 break;
615
616 case GL_TEXTURE_CUBE_MAP:
617 /* If the new texture's target is TEXTURE_CUBE_MAP, the clamped
618 * <numlayers> must be equal to 6.
619 */
620 if (newViewNumLayers != 6) {
621 _mesa_error(ctx, GL_INVALID_VALUE,
622 "glTextureView(clamped numlayers %d != 6)",
623 newViewNumLayers);
624 return;
625 }
626 break;
627
628 case GL_TEXTURE_CUBE_MAP_ARRAY:
629 /* If the new texture's target is TEXTURE_CUBE_MAP_ARRAY,
630 * then <numlayers> counts layer-faces rather than layers,
631 * and the clamped <numlayers> must be a multiple of 6.
632 * Otherwise, the error INVALID_VALUE is generated.
633 */
634 if ((newViewNumLayers % 6) != 0) {
635 _mesa_error(ctx, GL_INVALID_VALUE,
636 "glTextureView(clamped numlayers %d is not"
637 " a multiple of 6)",
638 newViewNumLayers);
639 return;
640 }
641 break;
642 }
643
644 /* If the new texture's target is TEXTURE_CUBE_MAP or
645 * TEXTURE_CUBE_MAP_ARRAY, the width and height of the original texture's
646 * levels must be equal otherwise the error INVALID_OPERATION is generated.
647 */
648 if ((target == GL_TEXTURE_CUBE_MAP || target == GL_TEXTURE_CUBE_MAP_ARRAY) &&
649 (origTexImage->Width != origTexImage->Height)) {
650 _mesa_error(ctx, GL_INVALID_OPERATION,
651 "glTextureView(origtexture width (%d) != height (%d))",
652 origTexImage->Width, origTexImage->Height);
653 return;
654 }
655
656 /* When the original texture's target is TEXTURE_CUBE_MAP, the layer
657 * parameters are interpreted in the same order as if it were a
658 * TEXTURE_CUBE_MAP_ARRAY with 6 layer-faces.
659 */
660
661 /* If the internal format does not exactly match the internal format of the
662 * original texture, the contents of the memory are reinterpreted in the
663 * same manner as for image bindings described in
664 * section 3.9.20 (Texture Image Loads and Stores).
665 */
666
667 /* TEXTURE_BASE_LEVEL and TEXTURE_MAX_LEVEL are interpreted
668 * relative to the view and not relative to the original data store.
669 */
670
671 if (!initialize_texture_fields(ctx, target, texObj, newViewNumLevels,
672 width, height, depth,
673 internalformat, texFormat)) {
674 return; /* Already recorded error */
675 }
676
677 texObj->MinLevel = newViewMinLevel;
678 texObj->MinLayer = newViewMinLayer;
679 texObj->NumLevels = newViewNumLevels;
680 texObj->NumLayers = newViewNumLayers;
681 texObj->Immutable = GL_TRUE;
682 texObj->ImmutableLevels = origTexObj->ImmutableLevels;
683 texObj->Target = target;
684
685 if (ctx->Driver.TextureView != NULL &&
686 !ctx->Driver.TextureView(ctx, texObj, origTexObj)) {
687 return; /* driver recorded error */
688 }
689 }