Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1 | /************************************************************************** |
| 2 | * |
| 3 | * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas. |
| 4 | * All Rights Reserved. |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | * copy of this software and associated documentation files (the |
| 8 | * "Software"), to deal in the Software without restriction, including |
| 9 | * without limitation the rights to use, copy, modify, merge, publish, |
| 10 | * distribute, sub license, and/or sell copies of the Software, and to |
| 11 | * permit persons to whom the Software is furnished to do so, subject to |
| 12 | * the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice (including the |
| 15 | * next paragraph) shall be included in all copies or substantial portions |
| 16 | * 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 |
| 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. |
| 21 | * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR |
| 22 | * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 23 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 24 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 25 | * |
| 26 | **************************************************************************/ |
| 27 | |
| 28 | #include "main/imports.h" |
| 29 | #include "main/convolve.h" |
| 30 | #include "main/enums.h" |
| 31 | #include "main/image.h" |
| 32 | #include "main/macros.h" |
| 33 | #include "main/texcompress.h" |
| 34 | #include "main/texformat.h" |
| 35 | #include "main/teximage.h" |
| 36 | #include "main/texobj.h" |
| 37 | #include "main/texstore.h" |
| 38 | |
| 39 | #include "state_tracker/st_context.h" |
Brian | b27498c | 2007-09-26 17:18:42 -0600 | [diff] [blame] | 40 | #include "state_tracker/st_cb_fbo.h" |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 41 | #include "state_tracker/st_cb_texture.h" |
Brian | f8ab247 | 2007-08-10 08:37:15 +0100 | [diff] [blame] | 42 | #include "state_tracker/st_format.h" |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 43 | #include "state_tracker/st_mipmap_tree.h" |
| 44 | |
| 45 | #include "pipe/p_context.h" |
Brian | b245840 | 2007-08-07 18:18:54 -0600 | [diff] [blame] | 46 | #include "pipe/p_defines.h" |
Michel Dänzer | 11a8016 | 2007-11-28 19:04:54 +0100 | [diff] [blame^] | 47 | #include "pipe/p_inlines.h" |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 48 | |
| 49 | |
| 50 | #define DBG if (0) printf |
| 51 | |
| 52 | |
| 53 | struct st_texture_object |
| 54 | { |
| 55 | struct gl_texture_object base; /* The "parent" object */ |
| 56 | |
| 57 | /* The mipmap tree must include at least these levels once |
| 58 | * validated: |
| 59 | */ |
| 60 | GLuint firstLevel; |
| 61 | GLuint lastLevel; |
| 62 | |
| 63 | /* Offset for firstLevel image: |
| 64 | */ |
| 65 | GLuint textureOffset; |
| 66 | |
| 67 | /* On validation any active images held in main memory or in other |
| 68 | * regions will be copied to this region and the old storage freed. |
| 69 | */ |
| 70 | struct pipe_mipmap_tree *mt; |
| 71 | |
| 72 | GLboolean imageOverride; |
| 73 | GLint depthOverride; |
| 74 | GLuint pitchOverride; |
| 75 | }; |
| 76 | |
| 77 | |
| 78 | |
| 79 | struct st_texture_image |
| 80 | { |
| 81 | struct gl_texture_image base; |
| 82 | |
| 83 | /* These aren't stored in gl_texture_image |
| 84 | */ |
| 85 | GLuint level; |
| 86 | GLuint face; |
| 87 | |
| 88 | /* If stImage->mt != NULL, image data is stored here. |
| 89 | * Else if stImage->base.Data != NULL, image is stored there. |
| 90 | * Else there is no image data. |
| 91 | */ |
| 92 | struct pipe_mipmap_tree *mt; |
| 93 | }; |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | static INLINE struct st_texture_object * |
| 99 | st_texture_object(struct gl_texture_object *obj) |
| 100 | { |
| 101 | return (struct st_texture_object *) obj; |
| 102 | } |
| 103 | |
| 104 | static INLINE struct st_texture_image * |
| 105 | st_texture_image(struct gl_texture_image *img) |
| 106 | { |
| 107 | return (struct st_texture_image *) img; |
| 108 | } |
| 109 | |
| 110 | |
Brian | d78dab1 | 2007-08-07 15:12:22 -0600 | [diff] [blame] | 111 | struct pipe_mipmap_tree * |
| 112 | st_get_texobj_mipmap_tree(struct gl_texture_object *texObj) |
| 113 | { |
| 114 | struct st_texture_object *stObj = st_texture_object(texObj); |
| 115 | return stObj->mt; |
| 116 | } |
| 117 | |
| 118 | |
Brian | 5390a43 | 2007-08-15 19:23:58 -0600 | [diff] [blame] | 119 | static unsigned |
| 120 | gl_target_to_pipe(GLenum target) |
| 121 | { |
| 122 | switch (target) { |
| 123 | case GL_TEXTURE_1D: |
| 124 | return PIPE_TEXTURE_1D; |
| 125 | |
| 126 | case GL_TEXTURE_2D: |
| 127 | case GL_TEXTURE_RECTANGLE_NV: |
| 128 | return PIPE_TEXTURE_2D; |
| 129 | |
| 130 | case GL_TEXTURE_3D: |
| 131 | return PIPE_TEXTURE_3D; |
| 132 | |
| 133 | case GL_TEXTURE_CUBE_MAP_ARB: |
| 134 | return PIPE_TEXTURE_CUBE; |
| 135 | |
| 136 | default: |
| 137 | assert(0); |
| 138 | return 0; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 143 | static int |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 144 | compressed_num_bytes(GLuint mesaFormat) |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 145 | { |
| 146 | int bytes = 0; |
| 147 | switch(mesaFormat) { |
| 148 | |
| 149 | case MESA_FORMAT_RGB_FXT1: |
| 150 | case MESA_FORMAT_RGBA_FXT1: |
| 151 | case MESA_FORMAT_RGB_DXT1: |
| 152 | case MESA_FORMAT_RGBA_DXT1: |
| 153 | bytes = 2; |
| 154 | break; |
| 155 | |
| 156 | case MESA_FORMAT_RGBA_DXT3: |
| 157 | case MESA_FORMAT_RGBA_DXT5: |
| 158 | bytes = 4; |
| 159 | default: |
| 160 | break; |
| 161 | } |
| 162 | |
| 163 | return bytes; |
| 164 | } |
| 165 | |
| 166 | |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 167 | |
| 168 | |
| 169 | static GLboolean |
| 170 | st_IsTextureResident(GLcontext * ctx, struct gl_texture_object *texObj) |
| 171 | { |
| 172 | #if 0 |
| 173 | struct intel_context *intel = intel_context(ctx); |
| 174 | struct st_texture_object *stObj = st_texture_object(texObj); |
| 175 | |
| 176 | return |
| 177 | stObj->mt && |
| 178 | stObj->mt->region && |
| 179 | intel_is_region_resident(intel, stObj->mt->region); |
| 180 | #endif |
| 181 | return 1; |
| 182 | } |
| 183 | |
| 184 | |
| 185 | |
| 186 | static struct gl_texture_image * |
| 187 | st_NewTextureImage(GLcontext * ctx) |
| 188 | { |
| 189 | DBG("%s\n", __FUNCTION__); |
| 190 | (void) ctx; |
| 191 | return (struct gl_texture_image *) CALLOC_STRUCT(st_texture_image); |
| 192 | } |
| 193 | |
| 194 | |
| 195 | static struct gl_texture_object * |
| 196 | st_NewTextureObject(GLcontext * ctx, GLuint name, GLenum target) |
| 197 | { |
| 198 | struct st_texture_object *obj = CALLOC_STRUCT(st_texture_object); |
| 199 | |
| 200 | DBG("%s\n", __FUNCTION__); |
| 201 | _mesa_initialize_texture_object(&obj->base, name, target); |
| 202 | |
| 203 | return &obj->base; |
| 204 | } |
| 205 | |
| 206 | static void |
| 207 | st_DeleteTextureObject(GLcontext *ctx, |
| 208 | struct gl_texture_object *texObj) |
| 209 | { |
| 210 | struct pipe_context *pipe = ctx->st->pipe; |
| 211 | struct st_texture_object *stObj = st_texture_object(texObj); |
| 212 | |
| 213 | if (stObj->mt) |
| 214 | st_miptree_release(pipe, &stObj->mt); |
| 215 | |
| 216 | _mesa_delete_texture_object(ctx, texObj); |
| 217 | } |
| 218 | |
| 219 | |
| 220 | static void |
| 221 | st_FreeTextureImageData(GLcontext * ctx, struct gl_texture_image *texImage) |
| 222 | { |
| 223 | struct pipe_context *pipe = ctx->st->pipe; |
| 224 | struct st_texture_image *stImage = st_texture_image(texImage); |
| 225 | |
| 226 | DBG("%s\n", __FUNCTION__); |
| 227 | |
| 228 | if (stImage->mt) { |
| 229 | st_miptree_release(pipe, &stImage->mt); |
| 230 | } |
| 231 | |
| 232 | if (texImage->Data) { |
| 233 | free(texImage->Data); |
| 234 | texImage->Data = NULL; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | /* ================================================================ |
| 242 | * From linux kernel i386 header files, copes with odd sizes better |
| 243 | * than COPY_DWORDS would: |
| 244 | * XXX Put this in src/mesa/main/imports.h ??? |
| 245 | */ |
| 246 | #if defined(i386) || defined(__i386__) |
| 247 | static INLINE void * |
| 248 | __memcpy(void *to, const void *from, size_t n) |
| 249 | { |
| 250 | int d0, d1, d2; |
| 251 | __asm__ __volatile__("rep ; movsl\n\t" |
| 252 | "testb $2,%b4\n\t" |
| 253 | "je 1f\n\t" |
| 254 | "movsw\n" |
| 255 | "1:\ttestb $1,%b4\n\t" |
| 256 | "je 2f\n\t" |
| 257 | "movsb\n" "2:":"=&c"(d0), "=&D"(d1), "=&S"(d2) |
| 258 | :"0"(n / 4), "q"(n), "1"((long) to), "2"((long) from) |
| 259 | :"memory"); |
| 260 | return (to); |
| 261 | } |
| 262 | #else |
| 263 | #define __memcpy(a,b,c) memcpy(a,b,c) |
| 264 | #endif |
| 265 | |
| 266 | |
| 267 | /* The system memcpy (at least on ubuntu 5.10) has problems copying |
| 268 | * to agp (writecombined) memory from a source which isn't 64-byte |
| 269 | * aligned - there is a 4x performance falloff. |
| 270 | * |
| 271 | * The x86 __memcpy is immune to this but is slightly slower |
| 272 | * (10%-ish) than the system memcpy. |
| 273 | * |
| 274 | * The sse_memcpy seems to have a slight cliff at 64/32 bytes, but |
| 275 | * isn't much faster than x86_memcpy for agp copies. |
| 276 | * |
| 277 | * TODO: switch dynamically. |
| 278 | */ |
| 279 | static void * |
| 280 | do_memcpy(void *dest, const void *src, size_t n) |
| 281 | { |
| 282 | if ((((unsigned) src) & 63) || (((unsigned) dest) & 63)) { |
| 283 | return __memcpy(dest, src, n); |
| 284 | } |
| 285 | else |
| 286 | return memcpy(dest, src, n); |
| 287 | } |
| 288 | |
| 289 | |
| 290 | /* Functions to store texture images. Where possible, mipmap_tree's |
| 291 | * will be created or further instantiated with image data, otherwise |
| 292 | * images will be stored in malloc'd memory. A validation step is |
| 293 | * required to pull those images into a mipmap tree, or otherwise |
| 294 | * decide a fallback is required. |
| 295 | */ |
| 296 | |
| 297 | |
| 298 | static int |
| 299 | logbase2(int n) |
| 300 | { |
| 301 | GLint i = 1; |
| 302 | GLint log2 = 0; |
| 303 | |
| 304 | while (n > i) { |
| 305 | i *= 2; |
| 306 | log2++; |
| 307 | } |
| 308 | |
| 309 | return log2; |
| 310 | } |
| 311 | |
| 312 | |
| 313 | /* Otherwise, store it in memory if (Border != 0) or (any dimension == |
| 314 | * 1). |
| 315 | * |
| 316 | * Otherwise, if max_level >= level >= min_level, create tree with |
| 317 | * space for textures from min_level down to max_level. |
| 318 | * |
| 319 | * Otherwise, create tree with space for textures from (level |
| 320 | * 0)..(1x1). Consider pruning this tree at a validation if the |
| 321 | * saving is worth it. |
| 322 | */ |
| 323 | static void |
| 324 | guess_and_alloc_mipmap_tree(struct pipe_context *pipe, |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 325 | struct st_texture_object *stObj, |
| 326 | struct st_texture_image *stImage) |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 327 | { |
| 328 | GLuint firstLevel; |
| 329 | GLuint lastLevel; |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 330 | GLuint width = stImage->base.Width; |
| 331 | GLuint height = stImage->base.Height; |
| 332 | GLuint depth = stImage->base.Depth; |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 333 | GLuint l2width, l2height, l2depth; |
| 334 | GLuint i, comp_byte = 0; |
| 335 | |
| 336 | DBG("%s\n", __FUNCTION__); |
| 337 | |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 338 | if (stImage->base.Border) |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 339 | return; |
| 340 | |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 341 | if (stImage->level > stObj->base.BaseLevel && |
| 342 | (stImage->base.Width == 1 || |
| 343 | (stObj->base.Target != GL_TEXTURE_1D && |
| 344 | stImage->base.Height == 1) || |
| 345 | (stObj->base.Target == GL_TEXTURE_3D && |
| 346 | stImage->base.Depth == 1))) |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 347 | return; |
| 348 | |
| 349 | /* If this image disrespects BaseLevel, allocate from level zero. |
| 350 | * Usually BaseLevel == 0, so it's unlikely to happen. |
| 351 | */ |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 352 | if (stImage->level < stObj->base.BaseLevel) |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 353 | firstLevel = 0; |
| 354 | else |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 355 | firstLevel = stObj->base.BaseLevel; |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 356 | |
| 357 | |
| 358 | /* Figure out image dimensions at start level. |
| 359 | */ |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 360 | for (i = stImage->level; i > firstLevel; i--) { |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 361 | width <<= 1; |
| 362 | if (height != 1) |
| 363 | height <<= 1; |
| 364 | if (depth != 1) |
| 365 | depth <<= 1; |
| 366 | } |
| 367 | |
| 368 | /* Guess a reasonable value for lastLevel. This is probably going |
| 369 | * to be wrong fairly often and might mean that we have to look at |
| 370 | * resizable buffers, or require that buffers implement lazy |
| 371 | * pagetable arrangements. |
| 372 | */ |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 373 | if ((stObj->base.MinFilter == GL_NEAREST || |
| 374 | stObj->base.MinFilter == GL_LINEAR) && |
| 375 | stImage->level == firstLevel) { |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 376 | lastLevel = firstLevel; |
| 377 | } |
| 378 | else { |
| 379 | l2width = logbase2(width); |
| 380 | l2height = logbase2(height); |
| 381 | l2depth = logbase2(depth); |
| 382 | lastLevel = firstLevel + MAX2(MAX2(l2width, l2height), l2depth); |
| 383 | } |
| 384 | |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 385 | assert(!stObj->mt); |
| 386 | if (stImage->base.IsCompressed) |
| 387 | comp_byte = compressed_num_bytes(stImage->base.TexFormat->MesaFormat); |
| 388 | stObj->mt = st_miptree_create(pipe, |
Brian | 5390a43 | 2007-08-15 19:23:58 -0600 | [diff] [blame] | 389 | gl_target_to_pipe(stObj->base.Target), |
| 390 | stImage->base.InternalFormat, |
| 391 | firstLevel, |
| 392 | lastLevel, |
| 393 | width, |
| 394 | height, |
| 395 | depth, |
| 396 | stImage->base.TexFormat->TexelBytes, |
| 397 | comp_byte); |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 398 | |
Brian | b245840 | 2007-08-07 18:18:54 -0600 | [diff] [blame] | 399 | stObj->mt->format |
Brian | 9cf9aa1 | 2007-08-10 13:02:51 -0600 | [diff] [blame] | 400 | = st_mesa_format_to_pipe_format(stImage->base.TexFormat->MesaFormat); |
Brian | b245840 | 2007-08-07 18:18:54 -0600 | [diff] [blame] | 401 | |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 402 | DBG("%s - success\n", __FUNCTION__); |
| 403 | } |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | static GLuint |
| 409 | target_to_face(GLenum target) |
| 410 | { |
| 411 | switch (target) { |
| 412 | case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB: |
| 413 | case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB: |
| 414 | case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB: |
| 415 | case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB: |
| 416 | case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB: |
| 417 | case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB: |
| 418 | return ((GLuint) target - (GLuint) GL_TEXTURE_CUBE_MAP_POSITIVE_X); |
| 419 | default: |
| 420 | return 0; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | |
| 425 | |
| 426 | /* There are actually quite a few combinations this will work for, |
| 427 | * more than what I've listed here. |
| 428 | */ |
| 429 | static GLboolean |
| 430 | check_pbo_format(GLint internalFormat, |
| 431 | GLenum format, GLenum type, |
| 432 | const struct gl_texture_format *mesa_format) |
| 433 | { |
| 434 | switch (internalFormat) { |
| 435 | case 4: |
| 436 | case GL_RGBA: |
| 437 | return (format == GL_BGRA && |
| 438 | (type == GL_UNSIGNED_BYTE || |
| 439 | type == GL_UNSIGNED_INT_8_8_8_8_REV) && |
| 440 | mesa_format == &_mesa_texformat_argb8888); |
| 441 | case 3: |
| 442 | case GL_RGB: |
| 443 | return (format == GL_RGB && |
| 444 | type == GL_UNSIGNED_SHORT_5_6_5 && |
| 445 | mesa_format == &_mesa_texformat_rgb565); |
| 446 | case GL_YCBCR_MESA: |
| 447 | return (type == GL_UNSIGNED_SHORT_8_8_MESA || type == GL_UNSIGNED_BYTE); |
| 448 | default: |
| 449 | return GL_FALSE; |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | |
| 454 | /* XXX: Do this for TexSubImage also: |
| 455 | */ |
| 456 | static GLboolean |
| 457 | try_pbo_upload(GLcontext *ctx, |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 458 | struct st_texture_image *stImage, |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 459 | const struct gl_pixelstore_attrib *unpack, |
| 460 | GLint internalFormat, |
| 461 | GLint width, GLint height, |
| 462 | GLenum format, GLenum type, const void *pixels) |
| 463 | { |
| 464 | return GL_FALSE; /* XXX fix flushing/locking/blitting below */ |
| 465 | #if 000 |
| 466 | struct intel_context *intel = intel_context(ctx); |
| 467 | struct intel_buffer_object *pbo = intel_buffer_object(unpack->BufferObj); |
| 468 | GLuint src_offset, src_stride; |
| 469 | GLuint dst_offset, dst_stride; |
| 470 | |
| 471 | if (!pbo || |
| 472 | ctx._ImageTransferState || |
| 473 | unpack->SkipPixels || unpack->SkipRows) { |
| 474 | _mesa_printf("%s: failure 1\n", __FUNCTION__); |
| 475 | return GL_FALSE; |
| 476 | } |
| 477 | |
| 478 | src_offset = (GLuint) pixels; |
| 479 | |
| 480 | if (unpack->RowLength > 0) |
| 481 | src_stride = unpack->RowLength; |
| 482 | else |
| 483 | src_stride = width; |
| 484 | |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 485 | dst_offset = st_miptree_image_offset(stImage->mt, |
| 486 | stImage->face, |
| 487 | stImage->level); |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 488 | |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 489 | dst_stride = stImage->mt->pitch; |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 490 | |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 491 | { |
| 492 | struct _DriBufferObject *src_buffer = |
| 493 | intel_bufferobj_buffer(intel, pbo, INTEL_READ); |
| 494 | |
| 495 | /* Temporary hack: cast to _DriBufferObject: |
| 496 | */ |
| 497 | struct _DriBufferObject *dst_buffer = |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 498 | (struct _DriBufferObject *)stImage->mt->region->buffer; |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 499 | |
| 500 | |
| 501 | intelEmitCopyBlit(intel, |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 502 | stImage->mt->cpp, |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 503 | src_stride, src_buffer, src_offset, |
| 504 | dst_stride, dst_buffer, dst_offset, |
| 505 | 0, 0, 0, 0, width, height, |
| 506 | GL_COPY); |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 507 | } |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 508 | |
| 509 | return GL_TRUE; |
| 510 | #endif |
| 511 | } |
| 512 | |
| 513 | |
| 514 | |
| 515 | static GLboolean |
| 516 | try_pbo_zcopy(GLcontext *ctx, |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 517 | struct st_texture_image *stImage, |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 518 | const struct gl_pixelstore_attrib *unpack, |
| 519 | GLint internalFormat, |
| 520 | GLint width, GLint height, |
| 521 | GLenum format, GLenum type, const void *pixels) |
| 522 | { |
| 523 | return GL_FALSE; |
| 524 | } |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | |
| 530 | |
| 531 | static void |
| 532 | st_TexImage(GLcontext * ctx, |
| 533 | GLint dims, |
| 534 | GLenum target, GLint level, |
| 535 | GLint internalFormat, |
| 536 | GLint width, GLint height, GLint depth, |
| 537 | GLint border, |
| 538 | GLenum format, GLenum type, const void *pixels, |
| 539 | const struct gl_pixelstore_attrib *unpack, |
| 540 | struct gl_texture_object *texObj, |
| 541 | struct gl_texture_image *texImage, GLsizei imageSize, int compressed) |
| 542 | { |
| 543 | struct pipe_context *pipe = ctx->st->pipe; |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 544 | struct st_texture_object *stObj = st_texture_object(texObj); |
| 545 | struct st_texture_image *stImage = st_texture_image(texImage); |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 546 | GLint postConvWidth = width; |
| 547 | GLint postConvHeight = height; |
| 548 | GLint texelBytes, sizeInBytes; |
| 549 | GLuint dstRowStride; |
| 550 | |
| 551 | |
| 552 | DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__, |
| 553 | _mesa_lookup_enum_by_nr(target), level, width, height, depth, border); |
| 554 | |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 555 | stImage->face = target_to_face(target); |
| 556 | stImage->level = level; |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 557 | |
| 558 | if (ctx->_ImageTransferState & IMAGE_CONVOLUTION_BIT) { |
| 559 | _mesa_adjust_image_for_convolution(ctx, dims, &postConvWidth, |
| 560 | &postConvHeight); |
| 561 | } |
| 562 | |
| 563 | /* choose the texture format */ |
| 564 | texImage->TexFormat = st_ChooseTextureFormat(ctx, internalFormat, |
| 565 | format, type); |
| 566 | |
| 567 | _mesa_set_fetch_functions(texImage, dims); |
| 568 | |
| 569 | if (texImage->TexFormat->TexelBytes == 0) { |
| 570 | /* must be a compressed format */ |
| 571 | texelBytes = 0; |
| 572 | texImage->IsCompressed = GL_TRUE; |
| 573 | texImage->CompressedSize = |
| 574 | ctx->Driver.CompressedTextureSize(ctx, texImage->Width, |
| 575 | texImage->Height, texImage->Depth, |
| 576 | texImage->TexFormat->MesaFormat); |
| 577 | } else { |
| 578 | texelBytes = texImage->TexFormat->TexelBytes; |
| 579 | |
| 580 | /* Minimum pitch of 32 bytes */ |
| 581 | if (postConvWidth * texelBytes < 32) { |
| 582 | postConvWidth = 32 / texelBytes; |
| 583 | texImage->RowStride = postConvWidth; |
| 584 | } |
| 585 | |
| 586 | assert(texImage->RowStride == postConvWidth); |
| 587 | } |
| 588 | |
| 589 | /* Release the reference to a potentially orphaned buffer. |
| 590 | * Release any old malloced memory. |
| 591 | */ |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 592 | if (stImage->mt) { |
| 593 | st_miptree_release(pipe, &stImage->mt); |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 594 | assert(!texImage->Data); |
| 595 | } |
| 596 | else if (texImage->Data) { |
| 597 | _mesa_align_free(texImage->Data); |
| 598 | } |
| 599 | |
| 600 | /* If this is the only texture image in the tree, could call |
| 601 | * bmBufferData with NULL data to free the old block and avoid |
| 602 | * waiting on any outstanding fences. |
| 603 | */ |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 604 | if (stObj->mt && |
| 605 | stObj->mt->first_level == level && |
| 606 | stObj->mt->last_level == level && |
Brian | 5390a43 | 2007-08-15 19:23:58 -0600 | [diff] [blame] | 607 | stObj->mt->target != PIPE_TEXTURE_CUBE && |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 608 | !st_miptree_match_image(stObj->mt, &stImage->base, |
| 609 | stImage->face, stImage->level)) { |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 610 | |
| 611 | DBG("release it\n"); |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 612 | st_miptree_release(pipe, &stObj->mt); |
| 613 | assert(!stObj->mt); |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 614 | } |
| 615 | |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 616 | if (!stObj->mt) { |
| 617 | guess_and_alloc_mipmap_tree(pipe, stObj, stImage); |
| 618 | if (!stObj->mt) { |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 619 | DBG("guess_and_alloc_mipmap_tree: failed\n"); |
| 620 | } |
| 621 | } |
| 622 | |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 623 | assert(!stImage->mt); |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 624 | |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 625 | if (stObj->mt && |
| 626 | st_miptree_match_image(stObj->mt, &stImage->base, |
| 627 | stImage->face, stImage->level)) { |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 628 | |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 629 | st_miptree_reference(&stImage->mt, stObj->mt); |
| 630 | assert(stImage->mt); |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 631 | } |
| 632 | |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 633 | if (!stImage->mt) |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 634 | DBG("XXX: Image did not fit into tree - storing in local memory!\n"); |
| 635 | |
| 636 | #if 0 /* XXX FIX when st_buffer_objects are in place */ |
| 637 | /* PBO fastpaths: |
| 638 | */ |
| 639 | if (dims <= 2 && |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 640 | stImage->mt && |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 641 | intel_buffer_object(unpack->BufferObj) && |
| 642 | check_pbo_format(internalFormat, format, |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 643 | type, stImage->base.TexFormat)) { |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 644 | |
| 645 | DBG("trying pbo upload\n"); |
| 646 | |
| 647 | /* Attempt to texture directly from PBO data (zero copy upload). |
| 648 | * |
| 649 | * Currently disable as it can lead to worse as well as better |
| 650 | * performance (in particular when pipe_region_cow() is |
| 651 | * required). |
| 652 | */ |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 653 | if (stObj->mt == stImage->mt && |
| 654 | stObj->mt->first_level == level && |
| 655 | stObj->mt->last_level == level) { |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 656 | |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 657 | if (try_pbo_zcopy(intel, stImage, unpack, |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 658 | internalFormat, |
| 659 | width, height, format, type, pixels)) { |
| 660 | |
| 661 | DBG("pbo zcopy upload succeeded\n"); |
| 662 | return; |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | |
| 667 | /* Otherwise, attempt to use the blitter for PBO image uploads. |
| 668 | */ |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 669 | if (try_pbo_upload(intel, stImage, unpack, |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 670 | internalFormat, |
| 671 | width, height, format, type, pixels)) { |
| 672 | DBG("pbo upload succeeded\n"); |
| 673 | return; |
| 674 | } |
| 675 | |
| 676 | DBG("pbo upload failed\n"); |
| 677 | } |
| 678 | #else |
| 679 | (void) try_pbo_upload; |
| 680 | (void) check_pbo_format; |
| 681 | (void) try_pbo_zcopy; |
| 682 | #endif |
| 683 | |
| 684 | |
| 685 | /* intelCopyTexImage calls this function with pixels == NULL, with |
| 686 | * the expectation that the mipmap tree will be set up but nothing |
| 687 | * more will be done. This is where those calls return: |
| 688 | */ |
| 689 | if (compressed) { |
| 690 | pixels = _mesa_validate_pbo_compressed_teximage(ctx, imageSize, pixels, |
| 691 | unpack, |
| 692 | "glCompressedTexImage"); |
| 693 | } else { |
| 694 | pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, 1, |
| 695 | format, type, |
| 696 | pixels, unpack, "glTexImage"); |
| 697 | } |
| 698 | if (!pixels) |
| 699 | return; |
| 700 | |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 701 | if (stImage->mt) { |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 702 | texImage->Data = st_miptree_image_map(pipe, |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 703 | stImage->mt, |
| 704 | stImage->face, |
| 705 | stImage->level, |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 706 | &dstRowStride, |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 707 | stImage->base.ImageOffsets); |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 708 | } |
| 709 | else { |
| 710 | /* Allocate regular memory and store the image there temporarily. */ |
| 711 | if (texImage->IsCompressed) { |
| 712 | sizeInBytes = texImage->CompressedSize; |
| 713 | dstRowStride = |
| 714 | _mesa_compressed_row_stride(texImage->TexFormat->MesaFormat, width); |
| 715 | assert(dims != 3); |
| 716 | } |
| 717 | else { |
| 718 | dstRowStride = postConvWidth * texelBytes; |
| 719 | sizeInBytes = depth * dstRowStride * postConvHeight; |
| 720 | } |
| 721 | |
| 722 | texImage->Data = malloc(sizeInBytes); |
| 723 | } |
| 724 | |
| 725 | DBG("Upload image %dx%dx%d row_len %x pitch %x\n", |
| 726 | width, height, depth, width * texelBytes, dstRowStride); |
| 727 | |
| 728 | /* Copy data. Would like to know when it's ok for us to eg. use |
| 729 | * the blitter to copy. Or, use the hardware to do the format |
| 730 | * conversion and copy: |
| 731 | */ |
| 732 | if (compressed) { |
| 733 | memcpy(texImage->Data, pixels, imageSize); |
| 734 | } |
| 735 | else if (!texImage->TexFormat->StoreImage(ctx, dims, |
| 736 | texImage->_BaseFormat, |
| 737 | texImage->TexFormat, |
| 738 | texImage->Data, |
| 739 | 0, 0, 0, /* dstX/Y/Zoffset */ |
| 740 | dstRowStride, |
| 741 | texImage->ImageOffsets, |
| 742 | width, height, depth, |
| 743 | format, type, pixels, unpack)) { |
| 744 | _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage"); |
| 745 | } |
| 746 | |
| 747 | _mesa_unmap_teximage_pbo(ctx, unpack); |
| 748 | |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 749 | if (stImage->mt) { |
| 750 | st_miptree_image_unmap(pipe, stImage->mt); |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 751 | texImage->Data = NULL; |
| 752 | } |
| 753 | |
| 754 | #if 0 |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 755 | /* GL_SGIS_generate_mipmap -- this can be accelerated now. |
| 756 | */ |
| 757 | if (level == texObj->BaseLevel && texObj->GenerateMipmap) { |
| 758 | intel_generate_mipmap(ctx, target, |
| 759 | &ctx->Texture.Unit[ctx->Texture.CurrentUnit], |
| 760 | texObj); |
| 761 | } |
| 762 | #endif |
| 763 | } |
| 764 | |
| 765 | |
| 766 | static void |
| 767 | st_TexImage3D(GLcontext * ctx, |
| 768 | GLenum target, GLint level, |
| 769 | GLint internalFormat, |
| 770 | GLint width, GLint height, GLint depth, |
| 771 | GLint border, |
| 772 | GLenum format, GLenum type, const void *pixels, |
| 773 | const struct gl_pixelstore_attrib *unpack, |
| 774 | struct gl_texture_object *texObj, |
| 775 | struct gl_texture_image *texImage) |
| 776 | { |
| 777 | st_TexImage(ctx, 3, target, level, |
| 778 | internalFormat, width, height, depth, border, |
| 779 | format, type, pixels, unpack, texObj, texImage, 0, 0); |
| 780 | } |
| 781 | |
| 782 | |
| 783 | static void |
| 784 | st_TexImage2D(GLcontext * ctx, |
| 785 | GLenum target, GLint level, |
| 786 | GLint internalFormat, |
| 787 | GLint width, GLint height, GLint border, |
| 788 | GLenum format, GLenum type, const void *pixels, |
| 789 | const struct gl_pixelstore_attrib *unpack, |
| 790 | struct gl_texture_object *texObj, |
| 791 | struct gl_texture_image *texImage) |
| 792 | { |
| 793 | st_TexImage(ctx, 2, target, level, |
| 794 | internalFormat, width, height, 1, border, |
| 795 | format, type, pixels, unpack, texObj, texImage, 0, 0); |
| 796 | } |
| 797 | |
| 798 | |
| 799 | static void |
| 800 | st_TexImage1D(GLcontext * ctx, |
| 801 | GLenum target, GLint level, |
| 802 | GLint internalFormat, |
| 803 | GLint width, GLint border, |
| 804 | GLenum format, GLenum type, const void *pixels, |
| 805 | const struct gl_pixelstore_attrib *unpack, |
| 806 | struct gl_texture_object *texObj, |
| 807 | struct gl_texture_image *texImage) |
| 808 | { |
| 809 | st_TexImage(ctx, 1, target, level, |
| 810 | internalFormat, width, 1, 1, border, |
| 811 | format, type, pixels, unpack, texObj, texImage, 0, 0); |
| 812 | } |
| 813 | |
| 814 | |
| 815 | static void |
| 816 | st_CompressedTexImage2D( GLcontext *ctx, GLenum target, GLint level, |
| 817 | GLint internalFormat, |
| 818 | GLint width, GLint height, GLint border, |
| 819 | GLsizei imageSize, const GLvoid *data, |
| 820 | struct gl_texture_object *texObj, |
| 821 | struct gl_texture_image *texImage ) |
| 822 | { |
| 823 | st_TexImage(ctx, 2, target, level, |
| 824 | internalFormat, width, height, 1, border, |
| 825 | 0, 0, data, &ctx->Unpack, texObj, texImage, imageSize, 1); |
| 826 | } |
| 827 | |
| 828 | |
| 829 | /** |
| 830 | * Need to map texture image into memory before copying image data, |
| 831 | * then unmap it. |
| 832 | */ |
| 833 | static void |
| 834 | st_get_tex_image(GLcontext * ctx, GLenum target, GLint level, |
| 835 | GLenum format, GLenum type, GLvoid * pixels, |
| 836 | struct gl_texture_object *texObj, |
| 837 | struct gl_texture_image *texImage, int compressed) |
| 838 | { |
| 839 | /* |
| 840 | struct intel_context *intel = intel_context(ctx); |
| 841 | */ |
| 842 | struct pipe_context *pipe = ctx->st->pipe; |
| 843 | struct st_texture_image *stImage = st_texture_image(texImage); |
| 844 | |
| 845 | /* Map */ |
| 846 | if (stImage->mt) { |
| 847 | /* Image is stored in hardware format in a buffer managed by the |
| 848 | * kernel. Need to explicitly map and unmap it. |
| 849 | */ |
| 850 | stImage->base.Data = |
| 851 | st_miptree_image_map(pipe, |
| 852 | stImage->mt, |
| 853 | stImage->face, |
| 854 | stImage->level, |
| 855 | &stImage->base.RowStride, |
| 856 | stImage->base.ImageOffsets); |
| 857 | stImage->base.RowStride /= stImage->mt->cpp; |
| 858 | } |
| 859 | else { |
| 860 | /* Otherwise, the image should actually be stored in |
| 861 | * stImage->base.Data. This is pretty confusing for |
| 862 | * everybody, I'd much prefer to separate the two functions of |
| 863 | * texImage->Data - storage for texture images in main memory |
| 864 | * and access (ie mappings) of images. In other words, we'd |
| 865 | * create a new texImage->Map field and leave Data simply for |
| 866 | * storage. |
| 867 | */ |
| 868 | assert(stImage->base.Data); |
| 869 | } |
| 870 | |
| 871 | |
| 872 | if (compressed) { |
| 873 | _mesa_get_compressed_teximage(ctx, target, level, pixels, |
| 874 | texObj, texImage); |
| 875 | } else { |
| 876 | _mesa_get_teximage(ctx, target, level, format, type, pixels, |
| 877 | texObj, texImage); |
| 878 | } |
| 879 | |
| 880 | |
| 881 | /* Unmap */ |
| 882 | if (stImage->mt) { |
| 883 | st_miptree_image_unmap(pipe, stImage->mt); |
| 884 | stImage->base.Data = NULL; |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | |
| 889 | static void |
| 890 | st_GetTexImage(GLcontext * ctx, GLenum target, GLint level, |
| 891 | GLenum format, GLenum type, GLvoid * pixels, |
| 892 | struct gl_texture_object *texObj, |
| 893 | struct gl_texture_image *texImage) |
| 894 | { |
| 895 | st_get_tex_image(ctx, target, level, format, type, pixels, |
| 896 | texObj, texImage, 0); |
| 897 | } |
| 898 | |
| 899 | |
| 900 | static void |
| 901 | st_GetCompressedTexImage(GLcontext *ctx, GLenum target, GLint level, |
| 902 | GLvoid *pixels, |
| 903 | const struct gl_texture_object *texObj, |
| 904 | const struct gl_texture_image *texImage) |
| 905 | { |
| 906 | st_get_tex_image(ctx, target, level, 0, 0, pixels, |
| 907 | (struct gl_texture_object *) texObj, |
| 908 | (struct gl_texture_image *) texImage, 1); |
| 909 | } |
| 910 | |
| 911 | |
| 912 | |
| 913 | static void |
| 914 | st_TexSubimage(GLcontext * ctx, |
| 915 | GLint dims, |
| 916 | GLenum target, GLint level, |
| 917 | GLint xoffset, GLint yoffset, GLint zoffset, |
| 918 | GLint width, GLint height, GLint depth, |
| 919 | GLenum format, GLenum type, const void *pixels, |
| 920 | const struct gl_pixelstore_attrib *packing, |
| 921 | struct gl_texture_object *texObj, |
| 922 | struct gl_texture_image *texImage) |
| 923 | { |
| 924 | struct pipe_context *pipe = ctx->st->pipe; |
| 925 | struct st_texture_image *stImage = st_texture_image(texImage); |
| 926 | GLuint dstRowStride; |
| 927 | |
| 928 | DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__, |
| 929 | _mesa_lookup_enum_by_nr(target), |
| 930 | level, xoffset, yoffset, width, height); |
| 931 | |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 932 | pixels = |
| 933 | _mesa_validate_pbo_teximage(ctx, dims, width, height, depth, format, |
| 934 | type, pixels, packing, "glTexSubImage2D"); |
| 935 | if (!pixels) |
| 936 | return; |
| 937 | |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 938 | /* Map buffer if necessary. Need to lock to prevent other contexts |
| 939 | * from uploading the buffer under us. |
| 940 | */ |
| 941 | if (stImage->mt) |
| 942 | texImage->Data = st_miptree_image_map(pipe, |
| 943 | stImage->mt, |
| 944 | stImage->face, |
| 945 | stImage->level, |
| 946 | &dstRowStride, |
| 947 | texImage->ImageOffsets); |
| 948 | |
| 949 | assert(dstRowStride); |
| 950 | |
| 951 | if (!texImage->TexFormat->StoreImage(ctx, dims, texImage->_BaseFormat, |
| 952 | texImage->TexFormat, |
| 953 | texImage->Data, |
| 954 | xoffset, yoffset, zoffset, |
| 955 | dstRowStride, |
| 956 | texImage->ImageOffsets, |
| 957 | width, height, depth, |
| 958 | format, type, pixels, packing)) { |
| 959 | _mesa_error(ctx, GL_OUT_OF_MEMORY, "intelTexSubImage"); |
| 960 | } |
| 961 | |
| 962 | #if 0 |
| 963 | /* GL_SGIS_generate_mipmap */ |
| 964 | if (level == texObj->BaseLevel && texObj->GenerateMipmap) { |
| 965 | _mesa_generate_mipmap(ctx, target, |
| 966 | &ctx->Texture.Unit[ctx->Texture.CurrentUnit], |
| 967 | texObj); |
| 968 | } |
| 969 | #endif |
| 970 | |
| 971 | _mesa_unmap_teximage_pbo(ctx, packing); |
| 972 | |
| 973 | if (stImage->mt) { |
| 974 | st_miptree_image_unmap(pipe, stImage->mt); |
| 975 | texImage->Data = NULL; |
| 976 | } |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | |
| 980 | |
| 981 | static void |
| 982 | st_TexSubImage3D(GLcontext * ctx, |
| 983 | GLenum target, |
| 984 | GLint level, |
| 985 | GLint xoffset, GLint yoffset, GLint zoffset, |
| 986 | GLsizei width, GLsizei height, GLsizei depth, |
| 987 | GLenum format, GLenum type, |
| 988 | const GLvoid * pixels, |
| 989 | const struct gl_pixelstore_attrib *packing, |
| 990 | struct gl_texture_object *texObj, |
| 991 | struct gl_texture_image *texImage) |
| 992 | { |
Brian | b245840 | 2007-08-07 18:18:54 -0600 | [diff] [blame] | 993 | st_TexSubimage(ctx, 3, target, level, |
| 994 | xoffset, yoffset, zoffset, |
| 995 | width, height, depth, |
| 996 | format, type, pixels, packing, texObj, texImage); |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | |
| 1000 | |
| 1001 | static void |
| 1002 | st_TexSubImage2D(GLcontext * ctx, |
| 1003 | GLenum target, |
| 1004 | GLint level, |
| 1005 | GLint xoffset, GLint yoffset, |
| 1006 | GLsizei width, GLsizei height, |
| 1007 | GLenum format, GLenum type, |
| 1008 | const GLvoid * pixels, |
| 1009 | const struct gl_pixelstore_attrib *packing, |
| 1010 | struct gl_texture_object *texObj, |
| 1011 | struct gl_texture_image *texImage) |
| 1012 | { |
Brian | b245840 | 2007-08-07 18:18:54 -0600 | [diff] [blame] | 1013 | st_TexSubimage(ctx, 2, target, level, |
| 1014 | xoffset, yoffset, 0, |
| 1015 | width, height, 1, |
| 1016 | format, type, pixels, packing, texObj, texImage); |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | |
| 1020 | static void |
| 1021 | st_TexSubImage1D(GLcontext * ctx, |
| 1022 | GLenum target, |
| 1023 | GLint level, |
| 1024 | GLint xoffset, |
| 1025 | GLsizei width, |
| 1026 | GLenum format, GLenum type, |
| 1027 | const GLvoid * pixels, |
| 1028 | const struct gl_pixelstore_attrib *packing, |
| 1029 | struct gl_texture_object *texObj, |
| 1030 | struct gl_texture_image *texImage) |
| 1031 | { |
Brian | b245840 | 2007-08-07 18:18:54 -0600 | [diff] [blame] | 1032 | st_TexSubimage(ctx, 1, target, level, |
| 1033 | xoffset, 0, 0, |
| 1034 | width, 1, 1, |
| 1035 | format, type, pixels, packing, texObj, texImage); |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | |
| 1039 | |
| 1040 | /** |
Brian | 038cb56 | 2007-09-26 17:57:15 -0600 | [diff] [blame] | 1041 | * Return 0 for GL_TEXTURE_CUBE_MAP_POSITIVE_X, |
| 1042 | * 1 for GL_TEXTURE_CUBE_MAP_NEGATIVE_X, |
| 1043 | * etc. |
| 1044 | * XXX duplicated from main/teximage.c |
| 1045 | */ |
| 1046 | static uint |
| 1047 | texture_face(GLenum target) |
| 1048 | { |
| 1049 | if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB && |
| 1050 | target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) |
| 1051 | return (GLuint) target - (GLuint) GL_TEXTURE_CUBE_MAP_POSITIVE_X; |
| 1052 | else |
| 1053 | return 0; |
| 1054 | } |
| 1055 | |
| 1056 | |
| 1057 | |
| 1058 | /** |
| 1059 | * Do a CopyTexSubImage operation by mapping the source region and |
Brian | 5c83f13 | 2007-09-26 18:54:20 -0600 | [diff] [blame] | 1060 | * dest region and using get_tile()/put_tile() to access the pixels/texels. |
Brian | c6717a8 | 2007-09-26 18:39:14 -0600 | [diff] [blame] | 1061 | * |
| 1062 | * Note: srcY=0=TOP of renderbuffer |
Brian | 038cb56 | 2007-09-26 17:57:15 -0600 | [diff] [blame] | 1063 | */ |
| 1064 | static void |
| 1065 | fallback_copy_texsubimage(GLcontext *ctx, |
| 1066 | GLenum target, |
| 1067 | GLint level, |
| 1068 | struct st_renderbuffer *strb, |
| 1069 | struct st_texture_image *stImage, |
| 1070 | GLenum baseFormat, |
| 1071 | GLint destX, GLint destY, GLint destZ, |
| 1072 | GLint srcX, GLint srcY, |
| 1073 | GLsizei width, GLsizei height) |
| 1074 | { |
| 1075 | struct pipe_context *pipe = ctx->st->pipe; |
| 1076 | const uint face = texture_face(target); |
| 1077 | struct pipe_mipmap_tree *mt = stImage->mt; |
| 1078 | struct pipe_surface *src_surf, *dest_surf; |
| 1079 | GLfloat *data; |
Brian | 5c83f13 | 2007-09-26 18:54:20 -0600 | [diff] [blame] | 1080 | GLint row, yStep; |
| 1081 | |
| 1082 | /* determine bottom-to-top vs. top-to-bottom order */ |
| 1083 | if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) { |
| 1084 | destY = height - 1 - destY; |
| 1085 | yStep = -1; |
| 1086 | } |
| 1087 | else { |
| 1088 | yStep = 1; |
| 1089 | } |
Brian | 038cb56 | 2007-09-26 17:57:15 -0600 | [diff] [blame] | 1090 | |
| 1091 | src_surf = strb->surface; |
| 1092 | |
| 1093 | dest_surf = pipe->get_tex_surface(pipe, mt, |
| 1094 | face, level, destZ); |
| 1095 | |
Brian | c6717a8 | 2007-09-26 18:39:14 -0600 | [diff] [blame] | 1096 | (void) pipe->region_map(pipe, dest_surf->region); |
| 1097 | (void) pipe->region_map(pipe, src_surf->region); |
| 1098 | |
Brian | 5c83f13 | 2007-09-26 18:54:20 -0600 | [diff] [blame] | 1099 | /* buffer for one row */ |
| 1100 | data = (GLfloat *) malloc(width * 4 * sizeof(GLfloat)); |
Brian | 038cb56 | 2007-09-26 17:57:15 -0600 | [diff] [blame] | 1101 | |
Brian | 5c83f13 | 2007-09-26 18:54:20 -0600 | [diff] [blame] | 1102 | /* do copy row by row */ |
| 1103 | for (row = 0; row < height; row++) { |
Brian | ee80e0b | 2007-10-25 18:50:15 -0600 | [diff] [blame] | 1104 | pipe->get_tile_rgba(pipe, src_surf, srcX, srcY + row, width, 1, data); |
Brian | 038cb56 | 2007-09-26 17:57:15 -0600 | [diff] [blame] | 1105 | |
Brian | 5c83f13 | 2007-09-26 18:54:20 -0600 | [diff] [blame] | 1106 | /* XXX we're ignoring convolution for now */ |
| 1107 | if (ctx->_ImageTransferState) { |
| 1108 | _mesa_apply_rgba_transfer_ops(ctx, |
| 1109 | ctx->_ImageTransferState & ~IMAGE_CONVOLUTION_BIT, |
| 1110 | width, (GLfloat (*)[4])data); |
| 1111 | } |
| 1112 | |
Brian | ee80e0b | 2007-10-25 18:50:15 -0600 | [diff] [blame] | 1113 | pipe->put_tile_rgba(pipe, dest_surf, destX, destY, width, 1, data); |
Brian | 5c83f13 | 2007-09-26 18:54:20 -0600 | [diff] [blame] | 1114 | destY += yStep; |
| 1115 | } |
| 1116 | |
Brian | 038cb56 | 2007-09-26 17:57:15 -0600 | [diff] [blame] | 1117 | |
Brian | c6717a8 | 2007-09-26 18:39:14 -0600 | [diff] [blame] | 1118 | (void) pipe->region_unmap(pipe, dest_surf->region); |
| 1119 | (void) pipe->region_unmap(pipe, src_surf->region); |
| 1120 | |
Brian | 038cb56 | 2007-09-26 17:57:15 -0600 | [diff] [blame] | 1121 | free(data); |
| 1122 | } |
| 1123 | |
| 1124 | |
| 1125 | |
| 1126 | |
| 1127 | /** |
Brian | b27498c | 2007-09-26 17:18:42 -0600 | [diff] [blame] | 1128 | * Do a CopyTex[Sub]Image using an optimized hardware (blit) path. |
| 1129 | * Note that the region to copy has already been clip tested. |
Brian | c6717a8 | 2007-09-26 18:39:14 -0600 | [diff] [blame] | 1130 | * |
| 1131 | * Note: srcY=0=Bottom of renderbuffer |
| 1132 | * |
Brian | b27498c | 2007-09-26 17:18:42 -0600 | [diff] [blame] | 1133 | * \return GL_TRUE if success, GL_FALSE if failure (use a fallback) |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1134 | */ |
Brian | 038cb56 | 2007-09-26 17:57:15 -0600 | [diff] [blame] | 1135 | static void |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1136 | do_copy_texsubimage(GLcontext *ctx, |
Brian | 038cb56 | 2007-09-26 17:57:15 -0600 | [diff] [blame] | 1137 | GLenum target, GLint level, |
| 1138 | GLint destX, GLint destY, GLint destZ, |
| 1139 | GLint srcX, GLint srcY, |
| 1140 | GLsizei width, GLsizei height) |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1141 | { |
Brian | 038cb56 | 2007-09-26 17:57:15 -0600 | [diff] [blame] | 1142 | struct gl_texture_unit *texUnit = |
| 1143 | &ctx->Texture.Unit[ctx->Texture.CurrentUnit]; |
| 1144 | struct gl_texture_object *texObj = |
| 1145 | _mesa_select_tex_object(ctx, texUnit, target); |
| 1146 | struct gl_texture_image *texImage = |
| 1147 | _mesa_select_tex_image(ctx, texObj, target, level); |
| 1148 | struct st_texture_image *stImage = st_texture_image(texImage); |
| 1149 | GLenum baseFormat = texImage->InternalFormat; |
Brian | b27498c | 2007-09-26 17:18:42 -0600 | [diff] [blame] | 1150 | struct gl_framebuffer *fb = ctx->ReadBuffer; |
| 1151 | struct st_renderbuffer *strb; |
| 1152 | struct pipe_context *pipe = ctx->st->pipe; |
| 1153 | struct pipe_region *src_region, *dest_region; |
Brian | b27498c | 2007-09-26 17:18:42 -0600 | [diff] [blame] | 1154 | uint dest_format, src_format; |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1155 | |
Brian | 038cb56 | 2007-09-26 17:57:15 -0600 | [diff] [blame] | 1156 | (void) texImage; |
| 1157 | |
Brian | b27498c | 2007-09-26 17:18:42 -0600 | [diff] [blame] | 1158 | /* determine if copying depth or color data */ |
| 1159 | if (baseFormat == GL_DEPTH_COMPONENT) { |
| 1160 | strb = st_renderbuffer(fb->_DepthBuffer); |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1161 | } |
Brian | b27498c | 2007-09-26 17:18:42 -0600 | [diff] [blame] | 1162 | else if (baseFormat == GL_DEPTH_STENCIL_EXT) { |
| 1163 | strb = st_renderbuffer(fb->_StencilBuffer); |
| 1164 | } |
| 1165 | else { |
| 1166 | /* baseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */ |
| 1167 | strb = st_renderbuffer(fb->_ColorReadBuffer); |
| 1168 | } |
| 1169 | |
| 1170 | assert(strb); |
| 1171 | assert(strb->surface); |
| 1172 | assert(stImage->mt); |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1173 | |
Brian | c6717a8 | 2007-09-26 18:39:14 -0600 | [diff] [blame] | 1174 | if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) { |
| 1175 | srcY = strb->Base.Height - srcY - height; |
| 1176 | } |
| 1177 | |
Brian | b27498c | 2007-09-26 17:18:42 -0600 | [diff] [blame] | 1178 | src_format = strb->surface->format; |
| 1179 | dest_format = stImage->mt->format; |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1180 | |
Brian | b27498c | 2007-09-26 17:18:42 -0600 | [diff] [blame] | 1181 | src_region = strb->surface->region; |
| 1182 | dest_region = stImage->mt->region; |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1183 | |
Brian | 038cb56 | 2007-09-26 17:57:15 -0600 | [diff] [blame] | 1184 | if (src_format == dest_format && |
Brian | 5c83f13 | 2007-09-26 18:54:20 -0600 | [diff] [blame] | 1185 | ctx->_ImageTransferState == 0x0 && |
Brian | 038cb56 | 2007-09-26 17:57:15 -0600 | [diff] [blame] | 1186 | src_region && |
| 1187 | dest_region && |
Michel Dänzer | 11a8016 | 2007-11-28 19:04:54 +0100 | [diff] [blame^] | 1188 | strb->surface->cpp == stImage->mt->cpp) { |
Brian | 038cb56 | 2007-09-26 17:57:15 -0600 | [diff] [blame] | 1189 | /* do blit-style copy */ |
Michel Dänzer | 11a8016 | 2007-11-28 19:04:54 +0100 | [diff] [blame^] | 1190 | struct pipe_surface *dest_surface = pipe->get_tex_surface(pipe, |
| 1191 | stImage->mt, |
| 1192 | stImage->face, |
| 1193 | stImage->level, |
| 1194 | destZ); |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1195 | |
Brian | 038cb56 | 2007-09-26 17:57:15 -0600 | [diff] [blame] | 1196 | /* XXX may need to invert image depending on window |
| 1197 | * vs. user-created FBO |
| 1198 | */ |
| 1199 | |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1200 | #if 0 |
Brian | 038cb56 | 2007-09-26 17:57:15 -0600 | [diff] [blame] | 1201 | /* A bit of fiddling to get the blitter to work with -ve |
| 1202 | * pitches. But we get a nice inverted blit this way, so it's |
| 1203 | * worth it: |
| 1204 | */ |
| 1205 | intelEmitCopyBlit(intel, |
| 1206 | stImage->mt->cpp, |
| 1207 | -src->pitch, |
| 1208 | src->buffer, |
| 1209 | src->height * src->pitch * src->cpp, |
| 1210 | stImage->mt->pitch, |
| 1211 | stImage->mt->region->buffer, |
| 1212 | dest_offset, |
| 1213 | x, y + height, dstx, dsty, width, height, |
| 1214 | GL_COPY); /* ? */ |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1215 | #else |
Brian | b27498c | 2007-09-26 17:18:42 -0600 | [diff] [blame] | 1216 | |
Michel Dänzer | 11a8016 | 2007-11-28 19:04:54 +0100 | [diff] [blame^] | 1217 | pipe->surface_copy(pipe, |
| 1218 | /* dest */ |
| 1219 | dest_surface, |
| 1220 | destX, destY, |
| 1221 | /* src */ |
| 1222 | strb->surface, |
| 1223 | srcX, srcY, |
| 1224 | /* size */ |
| 1225 | width, height); |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1226 | #endif |
Michel Dänzer | 11a8016 | 2007-11-28 19:04:54 +0100 | [diff] [blame^] | 1227 | |
| 1228 | pipe_surface_reference(&dest_surface, NULL); |
Brian | 038cb56 | 2007-09-26 17:57:15 -0600 | [diff] [blame] | 1229 | } |
| 1230 | else { |
| 1231 | fallback_copy_texsubimage(ctx, target, level, |
| 1232 | strb, stImage, baseFormat, |
| 1233 | destX, destY, destZ, |
| 1234 | srcX, srcY, width, height); |
| 1235 | } |
| 1236 | |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1237 | |
| 1238 | #if 0 |
| 1239 | /* GL_SGIS_generate_mipmap -- this can be accelerated now. |
| 1240 | * XXX Add a ctx->Driver.GenerateMipmaps() function? |
| 1241 | */ |
| 1242 | if (level == texObj->BaseLevel && texObj->GenerateMipmap) { |
| 1243 | intel_generate_mipmap(ctx, target, |
| 1244 | &ctx->Texture.Unit[ctx->Texture.CurrentUnit], |
| 1245 | texObj); |
| 1246 | } |
| 1247 | #endif |
| 1248 | |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1249 | } |
| 1250 | |
| 1251 | |
Brian | 038cb56 | 2007-09-26 17:57:15 -0600 | [diff] [blame] | 1252 | |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1253 | static void |
| 1254 | st_CopyTexImage1D(GLcontext * ctx, GLenum target, GLint level, |
| 1255 | GLenum internalFormat, |
| 1256 | GLint x, GLint y, GLsizei width, GLint border) |
| 1257 | { |
| 1258 | struct gl_texture_unit *texUnit = |
| 1259 | &ctx->Texture.Unit[ctx->Texture.CurrentUnit]; |
| 1260 | struct gl_texture_object *texObj = |
| 1261 | _mesa_select_tex_object(ctx, texUnit, target); |
| 1262 | struct gl_texture_image *texImage = |
| 1263 | _mesa_select_tex_image(ctx, texObj, target, level); |
| 1264 | |
Brian | 038cb56 | 2007-09-26 17:57:15 -0600 | [diff] [blame] | 1265 | #if 0 |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1266 | if (border) |
| 1267 | goto fail; |
Brian | 038cb56 | 2007-09-26 17:57:15 -0600 | [diff] [blame] | 1268 | #endif |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1269 | |
| 1270 | /* Setup or redefine the texture object, mipmap tree and texture |
| 1271 | * image. Don't populate yet. |
| 1272 | */ |
| 1273 | ctx->Driver.TexImage1D(ctx, target, level, internalFormat, |
| 1274 | width, border, |
| 1275 | GL_RGBA, CHAN_TYPE, NULL, |
| 1276 | &ctx->DefaultPacking, texObj, texImage); |
| 1277 | |
Brian | 038cb56 | 2007-09-26 17:57:15 -0600 | [diff] [blame] | 1278 | do_copy_texsubimage(ctx, target, level, |
| 1279 | 0, 0, 0, |
| 1280 | x, y, width, 1); |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | |
| 1284 | static void |
| 1285 | st_CopyTexImage2D(GLcontext * ctx, GLenum target, GLint level, |
| 1286 | GLenum internalFormat, |
| 1287 | GLint x, GLint y, GLsizei width, GLsizei height, |
| 1288 | GLint border) |
| 1289 | { |
| 1290 | struct gl_texture_unit *texUnit = |
| 1291 | &ctx->Texture.Unit[ctx->Texture.CurrentUnit]; |
| 1292 | struct gl_texture_object *texObj = |
| 1293 | _mesa_select_tex_object(ctx, texUnit, target); |
| 1294 | struct gl_texture_image *texImage = |
| 1295 | _mesa_select_tex_image(ctx, texObj, target, level); |
| 1296 | |
Brian | 038cb56 | 2007-09-26 17:57:15 -0600 | [diff] [blame] | 1297 | #if 0 |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1298 | if (border) |
| 1299 | goto fail; |
Brian | 038cb56 | 2007-09-26 17:57:15 -0600 | [diff] [blame] | 1300 | #endif |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1301 | |
| 1302 | /* Setup or redefine the texture object, mipmap tree and texture |
| 1303 | * image. Don't populate yet. |
| 1304 | */ |
| 1305 | ctx->Driver.TexImage2D(ctx, target, level, internalFormat, |
| 1306 | width, height, border, |
| 1307 | GL_RGBA, CHAN_TYPE, NULL, |
| 1308 | &ctx->DefaultPacking, texObj, texImage); |
| 1309 | |
| 1310 | |
Brian | 038cb56 | 2007-09-26 17:57:15 -0600 | [diff] [blame] | 1311 | do_copy_texsubimage(ctx, target, level, |
| 1312 | 0, 0, 0, |
| 1313 | x, y, width, height); |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1314 | } |
| 1315 | |
| 1316 | |
| 1317 | static void |
| 1318 | st_CopyTexSubImage1D(GLcontext * ctx, GLenum target, GLint level, |
| 1319 | GLint xoffset, GLint x, GLint y, GLsizei width) |
| 1320 | { |
Brian | 038cb56 | 2007-09-26 17:57:15 -0600 | [diff] [blame] | 1321 | const GLint yoffset = 0, zoffset = 0; |
| 1322 | const GLsizei height = 1; |
| 1323 | do_copy_texsubimage(ctx, target, level, |
| 1324 | xoffset, yoffset, zoffset, |
| 1325 | x, y, width, height); |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1326 | } |
| 1327 | |
| 1328 | |
| 1329 | static void |
| 1330 | st_CopyTexSubImage2D(GLcontext * ctx, GLenum target, GLint level, |
| 1331 | GLint xoffset, GLint yoffset, |
| 1332 | GLint x, GLint y, GLsizei width, GLsizei height) |
| 1333 | { |
Brian | 038cb56 | 2007-09-26 17:57:15 -0600 | [diff] [blame] | 1334 | const GLint zoffset = 0; |
| 1335 | do_copy_texsubimage(ctx, target, level, |
| 1336 | xoffset, yoffset, zoffset, |
| 1337 | x, y, width, height); |
| 1338 | } |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1339 | |
| 1340 | |
Brian | 038cb56 | 2007-09-26 17:57:15 -0600 | [diff] [blame] | 1341 | static void |
| 1342 | st_CopyTexSubImage3D(GLcontext * ctx, GLenum target, GLint level, |
| 1343 | GLint xoffset, GLint yoffset, GLint zoffset, |
| 1344 | GLint x, GLint y, GLsizei width, GLsizei height) |
| 1345 | { |
| 1346 | do_copy_texsubimage(ctx, target, level, |
| 1347 | xoffset, yoffset, zoffset, |
| 1348 | x, y, width, height); |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | |
| 1352 | |
| 1353 | |
| 1354 | /** |
| 1355 | * Compute which mipmap levels that really need to be sent to the hardware. |
| 1356 | * This depends on the base image size, GL_TEXTURE_MIN_LOD, |
| 1357 | * GL_TEXTURE_MAX_LOD, GL_TEXTURE_BASE_LEVEL, and GL_TEXTURE_MAX_LEVEL. |
| 1358 | */ |
| 1359 | static void |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 1360 | calculate_first_last_level(struct st_texture_object *stObj) |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1361 | { |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 1362 | struct gl_texture_object *tObj = &stObj->base; |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1363 | const struct gl_texture_image *const baseImage = |
| 1364 | tObj->Image[0][tObj->BaseLevel]; |
| 1365 | |
| 1366 | /* These must be signed values. MinLod and MaxLod can be negative numbers, |
| 1367 | * and having firstLevel and lastLevel as signed prevents the need for |
| 1368 | * extra sign checks. |
| 1369 | */ |
| 1370 | int firstLevel; |
| 1371 | int lastLevel; |
| 1372 | |
| 1373 | /* Yes, this looks overly complicated, but it's all needed. |
| 1374 | */ |
| 1375 | switch (tObj->Target) { |
| 1376 | case GL_TEXTURE_1D: |
| 1377 | case GL_TEXTURE_2D: |
| 1378 | case GL_TEXTURE_3D: |
| 1379 | case GL_TEXTURE_CUBE_MAP: |
| 1380 | if (tObj->MinFilter == GL_NEAREST || tObj->MinFilter == GL_LINEAR) { |
| 1381 | /* GL_NEAREST and GL_LINEAR only care about GL_TEXTURE_BASE_LEVEL. |
| 1382 | */ |
| 1383 | firstLevel = lastLevel = tObj->BaseLevel; |
| 1384 | } |
| 1385 | else { |
| 1386 | firstLevel = tObj->BaseLevel + (GLint) (tObj->MinLod + 0.5); |
| 1387 | firstLevel = MAX2(firstLevel, tObj->BaseLevel); |
| 1388 | lastLevel = tObj->BaseLevel + (GLint) (tObj->MaxLod + 0.5); |
| 1389 | lastLevel = MAX2(lastLevel, tObj->BaseLevel); |
| 1390 | lastLevel = MIN2(lastLevel, tObj->BaseLevel + baseImage->MaxLog2); |
| 1391 | lastLevel = MIN2(lastLevel, tObj->MaxLevel); |
| 1392 | lastLevel = MAX2(firstLevel, lastLevel); /* need at least one level */ |
| 1393 | } |
| 1394 | break; |
| 1395 | case GL_TEXTURE_RECTANGLE_NV: |
| 1396 | case GL_TEXTURE_4D_SGIS: |
| 1397 | firstLevel = lastLevel = 0; |
| 1398 | break; |
| 1399 | default: |
| 1400 | return; |
| 1401 | } |
| 1402 | |
| 1403 | /* save these values */ |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 1404 | stObj->firstLevel = firstLevel; |
| 1405 | stObj->lastLevel = lastLevel; |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1406 | } |
| 1407 | |
| 1408 | |
| 1409 | static void |
| 1410 | copy_image_data_to_tree(struct pipe_context *pipe, |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 1411 | struct st_texture_object *stObj, |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1412 | struct st_texture_image *stImage) |
| 1413 | { |
| 1414 | if (stImage->mt) { |
| 1415 | /* Copy potentially with the blitter: |
| 1416 | */ |
| 1417 | st_miptree_image_copy(pipe, |
Brian | 28b315d | 2007-09-27 16:31:13 -0600 | [diff] [blame] | 1418 | stObj->mt, /* dest miptree */ |
| 1419 | stImage->face, stImage->level, |
| 1420 | stImage->mt /* src miptree */ |
| 1421 | ); |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1422 | |
| 1423 | st_miptree_release(pipe, &stImage->mt); |
| 1424 | } |
| 1425 | else { |
| 1426 | assert(stImage->base.Data != NULL); |
| 1427 | |
| 1428 | /* More straightforward upload. |
| 1429 | */ |
| 1430 | st_miptree_image_data(pipe, |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 1431 | stObj->mt, |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1432 | stImage->face, |
| 1433 | stImage->level, |
| 1434 | stImage->base.Data, |
| 1435 | stImage->base.RowStride, |
| 1436 | stImage->base.RowStride * |
| 1437 | stImage->base.Height); |
| 1438 | _mesa_align_free(stImage->base.Data); |
| 1439 | stImage->base.Data = NULL; |
| 1440 | } |
| 1441 | |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 1442 | st_miptree_reference(&stImage->mt, stObj->mt); |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1443 | } |
| 1444 | |
| 1445 | |
| 1446 | /* |
| 1447 | */ |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 1448 | GLboolean |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1449 | st_finalize_mipmap_tree(GLcontext *ctx, |
| 1450 | struct pipe_context *pipe, GLuint unit, |
| 1451 | GLboolean *needFlush) |
| 1452 | { |
| 1453 | struct gl_texture_object *tObj = ctx->Texture.Unit[unit]._Current; |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 1454 | struct st_texture_object *stObj = st_texture_object(tObj); |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1455 | int comp_byte = 0; |
| 1456 | int cpp; |
| 1457 | |
| 1458 | GLuint face, i; |
| 1459 | GLuint nr_faces = 0; |
| 1460 | struct st_texture_image *firstImage; |
| 1461 | |
| 1462 | *needFlush = GL_FALSE; |
| 1463 | |
| 1464 | /* We know/require this is true by now: |
| 1465 | */ |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 1466 | assert(stObj->base._Complete); |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1467 | |
| 1468 | /* What levels must the tree include at a minimum? |
| 1469 | */ |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 1470 | calculate_first_last_level(stObj); |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1471 | firstImage = |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 1472 | st_texture_image(stObj->base.Image[0][stObj->firstLevel]); |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1473 | |
| 1474 | /* Fallback case: |
| 1475 | */ |
| 1476 | if (firstImage->base.Border) { |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 1477 | if (stObj->mt) { |
| 1478 | st_miptree_release(pipe, &stObj->mt); |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1479 | } |
| 1480 | return GL_FALSE; |
| 1481 | } |
| 1482 | |
| 1483 | |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 1484 | /* If both firstImage and stObj have a tree which can contain |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1485 | * all active images, favour firstImage. Note that because of the |
| 1486 | * completeness requirement, we know that the image dimensions |
| 1487 | * will match. |
| 1488 | */ |
| 1489 | if (firstImage->mt && |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 1490 | firstImage->mt != stObj->mt && |
| 1491 | firstImage->mt->first_level <= stObj->firstLevel && |
| 1492 | firstImage->mt->last_level >= stObj->lastLevel) { |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1493 | |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 1494 | if (stObj->mt) |
| 1495 | st_miptree_release(pipe, &stObj->mt); |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1496 | |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 1497 | st_miptree_reference(&stObj->mt, firstImage->mt); |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | if (firstImage->base.IsCompressed) { |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 1501 | comp_byte = compressed_num_bytes(firstImage->base.TexFormat->MesaFormat); |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1502 | cpp = comp_byte; |
| 1503 | } |
Brian | 5cb0d74 | 2007-10-17 14:57:15 -0600 | [diff] [blame] | 1504 | else { |
| 1505 | cpp = firstImage->base.TexFormat->TexelBytes; |
| 1506 | } |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1507 | |
| 1508 | /* Check tree can hold all active levels. Check tree matches |
| 1509 | * target, imageFormat, etc. |
| 1510 | * |
| 1511 | * XXX: For some layouts (eg i945?), the test might have to be |
| 1512 | * first_level == firstLevel, as the tree isn't valid except at the |
| 1513 | * original start level. Hope to get around this by |
| 1514 | * programming minLod, maxLod, baseLevel into the hardware and |
| 1515 | * leaving the tree alone. |
| 1516 | */ |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 1517 | if (stObj->mt && |
Brian | 5390a43 | 2007-08-15 19:23:58 -0600 | [diff] [blame] | 1518 | (stObj->mt->target != gl_target_to_pipe(stObj->base.Target) || |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 1519 | stObj->mt->internal_format != firstImage->base.InternalFormat || |
| 1520 | stObj->mt->first_level != stObj->firstLevel || |
| 1521 | stObj->mt->last_level != stObj->lastLevel || |
| 1522 | stObj->mt->width0 != firstImage->base.Width || |
| 1523 | stObj->mt->height0 != firstImage->base.Height || |
| 1524 | stObj->mt->depth0 != firstImage->base.Depth || |
| 1525 | stObj->mt->cpp != cpp || |
| 1526 | stObj->mt->compressed != firstImage->base.IsCompressed)) { |
| 1527 | st_miptree_release(pipe, &stObj->mt); |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1528 | } |
| 1529 | |
| 1530 | |
| 1531 | /* May need to create a new tree: |
| 1532 | */ |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 1533 | if (!stObj->mt) { |
| 1534 | stObj->mt = st_miptree_create(pipe, |
Brian | 5390a43 | 2007-08-15 19:23:58 -0600 | [diff] [blame] | 1535 | gl_target_to_pipe(stObj->base.Target), |
Brian | b245840 | 2007-08-07 18:18:54 -0600 | [diff] [blame] | 1536 | firstImage->base.InternalFormat, |
| 1537 | stObj->firstLevel, |
| 1538 | stObj->lastLevel, |
| 1539 | firstImage->base.Width, |
| 1540 | firstImage->base.Height, |
| 1541 | firstImage->base.Depth, |
| 1542 | cpp, |
| 1543 | comp_byte); |
| 1544 | |
| 1545 | stObj->mt->format |
Brian | 9cf9aa1 | 2007-08-10 13:02:51 -0600 | [diff] [blame] | 1546 | = st_mesa_format_to_pipe_format(firstImage->base.TexFormat->MesaFormat); |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1547 | } |
| 1548 | |
| 1549 | /* Pull in any images not in the object's tree: |
| 1550 | */ |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 1551 | nr_faces = (stObj->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1; |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1552 | for (face = 0; face < nr_faces; face++) { |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 1553 | for (i = stObj->firstLevel; i <= stObj->lastLevel; i++) { |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1554 | struct st_texture_image *stImage = |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 1555 | st_texture_image(stObj->base.Image[face][i]); |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1556 | |
| 1557 | /* Need to import images in main memory or held in other trees. |
| 1558 | */ |
Brian | 14b9834 | 2007-08-07 16:42:08 -0600 | [diff] [blame] | 1559 | if (stObj->mt != stImage->mt) { |
| 1560 | copy_image_data_to_tree(pipe, stObj, stImage); |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1561 | *needFlush = GL_TRUE; |
| 1562 | } |
| 1563 | } |
| 1564 | } |
| 1565 | |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1566 | |
| 1567 | return GL_TRUE; |
| 1568 | } |
| 1569 | |
| 1570 | |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1571 | |
| 1572 | |
Brian | 6da9234 | 2007-08-06 20:53:28 +0100 | [diff] [blame] | 1573 | void |
| 1574 | st_init_texture_functions(struct dd_function_table *functions) |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1575 | { |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1576 | functions->ChooseTextureFormat = st_ChooseTextureFormat; |
| 1577 | functions->TexImage1D = st_TexImage1D; |
| 1578 | functions->TexImage2D = st_TexImage2D; |
| 1579 | functions->TexImage3D = st_TexImage3D; |
| 1580 | functions->TexSubImage1D = st_TexSubImage1D; |
| 1581 | functions->TexSubImage2D = st_TexSubImage2D; |
| 1582 | functions->TexSubImage3D = st_TexSubImage3D; |
| 1583 | functions->CopyTexImage1D = st_CopyTexImage1D; |
| 1584 | functions->CopyTexImage2D = st_CopyTexImage2D; |
| 1585 | functions->CopyTexSubImage1D = st_CopyTexSubImage1D; |
| 1586 | functions->CopyTexSubImage2D = st_CopyTexSubImage2D; |
Brian | 038cb56 | 2007-09-26 17:57:15 -0600 | [diff] [blame] | 1587 | functions->CopyTexSubImage3D = st_CopyTexSubImage3D; |
| 1588 | |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1589 | functions->GetTexImage = st_GetTexImage; |
| 1590 | |
| 1591 | /* compressed texture functions */ |
| 1592 | functions->CompressedTexImage2D = st_CompressedTexImage2D; |
| 1593 | functions->GetCompressedTexImage = st_GetCompressedTexImage; |
| 1594 | |
| 1595 | functions->NewTextureObject = st_NewTextureObject; |
| 1596 | functions->NewTextureImage = st_NewTextureImage; |
| 1597 | functions->DeleteTexture = st_DeleteTextureObject; |
| 1598 | functions->FreeTexImageData = st_FreeTextureImageData; |
| 1599 | functions->UpdateTexturePalette = 0; |
| 1600 | functions->IsTextureResident = st_IsTextureResident; |
| 1601 | |
| 1602 | functions->TextureMemCpy = do_memcpy; |
Brian | f8549e8 | 2007-11-01 14:14:30 -0600 | [diff] [blame] | 1603 | |
| 1604 | /* XXX Temporary until we can query pipe's texture sizes */ |
| 1605 | functions->TestProxyTexImage = _mesa_test_proxy_teximage; |
Brian | 24df8f8 | 2007-08-06 15:48:42 -0600 | [diff] [blame] | 1606 | } |