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