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