mesa: Allow GL_TEXTURE_CUBE_MAP target with compressed internal formats
[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 GLboolean
1774 _mesa_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:
1785 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1786 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1787 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1788 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1789 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1790 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1791 return ctx->Extensions.ARB_texture_cube_map;
1792 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
1793 case GL_TEXTURE_2D_ARRAY_EXT:
1794 return ctx->Extensions.EXT_texture_array;
1795 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
1796 case GL_TEXTURE_CUBE_MAP_ARRAY:
1797 return ctx->Extensions.ARB_texture_cube_map_array;
1798 default:
1799 return GL_FALSE;
1800 }
1801 }
1802
1803
1804 /**
1805 * Check if the given texture target value is legal for a
1806 * glTexImage1/2/3D call.
1807 */
1808 static GLboolean
1809 legal_teximage_target(struct gl_context *ctx, GLuint dims, GLenum target)
1810 {
1811 switch (dims) {
1812 case 1:
1813 switch (target) {
1814 case GL_TEXTURE_1D:
1815 case GL_PROXY_TEXTURE_1D:
1816 return _mesa_is_desktop_gl(ctx);
1817 default:
1818 return GL_FALSE;
1819 }
1820 case 2:
1821 switch (target) {
1822 case GL_TEXTURE_2D:
1823 return GL_TRUE;
1824 case GL_PROXY_TEXTURE_2D:
1825 return _mesa_is_desktop_gl(ctx);
1826 case GL_PROXY_TEXTURE_CUBE_MAP:
1827 return _mesa_is_desktop_gl(ctx)
1828 && ctx->Extensions.ARB_texture_cube_map;
1829 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1830 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1831 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1832 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1833 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1834 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1835 return ctx->Extensions.ARB_texture_cube_map;
1836 case GL_TEXTURE_RECTANGLE_NV:
1837 case GL_PROXY_TEXTURE_RECTANGLE_NV:
1838 return _mesa_is_desktop_gl(ctx)
1839 && ctx->Extensions.NV_texture_rectangle;
1840 case GL_TEXTURE_1D_ARRAY_EXT:
1841 case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
1842 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array;
1843 default:
1844 return GL_FALSE;
1845 }
1846 case 3:
1847 switch (target) {
1848 case GL_TEXTURE_3D:
1849 return GL_TRUE;
1850 case GL_PROXY_TEXTURE_3D:
1851 return _mesa_is_desktop_gl(ctx);
1852 case GL_TEXTURE_2D_ARRAY_EXT:
1853 return (_mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array)
1854 || _mesa_is_gles3(ctx);
1855 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
1856 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array;
1857 case GL_TEXTURE_CUBE_MAP_ARRAY:
1858 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
1859 return ctx->Extensions.ARB_texture_cube_map_array;
1860 default:
1861 return GL_FALSE;
1862 }
1863 default:
1864 _mesa_problem(ctx, "invalid dims=%u in legal_teximage_target()", dims);
1865 return GL_FALSE;
1866 }
1867 }
1868
1869
1870 /**
1871 * Check if the given texture target value is legal for a
1872 * glTexSubImage, glCopyTexSubImage or glCopyTexImage call.
1873 * The difference compared to legal_teximage_target() above is that
1874 * proxy targets are not supported.
1875 */
1876 static GLboolean
1877 legal_texsubimage_target(struct gl_context *ctx, GLuint dims, GLenum target)
1878 {
1879 switch (dims) {
1880 case 1:
1881 return _mesa_is_desktop_gl(ctx) && target == GL_TEXTURE_1D;
1882 case 2:
1883 switch (target) {
1884 case GL_TEXTURE_2D:
1885 return GL_TRUE;
1886 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1887 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1888 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1889 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1890 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1891 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1892 return ctx->Extensions.ARB_texture_cube_map;
1893 case GL_TEXTURE_RECTANGLE_NV:
1894 return _mesa_is_desktop_gl(ctx)
1895 && ctx->Extensions.NV_texture_rectangle;
1896 case GL_TEXTURE_1D_ARRAY_EXT:
1897 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array;
1898 default:
1899 return GL_FALSE;
1900 }
1901 case 3:
1902 switch (target) {
1903 case GL_TEXTURE_3D:
1904 return GL_TRUE;
1905 case GL_TEXTURE_2D_ARRAY_EXT:
1906 return (_mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array)
1907 || _mesa_is_gles3(ctx);
1908 case GL_TEXTURE_CUBE_MAP_ARRAY:
1909 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
1910 return ctx->Extensions.ARB_texture_cube_map_array;
1911 default:
1912 return GL_FALSE;
1913 }
1914 default:
1915 _mesa_problem(ctx, "invalid dims=%u in legal_texsubimage_target()",
1916 dims);
1917 return GL_FALSE;
1918 }
1919 }
1920
1921
1922 /**
1923 * Helper function to determine if a texture object is mutable (in terms
1924 * of GL_ARB_texture_storage).
1925 */
1926 static GLboolean
1927 mutable_tex_object(struct gl_context *ctx, GLenum target)
1928 {
1929 struct gl_texture_object *texObj = _mesa_get_current_tex_object(ctx, target);
1930 return !texObj->Immutable;
1931 }
1932
1933
1934 /**
1935 * Return expected size of a compressed texture.
1936 */
1937 static GLuint
1938 compressed_tex_size(GLsizei width, GLsizei height, GLsizei depth,
1939 GLenum glformat)
1940 {
1941 mesa_format mesaFormat = _mesa_glenum_to_compressed_format(glformat);
1942 return _mesa_format_image_size(mesaFormat, width, height, depth);
1943 }
1944
1945 /**
1946 * Verify that a texture format is valid with a particular target
1947 *
1948 * In particular, textures with base format of \c GL_DEPTH_COMPONENT or
1949 * \c GL_DEPTH_STENCIL are only valid with certain, context dependent texture
1950 * targets.
1951 *
1952 * \param ctx GL context
1953 * \param target Texture target
1954 * \param internalFormat Internal format of the texture image
1955 * \param dimensions Dimensionality at the caller. This is \b not used
1956 * in the validation. It is only used when logging
1957 * error messages.
1958 * \param caller Base name of the calling function (e.g.,
1959 * "glTexImage" or "glTexStorage").
1960 *
1961 * \returns true if the combination is legal, false otherwise.
1962 */
1963 bool
1964 _mesa_legal_texture_base_format_for_target(struct gl_context *ctx,
1965 GLenum target, GLenum internalFormat,
1966 unsigned dimensions,
1967 const char *caller)
1968 {
1969 if (_mesa_base_tex_format(ctx, internalFormat) == GL_DEPTH_COMPONENT
1970 || _mesa_base_tex_format(ctx, internalFormat) == GL_DEPTH_STENCIL) {
1971 /* Section 3.8.3 (Texture Image Specification) of the OpenGL 3.3 Core
1972 * Profile spec says:
1973 *
1974 * "Textures with a base internal format of DEPTH_COMPONENT or
1975 * DEPTH_STENCIL are supported by texture image specification
1976 * commands only if target is TEXTURE_1D, TEXTURE_2D,
1977 * TEXTURE_1D_ARRAY, TEXTURE_2D_ARRAY, TEXTURE_RECTANGLE,
1978 * TEXTURE_CUBE_MAP, PROXY_TEXTURE_1D, PROXY_TEXTURE_2D,
1979 * PROXY_TEXTURE_1D_ARRAY, PROXY_TEXTURE_2D_ARRAY,
1980 * PROXY_TEXTURE_RECTANGLE, or PROXY_TEXTURE_CUBE_MAP. Using these
1981 * formats in conjunction with any other target will result in an
1982 * INVALID_OPERATION error."
1983 *
1984 * Cubemaps are only supported with desktop OpenGL version >= 3.0,
1985 * EXT_gpu_shader4, or, on OpenGL ES 2.0+, OES_depth_texture_cube_map.
1986 */
1987 if (target != GL_TEXTURE_1D &&
1988 target != GL_PROXY_TEXTURE_1D &&
1989 target != GL_TEXTURE_2D &&
1990 target != GL_PROXY_TEXTURE_2D &&
1991 target != GL_TEXTURE_1D_ARRAY &&
1992 target != GL_PROXY_TEXTURE_1D_ARRAY &&
1993 target != GL_TEXTURE_2D_ARRAY &&
1994 target != GL_PROXY_TEXTURE_2D_ARRAY &&
1995 target != GL_TEXTURE_RECTANGLE_ARB &&
1996 target != GL_PROXY_TEXTURE_RECTANGLE_ARB &&
1997 !((_mesa_is_cube_face(target) ||
1998 target == GL_TEXTURE_CUBE_MAP ||
1999 target == GL_PROXY_TEXTURE_CUBE_MAP) &&
2000 (ctx->Version >= 30 || ctx->Extensions.EXT_gpu_shader4
2001 || (ctx->API == API_OPENGLES2 && ctx->Extensions.OES_depth_texture_cube_map))) &&
2002 !((target == GL_TEXTURE_CUBE_MAP_ARRAY ||
2003 target == GL_PROXY_TEXTURE_CUBE_MAP_ARRAY) &&
2004 ctx->Extensions.ARB_texture_cube_map_array)) {
2005 _mesa_error(ctx, GL_INVALID_OPERATION,
2006 "%s%dD(bad target for depth texture)",
2007 caller, dimensions);
2008 return false;
2009 }
2010 }
2011
2012 return true;
2013 }
2014
2015 static bool
2016 texture_formats_agree(GLenum internalFormat,
2017 GLenum format)
2018 {
2019 GLboolean colorFormat;
2020 GLboolean is_format_depth_or_depthstencil;
2021 GLboolean is_internalFormat_depth_or_depthstencil;
2022
2023 /* Even though there are no color-index textures, we still have to support
2024 * uploading color-index data and remapping it to RGB via the
2025 * GL_PIXEL_MAP_I_TO_[RGBA] tables.
2026 */
2027 const GLboolean indexFormat = (format == GL_COLOR_INDEX);
2028
2029 is_internalFormat_depth_or_depthstencil =
2030 _mesa_is_depth_format(internalFormat) ||
2031 _mesa_is_depthstencil_format(internalFormat);
2032
2033 is_format_depth_or_depthstencil =
2034 _mesa_is_depth_format(format) ||
2035 _mesa_is_depthstencil_format(format);
2036
2037 colorFormat = _mesa_is_color_format(format);
2038
2039 if (_mesa_is_color_format(internalFormat) && !colorFormat && !indexFormat)
2040 return false;
2041
2042 if (is_internalFormat_depth_or_depthstencil !=
2043 is_format_depth_or_depthstencil)
2044 return false;
2045
2046 if (_mesa_is_ycbcr_format(internalFormat) != _mesa_is_ycbcr_format(format))
2047 return false;
2048
2049 return true;
2050 }
2051
2052 /**
2053 * Test the glTexImage[123]D() parameters for errors.
2054 *
2055 * \param ctx GL context.
2056 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2057 * \param target texture target given by the user (already validated).
2058 * \param level image level given by the user.
2059 * \param internalFormat internal format given by the user.
2060 * \param format pixel data format given by the user.
2061 * \param type pixel data type given by the user.
2062 * \param width image width given by the user.
2063 * \param height image height given by the user.
2064 * \param depth image depth given by the user.
2065 * \param border image border given by the user.
2066 *
2067 * \return GL_TRUE if a error is found, GL_FALSE otherwise
2068 *
2069 * Verifies each of the parameters against the constants specified in
2070 * __struct gl_contextRec::Const and the supported extensions, and according
2071 * to the OpenGL specification.
2072 * Note that we don't fully error-check the width, height, depth values
2073 * here. That's done in _mesa_legal_texture_dimensions() which is used
2074 * by several other GL entrypoints. Plus, texture dims have a special
2075 * interaction with proxy textures.
2076 */
2077 static GLboolean
2078 texture_error_check( struct gl_context *ctx,
2079 GLuint dimensions, GLenum target,
2080 GLint level, GLint internalFormat,
2081 GLenum format, GLenum type,
2082 GLint width, GLint height,
2083 GLint depth, GLint border )
2084 {
2085 GLenum err;
2086
2087 /* Note: for proxy textures, some error conditions immediately generate
2088 * a GL error in the usual way. But others do not generate a GL error.
2089 * Instead, they cause the width, height, depth, format fields of the
2090 * texture image to be zeroed-out. The GL spec seems to indicate that the
2091 * zero-out behaviour is only used in cases related to memory allocation.
2092 */
2093
2094 /* level check */
2095 if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
2096 _mesa_error(ctx, GL_INVALID_VALUE,
2097 "glTexImage%dD(level=%d)", dimensions, level);
2098 return GL_TRUE;
2099 }
2100
2101 /* Check border */
2102 if (border < 0 || border > 1 ||
2103 ((ctx->API != API_OPENGL_COMPAT ||
2104 target == GL_TEXTURE_RECTANGLE_NV ||
2105 target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
2106 _mesa_error(ctx, GL_INVALID_VALUE,
2107 "glTexImage%dD(border=%d)", dimensions, border);
2108 return GL_TRUE;
2109 }
2110
2111 if (width < 0 || height < 0 || depth < 0) {
2112 _mesa_error(ctx, GL_INVALID_VALUE,
2113 "glTexImage%dD(width, height or depth < 0)", dimensions);
2114 return GL_TRUE;
2115 }
2116
2117 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2118 * combinations of format, internalFormat, and type that can be used.
2119 * Formats and types that require additional extensions (e.g., GL_FLOAT
2120 * requires GL_OES_texture_float) are filtered elsewhere.
2121 */
2122
2123 if (_mesa_is_gles(ctx)) {
2124 if (_mesa_is_gles3(ctx)) {
2125 err = _mesa_es3_error_check_format_and_type(format, type,
2126 internalFormat);
2127 } else {
2128 if (format != internalFormat) {
2129 _mesa_error(ctx, GL_INVALID_OPERATION,
2130 "glTexImage%dD(format = %s, internalFormat = %s)",
2131 dimensions,
2132 _mesa_lookup_enum_by_nr(format),
2133 _mesa_lookup_enum_by_nr(internalFormat));
2134 return GL_TRUE;
2135 }
2136
2137 err = _mesa_es_error_check_format_and_type(format, type, dimensions);
2138 }
2139 if (err != GL_NO_ERROR) {
2140 _mesa_error(ctx, err,
2141 "glTexImage%dD(format = %s, type = %s, internalFormat = %s)",
2142 dimensions,
2143 _mesa_lookup_enum_by_nr(format),
2144 _mesa_lookup_enum_by_nr(type),
2145 _mesa_lookup_enum_by_nr(internalFormat));
2146 return GL_TRUE;
2147 }
2148 }
2149
2150 /* Check internalFormat */
2151 if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
2152 _mesa_error(ctx, GL_INVALID_VALUE,
2153 "glTexImage%dD(internalFormat=%s)",
2154 dimensions, _mesa_lookup_enum_by_nr(internalFormat));
2155 return GL_TRUE;
2156 }
2157
2158 /* Check incoming image format and type */
2159 err = _mesa_error_check_format_and_type(ctx, format, type);
2160 if (err != GL_NO_ERROR) {
2161 _mesa_error(ctx, err,
2162 "glTexImage%dD(incompatible format = %s, type = %s)",
2163 dimensions, _mesa_lookup_enum_by_nr(format),
2164 _mesa_lookup_enum_by_nr(type));
2165 return GL_TRUE;
2166 }
2167
2168 /* make sure internal format and format basically agree */
2169 if (!texture_formats_agree(internalFormat, format)) {
2170 _mesa_error(ctx, GL_INVALID_OPERATION,
2171 "glTexImage%dD(incompatible internalFormat = %s, format = %s)",
2172 dimensions, _mesa_lookup_enum_by_nr(internalFormat),
2173 _mesa_lookup_enum_by_nr(format));
2174 return GL_TRUE;
2175 }
2176
2177 /* additional checks for ycbcr textures */
2178 if (internalFormat == GL_YCBCR_MESA) {
2179 ASSERT(ctx->Extensions.MESA_ycbcr_texture);
2180 if (type != GL_UNSIGNED_SHORT_8_8_MESA &&
2181 type != GL_UNSIGNED_SHORT_8_8_REV_MESA) {
2182 char message[100];
2183 _mesa_snprintf(message, sizeof(message),
2184 "glTexImage%dD(format/type YCBCR mismatch)",
2185 dimensions);
2186 _mesa_error(ctx, GL_INVALID_ENUM, "%s", message);
2187 return GL_TRUE; /* error */
2188 }
2189 if (target != GL_TEXTURE_2D &&
2190 target != GL_PROXY_TEXTURE_2D &&
2191 target != GL_TEXTURE_RECTANGLE_NV &&
2192 target != GL_PROXY_TEXTURE_RECTANGLE_NV) {
2193 _mesa_error(ctx, GL_INVALID_ENUM,
2194 "glTexImage%dD(bad target for YCbCr texture)",
2195 dimensions);
2196 return GL_TRUE;
2197 }
2198 if (border != 0) {
2199 char message[100];
2200 _mesa_snprintf(message, sizeof(message),
2201 "glTexImage%dD(format=GL_YCBCR_MESA and border=%d)",
2202 dimensions, border);
2203 _mesa_error(ctx, GL_INVALID_VALUE, "%s", message);
2204 return GL_TRUE;
2205 }
2206 }
2207
2208 /* additional checks for depth textures */
2209 if (!_mesa_legal_texture_base_format_for_target(ctx, target, internalFormat,
2210 dimensions, "glTexImage"))
2211 return GL_TRUE;
2212
2213 /* additional checks for compressed textures */
2214 if (_mesa_is_compressed_format(ctx, internalFormat)) {
2215 if (!_mesa_target_can_be_compressed(ctx, target, internalFormat)) {
2216 _mesa_error(ctx, GL_INVALID_ENUM,
2217 "glTexImage%dD(target can't be compressed)", dimensions);
2218 return GL_TRUE;
2219 }
2220 if (compressedteximage_only_format(ctx, internalFormat)) {
2221 _mesa_error(ctx, GL_INVALID_OPERATION,
2222 "glTexImage%dD(no compression for format)", dimensions);
2223 return GL_TRUE;
2224 }
2225 if (border != 0) {
2226 _mesa_error(ctx, GL_INVALID_OPERATION,
2227 "glTexImage%dD(border!=0)", dimensions);
2228 return GL_TRUE;
2229 }
2230 }
2231
2232 /* additional checks for integer textures */
2233 if ((ctx->Version >= 30 || ctx->Extensions.EXT_texture_integer) &&
2234 (_mesa_is_enum_format_integer(format) !=
2235 _mesa_is_enum_format_integer(internalFormat))) {
2236 _mesa_error(ctx, GL_INVALID_OPERATION,
2237 "glTexImage%dD(integer/non-integer format mismatch)",
2238 dimensions);
2239 return GL_TRUE;
2240 }
2241
2242 if (!mutable_tex_object(ctx, target)) {
2243 _mesa_error(ctx, GL_INVALID_OPERATION,
2244 "glTexImage%dD(immutable texture)", dimensions);
2245 return GL_TRUE;
2246 }
2247
2248 /* if we get here, the parameters are OK */
2249 return GL_FALSE;
2250 }
2251
2252
2253 bool
2254 _mesa_compressed_texture_pixel_storage_error_check(struct gl_context *ctx,
2255 GLint dimensions,
2256 struct gl_pixelstore_attrib *packing,
2257 const char *caller)
2258 {
2259 if (!_mesa_is_desktop_gl(ctx) || !packing->CompressedBlockSize)
2260 return true;
2261
2262 if (packing->CompressedBlockWidth && packing->SkipPixels % packing->CompressedBlockWidth) {
2263 _mesa_error(ctx, GL_INVALID_OPERATION,
2264 "%s(skip-pixels %% block-width)", caller);
2265 return false;
2266 }
2267
2268 if (dimensions > 1 && packing->CompressedBlockHeight && packing->SkipRows % packing->CompressedBlockHeight) {
2269 _mesa_error(ctx, GL_INVALID_OPERATION,
2270 "%s(skip-rows %% block-height)", caller);
2271 return false;
2272 }
2273
2274 if (dimensions > 2 && packing->CompressedBlockDepth && packing->SkipImages % packing->CompressedBlockDepth) {
2275 _mesa_error(ctx, GL_INVALID_OPERATION,
2276 "%s(skip-images %% block-depth)", caller);
2277 return false;
2278 }
2279
2280 return true;
2281 }
2282
2283 /**
2284 * Error checking for glCompressedTexImage[123]D().
2285 * Note that the width, height and depth values are not fully error checked
2286 * here.
2287 * \return GL_TRUE if a error is found, GL_FALSE otherwise
2288 */
2289 static GLenum
2290 compressed_texture_error_check(struct gl_context *ctx, GLint dimensions,
2291 GLenum target, GLint level,
2292 GLenum internalFormat, GLsizei width,
2293 GLsizei height, GLsizei depth, GLint border,
2294 GLsizei imageSize)
2295 {
2296 const GLint maxLevels = _mesa_max_texture_levels(ctx, target);
2297 GLint expectedSize;
2298 GLenum error = GL_NO_ERROR;
2299 char *reason = ""; /* no error */
2300
2301 if (!_mesa_target_can_be_compressed(ctx, target, internalFormat)) {
2302 reason = "target";
2303 /* From section 3.8.6, page 146 of OpenGL ES 3.0 spec:
2304 *
2305 * "The ETC2/EAC texture compression algorithm supports only
2306 * two-dimensional images. If internalformat is an ETC2/EAC format,
2307 * CompressedTexImage3D will generate an INVALID_OPERATION error if
2308 * target is not TEXTURE_2D_ARRAY."
2309 */
2310 error = _mesa_is_desktop_gl(ctx) ? GL_INVALID_ENUM : GL_INVALID_OPERATION;
2311 goto error;
2312 }
2313
2314 /* This will detect any invalid internalFormat value */
2315 if (!_mesa_is_compressed_format(ctx, internalFormat)) {
2316 _mesa_error(ctx, GL_INVALID_ENUM,
2317 "glCompressedTexImage%dD(internalFormat=%s)",
2318 dimensions, _mesa_lookup_enum_by_nr(internalFormat));
2319 return GL_TRUE;
2320 }
2321
2322 switch (internalFormat) {
2323 case GL_PALETTE4_RGB8_OES:
2324 case GL_PALETTE4_RGBA8_OES:
2325 case GL_PALETTE4_R5_G6_B5_OES:
2326 case GL_PALETTE4_RGBA4_OES:
2327 case GL_PALETTE4_RGB5_A1_OES:
2328 case GL_PALETTE8_RGB8_OES:
2329 case GL_PALETTE8_RGBA8_OES:
2330 case GL_PALETTE8_R5_G6_B5_OES:
2331 case GL_PALETTE8_RGBA4_OES:
2332 case GL_PALETTE8_RGB5_A1_OES:
2333 /* check level (note that level should be zero or less!) */
2334 if (level > 0 || level < -maxLevels) {
2335 reason = "level";
2336 error = GL_INVALID_VALUE;
2337 goto error;
2338 }
2339
2340 if (dimensions != 2) {
2341 reason = "compressed paletted textures must be 2D";
2342 error = GL_INVALID_OPERATION;
2343 goto error;
2344 }
2345
2346 /* Figure out the expected texture size (in bytes). This will be
2347 * checked against the actual size later.
2348 */
2349 expectedSize = _mesa_cpal_compressed_size(level, internalFormat,
2350 width, height);
2351
2352 /* This is for the benefit of the TestProxyTexImage below. It expects
2353 * level to be non-negative. OES_compressed_paletted_texture uses a
2354 * weird mechanism where the level specified to glCompressedTexImage2D
2355 * is -(n-1) number of levels in the texture, and the data specifies the
2356 * complete mipmap stack. This is done to ensure the palette is the
2357 * same for all levels.
2358 */
2359 level = -level;
2360 break;
2361
2362 default:
2363 /* check level */
2364 if (level < 0 || level >= maxLevels) {
2365 reason = "level";
2366 error = GL_INVALID_VALUE;
2367 goto error;
2368 }
2369
2370 /* Figure out the expected texture size (in bytes). This will be
2371 * checked against the actual size later.
2372 */
2373 expectedSize = compressed_tex_size(width, height, depth, internalFormat);
2374 break;
2375 }
2376
2377 /* This should really never fail */
2378 if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
2379 reason = "internalFormat";
2380 error = GL_INVALID_ENUM;
2381 goto error;
2382 }
2383
2384 /* No compressed formats support borders at this time */
2385 if (border != 0) {
2386 reason = "border != 0";
2387 error = GL_INVALID_VALUE;
2388 goto error;
2389 }
2390
2391 /* Check for invalid pixel storage modes */
2392 if (!_mesa_compressed_texture_pixel_storage_error_check(ctx, dimensions,
2393 &ctx->Unpack,
2394 "glCompressedTexImage")) {
2395 return GL_FALSE;
2396 }
2397
2398 /* check image size in bytes */
2399 if (expectedSize != imageSize) {
2400 /* Per GL_ARB_texture_compression: GL_INVALID_VALUE is generated [...]
2401 * if <imageSize> is not consistent with the format, dimensions, and
2402 * contents of the specified image.
2403 */
2404 reason = "imageSize inconsistant with width/height/format";
2405 error = GL_INVALID_VALUE;
2406 goto error;
2407 }
2408
2409 if (!mutable_tex_object(ctx, target)) {
2410 reason = "immutable texture";
2411 error = GL_INVALID_OPERATION;
2412 goto error;
2413 }
2414
2415 return GL_FALSE;
2416
2417 error:
2418 /* Note: not all error paths exit through here. */
2419 _mesa_error(ctx, error, "glCompressedTexImage%dD(%s)", dimensions, reason);
2420 return GL_TRUE;
2421 }
2422
2423
2424
2425 /**
2426 * Test glTexSubImage[123]D() parameters for errors.
2427 *
2428 * \param ctx GL context.
2429 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2430 * \param target texture target given by the user (already validated)
2431 * \param level image level given by the user.
2432 * \param xoffset sub-image x offset given by the user.
2433 * \param yoffset sub-image y offset given by the user.
2434 * \param zoffset sub-image z offset given by the user.
2435 * \param format pixel data format given by the user.
2436 * \param type pixel data type given by the user.
2437 * \param width image width given by the user.
2438 * \param height image height given by the user.
2439 * \param depth image depth given by the user.
2440 *
2441 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2442 *
2443 * Verifies each of the parameters against the constants specified in
2444 * __struct gl_contextRec::Const and the supported extensions, and according
2445 * to the OpenGL specification.
2446 */
2447 static GLboolean
2448 texsubimage_error_check(struct gl_context *ctx, GLuint dimensions,
2449 GLenum target, GLint level,
2450 GLint xoffset, GLint yoffset, GLint zoffset,
2451 GLint width, GLint height, GLint depth,
2452 GLenum format, GLenum type)
2453 {
2454 struct gl_texture_object *texObj;
2455 struct gl_texture_image *texImage;
2456 GLenum err;
2457
2458 /* check target (proxies not allowed) */
2459 if (!legal_texsubimage_target(ctx, dimensions, target)) {
2460 _mesa_error(ctx, GL_INVALID_ENUM, "glTexSubImage%uD(target=%s)",
2461 dimensions, _mesa_lookup_enum_by_nr(target));
2462 return GL_TRUE;
2463 }
2464
2465 /* level check */
2466 if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
2467 _mesa_error(ctx, GL_INVALID_ENUM, "glTexSubImage%uD(level=%d)",
2468 dimensions, level);
2469 return GL_TRUE;
2470 }
2471
2472 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2473 * combinations of format and type that can be used. Formats and types
2474 * that require additional extensions (e.g., GL_FLOAT requires
2475 * GL_OES_texture_float) are filtered elsewhere.
2476 */
2477 if (_mesa_is_gles(ctx) && !_mesa_is_gles3(ctx)) {
2478 err = _mesa_es_error_check_format_and_type(format, type, dimensions);
2479 if (err != GL_NO_ERROR) {
2480 _mesa_error(ctx, err,
2481 "glTexSubImage%dD(format = %s, type = %s)",
2482 dimensions,
2483 _mesa_lookup_enum_by_nr(format),
2484 _mesa_lookup_enum_by_nr(type));
2485 return GL_TRUE;
2486 }
2487 }
2488
2489 err = _mesa_error_check_format_and_type(ctx, format, type);
2490 if (err != GL_NO_ERROR) {
2491 _mesa_error(ctx, err,
2492 "glTexSubImage%dD(incompatible format = %s, type = %s)",
2493 dimensions, _mesa_lookup_enum_by_nr(format),
2494 _mesa_lookup_enum_by_nr(type));
2495 return GL_TRUE;
2496 }
2497
2498 /* Get dest texture object / image pointers */
2499 texObj = _mesa_get_current_tex_object(ctx, target);
2500 if (!texObj) {
2501 /* must be out of memory */
2502 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage%dD()", dimensions);
2503 return GL_TRUE;
2504 }
2505
2506 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
2507 if (!texImage) {
2508 /* non-existant texture level */
2509 _mesa_error(ctx, GL_INVALID_OPERATION,
2510 "glTexSubImage%dD(invalid texture image)", dimensions);
2511 return GL_TRUE;
2512 }
2513
2514 if (error_check_subtexture_dimensions(ctx, "glTexSubImage", dimensions,
2515 texImage, xoffset, yoffset, 0,
2516 width, height, 1)) {
2517 return GL_TRUE;
2518 }
2519
2520 if (_mesa_is_format_compressed(texImage->TexFormat)) {
2521 if (compressedteximage_only_format(ctx, texImage->InternalFormat)) {
2522 _mesa_error(ctx, GL_INVALID_OPERATION,
2523 "glTexSubImage%dD(no compression for format)", dimensions);
2524 return GL_TRUE;
2525 }
2526 }
2527
2528 if (ctx->Version >= 30 || ctx->Extensions.EXT_texture_integer) {
2529 /* both source and dest must be integer-valued, or neither */
2530 if (_mesa_is_format_integer_color(texImage->TexFormat) !=
2531 _mesa_is_enum_format_integer(format)) {
2532 _mesa_error(ctx, GL_INVALID_OPERATION,
2533 "glTexSubImage%dD(integer/non-integer format mismatch)",
2534 dimensions);
2535 return GL_TRUE;
2536 }
2537 }
2538
2539 return GL_FALSE;
2540 }
2541
2542
2543 /**
2544 * Test glCopyTexImage[12]D() parameters for errors.
2545 *
2546 * \param ctx GL context.
2547 * \param dimensions texture image dimensions (must be 1, 2 or 3).
2548 * \param target texture target given by the user.
2549 * \param level image level given by the user.
2550 * \param internalFormat internal format given by the user.
2551 * \param width image width given by the user.
2552 * \param height image height given by the user.
2553 * \param border texture border.
2554 *
2555 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2556 *
2557 * Verifies each of the parameters against the constants specified in
2558 * __struct gl_contextRec::Const and the supported extensions, and according
2559 * to the OpenGL specification.
2560 */
2561 static GLboolean
2562 copytexture_error_check( struct gl_context *ctx, GLuint dimensions,
2563 GLenum target, GLint level, GLint internalFormat,
2564 GLint width, GLint height, GLint border )
2565 {
2566 GLint baseFormat;
2567 GLint rb_base_format;
2568 struct gl_renderbuffer *rb;
2569 GLenum rb_internal_format;
2570
2571 /* check target */
2572 if (!legal_texsubimage_target(ctx, dimensions, target)) {
2573 _mesa_error(ctx, GL_INVALID_ENUM, "glCopyTexImage%uD(target=%s)",
2574 dimensions, _mesa_lookup_enum_by_nr(target));
2575 return GL_TRUE;
2576 }
2577
2578 /* level check */
2579 if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
2580 _mesa_error(ctx, GL_INVALID_VALUE,
2581 "glCopyTexImage%dD(level=%d)", dimensions, level);
2582 return GL_TRUE;
2583 }
2584
2585 /* Check that the source buffer is complete */
2586 if (_mesa_is_user_fbo(ctx->ReadBuffer)) {
2587 if (ctx->ReadBuffer->_Status == 0) {
2588 _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
2589 }
2590 if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2591 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2592 "glCopyTexImage%dD(invalid readbuffer)", dimensions);
2593 return GL_TRUE;
2594 }
2595
2596 if (ctx->ReadBuffer->Visual.samples > 0) {
2597 _mesa_error(ctx, GL_INVALID_OPERATION,
2598 "glCopyTexImage%dD(multisample FBO)",
2599 dimensions);
2600 return GL_TRUE;
2601 }
2602 }
2603
2604 /* Check border */
2605 if (border < 0 || border > 1 ||
2606 ((ctx->API != API_OPENGL_COMPAT ||
2607 target == GL_TEXTURE_RECTANGLE_NV ||
2608 target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
2609 _mesa_error(ctx, GL_INVALID_VALUE,
2610 "glCopyTexImage%dD(border=%d)", dimensions, border);
2611 return GL_TRUE;
2612 }
2613
2614 rb = _mesa_get_read_renderbuffer_for_format(ctx, internalFormat);
2615 if (rb == NULL) {
2616 _mesa_error(ctx, GL_INVALID_OPERATION,
2617 "glCopyTexImage%dD(read buffer)", dimensions);
2618 return GL_TRUE;
2619 }
2620
2621 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
2622 * internalFormat.
2623 */
2624 if (_mesa_is_gles(ctx) && !_mesa_is_gles3(ctx)) {
2625 switch (internalFormat) {
2626 case GL_ALPHA:
2627 case GL_RGB:
2628 case GL_RGBA:
2629 case GL_LUMINANCE:
2630 case GL_LUMINANCE_ALPHA:
2631 break;
2632 default:
2633 _mesa_error(ctx, GL_INVALID_VALUE,
2634 "glCopyTexImage%dD(internalFormat=%s)", dimensions,
2635 _mesa_lookup_enum_by_nr(internalFormat));
2636 return GL_TRUE;
2637 }
2638 }
2639
2640 baseFormat = _mesa_base_tex_format(ctx, internalFormat);
2641 if (baseFormat < 0) {
2642 _mesa_error(ctx, GL_INVALID_OPERATION,
2643 "glCopyTexImage%dD(internalFormat=%s)", dimensions,
2644 _mesa_lookup_enum_by_nr(internalFormat));
2645 return GL_TRUE;
2646 }
2647
2648 rb_internal_format = rb->InternalFormat;
2649 rb_base_format = _mesa_base_tex_format(ctx, rb->InternalFormat);
2650 if (_mesa_is_color_format(internalFormat)) {
2651 if (rb_base_format < 0) {
2652 _mesa_error(ctx, GL_INVALID_VALUE,
2653 "glCopyTexImage%dD(internalFormat=%s)", dimensions,
2654 _mesa_lookup_enum_by_nr(internalFormat));
2655 return GL_TRUE;
2656 }
2657 }
2658
2659 if (_mesa_is_gles(ctx)) {
2660 bool valid = true;
2661 if (_mesa_base_format_component_count(baseFormat) >
2662 _mesa_base_format_component_count(rb_base_format)) {
2663 valid = false;
2664 }
2665 if (baseFormat == GL_DEPTH_COMPONENT ||
2666 baseFormat == GL_DEPTH_STENCIL ||
2667 rb_base_format == GL_DEPTH_COMPONENT ||
2668 rb_base_format == GL_DEPTH_STENCIL ||
2669 ((baseFormat == GL_LUMINANCE_ALPHA ||
2670 baseFormat == GL_ALPHA) &&
2671 rb_base_format != GL_RGBA) ||
2672 internalFormat == GL_RGB9_E5) {
2673 valid = false;
2674 }
2675 if (internalFormat == GL_RGB9_E5) {
2676 valid = false;
2677 }
2678 if (!valid) {
2679 _mesa_error(ctx, GL_INVALID_OPERATION,
2680 "glCopyTexImage%dD(internalFormat=%s)", dimensions,
2681 _mesa_lookup_enum_by_nr(internalFormat));
2682 return GL_TRUE;
2683 }
2684 }
2685
2686 if (_mesa_is_gles3(ctx)) {
2687 bool rb_is_srgb = false;
2688 bool dst_is_srgb = false;
2689
2690 if (ctx->Extensions.EXT_framebuffer_sRGB &&
2691 _mesa_get_format_color_encoding(rb->Format) == GL_SRGB) {
2692 rb_is_srgb = true;
2693 }
2694
2695 if (_mesa_get_linear_internalformat(internalFormat) != internalFormat) {
2696 dst_is_srgb = true;
2697 }
2698
2699 if (rb_is_srgb != dst_is_srgb) {
2700 /* Page 137 (page 149 of the PDF) in section 3.8.5 of the
2701 * OpenGLES 3.0.0 spec says:
2702 *
2703 * "The error INVALID_OPERATION is also generated if the
2704 * value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING for the
2705 * framebuffer attachment corresponding to the read buffer
2706 * is LINEAR (see section 6.1.13) and internalformat is
2707 * one of the sRGB formats described in section 3.8.16, or
2708 * if the value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING is
2709 * SRGB and internalformat is not one of the sRGB formats."
2710 */
2711 _mesa_error(ctx, GL_INVALID_OPERATION,
2712 "glCopyTexImage%dD(srgb usage mismatch)", dimensions);
2713 return GL_TRUE;
2714 }
2715
2716 /* Page 139, Table 3.15 of OpenGL ES 3.0 spec does not define ReadPixels
2717 * types for SNORM formats. Also, conversion to SNORM formats is not
2718 * allowed by Table 3.2 on Page 110.
2719 */
2720 if(_mesa_is_enum_format_snorm(internalFormat)) {
2721 _mesa_error(ctx, GL_INVALID_OPERATION,
2722 "glCopyTexImage%dD(internalFormat=%s)", dimensions,
2723 _mesa_lookup_enum_by_nr(internalFormat));
2724 return GL_TRUE;
2725 }
2726 }
2727
2728 if (!_mesa_source_buffer_exists(ctx, baseFormat)) {
2729 _mesa_error(ctx, GL_INVALID_OPERATION,
2730 "glCopyTexImage%dD(missing readbuffer)", dimensions);
2731 return GL_TRUE;
2732 }
2733
2734 /* From the EXT_texture_integer spec:
2735 *
2736 * "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
2737 * if the texture internalformat is an integer format and the read color
2738 * buffer is not an integer format, or if the internalformat is not an
2739 * integer format and the read color buffer is an integer format."
2740 */
2741 if (_mesa_is_color_format(internalFormat)) {
2742 bool is_int = _mesa_is_enum_format_integer(internalFormat);
2743 bool is_rbint = _mesa_is_enum_format_integer(rb_internal_format);
2744 bool is_unorm = _mesa_is_enum_format_unorm(internalFormat);
2745 bool is_rbunorm = _mesa_is_enum_format_unorm(rb_internal_format);
2746 if (is_int || is_rbint) {
2747 if (is_int != is_rbint) {
2748 _mesa_error(ctx, GL_INVALID_OPERATION,
2749 "glCopyTexImage%dD(integer vs non-integer)", dimensions);
2750 return GL_TRUE;
2751 } else if (_mesa_is_gles(ctx) &&
2752 _mesa_is_enum_format_unsigned_int(internalFormat) !=
2753 _mesa_is_enum_format_unsigned_int(rb_internal_format)) {
2754 _mesa_error(ctx, GL_INVALID_OPERATION,
2755 "glCopyTexImage%dD(signed vs unsigned integer)", dimensions);
2756 return GL_TRUE;
2757 }
2758 }
2759
2760 /* From page 138 of OpenGL ES 3.0 spec:
2761 * "The error INVALID_OPERATION is generated if floating-point RGBA
2762 * data is required; if signed integer RGBA data is required and the
2763 * format of the current color buffer is not signed integer; if
2764 * unsigned integer RGBA data is required and the format of the
2765 * current color buffer is not unsigned integer; or if fixed-point
2766 * RGBA data is required and the format of the current color buffer
2767 * is not fixed-point.
2768 */
2769 if (_mesa_is_gles(ctx) && is_unorm != is_rbunorm)
2770 _mesa_error(ctx, GL_INVALID_OPERATION,
2771 "glCopyTexImage%dD(unorm vs non-unorm)", dimensions);
2772 }
2773
2774 if (_mesa_is_compressed_format(ctx, internalFormat)) {
2775 if (!_mesa_target_can_be_compressed(ctx, target, internalFormat)) {
2776 _mesa_error(ctx, GL_INVALID_ENUM,
2777 "glCopyTexImage%dD(target)", dimensions);
2778 return GL_TRUE;
2779 }
2780 if (compressedteximage_only_format(ctx, internalFormat)) {
2781 _mesa_error(ctx, GL_INVALID_OPERATION,
2782 "glCopyTexImage%dD(no compression for format)", dimensions);
2783 return GL_TRUE;
2784 }
2785 if (border != 0) {
2786 _mesa_error(ctx, GL_INVALID_OPERATION,
2787 "glCopyTexImage%dD(border!=0)", dimensions);
2788 return GL_TRUE;
2789 }
2790 }
2791
2792 if (!mutable_tex_object(ctx, target)) {
2793 _mesa_error(ctx, GL_INVALID_OPERATION,
2794 "glCopyTexImage%dD(immutable texture)", dimensions);
2795 return GL_TRUE;
2796 }
2797
2798 /* if we get here, the parameters are OK */
2799 return GL_FALSE;
2800 }
2801
2802
2803 /**
2804 * Test glCopyTexSubImage[12]D() parameters for errors.
2805 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
2806 */
2807 static GLboolean
2808 copytexsubimage_error_check(struct gl_context *ctx, GLuint dimensions,
2809 GLenum target, GLint level,
2810 GLint xoffset, GLint yoffset, GLint zoffset,
2811 GLint width, GLint height)
2812 {
2813 struct gl_texture_object *texObj;
2814 struct gl_texture_image *texImage;
2815
2816 /* Check that the source buffer is complete */
2817 if (_mesa_is_user_fbo(ctx->ReadBuffer)) {
2818 if (ctx->ReadBuffer->_Status == 0) {
2819 _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
2820 }
2821 if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2822 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2823 "glCopyTexImage%dD(invalid readbuffer)", dimensions);
2824 return GL_TRUE;
2825 }
2826
2827 if (ctx->ReadBuffer->Visual.samples > 0) {
2828 _mesa_error(ctx, GL_INVALID_OPERATION,
2829 "glCopyTexSubImage%dD(multisample FBO)",
2830 dimensions);
2831 return GL_TRUE;
2832 }
2833 }
2834
2835 /* check target (proxies not allowed) */
2836 if (!legal_texsubimage_target(ctx, dimensions, target)) {
2837 _mesa_error(ctx, GL_INVALID_ENUM, "glCopyTexSubImage%uD(target=%s)",
2838 dimensions, _mesa_lookup_enum_by_nr(target));
2839 return GL_TRUE;
2840 }
2841
2842 /* Check level */
2843 if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
2844 _mesa_error(ctx, GL_INVALID_VALUE,
2845 "glCopyTexSubImage%dD(level=%d)", dimensions, level);
2846 return GL_TRUE;
2847 }
2848
2849 /* Get dest texture object / image pointers */
2850 texObj = _mesa_get_current_tex_object(ctx, target);
2851 if (!texObj) {
2852 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexSubImage%dD()", dimensions);
2853 return GL_TRUE;
2854 }
2855
2856 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
2857 if (!texImage) {
2858 /* destination image does not exist */
2859 _mesa_error(ctx, GL_INVALID_OPERATION,
2860 "glCopyTexSubImage%dD(invalid texture image)", dimensions);
2861 return GL_TRUE;
2862 }
2863
2864 if (error_check_subtexture_dimensions(ctx, "glCopyTexSubImage",
2865 dimensions, texImage,
2866 xoffset, yoffset, zoffset,
2867 width, height, 1)) {
2868 return GL_TRUE;
2869 }
2870
2871 if (_mesa_is_format_compressed(texImage->TexFormat)) {
2872 if (compressedteximage_only_format(ctx, texImage->InternalFormat)) {
2873 _mesa_error(ctx, GL_INVALID_OPERATION,
2874 "glCopyTexSubImage%dD(no compression for format)", dimensions);
2875 return GL_TRUE;
2876 }
2877 }
2878
2879 if (texImage->InternalFormat == GL_YCBCR_MESA) {
2880 _mesa_error(ctx, GL_INVALID_OPERATION, "glCopyTexSubImage2D");
2881 return GL_TRUE;
2882 }
2883
2884 if (!_mesa_source_buffer_exists(ctx, texImage->_BaseFormat)) {
2885 _mesa_error(ctx, GL_INVALID_OPERATION,
2886 "glCopyTexSubImage%dD(missing readbuffer, format=0x%x)",
2887 dimensions, texImage->_BaseFormat);
2888 return GL_TRUE;
2889 }
2890
2891 /* From the EXT_texture_integer spec:
2892 *
2893 * "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
2894 * if the texture internalformat is an integer format and the read color
2895 * buffer is not an integer format, or if the internalformat is not an
2896 * integer format and the read color buffer is an integer format."
2897 */
2898 if (_mesa_is_color_format(texImage->InternalFormat)) {
2899 struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
2900
2901 if (_mesa_is_format_integer_color(rb->Format) !=
2902 _mesa_is_format_integer_color(texImage->TexFormat)) {
2903 _mesa_error(ctx, GL_INVALID_OPERATION,
2904 "glCopyTexImage%dD(integer vs non-integer)", dimensions);
2905 return GL_TRUE;
2906 }
2907 }
2908
2909 /* if we get here, the parameters are OK */
2910 return GL_FALSE;
2911 }
2912
2913
2914 /** Callback info for walking over FBO hash table */
2915 struct cb_info
2916 {
2917 struct gl_context *ctx;
2918 struct gl_texture_object *texObj;
2919 GLuint level, face;
2920 };
2921
2922
2923 /**
2924 * Check render to texture callback. Called from _mesa_HashWalk().
2925 */
2926 static void
2927 check_rtt_cb(GLuint key, void *data, void *userData)
2928 {
2929 struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
2930 const struct cb_info *info = (struct cb_info *) userData;
2931 struct gl_context *ctx = info->ctx;
2932 const struct gl_texture_object *texObj = info->texObj;
2933 const GLuint level = info->level, face = info->face;
2934
2935 /* If this is a user-created FBO */
2936 if (_mesa_is_user_fbo(fb)) {
2937 GLuint i;
2938 /* check if any of the FBO's attachments point to 'texObj' */
2939 for (i = 0; i < BUFFER_COUNT; i++) {
2940 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
2941 if (att->Type == GL_TEXTURE &&
2942 att->Texture == texObj &&
2943 att->TextureLevel == level &&
2944 att->CubeMapFace == face) {
2945 _mesa_update_texture_renderbuffer(ctx, ctx->DrawBuffer, att);
2946 ASSERT(att->Renderbuffer->TexImage);
2947 /* Mark fb status as indeterminate to force re-validation */
2948 fb->_Status = 0;
2949 }
2950 }
2951 }
2952 }
2953
2954
2955 /**
2956 * When a texture image is specified we have to check if it's bound to
2957 * any framebuffer objects (render to texture) in order to detect changes
2958 * in size or format since that effects FBO completeness.
2959 * Any FBOs rendering into the texture must be re-validated.
2960 */
2961 void
2962 _mesa_update_fbo_texture(struct gl_context *ctx,
2963 struct gl_texture_object *texObj,
2964 GLuint face, GLuint level)
2965 {
2966 /* Only check this texture if it's been marked as RenderToTexture */
2967 if (texObj->_RenderToTexture) {
2968 struct cb_info info;
2969 info.ctx = ctx;
2970 info.texObj = texObj;
2971 info.level = level;
2972 info.face = face;
2973 _mesa_HashWalk(ctx->Shared->FrameBuffers, check_rtt_cb, &info);
2974 }
2975 }
2976
2977
2978 /**
2979 * If the texture object's GenerateMipmap flag is set and we've
2980 * changed the texture base level image, regenerate the rest of the
2981 * mipmap levels now.
2982 */
2983 static inline void
2984 check_gen_mipmap(struct gl_context *ctx, GLenum target,
2985 struct gl_texture_object *texObj, GLint level)
2986 {
2987 ASSERT(target != GL_TEXTURE_CUBE_MAP);
2988 if (texObj->GenerateMipmap &&
2989 level == texObj->BaseLevel &&
2990 level < texObj->MaxLevel) {
2991 ASSERT(ctx->Driver.GenerateMipmap);
2992 ctx->Driver.GenerateMipmap(ctx, target, texObj);
2993 }
2994 }
2995
2996
2997 /** Debug helper: override the user-requested internal format */
2998 static GLenum
2999 override_internal_format(GLenum internalFormat, GLint width, GLint height)
3000 {
3001 #if 0
3002 if (internalFormat == GL_RGBA16F_ARB ||
3003 internalFormat == GL_RGBA32F_ARB) {
3004 printf("Convert rgba float tex to int %d x %d\n", width, height);
3005 return GL_RGBA;
3006 }
3007 else if (internalFormat == GL_RGB16F_ARB ||
3008 internalFormat == GL_RGB32F_ARB) {
3009 printf("Convert rgb float tex to int %d x %d\n", width, height);
3010 return GL_RGB;
3011 }
3012 else if (internalFormat == GL_LUMINANCE_ALPHA16F_ARB ||
3013 internalFormat == GL_LUMINANCE_ALPHA32F_ARB) {
3014 printf("Convert luminance float tex to int %d x %d\n", width, height);
3015 return GL_LUMINANCE_ALPHA;
3016 }
3017 else if (internalFormat == GL_LUMINANCE16F_ARB ||
3018 internalFormat == GL_LUMINANCE32F_ARB) {
3019 printf("Convert luminance float tex to int %d x %d\n", width, height);
3020 return GL_LUMINANCE;
3021 }
3022 else if (internalFormat == GL_ALPHA16F_ARB ||
3023 internalFormat == GL_ALPHA32F_ARB) {
3024 printf("Convert luminance float tex to int %d x %d\n", width, height);
3025 return GL_ALPHA;
3026 }
3027 /*
3028 else if (internalFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) {
3029 internalFormat = GL_RGBA;
3030 }
3031 */
3032 else {
3033 return internalFormat;
3034 }
3035 #else
3036 return internalFormat;
3037 #endif
3038 }
3039
3040
3041 /**
3042 * Choose the actual hardware format for a texture image.
3043 * Try to use the same format as the previous image level when possible.
3044 * Otherwise, ask the driver for the best format.
3045 * It's important to try to choose a consistant format for all levels
3046 * for efficient texture memory layout/allocation. In particular, this
3047 * comes up during automatic mipmap generation.
3048 */
3049 mesa_format
3050 _mesa_choose_texture_format(struct gl_context *ctx,
3051 struct gl_texture_object *texObj,
3052 GLenum target, GLint level,
3053 GLenum internalFormat, GLenum format, GLenum type)
3054 {
3055 mesa_format f;
3056
3057 /* see if we've already chosen a format for the previous level */
3058 if (level > 0) {
3059 struct gl_texture_image *prevImage =
3060 _mesa_select_tex_image(ctx, texObj, target, level - 1);
3061 /* See if the prev level is defined and has an internal format which
3062 * matches the new internal format.
3063 */
3064 if (prevImage &&
3065 prevImage->Width > 0 &&
3066 prevImage->InternalFormat == internalFormat) {
3067 /* use the same format */
3068 ASSERT(prevImage->TexFormat != MESA_FORMAT_NONE);
3069 return prevImage->TexFormat;
3070 }
3071 }
3072
3073 /* If the application requested compression to an S3TC format but we don't
3074 * have the DXTn library, force a generic compressed format instead.
3075 */
3076 if (internalFormat != format && format != GL_NONE) {
3077 const GLenum before = internalFormat;
3078
3079 switch (internalFormat) {
3080 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
3081 if (!ctx->Mesa_DXTn)
3082 internalFormat = GL_COMPRESSED_RGB;
3083 break;
3084 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
3085 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
3086 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
3087 if (!ctx->Mesa_DXTn)
3088 internalFormat = GL_COMPRESSED_RGBA;
3089 break;
3090 default:
3091 break;
3092 }
3093
3094 if (before != internalFormat) {
3095 _mesa_warning(ctx,
3096 "DXT compression requested (%s), "
3097 "but libtxc_dxtn library not installed. Using %s "
3098 "instead.",
3099 _mesa_lookup_enum_by_nr(before),
3100 _mesa_lookup_enum_by_nr(internalFormat));
3101 }
3102 }
3103
3104 /* choose format from scratch */
3105 f = ctx->Driver.ChooseTextureFormat(ctx, target, internalFormat,
3106 format, type);
3107 ASSERT(f != MESA_FORMAT_NONE);
3108 return f;
3109 }
3110
3111
3112 /**
3113 * Adjust pixel unpack params and image dimensions to strip off the
3114 * one-pixel texture border.
3115 *
3116 * Gallium and intel don't support texture borders. They've seldem been used
3117 * and seldom been implemented correctly anyway.
3118 *
3119 * \param unpackNew returns the new pixel unpack parameters
3120 */
3121 static void
3122 strip_texture_border(GLenum target,
3123 GLint *width, GLint *height, GLint *depth,
3124 const struct gl_pixelstore_attrib *unpack,
3125 struct gl_pixelstore_attrib *unpackNew)
3126 {
3127 assert(width);
3128 assert(height);
3129 assert(depth);
3130
3131 *unpackNew = *unpack;
3132
3133 if (unpackNew->RowLength == 0)
3134 unpackNew->RowLength = *width;
3135
3136 if (unpackNew->ImageHeight == 0)
3137 unpackNew->ImageHeight = *height;
3138
3139 assert(*width >= 3);
3140 unpackNew->SkipPixels++; /* skip the border */
3141 *width = *width - 2; /* reduce the width by two border pixels */
3142
3143 /* The min height of a texture with a border is 3 */
3144 if (*height >= 3 && target != GL_TEXTURE_1D_ARRAY) {
3145 unpackNew->SkipRows++; /* skip the border */
3146 *height = *height - 2; /* reduce the height by two border pixels */
3147 }
3148
3149 if (*depth >= 3 &&
3150 target != GL_TEXTURE_2D_ARRAY &&
3151 target != GL_TEXTURE_CUBE_MAP_ARRAY) {
3152 unpackNew->SkipImages++; /* skip the border */
3153 *depth = *depth - 2; /* reduce the depth by two border pixels */
3154 }
3155 }
3156
3157
3158 /**
3159 * Common code to implement all the glTexImage1D/2D/3D functions
3160 * as well as glCompressedTexImage1D/2D/3D.
3161 * \param compressed only GL_TRUE for glCompressedTexImage1D/2D/3D calls.
3162 * \param format the user's image format (only used if !compressed)
3163 * \param type the user's image type (only used if !compressed)
3164 * \param imageSize only used for glCompressedTexImage1D/2D/3D calls.
3165 */
3166 static void
3167 teximage(struct gl_context *ctx, GLboolean compressed, GLuint dims,
3168 GLenum target, GLint level, GLint internalFormat,
3169 GLsizei width, GLsizei height, GLsizei depth,
3170 GLint border, GLenum format, GLenum type,
3171 GLsizei imageSize, const GLvoid *pixels)
3172 {
3173 const char *func = compressed ? "glCompressedTexImage" : "glTexImage";
3174 struct gl_pixelstore_attrib unpack_no_border;
3175 const struct gl_pixelstore_attrib *unpack = &ctx->Unpack;
3176 struct gl_texture_object *texObj;
3177 mesa_format texFormat;
3178 GLboolean dimensionsOK, sizeOK;
3179
3180 FLUSH_VERTICES(ctx, 0);
3181
3182 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE)) {
3183 if (compressed)
3184 _mesa_debug(ctx,
3185 "glCompressedTexImage%uD %s %d %s %d %d %d %d %p\n",
3186 dims,
3187 _mesa_lookup_enum_by_nr(target), level,
3188 _mesa_lookup_enum_by_nr(internalFormat),
3189 width, height, depth, border, pixels);
3190 else
3191 _mesa_debug(ctx,
3192 "glTexImage%uD %s %d %s %d %d %d %d %s %s %p\n",
3193 dims,
3194 _mesa_lookup_enum_by_nr(target), level,
3195 _mesa_lookup_enum_by_nr(internalFormat),
3196 width, height, depth, border,
3197 _mesa_lookup_enum_by_nr(format),
3198 _mesa_lookup_enum_by_nr(type), pixels);
3199 }
3200
3201 internalFormat = override_internal_format(internalFormat, width, height);
3202
3203 /* target error checking */
3204 if (!legal_teximage_target(ctx, dims, target)) {
3205 _mesa_error(ctx, GL_INVALID_ENUM, "%s%uD(target=%s)",
3206 func, dims, _mesa_lookup_enum_by_nr(target));
3207 return;
3208 }
3209
3210 /* general error checking */
3211 if (compressed) {
3212 if (compressed_texture_error_check(ctx, dims, target, level,
3213 internalFormat,
3214 width, height, depth,
3215 border, imageSize))
3216 return;
3217 }
3218 else {
3219 if (texture_error_check(ctx, dims, target, level, internalFormat,
3220 format, type, width, height, depth, border))
3221 return;
3222 }
3223
3224 /* Here we convert a cpal compressed image into a regular glTexImage2D
3225 * call by decompressing the texture. If we really want to support cpal
3226 * textures in any driver this would have to be changed.
3227 */
3228 if (ctx->API == API_OPENGLES && compressed && dims == 2) {
3229 switch (internalFormat) {
3230 case GL_PALETTE4_RGB8_OES:
3231 case GL_PALETTE4_RGBA8_OES:
3232 case GL_PALETTE4_R5_G6_B5_OES:
3233 case GL_PALETTE4_RGBA4_OES:
3234 case GL_PALETTE4_RGB5_A1_OES:
3235 case GL_PALETTE8_RGB8_OES:
3236 case GL_PALETTE8_RGBA8_OES:
3237 case GL_PALETTE8_R5_G6_B5_OES:
3238 case GL_PALETTE8_RGBA4_OES:
3239 case GL_PALETTE8_RGB5_A1_OES:
3240 _mesa_cpal_compressed_teximage2d(target, level, internalFormat,
3241 width, height, imageSize, pixels);
3242 return;
3243 }
3244 }
3245
3246 texObj = _mesa_get_current_tex_object(ctx, target);
3247 assert(texObj);
3248
3249 if (compressed) {
3250 /* For glCompressedTexImage() the driver has no choice about the
3251 * texture format since we'll never transcode the user's compressed
3252 * image data. The internalFormat was error checked earlier.
3253 */
3254 texFormat = _mesa_glenum_to_compressed_format(internalFormat);
3255 }
3256 else {
3257 texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
3258 internalFormat, format, type);
3259 }
3260
3261 assert(texFormat != MESA_FORMAT_NONE);
3262
3263 /* check that width, height, depth are legal for the mipmap level */
3264 dimensionsOK = _mesa_legal_texture_dimensions(ctx, target, level, width,
3265 height, depth, border);
3266
3267 /* check that the texture won't take too much memory, etc */
3268 sizeOK = ctx->Driver.TestProxyTexImage(ctx, proxy_target(target),
3269 level, texFormat,
3270 width, height, depth, border);
3271
3272 if (_mesa_is_proxy_texture(target)) {
3273 /* Proxy texture: just clear or set state depending on error checking */
3274 struct gl_texture_image *texImage =
3275 get_proxy_tex_image(ctx, target, level);
3276
3277 if (!texImage)
3278 return; /* GL_OUT_OF_MEMORY already recorded */
3279
3280 if (dimensionsOK && sizeOK) {
3281 _mesa_init_teximage_fields(ctx, texImage, width, height, depth,
3282 border, internalFormat, texFormat);
3283 }
3284 else {
3285 clear_teximage_fields(texImage);
3286 }
3287 }
3288 else {
3289 /* non-proxy target */
3290 const GLuint face = _mesa_tex_target_to_face(target);
3291 struct gl_texture_image *texImage;
3292
3293 if (!dimensionsOK) {
3294 _mesa_error(ctx, GL_INVALID_VALUE,
3295 "glTexImage%uD(invalid width or height or depth)",
3296 dims);
3297 return;
3298 }
3299
3300 if (!sizeOK) {
3301 _mesa_error(ctx, GL_OUT_OF_MEMORY,
3302 "glTexImage%uD(image too large)", dims);
3303 return;
3304 }
3305
3306 /* Allow a hardware driver to just strip out the border, to provide
3307 * reliable but slightly incorrect hardware rendering instead of
3308 * rarely-tested software fallback rendering.
3309 */
3310 if (border && ctx->Const.StripTextureBorder) {
3311 strip_texture_border(target, &width, &height, &depth, unpack,
3312 &unpack_no_border);
3313 border = 0;
3314 unpack = &unpack_no_border;
3315 }
3316
3317 if (ctx->NewState & _NEW_PIXEL)
3318 _mesa_update_state(ctx);
3319
3320 _mesa_lock_texture(ctx, texObj);
3321 {
3322 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
3323
3324 if (!texImage) {
3325 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s%uD", func, dims);
3326 }
3327 else {
3328 ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
3329
3330 _mesa_init_teximage_fields(ctx, texImage,
3331 width, height, depth,
3332 border, internalFormat, texFormat);
3333
3334 /* Give the texture to the driver. <pixels> may be null. */
3335 if (width > 0 && height > 0 && depth > 0) {
3336 if (compressed) {
3337 ctx->Driver.CompressedTexImage(ctx, dims, texImage,
3338 imageSize, pixels);
3339 }
3340 else {
3341 ctx->Driver.TexImage(ctx, dims, texImage, format,
3342 type, pixels, unpack);
3343 }
3344 }
3345
3346 check_gen_mipmap(ctx, target, texObj, level);
3347
3348 _mesa_update_fbo_texture(ctx, texObj, face, level);
3349
3350 _mesa_dirty_texobj(ctx, texObj);
3351 }
3352 }
3353 _mesa_unlock_texture(ctx, texObj);
3354 }
3355 }
3356
3357
3358
3359 /*
3360 * Called from the API. Note that width includes the border.
3361 */
3362 void GLAPIENTRY
3363 _mesa_TexImage1D( GLenum target, GLint level, GLint internalFormat,
3364 GLsizei width, GLint border, GLenum format,
3365 GLenum type, const GLvoid *pixels )
3366 {
3367 GET_CURRENT_CONTEXT(ctx);
3368 teximage(ctx, GL_FALSE, 1, target, level, internalFormat, width, 1, 1,
3369 border, format, type, 0, pixels);
3370 }
3371
3372
3373 void GLAPIENTRY
3374 _mesa_TexImage2D( GLenum target, GLint level, GLint internalFormat,
3375 GLsizei width, GLsizei height, GLint border,
3376 GLenum format, GLenum type,
3377 const GLvoid *pixels )
3378 {
3379 GET_CURRENT_CONTEXT(ctx);
3380 teximage(ctx, GL_FALSE, 2, target, level, internalFormat, width, height, 1,
3381 border, format, type, 0, pixels);
3382 }
3383
3384
3385 /*
3386 * Called by the API or display list executor.
3387 * Note that width and height include the border.
3388 */
3389 void GLAPIENTRY
3390 _mesa_TexImage3D( GLenum target, GLint level, GLint internalFormat,
3391 GLsizei width, GLsizei height, GLsizei depth,
3392 GLint border, GLenum format, GLenum type,
3393 const GLvoid *pixels )
3394 {
3395 GET_CURRENT_CONTEXT(ctx);
3396 teximage(ctx, GL_FALSE, 3, target, level, internalFormat,
3397 width, height, depth,
3398 border, format, type, 0, pixels);
3399 }
3400
3401
3402 void GLAPIENTRY
3403 _mesa_TexImage3DEXT( GLenum target, GLint level, GLenum internalFormat,
3404 GLsizei width, GLsizei height, GLsizei depth,
3405 GLint border, GLenum format, GLenum type,
3406 const GLvoid *pixels )
3407 {
3408 _mesa_TexImage3D(target, level, (GLint) internalFormat, width, height,
3409 depth, border, format, type, pixels);
3410 }
3411
3412
3413 void GLAPIENTRY
3414 _mesa_EGLImageTargetTexture2DOES (GLenum target, GLeglImageOES image)
3415 {
3416 struct gl_texture_object *texObj;
3417 struct gl_texture_image *texImage;
3418 bool valid_target;
3419 GET_CURRENT_CONTEXT(ctx);
3420 FLUSH_VERTICES(ctx, 0);
3421
3422 switch (target) {
3423 case GL_TEXTURE_2D:
3424 valid_target = ctx->Extensions.OES_EGL_image;
3425 break;
3426 case GL_TEXTURE_EXTERNAL_OES:
3427 valid_target =
3428 _mesa_is_gles(ctx) ? ctx->Extensions.OES_EGL_image_external : false;
3429 break;
3430 default:
3431 valid_target = false;
3432 break;
3433 }
3434
3435 if (!valid_target) {
3436 _mesa_error(ctx, GL_INVALID_ENUM,
3437 "glEGLImageTargetTexture2D(target=%d)", target);
3438 return;
3439 }
3440
3441 if (!image) {
3442 _mesa_error(ctx, GL_INVALID_OPERATION,
3443 "glEGLImageTargetTexture2D(image=%p)", image);
3444 return;
3445 }
3446
3447 if (ctx->NewState & _NEW_PIXEL)
3448 _mesa_update_state(ctx);
3449
3450 texObj = _mesa_get_current_tex_object(ctx, target);
3451 _mesa_lock_texture(ctx, texObj);
3452
3453 if (texObj->Immutable) {
3454 _mesa_error(ctx, GL_INVALID_OPERATION,
3455 "glEGLImageTargetTexture2D(texture is immutable)");
3456 _mesa_unlock_texture(ctx, texObj);
3457 return;
3458 }
3459
3460 texImage = _mesa_get_tex_image(ctx, texObj, target, 0);
3461 if (!texImage) {
3462 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glEGLImageTargetTexture2D");
3463 } else {
3464 ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
3465
3466 ctx->Driver.EGLImageTargetTexture2D(ctx, target,
3467 texObj, texImage, image);
3468
3469 _mesa_dirty_texobj(ctx, texObj);
3470 }
3471 _mesa_unlock_texture(ctx, texObj);
3472
3473 }
3474
3475
3476
3477 /**
3478 * Implement all the glTexSubImage1/2/3D() functions.
3479 */
3480 static void
3481 texsubimage(struct gl_context *ctx, GLuint dims, GLenum target, GLint level,
3482 GLint xoffset, GLint yoffset, GLint zoffset,
3483 GLsizei width, GLsizei height, GLsizei depth,
3484 GLenum format, GLenum type, const GLvoid *pixels )
3485 {
3486 struct gl_texture_object *texObj;
3487 struct gl_texture_image *texImage;
3488
3489 FLUSH_VERTICES(ctx, 0);
3490
3491 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3492 _mesa_debug(ctx, "glTexSubImage%uD %s %d %d %d %d %d %d %d %s %s %p\n",
3493 dims,
3494 _mesa_lookup_enum_by_nr(target), level,
3495 xoffset, yoffset, zoffset, width, height, depth,
3496 _mesa_lookup_enum_by_nr(format),
3497 _mesa_lookup_enum_by_nr(type), pixels);
3498
3499 /* check target (proxies not allowed) */
3500 if (!legal_texsubimage_target(ctx, dims, target)) {
3501 _mesa_error(ctx, GL_INVALID_ENUM, "glTexSubImage%uD(target=%s)",
3502 dims, _mesa_lookup_enum_by_nr(target));
3503 return;
3504 }
3505
3506 if (ctx->NewState & _NEW_PIXEL)
3507 _mesa_update_state(ctx);
3508
3509 if (texsubimage_error_check(ctx, dims, target, level,
3510 xoffset, yoffset, zoffset,
3511 width, height, depth, format, type)) {
3512 return; /* error was detected */
3513 }
3514
3515 texObj = _mesa_get_current_tex_object(ctx, target);
3516
3517 _mesa_lock_texture(ctx, texObj);
3518 {
3519 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3520
3521 if (width > 0 && height > 0 && depth > 0) {
3522 /* If we have a border, offset=-1 is legal. Bias by border width. */
3523 switch (dims) {
3524 case 3:
3525 if (target != GL_TEXTURE_2D_ARRAY)
3526 zoffset += texImage->Border;
3527 /* fall-through */
3528 case 2:
3529 if (target != GL_TEXTURE_1D_ARRAY)
3530 yoffset += texImage->Border;
3531 /* fall-through */
3532 case 1:
3533 xoffset += texImage->Border;
3534 }
3535
3536 ctx->Driver.TexSubImage(ctx, dims, texImage,
3537 xoffset, yoffset, zoffset,
3538 width, height, depth,
3539 format, type, pixels, &ctx->Unpack);
3540
3541 check_gen_mipmap(ctx, target, texObj, level);
3542
3543 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
3544 * the texel data, not the texture format, size, etc.
3545 */
3546 }
3547 }
3548 _mesa_unlock_texture(ctx, texObj);
3549 }
3550
3551
3552 void GLAPIENTRY
3553 _mesa_TexSubImage1D( GLenum target, GLint level,
3554 GLint xoffset, GLsizei width,
3555 GLenum format, GLenum type,
3556 const GLvoid *pixels )
3557 {
3558 GET_CURRENT_CONTEXT(ctx);
3559 texsubimage(ctx, 1, target, level,
3560 xoffset, 0, 0,
3561 width, 1, 1,
3562 format, type, pixels);
3563 }
3564
3565
3566 void GLAPIENTRY
3567 _mesa_TexSubImage2D( GLenum target, GLint level,
3568 GLint xoffset, GLint yoffset,
3569 GLsizei width, GLsizei height,
3570 GLenum format, GLenum type,
3571 const GLvoid *pixels )
3572 {
3573 GET_CURRENT_CONTEXT(ctx);
3574 texsubimage(ctx, 2, target, level,
3575 xoffset, yoffset, 0,
3576 width, height, 1,
3577 format, type, pixels);
3578 }
3579
3580
3581
3582 void GLAPIENTRY
3583 _mesa_TexSubImage3D( GLenum target, GLint level,
3584 GLint xoffset, GLint yoffset, GLint zoffset,
3585 GLsizei width, GLsizei height, GLsizei depth,
3586 GLenum format, GLenum type,
3587 const GLvoid *pixels )
3588 {
3589 GET_CURRENT_CONTEXT(ctx);
3590 texsubimage(ctx, 3, target, level,
3591 xoffset, yoffset, zoffset,
3592 width, height, depth,
3593 format, type, pixels);
3594 }
3595
3596
3597
3598 /**
3599 * For glCopyTexSubImage, return the source renderbuffer to copy texel data
3600 * from. This depends on whether the texture contains color or depth values.
3601 */
3602 static struct gl_renderbuffer *
3603 get_copy_tex_image_source(struct gl_context *ctx, mesa_format texFormat)
3604 {
3605 if (_mesa_get_format_bits(texFormat, GL_DEPTH_BITS) > 0) {
3606 /* reading from depth/stencil buffer */
3607 return ctx->ReadBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
3608 }
3609 else {
3610 /* copying from color buffer */
3611 return ctx->ReadBuffer->_ColorReadBuffer;
3612 }
3613 }
3614
3615 static void
3616 copytexsubimage_by_slice(struct gl_context *ctx,
3617 struct gl_texture_image *texImage,
3618 GLuint dims,
3619 GLint xoffset, GLint yoffset, GLint zoffset,
3620 struct gl_renderbuffer *rb,
3621 GLint x, GLint y,
3622 GLsizei width, GLsizei height)
3623 {
3624 if (texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
3625 int slice;
3626
3627 /* For 1D arrays, we copy each scanline of the source rectangle into the
3628 * next array slice.
3629 */
3630 assert(zoffset == 0);
3631
3632 for (slice = 0; slice < height; slice++) {
3633 assert(yoffset + slice < texImage->Height);
3634 ctx->Driver.CopyTexSubImage(ctx, 2, texImage,
3635 xoffset, 0, yoffset + slice,
3636 rb, x, y + slice, width, 1);
3637 }
3638 } else {
3639 ctx->Driver.CopyTexSubImage(ctx, dims, texImage,
3640 xoffset, yoffset, zoffset,
3641 rb, x, y, width, height);
3642 }
3643 }
3644
3645 static GLboolean
3646 formats_differ_in_component_sizes (mesa_format f1,
3647 mesa_format f2)
3648 {
3649 GLint f1_r_bits = _mesa_get_format_bits(f1, GL_RED_BITS);
3650 GLint f1_g_bits = _mesa_get_format_bits(f1, GL_GREEN_BITS);
3651 GLint f1_b_bits = _mesa_get_format_bits(f1, GL_BLUE_BITS);
3652 GLint f1_a_bits = _mesa_get_format_bits(f1, GL_ALPHA_BITS);
3653
3654 GLint f2_r_bits = _mesa_get_format_bits(f2, GL_RED_BITS);
3655 GLint f2_g_bits = _mesa_get_format_bits(f2, GL_GREEN_BITS);
3656 GLint f2_b_bits = _mesa_get_format_bits(f2, GL_BLUE_BITS);
3657 GLint f2_a_bits = _mesa_get_format_bits(f2, GL_ALPHA_BITS);
3658
3659 if ((f1_r_bits && f2_r_bits && f1_r_bits != f2_r_bits)
3660 || (f1_g_bits && f2_g_bits && f1_g_bits != f2_g_bits)
3661 || (f1_b_bits && f2_b_bits && f1_b_bits != f2_b_bits)
3662 || (f1_a_bits && f2_a_bits && f1_a_bits != f2_a_bits))
3663 return GL_TRUE;
3664
3665 return GL_FALSE;
3666 }
3667
3668 /**
3669 * Implement the glCopyTexImage1/2D() functions.
3670 */
3671 static void
3672 copyteximage(struct gl_context *ctx, GLuint dims,
3673 GLenum target, GLint level, GLenum internalFormat,
3674 GLint x, GLint y, GLsizei width, GLsizei height, GLint border )
3675 {
3676 struct gl_texture_object *texObj;
3677 struct gl_texture_image *texImage;
3678 const GLuint face = _mesa_tex_target_to_face(target);
3679 mesa_format texFormat;
3680 struct gl_renderbuffer *rb;
3681
3682 FLUSH_VERTICES(ctx, 0);
3683
3684 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3685 _mesa_debug(ctx, "glCopyTexImage%uD %s %d %s %d %d %d %d %d\n",
3686 dims,
3687 _mesa_lookup_enum_by_nr(target), level,
3688 _mesa_lookup_enum_by_nr(internalFormat),
3689 x, y, width, height, border);
3690
3691 if (ctx->NewState & NEW_COPY_TEX_STATE)
3692 _mesa_update_state(ctx);
3693
3694 if (copytexture_error_check(ctx, dims, target, level, internalFormat,
3695 width, height, border))
3696 return;
3697
3698 if (!_mesa_legal_texture_dimensions(ctx, target, level, width, height,
3699 1, border)) {
3700 _mesa_error(ctx, GL_INVALID_VALUE,
3701 "glCopyTexImage%uD(invalid width or height)", dims);
3702 return;
3703 }
3704
3705 texObj = _mesa_get_current_tex_object(ctx, target);
3706 assert(texObj);
3707
3708 texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
3709 internalFormat, GL_NONE, GL_NONE);
3710
3711 rb = _mesa_get_read_renderbuffer_for_format(ctx, internalFormat);
3712
3713 if (_mesa_is_gles3(ctx)) {
3714 if (_mesa_is_enum_format_unsized(internalFormat)) {
3715 /* Conversion from GL_RGB10_A2 source buffer format is not allowed in
3716 * OpenGL ES 3.0. Khronos bug# 9807.
3717 */
3718 if (rb->InternalFormat == GL_RGB10_A2) {
3719 _mesa_error(ctx, GL_INVALID_OPERATION,
3720 "glCopyTexImage%uD(Reading from GL_RGB10_A2 buffer and"
3721 " writing to unsized internal format)", dims);
3722 return;
3723 }
3724 }
3725 /* From Page 139 of OpenGL ES 3.0 spec:
3726 * "If internalformat is sized, the internal format of the new texel
3727 * array is internalformat, and this is also the new texel array’s
3728 * effective internal format. If the component sizes of internalformat
3729 * do not exactly match the corresponding component sizes of the source
3730 * buffer’s effective internal format, described below, an
3731 * INVALID_OPERATION error is generated. If internalformat is unsized,
3732 * the internal format of the new texel array is the effective internal
3733 * format of the source buffer, and this is also the new texel array’s
3734 * effective internal format.
3735 */
3736 else if (formats_differ_in_component_sizes (texFormat, rb->Format)) {
3737 _mesa_error(ctx, GL_INVALID_OPERATION,
3738 "glCopyTexImage%uD(componenet size changed in"
3739 " internal format)", dims);
3740 return;
3741 }
3742 }
3743
3744 assert(texFormat != MESA_FORMAT_NONE);
3745
3746 if (!ctx->Driver.TestProxyTexImage(ctx, proxy_target(target),
3747 level, texFormat,
3748 width, height, 1, border)) {
3749 _mesa_error(ctx, GL_OUT_OF_MEMORY,
3750 "glCopyTexImage%uD(image too large)", dims);
3751 return;
3752 }
3753
3754 if (border && ctx->Const.StripTextureBorder) {
3755 x += border;
3756 width -= border * 2;
3757 if (dims == 2) {
3758 y += border;
3759 height -= border * 2;
3760 }
3761 border = 0;
3762 }
3763
3764 _mesa_lock_texture(ctx, texObj);
3765 {
3766 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
3767
3768 if (!texImage) {
3769 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage%uD", dims);
3770 }
3771 else {
3772 GLint srcX = x, srcY = y, dstX = 0, dstY = 0, dstZ = 0;
3773
3774 /* Free old texture image */
3775 ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
3776
3777 _mesa_init_teximage_fields(ctx, texImage, width, height, 1,
3778 border, internalFormat, texFormat);
3779
3780 if (width && height) {
3781 /* Allocate texture memory (no pixel data yet) */
3782 ctx->Driver.AllocTextureImageBuffer(ctx, texImage);
3783
3784 if (_mesa_clip_copytexsubimage(ctx, &dstX, &dstY, &srcX, &srcY,
3785 &width, &height)) {
3786 struct gl_renderbuffer *srcRb =
3787 get_copy_tex_image_source(ctx, texImage->TexFormat);
3788
3789 copytexsubimage_by_slice(ctx, texImage, dims,
3790 dstX, dstY, dstZ,
3791 srcRb, srcX, srcY, width, height);
3792 }
3793
3794 check_gen_mipmap(ctx, target, texObj, level);
3795 }
3796
3797 _mesa_update_fbo_texture(ctx, texObj, face, level);
3798
3799 _mesa_dirty_texobj(ctx, texObj);
3800 }
3801 }
3802 _mesa_unlock_texture(ctx, texObj);
3803 }
3804
3805
3806
3807 void GLAPIENTRY
3808 _mesa_CopyTexImage1D( GLenum target, GLint level,
3809 GLenum internalFormat,
3810 GLint x, GLint y,
3811 GLsizei width, GLint border )
3812 {
3813 GET_CURRENT_CONTEXT(ctx);
3814 copyteximage(ctx, 1, target, level, internalFormat, x, y, width, 1, border);
3815 }
3816
3817
3818
3819 void GLAPIENTRY
3820 _mesa_CopyTexImage2D( GLenum target, GLint level, GLenum internalFormat,
3821 GLint x, GLint y, GLsizei width, GLsizei height,
3822 GLint border )
3823 {
3824 GET_CURRENT_CONTEXT(ctx);
3825 copyteximage(ctx, 2, target, level, internalFormat,
3826 x, y, width, height, border);
3827 }
3828
3829
3830
3831 /**
3832 * Implementation for glCopyTexSubImage1/2/3D() functions.
3833 */
3834 static void
3835 copytexsubimage(struct gl_context *ctx, GLuint dims, GLenum target, GLint level,
3836 GLint xoffset, GLint yoffset, GLint zoffset,
3837 GLint x, GLint y, GLsizei width, GLsizei height)
3838 {
3839 struct gl_texture_object *texObj;
3840 struct gl_texture_image *texImage;
3841
3842 FLUSH_VERTICES(ctx, 0);
3843
3844 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3845 _mesa_debug(ctx, "glCopyTexSubImage%uD %s %d %d %d %d %d %d %d %d\n",
3846 dims,
3847 _mesa_lookup_enum_by_nr(target),
3848 level, xoffset, yoffset, zoffset, x, y, width, height);
3849
3850 if (ctx->NewState & NEW_COPY_TEX_STATE)
3851 _mesa_update_state(ctx);
3852
3853 if (copytexsubimage_error_check(ctx, dims, target, level,
3854 xoffset, yoffset, zoffset, width, height)) {
3855 return;
3856 }
3857
3858 texObj = _mesa_get_current_tex_object(ctx, target);
3859
3860 _mesa_lock_texture(ctx, texObj);
3861 {
3862 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3863
3864 /* If we have a border, offset=-1 is legal. Bias by border width. */
3865 switch (dims) {
3866 case 3:
3867 if (target != GL_TEXTURE_2D_ARRAY)
3868 zoffset += texImage->Border;
3869 /* fall-through */
3870 case 2:
3871 if (target != GL_TEXTURE_1D_ARRAY)
3872 yoffset += texImage->Border;
3873 /* fall-through */
3874 case 1:
3875 xoffset += texImage->Border;
3876 }
3877
3878 if (_mesa_clip_copytexsubimage(ctx, &xoffset, &yoffset, &x, &y,
3879 &width, &height)) {
3880 struct gl_renderbuffer *srcRb =
3881 get_copy_tex_image_source(ctx, texImage->TexFormat);
3882
3883 copytexsubimage_by_slice(ctx, texImage, dims,
3884 xoffset, yoffset, zoffset,
3885 srcRb, x, y, width, height);
3886
3887 check_gen_mipmap(ctx, target, texObj, level);
3888
3889 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
3890 * the texel data, not the texture format, size, etc.
3891 */
3892 }
3893 }
3894 _mesa_unlock_texture(ctx, texObj);
3895 }
3896
3897
3898 void GLAPIENTRY
3899 _mesa_CopyTexSubImage1D( GLenum target, GLint level,
3900 GLint xoffset, GLint x, GLint y, GLsizei width )
3901 {
3902 GET_CURRENT_CONTEXT(ctx);
3903 copytexsubimage(ctx, 1, target, level, xoffset, 0, 0, x, y, width, 1);
3904 }
3905
3906
3907
3908 void GLAPIENTRY
3909 _mesa_CopyTexSubImage2D( GLenum target, GLint level,
3910 GLint xoffset, GLint yoffset,
3911 GLint x, GLint y, GLsizei width, GLsizei height )
3912 {
3913 GET_CURRENT_CONTEXT(ctx);
3914 copytexsubimage(ctx, 2, target, level, xoffset, yoffset, 0, x, y,
3915 width, height);
3916 }
3917
3918
3919
3920 void GLAPIENTRY
3921 _mesa_CopyTexSubImage3D( GLenum target, GLint level,
3922 GLint xoffset, GLint yoffset, GLint zoffset,
3923 GLint x, GLint y, GLsizei width, GLsizei height )
3924 {
3925 GET_CURRENT_CONTEXT(ctx);
3926 copytexsubimage(ctx, 3, target, level, xoffset, yoffset, zoffset,
3927 x, y, width, height);
3928 }
3929
3930 static bool
3931 check_clear_tex_image(struct gl_context *ctx,
3932 const char *function,
3933 struct gl_texture_image *texImage,
3934 GLenum format, GLenum type,
3935 const void *data,
3936 GLubyte *clearValue)
3937 {
3938 struct gl_texture_object *texObj = texImage->TexObject;
3939 static const GLubyte zeroData[MAX_PIXEL_BYTES];
3940 GLenum internalFormat = texImage->InternalFormat;
3941 GLenum err;
3942
3943 if (texObj->Target == GL_TEXTURE_BUFFER) {
3944 _mesa_error(ctx, GL_INVALID_OPERATION,
3945 "%s(buffer texture)", function);
3946 return false;
3947 }
3948
3949 if (_mesa_is_compressed_format(ctx, internalFormat)) {
3950 _mesa_error(ctx, GL_INVALID_OPERATION,
3951 "%s(compressed texture)", function);
3952 return false;
3953 }
3954
3955 err = _mesa_error_check_format_and_type(ctx, format, type);
3956 if (err != GL_NO_ERROR) {
3957 _mesa_error(ctx, err,
3958 "%s(incompatible format = %s, type = %s)",
3959 function,
3960 _mesa_lookup_enum_by_nr(format),
3961 _mesa_lookup_enum_by_nr(type));
3962 return false;
3963 }
3964
3965 /* make sure internal format and format basically agree */
3966 if (!texture_formats_agree(internalFormat, format)) {
3967 _mesa_error(ctx, GL_INVALID_OPERATION,
3968 "%s(incompatible internalFormat = %s, format = %s)",
3969 function,
3970 _mesa_lookup_enum_by_nr(internalFormat),
3971 _mesa_lookup_enum_by_nr(format));
3972 return false;
3973 }
3974
3975 if (ctx->Version >= 30 || ctx->Extensions.EXT_texture_integer) {
3976 /* both source and dest must be integer-valued, or neither */
3977 if (_mesa_is_format_integer_color(texImage->TexFormat) !=
3978 _mesa_is_enum_format_integer(format)) {
3979 _mesa_error(ctx, GL_INVALID_OPERATION,
3980 "%s(integer/non-integer format mismatch)",
3981 function);
3982 return false;
3983 }
3984 }
3985
3986 if (!_mesa_texstore(ctx,
3987 1, /* dims */
3988 texImage->_BaseFormat,
3989 texImage->TexFormat,
3990 0, /* dstRowStride */
3991 &clearValue,
3992 1, 1, 1, /* srcWidth/Height/Depth */
3993 format, type,
3994 data ? data : zeroData,
3995 &ctx->DefaultPacking)) {
3996 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid format)", function);
3997 return false;
3998 }
3999
4000 return true;
4001 }
4002
4003 static struct gl_texture_object *
4004 get_tex_obj_for_clear(struct gl_context *ctx,
4005 const char *function,
4006 GLuint texture)
4007 {
4008 struct gl_texture_object *texObj;
4009
4010 if (texture == 0) {
4011 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(zero texture)", function);
4012 return NULL;
4013 }
4014
4015 texObj = _mesa_lookup_texture(ctx, texture);
4016
4017 if (texObj == NULL) {
4018 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(non-gen name)", function);
4019 return NULL;
4020 }
4021
4022 if (texObj->Target == 0) {
4023 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(unbound tex)", function);
4024 return NULL;
4025 }
4026
4027 return texObj;
4028 }
4029
4030 static int
4031 get_tex_images_for_clear(struct gl_context *ctx,
4032 const char *function,
4033 struct gl_texture_object *texObj,
4034 GLint level,
4035 struct gl_texture_image **texImages)
4036 {
4037 GLenum target;
4038 int i;
4039
4040 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
4041 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid level)", function);
4042 return 0;
4043 }
4044
4045 if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
4046 for (i = 0; i < MAX_FACES; i++) {
4047 target = GL_TEXTURE_CUBE_MAP_POSITIVE_X + i;
4048
4049 texImages[i] = _mesa_select_tex_image(ctx, texObj, target, level);
4050 if (texImages[i] == NULL) {
4051 _mesa_error(ctx, GL_INVALID_OPERATION,
4052 "%s(invalid level)", function);
4053 return 0;
4054 }
4055 }
4056
4057 return MAX_FACES;
4058 }
4059
4060 texImages[0] = _mesa_select_tex_image(ctx, texObj, texObj->Target, level);
4061
4062 if (texImages[0] == NULL) {
4063 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid level)", function);
4064 return 0;
4065 }
4066
4067 return 1;
4068 }
4069
4070 void GLAPIENTRY
4071 _mesa_ClearTexSubImage( GLuint texture, GLint level,
4072 GLint xoffset, GLint yoffset, GLint zoffset,
4073 GLsizei width, GLsizei height, GLsizei depth,
4074 GLenum format, GLenum type, const void *data )
4075 {
4076 GET_CURRENT_CONTEXT(ctx);
4077 struct gl_texture_object *texObj;
4078 struct gl_texture_image *texImages[MAX_FACES];
4079 GLubyte clearValue[MAX_FACES][MAX_PIXEL_BYTES];
4080 int i, numImages;
4081 int minDepth, maxDepth;
4082
4083 texObj = get_tex_obj_for_clear(ctx, "glClearTexSubImage", texture);
4084
4085 if (texObj == NULL)
4086 return;
4087
4088 _mesa_lock_texture(ctx, texObj);
4089
4090 numImages = get_tex_images_for_clear(ctx, "glClearTexSubImage",
4091 texObj, level, texImages);
4092 if (numImages == 0)
4093 goto out;
4094
4095 if (numImages == 1) {
4096 minDepth = -(int) texImages[0]->Border;
4097 maxDepth = texImages[0]->Depth;
4098 } else {
4099 minDepth = 0;
4100 maxDepth = numImages;
4101 }
4102
4103 if (xoffset < -(GLint) texImages[0]->Border ||
4104 yoffset < -(GLint) texImages[0]->Border ||
4105 zoffset < minDepth ||
4106 width < 0 ||
4107 height < 0 ||
4108 depth < 0 ||
4109 xoffset + width > texImages[0]->Width ||
4110 yoffset + height > texImages[0]->Height ||
4111 zoffset + depth > maxDepth) {
4112 _mesa_error(ctx, GL_INVALID_OPERATION,
4113 "glClearSubTexImage(invalid dimensions)");
4114 goto out;
4115 }
4116
4117 if (numImages == 1) {
4118 if (check_clear_tex_image(ctx, "glClearTexSubImage",
4119 texImages[0],
4120 format, type, data, clearValue[0])) {
4121 ctx->Driver.ClearTexSubImage(ctx,
4122 texImages[0],
4123 xoffset, yoffset, zoffset,
4124 width, height, depth,
4125 data ? clearValue[0] : NULL);
4126 }
4127 } else {
4128 for (i = zoffset; i < zoffset + depth; i++) {
4129 if (!check_clear_tex_image(ctx, "glClearTexSubImage",
4130 texImages[i],
4131 format, type, data, clearValue[i]))
4132 goto out;
4133 }
4134 for (i = zoffset; i < zoffset + depth; i++) {
4135 ctx->Driver.ClearTexSubImage(ctx,
4136 texImages[i],
4137 xoffset, yoffset, 0,
4138 width, height, 1,
4139 data ? clearValue[i] : NULL);
4140 }
4141 }
4142
4143 out:
4144 _mesa_unlock_texture(ctx, texObj);
4145 }
4146
4147 void GLAPIENTRY
4148 _mesa_ClearTexImage( GLuint texture, GLint level,
4149 GLenum format, GLenum type, const void *data )
4150 {
4151 GET_CURRENT_CONTEXT(ctx);
4152 struct gl_texture_object *texObj;
4153 struct gl_texture_image *texImages[MAX_FACES];
4154 GLubyte clearValue[MAX_FACES][MAX_PIXEL_BYTES];
4155 int i, numImages;
4156
4157 texObj = get_tex_obj_for_clear(ctx, "glClearTexImage", texture);
4158
4159 if (texObj == NULL)
4160 return;
4161
4162 _mesa_lock_texture(ctx, texObj);
4163
4164 numImages = get_tex_images_for_clear(ctx, "glClearTexImage",
4165 texObj, level, texImages);
4166
4167 for (i = 0; i < numImages; i++) {
4168 if (!check_clear_tex_image(ctx, "glClearTexImage",
4169 texImages[i],
4170 format, type, data,
4171 clearValue[i]))
4172 goto out;
4173 }
4174
4175 for (i = 0; i < numImages; i++) {
4176 ctx->Driver.ClearTexSubImage(ctx, texImages[i],
4177 -(GLint) texImages[i]->Border, /* xoffset */
4178 -(GLint) texImages[i]->Border, /* yoffset */
4179 -(GLint) texImages[i]->Border, /* zoffset */
4180 texImages[i]->Width,
4181 texImages[i]->Height,
4182 texImages[i]->Depth,
4183 data ? clearValue[i] : NULL);
4184 }
4185
4186 out:
4187 _mesa_unlock_texture(ctx, texObj);
4188 }
4189
4190
4191
4192
4193 /**********************************************************************/
4194 /****** Compressed Textures ******/
4195 /**********************************************************************/
4196
4197
4198 /**
4199 * Error checking for glCompressedTexSubImage[123]D().
4200 * \return error code or GL_NO_ERROR.
4201 */
4202 static GLenum
4203 compressed_subtexture_error_check(struct gl_context *ctx, GLint dims,
4204 GLenum target, GLint level,
4205 GLint xoffset, GLint yoffset, GLint zoffset,
4206 GLsizei width, GLsizei height, GLsizei depth,
4207 GLenum format, GLsizei imageSize)
4208 {
4209 struct gl_texture_object *texObj;
4210 struct gl_texture_image *texImage;
4211 GLint expectedSize;
4212 GLboolean targetOK;
4213
4214 switch (dims) {
4215 case 2:
4216 switch (target) {
4217 case GL_TEXTURE_2D:
4218 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
4219 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
4220 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
4221 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
4222 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
4223 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
4224 targetOK = GL_TRUE;
4225 break;
4226 default:
4227 targetOK = GL_FALSE;
4228 break;
4229 }
4230 break;
4231 case 3:
4232 targetOK = (target == GL_TEXTURE_2D_ARRAY);
4233 break;
4234 default:
4235 assert(dims == 1);
4236 /* no 1D compressed textures at this time */
4237 targetOK = GL_FALSE;
4238 break;
4239 }
4240
4241 if (!targetOK) {
4242 _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexSubImage%uD(target)",
4243 dims);
4244 return GL_TRUE;
4245 }
4246
4247 /* this will catch any invalid compressed format token */
4248 if (!_mesa_is_compressed_format(ctx, format)) {
4249 _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage%uD(format)",
4250 dims);
4251 return GL_TRUE;
4252 }
4253
4254 if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
4255 _mesa_error(ctx, GL_INVALID_VALUE, "glCompressedTexSubImage%uD(level=%d)",
4256 dims, level);
4257 return GL_TRUE;
4258 }
4259
4260 /* Check for invalid pixel storage modes */
4261 if (!_mesa_compressed_texture_pixel_storage_error_check(ctx, dims,
4262 &ctx->Unpack,
4263 "glCompressedTexSubImage")) {
4264 return GL_FALSE;
4265 }
4266
4267
4268 expectedSize = compressed_tex_size(width, height, depth, format);
4269 if (expectedSize != imageSize) {
4270 _mesa_error(ctx, GL_INVALID_VALUE, "glCompressedTexSubImage%uD(size=%d)",
4271 dims, imageSize);
4272 return GL_TRUE;
4273 }
4274
4275 texObj = _mesa_get_current_tex_object(ctx, target);
4276 if (!texObj) {
4277 _mesa_error(ctx, GL_OUT_OF_MEMORY,
4278 "glCompressedTexSubImage%uD()", dims);
4279 return GL_TRUE;
4280 }
4281
4282 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
4283 if (!texImage) {
4284 _mesa_error(ctx, GL_INVALID_OPERATION,
4285 "glCompressedTexSubImage%uD(invalid texture image)", dims);
4286 return GL_TRUE;
4287 }
4288
4289 if ((GLint) format != texImage->InternalFormat) {
4290 _mesa_error(ctx, GL_INVALID_OPERATION,
4291 "glCompressedTexSubImage%uD(format=0x%x)", dims, format);
4292 return GL_TRUE;
4293 }
4294
4295 if (compressedteximage_only_format(ctx, format)) {
4296 _mesa_error(ctx, GL_INVALID_OPERATION,
4297 "glCompressedTexSubImage%uD(format=0x%x cannot be updated)"
4298 , dims, format);
4299 return GL_TRUE;
4300 }
4301
4302 if (error_check_subtexture_dimensions(ctx, "glCompressedTexSubImage", dims,
4303 texImage, xoffset, yoffset, zoffset,
4304 width, height, depth)) {
4305 return GL_TRUE;
4306 }
4307
4308 return GL_FALSE;
4309 }
4310
4311
4312 void GLAPIENTRY
4313 _mesa_CompressedTexImage1D(GLenum target, GLint level,
4314 GLenum internalFormat, GLsizei width,
4315 GLint border, GLsizei imageSize,
4316 const GLvoid *data)
4317 {
4318 GET_CURRENT_CONTEXT(ctx);
4319 teximage(ctx, GL_TRUE, 1, target, level, internalFormat,
4320 width, 1, 1, border, GL_NONE, GL_NONE, imageSize, data);
4321 }
4322
4323
4324 void GLAPIENTRY
4325 _mesa_CompressedTexImage2D(GLenum target, GLint level,
4326 GLenum internalFormat, GLsizei width,
4327 GLsizei height, GLint border, GLsizei imageSize,
4328 const GLvoid *data)
4329 {
4330 GET_CURRENT_CONTEXT(ctx);
4331 teximage(ctx, GL_TRUE, 2, target, level, internalFormat,
4332 width, height, 1, border, GL_NONE, GL_NONE, imageSize, data);
4333 }
4334
4335
4336 void GLAPIENTRY
4337 _mesa_CompressedTexImage3D(GLenum target, GLint level,
4338 GLenum internalFormat, GLsizei width,
4339 GLsizei height, GLsizei depth, GLint border,
4340 GLsizei imageSize, const GLvoid *data)
4341 {
4342 GET_CURRENT_CONTEXT(ctx);
4343 teximage(ctx, GL_TRUE, 3, target, level, internalFormat,
4344 width, height, depth, border, GL_NONE, GL_NONE, imageSize, data);
4345 }
4346
4347
4348 /**
4349 * Common helper for glCompressedTexSubImage1/2/3D().
4350 */
4351 static void
4352 compressed_tex_sub_image(GLuint dims, GLenum target, GLint level,
4353 GLint xoffset, GLint yoffset, GLint zoffset,
4354 GLsizei width, GLsizei height, GLsizei depth,
4355 GLenum format, GLsizei imageSize, const GLvoid *data)
4356 {
4357 struct gl_texture_object *texObj;
4358 struct gl_texture_image *texImage;
4359 GET_CURRENT_CONTEXT(ctx);
4360 FLUSH_VERTICES(ctx, 0);
4361
4362 if (compressed_subtexture_error_check(ctx, dims, target, level,
4363 xoffset, yoffset, zoffset,
4364 width, height, depth,
4365 format, imageSize)) {
4366 return;
4367 }
4368
4369 texObj = _mesa_get_current_tex_object(ctx, target);
4370
4371 _mesa_lock_texture(ctx, texObj);
4372 {
4373 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
4374 assert(texImage);
4375
4376 if (width > 0 && height > 0 && depth > 0) {
4377 ctx->Driver.CompressedTexSubImage(ctx, dims, texImage,
4378 xoffset, yoffset, zoffset,
4379 width, height, depth,
4380 format, imageSize, data);
4381
4382 check_gen_mipmap(ctx, target, texObj, level);
4383
4384 /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
4385 * the texel data, not the texture format, size, etc.
4386 */
4387 }
4388 }
4389 _mesa_unlock_texture(ctx, texObj);
4390 }
4391
4392
4393 void GLAPIENTRY
4394 _mesa_CompressedTexSubImage1D(GLenum target, GLint level, GLint xoffset,
4395 GLsizei width, GLenum format,
4396 GLsizei imageSize, const GLvoid *data)
4397 {
4398 compressed_tex_sub_image(1, target, level, xoffset, 0, 0, width, 1, 1,
4399 format, imageSize, data);
4400 }
4401
4402
4403 void GLAPIENTRY
4404 _mesa_CompressedTexSubImage2D(GLenum target, GLint level, GLint xoffset,
4405 GLint yoffset, GLsizei width, GLsizei height,
4406 GLenum format, GLsizei imageSize,
4407 const GLvoid *data)
4408 {
4409 compressed_tex_sub_image(2, target, level, xoffset, yoffset, 0,
4410 width, height, 1, format, imageSize, data);
4411 }
4412
4413
4414 void GLAPIENTRY
4415 _mesa_CompressedTexSubImage3D(GLenum target, GLint level, GLint xoffset,
4416 GLint yoffset, GLint zoffset, GLsizei width,
4417 GLsizei height, GLsizei depth, GLenum format,
4418 GLsizei imageSize, const GLvoid *data)
4419 {
4420 compressed_tex_sub_image(3, target, level, xoffset, yoffset, zoffset,
4421 width, height, depth, format, imageSize, data);
4422 }
4423
4424 static mesa_format
4425 get_texbuffer_format(const struct gl_context *ctx, GLenum internalFormat)
4426 {
4427 if (ctx->API != API_OPENGL_CORE) {
4428 switch (internalFormat) {
4429 case GL_ALPHA8:
4430 return MESA_FORMAT_A_UNORM8;
4431 case GL_ALPHA16:
4432 return MESA_FORMAT_A_UNORM16;
4433 case GL_ALPHA16F_ARB:
4434 return MESA_FORMAT_A_FLOAT16;
4435 case GL_ALPHA32F_ARB:
4436 return MESA_FORMAT_A_FLOAT32;
4437 case GL_ALPHA8I_EXT:
4438 return MESA_FORMAT_A_SINT8;
4439 case GL_ALPHA16I_EXT:
4440 return MESA_FORMAT_A_SINT16;
4441 case GL_ALPHA32I_EXT:
4442 return MESA_FORMAT_A_SINT32;
4443 case GL_ALPHA8UI_EXT:
4444 return MESA_FORMAT_A_UINT8;
4445 case GL_ALPHA16UI_EXT:
4446 return MESA_FORMAT_A_UINT16;
4447 case GL_ALPHA32UI_EXT:
4448 return MESA_FORMAT_A_UINT32;
4449 case GL_LUMINANCE8:
4450 return MESA_FORMAT_L_UNORM8;
4451 case GL_LUMINANCE16:
4452 return MESA_FORMAT_L_UNORM16;
4453 case GL_LUMINANCE16F_ARB:
4454 return MESA_FORMAT_L_FLOAT16;
4455 case GL_LUMINANCE32F_ARB:
4456 return MESA_FORMAT_L_FLOAT32;
4457 case GL_LUMINANCE8I_EXT:
4458 return MESA_FORMAT_L_SINT8;
4459 case GL_LUMINANCE16I_EXT:
4460 return MESA_FORMAT_L_SINT16;
4461 case GL_LUMINANCE32I_EXT:
4462 return MESA_FORMAT_L_SINT32;
4463 case GL_LUMINANCE8UI_EXT:
4464 return MESA_FORMAT_L_UINT8;
4465 case GL_LUMINANCE16UI_EXT:
4466 return MESA_FORMAT_L_UINT16;
4467 case GL_LUMINANCE32UI_EXT:
4468 return MESA_FORMAT_L_UINT32;
4469 case GL_LUMINANCE8_ALPHA8:
4470 return MESA_FORMAT_L8A8_UNORM;
4471 case GL_LUMINANCE16_ALPHA16:
4472 return MESA_FORMAT_L16A16_UNORM;
4473 case GL_LUMINANCE_ALPHA16F_ARB:
4474 return MESA_FORMAT_LA_FLOAT16;
4475 case GL_LUMINANCE_ALPHA32F_ARB:
4476 return MESA_FORMAT_LA_FLOAT32;
4477 case GL_LUMINANCE_ALPHA8I_EXT:
4478 return MESA_FORMAT_LA_SINT8;
4479 case GL_LUMINANCE_ALPHA16I_EXT:
4480 return MESA_FORMAT_LA_SINT8;
4481 case GL_LUMINANCE_ALPHA32I_EXT:
4482 return MESA_FORMAT_LA_SINT16;
4483 case GL_LUMINANCE_ALPHA8UI_EXT:
4484 return MESA_FORMAT_LA_UINT8;
4485 case GL_LUMINANCE_ALPHA16UI_EXT:
4486 return MESA_FORMAT_LA_UINT16;
4487 case GL_LUMINANCE_ALPHA32UI_EXT:
4488 return MESA_FORMAT_LA_UINT32;
4489 case GL_INTENSITY8:
4490 return MESA_FORMAT_I_UNORM8;
4491 case GL_INTENSITY16:
4492 return MESA_FORMAT_I_UNORM16;
4493 case GL_INTENSITY16F_ARB:
4494 return MESA_FORMAT_I_FLOAT16;
4495 case GL_INTENSITY32F_ARB:
4496 return MESA_FORMAT_I_FLOAT32;
4497 case GL_INTENSITY8I_EXT:
4498 return MESA_FORMAT_I_SINT8;
4499 case GL_INTENSITY16I_EXT:
4500 return MESA_FORMAT_I_SINT16;
4501 case GL_INTENSITY32I_EXT:
4502 return MESA_FORMAT_I_SINT32;
4503 case GL_INTENSITY8UI_EXT:
4504 return MESA_FORMAT_I_UINT8;
4505 case GL_INTENSITY16UI_EXT:
4506 return MESA_FORMAT_I_UINT16;
4507 case GL_INTENSITY32UI_EXT:
4508 return MESA_FORMAT_I_UINT32;
4509 default:
4510 break;
4511 }
4512 }
4513
4514 if (ctx->API == API_OPENGL_CORE &&
4515 ctx->Extensions.ARB_texture_buffer_object_rgb32) {
4516 switch (internalFormat) {
4517 case GL_RGB32F:
4518 return MESA_FORMAT_RGB_FLOAT32;
4519 case GL_RGB32UI:
4520 return MESA_FORMAT_RGB_UINT32;
4521 case GL_RGB32I:
4522 return MESA_FORMAT_RGB_SINT32;
4523 default:
4524 break;
4525 }
4526 }
4527
4528 switch (internalFormat) {
4529 case GL_RGBA8:
4530 return MESA_FORMAT_R8G8B8A8_UNORM;
4531 case GL_RGBA16:
4532 return MESA_FORMAT_RGBA_UNORM16;
4533 case GL_RGBA16F_ARB:
4534 return MESA_FORMAT_RGBA_FLOAT16;
4535 case GL_RGBA32F_ARB:
4536 return MESA_FORMAT_RGBA_FLOAT32;
4537 case GL_RGBA8I_EXT:
4538 return MESA_FORMAT_RGBA_SINT8;
4539 case GL_RGBA16I_EXT:
4540 return MESA_FORMAT_RGBA_SINT16;
4541 case GL_RGBA32I_EXT:
4542 return MESA_FORMAT_RGBA_SINT32;
4543 case GL_RGBA8UI_EXT:
4544 return MESA_FORMAT_RGBA_UINT8;
4545 case GL_RGBA16UI_EXT:
4546 return MESA_FORMAT_RGBA_UINT16;
4547 case GL_RGBA32UI_EXT:
4548 return MESA_FORMAT_RGBA_UINT32;
4549
4550 case GL_RG8:
4551 return MESA_FORMAT_R8G8_UNORM;
4552 case GL_RG16:
4553 return MESA_FORMAT_R16G16_UNORM;
4554 case GL_RG16F:
4555 return MESA_FORMAT_RG_FLOAT16;
4556 case GL_RG32F:
4557 return MESA_FORMAT_RG_FLOAT32;
4558 case GL_RG8I:
4559 return MESA_FORMAT_RG_SINT8;
4560 case GL_RG16I:
4561 return MESA_FORMAT_RG_SINT16;
4562 case GL_RG32I:
4563 return MESA_FORMAT_RG_SINT32;
4564 case GL_RG8UI:
4565 return MESA_FORMAT_RG_UINT8;
4566 case GL_RG16UI:
4567 return MESA_FORMAT_RG_UINT16;
4568 case GL_RG32UI:
4569 return MESA_FORMAT_RG_UINT32;
4570
4571 case GL_R8:
4572 return MESA_FORMAT_R_UNORM8;
4573 case GL_R16:
4574 return MESA_FORMAT_R_UNORM16;
4575 case GL_R16F:
4576 return MESA_FORMAT_R_FLOAT16;
4577 case GL_R32F:
4578 return MESA_FORMAT_R_FLOAT32;
4579 case GL_R8I:
4580 return MESA_FORMAT_R_SINT8;
4581 case GL_R16I:
4582 return MESA_FORMAT_R_SINT16;
4583 case GL_R32I:
4584 return MESA_FORMAT_R_SINT32;
4585 case GL_R8UI:
4586 return MESA_FORMAT_R_UINT8;
4587 case GL_R16UI:
4588 return MESA_FORMAT_R_UINT16;
4589 case GL_R32UI:
4590 return MESA_FORMAT_R_UINT32;
4591
4592 default:
4593 return MESA_FORMAT_NONE;
4594 }
4595 }
4596
4597
4598 mesa_format
4599 _mesa_validate_texbuffer_format(const struct gl_context *ctx,
4600 GLenum internalFormat)
4601 {
4602 mesa_format format = get_texbuffer_format(ctx, internalFormat);
4603 GLenum datatype;
4604
4605 if (format == MESA_FORMAT_NONE)
4606 return MESA_FORMAT_NONE;
4607
4608 datatype = _mesa_get_format_datatype(format);
4609
4610 /* The GL_ARB_texture_buffer_object spec says:
4611 *
4612 * "If ARB_texture_float is not supported, references to the
4613 * floating-point internal formats provided by that extension should be
4614 * removed, and such formats may not be passed to TexBufferARB."
4615 *
4616 * As a result, GL_HALF_FLOAT internal format depends on both
4617 * GL_ARB_texture_float and GL_ARB_half_float_pixel.
4618 */
4619 if ((datatype == GL_FLOAT || datatype == GL_HALF_FLOAT) &&
4620 !ctx->Extensions.ARB_texture_float)
4621 return MESA_FORMAT_NONE;
4622
4623 if (!ctx->Extensions.ARB_texture_rg) {
4624 GLenum base_format = _mesa_get_format_base_format(format);
4625 if (base_format == GL_R || base_format == GL_RG)
4626 return MESA_FORMAT_NONE;
4627 }
4628
4629 if (!ctx->Extensions.ARB_texture_buffer_object_rgb32) {
4630 GLenum base_format = _mesa_get_format_base_format(format);
4631 if (base_format == GL_RGB)
4632 return MESA_FORMAT_NONE;
4633 }
4634 return format;
4635 }
4636
4637
4638 static void
4639 texbufferrange(struct gl_context *ctx, GLenum target, GLenum internalFormat,
4640 struct gl_buffer_object *bufObj,
4641 GLintptr offset, GLsizeiptr size)
4642 {
4643 struct gl_texture_object *texObj;
4644 mesa_format format;
4645
4646 FLUSH_VERTICES(ctx, 0);
4647
4648 if (target != GL_TEXTURE_BUFFER_ARB) {
4649 _mesa_error(ctx, GL_INVALID_ENUM, "glTexBuffer(target)");
4650 return;
4651 }
4652
4653 format = _mesa_validate_texbuffer_format(ctx, internalFormat);
4654 if (format == MESA_FORMAT_NONE) {
4655 _mesa_error(ctx, GL_INVALID_ENUM, "glTexBuffer(internalFormat 0x%x)",
4656 internalFormat);
4657 return;
4658 }
4659
4660 texObj = _mesa_get_current_tex_object(ctx, target);
4661
4662 _mesa_lock_texture(ctx, texObj);
4663 {
4664 _mesa_reference_buffer_object(ctx, &texObj->BufferObject, bufObj);
4665 texObj->BufferObjectFormat = internalFormat;
4666 texObj->_BufferObjectFormat = format;
4667 texObj->BufferOffset = offset;
4668 texObj->BufferSize = size;
4669 }
4670 _mesa_unlock_texture(ctx, texObj);
4671 }
4672
4673
4674 /** GL_ARB_texture_buffer_object */
4675 void GLAPIENTRY
4676 _mesa_TexBuffer(GLenum target, GLenum internalFormat, GLuint buffer)
4677 {
4678 struct gl_buffer_object *bufObj;
4679
4680 GET_CURRENT_CONTEXT(ctx);
4681
4682 /* NOTE: ARB_texture_buffer_object has interactions with
4683 * the compatibility profile that are not implemented.
4684 */
4685 if (!(ctx->API == API_OPENGL_CORE &&
4686 ctx->Extensions.ARB_texture_buffer_object)) {
4687 _mesa_error(ctx, GL_INVALID_OPERATION, "glTexBuffer");
4688 return;
4689 }
4690
4691 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
4692 if (!bufObj && buffer) {
4693 _mesa_error(ctx, GL_INVALID_OPERATION, "glTexBuffer(buffer %u)", buffer);
4694 return;
4695 }
4696
4697 texbufferrange(ctx, target, internalFormat, bufObj, 0, buffer ? -1 : 0);
4698 }
4699
4700
4701 /** GL_ARB_texture_buffer_range */
4702 void GLAPIENTRY
4703 _mesa_TexBufferRange(GLenum target, GLenum internalFormat, GLuint buffer,
4704 GLintptr offset, GLsizeiptr size)
4705 {
4706 struct gl_buffer_object *bufObj;
4707
4708 GET_CURRENT_CONTEXT(ctx);
4709
4710 if (!(ctx->API == API_OPENGL_CORE &&
4711 ctx->Extensions.ARB_texture_buffer_range)) {
4712 _mesa_error(ctx, GL_INVALID_OPERATION, "glTexBufferRange");
4713 return;
4714 }
4715
4716 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
4717 if (bufObj) {
4718 if (offset < 0 ||
4719 size <= 0 ||
4720 (offset + size) > bufObj->Size) {
4721 _mesa_error(ctx, GL_INVALID_VALUE, "glTexBufferRange");
4722 return;
4723 }
4724 if (offset % ctx->Const.TextureBufferOffsetAlignment) {
4725 _mesa_error(ctx, GL_INVALID_VALUE,
4726 "glTexBufferRange(invalid offset alignment)");
4727 return;
4728 }
4729 } else if (buffer) {
4730 _mesa_error(ctx, GL_INVALID_OPERATION, "glTexBufferRange(buffer %u)",
4731 buffer);
4732 return;
4733 } else {
4734 offset = 0;
4735 size = 0;
4736 }
4737
4738 texbufferrange(ctx, target, internalFormat, bufObj, offset, size);
4739 }
4740
4741
4742 static GLboolean
4743 is_renderable_texture_format(struct gl_context *ctx, GLenum internalformat)
4744 {
4745 /* Everything that is allowed for renderbuffers,
4746 * except for a base format of GL_STENCIL_INDEX.
4747 */
4748 GLenum baseFormat = _mesa_base_fbo_format(ctx, internalformat);
4749 return baseFormat != 0 && baseFormat != GL_STENCIL_INDEX;
4750 }
4751
4752
4753 /** GL_ARB_texture_multisample */
4754 static GLboolean
4755 check_multisample_target(GLuint dims, GLenum target)
4756 {
4757 switch(target) {
4758 case GL_TEXTURE_2D_MULTISAMPLE:
4759 case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
4760 return dims == 2;
4761
4762 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
4763 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
4764 return dims == 3;
4765
4766 default:
4767 return GL_FALSE;
4768 }
4769 }
4770
4771
4772 static void
4773 teximagemultisample(GLuint dims, GLenum target, GLsizei samples,
4774 GLint internalformat, GLsizei width, GLsizei height,
4775 GLsizei depth, GLboolean fixedsamplelocations,
4776 GLboolean immutable, const char *func)
4777 {
4778 struct gl_texture_object *texObj;
4779 struct gl_texture_image *texImage;
4780 GLboolean sizeOK, dimensionsOK, samplesOK;
4781 mesa_format texFormat;
4782 GLenum sample_count_error;
4783
4784 GET_CURRENT_CONTEXT(ctx);
4785
4786 if (!(ctx->Extensions.ARB_texture_multisample
4787 && _mesa_is_desktop_gl(ctx))) {
4788 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(unsupported)", func);
4789 return;
4790 }
4791
4792 if (!check_multisample_target(dims, target)) {
4793 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", func);
4794 return;
4795 }
4796
4797 /* check that the specified internalformat is color/depth/stencil-renderable;
4798 * refer GL3.1 spec 4.4.4
4799 */
4800
4801 if (immutable && !_mesa_is_legal_tex_storage_format(ctx, internalformat)) {
4802 _mesa_error(ctx, GL_INVALID_ENUM,
4803 "%s(internalformat=%s not legal for immutable-format)",
4804 func, _mesa_lookup_enum_by_nr(internalformat));
4805 return;
4806 }
4807
4808 if (!is_renderable_texture_format(ctx, internalformat)) {
4809 _mesa_error(ctx, GL_INVALID_OPERATION,
4810 "%s(internalformat=%s)",
4811 func, _mesa_lookup_enum_by_nr(internalformat));
4812 return;
4813 }
4814
4815 sample_count_error = _mesa_check_sample_count(ctx, target,
4816 internalformat, samples);
4817 samplesOK = sample_count_error == GL_NO_ERROR;
4818
4819 /* Page 254 of OpenGL 4.4 spec says:
4820 * "Proxy arrays for two-dimensional multisample and two-dimensional
4821 * multisample array textures are operated on in the same way when
4822 * TexImage2DMultisample is called with target specified as
4823 * PROXY_TEXTURE_2D_MULTISAMPLE, or TexImage3DMultisample is called
4824 * with target specified as PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY.
4825 * However, if samples is not supported, then no error is generated.
4826 */
4827 if (!samplesOK && !_mesa_is_proxy_texture(target)) {
4828 _mesa_error(ctx, sample_count_error, "%s(samples)", func);
4829 return;
4830 }
4831
4832 texObj = _mesa_get_current_tex_object(ctx, target);
4833
4834 if (immutable && (!texObj || (texObj->Name == 0))) {
4835 _mesa_error(ctx, GL_INVALID_OPERATION,
4836 "%s(texture object 0)",
4837 func);
4838 return;
4839 }
4840
4841 texImage = _mesa_get_tex_image(ctx, texObj, 0, 0);
4842
4843 if (texImage == NULL) {
4844 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s()", func);
4845 return;
4846 }
4847
4848 texFormat = _mesa_choose_texture_format(ctx, texObj, target, 0,
4849 internalformat, GL_NONE, GL_NONE);
4850 assert(texFormat != MESA_FORMAT_NONE);
4851
4852 dimensionsOK = _mesa_legal_texture_dimensions(ctx, target, 0,
4853 width, height, depth, 0);
4854
4855 sizeOK = ctx->Driver.TestProxyTexImage(ctx, target, 0, texFormat,
4856 width, height, depth, 0);
4857
4858 if (_mesa_is_proxy_texture(target)) {
4859 if (samplesOK && dimensionsOK && sizeOK) {
4860 init_teximage_fields_ms(ctx, texImage, width, height, depth, 0,
4861 internalformat, texFormat,
4862 samples, fixedsamplelocations);
4863 }
4864 else {
4865 /* clear all image fields */
4866 clear_teximage_fields(texImage);
4867 }
4868 }
4869 else {
4870 if (!dimensionsOK) {
4871 _mesa_error(ctx, GL_INVALID_VALUE,
4872 "%s(invalid width or height)", func);
4873 return;
4874 }
4875
4876 if (!sizeOK) {
4877 _mesa_error(ctx, GL_OUT_OF_MEMORY,
4878 "%s(texture too large)", func);
4879 return;
4880 }
4881
4882 /* Check if texObj->Immutable is set */
4883 if (texObj->Immutable) {
4884 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(immutable)", func);
4885 return;
4886 }
4887
4888 ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
4889
4890 init_teximage_fields_ms(ctx, texImage, width, height, depth, 0,
4891 internalformat, texFormat,
4892 samples, fixedsamplelocations);
4893
4894 if (width > 0 && height > 0 && depth > 0) {
4895 if (!ctx->Driver.AllocTextureStorage(ctx, texObj, 1,
4896 width, height, depth)) {
4897 /* tidy up the texture image state. strictly speaking,
4898 * we're allowed to just leave this in whatever state we
4899 * like, but being tidy is good.
4900 */
4901 _mesa_init_teximage_fields(ctx, texImage,
4902 0, 0, 0, 0, GL_NONE, MESA_FORMAT_NONE);
4903 }
4904 }
4905
4906 texObj->Immutable = immutable;
4907
4908 if (immutable) {
4909 _mesa_set_texture_view_state(ctx, texObj, target, 1);
4910 }
4911
4912 _mesa_update_fbo_texture(ctx, texObj, 0, 0);
4913 }
4914 }
4915
4916
4917 void GLAPIENTRY
4918 _mesa_TexImage2DMultisample(GLenum target, GLsizei samples,
4919 GLenum internalformat, GLsizei width,
4920 GLsizei height, GLboolean fixedsamplelocations)
4921 {
4922 teximagemultisample(2, target, samples, internalformat,
4923 width, height, 1, fixedsamplelocations, GL_FALSE,
4924 "glTexImage2DMultisample");
4925 }
4926
4927
4928 void GLAPIENTRY
4929 _mesa_TexImage3DMultisample(GLenum target, GLsizei samples,
4930 GLenum internalformat, GLsizei width,
4931 GLsizei height, GLsizei depth,
4932 GLboolean fixedsamplelocations)
4933 {
4934 teximagemultisample(3, target, samples, internalformat,
4935 width, height, depth, fixedsamplelocations, GL_FALSE,
4936 "glTexImage3DMultisample");
4937 }
4938
4939
4940 void GLAPIENTRY
4941 _mesa_TexStorage2DMultisample(GLenum target, GLsizei samples,
4942 GLenum internalformat, GLsizei width,
4943 GLsizei height, GLboolean fixedsamplelocations)
4944 {
4945 teximagemultisample(2, target, samples, internalformat,
4946 width, height, 1, fixedsamplelocations, GL_TRUE,
4947 "glTexStorage2DMultisample");
4948 }
4949
4950 void GLAPIENTRY
4951 _mesa_TexStorage3DMultisample(GLenum target, GLsizei samples,
4952 GLenum internalformat, GLsizei width,
4953 GLsizei height, GLsizei depth,
4954 GLboolean fixedsamplelocations)
4955 {
4956 teximagemultisample(3, target, samples, internalformat,
4957 width, height, depth, fixedsamplelocations, GL_TRUE,
4958 "glTexStorage3DMultisample");
4959 }