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