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