i915g: Add some debug prints in texture code
[mesa.git] / src / gallium / drivers / i915 / i915_resource_texture.c
1 /**************************************************************************
2 *
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27 /*
28 * Authors:
29 * Keith Whitwell <keith@tungstengraphics.com>
30 * Michel Dänzer <michel@tungstengraphics.com>
31 */
32
33 #include "pipe/p_state.h"
34 #include "pipe/p_context.h"
35 #include "pipe/p_defines.h"
36 #include "util/u_inlines.h"
37 #include "util/u_format.h"
38 #include "util/u_math.h"
39 #include "util/u_memory.h"
40
41 #include "i915_context.h"
42 #include "i915_resource.h"
43 #include "i915_screen.h"
44 #include "i915_winsys.h"
45 #include "i915_debug.h"
46
47
48 #define DEBUG_TEXTURES 0
49
50 /*
51 * Helper function and arrays
52 */
53
54
55 /**
56 * Initial offset for Cube map.
57 */
58 static const int initial_offsets[6][2] = {
59 [PIPE_TEX_FACE_POS_X] = {0, 0},
60 [PIPE_TEX_FACE_POS_Y] = {1, 0},
61 [PIPE_TEX_FACE_POS_Z] = {1, 1},
62 [PIPE_TEX_FACE_NEG_X] = {0, 2},
63 [PIPE_TEX_FACE_NEG_Y] = {1, 2},
64 [PIPE_TEX_FACE_NEG_Z] = {1, 3},
65 };
66
67 /**
68 * Step offsets for Cube map.
69 */
70 static const int step_offsets[6][2] = {
71 [PIPE_TEX_FACE_POS_X] = { 0, 2},
72 [PIPE_TEX_FACE_POS_Y] = {-1, 2},
73 [PIPE_TEX_FACE_POS_Z] = {-1, 1},
74 [PIPE_TEX_FACE_NEG_X] = { 0, 2},
75 [PIPE_TEX_FACE_NEG_Y] = {-1, 2},
76 [PIPE_TEX_FACE_NEG_Z] = {-1, 1},
77 };
78
79 /**
80 * For compressed level 2
81 */
82 static const int bottom_offsets[6] = {
83 [PIPE_TEX_FACE_POS_X] = 16 + 0 * 8,
84 [PIPE_TEX_FACE_POS_Y] = 16 + 1 * 8,
85 [PIPE_TEX_FACE_POS_Z] = 16 + 2 * 8,
86 [PIPE_TEX_FACE_NEG_X] = 16 + 3 * 8,
87 [PIPE_TEX_FACE_NEG_Y] = 16 + 4 * 8,
88 [PIPE_TEX_FACE_NEG_Z] = 16 + 5 * 8,
89 };
90
91 static INLINE unsigned
92 align_nblocksx(enum pipe_format format, unsigned width, unsigned align_to)
93 {
94 return align(util_format_get_nblocksx(format, width), align_to);
95 }
96
97 static INLINE unsigned
98 align_nblocksy(enum pipe_format format, unsigned width, unsigned align_to)
99 {
100 return align(util_format_get_nblocksy(format, width), align_to);
101 }
102
103 static INLINE unsigned
104 get_pot_stride(enum pipe_format format, unsigned width)
105 {
106 return util_next_power_of_two(util_format_get_stride(format, width));
107 }
108
109 /*
110 * More advanced helper funcs
111 */
112
113
114 static void
115 i915_texture_set_level_info(struct i915_texture *tex,
116 unsigned level, unsigned nr_images)
117 {
118 assert(level < Elements(tex->nr_images));
119 assert(nr_images);
120 assert(!tex->image_offset[level]);
121
122 tex->nr_images[level] = nr_images;
123 tex->image_offset[level] = (unsigned *) MALLOC(nr_images * sizeof(unsigned));
124 tex->image_offset[level][0] = 0;
125 }
126
127 static void
128 i915_texture_set_image_offset(struct i915_texture *tex,
129 unsigned level, unsigned img,
130 unsigned x, unsigned y)
131 {
132 /* for the first image and level make sure offset is zero */
133 assert(!(img == 0 && level == 0) || (x == 0 && y == 0));
134 assert(img < tex->nr_images[level]);
135
136 tex->image_offset[level][img] = y * tex->stride + x * util_format_get_blocksize(tex->b.b.format);
137
138 #if DEBUG_TEXTURES
139 debug_printf("%s: %p level %u, img %u (%u, %u) %p\n", __FUNCTION__,
140 tex, level, img, x, y,
141 (void*)(uintptr_t)tex->image_offset[level][img]);
142 #endif
143 }
144
145
146 /*
147 * Shared layout functions
148 */
149
150
151 /**
152 * Special case to deal with scanout textures.
153 */
154 static boolean
155 i9x5_scanout_layout(struct i915_texture *tex)
156 {
157 struct pipe_resource *pt = &tex->b.b;
158
159 if (pt->last_level > 0 || util_format_get_blocksize(pt->format) != 4)
160 return FALSE;
161
162 i915_texture_set_level_info(tex, 0, 1);
163 i915_texture_set_image_offset(tex, 0, 0, 0, 0);
164
165 if (pt->width0 >= 240) {
166 tex->stride = get_pot_stride(pt->format, pt->width0);
167 tex->total_nblocksy = align_nblocksy(pt->format, pt->height0, 8);
168 tex->hw_tiled = I915_TILE_X;
169 } else if (pt->width0 == 64 && pt->height0 == 64) {
170 tex->stride = get_pot_stride(pt->format, pt->width0);
171 tex->total_nblocksy = align_nblocksy(pt->format, pt->height0, 8);
172 } else {
173 return FALSE;
174 }
175
176 #if DEBUG_TEXTURE
177 debug_printf("%s size: %d,%d,%d offset %d,%d (0x%x)\n", __FUNCTION__,
178 pt->width0, pt->height0, util_format_get_blocksize(pt->format),
179 tex->stride, tex->total_nblocksy, tex->stride * tex->total_nblocksy);
180 #endif
181
182 return TRUE;
183 }
184
185 /**
186 * Special case to deal with shared textures.
187 */
188 static boolean
189 i9x5_display_target_layout(struct i915_texture *tex)
190 {
191 struct pipe_resource *pt = &tex->b.b;
192
193 if (pt->last_level > 0 || util_format_get_blocksize(pt->format) != 4)
194 return FALSE;
195
196 /* fallback to normal textures for small textures */
197 if (pt->width0 < 240)
198 return FALSE;
199
200 i915_texture_set_level_info(tex, 0, 1);
201 i915_texture_set_image_offset(tex, 0, 0, 0, 0);
202
203 tex->stride = get_pot_stride(pt->format, pt->width0);
204 tex->total_nblocksy = align_nblocksy(pt->format, pt->height0, 8);
205 tex->hw_tiled = I915_TILE_X;
206
207 #if DEBUG_TEXTURE
208 debug_printf("%s size: %d,%d,%d offset %d,%d (0x%x)\n", __FUNCTION__,
209 pt->width0, pt->height0, util_format_get_blocksize(pt->format),
210 tex->stride, tex->total_nblocksy, tex->stride * tex->total_nblocksy);
211 #endif
212
213 return TRUE;
214 }
215
216 /**
217 * Helper function for special layouts
218 */
219 static boolean
220 i9x5_special_layout(struct i915_texture *tex)
221 {
222 struct pipe_resource *pt = &tex->b.b;
223
224 /* Scanouts needs special care */
225 if (pt->bind & PIPE_BIND_SCANOUT)
226 if (i9x5_scanout_layout(tex))
227 return TRUE;
228
229 /* Shared buffers needs to be compatible with X servers
230 *
231 * XXX: need a better name than shared for this if it is to be part
232 * of core gallium, and probably move the flag to resource.flags,
233 * rather than bindings.
234 */
235 if (pt->bind & (PIPE_BIND_SHARED | PIPE_BIND_DISPLAY_TARGET))
236 if (i9x5_display_target_layout(tex))
237 return TRUE;
238
239 return FALSE;
240 }
241
242 /**
243 * Cube layout used on i915 and for non-compressed textures on i945.
244 */
245 static void
246 i9x5_texture_layout_cube(struct i915_texture *tex)
247 {
248 struct pipe_resource *pt = &tex->b.b;
249 const unsigned nblocks = util_format_get_nblocksx(pt->format, pt->width0);
250 unsigned level;
251 unsigned face;
252
253 assert(pt->width0 == pt->height0); /* cubemap images are square */
254
255 /* double pitch for cube layouts */
256 tex->stride = align(nblocks * util_format_get_blocksize(pt->format) * 2, 4);
257 tex->total_nblocksy = nblocks * 4;
258
259 for (level = 0; level <= pt->last_level; level++)
260 i915_texture_set_level_info(tex, level, 6);
261
262 for (face = 0; face < 6; face++) {
263 unsigned x = initial_offsets[face][0] * nblocks;
264 unsigned y = initial_offsets[face][1] * nblocks;
265 unsigned d = nblocks;
266
267 for (level = 0; level <= pt->last_level; level++) {
268 i915_texture_set_image_offset(tex, level, face, x, y);
269 d >>= 1;
270 x += step_offsets[face][0] * d;
271 y += step_offsets[face][1] * d;
272 }
273 }
274 }
275
276
277 /*
278 * i915 layout functions
279 */
280
281
282 static void
283 i915_texture_layout_2d(struct i915_texture *tex)
284 {
285 struct pipe_resource *pt = &tex->b.b;
286 unsigned level;
287 unsigned width = pt->width0;
288 unsigned height = pt->height0;
289 unsigned nblocksy = util_format_get_nblocksy(pt->format, pt->width0);
290 unsigned align_y = 2;
291
292 if (util_format_is_s3tc(pt->format))
293 align_y = 1;
294
295 tex->stride = align(util_format_get_stride(pt->format, pt->width0), 4);
296 tex->total_nblocksy = 0;
297
298 for (level = 0; level <= pt->last_level; level++) {
299 i915_texture_set_level_info(tex, level, 1);
300 i915_texture_set_image_offset(tex, level, 0, 0, tex->total_nblocksy);
301
302 tex->total_nblocksy += nblocksy;
303
304 width = u_minify(width, 1);
305 height = u_minify(height, 1);
306 nblocksy = align_nblocksy(pt->format, height, align_y);
307 }
308 }
309
310 static void
311 i915_texture_layout_3d(struct i915_texture *tex)
312 {
313 struct pipe_resource *pt = &tex->b.b;
314 unsigned level;
315
316 unsigned width = pt->width0;
317 unsigned height = pt->height0;
318 unsigned depth = pt->depth0;
319 unsigned nblocksy = util_format_get_nblocksy(pt->format, pt->height0);
320 unsigned stack_nblocksy = 0;
321
322 /* Calculate the size of a single slice.
323 */
324 tex->stride = align(util_format_get_stride(pt->format, pt->width0), 4);
325
326 /* XXX: hardware expects/requires 9 levels at minimum.
327 */
328 for (level = 0; level <= MAX2(8, pt->last_level); level++) {
329 i915_texture_set_level_info(tex, level, depth);
330
331 stack_nblocksy += MAX2(2, nblocksy);
332
333 width = u_minify(width, 1);
334 height = u_minify(height, 1);
335 nblocksy = util_format_get_nblocksy(pt->format, height);
336 }
337
338 /* Fixup depth image_offsets:
339 */
340 for (level = 0; level <= pt->last_level; level++) {
341 unsigned i;
342 for (i = 0; i < depth; i++)
343 i915_texture_set_image_offset(tex, level, i, 0, i * stack_nblocksy);
344
345 depth = u_minify(depth, 1);
346 }
347
348 /* Multiply slice size by texture depth for total size. It's
349 * remarkable how wasteful of memory the i915 texture layouts
350 * are. They are largely fixed in the i945.
351 */
352 tex->total_nblocksy = stack_nblocksy * pt->depth0;
353 }
354
355 static boolean
356 i915_texture_layout(struct i915_texture * tex)
357 {
358 struct pipe_resource *pt = &tex->b.b;
359
360 switch (pt->target) {
361 case PIPE_TEXTURE_1D:
362 case PIPE_TEXTURE_2D:
363 if (!i9x5_special_layout(tex))
364 i915_texture_layout_2d(tex);
365 break;
366 case PIPE_TEXTURE_3D:
367 i915_texture_layout_3d(tex);
368 break;
369 case PIPE_TEXTURE_CUBE:
370 i9x5_texture_layout_cube(tex);
371 break;
372 default:
373 assert(0);
374 return FALSE;
375 }
376
377 return TRUE;
378 }
379
380
381 /*
382 * i945 layout functions
383 */
384
385
386 static void
387 i945_texture_layout_2d(struct i915_texture *tex)
388 {
389 struct pipe_resource *pt = &tex->b.b;
390 int align_x = 4, align_y = 2;
391 unsigned level;
392 unsigned x = 0;
393 unsigned y = 0;
394 unsigned width = pt->width0;
395 unsigned height = pt->height0;
396 unsigned nblocksx = util_format_get_nblocksx(pt->format, pt->width0);
397 unsigned nblocksy = util_format_get_nblocksy(pt->format, pt->height0);
398
399 if (util_format_is_s3tc(pt->format)) {
400 align_x = 1;
401 align_y = 1;
402 }
403
404 tex->stride = align(util_format_get_stride(pt->format, pt->width0), 4);
405
406 /* May need to adjust pitch to accomodate the placement of
407 * the 2nd mipmap level. This occurs when the alignment
408 * constraints of mipmap placement push the right edge of the
409 * 2nd mipmap level out past the width of its parent.
410 */
411 if (pt->last_level > 0) {
412 unsigned mip1_nblocksx =
413 align_nblocksx(pt->format, u_minify(pt->width0, 1), align_x) +
414 util_format_get_nblocksx(pt->format, u_minify(pt->width0, 2));
415
416 if (mip1_nblocksx > nblocksx)
417 tex->stride = mip1_nblocksx * util_format_get_blocksize(pt->format);
418 }
419
420 /* Pitch must be a whole number of dwords
421 */
422 tex->stride = align(tex->stride, 64);
423 tex->total_nblocksy = 0;
424
425 for (level = 0; level <= pt->last_level; level++) {
426 i915_texture_set_level_info(tex, level, 1);
427 i915_texture_set_image_offset(tex, level, 0, x, y);
428
429 /* Because the images are packed better, the final offset
430 * might not be the maximal one:
431 */
432 tex->total_nblocksy = MAX2(tex->total_nblocksy, y + nblocksy);
433
434 /* Layout_below: step right after second mipmap level.
435 */
436 if (level == 1) {
437 x += nblocksx;
438 } else {
439 y += nblocksy;
440 }
441
442 width = u_minify(width, 1);
443 height = u_minify(height, 1);
444 nblocksx = align_nblocksx(pt->format, width, align_x);
445 nblocksy = align_nblocksy(pt->format, height, align_y);
446 }
447 }
448
449 static void
450 i945_texture_layout_3d(struct i915_texture *tex)
451 {
452 struct pipe_resource *pt = &tex->b.b;
453 unsigned width = pt->width0;
454 unsigned height = pt->height0;
455 unsigned depth = pt->depth0;
456 unsigned nblocksy = util_format_get_nblocksy(pt->format, pt->width0);
457 unsigned pack_x_pitch, pack_x_nr;
458 unsigned pack_y_pitch;
459 unsigned level;
460
461 tex->stride = align(util_format_get_stride(pt->format, pt->width0), 4);
462 tex->total_nblocksy = 0;
463
464 pack_y_pitch = MAX2(nblocksy, 2);
465 pack_x_pitch = tex->stride / util_format_get_blocksize(pt->format);
466 pack_x_nr = 1;
467
468 for (level = 0; level <= pt->last_level; level++) {
469 int x = 0;
470 int y = 0;
471 unsigned q, j;
472
473 i915_texture_set_level_info(tex, level, depth);
474
475 for (q = 0; q < depth;) {
476 for (j = 0; j < pack_x_nr && q < depth; j++, q++) {
477 i915_texture_set_image_offset(tex, level, q, x, y + tex->total_nblocksy);
478 x += pack_x_pitch;
479 }
480
481 x = 0;
482 y += pack_y_pitch;
483 }
484
485 tex->total_nblocksy += y;
486
487 if (pack_x_pitch > 4) {
488 pack_x_pitch >>= 1;
489 pack_x_nr <<= 1;
490 assert(pack_x_pitch * pack_x_nr * util_format_get_blocksize(pt->format) <= tex->stride);
491 }
492
493 if (pack_y_pitch > 2) {
494 pack_y_pitch >>= 1;
495 }
496
497 width = u_minify(width, 1);
498 height = u_minify(height, 1);
499 depth = u_minify(depth, 1);
500 nblocksy = util_format_get_nblocksy(pt->format, height);
501 }
502 }
503
504 static void
505 i945_texture_layout_cube(struct i915_texture *tex)
506 {
507 struct pipe_resource *pt = &tex->b.b;
508 const unsigned nblocks = util_format_get_nblocksx(pt->format, pt->width0);
509 const unsigned dim = pt->width0;
510 unsigned level;
511 unsigned face;
512
513 assert(pt->width0 == pt->height0); /* cubemap images are square */
514 assert(util_next_power_of_two(pt->width0) == pt->width0); /* npot only */
515 assert(util_format_is_s3tc(pt->format)); /* compressed only */
516
517 /*
518 * Depending on the size of the largest images, pitch can be
519 * determined either by the old-style packing of cubemap faces,
520 * or the final row of 4x4, 2x2 and 1x1 faces below this.
521 *
522 * 64 * 2 / 4 = 32
523 * 14 * 2 = 28
524 */
525 if (pt->width0 >= 64)
526 tex->stride = nblocks * 2 * util_format_get_blocksize(pt->format);
527 else
528 tex->stride = 14 * 2 * util_format_get_blocksize(pt->format);
529
530 /*
531 * Something similary apply for height as well.
532 */
533 if (pt->width0 >= 4)
534 tex->total_nblocksy = nblocks * 4 + 1;
535 else
536 tex->total_nblocksy = 1;
537
538 /* Set all the levels to effectively occupy the whole rectangular region */
539 for (level = 0; level <= pt->last_level; level++)
540 i915_texture_set_level_info(tex, level, 6);
541
542 for (face = 0; face < 6; face++) {
543 /* all calculations in pixels */
544 unsigned total_height = tex->total_nblocksy * 4;
545 unsigned x = initial_offsets[face][0] * dim;
546 unsigned y = initial_offsets[face][1] * dim;
547 unsigned d = dim;
548
549 if (dim == 4 && face >= 4) {
550 x = (face - 4) * 8;
551 y = tex->total_nblocksy * 4 - 4; /* 4 = 1 block */
552 } else if (dim < 4 && (face > 0)) {
553 x = face * 8;
554 y = total_height - 4;
555 }
556
557 for (level = 0; level <= pt->last_level; level++) {
558 i915_texture_set_image_offset(tex, level, face,
559 util_format_get_nblocksx(pt->format, x),
560 util_format_get_nblocksy(pt->format, y));
561
562 d >>= 1;
563
564 switch (d) {
565 case 4:
566 switch (face) {
567 case PIPE_TEX_FACE_POS_X:
568 case PIPE_TEX_FACE_NEG_X:
569 x += step_offsets[face][0] * d;
570 y += step_offsets[face][1] * d;
571 break;
572 case PIPE_TEX_FACE_POS_Y:
573 case PIPE_TEX_FACE_NEG_Y:
574 y += 12;
575 x -= 8;
576 break;
577 case PIPE_TEX_FACE_POS_Z:
578 case PIPE_TEX_FACE_NEG_Z:
579 y = total_height - 4;
580 x = (face - 4) * 8;
581 break;
582 }
583 break;
584 case 2:
585 y = total_height - 4;
586 x = bottom_offsets[face];
587 break;
588 case 1:
589 x += 48;
590 break;
591 default:
592 x += step_offsets[face][0] * d;
593 y += step_offsets[face][1] * d;
594 break;
595 }
596 }
597 }
598 }
599
600 static boolean
601 i945_texture_layout(struct i915_texture * tex)
602 {
603 struct pipe_resource *pt = &tex->b.b;
604
605 switch (pt->target) {
606 case PIPE_TEXTURE_1D:
607 case PIPE_TEXTURE_2D:
608 if (!i9x5_special_layout(tex))
609 i945_texture_layout_2d(tex);
610 break;
611 case PIPE_TEXTURE_3D:
612 i945_texture_layout_3d(tex);
613 break;
614 case PIPE_TEXTURE_CUBE:
615 if (!util_format_is_s3tc(pt->format))
616 i9x5_texture_layout_cube(tex);
617 else
618 i945_texture_layout_cube(tex);
619 break;
620 default:
621 assert(0);
622 return FALSE;
623 }
624
625 return TRUE;
626 }
627
628
629
630 /*
631 * Screen texture functions
632 */
633
634
635
636 static boolean
637 i915_texture_get_handle(struct pipe_screen * screen,
638 struct pipe_resource *texture,
639 struct winsys_handle *whandle)
640 {
641 struct i915_screen *is = i915_screen(screen);
642 struct i915_texture *tex = i915_texture(texture);
643 struct i915_winsys *iws = is->iws;
644
645 return iws->buffer_get_handle(iws, tex->buffer, whandle, tex->stride);
646 }
647
648
649 static void
650 i915_texture_destroy(struct pipe_screen *screen,
651 struct pipe_resource *pt)
652 {
653 struct i915_texture *tex = i915_texture(pt);
654 struct i915_winsys *iws = i915_screen(screen)->iws;
655 uint i;
656
657 iws->buffer_destroy(iws, tex->buffer);
658
659 for (i = 0; i < Elements(tex->image_offset); i++)
660 if (tex->image_offset[i])
661 FREE(tex->image_offset[i]);
662
663 FREE(tex);
664 }
665
666 static struct pipe_transfer *
667 i915_texture_get_transfer(struct pipe_context *context,
668 struct pipe_resource *resource,
669 struct pipe_subresource sr,
670 unsigned usage,
671 const struct pipe_box *box)
672 {
673 struct i915_texture *tex = i915_texture(resource);
674 struct pipe_transfer *transfer = CALLOC_STRUCT(pipe_transfer);
675 if (transfer == NULL)
676 return NULL;
677
678 transfer->resource = resource;
679 transfer->sr = sr;
680 transfer->usage = usage;
681 transfer->box = *box;
682 transfer->stride = tex->stride;
683
684 return transfer;
685 }
686
687
688 static void *
689 i915_texture_transfer_map(struct pipe_context *pipe,
690 struct pipe_transfer *transfer)
691 {
692 struct pipe_resource *resource = transfer->resource;
693 struct i915_texture *tex = i915_texture(resource);
694 struct i915_winsys *iws = i915_screen(pipe->screen)->iws;
695 struct pipe_subresource sr = transfer->sr;
696 struct pipe_box *box = &transfer->box;
697 enum pipe_format format = resource->format;
698 unsigned offset;
699 char *map;
700
701 if (resource->target == PIPE_TEXTURE_CUBE) {
702 offset = tex->image_offset[sr.level][sr.face];
703 } else if (resource->target == PIPE_TEXTURE_3D) {
704 offset = tex->image_offset[sr.level][box->z];
705 } else {
706 offset = tex->image_offset[sr.level][0];
707 assert(sr.face == 0);
708 assert(box->z == 0);
709 }
710
711 map = iws->buffer_map(iws, tex->buffer,
712 (transfer->usage & PIPE_TRANSFER_WRITE) ? TRUE : FALSE);
713 if (map == NULL)
714 return NULL;
715
716 return map + offset +
717 box->y / util_format_get_blockheight(format) * transfer->stride +
718 box->x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
719 }
720
721 static void
722 i915_texture_transfer_unmap(struct pipe_context *pipe,
723 struct pipe_transfer *transfer)
724 {
725 struct i915_texture *tex = i915_texture(transfer->resource);
726 struct i915_winsys *iws = i915_screen(tex->b.b.screen)->iws;
727 iws->buffer_unmap(iws, tex->buffer);
728 }
729
730
731
732 struct u_resource_vtbl i915_texture_vtbl =
733 {
734 i915_texture_get_handle, /* get_handle */
735 i915_texture_destroy, /* resource_destroy */
736 NULL, /* is_resource_referenced */
737 i915_texture_get_transfer, /* get_transfer */
738 u_default_transfer_destroy, /* transfer_destroy */
739 i915_texture_transfer_map, /* transfer_map */
740 u_default_transfer_flush_region, /* transfer_flush_region */
741 i915_texture_transfer_unmap, /* transfer_unmap */
742 u_default_transfer_inline_write /* transfer_inline_write */
743 };
744
745
746
747
748 struct pipe_resource *
749 i915_texture_create(struct pipe_screen *screen,
750 const struct pipe_resource *template)
751 {
752 struct i915_screen *is = i915_screen(screen);
753 struct i915_winsys *iws = is->iws;
754 struct i915_texture *tex = CALLOC_STRUCT(i915_texture);
755 size_t tex_size;
756 unsigned buf_usage = 0;
757
758 if (!tex)
759 return NULL;
760
761 tex->b.b = *template;
762 tex->b.vtbl = &i915_texture_vtbl;
763 pipe_reference_init(&tex->b.b.reference, 1);
764 tex->b.b.screen = screen;
765
766 if (is->is_i945) {
767 if (!i945_texture_layout(tex))
768 goto fail;
769 } else {
770 if (!i915_texture_layout(tex))
771 goto fail;
772 }
773
774 tex_size = tex->stride * tex->total_nblocksy;
775
776 /* for scanouts and cursors, cursors arn't scanouts */
777
778 /* XXX: use a custom flag for cursors, don't rely on magically
779 * guessing that this is Xorg asking for a cursor
780 */
781 if ((template->bind & PIPE_BIND_SCANOUT) && template->width0 != 64)
782 buf_usage = I915_NEW_SCANOUT;
783 else
784 buf_usage = I915_NEW_TEXTURE;
785
786 tex->buffer = iws->buffer_create(iws, tex_size, 64, buf_usage);
787 if (!tex->buffer)
788 goto fail;
789
790 /* setup any hw fences */
791 if (tex->hw_tiled) {
792 assert(tex->sw_tiled == I915_TILE_NONE);
793 iws->buffer_set_fence_reg(iws, tex->buffer, tex->stride, tex->hw_tiled);
794 }
795
796
797 #if 0
798 void *ptr = ws->buffer_map(ws, tex->buffer,
799 PIPE_BUFFER_USAGE_CPU_WRITE);
800 memset(ptr, 0x80, tex_size);
801 ws->buffer_unmap(ws, tex->buffer);
802 #endif
803
804 I915_DBG(DBG_TEXTURE, "%s: %p size %u, stride %u, blocks (%u, %u)\n", __func__,
805 tex, (unsigned int)tex_size, tex->stride,
806 tex->stride / util_format_get_blocksize(tex->b.b.format),
807 tex->total_nblocksy);
808
809 return &tex->b.b;
810
811 fail:
812 FREE(tex);
813 return NULL;
814 }
815
816 struct pipe_resource *
817 i915_texture_from_handle(struct pipe_screen * screen,
818 const struct pipe_resource *template,
819 struct winsys_handle *whandle)
820 {
821 struct i915_screen *is = i915_screen(screen);
822 struct i915_texture *tex;
823 struct i915_winsys *iws = is->iws;
824 struct i915_winsys_buffer *buffer;
825 unsigned stride;
826
827 assert(screen);
828
829 buffer = iws->buffer_from_handle(iws, whandle, &stride);
830
831 /* Only supports one type */
832 if (template->target != PIPE_TEXTURE_2D ||
833 template->last_level != 0 ||
834 template->depth0 != 1) {
835 return NULL;
836 }
837
838 tex = CALLOC_STRUCT(i915_texture);
839 if (!tex)
840 return NULL;
841
842 tex->b.b = *template;
843 tex->b.vtbl = &i915_texture_vtbl;
844 pipe_reference_init(&tex->b.b.reference, 1);
845 tex->b.b.screen = screen;
846
847 tex->stride = stride;
848
849 i915_texture_set_level_info(tex, 0, 1);
850 i915_texture_set_image_offset(tex, 0, 0, 0, 0);
851
852 tex->buffer = buffer;
853
854 I915_DBG(DBG_TEXTURE, "%s: %p stride %u, blocks (%ux%u)\n", __func__,
855 tex, tex->stride,
856 tex->stride / util_format_get_blocksize(tex->b.b.format),
857 tex->total_nblocksy);
858
859 return &tex->b.b;
860 }
861