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