1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
|
/*
* Mesa 3-D graphics library
*
* Copyright (C) 2011 VMware, Inc. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* \file texstorage.c
* GL_ARB_texture_storage functions
*/
#include "glheader.h"
#include "context.h"
#include "enums.h"
#include "imports.h"
#include "macros.h"
#include "mfeatures.h"
#include "teximage.h"
#include "texstorage.h"
#include "mtypes.h"
/**
* Check if the given texture target is a legal texture object target
* for a glTexStorage() command.
* This is a bit different than legal_teximage_target() when it comes
* to cube maps.
*/
static GLboolean
legal_texobj_target(struct gl_context *ctx, GLuint dims, GLenum target)
{
switch (dims) {
case 1:
switch (target) {
case GL_TEXTURE_1D:
case GL_PROXY_TEXTURE_1D:
return GL_TRUE;
default:
return GL_FALSE;
}
case 2:
switch (target) {
case GL_TEXTURE_2D:
case GL_PROXY_TEXTURE_2D:
return GL_TRUE;
case GL_TEXTURE_CUBE_MAP:
case GL_PROXY_TEXTURE_CUBE_MAP:
return ctx->Extensions.ARB_texture_cube_map;
case GL_TEXTURE_RECTANGLE:
case GL_PROXY_TEXTURE_RECTANGLE:
return ctx->Extensions.NV_texture_rectangle;
case GL_TEXTURE_1D_ARRAY:
case GL_PROXY_TEXTURE_1D_ARRAY:
return (ctx->Extensions.MESA_texture_array ||
ctx->Extensions.EXT_texture_array);
default:
return GL_FALSE;
}
case 3:
switch (target) {
case GL_TEXTURE_3D:
case GL_PROXY_TEXTURE_3D:
return GL_TRUE;
case GL_TEXTURE_2D_ARRAY:
case GL_PROXY_TEXTURE_2D_ARRAY:
return (ctx->Extensions.MESA_texture_array ||
ctx->Extensions.EXT_texture_array);
default:
return GL_FALSE;
}
default:
_mesa_problem(ctx, "invalid dims=%u in legal_texobj_target()", dims);
return GL_FALSE;
}
}
/**
* Compute the size of the next mipmap level.
*/
static void
next_mipmap_level_size(GLenum target,
GLint *width, GLint *height, GLint *depth)
{
if (*width > 1) {
*width /= 2;
}
if ((*height > 1) && (target != GL_TEXTURE_1D_ARRAY)) {
*height /= 2;
}
if ((*depth > 1) && (target != GL_TEXTURE_2D_ARRAY)) {
*depth /= 2;
}
}
/**
* Do actual memory allocation for glTexStorage1/2/3D().
*/
static void
setup_texstorage(struct gl_context *ctx,
struct gl_texture_object *texObj,
GLuint dims,
GLsizei levels, GLenum internalFormat,
GLsizei width, GLsizei height, GLsizei depth)
{
const GLenum target = texObj->Target;
const GLuint numFaces = (target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
gl_format texFormat;
GLint level, levelWidth = width, levelHeight = height, levelDepth = depth;
GLuint face;
assert(levels > 0);
assert(width > 0);
assert(height > 0);
assert(depth > 0);
texFormat = _mesa_choose_texture_format(ctx, texObj, target, 0,
internalFormat, GL_NONE, GL_NONE);
/* Set up all the texture object's gl_texture_images */
for (level = 0; level < levels; level++) {
for (face = 0; face < numFaces; face++) {
const GLenum faceTarget =
(target == GL_TEXTURE_CUBE_MAP)
? GL_TEXTURE_CUBE_MAP_POSITIVE_X + face : target;
struct gl_texture_image *texImage =
_mesa_get_tex_image(ctx, texObj, faceTarget, level);
if (!texImage) {
_mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage%uD", dims);
return;
}
_mesa_init_teximage_fields(ctx, target, texImage,
levelWidth, levelHeight, levelDepth,
0, internalFormat, texFormat);
}
next_mipmap_level_size(target, &levelWidth, &levelHeight, &levelDepth);
}
assert(levelWidth > 0);
assert(levelHeight > 0);
assert(levelDepth > 0);
if (!_mesa_is_proxy_texture(texObj->Target)) {
/* Do actual texture memory allocation */
if (!ctx->Driver.AllocTextureStorage(ctx, texObj, levels,
width, height, depth)) {
/* Reset the texture images' info to zeros.
* Strictly speaking, we probably don't have to do this since
* generating GL_OUT_OF_MEMORY can leave things in an undefined
* state but this puts things in a consistent state.
*/
for (level = 0; level < levels; level++) {
for (face = 0; face < numFaces; face++) {
struct gl_texture_image *texImage = texObj->Image[face][level];
if (texImage) {
_mesa_init_teximage_fields(ctx, target, texImage,
0, 0, 0, 0,
GL_NONE, MESA_FORMAT_NONE);
}
}
}
_mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexStorage%uD", dims);
return;
}
}
texObj->Immutable = GL_TRUE;
}
/**
* Clear all fields of texture object to zeros. Used for proxy texture tests.
*/
static void
clear_image_fields(struct gl_context *ctx,
GLuint dims,
struct gl_texture_object *texObj)
{
const GLenum target = texObj->Target;
const GLuint numFaces = (target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
GLint level;
GLuint face;
for (level = 0; level < Elements(texObj->Image[0]); level++) {
for (face = 0; face < numFaces; face++) {
const GLenum faceTarget =
(target == GL_TEXTURE_CUBE_MAP)
? GL_TEXTURE_CUBE_MAP_POSITIVE_X + face : target;
struct gl_texture_image *texImage =
_mesa_get_tex_image(ctx, texObj, faceTarget, level);
if (!texImage) {
_mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexStorage%uD", dims);
return;
}
_mesa_init_teximage_fields(ctx, target, texImage,
0, 0, 0, 0, GL_NONE, MESA_FORMAT_NONE);
}
}
}
/**
* Do error checking for calls to glTexStorage1/2/3D().
* If an error is found, record it with _mesa_error(), unless the target
* is a proxy texture.
* \return GL_TRUE if any error, GL_FALSE otherwise.
*/
static GLboolean
tex_storage_error_check(struct gl_context *ctx, GLuint dims, GLenum target,
GLsizei levels, GLenum internalformat,
GLsizei width, GLsizei height, GLsizei depth)
{
const GLboolean isProxy = _mesa_is_proxy_texture(target);
struct gl_texture_object *texObj;
GLuint maxDim;
/* size check */
if (width < 1 || height < 1 || depth < 1) {
if (!isProxy) {
_mesa_error(ctx, GL_INVALID_VALUE,
"glTexStorage%uD(width, height or depth < 1)", dims);
}
return GL_TRUE;
}
/* levels check */
if (levels < 1 || height < 1 || depth < 1) {
if (!isProxy) {
_mesa_error(ctx, GL_INVALID_VALUE, "glTexStorage%uD(levels < 1)",
dims);
}
return GL_TRUE;
}
/* target check */
if (!legal_texobj_target(ctx, dims, target)) {
_mesa_error(ctx, GL_INVALID_ENUM,
"glTexStorage%uD(illegal target=%s)",
dims, _mesa_lookup_enum_by_nr(target));
return GL_TRUE;
}
/* check levels against maximum */
if (levels > _mesa_max_texture_levels(ctx, target)) {
if (!isProxy) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glTexStorage%uD(levels too large)", dims);
}
return GL_TRUE;
}
/* check levels against width/height/depth */
maxDim = MAX3(width, height, depth);
if (levels > _mesa_logbase2(maxDim) + 1) {
if (!isProxy) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glTexStorage%uD(too many levels for max texture dimension)",
dims);
}
return GL_TRUE;
}
/* non-default texture object check */
texObj = _mesa_get_current_tex_object(ctx, target);
if (!texObj || (texObj->Name == 0 && !isProxy)) {
if (!isProxy) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glTexStorage%uD(texture object 0)", dims);
}
return GL_TRUE;
}
/* Check if texObj->Immutable is set */
if (texObj->Immutable) {
if (!isProxy) {
_mesa_error(ctx, GL_INVALID_OPERATION, "glTexStorage%uD(immutable)",
dims);
}
return GL_TRUE;
}
return GL_FALSE;
}
/**
* Helper used by _mesa_TexStorage1/2/3D().
*/
static void
texstorage(GLuint dims, GLenum target, GLsizei levels, GLenum internalformat,
GLsizei width, GLsizei height, GLsizei depth)
{
struct gl_texture_object *texObj;
GLboolean error;
GET_CURRENT_CONTEXT(ctx);
texObj = _mesa_get_current_tex_object(ctx, target);
error = tex_storage_error_check(ctx, dims, target, levels,
internalformat, width, height, depth);
if (!error) {
setup_texstorage(ctx, texObj, dims, levels, internalformat,
width, height, depth);
}
else if (_mesa_is_proxy_texture(target)) {
/* clear all image fields for [levels] */
clear_image_fields(ctx, dims, texObj);
}
}
void GLAPIENTRY
_mesa_TexStorage1D(GLenum target, GLsizei levels, GLenum internalformat,
GLsizei width)
{
texstorage(1, target, levels, internalformat, width, 1, 1);
}
void GLAPIENTRY
_mesa_TexStorage2D(GLenum target, GLsizei levels, GLenum internalformat,
GLsizei width, GLsizei height)
{
texstorage(2, target, levels, internalformat, width, height, 1);
}
void GLAPIENTRY
_mesa_TexStorage3D(GLenum target, GLsizei levels, GLenum internalformat,
GLsizei width, GLsizei height, GLsizei depth)
{
texstorage(3, target, levels, internalformat, width, height, depth);
}
/*
* Note: we don't support GL_EXT_direct_state_access and the spec says
* we don't need the following functions. However, glew checks for the
* presence of all six functions and will say that GL_ARB_texture_storage
* is not supported if these functions are missing.
*/
void GLAPIENTRY
_mesa_TextureStorage1DEXT(GLuint texture, GLenum target, GLsizei levels,
GLenum internalformat,
GLsizei width)
{
/* no-op */
}
void GLAPIENTRY
_mesa_TextureStorage2DEXT(GLuint texture, GLenum target, GLsizei levels,
GLenum internalformat,
GLsizei width, GLsizei height)
{
/* no-op */
}
void GLAPIENTRY
_mesa_TextureStorage3DEXT(GLuint texture, GLenum target, GLsizei levels,
GLenum internalformat,
GLsizei width, GLsizei height, GLsizei depth)
{
/* no-op */
}
|