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