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