2 * Copyright (C) 2018 Rob Clark <robclark@freedesktop.org>
3 * Copyright © 2018-2019 Google, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
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 FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * Rob Clark <robclark@freedesktop.org>
30 #include "freedreno_layout.h"
32 /* indexed by cpp, including msaa 2x and 4x: */
36 } tile_alignment
[] = {
50 /* special cases for r8g8: */
54 /* NOTE: good way to test this is: (for example)
55 * piglit/bin/texelFetch fs sampler3D 100x100x8
58 fdl6_layout(struct fdl_layout
*layout
,
59 enum pipe_format format
, uint32_t nr_samples
,
60 uint32_t width0
, uint32_t height0
, uint32_t depth0
,
61 uint32_t mip_levels
, uint32_t array_size
, bool is_3d
)
63 assert(nr_samples
> 0);
64 layout
->width0
= width0
;
65 layout
->height0
= height0
;
66 layout
->depth0
= depth0
;
68 layout
->cpp
= util_format_get_blocksize(format
);
69 layout
->cpp
*= nr_samples
;
71 const struct util_format_description
*format_desc
=
72 util_format_description(format
);
74 uint32_t depth
= depth0
;
75 /* linear dimensions: */
76 uint32_t lwidth
= width0
;
77 uint32_t lheight
= height0
;
78 /* tile_mode dimensions: */
79 uint32_t twidth
= util_next_power_of_two(lwidth
);
80 uint32_t theight
= util_next_power_of_two(lheight
);
83 /* The z16/r16 formats seem to not play by the normal tiling rules: */
84 if ((layout
->cpp
== 2) && (util_format_get_nr_components(format
) == 2))
89 layout
->layer_first
= false;
92 layout
->layer_first
= true;
95 /* in layer_first layout, the level (slice) contains just one
96 * layer (since in fact the layer contains the slices)
98 uint32_t layers_in_level
= layout
->layer_first
? 1 : array_size
;
100 debug_assert(ta
< ARRAY_SIZE(tile_alignment
));
101 debug_assert(tile_alignment
[ta
].pitchalign
);
103 for (level
= 0; level
< mip_levels
; level
++) {
104 struct fdl_slice
*slice
= &layout
->slices
[level
];
105 uint32_t tile_mode
= fdl_tile_mode(layout
, level
);
106 uint32_t width
, height
;
108 /* tiled levels of 3D textures are rounded up to PoT dimensions: */
109 if (is_3d
&& tile_mode
) {
116 uint32_t aligned_height
= height
;
120 pitchalign
= tile_alignment
[ta
].pitchalign
;
121 aligned_height
= align(aligned_height
,
122 tile_alignment
[ta
].heightalign
);
127 /* The blits used for mem<->gmem work at a granularity of
128 * 32x32, which can cause faults due to over-fetch on the
129 * last level. The simple solution is to over-allocate a
130 * bit the last level to ensure any over-fetch is harmless.
131 * The pitch is already sufficiently aligned, but height
134 if (level
== mip_levels
- 1)
135 aligned_height
= align(aligned_height
, 32);
137 if (format_desc
->layout
== UTIL_FORMAT_LAYOUT_ASTC
)
139 util_align_npot(width
, pitchalign
* util_format_get_blockwidth(format
));
141 slice
->pitch
= align(width
, pitchalign
);
143 slice
->offset
= layout
->size
;
144 uint32_t blocks
= util_format_get_nblocks(format
,
145 slice
->pitch
, aligned_height
);
147 /* 1d array and 2d array textures must all have the same layer size
148 * for each miplevel on a6xx. 3d textures can have different layer
149 * sizes for high levels, but the hw auto-sizer is buggy (or at least
150 * different than what this code does), so as soon as the layer size
151 * range gets into range, we stop reducing it.
154 if (level
< 1 || layout
->slices
[level
- 1].size0
> 0xf000) {
155 slice
->size0
= align(blocks
* layout
->cpp
, alignment
);
157 slice
->size0
= layout
->slices
[level
- 1].size0
;
160 slice
->size0
= align(blocks
* layout
->cpp
, alignment
);
163 layout
->size
+= slice
->size0
* depth
* layers_in_level
;
166 fprintf(stderr
, "%s: %ux%ux%u@%u:\t%2u: stride=%4u, size=%6u,%7u, aligned_height=%3u, blocks=%u, offset=0x%x tiling=%d\n",
167 util_format_name(format
),
168 width
, height
, depth
, layout
->cpp
,
169 level
, slice
->pitch
* layout
->cpp
,
170 slice
->size0
, layout
->size
, aligned_height
, blocks
,
171 slice
->offset
, tile_mode
);
174 depth
= u_minify(depth
, 1);
175 lwidth
= u_minify(lwidth
, 1);
176 lheight
= u_minify(lheight
, 1);
177 twidth
= u_minify(twidth
, 1);
178 theight
= u_minify(theight
, 1);
181 if (layout
->layer_first
) {
182 layout
->layer_size
= align(layout
->size
, 4096);
183 layout
->size
= layout
->layer_size
* array_size
;