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