Merge branch 'mesa_7_5_branch'
[mesa.git] / src / mesa / drivers / dri / intel / intel_tex_image.c
1
2 #include <stdlib.h>
3 #include <stdio.h>
4
5 #include "main/glheader.h"
6 #include "main/macros.h"
7 #include "main/mtypes.h"
8 #include "main/enums.h"
9 #include "main/colortab.h"
10 #include "main/convolve.h"
11 #include "main/context.h"
12 #include "main/simple_list.h"
13 #include "main/texcompress.h"
14 #include "main/texformat.h"
15 #include "main/texgetimage.h"
16 #include "main/texobj.h"
17 #include "main/texstore.h"
18 #include "main/teximage.h"
19
20 #include "intel_context.h"
21 #include "intel_mipmap_tree.h"
22 #include "intel_buffer_objects.h"
23 #include "intel_batchbuffer.h"
24 #include "intel_tex.h"
25 #include "intel_blit.h"
26 #include "intel_fbo.h"
27
28 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
29
30 /* Functions to store texture images. Where possible, mipmap_tree's
31 * will be created or further instantiated with image data, otherwise
32 * images will be stored in malloc'd memory. A validation step is
33 * required to pull those images into a mipmap tree, or otherwise
34 * decide a fallback is required.
35 */
36
37
38 static int
39 logbase2(int n)
40 {
41 GLint i = 1;
42 GLint log2 = 0;
43
44 while (n > i) {
45 i *= 2;
46 log2++;
47 }
48
49 return log2;
50 }
51
52
53 /* Otherwise, store it in memory if (Border != 0) or (any dimension ==
54 * 1).
55 *
56 * Otherwise, if max_level >= level >= min_level, create tree with
57 * space for textures from min_level down to max_level.
58 *
59 * Otherwise, create tree with space for textures from (level
60 * 0)..(1x1). Consider pruning this tree at a validation if the
61 * saving is worth it.
62 */
63 static void
64 guess_and_alloc_mipmap_tree(struct intel_context *intel,
65 struct intel_texture_object *intelObj,
66 struct intel_texture_image *intelImage,
67 GLboolean expect_accelerated_upload)
68 {
69 GLuint firstLevel;
70 GLuint lastLevel;
71 GLuint width = intelImage->base.Width;
72 GLuint height = intelImage->base.Height;
73 GLuint depth = intelImage->base.Depth;
74 GLuint l2width, l2height, l2depth;
75 GLuint i, comp_byte = 0;
76
77 DBG("%s\n", __FUNCTION__);
78
79 if (intelImage->base.Border ||
80 ((intelImage->base._BaseFormat == GL_DEPTH_COMPONENT) &&
81 ((intelObj->base.WrapS == GL_CLAMP_TO_BORDER) ||
82 (intelObj->base.WrapT == GL_CLAMP_TO_BORDER))))
83 return;
84
85 if (intelImage->level > intelObj->base.BaseLevel &&
86 (intelImage->base.Width == 1 ||
87 (intelObj->base.Target != GL_TEXTURE_1D &&
88 intelImage->base.Height == 1) ||
89 (intelObj->base.Target == GL_TEXTURE_3D &&
90 intelImage->base.Depth == 1)))
91 return;
92
93 /* If this image disrespects BaseLevel, allocate from level zero.
94 * Usually BaseLevel == 0, so it's unlikely to happen.
95 */
96 if (intelImage->level < intelObj->base.BaseLevel)
97 firstLevel = 0;
98 else
99 firstLevel = intelObj->base.BaseLevel;
100
101
102 /* Figure out image dimensions at start level.
103 */
104 for (i = intelImage->level; i > firstLevel; i--) {
105 width <<= 1;
106 if (height != 1)
107 height <<= 1;
108 if (depth != 1)
109 depth <<= 1;
110 }
111
112 /* Guess a reasonable value for lastLevel. This is probably going
113 * to be wrong fairly often and might mean that we have to look at
114 * resizable buffers, or require that buffers implement lazy
115 * pagetable arrangements.
116 */
117 if ((intelObj->base.MinFilter == GL_NEAREST ||
118 intelObj->base.MinFilter == GL_LINEAR) &&
119 intelImage->level == firstLevel) {
120 lastLevel = firstLevel;
121 }
122 else {
123 l2width = logbase2(width);
124 l2height = logbase2(height);
125 l2depth = logbase2(depth);
126 lastLevel = firstLevel + MAX2(MAX2(l2width, l2height), l2depth);
127 }
128
129 assert(!intelObj->mt);
130 if (intelImage->base.IsCompressed)
131 comp_byte = intel_compressed_num_bytes(intelImage->base.TexFormat->MesaFormat);
132 intelObj->mt = intel_miptree_create(intel,
133 intelObj->base.Target,
134 intelImage->base._BaseFormat,
135 intelImage->base.InternalFormat,
136 firstLevel,
137 lastLevel,
138 width,
139 height,
140 depth,
141 intelImage->base.TexFormat->TexelBytes,
142 comp_byte,
143 expect_accelerated_upload);
144
145 DBG("%s - success\n", __FUNCTION__);
146 }
147
148
149
150
151 static GLuint
152 target_to_face(GLenum target)
153 {
154 switch (target) {
155 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
156 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
157 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
158 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
159 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
160 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
161 return ((GLuint) target - (GLuint) GL_TEXTURE_CUBE_MAP_POSITIVE_X);
162 default:
163 return 0;
164 }
165 }
166
167 /* There are actually quite a few combinations this will work for,
168 * more than what I've listed here.
169 */
170 static GLboolean
171 check_pbo_format(GLint internalFormat,
172 GLenum format, GLenum type,
173 const struct gl_texture_format *mesa_format)
174 {
175 switch (internalFormat) {
176 case 4:
177 case GL_RGBA:
178 return (format == GL_BGRA &&
179 (type == GL_UNSIGNED_BYTE ||
180 type == GL_UNSIGNED_INT_8_8_8_8_REV) &&
181 mesa_format == &_mesa_texformat_argb8888);
182 case 3:
183 case GL_RGB:
184 return (format == GL_RGB &&
185 type == GL_UNSIGNED_SHORT_5_6_5 &&
186 mesa_format == &_mesa_texformat_rgb565);
187 case GL_YCBCR_MESA:
188 return (type == GL_UNSIGNED_SHORT_8_8_MESA || type == GL_UNSIGNED_BYTE);
189 default:
190 return GL_FALSE;
191 }
192 }
193
194
195 /* XXX: Do this for TexSubImage also:
196 */
197 static GLboolean
198 try_pbo_upload(struct intel_context *intel,
199 struct intel_texture_image *intelImage,
200 const struct gl_pixelstore_attrib *unpack,
201 GLint internalFormat,
202 GLint width, GLint height,
203 GLenum format, GLenum type, const void *pixels)
204 {
205 struct intel_buffer_object *pbo = intel_buffer_object(unpack->BufferObj);
206 GLuint src_offset, src_stride;
207 GLuint dst_offset, dst_stride;
208
209 if (unpack->BufferObj->Name == 0 ||
210 intel->ctx._ImageTransferState ||
211 unpack->SkipPixels || unpack->SkipRows) {
212 DBG("%s: failure 1\n", __FUNCTION__);
213 return GL_FALSE;
214 }
215
216 /* note: potential 64-bit ptr to 32-bit int cast */
217 src_offset = (GLuint) (unsigned long) pixels;
218
219 if (unpack->RowLength > 0)
220 src_stride = unpack->RowLength;
221 else
222 src_stride = width;
223
224 dst_offset = intel_miptree_image_offset(intelImage->mt,
225 intelImage->face,
226 intelImage->level);
227
228 dst_stride = intelImage->mt->pitch;
229
230 intelFlush(&intel->ctx);
231 LOCK_HARDWARE(intel);
232 {
233 dri_bo *src_buffer = intel_bufferobj_buffer(intel, pbo, INTEL_READ);
234 dri_bo *dst_buffer = intel_region_buffer(intel,
235 intelImage->mt->region,
236 INTEL_WRITE_FULL);
237
238
239 if (!intelEmitCopyBlit(intel,
240 intelImage->mt->cpp,
241 src_stride, src_buffer, src_offset, GL_FALSE,
242 dst_stride, dst_buffer, dst_offset, GL_FALSE,
243 0, 0, 0, 0, width, height,
244 GL_COPY)) {
245 UNLOCK_HARDWARE(intel);
246 return GL_FALSE;
247 }
248 }
249 UNLOCK_HARDWARE(intel);
250
251 return GL_TRUE;
252 }
253
254
255 static GLboolean
256 try_pbo_zcopy(struct intel_context *intel,
257 struct intel_texture_image *intelImage,
258 const struct gl_pixelstore_attrib *unpack,
259 GLint internalFormat,
260 GLint width, GLint height,
261 GLenum format, GLenum type, const void *pixels)
262 {
263 struct intel_buffer_object *pbo = intel_buffer_object(unpack->BufferObj);
264 GLuint src_offset, src_stride;
265 GLuint dst_offset, dst_stride;
266
267 if (unpack->BufferObj->Name == 0 ||
268 intel->ctx._ImageTransferState ||
269 unpack->SkipPixels || unpack->SkipRows) {
270 DBG("%s: failure 1\n", __FUNCTION__);
271 return GL_FALSE;
272 }
273
274 /* note: potential 64-bit ptr to 32-bit int cast */
275 src_offset = (GLuint) (unsigned long) pixels;
276
277 if (unpack->RowLength > 0)
278 src_stride = unpack->RowLength;
279 else
280 src_stride = width;
281
282 dst_offset = intel_miptree_image_offset(intelImage->mt,
283 intelImage->face,
284 intelImage->level);
285
286 dst_stride = intelImage->mt->pitch;
287
288 if (src_stride != dst_stride || dst_offset != 0 || src_offset != 0) {
289 DBG("%s: failure 2\n", __FUNCTION__);
290 return GL_FALSE;
291 }
292
293 intel_region_attach_pbo(intel, intelImage->mt->region, pbo);
294
295 return GL_TRUE;
296 }
297
298
299 static void
300 intelTexImage(GLcontext * ctx,
301 GLint dims,
302 GLenum target, GLint level,
303 GLint internalFormat,
304 GLint width, GLint height, GLint depth,
305 GLint border,
306 GLenum format, GLenum type, const void *pixels,
307 const struct gl_pixelstore_attrib *unpack,
308 struct gl_texture_object *texObj,
309 struct gl_texture_image *texImage, GLsizei imageSize,
310 GLboolean compressed)
311 {
312 struct intel_context *intel = intel_context(ctx);
313 struct intel_texture_object *intelObj = intel_texture_object(texObj);
314 struct intel_texture_image *intelImage = intel_texture_image(texImage);
315 GLint postConvWidth = width;
316 GLint postConvHeight = height;
317 GLint texelBytes, sizeInBytes;
318 GLuint dstRowStride = 0, srcRowStride = texImage->RowStride;
319
320 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__,
321 _mesa_lookup_enum_by_nr(target), level, width, height, depth, border);
322
323 intelFlush(ctx);
324
325 intelImage->face = target_to_face(target);
326 intelImage->level = level;
327
328 if (ctx->_ImageTransferState & IMAGE_CONVOLUTION_BIT) {
329 _mesa_adjust_image_for_convolution(ctx, dims, &postConvWidth,
330 &postConvHeight);
331 }
332
333 /* choose the texture format */
334 texImage->TexFormat = intelChooseTextureFormat(ctx, internalFormat,
335 format, type);
336
337 _mesa_set_fetch_functions(texImage, dims);
338
339 if (texImage->TexFormat->TexelBytes == 0) {
340 /* must be a compressed format */
341 texelBytes = 0;
342 texImage->IsCompressed = GL_TRUE;
343 texImage->CompressedSize =
344 ctx->Driver.CompressedTextureSize(ctx, texImage->Width,
345 texImage->Height, texImage->Depth,
346 texImage->TexFormat->MesaFormat);
347 } else {
348 texelBytes = texImage->TexFormat->TexelBytes;
349
350 /* Minimum pitch of 32 bytes */
351 if (postConvWidth * texelBytes < 32) {
352 postConvWidth = 32 / texelBytes;
353 texImage->RowStride = postConvWidth;
354 }
355
356 if (!intelImage->mt) {
357 assert(texImage->RowStride == postConvWidth);
358 }
359 }
360
361 /* Release the reference to a potentially orphaned buffer.
362 * Release any old malloced memory.
363 */
364 if (intelImage->mt) {
365 intel_miptree_release(intel, &intelImage->mt);
366 assert(!texImage->Data);
367 }
368 else if (texImage->Data) {
369 _mesa_free_texmemory(texImage->Data);
370 texImage->Data = NULL;
371 }
372
373 /* If this is the only texture image in the tree, could call
374 * bmBufferData with NULL data to free the old block and avoid
375 * waiting on any outstanding fences.
376 */
377 if (intelObj->mt &&
378 intelObj->mt->first_level == level &&
379 intelObj->mt->last_level == level &&
380 intelObj->mt->target != GL_TEXTURE_CUBE_MAP_ARB &&
381 !intel_miptree_match_image(intelObj->mt, &intelImage->base,
382 intelImage->face, intelImage->level)) {
383
384 DBG("release it\n");
385 intel_miptree_release(intel, &intelObj->mt);
386 assert(!intelObj->mt);
387 }
388
389 if (!intelObj->mt) {
390 guess_and_alloc_mipmap_tree(intel, intelObj, intelImage, pixels == NULL);
391 if (!intelObj->mt) {
392 DBG("guess_and_alloc_mipmap_tree: failed\n");
393 }
394 }
395
396 assert(!intelImage->mt);
397
398 if (intelObj->mt &&
399 intel_miptree_match_image(intelObj->mt, &intelImage->base,
400 intelImage->face, intelImage->level)) {
401
402 intel_miptree_reference(&intelImage->mt, intelObj->mt);
403 assert(intelImage->mt);
404 } else if (intelImage->base.Border == 0) {
405 int comp_byte = 0;
406
407 if (intelImage->base.IsCompressed) {
408 comp_byte =
409 intel_compressed_num_bytes(intelImage->base.TexFormat->MesaFormat);
410 }
411
412 /* Didn't fit in the object miptree, but it's suitable for inclusion in
413 * a miptree, so create one just for our level and store it in the image.
414 * It'll get moved into the object miptree at validate time.
415 */
416 intelImage->mt = intel_miptree_create(intel, target,
417 intelImage->base.TexFormat->BaseFormat,
418 internalFormat,
419 level, level,
420 width, height, depth,
421 intelImage->base.TexFormat->TexelBytes,
422 comp_byte, pixels == NULL);
423
424 }
425
426 /* PBO fastpaths:
427 */
428 if (dims <= 2 &&
429 intelImage->mt &&
430 unpack->BufferObj->Name != 0 &&
431 check_pbo_format(internalFormat, format,
432 type, intelImage->base.TexFormat)) {
433
434 DBG("trying pbo upload\n");
435
436 /* Attempt to texture directly from PBO data (zero copy upload).
437 *
438 * Currently disable as it can lead to worse as well as better
439 * performance (in particular when intel_region_cow() is
440 * required).
441 */
442 if (intelObj->mt == intelImage->mt &&
443 intelObj->mt->first_level == level &&
444 intelObj->mt->last_level == level) {
445
446 if (try_pbo_zcopy(intel, intelImage, unpack,
447 internalFormat,
448 width, height, format, type, pixels)) {
449
450 DBG("pbo zcopy upload succeeded\n");
451 return;
452 }
453 }
454
455
456 /* Otherwise, attempt to use the blitter for PBO image uploads.
457 */
458 if (try_pbo_upload(intel, intelImage, unpack,
459 internalFormat,
460 width, height, format, type, pixels)) {
461 DBG("pbo upload succeeded\n");
462 return;
463 }
464
465 DBG("pbo upload failed\n");
466 }
467
468 /* intelCopyTexImage calls this function with pixels == NULL, with
469 * the expectation that the mipmap tree will be set up but nothing
470 * more will be done. This is where those calls return:
471 */
472 if (compressed) {
473 pixels = _mesa_validate_pbo_compressed_teximage(ctx, imageSize, pixels,
474 unpack,
475 "glCompressedTexImage");
476 } else {
477 pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, 1,
478 format, type,
479 pixels, unpack, "glTexImage");
480 }
481
482 LOCK_HARDWARE(intel);
483
484 if (intelImage->mt) {
485 if (pixels != NULL)
486 texImage->Data = intel_miptree_image_map(intel,
487 intelImage->mt,
488 intelImage->face,
489 intelImage->level,
490 &dstRowStride,
491 intelImage->base.ImageOffsets);
492 texImage->RowStride = dstRowStride / intelImage->mt->cpp;
493 }
494 else {
495 /* Allocate regular memory and store the image there temporarily. */
496 if (texImage->IsCompressed) {
497 sizeInBytes = texImage->CompressedSize;
498 dstRowStride =
499 _mesa_compressed_row_stride(texImage->TexFormat->MesaFormat, width);
500 assert(dims != 3);
501 }
502 else {
503 dstRowStride = postConvWidth * texelBytes;
504 sizeInBytes = depth * dstRowStride * postConvHeight;
505 }
506
507 texImage->Data = _mesa_alloc_texmemory(sizeInBytes);
508 }
509
510 DBG("Upload image %dx%dx%d row_len %d "
511 "pitch %d pixels %d compressed %d\n",
512 width, height, depth, width * texelBytes, dstRowStride,
513 pixels ? 1 : 0, compressed);
514
515 /* Copy data. Would like to know when it's ok for us to eg. use
516 * the blitter to copy. Or, use the hardware to do the format
517 * conversion and copy:
518 */
519 if (pixels) {
520 if (compressed) {
521 if (intelImage->mt) {
522 struct intel_region *dst = intelImage->mt->region;
523 _mesa_copy_rect(texImage->Data, dst->cpp, dst->pitch,
524 0, 0,
525 intelImage->mt->level[level].width,
526 (intelImage->mt->level[level].height+3)/4,
527 pixels,
528 srcRowStride,
529 0, 0);
530 } else
531 memcpy(texImage->Data, pixels, imageSize);
532 } else if (!texImage->TexFormat->StoreImage(ctx, dims,
533 texImage->_BaseFormat,
534 texImage->TexFormat,
535 texImage->Data, 0, 0, 0, /* dstX/Y/Zoffset */
536 dstRowStride,
537 texImage->ImageOffsets,
538 width, height, depth,
539 format, type, pixels, unpack)) {
540 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
541 }
542 }
543
544 _mesa_unmap_teximage_pbo(ctx, unpack);
545
546 if (intelImage->mt) {
547 if (pixels != NULL)
548 intel_miptree_image_unmap(intel, intelImage->mt);
549 texImage->Data = NULL;
550 }
551
552 UNLOCK_HARDWARE(intel);
553
554 /* GL_SGIS_generate_mipmap */
555 if (level == texObj->BaseLevel && texObj->GenerateMipmap) {
556 intel_generate_mipmap(ctx, target, texObj);
557 }
558 }
559
560
561 static void
562 intelTexImage3D(GLcontext * ctx,
563 GLenum target, GLint level,
564 GLint internalFormat,
565 GLint width, GLint height, GLint depth,
566 GLint border,
567 GLenum format, GLenum type, const void *pixels,
568 const struct gl_pixelstore_attrib *unpack,
569 struct gl_texture_object *texObj,
570 struct gl_texture_image *texImage)
571 {
572 intelTexImage(ctx, 3, target, level,
573 internalFormat, width, height, depth, border,
574 format, type, pixels, unpack, texObj, texImage, 0, GL_FALSE);
575 }
576
577
578 static void
579 intelTexImage2D(GLcontext * ctx,
580 GLenum target, GLint level,
581 GLint internalFormat,
582 GLint width, GLint height, GLint border,
583 GLenum format, GLenum type, const void *pixels,
584 const struct gl_pixelstore_attrib *unpack,
585 struct gl_texture_object *texObj,
586 struct gl_texture_image *texImage)
587 {
588 intelTexImage(ctx, 2, target, level,
589 internalFormat, width, height, 1, border,
590 format, type, pixels, unpack, texObj, texImage, 0, GL_FALSE);
591 }
592
593
594 static void
595 intelTexImage1D(GLcontext * ctx,
596 GLenum target, GLint level,
597 GLint internalFormat,
598 GLint width, GLint border,
599 GLenum format, GLenum type, const void *pixels,
600 const struct gl_pixelstore_attrib *unpack,
601 struct gl_texture_object *texObj,
602 struct gl_texture_image *texImage)
603 {
604 intelTexImage(ctx, 1, target, level,
605 internalFormat, width, 1, 1, border,
606 format, type, pixels, unpack, texObj, texImage, 0, GL_FALSE);
607 }
608
609
610 static void
611 intelCompressedTexImage2D( GLcontext *ctx, GLenum target, GLint level,
612 GLint internalFormat,
613 GLint width, GLint height, GLint border,
614 GLsizei imageSize, const GLvoid *data,
615 struct gl_texture_object *texObj,
616 struct gl_texture_image *texImage )
617 {
618 intelTexImage(ctx, 2, target, level,
619 internalFormat, width, height, 1, border,
620 0, 0, data, &ctx->Unpack, texObj, texImage, imageSize, GL_TRUE);
621 }
622
623
624 /**
625 * Need to map texture image into memory before copying image data,
626 * then unmap it.
627 */
628 static void
629 intel_get_tex_image(GLcontext * ctx, GLenum target, GLint level,
630 GLenum format, GLenum type, GLvoid * pixels,
631 struct gl_texture_object *texObj,
632 struct gl_texture_image *texImage, GLboolean compressed)
633 {
634 struct intel_context *intel = intel_context(ctx);
635 struct intel_texture_image *intelImage = intel_texture_image(texImage);
636
637 /* If we're reading from a texture that has been rendered to, need to
638 * make sure rendering is complete.
639 * We could probably predicate this on texObj->_RenderToTexture
640 */
641 intelFlush(ctx);
642
643 /* Map */
644 if (intelImage->mt) {
645 /* Image is stored in hardware format in a buffer managed by the
646 * kernel. Need to explicitly map and unmap it.
647 */
648 intelImage->base.Data =
649 intel_miptree_image_map(intel,
650 intelImage->mt,
651 intelImage->face,
652 intelImage->level,
653 &intelImage->base.RowStride,
654 intelImage->base.ImageOffsets);
655 intelImage->base.RowStride /= intelImage->mt->cpp;
656 }
657 else {
658 /* Otherwise, the image should actually be stored in
659 * intelImage->base.Data. This is pretty confusing for
660 * everybody, I'd much prefer to separate the two functions of
661 * texImage->Data - storage for texture images in main memory
662 * and access (ie mappings) of images. In other words, we'd
663 * create a new texImage->Map field and leave Data simply for
664 * storage.
665 */
666 assert(intelImage->base.Data);
667 }
668
669
670 if (compressed) {
671 _mesa_get_compressed_teximage(ctx, target, level, pixels,
672 texObj, texImage);
673 } else {
674 _mesa_get_teximage(ctx, target, level, format, type, pixels,
675 texObj, texImage);
676 }
677
678
679 /* Unmap */
680 if (intelImage->mt) {
681 intel_miptree_image_unmap(intel, intelImage->mt);
682 intelImage->base.Data = NULL;
683 }
684 }
685
686
687 static void
688 intelGetTexImage(GLcontext * ctx, GLenum target, GLint level,
689 GLenum format, GLenum type, GLvoid * pixels,
690 struct gl_texture_object *texObj,
691 struct gl_texture_image *texImage)
692 {
693 intel_get_tex_image(ctx, target, level, format, type, pixels,
694 texObj, texImage, GL_FALSE);
695 }
696
697
698 static void
699 intelGetCompressedTexImage(GLcontext *ctx, GLenum target, GLint level,
700 GLvoid *pixels,
701 struct gl_texture_object *texObj,
702 struct gl_texture_image *texImage)
703 {
704 intel_get_tex_image(ctx, target, level, 0, 0, pixels,
705 texObj, texImage, GL_TRUE);
706 }
707
708
709 void
710 intelSetTexOffset(__DRIcontext *pDRICtx, GLint texname,
711 unsigned long long offset, GLint depth, GLuint pitch)
712 {
713 struct intel_context *intel = pDRICtx->driverPrivate;
714 struct gl_texture_object *tObj = _mesa_lookup_texture(&intel->ctx, texname);
715 struct intel_texture_object *intelObj = intel_texture_object(tObj);
716
717 if (!intelObj)
718 return;
719
720 if (intelObj->mt)
721 intel_miptree_release(intel, &intelObj->mt);
722
723 intelObj->imageOverride = GL_TRUE;
724 intelObj->depthOverride = depth;
725 intelObj->pitchOverride = pitch;
726
727 if (offset)
728 intelObj->textureOffset = offset;
729 }
730
731 void
732 intelSetTexBuffer2(__DRIcontext *pDRICtx, GLint target,
733 GLint glx_texture_format,
734 __DRIdrawable *dPriv)
735 {
736 struct intel_framebuffer *intel_fb = dPriv->driverPrivate;
737 struct intel_context *intel = pDRICtx->driverPrivate;
738 struct intel_texture_object *intelObj;
739 struct intel_texture_image *intelImage;
740 struct intel_mipmap_tree *mt;
741 struct intel_renderbuffer *rb;
742 struct gl_texture_unit *texUnit;
743 struct gl_texture_object *texObj;
744 struct gl_texture_image *texImage;
745 int level = 0, type, format, internalFormat;
746
747 texUnit = &intel->ctx.Texture.Unit[intel->ctx.Texture.CurrentUnit];
748 texObj = _mesa_select_tex_object(&intel->ctx, texUnit, target);
749 intelObj = intel_texture_object(texObj);
750
751 if (!intelObj)
752 return;
753
754 intel_update_renderbuffers(pDRICtx, dPriv);
755
756 rb = intel_fb->color_rb[0];
757 /* If the region isn't set, then intel_update_renderbuffers was unable
758 * to get the buffers for the drawable.
759 */
760 if (rb->region == NULL)
761 return;
762
763 type = GL_BGRA;
764 format = GL_UNSIGNED_BYTE;
765 if (glx_texture_format == GLX_TEXTURE_FORMAT_RGB_EXT)
766 internalFormat = GL_RGB;
767 else
768 internalFormat = GL_RGBA;
769
770 mt = intel_miptree_create_for_region(intel, target,
771 internalFormat,
772 0, 0, rb->region, 1, 0);
773 if (mt == NULL)
774 return;
775
776 _mesa_lock_texture(&intel->ctx, texObj);
777
778 texImage = _mesa_get_tex_image(&intel->ctx, texObj, target, level);
779 intelImage = intel_texture_image(texImage);
780
781 if (intelImage->mt) {
782 intel_miptree_release(intel, &intelImage->mt);
783 assert(!texImage->Data);
784 }
785 if (intelObj->mt)
786 intel_miptree_release(intel, &intelObj->mt);
787
788 intelObj->mt = mt;
789 _mesa_init_teximage_fields(&intel->ctx, target, texImage,
790 rb->region->width, rb->region->height, 1,
791 0, internalFormat);
792
793 intelImage->face = target_to_face(target);
794 intelImage->level = level;
795 texImage->TexFormat = intelChooseTextureFormat(&intel->ctx, internalFormat,
796 type, format);
797 _mesa_set_fetch_functions(texImage, 2);
798 texImage->RowStride = rb->region->pitch;
799 intel_miptree_reference(&intelImage->mt, intelObj->mt);
800
801 if (!intel_miptree_match_image(intelObj->mt, &intelImage->base,
802 intelImage->face, intelImage->level)) {
803 fprintf(stderr, "miptree doesn't match image\n");
804 }
805
806 _mesa_unlock_texture(&intel->ctx, texObj);
807 }
808
809 void
810 intelSetTexBuffer(__DRIcontext *pDRICtx, GLint target, __DRIdrawable *dPriv)
811 {
812 /* The old interface didn't have the format argument, so copy our
813 * implementation's behavior at the time.
814 */
815 intelSetTexBuffer2(pDRICtx, target, GLX_TEXTURE_FORMAT_RGBA_EXT, dPriv);
816 }
817
818
819 void
820 intelInitTextureImageFuncs(struct dd_function_table *functions)
821 {
822 functions->TexImage1D = intelTexImage1D;
823 functions->TexImage2D = intelTexImage2D;
824 functions->TexImage3D = intelTexImage3D;
825 functions->GetTexImage = intelGetTexImage;
826
827 functions->CompressedTexImage2D = intelCompressedTexImage2D;
828 functions->GetCompressedTexImage = intelGetCompressedTexImage;
829 }