cbe29f61a2bac5dc91680a0642981d62be1fb1f1
[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/texstore.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 GLuint texelBytes;
73
74 DBG("%s\n", __FUNCTION__);
75
76 if (intelImage->base.Border ||
77 ((intelImage->base._BaseFormat == GL_DEPTH_COMPONENT) &&
78 ((intelObj->base.WrapS == GL_CLAMP_TO_BORDER) ||
79 (intelObj->base.WrapT == GL_CLAMP_TO_BORDER))))
80 return;
81
82 if (intelImage->level > intelObj->base.BaseLevel &&
83 (intelImage->base.Width == 1 ||
84 (intelObj->base.Target != GL_TEXTURE_1D &&
85 intelImage->base.Height == 1) ||
86 (intelObj->base.Target == GL_TEXTURE_3D &&
87 intelImage->base.Depth == 1)))
88 return;
89
90 /* If this image disrespects BaseLevel, allocate from level zero.
91 * Usually BaseLevel == 0, so it's unlikely to happen.
92 */
93 if (intelImage->level < intelObj->base.BaseLevel)
94 firstLevel = 0;
95 else
96 firstLevel = intelObj->base.BaseLevel;
97
98
99 /* Figure out image dimensions at start level.
100 */
101 for (i = intelImage->level; i > firstLevel; i--) {
102 width <<= 1;
103 if (height != 1)
104 height <<= 1;
105 if (depth != 1)
106 depth <<= 1;
107 }
108
109 /* Guess a reasonable value for lastLevel. This is probably going
110 * to be wrong fairly often and might mean that we have to look at
111 * resizable buffers, or require that buffers implement lazy
112 * pagetable arrangements.
113 */
114 if ((intelObj->base.MinFilter == GL_NEAREST ||
115 intelObj->base.MinFilter == GL_LINEAR) &&
116 intelImage->level == firstLevel) {
117 lastLevel = firstLevel;
118 }
119 else {
120 l2width = logbase2(width);
121 l2height = logbase2(height);
122 l2depth = logbase2(depth);
123 lastLevel = firstLevel + MAX2(MAX2(l2width, l2height), l2depth);
124 }
125
126 assert(!intelObj->mt);
127 if (_mesa_is_format_compressed(intelImage->base.TexFormat))
128 comp_byte = intel_compressed_num_bytes(intelImage->base.TexFormat);
129
130 texelBytes = _mesa_get_format_bytes(intelImage->base.TexFormat);
131
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 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 gl_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_FORMAT_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_FORMAT_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 (!_mesa_is_bufferobj(unpack->BufferObj) ||
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 (!_mesa_is_bufferobj(unpack->BufferObj) ||
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 if (_mesa_is_format_compressed(texImage->TexFormat)) {
338 texelBytes = 0;
339 }
340 else {
341 texelBytes = _mesa_get_format_bytes(texImage->TexFormat);
342
343 /* Minimum pitch of 32 bytes */
344 if (postConvWidth * texelBytes < 32) {
345 postConvWidth = 32 / texelBytes;
346 texImage->RowStride = postConvWidth;
347 }
348
349 if (!intelImage->mt) {
350 assert(texImage->RowStride == postConvWidth);
351 }
352 }
353
354 /* Release the reference to a potentially orphaned buffer.
355 * Release any old malloced memory.
356 */
357 if (intelImage->mt) {
358 intel_miptree_release(intel, &intelImage->mt);
359 assert(!texImage->Data);
360 }
361 else if (texImage->Data) {
362 _mesa_free_texmemory(texImage->Data);
363 texImage->Data = NULL;
364 }
365
366 /* If this is the only texture image in the tree, could call
367 * bmBufferData with NULL data to free the old block and avoid
368 * waiting on any outstanding fences.
369 */
370 if (intelObj->mt &&
371 intelObj->mt->first_level == level &&
372 intelObj->mt->last_level == level &&
373 intelObj->mt->target != GL_TEXTURE_CUBE_MAP_ARB &&
374 !intel_miptree_match_image(intelObj->mt, &intelImage->base,
375 intelImage->face, intelImage->level)) {
376
377 DBG("release it\n");
378 intel_miptree_release(intel, &intelObj->mt);
379 assert(!intelObj->mt);
380 }
381
382 if (!intelObj->mt) {
383 guess_and_alloc_mipmap_tree(intel, intelObj, intelImage, pixels == NULL);
384 if (!intelObj->mt) {
385 DBG("guess_and_alloc_mipmap_tree: failed\n");
386 }
387 }
388
389 assert(!intelImage->mt);
390
391 if (intelObj->mt &&
392 intel_miptree_match_image(intelObj->mt, &intelImage->base,
393 intelImage->face, intelImage->level)) {
394
395 intel_miptree_reference(&intelImage->mt, intelObj->mt);
396 assert(intelImage->mt);
397 } else if (intelImage->base.Border == 0) {
398 int comp_byte = 0;
399 GLuint texelBytes = _mesa_get_format_bytes(intelImage->base.TexFormat);
400 GLenum baseFormat = _mesa_get_format_base_format(intelImage->base.TexFormat);
401 if (_mesa_is_format_compressed(intelImage->base.TexFormat)) {
402 comp_byte =
403 intel_compressed_num_bytes(intelImage->base.TexFormat);
404 }
405
406 /* Didn't fit in the object miptree, but it's suitable for inclusion in
407 * a miptree, so create one just for our level and store it in the image.
408 * It'll get moved into the object miptree at validate time.
409 */
410 intelImage->mt = intel_miptree_create(intel, target,
411 baseFormat,
412 internalFormat,
413 level, level,
414 width, height, depth,
415 texelBytes,
416 comp_byte, pixels == NULL);
417
418 }
419
420 /* PBO fastpaths:
421 */
422 if (dims <= 2 &&
423 intelImage->mt &&
424 _mesa_is_bufferobj(unpack->BufferObj) &&
425 check_pbo_format(internalFormat, format,
426 type, intelImage->base.TexFormat)) {
427
428 DBG("trying pbo upload\n");
429
430 /* Attempt to texture directly from PBO data (zero copy upload).
431 *
432 * Currently disable as it can lead to worse as well as better
433 * performance (in particular when intel_region_cow() is
434 * required).
435 */
436 if (intelObj->mt == intelImage->mt &&
437 intelObj->mt->first_level == level &&
438 intelObj->mt->last_level == level) {
439
440 if (try_pbo_zcopy(intel, intelImage, unpack,
441 internalFormat,
442 width, height, format, type, pixels)) {
443
444 DBG("pbo zcopy upload succeeded\n");
445 return;
446 }
447 }
448
449
450 /* Otherwise, attempt to use the blitter for PBO image uploads.
451 */
452 if (try_pbo_upload(intel, intelImage, unpack,
453 internalFormat,
454 width, height, format, type, pixels)) {
455 DBG("pbo upload succeeded\n");
456 return;
457 }
458
459 DBG("pbo upload failed\n");
460 }
461
462 /* intelCopyTexImage calls this function with pixels == NULL, with
463 * the expectation that the mipmap tree will be set up but nothing
464 * more will be done. This is where those calls return:
465 */
466 if (compressed) {
467 pixels = _mesa_validate_pbo_compressed_teximage(ctx, imageSize, pixels,
468 unpack,
469 "glCompressedTexImage");
470 } else {
471 pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, 1,
472 format, type,
473 pixels, unpack, "glTexImage");
474 }
475
476 LOCK_HARDWARE(intel);
477
478 if (intelImage->mt) {
479 if (pixels != NULL)
480 texImage->Data = intel_miptree_image_map(intel,
481 intelImage->mt,
482 intelImage->face,
483 intelImage->level,
484 &dstRowStride,
485 intelImage->base.ImageOffsets);
486 texImage->RowStride = dstRowStride / intelImage->mt->cpp;
487 }
488 else {
489 /* Allocate regular memory and store the image there temporarily. */
490 if (_mesa_is_format_compressed(texImage->TexFormat)) {
491 sizeInBytes = _mesa_format_image_size(texImage->TexFormat,
492 texImage->Width,
493 texImage->Height,
494 texImage->Depth);
495 dstRowStride =
496 _mesa_compressed_row_stride(texImage->TexFormat, width);
497 assert(dims != 3);
498 }
499 else {
500 dstRowStride = postConvWidth * texelBytes;
501 sizeInBytes = depth * dstRowStride * postConvHeight;
502 }
503
504 texImage->Data = _mesa_alloc_texmemory(sizeInBytes);
505 }
506
507 DBG("Upload image %dx%dx%d row_len %d "
508 "pitch %d pixels %d compressed %d\n",
509 width, height, depth, width * texelBytes, dstRowStride,
510 pixels ? 1 : 0, compressed);
511
512 /* Copy data. Would like to know when it's ok for us to eg. use
513 * the blitter to copy. Or, use the hardware to do the format
514 * conversion and copy:
515 */
516 if (pixels) {
517 if (compressed) {
518 if (intelImage->mt) {
519 struct intel_region *dst = intelImage->mt->region;
520 _mesa_copy_rect(texImage->Data, dst->cpp, dst->pitch,
521 0, 0,
522 intelImage->mt->level[level].width,
523 (intelImage->mt->level[level].height+3)/4,
524 pixels,
525 srcRowStride,
526 0, 0);
527 }
528 else {
529 memcpy(texImage->Data, pixels, imageSize);
530 }
531 }
532 else if (!_mesa_texstore(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
555
556 static void
557 intelTexImage3D(GLcontext * ctx,
558 GLenum target, GLint level,
559 GLint internalFormat,
560 GLint width, GLint height, GLint depth,
561 GLint border,
562 GLenum format, GLenum type, const void *pixels,
563 const struct gl_pixelstore_attrib *unpack,
564 struct gl_texture_object *texObj,
565 struct gl_texture_image *texImage)
566 {
567 intelTexImage(ctx, 3, target, level,
568 internalFormat, width, height, depth, border,
569 format, type, pixels, unpack, texObj, texImage, 0, GL_FALSE);
570 }
571
572
573 static void
574 intelTexImage2D(GLcontext * ctx,
575 GLenum target, GLint level,
576 GLint internalFormat,
577 GLint width, GLint height, GLint border,
578 GLenum format, GLenum type, const void *pixels,
579 const struct gl_pixelstore_attrib *unpack,
580 struct gl_texture_object *texObj,
581 struct gl_texture_image *texImage)
582 {
583 intelTexImage(ctx, 2, target, level,
584 internalFormat, width, height, 1, border,
585 format, type, pixels, unpack, texObj, texImage, 0, GL_FALSE);
586 }
587
588
589 static 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, GL_FALSE);
602 }
603
604
605 static void
606 intelCompressedTexImage2D( GLcontext *ctx, GLenum target, GLint level,
607 GLint internalFormat,
608 GLint width, GLint height, GLint border,
609 GLsizei imageSize, const GLvoid *data,
610 struct gl_texture_object *texObj,
611 struct gl_texture_image *texImage )
612 {
613 intelTexImage(ctx, 2, target, level,
614 internalFormat, width, height, 1, border,
615 0, 0, data, &ctx->Unpack, texObj, texImage, imageSize, GL_TRUE);
616 }
617
618
619 /**
620 * Need to map texture image into memory before copying image data,
621 * then unmap it.
622 */
623 static void
624 intel_get_tex_image(GLcontext * ctx, GLenum target, GLint level,
625 GLenum format, GLenum type, GLvoid * pixels,
626 struct gl_texture_object *texObj,
627 struct gl_texture_image *texImage, GLboolean compressed)
628 {
629 struct intel_context *intel = intel_context(ctx);
630 struct intel_texture_image *intelImage = intel_texture_image(texImage);
631
632 /* If we're reading from a texture that has been rendered to, need to
633 * make sure rendering is complete.
634 * We could probably predicate this on texObj->_RenderToTexture
635 */
636 intelFlush(ctx);
637
638 /* Map */
639 if (intelImage->mt) {
640 /* Image is stored in hardware format in a buffer managed by the
641 * kernel. Need to explicitly map and unmap it.
642 */
643 intelImage->base.Data =
644 intel_miptree_image_map(intel,
645 intelImage->mt,
646 intelImage->face,
647 intelImage->level,
648 &intelImage->base.RowStride,
649 intelImage->base.ImageOffsets);
650 intelImage->base.RowStride /= intelImage->mt->cpp;
651 }
652 else {
653 /* Otherwise, the image should actually be stored in
654 * intelImage->base.Data. This is pretty confusing for
655 * everybody, I'd much prefer to separate the two functions of
656 * texImage->Data - storage for texture images in main memory
657 * and access (ie mappings) of images. In other words, we'd
658 * create a new texImage->Map field and leave Data simply for
659 * storage.
660 */
661 assert(intelImage->base.Data);
662 }
663
664
665 if (compressed) {
666 _mesa_get_compressed_teximage(ctx, target, level, pixels,
667 texObj, texImage);
668 } else {
669 _mesa_get_teximage(ctx, target, level, format, type, pixels,
670 texObj, texImage);
671 }
672
673
674 /* Unmap */
675 if (intelImage->mt) {
676 intel_miptree_image_unmap(intel, intelImage->mt);
677 intelImage->base.Data = NULL;
678 }
679 }
680
681
682 static void
683 intelGetTexImage(GLcontext * ctx, GLenum target, GLint level,
684 GLenum format, GLenum type, GLvoid * pixels,
685 struct gl_texture_object *texObj,
686 struct gl_texture_image *texImage)
687 {
688 intel_get_tex_image(ctx, target, level, format, type, pixels,
689 texObj, texImage, GL_FALSE);
690 }
691
692
693 static void
694 intelGetCompressedTexImage(GLcontext *ctx, GLenum target, GLint level,
695 GLvoid *pixels,
696 struct gl_texture_object *texObj,
697 struct gl_texture_image *texImage)
698 {
699 intel_get_tex_image(ctx, target, level, 0, 0, pixels,
700 texObj, texImage, GL_TRUE);
701 }
702
703
704 void
705 intelSetTexOffset(__DRIcontext *pDRICtx, GLint texname,
706 unsigned long long offset, GLint depth, GLuint pitch)
707 {
708 struct intel_context *intel = pDRICtx->driverPrivate;
709 struct gl_texture_object *tObj = _mesa_lookup_texture(&intel->ctx, texname);
710 struct intel_texture_object *intelObj = intel_texture_object(tObj);
711
712 if (!intelObj)
713 return;
714
715 if (intelObj->mt)
716 intel_miptree_release(intel, &intelObj->mt);
717
718 intelObj->imageOverride = GL_TRUE;
719 intelObj->depthOverride = depth;
720 intelObj->pitchOverride = pitch;
721
722 if (offset)
723 intelObj->textureOffset = offset;
724 }
725
726 void
727 intelSetTexBuffer2(__DRIcontext *pDRICtx, GLint target,
728 GLint glx_texture_format,
729 __DRIdrawable *dPriv)
730 {
731 struct intel_framebuffer *intel_fb = dPriv->driverPrivate;
732 struct intel_context *intel = pDRICtx->driverPrivate;
733 struct intel_texture_object *intelObj;
734 struct intel_texture_image *intelImage;
735 struct intel_mipmap_tree *mt;
736 struct intel_renderbuffer *rb;
737 struct gl_texture_unit *texUnit;
738 struct gl_texture_object *texObj;
739 struct gl_texture_image *texImage;
740 int level = 0, type, format, internalFormat;
741
742 texUnit = &intel->ctx.Texture.Unit[intel->ctx.Texture.CurrentUnit];
743 texObj = _mesa_select_tex_object(&intel->ctx, texUnit, target);
744 intelObj = intel_texture_object(texObj);
745
746 if (!intelObj)
747 return;
748
749 intel_update_renderbuffers(pDRICtx, dPriv);
750
751 rb = intel_fb->color_rb[0];
752 /* If the region isn't set, then intel_update_renderbuffers was unable
753 * to get the buffers for the drawable.
754 */
755 if (rb->region == NULL)
756 return;
757
758 type = GL_BGRA;
759 format = GL_UNSIGNED_BYTE;
760 if (glx_texture_format == GLX_TEXTURE_FORMAT_RGB_EXT)
761 internalFormat = GL_RGB;
762 else
763 internalFormat = GL_RGBA;
764
765 mt = intel_miptree_create_for_region(intel, target,
766 internalFormat,
767 0, 0, rb->region, 1, 0);
768 if (mt == NULL)
769 return;
770
771 _mesa_lock_texture(&intel->ctx, texObj);
772
773 texImage = _mesa_get_tex_image(&intel->ctx, texObj, target, level);
774 intelImage = intel_texture_image(texImage);
775
776 if (intelImage->mt) {
777 intel_miptree_release(intel, &intelImage->mt);
778 assert(!texImage->Data);
779 }
780 if (intelObj->mt)
781 intel_miptree_release(intel, &intelObj->mt);
782
783 intelObj->mt = mt;
784 _mesa_init_teximage_fields(&intel->ctx, target, texImage,
785 rb->region->width, rb->region->height, 1,
786 0, internalFormat);
787
788 intelImage->face = target_to_face(target);
789 intelImage->level = level;
790 texImage->TexFormat = intelChooseTextureFormat(&intel->ctx, internalFormat,
791 type, format);
792 texImage->RowStride = rb->region->pitch;
793 intel_miptree_reference(&intelImage->mt, intelObj->mt);
794
795 if (!intel_miptree_match_image(intelObj->mt, &intelImage->base,
796 intelImage->face, intelImage->level)) {
797 fprintf(stderr, "miptree doesn't match image\n");
798 }
799
800 _mesa_unlock_texture(&intel->ctx, texObj);
801 }
802
803 void
804 intelSetTexBuffer(__DRIcontext *pDRICtx, GLint target, __DRIdrawable *dPriv)
805 {
806 /* The old interface didn't have the format argument, so copy our
807 * implementation's behavior at the time.
808 */
809 intelSetTexBuffer2(pDRICtx, target, GLX_TEXTURE_FORMAT_RGBA_EXT, dPriv);
810 }
811
812
813 void
814 intelInitTextureImageFuncs(struct dd_function_table *functions)
815 {
816 functions->TexImage1D = intelTexImage1D;
817 functions->TexImage2D = intelTexImage2D;
818 functions->TexImage3D = intelTexImage3D;
819 functions->GetTexImage = intelGetTexImage;
820
821 functions->CompressedTexImage2D = intelCompressedTexImage2D;
822 functions->GetCompressedTexImage = intelGetCompressedTexImage;
823 }