blob: 81f6852baf00ba5dc3885d06fe7af5b7f5c75c57 [file] [log] [blame]
jtgafb833d1999-08-19 00:55:39 +00001
2/*
3 * Mesa 3-D graphics library
Brian Paulfbd8f211999-11-11 01:22:25 +00004 * Version: 3.3
jtgafb833d1999-08-19 00:55:39 +00005 *
Brian Paul663049a2000-01-31 23:10:16 +00006 * Copyright (C) 1999-2000 Brian Paul All Rights Reserved.
jtgafb833d1999-08-19 00:55:39 +00007 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27#ifdef PC_HEADER
28#include "all.h"
29#else
Brian Paulfbd8f211999-11-11 01:22:25 +000030#include "glheader.h"
jtgafb833d1999-08-19 00:55:39 +000031#include "context.h"
32#include "image.h"
Brian Paulfbd8f211999-11-11 01:22:25 +000033#include "mem.h"
jtgafb833d1999-08-19 00:55:39 +000034#include "mmath.h"
35#include "span.h"
36#include "teximage.h"
37#include "texstate.h"
38#include "types.h"
jtgafb833d1999-08-19 00:55:39 +000039#endif
40
41
42/*
43 * NOTES:
44 *
Brian Paulc3f0a511999-11-03 17:27:05 +000045 * Mesa's native texture datatype is GLubyte. Native formats are
46 * GL_ALPHA, GL_LUMINANCE, GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, GL_RGBA,
47 * and GL_COLOR_INDEX.
48 * Device drivers are free to implement any internal format they want.
jtgafb833d1999-08-19 00:55:39 +000049 */
50
51
Brian Paul48271792000-03-29 18:13:59 +000052#ifdef DEBUG
53static void PrintTexture(int w, int h, int c, const GLubyte *data)
54{
55 int i, j;
56 for (i = 0; i < h; i++) {
57 for (j = 0; j < w; j++) {
58 if (c==1)
59 printf("%02x ", data[0]);
60 else if (c==2)
61 printf("%02x %02x ", data[0], data[1]);
62 else if (c==3)
63 printf("%02x %02x %02x ", data[0], data[1], data[2]);
64 else if (c==4)
65 printf("%02x %02x %02x %02x ", data[0], data[1], data[2], data[3]);
66 data += c;
67 }
68 printf("\n");
69 }
70}
71#endif
72
73
74
Brian Paulf7b57072000-03-20 14:37:52 +000075/*
jtgafb833d1999-08-19 00:55:39 +000076 * Compute log base 2 of n.
77 * If n isn't an exact power of two return -1.
78 * If n<0 return -1.
79 */
Brian Paulfbd8f211999-11-11 01:22:25 +000080static int
81logbase2( int n )
jtgafb833d1999-08-19 00:55:39 +000082{
83 GLint i = 1;
84 GLint log2 = 0;
85
86 if (n<0) {
87 return -1;
88 }
89
90 while ( n > i ) {
91 i *= 2;
92 log2++;
93 }
94 if (i != n) {
95 return -1;
96 }
97 else {
98 return log2;
99 }
100}
101
102
103
104/*
105 * Given an internal texture format enum or 1, 2, 3, 4 return the
106 * corresponding _base_ internal format: GL_ALPHA, GL_LUMINANCE,
Brian Paulc3f0a511999-11-03 17:27:05 +0000107 * GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, or GL_RGBA.
108 * Return -1 if invalid enum.
jtgafb833d1999-08-19 00:55:39 +0000109 */
Brian Paulb132e8d2000-03-23 16:23:14 +0000110GLint
111_mesa_base_tex_format( GLint format )
jtgafb833d1999-08-19 00:55:39 +0000112{
113 switch (format) {
114 case GL_ALPHA:
115 case GL_ALPHA4:
116 case GL_ALPHA8:
117 case GL_ALPHA12:
118 case GL_ALPHA16:
119 return GL_ALPHA;
120 case 1:
121 case GL_LUMINANCE:
122 case GL_LUMINANCE4:
123 case GL_LUMINANCE8:
124 case GL_LUMINANCE12:
125 case GL_LUMINANCE16:
126 return GL_LUMINANCE;
127 case 2:
128 case GL_LUMINANCE_ALPHA:
129 case GL_LUMINANCE4_ALPHA4:
130 case GL_LUMINANCE6_ALPHA2:
131 case GL_LUMINANCE8_ALPHA8:
132 case GL_LUMINANCE12_ALPHA4:
133 case GL_LUMINANCE12_ALPHA12:
134 case GL_LUMINANCE16_ALPHA16:
135 return GL_LUMINANCE_ALPHA;
136 case GL_INTENSITY:
137 case GL_INTENSITY4:
138 case GL_INTENSITY8:
139 case GL_INTENSITY12:
140 case GL_INTENSITY16:
141 return GL_INTENSITY;
142 case 3:
143 case GL_RGB:
144 case GL_R3_G3_B2:
145 case GL_RGB4:
146 case GL_RGB5:
147 case GL_RGB8:
148 case GL_RGB10:
149 case GL_RGB12:
150 case GL_RGB16:
151 return GL_RGB;
152 case 4:
153 case GL_RGBA:
154 case GL_RGBA2:
155 case GL_RGBA4:
156 case GL_RGB5_A1:
157 case GL_RGBA8:
158 case GL_RGB10_A2:
159 case GL_RGBA12:
160 case GL_RGBA16:
161 return GL_RGBA;
162 case GL_COLOR_INDEX:
163 case GL_COLOR_INDEX1_EXT:
164 case GL_COLOR_INDEX2_EXT:
165 case GL_COLOR_INDEX4_EXT:
166 case GL_COLOR_INDEX8_EXT:
167 case GL_COLOR_INDEX12_EXT:
168 case GL_COLOR_INDEX16_EXT:
169 return GL_COLOR_INDEX;
170 default:
171 return -1; /* error */
172 }
173}
174
175
176
177/*
178 * Given an internal texture format enum or 1, 2, 3, 4 return the
179 * corresponding _base_ internal format: GL_ALPHA, GL_LUMINANCE,
180 * GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, or GL_RGBA. Return the
181 * number of components for the format. Return -1 if invalid enum.
182 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000183static GLint
184components_in_intformat( GLint format )
jtgafb833d1999-08-19 00:55:39 +0000185{
186 switch (format) {
187 case GL_ALPHA:
188 case GL_ALPHA4:
189 case GL_ALPHA8:
190 case GL_ALPHA12:
191 case GL_ALPHA16:
192 return 1;
193 case 1:
194 case GL_LUMINANCE:
195 case GL_LUMINANCE4:
196 case GL_LUMINANCE8:
197 case GL_LUMINANCE12:
198 case GL_LUMINANCE16:
199 return 1;
200 case 2:
201 case GL_LUMINANCE_ALPHA:
202 case GL_LUMINANCE4_ALPHA4:
203 case GL_LUMINANCE6_ALPHA2:
204 case GL_LUMINANCE8_ALPHA8:
205 case GL_LUMINANCE12_ALPHA4:
206 case GL_LUMINANCE12_ALPHA12:
207 case GL_LUMINANCE16_ALPHA16:
208 return 2;
209 case GL_INTENSITY:
210 case GL_INTENSITY4:
211 case GL_INTENSITY8:
212 case GL_INTENSITY12:
213 case GL_INTENSITY16:
214 return 1;
215 case 3:
216 case GL_RGB:
217 case GL_R3_G3_B2:
218 case GL_RGB4:
219 case GL_RGB5:
220 case GL_RGB8:
221 case GL_RGB10:
222 case GL_RGB12:
223 case GL_RGB16:
224 return 3;
225 case 4:
226 case GL_RGBA:
227 case GL_RGBA2:
228 case GL_RGBA4:
229 case GL_RGB5_A1:
230 case GL_RGBA8:
231 case GL_RGB10_A2:
232 case GL_RGBA12:
233 case GL_RGBA16:
234 return 4;
235 case GL_COLOR_INDEX:
236 case GL_COLOR_INDEX1_EXT:
237 case GL_COLOR_INDEX2_EXT:
238 case GL_COLOR_INDEX4_EXT:
239 case GL_COLOR_INDEX8_EXT:
240 case GL_COLOR_INDEX12_EXT:
241 case GL_COLOR_INDEX16_EXT:
242 return 1;
243 default:
244 return -1; /* error */
245 }
246}
247
248
249
jtgafb833d1999-08-19 00:55:39 +0000250/*
251 * Examine the texImage->Format field and set the Red, Green, Blue, etc
252 * texel component sizes to default values.
253 * These fields are set only here by core Mesa but device drivers may
254 * overwritting these fields to indicate true texel resolution.
255 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000256static void
257set_teximage_component_sizes( struct gl_texture_image *texImage )
jtgafb833d1999-08-19 00:55:39 +0000258{
259 switch (texImage->Format) {
260 case GL_ALPHA:
261 texImage->RedBits = 0;
262 texImage->GreenBits = 0;
263 texImage->BlueBits = 0;
264 texImage->AlphaBits = 8;
265 texImage->IntensityBits = 0;
266 texImage->LuminanceBits = 0;
267 texImage->IndexBits = 0;
268 break;
269 case GL_LUMINANCE:
270 texImage->RedBits = 0;
271 texImage->GreenBits = 0;
272 texImage->BlueBits = 0;
273 texImage->AlphaBits = 0;
274 texImage->IntensityBits = 0;
275 texImage->LuminanceBits = 8;
276 texImage->IndexBits = 0;
277 break;
278 case GL_LUMINANCE_ALPHA:
279 texImage->RedBits = 0;
280 texImage->GreenBits = 0;
281 texImage->BlueBits = 0;
282 texImage->AlphaBits = 8;
283 texImage->IntensityBits = 0;
284 texImage->LuminanceBits = 8;
285 texImage->IndexBits = 0;
286 break;
287 case GL_INTENSITY:
288 texImage->RedBits = 0;
289 texImage->GreenBits = 0;
290 texImage->BlueBits = 0;
291 texImage->AlphaBits = 0;
292 texImage->IntensityBits = 8;
293 texImage->LuminanceBits = 0;
294 texImage->IndexBits = 0;
295 break;
Brian Paul91baaa31999-10-17 23:24:16 +0000296 case GL_RED:
297 texImage->RedBits = 8;
298 texImage->GreenBits = 0;
299 texImage->BlueBits = 0;
300 texImage->AlphaBits = 0;
301 texImage->IntensityBits = 0;
302 texImage->LuminanceBits = 0;
303 texImage->IndexBits = 0;
304 break;
305 case GL_GREEN:
306 texImage->RedBits = 0;
307 texImage->GreenBits = 8;
308 texImage->BlueBits = 0;
309 texImage->AlphaBits = 0;
310 texImage->IntensityBits = 0;
311 texImage->LuminanceBits = 0;
312 texImage->IndexBits = 0;
313 break;
314 case GL_BLUE:
315 texImage->RedBits = 0;
316 texImage->GreenBits = 0;
317 texImage->BlueBits = 8;
318 texImage->AlphaBits = 0;
319 texImage->IntensityBits = 0;
320 texImage->LuminanceBits = 0;
321 texImage->IndexBits = 0;
322 break;
jtgafb833d1999-08-19 00:55:39 +0000323 case GL_RGB:
Brian Pauld53573d1999-10-19 20:36:20 +0000324 case GL_BGR:
jtgafb833d1999-08-19 00:55:39 +0000325 texImage->RedBits = 8;
326 texImage->GreenBits = 8;
327 texImage->BlueBits = 8;
328 texImage->AlphaBits = 0;
329 texImage->IntensityBits = 0;
330 texImage->LuminanceBits = 0;
331 texImage->IndexBits = 0;
332 break;
333 case GL_RGBA:
Brian Pauld53573d1999-10-19 20:36:20 +0000334 case GL_BGRA:
335 case GL_ABGR_EXT:
jtgafb833d1999-08-19 00:55:39 +0000336 texImage->RedBits = 8;
337 texImage->GreenBits = 8;
338 texImage->BlueBits = 8;
339 texImage->AlphaBits = 8;
340 texImage->IntensityBits = 0;
341 texImage->LuminanceBits = 0;
342 texImage->IndexBits = 0;
343 break;
344 case GL_COLOR_INDEX:
345 texImage->RedBits = 0;
346 texImage->GreenBits = 0;
347 texImage->BlueBits = 0;
348 texImage->AlphaBits = 0;
349 texImage->IntensityBits = 0;
350 texImage->LuminanceBits = 0;
351 texImage->IndexBits = 8;
352 break;
353 default:
354 gl_problem(NULL, "unexpected format in set_teximage_component_sizes");
355 }
356}
357
358
Brian Paul77ce6da2000-03-20 23:40:12 +0000359
360/*
361 * Return new gl_texture_image struct with all fields initialized to zero.
362 */
363struct gl_texture_image *
Brian Paul021a5252000-03-27 17:54:17 +0000364_mesa_alloc_texture_image( void )
Brian Paul77ce6da2000-03-20 23:40:12 +0000365{
366 return CALLOC_STRUCT(gl_texture_image);
367}
368
369
370
371/*
Brian Paul02938782000-03-22 17:38:11 +0000372 * Initialize most fields of a gl_texture_image struct.
Brian Paul77ce6da2000-03-20 23:40:12 +0000373 */
Brian Paul02938782000-03-22 17:38:11 +0000374static void
375init_texture_image( struct gl_texture_image *img,
376 GLsizei width, GLsizei height, GLsizei depth,
377 GLint border, GLenum internalFormat )
Brian Paul77ce6da2000-03-20 23:40:12 +0000378{
Brian Paul02938782000-03-22 17:38:11 +0000379 ASSERT(img);
380 ASSERT(!img->Data);
Brian Paulb132e8d2000-03-23 16:23:14 +0000381 img->Format = (GLenum) _mesa_base_tex_format(internalFormat);
Brian Paul77ce6da2000-03-20 23:40:12 +0000382 set_teximage_component_sizes( img );
383 img->IntFormat = (GLenum) internalFormat;
384 img->Border = border;
385 img->Width = width;
386 img->Height = height;
387 img->Depth = depth;
388 img->WidthLog2 = logbase2(width - 2 * border);
389 if (height == 1) /* 1-D texture */
390 img->HeightLog2 = 0;
391 else
392 img->HeightLog2 = logbase2(height - 2 * border);
393 if (depth == 1) /* 2-D texture */
394 img->DepthLog2 = 0;
395 else
396 img->DepthLog2 = logbase2(depth - 2 * border);
397 img->Width2 = 1 << img->WidthLog2;
398 img->Height2 = 1 << img->HeightLog2;
399 img->Depth2 = 1 << img->DepthLog2;
400 img->MaxLog2 = MAX2(img->WidthLog2, img->HeightLog2);
Brian Paul77ce6da2000-03-20 23:40:12 +0000401}
402
403
404
405void
Brian Paul021a5252000-03-27 17:54:17 +0000406_mesa_free_texture_image( struct gl_texture_image *teximage )
Brian Paul77ce6da2000-03-20 23:40:12 +0000407{
408 if (teximage->Data) {
409 FREE( teximage->Data );
410 teximage->Data = NULL;
411 }
412 FREE( teximage );
413}
414
415
416
jtgafb833d1999-08-19 00:55:39 +0000417/* Need this to prevent an out-of-bounds memory access when using
418 * X86 optimized code.
419 */
420#ifdef USE_X86_ASM
421# define EXTRA_BYTE 1
422#else
423# define EXTRA_BYTE 0
424#endif
425
426
Brian Paulc3f0a511999-11-03 17:27:05 +0000427
jtgafb833d1999-08-19 00:55:39 +0000428/*
Brian Paul43911c82000-03-21 00:49:33 +0000429 * Called by glTexImage[123]D. Fill in a texture image with data given
430 * by the client. All pixel transfer and unpack modes are handled here.
431 * NOTE: All texture image parameters should have already been error checked.
jtgafb833d1999-08-19 00:55:39 +0000432 */
Brian Paul43911c82000-03-21 00:49:33 +0000433static void
434make_texture_image( GLcontext *ctx,
435 struct gl_texture_image *texImage,
Brian Paulc3f0a511999-11-03 17:27:05 +0000436 GLenum srcFormat, GLenum srcType, const GLvoid *pixels,
437 const struct gl_pixelstore_attrib *unpacking)
jtgafb833d1999-08-19 00:55:39 +0000438{
Brian Paulc3f0a511999-11-03 17:27:05 +0000439 GLint components, numPixels;
Brian Paul43911c82000-03-21 00:49:33 +0000440 GLint internalFormat, width, height, depth, border;
jtgafb833d1999-08-19 00:55:39 +0000441
Brian Paul43911c82000-03-21 00:49:33 +0000442 ASSERT(ctx);
443 ASSERT(texImage);
444 ASSERT(!texImage->Data);
445 ASSERT(pixels);
446 ASSERT(unpacking);
jtgafb833d1999-08-19 00:55:39 +0000447
Brian Paul43911c82000-03-21 00:49:33 +0000448 internalFormat = texImage->IntFormat;
449 width = texImage->Width;
450 height = texImage->Height;
451 depth = texImage->Depth;
452 border = texImage->Border;
Brian Paulc3f0a511999-11-03 17:27:05 +0000453 components = components_in_intformat(internalFormat);
Brian Paul43911c82000-03-21 00:49:33 +0000454
455 ASSERT(width > 0);
456 ASSERT(height > 0);
457 ASSERT(depth > 0);
458 ASSERT(border == 0 || border == 1);
459 ASSERT(pixels);
460 ASSERT(unpacking);
461 ASSERT(components);
462
463 numPixels = width * height * depth;
Brian Paulc3f0a511999-11-03 17:27:05 +0000464
465 texImage->Data = (GLubyte *) MALLOC(numPixels * components + EXTRA_BYTE);
Brian Paul43911c82000-03-21 00:49:33 +0000466 if (!texImage->Data)
467 return; /* out of memory */
Brian Paulc3f0a511999-11-03 17:27:05 +0000468
469 /*
470 * OK, the texture image struct has been initialized and the texture
471 * image memory has been allocated.
472 * Now fill in the texture image from the source data.
473 * This includes applying the pixel transfer operations.
474 */
475
476 /* try common 2D texture cases first */
477 if (!ctx->Pixel.ScaleOrBiasRGBA && !ctx->Pixel.MapColorFlag
478 && !ctx->Pixel.IndexOffset && !ctx->Pixel.IndexShift
479 && srcType == GL_UNSIGNED_BYTE && depth == 1) {
480
Brian Paul43911c82000-03-21 00:49:33 +0000481 if (srcFormat == internalFormat ||
482 (srcFormat == GL_LUMINANCE && internalFormat == 1) ||
483 (srcFormat == GL_LUMINANCE_ALPHA && internalFormat == 2) ||
484 (srcFormat == GL_RGB && internalFormat == 3) ||
485 (srcFormat == GL_RGBA && internalFormat == 4)) {
Brian Paulc3f0a511999-11-03 17:27:05 +0000486 /* This will cover the common GL_RGB, GL_RGBA, GL_ALPHA,
487 * GL_LUMINANCE_ALPHA, etc. texture formats.
488 */
Brian Paulb7d076f2000-03-21 01:03:40 +0000489 const GLubyte *src = (const GLubyte *) _mesa_image_address(
Brian Paul43911c82000-03-21 00:49:33 +0000490 unpacking, pixels, width, height, srcFormat, srcType, 0, 0, 0);
491 const GLint srcStride = _mesa_image_row_stride(unpacking, width,
492 srcFormat, srcType);
Brian Paulc3f0a511999-11-03 17:27:05 +0000493 GLubyte *dst = texImage->Data;
494 GLint dstBytesPerRow = width * components * sizeof(GLubyte);
495 if (srcStride == dstBytesPerRow) {
496 MEMCPY(dst, src, height * dstBytesPerRow);
497 }
498 else {
499 GLint i;
500 for (i = 0; i < height; i++) {
501 MEMCPY(dst, src, dstBytesPerRow);
502 src += srcStride;
503 dst += dstBytesPerRow;
504 }
505 }
Brian Paul43911c82000-03-21 00:49:33 +0000506 return; /* all done */
Brian Paulc3f0a511999-11-03 17:27:05 +0000507 }
508 else if (srcFormat == GL_RGBA && internalFormat == GL_RGB) {
509 /* commonly used by Quake */
Brian Paulb7d076f2000-03-21 01:03:40 +0000510 const GLubyte *src = (const GLubyte *) _mesa_image_address(
Brian Paul43911c82000-03-21 00:49:33 +0000511 unpacking, pixels, width, height, srcFormat, srcType, 0, 0, 0);
512 const GLint srcStride = _mesa_image_row_stride(unpacking, width,
513 srcFormat, srcType);
Brian Paulc3f0a511999-11-03 17:27:05 +0000514 GLubyte *dst = texImage->Data;
515 GLint i, j;
516 for (i = 0; i < height; i++) {
517 const GLubyte *s = src;
518 for (j = 0; j < width; j++) {
519 *dst++ = *s++; /*red*/
520 *dst++ = *s++; /*green*/
521 *dst++ = *s++; /*blue*/
522 s++; /*alpha*/
523 }
524 src += srcStride;
525 }
Brian Paul43911c82000-03-21 00:49:33 +0000526 return; /* all done */
Brian Paulc3f0a511999-11-03 17:27:05 +0000527 }
528 }
529
530
531 /*
532 * General case solutions
533 */
534 if (texImage->Format == GL_COLOR_INDEX) {
535 /* color index texture */
536 const GLint destBytesPerRow = width * components * sizeof(GLubyte);
537 const GLenum dstType = GL_UNSIGNED_BYTE;
538 GLubyte *dest = texImage->Data;
539 GLint img, row;
540 for (img = 0; img < depth; img++) {
541 for (row = 0; row < height; row++) {
Brian Paulb7d076f2000-03-21 01:03:40 +0000542 const GLvoid *source = _mesa_image_address(unpacking,
Brian Paulc3f0a511999-11-03 17:27:05 +0000543 pixels, width, height, srcFormat, srcType, img, row, 0);
544 _mesa_unpack_index_span(ctx, width, dstType, dest,
545 srcType, source, unpacking, GL_TRUE);
546 dest += destBytesPerRow;
547 }
548 }
jtgafb833d1999-08-19 00:55:39 +0000549 }
550 else {
Brian Paulc3f0a511999-11-03 17:27:05 +0000551 /* regular, color texture */
552 const GLint destBytesPerRow = width * components * sizeof(GLubyte);
553 const GLenum dstFormat = texImage->Format;
554 GLubyte *dest = texImage->Data;
555 GLint img, row;
556 for (img = 0; img < depth; img++) {
557 for (row = 0; row < height; row++) {
Brian Paulb7d076f2000-03-21 01:03:40 +0000558 const GLvoid *source = _mesa_image_address(unpacking,
Brian Paulc3f0a511999-11-03 17:27:05 +0000559 pixels, width, height, srcFormat, srcType, img, row, 0);
560 _mesa_unpack_ubyte_color_span(ctx, width, dstFormat, dest,
561 srcFormat, srcType, source, unpacking, GL_TRUE);
562 dest += destBytesPerRow;
563 }
564 }
jtgafb833d1999-08-19 00:55:39 +0000565 }
jtgafb833d1999-08-19 00:55:39 +0000566}
567
568
569
570/*
571 * glTexImage[123]D can accept a NULL image pointer. In this case we
572 * create a texture image with unspecified image contents per the OpenGL
Brian Paul43911c82000-03-21 00:49:33 +0000573 * spec. This function creates an empty image for the given texture image.
jtgafb833d1999-08-19 00:55:39 +0000574 */
Brian Paul43911c82000-03-21 00:49:33 +0000575static void
576make_null_texture( struct gl_texture_image *texImage )
jtgafb833d1999-08-19 00:55:39 +0000577{
578 GLint components;
jtgafb833d1999-08-19 00:55:39 +0000579 GLint numPixels;
jtgafb833d1999-08-19 00:55:39 +0000580
Brian Paul43911c82000-03-21 00:49:33 +0000581 ASSERT(texImage);
582 ASSERT(!texImage->Data);
jtgafb833d1999-08-19 00:55:39 +0000583
Brian Paul43911c82000-03-21 00:49:33 +0000584 components = components_in_intformat(texImage->IntFormat);
585 numPixels = texImage->Width * texImage->Height * texImage->Depth;
jtgafb833d1999-08-19 00:55:39 +0000586
Brian Paulbd5cdaf1999-10-13 18:42:49 +0000587 texImage->Data = (GLubyte *) MALLOC( numPixels * components + EXTRA_BYTE );
jtgafb833d1999-08-19 00:55:39 +0000588
589 /*
590 * Let's see if anyone finds this. If glTexImage2D() is called with
591 * a NULL image pointer then load the texture image with something
592 * interesting instead of leaving it indeterminate.
593 */
594 if (texImage->Data) {
Brian Paul65d54602000-03-01 23:28:20 +0000595 static const char message[8][32] = {
jtgafb833d1999-08-19 00:55:39 +0000596 " X X XXXXX XXX X ",
597 " XX XX X X X X X ",
598 " X X X X X X X ",
599 " X X XXXX XXX XXXXX ",
600 " X X X X X X ",
601 " X X X X X X X ",
602 " X X XXXXX XXX X X ",
603 " "
604 };
605
606 GLubyte *imgPtr = texImage->Data;
607 GLint i, j, k;
Brian Paul43911c82000-03-21 00:49:33 +0000608 for (i = 0; i < texImage->Height; i++) {
jtgafb833d1999-08-19 00:55:39 +0000609 GLint srcRow = 7 - i % 8;
Brian Paul43911c82000-03-21 00:49:33 +0000610 for (j = 0; j < texImage->Width; j++) {
jtgafb833d1999-08-19 00:55:39 +0000611 GLint srcCol = j % 32;
Brian Paul5b37c321999-11-05 06:43:10 +0000612 GLint texel = (message[srcRow][srcCol]=='X') ? 255 : 70;
jtgafb833d1999-08-19 00:55:39 +0000613 for (k=0;k<components;k++) {
Brian Paul5b37c321999-11-05 06:43:10 +0000614 *imgPtr++ = (GLubyte) texel;
jtgafb833d1999-08-19 00:55:39 +0000615 }
616 }
617 }
618 }
jtgafb833d1999-08-19 00:55:39 +0000619}
620
621
622
623/*
Brian Paulc3f0a511999-11-03 17:27:05 +0000624 * Test glTexImage[123]D() parameters for errors.
jtgafb833d1999-08-19 00:55:39 +0000625 * Input:
626 * dimensions - must be 1 or 2 or 3
627 * Return: GL_TRUE = an error was detected, GL_FALSE = no errors
628 */
Brian Paulc3f0a511999-11-03 17:27:05 +0000629static GLboolean
630texture_error_check( GLcontext *ctx, GLenum target,
631 GLint level, GLint internalFormat,
632 GLenum format, GLenum type,
Brian Paul5b37c321999-11-05 06:43:10 +0000633 GLuint dimensions,
Brian Paulc3f0a511999-11-03 17:27:05 +0000634 GLint width, GLint height,
635 GLint depth, GLint border )
jtgafb833d1999-08-19 00:55:39 +0000636{
637 GLboolean isProxy;
638 GLint iformat;
639
640 if (dimensions == 1) {
Brian Paul5b37c321999-11-05 06:43:10 +0000641 isProxy = (GLboolean) (target == GL_PROXY_TEXTURE_1D);
jtgafb833d1999-08-19 00:55:39 +0000642 if (target != GL_TEXTURE_1D && !isProxy) {
643 gl_error( ctx, GL_INVALID_ENUM, "glTexImage1D(target)" );
644 return GL_TRUE;
645 }
646 }
647 else if (dimensions == 2) {
Brian Paul5b37c321999-11-05 06:43:10 +0000648 isProxy = (GLboolean) (target == GL_PROXY_TEXTURE_2D);
jtgafb833d1999-08-19 00:55:39 +0000649 if (target != GL_TEXTURE_2D && !isProxy) {
650 gl_error( ctx, GL_INVALID_ENUM, "glTexImage2D(target)" );
651 return GL_TRUE;
652 }
653 }
654 else if (dimensions == 3) {
Brian Paul5b37c321999-11-05 06:43:10 +0000655 isProxy = (GLboolean) (target == GL_PROXY_TEXTURE_3D);
jtgafb833d1999-08-19 00:55:39 +0000656 if (target != GL_TEXTURE_3D && !isProxy) {
657 gl_error( ctx, GL_INVALID_ENUM, "glTexImage3D(target)" );
658 return GL_TRUE;
659 }
660 }
661 else {
662 gl_problem( ctx, "bad dims in texture_error_check" );
663 return GL_TRUE;
664 }
665
666 /* Border */
Brian Paul9fd2b0a2000-03-24 23:59:06 +0000667 if (border != 0 && border != 1) {
jtgafb833d1999-08-19 00:55:39 +0000668 if (!isProxy) {
Brian Paulc3f0a511999-11-03 17:27:05 +0000669 char message[100];
670 sprintf(message, "glTexImage%dD(border)", dimensions);
671 gl_error(ctx, GL_INVALID_VALUE, message);
jtgafb833d1999-08-19 00:55:39 +0000672 }
673 return GL_TRUE;
674 }
675
676 /* Width */
677 if (width < 2 * border || width > 2 + ctx->Const.MaxTextureSize
678 || logbase2( width - 2 * border ) < 0) {
679 if (!isProxy) {
Brian Paulc3f0a511999-11-03 17:27:05 +0000680 char message[100];
681 sprintf(message, "glTexImage%dD(width)", dimensions);
682 gl_error(ctx, GL_INVALID_VALUE, message);
jtgafb833d1999-08-19 00:55:39 +0000683 }
684 return GL_TRUE;
685 }
686
687 /* Height */
688 if (dimensions >= 2) {
689 if (height < 2 * border || height > 2 + ctx->Const.MaxTextureSize
690 || logbase2( height - 2 * border ) < 0) {
691 if (!isProxy) {
Brian Paulc3f0a511999-11-03 17:27:05 +0000692 char message[100];
693 sprintf(message, "glTexImage%dD(height)", dimensions);
694 gl_error(ctx, GL_INVALID_VALUE, message);
jtgafb833d1999-08-19 00:55:39 +0000695 }
Brian Paulc3f0a511999-11-03 17:27:05 +0000696 return GL_TRUE;
jtgafb833d1999-08-19 00:55:39 +0000697 }
698 }
699
700 /* Depth */
701 if (dimensions >= 3) {
702 if (depth < 2 * border || depth > 2 + ctx->Const.MaxTextureSize
703 || logbase2( depth - 2 * border ) < 0) {
704 if (!isProxy) {
705 gl_error( ctx, GL_INVALID_VALUE, "glTexImage3D(depth)" );
706 }
707 return GL_TRUE;
708 }
709 }
710
711 /* Level */
Brian Paul9fd2b0a2000-03-24 23:59:06 +0000712 if (level < 0 || level >= ctx->Const.MaxTextureLevels) {
Brian Paulc3f0a511999-11-03 17:27:05 +0000713 if (!isProxy) {
714 char message[100];
715 sprintf(message, "glTexImage%dD(level)", dimensions);
716 gl_error(ctx, GL_INVALID_VALUE, message);
717 }
jtgafb833d1999-08-19 00:55:39 +0000718 return GL_TRUE;
719 }
720
Brian Paulb132e8d2000-03-23 16:23:14 +0000721 iformat = _mesa_base_tex_format( internalFormat );
jtgafb833d1999-08-19 00:55:39 +0000722 if (iformat < 0) {
Brian Paulc3f0a511999-11-03 17:27:05 +0000723 if (!isProxy) {
724 char message[100];
725 sprintf(message, "glTexImage%dD(internalFormat)", dimensions);
726 gl_error(ctx, GL_INVALID_VALUE, message);
727 }
jtgafb833d1999-08-19 00:55:39 +0000728 return GL_TRUE;
729 }
730
Brian Paulb7d076f2000-03-21 01:03:40 +0000731 if (!_mesa_is_legal_format_and_type( format, type )) {
Brian Pauld53573d1999-10-19 20:36:20 +0000732 /* Yes, generate GL_INVALID_OPERATION, not GL_INVALID_ENUM, if there
733 * is a type/format mismatch. See 1.2 spec page 94, sec 3.6.4.
734 */
Brian Paulc3f0a511999-11-03 17:27:05 +0000735 if (!isProxy) {
736 char message[100];
737 sprintf(message, "glTexImage%dD(format or type)", dimensions);
738 gl_error(ctx, GL_INVALID_OPERATION, message);
739 }
jtgafb833d1999-08-19 00:55:39 +0000740 return GL_TRUE;
741 }
742
743 /* if we get here, the parameters are OK */
744 return GL_FALSE;
745}
746
747
748
749/*
Brian Paulc3f0a511999-11-03 17:27:05 +0000750 * Test glTexSubImage[123]D() parameters for errors.
751 * Input:
752 * dimensions - must be 1 or 2 or 3
753 * Return: GL_TRUE = an error was detected, GL_FALSE = no errors
754 */
755static GLboolean
Brian Paulfbd8f211999-11-11 01:22:25 +0000756subtexture_error_check( GLcontext *ctx, GLuint dimensions,
Brian Paulc3f0a511999-11-03 17:27:05 +0000757 GLenum target, GLint level,
758 GLint xoffset, GLint yoffset, GLint zoffset,
759 GLint width, GLint height, GLint depth,
760 GLenum format, GLenum type )
761{
762 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
763 struct gl_texture_image *destTex;
764
765 if (dimensions == 1) {
766 if (target != GL_TEXTURE_1D) {
767 gl_error( ctx, GL_INVALID_ENUM, "glTexSubImage1D(target)" );
768 return GL_TRUE;
769 }
770 }
771 else if (dimensions == 2) {
772 if (target != GL_TEXTURE_2D) {
773 gl_error( ctx, GL_INVALID_ENUM, "glTexSubImage2D(target)" );
774 return GL_TRUE;
775 }
776 }
777 else if (dimensions == 3) {
778 if (target != GL_TEXTURE_3D) {
779 gl_error( ctx, GL_INVALID_ENUM, "glTexSubImage3D(target)" );
780 return GL_TRUE;
781 }
782 }
783 else {
784 gl_problem( ctx, "bad dims in texture_error_check" );
785 return GL_TRUE;
786 }
787
788 if (level < 0 || level >= ctx->Const.MaxTextureLevels) {
789 gl_error(ctx, GL_INVALID_ENUM, "glTexSubImage2D(level)");
790 return GL_TRUE;
791 }
792
793 if (width < 0) {
794 char message[100];
795 sprintf(message, "glTexSubImage%dD(width)", dimensions);
796 gl_error(ctx, GL_INVALID_VALUE, message);
797 return GL_TRUE;
798 }
799 if (height < 0 && dimensions > 1) {
800 char message[100];
801 sprintf(message, "glTexSubImage%dD(height)", dimensions);
802 gl_error(ctx, GL_INVALID_VALUE, message);
803 return GL_TRUE;
804 }
805 if (depth < 0 && dimensions > 2) {
806 char message[100];
807 sprintf(message, "glTexSubImage%dD(depth)", dimensions);
808 gl_error(ctx, GL_INVALID_VALUE, message);
809 return GL_TRUE;
810 }
811
812 destTex = texUnit->CurrentD[2]->Image[level];
813 if (!destTex) {
814 gl_error(ctx, GL_INVALID_OPERATION, "glTexSubImage2D");
815 return GL_TRUE;
816 }
817
818 if (xoffset < -((GLint)destTex->Border)) {
819 gl_error(ctx, GL_INVALID_VALUE, "glTexSubImage1/2/3D(xoffset)");
820 return GL_TRUE;
821 }
822 if (xoffset + width > (GLint) (destTex->Width + destTex->Border)) {
823 gl_error(ctx, GL_INVALID_VALUE, "glTexSubImage1/2/3D(xoffset+width)");
824 return GL_TRUE;
825 }
826 if (dimensions > 1) {
827 if (yoffset < -((GLint)destTex->Border)) {
828 gl_error(ctx, GL_INVALID_VALUE, "glTexSubImage2/3D(yoffset)");
829 return GL_TRUE;
830 }
831 if (yoffset + height > (GLint) (destTex->Height + destTex->Border)) {
832 gl_error(ctx, GL_INVALID_VALUE, "glTexSubImage2/3D(yoffset+height)");
833 return GL_TRUE;
834 }
835 }
836 if (dimensions > 2) {
837 if (zoffset < -((GLint)destTex->Border)) {
838 gl_error(ctx, GL_INVALID_VALUE, "glTexSubImage3D(zoffset)");
839 return GL_TRUE;
840 }
841 if (zoffset + depth > (GLint) (destTex->Depth+destTex->Border)) {
842 gl_error(ctx, GL_INVALID_VALUE, "glTexSubImage3D(zoffset+depth)");
843 return GL_TRUE;
844 }
845 }
846
Brian Paulb7d076f2000-03-21 01:03:40 +0000847 if (!_mesa_is_legal_format_and_type(format, type)) {
Brian Paulc3f0a511999-11-03 17:27:05 +0000848 char message[100];
849 sprintf(message, "glTexSubImage%dD(format or type)", dimensions);
850 gl_error(ctx, GL_INVALID_ENUM, message);
851 return GL_TRUE;
852 }
853
854 return GL_FALSE;
855}
856
857
858/*
859 * Test glCopyTexImage[12]D() parameters for errors.
860 * Input: dimensions - must be 1 or 2 or 3
861 * Return: GL_TRUE = an error was detected, GL_FALSE = no errors
862 */
863static GLboolean
Brian Paulfbd8f211999-11-11 01:22:25 +0000864copytexture_error_check( GLcontext *ctx, GLuint dimensions,
Brian Paulc3f0a511999-11-03 17:27:05 +0000865 GLenum target, GLint level, GLint internalFormat,
866 GLint width, GLint height, GLint border )
867{
868 GLint iformat;
869
870 if (target != GL_TEXTURE_1D && target != GL_TEXTURE_2D) {
871 gl_error( ctx, GL_INVALID_ENUM, "glCopyTexImage1/2D(target)" );
872 return GL_TRUE;
873 }
874
875 if (dimensions == 1 && target != GL_TEXTURE_1D) {
876 gl_error( ctx, GL_INVALID_ENUM, "glCopyTexImage1D(target)" );
877 return GL_TRUE;
878 }
879 else if (dimensions == 2 && target != GL_TEXTURE_2D) {
880 gl_error( ctx, GL_INVALID_ENUM, "glCopyTexImage2D(target)" );
881 return GL_TRUE;
882 }
883
884 /* Border */
885 if (border!=0 && border!=1) {
886 char message[100];
887 sprintf(message, "glCopyTexImage%dD(border)", dimensions);
888 gl_error(ctx, GL_INVALID_VALUE, message);
889 return GL_TRUE;
890 }
891
892 /* Width */
893 if (width < 2 * border || width > 2 + ctx->Const.MaxTextureSize
894 || logbase2( width - 2 * border ) < 0) {
895 char message[100];
896 sprintf(message, "glCopyTexImage%dD(width)", dimensions);
897 gl_error(ctx, GL_INVALID_VALUE, message);
898 return GL_TRUE;
899 }
900
901 /* Height */
902 if (dimensions >= 2) {
903 if (height < 2 * border || height > 2 + ctx->Const.MaxTextureSize
904 || logbase2( height - 2 * border ) < 0) {
905 char message[100];
906 sprintf(message, "glCopyTexImage%dD(height)", dimensions);
907 gl_error(ctx, GL_INVALID_VALUE, message);
908 return GL_TRUE;
909 }
910 }
911
912 /* Level */
913 if (level<0 || level>=ctx->Const.MaxTextureLevels) {
914 char message[100];
915 sprintf(message, "glCopyTexImage%dD(level)", dimensions);
916 gl_error(ctx, GL_INVALID_VALUE, message);
917 return GL_TRUE;
918 }
919
Brian Paulb132e8d2000-03-23 16:23:14 +0000920 iformat = _mesa_base_tex_format( internalFormat );
Brian Paulc3f0a511999-11-03 17:27:05 +0000921 if (iformat < 0) {
922 char message[100];
923 sprintf(message, "glCopyTexImage%dD(internalFormat)", dimensions);
924 gl_error(ctx, GL_INVALID_VALUE, message);
925 return GL_TRUE;
926 }
927
928 /* if we get here, the parameters are OK */
929 return GL_FALSE;
930}
931
932
933static GLboolean
Brian Paulfbd8f211999-11-11 01:22:25 +0000934copytexsubimage_error_check( GLcontext *ctx, GLuint dimensions,
Brian Paulc3f0a511999-11-03 17:27:05 +0000935 GLenum target, GLint level,
936 GLint xoffset, GLint yoffset, GLint zoffset,
Brian Paul5b37c321999-11-05 06:43:10 +0000937 GLsizei width, GLsizei height )
Brian Paulc3f0a511999-11-03 17:27:05 +0000938{
939 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
940 struct gl_texture_image *teximage;
941
942 if (dimensions == 1 && target != GL_TEXTURE_1D) {
943 gl_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage1D(target)" );
944 return GL_TRUE;
945 }
946 else if (dimensions == 2 && target != GL_TEXTURE_2D) {
947 gl_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage2D(target)" );
948 return GL_TRUE;
949 }
950 else if (dimensions == 3 && target != GL_TEXTURE_3D) {
951 gl_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage3D(target)" );
952 return GL_TRUE;
953 }
954
955 if (level < 0 || level >= ctx->Const.MaxTextureLevels) {
956 char message[100];
957 sprintf(message, "glCopyTexSubImage%dD(level)", dimensions);
958 gl_error(ctx, GL_INVALID_VALUE, message);
959 return GL_TRUE;
960 }
961
962 if (width < 0) {
963 char message[100];
964 sprintf(message, "glCopyTexSubImage%dD(width)", dimensions );
965 gl_error(ctx, GL_INVALID_VALUE, message);
966 return GL_TRUE;
967 }
968 if (dimensions > 1 && height < 0) {
969 char message[100];
970 sprintf(message, "glCopyTexSubImage%dD(height)", dimensions );
971 gl_error(ctx, GL_INVALID_VALUE, message);
972 return GL_TRUE;
973 }
974
Brian Pauldf6a28d2000-02-21 16:34:21 +0000975 teximage = texUnit->CurrentD[dimensions]->Image[level];
Brian Paulc3f0a511999-11-03 17:27:05 +0000976 if (!teximage) {
977 char message[100];
978 sprintf(message, "glCopyTexSubImage%dD(undefined texture)", dimensions);
979 gl_error(ctx, GL_INVALID_OPERATION, message);
980 return GL_TRUE;
981 }
982
983 if (xoffset < -((GLint)teximage->Border)) {
984 char message[100];
985 sprintf(message, "glCopyTexSubImage%dD(xoffset)", dimensions);
986 gl_error(ctx, GL_INVALID_VALUE, message);
987 return GL_TRUE;
988 }
989 if (xoffset+width > (GLint) (teximage->Width+teximage->Border)) {
990 char message[100];
991 sprintf(message, "glCopyTexSubImage%dD(xoffset+width)", dimensions);
992 gl_error(ctx, GL_INVALID_VALUE, message);
993 return GL_TRUE;
994 }
995 if (dimensions > 1) {
996 if (yoffset < -((GLint)teximage->Border)) {
997 char message[100];
998 sprintf(message, "glCopyTexSubImage%dD(yoffset)", dimensions);
999 gl_error(ctx, GL_INVALID_VALUE, message);
1000 return GL_TRUE;
1001 }
1002 /* NOTE: we're adding the border here, not subtracting! */
1003 if (yoffset+height > (GLint) (teximage->Height+teximage->Border)) {
1004 char message[100];
1005 sprintf(message, "glCopyTexSubImage%dD(yoffset+height)", dimensions);
1006 gl_error(ctx, GL_INVALID_VALUE, message);
1007 return GL_TRUE;
1008 }
1009 }
1010
1011 if (dimensions > 2) {
1012 if (zoffset < -((GLint)teximage->Border)) {
1013 char message[100];
1014 sprintf(message, "glCopyTexSubImage%dD(zoffset)", dimensions);
1015 gl_error(ctx, GL_INVALID_VALUE, message);
1016 return GL_TRUE;
1017 }
1018 if (zoffset > (GLint) (teximage->Depth+teximage->Border)) {
1019 char message[100];
1020 sprintf(message, "glCopyTexSubImage%dD(zoffset+depth)", dimensions);
1021 gl_error(ctx, GL_INVALID_VALUE, message);
1022 return GL_TRUE;
1023 }
1024 }
1025
1026 /* if we get here, the parameters are OK */
1027 return GL_FALSE;
1028}
1029
1030
1031
1032
1033/*
jtgafb833d1999-08-19 00:55:39 +00001034 * Called from the API. Note that width includes the border.
1035 */
Brian Paulfbd8f211999-11-11 01:22:25 +00001036void
Brian Paul43911c82000-03-21 00:49:33 +00001037_mesa_TexImage1D( GLenum target, GLint level, GLint internalFormat,
Brian Paulfbd8f211999-11-11 01:22:25 +00001038 GLsizei width, GLint border, GLenum format,
1039 GLenum type, const GLvoid *pixels )
jtgafb833d1999-08-19 00:55:39 +00001040{
Brian Paulfbd8f211999-11-11 01:22:25 +00001041 GET_CURRENT_CONTEXT(ctx);
jtgafb833d1999-08-19 00:55:39 +00001042 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glTexImage1D");
1043
1044 if (target==GL_TEXTURE_1D) {
Brian Paulf7b57072000-03-20 14:37:52 +00001045 struct gl_texture_unit *texUnit;
Brian Paul02938782000-03-22 17:38:11 +00001046 struct gl_texture_object *texObj;
1047 struct gl_texture_image *texImage;
Brian Paulf7b57072000-03-20 14:37:52 +00001048
Brian Paul43911c82000-03-21 00:49:33 +00001049 if (texture_error_check( ctx, target, level, internalFormat,
jtgafb833d1999-08-19 00:55:39 +00001050 format, type, 1, width, 1, 1, border )) {
Brian Paulf7b57072000-03-20 14:37:52 +00001051 return; /* error in texture image was detected */
jtgafb833d1999-08-19 00:55:39 +00001052 }
1053
Brian Paulf7b57072000-03-20 14:37:52 +00001054 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
Brian Paul02938782000-03-22 17:38:11 +00001055 texObj = texUnit->CurrentD[1];
1056 texImage = texObj->Image[level];
Brian Paulf7b57072000-03-20 14:37:52 +00001057
Brian Paul02938782000-03-22 17:38:11 +00001058 if (!texImage) {
Brian Paul021a5252000-03-27 17:54:17 +00001059 texImage = _mesa_alloc_texture_image();
Brian Paul02938782000-03-22 17:38:11 +00001060 texObj->Image[level] = texImage;
1061 if (!texImage) {
1062 gl_error(ctx, GL_OUT_OF_MEMORY, "glTexImage1D");
1063 return;
1064 }
1065 }
1066 else if (texImage->Data) {
1067 FREE(texImage->Data);
1068 texImage->Data = NULL;
jtgafb833d1999-08-19 00:55:39 +00001069 }
1070
Brian Paul02938782000-03-22 17:38:11 +00001071 /* setup the teximage struct's fields */
1072 init_texture_image(texImage, width, 1, 1, border, internalFormat);
Brian Paul43911c82000-03-21 00:49:33 +00001073
Brian Paul02938782000-03-22 17:38:11 +00001074 /* process the texture image */
Brian Paulc3f0a511999-11-03 17:27:05 +00001075 if (pixels) {
Brian Paul02938782000-03-22 17:38:11 +00001076 GLboolean retain = GL_TRUE;
1077 GLboolean success = GL_FALSE;
1078 if (!ctx->Pixel.MapColorFlag && !ctx->Pixel.ScaleOrBiasRGBA
1079 && ctx->Driver.TexImage1D) {
1080 /* let device driver try to use raw image */
1081 success = (*ctx->Driver.TexImage1D)( ctx, target, level, format,
1082 type, pixels, &ctx->Unpack,
1083 texObj, texImage, &retain);
1084 }
1085 if (retain || !success) {
1086 /* make internal copy of the texture image */
1087 make_texture_image(ctx, texImage, format, type,
1088 pixels, &ctx->Unpack);
1089 if (!success && ctx->Driver.TexImage1D) {
1090 /* let device driver try to use unpacked image */
1091 (*ctx->Driver.TexImage1D)( ctx, target, level, texImage->Format,
1092 GL_UNSIGNED_BYTE, texImage->Data,
1093 &_mesa_native_packing,
1094 texObj, texImage, &retain);
1095 }
1096 }
1097 if (!retain && texImage->Data) {
1098 FREE(texImage->Data);
1099 texImage->Data = NULL;
1100 }
jtgafb833d1999-08-19 00:55:39 +00001101 }
1102 else {
Brian Paul02938782000-03-22 17:38:11 +00001103 make_null_texture(texImage);
1104 if (ctx->Driver.TexImage1D) {
1105 GLboolean retain;
1106 (*ctx->Driver.TexImage1D)( ctx, target, level, texImage->Format,
1107 GL_UNSIGNED_BYTE, texImage->Data,
1108 &_mesa_native_packing,
1109 texObj, texImage, &retain);
1110 }
jtgafb833d1999-08-19 00:55:39 +00001111 }
1112
Brian Paul02938782000-03-22 17:38:11 +00001113 /* state update */
1114 gl_put_texobj_on_dirty_list( ctx, texObj );
jtgafb833d1999-08-19 00:55:39 +00001115 ctx->NewState |= NEW_TEXTURING;
jtgafb833d1999-08-19 00:55:39 +00001116 }
1117 else if (target==GL_PROXY_TEXTURE_1D) {
1118 /* Proxy texture: check for errors and update proxy state */
Brian Paul43911c82000-03-21 00:49:33 +00001119 if (texture_error_check( ctx, target, level, internalFormat,
jtgafb833d1999-08-19 00:55:39 +00001120 format, type, 1, width, 1, 1, border )) {
1121 if (level>=0 && level<ctx->Const.MaxTextureLevels) {
1122 MEMSET( ctx->Texture.Proxy1D->Image[level], 0,
1123 sizeof(struct gl_texture_image) );
1124 }
1125 }
1126 else {
1127 ctx->Texture.Proxy1D->Image[level]->Format = (GLenum) format;
1128 set_teximage_component_sizes( ctx->Texture.Proxy1D->Image[level] );
Brian Paul43911c82000-03-21 00:49:33 +00001129 ctx->Texture.Proxy1D->Image[level]->IntFormat = (GLenum) internalFormat;
jtgafb833d1999-08-19 00:55:39 +00001130 ctx->Texture.Proxy1D->Image[level]->Border = border;
1131 ctx->Texture.Proxy1D->Image[level]->Width = width;
1132 ctx->Texture.Proxy1D->Image[level]->Height = 1;
1133 ctx->Texture.Proxy1D->Image[level]->Depth = 1;
1134 }
jtgafb833d1999-08-19 00:55:39 +00001135 }
1136 else {
1137 gl_error( ctx, GL_INVALID_ENUM, "glTexImage1D(target)" );
1138 return;
1139 }
1140}
1141
1142
Brian Paulfbd8f211999-11-11 01:22:25 +00001143void
Brian Paul43911c82000-03-21 00:49:33 +00001144_mesa_TexImage2D( GLenum target, GLint level, GLint internalFormat,
Brian Paulfbd8f211999-11-11 01:22:25 +00001145 GLsizei width, GLsizei height, GLint border,
1146 GLenum format, GLenum type,
1147 const GLvoid *pixels )
jtgafb833d1999-08-19 00:55:39 +00001148{
Brian Paulfbd8f211999-11-11 01:22:25 +00001149 GET_CURRENT_CONTEXT(ctx);
jtgafb833d1999-08-19 00:55:39 +00001150 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glTexImage2D");
1151
1152 if (target==GL_TEXTURE_2D) {
Brian Paulf7b57072000-03-20 14:37:52 +00001153 struct gl_texture_unit *texUnit;
Brian Paul02938782000-03-22 17:38:11 +00001154 struct gl_texture_object *texObj;
1155 struct gl_texture_image *texImage;
Brian Paulf7b57072000-03-20 14:37:52 +00001156
Brian Paul43911c82000-03-21 00:49:33 +00001157 if (texture_error_check( ctx, target, level, internalFormat,
jtgafb833d1999-08-19 00:55:39 +00001158 format, type, 2, width, height, 1, border )) {
Brian Paulf7b57072000-03-20 14:37:52 +00001159 return; /* error in texture image was detected */
jtgafb833d1999-08-19 00:55:39 +00001160 }
1161
Brian Paulf7b57072000-03-20 14:37:52 +00001162 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
Brian Paul02938782000-03-22 17:38:11 +00001163 texObj = texUnit->CurrentD[2];
1164 texImage = texObj->Image[level];
Brian Paulf7b57072000-03-20 14:37:52 +00001165
Brian Paul02938782000-03-22 17:38:11 +00001166 if (!texImage) {
Brian Paul021a5252000-03-27 17:54:17 +00001167 texImage = _mesa_alloc_texture_image();
Brian Paul02938782000-03-22 17:38:11 +00001168 texObj->Image[level] = texImage;
1169 if (!texImage) {
1170 gl_error(ctx, GL_OUT_OF_MEMORY, "glTexImage2D");
1171 return;
1172 }
1173 }
1174 else if (texImage->Data) {
1175 FREE(texImage->Data);
1176 texImage->Data = NULL;
jtgafb833d1999-08-19 00:55:39 +00001177 }
1178
Brian Paul02938782000-03-22 17:38:11 +00001179 /* setup the teximage struct's fields */
1180 init_texture_image(texImage, width, height, 1, border, internalFormat);
Brian Paul43911c82000-03-21 00:49:33 +00001181
Brian Paul02938782000-03-22 17:38:11 +00001182 /* process the texture image */
Brian Paulc3f0a511999-11-03 17:27:05 +00001183 if (pixels) {
Brian Paul02938782000-03-22 17:38:11 +00001184 GLboolean retain = GL_TRUE;
1185 GLboolean success = GL_FALSE;
1186 if (!ctx->Pixel.MapColorFlag && !ctx->Pixel.ScaleOrBiasRGBA
1187 && ctx->Driver.TexImage2D) {
1188 /* let device driver try to use raw image */
1189 success = (*ctx->Driver.TexImage2D)( ctx, target, level, format,
1190 type, pixels, &ctx->Unpack,
1191 texObj, texImage, &retain);
1192 }
1193 if (retain || !success) {
1194 /* make internal copy of the texture image */
1195 make_texture_image(ctx, texImage, format, type,
1196 pixels, &ctx->Unpack);
1197 if (!success && ctx->Driver.TexImage2D) {
1198 /* let device driver try to use unpacked image */
1199 (*ctx->Driver.TexImage2D)( ctx, target, level, texImage->Format,
1200 GL_UNSIGNED_BYTE, texImage->Data,
1201 &_mesa_native_packing,
1202 texObj, texImage, &retain);
1203 }
1204 }
1205 if (!retain && texImage->Data) {
1206 FREE(texImage->Data);
1207 texImage->Data = NULL;
1208 }
jtgafb833d1999-08-19 00:55:39 +00001209 }
1210 else {
Brian Paul02938782000-03-22 17:38:11 +00001211 make_null_texture(texImage);
1212 if (ctx->Driver.TexImage2D) {
1213 GLboolean retain;
1214 (*ctx->Driver.TexImage2D)( ctx, target, level, texImage->Format,
1215 GL_UNSIGNED_BYTE, texImage->Data,
1216 &_mesa_native_packing,
1217 texObj, texImage, &retain);
1218 }
jtgafb833d1999-08-19 00:55:39 +00001219 }
1220
Brian Paul02938782000-03-22 17:38:11 +00001221#define OLD_DD_TEXTURE
1222#ifdef OLD_DD_TEXTURE
1223 /* XXX this will be removed in the future */
jtgafb833d1999-08-19 00:55:39 +00001224 if (ctx->Driver.TexImage) {
Brian Paul02938782000-03-22 17:38:11 +00001225 (*ctx->Driver.TexImage)( ctx, target, texObj, level, internalFormat,
1226 texImage );
jtgafb833d1999-08-19 00:55:39 +00001227 }
Brian Paul02938782000-03-22 17:38:11 +00001228#endif
1229
1230 /* state update */
1231 gl_put_texobj_on_dirty_list( ctx, texObj );
1232 ctx->NewState |= NEW_TEXTURING;
jtgafb833d1999-08-19 00:55:39 +00001233 }
1234 else if (target==GL_PROXY_TEXTURE_2D) {
1235 /* Proxy texture: check for errors and update proxy state */
Brian Paul43911c82000-03-21 00:49:33 +00001236 if (texture_error_check( ctx, target, level, internalFormat,
jtgafb833d1999-08-19 00:55:39 +00001237 format, type, 2, width, height, 1, border )) {
1238 if (level>=0 && level<ctx->Const.MaxTextureLevels) {
1239 MEMSET( ctx->Texture.Proxy2D->Image[level], 0,
1240 sizeof(struct gl_texture_image) );
1241 }
1242 }
1243 else {
1244 ctx->Texture.Proxy2D->Image[level]->Format = (GLenum) format;
1245 set_teximage_component_sizes( ctx->Texture.Proxy2D->Image[level] );
Brian Paul43911c82000-03-21 00:49:33 +00001246 ctx->Texture.Proxy2D->Image[level]->IntFormat = (GLenum) internalFormat;
jtgafb833d1999-08-19 00:55:39 +00001247 ctx->Texture.Proxy2D->Image[level]->Border = border;
1248 ctx->Texture.Proxy2D->Image[level]->Width = width;
1249 ctx->Texture.Proxy2D->Image[level]->Height = height;
1250 ctx->Texture.Proxy2D->Image[level]->Depth = 1;
1251 }
jtgafb833d1999-08-19 00:55:39 +00001252 }
1253 else {
1254 gl_error( ctx, GL_INVALID_ENUM, "glTexImage2D(target)" );
1255 return;
1256 }
1257}
1258
1259
1260
1261/*
1262 * Called by the API or display list executor.
1263 * Note that width and height include the border.
1264 */
Brian Paulfbd8f211999-11-11 01:22:25 +00001265void
Brian Paul43911c82000-03-21 00:49:33 +00001266_mesa_TexImage3D( GLenum target, GLint level, GLint internalFormat,
Brian Paulfbd8f211999-11-11 01:22:25 +00001267 GLsizei width, GLsizei height, GLsizei depth,
1268 GLint border, GLenum format, GLenum type,
1269 const GLvoid *pixels )
jtgafb833d1999-08-19 00:55:39 +00001270{
Brian Paulfbd8f211999-11-11 01:22:25 +00001271 GET_CURRENT_CONTEXT(ctx);
Brian Paulf7b57072000-03-20 14:37:52 +00001272 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glTexImage3D");
jtgafb833d1999-08-19 00:55:39 +00001273
Brian Paulfbd8f211999-11-11 01:22:25 +00001274 if (target==GL_TEXTURE_3D_EXT) {
Brian Paulf7b57072000-03-20 14:37:52 +00001275 struct gl_texture_unit *texUnit;
Brian Paul02938782000-03-22 17:38:11 +00001276 struct gl_texture_object *texObj;
1277 struct gl_texture_image *texImage;
Brian Paul43911c82000-03-21 00:49:33 +00001278 if (texture_error_check( ctx, target, level, internalFormat,
jtgafb833d1999-08-19 00:55:39 +00001279 format, type, 3, width, height, depth,
1280 border )) {
Brian Paulf7b57072000-03-20 14:37:52 +00001281 return; /* error in texture image was detected */
jtgafb833d1999-08-19 00:55:39 +00001282 }
1283
Brian Paulf7b57072000-03-20 14:37:52 +00001284 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
Brian Paul02938782000-03-22 17:38:11 +00001285 texObj = texUnit->CurrentD[3];
1286 texImage = texObj->Image[level];
Brian Paulf7b57072000-03-20 14:37:52 +00001287
Brian Paul02938782000-03-22 17:38:11 +00001288 if (!texImage) {
Brian Paul021a5252000-03-27 17:54:17 +00001289 texImage = _mesa_alloc_texture_image();
Brian Paul02938782000-03-22 17:38:11 +00001290 texObj->Image[level] = texImage;
1291 if (!texImage) {
1292 gl_error(ctx, GL_OUT_OF_MEMORY, "glTexImage3D");
1293 return;
1294 }
1295 }
1296 else if (texImage->Data) {
1297 FREE(texImage->Data);
1298 texImage->Data = NULL;
jtgafb833d1999-08-19 00:55:39 +00001299 }
1300
Brian Paul02938782000-03-22 17:38:11 +00001301 /* setup the teximage struct's fields */
1302 init_texture_image(texImage, width, height, depth,
1303 border, internalFormat);
Brian Paul43911c82000-03-21 00:49:33 +00001304
Brian Paul02938782000-03-22 17:38:11 +00001305 /* process the texture image */
Brian Paulc3f0a511999-11-03 17:27:05 +00001306 if (pixels) {
Brian Paul02938782000-03-22 17:38:11 +00001307 GLboolean retain = GL_TRUE;
1308 GLboolean success = GL_FALSE;
1309 if (!ctx->Pixel.MapColorFlag && !ctx->Pixel.ScaleOrBiasRGBA
1310 && ctx->Driver.TexImage3D) {
1311 /* let device driver try to use raw image */
1312 success = (*ctx->Driver.TexImage3D)( ctx, target, level, format,
1313 type, pixels, &ctx->Unpack,
1314 texObj, texImage, &retain);
1315 }
1316 if (retain || !success) {
1317 /* make internal copy of the texture image */
1318 make_texture_image(ctx, texImage, format, type,
1319 pixels, &ctx->Unpack);
1320 if (!success && ctx->Driver.TexImage3D) {
1321 /* let device driver try to use unpacked image */
1322 (*ctx->Driver.TexImage3D)( ctx, target, level, texImage->Format,
1323 GL_UNSIGNED_BYTE, texImage->Data,
1324 &_mesa_native_packing,
1325 texObj, texImage, &retain);
1326 }
1327 }
1328 if (!retain && texImage->Data) {
1329 FREE(texImage->Data);
1330 texImage->Data = NULL;
1331 }
jtgafb833d1999-08-19 00:55:39 +00001332 }
1333 else {
Brian Paul02938782000-03-22 17:38:11 +00001334 make_null_texture(texImage);
1335 if (ctx->Driver.TexImage3D) {
1336 GLboolean retain;
1337 (*ctx->Driver.TexImage3D)( ctx, target, level, texImage->Format,
1338 GL_UNSIGNED_BYTE, texImage->Data,
1339 &_mesa_native_packing,
1340 texObj, texImage, &retain);
1341 }
jtgafb833d1999-08-19 00:55:39 +00001342 }
1343
Brian Paul02938782000-03-22 17:38:11 +00001344 /* state update */
1345 gl_put_texobj_on_dirty_list( ctx, texObj );
jtgafb833d1999-08-19 00:55:39 +00001346 ctx->NewState |= NEW_TEXTURING;
jtgafb833d1999-08-19 00:55:39 +00001347 }
1348 else if (target==GL_PROXY_TEXTURE_3D_EXT) {
1349 /* Proxy texture: check for errors and update proxy state */
Brian Paul43911c82000-03-21 00:49:33 +00001350 if (texture_error_check( ctx, target, level, internalFormat,
jtgafb833d1999-08-19 00:55:39 +00001351 format, type, 3, width, height, depth,
1352 border )) {
1353 if (level>=0 && level<ctx->Const.MaxTextureLevels) {
1354 MEMSET( ctx->Texture.Proxy3D->Image[level], 0,
1355 sizeof(struct gl_texture_image) );
1356 }
1357 }
1358 else {
1359 ctx->Texture.Proxy3D->Image[level]->Format = (GLenum) format;
1360 set_teximage_component_sizes( ctx->Texture.Proxy3D->Image[level] );
Brian Paul43911c82000-03-21 00:49:33 +00001361 ctx->Texture.Proxy3D->Image[level]->IntFormat = (GLenum) internalFormat;
jtgafb833d1999-08-19 00:55:39 +00001362 ctx->Texture.Proxy3D->Image[level]->Border = border;
1363 ctx->Texture.Proxy3D->Image[level]->Width = width;
1364 ctx->Texture.Proxy3D->Image[level]->Height = height;
1365 ctx->Texture.Proxy3D->Image[level]->Depth = depth;
1366 }
jtgafb833d1999-08-19 00:55:39 +00001367 }
1368 else {
Brian Paulc3f0a511999-11-03 17:27:05 +00001369 gl_error( ctx, GL_INVALID_ENUM, "glTexImage3D(target)" );
jtgafb833d1999-08-19 00:55:39 +00001370 return;
1371 }
1372}
1373
1374
Brian Paul663049a2000-01-31 23:10:16 +00001375void
Brian Paul43911c82000-03-21 00:49:33 +00001376_mesa_TexImage3DEXT( GLenum target, GLint level, GLenum internalFormat,
Brian Paul663049a2000-01-31 23:10:16 +00001377 GLsizei width, GLsizei height, GLsizei depth,
1378 GLint border, GLenum format, GLenum type,
1379 const GLvoid *pixels )
1380{
Brian Paul43911c82000-03-21 00:49:33 +00001381 _mesa_TexImage3D(target, level, (GLint) internalFormat, width, height,
Brian Paul663049a2000-01-31 23:10:16 +00001382 depth, border, format, type, pixels);
1383}
1384
1385
Brian Paulf7b57072000-03-20 14:37:52 +00001386/*
1387 * Fetch a texture image from the device driver.
1388 * Store the results in the given texture object at the given mipmap level.
1389 */
Brian Paul021a5252000-03-27 17:54:17 +00001390void
1391_mesa_get_teximage_from_driver( GLcontext *ctx, GLenum target, GLint level,
1392 const struct gl_texture_object *texObj )
Brian Paulf7b57072000-03-20 14:37:52 +00001393{
1394 GLvoid *image;
1395 GLenum imgFormat, imgType;
1396 GLboolean freeImage;
1397 struct gl_texture_image *texImage;
1398 GLint destComponents, numPixels, srcBytesPerTexel;
1399
1400 if (!ctx->Driver.GetTexImage)
1401 return;
1402
Brian Paul48271792000-03-29 18:13:59 +00001403 image = (*ctx->Driver.GetTexImage)( ctx, target, level, texObj,
Brian Paulf7b57072000-03-20 14:37:52 +00001404 &imgFormat, &imgType, &freeImage);
1405 if (!image)
1406 return;
1407
1408 texImage = texObj->Image[level];
1409 ASSERT(texImage);
1410 if (!texImage)
1411 return;
1412
1413 destComponents = components_in_intformat(texImage->Format);
1414 assert(destComponents > 0);
1415 numPixels = texImage->Width * texImage->Height * texImage->Depth;
1416 assert(numPixels > 0);
Brian Paulb7d076f2000-03-21 01:03:40 +00001417 srcBytesPerTexel = _mesa_bytes_per_pixel(imgFormat, imgType);
Brian Paulf7b57072000-03-20 14:37:52 +00001418 assert(srcBytesPerTexel > 0);
1419
1420 if (!texImage->Data) {
1421 /* Allocate memory for the texture image data */
1422 texImage->Data = (GLubyte *) MALLOC(numPixels * destComponents + EXTRA_BYTE);
1423 }
1424
1425 if (imgFormat == texImage->Format && imgType == GL_UNSIGNED_BYTE) {
1426 /* We got lucky! The driver's format and type match Mesa's format. */
1427 if (texImage->Data) {
1428 MEMCPY(texImage->Data, image, numPixels * destComponents);
1429 }
1430 }
1431 else {
1432 /* Convert the texture image from the driver's format to Mesa's
1433 * internal format.
1434 */
1435 const GLint width = texImage->Width;
1436 const GLint height = texImage->Height;
1437 const GLint depth = texImage->Depth;
1438 const GLint destBytesPerRow = width * destComponents * sizeof(GLchan);
1439 const GLint srcBytesPerRow = width * srcBytesPerTexel;
1440 const GLenum dstType = GL_UNSIGNED_BYTE;
1441 const GLenum dstFormat = texImage->Format;
1442 const GLubyte *srcPtr = (const GLubyte *) image;
1443 GLubyte *destPtr = texImage->Data;
1444
1445 if (texImage->Format == GL_COLOR_INDEX) {
1446 /* color index texture */
1447 GLint img, row;
1448 assert(imgFormat == GL_COLOR_INDEX);
1449 for (img = 0; img < depth; img++) {
1450 for (row = 0; row < height; row++) {
1451 _mesa_unpack_index_span(ctx, width, dstType, destPtr,
Brian Paul02938782000-03-22 17:38:11 +00001452 imgType, srcPtr, &_mesa_native_packing, GL_FALSE);
Brian Paulf7b57072000-03-20 14:37:52 +00001453 destPtr += destBytesPerRow;
1454 srcPtr += srcBytesPerRow;
1455 }
1456 }
1457 }
1458 else {
1459 /* color texture */
1460 GLint img, row;
1461 for (img = 0; img < depth; img++) {
1462 for (row = 0; row < height; row++) {
1463 _mesa_unpack_ubyte_color_span(ctx, width, dstFormat, destPtr,
Brian Paul02938782000-03-22 17:38:11 +00001464 imgFormat, imgType, srcPtr, &_mesa_native_packing, GL_FALSE);
Brian Paulf7b57072000-03-20 14:37:52 +00001465 destPtr += destBytesPerRow;
1466 srcPtr += srcBytesPerRow;
1467 }
1468 }
1469 }
1470 }
1471
1472 if (freeImage)
1473 FREE(image);
1474}
1475
jtgafb833d1999-08-19 00:55:39 +00001476
Brian Paulfbd8f211999-11-11 01:22:25 +00001477void
1478_mesa_GetTexImage( GLenum target, GLint level, GLenum format,
1479 GLenum type, GLvoid *pixels )
jtgafb833d1999-08-19 00:55:39 +00001480{
Brian Paulfbd8f211999-11-11 01:22:25 +00001481 GET_CURRENT_CONTEXT(ctx);
jtgafb833d1999-08-19 00:55:39 +00001482 const struct gl_texture_object *texObj;
Brian Paulf7b57072000-03-20 14:37:52 +00001483 struct gl_texture_image *texImage;
1484 GLboolean discardImage;
jtgafb833d1999-08-19 00:55:39 +00001485
1486 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glGetTexImage");
1487
1488 if (level < 0 || level >= ctx->Const.MaxTextureLevels) {
1489 gl_error( ctx, GL_INVALID_VALUE, "glGetTexImage(level)" );
1490 return;
1491 }
1492
Brian Paulb7d076f2000-03-21 01:03:40 +00001493 if (_mesa_sizeof_type(type) <= 0) {
jtgafb833d1999-08-19 00:55:39 +00001494 gl_error( ctx, GL_INVALID_ENUM, "glGetTexImage(type)" );
1495 return;
1496 }
1497
Brian Paulb7d076f2000-03-21 01:03:40 +00001498 if (_mesa_components_in_format(format) <= 0) {
jtgafb833d1999-08-19 00:55:39 +00001499 gl_error( ctx, GL_INVALID_ENUM, "glGetTexImage(format)" );
1500 return;
1501 }
1502
1503 if (!pixels)
Brian Paulf7b57072000-03-20 14:37:52 +00001504 return;
jtgafb833d1999-08-19 00:55:39 +00001505
1506 switch (target) {
1507 case GL_TEXTURE_1D:
1508 texObj = ctx->Texture.Unit[ctx->Texture.CurrentUnit].CurrentD[1];
1509 break;
1510 case GL_TEXTURE_2D:
1511 texObj = ctx->Texture.Unit[ctx->Texture.CurrentUnit].CurrentD[2];
1512 break;
1513 case GL_TEXTURE_3D:
1514 texObj = ctx->Texture.Unit[ctx->Texture.CurrentUnit].CurrentD[3];
1515 break;
1516 default:
1517 gl_error( ctx, GL_INVALID_ENUM, "glGetTexImage(target)" );
1518 return;
1519 }
1520
Brian Paulf7b57072000-03-20 14:37:52 +00001521 texImage = texObj->Image[level];
1522 if (!texImage) {
1523 /* invalid mipmap level */
1524 return;
1525 }
1526
1527 if (!texImage->Data) {
1528 /* try to get the texture image from the device driver */
Brian Paul021a5252000-03-27 17:54:17 +00001529 _mesa_get_teximage_from_driver(ctx, target, level, texObj);
Brian Paulf7b57072000-03-20 14:37:52 +00001530 discardImage = GL_TRUE;
1531 }
1532 else {
1533 discardImage = GL_FALSE;
1534 }
1535
1536 if (texImage->Data) {
jtgafb833d1999-08-19 00:55:39 +00001537 GLint width = texImage->Width;
1538 GLint height = texImage->Height;
1539 GLint row;
1540
1541 for (row = 0; row < height; row++) {
1542 /* compute destination address in client memory */
Brian Paulb7d076f2000-03-21 01:03:40 +00001543 GLvoid *dest = _mesa_image_address( &ctx->Unpack, pixels,
jtgafb833d1999-08-19 00:55:39 +00001544 width, height,
1545 format, type, 0, row, 0);
1546
1547 assert(dest);
1548 if (texImage->Format == GL_RGBA) {
1549 const GLubyte *src = texImage->Data + row * width * 4 * sizeof(GLubyte);
Brian Paulb7d076f2000-03-21 01:03:40 +00001550 _mesa_pack_rgba_span( ctx, width, (CONST GLubyte (*)[4]) src,
1551 format, type, dest, &ctx->Pack, GL_TRUE );
jtgafb833d1999-08-19 00:55:39 +00001552 }
1553 else {
1554 /* fetch RGBA row from texture image then pack it in client mem */
1555 GLubyte rgba[MAX_WIDTH][4];
1556 GLint i;
1557 const GLubyte *src;
1558 switch (texImage->Format) {
1559 case GL_ALPHA:
1560 src = texImage->Data + row * width * sizeof(GLubyte);
1561 for (i = 0; i < width; i++) {
1562 rgba[i][RCOMP] = 255;
1563 rgba[i][GCOMP] = 255;
1564 rgba[i][BCOMP] = 255;
1565 rgba[i][ACOMP] = src[i];
1566 }
1567 break;
1568 case GL_LUMINANCE:
1569 src = texImage->Data + row * width * sizeof(GLubyte);
1570 for (i = 0; i < width; i++) {
1571 rgba[i][RCOMP] = src[i];
1572 rgba[i][GCOMP] = src[i];
1573 rgba[i][BCOMP] = src[i];
1574 rgba[i][ACOMP] = 255;
1575 }
1576 break;
1577 case GL_LUMINANCE_ALPHA:
1578 src = texImage->Data + row * 2 * width * sizeof(GLubyte);
1579 for (i = 0; i < width; i++) {
1580 rgba[i][RCOMP] = src[i*2+0];
1581 rgba[i][GCOMP] = src[i*2+0];
1582 rgba[i][BCOMP] = src[i*2+0];
1583 rgba[i][ACOMP] = src[i*2+1];
1584 }
1585 break;
1586 case GL_INTENSITY:
1587 src = texImage->Data + row * width * sizeof(GLubyte);
1588 for (i = 0; i < width; i++) {
1589 rgba[i][RCOMP] = src[i];
1590 rgba[i][GCOMP] = src[i];
1591 rgba[i][BCOMP] = src[i];
1592 rgba[i][ACOMP] = 255;
1593 }
1594 break;
1595 case GL_RGB:
1596 src = texImage->Data + row * 3 * width * sizeof(GLubyte);
1597 for (i = 0; i < width; i++) {
1598 rgba[i][RCOMP] = src[i*3+0];
1599 rgba[i][GCOMP] = src[i*3+1];
1600 rgba[i][BCOMP] = src[i*3+2];
1601 rgba[i][ACOMP] = 255;
1602 }
1603 break;
1604 case GL_RGBA:
1605 /* this special case should have been handled above! */
1606 gl_problem( ctx, "error 1 in gl_GetTexImage" );
1607 break;
1608 case GL_COLOR_INDEX:
1609 gl_problem( ctx, "GL_COLOR_INDEX not implemented in gl_GetTexImage" );
1610 break;
1611 default:
1612 gl_problem( ctx, "bad format in gl_GetTexImage" );
1613 }
Brian Paulb7d076f2000-03-21 01:03:40 +00001614 _mesa_pack_rgba_span( ctx, width, (const GLubyte (*)[4])rgba,
1615 format, type, dest, &ctx->Pack, GL_TRUE );
jtgafb833d1999-08-19 00:55:39 +00001616 }
1617 }
Brian Paulf7b57072000-03-20 14:37:52 +00001618
1619 /* if we got the teximage from the device driver we'll discard it now */
1620 if (discardImage) {
1621 FREE(texImage->Data);
1622 texImage->Data = NULL;
1623 }
jtgafb833d1999-08-19 00:55:39 +00001624 }
1625}
1626
1627
1628
Brian Paulfbd8f211999-11-11 01:22:25 +00001629void
1630_mesa_TexSubImage1D( GLenum target, GLint level,
1631 GLint xoffset, GLsizei width,
1632 GLenum format, GLenum type,
1633 const GLvoid *pixels )
jtgafb833d1999-08-19 00:55:39 +00001634{
Brian Paulfbd8f211999-11-11 01:22:25 +00001635 GET_CURRENT_CONTEXT(ctx);
Brian Paul02938782000-03-22 17:38:11 +00001636 struct gl_texture_unit *texUnit;
1637 struct gl_texture_object *texObj;
1638 struct gl_texture_image *texImage;
1639 GLboolean success = GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +00001640
Brian Paulc3f0a511999-11-03 17:27:05 +00001641 if (subtexture_error_check(ctx, 1, target, level, xoffset, 0, 0,
1642 width, 1, 1, format, type)) {
Brian Paulf7b57072000-03-20 14:37:52 +00001643 return; /* error was detected */
jtgafb833d1999-08-19 00:55:39 +00001644 }
1645
Brian Paul02938782000-03-22 17:38:11 +00001646 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1647 texObj = texUnit->CurrentD[1];
1648 texImage = texObj->Image[level];
1649 assert(texImage);
jtgafb833d1999-08-19 00:55:39 +00001650
Brian Paulc3f0a511999-11-03 17:27:05 +00001651 if (width == 0 || !pixels)
1652 return; /* no-op, not an error */
jtgafb833d1999-08-19 00:55:39 +00001653
Brian Paulc3f0a511999-11-03 17:27:05 +00001654
Brian Paul02938782000-03-22 17:38:11 +00001655 if (!ctx->Pixel.MapColorFlag && !ctx->Pixel.ScaleOrBiasRGBA
1656 && ctx->Driver.TexSubImage1D) {
1657 success = (*ctx->Driver.TexSubImage1D)( ctx, target, level, xoffset,
1658 width, format, type, pixels,
1659 &ctx->Unpack, texObj, texImage );
1660 }
1661 if (!success) {
1662 /* XXX if Driver.TexSubImage1D, unpack image and try again? */
1663
1664 const GLint texComponents = components_in_intformat(texImage->Format);
1665 const GLenum texFormat = texImage->Format;
1666 const GLint xoffsetb = xoffset + texImage->Border;
1667 GLboolean retain = GL_TRUE;
1668 if (!texImage->Data) {
Brian Paul021a5252000-03-27 17:54:17 +00001669 _mesa_get_teximage_from_driver( ctx, target, level, texObj );
Brian Paul02938782000-03-22 17:38:11 +00001670 if (!texImage->Data) {
1671 make_null_texture(texImage);
1672 }
1673 if (!texImage->Data)
1674 return; /* we're really out of luck! */
1675 }
1676
Brian Paulc3f0a511999-11-03 17:27:05 +00001677 if (texFormat == GL_COLOR_INDEX) {
1678 /* color index texture */
Brian Paul02938782000-03-22 17:38:11 +00001679 GLubyte *dst = texImage->Data + xoffsetb * texComponents;
1680 const GLvoid *src = _mesa_image_address(&ctx->Unpack, pixels, width,
1681 1, format, type, 0, 0, 0);
Brian Paulc3f0a511999-11-03 17:27:05 +00001682 _mesa_unpack_index_span(ctx, width, GL_UNSIGNED_BYTE, dst,
Brian Paul02938782000-03-22 17:38:11 +00001683 type, src, &ctx->Unpack, GL_TRUE);
jtgafb833d1999-08-19 00:55:39 +00001684 }
1685 else {
Brian Paulc3f0a511999-11-03 17:27:05 +00001686 /* color texture */
Brian Paul02938782000-03-22 17:38:11 +00001687 GLubyte *dst = texImage->Data + xoffsetb * texComponents;
1688 const GLvoid *src = _mesa_image_address(&ctx->Unpack, pixels, width,
1689 1, format, type, 0, 0, 0);
1690 _mesa_unpack_ubyte_color_span(ctx, width, texFormat, dst, format,
1691 type, src, &ctx->Unpack, GL_TRUE);
1692 }
1693
1694 if (ctx->Driver.TexImage1D) {
1695 (*ctx->Driver.TexImage1D)( ctx, target, level, texImage->Format,
1696 GL_UNSIGNED_BYTE, texImage->Data,
1697 &_mesa_native_packing, texObj, texImage,
1698 &retain );
1699 }
1700
1701 if (!retain && texImage->Data) {
1702 FREE(texImage->Data);
1703 texImage->Data = NULL;
jtgafb833d1999-08-19 00:55:39 +00001704 }
Brian Paulc3f0a511999-11-03 17:27:05 +00001705 }
jtgafb833d1999-08-19 00:55:39 +00001706
Brian Paul02938782000-03-22 17:38:11 +00001707 /*gl_put_texobj_on_dirty_list( ctx, texUnit->CurrentD[1] );*/
jtgafb833d1999-08-19 00:55:39 +00001708}
1709
1710
Brian Paulfbd8f211999-11-11 01:22:25 +00001711void
1712_mesa_TexSubImage2D( GLenum target, GLint level,
1713 GLint xoffset, GLint yoffset,
1714 GLsizei width, GLsizei height,
1715 GLenum format, GLenum type,
1716 const GLvoid *pixels )
jtgafb833d1999-08-19 00:55:39 +00001717{
Brian Paulfbd8f211999-11-11 01:22:25 +00001718 GET_CURRENT_CONTEXT(ctx);
Brian Paul02938782000-03-22 17:38:11 +00001719 struct gl_texture_unit *texUnit;
1720 struct gl_texture_object *texObj;
1721 struct gl_texture_image *texImage;
1722 GLboolean success = GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +00001723
Brian Paulc3f0a511999-11-03 17:27:05 +00001724 if (subtexture_error_check(ctx, 2, target, level, xoffset, yoffset, 0,
1725 width, height, 1, format, type)) {
Brian Paulf7b57072000-03-20 14:37:52 +00001726 return; /* error was detected */
jtgafb833d1999-08-19 00:55:39 +00001727 }
1728
Brian Paul02938782000-03-22 17:38:11 +00001729 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1730 texObj = texUnit->CurrentD[2];
1731 texImage = texObj->Image[level];
1732 assert(texImage);
jtgafb833d1999-08-19 00:55:39 +00001733
Brian Paulc3f0a511999-11-03 17:27:05 +00001734 if (width == 0 || height == 0 || !pixels)
1735 return; /* no-op, not an error */
jtgafb833d1999-08-19 00:55:39 +00001736
Brian Paul02938782000-03-22 17:38:11 +00001737 if (!ctx->Pixel.MapColorFlag && !ctx->Pixel.ScaleOrBiasRGBA
1738 && ctx->Driver.TexSubImage2D) {
1739 success = (*ctx->Driver.TexSubImage2D)( ctx, target, level, xoffset,
1740 yoffset, width, height, format, type,
1741 pixels, &ctx->Unpack, texObj, texImage );
1742 }
1743 if (!success) {
1744 /* XXX if Driver.TexSubImage2D, unpack image and try again? */
Brian Paulc3f0a511999-11-03 17:27:05 +00001745
Brian Paul02938782000-03-22 17:38:11 +00001746 const GLint texComponents = components_in_intformat(texImage->Format);
1747 const GLenum texFormat = texImage->Format;
1748 const GLint xoffsetb = xoffset + texImage->Border;
1749 const GLint yoffsetb = yoffset + texImage->Border;
1750 const GLint srcStride = _mesa_image_row_stride(&ctx->Unpack, width,
1751 format, type);
1752 const GLint dstStride = texImage->Width * texComponents *sizeof(GLubyte);
1753 GLboolean retain = GL_TRUE;
1754
1755 if (!texImage->Data) {
Brian Paul021a5252000-03-27 17:54:17 +00001756 _mesa_get_teximage_from_driver( ctx, target, level, texObj );
Brian Paul02938782000-03-22 17:38:11 +00001757 if (!texImage->Data) {
1758 make_null_texture(texImage);
1759 }
1760 if (!texImage->Data)
1761 return; /* we're really out of luck! */
1762 }
1763
Brian Paulc3f0a511999-11-03 17:27:05 +00001764 if (texFormat == GL_COLOR_INDEX) {
1765 /* color index texture */
Brian Paul02938782000-03-22 17:38:11 +00001766 GLubyte *dst = texImage->Data
1767 + (yoffsetb * texImage->Width + xoffsetb) * texComponents;
1768 const GLubyte *src = _mesa_image_address(&ctx->Unpack, pixels,
1769 width, height, format, type, 0, 0, 0);
Brian Paulc3f0a511999-11-03 17:27:05 +00001770 GLint row;
1771 for (row = 0; row < height; row++) {
Brian Paul02938782000-03-22 17:38:11 +00001772 _mesa_unpack_index_span(ctx, width, GL_UNSIGNED_BYTE, dst, type,
1773 (const GLvoid *) src, &ctx->Unpack, GL_TRUE);
1774 src += srcStride;
1775 dst += dstStride;
Brian Paul64a79b21999-10-22 10:43:35 +00001776 }
1777 }
jtgafb833d1999-08-19 00:55:39 +00001778 else {
Brian Paulc3f0a511999-11-03 17:27:05 +00001779 /* color texture */
Brian Paul02938782000-03-22 17:38:11 +00001780 GLubyte *dst = texImage->Data
1781 + (yoffsetb * texImage->Width + xoffsetb) * texComponents;
1782 const GLubyte *src = _mesa_image_address(&ctx->Unpack, pixels,
1783 width, height, format, type, 0, 0, 0);
Brian Paulc3f0a511999-11-03 17:27:05 +00001784 GLint row;
1785 for (row = 0; row < height; row++) {
Brian Paul02938782000-03-22 17:38:11 +00001786 _mesa_unpack_ubyte_color_span(ctx, width, texFormat, dst, format,
1787 type, (const GLvoid *) src, &ctx->Unpack, GL_TRUE);
1788 src += srcStride;
1789 dst += dstStride;
jtgafb833d1999-08-19 00:55:39 +00001790 }
jtgafb833d1999-08-19 00:55:39 +00001791 }
jtgafb833d1999-08-19 00:55:39 +00001792
Brian Paul02938782000-03-22 17:38:11 +00001793 if (ctx->Driver.TexImage2D) {
1794 (*ctx->Driver.TexImage2D)(ctx, target, level, texImage->Format,
1795 GL_UNSIGNED_BYTE, texImage->Data,
1796 &_mesa_native_packing, texObj, texImage,
1797 &retain);
jtgafb833d1999-08-19 00:55:39 +00001798 }
Brian Paul02938782000-03-22 17:38:11 +00001799
1800 if (!retain && texImage->Data) {
1801 FREE(texImage->Data);
1802 texImage->Data = NULL;
1803 }
1804
1805#ifdef OLD_DD_TEXTURE
1806 /* XXX this will be removed in the future */
1807 if (ctx->Driver.TexSubImage) {
1808 (*ctx->Driver.TexSubImage)(ctx, target, texObj, level,
1809 xoffset, yoffset, width, height,
1810 texImage->IntFormat, texImage);
1811 }
1812 else if (ctx->Driver.TexImage) {
Brian Paul9fd2b0a2000-03-24 23:59:06 +00001813 (*ctx->Driver.TexImage)(ctx, GL_TEXTURE_2D, texObj,
Brian Paul02938782000-03-22 17:38:11 +00001814 level, texImage->IntFormat, texImage );
1815 }
1816#endif
jtgafb833d1999-08-19 00:55:39 +00001817 }
1818}
1819
1820
1821
Brian Paulfbd8f211999-11-11 01:22:25 +00001822void
1823_mesa_TexSubImage3D( GLenum target, GLint level,
1824 GLint xoffset, GLint yoffset, GLint zoffset,
1825 GLsizei width, GLsizei height, GLsizei depth,
1826 GLenum format, GLenum type,
1827 const GLvoid *pixels )
jtgafb833d1999-08-19 00:55:39 +00001828{
Brian Paulfbd8f211999-11-11 01:22:25 +00001829 GET_CURRENT_CONTEXT(ctx);
Brian Paul02938782000-03-22 17:38:11 +00001830 struct gl_texture_unit *texUnit;
1831 struct gl_texture_object *texObj;
1832 struct gl_texture_image *texImage;
1833 GLboolean success = GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +00001834
Brian Paulc3f0a511999-11-03 17:27:05 +00001835 if (subtexture_error_check(ctx, 3, target, level, xoffset, yoffset, zoffset,
1836 width, height, depth, format, type)) {
Brian Paulf7b57072000-03-20 14:37:52 +00001837 return; /* error was detected */
jtgafb833d1999-08-19 00:55:39 +00001838 }
1839
Brian Paul02938782000-03-22 17:38:11 +00001840 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1841 texObj = texUnit->CurrentD[3];
1842 texImage = texObj->Image[level];
1843 assert(texImage);
jtgafb833d1999-08-19 00:55:39 +00001844
Brian Paulc3f0a511999-11-03 17:27:05 +00001845 if (width == 0 || height == 0 || height == 0 || !pixels)
1846 return; /* no-op, not an error */
jtgafb833d1999-08-19 00:55:39 +00001847
Brian Paul02938782000-03-22 17:38:11 +00001848 if (!ctx->Pixel.MapColorFlag && !ctx->Pixel.ScaleOrBiasRGBA
1849 && ctx->Driver.TexSubImage3D) {
1850 success = (*ctx->Driver.TexSubImage3D)( ctx, target, level, xoffset,
1851 yoffset, zoffset, width, height, depth, format,
1852 type, pixels, &ctx->Unpack, texObj, texImage );
1853 }
1854 if (!success) {
1855 /* XXX if Driver.TexSubImage3D, unpack image and try again? */
1856
1857 const GLint texComponents = components_in_intformat(texImage->Format);
1858 const GLenum texFormat = texImage->Format;
1859 const GLint xoffsetb = xoffset + texImage->Border;
1860 const GLint yoffsetb = yoffset + texImage->Border;
1861 const GLint zoffsetb = zoffset + texImage->Border;
1862 const GLint texWidth = texImage->Width;
1863 const GLint dstRectArea = texWidth * texImage->Height;
1864 const GLint srcStride = _mesa_image_row_stride(&ctx->Unpack,
1865 width, format, type);
1866 const GLint dstStride = texWidth * texComponents * sizeof(GLubyte);
1867 GLboolean retain = GL_TRUE;
jtgafb833d1999-08-19 00:55:39 +00001868
Brian Paulc3f0a511999-11-03 17:27:05 +00001869 if (texFormat == GL_COLOR_INDEX) {
1870 /* color index texture */
Brian Paulc3f0a511999-11-03 17:27:05 +00001871 GLint img, row;
1872 for (img = 0; img < depth; img++) {
Brian Paul02938782000-03-22 17:38:11 +00001873 const GLubyte *src = _mesa_image_address(&ctx->Unpack, pixels,
1874 width, height, format, type, img, 0, 0);
1875 GLubyte *dst = texImage->Data + ((zoffsetb + img) * dstRectArea
1876 + yoffsetb * texWidth + xoffsetb) * texComponents;
Brian Paulc3f0a511999-11-03 17:27:05 +00001877 for (row = 0; row < height; row++) {
Brian Paulc3f0a511999-11-03 17:27:05 +00001878 _mesa_unpack_index_span(ctx, width, GL_UNSIGNED_BYTE, dst,
Brian Paul02938782000-03-22 17:38:11 +00001879 type, (const GLvoid *) src, &ctx->Unpack, GL_TRUE);
1880 src += srcStride;
1881 dst += dstStride;
Brian Paulc3f0a511999-11-03 17:27:05 +00001882 }
jtgafb833d1999-08-19 00:55:39 +00001883 }
1884 }
1885 else {
Brian Paulc3f0a511999-11-03 17:27:05 +00001886 /* color texture */
Brian Paulc3f0a511999-11-03 17:27:05 +00001887 GLint img, row;
1888 for (img = 0; img < depth; img++) {
Brian Paul02938782000-03-22 17:38:11 +00001889 const GLubyte *src = _mesa_image_address(&ctx->Unpack, pixels,
1890 width, height, format, type, img, 0, 0);
1891 GLubyte *dst = texImage->Data + ((zoffsetb + img) * dstRectArea
1892 + yoffsetb * texWidth + xoffsetb) * texComponents;
Brian Paulc3f0a511999-11-03 17:27:05 +00001893 for (row = 0; row < height; row++) {
Brian Paulc3f0a511999-11-03 17:27:05 +00001894 _mesa_unpack_ubyte_color_span(ctx, width, texFormat, dst,
Brian Paul02938782000-03-22 17:38:11 +00001895 format, type, (const GLvoid *) src, &ctx->Unpack, GL_TRUE);
1896 src += srcStride;
1897 dst += dstStride;
Brian Paulc3f0a511999-11-03 17:27:05 +00001898 }
jtgafb833d1999-08-19 00:55:39 +00001899 }
jtgafb833d1999-08-19 00:55:39 +00001900 }
Brian Paul02938782000-03-22 17:38:11 +00001901
1902 if (ctx->Driver.TexImage3D) {
1903 (*ctx->Driver.TexImage3D)(ctx, target, level, texImage->Format,
1904 GL_UNSIGNED_BYTE, texImage->Data,
1905 &_mesa_native_packing, texObj, texImage,
1906 &retain);
1907 }
1908
1909 if (!retain && texImage->Data) {
1910 FREE(texImage->Data);
1911 texImage->Data = NULL;
1912 }
jtgafb833d1999-08-19 00:55:39 +00001913 }
jtgafb833d1999-08-19 00:55:39 +00001914}
1915
1916
1917
1918/*
1919 * Read an RGBA image from the frame buffer.
Brian Paulc3f0a511999-11-03 17:27:05 +00001920 * This is used by glCopyTexSubImage[12]D().
jtgafb833d1999-08-19 00:55:39 +00001921 * Input: ctx - the context
1922 * x, y - lower left corner
1923 * width, height - size of region to read
Brian Paulc3f0a511999-11-03 17:27:05 +00001924 * Return: pointer to block of GL_RGBA, GLubyte data.
jtgafb833d1999-08-19 00:55:39 +00001925 */
Brian Paulc3f0a511999-11-03 17:27:05 +00001926static GLubyte *
1927read_color_image( GLcontext *ctx, GLint x, GLint y,
1928 GLsizei width, GLsizei height )
jtgafb833d1999-08-19 00:55:39 +00001929{
Brian Paulc3f0a511999-11-03 17:27:05 +00001930 GLint stride, i;
1931 GLubyte *image, *dst;
jtgafb833d1999-08-19 00:55:39 +00001932
Brian Paul959f8022000-03-19 01:10:11 +00001933 image = (GLubyte *) MALLOC(width * height * 4 * sizeof(GLubyte));
Brian Paulc3f0a511999-11-03 17:27:05 +00001934 if (!image)
jtgafb833d1999-08-19 00:55:39 +00001935 return NULL;
jtgafb833d1999-08-19 00:55:39 +00001936
1937 /* Select buffer to read from */
Brian Paulcea0e8e1999-11-25 17:36:48 +00001938 (*ctx->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
1939 ctx->Pixel.DriverReadBuffer );
jtgafb833d1999-08-19 00:55:39 +00001940
Brian Paulc3f0a511999-11-03 17:27:05 +00001941 dst = image;
1942 stride = width * 4 * sizeof(GLubyte);
1943 for (i = 0; i < height; i++) {
Brian Paul3f02f901999-11-24 18:48:30 +00001944 gl_read_rgba_span( ctx, ctx->ReadBuffer, width, x, y + i,
1945 (GLubyte (*)[4]) dst );
Brian Paulc3f0a511999-11-03 17:27:05 +00001946 dst += stride;
1947 }
jtgafb833d1999-08-19 00:55:39 +00001948
Brian Paulcea0e8e1999-11-25 17:36:48 +00001949 /* Read from draw buffer (the default) */
1950 (*ctx->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
1951 ctx->Color.DriverDrawBuffer );
jtgafb833d1999-08-19 00:55:39 +00001952
1953 return image;
1954}
1955
1956
1957
Brian Paulfbd8f211999-11-11 01:22:25 +00001958void
1959_mesa_CopyTexImage1D( GLenum target, GLint level,
1960 GLenum internalFormat,
1961 GLint x, GLint y,
1962 GLsizei width, GLint border )
jtgafb833d1999-08-19 00:55:39 +00001963{
Brian Paulfbd8f211999-11-11 01:22:25 +00001964 GET_CURRENT_CONTEXT(ctx);
jtgafb833d1999-08-19 00:55:39 +00001965 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glCopyTexImage1D");
jtgafb833d1999-08-19 00:55:39 +00001966
Brian Paulf7b57072000-03-20 14:37:52 +00001967 if (copytexture_error_check(ctx, 1, target, level, internalFormat,
1968 width, 1, border))
1969 return;
1970
1971 if (ctx->Pixel.MapColorFlag || ctx->Pixel.ScaleOrBiasRGBA
1972 || !ctx->Driver.CopyTexImage1D
1973 || !(*ctx->Driver.CopyTexImage1D)(ctx, target, level,
1974 internalFormat, x, y, width, border))
1975 {
Brian Paulc3f0a511999-11-03 17:27:05 +00001976 GLubyte *image = read_color_image( ctx, x, y, width, 1 );
1977 if (!image) {
1978 gl_error( ctx, GL_OUT_OF_MEMORY, "glCopyTexImage1D" );
1979 return;
1980 }
Brian Paul3ab6bbe2000-02-12 17:26:15 +00001981 (*ctx->Exec->TexImage1D)( target, level, internalFormat, width,
Brian Paulf7b57072000-03-20 14:37:52 +00001982 border, GL_RGBA, GL_UNSIGNED_BYTE, image );
Brian Paulc3f0a511999-11-03 17:27:05 +00001983 FREE(image);
jtgafb833d1999-08-19 00:55:39 +00001984 }
jtgafb833d1999-08-19 00:55:39 +00001985}
1986
1987
1988
Brian Paulfbd8f211999-11-11 01:22:25 +00001989void
1990_mesa_CopyTexImage2D( GLenum target, GLint level, GLenum internalFormat,
1991 GLint x, GLint y, GLsizei width, GLsizei height,
1992 GLint border )
jtgafb833d1999-08-19 00:55:39 +00001993{
Brian Paulfbd8f211999-11-11 01:22:25 +00001994 GET_CURRENT_CONTEXT(ctx);
jtgafb833d1999-08-19 00:55:39 +00001995 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glCopyTexImage2D");
jtgafb833d1999-08-19 00:55:39 +00001996
Brian Paulf7b57072000-03-20 14:37:52 +00001997 if (copytexture_error_check(ctx, 2, target, level, internalFormat,
1998 width, height, border))
1999 return;
2000
2001 if (ctx->Pixel.MapColorFlag || ctx->Pixel.ScaleOrBiasRGBA
2002 || !ctx->Driver.CopyTexImage2D
2003 || !(*ctx->Driver.CopyTexImage2D)(ctx, target, level,
2004 internalFormat, x, y, width, height, border))
2005 {
Brian Paulc3f0a511999-11-03 17:27:05 +00002006 GLubyte *image = read_color_image( ctx, x, y, width, height );
2007 if (!image) {
2008 gl_error( ctx, GL_OUT_OF_MEMORY, "glCopyTexImage2D" );
2009 return;
2010 }
Brian Paul3ab6bbe2000-02-12 17:26:15 +00002011 (ctx->Exec->TexImage2D)( target, level, internalFormat, width,
Brian Paulf7b57072000-03-20 14:37:52 +00002012 height, border, GL_RGBA, GL_UNSIGNED_BYTE, image );
Brian Paulc3f0a511999-11-03 17:27:05 +00002013 FREE(image);
jtgafb833d1999-08-19 00:55:39 +00002014 }
jtgafb833d1999-08-19 00:55:39 +00002015}
2016
2017
2018
jtgafb833d1999-08-19 00:55:39 +00002019/*
2020 * Do the work of glCopyTexSubImage[123]D.
jtgafb833d1999-08-19 00:55:39 +00002021 */
Brian Paulc3f0a511999-11-03 17:27:05 +00002022static void
2023copy_tex_sub_image( GLcontext *ctx, struct gl_texture_image *dest,
2024 GLint width, GLint height,
2025 GLint srcx, GLint srcy,
2026 GLint dstx, GLint dsty, GLint dstz )
jtgafb833d1999-08-19 00:55:39 +00002027{
Brian Paulc3f0a511999-11-03 17:27:05 +00002028 GLint i;
jtgafb833d1999-08-19 00:55:39 +00002029 GLint format, components, rectarea;
Brian Paul91baaa31999-10-17 23:24:16 +00002030 GLint texwidth, texheight, zoffset;
jtgafb833d1999-08-19 00:55:39 +00002031
Brian Paul91baaa31999-10-17 23:24:16 +00002032 /* dst[xyz] may be negative if we have a texture border! */
2033 dstx += dest->Border;
2034 dsty += dest->Border;
2035 dstz += dest->Border;
jtgafb833d1999-08-19 00:55:39 +00002036 texwidth = dest->Width;
2037 texheight = dest->Height;
2038 rectarea = texwidth * texheight;
Brian Paul91baaa31999-10-17 23:24:16 +00002039 zoffset = dstz * rectarea;
jtgafb833d1999-08-19 00:55:39 +00002040 format = dest->Format;
2041 components = components_in_intformat( format );
2042
2043 /* Select buffer to read from */
Brian Paulcea0e8e1999-11-25 17:36:48 +00002044 (*ctx->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
2045 ctx->Pixel.DriverReadBuffer );
jtgafb833d1999-08-19 00:55:39 +00002046
Brian Paulc3f0a511999-11-03 17:27:05 +00002047 for (i = 0;i < height; i++) {
jtgafb833d1999-08-19 00:55:39 +00002048 GLubyte rgba[MAX_WIDTH][4];
Brian Paulc3f0a511999-11-03 17:27:05 +00002049 GLubyte *dst;
Brian Paul3f02f901999-11-24 18:48:30 +00002050 gl_read_rgba_span( ctx, ctx->ReadBuffer, width, srcx, srcy + i, rgba );
Brian Paulc3f0a511999-11-03 17:27:05 +00002051 dst = dest->Data + ( zoffset + (dsty+i) * texwidth + dstx) * components;
2052 _mesa_unpack_ubyte_color_span(ctx, width, format, dst,
2053 GL_RGBA, GL_UNSIGNED_BYTE, rgba,
Brian Paul02938782000-03-22 17:38:11 +00002054 &_mesa_native_packing, GL_TRUE);
Brian Paulc3f0a511999-11-03 17:27:05 +00002055 }
jtgafb833d1999-08-19 00:55:39 +00002056
Brian Paulcea0e8e1999-11-25 17:36:48 +00002057 /* Read from draw buffer (the default) */
2058 (*ctx->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
2059 ctx->Color.DriverDrawBuffer );
jtgafb833d1999-08-19 00:55:39 +00002060}
2061
2062
2063
2064
Brian Paulfbd8f211999-11-11 01:22:25 +00002065void
2066_mesa_CopyTexSubImage1D( GLenum target, GLint level,
2067 GLint xoffset, GLint x, GLint y, GLsizei width )
jtgafb833d1999-08-19 00:55:39 +00002068{
Brian Paulfbd8f211999-11-11 01:22:25 +00002069 GET_CURRENT_CONTEXT(ctx);
jtgafb833d1999-08-19 00:55:39 +00002070 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glCopyTexSubImage1D");
jtgafb833d1999-08-19 00:55:39 +00002071
Brian Paulf7b57072000-03-20 14:37:52 +00002072 if (copytexsubimage_error_check(ctx, 1, target, level,
2073 xoffset, 0, 0, width, 1))
2074 return;
2075
2076 if (ctx->Pixel.MapColorFlag || ctx->Pixel.ScaleOrBiasRGBA
2077 || !ctx->Driver.CopyTexSubImage1D
2078 || !(*ctx->Driver.CopyTexSubImage1D)(ctx, target, level,
2079 xoffset, x, y, width)) {
Brian Paulc3f0a511999-11-03 17:27:05 +00002080 struct gl_texture_unit *texUnit;
2081 struct gl_texture_image *teximage;
2082 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2083 teximage = texUnit->CurrentD[1]->Image[level];
2084 assert(teximage);
jtgafb833d1999-08-19 00:55:39 +00002085 if (teximage->Data) {
Brian Paulc3f0a511999-11-03 17:27:05 +00002086 copy_tex_sub_image(ctx, teximage, width, 1, x, y, xoffset, 0, 0);
Brian Paulf7b57072000-03-20 14:37:52 +00002087 /* tell driver about the change */
Brian Paul9fd2b0a2000-03-24 23:59:06 +00002088 /* XXX this is obsolete */
Brian Paulf7b57072000-03-20 14:37:52 +00002089 if (ctx->Driver.TexImage) {
2090 (*ctx->Driver.TexImage)( ctx, GL_TEXTURE_1D,
2091 texUnit->CurrentD[1],
2092 level, teximage->IntFormat, teximage );
2093 }
jtgafb833d1999-08-19 00:55:39 +00002094 }
2095 }
Brian Paulc3f0a511999-11-03 17:27:05 +00002096}
2097
2098
2099
Brian Paulfbd8f211999-11-11 01:22:25 +00002100void
2101_mesa_CopyTexSubImage2D( GLenum target, GLint level,
2102 GLint xoffset, GLint yoffset,
2103 GLint x, GLint y, GLsizei width, GLsizei height )
Brian Paulc3f0a511999-11-03 17:27:05 +00002104{
Brian Paulfbd8f211999-11-11 01:22:25 +00002105 GET_CURRENT_CONTEXT(ctx);
Brian Paulc3f0a511999-11-03 17:27:05 +00002106 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glCopyTexSubImage2D");
2107
Brian Paulf7b57072000-03-20 14:37:52 +00002108 if (copytexsubimage_error_check(ctx, 2, target, level,
2109 xoffset, yoffset, 0, width, height))
2110 return;
2111
2112 if (ctx->Pixel.MapColorFlag || ctx->Pixel.ScaleOrBiasRGBA
2113 || !ctx->Driver.CopyTexSubImage2D
2114 || !(*ctx->Driver.CopyTexSubImage2D)(ctx, target, level,
2115 xoffset, yoffset, x, y, width, height )) {
Brian Paulc3f0a511999-11-03 17:27:05 +00002116 struct gl_texture_unit *texUnit;
2117 struct gl_texture_image *teximage;
2118 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2119 teximage = texUnit->CurrentD[2]->Image[level];
2120 assert(teximage);
2121 if (teximage->Data) {
2122 copy_tex_sub_image(ctx, teximage, width, height,
2123 x, y, xoffset, yoffset, 0);
Brian Paulf7b57072000-03-20 14:37:52 +00002124 /* tell driver about the change */
Brian Paul9fd2b0a2000-03-24 23:59:06 +00002125 /* XXX this is obsolete */
Brian Paulf7b57072000-03-20 14:37:52 +00002126 if (ctx->Driver.TexImage) {
2127 (*ctx->Driver.TexImage)( ctx, GL_TEXTURE_2D,
2128 texUnit->CurrentD[2],
2129 level, teximage->IntFormat, teximage );
Brian Paulc3f0a511999-11-03 17:27:05 +00002130 }
2131 }
2132 }
2133}
2134
2135
2136
Brian Paulfbd8f211999-11-11 01:22:25 +00002137void
2138_mesa_CopyTexSubImage3D( GLenum target, GLint level,
2139 GLint xoffset, GLint yoffset, GLint zoffset,
2140 GLint x, GLint y, GLsizei width, GLsizei height )
Brian Paulc3f0a511999-11-03 17:27:05 +00002141{
Brian Paulfbd8f211999-11-11 01:22:25 +00002142 GET_CURRENT_CONTEXT(ctx);
Brian Paulc3f0a511999-11-03 17:27:05 +00002143 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glCopyTexSubImage3D");
2144
Brian Paulf7b57072000-03-20 14:37:52 +00002145 if (copytexsubimage_error_check(ctx, 3, target, level,
2146 xoffset, yoffset, zoffset, width, height))
2147 return;
2148
Brian Paul02938782000-03-22 17:38:11 +00002149 if (ctx->Pixel.MapColorFlag || ctx->Pixel.ScaleOrBiasRGBA
Brian Paulf7b57072000-03-20 14:37:52 +00002150 || !ctx->Driver.CopyTexSubImage3D
2151 || !(*ctx->Driver.CopyTexSubImage3D)(ctx, target, level,
Brian Paul02938782000-03-22 17:38:11 +00002152 xoffset, yoffset, zoffset, x, y, width, height )) {
2153 struct gl_texture_unit *texUnit;
2154 struct gl_texture_image *teximage;
2155 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
2156 teximage = texUnit->CurrentD[3]->Image[level];
2157 assert(teximage);
2158 if (teximage->Data) {
2159 copy_tex_sub_image(ctx, teximage, width, height,
Brian Paulc3f0a511999-11-03 17:27:05 +00002160 x, y, xoffset, yoffset, zoffset);
Brian Paul02938782000-03-22 17:38:11 +00002161 /* tell driver about the change */
Brian Paul9fd2b0a2000-03-24 23:59:06 +00002162 /* XXX this is obsolete */
Brian Paul02938782000-03-22 17:38:11 +00002163 if (ctx->Driver.TexImage) {
2164 (*ctx->Driver.TexImage)( ctx, GL_TEXTURE_3D,
2165 texUnit->CurrentD[3],
2166 level, teximage->IntFormat, teximage );
Brian Paulf7b57072000-03-20 14:37:52 +00002167 }
Brian Paulc3f0a511999-11-03 17:27:05 +00002168 }
jtgafb833d1999-08-19 00:55:39 +00002169 }
2170}