Merge remote branch 'origin/master' into nv50-compiler
[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 case PIPE_TEXTURE_RECT:
364 if (!i9x5_special_layout(tex))
365 i915_texture_layout_2d(tex);
366 break;
367 case PIPE_TEXTURE_3D:
368 i915_texture_layout_3d(tex);
369 break;
370 case PIPE_TEXTURE_CUBE:
371 i9x5_texture_layout_cube(tex);
372 break;
373 default:
374 assert(0);
375 return FALSE;
376 }
377
378 return TRUE;
379 }
380
381
382 /*
383 * i945 layout functions
384 */
385
386
387 static void
388 i945_texture_layout_2d(struct i915_texture *tex)
389 {
390 struct pipe_resource *pt = &tex->b.b;
391 int align_x = 4, align_y = 2;
392 unsigned level;
393 unsigned x = 0;
394 unsigned y = 0;
395 unsigned width = pt->width0;
396 unsigned height = pt->height0;
397 unsigned nblocksx = util_format_get_nblocksx(pt->format, pt->width0);
398 unsigned nblocksy = util_format_get_nblocksy(pt->format, pt->height0);
399
400 if (util_format_is_s3tc(pt->format)) {
401 align_x = 1;
402 align_y = 1;
403 }
404
405 tex->stride = align(util_format_get_stride(pt->format, pt->width0), 4);
406
407 /* May need to adjust pitch to accomodate the placement of
408 * the 2nd mipmap level. This occurs when the alignment
409 * constraints of mipmap placement push the right edge of the
410 * 2nd mipmap level out past the width of its parent.
411 */
412 if (pt->last_level > 0) {
413 unsigned mip1_nblocksx =
414 align_nblocksx(pt->format, u_minify(pt->width0, 1), align_x) +
415 util_format_get_nblocksx(pt->format, u_minify(pt->width0, 2));
416
417 if (mip1_nblocksx > nblocksx)
418 tex->stride = mip1_nblocksx * util_format_get_blocksize(pt->format);
419 }
420
421 /* Pitch must be a whole number of dwords
422 */
423 tex->stride = align(tex->stride, 64);
424 tex->total_nblocksy = 0;
425
426 for (level = 0; level <= pt->last_level; level++) {
427 i915_texture_set_level_info(tex, level, 1);
428 i915_texture_set_image_offset(tex, level, 0, x, y);
429
430 /* Because the images are packed better, the final offset
431 * might not be the maximal one:
432 */
433 tex->total_nblocksy = MAX2(tex->total_nblocksy, y + nblocksy);
434
435 /* Layout_below: step right after second mipmap level.
436 */
437 if (level == 1) {
438 x += nblocksx;
439 } else {
440 y += nblocksy;
441 }
442
443 width = u_minify(width, 1);
444 height = u_minify(height, 1);
445 nblocksx = align_nblocksx(pt->format, width, align_x);
446 nblocksy = align_nblocksy(pt->format, height, align_y);
447 }
448 }
449
450 static void
451 i945_texture_layout_3d(struct i915_texture *tex)
452 {
453 struct pipe_resource *pt = &tex->b.b;
454 unsigned width = pt->width0;
455 unsigned height = pt->height0;
456 unsigned depth = pt->depth0;
457 unsigned nblocksy = util_format_get_nblocksy(pt->format, pt->width0);
458 unsigned pack_x_pitch, pack_x_nr;
459 unsigned pack_y_pitch;
460 unsigned level;
461
462 tex->stride = align(util_format_get_stride(pt->format, pt->width0), 4);
463 tex->total_nblocksy = 0;
464
465 pack_y_pitch = MAX2(nblocksy, 2);
466 pack_x_pitch = tex->stride / util_format_get_blocksize(pt->format);
467 pack_x_nr = 1;
468
469 for (level = 0; level <= pt->last_level; level++) {
470 int x = 0;
471 int y = 0;
472 unsigned q, j;
473
474 i915_texture_set_level_info(tex, level, depth);
475
476 for (q = 0; q < depth;) {
477 for (j = 0; j < pack_x_nr && q < depth; j++, q++) {
478 i915_texture_set_image_offset(tex, level, q, x, y + tex->total_nblocksy);
479 x += pack_x_pitch;
480 }
481
482 x = 0;
483 y += pack_y_pitch;
484 }
485
486 tex->total_nblocksy += y;
487
488 if (pack_x_pitch > 4) {
489 pack_x_pitch >>= 1;
490 pack_x_nr <<= 1;
491 assert(pack_x_pitch * pack_x_nr * util_format_get_blocksize(pt->format) <= tex->stride);
492 }
493
494 if (pack_y_pitch > 2) {
495 pack_y_pitch >>= 1;
496 }
497
498 width = u_minify(width, 1);
499 height = u_minify(height, 1);
500 depth = u_minify(depth, 1);
501 nblocksy = util_format_get_nblocksy(pt->format, height);
502 }
503 }
504
505 static void
506 i945_texture_layout_cube(struct i915_texture *tex)
507 {
508 struct pipe_resource *pt = &tex->b.b;
509 const unsigned nblocks = util_format_get_nblocksx(pt->format, pt->width0);
510 const unsigned dim = pt->width0;
511 unsigned level;
512 unsigned face;
513
514 assert(pt->width0 == pt->height0); /* cubemap images are square */
515 assert(util_next_power_of_two(pt->width0) == pt->width0); /* npot only */
516 assert(util_format_is_s3tc(pt->format)); /* compressed only */
517
518 /*
519 * Depending on the size of the largest images, pitch can be
520 * determined either by the old-style packing of cubemap faces,
521 * or the final row of 4x4, 2x2 and 1x1 faces below this.
522 *
523 * 64 * 2 / 4 = 32
524 * 14 * 2 = 28
525 */
526 if (pt->width0 >= 64)
527 tex->stride = nblocks * 2 * util_format_get_blocksize(pt->format);
528 else
529 tex->stride = 14 * 2 * util_format_get_blocksize(pt->format);
530
531 /*
532 * Something similary apply for height as well.
533 */
534 if (pt->width0 >= 4)
535 tex->total_nblocksy = nblocks * 4 + 1;
536 else
537 tex->total_nblocksy = 1;
538
539 /* Set all the levels to effectively occupy the whole rectangular region */
540 for (level = 0; level <= pt->last_level; level++)
541 i915_texture_set_level_info(tex, level, 6);
542
543 for (face = 0; face < 6; face++) {
544 /* all calculations in pixels */
545 unsigned total_height = tex->total_nblocksy * 4;
546 unsigned x = initial_offsets[face][0] * dim;
547 unsigned y = initial_offsets[face][1] * dim;
548 unsigned d = dim;
549
550 if (dim == 4 && face >= 4) {
551 x = (face - 4) * 8;
552 y = tex->total_nblocksy * 4 - 4; /* 4 = 1 block */
553 } else if (dim < 4 && (face > 0)) {
554 x = face * 8;
555 y = total_height - 4;
556 }
557
558 for (level = 0; level <= pt->last_level; level++) {
559 i915_texture_set_image_offset(tex, level, face,
560 util_format_get_nblocksx(pt->format, x),
561 util_format_get_nblocksy(pt->format, y));
562
563 d >>= 1;
564
565 switch (d) {
566 case 4:
567 switch (face) {
568 case PIPE_TEX_FACE_POS_X:
569 case PIPE_TEX_FACE_NEG_X:
570 x += step_offsets[face][0] * d;
571 y += step_offsets[face][1] * d;
572 break;
573 case PIPE_TEX_FACE_POS_Y:
574 case PIPE_TEX_FACE_NEG_Y:
575 y += 12;
576 x -= 8;
577 break;
578 case PIPE_TEX_FACE_POS_Z:
579 case PIPE_TEX_FACE_NEG_Z:
580 y = total_height - 4;
581 x = (face - 4) * 8;
582 break;
583 }
584 break;
585 case 2:
586 y = total_height - 4;
587 x = bottom_offsets[face];
588 break;
589 case 1:
590 x += 48;
591 break;
592 default:
593 x += step_offsets[face][0] * d;
594 y += step_offsets[face][1] * d;
595 break;
596 }
597 }
598 }
599 }
600
601 static boolean
602 i945_texture_layout(struct i915_texture * tex)
603 {
604 struct pipe_resource *pt = &tex->b.b;
605
606 switch (pt->target) {
607 case PIPE_TEXTURE_1D:
608 case PIPE_TEXTURE_2D:
609 case PIPE_TEXTURE_RECT:
610 if (!i9x5_special_layout(tex))
611 i945_texture_layout_2d(tex);
612 break;
613 case PIPE_TEXTURE_3D:
614 i945_texture_layout_3d(tex);
615 break;
616 case PIPE_TEXTURE_CUBE:
617 if (!util_format_is_s3tc(pt->format))
618 i9x5_texture_layout_cube(tex);
619 else
620 i945_texture_layout_cube(tex);
621 break;
622 default:
623 assert(0);
624 return FALSE;
625 }
626
627 return TRUE;
628 }
629
630
631
632 /*
633 * Screen texture functions
634 */
635
636
637
638 static boolean
639 i915_texture_get_handle(struct pipe_screen * screen,
640 struct pipe_resource *texture,
641 struct winsys_handle *whandle)
642 {
643 struct i915_screen *is = i915_screen(screen);
644 struct i915_texture *tex = i915_texture(texture);
645 struct i915_winsys *iws = is->iws;
646
647 return iws->buffer_get_handle(iws, tex->buffer, whandle, tex->stride);
648 }
649
650
651 static void
652 i915_texture_destroy(struct pipe_screen *screen,
653 struct pipe_resource *pt)
654 {
655 struct i915_texture *tex = i915_texture(pt);
656 struct i915_winsys *iws = i915_screen(screen)->iws;
657 uint i;
658
659 iws->buffer_destroy(iws, tex->buffer);
660
661 for (i = 0; i < Elements(tex->image_offset); i++)
662 if (tex->image_offset[i])
663 FREE(tex->image_offset[i]);
664
665 FREE(tex);
666 }
667
668 static struct pipe_transfer *
669 i915_texture_get_transfer(struct pipe_context *context,
670 struct pipe_resource *resource,
671 struct pipe_subresource sr,
672 unsigned usage,
673 const struct pipe_box *box)
674 {
675 struct i915_texture *tex = i915_texture(resource);
676 struct pipe_transfer *transfer = CALLOC_STRUCT(pipe_transfer);
677 if (transfer == NULL)
678 return NULL;
679
680 transfer->resource = resource;
681 transfer->sr = sr;
682 transfer->usage = usage;
683 transfer->box = *box;
684 transfer->stride = tex->stride;
685
686 return transfer;
687 }
688
689
690 static void *
691 i915_texture_transfer_map(struct pipe_context *pipe,
692 struct pipe_transfer *transfer)
693 {
694 struct pipe_resource *resource = transfer->resource;
695 struct i915_texture *tex = i915_texture(resource);
696 struct i915_winsys *iws = i915_screen(pipe->screen)->iws;
697 struct pipe_subresource sr = transfer->sr;
698 struct pipe_box *box = &transfer->box;
699 enum pipe_format format = resource->format;
700 unsigned offset;
701 char *map;
702
703 if (resource->target == PIPE_TEXTURE_CUBE) {
704 offset = tex->image_offset[sr.level][sr.face];
705 } else if (resource->target == PIPE_TEXTURE_3D) {
706 offset = tex->image_offset[sr.level][box->z];
707 } else {
708 offset = tex->image_offset[sr.level][0];
709 assert(sr.face == 0);
710 assert(box->z == 0);
711 }
712
713 map = iws->buffer_map(iws, tex->buffer,
714 (transfer->usage & PIPE_TRANSFER_WRITE) ? TRUE : FALSE);
715 if (map == NULL)
716 return NULL;
717
718 return map + offset +
719 box->y / util_format_get_blockheight(format) * transfer->stride +
720 box->x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
721 }
722
723 static void
724 i915_texture_transfer_unmap(struct pipe_context *pipe,
725 struct pipe_transfer *transfer)
726 {
727 struct i915_texture *tex = i915_texture(transfer->resource);
728 struct i915_winsys *iws = i915_screen(tex->b.b.screen)->iws;
729 iws->buffer_unmap(iws, tex->buffer);
730 }
731
732
733
734 struct u_resource_vtbl i915_texture_vtbl =
735 {
736 i915_texture_get_handle, /* get_handle */
737 i915_texture_destroy, /* resource_destroy */
738 NULL, /* is_resource_referenced */
739 i915_texture_get_transfer, /* get_transfer */
740 u_default_transfer_destroy, /* transfer_destroy */
741 i915_texture_transfer_map, /* transfer_map */
742 u_default_transfer_flush_region, /* transfer_flush_region */
743 i915_texture_transfer_unmap, /* transfer_unmap */
744 u_default_transfer_inline_write /* transfer_inline_write */
745 };
746
747
748
749
750 struct pipe_resource *
751 i915_texture_create(struct pipe_screen *screen,
752 const struct pipe_resource *template)
753 {
754 struct i915_screen *is = i915_screen(screen);
755 struct i915_winsys *iws = is->iws;
756 struct i915_texture *tex = CALLOC_STRUCT(i915_texture);
757 size_t tex_size;
758 unsigned buf_usage = 0;
759
760 if (!tex)
761 return NULL;
762
763 tex->b.b = *template;
764 tex->b.vtbl = &i915_texture_vtbl;
765 pipe_reference_init(&tex->b.b.reference, 1);
766 tex->b.b.screen = screen;
767
768 if (is->is_i945) {
769 if (!i945_texture_layout(tex))
770 goto fail;
771 } else {
772 if (!i915_texture_layout(tex))
773 goto fail;
774 }
775
776 tex_size = tex->stride * tex->total_nblocksy;
777
778 /* for scanouts and cursors, cursors arn't scanouts */
779
780 /* XXX: use a custom flag for cursors, don't rely on magically
781 * guessing that this is Xorg asking for a cursor
782 */
783 if ((template->bind & PIPE_BIND_SCANOUT) && template->width0 != 64)
784 buf_usage = I915_NEW_SCANOUT;
785 else
786 buf_usage = I915_NEW_TEXTURE;
787
788 tex->buffer = iws->buffer_create(iws, tex_size, 64, buf_usage);
789 if (!tex->buffer)
790 goto fail;
791
792 /* setup any hw fences */
793 if (tex->hw_tiled) {
794 assert(tex->sw_tiled == I915_TILE_NONE);
795 iws->buffer_set_fence_reg(iws, tex->buffer, tex->stride, tex->hw_tiled);
796 }
797
798
799 #if 0
800 void *ptr = ws->buffer_map(ws, tex->buffer,
801 PIPE_BUFFER_USAGE_CPU_WRITE);
802 memset(ptr, 0x80, tex_size);
803 ws->buffer_unmap(ws, tex->buffer);
804 #endif
805
806 I915_DBG(DBG_TEXTURE, "%s: %p size %u, stride %u, blocks (%u, %u)\n", __func__,
807 tex, (unsigned int)tex_size, tex->stride,
808 tex->stride / util_format_get_blocksize(tex->b.b.format),
809 tex->total_nblocksy);
810
811 return &tex->b.b;
812
813 fail:
814 FREE(tex);
815 return NULL;
816 }
817
818 struct pipe_resource *
819 i915_texture_from_handle(struct pipe_screen * screen,
820 const struct pipe_resource *template,
821 struct winsys_handle *whandle)
822 {
823 struct i915_screen *is = i915_screen(screen);
824 struct i915_texture *tex;
825 struct i915_winsys *iws = is->iws;
826 struct i915_winsys_buffer *buffer;
827 unsigned stride;
828
829 assert(screen);
830
831 buffer = iws->buffer_from_handle(iws, whandle, &stride);
832
833 /* Only supports one type */
834 if ((template->target != PIPE_TEXTURE_2D &&
835 template->target != PIPE_TEXTURE_RECT) ||
836 template->last_level != 0 ||
837 template->depth0 != 1) {
838 return NULL;
839 }
840
841 tex = CALLOC_STRUCT(i915_texture);
842 if (!tex)
843 return NULL;
844
845 tex->b.b = *template;
846 tex->b.vtbl = &i915_texture_vtbl;
847 pipe_reference_init(&tex->b.b.reference, 1);
848 tex->b.b.screen = screen;
849
850 tex->stride = stride;
851 tex->total_nblocksy = align_nblocksy(tex->b.b.format, tex->b.b.height0, 8);
852
853 i915_texture_set_level_info(tex, 0, 1);
854 i915_texture_set_image_offset(tex, 0, 0, 0, 0);
855
856 tex->buffer = buffer;
857
858 I915_DBG(DBG_TEXTURE, "%s: %p stride %u, blocks (%ux%u)\n", __func__,
859 tex, tex->stride,
860 tex->stride / util_format_get_blocksize(tex->b.b.format),
861 tex->total_nblocksy);
862
863 return &tex->b.b;
864 }
865