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