• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Mesa 3-D graphics library
3  *
4  * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
5  * Copyright (C) 2009  VMware, Inc.  All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  * and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23  * OTHER DEALINGS IN THE SOFTWARE.
24  */
25 
26 
27 /**
28  * \file teximage.c
29  * Texture image-related functions.
30  */
31 
32 #include <stdbool.h>
33 #include "glheader.h"
34 #include "bufferobj.h"
35 #include "context.h"
36 #include "enums.h"
37 #include "fbobject.h"
38 #include "framebuffer.h"
39 #include "hash.h"
40 #include "image.h"
41 
42 #include "macros.h"
43 #include "mipmap.h"
44 #include "multisample.h"
45 #include "pixelstore.h"
46 #include "state.h"
47 #include "texcompress.h"
48 #include "texcompress_cpal.h"
49 #include "teximage.h"
50 #include "texobj.h"
51 #include "texstate.h"
52 #include "texstorage.h"
53 #include "textureview.h"
54 #include "mtypes.h"
55 #include "glformats.h"
56 #include "texstore.h"
57 #include "pbo.h"
58 
59 
60 /**
61  * State changes which we care about for glCopyTex[Sub]Image() calls.
62  * In particular, we care about pixel transfer state and buffer state
63  * (such as glReadBuffer to make sure we read from the right renderbuffer).
64  */
65 #define NEW_COPY_TEX_STATE (_NEW_BUFFERS | _NEW_PIXEL)
66 
67 /**
68  * Returns a corresponding internal floating point format for a given base
69  * format as specifed by OES_texture_float. In case of GL_FLOAT, the internal
70  * format needs to be a 32 bit component and in case of GL_HALF_FLOAT_OES it
71  * needs to be a 16 bit component.
72  *
73  * For example, given base format GL_RGBA, type GL_FLOAT return GL_RGBA32F_ARB.
74  */
75 static GLenum
adjust_for_oes_float_texture(const struct gl_context * ctx,GLenum format,GLenum type)76 adjust_for_oes_float_texture(const struct gl_context *ctx,
77                              GLenum format, GLenum type)
78 {
79    switch (type) {
80    case GL_FLOAT:
81       if (ctx->Extensions.OES_texture_float) {
82          switch (format) {
83          case GL_RGBA:
84             return GL_RGBA32F;
85          case GL_RGB:
86             return GL_RGB32F;
87          case GL_ALPHA:
88             return GL_ALPHA32F_ARB;
89          case GL_LUMINANCE:
90             return GL_LUMINANCE32F_ARB;
91          case GL_LUMINANCE_ALPHA:
92             return GL_LUMINANCE_ALPHA32F_ARB;
93          default:
94             break;
95          }
96       }
97       break;
98 
99    case GL_HALF_FLOAT_OES:
100       if (ctx->Extensions.OES_texture_half_float) {
101          switch (format) {
102          case GL_RGBA:
103             return GL_RGBA16F;
104          case GL_RGB:
105             return GL_RGB16F;
106          case GL_ALPHA:
107             return GL_ALPHA16F_ARB;
108          case GL_LUMINANCE:
109             return GL_LUMINANCE16F_ARB;
110          case GL_LUMINANCE_ALPHA:
111             return GL_LUMINANCE_ALPHA16F_ARB;
112          default:
113             break;
114          }
115       }
116       break;
117 
118    default:
119       break;
120    }
121 
122    return format;
123 }
124 
125 /**
126  * Returns a corresponding base format for a given internal floating point
127  * format as specifed by OES_texture_float.
128  */
129 static GLenum
oes_float_internal_format(const struct gl_context * ctx,GLenum format,GLenum type)130 oes_float_internal_format(const struct gl_context *ctx,
131                           GLenum format, GLenum type)
132 {
133    switch (type) {
134    case GL_FLOAT:
135       if (ctx->Extensions.OES_texture_float) {
136          switch (format) {
137          case GL_RGBA32F:
138             return GL_RGBA;
139          case GL_RGB32F:
140             return GL_RGB;
141          case GL_ALPHA32F_ARB:
142             return GL_ALPHA;
143          case GL_LUMINANCE32F_ARB:
144             return GL_LUMINANCE;
145          case GL_LUMINANCE_ALPHA32F_ARB:
146             return GL_LUMINANCE_ALPHA;
147          default:
148             break;
149          }
150       }
151       break;
152 
153    case GL_HALF_FLOAT_OES:
154       if (ctx->Extensions.OES_texture_half_float) {
155          switch (format) {
156          case GL_RGBA16F:
157             return GL_RGBA;
158          case GL_RGB16F:
159             return GL_RGB;
160          case GL_ALPHA16F_ARB:
161             return GL_ALPHA;
162          case GL_LUMINANCE16F_ARB:
163             return GL_LUMINANCE;
164          case GL_LUMINANCE_ALPHA16F_ARB:
165             return GL_LUMINANCE_ALPHA;
166          default:
167             break;
168          }
169       }
170       break;
171    }
172    return format;
173 }
174 
175 
176 /**
177  * Install gl_texture_image in a gl_texture_object according to the target
178  * and level parameters.
179  *
180  * \param tObj texture object.
181  * \param target texture target.
182  * \param level image level.
183  * \param texImage texture image.
184  */
185 static void
set_tex_image(struct gl_texture_object * tObj,GLenum target,GLint level,struct gl_texture_image * texImage)186 set_tex_image(struct gl_texture_object *tObj,
187               GLenum target, GLint level,
188               struct gl_texture_image *texImage)
189 {
190    const GLuint face = _mesa_tex_target_to_face(target);
191 
192    assert(tObj);
193    assert(texImage);
194    if (target == GL_TEXTURE_RECTANGLE_NV || target == GL_TEXTURE_EXTERNAL_OES)
195       assert(level == 0);
196 
197    tObj->Image[face][level] = texImage;
198 
199    /* Set the 'back' pointer */
200    texImage->TexObject = tObj;
201    texImage->Level = level;
202    texImage->Face = face;
203 }
204 
205 
206 /**
207  * Free a gl_texture_image and associated data.
208  * This function is a fallback called via ctx->Driver.DeleteTextureImage().
209  *
210  * \param texImage texture image.
211  *
212  * Free the texture image structure and the associated image data.
213  */
214 void
_mesa_delete_texture_image(struct gl_context * ctx,struct gl_texture_image * texImage)215 _mesa_delete_texture_image(struct gl_context *ctx,
216                            struct gl_texture_image *texImage)
217 {
218    /* Free texImage->Data and/or any other driver-specific texture
219     * image storage.
220     */
221    assert(ctx->Driver.FreeTextureImageBuffer);
222    ctx->Driver.FreeTextureImageBuffer( ctx, texImage );
223    free(texImage);
224 }
225 
226 
227 /**
228  * Test if a target is a proxy target.
229  *
230  * \param target texture target.
231  *
232  * \return GL_TRUE if the target is a proxy target, GL_FALSE otherwise.
233  */
234 GLboolean
_mesa_is_proxy_texture(GLenum target)235 _mesa_is_proxy_texture(GLenum target)
236 {
237    unsigned i;
238    static const GLenum targets[] = {
239       GL_PROXY_TEXTURE_1D,
240       GL_PROXY_TEXTURE_2D,
241       GL_PROXY_TEXTURE_3D,
242       GL_PROXY_TEXTURE_CUBE_MAP,
243       GL_PROXY_TEXTURE_RECTANGLE,
244       GL_PROXY_TEXTURE_1D_ARRAY,
245       GL_PROXY_TEXTURE_2D_ARRAY,
246       GL_PROXY_TEXTURE_CUBE_MAP_ARRAY,
247       GL_PROXY_TEXTURE_2D_MULTISAMPLE,
248       GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
249    };
250    /*
251     * NUM_TEXTURE_TARGETS should match number of terms above, except there's no
252     * proxy for GL_TEXTURE_BUFFER and GL_TEXTURE_EXTERNAL_OES.
253     */
254    STATIC_ASSERT(NUM_TEXTURE_TARGETS == ARRAY_SIZE(targets) + 2);
255 
256    for (i = 0; i < ARRAY_SIZE(targets); ++i)
257       if (target == targets[i])
258          return GL_TRUE;
259    return GL_FALSE;
260 }
261 
262 
263 /**
264  * Test if a target is an array target.
265  *
266  * \param target texture target.
267  *
268  * \return true if the target is an array target, false otherwise.
269  */
270 bool
_mesa_is_array_texture(GLenum target)271 _mesa_is_array_texture(GLenum target)
272 {
273    switch (target) {
274    case GL_TEXTURE_1D_ARRAY:
275    case GL_TEXTURE_2D_ARRAY:
276    case GL_TEXTURE_CUBE_MAP_ARRAY:
277    case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
278       return true;
279    default:
280       return false;
281    };
282 }
283 
284 /**
285  * Test if a target is a cube map.
286  *
287  * \param target texture target.
288  *
289  * \return true if the target is a cube map, false otherwise.
290  */
291 bool
_mesa_is_cube_map_texture(GLenum target)292 _mesa_is_cube_map_texture(GLenum target)
293 {
294    switch(target) {
295    case GL_TEXTURE_CUBE_MAP:
296    case GL_TEXTURE_CUBE_MAP_ARRAY:
297       return true;
298    default:
299       return false;
300    }
301 }
302 
303 /**
304  * Return the proxy target which corresponds to the given texture target
305  */
306 static GLenum
proxy_target(GLenum target)307 proxy_target(GLenum target)
308 {
309    switch (target) {
310    case GL_TEXTURE_1D:
311    case GL_PROXY_TEXTURE_1D:
312       return GL_PROXY_TEXTURE_1D;
313    case GL_TEXTURE_2D:
314    case GL_PROXY_TEXTURE_2D:
315       return GL_PROXY_TEXTURE_2D;
316    case GL_TEXTURE_3D:
317    case GL_PROXY_TEXTURE_3D:
318       return GL_PROXY_TEXTURE_3D;
319    case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
320    case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
321    case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
322    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
323    case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
324    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
325    case GL_TEXTURE_CUBE_MAP:
326    case GL_PROXY_TEXTURE_CUBE_MAP:
327       return GL_PROXY_TEXTURE_CUBE_MAP;
328    case GL_TEXTURE_RECTANGLE_NV:
329    case GL_PROXY_TEXTURE_RECTANGLE_NV:
330       return GL_PROXY_TEXTURE_RECTANGLE_NV;
331    case GL_TEXTURE_1D_ARRAY_EXT:
332    case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
333       return GL_PROXY_TEXTURE_1D_ARRAY_EXT;
334    case GL_TEXTURE_2D_ARRAY_EXT:
335    case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
336       return GL_PROXY_TEXTURE_2D_ARRAY_EXT;
337    case GL_TEXTURE_CUBE_MAP_ARRAY:
338    case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
339       return GL_PROXY_TEXTURE_CUBE_MAP_ARRAY;
340    case GL_TEXTURE_2D_MULTISAMPLE:
341    case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
342       return GL_PROXY_TEXTURE_2D_MULTISAMPLE;
343    case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
344    case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
345       return GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY;
346    default:
347       _mesa_problem(NULL, "unexpected target in proxy_target()");
348       return 0;
349    }
350 }
351 
352 
353 
354 
355 /**
356  * Get a texture image pointer from a texture object, given a texture
357  * target and mipmap level.  The target and level parameters should
358  * have already been error-checked.
359  *
360  * \param texObj texture unit.
361  * \param target texture target.
362  * \param level image level.
363  *
364  * \return pointer to the texture image structure, or NULL on failure.
365  */
366 struct gl_texture_image *
_mesa_select_tex_image(const struct gl_texture_object * texObj,GLenum target,GLint level)367 _mesa_select_tex_image(const struct gl_texture_object *texObj,
368 		                 GLenum target, GLint level)
369 {
370    const GLuint face = _mesa_tex_target_to_face(target);
371 
372    assert(texObj);
373    assert(level >= 0);
374    assert(level < MAX_TEXTURE_LEVELS);
375 
376    return texObj->Image[face][level];
377 }
378 
379 
380 /**
381  * Like _mesa_select_tex_image() but if the image doesn't exist, allocate
382  * it and install it.  Only return NULL if passed a bad parameter or run
383  * out of memory.
384  */
385 struct gl_texture_image *
_mesa_get_tex_image(struct gl_context * ctx,struct gl_texture_object * texObj,GLenum target,GLint level)386 _mesa_get_tex_image(struct gl_context *ctx, struct gl_texture_object *texObj,
387                     GLenum target, GLint level)
388 {
389    struct gl_texture_image *texImage;
390 
391    if (!texObj)
392       return NULL;
393 
394    texImage = _mesa_select_tex_image(texObj, target, level);
395    if (!texImage) {
396       texImage = ctx->Driver.NewTextureImage(ctx);
397       if (!texImage) {
398          _mesa_error(ctx, GL_OUT_OF_MEMORY, "texture image allocation");
399          return NULL;
400       }
401 
402       set_tex_image(texObj, target, level, texImage);
403    }
404 
405    return texImage;
406 }
407 
408 
409 /**
410  * Return pointer to the specified proxy texture image.
411  * Note that proxy textures are per-context, not per-texture unit.
412  * \return pointer to texture image or NULL if invalid target, invalid
413  *         level, or out of memory.
414  */
415 static struct gl_texture_image *
get_proxy_tex_image(struct gl_context * ctx,GLenum target,GLint level)416 get_proxy_tex_image(struct gl_context *ctx, GLenum target, GLint level)
417 {
418    struct gl_texture_image *texImage;
419    GLuint texIndex;
420 
421    if (level < 0)
422       return NULL;
423 
424    switch (target) {
425    case GL_PROXY_TEXTURE_1D:
426       texIndex = TEXTURE_1D_INDEX;
427       break;
428    case GL_PROXY_TEXTURE_2D:
429       texIndex = TEXTURE_2D_INDEX;
430       break;
431    case GL_PROXY_TEXTURE_3D:
432       texIndex = TEXTURE_3D_INDEX;
433       break;
434    case GL_PROXY_TEXTURE_CUBE_MAP:
435       texIndex = TEXTURE_CUBE_INDEX;
436       break;
437    case GL_PROXY_TEXTURE_RECTANGLE_NV:
438       if (level > 0)
439          return NULL;
440       texIndex = TEXTURE_RECT_INDEX;
441       break;
442    case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
443       texIndex = TEXTURE_1D_ARRAY_INDEX;
444       break;
445    case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
446       texIndex = TEXTURE_2D_ARRAY_INDEX;
447       break;
448    case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
449       texIndex = TEXTURE_CUBE_ARRAY_INDEX;
450       break;
451    case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
452       texIndex = TEXTURE_2D_MULTISAMPLE_INDEX;
453       break;
454    case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
455       texIndex = TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX;
456       break;
457    default:
458       return NULL;
459    }
460 
461    texImage = ctx->Texture.ProxyTex[texIndex]->Image[0][level];
462    if (!texImage) {
463       texImage = ctx->Driver.NewTextureImage(ctx);
464       if (!texImage) {
465          _mesa_error(ctx, GL_OUT_OF_MEMORY, "proxy texture allocation");
466          return NULL;
467       }
468       ctx->Texture.ProxyTex[texIndex]->Image[0][level] = texImage;
469       /* Set the 'back' pointer */
470       texImage->TexObject = ctx->Texture.ProxyTex[texIndex];
471    }
472    return texImage;
473 }
474 
475 
476 /**
477  * Get the maximum number of allowed mipmap levels.
478  *
479  * \param ctx GL context.
480  * \param target texture target.
481  *
482  * \return the maximum number of allowed mipmap levels for the given
483  * texture target, or zero if passed a bad target.
484  *
485  * \sa gl_constants.
486  */
487 GLint
_mesa_max_texture_levels(const struct gl_context * ctx,GLenum target)488 _mesa_max_texture_levels(const struct gl_context *ctx, GLenum target)
489 {
490    switch (target) {
491    case GL_TEXTURE_1D:
492    case GL_PROXY_TEXTURE_1D:
493    case GL_TEXTURE_2D:
494    case GL_PROXY_TEXTURE_2D:
495       return ffs(util_next_power_of_two(ctx->Const.MaxTextureSize));
496    case GL_TEXTURE_3D:
497    case GL_PROXY_TEXTURE_3D:
498       return ctx->Const.Max3DTextureLevels;
499    case GL_TEXTURE_CUBE_MAP:
500    case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
501    case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
502    case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
503    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
504    case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
505    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
506    case GL_PROXY_TEXTURE_CUBE_MAP:
507       return ctx->Extensions.ARB_texture_cube_map
508          ? ctx->Const.MaxCubeTextureLevels : 0;
509    case GL_TEXTURE_RECTANGLE_NV:
510    case GL_PROXY_TEXTURE_RECTANGLE_NV:
511       return ctx->Extensions.NV_texture_rectangle ? 1 : 0;
512    case GL_TEXTURE_1D_ARRAY_EXT:
513    case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
514    case GL_TEXTURE_2D_ARRAY_EXT:
515    case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
516       return ctx->Extensions.EXT_texture_array
517          ? ffs(util_next_power_of_two(ctx->Const.MaxTextureSize)) : 0;
518    case GL_TEXTURE_CUBE_MAP_ARRAY:
519    case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
520       return _mesa_has_texture_cube_map_array(ctx)
521          ? ctx->Const.MaxCubeTextureLevels : 0;
522    case GL_TEXTURE_BUFFER:
523       return (_mesa_has_ARB_texture_buffer_object(ctx) ||
524               _mesa_has_OES_texture_buffer(ctx)) ? 1 : 0;
525    case GL_TEXTURE_2D_MULTISAMPLE:
526    case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
527    case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
528    case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
529       return (_mesa_is_desktop_gl(ctx) || _mesa_is_gles31(ctx))
530          && ctx->Extensions.ARB_texture_multisample
531          ? 1 : 0;
532    case GL_TEXTURE_EXTERNAL_OES:
533       return _mesa_has_OES_EGL_image_external(ctx) ? 1 : 0;
534    default:
535       return 0; /* bad target */
536    }
537 }
538 
539 
540 /**
541  * Return number of dimensions per mipmap level for the given texture target.
542  */
543 GLint
_mesa_get_texture_dimensions(GLenum target)544 _mesa_get_texture_dimensions(GLenum target)
545 {
546    switch (target) {
547    case GL_TEXTURE_1D:
548    case GL_PROXY_TEXTURE_1D:
549       return 1;
550    case GL_TEXTURE_2D:
551    case GL_TEXTURE_RECTANGLE:
552    case GL_TEXTURE_CUBE_MAP:
553    case GL_PROXY_TEXTURE_2D:
554    case GL_PROXY_TEXTURE_RECTANGLE:
555    case GL_PROXY_TEXTURE_CUBE_MAP:
556    case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
557    case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
558    case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
559    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
560    case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
561    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
562    case GL_TEXTURE_1D_ARRAY:
563    case GL_PROXY_TEXTURE_1D_ARRAY:
564    case GL_TEXTURE_EXTERNAL_OES:
565    case GL_TEXTURE_2D_MULTISAMPLE:
566    case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
567       return 2;
568    case GL_TEXTURE_3D:
569    case GL_PROXY_TEXTURE_3D:
570    case GL_TEXTURE_2D_ARRAY:
571    case GL_PROXY_TEXTURE_2D_ARRAY:
572    case GL_TEXTURE_CUBE_MAP_ARRAY:
573    case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
574    case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
575    case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
576       return 3;
577    case GL_TEXTURE_BUFFER:
578       /* fall-through */
579    default:
580       _mesa_problem(NULL, "invalid target 0x%x in get_texture_dimensions()",
581                     target);
582       return 2;
583    }
584 }
585 
586 
587 /**
588  * Check if a texture target can have more than one layer.
589  */
590 GLboolean
_mesa_tex_target_is_layered(GLenum target)591 _mesa_tex_target_is_layered(GLenum target)
592 {
593    switch (target) {
594    case GL_TEXTURE_1D:
595    case GL_PROXY_TEXTURE_1D:
596    case GL_TEXTURE_2D:
597    case GL_PROXY_TEXTURE_2D:
598    case GL_TEXTURE_RECTANGLE:
599    case GL_PROXY_TEXTURE_RECTANGLE:
600    case GL_TEXTURE_2D_MULTISAMPLE:
601    case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
602    case GL_TEXTURE_BUFFER:
603    case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
604    case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
605    case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
606    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
607    case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
608    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
609    case GL_TEXTURE_EXTERNAL_OES:
610       return GL_FALSE;
611 
612    case GL_TEXTURE_3D:
613    case GL_PROXY_TEXTURE_3D:
614    case GL_TEXTURE_CUBE_MAP:
615    case GL_PROXY_TEXTURE_CUBE_MAP:
616    case GL_TEXTURE_1D_ARRAY:
617    case GL_PROXY_TEXTURE_1D_ARRAY:
618    case GL_TEXTURE_2D_ARRAY:
619    case GL_PROXY_TEXTURE_2D_ARRAY:
620    case GL_TEXTURE_CUBE_MAP_ARRAY:
621    case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
622    case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
623    case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
624       return GL_TRUE;
625 
626    default:
627       assert(!"Invalid texture target.");
628       return GL_FALSE;
629    }
630 }
631 
632 
633 /**
634  * Return the number of layers present in the given level of an array,
635  * cubemap or 3D texture.  If the texture is not layered return zero.
636  */
637 GLuint
_mesa_get_texture_layers(const struct gl_texture_object * texObj,GLint level)638 _mesa_get_texture_layers(const struct gl_texture_object *texObj, GLint level)
639 {
640    assert(level >= 0 && level < MAX_TEXTURE_LEVELS);
641 
642    switch (texObj->Target) {
643    case GL_TEXTURE_1D:
644    case GL_TEXTURE_2D:
645    case GL_TEXTURE_RECTANGLE:
646    case GL_TEXTURE_2D_MULTISAMPLE:
647    case GL_TEXTURE_BUFFER:
648    case GL_TEXTURE_EXTERNAL_OES:
649       return 0;
650 
651    case GL_TEXTURE_CUBE_MAP:
652       return 6;
653 
654    case GL_TEXTURE_1D_ARRAY: {
655       struct gl_texture_image *img = texObj->Image[0][level];
656       return img ? img->Height : 0;
657    }
658 
659    case GL_TEXTURE_3D:
660    case GL_TEXTURE_2D_ARRAY:
661    case GL_TEXTURE_CUBE_MAP_ARRAY:
662    case GL_TEXTURE_2D_MULTISAMPLE_ARRAY: {
663       struct gl_texture_image *img = texObj->Image[0][level];
664       return img ? img->Depth : 0;
665    }
666 
667    default:
668       assert(!"Invalid texture target.");
669       return 0;
670    }
671 }
672 
673 
674 /**
675  * Return the maximum number of mipmap levels for the given target
676  * and the dimensions.
677  * The dimensions are expected not to include the border.
678  */
679 GLsizei
_mesa_get_tex_max_num_levels(GLenum target,GLsizei width,GLsizei height,GLsizei depth)680 _mesa_get_tex_max_num_levels(GLenum target, GLsizei width, GLsizei height,
681                              GLsizei depth)
682 {
683    GLsizei size;
684 
685    switch (target) {
686    case GL_TEXTURE_1D:
687    case GL_TEXTURE_1D_ARRAY:
688    case GL_PROXY_TEXTURE_1D:
689    case GL_PROXY_TEXTURE_1D_ARRAY:
690       size = width;
691       break;
692    case GL_TEXTURE_CUBE_MAP:
693    case GL_TEXTURE_CUBE_MAP_ARRAY:
694    case GL_PROXY_TEXTURE_CUBE_MAP:
695    case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
696       size = width;
697       break;
698    case GL_TEXTURE_2D:
699    case GL_TEXTURE_2D_ARRAY:
700    case GL_PROXY_TEXTURE_2D:
701    case GL_PROXY_TEXTURE_2D_ARRAY:
702       size = MAX2(width, height);
703       break;
704    case GL_TEXTURE_3D:
705    case GL_PROXY_TEXTURE_3D:
706       size = MAX3(width, height, depth);
707       break;
708    case GL_TEXTURE_RECTANGLE:
709    case GL_TEXTURE_EXTERNAL_OES:
710    case GL_TEXTURE_2D_MULTISAMPLE:
711    case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
712    case GL_PROXY_TEXTURE_RECTANGLE:
713    case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
714    case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
715       return 1;
716    default:
717       assert(0);
718       return 1;
719    }
720 
721    return util_logbase2(size) + 1;
722 }
723 
724 
725 #if 000 /* not used anymore */
726 /*
727  * glTexImage[123]D can accept a NULL image pointer.  In this case we
728  * create a texture image with unspecified image contents per the OpenGL
729  * spec.
730  */
731 static GLubyte *
732 make_null_texture(GLint width, GLint height, GLint depth, GLenum format)
733 {
734    const GLint components = _mesa_components_in_format(format);
735    const GLint numPixels = width * height * depth;
736    GLubyte *data = (GLubyte *) malloc(numPixels * components * sizeof(GLubyte));
737 
738 #ifdef DEBUG
739    /*
740     * Let's see if anyone finds this.  If glTexImage2D() is called with
741     * a NULL image pointer then load the texture image with something
742     * interesting instead of leaving it indeterminate.
743     */
744    if (data) {
745       static const char message[8][32] = {
746          "   X   X  XXXXX   XXX     X    ",
747          "   XX XX  X      X   X   X X   ",
748          "   X X X  X      X      X   X  ",
749          "   X   X  XXXX    XXX   XXXXX  ",
750          "   X   X  X          X  X   X  ",
751          "   X   X  X      X   X  X   X  ",
752          "   X   X  XXXXX   XXX   X   X  ",
753          "                               "
754       };
755 
756       GLubyte *imgPtr = data;
757       GLint h, i, j, k;
758       for (h = 0; h < depth; h++) {
759          for (i = 0; i < height; i++) {
760             GLint srcRow = 7 - (i % 8);
761             for (j = 0; j < width; j++) {
762                GLint srcCol = j % 32;
763                GLubyte texel = (message[srcRow][srcCol]=='X') ? 255 : 70;
764                for (k = 0; k < components; k++) {
765                   *imgPtr++ = texel;
766                }
767             }
768          }
769       }
770    }
771 #endif
772 
773    return data;
774 }
775 #endif
776 
777 
778 
779 /**
780  * Set the size and format-related fields of a gl_texture_image struct
781  * to zero.  This is used when a proxy texture test fails.
782  */
783 static void
clear_teximage_fields(struct gl_texture_image * img)784 clear_teximage_fields(struct gl_texture_image *img)
785 {
786    assert(img);
787    img->_BaseFormat = 0;
788    img->InternalFormat = 0;
789    img->Border = 0;
790    img->Width = 0;
791    img->Height = 0;
792    img->Depth = 0;
793    img->Width2 = 0;
794    img->Height2 = 0;
795    img->Depth2 = 0;
796    img->WidthLog2 = 0;
797    img->HeightLog2 = 0;
798    img->DepthLog2 = 0;
799    img->TexFormat = MESA_FORMAT_NONE;
800    img->NumSamples = 0;
801    img->FixedSampleLocations = GL_TRUE;
802 }
803 
804 
805 /**
806  * Initialize basic fields of the gl_texture_image struct.
807  *
808  * \param ctx GL context.
809  * \param img texture image structure to be initialized.
810  * \param width image width.
811  * \param height image height.
812  * \param depth image depth.
813  * \param border image border.
814  * \param internalFormat internal format.
815  * \param format  the actual hardware format (one of MESA_FORMAT_*)
816  * \param numSamples  number of samples per texel, or zero for non-MS.
817  * \param fixedSampleLocations  are sample locations fixed?
818  *
819  * Fills in the fields of \p img with the given information.
820  * Note: width, height and depth include the border.
821  */
822 void
_mesa_init_teximage_fields_ms(struct gl_context * ctx,struct gl_texture_image * img,GLsizei width,GLsizei height,GLsizei depth,GLint border,GLenum internalFormat,mesa_format format,GLuint numSamples,GLboolean fixedSampleLocations)823 _mesa_init_teximage_fields_ms(struct gl_context *ctx,
824                         struct gl_texture_image *img,
825                         GLsizei width, GLsizei height, GLsizei depth,
826                         GLint border, GLenum internalFormat,
827                         mesa_format format,
828                         GLuint numSamples, GLboolean fixedSampleLocations)
829 {
830    const GLint base_format =_mesa_base_tex_format(ctx, internalFormat);
831    GLenum target;
832    assert(img);
833    assert(width >= 0);
834    assert(height >= 0);
835    assert(depth >= 0);
836 
837    target = img->TexObject->Target;
838    assert(base_format != -1);
839    img->_BaseFormat = (GLenum16)base_format;
840    img->InternalFormat = internalFormat;
841    img->Border = border;
842    img->Width = width;
843    img->Height = height;
844    img->Depth = depth;
845 
846    img->Width2 = width - 2 * border;   /* == 1 << img->WidthLog2; */
847    img->WidthLog2 = util_logbase2(img->Width2);
848 
849    switch(target) {
850    case GL_TEXTURE_1D:
851    case GL_TEXTURE_BUFFER:
852    case GL_PROXY_TEXTURE_1D:
853       if (height == 0)
854          img->Height2 = 0;
855       else
856          img->Height2 = 1;
857       img->HeightLog2 = 0;
858       if (depth == 0)
859          img->Depth2 = 0;
860       else
861          img->Depth2 = 1;
862       img->DepthLog2 = 0;
863       break;
864    case GL_TEXTURE_1D_ARRAY:
865    case GL_PROXY_TEXTURE_1D_ARRAY:
866       img->Height2 = height; /* no border */
867       img->HeightLog2 = 0; /* not used */
868       if (depth == 0)
869          img->Depth2 = 0;
870       else
871          img->Depth2 = 1;
872       img->DepthLog2 = 0;
873       break;
874    case GL_TEXTURE_2D:
875    case GL_TEXTURE_RECTANGLE:
876    case GL_TEXTURE_CUBE_MAP:
877    case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
878    case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
879    case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
880    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
881    case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
882    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
883    case GL_TEXTURE_EXTERNAL_OES:
884    case GL_PROXY_TEXTURE_2D:
885    case GL_PROXY_TEXTURE_RECTANGLE:
886    case GL_PROXY_TEXTURE_CUBE_MAP:
887    case GL_TEXTURE_2D_MULTISAMPLE:
888    case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
889       img->Height2 = height - 2 * border; /* == 1 << img->HeightLog2; */
890       img->HeightLog2 = util_logbase2(img->Height2);
891       if (depth == 0)
892          img->Depth2 = 0;
893       else
894          img->Depth2 = 1;
895       img->DepthLog2 = 0;
896       break;
897    case GL_TEXTURE_2D_ARRAY:
898    case GL_PROXY_TEXTURE_2D_ARRAY:
899    case GL_TEXTURE_CUBE_MAP_ARRAY:
900    case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
901    case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
902    case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
903       img->Height2 = height - 2 * border; /* == 1 << img->HeightLog2; */
904       img->HeightLog2 = util_logbase2(img->Height2);
905       img->Depth2 = depth; /* no border */
906       img->DepthLog2 = 0; /* not used */
907       break;
908    case GL_TEXTURE_3D:
909    case GL_PROXY_TEXTURE_3D:
910       img->Height2 = height - 2 * border; /* == 1 << img->HeightLog2; */
911       img->HeightLog2 = util_logbase2(img->Height2);
912       img->Depth2 = depth - 2 * border;   /* == 1 << img->DepthLog2; */
913       img->DepthLog2 = util_logbase2(img->Depth2);
914       break;
915    default:
916       _mesa_problem(NULL, "invalid target 0x%x in _mesa_init_teximage_fields()",
917                     target);
918    }
919 
920    img->MaxNumLevels =
921       _mesa_get_tex_max_num_levels(target,
922                                    img->Width2, img->Height2, img->Depth2);
923    img->TexFormat = format;
924    img->NumSamples = numSamples;
925    img->FixedSampleLocations = fixedSampleLocations;
926 }
927 
928 
929 void
_mesa_init_teximage_fields(struct gl_context * ctx,struct gl_texture_image * img,GLsizei width,GLsizei height,GLsizei depth,GLint border,GLenum internalFormat,mesa_format format)930 _mesa_init_teximage_fields(struct gl_context *ctx,
931                            struct gl_texture_image *img,
932                            GLsizei width, GLsizei height, GLsizei depth,
933                            GLint border, GLenum internalFormat,
934                            mesa_format format)
935 {
936    _mesa_init_teximage_fields_ms(ctx, img, width, height, depth, border,
937                                  internalFormat, format, 0, GL_TRUE);
938 }
939 
940 
941 /**
942  * Free and clear fields of the gl_texture_image struct.
943  *
944  * \param ctx GL context.
945  * \param texImage texture image structure to be cleared.
946  *
947  * After the call, \p texImage will have no data associated with it.  Its
948  * fields are cleared so that its parent object will test incomplete.
949  */
950 void
_mesa_clear_texture_image(struct gl_context * ctx,struct gl_texture_image * texImage)951 _mesa_clear_texture_image(struct gl_context *ctx,
952                           struct gl_texture_image *texImage)
953 {
954    ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
955    clear_teximage_fields(texImage);
956 }
957 
958 
959 /**
960  * Check the width, height, depth and border of a texture image are legal.
961  * Used by all the glTexImage, glCompressedTexImage and glCopyTexImage
962  * functions.
963  * The target and level parameters will have already been validated.
964  * \return GL_TRUE if size is OK, GL_FALSE otherwise.
965  */
966 GLboolean
_mesa_legal_texture_dimensions(struct gl_context * ctx,GLenum target,GLint level,GLint width,GLint height,GLint depth,GLint border)967 _mesa_legal_texture_dimensions(struct gl_context *ctx, GLenum target,
968                                GLint level, GLint width, GLint height,
969                                GLint depth, GLint border)
970 {
971    GLint maxSize;
972 
973    switch (target) {
974    case GL_TEXTURE_1D:
975    case GL_PROXY_TEXTURE_1D:
976       maxSize = ctx->Const.MaxTextureSize >> level;
977       if (width < 2 * border || width > 2 * border + maxSize)
978          return GL_FALSE;
979       if (!ctx->Extensions.ARB_texture_non_power_of_two) {
980          if (width > 0 && !util_is_power_of_two_nonzero(width - 2 * border))
981             return GL_FALSE;
982       }
983       return GL_TRUE;
984 
985    case GL_TEXTURE_2D:
986    case GL_PROXY_TEXTURE_2D:
987    case GL_TEXTURE_2D_MULTISAMPLE:
988    case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
989       maxSize = ctx->Const.MaxTextureSize >> level;
990       if (width < 2 * border || width > 2 * border + maxSize)
991          return GL_FALSE;
992       if (height < 2 * border || height > 2 * border + maxSize)
993          return GL_FALSE;
994       if (!ctx->Extensions.ARB_texture_non_power_of_two) {
995          if (width > 0 && !util_is_power_of_two_nonzero(width - 2 * border))
996             return GL_FALSE;
997          if (height > 0 && !util_is_power_of_two_nonzero(height - 2 * border))
998             return GL_FALSE;
999       }
1000       return GL_TRUE;
1001 
1002    case GL_TEXTURE_3D:
1003    case GL_PROXY_TEXTURE_3D:
1004       maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
1005       maxSize >>= level;
1006       if (width < 2 * border || width > 2 * border + maxSize)
1007          return GL_FALSE;
1008       if (height < 2 * border || height > 2 * border + maxSize)
1009          return GL_FALSE;
1010       if (depth < 2 * border || depth > 2 * border + maxSize)
1011          return GL_FALSE;
1012       if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1013          if (width > 0 && !util_is_power_of_two_nonzero(width - 2 * border))
1014             return GL_FALSE;
1015          if (height > 0 && !util_is_power_of_two_nonzero(height - 2 * border))
1016             return GL_FALSE;
1017          if (depth > 0 && !util_is_power_of_two_nonzero(depth - 2 * border))
1018             return GL_FALSE;
1019       }
1020       return GL_TRUE;
1021 
1022    case GL_TEXTURE_RECTANGLE_NV:
1023    case GL_PROXY_TEXTURE_RECTANGLE_NV:
1024       if (level != 0)
1025          return GL_FALSE;
1026       maxSize = ctx->Const.MaxTextureRectSize;
1027       if (width < 0 || width > maxSize)
1028          return GL_FALSE;
1029       if (height < 0 || height > maxSize)
1030          return GL_FALSE;
1031       return GL_TRUE;
1032 
1033    case GL_TEXTURE_CUBE_MAP:
1034    case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1035    case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1036    case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1037    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1038    case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1039    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1040    case GL_PROXY_TEXTURE_CUBE_MAP:
1041       maxSize = 1 << (ctx->Const.MaxCubeTextureLevels - 1);
1042       maxSize >>= level;
1043       if (width != height)
1044          return GL_FALSE;
1045       if (width < 2 * border || width > 2 * border + maxSize)
1046          return GL_FALSE;
1047       if (height < 2 * border || height > 2 * border + maxSize)
1048          return GL_FALSE;
1049       if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1050          if (width > 0 && !util_is_power_of_two_nonzero(width - 2 * border))
1051             return GL_FALSE;
1052          if (height > 0 && !util_is_power_of_two_nonzero(height - 2 * border))
1053             return GL_FALSE;
1054       }
1055       return GL_TRUE;
1056 
1057    case GL_TEXTURE_1D_ARRAY_EXT:
1058    case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
1059       maxSize = ctx->Const.MaxTextureSize >> level;
1060       if (width < 2 * border || width > 2 * border + maxSize)
1061          return GL_FALSE;
1062       if (height < 0 || height > ctx->Const.MaxArrayTextureLayers)
1063          return GL_FALSE;
1064       if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1065          if (width > 0 && !util_is_power_of_two_nonzero(width - 2 * border))
1066             return GL_FALSE;
1067       }
1068       return GL_TRUE;
1069 
1070    case GL_TEXTURE_2D_ARRAY_EXT:
1071    case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
1072    case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
1073    case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
1074       maxSize = ctx->Const.MaxTextureSize >> level;
1075       if (width < 2 * border || width > 2 * border + maxSize)
1076          return GL_FALSE;
1077       if (height < 2 * border || height > 2 * border + maxSize)
1078          return GL_FALSE;
1079       if (depth < 0 || depth > ctx->Const.MaxArrayTextureLayers)
1080          return GL_FALSE;
1081       if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1082          if (width > 0 && !util_is_power_of_two_nonzero(width - 2 * border))
1083             return GL_FALSE;
1084          if (height > 0 && !util_is_power_of_two_nonzero(height - 2 * border))
1085             return GL_FALSE;
1086       }
1087       return GL_TRUE;
1088 
1089    case GL_TEXTURE_CUBE_MAP_ARRAY:
1090    case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
1091       maxSize = 1 << (ctx->Const.MaxCubeTextureLevels - 1);
1092       if (width < 2 * border || width > 2 * border + maxSize)
1093          return GL_FALSE;
1094       if (height < 2 * border || height > 2 * border + maxSize)
1095          return GL_FALSE;
1096       if (depth < 0 || depth > ctx->Const.MaxArrayTextureLayers || depth % 6)
1097          return GL_FALSE;
1098       if (width != height)
1099          return GL_FALSE;
1100       if (level >= ctx->Const.MaxCubeTextureLevels)
1101          return GL_FALSE;
1102       if (!ctx->Extensions.ARB_texture_non_power_of_two) {
1103          if (width > 0 && !util_is_power_of_two_nonzero(width - 2 * border))
1104             return GL_FALSE;
1105          if (height > 0 && !util_is_power_of_two_nonzero(height - 2 * border))
1106             return GL_FALSE;
1107       }
1108       return GL_TRUE;
1109    default:
1110       _mesa_problem(ctx, "Invalid target in _mesa_legal_texture_dimensions()");
1111       return GL_FALSE;
1112    }
1113 }
1114 
1115 static bool
error_check_subtexture_negative_dimensions(struct gl_context * ctx,GLuint dims,GLsizei subWidth,GLsizei subHeight,GLsizei subDepth,const char * func)1116 error_check_subtexture_negative_dimensions(struct gl_context *ctx,
1117                                            GLuint dims,
1118                                            GLsizei subWidth,
1119                                            GLsizei subHeight,
1120                                            GLsizei subDepth,
1121                                            const char *func)
1122 {
1123    /* Check size */
1124    if (subWidth < 0) {
1125       _mesa_error(ctx, GL_INVALID_VALUE, "%s(width=%d)", func, subWidth);
1126       return true;
1127    }
1128 
1129    if (dims > 1 && subHeight < 0) {
1130       _mesa_error(ctx, GL_INVALID_VALUE, "%s(height=%d)", func, subHeight);
1131       return true;
1132    }
1133 
1134    if (dims > 2 && subDepth < 0) {
1135       _mesa_error(ctx, GL_INVALID_VALUE, "%s(depth=%d)", func, subDepth);
1136       return true;
1137    }
1138 
1139    return false;
1140 }
1141 
1142 /**
1143  * Do error checking of xoffset, yoffset, zoffset, width, height and depth
1144  * for glTexSubImage, glCopyTexSubImage and glCompressedTexSubImage.
1145  * \param destImage  the destination texture image.
1146  * \return GL_TRUE if error found, GL_FALSE otherwise.
1147  */
1148 static GLboolean
error_check_subtexture_dimensions(struct gl_context * ctx,GLuint dims,const struct gl_texture_image * destImage,GLint xoffset,GLint yoffset,GLint zoffset,GLsizei subWidth,GLsizei subHeight,GLsizei subDepth,const char * func)1149 error_check_subtexture_dimensions(struct gl_context *ctx, GLuint dims,
1150                                   const struct gl_texture_image *destImage,
1151                                   GLint xoffset, GLint yoffset, GLint zoffset,
1152                                   GLsizei subWidth, GLsizei subHeight,
1153                                   GLsizei subDepth, const char *func)
1154 {
1155    const GLenum target = destImage->TexObject->Target;
1156    GLuint bw, bh, bd;
1157 
1158    /* check xoffset and width */
1159    if (xoffset < - (GLint) destImage->Border) {
1160       _mesa_error(ctx, GL_INVALID_VALUE, "%s(xoffset)", func);
1161       return GL_TRUE;
1162    }
1163 
1164    if (xoffset + subWidth > (GLint) destImage->Width) {
1165       _mesa_error(ctx, GL_INVALID_VALUE, "%s(xoffset %d + width %d > %u)", func,
1166                   xoffset, subWidth, destImage->Width);
1167       return GL_TRUE;
1168    }
1169 
1170    /* check yoffset and height */
1171    if (dims > 1) {
1172       GLint yBorder = (target == GL_TEXTURE_1D_ARRAY) ? 0 : destImage->Border;
1173       if (yoffset < -yBorder) {
1174          _mesa_error(ctx, GL_INVALID_VALUE, "%s(yoffset)", func);
1175          return GL_TRUE;
1176       }
1177       if (yoffset + subHeight > (GLint) destImage->Height) {
1178          _mesa_error(ctx, GL_INVALID_VALUE, "%s(yoffset %d + height %d > %u)",
1179                      func, yoffset, subHeight, destImage->Height);
1180          return GL_TRUE;
1181       }
1182    }
1183 
1184    /* check zoffset and depth */
1185    if (dims > 2) {
1186       GLint depth;
1187       GLint zBorder = (target == GL_TEXTURE_2D_ARRAY ||
1188                        target == GL_TEXTURE_CUBE_MAP_ARRAY) ?
1189                          0 : destImage->Border;
1190 
1191       if (zoffset < -zBorder) {
1192          _mesa_error(ctx, GL_INVALID_VALUE, "%s(zoffset)", func);
1193          return GL_TRUE;
1194       }
1195 
1196       depth = (GLint) destImage->Depth;
1197       if (target == GL_TEXTURE_CUBE_MAP)
1198          depth = 6;
1199       if (zoffset + subDepth  > depth) {
1200          _mesa_error(ctx, GL_INVALID_VALUE, "%s(zoffset %d + depth %d > %u)",
1201                      func, zoffset, subDepth, depth);
1202          return GL_TRUE;
1203       }
1204    }
1205 
1206    /*
1207     * The OpenGL spec (and GL_ARB_texture_compression) says only whole
1208     * compressed texture images can be updated.  But, that restriction may be
1209     * relaxed for particular compressed formats.  At this time, all the
1210     * compressed formats supported by Mesa allow sub-textures to be updated
1211     * along compressed block boundaries.
1212     */
1213    _mesa_get_format_block_size_3d(destImage->TexFormat, &bw, &bh, &bd);
1214 
1215    if (bw != 1 || bh != 1 || bd != 1) {
1216       /* offset must be multiple of block size */
1217       if ((xoffset % bw != 0) || (yoffset % bh != 0) || (zoffset % bd != 0)) {
1218          _mesa_error(ctx, GL_INVALID_OPERATION,
1219                      "%s(xoffset = %d, yoffset = %d, zoffset = %d)",
1220                      func, xoffset, yoffset, zoffset);
1221          return GL_TRUE;
1222       }
1223 
1224       /* The size must be a multiple of bw x bh, or we must be using a
1225        * offset+size that exactly hits the edge of the image.  This
1226        * is important for small mipmap levels (1x1, 2x1, etc) and for
1227        * NPOT textures.
1228        */
1229       if ((subWidth % bw != 0) &&
1230           (xoffset + subWidth != (GLint) destImage->Width)) {
1231          _mesa_error(ctx, GL_INVALID_OPERATION,
1232                      "%s(width = %d)", func, subWidth);
1233          return GL_TRUE;
1234       }
1235 
1236       if ((subHeight % bh != 0) &&
1237           (yoffset + subHeight != (GLint) destImage->Height)) {
1238          _mesa_error(ctx, GL_INVALID_OPERATION,
1239                      "%s(height = %d)", func, subHeight);
1240          return GL_TRUE;
1241       }
1242 
1243       if ((subDepth % bd != 0) &&
1244           (zoffset + subDepth != (GLint) destImage->Depth)) {
1245          _mesa_error(ctx, GL_INVALID_OPERATION,
1246                      "%s(depth = %d)", func, subDepth);
1247          return GL_TRUE;
1248       }
1249    }
1250 
1251    return GL_FALSE;
1252 }
1253 
1254 
1255 
1256 
1257 /**
1258  * This is the fallback for Driver.TestProxyTexImage() for doing device-
1259  * specific texture image size checks.
1260  *
1261  * A hardware driver might override this function if, for example, the
1262  * max 3D texture size is 512x512x64 (i.e. not a cube).
1263  *
1264  * Note that width, height, depth == 0 is not an error.  However, a
1265  * texture with zero width/height/depth will be considered "incomplete"
1266  * and texturing will effectively be disabled.
1267  *
1268  * \param target  any texture target/type
1269  * \param numLevels  number of mipmap levels in the texture or 0 if not known
1270  * \param level  as passed to glTexImage
1271  * \param format  the MESA_FORMAT_x for the tex image
1272  * \param numSamples  number of samples per texel
1273  * \param width  as passed to glTexImage
1274  * \param height  as passed to glTexImage
1275  * \param depth  as passed to glTexImage
1276  * \return GL_TRUE if the image is acceptable, GL_FALSE if not acceptable.
1277  */
1278 GLboolean
_mesa_test_proxy_teximage(struct gl_context * ctx,GLenum target,GLuint numLevels,ASSERTED GLint level,mesa_format format,GLuint numSamples,GLint width,GLint height,GLint depth)1279 _mesa_test_proxy_teximage(struct gl_context *ctx, GLenum target,
1280                           GLuint numLevels, ASSERTED GLint level,
1281                           mesa_format format, GLuint numSamples,
1282                           GLint width, GLint height, GLint depth)
1283 {
1284    uint64_t bytes, mbytes;
1285 
1286    if (numLevels > 0) {
1287       /* Compute total memory for a whole mipmap.  This is the path
1288        * taken for glTexStorage(GL_PROXY_TEXTURE_x).
1289        */
1290       unsigned l;
1291 
1292       assert(level == 0);
1293 
1294       bytes = 0;
1295 
1296       for (l = 0; l < numLevels; l++) {
1297          GLint nextWidth, nextHeight, nextDepth;
1298 
1299          bytes += _mesa_format_image_size64(format, width, height, depth);
1300 
1301          if (_mesa_next_mipmap_level_size(target, 0, width, height, depth,
1302                                           &nextWidth, &nextHeight,
1303                                           &nextDepth)) {
1304             width = nextWidth;
1305             height = nextHeight;
1306             depth = nextDepth;
1307          } else {
1308             break;
1309          }
1310       }
1311    } else {
1312       /* We just compute the size of one mipmap level.  This is the path
1313        * taken for glTexImage(GL_PROXY_TEXTURE_x).
1314        */
1315       bytes = _mesa_format_image_size64(format, width, height, depth);
1316    }
1317 
1318    bytes *= _mesa_num_tex_faces(target);
1319    bytes *= MAX2(1, numSamples);
1320 
1321    mbytes = bytes / (1024 * 1024); /* convert to MB */
1322 
1323    /* We just check if the image size is less than MaxTextureMbytes.
1324     * Some drivers may do more specific checks.
1325     */
1326    return mbytes <= (uint64_t) ctx->Const.MaxTextureMbytes;
1327 }
1328 
1329 
1330 /**
1331  * Return true if the format is only valid for glCompressedTexImage.
1332  */
1333 static bool
compressedteximage_only_format(GLenum format)1334 compressedteximage_only_format(GLenum format)
1335 {
1336    switch (format) {
1337    case GL_PALETTE4_RGB8_OES:
1338    case GL_PALETTE4_RGBA8_OES:
1339    case GL_PALETTE4_R5_G6_B5_OES:
1340    case GL_PALETTE4_RGBA4_OES:
1341    case GL_PALETTE4_RGB5_A1_OES:
1342    case GL_PALETTE8_RGB8_OES:
1343    case GL_PALETTE8_RGBA8_OES:
1344    case GL_PALETTE8_R5_G6_B5_OES:
1345    case GL_PALETTE8_RGBA4_OES:
1346    case GL_PALETTE8_RGB5_A1_OES:
1347    case GL_ATC_RGB_AMD:
1348    case GL_ATC_RGBA_EXPLICIT_ALPHA_AMD:
1349    case GL_ATC_RGBA_INTERPOLATED_ALPHA_AMD:
1350       return true;
1351    default:
1352       return false;
1353    }
1354 }
1355 
1356 /**
1357  * Return true if the format doesn't support online compression.
1358  */
1359 bool
_mesa_format_no_online_compression(GLenum format)1360 _mesa_format_no_online_compression(GLenum format)
1361 {
1362    return _mesa_is_astc_format(format) ||
1363           _mesa_is_etc2_format(format) ||
1364           compressedteximage_only_format(format);
1365 }
1366 
1367 /* Writes to an GL error pointer if non-null and returns whether or not the
1368  * error is GL_NO_ERROR */
1369 static bool
write_error(GLenum * err_ptr,GLenum error)1370 write_error(GLenum *err_ptr, GLenum error)
1371 {
1372    if (err_ptr)
1373       *err_ptr = error;
1374 
1375    return error == GL_NO_ERROR;
1376 }
1377 
1378 /**
1379  * Helper function to determine whether a target and specific compression
1380  * format are supported. The error parameter returns GL_NO_ERROR if the
1381  * target can be compressed. Otherwise it returns either GL_INVALID_OPERATION
1382  * or GL_INVALID_ENUM, whichever is more appropriate.
1383  */
1384 GLboolean
_mesa_target_can_be_compressed(const struct gl_context * ctx,GLenum target,GLenum intFormat,GLenum * error)1385 _mesa_target_can_be_compressed(const struct gl_context *ctx, GLenum target,
1386                                GLenum intFormat, GLenum *error)
1387 {
1388    GLboolean target_can_be_compresed = GL_FALSE;
1389    mesa_format format = _mesa_glenum_to_compressed_format(intFormat);
1390    enum mesa_format_layout layout = _mesa_get_format_layout(format);
1391 
1392    switch (target) {
1393    case GL_TEXTURE_2D:
1394    case GL_PROXY_TEXTURE_2D:
1395       target_can_be_compresed = GL_TRUE; /* true for any compressed format so far */
1396       break;
1397    case GL_PROXY_TEXTURE_CUBE_MAP:
1398    case GL_TEXTURE_CUBE_MAP:
1399    case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1400    case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1401    case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1402    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1403    case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1404    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1405       target_can_be_compresed = ctx->Extensions.ARB_texture_cube_map;
1406       break;
1407    case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
1408    case GL_TEXTURE_2D_ARRAY_EXT:
1409       target_can_be_compresed = ctx->Extensions.EXT_texture_array;
1410       break;
1411    case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
1412    case GL_TEXTURE_CUBE_MAP_ARRAY:
1413       /* From the KHR_texture_compression_astc_hdr spec:
1414        *
1415        *     Add a second new column "3D Tex." which is empty for all non-ASTC
1416        *     formats. If only the LDR profile is supported by the
1417        *     implementation, this column is also empty for all ASTC formats. If
1418        *     both the LDR and HDR profiles are supported only, this column is
1419        *     checked for all ASTC formats.
1420        *
1421        *     Add a third new column "Cube Map Array Tex." which is empty for all
1422        *     non-ASTC formats, and checked for all ASTC formats.
1423        *
1424        * and,
1425        *
1426        *     'An INVALID_OPERATION error is generated by CompressedTexImage3D
1427        *      if <internalformat> is TEXTURE_CUBE_MAP_ARRAY and the
1428        *      "Cube Map Array" column of table 8.19 is *not* checked, or if
1429        *      <internalformat> is TEXTURE_3D and the "3D Tex." column of table
1430        *      8.19 is *not* checked'
1431        *
1432        * The instances of <internalformat> above should say <target>.
1433        *
1434        * ETC2/EAC formats are the only alternative in GLES and thus such errors
1435        * have already been handled by normal ETC2/EAC behavior.
1436        */
1437 
1438       /* From section 3.8.6, page 146 of OpenGL ES 3.0 spec:
1439        *
1440        *    "The ETC2/EAC texture compression algorithm supports only
1441        *     two-dimensional images. If internalformat is an ETC2/EAC format,
1442        *     glCompressedTexImage3D will generate an INVALID_OPERATION error if
1443        *     target is not TEXTURE_2D_ARRAY."
1444        *
1445        * This should also be applicable for glTexStorage3D(). Other available
1446        * targets for these functions are: TEXTURE_3D and TEXTURE_CUBE_MAP_ARRAY.
1447        *
1448        * Section 8.7, page 179 of OpenGL ES 3.2 adds:
1449        *
1450        *      An INVALID_OPERATION error is generated by CompressedTexImage3D
1451        *      if internalformat is one of the the formats in table 8.17 and target is
1452        *      not TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP_ARRAY or TEXTURE_3D.
1453        *
1454        *      An INVALID_OPERATION error is generated by CompressedTexImage3D
1455        *      if internalformat is TEXTURE_CUBE_MAP_ARRAY and the “Cube Map
1456        *      Array” column of table 8.17 is not checked, or if internalformat
1457        *      is TEXTURE_- 3D and the “3D Tex.” column of table 8.17 is not
1458        *      checked.
1459        *
1460        * The instances of <internalformat> above should say <target>.
1461        *
1462        * Such table 8.17 has checked "Cube Map Array" column for all the
1463        * cases. So in practice, TEXTURE_CUBE_MAP_ARRAY is now valid for OpenGL ES 3.2
1464        */
1465       if (layout == MESA_FORMAT_LAYOUT_ETC2 && _mesa_is_gles3(ctx) &&
1466           !_mesa_is_gles32(ctx))
1467             return write_error(error, GL_INVALID_OPERATION);
1468       target_can_be_compresed = _mesa_has_texture_cube_map_array(ctx);
1469       break;
1470    case GL_TEXTURE_3D:
1471       switch (layout) {
1472       case MESA_FORMAT_LAYOUT_ETC2:
1473          /* See ETC2/EAC comment in case GL_TEXTURE_CUBE_MAP_ARRAY. */
1474          if (_mesa_is_gles3(ctx))
1475             return write_error(error, GL_INVALID_OPERATION);
1476          break;
1477       case MESA_FORMAT_LAYOUT_BPTC:
1478          target_can_be_compresed = ctx->Extensions.ARB_texture_compression_bptc;
1479          break;
1480       case MESA_FORMAT_LAYOUT_ASTC:
1481          target_can_be_compresed =
1482             ctx->Extensions.KHR_texture_compression_astc_hdr ||
1483             ctx->Extensions.KHR_texture_compression_astc_sliced_3d;
1484 
1485          /* Throw an INVALID_OPERATION error if the target is TEXTURE_3D and
1486           * neither of the above extensions are supported. See comment in
1487           * switch case GL_TEXTURE_CUBE_MAP_ARRAY for more info.
1488           */
1489          if (!target_can_be_compresed)
1490             return write_error(error, GL_INVALID_OPERATION);
1491          break;
1492       default:
1493          break;
1494       }
1495    default:
1496       break;
1497    }
1498    return write_error(error,
1499                       target_can_be_compresed ? GL_NO_ERROR : GL_INVALID_ENUM);
1500 }
1501 
1502 
1503 /**
1504  * Check if the given texture target value is legal for a
1505  * glTexImage1/2/3D call.
1506  */
1507 static GLboolean
legal_teximage_target(struct gl_context * ctx,GLuint dims,GLenum target)1508 legal_teximage_target(struct gl_context *ctx, GLuint dims, GLenum target)
1509 {
1510    switch (dims) {
1511    case 1:
1512       switch (target) {
1513       case GL_TEXTURE_1D:
1514       case GL_PROXY_TEXTURE_1D:
1515          return _mesa_is_desktop_gl(ctx);
1516       default:
1517          return GL_FALSE;
1518       }
1519    case 2:
1520       switch (target) {
1521       case GL_TEXTURE_2D:
1522          return GL_TRUE;
1523       case GL_PROXY_TEXTURE_2D:
1524          return _mesa_is_desktop_gl(ctx);
1525       case GL_PROXY_TEXTURE_CUBE_MAP:
1526          return _mesa_is_desktop_gl(ctx)
1527             && ctx->Extensions.ARB_texture_cube_map;
1528       case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1529       case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1530       case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1531       case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1532       case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1533       case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1534          return ctx->Extensions.ARB_texture_cube_map;
1535       case GL_TEXTURE_RECTANGLE_NV:
1536       case GL_PROXY_TEXTURE_RECTANGLE_NV:
1537          return _mesa_is_desktop_gl(ctx)
1538             && ctx->Extensions.NV_texture_rectangle;
1539       case GL_TEXTURE_1D_ARRAY_EXT:
1540       case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
1541          return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array;
1542       default:
1543          return GL_FALSE;
1544       }
1545    case 3:
1546       switch (target) {
1547       case GL_TEXTURE_3D:
1548          return GL_TRUE;
1549       case GL_PROXY_TEXTURE_3D:
1550          return _mesa_is_desktop_gl(ctx);
1551       case GL_TEXTURE_2D_ARRAY_EXT:
1552          return (_mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array)
1553             || _mesa_is_gles3(ctx);
1554       case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
1555          return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array;
1556       case GL_TEXTURE_CUBE_MAP_ARRAY:
1557       case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
1558          return _mesa_has_texture_cube_map_array(ctx);
1559       default:
1560          return GL_FALSE;
1561       }
1562    default:
1563       _mesa_problem(ctx, "invalid dims=%u in legal_teximage_target()", dims);
1564       return GL_FALSE;
1565    }
1566 }
1567 
1568 
1569 /**
1570  * Check if the given texture target value is legal for a
1571  * glTexSubImage, glCopyTexSubImage or glCopyTexImage call.
1572  * The difference compared to legal_teximage_target() above is that
1573  * proxy targets are not supported.
1574  */
1575 static GLboolean
legal_texsubimage_target(struct gl_context * ctx,GLuint dims,GLenum target,bool dsa)1576 legal_texsubimage_target(struct gl_context *ctx, GLuint dims, GLenum target,
1577                          bool dsa)
1578 {
1579    switch (dims) {
1580    case 1:
1581       return _mesa_is_desktop_gl(ctx) && target == GL_TEXTURE_1D;
1582    case 2:
1583       switch (target) {
1584       case GL_TEXTURE_2D:
1585          return GL_TRUE;
1586       case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1587       case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1588       case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1589       case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1590       case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1591       case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1592          return ctx->Extensions.ARB_texture_cube_map;
1593       case GL_TEXTURE_RECTANGLE_NV:
1594          return _mesa_is_desktop_gl(ctx)
1595             && ctx->Extensions.NV_texture_rectangle;
1596       case GL_TEXTURE_1D_ARRAY_EXT:
1597          return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array;
1598       default:
1599          return GL_FALSE;
1600       }
1601    case 3:
1602       switch (target) {
1603       case GL_TEXTURE_3D:
1604          return GL_TRUE;
1605       case GL_TEXTURE_2D_ARRAY_EXT:
1606          return (_mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array)
1607             || _mesa_is_gles3(ctx);
1608       case GL_TEXTURE_CUBE_MAP_ARRAY:
1609       case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
1610          return _mesa_has_texture_cube_map_array(ctx);
1611 
1612       /* Table 8.15 of the OpenGL 4.5 core profile spec
1613        * (20141030) says that TEXTURE_CUBE_MAP is valid for TextureSubImage3D
1614        * and CopyTextureSubImage3D.
1615        */
1616       case GL_TEXTURE_CUBE_MAP:
1617          return dsa;
1618       default:
1619          return GL_FALSE;
1620       }
1621    default:
1622       _mesa_problem(ctx, "invalid dims=%u in legal_texsubimage_target()",
1623                     dims);
1624       return GL_FALSE;
1625    }
1626 }
1627 
1628 
1629 /**
1630  * Helper function to determine if a texture object is mutable (in terms
1631  * of GL_ARB_texture_storage/GL_ARB_bindless_texture).
1632  */
1633 static GLboolean
mutable_tex_object(struct gl_texture_object * texObj)1634 mutable_tex_object(struct gl_texture_object *texObj)
1635 {
1636    if (!texObj)
1637       return GL_FALSE;
1638 
1639    if (texObj->HandleAllocated) {
1640       /* The ARB_bindless_texture spec says:
1641        *
1642        * "The error INVALID_OPERATION is generated by TexImage*, CopyTexImage*,
1643        *  CompressedTexImage*, TexBuffer*, TexParameter*, as well as other
1644        *  functions defined in terms of these, if the texture object to be
1645        *  modified is referenced by one or more texture or image handles."
1646        */
1647       return GL_FALSE;
1648    }
1649 
1650    return !texObj->Immutable;
1651 }
1652 
1653 
1654 /**
1655  * Return expected size of a compressed texture.
1656  */
1657 static GLuint
compressed_tex_size(GLsizei width,GLsizei height,GLsizei depth,GLenum glformat)1658 compressed_tex_size(GLsizei width, GLsizei height, GLsizei depth,
1659                     GLenum glformat)
1660 {
1661    mesa_format mesaFormat = _mesa_glenum_to_compressed_format(glformat);
1662    return _mesa_format_image_size(mesaFormat, width, height, depth);
1663 }
1664 
1665 /**
1666  * Verify that a texture format is valid with a particular target
1667  *
1668  * In particular, textures with base format of \c GL_DEPTH_COMPONENT or
1669  * \c GL_DEPTH_STENCIL are only valid with certain, context dependent texture
1670  * targets.
1671  *
1672  * \param ctx             GL context
1673  * \param target          Texture target
1674  * \param internalFormat  Internal format of the texture image
1675  *
1676  * \returns true if the combination is legal, false otherwise.
1677  */
1678 bool
_mesa_legal_texture_base_format_for_target(struct gl_context * ctx,GLenum target,GLenum internalFormat)1679 _mesa_legal_texture_base_format_for_target(struct gl_context *ctx,
1680                                            GLenum target, GLenum internalFormat)
1681 {
1682    if (_mesa_base_tex_format(ctx, internalFormat) == GL_DEPTH_COMPONENT
1683        || _mesa_base_tex_format(ctx, internalFormat) == GL_DEPTH_STENCIL
1684        || _mesa_base_tex_format(ctx, internalFormat) == GL_STENCIL_INDEX) {
1685       /* Section 3.8.3 (Texture Image Specification) of the OpenGL 3.3 Core
1686        * Profile spec says:
1687        *
1688        *     "Textures with a base internal format of DEPTH_COMPONENT or
1689        *     DEPTH_STENCIL are supported by texture image specification
1690        *     commands only if target is TEXTURE_1D, TEXTURE_2D,
1691        *     TEXTURE_1D_ARRAY, TEXTURE_2D_ARRAY, TEXTURE_RECTANGLE,
1692        *     TEXTURE_CUBE_MAP, PROXY_TEXTURE_1D, PROXY_TEXTURE_2D,
1693        *     PROXY_TEXTURE_1D_ARRAY, PROXY_TEXTURE_2D_ARRAY,
1694        *     PROXY_TEXTURE_RECTANGLE, or PROXY_TEXTURE_CUBE_MAP. Using these
1695        *     formats in conjunction with any other target will result in an
1696        *     INVALID_OPERATION error."
1697        *
1698        * Cubemaps are only supported with desktop OpenGL version >= 3.0,
1699        * EXT_gpu_shader4, or, on OpenGL ES 2.0+, OES_depth_texture_cube_map.
1700        */
1701       if (target != GL_TEXTURE_1D &&
1702           target != GL_PROXY_TEXTURE_1D &&
1703           target != GL_TEXTURE_2D &&
1704           target != GL_PROXY_TEXTURE_2D &&
1705           target != GL_TEXTURE_1D_ARRAY &&
1706           target != GL_PROXY_TEXTURE_1D_ARRAY &&
1707           target != GL_TEXTURE_2D_ARRAY &&
1708           target != GL_PROXY_TEXTURE_2D_ARRAY &&
1709           target != GL_TEXTURE_RECTANGLE_ARB &&
1710           target != GL_PROXY_TEXTURE_RECTANGLE_ARB &&
1711          !((_mesa_is_cube_face(target) ||
1712             target == GL_TEXTURE_CUBE_MAP ||
1713             target == GL_PROXY_TEXTURE_CUBE_MAP) &&
1714            (ctx->Version >= 30 || ctx->Extensions.EXT_gpu_shader4
1715             || (ctx->API == API_OPENGLES2 && ctx->Extensions.OES_depth_texture_cube_map))) &&
1716           !((target == GL_TEXTURE_CUBE_MAP_ARRAY ||
1717              target == GL_PROXY_TEXTURE_CUBE_MAP_ARRAY) &&
1718             _mesa_has_texture_cube_map_array(ctx))) {
1719          return false;
1720       }
1721    }
1722 
1723    return true;
1724 }
1725 
1726 static bool
texture_formats_agree(GLenum internalFormat,GLenum format)1727 texture_formats_agree(GLenum internalFormat,
1728                       GLenum format)
1729 {
1730    GLboolean colorFormat;
1731    GLboolean is_format_depth_or_depthstencil;
1732    GLboolean is_internalFormat_depth_or_depthstencil;
1733 
1734    /* Even though there are no color-index textures, we still have to support
1735     * uploading color-index data and remapping it to RGB via the
1736     * GL_PIXEL_MAP_I_TO_[RGBA] tables.
1737     */
1738    const GLboolean indexFormat = (format == GL_COLOR_INDEX);
1739 
1740    is_internalFormat_depth_or_depthstencil =
1741       _mesa_is_depth_format(internalFormat) ||
1742       _mesa_is_depthstencil_format(internalFormat);
1743 
1744    is_format_depth_or_depthstencil =
1745       _mesa_is_depth_format(format) ||
1746       _mesa_is_depthstencil_format(format);
1747 
1748    colorFormat = _mesa_is_color_format(format);
1749 
1750    if (_mesa_is_color_format(internalFormat) && !colorFormat && !indexFormat)
1751       return false;
1752 
1753    if (is_internalFormat_depth_or_depthstencil !=
1754        is_format_depth_or_depthstencil)
1755       return false;
1756 
1757    if (_mesa_is_ycbcr_format(internalFormat) != _mesa_is_ycbcr_format(format))
1758       return false;
1759 
1760    return true;
1761 }
1762 
1763 /**
1764  * Test the combination of format, type and internal format arguments of
1765  * different texture operations on GLES.
1766  *
1767  * \param ctx GL context.
1768  * \param format pixel data format given by the user.
1769  * \param type pixel data type given by the user.
1770  * \param internalFormat internal format given by the user.
1771  * \param callerName name of the caller function to print in the error message
1772  *
1773  * \return true if a error is found, false otherwise
1774  *
1775  * Currently, it is used by texture_error_check() and texsubimage_error_check().
1776  */
1777 static bool
texture_format_error_check_gles(struct gl_context * ctx,GLenum format,GLenum type,GLenum internalFormat,const char * callerName)1778 texture_format_error_check_gles(struct gl_context *ctx, GLenum format,
1779                                 GLenum type, GLenum internalFormat, const char *callerName)
1780 {
1781    GLenum err = _mesa_gles_error_check_format_and_type(ctx, format, type,
1782                                                        internalFormat);
1783    if (err != GL_NO_ERROR) {
1784       _mesa_error(ctx, err,
1785                   "%s(format = %s, type = %s, internalformat = %s)",
1786                   callerName, _mesa_enum_to_string(format),
1787                   _mesa_enum_to_string(type),
1788                   _mesa_enum_to_string(internalFormat));
1789       return true;
1790    }
1791 
1792    return false;
1793 }
1794 
1795 /**
1796  * Test the glTexImage[123]D() parameters for errors.
1797  *
1798  * \param ctx GL context.
1799  * \param dimensions texture image dimensions (must be 1, 2 or 3).
1800  * \param target texture target given by the user (already validated).
1801  * \param level image level given by the user.
1802  * \param internalFormat internal format given by the user.
1803  * \param format pixel data format given by the user.
1804  * \param type pixel data type given by the user.
1805  * \param width image width given by the user.
1806  * \param height image height given by the user.
1807  * \param depth image depth given by the user.
1808  * \param border image border given by the user.
1809  *
1810  * \return GL_TRUE if a error is found, GL_FALSE otherwise
1811  *
1812  * Verifies each of the parameters against the constants specified in
1813  * __struct gl_contextRec::Const and the supported extensions, and according
1814  * to the OpenGL specification.
1815  * Note that we don't fully error-check the width, height, depth values
1816  * here.  That's done in _mesa_legal_texture_dimensions() which is used
1817  * by several other GL entrypoints.  Plus, texture dims have a special
1818  * interaction with proxy textures.
1819  */
1820 static GLboolean
texture_error_check(struct gl_context * ctx,GLuint dimensions,GLenum target,struct gl_texture_object * texObj,GLint level,GLint internalFormat,GLenum format,GLenum type,GLint width,GLint height,GLint depth,GLint border,const GLvoid * pixels)1821 texture_error_check( struct gl_context *ctx,
1822                      GLuint dimensions, GLenum target,
1823                      struct gl_texture_object* texObj,
1824                      GLint level, GLint internalFormat,
1825                      GLenum format, GLenum type,
1826                      GLint width, GLint height,
1827                      GLint depth, GLint border,
1828                      const GLvoid *pixels )
1829 {
1830    GLenum err;
1831 
1832    /* Note: for proxy textures, some error conditions immediately generate
1833     * a GL error in the usual way.  But others do not generate a GL error.
1834     * Instead, they cause the width, height, depth, format fields of the
1835     * texture image to be zeroed-out.  The GL spec seems to indicate that the
1836     * zero-out behaviour is only used in cases related to memory allocation.
1837     */
1838 
1839    /* level check */
1840    if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
1841       _mesa_error(ctx, GL_INVALID_VALUE,
1842                   "glTexImage%dD(level=%d)", dimensions, level);
1843       return GL_TRUE;
1844    }
1845 
1846    /* Check border */
1847    if (border < 0 || border > 1 ||
1848        ((ctx->API != API_OPENGL_COMPAT ||
1849          target == GL_TEXTURE_RECTANGLE_NV ||
1850          target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
1851       _mesa_error(ctx, GL_INVALID_VALUE,
1852                   "glTexImage%dD(border=%d)", dimensions, border);
1853       return GL_TRUE;
1854    }
1855 
1856    if (width < 0 || height < 0 || depth < 0) {
1857       _mesa_error(ctx, GL_INVALID_VALUE,
1858                   "glTexImage%dD(width, height or depth < 0)", dimensions);
1859       return GL_TRUE;
1860    }
1861 
1862    /* Check incoming image format and type */
1863    err = _mesa_error_check_format_and_type(ctx, format, type);
1864    if (err != GL_NO_ERROR) {
1865       /* Prior to OpenGL-ES 2.0, an INVALID_VALUE is expected instead of
1866        * INVALID_ENUM. From page 73 OpenGL ES 1.1 spec:
1867        *
1868        *     "Specifying a value for internalformat that is not one of the
1869        *      above (acceptable) values generates the error INVALID VALUE."
1870        */
1871       if (err == GL_INVALID_ENUM && _mesa_is_gles(ctx) && ctx->Version < 20)
1872          err = GL_INVALID_VALUE;
1873 
1874       _mesa_error(ctx, err,
1875                   "glTexImage%dD(incompatible format = %s, type = %s)",
1876                   dimensions, _mesa_enum_to_string(format),
1877                   _mesa_enum_to_string(type));
1878       return GL_TRUE;
1879    }
1880 
1881    /* Check internalFormat */
1882    if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
1883       _mesa_error(ctx, GL_INVALID_VALUE,
1884                   "glTexImage%dD(internalFormat=%s)",
1885                   dimensions, _mesa_enum_to_string(internalFormat));
1886       return GL_TRUE;
1887    }
1888 
1889    /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
1890     * combinations of format, internalFormat, and type that can be used.
1891     * Formats and types that require additional extensions (e.g., GL_FLOAT
1892     * requires GL_OES_texture_float) are filtered elsewhere.
1893     */
1894    char bufCallerName[20];
1895    snprintf(bufCallerName, 20, "glTexImage%dD", dimensions);
1896    if (_mesa_is_gles(ctx) &&
1897        texture_format_error_check_gles(ctx, format, type,
1898                                        internalFormat, bufCallerName)) {
1899       return GL_TRUE;
1900    }
1901 
1902    /* validate the bound PBO, if any */
1903    if (!_mesa_validate_pbo_source(ctx, dimensions, &ctx->Unpack,
1904                                   width, height, depth, format, type,
1905                                   INT_MAX, pixels, "glTexImage")) {
1906       return GL_TRUE;
1907    }
1908 
1909    /* make sure internal format and format basically agree */
1910    if (!texture_formats_agree(internalFormat, format)) {
1911       _mesa_error(ctx, GL_INVALID_OPERATION,
1912                   "glTexImage%dD(incompatible internalFormat = %s, format = %s)",
1913                   dimensions, _mesa_enum_to_string(internalFormat),
1914                   _mesa_enum_to_string(format));
1915       return GL_TRUE;
1916    }
1917 
1918    /* additional checks for ycbcr textures */
1919    if (internalFormat == GL_YCBCR_MESA) {
1920       assert(ctx->Extensions.MESA_ycbcr_texture);
1921       if (type != GL_UNSIGNED_SHORT_8_8_MESA &&
1922           type != GL_UNSIGNED_SHORT_8_8_REV_MESA) {
1923          char message[100];
1924          snprintf(message, sizeof(message),
1925                         "glTexImage%dD(format/type YCBCR mismatch)",
1926                         dimensions);
1927          _mesa_error(ctx, GL_INVALID_ENUM, "%s", message);
1928          return GL_TRUE; /* error */
1929       }
1930       if (target != GL_TEXTURE_2D &&
1931           target != GL_PROXY_TEXTURE_2D &&
1932           target != GL_TEXTURE_RECTANGLE_NV &&
1933           target != GL_PROXY_TEXTURE_RECTANGLE_NV) {
1934          _mesa_error(ctx, GL_INVALID_ENUM,
1935                      "glTexImage%dD(bad target for YCbCr texture)",
1936                      dimensions);
1937          return GL_TRUE;
1938       }
1939       if (border != 0) {
1940          char message[100];
1941          snprintf(message, sizeof(message),
1942                         "glTexImage%dD(format=GL_YCBCR_MESA and border=%d)",
1943                         dimensions, border);
1944          _mesa_error(ctx, GL_INVALID_VALUE, "%s", message);
1945          return GL_TRUE;
1946       }
1947    }
1948 
1949    /* additional checks for depth textures */
1950    if (!_mesa_legal_texture_base_format_for_target(ctx, target, internalFormat)) {
1951       _mesa_error(ctx, GL_INVALID_OPERATION,
1952                   "glTexImage%dD(bad target for texture)", dimensions);
1953       return GL_TRUE;
1954    }
1955 
1956    /* additional checks for compressed textures */
1957    if (_mesa_is_compressed_format(ctx, internalFormat)) {
1958       GLenum err;
1959       if (!_mesa_target_can_be_compressed(ctx, target, internalFormat, &err)) {
1960          _mesa_error(ctx, err,
1961                      "glTexImage%dD(target can't be compressed)", dimensions);
1962          return GL_TRUE;
1963       }
1964       if (_mesa_format_no_online_compression(internalFormat)) {
1965          _mesa_error(ctx, GL_INVALID_OPERATION,
1966                      "glTexImage%dD(no compression for format)", dimensions);
1967          return GL_TRUE;
1968       }
1969       if (border != 0) {
1970          _mesa_error(ctx, GL_INVALID_OPERATION,
1971                      "glTexImage%dD(border!=0)", dimensions);
1972          return GL_TRUE;
1973       }
1974    }
1975 
1976    /* additional checks for integer textures */
1977    if ((ctx->Version >= 30 || ctx->Extensions.EXT_texture_integer) &&
1978        (_mesa_is_enum_format_integer(format) !=
1979         _mesa_is_enum_format_integer(internalFormat))) {
1980       _mesa_error(ctx, GL_INVALID_OPERATION,
1981                   "glTexImage%dD(integer/non-integer format mismatch)",
1982                   dimensions);
1983       return GL_TRUE;
1984    }
1985 
1986    if (!mutable_tex_object(texObj)) {
1987       _mesa_error(ctx, GL_INVALID_OPERATION,
1988                   "glTexImage%dD(immutable texture)", dimensions);
1989       return GL_TRUE;
1990    }
1991 
1992    /* if we get here, the parameters are OK */
1993    return GL_FALSE;
1994 }
1995 
1996 
1997 /**
1998  * Error checking for glCompressedTexImage[123]D().
1999  * Note that the width, height and depth values are not fully error checked
2000  * here.
2001  * \return GL_TRUE if a error is found, GL_FALSE otherwise
2002  */
2003 static GLenum
compressed_texture_error_check(struct gl_context * ctx,GLint dimensions,GLenum target,struct gl_texture_object * texObj,GLint level,GLenum internalFormat,GLsizei width,GLsizei height,GLsizei depth,GLint border,GLsizei imageSize,const GLvoid * data)2004 compressed_texture_error_check(struct gl_context *ctx, GLint dimensions,
2005                                GLenum target, struct gl_texture_object* texObj,
2006                                GLint level, GLenum internalFormat, GLsizei width,
2007                                GLsizei height, GLsizei depth, GLint border,
2008                                GLsizei imageSize, const GLvoid *data)
2009 {
2010    const GLint maxLevels = _mesa_max_texture_levels(ctx, target);
2011    GLint expectedSize;
2012    GLenum error = GL_NO_ERROR;
2013    char *reason = ""; /* no error */
2014 
2015    if (!_mesa_target_can_be_compressed(ctx, target, internalFormat, &error)) {
2016       reason = "target";
2017       goto error;
2018    }
2019 
2020    /* This will detect any invalid internalFormat value */
2021    if (!_mesa_is_compressed_format(ctx, internalFormat)) {
2022       _mesa_error(ctx, GL_INVALID_ENUM,
2023                   "glCompressedTexImage%dD(internalFormat=%s)",
2024                   dimensions, _mesa_enum_to_string(internalFormat));
2025       return GL_TRUE;
2026    }
2027 
2028    /* validate the bound PBO, if any */
2029    if (!_mesa_validate_pbo_source_compressed(ctx, dimensions, &ctx->Unpack,
2030                                              imageSize, data,
2031                                              "glCompressedTexImage")) {
2032       return GL_TRUE;
2033    }
2034 
2035    switch (internalFormat) {
2036    case GL_PALETTE4_RGB8_OES:
2037    case GL_PALETTE4_RGBA8_OES:
2038    case GL_PALETTE4_R5_G6_B5_OES:
2039    case GL_PALETTE4_RGBA4_OES:
2040    case GL_PALETTE4_RGB5_A1_OES:
2041    case GL_PALETTE8_RGB8_OES:
2042    case GL_PALETTE8_RGBA8_OES:
2043    case GL_PALETTE8_R5_G6_B5_OES:
2044    case GL_PALETTE8_RGBA4_OES:
2045    case GL_PALETTE8_RGB5_A1_OES:
2046       /* check level (note that level should be zero or less!) */
2047       if (level > 0 || level < -maxLevels) {
2048          reason = "level";
2049          error = GL_INVALID_VALUE;
2050          goto error;
2051       }
2052 
2053       if (dimensions != 2) {
2054          reason = "compressed paletted textures must be 2D";
2055          error = GL_INVALID_OPERATION;
2056          goto error;
2057       }
2058 
2059       /* Figure out the expected texture size (in bytes).  This will be
2060        * checked against the actual size later.
2061        */
2062       expectedSize = _mesa_cpal_compressed_size(level, internalFormat,
2063                                                 width, height);
2064 
2065       /* This is for the benefit of the TestProxyTexImage below.  It expects
2066        * level to be non-negative.  OES_compressed_paletted_texture uses a
2067        * weird mechanism where the level specified to glCompressedTexImage2D
2068        * is -(n-1) number of levels in the texture, and the data specifies the
2069        * complete mipmap stack.  This is done to ensure the palette is the
2070        * same for all levels.
2071        */
2072       level = -level;
2073       break;
2074 
2075    default:
2076       /* check level */
2077       if (level < 0 || level >= maxLevels) {
2078          reason = "level";
2079          error = GL_INVALID_VALUE;
2080          goto error;
2081       }
2082 
2083       /* Figure out the expected texture size (in bytes).  This will be
2084        * checked against the actual size later.
2085        */
2086       expectedSize = compressed_tex_size(width, height, depth, internalFormat);
2087       break;
2088    }
2089 
2090    /* This should really never fail */
2091    if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
2092       reason = "internalFormat";
2093       error = GL_INVALID_ENUM;
2094       goto error;
2095    }
2096 
2097    /* No compressed formats support borders at this time */
2098    if (border != 0) {
2099       reason = "border != 0";
2100       error = GL_INVALID_VALUE;
2101       goto error;
2102    }
2103 
2104    /* Check for invalid pixel storage modes */
2105    if (!_mesa_compressed_pixel_storage_error_check(ctx, dimensions,
2106                                                    &ctx->Unpack,
2107                                                    "glCompressedTexImage")) {
2108       return GL_FALSE;
2109    }
2110 
2111    /* check image size in bytes */
2112    if (expectedSize != imageSize) {
2113       /* Per GL_ARB_texture_compression:  GL_INVALID_VALUE is generated [...]
2114        * if <imageSize> is not consistent with the format, dimensions, and
2115        * contents of the specified image.
2116        */
2117       reason = "imageSize inconsistent with width/height/format";
2118       error = GL_INVALID_VALUE;
2119       goto error;
2120    }
2121 
2122    if (!mutable_tex_object(texObj)) {
2123       reason = "immutable texture";
2124       error = GL_INVALID_OPERATION;
2125       goto error;
2126    }
2127 
2128    return GL_FALSE;
2129 
2130 error:
2131    /* Note: not all error paths exit through here. */
2132    _mesa_error(ctx, error, "glCompressedTexImage%dD(%s)",
2133                dimensions, reason);
2134    return GL_TRUE;
2135 }
2136 
2137 
2138 
2139 /**
2140  * Test glTexSubImage[123]D() parameters for errors.
2141  *
2142  * \param ctx GL context.
2143  * \param dimensions texture image dimensions (must be 1, 2 or 3).
2144  * \param target texture target given by the user (already validated)
2145  * \param level image level given by the user.
2146  * \param xoffset sub-image x offset given by the user.
2147  * \param yoffset sub-image y offset given by the user.
2148  * \param zoffset sub-image z offset given by the user.
2149  * \param format pixel data format given by the user.
2150  * \param type pixel data type given by the user.
2151  * \param width image width given by the user.
2152  * \param height image height given by the user.
2153  * \param depth image depth given by the user.
2154  *
2155  * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2156  *
2157  * Verifies each of the parameters against the constants specified in
2158  * __struct gl_contextRec::Const and the supported extensions, and according
2159  * to the OpenGL specification.
2160  */
2161 static GLboolean
texsubimage_error_check(struct gl_context * ctx,GLuint dimensions,struct gl_texture_object * texObj,GLenum target,GLint level,GLint xoffset,GLint yoffset,GLint zoffset,GLint width,GLint height,GLint depth,GLenum format,GLenum type,const GLvoid * pixels,const char * callerName)2162 texsubimage_error_check(struct gl_context *ctx, GLuint dimensions,
2163                         struct gl_texture_object *texObj,
2164                         GLenum target, GLint level,
2165                         GLint xoffset, GLint yoffset, GLint zoffset,
2166                         GLint width, GLint height, GLint depth,
2167                         GLenum format, GLenum type, const GLvoid *pixels,
2168                         const char *callerName)
2169 {
2170    struct gl_texture_image *texImage;
2171    GLenum err;
2172 
2173    if (!texObj) {
2174       /* must be out of memory */
2175       _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s()", callerName);
2176       return GL_TRUE;
2177    }
2178 
2179    /* level check */
2180    if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
2181       _mesa_error(ctx, GL_INVALID_VALUE, "%s(level=%d)", callerName, level);
2182       return GL_TRUE;
2183    }
2184 
2185    if (error_check_subtexture_negative_dimensions(ctx, dimensions,
2186                                                   width, height, depth,
2187                                                   callerName)) {
2188       return GL_TRUE;
2189    }
2190 
2191    texImage = _mesa_select_tex_image(texObj, target, level);
2192    if (!texImage) {
2193       /* non-existant texture level */
2194       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid texture level %d)",
2195                   callerName, level);
2196       return GL_TRUE;
2197    }
2198 
2199    err = _mesa_error_check_format_and_type(ctx, format, type);
2200    if (err != GL_NO_ERROR) {
2201       _mesa_error(ctx, err,
2202                   "%s(incompatible format = %s, type = %s)",
2203                   callerName, _mesa_enum_to_string(format),
2204                   _mesa_enum_to_string(type));
2205       return GL_TRUE;
2206    }
2207 
2208    if (!texture_formats_agree(texImage->InternalFormat, format)) {
2209       _mesa_error(ctx, GL_INVALID_OPERATION,
2210                   "%s(incompatible internalFormat = %s, format = %s)",
2211                   callerName,
2212                   _mesa_enum_to_string(texImage->InternalFormat),
2213                   _mesa_enum_to_string(format));
2214       return GL_TRUE;
2215    }
2216 
2217    GLenum internalFormat = _mesa_is_gles(ctx) ?
2218       oes_float_internal_format(ctx, texImage->InternalFormat, type) :
2219       texImage->InternalFormat;
2220 
2221    /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2222     * combinations of format, internalFormat, and type that can be used.
2223     * Formats and types that require additional extensions (e.g., GL_FLOAT
2224     * requires GL_OES_texture_float) are filtered elsewhere.
2225     */
2226    if (_mesa_is_gles(ctx) &&
2227        texture_format_error_check_gles(ctx, format, type,
2228                                        internalFormat, callerName)) {
2229       return GL_TRUE;
2230    }
2231 
2232    /* validate the bound PBO, if any */
2233    if (!_mesa_validate_pbo_source(ctx, dimensions, &ctx->Unpack,
2234                                   width, height, depth, format, type,
2235                                   INT_MAX, pixels, callerName)) {
2236       return GL_TRUE;
2237    }
2238 
2239    if (error_check_subtexture_dimensions(ctx, dimensions,
2240                                          texImage, xoffset, yoffset, zoffset,
2241                                          width, height, depth, callerName)) {
2242       return GL_TRUE;
2243    }
2244 
2245    if (_mesa_is_format_compressed(texImage->TexFormat)) {
2246       if (_mesa_format_no_online_compression(texImage->InternalFormat)) {
2247          _mesa_error(ctx, GL_INVALID_OPERATION,
2248                "%s(no compression for format)", callerName);
2249          return GL_TRUE;
2250       }
2251    }
2252 
2253    if (ctx->Version >= 30 || ctx->Extensions.EXT_texture_integer) {
2254       /* both source and dest must be integer-valued, or neither */
2255       if (_mesa_is_format_integer_color(texImage->TexFormat) !=
2256           _mesa_is_enum_format_integer(format)) {
2257          _mesa_error(ctx, GL_INVALID_OPERATION,
2258                      "%s(integer/non-integer format mismatch)", callerName);
2259          return GL_TRUE;
2260       }
2261    }
2262 
2263    return GL_FALSE;
2264 }
2265 
2266 
2267 /**
2268  * Test glCopyTexImage[12]D() parameters for errors.
2269  *
2270  * \param ctx GL context.
2271  * \param dimensions texture image dimensions (must be 1, 2 or 3).
2272  * \param target texture target given by the user.
2273  * \param level image level given by the user.
2274  * \param internalFormat internal format given by the user.
2275  * \param width image width given by the user.
2276  * \param height image height given by the user.
2277  * \param border texture border.
2278  *
2279  * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2280  *
2281  * Verifies each of the parameters against the constants specified in
2282  * __struct gl_contextRec::Const and the supported extensions, and according
2283  * to the OpenGL specification.
2284  */
2285 static GLboolean
copytexture_error_check(struct gl_context * ctx,GLuint dimensions,GLenum target,struct gl_texture_object * texObj,GLint level,GLint internalFormat,GLint border)2286 copytexture_error_check( struct gl_context *ctx, GLuint dimensions,
2287                          GLenum target, struct gl_texture_object* texObj,
2288                          GLint level, GLint internalFormat, GLint border )
2289 {
2290    GLint baseFormat;
2291    GLint rb_base_format;
2292    struct gl_renderbuffer *rb;
2293    GLenum rb_internal_format;
2294 
2295    /* check target */
2296    if (!legal_texsubimage_target(ctx, dimensions, target, false)) {
2297       _mesa_error(ctx, GL_INVALID_ENUM, "glCopyTexImage%uD(target=%s)",
2298                   dimensions, _mesa_enum_to_string(target));
2299       return GL_TRUE;
2300    }
2301 
2302    /* level check */
2303    if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
2304       _mesa_error(ctx, GL_INVALID_VALUE,
2305                   "glCopyTexImage%dD(level=%d)", dimensions, level);
2306       return GL_TRUE;
2307    }
2308 
2309    /* Check that the source buffer is complete */
2310    if (_mesa_is_user_fbo(ctx->ReadBuffer)) {
2311       if (ctx->ReadBuffer->_Status == 0) {
2312          _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
2313       }
2314       if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2315          _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2316                      "glCopyTexImage%dD(invalid readbuffer)", dimensions);
2317          return GL_TRUE;
2318       }
2319 
2320       if (ctx->ReadBuffer->Visual.samples > 0) {
2321          _mesa_error(ctx, GL_INVALID_OPERATION,
2322                      "glCopyTexImage%dD(multisample FBO)", dimensions);
2323          return GL_TRUE;
2324       }
2325    }
2326 
2327    /* Check border */
2328    if (border < 0 || border > 1 ||
2329        ((ctx->API != API_OPENGL_COMPAT ||
2330          target == GL_TEXTURE_RECTANGLE_NV ||
2331          target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
2332       _mesa_error(ctx, GL_INVALID_VALUE,
2333                   "glCopyTexImage%dD(border=%d)", dimensions, border);
2334       return GL_TRUE;
2335    }
2336 
2337    /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2338     * internalFormat.
2339     */
2340    if (_mesa_is_gles(ctx) && !_mesa_is_gles3(ctx)) {
2341       switch (internalFormat) {
2342       case GL_ALPHA:
2343       case GL_RGB:
2344       case GL_RGBA:
2345       case GL_LUMINANCE:
2346       case GL_LUMINANCE_ALPHA:
2347          break;
2348       default:
2349          _mesa_error(ctx, GL_INVALID_ENUM,
2350                      "glCopyTexImage%dD(internalFormat=%s)", dimensions,
2351                      _mesa_enum_to_string(internalFormat));
2352          return GL_TRUE;
2353       }
2354    } else {
2355       /*
2356        * Section 8.6 (Alternate Texture Image Specification Commands) of the
2357        * OpenGL 4.5 (Compatibility Profile) spec says:
2358        *
2359        *     "Parameters level, internalformat, and border are specified using
2360        *     the same values, with the same meanings, as the corresponding
2361        *     arguments of TexImage2D, except that internalformat may not be
2362        *     specified as 1, 2, 3, or 4."
2363        */
2364       if (internalFormat >= 1 && internalFormat <= 4) {
2365          _mesa_error(ctx, GL_INVALID_ENUM,
2366                      "glCopyTexImage%dD(internalFormat=%d)", dimensions,
2367                      internalFormat);
2368          return GL_TRUE;
2369       }
2370    }
2371 
2372    baseFormat = _mesa_base_tex_format(ctx, internalFormat);
2373    if (baseFormat < 0) {
2374       _mesa_error(ctx, GL_INVALID_ENUM,
2375                   "glCopyTexImage%dD(internalFormat=%s)", dimensions,
2376                   _mesa_enum_to_string(internalFormat));
2377       return GL_TRUE;
2378    }
2379 
2380    rb = _mesa_get_read_renderbuffer_for_format(ctx, internalFormat);
2381    if (rb == NULL) {
2382       _mesa_error(ctx, GL_INVALID_OPERATION,
2383                   "glCopyTexImage%dD(read buffer)", dimensions);
2384       return GL_TRUE;
2385    }
2386 
2387    rb_internal_format = rb->InternalFormat;
2388    rb_base_format = _mesa_base_tex_format(ctx, rb->InternalFormat);
2389    if (_mesa_is_color_format(internalFormat)) {
2390       if (rb_base_format < 0) {
2391          _mesa_error(ctx, GL_INVALID_VALUE,
2392                      "glCopyTexImage%dD(internalFormat=%s)", dimensions,
2393                      _mesa_enum_to_string(internalFormat));
2394          return GL_TRUE;
2395       }
2396    }
2397 
2398    if (_mesa_is_gles(ctx)) {
2399       bool valid = true;
2400       if (_mesa_components_in_format(baseFormat) >
2401           _mesa_components_in_format(rb_base_format)) {
2402          valid = false;
2403       }
2404       if (baseFormat == GL_DEPTH_COMPONENT ||
2405           baseFormat == GL_DEPTH_STENCIL ||
2406           baseFormat == GL_STENCIL_INDEX ||
2407           rb_base_format == GL_DEPTH_COMPONENT ||
2408           rb_base_format == GL_DEPTH_STENCIL ||
2409           rb_base_format == GL_STENCIL_INDEX ||
2410           ((baseFormat == GL_LUMINANCE_ALPHA ||
2411             baseFormat == GL_ALPHA) &&
2412            rb_base_format != GL_RGBA) ||
2413           internalFormat == GL_RGB9_E5) {
2414          valid = false;
2415       }
2416       if (internalFormat == GL_RGB9_E5) {
2417          valid = false;
2418       }
2419       if (!valid) {
2420          _mesa_error(ctx, GL_INVALID_OPERATION,
2421                      "glCopyTexImage%dD(internalFormat=%s)", dimensions,
2422                      _mesa_enum_to_string(internalFormat));
2423          return GL_TRUE;
2424       }
2425    }
2426 
2427    if (_mesa_is_gles3(ctx)) {
2428       bool rb_is_srgb = (ctx->Extensions.EXT_sRGB &&
2429                          _mesa_is_format_srgb(rb->Format));
2430       bool dst_is_srgb = false;
2431 
2432       if (_mesa_get_linear_internalformat(internalFormat) != internalFormat) {
2433          dst_is_srgb = true;
2434       }
2435 
2436       if (rb_is_srgb != dst_is_srgb) {
2437          /* Page 137 (page 149 of the PDF) in section 3.8.5 of the
2438           * OpenGLES 3.0.0 spec says:
2439           *
2440           *     "The error INVALID_OPERATION is also generated if the
2441           *     value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING for the
2442           *     framebuffer attachment corresponding to the read buffer
2443           *     is LINEAR (see section 6.1.13) and internalformat is
2444           *     one of the sRGB formats described in section 3.8.16, or
2445           *     if the value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING is
2446           *     SRGB and internalformat is not one of the sRGB formats."
2447           */
2448          _mesa_error(ctx, GL_INVALID_OPERATION,
2449                      "glCopyTexImage%dD(srgb usage mismatch)", dimensions);
2450          return GL_TRUE;
2451       }
2452 
2453       /* Page 139, Table 3.15 of OpenGL ES 3.0 spec does not define ReadPixels
2454        * types for SNORM formats. Also, conversion to SNORM formats is not
2455        * allowed by Table 3.2 on Page 110.
2456        */
2457       if (!_mesa_has_EXT_render_snorm(ctx) &&
2458           _mesa_is_enum_format_snorm(internalFormat)) {
2459          _mesa_error(ctx, GL_INVALID_OPERATION,
2460                      "glCopyTexImage%dD(internalFormat=%s)", dimensions,
2461                      _mesa_enum_to_string(internalFormat));
2462          return GL_TRUE;
2463       }
2464    }
2465 
2466    if (!_mesa_source_buffer_exists(ctx, baseFormat)) {
2467       _mesa_error(ctx, GL_INVALID_OPERATION,
2468                   "glCopyTexImage%dD(missing readbuffer)", dimensions);
2469       return GL_TRUE;
2470    }
2471 
2472    /* From the EXT_texture_integer spec:
2473     *
2474     *     "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
2475     *      if the texture internalformat is an integer format and the read color
2476     *      buffer is not an integer format, or if the internalformat is not an
2477     *      integer format and the read color buffer is an integer format."
2478     */
2479    if (_mesa_is_color_format(internalFormat)) {
2480       bool is_int = _mesa_is_enum_format_integer(internalFormat);
2481       bool is_rbint = _mesa_is_enum_format_integer(rb_internal_format);
2482       bool is_unorm = _mesa_is_enum_format_unorm(internalFormat);
2483       bool is_rbunorm = _mesa_is_enum_format_unorm(rb_internal_format);
2484       if (is_int || is_rbint) {
2485          if (is_int != is_rbint) {
2486             _mesa_error(ctx, GL_INVALID_OPERATION,
2487                         "glCopyTexImage%dD(integer vs non-integer)", dimensions);
2488             return GL_TRUE;
2489          } else if (_mesa_is_gles(ctx) &&
2490                     _mesa_is_enum_format_unsigned_int(internalFormat) !=
2491                       _mesa_is_enum_format_unsigned_int(rb_internal_format)) {
2492             _mesa_error(ctx, GL_INVALID_OPERATION,
2493                         "glCopyTexImage%dD(signed vs unsigned integer)",
2494                         dimensions);
2495             return GL_TRUE;
2496          }
2497       }
2498 
2499       /* From page 138 of OpenGL ES 3.0 spec:
2500        *    "The error INVALID_OPERATION is generated if floating-point RGBA
2501        *    data is required; if signed integer RGBA data is required and the
2502        *    format of the current color buffer is not signed integer; if
2503        *    unsigned integer RGBA data is required and the format of the
2504        *    current color buffer is not unsigned integer; or if fixed-point
2505        *    RGBA data is required and the format of the current color buffer
2506        *    is not fixed-point.
2507        */
2508       if (_mesa_is_gles(ctx) && is_unorm != is_rbunorm)
2509             _mesa_error(ctx, GL_INVALID_OPERATION,
2510                         "glCopyTexImage%dD(unorm vs non-unorm)", dimensions);
2511    }
2512 
2513    if (_mesa_is_compressed_format(ctx, internalFormat)) {
2514       GLenum err;
2515       if (!_mesa_target_can_be_compressed(ctx, target, internalFormat, &err)) {
2516          _mesa_error(ctx, err,
2517                      "glCopyTexImage%dD(target can't be compressed)", dimensions);
2518          return GL_TRUE;
2519       }
2520       if (_mesa_format_no_online_compression(internalFormat)) {
2521          _mesa_error(ctx, GL_INVALID_OPERATION,
2522                "glCopyTexImage%dD(no compression for format)", dimensions);
2523          return GL_TRUE;
2524       }
2525       if (border != 0) {
2526          _mesa_error(ctx, GL_INVALID_OPERATION,
2527                      "glCopyTexImage%dD(border!=0)", dimensions);
2528          return GL_TRUE;
2529       }
2530    }
2531 
2532    if (!mutable_tex_object(texObj)) {
2533       _mesa_error(ctx, GL_INVALID_OPERATION,
2534                   "glCopyTexImage%dD(immutable texture)", dimensions);
2535       return GL_TRUE;
2536    }
2537 
2538    /* if we get here, the parameters are OK */
2539    return GL_FALSE;
2540 }
2541 
2542 
2543 /**
2544  * Test glCopyTexSubImage[12]D() parameters for errors.
2545  * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2546  */
2547 static GLboolean
copytexsubimage_error_check(struct gl_context * ctx,GLuint dimensions,const struct gl_texture_object * texObj,GLenum target,GLint level,GLint xoffset,GLint yoffset,GLint zoffset,GLint width,GLint height,const char * caller)2548 copytexsubimage_error_check(struct gl_context *ctx, GLuint dimensions,
2549                             const struct gl_texture_object *texObj,
2550                             GLenum target, GLint level,
2551                             GLint xoffset, GLint yoffset, GLint zoffset,
2552                             GLint width, GLint height, const char *caller)
2553 {
2554    assert(texObj);
2555 
2556    struct gl_texture_image *texImage;
2557 
2558    /* Check that the source buffer is complete */
2559    if (_mesa_is_user_fbo(ctx->ReadBuffer)) {
2560       if (ctx->ReadBuffer->_Status == 0) {
2561          _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
2562       }
2563       if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2564          _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2565                      "%s(invalid readbuffer)", caller);
2566          return GL_TRUE;
2567       }
2568 
2569       if (ctx->ReadBuffer->Visual.samples > 0) {
2570          _mesa_error(ctx, GL_INVALID_OPERATION,
2571                 "%s(multisample FBO)", caller);
2572          return GL_TRUE;
2573       }
2574    }
2575 
2576    /* Check level */
2577    if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
2578       _mesa_error(ctx, GL_INVALID_VALUE, "%s(level=%d)", caller, level);
2579       return GL_TRUE;
2580    }
2581 
2582    texImage = _mesa_select_tex_image(texObj, target, level);
2583    if (!texImage) {
2584       /* destination image does not exist */
2585       _mesa_error(ctx, GL_INVALID_OPERATION,
2586                   "%s(invalid texture level %d)", caller, level);
2587       return GL_TRUE;
2588    }
2589 
2590    if (error_check_subtexture_negative_dimensions(ctx, dimensions,
2591                                                   width, height, 1, caller)) {
2592       return GL_TRUE;
2593    }
2594 
2595    if (error_check_subtexture_dimensions(ctx, dimensions, texImage,
2596                                          xoffset, yoffset, zoffset,
2597                                          width, height, 1, caller)) {
2598       return GL_TRUE;
2599    }
2600 
2601    if (_mesa_is_format_compressed(texImage->TexFormat)) {
2602       if (_mesa_format_no_online_compression(texImage->InternalFormat)) {
2603          _mesa_error(ctx, GL_INVALID_OPERATION,
2604                "%s(no compression for format)", caller);
2605          return GL_TRUE;
2606       }
2607    }
2608 
2609    if (texImage->InternalFormat == GL_YCBCR_MESA) {
2610       _mesa_error(ctx, GL_INVALID_OPERATION, "%s()", caller);
2611       return GL_TRUE;
2612    }
2613 
2614    /* From OpenGL ES 3.2 spec, section 8.6:
2615     *
2616     *     "An INVALID_OPERATION error is generated by CopyTexSubImage3D,
2617     *      CopyTexImage2D, or CopyTexSubImage2D if the internalformat of the
2618     *      texture image being (re)specified is RGB9_E5"
2619     */
2620    if (texImage->InternalFormat == GL_RGB9_E5 &&
2621        !_mesa_is_desktop_gl(ctx)) {
2622       _mesa_error(ctx, GL_INVALID_OPERATION,
2623                   "%s(invalid internal format %s)", caller,
2624                   _mesa_enum_to_string(texImage->InternalFormat));
2625       return GL_TRUE;
2626    }
2627 
2628    if (!_mesa_source_buffer_exists(ctx, texImage->_BaseFormat)) {
2629       _mesa_error(ctx, GL_INVALID_OPERATION,
2630                   "%s(missing readbuffer, format=%s)", caller,
2631                   _mesa_enum_to_string(texImage->_BaseFormat));
2632       return GL_TRUE;
2633    }
2634 
2635    /* From the EXT_texture_integer spec:
2636     *
2637     *     "INVALID_OPERATION is generated by CopyTexImage* and
2638     *     CopyTexSubImage* if the texture internalformat is an integer format
2639     *     and the read color buffer is not an integer format, or if the
2640     *     internalformat is not an integer format and the read color buffer
2641     *     is an integer format."
2642     */
2643    if (_mesa_is_color_format(texImage->InternalFormat)) {
2644       struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
2645 
2646       if (_mesa_is_format_integer_color(rb->Format) !=
2647           _mesa_is_format_integer_color(texImage->TexFormat)) {
2648          _mesa_error(ctx, GL_INVALID_OPERATION,
2649                      "%s(integer vs non-integer)", caller);
2650          return GL_TRUE;
2651       }
2652    }
2653 
2654    /* In the ES 3.2 specification's Table 8.13 (Valid CopyTexImage source
2655     * framebuffer/destination texture base internal format combinations),
2656     * all the entries for stencil are left blank (unsupported).
2657     */
2658    if (_mesa_is_gles(ctx) && _mesa_is_stencil_format(texImage->_BaseFormat)) {
2659       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(stencil disallowed)", caller);
2660       return GL_TRUE;
2661    }
2662 
2663    /* if we get here, the parameters are OK */
2664    return GL_FALSE;
2665 }
2666 
2667 
2668 /** Callback info for walking over FBO hash table */
2669 struct cb_info
2670 {
2671    struct gl_context *ctx;
2672    struct gl_texture_object *texObj;
2673    GLuint level, face;
2674 };
2675 
2676 
2677 /**
2678  * Check render to texture callback.  Called from _mesa_HashWalk().
2679  */
2680 static void
check_rtt_cb(void * data,void * userData)2681 check_rtt_cb(void *data, void *userData)
2682 {
2683    struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
2684    const struct cb_info *info = (struct cb_info *) userData;
2685    struct gl_context *ctx = info->ctx;
2686    const struct gl_texture_object *texObj = info->texObj;
2687    const GLuint level = info->level, face = info->face;
2688 
2689    /* If this is a user-created FBO */
2690    if (_mesa_is_user_fbo(fb)) {
2691       GLuint i;
2692       /* check if any of the FBO's attachments point to 'texObj' */
2693       for (i = 0; i < BUFFER_COUNT; i++) {
2694          struct gl_renderbuffer_attachment *att = fb->Attachment + i;
2695          if (att->Type == GL_TEXTURE &&
2696              att->Texture == texObj &&
2697              att->TextureLevel == level &&
2698              att->CubeMapFace == face) {
2699             _mesa_update_texture_renderbuffer(ctx, fb, att);
2700             assert(att->Renderbuffer->TexImage);
2701             /* Mark fb status as indeterminate to force re-validation */
2702             fb->_Status = 0;
2703 
2704             /* Make sure that the revalidation actually happens if this is
2705              * being done to currently-bound buffers.
2706              */
2707             if (fb == ctx->DrawBuffer || fb == ctx->ReadBuffer)
2708                ctx->NewState |= _NEW_BUFFERS;
2709          }
2710       }
2711    }
2712 }
2713 
2714 
2715 /**
2716  * When a texture image is specified we have to check if it's bound to
2717  * any framebuffer objects (render to texture) in order to detect changes
2718  * in size or format since that effects FBO completeness.
2719  * Any FBOs rendering into the texture must be re-validated.
2720  */
2721 void
_mesa_update_fbo_texture(struct gl_context * ctx,struct gl_texture_object * texObj,GLuint face,GLuint level)2722 _mesa_update_fbo_texture(struct gl_context *ctx,
2723                          struct gl_texture_object *texObj,
2724                          GLuint face, GLuint level)
2725 {
2726    /* Only check this texture if it's been marked as RenderToTexture */
2727    if (texObj->_RenderToTexture) {
2728       struct cb_info info;
2729       info.ctx = ctx;
2730       info.texObj = texObj;
2731       info.level = level;
2732       info.face = face;
2733       _mesa_HashWalk(ctx->Shared->FrameBuffers, check_rtt_cb, &info);
2734    }
2735 }
2736 
2737 
2738 /**
2739  * If the texture object's GenerateMipmap flag is set and we've
2740  * changed the texture base level image, regenerate the rest of the
2741  * mipmap levels now.
2742  */
2743 static inline void
check_gen_mipmap(struct gl_context * ctx,GLenum target,struct gl_texture_object * texObj,GLint level)2744 check_gen_mipmap(struct gl_context *ctx, GLenum target,
2745                  struct gl_texture_object *texObj, GLint level)
2746 {
2747    if (texObj->GenerateMipmap &&
2748        level == texObj->BaseLevel &&
2749        level < texObj->MaxLevel) {
2750       assert(ctx->Driver.GenerateMipmap);
2751       ctx->Driver.GenerateMipmap(ctx, target, texObj);
2752    }
2753 }
2754 
2755 
2756 /** Debug helper: override the user-requested internal format */
2757 static GLenum
override_internal_format(GLenum internalFormat,UNUSED GLint width,UNUSED GLint height)2758 override_internal_format(GLenum internalFormat, UNUSED GLint width,
2759                          UNUSED GLint height)
2760 {
2761 #if 0
2762    if (internalFormat == GL_RGBA16F_ARB ||
2763        internalFormat == GL_RGBA32F_ARB) {
2764       printf("Convert rgba float tex to int %d x %d\n", width, height);
2765       return GL_RGBA;
2766    }
2767    else if (internalFormat == GL_RGB16F_ARB ||
2768             internalFormat == GL_RGB32F_ARB) {
2769       printf("Convert rgb float tex to int %d x %d\n", width, height);
2770       return GL_RGB;
2771    }
2772    else if (internalFormat == GL_LUMINANCE_ALPHA16F_ARB ||
2773             internalFormat == GL_LUMINANCE_ALPHA32F_ARB) {
2774       printf("Convert luminance float tex to int %d x %d\n", width, height);
2775       return GL_LUMINANCE_ALPHA;
2776    }
2777    else if (internalFormat == GL_LUMINANCE16F_ARB ||
2778             internalFormat == GL_LUMINANCE32F_ARB) {
2779       printf("Convert luminance float tex to int %d x %d\n", width, height);
2780       return GL_LUMINANCE;
2781    }
2782    else if (internalFormat == GL_ALPHA16F_ARB ||
2783             internalFormat == GL_ALPHA32F_ARB) {
2784       printf("Convert luminance float tex to int %d x %d\n", width, height);
2785       return GL_ALPHA;
2786    }
2787    /*
2788    else if (internalFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) {
2789       internalFormat = GL_RGBA;
2790    }
2791    */
2792    else {
2793       return internalFormat;
2794    }
2795 #else
2796    return internalFormat;
2797 #endif
2798 }
2799 
2800 
2801 /**
2802  * Choose the actual hardware format for a texture image.
2803  * Try to use the same format as the previous image level when possible.
2804  * Otherwise, ask the driver for the best format.
2805  * It's important to try to choose a consistant format for all levels
2806  * for efficient texture memory layout/allocation.  In particular, this
2807  * comes up during automatic mipmap generation.
2808  */
2809 mesa_format
_mesa_choose_texture_format(struct gl_context * ctx,struct gl_texture_object * texObj,GLenum target,GLint level,GLenum internalFormat,GLenum format,GLenum type)2810 _mesa_choose_texture_format(struct gl_context *ctx,
2811                             struct gl_texture_object *texObj,
2812                             GLenum target, GLint level,
2813                             GLenum internalFormat, GLenum format, GLenum type)
2814 {
2815    mesa_format f;
2816 
2817    /* see if we've already chosen a format for the previous level */
2818    if (level > 0) {
2819       struct gl_texture_image *prevImage =
2820          _mesa_select_tex_image(texObj, target, level - 1);
2821       /* See if the prev level is defined and has an internal format which
2822        * matches the new internal format.
2823        */
2824       if (prevImage &&
2825           prevImage->Width > 0 &&
2826           prevImage->InternalFormat == internalFormat) {
2827          /* use the same format */
2828          assert(prevImage->TexFormat != MESA_FORMAT_NONE);
2829          return prevImage->TexFormat;
2830       }
2831    }
2832 
2833    f = ctx->Driver.ChooseTextureFormat(ctx, target, internalFormat,
2834                                        format, type);
2835    assert(f != MESA_FORMAT_NONE);
2836    return f;
2837 }
2838 
2839 
2840 /**
2841  * Adjust pixel unpack params and image dimensions to strip off the
2842  * one-pixel texture border.
2843  *
2844  * Gallium and intel don't support texture borders.  They've seldem been used
2845  * and seldom been implemented correctly anyway.
2846  *
2847  * \param unpackNew returns the new pixel unpack parameters
2848  */
2849 static void
strip_texture_border(GLenum target,GLint * width,GLint * height,GLint * depth,const struct gl_pixelstore_attrib * unpack,struct gl_pixelstore_attrib * unpackNew)2850 strip_texture_border(GLenum target,
2851                      GLint *width, GLint *height, GLint *depth,
2852                      const struct gl_pixelstore_attrib *unpack,
2853                      struct gl_pixelstore_attrib *unpackNew)
2854 {
2855    assert(width);
2856    assert(height);
2857    assert(depth);
2858 
2859    *unpackNew = *unpack;
2860 
2861    if (unpackNew->RowLength == 0)
2862       unpackNew->RowLength = *width;
2863 
2864    if (unpackNew->ImageHeight == 0)
2865       unpackNew->ImageHeight = *height;
2866 
2867    assert(*width >= 3);
2868    unpackNew->SkipPixels++;  /* skip the border */
2869    *width = *width - 2;      /* reduce the width by two border pixels */
2870 
2871    /* The min height of a texture with a border is 3 */
2872    if (*height >= 3 && target != GL_TEXTURE_1D_ARRAY) {
2873       unpackNew->SkipRows++;  /* skip the border */
2874       *height = *height - 2;  /* reduce the height by two border pixels */
2875    }
2876 
2877    if (*depth >= 3 &&
2878        target != GL_TEXTURE_2D_ARRAY &&
2879        target != GL_TEXTURE_CUBE_MAP_ARRAY) {
2880       unpackNew->SkipImages++;  /* skip the border */
2881       *depth = *depth - 2;      /* reduce the depth by two border pixels */
2882    }
2883 }
2884 
2885 static struct gl_texture_object *
lookup_texture_ext_dsa(struct gl_context * ctx,GLenum target,GLuint texture,const char * caller)2886 lookup_texture_ext_dsa(struct gl_context *ctx, GLenum target, GLuint texture,
2887                        const char *caller)
2888 {
2889    GLenum boundTarget;
2890    switch (target) {
2891    case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
2892    case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
2893    case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
2894    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
2895    case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
2896    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
2897       boundTarget = GL_TEXTURE_CUBE_MAP;
2898       break;
2899    default:
2900       boundTarget = target;
2901       break;
2902    }
2903 
2904    int targetIndex = _mesa_tex_target_to_index(ctx, boundTarget);
2905    if (targetIndex < 0) {
2906       _mesa_error(ctx, GL_INVALID_ENUM, "%s(target = %s)", caller,
2907                   _mesa_enum_to_string(target));
2908             return NULL;
2909    }
2910    assert(targetIndex < NUM_TEXTURE_TARGETS);
2911 
2912    struct gl_texture_object *texObj;
2913    if (texture == 0) {
2914       /* Use a default texture object */
2915       texObj = ctx->Shared->DefaultTex[targetIndex];
2916       assert(texObj);
2917    } else {
2918       bool isGenName;
2919       texObj = _mesa_lookup_texture(ctx, texture);
2920       isGenName = texObj != NULL;
2921       if (!texObj && ctx->API == API_OPENGL_CORE) {
2922          _mesa_error(ctx, GL_INVALID_OPERATION, "%s(non-gen name)", caller);
2923          return NULL;
2924       }
2925 
2926       if (!texObj) {
2927          texObj = ctx->Driver.NewTextureObject(ctx, texture, boundTarget);
2928          if (!texObj) {
2929             _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", caller);
2930             return NULL;
2931          }
2932 
2933          /* insert into hash table */
2934          _mesa_HashInsert(ctx->Shared->TexObjects, texObj->Name, texObj, isGenName);
2935       }
2936 
2937       if (texObj->Target != boundTarget) {
2938          _mesa_error(ctx, GL_INVALID_OPERATION, "%s(%s != %s)",
2939                      caller, _mesa_enum_to_string(texObj->Target),
2940                      _mesa_enum_to_string(target));
2941          return NULL;
2942       }
2943    }
2944 
2945    return texObj;
2946 }
2947 
2948 /**
2949  * Common code to implement all the glTexImage1D/2D/3D functions,
2950  * glCompressedTexImage1D/2D/3D and glTextureImage1D/2D/3DEXT
2951  * \param compressed  only GL_TRUE for glCompressedTexImage1D/2D/3D calls.
2952  * \param format  the user's image format (only used if !compressed)
2953  * \param type  the user's image type (only used if !compressed)
2954  * \param imageSize  only used for glCompressedTexImage1D/2D/3D calls.
2955  */
2956 static ALWAYS_INLINE void
teximage(struct gl_context * ctx,GLboolean compressed,GLuint dims,struct gl_texture_object * texObj,GLenum target,GLint level,GLint internalFormat,GLsizei width,GLsizei height,GLsizei depth,GLint border,GLenum format,GLenum type,GLsizei imageSize,const GLvoid * pixels,bool no_error)2957 teximage(struct gl_context *ctx, GLboolean compressed, GLuint dims,
2958          struct gl_texture_object *texObj,
2959          GLenum target, GLint level, GLint internalFormat,
2960          GLsizei width, GLsizei height, GLsizei depth,
2961          GLint border, GLenum format, GLenum type,
2962          GLsizei imageSize, const GLvoid *pixels, bool no_error)
2963 {
2964    const char *func = compressed ? "glCompressedTexImage" : "glTexImage";
2965    struct gl_pixelstore_attrib unpack_no_border;
2966    const struct gl_pixelstore_attrib *unpack = &ctx->Unpack;
2967    mesa_format texFormat;
2968    bool dimensionsOK = true, sizeOK = true;
2969 
2970    FLUSH_VERTICES(ctx, 0);
2971 
2972    if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE)) {
2973       if (compressed)
2974          _mesa_debug(ctx,
2975                      "glCompressedTexImage%uD %s %d %s %d %d %d %d %p\n",
2976                      dims,
2977                      _mesa_enum_to_string(target), level,
2978                      _mesa_enum_to_string(internalFormat),
2979                      width, height, depth, border, pixels);
2980       else
2981          _mesa_debug(ctx,
2982                      "glTexImage%uD %s %d %s %d %d %d %d %s %s %p\n",
2983                      dims,
2984                      _mesa_enum_to_string(target), level,
2985                      _mesa_enum_to_string(internalFormat),
2986                      width, height, depth, border,
2987                      _mesa_enum_to_string(format),
2988                      _mesa_enum_to_string(type), pixels);
2989    }
2990 
2991    internalFormat = override_internal_format(internalFormat, width, height);
2992 
2993    if (!no_error &&
2994        /* target error checking */
2995        !legal_teximage_target(ctx, dims, target)) {
2996       _mesa_error(ctx, GL_INVALID_ENUM, "%s%uD(target=%s)",
2997                   func, dims, _mesa_enum_to_string(target));
2998       return;
2999    }
3000 
3001    if (!texObj)
3002       texObj = _mesa_get_current_tex_object(ctx, target);
3003 
3004    if (!no_error) {
3005       /* general error checking */
3006       if (compressed) {
3007          if (compressed_texture_error_check(ctx, dims, target, texObj,
3008                                             level, internalFormat,
3009                                             width, height, depth,
3010                                             border, imageSize, pixels))
3011             return;
3012       } else {
3013          if (texture_error_check(ctx, dims, target, texObj, level, internalFormat,
3014                                  format, type, width, height, depth, border,
3015                                  pixels))
3016             return;
3017       }
3018    }
3019    assert(texObj);
3020 
3021    /* Here we convert a cpal compressed image into a regular glTexImage2D
3022     * call by decompressing the texture.  If we really want to support cpal
3023     * textures in any driver this would have to be changed.
3024     */
3025    if (ctx->API == API_OPENGLES && compressed && dims == 2) {
3026       switch (internalFormat) {
3027       case GL_PALETTE4_RGB8_OES:
3028       case GL_PALETTE4_RGBA8_OES:
3029       case GL_PALETTE4_R5_G6_B5_OES:
3030       case GL_PALETTE4_RGBA4_OES:
3031       case GL_PALETTE4_RGB5_A1_OES:
3032       case GL_PALETTE8_RGB8_OES:
3033       case GL_PALETTE8_RGBA8_OES:
3034       case GL_PALETTE8_R5_G6_B5_OES:
3035       case GL_PALETTE8_RGBA4_OES:
3036       case GL_PALETTE8_RGB5_A1_OES:
3037          _mesa_cpal_compressed_teximage2d(target, level, internalFormat,
3038                                           width, height, imageSize, pixels);
3039          return;
3040       }
3041    }
3042 
3043    if (compressed) {
3044       /* For glCompressedTexImage() the driver has no choice about the
3045        * texture format since we'll never transcode the user's compressed
3046        * image data.  The internalFormat was error checked earlier.
3047        */
3048       texFormat = _mesa_glenum_to_compressed_format(internalFormat);
3049    }
3050    else {
3051       /* In case of HALF_FLOAT_OES or FLOAT_OES, find corresponding sized
3052        * internal floating point format for the given base format.
3053        */
3054       if (_mesa_is_gles(ctx) && format == internalFormat) {
3055          if (type == GL_FLOAT) {
3056             texObj->_IsFloat = GL_TRUE;
3057          } else if (type == GL_HALF_FLOAT_OES || type == GL_HALF_FLOAT) {
3058             texObj->_IsHalfFloat = GL_TRUE;
3059          }
3060 
3061          internalFormat = adjust_for_oes_float_texture(ctx, format, type);
3062       }
3063 
3064       texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
3065                                               internalFormat, format, type);
3066    }
3067 
3068    assert(texFormat != MESA_FORMAT_NONE);
3069 
3070    if (!no_error) {
3071       /* check that width, height, depth are legal for the mipmap level */
3072       dimensionsOK = _mesa_legal_texture_dimensions(ctx, target, level, width,
3073                                                     height, depth, border);
3074 
3075       /* check that the texture won't take too much memory, etc */
3076       sizeOK = ctx->Driver.TestProxyTexImage(ctx, proxy_target(target),
3077                                              0, level, texFormat, 1,
3078                                              width, height, depth);
3079    }
3080 
3081    if (_mesa_is_proxy_texture(target)) {
3082       /* Proxy texture: just clear or set state depending on error checking */
3083       struct gl_texture_image *texImage =
3084          get_proxy_tex_image(ctx, target, level);
3085 
3086       if (!texImage)
3087          return;  /* GL_OUT_OF_MEMORY already recorded */
3088 
3089       if (dimensionsOK && sizeOK) {
3090          _mesa_init_teximage_fields(ctx, texImage, width, height, depth,
3091                                     border, internalFormat, texFormat);
3092       }
3093       else {
3094          clear_teximage_fields(texImage);
3095       }
3096    }
3097    else {
3098       /* non-proxy target */
3099       const GLuint face = _mesa_tex_target_to_face(target);
3100       struct gl_texture_image *texImage;
3101 
3102       if (!dimensionsOK) {
3103          _mesa_error(ctx, GL_INVALID_VALUE,
3104                      "%s%uD(invalid width=%d or height=%d or depth=%d)",
3105                      func, dims, width, height, depth);
3106          return;
3107       }
3108 
3109       if (!sizeOK) {
3110          _mesa_error(ctx, GL_OUT_OF_MEMORY,
3111                      "%s%uD(image too large: %d x %d x %d, %s format)",
3112                      func, dims, width, height, depth,
3113                      _mesa_enum_to_string(internalFormat));
3114          return;
3115       }
3116 
3117       /* Allow a hardware driver to just strip out the border, to provide
3118        * reliable but slightly incorrect hardware rendering instead of
3119        * rarely-tested software fallback rendering.
3120        */
3121       if (border && ctx->Const.StripTextureBorder) {
3122          strip_texture_border(target, &width, &height, &depth, unpack,
3123                               &unpack_no_border);
3124          border = 0;
3125          unpack = &unpack_no_border;
3126       }
3127 
3128       if (ctx->NewState & _NEW_PIXEL)
3129          _mesa_update_state(ctx);
3130 
3131       _mesa_lock_texture(ctx, texObj);
3132       {
3133          texImage = _mesa_get_tex_image(ctx, texObj, target, level);
3134 
3135          if (!texImage) {
3136             _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s%uD", func, dims);
3137          }
3138          else {
3139             ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
3140 
3141             _mesa_init_teximage_fields(ctx, texImage,
3142                                        width, height, depth,
3143                                        border, internalFormat, texFormat);
3144 
3145             /* Give the texture to the driver.  <pixels> may be null. */
3146             if (width > 0 && height > 0 && depth > 0) {
3147                if (compressed) {
3148                   ctx->Driver.CompressedTexImage(ctx, dims, texImage,
3149                                                  imageSize, pixels);
3150                }
3151                else {
3152                   ctx->Driver.TexImage(ctx, dims, texImage, format,
3153                                        type, pixels, unpack);
3154                }
3155             }
3156 
3157             check_gen_mipmap(ctx, target, texObj, level);
3158 
3159             _mesa_update_fbo_texture(ctx, texObj, face, level);
3160 
3161             _mesa_dirty_texobj(ctx, texObj);
3162          }
3163       }
3164       _mesa_unlock_texture(ctx, texObj);
3165    }
3166 }
3167 
3168 
3169 /* This is a wrapper around teximage() so that we can force the KHR_no_error
3170  * logic to be inlined without inlining the function into all the callers.
3171  */
3172 static void
teximage_err(struct gl_context * ctx,GLboolean compressed,GLuint dims,GLenum target,GLint level,GLint internalFormat,GLsizei width,GLsizei height,GLsizei depth,GLint border,GLenum format,GLenum type,GLsizei imageSize,const GLvoid * pixels)3173 teximage_err(struct gl_context *ctx, GLboolean compressed, GLuint dims,
3174              GLenum target, GLint level, GLint internalFormat,
3175              GLsizei width, GLsizei height, GLsizei depth,
3176              GLint border, GLenum format, GLenum type,
3177              GLsizei imageSize, const GLvoid *pixels)
3178 {
3179    teximage(ctx, compressed, dims, NULL, target, level, internalFormat, width, height,
3180             depth, border, format, type, imageSize, pixels, false);
3181 }
3182 
3183 
3184 static void
teximage_no_error(struct gl_context * ctx,GLboolean compressed,GLuint dims,GLenum target,GLint level,GLint internalFormat,GLsizei width,GLsizei height,GLsizei depth,GLint border,GLenum format,GLenum type,GLsizei imageSize,const GLvoid * pixels)3185 teximage_no_error(struct gl_context *ctx, GLboolean compressed, GLuint dims,
3186                   GLenum target, GLint level, GLint internalFormat,
3187                   GLsizei width, GLsizei height, GLsizei depth,
3188                   GLint border, GLenum format, GLenum type,
3189                   GLsizei imageSize, const GLvoid *pixels)
3190 {
3191    teximage(ctx, compressed, dims, NULL, target, level, internalFormat, width, height,
3192             depth, border, format, type, imageSize, pixels, true);
3193 }
3194 
3195 
3196 /*
3197  * Called from the API.  Note that width includes the border.
3198  */
3199 void GLAPIENTRY
_mesa_TexImage1D(GLenum target,GLint level,GLint internalFormat,GLsizei width,GLint border,GLenum format,GLenum type,const GLvoid * pixels)3200 _mesa_TexImage1D( GLenum target, GLint level, GLint internalFormat,
3201                   GLsizei width, GLint border, GLenum format,
3202                   GLenum type, const GLvoid *pixels )
3203 {
3204    GET_CURRENT_CONTEXT(ctx);
3205    teximage_err(ctx, GL_FALSE, 1, target, level, internalFormat, width, 1, 1,
3206                 border, format, type, 0, pixels);
3207 }
3208 
3209 void GLAPIENTRY
_mesa_TextureImage1DEXT(GLuint texture,GLenum target,GLint level,GLint internalFormat,GLsizei width,GLint border,GLenum format,GLenum type,const GLvoid * pixels)3210 _mesa_TextureImage1DEXT(GLuint texture, GLenum target, GLint level,
3211                       GLint internalFormat, GLsizei width, GLint border,
3212                       GLenum format, GLenum type, const GLvoid *pixels )
3213 {
3214    struct gl_texture_object*  texObj;
3215    GET_CURRENT_CONTEXT(ctx);
3216 
3217    texObj = _mesa_lookup_or_create_texture(ctx, target, texture, false, true,
3218                                            "glTextureImage1DEXT");
3219    if (!texObj)
3220       return;
3221    teximage(ctx, GL_FALSE, 1, texObj, target, level, internalFormat,
3222             width, 1, 1, border, format, type, 0, pixels, false);
3223 }
3224 
3225 void GLAPIENTRY
_mesa_MultiTexImage1DEXT(GLenum texunit,GLenum target,GLint level,GLint internalFormat,GLsizei width,GLint border,GLenum format,GLenum type,const GLvoid * pixels)3226 _mesa_MultiTexImage1DEXT(GLenum texunit, GLenum target, GLint level,
3227                          GLint internalFormat, GLsizei width, GLint border,
3228                          GLenum format, GLenum type, const GLvoid *pixels )
3229 {
3230    struct gl_texture_object*  texObj;
3231    GET_CURRENT_CONTEXT(ctx);
3232 
3233    texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
3234                                                    texunit - GL_TEXTURE0,
3235                                                    true,
3236                                                    "glMultiTexImage1DEXT");
3237    if (!texObj)
3238       return;
3239    teximage(ctx, GL_FALSE, 1, texObj, target, level, internalFormat, width, 1, 1,
3240                 border, format, type, 0, pixels, false);
3241 }
3242 
3243 void GLAPIENTRY
_mesa_TexImage2D(GLenum target,GLint level,GLint internalFormat,GLsizei width,GLsizei height,GLint border,GLenum format,GLenum type,const GLvoid * pixels)3244 _mesa_TexImage2D( GLenum target, GLint level, GLint internalFormat,
3245                   GLsizei width, GLsizei height, GLint border,
3246                   GLenum format, GLenum type,
3247                   const GLvoid *pixels )
3248 {
3249    GET_CURRENT_CONTEXT(ctx);
3250    teximage_err(ctx, GL_FALSE, 2, target, level, internalFormat, width, height, 1,
3251                 border, format, type, 0, pixels);
3252 }
3253 
3254 void GLAPIENTRY
_mesa_TextureImage2DEXT(GLuint texture,GLenum target,GLint level,GLint internalFormat,GLsizei width,GLsizei height,GLint border,GLenum format,GLenum type,const GLvoid * pixels)3255 _mesa_TextureImage2DEXT(GLuint texture, GLenum target, GLint level,
3256                       GLint internalFormat, GLsizei width, GLsizei height,
3257                       GLint border,
3258                       GLenum format, GLenum type, const GLvoid *pixels )
3259 {
3260    struct gl_texture_object*  texObj;
3261    GET_CURRENT_CONTEXT(ctx);
3262 
3263    texObj = _mesa_lookup_or_create_texture(ctx, target, texture, false, true,
3264                                            "glTextureImage2DEXT");
3265    if (!texObj)
3266       return;
3267    teximage(ctx, GL_FALSE, 2, texObj, target, level, internalFormat,
3268             width, height, 1, border, format, type, 0, pixels, false);
3269 }
3270 
3271 void GLAPIENTRY
_mesa_MultiTexImage2DEXT(GLenum texunit,GLenum target,GLint level,GLint internalFormat,GLsizei width,GLsizei height,GLint border,GLenum format,GLenum type,const GLvoid * pixels)3272 _mesa_MultiTexImage2DEXT(GLenum texunit, GLenum target, GLint level,
3273                          GLint internalFormat, GLsizei width, GLsizei height,
3274                          GLint border,
3275                          GLenum format, GLenum type, const GLvoid *pixels )
3276 {
3277    struct gl_texture_object*  texObj;
3278    GET_CURRENT_CONTEXT(ctx);
3279 
3280    texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
3281                                                    texunit - GL_TEXTURE0,
3282                                                    true,
3283                                                    "glMultiTexImage2DEXT");
3284    if (!texObj)
3285       return;
3286    teximage(ctx, GL_FALSE, 2, texObj, target, level, internalFormat, width, height, 1,
3287                 border, format, type, 0, pixels, false);
3288 }
3289 
3290 /*
3291  * Called by the API or display list executor.
3292  * Note that width and height include the border.
3293  */
3294 void GLAPIENTRY
_mesa_TexImage3D(GLenum target,GLint level,GLint internalFormat,GLsizei width,GLsizei height,GLsizei depth,GLint border,GLenum format,GLenum type,const GLvoid * pixels)3295 _mesa_TexImage3D( GLenum target, GLint level, GLint internalFormat,
3296                   GLsizei width, GLsizei height, GLsizei depth,
3297                   GLint border, GLenum format, GLenum type,
3298                   const GLvoid *pixels )
3299 {
3300    GET_CURRENT_CONTEXT(ctx);
3301    teximage_err(ctx, GL_FALSE, 3, target, level, internalFormat,
3302                 width, height, depth, border, format, type, 0, pixels);
3303 }
3304 
3305 void GLAPIENTRY
_mesa_TextureImage3DEXT(GLuint texture,GLenum target,GLint level,GLint internalFormat,GLsizei width,GLsizei height,GLsizei depth,GLint border,GLenum format,GLenum type,const GLvoid * pixels)3306 _mesa_TextureImage3DEXT(GLuint texture, GLenum target, GLint level,
3307                       GLint internalFormat, GLsizei width, GLsizei height,
3308                       GLsizei depth, GLint border,
3309                       GLenum format, GLenum type, const GLvoid *pixels )
3310 {
3311    struct gl_texture_object*  texObj;
3312    GET_CURRENT_CONTEXT(ctx);
3313 
3314    texObj = _mesa_lookup_or_create_texture(ctx, target, texture, false, true,
3315                                            "glTextureImage3DEXT");
3316    if (!texObj)
3317       return;
3318    teximage(ctx, GL_FALSE, 3, texObj, target, level, internalFormat,
3319             width, height, depth, border, format, type, 0, pixels, false);
3320 }
3321 
3322 
3323 void GLAPIENTRY
_mesa_MultiTexImage3DEXT(GLenum texunit,GLenum target,GLint level,GLint internalFormat,GLsizei width,GLsizei height,GLsizei depth,GLint border,GLenum format,GLenum type,const GLvoid * pixels)3324 _mesa_MultiTexImage3DEXT(GLenum texunit, GLenum target, GLint level,
3325                          GLint internalFormat, GLsizei width, GLsizei height,
3326                          GLsizei depth, GLint border, GLenum format, GLenum type,
3327                          const GLvoid *pixels )
3328 {
3329    struct gl_texture_object*  texObj;
3330    GET_CURRENT_CONTEXT(ctx);
3331 
3332    texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
3333                                                    texunit - GL_TEXTURE0,
3334                                                    true,
3335                                                    "glMultiTexImage3DEXT");
3336    if (!texObj)
3337       return;
3338    teximage(ctx, GL_FALSE, 3, texObj, target, level, internalFormat,
3339                 width, height, depth, border, format, type, 0, pixels, false);
3340 }
3341 
3342 
3343 void GLAPIENTRY
_mesa_TexImage3DEXT(GLenum target,GLint level,GLenum internalFormat,GLsizei width,GLsizei height,GLsizei depth,GLint border,GLenum format,GLenum type,const GLvoid * pixels)3344 _mesa_TexImage3DEXT( GLenum target, GLint level, GLenum internalFormat,
3345                      GLsizei width, GLsizei height, GLsizei depth,
3346                      GLint border, GLenum format, GLenum type,
3347                      const GLvoid *pixels )
3348 {
3349    _mesa_TexImage3D(target, level, (GLint) internalFormat, width, height,
3350                     depth, border, format, type, pixels);
3351 }
3352 
3353 
3354 void GLAPIENTRY
_mesa_TexImage1D_no_error(GLenum target,GLint level,GLint internalFormat,GLsizei width,GLint border,GLenum format,GLenum type,const GLvoid * pixels)3355 _mesa_TexImage1D_no_error(GLenum target, GLint level, GLint internalFormat,
3356                           GLsizei width, GLint border, GLenum format,
3357                           GLenum type, const GLvoid *pixels)
3358 {
3359    GET_CURRENT_CONTEXT(ctx);
3360    teximage_no_error(ctx, GL_FALSE, 1, target, level, internalFormat, width, 1,
3361                      1, border, format, type, 0, pixels);
3362 }
3363 
3364 
3365 void GLAPIENTRY
_mesa_TexImage2D_no_error(GLenum target,GLint level,GLint internalFormat,GLsizei width,GLsizei height,GLint border,GLenum format,GLenum type,const GLvoid * pixels)3366 _mesa_TexImage2D_no_error(GLenum target, GLint level, GLint internalFormat,
3367                           GLsizei width, GLsizei height, GLint border,
3368                           GLenum format, GLenum type, const GLvoid *pixels)
3369 {
3370    GET_CURRENT_CONTEXT(ctx);
3371    teximage_no_error(ctx, GL_FALSE, 2, target, level, internalFormat, width,
3372                      height, 1, border, format, type, 0, pixels);
3373 }
3374 
3375 
3376 void GLAPIENTRY
_mesa_TexImage3D_no_error(GLenum target,GLint level,GLint internalFormat,GLsizei width,GLsizei height,GLsizei depth,GLint border,GLenum format,GLenum type,const GLvoid * pixels)3377 _mesa_TexImage3D_no_error(GLenum target, GLint level, GLint internalFormat,
3378                           GLsizei width, GLsizei height, GLsizei depth,
3379                           GLint border, GLenum format, GLenum type,
3380                           const GLvoid *pixels )
3381 {
3382    GET_CURRENT_CONTEXT(ctx);
3383    teximage_no_error(ctx, GL_FALSE, 3, target, level, internalFormat,
3384                      width, height, depth, border, format, type, 0, pixels);
3385 }
3386 
3387 /*
3388  * Helper used by __mesa_EGLImageTargetTexture2DOES and
3389  * _mesa_EGLImageTargetTexStorageEXT.
3390  */
3391 static void
egl_image_target_texture(struct gl_context * ctx,struct gl_texture_object * texObj,GLenum target,GLeglImageOES image,bool tex_storage,const char * caller)3392 egl_image_target_texture(struct gl_context *ctx,
3393                          struct gl_texture_object *texObj, GLenum target,
3394                          GLeglImageOES image, bool tex_storage,
3395                          const char *caller)
3396 {
3397    struct gl_texture_image *texImage;
3398    bool valid_target;
3399    FLUSH_VERTICES(ctx, 0);
3400 
3401    switch (target) {
3402    case GL_TEXTURE_2D:
3403       valid_target = _mesa_has_OES_EGL_image(ctx) ||
3404                      (tex_storage && _mesa_has_EXT_EGL_image_storage(ctx));
3405       break;
3406    case GL_TEXTURE_EXTERNAL_OES:
3407       valid_target =
3408          _mesa_is_gles(ctx) ? _mesa_has_OES_EGL_image_external(ctx) : false;
3409       break;
3410    default:
3411       valid_target = false;
3412       break;
3413    }
3414 
3415    if (!valid_target) {
3416       _mesa_error(ctx, GL_INVALID_ENUM, "%s(target=%d)", caller, target);
3417       return;
3418    }
3419 
3420    if (!image) {
3421       _mesa_error(ctx, GL_INVALID_VALUE, "%s(image=%p)", caller, image);
3422       return;
3423    }
3424 
3425    if (ctx->NewState & _NEW_PIXEL)
3426       _mesa_update_state(ctx);
3427 
3428    _mesa_lock_texture(ctx, texObj);
3429 
3430    if (texObj->Immutable) {
3431       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(texture is immutable)", caller);
3432       _mesa_unlock_texture(ctx, texObj);
3433       return;
3434    }
3435 
3436    texImage = _mesa_get_tex_image(ctx, texObj, target, 0);
3437    if (!texImage) {
3438       _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", caller);
3439    } else {
3440       ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
3441 
3442       if (tex_storage) {
3443          ctx->Driver.EGLImageTargetTexStorage(ctx, target, texObj, texImage,
3444                                               image);
3445       } else {
3446          ctx->Driver.EGLImageTargetTexture2D(ctx, target, texObj, texImage,
3447                                              image);
3448       }
3449 
3450       _mesa_dirty_texobj(ctx, texObj);
3451    }
3452 
3453    if (tex_storage)
3454       _mesa_set_texture_view_state(ctx, texObj, target, 1);
3455 
3456    _mesa_unlock_texture(ctx, texObj);
3457 }
3458 
3459 void GLAPIENTRY
_mesa_EGLImageTargetTexture2DOES(GLenum target,GLeglImageOES image)3460 _mesa_EGLImageTargetTexture2DOES(GLenum target, GLeglImageOES image)
3461 {
3462    struct gl_texture_object *texObj;
3463    const char *func = "glEGLImageTargetTexture2D";
3464    GET_CURRENT_CONTEXT(ctx);
3465 
3466    texObj = _mesa_get_current_tex_object(ctx, target);
3467    if (!texObj) {
3468       _mesa_error(ctx, GL_INVALID_ENUM, "%s(target=%d)", func, target);
3469       return;
3470    }
3471 
3472    egl_image_target_texture(ctx, texObj, target, image, false, func);
3473 }
3474 
3475 static void
egl_image_target_texture_storage(struct gl_context * ctx,struct gl_texture_object * texObj,GLenum target,GLeglImageOES image,const GLint * attrib_list,const char * caller)3476 egl_image_target_texture_storage(struct gl_context *ctx,
3477                                  struct gl_texture_object *texObj, GLenum target,
3478                                  GLeglImageOES image, const GLint *attrib_list,
3479                                  const char *caller)
3480 {
3481    /*
3482     * EXT_EGL_image_storage:
3483     *
3484     * "<attrib_list> must be NULL or a pointer to the value GL_NONE."
3485     */
3486    if (attrib_list && attrib_list[0] != GL_NONE) {
3487       _mesa_error(ctx, GL_INVALID_VALUE, "%s(image=%p)", caller, image);
3488       return;
3489    }
3490 
3491    switch (target) {
3492    case GL_TEXTURE_2D:
3493    case GL_TEXTURE_EXTERNAL_OES:
3494       break;
3495    default:
3496     /*
3497      * The EXT_EGL_image_storage spec allows for many other targets besides
3498      * GL_TEXTURE_2D and GL_TEXTURE_EXTERNAL_OES, however these are complicated
3499      * to implement.
3500      */
3501      _mesa_error(ctx, GL_INVALID_OPERATION, "%s(unsupported target=%d)",
3502                  caller, target);
3503      return;
3504    }
3505 
3506    egl_image_target_texture(ctx, texObj, target, image, true, caller);
3507 }
3508 
3509 
3510 void GLAPIENTRY
_mesa_EGLImageTargetTexStorageEXT(GLenum target,GLeglImageOES image,const GLint * attrib_list)3511 _mesa_EGLImageTargetTexStorageEXT(GLenum target, GLeglImageOES image,
3512                                   const GLint *attrib_list)
3513 {
3514    struct gl_texture_object *texObj;
3515    const char *func = "glEGLImageTargetTexStorageEXT";
3516    GET_CURRENT_CONTEXT(ctx);
3517 
3518    texObj = _mesa_get_current_tex_object(ctx, target);
3519    if (!texObj) {
3520       _mesa_error(ctx, GL_INVALID_ENUM, "%s(target=%d)", func, target);
3521       return;
3522    }
3523 
3524    egl_image_target_texture_storage(ctx, texObj, target, image, attrib_list,
3525                                     func);
3526 }
3527 
3528 void GLAPIENTRY
_mesa_EGLImageTargetTextureStorageEXT(GLuint texture,GLeglImageOES image,const GLint * attrib_list)3529 _mesa_EGLImageTargetTextureStorageEXT(GLuint texture, GLeglImageOES image,
3530                                       const GLint *attrib_list)
3531 {
3532    struct gl_texture_object *texObj;
3533    const char *func = "glEGLImageTargetTextureStorageEXT";
3534    GET_CURRENT_CONTEXT(ctx);
3535 
3536    if (!(_mesa_is_desktop_gl(ctx) && ctx->Version >= 45) &&
3537        !_mesa_has_ARB_direct_state_access(ctx) &&
3538        !_mesa_has_EXT_direct_state_access(ctx)) {
3539       _mesa_error(ctx, GL_INVALID_OPERATION, "direct access not supported");
3540       return;
3541    }
3542 
3543    texObj = _mesa_lookup_texture_err(ctx, texture, func);
3544    if (!texObj)
3545       return;
3546 
3547    egl_image_target_texture_storage(ctx, texObj, texObj->Target, image,
3548                                     attrib_list, func);
3549 }
3550 
3551 /**
3552  * Helper that implements the glTexSubImage1/2/3D()
3553  * and glTextureSubImage1/2/3D() functions.
3554  */
3555 static void
texture_sub_image(struct gl_context * ctx,GLuint dims,struct gl_texture_object * texObj,struct gl_texture_image * texImage,GLenum target,GLint level,GLint xoffset,GLint yoffset,GLint zoffset,GLsizei width,GLsizei height,GLsizei depth,GLenum format,GLenum type,const GLvoid * pixels)3556 texture_sub_image(struct gl_context *ctx, GLuint dims,
3557                   struct gl_texture_object *texObj,
3558                   struct gl_texture_image *texImage,
3559                   GLenum target, GLint level,
3560                   GLint xoffset, GLint yoffset, GLint zoffset,
3561                   GLsizei width, GLsizei height, GLsizei depth,
3562                   GLenum format, GLenum type, const GLvoid *pixels)
3563 {
3564    FLUSH_VERTICES(ctx, 0);
3565 
3566    if (ctx->NewState & _NEW_PIXEL)
3567       _mesa_update_state(ctx);
3568 
3569    _mesa_lock_texture(ctx, texObj);
3570    {
3571       if (width > 0 && height > 0 && depth > 0) {
3572          /* If we have a border, offset=-1 is legal.  Bias by border width. */
3573          switch (dims) {
3574          case 3:
3575             if (target != GL_TEXTURE_2D_ARRAY)
3576                zoffset += texImage->Border;
3577             /* fall-through */
3578          case 2:
3579             if (target != GL_TEXTURE_1D_ARRAY)
3580                yoffset += texImage->Border;
3581             /* fall-through */
3582          case 1:
3583             xoffset += texImage->Border;
3584          }
3585 
3586          ctx->Driver.TexSubImage(ctx, dims, texImage,
3587                                  xoffset, yoffset, zoffset,
3588                                  width, height, depth,
3589                                  format, type, pixels, &ctx->Unpack);
3590 
3591          check_gen_mipmap(ctx, target, texObj, level);
3592 
3593          /* NOTE: Don't signal _NEW_TEXTURE_OBJECT since we've only changed
3594           * the texel data, not the texture format, size, etc.
3595           */
3596       }
3597    }
3598    _mesa_unlock_texture(ctx, texObj);
3599 }
3600 
3601 /**
3602  * Implement all the glTexSubImage1/2/3D() functions.
3603  * Must split this out this way because of GL_TEXTURE_CUBE_MAP.
3604  */
3605 static void
texsubimage_err(struct gl_context * ctx,GLuint dims,GLenum target,GLint level,GLint xoffset,GLint yoffset,GLint zoffset,GLsizei width,GLsizei height,GLsizei depth,GLenum format,GLenum type,const GLvoid * pixels,const char * callerName)3606 texsubimage_err(struct gl_context *ctx, GLuint dims, GLenum target, GLint level,
3607                 GLint xoffset, GLint yoffset, GLint zoffset,
3608                 GLsizei width, GLsizei height, GLsizei depth,
3609                 GLenum format, GLenum type, const GLvoid *pixels,
3610                 const char *callerName)
3611 {
3612    struct gl_texture_object *texObj;
3613    struct gl_texture_image *texImage;
3614 
3615    /* check target (proxies not allowed) */
3616    if (!legal_texsubimage_target(ctx, dims, target, false)) {
3617       _mesa_error(ctx, GL_INVALID_ENUM, "glTexSubImage%uD(target=%s)",
3618                   dims, _mesa_enum_to_string(target));
3619       return;
3620    }
3621 
3622    texObj = _mesa_get_current_tex_object(ctx, target);
3623    if (!texObj)
3624       return;
3625 
3626    if (texsubimage_error_check(ctx, dims, texObj, target, level,
3627                                xoffset, yoffset, zoffset,
3628                                width, height, depth, format, type,
3629                                pixels, callerName)) {
3630       return;   /* error was detected */
3631    }
3632 
3633    texImage = _mesa_select_tex_image(texObj, target, level);
3634    /* texsubimage_error_check ensures that texImage is not NULL */
3635 
3636    if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3637       _mesa_debug(ctx, "glTexSubImage%uD %s %d %d %d %d %d %d %d %s %s %p\n",
3638                   dims,
3639                   _mesa_enum_to_string(target), level,
3640                   xoffset, yoffset, zoffset, width, height, depth,
3641                   _mesa_enum_to_string(format),
3642                   _mesa_enum_to_string(type), pixels);
3643 
3644    texture_sub_image(ctx, dims, texObj, texImage, target, level,
3645                      xoffset, yoffset, zoffset, width, height, depth,
3646                      format, type, pixels);
3647 }
3648 
3649 
3650 static void
texsubimage(struct gl_context * ctx,GLuint dims,GLenum target,GLint level,GLint xoffset,GLint yoffset,GLint zoffset,GLsizei width,GLsizei height,GLsizei depth,GLenum format,GLenum type,const GLvoid * pixels)3651 texsubimage(struct gl_context *ctx, GLuint dims, GLenum target, GLint level,
3652             GLint xoffset, GLint yoffset, GLint zoffset,
3653             GLsizei width, GLsizei height, GLsizei depth,
3654             GLenum format, GLenum type, const GLvoid *pixels)
3655 {
3656    struct gl_texture_object *texObj;
3657    struct gl_texture_image *texImage;
3658 
3659    texObj = _mesa_get_current_tex_object(ctx, target);
3660    texImage = _mesa_select_tex_image(texObj, target, level);
3661 
3662    texture_sub_image(ctx, dims, texObj, texImage, target, level,
3663                      xoffset, yoffset, zoffset, width, height, depth,
3664                      format, type, pixels);
3665 }
3666 
3667 
3668 /**
3669  * Implement all the glTextureSubImage1/2/3D() functions.
3670  * Must split this out this way because of GL_TEXTURE_CUBE_MAP.
3671  */
3672 static ALWAYS_INLINE void
texturesubimage(struct gl_context * ctx,GLuint dims,GLuint texture,GLenum target,GLint level,GLint xoffset,GLint yoffset,GLint zoffset,GLsizei width,GLsizei height,GLsizei depth,GLenum format,GLenum type,const GLvoid * pixels,const char * callerName,bool no_error,bool ext_dsa)3673 texturesubimage(struct gl_context *ctx, GLuint dims,
3674                 GLuint texture, GLenum target, GLint level,
3675                 GLint xoffset, GLint yoffset, GLint zoffset,
3676                 GLsizei width, GLsizei height, GLsizei depth,
3677                 GLenum format, GLenum type, const GLvoid *pixels,
3678                 const char *callerName, bool no_error, bool ext_dsa)
3679 {
3680    struct gl_texture_object *texObj;
3681    struct gl_texture_image *texImage;
3682    int i;
3683 
3684    if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3685       _mesa_debug(ctx,
3686                   "glTextureSubImage%uD %d %d %d %d %d %d %d %d %s %s %p\n",
3687                   dims, texture, level,
3688                   xoffset, yoffset, zoffset, width, height, depth,
3689                   _mesa_enum_to_string(format),
3690                   _mesa_enum_to_string(type), pixels);
3691 
3692    /* Get the texture object by Name. */
3693    if (!no_error) {
3694       if (!ext_dsa) {
3695          texObj = _mesa_lookup_texture_err(ctx, texture, callerName);
3696       } else {
3697          texObj = lookup_texture_ext_dsa(ctx, target, texture, callerName);
3698       }
3699       if (!texObj)
3700          return;
3701    } else {
3702       texObj = _mesa_lookup_texture(ctx, texture);
3703    }
3704 
3705    if (!no_error) {
3706       /* check target (proxies not allowed) */
3707       if (!legal_texsubimage_target(ctx, dims, texObj->Target, true)) {
3708          _mesa_error(ctx, GL_INVALID_OPERATION, "%s(target=%s)",
3709                      callerName, _mesa_enum_to_string(texObj->Target));
3710          return;
3711       }
3712 
3713       if (texsubimage_error_check(ctx, dims, texObj, texObj->Target, level,
3714                                   xoffset, yoffset, zoffset,
3715                                   width, height, depth, format, type,
3716                                   pixels, callerName)) {
3717          return;   /* error was detected */
3718       }
3719    }
3720 
3721    /* Must handle special case GL_TEXTURE_CUBE_MAP. */
3722    if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
3723       GLint imageStride;
3724 
3725       /*
3726        * What do we do if the user created a texture with the following code
3727        * and then called this function with its handle?
3728        *
3729        *    GLuint tex;
3730        *    glCreateTextures(GL_TEXTURE_CUBE_MAP, 1, &tex);
3731        *    glBindTexture(GL_TEXTURE_CUBE_MAP, tex);
3732        *    glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, ...);
3733        *    glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, ...);
3734        *    glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, ...);
3735        *    // Note: GL_TEXTURE_CUBE_MAP_NEGATIVE_Y not set, or given the
3736        *    // wrong format, or given the wrong size, etc.
3737        *    glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, ...);
3738        *    glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, ...);
3739        *
3740        * A bug has been filed against the spec for this case.  In the
3741        * meantime, we will check for cube completeness.
3742        *
3743        * According to Section 8.17 Texture Completeness in the OpenGL 4.5
3744        * Core Profile spec (30.10.2014):
3745        *    "[A] cube map texture is cube complete if the
3746        *    following conditions all hold true: The [base level] texture
3747        *    images of each of the six cube map faces have identical, positive,
3748        *    and square dimensions. The [base level] images were each specified
3749        *    with the same internal format."
3750        *
3751        * It seems reasonable to check for cube completeness of an arbitrary
3752        * level here so that the image data has a consistent format and size.
3753        */
3754       if (!no_error && !_mesa_cube_level_complete(texObj, level)) {
3755          _mesa_error(ctx, GL_INVALID_OPERATION,
3756                      "glTextureSubImage%uD(cube map incomplete)",
3757                      dims);
3758          return;
3759       }
3760 
3761       imageStride = _mesa_image_image_stride(&ctx->Unpack, width, height,
3762                                              format, type);
3763       /* Copy in each face. */
3764       for (i = zoffset; i < zoffset + depth; ++i) {
3765          texImage = texObj->Image[i][level];
3766          assert(texImage);
3767 
3768          texture_sub_image(ctx, 3, texObj, texImage, texObj->Target,
3769                            level, xoffset, yoffset, 0,
3770                            width, height, 1, format,
3771                            type, pixels);
3772          pixels = (GLubyte *) pixels + imageStride;
3773       }
3774    }
3775    else {
3776       texImage = _mesa_select_tex_image(texObj, texObj->Target, level);
3777       assert(texImage);
3778 
3779       texture_sub_image(ctx, dims, texObj, texImage, texObj->Target,
3780                         level, xoffset, yoffset, zoffset,
3781                         width, height, depth, format,
3782                         type, pixels);
3783    }
3784 }
3785 
3786 
3787 static void
texturesubimage_error(struct gl_context * ctx,GLuint dims,GLuint texture,GLenum target,GLint level,GLint xoffset,GLint yoffset,GLint zoffset,GLsizei width,GLsizei height,GLsizei depth,GLenum format,GLenum type,const GLvoid * pixels,const char * callerName,bool ext_dsa)3788 texturesubimage_error(struct gl_context *ctx, GLuint dims,
3789                       GLuint texture, GLenum target, GLint level,
3790                       GLint xoffset, GLint yoffset, GLint zoffset,
3791                       GLsizei width, GLsizei height, GLsizei depth,
3792                       GLenum format, GLenum type, const GLvoid *pixels,
3793                       const char *callerName, bool ext_dsa)
3794 {
3795    texturesubimage(ctx, dims, texture, target, level, xoffset, yoffset,
3796                    zoffset, width, height, depth, format, type, pixels,
3797                    callerName, false, ext_dsa);
3798 }
3799 
3800 
3801 static void
texturesubimage_no_error(struct gl_context * ctx,GLuint dims,GLuint texture,GLenum target,GLint level,GLint xoffset,GLint yoffset,GLint zoffset,GLsizei width,GLsizei height,GLsizei depth,GLenum format,GLenum type,const GLvoid * pixels,const char * callerName,bool ext_dsa)3802 texturesubimage_no_error(struct gl_context *ctx, GLuint dims,
3803                          GLuint texture, GLenum target, GLint level,
3804                          GLint xoffset, GLint yoffset, GLint zoffset,
3805                          GLsizei width, GLsizei height, GLsizei depth,
3806                          GLenum format, GLenum type, const GLvoid *pixels,
3807                          const char *callerName, bool ext_dsa)
3808 {
3809    texturesubimage(ctx, dims, texture, target, level, xoffset, yoffset,
3810                    zoffset, width, height, depth, format, type, pixels,
3811                    callerName, true, ext_dsa);
3812 }
3813 
3814 
3815 void GLAPIENTRY
_mesa_TexSubImage1D_no_error(GLenum target,GLint level,GLint xoffset,GLsizei width,GLenum format,GLenum type,const GLvoid * pixels)3816 _mesa_TexSubImage1D_no_error(GLenum target, GLint level,
3817                              GLint xoffset, GLsizei width,
3818                              GLenum format, GLenum type,
3819                              const GLvoid *pixels)
3820 {
3821    GET_CURRENT_CONTEXT(ctx);
3822    texsubimage(ctx, 1, target, level,
3823                xoffset, 0, 0,
3824                width, 1, 1,
3825                format, type, pixels);
3826 }
3827 
3828 
3829 void GLAPIENTRY
_mesa_TexSubImage1D(GLenum target,GLint level,GLint xoffset,GLsizei width,GLenum format,GLenum type,const GLvoid * pixels)3830 _mesa_TexSubImage1D( GLenum target, GLint level,
3831                      GLint xoffset, GLsizei width,
3832                      GLenum format, GLenum type,
3833                      const GLvoid *pixels )
3834 {
3835    GET_CURRENT_CONTEXT(ctx);
3836    texsubimage_err(ctx, 1, target, level,
3837                    xoffset, 0, 0,
3838                    width, 1, 1,
3839                    format, type, pixels, "glTexSubImage1D");
3840 }
3841 
3842 
3843 void GLAPIENTRY
_mesa_TexSubImage2D_no_error(GLenum target,GLint level,GLint xoffset,GLint yoffset,GLsizei width,GLsizei height,GLenum format,GLenum type,const GLvoid * pixels)3844 _mesa_TexSubImage2D_no_error(GLenum target, GLint level,
3845                              GLint xoffset, GLint yoffset,
3846                              GLsizei width, GLsizei height,
3847                              GLenum format, GLenum type,
3848                              const GLvoid *pixels)
3849 {
3850    GET_CURRENT_CONTEXT(ctx);
3851    texsubimage(ctx, 2, target, level,
3852                xoffset, yoffset, 0,
3853                width, height, 1,
3854                format, type, pixels);
3855 }
3856 
3857 
3858 void GLAPIENTRY
_mesa_TexSubImage2D(GLenum target,GLint level,GLint xoffset,GLint yoffset,GLsizei width,GLsizei height,GLenum format,GLenum type,const GLvoid * pixels)3859 _mesa_TexSubImage2D( GLenum target, GLint level,
3860                      GLint xoffset, GLint yoffset,
3861                      GLsizei width, GLsizei height,
3862                      GLenum format, GLenum type,
3863                      const GLvoid *pixels )
3864 {
3865    GET_CURRENT_CONTEXT(ctx);
3866    texsubimage_err(ctx, 2, target, level,
3867                    xoffset, yoffset, 0,
3868                    width, height, 1,
3869                    format, type, pixels, "glTexSubImage2D");
3870 }
3871 
3872 
3873 void GLAPIENTRY
_mesa_TexSubImage3D_no_error(GLenum target,GLint level,GLint xoffset,GLint yoffset,GLint zoffset,GLsizei width,GLsizei height,GLsizei depth,GLenum format,GLenum type,const GLvoid * pixels)3874 _mesa_TexSubImage3D_no_error(GLenum target, GLint level,
3875                              GLint xoffset, GLint yoffset, GLint zoffset,
3876                              GLsizei width, GLsizei height, GLsizei depth,
3877                              GLenum format, GLenum type,
3878                              const GLvoid *pixels)
3879 {
3880    GET_CURRENT_CONTEXT(ctx);
3881    texsubimage(ctx, 3, target, level,
3882                xoffset, yoffset, zoffset,
3883                width, height, depth,
3884                format, type, pixels);
3885 }
3886 
3887 
3888 void GLAPIENTRY
_mesa_TexSubImage3D(GLenum target,GLint level,GLint xoffset,GLint yoffset,GLint zoffset,GLsizei width,GLsizei height,GLsizei depth,GLenum format,GLenum type,const GLvoid * pixels)3889 _mesa_TexSubImage3D( GLenum target, GLint level,
3890                      GLint xoffset, GLint yoffset, GLint zoffset,
3891                      GLsizei width, GLsizei height, GLsizei depth,
3892                      GLenum format, GLenum type,
3893                      const GLvoid *pixels )
3894 {
3895    GET_CURRENT_CONTEXT(ctx);
3896    texsubimage_err(ctx, 3, target, level,
3897                    xoffset, yoffset, zoffset,
3898                    width, height, depth,
3899                    format, type, pixels, "glTexSubImage3D");
3900 }
3901 
3902 
3903 void GLAPIENTRY
_mesa_TextureSubImage1D_no_error(GLuint texture,GLint level,GLint xoffset,GLsizei width,GLenum format,GLenum type,const GLvoid * pixels)3904 _mesa_TextureSubImage1D_no_error(GLuint texture, GLint level, GLint xoffset,
3905                                  GLsizei width, GLenum format, GLenum type,
3906                                  const GLvoid *pixels)
3907 {
3908    GET_CURRENT_CONTEXT(ctx);
3909    texturesubimage_no_error(ctx, 1, texture, 0, level, xoffset, 0, 0, width,
3910                             1, 1, format, type, pixels, "glTextureSubImage1D",
3911                             false);
3912 }
3913 
3914 
3915 void GLAPIENTRY
_mesa_TextureSubImage1DEXT(GLuint texture,GLenum target,GLint level,GLint xoffset,GLsizei width,GLenum format,GLenum type,const GLvoid * pixels)3916 _mesa_TextureSubImage1DEXT(GLuint texture, GLenum target, GLint level,
3917                         GLint xoffset, GLsizei width,
3918                         GLenum format, GLenum type,
3919                         const GLvoid *pixels)
3920 {
3921    GET_CURRENT_CONTEXT(ctx);
3922    texturesubimage_error(ctx, 1, texture, target, level, xoffset, 0, 0, width, 1,
3923                          1, format, type, pixels, "glTextureSubImage1DEXT",
3924                          false);
3925 }
3926 
3927 
3928 void GLAPIENTRY
_mesa_MultiTexSubImage1DEXT(GLenum texunit,GLenum target,GLint level,GLint xoffset,GLsizei width,GLenum format,GLenum type,const GLvoid * pixels)3929 _mesa_MultiTexSubImage1DEXT(GLenum texunit, GLenum target, GLint level,
3930                             GLint xoffset, GLsizei width,
3931                             GLenum format, GLenum type,
3932                             const GLvoid *pixels)
3933 {
3934    GET_CURRENT_CONTEXT(ctx);
3935    struct gl_texture_object *texObj;
3936    struct gl_texture_image *texImage;
3937 
3938    texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
3939                                                    texunit - GL_TEXTURE0,
3940                                                    false,
3941                                                    "glMultiTexImage1DEXT");
3942    texImage = _mesa_select_tex_image(texObj, target, level);
3943 
3944    texture_sub_image(ctx, 1, texObj, texImage, target, level,
3945                      xoffset, 0, 0, width, 1, 1,
3946                      format, type, pixels);
3947 }
3948 
3949 
3950 void GLAPIENTRY
_mesa_TextureSubImage1D(GLuint texture,GLint level,GLint xoffset,GLsizei width,GLenum format,GLenum type,const GLvoid * pixels)3951 _mesa_TextureSubImage1D(GLuint texture, GLint level,
3952                         GLint xoffset, GLsizei width,
3953                         GLenum format, GLenum type,
3954                         const GLvoid *pixels)
3955 {
3956    GET_CURRENT_CONTEXT(ctx);
3957    texturesubimage_error(ctx, 1, texture, 0, level, xoffset, 0, 0, width, 1,
3958                          1, format, type, pixels, "glTextureSubImage1D",
3959                          false);
3960 }
3961 
3962 
3963 void GLAPIENTRY
_mesa_TextureSubImage2D_no_error(GLuint texture,GLint level,GLint xoffset,GLint yoffset,GLsizei width,GLsizei height,GLenum format,GLenum type,const GLvoid * pixels)3964 _mesa_TextureSubImage2D_no_error(GLuint texture, GLint level, GLint xoffset,
3965                                  GLint yoffset, GLsizei width, GLsizei height,
3966                                  GLenum format, GLenum type,
3967                                  const GLvoid *pixels)
3968 {
3969    GET_CURRENT_CONTEXT(ctx);
3970    texturesubimage_no_error(ctx, 2, texture, 0, level, xoffset, yoffset, 0,
3971                             width, height, 1, format, type, pixels,
3972                             "glTextureSubImage2D", false);
3973 }
3974 
3975 
3976 void GLAPIENTRY
_mesa_TextureSubImage2DEXT(GLuint texture,GLenum target,GLint level,GLint xoffset,GLint yoffset,GLsizei width,GLsizei height,GLenum format,GLenum type,const GLvoid * pixels)3977 _mesa_TextureSubImage2DEXT(GLuint texture, GLenum target, GLint level,
3978                            GLint xoffset, GLint yoffset, GLsizei width,
3979                            GLsizei height, GLenum format, GLenum type,
3980                            const GLvoid *pixels)
3981 {
3982    GET_CURRENT_CONTEXT(ctx);
3983    texturesubimage_error(ctx, 2, texture, target, level, xoffset, yoffset, 0,
3984                          width, height, 1, format, type, pixels,
3985                          "glTextureSubImage2DEXT", true);
3986 }
3987 
3988 
3989 void GLAPIENTRY
_mesa_MultiTexSubImage2DEXT(GLenum texunit,GLenum target,GLint level,GLint xoffset,GLint yoffset,GLsizei width,GLsizei height,GLenum format,GLenum type,const GLvoid * pixels)3990 _mesa_MultiTexSubImage2DEXT(GLenum texunit, GLenum target, GLint level,
3991                             GLint xoffset, GLint yoffset, GLsizei width,
3992                             GLsizei height, GLenum format, GLenum type,
3993                             const GLvoid *pixels)
3994 {
3995    GET_CURRENT_CONTEXT(ctx);
3996    struct gl_texture_object *texObj;
3997    struct gl_texture_image *texImage;
3998 
3999    texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
4000                                                    texunit - GL_TEXTURE0,
4001                                                    false,
4002                                                    "glMultiTexImage2DEXT");
4003    texImage = _mesa_select_tex_image(texObj, target, level);
4004 
4005    texture_sub_image(ctx, 2, texObj, texImage, target, level,
4006                      xoffset, yoffset, 0, width, height, 1,
4007                      format, type, pixels);
4008 }
4009 
4010 
4011 void GLAPIENTRY
_mesa_TextureSubImage2D(GLuint texture,GLint level,GLint xoffset,GLint yoffset,GLsizei width,GLsizei height,GLenum format,GLenum type,const GLvoid * pixels)4012 _mesa_TextureSubImage2D(GLuint texture, GLint level,
4013                         GLint xoffset, GLint yoffset,
4014                         GLsizei width, GLsizei height,
4015                         GLenum format, GLenum type,
4016                         const GLvoid *pixels)
4017 {
4018    GET_CURRENT_CONTEXT(ctx);
4019    texturesubimage_error(ctx, 2, texture, 0, level, xoffset, yoffset, 0,
4020                          width, height, 1, format, type, pixels,
4021                          "glTextureSubImage2D", false);
4022 }
4023 
4024 
4025 void GLAPIENTRY
_mesa_TextureSubImage3D_no_error(GLuint texture,GLint level,GLint xoffset,GLint yoffset,GLint zoffset,GLsizei width,GLsizei height,GLsizei depth,GLenum format,GLenum type,const GLvoid * pixels)4026 _mesa_TextureSubImage3D_no_error(GLuint texture, GLint level, GLint xoffset,
4027                                  GLint yoffset, GLint zoffset, GLsizei width,
4028                                  GLsizei height, GLsizei depth, GLenum format,
4029                                  GLenum type, const GLvoid *pixels)
4030 {
4031    GET_CURRENT_CONTEXT(ctx);
4032    texturesubimage_no_error(ctx, 3, texture, 0, level, xoffset, yoffset,
4033                             zoffset, width, height, depth, format, type,
4034                             pixels, "glTextureSubImage3D", false);
4035 }
4036 
4037 
4038 void GLAPIENTRY
_mesa_TextureSubImage3DEXT(GLuint texture,GLenum target,GLint level,GLint xoffset,GLint yoffset,GLint zoffset,GLsizei width,GLsizei height,GLsizei depth,GLenum format,GLenum type,const GLvoid * pixels)4039 _mesa_TextureSubImage3DEXT(GLuint texture, GLenum target, GLint level,
4040                            GLint xoffset, GLint yoffset, GLint zoffset,
4041                            GLsizei width, GLsizei height, GLsizei depth,
4042                            GLenum format, GLenum type, const GLvoid *pixels)
4043 {
4044    GET_CURRENT_CONTEXT(ctx);
4045    texturesubimage_error(ctx, 3, texture, target, level, xoffset, yoffset,
4046                          zoffset, width, height, depth, format, type,
4047                          pixels, "glTextureSubImage3DEXT", true);
4048 }
4049 
4050 
4051 void GLAPIENTRY
_mesa_MultiTexSubImage3DEXT(GLenum texunit,GLenum target,GLint level,GLint xoffset,GLint yoffset,GLint zoffset,GLsizei width,GLsizei height,GLsizei depth,GLenum format,GLenum type,const GLvoid * pixels)4052 _mesa_MultiTexSubImage3DEXT(GLenum texunit, GLenum target, GLint level,
4053                            GLint xoffset, GLint yoffset, GLint zoffset,
4054                            GLsizei width, GLsizei height, GLsizei depth,
4055                            GLenum format, GLenum type, const GLvoid *pixels)
4056 {
4057    GET_CURRENT_CONTEXT(ctx);
4058    struct gl_texture_object *texObj;
4059    struct gl_texture_image *texImage;
4060 
4061    texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
4062                                                    texunit - GL_TEXTURE0,
4063                                                    false,
4064                                                    "glMultiTexImage3DEXT");
4065    texImage = _mesa_select_tex_image(texObj, target, level);
4066 
4067    texture_sub_image(ctx, 3, texObj, texImage, target, level,
4068                      xoffset, yoffset, zoffset, width, height, depth,
4069                      format, type, pixels);
4070 }
4071 
4072 
4073 void GLAPIENTRY
_mesa_TextureSubImage3D(GLuint texture,GLint level,GLint xoffset,GLint yoffset,GLint zoffset,GLsizei width,GLsizei height,GLsizei depth,GLenum format,GLenum type,const GLvoid * pixels)4074 _mesa_TextureSubImage3D(GLuint texture, GLint level,
4075                         GLint xoffset, GLint yoffset, GLint zoffset,
4076                         GLsizei width, GLsizei height, GLsizei depth,
4077                         GLenum format, GLenum type,
4078                         const GLvoid *pixels)
4079 {
4080    GET_CURRENT_CONTEXT(ctx);
4081    texturesubimage_error(ctx, 3, texture, 0, level, xoffset, yoffset, zoffset,
4082                          width, height, depth, format, type, pixels,
4083                          "glTextureSubImage3D", false);
4084 }
4085 
4086 
4087 /**
4088  * For glCopyTexSubImage, return the source renderbuffer to copy texel data
4089  * from.  This depends on whether the texture contains color or depth values.
4090  */
4091 static struct gl_renderbuffer *
get_copy_tex_image_source(struct gl_context * ctx,mesa_format texFormat)4092 get_copy_tex_image_source(struct gl_context *ctx, mesa_format texFormat)
4093 {
4094    if (_mesa_get_format_bits(texFormat, GL_DEPTH_BITS) > 0) {
4095       /* reading from depth/stencil buffer */
4096       return ctx->ReadBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
4097    } else if (_mesa_get_format_bits(texFormat, GL_STENCIL_BITS) > 0) {
4098       return ctx->ReadBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
4099    } else {
4100       /* copying from color buffer */
4101       return ctx->ReadBuffer->_ColorReadBuffer;
4102    }
4103 }
4104 
4105 
4106 static void
copytexsubimage_by_slice(struct gl_context * ctx,struct gl_texture_image * texImage,GLuint dims,GLint xoffset,GLint yoffset,GLint zoffset,struct gl_renderbuffer * rb,GLint x,GLint y,GLsizei width,GLsizei height)4107 copytexsubimage_by_slice(struct gl_context *ctx,
4108                          struct gl_texture_image *texImage,
4109                          GLuint dims,
4110                          GLint xoffset, GLint yoffset, GLint zoffset,
4111                          struct gl_renderbuffer *rb,
4112                          GLint x, GLint y,
4113                          GLsizei width, GLsizei height)
4114 {
4115    if (texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
4116       int slice;
4117 
4118       /* For 1D arrays, we copy each scanline of the source rectangle into the
4119        * next array slice.
4120        */
4121       assert(zoffset == 0);
4122 
4123       for (slice = 0; slice < height; slice++) {
4124          assert(yoffset + slice < texImage->Height);
4125          ctx->Driver.CopyTexSubImage(ctx, 2, texImage,
4126                                      xoffset, 0, yoffset + slice,
4127                                      rb, x, y + slice, width, 1);
4128       }
4129    } else {
4130       ctx->Driver.CopyTexSubImage(ctx, dims, texImage,
4131                                   xoffset, yoffset, zoffset,
4132                                   rb, x, y, width, height);
4133    }
4134 }
4135 
4136 
4137 static GLboolean
formats_differ_in_component_sizes(mesa_format f1,mesa_format f2)4138 formats_differ_in_component_sizes(mesa_format f1, mesa_format f2)
4139 {
4140    GLint f1_r_bits = _mesa_get_format_bits(f1, GL_RED_BITS);
4141    GLint f1_g_bits = _mesa_get_format_bits(f1, GL_GREEN_BITS);
4142    GLint f1_b_bits = _mesa_get_format_bits(f1, GL_BLUE_BITS);
4143    GLint f1_a_bits = _mesa_get_format_bits(f1, GL_ALPHA_BITS);
4144 
4145    GLint f2_r_bits = _mesa_get_format_bits(f2, GL_RED_BITS);
4146    GLint f2_g_bits = _mesa_get_format_bits(f2, GL_GREEN_BITS);
4147    GLint f2_b_bits = _mesa_get_format_bits(f2, GL_BLUE_BITS);
4148    GLint f2_a_bits = _mesa_get_format_bits(f2, GL_ALPHA_BITS);
4149 
4150    if ((f1_r_bits && f2_r_bits && f1_r_bits != f2_r_bits)
4151        || (f1_g_bits && f2_g_bits && f1_g_bits != f2_g_bits)
4152        || (f1_b_bits && f2_b_bits && f1_b_bits != f2_b_bits)
4153        || (f1_a_bits && f2_a_bits && f1_a_bits != f2_a_bits))
4154       return GL_TRUE;
4155 
4156    return GL_FALSE;
4157 }
4158 
4159 
4160 /**
4161  * Check if the given texture format and size arguments match those
4162  * of the texture image.
4163  * \param return true if arguments match, false otherwise.
4164  */
4165 static bool
can_avoid_reallocation(const struct gl_texture_image * texImage,GLenum internalFormat,mesa_format texFormat,GLsizei width,GLsizei height,GLint border)4166 can_avoid_reallocation(const struct gl_texture_image *texImage,
4167                        GLenum internalFormat,
4168                        mesa_format texFormat, GLsizei width,
4169                        GLsizei height, GLint border)
4170 {
4171    if (texImage->InternalFormat != internalFormat)
4172       return false;
4173    if (texImage->TexFormat != texFormat)
4174       return false;
4175    if (texImage->Border != border)
4176       return false;
4177    if (texImage->Width2 != width)
4178       return false;
4179    if (texImage->Height2 != height)
4180       return false;
4181    return true;
4182 }
4183 
4184 
4185 /**
4186  * Implementation for glCopyTex(ture)SubImage1/2/3D() functions.
4187  */
4188 static void
copy_texture_sub_image(struct gl_context * ctx,GLuint dims,struct gl_texture_object * texObj,GLenum target,GLint level,GLint xoffset,GLint yoffset,GLint zoffset,GLint x,GLint y,GLsizei width,GLsizei height)4189 copy_texture_sub_image(struct gl_context *ctx, GLuint dims,
4190                        struct gl_texture_object *texObj,
4191                        GLenum target, GLint level,
4192                        GLint xoffset, GLint yoffset, GLint zoffset,
4193                        GLint x, GLint y, GLsizei width, GLsizei height)
4194 {
4195    struct gl_texture_image *texImage;
4196 
4197    _mesa_lock_texture(ctx, texObj);
4198 
4199    texImage = _mesa_select_tex_image(texObj, target, level);
4200 
4201    /* If we have a border, offset=-1 is legal.  Bias by border width. */
4202    switch (dims) {
4203    case 3:
4204       if (target != GL_TEXTURE_2D_ARRAY)
4205          zoffset += texImage->Border;
4206       /* fall-through */
4207    case 2:
4208       if (target != GL_TEXTURE_1D_ARRAY)
4209          yoffset += texImage->Border;
4210       /* fall-through */
4211    case 1:
4212       xoffset += texImage->Border;
4213    }
4214 
4215    if (ctx->Const.NoClippingOnCopyTex ||
4216        _mesa_clip_copytexsubimage(ctx, &xoffset, &yoffset, &x, &y,
4217                                   &width, &height)) {
4218       struct gl_renderbuffer *srcRb =
4219          get_copy_tex_image_source(ctx, texImage->TexFormat);
4220 
4221       copytexsubimage_by_slice(ctx, texImage, dims, xoffset, yoffset, zoffset,
4222                                srcRb, x, y, width, height);
4223 
4224       check_gen_mipmap(ctx, target, texObj, level);
4225 
4226       /* NOTE: Don't signal _NEW_TEXTURE_OBJECT since we've only changed
4227        * the texel data, not the texture format, size, etc.
4228        */
4229    }
4230 
4231    _mesa_unlock_texture(ctx, texObj);
4232 }
4233 
4234 
4235 static void
copy_texture_sub_image_err(struct gl_context * ctx,GLuint dims,struct gl_texture_object * texObj,GLenum target,GLint level,GLint xoffset,GLint yoffset,GLint zoffset,GLint x,GLint y,GLsizei width,GLsizei height,const char * caller)4236 copy_texture_sub_image_err(struct gl_context *ctx, GLuint dims,
4237                            struct gl_texture_object *texObj,
4238                            GLenum target, GLint level,
4239                            GLint xoffset, GLint yoffset, GLint zoffset,
4240                            GLint x, GLint y, GLsizei width, GLsizei height,
4241                            const char *caller)
4242 {
4243    FLUSH_VERTICES(ctx, 0);
4244 
4245    if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
4246       _mesa_debug(ctx, "%s %s %d %d %d %d %d %d %d %d\n", caller,
4247                   _mesa_enum_to_string(target),
4248                   level, xoffset, yoffset, zoffset, x, y, width, height);
4249 
4250    if (ctx->NewState & NEW_COPY_TEX_STATE)
4251       _mesa_update_state(ctx);
4252 
4253    if (copytexsubimage_error_check(ctx, dims, texObj, target, level,
4254                                    xoffset, yoffset, zoffset,
4255                                    width, height, caller)) {
4256       return;
4257    }
4258 
4259    copy_texture_sub_image(ctx, dims, texObj, target, level, xoffset, yoffset,
4260                           zoffset, x, y, width, height);
4261 }
4262 
4263 
4264 static void
copy_texture_sub_image_no_error(struct gl_context * ctx,GLuint dims,struct gl_texture_object * texObj,GLenum target,GLint level,GLint xoffset,GLint yoffset,GLint zoffset,GLint x,GLint y,GLsizei width,GLsizei height)4265 copy_texture_sub_image_no_error(struct gl_context *ctx, GLuint dims,
4266                                 struct gl_texture_object *texObj,
4267                                 GLenum target, GLint level,
4268                                 GLint xoffset, GLint yoffset, GLint zoffset,
4269                                 GLint x, GLint y, GLsizei width, GLsizei height)
4270 {
4271    FLUSH_VERTICES(ctx, 0);
4272 
4273    if (ctx->NewState & NEW_COPY_TEX_STATE)
4274       _mesa_update_state(ctx);
4275 
4276    copy_texture_sub_image(ctx, dims, texObj, target, level, xoffset, yoffset,
4277                           zoffset, x, y, width, height);
4278 }
4279 
4280 
4281 /**
4282  * Implement the glCopyTexImage1/2D() functions.
4283  */
4284 static ALWAYS_INLINE void
copyteximage(struct gl_context * ctx,GLuint dims,struct gl_texture_object * texObj,GLenum target,GLint level,GLenum internalFormat,GLint x,GLint y,GLsizei width,GLsizei height,GLint border,bool no_error)4285 copyteximage(struct gl_context *ctx, GLuint dims, struct gl_texture_object *texObj,
4286              GLenum target, GLint level, GLenum internalFormat,
4287              GLint x, GLint y, GLsizei width, GLsizei height, GLint border,
4288              bool no_error)
4289 {
4290    struct gl_texture_image *texImage;
4291    mesa_format texFormat;
4292 
4293    FLUSH_VERTICES(ctx, 0);
4294 
4295    if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
4296       _mesa_debug(ctx, "glCopyTexImage%uD %s %d %s %d %d %d %d %d\n",
4297                   dims,
4298                   _mesa_enum_to_string(target), level,
4299                   _mesa_enum_to_string(internalFormat),
4300                   x, y, width, height, border);
4301 
4302    if (ctx->NewState & NEW_COPY_TEX_STATE)
4303       _mesa_update_state(ctx);
4304 
4305    if (!no_error) {
4306       if (copytexture_error_check(ctx, dims, target, texObj, level,
4307                                   internalFormat, border))
4308          return;
4309 
4310       if (!_mesa_legal_texture_dimensions(ctx, target, level, width, height,
4311                                           1, border)) {
4312          _mesa_error(ctx, GL_INVALID_VALUE,
4313                      "glCopyTexImage%uD(invalid width=%d or height=%d)",
4314                      dims, width, height);
4315          return;
4316       }
4317    }
4318 
4319    assert(texObj);
4320 
4321    texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
4322                                            internalFormat, GL_NONE, GL_NONE);
4323 
4324    /* First check if reallocating the texture buffer can be avoided.
4325     * Without the realloc the copy can be 20x faster.
4326     */
4327    _mesa_lock_texture(ctx, texObj);
4328    {
4329       texImage = _mesa_select_tex_image(texObj, target, level);
4330       if (texImage && can_avoid_reallocation(texImage, internalFormat, texFormat,
4331                                              width, height, border)) {
4332          _mesa_unlock_texture(ctx, texObj);
4333          if (no_error) {
4334             copy_texture_sub_image_no_error(ctx, dims, texObj, target, level, 0,
4335                                             0, 0, x, y, width, height);
4336          } else {
4337             copy_texture_sub_image_err(ctx, dims, texObj, target, level, 0, 0,
4338                                        0, x, y, width, height,"CopyTexImage");
4339          }
4340          return;
4341       }
4342    }
4343    _mesa_unlock_texture(ctx, texObj);
4344    _mesa_perf_debug(ctx, MESA_DEBUG_SEVERITY_LOW, "glCopyTexImage "
4345                     "can't avoid reallocating texture storage\n");
4346 
4347    if (!no_error && _mesa_is_gles3(ctx)) {
4348       struct gl_renderbuffer *rb =
4349          _mesa_get_read_renderbuffer_for_format(ctx, internalFormat);
4350 
4351       if (_mesa_is_enum_format_unsized(internalFormat)) {
4352       /* Conversion from GL_RGB10_A2 source buffer format is not allowed in
4353        * OpenGL ES 3.0. Khronos bug# 9807.
4354        */
4355          if (rb->InternalFormat == GL_RGB10_A2) {
4356                _mesa_error(ctx, GL_INVALID_OPERATION,
4357                            "glCopyTexImage%uD(Reading from GL_RGB10_A2 buffer"
4358                            " and writing to unsized internal format)", dims);
4359                return;
4360          }
4361       }
4362       /* From Page 139 of OpenGL ES 3.0 spec:
4363        *    "If internalformat is sized, the internal format of the new texel
4364        *    array is internalformat, and this is also the new texel array’s
4365        *    effective internal format. If the component sizes of internalformat
4366        *    do not exactly match the corresponding component sizes of the source
4367        *    buffer’s effective internal format, described below, an
4368        *    INVALID_OPERATION error is generated. If internalformat is unsized,
4369        *    the internal format of the new texel array is the effective internal
4370        *    format of the source buffer, and this is also the new texel array’s
4371        *    effective internal format.
4372        */
4373       else if (formats_differ_in_component_sizes (texFormat, rb->Format)) {
4374             _mesa_error(ctx, GL_INVALID_OPERATION,
4375                         "glCopyTexImage%uD(component size changed in"
4376                         " internal format)", dims);
4377             return;
4378       }
4379    }
4380 
4381    assert(texFormat != MESA_FORMAT_NONE);
4382 
4383    if (!ctx->Driver.TestProxyTexImage(ctx, proxy_target(target),
4384                                       0, level, texFormat, 1,
4385                                       width, height, 1)) {
4386       _mesa_error(ctx, GL_OUT_OF_MEMORY,
4387                   "glCopyTexImage%uD(image too large)", dims);
4388       return;
4389    }
4390 
4391    if (border && ctx->Const.StripTextureBorder) {
4392       x += border;
4393       width -= border * 2;
4394       if (dims == 2) {
4395          y += border;
4396          height -= border * 2;
4397       }
4398       border = 0;
4399    }
4400 
4401    _mesa_lock_texture(ctx, texObj);
4402    {
4403       texImage = _mesa_get_tex_image(ctx, texObj, target, level);
4404 
4405       if (!texImage) {
4406          _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage%uD", dims);
4407       }
4408       else {
4409          GLint srcX = x, srcY = y, dstX = 0, dstY = 0, dstZ = 0;
4410          const GLuint face = _mesa_tex_target_to_face(target);
4411 
4412          /* Free old texture image */
4413          ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
4414 
4415          _mesa_init_teximage_fields(ctx, texImage, width, height, 1,
4416                                     border, internalFormat, texFormat);
4417 
4418          if (width && height) {
4419             /* Allocate texture memory (no pixel data yet) */
4420             ctx->Driver.AllocTextureImageBuffer(ctx, texImage);
4421 
4422             if (ctx->Const.NoClippingOnCopyTex ||
4423                 _mesa_clip_copytexsubimage(ctx, &dstX, &dstY, &srcX, &srcY,
4424                                            &width, &height)) {
4425                struct gl_renderbuffer *srcRb =
4426                   get_copy_tex_image_source(ctx, texImage->TexFormat);
4427 
4428                copytexsubimage_by_slice(ctx, texImage, dims,
4429                                         dstX, dstY, dstZ,
4430                                         srcRb, srcX, srcY, width, height);
4431             }
4432 
4433             check_gen_mipmap(ctx, target, texObj, level);
4434          }
4435 
4436          _mesa_update_fbo_texture(ctx, texObj, face, level);
4437 
4438          _mesa_dirty_texobj(ctx, texObj);
4439       }
4440    }
4441    _mesa_unlock_texture(ctx, texObj);
4442 }
4443 
4444 
4445 static void
copyteximage_err(struct gl_context * ctx,GLuint dims,GLenum target,GLint level,GLenum internalFormat,GLint x,GLint y,GLsizei width,GLsizei height,GLint border)4446 copyteximage_err(struct gl_context *ctx, GLuint dims,
4447                  GLenum target,
4448                  GLint level, GLenum internalFormat, GLint x, GLint y,
4449                  GLsizei width, GLsizei height, GLint border)
4450 {
4451    struct gl_texture_object* texObj = _mesa_get_current_tex_object(ctx, target);
4452    copyteximage(ctx, dims, texObj, target, level, internalFormat, x, y, width, height,
4453                 border, false);
4454 }
4455 
4456 
4457 static void
copyteximage_no_error(struct gl_context * ctx,GLuint dims,GLenum target,GLint level,GLenum internalFormat,GLint x,GLint y,GLsizei width,GLsizei height,GLint border)4458 copyteximage_no_error(struct gl_context *ctx, GLuint dims, GLenum target,
4459                       GLint level, GLenum internalFormat, GLint x, GLint y,
4460                       GLsizei width, GLsizei height, GLint border)
4461 {
4462    struct gl_texture_object* texObj = _mesa_get_current_tex_object(ctx, target);
4463    copyteximage(ctx, dims, texObj, target, level, internalFormat, x, y, width, height,
4464                 border, true);
4465 }
4466 
4467 
4468 void GLAPIENTRY
_mesa_CopyTexImage1D(GLenum target,GLint level,GLenum internalFormat,GLint x,GLint y,GLsizei width,GLint border)4469 _mesa_CopyTexImage1D( GLenum target, GLint level,
4470                       GLenum internalFormat,
4471                       GLint x, GLint y,
4472                       GLsizei width, GLint border )
4473 {
4474    GET_CURRENT_CONTEXT(ctx);
4475    copyteximage_err(ctx, 1, target, level, internalFormat, x, y, width, 1,
4476                     border);
4477 }
4478 
4479 
4480 void GLAPIENTRY
_mesa_CopyTextureImage1DEXT(GLuint texture,GLenum target,GLint level,GLenum internalFormat,GLint x,GLint y,GLsizei width,GLint border)4481 _mesa_CopyTextureImage1DEXT( GLuint texture, GLenum target, GLint level,
4482                              GLenum internalFormat,
4483                              GLint x, GLint y,
4484                              GLsizei width, GLint border )
4485 {
4486    GET_CURRENT_CONTEXT(ctx);
4487    struct gl_texture_object* texObj =
4488       _mesa_lookup_or_create_texture(ctx, target, texture, false, true,
4489                                      "glCopyTextureImage1DEXT");
4490    if (!texObj)
4491       return;
4492    copyteximage(ctx, 1, texObj, target, level, internalFormat, x, y, width, 1,
4493                 border, false);
4494 }
4495 
4496 
4497 void GLAPIENTRY
_mesa_CopyMultiTexImage1DEXT(GLenum texunit,GLenum target,GLint level,GLenum internalFormat,GLint x,GLint y,GLsizei width,GLint border)4498 _mesa_CopyMultiTexImage1DEXT( GLenum texunit, GLenum target, GLint level,
4499                               GLenum internalFormat,
4500                               GLint x, GLint y,
4501                               GLsizei width, GLint border )
4502 {
4503    GET_CURRENT_CONTEXT(ctx);
4504    struct gl_texture_object* texObj =
4505       _mesa_get_texobj_by_target_and_texunit(ctx, target,
4506                                              texunit - GL_TEXTURE0,
4507                                              false,
4508                                              "glCopyMultiTexImage1DEXT");
4509    if (!texObj)
4510       return;
4511    copyteximage(ctx, 1, texObj, target, level, internalFormat, x, y, width, 1,
4512                 border, false);
4513 }
4514 
4515 
4516 void GLAPIENTRY
_mesa_CopyTexImage2D(GLenum target,GLint level,GLenum internalFormat,GLint x,GLint y,GLsizei width,GLsizei height,GLint border)4517 _mesa_CopyTexImage2D( GLenum target, GLint level, GLenum internalFormat,
4518                       GLint x, GLint y, GLsizei width, GLsizei height,
4519                       GLint border )
4520 {
4521    GET_CURRENT_CONTEXT(ctx);
4522    copyteximage_err(ctx, 2, target, level, internalFormat,
4523                     x, y, width, height, border);
4524 }
4525 
4526 
4527 void GLAPIENTRY
_mesa_CopyTextureImage2DEXT(GLuint texture,GLenum target,GLint level,GLenum internalFormat,GLint x,GLint y,GLsizei width,GLsizei height,GLint border)4528 _mesa_CopyTextureImage2DEXT( GLuint texture, GLenum target, GLint level,
4529                              GLenum internalFormat,
4530                              GLint x, GLint y,
4531                              GLsizei width, GLsizei height,
4532                              GLint border )
4533 {
4534    GET_CURRENT_CONTEXT(ctx);
4535    struct gl_texture_object* texObj =
4536       _mesa_lookup_or_create_texture(ctx, target, texture, false, true,
4537                                      "glCopyTextureImage2DEXT");
4538    if (!texObj)
4539       return;
4540    copyteximage(ctx, 2, texObj, target, level, internalFormat, x, y, width, height,
4541                 border, false);
4542 }
4543 
4544 
4545 void GLAPIENTRY
_mesa_CopyMultiTexImage2DEXT(GLenum texunit,GLenum target,GLint level,GLenum internalFormat,GLint x,GLint y,GLsizei width,GLsizei height,GLint border)4546 _mesa_CopyMultiTexImage2DEXT( GLenum texunit, GLenum target, GLint level,
4547                               GLenum internalFormat,
4548                               GLint x, GLint y,
4549                               GLsizei width, GLsizei height, GLint border )
4550 {
4551    GET_CURRENT_CONTEXT(ctx);
4552    struct gl_texture_object* texObj =
4553       _mesa_get_texobj_by_target_and_texunit(ctx, target,
4554                                              texunit - GL_TEXTURE0,
4555                                              false,
4556                                              "glCopyMultiTexImage2DEXT");
4557    if (!texObj)
4558       return;
4559    copyteximage(ctx, 2, texObj, target, level, internalFormat, x, y, width, height,
4560                 border, false);
4561 }
4562 
4563 
4564 void GLAPIENTRY
_mesa_CopyTexImage1D_no_error(GLenum target,GLint level,GLenum internalFormat,GLint x,GLint y,GLsizei width,GLint border)4565 _mesa_CopyTexImage1D_no_error(GLenum target, GLint level, GLenum internalFormat,
4566                               GLint x, GLint y, GLsizei width, GLint border)
4567 {
4568    GET_CURRENT_CONTEXT(ctx);
4569    copyteximage_no_error(ctx, 1, target, level, internalFormat, x, y, width, 1,
4570                          border);
4571 }
4572 
4573 
4574 void GLAPIENTRY
_mesa_CopyTexImage2D_no_error(GLenum target,GLint level,GLenum internalFormat,GLint x,GLint y,GLsizei width,GLsizei height,GLint border)4575 _mesa_CopyTexImage2D_no_error(GLenum target, GLint level, GLenum internalFormat,
4576                               GLint x, GLint y, GLsizei width, GLsizei height,
4577                               GLint border)
4578 {
4579    GET_CURRENT_CONTEXT(ctx);
4580    copyteximage_no_error(ctx, 2, target, level, internalFormat,
4581                          x, y, width, height, border);
4582 }
4583 
4584 
4585 void GLAPIENTRY
_mesa_CopyTexSubImage1D(GLenum target,GLint level,GLint xoffset,GLint x,GLint y,GLsizei width)4586 _mesa_CopyTexSubImage1D(GLenum target, GLint level,
4587                         GLint xoffset, GLint x, GLint y, GLsizei width)
4588 {
4589    struct gl_texture_object* texObj;
4590    const char *self = "glCopyTexSubImage1D";
4591    GET_CURRENT_CONTEXT(ctx);
4592 
4593    /* Check target (proxies not allowed). Target must be checked prior to
4594     * calling _mesa_get_current_tex_object.
4595     */
4596    if (!legal_texsubimage_target(ctx, 1, target, false)) {
4597       _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", self,
4598                   _mesa_enum_to_string(target));
4599       return;
4600    }
4601 
4602    texObj = _mesa_get_current_tex_object(ctx, target);
4603    if (!texObj)
4604       return;
4605 
4606    copy_texture_sub_image_err(ctx, 1, texObj, target, level, xoffset, 0, 0,
4607                               x, y, width, 1, self);
4608 }
4609 
4610 
4611 void GLAPIENTRY
_mesa_CopyTexSubImage2D(GLenum target,GLint level,GLint xoffset,GLint yoffset,GLint x,GLint y,GLsizei width,GLsizei height)4612 _mesa_CopyTexSubImage2D(GLenum target, GLint level,
4613                         GLint xoffset, GLint yoffset,
4614                         GLint x, GLint y, GLsizei width, GLsizei height)
4615 {
4616    struct gl_texture_object* texObj;
4617    const char *self = "glCopyTexSubImage2D";
4618    GET_CURRENT_CONTEXT(ctx);
4619 
4620    /* Check target (proxies not allowed). Target must be checked prior to
4621     * calling _mesa_get_current_tex_object.
4622     */
4623    if (!legal_texsubimage_target(ctx, 2, target, false)) {
4624       _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", self,
4625                   _mesa_enum_to_string(target));
4626       return;
4627    }
4628 
4629    texObj = _mesa_get_current_tex_object(ctx, target);
4630    if (!texObj)
4631       return;
4632 
4633    copy_texture_sub_image_err(ctx, 2, texObj, target, level, xoffset, yoffset,
4634                               0, x, y, width, height, self);
4635 }
4636 
4637 
4638 void GLAPIENTRY
_mesa_CopyTexSubImage3D(GLenum target,GLint level,GLint xoffset,GLint yoffset,GLint zoffset,GLint x,GLint y,GLsizei width,GLsizei height)4639 _mesa_CopyTexSubImage3D(GLenum target, GLint level,
4640                         GLint xoffset, GLint yoffset, GLint zoffset,
4641                         GLint x, GLint y, GLsizei width, GLsizei height)
4642 {
4643    struct gl_texture_object* texObj;
4644    const char *self = "glCopyTexSubImage3D";
4645    GET_CURRENT_CONTEXT(ctx);
4646 
4647    /* Check target (proxies not allowed). Target must be checked prior to
4648     * calling _mesa_get_current_tex_object.
4649     */
4650    if (!legal_texsubimage_target(ctx, 3, target, false)) {
4651       _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", self,
4652                   _mesa_enum_to_string(target));
4653       return;
4654    }
4655 
4656    texObj = _mesa_get_current_tex_object(ctx, target);
4657    if (!texObj)
4658       return;
4659 
4660    copy_texture_sub_image_err(ctx, 3, texObj, target, level, xoffset, yoffset,
4661                               zoffset, x, y, width, height, self);
4662 }
4663 
4664 
4665 void GLAPIENTRY
_mesa_CopyTextureSubImage1D(GLuint texture,GLint level,GLint xoffset,GLint x,GLint y,GLsizei width)4666 _mesa_CopyTextureSubImage1D(GLuint texture, GLint level,
4667                             GLint xoffset, GLint x, GLint y, GLsizei width)
4668 {
4669    struct gl_texture_object* texObj;
4670    const char *self = "glCopyTextureSubImage1D";
4671    GET_CURRENT_CONTEXT(ctx);
4672 
4673    texObj = _mesa_lookup_texture_err(ctx, texture, self);
4674    if (!texObj)
4675       return;
4676 
4677    /* Check target (proxies not allowed). */
4678    if (!legal_texsubimage_target(ctx, 1, texObj->Target, true)) {
4679       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid target %s)", self,
4680                   _mesa_enum_to_string(texObj->Target));
4681       return;
4682    }
4683 
4684    copy_texture_sub_image_err(ctx, 1, texObj, texObj->Target, level, xoffset, 0,
4685                               0, x, y, width, 1, self);
4686 }
4687 
4688 
4689 void GLAPIENTRY
_mesa_CopyTextureSubImage1DEXT(GLuint texture,GLenum target,GLint level,GLint xoffset,GLint x,GLint y,GLsizei width)4690 _mesa_CopyTextureSubImage1DEXT(GLuint texture, GLenum target, GLint level,
4691                                GLint xoffset, GLint x, GLint y, GLsizei width)
4692 {
4693    struct gl_texture_object* texObj;
4694    const char *self = "glCopyTextureSubImage1DEXT";
4695    GET_CURRENT_CONTEXT(ctx);
4696 
4697    texObj = _mesa_lookup_or_create_texture(ctx, target, texture, false, true,
4698                                            self);
4699    if (!texObj)
4700       return;
4701 
4702    /* Check target (proxies not allowed). */
4703    if (!legal_texsubimage_target(ctx, 1, texObj->Target, true)) {
4704       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid target %s)", self,
4705                   _mesa_enum_to_string(texObj->Target));
4706       return;
4707    }
4708 
4709    copy_texture_sub_image_err(ctx, 1, texObj, texObj->Target, level, xoffset, 0,
4710                               0, x, y, width, 1, self);
4711 }
4712 
4713 
4714 void GLAPIENTRY
_mesa_CopyMultiTexSubImage1DEXT(GLenum texunit,GLenum target,GLint level,GLint xoffset,GLint x,GLint y,GLsizei width)4715 _mesa_CopyMultiTexSubImage1DEXT(GLenum texunit, GLenum target, GLint level,
4716                                 GLint xoffset, GLint x, GLint y, GLsizei width)
4717 {
4718    struct gl_texture_object* texObj;
4719    const char *self = "glCopyMultiTexSubImage1DEXT";
4720    GET_CURRENT_CONTEXT(ctx);
4721 
4722    texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
4723                                                    texunit - GL_TEXTURE0,
4724                                                    false, self);
4725    if (!texObj)
4726       return;
4727 
4728    copy_texture_sub_image_err(ctx, 1, texObj, texObj->Target, level, xoffset, 0,
4729                               0, x, y, width, 1, self);
4730 }
4731 
4732 
4733 void GLAPIENTRY
_mesa_CopyTextureSubImage2D(GLuint texture,GLint level,GLint xoffset,GLint yoffset,GLint x,GLint y,GLsizei width,GLsizei height)4734 _mesa_CopyTextureSubImage2D(GLuint texture, GLint level,
4735                             GLint xoffset, GLint yoffset,
4736                             GLint x, GLint y, GLsizei width, GLsizei height)
4737 {
4738    struct gl_texture_object* texObj;
4739    const char *self = "glCopyTextureSubImage2D";
4740    GET_CURRENT_CONTEXT(ctx);
4741 
4742    texObj = _mesa_lookup_texture_err(ctx, texture, self);
4743    if (!texObj)
4744       return;
4745 
4746    /* Check target (proxies not allowed). */
4747    if (!legal_texsubimage_target(ctx, 2, texObj->Target, true)) {
4748       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid target %s)", self,
4749                   _mesa_enum_to_string(texObj->Target));
4750       return;
4751    }
4752 
4753    copy_texture_sub_image_err(ctx, 2, texObj, texObj->Target, level, xoffset,
4754                               yoffset, 0, x, y, width, height, self);
4755 }
4756 
4757 
4758 void GLAPIENTRY
_mesa_CopyTextureSubImage2DEXT(GLuint texture,GLenum target,GLint level,GLint xoffset,GLint yoffset,GLint x,GLint y,GLsizei width,GLsizei height)4759 _mesa_CopyTextureSubImage2DEXT(GLuint texture, GLenum target, GLint level,
4760                                GLint xoffset, GLint yoffset,
4761                                GLint x, GLint y, GLsizei width, GLsizei height)
4762 {
4763    struct gl_texture_object* texObj;
4764    const char *self = "glCopyTextureSubImage2DEXT";
4765    GET_CURRENT_CONTEXT(ctx);
4766 
4767    texObj = _mesa_lookup_or_create_texture(ctx, target, texture, false, true, self);
4768    if (!texObj)
4769       return;
4770 
4771    /* Check target (proxies not allowed). */
4772    if (!legal_texsubimage_target(ctx, 2, texObj->Target, true)) {
4773       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid target %s)", self,
4774                   _mesa_enum_to_string(texObj->Target));
4775       return;
4776    }
4777 
4778    copy_texture_sub_image_err(ctx, 2, texObj, texObj->Target, level, xoffset,
4779                               yoffset, 0, x, y, width, height, self);
4780 }
4781 
4782 
4783 void GLAPIENTRY
_mesa_CopyMultiTexSubImage2DEXT(GLenum texunit,GLenum target,GLint level,GLint xoffset,GLint yoffset,GLint x,GLint y,GLsizei width,GLsizei height)4784 _mesa_CopyMultiTexSubImage2DEXT(GLenum texunit, GLenum target, GLint level,
4785                                GLint xoffset, GLint yoffset,
4786                                GLint x, GLint y, GLsizei width, GLsizei height)
4787 {
4788    struct gl_texture_object* texObj;
4789    const char *self = "glCopyMultiTexSubImage2DEXT";
4790    GET_CURRENT_CONTEXT(ctx);
4791 
4792    texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
4793                                                    texunit - GL_TEXTURE0,
4794                                                    false, self);
4795    if (!texObj)
4796       return;
4797 
4798    copy_texture_sub_image_err(ctx, 2, texObj, texObj->Target, level, xoffset,
4799                               yoffset, 0, x, y, width, height, self);
4800 }
4801 
4802 void GLAPIENTRY
_mesa_CopyTextureSubImage3D(GLuint texture,GLint level,GLint xoffset,GLint yoffset,GLint zoffset,GLint x,GLint y,GLsizei width,GLsizei height)4803 _mesa_CopyTextureSubImage3D(GLuint texture, GLint level,
4804                             GLint xoffset, GLint yoffset, GLint zoffset,
4805                             GLint x, GLint y, GLsizei width, GLsizei height)
4806 {
4807    struct gl_texture_object* texObj;
4808    const char *self = "glCopyTextureSubImage3D";
4809    GET_CURRENT_CONTEXT(ctx);
4810 
4811    texObj = _mesa_lookup_texture_err(ctx, texture, self);
4812    if (!texObj)
4813       return;
4814 
4815    /* Check target (proxies not allowed). */
4816    if (!legal_texsubimage_target(ctx, 3, texObj->Target, true)) {
4817       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid target %s)", self,
4818                   _mesa_enum_to_string(texObj->Target));
4819       return;
4820    }
4821 
4822    if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
4823       /* Act like CopyTexSubImage2D */
4824       copy_texture_sub_image_err(ctx, 2, texObj,
4825                                 GL_TEXTURE_CUBE_MAP_POSITIVE_X + zoffset,
4826                                 level, xoffset, yoffset, 0, x, y, width, height,
4827                                 self);
4828    }
4829    else
4830       copy_texture_sub_image_err(ctx, 3, texObj, texObj->Target, level, xoffset,
4831                                  yoffset, zoffset, x, y, width, height, self);
4832 }
4833 
4834 
4835 void GLAPIENTRY
_mesa_CopyTextureSubImage3DEXT(GLuint texture,GLenum target,GLint level,GLint xoffset,GLint yoffset,GLint zoffset,GLint x,GLint y,GLsizei width,GLsizei height)4836 _mesa_CopyTextureSubImage3DEXT(GLuint texture, GLenum target, GLint level,
4837                                GLint xoffset, GLint yoffset, GLint zoffset,
4838                                GLint x, GLint y, GLsizei width, GLsizei height)
4839 {
4840    struct gl_texture_object* texObj;
4841    const char *self = "glCopyTextureSubImage3D";
4842    GET_CURRENT_CONTEXT(ctx);
4843 
4844    texObj = _mesa_lookup_or_create_texture(ctx, target, texture, false, true, self);
4845    if (!texObj)
4846       return;
4847 
4848    /* Check target (proxies not allowed). */
4849    if (!legal_texsubimage_target(ctx, 3, texObj->Target, true)) {
4850       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid target %s)", self,
4851                   _mesa_enum_to_string(texObj->Target));
4852       return;
4853    }
4854 
4855    if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
4856       /* Act like CopyTexSubImage2D */
4857       copy_texture_sub_image_err(ctx, 2, texObj,
4858                                 GL_TEXTURE_CUBE_MAP_POSITIVE_X + zoffset,
4859                                 level, xoffset, yoffset, 0, x, y, width, height,
4860                                 self);
4861    }
4862    else
4863       copy_texture_sub_image_err(ctx, 3, texObj, texObj->Target, level, xoffset,
4864                                  yoffset, zoffset, x, y, width, height, self);
4865 }
4866 
4867 
4868 void GLAPIENTRY
_mesa_CopyMultiTexSubImage3DEXT(GLenum texunit,GLenum target,GLint level,GLint xoffset,GLint yoffset,GLint zoffset,GLint x,GLint y,GLsizei width,GLsizei height)4869 _mesa_CopyMultiTexSubImage3DEXT(GLenum texunit, GLenum target, GLint level,
4870                                 GLint xoffset, GLint yoffset, GLint zoffset,
4871                                 GLint x, GLint y, GLsizei width, GLsizei height)
4872 {
4873    struct gl_texture_object* texObj;
4874    const char *self = "glCopyMultiTexSubImage3D";
4875    GET_CURRENT_CONTEXT(ctx);
4876 
4877    texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
4878                                                    texunit - GL_TEXTURE0,
4879                                                    false, self);
4880    if (!texObj)
4881       return;
4882 
4883    if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
4884       /* Act like CopyTexSubImage2D */
4885       copy_texture_sub_image_err(ctx, 2, texObj,
4886                                 GL_TEXTURE_CUBE_MAP_POSITIVE_X + zoffset,
4887                                 level, xoffset, yoffset, 0, x, y, width, height,
4888                                 self);
4889    }
4890    else
4891       copy_texture_sub_image_err(ctx, 3, texObj, texObj->Target, level, xoffset,
4892                                  yoffset, zoffset, x, y, width, height, self);
4893 }
4894 
4895 
4896 void GLAPIENTRY
_mesa_CopyTexSubImage1D_no_error(GLenum target,GLint level,GLint xoffset,GLint x,GLint y,GLsizei width)4897 _mesa_CopyTexSubImage1D_no_error(GLenum target, GLint level, GLint xoffset,
4898                                  GLint x, GLint y, GLsizei width)
4899 {
4900    GET_CURRENT_CONTEXT(ctx);
4901 
4902    struct gl_texture_object* texObj = _mesa_get_current_tex_object(ctx, target);
4903    copy_texture_sub_image_no_error(ctx, 1, texObj, target, level, xoffset, 0, 0,
4904                                    x, y, width, 1);
4905 }
4906 
4907 
4908 void GLAPIENTRY
_mesa_CopyTexSubImage2D_no_error(GLenum target,GLint level,GLint xoffset,GLint yoffset,GLint x,GLint y,GLsizei width,GLsizei height)4909 _mesa_CopyTexSubImage2D_no_error(GLenum target, GLint level, GLint xoffset,
4910                                  GLint yoffset, GLint x, GLint y, GLsizei width,
4911                                  GLsizei height)
4912 {
4913    GET_CURRENT_CONTEXT(ctx);
4914 
4915    struct gl_texture_object* texObj = _mesa_get_current_tex_object(ctx, target);
4916    copy_texture_sub_image_no_error(ctx, 2, texObj, target, level, xoffset,
4917                                    yoffset, 0, x, y, width, height);
4918 }
4919 
4920 
4921 void GLAPIENTRY
_mesa_CopyTexSubImage3D_no_error(GLenum target,GLint level,GLint xoffset,GLint yoffset,GLint zoffset,GLint x,GLint y,GLsizei width,GLsizei height)4922 _mesa_CopyTexSubImage3D_no_error(GLenum target, GLint level, GLint xoffset,
4923                                  GLint yoffset, GLint zoffset, GLint x, GLint y,
4924                                  GLsizei width, GLsizei height)
4925 {
4926    GET_CURRENT_CONTEXT(ctx);
4927 
4928    struct gl_texture_object* texObj = _mesa_get_current_tex_object(ctx, target);
4929    copy_texture_sub_image_no_error(ctx, 3, texObj, target, level, xoffset,
4930                                    yoffset, zoffset, x, y, width, height);
4931 }
4932 
4933 
4934 void GLAPIENTRY
_mesa_CopyTextureSubImage1D_no_error(GLuint texture,GLint level,GLint xoffset,GLint x,GLint y,GLsizei width)4935 _mesa_CopyTextureSubImage1D_no_error(GLuint texture, GLint level, GLint xoffset,
4936                                      GLint x, GLint y, GLsizei width)
4937 {
4938    GET_CURRENT_CONTEXT(ctx);
4939 
4940    struct gl_texture_object* texObj = _mesa_lookup_texture(ctx, texture);
4941    copy_texture_sub_image_no_error(ctx, 1, texObj, texObj->Target, level,
4942                                    xoffset, 0, 0, x, y, width, 1);
4943 }
4944 
4945 
4946 void GLAPIENTRY
_mesa_CopyTextureSubImage2D_no_error(GLuint texture,GLint level,GLint xoffset,GLint yoffset,GLint x,GLint y,GLsizei width,GLsizei height)4947 _mesa_CopyTextureSubImage2D_no_error(GLuint texture, GLint level, GLint xoffset,
4948                                      GLint yoffset, GLint x, GLint y,
4949                                      GLsizei width, GLsizei height)
4950 {
4951    GET_CURRENT_CONTEXT(ctx);
4952 
4953    struct gl_texture_object* texObj = _mesa_lookup_texture(ctx, texture);
4954    copy_texture_sub_image_no_error(ctx, 2, texObj, texObj->Target, level,
4955                                    xoffset, yoffset, 0, x, y, width, height);
4956 }
4957 
4958 
4959 void GLAPIENTRY
_mesa_CopyTextureSubImage3D_no_error(GLuint texture,GLint level,GLint xoffset,GLint yoffset,GLint zoffset,GLint x,GLint y,GLsizei width,GLsizei height)4960 _mesa_CopyTextureSubImage3D_no_error(GLuint texture, GLint level, GLint xoffset,
4961                                      GLint yoffset, GLint zoffset, GLint x,
4962                                      GLint y, GLsizei width, GLsizei height)
4963 {
4964    GET_CURRENT_CONTEXT(ctx);
4965 
4966    struct gl_texture_object* texObj = _mesa_lookup_texture(ctx, texture);
4967    if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
4968       /* Act like CopyTexSubImage2D */
4969       copy_texture_sub_image_no_error(ctx, 2, texObj,
4970                                       GL_TEXTURE_CUBE_MAP_POSITIVE_X + zoffset,
4971                                       level, xoffset, yoffset, 0, x, y, width,
4972                                       height);
4973    }
4974    else
4975       copy_texture_sub_image_no_error(ctx, 3, texObj, texObj->Target, level,
4976                                       xoffset, yoffset, zoffset, x, y, width,
4977                                       height);
4978 }
4979 
4980 
4981 static bool
check_clear_tex_image(struct gl_context * ctx,const char * function,struct gl_texture_image * texImage,GLenum format,GLenum type,const void * data,GLubyte * clearValue)4982 check_clear_tex_image(struct gl_context *ctx,
4983                       const char *function,
4984                       struct gl_texture_image *texImage,
4985                       GLenum format, GLenum type,
4986                       const void *data,
4987                       GLubyte *clearValue)
4988 {
4989    struct gl_texture_object *texObj = texImage->TexObject;
4990    static const GLubyte zeroData[MAX_PIXEL_BYTES];
4991    GLenum internalFormat = texImage->InternalFormat;
4992    GLenum err;
4993 
4994    if (texObj->Target == GL_TEXTURE_BUFFER) {
4995       _mesa_error(ctx, GL_INVALID_OPERATION,
4996                   "%s(buffer texture)", function);
4997       return false;
4998    }
4999 
5000    if (_mesa_is_compressed_format(ctx, internalFormat)) {
5001       _mesa_error(ctx, GL_INVALID_OPERATION,
5002                   "%s(compressed texture)", function);
5003       return false;
5004    }
5005 
5006    err = _mesa_error_check_format_and_type(ctx, format, type);
5007    if (err != GL_NO_ERROR) {
5008       _mesa_error(ctx, err,
5009                   "%s(incompatible format = %s, type = %s)",
5010                   function,
5011                   _mesa_enum_to_string(format),
5012                   _mesa_enum_to_string(type));
5013       return false;
5014    }
5015 
5016    /* make sure internal format and format basically agree */
5017    if (!texture_formats_agree(internalFormat, format)) {
5018       _mesa_error(ctx, GL_INVALID_OPERATION,
5019                   "%s(incompatible internalFormat = %s, format = %s)",
5020                   function,
5021                   _mesa_enum_to_string(internalFormat),
5022                   _mesa_enum_to_string(format));
5023       return false;
5024    }
5025 
5026    if (ctx->Version >= 30 || ctx->Extensions.EXT_texture_integer) {
5027       /* both source and dest must be integer-valued, or neither */
5028       if (_mesa_is_format_integer_color(texImage->TexFormat) !=
5029           _mesa_is_enum_format_integer(format)) {
5030          _mesa_error(ctx, GL_INVALID_OPERATION,
5031                      "%s(integer/non-integer format mismatch)",
5032                      function);
5033          return false;
5034       }
5035    }
5036 
5037    if (!_mesa_texstore(ctx,
5038                        1, /* dims */
5039                        texImage->_BaseFormat,
5040                        texImage->TexFormat,
5041                        0, /* dstRowStride */
5042                        &clearValue,
5043                        1, 1, 1, /* srcWidth/Height/Depth */
5044                        format, type,
5045                        data ? data : zeroData,
5046                        &ctx->DefaultPacking)) {
5047       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid format)", function);
5048       return false;
5049    }
5050 
5051    return true;
5052 }
5053 
5054 
5055 static struct gl_texture_object *
get_tex_obj_for_clear(struct gl_context * ctx,const char * function,GLuint texture)5056 get_tex_obj_for_clear(struct gl_context *ctx,
5057                       const char *function,
5058                       GLuint texture)
5059 {
5060    struct gl_texture_object *texObj;
5061 
5062    texObj = _mesa_lookup_texture_err(ctx, texture, function);
5063    if (!texObj)
5064       return NULL;
5065 
5066    if (texObj->Target == 0) {
5067       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(unbound tex)", function);
5068       return NULL;
5069    }
5070 
5071    return texObj;
5072 }
5073 
5074 
5075 /**
5076  * For clearing cube textures, the zoffset and depth parameters indicate
5077  * which cube map faces are to be cleared.  This is the one case where we
5078  * need to be concerned with multiple gl_texture_images.  This function
5079  * returns the array of texture images to clear for cube maps, or one
5080  * texture image otherwise.
5081  * \return number of texture images, 0 for error, 6 for cube, 1 otherwise.
5082  */
5083 static int
get_tex_images_for_clear(struct gl_context * ctx,const char * function,struct gl_texture_object * texObj,GLint level,struct gl_texture_image ** texImages)5084 get_tex_images_for_clear(struct gl_context *ctx,
5085                          const char *function,
5086                          struct gl_texture_object *texObj,
5087                          GLint level,
5088                          struct gl_texture_image **texImages)
5089 {
5090    GLenum target;
5091    int numFaces, i;
5092 
5093    if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
5094       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid level)", function);
5095       return 0;
5096    }
5097 
5098    if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
5099       target = GL_TEXTURE_CUBE_MAP_POSITIVE_X;
5100       numFaces = MAX_FACES;
5101    }
5102    else {
5103       target = texObj->Target;
5104       numFaces = 1;
5105    }
5106 
5107    for (i = 0; i < numFaces; i++) {
5108       texImages[i] = _mesa_select_tex_image(texObj, target + i, level);
5109       if (texImages[i] == NULL) {
5110          _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid level)", function);
5111          return 0;
5112       }
5113    }
5114 
5115    return numFaces;
5116 }
5117 
5118 
5119 void GLAPIENTRY
_mesa_ClearTexSubImage(GLuint texture,GLint level,GLint xoffset,GLint yoffset,GLint zoffset,GLsizei width,GLsizei height,GLsizei depth,GLenum format,GLenum type,const void * data)5120 _mesa_ClearTexSubImage(GLuint texture, GLint level,
5121                        GLint xoffset, GLint yoffset, GLint zoffset,
5122                        GLsizei width, GLsizei height, GLsizei depth,
5123                        GLenum format, GLenum type, const void *data)
5124 {
5125    GET_CURRENT_CONTEXT(ctx);
5126    struct gl_texture_object *texObj;
5127    struct gl_texture_image *texImages[MAX_FACES];
5128    GLubyte clearValue[MAX_FACES][MAX_PIXEL_BYTES];
5129    int i, numImages;
5130    int minDepth, maxDepth;
5131 
5132    texObj = get_tex_obj_for_clear(ctx, "glClearTexSubImage", texture);
5133 
5134    if (texObj == NULL)
5135       return;
5136 
5137    _mesa_lock_texture(ctx, texObj);
5138 
5139    numImages = get_tex_images_for_clear(ctx, "glClearTexSubImage",
5140                                         texObj, level, texImages);
5141    if (numImages == 0)
5142       goto out;
5143 
5144    if (numImages == 1) {
5145       minDepth = -(int) texImages[0]->Border;
5146       maxDepth = texImages[0]->Depth;
5147    } else {
5148       assert(numImages == MAX_FACES);
5149       minDepth = 0;
5150       maxDepth = numImages;
5151    }
5152 
5153    if (xoffset < -(GLint) texImages[0]->Border ||
5154        yoffset < -(GLint) texImages[0]->Border ||
5155        zoffset < minDepth ||
5156        width < 0 ||
5157        height < 0 ||
5158        depth < 0 ||
5159        xoffset + width > texImages[0]->Width ||
5160        yoffset + height > texImages[0]->Height ||
5161        zoffset + depth > maxDepth) {
5162       _mesa_error(ctx, GL_INVALID_OPERATION,
5163                   "glClearSubTexImage(invalid dimensions)");
5164       goto out;
5165    }
5166 
5167    if (numImages == 1) {
5168       if (check_clear_tex_image(ctx, "glClearTexSubImage", texImages[0],
5169                                 format, type, data, clearValue[0])) {
5170          ctx->Driver.ClearTexSubImage(ctx,
5171                                       texImages[0],
5172                                       xoffset, yoffset, zoffset,
5173                                       width, height, depth,
5174                                       data ? clearValue[0] : NULL);
5175       }
5176    } else {
5177       /* loop over cube face images */
5178       for (i = zoffset; i < zoffset + depth; i++) {
5179          assert(i < MAX_FACES);
5180          if (!check_clear_tex_image(ctx, "glClearTexSubImage", texImages[i],
5181                                     format, type, data, clearValue[i]))
5182             goto out;
5183       }
5184       for (i = zoffset; i < zoffset + depth; i++) {
5185          ctx->Driver.ClearTexSubImage(ctx,
5186                                       texImages[i],
5187                                       xoffset, yoffset, 0,
5188                                       width, height, 1,
5189                                       data ? clearValue[i] : NULL);
5190       }
5191    }
5192 
5193  out:
5194    _mesa_unlock_texture(ctx, texObj);
5195 }
5196 
5197 
5198 void GLAPIENTRY
_mesa_ClearTexImage(GLuint texture,GLint level,GLenum format,GLenum type,const void * data)5199 _mesa_ClearTexImage( GLuint texture, GLint level,
5200                      GLenum format, GLenum type, const void *data )
5201 {
5202    GET_CURRENT_CONTEXT(ctx);
5203    struct gl_texture_object *texObj;
5204    struct gl_texture_image *texImages[MAX_FACES];
5205    GLubyte clearValue[MAX_FACES][MAX_PIXEL_BYTES];
5206    int i, numImages;
5207 
5208    texObj = get_tex_obj_for_clear(ctx, "glClearTexImage", texture);
5209 
5210    if (texObj == NULL)
5211       return;
5212 
5213    _mesa_lock_texture(ctx, texObj);
5214 
5215    numImages = get_tex_images_for_clear(ctx, "glClearTexImage",
5216                                         texObj, level, texImages);
5217 
5218    for (i = 0; i < numImages; i++) {
5219       if (!check_clear_tex_image(ctx, "glClearTexImage", texImages[i], format,
5220                                  type, data, clearValue[i]))
5221          goto out;
5222    }
5223 
5224    for (i = 0; i < numImages; i++) {
5225       ctx->Driver.ClearTexSubImage(ctx, texImages[i],
5226                                    -(GLint) texImages[i]->Border, /* xoffset */
5227                                    -(GLint) texImages[i]->Border, /* yoffset */
5228                                    -(GLint) texImages[i]->Border, /* zoffset */
5229                                    texImages[i]->Width,
5230                                    texImages[i]->Height,
5231                                    texImages[i]->Depth,
5232                                    data ? clearValue[i] : NULL);
5233    }
5234 
5235 out:
5236    _mesa_unlock_texture(ctx, texObj);
5237 }
5238 
5239 
5240 
5241 
5242 /**********************************************************************/
5243 /******                   Compressed Textures                    ******/
5244 /**********************************************************************/
5245 
5246 
5247 /**
5248  * Target checking for glCompressedTexSubImage[123]D().
5249  * \return GL_TRUE if error, GL_FALSE if no error
5250  * Must come before other error checking so that the texture object can
5251  * be correctly retrieved using _mesa_get_current_tex_object.
5252  */
5253 static GLboolean
compressed_subtexture_target_check(struct gl_context * ctx,GLenum target,GLint dims,GLenum intFormat,bool dsa,const char * caller)5254 compressed_subtexture_target_check(struct gl_context *ctx, GLenum target,
5255                                    GLint dims, GLenum intFormat, bool dsa,
5256                                    const char *caller)
5257 {
5258    GLboolean targetOK;
5259    mesa_format format;
5260    enum mesa_format_layout layout;
5261 
5262    if (dsa && target == GL_TEXTURE_RECTANGLE) {
5263       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid target %s)", caller,
5264                   _mesa_enum_to_string(target));
5265       return GL_TRUE;
5266    }
5267 
5268    switch (dims) {
5269    case 2:
5270       switch (target) {
5271       case GL_TEXTURE_2D:
5272          targetOK = GL_TRUE;
5273          break;
5274       case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
5275       case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
5276       case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
5277       case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
5278       case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
5279       case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
5280          targetOK = ctx->Extensions.ARB_texture_cube_map;
5281          break;
5282       default:
5283          targetOK = GL_FALSE;
5284          break;
5285       }
5286       break;
5287    case 3:
5288       switch (target) {
5289       case GL_TEXTURE_CUBE_MAP:
5290          targetOK = dsa && ctx->Extensions.ARB_texture_cube_map;
5291          break;
5292       case GL_TEXTURE_2D_ARRAY:
5293          targetOK = _mesa_is_gles3(ctx) ||
5294             (_mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array);
5295          break;
5296       case GL_TEXTURE_CUBE_MAP_ARRAY:
5297          targetOK = _mesa_has_texture_cube_map_array(ctx);
5298          break;
5299       case GL_TEXTURE_3D:
5300          targetOK = GL_TRUE;
5301          /*
5302           * OpenGL 4.5 spec (30.10.2014) says in Section 8.7 Compressed Texture
5303           * Images:
5304           *    "An INVALID_OPERATION error is generated by
5305           *    CompressedTex*SubImage3D if the internal format of the texture
5306           *    is one of the EAC, ETC2, or RGTC formats and either border is
5307           *    non-zero, or the effective target for the texture is not
5308           *    TEXTURE_2D_ARRAY."
5309           *
5310           * NOTE: that's probably a spec error.  It should probably say
5311           *    "... or the effective target for the texture is not
5312           *    TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP, nor
5313           *    GL_TEXTURE_CUBE_MAP_ARRAY."
5314           * since those targets are 2D images and they support all compression
5315           * formats.
5316           *
5317           * Instead of listing all these, just list those which are allowed,
5318           * which is (at this time) only bptc. Otherwise we'd say s3tc (and
5319           * more) are valid here, which they are not, but of course not
5320           * mentioned by core spec.
5321           *
5322           * Also, from GL_KHR_texture_compression_astc_{hdr,ldr}:
5323           *
5324           *    "Add a second new column "3D Tex." which is empty for all non-ASTC
5325           *     formats. If only the LDR profile is supported by the implementation,
5326           *     this column is also empty for all ASTC formats. If both the LDR and HDR
5327           *     profiles are supported, this column is checked for all ASTC formats."
5328           *
5329           *    "An INVALID_OPERATION error is generated by CompressedTexSubImage3D if
5330           *     <format> is one of the formats in table 8.19 and <target> is not
5331           *     TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP_ARRAY, or TEXTURE_3D.
5332           *
5333           *     An INVALID_OPERATION error is generated by CompressedTexSubImage3D if
5334           *     <format> is TEXTURE_CUBE_MAP_ARRAY and the "Cube Map Array" column of
5335           *     table 8.19 is *not* checked, or if <format> is TEXTURE_3D and the "3D
5336           *     Tex." column of table 8.19 is *not* checked"
5337           *
5338           * And from GL_KHR_texture_compression_astc_sliced_3d:
5339           *
5340           *    "Modify the "3D Tex." column to be checked for all ASTC formats."
5341           */
5342          format = _mesa_glenum_to_compressed_format(intFormat);
5343          layout = _mesa_get_format_layout(format);
5344          switch (layout) {
5345          case MESA_FORMAT_LAYOUT_BPTC:
5346             /* valid format */
5347             break;
5348          case MESA_FORMAT_LAYOUT_ASTC:
5349             targetOK =
5350                ctx->Extensions.KHR_texture_compression_astc_hdr ||
5351                ctx->Extensions.KHR_texture_compression_astc_sliced_3d;
5352             break;
5353          default:
5354             /* invalid format */
5355             _mesa_error(ctx, GL_INVALID_OPERATION,
5356                         "%s(invalid target %s for format %s)", caller,
5357                         _mesa_enum_to_string(target),
5358                         _mesa_enum_to_string(intFormat));
5359             return GL_TRUE;
5360          }
5361          break;
5362       default:
5363          targetOK = GL_FALSE;
5364       }
5365 
5366       break;
5367    default:
5368       assert(dims == 1);
5369       /* no 1D compressed textures at this time */
5370       targetOK = GL_FALSE;
5371       break;
5372    }
5373 
5374    if (!targetOK) {
5375       _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", caller,
5376                   _mesa_enum_to_string(target));
5377       return GL_TRUE;
5378    }
5379 
5380    return GL_FALSE;
5381 }
5382 
5383 /**
5384  * Error checking for glCompressedTexSubImage[123]D().
5385  * \return GL_TRUE if error, GL_FALSE if no error
5386  */
5387 static GLboolean
compressed_subtexture_error_check(struct gl_context * ctx,GLint dims,const struct gl_texture_object * texObj,GLenum target,GLint level,GLint xoffset,GLint yoffset,GLint zoffset,GLsizei width,GLsizei height,GLsizei depth,GLenum format,GLsizei imageSize,const GLvoid * data,const char * callerName)5388 compressed_subtexture_error_check(struct gl_context *ctx, GLint dims,
5389                                   const struct gl_texture_object *texObj,
5390                                   GLenum target, GLint level,
5391                                   GLint xoffset, GLint yoffset, GLint zoffset,
5392                                   GLsizei width, GLsizei height, GLsizei depth,
5393                                   GLenum format, GLsizei imageSize,
5394                                   const GLvoid *data, const char *callerName)
5395 {
5396    struct gl_texture_image *texImage;
5397    GLint expectedSize;
5398 
5399    /* this will catch any invalid compressed format token */
5400    if (!_mesa_is_compressed_format(ctx, format)) {
5401       _mesa_error(ctx, GL_INVALID_ENUM, "%s(format)", callerName);
5402       return GL_TRUE;
5403    }
5404 
5405    if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
5406       _mesa_error(ctx, GL_INVALID_VALUE, "%s(level=%d)", callerName, level);
5407       return GL_TRUE;
5408    }
5409 
5410    /* validate the bound PBO, if any */
5411    if (!_mesa_validate_pbo_source_compressed(ctx, dims, &ctx->Unpack,
5412                                      imageSize, data, callerName)) {
5413       return GL_TRUE;
5414    }
5415 
5416    /* Check for invalid pixel storage modes */
5417    if (!_mesa_compressed_pixel_storage_error_check(ctx, dims,
5418                                                    &ctx->Unpack, callerName)) {
5419       return GL_TRUE;
5420    }
5421 
5422    expectedSize = compressed_tex_size(width, height, depth, format);
5423    if (expectedSize != imageSize) {
5424       _mesa_error(ctx, GL_INVALID_VALUE, "%s(size=%d)", callerName, imageSize);
5425       return GL_TRUE;
5426    }
5427 
5428    texImage = _mesa_select_tex_image(texObj, target, level);
5429    if (!texImage) {
5430       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid texture level %d)",
5431                   callerName, level);
5432       return GL_TRUE;
5433    }
5434 
5435    if ((GLint) format != texImage->InternalFormat) {
5436       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(format=%s)",
5437                   callerName, _mesa_enum_to_string(format));
5438       return GL_TRUE;
5439    }
5440 
5441    if (compressedteximage_only_format(format)) {
5442       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(format=%s cannot be updated)",
5443                   callerName, _mesa_enum_to_string(format));
5444       return GL_TRUE;
5445    }
5446 
5447    if (error_check_subtexture_negative_dimensions(ctx, dims, width, height,
5448                                                   depth, callerName)) {
5449       return GL_TRUE;
5450    }
5451 
5452    if (error_check_subtexture_dimensions(ctx, dims, texImage, xoffset, yoffset,
5453                                          zoffset, width, height, depth,
5454                                          callerName)) {
5455       return GL_TRUE;
5456    }
5457 
5458    return GL_FALSE;
5459 }
5460 
5461 
5462 void GLAPIENTRY
_mesa_CompressedTexImage1D(GLenum target,GLint level,GLenum internalFormat,GLsizei width,GLint border,GLsizei imageSize,const GLvoid * data)5463 _mesa_CompressedTexImage1D(GLenum target, GLint level,
5464                               GLenum internalFormat, GLsizei width,
5465                               GLint border, GLsizei imageSize,
5466                               const GLvoid *data)
5467 {
5468    GET_CURRENT_CONTEXT(ctx);
5469    teximage_err(ctx, GL_TRUE, 1, target, level, internalFormat,
5470                 width, 1, 1, border, GL_NONE, GL_NONE, imageSize, data);
5471 }
5472 
5473 
5474 void GLAPIENTRY
_mesa_CompressedTextureImage1DEXT(GLuint texture,GLenum target,GLint level,GLenum internalFormat,GLsizei width,GLint border,GLsizei imageSize,const GLvoid * pixels)5475 _mesa_CompressedTextureImage1DEXT(GLuint texture, GLenum target, GLint level,
5476                                   GLenum internalFormat, GLsizei width,
5477                                   GLint border, GLsizei imageSize,
5478                                   const GLvoid *pixels)
5479 {
5480    struct gl_texture_object*  texObj;
5481    GET_CURRENT_CONTEXT(ctx);
5482 
5483    texObj = _mesa_lookup_or_create_texture(ctx, target, texture, false, true,
5484                                            "glCompressedTextureImage1DEXT");
5485    if (!texObj)
5486       return;
5487    teximage(ctx, GL_TRUE, 1, texObj, target, level, internalFormat,
5488             width, 1, 1, border, GL_NONE, GL_NONE, imageSize, pixels, false);
5489 }
5490 
5491 
5492 void GLAPIENTRY
_mesa_CompressedMultiTexImage1DEXT(GLenum texunit,GLenum target,GLint level,GLenum internalFormat,GLsizei width,GLint border,GLsizei imageSize,const GLvoid * pixels)5493 _mesa_CompressedMultiTexImage1DEXT(GLenum texunit, GLenum target, GLint level,
5494                                    GLenum internalFormat, GLsizei width,
5495                                    GLint border, GLsizei imageSize,
5496                                    const GLvoid *pixels)
5497 {
5498    struct gl_texture_object*  texObj;
5499    GET_CURRENT_CONTEXT(ctx);
5500 
5501    texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
5502                                                    texunit - GL_TEXTURE0,
5503                                                    true,
5504                                                    "glCompressedMultiTexImage1DEXT");
5505    if (!texObj)
5506       return;
5507    teximage(ctx, GL_TRUE, 1, texObj, target, level, internalFormat,
5508             width, 1, 1, border, GL_NONE, GL_NONE, imageSize, pixels, false);
5509 }
5510 
5511 
5512 void GLAPIENTRY
_mesa_CompressedTexImage2D(GLenum target,GLint level,GLenum internalFormat,GLsizei width,GLsizei height,GLint border,GLsizei imageSize,const GLvoid * data)5513 _mesa_CompressedTexImage2D(GLenum target, GLint level,
5514                               GLenum internalFormat, GLsizei width,
5515                               GLsizei height, GLint border, GLsizei imageSize,
5516                               const GLvoid *data)
5517 {
5518    GET_CURRENT_CONTEXT(ctx);
5519    teximage_err(ctx, GL_TRUE, 2, target, level, internalFormat,
5520                 width, height, 1, border, GL_NONE, GL_NONE, imageSize, data);
5521 }
5522 
5523 
5524 void GLAPIENTRY
_mesa_CompressedTextureImage2DEXT(GLuint texture,GLenum target,GLint level,GLenum internalFormat,GLsizei width,GLsizei height,GLint border,GLsizei imageSize,const GLvoid * pixels)5525 _mesa_CompressedTextureImage2DEXT(GLuint texture, GLenum target, GLint level,
5526                                   GLenum internalFormat, GLsizei width,
5527                                   GLsizei height, GLint border, GLsizei imageSize,
5528                                   const GLvoid *pixels)
5529 {
5530    struct gl_texture_object*  texObj;
5531    GET_CURRENT_CONTEXT(ctx);
5532 
5533    texObj = _mesa_lookup_or_create_texture(ctx, target, texture, false, true,
5534                                            "glCompressedTextureImage2DEXT");
5535    if (!texObj)
5536       return;
5537    teximage(ctx, GL_TRUE, 2, texObj, target, level, internalFormat,
5538             width, height, 1, border, GL_NONE, GL_NONE, imageSize, pixels, false);
5539 }
5540 
5541 
5542 void GLAPIENTRY
_mesa_CompressedMultiTexImage2DEXT(GLenum texunit,GLenum target,GLint level,GLenum internalFormat,GLsizei width,GLsizei height,GLint border,GLsizei imageSize,const GLvoid * pixels)5543 _mesa_CompressedMultiTexImage2DEXT(GLenum texunit, GLenum target, GLint level,
5544                                    GLenum internalFormat, GLsizei width,
5545                                    GLsizei height, GLint border, GLsizei imageSize,
5546                                    const GLvoid *pixels)
5547 {
5548    struct gl_texture_object*  texObj;
5549    GET_CURRENT_CONTEXT(ctx);
5550 
5551    texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
5552                                                    texunit - GL_TEXTURE0,
5553                                                    true,
5554                                                    "glCompressedMultiTexImage2DEXT");
5555    if (!texObj)
5556       return;
5557    teximage(ctx, GL_TRUE, 2, texObj, target, level, internalFormat,
5558             width, height, 1, border, GL_NONE, GL_NONE, imageSize, pixels, false);
5559 }
5560 
5561 
5562 void GLAPIENTRY
_mesa_CompressedTexImage3D(GLenum target,GLint level,GLenum internalFormat,GLsizei width,GLsizei height,GLsizei depth,GLint border,GLsizei imageSize,const GLvoid * data)5563 _mesa_CompressedTexImage3D(GLenum target, GLint level,
5564                               GLenum internalFormat, GLsizei width,
5565                               GLsizei height, GLsizei depth, GLint border,
5566                               GLsizei imageSize, const GLvoid *data)
5567 {
5568    GET_CURRENT_CONTEXT(ctx);
5569    teximage_err(ctx, GL_TRUE, 3, target, level, internalFormat, width, height,
5570                 depth, border, GL_NONE, GL_NONE, imageSize, data);
5571 }
5572 
5573 
5574 void GLAPIENTRY
_mesa_CompressedTextureImage3DEXT(GLuint texture,GLenum target,GLint level,GLenum internalFormat,GLsizei width,GLsizei height,GLsizei depth,GLint border,GLsizei imageSize,const GLvoid * pixels)5575 _mesa_CompressedTextureImage3DEXT(GLuint texture, GLenum target, GLint level,
5576                                   GLenum internalFormat, GLsizei width,
5577                                   GLsizei height, GLsizei depth, GLint border,
5578                                   GLsizei imageSize, const GLvoid *pixels)
5579 {
5580    struct gl_texture_object*  texObj;
5581    GET_CURRENT_CONTEXT(ctx);
5582 
5583    texObj = _mesa_lookup_or_create_texture(ctx, target, texture, false, true,
5584                                            "glCompressedTextureImage3DEXT");
5585    if (!texObj)
5586       return;
5587    teximage(ctx, GL_TRUE, 3, texObj, target, level, internalFormat,
5588             width, height, depth, border, GL_NONE, GL_NONE, imageSize, pixels, false);
5589 }
5590 
5591 
5592 void GLAPIENTRY
_mesa_CompressedMultiTexImage3DEXT(GLenum texunit,GLenum target,GLint level,GLenum internalFormat,GLsizei width,GLsizei height,GLsizei depth,GLint border,GLsizei imageSize,const GLvoid * pixels)5593 _mesa_CompressedMultiTexImage3DEXT(GLenum texunit, GLenum target, GLint level,
5594                                    GLenum internalFormat, GLsizei width,
5595                                    GLsizei height, GLsizei depth, GLint border,
5596                                    GLsizei imageSize, const GLvoid *pixels)
5597 {
5598    struct gl_texture_object*  texObj;
5599    GET_CURRENT_CONTEXT(ctx);
5600 
5601    texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
5602                                                    texunit - GL_TEXTURE0,
5603                                                    true,
5604                                                    "glCompressedMultiTexImage3DEXT");
5605    if (!texObj)
5606       return;
5607    teximage(ctx, GL_TRUE, 3, texObj, target, level, internalFormat,
5608             width, height, depth, border, GL_NONE, GL_NONE, imageSize, pixels, false);
5609 }
5610 
5611 
5612 void GLAPIENTRY
_mesa_CompressedTexImage1D_no_error(GLenum target,GLint level,GLenum internalFormat,GLsizei width,GLint border,GLsizei imageSize,const GLvoid * data)5613 _mesa_CompressedTexImage1D_no_error(GLenum target, GLint level,
5614                                     GLenum internalFormat, GLsizei width,
5615                                     GLint border, GLsizei imageSize,
5616                                     const GLvoid *data)
5617 {
5618    GET_CURRENT_CONTEXT(ctx);
5619    teximage_no_error(ctx, GL_TRUE, 1, target, level, internalFormat, width, 1,
5620                      1, border, GL_NONE, GL_NONE, imageSize, data);
5621 }
5622 
5623 
5624 void GLAPIENTRY
_mesa_CompressedTexImage2D_no_error(GLenum target,GLint level,GLenum internalFormat,GLsizei width,GLsizei height,GLint border,GLsizei imageSize,const GLvoid * data)5625 _mesa_CompressedTexImage2D_no_error(GLenum target, GLint level,
5626                                     GLenum internalFormat, GLsizei width,
5627                                     GLsizei height, GLint border,
5628                                     GLsizei imageSize, const GLvoid *data)
5629 {
5630    GET_CURRENT_CONTEXT(ctx);
5631    teximage_no_error(ctx, GL_TRUE, 2, target, level, internalFormat, width,
5632                      height, 1, border, GL_NONE, GL_NONE, imageSize, data);
5633 }
5634 
5635 
5636 void GLAPIENTRY
_mesa_CompressedTexImage3D_no_error(GLenum target,GLint level,GLenum internalFormat,GLsizei width,GLsizei height,GLsizei depth,GLint border,GLsizei imageSize,const GLvoid * data)5637 _mesa_CompressedTexImage3D_no_error(GLenum target, GLint level,
5638                                     GLenum internalFormat, GLsizei width,
5639                                     GLsizei height, GLsizei depth, GLint border,
5640                                     GLsizei imageSize, const GLvoid *data)
5641 {
5642    GET_CURRENT_CONTEXT(ctx);
5643    teximage_no_error(ctx, GL_TRUE, 3, target, level, internalFormat, width,
5644                      height, depth, border, GL_NONE, GL_NONE, imageSize, data);
5645 }
5646 
5647 
5648 /**
5649  * Common helper for glCompressedTexSubImage1/2/3D() and
5650  * glCompressedTextureSubImage1/2/3D().
5651  */
5652 static void
compressed_texture_sub_image(struct gl_context * ctx,GLuint dims,struct gl_texture_object * texObj,struct gl_texture_image * texImage,GLenum target,GLint level,GLint xoffset,GLint yoffset,GLint zoffset,GLsizei width,GLsizei height,GLsizei depth,GLenum format,GLsizei imageSize,const GLvoid * data)5653 compressed_texture_sub_image(struct gl_context *ctx, GLuint dims,
5654                              struct gl_texture_object *texObj,
5655                              struct gl_texture_image *texImage,
5656                              GLenum target, GLint level, GLint xoffset,
5657                              GLint yoffset, GLint zoffset, GLsizei width,
5658                              GLsizei height, GLsizei depth, GLenum format,
5659                              GLsizei imageSize, const GLvoid *data)
5660 {
5661    FLUSH_VERTICES(ctx, 0);
5662 
5663    _mesa_lock_texture(ctx, texObj);
5664    {
5665       if (width > 0 && height > 0 && depth > 0) {
5666          ctx->Driver.CompressedTexSubImage(ctx, dims, texImage,
5667                                            xoffset, yoffset, zoffset,
5668                                            width, height, depth,
5669                                            format, imageSize, data);
5670 
5671          check_gen_mipmap(ctx, target, texObj, level);
5672 
5673          /* NOTE: Don't signal _NEW_TEXTURE_OBJECT since we've only changed
5674           * the texel data, not the texture format, size, etc.
5675           */
5676       }
5677    }
5678    _mesa_unlock_texture(ctx, texObj);
5679 }
5680 
5681 
5682 enum tex_mode {
5683    /* Use bound texture to current unit */
5684    TEX_MODE_CURRENT_NO_ERROR = 0,
5685    TEX_MODE_CURRENT_ERROR,
5686    /* Use the specified texture name */
5687    TEX_MODE_DSA_NO_ERROR,
5688    TEX_MODE_DSA_ERROR,
5689    /* Use the specified texture name + target */
5690    TEX_MODE_EXT_DSA_TEXTURE,
5691    /* Use the specified texture unit + target */
5692    TEX_MODE_EXT_DSA_TEXUNIT,
5693 };
5694 
5695 
5696 static void
compressed_tex_sub_image(unsigned dim,GLenum target,GLuint textureOrIndex,GLint level,GLint xoffset,GLint yoffset,GLint zoffset,GLsizei width,GLsizei height,GLsizei depth,GLenum format,GLsizei imageSize,const GLvoid * data,enum tex_mode mode,const char * caller)5697 compressed_tex_sub_image(unsigned dim, GLenum target, GLuint textureOrIndex,
5698                          GLint level, GLint xoffset, GLint yoffset,
5699                          GLint zoffset, GLsizei width, GLsizei height,
5700                          GLsizei depth, GLenum format, GLsizei imageSize,
5701                          const GLvoid *data, enum tex_mode mode,
5702                          const char *caller)
5703 {
5704    struct gl_texture_object *texObj = NULL;
5705    struct gl_texture_image *texImage;
5706    bool no_error = false;
5707    GET_CURRENT_CONTEXT(ctx);
5708 
5709    switch (mode) {
5710       case TEX_MODE_DSA_ERROR:
5711          assert(target == 0);
5712          texObj = _mesa_lookup_texture_err(ctx, textureOrIndex, caller);
5713          if (texObj)
5714             target = texObj->Target;
5715          break;
5716       case TEX_MODE_DSA_NO_ERROR:
5717          assert(target == 0);
5718          texObj = _mesa_lookup_texture(ctx, textureOrIndex);
5719          if (texObj)
5720             target = texObj->Target;
5721          no_error = true;
5722          break;
5723       case TEX_MODE_EXT_DSA_TEXTURE:
5724          texObj = _mesa_lookup_or_create_texture(ctx, target, textureOrIndex,
5725                                                  false, true, caller);
5726          break;
5727       case TEX_MODE_EXT_DSA_TEXUNIT:
5728          texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
5729                                                          textureOrIndex,
5730                                                          false,
5731                                                          caller);
5732          break;
5733       case TEX_MODE_CURRENT_NO_ERROR:
5734          no_error = true;
5735          /* fallthrough */
5736       case TEX_MODE_CURRENT_ERROR:
5737       default:
5738          assert(textureOrIndex == 0);
5739          break;
5740    }
5741 
5742    if (!no_error &&
5743        compressed_subtexture_target_check(ctx, target, dim, format,
5744                                           mode == TEX_MODE_DSA_ERROR,
5745                                           caller)) {
5746       return;
5747    }
5748 
5749    if (mode == TEX_MODE_CURRENT_NO_ERROR ||
5750        mode == TEX_MODE_CURRENT_ERROR) {
5751       texObj = _mesa_get_current_tex_object(ctx, target);
5752    }
5753 
5754    if (!texObj)
5755       return;
5756 
5757    if (!no_error &&
5758        compressed_subtexture_error_check(ctx, dim, texObj, target, level,
5759                                          xoffset, yoffset, zoffset, width,
5760                                          height, depth, format,
5761                                          imageSize, data, caller)) {
5762       return;
5763    }
5764 
5765    /* Must handle special case GL_TEXTURE_CUBE_MAP. */
5766    if (dim == 3 &&
5767        (mode == TEX_MODE_DSA_ERROR || mode == TEX_MODE_DSA_NO_ERROR) &&
5768        texObj->Target == GL_TEXTURE_CUBE_MAP) {
5769       const char *pixels = data;
5770       GLint image_stride;
5771 
5772       /* Make sure the texture object is a proper cube.
5773        * (See texturesubimage in teximage.c for details on why this check is
5774        * performed.)
5775        */
5776       if (!no_error && !_mesa_cube_level_complete(texObj, level)) {
5777          _mesa_error(ctx, GL_INVALID_OPERATION,
5778                      "glCompressedTextureSubImage3D(cube map incomplete)");
5779          return;
5780       }
5781 
5782       /* Copy in each face. */
5783       for (int i = zoffset; i < zoffset + depth; ++i) {
5784          texImage = texObj->Image[i][level];
5785          assert(texImage);
5786 
5787          compressed_texture_sub_image(ctx, 3, texObj, texImage,
5788                                       texObj->Target, level, xoffset, yoffset,
5789                                       0, width, height, 1, format,
5790                                       imageSize, pixels);
5791 
5792          /* Compressed images don't have a client format */
5793          image_stride = _mesa_format_image_size(texImage->TexFormat,
5794                                                 texImage->Width,
5795                                                 texImage->Height, 1);
5796 
5797          pixels += image_stride;
5798          imageSize -= image_stride;
5799       }
5800    } else {
5801       texImage = _mesa_select_tex_image(texObj, target, level);
5802       assert(texImage);
5803 
5804       compressed_texture_sub_image(ctx, dim, texObj, texImage, target, level,
5805                                    xoffset, yoffset, zoffset, width, height,
5806                                    depth, format, imageSize, data);
5807    }
5808 }
5809 
5810 
5811 void GLAPIENTRY
_mesa_CompressedTexSubImage1D_no_error(GLenum target,GLint level,GLint xoffset,GLsizei width,GLenum format,GLsizei imageSize,const GLvoid * data)5812 _mesa_CompressedTexSubImage1D_no_error(GLenum target, GLint level,
5813                                        GLint xoffset, GLsizei width,
5814                                        GLenum format, GLsizei imageSize,
5815                                        const GLvoid *data)
5816 {
5817    compressed_tex_sub_image(1, target, 0,
5818                             level, xoffset, 0, 0, width,
5819                             1, 1, format, imageSize, data,
5820                             TEX_MODE_CURRENT_NO_ERROR,
5821                             "glCompressedTexSubImage1D");
5822 }
5823 
5824 
5825 void GLAPIENTRY
_mesa_CompressedTexSubImage1D(GLenum target,GLint level,GLint xoffset,GLsizei width,GLenum format,GLsizei imageSize,const GLvoid * data)5826 _mesa_CompressedTexSubImage1D(GLenum target, GLint level, GLint xoffset,
5827                               GLsizei width, GLenum format,
5828                               GLsizei imageSize, const GLvoid *data)
5829 {
5830    compressed_tex_sub_image(1, target, 0,
5831                             level, xoffset, 0, 0, width,
5832                             1, 1, format, imageSize, data,
5833                             TEX_MODE_CURRENT_ERROR,
5834                             "glCompressedTexSubImage1D");
5835 }
5836 
5837 
5838 void GLAPIENTRY
_mesa_CompressedTextureSubImage1D_no_error(GLuint texture,GLint level,GLint xoffset,GLsizei width,GLenum format,GLsizei imageSize,const GLvoid * data)5839 _mesa_CompressedTextureSubImage1D_no_error(GLuint texture, GLint level,
5840                                            GLint xoffset, GLsizei width,
5841                                            GLenum format, GLsizei imageSize,
5842                                            const GLvoid *data)
5843 {
5844    compressed_tex_sub_image(1, 0, texture,
5845                             level, xoffset, 0, 0,
5846                             width, 1, 1, format, imageSize, data,
5847                             TEX_MODE_DSA_NO_ERROR,
5848                             "glCompressedTextureSubImage1D");
5849 }
5850 
5851 
5852 void GLAPIENTRY
_mesa_CompressedTextureSubImage1D(GLuint texture,GLint level,GLint xoffset,GLsizei width,GLenum format,GLsizei imageSize,const GLvoid * data)5853 _mesa_CompressedTextureSubImage1D(GLuint texture, GLint level, GLint xoffset,
5854                                   GLsizei width, GLenum format,
5855                                   GLsizei imageSize, const GLvoid *data)
5856 {
5857    compressed_tex_sub_image(1, 0, texture,
5858                             level, xoffset, 0, 0,
5859                             width, 1, 1, format, imageSize, data,
5860                             TEX_MODE_DSA_ERROR,
5861                             "glCompressedTextureSubImage1D");
5862 }
5863 
5864 
5865 void GLAPIENTRY
_mesa_CompressedTextureSubImage1DEXT(GLuint texture,GLenum target,GLint level,GLint xoffset,GLsizei width,GLenum format,GLsizei imageSize,const GLvoid * data)5866 _mesa_CompressedTextureSubImage1DEXT(GLuint texture, GLenum target,
5867                                      GLint level, GLint xoffset,
5868                                      GLsizei width, GLenum format,
5869                                      GLsizei imageSize, const GLvoid *data)
5870 {
5871    compressed_tex_sub_image(1, target, texture, level, xoffset, 0,
5872                             0, width, 1, 1, format, imageSize,
5873                             data,
5874                             TEX_MODE_EXT_DSA_TEXTURE,
5875                             "glCompressedTextureSubImage1DEXT");
5876 }
5877 
5878 
5879 void GLAPIENTRY
_mesa_CompressedMultiTexSubImage1DEXT(GLenum texunit,GLenum target,GLint level,GLint xoffset,GLsizei width,GLenum format,GLsizei imageSize,const GLvoid * data)5880 _mesa_CompressedMultiTexSubImage1DEXT(GLenum texunit, GLenum target,
5881                                       GLint level, GLint xoffset,
5882                                       GLsizei width, GLenum format,
5883                                       GLsizei imageSize, const GLvoid *data)
5884 {
5885    compressed_tex_sub_image(1, target, texunit - GL_TEXTURE0, level,
5886                             xoffset, 0, 0, width, 1, 1, format, imageSize,
5887                             data,
5888                             TEX_MODE_EXT_DSA_TEXUNIT,
5889                             "glCompressedMultiTexSubImage1DEXT");
5890 }
5891 
5892 
5893 void GLAPIENTRY
_mesa_CompressedTexSubImage2D_no_error(GLenum target,GLint level,GLint xoffset,GLint yoffset,GLsizei width,GLsizei height,GLenum format,GLsizei imageSize,const GLvoid * data)5894 _mesa_CompressedTexSubImage2D_no_error(GLenum target, GLint level,
5895                                        GLint xoffset, GLint yoffset,
5896                                        GLsizei width, GLsizei height,
5897                                        GLenum format, GLsizei imageSize,
5898                                        const GLvoid *data)
5899 {
5900    compressed_tex_sub_image(2, target, 0, level,
5901                             xoffset, yoffset, 0,
5902                             width, height, 1, format, imageSize, data,
5903                             TEX_MODE_CURRENT_NO_ERROR,
5904                             "glCompressedTexSubImage2D");
5905 }
5906 
5907 
5908 void GLAPIENTRY
_mesa_CompressedTexSubImage2D(GLenum target,GLint level,GLint xoffset,GLint yoffset,GLsizei width,GLsizei height,GLenum format,GLsizei imageSize,const GLvoid * data)5909 _mesa_CompressedTexSubImage2D(GLenum target, GLint level, GLint xoffset,
5910                               GLint yoffset, GLsizei width, GLsizei height,
5911                               GLenum format, GLsizei imageSize,
5912                               const GLvoid *data)
5913 {
5914    compressed_tex_sub_image(2, target, 0, level,
5915                             xoffset, yoffset, 0,
5916                             width, height, 1, format, imageSize, data,
5917                             TEX_MODE_CURRENT_ERROR,
5918                             "glCompressedTexSubImage2D");
5919 }
5920 
5921 
5922 void GLAPIENTRY
_mesa_CompressedTextureSubImage2DEXT(GLuint texture,GLenum target,GLint level,GLint xoffset,GLint yoffset,GLsizei width,GLsizei height,GLenum format,GLsizei imageSize,const GLvoid * data)5923 _mesa_CompressedTextureSubImage2DEXT(GLuint texture, GLenum target,
5924                                      GLint level, GLint xoffset,
5925                                      GLint yoffset, GLsizei width,
5926                                      GLsizei height, GLenum format,
5927                                      GLsizei imageSize, const GLvoid *data)
5928 {
5929    compressed_tex_sub_image(2, target, texture, level, xoffset,
5930                             yoffset, 0, width, height, 1, format,
5931                             imageSize, data,
5932                             TEX_MODE_EXT_DSA_TEXTURE,
5933                             "glCompressedTextureSubImage2DEXT");
5934 }
5935 
5936 
5937 void GLAPIENTRY
_mesa_CompressedMultiTexSubImage2DEXT(GLenum texunit,GLenum target,GLint level,GLint xoffset,GLint yoffset,GLsizei width,GLsizei height,GLenum format,GLsizei imageSize,const GLvoid * data)5938 _mesa_CompressedMultiTexSubImage2DEXT(GLenum texunit, GLenum target,
5939                                       GLint level, GLint xoffset, GLint yoffset,
5940                                       GLsizei width, GLsizei height, GLenum format,
5941                                       GLsizei imageSize, const GLvoid *data)
5942 {
5943    compressed_tex_sub_image(2, target, texunit - GL_TEXTURE0, level,
5944                             xoffset, yoffset, 0, width, height, 1, format,
5945                             imageSize, data,
5946                             TEX_MODE_EXT_DSA_TEXUNIT,
5947                             "glCompressedMultiTexSubImage2DEXT");
5948 }
5949 
5950 
5951 void GLAPIENTRY
_mesa_CompressedTextureSubImage2D_no_error(GLuint texture,GLint level,GLint xoffset,GLint yoffset,GLsizei width,GLsizei height,GLenum format,GLsizei imageSize,const GLvoid * data)5952 _mesa_CompressedTextureSubImage2D_no_error(GLuint texture, GLint level,
5953                                            GLint xoffset, GLint yoffset,
5954                                            GLsizei width, GLsizei height,
5955                                            GLenum format, GLsizei imageSize,
5956                                            const GLvoid *data)
5957 {
5958    compressed_tex_sub_image(2, 0, texture, level, xoffset, yoffset, 0,
5959                             width, height, 1, format, imageSize, data,
5960                             TEX_MODE_DSA_NO_ERROR,
5961                             "glCompressedTextureSubImage2D");
5962 }
5963 
5964 
5965 void GLAPIENTRY
_mesa_CompressedTextureSubImage2D(GLuint texture,GLint level,GLint xoffset,GLint yoffset,GLsizei width,GLsizei height,GLenum format,GLsizei imageSize,const GLvoid * data)5966 _mesa_CompressedTextureSubImage2D(GLuint texture, GLint level, GLint xoffset,
5967                                   GLint yoffset,
5968                                   GLsizei width, GLsizei height,
5969                                   GLenum format, GLsizei imageSize,
5970                                   const GLvoid *data)
5971 {
5972    compressed_tex_sub_image(2, 0, texture, level, xoffset, yoffset, 0,
5973                             width, height, 1, format, imageSize, data,
5974                             TEX_MODE_DSA_ERROR,
5975                             "glCompressedTextureSubImage2D");
5976 }
5977 
5978 void GLAPIENTRY
_mesa_CompressedTexSubImage3D_no_error(GLenum target,GLint level,GLint xoffset,GLint yoffset,GLint zoffset,GLsizei width,GLsizei height,GLsizei depth,GLenum format,GLsizei imageSize,const GLvoid * data)5979 _mesa_CompressedTexSubImage3D_no_error(GLenum target, GLint level,
5980                                        GLint xoffset, GLint yoffset,
5981                                        GLint zoffset, GLsizei width,
5982                                        GLsizei height, GLsizei depth,
5983                                        GLenum format, GLsizei imageSize,
5984                                        const GLvoid *data)
5985 {
5986    compressed_tex_sub_image(3, target, 0, level, xoffset, yoffset,
5987                             zoffset, width, height, depth, format,
5988                             imageSize, data,
5989                             TEX_MODE_CURRENT_NO_ERROR,
5990                             "glCompressedTexSubImage3D");
5991 }
5992 
5993 void GLAPIENTRY
_mesa_CompressedTexSubImage3D(GLenum target,GLint level,GLint xoffset,GLint yoffset,GLint zoffset,GLsizei width,GLsizei height,GLsizei depth,GLenum format,GLsizei imageSize,const GLvoid * data)5994 _mesa_CompressedTexSubImage3D(GLenum target, GLint level, GLint xoffset,
5995                               GLint yoffset, GLint zoffset, GLsizei width,
5996                               GLsizei height, GLsizei depth, GLenum format,
5997                               GLsizei imageSize, const GLvoid *data)
5998 {
5999    compressed_tex_sub_image(3, target, 0, level, xoffset, yoffset,
6000                             zoffset, width, height, depth, format,
6001                             imageSize, data,
6002                             TEX_MODE_CURRENT_ERROR,
6003                             "glCompressedTexSubImage3D");
6004 }
6005 
6006 void GLAPIENTRY
_mesa_CompressedTextureSubImage3D_no_error(GLuint texture,GLint level,GLint xoffset,GLint yoffset,GLint zoffset,GLsizei width,GLsizei height,GLsizei depth,GLenum format,GLsizei imageSize,const GLvoid * data)6007 _mesa_CompressedTextureSubImage3D_no_error(GLuint texture, GLint level,
6008                                            GLint xoffset, GLint yoffset,
6009                                            GLint zoffset, GLsizei width,
6010                                            GLsizei height, GLsizei depth,
6011                                            GLenum format, GLsizei imageSize,
6012                                            const GLvoid *data)
6013 {
6014    compressed_tex_sub_image(3, 0, texture, level, xoffset, yoffset,
6015                             zoffset, width, height, depth, format,
6016                             imageSize, data,
6017                             TEX_MODE_DSA_NO_ERROR,
6018                             "glCompressedTextureSubImage3D");
6019 }
6020 
6021 void GLAPIENTRY
_mesa_CompressedTextureSubImage3D(GLuint texture,GLint level,GLint xoffset,GLint yoffset,GLint zoffset,GLsizei width,GLsizei height,GLsizei depth,GLenum format,GLsizei imageSize,const GLvoid * data)6022 _mesa_CompressedTextureSubImage3D(GLuint texture, GLint level, GLint xoffset,
6023                                   GLint yoffset, GLint zoffset, GLsizei width,
6024                                   GLsizei height, GLsizei depth,
6025                                   GLenum format, GLsizei imageSize,
6026                                   const GLvoid *data)
6027 {
6028    compressed_tex_sub_image(3, 0, texture, level, xoffset, yoffset,
6029                             zoffset, width, height, depth, format,
6030                             imageSize, data,
6031                             TEX_MODE_DSA_ERROR,
6032                             "glCompressedTextureSubImage3D");
6033 }
6034 
6035 
6036 void GLAPIENTRY
_mesa_CompressedTextureSubImage3DEXT(GLuint texture,GLenum target,GLint level,GLint xoffset,GLint yoffset,GLint zoffset,GLsizei width,GLsizei height,GLsizei depth,GLenum format,GLsizei imageSize,const GLvoid * data)6037 _mesa_CompressedTextureSubImage3DEXT(GLuint texture, GLenum target,
6038                                      GLint level, GLint xoffset,
6039                                      GLint yoffset, GLint zoffset,
6040                                      GLsizei width, GLsizei height,
6041                                      GLsizei depth, GLenum format,
6042                                      GLsizei imageSize, const GLvoid *data)
6043 {
6044    compressed_tex_sub_image(3, target, texture, level, xoffset, yoffset,
6045                             zoffset, width, height, depth, format,
6046                             imageSize, data,
6047                             TEX_MODE_EXT_DSA_TEXTURE,
6048                             "glCompressedTextureSubImage3DEXT");
6049 }
6050 
6051 
6052 void GLAPIENTRY
_mesa_CompressedMultiTexSubImage3DEXT(GLenum texunit,GLenum target,GLint level,GLint xoffset,GLint yoffset,GLint zoffset,GLsizei width,GLsizei height,GLsizei depth,GLenum format,GLsizei imageSize,const GLvoid * data)6053 _mesa_CompressedMultiTexSubImage3DEXT(GLenum texunit, GLenum target,
6054                                       GLint level, GLint xoffset, GLint yoffset,
6055                                       GLint zoffset, GLsizei width, GLsizei height,
6056                                       GLsizei depth, GLenum format,
6057                                       GLsizei imageSize, const GLvoid *data)
6058 {
6059    compressed_tex_sub_image(3, target, texunit - GL_TEXTURE0, level,
6060                             xoffset, yoffset, zoffset, width, height, depth,
6061                             format, imageSize, data,
6062                             TEX_MODE_EXT_DSA_TEXUNIT,
6063                             "glCompressedMultiTexSubImage3DEXT");
6064 }
6065 
6066 
6067 mesa_format
_mesa_get_texbuffer_format(const struct gl_context * ctx,GLenum internalFormat)6068 _mesa_get_texbuffer_format(const struct gl_context *ctx, GLenum internalFormat)
6069 {
6070    if (ctx->API == API_OPENGL_COMPAT) {
6071       switch (internalFormat) {
6072       case GL_ALPHA8:
6073          return MESA_FORMAT_A_UNORM8;
6074       case GL_ALPHA16:
6075          return MESA_FORMAT_A_UNORM16;
6076       case GL_ALPHA16F_ARB:
6077          return MESA_FORMAT_A_FLOAT16;
6078       case GL_ALPHA32F_ARB:
6079          return MESA_FORMAT_A_FLOAT32;
6080       case GL_ALPHA8I_EXT:
6081          return MESA_FORMAT_A_SINT8;
6082       case GL_ALPHA16I_EXT:
6083          return MESA_FORMAT_A_SINT16;
6084       case GL_ALPHA32I_EXT:
6085          return MESA_FORMAT_A_SINT32;
6086       case GL_ALPHA8UI_EXT:
6087          return MESA_FORMAT_A_UINT8;
6088       case GL_ALPHA16UI_EXT:
6089          return MESA_FORMAT_A_UINT16;
6090       case GL_ALPHA32UI_EXT:
6091          return MESA_FORMAT_A_UINT32;
6092       case GL_LUMINANCE8:
6093          return MESA_FORMAT_L_UNORM8;
6094       case GL_LUMINANCE16:
6095          return MESA_FORMAT_L_UNORM16;
6096       case GL_LUMINANCE16F_ARB:
6097          return MESA_FORMAT_L_FLOAT16;
6098       case GL_LUMINANCE32F_ARB:
6099          return MESA_FORMAT_L_FLOAT32;
6100       case GL_LUMINANCE8I_EXT:
6101          return MESA_FORMAT_L_SINT8;
6102       case GL_LUMINANCE16I_EXT:
6103          return MESA_FORMAT_L_SINT16;
6104       case GL_LUMINANCE32I_EXT:
6105          return MESA_FORMAT_L_SINT32;
6106       case GL_LUMINANCE8UI_EXT:
6107          return MESA_FORMAT_L_UINT8;
6108       case GL_LUMINANCE16UI_EXT:
6109          return MESA_FORMAT_L_UINT16;
6110       case GL_LUMINANCE32UI_EXT:
6111          return MESA_FORMAT_L_UINT32;
6112       case GL_LUMINANCE8_ALPHA8:
6113          return MESA_FORMAT_LA_UNORM8;
6114       case GL_LUMINANCE16_ALPHA16:
6115          return MESA_FORMAT_LA_UNORM16;
6116       case GL_LUMINANCE_ALPHA16F_ARB:
6117          return MESA_FORMAT_LA_FLOAT16;
6118       case GL_LUMINANCE_ALPHA32F_ARB:
6119          return MESA_FORMAT_LA_FLOAT32;
6120       case GL_LUMINANCE_ALPHA8I_EXT:
6121          return MESA_FORMAT_LA_SINT8;
6122       case GL_LUMINANCE_ALPHA16I_EXT:
6123          return MESA_FORMAT_LA_SINT16;
6124       case GL_LUMINANCE_ALPHA32I_EXT:
6125          return MESA_FORMAT_LA_SINT32;
6126       case GL_LUMINANCE_ALPHA8UI_EXT:
6127          return MESA_FORMAT_LA_UINT8;
6128       case GL_LUMINANCE_ALPHA16UI_EXT:
6129          return MESA_FORMAT_LA_UINT16;
6130       case GL_LUMINANCE_ALPHA32UI_EXT:
6131          return MESA_FORMAT_LA_UINT32;
6132       case GL_INTENSITY8:
6133          return MESA_FORMAT_I_UNORM8;
6134       case GL_INTENSITY16:
6135          return MESA_FORMAT_I_UNORM16;
6136       case GL_INTENSITY16F_ARB:
6137          return MESA_FORMAT_I_FLOAT16;
6138       case GL_INTENSITY32F_ARB:
6139          return MESA_FORMAT_I_FLOAT32;
6140       case GL_INTENSITY8I_EXT:
6141          return MESA_FORMAT_I_SINT8;
6142       case GL_INTENSITY16I_EXT:
6143          return MESA_FORMAT_I_SINT16;
6144       case GL_INTENSITY32I_EXT:
6145          return MESA_FORMAT_I_SINT32;
6146       case GL_INTENSITY8UI_EXT:
6147          return MESA_FORMAT_I_UINT8;
6148       case GL_INTENSITY16UI_EXT:
6149          return MESA_FORMAT_I_UINT16;
6150       case GL_INTENSITY32UI_EXT:
6151          return MESA_FORMAT_I_UINT32;
6152       default:
6153          break;
6154       }
6155    }
6156 
6157    if (_mesa_has_ARB_texture_buffer_object_rgb32(ctx) ||
6158        _mesa_has_OES_texture_buffer(ctx)) {
6159       switch (internalFormat) {
6160       case GL_RGB32F:
6161          return MESA_FORMAT_RGB_FLOAT32;
6162       case GL_RGB32UI:
6163          return MESA_FORMAT_RGB_UINT32;
6164       case GL_RGB32I:
6165          return MESA_FORMAT_RGB_SINT32;
6166       default:
6167          break;
6168       }
6169    }
6170 
6171    switch (internalFormat) {
6172    case GL_RGBA8:
6173       return MESA_FORMAT_R8G8B8A8_UNORM;
6174    case GL_RGBA16:
6175       if (_mesa_is_gles(ctx) && !_mesa_has_EXT_texture_norm16(ctx))
6176          return MESA_FORMAT_NONE;
6177       return MESA_FORMAT_RGBA_UNORM16;
6178    case GL_RGBA16F_ARB:
6179       return MESA_FORMAT_RGBA_FLOAT16;
6180    case GL_RGBA32F_ARB:
6181       return MESA_FORMAT_RGBA_FLOAT32;
6182    case GL_RGBA8I_EXT:
6183       return MESA_FORMAT_RGBA_SINT8;
6184    case GL_RGBA16I_EXT:
6185       return MESA_FORMAT_RGBA_SINT16;
6186    case GL_RGBA32I_EXT:
6187       return MESA_FORMAT_RGBA_SINT32;
6188    case GL_RGBA8UI_EXT:
6189       return MESA_FORMAT_RGBA_UINT8;
6190    case GL_RGBA16UI_EXT:
6191       return MESA_FORMAT_RGBA_UINT16;
6192    case GL_RGBA32UI_EXT:
6193       return MESA_FORMAT_RGBA_UINT32;
6194 
6195    case GL_RG8:
6196       return MESA_FORMAT_RG_UNORM8;
6197    case GL_RG16:
6198       if (_mesa_is_gles(ctx) && !_mesa_has_EXT_texture_norm16(ctx))
6199          return MESA_FORMAT_NONE;
6200       return MESA_FORMAT_RG_UNORM16;
6201    case GL_RG16F:
6202       return MESA_FORMAT_RG_FLOAT16;
6203    case GL_RG32F:
6204       return MESA_FORMAT_RG_FLOAT32;
6205    case GL_RG8I:
6206       return MESA_FORMAT_RG_SINT8;
6207    case GL_RG16I:
6208       return MESA_FORMAT_RG_SINT16;
6209    case GL_RG32I:
6210       return MESA_FORMAT_RG_SINT32;
6211    case GL_RG8UI:
6212       return MESA_FORMAT_RG_UINT8;
6213    case GL_RG16UI:
6214       return MESA_FORMAT_RG_UINT16;
6215    case GL_RG32UI:
6216       return MESA_FORMAT_RG_UINT32;
6217 
6218    case GL_R8:
6219       return MESA_FORMAT_R_UNORM8;
6220    case GL_R16:
6221       if (_mesa_is_gles(ctx) && !_mesa_has_EXT_texture_norm16(ctx))
6222          return MESA_FORMAT_NONE;
6223       return MESA_FORMAT_R_UNORM16;
6224    case GL_R16F:
6225       return MESA_FORMAT_R_FLOAT16;
6226    case GL_R32F:
6227       return MESA_FORMAT_R_FLOAT32;
6228    case GL_R8I:
6229       return MESA_FORMAT_R_SINT8;
6230    case GL_R16I:
6231       return MESA_FORMAT_R_SINT16;
6232    case GL_R32I:
6233       return MESA_FORMAT_R_SINT32;
6234    case GL_R8UI:
6235       return MESA_FORMAT_R_UINT8;
6236    case GL_R16UI:
6237       return MESA_FORMAT_R_UINT16;
6238    case GL_R32UI:
6239       return MESA_FORMAT_R_UINT32;
6240 
6241    default:
6242       return MESA_FORMAT_NONE;
6243    }
6244 }
6245 
6246 
6247 mesa_format
_mesa_validate_texbuffer_format(const struct gl_context * ctx,GLenum internalFormat)6248 _mesa_validate_texbuffer_format(const struct gl_context *ctx,
6249                                 GLenum internalFormat)
6250 {
6251    mesa_format format = _mesa_get_texbuffer_format(ctx, internalFormat);
6252    GLenum datatype;
6253 
6254    if (format == MESA_FORMAT_NONE)
6255       return MESA_FORMAT_NONE;
6256 
6257    datatype = _mesa_get_format_datatype(format);
6258 
6259    /* The GL_ARB_texture_buffer_object spec says:
6260     *
6261     *     "If ARB_texture_float is not supported, references to the
6262     *     floating-point internal formats provided by that extension should be
6263     *     removed, and such formats may not be passed to TexBufferARB."
6264     *
6265     * As a result, GL_HALF_FLOAT internal format depends on both
6266     * GL_ARB_texture_float and GL_ARB_half_float_pixel.
6267     */
6268    if ((datatype == GL_FLOAT || datatype == GL_HALF_FLOAT) &&
6269        !ctx->Extensions.ARB_texture_float)
6270       return MESA_FORMAT_NONE;
6271 
6272    if (!ctx->Extensions.ARB_texture_rg) {
6273       GLenum base_format = _mesa_get_format_base_format(format);
6274       if (base_format == GL_R || base_format == GL_RG)
6275          return MESA_FORMAT_NONE;
6276    }
6277 
6278    if (!ctx->Extensions.ARB_texture_buffer_object_rgb32) {
6279       GLenum base_format = _mesa_get_format_base_format(format);
6280       if (base_format == GL_RGB)
6281          return MESA_FORMAT_NONE;
6282    }
6283    return format;
6284 }
6285 
6286 
6287 /**
6288  * Do work common to glTexBuffer, glTexBufferRange, glTextureBuffer
6289  * and glTextureBufferRange, including some error checking.
6290  */
6291 static void
texture_buffer_range(struct gl_context * ctx,struct gl_texture_object * texObj,GLenum internalFormat,struct gl_buffer_object * bufObj,GLintptr offset,GLsizeiptr size,const char * caller)6292 texture_buffer_range(struct gl_context *ctx,
6293                      struct gl_texture_object *texObj,
6294                      GLenum internalFormat,
6295                      struct gl_buffer_object *bufObj,
6296                      GLintptr offset, GLsizeiptr size,
6297                      const char *caller)
6298 {
6299    GLintptr oldOffset = texObj->BufferOffset;
6300    GLsizeiptr oldSize = texObj->BufferSize;
6301    mesa_format format;
6302 
6303    /* NOTE: ARB_texture_buffer_object might not be supported in
6304     * the compatibility profile.
6305     */
6306    if (!_mesa_has_ARB_texture_buffer_object(ctx) &&
6307        !_mesa_has_OES_texture_buffer(ctx)) {
6308       _mesa_error(ctx, GL_INVALID_OPERATION,
6309                   "%s(ARB_texture_buffer_object is not"
6310                   " implemented for the compatibility profile)", caller);
6311       return;
6312    }
6313 
6314    if (texObj->HandleAllocated) {
6315       /* The ARB_bindless_texture spec says:
6316        *
6317        * "The error INVALID_OPERATION is generated by TexImage*, CopyTexImage*,
6318        *  CompressedTexImage*, TexBuffer*, TexParameter*, as well as other
6319        *  functions defined in terms of these, if the texture object to be
6320        *  modified is referenced by one or more texture or image handles."
6321        */
6322       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(immutable texture)", caller);
6323       return;
6324    }
6325 
6326    format = _mesa_validate_texbuffer_format(ctx, internalFormat);
6327    if (format == MESA_FORMAT_NONE) {
6328       _mesa_error(ctx, GL_INVALID_ENUM, "%s(internalFormat %s)",
6329                   caller, _mesa_enum_to_string(internalFormat));
6330       return;
6331    }
6332 
6333    FLUSH_VERTICES(ctx, 0);
6334 
6335    _mesa_lock_texture(ctx, texObj);
6336    {
6337       _mesa_reference_buffer_object(ctx, &texObj->BufferObject, bufObj);
6338       texObj->BufferObjectFormat = internalFormat;
6339       texObj->_BufferObjectFormat = format;
6340       texObj->BufferOffset = offset;
6341       texObj->BufferSize = size;
6342    }
6343    _mesa_unlock_texture(ctx, texObj);
6344 
6345    if (ctx->Driver.TexParameter) {
6346       if (offset != oldOffset) {
6347          ctx->Driver.TexParameter(ctx, texObj, GL_TEXTURE_BUFFER_OFFSET);
6348       }
6349       if (size != oldSize) {
6350          ctx->Driver.TexParameter(ctx, texObj, GL_TEXTURE_BUFFER_SIZE);
6351       }
6352    }
6353 
6354    ctx->NewDriverState |= ctx->DriverFlags.NewTextureBuffer;
6355 
6356    if (bufObj) {
6357       bufObj->UsageHistory |= USAGE_TEXTURE_BUFFER;
6358    }
6359 }
6360 
6361 
6362 /**
6363  * Make sure the texture buffer target is GL_TEXTURE_BUFFER.
6364  * Return true if it is, and return false if it is not
6365  * (and throw INVALID ENUM as dictated in the OpenGL 4.5
6366  * core spec, 02.02.2015, PDF page 245).
6367  */
6368 static bool
check_texture_buffer_target(struct gl_context * ctx,GLenum target,const char * caller,bool dsa)6369 check_texture_buffer_target(struct gl_context *ctx, GLenum target,
6370                             const char *caller, bool dsa)
6371 {
6372    if (target != GL_TEXTURE_BUFFER_ARB) {
6373       _mesa_error(ctx, dsa ? GL_INVALID_OPERATION : GL_INVALID_ENUM,
6374                   "%s(texture target is not GL_TEXTURE_BUFFER)", caller);
6375       return false;
6376    }
6377    else
6378       return true;
6379 }
6380 
6381 /**
6382  * Check for errors related to the texture buffer range.
6383  * Return false if errors are found, true if none are found.
6384  */
6385 static bool
check_texture_buffer_range(struct gl_context * ctx,struct gl_buffer_object * bufObj,GLintptr offset,GLsizeiptr size,const char * caller)6386 check_texture_buffer_range(struct gl_context *ctx,
6387                            struct gl_buffer_object *bufObj,
6388                            GLintptr offset, GLsizeiptr size,
6389                            const char *caller)
6390 {
6391    /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
6392     * Textures (PDF page 245):
6393     *    "An INVALID_VALUE error is generated if offset is negative, if
6394     *    size is less than or equal to zero, or if offset + size is greater
6395     *    than the value of BUFFER_SIZE for the buffer bound to target."
6396     */
6397    if (offset < 0) {
6398       _mesa_error(ctx, GL_INVALID_VALUE, "%s(offset=%d < 0)", caller,
6399                   (int) offset);
6400       return false;
6401    }
6402 
6403    if (size <= 0) {
6404       _mesa_error(ctx, GL_INVALID_VALUE, "%s(size=%d <= 0)", caller,
6405                   (int) size);
6406       return false;
6407    }
6408 
6409    if (offset + size > bufObj->Size) {
6410       _mesa_error(ctx, GL_INVALID_VALUE,
6411                   "%s(offset=%d + size=%d > buffer_size=%d)", caller,
6412                   (int) offset, (int) size, (int) bufObj->Size);
6413       return false;
6414    }
6415 
6416    /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
6417     * Textures (PDF page 245):
6418     *    "An INVALID_VALUE error is generated if offset is not an integer
6419     *    multiple of the value of TEXTURE_BUFFER_OFFSET_ALIGNMENT."
6420     */
6421    if (offset % ctx->Const.TextureBufferOffsetAlignment) {
6422       _mesa_error(ctx, GL_INVALID_VALUE,
6423                   "%s(invalid offset alignment)", caller);
6424       return false;
6425    }
6426 
6427    return true;
6428 }
6429 
6430 
6431 /** GL_ARB_texture_buffer_object */
6432 void GLAPIENTRY
_mesa_TexBuffer(GLenum target,GLenum internalFormat,GLuint buffer)6433 _mesa_TexBuffer(GLenum target, GLenum internalFormat, GLuint buffer)
6434 {
6435    struct gl_texture_object *texObj;
6436    struct gl_buffer_object *bufObj;
6437 
6438    GET_CURRENT_CONTEXT(ctx);
6439 
6440    /* Need to catch a bad target before it gets to
6441     * _mesa_get_current_tex_object.
6442     */
6443    if (!check_texture_buffer_target(ctx, target, "glTexBuffer", false))
6444       return;
6445 
6446    if (buffer) {
6447       bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glTexBuffer");
6448       if (!bufObj)
6449          return;
6450    } else
6451       bufObj = NULL;
6452 
6453    texObj = _mesa_get_current_tex_object(ctx, target);
6454    if (!texObj)
6455       return;
6456 
6457    texture_buffer_range(ctx, texObj, internalFormat, bufObj, 0,
6458                         buffer ? -1 : 0, "glTexBuffer");
6459 }
6460 
6461 
6462 /** GL_ARB_texture_buffer_range */
6463 void GLAPIENTRY
_mesa_TexBufferRange(GLenum target,GLenum internalFormat,GLuint buffer,GLintptr offset,GLsizeiptr size)6464 _mesa_TexBufferRange(GLenum target, GLenum internalFormat, GLuint buffer,
6465                      GLintptr offset, GLsizeiptr size)
6466 {
6467    struct gl_texture_object *texObj;
6468    struct gl_buffer_object *bufObj;
6469 
6470    GET_CURRENT_CONTEXT(ctx);
6471 
6472    /* Need to catch a bad target before it gets to
6473     * _mesa_get_current_tex_object.
6474     */
6475    if (!check_texture_buffer_target(ctx, target, "glTexBufferRange", false))
6476       return;
6477 
6478    if (buffer) {
6479       bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glTexBufferRange");
6480       if (!bufObj)
6481          return;
6482 
6483       if (!check_texture_buffer_range(ctx, bufObj, offset, size,
6484           "glTexBufferRange"))
6485          return;
6486 
6487    } else {
6488       /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
6489        * Textures (PDF page 254):
6490        *    "If buffer is zero, then any buffer object attached to the buffer
6491        *    texture is detached, the values offset and size are ignored and
6492        *    the state for offset and size for the buffer texture are reset to
6493        *    zero."
6494        */
6495       offset = 0;
6496       size = 0;
6497       bufObj = NULL;
6498    }
6499 
6500    texObj = _mesa_get_current_tex_object(ctx, target);
6501    if (!texObj)
6502       return;
6503 
6504    texture_buffer_range(ctx, texObj, internalFormat, bufObj,
6505                         offset, size, "glTexBufferRange");
6506 }
6507 
6508 
6509 /** GL_ARB_texture_buffer_range + GL_EXT_direct_state_access */
6510 void GLAPIENTRY
_mesa_TextureBufferRangeEXT(GLuint texture,GLenum target,GLenum internalFormat,GLuint buffer,GLintptr offset,GLsizeiptr size)6511 _mesa_TextureBufferRangeEXT(GLuint texture, GLenum target, GLenum internalFormat,
6512                             GLuint buffer, GLintptr offset, GLsizeiptr size)
6513 {
6514    struct gl_texture_object *texObj;
6515    struct gl_buffer_object *bufObj;
6516 
6517    GET_CURRENT_CONTEXT(ctx);
6518 
6519    texObj = _mesa_lookup_or_create_texture(ctx, target, texture, false, true,
6520                                            "glTextureBufferRangeEXT");
6521    if (!texObj)
6522       return;
6523 
6524    if (!check_texture_buffer_target(ctx, target, "glTextureBufferRangeEXT", true))
6525       return;
6526 
6527    if (buffer) {
6528       bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glTextureBufferRangeEXT");
6529       if (!bufObj)
6530          return;
6531 
6532       if (!check_texture_buffer_range(ctx, bufObj, offset, size,
6533           "glTextureBufferRangeEXT"))
6534          return;
6535 
6536    } else {
6537       /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
6538        * Textures (PDF page 254):
6539        *    "If buffer is zero, then any buffer object attached to the buffer
6540        *    texture is detached, the values offset and size are ignored and
6541        *    the state for offset and size for the buffer texture are reset to
6542        *    zero."
6543        */
6544       offset = 0;
6545       size = 0;
6546       bufObj = NULL;
6547    }
6548 
6549    texture_buffer_range(ctx, texObj, internalFormat, bufObj,
6550                         offset, size, "glTextureBufferRangeEXT");
6551 }
6552 
6553 
6554 void GLAPIENTRY
_mesa_TextureBuffer(GLuint texture,GLenum internalFormat,GLuint buffer)6555 _mesa_TextureBuffer(GLuint texture, GLenum internalFormat, GLuint buffer)
6556 {
6557    struct gl_texture_object *texObj;
6558    struct gl_buffer_object *bufObj;
6559 
6560    GET_CURRENT_CONTEXT(ctx);
6561 
6562    if (buffer) {
6563       bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glTextureBuffer");
6564       if (!bufObj)
6565          return;
6566    } else
6567       bufObj = NULL;
6568 
6569    /* Get the texture object by Name. */
6570    texObj = _mesa_lookup_texture_err(ctx, texture, "glTextureBuffer");
6571    if (!texObj)
6572       return;
6573 
6574    if (!check_texture_buffer_target(ctx, texObj->Target, "glTextureBuffer", true))
6575       return;
6576 
6577    texture_buffer_range(ctx, texObj, internalFormat,
6578                         bufObj, 0, buffer ? -1 : 0, "glTextureBuffer");
6579 }
6580 
6581 void GLAPIENTRY
_mesa_TextureBufferEXT(GLuint texture,GLenum target,GLenum internalFormat,GLuint buffer)6582 _mesa_TextureBufferEXT(GLuint texture, GLenum target,
6583                        GLenum internalFormat, GLuint buffer)
6584 {
6585    struct gl_texture_object *texObj;
6586    struct gl_buffer_object *bufObj;
6587 
6588    GET_CURRENT_CONTEXT(ctx);
6589 
6590    if (buffer) {
6591       bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glTextureBuffer");
6592       if (!bufObj)
6593          return;
6594    } else
6595       bufObj = NULL;
6596 
6597    /* Get the texture object by Name. */
6598    texObj = _mesa_lookup_or_create_texture(ctx, target, texture,
6599                                            false, true,
6600                                            "glTextureBufferEXT");
6601 
6602    if (!texObj ||
6603        !check_texture_buffer_target(ctx, texObj->Target, "glTextureBufferEXT", true))
6604       return;
6605 
6606    texture_buffer_range(ctx, texObj, internalFormat,
6607                         bufObj, 0, buffer ? -1 : 0, "glTextureBufferEXT");
6608 }
6609 
6610 void GLAPIENTRY
_mesa_MultiTexBufferEXT(GLenum texunit,GLenum target,GLenum internalFormat,GLuint buffer)6611 _mesa_MultiTexBufferEXT(GLenum texunit, GLenum target,
6612                         GLenum internalFormat, GLuint buffer)
6613 {
6614    struct gl_texture_object *texObj;
6615    struct gl_buffer_object *bufObj;
6616 
6617    GET_CURRENT_CONTEXT(ctx);
6618 
6619    if (buffer) {
6620       bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glMultiTexBufferEXT");
6621       if (!bufObj)
6622          return;
6623    } else
6624       bufObj = NULL;
6625 
6626    /* Get the texture object */
6627    texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
6628                                                    texunit - GL_TEXTURE0,
6629                                                    true,
6630                                                    "glMultiTexBufferEXT");
6631 
6632    if (!texObj ||
6633        !check_texture_buffer_target(ctx, texObj->Target, "glMultiTexBufferEXT", false))
6634       return;
6635 
6636    texture_buffer_range(ctx, texObj, internalFormat,
6637                         bufObj, 0, buffer ? -1 : 0, "glMultiTexBufferEXT");
6638 }
6639 
6640 void GLAPIENTRY
_mesa_TextureBufferRange(GLuint texture,GLenum internalFormat,GLuint buffer,GLintptr offset,GLsizeiptr size)6641 _mesa_TextureBufferRange(GLuint texture, GLenum internalFormat, GLuint buffer,
6642                          GLintptr offset, GLsizeiptr size)
6643 {
6644    struct gl_texture_object *texObj;
6645    struct gl_buffer_object *bufObj;
6646 
6647    GET_CURRENT_CONTEXT(ctx);
6648 
6649    if (buffer) {
6650       bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
6651                                           "glTextureBufferRange");
6652       if (!bufObj)
6653          return;
6654 
6655       if (!check_texture_buffer_range(ctx, bufObj, offset, size,
6656           "glTextureBufferRange"))
6657          return;
6658 
6659    } else {
6660       /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
6661        * Textures (PDF page 254):
6662        *    "If buffer is zero, then any buffer object attached to the buffer
6663        *    texture is detached, the values offset and size are ignored and
6664        *    the state for offset and size for the buffer texture are reset to
6665        *    zero."
6666        */
6667       offset = 0;
6668       size = 0;
6669       bufObj = NULL;
6670    }
6671 
6672    /* Get the texture object by Name. */
6673    texObj = _mesa_lookup_texture_err(ctx, texture, "glTextureBufferRange");
6674    if (!texObj)
6675       return;
6676 
6677    if (!check_texture_buffer_target(ctx, texObj->Target,
6678        "glTextureBufferRange", true))
6679       return;
6680 
6681    texture_buffer_range(ctx, texObj, internalFormat,
6682                         bufObj, offset, size, "glTextureBufferRange");
6683 }
6684 
6685 GLboolean
_mesa_is_renderable_texture_format(const struct gl_context * ctx,GLenum internalformat)6686 _mesa_is_renderable_texture_format(const struct gl_context *ctx,
6687                                    GLenum internalformat)
6688 {
6689    /* Everything that is allowed for renderbuffers,
6690     * except for a base format of GL_STENCIL_INDEX, unless supported.
6691     */
6692    GLenum baseFormat = _mesa_base_fbo_format(ctx, internalformat);
6693    if (ctx->Extensions.ARB_texture_stencil8)
6694       return baseFormat != 0;
6695    else
6696       return baseFormat != 0 && baseFormat != GL_STENCIL_INDEX;
6697 }
6698 
6699 
6700 /** GL_ARB_texture_multisample */
6701 static GLboolean
check_multisample_target(GLuint dims,GLenum target,bool dsa)6702 check_multisample_target(GLuint dims, GLenum target, bool dsa)
6703 {
6704    switch(target) {
6705    case GL_TEXTURE_2D_MULTISAMPLE:
6706       return dims == 2;
6707    case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
6708       return dims == 2 && !dsa;
6709    case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
6710       return dims == 3;
6711    case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
6712       return dims == 3 && !dsa;
6713    default:
6714       return GL_FALSE;
6715    }
6716 }
6717 
6718 
6719 static void
texture_image_multisample(struct gl_context * ctx,GLuint dims,struct gl_texture_object * texObj,struct gl_memory_object * memObj,GLenum target,GLsizei samples,GLint internalformat,GLsizei width,GLsizei height,GLsizei depth,GLboolean fixedsamplelocations,GLboolean immutable,GLuint64 offset,const char * func)6720 texture_image_multisample(struct gl_context *ctx, GLuint dims,
6721                           struct gl_texture_object *texObj,
6722                           struct gl_memory_object *memObj,
6723                           GLenum target, GLsizei samples,
6724                           GLint internalformat, GLsizei width,
6725                           GLsizei height, GLsizei depth,
6726                           GLboolean fixedsamplelocations,
6727                           GLboolean immutable, GLuint64 offset,
6728                           const char *func)
6729 {
6730    struct gl_texture_image *texImage;
6731    GLboolean sizeOK, dimensionsOK, samplesOK;
6732    mesa_format texFormat;
6733    GLenum sample_count_error;
6734    bool dsa = strstr(func, "ture") ? true : false;
6735 
6736    if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE)) {
6737       _mesa_debug(ctx, "%s(target=%s, samples=%d)\n", func,
6738                   _mesa_enum_to_string(target), samples);
6739    }
6740 
6741    if (!((ctx->Extensions.ARB_texture_multisample
6742          && _mesa_is_desktop_gl(ctx))) && !_mesa_is_gles31(ctx)) {
6743       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(unsupported)", func);
6744       return;
6745    }
6746 
6747    if (samples < 1) {
6748       _mesa_error(ctx, GL_INVALID_VALUE, "%s(samples < 1)", func);
6749       return;
6750    }
6751 
6752    if (!check_multisample_target(dims, target, dsa)) {
6753       GLenum err = dsa ? GL_INVALID_OPERATION : GL_INVALID_ENUM;
6754       _mesa_error(ctx, err, "%s(target=%s)", func,
6755                   _mesa_enum_to_string(target));
6756       return;
6757    }
6758 
6759    /* check that the specified internalformat is color/depth/stencil-renderable;
6760     * refer GL3.1 spec 4.4.4
6761     */
6762 
6763    if (immutable && !_mesa_is_legal_tex_storage_format(ctx, internalformat)) {
6764       _mesa_error(ctx, GL_INVALID_ENUM,
6765             "%s(internalformat=%s not legal for immutable-format)",
6766             func, _mesa_enum_to_string(internalformat));
6767       return;
6768    }
6769 
6770    if (!_mesa_is_renderable_texture_format(ctx, internalformat)) {
6771       /* Page 172 of OpenGL ES 3.1 spec says:
6772        *   "An INVALID_ENUM error is generated if sizedinternalformat is not
6773        *   color-renderable, depth-renderable, or stencil-renderable (as
6774        *   defined in section 9.4).
6775        *
6776        *  (Same error is also defined for desktop OpenGL for multisample
6777        *  teximage/texstorage functions.)
6778        */
6779       _mesa_error(ctx, GL_INVALID_ENUM, "%s(internalformat=%s)", func,
6780                   _mesa_enum_to_string(internalformat));
6781       return;
6782    }
6783 
6784    sample_count_error = _mesa_check_sample_count(ctx, target,
6785          internalformat, samples, samples);
6786    samplesOK = sample_count_error == GL_NO_ERROR;
6787 
6788    /* Page 254 of OpenGL 4.4 spec says:
6789     *   "Proxy arrays for two-dimensional multisample and two-dimensional
6790     *    multisample array textures are operated on in the same way when
6791     *    TexImage2DMultisample is called with target specified as
6792     *    PROXY_TEXTURE_2D_MULTISAMPLE, or TexImage3DMultisample is called
6793     *    with target specified as PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY.
6794     *    However, if samples is not supported, then no error is generated.
6795     */
6796    if (!samplesOK && !_mesa_is_proxy_texture(target)) {
6797       _mesa_error(ctx, sample_count_error, "%s(samples=%d)", func, samples);
6798       return;
6799    }
6800 
6801    if (immutable && (!texObj || (texObj->Name == 0))) {
6802       _mesa_error(ctx, GL_INVALID_OPERATION,
6803             "%s(texture object 0)",
6804             func);
6805       return;
6806    }
6807 
6808    texImage = _mesa_get_tex_image(ctx, texObj, 0, 0);
6809 
6810    if (texImage == NULL) {
6811       _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s()", func);
6812       return;
6813    }
6814 
6815    texFormat = _mesa_choose_texture_format(ctx, texObj, target, 0,
6816          internalformat, GL_NONE, GL_NONE);
6817    assert(texFormat != MESA_FORMAT_NONE);
6818 
6819    dimensionsOK = _mesa_legal_texture_dimensions(ctx, target, 0,
6820          width, height, depth, 0);
6821 
6822    sizeOK = ctx->Driver.TestProxyTexImage(ctx, target, 0, 0, texFormat,
6823                                           samples, width, height, depth);
6824 
6825    if (_mesa_is_proxy_texture(target)) {
6826       if (samplesOK && dimensionsOK && sizeOK) {
6827          _mesa_init_teximage_fields_ms(ctx, texImage, width, height, depth, 0,
6828                                        internalformat, texFormat,
6829                                        samples, fixedsamplelocations);
6830       }
6831       else {
6832          /* clear all image fields */
6833          clear_teximage_fields(texImage);
6834       }
6835    }
6836    else {
6837       if (!dimensionsOK) {
6838          _mesa_error(ctx, GL_INVALID_VALUE,
6839                      "%s(invalid width=%d or height=%d)", func, width, height);
6840          return;
6841       }
6842 
6843       if (!sizeOK) {
6844          _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s(texture too large)", func);
6845          return;
6846       }
6847 
6848       /* Check if texObj->Immutable is set */
6849       if (texObj->Immutable) {
6850          _mesa_error(ctx, GL_INVALID_OPERATION, "%s(immutable)", func);
6851          return;
6852       }
6853 
6854       ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
6855 
6856       _mesa_init_teximage_fields_ms(ctx, texImage, width, height, depth, 0,
6857                                     internalformat, texFormat,
6858                                     samples, fixedsamplelocations);
6859 
6860       if (width > 0 && height > 0 && depth > 0) {
6861          if (memObj) {
6862             if (!ctx->Driver.SetTextureStorageForMemoryObject(ctx, texObj,
6863                                                               memObj, 1, width,
6864                                                               height, depth,
6865                                                               offset)) {
6866 
6867                _mesa_init_teximage_fields(ctx, texImage, 0, 0, 0, 0,
6868                                           internalformat, texFormat);
6869             }
6870          } else {
6871             if (!ctx->Driver.AllocTextureStorage(ctx, texObj, 1,
6872                                                  width, height, depth)) {
6873                /* tidy up the texture image state. strictly speaking,
6874                 * we're allowed to just leave this in whatever state we
6875                 * like, but being tidy is good.
6876                 */
6877                _mesa_init_teximage_fields(ctx, texImage, 0, 0, 0, 0,
6878                                           internalformat, texFormat);
6879             }
6880          }
6881       }
6882 
6883       texObj->Immutable |= immutable;
6884 
6885       if (immutable) {
6886          _mesa_set_texture_view_state(ctx, texObj, target, 1);
6887       }
6888 
6889       _mesa_update_fbo_texture(ctx, texObj, 0, 0);
6890    }
6891 }
6892 
6893 
6894 void GLAPIENTRY
_mesa_TexImage2DMultisample(GLenum target,GLsizei samples,GLenum internalformat,GLsizei width,GLsizei height,GLboolean fixedsamplelocations)6895 _mesa_TexImage2DMultisample(GLenum target, GLsizei samples,
6896                             GLenum internalformat, GLsizei width,
6897                             GLsizei height, GLboolean fixedsamplelocations)
6898 {
6899    struct gl_texture_object *texObj;
6900    GET_CURRENT_CONTEXT(ctx);
6901 
6902    texObj = _mesa_get_current_tex_object(ctx, target);
6903    if (!texObj)
6904       return;
6905 
6906    texture_image_multisample(ctx, 2, texObj, NULL, target, samples,
6907                              internalformat, width, height, 1,
6908                              fixedsamplelocations, GL_FALSE, 0,
6909                              "glTexImage2DMultisample");
6910 }
6911 
6912 
6913 void GLAPIENTRY
_mesa_TexImage3DMultisample(GLenum target,GLsizei samples,GLenum internalformat,GLsizei width,GLsizei height,GLsizei depth,GLboolean fixedsamplelocations)6914 _mesa_TexImage3DMultisample(GLenum target, GLsizei samples,
6915                             GLenum internalformat, GLsizei width,
6916                             GLsizei height, GLsizei depth,
6917                             GLboolean fixedsamplelocations)
6918 {
6919    struct gl_texture_object *texObj;
6920    GET_CURRENT_CONTEXT(ctx);
6921 
6922    texObj = _mesa_get_current_tex_object(ctx, target);
6923    if (!texObj)
6924       return;
6925 
6926    texture_image_multisample(ctx, 3, texObj, NULL, target, samples,
6927                              internalformat, width, height, depth,
6928                              fixedsamplelocations, GL_FALSE, 0,
6929                              "glTexImage3DMultisample");
6930 }
6931 
6932 static bool
valid_texstorage_ms_parameters(GLsizei width,GLsizei height,GLsizei depth,unsigned dims)6933 valid_texstorage_ms_parameters(GLsizei width, GLsizei height, GLsizei depth,
6934                                unsigned dims)
6935 {
6936    GET_CURRENT_CONTEXT(ctx);
6937 
6938    if (!_mesa_valid_tex_storage_dim(width, height, depth)) {
6939       _mesa_error(ctx, GL_INVALID_VALUE,
6940                   "glTexStorage%uDMultisample(width=%d,height=%d,depth=%d)",
6941                   dims, width, height, depth);
6942       return false;
6943    }
6944    return true;
6945 }
6946 
6947 void GLAPIENTRY
_mesa_TexStorage2DMultisample(GLenum target,GLsizei samples,GLenum internalformat,GLsizei width,GLsizei height,GLboolean fixedsamplelocations)6948 _mesa_TexStorage2DMultisample(GLenum target, GLsizei samples,
6949                               GLenum internalformat, GLsizei width,
6950                               GLsizei height, GLboolean fixedsamplelocations)
6951 {
6952    struct gl_texture_object *texObj;
6953    GET_CURRENT_CONTEXT(ctx);
6954 
6955    texObj = _mesa_get_current_tex_object(ctx, target);
6956    if (!texObj)
6957       return;
6958 
6959    if (!valid_texstorage_ms_parameters(width, height, 1, 2))
6960       return;
6961 
6962    texture_image_multisample(ctx, 2, texObj, NULL, target, samples,
6963                              internalformat, width, height, 1,
6964                              fixedsamplelocations, GL_TRUE, 0,
6965                              "glTexStorage2DMultisample");
6966 }
6967 
6968 void GLAPIENTRY
_mesa_TexStorage3DMultisample(GLenum target,GLsizei samples,GLenum internalformat,GLsizei width,GLsizei height,GLsizei depth,GLboolean fixedsamplelocations)6969 _mesa_TexStorage3DMultisample(GLenum target, GLsizei samples,
6970                               GLenum internalformat, GLsizei width,
6971                               GLsizei height, GLsizei depth,
6972                               GLboolean fixedsamplelocations)
6973 {
6974    struct gl_texture_object *texObj;
6975    GET_CURRENT_CONTEXT(ctx);
6976 
6977    texObj = _mesa_get_current_tex_object(ctx, target);
6978    if (!texObj)
6979       return;
6980 
6981    if (!valid_texstorage_ms_parameters(width, height, depth, 3))
6982       return;
6983 
6984    texture_image_multisample(ctx, 3, texObj, NULL, target, samples,
6985                              internalformat, width, height, depth,
6986                              fixedsamplelocations, GL_TRUE, 0,
6987                              "glTexStorage3DMultisample");
6988 }
6989 
6990 void GLAPIENTRY
_mesa_TextureStorage2DMultisample(GLuint texture,GLsizei samples,GLenum internalformat,GLsizei width,GLsizei height,GLboolean fixedsamplelocations)6991 _mesa_TextureStorage2DMultisample(GLuint texture, GLsizei samples,
6992                                   GLenum internalformat, GLsizei width,
6993                                   GLsizei height,
6994                                   GLboolean fixedsamplelocations)
6995 {
6996    struct gl_texture_object *texObj;
6997    GET_CURRENT_CONTEXT(ctx);
6998 
6999    texObj = _mesa_lookup_texture_err(ctx, texture,
7000                                      "glTextureStorage2DMultisample");
7001    if (!texObj)
7002       return;
7003 
7004    if (!valid_texstorage_ms_parameters(width, height, 1, 2))
7005       return;
7006 
7007    texture_image_multisample(ctx, 2, texObj, NULL, texObj->Target,
7008                              samples, internalformat, width, height, 1,
7009                              fixedsamplelocations, GL_TRUE, 0,
7010                              "glTextureStorage2DMultisample");
7011 }
7012 
7013 void GLAPIENTRY
_mesa_TextureStorage3DMultisample(GLuint texture,GLsizei samples,GLenum internalformat,GLsizei width,GLsizei height,GLsizei depth,GLboolean fixedsamplelocations)7014 _mesa_TextureStorage3DMultisample(GLuint texture, GLsizei samples,
7015                                   GLenum internalformat, GLsizei width,
7016                                   GLsizei height, GLsizei depth,
7017                                   GLboolean fixedsamplelocations)
7018 {
7019    struct gl_texture_object *texObj;
7020    GET_CURRENT_CONTEXT(ctx);
7021 
7022    /* Get the texture object by Name. */
7023    texObj = _mesa_lookup_texture_err(ctx, texture,
7024                                      "glTextureStorage3DMultisample");
7025    if (!texObj)
7026       return;
7027 
7028    if (!valid_texstorage_ms_parameters(width, height, depth, 3))
7029       return;
7030 
7031    texture_image_multisample(ctx, 3, texObj, NULL, texObj->Target, samples,
7032                              internalformat, width, height, depth,
7033                              fixedsamplelocations, GL_TRUE, 0,
7034                              "glTextureStorage3DMultisample");
7035 }
7036 
7037 void GLAPIENTRY
_mesa_TextureStorage2DMultisampleEXT(GLuint texture,GLenum target,GLsizei samples,GLenum internalformat,GLsizei width,GLsizei height,GLboolean fixedsamplelocations)7038 _mesa_TextureStorage2DMultisampleEXT(GLuint texture, GLenum target, GLsizei samples,
7039                                      GLenum internalformat, GLsizei width,
7040                                      GLsizei height,
7041                                      GLboolean fixedsamplelocations)
7042 {
7043    struct gl_texture_object *texObj;
7044    GET_CURRENT_CONTEXT(ctx);
7045 
7046    texObj = lookup_texture_ext_dsa(ctx, target, texture,
7047                                    "glTextureStorage2DMultisampleEXT");
7048    if (!texObj)
7049       return;
7050 
7051    if (!valid_texstorage_ms_parameters(width, height, 1, 2))
7052       return;
7053 
7054    texture_image_multisample(ctx, 2, texObj, NULL, texObj->Target,
7055                              samples, internalformat, width, height, 1,
7056                              fixedsamplelocations, GL_TRUE, 0,
7057                              "glTextureStorage2DMultisampleEXT");
7058 }
7059 
7060 void GLAPIENTRY
_mesa_TextureStorage3DMultisampleEXT(GLuint texture,GLenum target,GLsizei samples,GLenum internalformat,GLsizei width,GLsizei height,GLsizei depth,GLboolean fixedsamplelocations)7061 _mesa_TextureStorage3DMultisampleEXT(GLuint texture, GLenum target, GLsizei samples,
7062                                      GLenum internalformat, GLsizei width,
7063                                      GLsizei height, GLsizei depth,
7064                                      GLboolean fixedsamplelocations)
7065 {
7066    struct gl_texture_object *texObj;
7067    GET_CURRENT_CONTEXT(ctx);
7068 
7069    texObj = lookup_texture_ext_dsa(ctx, target, texture,
7070                                    "glTextureStorage3DMultisampleEXT");
7071    if (!texObj)
7072       return;
7073 
7074    if (!valid_texstorage_ms_parameters(width, height, depth, 3))
7075       return;
7076 
7077    texture_image_multisample(ctx, 3, texObj, NULL, texObj->Target, samples,
7078                              internalformat, width, height, depth,
7079                              fixedsamplelocations, GL_TRUE, 0,
7080                              "glTextureStorage3DMultisampleEXT");
7081 }
7082 
7083 void
_mesa_texture_storage_ms_memory(struct gl_context * ctx,GLuint dims,struct gl_texture_object * texObj,struct gl_memory_object * memObj,GLenum target,GLsizei samples,GLenum internalFormat,GLsizei width,GLsizei height,GLsizei depth,GLboolean fixedSampleLocations,GLuint64 offset,const char * func)7084 _mesa_texture_storage_ms_memory(struct gl_context *ctx, GLuint dims,
7085                                 struct gl_texture_object *texObj,
7086                                 struct gl_memory_object *memObj,
7087                                 GLenum target, GLsizei samples,
7088                                 GLenum internalFormat, GLsizei width,
7089                                 GLsizei height, GLsizei depth,
7090                                 GLboolean fixedSampleLocations,
7091                                 GLuint64 offset, const char* func)
7092 {
7093    assert(memObj);
7094 
7095    texture_image_multisample(ctx, dims, texObj, memObj, target, samples,
7096                              internalFormat, width, height, depth,
7097                              fixedSampleLocations, GL_TRUE, offset,
7098                              func);
7099 }
7100