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