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