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