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