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