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