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