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