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