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