2 * Mesa 3-D graphics library
4 * Copyright (C) 2013 LunarG, Inc.
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:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
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.
25 * Courtney Goeltzenleuchter <courtney@lunarg.com>
31 * GL_ARB_texture_view functions
42 #include "texstorage.h"
43 #include "textureview.h"
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 ---------------------------------------------------------------------------
85 struct internal_format_class_info
{
87 GLenum internal_format
;
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
},
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
},
166 * Lookup format view class based on internalformat
167 * \return VIEW_CLASS if internalformat found in table, false otherwise.
170 lookup_view_class(struct gl_context
*ctx
, GLenum internalformat
)
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
;
179 if (ctx
->Extensions
.EXT_texture_compression_s3tc
&& ctx
->Extensions
.EXT_texture_sRGB
) {
180 for (i
= 0; i
< ARRAY_SIZE(s3tc_compatible_internal_formats
); i
++) {
181 if (s3tc_compatible_internal_formats
[i
].internal_format
== internalformat
)
182 return s3tc_compatible_internal_formats
[i
].view_class
;
189 * Initialize new texture's gl_texture_image structures. Will not call driver
190 * to allocate new space, simply record relevant layer, face, format, etc.
191 * \return GL_FALSE if any error, GL_TRUE otherwise.
194 initialize_texture_fields(struct gl_context
*ctx
,
196 struct gl_texture_object
*texObj
,
198 GLsizei width
, GLsizei height
, GLsizei depth
,
199 GLenum internalFormat
, gl_format texFormat
)
201 const GLuint numFaces
= _mesa_num_tex_faces(target
);
202 GLint level
, levelWidth
= width
, levelHeight
= height
, levelDepth
= depth
;
205 /* Pretend we are bound to initialize the gl_texture_image structs */
206 texObj
->Target
= target
;
208 /* Set up all the texture object's gl_texture_images */
209 for (level
= 0; level
< levels
; level
++) {
210 for (face
= 0; face
< numFaces
; face
++) {
211 struct gl_texture_image
*texImage
=
212 _mesa_get_tex_image(ctx
, texObj
, face
, level
);
215 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexStorage");
219 _mesa_init_teximage_fields(ctx
, texImage
,
220 levelWidth
, levelHeight
, levelDepth
,
221 0, internalFormat
, texFormat
);
224 _mesa_next_mipmap_level_size(target
, 0, levelWidth
, levelHeight
, levelDepth
,
225 &levelWidth
, &levelHeight
, &levelDepth
);
234 #define RETURN_IF_SUPPORTED(t) do { \
235 if (newTarget == GL_ ## t) \
240 * Check for compatible target
241 * If an error is found, record it with _mesa_error()
242 * \return false if any error, true otherwise.
245 target_valid(struct gl_context
*ctx
, GLenum origTarget
, GLenum newTarget
)
248 * From ARB_texture_view spec:
249 ---------------------------------------------------------------------------------------------------------
250 | Original target | Valid new targets |
251 ---------------------------------------------------------------------------------------------------------
252 | TEXTURE_1D | TEXTURE_1D, TEXTURE_1D_ARRAY |
253 | ------------------------------------------------------------------------------------------------------- |
254 | TEXTURE_2D | TEXTURE_2D, TEXTURE_2D_ARRAY |
255 | ------------------------------------------------------------------------------------------------------- |
256 | TEXTURE_3D | TEXTURE_3D |
257 | ------------------------------------------------------------------------------------------------------- |
258 | TEXTURE_CUBE_MAP | TEXTURE_CUBE_MAP, TEXTURE_2D, TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP_ARRAY |
259 | ------------------------------------------------------------------------------------------------------- |
260 | TEXTURE_RECTANGLE | TEXTURE_RECTANGLE |
261 | ------------------------------------------------------------------------------------------------------- |
262 | TEXTURE_BUFFER | <none> |
263 | ------------------------------------------------------------------------------------------------------- |
264 | TEXTURE_1D_ARRAY | TEXTURE_1D_ARRAY, TEXTURE_1D |
265 | ------------------------------------------------------------------------------------------------------- |
266 | TEXTURE_2D_ARRAY | TEXTURE_2D_ARRAY, TEXTURE_2D, TEXTURE_CUBE_MAP, TEXTURE_CUBE_MAP_ARRAY |
267 | ------------------------------------------------------------------------------------------------------- |
268 | TEXTURE_CUBE_MAP_ARRAY | TEXTURE_CUBE_MAP_ARRAY, TEXTURE_2D_ARRAY, TEXTURE_2D, TEXTURE_CUBE_MAP |
269 | ------------------------------------------------------------------------------------------------------- |
270 | TEXTURE_2D_MULTISAMPLE | TEXTURE_2D_MULTISAMPLE, TEXTURE_2D_MULTISAMPLE_ARRAY |
271 | ------------------------------------------------------------------------------------------------------- |
272 | TEXTURE_2D_MULTISAMPLE_ARRAY | TEXTURE_2D_MULTISAMPLE, TEXTURE_2D_MULTISAMPLE_ARRAY |
273 ---------------------------------------------------------------------------------------------------------
276 switch (origTarget
) {
278 case GL_TEXTURE_1D_ARRAY
:
279 RETURN_IF_SUPPORTED(TEXTURE_1D
);
280 RETURN_IF_SUPPORTED(TEXTURE_1D_ARRAY
);
283 RETURN_IF_SUPPORTED(TEXTURE_2D
);
284 RETURN_IF_SUPPORTED(TEXTURE_2D_ARRAY
);
287 RETURN_IF_SUPPORTED(TEXTURE_3D
);
289 case GL_TEXTURE_RECTANGLE
:
290 RETURN_IF_SUPPORTED(TEXTURE_RECTANGLE
);
292 case GL_TEXTURE_CUBE_MAP
:
293 case GL_TEXTURE_2D_ARRAY
:
294 case GL_TEXTURE_CUBE_MAP_ARRAY
:
295 RETURN_IF_SUPPORTED(TEXTURE_2D
);
296 RETURN_IF_SUPPORTED(TEXTURE_2D_ARRAY
);
297 RETURN_IF_SUPPORTED(TEXTURE_CUBE_MAP
);
298 RETURN_IF_SUPPORTED(TEXTURE_CUBE_MAP_ARRAY
);
300 case GL_TEXTURE_2D_MULTISAMPLE
:
301 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
302 RETURN_IF_SUPPORTED(TEXTURE_2D_MULTISAMPLE
);
303 RETURN_IF_SUPPORTED(TEXTURE_2D_MULTISAMPLE_ARRAY
);
306 _mesa_error(ctx
, GL_INVALID_OPERATION
,
307 "glTextureView(illegal target=%s)",
308 _mesa_lookup_enum_by_nr(newTarget
));
311 #undef RETURN_IF_SUPPORTED
314 * Check for compatible format
315 * If an error is found, record it with _mesa_error()
316 * \return false if any error, true otherwise.
319 compatible_format(struct gl_context
*ctx
, const struct gl_texture_object
*origTexObj
,
320 GLenum internalformat
)
322 /* Level 0 of a texture created by glTextureStorage or glTextureView
325 struct gl_texture_image
*texImage
= origTexObj
->Image
[0][0];
326 GLint origInternalFormat
= texImage
->InternalFormat
;
327 unsigned int origViewClass
, newViewClass
;
329 /* The two textures' internal formats must be compatible according to
330 * Table 3.X.2 (Compatible internal formats for TextureView)
331 * if the internal format exists in that table the view class must match.
332 * The internal formats must be identical if not in that table,
333 * or an INVALID_OPERATION error is generated.
335 if (origInternalFormat
== internalformat
)
338 origViewClass
= lookup_view_class(ctx
, origInternalFormat
);
339 newViewClass
= lookup_view_class(ctx
, internalformat
);
340 if ((origViewClass
== newViewClass
) && origViewClass
!= false)
343 _mesa_error(ctx
, GL_INVALID_OPERATION
,
344 "glTextureView(internalformat %s not compatible with origtexture %s)",
345 _mesa_lookup_enum_by_nr(internalformat
),
346 _mesa_lookup_enum_by_nr(origInternalFormat
));
350 * Helper function for TexStorage and teximagemultisample to set immutable
351 * texture state needed by ARB_texture_view.
354 _mesa_set_texture_view_state(struct gl_context
*ctx
,
355 struct gl_texture_object
*texObj
,
356 GLenum target
, GLuint levels
)
358 struct gl_texture_image
*texImage
;
360 /* Get a reference to what will become this View's base level */
361 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, 0);
363 /* When an immutable texture is created via glTexStorage or glTexImageMultisample,
364 * TEXTURE_IMMUTABLE_FORMAT becomes TRUE.
365 * TEXTURE_IMMUTABLE_LEVELS and TEXTURE_VIEW_NUM_LEVELS become levels.
366 * If the texture target is TEXTURE_1D_ARRAY then
367 * TEXTURE_VIEW_NUM_LAYERS becomes height.
368 * If the texture target is TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP_ARRAY,
369 * or TEXTURE_2D_MULTISAMPLE_ARRAY then TEXTURE_VIEW_NUM_LAYERS becomes 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.
374 * ARB_texture_multisample: Multisample textures do
375 * not have multiple image levels.
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;
385 case GL_TEXTURE_1D_ARRAY
:
386 texObj
->NumLayers
= texImage
->Height
;
389 case GL_TEXTURE_2D_MULTISAMPLE
:
390 texObj
->NumLevels
= 1;
391 texObj
->ImmutableLevels
= 1;
394 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
395 texObj
->NumLevels
= 1;
396 texObj
->ImmutableLevels
= 1;
397 /* fall through to set NumLayers */
399 case GL_TEXTURE_2D_ARRAY
:
400 case GL_TEXTURE_CUBE_MAP_ARRAY
:
401 texObj
->NumLayers
= texImage
->Depth
;
404 case GL_TEXTURE_CUBE_MAP
:
405 texObj
->NumLayers
= 6;
412 * glTextureView (ARB_texture_view)
413 * If an error is found, record it with _mesa_error()
417 _mesa_TextureView(GLuint texture
, GLenum target
, GLuint origtexture
,
418 GLenum internalformat
,
419 GLuint minlevel
, GLuint numlevels
,
420 GLuint minlayer
, GLuint numlayers
)
422 struct gl_texture_object
*texObj
;
423 struct gl_texture_object
*origTexObj
;
424 struct gl_texture_image
*origTexImage
;
425 GLuint newViewMinLevel
, newViewMinLayer
;
426 GLuint newViewNumLevels
, newViewNumLayers
;
427 GLsizei width
, height
, depth
;
429 GLboolean sizeOK
, dimensionsOK
;
432 GET_CURRENT_CONTEXT(ctx
);
434 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
))
435 _mesa_debug(ctx
, "glTextureView %d %s %d %s %d %d %d %d\n",
436 texture
, _mesa_lookup_enum_by_nr(target
), origtexture
,
437 _mesa_lookup_enum_by_nr(internalformat
),
438 minlevel
, numlevels
, minlayer
, numlayers
);
440 if (origtexture
== 0) {
441 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTextureView(origtexture = %u)", origtexture
);
445 /* Need original texture information to validate arguments */
446 origTexObj
= _mesa_lookup_texture(ctx
, origtexture
);
448 /* If <origtexture> is not the name of a texture, INVALID_VALUE is generated. */
450 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTextureView(origtexture = %u)", origtexture
);
454 /* If <origtexture>'s TEXTURE_IMMUTABLE_FORMAT value is not TRUE,
455 * INVALID_OPERATION is generated.
457 if (!origTexObj
->Immutable
) {
458 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTextureView(origtexture not immutable)");
462 /* If <texture> is 0, INVALID_VALUE is generated. */
464 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTextureView(texture = 0)");
468 /* If <texture> is not a valid name returned by GenTextures,
469 * the error INVALID_OPERATION is generated.
471 texObj
= _mesa_lookup_texture(ctx
, texture
);
472 if (texObj
== NULL
) {
473 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTextureView(texture = %u non-gen name)", texture
);
477 /* If <texture> has already been bound and given a target, then
478 * the error INVALID_OPERATION is generated.
480 if (texObj
->Target
) {
481 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTextureView(texture = %u already bound)", texture
);
485 /* Check for compatible target */
486 if (!target_valid(ctx
, origTexObj
->Target
, target
)) {
487 return; /* error was recorded */
490 /* minlevel and minlayer are relative to the view of origtexture
491 * If minlevel or minlayer is greater than level or layer, respectively,
492 * of origtexture return INVALID_VALUE.
494 newViewMinLevel
= origTexObj
->MinLevel
+ minlevel
;
495 newViewMinLayer
= origTexObj
->MinLayer
+ minlayer
;
496 if (newViewMinLevel
>= (origTexObj
->MinLevel
+ origTexObj
->NumLevels
)) {
497 _mesa_error(ctx
, GL_INVALID_VALUE
,
498 "glTextureView(new minlevel (%d) > orig minlevel (%d) + orig numlevels (%d))",
499 newViewMinLevel
, origTexObj
->MinLevel
, origTexObj
->NumLevels
);
503 if (newViewMinLayer
>= (origTexObj
->MinLayer
+ origTexObj
->NumLayers
)) {
504 _mesa_error(ctx
, GL_INVALID_VALUE
,
505 "glTextureView(new minlayer (%d) > orig minlayer (%d) + orig numlayers (%d))",
506 newViewMinLayer
, origTexObj
->MinLayer
, origTexObj
->NumLayers
);
510 if (!compatible_format(ctx
, origTexObj
, internalformat
)) {
511 return; /* Error logged */
514 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, 0,
515 internalformat
, GL_NONE
, GL_NONE
);
516 assert(texFormat
!= MESA_FORMAT_NONE
);
517 if (texFormat
== MESA_FORMAT_NONE
) return;
519 newViewNumLevels
= MIN2(numlevels
, origTexObj
->NumLevels
- minlevel
);
520 newViewNumLayers
= MIN2(numlayers
, origTexObj
->NumLayers
- minlayer
);
522 faceTarget
= origTexObj
->Target
;
523 if (faceTarget
== GL_TEXTURE_CUBE_MAP
)
524 faceTarget
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ minlayer
;
526 /* Get a reference to what will become this View's base level */
527 origTexImage
= _mesa_select_tex_image(ctx
, origTexObj
,
528 faceTarget
, minlevel
);
529 width
= origTexImage
->Width
;
530 height
= origTexImage
->Height
;
531 depth
= origTexImage
->Depth
;
533 /* Adjust width, height, depth to be appropriate for new target */
539 case GL_TEXTURE_1D_ARRAY
:
540 height
= (GLsizei
) newViewNumLayers
;
544 case GL_TEXTURE_2D_MULTISAMPLE
:
545 case GL_TEXTURE_RECTANGLE
:
546 case GL_TEXTURE_CUBE_MAP
:
550 case GL_TEXTURE_2D_ARRAY
:
551 case GL_TEXTURE_CUBE_MAP_ARRAY
:
552 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
553 depth
= newViewNumLayers
;
557 /* If the dimensions of the original texture are larger than the maximum
558 * supported dimensions of the new target, the error INVALID_OPERATION is
559 * generated. For example, if the original texture has a TEXTURE_2D_ARRAY
560 * target and its width is greater than MAX_CUBE_MAP_TEXTURE_SIZE, an error
561 * will be generated if TextureView is called to create a TEXTURE_CUBE_MAP
564 dimensionsOK
= _mesa_legal_texture_dimensions(ctx
, target
, 0,
565 width
, height
, depth
, 0);
567 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTextureView(invalid width or height or depth)");
571 sizeOK
= ctx
->Driver
.TestProxyTexImage(ctx
, target
, 0, texFormat
,
572 width
, height
, depth
, 0);
574 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTextureView(invalid texture size)");
578 /* If <target> is TEXTURE_1D, TEXTURE_2D, TEXTURE_3D, TEXTURE_RECTANGLE,
579 * or TEXTURE_2D_MULTISAMPLE and <numlayers> does not equal 1, the error
580 * INVALID_VALUE is generated.
586 case GL_TEXTURE_RECTANGLE
:
587 case GL_TEXTURE_2D_MULTISAMPLE
:
588 if (numlayers
!= 1) {
589 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTextureView(numlayers %d != 1)", numlayers
);
594 case GL_TEXTURE_CUBE_MAP
:
595 /* If the new texture's target is TEXTURE_CUBE_MAP, the clamped <numlayers>
596 * must be equal to 6.
598 if (newViewNumLayers
!= 6) {
599 _mesa_error(ctx
, GL_INVALID_VALUE
, "glTextureView(clamped numlayers %d != 6)",
605 case GL_TEXTURE_CUBE_MAP_ARRAY
:
606 /* If the new texture's target is TEXTURE_CUBE_MAP_ARRAY,
607 * then <numlayers> counts layer-faces rather than layers,
608 * and the clamped <numlayers> must be a multiple of 6.
609 * Otherwise, the error INVALID_VALUE is generated.
611 if ((newViewNumLayers
% 6) != 0) {
612 _mesa_error(ctx
, GL_INVALID_VALUE
,
613 "glTextureView(clamped numlayers %d is not a multiple of 6)",
620 /* If the new texture's target is TEXTURE_CUBE_MAP or
621 * TEXTURE_CUBE_MAP_ARRAY, the width and height of the original texture's
622 * levels must be equal otherwise the error INVALID_OPERATION is generated.
624 if ((target
== GL_TEXTURE_CUBE_MAP
|| target
== GL_TEXTURE_CUBE_MAP_ARRAY
) &&
625 (origTexImage
->Width
!= origTexImage
->Height
)) {
626 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glTextureView(origtexture width (%d) != height (%d))",
627 origTexImage
->Width
, origTexImage
->Height
);
631 /* When the original texture's target is TEXTURE_CUBE_MAP, the layer
632 * parameters are interpreted in the same order as if it were a
633 * TEXTURE_CUBE_MAP_ARRAY with 6 layer-faces.
636 /* If the internal format does not exactly match the internal format of the
637 * original texture, the contents of the memory are reinterpreted in the
638 * same manner as for image bindings described in
639 * section 3.9.20 (Texture Image Loads and Stores).
642 /* TEXTURE_BASE_LEVEL and TEXTURE_MAX_LEVEL are interpreted
643 * relative to the view and not relative to the original data store.
646 if (!initialize_texture_fields(ctx
, target
, texObj
, newViewNumLevels
,
647 width
, height
, depth
,
648 internalformat
, texFormat
)) {
649 return; /* Already recorded error */
652 texObj
->MinLevel
= newViewMinLevel
;
653 texObj
->MinLayer
= newViewMinLayer
;
654 texObj
->NumLevels
= newViewNumLevels
;
655 texObj
->NumLayers
= newViewNumLayers
;
656 texObj
->Immutable
= GL_TRUE
;
657 texObj
->ImmutableLevels
= origTexObj
->ImmutableLevels
;
658 texObj
->Target
= target
;
660 if (ctx
->Driver
.TextureView
!= NULL
&& !ctx
->Driver
.TextureView(ctx
, texObj
, origTexObj
)) {
661 return; /* driver recorded error */