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