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