mesa: Add "OR COPYRIGHT HOLDERS" to license text disclaiming liability.
[mesa.git] / src / mesa / main / texstorage.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2011 VMware, Inc. All Rights Reserved.
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
17 * OR 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 LIABILITY, WHETHER IN
20 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24
25 /**
26 * \file texstorage.c
27 * GL_ARB_texture_storage functions
28 */
29
30
31
32 #include "glheader.h"
33 #include "context.h"
34 #include "enums.h"
35 #include "imports.h"
36 #include "macros.h"
37 #include "teximage.h"
38 #include "texobj.h"
39 #include "texstorage.h"
40 #include "mtypes.h"
41
42
43
44 /**
45 * Check if the given texture target is a legal texture object target
46 * for a glTexStorage() command.
47 * This is a bit different than legal_teximage_target() when it comes
48 * to cube maps.
49 */
50 static GLboolean
51 legal_texobj_target(struct gl_context *ctx, GLuint dims, GLenum target)
52 {
53 switch (dims) {
54 case 1:
55 switch (target) {
56 case GL_TEXTURE_1D:
57 case GL_PROXY_TEXTURE_1D:
58 return GL_TRUE;
59 default:
60 return GL_FALSE;
61 }
62 case 2:
63 switch (target) {
64 case GL_TEXTURE_2D:
65 case GL_PROXY_TEXTURE_2D:
66 return GL_TRUE;
67 case GL_TEXTURE_CUBE_MAP:
68 case GL_PROXY_TEXTURE_CUBE_MAP:
69 return ctx->Extensions.ARB_texture_cube_map;
70 case GL_TEXTURE_RECTANGLE:
71 case GL_PROXY_TEXTURE_RECTANGLE:
72 return ctx->Extensions.NV_texture_rectangle;
73 case GL_TEXTURE_1D_ARRAY:
74 case GL_PROXY_TEXTURE_1D_ARRAY:
75 return (ctx->Extensions.MESA_texture_array ||
76 ctx->Extensions.EXT_texture_array);
77 default:
78 return GL_FALSE;
79 }
80 case 3:
81 switch (target) {
82 case GL_TEXTURE_3D:
83 case GL_PROXY_TEXTURE_3D:
84 return GL_TRUE;
85 case GL_TEXTURE_2D_ARRAY:
86 case GL_PROXY_TEXTURE_2D_ARRAY:
87 return (ctx->Extensions.MESA_texture_array ||
88 ctx->Extensions.EXT_texture_array);
89 case GL_TEXTURE_CUBE_MAP_ARRAY:
90 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
91 return ctx->Extensions.ARB_texture_cube_map_array;
92 default:
93 return GL_FALSE;
94 }
95 default:
96 _mesa_problem(ctx, "invalid dims=%u in legal_texobj_target()", dims);
97 return GL_FALSE;
98 }
99 }
100
101
102 /**
103 * Compute the size of the next mipmap level.
104 */
105 static void
106 next_mipmap_level_size(GLenum target,
107 GLint *width, GLint *height, GLint *depth)
108 {
109 if (*width > 1) {
110 *width /= 2;
111 }
112
113 if ((*height > 1) && (target != GL_TEXTURE_1D_ARRAY)) {
114 *height /= 2;
115 }
116
117 if ((*depth > 1) && (target != GL_TEXTURE_2D_ARRAY)) {
118 *depth /= 2;
119 }
120 }
121
122
123 /** Helper to get a particular texture image in a texture object */
124 static struct gl_texture_image *
125 get_tex_image(struct gl_context *ctx,
126 struct gl_texture_object *texObj,
127 GLuint face, GLuint level)
128 {
129 const GLenum faceTarget =
130 (texObj->Target == GL_TEXTURE_CUBE_MAP ||
131 texObj->Target == GL_PROXY_TEXTURE_CUBE_MAP)
132 ? GL_TEXTURE_CUBE_MAP_POSITIVE_X + face : texObj->Target;
133 return _mesa_get_tex_image(ctx, texObj, faceTarget, level);
134 }
135
136
137
138 static GLboolean
139 initialize_texture_fields(struct gl_context *ctx,
140 struct gl_texture_object *texObj,
141 GLint levels,
142 GLsizei width, GLsizei height, GLsizei depth,
143 GLenum internalFormat, gl_format texFormat)
144 {
145 const GLenum target = texObj->Target;
146 const GLuint numFaces = _mesa_num_tex_faces(target);
147 GLint level, levelWidth = width, levelHeight = height, levelDepth = depth;
148 GLuint face;
149
150 /* Set up all the texture object's gl_texture_images */
151 for (level = 0; level < levels; level++) {
152 for (face = 0; face < numFaces; face++) {
153 struct gl_texture_image *texImage =
154 get_tex_image(ctx, texObj, face, level);
155
156 if (!texImage) {
157 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexStorage");
158 return GL_FALSE;
159 }
160
161 _mesa_init_teximage_fields(ctx, texImage,
162 levelWidth, levelHeight, levelDepth,
163 0, internalFormat, texFormat);
164 }
165
166 next_mipmap_level_size(target, &levelWidth, &levelHeight, &levelDepth);
167 }
168 return GL_TRUE;
169 }
170
171
172 /**
173 * Clear all fields of texture object to zeros. Used for proxy texture tests.
174 * Used for proxy texture tests (and to clean up when a texture memory
175 * allocation fails).
176 */
177 static void
178 clear_texture_fields(struct gl_context *ctx,
179 struct gl_texture_object *texObj)
180 {
181 const GLenum target = texObj->Target;
182 const GLuint numFaces = _mesa_num_tex_faces(target);
183 GLint level;
184 GLuint face;
185
186 for (level = 0; level < Elements(texObj->Image[0]); level++) {
187 for (face = 0; face < numFaces; face++) {
188 struct gl_texture_image *texImage =
189 get_tex_image(ctx, texObj, face, level);
190
191 if (!texImage) {
192 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexStorage");
193 return;
194 }
195
196 _mesa_init_teximage_fields(ctx, texImage,
197 0, 0, 0, 0, /* w, h, d, border */
198 GL_NONE, MESA_FORMAT_NONE);
199 }
200 }
201 }
202
203
204 GLboolean
205 _mesa_is_legal_tex_storage_format(struct gl_context *ctx, GLenum internalformat)
206 {
207 /* check internal format - note that only sized formats are allowed */
208 switch (internalformat) {
209 case GL_ALPHA:
210 case GL_LUMINANCE:
211 case GL_LUMINANCE_ALPHA:
212 case GL_INTENSITY:
213 case GL_RED:
214 case GL_RG:
215 case GL_RGB:
216 case GL_RGBA:
217 case GL_BGRA:
218 case GL_DEPTH_COMPONENT:
219 case GL_DEPTH_STENCIL:
220 case GL_COMPRESSED_ALPHA:
221 case GL_COMPRESSED_LUMINANCE_ALPHA:
222 case GL_COMPRESSED_LUMINANCE:
223 case GL_COMPRESSED_INTENSITY:
224 case GL_COMPRESSED_RGB:
225 case GL_COMPRESSED_RGBA:
226 case GL_COMPRESSED_SRGB:
227 case GL_COMPRESSED_SRGB_ALPHA:
228 case GL_COMPRESSED_SLUMINANCE:
229 case GL_COMPRESSED_SLUMINANCE_ALPHA:
230 case GL_RED_INTEGER:
231 case GL_GREEN_INTEGER:
232 case GL_BLUE_INTEGER:
233 case GL_ALPHA_INTEGER:
234 case GL_RGB_INTEGER:
235 case GL_RGBA_INTEGER:
236 case GL_BGR_INTEGER:
237 case GL_BGRA_INTEGER:
238 case GL_LUMINANCE_INTEGER_EXT:
239 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
240 /* these unsized formats are illegal */
241 return GL_FALSE;
242 default:
243 return _mesa_base_tex_format(ctx, internalformat) > 0;
244 }
245 }
246
247 /**
248 * Default ctx->Driver.AllocTextureStorage() handler.
249 *
250 * The driver can override this with a more specific implementation if it
251 * desires, but this can be used to get the texture images allocated using the
252 * usual texture image handling code. The immutability of
253 * GL_ARB_texture_storage texture layouts is handled by texObj->Immutable
254 * checks at glTexImage* time.
255 */
256 GLboolean
257 _mesa_alloc_texture_storage(struct gl_context *ctx,
258 struct gl_texture_object *texObj,
259 GLsizei levels, GLsizei width,
260 GLsizei height, GLsizei depth)
261 {
262 const int numFaces = _mesa_num_tex_faces(texObj->Target);
263 int face;
264 int level;
265
266 for (face = 0; face < numFaces; face++) {
267 for (level = 0; level < levels; level++) {
268 struct gl_texture_image *const texImage = texObj->Image[face][level];
269 if (!ctx->Driver.AllocTextureImageBuffer(ctx, texImage))
270 return GL_FALSE;
271 }
272 }
273
274 return GL_TRUE;
275 }
276
277
278 /**
279 * Do error checking for calls to glTexStorage1/2/3D().
280 * If an error is found, record it with _mesa_error(), unless the target
281 * is a proxy texture.
282 * \return GL_TRUE if any error, GL_FALSE otherwise.
283 */
284 static GLboolean
285 tex_storage_error_check(struct gl_context *ctx, GLuint dims, GLenum target,
286 GLsizei levels, GLenum internalformat,
287 GLsizei width, GLsizei height, GLsizei depth)
288 {
289 struct gl_texture_object *texObj;
290
291 if (!_mesa_is_legal_tex_storage_format(ctx, internalformat)) {
292 _mesa_error(ctx, GL_INVALID_ENUM,
293 "glTexStorage%uD(internalformat = %s)", dims,
294 _mesa_lookup_enum_by_nr(internalformat));
295 return GL_TRUE;
296 }
297
298 /* size check */
299 if (width < 1 || height < 1 || depth < 1) {
300 _mesa_error(ctx, GL_INVALID_VALUE,
301 "glTexStorage%uD(width, height or depth < 1)", dims);
302 return GL_TRUE;
303 }
304
305 /* target check */
306 if (!legal_texobj_target(ctx, dims, target)) {
307 _mesa_error(ctx, GL_INVALID_ENUM,
308 "glTexStorage%uD(illegal target=%s)",
309 dims, _mesa_lookup_enum_by_nr(target));
310 return GL_TRUE;
311 }
312
313 /* levels check */
314 if (levels < 1) {
315 _mesa_error(ctx, GL_INVALID_VALUE, "glTexStorage%uD(levels < 1)",
316 dims);
317 return GL_TRUE;
318 }
319
320 /* check levels against maximum (note different error than above) */
321 if (levels > (GLint) _mesa_max_texture_levels(ctx, target)) {
322 _mesa_error(ctx, GL_INVALID_OPERATION,
323 "glTexStorage%uD(levels too large)", dims);
324 return GL_TRUE;
325 }
326
327 /* check levels against width/height/depth */
328 if (levels > _mesa_get_tex_max_num_levels(target, width, height, depth)) {
329 _mesa_error(ctx, GL_INVALID_OPERATION,
330 "glTexStorage%uD(too many levels for max texture dimension)",
331 dims);
332 return GL_TRUE;
333 }
334
335 /* non-default texture object check */
336 texObj = _mesa_get_current_tex_object(ctx, target);
337 if (!texObj || (texObj->Name == 0)) {
338 _mesa_error(ctx, GL_INVALID_OPERATION,
339 "glTexStorage%uD(texture object 0)", dims);
340 return GL_TRUE;
341 }
342
343 /* Check if texObj->Immutable is set */
344 if (texObj->Immutable) {
345 _mesa_error(ctx, GL_INVALID_OPERATION, "glTexStorage%uD(immutable)",
346 dims);
347 return GL_TRUE;
348 }
349
350 return GL_FALSE;
351 }
352
353
354 /**
355 * Helper used by _mesa_TexStorage1/2/3D().
356 */
357 static void
358 texstorage(GLuint dims, GLenum target, GLsizei levels, GLenum internalformat,
359 GLsizei width, GLsizei height, GLsizei depth)
360 {
361 struct gl_texture_object *texObj;
362 GLboolean sizeOK, dimensionsOK;
363 gl_format texFormat;
364
365 GET_CURRENT_CONTEXT(ctx);
366
367 if (tex_storage_error_check(ctx, dims, target, levels,
368 internalformat, width, height, depth)) {
369 return; /* error was recorded */
370 }
371
372 texObj = _mesa_get_current_tex_object(ctx, target);
373 assert(texObj);
374
375 texFormat = _mesa_choose_texture_format(ctx, texObj, target, 0,
376 internalformat, GL_NONE, GL_NONE);
377 assert(texFormat != MESA_FORMAT_NONE);
378
379 /* check that width, height, depth are legal for the mipmap level */
380 dimensionsOK = _mesa_legal_texture_dimensions(ctx, target, 0,
381 width, height, depth, 0);
382
383 sizeOK = ctx->Driver.TestProxyTexImage(ctx, target, 0, texFormat,
384 width, height, depth, 0);
385
386 if (_mesa_is_proxy_texture(texObj->Target)) {
387 if (dimensionsOK && sizeOK) {
388 initialize_texture_fields(ctx, texObj, levels, width, height, depth,
389 internalformat, texFormat);
390 }
391 else {
392 /* clear all image fields for [levels] */
393 clear_texture_fields(ctx, texObj);
394 }
395 }
396 else {
397 if (!dimensionsOK) {
398 _mesa_error(ctx, GL_INVALID_VALUE,
399 "glTexStorage%uD(invalid width, height or depth)", dims);
400 return;
401 }
402
403 if (!sizeOK) {
404 _mesa_error(ctx, GL_OUT_OF_MEMORY,
405 "glTexStorage%uD(texture too large)", dims);
406 }
407
408 assert(levels > 0);
409 assert(width > 0);
410 assert(height > 0);
411 assert(depth > 0);
412
413 if (!initialize_texture_fields(ctx, texObj, levels, width, height, depth,
414 internalformat, texFormat)) {
415 return;
416 }
417
418 /* Do actual texture memory allocation */
419 if (!ctx->Driver.AllocTextureStorage(ctx, texObj, levels,
420 width, height, depth)) {
421 /* Reset the texture images' info to zeros.
422 * Strictly speaking, we probably don't have to do this since
423 * generating GL_OUT_OF_MEMORY can leave things in an undefined
424 * state but this puts things in a consistent state.
425 */
426 clear_texture_fields(ctx, texObj);
427 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexStorage%uD", dims);
428 return;
429 }
430
431 texObj->Immutable = GL_TRUE;
432 texObj->ImmutableLevels = levels;
433 }
434 }
435
436
437 void GLAPIENTRY
438 _mesa_TexStorage1D(GLenum target, GLsizei levels, GLenum internalformat,
439 GLsizei width)
440 {
441 texstorage(1, target, levels, internalformat, width, 1, 1);
442 }
443
444
445 void GLAPIENTRY
446 _mesa_TexStorage2D(GLenum target, GLsizei levels, GLenum internalformat,
447 GLsizei width, GLsizei height)
448 {
449 texstorage(2, target, levels, internalformat, width, height, 1);
450 }
451
452
453 void GLAPIENTRY
454 _mesa_TexStorage3D(GLenum target, GLsizei levels, GLenum internalformat,
455 GLsizei width, GLsizei height, GLsizei depth)
456 {
457 texstorage(3, target, levels, internalformat, width, height, depth);
458 }
459
460
461
462 /*
463 * Note: we don't support GL_EXT_direct_state_access and the spec says
464 * we don't need the following functions. However, glew checks for the
465 * presence of all six functions and will say that GL_ARB_texture_storage
466 * is not supported if these functions are missing.
467 */
468
469
470 void GLAPIENTRY
471 _mesa_TextureStorage1DEXT(GLuint texture, GLenum target, GLsizei levels,
472 GLenum internalformat,
473 GLsizei width)
474 {
475 /* no-op */
476 }
477
478
479 void GLAPIENTRY
480 _mesa_TextureStorage2DEXT(GLuint texture, GLenum target, GLsizei levels,
481 GLenum internalformat,
482 GLsizei width, GLsizei height)
483 {
484 /* no-op */
485 }
486
487
488
489 void GLAPIENTRY
490 _mesa_TextureStorage3DEXT(GLuint texture, GLenum target, GLsizei levels,
491 GLenum internalformat,
492 GLsizei width, GLsizei height, GLsizei depth)
493 {
494 /* no-op */
495 }