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