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