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