Merge remote branch 'origin/mesa_7_7_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/formats.h"
10 #include "main/image.h"
11 #include "main/texcompress.h"
12 #include "main/texstore.h"
13 #include "main/texgetimage.h"
14 #include "main/texobj.h"
15 #include "main/texstore.h"
16 #include "main/teximage.h"
17
18 #include "intel_context.h"
19 #include "intel_mipmap_tree.h"
20 #include "intel_buffer_objects.h"
21 #include "intel_batchbuffer.h"
22 #include "intel_tex.h"
23 #include "intel_blit.h"
24 #include "intel_fbo.h"
25
26 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
27
28 /* Functions to store texture images. Where possible, mipmap_tree's
29 * will be created or further instantiated with image data, otherwise
30 * images will be stored in malloc'd memory. A validation step is
31 * required to pull those images into a mipmap tree, or otherwise
32 * decide a fallback is required.
33 */
34
35
36 static int
37 logbase2(int n)
38 {
39 GLint i = 1;
40 GLint log2 = 0;
41
42 while (n > i) {
43 i *= 2;
44 log2++;
45 }
46
47 return log2;
48 }
49
50
51 /* Otherwise, store it in memory if (Border != 0) or (any dimension ==
52 * 1).
53 *
54 * Otherwise, if max_level >= level >= min_level, create tree with
55 * space for textures from min_level down to max_level.
56 *
57 * Otherwise, create tree with space for textures from (level
58 * 0)..(1x1). Consider pruning this tree at a validation if the
59 * saving is worth it.
60 */
61 static void
62 guess_and_alloc_mipmap_tree(struct intel_context *intel,
63 struct intel_texture_object *intelObj,
64 struct intel_texture_image *intelImage,
65 GLboolean expect_accelerated_upload)
66 {
67 GLuint firstLevel;
68 GLuint lastLevel;
69 GLuint width = intelImage->base.Width;
70 GLuint height = intelImage->base.Height;
71 GLuint depth = intelImage->base.Depth;
72 GLuint l2width, l2height, l2depth;
73 GLuint i, comp_byte = 0;
74 GLuint texelBytes;
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 (intel->gen < 4 || firstLevel == 0)) {
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 (_mesa_is_format_compressed(intelImage->base.TexFormat))
131 comp_byte = intel_compressed_num_bytes(intelImage->base.TexFormat);
132
133 texelBytes = _mesa_get_format_bytes(intelImage->base.TexFormat);
134
135 intelObj->mt = intel_miptree_create(intel,
136 intelObj->base.Target,
137 intelImage->base._BaseFormat,
138 intelImage->base.InternalFormat,
139 firstLevel,
140 lastLevel,
141 width,
142 height,
143 depth,
144 texelBytes,
145 comp_byte,
146 expect_accelerated_upload);
147
148 DBG("%s - success\n", __FUNCTION__);
149 }
150
151
152
153
154 static GLuint
155 target_to_face(GLenum target)
156 {
157 switch (target) {
158 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
159 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
160 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
161 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
162 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
163 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
164 return ((GLuint) target - (GLuint) GL_TEXTURE_CUBE_MAP_POSITIVE_X);
165 default:
166 return 0;
167 }
168 }
169
170 /* There are actually quite a few combinations this will work for,
171 * more than what I've listed here.
172 */
173 static GLboolean
174 check_pbo_format(GLint internalFormat,
175 GLenum format, GLenum type,
176 gl_format mesa_format)
177 {
178 switch (internalFormat) {
179 case 4:
180 case GL_RGBA:
181 return (format == GL_BGRA &&
182 (type == GL_UNSIGNED_BYTE ||
183 type == GL_UNSIGNED_INT_8_8_8_8_REV) &&
184 mesa_format == MESA_FORMAT_ARGB8888);
185 case 3:
186 case GL_RGB:
187 return (format == GL_RGB &&
188 type == GL_UNSIGNED_SHORT_5_6_5 &&
189 mesa_format == MESA_FORMAT_RGB565);
190 case GL_YCBCR_MESA:
191 return (type == GL_UNSIGNED_SHORT_8_8_MESA || type == GL_UNSIGNED_BYTE);
192 default:
193 return GL_FALSE;
194 }
195 }
196
197
198 /* XXX: Do this for TexSubImage also:
199 */
200 static GLboolean
201 try_pbo_upload(struct intel_context *intel,
202 struct intel_texture_image *intelImage,
203 const struct gl_pixelstore_attrib *unpack,
204 GLint internalFormat,
205 GLint width, GLint height,
206 GLenum format, GLenum type, const void *pixels)
207 {
208 struct intel_buffer_object *pbo = intel_buffer_object(unpack->BufferObj);
209 GLuint src_offset, src_stride;
210 GLuint dst_x, dst_y, dst_stride;
211 dri_bo *dst_buffer = intel_region_buffer(intel,
212 intelImage->mt->region,
213 INTEL_WRITE_FULL);
214
215 if (!_mesa_is_bufferobj(unpack->BufferObj) ||
216 intel->ctx._ImageTransferState ||
217 unpack->SkipPixels || unpack->SkipRows) {
218 DBG("%s: failure 1\n", __FUNCTION__);
219 return GL_FALSE;
220 }
221
222 /* note: potential 64-bit ptr to 32-bit int cast */
223 src_offset = (GLuint) (unsigned long) pixels;
224
225 if (unpack->RowLength > 0)
226 src_stride = unpack->RowLength;
227 else
228 src_stride = width;
229
230 intel_miptree_get_image_offset(intelImage->mt, intelImage->level,
231 intelImage->face, 0,
232 &dst_x, &dst_y);
233
234 dst_stride = intelImage->mt->pitch;
235
236 if (drm_intel_bo_references(intel->batch->buf, dst_buffer))
237 intelFlush(&intel->ctx);
238 {
239 dri_bo *src_buffer = intel_bufferobj_buffer(intel, pbo, INTEL_READ);
240
241 if (!intelEmitCopyBlit(intel,
242 intelImage->mt->cpp,
243 src_stride, src_buffer, src_offset, GL_FALSE,
244 dst_stride, dst_buffer, 0, GL_FALSE,
245 0, 0, dst_x, dst_y, width, height,
246 GL_COPY)) {
247 return GL_FALSE;
248 }
249 }
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_x, dst_y, 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 intel_miptree_get_image_offset(intelImage->mt, intelImage->level,
283 intelImage->face, 0,
284 &dst_x, &dst_y);
285
286 dst_stride = intelImage->mt->pitch;
287
288 if (src_stride != dst_stride || dst_x != 0 || dst_y != 0 ||
289 src_offset != 0) {
290 DBG("%s: failure 2\n", __FUNCTION__);
291 return GL_FALSE;
292 }
293
294 intel_region_attach_pbo(intel, intelImage->mt->region, pbo);
295
296 return GL_TRUE;
297 }
298
299
300 static void
301 intelTexImage(GLcontext * ctx,
302 GLint dims,
303 GLenum target, GLint level,
304 GLint internalFormat,
305 GLint width, GLint height, GLint depth,
306 GLint border,
307 GLenum format, GLenum type, const void *pixels,
308 const struct gl_pixelstore_attrib *unpack,
309 struct gl_texture_object *texObj,
310 struct gl_texture_image *texImage, GLsizei imageSize,
311 GLboolean compressed)
312 {
313 struct intel_context *intel = intel_context(ctx);
314 struct intel_texture_object *intelObj = intel_texture_object(texObj);
315 struct intel_texture_image *intelImage = intel_texture_image(texImage);
316 GLint postConvWidth = width;
317 GLint postConvHeight = height;
318 GLint texelBytes, sizeInBytes;
319 GLuint dstRowStride = 0, srcRowStride = texImage->RowStride;
320
321 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__,
322 _mesa_lookup_enum_by_nr(target), level, width, height, depth, border);
323
324 intelImage->face = target_to_face(target);
325 intelImage->level = level;
326
327 if (ctx->_ImageTransferState & IMAGE_CONVOLUTION_BIT) {
328 _mesa_adjust_image_for_convolution(ctx, dims, &postConvWidth,
329 &postConvHeight);
330 }
331
332 if (_mesa_is_format_compressed(texImage->TexFormat)) {
333 texelBytes = 0;
334 }
335 else {
336 texelBytes = _mesa_get_format_bytes(texImage->TexFormat);
337
338 /* Minimum pitch of 32 bytes */
339 if (postConvWidth * texelBytes < 32) {
340 postConvWidth = 32 / texelBytes;
341 texImage->RowStride = postConvWidth;
342 }
343
344 if (!intelImage->mt) {
345 assert(texImage->RowStride == postConvWidth);
346 }
347 }
348
349 /* Release the reference to a potentially orphaned buffer.
350 * Release any old malloced memory.
351 */
352 if (intelImage->mt) {
353 intel_miptree_release(intel, &intelImage->mt);
354 assert(!texImage->Data);
355 }
356 else if (texImage->Data) {
357 _mesa_free_texmemory(texImage->Data);
358 texImage->Data = NULL;
359 }
360
361 /* If this is the only texture image in the tree, could call
362 * bmBufferData with NULL data to free the old block and avoid
363 * waiting on any outstanding fences.
364 */
365 if (intelObj->mt &&
366 intelObj->mt->first_level == level &&
367 intelObj->mt->last_level == level &&
368 intelObj->mt->target != GL_TEXTURE_CUBE_MAP_ARB &&
369 !intel_miptree_match_image(intelObj->mt, &intelImage->base)) {
370
371 DBG("release it\n");
372 intel_miptree_release(intel, &intelObj->mt);
373 assert(!intelObj->mt);
374 }
375
376 if (!intelObj->mt) {
377 guess_and_alloc_mipmap_tree(intel, intelObj, intelImage, pixels == NULL);
378 if (!intelObj->mt) {
379 DBG("guess_and_alloc_mipmap_tree: failed\n");
380 }
381 }
382
383 assert(!intelImage->mt);
384
385 if (intelObj->mt &&
386 intel_miptree_match_image(intelObj->mt, &intelImage->base)) {
387
388 intel_miptree_reference(&intelImage->mt, intelObj->mt);
389 assert(intelImage->mt);
390 } else if (intelImage->base.Border == 0) {
391 int comp_byte = 0;
392 GLuint texelBytes = _mesa_get_format_bytes(intelImage->base.TexFormat);
393 GLenum baseFormat = _mesa_get_format_base_format(intelImage->base.TexFormat);
394 if (_mesa_is_format_compressed(intelImage->base.TexFormat)) {
395 comp_byte =
396 intel_compressed_num_bytes(intelImage->base.TexFormat);
397 }
398
399 /* Didn't fit in the object miptree, but it's suitable for inclusion in
400 * a miptree, so create one just for our level and store it in the image.
401 * It'll get moved into the object miptree at validate time.
402 */
403 intelImage->mt = intel_miptree_create(intel, target,
404 baseFormat,
405 internalFormat,
406 level, level,
407 width, height, depth,
408 texelBytes,
409 comp_byte, pixels == NULL);
410
411 }
412
413 /* PBO fastpaths:
414 */
415 if (dims <= 2 &&
416 intelImage->mt &&
417 _mesa_is_bufferobj(unpack->BufferObj) &&
418 check_pbo_format(internalFormat, format,
419 type, intelImage->base.TexFormat)) {
420
421 DBG("trying pbo upload\n");
422
423 /* Attempt to texture directly from PBO data (zero copy upload).
424 *
425 * Currently disable as it can lead to worse as well as better
426 * performance (in particular when intel_region_cow() is
427 * required).
428 */
429 if (intelObj->mt == intelImage->mt &&
430 intelObj->mt->first_level == level &&
431 intelObj->mt->last_level == level) {
432
433 if (try_pbo_zcopy(intel, intelImage, unpack,
434 internalFormat,
435 width, height, format, type, pixels)) {
436
437 DBG("pbo zcopy upload succeeded\n");
438 return;
439 }
440 }
441
442
443 /* Otherwise, attempt to use the blitter for PBO image uploads.
444 */
445 if (try_pbo_upload(intel, intelImage, unpack,
446 internalFormat,
447 width, height, format, type, pixels)) {
448 DBG("pbo upload succeeded\n");
449 return;
450 }
451
452 DBG("pbo upload failed\n");
453 }
454
455 /* intelCopyTexImage calls this function with pixels == NULL, with
456 * the expectation that the mipmap tree will be set up but nothing
457 * more will be done. This is where those calls return:
458 */
459 if (compressed) {
460 pixels = _mesa_validate_pbo_compressed_teximage(ctx, imageSize, pixels,
461 unpack,
462 "glCompressedTexImage");
463 } else {
464 pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, 1,
465 format, type,
466 pixels, unpack, "glTexImage");
467 }
468
469 if (intelImage->mt) {
470 if (pixels != NULL) {
471 /* Flush any queued rendering with the texture before mapping. */
472 if (drm_intel_bo_references(intel->batch->buf,
473 intelImage->mt->region->buffer)) {
474 intelFlush(ctx);
475 }
476 texImage->Data = intel_miptree_image_map(intel,
477 intelImage->mt,
478 intelImage->face,
479 intelImage->level,
480 &dstRowStride,
481 intelImage->base.ImageOffsets);
482 }
483
484 texImage->RowStride = dstRowStride / intelImage->mt->cpp;
485 }
486 else {
487 /* Allocate regular memory and store the image there temporarily. */
488 if (_mesa_is_format_compressed(texImage->TexFormat)) {
489 sizeInBytes = _mesa_format_image_size(texImage->TexFormat,
490 texImage->Width,
491 texImage->Height,
492 texImage->Depth);
493 dstRowStride =
494 _mesa_format_row_stride(texImage->TexFormat, width);
495 assert(dims != 3);
496 }
497 else {
498 dstRowStride = postConvWidth * texelBytes;
499 sizeInBytes = depth * dstRowStride * postConvHeight;
500 }
501
502 texImage->Data = _mesa_alloc_texmemory(sizeInBytes);
503 }
504
505 DBG("Upload image %dx%dx%d row_len %d "
506 "pitch %d pixels %d compressed %d\n",
507 width, height, depth, width * texelBytes, dstRowStride,
508 pixels ? 1 : 0, compressed);
509
510 /* Copy data. Would like to know when it's ok for us to eg. use
511 * the blitter to copy. Or, use the hardware to do the format
512 * conversion and copy:
513 */
514 if (pixels) {
515 if (compressed) {
516 if (intelImage->mt) {
517 struct intel_region *dst = intelImage->mt->region;
518 _mesa_copy_rect(texImage->Data, dst->cpp, dst->pitch,
519 0, 0,
520 intelImage->mt->level[level].width,
521 (intelImage->mt->level[level].height+3)/4,
522 pixels,
523 srcRowStride,
524 0, 0);
525 }
526 else {
527 memcpy(texImage->Data, pixels, imageSize);
528 }
529 }
530 else if (!_mesa_texstore(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 _mesa_unmap_teximage_pbo(ctx, unpack);
543
544 if (intelImage->mt) {
545 if (pixels != NULL)
546 intel_miptree_image_unmap(intel, intelImage->mt);
547 texImage->Data = NULL;
548 }
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 }
665 else {
666 _mesa_get_teximage(ctx, target, level, format, type, pixels,
667 texObj, texImage);
668 }
669
670
671 /* Unmap */
672 if (intelImage->mt) {
673 intel_miptree_image_unmap(intel, intelImage->mt);
674 intelImage->base.Data = NULL;
675 }
676 }
677
678
679 static void
680 intelGetTexImage(GLcontext * ctx, GLenum target, GLint level,
681 GLenum format, GLenum type, GLvoid * pixels,
682 struct gl_texture_object *texObj,
683 struct gl_texture_image *texImage)
684 {
685 intel_get_tex_image(ctx, target, level, format, type, pixels,
686 texObj, texImage, GL_FALSE);
687 }
688
689
690 static void
691 intelGetCompressedTexImage(GLcontext *ctx, GLenum target, GLint level,
692 GLvoid *pixels,
693 struct gl_texture_object *texObj,
694 struct gl_texture_image *texImage)
695 {
696 intel_get_tex_image(ctx, target, level, 0, 0, pixels,
697 texObj, texImage, GL_TRUE);
698 }
699
700
701 void
702 intelSetTexOffset(__DRIcontext *pDRICtx, GLint texname,
703 unsigned long long offset, GLint depth, GLuint pitch)
704 {
705 struct intel_context *intel = pDRICtx->driverPrivate;
706 struct gl_texture_object *tObj = _mesa_lookup_texture(&intel->ctx, texname);
707 struct intel_texture_object *intelObj = intel_texture_object(tObj);
708
709 if (!intelObj)
710 return;
711
712 if (intelObj->mt)
713 intel_miptree_release(intel, &intelObj->mt);
714
715 intelObj->imageOverride = GL_TRUE;
716 intelObj->depthOverride = depth;
717 intelObj->pitchOverride = pitch;
718
719 if (offset)
720 intelObj->textureOffset = offset;
721 }
722
723 void
724 intelSetTexBuffer2(__DRIcontext *pDRICtx, GLint target,
725 GLint glx_texture_format,
726 __DRIdrawable *dPriv)
727 {
728 struct gl_framebuffer *fb = dPriv->driverPrivate;
729 struct intel_context *intel = pDRICtx->driverPrivate;
730 GLcontext *ctx = &intel->ctx;
731 struct intel_texture_object *intelObj;
732 struct intel_texture_image *intelImage;
733 struct intel_mipmap_tree *mt;
734 struct intel_renderbuffer *rb;
735 struct gl_texture_object *texObj;
736 struct gl_texture_image *texImage;
737 int level = 0, internalFormat;
738
739 texObj = _mesa_get_current_tex_object(ctx, target);
740 intelObj = intel_texture_object(texObj);
741
742 if (!intelObj)
743 return;
744
745 intel_update_renderbuffers(pDRICtx, dPriv);
746
747 rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
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 if (glx_texture_format == GLX_TEXTURE_FORMAT_RGB_EXT)
755 internalFormat = GL_RGB;
756 else
757 internalFormat = GL_RGBA;
758
759 mt = intel_miptree_create_for_region(intel, target,
760 internalFormat,
761 0, 0, rb->region, 1, 0);
762 if (mt == NULL)
763 return;
764
765 _mesa_lock_texture(&intel->ctx, texObj);
766
767 texImage = _mesa_get_tex_image(&intel->ctx, texObj, target, level);
768 intelImage = intel_texture_image(texImage);
769
770 if (intelImage->mt) {
771 intel_miptree_release(intel, &intelImage->mt);
772 assert(!texImage->Data);
773 }
774 if (intelObj->mt)
775 intel_miptree_release(intel, &intelObj->mt);
776
777 intelObj->mt = mt;
778 _mesa_init_teximage_fields(&intel->ctx, target, texImage,
779 rb->region->width, rb->region->height, 1,
780 0, internalFormat);
781
782 intelImage->face = target_to_face(target);
783 intelImage->level = level;
784 if (glx_texture_format == GLX_TEXTURE_FORMAT_RGB_EXT)
785 texImage->TexFormat = MESA_FORMAT_XRGB8888;
786 else
787 texImage->TexFormat = MESA_FORMAT_ARGB8888;
788 texImage->RowStride = rb->region->pitch;
789 intel_miptree_reference(&intelImage->mt, intelObj->mt);
790
791 if (!intel_miptree_match_image(intelObj->mt, &intelImage->base)) {
792 fprintf(stderr, "miptree doesn't match image\n");
793 }
794
795 _mesa_unlock_texture(&intel->ctx, texObj);
796 }
797
798 void
799 intelSetTexBuffer(__DRIcontext *pDRICtx, GLint target, __DRIdrawable *dPriv)
800 {
801 /* The old interface didn't have the format argument, so copy our
802 * implementation's behavior at the time.
803 */
804 intelSetTexBuffer2(pDRICtx, target, GLX_TEXTURE_FORMAT_RGBA_EXT, dPriv);
805 }
806
807
808 void
809 intelInitTextureImageFuncs(struct dd_function_table *functions)
810 {
811 functions->TexImage1D = intelTexImage1D;
812 functions->TexImage2D = intelTexImage2D;
813 functions->TexImage3D = intelTexImage3D;
814 functions->GetTexImage = intelGetTexImage;
815
816 functions->CompressedTexImage2D = intelCompressedTexImage2D;
817 functions->GetCompressedTexImage = intelGetCompressedTexImage;
818 }