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