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