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