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