Sync with trunk r63647.
[reactos.git] / dll / opengl / mesa / main / teximage.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 /**
27 * \file teximage.c
28 * Texture image-related functions.
29 */
30
31 #include <precomp.h>
32
33 /**
34 * State changes which we care about for glCopyTex[Sub]Image() calls.
35 * In particular, we care about pixel transfer state and buffer state
36 * (such as glReadBuffer to make sure we read from the right renderbuffer).
37 */
38 #define NEW_COPY_TEX_STATE (_NEW_BUFFERS | _NEW_PIXEL)
39
40
41
42 /**
43 * Return the simple base format for a given internal texture format.
44 * For example, given GL_LUMINANCE12_ALPHA4, return GL_LUMINANCE_ALPHA.
45 *
46 * \param ctx GL context.
47 * \param internalFormat the internal texture format token or 1, 2, 3, or 4.
48 *
49 * \return the corresponding \u base internal format (GL_ALPHA, GL_LUMINANCE,
50 * GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, or GL_RGBA), or -1 if invalid enum.
51 *
52 * This is the format which is used during texture application (i.e. the
53 * texture format and env mode determine the arithmetic used.
54 *
55 * XXX this could be static
56 */
57 GLint
58 _mesa_base_tex_format( struct gl_context *ctx, GLint internalFormat )
59 {
60 switch (internalFormat) {
61 case GL_ALPHA:
62 case GL_ALPHA4:
63 case GL_ALPHA8:
64 case GL_ALPHA12:
65 case GL_ALPHA16:
66 return GL_ALPHA;
67 case 1:
68 case GL_LUMINANCE:
69 case GL_LUMINANCE4:
70 case GL_LUMINANCE8:
71 case GL_LUMINANCE12:
72 case GL_LUMINANCE16:
73 return GL_LUMINANCE;
74 case 2:
75 case GL_LUMINANCE_ALPHA:
76 case GL_LUMINANCE4_ALPHA4:
77 case GL_LUMINANCE6_ALPHA2:
78 case GL_LUMINANCE8_ALPHA8:
79 case GL_LUMINANCE12_ALPHA4:
80 case GL_LUMINANCE12_ALPHA12:
81 case GL_LUMINANCE16_ALPHA16:
82 return GL_LUMINANCE_ALPHA;
83 case GL_INTENSITY:
84 case GL_INTENSITY4:
85 case GL_INTENSITY8:
86 case GL_INTENSITY12:
87 case GL_INTENSITY16:
88 return GL_INTENSITY;
89 case 3:
90 case GL_RGB:
91 case GL_R3_G3_B2:
92 case GL_RGB4:
93 case GL_RGB5:
94 case GL_RGB8:
95 case GL_RGB10:
96 case GL_RGB12:
97 case GL_RGB16:
98 return GL_RGB;
99 case 4:
100 case GL_RGBA:
101 case GL_RGBA2:
102 case GL_RGBA4:
103 case GL_RGB5_A1:
104 case GL_RGBA8:
105 case GL_RGB10_A2:
106 case GL_RGBA12:
107 case GL_RGBA16:
108 return GL_RGBA;
109 default:
110 ; /* fallthrough */
111 }
112
113 if (ctx->Extensions.MESA_ycbcr_texture) {
114 if (internalFormat == GL_YCBCR_MESA)
115 return GL_YCBCR_MESA;
116 }
117
118 if (ctx->VersionMajor >= 3 ||
119 ctx->Extensions.EXT_texture_integer) {
120 switch (internalFormat) {
121 case GL_RGBA8UI_EXT:
122 case GL_RGBA16UI_EXT:
123 case GL_RGBA32UI_EXT:
124 case GL_RGBA8I_EXT:
125 case GL_RGBA16I_EXT:
126 case GL_RGBA32I_EXT:
127 case GL_RGB10_A2UI:
128 return GL_RGBA;
129 case GL_RGB8UI_EXT:
130 case GL_RGB16UI_EXT:
131 case GL_RGB32UI_EXT:
132 case GL_RGB8I_EXT:
133 case GL_RGB16I_EXT:
134 case GL_RGB32I_EXT:
135 return GL_RGB;
136 }
137 }
138
139 if (ctx->Extensions.EXT_texture_integer) {
140 switch (internalFormat) {
141 case GL_ALPHA8UI_EXT:
142 case GL_ALPHA16UI_EXT:
143 case GL_ALPHA32UI_EXT:
144 case GL_ALPHA8I_EXT:
145 case GL_ALPHA16I_EXT:
146 case GL_ALPHA32I_EXT:
147 return GL_ALPHA;
148 case GL_INTENSITY8UI_EXT:
149 case GL_INTENSITY16UI_EXT:
150 case GL_INTENSITY32UI_EXT:
151 case GL_INTENSITY8I_EXT:
152 case GL_INTENSITY16I_EXT:
153 case GL_INTENSITY32I_EXT:
154 return GL_INTENSITY;
155 case GL_LUMINANCE8UI_EXT:
156 case GL_LUMINANCE16UI_EXT:
157 case GL_LUMINANCE32UI_EXT:
158 case GL_LUMINANCE8I_EXT:
159 case GL_LUMINANCE16I_EXT:
160 case GL_LUMINANCE32I_EXT:
161 return GL_LUMINANCE;
162 case GL_LUMINANCE_ALPHA8UI_EXT:
163 case GL_LUMINANCE_ALPHA16UI_EXT:
164 case GL_LUMINANCE_ALPHA32UI_EXT:
165 case GL_LUMINANCE_ALPHA8I_EXT:
166 case GL_LUMINANCE_ALPHA16I_EXT:
167 case GL_LUMINANCE_ALPHA32I_EXT:
168 return GL_LUMINANCE_ALPHA;
169 default:
170 ; /* fallthrough */
171 }
172 }
173
174 return -1; /* error */
175 }
176
177
178 /**
179 * Is the given texture format a generic compressed format?
180 */
181
182
183 /**
184 * For cube map faces, return a face index in [0,5].
185 * For other targets return 0;
186 */
187 GLuint
188 _mesa_tex_target_to_face(GLenum target)
189 {
190 if (_mesa_is_cube_face(target))
191 return (GLuint) target - (GLuint) GL_TEXTURE_CUBE_MAP_POSITIVE_X;
192 else
193 return 0;
194 }
195
196
197
198 /**
199 * Install gl_texture_image in a gl_texture_object according to the target
200 * and level parameters.
201 *
202 * \param tObj texture object.
203 * \param target texture target.
204 * \param level image level.
205 * \param texImage texture image.
206 */
207 static void
208 set_tex_image(struct gl_texture_object *tObj,
209 GLenum target, GLint level,
210 struct gl_texture_image *texImage)
211 {
212 const GLuint face = _mesa_tex_target_to_face(target);
213
214 ASSERT(tObj);
215 ASSERT(texImage);
216
217 tObj->Image[face][level] = texImage;
218
219 /* Set the 'back' pointer */
220 texImage->TexObject = tObj;
221 texImage->Level = level;
222 texImage->Face = face;
223 }
224
225
226 /**
227 * Allocate a texture image structure.
228 *
229 * Called via ctx->Driver.NewTextureImage() unless overriden by a device
230 * driver.
231 *
232 * \return a pointer to gl_texture_image struct with all fields initialized to
233 * zero.
234 */
235 struct gl_texture_image *
236 _mesa_new_texture_image( struct gl_context *ctx )
237 {
238 (void) ctx;
239 return CALLOC_STRUCT(gl_texture_image);
240 }
241
242
243 /**
244 * Free a gl_texture_image and associated data.
245 * This function is a fallback called via ctx->Driver.DeleteTextureImage().
246 *
247 * \param texImage texture image.
248 *
249 * Free the texture image structure and the associated image data.
250 */
251 void
252 _mesa_delete_texture_image(struct gl_context *ctx,
253 struct gl_texture_image *texImage)
254 {
255 /* Free texImage->Data and/or any other driver-specific texture
256 * image storage.
257 */
258 ASSERT(ctx->Driver.FreeTextureImageBuffer);
259 ctx->Driver.FreeTextureImageBuffer( ctx, texImage );
260 free(texImage);
261 }
262
263
264 /**
265 * Test if a target is a proxy target.
266 *
267 * \param target texture target.
268 *
269 * \return GL_TRUE if the target is a proxy target, GL_FALSE otherwise.
270 */
271 GLboolean
272 _mesa_is_proxy_texture(GLenum target)
273 {
274 /*
275 * NUM_TEXTURE_TARGETS should match number of terms below, except there's no
276 * proxy for GL_TEXTURE_BUFFER and GL_TEXTURE_EXTERNAL_OES.
277 */
278 assert(NUM_TEXTURE_TARGETS == 7 + 2);
279
280 return (target == GL_PROXY_TEXTURE_1D ||
281 target == GL_PROXY_TEXTURE_2D ||
282 target == GL_PROXY_TEXTURE_CUBE_MAP_ARB);
283 }
284
285
286 /**
287 * Return the proxy target which corresponds to the given texture target
288 */
289 static GLenum
290 get_proxy_target(GLenum target)
291 {
292 switch (target) {
293 case GL_TEXTURE_1D:
294 case GL_PROXY_TEXTURE_1D:
295 return GL_PROXY_TEXTURE_1D;
296 case GL_TEXTURE_2D:
297 case GL_PROXY_TEXTURE_2D:
298 return GL_PROXY_TEXTURE_2D;
299 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
300 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
301 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
302 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
303 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
304 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
305 case GL_TEXTURE_CUBE_MAP_ARB:
306 case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
307 return GL_PROXY_TEXTURE_CUBE_MAP_ARB;
308 default:
309 _mesa_problem(NULL, "unexpected target in get_proxy_target()");
310 return 0;
311 }
312 }
313
314
315 /**
316 * Get the texture object that corresponds to the target of the given
317 * texture unit. The target should have already been checked for validity.
318 *
319 * \param ctx GL context.
320 * \param texUnit texture unit.
321 * \param target texture target.
322 *
323 * \return pointer to the texture object on success, or NULL on failure.
324 */
325 struct gl_texture_object *
326 _mesa_select_tex_object(struct gl_context *ctx,
327 GLenum target)
328 {
329 switch (target) {
330 case GL_TEXTURE_1D:
331 return ctx->Texture.Unit.CurrentTex[TEXTURE_1D_INDEX];
332 case GL_PROXY_TEXTURE_1D:
333 return ctx->Texture.ProxyTex[TEXTURE_1D_INDEX];
334 case GL_TEXTURE_2D:
335 return ctx->Texture.Unit.CurrentTex[TEXTURE_2D_INDEX];
336 case GL_PROXY_TEXTURE_2D:
337 return ctx->Texture.ProxyTex[TEXTURE_2D_INDEX];
338 default:
339 _mesa_problem(NULL, "bad target in _mesa_select_tex_object()");
340 return NULL;
341 }
342 }
343
344
345 /**
346 * Get a texture image pointer from a texture object, given a texture
347 * target and mipmap level. The target and level parameters should
348 * have already been error-checked.
349 *
350 * \param ctx GL context.
351 * \param texObj texture unit.
352 * \param target texture target.
353 * \param level image level.
354 *
355 * \return pointer to the texture image structure, or NULL on failure.
356 */
357 struct gl_texture_image *
358 _mesa_select_tex_image(struct gl_context *ctx,
359 const struct gl_texture_object *texObj,
360 GLenum target, GLint level)
361 {
362 const GLuint face = _mesa_tex_target_to_face(target);
363
364 ASSERT(texObj);
365 ASSERT(level >= 0);
366 ASSERT(level < MAX_TEXTURE_LEVELS);
367
368 return texObj->Image[face][level];
369 }
370
371
372 /**
373 * Like _mesa_select_tex_image() but if the image doesn't exist, allocate
374 * it and install it. Only return NULL if passed a bad parameter or run
375 * out of memory.
376 */
377 struct gl_texture_image *
378 _mesa_get_tex_image(struct gl_context *ctx, struct gl_texture_object *texObj,
379 GLenum target, GLint level)
380 {
381 struct gl_texture_image *texImage;
382
383 if (!texObj)
384 return NULL;
385
386 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
387 if (!texImage) {
388 texImage = ctx->Driver.NewTextureImage(ctx);
389 if (!texImage) {
390 _mesa_error(ctx, GL_OUT_OF_MEMORY, "texture image allocation");
391 return NULL;
392 }
393
394 set_tex_image(texObj, target, level, texImage);
395 }
396
397 return texImage;
398 }
399
400
401 /**
402 * Return pointer to the specified proxy texture image.
403 * Note that proxy textures are per-context, not per-texture unit.
404 * \return pointer to texture image or NULL if invalid target, invalid
405 * level, or out of memory.
406 */
407 struct gl_texture_image *
408 _mesa_get_proxy_tex_image(struct gl_context *ctx, GLenum target, GLint level)
409 {
410 struct gl_texture_image *texImage;
411 GLuint texIndex;
412
413 if (level < 0 )
414 return NULL;
415
416 switch (target) {
417 case GL_PROXY_TEXTURE_1D:
418 if (level >= ctx->Const.MaxTextureLevels)
419 return NULL;
420 texIndex = TEXTURE_1D_INDEX;
421 break;
422 case GL_PROXY_TEXTURE_2D:
423 if (level >= ctx->Const.MaxTextureLevels)
424 return NULL;
425 texIndex = TEXTURE_2D_INDEX;
426 break;
427 case GL_PROXY_TEXTURE_CUBE_MAP:
428 if (level >= ctx->Const.MaxCubeTextureLevels)
429 return NULL;
430 texIndex = TEXTURE_CUBE_INDEX;
431 break;
432 default:
433 return NULL;
434 }
435
436 texImage = ctx->Texture.ProxyTex[texIndex]->Image[0][level];
437 if (!texImage) {
438 texImage = ctx->Driver.NewTextureImage(ctx);
439 if (!texImage) {
440 _mesa_error(ctx, GL_OUT_OF_MEMORY, "proxy texture allocation");
441 return NULL;
442 }
443 ctx->Texture.ProxyTex[texIndex]->Image[0][level] = texImage;
444 /* Set the 'back' pointer */
445 texImage->TexObject = ctx->Texture.ProxyTex[texIndex];
446 }
447 return texImage;
448 }
449
450
451 /**
452 * Get the maximum number of allowed mipmap levels.
453 *
454 * \param ctx GL context.
455 * \param target texture target.
456 *
457 * \return the maximum number of allowed mipmap levels for the given
458 * texture target, or zero if passed a bad target.
459 *
460 * \sa gl_constants.
461 */
462 GLint
463 _mesa_max_texture_levels(struct gl_context *ctx, GLenum target)
464 {
465 switch (target) {
466 case GL_TEXTURE_1D:
467 case GL_PROXY_TEXTURE_1D:
468 case GL_TEXTURE_2D:
469 case GL_PROXY_TEXTURE_2D:
470 return ctx->Const.MaxTextureLevels;
471 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
472 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
473 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
474 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
475 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
476 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
477 case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
478 return ctx->Extensions.ARB_texture_cube_map
479 ? ctx->Const.MaxCubeTextureLevels : 0;
480 default:
481 return 0; /* bad target */
482 }
483 }
484
485
486 /**
487 * Return number of dimensions per mipmap level for the given texture target.
488 */
489 GLint
490 _mesa_get_texture_dimensions(GLenum target)
491 {
492 switch (target) {
493 case GL_TEXTURE_1D:
494 case GL_PROXY_TEXTURE_1D:
495 return 1;
496 case GL_TEXTURE_2D:
497 case GL_TEXTURE_CUBE_MAP:
498 case GL_PROXY_TEXTURE_2D:
499 case GL_PROXY_TEXTURE_CUBE_MAP:
500 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
501 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
502 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
503 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
504 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
505 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
506 return 2;
507 default:
508 _mesa_problem(NULL, "invalid target 0x%x in get_texture_dimensions()",
509 target);
510 return 2;
511 }
512 }
513
514
515
516
517 #if 000 /* not used anymore */
518 /*
519 * glTexImage[123]D can accept a NULL image pointer. In this case we
520 * create a texture image with unspecified image contents per the OpenGL
521 * spec.
522 */
523 static GLubyte *
524 make_null_texture(GLint width, GLint height, GLint depth, GLenum format)
525 {
526 const GLint components = _mesa_components_in_format(format);
527 const GLint numPixels = width * height * depth;
528 GLubyte *data = (GLubyte *) MALLOC(numPixels * components * sizeof(GLubyte));
529
530 #ifdef DEBUG
531 /*
532 * Let's see if anyone finds this. If glTexImage2D() is called with
533 * a NULL image pointer then load the texture image with something
534 * interesting instead of leaving it indeterminate.
535 */
536 if (data) {
537 static const char message[8][32] = {
538 " X X XXXXX XXX X ",
539 " XX XX X X X X X ",
540 " X X X X X X X ",
541 " X X XXXX XXX XXXXX ",
542 " X X X X X X ",
543 " X X X X X X X ",
544 " X X XXXXX XXX X X ",
545 " "
546 };
547
548 GLubyte *imgPtr = data;
549 GLint h, i, j, k;
550 for (h = 0; h < depth; h++) {
551 for (i = 0; i < height; i++) {
552 GLint srcRow = 7 - (i % 8);
553 for (j = 0; j < width; j++) {
554 GLint srcCol = j % 32;
555 GLubyte texel = (message[srcRow][srcCol]=='X') ? 255 : 70;
556 for (k = 0; k < components; k++) {
557 *imgPtr++ = texel;
558 }
559 }
560 }
561 }
562 }
563 #endif
564
565 return data;
566 }
567 #endif
568
569
570
571 /**
572 * Set the size and format-related fields of a gl_texture_image struct
573 * to zero. This is used when a proxy texture test fails.
574 */
575 static void
576 clear_teximage_fields(struct gl_texture_image *img)
577 {
578 ASSERT(img);
579 img->_BaseFormat = 0;
580 img->InternalFormat = 0;
581 img->Border = 0;
582 img->Width = 0;
583 img->Height = 0;
584 img->Depth = 0;
585 img->Width2 = 0;
586 img->Height2 = 0;
587 img->Depth2 = 0;
588 img->WidthLog2 = 0;
589 img->HeightLog2 = 0;
590 img->DepthLog2 = 0;
591 img->TexFormat = MESA_FORMAT_NONE;
592 }
593
594
595 /**
596 * Initialize basic fields of the gl_texture_image struct.
597 *
598 * \param ctx GL context.
599 * \param img texture image structure to be initialized.
600 * \param width image width.
601 * \param height image height.
602 * \param depth image depth.
603 * \param border image border.
604 * \param internalFormat internal format.
605 * \param format the actual hardware format (one of MESA_FORMAT_*)
606 *
607 * Fills in the fields of \p img with the given information.
608 * Note: width, height and depth include the border.
609 */
610 void
611 _mesa_init_teximage_fields(struct gl_context *ctx,
612 struct gl_texture_image *img,
613 GLsizei width, GLsizei height, GLsizei depth,
614 GLint border, GLenum internalFormat,
615 gl_format format)
616 {
617 GLenum target;
618 ASSERT(img);
619 ASSERT(width >= 0);
620 ASSERT(height >= 0);
621 ASSERT(depth >= 0);
622
623 target = img->TexObject->Target;
624 img->_BaseFormat = _mesa_base_tex_format( ctx, internalFormat );
625 ASSERT(img->_BaseFormat > 0);
626 img->InternalFormat = internalFormat;
627 img->Border = border;
628 img->Width = width;
629 img->Height = height;
630 img->Depth = depth;
631
632 img->Width2 = width - 2 * border; /* == 1 << img->WidthLog2; */
633 img->WidthLog2 = _mesa_logbase2(img->Width2);
634
635 switch(target) {
636 case GL_TEXTURE_1D:
637 case GL_PROXY_TEXTURE_1D:
638 if (height == 0)
639 img->Height2 = 0;
640 else
641 img->Height2 = 1;
642 img->HeightLog2 = 0;
643 if (depth == 0)
644 img->Depth2 = 0;
645 else
646 img->Depth2 = 1;
647 img->DepthLog2 = 0;
648 break;
649 case GL_TEXTURE_2D:
650 case GL_TEXTURE_CUBE_MAP:
651 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
652 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
653 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
654 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
655 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
656 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
657 case GL_PROXY_TEXTURE_2D:
658 case GL_PROXY_TEXTURE_CUBE_MAP:
659 img->Height2 = height - 2 * border; /* == 1 << img->HeightLog2; */
660 img->HeightLog2 = _mesa_logbase2(img->Height2);
661 if (depth == 0)
662 img->Depth2 = 0;
663 else
664 img->Depth2 = 1;
665 img->DepthLog2 = 0;
666 break;
667 default:
668 _mesa_problem(NULL, "invalid target 0x%x in _mesa_init_teximage_fields()",
669 target);
670 }
671
672 img->MaxLog2 = MAX2(img->WidthLog2, img->HeightLog2);
673 img->TexFormat = format;
674 }
675
676
677 /**
678 * Free and clear fields of the gl_texture_image struct.
679 *
680 * \param ctx GL context.
681 * \param texImage texture image structure to be cleared.
682 *
683 * After the call, \p texImage will have no data associated with it. Its
684 * fields are cleared so that its parent object will test incomplete.
685 */
686 void
687 _mesa_clear_texture_image(struct gl_context *ctx,
688 struct gl_texture_image *texImage)
689 {
690 ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
691 clear_teximage_fields(texImage);
692 }
693
694
695 /**
696 * This is the fallback for Driver.TestProxyTexImage(). Test the texture
697 * level, width, height and depth against the ctx->Const limits for textures.
698 *
699 * A hardware driver might override this function if, for example, the
700 * max 3D texture size is 512x512x64 (i.e. not a cube).
701 *
702 * Note that width, height, depth == 0 is not an error. However, a
703 * texture with zero width/height/depth will be considered "incomplete"
704 * and texturing will effectively be disabled.
705 *
706 * \param target one of GL_PROXY_TEXTURE_1D, GL_PROXY_TEXTURE_2D,
707 * GL_PROXY_TEXTURE_3D, GL_PROXY_TEXTURE_RECTANGLE_NV,
708 * GL_PROXY_TEXTURE_CUBE_MAP_ARB.
709 * \param level as passed to glTexImage
710 * \param internalFormat as passed to glTexImage
711 * \param format as passed to glTexImage
712 * \param type as passed to glTexImage
713 * \param width as passed to glTexImage
714 * \param height as passed to glTexImage
715 * \param depth as passed to glTexImage
716 * \param border as passed to glTexImage
717 * \return GL_TRUE if the image is acceptable, GL_FALSE if not acceptable.
718 */
719 GLboolean
720 _mesa_test_proxy_teximage(struct gl_context *ctx, GLenum target, GLint level,
721 GLint internalFormat, GLenum format, GLenum type,
722 GLint width, GLint height, GLint depth, GLint border)
723 {
724 GLint maxSize;
725
726 (void) internalFormat;
727 (void) format;
728 (void) type;
729
730 switch (target) {
731 case GL_PROXY_TEXTURE_1D:
732 maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
733 if (width < 2 * border || width > 2 * border + maxSize)
734 return GL_FALSE;
735 if (level >= ctx->Const.MaxTextureLevels)
736 return GL_FALSE;
737 if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
738 return GL_FALSE;
739 return GL_TRUE;
740
741 case GL_PROXY_TEXTURE_2D:
742 maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
743 if (width < 2 * border || width > 2 * border + maxSize)
744 return GL_FALSE;
745 if (height < 2 * border || height > 2 * border + maxSize)
746 return GL_FALSE;
747 if (level >= ctx->Const.MaxTextureLevels)
748 return GL_FALSE;
749 if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
750 return GL_FALSE;
751 if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
752 return GL_FALSE;
753 return GL_TRUE;
754
755 case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
756 maxSize = 1 << (ctx->Const.MaxCubeTextureLevels - 1);
757 if (width < 2 * border || width > 2 * border + maxSize)
758 return GL_FALSE;
759 if (height < 2 * border || height > 2 * border + maxSize)
760 return GL_FALSE;
761 if (level >= ctx->Const.MaxCubeTextureLevels)
762 return GL_FALSE;
763 if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
764 return GL_FALSE;
765 if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
766 return GL_FALSE;
767 return GL_TRUE;
768
769 default:
770 _mesa_problem(ctx, "Invalid target in _mesa_test_proxy_teximage");
771 return GL_FALSE;
772 }
773 }
774
775
776 /**
777 * Check if the memory used by the texture would exceed the driver's limit.
778 * This lets us support a max 3D texture size of 8K (for example) but
779 * prevents allocating a full 8K x 8K x 8K texture.
780 * XXX this could be rolled into the proxy texture size test (above) but
781 * we don't have the actual texture internal format at that point.
782 */
783 static GLboolean
784 legal_texture_size(struct gl_context *ctx, gl_format format,
785 GLint width, GLint height, GLint depth)
786 {
787 uint64_t bytes = _mesa_format_image_size64(format, width, height, depth);
788 uint64_t mbytes = bytes / (1024 * 1024); /* convert to MB */
789 return mbytes <= (uint64_t) ctx->Const.MaxTextureMbytes;
790 }
791
792 /**
793 * Check if the given texture target value is legal for a
794 * glTexImage1/2/3D call.
795 */
796 static GLboolean
797 legal_teximage_target(struct gl_context *ctx, GLuint dims, GLenum target)
798 {
799 switch (dims) {
800 case 1:
801 switch (target) {
802 case GL_TEXTURE_1D:
803 case GL_PROXY_TEXTURE_1D:
804 return GL_TRUE;
805 default:
806 return GL_FALSE;
807 }
808 case 2:
809 switch (target) {
810 case GL_TEXTURE_2D:
811 case GL_PROXY_TEXTURE_2D:
812 return GL_TRUE;
813 case GL_PROXY_TEXTURE_CUBE_MAP:
814 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
815 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
816 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
817 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
818 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
819 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
820 return ctx->Extensions.ARB_texture_cube_map;
821 default:
822 return GL_FALSE;
823 }
824 default:
825 _mesa_problem(ctx, "invalid dims=%u in legal_teximage_target()", dims);
826 return GL_FALSE;
827 }
828 }
829
830
831 /**
832 * Check if the given texture target value is legal for a
833 * glTexSubImage, glCopyTexSubImage or glCopyTexImage call.
834 * The difference compared to legal_teximage_target() above is that
835 * proxy targets are not supported.
836 */
837 static GLboolean
838 legal_texsubimage_target(struct gl_context *ctx, GLuint dims, GLenum target)
839 {
840 switch (dims) {
841 case 1:
842 return target == GL_TEXTURE_1D;
843 case 2:
844 switch (target) {
845 case GL_TEXTURE_2D:
846 return GL_TRUE;
847 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
848 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
849 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
850 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
851 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
852 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
853 return ctx->Extensions.ARB_texture_cube_map;
854 default:
855 return GL_FALSE;
856 }
857 default:
858 _mesa_problem(ctx, "invalid dims=%u in legal_texsubimage_target()",
859 dims);
860 return GL_FALSE;
861 }
862 }
863
864
865 /**
866 * Helper function to determine if a texture object is mutable (in terms
867 * of GL_ARB_texture_storage).
868 */
869 static GLboolean
870 mutable_tex_object(struct gl_context *ctx, GLenum target)
871 {
872 if (ctx->Extensions.ARB_texture_storage) {
873 struct gl_texture_object *texObj =
874 _mesa_select_tex_object(ctx, target);
875 return !texObj->Immutable;
876 }
877 return GL_TRUE;
878 }
879
880
881
882 /**
883 * Test the glTexImage[123]D() parameters for errors.
884 *
885 * \param ctx GL context.
886 * \param dimensions texture image dimensions (must be 1, 2 or 3).
887 * \param target texture target given by the user.
888 * \param level image level given by the user.
889 * \param internalFormat internal format given by the user.
890 * \param format pixel data format given by the user.
891 * \param type pixel data type given by the user.
892 * \param width image width given by the user.
893 * \param height image height given by the user.
894 * \param depth image depth given by the user.
895 * \param border image border given by the user.
896 *
897 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
898 *
899 * Verifies each of the parameters against the constants specified in
900 * __struct gl_contextRec::Const and the supported extensions, and according
901 * to the OpenGL specification.
902 */
903 static GLboolean
904 texture_error_check( struct gl_context *ctx,
905 GLuint dimensions, GLenum target,
906 GLint level, GLint internalFormat,
907 GLenum format, GLenum type,
908 GLint width, GLint height,
909 GLint depth, GLint border )
910 {
911 const GLenum proxyTarget = get_proxy_target(target);
912 const GLboolean isProxy = target == proxyTarget;
913 GLboolean sizeOK = GL_TRUE;
914 GLboolean colorFormat;
915 GLenum err;
916
917 /* Even though there are no color-index textures, we still have to support
918 * uploading color-index data and remapping it to RGB via the
919 * GL_PIXEL_MAP_I_TO_[RGBA] tables.
920 */
921 const GLboolean indexFormat = (format == GL_COLOR_INDEX);
922
923 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
924 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
925 if (!isProxy) {
926 _mesa_error(ctx, GL_INVALID_VALUE,
927 "glTexImage%dD(level=%d)", dimensions, level);
928 }
929 return GL_TRUE;
930 }
931
932 /* Check border */
933 if (border < 0 || border > 1) {
934 if (!isProxy) {
935 _mesa_error(ctx, GL_INVALID_VALUE,
936 "glTexImage%dD(border=%d)", dimensions, border);
937 }
938 return GL_TRUE;
939 }
940
941 if (width < 0 || height < 0 || depth < 0) {
942 if (!isProxy) {
943 _mesa_error(ctx, GL_INVALID_VALUE,
944 "glTexImage%dD(width, height or depth < 0)", dimensions);
945 }
946 return GL_TRUE;
947 }
948
949 /* Do this simple check before calling the TestProxyTexImage() function */
950 if (proxyTarget == GL_PROXY_TEXTURE_CUBE_MAP_ARB) {
951 sizeOK = (width == height);
952 }
953
954 /*
955 * Use the proxy texture driver hook to see if the size/level/etc are
956 * legal.
957 */
958 sizeOK = sizeOK && ctx->Driver.TestProxyTexImage(ctx, proxyTarget, level,
959 internalFormat, format,
960 type, width, height,
961 depth, border);
962 if (!sizeOK) {
963 if (!isProxy) {
964 _mesa_error(ctx, GL_INVALID_VALUE,
965 "glTexImage%dD(level=%d, width=%d, height=%d, depth=%d)",
966 dimensions, level, width, height, depth);
967 }
968 return GL_TRUE;
969 }
970
971 /* Check internalFormat */
972 if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
973 if (!isProxy) {
974 _mesa_error(ctx, GL_INVALID_VALUE,
975 "glTexImage%dD(internalFormat=%s)",
976 dimensions, _mesa_lookup_enum_by_nr(internalFormat));
977 }
978 return GL_TRUE;
979 }
980
981 /* Check incoming image format and type */
982 err = _mesa_error_check_format_and_type(ctx, format, type);
983 if (err != GL_NO_ERROR) {
984 if (!isProxy) {
985 _mesa_error(ctx, err,
986 "glTexImage%dD(incompatible format 0x%x, type 0x%x)",
987 dimensions, format, type);
988 }
989 return GL_TRUE;
990 }
991
992 /* make sure internal format and format basically agree */
993 colorFormat = _mesa_is_color_format(format);
994 if ((_mesa_is_color_format(internalFormat) && !colorFormat && !indexFormat) ||
995 (_mesa_is_depth_format(internalFormat) != _mesa_is_depth_format(format)) ||
996 (_mesa_is_ycbcr_format(internalFormat) != _mesa_is_ycbcr_format(format))) {
997 if (!isProxy)
998 _mesa_error(ctx, GL_INVALID_OPERATION,
999 "glTexImage%dD(incompatible internalFormat 0x%x, format 0x%x)",
1000 dimensions, internalFormat, format);
1001 return GL_TRUE;
1002 }
1003
1004 /* additional checks for ycbcr textures */
1005 if (internalFormat == GL_YCBCR_MESA) {
1006 ASSERT(ctx->Extensions.MESA_ycbcr_texture);
1007 if (type != GL_UNSIGNED_SHORT_8_8_MESA &&
1008 type != GL_UNSIGNED_SHORT_8_8_REV_MESA) {
1009 char message[100];
1010 _mesa_snprintf(message, sizeof(message),
1011 "glTexImage%dD(format/type YCBCR mismatch", dimensions);
1012 _mesa_error(ctx, GL_INVALID_ENUM, "%s", message);
1013 return GL_TRUE; /* error */
1014 }
1015 if (target != GL_TEXTURE_2D &&
1016 target != GL_PROXY_TEXTURE_2D) {
1017 if (!isProxy)
1018 _mesa_error(ctx, GL_INVALID_ENUM, "glTexImage(target)");
1019 return GL_TRUE;
1020 }
1021 if (border != 0) {
1022 if (!isProxy) {
1023 char message[100];
1024 _mesa_snprintf(message, sizeof(message),
1025 "glTexImage%dD(format=GL_YCBCR_MESA and border=%d)",
1026 dimensions, border);
1027 _mesa_error(ctx, GL_INVALID_VALUE, "%s", message);
1028 }
1029 return GL_TRUE;
1030 }
1031 }
1032
1033 /* additional checks for depth textures */
1034 if (_mesa_base_tex_format(ctx, internalFormat) == GL_DEPTH_COMPONENT) {
1035 /* Only 1D, 2D, rect, array and cube textures supported, not 3D
1036 * Cubemaps are only supported for GL version > 3.0 or with EXT_gpu_shader4 */
1037 if (target != GL_TEXTURE_1D &&
1038 target != GL_PROXY_TEXTURE_1D &&
1039 target != GL_TEXTURE_2D &&
1040 target != GL_PROXY_TEXTURE_2D &&
1041 !((_mesa_is_cube_face(target) || target == GL_PROXY_TEXTURE_CUBE_MAP) &&
1042 (ctx->VersionMajor >= 3))) {
1043 if (!isProxy)
1044 _mesa_error(ctx, GL_INVALID_ENUM,
1045 "glTexImage(target/internalFormat)");
1046 return GL_TRUE;
1047 }
1048 }
1049
1050 /* additional checks for integer textures */
1051 if ((ctx->VersionMajor >= 3 || ctx->Extensions.EXT_texture_integer) &&
1052 (_mesa_is_integer_format(format) !=
1053 _mesa_is_integer_format(internalFormat))) {
1054 if (!isProxy) {
1055 _mesa_error(ctx, GL_INVALID_OPERATION,
1056 "glTexImage%dD(integer/non-integer format mismatch)",
1057 dimensions);
1058 }
1059 return GL_TRUE;
1060 }
1061
1062 if (!mutable_tex_object(ctx, target)) {
1063 _mesa_error(ctx, GL_INVALID_OPERATION,
1064 "glTexImage%dD(immutable texture)", dimensions);
1065 return GL_TRUE;
1066 }
1067
1068 /* if we get here, the parameters are OK */
1069 return GL_FALSE;
1070 }
1071
1072
1073 /**
1074 * Test glTexSubImage[123]D() parameters for errors.
1075 *
1076 * \param ctx GL context.
1077 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1078 * \param target texture target given by the user.
1079 * \param level image level given by the user.
1080 * \param xoffset sub-image x offset given by the user.
1081 * \param yoffset sub-image y offset given by the user.
1082 * \param zoffset sub-image z offset given by the user.
1083 * \param format pixel data format given by the user.
1084 * \param type pixel data type given by the user.
1085 * \param width image width given by the user.
1086 * \param height image height given by the user.
1087 * \param depth image depth given by the user.
1088 *
1089 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1090 *
1091 * Verifies each of the parameters against the constants specified in
1092 * __struct gl_contextRec::Const and the supported extensions, and according
1093 * to the OpenGL specification.
1094 */
1095 static GLboolean
1096 subtexture_error_check( struct gl_context *ctx, GLuint dimensions,
1097 GLenum target, GLint level,
1098 GLint xoffset, GLint yoffset, GLint zoffset,
1099 GLint width, GLint height, GLint depth,
1100 GLenum format, GLenum type )
1101 {
1102 GLenum err;
1103
1104 /* Basic level check */
1105 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
1106 _mesa_error(ctx, GL_INVALID_ENUM, "glTexSubImage2D(level=%d)", level);
1107 return GL_TRUE;
1108 }
1109
1110 /* Check for negative sizes */
1111 if (width < 0) {
1112 _mesa_error(ctx, GL_INVALID_VALUE,
1113 "glTexSubImage%dD(width=%d)", dimensions, width);
1114 return GL_TRUE;
1115 }
1116 if (height < 0 && dimensions > 1) {
1117 _mesa_error(ctx, GL_INVALID_VALUE,
1118 "glTexSubImage%dD(height=%d)", dimensions, height);
1119 return GL_TRUE;
1120 }
1121 if (depth < 0 && dimensions > 2) {
1122 _mesa_error(ctx, GL_INVALID_VALUE,
1123 "glTexSubImage%dD(depth=%d)", dimensions, depth);
1124 return GL_TRUE;
1125 }
1126
1127 err = _mesa_error_check_format_and_type(ctx, format, type);
1128 if (err != GL_NO_ERROR) {
1129 _mesa_error(ctx, err,
1130 "glTexSubImage%dD(incompatible format 0x%x, type 0x%x)",
1131 dimensions, format, type);
1132 return GL_TRUE;
1133 }
1134
1135 return GL_FALSE;
1136 }
1137
1138
1139 /**
1140 * Do second part of glTexSubImage which depends on the destination texture.
1141 * \return GL_TRUE if error recorded, GL_FALSE otherwise
1142 */
1143 static GLboolean
1144 subtexture_error_check2( struct gl_context *ctx, GLuint dimensions,
1145 GLenum target, GLint level,
1146 GLint xoffset, GLint yoffset, GLint zoffset,
1147 GLint width, GLint height, GLint depth,
1148 GLenum format, GLenum type,
1149 const struct gl_texture_image *destTex )
1150 {
1151 if (!destTex) {
1152 /* undefined image level */
1153 _mesa_error(ctx, GL_INVALID_OPERATION, "glTexSubImage%dD", dimensions);
1154 return GL_TRUE;
1155 }
1156
1157 if (xoffset < -((GLint)destTex->Border)) {
1158 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(xoffset)",
1159 dimensions);
1160 return GL_TRUE;
1161 }
1162 if (xoffset + width > (GLint) (destTex->Width + destTex->Border)) {
1163 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(xoffset+width)",
1164 dimensions);
1165 return GL_TRUE;
1166 }
1167 if (dimensions > 1) {
1168 if (yoffset < -((GLint)destTex->Border)) {
1169 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(yoffset)",
1170 dimensions);
1171 return GL_TRUE;
1172 }
1173 if (yoffset + height > (GLint) (destTex->Height + destTex->Border)) {
1174 _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(yoffset+height)",
1175 dimensions);
1176 return GL_TRUE;
1177 }
1178 }
1179
1180 if (ctx->VersionMajor >= 3 || ctx->Extensions.EXT_texture_integer) {
1181 /* both source and dest must be integer-valued, or neither */
1182 if (_mesa_is_format_integer_color(destTex->TexFormat) !=
1183 _mesa_is_integer_format(format)) {
1184 _mesa_error(ctx, GL_INVALID_OPERATION,
1185 "glTexSubImage%dD(integer/non-integer format mismatch)",
1186 dimensions);
1187 return GL_TRUE;
1188 }
1189 }
1190
1191 return GL_FALSE;
1192 }
1193
1194
1195 /**
1196 * Test glCopyTexImage[12]D() parameters for errors.
1197 *
1198 * \param ctx GL context.
1199 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1200 * \param target texture target given by the user.
1201 * \param level image level given by the user.
1202 * \param internalFormat internal format given by the user.
1203 * \param width image width given by the user.
1204 * \param height image height given by the user.
1205 * \param border texture border.
1206 *
1207 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1208 *
1209 * Verifies each of the parameters against the constants specified in
1210 * __struct gl_contextRec::Const and the supported extensions, and according
1211 * to the OpenGL specification.
1212 */
1213 static GLboolean
1214 copytexture_error_check( struct gl_context *ctx, GLuint dimensions,
1215 GLenum target, GLint level, GLint internalFormat,
1216 GLint width, GLint height, GLint border )
1217 {
1218 const GLenum proxyTarget = get_proxy_target(target);
1219 const GLenum type = GL_FLOAT;
1220 GLboolean sizeOK;
1221 GLint baseFormat;
1222
1223 /* check target */
1224 if (!legal_texsubimage_target(ctx, dimensions, target)) {
1225 _mesa_error(ctx, GL_INVALID_ENUM, "glCopyTexImage%uD(target=%s)",
1226 dimensions, _mesa_lookup_enum_by_nr(target));
1227 return GL_TRUE;
1228 }
1229
1230 /* Basic level check (more checking in ctx->Driver.TestProxyTexImage) */
1231 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
1232 _mesa_error(ctx, GL_INVALID_VALUE,
1233 "glCopyTexImage%dD(level=%d)", dimensions, level);
1234 return GL_TRUE;
1235 }
1236
1237 /* Check border */
1238 if (border < 0 || border > 1) {
1239 return GL_TRUE;
1240 }
1241
1242 baseFormat = _mesa_base_tex_format(ctx, internalFormat);
1243 if (baseFormat < 0) {
1244 _mesa_error(ctx, GL_INVALID_VALUE,
1245 "glCopyTexImage%dD(internalFormat)", dimensions);
1246 return GL_TRUE;
1247 }
1248
1249 if (!_mesa_source_buffer_exists(ctx, baseFormat)) {
1250 _mesa_error(ctx, GL_INVALID_OPERATION,
1251 "glCopyTexImage%dD(missing readbuffer)", dimensions);
1252 return GL_TRUE;
1253 }
1254
1255 /* From the EXT_texture_integer spec:
1256 *
1257 * "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
1258 * if the texture internalformat is an integer format and the read color
1259 * buffer is not an integer format, or if the internalformat is not an
1260 * integer format and the read color buffer is an integer format."
1261 */
1262 if (_mesa_is_color_format(internalFormat)) {
1263 struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
1264
1265 if (_mesa_is_integer_format(rb->InternalFormat) !=
1266 _mesa_is_integer_format(internalFormat)) {
1267 _mesa_error(ctx, GL_INVALID_OPERATION,
1268 "glCopyTexImage%dD(integer vs non-integer)", dimensions);
1269 return GL_TRUE;
1270 }
1271 }
1272
1273 /* Do size, level checking */
1274 sizeOK = (proxyTarget == GL_PROXY_TEXTURE_CUBE_MAP_ARB)
1275 ? (width == height) : 1;
1276
1277 sizeOK = sizeOK && ctx->Driver.TestProxyTexImage(ctx, proxyTarget, level,
1278 internalFormat, baseFormat,
1279 type, width, height,
1280 1, border);
1281
1282 if (!sizeOK) {
1283 if (dimensions == 1) {
1284 _mesa_error(ctx, GL_INVALID_VALUE,
1285 "glCopyTexImage1D(width=%d)", width);
1286 }
1287 else {
1288 ASSERT(dimensions == 2);
1289 _mesa_error(ctx, GL_INVALID_VALUE,
1290 "glCopyTexImage2D(width=%d, height=%d)", width, height);
1291 }
1292 return GL_TRUE;
1293 }
1294
1295 if (!mutable_tex_object(ctx, target)) {
1296 _mesa_error(ctx, GL_INVALID_OPERATION,
1297 "glCopyTexImage%dD(immutable texture)", dimensions);
1298 return GL_TRUE;
1299 }
1300
1301 /* if we get here, the parameters are OK */
1302 return GL_FALSE;
1303 }
1304
1305
1306 /**
1307 * Test glCopyTexSubImage[12]D() parameters for errors.
1308 * Note that this is the first part of error checking.
1309 * See also copytexsubimage_error_check2() below for the second part.
1310 *
1311 * \param ctx GL context.
1312 * \param dimensions texture image dimensions (must be 1, 2 or 3).
1313 * \param target texture target given by the user.
1314 * \param level image level given by the user.
1315 *
1316 * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
1317 */
1318 static GLboolean
1319 copytexsubimage_error_check1( struct gl_context *ctx, GLuint dimensions,
1320 GLenum target, GLint level)
1321 {
1322 /* check target (proxies not allowed) */
1323 if (!legal_texsubimage_target(ctx, dimensions, target)) {
1324 _mesa_error(ctx, GL_INVALID_ENUM, "glCopyTexSubImage%uD(target=%s)",
1325 dimensions, _mesa_lookup_enum_by_nr(target));
1326 return GL_TRUE;
1327 }
1328
1329 /* Check level */
1330 if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
1331 _mesa_error(ctx, GL_INVALID_VALUE,
1332 "glCopyTexSubImage%dD(level=%d)", dimensions, level);
1333 return GL_TRUE;
1334 }
1335
1336 return GL_FALSE;
1337 }
1338
1339
1340 /**
1341 * Second part of error checking for glCopyTexSubImage[12]D().
1342 * \param xoffset sub-image x offset given by the user.
1343 * \param yoffset sub-image y offset given by the user.
1344 * \param zoffset sub-image z offset given by the user.
1345 * \param width image width given by the user.
1346 * \param height image height given by the user.
1347 */
1348 static GLboolean
1349 copytexsubimage_error_check2( struct gl_context *ctx, GLuint dimensions,
1350 GLenum target, GLint level,
1351 GLint xoffset, GLint yoffset, GLint zoffset,
1352 GLsizei width, GLsizei height,
1353 const struct gl_texture_image *teximage )
1354 {
1355 /* check that dest tex image exists */
1356 if (!teximage) {
1357 _mesa_error(ctx, GL_INVALID_OPERATION,
1358 "glCopyTexSubImage%dD(undefined texture level: %d)",
1359 dimensions, level);
1360 return GL_TRUE;
1361 }
1362
1363 /* Check size */
1364 if (width < 0) {
1365 _mesa_error(ctx, GL_INVALID_VALUE,
1366 "glCopyTexSubImage%dD(width=%d)", dimensions, width);
1367 return GL_TRUE;
1368 }
1369 if (dimensions > 1 && height < 0) {
1370 _mesa_error(ctx, GL_INVALID_VALUE,
1371 "glCopyTexSubImage%dD(height=%d)", dimensions, height);
1372 return GL_TRUE;
1373 }
1374
1375 /* check x/y offsets */
1376 if (xoffset < -((GLint)teximage->Border)) {
1377 _mesa_error(ctx, GL_INVALID_VALUE,
1378 "glCopyTexSubImage%dD(xoffset=%d)", dimensions, xoffset);
1379 return GL_TRUE;
1380 }
1381 if (xoffset + width > (GLint) (teximage->Width + teximage->Border)) {
1382 _mesa_error(ctx, GL_INVALID_VALUE,
1383 "glCopyTexSubImage%dD(xoffset+width)", dimensions);
1384 return GL_TRUE;
1385 }
1386 if (dimensions > 1) {
1387 if (yoffset < -((GLint)teximage->Border)) {
1388 _mesa_error(ctx, GL_INVALID_VALUE,
1389 "glCopyTexSubImage%dD(yoffset=%d)", dimensions, yoffset);
1390 return GL_TRUE;
1391 }
1392 /* NOTE: we're adding the border here, not subtracting! */
1393 if (yoffset + height > (GLint) (teximage->Height + teximage->Border)) {
1394 _mesa_error(ctx, GL_INVALID_VALUE,
1395 "glCopyTexSubImage%dD(yoffset+height)", dimensions);
1396 return GL_TRUE;
1397 }
1398 }
1399
1400 /* check z offset */
1401 if (dimensions > 2) {
1402 if (zoffset < -((GLint)teximage->Border)) {
1403 _mesa_error(ctx, GL_INVALID_VALUE,
1404 "glCopyTexSubImage%dD(zoffset)", dimensions);
1405 return GL_TRUE;
1406 }
1407 if (zoffset > (GLint) (teximage->Depth + teximage->Border)) {
1408 _mesa_error(ctx, GL_INVALID_VALUE,
1409 "glCopyTexSubImage%dD(zoffset+depth)", dimensions);
1410 return GL_TRUE;
1411 }
1412 }
1413
1414 if (teximage->InternalFormat == GL_YCBCR_MESA) {
1415 _mesa_error(ctx, GL_INVALID_OPERATION, "glCopyTexSubImage2D");
1416 return GL_TRUE;
1417 }
1418
1419 if (!_mesa_source_buffer_exists(ctx, teximage->_BaseFormat)) {
1420 _mesa_error(ctx, GL_INVALID_OPERATION,
1421 "glCopyTexSubImage%dD(missing readbuffer, format=0x%x)",
1422 dimensions, teximage->_BaseFormat);
1423 return GL_TRUE;
1424 }
1425
1426 /* From the EXT_texture_integer spec:
1427 *
1428 * "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
1429 * if the texture internalformat is an integer format and the read color
1430 * buffer is not an integer format, or if the internalformat is not an
1431 * integer format and the read color buffer is an integer format."
1432 */
1433 if (_mesa_is_color_format(teximage->InternalFormat)) {
1434 struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
1435
1436 if (_mesa_is_format_integer_color(rb->Format) !=
1437 _mesa_is_format_integer_color(teximage->TexFormat)) {
1438 _mesa_error(ctx, GL_INVALID_OPERATION,
1439 "glCopyTexImage%dD(integer vs non-integer)", dimensions);
1440 return GL_TRUE;
1441 }
1442 }
1443
1444 /* if we get here, the parameters are OK */
1445 return GL_FALSE;
1446 }
1447
1448
1449 /** Callback info for walking over FBO hash table */
1450 struct cb_info
1451 {
1452 struct gl_context *ctx;
1453 struct gl_texture_object *texObj;
1454 GLuint level, face;
1455 };
1456
1457 /** Debug helper: override the user-requested internal format */
1458 static GLenum
1459 override_internal_format(GLenum internalFormat, GLint width, GLint height)
1460 {
1461 #if 0
1462 if (internalFormat == GL_RGBA16F_ARB ||
1463 internalFormat == GL_RGBA32F_ARB) {
1464 printf("Convert rgba float tex to int %d x %d\n", width, height);
1465 return GL_RGBA;
1466 }
1467 else if (internalFormat == GL_RGB16F_ARB ||
1468 internalFormat == GL_RGB32F_ARB) {
1469 printf("Convert rgb float tex to int %d x %d\n", width, height);
1470 return GL_RGB;
1471 }
1472 else if (internalFormat == GL_LUMINANCE_ALPHA16F_ARB ||
1473 internalFormat == GL_LUMINANCE_ALPHA32F_ARB) {
1474 printf("Convert luminance float tex to int %d x %d\n", width, height);
1475 return GL_LUMINANCE_ALPHA;
1476 }
1477 else if (internalFormat == GL_LUMINANCE16F_ARB ||
1478 internalFormat == GL_LUMINANCE32F_ARB) {
1479 printf("Convert luminance float tex to int %d x %d\n", width, height);
1480 return GL_LUMINANCE;
1481 }
1482 else if (internalFormat == GL_ALPHA16F_ARB ||
1483 internalFormat == GL_ALPHA32F_ARB) {
1484 printf("Convert luminance float tex to int %d x %d\n", width, height);
1485 return GL_ALPHA;
1486 }
1487 else {
1488 return internalFormat;
1489 }
1490 #else
1491 return internalFormat;
1492 #endif
1493 }
1494
1495
1496 /**
1497 * Choose the actual hardware format for a texture image.
1498 * Try to use the same format as the previous image level when possible.
1499 * Otherwise, ask the driver for the best format.
1500 * It's important to try to choose a consistant format for all levels
1501 * for efficient texture memory layout/allocation. In particular, this
1502 * comes up during automatic mipmap generation.
1503 */
1504 gl_format
1505 _mesa_choose_texture_format(struct gl_context *ctx,
1506 struct gl_texture_object *texObj,
1507 GLenum target, GLint level,
1508 GLenum internalFormat, GLenum format, GLenum type)
1509 {
1510 gl_format f;
1511
1512 /* see if we've already chosen a format for the previous level */
1513 if (level > 0) {
1514 struct gl_texture_image *prevImage =
1515 _mesa_select_tex_image(ctx, texObj, target, level - 1);
1516 /* See if the prev level is defined and has an internal format which
1517 * matches the new internal format.
1518 */
1519 if (prevImage &&
1520 prevImage->Width > 0 &&
1521 prevImage->InternalFormat == internalFormat) {
1522 /* use the same format */
1523 ASSERT(prevImage->TexFormat != MESA_FORMAT_NONE);
1524 return prevImage->TexFormat;
1525 }
1526 }
1527
1528 /* choose format from scratch */
1529 f = ctx->Driver.ChooseTextureFormat(ctx, internalFormat, format, type);
1530 ASSERT(f != MESA_FORMAT_NONE);
1531 return f;
1532 }
1533
1534 /**
1535 * Adjust pixel unpack params and image dimensions to strip off the
1536 * texture border.
1537 *
1538 * Gallium and intel don't support texture borders. They've seldem been used
1539 * and seldom been implemented correctly anyway.
1540 *
1541 * \param unpackNew returns the new pixel unpack parameters
1542 */
1543 static void
1544 strip_texture_border(GLint *border,
1545 GLint *width, GLint *height, GLint *depth,
1546 const struct gl_pixelstore_attrib *unpack,
1547 struct gl_pixelstore_attrib *unpackNew)
1548 {
1549 assert(*border > 0); /* sanity check */
1550
1551 *unpackNew = *unpack;
1552
1553 if (unpackNew->RowLength == 0)
1554 unpackNew->RowLength = *width;
1555
1556 if (depth && unpackNew->ImageHeight == 0)
1557 unpackNew->ImageHeight = *height;
1558
1559 unpackNew->SkipPixels += *border;
1560 if (height)
1561 unpackNew->SkipRows += *border;
1562 if (depth)
1563 unpackNew->SkipImages += *border;
1564
1565 assert(*width >= 3);
1566 *width = *width - 2 * *border;
1567 if (height && *height >= 3)
1568 *height = *height - 2 * *border;
1569 if (depth && *depth >= 3)
1570 *depth = *depth - 2 * *border;
1571 *border = 0;
1572 }
1573
1574 /**
1575 * Common code to implement all the glTexImage1D/2D/3D functions.
1576 */
1577 static void
1578 teximage(struct gl_context *ctx, GLuint dims,
1579 GLenum target, GLint level, GLint internalFormat,
1580 GLsizei width, GLsizei height, GLsizei depth,
1581 GLint border, GLenum format, GLenum type,
1582 const GLvoid *pixels)
1583 {
1584 GLboolean error;
1585 struct gl_pixelstore_attrib unpack_no_border;
1586 const struct gl_pixelstore_attrib *unpack = &ctx->Unpack;
1587
1588 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1589
1590 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
1591 _mesa_debug(ctx, "glTexImage%uD %s %d %s %d %d %d %d %s %s %p\n",
1592 dims,
1593 _mesa_lookup_enum_by_nr(target), level,
1594 _mesa_lookup_enum_by_nr(internalFormat),
1595 width, height, depth, border,
1596 _mesa_lookup_enum_by_nr(format),
1597 _mesa_lookup_enum_by_nr(type), pixels);
1598
1599 internalFormat = override_internal_format(internalFormat, width, height);
1600
1601 /* target error checking */
1602 if (!legal_teximage_target(ctx, dims, target)) {
1603 _mesa_error(ctx, GL_INVALID_ENUM, "glTexImage%uD(target=%s)",
1604 dims, _mesa_lookup_enum_by_nr(target));
1605 return;
1606 }
1607
1608 /* general error checking */
1609 error = texture_error_check(ctx, dims, target, level, internalFormat,
1610 format, type, width, height, depth, border);
1611
1612 if (_mesa_is_proxy_texture(target)) {
1613 /* Proxy texture: just clear or set state depending on error checking */
1614 struct gl_texture_image *texImage =
1615 _mesa_get_proxy_tex_image(ctx, target, level);
1616
1617 if (error) {
1618 /* when error, clear all proxy texture image parameters */
1619 if (texImage)
1620 clear_teximage_fields(texImage);
1621 }
1622 else {
1623 /* no error, set the tex image parameters */
1624 struct gl_texture_object *texObj =
1625 _mesa_select_tex_object(ctx, target);
1626 gl_format texFormat = _mesa_choose_texture_format(ctx, texObj,
1627 target, level,
1628 internalFormat,
1629 format, type);
1630
1631 if (legal_texture_size(ctx, texFormat, width, height, depth)) {
1632 _mesa_init_teximage_fields(ctx, texImage, width, height,
1633 depth, border, internalFormat,
1634 texFormat);
1635 }
1636 else if (texImage) {
1637 clear_teximage_fields(texImage);
1638 }
1639 }
1640 }
1641 else {
1642 /* non-proxy target */
1643 struct gl_texture_object *texObj;
1644 struct gl_texture_image *texImage;
1645
1646 if (error) {
1647 return; /* error was recorded */
1648 }
1649
1650 /* Allow a hardware driver to just strip out the border, to provide
1651 * reliable but slightly incorrect hardware rendering instead of
1652 * rarely-tested software fallback rendering.
1653 */
1654 if (border && ctx->Const.StripTextureBorder) {
1655 strip_texture_border(&border, &width, &height, &depth, unpack,
1656 &unpack_no_border);
1657 unpack = &unpack_no_border;
1658 }
1659
1660 if (ctx->NewState & _NEW_PIXEL)
1661 _mesa_update_state(ctx);
1662
1663 texObj = _mesa_select_tex_object(ctx, target);
1664
1665 _mesa_lock_texture(ctx, texObj);
1666 {
1667 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
1668
1669 if (!texImage) {
1670 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage%uD", dims);
1671 }
1672 else {
1673 gl_format texFormat;
1674
1675 ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
1676
1677 texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
1678 internalFormat, format,
1679 type);
1680
1681 if (legal_texture_size(ctx, texFormat, width, height, depth)) {
1682 _mesa_init_teximage_fields(ctx, texImage,
1683 width, height, depth,
1684 border, internalFormat, texFormat);
1685
1686 /* Give the texture to the driver. <pixels> may be null. */
1687 switch (dims) {
1688 case 1:
1689 ctx->Driver.TexImage1D(ctx, texImage, internalFormat,
1690 width, border, format,
1691 type, pixels, unpack);
1692 break;
1693 case 2:
1694 ctx->Driver.TexImage2D(ctx, texImage, internalFormat,
1695 width, height, border, format,
1696 type, pixels, unpack);
1697 break;
1698 default:
1699 _mesa_problem(ctx, "invalid dims=%u in teximage()", dims);
1700 }
1701
1702 /* state update */
1703 texObj->_Complete = GL_FALSE;
1704 ctx->NewState |= _NEW_TEXTURE;
1705 }
1706 else {
1707 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage%uD", dims);
1708 }
1709 }
1710 }
1711 _mesa_unlock_texture(ctx, texObj);
1712 }
1713 }
1714
1715
1716 /*
1717 * Called from the API. Note that width includes the border.
1718 */
1719 void GLAPIENTRY
1720 _mesa_TexImage1D( GLenum target, GLint level, GLint internalFormat,
1721 GLsizei width, GLint border, GLenum format,
1722 GLenum type, const GLvoid *pixels )
1723 {
1724 GET_CURRENT_CONTEXT(ctx);
1725 teximage(ctx, 1, target, level, internalFormat, width, 1, 1,
1726 border, format, type, pixels);
1727 }
1728
1729
1730 void GLAPIENTRY
1731 _mesa_TexImage2D( GLenum target, GLint level, GLint internalFormat,
1732 GLsizei width, GLsizei height, GLint border,
1733 GLenum format, GLenum type,
1734 const GLvoid *pixels )
1735 {
1736 GET_CURRENT_CONTEXT(ctx);
1737 teximage(ctx, 2, target, level, internalFormat, width, height, 1,
1738 border, format, type, pixels);
1739 }
1740
1741
1742 /**
1743 * Implement all the glTexSubImage1/2/3D() functions.
1744 */
1745 static void
1746 texsubimage(struct gl_context *ctx, GLuint dims, GLenum target, GLint level,
1747 GLint xoffset, GLint yoffset, GLint zoffset,
1748 GLsizei width, GLsizei height, GLsizei depth,
1749 GLenum format, GLenum type, const GLvoid *pixels )
1750 {
1751 struct gl_texture_object *texObj;
1752 struct gl_texture_image *texImage;
1753
1754 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1755
1756 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
1757 _mesa_debug(ctx, "glTexSubImage%uD %s %d %d %d %d %d %d %d %s %s %p\n",
1758 dims,
1759 _mesa_lookup_enum_by_nr(target), level,
1760 xoffset, yoffset, zoffset, width, height, depth,
1761 _mesa_lookup_enum_by_nr(format),
1762 _mesa_lookup_enum_by_nr(type), pixels);
1763
1764 /* check target (proxies not allowed) */
1765 if (!legal_texsubimage_target(ctx, dims, target)) {
1766 _mesa_error(ctx, GL_INVALID_ENUM, "glTexSubImage%uD(target=%s)",
1767 dims, _mesa_lookup_enum_by_nr(target));
1768 return;
1769 }
1770
1771 if (ctx->NewState & _NEW_PIXEL)
1772 _mesa_update_state(ctx);
1773
1774 if (subtexture_error_check(ctx, dims, target, level, xoffset, yoffset, zoffset,
1775 width, height, depth, format, type)) {
1776 return; /* error was detected */
1777 }
1778
1779 texObj = _mesa_select_tex_object(ctx, target);
1780
1781 _mesa_lock_texture(ctx, texObj);
1782 {
1783 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
1784
1785 if (subtexture_error_check2(ctx, dims, target, level,
1786 xoffset, yoffset, zoffset,
1787 width, height, depth,
1788 format, type, texImage)) {
1789 /* error was recorded */
1790 }
1791 else if (width > 0 && height > 0 && depth > 0) {
1792 /* If we have a border, offset=-1 is legal. Bias by border width. */
1793 switch (dims) {
1794 case 3:
1795 zoffset += texImage->Border;
1796 /* fall-through */
1797 case 2:
1798 yoffset += texImage->Border;
1799 /* fall-through */
1800 case 1:
1801 xoffset += texImage->Border;
1802 }
1803
1804 switch (dims) {
1805 case 1:
1806 ctx->Driver.TexSubImage1D(ctx, texImage,
1807 xoffset, width,
1808 format, type, pixels, &ctx->Unpack);
1809 break;
1810 case 2:
1811 ctx->Driver.TexSubImage2D(ctx, texImage,
1812 xoffset, yoffset, width, height,
1813 format, type, pixels, &ctx->Unpack);
1814 break;
1815 default:
1816 _mesa_problem(ctx, "unexpected dims in subteximage()");
1817 }
1818
1819 ctx->NewState |= _NEW_TEXTURE;
1820 }
1821 }
1822 _mesa_unlock_texture(ctx, texObj);
1823 }
1824
1825
1826 void GLAPIENTRY
1827 _mesa_TexSubImage1D( GLenum target, GLint level,
1828 GLint xoffset, GLsizei width,
1829 GLenum format, GLenum type,
1830 const GLvoid *pixels )
1831 {
1832 GET_CURRENT_CONTEXT(ctx);
1833 texsubimage(ctx, 1, target, level,
1834 xoffset, 0, 0,
1835 width, 1, 1,
1836 format, type, pixels);
1837 }
1838
1839
1840 void GLAPIENTRY
1841 _mesa_TexSubImage2D( GLenum target, GLint level,
1842 GLint xoffset, GLint yoffset,
1843 GLsizei width, GLsizei height,
1844 GLenum format, GLenum type,
1845 const GLvoid *pixels )
1846 {
1847 GET_CURRENT_CONTEXT(ctx);
1848 texsubimage(ctx, 2, target, level,
1849 xoffset, yoffset, 0,
1850 width, height, 1,
1851 format, type, pixels);
1852 }
1853
1854
1855
1856 /**
1857 * For glCopyTexSubImage, return the source renderbuffer to copy texel data
1858 * from. This depends on whether the texture contains color or depth values.
1859 */
1860 static struct gl_renderbuffer *
1861 get_copy_tex_image_source(struct gl_context *ctx, gl_format texFormat)
1862 {
1863 if (_mesa_get_format_bits(texFormat, GL_DEPTH_BITS) > 0) {
1864 /* reading from depth/stencil buffer */
1865 return ctx->ReadBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
1866 }
1867 else {
1868 /* copying from color buffer */
1869 return ctx->ReadBuffer->_ColorReadBuffer;
1870 }
1871 }
1872
1873
1874
1875 /**
1876 * Implement the glCopyTexImage1/2D() functions.
1877 */
1878 static void
1879 copyteximage(struct gl_context *ctx, GLuint dims,
1880 GLenum target, GLint level, GLenum internalFormat,
1881 GLint x, GLint y, GLsizei width, GLsizei height, GLint border )
1882 {
1883 struct gl_texture_object *texObj;
1884 struct gl_texture_image *texImage;
1885
1886 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1887
1888 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
1889 _mesa_debug(ctx, "glCopyTexImage%uD %s %d %s %d %d %d %d %d\n",
1890 dims,
1891 _mesa_lookup_enum_by_nr(target), level,
1892 _mesa_lookup_enum_by_nr(internalFormat),
1893 x, y, width, height, border);
1894
1895 if (ctx->NewState & NEW_COPY_TEX_STATE)
1896 _mesa_update_state(ctx);
1897
1898 if (copytexture_error_check(ctx, dims, target, level, internalFormat,
1899 width, height, border))
1900 return;
1901
1902 texObj = _mesa_select_tex_object(ctx, target);
1903
1904 if (border && ctx->Const.StripTextureBorder) {
1905 x += border;
1906 width -= border * 2;
1907 if (dims == 2) {
1908 y += border;
1909 height -= border * 2;
1910 }
1911 border = 0;
1912 }
1913
1914 _mesa_lock_texture(ctx, texObj);
1915 {
1916 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
1917
1918 if (!texImage) {
1919 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage%uD", dims);
1920 }
1921 else {
1922 /* choose actual hw format */
1923 gl_format texFormat = _mesa_choose_texture_format(ctx, texObj,
1924 target, level,
1925 internalFormat,
1926 GL_NONE, GL_NONE);
1927
1928 if (legal_texture_size(ctx, texFormat, width, height, 1)) {
1929 GLint srcX = x, srcY = y, dstX = 0, dstY = 0;
1930
1931 /* Free old texture image */
1932 ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
1933
1934 _mesa_init_teximage_fields(ctx, texImage, width, height, 1,
1935 border, internalFormat, texFormat);
1936
1937 /* Allocate texture memory (no pixel data yet) */
1938 if (dims == 1) {
1939 ctx->Driver.TexImage1D(ctx, texImage, internalFormat,
1940 width, border, GL_NONE, GL_NONE, NULL,
1941 &ctx->Unpack);
1942 }
1943 else {
1944 ctx->Driver.TexImage2D(ctx, texImage, internalFormat,
1945 width, height, border, GL_NONE, GL_NONE,
1946 NULL, &ctx->Unpack);
1947 }
1948
1949 if (_mesa_clip_copytexsubimage(ctx, &dstX, &dstY, &srcX, &srcY,
1950 &width, &height)) {
1951 struct gl_renderbuffer *srcRb =
1952 get_copy_tex_image_source(ctx, texImage->TexFormat);
1953
1954 if (dims == 1)
1955 ctx->Driver.CopyTexSubImage1D(ctx, texImage, dstX,
1956 srcRb, srcX, srcY, width);
1957
1958 else
1959 ctx->Driver.CopyTexSubImage2D(ctx, texImage, dstX, dstY,
1960 srcRb, srcX, srcY, width, height);
1961 }
1962
1963 /* state update */
1964 texObj->_Complete = GL_FALSE;
1965 ctx->NewState |= _NEW_TEXTURE;
1966 }
1967 else {
1968 /* probably too large of image */
1969 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage%uD", dims);
1970 }
1971 }
1972 }
1973 _mesa_unlock_texture(ctx, texObj);
1974 }
1975
1976
1977
1978 void GLAPIENTRY
1979 _mesa_CopyTexImage1D( GLenum target, GLint level,
1980 GLenum internalFormat,
1981 GLint x, GLint y,
1982 GLsizei width, GLint border )
1983 {
1984 GET_CURRENT_CONTEXT(ctx);
1985 copyteximage(ctx, 1, target, level, internalFormat, x, y, width, 1, border);
1986 }
1987
1988
1989
1990 void GLAPIENTRY
1991 _mesa_CopyTexImage2D( GLenum target, GLint level, GLenum internalFormat,
1992 GLint x, GLint y, GLsizei width, GLsizei height,
1993 GLint border )
1994 {
1995 GET_CURRENT_CONTEXT(ctx);
1996 copyteximage(ctx, 2, target, level, internalFormat,
1997 x, y, width, height, border);
1998 }
1999
2000
2001
2002 /**
2003 * Implementation for glCopyTexSubImage1/2/3D() functions.
2004 */
2005 static void
2006 copytexsubimage(struct gl_context *ctx, GLuint dims, GLenum target, GLint level,
2007 GLint xoffset, GLint yoffset, GLint zoffset,
2008 GLint x, GLint y, GLsizei width, GLsizei height)
2009 {
2010 struct gl_texture_object *texObj;
2011 struct gl_texture_image *texImage;
2012
2013 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2014
2015 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2016 _mesa_debug(ctx, "glCopyTexSubImage%uD %s %d %d %d %d %d %d %d %d\n",
2017 dims,
2018 _mesa_lookup_enum_by_nr(target),
2019 level, xoffset, yoffset, zoffset, x, y, width, height);
2020
2021 if (ctx->NewState & NEW_COPY_TEX_STATE)
2022 _mesa_update_state(ctx);
2023
2024 if (copytexsubimage_error_check1(ctx, dims, target, level))
2025 return;
2026
2027 texObj = _mesa_select_tex_object(ctx, target);
2028
2029 _mesa_lock_texture(ctx, texObj);
2030 {
2031 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
2032
2033 if (copytexsubimage_error_check2(ctx, dims, target, level, xoffset, yoffset,
2034 zoffset, width, height, texImage)) {
2035 /* error was recored */
2036 }
2037 else {
2038 /* If we have a border, offset=-1 is legal. Bias by border width. */
2039 switch (dims) {
2040 case 3:
2041 zoffset += texImage->Border;
2042 /* fall-through */
2043 case 2:
2044 yoffset += texImage->Border;
2045 /* fall-through */
2046 case 1:
2047 xoffset += texImage->Border;
2048 }
2049
2050 if (_mesa_clip_copytexsubimage(ctx, &xoffset, &yoffset, &x, &y,
2051 &width, &height)) {
2052 struct gl_renderbuffer *srcRb =
2053 get_copy_tex_image_source(ctx, texImage->TexFormat);
2054
2055 switch (dims) {
2056 case 1:
2057 ctx->Driver.CopyTexSubImage1D(ctx, texImage, xoffset,
2058 srcRb, x, y, width);
2059 break;
2060 case 2:
2061 ctx->Driver.CopyTexSubImage2D(ctx, texImage, xoffset, yoffset,
2062 srcRb, x, y, width, height);
2063 break;
2064 default:
2065 _mesa_problem(ctx, "bad dims in copytexsubimage()");
2066 }
2067
2068 ctx->NewState |= _NEW_TEXTURE;
2069 }
2070 }
2071 }
2072 _mesa_unlock_texture(ctx, texObj);
2073 }
2074
2075
2076 void GLAPIENTRY
2077 _mesa_CopyTexSubImage1D( GLenum target, GLint level,
2078 GLint xoffset, GLint x, GLint y, GLsizei width )
2079 {
2080 GET_CURRENT_CONTEXT(ctx);
2081 copytexsubimage(ctx, 1, target, level, xoffset, 0, 0, x, y, width, 1);
2082 }
2083
2084
2085
2086 void GLAPIENTRY
2087 _mesa_CopyTexSubImage2D( GLenum target, GLint level,
2088 GLint xoffset, GLint yoffset,
2089 GLint x, GLint y, GLsizei width, GLsizei height )
2090 {
2091 GET_CURRENT_CONTEXT(ctx);
2092 copytexsubimage(ctx, 2, target, level, xoffset, yoffset, 0, x, y,
2093 width, height);
2094 }