mesa: Fix valid texture target test in _mesa_GetTexLevelParameteriv()
[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 * 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(GLint *border,
2433 GLint *width, GLint *height, GLint *depth,
2434 const struct gl_pixelstore_attrib *unpack,
2435 struct gl_pixelstore_attrib *unpackNew)
2436 {
2437 assert(*border > 0); /* sanity check */
2438
2439 *unpackNew = *unpack;
2440
2441 if (unpackNew->RowLength == 0)
2442 unpackNew->RowLength = *width;
2443
2444 if (depth && unpackNew->ImageHeight == 0)
2445 unpackNew->ImageHeight = *height;
2446
2447 unpackNew->SkipPixels += *border;
2448 if (height)
2449 unpackNew->SkipRows += *border;
2450 if (depth)
2451 unpackNew->SkipImages += *border;
2452
2453 assert(*width >= 3);
2454 *width = *width - 2 * *border;
2455 if (height && *height >= 3)
2456 *height = *height - 2 * *border;
2457 if (depth && *depth >= 3)
2458 *depth = *depth - 2 * *border;
2459 *border = 0;
2460 }
2461
2462 /**
2463 * Common code to implement all the glTexImage1D/2D/3D functions.
2464 */
2465 static void
2466 teximage(struct gl_context *ctx, GLuint dims,
2467 GLenum target, GLint level, GLint internalFormat,
2468 GLsizei width, GLsizei height, GLsizei depth,
2469 GLint border, GLenum format, GLenum type,
2470 const GLvoid *pixels)
2471 {
2472 GLboolean error;
2473 struct gl_pixelstore_attrib unpack_no_border;
2474 const struct gl_pixelstore_attrib *unpack = &ctx->Unpack;
2475
2476 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2477
2478 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2479 _mesa_debug(ctx, "glTexImage%uD %s %d %s %d %d %d %d %s %s %p\n",
2480 dims,
2481 _mesa_lookup_enum_by_nr(target), level,
2482 _mesa_lookup_enum_by_nr(internalFormat),
2483 width, height, depth, border,
2484 _mesa_lookup_enum_by_nr(format),
2485 _mesa_lookup_enum_by_nr(type), pixels);
2486
2487 internalFormat = override_internal_format(internalFormat, width, height);
2488
2489 /* target error checking */
2490 if (!legal_teximage_target(ctx, dims, target)) {
2491 _mesa_error(ctx, GL_INVALID_ENUM, "glTexImage%uD(target=%s)",
2492 dims, _mesa_lookup_enum_by_nr(target));
2493 return;
2494 }
2495
2496 /* general error checking */
2497 error = texture_error_check(ctx, dims, target, level, internalFormat,
2498 format, type, width, height, depth, border);
2499
2500 if (_mesa_is_proxy_texture(target)) {
2501 /* Proxy texture: just clear or set state depending on error checking */
2502 struct gl_texture_image *texImage =
2503 _mesa_get_proxy_tex_image(ctx, target, level);
2504
2505 if (error) {
2506 /* when error, clear all proxy texture image parameters */
2507 if (texImage)
2508 clear_teximage_fields(texImage);
2509 }
2510 else {
2511 /* no error, set the tex image parameters */
2512 struct gl_texture_object *texObj =
2513 _mesa_get_current_tex_object(ctx, target);
2514 gl_format texFormat = _mesa_choose_texture_format(ctx, texObj,
2515 target, level,
2516 internalFormat,
2517 format, type);
2518
2519 if (legal_texture_size(ctx, texFormat, width, height, depth)) {
2520 _mesa_init_teximage_fields(ctx, texImage, width, height,
2521 depth, border, internalFormat,
2522 texFormat);
2523 }
2524 else if (texImage) {
2525 clear_teximage_fields(texImage);
2526 }
2527 }
2528 }
2529 else {
2530 /* non-proxy target */
2531 const GLuint face = _mesa_tex_target_to_face(target);
2532 struct gl_texture_object *texObj;
2533 struct gl_texture_image *texImage;
2534
2535 if (error) {
2536 return; /* error was recorded */
2537 }
2538
2539 /* Allow a hardware driver to just strip out the border, to provide
2540 * reliable but slightly incorrect hardware rendering instead of
2541 * rarely-tested software fallback rendering.
2542 */
2543 if (border && ctx->Const.StripTextureBorder) {
2544 strip_texture_border(&border, &width, &height, &depth, unpack,
2545 &unpack_no_border);
2546 unpack = &unpack_no_border;
2547 }
2548
2549 if (ctx->NewState & _NEW_PIXEL)
2550 _mesa_update_state(ctx);
2551
2552 texObj = _mesa_get_current_tex_object(ctx, target);
2553
2554 _mesa_lock_texture(ctx, texObj);
2555 {
2556 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
2557
2558 if (!texImage) {
2559 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage%uD", dims);
2560 }
2561 else {
2562 gl_format texFormat;
2563
2564 ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
2565
2566 texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
2567 internalFormat, format,
2568 type);
2569
2570 if (legal_texture_size(ctx, texFormat, width, height, depth)) {
2571 _mesa_init_teximage_fields(ctx, texImage,
2572 width, height, depth,
2573 border, internalFormat, texFormat);
2574
2575 /* Give the texture to the driver. <pixels> may be null. */
2576 ASSERT(ctx->Driver.TexImage3D);
2577 switch (dims) {
2578 case 1:
2579 ctx->Driver.TexImage1D(ctx, texImage, internalFormat,
2580 width, border, format,
2581 type, pixels, unpack);
2582 break;
2583 case 2:
2584 ctx->Driver.TexImage2D(ctx, texImage, internalFormat,
2585 width, height, border, format,
2586 type, pixels, unpack);
2587 break;
2588 case 3:
2589 ctx->Driver.TexImage3D(ctx, texImage, internalFormat,
2590 width, height, depth, border, format,
2591 type, pixels, unpack);
2592 break;
2593 default:
2594 _mesa_problem(ctx, "invalid dims=%u in teximage()", dims);
2595 }
2596
2597 check_gen_mipmap(ctx, target, texObj, level);
2598
2599 _mesa_update_fbo_texture(ctx, texObj, face, level);
2600
2601 /* state update */
2602 texObj->_Complete = GL_FALSE;
2603 ctx->NewState |= _NEW_TEXTURE;
2604 }
2605 else {
2606 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage%uD", dims);
2607 }
2608 }
2609 }
2610 _mesa_unlock_texture(ctx, texObj);
2611 }
2612 }
2613
2614
2615 /*
2616 * Called from the API. Note that width includes the border.
2617 */
2618 void GLAPIENTRY
2619 _mesa_TexImage1D( GLenum target, GLint level, GLint internalFormat,
2620 GLsizei width, GLint border, GLenum format,
2621 GLenum type, const GLvoid *pixels )
2622 {
2623 GET_CURRENT_CONTEXT(ctx);
2624 teximage(ctx, 1, target, level, internalFormat, width, 1, 1,
2625 border, format, type, pixels);
2626 }
2627
2628
2629 void GLAPIENTRY
2630 _mesa_TexImage2D( GLenum target, GLint level, GLint internalFormat,
2631 GLsizei width, GLsizei height, GLint border,
2632 GLenum format, GLenum type,
2633 const GLvoid *pixels )
2634 {
2635 GET_CURRENT_CONTEXT(ctx);
2636 teximage(ctx, 2, target, level, internalFormat, width, height, 1,
2637 border, format, type, pixels);
2638 }
2639
2640
2641 /*
2642 * Called by the API or display list executor.
2643 * Note that width and height include the border.
2644 */
2645 void GLAPIENTRY
2646 _mesa_TexImage3D( GLenum target, GLint level, GLint internalFormat,
2647 GLsizei width, GLsizei height, GLsizei depth,
2648 GLint border, GLenum format, GLenum type,
2649 const GLvoid *pixels )
2650 {
2651 GET_CURRENT_CONTEXT(ctx);
2652 teximage(ctx, 3, target, level, internalFormat, width, height, depth,
2653 border, format, type, pixels);
2654 }
2655
2656
2657 void GLAPIENTRY
2658 _mesa_TexImage3DEXT( GLenum target, GLint level, GLenum internalFormat,
2659 GLsizei width, GLsizei height, GLsizei depth,
2660 GLint border, GLenum format, GLenum type,
2661 const GLvoid *pixels )
2662 {
2663 _mesa_TexImage3D(target, level, (GLint) internalFormat, width, height,
2664 depth, border, format, type, pixels);
2665 }
2666
2667
2668 #if FEATURE_OES_EGL_image
2669 void GLAPIENTRY
2670 _mesa_EGLImageTargetTexture2DOES (GLenum target, GLeglImageOES image)
2671 {
2672 struct gl_texture_object *texObj;
2673 struct gl_texture_image *texImage;
2674 GET_CURRENT_CONTEXT(ctx);
2675 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2676
2677 if ((target == GL_TEXTURE_2D &&
2678 !ctx->Extensions.OES_EGL_image) ||
2679 (target == GL_TEXTURE_EXTERNAL_OES &&
2680 !ctx->Extensions.OES_EGL_image_external)) {
2681 _mesa_error(ctx, GL_INVALID_ENUM,
2682 "glEGLImageTargetTexture2D(target=%d)", target);
2683 return;
2684 }
2685
2686 if (ctx->NewState & _NEW_PIXEL)
2687 _mesa_update_state(ctx);
2688
2689 texObj = _mesa_get_current_tex_object(ctx, target);
2690 _mesa_lock_texture(ctx, texObj);
2691
2692 texImage = _mesa_get_tex_image(ctx, texObj, target, 0);
2693 if (!texImage) {
2694 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glEGLImageTargetTexture2D");
2695 } else {
2696 ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
2697
2698 ctx->Driver.EGLImageTargetTexture2D(ctx, target,
2699 texObj, texImage, image);
2700
2701 /* state update */
2702 texObj->_Complete = GL_FALSE;
2703 ctx->NewState |= _NEW_TEXTURE;
2704 }
2705 _mesa_unlock_texture(ctx, texObj);
2706
2707 }
2708 #endif
2709
2710
2711
2712 /**
2713 * Implement all the glTexSubImage1/2/3D() functions.
2714 */
2715 static void
2716 texsubimage(struct gl_context *ctx, GLuint dims, GLenum target, GLint level,
2717 GLint xoffset, GLint yoffset, GLint zoffset,
2718 GLsizei width, GLsizei height, GLsizei depth,
2719 GLenum format, GLenum type, const GLvoid *pixels )
2720 {
2721 struct gl_texture_object *texObj;
2722 struct gl_texture_image *texImage;
2723
2724 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2725
2726 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2727 _mesa_debug(ctx, "glTexSubImage%uD %s %d %d %d %d %d %d %d %s %s %p\n",
2728 dims,
2729 _mesa_lookup_enum_by_nr(target), level,
2730 xoffset, yoffset, zoffset, width, height, depth,
2731 _mesa_lookup_enum_by_nr(format),
2732 _mesa_lookup_enum_by_nr(type), pixels);
2733
2734 /* check target (proxies not allowed) */
2735 if (!legal_texsubimage_target(ctx, dims, target)) {
2736 _mesa_error(ctx, GL_INVALID_ENUM, "glTexSubImage%uD(target=%s)",
2737 dims, _mesa_lookup_enum_by_nr(target));
2738 return;
2739 }
2740
2741 if (ctx->NewState & _NEW_PIXEL)
2742 _mesa_update_state(ctx);
2743
2744 if (subtexture_error_check(ctx, dims, target, level, xoffset, yoffset, zoffset,
2745 width, height, depth, format, type)) {
2746 return; /* error was detected */
2747 }
2748
2749 texObj = _mesa_get_current_tex_object(ctx, target);
2750
2751 _mesa_lock_texture(ctx, texObj);
2752 {
2753 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
2754
2755 if (subtexture_error_check2(ctx, dims, target, level,
2756 xoffset, yoffset, zoffset,
2757 width, height, depth,
2758 format, type, texImage)) {
2759 /* error was recorded */
2760 }
2761 else if (width > 0 && height > 0 && depth > 0) {
2762 /* If we have a border, offset=-1 is legal. Bias by border width. */
2763 switch (dims) {
2764 case 3:
2765 if (target != GL_TEXTURE_2D_ARRAY)
2766 zoffset += texImage->Border;
2767 /* fall-through */
2768 case 2:
2769 if (target != GL_TEXTURE_1D_ARRAY)
2770 yoffset += texImage->Border;
2771 /* fall-through */
2772 case 1:
2773 xoffset += texImage->Border;
2774 }
2775
2776 switch (dims) {
2777 case 1:
2778 ctx->Driver.TexSubImage1D(ctx, texImage,
2779 xoffset, width,
2780 format, type, pixels, &ctx->Unpack);
2781 break;
2782 case 2:
2783 ctx->Driver.TexSubImage2D(ctx, texImage,
2784 xoffset, yoffset, width, height,
2785 format, type, pixels, &ctx->Unpack);
2786 break;
2787 case 3:
2788 ctx->Driver.TexSubImage3D(ctx, texImage,
2789 xoffset, yoffset, zoffset,
2790 width, height, depth,
2791 format, type, pixels, &ctx->Unpack);
2792 break;
2793 default:
2794 _mesa_problem(ctx, "unexpected dims in subteximage()");
2795 }
2796
2797 check_gen_mipmap(ctx, target, texObj, level);
2798
2799 ctx->NewState |= _NEW_TEXTURE;
2800 }
2801 }
2802 _mesa_unlock_texture(ctx, texObj);
2803 }
2804
2805
2806 void GLAPIENTRY
2807 _mesa_TexSubImage1D( GLenum target, GLint level,
2808 GLint xoffset, GLsizei width,
2809 GLenum format, GLenum type,
2810 const GLvoid *pixels )
2811 {
2812 GET_CURRENT_CONTEXT(ctx);
2813 texsubimage(ctx, 1, target, level,
2814 xoffset, 0, 0,
2815 width, 1, 1,
2816 format, type, pixels);
2817 }
2818
2819
2820 void GLAPIENTRY
2821 _mesa_TexSubImage2D( GLenum target, GLint level,
2822 GLint xoffset, GLint yoffset,
2823 GLsizei width, GLsizei height,
2824 GLenum format, GLenum type,
2825 const GLvoid *pixels )
2826 {
2827 GET_CURRENT_CONTEXT(ctx);
2828 texsubimage(ctx, 2, target, level,
2829 xoffset, yoffset, 0,
2830 width, height, 1,
2831 format, type, pixels);
2832 }
2833
2834
2835
2836 void GLAPIENTRY
2837 _mesa_TexSubImage3D( GLenum target, GLint level,
2838 GLint xoffset, GLint yoffset, GLint zoffset,
2839 GLsizei width, GLsizei height, GLsizei depth,
2840 GLenum format, GLenum type,
2841 const GLvoid *pixels )
2842 {
2843 GET_CURRENT_CONTEXT(ctx);
2844 texsubimage(ctx, 3, target, level,
2845 xoffset, yoffset, zoffset,
2846 width, height, depth,
2847 format, type, pixels);
2848 }
2849
2850
2851
2852 /**
2853 * For glCopyTexSubImage, return the source renderbuffer to copy texel data
2854 * from. This depends on whether the texture contains color or depth values.
2855 */
2856 static struct gl_renderbuffer *
2857 get_copy_tex_image_source(struct gl_context *ctx, gl_format texFormat)
2858 {
2859 if (_mesa_get_format_bits(texFormat, GL_DEPTH_BITS) > 0) {
2860 /* reading from depth/stencil buffer */
2861 return ctx->ReadBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
2862 }
2863 else {
2864 /* copying from color buffer */
2865 return ctx->ReadBuffer->_ColorReadBuffer;
2866 }
2867 }
2868
2869
2870
2871 /**
2872 * Implement the glCopyTexImage1/2D() functions.
2873 */
2874 static void
2875 copyteximage(struct gl_context *ctx, GLuint dims,
2876 GLenum target, GLint level, GLenum internalFormat,
2877 GLint x, GLint y, GLsizei width, GLsizei height, GLint border )
2878 {
2879 struct gl_texture_object *texObj;
2880 struct gl_texture_image *texImage;
2881 const GLuint face = _mesa_tex_target_to_face(target);
2882
2883 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2884
2885 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2886 _mesa_debug(ctx, "glCopyTexImage%uD %s %d %s %d %d %d %d %d\n",
2887 dims,
2888 _mesa_lookup_enum_by_nr(target), level,
2889 _mesa_lookup_enum_by_nr(internalFormat),
2890 x, y, width, height, border);
2891
2892 if (ctx->NewState & NEW_COPY_TEX_STATE)
2893 _mesa_update_state(ctx);
2894
2895 if (copytexture_error_check(ctx, dims, target, level, internalFormat,
2896 width, height, border))
2897 return;
2898
2899 texObj = _mesa_get_current_tex_object(ctx, target);
2900
2901 if (border && ctx->Const.StripTextureBorder) {
2902 x += border;
2903 width -= border * 2;
2904 if (dims == 2) {
2905 y += border;
2906 height -= border * 2;
2907 }
2908 border = 0;
2909 }
2910
2911 _mesa_lock_texture(ctx, texObj);
2912 {
2913 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
2914
2915 if (!texImage) {
2916 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage%uD", dims);
2917 }
2918 else {
2919 /* choose actual hw format */
2920 gl_format texFormat = _mesa_choose_texture_format(ctx, texObj,
2921 target, level,
2922 internalFormat,
2923 GL_NONE, GL_NONE);
2924
2925 if (legal_texture_size(ctx, texFormat, width, height, 1)) {
2926 GLint srcX = x, srcY = y, dstX = 0, dstY = 0;
2927
2928 /* Free old texture image */
2929 ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
2930
2931 _mesa_init_teximage_fields(ctx, texImage, width, height, 1,
2932 border, internalFormat, texFormat);
2933
2934 /* Allocate texture memory (no pixel data yet) */
2935 if (dims == 1) {
2936 ctx->Driver.TexImage1D(ctx, texImage, internalFormat,
2937 width, border, GL_NONE, GL_NONE, NULL,
2938 &ctx->Unpack);
2939 }
2940 else {
2941 ctx->Driver.TexImage2D(ctx, texImage, internalFormat,
2942 width, height, border, GL_NONE, GL_NONE,
2943 NULL, &ctx->Unpack);
2944 }
2945
2946 if (_mesa_clip_copytexsubimage(ctx, &dstX, &dstY, &srcX, &srcY,
2947 &width, &height)) {
2948 struct gl_renderbuffer *srcRb =
2949 get_copy_tex_image_source(ctx, texImage->TexFormat);
2950
2951 if (dims == 1)
2952 ctx->Driver.CopyTexSubImage1D(ctx, texImage, dstX,
2953 srcRb, srcX, srcY, width);
2954
2955 else
2956 ctx->Driver.CopyTexSubImage2D(ctx, texImage, dstX, dstY,
2957 srcRb, srcX, srcY, width, height);
2958 }
2959
2960 check_gen_mipmap(ctx, target, texObj, level);
2961
2962 _mesa_update_fbo_texture(ctx, texObj, face, level);
2963
2964 /* state update */
2965 texObj->_Complete = GL_FALSE;
2966 ctx->NewState |= _NEW_TEXTURE;
2967 }
2968 else {
2969 /* probably too large of image */
2970 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage%uD", dims);
2971 }
2972 }
2973 }
2974 _mesa_unlock_texture(ctx, texObj);
2975 }
2976
2977
2978
2979 void GLAPIENTRY
2980 _mesa_CopyTexImage1D( GLenum target, GLint level,
2981 GLenum internalFormat,
2982 GLint x, GLint y,
2983 GLsizei width, GLint border )
2984 {
2985 GET_CURRENT_CONTEXT(ctx);
2986 copyteximage(ctx, 1, target, level, internalFormat, x, y, width, 1, border);
2987 }
2988
2989
2990
2991 void GLAPIENTRY
2992 _mesa_CopyTexImage2D( GLenum target, GLint level, GLenum internalFormat,
2993 GLint x, GLint y, GLsizei width, GLsizei height,
2994 GLint border )
2995 {
2996 GET_CURRENT_CONTEXT(ctx);
2997 copyteximage(ctx, 2, target, level, internalFormat,
2998 x, y, width, height, border);
2999 }
3000
3001
3002
3003 /**
3004 * Implementation for glCopyTexSubImage1/2/3D() functions.
3005 */
3006 static void
3007 copytexsubimage(struct gl_context *ctx, GLuint dims, GLenum target, GLint level,
3008 GLint xoffset, GLint yoffset, GLint zoffset,
3009 GLint x, GLint y, GLsizei width, GLsizei height)
3010 {
3011 struct gl_texture_object *texObj;
3012 struct gl_texture_image *texImage;
3013
3014 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3015
3016 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3017 _mesa_debug(ctx, "glCopyTexSubImage%uD %s %d %d %d %d %d %d %d %d\n",
3018 dims,
3019 _mesa_lookup_enum_by_nr(target),
3020 level, xoffset, yoffset, zoffset, x, y, width, height);
3021
3022 if (ctx->NewState & NEW_COPY_TEX_STATE)
3023 _mesa_update_state(ctx);
3024
3025 if (copytexsubimage_error_check1(ctx, dims, target, level))
3026 return;
3027
3028 texObj = _mesa_get_current_tex_object(ctx, target);
3029
3030 _mesa_lock_texture(ctx, texObj);
3031 {
3032 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3033
3034 if (copytexsubimage_error_check2(ctx, dims, target, level, xoffset, yoffset,
3035 zoffset, width, height, texImage)) {
3036 /* error was recored */
3037 }
3038 else {
3039 /* If we have a border, offset=-1 is legal. Bias by border width. */
3040 switch (dims) {
3041 case 3:
3042 if (target != GL_TEXTURE_2D_ARRAY)
3043 zoffset += texImage->Border;
3044 /* fall-through */
3045 case 2:
3046 if (target != GL_TEXTURE_1D_ARRAY)
3047 yoffset += texImage->Border;
3048 /* fall-through */
3049 case 1:
3050 xoffset += texImage->Border;
3051 }
3052
3053 if (_mesa_clip_copytexsubimage(ctx, &xoffset, &yoffset, &x, &y,
3054 &width, &height)) {
3055 struct gl_renderbuffer *srcRb =
3056 get_copy_tex_image_source(ctx, texImage->TexFormat);
3057
3058 switch (dims) {
3059 case 1:
3060 ctx->Driver.CopyTexSubImage1D(ctx, texImage, xoffset,
3061 srcRb, x, y, width);
3062 break;
3063 case 2:
3064 ctx->Driver.CopyTexSubImage2D(ctx, texImage, xoffset, yoffset,
3065 srcRb, x, y, width, height);
3066 break;
3067 case 3:
3068 ctx->Driver.CopyTexSubImage3D(ctx, texImage,
3069 xoffset, yoffset, zoffset,
3070 srcRb, x, y, width, height);
3071 break;
3072 default:
3073 _mesa_problem(ctx, "bad dims in copytexsubimage()");
3074 }
3075
3076 check_gen_mipmap(ctx, target, texObj, level);
3077
3078 ctx->NewState |= _NEW_TEXTURE;
3079 }
3080 }
3081 }
3082 _mesa_unlock_texture(ctx, texObj);
3083 }
3084
3085
3086 void GLAPIENTRY
3087 _mesa_CopyTexSubImage1D( GLenum target, GLint level,
3088 GLint xoffset, GLint x, GLint y, GLsizei width )
3089 {
3090 GET_CURRENT_CONTEXT(ctx);
3091 copytexsubimage(ctx, 1, target, level, xoffset, 0, 0, x, y, width, 1);
3092 }
3093
3094
3095
3096 void GLAPIENTRY
3097 _mesa_CopyTexSubImage2D( GLenum target, GLint level,
3098 GLint xoffset, GLint yoffset,
3099 GLint x, GLint y, GLsizei width, GLsizei height )
3100 {
3101 GET_CURRENT_CONTEXT(ctx);
3102 copytexsubimage(ctx, 2, target, level, xoffset, yoffset, 0, x, y,
3103 width, height);
3104 }
3105
3106
3107
3108 void GLAPIENTRY
3109 _mesa_CopyTexSubImage3D( GLenum target, GLint level,
3110 GLint xoffset, GLint yoffset, GLint zoffset,
3111 GLint x, GLint y, GLsizei width, GLsizei height )
3112 {
3113 GET_CURRENT_CONTEXT(ctx);
3114 copytexsubimage(ctx, 3, target, level, xoffset, yoffset, zoffset,
3115 x, y, width, height);
3116 }
3117
3118
3119
3120
3121 /**********************************************************************/
3122 /****** Compressed Textures ******/
3123 /**********************************************************************/
3124
3125
3126 /**
3127 * Return expected size of a compressed texture.
3128 */
3129 static GLuint
3130 compressed_tex_size(GLsizei width, GLsizei height, GLsizei depth,
3131 GLenum glformat)
3132 {
3133 gl_format mesaFormat = _mesa_glenum_to_compressed_format(glformat);
3134 return _mesa_format_image_size(mesaFormat, width, height, depth);
3135 }
3136
3137
3138 /*
3139 * Return compressed texture block size, in pixels.
3140 */
3141 static void
3142 get_compressed_block_size(GLenum glformat, GLuint *bw, GLuint *bh)
3143 {
3144 gl_format mesaFormat = _mesa_glenum_to_compressed_format(glformat);
3145 _mesa_get_format_block_size(mesaFormat, bw, bh);
3146 }
3147
3148
3149 /**
3150 * Error checking for glCompressedTexImage[123]D().
3151 * \param reason returns reason for error, if any
3152 * \return error code or GL_NO_ERROR.
3153 */
3154 static GLenum
3155 compressed_texture_error_check(struct gl_context *ctx, GLint dimensions,
3156 GLenum target, GLint level,
3157 GLenum internalFormat, GLsizei width,
3158 GLsizei height, GLsizei depth, GLint border,
3159 GLsizei imageSize, char **reason)
3160 {
3161 const GLenum proxyTarget = get_proxy_target(target);
3162 const GLint maxLevels = _mesa_max_texture_levels(ctx, target);
3163 GLint expectedSize;
3164 GLenum choose_format;
3165 GLenum choose_type;
3166 GLenum proxy_format;
3167
3168 *reason = ""; /* no error */
3169
3170 if (!target_can_be_compressed(ctx, target, internalFormat)) {
3171 *reason = "target";
3172 return GL_INVALID_ENUM;
3173 }
3174
3175 /* This will detect any invalid internalFormat value */
3176 if (!_mesa_is_compressed_format(ctx, internalFormat)) {
3177 *reason = "internalFormat";
3178 return GL_INVALID_ENUM;
3179 }
3180
3181 switch (internalFormat) {
3182 #if FEATURE_ES
3183 case GL_PALETTE4_RGB8_OES:
3184 case GL_PALETTE4_RGBA8_OES:
3185 case GL_PALETTE4_R5_G6_B5_OES:
3186 case GL_PALETTE4_RGBA4_OES:
3187 case GL_PALETTE4_RGB5_A1_OES:
3188 case GL_PALETTE8_RGB8_OES:
3189 case GL_PALETTE8_RGBA8_OES:
3190 case GL_PALETTE8_R5_G6_B5_OES:
3191 case GL_PALETTE8_RGBA4_OES:
3192 case GL_PALETTE8_RGB5_A1_OES:
3193 _mesa_cpal_compressed_format_type(internalFormat, &choose_format,
3194 &choose_type);
3195 proxy_format = choose_format;
3196
3197 /* check level */
3198 if (level > 0 || level < -maxLevels) {
3199 *reason = "level";
3200 return GL_INVALID_VALUE;
3201 }
3202
3203 if (dimensions != 2) {
3204 *reason = "compressed paletted textures must be 2D";
3205 return GL_INVALID_OPERATION;
3206 }
3207
3208 /* Figure out the expected texture size (in bytes). This will be
3209 * checked against the actual size later.
3210 */
3211 expectedSize = _mesa_cpal_compressed_size(level, internalFormat,
3212 width, height);
3213
3214 /* This is for the benefit of the TestProxyTexImage below. It expects
3215 * level to be non-negative. OES_compressed_paletted_texture uses a
3216 * weird mechanism where the level specified to glCompressedTexImage2D
3217 * is -(n-1) number of levels in the texture, and the data specifies the
3218 * complete mipmap stack. This is done to ensure the palette is the
3219 * same for all levels.
3220 */
3221 level = -level;
3222 break;
3223 #endif
3224
3225 default:
3226 choose_format = GL_NONE;
3227 choose_type = GL_NONE;
3228 proxy_format = internalFormat;
3229
3230 /* check level */
3231 if (level < 0 || level >= maxLevels) {
3232 *reason = "level";
3233 return GL_INVALID_VALUE;
3234 }
3235
3236 /* Figure out the expected texture size (in bytes). This will be
3237 * checked against the actual size later.
3238 */
3239 expectedSize = compressed_tex_size(width, height, depth, internalFormat);
3240 break;
3241 }
3242
3243 /* This should really never fail */
3244 if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
3245 *reason = "internalFormat";
3246 return GL_INVALID_ENUM;
3247 }
3248
3249 /* No compressed formats support borders at this time */
3250 if (border != 0) {
3251 *reason = "border != 0";
3252 return GL_INVALID_VALUE;
3253 }
3254
3255 /* For cube map, width must equal height */
3256 if (_mesa_is_cube_face(target) && width != height) {
3257 *reason = "width != height";
3258 return GL_INVALID_VALUE;
3259 }
3260
3261 /* check image size against compression block size */
3262 {
3263 gl_format texFormat =
3264 ctx->Driver.ChooseTextureFormat(ctx, proxy_format,
3265 choose_format, choose_type);
3266 GLuint bw, bh;
3267
3268 _mesa_get_format_block_size(texFormat, &bw, &bh);
3269 if ((width > bw && width % bw > 0) ||
3270 (height > bh && height % bh > 0)) {
3271 /*
3272 * Per GL_ARB_texture_compression: GL_INVALID_OPERATION is
3273 * generated [...] if any parameter combinations are not
3274 * supported by the specific compressed internal format.
3275 */
3276 *reason = "invalid width or height for compression format";
3277 return GL_INVALID_OPERATION;
3278 }
3279 }
3280
3281 /* check image sizes */
3282 if (!ctx->Driver.TestProxyTexImage(ctx, proxyTarget, level,
3283 proxy_format, choose_format,
3284 choose_type,
3285 width, height, depth, border)) {
3286 /* See error comment above */
3287 *reason = "invalid width, height or format";
3288 return GL_INVALID_OPERATION;
3289 }
3290
3291 /* check image size in bytes */
3292 if (expectedSize != imageSize) {
3293 /* Per GL_ARB_texture_compression: GL_INVALID_VALUE is generated [...]
3294 * if <imageSize> is not consistent with the format, dimensions, and
3295 * contents of the specified image.
3296 */
3297 *reason = "imageSize inconsistant with width/height/format";
3298 return GL_INVALID_VALUE;
3299 }
3300
3301 if (!mutable_tex_object(ctx, target)) {
3302 *reason = "immutable texture";
3303 return GL_INVALID_OPERATION;
3304 }
3305
3306 return GL_NO_ERROR;
3307 }
3308
3309
3310 /**
3311 * Error checking for glCompressedTexSubImage[123]D().
3312 * \warning There are some bad assumptions here about the size of compressed
3313 * texture tiles (multiple of 4) used to test the validity of the
3314 * offset and size parameters.
3315 * \return error code or GL_NO_ERROR.
3316 */
3317 static GLenum
3318 compressed_subtexture_error_check(struct gl_context *ctx, GLint dimensions,
3319 GLenum target, GLint level,
3320 GLint xoffset, GLint yoffset, GLint zoffset,
3321 GLsizei width, GLsizei height, GLsizei depth,
3322 GLenum format, GLsizei imageSize)
3323 {
3324 GLint expectedSize, maxLevels = 0, maxTextureSize;
3325 GLuint bw, bh;
3326 (void) zoffset;
3327
3328 if (dimensions == 1) {
3329 /* 1D compressed textures not allowed */
3330 return GL_INVALID_ENUM;
3331 }
3332 else if (dimensions == 2) {
3333 if (target == GL_PROXY_TEXTURE_2D) {
3334 maxLevels = ctx->Const.MaxTextureLevels;
3335 }
3336 else if (target == GL_TEXTURE_2D) {
3337 maxLevels = ctx->Const.MaxTextureLevels;
3338 }
3339 else if (target == GL_PROXY_TEXTURE_CUBE_MAP_ARB) {
3340 if (!ctx->Extensions.ARB_texture_cube_map)
3341 return GL_INVALID_ENUM; /*target*/
3342 maxLevels = ctx->Const.MaxCubeTextureLevels;
3343 }
3344 else if (_mesa_is_cube_face(target)) {
3345 if (!ctx->Extensions.ARB_texture_cube_map)
3346 return GL_INVALID_ENUM; /*target*/
3347 maxLevels = ctx->Const.MaxCubeTextureLevels;
3348 }
3349 else {
3350 return GL_INVALID_ENUM; /*target*/
3351 }
3352 }
3353 else if (dimensions == 3) {
3354 /* 3D compressed textures not allowed */
3355 return GL_INVALID_ENUM;
3356 }
3357
3358 maxTextureSize = 1 << (maxLevels - 1);
3359
3360 /* this will catch any invalid compressed format token */
3361 if (!_mesa_is_compressed_format(ctx, format))
3362 return GL_INVALID_ENUM;
3363
3364 if (width < 1 || width > maxTextureSize)
3365 return GL_INVALID_VALUE;
3366
3367 if ((height < 1 || height > maxTextureSize)
3368 && dimensions > 1)
3369 return GL_INVALID_VALUE;
3370
3371 if (level < 0 || level >= maxLevels)
3372 return GL_INVALID_VALUE;
3373
3374 /*
3375 * do checks which depend on compression block size
3376 */
3377 get_compressed_block_size(format, &bw, &bh);
3378
3379 if ((xoffset % bw != 0) || (yoffset % bh != 0))
3380 return GL_INVALID_VALUE;
3381
3382 if ((width % bw != 0) && width != 2 && width != 1)
3383 return GL_INVALID_VALUE;
3384
3385 if ((height % bh != 0) && height != 2 && height != 1)
3386 return GL_INVALID_VALUE;
3387
3388 expectedSize = compressed_tex_size(width, height, depth, format);
3389 if (expectedSize != imageSize)
3390 return GL_INVALID_VALUE;
3391
3392 return GL_NO_ERROR;
3393 }
3394
3395
3396 /**
3397 * Do second part of glCompressedTexSubImage error checking.
3398 * \return GL_TRUE if error found, GL_FALSE otherwise.
3399 */
3400 static GLboolean
3401 compressed_subtexture_error_check2(struct gl_context *ctx, GLuint dims,
3402 GLsizei width, GLsizei height,
3403 GLsizei depth, GLenum format,
3404 struct gl_texture_image *texImage)
3405 {
3406
3407 if ((GLint) format != texImage->InternalFormat) {
3408 _mesa_error(ctx, GL_INVALID_OPERATION,
3409 "glCompressedTexSubImage%uD(format=0x%x)", dims, format);
3410 return GL_TRUE;
3411 }
3412
3413 if (compressedteximage_only_format(ctx, format)) {
3414 _mesa_error(ctx, GL_INVALID_OPERATION,
3415 "glCompressedTexSubImage%uD(format=0x%x cannot be updated)"
3416 , dims, format);
3417 return GL_TRUE;
3418 }
3419
3420 if (((width == 1 || width == 2) &&
3421 width != (GLsizei) texImage->Width) ||
3422 (width > (GLsizei) texImage->Width)) {
3423 _mesa_error(ctx, GL_INVALID_VALUE,
3424 "glCompressedTexSubImage%uD(width=%d)", dims, width);
3425 return GL_TRUE;
3426 }
3427
3428 if (dims >= 2) {
3429 if (((height == 1 || height == 2) &&
3430 height != (GLsizei) texImage->Height) ||
3431 (height > (GLsizei) texImage->Height)) {
3432 _mesa_error(ctx, GL_INVALID_VALUE,
3433 "glCompressedTexSubImage%uD(height=%d)", dims, height);
3434 return GL_TRUE;
3435 }
3436 }
3437
3438 if (dims >= 3) {
3439 if (((depth == 1 || depth == 2) &&
3440 depth != (GLsizei) texImage->Depth) ||
3441 (depth > (GLsizei) texImage->Depth)) {
3442 _mesa_error(ctx, GL_INVALID_VALUE,
3443 "glCompressedTexSubImage%uD(depth=%d)", dims, depth);
3444 return GL_TRUE;
3445 }
3446 }
3447
3448 return GL_FALSE;
3449 }
3450
3451
3452 /**
3453 * Implementation of the glCompressedTexImage1/2/3D() functions.
3454 */
3455 static void
3456 compressedteximage(struct gl_context *ctx, GLuint dims,
3457 GLenum target, GLint level,
3458 GLenum internalFormat, GLsizei width,
3459 GLsizei height, GLsizei depth, GLint border,
3460 GLsizei imageSize, const GLvoid *data)
3461 {
3462 GLenum error;
3463 char *reason = "";
3464
3465 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3466
3467 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
3468 _mesa_debug(ctx,
3469 "glCompressedTexImage%uDARB %s %d %s %d %d %d %d %d %p\n",
3470 dims,
3471 _mesa_lookup_enum_by_nr(target), level,
3472 _mesa_lookup_enum_by_nr(internalFormat),
3473 width, height, depth, border, imageSize, data);
3474
3475 /* check target */
3476 if (!legal_teximage_target(ctx, dims, target)) {
3477 _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage%uD(target=%s)",
3478 dims, _mesa_lookup_enum_by_nr(target));
3479 return;
3480 }
3481
3482 error = compressed_texture_error_check(ctx, dims, target, level,
3483 internalFormat, width, height, depth,
3484 border, imageSize, &reason);
3485
3486 #if FEATURE_ES
3487 /* XXX this is kind of a hack */
3488 if (!error && dims == 2) {
3489 switch (internalFormat) {
3490 case GL_PALETTE4_RGB8_OES:
3491 case GL_PALETTE4_RGBA8_OES:
3492 case GL_PALETTE4_R5_G6_B5_OES:
3493 case GL_PALETTE4_RGBA4_OES:
3494 case GL_PALETTE4_RGB5_A1_OES:
3495 case GL_PALETTE8_RGB8_OES:
3496 case GL_PALETTE8_RGBA8_OES:
3497 case GL_PALETTE8_R5_G6_B5_OES:
3498 case GL_PALETTE8_RGBA4_OES:
3499 case GL_PALETTE8_RGB5_A1_OES:
3500 _mesa_cpal_compressed_teximage2d(target, level, internalFormat,
3501 width, height, imageSize, data);
3502 return;
3503 }
3504 }
3505 #endif
3506
3507 if (_mesa_is_proxy_texture(target)) {
3508 /* Proxy texture: just check for errors and update proxy state */
3509 struct gl_texture_image *texImage;
3510
3511 if (!error) {
3512 struct gl_texture_object *texObj =
3513 _mesa_get_current_tex_object(ctx, target);
3514 gl_format texFormat =
3515 _mesa_choose_texture_format(ctx, texObj, target, level,
3516 internalFormat, GL_NONE, GL_NONE);
3517 if (!legal_texture_size(ctx, texFormat, width, height, depth)) {
3518 error = GL_OUT_OF_MEMORY;
3519 }
3520 }
3521
3522 texImage = _mesa_get_proxy_tex_image(ctx, target, level);
3523 if (texImage) {
3524 if (error) {
3525 /* if error, clear all proxy texture image parameters */
3526 clear_teximage_fields(texImage);
3527 }
3528 else {
3529 /* no error: store the teximage parameters */
3530 _mesa_init_teximage_fields(ctx, texImage, width, height,
3531 depth, border, internalFormat,
3532 MESA_FORMAT_NONE);
3533 }
3534 }
3535 }
3536 else {
3537 /* non-proxy target */
3538 struct gl_texture_object *texObj;
3539 struct gl_texture_image *texImage;
3540
3541 if (error) {
3542 _mesa_error(ctx, error, "glCompressedTexImage%uD(%s)", dims, reason);
3543 return;
3544 }
3545
3546 texObj = _mesa_get_current_tex_object(ctx, target);
3547
3548 _mesa_lock_texture(ctx, texObj);
3549 {
3550 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
3551 if (!texImage) {
3552 _mesa_error(ctx, GL_OUT_OF_MEMORY,
3553 "glCompressedTexImage%uD", dims);
3554 }
3555 else {
3556 gl_format texFormat;
3557
3558 ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
3559
3560 texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
3561 internalFormat, GL_NONE,
3562 GL_NONE);
3563
3564 if (legal_texture_size(ctx, texFormat, width, height, depth)) {
3565 _mesa_init_teximage_fields(ctx, texImage,
3566 width, height, depth,
3567 border, internalFormat, texFormat);
3568
3569 switch (dims) {
3570 case 1:
3571 ASSERT(ctx->Driver.CompressedTexImage1D);
3572 ctx->Driver.CompressedTexImage1D(ctx, texImage,
3573 internalFormat,
3574 width,
3575 border, imageSize, data);
3576 break;
3577 case 2:
3578 ASSERT(ctx->Driver.CompressedTexImage2D);
3579 ctx->Driver.CompressedTexImage2D(ctx, texImage,
3580 internalFormat,
3581 width, height,
3582 border, imageSize, data);
3583 break;
3584 case 3:
3585 ASSERT(ctx->Driver.CompressedTexImage3D);
3586 ctx->Driver.CompressedTexImage3D(ctx, texImage,
3587 internalFormat,
3588 width, height, depth,
3589 border, imageSize, data);
3590 break;
3591 default:
3592 _mesa_problem(ctx, "bad dims in compressedteximage");
3593 }
3594
3595 check_gen_mipmap(ctx, target, texObj, level);
3596
3597 /* state update */
3598 texObj->_Complete = GL_FALSE;
3599 ctx->NewState |= _NEW_TEXTURE;
3600 }
3601 else {
3602 _mesa_error(ctx, GL_OUT_OF_MEMORY,
3603 "glCompressedTexImage%uD", dims);
3604 }
3605 }
3606 }
3607 _mesa_unlock_texture(ctx, texObj);
3608 }
3609 }
3610
3611
3612 void GLAPIENTRY
3613 _mesa_CompressedTexImage1DARB(GLenum target, GLint level,
3614 GLenum internalFormat, GLsizei width,
3615 GLint border, GLsizei imageSize,
3616 const GLvoid *data)
3617 {
3618 GET_CURRENT_CONTEXT(ctx);
3619 compressedteximage(ctx, 1, target, level, internalFormat,
3620 width, 1, 1, border, imageSize, data);
3621 }
3622
3623
3624 void GLAPIENTRY
3625 _mesa_CompressedTexImage2DARB(GLenum target, GLint level,
3626 GLenum internalFormat, GLsizei width,
3627 GLsizei height, GLint border, GLsizei imageSize,
3628 const GLvoid *data)
3629 {
3630 GET_CURRENT_CONTEXT(ctx);
3631 compressedteximage(ctx, 2, target, level, internalFormat,
3632 width, height, 1, border, imageSize, data);
3633 }
3634
3635
3636 void GLAPIENTRY
3637 _mesa_CompressedTexImage3DARB(GLenum target, GLint level,
3638 GLenum internalFormat, GLsizei width,
3639 GLsizei height, GLsizei depth, GLint border,
3640 GLsizei imageSize, const GLvoid *data)
3641 {
3642 GET_CURRENT_CONTEXT(ctx);
3643 compressedteximage(ctx, 3, target, level, internalFormat,
3644 width, height, depth, border, imageSize, data);
3645 }
3646
3647
3648 /**
3649 * Common helper for glCompressedTexSubImage1/2/3D().
3650 */
3651 static void
3652 compressed_tex_sub_image(GLuint dims, GLenum target, GLint level,
3653 GLint xoffset, GLint yoffset, GLint zoffset,
3654 GLsizei width, GLsizei height, GLsizei depth,
3655 GLenum format, GLsizei imageSize, const GLvoid *data)
3656 {
3657 struct gl_texture_object *texObj;
3658 struct gl_texture_image *texImage;
3659 GLenum error;
3660 GET_CURRENT_CONTEXT(ctx);
3661 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3662
3663 error = compressed_subtexture_error_check(ctx, dims, target, level,
3664 xoffset, 0, 0, /* pos */
3665 width, height, depth, /* size */
3666 format, imageSize);
3667 if (error) {
3668 _mesa_error(ctx, error, "glCompressedTexSubImage%uD", dims);
3669 return;
3670 }
3671
3672 texObj = _mesa_get_current_tex_object(ctx, target);
3673
3674 _mesa_lock_texture(ctx, texObj);
3675 {
3676 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
3677 assert(texImage);
3678
3679 if (compressed_subtexture_error_check2(ctx, dims, width, height, depth,
3680 format, texImage)) {
3681 /* error was recorded */
3682 }
3683 else if (width > 0 && height > 0 && depth > 0) {
3684 switch (dims) {
3685 case 1:
3686 if (ctx->Driver.CompressedTexSubImage1D) {
3687 ctx->Driver.CompressedTexSubImage1D(ctx, texImage,
3688 xoffset, width,
3689 format, imageSize, data);
3690 }
3691 break;
3692 case 2:
3693 if (ctx->Driver.CompressedTexSubImage2D) {
3694 ctx->Driver.CompressedTexSubImage2D(ctx, texImage,
3695 xoffset, yoffset,
3696 width, height,
3697 format, imageSize, data);
3698 }
3699 break;
3700 case 3:
3701 if (ctx->Driver.CompressedTexSubImage3D) {
3702 ctx->Driver.CompressedTexSubImage3D(ctx, texImage,
3703 xoffset, yoffset, zoffset,
3704 width, height, depth,
3705 format, imageSize, data);
3706 }
3707 break;
3708 default:
3709 ;
3710 }
3711
3712 check_gen_mipmap(ctx, target, texObj, level);
3713
3714 ctx->NewState |= _NEW_TEXTURE;
3715 }
3716 }
3717 _mesa_unlock_texture(ctx, texObj);
3718 }
3719
3720
3721 void GLAPIENTRY
3722 _mesa_CompressedTexSubImage1DARB(GLenum target, GLint level, GLint xoffset,
3723 GLsizei width, GLenum format,
3724 GLsizei imageSize, const GLvoid *data)
3725 {
3726 compressed_tex_sub_image(1, target, level, xoffset, 0, 0, width, 1, 1,
3727 format, imageSize, data);
3728 }
3729
3730
3731 void GLAPIENTRY
3732 _mesa_CompressedTexSubImage2DARB(GLenum target, GLint level, GLint xoffset,
3733 GLint yoffset, GLsizei width, GLsizei height,
3734 GLenum format, GLsizei imageSize,
3735 const GLvoid *data)
3736 {
3737 compressed_tex_sub_image(2, target, level, xoffset, yoffset, 0,
3738 width, height, 1, format, imageSize, data);
3739 }
3740
3741
3742 void GLAPIENTRY
3743 _mesa_CompressedTexSubImage3DARB(GLenum target, GLint level, GLint xoffset,
3744 GLint yoffset, GLint zoffset, GLsizei width,
3745 GLsizei height, GLsizei depth, GLenum format,
3746 GLsizei imageSize, const GLvoid *data)
3747 {
3748 compressed_tex_sub_image(3, target, level, xoffset, yoffset, zoffset,
3749 width, height, depth, format, imageSize, data);
3750 }
3751
3752
3753 /**
3754 * Helper for glTexBuffer(). Check if internalFormat is legal. If so,
3755 * return the basic data type and number of components for the format.
3756 * \param return GL_TRUE if internalFormat is legal, GL_FALSE otherwise
3757 */
3758 static GLboolean
3759 get_sized_format_info(const struct gl_context *ctx, GLenum internalFormat,
3760 GLenum *datatype, GLuint *components)
3761 {
3762 switch (internalFormat) {
3763 case GL_ALPHA8:
3764 *datatype = GL_UNSIGNED_BYTE;
3765 *components = 1;
3766 break;
3767 case GL_ALPHA16:
3768 *datatype = GL_UNSIGNED_SHORT;
3769 *components = 1;
3770 break;
3771 case GL_ALPHA16F_ARB:
3772 *datatype = GL_HALF_FLOAT;
3773 *components = 1;
3774 break;
3775 case GL_ALPHA32F_ARB:
3776 *datatype = GL_FLOAT;
3777 *components = 1;
3778 break;
3779 case GL_ALPHA8I_EXT:
3780 *datatype = GL_BYTE;
3781 *components = 1;
3782 break;
3783 case GL_ALPHA16I_EXT:
3784 *datatype = GL_SHORT;
3785 *components = 1;
3786 break;
3787 case GL_ALPHA32I_EXT:
3788 *datatype = GL_INT;
3789 *components = 1;
3790 break;
3791 case GL_ALPHA8UI_EXT:
3792 *datatype = GL_UNSIGNED_BYTE;
3793 *components = 1;
3794 break;
3795 case GL_ALPHA16UI_EXT:
3796 *datatype = GL_UNSIGNED_SHORT;
3797 *components = 1;
3798 break;
3799 case GL_ALPHA32UI_EXT:
3800 *datatype = GL_UNSIGNED_INT;
3801 *components = 1;
3802 break;
3803 case GL_LUMINANCE8:
3804 *datatype = GL_UNSIGNED_BYTE;
3805 *components = 1;
3806 break;
3807 case GL_LUMINANCE16:
3808 *datatype = GL_UNSIGNED_SHORT;
3809 *components = 1;
3810 break;
3811 case GL_LUMINANCE16F_ARB:
3812 *datatype = GL_HALF_FLOAT;
3813 *components = 1;
3814 break;
3815 case GL_LUMINANCE32F_ARB:
3816 *datatype = GL_FLOAT;
3817 *components = 1;
3818 break;
3819 case GL_LUMINANCE8I_EXT:
3820 *datatype = GL_BYTE;
3821 *components = 1;
3822 break;
3823 case GL_LUMINANCE16I_EXT:
3824 *datatype = GL_SHORT;
3825 *components = 1;
3826 break;
3827 case GL_LUMINANCE32I_EXT:
3828 *datatype = GL_INT;
3829 *components = 1;
3830 break;
3831 case GL_LUMINANCE8UI_EXT:
3832 *datatype = GL_UNSIGNED_BYTE;
3833 *components = 1;
3834 break;
3835 case GL_LUMINANCE16UI_EXT:
3836 *datatype = GL_UNSIGNED_SHORT;
3837 *components = 1;
3838 break;
3839 case GL_LUMINANCE32UI_EXT:
3840 *datatype = GL_UNSIGNED_INT;
3841 *components = 1;
3842 break;
3843 case GL_LUMINANCE8_ALPHA8:
3844 *datatype = GL_UNSIGNED_BYTE;
3845 *components = 2;
3846 break;
3847 case GL_LUMINANCE16_ALPHA16:
3848 *datatype = GL_UNSIGNED_SHORT;
3849 *components = 2;
3850 break;
3851 case GL_LUMINANCE_ALPHA16F_ARB:
3852 *datatype = GL_HALF_FLOAT;
3853 *components = 2;
3854 break;
3855 case GL_LUMINANCE_ALPHA32F_ARB:
3856 *datatype = GL_FLOAT;
3857 *components = 2;
3858 break;
3859 case GL_LUMINANCE_ALPHA8I_EXT:
3860 *datatype = GL_BYTE;
3861 *components = 2;
3862 break;
3863 case GL_LUMINANCE_ALPHA16I_EXT:
3864 *datatype = GL_SHORT;
3865 *components = 2;
3866 break;
3867 case GL_LUMINANCE_ALPHA32I_EXT:
3868 *datatype = GL_INT;
3869 *components = 2;
3870 break;
3871 case GL_LUMINANCE_ALPHA8UI_EXT:
3872 *datatype = GL_UNSIGNED_BYTE;
3873 *components = 2;
3874 break;
3875 case GL_LUMINANCE_ALPHA16UI_EXT:
3876 *datatype = GL_UNSIGNED_SHORT;
3877 *components = 2;
3878 break;
3879 case GL_LUMINANCE_ALPHA32UI_EXT:
3880 *datatype = GL_UNSIGNED_INT;
3881 *components = 2;
3882 break;
3883 case GL_INTENSITY8:
3884 *datatype = GL_UNSIGNED_BYTE;
3885 *components = 1;
3886 break;
3887 case GL_INTENSITY16:
3888 *datatype = GL_UNSIGNED_SHORT;
3889 *components = 1;
3890 break;
3891 case GL_INTENSITY16F_ARB:
3892 *datatype = GL_HALF_FLOAT;
3893 *components = 1;
3894 break;
3895 case GL_INTENSITY32F_ARB:
3896 *datatype = GL_FLOAT;
3897 *components = 1;
3898 break;
3899 case GL_INTENSITY8I_EXT:
3900 *datatype = GL_BYTE;
3901 *components = 1;
3902 break;
3903 case GL_INTENSITY16I_EXT:
3904 *datatype = GL_SHORT;
3905 *components = 1;
3906 break;
3907 case GL_INTENSITY32I_EXT:
3908 *datatype = GL_INT;
3909 *components = 1;
3910 break;
3911 case GL_INTENSITY8UI_EXT:
3912 *datatype = GL_UNSIGNED_BYTE;
3913 *components = 1;
3914 break;
3915 case GL_INTENSITY16UI_EXT:
3916 *datatype = GL_UNSIGNED_SHORT;
3917 *components = 1;
3918 break;
3919 case GL_INTENSITY32UI_EXT:
3920 *datatype = GL_UNSIGNED_INT;
3921 *components = 1;
3922 break;
3923 case GL_RGBA8:
3924 *datatype = GL_UNSIGNED_BYTE;
3925 *components = 4;
3926 break;
3927 case GL_RGBA16:
3928 *datatype = GL_UNSIGNED_SHORT;
3929 *components = 4;
3930 break;
3931 case GL_RGBA16F_ARB:
3932 *datatype = GL_HALF_FLOAT;
3933 *components = 4;
3934 break;
3935 case GL_RGBA32F_ARB:
3936 *datatype = GL_FLOAT;
3937 *components = 4;
3938 break;
3939 case GL_RGBA8I_EXT:
3940 *datatype = GL_BYTE;
3941 *components = 4;
3942 break;
3943 case GL_RGBA16I_EXT:
3944 *datatype = GL_SHORT;
3945 *components = 4;
3946 break;
3947 case GL_RGBA32I_EXT:
3948 *datatype = GL_INT;
3949 *components = 4;
3950 break;
3951 case GL_RGBA8UI_EXT:
3952 *datatype = GL_UNSIGNED_BYTE;
3953 *components = 4;
3954 break;
3955 case GL_RGBA16UI_EXT:
3956 *datatype = GL_UNSIGNED_SHORT;
3957 *components = 4;
3958 break;
3959 case GL_RGBA32UI_EXT:
3960 *datatype = GL_UNSIGNED_INT;
3961 *components = 4;
3962 break;
3963 default:
3964 return GL_FALSE;
3965 }
3966
3967 if (*datatype == GL_FLOAT && !ctx->Extensions.ARB_texture_float)
3968 return GL_FALSE;
3969
3970 if (*datatype == GL_HALF_FLOAT && !ctx->Extensions.ARB_half_float_pixel)
3971 return GL_FALSE;
3972
3973 return GL_TRUE;
3974 }
3975
3976
3977 /** GL_ARB_texture_buffer_object */
3978 void GLAPIENTRY
3979 _mesa_TexBuffer(GLenum target, GLenum internalFormat, GLuint buffer)
3980 {
3981 struct gl_texture_object *texObj;
3982 struct gl_buffer_object *bufObj;
3983 GLenum dataType;
3984 GLuint comps;
3985
3986 GET_CURRENT_CONTEXT(ctx);
3987 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3988
3989 if (!ctx->Extensions.ARB_texture_buffer_object) {
3990 _mesa_error(ctx, GL_INVALID_OPERATION, "glTexBuffer");
3991 return;
3992 }
3993
3994 if (target != GL_TEXTURE_BUFFER_ARB) {
3995 _mesa_error(ctx, GL_INVALID_ENUM, "glTexBuffer(target)");
3996 return;
3997 }
3998
3999 if (!get_sized_format_info(ctx, internalFormat, &dataType, &comps)) {
4000 _mesa_error(ctx, GL_INVALID_ENUM, "glTexBuffer(internalFormat 0x%x)",
4001 internalFormat);
4002 return;
4003 }
4004
4005 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
4006 if (buffer && !bufObj) {
4007 _mesa_error(ctx, GL_INVALID_OPERATION, "glTexBuffer(buffer %u)", buffer);
4008 return;
4009 }
4010
4011 texObj = _mesa_get_current_tex_object(ctx, target);
4012
4013 _mesa_lock_texture(ctx, texObj);
4014 {
4015 _mesa_reference_buffer_object(ctx, &texObj->BufferObject, bufObj);
4016 texObj->BufferObjectFormat = internalFormat;
4017 }
4018 _mesa_unlock_texture(ctx, texObj);
4019 }