intel: Set the texture format in the TFP path.
[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 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 return (format == GL_BGRA &&
181 (type == GL_UNSIGNED_BYTE ||
182 type == GL_UNSIGNED_INT_8_8_8_8_REV) &&
183 mesa_format == MESA_FORMAT_ARGB8888);
184 case 3:
185 case GL_RGB:
186 return (format == GL_RGB &&
187 type == GL_UNSIGNED_SHORT_5_6_5 &&
188 mesa_format == MESA_FORMAT_RGB565);
189 case GL_YCBCR_MESA:
190 return (type == GL_UNSIGNED_SHORT_8_8_MESA || type == GL_UNSIGNED_BYTE);
191 default:
192 return GL_FALSE;
193 }
194 }
195
196
197 /* XXX: Do this for TexSubImage also:
198 */
199 static GLboolean
200 try_pbo_upload(struct intel_context *intel,
201 struct intel_texture_image *intelImage,
202 const struct gl_pixelstore_attrib *unpack,
203 GLint internalFormat,
204 GLint width, GLint height,
205 GLenum format, GLenum type, const void *pixels)
206 {
207 struct intel_buffer_object *pbo = intel_buffer_object(unpack->BufferObj);
208 GLuint src_offset, src_stride;
209 GLuint dst_x, dst_y, dst_stride;
210 dri_bo *dst_buffer = intel_region_buffer(intel,
211 intelImage->mt->region,
212 INTEL_WRITE_FULL);
213
214 if (!_mesa_is_bufferobj(unpack->BufferObj) ||
215 intel->ctx._ImageTransferState ||
216 unpack->SkipPixels || unpack->SkipRows) {
217 DBG("%s: failure 1\n", __FUNCTION__);
218 return GL_FALSE;
219 }
220
221 /* note: potential 64-bit ptr to 32-bit int cast */
222 src_offset = (GLuint) (unsigned long) pixels;
223
224 if (unpack->RowLength > 0)
225 src_stride = unpack->RowLength;
226 else
227 src_stride = width;
228
229 intel_miptree_get_image_offset(intelImage->mt, intelImage->level,
230 intelImage->face, 0,
231 &dst_x, &dst_y);
232
233 dst_stride = intelImage->mt->pitch;
234
235 if (drm_intel_bo_references(intel->batch->buf, dst_buffer))
236 intelFlush(&intel->ctx);
237 LOCK_HARDWARE(intel);
238 {
239 dri_bo *src_buffer = intel_bufferobj_buffer(intel, pbo, INTEL_READ);
240
241 if (!intelEmitCopyBlit(intel,
242 intelImage->mt->cpp,
243 src_stride, src_buffer, src_offset, GL_FALSE,
244 dst_stride, dst_buffer, 0, GL_FALSE,
245 0, 0, dst_x, dst_y, width, height,
246 GL_COPY)) {
247 UNLOCK_HARDWARE(intel);
248 return GL_FALSE;
249 }
250 }
251 UNLOCK_HARDWARE(intel);
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->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(GLcontext * 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 postConvWidth = width;
319 GLint postConvHeight = height;
320 GLint texelBytes, sizeInBytes;
321 GLuint dstRowStride = 0, srcRowStride = texImage->RowStride;
322
323 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__,
324 _mesa_lookup_enum_by_nr(target), level, width, height, depth, border);
325
326 intelImage->face = target_to_face(target);
327 intelImage->level = level;
328
329 if (ctx->_ImageTransferState & IMAGE_CONVOLUTION_BIT) {
330 _mesa_adjust_image_for_convolution(ctx, dims, &postConvWidth,
331 &postConvHeight);
332 }
333
334 if (_mesa_is_format_compressed(texImage->TexFormat)) {
335 texelBytes = 0;
336 }
337 else {
338 texelBytes = _mesa_get_format_bytes(texImage->TexFormat);
339
340 /* Minimum pitch of 32 bytes */
341 if (postConvWidth * texelBytes < 32) {
342 postConvWidth = 32 / texelBytes;
343 texImage->RowStride = postConvWidth;
344 }
345
346 if (!intelImage->mt) {
347 assert(texImage->RowStride == postConvWidth);
348 }
349 }
350
351 /* Release the reference to a potentially orphaned buffer.
352 * Release any old malloced memory.
353 */
354 if (intelImage->mt) {
355 intel_miptree_release(intel, &intelImage->mt);
356 assert(!texImage->Data);
357 }
358 else if (texImage->Data) {
359 _mesa_free_texmemory(texImage->Data);
360 texImage->Data = NULL;
361 }
362
363 /* If this is the only texture image in the tree, could call
364 * bmBufferData with NULL data to free the old block and avoid
365 * waiting on any outstanding fences.
366 */
367 if (intelObj->mt &&
368 intelObj->mt->first_level == level &&
369 intelObj->mt->last_level == level &&
370 intelObj->mt->target != GL_TEXTURE_CUBE_MAP_ARB &&
371 !intel_miptree_match_image(intelObj->mt, &intelImage->base,
372 intelImage->face, intelImage->level)) {
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 intelImage->face, intelImage->level)) {
391
392 intel_miptree_reference(&intelImage->mt, intelObj->mt);
393 assert(intelImage->mt);
394 } else if (intelImage->base.Border == 0) {
395 int comp_byte = 0;
396 GLuint texelBytes = _mesa_get_format_bytes(intelImage->base.TexFormat);
397 GLenum baseFormat = _mesa_get_format_base_format(intelImage->base.TexFormat);
398 if (_mesa_is_format_compressed(intelImage->base.TexFormat)) {
399 comp_byte =
400 intel_compressed_num_bytes(intelImage->base.TexFormat);
401 }
402
403 /* Didn't fit in the object miptree, but it's suitable for inclusion in
404 * a miptree, so create one just for our level and store it in the image.
405 * It'll get moved into the object miptree at validate time.
406 */
407 intelImage->mt = intel_miptree_create(intel, target,
408 baseFormat,
409 internalFormat,
410 level, level,
411 width, height, depth,
412 texelBytes,
413 comp_byte, pixels == NULL);
414
415 }
416
417 /* PBO fastpaths:
418 */
419 if (dims <= 2 &&
420 intelImage->mt &&
421 _mesa_is_bufferobj(unpack->BufferObj) &&
422 check_pbo_format(internalFormat, format,
423 type, intelImage->base.TexFormat)) {
424
425 DBG("trying pbo upload\n");
426
427 /* Attempt to texture directly from PBO data (zero copy upload).
428 *
429 * Currently disable as it can lead to worse as well as better
430 * performance (in particular when intel_region_cow() is
431 * required).
432 */
433 if (intelObj->mt == intelImage->mt &&
434 intelObj->mt->first_level == level &&
435 intelObj->mt->last_level == level) {
436
437 if (try_pbo_zcopy(intel, intelImage, unpack,
438 internalFormat,
439 width, height, format, type, pixels)) {
440
441 DBG("pbo zcopy upload succeeded\n");
442 return;
443 }
444 }
445
446
447 /* Otherwise, attempt to use the blitter for PBO image uploads.
448 */
449 if (try_pbo_upload(intel, intelImage, unpack,
450 internalFormat,
451 width, height, format, type, pixels)) {
452 DBG("pbo upload succeeded\n");
453 return;
454 }
455
456 DBG("pbo upload failed\n");
457 }
458
459 /* intelCopyTexImage calls this function with pixels == NULL, with
460 * the expectation that the mipmap tree will be set up but nothing
461 * more will be done. This is where those calls return:
462 */
463 if (compressed) {
464 pixels = _mesa_validate_pbo_compressed_teximage(ctx, imageSize, pixels,
465 unpack,
466 "glCompressedTexImage");
467 } else {
468 pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, 1,
469 format, type,
470 pixels, unpack, "glTexImage");
471 }
472
473 LOCK_HARDWARE(intel);
474
475 if (intelImage->mt) {
476 if (pixels != NULL) {
477 /* Flush any queued rendering with the texture before mapping. */
478 if (drm_intel_bo_references(intel->batch->buf,
479 intelImage->mt->region->buffer)) {
480 intelFlush(ctx);
481 }
482 texImage->Data = intel_miptree_image_map(intel,
483 intelImage->mt,
484 intelImage->face,
485 intelImage->level,
486 &dstRowStride,
487 intelImage->base.ImageOffsets);
488 }
489
490 texImage->RowStride = dstRowStride / intelImage->mt->cpp;
491 }
492 else {
493 /* Allocate regular memory and store the image there temporarily. */
494 if (_mesa_is_format_compressed(texImage->TexFormat)) {
495 sizeInBytes = _mesa_format_image_size(texImage->TexFormat,
496 texImage->Width,
497 texImage->Height,
498 texImage->Depth);
499 dstRowStride =
500 _mesa_format_row_stride(texImage->TexFormat, width);
501 assert(dims != 3);
502 }
503 else {
504 dstRowStride = postConvWidth * texelBytes;
505 sizeInBytes = depth * dstRowStride * postConvHeight;
506 }
507
508 texImage->Data = _mesa_alloc_texmemory(sizeInBytes);
509 }
510
511 DBG("Upload image %dx%dx%d row_len %d "
512 "pitch %d pixels %d compressed %d\n",
513 width, height, depth, width * texelBytes, dstRowStride,
514 pixels ? 1 : 0, compressed);
515
516 /* Copy data. Would like to know when it's ok for us to eg. use
517 * the blitter to copy. Or, use the hardware to do the format
518 * conversion and copy:
519 */
520 if (pixels) {
521 if (compressed) {
522 if (intelImage->mt) {
523 struct intel_region *dst = intelImage->mt->region;
524 _mesa_copy_rect(texImage->Data, dst->cpp, dst->pitch,
525 0, 0,
526 intelImage->mt->level[level].width,
527 (intelImage->mt->level[level].height+3)/4,
528 pixels,
529 srcRowStride,
530 0, 0);
531 }
532 else {
533 memcpy(texImage->Data, pixels, imageSize);
534 }
535 }
536 else if (!_mesa_texstore(ctx, dims,
537 texImage->_BaseFormat,
538 texImage->TexFormat,
539 texImage->Data, 0, 0, 0, /* dstX/Y/Zoffset */
540 dstRowStride,
541 texImage->ImageOffsets,
542 width, height, depth,
543 format, type, pixels, unpack)) {
544 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
545 }
546 }
547
548 _mesa_unmap_teximage_pbo(ctx, unpack);
549
550 if (intelImage->mt) {
551 if (pixels != NULL)
552 intel_miptree_image_unmap(intel, intelImage->mt);
553 texImage->Data = NULL;
554 }
555
556 UNLOCK_HARDWARE(intel);
557 }
558
559
560 static void
561 intelTexImage3D(GLcontext * ctx,
562 GLenum target, GLint level,
563 GLint internalFormat,
564 GLint width, GLint height, GLint depth,
565 GLint border,
566 GLenum format, GLenum type, const void *pixels,
567 const struct gl_pixelstore_attrib *unpack,
568 struct gl_texture_object *texObj,
569 struct gl_texture_image *texImage)
570 {
571 intelTexImage(ctx, 3, target, level,
572 internalFormat, width, height, depth, border,
573 format, type, pixels, unpack, texObj, texImage, 0, GL_FALSE);
574 }
575
576
577 static void
578 intelTexImage2D(GLcontext * ctx,
579 GLenum target, GLint level,
580 GLint internalFormat,
581 GLint width, GLint height, GLint border,
582 GLenum format, GLenum type, const void *pixels,
583 const struct gl_pixelstore_attrib *unpack,
584 struct gl_texture_object *texObj,
585 struct gl_texture_image *texImage)
586 {
587 intelTexImage(ctx, 2, target, level,
588 internalFormat, width, height, 1, border,
589 format, type, pixels, unpack, texObj, texImage, 0, GL_FALSE);
590 }
591
592
593 static void
594 intelTexImage1D(GLcontext * ctx,
595 GLenum target, GLint level,
596 GLint internalFormat,
597 GLint width, GLint border,
598 GLenum format, GLenum type, const void *pixels,
599 const struct gl_pixelstore_attrib *unpack,
600 struct gl_texture_object *texObj,
601 struct gl_texture_image *texImage)
602 {
603 intelTexImage(ctx, 1, target, level,
604 internalFormat, width, 1, 1, border,
605 format, type, pixels, unpack, texObj, texImage, 0, GL_FALSE);
606 }
607
608
609 static void
610 intelCompressedTexImage2D( GLcontext *ctx, GLenum target, GLint level,
611 GLint internalFormat,
612 GLint width, GLint height, GLint border,
613 GLsizei imageSize, const GLvoid *data,
614 struct gl_texture_object *texObj,
615 struct gl_texture_image *texImage )
616 {
617 intelTexImage(ctx, 2, target, level,
618 internalFormat, width, height, 1, border,
619 0, 0, data, &ctx->Unpack, texObj, texImage, imageSize, GL_TRUE);
620 }
621
622
623 /**
624 * Need to map texture image into memory before copying image data,
625 * then unmap it.
626 */
627 static void
628 intel_get_tex_image(GLcontext * ctx, GLenum target, GLint level,
629 GLenum format, GLenum type, GLvoid * pixels,
630 struct gl_texture_object *texObj,
631 struct gl_texture_image *texImage, GLboolean compressed)
632 {
633 struct intel_context *intel = intel_context(ctx);
634 struct intel_texture_image *intelImage = intel_texture_image(texImage);
635
636 /* If we're reading from a texture that has been rendered to, need to
637 * make sure rendering is complete.
638 * We could probably predicate this on texObj->_RenderToTexture
639 */
640 intelFlush(ctx);
641
642 /* Map */
643 if (intelImage->mt) {
644 /* Image is stored in hardware format in a buffer managed by the
645 * kernel. Need to explicitly map and unmap it.
646 */
647 intelImage->base.Data =
648 intel_miptree_image_map(intel,
649 intelImage->mt,
650 intelImage->face,
651 intelImage->level,
652 &intelImage->base.RowStride,
653 intelImage->base.ImageOffsets);
654 intelImage->base.RowStride /= intelImage->mt->cpp;
655 }
656 else {
657 /* Otherwise, the image should actually be stored in
658 * intelImage->base.Data. This is pretty confusing for
659 * everybody, I'd much prefer to separate the two functions of
660 * texImage->Data - storage for texture images in main memory
661 * and access (ie mappings) of images. In other words, we'd
662 * create a new texImage->Map field and leave Data simply for
663 * storage.
664 */
665 assert(intelImage->base.Data);
666 }
667
668
669 if (compressed) {
670 _mesa_get_compressed_teximage(ctx, target, level, pixels,
671 texObj, texImage);
672 }
673 else {
674 _mesa_get_teximage(ctx, target, level, format, type, pixels,
675 texObj, texImage);
676 }
677
678
679 /* Unmap */
680 if (intelImage->mt) {
681 intel_miptree_image_unmap(intel, intelImage->mt);
682 intelImage->base.Data = NULL;
683 }
684 }
685
686
687 static void
688 intelGetTexImage(GLcontext * ctx, GLenum target, GLint level,
689 GLenum format, GLenum type, GLvoid * pixels,
690 struct gl_texture_object *texObj,
691 struct gl_texture_image *texImage)
692 {
693 intel_get_tex_image(ctx, target, level, format, type, pixels,
694 texObj, texImage, GL_FALSE);
695 }
696
697
698 static void
699 intelGetCompressedTexImage(GLcontext *ctx, GLenum target, GLint level,
700 GLvoid *pixels,
701 struct gl_texture_object *texObj,
702 struct gl_texture_image *texImage)
703 {
704 intel_get_tex_image(ctx, target, level, 0, 0, pixels,
705 texObj, texImage, GL_TRUE);
706 }
707
708
709 void
710 intelSetTexOffset(__DRIcontext *pDRICtx, GLint texname,
711 unsigned long long offset, GLint depth, GLuint pitch)
712 {
713 struct intel_context *intel = pDRICtx->driverPrivate;
714 struct gl_texture_object *tObj = _mesa_lookup_texture(&intel->ctx, texname);
715 struct intel_texture_object *intelObj = intel_texture_object(tObj);
716
717 if (!intelObj)
718 return;
719
720 if (intelObj->mt)
721 intel_miptree_release(intel, &intelObj->mt);
722
723 intelObj->imageOverride = GL_TRUE;
724 intelObj->depthOverride = depth;
725 intelObj->pitchOverride = pitch;
726
727 if (offset)
728 intelObj->textureOffset = offset;
729 }
730
731 void
732 intelSetTexBuffer2(__DRIcontext *pDRICtx, GLint target,
733 GLint glx_texture_format,
734 __DRIdrawable *dPriv)
735 {
736 struct intel_framebuffer *intel_fb = dPriv->driverPrivate;
737 struct intel_context *intel = pDRICtx->driverPrivate;
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_unit *texUnit;
743 struct gl_texture_object *texObj;
744 struct gl_texture_image *texImage;
745 int level = 0, internalFormat;
746
747 texUnit = &intel->ctx.Texture.Unit[intel->ctx.Texture.CurrentUnit];
748 texObj = _mesa_select_tex_object(&intel->ctx, texUnit, target);
749 intelObj = intel_texture_object(texObj);
750
751 if (!intelObj)
752 return;
753
754 intel_update_renderbuffers(pDRICtx, dPriv);
755
756 rb = intel_fb->color_rb[0];
757 /* If the region isn't set, then intel_update_renderbuffers was unable
758 * to get the buffers for the drawable.
759 */
760 if (rb->region == NULL)
761 return;
762
763 if (glx_texture_format == GLX_TEXTURE_FORMAT_RGB_EXT)
764 internalFormat = GL_RGB;
765 else
766 internalFormat = GL_RGBA;
767
768 mt = intel_miptree_create_for_region(intel, target,
769 internalFormat,
770 0, 0, rb->region, 1, 0);
771 if (mt == NULL)
772 return;
773
774 _mesa_lock_texture(&intel->ctx, texObj);
775
776 texImage = _mesa_get_tex_image(&intel->ctx, texObj, target, level);
777 intelImage = intel_texture_image(texImage);
778
779 if (intelImage->mt) {
780 intel_miptree_release(intel, &intelImage->mt);
781 assert(!texImage->Data);
782 }
783 if (intelObj->mt)
784 intel_miptree_release(intel, &intelObj->mt);
785
786 intelObj->mt = mt;
787 _mesa_init_teximage_fields(&intel->ctx, target, texImage,
788 rb->region->width, rb->region->height, 1,
789 0, internalFormat);
790
791 intelImage->face = target_to_face(target);
792 intelImage->level = level;
793 if (glx_texture_format == GLX_TEXTURE_FORMAT_RGB_EXT)
794 texImage->TexFormat = MESA_FORMAT_XRGB8888;
795 else
796 texImage->TexFormat = MESA_FORMAT_ARGB8888;
797 texImage->RowStride = rb->region->pitch;
798 intel_miptree_reference(&intelImage->mt, intelObj->mt);
799
800 if (!intel_miptree_match_image(intelObj->mt, &intelImage->base,
801 intelImage->face, intelImage->level)) {
802 fprintf(stderr, "miptree doesn't match image\n");
803 }
804
805 _mesa_unlock_texture(&intel->ctx, texObj);
806 }
807
808 void
809 intelSetTexBuffer(__DRIcontext *pDRICtx, GLint target, __DRIdrawable *dPriv)
810 {
811 /* The old interface didn't have the format argument, so copy our
812 * implementation's behavior at the time.
813 */
814 intelSetTexBuffer2(pDRICtx, target, GLX_TEXTURE_FORMAT_RGBA_EXT, dPriv);
815 }
816
817
818 void
819 intelInitTextureImageFuncs(struct dd_function_table *functions)
820 {
821 functions->TexImage1D = intelTexImage1D;
822 functions->TexImage2D = intelTexImage2D;
823 functions->TexImage3D = intelTexImage3D;
824 functions->GetTexImage = intelGetTexImage;
825
826 functions->CompressedTexImage2D = intelCompressedTexImage2D;
827 functions->GetCompressedTexImage = intelGetCompressedTexImage;
828 }