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