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