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