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