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