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