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