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