2 * Mesa 3-D graphics library
4 * Copyright (C) 2012-2013 LunarG, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 * Chia-I Wu <olv@lunarg.com>
28 #include "ilo_screen.h"
29 #include "ilo_resource.h"
31 /* use PIPE_BIND_CUSTOM to indicate MCS */
32 #define ILO_BIND_MCS PIPE_BIND_CUSTOM
35 const struct ilo_dev_info
*dev
;
36 const struct pipe_resource
*templ
;
38 bool has_depth
, has_stencil
;
39 bool hiz
, separate_stencil
;
41 enum pipe_format format
;
42 unsigned block_width
, block_height
, block_size
;
45 enum intel_tiling_mode tiling
;
48 bool array_spacing_full
;
53 struct ilo_texture_slice
*slices
;
54 } levels
[PIPE_MAX_TEXTURE_LEVELS
];
61 int bo_stride
, bo_height
;
62 int hiz_stride
, hiz_height
;
66 * We do not know if we will fail until we try to allocate the bo.
67 * So just set a limit on the texture size.
69 static const size_t max_resource_size
= 1u << 30;
72 tex_layout_init_qpitch(struct tex_layout
*layout
)
74 const struct pipe_resource
*templ
= layout
->templ
;
77 if (templ
->array_size
<= 1)
80 h0
= align(layout
->levels
[0].h
, layout
->align_j
);
82 if (!layout
->array_spacing_full
) {
87 h1
= align(layout
->levels
[1].h
, layout
->align_j
);
90 * From the Sandy Bridge PRM, volume 1 part 1, page 115:
92 * "The following equation is used for surface formats other than
93 * compressed textures:
95 * QPitch = (h0 + h1 + 11j)"
97 * "The equation for compressed textures (BC* and FXT1 surface formats)
100 * QPitch = (h0 + h1 + 11j) / 4"
102 * "[DevSNB] Errata: Sampler MSAA Qpitch will be 4 greater than the
103 * value calculated in the equation above, for every other odd Surface
104 * Height starting from 1 i.e. 1,5,9,13"
106 * From the Ivy Bridge PRM, volume 1 part 1, page 111-112:
108 * "If Surface Array Spacing is set to ARYSPC_FULL (note that the depth
109 * buffer and stencil buffer have an implied value of ARYSPC_FULL):
111 * QPitch = (h0 + h1 + 12j)
112 * QPitch = (h0 + h1 + 12j) / 4 (compressed)
114 * (There are many typos or missing words here...)"
116 * To access the N-th slice, an offset of (Stride * QPitch * N) is added to
117 * the base address. The PRM divides QPitch by 4 for compressed formats
118 * because the block height for those formats are 4, and it wants QPitch to
119 * mean the number of memory rows, as opposed to texel rows, between
120 * slices. Since we use texel rows in tex->slice_offsets, we do not need
121 * to divide QPitch by 4.
123 layout
->qpitch
= h0
+ h1
+
124 ((layout
->dev
->gen
>= ILO_GEN(7)) ? 12 : 11) * layout
->align_j
;
126 if (layout
->dev
->gen
== ILO_GEN(6) && templ
->nr_samples
> 1 &&
127 templ
->height0
% 4 == 1)
132 tex_layout_init_alignments(struct tex_layout
*layout
)
134 const struct pipe_resource
*templ
= layout
->templ
;
137 * From the Sandy Bridge PRM, volume 1 part 1, page 113:
139 * "surface format align_i align_j
140 * YUV 4:2:2 formats 4 *see below
143 * all other formats 4 *see below"
145 * "- align_j = 4 for any depth buffer
146 * - align_j = 2 for separate stencil buffer
147 * - align_j = 4 for any render target surface is multisampled (4x)
148 * - align_j = 4 for any render target surface with Surface Vertical
149 * Alignment = VALIGN_4
150 * - align_j = 2 for any render target surface with Surface Vertical
151 * Alignment = VALIGN_2
152 * - align_j = 2 for all other render target surface
153 * - align_j = 2 for any sampling engine surface with Surface Vertical
154 * Alignment = VALIGN_2
155 * - align_j = 4 for any sampling engine surface with Surface Vertical
156 * Alignment = VALIGN_4"
158 * From the Sandy Bridge PRM, volume 4 part 1, page 86:
160 * "This field (Surface Vertical Alignment) must be set to VALIGN_2 if
161 * the Surface Format is 96 bits per element (BPE)."
163 * They can be rephrased as
166 * compressed formats block width block height
167 * PIPE_FORMAT_S8_UINT 4 2
168 * other depth/stencil formats 4 4
169 * 4x multisampled 4 4
175 * From the Ivy Bridge PRM, volume 1 part 1, page 110:
177 * "surface defined by surface format align_i align_j
178 * 3DSTATE_DEPTH_BUFFER D16_UNORM 8 4
180 * 3DSTATE_STENCIL_BUFFER N/A 8 8
181 * SURFACE_STATE BC*, ETC*, EAC* 4 4
183 * all others (set by SURFACE_STATE)"
185 * From the Ivy Bridge PRM, volume 4 part 1, page 63:
187 * "- This field (Surface Vertical Aligment) is intended to be set to
188 * VALIGN_4 if the surface was rendered as a depth buffer, for a
189 * multisampled (4x) render target, or for a multisampled (8x)
190 * render target, since these surfaces support only alignment of 4.
191 * - Use of VALIGN_4 for other surfaces is supported, but uses more
193 * - This field must be set to VALIGN_4 for all tiled Y Render Target
195 * - Value of 1 is not supported for format YCRCB_NORMAL (0x182),
196 * YCRCB_SWAPUVY (0x183), YCRCB_SWAPUV (0x18f), YCRCB_SWAPY (0x190)
197 * - If Number of Multisamples is not MULTISAMPLECOUNT_1, this field
198 * must be set to VALIGN_4."
199 * - VALIGN_4 is not supported for surface format R32G32B32_FLOAT."
201 * "- This field (Surface Horizontal Aligment) is intended to be set to
202 * HALIGN_8 only if the surface was rendered as a depth buffer with
203 * Z16 format or a stencil buffer, since these surfaces support only
205 * - Use of HALIGN_8 for other surfaces is supported, but uses more
207 * - This field must be set to HALIGN_4 if the Surface Format is BC*.
208 * - This field must be set to HALIGN_8 if the Surface Format is
211 * They can be rephrased as
214 * compressed formats block width block height
215 * PIPE_FORMAT_Z16_UNORM 8 4
216 * PIPE_FORMAT_S8_UINT 8 8
217 * other depth/stencil formats 4 or 8 4
218 * 2x or 4x multisampled 4 or 8 4
219 * tiled Y 4 or 8 4 (if rt)
220 * PIPE_FORMAT_R32G32B32_FLOAT 4 or 8 2
221 * others 4 or 8 2 or 4
224 if (layout
->compressed
) {
225 /* this happens to be the case */
226 layout
->align_i
= layout
->block_width
;
227 layout
->align_j
= layout
->block_height
;
229 else if (layout
->has_depth
|| layout
->has_stencil
) {
230 if (layout
->dev
->gen
>= ILO_GEN(7)) {
231 switch (layout
->format
) {
232 case PIPE_FORMAT_Z16_UNORM
:
236 case PIPE_FORMAT_S8_UINT
:
247 switch (layout
->format
) {
248 case PIPE_FORMAT_S8_UINT
:
260 const bool valign_4
= (templ
->nr_samples
> 1) ||
261 (layout
->dev
->gen
>= ILO_GEN(7) &&
262 layout
->tiling
== INTEL_TILING_Y
&&
263 (templ
->bind
& PIPE_BIND_RENDER_TARGET
));
266 assert(layout
->block_size
!= 12);
269 layout
->align_j
= (valign_4
) ? 4 : 2;
273 * the fact that align i and j are multiples of block width and height
274 * respectively is what makes the size of the bo a multiple of the block
275 * size, slices start at block boundaries, and many of the computations
278 assert(layout
->align_i
% layout
->block_width
== 0);
279 assert(layout
->align_j
% layout
->block_height
== 0);
281 /* make sure align() works */
282 assert(util_is_power_of_two(layout
->align_i
) &&
283 util_is_power_of_two(layout
->align_j
));
284 assert(util_is_power_of_two(layout
->block_width
) &&
285 util_is_power_of_two(layout
->block_height
));
289 tex_layout_init_levels(struct tex_layout
*layout
)
291 const struct pipe_resource
*templ
= layout
->templ
;
294 last_level
= templ
->last_level
;
296 /* need at least 2 levels to compute full qpitch */
297 if (last_level
== 0 && templ
->array_size
> 1 && layout
->array_spacing_full
)
300 /* compute mip level sizes */
301 for (lv
= 0; lv
<= last_level
; lv
++) {
304 w
= u_minify(templ
->width0
, lv
);
305 h
= u_minify(templ
->height0
, lv
);
306 d
= u_minify(templ
->depth0
, lv
);
309 * From the Sandy Bridge PRM, volume 1 part 1, page 114:
311 * "The dimensions of the mip maps are first determined by applying
312 * the sizing algorithm presented in Non-Power-of-Two Mipmaps
313 * above. Then, if necessary, they are padded out to compression
316 w
= align(w
, layout
->block_width
);
317 h
= align(h
, layout
->block_height
);
320 * From the Sandy Bridge PRM, volume 1 part 1, page 111:
322 * "If the surface is multisampled (4x), these values must be
323 * adjusted as follows before proceeding:
325 * W_L = ceiling(W_L / 2) * 4
326 * H_L = ceiling(H_L / 2) * 4"
328 * From the Ivy Bridge PRM, volume 1 part 1, page 108:
330 * "If the surface is multisampled and it is a depth or stencil
331 * surface or Multisampled Surface StorageFormat in SURFACE_STATE
332 * is MSFMT_DEPTH_STENCIL, W_L and H_L must be adjusted as follows
335 * #samples W_L = H_L =
336 * 2 ceiling(W_L / 2) * 4 HL [no adjustment]
337 * 4 ceiling(W_L / 2) * 4 ceiling(H_L / 2) * 4
338 * 8 ceiling(W_L / 2) * 8 ceiling(H_L / 2) * 4
339 * 16 ceiling(W_L / 2) * 8 ceiling(H_L / 2) * 8"
341 * For interleaved samples (4x), where pixels
344 * (x, y+1) (x+1, y+1)
346 * would be is occupied by
348 * (x, y , si0) (x+1, y , si0) (x, y , si1) (x+1, y , si1)
349 * (x, y+1, si0) (x+1, y+1, si0) (x, y+1, si1) (x+1, y+1, si1)
350 * (x, y , si2) (x+1, y , si2) (x, y , si3) (x+1, y , si3)
351 * (x, y+1, si2) (x+1, y+1, si2) (x, y+1, si3) (x+1, y+1, si3)
355 * w = align(w, 2) * 2;
356 * y = align(y, 2) * 2;
358 if (layout
->interleaved
) {
359 switch (templ
->nr_samples
) {
379 assert(!"unsupported sample count");
384 layout
->levels
[lv
].w
= w
;
385 layout
->levels
[lv
].h
= h
;
386 layout
->levels
[lv
].d
= d
;
391 tex_layout_init_spacing(struct tex_layout
*layout
)
393 const struct pipe_resource
*templ
= layout
->templ
;
395 if (layout
->dev
->gen
>= ILO_GEN(7)) {
397 * It is not explicitly states, but render targets are expected to be
398 * UMS/CMS (samples non-interleaved) and depth/stencil buffers are
399 * expected to be IMS (samples interleaved).
401 * See "Multisampled Surface Storage Format" field of SURFACE_STATE.
403 if (layout
->has_depth
|| layout
->has_stencil
) {
404 layout
->interleaved
= true;
407 * From the Ivy Bridge PRM, volume 1 part 1, page 111:
409 * "note that the depth buffer and stencil buffer have an implied
410 * value of ARYSPC_FULL"
412 layout
->array_spacing_full
= true;
415 layout
->interleaved
= false;
418 * From the Ivy Bridge PRM, volume 4 part 1, page 66:
420 * "If Multisampled Surface Storage Format is MSFMT_MSS and
421 * Number of Multisamples is not MULTISAMPLECOUNT_1, this field
422 * (Surface Array Spacing) must be set to ARYSPC_LOD0."
424 * As multisampled resources are not mipmapped, we never use
425 * ARYSPC_FULL for them.
427 if (templ
->nr_samples
> 1)
428 assert(templ
->last_level
== 0);
429 layout
->array_spacing_full
= (templ
->last_level
> 0);
433 /* GEN6 supports only interleaved samples */
434 layout
->interleaved
= true;
437 * From the Sandy Bridge PRM, volume 1 part 1, page 115:
439 * "The separate stencil buffer does not support mip mapping, thus
440 * the storage for LODs other than LOD 0 is not needed. The
441 * following QPitch equation applies only to the separate stencil
446 * GEN6 does not support compact spacing otherwise.
448 layout
->array_spacing_full
= (layout
->format
!= PIPE_FORMAT_S8_UINT
);
453 tex_layout_init_tiling(struct tex_layout
*layout
)
455 const struct pipe_resource
*templ
= layout
->templ
;
456 const enum pipe_format format
= layout
->format
;
457 const unsigned tile_none
= 1 << INTEL_TILING_NONE
;
458 const unsigned tile_x
= 1 << INTEL_TILING_X
;
459 const unsigned tile_y
= 1 << INTEL_TILING_Y
;
460 unsigned valid_tilings
= tile_none
| tile_x
| tile_y
;
463 * From the Sandy Bridge PRM, volume 1 part 2, page 32:
465 * "Display/Overlay Y-Major not supported.
466 * X-Major required for Async Flips"
468 if (unlikely(templ
->bind
& PIPE_BIND_SCANOUT
))
469 valid_tilings
&= tile_x
;
472 * From the Sandy Bridge PRM, volume 3 part 2, page 158:
474 * "The cursor surface address must be 4K byte aligned. The cursor must
475 * be in linear memory, it cannot be tiled."
477 if (unlikely(templ
->bind
& (PIPE_BIND_CURSOR
| PIPE_BIND_LINEAR
)))
478 valid_tilings
&= tile_none
;
481 * From the Ivy Bridge PRM, volume 4 part 1, page 76:
483 * "The MCS surface must be stored as Tile Y."
485 if (templ
->bind
& ILO_BIND_MCS
)
486 valid_tilings
&= tile_y
;
489 * From the Sandy Bridge PRM, volume 2 part 1, page 318:
491 * "[DevSNB+]: This field (Tiled Surface) must be set to TRUE. Linear
492 * Depth Buffer is not supported."
494 * "The Depth Buffer, if tiled, must use Y-Major tiling."
496 * From the Sandy Bridge PRM, volume 1 part 2, page 22:
498 * "W-Major Tile Format is used for separate stencil."
500 * Since the HW does not support W-tiled fencing, we have to do it in the
503 if (templ
->bind
& PIPE_BIND_DEPTH_STENCIL
) {
505 case PIPE_FORMAT_S8_UINT
:
506 valid_tilings
&= tile_none
;
509 valid_tilings
&= tile_y
;
514 if (templ
->bind
& (PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
)) {
515 if (templ
->bind
& PIPE_BIND_RENDER_TARGET
) {
517 * From the Sandy Bridge PRM, volume 1 part 2, page 32:
519 * "NOTE: 128BPE Format Color buffer ( render target ) MUST be
520 * either TileX or Linear."
522 if (layout
->block_size
== 16)
523 valid_tilings
&= ~tile_y
;
526 * From the Ivy Bridge PRM, volume 4 part 1, page 63:
528 * "This field (Surface Vertical Aligment) must be set to
529 * VALIGN_4 for all tiled Y Render Target surfaces."
531 * "VALIGN_4 is not supported for surface format
534 if (layout
->dev
->gen
>= ILO_GEN(7) && layout
->block_size
== 12)
535 valid_tilings
&= ~tile_y
;
539 * Also, heuristically set a minimum width/height for enabling tiling.
541 if (templ
->width0
< 64 && (valid_tilings
& ~tile_x
))
542 valid_tilings
&= ~tile_x
;
544 if ((templ
->width0
< 32 || templ
->height0
< 16) &&
545 (templ
->width0
< 16 || templ
->height0
< 32) &&
546 (valid_tilings
& ~tile_y
))
547 valid_tilings
&= ~tile_y
;
550 /* force linear if we are not sure where the texture is bound to */
551 if (valid_tilings
& tile_none
)
552 valid_tilings
&= tile_none
;
555 /* no conflicting binding flags */
556 assert(valid_tilings
);
558 /* prefer tiled than linear */
559 if (valid_tilings
& tile_y
)
560 layout
->tiling
= INTEL_TILING_Y
;
561 else if (valid_tilings
& tile_x
)
562 layout
->tiling
= INTEL_TILING_X
;
564 layout
->tiling
= INTEL_TILING_NONE
;
566 layout
->can_be_linear
= valid_tilings
& tile_none
;
570 tex_layout_init_format(struct tex_layout
*layout
)
572 const struct pipe_resource
*templ
= layout
->templ
;
573 enum pipe_format format
;
575 switch (templ
->format
) {
576 case PIPE_FORMAT_ETC1_RGB8
:
577 format
= PIPE_FORMAT_R8G8B8X8_UNORM
;
579 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
580 if (layout
->separate_stencil
)
581 format
= PIPE_FORMAT_Z24X8_UNORM
;
583 format
= templ
->format
;
585 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
586 if (layout
->separate_stencil
)
587 format
= PIPE_FORMAT_Z32_FLOAT
;
589 format
= templ
->format
;
592 format
= templ
->format
;
596 layout
->format
= format
;
598 layout
->block_width
= util_format_get_blockwidth(format
);
599 layout
->block_height
= util_format_get_blockheight(format
);
600 layout
->block_size
= util_format_get_blocksize(format
);
601 layout
->compressed
= util_format_is_compressed(format
);
605 tex_layout_init_hiz(struct tex_layout
*layout
)
607 const struct pipe_resource
*templ
= layout
->templ
;
608 const struct util_format_description
*desc
;
610 desc
= util_format_description(templ
->format
);
611 layout
->has_depth
= util_format_has_depth(desc
);
612 layout
->has_stencil
= util_format_has_stencil(desc
);
614 if (!layout
->has_depth
)
619 /* no point in having HiZ */
620 if (templ
->usage
== PIPE_USAGE_STAGING
)
623 if (layout
->dev
->gen
== ILO_GEN(6)) {
625 * From the Sandy Bridge PRM, volume 2 part 1, page 312:
627 * "The hierarchical depth buffer does not support the LOD field, it
628 * is assumed by hardware to be zero. A separate hierarachical
629 * depth buffer is required for each LOD used, and the
630 * corresponding buffer's state delivered to hardware each time a
631 * new depth buffer state with modified LOD is delivered."
633 * But we have a stronger requirement. Because of layer offsetting
634 * (check out the callers of ilo_texture_get_slice_offset()), we already
635 * have to require the texture to be non-mipmapped and non-array.
637 if (templ
->last_level
> 0 || templ
->array_size
> 1 || templ
->depth0
> 1)
641 if (ilo_debug
& ILO_DEBUG_NOHIZ
)
644 if (layout
->has_stencil
) {
646 * From the Sandy Bridge PRM, volume 2 part 1, page 317:
648 * "This field (Separate Stencil Buffer Enable) must be set to the
649 * same value (enabled or disabled) as Hierarchical Depth Buffer
652 * GEN7+ requires separate stencil buffers.
654 if (layout
->dev
->gen
>= ILO_GEN(7))
655 layout
->separate_stencil
= true;
657 layout
->separate_stencil
= layout
->hiz
;
659 if (layout
->separate_stencil
)
660 layout
->has_stencil
= false;
665 tex_layout_init(struct tex_layout
*layout
,
666 struct pipe_screen
*screen
,
667 const struct pipe_resource
*templ
,
668 struct ilo_texture_slice
**slices
)
670 struct ilo_screen
*is
= ilo_screen(screen
);
672 memset(layout
, 0, sizeof(*layout
));
674 layout
->dev
= &is
->dev
;
675 layout
->templ
= templ
;
677 /* note that there are dependencies between these functions */
678 tex_layout_init_hiz(layout
);
679 tex_layout_init_format(layout
);
680 tex_layout_init_tiling(layout
);
681 tex_layout_init_spacing(layout
);
682 tex_layout_init_levels(layout
);
683 tex_layout_init_alignments(layout
);
684 tex_layout_init_qpitch(layout
);
689 for (lv
= 0; lv
<= templ
->last_level
; lv
++)
690 layout
->levels
[lv
].slices
= slices
[lv
];
695 tex_layout_align(struct tex_layout
*layout
)
697 int align_w
= 1, align_h
= 1, pad_h
= 0;
700 * From the Sandy Bridge PRM, volume 1 part 1, page 118:
702 * "To determine the necessary padding on the bottom and right side of
703 * the surface, refer to the table in Section 7.18.3.4 for the i and j
704 * parameters for the surface format in use. The surface must then be
705 * extended to the next multiple of the alignment unit size in each
706 * dimension, and all texels contained in this extended surface must
707 * have valid GTT entries."
709 * "For cube surfaces, an additional two rows of padding are required
710 * at the bottom of the surface. This must be ensured regardless of
711 * whether the surface is stored tiled or linear. This is due to the
712 * potential rotation of cache line orientation from memory to cache."
714 * "For compressed textures (BC* and FXT1 surface formats), padding at
715 * the bottom of the surface is to an even compressed row, which is
716 * equal to a multiple of 8 uncompressed texel rows. Thus, for padding
717 * purposes, these surfaces behave as if j = 8 only for surface
718 * padding purposes. The value of 4 for j still applies for mip level
719 * alignment and QPitch calculation."
721 if (layout
->templ
->bind
& PIPE_BIND_SAMPLER_VIEW
) {
722 align_w
= MAX2(align_w
, layout
->align_i
);
723 align_h
= MAX2(align_h
, layout
->align_j
);
725 if (layout
->templ
->target
== PIPE_TEXTURE_CUBE
)
728 if (layout
->compressed
)
729 align_h
= MAX2(align_h
, layout
->align_j
* 2);
733 * From the Sandy Bridge PRM, volume 1 part 1, page 118:
735 * "If the surface contains an odd number of rows of data, a final row
736 * below the surface must be allocated."
738 if (layout
->templ
->bind
& PIPE_BIND_RENDER_TARGET
)
739 align_h
= MAX2(align_h
, 2);
742 * Depth Buffer Clear/Resolve works in 8x4 sample blocks. In
743 * ilo_texture_can_enable_hiz(), we always return true for the first slice.
744 * To avoid out-of-bound access, we have to pad.
747 align_w
= MAX2(align_w
, 8);
748 align_h
= MAX2(align_h
, 4);
751 layout
->width
= align(layout
->width
, align_w
);
752 layout
->height
= align(layout
->height
+ pad_h
, align_h
);
756 * Layout a 2D texture.
759 tex_layout_2d(struct tex_layout
*layout
)
761 const struct pipe_resource
*templ
= layout
->templ
;
762 unsigned int level_x
, level_y
, num_slices
;
767 for (lv
= 0; lv
<= templ
->last_level
; lv
++) {
768 const unsigned int level_w
= layout
->levels
[lv
].w
;
769 const unsigned int level_h
= layout
->levels
[lv
].h
;
772 /* set slice offsets */
773 if (layout
->levels
[lv
].slices
) {
774 for (slice
= 0; slice
< templ
->array_size
; slice
++) {
775 layout
->levels
[lv
].slices
[slice
].x
= level_x
;
776 /* slices are qpitch apart in Y-direction */
777 layout
->levels
[lv
].slices
[slice
].y
=
778 level_y
+ layout
->qpitch
* slice
;
782 /* extend the size of the monolithic bo to cover this mip level */
783 if (layout
->width
< level_x
+ level_w
)
784 layout
->width
= level_x
+ level_w
;
785 if (layout
->height
< level_y
+ level_h
)
786 layout
->height
= level_y
+ level_h
;
788 /* MIPLAYOUT_BELOW */
790 level_x
+= align(level_w
, layout
->align_i
);
792 level_y
+= align(level_h
, layout
->align_j
);
795 num_slices
= templ
->array_size
;
796 /* samples of the same index are stored in a slice */
797 if (templ
->nr_samples
> 1 && !layout
->interleaved
)
798 num_slices
*= templ
->nr_samples
;
800 /* we did not take slices into consideration in the computation above */
801 layout
->height
+= layout
->qpitch
* (num_slices
- 1);
803 tex_layout_align(layout
);
807 * Layout a 3D texture.
810 tex_layout_3d(struct tex_layout
*layout
)
812 const struct pipe_resource
*templ
= layout
->templ
;
813 unsigned int level_y
;
817 for (lv
= 0; lv
<= templ
->last_level
; lv
++) {
818 const unsigned int level_w
= layout
->levels
[lv
].w
;
819 const unsigned int level_h
= layout
->levels
[lv
].h
;
820 const unsigned int level_d
= layout
->levels
[lv
].d
;
821 const unsigned int slice_pitch
= align(level_w
, layout
->align_i
);
822 const unsigned int slice_qpitch
= align(level_h
, layout
->align_j
);
823 const unsigned int num_slices_per_row
= 1 << lv
;
826 for (slice
= 0; slice
< level_d
; slice
+= num_slices_per_row
) {
829 /* set slice offsets */
830 if (layout
->levels
[lv
].slices
) {
831 for (i
= 0; i
< num_slices_per_row
&& slice
+ i
< level_d
; i
++) {
832 layout
->levels
[lv
].slices
[slice
+ i
].x
= slice_pitch
* i
;
833 layout
->levels
[lv
].slices
[slice
+ i
].y
= level_y
;
837 /* move on to the next slice row */
838 level_y
+= slice_qpitch
;
841 /* rightmost slice */
842 slice
= MIN2(num_slices_per_row
, level_d
) - 1;
844 /* extend the size of the monolithic bo to cover this slice */
845 if (layout
->width
< slice_pitch
* slice
+ level_w
)
846 layout
->width
= slice_pitch
* slice
+ level_w
;
847 if (lv
== templ
->last_level
)
848 layout
->height
= (level_y
- slice_qpitch
) + level_h
;
851 tex_layout_align(layout
);
854 /* note that this may force the texture to be linear */
856 tex_layout_calculate_bo_size(struct tex_layout
*layout
)
858 assert(layout
->width
% layout
->block_width
== 0);
859 assert(layout
->height
% layout
->block_height
== 0);
860 assert(layout
->qpitch
% layout
->block_height
== 0);
863 (layout
->width
/ layout
->block_width
) * layout
->block_size
;
864 layout
->bo_height
= layout
->height
/ layout
->block_height
;
867 int w
= layout
->bo_stride
, h
= layout
->bo_height
;
868 int align_w
, align_h
;
871 * From the Haswell PRM, volume 5, page 163:
873 * "For linear surfaces, additional padding of 64 bytes is required
874 * at the bottom of the surface. This is in addition to the padding
877 if (layout
->dev
->gen
>= ILO_GEN(7.5) &&
878 (layout
->templ
->bind
& PIPE_BIND_SAMPLER_VIEW
) &&
879 layout
->tiling
== INTEL_TILING_NONE
) {
881 (64 + layout
->bo_stride
- 1) / layout
->bo_stride
;
885 * From the Sandy Bridge PRM, volume 4 part 1, page 81:
887 * "- For linear render target surfaces, the pitch must be a
888 * multiple of the element size for non-YUV surface formats.
889 * Pitch must be a multiple of 2 * element size for YUV surface
891 * - For other linear surfaces, the pitch can be any multiple of
893 * - For tiled surfaces, the pitch must be a multiple of the tile
896 * Different requirements may exist when the bo is used in different
897 * places, but our alignments here should be good enough that we do not
898 * need to check layout->templ->bind.
900 switch (layout
->tiling
) {
910 if (layout
->format
== PIPE_FORMAT_S8_UINT
) {
912 * From the Sandy Bridge PRM, volume 1 part 2, page 22:
914 * "A 4KB tile is subdivided into 8-high by 8-wide array of
915 * Blocks for W-Major Tiles (W Tiles). Each Block is 8 rows by 8
918 * Since we asked for INTEL_TILING_NONE instead of the non-existent
919 * INTEL_TILING_W, we want to align to W tiles here.
925 /* some good enough values */
932 w
= align(w
, align_w
);
933 h
= align(h
, align_h
);
935 /* make sure the bo is mappable */
936 if (layout
->tiling
!= INTEL_TILING_NONE
) {
938 * Usually only the first 256MB of the GTT is mappable.
940 * See also how intel_context::max_gtt_map_object_size is calculated.
942 const size_t mappable_gtt_size
= 256 * 1024 * 1024;
945 * Be conservative. We may be able to switch from VALIGN_4 to
946 * VALIGN_2 if the layout was Y-tiled, but let's keep it simple.
948 if (mappable_gtt_size
/ w
/ 4 < h
) {
949 if (layout
->can_be_linear
) {
950 layout
->tiling
= INTEL_TILING_NONE
;
954 ilo_warn("cannot force texture to be linear\n");
959 layout
->bo_stride
= w
;
960 layout
->bo_height
= h
;
964 return (layout
->bo_height
<= max_resource_size
/ layout
->bo_stride
);
968 tex_layout_calculate_hiz_size(struct tex_layout
*layout
)
970 const struct pipe_resource
*templ
= layout
->templ
;
971 const int hz_align_j
= 8;
972 int hz_width
, hz_height
;
978 * See the Sandy Bridge PRM, volume 2 part 1, page 312, and the Ivy Bridge
979 * PRM, volume 2 part 1, page 312-313.
981 * It seems HiZ buffer is aligned to 8x8, with every two rows packed into a
985 hz_width
= align(layout
->levels
[0].w
, 16);
987 if (templ
->target
== PIPE_TEXTURE_3D
) {
992 for (lv
= 0; lv
<= templ
->last_level
; lv
++) {
993 const unsigned h
= align(layout
->levels
[lv
].h
, hz_align_j
);
994 hz_height
+= h
* layout
->levels
[lv
].d
;
1000 const unsigned h0
= align(layout
->levels
[0].h
, hz_align_j
);
1001 unsigned hz_qpitch
= h0
;
1003 if (layout
->array_spacing_full
) {
1004 const unsigned h1
= align(layout
->levels
[1].h
, hz_align_j
);
1005 const unsigned htail
=
1006 ((layout
->dev
->gen
>= ILO_GEN(7)) ? 12 : 11) * hz_align_j
;
1008 hz_qpitch
+= h1
+ htail
;
1011 hz_height
= hz_qpitch
* templ
->array_size
/ 2;
1013 if (layout
->dev
->gen
>= ILO_GEN(7))
1014 hz_height
= align(hz_height
, 8);
1017 /* align to Y-tile */
1018 layout
->hiz_stride
= align(hz_width
, 128);
1019 layout
->hiz_height
= align(hz_height
, 32);
1023 tex_free_slices(struct ilo_texture
*tex
)
1025 FREE(tex
->slices
[0]);
1029 tex_alloc_slices(struct ilo_texture
*tex
)
1031 const struct pipe_resource
*templ
= &tex
->base
;
1032 struct ilo_texture_slice
*slices
;
1035 /* sum the depths of all levels */
1037 for (lv
= 0; lv
<= templ
->last_level
; lv
++)
1038 depth
+= u_minify(templ
->depth0
, lv
);
1041 * There are (depth * tex->base.array_size) slices in total. Either depth
1042 * is one (non-3D) or templ->array_size is one (non-array), but it does
1045 slices
= CALLOC(depth
* templ
->array_size
, sizeof(*slices
));
1049 tex
->slices
[0] = slices
;
1051 /* point to the respective positions in the buffer */
1052 for (lv
= 1; lv
<= templ
->last_level
; lv
++) {
1053 tex
->slices
[lv
] = tex
->slices
[lv
- 1] +
1054 u_minify(templ
->depth0
, lv
- 1) * templ
->array_size
;
1061 tex_create_bo(struct ilo_texture
*tex
,
1062 const struct winsys_handle
*handle
)
1064 struct ilo_screen
*is
= ilo_screen(tex
->base
.screen
);
1066 struct intel_bo
*bo
;
1068 switch (tex
->base
.target
) {
1069 case PIPE_TEXTURE_1D
:
1070 name
= "1D texture";
1072 case PIPE_TEXTURE_2D
:
1073 name
= "2D texture";
1075 case PIPE_TEXTURE_3D
:
1076 name
= "3D texture";
1078 case PIPE_TEXTURE_CUBE
:
1079 name
= "cube texture";
1081 case PIPE_TEXTURE_RECT
:
1082 name
= "rectangle texture";
1084 case PIPE_TEXTURE_1D_ARRAY
:
1085 name
= "1D array texture";
1087 case PIPE_TEXTURE_2D_ARRAY
:
1088 name
= "2D array texture";
1090 case PIPE_TEXTURE_CUBE_ARRAY
:
1091 name
= "cube array texture";
1094 name
="unknown texture";
1099 enum intel_tiling_mode tiling
;
1100 unsigned long pitch
;
1102 bo
= intel_winsys_import_handle(is
->winsys
, name
, handle
,
1103 tex
->bo_height
, &tiling
, &pitch
);
1106 tex
->tiling
= tiling
;
1107 tex
->bo_stride
= pitch
;
1111 const uint32_t initial_domain
=
1112 (tex
->base
.bind
& (PIPE_BIND_DEPTH_STENCIL
|
1113 PIPE_BIND_RENDER_TARGET
)) ?
1114 INTEL_DOMAIN_RENDER
: 0;
1116 bo
= intel_winsys_alloc_bo(is
->winsys
, name
, tex
->tiling
,
1117 tex
->bo_stride
, tex
->bo_height
, initial_domain
);
1124 intel_bo_unreference(tex
->bo
);
1132 tex_create_separate_stencil(struct ilo_texture
*tex
)
1134 struct pipe_resource templ
= tex
->base
;
1135 struct pipe_resource
*s8
;
1138 * Unless PIPE_BIND_DEPTH_STENCIL is set, the resource may have other
1139 * tilings. But that should be fine since it will never be bound as the
1140 * stencil buffer, and our transfer code can handle all tilings.
1142 templ
.format
= PIPE_FORMAT_S8_UINT
;
1144 s8
= tex
->base
.screen
->resource_create(tex
->base
.screen
, &templ
);
1148 tex
->separate_s8
= ilo_texture(s8
);
1150 assert(tex
->separate_s8
->bo_format
== PIPE_FORMAT_S8_UINT
);
1156 tex_create_hiz(struct ilo_texture
*tex
, const struct tex_layout
*layout
)
1158 struct ilo_screen
*is
= ilo_screen(tex
->base
.screen
);
1159 const struct pipe_resource
*templ
= layout
->templ
;
1162 tex
->hiz
.bo
= intel_winsys_alloc_bo(is
->winsys
, "hiz texture",
1163 INTEL_TILING_Y
, layout
->hiz_stride
, layout
->hiz_height
,
1164 INTEL_DOMAIN_RENDER
);
1168 tex
->hiz
.bo_stride
= layout
->hiz_stride
;
1171 * From the Sandy Bridge PRM, volume 2 part 1, page 313-314:
1173 * "A rectangle primitive representing the clear area is delivered. The
1174 * primitive must adhere to the following restrictions on size:
1176 * - If Number of Multisamples is NUMSAMPLES_1, the rectangle must be
1177 * aligned to an 8x4 pixel block relative to the upper left corner
1178 * of the depth buffer, and contain an integer number of these pixel
1179 * blocks, and all 8x4 pixels must be lit.
1181 * - If Number of Multisamples is NUMSAMPLES_4, the rectangle must be
1182 * aligned to a 4x2 pixel block (8x4 sample block) relative to the
1183 * upper left corner of the depth buffer, and contain an integer
1184 * number of these pixel blocks, and all samples of the 4x2 pixels
1187 * - If Number of Multisamples is NUMSAMPLES_8, the rectangle must be
1188 * aligned to a 2x2 pixel block (8x4 sample block) relative to the
1189 * upper left corner of the depth buffer, and contain an integer
1190 * number of these pixel blocks, and all samples of the 2x2 pixels
1193 * "The following is required when performing a depth buffer resolve:
1195 * - A rectangle primitive of the same size as the previous depth
1196 * buffer clear operation must be delivered, and depth buffer state
1197 * cannot have changed since the previous depth buffer clear
1200 * Experiments on Haswell show that depth buffer resolves have the same
1201 * alignment requirements, and aligning the RECTLIST primitive and
1202 * 3DSTATE_DRAWING_RECTANGLE alone are not enough. The mipmap size must be
1205 for (lv
= 0; lv
<= templ
->last_level
; lv
++) {
1206 unsigned align_w
= 8, align_h
= 4;
1209 switch (templ
->nr_samples
) {
1227 if (u_minify(templ
->width0
, lv
) % align_w
== 0 &&
1228 u_minify(templ
->height0
, lv
) % align_h
== 0) {
1229 flags
|= ILO_TEXTURE_HIZ
;
1231 /* this will trigger a HiZ resolve */
1233 flags
|= ILO_TEXTURE_CPU_WRITE
;
1237 const unsigned num_slices
= (templ
->target
== PIPE_TEXTURE_3D
) ?
1238 u_minify(templ
->depth0
, lv
) : templ
->array_size
;
1239 ilo_texture_set_slice_flags(tex
, lv
, 0, num_slices
, flags
, flags
);
1247 tex_apply_layout(struct ilo_texture
*tex
,
1248 const struct tex_layout
*layout
,
1249 const struct winsys_handle
*handle
)
1251 tex
->bo_format
= layout
->format
;
1253 tex
->tiling
= layout
->tiling
;
1254 tex
->bo_stride
= layout
->bo_stride
;
1255 tex
->bo_height
= layout
->bo_height
;
1257 tex
->block_width
= layout
->block_width
;
1258 tex
->block_height
= layout
->block_height
;
1259 tex
->block_size
= layout
->block_size
;
1261 tex
->halign_8
= (layout
->align_i
== 8);
1262 tex
->valign_4
= (layout
->align_j
== 4);
1263 tex
->array_spacing_full
= layout
->array_spacing_full
;
1264 tex
->interleaved
= layout
->interleaved
;
1266 if (!tex_create_bo(tex
, handle
))
1269 /* allocate separate stencil resource */
1270 if (layout
->separate_stencil
&& !tex_create_separate_stencil(tex
))
1273 if (layout
->hiz
&& !tex_create_hiz(tex
, layout
)) {
1274 /* Separate Stencil Buffer requires HiZ to be enabled */
1275 if (layout
->dev
->gen
== ILO_GEN(6) && layout
->separate_stencil
)
1283 tex_destroy(struct ilo_texture
*tex
)
1286 intel_bo_unreference(tex
->hiz
.bo
);
1288 if (tex
->separate_s8
)
1289 tex_destroy(tex
->separate_s8
);
1292 intel_bo_unreference(tex
->bo
);
1294 tex_free_slices(tex
);
1298 static struct pipe_resource
*
1299 tex_create(struct pipe_screen
*screen
,
1300 const struct pipe_resource
*templ
,
1301 const struct winsys_handle
*handle
)
1303 struct tex_layout layout
;
1304 struct ilo_texture
*tex
;
1306 tex
= CALLOC_STRUCT(ilo_texture
);
1311 tex
->base
.screen
= screen
;
1312 pipe_reference_init(&tex
->base
.reference
, 1);
1314 if (!tex_alloc_slices(tex
)) {
1319 tex
->imported
= (handle
!= NULL
);
1321 tex_layout_init(&layout
, screen
, templ
, tex
->slices
);
1323 switch (templ
->target
) {
1324 case PIPE_TEXTURE_1D
:
1325 case PIPE_TEXTURE_2D
:
1326 case PIPE_TEXTURE_CUBE
:
1327 case PIPE_TEXTURE_RECT
:
1328 case PIPE_TEXTURE_1D_ARRAY
:
1329 case PIPE_TEXTURE_2D_ARRAY
:
1330 case PIPE_TEXTURE_CUBE_ARRAY
:
1331 tex_layout_2d(&layout
);
1333 case PIPE_TEXTURE_3D
:
1334 tex_layout_3d(&layout
);
1337 assert(!"unknown resource target");
1341 if (!tex_layout_calculate_bo_size(&layout
)) {
1346 tex_layout_calculate_hiz_size(&layout
);
1348 if (!tex_apply_layout(tex
, &layout
, handle
)) {
1357 tex_get_handle(struct ilo_texture
*tex
, struct winsys_handle
*handle
)
1359 struct ilo_screen
*is
= ilo_screen(tex
->base
.screen
);
1362 err
= intel_winsys_export_handle(is
->winsys
, tex
->bo
,
1363 tex
->tiling
, tex
->bo_stride
, tex
->bo_height
, handle
);
1369 buf_create_bo(struct ilo_buffer
*buf
)
1371 const uint32_t initial_domain
=
1372 (buf
->base
.bind
& PIPE_BIND_STREAM_OUTPUT
) ?
1373 INTEL_DOMAIN_RENDER
: 0;
1374 struct ilo_screen
*is
= ilo_screen(buf
->base
.screen
);
1376 struct intel_bo
*bo
;
1378 switch (buf
->base
.bind
) {
1379 case PIPE_BIND_VERTEX_BUFFER
:
1380 name
= "vertex buffer";
1382 case PIPE_BIND_INDEX_BUFFER
:
1383 name
= "index buffer";
1385 case PIPE_BIND_CONSTANT_BUFFER
:
1386 name
= "constant buffer";
1388 case PIPE_BIND_STREAM_OUTPUT
:
1389 name
= "stream output";
1392 name
= "unknown buffer";
1396 bo
= intel_winsys_alloc_buffer(is
->winsys
,
1397 name
, buf
->bo_size
, initial_domain
);
1402 intel_bo_unreference(buf
->bo
);
1410 buf_destroy(struct ilo_buffer
*buf
)
1412 intel_bo_unreference(buf
->bo
);
1416 static struct pipe_resource
*
1417 buf_create(struct pipe_screen
*screen
, const struct pipe_resource
*templ
)
1419 struct ilo_buffer
*buf
;
1421 buf
= CALLOC_STRUCT(ilo_buffer
);
1426 buf
->base
.screen
= screen
;
1427 pipe_reference_init(&buf
->base
.reference
, 1);
1429 buf
->bo_size
= templ
->width0
;
1432 * From the Sandy Bridge PRM, volume 1 part 1, page 118:
1434 * "For buffers, which have no inherent "height," padding requirements
1435 * are different. A buffer must be padded to the next multiple of 256
1436 * array elements, with an additional 16 bytes added beyond that to
1437 * account for the L1 cache line."
1439 if (templ
->bind
& PIPE_BIND_SAMPLER_VIEW
)
1440 buf
->bo_size
= align(buf
->bo_size
, 256) + 16;
1442 if (templ
->bind
& PIPE_BIND_VERTEX_BUFFER
) {
1444 * As noted in ilo_translate_format(), we treat some 3-component formats
1445 * as 4-component formats to work around hardware limitations. Imagine
1446 * the case where the vertex buffer holds a single
1447 * PIPE_FORMAT_R16G16B16_FLOAT vertex, and buf->bo_size is 6. The
1448 * hardware would fail to fetch it at boundary check because the vertex
1449 * buffer is expected to hold a PIPE_FORMAT_R16G16B16A16_FLOAT vertex
1450 * and that takes at least 8 bytes.
1452 * For the workaround to work, we should add 2 to the bo size. But that
1453 * would waste a page when the bo size is already page aligned. Let's
1454 * round it to page size for now and revisit this when needed.
1456 buf
->bo_size
= align(buf
->bo_size
, 4096);
1459 if (buf
->bo_size
< templ
->width0
||
1460 buf
->bo_size
> max_resource_size
||
1461 !buf_create_bo(buf
)) {
1470 ilo_can_create_resource(struct pipe_screen
*screen
,
1471 const struct pipe_resource
*templ
)
1473 struct tex_layout layout
;
1475 if (templ
->target
== PIPE_BUFFER
)
1476 return (templ
->width0
<= max_resource_size
);
1478 tex_layout_init(&layout
, screen
, templ
, NULL
);
1480 switch (templ
->target
) {
1481 case PIPE_TEXTURE_3D
:
1482 tex_layout_3d(&layout
);
1485 tex_layout_2d(&layout
);
1489 return tex_layout_calculate_bo_size(&layout
);
1492 static struct pipe_resource
*
1493 ilo_resource_create(struct pipe_screen
*screen
,
1494 const struct pipe_resource
*templ
)
1496 if (templ
->target
== PIPE_BUFFER
)
1497 return buf_create(screen
, templ
);
1499 return tex_create(screen
, templ
, NULL
);
1502 static struct pipe_resource
*
1503 ilo_resource_from_handle(struct pipe_screen
*screen
,
1504 const struct pipe_resource
*templ
,
1505 struct winsys_handle
*handle
)
1507 if (templ
->target
== PIPE_BUFFER
)
1510 return tex_create(screen
, templ
, handle
);
1514 ilo_resource_get_handle(struct pipe_screen
*screen
,
1515 struct pipe_resource
*res
,
1516 struct winsys_handle
*handle
)
1518 if (res
->target
== PIPE_BUFFER
)
1521 return tex_get_handle(ilo_texture(res
), handle
);
1526 ilo_resource_destroy(struct pipe_screen
*screen
,
1527 struct pipe_resource
*res
)
1529 if (res
->target
== PIPE_BUFFER
)
1530 buf_destroy(ilo_buffer(res
));
1532 tex_destroy(ilo_texture(res
));
1536 * Initialize resource-related functions.
1539 ilo_init_resource_functions(struct ilo_screen
*is
)
1541 is
->base
.can_create_resource
= ilo_can_create_resource
;
1542 is
->base
.resource_create
= ilo_resource_create
;
1543 is
->base
.resource_from_handle
= ilo_resource_from_handle
;
1544 is
->base
.resource_get_handle
= ilo_resource_get_handle
;
1545 is
->base
.resource_destroy
= ilo_resource_destroy
;
1549 ilo_buffer_alloc_bo(struct ilo_buffer
*buf
)
1551 return buf_create_bo(buf
);
1555 ilo_texture_alloc_bo(struct ilo_texture
*tex
)
1557 /* a shared bo cannot be reallocated */
1561 return tex_create_bo(tex
, NULL
);
1565 * Return the offset (in bytes) to a slice within the bo.
1567 * The returned offset is aligned to tile size. Since slices are not
1568 * guaranteed to start at tile boundaries, the X and Y offsets (in pixels)
1569 * from the tile origin to the slice are also returned. X offset is always a
1570 * multiple of 4 and Y offset is always a multiple of 2.
1573 ilo_texture_get_slice_offset(const struct ilo_texture
*tex
,
1574 unsigned level
, unsigned slice
,
1575 unsigned *x_offset
, unsigned *y_offset
)
1577 const struct ilo_texture_slice
*s
=
1578 ilo_texture_get_slice(tex
, level
, slice
);
1579 unsigned tile_w
, tile_h
, tile_size
, row_size
;
1580 unsigned x
, y
, slice_offset
;
1582 /* see the Sandy Bridge PRM, volume 1 part 2, page 24 */
1584 switch (tex
->tiling
) {
1585 case INTEL_TILING_NONE
:
1587 if (tex
->bo_format
== PIPE_FORMAT_S8_UINT
) {
1596 case INTEL_TILING_X
:
1600 case INTEL_TILING_Y
:
1605 assert(!"unknown tiling");
1611 tile_size
= tile_w
* tile_h
;
1612 row_size
= tex
->bo_stride
* tile_h
;
1615 x
= s
->x
/ tex
->block_width
* tex
->block_size
;
1616 y
= s
->y
/ tex
->block_height
;
1617 slice_offset
= row_size
* (y
/ tile_h
) + tile_size
* (x
/ tile_w
);
1620 * Since tex->bo_stride is a multiple of tile_w, slice_offset should be
1621 * aligned at this point.
1623 assert(slice_offset
% tile_size
== 0);
1626 * because of the possible values of align_i and align_j in
1627 * tex_layout_init_alignments(), x_offset is guaranteed to be a multiple of
1628 * 4 and y_offset is guaranteed to be a multiple of 2.
1632 x
= (x
% tile_w
) / tex
->block_size
* tex
->block_width
;
1640 y
= (y
% tile_h
) * tex
->block_height
;
1646 return slice_offset
;