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/texcompress.h"
11 #include "main/texstore.h"
12 #include "main/texgetimage.h"
13 #include "main/texobj.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 struct intel_mipmap_tree *
60 intel_miptree_create_for_teximage(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 i, comp_byte = 0;
71 GLuint texelBytes;
72
73 DBG("%s\n", __FUNCTION__);
74
75 if (intelImage->base.Border)
76 return NULL;
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 /* For this combination, we're at some lower mipmap level and
85 * some important dimension is 1. We can't extrapolate up to a
86 * likely base level width/height/depth for a full mipmap stack
87 * from this info, so just allocate this one level.
88 */
89 firstLevel = intelImage->level;
90 lastLevel = intelImage->level;
91 } else {
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 /* Figure out image dimensions at start level. */
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 } else {
120 lastLevel = firstLevel + logbase2(MAX2(MAX2(width, height), depth));
121 }
122 }
123
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 return 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
143
144
145
146 static GLuint
147 target_to_face(GLenum target)
148 {
149 switch (target) {
150 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
151 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
152 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
153 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
154 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
155 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
156 return ((GLuint) target - (GLuint) GL_TEXTURE_CUBE_MAP_POSITIVE_X);
157 default:
158 return 0;
159 }
160 }
161
162 /* There are actually quite a few combinations this will work for,
163 * more than what I've listed here.
164 */
165 static GLboolean
166 check_pbo_format(GLint internalFormat,
167 GLenum format, GLenum type,
168 gl_format mesa_format)
169 {
170 switch (internalFormat) {
171 case 4:
172 case GL_RGBA:
173 case GL_RGBA8:
174 return (format == GL_BGRA &&
175 (type == GL_UNSIGNED_BYTE ||
176 type == GL_UNSIGNED_INT_8_8_8_8_REV) &&
177 mesa_format == MESA_FORMAT_ARGB8888);
178 case 3:
179 case GL_RGB:
180 return (format == GL_RGB &&
181 type == GL_UNSIGNED_SHORT_5_6_5 &&
182 mesa_format == MESA_FORMAT_RGB565);
183 case 1:
184 case GL_LUMINANCE:
185 return (format == GL_LUMINANCE &&
186 type == GL_UNSIGNED_BYTE &&
187 mesa_format == MESA_FORMAT_L8);
188 case GL_YCBCR_MESA:
189 return (type == GL_UNSIGNED_SHORT_8_8_MESA || type == GL_UNSIGNED_BYTE);
190 default:
191 return GL_FALSE;
192 }
193 }
194
195
196 /* XXX: Do this for TexSubImage also:
197 */
198 static GLboolean
199 try_pbo_upload(struct intel_context *intel,
200 struct intel_texture_image *intelImage,
201 const struct gl_pixelstore_attrib *unpack,
202 GLint internalFormat,
203 GLint width, GLint height,
204 GLenum format, GLenum type, const void *pixels)
205 {
206 struct intel_buffer_object *pbo = intel_buffer_object(unpack->BufferObj);
207 GLuint src_offset, src_stride;
208 GLuint dst_x, dst_y, dst_stride;
209 drm_intel_bo *dst_buffer = intel_region_buffer(intel,
210 intelImage->mt->region,
211 INTEL_WRITE_FULL);
212
213 if (!_mesa_is_bufferobj(unpack->BufferObj) ||
214 intel->ctx._ImageTransferState ||
215 unpack->SkipPixels || unpack->SkipRows) {
216 DBG("%s: failure 1\n", __FUNCTION__);
217 return GL_FALSE;
218 }
219
220 /* note: potential 64-bit ptr to 32-bit int cast */
221 src_offset = (GLuint) (unsigned long) pixels;
222
223 if (unpack->RowLength > 0)
224 src_stride = unpack->RowLength;
225 else
226 src_stride = width;
227
228 intel_miptree_get_image_offset(intelImage->mt, intelImage->level,
229 intelImage->face, 0,
230 &dst_x, &dst_y);
231
232 dst_stride = intelImage->mt->region->pitch;
233
234 if (drm_intel_bo_references(intel->batch.bo, dst_buffer))
235 intel_flush(&intel->ctx);
236
237 {
238 GLuint offset;
239 drm_intel_bo *src_buffer = intel_bufferobj_source(intel, pbo, &offset);
240
241 if (!intelEmitCopyBlit(intel,
242 intelImage->mt->cpp,
243 src_stride, src_buffer,
244 src_offset + 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 assert(!intelImage->mt);
351
352 if (intelObj->mt &&
353 intel_miptree_match_image(intelObj->mt, &intelImage->base)) {
354 /* Use an existing miptree when possible */
355 intel_miptree_reference(&intelImage->mt, intelObj->mt);
356 assert(intelImage->mt);
357 } else if (intelImage->base.Border == 0) {
358 /* Didn't fit in the object miptree, but it's suitable for inclusion in
359 * a miptree, so create one just for our level and store it in the image.
360 * It'll get moved into the object miptree at validate time.
361 */
362 intelImage->mt = intel_miptree_create_for_teximage(intel, intelObj,
363 intelImage,
364 pixels == NULL);
365
366 /* Even if the object currently has a mipmap tree associated
367 * with it, this one is a more likely candidate to represent the
368 * whole object since our level didn't fit what was there
369 * before, and any lower levels would fit into our miptree.
370 */
371 if (intelImage->mt)
372 intel_miptree_reference(&intelObj->mt, intelImage->mt);
373 }
374
375 /* PBO fastpaths:
376 */
377 if (dims <= 2 &&
378 intelImage->mt &&
379 _mesa_is_bufferobj(unpack->BufferObj) &&
380 check_pbo_format(internalFormat, format,
381 type, intelImage->base.TexFormat)) {
382
383 DBG("trying pbo upload\n");
384
385 /* Attempt to texture directly from PBO data (zero copy upload).
386 *
387 * Currently disable as it can lead to worse as well as better
388 * performance (in particular when intel_region_cow() is
389 * required).
390 */
391 if (intelObj->mt == intelImage->mt &&
392 intelObj->mt->first_level == level &&
393 intelObj->mt->last_level == level) {
394
395 if (try_pbo_zcopy(intel, intelImage, unpack,
396 internalFormat,
397 width, height, format, type, pixels)) {
398
399 DBG("pbo zcopy upload succeeded\n");
400 return;
401 }
402 }
403
404
405 /* Otherwise, attempt to use the blitter for PBO image uploads.
406 */
407 if (try_pbo_upload(intel, intelImage, unpack,
408 internalFormat,
409 width, height, format, type, pixels)) {
410 DBG("pbo upload succeeded\n");
411 return;
412 }
413
414 DBG("pbo upload failed\n");
415 }
416
417 /* intelCopyTexImage calls this function with pixels == NULL, with
418 * the expectation that the mipmap tree will be set up but nothing
419 * more will be done. This is where those calls return:
420 */
421 if (compressed) {
422 pixels = _mesa_validate_pbo_compressed_teximage(ctx, imageSize, pixels,
423 unpack,
424 "glCompressedTexImage");
425 } else {
426 pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, 1,
427 format, type,
428 pixels, unpack, "glTexImage");
429 }
430
431 if (intelImage->mt) {
432 if (pixels != NULL) {
433 /* Flush any queued rendering with the texture before mapping. */
434 if (drm_intel_bo_references(intel->batch.bo,
435 intelImage->mt->region->buffer)) {
436 intel_flush(ctx);
437 }
438 texImage->Data = intel_miptree_image_map(intel,
439 intelImage->mt,
440 intelImage->face,
441 intelImage->level,
442 &dstRowStride,
443 intelImage->base.ImageOffsets);
444 }
445
446 texImage->RowStride = dstRowStride / intelImage->mt->cpp;
447 }
448 else {
449 /* Allocate regular memory and store the image there temporarily. */
450 if (_mesa_is_format_compressed(texImage->TexFormat)) {
451 sizeInBytes = _mesa_format_image_size(texImage->TexFormat,
452 texImage->Width,
453 texImage->Height,
454 texImage->Depth);
455 dstRowStride =
456 _mesa_format_row_stride(texImage->TexFormat, width);
457 assert(dims != 3);
458 }
459 else {
460 dstRowStride = width * texelBytes;
461 sizeInBytes = depth * dstRowStride * height;
462 }
463
464 texImage->Data = _mesa_alloc_texmemory(sizeInBytes);
465 }
466
467 DBG("Upload image %dx%dx%d row_len %d "
468 "pitch %d pixels %d compressed %d\n",
469 width, height, depth, width * texelBytes, dstRowStride,
470 pixels ? 1 : 0, compressed);
471
472 /* Copy data. Would like to know when it's ok for us to eg. use
473 * the blitter to copy. Or, use the hardware to do the format
474 * conversion and copy:
475 */
476 if (pixels) {
477 if (compressed) {
478 if (intelImage->mt) {
479 struct intel_region *dst = intelImage->mt->region;
480 _mesa_copy_rect(texImage->Data, dst->cpp, dst->pitch,
481 0, 0,
482 intelImage->mt->level[level].width,
483 (intelImage->mt->level[level].height+3)/4,
484 pixels,
485 srcRowStride,
486 0, 0);
487 }
488 else {
489 memcpy(texImage->Data, pixels, imageSize);
490 }
491 }
492 else if (!_mesa_texstore(ctx, dims,
493 texImage->_BaseFormat,
494 texImage->TexFormat,
495 texImage->Data, 0, 0, 0, /* dstX/Y/Zoffset */
496 dstRowStride,
497 texImage->ImageOffsets,
498 width, height, depth,
499 format, type, pixels, unpack)) {
500 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
501 }
502 }
503
504 _mesa_unmap_teximage_pbo(ctx, unpack);
505
506 if (intelImage->mt) {
507 if (pixels != NULL)
508 intel_miptree_image_unmap(intel, intelImage->mt);
509 texImage->Data = NULL;
510 }
511 }
512
513
514 static void
515 intelTexImage3D(struct gl_context * ctx,
516 GLenum target, GLint level,
517 GLint internalFormat,
518 GLint width, GLint height, GLint depth,
519 GLint border,
520 GLenum format, GLenum type, const void *pixels,
521 const struct gl_pixelstore_attrib *unpack,
522 struct gl_texture_object *texObj,
523 struct gl_texture_image *texImage)
524 {
525 intelTexImage(ctx, 3, target, level,
526 internalFormat, width, height, depth, border,
527 format, type, pixels, unpack, texObj, texImage, 0, GL_FALSE);
528 }
529
530
531 static void
532 intelTexImage2D(struct gl_context * ctx,
533 GLenum target, GLint level,
534 GLint internalFormat,
535 GLint width, GLint height, GLint border,
536 GLenum format, GLenum type, const void *pixels,
537 const struct gl_pixelstore_attrib *unpack,
538 struct gl_texture_object *texObj,
539 struct gl_texture_image *texImage)
540 {
541 intelTexImage(ctx, 2, target, level,
542 internalFormat, width, height, 1, border,
543 format, type, pixels, unpack, texObj, texImage, 0, GL_FALSE);
544 }
545
546
547 static void
548 intelTexImage1D(struct gl_context * ctx,
549 GLenum target, GLint level,
550 GLint internalFormat,
551 GLint width, GLint border,
552 GLenum format, GLenum type, const void *pixels,
553 const struct gl_pixelstore_attrib *unpack,
554 struct gl_texture_object *texObj,
555 struct gl_texture_image *texImage)
556 {
557 intelTexImage(ctx, 1, target, level,
558 internalFormat, width, 1, 1, border,
559 format, type, pixels, unpack, texObj, texImage, 0, GL_FALSE);
560 }
561
562
563 static void
564 intelCompressedTexImage2D( struct gl_context *ctx, GLenum target, GLint level,
565 GLint internalFormat,
566 GLint width, GLint height, GLint border,
567 GLsizei imageSize, const GLvoid *data,
568 struct gl_texture_object *texObj,
569 struct gl_texture_image *texImage )
570 {
571 intelTexImage(ctx, 2, target, level,
572 internalFormat, width, height, 1, border,
573 0, 0, data, &ctx->Unpack, texObj, texImage, imageSize, GL_TRUE);
574 }
575
576
577 /**
578 * Need to map texture image into memory before copying image data,
579 * then unmap it.
580 */
581 static void
582 intel_get_tex_image(struct gl_context * ctx, GLenum target, GLint level,
583 GLenum format, GLenum type, GLvoid * pixels,
584 struct gl_texture_object *texObj,
585 struct gl_texture_image *texImage, GLboolean compressed)
586 {
587 struct intel_context *intel = intel_context(ctx);
588 struct intel_texture_image *intelImage = intel_texture_image(texImage);
589
590 /* If we're reading from a texture that has been rendered to, need to
591 * make sure rendering is complete.
592 * We could probably predicate this on texObj->_RenderToTexture
593 */
594 intel_flush(ctx);
595
596 /* Map */
597 if (intelImage->mt) {
598 /* Image is stored in hardware format in a buffer managed by the
599 * kernel. Need to explicitly map and unmap it.
600 */
601 intelImage->base.Data =
602 intel_miptree_image_map(intel,
603 intelImage->mt,
604 intelImage->face,
605 intelImage->level,
606 &intelImage->base.RowStride,
607 intelImage->base.ImageOffsets);
608 intelImage->base.RowStride /= intelImage->mt->cpp;
609 }
610 else {
611 /* Otherwise, the image should actually be stored in
612 * intelImage->base.Data. This is pretty confusing for
613 * everybody, I'd much prefer to separate the two functions of
614 * texImage->Data - storage for texture images in main memory
615 * and access (ie mappings) of images. In other words, we'd
616 * create a new texImage->Map field and leave Data simply for
617 * storage.
618 */
619 assert(intelImage->base.Data);
620 }
621
622
623 if (compressed) {
624 _mesa_get_compressed_teximage(ctx, target, level, pixels,
625 texObj, texImage);
626 }
627 else {
628 _mesa_get_teximage(ctx, target, level, format, type, pixels,
629 texObj, texImage);
630 }
631
632
633 /* Unmap */
634 if (intelImage->mt) {
635 intel_miptree_image_unmap(intel, intelImage->mt);
636 intelImage->base.Data = NULL;
637 }
638 }
639
640
641 static void
642 intelGetTexImage(struct gl_context * ctx, GLenum target, GLint level,
643 GLenum format, GLenum type, GLvoid * pixels,
644 struct gl_texture_object *texObj,
645 struct gl_texture_image *texImage)
646 {
647 intel_get_tex_image(ctx, target, level, format, type, pixels,
648 texObj, texImage, GL_FALSE);
649 }
650
651
652 static void
653 intelGetCompressedTexImage(struct gl_context *ctx, GLenum target, GLint level,
654 GLvoid *pixels,
655 struct gl_texture_object *texObj,
656 struct gl_texture_image *texImage)
657 {
658 intel_get_tex_image(ctx, target, level, 0, 0, pixels,
659 texObj, texImage, GL_TRUE);
660 }
661
662 void
663 intelSetTexBuffer2(__DRIcontext *pDRICtx, GLint target,
664 GLint texture_format,
665 __DRIdrawable *dPriv)
666 {
667 struct gl_framebuffer *fb = dPriv->driverPrivate;
668 struct intel_context *intel = pDRICtx->driverPrivate;
669 struct gl_context *ctx = &intel->ctx;
670 struct intel_texture_object *intelObj;
671 struct intel_texture_image *intelImage;
672 struct intel_mipmap_tree *mt;
673 struct intel_renderbuffer *rb;
674 struct gl_texture_object *texObj;
675 struct gl_texture_image *texImage;
676 int level = 0, internalFormat;
677 gl_format texFormat;
678
679 texObj = _mesa_get_current_tex_object(ctx, target);
680 intelObj = intel_texture_object(texObj);
681
682 if (!intelObj)
683 return;
684
685 if (dPriv->lastStamp != dPriv->dri2.stamp ||
686 !pDRICtx->driScreenPriv->dri2.useInvalidate)
687 intel_update_renderbuffers(pDRICtx, dPriv);
688
689 rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
690 /* If the region isn't set, then intel_update_renderbuffers was unable
691 * to get the buffers for the drawable.
692 */
693 if (rb->region == NULL)
694 return;
695
696 if (texture_format == __DRI_TEXTURE_FORMAT_RGB) {
697 internalFormat = GL_RGB;
698 texFormat = MESA_FORMAT_XRGB8888;
699 }
700 else {
701 internalFormat = GL_RGBA;
702 texFormat = MESA_FORMAT_ARGB8888;
703 }
704
705 mt = intel_miptree_create_for_region(intel, target,
706 internalFormat, rb->region, 1, 0);
707 if (mt == NULL)
708 return;
709
710 _mesa_lock_texture(&intel->ctx, texObj);
711
712 texImage = _mesa_get_tex_image(&intel->ctx, texObj, target, level);
713 intelImage = intel_texture_image(texImage);
714
715 if (intelImage->mt) {
716 intel_miptree_release(intel, &intelImage->mt);
717 assert(!texImage->Data);
718 }
719 if (intelObj->mt)
720 intel_miptree_release(intel, &intelObj->mt);
721
722 intelObj->mt = mt;
723
724 _mesa_init_teximage_fields(&intel->ctx, target, texImage,
725 rb->region->width, rb->region->height, 1,
726 0, internalFormat, texFormat);
727
728 intelImage->face = target_to_face(target);
729 intelImage->level = level;
730 texImage->RowStride = rb->region->pitch;
731 intel_miptree_reference(&intelImage->mt, intelObj->mt);
732
733 if (!intel_miptree_match_image(intelObj->mt, &intelImage->base)) {
734 fprintf(stderr, "miptree doesn't match image\n");
735 }
736
737 _mesa_unlock_texture(&intel->ctx, texObj);
738 }
739
740 void
741 intelSetTexBuffer(__DRIcontext *pDRICtx, GLint target, __DRIdrawable *dPriv)
742 {
743 /* The old interface didn't have the format argument, so copy our
744 * implementation's behavior at the time.
745 */
746 intelSetTexBuffer2(pDRICtx, target, __DRI_TEXTURE_FORMAT_RGBA, dPriv);
747 }
748
749 #if FEATURE_OES_EGL_image
750 static void
751 intel_image_target_texture_2d(struct gl_context *ctx, GLenum target,
752 struct gl_texture_object *texObj,
753 struct gl_texture_image *texImage,
754 GLeglImageOES image_handle)
755 {
756 struct intel_context *intel = intel_context(ctx);
757 struct intel_texture_object *intelObj = intel_texture_object(texObj);
758 struct intel_texture_image *intelImage = intel_texture_image(texImage);
759 struct intel_mipmap_tree *mt;
760 __DRIscreen *screen;
761 __DRIimage *image;
762
763 screen = intel->intelScreen->driScrnPriv;
764 image = screen->dri2.image->lookupEGLImage(screen, image_handle,
765 screen->loaderPrivate);
766 if (image == NULL)
767 return;
768
769 mt = intel_miptree_create_for_region(intel, target,
770 image->internal_format,
771 image->region, 1, 0);
772 if (mt == NULL)
773 return;
774
775 if (intelImage->mt) {
776 intel_miptree_release(intel, &intelImage->mt);
777 assert(!texImage->Data);
778 }
779 if (intelObj->mt)
780 intel_miptree_release(intel, &intelObj->mt);
781
782 intelObj->mt = mt;
783 _mesa_init_teximage_fields(&intel->ctx, target, texImage,
784 image->region->width, image->region->height, 1,
785 0, image->internal_format, image->format);
786
787 intelImage->face = target_to_face(target);
788 intelImage->level = 0;
789 texImage->RowStride = image->region->pitch;
790 intel_miptree_reference(&intelImage->mt, intelObj->mt);
791
792 if (!intel_miptree_match_image(intelObj->mt, &intelImage->base))
793 fprintf(stderr, "miptree doesn't match image\n");
794 }
795 #endif
796
797 void
798 intelInitTextureImageFuncs(struct dd_function_table *functions)
799 {
800 functions->TexImage1D = intelTexImage1D;
801 functions->TexImage2D = intelTexImage2D;
802 functions->TexImage3D = intelTexImage3D;
803 functions->GetTexImage = intelGetTexImage;
804
805 functions->CompressedTexImage2D = intelCompressedTexImage2D;
806 functions->GetCompressedTexImage = intelGetCompressedTexImage;
807
808 #if FEATURE_OES_EGL_image
809 functions->EGLImageTargetTexture2D = intel_image_target_texture_2d;
810 #endif
811 }