• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Mesa 3-D graphics library
3  *
4  * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
5  * Copyright (C) 2009  VMware, Inc.  All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  * and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23  * OTHER DEALINGS IN THE SOFTWARE.
24  */
25 
26 
27 /**
28  * \file teximage.c
29  * Texture image-related functions.
30  */
31 
32 #include <stdbool.h>
33 #include "util/glheader.h"
34 #include "bufferobj.h"
35 #include "context.h"
36 #include "enums.h"
37 #include "fbobject.h"
38 #include "framebuffer.h"
39 #include "hash.h"
40 #include "image.h"
41 
42 #include "macros.h"
43 #include "mipmap.h"
44 #include "multisample.h"
45 #include "pixel.h"
46 #include "pixelstore.h"
47 #include "state.h"
48 #include "texcompress.h"
49 #include "texcompress_cpal.h"
50 #include "teximage.h"
51 #include "texobj.h"
52 #include "texstate.h"
53 #include "texstorage.h"
54 #include "textureview.h"
55 #include "mtypes.h"
56 #include "glformats.h"
57 #include "texstore.h"
58 #include "pbo.h"
59 #include "api_exec_decl.h"
60 
61 #include "util/u_memory.h"
62 
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 #ifdef 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(ctx) || ctx->Extensions.ARB_ES3_compatibility);
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_gles(ctx) && ctx->Version < 20)
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       default:
2480          _mesa_error(ctx, GL_INVALID_ENUM,
2481                      "glCopyTexImage%dD(internalFormat=%s)", dimensions,
2482                      _mesa_enum_to_string(internalFormat));
2483          return GL_TRUE;
2484       }
2485    } else {
2486       /*
2487        * Section 8.6 (Alternate Texture Image Specification Commands) of the
2488        * OpenGL 4.5 (Compatibility Profile) spec says:
2489        *
2490        *     "Parameters level, internalformat, and border are specified using
2491        *     the same values, with the same meanings, as the corresponding
2492        *     arguments of TexImage2D, except that internalformat may not be
2493        *     specified as 1, 2, 3, or 4."
2494        */
2495       if (internalFormat >= 1 && internalFormat <= 4) {
2496          _mesa_error(ctx, GL_INVALID_ENUM,
2497                      "glCopyTexImage%dD(internalFormat=%d)", dimensions,
2498                      internalFormat);
2499          return GL_TRUE;
2500       }
2501    }
2502 
2503    baseFormat = _mesa_base_tex_format(ctx, internalFormat);
2504    if (baseFormat < 0) {
2505       _mesa_error(ctx, GL_INVALID_ENUM,
2506                   "glCopyTexImage%dD(internalFormat=%s)", dimensions,
2507                   _mesa_enum_to_string(internalFormat));
2508       return GL_TRUE;
2509    }
2510 
2511    rb = _mesa_get_read_renderbuffer_for_format(ctx, internalFormat);
2512    if (rb == NULL) {
2513       _mesa_error(ctx, GL_INVALID_OPERATION,
2514                   "glCopyTexImage%dD(read buffer)", dimensions);
2515       return GL_TRUE;
2516    }
2517 
2518    rb_internal_format = rb->InternalFormat;
2519    rb_base_format = _mesa_base_tex_format(ctx, rb->InternalFormat);
2520    if (_mesa_is_color_format(internalFormat)) {
2521       if (rb_base_format < 0) {
2522          _mesa_error(ctx, GL_INVALID_VALUE,
2523                      "glCopyTexImage%dD(internalFormat=%s)", dimensions,
2524                      _mesa_enum_to_string(internalFormat));
2525          return GL_TRUE;
2526       }
2527    }
2528 
2529    if (_mesa_is_gles(ctx)) {
2530       bool valid = true;
2531       if (_mesa_components_in_format(baseFormat) >
2532           _mesa_components_in_format(rb_base_format)) {
2533          valid = false;
2534       }
2535       if (baseFormat == GL_DEPTH_COMPONENT ||
2536           baseFormat == GL_DEPTH_STENCIL ||
2537           baseFormat == GL_STENCIL_INDEX ||
2538           rb_base_format == GL_DEPTH_COMPONENT ||
2539           rb_base_format == GL_DEPTH_STENCIL ||
2540           rb_base_format == GL_STENCIL_INDEX ||
2541           ((baseFormat == GL_LUMINANCE_ALPHA ||
2542             baseFormat == GL_ALPHA) &&
2543            rb_base_format != GL_RGBA) ||
2544           internalFormat == GL_RGB9_E5) {
2545          valid = false;
2546       }
2547       if (internalFormat == GL_RGB9_E5) {
2548          valid = false;
2549       }
2550       if (!valid) {
2551          _mesa_error(ctx, GL_INVALID_OPERATION,
2552                      "glCopyTexImage%dD(internalFormat=%s)", dimensions,
2553                      _mesa_enum_to_string(internalFormat));
2554          return GL_TRUE;
2555       }
2556    }
2557 
2558    if (_mesa_is_gles3(ctx)) {
2559       bool rb_is_srgb = (ctx->Extensions.EXT_sRGB &&
2560                          _mesa_is_format_srgb(rb->Format));
2561       bool dst_is_srgb = false;
2562 
2563       if (_mesa_get_linear_internalformat(internalFormat) != internalFormat) {
2564          dst_is_srgb = true;
2565       }
2566 
2567       if (rb_is_srgb != dst_is_srgb) {
2568          /* Page 137 (page 149 of the PDF) in section 3.8.5 of the
2569           * OpenGLES 3.0.0 spec says:
2570           *
2571           *     "The error INVALID_OPERATION is also generated if the
2572           *     value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING for the
2573           *     framebuffer attachment corresponding to the read buffer
2574           *     is LINEAR (see section 6.1.13) and internalformat is
2575           *     one of the sRGB formats described in section 3.8.16, or
2576           *     if the value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING is
2577           *     SRGB and internalformat is not one of the sRGB formats."
2578           */
2579          _mesa_error(ctx, GL_INVALID_OPERATION,
2580                      "glCopyTexImage%dD(srgb usage mismatch)", dimensions);
2581          return GL_TRUE;
2582       }
2583 
2584       /* Page 139, Table 3.15 of OpenGL ES 3.0 spec does not define ReadPixels
2585        * types for SNORM formats. Also, conversion to SNORM formats is not
2586        * allowed by Table 3.2 on Page 110.
2587        */
2588       if (!_mesa_has_EXT_render_snorm(ctx) &&
2589           _mesa_is_enum_format_snorm(internalFormat)) {
2590          _mesa_error(ctx, GL_INVALID_OPERATION,
2591                      "glCopyTexImage%dD(internalFormat=%s)", dimensions,
2592                      _mesa_enum_to_string(internalFormat));
2593          return GL_TRUE;
2594       }
2595    }
2596 
2597    if (!_mesa_source_buffer_exists(ctx, baseFormat)) {
2598       _mesa_error(ctx, GL_INVALID_OPERATION,
2599                   "glCopyTexImage%dD(missing readbuffer)", dimensions);
2600       return GL_TRUE;
2601    }
2602 
2603    /* From the EXT_texture_integer spec:
2604     *
2605     *     "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
2606     *      if the texture internalformat is an integer format and the read color
2607     *      buffer is not an integer format, or if the internalformat is not an
2608     *      integer format and the read color buffer is an integer format."
2609     */
2610    if (_mesa_is_color_format(internalFormat)) {
2611       bool is_int = _mesa_is_enum_format_integer(internalFormat);
2612       bool is_rbint = _mesa_is_enum_format_integer(rb_internal_format);
2613       bool is_unorm = _mesa_is_enum_format_unorm(internalFormat);
2614       bool is_rbunorm = _mesa_is_enum_format_unorm(rb_internal_format);
2615       if (is_int || is_rbint) {
2616          if (is_int != is_rbint) {
2617             _mesa_error(ctx, GL_INVALID_OPERATION,
2618                         "glCopyTexImage%dD(integer vs non-integer)", dimensions);
2619             return GL_TRUE;
2620          } else if (_mesa_is_gles(ctx) &&
2621                     _mesa_is_enum_format_unsigned_int(internalFormat) !=
2622                       _mesa_is_enum_format_unsigned_int(rb_internal_format)) {
2623             _mesa_error(ctx, GL_INVALID_OPERATION,
2624                         "glCopyTexImage%dD(signed vs unsigned integer)",
2625                         dimensions);
2626             return GL_TRUE;
2627          }
2628       }
2629 
2630       /* From page 138 of OpenGL ES 3.0 spec:
2631        *    "The error INVALID_OPERATION is generated if floating-point RGBA
2632        *    data is required; if signed integer RGBA data is required and the
2633        *    format of the current color buffer is not signed integer; if
2634        *    unsigned integer RGBA data is required and the format of the
2635        *    current color buffer is not unsigned integer; or if fixed-point
2636        *    RGBA data is required and the format of the current color buffer
2637        *    is not fixed-point.
2638        */
2639       if (_mesa_is_gles(ctx) && is_unorm != is_rbunorm)
2640             _mesa_error(ctx, GL_INVALID_OPERATION,
2641                         "glCopyTexImage%dD(unorm vs non-unorm)", dimensions);
2642    }
2643 
2644    if (_mesa_is_compressed_format(ctx, internalFormat)) {
2645       GLenum err;
2646       if (!_mesa_target_can_be_compressed(ctx, target, internalFormat, &err)) {
2647          _mesa_error(ctx, err,
2648                      "glCopyTexImage%dD(target can't be compressed)", dimensions);
2649          return GL_TRUE;
2650       }
2651       if (_mesa_format_no_online_compression(internalFormat)) {
2652          _mesa_error(ctx, GL_INVALID_OPERATION,
2653                "glCopyTexImage%dD(no compression for format)", dimensions);
2654          return GL_TRUE;
2655       }
2656       if (border != 0) {
2657          _mesa_error(ctx, GL_INVALID_OPERATION,
2658                      "glCopyTexImage%dD(border!=0)", dimensions);
2659          return GL_TRUE;
2660       }
2661    }
2662 
2663    if (!mutable_tex_object(texObj)) {
2664       _mesa_error(ctx, GL_INVALID_OPERATION,
2665                   "glCopyTexImage%dD(immutable texture)", dimensions);
2666       return GL_TRUE;
2667    }
2668 
2669    /* if we get here, the parameters are OK */
2670    return GL_FALSE;
2671 }
2672 
2673 
2674 /**
2675  * Test glCopyTexSubImage[12]D() parameters for errors.
2676  * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2677  */
2678 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)2679 copytexsubimage_error_check(struct gl_context *ctx, GLuint dimensions,
2680                             const struct gl_texture_object *texObj,
2681                             GLenum target, GLint level,
2682                             GLint xoffset, GLint yoffset, GLint zoffset,
2683                             GLint width, GLint height, const char *caller)
2684 {
2685    assert(texObj);
2686 
2687    struct gl_texture_image *texImage;
2688 
2689    /* Check that the source buffer is complete */
2690    if (_mesa_is_user_fbo(ctx->ReadBuffer)) {
2691       if (ctx->ReadBuffer->_Status == 0) {
2692          _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
2693       }
2694       if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2695          _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2696                      "%s(invalid readbuffer)", caller);
2697          return GL_TRUE;
2698       }
2699 
2700       /**
2701        * From the GL_EXT_multisampled_render_to_texture spec:
2702        *
2703        * "An INVALID_OPERATION error is generated by CopyTexSubImage3D,
2704        * CopyTexImage2D, or CopyTexSubImage2D if [...] the value of
2705        * READ_FRAMEBUFFER_BINDING is non-zero, and:
2706        *   - the read buffer selects an attachment that has no image attached,
2707        *     or
2708        *   - the value of SAMPLE_BUFFERS for the read framebuffer is one."
2709        *
2710        * [...]
2711        * These errors do not apply to textures and renderbuffers that have
2712        * associated multisample data specified by the mechanisms described in
2713        * this extension, i.e., the above operations are allowed even when
2714        * SAMPLE_BUFFERS is non-zero for renderbuffers created via Renderbuffer-
2715        * StorageMultisampleEXT or textures attached via FramebufferTexture2D-
2716        * MultisampleEXT.
2717        */
2718       if (!ctx->st_opts->allow_multisampled_copyteximage &&
2719           ctx->ReadBuffer->Visual.samples > 0 &&
2720           !_mesa_has_rtt_samples(ctx->ReadBuffer)) {
2721          _mesa_error(ctx, GL_INVALID_OPERATION, "%s(multisample FBO)",
2722                      caller);
2723          return GL_TRUE;
2724       }
2725    }
2726 
2727    /* Check level */
2728    if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
2729       _mesa_error(ctx, GL_INVALID_VALUE, "%s(level=%d)", caller, level);
2730       return GL_TRUE;
2731    }
2732 
2733    texImage = _mesa_select_tex_image(texObj, target, level);
2734    if (!texImage) {
2735       /* destination image does not exist */
2736       _mesa_error(ctx, GL_INVALID_OPERATION,
2737                   "%s(invalid texture level %d)", caller, level);
2738       return GL_TRUE;
2739    }
2740 
2741    if (error_check_subtexture_negative_dimensions(ctx, dimensions,
2742                                                   width, height, 1, caller)) {
2743       return GL_TRUE;
2744    }
2745 
2746    if (error_check_subtexture_dimensions(ctx, dimensions, texImage,
2747                                          xoffset, yoffset, zoffset,
2748                                          width, height, 1, caller)) {
2749       return GL_TRUE;
2750    }
2751 
2752    if (_mesa_is_format_compressed(texImage->TexFormat)) {
2753       if (_mesa_format_no_online_compression(texImage->InternalFormat)) {
2754          _mesa_error(ctx, GL_INVALID_OPERATION,
2755                "%s(no compression for format)", caller);
2756          return GL_TRUE;
2757       }
2758    }
2759 
2760    if (texImage->InternalFormat == GL_YCBCR_MESA) {
2761       _mesa_error(ctx, GL_INVALID_OPERATION, "%s()", caller);
2762       return GL_TRUE;
2763    }
2764 
2765    /* From OpenGL ES 3.2 spec, section 8.6:
2766     *
2767     *     "An INVALID_OPERATION error is generated by CopyTexSubImage3D,
2768     *      CopyTexImage2D, or CopyTexSubImage2D if the internalformat of the
2769     *      texture image being (re)specified is RGB9_E5"
2770     */
2771    if (texImage->InternalFormat == GL_RGB9_E5 &&
2772        !_mesa_is_desktop_gl(ctx)) {
2773       _mesa_error(ctx, GL_INVALID_OPERATION,
2774                   "%s(invalid internal format %s)", caller,
2775                   _mesa_enum_to_string(texImage->InternalFormat));
2776       return GL_TRUE;
2777    }
2778 
2779    if (!_mesa_source_buffer_exists(ctx, texImage->_BaseFormat)) {
2780       _mesa_error(ctx, GL_INVALID_OPERATION,
2781                   "%s(missing readbuffer, format=%s)", caller,
2782                   _mesa_enum_to_string(texImage->_BaseFormat));
2783       return GL_TRUE;
2784    }
2785 
2786    /* From the EXT_texture_integer spec:
2787     *
2788     *     "INVALID_OPERATION is generated by CopyTexImage* and
2789     *     CopyTexSubImage* if the texture internalformat is an integer format
2790     *     and the read color buffer is not an integer format, or if the
2791     *     internalformat is not an integer format and the read color buffer
2792     *     is an integer format."
2793     */
2794    if (_mesa_is_color_format(texImage->InternalFormat)) {
2795       struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
2796 
2797       if (_mesa_is_format_integer_color(rb->Format) !=
2798           _mesa_is_format_integer_color(texImage->TexFormat)) {
2799          _mesa_error(ctx, GL_INVALID_OPERATION,
2800                      "%s(integer vs non-integer)", caller);
2801          return GL_TRUE;
2802       }
2803    }
2804 
2805    /* In the ES 3.2 specification's Table 8.13 (Valid CopyTexImage source
2806     * framebuffer/destination texture base internal format combinations),
2807     * all the entries for stencil are left blank (unsupported).
2808     */
2809    if (_mesa_is_gles(ctx) && _mesa_is_stencil_format(texImage->_BaseFormat)) {
2810       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(stencil disallowed)", caller);
2811       return GL_TRUE;
2812    }
2813 
2814    /* if we get here, the parameters are OK */
2815    return GL_FALSE;
2816 }
2817 
2818 
2819 /** Callback info for walking over FBO hash table */
2820 struct cb_info
2821 {
2822    struct gl_context *ctx;
2823    struct gl_texture_object *texObj;
2824    GLuint level, face;
2825 };
2826 
2827 
2828 /**
2829  * Check render to texture callback.  Called from _mesa_HashWalk().
2830  */
2831 static void
check_rtt_cb(void * data,void * userData)2832 check_rtt_cb(void *data, void *userData)
2833 {
2834    struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
2835    const struct cb_info *info = (struct cb_info *) userData;
2836    struct gl_context *ctx = info->ctx;
2837    const struct gl_texture_object *texObj = info->texObj;
2838    const GLuint level = info->level, face = info->face;
2839 
2840    /* If this is a user-created FBO */
2841    if (_mesa_is_user_fbo(fb)) {
2842       GLuint i;
2843       /* check if any of the FBO's attachments point to 'texObj' */
2844       for (i = 0; i < BUFFER_COUNT; i++) {
2845          struct gl_renderbuffer_attachment *att = fb->Attachment + i;
2846          if (att->Type == GL_TEXTURE &&
2847              att->Texture == texObj &&
2848              att->TextureLevel == level &&
2849              att->CubeMapFace == face) {
2850             _mesa_update_texture_renderbuffer(ctx, fb, att);
2851             assert(att->Renderbuffer->TexImage);
2852             /* Mark fb status as indeterminate to force re-validation */
2853             fb->_Status = 0;
2854 
2855             /* Make sure that the revalidation actually happens if this is
2856              * being done to currently-bound buffers.
2857              */
2858             if (fb == ctx->DrawBuffer || fb == ctx->ReadBuffer)
2859                ctx->NewState |= _NEW_BUFFERS;
2860          }
2861       }
2862    }
2863 }
2864 
2865 
2866 /**
2867  * When a texture image is specified we have to check if it's bound to
2868  * any framebuffer objects (render to texture) in order to detect changes
2869  * in size or format since that effects FBO completeness.
2870  * Any FBOs rendering into the texture must be re-validated.
2871  */
2872 void
_mesa_update_fbo_texture(struct gl_context * ctx,struct gl_texture_object * texObj,GLuint face,GLuint level)2873 _mesa_update_fbo_texture(struct gl_context *ctx,
2874                          struct gl_texture_object *texObj,
2875                          GLuint face, GLuint level)
2876 {
2877    /* Only check this texture if it's been marked as RenderToTexture */
2878    if (texObj->_RenderToTexture) {
2879       struct cb_info info;
2880       info.ctx = ctx;
2881       info.texObj = texObj;
2882       info.level = level;
2883       info.face = face;
2884       _mesa_HashWalk(&ctx->Shared->FrameBuffers, check_rtt_cb, &info);
2885    }
2886 }
2887 
2888 
2889 /**
2890  * If the texture object's GenerateMipmap flag is set and we've
2891  * changed the texture base level image, regenerate the rest of the
2892  * mipmap levels now.
2893  */
2894 static inline void
check_gen_mipmap(struct gl_context * ctx,GLenum target,struct gl_texture_object * texObj,GLint level)2895 check_gen_mipmap(struct gl_context *ctx, GLenum target,
2896                  struct gl_texture_object *texObj, GLint level)
2897 {
2898    if (texObj->Attrib.GenerateMipmap &&
2899        level == texObj->Attrib.BaseLevel &&
2900        level < texObj->Attrib.MaxLevel) {
2901       st_generate_mipmap(ctx, target, texObj);
2902    }
2903 }
2904 
2905 
2906 /** Debug helper: override the user-requested internal format */
2907 static GLenum
override_internal_format(GLenum internalFormat,UNUSED GLint width,UNUSED GLint height)2908 override_internal_format(GLenum internalFormat, UNUSED GLint width,
2909                          UNUSED GLint height)
2910 {
2911 #if 0
2912    if (internalFormat == GL_RGBA16F_ARB ||
2913        internalFormat == GL_RGBA32F_ARB) {
2914       printf("Convert rgba float tex to int %d x %d\n", width, height);
2915       return GL_RGBA;
2916    }
2917    else if (internalFormat == GL_RGB16F_ARB ||
2918             internalFormat == GL_RGB32F_ARB) {
2919       printf("Convert rgb float tex to int %d x %d\n", width, height);
2920       return GL_RGB;
2921    }
2922    else if (internalFormat == GL_LUMINANCE_ALPHA16F_ARB ||
2923             internalFormat == GL_LUMINANCE_ALPHA32F_ARB) {
2924       printf("Convert luminance float tex to int %d x %d\n", width, height);
2925       return GL_LUMINANCE_ALPHA;
2926    }
2927    else if (internalFormat == GL_LUMINANCE16F_ARB ||
2928             internalFormat == GL_LUMINANCE32F_ARB) {
2929       printf("Convert luminance float tex to int %d x %d\n", width, height);
2930       return GL_LUMINANCE;
2931    }
2932    else if (internalFormat == GL_ALPHA16F_ARB ||
2933             internalFormat == GL_ALPHA32F_ARB) {
2934       printf("Convert luminance float tex to int %d x %d\n", width, height);
2935       return GL_ALPHA;
2936    }
2937    /*
2938    else if (internalFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) {
2939       internalFormat = GL_RGBA;
2940    }
2941    */
2942    else {
2943       return internalFormat;
2944    }
2945 #else
2946    return internalFormat;
2947 #endif
2948 }
2949 
2950 
2951 /**
2952  * Choose the actual hardware format for a texture image.
2953  * Try to use the same format as the previous image level when possible.
2954  * Otherwise, ask the driver for the best format.
2955  * It's important to try to choose a consistant format for all levels
2956  * for efficient texture memory layout/allocation.  In particular, this
2957  * comes up during automatic mipmap generation.
2958  */
2959 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)2960 _mesa_choose_texture_format(struct gl_context *ctx,
2961                             struct gl_texture_object *texObj,
2962                             GLenum target, GLint level,
2963                             GLenum internalFormat, GLenum format, GLenum type)
2964 {
2965    mesa_format f = MESA_FORMAT_NONE;
2966 
2967    /* see if we've already chosen a format for the previous level */
2968    if (level > 0) {
2969       struct gl_texture_image *prevImage =
2970          _mesa_select_tex_image(texObj, target, level - 1);
2971       /* See if the prev level is defined and has an internal format which
2972        * matches the new internal format.
2973        */
2974       if (prevImage &&
2975           prevImage->Width > 0 &&
2976           prevImage->InternalFormat == internalFormat) {
2977          /* use the same format */
2978          assert(prevImage->TexFormat != MESA_FORMAT_NONE);
2979          return prevImage->TexFormat;
2980       }
2981    }
2982 
2983    f = st_ChooseTextureFormat(ctx, target, internalFormat,
2984                               format, type);
2985    assert(f != MESA_FORMAT_NONE);
2986    return f;
2987 }
2988 
2989 
2990 /**
2991  * Adjust pixel unpack params and image dimensions to strip off the
2992  * one-pixel texture border.
2993  *
2994  * Gallium and intel don't support texture borders.  They've seldem been used
2995  * and seldom been implemented correctly anyway.
2996  *
2997  * \param unpackNew returns the new pixel unpack parameters
2998  */
2999 static void
strip_texture_border(GLenum target,GLint * width,GLint * height,GLint * depth,const struct gl_pixelstore_attrib * unpack,struct gl_pixelstore_attrib * unpackNew)3000 strip_texture_border(GLenum target,
3001                      GLint *width, GLint *height, GLint *depth,
3002                      const struct gl_pixelstore_attrib *unpack,
3003                      struct gl_pixelstore_attrib *unpackNew)
3004 {
3005    assert(width);
3006    assert(height);
3007    assert(depth);
3008 
3009    *unpackNew = *unpack;
3010 
3011    if (unpackNew->RowLength == 0)
3012       unpackNew->RowLength = *width;
3013 
3014    if (unpackNew->ImageHeight == 0)
3015       unpackNew->ImageHeight = *height;
3016 
3017    assert(*width >= 3);
3018    unpackNew->SkipPixels++;  /* skip the border */
3019    *width = *width - 2;      /* reduce the width by two border pixels */
3020 
3021    /* The min height of a texture with a border is 3 */
3022    if (*height >= 3 && target != GL_TEXTURE_1D_ARRAY) {
3023       unpackNew->SkipRows++;  /* skip the border */
3024       *height = *height - 2;  /* reduce the height by two border pixels */
3025    }
3026 
3027    if (*depth >= 3 &&
3028        target != GL_TEXTURE_2D_ARRAY &&
3029        target != GL_TEXTURE_CUBE_MAP_ARRAY) {
3030       unpackNew->SkipImages++;  /* skip the border */
3031       *depth = *depth - 2;      /* reduce the depth by two border pixels */
3032    }
3033 }
3034 
3035 static struct gl_texture_object *
lookup_texture_ext_dsa(struct gl_context * ctx,GLenum target,GLuint texture,const char * caller)3036 lookup_texture_ext_dsa(struct gl_context *ctx, GLenum target, GLuint texture,
3037                        const char *caller)
3038 {
3039    GLenum boundTarget;
3040    switch (target) {
3041    case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
3042    case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
3043    case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
3044    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
3045    case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
3046    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
3047       boundTarget = GL_TEXTURE_CUBE_MAP;
3048       break;
3049    default:
3050       boundTarget = target;
3051       break;
3052    }
3053 
3054    int targetIndex = _mesa_tex_target_to_index(ctx, boundTarget);
3055    if (targetIndex < 0) {
3056       _mesa_error(ctx, GL_INVALID_ENUM, "%s(target = %s)", caller,
3057                   _mesa_enum_to_string(target));
3058             return NULL;
3059    }
3060    assert(targetIndex < NUM_TEXTURE_TARGETS);
3061 
3062    struct gl_texture_object *texObj;
3063    if (texture == 0) {
3064       /* Use a default texture object */
3065       texObj = ctx->Shared->DefaultTex[targetIndex];
3066       assert(texObj);
3067    } else {
3068       texObj = _mesa_lookup_texture(ctx, texture);
3069       if (!texObj && _mesa_is_desktop_gl_core(ctx)) {
3070          _mesa_error(ctx, GL_INVALID_OPERATION, "%s(non-gen name)", caller);
3071          return NULL;
3072       }
3073 
3074       if (!texObj) {
3075          texObj = _mesa_new_texture_object(ctx, texture, boundTarget);
3076          if (!texObj) {
3077             _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", caller);
3078             return NULL;
3079          }
3080 
3081          /* insert into hash table */
3082          _mesa_HashInsert(&ctx->Shared->TexObjects, texObj->Name, texObj);
3083       }
3084 
3085       if (texObj->Target != boundTarget) {
3086          _mesa_error(ctx, GL_INVALID_OPERATION, "%s(%s != %s)",
3087                      caller, _mesa_enum_to_string(texObj->Target),
3088                      _mesa_enum_to_string(target));
3089          return NULL;
3090       }
3091    }
3092 
3093    return texObj;
3094 }
3095 
3096 /**
3097  * Common code to implement all the glTexImage1D/2D/3D functions,
3098  * glCompressedTexImage1D/2D/3D and glTextureImage1D/2D/3DEXT
3099  * \param compressed  only GL_TRUE for glCompressedTexImage1D/2D/3D calls.
3100  * \param format  the user's image format (only used if !compressed)
3101  * \param type  the user's image type (only used if !compressed)
3102  * \param imageSize  only used for glCompressedTexImage1D/2D/3D calls.
3103  */
3104 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)3105 teximage(struct gl_context *ctx, GLboolean compressed, GLuint dims,
3106          struct gl_texture_object *texObj,
3107          GLenum target, GLint level, GLint internalFormat,
3108          GLsizei width, GLsizei height, GLsizei depth,
3109          GLint border, GLenum format, GLenum type,
3110          GLsizei imageSize, const GLvoid *pixels, bool no_error)
3111 {
3112    const char *func = compressed ? "glCompressedTexImage" : "glTexImage";
3113    struct gl_pixelstore_attrib unpack_no_border;
3114    const struct gl_pixelstore_attrib *unpack = &ctx->Unpack;
3115    mesa_format texFormat;
3116    bool dimensionsOK = true, sizeOK = true;
3117 
3118    FLUSH_VERTICES(ctx, 0, 0);
3119 
3120    if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE)) {
3121       if (compressed)
3122          _mesa_debug(ctx,
3123                      "glCompressedTexImage%uD %s %d %s %d %d %d %d %p\n",
3124                      dims,
3125                      _mesa_enum_to_string(target), level,
3126                      _mesa_enum_to_string(internalFormat),
3127                      width, height, depth, border, pixels);
3128       else
3129          _mesa_debug(ctx,
3130                      "glTexImage%uD %s %d %s %d %d %d %d %s %s %p\n",
3131                      dims,
3132                      _mesa_enum_to_string(target), level,
3133                      _mesa_enum_to_string(internalFormat),
3134                      width, height, depth, border,
3135                      _mesa_enum_to_string(format),
3136                      _mesa_enum_to_string(type), pixels);
3137    }
3138 
3139    internalFormat = override_internal_format(internalFormat, width, height);
3140 
3141    if (!no_error &&
3142        /* target error checking */
3143        !legal_teximage_target(ctx, dims, target)) {
3144       _mesa_error(ctx, GL_INVALID_ENUM, "%s%uD(target=%s)",
3145                   func, dims, _mesa_enum_to_string(target));
3146       return;
3147    }
3148 
3149    if (!texObj)
3150       texObj = _mesa_get_current_tex_object(ctx, target);
3151 
3152    if (!no_error) {
3153       /* general error checking */
3154       if (compressed) {
3155          if (compressed_texture_error_check(ctx, dims, target, texObj,
3156                                             level, internalFormat,
3157                                             width, height, depth,
3158                                             border, imageSize, pixels))
3159             return;
3160       } else {
3161          if (texture_error_check(ctx, dims, target, texObj, level, internalFormat,
3162                                  format, type, width, height, depth, border,
3163                                  pixels))
3164             return;
3165       }
3166    }
3167    assert(texObj);
3168 
3169    /* Here we convert a cpal compressed image into a regular glTexImage2D
3170     * call by decompressing the texture.  If we really want to support cpal
3171     * textures in any driver this would have to be changed.
3172     */
3173    if (_mesa_is_gles1(ctx) && compressed && dims == 2) {
3174       switch (internalFormat) {
3175       case GL_PALETTE4_RGB8_OES:
3176       case GL_PALETTE4_RGBA8_OES:
3177       case GL_PALETTE4_R5_G6_B5_OES:
3178       case GL_PALETTE4_RGBA4_OES:
3179       case GL_PALETTE4_RGB5_A1_OES:
3180       case GL_PALETTE8_RGB8_OES:
3181       case GL_PALETTE8_RGBA8_OES:
3182       case GL_PALETTE8_R5_G6_B5_OES:
3183       case GL_PALETTE8_RGBA4_OES:
3184       case GL_PALETTE8_RGB5_A1_OES:
3185          _mesa_cpal_compressed_teximage2d(target, level, internalFormat,
3186                                           width, height, imageSize, pixels);
3187          return;
3188       }
3189    }
3190 
3191    if (compressed) {
3192       /* For glCompressedTexImage() the driver has no choice about the
3193        * texture format since we'll never transcode the user's compressed
3194        * image data.  The internalFormat was error checked earlier.
3195        */
3196       texFormat = _mesa_glenum_to_compressed_format(internalFormat);
3197    }
3198    else {
3199       /* In case of HALF_FLOAT_OES or FLOAT_OES, find corresponding sized
3200        * internal floating point format for the given base format.
3201        */
3202       if (_mesa_is_gles(ctx) && format == internalFormat) {
3203          if (type == GL_FLOAT) {
3204             texObj->_IsFloat = GL_TRUE;
3205          } else if (type == GL_HALF_FLOAT_OES || type == GL_HALF_FLOAT) {
3206             texObj->_IsHalfFloat = GL_TRUE;
3207          }
3208 
3209          internalFormat = adjust_for_oes_float_texture(ctx, format, type);
3210       }
3211 
3212       texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
3213                                               internalFormat, format, type);
3214    }
3215 
3216    assert(texFormat != MESA_FORMAT_NONE);
3217 
3218    if (!no_error) {
3219       /* check that width, height, depth are legal for the mipmap level */
3220       dimensionsOK = _mesa_legal_texture_dimensions(ctx, target, level, width,
3221                                                     height, depth, border);
3222 
3223       /* check that the texture won't take too much memory, etc */
3224       sizeOK = st_TestProxyTexImage(ctx, proxy_target(target),
3225                                     0, level, texFormat, 1,
3226                                     width, height, depth);
3227    }
3228 
3229    if (_mesa_is_proxy_texture(target)) {
3230       /* Proxy texture: just clear or set state depending on error checking */
3231       struct gl_texture_image *texImage =
3232          get_proxy_tex_image(ctx, target, level);
3233 
3234       if (!texImage)
3235          return;  /* GL_OUT_OF_MEMORY already recorded */
3236 
3237       if (dimensionsOK && sizeOK) {
3238          _mesa_init_teximage_fields(ctx, texImage, width, height, depth,
3239                                     border, internalFormat, texFormat);
3240       }
3241       else {
3242          clear_teximage_fields(texImage);
3243       }
3244    }
3245    else {
3246       /* non-proxy target */
3247       const GLuint face = _mesa_tex_target_to_face(target);
3248       struct gl_texture_image *texImage;
3249 
3250       if (!dimensionsOK) {
3251          _mesa_error(ctx, GL_INVALID_VALUE,
3252                      "%s%uD(invalid width=%d or height=%d or depth=%d)",
3253                      func, dims, width, height, depth);
3254          return;
3255       }
3256 
3257       if (!sizeOK) {
3258          _mesa_error(ctx, GL_OUT_OF_MEMORY,
3259                      "%s%uD(image too large: %d x %d x %d, %s format)",
3260                      func, dims, width, height, depth,
3261                      _mesa_enum_to_string(internalFormat));
3262          return;
3263       }
3264 
3265       /* Allow a hardware driver to just strip out the border, to provide
3266        * reliable but slightly incorrect hardware rendering instead of
3267        * rarely-tested software fallback rendering.
3268        */
3269       if (border) {
3270          strip_texture_border(target, &width, &height, &depth, unpack,
3271                               &unpack_no_border);
3272          border = 0;
3273          unpack = &unpack_no_border;
3274       }
3275 
3276       _mesa_update_pixel(ctx);
3277 
3278       _mesa_lock_texture(ctx, texObj);
3279       {
3280          texObj->External = GL_FALSE;
3281 
3282          texImage = _mesa_get_tex_image(ctx, texObj, target, level);
3283 
3284          if (!texImage) {
3285             _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s%uD", func, dims);
3286          }
3287          else {
3288             st_FreeTextureImageBuffer(ctx, texImage);
3289 
3290             _mesa_init_teximage_fields(ctx, texImage,
3291                                        width, height, depth,
3292                                        border, internalFormat, texFormat);
3293 
3294             /* Give the texture to the driver.  <pixels> may be null. */
3295             if (width > 0 && height > 0 && depth > 0) {
3296                if (compressed) {
3297                   st_CompressedTexImage(ctx, dims, texImage,
3298                                         imageSize, pixels);
3299                }
3300                else {
3301                   st_TexImage(ctx, dims, texImage, format,
3302                               type, pixels, unpack);
3303                }
3304             }
3305 
3306             check_gen_mipmap(ctx, target, texObj, level);
3307 
3308             _mesa_update_fbo_texture(ctx, texObj, face, level);
3309 
3310             _mesa_dirty_texobj(ctx, texObj);
3311             /* only apply depthMode swizzle if it was explicitly changed */
3312             GLenum depth_mode = _mesa_is_desktop_gl_core(ctx) ? GL_RED : GL_LUMINANCE;
3313             if (texObj->Attrib.DepthMode != depth_mode)
3314                _mesa_update_teximage_format_swizzle(ctx, texObj->Image[0][texObj->Attrib.BaseLevel], texObj->Attrib.DepthMode);
3315             _mesa_update_texture_object_swizzle(ctx, texObj);
3316          }
3317       }
3318       _mesa_unlock_texture(ctx, texObj);
3319    }
3320 }
3321 
3322 
3323 /* This is a wrapper around teximage() so that we can force the KHR_no_error
3324  * logic to be inlined without inlining the function into all the callers.
3325  */
3326 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)3327 teximage_err(struct gl_context *ctx, GLboolean compressed, GLuint dims,
3328              GLenum target, GLint level, GLint internalFormat,
3329              GLsizei width, GLsizei height, GLsizei depth,
3330              GLint border, GLenum format, GLenum type,
3331              GLsizei imageSize, const GLvoid *pixels)
3332 {
3333    teximage(ctx, compressed, dims, NULL, target, level, internalFormat, width, height,
3334             depth, border, format, type, imageSize, pixels, false);
3335 }
3336 
3337 
3338 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)3339 teximage_no_error(struct gl_context *ctx, GLboolean compressed, GLuint dims,
3340                   GLenum target, GLint level, GLint internalFormat,
3341                   GLsizei width, GLsizei height, GLsizei depth,
3342                   GLint border, GLenum format, GLenum type,
3343                   GLsizei imageSize, const GLvoid *pixels)
3344 {
3345    teximage(ctx, compressed, dims, NULL, target, level, internalFormat, width, height,
3346             depth, border, format, type, imageSize, pixels, true);
3347 }
3348 
3349 
3350 /*
3351  * Called from the API.  Note that width includes the border.
3352  */
3353 void GLAPIENTRY
_mesa_TexImage1D(GLenum target,GLint level,GLint internalFormat,GLsizei width,GLint border,GLenum format,GLenum type,const GLvoid * pixels)3354 _mesa_TexImage1D( GLenum target, GLint level, GLint internalFormat,
3355                   GLsizei width, GLint border, GLenum format,
3356                   GLenum type, const GLvoid *pixels )
3357 {
3358    GET_CURRENT_CONTEXT(ctx);
3359    teximage_err(ctx, GL_FALSE, 1, target, level, internalFormat, width, 1, 1,
3360                 border, format, type, 0, pixels);
3361 }
3362 
3363 void GLAPIENTRY
_mesa_TextureImage1DEXT(GLuint texture,GLenum target,GLint level,GLint internalFormat,GLsizei width,GLint border,GLenum format,GLenum type,const GLvoid * pixels)3364 _mesa_TextureImage1DEXT(GLuint texture, GLenum target, GLint level,
3365                       GLint internalFormat, GLsizei width, GLint border,
3366                       GLenum format, GLenum type, const GLvoid *pixels )
3367 {
3368    struct gl_texture_object*  texObj;
3369    GET_CURRENT_CONTEXT(ctx);
3370 
3371    texObj = _mesa_lookup_or_create_texture(ctx, target, texture, false, true,
3372                                            "glTextureImage1DEXT");
3373    if (!texObj)
3374       return;
3375    teximage(ctx, GL_FALSE, 1, texObj, target, level, internalFormat,
3376             width, 1, 1, border, format, type, 0, pixels, false);
3377 }
3378 
3379 void GLAPIENTRY
_mesa_MultiTexImage1DEXT(GLenum texunit,GLenum target,GLint level,GLint internalFormat,GLsizei width,GLint border,GLenum format,GLenum type,const GLvoid * pixels)3380 _mesa_MultiTexImage1DEXT(GLenum texunit, GLenum target, GLint level,
3381                          GLint internalFormat, GLsizei width, GLint border,
3382                          GLenum format, GLenum type, const GLvoid *pixels )
3383 {
3384    struct gl_texture_object*  texObj;
3385    GET_CURRENT_CONTEXT(ctx);
3386 
3387    texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
3388                                                    texunit - GL_TEXTURE0,
3389                                                    true,
3390                                                    "glMultiTexImage1DEXT");
3391    if (!texObj)
3392       return;
3393    teximage(ctx, GL_FALSE, 1, texObj, target, level, internalFormat, width, 1, 1,
3394                 border, format, type, 0, pixels, false);
3395 }
3396 
3397 void GLAPIENTRY
_mesa_TexImage2D(GLenum target,GLint level,GLint internalFormat,GLsizei width,GLsizei height,GLint border,GLenum format,GLenum type,const GLvoid * pixels)3398 _mesa_TexImage2D( GLenum target, GLint level, GLint internalFormat,
3399                   GLsizei width, GLsizei height, GLint border,
3400                   GLenum format, GLenum type,
3401                   const GLvoid *pixels )
3402 {
3403    GET_CURRENT_CONTEXT(ctx);
3404    teximage_err(ctx, GL_FALSE, 2, target, level, internalFormat, width, height, 1,
3405                 border, format, type, 0, pixels);
3406 }
3407 
3408 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)3409 _mesa_TextureImage2DEXT(GLuint texture, GLenum target, GLint level,
3410                       GLint internalFormat, GLsizei width, GLsizei height,
3411                       GLint border,
3412                       GLenum format, GLenum type, const GLvoid *pixels )
3413 {
3414    struct gl_texture_object*  texObj;
3415    GET_CURRENT_CONTEXT(ctx);
3416 
3417    texObj = _mesa_lookup_or_create_texture(ctx, target, texture, false, true,
3418                                            "glTextureImage2DEXT");
3419    if (!texObj)
3420       return;
3421    teximage(ctx, GL_FALSE, 2, texObj, target, level, internalFormat,
3422             width, height, 1, border, format, type, 0, pixels, false);
3423 }
3424 
3425 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)3426 _mesa_MultiTexImage2DEXT(GLenum texunit, GLenum target, GLint level,
3427                          GLint internalFormat, GLsizei width, GLsizei height,
3428                          GLint border,
3429                          GLenum format, GLenum type, const GLvoid *pixels )
3430 {
3431    struct gl_texture_object*  texObj;
3432    GET_CURRENT_CONTEXT(ctx);
3433 
3434    texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
3435                                                    texunit - GL_TEXTURE0,
3436                                                    true,
3437                                                    "glMultiTexImage2DEXT");
3438    if (!texObj)
3439       return;
3440    teximage(ctx, GL_FALSE, 2, texObj, target, level, internalFormat, width, height, 1,
3441                 border, format, type, 0, pixels, false);
3442 }
3443 
3444 /*
3445  * Called by the API or display list executor.
3446  * Note that width and height include the border.
3447  */
3448 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)3449 _mesa_TexImage3D( GLenum target, GLint level, GLint internalFormat,
3450                   GLsizei width, GLsizei height, GLsizei depth,
3451                   GLint border, GLenum format, GLenum type,
3452                   const GLvoid *pixels )
3453 {
3454    GET_CURRENT_CONTEXT(ctx);
3455    teximage_err(ctx, GL_FALSE, 3, target, level, internalFormat,
3456                 width, height, depth, border, format, type, 0, pixels);
3457 }
3458 
3459 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)3460 _mesa_TextureImage3DEXT(GLuint texture, GLenum target, GLint level,
3461                       GLint internalFormat, GLsizei width, GLsizei height,
3462                       GLsizei depth, GLint border,
3463                       GLenum format, GLenum type, const GLvoid *pixels )
3464 {
3465    struct gl_texture_object*  texObj;
3466    GET_CURRENT_CONTEXT(ctx);
3467 
3468    texObj = _mesa_lookup_or_create_texture(ctx, target, texture, false, true,
3469                                            "glTextureImage3DEXT");
3470    if (!texObj)
3471       return;
3472    teximage(ctx, GL_FALSE, 3, texObj, target, level, internalFormat,
3473             width, height, depth, border, format, type, 0, pixels, false);
3474 }
3475 
3476 
3477 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)3478 _mesa_MultiTexImage3DEXT(GLenum texunit, GLenum target, GLint level,
3479                          GLint internalFormat, GLsizei width, GLsizei height,
3480                          GLsizei depth, GLint border, GLenum format, GLenum type,
3481                          const GLvoid *pixels )
3482 {
3483    struct gl_texture_object*  texObj;
3484    GET_CURRENT_CONTEXT(ctx);
3485 
3486    texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
3487                                                    texunit - GL_TEXTURE0,
3488                                                    true,
3489                                                    "glMultiTexImage3DEXT");
3490    if (!texObj)
3491       return;
3492    teximage(ctx, GL_FALSE, 3, texObj, target, level, internalFormat,
3493                 width, height, depth, border, format, type, 0, pixels, false);
3494 }
3495 
3496 
3497 void GLAPIENTRY
_mesa_TexImage1D_no_error(GLenum target,GLint level,GLint internalFormat,GLsizei width,GLint border,GLenum format,GLenum type,const GLvoid * pixels)3498 _mesa_TexImage1D_no_error(GLenum target, GLint level, GLint internalFormat,
3499                           GLsizei width, GLint border, GLenum format,
3500                           GLenum type, const GLvoid *pixels)
3501 {
3502    GET_CURRENT_CONTEXT(ctx);
3503    teximage_no_error(ctx, GL_FALSE, 1, target, level, internalFormat, width, 1,
3504                      1, border, format, type, 0, pixels);
3505 }
3506 
3507 
3508 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)3509 _mesa_TexImage2D_no_error(GLenum target, GLint level, GLint internalFormat,
3510                           GLsizei width, GLsizei height, GLint border,
3511                           GLenum format, GLenum type, const GLvoid *pixels)
3512 {
3513    GET_CURRENT_CONTEXT(ctx);
3514    teximage_no_error(ctx, GL_FALSE, 2, target, level, internalFormat, width,
3515                      height, 1, border, format, type, 0, pixels);
3516 }
3517 
3518 
3519 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)3520 _mesa_TexImage3D_no_error(GLenum target, GLint level, GLint internalFormat,
3521                           GLsizei width, GLsizei height, GLsizei depth,
3522                           GLint border, GLenum format, GLenum type,
3523                           const GLvoid *pixels )
3524 {
3525    GET_CURRENT_CONTEXT(ctx);
3526    teximage_no_error(ctx, GL_FALSE, 3, target, level, internalFormat,
3527                      width, height, depth, border, format, type, 0, pixels);
3528 }
3529 
3530 /*
3531  * Helper used by __mesa_EGLImageTargetTexture2DOES and
3532  * _mesa_EGLImageTargetTexStorageEXT.
3533  */
3534 static void
egl_image_target_texture(struct gl_context * ctx,struct gl_texture_object * texObj,GLenum target,GLeglImageOES image,bool tex_storage,const char * caller)3535 egl_image_target_texture(struct gl_context *ctx,
3536                          struct gl_texture_object *texObj, GLenum target,
3537                          GLeglImageOES image, bool tex_storage,
3538                          const char *caller)
3539 {
3540    struct gl_texture_image *texImage;
3541    FLUSH_VERTICES(ctx, 0, 0);
3542 
3543    if (!texObj)
3544       texObj = _mesa_get_current_tex_object(ctx, target);
3545    if (!texObj)
3546       return;
3547 
3548    if (!image || (ctx->Driver.ValidateEGLImage &&
3549                   !ctx->Driver.ValidateEGLImage(ctx, image))) {
3550       _mesa_error(ctx, GL_INVALID_VALUE, "%s(image=%p)", caller, image);
3551       return;
3552    }
3553 
3554    _mesa_lock_texture(ctx, texObj);
3555 
3556    if (texObj->Immutable) {
3557       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(texture is immutable)", caller);
3558       _mesa_unlock_texture(ctx, texObj);
3559       return;
3560    }
3561 
3562    texImage = _mesa_get_tex_image(ctx, texObj, target, 0);
3563    if (!texImage) {
3564       _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", caller);
3565    } else {
3566       st_FreeTextureImageBuffer(ctx, texImage);
3567 
3568       texObj->External = GL_TRUE;
3569 
3570       struct st_egl_image stimg;
3571       bool native_supported;
3572       if (!st_get_egl_image(ctx, image, PIPE_BIND_SAMPLER_VIEW, caller,
3573                             &stimg, &native_supported)) {
3574          _mesa_unlock_texture(ctx, texObj);
3575          return;
3576       }
3577 
3578       if (tex_storage) {
3579          /* EXT_EGL_image_storage
3580           * If the EGL image was created using EGL_EXT_image_dma_buf_import,
3581           * then the following applies:
3582           *    - <target> must be GL_TEXTURE_2D or GL_TEXTURE_EXTERNAL_OES.
3583           *    Otherwise, the error INVALID_OPERATION is generated.
3584           */
3585          if (stimg.imported_dmabuf &&
3586              !(target == GL_TEXTURE_2D || target == GL_TEXTURE_EXTERNAL_OES)) {
3587             _mesa_error(ctx, GL_INVALID_OPERATION,
3588                         "%s(texture is imported from dmabuf)", caller);
3589             pipe_resource_reference(&stimg.texture, NULL);
3590             _mesa_unlock_texture(ctx, texObj);
3591             return;
3592          }
3593          st_bind_egl_image(ctx, texObj, texImage, &stimg, true, native_supported);
3594       } else {
3595          st_bind_egl_image(ctx, texObj, texImage, &stimg,
3596                            target != GL_TEXTURE_EXTERNAL_OES, native_supported);
3597       }
3598 
3599       pipe_resource_reference(&stimg.texture, NULL);
3600 
3601       _mesa_dirty_texobj(ctx, texObj);
3602    }
3603 
3604    if (tex_storage)
3605       _mesa_set_texture_view_state(ctx, texObj, target, 1);
3606 
3607    _mesa_update_fbo_texture(ctx, texObj, 0, 0);
3608 
3609    _mesa_unlock_texture(ctx, texObj);
3610 }
3611 
3612 void GLAPIENTRY
_mesa_EGLImageTargetTexture2DOES(GLenum target,GLeglImageOES image)3613 _mesa_EGLImageTargetTexture2DOES(GLenum target, GLeglImageOES image)
3614 {
3615    const char *func = "glEGLImageTargetTexture2D";
3616    GET_CURRENT_CONTEXT(ctx);
3617 
3618    bool valid_target;
3619    switch (target) {
3620    case GL_TEXTURE_2D:
3621       valid_target = _mesa_has_OES_EGL_image(ctx);
3622       break;
3623    case GL_TEXTURE_EXTERNAL_OES:
3624       valid_target = _mesa_has_OES_EGL_image_external(ctx);
3625       break;
3626    default:
3627       valid_target = false;
3628       break;
3629    }
3630 
3631    if (valid_target) {
3632       egl_image_target_texture(ctx, NULL, target, image, false, func);
3633    } else {
3634       _mesa_error(ctx, GL_INVALID_ENUM, "%s(target=%d)", func, target);
3635    }
3636 }
3637 
3638 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)3639 egl_image_target_texture_storage(struct gl_context *ctx,
3640                                  struct gl_texture_object *texObj, GLenum target,
3641                                  GLeglImageOES image, const GLint *attrib_list,
3642                                  const char *caller)
3643 {
3644    /*
3645     * EXT_EGL_image_storage:
3646     *
3647     * "<attrib_list> must be NULL or a pointer to the value GL_NONE."
3648     */
3649    if (attrib_list && attrib_list[0] != GL_NONE) {
3650       _mesa_error(ctx, GL_INVALID_VALUE, "%s(image=%p)", caller, image);
3651       return;
3652    }
3653 
3654    /*
3655     * <target> must be one of GL_TEXTURE_2D, GL_TEXTURE_2D_ARRAY,
3656     * GL_TEXTURE_3D, GL_TEXTURE_CUBE_MAP, GL_TEXTURE_CUBE_MAP_ARRAY. On
3657     * OpenGL implementations (non-ES), <target> can also be GL_TEXTURE_1D or
3658     * GL_TEXTURE_1D_ARRAY.
3659     * If the implementation supports OES_EGL_image_external, <target> can be
3660     * GL_TEXTURE_EXTERNAL_OES.
3661     */
3662    bool valid_target;
3663    switch (target) {
3664    case GL_TEXTURE_2D:
3665    case GL_TEXTURE_2D_ARRAY:
3666    case GL_TEXTURE_3D:
3667    case GL_TEXTURE_CUBE_MAP:
3668    case GL_TEXTURE_CUBE_MAP_ARRAY:
3669       valid_target = true;
3670       break;
3671    case GL_TEXTURE_1D:
3672    case GL_TEXTURE_1D_ARRAY:
3673       /* No eglImageOES for 1D textures */
3674       valid_target = !_mesa_is_gles(ctx);
3675       break;
3676    case GL_TEXTURE_EXTERNAL_OES:
3677       valid_target = _mesa_has_OES_EGL_image_external(ctx);
3678       break;
3679    default:
3680       valid_target = false;
3681       break;
3682    }
3683 
3684    if (valid_target) {
3685       egl_image_target_texture(ctx, texObj, target, image, true, caller);
3686    } else {
3687       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(target=%d)", caller, target);
3688    }
3689 
3690 }
3691 
3692 
3693 void GLAPIENTRY
_mesa_EGLImageTargetTexStorageEXT(GLenum target,GLeglImageOES image,const GLint * attrib_list)3694 _mesa_EGLImageTargetTexStorageEXT(GLenum target, GLeglImageOES image,
3695                                   const GLint *attrib_list)
3696 {
3697    const char *func = "glEGLImageTargetTexStorageEXT";
3698    GET_CURRENT_CONTEXT(ctx);
3699 
3700    if (!(_mesa_is_desktop_gl(ctx) && ctx->Version >= 42) &&
3701        !_mesa_is_gles3(ctx) && !_mesa_has_ARB_texture_storage(ctx)) {
3702       _mesa_error(ctx, GL_INVALID_OPERATION,
3703          "OpenGL 4.2, OpenGL ES 3.0 or ARB_texture_storage required");
3704       return;
3705    }
3706 
3707    egl_image_target_texture_storage(ctx, NULL, target, image, attrib_list,
3708                                     func);
3709 }
3710 
3711 void GLAPIENTRY
_mesa_EGLImageTargetTextureStorageEXT(GLuint texture,GLeglImageOES image,const GLint * attrib_list)3712 _mesa_EGLImageTargetTextureStorageEXT(GLuint texture, GLeglImageOES image,
3713                                       const GLint *attrib_list)
3714 {
3715    struct gl_texture_object *texObj;
3716    const char *func = "glEGLImageTargetTextureStorageEXT";
3717    GET_CURRENT_CONTEXT(ctx);
3718 
3719    if (!_mesa_has_ARB_direct_state_access(ctx) &&
3720        !_mesa_has_EXT_direct_state_access(ctx)) {
3721       _mesa_error(ctx, GL_INVALID_OPERATION, "direct access not supported");
3722       return;
3723    }
3724 
3725    if (!(_mesa_is_desktop_gl(ctx) && ctx->Version >= 42) &&
3726        !_mesa_is_gles3(ctx) && !_mesa_has_ARB_texture_storage(ctx)) {
3727       _mesa_error(ctx, GL_INVALID_OPERATION,
3728          "OpenGL 4.2, OpenGL ES 3.0 or ARB_texture_storage required");
3729       return;
3730    }
3731 
3732    texObj = _mesa_lookup_texture_err(ctx, texture, func);
3733    if (!texObj)
3734       return;
3735 
3736    egl_image_target_texture_storage(ctx, texObj, texObj->Target, image,
3737                                     attrib_list, func);
3738 }
3739 
3740 /**
3741  * Helper that implements the glTexSubImage1/2/3D()
3742  * and glTextureSubImage1/2/3D() functions.
3743  */
3744 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)3745 texture_sub_image(struct gl_context *ctx, GLuint dims,
3746                   struct gl_texture_object *texObj,
3747                   struct gl_texture_image *texImage,
3748                   GLenum target, GLint level,
3749                   GLint xoffset, GLint yoffset, GLint zoffset,
3750                   GLsizei width, GLsizei height, GLsizei depth,
3751                   GLenum format, GLenum type, const GLvoid *pixels)
3752 {
3753    FLUSH_VERTICES(ctx, 0, 0);
3754 
3755    _mesa_update_pixel(ctx);
3756 
3757    _mesa_lock_texture(ctx, texObj);
3758    {
3759       if (width > 0 && height > 0 && depth > 0) {
3760          /* If we have a border, offset=-1 is legal.  Bias by border width. */
3761          switch (dims) {
3762          case 3:
3763             if (target != GL_TEXTURE_2D_ARRAY)
3764                zoffset += texImage->Border;
3765             FALLTHROUGH;
3766          case 2:
3767             if (target != GL_TEXTURE_1D_ARRAY)
3768                yoffset += texImage->Border;
3769             FALLTHROUGH;
3770          case 1:
3771             xoffset += texImage->Border;
3772          }
3773 
3774          st_TexSubImage(ctx, dims, texImage,
3775                         xoffset, yoffset, zoffset,
3776                         width, height, depth,
3777                         format, type, pixels, &ctx->Unpack);
3778 
3779          check_gen_mipmap(ctx, target, texObj, level);
3780 
3781          /* NOTE: Don't signal _NEW_TEXTURE_OBJECT since we've only changed
3782           * the texel data, not the texture format, size, etc.
3783           */
3784       }
3785    }
3786    _mesa_unlock_texture(ctx, texObj);
3787 }
3788 
3789 /**
3790  * Implement all the glTexSubImage1/2/3D() functions.
3791  * Must split this out this way because of GL_TEXTURE_CUBE_MAP.
3792  */
3793 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)3794 texsubimage_err(struct gl_context *ctx, GLuint dims, GLenum target, GLint level,
3795                 GLint xoffset, GLint yoffset, GLint zoffset,
3796                 GLsizei width, GLsizei height, GLsizei depth,
3797                 GLenum format, GLenum type, const GLvoid *pixels,
3798                 const char *callerName)
3799 {
3800    struct gl_texture_object *texObj;
3801    struct gl_texture_image *texImage;
3802 
3803    /* check target (proxies not allowed) */
3804    if (!legal_texsubimage_target(ctx, dims, target, false)) {
3805       _mesa_error(ctx, GL_INVALID_ENUM, "glTexSubImage%uD(target=%s)",
3806                   dims, _mesa_enum_to_string(target));
3807       return;
3808    }
3809 
3810    texObj = _mesa_get_current_tex_object(ctx, target);
3811    if (!texObj)
3812       return;
3813 
3814    if (texsubimage_error_check(ctx, dims, texObj, target, level,
3815                                xoffset, yoffset, zoffset,
3816                                width, height, depth, format, type,
3817                                pixels, callerName)) {
3818       return;   /* error was detected */
3819    }
3820 
3821    texImage = _mesa_select_tex_image(texObj, target, level);
3822    /* texsubimage_error_check ensures that texImage is not NULL */
3823 
3824    if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3825       _mesa_debug(ctx, "glTexSubImage%uD %s %d %d %d %d %d %d %d %s %s %p\n",
3826                   dims,
3827                   _mesa_enum_to_string(target), level,
3828                   xoffset, yoffset, zoffset, width, height, depth,
3829                   _mesa_enum_to_string(format),
3830                   _mesa_enum_to_string(type), pixels);
3831 
3832    texture_sub_image(ctx, dims, texObj, texImage, target, level,
3833                      xoffset, yoffset, zoffset, width, height, depth,
3834                      format, type, pixels);
3835 }
3836 
3837 
3838 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)3839 texsubimage(struct gl_context *ctx, GLuint dims, GLenum target, GLint level,
3840             GLint xoffset, GLint yoffset, GLint zoffset,
3841             GLsizei width, GLsizei height, GLsizei depth,
3842             GLenum format, GLenum type, const GLvoid *pixels)
3843 {
3844    struct gl_texture_object *texObj;
3845    struct gl_texture_image *texImage;
3846 
3847    texObj = _mesa_get_current_tex_object(ctx, target);
3848    texImage = _mesa_select_tex_image(texObj, target, level);
3849 
3850    texture_sub_image(ctx, dims, texObj, texImage, target, level,
3851                      xoffset, yoffset, zoffset, width, height, depth,
3852                      format, type, pixels);
3853 }
3854 
3855 
3856 /**
3857  * Implement all the glTextureSubImage1/2/3D() functions.
3858  * Must split this out this way because of GL_TEXTURE_CUBE_MAP.
3859  */
3860 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)3861 texturesubimage(struct gl_context *ctx, GLuint dims,
3862                 GLuint texture, GLenum target, GLint level,
3863                 GLint xoffset, GLint yoffset, GLint zoffset,
3864                 GLsizei width, GLsizei height, GLsizei depth,
3865                 GLenum format, GLenum type, const GLvoid *pixels,
3866                 const char *callerName, bool no_error, bool ext_dsa)
3867 {
3868    struct gl_texture_object *texObj;
3869    struct gl_texture_image *texImage;
3870    int i;
3871 
3872    if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3873       _mesa_debug(ctx,
3874                   "glTextureSubImage%uD %d %d %d %d %d %d %d %d %s %s %p\n",
3875                   dims, texture, level,
3876                   xoffset, yoffset, zoffset, width, height, depth,
3877                   _mesa_enum_to_string(format),
3878                   _mesa_enum_to_string(type), pixels);
3879 
3880    /* Get the texture object by Name. */
3881    if (!no_error) {
3882       if (!ext_dsa) {
3883          texObj = _mesa_lookup_texture_err(ctx, texture, callerName);
3884       } else {
3885          texObj = lookup_texture_ext_dsa(ctx, target, texture, callerName);
3886       }
3887       if (!texObj)
3888          return;
3889    } else {
3890       texObj = _mesa_lookup_texture(ctx, texture);
3891    }
3892 
3893    if (!no_error) {
3894       /* check target (proxies not allowed) */
3895       if (!legal_texsubimage_target(ctx, dims, texObj->Target, true)) {
3896          _mesa_error(ctx, GL_INVALID_OPERATION, "%s(target=%s)",
3897                      callerName, _mesa_enum_to_string(texObj->Target));
3898          return;
3899       }
3900 
3901       if (texsubimage_error_check(ctx, dims, texObj, texObj->Target, level,
3902                                   xoffset, yoffset, zoffset,
3903                                   width, height, depth, format, type,
3904                                   pixels, callerName)) {
3905          return;   /* error was detected */
3906       }
3907    }
3908 
3909    /* Must handle special case GL_TEXTURE_CUBE_MAP. */
3910    if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
3911       intptr_t imageStride;
3912 
3913       /*
3914        * What do we do if the user created a texture with the following code
3915        * and then called this function with its handle?
3916        *
3917        *    GLuint tex;
3918        *    glCreateTextures(GL_TEXTURE_CUBE_MAP, 1, &tex);
3919        *    glBindTexture(GL_TEXTURE_CUBE_MAP, tex);
3920        *    glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, ...);
3921        *    glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, ...);
3922        *    glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, ...);
3923        *    // Note: GL_TEXTURE_CUBE_MAP_NEGATIVE_Y not set, or given the
3924        *    // wrong format, or given the wrong size, etc.
3925        *    glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, ...);
3926        *    glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, ...);
3927        *
3928        * A bug has been filed against the spec for this case.  In the
3929        * meantime, we will check for cube completeness.
3930        *
3931        * According to Section 8.17 Texture Completeness in the OpenGL 4.5
3932        * Core Profile spec (30.10.2014):
3933        *    "[A] cube map texture is cube complete if the
3934        *    following conditions all hold true: The [base level] texture
3935        *    images of each of the six cube map faces have identical, positive,
3936        *    and square dimensions. The [base level] images were each specified
3937        *    with the same internal format."
3938        *
3939        * It seems reasonable to check for cube completeness of an arbitrary
3940        * level here so that the image data has a consistent format and size.
3941        */
3942       if (!no_error && !_mesa_cube_level_complete(texObj, level)) {
3943          _mesa_error(ctx, GL_INVALID_OPERATION,
3944                      "glTextureSubImage%uD(cube map incomplete)",
3945                      dims);
3946          return;
3947       }
3948 
3949       imageStride = _mesa_image_image_stride(&ctx->Unpack, width, height,
3950                                              format, type);
3951       /* Copy in each face. */
3952       for (i = zoffset; i < zoffset + depth; ++i) {
3953          texImage = texObj->Image[i][level];
3954          assert(texImage);
3955 
3956          texture_sub_image(ctx, 3, texObj, texImage, texObj->Target,
3957                            level, xoffset, yoffset, 0,
3958                            width, height, 1, format,
3959                            type, pixels);
3960          pixels = (GLubyte *) pixels + imageStride;
3961       }
3962    }
3963    else {
3964       texImage = _mesa_select_tex_image(texObj, texObj->Target, level);
3965       assert(texImage);
3966 
3967       texture_sub_image(ctx, dims, texObj, texImage, texObj->Target,
3968                         level, xoffset, yoffset, zoffset,
3969                         width, height, depth, format,
3970                         type, pixels);
3971    }
3972 }
3973 
3974 
3975 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)3976 texturesubimage_error(struct gl_context *ctx, GLuint dims,
3977                       GLuint texture, GLenum target, GLint level,
3978                       GLint xoffset, GLint yoffset, GLint zoffset,
3979                       GLsizei width, GLsizei height, GLsizei depth,
3980                       GLenum format, GLenum type, const GLvoid *pixels,
3981                       const char *callerName, bool ext_dsa)
3982 {
3983    texturesubimage(ctx, dims, texture, target, level, xoffset, yoffset,
3984                    zoffset, width, height, depth, format, type, pixels,
3985                    callerName, false, ext_dsa);
3986 }
3987 
3988 
3989 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)3990 texturesubimage_no_error(struct gl_context *ctx, GLuint dims,
3991                          GLuint texture, GLenum target, GLint level,
3992                          GLint xoffset, GLint yoffset, GLint zoffset,
3993                          GLsizei width, GLsizei height, GLsizei depth,
3994                          GLenum format, GLenum type, const GLvoid *pixels,
3995                          const char *callerName, bool ext_dsa)
3996 {
3997    texturesubimage(ctx, dims, texture, target, level, xoffset, yoffset,
3998                    zoffset, width, height, depth, format, type, pixels,
3999                    callerName, true, ext_dsa);
4000 }
4001 
4002 
4003 void GLAPIENTRY
_mesa_TexSubImage1D_no_error(GLenum target,GLint level,GLint xoffset,GLsizei width,GLenum format,GLenum type,const GLvoid * pixels)4004 _mesa_TexSubImage1D_no_error(GLenum target, GLint level,
4005                              GLint xoffset, GLsizei width,
4006                              GLenum format, GLenum type,
4007                              const GLvoid *pixels)
4008 {
4009    GET_CURRENT_CONTEXT(ctx);
4010    texsubimage(ctx, 1, target, level,
4011                xoffset, 0, 0,
4012                width, 1, 1,
4013                format, type, pixels);
4014 }
4015 
4016 
4017 void GLAPIENTRY
_mesa_TexSubImage1D(GLenum target,GLint level,GLint xoffset,GLsizei width,GLenum format,GLenum type,const GLvoid * pixels)4018 _mesa_TexSubImage1D( GLenum target, GLint level,
4019                      GLint xoffset, GLsizei width,
4020                      GLenum format, GLenum type,
4021                      const GLvoid *pixels )
4022 {
4023    GET_CURRENT_CONTEXT(ctx);
4024    texsubimage_err(ctx, 1, target, level,
4025                    xoffset, 0, 0,
4026                    width, 1, 1,
4027                    format, type, pixels, "glTexSubImage1D");
4028 }
4029 
4030 
4031 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)4032 _mesa_TexSubImage2D_no_error(GLenum target, GLint level,
4033                              GLint xoffset, GLint yoffset,
4034                              GLsizei width, GLsizei height,
4035                              GLenum format, GLenum type,
4036                              const GLvoid *pixels)
4037 {
4038    GET_CURRENT_CONTEXT(ctx);
4039    texsubimage(ctx, 2, target, level,
4040                xoffset, yoffset, 0,
4041                width, height, 1,
4042                format, type, pixels);
4043 }
4044 
4045 
4046 void GLAPIENTRY
_mesa_TexSubImage2D(GLenum target,GLint level,GLint xoffset,GLint yoffset,GLsizei width,GLsizei height,GLenum format,GLenum type,const GLvoid * pixels)4047 _mesa_TexSubImage2D( GLenum target, GLint level,
4048                      GLint xoffset, GLint yoffset,
4049                      GLsizei width, GLsizei height,
4050                      GLenum format, GLenum type,
4051                      const GLvoid *pixels )
4052 {
4053    GET_CURRENT_CONTEXT(ctx);
4054    texsubimage_err(ctx, 2, target, level,
4055                    xoffset, yoffset, 0,
4056                    width, height, 1,
4057                    format, type, pixels, "glTexSubImage2D");
4058 }
4059 
4060 
4061 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)4062 _mesa_TexSubImage3D_no_error(GLenum target, GLint level,
4063                              GLint xoffset, GLint yoffset, GLint zoffset,
4064                              GLsizei width, GLsizei height, GLsizei depth,
4065                              GLenum format, GLenum type,
4066                              const GLvoid *pixels)
4067 {
4068    GET_CURRENT_CONTEXT(ctx);
4069    texsubimage(ctx, 3, target, level,
4070                xoffset, yoffset, zoffset,
4071                width, height, depth,
4072                format, type, pixels);
4073 }
4074 
4075 
4076 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)4077 _mesa_TexSubImage3D( GLenum target, GLint level,
4078                      GLint xoffset, GLint yoffset, GLint zoffset,
4079                      GLsizei width, GLsizei height, GLsizei depth,
4080                      GLenum format, GLenum type,
4081                      const GLvoid *pixels )
4082 {
4083    GET_CURRENT_CONTEXT(ctx);
4084    texsubimage_err(ctx, 3, target, level,
4085                    xoffset, yoffset, zoffset,
4086                    width, height, depth,
4087                    format, type, pixels, "glTexSubImage3D");
4088 }
4089 
4090 
4091 void GLAPIENTRY
_mesa_TextureSubImage1D_no_error(GLuint texture,GLint level,GLint xoffset,GLsizei width,GLenum format,GLenum type,const GLvoid * pixels)4092 _mesa_TextureSubImage1D_no_error(GLuint texture, GLint level, GLint xoffset,
4093                                  GLsizei width, GLenum format, GLenum type,
4094                                  const GLvoid *pixels)
4095 {
4096    GET_CURRENT_CONTEXT(ctx);
4097    texturesubimage_no_error(ctx, 1, texture, 0, level, xoffset, 0, 0, width,
4098                             1, 1, format, type, pixels, "glTextureSubImage1D",
4099                             false);
4100 }
4101 
4102 
4103 void GLAPIENTRY
_mesa_TextureSubImage1DEXT(GLuint texture,GLenum target,GLint level,GLint xoffset,GLsizei width,GLenum format,GLenum type,const GLvoid * pixels)4104 _mesa_TextureSubImage1DEXT(GLuint texture, GLenum target, GLint level,
4105                         GLint xoffset, GLsizei width,
4106                         GLenum format, GLenum type,
4107                         const GLvoid *pixels)
4108 {
4109    GET_CURRENT_CONTEXT(ctx);
4110    texturesubimage_error(ctx, 1, texture, target, level, xoffset, 0, 0, width, 1,
4111                          1, format, type, pixels, "glTextureSubImage1DEXT",
4112                          false);
4113 }
4114 
4115 
4116 void GLAPIENTRY
_mesa_MultiTexSubImage1DEXT(GLenum texunit,GLenum target,GLint level,GLint xoffset,GLsizei width,GLenum format,GLenum type,const GLvoid * pixels)4117 _mesa_MultiTexSubImage1DEXT(GLenum texunit, GLenum target, GLint level,
4118                             GLint xoffset, GLsizei width,
4119                             GLenum format, GLenum type,
4120                             const GLvoid *pixels)
4121 {
4122    GET_CURRENT_CONTEXT(ctx);
4123    struct gl_texture_object *texObj;
4124    struct gl_texture_image *texImage;
4125 
4126    texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
4127                                                    texunit - GL_TEXTURE0,
4128                                                    false,
4129                                                    "glMultiTexImage1DEXT");
4130    texImage = _mesa_select_tex_image(texObj, target, level);
4131 
4132    texture_sub_image(ctx, 1, texObj, texImage, target, level,
4133                      xoffset, 0, 0, width, 1, 1,
4134                      format, type, pixels);
4135 }
4136 
4137 
4138 void GLAPIENTRY
_mesa_TextureSubImage1D(GLuint texture,GLint level,GLint xoffset,GLsizei width,GLenum format,GLenum type,const GLvoid * pixels)4139 _mesa_TextureSubImage1D(GLuint texture, GLint level,
4140                         GLint xoffset, GLsizei width,
4141                         GLenum format, GLenum type,
4142                         const GLvoid *pixels)
4143 {
4144    GET_CURRENT_CONTEXT(ctx);
4145    texturesubimage_error(ctx, 1, texture, 0, level, xoffset, 0, 0, width, 1,
4146                          1, format, type, pixels, "glTextureSubImage1D",
4147                          false);
4148 }
4149 
4150 
4151 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)4152 _mesa_TextureSubImage2D_no_error(GLuint texture, GLint level, GLint xoffset,
4153                                  GLint yoffset, GLsizei width, GLsizei height,
4154                                  GLenum format, GLenum type,
4155                                  const GLvoid *pixels)
4156 {
4157    GET_CURRENT_CONTEXT(ctx);
4158    texturesubimage_no_error(ctx, 2, texture, 0, level, xoffset, yoffset, 0,
4159                             width, height, 1, format, type, pixels,
4160                             "glTextureSubImage2D", false);
4161 }
4162 
4163 
4164 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)4165 _mesa_TextureSubImage2DEXT(GLuint texture, GLenum target, GLint level,
4166                            GLint xoffset, GLint yoffset, GLsizei width,
4167                            GLsizei height, GLenum format, GLenum type,
4168                            const GLvoid *pixels)
4169 {
4170    GET_CURRENT_CONTEXT(ctx);
4171    texturesubimage_error(ctx, 2, texture, target, level, xoffset, yoffset, 0,
4172                          width, height, 1, format, type, pixels,
4173                          "glTextureSubImage2DEXT", true);
4174 }
4175 
4176 
4177 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)4178 _mesa_MultiTexSubImage2DEXT(GLenum texunit, GLenum target, GLint level,
4179                             GLint xoffset, GLint yoffset, GLsizei width,
4180                             GLsizei height, GLenum format, GLenum type,
4181                             const GLvoid *pixels)
4182 {
4183    GET_CURRENT_CONTEXT(ctx);
4184    struct gl_texture_object *texObj;
4185    struct gl_texture_image *texImage;
4186 
4187    texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
4188                                                    texunit - GL_TEXTURE0,
4189                                                    false,
4190                                                    "glMultiTexImage2DEXT");
4191    texImage = _mesa_select_tex_image(texObj, target, level);
4192 
4193    texture_sub_image(ctx, 2, texObj, texImage, target, level,
4194                      xoffset, yoffset, 0, width, height, 1,
4195                      format, type, pixels);
4196 }
4197 
4198 
4199 void GLAPIENTRY
_mesa_TextureSubImage2D(GLuint texture,GLint level,GLint xoffset,GLint yoffset,GLsizei width,GLsizei height,GLenum format,GLenum type,const GLvoid * pixels)4200 _mesa_TextureSubImage2D(GLuint texture, GLint level,
4201                         GLint xoffset, GLint yoffset,
4202                         GLsizei width, GLsizei height,
4203                         GLenum format, GLenum type,
4204                         const GLvoid *pixels)
4205 {
4206    GET_CURRENT_CONTEXT(ctx);
4207    texturesubimage_error(ctx, 2, texture, 0, level, xoffset, yoffset, 0,
4208                          width, height, 1, format, type, pixels,
4209                          "glTextureSubImage2D", false);
4210 }
4211 
4212 
4213 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)4214 _mesa_TextureSubImage3D_no_error(GLuint texture, GLint level, GLint xoffset,
4215                                  GLint yoffset, GLint zoffset, GLsizei width,
4216                                  GLsizei height, GLsizei depth, GLenum format,
4217                                  GLenum type, const GLvoid *pixels)
4218 {
4219    GET_CURRENT_CONTEXT(ctx);
4220    texturesubimage_no_error(ctx, 3, texture, 0, level, xoffset, yoffset,
4221                             zoffset, width, height, depth, format, type,
4222                             pixels, "glTextureSubImage3D", false);
4223 }
4224 
4225 
4226 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)4227 _mesa_TextureSubImage3DEXT(GLuint texture, GLenum target, GLint level,
4228                            GLint xoffset, GLint yoffset, GLint zoffset,
4229                            GLsizei width, GLsizei height, GLsizei depth,
4230                            GLenum format, GLenum type, const GLvoid *pixels)
4231 {
4232    GET_CURRENT_CONTEXT(ctx);
4233    texturesubimage_error(ctx, 3, texture, target, level, xoffset, yoffset,
4234                          zoffset, width, height, depth, format, type,
4235                          pixels, "glTextureSubImage3DEXT", true);
4236 }
4237 
4238 
4239 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)4240 _mesa_MultiTexSubImage3DEXT(GLenum texunit, GLenum target, GLint level,
4241                            GLint xoffset, GLint yoffset, GLint zoffset,
4242                            GLsizei width, GLsizei height, GLsizei depth,
4243                            GLenum format, GLenum type, const GLvoid *pixels)
4244 {
4245    GET_CURRENT_CONTEXT(ctx);
4246    struct gl_texture_object *texObj;
4247    struct gl_texture_image *texImage;
4248 
4249    texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
4250                                                    texunit - GL_TEXTURE0,
4251                                                    false,
4252                                                    "glMultiTexImage3DEXT");
4253    texImage = _mesa_select_tex_image(texObj, target, level);
4254 
4255    texture_sub_image(ctx, 3, texObj, texImage, target, level,
4256                      xoffset, yoffset, zoffset, width, height, depth,
4257                      format, type, pixels);
4258 }
4259 
4260 
4261 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)4262 _mesa_TextureSubImage3D(GLuint texture, GLint level,
4263                         GLint xoffset, GLint yoffset, GLint zoffset,
4264                         GLsizei width, GLsizei height, GLsizei depth,
4265                         GLenum format, GLenum type,
4266                         const GLvoid *pixels)
4267 {
4268    GET_CURRENT_CONTEXT(ctx);
4269    texturesubimage_error(ctx, 3, texture, 0, level, xoffset, yoffset, zoffset,
4270                          width, height, depth, format, type, pixels,
4271                          "glTextureSubImage3D", false);
4272 }
4273 
4274 
4275 /**
4276  * For glCopyTexSubImage, return the source renderbuffer to copy texel data
4277  * from.  This depends on whether the texture contains color or depth values.
4278  */
4279 static struct gl_renderbuffer *
get_copy_tex_image_source(struct gl_context * ctx,mesa_format texFormat)4280 get_copy_tex_image_source(struct gl_context *ctx, mesa_format texFormat)
4281 {
4282    if (_mesa_get_format_bits(texFormat, GL_DEPTH_BITS) > 0) {
4283       /* reading from depth/stencil buffer */
4284       return ctx->ReadBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
4285    } else if (_mesa_get_format_bits(texFormat, GL_STENCIL_BITS) > 0) {
4286       return ctx->ReadBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
4287    } else {
4288       /* copying from color buffer */
4289       return ctx->ReadBuffer->_ColorReadBuffer;
4290    }
4291 }
4292 
4293 
4294 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)4295 copytexsubimage_by_slice(struct gl_context *ctx,
4296                          struct gl_texture_image *texImage,
4297                          GLuint dims,
4298                          GLint xoffset, GLint yoffset, GLint zoffset,
4299                          struct gl_renderbuffer *rb,
4300                          GLint x, GLint y,
4301                          GLsizei width, GLsizei height)
4302 {
4303    if (texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
4304       int slice;
4305 
4306       /* For 1D arrays, we copy each scanline of the source rectangle into the
4307        * next array slice.
4308        */
4309       assert(zoffset == 0);
4310 
4311       for (slice = 0; slice < height; slice++) {
4312          assert(yoffset + slice < texImage->Height);
4313          st_CopyTexSubImage(ctx, 2, texImage,
4314                             xoffset, 0, yoffset + slice,
4315                             rb, x, y + slice, width, 1);
4316       }
4317    } else {
4318       st_CopyTexSubImage(ctx, dims, texImage,
4319                          xoffset, yoffset, zoffset,
4320                          rb, x, y, width, height);
4321    }
4322 }
4323 
4324 
4325 static GLboolean
formats_differ_in_component_sizes(mesa_format f1,mesa_format f2)4326 formats_differ_in_component_sizes(mesa_format f1, mesa_format f2)
4327 {
4328    GLint f1_r_bits = _mesa_get_format_bits(f1, GL_RED_BITS);
4329    GLint f1_g_bits = _mesa_get_format_bits(f1, GL_GREEN_BITS);
4330    GLint f1_b_bits = _mesa_get_format_bits(f1, GL_BLUE_BITS);
4331    GLint f1_a_bits = _mesa_get_format_bits(f1, GL_ALPHA_BITS);
4332 
4333    GLint f2_r_bits = _mesa_get_format_bits(f2, GL_RED_BITS);
4334    GLint f2_g_bits = _mesa_get_format_bits(f2, GL_GREEN_BITS);
4335    GLint f2_b_bits = _mesa_get_format_bits(f2, GL_BLUE_BITS);
4336    GLint f2_a_bits = _mesa_get_format_bits(f2, GL_ALPHA_BITS);
4337 
4338    if ((f1_r_bits && f2_r_bits && f1_r_bits != f2_r_bits)
4339        || (f1_g_bits && f2_g_bits && f1_g_bits != f2_g_bits)
4340        || (f1_b_bits && f2_b_bits && f1_b_bits != f2_b_bits)
4341        || (f1_a_bits && f2_a_bits && f1_a_bits != f2_a_bits))
4342       return GL_TRUE;
4343 
4344    return GL_FALSE;
4345 }
4346 
4347 
4348 /**
4349  * Check if the given texture format and size arguments match those
4350  * of the texture image.
4351  * \param return true if arguments match, false otherwise.
4352  */
4353 static bool
can_avoid_reallocation(const struct gl_texture_image * texImage,GLenum internalFormat,mesa_format texFormat,GLsizei width,GLsizei height,GLint border)4354 can_avoid_reallocation(const struct gl_texture_image *texImage,
4355                        GLenum internalFormat,
4356                        mesa_format texFormat, GLsizei width,
4357                        GLsizei height, GLint border)
4358 {
4359    if (texImage->InternalFormat != internalFormat)
4360       return false;
4361    if (texImage->TexFormat != texFormat)
4362       return false;
4363    if (texImage->Border != border)
4364       return false;
4365    if (texImage->Width2 != width)
4366       return false;
4367    if (texImage->Height2 != height)
4368       return false;
4369    return true;
4370 }
4371 
4372 
4373 /**
4374  * Implementation for glCopyTex(ture)SubImage1/2/3D() functions.
4375  */
4376 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)4377 copy_texture_sub_image(struct gl_context *ctx, GLuint dims,
4378                        struct gl_texture_object *texObj,
4379                        GLenum target, GLint level,
4380                        GLint xoffset, GLint yoffset, GLint zoffset,
4381                        GLint x, GLint y, GLsizei width, GLsizei height)
4382 {
4383    struct gl_texture_image *texImage;
4384 
4385    _mesa_lock_texture(ctx, texObj);
4386 
4387    texImage = _mesa_select_tex_image(texObj, target, level);
4388 
4389    /* If we have a border, offset=-1 is legal.  Bias by border width. */
4390    switch (dims) {
4391    case 3:
4392       if (target != GL_TEXTURE_2D_ARRAY)
4393          zoffset += texImage->Border;
4394       FALLTHROUGH;
4395    case 2:
4396       if (target != GL_TEXTURE_1D_ARRAY)
4397          yoffset += texImage->Border;
4398       FALLTHROUGH;
4399    case 1:
4400       xoffset += texImage->Border;
4401    }
4402 
4403    if (ctx->Const.NoClippingOnCopyTex ||
4404        _mesa_clip_copytexsubimage(ctx, &xoffset, &yoffset, &x, &y,
4405                                   &width, &height)) {
4406       struct gl_renderbuffer *srcRb =
4407          get_copy_tex_image_source(ctx, texImage->TexFormat);
4408 
4409       copytexsubimage_by_slice(ctx, texImage, dims, xoffset, yoffset, zoffset,
4410                                srcRb, x, y, width, height);
4411 
4412       check_gen_mipmap(ctx, target, texObj, level);
4413 
4414       /* NOTE: Don't signal _NEW_TEXTURE_OBJECT since we've only changed
4415        * the texel data, not the texture format, size, etc.
4416        */
4417    }
4418 
4419    _mesa_unlock_texture(ctx, texObj);
4420 }
4421 
4422 
4423 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)4424 copy_texture_sub_image_err(struct gl_context *ctx, GLuint dims,
4425                            struct gl_texture_object *texObj,
4426                            GLenum target, GLint level,
4427                            GLint xoffset, GLint yoffset, GLint zoffset,
4428                            GLint x, GLint y, GLsizei width, GLsizei height,
4429                            const char *caller)
4430 {
4431    FLUSH_VERTICES(ctx, 0, 0);
4432 
4433    if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
4434       _mesa_debug(ctx, "%s %s %d %d %d %d %d %d %d %d\n", caller,
4435                   _mesa_enum_to_string(target),
4436                   level, xoffset, yoffset, zoffset, x, y, width, height);
4437 
4438    _mesa_update_pixel(ctx);
4439 
4440    if (ctx->NewState & _NEW_BUFFERS)
4441       _mesa_update_state(ctx);
4442 
4443    if (copytexsubimage_error_check(ctx, dims, texObj, target, level,
4444                                    xoffset, yoffset, zoffset,
4445                                    width, height, caller)) {
4446       return;
4447    }
4448 
4449    copy_texture_sub_image(ctx, dims, texObj, target, level, xoffset, yoffset,
4450                           zoffset, x, y, width, height);
4451 }
4452 
4453 
4454 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)4455 copy_texture_sub_image_no_error(struct gl_context *ctx, GLuint dims,
4456                                 struct gl_texture_object *texObj,
4457                                 GLenum target, GLint level,
4458                                 GLint xoffset, GLint yoffset, GLint zoffset,
4459                                 GLint x, GLint y, GLsizei width, GLsizei height)
4460 {
4461    FLUSH_VERTICES(ctx, 0, 0);
4462 
4463    _mesa_update_pixel(ctx);
4464 
4465    if (ctx->NewState & _NEW_BUFFERS)
4466       _mesa_update_state(ctx);
4467 
4468    copy_texture_sub_image(ctx, dims, texObj, target, level, xoffset, yoffset,
4469                           zoffset, x, y, width, height);
4470 }
4471 
4472 
4473 /**
4474  * Implement the glCopyTexImage1/2D() functions.
4475  */
4476 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)4477 copyteximage(struct gl_context *ctx, GLuint dims, struct gl_texture_object *texObj,
4478              GLenum target, GLint level, GLenum internalFormat,
4479              GLint x, GLint y, GLsizei width, GLsizei height, GLint border,
4480              bool no_error)
4481 {
4482    struct gl_texture_image *texImage;
4483    mesa_format texFormat;
4484 
4485    FLUSH_VERTICES(ctx, 0, 0);
4486 
4487    if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
4488       _mesa_debug(ctx, "glCopyTexImage%uD %s %d %s %d %d %d %d %d\n",
4489                   dims,
4490                   _mesa_enum_to_string(target), level,
4491                   _mesa_enum_to_string(internalFormat),
4492                   x, y, width, height, border);
4493 
4494    _mesa_update_pixel(ctx);
4495 
4496    if (ctx->NewState & _NEW_BUFFERS)
4497       _mesa_update_state(ctx);
4498 
4499    /* check target */
4500    if (!no_error && !legal_texsubimage_target(ctx, dims, target, false)) {
4501       _mesa_error(ctx, GL_INVALID_ENUM, "glCopyTexImage%uD(target=%s)",
4502                   dims, _mesa_enum_to_string(target));
4503       return;
4504    }
4505 
4506    if (!texObj)
4507       texObj = _mesa_get_current_tex_object(ctx, target);
4508 
4509    if (!no_error) {
4510       if (copytexture_error_check(ctx, dims, target, texObj, level,
4511                                   internalFormat, border))
4512          return;
4513 
4514       if (!_mesa_legal_texture_dimensions(ctx, target, level, width, height,
4515                                           1, border)) {
4516          _mesa_error(ctx, GL_INVALID_VALUE,
4517                      "glCopyTexImage%uD(invalid width=%d or height=%d)",
4518                      dims, width, height);
4519          return;
4520       }
4521    }
4522 
4523    assert(texObj);
4524 
4525    texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
4526                                            internalFormat, GL_NONE, GL_NONE);
4527 
4528    /* First check if reallocating the texture buffer can be avoided.
4529     * Without the realloc the copy can be 20x faster.
4530     */
4531    _mesa_lock_texture(ctx, texObj);
4532    {
4533       texImage = _mesa_select_tex_image(texObj, target, level);
4534       if (texImage && can_avoid_reallocation(texImage, internalFormat, texFormat,
4535                                              width, height, border)) {
4536          _mesa_unlock_texture(ctx, texObj);
4537          if (no_error) {
4538             copy_texture_sub_image_no_error(ctx, dims, texObj, target, level, 0,
4539                                             0, 0, x, y, width, height);
4540          } else {
4541             copy_texture_sub_image_err(ctx, dims, texObj, target, level, 0, 0,
4542                                        0, x, y, width, height,"CopyTexImage");
4543          }
4544          return;
4545       }
4546    }
4547    _mesa_unlock_texture(ctx, texObj);
4548    _mesa_perf_debug(ctx, MESA_DEBUG_SEVERITY_LOW, "glCopyTexImage "
4549                     "can't avoid reallocating texture storage\n");
4550 
4551    if (!no_error && _mesa_is_gles3(ctx)) {
4552       struct gl_renderbuffer *rb =
4553          _mesa_get_read_renderbuffer_for_format(ctx, internalFormat);
4554 
4555       if (_mesa_is_enum_format_unsized(internalFormat)) {
4556       /* Conversion from GL_RGB10_A2 source buffer format is not allowed in
4557        * OpenGL ES 3.0. Khronos bug# 9807.
4558        */
4559          if (rb->InternalFormat == GL_RGB10_A2) {
4560                _mesa_error(ctx, GL_INVALID_OPERATION,
4561                            "glCopyTexImage%uD(Reading from GL_RGB10_A2 buffer"
4562                            " and writing to unsized internal format)", dims);
4563                return;
4564          }
4565       }
4566       /* From Page 139 of OpenGL ES 3.0 spec:
4567        *    "If internalformat is sized, the internal format of the new texel
4568        *    array is internalformat, and this is also the new texel array’s
4569        *    effective internal format. If the component sizes of internalformat
4570        *    do not exactly match the corresponding component sizes of the source
4571        *    buffer’s effective internal format, described below, an
4572        *    INVALID_OPERATION error is generated. If internalformat is unsized,
4573        *    the internal format of the new texel array is the effective internal
4574        *    format of the source buffer, and this is also the new texel array’s
4575        *    effective internal format.
4576        */
4577       else if (formats_differ_in_component_sizes (texFormat, rb->Format)) {
4578             _mesa_error(ctx, GL_INVALID_OPERATION,
4579                         "glCopyTexImage%uD(component size changed in"
4580                         " internal format)", dims);
4581             return;
4582       }
4583    }
4584 
4585    assert(texFormat != MESA_FORMAT_NONE);
4586 
4587    if (!st_TestProxyTexImage(ctx, proxy_target(target),
4588                              0, level, texFormat, 1,
4589                              width, height, 1)) {
4590       _mesa_error(ctx, GL_OUT_OF_MEMORY,
4591                   "glCopyTexImage%uD(image too large)", dims);
4592       return;
4593    }
4594 
4595    if (border) {
4596       x += border;
4597       width -= border * 2;
4598       if (dims == 2) {
4599          y += border;
4600          height -= border * 2;
4601       }
4602       border = 0;
4603    }
4604 
4605    _mesa_lock_texture(ctx, texObj);
4606    {
4607       texObj->External = GL_FALSE;
4608       texImage = _mesa_get_tex_image(ctx, texObj, target, level);
4609 
4610       if (!texImage) {
4611          _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage%uD", dims);
4612       }
4613       else {
4614          GLint srcX = x, srcY = y, dstX = 0, dstY = 0, dstZ = 0;
4615          const GLuint face = _mesa_tex_target_to_face(target);
4616 
4617          /* Free old texture image */
4618          st_FreeTextureImageBuffer(ctx, texImage);
4619 
4620          _mesa_init_teximage_fields(ctx, texImage, width, height, 1,
4621                                     border, internalFormat, texFormat);
4622 
4623          if (width && height) {
4624             /* Allocate texture memory (no pixel data yet) */
4625             st_AllocTextureImageBuffer(ctx, texImage);
4626 
4627             if (ctx->Const.NoClippingOnCopyTex ||
4628                 _mesa_clip_copytexsubimage(ctx, &dstX, &dstY, &srcX, &srcY,
4629                                            &width, &height)) {
4630                struct gl_renderbuffer *srcRb =
4631                   get_copy_tex_image_source(ctx, texImage->TexFormat);
4632 
4633                copytexsubimage_by_slice(ctx, texImage, dims,
4634                                         dstX, dstY, dstZ,
4635                                         srcRb, srcX, srcY, width, height);
4636             }
4637 
4638             check_gen_mipmap(ctx, target, texObj, level);
4639          }
4640 
4641          _mesa_update_fbo_texture(ctx, texObj, face, level);
4642 
4643          _mesa_dirty_texobj(ctx, texObj);
4644          _mesa_update_texture_object_swizzle(ctx, texObj);
4645       }
4646    }
4647    _mesa_unlock_texture(ctx, texObj);
4648 }
4649 
4650 
4651 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)4652 copyteximage_err(struct gl_context *ctx, GLuint dims,
4653                  GLenum target,
4654                  GLint level, GLenum internalFormat, GLint x, GLint y,
4655                  GLsizei width, GLsizei height, GLint border)
4656 {
4657    copyteximage(ctx, dims, NULL, target, level, internalFormat, x, y, width, height,
4658                 border, false);
4659 }
4660 
4661 
4662 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)4663 copyteximage_no_error(struct gl_context *ctx, GLuint dims, GLenum target,
4664                       GLint level, GLenum internalFormat, GLint x, GLint y,
4665                       GLsizei width, GLsizei height, GLint border)
4666 {
4667    copyteximage(ctx, dims, NULL, target, level, internalFormat, x, y, width, height,
4668                 border, true);
4669 }
4670 
4671 
4672 void GLAPIENTRY
_mesa_CopyTexImage1D(GLenum target,GLint level,GLenum internalFormat,GLint x,GLint y,GLsizei width,GLint border)4673 _mesa_CopyTexImage1D( GLenum target, GLint level,
4674                       GLenum internalFormat,
4675                       GLint x, GLint y,
4676                       GLsizei width, GLint border )
4677 {
4678    GET_CURRENT_CONTEXT(ctx);
4679    copyteximage_err(ctx, 1, target, level, internalFormat, x, y, width, 1,
4680                     border);
4681 }
4682 
4683 
4684 void GLAPIENTRY
_mesa_CopyTextureImage1DEXT(GLuint texture,GLenum target,GLint level,GLenum internalFormat,GLint x,GLint y,GLsizei width,GLint border)4685 _mesa_CopyTextureImage1DEXT( GLuint texture, GLenum target, GLint level,
4686                              GLenum internalFormat,
4687                              GLint x, GLint y,
4688                              GLsizei width, GLint border )
4689 {
4690    GET_CURRENT_CONTEXT(ctx);
4691    struct gl_texture_object* texObj =
4692       _mesa_lookup_or_create_texture(ctx, target, texture, false, true,
4693                                      "glCopyTextureImage1DEXT");
4694    if (!texObj)
4695       return;
4696    copyteximage(ctx, 1, texObj, target, level, internalFormat, x, y, width, 1,
4697                 border, false);
4698 }
4699 
4700 
4701 void GLAPIENTRY
_mesa_CopyMultiTexImage1DEXT(GLenum texunit,GLenum target,GLint level,GLenum internalFormat,GLint x,GLint y,GLsizei width,GLint border)4702 _mesa_CopyMultiTexImage1DEXT( GLenum texunit, GLenum target, GLint level,
4703                               GLenum internalFormat,
4704                               GLint x, GLint y,
4705                               GLsizei width, GLint border )
4706 {
4707    GET_CURRENT_CONTEXT(ctx);
4708    struct gl_texture_object* texObj =
4709       _mesa_get_texobj_by_target_and_texunit(ctx, target,
4710                                              texunit - GL_TEXTURE0,
4711                                              false,
4712                                              "glCopyMultiTexImage1DEXT");
4713    if (!texObj)
4714       return;
4715    copyteximage(ctx, 1, texObj, target, level, internalFormat, x, y, width, 1,
4716                 border, false);
4717 }
4718 
4719 
4720 void GLAPIENTRY
_mesa_CopyTexImage2D(GLenum target,GLint level,GLenum internalFormat,GLint x,GLint y,GLsizei width,GLsizei height,GLint border)4721 _mesa_CopyTexImage2D( GLenum target, GLint level, GLenum internalFormat,
4722                       GLint x, GLint y, GLsizei width, GLsizei height,
4723                       GLint border )
4724 {
4725    GET_CURRENT_CONTEXT(ctx);
4726    copyteximage_err(ctx, 2, target, level, internalFormat,
4727                     x, y, width, height, border);
4728 }
4729 
4730 
4731 void GLAPIENTRY
_mesa_CopyTextureImage2DEXT(GLuint texture,GLenum target,GLint level,GLenum internalFormat,GLint x,GLint y,GLsizei width,GLsizei height,GLint border)4732 _mesa_CopyTextureImage2DEXT( GLuint texture, GLenum target, GLint level,
4733                              GLenum internalFormat,
4734                              GLint x, GLint y,
4735                              GLsizei width, GLsizei height,
4736                              GLint border )
4737 {
4738    GET_CURRENT_CONTEXT(ctx);
4739    struct gl_texture_object* texObj =
4740       _mesa_lookup_or_create_texture(ctx, target, texture, false, true,
4741                                      "glCopyTextureImage2DEXT");
4742    if (!texObj)
4743       return;
4744    copyteximage(ctx, 2, texObj, target, level, internalFormat, x, y, width, height,
4745                 border, false);
4746 }
4747 
4748 
4749 void GLAPIENTRY
_mesa_CopyMultiTexImage2DEXT(GLenum texunit,GLenum target,GLint level,GLenum internalFormat,GLint x,GLint y,GLsizei width,GLsizei height,GLint border)4750 _mesa_CopyMultiTexImage2DEXT( GLenum texunit, GLenum target, GLint level,
4751                               GLenum internalFormat,
4752                               GLint x, GLint y,
4753                               GLsizei width, GLsizei height, GLint border )
4754 {
4755    GET_CURRENT_CONTEXT(ctx);
4756    struct gl_texture_object* texObj =
4757       _mesa_get_texobj_by_target_and_texunit(ctx, target,
4758                                              texunit - GL_TEXTURE0,
4759                                              false,
4760                                              "glCopyMultiTexImage2DEXT");
4761    if (!texObj)
4762       return;
4763    copyteximage(ctx, 2, texObj, target, level, internalFormat, x, y, width, height,
4764                 border, false);
4765 }
4766 
4767 
4768 void GLAPIENTRY
_mesa_CopyTexImage1D_no_error(GLenum target,GLint level,GLenum internalFormat,GLint x,GLint y,GLsizei width,GLint border)4769 _mesa_CopyTexImage1D_no_error(GLenum target, GLint level, GLenum internalFormat,
4770                               GLint x, GLint y, GLsizei width, GLint border)
4771 {
4772    GET_CURRENT_CONTEXT(ctx);
4773    copyteximage_no_error(ctx, 1, target, level, internalFormat, x, y, width, 1,
4774                          border);
4775 }
4776 
4777 
4778 void GLAPIENTRY
_mesa_CopyTexImage2D_no_error(GLenum target,GLint level,GLenum internalFormat,GLint x,GLint y,GLsizei width,GLsizei height,GLint border)4779 _mesa_CopyTexImage2D_no_error(GLenum target, GLint level, GLenum internalFormat,
4780                               GLint x, GLint y, GLsizei width, GLsizei height,
4781                               GLint border)
4782 {
4783    GET_CURRENT_CONTEXT(ctx);
4784    copyteximage_no_error(ctx, 2, target, level, internalFormat,
4785                          x, y, width, height, border);
4786 }
4787 
4788 
4789 void GLAPIENTRY
_mesa_CopyTexSubImage1D(GLenum target,GLint level,GLint xoffset,GLint x,GLint y,GLsizei width)4790 _mesa_CopyTexSubImage1D(GLenum target, GLint level,
4791                         GLint xoffset, GLint x, GLint y, GLsizei width)
4792 {
4793    struct gl_texture_object* texObj;
4794    const char *self = "glCopyTexSubImage1D";
4795    GET_CURRENT_CONTEXT(ctx);
4796 
4797    /* Check target (proxies not allowed). Target must be checked prior to
4798     * calling _mesa_get_current_tex_object.
4799     */
4800    if (!legal_texsubimage_target(ctx, 1, target, false)) {
4801       _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", self,
4802                   _mesa_enum_to_string(target));
4803       return;
4804    }
4805 
4806    texObj = _mesa_get_current_tex_object(ctx, target);
4807    if (!texObj)
4808       return;
4809 
4810    copy_texture_sub_image_err(ctx, 1, texObj, target, level, xoffset, 0, 0,
4811                               x, y, width, 1, self);
4812 }
4813 
4814 
4815 void GLAPIENTRY
_mesa_CopyTexSubImage2D(GLenum target,GLint level,GLint xoffset,GLint yoffset,GLint x,GLint y,GLsizei width,GLsizei height)4816 _mesa_CopyTexSubImage2D(GLenum target, GLint level,
4817                         GLint xoffset, GLint yoffset,
4818                         GLint x, GLint y, GLsizei width, GLsizei height)
4819 {
4820    struct gl_texture_object* texObj;
4821    const char *self = "glCopyTexSubImage2D";
4822    GET_CURRENT_CONTEXT(ctx);
4823 
4824    /* Check target (proxies not allowed). Target must be checked prior to
4825     * calling _mesa_get_current_tex_object.
4826     */
4827    if (!legal_texsubimage_target(ctx, 2, target, false)) {
4828       _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", self,
4829                   _mesa_enum_to_string(target));
4830       return;
4831    }
4832 
4833    texObj = _mesa_get_current_tex_object(ctx, target);
4834    if (!texObj)
4835       return;
4836 
4837    copy_texture_sub_image_err(ctx, 2, texObj, target, level, xoffset, yoffset,
4838                               0, x, y, width, height, self);
4839 }
4840 
4841 
4842 void GLAPIENTRY
_mesa_CopyTexSubImage3D(GLenum target,GLint level,GLint xoffset,GLint yoffset,GLint zoffset,GLint x,GLint y,GLsizei width,GLsizei height)4843 _mesa_CopyTexSubImage3D(GLenum target, GLint level,
4844                         GLint xoffset, GLint yoffset, GLint zoffset,
4845                         GLint x, GLint y, GLsizei width, GLsizei height)
4846 {
4847    struct gl_texture_object* texObj;
4848    const char *self = "glCopyTexSubImage3D";
4849    GET_CURRENT_CONTEXT(ctx);
4850 
4851    /* Check target (proxies not allowed). Target must be checked prior to
4852     * calling _mesa_get_current_tex_object.
4853     */
4854    if (!legal_texsubimage_target(ctx, 3, target, false)) {
4855       _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", self,
4856                   _mesa_enum_to_string(target));
4857       return;
4858    }
4859 
4860    texObj = _mesa_get_current_tex_object(ctx, target);
4861    if (!texObj)
4862       return;
4863 
4864    copy_texture_sub_image_err(ctx, 3, texObj, target, level, xoffset, yoffset,
4865                               zoffset, x, y, width, height, self);
4866 }
4867 
4868 
4869 void GLAPIENTRY
_mesa_CopyTextureSubImage1D(GLuint texture,GLint level,GLint xoffset,GLint x,GLint y,GLsizei width)4870 _mesa_CopyTextureSubImage1D(GLuint texture, GLint level,
4871                             GLint xoffset, GLint x, GLint y, GLsizei width)
4872 {
4873    struct gl_texture_object* texObj;
4874    const char *self = "glCopyTextureSubImage1D";
4875    GET_CURRENT_CONTEXT(ctx);
4876 
4877    texObj = _mesa_lookup_texture_err(ctx, texture, self);
4878    if (!texObj)
4879       return;
4880 
4881    /* Check target (proxies not allowed). */
4882    if (!legal_texsubimage_target(ctx, 1, texObj->Target, true)) {
4883       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid target %s)", self,
4884                   _mesa_enum_to_string(texObj->Target));
4885       return;
4886    }
4887 
4888    copy_texture_sub_image_err(ctx, 1, texObj, texObj->Target, level, xoffset, 0,
4889                               0, x, y, width, 1, self);
4890 }
4891 
4892 
4893 void GLAPIENTRY
_mesa_CopyTextureSubImage1DEXT(GLuint texture,GLenum target,GLint level,GLint xoffset,GLint x,GLint y,GLsizei width)4894 _mesa_CopyTextureSubImage1DEXT(GLuint texture, GLenum target, GLint level,
4895                                GLint xoffset, GLint x, GLint y, GLsizei width)
4896 {
4897    struct gl_texture_object* texObj;
4898    const char *self = "glCopyTextureSubImage1DEXT";
4899    GET_CURRENT_CONTEXT(ctx);
4900 
4901    texObj = _mesa_lookup_or_create_texture(ctx, target, texture, false, true,
4902                                            self);
4903    if (!texObj)
4904       return;
4905 
4906    /* Check target (proxies not allowed). */
4907    if (!legal_texsubimage_target(ctx, 1, texObj->Target, true)) {
4908       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid target %s)", self,
4909                   _mesa_enum_to_string(texObj->Target));
4910       return;
4911    }
4912 
4913    copy_texture_sub_image_err(ctx, 1, texObj, texObj->Target, level, xoffset, 0,
4914                               0, x, y, width, 1, self);
4915 }
4916 
4917 
4918 void GLAPIENTRY
_mesa_CopyMultiTexSubImage1DEXT(GLenum texunit,GLenum target,GLint level,GLint xoffset,GLint x,GLint y,GLsizei width)4919 _mesa_CopyMultiTexSubImage1DEXT(GLenum texunit, GLenum target, GLint level,
4920                                 GLint xoffset, GLint x, GLint y, GLsizei width)
4921 {
4922    struct gl_texture_object* texObj;
4923    const char *self = "glCopyMultiTexSubImage1DEXT";
4924    GET_CURRENT_CONTEXT(ctx);
4925 
4926    texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
4927                                                    texunit - GL_TEXTURE0,
4928                                                    false, self);
4929    if (!texObj)
4930       return;
4931 
4932    copy_texture_sub_image_err(ctx, 1, texObj, texObj->Target, level, xoffset, 0,
4933                               0, x, y, width, 1, self);
4934 }
4935 
4936 
4937 void GLAPIENTRY
_mesa_CopyTextureSubImage2D(GLuint texture,GLint level,GLint xoffset,GLint yoffset,GLint x,GLint y,GLsizei width,GLsizei height)4938 _mesa_CopyTextureSubImage2D(GLuint texture, GLint level,
4939                             GLint xoffset, GLint yoffset,
4940                             GLint x, GLint y, GLsizei width, GLsizei height)
4941 {
4942    struct gl_texture_object* texObj;
4943    const char *self = "glCopyTextureSubImage2D";
4944    GET_CURRENT_CONTEXT(ctx);
4945 
4946    texObj = _mesa_lookup_texture_err(ctx, texture, self);
4947    if (!texObj)
4948       return;
4949 
4950    /* Check target (proxies not allowed). */
4951    if (!legal_texsubimage_target(ctx, 2, texObj->Target, true)) {
4952       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid target %s)", self,
4953                   _mesa_enum_to_string(texObj->Target));
4954       return;
4955    }
4956 
4957    copy_texture_sub_image_err(ctx, 2, texObj, texObj->Target, level, xoffset,
4958                               yoffset, 0, x, y, width, height, self);
4959 }
4960 
4961 
4962 void GLAPIENTRY
_mesa_CopyTextureSubImage2DEXT(GLuint texture,GLenum target,GLint level,GLint xoffset,GLint yoffset,GLint x,GLint y,GLsizei width,GLsizei height)4963 _mesa_CopyTextureSubImage2DEXT(GLuint texture, GLenum target, GLint level,
4964                                GLint xoffset, GLint yoffset,
4965                                GLint x, GLint y, GLsizei width, GLsizei height)
4966 {
4967    struct gl_texture_object* texObj;
4968    const char *self = "glCopyTextureSubImage2DEXT";
4969    GET_CURRENT_CONTEXT(ctx);
4970 
4971    texObj = _mesa_lookup_or_create_texture(ctx, target, texture, false, true, self);
4972    if (!texObj)
4973       return;
4974 
4975    /* Check target (proxies not allowed). */
4976    if (!legal_texsubimage_target(ctx, 2, texObj->Target, true)) {
4977       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid target %s)", self,
4978                   _mesa_enum_to_string(texObj->Target));
4979       return;
4980    }
4981 
4982    copy_texture_sub_image_err(ctx, 2, texObj, texObj->Target, level, xoffset,
4983                               yoffset, 0, x, y, width, height, self);
4984 }
4985 
4986 
4987 void GLAPIENTRY
_mesa_CopyMultiTexSubImage2DEXT(GLenum texunit,GLenum target,GLint level,GLint xoffset,GLint yoffset,GLint x,GLint y,GLsizei width,GLsizei height)4988 _mesa_CopyMultiTexSubImage2DEXT(GLenum texunit, GLenum target, GLint level,
4989                                GLint xoffset, GLint yoffset,
4990                                GLint x, GLint y, GLsizei width, GLsizei height)
4991 {
4992    struct gl_texture_object* texObj;
4993    const char *self = "glCopyMultiTexSubImage2DEXT";
4994    GET_CURRENT_CONTEXT(ctx);
4995 
4996    texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
4997                                                    texunit - GL_TEXTURE0,
4998                                                    false, self);
4999    if (!texObj)
5000       return;
5001 
5002    copy_texture_sub_image_err(ctx, 2, texObj, texObj->Target, level, xoffset,
5003                               yoffset, 0, x, y, width, height, self);
5004 }
5005 
5006 void GLAPIENTRY
_mesa_CopyTextureSubImage3D(GLuint texture,GLint level,GLint xoffset,GLint yoffset,GLint zoffset,GLint x,GLint y,GLsizei width,GLsizei height)5007 _mesa_CopyTextureSubImage3D(GLuint texture, GLint level,
5008                             GLint xoffset, GLint yoffset, GLint zoffset,
5009                             GLint x, GLint y, GLsizei width, GLsizei height)
5010 {
5011    struct gl_texture_object* texObj;
5012    const char *self = "glCopyTextureSubImage3D";
5013    GET_CURRENT_CONTEXT(ctx);
5014 
5015    texObj = _mesa_lookup_texture_err(ctx, texture, self);
5016    if (!texObj)
5017       return;
5018 
5019    /* Check target (proxies not allowed). */
5020    if (!legal_texsubimage_target(ctx, 3, texObj->Target, true)) {
5021       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid target %s)", self,
5022                   _mesa_enum_to_string(texObj->Target));
5023       return;
5024    }
5025 
5026    if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
5027       /* Act like CopyTexSubImage2D */
5028       copy_texture_sub_image_err(ctx, 2, texObj,
5029                                 GL_TEXTURE_CUBE_MAP_POSITIVE_X + zoffset,
5030                                 level, xoffset, yoffset, 0, x, y, width, height,
5031                                 self);
5032    }
5033    else
5034       copy_texture_sub_image_err(ctx, 3, texObj, texObj->Target, level, xoffset,
5035                                  yoffset, zoffset, x, y, width, height, self);
5036 }
5037 
5038 
5039 void GLAPIENTRY
_mesa_CopyTextureSubImage3DEXT(GLuint texture,GLenum target,GLint level,GLint xoffset,GLint yoffset,GLint zoffset,GLint x,GLint y,GLsizei width,GLsizei height)5040 _mesa_CopyTextureSubImage3DEXT(GLuint texture, GLenum target, GLint level,
5041                                GLint xoffset, GLint yoffset, GLint zoffset,
5042                                GLint x, GLint y, GLsizei width, GLsizei height)
5043 {
5044    struct gl_texture_object* texObj;
5045    const char *self = "glCopyTextureSubImage3D";
5046    GET_CURRENT_CONTEXT(ctx);
5047 
5048    texObj = _mesa_lookup_or_create_texture(ctx, target, texture, false, true, self);
5049    if (!texObj)
5050       return;
5051 
5052    /* Check target (proxies not allowed). */
5053    if (!legal_texsubimage_target(ctx, 3, texObj->Target, true)) {
5054       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid target %s)", self,
5055                   _mesa_enum_to_string(texObj->Target));
5056       return;
5057    }
5058 
5059    if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
5060       /* Act like CopyTexSubImage2D */
5061       copy_texture_sub_image_err(ctx, 2, texObj,
5062                                 GL_TEXTURE_CUBE_MAP_POSITIVE_X + zoffset,
5063                                 level, xoffset, yoffset, 0, x, y, width, height,
5064                                 self);
5065    }
5066    else
5067       copy_texture_sub_image_err(ctx, 3, texObj, texObj->Target, level, xoffset,
5068                                  yoffset, zoffset, x, y, width, height, self);
5069 }
5070 
5071 
5072 void GLAPIENTRY
_mesa_CopyMultiTexSubImage3DEXT(GLenum texunit,GLenum target,GLint level,GLint xoffset,GLint yoffset,GLint zoffset,GLint x,GLint y,GLsizei width,GLsizei height)5073 _mesa_CopyMultiTexSubImage3DEXT(GLenum texunit, GLenum target, GLint level,
5074                                 GLint xoffset, GLint yoffset, GLint zoffset,
5075                                 GLint x, GLint y, GLsizei width, GLsizei height)
5076 {
5077    struct gl_texture_object* texObj;
5078    const char *self = "glCopyMultiTexSubImage3D";
5079    GET_CURRENT_CONTEXT(ctx);
5080 
5081    texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
5082                                                    texunit - GL_TEXTURE0,
5083                                                    false, self);
5084    if (!texObj)
5085       return;
5086 
5087    if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
5088       /* Act like CopyTexSubImage2D */
5089       copy_texture_sub_image_err(ctx, 2, texObj,
5090                                 GL_TEXTURE_CUBE_MAP_POSITIVE_X + zoffset,
5091                                 level, xoffset, yoffset, 0, x, y, width, height,
5092                                 self);
5093    }
5094    else
5095       copy_texture_sub_image_err(ctx, 3, texObj, texObj->Target, level, xoffset,
5096                                  yoffset, zoffset, x, y, width, height, self);
5097 }
5098 
5099 
5100 void GLAPIENTRY
_mesa_CopyTexSubImage1D_no_error(GLenum target,GLint level,GLint xoffset,GLint x,GLint y,GLsizei width)5101 _mesa_CopyTexSubImage1D_no_error(GLenum target, GLint level, GLint xoffset,
5102                                  GLint x, GLint y, GLsizei width)
5103 {
5104    GET_CURRENT_CONTEXT(ctx);
5105 
5106    struct gl_texture_object* texObj = _mesa_get_current_tex_object(ctx, target);
5107    copy_texture_sub_image_no_error(ctx, 1, texObj, target, level, xoffset, 0, 0,
5108                                    x, y, width, 1);
5109 }
5110 
5111 
5112 void GLAPIENTRY
_mesa_CopyTexSubImage2D_no_error(GLenum target,GLint level,GLint xoffset,GLint yoffset,GLint x,GLint y,GLsizei width,GLsizei height)5113 _mesa_CopyTexSubImage2D_no_error(GLenum target, GLint level, GLint xoffset,
5114                                  GLint yoffset, GLint x, GLint y, GLsizei width,
5115                                  GLsizei height)
5116 {
5117    GET_CURRENT_CONTEXT(ctx);
5118 
5119    struct gl_texture_object* texObj = _mesa_get_current_tex_object(ctx, target);
5120    copy_texture_sub_image_no_error(ctx, 2, texObj, target, level, xoffset,
5121                                    yoffset, 0, x, y, width, height);
5122 }
5123 
5124 
5125 void GLAPIENTRY
_mesa_CopyTexSubImage3D_no_error(GLenum target,GLint level,GLint xoffset,GLint yoffset,GLint zoffset,GLint x,GLint y,GLsizei width,GLsizei height)5126 _mesa_CopyTexSubImage3D_no_error(GLenum target, GLint level, GLint xoffset,
5127                                  GLint yoffset, GLint zoffset, GLint x, GLint y,
5128                                  GLsizei width, GLsizei height)
5129 {
5130    GET_CURRENT_CONTEXT(ctx);
5131 
5132    struct gl_texture_object* texObj = _mesa_get_current_tex_object(ctx, target);
5133    copy_texture_sub_image_no_error(ctx, 3, texObj, target, level, xoffset,
5134                                    yoffset, zoffset, x, y, width, height);
5135 }
5136 
5137 
5138 void GLAPIENTRY
_mesa_CopyTextureSubImage1D_no_error(GLuint texture,GLint level,GLint xoffset,GLint x,GLint y,GLsizei width)5139 _mesa_CopyTextureSubImage1D_no_error(GLuint texture, GLint level, GLint xoffset,
5140                                      GLint x, GLint y, GLsizei width)
5141 {
5142    GET_CURRENT_CONTEXT(ctx);
5143 
5144    struct gl_texture_object* texObj = _mesa_lookup_texture(ctx, texture);
5145    copy_texture_sub_image_no_error(ctx, 1, texObj, texObj->Target, level,
5146                                    xoffset, 0, 0, x, y, width, 1);
5147 }
5148 
5149 
5150 void GLAPIENTRY
_mesa_CopyTextureSubImage2D_no_error(GLuint texture,GLint level,GLint xoffset,GLint yoffset,GLint x,GLint y,GLsizei width,GLsizei height)5151 _mesa_CopyTextureSubImage2D_no_error(GLuint texture, GLint level, GLint xoffset,
5152                                      GLint yoffset, GLint x, GLint y,
5153                                      GLsizei width, GLsizei height)
5154 {
5155    GET_CURRENT_CONTEXT(ctx);
5156 
5157    struct gl_texture_object* texObj = _mesa_lookup_texture(ctx, texture);
5158    copy_texture_sub_image_no_error(ctx, 2, texObj, texObj->Target, level,
5159                                    xoffset, yoffset, 0, x, y, width, height);
5160 }
5161 
5162 
5163 void GLAPIENTRY
_mesa_CopyTextureSubImage3D_no_error(GLuint texture,GLint level,GLint xoffset,GLint yoffset,GLint zoffset,GLint x,GLint y,GLsizei width,GLsizei height)5164 _mesa_CopyTextureSubImage3D_no_error(GLuint texture, GLint level, GLint xoffset,
5165                                      GLint yoffset, GLint zoffset, GLint x,
5166                                      GLint y, GLsizei width, GLsizei height)
5167 {
5168    GET_CURRENT_CONTEXT(ctx);
5169 
5170    struct gl_texture_object* texObj = _mesa_lookup_texture(ctx, texture);
5171    if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
5172       /* Act like CopyTexSubImage2D */
5173       copy_texture_sub_image_no_error(ctx, 2, texObj,
5174                                       GL_TEXTURE_CUBE_MAP_POSITIVE_X + zoffset,
5175                                       level, xoffset, yoffset, 0, x, y, width,
5176                                       height);
5177    }
5178    else
5179       copy_texture_sub_image_no_error(ctx, 3, texObj, texObj->Target, level,
5180                                       xoffset, yoffset, zoffset, x, y, width,
5181                                       height);
5182 }
5183 
5184 
5185 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)5186 check_clear_tex_image(struct gl_context *ctx,
5187                       const char *function,
5188                       struct gl_texture_image *texImage,
5189                       GLenum format, GLenum type,
5190                       const void *data,
5191                       GLubyte *clearValue)
5192 {
5193    struct gl_texture_object *texObj = texImage->TexObject;
5194    static const GLubyte zeroData[MAX_PIXEL_BYTES];
5195    GLenum internalFormat = texImage->InternalFormat;
5196    GLenum err;
5197 
5198    if (texObj->Target == GL_TEXTURE_BUFFER) {
5199       _mesa_error(ctx, GL_INVALID_OPERATION,
5200                   "%s(buffer texture)", function);
5201       return false;
5202    }
5203 
5204    if (_mesa_is_compressed_format(ctx, internalFormat)) {
5205       _mesa_error(ctx, GL_INVALID_OPERATION,
5206                   "%s(compressed texture)", function);
5207       return false;
5208    }
5209 
5210    err = _mesa_error_check_format_and_type(ctx, format, type);
5211    if (err != GL_NO_ERROR) {
5212       _mesa_error(ctx, err,
5213                   "%s(incompatible format = %s, type = %s)",
5214                   function,
5215                   _mesa_enum_to_string(format),
5216                   _mesa_enum_to_string(type));
5217       return false;
5218    }
5219 
5220    /* make sure internal format and format basically agree */
5221    if (!texture_formats_agree(internalFormat, format)) {
5222       _mesa_error(ctx, GL_INVALID_OPERATION,
5223                   "%s(incompatible internalFormat = %s, format = %s)",
5224                   function,
5225                   _mesa_enum_to_string(internalFormat),
5226                   _mesa_enum_to_string(format));
5227       return false;
5228    }
5229 
5230    if (ctx->Version >= 30 || ctx->Extensions.EXT_texture_integer) {
5231       /* both source and dest must be integer-valued, or neither */
5232       if (_mesa_is_format_integer_color(texImage->TexFormat) !=
5233           _mesa_is_enum_format_integer(format)) {
5234          _mesa_error(ctx, GL_INVALID_OPERATION,
5235                      "%s(integer/non-integer format mismatch)",
5236                      function);
5237          return false;
5238       }
5239    }
5240 
5241    if (!_mesa_texstore(ctx,
5242                        1, /* dims */
5243                        texImage->_BaseFormat,
5244                        texImage->TexFormat,
5245                        0, /* dstRowStride */
5246                        &clearValue,
5247                        1, 1, 1, /* srcWidth/Height/Depth */
5248                        format, type,
5249                        data ? data : zeroData,
5250                        &ctx->DefaultPacking)) {
5251       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid format)", function);
5252       return false;
5253    }
5254 
5255    return true;
5256 }
5257 
5258 
5259 static struct gl_texture_object *
get_tex_obj_for_clear(struct gl_context * ctx,const char * function,GLuint texture)5260 get_tex_obj_for_clear(struct gl_context *ctx,
5261                       const char *function,
5262                       GLuint texture)
5263 {
5264    struct gl_texture_object *texObj;
5265 
5266    texObj = _mesa_lookup_texture_err(ctx, texture, function);
5267    if (!texObj)
5268       return NULL;
5269 
5270    if (texObj->Target == 0) {
5271       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(unbound tex)", function);
5272       return NULL;
5273    }
5274 
5275    return texObj;
5276 }
5277 
5278 
5279 /**
5280  * For clearing cube textures, the zoffset and depth parameters indicate
5281  * which cube map faces are to be cleared.  This is the one case where we
5282  * need to be concerned with multiple gl_texture_images.  This function
5283  * returns the array of texture images to clear for cube maps, or one
5284  * texture image otherwise.
5285  * \return number of texture images, 0 for error, 6 for cube, 1 otherwise.
5286  */
5287 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)5288 get_tex_images_for_clear(struct gl_context *ctx,
5289                          const char *function,
5290                          struct gl_texture_object *texObj,
5291                          GLint level,
5292                          struct gl_texture_image **texImages)
5293 {
5294    GLenum target;
5295    int numFaces, i;
5296 
5297    if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
5298       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid level)", function);
5299       return 0;
5300    }
5301 
5302    if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
5303       target = GL_TEXTURE_CUBE_MAP_POSITIVE_X;
5304       numFaces = MAX_FACES;
5305    }
5306    else {
5307       target = texObj->Target;
5308       numFaces = 1;
5309    }
5310 
5311    for (i = 0; i < numFaces; i++) {
5312       texImages[i] = _mesa_select_tex_image(texObj, target + i, level);
5313       if (texImages[i] == NULL) {
5314          _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid level)", function);
5315          return 0;
5316       }
5317    }
5318 
5319    return numFaces;
5320 }
5321 
5322 
5323 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)5324 _mesa_ClearTexSubImage(GLuint texture, GLint level,
5325                        GLint xoffset, GLint yoffset, GLint zoffset,
5326                        GLsizei width, GLsizei height, GLsizei depth,
5327                        GLenum format, GLenum type, const void *data)
5328 {
5329    GET_CURRENT_CONTEXT(ctx);
5330    struct gl_texture_object *texObj;
5331    struct gl_texture_image *texImages[MAX_FACES];
5332    GLubyte clearValue[MAX_FACES][MAX_PIXEL_BYTES];
5333    int i, numImages;
5334    int minDepth, maxDepth;
5335 
5336    texObj = get_tex_obj_for_clear(ctx, "glClearTexSubImage", texture);
5337 
5338    if (texObj == NULL)
5339       return;
5340 
5341    _mesa_lock_texture(ctx, texObj);
5342 
5343    numImages = get_tex_images_for_clear(ctx, "glClearTexSubImage",
5344                                         texObj, level, texImages);
5345    if (numImages == 0)
5346       goto out;
5347 
5348    if (numImages == 1) {
5349       minDepth = -(int) texImages[0]->Border;
5350       maxDepth = texImages[0]->Depth;
5351    } else {
5352       assert(numImages == MAX_FACES);
5353       minDepth = 0;
5354       maxDepth = numImages;
5355    }
5356 
5357    if (xoffset < -(GLint) texImages[0]->Border ||
5358        yoffset < -(GLint) texImages[0]->Border ||
5359        zoffset < minDepth ||
5360        width < 0 ||
5361        height < 0 ||
5362        depth < 0 ||
5363        xoffset + width > texImages[0]->Width ||
5364        yoffset + height > texImages[0]->Height ||
5365        zoffset + depth > maxDepth) {
5366       _mesa_error(ctx, GL_INVALID_OPERATION,
5367                   "glClearSubTexImage(invalid dimensions)");
5368       goto out;
5369    }
5370 
5371    if (numImages == 1) {
5372       if (check_clear_tex_image(ctx, "glClearTexSubImage", texImages[0],
5373                                 format, type, data, clearValue[0])) {
5374          st_ClearTexSubImage(ctx,
5375                              texImages[0],
5376                              xoffset, yoffset, zoffset,
5377                              width, height, depth,
5378                              data ? clearValue[0] : NULL);
5379       }
5380    } else {
5381       /* loop over cube face images */
5382       for (i = zoffset; i < zoffset + depth; i++) {
5383          assert(i < MAX_FACES);
5384          if (!check_clear_tex_image(ctx, "glClearTexSubImage", texImages[i],
5385                                     format, type, data, clearValue[i]))
5386             goto out;
5387       }
5388       for (i = zoffset; i < zoffset + depth; i++) {
5389          st_ClearTexSubImage(ctx,
5390                              texImages[i],
5391                              xoffset, yoffset, 0,
5392                              width, height, 1,
5393                              data ? clearValue[i] : NULL);
5394       }
5395    }
5396 
5397  out:
5398    _mesa_unlock_texture(ctx, texObj);
5399 }
5400 
5401 
5402 void GLAPIENTRY
_mesa_ClearTexImage(GLuint texture,GLint level,GLenum format,GLenum type,const void * data)5403 _mesa_ClearTexImage( GLuint texture, GLint level,
5404                      GLenum format, GLenum type, const void *data )
5405 {
5406    GET_CURRENT_CONTEXT(ctx);
5407    struct gl_texture_object *texObj;
5408    struct gl_texture_image *texImages[MAX_FACES];
5409    GLubyte clearValue[MAX_FACES][MAX_PIXEL_BYTES];
5410    int i, numImages;
5411 
5412    texObj = get_tex_obj_for_clear(ctx, "glClearTexImage", texture);
5413 
5414    if (texObj == NULL)
5415       return;
5416 
5417    _mesa_lock_texture(ctx, texObj);
5418 
5419    numImages = get_tex_images_for_clear(ctx, "glClearTexImage",
5420                                         texObj, level, texImages);
5421 
5422    for (i = 0; i < numImages; i++) {
5423       if (!check_clear_tex_image(ctx, "glClearTexImage", texImages[i], format,
5424                                  type, data, clearValue[i]))
5425          goto out;
5426    }
5427 
5428    for (i = 0; i < numImages; i++) {
5429       st_ClearTexSubImage(ctx, texImages[i],
5430                           -(GLint) texImages[i]->Border, /* xoffset */
5431                           -(GLint) texImages[i]->Border, /* yoffset */
5432                           -(GLint) texImages[i]->Border, /* zoffset */
5433                           texImages[i]->Width,
5434                           texImages[i]->Height,
5435                           texImages[i]->Depth,
5436                           data ? clearValue[i] : NULL);
5437    }
5438 
5439 out:
5440    _mesa_unlock_texture(ctx, texObj);
5441 }
5442 
5443 
5444 
5445 
5446 /**********************************************************************/
5447 /******                   Compressed Textures                    ******/
5448 /**********************************************************************/
5449 
5450 
5451 /**
5452  * Target checking for glCompressedTexSubImage[123]D().
5453  * \return GL_TRUE if error, GL_FALSE if no error
5454  * Must come before other error checking so that the texture object can
5455  * be correctly retrieved using _mesa_get_current_tex_object.
5456  */
5457 static GLboolean
compressed_subtexture_target_check(struct gl_context * ctx,GLenum target,GLint dims,GLenum intFormat,bool dsa,const char * caller)5458 compressed_subtexture_target_check(struct gl_context *ctx, GLenum target,
5459                                    GLint dims, GLenum intFormat, bool dsa,
5460                                    const char *caller)
5461 {
5462    GLboolean targetOK;
5463    mesa_format format;
5464    enum mesa_format_layout layout;
5465 
5466    if (dsa && target == GL_TEXTURE_RECTANGLE) {
5467       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid target %s)", caller,
5468                   _mesa_enum_to_string(target));
5469       return GL_TRUE;
5470    }
5471 
5472    switch (dims) {
5473    case 2:
5474       switch (target) {
5475       case GL_TEXTURE_2D:
5476          targetOK = GL_TRUE;
5477          break;
5478       case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
5479       case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
5480       case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
5481       case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
5482       case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
5483       case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
5484          targetOK = GL_TRUE;
5485          break;
5486       default:
5487          targetOK = GL_FALSE;
5488          break;
5489       }
5490       break;
5491    case 3:
5492       switch (target) {
5493       case GL_TEXTURE_CUBE_MAP:
5494          targetOK = dsa;
5495          break;
5496       case GL_TEXTURE_2D_ARRAY:
5497          targetOK = _mesa_is_gles3(ctx) ||
5498             (_mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array);
5499          break;
5500       case GL_TEXTURE_CUBE_MAP_ARRAY:
5501          targetOK = _mesa_has_texture_cube_map_array(ctx);
5502          break;
5503       case GL_TEXTURE_3D:
5504          targetOK = GL_TRUE;
5505          /*
5506           * OpenGL 4.5 spec (30.10.2014) says in Section 8.7 Compressed Texture
5507           * Images:
5508           *    "An INVALID_OPERATION error is generated by
5509           *    CompressedTex*SubImage3D if the internal format of the texture
5510           *    is one of the EAC, ETC2, or RGTC formats and either border is
5511           *    non-zero, or the effective target for the texture is not
5512           *    TEXTURE_2D_ARRAY."
5513           *
5514           * NOTE: that's probably a spec error.  It should probably say
5515           *    "... or the effective target for the texture is not
5516           *    TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP, nor
5517           *    GL_TEXTURE_CUBE_MAP_ARRAY."
5518           * since those targets are 2D images and they support all compression
5519           * formats.
5520           *
5521           * Instead of listing all these, just list those which are allowed,
5522           * which is (at this time) only bptc. Otherwise we'd say s3tc (and
5523           * more) are valid here, which they are not, but of course not
5524           * mentioned by core spec.
5525           *
5526           * Also, from GL_KHR_texture_compression_astc_{hdr,ldr}:
5527           *
5528           *    "Add a second new column "3D Tex." which is empty for all non-ASTC
5529           *     formats. If only the LDR profile is supported by the implementation,
5530           *     this column is also empty for all ASTC formats. If both the LDR and HDR
5531           *     profiles are supported, this column is checked for all ASTC formats."
5532           *
5533           *    "An INVALID_OPERATION error is generated by CompressedTexSubImage3D if
5534           *     <format> is one of the formats in table 8.19 and <target> is not
5535           *     TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP_ARRAY, or TEXTURE_3D.
5536           *
5537           *     An INVALID_OPERATION error is generated by CompressedTexSubImage3D if
5538           *     <format> is TEXTURE_CUBE_MAP_ARRAY and the "Cube Map Array" column of
5539           *     table 8.19 is *not* checked, or if <format> is TEXTURE_3D and the "3D
5540           *     Tex." column of table 8.19 is *not* checked"
5541           *
5542           * And from GL_KHR_texture_compression_astc_sliced_3d:
5543           *
5544           *    "Modify the "3D Tex." column to be checked for all ASTC formats."
5545           */
5546          format = _mesa_glenum_to_compressed_format(intFormat);
5547          layout = _mesa_get_format_layout(format);
5548          switch (layout) {
5549          case MESA_FORMAT_LAYOUT_BPTC:
5550             /* valid format */
5551             break;
5552          case MESA_FORMAT_LAYOUT_S3TC:
5553             targetOK =
5554                ctx->Extensions.EXT_texture_compression_s3tc &&
5555                (_mesa_is_gles3(ctx) || ctx->Extensions.ARB_ES3_compatibility);
5556             break;
5557          case MESA_FORMAT_LAYOUT_ASTC:
5558             targetOK =
5559                ctx->Extensions.KHR_texture_compression_astc_hdr ||
5560                ctx->Extensions.KHR_texture_compression_astc_sliced_3d;
5561             break;
5562          default:
5563             /* invalid format */
5564             _mesa_error(ctx, GL_INVALID_OPERATION,
5565                         "%s(invalid target %s for format %s)", caller,
5566                         _mesa_enum_to_string(target),
5567                         _mesa_enum_to_string(intFormat));
5568             return GL_TRUE;
5569          }
5570          break;
5571       default:
5572          targetOK = GL_FALSE;
5573       }
5574 
5575       break;
5576    default:
5577       assert(dims == 1);
5578       /* no 1D compressed textures at this time */
5579       targetOK = GL_FALSE;
5580       break;
5581    }
5582 
5583    if (!targetOK) {
5584       _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", caller,
5585                   _mesa_enum_to_string(target));
5586       return GL_TRUE;
5587    }
5588 
5589    return GL_FALSE;
5590 }
5591 
5592 /**
5593  * Error checking for glCompressedTexSubImage[123]D().
5594  * \return GL_TRUE if error, GL_FALSE if no error
5595  */
5596 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)5597 compressed_subtexture_error_check(struct gl_context *ctx, GLint dims,
5598                                   const struct gl_texture_object *texObj,
5599                                   GLenum target, GLint level,
5600                                   GLint xoffset, GLint yoffset, GLint zoffset,
5601                                   GLsizei width, GLsizei height, GLsizei depth,
5602                                   GLenum format, GLsizei imageSize,
5603                                   const GLvoid *data, const char *callerName)
5604 {
5605    struct gl_texture_image *texImage;
5606    GLint expectedSize;
5607 
5608    GLenum is_generic_compressed_token =
5609       _mesa_generic_compressed_format_to_uncompressed_format(format) !=
5610       format;
5611 
5612    /* OpenGL 4.6 and OpenGL ES 3.2 spec:
5613     *
5614     *   "An INVALID_OPERATION error is generated if format does not match the
5615     *    internal format of the texture image being modified, since these commands do
5616     *    not provide for image format conversion."
5617     *
5618     *  Desktop spec has an additional rule for GL_INVALID_ENUM:
5619     *
5620     *   "An INVALID_ENUM error is generated if format is one of the generic
5621     *    compressed internal formats."
5622     */
5623    /* this will catch any invalid compressed format token */
5624    if (!_mesa_is_compressed_format(ctx, format)) {
5625       GLenum error = _mesa_is_desktop_gl(ctx) && is_generic_compressed_token ?
5626          GL_INVALID_ENUM : GL_INVALID_OPERATION;
5627       _mesa_error(ctx, error, "%s(format)", callerName);
5628       return GL_TRUE;
5629    }
5630 
5631    if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
5632       _mesa_error(ctx, GL_INVALID_VALUE, "%s(level=%d)", callerName, level);
5633       return GL_TRUE;
5634    }
5635 
5636    /* validate the bound PBO, if any */
5637    if (!_mesa_validate_pbo_source_compressed(ctx, dims, &ctx->Unpack,
5638                                      imageSize, data, callerName)) {
5639       return GL_TRUE;
5640    }
5641 
5642    /* Check for invalid pixel storage modes */
5643    if (!_mesa_compressed_pixel_storage_error_check(ctx, dims,
5644                                                    &ctx->Unpack, callerName)) {
5645       return GL_TRUE;
5646    }
5647 
5648    expectedSize = compressed_tex_size(width, height, depth, format);
5649    if (expectedSize != imageSize) {
5650       _mesa_error(ctx, GL_INVALID_VALUE, "%s(size=%d)", callerName, imageSize);
5651       return GL_TRUE;
5652    }
5653 
5654    texImage = _mesa_select_tex_image(texObj, target, level);
5655    if (!texImage) {
5656       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid texture level %d)",
5657                   callerName, level);
5658       return GL_TRUE;
5659    }
5660 
5661    if ((GLint) format != texImage->InternalFormat) {
5662       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(format=%s)",
5663                   callerName, _mesa_enum_to_string(format));
5664       return GL_TRUE;
5665    }
5666 
5667    if (compressedteximage_only_format(format)) {
5668       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(format=%s cannot be updated)",
5669                   callerName, _mesa_enum_to_string(format));
5670       return GL_TRUE;
5671    }
5672 
5673    if (error_check_subtexture_negative_dimensions(ctx, dims, width, height,
5674                                                   depth, callerName)) {
5675       return GL_TRUE;
5676    }
5677 
5678    if (error_check_subtexture_dimensions(ctx, dims, texImage, xoffset, yoffset,
5679                                          zoffset, width, height, depth,
5680                                          callerName)) {
5681       return GL_TRUE;
5682    }
5683 
5684    return GL_FALSE;
5685 }
5686 
5687 
5688 void GLAPIENTRY
_mesa_CompressedTexImage1D(GLenum target,GLint level,GLenum internalFormat,GLsizei width,GLint border,GLsizei imageSize,const GLvoid * data)5689 _mesa_CompressedTexImage1D(GLenum target, GLint level,
5690                               GLenum internalFormat, GLsizei width,
5691                               GLint border, GLsizei imageSize,
5692                               const GLvoid *data)
5693 {
5694    GET_CURRENT_CONTEXT(ctx);
5695    teximage_err(ctx, GL_TRUE, 1, target, level, internalFormat,
5696                 width, 1, 1, border, GL_NONE, GL_NONE, imageSize, data);
5697 }
5698 
5699 
5700 void GLAPIENTRY
_mesa_CompressedTextureImage1DEXT(GLuint texture,GLenum target,GLint level,GLenum internalFormat,GLsizei width,GLint border,GLsizei imageSize,const GLvoid * pixels)5701 _mesa_CompressedTextureImage1DEXT(GLuint texture, GLenum target, GLint level,
5702                                   GLenum internalFormat, GLsizei width,
5703                                   GLint border, GLsizei imageSize,
5704                                   const GLvoid *pixels)
5705 {
5706    struct gl_texture_object*  texObj;
5707    GET_CURRENT_CONTEXT(ctx);
5708 
5709    texObj = _mesa_lookup_or_create_texture(ctx, target, texture, false, true,
5710                                            "glCompressedTextureImage1DEXT");
5711    if (!texObj)
5712       return;
5713    teximage(ctx, GL_TRUE, 1, texObj, target, level, internalFormat,
5714             width, 1, 1, border, GL_NONE, GL_NONE, imageSize, pixels, false);
5715 }
5716 
5717 
5718 void GLAPIENTRY
_mesa_CompressedMultiTexImage1DEXT(GLenum texunit,GLenum target,GLint level,GLenum internalFormat,GLsizei width,GLint border,GLsizei imageSize,const GLvoid * pixels)5719 _mesa_CompressedMultiTexImage1DEXT(GLenum texunit, GLenum target, GLint level,
5720                                    GLenum internalFormat, GLsizei width,
5721                                    GLint border, GLsizei imageSize,
5722                                    const GLvoid *pixels)
5723 {
5724    struct gl_texture_object*  texObj;
5725    GET_CURRENT_CONTEXT(ctx);
5726 
5727    texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
5728                                                    texunit - GL_TEXTURE0,
5729                                                    true,
5730                                                    "glCompressedMultiTexImage1DEXT");
5731    if (!texObj)
5732       return;
5733    teximage(ctx, GL_TRUE, 1, texObj, target, level, internalFormat,
5734             width, 1, 1, border, GL_NONE, GL_NONE, imageSize, pixels, false);
5735 }
5736 
5737 
5738 void GLAPIENTRY
_mesa_CompressedTexImage2D(GLenum target,GLint level,GLenum internalFormat,GLsizei width,GLsizei height,GLint border,GLsizei imageSize,const GLvoid * data)5739 _mesa_CompressedTexImage2D(GLenum target, GLint level,
5740                               GLenum internalFormat, GLsizei width,
5741                               GLsizei height, GLint border, GLsizei imageSize,
5742                               const GLvoid *data)
5743 {
5744    GET_CURRENT_CONTEXT(ctx);
5745    teximage_err(ctx, GL_TRUE, 2, target, level, internalFormat,
5746                 width, height, 1, border, GL_NONE, GL_NONE, imageSize, data);
5747 }
5748 
5749 
5750 void GLAPIENTRY
_mesa_CompressedTextureImage2DEXT(GLuint texture,GLenum target,GLint level,GLenum internalFormat,GLsizei width,GLsizei height,GLint border,GLsizei imageSize,const GLvoid * pixels)5751 _mesa_CompressedTextureImage2DEXT(GLuint texture, GLenum target, GLint level,
5752                                   GLenum internalFormat, GLsizei width,
5753                                   GLsizei height, GLint border, GLsizei imageSize,
5754                                   const GLvoid *pixels)
5755 {
5756    struct gl_texture_object*  texObj;
5757    GET_CURRENT_CONTEXT(ctx);
5758 
5759    texObj = _mesa_lookup_or_create_texture(ctx, target, texture, false, true,
5760                                            "glCompressedTextureImage2DEXT");
5761    if (!texObj)
5762       return;
5763    teximage(ctx, GL_TRUE, 2, texObj, target, level, internalFormat,
5764             width, height, 1, border, GL_NONE, GL_NONE, imageSize, pixels, false);
5765 }
5766 
5767 
5768 void GLAPIENTRY
_mesa_CompressedMultiTexImage2DEXT(GLenum texunit,GLenum target,GLint level,GLenum internalFormat,GLsizei width,GLsizei height,GLint border,GLsizei imageSize,const GLvoid * pixels)5769 _mesa_CompressedMultiTexImage2DEXT(GLenum texunit, GLenum target, GLint level,
5770                                    GLenum internalFormat, GLsizei width,
5771                                    GLsizei height, GLint border, GLsizei imageSize,
5772                                    const GLvoid *pixels)
5773 {
5774    struct gl_texture_object*  texObj;
5775    GET_CURRENT_CONTEXT(ctx);
5776 
5777    texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
5778                                                    texunit - GL_TEXTURE0,
5779                                                    true,
5780                                                    "glCompressedMultiTexImage2DEXT");
5781    if (!texObj)
5782       return;
5783    teximage(ctx, GL_TRUE, 2, texObj, target, level, internalFormat,
5784             width, height, 1, border, GL_NONE, GL_NONE, imageSize, pixels, false);
5785 }
5786 
5787 
5788 void GLAPIENTRY
_mesa_CompressedTexImage3D(GLenum target,GLint level,GLenum internalFormat,GLsizei width,GLsizei height,GLsizei depth,GLint border,GLsizei imageSize,const GLvoid * data)5789 _mesa_CompressedTexImage3D(GLenum target, GLint level,
5790                               GLenum internalFormat, GLsizei width,
5791                               GLsizei height, GLsizei depth, GLint border,
5792                               GLsizei imageSize, const GLvoid *data)
5793 {
5794    GET_CURRENT_CONTEXT(ctx);
5795    teximage_err(ctx, GL_TRUE, 3, target, level, internalFormat, width, height,
5796                 depth, border, GL_NONE, GL_NONE, imageSize, data);
5797 }
5798 
5799 
5800 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)5801 _mesa_CompressedTextureImage3DEXT(GLuint texture, GLenum target, GLint level,
5802                                   GLenum internalFormat, GLsizei width,
5803                                   GLsizei height, GLsizei depth, GLint border,
5804                                   GLsizei imageSize, const GLvoid *pixels)
5805 {
5806    struct gl_texture_object*  texObj;
5807    GET_CURRENT_CONTEXT(ctx);
5808 
5809    texObj = _mesa_lookup_or_create_texture(ctx, target, texture, false, true,
5810                                            "glCompressedTextureImage3DEXT");
5811    if (!texObj)
5812       return;
5813    teximage(ctx, GL_TRUE, 3, texObj, target, level, internalFormat,
5814             width, height, depth, border, GL_NONE, GL_NONE, imageSize, pixels, false);
5815 }
5816 
5817 
5818 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)5819 _mesa_CompressedMultiTexImage3DEXT(GLenum texunit, GLenum target, GLint level,
5820                                    GLenum internalFormat, GLsizei width,
5821                                    GLsizei height, GLsizei depth, GLint border,
5822                                    GLsizei imageSize, const GLvoid *pixels)
5823 {
5824    struct gl_texture_object*  texObj;
5825    GET_CURRENT_CONTEXT(ctx);
5826 
5827    texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
5828                                                    texunit - GL_TEXTURE0,
5829                                                    true,
5830                                                    "glCompressedMultiTexImage3DEXT");
5831    if (!texObj)
5832       return;
5833    teximage(ctx, GL_TRUE, 3, texObj, target, level, internalFormat,
5834             width, height, depth, border, GL_NONE, GL_NONE, imageSize, pixels, false);
5835 }
5836 
5837 
5838 void GLAPIENTRY
_mesa_CompressedTexImage1D_no_error(GLenum target,GLint level,GLenum internalFormat,GLsizei width,GLint border,GLsizei imageSize,const GLvoid * data)5839 _mesa_CompressedTexImage1D_no_error(GLenum target, GLint level,
5840                                     GLenum internalFormat, GLsizei width,
5841                                     GLint border, GLsizei imageSize,
5842                                     const GLvoid *data)
5843 {
5844    GET_CURRENT_CONTEXT(ctx);
5845    teximage_no_error(ctx, GL_TRUE, 1, target, level, internalFormat, width, 1,
5846                      1, border, GL_NONE, GL_NONE, imageSize, data);
5847 }
5848 
5849 
5850 void GLAPIENTRY
_mesa_CompressedTexImage2D_no_error(GLenum target,GLint level,GLenum internalFormat,GLsizei width,GLsizei height,GLint border,GLsizei imageSize,const GLvoid * data)5851 _mesa_CompressedTexImage2D_no_error(GLenum target, GLint level,
5852                                     GLenum internalFormat, GLsizei width,
5853                                     GLsizei height, GLint border,
5854                                     GLsizei imageSize, const GLvoid *data)
5855 {
5856    GET_CURRENT_CONTEXT(ctx);
5857    teximage_no_error(ctx, GL_TRUE, 2, target, level, internalFormat, width,
5858                      height, 1, border, GL_NONE, GL_NONE, imageSize, data);
5859 }
5860 
5861 
5862 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)5863 _mesa_CompressedTexImage3D_no_error(GLenum target, GLint level,
5864                                     GLenum internalFormat, GLsizei width,
5865                                     GLsizei height, GLsizei depth, GLint border,
5866                                     GLsizei imageSize, const GLvoid *data)
5867 {
5868    GET_CURRENT_CONTEXT(ctx);
5869    teximage_no_error(ctx, GL_TRUE, 3, target, level, internalFormat, width,
5870                      height, depth, border, GL_NONE, GL_NONE, imageSize, data);
5871 }
5872 
5873 
5874 /**
5875  * Common helper for glCompressedTexSubImage1/2/3D() and
5876  * glCompressedTextureSubImage1/2/3D().
5877  */
5878 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)5879 compressed_texture_sub_image(struct gl_context *ctx, GLuint dims,
5880                              struct gl_texture_object *texObj,
5881                              struct gl_texture_image *texImage,
5882                              GLenum target, GLint level, GLint xoffset,
5883                              GLint yoffset, GLint zoffset, GLsizei width,
5884                              GLsizei height, GLsizei depth, GLenum format,
5885                              GLsizei imageSize, const GLvoid *data)
5886 {
5887    FLUSH_VERTICES(ctx, 0, 0);
5888 
5889    _mesa_lock_texture(ctx, texObj);
5890    {
5891       if (width > 0 && height > 0 && depth > 0) {
5892          st_CompressedTexSubImage(ctx, dims, texImage,
5893                                   xoffset, yoffset, zoffset,
5894                                   width, height, depth,
5895                                   format, imageSize, data);
5896 
5897          check_gen_mipmap(ctx, target, texObj, level);
5898 
5899          /* NOTE: Don't signal _NEW_TEXTURE_OBJECT since we've only changed
5900           * the texel data, not the texture format, size, etc.
5901           */
5902       }
5903    }
5904    _mesa_unlock_texture(ctx, texObj);
5905 }
5906 
5907 
5908 enum tex_mode {
5909    /* Use bound texture to current unit */
5910    TEX_MODE_CURRENT_NO_ERROR = 0,
5911    TEX_MODE_CURRENT_ERROR,
5912    /* Use the specified texture name */
5913    TEX_MODE_DSA_NO_ERROR,
5914    TEX_MODE_DSA_ERROR,
5915    /* Use the specified texture name + target */
5916    TEX_MODE_EXT_DSA_TEXTURE,
5917    /* Use the specified texture unit + target */
5918    TEX_MODE_EXT_DSA_TEXUNIT,
5919 };
5920 
5921 
5922 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)5923 compressed_tex_sub_image(unsigned dim, GLenum target, GLuint textureOrIndex,
5924                          GLint level, GLint xoffset, GLint yoffset,
5925                          GLint zoffset, GLsizei width, GLsizei height,
5926                          GLsizei depth, GLenum format, GLsizei imageSize,
5927                          const GLvoid *data, enum tex_mode mode,
5928                          const char *caller)
5929 {
5930    struct gl_texture_object *texObj = NULL;
5931    struct gl_texture_image *texImage;
5932    bool no_error = false;
5933    GET_CURRENT_CONTEXT(ctx);
5934 
5935    switch (mode) {
5936       case TEX_MODE_DSA_ERROR:
5937          assert(target == 0);
5938          texObj = _mesa_lookup_texture_err(ctx, textureOrIndex, caller);
5939          if (texObj)
5940             target = texObj->Target;
5941          break;
5942       case TEX_MODE_DSA_NO_ERROR:
5943          assert(target == 0);
5944          texObj = _mesa_lookup_texture(ctx, textureOrIndex);
5945          if (texObj)
5946             target = texObj->Target;
5947          no_error = true;
5948          break;
5949       case TEX_MODE_EXT_DSA_TEXTURE:
5950          texObj = _mesa_lookup_or_create_texture(ctx, target, textureOrIndex,
5951                                                  false, true, caller);
5952          break;
5953       case TEX_MODE_EXT_DSA_TEXUNIT:
5954          texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
5955                                                          textureOrIndex,
5956                                                          false,
5957                                                          caller);
5958          break;
5959       case TEX_MODE_CURRENT_NO_ERROR:
5960          no_error = true;
5961          FALLTHROUGH;
5962       case TEX_MODE_CURRENT_ERROR:
5963       default:
5964          assert(textureOrIndex == 0);
5965          break;
5966    }
5967 
5968    if (!no_error &&
5969        compressed_subtexture_target_check(ctx, target, dim, format,
5970                                           mode == TEX_MODE_DSA_ERROR,
5971                                           caller)) {
5972       return;
5973    }
5974 
5975    if (mode == TEX_MODE_CURRENT_NO_ERROR ||
5976        mode == TEX_MODE_CURRENT_ERROR) {
5977       texObj = _mesa_get_current_tex_object(ctx, target);
5978    }
5979 
5980    if (!texObj)
5981       return;
5982 
5983    if (!no_error &&
5984        compressed_subtexture_error_check(ctx, dim, texObj, target, level,
5985                                          xoffset, yoffset, zoffset, width,
5986                                          height, depth, format,
5987                                          imageSize, data, caller)) {
5988       return;
5989    }
5990 
5991    /* Must handle special case GL_TEXTURE_CUBE_MAP. */
5992    if (dim == 3 &&
5993        (mode == TEX_MODE_DSA_ERROR || mode == TEX_MODE_DSA_NO_ERROR) &&
5994        texObj->Target == GL_TEXTURE_CUBE_MAP) {
5995       const char *pixels = data;
5996       GLint image_stride;
5997 
5998       /* Make sure the texture object is a proper cube.
5999        * (See texturesubimage in teximage.c for details on why this check is
6000        * performed.)
6001        */
6002       if (!no_error && !_mesa_cube_level_complete(texObj, level)) {
6003          _mesa_error(ctx, GL_INVALID_OPERATION,
6004                      "glCompressedTextureSubImage3D(cube map incomplete)");
6005          return;
6006       }
6007 
6008       /* Copy in each face. */
6009       for (int i = zoffset; i < zoffset + depth; ++i) {
6010          texImage = texObj->Image[i][level];
6011          assert(texImage);
6012 
6013          compressed_texture_sub_image(ctx, 3, texObj, texImage,
6014                                       texObj->Target, level, xoffset, yoffset,
6015                                       0, width, height, 1, format,
6016                                       imageSize, pixels);
6017 
6018          /* Compressed images don't have a client format */
6019          image_stride = _mesa_format_image_size(texImage->TexFormat,
6020                                                 texImage->Width,
6021                                                 texImage->Height, 1);
6022 
6023          pixels += image_stride;
6024          imageSize -= image_stride;
6025       }
6026    } else {
6027       texImage = _mesa_select_tex_image(texObj, target, level);
6028       assert(texImage);
6029 
6030       compressed_texture_sub_image(ctx, dim, texObj, texImage, target, level,
6031                                    xoffset, yoffset, zoffset, width, height,
6032                                    depth, format, imageSize, data);
6033    }
6034 }
6035 
6036 
6037 void GLAPIENTRY
_mesa_CompressedTexSubImage1D_no_error(GLenum target,GLint level,GLint xoffset,GLsizei width,GLenum format,GLsizei imageSize,const GLvoid * data)6038 _mesa_CompressedTexSubImage1D_no_error(GLenum target, GLint level,
6039                                        GLint xoffset, GLsizei width,
6040                                        GLenum format, GLsizei imageSize,
6041                                        const GLvoid *data)
6042 {
6043    compressed_tex_sub_image(1, target, 0,
6044                             level, xoffset, 0, 0, width,
6045                             1, 1, format, imageSize, data,
6046                             TEX_MODE_CURRENT_NO_ERROR,
6047                             "glCompressedTexSubImage1D");
6048 }
6049 
6050 
6051 void GLAPIENTRY
_mesa_CompressedTexSubImage1D(GLenum target,GLint level,GLint xoffset,GLsizei width,GLenum format,GLsizei imageSize,const GLvoid * data)6052 _mesa_CompressedTexSubImage1D(GLenum target, GLint level, GLint xoffset,
6053                               GLsizei width, GLenum format,
6054                               GLsizei imageSize, const GLvoid *data)
6055 {
6056    compressed_tex_sub_image(1, target, 0,
6057                             level, xoffset, 0, 0, width,
6058                             1, 1, format, imageSize, data,
6059                             TEX_MODE_CURRENT_ERROR,
6060                             "glCompressedTexSubImage1D");
6061 }
6062 
6063 
6064 void GLAPIENTRY
_mesa_CompressedTextureSubImage1D_no_error(GLuint texture,GLint level,GLint xoffset,GLsizei width,GLenum format,GLsizei imageSize,const GLvoid * data)6065 _mesa_CompressedTextureSubImage1D_no_error(GLuint texture, GLint level,
6066                                            GLint xoffset, GLsizei width,
6067                                            GLenum format, GLsizei imageSize,
6068                                            const GLvoid *data)
6069 {
6070    compressed_tex_sub_image(1, 0, texture,
6071                             level, xoffset, 0, 0,
6072                             width, 1, 1, format, imageSize, data,
6073                             TEX_MODE_DSA_NO_ERROR,
6074                             "glCompressedTextureSubImage1D");
6075 }
6076 
6077 
6078 void GLAPIENTRY
_mesa_CompressedTextureSubImage1D(GLuint texture,GLint level,GLint xoffset,GLsizei width,GLenum format,GLsizei imageSize,const GLvoid * data)6079 _mesa_CompressedTextureSubImage1D(GLuint texture, GLint level, GLint xoffset,
6080                                   GLsizei width, GLenum format,
6081                                   GLsizei imageSize, const GLvoid *data)
6082 {
6083    compressed_tex_sub_image(1, 0, texture,
6084                             level, xoffset, 0, 0,
6085                             width, 1, 1, format, imageSize, data,
6086                             TEX_MODE_DSA_ERROR,
6087                             "glCompressedTextureSubImage1D");
6088 }
6089 
6090 
6091 void GLAPIENTRY
_mesa_CompressedTextureSubImage1DEXT(GLuint texture,GLenum target,GLint level,GLint xoffset,GLsizei width,GLenum format,GLsizei imageSize,const GLvoid * data)6092 _mesa_CompressedTextureSubImage1DEXT(GLuint texture, GLenum target,
6093                                      GLint level, GLint xoffset,
6094                                      GLsizei width, GLenum format,
6095                                      GLsizei imageSize, const GLvoid *data)
6096 {
6097    compressed_tex_sub_image(1, target, texture, level, xoffset, 0,
6098                             0, width, 1, 1, format, imageSize,
6099                             data,
6100                             TEX_MODE_EXT_DSA_TEXTURE,
6101                             "glCompressedTextureSubImage1DEXT");
6102 }
6103 
6104 
6105 void GLAPIENTRY
_mesa_CompressedMultiTexSubImage1DEXT(GLenum texunit,GLenum target,GLint level,GLint xoffset,GLsizei width,GLenum format,GLsizei imageSize,const GLvoid * data)6106 _mesa_CompressedMultiTexSubImage1DEXT(GLenum texunit, GLenum target,
6107                                       GLint level, GLint xoffset,
6108                                       GLsizei width, GLenum format,
6109                                       GLsizei imageSize, const GLvoid *data)
6110 {
6111    compressed_tex_sub_image(1, target, texunit - GL_TEXTURE0, level,
6112                             xoffset, 0, 0, width, 1, 1, format, imageSize,
6113                             data,
6114                             TEX_MODE_EXT_DSA_TEXUNIT,
6115                             "glCompressedMultiTexSubImage1DEXT");
6116 }
6117 
6118 
6119 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)6120 _mesa_CompressedTexSubImage2D_no_error(GLenum target, GLint level,
6121                                        GLint xoffset, GLint yoffset,
6122                                        GLsizei width, GLsizei height,
6123                                        GLenum format, GLsizei imageSize,
6124                                        const GLvoid *data)
6125 {
6126    compressed_tex_sub_image(2, target, 0, level,
6127                             xoffset, yoffset, 0,
6128                             width, height, 1, format, imageSize, data,
6129                             TEX_MODE_CURRENT_NO_ERROR,
6130                             "glCompressedTexSubImage2D");
6131 }
6132 
6133 
6134 void GLAPIENTRY
_mesa_CompressedTexSubImage2D(GLenum target,GLint level,GLint xoffset,GLint yoffset,GLsizei width,GLsizei height,GLenum format,GLsizei imageSize,const GLvoid * data)6135 _mesa_CompressedTexSubImage2D(GLenum target, GLint level, GLint xoffset,
6136                               GLint yoffset, GLsizei width, GLsizei height,
6137                               GLenum format, GLsizei imageSize,
6138                               const GLvoid *data)
6139 {
6140    compressed_tex_sub_image(2, target, 0, level,
6141                             xoffset, yoffset, 0,
6142                             width, height, 1, format, imageSize, data,
6143                             TEX_MODE_CURRENT_ERROR,
6144                             "glCompressedTexSubImage2D");
6145 }
6146 
6147 
6148 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)6149 _mesa_CompressedTextureSubImage2DEXT(GLuint texture, GLenum target,
6150                                      GLint level, GLint xoffset,
6151                                      GLint yoffset, GLsizei width,
6152                                      GLsizei height, GLenum format,
6153                                      GLsizei imageSize, const GLvoid *data)
6154 {
6155    compressed_tex_sub_image(2, target, texture, level, xoffset,
6156                             yoffset, 0, width, height, 1, format,
6157                             imageSize, data,
6158                             TEX_MODE_EXT_DSA_TEXTURE,
6159                             "glCompressedTextureSubImage2DEXT");
6160 }
6161 
6162 
6163 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)6164 _mesa_CompressedMultiTexSubImage2DEXT(GLenum texunit, GLenum target,
6165                                       GLint level, GLint xoffset, GLint yoffset,
6166                                       GLsizei width, GLsizei height, GLenum format,
6167                                       GLsizei imageSize, const GLvoid *data)
6168 {
6169    compressed_tex_sub_image(2, target, texunit - GL_TEXTURE0, level,
6170                             xoffset, yoffset, 0, width, height, 1, format,
6171                             imageSize, data,
6172                             TEX_MODE_EXT_DSA_TEXUNIT,
6173                             "glCompressedMultiTexSubImage2DEXT");
6174 }
6175 
6176 
6177 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)6178 _mesa_CompressedTextureSubImage2D_no_error(GLuint texture, GLint level,
6179                                            GLint xoffset, GLint yoffset,
6180                                            GLsizei width, GLsizei height,
6181                                            GLenum format, GLsizei imageSize,
6182                                            const GLvoid *data)
6183 {
6184    compressed_tex_sub_image(2, 0, texture, level, xoffset, yoffset, 0,
6185                             width, height, 1, format, imageSize, data,
6186                             TEX_MODE_DSA_NO_ERROR,
6187                             "glCompressedTextureSubImage2D");
6188 }
6189 
6190 
6191 void GLAPIENTRY
_mesa_CompressedTextureSubImage2D(GLuint texture,GLint level,GLint xoffset,GLint yoffset,GLsizei width,GLsizei height,GLenum format,GLsizei imageSize,const GLvoid * data)6192 _mesa_CompressedTextureSubImage2D(GLuint texture, GLint level, GLint xoffset,
6193                                   GLint yoffset,
6194                                   GLsizei width, GLsizei height,
6195                                   GLenum format, GLsizei imageSize,
6196                                   const GLvoid *data)
6197 {
6198    compressed_tex_sub_image(2, 0, texture, level, xoffset, yoffset, 0,
6199                             width, height, 1, format, imageSize, data,
6200                             TEX_MODE_DSA_ERROR,
6201                             "glCompressedTextureSubImage2D");
6202 }
6203 
6204 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)6205 _mesa_CompressedTexSubImage3D_no_error(GLenum target, GLint level,
6206                                        GLint xoffset, GLint yoffset,
6207                                        GLint zoffset, GLsizei width,
6208                                        GLsizei height, GLsizei depth,
6209                                        GLenum format, GLsizei imageSize,
6210                                        const GLvoid *data)
6211 {
6212    compressed_tex_sub_image(3, target, 0, level, xoffset, yoffset,
6213                             zoffset, width, height, depth, format,
6214                             imageSize, data,
6215                             TEX_MODE_CURRENT_NO_ERROR,
6216                             "glCompressedTexSubImage3D");
6217 }
6218 
6219 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)6220 _mesa_CompressedTexSubImage3D(GLenum target, GLint level, GLint xoffset,
6221                               GLint yoffset, GLint zoffset, GLsizei width,
6222                               GLsizei height, GLsizei depth, GLenum format,
6223                               GLsizei imageSize, const GLvoid *data)
6224 {
6225    compressed_tex_sub_image(3, target, 0, level, xoffset, yoffset,
6226                             zoffset, width, height, depth, format,
6227                             imageSize, data,
6228                             TEX_MODE_CURRENT_ERROR,
6229                             "glCompressedTexSubImage3D");
6230 }
6231 
6232 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)6233 _mesa_CompressedTextureSubImage3D_no_error(GLuint texture, GLint level,
6234                                            GLint xoffset, GLint yoffset,
6235                                            GLint zoffset, GLsizei width,
6236                                            GLsizei height, GLsizei depth,
6237                                            GLenum format, GLsizei imageSize,
6238                                            const GLvoid *data)
6239 {
6240    compressed_tex_sub_image(3, 0, texture, level, xoffset, yoffset,
6241                             zoffset, width, height, depth, format,
6242                             imageSize, data,
6243                             TEX_MODE_DSA_NO_ERROR,
6244                             "glCompressedTextureSubImage3D");
6245 }
6246 
6247 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)6248 _mesa_CompressedTextureSubImage3D(GLuint texture, GLint level, GLint xoffset,
6249                                   GLint yoffset, GLint zoffset, GLsizei width,
6250                                   GLsizei height, GLsizei depth,
6251                                   GLenum format, GLsizei imageSize,
6252                                   const GLvoid *data)
6253 {
6254    compressed_tex_sub_image(3, 0, texture, level, xoffset, yoffset,
6255                             zoffset, width, height, depth, format,
6256                             imageSize, data,
6257                             TEX_MODE_DSA_ERROR,
6258                             "glCompressedTextureSubImage3D");
6259 }
6260 
6261 
6262 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)6263 _mesa_CompressedTextureSubImage3DEXT(GLuint texture, GLenum target,
6264                                      GLint level, GLint xoffset,
6265                                      GLint yoffset, GLint zoffset,
6266                                      GLsizei width, GLsizei height,
6267                                      GLsizei depth, GLenum format,
6268                                      GLsizei imageSize, const GLvoid *data)
6269 {
6270    compressed_tex_sub_image(3, target, texture, level, xoffset, yoffset,
6271                             zoffset, width, height, depth, format,
6272                             imageSize, data,
6273                             TEX_MODE_EXT_DSA_TEXTURE,
6274                             "glCompressedTextureSubImage3DEXT");
6275 }
6276 
6277 
6278 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)6279 _mesa_CompressedMultiTexSubImage3DEXT(GLenum texunit, GLenum target,
6280                                       GLint level, GLint xoffset, GLint yoffset,
6281                                       GLint zoffset, GLsizei width, GLsizei height,
6282                                       GLsizei depth, GLenum format,
6283                                       GLsizei imageSize, const GLvoid *data)
6284 {
6285    compressed_tex_sub_image(3, target, texunit - GL_TEXTURE0, level,
6286                             xoffset, yoffset, zoffset, width, height, depth,
6287                             format, imageSize, data,
6288                             TEX_MODE_EXT_DSA_TEXUNIT,
6289                             "glCompressedMultiTexSubImage3DEXT");
6290 }
6291 
6292 
6293 mesa_format
_mesa_get_texbuffer_format(const struct gl_context * ctx,GLenum internalFormat)6294 _mesa_get_texbuffer_format(const struct gl_context *ctx, GLenum internalFormat)
6295 {
6296    if (_mesa_is_desktop_gl_compat(ctx)) {
6297       switch (internalFormat) {
6298       case GL_ALPHA8:
6299          return MESA_FORMAT_A_UNORM8;
6300       case GL_ALPHA16:
6301          return MESA_FORMAT_A_UNORM16;
6302       case GL_ALPHA16F_ARB:
6303          return MESA_FORMAT_A_FLOAT16;
6304       case GL_ALPHA32F_ARB:
6305          return MESA_FORMAT_A_FLOAT32;
6306       case GL_ALPHA8I_EXT:
6307          return MESA_FORMAT_A_SINT8;
6308       case GL_ALPHA16I_EXT:
6309          return MESA_FORMAT_A_SINT16;
6310       case GL_ALPHA32I_EXT:
6311          return MESA_FORMAT_A_SINT32;
6312       case GL_ALPHA8UI_EXT:
6313          return MESA_FORMAT_A_UINT8;
6314       case GL_ALPHA16UI_EXT:
6315          return MESA_FORMAT_A_UINT16;
6316       case GL_ALPHA32UI_EXT:
6317          return MESA_FORMAT_A_UINT32;
6318       case GL_LUMINANCE8:
6319          return MESA_FORMAT_L_UNORM8;
6320       case GL_LUMINANCE16:
6321          return MESA_FORMAT_L_UNORM16;
6322       case GL_LUMINANCE16F_ARB:
6323          return MESA_FORMAT_L_FLOAT16;
6324       case GL_LUMINANCE32F_ARB:
6325          return MESA_FORMAT_L_FLOAT32;
6326       case GL_LUMINANCE8I_EXT:
6327          return MESA_FORMAT_L_SINT8;
6328       case GL_LUMINANCE16I_EXT:
6329          return MESA_FORMAT_L_SINT16;
6330       case GL_LUMINANCE32I_EXT:
6331          return MESA_FORMAT_L_SINT32;
6332       case GL_LUMINANCE8UI_EXT:
6333          return MESA_FORMAT_L_UINT8;
6334       case GL_LUMINANCE16UI_EXT:
6335          return MESA_FORMAT_L_UINT16;
6336       case GL_LUMINANCE32UI_EXT:
6337          return MESA_FORMAT_L_UINT32;
6338       case GL_LUMINANCE8_ALPHA8:
6339          return MESA_FORMAT_LA_UNORM8;
6340       case GL_LUMINANCE16_ALPHA16:
6341          return MESA_FORMAT_LA_UNORM16;
6342       case GL_LUMINANCE_ALPHA16F_ARB:
6343          return MESA_FORMAT_LA_FLOAT16;
6344       case GL_LUMINANCE_ALPHA32F_ARB:
6345          return MESA_FORMAT_LA_FLOAT32;
6346       case GL_LUMINANCE_ALPHA8I_EXT:
6347          return MESA_FORMAT_LA_SINT8;
6348       case GL_LUMINANCE_ALPHA16I_EXT:
6349          return MESA_FORMAT_LA_SINT16;
6350       case GL_LUMINANCE_ALPHA32I_EXT:
6351          return MESA_FORMAT_LA_SINT32;
6352       case GL_LUMINANCE_ALPHA8UI_EXT:
6353          return MESA_FORMAT_LA_UINT8;
6354       case GL_LUMINANCE_ALPHA16UI_EXT:
6355          return MESA_FORMAT_LA_UINT16;
6356       case GL_LUMINANCE_ALPHA32UI_EXT:
6357          return MESA_FORMAT_LA_UINT32;
6358       case GL_INTENSITY8:
6359          return MESA_FORMAT_I_UNORM8;
6360       case GL_INTENSITY16:
6361          return MESA_FORMAT_I_UNORM16;
6362       case GL_INTENSITY16F_ARB:
6363          return MESA_FORMAT_I_FLOAT16;
6364       case GL_INTENSITY32F_ARB:
6365          return MESA_FORMAT_I_FLOAT32;
6366       case GL_INTENSITY8I_EXT:
6367          return MESA_FORMAT_I_SINT8;
6368       case GL_INTENSITY16I_EXT:
6369          return MESA_FORMAT_I_SINT16;
6370       case GL_INTENSITY32I_EXT:
6371          return MESA_FORMAT_I_SINT32;
6372       case GL_INTENSITY8UI_EXT:
6373          return MESA_FORMAT_I_UINT8;
6374       case GL_INTENSITY16UI_EXT:
6375          return MESA_FORMAT_I_UINT16;
6376       case GL_INTENSITY32UI_EXT:
6377          return MESA_FORMAT_I_UINT32;
6378       default:
6379          break;
6380       }
6381    }
6382 
6383    if (_mesa_has_ARB_texture_buffer_object_rgb32(ctx) ||
6384        _mesa_has_OES_texture_buffer(ctx)) {
6385       switch (internalFormat) {
6386       case GL_RGB32F:
6387          return MESA_FORMAT_RGB_FLOAT32;
6388       case GL_RGB32UI:
6389          return MESA_FORMAT_RGB_UINT32;
6390       case GL_RGB32I:
6391          return MESA_FORMAT_RGB_SINT32;
6392       default:
6393          break;
6394       }
6395    }
6396 
6397    switch (internalFormat) {
6398    case GL_RGBA8:
6399       return MESA_FORMAT_R8G8B8A8_UNORM;
6400    case GL_RGBA16:
6401       if (_mesa_is_gles(ctx) && !_mesa_has_EXT_texture_norm16(ctx))
6402          return MESA_FORMAT_NONE;
6403       return MESA_FORMAT_RGBA_UNORM16;
6404    case GL_RGBA16F_ARB:
6405       return MESA_FORMAT_RGBA_FLOAT16;
6406    case GL_RGBA32F_ARB:
6407       return MESA_FORMAT_RGBA_FLOAT32;
6408    case GL_RGBA8I_EXT:
6409       return MESA_FORMAT_RGBA_SINT8;
6410    case GL_RGBA16I_EXT:
6411       return MESA_FORMAT_RGBA_SINT16;
6412    case GL_RGBA32I_EXT:
6413       return MESA_FORMAT_RGBA_SINT32;
6414    case GL_RGBA8UI_EXT:
6415       return MESA_FORMAT_RGBA_UINT8;
6416    case GL_RGBA16UI_EXT:
6417       return MESA_FORMAT_RGBA_UINT16;
6418    case GL_RGBA32UI_EXT:
6419       return MESA_FORMAT_RGBA_UINT32;
6420 
6421    case GL_RG8:
6422       return MESA_FORMAT_RG_UNORM8;
6423    case GL_RG16:
6424       if (_mesa_is_gles(ctx) && !_mesa_has_EXT_texture_norm16(ctx))
6425          return MESA_FORMAT_NONE;
6426       return MESA_FORMAT_RG_UNORM16;
6427    case GL_RG16F:
6428       return MESA_FORMAT_RG_FLOAT16;
6429    case GL_RG32F:
6430       return MESA_FORMAT_RG_FLOAT32;
6431    case GL_RG8I:
6432       return MESA_FORMAT_RG_SINT8;
6433    case GL_RG16I:
6434       return MESA_FORMAT_RG_SINT16;
6435    case GL_RG32I:
6436       return MESA_FORMAT_RG_SINT32;
6437    case GL_RG8UI:
6438       return MESA_FORMAT_RG_UINT8;
6439    case GL_RG16UI:
6440       return MESA_FORMAT_RG_UINT16;
6441    case GL_RG32UI:
6442       return MESA_FORMAT_RG_UINT32;
6443 
6444    case GL_R8:
6445       return MESA_FORMAT_R_UNORM8;
6446    case GL_R16:
6447       if (_mesa_is_gles(ctx) && !_mesa_has_EXT_texture_norm16(ctx))
6448          return MESA_FORMAT_NONE;
6449       return MESA_FORMAT_R_UNORM16;
6450    case GL_R16F:
6451       return MESA_FORMAT_R_FLOAT16;
6452    case GL_R32F:
6453       return MESA_FORMAT_R_FLOAT32;
6454    case GL_R8I:
6455       return MESA_FORMAT_R_SINT8;
6456    case GL_R16I:
6457       return MESA_FORMAT_R_SINT16;
6458    case GL_R32I:
6459       return MESA_FORMAT_R_SINT32;
6460    case GL_R8UI:
6461       return MESA_FORMAT_R_UINT8;
6462    case GL_R16UI:
6463       return MESA_FORMAT_R_UINT16;
6464    case GL_R32UI:
6465       return MESA_FORMAT_R_UINT32;
6466 
6467    default:
6468       return MESA_FORMAT_NONE;
6469    }
6470 }
6471 
6472 
6473 mesa_format
_mesa_validate_texbuffer_format(const struct gl_context * ctx,GLenum internalFormat)6474 _mesa_validate_texbuffer_format(const struct gl_context *ctx,
6475                                 GLenum internalFormat)
6476 {
6477    mesa_format format = _mesa_get_texbuffer_format(ctx, internalFormat);
6478    GLenum datatype;
6479 
6480    if (format == MESA_FORMAT_NONE)
6481       return MESA_FORMAT_NONE;
6482 
6483    datatype = _mesa_get_format_datatype(format);
6484 
6485    /* The GL_ARB_texture_buffer_object spec says:
6486     *
6487     *     "If ARB_texture_float is not supported, references to the
6488     *     floating-point internal formats provided by that extension should be
6489     *     removed, and such formats may not be passed to TexBufferARB."
6490     *
6491     * As a result, GL_HALF_FLOAT internal format depends on both
6492     * GL_ARB_texture_float and GL_ARB_half_float_pixel.
6493     */
6494    if ((datatype == GL_FLOAT || datatype == GL_HALF_FLOAT) &&
6495        !ctx->Extensions.ARB_texture_float)
6496       return MESA_FORMAT_NONE;
6497 
6498    if (!ctx->Extensions.ARB_texture_rg) {
6499       GLenum base_format = _mesa_get_format_base_format(format);
6500       if (base_format == GL_R || base_format == GL_RG)
6501          return MESA_FORMAT_NONE;
6502    }
6503 
6504    if (!ctx->Extensions.ARB_texture_buffer_object_rgb32) {
6505       GLenum base_format = _mesa_get_format_base_format(format);
6506       if (base_format == GL_RGB)
6507          return MESA_FORMAT_NONE;
6508    }
6509    return format;
6510 }
6511 
6512 
6513 /**
6514  * Do work common to glTexBuffer, glTexBufferRange, glTextureBuffer
6515  * and glTextureBufferRange, including some error checking.
6516  */
6517 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)6518 texture_buffer_range(struct gl_context *ctx,
6519                      struct gl_texture_object *texObj,
6520                      GLenum internalFormat,
6521                      struct gl_buffer_object *bufObj,
6522                      GLintptr offset, GLsizeiptr size,
6523                      const char *caller)
6524 {
6525    GLintptr oldOffset = texObj->BufferOffset;
6526    GLsizeiptr oldSize = texObj->BufferSize;
6527    mesa_format format;
6528    mesa_format old_format;
6529 
6530    /* NOTE: ARB_texture_buffer_object might not be supported in
6531     * the compatibility profile.
6532     */
6533    if (!_mesa_has_ARB_texture_buffer_object(ctx) &&
6534        !_mesa_has_OES_texture_buffer(ctx)) {
6535       _mesa_error(ctx, GL_INVALID_OPERATION,
6536                   "%s(ARB_texture_buffer_object is not"
6537                   " implemented for the compatibility profile)", caller);
6538       return;
6539    }
6540 
6541    if (texObj->HandleAllocated) {
6542       /* The ARB_bindless_texture spec says:
6543        *
6544        * "The error INVALID_OPERATION is generated by TexImage*, CopyTexImage*,
6545        *  CompressedTexImage*, TexBuffer*, TexParameter*, as well as other
6546        *  functions defined in terms of these, if the texture object to be
6547        *  modified is referenced by one or more texture or image handles."
6548        */
6549       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(immutable texture)", caller);
6550       return;
6551    }
6552 
6553    format = _mesa_validate_texbuffer_format(ctx, internalFormat);
6554    if (format == MESA_FORMAT_NONE) {
6555       _mesa_error(ctx, GL_INVALID_ENUM, "%s(internalFormat %s)",
6556                   caller, _mesa_enum_to_string(internalFormat));
6557       return;
6558    }
6559 
6560    FLUSH_VERTICES(ctx, 0, GL_TEXTURE_BIT);
6561 
6562    _mesa_lock_texture(ctx, texObj);
6563    {
6564       _mesa_reference_buffer_object_shared(ctx, &texObj->BufferObject, bufObj);
6565       texObj->BufferObjectFormat = internalFormat;
6566       old_format = texObj->_BufferObjectFormat;
6567       texObj->_BufferObjectFormat = format;
6568       texObj->BufferOffset = offset;
6569       texObj->BufferSize = size;
6570    }
6571    _mesa_unlock_texture(ctx, texObj);
6572 
6573    if (old_format != format) {
6574       st_texture_release_all_sampler_views(st_context(ctx), texObj);
6575    } else {
6576       if (offset != oldOffset) {
6577          st_texture_release_all_sampler_views(st_context(ctx), texObj);
6578       }
6579       if (size != oldSize) {
6580          st_texture_release_all_sampler_views(st_context(ctx), texObj);
6581       }
6582    }
6583 
6584    ctx->NewDriverState |= ST_NEW_SAMPLER_VIEWS;
6585 
6586    if (bufObj) {
6587       bufObj->UsageHistory |= USAGE_TEXTURE_BUFFER;
6588    }
6589 }
6590 
6591 
6592 /**
6593  * Make sure the texture buffer target is GL_TEXTURE_BUFFER.
6594  * Return true if it is, and return false if it is not
6595  * (and throw INVALID ENUM as dictated in the OpenGL 4.5
6596  * core spec, 02.02.2015, PDF page 245).
6597  */
6598 static bool
check_texture_buffer_target(struct gl_context * ctx,GLenum target,const char * caller,bool dsa)6599 check_texture_buffer_target(struct gl_context *ctx, GLenum target,
6600                             const char *caller, bool dsa)
6601 {
6602    if (target != GL_TEXTURE_BUFFER_ARB) {
6603       _mesa_error(ctx, dsa ? GL_INVALID_OPERATION : GL_INVALID_ENUM,
6604                   "%s(texture target is not GL_TEXTURE_BUFFER)", caller);
6605       return false;
6606    }
6607    else
6608       return true;
6609 }
6610 
6611 /**
6612  * Check for errors related to the texture buffer range.
6613  * Return false if errors are found, true if none are found.
6614  */
6615 static bool
check_texture_buffer_range(struct gl_context * ctx,struct gl_buffer_object * bufObj,GLintptr offset,GLsizeiptr size,const char * caller)6616 check_texture_buffer_range(struct gl_context *ctx,
6617                            struct gl_buffer_object *bufObj,
6618                            GLintptr offset, GLsizeiptr size,
6619                            const char *caller)
6620 {
6621    /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
6622     * Textures (PDF page 245):
6623     *    "An INVALID_VALUE error is generated if offset is negative, if
6624     *    size is less than or equal to zero, or if offset + size is greater
6625     *    than the value of BUFFER_SIZE for the buffer bound to target."
6626     */
6627    if (offset < 0) {
6628       _mesa_error(ctx, GL_INVALID_VALUE, "%s(offset=%d < 0)", caller,
6629                   (int) offset);
6630       return false;
6631    }
6632 
6633    if (size <= 0) {
6634       _mesa_error(ctx, GL_INVALID_VALUE, "%s(size=%d <= 0)", caller,
6635                   (int) size);
6636       return false;
6637    }
6638 
6639    if (offset + size > bufObj->Size) {
6640       _mesa_error(ctx, GL_INVALID_VALUE,
6641                   "%s(offset=%d + size=%d > buffer_size=%d)", caller,
6642                   (int) offset, (int) size, (int) bufObj->Size);
6643       return false;
6644    }
6645 
6646    /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
6647     * Textures (PDF page 245):
6648     *    "An INVALID_VALUE error is generated if offset is not an integer
6649     *    multiple of the value of TEXTURE_BUFFER_OFFSET_ALIGNMENT."
6650     */
6651    if (offset % ctx->Const.TextureBufferOffsetAlignment) {
6652       _mesa_error(ctx, GL_INVALID_VALUE,
6653                   "%s(invalid offset alignment)", caller);
6654       return false;
6655    }
6656 
6657    return true;
6658 }
6659 
6660 
6661 /** GL_ARB_texture_buffer_object */
6662 void GLAPIENTRY
_mesa_TexBuffer(GLenum target,GLenum internalFormat,GLuint buffer)6663 _mesa_TexBuffer(GLenum target, GLenum internalFormat, GLuint buffer)
6664 {
6665    struct gl_texture_object *texObj;
6666    struct gl_buffer_object *bufObj;
6667 
6668    GET_CURRENT_CONTEXT(ctx);
6669 
6670    /* Need to catch a bad target before it gets to
6671     * _mesa_get_current_tex_object.
6672     */
6673    if (!check_texture_buffer_target(ctx, target, "glTexBuffer", false))
6674       return;
6675 
6676    if (buffer) {
6677       bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glTexBuffer");
6678       if (!bufObj)
6679          return;
6680    } else
6681       bufObj = NULL;
6682 
6683    texObj = _mesa_get_current_tex_object(ctx, target);
6684    if (!texObj)
6685       return;
6686 
6687    texture_buffer_range(ctx, texObj, internalFormat, bufObj, 0,
6688                         buffer ? -1 : 0, "glTexBuffer");
6689 }
6690 
6691 
6692 /** GL_ARB_texture_buffer_range */
6693 void GLAPIENTRY
_mesa_TexBufferRange(GLenum target,GLenum internalFormat,GLuint buffer,GLintptr offset,GLsizeiptr size)6694 _mesa_TexBufferRange(GLenum target, GLenum internalFormat, GLuint buffer,
6695                      GLintptr offset, GLsizeiptr size)
6696 {
6697    struct gl_texture_object *texObj;
6698    struct gl_buffer_object *bufObj;
6699 
6700    GET_CURRENT_CONTEXT(ctx);
6701 
6702    /* Need to catch a bad target before it gets to
6703     * _mesa_get_current_tex_object.
6704     */
6705    if (!check_texture_buffer_target(ctx, target, "glTexBufferRange", false))
6706       return;
6707 
6708    if (buffer) {
6709       bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glTexBufferRange");
6710       if (!bufObj)
6711          return;
6712 
6713       if (!check_texture_buffer_range(ctx, bufObj, offset, size,
6714           "glTexBufferRange"))
6715          return;
6716 
6717    } else {
6718       /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
6719        * Textures (PDF page 254):
6720        *    "If buffer is zero, then any buffer object attached to the buffer
6721        *    texture is detached, the values offset and size are ignored and
6722        *    the state for offset and size for the buffer texture are reset to
6723        *    zero."
6724        */
6725       offset = 0;
6726       size = 0;
6727       bufObj = NULL;
6728    }
6729 
6730    texObj = _mesa_get_current_tex_object(ctx, target);
6731    if (!texObj)
6732       return;
6733 
6734    texture_buffer_range(ctx, texObj, internalFormat, bufObj,
6735                         offset, size, "glTexBufferRange");
6736 }
6737 
6738 
6739 /** GL_ARB_texture_buffer_range + GL_EXT_direct_state_access */
6740 void GLAPIENTRY
_mesa_TextureBufferRangeEXT(GLuint texture,GLenum target,GLenum internalFormat,GLuint buffer,GLintptr offset,GLsizeiptr size)6741 _mesa_TextureBufferRangeEXT(GLuint texture, GLenum target, GLenum internalFormat,
6742                             GLuint buffer, GLintptr offset, GLsizeiptr size)
6743 {
6744    struct gl_texture_object *texObj;
6745    struct gl_buffer_object *bufObj;
6746 
6747    GET_CURRENT_CONTEXT(ctx);
6748 
6749    texObj = _mesa_lookup_or_create_texture(ctx, target, texture, false, true,
6750                                            "glTextureBufferRangeEXT");
6751    if (!texObj)
6752       return;
6753 
6754    if (!check_texture_buffer_target(ctx, target, "glTextureBufferRangeEXT", true))
6755       return;
6756 
6757    if (buffer) {
6758       bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glTextureBufferRangeEXT");
6759       if (!bufObj)
6760          return;
6761 
6762       if (!check_texture_buffer_range(ctx, bufObj, offset, size,
6763           "glTextureBufferRangeEXT"))
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    texture_buffer_range(ctx, texObj, internalFormat, bufObj,
6780                         offset, size, "glTextureBufferRangeEXT");
6781 }
6782 
6783 
6784 void GLAPIENTRY
_mesa_TextureBuffer(GLuint texture,GLenum internalFormat,GLuint buffer)6785 _mesa_TextureBuffer(GLuint texture, GLenum internalFormat, GLuint buffer)
6786 {
6787    struct gl_texture_object *texObj;
6788    struct gl_buffer_object *bufObj;
6789 
6790    GET_CURRENT_CONTEXT(ctx);
6791 
6792    if (buffer) {
6793       bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glTextureBuffer");
6794       if (!bufObj)
6795          return;
6796    } else
6797       bufObj = NULL;
6798 
6799    /* Get the texture object by Name. */
6800    texObj = _mesa_lookup_texture_err(ctx, texture, "glTextureBuffer");
6801    if (!texObj)
6802       return;
6803 
6804    if (!check_texture_buffer_target(ctx, texObj->Target, "glTextureBuffer", true))
6805       return;
6806 
6807    texture_buffer_range(ctx, texObj, internalFormat,
6808                         bufObj, 0, buffer ? -1 : 0, "glTextureBuffer");
6809 }
6810 
6811 void GLAPIENTRY
_mesa_TextureBufferEXT(GLuint texture,GLenum target,GLenum internalFormat,GLuint buffer)6812 _mesa_TextureBufferEXT(GLuint texture, GLenum target,
6813                        GLenum internalFormat, GLuint buffer)
6814 {
6815    struct gl_texture_object *texObj;
6816    struct gl_buffer_object *bufObj;
6817 
6818    GET_CURRENT_CONTEXT(ctx);
6819 
6820    if (buffer) {
6821       bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glTextureBuffer");
6822       if (!bufObj)
6823          return;
6824    } else
6825       bufObj = NULL;
6826 
6827    /* Get the texture object by Name. */
6828    texObj = _mesa_lookup_or_create_texture(ctx, target, texture,
6829                                            false, true,
6830                                            "glTextureBufferEXT");
6831 
6832    if (!texObj ||
6833        !check_texture_buffer_target(ctx, texObj->Target, "glTextureBufferEXT", true))
6834       return;
6835 
6836    texture_buffer_range(ctx, texObj, internalFormat,
6837                         bufObj, 0, buffer ? -1 : 0, "glTextureBufferEXT");
6838 }
6839 
6840 void GLAPIENTRY
_mesa_MultiTexBufferEXT(GLenum texunit,GLenum target,GLenum internalFormat,GLuint buffer)6841 _mesa_MultiTexBufferEXT(GLenum texunit, GLenum target,
6842                         GLenum internalFormat, GLuint buffer)
6843 {
6844    struct gl_texture_object *texObj;
6845    struct gl_buffer_object *bufObj;
6846 
6847    GET_CURRENT_CONTEXT(ctx);
6848 
6849    if (buffer) {
6850       bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glMultiTexBufferEXT");
6851       if (!bufObj)
6852          return;
6853    } else
6854       bufObj = NULL;
6855 
6856    /* Get the texture object */
6857    texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
6858                                                    texunit - GL_TEXTURE0,
6859                                                    true,
6860                                                    "glMultiTexBufferEXT");
6861 
6862    if (!texObj ||
6863        !check_texture_buffer_target(ctx, texObj->Target, "glMultiTexBufferEXT", false))
6864       return;
6865 
6866    texture_buffer_range(ctx, texObj, internalFormat,
6867                         bufObj, 0, buffer ? -1 : 0, "glMultiTexBufferEXT");
6868 }
6869 
6870 void GLAPIENTRY
_mesa_TextureBufferRange(GLuint texture,GLenum internalFormat,GLuint buffer,GLintptr offset,GLsizeiptr size)6871 _mesa_TextureBufferRange(GLuint texture, GLenum internalFormat, GLuint buffer,
6872                          GLintptr offset, GLsizeiptr size)
6873 {
6874    struct gl_texture_object *texObj;
6875    struct gl_buffer_object *bufObj;
6876 
6877    GET_CURRENT_CONTEXT(ctx);
6878 
6879    if (buffer) {
6880       bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
6881                                           "glTextureBufferRange");
6882       if (!bufObj)
6883          return;
6884 
6885       if (!check_texture_buffer_range(ctx, bufObj, offset, size,
6886           "glTextureBufferRange"))
6887          return;
6888 
6889    } else {
6890       /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
6891        * Textures (PDF page 254):
6892        *    "If buffer is zero, then any buffer object attached to the buffer
6893        *    texture is detached, the values offset and size are ignored and
6894        *    the state for offset and size for the buffer texture are reset to
6895        *    zero."
6896        */
6897       offset = 0;
6898       size = 0;
6899       bufObj = NULL;
6900    }
6901 
6902    /* Get the texture object by Name. */
6903    texObj = _mesa_lookup_texture_err(ctx, texture, "glTextureBufferRange");
6904    if (!texObj)
6905       return;
6906 
6907    if (!check_texture_buffer_target(ctx, texObj->Target,
6908        "glTextureBufferRange", true))
6909       return;
6910 
6911    texture_buffer_range(ctx, texObj, internalFormat,
6912                         bufObj, offset, size, "glTextureBufferRange");
6913 }
6914 
6915 GLboolean
_mesa_is_renderable_texture_format(const struct gl_context * ctx,GLenum internalformat)6916 _mesa_is_renderable_texture_format(const struct gl_context *ctx,
6917                                    GLenum internalformat)
6918 {
6919    /* Everything that is allowed for renderbuffers,
6920     * except for a base format of GL_STENCIL_INDEX, unless supported.
6921     */
6922    GLenum baseFormat = _mesa_base_fbo_format(ctx, internalformat);
6923    if (ctx->Extensions.ARB_texture_stencil8)
6924       return baseFormat != 0;
6925    else
6926       return baseFormat != 0 && baseFormat != GL_STENCIL_INDEX;
6927 }
6928 
6929 
6930 /** GL_ARB_texture_multisample */
6931 static GLboolean
check_multisample_target(GLuint dims,GLenum target,bool dsa)6932 check_multisample_target(GLuint dims, GLenum target, bool dsa)
6933 {
6934    switch(target) {
6935    case GL_TEXTURE_2D_MULTISAMPLE:
6936       return dims == 2;
6937    case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
6938       return dims == 2 && !dsa;
6939    case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
6940       return dims == 3;
6941    case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
6942       return dims == 3 && !dsa;
6943    default:
6944       return GL_FALSE;
6945    }
6946 }
6947 
6948 
6949 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)6950 texture_image_multisample(struct gl_context *ctx, GLuint dims,
6951                           struct gl_texture_object *texObj,
6952                           struct gl_memory_object *memObj,
6953                           GLenum target, GLsizei samples,
6954                           GLint internalformat, GLsizei width,
6955                           GLsizei height, GLsizei depth,
6956                           GLboolean fixedsamplelocations,
6957                           GLboolean immutable, GLuint64 offset,
6958                           const char *func)
6959 {
6960    struct gl_texture_image *texImage;
6961    GLboolean sizeOK, dimensionsOK, samplesOK;
6962    mesa_format texFormat;
6963    GLenum sample_count_error;
6964    bool dsa = strstr(func, "ture") ? true : false;
6965 
6966    if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE)) {
6967       _mesa_debug(ctx, "%s(target=%s, samples=%d, internalformat=%s)\n", func,
6968                   _mesa_enum_to_string(target), samples, _mesa_enum_to_string(internalformat));
6969    }
6970 
6971    if (!((ctx->Extensions.ARB_texture_multisample
6972          && _mesa_is_desktop_gl(ctx))) && !_mesa_is_gles31(ctx)) {
6973       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(unsupported)", func);
6974       return;
6975    }
6976 
6977    if (samples < 1) {
6978       _mesa_error(ctx, GL_INVALID_VALUE, "%s(samples < 1)", func);
6979       return;
6980    }
6981 
6982    if (!check_multisample_target(dims, target, dsa)) {
6983       GLenum err = dsa ? GL_INVALID_OPERATION : GL_INVALID_ENUM;
6984       _mesa_error(ctx, err, "%s(target=%s)", func,
6985                   _mesa_enum_to_string(target));
6986       return;
6987    }
6988 
6989    /* check that the specified internalformat is color/depth/stencil-renderable;
6990     * refer GL3.1 spec 4.4.4
6991     */
6992 
6993    if (immutable && !_mesa_is_legal_tex_storage_format(ctx, internalformat)) {
6994       _mesa_error(ctx, GL_INVALID_ENUM,
6995             "%s(internalformat=%s not legal for immutable-format)",
6996             func, _mesa_enum_to_string(internalformat));
6997       return;
6998    }
6999 
7000    if (!_mesa_is_renderable_texture_format(ctx, internalformat)) {
7001       /* Page 172 of OpenGL ES 3.1 spec says:
7002        *   "An INVALID_ENUM error is generated if sizedinternalformat is not
7003        *   color-renderable, depth-renderable, or stencil-renderable (as
7004        *   defined in section 9.4).
7005        *
7006        *  (Same error is also defined for desktop OpenGL for multisample
7007        *  teximage/texstorage functions.)
7008        */
7009       _mesa_error(ctx, GL_INVALID_ENUM, "%s(internalformat=%s)", func,
7010                   _mesa_enum_to_string(internalformat));
7011       return;
7012    }
7013 
7014    sample_count_error = _mesa_check_sample_count(ctx, target,
7015          internalformat, samples, samples);
7016    samplesOK = sample_count_error == GL_NO_ERROR;
7017 
7018    /* Page 254 of OpenGL 4.4 spec says:
7019     *   "Proxy arrays for two-dimensional multisample and two-dimensional
7020     *    multisample array textures are operated on in the same way when
7021     *    TexImage2DMultisample is called with target specified as
7022     *    PROXY_TEXTURE_2D_MULTISAMPLE, or TexImage3DMultisample is called
7023     *    with target specified as PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY.
7024     *    However, if samples is not supported, then no error is generated.
7025     */
7026    if (!samplesOK && !_mesa_is_proxy_texture(target)) {
7027       _mesa_error(ctx, sample_count_error, "%s(samples=%d)", func, samples);
7028       return;
7029    }
7030 
7031    if (!texObj) {
7032       texObj = _mesa_get_current_tex_object(ctx, target);
7033       if (!texObj)
7034          return;
7035    }
7036 
7037    if (immutable && texObj->Name == 0) {
7038       _mesa_error(ctx, GL_INVALID_OPERATION,
7039             "%s(texture object 0)",
7040             func);
7041       return;
7042    }
7043 
7044    texImage = _mesa_get_tex_image(ctx, texObj, 0, 0);
7045 
7046    if (texImage == NULL) {
7047       _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s()", func);
7048       return;
7049    }
7050 
7051    texFormat = _mesa_choose_texture_format(ctx, texObj, target, 0,
7052          internalformat, GL_NONE, GL_NONE);
7053    assert(texFormat != MESA_FORMAT_NONE);
7054 
7055    dimensionsOK = _mesa_legal_texture_dimensions(ctx, target, 0,
7056          width, height, depth, 0);
7057 
7058    sizeOK = st_TestProxyTexImage(ctx, target, 0, 0, texFormat,
7059                                  samples, width, height, depth);
7060 
7061    if (_mesa_is_proxy_texture(target)) {
7062       if (samplesOK && dimensionsOK && sizeOK) {
7063          _mesa_init_teximage_fields_ms(ctx, texImage, width, height, depth, 0,
7064                                        internalformat, texFormat,
7065                                        samples, fixedsamplelocations);
7066       }
7067       else {
7068          /* clear all image fields */
7069          clear_teximage_fields(texImage);
7070       }
7071    }
7072    else {
7073       if (!dimensionsOK) {
7074          _mesa_error(ctx, GL_INVALID_VALUE,
7075                      "%s(invalid width=%d or height=%d)", func, width, height);
7076          return;
7077       }
7078 
7079       if (!sizeOK) {
7080          _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s(texture too large)", func);
7081          return;
7082       }
7083 
7084       /* Check if texObj->Immutable is set */
7085       if (texObj->Immutable) {
7086          _mesa_error(ctx, GL_INVALID_OPERATION, "%s(immutable)", func);
7087          return;
7088       }
7089 
7090       if (texObj->IsSparse &&
7091           _mesa_sparse_texture_error_check(ctx, dims, texObj, texFormat, target, 0,
7092                                            width, height, depth, func))
7093          return; /* error was recorded */
7094 
7095       st_FreeTextureImageBuffer(ctx, texImage);
7096 
7097       _mesa_init_teximage_fields_ms(ctx, texImage, width, height, depth, 0,
7098                                     internalformat, texFormat,
7099                                     samples, fixedsamplelocations);
7100 
7101       if (width > 0 && height > 0 && depth > 0) {
7102          if (memObj) {
7103             if (!st_SetTextureStorageForMemoryObject(ctx, texObj,
7104                                                      memObj, 1, width,
7105                                                      height, depth,
7106                                                      offset, func)) {
7107 
7108                _mesa_init_teximage_fields(ctx, texImage, 0, 0, 0, 0,
7109                                           internalformat, texFormat);
7110             }
7111          } else {
7112             if (!st_AllocTextureStorage(ctx, texObj, 1,
7113                                         width, height, depth, func)) {
7114                /* tidy up the texture image state. strictly speaking,
7115                 * we're allowed to just leave this in whatever state we
7116                 * like, but being tidy is good.
7117                 */
7118                _mesa_init_teximage_fields(ctx, texImage, 0, 0, 0, 0,
7119                                           internalformat, texFormat);
7120             }
7121          }
7122       }
7123 
7124       texObj->External = GL_FALSE;
7125       texObj->Immutable |= immutable;
7126 
7127       if (immutable) {
7128          _mesa_set_texture_view_state(ctx, texObj, target, 1);
7129       }
7130 
7131       _mesa_update_fbo_texture(ctx, texObj, 0, 0);
7132    }
7133    _mesa_update_texture_object_swizzle(ctx, texObj);
7134 }
7135 
7136 
7137 void GLAPIENTRY
_mesa_TexImage2DMultisample(GLenum target,GLsizei samples,GLenum internalformat,GLsizei width,GLsizei height,GLboolean fixedsamplelocations)7138 _mesa_TexImage2DMultisample(GLenum target, GLsizei samples,
7139                             GLenum internalformat, GLsizei width,
7140                             GLsizei height, GLboolean fixedsamplelocations)
7141 {
7142    GET_CURRENT_CONTEXT(ctx);
7143 
7144    texture_image_multisample(ctx, 2, NULL, NULL, target, samples,
7145                              internalformat, width, height, 1,
7146                              fixedsamplelocations, GL_FALSE, 0,
7147                              "glTexImage2DMultisample");
7148 }
7149 
7150 
7151 void GLAPIENTRY
_mesa_TexImage3DMultisample(GLenum target,GLsizei samples,GLenum internalformat,GLsizei width,GLsizei height,GLsizei depth,GLboolean fixedsamplelocations)7152 _mesa_TexImage3DMultisample(GLenum target, GLsizei samples,
7153                             GLenum internalformat, GLsizei width,
7154                             GLsizei height, GLsizei depth,
7155                             GLboolean fixedsamplelocations)
7156 {
7157    GET_CURRENT_CONTEXT(ctx);
7158 
7159    texture_image_multisample(ctx, 3, NULL, NULL, target, samples,
7160                              internalformat, width, height, depth,
7161                              fixedsamplelocations, GL_FALSE, 0,
7162                              "glTexImage3DMultisample");
7163 }
7164 
7165 static bool
valid_texstorage_ms_parameters(GLsizei width,GLsizei height,GLsizei depth,unsigned dims)7166 valid_texstorage_ms_parameters(GLsizei width, GLsizei height, GLsizei depth,
7167                                unsigned dims)
7168 {
7169    GET_CURRENT_CONTEXT(ctx);
7170 
7171    if (!_mesa_valid_tex_storage_dim(width, height, depth)) {
7172       _mesa_error(ctx, GL_INVALID_VALUE,
7173                   "glTexStorage%uDMultisample(width=%d,height=%d,depth=%d)",
7174                   dims, width, height, depth);
7175       return false;
7176    }
7177    return true;
7178 }
7179 
7180 void GLAPIENTRY
_mesa_TexStorage2DMultisample(GLenum target,GLsizei samples,GLenum internalformat,GLsizei width,GLsizei height,GLboolean fixedsamplelocations)7181 _mesa_TexStorage2DMultisample(GLenum target, GLsizei samples,
7182                               GLenum internalformat, GLsizei width,
7183                               GLsizei height, GLboolean fixedsamplelocations)
7184 {
7185    GET_CURRENT_CONTEXT(ctx);
7186 
7187    if (!valid_texstorage_ms_parameters(width, height, 1, 2))
7188       return;
7189 
7190    texture_image_multisample(ctx, 2, NULL, NULL, target, samples,
7191                              internalformat, width, height, 1,
7192                              fixedsamplelocations, GL_TRUE, 0,
7193                              "glTexStorage2DMultisample");
7194 }
7195 
7196 void GLAPIENTRY
_mesa_TexStorage3DMultisample(GLenum target,GLsizei samples,GLenum internalformat,GLsizei width,GLsizei height,GLsizei depth,GLboolean fixedsamplelocations)7197 _mesa_TexStorage3DMultisample(GLenum target, GLsizei samples,
7198                               GLenum internalformat, GLsizei width,
7199                               GLsizei height, GLsizei depth,
7200                               GLboolean fixedsamplelocations)
7201 {
7202    GET_CURRENT_CONTEXT(ctx);
7203 
7204    if (!valid_texstorage_ms_parameters(width, height, depth, 3))
7205       return;
7206 
7207    texture_image_multisample(ctx, 3, NULL, NULL, target, samples,
7208                              internalformat, width, height, depth,
7209                              fixedsamplelocations, GL_TRUE, 0,
7210                              "glTexStorage3DMultisample");
7211 }
7212 
7213 void GLAPIENTRY
_mesa_TextureStorage2DMultisample(GLuint texture,GLsizei samples,GLenum internalformat,GLsizei width,GLsizei height,GLboolean fixedsamplelocations)7214 _mesa_TextureStorage2DMultisample(GLuint texture, GLsizei samples,
7215                                   GLenum internalformat, GLsizei width,
7216                                   GLsizei height,
7217                                   GLboolean fixedsamplelocations)
7218 {
7219    struct gl_texture_object *texObj;
7220    GET_CURRENT_CONTEXT(ctx);
7221 
7222    texObj = _mesa_lookup_texture_err(ctx, texture,
7223                                      "glTextureStorage2DMultisample");
7224    if (!texObj)
7225       return;
7226 
7227    if (!valid_texstorage_ms_parameters(width, height, 1, 2))
7228       return;
7229 
7230    texture_image_multisample(ctx, 2, texObj, NULL, texObj->Target,
7231                              samples, internalformat, width, height, 1,
7232                              fixedsamplelocations, GL_TRUE, 0,
7233                              "glTextureStorage2DMultisample");
7234 }
7235 
7236 void GLAPIENTRY
_mesa_TextureStorage3DMultisample(GLuint texture,GLsizei samples,GLenum internalformat,GLsizei width,GLsizei height,GLsizei depth,GLboolean fixedsamplelocations)7237 _mesa_TextureStorage3DMultisample(GLuint texture, GLsizei samples,
7238                                   GLenum internalformat, GLsizei width,
7239                                   GLsizei height, GLsizei depth,
7240                                   GLboolean fixedsamplelocations)
7241 {
7242    struct gl_texture_object *texObj;
7243    GET_CURRENT_CONTEXT(ctx);
7244 
7245    /* Get the texture object by Name. */
7246    texObj = _mesa_lookup_texture_err(ctx, texture,
7247                                      "glTextureStorage3DMultisample");
7248    if (!texObj)
7249       return;
7250 
7251    if (!valid_texstorage_ms_parameters(width, height, depth, 3))
7252       return;
7253 
7254    texture_image_multisample(ctx, 3, texObj, NULL, texObj->Target, samples,
7255                              internalformat, width, height, depth,
7256                              fixedsamplelocations, GL_TRUE, 0,
7257                              "glTextureStorage3DMultisample");
7258 }
7259 
7260 void GLAPIENTRY
_mesa_TextureStorage2DMultisampleEXT(GLuint texture,GLenum target,GLsizei samples,GLenum internalformat,GLsizei width,GLsizei height,GLboolean fixedsamplelocations)7261 _mesa_TextureStorage2DMultisampleEXT(GLuint texture, GLenum target, GLsizei samples,
7262                                      GLenum internalformat, GLsizei width,
7263                                      GLsizei height,
7264                                      GLboolean fixedsamplelocations)
7265 {
7266    struct gl_texture_object *texObj;
7267    GET_CURRENT_CONTEXT(ctx);
7268 
7269    texObj = lookup_texture_ext_dsa(ctx, target, texture,
7270                                    "glTextureStorage2DMultisampleEXT");
7271    if (!texObj)
7272       return;
7273 
7274    if (!valid_texstorage_ms_parameters(width, height, 1, 2))
7275       return;
7276 
7277    texture_image_multisample(ctx, 2, texObj, NULL, texObj->Target,
7278                              samples, internalformat, width, height, 1,
7279                              fixedsamplelocations, GL_TRUE, 0,
7280                              "glTextureStorage2DMultisampleEXT");
7281 }
7282 
7283 void GLAPIENTRY
_mesa_TextureStorage3DMultisampleEXT(GLuint texture,GLenum target,GLsizei samples,GLenum internalformat,GLsizei width,GLsizei height,GLsizei depth,GLboolean fixedsamplelocations)7284 _mesa_TextureStorage3DMultisampleEXT(GLuint texture, GLenum target, GLsizei samples,
7285                                      GLenum internalformat, GLsizei width,
7286                                      GLsizei height, GLsizei depth,
7287                                      GLboolean fixedsamplelocations)
7288 {
7289    struct gl_texture_object *texObj;
7290    GET_CURRENT_CONTEXT(ctx);
7291 
7292    texObj = lookup_texture_ext_dsa(ctx, target, texture,
7293                                    "glTextureStorage3DMultisampleEXT");
7294    if (!texObj)
7295       return;
7296 
7297    if (!valid_texstorage_ms_parameters(width, height, depth, 3))
7298       return;
7299 
7300    texture_image_multisample(ctx, 3, texObj, NULL, texObj->Target, samples,
7301                              internalformat, width, height, depth,
7302                              fixedsamplelocations, GL_TRUE, 0,
7303                              "glTextureStorage3DMultisampleEXT");
7304 }
7305 
7306 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)7307 _mesa_texture_storage_ms_memory(struct gl_context *ctx, GLuint dims,
7308                                 struct gl_texture_object *texObj,
7309                                 struct gl_memory_object *memObj,
7310                                 GLenum target, GLsizei samples,
7311                                 GLenum internalFormat, GLsizei width,
7312                                 GLsizei height, GLsizei depth,
7313                                 GLboolean fixedSampleLocations,
7314                                 GLuint64 offset, const char* func)
7315 {
7316    assert(memObj);
7317 
7318    texture_image_multisample(ctx, dims, texObj, memObj, target, samples,
7319                              internalFormat, width, height, depth,
7320                              fixedSampleLocations, GL_TRUE, offset,
7321                              func);
7322 }
7323