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