mesa: Remove EXT_convolution.
[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/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 (intel->gen < 4 || firstLevel == 0)) {
118 lastLevel = firstLevel;
119 }
120 else {
121 l2width = logbase2(width);
122 l2height = logbase2(height);
123 l2depth = logbase2(depth);
124 lastLevel = firstLevel + MAX2(MAX2(l2width, l2height), l2depth);
125 }
126
127 assert(!intelObj->mt);
128 if (_mesa_is_format_compressed(intelImage->base.TexFormat))
129 comp_byte = intel_compressed_num_bytes(intelImage->base.TexFormat);
130
131 texelBytes = _mesa_get_format_bytes(intelImage->base.TexFormat);
132
133 intelObj->mt = intel_miptree_create(intel,
134 intelObj->base.Target,
135 intelImage->base._BaseFormat,
136 intelImage->base.InternalFormat,
137 firstLevel,
138 lastLevel,
139 width,
140 height,
141 depth,
142 texelBytes,
143 comp_byte,
144 expect_accelerated_upload);
145
146 DBG("%s - success\n", __FUNCTION__);
147 }
148
149
150
151
152 static GLuint
153 target_to_face(GLenum target)
154 {
155 switch (target) {
156 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
157 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
158 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
159 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
160 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
161 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
162 return ((GLuint) target - (GLuint) GL_TEXTURE_CUBE_MAP_POSITIVE_X);
163 default:
164 return 0;
165 }
166 }
167
168 /* There are actually quite a few combinations this will work for,
169 * more than what I've listed here.
170 */
171 static GLboolean
172 check_pbo_format(GLint internalFormat,
173 GLenum format, GLenum type,
174 gl_format mesa_format)
175 {
176 switch (internalFormat) {
177 case 4:
178 case GL_RGBA:
179 case GL_RGBA8:
180 return (format == GL_BGRA &&
181 (type == GL_UNSIGNED_BYTE ||
182 type == GL_UNSIGNED_INT_8_8_8_8_REV) &&
183 mesa_format == MESA_FORMAT_ARGB8888);
184 case 3:
185 case GL_RGB:
186 return (format == GL_RGB &&
187 type == GL_UNSIGNED_SHORT_5_6_5 &&
188 mesa_format == MESA_FORMAT_RGB565);
189 case 1:
190 case GL_LUMINANCE:
191 return (format == GL_LUMINANCE &&
192 type == GL_UNSIGNED_BYTE &&
193 mesa_format == MESA_FORMAT_L8);
194 case GL_YCBCR_MESA:
195 return (type == GL_UNSIGNED_SHORT_8_8_MESA || type == GL_UNSIGNED_BYTE);
196 default:
197 return GL_FALSE;
198 }
199 }
200
201
202 /* XXX: Do this for TexSubImage also:
203 */
204 static GLboolean
205 try_pbo_upload(struct intel_context *intel,
206 struct intel_texture_image *intelImage,
207 const struct gl_pixelstore_attrib *unpack,
208 GLint internalFormat,
209 GLint width, GLint height,
210 GLenum format, GLenum type, const void *pixels)
211 {
212 struct intel_buffer_object *pbo = intel_buffer_object(unpack->BufferObj);
213 GLuint src_offset, src_stride;
214 GLuint dst_x, dst_y, dst_stride;
215 drm_intel_bo *dst_buffer = intel_region_buffer(intel,
216 intelImage->mt->region,
217 INTEL_WRITE_FULL);
218
219 if (!_mesa_is_bufferobj(unpack->BufferObj) ||
220 intel->ctx._ImageTransferState ||
221 unpack->SkipPixels || unpack->SkipRows) {
222 DBG("%s: failure 1\n", __FUNCTION__);
223 return GL_FALSE;
224 }
225
226 /* note: potential 64-bit ptr to 32-bit int cast */
227 src_offset = (GLuint) (unsigned long) pixels;
228
229 if (unpack->RowLength > 0)
230 src_stride = unpack->RowLength;
231 else
232 src_stride = width;
233
234 intel_miptree_get_image_offset(intelImage->mt, intelImage->level,
235 intelImage->face, 0,
236 &dst_x, &dst_y);
237
238 dst_stride = intelImage->mt->region->pitch;
239
240 if (drm_intel_bo_references(intel->batch->buf, dst_buffer))
241 intel_flush(&intel->ctx);
242
243 {
244 drm_intel_bo *src_buffer = intel_bufferobj_buffer(intel, pbo, INTEL_READ);
245
246 if (!intelEmitCopyBlit(intel,
247 intelImage->mt->cpp,
248 src_stride, src_buffer, src_offset, GL_FALSE,
249 dst_stride, dst_buffer, 0,
250 intelImage->mt->region->tiling,
251 0, 0, dst_x, dst_y, width, height,
252 GL_COPY)) {
253 return GL_FALSE;
254 }
255 }
256
257 return GL_TRUE;
258 }
259
260
261 static GLboolean
262 try_pbo_zcopy(struct intel_context *intel,
263 struct intel_texture_image *intelImage,
264 const struct gl_pixelstore_attrib *unpack,
265 GLint internalFormat,
266 GLint width, GLint height,
267 GLenum format, GLenum type, const void *pixels)
268 {
269 struct intel_buffer_object *pbo = intel_buffer_object(unpack->BufferObj);
270 GLuint src_offset, src_stride;
271 GLuint dst_x, dst_y, dst_stride;
272
273 if (!_mesa_is_bufferobj(unpack->BufferObj) ||
274 intel->ctx._ImageTransferState ||
275 unpack->SkipPixels || unpack->SkipRows) {
276 DBG("%s: failure 1\n", __FUNCTION__);
277 return GL_FALSE;
278 }
279
280 /* note: potential 64-bit ptr to 32-bit int cast */
281 src_offset = (GLuint) (unsigned long) pixels;
282
283 if (unpack->RowLength > 0)
284 src_stride = unpack->RowLength;
285 else
286 src_stride = width;
287
288 intel_miptree_get_image_offset(intelImage->mt, intelImage->level,
289 intelImage->face, 0,
290 &dst_x, &dst_y);
291
292 dst_stride = intelImage->mt->region->pitch;
293
294 if (src_stride != dst_stride || dst_x != 0 || dst_y != 0 ||
295 src_offset != 0) {
296 DBG("%s: failure 2\n", __FUNCTION__);
297 return GL_FALSE;
298 }
299
300 intel_region_attach_pbo(intel, intelImage->mt->region, pbo);
301
302 return GL_TRUE;
303 }
304
305
306 static void
307 intelTexImage(GLcontext * ctx,
308 GLint dims,
309 GLenum target, GLint level,
310 GLint internalFormat,
311 GLint width, GLint height, GLint depth,
312 GLint border,
313 GLenum format, GLenum type, const void *pixels,
314 const struct gl_pixelstore_attrib *unpack,
315 struct gl_texture_object *texObj,
316 struct gl_texture_image *texImage, GLsizei imageSize,
317 GLboolean compressed)
318 {
319 struct intel_context *intel = intel_context(ctx);
320 struct intel_texture_object *intelObj = intel_texture_object(texObj);
321 struct intel_texture_image *intelImage = intel_texture_image(texImage);
322 GLint texelBytes, sizeInBytes;
323 GLuint dstRowStride = 0, srcRowStride = texImage->RowStride;
324
325 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__,
326 _mesa_lookup_enum_by_nr(target), level, width, height, depth, border);
327
328 intelImage->face = target_to_face(target);
329 intelImage->level = level;
330
331 if (_mesa_is_format_compressed(texImage->TexFormat)) {
332 texelBytes = 0;
333 }
334 else {
335 texelBytes = _mesa_get_format_bytes(texImage->TexFormat);
336
337 /* Minimum pitch of 32 bytes */
338 if (width * texelBytes < 32) {
339 width = 32 / texelBytes;
340 texImage->RowStride = width;
341 }
342
343 if (!intelImage->mt) {
344 assert(texImage->RowStride == width);
345 }
346 }
347
348 /* Release the reference to a potentially orphaned buffer.
349 * Release any old malloced memory.
350 */
351 if (intelImage->mt) {
352 intel_miptree_release(intel, &intelImage->mt);
353 assert(!texImage->Data);
354 }
355 else if (texImage->Data) {
356 _mesa_free_texmemory(texImage->Data);
357 texImage->Data = NULL;
358 }
359
360 /* If this is the only texture image in the tree, could call
361 * bmBufferData with NULL data to free the old block and avoid
362 * waiting on any outstanding fences.
363 */
364 if (intelObj->mt &&
365 intelObj->mt->first_level == level &&
366 intelObj->mt->last_level == level &&
367 intelObj->mt->target != GL_TEXTURE_CUBE_MAP_ARB &&
368 !intel_miptree_match_image(intelObj->mt, &intelImage->base)) {
369
370 DBG("release it\n");
371 intel_miptree_release(intel, &intelObj->mt);
372 assert(!intelObj->mt);
373 }
374
375 if (!intelObj->mt) {
376 guess_and_alloc_mipmap_tree(intel, intelObj, intelImage, pixels == NULL);
377 if (!intelObj->mt) {
378 DBG("guess_and_alloc_mipmap_tree: failed\n");
379 }
380 }
381
382 assert(!intelImage->mt);
383
384 if (intelObj->mt &&
385 intel_miptree_match_image(intelObj->mt, &intelImage->base)) {
386
387 intel_miptree_reference(&intelImage->mt, intelObj->mt);
388 assert(intelImage->mt);
389 } else if (intelImage->base.Border == 0) {
390 int comp_byte = 0;
391 GLuint texelBytes = _mesa_get_format_bytes(intelImage->base.TexFormat);
392 GLenum baseFormat = _mesa_get_format_base_format(intelImage->base.TexFormat);
393 if (_mesa_is_format_compressed(intelImage->base.TexFormat)) {
394 comp_byte =
395 intel_compressed_num_bytes(intelImage->base.TexFormat);
396 }
397
398 /* Didn't fit in the object miptree, but it's suitable for inclusion in
399 * a miptree, so create one just for our level and store it in the image.
400 * It'll get moved into the object miptree at validate time.
401 */
402 intelImage->mt = intel_miptree_create(intel, target,
403 baseFormat,
404 internalFormat,
405 level, level,
406 width, height, depth,
407 texelBytes,
408 comp_byte, pixels == NULL);
409
410 }
411
412 /* PBO fastpaths:
413 */
414 if (dims <= 2 &&
415 intelImage->mt &&
416 _mesa_is_bufferobj(unpack->BufferObj) &&
417 check_pbo_format(internalFormat, format,
418 type, intelImage->base.TexFormat)) {
419
420 DBG("trying pbo upload\n");
421
422 /* Attempt to texture directly from PBO data (zero copy upload).
423 *
424 * Currently disable as it can lead to worse as well as better
425 * performance (in particular when intel_region_cow() is
426 * required).
427 */
428 if (intelObj->mt == intelImage->mt &&
429 intelObj->mt->first_level == level &&
430 intelObj->mt->last_level == level) {
431
432 if (try_pbo_zcopy(intel, intelImage, unpack,
433 internalFormat,
434 width, height, format, type, pixels)) {
435
436 DBG("pbo zcopy upload succeeded\n");
437 return;
438 }
439 }
440
441
442 /* Otherwise, attempt to use the blitter for PBO image uploads.
443 */
444 if (try_pbo_upload(intel, intelImage, unpack,
445 internalFormat,
446 width, height, format, type, pixels)) {
447 DBG("pbo upload succeeded\n");
448 return;
449 }
450
451 DBG("pbo upload failed\n");
452 }
453
454 /* intelCopyTexImage calls this function with pixels == NULL, with
455 * the expectation that the mipmap tree will be set up but nothing
456 * more will be done. This is where those calls return:
457 */
458 if (compressed) {
459 pixels = _mesa_validate_pbo_compressed_teximage(ctx, imageSize, pixels,
460 unpack,
461 "glCompressedTexImage");
462 } else {
463 pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, 1,
464 format, type,
465 pixels, unpack, "glTexImage");
466 }
467
468 if (intelImage->mt) {
469 if (pixels != NULL) {
470 /* Flush any queued rendering with the texture before mapping. */
471 if (drm_intel_bo_references(intel->batch->buf,
472 intelImage->mt->region->buffer)) {
473 intel_flush(ctx);
474 }
475 texImage->Data = intel_miptree_image_map(intel,
476 intelImage->mt,
477 intelImage->face,
478 intelImage->level,
479 &dstRowStride,
480 intelImage->base.ImageOffsets);
481 }
482
483 texImage->RowStride = dstRowStride / intelImage->mt->cpp;
484 }
485 else {
486 /* Allocate regular memory and store the image there temporarily. */
487 if (_mesa_is_format_compressed(texImage->TexFormat)) {
488 sizeInBytes = _mesa_format_image_size(texImage->TexFormat,
489 texImage->Width,
490 texImage->Height,
491 texImage->Depth);
492 dstRowStride =
493 _mesa_format_row_stride(texImage->TexFormat, width);
494 assert(dims != 3);
495 }
496 else {
497 dstRowStride = width * texelBytes;
498 sizeInBytes = depth * dstRowStride * height;
499 }
500
501 texImage->Data = _mesa_alloc_texmemory(sizeInBytes);
502 }
503
504 DBG("Upload image %dx%dx%d row_len %d "
505 "pitch %d pixels %d compressed %d\n",
506 width, height, depth, width * texelBytes, dstRowStride,
507 pixels ? 1 : 0, compressed);
508
509 /* Copy data. Would like to know when it's ok for us to eg. use
510 * the blitter to copy. Or, use the hardware to do the format
511 * conversion and copy:
512 */
513 if (pixels) {
514 if (compressed) {
515 if (intelImage->mt) {
516 struct intel_region *dst = intelImage->mt->region;
517 _mesa_copy_rect(texImage->Data, dst->cpp, dst->pitch,
518 0, 0,
519 intelImage->mt->level[level].width,
520 (intelImage->mt->level[level].height+3)/4,
521 pixels,
522 srcRowStride,
523 0, 0);
524 }
525 else {
526 memcpy(texImage->Data, pixels, imageSize);
527 }
528 }
529 else if (!_mesa_texstore(ctx, dims,
530 texImage->_BaseFormat,
531 texImage->TexFormat,
532 texImage->Data, 0, 0, 0, /* dstX/Y/Zoffset */
533 dstRowStride,
534 texImage->ImageOffsets,
535 width, height, depth,
536 format, type, pixels, unpack)) {
537 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
538 }
539 }
540
541 _mesa_unmap_teximage_pbo(ctx, unpack);
542
543 if (intelImage->mt) {
544 if (pixels != NULL)
545 intel_miptree_image_unmap(intel, intelImage->mt);
546 texImage->Data = NULL;
547 }
548 }
549
550
551 static void
552 intelTexImage3D(GLcontext * ctx,
553 GLenum target, GLint level,
554 GLint internalFormat,
555 GLint width, GLint height, GLint depth,
556 GLint border,
557 GLenum format, GLenum type, const void *pixels,
558 const struct gl_pixelstore_attrib *unpack,
559 struct gl_texture_object *texObj,
560 struct gl_texture_image *texImage)
561 {
562 intelTexImage(ctx, 3, target, level,
563 internalFormat, width, height, depth, border,
564 format, type, pixels, unpack, texObj, texImage, 0, GL_FALSE);
565 }
566
567
568 static void
569 intelTexImage2D(GLcontext * ctx,
570 GLenum target, GLint level,
571 GLint internalFormat,
572 GLint width, GLint height, GLint border,
573 GLenum format, GLenum type, const void *pixels,
574 const struct gl_pixelstore_attrib *unpack,
575 struct gl_texture_object *texObj,
576 struct gl_texture_image *texImage)
577 {
578 intelTexImage(ctx, 2, target, level,
579 internalFormat, width, height, 1, border,
580 format, type, pixels, unpack, texObj, texImage, 0, GL_FALSE);
581 }
582
583
584 static void
585 intelTexImage1D(GLcontext * ctx,
586 GLenum target, GLint level,
587 GLint internalFormat,
588 GLint width, GLint border,
589 GLenum format, GLenum type, const void *pixels,
590 const struct gl_pixelstore_attrib *unpack,
591 struct gl_texture_object *texObj,
592 struct gl_texture_image *texImage)
593 {
594 intelTexImage(ctx, 1, target, level,
595 internalFormat, width, 1, 1, border,
596 format, type, pixels, unpack, texObj, texImage, 0, GL_FALSE);
597 }
598
599
600 static void
601 intelCompressedTexImage2D( GLcontext *ctx, GLenum target, GLint level,
602 GLint internalFormat,
603 GLint width, GLint height, GLint border,
604 GLsizei imageSize, const GLvoid *data,
605 struct gl_texture_object *texObj,
606 struct gl_texture_image *texImage )
607 {
608 intelTexImage(ctx, 2, target, level,
609 internalFormat, width, height, 1, border,
610 0, 0, data, &ctx->Unpack, texObj, texImage, imageSize, GL_TRUE);
611 }
612
613
614 /**
615 * Need to map texture image into memory before copying image data,
616 * then unmap it.
617 */
618 static void
619 intel_get_tex_image(GLcontext * ctx, GLenum target, GLint level,
620 GLenum format, GLenum type, GLvoid * pixels,
621 struct gl_texture_object *texObj,
622 struct gl_texture_image *texImage, GLboolean compressed)
623 {
624 struct intel_context *intel = intel_context(ctx);
625 struct intel_texture_image *intelImage = intel_texture_image(texImage);
626
627 /* If we're reading from a texture that has been rendered to, need to
628 * make sure rendering is complete.
629 * We could probably predicate this on texObj->_RenderToTexture
630 */
631 intel_flush(ctx);
632
633 /* Map */
634 if (intelImage->mt) {
635 /* Image is stored in hardware format in a buffer managed by the
636 * kernel. Need to explicitly map and unmap it.
637 */
638 intelImage->base.Data =
639 intel_miptree_image_map(intel,
640 intelImage->mt,
641 intelImage->face,
642 intelImage->level,
643 &intelImage->base.RowStride,
644 intelImage->base.ImageOffsets);
645 intelImage->base.RowStride /= intelImage->mt->cpp;
646 }
647 else {
648 /* Otherwise, the image should actually be stored in
649 * intelImage->base.Data. This is pretty confusing for
650 * everybody, I'd much prefer to separate the two functions of
651 * texImage->Data - storage for texture images in main memory
652 * and access (ie mappings) of images. In other words, we'd
653 * create a new texImage->Map field and leave Data simply for
654 * storage.
655 */
656 assert(intelImage->base.Data);
657 }
658
659
660 if (compressed) {
661 _mesa_get_compressed_teximage(ctx, target, level, pixels,
662 texObj, texImage);
663 }
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 void
700 intelSetTexBuffer2(__DRIcontext *pDRICtx, GLint target,
701 GLint texture_format,
702 __DRIdrawable *dPriv)
703 {
704 struct gl_framebuffer *fb = dPriv->driverPrivate;
705 struct intel_context *intel = pDRICtx->driverPrivate;
706 GLcontext *ctx = &intel->ctx;
707 struct intel_texture_object *intelObj;
708 struct intel_texture_image *intelImage;
709 struct intel_mipmap_tree *mt;
710 struct intel_renderbuffer *rb;
711 struct gl_texture_object *texObj;
712 struct gl_texture_image *texImage;
713 int level = 0, internalFormat;
714
715 texObj = _mesa_get_current_tex_object(ctx, target);
716 intelObj = intel_texture_object(texObj);
717
718 if (!intelObj)
719 return;
720
721 if (dPriv->lastStamp != dPriv->dri2.stamp ||
722 !pDRICtx->driScreenPriv->dri2.useInvalidate)
723 intel_update_renderbuffers(pDRICtx, dPriv);
724
725 rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
726 /* If the region isn't set, then intel_update_renderbuffers was unable
727 * to get the buffers for the drawable.
728 */
729 if (rb->region == NULL)
730 return;
731
732 if (texture_format == __DRI_TEXTURE_FORMAT_RGB)
733 internalFormat = GL_RGB;
734 else
735 internalFormat = GL_RGBA;
736
737 mt = intel_miptree_create_for_region(intel, target,
738 internalFormat,
739 0, 0, rb->region, 1, 0);
740 if (mt == NULL)
741 return;
742
743 _mesa_lock_texture(&intel->ctx, texObj);
744
745 texImage = _mesa_get_tex_image(&intel->ctx, texObj, target, level);
746 intelImage = intel_texture_image(texImage);
747
748 if (intelImage->mt) {
749 intel_miptree_release(intel, &intelImage->mt);
750 assert(!texImage->Data);
751 }
752 if (intelObj->mt)
753 intel_miptree_release(intel, &intelObj->mt);
754
755 intelObj->mt = mt;
756 _mesa_init_teximage_fields(&intel->ctx, target, texImage,
757 rb->region->width, rb->region->height, 1,
758 0, internalFormat);
759
760 intelImage->face = target_to_face(target);
761 intelImage->level = level;
762 if (texture_format == __DRI_TEXTURE_FORMAT_RGB)
763 texImage->TexFormat = MESA_FORMAT_XRGB8888;
764 else
765 texImage->TexFormat = MESA_FORMAT_ARGB8888;
766 texImage->RowStride = rb->region->pitch;
767 intel_miptree_reference(&intelImage->mt, intelObj->mt);
768
769 if (!intel_miptree_match_image(intelObj->mt, &intelImage->base)) {
770 fprintf(stderr, "miptree doesn't match image\n");
771 }
772
773 _mesa_unlock_texture(&intel->ctx, texObj);
774 }
775
776 void
777 intelSetTexBuffer(__DRIcontext *pDRICtx, GLint target, __DRIdrawable *dPriv)
778 {
779 /* The old interface didn't have the format argument, so copy our
780 * implementation's behavior at the time.
781 */
782 intelSetTexBuffer2(pDRICtx, target, __DRI_TEXTURE_FORMAT_RGBA, dPriv);
783 }
784
785 #if FEATURE_OES_EGL_image
786 static void
787 intel_image_target_texture_2d(GLcontext *ctx, GLenum target,
788 struct gl_texture_object *texObj,
789 struct gl_texture_image *texImage,
790 GLeglImageOES image_handle)
791 {
792 struct intel_context *intel = intel_context(ctx);
793 struct intel_texture_object *intelObj = intel_texture_object(texObj);
794 struct intel_texture_image *intelImage = intel_texture_image(texImage);
795 struct intel_mipmap_tree *mt;
796 __DRIscreen *screen;
797 __DRIimage *image;
798
799 screen = intel->intelScreen->driScrnPriv;
800 image = screen->dri2.image->lookupEGLImage(screen, image_handle,
801 screen->loaderPrivate);
802 if (image == NULL)
803 return;
804
805 mt = intel_miptree_create_for_region(intel, target,
806 image->internal_format,
807 0, 0, image->region, 1, 0);
808 if (mt == NULL)
809 return;
810
811 if (intelImage->mt) {
812 intel_miptree_release(intel, &intelImage->mt);
813 assert(!texImage->Data);
814 }
815 if (intelObj->mt)
816 intel_miptree_release(intel, &intelObj->mt);
817
818 intelObj->mt = mt;
819 _mesa_init_teximage_fields(&intel->ctx, target, texImage,
820 image->region->width, image->region->height, 1,
821 0, image->internal_format);
822
823 intelImage->face = target_to_face(target);
824 intelImage->level = 0;
825 texImage->TexFormat = image->format;
826 texImage->RowStride = image->region->pitch;
827 intel_miptree_reference(&intelImage->mt, intelObj->mt);
828
829 if (!intel_miptree_match_image(intelObj->mt, &intelImage->base))
830 fprintf(stderr, "miptree doesn't match image\n");
831 }
832 #endif
833
834 void
835 intelInitTextureImageFuncs(struct dd_function_table *functions)
836 {
837 functions->TexImage1D = intelTexImage1D;
838 functions->TexImage2D = intelTexImage2D;
839 functions->TexImage3D = intelTexImage3D;
840 functions->GetTexImage = intelGetTexImage;
841
842 functions->CompressedTexImage2D = intelCompressedTexImage2D;
843 functions->GetCompressedTexImage = intelGetCompressedTexImage;
844
845 #if FEATURE_OES_EGL_image
846 functions->EGLImageTargetTexture2D = intel_image_target_texture_2d;
847 #endif
848 }