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