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
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
86 #define VIEW_CLASS_GLES(x) (GL_VIEW_CLASS_BPTC_FLOAT + 1 + x)
87 #define VIEW_CLASS_EAC_R11 VIEW_CLASS_GLES(0)
88 #define VIEW_CLASS_EAC_RG11 VIEW_CLASS_GLES(1)
89 #define VIEW_CLASS_ETC2_RGB VIEW_CLASS_GLES(2)
90 #define VIEW_CLASS_ETC2_RGBA VIEW_CLASS_GLES(3)
91 #define VIEW_CLASS_ETC2_EAC_RGBA VIEW_CLASS_GLES(4)
92 #define VIEW_CLASS_ASTC_4x4_RGBA VIEW_CLASS_GLES(5)
93 #define VIEW_CLASS_ASTC_5x4_RGBA VIEW_CLASS_GLES(6)
94 #define VIEW_CLASS_ASTC_5x5_RGBA VIEW_CLASS_GLES(7)
95 #define VIEW_CLASS_ASTC_6x5_RGBA VIEW_CLASS_GLES(8)
96 #define VIEW_CLASS_ASTC_6x6_RGBA VIEW_CLASS_GLES(9)
97 #define VIEW_CLASS_ASTC_8x5_RGBA VIEW_CLASS_GLES(10)
98 #define VIEW_CLASS_ASTC_8x6_RGBA VIEW_CLASS_GLES(11)
99 #define VIEW_CLASS_ASTC_8x8_RGBA VIEW_CLASS_GLES(12)
100 #define VIEW_CLASS_ASTC_10x5_RGBA VIEW_CLASS_GLES(13)
101 #define VIEW_CLASS_ASTC_10x6_RGBA VIEW_CLASS_GLES(14)
102 #define VIEW_CLASS_ASTC_10x8_RGBA VIEW_CLASS_GLES(15)
103 #define VIEW_CLASS_ASTC_10x10_RGBA VIEW_CLASS_GLES(16)
104 #define VIEW_CLASS_ASTC_12x10_RGBA VIEW_CLASS_GLES(17)
105 #define VIEW_CLASS_ASTC_12x12_RGBA VIEW_CLASS_GLES(18)
106 #define VIEW_CLASS_ASTC_3x3x3_RGBA VIEW_CLASS_GLES(19)
107 #define VIEW_CLASS_ASTC_4x3x3_RGBA VIEW_CLASS_GLES(20)
108 #define VIEW_CLASS_ASTC_4x4x3_RGBA VIEW_CLASS_GLES(21)
109 #define VIEW_CLASS_ASTC_4x4x4_RGBA VIEW_CLASS_GLES(22)
110 #define VIEW_CLASS_ASTC_5x4x4_RGBA VIEW_CLASS_GLES(23)
111 #define VIEW_CLASS_ASTC_5x5x4_RGBA VIEW_CLASS_GLES(24)
112 #define VIEW_CLASS_ASTC_5x5x5_RGBA VIEW_CLASS_GLES(25)
113 #define VIEW_CLASS_ASTC_6x5x5_RGBA VIEW_CLASS_GLES(26)
114 #define VIEW_CLASS_ASTC_6x6x5_RGBA VIEW_CLASS_GLES(27)
115 #define VIEW_CLASS_ASTC_6x6x6_RGBA VIEW_CLASS_GLES(28)
116
117
118 struct internal_format_class_info {
119 GLenum view_class;
120 GLenum internal_format;
121 };
122 static const struct internal_format_class_info compatible_internal_formats[] = {
123 {GL_VIEW_CLASS_128_BITS, GL_RGBA32F},
124 {GL_VIEW_CLASS_128_BITS, GL_RGBA32UI},
125 {GL_VIEW_CLASS_128_BITS, GL_RGBA32I},
126 {GL_VIEW_CLASS_96_BITS, GL_RGB32F},
127 {GL_VIEW_CLASS_96_BITS, GL_RGB32UI},
128 {GL_VIEW_CLASS_96_BITS, GL_RGB32I},
129 {GL_VIEW_CLASS_64_BITS, GL_RGBA16F},
130 {GL_VIEW_CLASS_64_BITS, GL_RG32F},
131 {GL_VIEW_CLASS_64_BITS, GL_RGBA16UI},
132 {GL_VIEW_CLASS_64_BITS, GL_RG32UI},
133 {GL_VIEW_CLASS_64_BITS, GL_RGBA16I},
134 {GL_VIEW_CLASS_64_BITS, GL_RG32I},
135 {GL_VIEW_CLASS_64_BITS, GL_RGBA16},
136 {GL_VIEW_CLASS_64_BITS, GL_RGBA16_SNORM},
137 {GL_VIEW_CLASS_48_BITS, GL_RGB16},
138 {GL_VIEW_CLASS_48_BITS, GL_RGB16_SNORM},
139 {GL_VIEW_CLASS_48_BITS, GL_RGB16F},
140 {GL_VIEW_CLASS_48_BITS, GL_RGB16UI},
141 {GL_VIEW_CLASS_48_BITS, GL_RGB16I},
142 {GL_VIEW_CLASS_32_BITS, GL_RG16F},
143 {GL_VIEW_CLASS_32_BITS, GL_R11F_G11F_B10F},
144 {GL_VIEW_CLASS_32_BITS, GL_R32F},
145 {GL_VIEW_CLASS_32_BITS, GL_RGB10_A2UI},
146 {GL_VIEW_CLASS_32_BITS, GL_RGBA8UI},
147 {GL_VIEW_CLASS_32_BITS, GL_RG16UI},
148 {GL_VIEW_CLASS_32_BITS, GL_R32UI},
149 {GL_VIEW_CLASS_32_BITS, GL_RGBA8I},
150 {GL_VIEW_CLASS_32_BITS, GL_RG16I},
151 {GL_VIEW_CLASS_32_BITS, GL_R32I},
152 {GL_VIEW_CLASS_32_BITS, GL_RGB10_A2},
153 {GL_VIEW_CLASS_32_BITS, GL_RGBA8},
154 {GL_VIEW_CLASS_32_BITS, GL_RG16},
155 {GL_VIEW_CLASS_32_BITS, GL_RGBA8_SNORM},
156 {GL_VIEW_CLASS_32_BITS, GL_RG16_SNORM},
157 {GL_VIEW_CLASS_32_BITS, GL_SRGB8_ALPHA8},
158 {GL_VIEW_CLASS_32_BITS, GL_RGB9_E5},
159 {GL_VIEW_CLASS_24_BITS, GL_RGB8},
160 {GL_VIEW_CLASS_24_BITS, GL_RGB8_SNORM},
161 {GL_VIEW_CLASS_24_BITS, GL_SRGB8},
162 {GL_VIEW_CLASS_24_BITS, GL_RGB8UI},
163 {GL_VIEW_CLASS_24_BITS, GL_RGB8I},
164 {GL_VIEW_CLASS_16_BITS, GL_R16F},
165 {GL_VIEW_CLASS_16_BITS, GL_RG8UI},
166 {GL_VIEW_CLASS_16_BITS, GL_R16UI},
167 {GL_VIEW_CLASS_16_BITS, GL_RG8I},
168 {GL_VIEW_CLASS_16_BITS, GL_R16I},
169 {GL_VIEW_CLASS_16_BITS, GL_RG8},
170 {GL_VIEW_CLASS_16_BITS, GL_R16},
171 {GL_VIEW_CLASS_16_BITS, GL_RG8_SNORM},
172 {GL_VIEW_CLASS_16_BITS, GL_R16_SNORM},
173 {GL_VIEW_CLASS_8_BITS, GL_R8UI},
174 {GL_VIEW_CLASS_8_BITS, GL_R8I},
175 {GL_VIEW_CLASS_8_BITS, GL_R8},
176 {GL_VIEW_CLASS_8_BITS, GL_R8_SNORM},
177 {GL_VIEW_CLASS_8_BITS, GL_SR8_EXT},
178 {GL_VIEW_CLASS_RGTC1_RED, GL_COMPRESSED_RED_RGTC1},
179 {GL_VIEW_CLASS_RGTC1_RED, GL_COMPRESSED_SIGNED_RED_RGTC1},
180 {GL_VIEW_CLASS_RGTC2_RG, GL_COMPRESSED_RG_RGTC2},
181 {GL_VIEW_CLASS_RGTC2_RG, GL_COMPRESSED_SIGNED_RG_RGTC2},
182 {GL_VIEW_CLASS_BPTC_UNORM, GL_COMPRESSED_RGBA_BPTC_UNORM_ARB},
183 {GL_VIEW_CLASS_BPTC_UNORM, GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB},
184 {GL_VIEW_CLASS_BPTC_FLOAT, GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB},
185 {GL_VIEW_CLASS_BPTC_FLOAT, GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB},
186 };
187
188 static const struct internal_format_class_info s3tc_compatible_internal_formats[] = {
189 {GL_VIEW_CLASS_S3TC_DXT1_RGB, GL_COMPRESSED_RGB_S3TC_DXT1_EXT},
190 {GL_VIEW_CLASS_S3TC_DXT1_RGB, GL_COMPRESSED_SRGB_S3TC_DXT1_EXT},
191 {GL_VIEW_CLASS_S3TC_DXT1_RGBA, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT},
192 {GL_VIEW_CLASS_S3TC_DXT1_RGBA, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT},
193 {GL_VIEW_CLASS_S3TC_DXT3_RGBA, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT},
194 {GL_VIEW_CLASS_S3TC_DXT3_RGBA, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT},
195 {GL_VIEW_CLASS_S3TC_DXT5_RGBA, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT},
196 {GL_VIEW_CLASS_S3TC_DXT5_RGBA, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT},
197 };
198
199 static const struct internal_format_class_info gles_etc2_compatible_internal_formats[] = {
200 {VIEW_CLASS_EAC_R11, GL_COMPRESSED_R11_EAC},
201 {VIEW_CLASS_EAC_R11, GL_COMPRESSED_SIGNED_R11_EAC},
202 {VIEW_CLASS_EAC_RG11, GL_COMPRESSED_RG11_EAC},
203 {VIEW_CLASS_EAC_RG11, GL_COMPRESSED_SIGNED_RG11_EAC},
204 {VIEW_CLASS_ETC2_RGB, GL_COMPRESSED_RGB8_ETC2},
205 {VIEW_CLASS_ETC2_RGB, GL_COMPRESSED_SRGB8_ETC2},
206 {VIEW_CLASS_ETC2_RGBA, GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2},
207 {VIEW_CLASS_ETC2_RGBA, GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2},
208 {VIEW_CLASS_ETC2_EAC_RGBA, GL_COMPRESSED_RGBA8_ETC2_EAC},
209 {VIEW_CLASS_ETC2_EAC_RGBA, GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC},
210 };
211
212 static const struct internal_format_class_info gles_astc_compatible_internal_formats[] = {
213 #define ASTC_FMT(size) \
214 {VIEW_CLASS_ASTC_##size##_RGBA, GL_COMPRESSED_RGBA_ASTC_##size##_KHR}, \
215 {VIEW_CLASS_ASTC_##size##_RGBA, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_##size##_KHR}
216
217 ASTC_FMT(4x4),
218 ASTC_FMT(5x4),
219 ASTC_FMT(5x5),
220 ASTC_FMT(6x5),
221 ASTC_FMT(6x6),
222 ASTC_FMT(8x5),
223 ASTC_FMT(8x6),
224 ASTC_FMT(8x8),
225 ASTC_FMT(10x5),
226 ASTC_FMT(10x6),
227 ASTC_FMT(10x8),
228 ASTC_FMT(10x10),
229 ASTC_FMT(12x10),
230 ASTC_FMT(12x12),
231 #undef ASTC_FMT
232 };
233
234 static const struct internal_format_class_info gles_astc_3d_compatible_internal_formats[] = {
235 #define ASTC_FMT(size) \
236 {VIEW_CLASS_ASTC_##size##_RGBA, GL_COMPRESSED_RGBA_ASTC_##size##_OES}, \
237 {VIEW_CLASS_ASTC_##size##_RGBA, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_##size##_OES}
238
239 ASTC_FMT(3x3x3),
240 ASTC_FMT(4x3x3),
241 ASTC_FMT(4x4x3),
242 ASTC_FMT(4x4x4),
243 ASTC_FMT(5x4x4),
244 ASTC_FMT(5x5x4),
245 ASTC_FMT(5x5x5),
246 ASTC_FMT(6x5x5),
247 ASTC_FMT(6x6x5),
248 ASTC_FMT(6x6x6),
249 #undef ASTC_FMT
250 };
251
252 GLenum
_mesa_texture_view_lookup_view_class(const struct gl_context * ctx,GLenum internalformat)253 _mesa_texture_view_lookup_view_class(const struct gl_context *ctx, GLenum internalformat)
254 {
255 GLuint i;
256
257 for (i = 0; i < ARRAY_SIZE(compatible_internal_formats); i++) {
258 if (compatible_internal_formats[i].internal_format == internalformat)
259 return compatible_internal_formats[i].view_class;
260 }
261
262 if (ctx->Extensions.EXT_texture_compression_s3tc &&
263 ctx->Extensions.EXT_texture_sRGB) {
264 for (i = 0; i < ARRAY_SIZE(s3tc_compatible_internal_formats); i++) {
265 if (s3tc_compatible_internal_formats[i].internal_format
266 == internalformat)
267 return s3tc_compatible_internal_formats[i].view_class;
268 }
269 }
270
271 if (_mesa_is_gles3(ctx)) {
272 for (i = 0; i < ARRAY_SIZE(gles_etc2_compatible_internal_formats); i++) {
273 if (gles_etc2_compatible_internal_formats[i].internal_format
274 == internalformat)
275 return gles_etc2_compatible_internal_formats[i].view_class;
276 }
277
278 if (ctx->Extensions.KHR_texture_compression_astc_ldr) {
279 for (i = 0; i < ARRAY_SIZE(gles_astc_compatible_internal_formats); i++) {
280 if (gles_astc_compatible_internal_formats[i].internal_format
281 == internalformat)
282 return gles_astc_compatible_internal_formats[i].view_class;
283 }
284 }
285
286 if (ctx->Extensions.OES_texture_compression_astc) {
287 for (i = 0; i < ARRAY_SIZE(gles_astc_3d_compatible_internal_formats); i++) {
288 if (gles_astc_3d_compatible_internal_formats[i].internal_format
289 == internalformat)
290 return gles_astc_3d_compatible_internal_formats[i].view_class;
291 }
292 }
293 }
294 return GL_FALSE;
295 }
296
297 /**
298 * Initialize new texture's gl_texture_image structures. Will not call driver
299 * to allocate new space, simply record relevant layer, face, format, etc.
300 * \return GL_FALSE if any error, GL_TRUE otherwise.
301 */
302 static GLboolean
initialize_texture_fields(struct gl_context * ctx,GLenum target,struct gl_texture_object * texObj,GLint levels,GLsizei width,GLsizei height,GLsizei depth,GLenum internalFormat,mesa_format texFormat,GLuint numSamples,GLboolean fixedSampleLocations)303 initialize_texture_fields(struct gl_context *ctx,
304 GLenum target,
305 struct gl_texture_object *texObj,
306 GLint levels,
307 GLsizei width, GLsizei height, GLsizei depth,
308 GLenum internalFormat, mesa_format texFormat,
309 GLuint numSamples, GLboolean fixedSampleLocations)
310 {
311 const GLuint numFaces = _mesa_num_tex_faces(target);
312 GLint level, levelWidth = width, levelHeight = height, levelDepth = depth;
313 GLuint face;
314
315 /* Pretend we are bound to initialize the gl_texture_image structs */
316 texObj->Target = target;
317
318 /* Set up all the texture object's gl_texture_images */
319 for (level = 0; level < levels; level++) {
320 for (face = 0; face < numFaces; face++) {
321 struct gl_texture_image *texImage;
322 const GLenum faceTarget = _mesa_cube_face_target(target, face);
323
324 texImage = _mesa_get_tex_image(ctx, texObj, faceTarget, level);
325
326 if (!texImage) {
327 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexStorage");
328 return GL_FALSE;
329 }
330
331 _mesa_init_teximage_fields_ms(ctx, texImage,
332 levelWidth, levelHeight, levelDepth,
333 0, internalFormat, texFormat,
334 numSamples, fixedSampleLocations);
335 }
336
337 _mesa_next_mipmap_level_size(target, 0,
338 levelWidth, levelHeight, levelDepth,
339 &levelWidth, &levelHeight, &levelDepth);
340 }
341
342 /* "unbind" */
343 texObj->Target = 0;
344
345 return GL_TRUE;
346 }
347
348 #define RETURN_IF_SUPPORTED(t) do { \
349 if (newTarget == GL_ ## t) \
350 return true; \
351 } while (0)
352
353 /**
354 * Check for compatible target
355 * If an error is found, record it with _mesa_error()
356 * \return false if any error, true otherwise.
357 */
358 static bool
target_valid(struct gl_context * ctx,GLenum origTarget,GLenum newTarget)359 target_valid(struct gl_context *ctx, GLenum origTarget, GLenum newTarget)
360 {
361 /*
362 * From ARB_texture_view spec:
363 ---------------------------------------------------------------------------------------------------------
364 | Original target | Valid new targets |
365 ---------------------------------------------------------------------------------------------------------
366 | TEXTURE_1D | TEXTURE_1D, TEXTURE_1D_ARRAY |
367 | ------------------------------------------------------------------------------------------------------- |
368 | TEXTURE_2D | TEXTURE_2D, TEXTURE_2D_ARRAY |
369 | ------------------------------------------------------------------------------------------------------- |
370 | TEXTURE_3D | TEXTURE_3D |
371 | ------------------------------------------------------------------------------------------------------- |
372 | TEXTURE_CUBE_MAP | TEXTURE_CUBE_MAP, TEXTURE_2D, TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP_ARRAY |
373 | ------------------------------------------------------------------------------------------------------- |
374 | TEXTURE_RECTANGLE | TEXTURE_RECTANGLE |
375 | ------------------------------------------------------------------------------------------------------- |
376 | TEXTURE_BUFFER | <none> |
377 | ------------------------------------------------------------------------------------------------------- |
378 | TEXTURE_1D_ARRAY | TEXTURE_1D_ARRAY, TEXTURE_1D |
379 | ------------------------------------------------------------------------------------------------------- |
380 | TEXTURE_2D_ARRAY | TEXTURE_2D_ARRAY, TEXTURE_2D, TEXTURE_CUBE_MAP, TEXTURE_CUBE_MAP_ARRAY |
381 | ------------------------------------------------------------------------------------------------------- |
382 | TEXTURE_CUBE_MAP_ARRAY | TEXTURE_CUBE_MAP_ARRAY, TEXTURE_2D_ARRAY, TEXTURE_2D, TEXTURE_CUBE_MAP |
383 | ------------------------------------------------------------------------------------------------------- |
384 | TEXTURE_2D_MULTISAMPLE | TEXTURE_2D_MULTISAMPLE, TEXTURE_2D_MULTISAMPLE_ARRAY |
385 | ------------------------------------------------------------------------------------------------------- |
386 | TEXTURE_2D_MULTISAMPLE_ARRAY | TEXTURE_2D_MULTISAMPLE, TEXTURE_2D_MULTISAMPLE_ARRAY |
387 ---------------------------------------------------------------------------------------------------------
388 */
389
390 switch (origTarget) {
391 case GL_TEXTURE_1D:
392 case GL_TEXTURE_1D_ARRAY:
393 RETURN_IF_SUPPORTED(TEXTURE_1D);
394 RETURN_IF_SUPPORTED(TEXTURE_1D_ARRAY);
395 break;
396 case GL_TEXTURE_2D:
397 RETURN_IF_SUPPORTED(TEXTURE_2D);
398 RETURN_IF_SUPPORTED(TEXTURE_2D_ARRAY);
399 break;
400 case GL_TEXTURE_3D:
401 RETURN_IF_SUPPORTED(TEXTURE_3D);
402 break;
403 case GL_TEXTURE_RECTANGLE:
404 RETURN_IF_SUPPORTED(TEXTURE_RECTANGLE);
405 break;
406 case GL_TEXTURE_CUBE_MAP:
407 case GL_TEXTURE_2D_ARRAY:
408 case GL_TEXTURE_CUBE_MAP_ARRAY:
409 RETURN_IF_SUPPORTED(TEXTURE_2D);
410 RETURN_IF_SUPPORTED(TEXTURE_2D_ARRAY);
411 RETURN_IF_SUPPORTED(TEXTURE_CUBE_MAP);
412 RETURN_IF_SUPPORTED(TEXTURE_CUBE_MAP_ARRAY);
413 break;
414 case GL_TEXTURE_2D_MULTISAMPLE:
415 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
416 RETURN_IF_SUPPORTED(TEXTURE_2D_MULTISAMPLE);
417 RETURN_IF_SUPPORTED(TEXTURE_2D_MULTISAMPLE_ARRAY);
418 break;
419 }
420 _mesa_error(ctx, GL_INVALID_OPERATION,
421 "glTextureView(illegal target=%s)",
422 _mesa_enum_to_string(newTarget));
423 return false;
424 }
425 #undef RETURN_IF_SUPPORTED
426
427 /**
428 * Check for compatible format
429 * If an error is found, record it with _mesa_error()
430 * \return false if any error, true otherwise.
431 */
432 bool
_mesa_texture_view_compatible_format(const struct gl_context * ctx,GLenum origInternalFormat,GLenum newInternalFormat)433 _mesa_texture_view_compatible_format(const struct gl_context *ctx,
434 GLenum origInternalFormat,
435 GLenum newInternalFormat)
436 {
437 unsigned int origViewClass, newViewClass;
438
439 /* The two textures' internal formats must be compatible according to
440 * Table 3.X.2 (Compatible internal formats for TextureView)
441 * if the internal format exists in that table the view class must match.
442 * The internal formats must be identical if not in that table,
443 * or an INVALID_OPERATION error is generated.
444 */
445 if (origInternalFormat == newInternalFormat)
446 return true;
447
448 origViewClass = _mesa_texture_view_lookup_view_class(ctx, origInternalFormat);
449 newViewClass = _mesa_texture_view_lookup_view_class(ctx, newInternalFormat);
450 if ((origViewClass == newViewClass) && origViewClass != false)
451 return true;
452
453 return false;
454 }
455
456 /**
457 * Helper function for TexStorage and teximagemultisample to set immutable
458 * texture state needed by ARB_texture_view.
459 */
460 void
_mesa_set_texture_view_state(struct gl_context * ctx,struct gl_texture_object * texObj,GLenum target,GLuint levels)461 _mesa_set_texture_view_state(struct gl_context *ctx,
462 struct gl_texture_object *texObj,
463 GLenum target, GLuint levels)
464 {
465 struct gl_texture_image *texImage;
466
467 /* Get a reference to what will become this View's base level */
468 texImage = _mesa_select_tex_image(texObj, target, 0);
469
470 /* When an immutable texture is created via glTexStorage or
471 * glTexImageMultisample,
472 * TEXTURE_IMMUTABLE_FORMAT becomes TRUE.
473 * TEXTURE_IMMUTABLE_LEVELS and TEXTURE_VIEW_NUM_LEVELS become levels.
474 * If the texture target is TEXTURE_1D_ARRAY then
475 * TEXTURE_VIEW_NUM_LAYERS becomes height.
476 * If the texture target is TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP_ARRAY,
477 * or TEXTURE_2D_MULTISAMPLE_ARRAY then TEXTURE_VIEW_NUM_LAYERS becomes
478 * depth.
479 * If the texture target is TEXTURE_CUBE_MAP, then
480 * TEXTURE_VIEW_NUM_LAYERS becomes 6.
481 * For any other texture target, TEXTURE_VIEW_NUM_LAYERS becomes 1.
482 *
483 * ARB_texture_multisample: Multisample textures do
484 * not have multiple image levels.
485 */
486
487 texObj->Immutable = GL_TRUE;
488 texObj->ImmutableLevels = levels;
489 texObj->MinLevel = 0;
490 texObj->NumLevels = levels;
491 texObj->MinLayer = 0;
492 texObj->NumLayers = 1;
493 switch (target) {
494 case GL_TEXTURE_1D_ARRAY:
495 texObj->NumLayers = texImage->Height;
496 break;
497
498 case GL_TEXTURE_2D_MULTISAMPLE:
499 texObj->NumLevels = 1;
500 texObj->ImmutableLevels = 1;
501 break;
502
503 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
504 texObj->NumLevels = 1;
505 texObj->ImmutableLevels = 1;
506 /* fallthrough - to set NumLayers */
507
508 case GL_TEXTURE_2D_ARRAY:
509 case GL_TEXTURE_CUBE_MAP_ARRAY:
510 texObj->NumLayers = texImage->Depth;
511 break;
512
513 case GL_TEXTURE_CUBE_MAP:
514 texObj->NumLayers = 6;
515 break;
516 }
517 }
518
519 /**
520 * glTextureView (ARB_texture_view)
521 * If an error is found, record it with _mesa_error()
522 * \return none.
523 */
524 static ALWAYS_INLINE void
texture_view(struct gl_context * ctx,struct gl_texture_object * origTexObj,struct gl_texture_object * texObj,GLenum target,GLenum internalformat,GLuint minlevel,GLuint numlevels,GLuint minlayer,GLuint numlayers,bool no_error)525 texture_view(struct gl_context *ctx, struct gl_texture_object *origTexObj,
526 struct gl_texture_object *texObj, GLenum target,
527 GLenum internalformat, GLuint minlevel, GLuint numlevels,
528 GLuint minlayer, GLuint numlayers, bool no_error)
529 {
530 struct gl_texture_image *origTexImage;
531 GLuint newViewNumLevels, newViewNumLayers;
532 GLsizei width, height, depth;
533 mesa_format texFormat;
534 GLboolean sizeOK, dimensionsOK;
535 GLenum faceTarget;
536
537 texFormat = _mesa_choose_texture_format(ctx, texObj, target, 0,
538 internalformat, GL_NONE, GL_NONE);
539 if (texFormat == MESA_FORMAT_NONE) return;
540
541 newViewNumLevels = MIN2(numlevels, origTexObj->NumLevels - minlevel);
542 newViewNumLayers = MIN2(numlayers, origTexObj->NumLayers - minlayer);
543
544 faceTarget = _mesa_cube_face_target(origTexObj->Target, minlayer);
545
546 /* Get a reference to what will become this View's base level */
547 origTexImage = _mesa_select_tex_image(origTexObj, faceTarget, minlevel);
548 width = origTexImage->Width;
549 height = origTexImage->Height;
550 depth = origTexImage->Depth;
551
552 /* Adjust width, height, depth to be appropriate for new target */
553 switch (target) {
554 case GL_TEXTURE_1D:
555 height = 1;
556 break;
557
558 case GL_TEXTURE_3D:
559 break;
560
561 case GL_TEXTURE_1D_ARRAY:
562 height = (GLsizei) newViewNumLayers;
563 break;
564
565 case GL_TEXTURE_2D:
566 case GL_TEXTURE_2D_MULTISAMPLE:
567 case GL_TEXTURE_RECTANGLE:
568 depth = 1;
569 break;
570 case GL_TEXTURE_CUBE_MAP:
571 /* If the new texture's target is TEXTURE_CUBE_MAP, the clamped
572 * <numlayers> must be equal to 6.
573 */
574 if (!no_error && newViewNumLayers != 6) {
575 _mesa_error(ctx, GL_INVALID_VALUE,
576 "glTextureView(clamped numlayers %d != 6)",
577 newViewNumLayers);
578 return;
579 }
580 depth = 1;
581 break;
582
583 case GL_TEXTURE_2D_ARRAY:
584 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
585 depth = newViewNumLayers;
586 break;
587 case GL_TEXTURE_CUBE_MAP_ARRAY:
588 /* If the new texture's target is TEXTURE_CUBE_MAP_ARRAY,
589 * then <numlayers> counts layer-faces rather than layers,
590 * and the clamped <numlayers> must be a multiple of 6.
591 * Otherwise, the error INVALID_VALUE is generated.
592 */
593 if (!no_error && (newViewNumLayers % 6) != 0) {
594 _mesa_error(ctx, GL_INVALID_VALUE,
595 "glTextureView(clamped numlayers %d is not"
596 " a multiple of 6)",
597 newViewNumLayers);
598 return;
599 }
600 depth = newViewNumLayers;
601 break;
602 }
603
604 if (!no_error) {
605 /* If the dimensions of the original texture are larger than the maximum
606 * supported dimensions of the new target, the error INVALID_OPERATION is
607 * generated. For example, if the original texture has a TEXTURE_2D_ARRAY
608 * target and its width is greater than MAX_CUBE_MAP_TEXTURE_SIZE, an
609 * error will be generated if TextureView is called to create a
610 * TEXTURE_CUBE_MAP view.
611 */
612 dimensionsOK = _mesa_legal_texture_dimensions(ctx, target, 0,
613 width, height, depth, 0);
614 if (!dimensionsOK) {
615 _mesa_error(ctx, GL_INVALID_OPERATION,
616 "glTextureView(invalid width or height or depth)");
617 return;
618 }
619
620 sizeOK = ctx->Driver.TestProxyTexImage(ctx, target, 1, 0, texFormat,
621 origTexImage->NumSamples,
622 width, height, depth);
623 if (!sizeOK) {
624 _mesa_error(ctx, GL_INVALID_OPERATION,
625 "glTextureView(invalid texture size)");
626 return;
627 }
628
629 /* If <target> is TEXTURE_1D, TEXTURE_2D, TEXTURE_3D, TEXTURE_RECTANGLE,
630 * or TEXTURE_2D_MULTISAMPLE and <numlayers> does not equal 1, the error
631 * INVALID_VALUE is generated.
632 */
633 switch (target) {
634 case GL_TEXTURE_1D:
635 case GL_TEXTURE_2D:
636 case GL_TEXTURE_3D:
637 case GL_TEXTURE_RECTANGLE:
638 case GL_TEXTURE_2D_MULTISAMPLE:
639 if (numlayers != 1) {
640 _mesa_error(ctx, GL_INVALID_VALUE, "glTextureView(numlayers %d != 1)",
641 numlayers);
642 return;
643 }
644 break;
645 case GL_TEXTURE_CUBE_MAP:
646 break;
647 case GL_TEXTURE_CUBE_MAP_ARRAY:
648 break;
649 }
650
651 /* If the new texture's target is TEXTURE_CUBE_MAP or
652 * TEXTURE_CUBE_MAP_ARRAY, the width and height of the original texture's
653 * levels must be equal otherwise the error INVALID_OPERATION is
654 * generated.
655 */
656 if ((target == GL_TEXTURE_CUBE_MAP || target == GL_TEXTURE_CUBE_MAP_ARRAY)
657 && (origTexImage->Width != origTexImage->Height)) {
658 _mesa_error(ctx, GL_INVALID_OPERATION,
659 "glTextureView(origtexture width (%d) != height (%d))",
660 origTexImage->Width, origTexImage->Height);
661 return;
662 }
663 }
664
665 /* When the original texture's target is TEXTURE_CUBE_MAP, the layer
666 * parameters are interpreted in the same order as if it were a
667 * TEXTURE_CUBE_MAP_ARRAY with 6 layer-faces.
668 */
669
670 /* If the internal format does not exactly match the internal format of the
671 * original texture, the contents of the memory are reinterpreted in the
672 * same manner as for image bindings described in
673 * section 3.9.20 (Texture Image Loads and Stores).
674 */
675
676 /* TEXTURE_BASE_LEVEL and TEXTURE_MAX_LEVEL are interpreted
677 * relative to the view and not relative to the original data store.
678 */
679
680 if (!initialize_texture_fields(ctx, target, texObj, newViewNumLevels,
681 width, height, depth,
682 internalformat, texFormat,
683 origTexImage->NumSamples,
684 origTexImage->FixedSampleLocations)) {
685 return; /* Already recorded error */
686 }
687
688 texObj->MinLevel = origTexObj->MinLevel + minlevel;
689 texObj->MinLayer = origTexObj->MinLayer + minlayer;
690 texObj->NumLevels = newViewNumLevels;
691 texObj->NumLayers = newViewNumLayers;
692 texObj->Immutable = GL_TRUE;
693 texObj->ImmutableLevels = origTexObj->ImmutableLevels;
694 texObj->Target = target;
695 texObj->TargetIndex = _mesa_tex_target_to_index(ctx, target);
696 assert(texObj->TargetIndex < NUM_TEXTURE_TARGETS);
697
698 if (ctx->Driver.TextureView != NULL &&
699 !ctx->Driver.TextureView(ctx, texObj, origTexObj)) {
700 return; /* driver recorded error */
701 }
702 }
703
704 void GLAPIENTRY
_mesa_TextureView_no_error(GLuint texture,GLenum target,GLuint origtexture,GLenum internalformat,GLuint minlevel,GLuint numlevels,GLuint minlayer,GLuint numlayers)705 _mesa_TextureView_no_error(GLuint texture, GLenum target, GLuint origtexture,
706 GLenum internalformat,
707 GLuint minlevel, GLuint numlevels,
708 GLuint minlayer, GLuint numlayers)
709 {
710 struct gl_texture_object *texObj;
711 struct gl_texture_object *origTexObj;
712
713 GET_CURRENT_CONTEXT(ctx);
714
715 origTexObj = _mesa_lookup_texture(ctx, origtexture);
716 texObj = _mesa_lookup_texture(ctx, texture);
717
718 texture_view(ctx, origTexObj, texObj, target, internalformat, minlevel,
719 numlevels, minlayer, numlayers, true);
720 }
721
722 void GLAPIENTRY
_mesa_TextureView(GLuint texture,GLenum target,GLuint origtexture,GLenum internalformat,GLuint minlevel,GLuint numlevels,GLuint minlayer,GLuint numlayers)723 _mesa_TextureView(GLuint texture, GLenum target, GLuint origtexture,
724 GLenum internalformat,
725 GLuint minlevel, GLuint numlevels,
726 GLuint minlayer, GLuint numlayers)
727 {
728 struct gl_texture_object *texObj;
729 struct gl_texture_object *origTexObj;
730 GLuint newViewMinLevel, newViewMinLayer;
731
732 GET_CURRENT_CONTEXT(ctx);
733
734 if (MESA_VERBOSE & (VERBOSE_API | VERBOSE_TEXTURE))
735 _mesa_debug(ctx, "glTextureView %d %s %d %s %d %d %d %d\n",
736 texture, _mesa_enum_to_string(target), origtexture,
737 _mesa_enum_to_string(internalformat),
738 minlevel, numlevels, minlayer, numlayers);
739
740 if (origtexture == 0) {
741 _mesa_error(ctx, GL_INVALID_VALUE, "glTextureView(origtexture = %u)",
742 origtexture);
743 return;
744 }
745
746 /* Need original texture information to validate arguments */
747 origTexObj = _mesa_lookup_texture(ctx, origtexture);
748
749 /* If <origtexture> is not the name of a texture, INVALID_VALUE
750 * is generated.
751 */
752 if (!origTexObj) {
753 _mesa_error(ctx, GL_INVALID_VALUE, "glTextureView(origtexture = %u)",
754 origtexture);
755 return;
756 }
757
758 /* If <origtexture>'s TEXTURE_IMMUTABLE_FORMAT value is not TRUE,
759 * INVALID_OPERATION is generated.
760 */
761 if (!origTexObj->Immutable) {
762 _mesa_error(ctx, GL_INVALID_OPERATION,
763 "glTextureView(origtexture not immutable)");
764 return;
765 }
766
767 /* If <texture> is 0, INVALID_VALUE is generated. */
768 if (texture == 0) {
769 _mesa_error(ctx, GL_INVALID_VALUE, "glTextureView(texture = 0)");
770 return;
771 }
772
773 /* If <texture> is not a valid name returned by GenTextures,
774 * the error INVALID_OPERATION is generated.
775 */
776 texObj = _mesa_lookup_texture(ctx, texture);
777 if (texObj == NULL) {
778 _mesa_error(ctx, GL_INVALID_OPERATION,
779 "glTextureView(texture = %u non-gen name)", texture);
780 return;
781 }
782
783 /* If <texture> has already been bound and given a target, then
784 * the error INVALID_OPERATION is generated.
785 */
786 if (texObj->Target) {
787 _mesa_error(ctx, GL_INVALID_OPERATION,
788 "glTextureView(texture = %u already bound)", texture);
789 return;
790 }
791
792 /* Check for compatible target */
793 if (!target_valid(ctx, origTexObj->Target, target)) {
794 return; /* error was recorded */
795 }
796
797 /* minlevel and minlayer are relative to the view of origtexture.
798 * If minlevel or minlayer is greater than level or layer, respectively,
799 * return INVALID_VALUE.
800 */
801 newViewMinLevel = origTexObj->MinLevel + minlevel;
802 newViewMinLayer = origTexObj->MinLayer + minlayer;
803 if (newViewMinLevel >= (origTexObj->MinLevel + origTexObj->NumLevels)) {
804 _mesa_error(ctx, GL_INVALID_VALUE,
805 "glTextureView(new minlevel (%d) > orig minlevel (%d)"
806 " + orig numlevels (%d))",
807 newViewMinLevel, origTexObj->MinLevel, origTexObj->NumLevels);
808 return;
809 }
810
811 if (newViewMinLayer >= (origTexObj->MinLayer + origTexObj->NumLayers)) {
812 _mesa_error(ctx, GL_INVALID_VALUE,
813 "glTextureView(new minlayer (%d) > orig minlayer (%d)"
814 " + orig numlayers (%d))",
815 newViewMinLayer, origTexObj->MinLayer, origTexObj->NumLayers);
816 return;
817 }
818
819 if (!_mesa_texture_view_compatible_format(ctx,
820 origTexObj->Image[0][0]->InternalFormat,
821 internalformat)) {
822 _mesa_error(ctx, GL_INVALID_OPERATION,
823 "glTextureView(internalformat %s not compatible with origtexture %s)",
824 _mesa_enum_to_string(internalformat),
825 _mesa_enum_to_string(origTexObj->Image[0][0]->InternalFormat));
826 return;
827 }
828
829 texture_view(ctx, origTexObj, texObj, target, internalformat, minlevel,
830 numlevels, minlayer, numlayers, false);
831 }
832