| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Mesa 3-D graphics library |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 4 | * Version: 3.3 |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 5 | * |
| Brian Paul | 663049a | 2000-01-31 23:10:16 +0000 | [diff] [blame] | 6 | * Copyright (C) 1999-2000 Brian Paul All Rights Reserved. |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 7 | * |
| 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 Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 30 | #include "glheader.h" |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 31 | #include "context.h" |
| 32 | #include "image.h" |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 33 | #include "mem.h" |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 34 | #include "mmath.h" |
| 35 | #include "span.h" |
| 36 | #include "teximage.h" |
| 37 | #include "texstate.h" |
| 38 | #include "types.h" |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 39 | #endif |
| 40 | |
| 41 | |
| 42 | /* |
| 43 | * NOTES: |
| 44 | * |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 45 | * 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. |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 49 | */ |
| 50 | |
| 51 | |
| Brian Paul | 4827179 | 2000-03-29 18:13:59 +0000 | [diff] [blame] | 52 | #ifdef DEBUG |
| 53 | static 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 Paul | f7b5707 | 2000-03-20 14:37:52 +0000 | [diff] [blame] | 75 | /* |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 76 | * 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 Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 80 | static int |
| 81 | logbase2( int n ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 82 | { |
| 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 Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 107 | * GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, or GL_RGBA. |
| 108 | * Return -1 if invalid enum. |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 109 | */ |
| Brian Paul | b132e8d | 2000-03-23 16:23:14 +0000 | [diff] [blame] | 110 | GLint |
| 111 | _mesa_base_tex_format( GLint format ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 112 | { |
| 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 Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 183 | static GLint |
| 184 | components_in_intformat( GLint format ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 185 | { |
| 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 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 250 | /* |
| 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 Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 256 | static void |
| 257 | set_teximage_component_sizes( struct gl_texture_image *texImage ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 258 | { |
| 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 Paul | 91baaa3 | 1999-10-17 23:24:16 +0000 | [diff] [blame] | 296 | 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; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 323 | case GL_RGB: |
| Brian Paul | d53573d | 1999-10-19 20:36:20 +0000 | [diff] [blame] | 324 | case GL_BGR: |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 325 | 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 Paul | d53573d | 1999-10-19 20:36:20 +0000 | [diff] [blame] | 334 | case GL_BGRA: |
| 335 | case GL_ABGR_EXT: |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 336 | 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 Paul | 77ce6da | 2000-03-20 23:40:12 +0000 | [diff] [blame] | 359 | |
| 360 | /* |
| 361 | * Return new gl_texture_image struct with all fields initialized to zero. |
| 362 | */ |
| 363 | struct gl_texture_image * |
| Brian Paul | 021a525 | 2000-03-27 17:54:17 +0000 | [diff] [blame] | 364 | _mesa_alloc_texture_image( void ) |
| Brian Paul | 77ce6da | 2000-03-20 23:40:12 +0000 | [diff] [blame] | 365 | { |
| 366 | return CALLOC_STRUCT(gl_texture_image); |
| 367 | } |
| 368 | |
| 369 | |
| 370 | |
| 371 | /* |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 372 | * Initialize most fields of a gl_texture_image struct. |
| Brian Paul | 77ce6da | 2000-03-20 23:40:12 +0000 | [diff] [blame] | 373 | */ |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 374 | static void |
| 375 | init_texture_image( struct gl_texture_image *img, |
| 376 | GLsizei width, GLsizei height, GLsizei depth, |
| 377 | GLint border, GLenum internalFormat ) |
| Brian Paul | 77ce6da | 2000-03-20 23:40:12 +0000 | [diff] [blame] | 378 | { |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 379 | ASSERT(img); |
| 380 | ASSERT(!img->Data); |
| Brian Paul | b132e8d | 2000-03-23 16:23:14 +0000 | [diff] [blame] | 381 | img->Format = (GLenum) _mesa_base_tex_format(internalFormat); |
| Brian Paul | 77ce6da | 2000-03-20 23:40:12 +0000 | [diff] [blame] | 382 | 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 Paul | 77ce6da | 2000-03-20 23:40:12 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | |
| 404 | |
| 405 | void |
| Brian Paul | 021a525 | 2000-03-27 17:54:17 +0000 | [diff] [blame] | 406 | _mesa_free_texture_image( struct gl_texture_image *teximage ) |
| Brian Paul | 77ce6da | 2000-03-20 23:40:12 +0000 | [diff] [blame] | 407 | { |
| 408 | if (teximage->Data) { |
| 409 | FREE( teximage->Data ); |
| 410 | teximage->Data = NULL; |
| 411 | } |
| 412 | FREE( teximage ); |
| 413 | } |
| 414 | |
| 415 | |
| 416 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 417 | /* 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 Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 427 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 428 | /* |
| Brian Paul | 43911c8 | 2000-03-21 00:49:33 +0000 | [diff] [blame] | 429 | * 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. |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 432 | */ |
| Brian Paul | 43911c8 | 2000-03-21 00:49:33 +0000 | [diff] [blame] | 433 | static void |
| 434 | make_texture_image( GLcontext *ctx, |
| 435 | struct gl_texture_image *texImage, |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 436 | GLenum srcFormat, GLenum srcType, const GLvoid *pixels, |
| 437 | const struct gl_pixelstore_attrib *unpacking) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 438 | { |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 439 | GLint components, numPixels; |
| Brian Paul | 43911c8 | 2000-03-21 00:49:33 +0000 | [diff] [blame] | 440 | GLint internalFormat, width, height, depth, border; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 441 | |
| Brian Paul | 43911c8 | 2000-03-21 00:49:33 +0000 | [diff] [blame] | 442 | ASSERT(ctx); |
| 443 | ASSERT(texImage); |
| 444 | ASSERT(!texImage->Data); |
| 445 | ASSERT(pixels); |
| 446 | ASSERT(unpacking); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 447 | |
| Brian Paul | 43911c8 | 2000-03-21 00:49:33 +0000 | [diff] [blame] | 448 | internalFormat = texImage->IntFormat; |
| 449 | width = texImage->Width; |
| 450 | height = texImage->Height; |
| 451 | depth = texImage->Depth; |
| 452 | border = texImage->Border; |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 453 | components = components_in_intformat(internalFormat); |
| Brian Paul | 43911c8 | 2000-03-21 00:49:33 +0000 | [diff] [blame] | 454 | |
| 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 Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 464 | |
| 465 | texImage->Data = (GLubyte *) MALLOC(numPixels * components + EXTRA_BYTE); |
| Brian Paul | 43911c8 | 2000-03-21 00:49:33 +0000 | [diff] [blame] | 466 | if (!texImage->Data) |
| 467 | return; /* out of memory */ |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 468 | |
| 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 Paul | 43911c8 | 2000-03-21 00:49:33 +0000 | [diff] [blame] | 481 | 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 Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 486 | /* This will cover the common GL_RGB, GL_RGBA, GL_ALPHA, |
| 487 | * GL_LUMINANCE_ALPHA, etc. texture formats. |
| 488 | */ |
| Brian Paul | b7d076f | 2000-03-21 01:03:40 +0000 | [diff] [blame] | 489 | const GLubyte *src = (const GLubyte *) _mesa_image_address( |
| Brian Paul | 43911c8 | 2000-03-21 00:49:33 +0000 | [diff] [blame] | 490 | unpacking, pixels, width, height, srcFormat, srcType, 0, 0, 0); |
| 491 | const GLint srcStride = _mesa_image_row_stride(unpacking, width, |
| 492 | srcFormat, srcType); |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 493 | 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 Paul | 43911c8 | 2000-03-21 00:49:33 +0000 | [diff] [blame] | 506 | return; /* all done */ |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 507 | } |
| 508 | else if (srcFormat == GL_RGBA && internalFormat == GL_RGB) { |
| 509 | /* commonly used by Quake */ |
| Brian Paul | b7d076f | 2000-03-21 01:03:40 +0000 | [diff] [blame] | 510 | const GLubyte *src = (const GLubyte *) _mesa_image_address( |
| Brian Paul | 43911c8 | 2000-03-21 00:49:33 +0000 | [diff] [blame] | 511 | unpacking, pixels, width, height, srcFormat, srcType, 0, 0, 0); |
| 512 | const GLint srcStride = _mesa_image_row_stride(unpacking, width, |
| 513 | srcFormat, srcType); |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 514 | 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 Paul | 43911c8 | 2000-03-21 00:49:33 +0000 | [diff] [blame] | 526 | return; /* all done */ |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 527 | } |
| 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 Paul | b7d076f | 2000-03-21 01:03:40 +0000 | [diff] [blame] | 542 | const GLvoid *source = _mesa_image_address(unpacking, |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 543 | 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 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 549 | } |
| 550 | else { |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 551 | /* 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 Paul | b7d076f | 2000-03-21 01:03:40 +0000 | [diff] [blame] | 558 | const GLvoid *source = _mesa_image_address(unpacking, |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 559 | 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 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 565 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 566 | } |
| 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 Paul | 43911c8 | 2000-03-21 00:49:33 +0000 | [diff] [blame] | 573 | * spec. This function creates an empty image for the given texture image. |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 574 | */ |
| Brian Paul | 43911c8 | 2000-03-21 00:49:33 +0000 | [diff] [blame] | 575 | static void |
| 576 | make_null_texture( struct gl_texture_image *texImage ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 577 | { |
| 578 | GLint components; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 579 | GLint numPixels; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 580 | |
| Brian Paul | 43911c8 | 2000-03-21 00:49:33 +0000 | [diff] [blame] | 581 | ASSERT(texImage); |
| 582 | ASSERT(!texImage->Data); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 583 | |
| Brian Paul | 43911c8 | 2000-03-21 00:49:33 +0000 | [diff] [blame] | 584 | components = components_in_intformat(texImage->IntFormat); |
| 585 | numPixels = texImage->Width * texImage->Height * texImage->Depth; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 586 | |
| Brian Paul | bd5cdaf | 1999-10-13 18:42:49 +0000 | [diff] [blame] | 587 | texImage->Data = (GLubyte *) MALLOC( numPixels * components + EXTRA_BYTE ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 588 | |
| 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 Paul | 65d5460 | 2000-03-01 23:28:20 +0000 | [diff] [blame] | 595 | static const char message[8][32] = { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 596 | " 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 Paul | 43911c8 | 2000-03-21 00:49:33 +0000 | [diff] [blame] | 608 | for (i = 0; i < texImage->Height; i++) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 609 | GLint srcRow = 7 - i % 8; |
| Brian Paul | 43911c8 | 2000-03-21 00:49:33 +0000 | [diff] [blame] | 610 | for (j = 0; j < texImage->Width; j++) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 611 | GLint srcCol = j % 32; |
| Brian Paul | 5b37c32 | 1999-11-05 06:43:10 +0000 | [diff] [blame] | 612 | GLint texel = (message[srcRow][srcCol]=='X') ? 255 : 70; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 613 | for (k=0;k<components;k++) { |
| Brian Paul | 5b37c32 | 1999-11-05 06:43:10 +0000 | [diff] [blame] | 614 | *imgPtr++ = (GLubyte) texel; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 615 | } |
| 616 | } |
| 617 | } |
| 618 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | |
| 622 | |
| 623 | /* |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 624 | * Test glTexImage[123]D() parameters for errors. |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 625 | * Input: |
| 626 | * dimensions - must be 1 or 2 or 3 |
| 627 | * Return: GL_TRUE = an error was detected, GL_FALSE = no errors |
| 628 | */ |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 629 | static GLboolean |
| 630 | texture_error_check( GLcontext *ctx, GLenum target, |
| 631 | GLint level, GLint internalFormat, |
| 632 | GLenum format, GLenum type, |
| Brian Paul | 5b37c32 | 1999-11-05 06:43:10 +0000 | [diff] [blame] | 633 | GLuint dimensions, |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 634 | GLint width, GLint height, |
| 635 | GLint depth, GLint border ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 636 | { |
| 637 | GLboolean isProxy; |
| 638 | GLint iformat; |
| 639 | |
| 640 | if (dimensions == 1) { |
| Brian Paul | 5b37c32 | 1999-11-05 06:43:10 +0000 | [diff] [blame] | 641 | isProxy = (GLboolean) (target == GL_PROXY_TEXTURE_1D); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 642 | 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 Paul | 5b37c32 | 1999-11-05 06:43:10 +0000 | [diff] [blame] | 648 | isProxy = (GLboolean) (target == GL_PROXY_TEXTURE_2D); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 649 | 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 Paul | 5b37c32 | 1999-11-05 06:43:10 +0000 | [diff] [blame] | 655 | isProxy = (GLboolean) (target == GL_PROXY_TEXTURE_3D); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 656 | 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 Paul | 9fd2b0a | 2000-03-24 23:59:06 +0000 | [diff] [blame] | 667 | if (border != 0 && border != 1) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 668 | if (!isProxy) { |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 669 | char message[100]; |
| 670 | sprintf(message, "glTexImage%dD(border)", dimensions); |
| 671 | gl_error(ctx, GL_INVALID_VALUE, message); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 672 | } |
| 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 Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 680 | char message[100]; |
| 681 | sprintf(message, "glTexImage%dD(width)", dimensions); |
| 682 | gl_error(ctx, GL_INVALID_VALUE, message); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 683 | } |
| 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 Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 692 | char message[100]; |
| 693 | sprintf(message, "glTexImage%dD(height)", dimensions); |
| 694 | gl_error(ctx, GL_INVALID_VALUE, message); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 695 | } |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 696 | return GL_TRUE; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 697 | } |
| 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 Paul | 9fd2b0a | 2000-03-24 23:59:06 +0000 | [diff] [blame] | 712 | if (level < 0 || level >= ctx->Const.MaxTextureLevels) { |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 713 | if (!isProxy) { |
| 714 | char message[100]; |
| 715 | sprintf(message, "glTexImage%dD(level)", dimensions); |
| 716 | gl_error(ctx, GL_INVALID_VALUE, message); |
| 717 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 718 | return GL_TRUE; |
| 719 | } |
| 720 | |
| Brian Paul | b132e8d | 2000-03-23 16:23:14 +0000 | [diff] [blame] | 721 | iformat = _mesa_base_tex_format( internalFormat ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 722 | if (iformat < 0) { |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 723 | if (!isProxy) { |
| 724 | char message[100]; |
| 725 | sprintf(message, "glTexImage%dD(internalFormat)", dimensions); |
| 726 | gl_error(ctx, GL_INVALID_VALUE, message); |
| 727 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 728 | return GL_TRUE; |
| 729 | } |
| 730 | |
| Brian Paul | b7d076f | 2000-03-21 01:03:40 +0000 | [diff] [blame] | 731 | if (!_mesa_is_legal_format_and_type( format, type )) { |
| Brian Paul | d53573d | 1999-10-19 20:36:20 +0000 | [diff] [blame] | 732 | /* 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 Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 735 | if (!isProxy) { |
| 736 | char message[100]; |
| 737 | sprintf(message, "glTexImage%dD(format or type)", dimensions); |
| 738 | gl_error(ctx, GL_INVALID_OPERATION, message); |
| 739 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 740 | return GL_TRUE; |
| 741 | } |
| 742 | |
| 743 | /* if we get here, the parameters are OK */ |
| 744 | return GL_FALSE; |
| 745 | } |
| 746 | |
| 747 | |
| 748 | |
| 749 | /* |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 750 | * 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 | */ |
| 755 | static GLboolean |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 756 | subtexture_error_check( GLcontext *ctx, GLuint dimensions, |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 757 | 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 Paul | b7d076f | 2000-03-21 01:03:40 +0000 | [diff] [blame] | 847 | if (!_mesa_is_legal_format_and_type(format, type)) { |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 848 | 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 | */ |
| 863 | static GLboolean |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 864 | copytexture_error_check( GLcontext *ctx, GLuint dimensions, |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 865 | 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 Paul | b132e8d | 2000-03-23 16:23:14 +0000 | [diff] [blame] | 920 | iformat = _mesa_base_tex_format( internalFormat ); |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 921 | 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 | |
| 933 | static GLboolean |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 934 | copytexsubimage_error_check( GLcontext *ctx, GLuint dimensions, |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 935 | GLenum target, GLint level, |
| 936 | GLint xoffset, GLint yoffset, GLint zoffset, |
| Brian Paul | 5b37c32 | 1999-11-05 06:43:10 +0000 | [diff] [blame] | 937 | GLsizei width, GLsizei height ) |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 938 | { |
| 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 Paul | df6a28d | 2000-02-21 16:34:21 +0000 | [diff] [blame] | 975 | teximage = texUnit->CurrentD[dimensions]->Image[level]; |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 976 | 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 | /* |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1034 | * Called from the API. Note that width includes the border. |
| 1035 | */ |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 1036 | void |
| Brian Paul | 43911c8 | 2000-03-21 00:49:33 +0000 | [diff] [blame] | 1037 | _mesa_TexImage1D( GLenum target, GLint level, GLint internalFormat, |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 1038 | GLsizei width, GLint border, GLenum format, |
| 1039 | GLenum type, const GLvoid *pixels ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1040 | { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 1041 | GET_CURRENT_CONTEXT(ctx); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1042 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glTexImage1D"); |
| 1043 | |
| 1044 | if (target==GL_TEXTURE_1D) { |
| Brian Paul | f7b5707 | 2000-03-20 14:37:52 +0000 | [diff] [blame] | 1045 | struct gl_texture_unit *texUnit; |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1046 | struct gl_texture_object *texObj; |
| 1047 | struct gl_texture_image *texImage; |
| Brian Paul | f7b5707 | 2000-03-20 14:37:52 +0000 | [diff] [blame] | 1048 | |
| Brian Paul | 43911c8 | 2000-03-21 00:49:33 +0000 | [diff] [blame] | 1049 | if (texture_error_check( ctx, target, level, internalFormat, |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1050 | format, type, 1, width, 1, 1, border )) { |
| Brian Paul | f7b5707 | 2000-03-20 14:37:52 +0000 | [diff] [blame] | 1051 | return; /* error in texture image was detected */ |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
| Brian Paul | f7b5707 | 2000-03-20 14:37:52 +0000 | [diff] [blame] | 1054 | texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit]; |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1055 | texObj = texUnit->CurrentD[1]; |
| 1056 | texImage = texObj->Image[level]; |
| Brian Paul | f7b5707 | 2000-03-20 14:37:52 +0000 | [diff] [blame] | 1057 | |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1058 | if (!texImage) { |
| Brian Paul | 021a525 | 2000-03-27 17:54:17 +0000 | [diff] [blame] | 1059 | texImage = _mesa_alloc_texture_image(); |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1060 | 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; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1071 | /* setup the teximage struct's fields */ |
| 1072 | init_texture_image(texImage, width, 1, 1, border, internalFormat); |
| Brian Paul | 43911c8 | 2000-03-21 00:49:33 +0000 | [diff] [blame] | 1073 | |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1074 | /* process the texture image */ |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 1075 | if (pixels) { |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1076 | 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 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1101 | } |
| 1102 | else { |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1103 | 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 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1113 | /* state update */ |
| 1114 | gl_put_texobj_on_dirty_list( ctx, texObj ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1115 | ctx->NewState |= NEW_TEXTURING; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1116 | } |
| 1117 | else if (target==GL_PROXY_TEXTURE_1D) { |
| 1118 | /* Proxy texture: check for errors and update proxy state */ |
| Brian Paul | 43911c8 | 2000-03-21 00:49:33 +0000 | [diff] [blame] | 1119 | if (texture_error_check( ctx, target, level, internalFormat, |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1120 | 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 Paul | 43911c8 | 2000-03-21 00:49:33 +0000 | [diff] [blame] | 1129 | ctx->Texture.Proxy1D->Image[level]->IntFormat = (GLenum) internalFormat; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1130 | 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 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1135 | } |
| 1136 | else { |
| 1137 | gl_error( ctx, GL_INVALID_ENUM, "glTexImage1D(target)" ); |
| 1138 | return; |
| 1139 | } |
| 1140 | } |
| 1141 | |
| 1142 | |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 1143 | void |
| Brian Paul | 43911c8 | 2000-03-21 00:49:33 +0000 | [diff] [blame] | 1144 | _mesa_TexImage2D( GLenum target, GLint level, GLint internalFormat, |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 1145 | GLsizei width, GLsizei height, GLint border, |
| 1146 | GLenum format, GLenum type, |
| 1147 | const GLvoid *pixels ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1148 | { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 1149 | GET_CURRENT_CONTEXT(ctx); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1150 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glTexImage2D"); |
| 1151 | |
| 1152 | if (target==GL_TEXTURE_2D) { |
| Brian Paul | f7b5707 | 2000-03-20 14:37:52 +0000 | [diff] [blame] | 1153 | struct gl_texture_unit *texUnit; |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1154 | struct gl_texture_object *texObj; |
| 1155 | struct gl_texture_image *texImage; |
| Brian Paul | f7b5707 | 2000-03-20 14:37:52 +0000 | [diff] [blame] | 1156 | |
| Brian Paul | 43911c8 | 2000-03-21 00:49:33 +0000 | [diff] [blame] | 1157 | if (texture_error_check( ctx, target, level, internalFormat, |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1158 | format, type, 2, width, height, 1, border )) { |
| Brian Paul | f7b5707 | 2000-03-20 14:37:52 +0000 | [diff] [blame] | 1159 | return; /* error in texture image was detected */ |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
| Brian Paul | f7b5707 | 2000-03-20 14:37:52 +0000 | [diff] [blame] | 1162 | texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit]; |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1163 | texObj = texUnit->CurrentD[2]; |
| 1164 | texImage = texObj->Image[level]; |
| Brian Paul | f7b5707 | 2000-03-20 14:37:52 +0000 | [diff] [blame] | 1165 | |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1166 | if (!texImage) { |
| Brian Paul | 021a525 | 2000-03-27 17:54:17 +0000 | [diff] [blame] | 1167 | texImage = _mesa_alloc_texture_image(); |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1168 | 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; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1179 | /* setup the teximage struct's fields */ |
| 1180 | init_texture_image(texImage, width, height, 1, border, internalFormat); |
| Brian Paul | 43911c8 | 2000-03-21 00:49:33 +0000 | [diff] [blame] | 1181 | |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1182 | /* process the texture image */ |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 1183 | if (pixels) { |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1184 | 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 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1209 | } |
| 1210 | else { |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1211 | 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 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1221 | #define OLD_DD_TEXTURE |
| 1222 | #ifdef OLD_DD_TEXTURE |
| 1223 | /* XXX this will be removed in the future */ |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1224 | if (ctx->Driver.TexImage) { |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1225 | (*ctx->Driver.TexImage)( ctx, target, texObj, level, internalFormat, |
| 1226 | texImage ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1227 | } |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1228 | #endif |
| 1229 | |
| 1230 | /* state update */ |
| 1231 | gl_put_texobj_on_dirty_list( ctx, texObj ); |
| 1232 | ctx->NewState |= NEW_TEXTURING; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1233 | } |
| 1234 | else if (target==GL_PROXY_TEXTURE_2D) { |
| 1235 | /* Proxy texture: check for errors and update proxy state */ |
| Brian Paul | 43911c8 | 2000-03-21 00:49:33 +0000 | [diff] [blame] | 1236 | if (texture_error_check( ctx, target, level, internalFormat, |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1237 | 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 Paul | 43911c8 | 2000-03-21 00:49:33 +0000 | [diff] [blame] | 1246 | ctx->Texture.Proxy2D->Image[level]->IntFormat = (GLenum) internalFormat; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1247 | 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 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1252 | } |
| 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 Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 1265 | void |
| Brian Paul | 43911c8 | 2000-03-21 00:49:33 +0000 | [diff] [blame] | 1266 | _mesa_TexImage3D( GLenum target, GLint level, GLint internalFormat, |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 1267 | GLsizei width, GLsizei height, GLsizei depth, |
| 1268 | GLint border, GLenum format, GLenum type, |
| 1269 | const GLvoid *pixels ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1270 | { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 1271 | GET_CURRENT_CONTEXT(ctx); |
| Brian Paul | f7b5707 | 2000-03-20 14:37:52 +0000 | [diff] [blame] | 1272 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glTexImage3D"); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1273 | |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 1274 | if (target==GL_TEXTURE_3D_EXT) { |
| Brian Paul | f7b5707 | 2000-03-20 14:37:52 +0000 | [diff] [blame] | 1275 | struct gl_texture_unit *texUnit; |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1276 | struct gl_texture_object *texObj; |
| 1277 | struct gl_texture_image *texImage; |
| Brian Paul | 43911c8 | 2000-03-21 00:49:33 +0000 | [diff] [blame] | 1278 | if (texture_error_check( ctx, target, level, internalFormat, |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1279 | format, type, 3, width, height, depth, |
| 1280 | border )) { |
| Brian Paul | f7b5707 | 2000-03-20 14:37:52 +0000 | [diff] [blame] | 1281 | return; /* error in texture image was detected */ |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1282 | } |
| 1283 | |
| Brian Paul | f7b5707 | 2000-03-20 14:37:52 +0000 | [diff] [blame] | 1284 | texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit]; |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1285 | texObj = texUnit->CurrentD[3]; |
| 1286 | texImage = texObj->Image[level]; |
| Brian Paul | f7b5707 | 2000-03-20 14:37:52 +0000 | [diff] [blame] | 1287 | |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1288 | if (!texImage) { |
| Brian Paul | 021a525 | 2000-03-27 17:54:17 +0000 | [diff] [blame] | 1289 | texImage = _mesa_alloc_texture_image(); |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1290 | 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; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1301 | /* setup the teximage struct's fields */ |
| 1302 | init_texture_image(texImage, width, height, depth, |
| 1303 | border, internalFormat); |
| Brian Paul | 43911c8 | 2000-03-21 00:49:33 +0000 | [diff] [blame] | 1304 | |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1305 | /* process the texture image */ |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 1306 | if (pixels) { |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1307 | 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 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1332 | } |
| 1333 | else { |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1334 | 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 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1342 | } |
| 1343 | |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1344 | /* state update */ |
| 1345 | gl_put_texobj_on_dirty_list( ctx, texObj ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1346 | ctx->NewState |= NEW_TEXTURING; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1347 | } |
| 1348 | else if (target==GL_PROXY_TEXTURE_3D_EXT) { |
| 1349 | /* Proxy texture: check for errors and update proxy state */ |
| Brian Paul | 43911c8 | 2000-03-21 00:49:33 +0000 | [diff] [blame] | 1350 | if (texture_error_check( ctx, target, level, internalFormat, |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1351 | 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 Paul | 43911c8 | 2000-03-21 00:49:33 +0000 | [diff] [blame] | 1361 | ctx->Texture.Proxy3D->Image[level]->IntFormat = (GLenum) internalFormat; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1362 | 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 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1367 | } |
| 1368 | else { |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 1369 | gl_error( ctx, GL_INVALID_ENUM, "glTexImage3D(target)" ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1370 | return; |
| 1371 | } |
| 1372 | } |
| 1373 | |
| 1374 | |
| Brian Paul | 663049a | 2000-01-31 23:10:16 +0000 | [diff] [blame] | 1375 | void |
| Brian Paul | 43911c8 | 2000-03-21 00:49:33 +0000 | [diff] [blame] | 1376 | _mesa_TexImage3DEXT( GLenum target, GLint level, GLenum internalFormat, |
| Brian Paul | 663049a | 2000-01-31 23:10:16 +0000 | [diff] [blame] | 1377 | GLsizei width, GLsizei height, GLsizei depth, |
| 1378 | GLint border, GLenum format, GLenum type, |
| 1379 | const GLvoid *pixels ) |
| 1380 | { |
| Brian Paul | 43911c8 | 2000-03-21 00:49:33 +0000 | [diff] [blame] | 1381 | _mesa_TexImage3D(target, level, (GLint) internalFormat, width, height, |
| Brian Paul | 663049a | 2000-01-31 23:10:16 +0000 | [diff] [blame] | 1382 | depth, border, format, type, pixels); |
| 1383 | } |
| 1384 | |
| 1385 | |
| Brian Paul | f7b5707 | 2000-03-20 14:37:52 +0000 | [diff] [blame] | 1386 | /* |
| 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 Paul | 021a525 | 2000-03-27 17:54:17 +0000 | [diff] [blame] | 1390 | void |
| 1391 | _mesa_get_teximage_from_driver( GLcontext *ctx, GLenum target, GLint level, |
| 1392 | const struct gl_texture_object *texObj ) |
| Brian Paul | f7b5707 | 2000-03-20 14:37:52 +0000 | [diff] [blame] | 1393 | { |
| 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 Paul | 4827179 | 2000-03-29 18:13:59 +0000 | [diff] [blame] | 1403 | image = (*ctx->Driver.GetTexImage)( ctx, target, level, texObj, |
| Brian Paul | f7b5707 | 2000-03-20 14:37:52 +0000 | [diff] [blame] | 1404 | &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 Paul | b7d076f | 2000-03-21 01:03:40 +0000 | [diff] [blame] | 1417 | srcBytesPerTexel = _mesa_bytes_per_pixel(imgFormat, imgType); |
| Brian Paul | f7b5707 | 2000-03-20 14:37:52 +0000 | [diff] [blame] | 1418 | 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 Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1452 | imgType, srcPtr, &_mesa_native_packing, GL_FALSE); |
| Brian Paul | f7b5707 | 2000-03-20 14:37:52 +0000 | [diff] [blame] | 1453 | 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 Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1464 | imgFormat, imgType, srcPtr, &_mesa_native_packing, GL_FALSE); |
| Brian Paul | f7b5707 | 2000-03-20 14:37:52 +0000 | [diff] [blame] | 1465 | destPtr += destBytesPerRow; |
| 1466 | srcPtr += srcBytesPerRow; |
| 1467 | } |
| 1468 | } |
| 1469 | } |
| 1470 | } |
| 1471 | |
| 1472 | if (freeImage) |
| 1473 | FREE(image); |
| 1474 | } |
| 1475 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1476 | |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 1477 | void |
| 1478 | _mesa_GetTexImage( GLenum target, GLint level, GLenum format, |
| 1479 | GLenum type, GLvoid *pixels ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1480 | { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 1481 | GET_CURRENT_CONTEXT(ctx); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1482 | const struct gl_texture_object *texObj; |
| Brian Paul | f7b5707 | 2000-03-20 14:37:52 +0000 | [diff] [blame] | 1483 | struct gl_texture_image *texImage; |
| 1484 | GLboolean discardImage; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1485 | |
| 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 Paul | b7d076f | 2000-03-21 01:03:40 +0000 | [diff] [blame] | 1493 | if (_mesa_sizeof_type(type) <= 0) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1494 | gl_error( ctx, GL_INVALID_ENUM, "glGetTexImage(type)" ); |
| 1495 | return; |
| 1496 | } |
| 1497 | |
| Brian Paul | b7d076f | 2000-03-21 01:03:40 +0000 | [diff] [blame] | 1498 | if (_mesa_components_in_format(format) <= 0) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1499 | gl_error( ctx, GL_INVALID_ENUM, "glGetTexImage(format)" ); |
| 1500 | return; |
| 1501 | } |
| 1502 | |
| 1503 | if (!pixels) |
| Brian Paul | f7b5707 | 2000-03-20 14:37:52 +0000 | [diff] [blame] | 1504 | return; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1505 | |
| 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 Paul | f7b5707 | 2000-03-20 14:37:52 +0000 | [diff] [blame] | 1521 | 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 Paul | 021a525 | 2000-03-27 17:54:17 +0000 | [diff] [blame] | 1529 | _mesa_get_teximage_from_driver(ctx, target, level, texObj); |
| Brian Paul | f7b5707 | 2000-03-20 14:37:52 +0000 | [diff] [blame] | 1530 | discardImage = GL_TRUE; |
| 1531 | } |
| 1532 | else { |
| 1533 | discardImage = GL_FALSE; |
| 1534 | } |
| 1535 | |
| 1536 | if (texImage->Data) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1537 | 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 Paul | b7d076f | 2000-03-21 01:03:40 +0000 | [diff] [blame] | 1543 | GLvoid *dest = _mesa_image_address( &ctx->Unpack, pixels, |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1544 | 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 Paul | b7d076f | 2000-03-21 01:03:40 +0000 | [diff] [blame] | 1550 | _mesa_pack_rgba_span( ctx, width, (CONST GLubyte (*)[4]) src, |
| 1551 | format, type, dest, &ctx->Pack, GL_TRUE ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1552 | } |
| 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 Paul | b7d076f | 2000-03-21 01:03:40 +0000 | [diff] [blame] | 1614 | _mesa_pack_rgba_span( ctx, width, (const GLubyte (*)[4])rgba, |
| 1615 | format, type, dest, &ctx->Pack, GL_TRUE ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1616 | } |
| 1617 | } |
| Brian Paul | f7b5707 | 2000-03-20 14:37:52 +0000 | [diff] [blame] | 1618 | |
| 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 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1624 | } |
| 1625 | } |
| 1626 | |
| 1627 | |
| 1628 | |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 1629 | void |
| 1630 | _mesa_TexSubImage1D( GLenum target, GLint level, |
| 1631 | GLint xoffset, GLsizei width, |
| 1632 | GLenum format, GLenum type, |
| 1633 | const GLvoid *pixels ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1634 | { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 1635 | GET_CURRENT_CONTEXT(ctx); |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1636 | struct gl_texture_unit *texUnit; |
| 1637 | struct gl_texture_object *texObj; |
| 1638 | struct gl_texture_image *texImage; |
| 1639 | GLboolean success = GL_FALSE; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1640 | |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 1641 | if (subtexture_error_check(ctx, 1, target, level, xoffset, 0, 0, |
| 1642 | width, 1, 1, format, type)) { |
| Brian Paul | f7b5707 | 2000-03-20 14:37:52 +0000 | [diff] [blame] | 1643 | return; /* error was detected */ |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1644 | } |
| 1645 | |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1646 | texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit]; |
| 1647 | texObj = texUnit->CurrentD[1]; |
| 1648 | texImage = texObj->Image[level]; |
| 1649 | assert(texImage); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1650 | |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 1651 | if (width == 0 || !pixels) |
| 1652 | return; /* no-op, not an error */ |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1653 | |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 1654 | |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1655 | 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 Paul | 021a525 | 2000-03-27 17:54:17 +0000 | [diff] [blame] | 1669 | _mesa_get_teximage_from_driver( ctx, target, level, texObj ); |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1670 | if (!texImage->Data) { |
| 1671 | make_null_texture(texImage); |
| 1672 | } |
| 1673 | if (!texImage->Data) |
| 1674 | return; /* we're really out of luck! */ |
| 1675 | } |
| 1676 | |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 1677 | if (texFormat == GL_COLOR_INDEX) { |
| 1678 | /* color index texture */ |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1679 | 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 Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 1682 | _mesa_unpack_index_span(ctx, width, GL_UNSIGNED_BYTE, dst, |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1683 | type, src, &ctx->Unpack, GL_TRUE); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1684 | } |
| 1685 | else { |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 1686 | /* color texture */ |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1687 | 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; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1704 | } |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 1705 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1706 | |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1707 | /*gl_put_texobj_on_dirty_list( ctx, texUnit->CurrentD[1] );*/ |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1708 | } |
| 1709 | |
| 1710 | |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 1711 | void |
| 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 ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1717 | { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 1718 | GET_CURRENT_CONTEXT(ctx); |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1719 | struct gl_texture_unit *texUnit; |
| 1720 | struct gl_texture_object *texObj; |
| 1721 | struct gl_texture_image *texImage; |
| 1722 | GLboolean success = GL_FALSE; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1723 | |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 1724 | if (subtexture_error_check(ctx, 2, target, level, xoffset, yoffset, 0, |
| 1725 | width, height, 1, format, type)) { |
| Brian Paul | f7b5707 | 2000-03-20 14:37:52 +0000 | [diff] [blame] | 1726 | return; /* error was detected */ |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1727 | } |
| 1728 | |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1729 | texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit]; |
| 1730 | texObj = texUnit->CurrentD[2]; |
| 1731 | texImage = texObj->Image[level]; |
| 1732 | assert(texImage); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1733 | |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 1734 | if (width == 0 || height == 0 || !pixels) |
| 1735 | return; /* no-op, not an error */ |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1736 | |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1737 | 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 Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 1745 | |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1746 | 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 Paul | 021a525 | 2000-03-27 17:54:17 +0000 | [diff] [blame] | 1756 | _mesa_get_teximage_from_driver( ctx, target, level, texObj ); |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1757 | if (!texImage->Data) { |
| 1758 | make_null_texture(texImage); |
| 1759 | } |
| 1760 | if (!texImage->Data) |
| 1761 | return; /* we're really out of luck! */ |
| 1762 | } |
| 1763 | |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 1764 | if (texFormat == GL_COLOR_INDEX) { |
| 1765 | /* color index texture */ |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1766 | 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 Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 1770 | GLint row; |
| 1771 | for (row = 0; row < height; row++) { |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1772 | _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 Paul | 64a79b2 | 1999-10-22 10:43:35 +0000 | [diff] [blame] | 1776 | } |
| 1777 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1778 | else { |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 1779 | /* color texture */ |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1780 | 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 Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 1784 | GLint row; |
| 1785 | for (row = 0; row < height; row++) { |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1786 | _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; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1790 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1791 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1792 | |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1793 | 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); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1798 | } |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1799 | |
| 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 Paul | 9fd2b0a | 2000-03-24 23:59:06 +0000 | [diff] [blame] | 1813 | (*ctx->Driver.TexImage)(ctx, GL_TEXTURE_2D, texObj, |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1814 | level, texImage->IntFormat, texImage ); |
| 1815 | } |
| 1816 | #endif |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1817 | } |
| 1818 | } |
| 1819 | |
| 1820 | |
| 1821 | |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 1822 | void |
| 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 ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1828 | { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 1829 | GET_CURRENT_CONTEXT(ctx); |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1830 | struct gl_texture_unit *texUnit; |
| 1831 | struct gl_texture_object *texObj; |
| 1832 | struct gl_texture_image *texImage; |
| 1833 | GLboolean success = GL_FALSE; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1834 | |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 1835 | if (subtexture_error_check(ctx, 3, target, level, xoffset, yoffset, zoffset, |
| 1836 | width, height, depth, format, type)) { |
| Brian Paul | f7b5707 | 2000-03-20 14:37:52 +0000 | [diff] [blame] | 1837 | return; /* error was detected */ |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1838 | } |
| 1839 | |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1840 | texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit]; |
| 1841 | texObj = texUnit->CurrentD[3]; |
| 1842 | texImage = texObj->Image[level]; |
| 1843 | assert(texImage); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1844 | |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 1845 | if (width == 0 || height == 0 || height == 0 || !pixels) |
| 1846 | return; /* no-op, not an error */ |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1847 | |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1848 | 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; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1868 | |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 1869 | if (texFormat == GL_COLOR_INDEX) { |
| 1870 | /* color index texture */ |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 1871 | GLint img, row; |
| 1872 | for (img = 0; img < depth; img++) { |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1873 | 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 Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 1877 | for (row = 0; row < height; row++) { |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 1878 | _mesa_unpack_index_span(ctx, width, GL_UNSIGNED_BYTE, dst, |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1879 | type, (const GLvoid *) src, &ctx->Unpack, GL_TRUE); |
| 1880 | src += srcStride; |
| 1881 | dst += dstStride; |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 1882 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1883 | } |
| 1884 | } |
| 1885 | else { |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 1886 | /* color texture */ |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 1887 | GLint img, row; |
| 1888 | for (img = 0; img < depth; img++) { |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1889 | 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 Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 1893 | for (row = 0; row < height; row++) { |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 1894 | _mesa_unpack_ubyte_color_span(ctx, width, texFormat, dst, |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1895 | format, type, (const GLvoid *) src, &ctx->Unpack, GL_TRUE); |
| 1896 | src += srcStride; |
| 1897 | dst += dstStride; |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 1898 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1899 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1900 | } |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 1901 | |
| 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 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1913 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1914 | } |
| 1915 | |
| 1916 | |
| 1917 | |
| 1918 | /* |
| 1919 | * Read an RGBA image from the frame buffer. |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 1920 | * This is used by glCopyTexSubImage[12]D(). |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1921 | * Input: ctx - the context |
| 1922 | * x, y - lower left corner |
| 1923 | * width, height - size of region to read |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 1924 | * Return: pointer to block of GL_RGBA, GLubyte data. |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1925 | */ |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 1926 | static GLubyte * |
| 1927 | read_color_image( GLcontext *ctx, GLint x, GLint y, |
| 1928 | GLsizei width, GLsizei height ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1929 | { |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 1930 | GLint stride, i; |
| 1931 | GLubyte *image, *dst; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1932 | |
| Brian Paul | 959f802 | 2000-03-19 01:10:11 +0000 | [diff] [blame] | 1933 | image = (GLubyte *) MALLOC(width * height * 4 * sizeof(GLubyte)); |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 1934 | if (!image) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1935 | return NULL; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1936 | |
| 1937 | /* Select buffer to read from */ |
| Brian Paul | cea0e8e | 1999-11-25 17:36:48 +0000 | [diff] [blame] | 1938 | (*ctx->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer, |
| 1939 | ctx->Pixel.DriverReadBuffer ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1940 | |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 1941 | dst = image; |
| 1942 | stride = width * 4 * sizeof(GLubyte); |
| 1943 | for (i = 0; i < height; i++) { |
| Brian Paul | 3f02f90 | 1999-11-24 18:48:30 +0000 | [diff] [blame] | 1944 | gl_read_rgba_span( ctx, ctx->ReadBuffer, width, x, y + i, |
| 1945 | (GLubyte (*)[4]) dst ); |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 1946 | dst += stride; |
| 1947 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1948 | |
| Brian Paul | cea0e8e | 1999-11-25 17:36:48 +0000 | [diff] [blame] | 1949 | /* Read from draw buffer (the default) */ |
| 1950 | (*ctx->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer, |
| 1951 | ctx->Color.DriverDrawBuffer ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1952 | |
| 1953 | return image; |
| 1954 | } |
| 1955 | |
| 1956 | |
| 1957 | |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 1958 | void |
| 1959 | _mesa_CopyTexImage1D( GLenum target, GLint level, |
| 1960 | GLenum internalFormat, |
| 1961 | GLint x, GLint y, |
| 1962 | GLsizei width, GLint border ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1963 | { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 1964 | GET_CURRENT_CONTEXT(ctx); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1965 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glCopyTexImage1D"); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1966 | |
| Brian Paul | f7b5707 | 2000-03-20 14:37:52 +0000 | [diff] [blame] | 1967 | 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 Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 1976 | 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 Paul | 3ab6bbe | 2000-02-12 17:26:15 +0000 | [diff] [blame] | 1981 | (*ctx->Exec->TexImage1D)( target, level, internalFormat, width, |
| Brian Paul | f7b5707 | 2000-03-20 14:37:52 +0000 | [diff] [blame] | 1982 | border, GL_RGBA, GL_UNSIGNED_BYTE, image ); |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 1983 | FREE(image); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1984 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1985 | } |
| 1986 | |
| 1987 | |
| 1988 | |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 1989 | void |
| 1990 | _mesa_CopyTexImage2D( GLenum target, GLint level, GLenum internalFormat, |
| 1991 | GLint x, GLint y, GLsizei width, GLsizei height, |
| 1992 | GLint border ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1993 | { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 1994 | GET_CURRENT_CONTEXT(ctx); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1995 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glCopyTexImage2D"); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1996 | |
| Brian Paul | f7b5707 | 2000-03-20 14:37:52 +0000 | [diff] [blame] | 1997 | 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 Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 2006 | 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 Paul | 3ab6bbe | 2000-02-12 17:26:15 +0000 | [diff] [blame] | 2011 | (ctx->Exec->TexImage2D)( target, level, internalFormat, width, |
| Brian Paul | f7b5707 | 2000-03-20 14:37:52 +0000 | [diff] [blame] | 2012 | height, border, GL_RGBA, GL_UNSIGNED_BYTE, image ); |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 2013 | FREE(image); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 2014 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 2015 | } |
| 2016 | |
| 2017 | |
| 2018 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 2019 | /* |
| 2020 | * Do the work of glCopyTexSubImage[123]D. |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 2021 | */ |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 2022 | static void |
| 2023 | copy_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 ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 2027 | { |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 2028 | GLint i; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 2029 | GLint format, components, rectarea; |
| Brian Paul | 91baaa3 | 1999-10-17 23:24:16 +0000 | [diff] [blame] | 2030 | GLint texwidth, texheight, zoffset; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 2031 | |
| Brian Paul | 91baaa3 | 1999-10-17 23:24:16 +0000 | [diff] [blame] | 2032 | /* dst[xyz] may be negative if we have a texture border! */ |
| 2033 | dstx += dest->Border; |
| 2034 | dsty += dest->Border; |
| 2035 | dstz += dest->Border; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 2036 | texwidth = dest->Width; |
| 2037 | texheight = dest->Height; |
| 2038 | rectarea = texwidth * texheight; |
| Brian Paul | 91baaa3 | 1999-10-17 23:24:16 +0000 | [diff] [blame] | 2039 | zoffset = dstz * rectarea; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 2040 | format = dest->Format; |
| 2041 | components = components_in_intformat( format ); |
| 2042 | |
| 2043 | /* Select buffer to read from */ |
| Brian Paul | cea0e8e | 1999-11-25 17:36:48 +0000 | [diff] [blame] | 2044 | (*ctx->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer, |
| 2045 | ctx->Pixel.DriverReadBuffer ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 2046 | |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 2047 | for (i = 0;i < height; i++) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 2048 | GLubyte rgba[MAX_WIDTH][4]; |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 2049 | GLubyte *dst; |
| Brian Paul | 3f02f90 | 1999-11-24 18:48:30 +0000 | [diff] [blame] | 2050 | gl_read_rgba_span( ctx, ctx->ReadBuffer, width, srcx, srcy + i, rgba ); |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 2051 | 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 Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 2054 | &_mesa_native_packing, GL_TRUE); |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 2055 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 2056 | |
| Brian Paul | cea0e8e | 1999-11-25 17:36:48 +0000 | [diff] [blame] | 2057 | /* Read from draw buffer (the default) */ |
| 2058 | (*ctx->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer, |
| 2059 | ctx->Color.DriverDrawBuffer ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 2060 | } |
| 2061 | |
| 2062 | |
| 2063 | |
| 2064 | |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 2065 | void |
| 2066 | _mesa_CopyTexSubImage1D( GLenum target, GLint level, |
| 2067 | GLint xoffset, GLint x, GLint y, GLsizei width ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 2068 | { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 2069 | GET_CURRENT_CONTEXT(ctx); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 2070 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glCopyTexSubImage1D"); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 2071 | |
| Brian Paul | f7b5707 | 2000-03-20 14:37:52 +0000 | [diff] [blame] | 2072 | 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 Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 2080 | 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); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 2085 | if (teximage->Data) { |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 2086 | copy_tex_sub_image(ctx, teximage, width, 1, x, y, xoffset, 0, 0); |
| Brian Paul | f7b5707 | 2000-03-20 14:37:52 +0000 | [diff] [blame] | 2087 | /* tell driver about the change */ |
| Brian Paul | 9fd2b0a | 2000-03-24 23:59:06 +0000 | [diff] [blame] | 2088 | /* XXX this is obsolete */ |
| Brian Paul | f7b5707 | 2000-03-20 14:37:52 +0000 | [diff] [blame] | 2089 | if (ctx->Driver.TexImage) { |
| 2090 | (*ctx->Driver.TexImage)( ctx, GL_TEXTURE_1D, |
| 2091 | texUnit->CurrentD[1], |
| 2092 | level, teximage->IntFormat, teximage ); |
| 2093 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 2094 | } |
| 2095 | } |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 2096 | } |
| 2097 | |
| 2098 | |
| 2099 | |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 2100 | void |
| 2101 | _mesa_CopyTexSubImage2D( GLenum target, GLint level, |
| 2102 | GLint xoffset, GLint yoffset, |
| 2103 | GLint x, GLint y, GLsizei width, GLsizei height ) |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 2104 | { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 2105 | GET_CURRENT_CONTEXT(ctx); |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 2106 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glCopyTexSubImage2D"); |
| 2107 | |
| Brian Paul | f7b5707 | 2000-03-20 14:37:52 +0000 | [diff] [blame] | 2108 | 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 Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 2116 | 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 Paul | f7b5707 | 2000-03-20 14:37:52 +0000 | [diff] [blame] | 2124 | /* tell driver about the change */ |
| Brian Paul | 9fd2b0a | 2000-03-24 23:59:06 +0000 | [diff] [blame] | 2125 | /* XXX this is obsolete */ |
| Brian Paul | f7b5707 | 2000-03-20 14:37:52 +0000 | [diff] [blame] | 2126 | if (ctx->Driver.TexImage) { |
| 2127 | (*ctx->Driver.TexImage)( ctx, GL_TEXTURE_2D, |
| 2128 | texUnit->CurrentD[2], |
| 2129 | level, teximage->IntFormat, teximage ); |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 2130 | } |
| 2131 | } |
| 2132 | } |
| 2133 | } |
| 2134 | |
| 2135 | |
| 2136 | |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 2137 | void |
| 2138 | _mesa_CopyTexSubImage3D( GLenum target, GLint level, |
| 2139 | GLint xoffset, GLint yoffset, GLint zoffset, |
| 2140 | GLint x, GLint y, GLsizei width, GLsizei height ) |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 2141 | { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 2142 | GET_CURRENT_CONTEXT(ctx); |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 2143 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glCopyTexSubImage3D"); |
| 2144 | |
| Brian Paul | f7b5707 | 2000-03-20 14:37:52 +0000 | [diff] [blame] | 2145 | if (copytexsubimage_error_check(ctx, 3, target, level, |
| 2146 | xoffset, yoffset, zoffset, width, height)) |
| 2147 | return; |
| 2148 | |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 2149 | if (ctx->Pixel.MapColorFlag || ctx->Pixel.ScaleOrBiasRGBA |
| Brian Paul | f7b5707 | 2000-03-20 14:37:52 +0000 | [diff] [blame] | 2150 | || !ctx->Driver.CopyTexSubImage3D |
| 2151 | || !(*ctx->Driver.CopyTexSubImage3D)(ctx, target, level, |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 2152 | 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 Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 2160 | x, y, xoffset, yoffset, zoffset); |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 2161 | /* tell driver about the change */ |
| Brian Paul | 9fd2b0a | 2000-03-24 23:59:06 +0000 | [diff] [blame] | 2162 | /* XXX this is obsolete */ |
| Brian Paul | 0293878 | 2000-03-22 17:38:11 +0000 | [diff] [blame] | 2163 | if (ctx->Driver.TexImage) { |
| 2164 | (*ctx->Driver.TexImage)( ctx, GL_TEXTURE_3D, |
| 2165 | texUnit->CurrentD[3], |
| 2166 | level, teximage->IntFormat, teximage ); |
| Brian Paul | f7b5707 | 2000-03-20 14:37:52 +0000 | [diff] [blame] | 2167 | } |
| Brian Paul | c3f0a51 | 1999-11-03 17:27:05 +0000 | [diff] [blame] | 2168 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 2169 | } |
| 2170 | } |