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