main: Refactor in teximage.c to handle NULL from _mesa_get_current_tex_object.
[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 for (i = 0; i < 6; ++i) { /* For each face. */
3561 if (!texObj->Image[i][level]) {
3562 /* Not enough image planes for a cube map. The spec does not say
3563 * what should happen in this case because the user has always
3564 * specified each cube face separately (using
3565 * GL_TEXTURE_CUBE_MAP_POSITIVE_X+i) in previous GL versions.
3566 * This is addressed in Khronos Bug 13223.
3567 */
3568 _mesa_error(ctx, GL_INVALID_OPERATION,
3569 "glTextureSubImage%uD(insufficient cube map storage)",
3570 dims);
3571 return;
3572 }
3573 }
3574
3575 /* Copy in each face. */
3576 for (i = 0; i < 6; ++i) {
3577 texImage = texObj->Image[i][level];
3578 _mesa_texture_sub_image(ctx, 3, texObj, texImage, texObj->Target,
3579 level, xoffset, yoffset, zoffset,
3580 width, height, 1, format,
3581 type, pixels, true);
3582 pixels += _mesa_image_image_stride(&ctx->Unpack, width, height,
3583 format, type);
3584 }
3585 }
3586 else {
3587 texImage = _mesa_select_tex_image(texObj, texObj->Target, level);
3588 if (!texImage)
3589 return;
3590
3591 _mesa_texture_sub_image(ctx, dims, texObj, texImage, texObj->Target,
3592 level, xoffset, yoffset, zoffset,
3593 width, height, depth, format,
3594 type, pixels, true);
3595 }
3596 }
3597
3598
3599 void GLAPIENTRY
3600 _mesa_TexSubImage1D( GLenum target, GLint level,
3601 GLint xoffset, GLsizei width,
3602 GLenum format, GLenum type,
3603 const GLvoid *pixels )
3604 {
3605 GET_CURRENT_CONTEXT(ctx);
3606 texsubimage(ctx, 1, target, level,
3607 xoffset, 0, 0,
3608 width, 1, 1,
3609 format, type, pixels);
3610 }
3611
3612
3613 void GLAPIENTRY
3614 _mesa_TexSubImage2D( GLenum target, GLint level,
3615 GLint xoffset, GLint yoffset,
3616 GLsizei width, GLsizei height,
3617 GLenum format, GLenum type,
3618 const GLvoid *pixels )
3619 {
3620 GET_CURRENT_CONTEXT(ctx);
3621 texsubimage(ctx, 2, target, level,
3622 xoffset, yoffset, 0,
3623 width, height, 1,
3624 format, type, pixels);
3625 }
3626
3627
3628
3629 void GLAPIENTRY
3630 _mesa_TexSubImage3D( GLenum target, GLint level,
3631 GLint xoffset, GLint yoffset, GLint zoffset,
3632 GLsizei width, GLsizei height, GLsizei depth,
3633 GLenum format, GLenum type,
3634 const GLvoid *pixels )
3635 {
3636 GET_CURRENT_CONTEXT(ctx);
3637 texsubimage(ctx, 3, target, level,
3638 xoffset, yoffset, zoffset,
3639 width, height, depth,
3640 format, type, pixels);
3641 }
3642
3643 void GLAPIENTRY
3644 _mesa_TextureSubImage1D(GLuint texture, GLint level,
3645 GLint xoffset, GLsizei width,
3646 GLenum format, GLenum type,
3647 const GLvoid *pixels)
3648 {
3649 GET_CURRENT_CONTEXT(ctx);
3650 texturesubimage(ctx, 1, texture, level,
3651 xoffset, 0, 0,
3652 width, 1, 1,
3653 format, type, pixels);
3654 }
3655
3656
3657 void GLAPIENTRY
3658 _mesa_TextureSubImage2D(GLuint texture, GLint level,
3659 GLint xoffset, GLint yoffset,
3660 GLsizei width, GLsizei height,
3661 GLenum format, GLenum type,
3662 const GLvoid *pixels)
3663 {
3664 GET_CURRENT_CONTEXT(ctx);
3665 texturesubimage(ctx, 2, texture, level,
3666 xoffset, yoffset, 0,
3667 width, height, 1,
3668 format, type, pixels);
3669 }
3670
3671
3672 void GLAPIENTRY
3673 _mesa_TextureSubImage3D(GLuint texture, GLint level,
3674 GLint xoffset, GLint yoffset, GLint zoffset,
3675 GLsizei width, GLsizei height, GLsizei depth,
3676 GLenum format, GLenum type,
3677 const GLvoid *pixels)
3678 {
3679 GET_CURRENT_CONTEXT(ctx);
3680 texturesubimage(ctx, 3, texture, level,
3681 xoffset, yoffset, zoffset,
3682 width, height, depth,
3683 format, type, pixels);
3684 }
3685
3686
3687 /**
3688 * For glCopyTexSubImage, return the source renderbuffer to copy texel data
3689 * from. This depends on whether the texture contains color or depth values.
3690 */
3691 static struct gl_renderbuffer *
3692 get_copy_tex_image_source(struct gl_context *ctx, mesa_format texFormat)
3693 {
3694 if (_mesa_get_format_bits(texFormat, GL_DEPTH_BITS) > 0) {
3695 /* reading from depth/stencil buffer */
3696 return ctx->ReadBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
3697 }
3698 else {
3699 /* copying from color buffer */
3700 return ctx->ReadBuffer->_ColorReadBuffer;
3701 }
3702 }
3703
3704 static void
3705 copytexsubimage_by_slice(struct gl_context *ctx,
3706 struct gl_texture_image *texImage,
3707 GLuint dims,
3708 GLint xoffset, GLint yoffset, GLint zoffset,
3709 struct gl_renderbuffer *rb,
3710 GLint x, GLint y,
3711 GLsizei width, GLsizei height)
3712 {
3713 if (texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
3714 int slice;
3715
3716 /* For 1D arrays, we copy each scanline of the source rectangle into the
3717 * next array slice.
3718 */
3719 assert(zoffset == 0);
3720
3721 for (slice = 0; slice < height; slice++) {
3722 assert(yoffset + slice < texImage->Height);
3723 ctx->Driver.CopyTexSubImage(ctx, 2, texImage,
3724 xoffset, 0, yoffset + slice,
3725 rb, x, y + slice, width, 1);
3726 }
3727 } else {
3728 ctx->Driver.CopyTexSubImage(ctx, dims, texImage,
3729 xoffset, yoffset, zoffset,
3730 rb, x, y, width, height);
3731 }
3732 }
3733
3734 static GLboolean
3735 formats_differ_in_component_sizes (mesa_format f1,
3736 mesa_format f2)
3737 {
3738 GLint f1_r_bits = _mesa_get_format_bits(f1, GL_RED_BITS);
3739 GLint f1_g_bits = _mesa_get_format_bits(f1, GL_GREEN_BITS);
3740 GLint f1_b_bits = _mesa_get_format_bits(f1, GL_BLUE_BITS);
3741 GLint f1_a_bits = _mesa_get_format_bits(f1, GL_ALPHA_BITS);
3742
3743 GLint f2_r_bits = _mesa_get_format_bits(f2, GL_RED_BITS);
3744 GLint f2_g_bits = _mesa_get_format_bits(f2, GL_GREEN_BITS);
3745 GLint f2_b_bits = _mesa_get_format_bits(f2, GL_BLUE_BITS);
3746 GLint f2_a_bits = _mesa_get_format_bits(f2, GL_ALPHA_BITS);
3747
3748 if ((f1_r_bits && f2_r_bits && f1_r_bits != f2_r_bits)
3749 || (f1_g_bits && f2_g_bits && f1_g_bits != f2_g_bits)
3750 || (f1_b_bits && f2_b_bits && f1_b_bits != f2_b_bits)
3751 || (f1_a_bits && f2_a_bits && f1_a_bits != f2_a_bits))
3752 return GL_TRUE;
3753
3754 return GL_FALSE;
3755 }
3756
3757 /**
3758 * Implement the glCopyTexImage1/2D() functions.
3759 */
3760 static void
3761 copyteximage(struct gl_context *ctx, GLuint dims,
3762 GLenum target, GLint level, GLenum internalFormat,
3763 GLint x, GLint y, GLsizei width, GLsizei height, GLint border )
3764 {
3765 struct gl_texture_object *texObj;
3766 struct gl_texture_image *texImage;
3767 const GLuint face = _mesa_tex_target_to_face(target);
3768 mesa_format texFormat;
3769 struct gl_renderbuffer *rb;
3770
3771 FLUSH_VERTICES(ctx, 0);
3772
3773 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3774 _mesa_debug(ctx, "glCopyTexImage%uD %s %d %s %d %d %d %d %d\n",
3775 dims,
3776 _mesa_lookup_enum_by_nr(target), level,
3777 _mesa_lookup_enum_by_nr(internalFormat),
3778 x, y, width, height, border);
3779
3780 if (ctx->NewState & NEW_COPY_TEX_STATE)
3781 _mesa_update_state(ctx);
3782
3783 if (copytexture_error_check(ctx, dims, target, level, internalFormat,
3784 width, height, border))
3785 return;
3786
3787 if (!_mesa_legal_texture_dimensions(ctx, target, level, width, height,
3788 1, border)) {
3789 _mesa_error(ctx, GL_INVALID_VALUE,
3790 "glCopyTexImage%uD(invalid width or height)", dims);
3791 return;
3792 }
3793
3794 texObj = _mesa_get_current_tex_object(ctx, target);
3795 assert(texObj);
3796
3797 texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
3798 internalFormat, GL_NONE, GL_NONE);
3799
3800 rb = _mesa_get_read_renderbuffer_for_format(ctx, internalFormat);
3801
3802 if (_mesa_is_gles3(ctx)) {
3803 if (_mesa_is_enum_format_unsized(internalFormat)) {
3804 /* Conversion from GL_RGB10_A2 source buffer format is not allowed in
3805 * OpenGL ES 3.0. Khronos bug# 9807.
3806 */
3807 if (rb->InternalFormat == GL_RGB10_A2) {
3808 _mesa_error(ctx, GL_INVALID_OPERATION,
3809 "glCopyTexImage%uD(Reading from GL_RGB10_A2 buffer and"
3810 " writing to unsized internal format)", dims);
3811 return;
3812 }
3813 }
3814 /* From Page 139 of OpenGL ES 3.0 spec:
3815 * "If internalformat is sized, the internal format of the new texel
3816 * array is internalformat, and this is also the new texel array’s
3817 * effective internal format. If the component sizes of internalformat
3818 * do not exactly match the corresponding component sizes of the source
3819 * buffer’s effective internal format, described below, an
3820 * INVALID_OPERATION error is generated. If internalformat is unsized,
3821 * the internal format of the new texel array is the effective internal
3822 * format of the source buffer, and this is also the new texel array’s
3823 * effective internal format.
3824 */
3825 else if (formats_differ_in_component_sizes (texFormat, rb->Format)) {
3826 _mesa_error(ctx, GL_INVALID_OPERATION,
3827 "glCopyTexImage%uD(componenet size changed in"
3828 " internal format)", dims);
3829 return;
3830 }
3831 }
3832
3833 assert(texFormat != MESA_FORMAT_NONE);
3834
3835 if (!ctx->Driver.TestProxyTexImage(ctx, proxy_target(target),
3836 level, texFormat,
3837 width, height, 1, border)) {
3838 _mesa_error(ctx, GL_OUT_OF_MEMORY,
3839 "glCopyTexImage%uD(image too large)", dims);
3840 return;
3841 }
3842
3843 if (border && ctx->Const.StripTextureBorder) {
3844 x += border;
3845 width -= border * 2;
3846 if (dims == 2) {
3847 y += border;
3848 height -= border * 2;
3849 }
3850 border = 0;
3851 }
3852
3853 _mesa_lock_texture(ctx, texObj);
3854 {
3855 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
3856
3857 if (!texImage) {
3858 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage%uD", dims);
3859 }
3860 else {
3861 GLint srcX = x, srcY = y, dstX = 0, dstY = 0, dstZ = 0;
3862
3863 /* Free old texture image */
3864 ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
3865
3866 _mesa_init_teximage_fields(ctx, texImage, width, height, 1,
3867 border, internalFormat, texFormat);
3868
3869 if (width && height) {
3870 /* Allocate texture memory (no pixel data yet) */
3871 ctx->Driver.AllocTextureImageBuffer(ctx, texImage);
3872
3873 if (_mesa_clip_copytexsubimage(ctx, &dstX, &dstY, &srcX, &srcY,
3874 &width, &height)) {
3875 struct gl_renderbuffer *srcRb =
3876 get_copy_tex_image_source(ctx, texImage->TexFormat);
3877
3878 copytexsubimage_by_slice(ctx, texImage, dims,
3879 dstX, dstY, dstZ,
3880 srcRb, srcX, srcY, width, height);
3881 }
3882
3883 check_gen_mipmap(ctx, target, texObj, level);
3884 }
3885
3886 _mesa_update_fbo_texture(ctx, texObj, face, level);
3887
3888 _mesa_dirty_texobj(ctx, texObj);
3889 }
3890 }
3891 _mesa_unlock_texture(ctx, texObj);
3892 }
3893
3894
3895
3896 void GLAPIENTRY
3897 _mesa_CopyTexImage1D( GLenum target, GLint level,
3898 GLenum internalFormat,
3899 GLint x, GLint y,
3900 GLsizei width, GLint border )
3901 {
3902 GET_CURRENT_CONTEXT(ctx);
3903 copyteximage(ctx, 1, target, level, internalFormat, x, y, width, 1, border);
3904 }
3905
3906
3907
3908 void GLAPIENTRY
3909 _mesa_CopyTexImage2D( GLenum target, GLint level, GLenum internalFormat,
3910 GLint x, GLint y, GLsizei width, GLsizei height,
3911 GLint border )
3912 {
3913 GET_CURRENT_CONTEXT(ctx);
3914 copyteximage(ctx, 2, target, level, internalFormat,
3915 x, y, width, height, border);
3916 }
3917
3918 /**
3919 * Implementation for glCopyTex(ture)SubImage1/2/3D() functions.
3920 */
3921 void
3922 _mesa_copy_texture_sub_image(struct gl_context *ctx, GLuint dims,
3923 struct gl_texture_object *texObj,
3924 GLenum target, GLint level,
3925 GLint xoffset, GLint yoffset, GLint zoffset,
3926 GLint x, GLint y,
3927 GLsizei width, GLsizei height,
3928 bool dsa)
3929 {
3930 struct gl_texture_image *texImage;
3931
3932 FLUSH_VERTICES(ctx, 0);
3933
3934 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3935 _mesa_debug(ctx, "glCopyTex%sSubImage%uD %s %d %d %d %d %d %d %d %d\n",
3936 dsa ? "ture" : "", dims,
3937 _mesa_lookup_enum_by_nr(target),
3938 level, xoffset, yoffset, zoffset, x, y, width, height);
3939
3940 if (ctx->NewState & NEW_COPY_TEX_STATE)
3941 _mesa_update_state(ctx);
3942
3943 if (copytexsubimage_error_check(ctx, dims, texObj, target, level,
3944 xoffset, yoffset, zoffset,
3945 width, height, dsa)) {
3946 return;
3947 }
3948
3949 _mesa_lock_texture(ctx, texObj);
3950 {
3951 texImage = _mesa_select_tex_image(texObj, target, level);
3952
3953 /* If we have a border, offset=-1 is legal. Bias by border width. */
3954 switch (dims) {
3955 case 3:
3956 if (target != GL_TEXTURE_2D_ARRAY)
3957 zoffset += texImage->Border;
3958 /* fall-through */
3959 case 2:
3960 if (target != GL_TEXTURE_1D_ARRAY)
3961 yoffset += texImage->Border;
3962 /* fall-through */
3963 case 1:
3964 xoffset += texImage->Border;
3965 }
3966
3967 if (_mesa_clip_copytexsubimage(ctx, &xoffset, &yoffset, &x, &y,
3968 &width, &height)) {
3969 struct gl_renderbuffer *srcRb =
3970 get_copy_tex_image_source(ctx, texImage->TexFormat);
3971
3972 copytexsubimage_by_slice(ctx, texImage, dims,
3973 xoffset, yoffset, zoffset,
3974 srcRb, x, y, width, height);
3975
3976 check_gen_mipmap(ctx, target, texObj, level);
3977
3978 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
3979 * the texel data, not the texture format, size, etc.
3980 */
3981 }
3982 }
3983 _mesa_unlock_texture(ctx, texObj);
3984 }
3985
3986 void GLAPIENTRY
3987 _mesa_CopyTexSubImage1D( GLenum target, GLint level,
3988 GLint xoffset, GLint x, GLint y, GLsizei width )
3989 {
3990 struct gl_texture_object* texObj;
3991 GET_CURRENT_CONTEXT(ctx);
3992
3993 texObj = _mesa_get_current_tex_object(ctx, target);
3994 if (!texObj)
3995 return;
3996
3997 _mesa_copy_texture_sub_image(ctx, 1, texObj, target, level, xoffset, 0, 0,
3998 x, y, width, 1, false);
3999 }
4000
4001
4002
4003 void GLAPIENTRY
4004 _mesa_CopyTexSubImage2D( GLenum target, GLint level,
4005 GLint xoffset, GLint yoffset,
4006 GLint x, GLint y, GLsizei width, GLsizei height )
4007 {
4008 struct gl_texture_object* texObj;
4009 GET_CURRENT_CONTEXT(ctx);
4010
4011 texObj = _mesa_get_current_tex_object(ctx, target);
4012 if (!texObj)
4013 return;
4014
4015 _mesa_copy_texture_sub_image(ctx, 2, texObj, target, level,
4016 xoffset, yoffset, 0,
4017 x, y, width, height, false);
4018 }
4019
4020
4021
4022 void GLAPIENTRY
4023 _mesa_CopyTexSubImage3D( GLenum target, GLint level,
4024 GLint xoffset, GLint yoffset, GLint zoffset,
4025 GLint x, GLint y, GLsizei width, GLsizei height )
4026 {
4027 struct gl_texture_object* texObj;
4028 GET_CURRENT_CONTEXT(ctx);
4029
4030 texObj = _mesa_get_current_tex_object(ctx, target);
4031 if (!texObj)
4032 return;
4033
4034 _mesa_copy_texture_sub_image(ctx, 3, texObj, target, level,
4035 xoffset, yoffset, zoffset,
4036 x, y, width, height, false);
4037 }
4038
4039 void GLAPIENTRY
4040 _mesa_CopyTextureSubImage1D(GLuint texture, GLint level,
4041 GLint xoffset, GLint x, GLint y, GLsizei width)
4042 {
4043 struct gl_texture_object* texObj;
4044 GET_CURRENT_CONTEXT(ctx);
4045
4046 texObj = _mesa_lookup_texture_err(ctx, texture, "glCopyTextureSubImage1D");
4047 if (!texObj)
4048 return;
4049
4050 _mesa_copy_texture_sub_image(ctx, 1, texObj, texObj->Target, level,
4051 xoffset, 0, 0, x, y, width, 1, true);
4052 }
4053
4054 void GLAPIENTRY
4055 _mesa_CopyTextureSubImage2D(GLuint texture, GLint level,
4056 GLint xoffset, GLint yoffset,
4057 GLint x, GLint y, GLsizei width, GLsizei height)
4058 {
4059 struct gl_texture_object* texObj;
4060 GET_CURRENT_CONTEXT(ctx);
4061
4062 texObj = _mesa_lookup_texture_err(ctx, texture, "glCopyTextureSubImage2D");
4063 if (!texObj)
4064 return;
4065
4066 _mesa_copy_texture_sub_image(ctx, 2, texObj, texObj->Target, level,
4067 xoffset, yoffset, 0,
4068 x, y, width, height, true);
4069 }
4070
4071
4072
4073 void GLAPIENTRY
4074 _mesa_CopyTextureSubImage3D(GLuint texture, GLint level,
4075 GLint xoffset, GLint yoffset, GLint zoffset,
4076 GLint x, GLint y, GLsizei width, GLsizei height)
4077 {
4078 struct gl_texture_object* texObj;
4079 GET_CURRENT_CONTEXT(ctx);
4080
4081 texObj = _mesa_lookup_texture_err(ctx, texture, "glCopyTextureSubImage3D");
4082 if (!texObj)
4083 return;
4084
4085 _mesa_copy_texture_sub_image(ctx, 3, texObj, texObj->Target, level,
4086 xoffset, yoffset, zoffset,
4087 x, y, width, height, true);
4088 }
4089
4090 static bool
4091 check_clear_tex_image(struct gl_context *ctx,
4092 const char *function,
4093 struct gl_texture_image *texImage,
4094 GLenum format, GLenum type,
4095 const void *data,
4096 GLubyte *clearValue)
4097 {
4098 struct gl_texture_object *texObj = texImage->TexObject;
4099 static const GLubyte zeroData[MAX_PIXEL_BYTES];
4100 GLenum internalFormat = texImage->InternalFormat;
4101 GLenum err;
4102
4103 if (texObj->Target == GL_TEXTURE_BUFFER) {
4104 _mesa_error(ctx, GL_INVALID_OPERATION,
4105 "%s(buffer texture)", function);
4106 return false;
4107 }
4108
4109 if (_mesa_is_compressed_format(ctx, internalFormat)) {
4110 _mesa_error(ctx, GL_INVALID_OPERATION,
4111 "%s(compressed texture)", function);
4112 return false;
4113 }
4114
4115 err = _mesa_error_check_format_and_type(ctx, format, type);
4116 if (err != GL_NO_ERROR) {
4117 _mesa_error(ctx, err,
4118 "%s(incompatible format = %s, type = %s)",
4119 function,
4120 _mesa_lookup_enum_by_nr(format),
4121 _mesa_lookup_enum_by_nr(type));
4122 return false;
4123 }
4124
4125 /* make sure internal format and format basically agree */
4126 if (!texture_formats_agree(internalFormat, format)) {
4127 _mesa_error(ctx, GL_INVALID_OPERATION,
4128 "%s(incompatible internalFormat = %s, format = %s)",
4129 function,
4130 _mesa_lookup_enum_by_nr(internalFormat),
4131 _mesa_lookup_enum_by_nr(format));
4132 return false;
4133 }
4134
4135 if (ctx->Version >= 30 || ctx->Extensions.EXT_texture_integer) {
4136 /* both source and dest must be integer-valued, or neither */
4137 if (_mesa_is_format_integer_color(texImage->TexFormat) !=
4138 _mesa_is_enum_format_integer(format)) {
4139 _mesa_error(ctx, GL_INVALID_OPERATION,
4140 "%s(integer/non-integer format mismatch)",
4141 function);
4142 return false;
4143 }
4144 }
4145
4146 if (!_mesa_texstore(ctx,
4147 1, /* dims */
4148 texImage->_BaseFormat,
4149 texImage->TexFormat,
4150 0, /* dstRowStride */
4151 &clearValue,
4152 1, 1, 1, /* srcWidth/Height/Depth */
4153 format, type,
4154 data ? data : zeroData,
4155 &ctx->DefaultPacking)) {
4156 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid format)", function);
4157 return false;
4158 }
4159
4160 return true;
4161 }
4162
4163 static struct gl_texture_object *
4164 get_tex_obj_for_clear(struct gl_context *ctx,
4165 const char *function,
4166 GLuint texture)
4167 {
4168 struct gl_texture_object *texObj;
4169
4170 if (texture == 0) {
4171 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(zero texture)", function);
4172 return NULL;
4173 }
4174
4175 texObj = _mesa_lookup_texture(ctx, texture);
4176
4177 if (texObj == NULL) {
4178 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(non-gen name)", function);
4179 return NULL;
4180 }
4181
4182 if (texObj->Target == 0) {
4183 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(unbound tex)", function);
4184 return NULL;
4185 }
4186
4187 return texObj;
4188 }
4189
4190 static int
4191 get_tex_images_for_clear(struct gl_context *ctx,
4192 const char *function,
4193 struct gl_texture_object *texObj,
4194 GLint level,
4195 struct gl_texture_image **texImages)
4196 {
4197 GLenum target;
4198 int i;
4199
4200 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
4201 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid level)", function);
4202 return 0;
4203 }
4204
4205 if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
4206 for (i = 0; i < MAX_FACES; i++) {
4207 target = GL_TEXTURE_CUBE_MAP_POSITIVE_X + i;
4208
4209 texImages[i] = _mesa_select_tex_image(texObj, target, level);
4210 if (texImages[i] == NULL) {
4211 _mesa_error(ctx, GL_INVALID_OPERATION,
4212 "%s(invalid level)", function);
4213 return 0;
4214 }
4215 }
4216
4217 return MAX_FACES;
4218 }
4219
4220 texImages[0] = _mesa_select_tex_image(texObj, texObj->Target, level);
4221
4222 if (texImages[0] == NULL) {
4223 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid level)", function);
4224 return 0;
4225 }
4226
4227 return 1;
4228 }
4229
4230 void GLAPIENTRY
4231 _mesa_ClearTexSubImage( GLuint texture, GLint level,
4232 GLint xoffset, GLint yoffset, GLint zoffset,
4233 GLsizei width, GLsizei height, GLsizei depth,
4234 GLenum format, GLenum type, const void *data )
4235 {
4236 GET_CURRENT_CONTEXT(ctx);
4237 struct gl_texture_object *texObj;
4238 struct gl_texture_image *texImages[MAX_FACES];
4239 GLubyte clearValue[MAX_FACES][MAX_PIXEL_BYTES];
4240 int i, numImages;
4241 int minDepth, maxDepth;
4242
4243 texObj = get_tex_obj_for_clear(ctx, "glClearTexSubImage", texture);
4244
4245 if (texObj == NULL)
4246 return;
4247
4248 _mesa_lock_texture(ctx, texObj);
4249
4250 numImages = get_tex_images_for_clear(ctx, "glClearTexSubImage",
4251 texObj, level, texImages);
4252 if (numImages == 0)
4253 goto out;
4254
4255 if (numImages == 1) {
4256 minDepth = -(int) texImages[0]->Border;
4257 maxDepth = texImages[0]->Depth;
4258 } else {
4259 minDepth = 0;
4260 maxDepth = numImages;
4261 }
4262
4263 if (xoffset < -(GLint) texImages[0]->Border ||
4264 yoffset < -(GLint) texImages[0]->Border ||
4265 zoffset < minDepth ||
4266 width < 0 ||
4267 height < 0 ||
4268 depth < 0 ||
4269 xoffset + width > texImages[0]->Width ||
4270 yoffset + height > texImages[0]->Height ||
4271 zoffset + depth > maxDepth) {
4272 _mesa_error(ctx, GL_INVALID_OPERATION,
4273 "glClearSubTexImage(invalid dimensions)");
4274 goto out;
4275 }
4276
4277 if (numImages == 1) {
4278 if (check_clear_tex_image(ctx, "glClearTexSubImage",
4279 texImages[0],
4280 format, type, data, clearValue[0])) {
4281 ctx->Driver.ClearTexSubImage(ctx,
4282 texImages[0],
4283 xoffset, yoffset, zoffset,
4284 width, height, depth,
4285 data ? clearValue[0] : NULL);
4286 }
4287 } else {
4288 for (i = zoffset; i < zoffset + depth; i++) {
4289 if (!check_clear_tex_image(ctx, "glClearTexSubImage",
4290 texImages[i],
4291 format, type, data, clearValue[i]))
4292 goto out;
4293 }
4294 for (i = zoffset; i < zoffset + depth; i++) {
4295 ctx->Driver.ClearTexSubImage(ctx,
4296 texImages[i],
4297 xoffset, yoffset, 0,
4298 width, height, 1,
4299 data ? clearValue[i] : NULL);
4300 }
4301 }
4302
4303 out:
4304 _mesa_unlock_texture(ctx, texObj);
4305 }
4306
4307 void GLAPIENTRY
4308 _mesa_ClearTexImage( GLuint texture, GLint level,
4309 GLenum format, GLenum type, const void *data )
4310 {
4311 GET_CURRENT_CONTEXT(ctx);
4312 struct gl_texture_object *texObj;
4313 struct gl_texture_image *texImages[MAX_FACES];
4314 GLubyte clearValue[MAX_FACES][MAX_PIXEL_BYTES];
4315 int i, numImages;
4316
4317 texObj = get_tex_obj_for_clear(ctx, "glClearTexImage", texture);
4318
4319 if (texObj == NULL)
4320 return;
4321
4322 _mesa_lock_texture(ctx, texObj);
4323
4324 numImages = get_tex_images_for_clear(ctx, "glClearTexImage",
4325 texObj, level, texImages);
4326
4327 for (i = 0; i < numImages; i++) {
4328 if (!check_clear_tex_image(ctx, "glClearTexImage",
4329 texImages[i],
4330 format, type, data,
4331 clearValue[i]))
4332 goto out;
4333 }
4334
4335 for (i = 0; i < numImages; i++) {
4336 ctx->Driver.ClearTexSubImage(ctx, texImages[i],
4337 -(GLint) texImages[i]->Border, /* xoffset */
4338 -(GLint) texImages[i]->Border, /* yoffset */
4339 -(GLint) texImages[i]->Border, /* zoffset */
4340 texImages[i]->Width,
4341 texImages[i]->Height,
4342 texImages[i]->Depth,
4343 data ? clearValue[i] : NULL);
4344 }
4345
4346 out:
4347 _mesa_unlock_texture(ctx, texObj);
4348 }
4349
4350
4351
4352
4353 /**********************************************************************/
4354 /****** Compressed Textures ******/
4355 /**********************************************************************/
4356
4357
4358 /**
4359 * Error checking for glCompressedTexSubImage[123]D().
4360 * \return GL_TRUE if error, GL_FALSE if no error
4361 */
4362 static GLboolean
4363 compressed_subtexture_error_check(struct gl_context *ctx, GLint dims,
4364 const struct gl_texture_object *texObj,
4365 GLenum target, GLint level,
4366 GLint xoffset, GLint yoffset, GLint zoffset,
4367 GLsizei width, GLsizei height, GLsizei depth,
4368 GLenum format, GLsizei imageSize, bool dsa)
4369 {
4370 struct gl_texture_image *texImage;
4371 GLint expectedSize;
4372 GLboolean targetOK;
4373 const char *suffix = dsa ? "ture" : "";
4374
4375 if (dsa && target == GL_TEXTURE_RECTANGLE) {
4376 _mesa_error(ctx, GL_INVALID_OPERATION,
4377 "glCompressedSubTexture%dD(target)", dims);
4378 return GL_TRUE;
4379 }
4380
4381 switch (dims) {
4382 case 2:
4383 switch (target) {
4384 case GL_TEXTURE_2D:
4385 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
4386 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
4387 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
4388 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
4389 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
4390 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
4391 targetOK = GL_TRUE;
4392 break;
4393 default:
4394 targetOK = GL_FALSE;
4395 break;
4396 }
4397 break;
4398 case 3:
4399 targetOK = (target == GL_TEXTURE_3D) ||
4400 (target == GL_TEXTURE_2D_ARRAY) ||
4401 (target == GL_TEXTURE_CUBE_MAP_ARRAY) ||
4402 (target == GL_TEXTURE_CUBE_MAP && dsa);
4403
4404 /* OpenGL 4.5 spec (30.10.2014) says in Section 8.7 Compressed Texture
4405 * Images:
4406 * "An INVALID_OPERATION error is generated by
4407 * CompressedTex*SubImage3D if the internal format of the texture is
4408 * one of the EAC, ETC2, or RGTC formats and either border is
4409 * non-zero, or the effective target for the texture is not
4410 * TEXTURE_2D_ARRAY."
4411 */
4412 if (target != GL_TEXTURE_2D_ARRAY) {
4413 bool invalidformat;
4414 switch (format) {
4415 /* These came from _mesa_is_compressed_format in glformats.c. */
4416 /* EAC formats */
4417 case GL_COMPRESSED_RGBA8_ETC2_EAC:
4418 case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
4419 case GL_COMPRESSED_R11_EAC:
4420 case GL_COMPRESSED_RG11_EAC:
4421 case GL_COMPRESSED_SIGNED_R11_EAC:
4422 case GL_COMPRESSED_SIGNED_RG11_EAC:
4423 /* ETC2 formats */
4424 case GL_COMPRESSED_RGB8_ETC2:
4425 case GL_COMPRESSED_SRGB8_ETC2:
4426 case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
4427 case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
4428 /* RGTC formats */
4429 case GL_COMPRESSED_RED_RGTC1:
4430 case GL_COMPRESSED_SIGNED_RED_RGTC1:
4431 case GL_COMPRESSED_RG_RGTC2:
4432 case GL_COMPRESSED_SIGNED_RG_RGTC2:
4433 invalidformat = true;
4434 break;
4435 default:
4436 invalidformat = false;
4437 }
4438 if (invalidformat) {
4439 _mesa_error(ctx, GL_INVALID_OPERATION,
4440 "glCompressedTex%sSubImage%uD(target)", suffix, dims);
4441 return GL_TRUE;
4442 }
4443 }
4444
4445 break;
4446 default:
4447 assert(dims == 1);
4448 /* no 1D compressed textures at this time */
4449 targetOK = GL_FALSE;
4450 break;
4451 }
4452
4453 if (!targetOK) {
4454 _mesa_error(ctx, GL_INVALID_ENUM,
4455 "glCompressedTex%sSubImage%uD(target)", suffix, dims);
4456 return GL_TRUE;
4457 }
4458
4459 /* this will catch any invalid compressed format token */
4460 if (!_mesa_is_compressed_format(ctx, format)) {
4461 _mesa_error(ctx, GL_INVALID_ENUM,
4462 "glCompressedTex%sSubImage%uD(format)", suffix, dims);
4463 return GL_TRUE;
4464 }
4465
4466 if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
4467 _mesa_error(ctx, GL_INVALID_VALUE,
4468 "glCompressedTex%sSubImage%uD(level=%d)",
4469 suffix, dims, level);
4470 return GL_TRUE;
4471 }
4472
4473 /* Check for invalid pixel storage modes */
4474 if (!_mesa_compressed_pixel_storage_error_check(ctx, dims,
4475 &ctx->Unpack,
4476 dsa ? "glCompressedTextureSubImage" :
4477 "glCompressedTexSubImage")) {
4478 return GL_TRUE;
4479 }
4480
4481 expectedSize = compressed_tex_size(width, height, depth, format);
4482 if (expectedSize != imageSize) {
4483 _mesa_error(ctx, GL_INVALID_VALUE,
4484 "glCompressedTex%sSubImage%uD(size=%d)",
4485 suffix, dims, imageSize);
4486 return GL_TRUE;
4487 }
4488
4489 texImage = _mesa_select_tex_image(texObj, target, level);
4490 if (!texImage) {
4491 _mesa_error(ctx, GL_INVALID_OPERATION,
4492 "glCompressedTex%sSubImage%uD(invalid texture image)",
4493 suffix, dims);
4494 return GL_TRUE;
4495 }
4496
4497 if ((GLint) format != texImage->InternalFormat) {
4498 _mesa_error(ctx, GL_INVALID_OPERATION,
4499 "glCompressedTex%sSubImage%uD(format=0x%x)",
4500 suffix, dims, format);
4501 return GL_TRUE;
4502 }
4503
4504 if (compressedteximage_only_format(ctx, format)) {
4505 _mesa_error(ctx, GL_INVALID_OPERATION,
4506 "glCompressedTex%sSubImage%uD(format=0x%x cannot be updated)",
4507 suffix, dims, format);
4508 return GL_TRUE;
4509 }
4510
4511 if (error_check_subtexture_dimensions(ctx, dims,
4512 texImage, xoffset, yoffset, zoffset,
4513 width, height, depth,
4514 "glCompressedTexSubImage")) {
4515 return GL_TRUE;
4516 }
4517
4518 return GL_FALSE;
4519 }
4520
4521
4522 void GLAPIENTRY
4523 _mesa_CompressedTexImage1D(GLenum target, GLint level,
4524 GLenum internalFormat, GLsizei width,
4525 GLint border, GLsizei imageSize,
4526 const GLvoid *data)
4527 {
4528 GET_CURRENT_CONTEXT(ctx);
4529 teximage(ctx, GL_TRUE, 1, target, level, internalFormat,
4530 width, 1, 1, border, GL_NONE, GL_NONE, imageSize, data);
4531 }
4532
4533
4534 void GLAPIENTRY
4535 _mesa_CompressedTexImage2D(GLenum target, GLint level,
4536 GLenum internalFormat, GLsizei width,
4537 GLsizei height, GLint border, GLsizei imageSize,
4538 const GLvoid *data)
4539 {
4540 GET_CURRENT_CONTEXT(ctx);
4541 teximage(ctx, GL_TRUE, 2, target, level, internalFormat,
4542 width, height, 1, border, GL_NONE, GL_NONE, imageSize, data);
4543 }
4544
4545
4546 void GLAPIENTRY
4547 _mesa_CompressedTexImage3D(GLenum target, GLint level,
4548 GLenum internalFormat, GLsizei width,
4549 GLsizei height, GLsizei depth, GLint border,
4550 GLsizei imageSize, const GLvoid *data)
4551 {
4552 GET_CURRENT_CONTEXT(ctx);
4553 teximage(ctx, GL_TRUE, 3, target, level, internalFormat,
4554 width, height, depth, border, GL_NONE, GL_NONE, imageSize, data);
4555 }
4556
4557
4558 /**
4559 * Common helper for glCompressedTexSubImage1/2/3D() and
4560 * glCompressedTextureSubImage1/2/3D().
4561 */
4562 void
4563 _mesa_compressed_texture_sub_image(struct gl_context *ctx, GLuint dims,
4564 struct gl_texture_object *texObj,
4565 GLenum target, GLint level,
4566 GLint xoffset, GLint yoffset,
4567 GLint zoffset,
4568 GLsizei width, GLsizei height,
4569 GLsizei depth,
4570 GLenum format, GLsizei imageSize,
4571 const GLvoid *data, bool dsa)
4572 {
4573 struct gl_texture_image *texImage;
4574
4575 if (compressed_subtexture_error_check(ctx, dims, texObj, target,
4576 level, xoffset, yoffset, zoffset,
4577 width, height, depth,
4578 format, imageSize, dsa)) {
4579 return;
4580 }
4581
4582 FLUSH_VERTICES(ctx, 0);
4583
4584 _mesa_lock_texture(ctx, texObj);
4585 {
4586 texImage = _mesa_select_tex_image(texObj, target, level);
4587 assert(texImage);
4588
4589 if (width > 0 && height > 0 && depth > 0) {
4590 ctx->Driver.CompressedTexSubImage(ctx, dims, texImage,
4591 xoffset, yoffset, zoffset,
4592 width, height, depth,
4593 format, imageSize, data);
4594
4595 check_gen_mipmap(ctx, target, texObj, level);
4596
4597 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
4598 * the texel data, not the texture format, size, etc.
4599 */
4600 }
4601 }
4602 _mesa_unlock_texture(ctx, texObj);
4603 }
4604
4605
4606 void GLAPIENTRY
4607 _mesa_CompressedTexSubImage1D(GLenum target, GLint level, GLint xoffset,
4608 GLsizei width, GLenum format,
4609 GLsizei imageSize, const GLvoid *data)
4610 {
4611 struct gl_texture_object *texObj;
4612 GET_CURRENT_CONTEXT(ctx);
4613
4614 texObj = _mesa_get_current_tex_object(ctx, target);
4615 if (!texObj)
4616 return;
4617
4618 _mesa_compressed_texture_sub_image(ctx, 1, texObj, target, level,
4619 xoffset, 0, 0, width, 1, 1,
4620 format, imageSize, data, false);
4621 }
4622
4623 void GLAPIENTRY
4624 _mesa_CompressedTextureSubImage1D(GLuint texture, GLint level, GLint xoffset,
4625 GLsizei width, GLenum format,
4626 GLsizei imageSize, const GLvoid *data)
4627 {
4628 struct gl_texture_object *texObj;
4629 GET_CURRENT_CONTEXT(ctx);
4630
4631 texObj = _mesa_lookup_texture_err(ctx, texture,
4632 "glCompressedTextureSubImage1D");
4633 if (!texObj)
4634 return;
4635
4636 _mesa_compressed_texture_sub_image(ctx, 1, texObj, texObj->Target, level,
4637 xoffset, 0, 0, width, 1, 1,
4638 format, imageSize, data, true);
4639 }
4640
4641
4642 void GLAPIENTRY
4643 _mesa_CompressedTexSubImage2D(GLenum target, GLint level, GLint xoffset,
4644 GLint yoffset, GLsizei width, GLsizei height,
4645 GLenum format, GLsizei imageSize,
4646 const GLvoid *data)
4647 {
4648 struct gl_texture_object *texObj;
4649 GET_CURRENT_CONTEXT(ctx);
4650
4651 texObj = _mesa_get_current_tex_object(ctx, target);
4652 if (!texObj)
4653 return;
4654
4655 _mesa_compressed_texture_sub_image(ctx, 2, texObj, target, level,
4656 xoffset, yoffset, 0, width, height, 1,
4657 format, imageSize, data, false);
4658 }
4659
4660 void GLAPIENTRY
4661 _mesa_CompressedTextureSubImage2D(GLuint texture, GLint level, GLint xoffset,
4662 GLint yoffset,
4663 GLsizei width, GLsizei height,
4664 GLenum format, GLsizei imageSize,
4665 const GLvoid *data)
4666 {
4667 struct gl_texture_object *texObj;
4668 GET_CURRENT_CONTEXT(ctx);
4669
4670 texObj = _mesa_lookup_texture_err(ctx, texture,
4671 "glCompressedTextureSubImage2D");
4672 if (!texObj)
4673 return;
4674
4675 _mesa_compressed_texture_sub_image(ctx, 2, texObj, texObj->Target, level,
4676 xoffset, yoffset, 0, width, height, 1,
4677 format, imageSize, data, true);
4678 }
4679
4680 void GLAPIENTRY
4681 _mesa_CompressedTexSubImage3D(GLenum target, GLint level, GLint xoffset,
4682 GLint yoffset, GLint zoffset, GLsizei width,
4683 GLsizei height, GLsizei depth, GLenum format,
4684 GLsizei imageSize, const GLvoid *data)
4685 {
4686 struct gl_texture_object *texObj;
4687 GET_CURRENT_CONTEXT(ctx);
4688
4689 texObj = _mesa_get_current_tex_object(ctx, target);
4690 if (!texObj)
4691 return;
4692
4693 _mesa_compressed_texture_sub_image(ctx, 3, texObj, target, level,
4694 xoffset, yoffset, zoffset,
4695 width, height, depth,
4696 format, imageSize, data, false);
4697 }
4698
4699 void GLAPIENTRY
4700 _mesa_CompressedTextureSubImage3D(GLuint texture, GLint level, GLint xoffset,
4701 GLint yoffset, GLint zoffset, GLsizei width,
4702 GLsizei height, GLsizei depth,
4703 GLenum format, GLsizei imageSize,
4704 const GLvoid *data)
4705 {
4706 struct gl_texture_object *texObj;
4707 GET_CURRENT_CONTEXT(ctx);
4708
4709 texObj = _mesa_lookup_texture_err(ctx, texture,
4710 "glCompressedTextureSubImage3D");
4711
4712 _mesa_compressed_texture_sub_image(ctx, 3, texObj, texObj->Target, level,
4713 xoffset, yoffset, zoffset,
4714 width, height, depth,
4715 format, imageSize, data, true);
4716 }
4717
4718 static mesa_format
4719 get_texbuffer_format(const struct gl_context *ctx, GLenum internalFormat)
4720 {
4721 if (ctx->API != API_OPENGL_CORE) {
4722 switch (internalFormat) {
4723 case GL_ALPHA8:
4724 return MESA_FORMAT_A_UNORM8;
4725 case GL_ALPHA16:
4726 return MESA_FORMAT_A_UNORM16;
4727 case GL_ALPHA16F_ARB:
4728 return MESA_FORMAT_A_FLOAT16;
4729 case GL_ALPHA32F_ARB:
4730 return MESA_FORMAT_A_FLOAT32;
4731 case GL_ALPHA8I_EXT:
4732 return MESA_FORMAT_A_SINT8;
4733 case GL_ALPHA16I_EXT:
4734 return MESA_FORMAT_A_SINT16;
4735 case GL_ALPHA32I_EXT:
4736 return MESA_FORMAT_A_SINT32;
4737 case GL_ALPHA8UI_EXT:
4738 return MESA_FORMAT_A_UINT8;
4739 case GL_ALPHA16UI_EXT:
4740 return MESA_FORMAT_A_UINT16;
4741 case GL_ALPHA32UI_EXT:
4742 return MESA_FORMAT_A_UINT32;
4743 case GL_LUMINANCE8:
4744 return MESA_FORMAT_L_UNORM8;
4745 case GL_LUMINANCE16:
4746 return MESA_FORMAT_L_UNORM16;
4747 case GL_LUMINANCE16F_ARB:
4748 return MESA_FORMAT_L_FLOAT16;
4749 case GL_LUMINANCE32F_ARB:
4750 return MESA_FORMAT_L_FLOAT32;
4751 case GL_LUMINANCE8I_EXT:
4752 return MESA_FORMAT_L_SINT8;
4753 case GL_LUMINANCE16I_EXT:
4754 return MESA_FORMAT_L_SINT16;
4755 case GL_LUMINANCE32I_EXT:
4756 return MESA_FORMAT_L_SINT32;
4757 case GL_LUMINANCE8UI_EXT:
4758 return MESA_FORMAT_L_UINT8;
4759 case GL_LUMINANCE16UI_EXT:
4760 return MESA_FORMAT_L_UINT16;
4761 case GL_LUMINANCE32UI_EXT:
4762 return MESA_FORMAT_L_UINT32;
4763 case GL_LUMINANCE8_ALPHA8:
4764 return MESA_FORMAT_L8A8_UNORM;
4765 case GL_LUMINANCE16_ALPHA16:
4766 return MESA_FORMAT_L16A16_UNORM;
4767 case GL_LUMINANCE_ALPHA16F_ARB:
4768 return MESA_FORMAT_LA_FLOAT16;
4769 case GL_LUMINANCE_ALPHA32F_ARB:
4770 return MESA_FORMAT_LA_FLOAT32;
4771 case GL_LUMINANCE_ALPHA8I_EXT:
4772 return MESA_FORMAT_LA_SINT8;
4773 case GL_LUMINANCE_ALPHA16I_EXT:
4774 return MESA_FORMAT_LA_SINT8;
4775 case GL_LUMINANCE_ALPHA32I_EXT:
4776 return MESA_FORMAT_LA_SINT16;
4777 case GL_LUMINANCE_ALPHA8UI_EXT:
4778 return MESA_FORMAT_LA_UINT8;
4779 case GL_LUMINANCE_ALPHA16UI_EXT:
4780 return MESA_FORMAT_LA_UINT16;
4781 case GL_LUMINANCE_ALPHA32UI_EXT:
4782 return MESA_FORMAT_LA_UINT32;
4783 case GL_INTENSITY8:
4784 return MESA_FORMAT_I_UNORM8;
4785 case GL_INTENSITY16:
4786 return MESA_FORMAT_I_UNORM16;
4787 case GL_INTENSITY16F_ARB:
4788 return MESA_FORMAT_I_FLOAT16;
4789 case GL_INTENSITY32F_ARB:
4790 return MESA_FORMAT_I_FLOAT32;
4791 case GL_INTENSITY8I_EXT:
4792 return MESA_FORMAT_I_SINT8;
4793 case GL_INTENSITY16I_EXT:
4794 return MESA_FORMAT_I_SINT16;
4795 case GL_INTENSITY32I_EXT:
4796 return MESA_FORMAT_I_SINT32;
4797 case GL_INTENSITY8UI_EXT:
4798 return MESA_FORMAT_I_UINT8;
4799 case GL_INTENSITY16UI_EXT:
4800 return MESA_FORMAT_I_UINT16;
4801 case GL_INTENSITY32UI_EXT:
4802 return MESA_FORMAT_I_UINT32;
4803 default:
4804 break;
4805 }
4806 }
4807
4808 if (ctx->API == API_OPENGL_CORE &&
4809 ctx->Extensions.ARB_texture_buffer_object_rgb32) {
4810 switch (internalFormat) {
4811 case GL_RGB32F:
4812 return MESA_FORMAT_RGB_FLOAT32;
4813 case GL_RGB32UI:
4814 return MESA_FORMAT_RGB_UINT32;
4815 case GL_RGB32I:
4816 return MESA_FORMAT_RGB_SINT32;
4817 default:
4818 break;
4819 }
4820 }
4821
4822 switch (internalFormat) {
4823 case GL_RGBA8:
4824 return MESA_FORMAT_R8G8B8A8_UNORM;
4825 case GL_RGBA16:
4826 return MESA_FORMAT_RGBA_UNORM16;
4827 case GL_RGBA16F_ARB:
4828 return MESA_FORMAT_RGBA_FLOAT16;
4829 case GL_RGBA32F_ARB:
4830 return MESA_FORMAT_RGBA_FLOAT32;
4831 case GL_RGBA8I_EXT:
4832 return MESA_FORMAT_RGBA_SINT8;
4833 case GL_RGBA16I_EXT:
4834 return MESA_FORMAT_RGBA_SINT16;
4835 case GL_RGBA32I_EXT:
4836 return MESA_FORMAT_RGBA_SINT32;
4837 case GL_RGBA8UI_EXT:
4838 return MESA_FORMAT_RGBA_UINT8;
4839 case GL_RGBA16UI_EXT:
4840 return MESA_FORMAT_RGBA_UINT16;
4841 case GL_RGBA32UI_EXT:
4842 return MESA_FORMAT_RGBA_UINT32;
4843
4844 case GL_RG8:
4845 return MESA_FORMAT_R8G8_UNORM;
4846 case GL_RG16:
4847 return MESA_FORMAT_R16G16_UNORM;
4848 case GL_RG16F:
4849 return MESA_FORMAT_RG_FLOAT16;
4850 case GL_RG32F:
4851 return MESA_FORMAT_RG_FLOAT32;
4852 case GL_RG8I:
4853 return MESA_FORMAT_RG_SINT8;
4854 case GL_RG16I:
4855 return MESA_FORMAT_RG_SINT16;
4856 case GL_RG32I:
4857 return MESA_FORMAT_RG_SINT32;
4858 case GL_RG8UI:
4859 return MESA_FORMAT_RG_UINT8;
4860 case GL_RG16UI:
4861 return MESA_FORMAT_RG_UINT16;
4862 case GL_RG32UI:
4863 return MESA_FORMAT_RG_UINT32;
4864
4865 case GL_R8:
4866 return MESA_FORMAT_R_UNORM8;
4867 case GL_R16:
4868 return MESA_FORMAT_R_UNORM16;
4869 case GL_R16F:
4870 return MESA_FORMAT_R_FLOAT16;
4871 case GL_R32F:
4872 return MESA_FORMAT_R_FLOAT32;
4873 case GL_R8I:
4874 return MESA_FORMAT_R_SINT8;
4875 case GL_R16I:
4876 return MESA_FORMAT_R_SINT16;
4877 case GL_R32I:
4878 return MESA_FORMAT_R_SINT32;
4879 case GL_R8UI:
4880 return MESA_FORMAT_R_UINT8;
4881 case GL_R16UI:
4882 return MESA_FORMAT_R_UINT16;
4883 case GL_R32UI:
4884 return MESA_FORMAT_R_UINT32;
4885
4886 default:
4887 return MESA_FORMAT_NONE;
4888 }
4889 }
4890
4891
4892 mesa_format
4893 _mesa_validate_texbuffer_format(const struct gl_context *ctx,
4894 GLenum internalFormat)
4895 {
4896 mesa_format format = get_texbuffer_format(ctx, internalFormat);
4897 GLenum datatype;
4898
4899 if (format == MESA_FORMAT_NONE)
4900 return MESA_FORMAT_NONE;
4901
4902 datatype = _mesa_get_format_datatype(format);
4903
4904 /* The GL_ARB_texture_buffer_object spec says:
4905 *
4906 * "If ARB_texture_float is not supported, references to the
4907 * floating-point internal formats provided by that extension should be
4908 * removed, and such formats may not be passed to TexBufferARB."
4909 *
4910 * As a result, GL_HALF_FLOAT internal format depends on both
4911 * GL_ARB_texture_float and GL_ARB_half_float_pixel.
4912 */
4913 if ((datatype == GL_FLOAT || datatype == GL_HALF_FLOAT) &&
4914 !ctx->Extensions.ARB_texture_float)
4915 return MESA_FORMAT_NONE;
4916
4917 if (!ctx->Extensions.ARB_texture_rg) {
4918 GLenum base_format = _mesa_get_format_base_format(format);
4919 if (base_format == GL_R || base_format == GL_RG)
4920 return MESA_FORMAT_NONE;
4921 }
4922
4923 if (!ctx->Extensions.ARB_texture_buffer_object_rgb32) {
4924 GLenum base_format = _mesa_get_format_base_format(format);
4925 if (base_format == GL_RGB)
4926 return MESA_FORMAT_NONE;
4927 }
4928 return format;
4929 }
4930
4931
4932 void
4933 _mesa_texture_buffer_range(struct gl_context *ctx,
4934 struct gl_texture_object *texObj, GLenum target,
4935 GLenum internalFormat,
4936 struct gl_buffer_object *bufObj,
4937 GLintptr offset, GLsizeiptr size, bool range,
4938 bool dsa)
4939 {
4940 mesa_format format;
4941
4942 FLUSH_VERTICES(ctx, 0);
4943
4944 format = _mesa_validate_texbuffer_format(ctx, internalFormat);
4945 if (format == MESA_FORMAT_NONE) {
4946 _mesa_error(ctx, GL_INVALID_ENUM,
4947 "glTex%sBuffer%s(internalFormat 0x%x)", dsa ? "ture" : "",
4948 range ? "Range" : "", internalFormat);
4949 return;
4950 }
4951
4952 _mesa_lock_texture(ctx, texObj);
4953 {
4954 _mesa_reference_buffer_object(ctx, &texObj->BufferObject, bufObj);
4955 texObj->BufferObjectFormat = internalFormat;
4956 texObj->_BufferObjectFormat = format;
4957 texObj->BufferOffset = offset;
4958 texObj->BufferSize = size;
4959 }
4960 _mesa_unlock_texture(ctx, texObj);
4961
4962 ctx->NewDriverState |= ctx->DriverFlags.NewTextureBuffer;
4963
4964 if (bufObj) {
4965 bufObj->UsageHistory |= USAGE_TEXTURE_BUFFER;
4966 }
4967 }
4968
4969
4970 /** GL_ARB_texture_buffer_object */
4971 void GLAPIENTRY
4972 _mesa_TexBuffer(GLenum target, GLenum internalFormat, GLuint buffer)
4973 {
4974 struct gl_texture_object *texObj;
4975 struct gl_buffer_object *bufObj;
4976
4977 GET_CURRENT_CONTEXT(ctx);
4978
4979 /* Need to catch this before it gets to _mesa_get_current_tex_object */
4980 if (target != GL_TEXTURE_BUFFER_ARB) {
4981 _mesa_error(ctx, GL_INVALID_ENUM, "glTexBuffer(target)");
4982 return;
4983 }
4984
4985 /* NOTE: ARB_texture_buffer_object has interactions with
4986 * the compatibility profile that are not implemented.
4987 */
4988 if (!(ctx->API == API_OPENGL_CORE &&
4989 ctx->Extensions.ARB_texture_buffer_object)) {
4990 _mesa_error(ctx, GL_INVALID_OPERATION, "glTexBuffer");
4991 return;
4992 }
4993
4994 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
4995 if (!bufObj && buffer) {
4996 _mesa_error(ctx, GL_INVALID_OPERATION, "glTexBuffer(buffer %u)", buffer);
4997 return;
4998 }
4999
5000 texObj = _mesa_get_current_tex_object(ctx, target);
5001 if (!texObj)
5002 return;
5003
5004 _mesa_texture_buffer_range(ctx, texObj, target, internalFormat, bufObj, 0,
5005 buffer ? -1 : 0, false, false);
5006 }
5007
5008
5009 /** GL_ARB_texture_buffer_range */
5010 void GLAPIENTRY
5011 _mesa_TexBufferRange(GLenum target, GLenum internalFormat, GLuint buffer,
5012 GLintptr offset, GLsizeiptr size)
5013 {
5014 struct gl_texture_object *texObj;
5015 struct gl_buffer_object *bufObj;
5016
5017 GET_CURRENT_CONTEXT(ctx);
5018
5019 /* Need to catch this before it gets to _mesa_get_current_tex_object */
5020 if (target != GL_TEXTURE_BUFFER_ARB) {
5021 _mesa_error(ctx, GL_INVALID_ENUM, "glTexBufferRange(target)");
5022 return;
5023 }
5024
5025 if (!(ctx->API == API_OPENGL_CORE &&
5026 ctx->Extensions.ARB_texture_buffer_range)) {
5027 _mesa_error(ctx, GL_INVALID_OPERATION, "glTexBufferRange");
5028 return;
5029 }
5030
5031 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
5032 if (bufObj) {
5033 if (offset < 0 ||
5034 size <= 0 ||
5035 (offset + size) > bufObj->Size) {
5036 _mesa_error(ctx, GL_INVALID_VALUE, "glTexBufferRange");
5037 return;
5038 }
5039 if (offset % ctx->Const.TextureBufferOffsetAlignment) {
5040 _mesa_error(ctx, GL_INVALID_VALUE,
5041 "glTexBufferRange(invalid offset alignment)");
5042 return;
5043 }
5044 } else if (buffer) {
5045 _mesa_error(ctx, GL_INVALID_OPERATION, "glTexBufferRange(buffer %u)",
5046 buffer);
5047 return;
5048 } else {
5049 offset = 0;
5050 size = 0;
5051 }
5052
5053 texObj = _mesa_get_current_tex_object(ctx, target);
5054 if (!texObj)
5055 return;
5056
5057 _mesa_texture_buffer_range(ctx, texObj, target, internalFormat, bufObj,
5058 offset, size, true, false);
5059 }
5060
5061 void GLAPIENTRY
5062 _mesa_TextureBuffer(GLuint texture, GLenum internalFormat, GLuint buffer)
5063 {
5064 struct gl_texture_object *texObj;
5065 struct gl_buffer_object *bufObj;
5066
5067 GET_CURRENT_CONTEXT(ctx);
5068
5069 /* NOTE: ARB_texture_buffer_object has interactions with
5070 * the compatibility profile that are not implemented.
5071 */
5072 if (!(ctx->API == API_OPENGL_CORE &&
5073 ctx->Extensions.ARB_texture_buffer_object)) {
5074 _mesa_error(ctx, GL_INVALID_OPERATION, "glTextureBuffer");
5075 return;
5076 }
5077
5078 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
5079 if (!bufObj && buffer) {
5080 _mesa_error(ctx, GL_INVALID_OPERATION, "glTextureBuffer(buffer %u)",
5081 buffer);
5082 return;
5083 }
5084
5085 /* Get the texture object by Name. */
5086 texObj = _mesa_lookup_texture_err(ctx, texture,
5087 "glTextureBuffer(texture)");
5088 if (!texObj)
5089 return;
5090
5091 if (texObj->Target != GL_TEXTURE_BUFFER_ARB) {
5092 _mesa_error(ctx, GL_INVALID_ENUM, "glTextureBuffer(target)");
5093 return;
5094 }
5095
5096 _mesa_texture_buffer_range(ctx, texObj, texObj->Target, internalFormat,
5097 bufObj, 0, buffer ? -1 : 0, false, true);
5098 }
5099
5100 static GLboolean
5101 is_renderable_texture_format(struct gl_context *ctx, GLenum internalformat)
5102 {
5103 /* Everything that is allowed for renderbuffers,
5104 * except for a base format of GL_STENCIL_INDEX.
5105 */
5106 GLenum baseFormat = _mesa_base_fbo_format(ctx, internalformat);
5107 return baseFormat != 0 && baseFormat != GL_STENCIL_INDEX;
5108 }
5109
5110
5111 /** GL_ARB_texture_multisample */
5112 static GLboolean
5113 check_multisample_target(GLuint dims, GLenum target, bool dsa)
5114 {
5115 switch(target) {
5116 case GL_TEXTURE_2D_MULTISAMPLE:
5117 return dims == 2;
5118 case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
5119 return dims == 2 && !dsa;
5120
5121 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
5122 return dims == 3;
5123 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
5124 return dims == 3 && !dsa;
5125
5126 default:
5127 return GL_FALSE;
5128 }
5129 }
5130
5131
5132 void
5133 _mesa_texture_image_multisample(struct gl_context *ctx, GLuint dims,
5134 struct gl_texture_object *texObj,
5135 GLenum target, GLsizei samples,
5136 GLint internalformat, GLsizei width,
5137 GLsizei height, GLsizei depth,
5138 GLboolean fixedsamplelocations,
5139 GLboolean immutable, const char *func)
5140 {
5141 struct gl_texture_image *texImage;
5142 GLboolean sizeOK, dimensionsOK, samplesOK;
5143 mesa_format texFormat;
5144 GLenum sample_count_error;
5145 bool dsa = strstr(func, "ture") ? true : false;
5146
5147 if (!(ctx->Extensions.ARB_texture_multisample
5148 && _mesa_is_desktop_gl(ctx))) {
5149 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(unsupported)", func);
5150 return;
5151 }
5152
5153 if (!check_multisample_target(dims, target, dsa)) {
5154 if (dsa) {
5155 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(target)", func);
5156 return;
5157 }
5158 else {
5159 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", func);
5160 return;
5161 }
5162 }
5163
5164 /* check that the specified internalformat is color/depth/stencil-renderable;
5165 * refer GL3.1 spec 4.4.4
5166 */
5167
5168 if (immutable && !_mesa_is_legal_tex_storage_format(ctx, internalformat)) {
5169 _mesa_error(ctx, GL_INVALID_ENUM,
5170 "%s(internalformat=%s not legal for immutable-format)",
5171 func, _mesa_lookup_enum_by_nr(internalformat));
5172 return;
5173 }
5174
5175 if (!is_renderable_texture_format(ctx, internalformat)) {
5176 _mesa_error(ctx, GL_INVALID_OPERATION,
5177 "%s(internalformat=%s)",
5178 func, _mesa_lookup_enum_by_nr(internalformat));
5179 return;
5180 }
5181
5182 sample_count_error = _mesa_check_sample_count(ctx, target,
5183 internalformat, samples);
5184 samplesOK = sample_count_error == GL_NO_ERROR;
5185
5186 /* Page 254 of OpenGL 4.4 spec says:
5187 * "Proxy arrays for two-dimensional multisample and two-dimensional
5188 * multisample array textures are operated on in the same way when
5189 * TexImage2DMultisample is called with target specified as
5190 * PROXY_TEXTURE_2D_MULTISAMPLE, or TexImage3DMultisample is called
5191 * with target specified as PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY.
5192 * However, if samples is not supported, then no error is generated.
5193 */
5194 if (!samplesOK && !_mesa_is_proxy_texture(target)) {
5195 _mesa_error(ctx, sample_count_error, "%s(samples)", func);
5196 return;
5197 }
5198
5199 if (immutable && (!texObj || (texObj->Name == 0))) {
5200 _mesa_error(ctx, GL_INVALID_OPERATION,
5201 "%s(texture object 0)",
5202 func);
5203 return;
5204 }
5205
5206 texImage = _mesa_get_tex_image(ctx, texObj, 0, 0);
5207
5208 if (texImage == NULL) {
5209 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s()", func);
5210 return;
5211 }
5212
5213 texFormat = _mesa_choose_texture_format(ctx, texObj, target, 0,
5214 internalformat, GL_NONE, GL_NONE);
5215 assert(texFormat != MESA_FORMAT_NONE);
5216
5217 dimensionsOK = _mesa_legal_texture_dimensions(ctx, target, 0,
5218 width, height, depth, 0);
5219
5220 sizeOK = ctx->Driver.TestProxyTexImage(ctx, target, 0, texFormat,
5221 width, height, depth, 0);
5222
5223 if (_mesa_is_proxy_texture(target)) {
5224 if (samplesOK && dimensionsOK && sizeOK) {
5225 init_teximage_fields_ms(ctx, texImage, width, height, depth, 0,
5226 internalformat, texFormat,
5227 samples, fixedsamplelocations);
5228 }
5229 else {
5230 /* clear all image fields */
5231 clear_teximage_fields(texImage);
5232 }
5233 }
5234 else {
5235 if (!dimensionsOK) {
5236 _mesa_error(ctx, GL_INVALID_VALUE,
5237 "%s(invalid width or height)", func);
5238 return;
5239 }
5240
5241 if (!sizeOK) {
5242 _mesa_error(ctx, GL_OUT_OF_MEMORY,
5243 "%s(texture too large)", func);
5244 return;
5245 }
5246
5247 /* Check if texObj->Immutable is set */
5248 if (texObj->Immutable) {
5249 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(immutable)", func);
5250 return;
5251 }
5252
5253 ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
5254
5255 init_teximage_fields_ms(ctx, texImage, width, height, depth, 0,
5256 internalformat, texFormat,
5257 samples, fixedsamplelocations);
5258
5259 if (width > 0 && height > 0 && depth > 0) {
5260 if (!ctx->Driver.AllocTextureStorage(ctx, texObj, 1,
5261 width, height, depth)) {
5262 /* tidy up the texture image state. strictly speaking,
5263 * we're allowed to just leave this in whatever state we
5264 * like, but being tidy is good.
5265 */
5266 _mesa_init_teximage_fields(ctx, texImage,
5267 0, 0, 0, 0, GL_NONE, MESA_FORMAT_NONE);
5268 }
5269 }
5270
5271 texObj->Immutable |= immutable;
5272
5273 if (immutable) {
5274 _mesa_set_texture_view_state(ctx, texObj, target, 1);
5275 }
5276
5277 _mesa_update_fbo_texture(ctx, texObj, 0, 0);
5278 }
5279 }
5280
5281
5282 void GLAPIENTRY
5283 _mesa_TexImage2DMultisample(GLenum target, GLsizei samples,
5284 GLenum internalformat, GLsizei width,
5285 GLsizei height, GLboolean fixedsamplelocations)
5286 {
5287 struct gl_texture_object *texObj;
5288 GET_CURRENT_CONTEXT(ctx);
5289
5290 texObj = _mesa_get_current_tex_object(ctx, target);
5291 if (!texObj)
5292 return;
5293
5294 _mesa_texture_image_multisample(ctx, 2, texObj, target, samples,
5295 internalformat, width, height, 1,
5296 fixedsamplelocations, GL_FALSE,
5297 "glTexImage2DMultisample");
5298 }
5299
5300
5301 void GLAPIENTRY
5302 _mesa_TexImage3DMultisample(GLenum target, GLsizei samples,
5303 GLenum internalformat, GLsizei width,
5304 GLsizei height, GLsizei depth,
5305 GLboolean fixedsamplelocations)
5306 {
5307 struct gl_texture_object *texObj;
5308 GET_CURRENT_CONTEXT(ctx);
5309
5310 texObj = _mesa_get_current_tex_object(ctx, target);
5311 if (!texObj)
5312 return;
5313
5314 _mesa_texture_image_multisample(ctx, 3, texObj, target, samples,
5315 internalformat, width, height, depth,
5316 fixedsamplelocations, GL_FALSE,
5317 "glTexImage3DMultisample");
5318 }
5319
5320
5321 void GLAPIENTRY
5322 _mesa_TexStorage2DMultisample(GLenum target, GLsizei samples,
5323 GLenum internalformat, GLsizei width,
5324 GLsizei height, GLboolean fixedsamplelocations)
5325 {
5326 struct gl_texture_object *texObj;
5327 GET_CURRENT_CONTEXT(ctx);
5328
5329 texObj = _mesa_get_current_tex_object(ctx, target);
5330 if (!texObj)
5331 return;
5332
5333 _mesa_texture_image_multisample(ctx, 2, texObj, target, samples,
5334 internalformat, width, height, 1,
5335 fixedsamplelocations, GL_TRUE,
5336 "glTexStorage2DMultisample");
5337 }
5338
5339 void GLAPIENTRY
5340 _mesa_TexStorage3DMultisample(GLenum target, GLsizei samples,
5341 GLenum internalformat, GLsizei width,
5342 GLsizei height, GLsizei depth,
5343 GLboolean fixedsamplelocations)
5344 {
5345 struct gl_texture_object *texObj;
5346 GET_CURRENT_CONTEXT(ctx);
5347
5348 texObj = _mesa_get_current_tex_object(ctx, target);
5349 if (!texObj)
5350 return;
5351
5352 _mesa_texture_image_multisample(ctx, 3, texObj, target, samples,
5353 internalformat, width, height, depth,
5354 fixedsamplelocations, GL_TRUE,
5355 "glTexStorage3DMultisample");
5356 }
5357
5358 void GLAPIENTRY
5359 _mesa_TextureStorage2DMultisample(GLuint texture, GLsizei samples,
5360 GLenum internalformat, GLsizei width,
5361 GLsizei height,
5362 GLboolean fixedsamplelocations)
5363 {
5364 struct gl_texture_object *texObj;
5365 GET_CURRENT_CONTEXT(ctx);
5366
5367 texObj = _mesa_lookup_texture_err(ctx, texture,
5368 "glTextureStorage2DMultisample");
5369 if (!texObj)
5370 return;
5371
5372 _mesa_texture_image_multisample(ctx, 2, texObj, texObj->Target, samples,
5373 internalformat, width, height, 1,
5374 fixedsamplelocations, GL_TRUE,
5375 "glTextureStorage2DMultisample");
5376 }
5377
5378 void GLAPIENTRY
5379 _mesa_TextureStorage3DMultisample(GLuint texture, GLsizei samples,
5380 GLenum internalformat, GLsizei width,
5381 GLsizei height, GLsizei depth,
5382 GLboolean fixedsamplelocations)
5383 {
5384 struct gl_texture_object *texObj;
5385 GET_CURRENT_CONTEXT(ctx);
5386
5387 /* Get the texture object by Name. */
5388 texObj = _mesa_lookup_texture_err(ctx, texture,
5389 "glTextureStorage3DMultisample");
5390 if (!texObj)
5391 return;
5392
5393 _mesa_texture_image_multisample(ctx, 3, texObj, texObj->Target, samples,
5394 internalformat, width, height, depth,
5395 fixedsamplelocations, GL_TRUE,
5396 "glTextureStorage3DMultisample");
5397 }