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