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