gallium: kill is_resource_referenced
[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 static INLINE const char*
110 get_tiling_string(enum i915_winsys_buffer_tile tile)
111 {
112 switch(tile) {
113 case I915_TILE_NONE:
114 return "none";
115 case I915_TILE_X:
116 return "x";
117 case I915_TILE_Y:
118 return "y";
119 default:
120 assert(FALSE);
121 return "?";
122 }
123 }
124
125
126 /*
127 * More advanced helper funcs
128 */
129
130
131 static void
132 i915_texture_set_level_info(struct i915_texture *tex,
133 unsigned level, unsigned nr_images)
134 {
135 assert(level < Elements(tex->nr_images));
136 assert(nr_images);
137 assert(!tex->image_offset[level]);
138
139 tex->nr_images[level] = nr_images;
140 tex->image_offset[level] = MALLOC(nr_images * sizeof(struct offset_pair));
141 tex->image_offset[level][0].nblocksx = 0;
142 tex->image_offset[level][0].nblocksy = 0;
143 }
144
145 INLINE unsigned i915_texture_offset(struct i915_texture *tex,
146 unsigned level, unsigned layer)
147 {
148 unsigned x, y;
149 x = tex->image_offset[level][layer].nblocksx
150 * util_format_get_blocksize(tex->b.b.format);
151 y = tex->image_offset[level][layer].nblocksy;
152
153 return y * tex->stride + x;
154 }
155
156 static void
157 i915_texture_set_image_offset(struct i915_texture *tex,
158 unsigned level, unsigned img,
159 unsigned nblocksx, unsigned nblocksy)
160 {
161 /* for the first image and level make sure offset is zero */
162 assert(!(img == 0 && level == 0) || (nblocksx == 0 && nblocksy == 0));
163 assert(img < tex->nr_images[level]);
164
165 tex->image_offset[level][img].nblocksx = nblocksx;
166 tex->image_offset[level][img].nblocksy = nblocksy;
167
168 #if DEBUG_TEXTURES
169 debug_printf("%s: %p level %u, img %u (%u, %u)\n", __FUNCTION__,
170 tex, level, img, x, y);
171 #endif
172 }
173
174 static enum i915_winsys_buffer_tile
175 i915_texture_tiling(struct i915_screen *is, struct i915_texture *tex)
176 {
177 if (!is->debug.tiling)
178 return I915_TILE_NONE;
179
180 if (tex->b.b.target == PIPE_TEXTURE_1D)
181 return I915_TILE_NONE;
182
183 if (util_format_is_s3tc(tex->b.b.format))
184 /* XXX X-tiling might make sense */
185 return I915_TILE_NONE;
186
187 return I915_TILE_X;
188 }
189
190
191 /*
192 * Shared layout functions
193 */
194
195
196 /**
197 * Special case to deal with scanout textures.
198 */
199 static boolean
200 i9x5_scanout_layout(struct i915_texture *tex)
201 {
202 struct pipe_resource *pt = &tex->b.b;
203
204 if (pt->last_level > 0 || util_format_get_blocksize(pt->format) != 4)
205 return FALSE;
206
207 i915_texture_set_level_info(tex, 0, 1);
208 i915_texture_set_image_offset(tex, 0, 0, 0, 0);
209
210 if (pt->width0 >= 240) {
211 tex->stride = align(util_format_get_stride(pt->format, pt->width0), 64);
212 tex->total_nblocksy = align_nblocksy(pt->format, pt->height0, 8);
213 tex->tiling = I915_TILE_X;
214 /* special case for cursors */
215 } else if (pt->width0 == 64 && pt->height0 == 64) {
216 tex->stride = get_pot_stride(pt->format, pt->width0);
217 tex->total_nblocksy = align_nblocksy(pt->format, pt->height0, 8);
218 } else {
219 return FALSE;
220 }
221
222 #if DEBUG_TEXTURE
223 debug_printf("%s size: %d,%d,%d offset %d,%d (0x%x)\n", __FUNCTION__,
224 pt->width0, pt->height0, util_format_get_blocksize(pt->format),
225 tex->stride, tex->total_nblocksy, tex->stride * tex->total_nblocksy);
226 #endif
227
228 return TRUE;
229 }
230
231 /**
232 * Special case to deal with shared textures.
233 */
234 static boolean
235 i9x5_display_target_layout(struct i915_texture *tex)
236 {
237 struct pipe_resource *pt = &tex->b.b;
238
239 if (pt->last_level > 0 || util_format_get_blocksize(pt->format) != 4)
240 return FALSE;
241
242 /* fallback to normal textures for small textures */
243 if (pt->width0 < 240)
244 return FALSE;
245
246 i915_texture_set_level_info(tex, 0, 1);
247 i915_texture_set_image_offset(tex, 0, 0, 0, 0);
248
249 tex->stride = align(util_format_get_stride(pt->format, pt->width0), 64);
250 tex->total_nblocksy = align_nblocksy(pt->format, pt->height0, 8);
251 tex->tiling = I915_TILE_X;
252
253 #if DEBUG_TEXTURE
254 debug_printf("%s size: %d,%d,%d offset %d,%d (0x%x)\n", __FUNCTION__,
255 pt->width0, pt->height0, util_format_get_blocksize(pt->format),
256 tex->stride, tex->total_nblocksy, tex->stride * tex->total_nblocksy);
257 #endif
258
259 return TRUE;
260 }
261
262 /**
263 * Helper function for special layouts
264 */
265 static boolean
266 i9x5_special_layout(struct i915_texture *tex)
267 {
268 struct pipe_resource *pt = &tex->b.b;
269
270 /* Scanouts needs special care */
271 if (pt->bind & PIPE_BIND_SCANOUT)
272 if (i9x5_scanout_layout(tex))
273 return TRUE;
274
275 /* Shared buffers needs to be compatible with X servers
276 *
277 * XXX: need a better name than shared for this if it is to be part
278 * of core gallium, and probably move the flag to resource.flags,
279 * rather than bindings.
280 */
281 if (pt->bind & (PIPE_BIND_SHARED | PIPE_BIND_DISPLAY_TARGET))
282 if (i9x5_display_target_layout(tex))
283 return TRUE;
284
285 return FALSE;
286 }
287
288 /**
289 * Cube layout used on i915 and for non-compressed textures on i945.
290 */
291 static void
292 i9x5_texture_layout_cube(struct i915_texture *tex)
293 {
294 struct pipe_resource *pt = &tex->b.b;
295 const unsigned nblocks = util_format_get_nblocksx(pt->format, pt->width0);
296 unsigned level;
297 unsigned face;
298
299 assert(pt->width0 == pt->height0); /* cubemap images are square */
300
301 /* double pitch for cube layouts */
302 tex->stride = align(nblocks * util_format_get_blocksize(pt->format) * 2, 4);
303 tex->total_nblocksy = nblocks * 4;
304
305 for (level = 0; level <= pt->last_level; level++)
306 i915_texture_set_level_info(tex, level, 6);
307
308 for (face = 0; face < 6; face++) {
309 unsigned x = initial_offsets[face][0] * nblocks;
310 unsigned y = initial_offsets[face][1] * nblocks;
311 unsigned d = nblocks;
312
313 for (level = 0; level <= pt->last_level; level++) {
314 i915_texture_set_image_offset(tex, level, face, x, y);
315 d >>= 1;
316 x += step_offsets[face][0] * d;
317 y += step_offsets[face][1] * d;
318 }
319 }
320 }
321
322
323 /*
324 * i915 layout functions
325 */
326
327
328 static void
329 i915_texture_layout_2d(struct i915_texture *tex)
330 {
331 struct pipe_resource *pt = &tex->b.b;
332 unsigned level;
333 unsigned width = pt->width0;
334 unsigned height = pt->height0;
335 unsigned nblocksy = util_format_get_nblocksy(pt->format, pt->width0);
336 unsigned align_y = 2;
337
338 if (util_format_is_s3tc(pt->format))
339 align_y = 1;
340
341 tex->stride = align(util_format_get_stride(pt->format, pt->width0), 4);
342 tex->total_nblocksy = 0;
343
344 for (level = 0; level <= pt->last_level; level++) {
345 i915_texture_set_level_info(tex, level, 1);
346 i915_texture_set_image_offset(tex, level, 0, 0, tex->total_nblocksy);
347
348 tex->total_nblocksy += nblocksy;
349
350 width = u_minify(width, 1);
351 height = u_minify(height, 1);
352 nblocksy = align_nblocksy(pt->format, height, align_y);
353 }
354 }
355
356 static void
357 i915_texture_layout_3d(struct i915_texture *tex)
358 {
359 struct pipe_resource *pt = &tex->b.b;
360 unsigned level;
361
362 unsigned width = pt->width0;
363 unsigned height = pt->height0;
364 unsigned depth = pt->depth0;
365 unsigned nblocksy = util_format_get_nblocksy(pt->format, pt->height0);
366 unsigned stack_nblocksy = 0;
367
368 /* Calculate the size of a single slice.
369 */
370 tex->stride = align(util_format_get_stride(pt->format, pt->width0), 4);
371
372 /* XXX: hardware expects/requires 9 levels at minimum.
373 */
374 for (level = 0; level <= MAX2(8, pt->last_level); level++) {
375 i915_texture_set_level_info(tex, level, depth);
376
377 stack_nblocksy += MAX2(2, nblocksy);
378
379 width = u_minify(width, 1);
380 height = u_minify(height, 1);
381 nblocksy = util_format_get_nblocksy(pt->format, height);
382 }
383
384 /* Fixup depth image_offsets:
385 */
386 for (level = 0; level <= pt->last_level; level++) {
387 unsigned i;
388 for (i = 0; i < depth; i++)
389 i915_texture_set_image_offset(tex, level, i, 0, i * stack_nblocksy);
390
391 depth = u_minify(depth, 1);
392 }
393
394 /* Multiply slice size by texture depth for total size. It's
395 * remarkable how wasteful of memory the i915 texture layouts
396 * are. They are largely fixed in the i945.
397 */
398 tex->total_nblocksy = stack_nblocksy * pt->depth0;
399 }
400
401 static boolean
402 i915_texture_layout(struct i915_texture * tex)
403 {
404 switch (tex->b.b.target) {
405 case PIPE_TEXTURE_1D:
406 case PIPE_TEXTURE_2D:
407 case PIPE_TEXTURE_RECT:
408 if (!i9x5_special_layout(tex))
409 i915_texture_layout_2d(tex);
410 break;
411 case PIPE_TEXTURE_3D:
412 i915_texture_layout_3d(tex);
413 break;
414 case PIPE_TEXTURE_CUBE:
415 i9x5_texture_layout_cube(tex);
416 break;
417 default:
418 assert(0);
419 return FALSE;
420 }
421
422 return TRUE;
423 }
424
425
426 /*
427 * i945 layout functions
428 */
429
430
431 static void
432 i945_texture_layout_2d(struct i915_texture *tex)
433 {
434 struct pipe_resource *pt = &tex->b.b;
435 int align_x = 4, align_y = 2;
436 unsigned level;
437 unsigned x = 0;
438 unsigned y = 0;
439 unsigned width = pt->width0;
440 unsigned height = pt->height0;
441 unsigned nblocksx = util_format_get_nblocksx(pt->format, pt->width0);
442 unsigned nblocksy = util_format_get_nblocksy(pt->format, pt->height0);
443
444 if (util_format_is_s3tc(pt->format)) {
445 align_x = 1;
446 align_y = 1;
447 }
448
449 tex->stride = align(util_format_get_stride(pt->format, pt->width0), 4);
450
451 /* May need to adjust pitch to accomodate the placement of
452 * the 2nd mipmap level. This occurs when the alignment
453 * constraints of mipmap placement push the right edge of the
454 * 2nd mipmap level out past the width of its parent.
455 */
456 if (pt->last_level > 0) {
457 unsigned mip1_nblocksx =
458 align_nblocksx(pt->format, u_minify(pt->width0, 1), align_x) +
459 util_format_get_nblocksx(pt->format, u_minify(pt->width0, 2));
460
461 if (mip1_nblocksx > nblocksx)
462 tex->stride = mip1_nblocksx * util_format_get_blocksize(pt->format);
463 }
464
465 /* Pitch must be a whole number of dwords
466 */
467 tex->stride = align(tex->stride, 64);
468 tex->total_nblocksy = 0;
469
470 for (level = 0; level <= pt->last_level; level++) {
471 i915_texture_set_level_info(tex, level, 1);
472 i915_texture_set_image_offset(tex, level, 0, x, y);
473
474 /* Because the images are packed better, the final offset
475 * might not be the maximal one:
476 */
477 tex->total_nblocksy = MAX2(tex->total_nblocksy, y + nblocksy);
478
479 /* Layout_below: step right after second mipmap level.
480 */
481 if (level == 1) {
482 x += nblocksx;
483 } else {
484 y += nblocksy;
485 }
486
487 width = u_minify(width, 1);
488 height = u_minify(height, 1);
489 nblocksx = align_nblocksx(pt->format, width, align_x);
490 nblocksy = align_nblocksy(pt->format, height, align_y);
491 }
492 }
493
494 static void
495 i945_texture_layout_3d(struct i915_texture *tex)
496 {
497 struct pipe_resource *pt = &tex->b.b;
498 unsigned width = pt->width0;
499 unsigned height = pt->height0;
500 unsigned depth = pt->depth0;
501 unsigned nblocksy = util_format_get_nblocksy(pt->format, pt->width0);
502 unsigned pack_x_pitch, pack_x_nr;
503 unsigned pack_y_pitch;
504 unsigned level;
505
506 tex->stride = align(util_format_get_stride(pt->format, pt->width0), 4);
507 tex->total_nblocksy = 0;
508
509 pack_y_pitch = MAX2(nblocksy, 2);
510 pack_x_pitch = tex->stride / util_format_get_blocksize(pt->format);
511 pack_x_nr = 1;
512
513 for (level = 0; level <= pt->last_level; level++) {
514 int x = 0;
515 int y = 0;
516 unsigned q, j;
517
518 i915_texture_set_level_info(tex, level, depth);
519
520 for (q = 0; q < depth;) {
521 for (j = 0; j < pack_x_nr && q < depth; j++, q++) {
522 i915_texture_set_image_offset(tex, level, q, x, y + tex->total_nblocksy);
523 x += pack_x_pitch;
524 }
525
526 x = 0;
527 y += pack_y_pitch;
528 }
529
530 tex->total_nblocksy += y;
531
532 if (pack_x_pitch > 4) {
533 pack_x_pitch >>= 1;
534 pack_x_nr <<= 1;
535 assert(pack_x_pitch * pack_x_nr * util_format_get_blocksize(pt->format) <= tex->stride);
536 }
537
538 if (pack_y_pitch > 2) {
539 pack_y_pitch >>= 1;
540 }
541
542 width = u_minify(width, 1);
543 height = u_minify(height, 1);
544 depth = u_minify(depth, 1);
545 nblocksy = util_format_get_nblocksy(pt->format, height);
546 }
547 }
548
549 static void
550 i945_texture_layout_cube(struct i915_texture *tex)
551 {
552 struct pipe_resource *pt = &tex->b.b;
553 const unsigned nblocks = util_format_get_nblocksx(pt->format, pt->width0);
554 const unsigned dim = pt->width0;
555 unsigned level;
556 unsigned face;
557
558 assert(pt->width0 == pt->height0); /* cubemap images are square */
559 assert(util_next_power_of_two(pt->width0) == pt->width0); /* npot only */
560 assert(util_format_is_s3tc(pt->format)); /* compressed only */
561
562 /*
563 * Depending on the size of the largest images, pitch can be
564 * determined either by the old-style packing of cubemap faces,
565 * or the final row of 4x4, 2x2 and 1x1 faces below this.
566 *
567 * 64 * 2 / 4 = 32
568 * 14 * 2 = 28
569 */
570 if (pt->width0 >= 64)
571 tex->stride = nblocks * 2 * util_format_get_blocksize(pt->format);
572 else
573 tex->stride = 14 * 2 * util_format_get_blocksize(pt->format);
574
575 /*
576 * Something similary apply for height as well.
577 */
578 if (pt->width0 >= 4)
579 tex->total_nblocksy = nblocks * 4 + 1;
580 else
581 tex->total_nblocksy = 1;
582
583 /* Set all the levels to effectively occupy the whole rectangular region */
584 for (level = 0; level <= pt->last_level; level++)
585 i915_texture_set_level_info(tex, level, 6);
586
587 for (face = 0; face < 6; face++) {
588 /* all calculations in pixels */
589 unsigned total_height = tex->total_nblocksy * 4;
590 unsigned x = initial_offsets[face][0] * dim;
591 unsigned y = initial_offsets[face][1] * dim;
592 unsigned d = dim;
593
594 if (dim == 4 && face >= 4) {
595 x = (face - 4) * 8;
596 y = tex->total_nblocksy * 4 - 4; /* 4 = 1 block */
597 } else if (dim < 4 && (face > 0)) {
598 x = face * 8;
599 y = total_height - 4;
600 }
601
602 for (level = 0; level <= pt->last_level; level++) {
603 i915_texture_set_image_offset(tex, level, face,
604 util_format_get_nblocksx(pt->format, x),
605 util_format_get_nblocksy(pt->format, y));
606
607 d >>= 1;
608
609 switch (d) {
610 case 4:
611 switch (face) {
612 case PIPE_TEX_FACE_POS_X:
613 case PIPE_TEX_FACE_NEG_X:
614 x += step_offsets[face][0] * d;
615 y += step_offsets[face][1] * d;
616 break;
617 case PIPE_TEX_FACE_POS_Y:
618 case PIPE_TEX_FACE_NEG_Y:
619 y += 12;
620 x -= 8;
621 break;
622 case PIPE_TEX_FACE_POS_Z:
623 case PIPE_TEX_FACE_NEG_Z:
624 y = total_height - 4;
625 x = (face - 4) * 8;
626 break;
627 }
628 break;
629 case 2:
630 y = total_height - 4;
631 x = bottom_offsets[face];
632 break;
633 case 1:
634 x += 48;
635 break;
636 default:
637 x += step_offsets[face][0] * d;
638 y += step_offsets[face][1] * d;
639 break;
640 }
641 }
642 }
643 }
644
645 static boolean
646 i945_texture_layout(struct i915_texture * tex)
647 {
648 switch (tex->b.b.target) {
649 case PIPE_TEXTURE_1D:
650 case PIPE_TEXTURE_2D:
651 case PIPE_TEXTURE_RECT:
652 if (!i9x5_special_layout(tex))
653 i945_texture_layout_2d(tex);
654 break;
655 case PIPE_TEXTURE_3D:
656 i945_texture_layout_3d(tex);
657 break;
658 case PIPE_TEXTURE_CUBE:
659 if (!util_format_is_s3tc(tex->b.b.format))
660 i9x5_texture_layout_cube(tex);
661 else
662 i945_texture_layout_cube(tex);
663 break;
664 default:
665 assert(0);
666 return FALSE;
667 }
668
669 return TRUE;
670 }
671
672
673
674 /*
675 * Screen texture functions
676 */
677
678
679
680 static boolean
681 i915_texture_get_handle(struct pipe_screen * screen,
682 struct pipe_resource *texture,
683 struct winsys_handle *whandle)
684 {
685 struct i915_screen *is = i915_screen(screen);
686 struct i915_texture *tex = i915_texture(texture);
687 struct i915_winsys *iws = is->iws;
688
689 return iws->buffer_get_handle(iws, tex->buffer, whandle, tex->stride);
690 }
691
692
693 static void
694 i915_texture_destroy(struct pipe_screen *screen,
695 struct pipe_resource *pt)
696 {
697 struct i915_texture *tex = i915_texture(pt);
698 struct i915_winsys *iws = i915_screen(screen)->iws;
699 uint i;
700
701 iws->buffer_destroy(iws, tex->buffer);
702
703 for (i = 0; i < Elements(tex->image_offset); i++)
704 if (tex->image_offset[i])
705 FREE(tex->image_offset[i]);
706
707 FREE(tex);
708 }
709
710 static struct pipe_transfer *
711 i915_texture_get_transfer(struct pipe_context *pipe,
712 struct pipe_resource *resource,
713 unsigned level,
714 unsigned usage,
715 const struct pipe_box *box)
716 {
717 struct i915_context *i915 = i915_context(pipe);
718 struct i915_texture *tex = i915_texture(resource);
719 struct pipe_transfer *transfer = util_slab_alloc(&i915->transfer_pool);
720
721 if (transfer == NULL)
722 return NULL;
723
724 transfer->resource = resource;
725 transfer->level = level;
726 transfer->usage = usage;
727 transfer->box = *box;
728 transfer->stride = tex->stride;
729 /* FIXME: layer_stride */
730
731 return transfer;
732 }
733
734 static void
735 i915_transfer_destroy(struct pipe_context *pipe,
736 struct pipe_transfer *transfer)
737 {
738 struct i915_context *i915 = i915_context(pipe);
739 util_slab_free(&i915->transfer_pool, transfer);
740 }
741
742 static void *
743 i915_texture_transfer_map(struct pipe_context *pipe,
744 struct pipe_transfer *transfer)
745 {
746 struct pipe_resource *resource = transfer->resource;
747 struct i915_texture *tex = i915_texture(resource);
748 struct i915_winsys *iws = i915_screen(pipe->screen)->iws;
749 struct pipe_box *box = &transfer->box;
750 enum pipe_format format = resource->format;
751 unsigned offset;
752 char *map;
753
754 if (resource->target != PIPE_TEXTURE_3D &&
755 resource->target != PIPE_TEXTURE_CUBE)
756 assert(box->z == 0);
757 offset = i915_texture_offset(tex, transfer->level, box->z);
758
759 map = iws->buffer_map(iws, tex->buffer,
760 (transfer->usage & PIPE_TRANSFER_WRITE) ? TRUE : FALSE);
761 if (map == NULL)
762 return NULL;
763
764 return map + offset +
765 box->y / util_format_get_blockheight(format) * transfer->stride +
766 box->x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
767 }
768
769 static void
770 i915_texture_transfer_unmap(struct pipe_context *pipe,
771 struct pipe_transfer *transfer)
772 {
773 struct i915_texture *tex = i915_texture(transfer->resource);
774 struct i915_winsys *iws = i915_screen(tex->b.b.screen)->iws;
775 iws->buffer_unmap(iws, tex->buffer);
776 }
777
778
779
780 struct u_resource_vtbl i915_texture_vtbl =
781 {
782 i915_texture_get_handle, /* get_handle */
783 i915_texture_destroy, /* resource_destroy */
784 i915_texture_get_transfer, /* get_transfer */
785 i915_transfer_destroy, /* transfer_destroy */
786 i915_texture_transfer_map, /* transfer_map */
787 u_default_transfer_flush_region, /* transfer_flush_region */
788 i915_texture_transfer_unmap, /* transfer_unmap */
789 u_default_transfer_inline_write /* transfer_inline_write */
790 };
791
792
793
794
795 struct pipe_resource *
796 i915_texture_create(struct pipe_screen *screen,
797 const struct pipe_resource *template)
798 {
799 struct i915_screen *is = i915_screen(screen);
800 struct i915_winsys *iws = is->iws;
801 struct i915_texture *tex = CALLOC_STRUCT(i915_texture);
802 unsigned buf_usage = 0;
803
804 if (!tex)
805 return NULL;
806
807 tex->b.b = *template;
808 tex->b.vtbl = &i915_texture_vtbl;
809 pipe_reference_init(&tex->b.b.reference, 1);
810 tex->b.b.screen = screen;
811
812 tex->tiling = i915_texture_tiling(is, tex);
813
814 if (is->is_i945) {
815 if (!i945_texture_layout(tex))
816 goto fail;
817 } else {
818 if (!i915_texture_layout(tex))
819 goto fail;
820 }
821
822 /* for scanouts and cursors, cursors arn't scanouts */
823
824 /* XXX: use a custom flag for cursors, don't rely on magically
825 * guessing that this is Xorg asking for a cursor
826 */
827 if ((template->bind & PIPE_BIND_SCANOUT) && template->width0 != 64)
828 buf_usage = I915_NEW_SCANOUT;
829 else
830 buf_usage = I915_NEW_TEXTURE;
831
832 tex->buffer = iws->buffer_create_tiled(iws, &tex->stride, tex->total_nblocksy,
833 &tex->tiling, buf_usage);
834 if (!tex->buffer)
835 goto fail;
836
837 I915_DBG(DBG_TEXTURE, "%s: %p stride %u, blocks (%u, %u) tiling %s\n", __func__,
838 tex, tex->stride,
839 tex->stride / util_format_get_blocksize(tex->b.b.format),
840 tex->total_nblocksy, get_tiling_string(tex->tiling));
841
842 return &tex->b.b;
843
844 fail:
845 FREE(tex);
846 return NULL;
847 }
848
849 struct pipe_resource *
850 i915_texture_from_handle(struct pipe_screen * screen,
851 const struct pipe_resource *template,
852 struct winsys_handle *whandle)
853 {
854 struct i915_screen *is = i915_screen(screen);
855 struct i915_texture *tex;
856 struct i915_winsys *iws = is->iws;
857 struct i915_winsys_buffer *buffer;
858 unsigned stride;
859 enum i915_winsys_buffer_tile tiling;
860
861 assert(screen);
862
863 buffer = iws->buffer_from_handle(iws, whandle, &tiling, &stride);
864
865 /* Only supports one type */
866 if ((template->target != PIPE_TEXTURE_2D &&
867 template->target != PIPE_TEXTURE_RECT) ||
868 template->last_level != 0 ||
869 template->depth0 != 1) {
870 return NULL;
871 }
872
873 tex = CALLOC_STRUCT(i915_texture);
874 if (!tex)
875 return NULL;
876
877 tex->b.b = *template;
878 tex->b.vtbl = &i915_texture_vtbl;
879 pipe_reference_init(&tex->b.b.reference, 1);
880 tex->b.b.screen = screen;
881
882 tex->stride = stride;
883 tex->tiling = tiling;
884 tex->total_nblocksy = align_nblocksy(tex->b.b.format, tex->b.b.height0, 8);
885
886 i915_texture_set_level_info(tex, 0, 1);
887 i915_texture_set_image_offset(tex, 0, 0, 0, 0);
888
889 tex->buffer = buffer;
890
891 I915_DBG(DBG_TEXTURE, "%s: %p stride %u, blocks (%u, %u) tiling %s\n", __func__,
892 tex, tex->stride,
893 tex->stride / util_format_get_blocksize(tex->b.b.format),
894 tex->total_nblocksy, get_tiling_string(tex->tiling));
895
896 return &tex->b.b;
897 }
898