blob: 461705119fae9bc323e5e6344d5a41d56a048ae9 [file] [log] [blame]
Brian24df8f82007-08-06 15:48:42 -06001/**************************************************************************
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"
Brianb27498c2007-09-26 17:18:42 -060040#include "state_tracker/st_cb_fbo.h"
Brian24df8f82007-08-06 15:48:42 -060041#include "state_tracker/st_cb_texture.h"
Brianf8ab2472007-08-10 08:37:15 +010042#include "state_tracker/st_format.h"
Brian24df8f82007-08-06 15:48:42 -060043#include "state_tracker/st_mipmap_tree.h"
44
45#include "pipe/p_context.h"
Brianb2458402007-08-07 18:18:54 -060046#include "pipe/p_defines.h"
Michel Dänzer11a80162007-11-28 19:04:54 +010047#include "pipe/p_inlines.h"
Brian24df8f82007-08-06 15:48:42 -060048
49
50#define DBG if (0) printf
51
52
53struct 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
79struct 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
98static INLINE struct st_texture_object *
99st_texture_object(struct gl_texture_object *obj)
100{
101 return (struct st_texture_object *) obj;
102}
103
104static INLINE struct st_texture_image *
105st_texture_image(struct gl_texture_image *img)
106{
107 return (struct st_texture_image *) img;
108}
109
110
Briand78dab12007-08-07 15:12:22 -0600111struct pipe_mipmap_tree *
112st_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
Brian5390a432007-08-15 19:23:58 -0600119static unsigned
120gl_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
Brian24df8f82007-08-06 15:48:42 -0600143static int
Brian14b98342007-08-07 16:42:08 -0600144compressed_num_bytes(GLuint mesaFormat)
Brian24df8f82007-08-06 15:48:42 -0600145{
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
Brian24df8f82007-08-06 15:48:42 -0600167
168
169static GLboolean
170st_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
186static struct gl_texture_image *
187st_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
195static struct gl_texture_object *
196st_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
206static void
207st_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
220static void
221st_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__)
247static 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 */
279static void *
280do_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
298static int
299logbase2(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 */
323static void
324guess_and_alloc_mipmap_tree(struct pipe_context *pipe,
Brian14b98342007-08-07 16:42:08 -0600325 struct st_texture_object *stObj,
326 struct st_texture_image *stImage)
Brian24df8f82007-08-06 15:48:42 -0600327{
328 GLuint firstLevel;
329 GLuint lastLevel;
Brian14b98342007-08-07 16:42:08 -0600330 GLuint width = stImage->base.Width;
331 GLuint height = stImage->base.Height;
332 GLuint depth = stImage->base.Depth;
Brian24df8f82007-08-06 15:48:42 -0600333 GLuint l2width, l2height, l2depth;
334 GLuint i, comp_byte = 0;
335
336 DBG("%s\n", __FUNCTION__);
337
Brian14b98342007-08-07 16:42:08 -0600338 if (stImage->base.Border)
Brian24df8f82007-08-06 15:48:42 -0600339 return;
340
Brian14b98342007-08-07 16:42:08 -0600341 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)))
Brian24df8f82007-08-06 15:48:42 -0600347 return;
348
349 /* If this image disrespects BaseLevel, allocate from level zero.
350 * Usually BaseLevel == 0, so it's unlikely to happen.
351 */
Brian14b98342007-08-07 16:42:08 -0600352 if (stImage->level < stObj->base.BaseLevel)
Brian24df8f82007-08-06 15:48:42 -0600353 firstLevel = 0;
354 else
Brian14b98342007-08-07 16:42:08 -0600355 firstLevel = stObj->base.BaseLevel;
Brian24df8f82007-08-06 15:48:42 -0600356
357
358 /* Figure out image dimensions at start level.
359 */
Brian14b98342007-08-07 16:42:08 -0600360 for (i = stImage->level; i > firstLevel; i--) {
Brian24df8f82007-08-06 15:48:42 -0600361 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 */
Brian14b98342007-08-07 16:42:08 -0600373 if ((stObj->base.MinFilter == GL_NEAREST ||
374 stObj->base.MinFilter == GL_LINEAR) &&
375 stImage->level == firstLevel) {
Brian24df8f82007-08-06 15:48:42 -0600376 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
Brian14b98342007-08-07 16:42:08 -0600385 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,
Brian5390a432007-08-15 19:23:58 -0600389 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);
Brian24df8f82007-08-06 15:48:42 -0600398
Brianb2458402007-08-07 18:18:54 -0600399 stObj->mt->format
Brian9cf9aa12007-08-10 13:02:51 -0600400 = st_mesa_format_to_pipe_format(stImage->base.TexFormat->MesaFormat);
Brianb2458402007-08-07 18:18:54 -0600401
Brian24df8f82007-08-06 15:48:42 -0600402 DBG("%s - success\n", __FUNCTION__);
403}
404
405
406
407
408static GLuint
409target_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 */
429static GLboolean
430check_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 */
456static GLboolean
457try_pbo_upload(GLcontext *ctx,
Brian14b98342007-08-07 16:42:08 -0600458 struct st_texture_image *stImage,
Brian24df8f82007-08-06 15:48:42 -0600459 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
Brian14b98342007-08-07 16:42:08 -0600485 dst_offset = st_miptree_image_offset(stImage->mt,
486 stImage->face,
487 stImage->level);
Brian24df8f82007-08-06 15:48:42 -0600488
Brian14b98342007-08-07 16:42:08 -0600489 dst_stride = stImage->mt->pitch;
Brian24df8f82007-08-06 15:48:42 -0600490
Brian24df8f82007-08-06 15:48:42 -0600491 {
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 =
Brian14b98342007-08-07 16:42:08 -0600498 (struct _DriBufferObject *)stImage->mt->region->buffer;
Brian24df8f82007-08-06 15:48:42 -0600499
500
501 intelEmitCopyBlit(intel,
Brian14b98342007-08-07 16:42:08 -0600502 stImage->mt->cpp,
Brian24df8f82007-08-06 15:48:42 -0600503 src_stride, src_buffer, src_offset,
504 dst_stride, dst_buffer, dst_offset,
505 0, 0, 0, 0, width, height,
506 GL_COPY);
Brian24df8f82007-08-06 15:48:42 -0600507 }
Brian24df8f82007-08-06 15:48:42 -0600508
509 return GL_TRUE;
510#endif
511}
512
513
514
515static GLboolean
516try_pbo_zcopy(GLcontext *ctx,
Brian14b98342007-08-07 16:42:08 -0600517 struct st_texture_image *stImage,
Brian24df8f82007-08-06 15:48:42 -0600518 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
531static void
532st_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;
Brian14b98342007-08-07 16:42:08 -0600544 struct st_texture_object *stObj = st_texture_object(texObj);
545 struct st_texture_image *stImage = st_texture_image(texImage);
Brian24df8f82007-08-06 15:48:42 -0600546 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
Brian14b98342007-08-07 16:42:08 -0600555 stImage->face = target_to_face(target);
556 stImage->level = level;
Brian24df8f82007-08-06 15:48:42 -0600557
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 */
Brian14b98342007-08-07 16:42:08 -0600592 if (stImage->mt) {
593 st_miptree_release(pipe, &stImage->mt);
Brian24df8f82007-08-06 15:48:42 -0600594 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 */
Brian14b98342007-08-07 16:42:08 -0600604 if (stObj->mt &&
605 stObj->mt->first_level == level &&
606 stObj->mt->last_level == level &&
Brian5390a432007-08-15 19:23:58 -0600607 stObj->mt->target != PIPE_TEXTURE_CUBE &&
Brian14b98342007-08-07 16:42:08 -0600608 !st_miptree_match_image(stObj->mt, &stImage->base,
609 stImage->face, stImage->level)) {
Brian24df8f82007-08-06 15:48:42 -0600610
611 DBG("release it\n");
Brian14b98342007-08-07 16:42:08 -0600612 st_miptree_release(pipe, &stObj->mt);
613 assert(!stObj->mt);
Brian24df8f82007-08-06 15:48:42 -0600614 }
615
Brian14b98342007-08-07 16:42:08 -0600616 if (!stObj->mt) {
617 guess_and_alloc_mipmap_tree(pipe, stObj, stImage);
618 if (!stObj->mt) {
Brian24df8f82007-08-06 15:48:42 -0600619 DBG("guess_and_alloc_mipmap_tree: failed\n");
620 }
621 }
622
Brian14b98342007-08-07 16:42:08 -0600623 assert(!stImage->mt);
Brian24df8f82007-08-06 15:48:42 -0600624
Brian14b98342007-08-07 16:42:08 -0600625 if (stObj->mt &&
626 st_miptree_match_image(stObj->mt, &stImage->base,
627 stImage->face, stImage->level)) {
Brian24df8f82007-08-06 15:48:42 -0600628
Brian14b98342007-08-07 16:42:08 -0600629 st_miptree_reference(&stImage->mt, stObj->mt);
630 assert(stImage->mt);
Brian24df8f82007-08-06 15:48:42 -0600631 }
632
Brian14b98342007-08-07 16:42:08 -0600633 if (!stImage->mt)
Brian24df8f82007-08-06 15:48:42 -0600634 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 &&
Brian14b98342007-08-07 16:42:08 -0600640 stImage->mt &&
Brian24df8f82007-08-06 15:48:42 -0600641 intel_buffer_object(unpack->BufferObj) &&
642 check_pbo_format(internalFormat, format,
Brian14b98342007-08-07 16:42:08 -0600643 type, stImage->base.TexFormat)) {
Brian24df8f82007-08-06 15:48:42 -0600644
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 */
Brian14b98342007-08-07 16:42:08 -0600653 if (stObj->mt == stImage->mt &&
654 stObj->mt->first_level == level &&
655 stObj->mt->last_level == level) {
Brian24df8f82007-08-06 15:48:42 -0600656
Brian14b98342007-08-07 16:42:08 -0600657 if (try_pbo_zcopy(intel, stImage, unpack,
Brian24df8f82007-08-06 15:48:42 -0600658 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 */
Brian14b98342007-08-07 16:42:08 -0600669 if (try_pbo_upload(intel, stImage, unpack,
Brian24df8f82007-08-06 15:48:42 -0600670 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
Brian14b98342007-08-07 16:42:08 -0600701 if (stImage->mt) {
Brian24df8f82007-08-06 15:48:42 -0600702 texImage->Data = st_miptree_image_map(pipe,
Brian14b98342007-08-07 16:42:08 -0600703 stImage->mt,
704 stImage->face,
705 stImage->level,
Brian24df8f82007-08-06 15:48:42 -0600706 &dstRowStride,
Brian14b98342007-08-07 16:42:08 -0600707 stImage->base.ImageOffsets);
Brian24df8f82007-08-06 15:48:42 -0600708 }
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
Brian14b98342007-08-07 16:42:08 -0600749 if (stImage->mt) {
750 st_miptree_image_unmap(pipe, stImage->mt);
Brian24df8f82007-08-06 15:48:42 -0600751 texImage->Data = NULL;
752 }
753
754#if 0
Brian24df8f82007-08-06 15:48:42 -0600755 /* 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
766static void
767st_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
783static void
784st_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
799static void
800st_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
815static void
816st_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 */
833static void
834st_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
889static void
890st_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
900static void
901st_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
913static void
914st_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
Brian24df8f82007-08-06 15:48:42 -0600932 pixels =
933 _mesa_validate_pbo_teximage(ctx, dims, width, height, depth, format,
934 type, pixels, packing, "glTexSubImage2D");
935 if (!pixels)
936 return;
937
Brian24df8f82007-08-06 15:48:42 -0600938 /* 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 }
Brian24df8f82007-08-06 15:48:42 -0600977}
978
979
980
981static void
982st_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{
Brianb2458402007-08-07 18:18:54 -0600993 st_TexSubimage(ctx, 3, target, level,
994 xoffset, yoffset, zoffset,
995 width, height, depth,
996 format, type, pixels, packing, texObj, texImage);
Brian24df8f82007-08-06 15:48:42 -0600997}
998
999
1000
1001static void
1002st_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{
Brianb2458402007-08-07 18:18:54 -06001013 st_TexSubimage(ctx, 2, target, level,
1014 xoffset, yoffset, 0,
1015 width, height, 1,
1016 format, type, pixels, packing, texObj, texImage);
Brian24df8f82007-08-06 15:48:42 -06001017}
1018
1019
1020static void
1021st_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{
Brianb2458402007-08-07 18:18:54 -06001032 st_TexSubimage(ctx, 1, target, level,
1033 xoffset, 0, 0,
1034 width, 1, 1,
1035 format, type, pixels, packing, texObj, texImage);
Brian24df8f82007-08-06 15:48:42 -06001036}
1037
1038
1039
1040/**
Brian038cb562007-09-26 17:57:15 -06001041 * 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 */
1046static uint
1047texture_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
Brian5c83f132007-09-26 18:54:20 -06001060 * dest region and using get_tile()/put_tile() to access the pixels/texels.
Brianc6717a82007-09-26 18:39:14 -06001061 *
1062 * Note: srcY=0=TOP of renderbuffer
Brian038cb562007-09-26 17:57:15 -06001063 */
1064static void
1065fallback_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;
Brian5c83f132007-09-26 18:54:20 -06001080 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 }
Brian038cb562007-09-26 17:57:15 -06001090
1091 src_surf = strb->surface;
1092
1093 dest_surf = pipe->get_tex_surface(pipe, mt,
1094 face, level, destZ);
1095
Brianc6717a82007-09-26 18:39:14 -06001096 (void) pipe->region_map(pipe, dest_surf->region);
1097 (void) pipe->region_map(pipe, src_surf->region);
1098
Brian5c83f132007-09-26 18:54:20 -06001099 /* buffer for one row */
1100 data = (GLfloat *) malloc(width * 4 * sizeof(GLfloat));
Brian038cb562007-09-26 17:57:15 -06001101
Brian5c83f132007-09-26 18:54:20 -06001102 /* do copy row by row */
1103 for (row = 0; row < height; row++) {
Brianee80e0b2007-10-25 18:50:15 -06001104 pipe->get_tile_rgba(pipe, src_surf, srcX, srcY + row, width, 1, data);
Brian038cb562007-09-26 17:57:15 -06001105
Brian5c83f132007-09-26 18:54:20 -06001106 /* 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
Brianee80e0b2007-10-25 18:50:15 -06001113 pipe->put_tile_rgba(pipe, dest_surf, destX, destY, width, 1, data);
Brian5c83f132007-09-26 18:54:20 -06001114 destY += yStep;
1115 }
1116
Brian038cb562007-09-26 17:57:15 -06001117
Brianc6717a82007-09-26 18:39:14 -06001118 (void) pipe->region_unmap(pipe, dest_surf->region);
1119 (void) pipe->region_unmap(pipe, src_surf->region);
1120
Brian038cb562007-09-26 17:57:15 -06001121 free(data);
1122}
1123
1124
1125
1126
1127/**
Brianb27498c2007-09-26 17:18:42 -06001128 * Do a CopyTex[Sub]Image using an optimized hardware (blit) path.
1129 * Note that the region to copy has already been clip tested.
Brianc6717a82007-09-26 18:39:14 -06001130 *
1131 * Note: srcY=0=Bottom of renderbuffer
1132 *
Brianb27498c2007-09-26 17:18:42 -06001133 * \return GL_TRUE if success, GL_FALSE if failure (use a fallback)
Brian24df8f82007-08-06 15:48:42 -06001134 */
Brian038cb562007-09-26 17:57:15 -06001135static void
Brian24df8f82007-08-06 15:48:42 -06001136do_copy_texsubimage(GLcontext *ctx,
Brian038cb562007-09-26 17:57:15 -06001137 GLenum target, GLint level,
1138 GLint destX, GLint destY, GLint destZ,
1139 GLint srcX, GLint srcY,
1140 GLsizei width, GLsizei height)
Brian24df8f82007-08-06 15:48:42 -06001141{
Brian038cb562007-09-26 17:57:15 -06001142 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;
Brianb27498c2007-09-26 17:18:42 -06001150 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;
Brianb27498c2007-09-26 17:18:42 -06001154 uint dest_format, src_format;
Brian24df8f82007-08-06 15:48:42 -06001155
Brian038cb562007-09-26 17:57:15 -06001156 (void) texImage;
1157
Brianb27498c2007-09-26 17:18:42 -06001158 /* determine if copying depth or color data */
1159 if (baseFormat == GL_DEPTH_COMPONENT) {
1160 strb = st_renderbuffer(fb->_DepthBuffer);
Brian24df8f82007-08-06 15:48:42 -06001161 }
Brianb27498c2007-09-26 17:18:42 -06001162 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);
Brian24df8f82007-08-06 15:48:42 -06001173
Brianc6717a82007-09-26 18:39:14 -06001174 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1175 srcY = strb->Base.Height - srcY - height;
1176 }
1177
Brianb27498c2007-09-26 17:18:42 -06001178 src_format = strb->surface->format;
1179 dest_format = stImage->mt->format;
Brian24df8f82007-08-06 15:48:42 -06001180
Brianb27498c2007-09-26 17:18:42 -06001181 src_region = strb->surface->region;
1182 dest_region = stImage->mt->region;
Brian24df8f82007-08-06 15:48:42 -06001183
Brian038cb562007-09-26 17:57:15 -06001184 if (src_format == dest_format &&
Brian5c83f132007-09-26 18:54:20 -06001185 ctx->_ImageTransferState == 0x0 &&
Brian038cb562007-09-26 17:57:15 -06001186 src_region &&
1187 dest_region &&
Michel Dänzer11a80162007-11-28 19:04:54 +01001188 strb->surface->cpp == stImage->mt->cpp) {
Brian038cb562007-09-26 17:57:15 -06001189 /* do blit-style copy */
Michel Dänzer11a80162007-11-28 19:04:54 +01001190 struct pipe_surface *dest_surface = pipe->get_tex_surface(pipe,
1191 stImage->mt,
1192 stImage->face,
1193 stImage->level,
1194 destZ);
Brian24df8f82007-08-06 15:48:42 -06001195
Brian038cb562007-09-26 17:57:15 -06001196 /* XXX may need to invert image depending on window
1197 * vs. user-created FBO
1198 */
1199
Brian24df8f82007-08-06 15:48:42 -06001200#if 0
Brian038cb562007-09-26 17:57:15 -06001201 /* 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); /* ? */
Brian24df8f82007-08-06 15:48:42 -06001215#else
Brianb27498c2007-09-26 17:18:42 -06001216
Michel Dänzer11a80162007-11-28 19:04:54 +01001217 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);
Brian24df8f82007-08-06 15:48:42 -06001226#endif
Michel Dänzer11a80162007-11-28 19:04:54 +01001227
1228 pipe_surface_reference(&dest_surface, NULL);
Brian038cb562007-09-26 17:57:15 -06001229 }
1230 else {
1231 fallback_copy_texsubimage(ctx, target, level,
1232 strb, stImage, baseFormat,
1233 destX, destY, destZ,
1234 srcX, srcY, width, height);
1235 }
1236
Brian24df8f82007-08-06 15:48:42 -06001237
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
Brian24df8f82007-08-06 15:48:42 -06001249}
1250
1251
Brian038cb562007-09-26 17:57:15 -06001252
Brian24df8f82007-08-06 15:48:42 -06001253static void
1254st_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
Brian038cb562007-09-26 17:57:15 -06001265#if 0
Brian24df8f82007-08-06 15:48:42 -06001266 if (border)
1267 goto fail;
Brian038cb562007-09-26 17:57:15 -06001268#endif
Brian24df8f82007-08-06 15:48:42 -06001269
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
Brian038cb562007-09-26 17:57:15 -06001278 do_copy_texsubimage(ctx, target, level,
1279 0, 0, 0,
1280 x, y, width, 1);
Brian24df8f82007-08-06 15:48:42 -06001281}
1282
1283
1284static void
1285st_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
Brian038cb562007-09-26 17:57:15 -06001297#if 0
Brian24df8f82007-08-06 15:48:42 -06001298 if (border)
1299 goto fail;
Brian038cb562007-09-26 17:57:15 -06001300#endif
Brian24df8f82007-08-06 15:48:42 -06001301
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
Brian038cb562007-09-26 17:57:15 -06001311 do_copy_texsubimage(ctx, target, level,
1312 0, 0, 0,
1313 x, y, width, height);
Brian24df8f82007-08-06 15:48:42 -06001314}
1315
1316
1317static void
1318st_CopyTexSubImage1D(GLcontext * ctx, GLenum target, GLint level,
1319 GLint xoffset, GLint x, GLint y, GLsizei width)
1320{
Brian038cb562007-09-26 17:57:15 -06001321 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);
Brian24df8f82007-08-06 15:48:42 -06001326}
1327
1328
1329static void
1330st_CopyTexSubImage2D(GLcontext * ctx, GLenum target, GLint level,
1331 GLint xoffset, GLint yoffset,
1332 GLint x, GLint y, GLsizei width, GLsizei height)
1333{
Brian038cb562007-09-26 17:57:15 -06001334 const GLint zoffset = 0;
1335 do_copy_texsubimage(ctx, target, level,
1336 xoffset, yoffset, zoffset,
1337 x, y, width, height);
1338}
Brian24df8f82007-08-06 15:48:42 -06001339
1340
Brian038cb562007-09-26 17:57:15 -06001341static void
1342st_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);
Brian24df8f82007-08-06 15:48:42 -06001349}
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 */
1359static void
Brian14b98342007-08-07 16:42:08 -06001360calculate_first_last_level(struct st_texture_object *stObj)
Brian24df8f82007-08-06 15:48:42 -06001361{
Brian14b98342007-08-07 16:42:08 -06001362 struct gl_texture_object *tObj = &stObj->base;
Brian24df8f82007-08-06 15:48:42 -06001363 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 */
Brian14b98342007-08-07 16:42:08 -06001404 stObj->firstLevel = firstLevel;
1405 stObj->lastLevel = lastLevel;
Brian24df8f82007-08-06 15:48:42 -06001406}
1407
1408
1409static void
1410copy_image_data_to_tree(struct pipe_context *pipe,
Brian14b98342007-08-07 16:42:08 -06001411 struct st_texture_object *stObj,
Brian24df8f82007-08-06 15:48:42 -06001412 struct st_texture_image *stImage)
1413{
1414 if (stImage->mt) {
1415 /* Copy potentially with the blitter:
1416 */
1417 st_miptree_image_copy(pipe,
Brian28b315d2007-09-27 16:31:13 -06001418 stObj->mt, /* dest miptree */
1419 stImage->face, stImage->level,
1420 stImage->mt /* src miptree */
1421 );
Brian24df8f82007-08-06 15:48:42 -06001422
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,
Brian14b98342007-08-07 16:42:08 -06001431 stObj->mt,
Brian24df8f82007-08-06 15:48:42 -06001432 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
Brian14b98342007-08-07 16:42:08 -06001442 st_miptree_reference(&stImage->mt, stObj->mt);
Brian24df8f82007-08-06 15:48:42 -06001443}
1444
1445
1446/*
1447 */
Brian14b98342007-08-07 16:42:08 -06001448GLboolean
Brian24df8f82007-08-06 15:48:42 -06001449st_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;
Brian14b98342007-08-07 16:42:08 -06001454 struct st_texture_object *stObj = st_texture_object(tObj);
Brian24df8f82007-08-06 15:48:42 -06001455 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 */
Brian14b98342007-08-07 16:42:08 -06001466 assert(stObj->base._Complete);
Brian24df8f82007-08-06 15:48:42 -06001467
1468 /* What levels must the tree include at a minimum?
1469 */
Brian14b98342007-08-07 16:42:08 -06001470 calculate_first_last_level(stObj);
Brian24df8f82007-08-06 15:48:42 -06001471 firstImage =
Brian14b98342007-08-07 16:42:08 -06001472 st_texture_image(stObj->base.Image[0][stObj->firstLevel]);
Brian24df8f82007-08-06 15:48:42 -06001473
1474 /* Fallback case:
1475 */
1476 if (firstImage->base.Border) {
Brian14b98342007-08-07 16:42:08 -06001477 if (stObj->mt) {
1478 st_miptree_release(pipe, &stObj->mt);
Brian24df8f82007-08-06 15:48:42 -06001479 }
1480 return GL_FALSE;
1481 }
1482
1483
Brian14b98342007-08-07 16:42:08 -06001484 /* If both firstImage and stObj have a tree which can contain
Brian24df8f82007-08-06 15:48:42 -06001485 * 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 &&
Brian14b98342007-08-07 16:42:08 -06001490 firstImage->mt != stObj->mt &&
1491 firstImage->mt->first_level <= stObj->firstLevel &&
1492 firstImage->mt->last_level >= stObj->lastLevel) {
Brian24df8f82007-08-06 15:48:42 -06001493
Brian14b98342007-08-07 16:42:08 -06001494 if (stObj->mt)
1495 st_miptree_release(pipe, &stObj->mt);
Brian24df8f82007-08-06 15:48:42 -06001496
Brian14b98342007-08-07 16:42:08 -06001497 st_miptree_reference(&stObj->mt, firstImage->mt);
Brian24df8f82007-08-06 15:48:42 -06001498 }
1499
1500 if (firstImage->base.IsCompressed) {
Brian14b98342007-08-07 16:42:08 -06001501 comp_byte = compressed_num_bytes(firstImage->base.TexFormat->MesaFormat);
Brian24df8f82007-08-06 15:48:42 -06001502 cpp = comp_byte;
1503 }
Brian5cb0d742007-10-17 14:57:15 -06001504 else {
1505 cpp = firstImage->base.TexFormat->TexelBytes;
1506 }
Brian24df8f82007-08-06 15:48:42 -06001507
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 */
Brian14b98342007-08-07 16:42:08 -06001517 if (stObj->mt &&
Brian5390a432007-08-15 19:23:58 -06001518 (stObj->mt->target != gl_target_to_pipe(stObj->base.Target) ||
Brian14b98342007-08-07 16:42:08 -06001519 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);
Brian24df8f82007-08-06 15:48:42 -06001528 }
1529
1530
1531 /* May need to create a new tree:
1532 */
Brian14b98342007-08-07 16:42:08 -06001533 if (!stObj->mt) {
1534 stObj->mt = st_miptree_create(pipe,
Brian5390a432007-08-15 19:23:58 -06001535 gl_target_to_pipe(stObj->base.Target),
Brianb2458402007-08-07 18:18:54 -06001536 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
Brian9cf9aa12007-08-10 13:02:51 -06001546 = st_mesa_format_to_pipe_format(firstImage->base.TexFormat->MesaFormat);
Brian24df8f82007-08-06 15:48:42 -06001547 }
1548
1549 /* Pull in any images not in the object's tree:
1550 */
Brian14b98342007-08-07 16:42:08 -06001551 nr_faces = (stObj->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
Brian24df8f82007-08-06 15:48:42 -06001552 for (face = 0; face < nr_faces; face++) {
Brian14b98342007-08-07 16:42:08 -06001553 for (i = stObj->firstLevel; i <= stObj->lastLevel; i++) {
Brian24df8f82007-08-06 15:48:42 -06001554 struct st_texture_image *stImage =
Brian14b98342007-08-07 16:42:08 -06001555 st_texture_image(stObj->base.Image[face][i]);
Brian24df8f82007-08-06 15:48:42 -06001556
1557 /* Need to import images in main memory or held in other trees.
1558 */
Brian14b98342007-08-07 16:42:08 -06001559 if (stObj->mt != stImage->mt) {
1560 copy_image_data_to_tree(pipe, stObj, stImage);
Brian24df8f82007-08-06 15:48:42 -06001561 *needFlush = GL_TRUE;
1562 }
1563 }
1564 }
1565
Brian24df8f82007-08-06 15:48:42 -06001566
1567 return GL_TRUE;
1568}
1569
1570
Brian24df8f82007-08-06 15:48:42 -06001571
1572
Brian6da92342007-08-06 20:53:28 +01001573void
1574st_init_texture_functions(struct dd_function_table *functions)
Brian24df8f82007-08-06 15:48:42 -06001575{
Brian24df8f82007-08-06 15:48:42 -06001576 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;
Brian038cb562007-09-26 17:57:15 -06001587 functions->CopyTexSubImage3D = st_CopyTexSubImage3D;
1588
Brian24df8f82007-08-06 15:48:42 -06001589 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;
Brianf8549e82007-11-01 14:14:30 -06001603
1604 /* XXX Temporary until we can query pipe's texture sizes */
1605 functions->TestProxyTexImage = _mesa_test_proxy_teximage;
Brian24df8f82007-08-06 15:48:42 -06001606}