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