1 /**************************************************************************
2 *
3 * Copyright 2006 VMware, Inc.
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 VMWARE 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 <keithw@vmware.com>
30 * Michel Dänzer <daenzer@vmware.com>
31 */
32
33 #include "pipe/p_context.h"
34 #include "pipe/p_defines.h"
35 #include "pipe/p_state.h"
36 #include "util/format/u_format.h"
37 #include "util/u_inlines.h"
38 #include "util/u_math.h"
39 #include "util/u_memory.h"
40 #include "util/u_rect.h"
41
42 #include "i915_context.h"
43 #include "i915_debug.h"
44 #include "i915_resource.h"
45 #include "i915_screen.h"
46 #include "i915_winsys.h"
47
48 #define DEBUG_TEXTURES 0
49
50 /*
51 * Helper function and arrays
52 */
53
54 /**
55 * Initial offset for Cube map.
56 */
57 static const int initial_offsets[6][2] = {
58 [PIPE_TEX_FACE_POS_X] = {0, 0}, [PIPE_TEX_FACE_POS_Y] = {1, 0},
59 [PIPE_TEX_FACE_POS_Z] = {1, 1}, [PIPE_TEX_FACE_NEG_X] = {0, 2},
60 [PIPE_TEX_FACE_NEG_Y] = {1, 2}, [PIPE_TEX_FACE_NEG_Z] = {1, 3},
61 };
62
63 /**
64 * Step offsets for Cube map.
65 */
66 static const int step_offsets[6][2] = {
67 [PIPE_TEX_FACE_POS_X] = {0, 2}, [PIPE_TEX_FACE_POS_Y] = {-1, 2},
68 [PIPE_TEX_FACE_POS_Z] = {-1, 1}, [PIPE_TEX_FACE_NEG_X] = {0, 2},
69 [PIPE_TEX_FACE_NEG_Y] = {-1, 2}, [PIPE_TEX_FACE_NEG_Z] = {-1, 1},
70 };
71
72 /**
73 * For compressed level 2
74 */
75 static const int bottom_offsets[6] = {
76 [PIPE_TEX_FACE_POS_X] = 16 + 0 * 8, [PIPE_TEX_FACE_POS_Y] = 16 + 1 * 8,
77 [PIPE_TEX_FACE_POS_Z] = 16 + 2 * 8, [PIPE_TEX_FACE_NEG_X] = 16 + 3 * 8,
78 [PIPE_TEX_FACE_NEG_Y] = 16 + 4 * 8, [PIPE_TEX_FACE_NEG_Z] = 16 + 5 * 8,
79 };
80
81 static inline unsigned
align_nblocksx(enum pipe_format format,unsigned width,unsigned align_to)82 align_nblocksx(enum pipe_format format, unsigned width, unsigned align_to)
83 {
84 return align(util_format_get_nblocksx(format, width), align_to);
85 }
86
87 static inline unsigned
align_nblocksy(enum pipe_format format,unsigned width,unsigned align_to)88 align_nblocksy(enum pipe_format format, unsigned width, unsigned align_to)
89 {
90 return align(util_format_get_nblocksy(format, width), align_to);
91 }
92
93 static inline unsigned
get_pot_stride(enum pipe_format format,unsigned width)94 get_pot_stride(enum pipe_format format, unsigned width)
95 {
96 return util_next_power_of_two(util_format_get_stride(format, width));
97 }
98
99 static inline const char *
get_tiling_string(enum i915_winsys_buffer_tile tile)100 get_tiling_string(enum i915_winsys_buffer_tile tile)
101 {
102 switch (tile) {
103 case I915_TILE_NONE:
104 return "none";
105 case I915_TILE_X:
106 return "x";
107 case I915_TILE_Y:
108 return "y";
109 default:
110 assert(false);
111 return "?";
112 }
113 }
114
115 /*
116 * More advanced helper funcs
117 */
118
119 static void
i915_texture_set_level_info(struct i915_texture * tex,unsigned level,unsigned nr_images)120 i915_texture_set_level_info(struct i915_texture *tex, unsigned level,
121 unsigned nr_images)
122 {
123 assert(level < ARRAY_SIZE(tex->nr_images));
124 assert(nr_images);
125 assert(!tex->image_offset[level]);
126
127 tex->nr_images[level] = nr_images;
128 tex->image_offset[level] = MALLOC(nr_images * sizeof(struct offset_pair));
129 tex->image_offset[level][0].nblocksx = 0;
130 tex->image_offset[level][0].nblocksy = 0;
131 }
132
133 unsigned
i915_texture_offset(const struct i915_texture * tex,unsigned level,unsigned layer)134 i915_texture_offset(const struct i915_texture *tex, unsigned level,
135 unsigned layer)
136 {
137 unsigned x, y;
138 x = tex->image_offset[level][layer].nblocksx *
139 util_format_get_blocksize(tex->b.format);
140 y = tex->image_offset[level][layer].nblocksy;
141
142 return y * tex->stride + x;
143 }
144
145 static void
i915_texture_set_image_offset(struct i915_texture * tex,unsigned level,unsigned img,unsigned nblocksx,unsigned nblocksy)146 i915_texture_set_image_offset(struct i915_texture *tex, unsigned level,
147 unsigned img, unsigned nblocksx,
148 unsigned nblocksy)
149 {
150 /* for the first image and level make sure offset is zero */
151 assert(!(img == 0 && level == 0) || (nblocksx == 0 && nblocksy == 0));
152 assert(img < tex->nr_images[level]);
153
154 tex->image_offset[level][img].nblocksx = nblocksx;
155 tex->image_offset[level][img].nblocksy = nblocksy;
156
157 #if DEBUG_TEXTURES
158 debug_printf("%s: %p level %u, img %u (%u, %u)\n", __func__, tex, level, img,
159 x, y);
160 #endif
161 }
162
163 static enum i915_winsys_buffer_tile
i915_texture_tiling(struct i915_screen * is,struct i915_texture * tex)164 i915_texture_tiling(struct i915_screen *is, struct i915_texture *tex)
165 {
166 if (!is->debug.tiling)
167 return I915_TILE_NONE;
168
169 if (tex->b.target == PIPE_TEXTURE_1D)
170 return I915_TILE_NONE;
171
172 if (util_format_is_compressed(tex->b.format))
173 return I915_TILE_X;
174
175 if (is->debug.use_blitter)
176 return I915_TILE_X;
177 else
178 return I915_TILE_Y;
179 }
180
181 /*
182 * Shared layout functions
183 */
184
185 /**
186 * Special case to deal with scanout textures.
187 */
188 static bool
i9x5_scanout_layout(struct i915_texture * tex)189 i9x5_scanout_layout(struct i915_texture *tex)
190 {
191 struct pipe_resource *pt = &tex->b;
192
193 if (pt->last_level > 0 || util_format_get_blocksize(pt->format) != 4)
194 return false;
195
196 if (pt->width0 >= 240) {
197 tex->stride = align(util_format_get_stride(pt->format, pt->width0), 64);
198 tex->total_nblocksy = align_nblocksy(pt->format, pt->height0, 8);
199 tex->tiling = I915_TILE_X;
200 /* special case for cursors */
201 } else if (pt->width0 == 64 && pt->height0 == 64) {
202 tex->stride = get_pot_stride(pt->format, pt->width0);
203 tex->total_nblocksy = align_nblocksy(pt->format, pt->height0, 8);
204 } else {
205 return false;
206 }
207
208 i915_texture_set_level_info(tex, 0, 1);
209 i915_texture_set_image_offset(tex, 0, 0, 0, 0);
210
211 #if DEBUG_TEXTURES
212 debug_printf("%s size: %d,%d,%d offset %d,%d (0x%x)\n", __func__, pt->width0,
213 pt->height0, util_format_get_blocksize(pt->format), tex->stride,
214 tex->total_nblocksy, tex->stride * tex->total_nblocksy);
215 #endif
216
217 return true;
218 }
219
220 /**
221 * Special case to deal with shared textures.
222 */
223 static bool
i9x5_display_target_layout(struct i915_texture * tex)224 i9x5_display_target_layout(struct i915_texture *tex)
225 {
226 struct pipe_resource *pt = &tex->b;
227
228 if (pt->last_level > 0 || util_format_get_blocksize(pt->format) != 4)
229 return false;
230
231 /* fallback to normal textures for small textures */
232 if (pt->width0 < 240)
233 return false;
234
235 i915_texture_set_level_info(tex, 0, 1);
236 i915_texture_set_image_offset(tex, 0, 0, 0, 0);
237
238 tex->stride = align(util_format_get_stride(pt->format, pt->width0), 64);
239 tex->total_nblocksy = align_nblocksy(pt->format, pt->height0, 8);
240 tex->tiling = I915_TILE_X;
241
242 #if DEBUG_TEXTURES
243 debug_printf("%s size: %d,%d,%d offset %d,%d (0x%x)\n", __func__, pt->width0,
244 pt->height0, util_format_get_blocksize(pt->format), tex->stride,
245 tex->total_nblocksy, tex->stride * tex->total_nblocksy);
246 #endif
247
248 return true;
249 }
250
251 /**
252 * Helper function for special layouts
253 */
254 static bool
i9x5_special_layout(struct i915_texture * tex)255 i9x5_special_layout(struct i915_texture *tex)
256 {
257 struct pipe_resource *pt = &tex->b;
258
259 /* Scanouts needs special care */
260 if (pt->bind & PIPE_BIND_SCANOUT)
261 if (i9x5_scanout_layout(tex))
262 return true;
263
264 /* Shared buffers needs to be compatible with X servers
265 *
266 * XXX: need a better name than shared for this if it is to be part
267 * of core gallium, and probably move the flag to resource.flags,
268 * rather than bindings.
269 */
270 if (pt->bind & (PIPE_BIND_SHARED | PIPE_BIND_DISPLAY_TARGET))
271 if (i9x5_display_target_layout(tex))
272 return true;
273
274 return false;
275 }
276
277 /**
278 * Cube layout used on i915 and for non-compressed textures on i945.
279 *
280 * Hardware layout looks like:
281 *
282 * +-------+-------+
283 * | | |
284 * | | |
285 * | | |
286 * | +x | +y |
287 * | | |
288 * | | |
289 * | | |
290 * | | |
291 * +---+---+-------+
292 * | | | |
293 * | +x| +y| |
294 * | | | |
295 * | | | |
296 * +-+-+---+ +z |
297 * | | | | |
298 * +-+-+ +z| |
299 * | | | |
300 * +-+-+---+-------+
301 * | | |
302 * | | |
303 * | | |
304 * | -x | -y |
305 * | | |
306 * | | |
307 * | | |
308 * | | |
309 * +---+---+-------+
310 * | | | |
311 * | -x| -y| |
312 * | | | |
313 * | | | |
314 * +-+-+---+ -z |
315 * | | | | |
316 * +-+-+ -z| |
317 * | | | |
318 * +-+---+-------+
319 *
320 */
321 static void
i9x5_texture_layout_cube(struct i915_texture * tex)322 i9x5_texture_layout_cube(struct i915_texture *tex)
323 {
324 struct pipe_resource *pt = &tex->b;
325 unsigned width = util_next_power_of_two(pt->width0);
326 const unsigned nblocks = util_format_get_nblocksx(pt->format, width);
327 unsigned level;
328 unsigned face;
329
330 assert(pt->width0 == pt->height0); /* cubemap images are square */
331
332 /* double pitch for cube layouts */
333 tex->stride = align(nblocks * util_format_get_blocksize(pt->format) * 2, 4);
334 tex->total_nblocksy = nblocks * 4;
335
336 for (level = 0; level <= pt->last_level; level++)
337 i915_texture_set_level_info(tex, level, 6);
338
339 for (face = 0; face < 6; face++) {
340 unsigned x = initial_offsets[face][0] * nblocks;
341 unsigned y = initial_offsets[face][1] * nblocks;
342 unsigned d = nblocks;
343
344 for (level = 0; level <= pt->last_level; level++) {
345 i915_texture_set_image_offset(tex, level, face, x, y);
346 d >>= 1;
347 x += step_offsets[face][0] * d;
348 y += step_offsets[face][1] * d;
349 }
350 }
351 }
352
353 /*
354 * i915 layout functions
355 */
356
357 static void
i915_texture_layout_2d(struct i915_texture * tex)358 i915_texture_layout_2d(struct i915_texture *tex)
359 {
360 struct pipe_resource *pt = &tex->b;
361 unsigned level;
362 unsigned width = util_next_power_of_two(pt->width0);
363 unsigned height = util_next_power_of_two(pt->height0);
364 unsigned nblocksy = util_format_get_nblocksy(pt->format, width);
365 unsigned align_y = 2;
366
367 if (util_format_is_compressed(pt->format))
368 align_y = 1;
369
370 tex->stride = align(util_format_get_stride(pt->format, width), 4);
371 tex->total_nblocksy = 0;
372
373 for (level = 0; level <= pt->last_level; level++) {
374 i915_texture_set_level_info(tex, level, 1);
375 i915_texture_set_image_offset(tex, level, 0, 0, tex->total_nblocksy);
376
377 tex->total_nblocksy += nblocksy;
378
379 width = u_minify(width, 1);
380 height = u_minify(height, 1);
381 nblocksy = align_nblocksy(pt->format, height, align_y);
382 }
383 }
384
385 static void
i915_texture_layout_3d(struct i915_texture * tex)386 i915_texture_layout_3d(struct i915_texture *tex)
387 {
388 struct pipe_resource *pt = &tex->b;
389 unsigned level;
390
391 unsigned width = util_next_power_of_two(pt->width0);
392 unsigned height = util_next_power_of_two(pt->height0);
393 unsigned depth = util_next_power_of_two(pt->depth0);
394 unsigned nblocksy = util_format_get_nblocksy(pt->format, height);
395 unsigned stack_nblocksy = 0;
396
397 /* Calculate the size of a single slice.
398 */
399 tex->stride = align(util_format_get_stride(pt->format, width), 4);
400
401 /* XXX: hardware expects/requires 9 levels at minimum.
402 */
403 for (level = 0; level <= MAX2(8, pt->last_level); level++) {
404 i915_texture_set_level_info(tex, level, depth);
405
406 stack_nblocksy += MAX2(2, nblocksy);
407
408 width = u_minify(width, 1);
409 height = u_minify(height, 1);
410 nblocksy = util_format_get_nblocksy(pt->format, height);
411 }
412
413 /* Fixup depth image_offsets:
414 */
415 for (level = 0; level <= pt->last_level; level++) {
416 unsigned i;
417 for (i = 0; i < depth; i++)
418 i915_texture_set_image_offset(tex, level, i, 0, i * stack_nblocksy);
419
420 depth = u_minify(depth, 1);
421 }
422
423 /* Multiply slice size by texture depth for total size. It's
424 * remarkable how wasteful of memory the i915 texture layouts
425 * are. They are largely fixed in the i945.
426 */
427 tex->total_nblocksy = stack_nblocksy * util_next_power_of_two(pt->depth0);
428 }
429
430 static bool
i915_texture_layout(struct i915_texture * tex)431 i915_texture_layout(struct i915_texture *tex)
432 {
433 switch (tex->b.target) {
434 case PIPE_TEXTURE_1D:
435 case PIPE_TEXTURE_2D:
436 case PIPE_TEXTURE_RECT:
437 if (!i9x5_special_layout(tex))
438 i915_texture_layout_2d(tex);
439 break;
440 case PIPE_TEXTURE_3D:
441 i915_texture_layout_3d(tex);
442 break;
443 case PIPE_TEXTURE_CUBE:
444 i9x5_texture_layout_cube(tex);
445 break;
446 default:
447 assert(0);
448 return false;
449 }
450
451 return true;
452 }
453
454 /*
455 * i945 layout functions
456 */
457
458 static void
i945_texture_layout_2d(struct i915_texture * tex)459 i945_texture_layout_2d(struct i915_texture *tex)
460 {
461 struct pipe_resource *pt = &tex->b;
462 int align_x = 4, align_y = 2;
463 unsigned level;
464 unsigned x = 0;
465 unsigned y = 0;
466 unsigned width = util_next_power_of_two(pt->width0);
467 unsigned height = util_next_power_of_two(pt->height0);
468 unsigned nblocksx = util_format_get_nblocksx(pt->format, width);
469 unsigned nblocksy = util_format_get_nblocksy(pt->format, height);
470
471 if (util_format_is_compressed(pt->format)) {
472 align_x = 1;
473 align_y = 1;
474 }
475
476 tex->stride = align(util_format_get_stride(pt->format, width), 4);
477
478 /* May need to adjust pitch to accommodate the placement of
479 * the 2nd mipmap level. This occurs when the alignment
480 * constraints of mipmap placement push the right edge of the
481 * 2nd mipmap level out past the width of its parent.
482 */
483 if (pt->last_level > 0) {
484 unsigned mip1_nblocksx =
485 align_nblocksx(pt->format, u_minify(width, 1), align_x) +
486 util_format_get_nblocksx(pt->format, u_minify(width, 2));
487
488 if (mip1_nblocksx > nblocksx)
489 tex->stride = mip1_nblocksx * util_format_get_blocksize(pt->format);
490 }
491
492 /* Pitch must be a whole number of dwords
493 */
494 tex->stride = align(tex->stride, 64);
495 tex->total_nblocksy = 0;
496
497 for (level = 0; level <= pt->last_level; level++) {
498 i915_texture_set_level_info(tex, level, 1);
499 i915_texture_set_image_offset(tex, level, 0, x, y);
500
501 /* Because the images are packed better, the final offset
502 * might not be the maximal one:
503 */
504 tex->total_nblocksy = MAX2(tex->total_nblocksy, y + nblocksy);
505
506 /* Layout_below: step right after second mipmap level.
507 */
508 if (level == 1) {
509 x += nblocksx;
510 } else {
511 y += nblocksy;
512 }
513
514 width = u_minify(width, 1);
515 height = u_minify(height, 1);
516 nblocksx = align_nblocksx(pt->format, width, align_x);
517 nblocksy = align_nblocksy(pt->format, height, align_y);
518 }
519 }
520
521 static void
i945_texture_layout_3d(struct i915_texture * tex)522 i945_texture_layout_3d(struct i915_texture *tex)
523 {
524 struct pipe_resource *pt = &tex->b;
525 unsigned width = util_next_power_of_two(pt->width0);
526 unsigned height = util_next_power_of_two(pt->height0);
527 unsigned depth = util_next_power_of_two(pt->depth0);
528 unsigned nblocksy = util_format_get_nblocksy(pt->format, height);
529 unsigned pack_x_pitch, pack_x_nr;
530 unsigned pack_y_pitch;
531 unsigned level;
532
533 tex->stride = align(util_format_get_stride(pt->format, width), 4);
534 tex->total_nblocksy = 0;
535
536 pack_y_pitch = MAX2(nblocksy, 2);
537 pack_x_pitch = tex->stride / util_format_get_blocksize(pt->format);
538 pack_x_nr = 1;
539
540 for (level = 0; level <= pt->last_level; level++) {
541 int x = 0;
542 int y = 0;
543 unsigned q, j;
544
545 i915_texture_set_level_info(tex, level, depth);
546
547 for (q = 0; q < depth;) {
548 for (j = 0; j < pack_x_nr && q < depth; j++, q++) {
549 i915_texture_set_image_offset(tex, level, q, x,
550 y + tex->total_nblocksy);
551 x += pack_x_pitch;
552 }
553
554 x = 0;
555 y += pack_y_pitch;
556 }
557
558 tex->total_nblocksy += y;
559
560 if (pack_x_pitch > 4) {
561 pack_x_pitch >>= 1;
562 pack_x_nr <<= 1;
563 assert(pack_x_pitch * pack_x_nr *
564 util_format_get_blocksize(pt->format) <=
565 tex->stride);
566 }
567
568 if (pack_y_pitch > 2) {
569 pack_y_pitch >>= 1;
570 }
571
572 width = u_minify(width, 1);
573 height = u_minify(height, 1);
574 depth = u_minify(depth, 1);
575 nblocksy = util_format_get_nblocksy(pt->format, height);
576 }
577 }
578
579 /**
580 * Compressed cube texture map layout for i945 and later.
581 *
582 * The hardware layout looks like the 830-915 layout, except for the small
583 * sizes. A zoomed in view of the layout for 945 is:
584 *
585 * +-------+-------+
586 * | 8x8 | 8x8 |
587 * | | |
588 * | | |
589 * | +x | +y |
590 * | | |
591 * | | |
592 * | | |
593 * | | |
594 * +---+---+-------+
595 * |4x4| | 8x8 |
596 * | +x| | |
597 * | | | |
598 * | | | |
599 * +---+ | +z |
600 * |4x4| | |
601 * | +y| | |
602 * | | | |
603 * +---+ +-------+
604 *
605 * ...
606 *
607 * +-------+-------+
608 * | 8x8 | 8x8 |
609 * | | |
610 * | | |
611 * | -x | -y |
612 * | | |
613 * | | |
614 * | | |
615 * | | |
616 * +---+---+-------+
617 * |4x4| | 8x8 |
618 * | -x| | |
619 * | | | |
620 * | | | |
621 * +---+ | -z |
622 * |4x4| | |
623 * | -y| | |
624 * | | | |
625 * +---+ +---+---+---+---+---+---+---+---+---+
626 * |4x4| |4x4| |2x2| |2x2| |2x2| |2x2|
627 * | +z| | -z| | +x| | +y| | +z| | -x| ...
628 * | | | | | | | | | | | |
629 * +---+ +---+ +---+ +---+ +---+ +---+
630 *
631 * The bottom row continues with the remaining 2x2 then the 1x1 mip contents
632 * in order, with each of them aligned to a 8x8 block boundary. Thus, for
633 * 32x32 cube maps and smaller, the bottom row layout is going to dictate the
634 * pitch of the tree. For a tree with 4x4 images, the pitch is at least
635 * 14 * 8 = 112 texels, for 2x2 it is at least 12 * 8 texels, and for 1x1
636 * it is 6 * 8 texels.
637 */
638 static void
i945_texture_layout_cube(struct i915_texture * tex)639 i945_texture_layout_cube(struct i915_texture *tex)
640 {
641 struct pipe_resource *pt = &tex->b;
642 unsigned width = util_next_power_of_two(pt->width0);
643 const unsigned nblocks = util_format_get_nblocksx(pt->format, width);
644 const unsigned dim = width;
645 unsigned level;
646 unsigned face;
647
648 assert(pt->width0 == pt->height0); /* cubemap images are square */
649 assert(util_format_is_compressed(pt->format)); /* compressed only */
650
651 /*
652 * Depending on the size of the largest images, pitch can be
653 * determined either by the old-style packing of cubemap faces,
654 * or the final row of 4x4, 2x2 and 1x1 faces below this.
655 *
656 * 64 * 2 / 4 = 32
657 * 14 * 2 = 28
658 */
659 if (width >= 64)
660 tex->stride = nblocks * 2 * util_format_get_blocksize(pt->format);
661 else
662 tex->stride = 14 * 2 * util_format_get_blocksize(pt->format);
663
664 /*
665 * Something similary apply for height as well.
666 */
667 if (width >= 4)
668 tex->total_nblocksy = nblocks * 4 + 1;
669 else
670 tex->total_nblocksy = 1;
671
672 /* Set all the levels to effectively occupy the whole rectangular region */
673 for (level = 0; level <= pt->last_level; level++)
674 i915_texture_set_level_info(tex, level, 6);
675
676 for (face = 0; face < 6; face++) {
677 /* all calculations in pixels */
678 unsigned total_height = tex->total_nblocksy * 4;
679 unsigned x = initial_offsets[face][0] * dim;
680 unsigned y = initial_offsets[face][1] * dim;
681 unsigned d = dim;
682
683 if (dim == 4 && face >= 4) {
684 x = (face - 4) * 8;
685 y = tex->total_nblocksy * 4 - 4; /* 4 = 1 block */
686 } else if (dim < 4 && (face > 0)) {
687 x = face * 8;
688 y = total_height - 4;
689 }
690
691 for (level = 0; level <= pt->last_level; level++) {
692 i915_texture_set_image_offset(tex, level, face,
693 util_format_get_nblocksx(pt->format, x),
694 util_format_get_nblocksy(pt->format, y));
695
696 d >>= 1;
697
698 switch (d) {
699 case 4:
700 switch (face) {
701 case PIPE_TEX_FACE_POS_X:
702 case PIPE_TEX_FACE_NEG_X:
703 x += step_offsets[face][0] * d;
704 y += step_offsets[face][1] * d;
705 break;
706 case PIPE_TEX_FACE_POS_Y:
707 case PIPE_TEX_FACE_NEG_Y:
708 y += 12;
709 x -= 8;
710 break;
711 case PIPE_TEX_FACE_POS_Z:
712 case PIPE_TEX_FACE_NEG_Z:
713 y = total_height - 4;
714 x = (face - 4) * 8;
715 break;
716 }
717 break;
718 case 2:
719 y = total_height - 4;
720 x = bottom_offsets[face];
721 break;
722 case 1:
723 x += 48;
724 break;
725 default:
726 x += step_offsets[face][0] * d;
727 y += step_offsets[face][1] * d;
728 break;
729 }
730 }
731 }
732 }
733
734 static bool
i945_texture_layout(struct i915_texture * tex)735 i945_texture_layout(struct i915_texture *tex)
736 {
737 switch (tex->b.target) {
738 case PIPE_TEXTURE_1D:
739 case PIPE_TEXTURE_2D:
740 case PIPE_TEXTURE_RECT:
741 if (!i9x5_special_layout(tex))
742 i945_texture_layout_2d(tex);
743 break;
744 case PIPE_TEXTURE_3D:
745 i945_texture_layout_3d(tex);
746 break;
747 case PIPE_TEXTURE_CUBE:
748 if (!util_format_is_compressed(tex->b.format))
749 i9x5_texture_layout_cube(tex);
750 else
751 i945_texture_layout_cube(tex);
752 break;
753 default:
754 assert(0);
755 return false;
756 }
757
758 return true;
759 }
760
761 /*
762 * Screen texture functions
763 */
764
765 bool
i915_resource_get_handle(struct pipe_screen * screen,struct pipe_context * context,struct pipe_resource * texture,struct winsys_handle * whandle,unsigned usage)766 i915_resource_get_handle(struct pipe_screen *screen,
767 struct pipe_context *context,
768 struct pipe_resource *texture,
769 struct winsys_handle *whandle, unsigned usage)
770 {
771 if (texture->target == PIPE_BUFFER)
772 return false;
773
774 struct i915_screen *is = i915_screen(screen);
775 struct i915_texture *tex = i915_texture(texture);
776 struct i915_winsys *iws = is->iws;
777
778 return iws->buffer_get_handle(iws, tex->buffer, whandle, tex->stride);
779 }
780
781 void *
i915_texture_transfer_map(struct pipe_context * pipe,struct pipe_resource * resource,unsigned level,unsigned usage,const struct pipe_box * box,struct pipe_transfer ** ptransfer)782 i915_texture_transfer_map(struct pipe_context *pipe,
783 struct pipe_resource *resource, unsigned level,
784 unsigned usage, const struct pipe_box *box,
785 struct pipe_transfer **ptransfer)
786 {
787 struct i915_context *i915 = i915_context(pipe);
788 struct i915_texture *tex = i915_texture(resource);
789 struct i915_transfer *transfer = slab_alloc_st(&i915->texture_transfer_pool);
790 bool use_staging_texture = false;
791 struct i915_winsys *iws = i915_screen(pipe->screen)->iws;
792 enum pipe_format format = resource->format;
793 unsigned offset;
794 char *map;
795
796 if (!transfer)
797 return NULL;
798
799 transfer->b.resource = resource;
800 transfer->b.level = level;
801 transfer->b.usage = usage;
802 transfer->b.box = *box;
803 transfer->b.stride = tex->stride;
804 transfer->staging_texture = NULL;
805 /* XXX: handle depth textures everyhwere*/
806 transfer->b.layer_stride = 0;
807
808 /* if we use staging transfers, only support textures we can render to,
809 * because we need that for u_blitter */
810 if (i915->blitter &&
811 util_blitter_is_copy_supported(i915->blitter, resource, resource) &&
812 (usage & PIPE_MAP_WRITE) &&
813 !(usage &
814 (PIPE_MAP_READ | PIPE_MAP_DONTBLOCK | PIPE_MAP_UNSYNCHRONIZED)))
815 use_staging_texture = true;
816
817 use_staging_texture = false;
818
819 if (use_staging_texture) {
820 /*
821 * Allocate the untiled staging texture.
822 * If the alloc fails, transfer->staging_texture is NULL and we fallback
823 * to a map()
824 */
825 transfer->staging_texture =
826 i915_texture_create(pipe->screen, resource, true);
827 }
828
829 if (resource->target != PIPE_TEXTURE_3D &&
830 resource->target != PIPE_TEXTURE_CUBE) {
831 assert(box->z == 0);
832 assert(box->depth == 1);
833 }
834
835 if (transfer->staging_texture) {
836 tex = i915_texture(transfer->staging_texture);
837 } else {
838 /* TODO this is a sledgehammer */
839 tex = i915_texture(resource);
840 pipe->flush(pipe, NULL, 0);
841 }
842
843 offset = i915_texture_offset(tex, transfer->b.level, box->z);
844
845 map = iws->buffer_map(iws, tex->buffer,
846 (transfer->b.usage & PIPE_MAP_WRITE) ? true : false);
847 if (!map) {
848 pipe_resource_reference(&transfer->staging_texture, NULL);
849 FREE(transfer);
850 return NULL;
851 }
852
853 *ptransfer = &transfer->b;
854
855 return map + offset +
856 box->y / util_format_get_blockheight(format) * transfer->b.stride +
857 box->x / util_format_get_blockwidth(format) *
858 util_format_get_blocksize(format);
859 }
860
861 void
i915_texture_transfer_unmap(struct pipe_context * pipe,struct pipe_transfer * transfer)862 i915_texture_transfer_unmap(struct pipe_context *pipe,
863 struct pipe_transfer *transfer)
864 {
865 struct i915_context *i915 = i915_context(pipe);
866 struct i915_transfer *itransfer = (struct i915_transfer *)transfer;
867 struct i915_texture *tex = i915_texture(itransfer->b.resource);
868 struct i915_winsys *iws = i915_screen(tex->b.screen)->iws;
869
870 if (itransfer->staging_texture)
871 tex = i915_texture(itransfer->staging_texture);
872
873 iws->buffer_unmap(iws, tex->buffer);
874
875 if ((itransfer->staging_texture) && (transfer->usage & PIPE_MAP_WRITE)) {
876 struct pipe_box sbox;
877
878 u_box_origin_2d(itransfer->b.box.width, itransfer->b.box.height, &sbox);
879 pipe->resource_copy_region(pipe, itransfer->b.resource,
880 itransfer->b.level, itransfer->b.box.x,
881 itransfer->b.box.y, itransfer->b.box.z,
882 itransfer->staging_texture, 0, &sbox);
883 pipe->flush(pipe, NULL, 0);
884 pipe_resource_reference(&itransfer->staging_texture, NULL);
885 }
886
887 slab_free_st(&i915->texture_transfer_pool, itransfer);
888 }
889
890 void
i915_texture_subdata(struct pipe_context * pipe,struct pipe_resource * resource,unsigned level,unsigned usage,const struct pipe_box * box,const void * data,unsigned stride,uintptr_t layer_stride)891 i915_texture_subdata(struct pipe_context *pipe, struct pipe_resource *resource,
892 unsigned level, unsigned usage, const struct pipe_box *box,
893 const void *data, unsigned stride, uintptr_t layer_stride)
894 {
895 /* i915's cube and 3D maps are not laid out such that one could use a
896 * layer_stride to get from one layer to the next, so we have to walk the
897 * layers individually.
898 */
899 struct pipe_box layer_box = *box;
900 layer_box.depth = 1;
901 for (layer_box.z = box->z; layer_box.z < box->z + box->depth;
902 layer_box.z++) {
903 u_default_texture_subdata(pipe, resource, level, usage, &layer_box, data,
904 stride, layer_stride);
905 data += layer_stride;
906 }
907 }
908
909 struct pipe_resource *
i915_texture_create(struct pipe_screen * screen,const struct pipe_resource * template,bool force_untiled)910 i915_texture_create(struct pipe_screen *screen,
911 const struct pipe_resource *template, bool force_untiled)
912 {
913 struct i915_screen *is = i915_screen(screen);
914 struct i915_winsys *iws = is->iws;
915 struct i915_texture *tex = CALLOC_STRUCT(i915_texture);
916 unsigned buf_usage = 0;
917
918 if (!tex)
919 return NULL;
920
921 tex->b = *template;
922 pipe_reference_init(&tex->b.reference, 1);
923 tex->b.screen = screen;
924
925 if ((force_untiled) || (template->usage == PIPE_USAGE_STREAM))
926 tex->tiling = I915_TILE_NONE;
927 else
928 tex->tiling = i915_texture_tiling(is, tex);
929
930 if (is->is_i945) {
931 if (!i945_texture_layout(tex))
932 goto fail;
933 } else {
934 if (!i915_texture_layout(tex))
935 goto fail;
936 }
937
938 /* for scanouts and cursors, cursors arn't scanouts */
939
940 /* XXX: use a custom flag for cursors, don't rely on magically
941 * guessing that this is Xorg asking for a cursor
942 */
943 if ((template->bind & PIPE_BIND_SCANOUT) && template->width0 != 64)
944 buf_usage = I915_NEW_SCANOUT;
945 else
946 buf_usage = I915_NEW_TEXTURE;
947
948 tex->buffer = iws->buffer_create_tiled(
949 iws, &tex->stride, tex->total_nblocksy, &tex->tiling, buf_usage);
950 if (!tex->buffer)
951 goto fail;
952
953 I915_DBG(DBG_TEXTURE, "%s: %p stride %u, blocks (%u, %u) tiling %s\n",
954 __func__, tex, tex->stride,
955 tex->stride / util_format_get_blocksize(tex->b.format),
956 tex->total_nblocksy, get_tiling_string(tex->tiling));
957
958 return &tex->b;
959
960 fail:
961 FREE(tex);
962 return NULL;
963 }
964
965 struct pipe_resource *
i915_texture_from_handle(struct pipe_screen * screen,const struct pipe_resource * template,struct winsys_handle * whandle)966 i915_texture_from_handle(struct pipe_screen *screen,
967 const struct pipe_resource *template,
968 struct winsys_handle *whandle)
969 {
970 struct i915_screen *is = i915_screen(screen);
971 struct i915_texture *tex;
972 struct i915_winsys *iws = is->iws;
973 struct i915_winsys_buffer *buffer;
974 unsigned stride;
975 enum i915_winsys_buffer_tile tiling;
976
977 assert(screen);
978
979 buffer = iws->buffer_from_handle(iws, whandle, template->height0, &tiling,
980 &stride);
981
982 /* Only supports one type */
983 if ((template->target != PIPE_TEXTURE_2D &&
984 template->target != PIPE_TEXTURE_RECT) ||
985 template->last_level != 0 || template->depth0 != 1) {
986 return NULL;
987 }
988
989 tex = CALLOC_STRUCT(i915_texture);
990 if (!tex)
991 return NULL;
992
993 tex->b = *template;
994 pipe_reference_init(&tex->b.reference, 1);
995 tex->b.screen = screen;
996
997 tex->stride = stride;
998 tex->tiling = tiling;
999 tex->total_nblocksy = align_nblocksy(tex->b.format, tex->b.height0, 8);
1000
1001 i915_texture_set_level_info(tex, 0, 1);
1002 i915_texture_set_image_offset(tex, 0, 0, 0, 0);
1003
1004 tex->buffer = buffer;
1005
1006 I915_DBG(DBG_TEXTURE, "%s: %p stride %u, blocks (%u, %u) tiling %s\n",
1007 __func__, tex, tex->stride,
1008 tex->stride / util_format_get_blocksize(tex->b.format),
1009 tex->total_nblocksy, get_tiling_string(tex->tiling));
1010
1011 return &tex->b;
1012 }
1013