xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/i915/i915_resource_texture.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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 <[email protected]>
30  *   Michel Dänzer <[email protected]>
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 = pt->width0;
363    unsigned height = pt->height0;
364    unsigned nblocksy = 0;
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    nblocksy = align_nblocksy(pt->format, height, align_y);
372    tex->total_nblocksy = 0;
373 
374    for (level = 0; level <= pt->last_level; level++) {
375       i915_texture_set_level_info(tex, level, 1);
376       i915_texture_set_image_offset(tex, level, 0, 0, tex->total_nblocksy);
377 
378       tex->total_nblocksy += nblocksy;
379 
380       width = u_minify(width, 1);
381       height = u_minify(height, 1);
382       nblocksy = align_nblocksy(pt->format, height, align_y);
383    }
384 }
385 
386 static void
i915_texture_layout_3d(struct i915_texture * tex)387 i915_texture_layout_3d(struct i915_texture *tex)
388 {
389    struct pipe_resource *pt = &tex->b;
390    unsigned level;
391 
392    unsigned width = util_next_power_of_two(pt->width0);
393    unsigned height = util_next_power_of_two(pt->height0);
394    unsigned depth = util_next_power_of_two(pt->depth0);
395    unsigned nblocksy = util_format_get_nblocksy(pt->format, height);
396    unsigned stack_nblocksy = 0;
397 
398    /* Calculate the size of a single slice.
399     */
400    tex->stride = align(util_format_get_stride(pt->format, width), 4);
401 
402    /* XXX: hardware expects/requires 9 levels at minimum.
403     */
404    for (level = 0; level <= MAX2(8, pt->last_level); level++) {
405       i915_texture_set_level_info(tex, level, depth);
406 
407       stack_nblocksy += MAX2(2, nblocksy);
408 
409       width = u_minify(width, 1);
410       height = u_minify(height, 1);
411       nblocksy = util_format_get_nblocksy(pt->format, height);
412    }
413 
414    /* Fixup depth image_offsets:
415     */
416    for (level = 0; level <= pt->last_level; level++) {
417       unsigned i;
418       for (i = 0; i < depth; i++)
419          i915_texture_set_image_offset(tex, level, i, 0, i * stack_nblocksy);
420 
421       depth = u_minify(depth, 1);
422    }
423 
424    /* Multiply slice size by texture depth for total size.  It's
425     * remarkable how wasteful of memory the i915 texture layouts
426     * are.  They are largely fixed in the i945.
427     */
428    tex->total_nblocksy = stack_nblocksy * util_next_power_of_two(pt->depth0);
429 }
430 
431 static bool
i915_texture_layout(struct i915_texture * tex)432 i915_texture_layout(struct i915_texture *tex)
433 {
434    switch (tex->b.target) {
435    case PIPE_TEXTURE_1D:
436    case PIPE_TEXTURE_2D:
437    case PIPE_TEXTURE_RECT:
438       if (!i9x5_special_layout(tex))
439          i915_texture_layout_2d(tex);
440       break;
441    case PIPE_TEXTURE_3D:
442       i915_texture_layout_3d(tex);
443       break;
444    case PIPE_TEXTURE_CUBE:
445       i9x5_texture_layout_cube(tex);
446       break;
447    default:
448       assert(0);
449       return false;
450    }
451 
452    return true;
453 }
454 
455 /*
456  * i945 layout functions
457  */
458 
459 static void
i945_texture_layout_2d(struct i915_texture * tex)460 i945_texture_layout_2d(struct i915_texture *tex)
461 {
462    struct pipe_resource *pt = &tex->b;
463    int align_x = 4, align_y = 2;
464    unsigned level;
465    unsigned x = 0;
466    unsigned y = 0;
467    unsigned width = pt->width0;
468    unsigned height = pt->height0;
469    unsigned nblocksx = 0;
470    unsigned nblocksy = 0;
471 
472    if (util_format_is_compressed(pt->format)) {
473       align_x = 1;
474       align_y = 1;
475    }
476 
477    tex->stride = align(util_format_get_stride(pt->format, width), 4);
478    nblocksx = align_nblocksx(pt->format, width, align_x);
479    nblocksy = align_nblocksy(pt->format, height, align_y);
480 
481    /* Pitch must be a whole number of dwords
482     */
483    tex->stride = align(tex->stride, 64);
484    tex->total_nblocksy = 0;
485 
486    for (level = 0; level <= pt->last_level; level++) {
487       i915_texture_set_level_info(tex, level, 1);
488       i915_texture_set_image_offset(tex, level, 0, x, y);
489 
490       /* Because the images are packed better, the final offset
491        * might not be the maximal one:
492        */
493       tex->total_nblocksy = MAX2(tex->total_nblocksy, y + nblocksy);
494 
495       /* Layout_below: step right after second mipmap level.
496        */
497       if (level == 1) {
498          x += nblocksx;
499       } else {
500          y += nblocksy;
501       }
502 
503       width = u_minify(width, 1);
504       height = u_minify(height, 1);
505       nblocksx = align_nblocksx(pt->format, width, align_x);
506       nblocksy = align_nblocksy(pt->format, height, align_y);
507    }
508 }
509 
510 static void
i945_texture_layout_3d(struct i915_texture * tex)511 i945_texture_layout_3d(struct i915_texture *tex)
512 {
513    struct pipe_resource *pt = &tex->b;
514    unsigned width = util_next_power_of_two(pt->width0);
515    unsigned height = util_next_power_of_two(pt->height0);
516    unsigned depth = util_next_power_of_two(pt->depth0);
517    unsigned nblocksy = util_format_get_nblocksy(pt->format, height);
518    unsigned pack_x_pitch, pack_x_nr;
519    unsigned pack_y_pitch;
520    unsigned level;
521 
522    tex->stride = align(util_format_get_stride(pt->format, width), 4);
523    tex->total_nblocksy = 0;
524 
525    pack_y_pitch = MAX2(nblocksy, 2);
526    pack_x_pitch = tex->stride / util_format_get_blocksize(pt->format);
527    pack_x_nr = 1;
528 
529    for (level = 0; level <= pt->last_level; level++) {
530       int x = 0;
531       int y = 0;
532       unsigned q, j;
533 
534       i915_texture_set_level_info(tex, level, depth);
535 
536       for (q = 0; q < depth;) {
537          for (j = 0; j < pack_x_nr && q < depth; j++, q++) {
538             i915_texture_set_image_offset(tex, level, q, x,
539                                           y + tex->total_nblocksy);
540             x += pack_x_pitch;
541          }
542 
543          x = 0;
544          y += pack_y_pitch;
545       }
546 
547       tex->total_nblocksy += y;
548 
549       if (pack_x_pitch > 4) {
550          pack_x_pitch >>= 1;
551          pack_x_nr <<= 1;
552          assert(pack_x_pitch * pack_x_nr *
553                    util_format_get_blocksize(pt->format) <=
554                 tex->stride);
555       }
556 
557       if (pack_y_pitch > 2) {
558          pack_y_pitch >>= 1;
559       }
560 
561       width = u_minify(width, 1);
562       height = u_minify(height, 1);
563       depth = u_minify(depth, 1);
564       nblocksy = util_format_get_nblocksy(pt->format, height);
565    }
566 }
567 
568 /**
569  * Compressed cube texture map layout for i945 and later.
570  *
571  * The hardware layout looks like the 830-915 layout, except for the small
572  * sizes.  A zoomed in view of the layout for 945 is:
573  *
574  * +-------+-------+
575  * |  8x8  |  8x8  |
576  * |       |       |
577  * |       |       |
578  * |  +x   |  +y   |
579  * |       |       |
580  * |       |       |
581  * |       |       |
582  * |       |       |
583  * +---+---+-------+
584  * |4x4|   |  8x8  |
585  * | +x|   |       |
586  * |   |   |       |
587  * |   |   |       |
588  * +---+   |  +z   |
589  * |4x4|   |       |
590  * | +y|   |       |
591  * |   |   |       |
592  * +---+   +-------+
593  *
594  * ...
595  *
596  * +-------+-------+
597  * |  8x8  |  8x8  |
598  * |       |       |
599  * |       |       |
600  * |  -x   |  -y   |
601  * |       |       |
602  * |       |       |
603  * |       |       |
604  * |       |       |
605  * +---+---+-------+
606  * |4x4|   |  8x8  |
607  * | -x|   |       |
608  * |   |   |       |
609  * |   |   |       |
610  * +---+   |  -z   |
611  * |4x4|   |       |
612  * | -y|   |       |
613  * |   |   |       |
614  * +---+   +---+---+---+---+---+---+---+---+---+
615  * |4x4|   |4x4|   |2x2|   |2x2|   |2x2|   |2x2|
616  * | +z|   | -z|   | +x|   | +y|   | +z|   | -x| ...
617  * |   |   |   |   |   |   |   |   |   |   |   |
618  * +---+   +---+   +---+   +---+   +---+   +---+
619  *
620  * The bottom row continues with the remaining 2x2 then the 1x1 mip contents
621  * in order, with each of them aligned to a 8x8 block boundary.  Thus, for
622  * 32x32 cube maps and smaller, the bottom row layout is going to dictate the
623  * pitch of the tree.  For a tree with 4x4 images, the pitch is at least
624  * 14 * 8 = 112 texels, for 2x2 it is at least 12 * 8 texels, and for 1x1
625  * it is 6 * 8 texels.
626  */
627 static void
i945_texture_layout_cube(struct i915_texture * tex)628 i945_texture_layout_cube(struct i915_texture *tex)
629 {
630    struct pipe_resource *pt = &tex->b;
631    unsigned width = util_next_power_of_two(pt->width0);
632    const unsigned nblocks = util_format_get_nblocksx(pt->format, width);
633    const unsigned dim = width;
634    unsigned level;
635    unsigned face;
636 
637    assert(pt->width0 == pt->height0); /* cubemap images are square */
638    assert(util_format_is_compressed(pt->format)); /* compressed only */
639 
640    /*
641     * Depending on the size of the largest images, pitch can be
642     * determined either by the old-style packing of cubemap faces,
643     * or the final row of 4x4, 2x2 and 1x1 faces below this.
644     *
645     * 64  * 2 / 4 = 32
646     * 14 * 2 = 28
647     */
648    if (width >= 64)
649       tex->stride = nblocks * 2 * util_format_get_blocksize(pt->format);
650    else
651       tex->stride = 14 * 2 * util_format_get_blocksize(pt->format);
652 
653    /*
654     * Something similary apply for height as well.
655     */
656    if (width >= 4)
657       tex->total_nblocksy = nblocks * 4 + 1;
658    else
659       tex->total_nblocksy = 1;
660 
661    /* Set all the levels to effectively occupy the whole rectangular region */
662    for (level = 0; level <= pt->last_level; level++)
663       i915_texture_set_level_info(tex, level, 6);
664 
665    for (face = 0; face < 6; face++) {
666       /* all calculations in pixels */
667       unsigned total_height = tex->total_nblocksy * 4;
668       unsigned x = initial_offsets[face][0] * dim;
669       unsigned y = initial_offsets[face][1] * dim;
670       unsigned d = dim;
671 
672       if (dim == 4 && face >= 4) {
673          x = (face - 4) * 8;
674          y = tex->total_nblocksy * 4 - 4; /* 4 = 1 block */
675       } else if (dim < 4 && (face > 0)) {
676          x = face * 8;
677          y = total_height - 4;
678       }
679 
680       for (level = 0; level <= pt->last_level; level++) {
681          i915_texture_set_image_offset(tex, level, face,
682                                        util_format_get_nblocksx(pt->format, x),
683                                        util_format_get_nblocksy(pt->format, y));
684 
685          d >>= 1;
686 
687          switch (d) {
688          case 4:
689             switch (face) {
690             case PIPE_TEX_FACE_POS_X:
691             case PIPE_TEX_FACE_NEG_X:
692                x += step_offsets[face][0] * d;
693                y += step_offsets[face][1] * d;
694                break;
695             case PIPE_TEX_FACE_POS_Y:
696             case PIPE_TEX_FACE_NEG_Y:
697                y += 12;
698                x -= 8;
699                break;
700             case PIPE_TEX_FACE_POS_Z:
701             case PIPE_TEX_FACE_NEG_Z:
702                y = total_height - 4;
703                x = (face - 4) * 8;
704                break;
705             }
706             break;
707          case 2:
708             y = total_height - 4;
709             x = bottom_offsets[face];
710             break;
711          case 1:
712             x += 48;
713             break;
714          default:
715             x += step_offsets[face][0] * d;
716             y += step_offsets[face][1] * d;
717             break;
718          }
719       }
720    }
721 }
722 
723 static bool
i945_texture_layout(struct i915_texture * tex)724 i945_texture_layout(struct i915_texture *tex)
725 {
726    switch (tex->b.target) {
727    case PIPE_TEXTURE_1D:
728    case PIPE_TEXTURE_2D:
729    case PIPE_TEXTURE_RECT:
730       if (!i9x5_special_layout(tex))
731          i945_texture_layout_2d(tex);
732       break;
733    case PIPE_TEXTURE_3D:
734       i945_texture_layout_3d(tex);
735       break;
736    case PIPE_TEXTURE_CUBE:
737       if (!util_format_is_compressed(tex->b.format))
738          i9x5_texture_layout_cube(tex);
739       else
740          i945_texture_layout_cube(tex);
741       break;
742    default:
743       assert(0);
744       return false;
745    }
746 
747    return true;
748 }
749 
750 /*
751  * Screen texture functions
752  */
753 
754 bool
i915_resource_get_handle(struct pipe_screen * screen,struct pipe_context * context,struct pipe_resource * texture,struct winsys_handle * whandle,unsigned usage)755 i915_resource_get_handle(struct pipe_screen *screen,
756                          struct pipe_context *context,
757                          struct pipe_resource *texture,
758                          struct winsys_handle *whandle, unsigned usage)
759 {
760    if (texture->target == PIPE_BUFFER)
761       return false;
762 
763    struct i915_screen *is = i915_screen(screen);
764    struct i915_texture *tex = i915_texture(texture);
765    struct i915_winsys *iws = is->iws;
766 
767    return iws->buffer_get_handle(iws, tex->buffer, whandle, tex->stride);
768 }
769 
770 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)771 i915_texture_transfer_map(struct pipe_context *pipe,
772                           struct pipe_resource *resource, unsigned level,
773                           unsigned usage, const struct pipe_box *box,
774                           struct pipe_transfer **ptransfer)
775 {
776    struct i915_context *i915 = i915_context(pipe);
777    struct i915_texture *tex = i915_texture(resource);
778    struct i915_transfer *transfer = slab_alloc_st(&i915->texture_transfer_pool);
779    bool use_staging_texture = false;
780    struct i915_winsys *iws = i915_screen(pipe->screen)->iws;
781    enum pipe_format format = resource->format;
782    unsigned offset;
783    char *map;
784 
785    if (!transfer)
786       return NULL;
787 
788    transfer->b.resource = resource;
789    transfer->b.level = level;
790    transfer->b.usage = usage;
791    transfer->b.box = *box;
792    transfer->b.stride = tex->stride;
793    transfer->staging_texture = NULL;
794    /* XXX: handle depth textures everyhwere*/
795    transfer->b.layer_stride = 0;
796 
797    /* if we use staging transfers, only support textures we can render to,
798     * because we need that for u_blitter */
799    if (i915->blitter &&
800        util_blitter_is_copy_supported(i915->blitter, resource, resource) &&
801        (usage & PIPE_MAP_WRITE) &&
802        !(usage &
803          (PIPE_MAP_READ | PIPE_MAP_DONTBLOCK | PIPE_MAP_UNSYNCHRONIZED)))
804       use_staging_texture = true;
805 
806    use_staging_texture = false;
807 
808    if (use_staging_texture) {
809       /*
810        * Allocate the untiled staging texture.
811        * If the alloc fails, transfer->staging_texture is NULL and we fallback
812        * to a map()
813        */
814       transfer->staging_texture =
815          i915_texture_create(pipe->screen, resource, true);
816    }
817 
818    if (resource->target != PIPE_TEXTURE_3D &&
819        resource->target != PIPE_TEXTURE_CUBE) {
820       assert(box->z == 0);
821       assert(box->depth == 1);
822    }
823 
824    if (transfer->staging_texture) {
825       tex = i915_texture(transfer->staging_texture);
826    } else {
827       /* TODO this is a sledgehammer */
828       tex = i915_texture(resource);
829       pipe->flush(pipe, NULL, 0);
830    }
831 
832    offset = i915_texture_offset(tex, transfer->b.level, box->z);
833 
834    map = iws->buffer_map(iws, tex->buffer,
835                          (transfer->b.usage & PIPE_MAP_WRITE) ? true : false);
836    if (!map) {
837       pipe_resource_reference(&transfer->staging_texture, NULL);
838       FREE(transfer);
839       return NULL;
840    }
841 
842    *ptransfer = &transfer->b;
843 
844    return map + offset +
845           box->y / util_format_get_blockheight(format) * transfer->b.stride +
846           box->x / util_format_get_blockwidth(format) *
847              util_format_get_blocksize(format);
848 }
849 
850 void
i915_texture_transfer_unmap(struct pipe_context * pipe,struct pipe_transfer * transfer)851 i915_texture_transfer_unmap(struct pipe_context *pipe,
852                             struct pipe_transfer *transfer)
853 {
854    struct i915_context *i915 = i915_context(pipe);
855    struct i915_transfer *itransfer = (struct i915_transfer *)transfer;
856    struct i915_texture *tex = i915_texture(itransfer->b.resource);
857    struct i915_winsys *iws = i915_screen(tex->b.screen)->iws;
858 
859    if (itransfer->staging_texture)
860       tex = i915_texture(itransfer->staging_texture);
861 
862    iws->buffer_unmap(iws, tex->buffer);
863 
864    if ((itransfer->staging_texture) && (transfer->usage & PIPE_MAP_WRITE)) {
865       struct pipe_box sbox;
866 
867       u_box_origin_2d(itransfer->b.box.width, itransfer->b.box.height, &sbox);
868       pipe->resource_copy_region(pipe, itransfer->b.resource,
869                                  itransfer->b.level, itransfer->b.box.x,
870                                  itransfer->b.box.y, itransfer->b.box.z,
871                                  itransfer->staging_texture, 0, &sbox);
872       pipe->flush(pipe, NULL, 0);
873       pipe_resource_reference(&itransfer->staging_texture, NULL);
874    }
875 
876    slab_free_st(&i915->texture_transfer_pool, itransfer);
877 }
878 
879 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)880 i915_texture_subdata(struct pipe_context *pipe, struct pipe_resource *resource,
881                      unsigned level, unsigned usage, const struct pipe_box *box,
882                      const void *data, unsigned stride, uintptr_t layer_stride)
883 {
884    /* i915's cube and 3D maps are not laid out such that one could use a
885     * layer_stride to get from one layer to the next, so we have to walk the
886     * layers individually.
887     */
888    struct pipe_box layer_box = *box;
889    layer_box.depth = 1;
890    for (layer_box.z = box->z; layer_box.z < box->z + box->depth;
891         layer_box.z++) {
892       u_default_texture_subdata(pipe, resource, level, usage, &layer_box, data,
893                                 stride, layer_stride);
894       data += layer_stride;
895    }
896 }
897 
898 struct pipe_resource *
i915_texture_create(struct pipe_screen * screen,const struct pipe_resource * template,bool force_untiled)899 i915_texture_create(struct pipe_screen *screen,
900                     const struct pipe_resource *template, bool force_untiled)
901 {
902    struct i915_screen *is = i915_screen(screen);
903    struct i915_winsys *iws = is->iws;
904    struct i915_texture *tex = CALLOC_STRUCT(i915_texture);
905    unsigned buf_usage = 0;
906 
907    if (!tex)
908       return NULL;
909 
910    tex->b = *template;
911    pipe_reference_init(&tex->b.reference, 1);
912    tex->b.screen = screen;
913 
914    if ((force_untiled) || (template->usage == PIPE_USAGE_STREAM))
915       tex->tiling = I915_TILE_NONE;
916    else
917       tex->tiling = i915_texture_tiling(is, tex);
918 
919    if (is->is_i945) {
920       if (!i945_texture_layout(tex))
921          goto fail;
922    } else {
923       if (!i915_texture_layout(tex))
924          goto fail;
925    }
926 
927    /* for scanouts and cursors, cursors arn't scanouts */
928 
929    /* XXX: use a custom flag for cursors, don't rely on magically
930     * guessing that this is Xorg asking for a cursor
931     */
932    if ((template->bind & PIPE_BIND_SCANOUT) && template->width0 != 64)
933       buf_usage = I915_NEW_SCANOUT;
934    else
935       buf_usage = I915_NEW_TEXTURE;
936 
937    tex->buffer = iws->buffer_create_tiled(
938       iws, &tex->stride, tex->total_nblocksy, &tex->tiling, buf_usage);
939    if (!tex->buffer)
940       goto fail;
941 
942    I915_DBG(DBG_TEXTURE, "%s: %p stride %u, blocks (%u, %u) tiling %s\n",
943             __func__, tex, tex->stride,
944             tex->stride / util_format_get_blocksize(tex->b.format),
945             tex->total_nblocksy, get_tiling_string(tex->tiling));
946 
947    return &tex->b;
948 
949 fail:
950    FREE(tex);
951    return NULL;
952 }
953 
954 struct pipe_resource *
i915_texture_from_handle(struct pipe_screen * screen,const struct pipe_resource * template,struct winsys_handle * whandle)955 i915_texture_from_handle(struct pipe_screen *screen,
956                          const struct pipe_resource *template,
957                          struct winsys_handle *whandle)
958 {
959    struct i915_screen *is = i915_screen(screen);
960    struct i915_texture *tex;
961    struct i915_winsys *iws = is->iws;
962    struct i915_winsys_buffer *buffer;
963    unsigned stride;
964    enum i915_winsys_buffer_tile tiling;
965 
966    assert(screen);
967 
968    buffer = iws->buffer_from_handle(iws, whandle, template->height0, &tiling,
969                                     &stride);
970 
971    /* Only supports one type */
972    if ((template->target != PIPE_TEXTURE_2D &&
973         template->target != PIPE_TEXTURE_RECT) ||
974        template->last_level != 0 || template->depth0 != 1) {
975       return NULL;
976    }
977 
978    tex = CALLOC_STRUCT(i915_texture);
979    if (!tex)
980       return NULL;
981 
982    tex->b = *template;
983    pipe_reference_init(&tex->b.reference, 1);
984    tex->b.screen = screen;
985 
986    tex->stride = stride;
987    tex->tiling = tiling;
988    tex->total_nblocksy = align_nblocksy(tex->b.format, tex->b.height0, 8);
989 
990    i915_texture_set_level_info(tex, 0, 1);
991    i915_texture_set_image_offset(tex, 0, 0, 0, 0);
992 
993    tex->buffer = buffer;
994 
995    I915_DBG(DBG_TEXTURE, "%s: %p stride %u, blocks (%u, %u) tiling %s\n",
996             __func__, tex, tex->stride,
997             tex->stride / util_format_get_blocksize(tex->b.format),
998             tex->total_nblocksy, get_tiling_string(tex->tiling));
999 
1000    return &tex->b;
1001 }
1002