2 * Copyright (C) 2018 Rob Clark <robclark@freedesktop.org>
3 * Copyright © 2018 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>
28 #include "fd6_resource.h"
30 /* indexed by cpp, including msaa 2x and 4x: */
34 } tile_alignment
[] = {
48 /* special cases for r16: */
52 /* NOTE: good way to test this is: (for example)
53 * piglit/bin/texelFetch fs sampler3D 100x100x8
56 setup_slices(struct fd_resource
*rsc
, uint32_t alignment
, enum pipe_format format
)
58 struct pipe_resource
*prsc
= &rsc
->base
;
59 struct fd_screen
*screen
= fd_screen(prsc
->screen
);
60 enum util_format_layout layout
= util_format_description(format
)->layout
;
61 uint32_t pitchalign
= screen
->gmem_alignw
;
62 uint32_t level
, size
= 0;
63 uint32_t depth
= prsc
->depth0
;
64 /* linear dimensions: */
65 uint32_t lwidth
= prsc
->width0
;
66 uint32_t lheight
= prsc
->height0
;
67 /* tile_mode dimensions: */
68 uint32_t twidth
= util_next_power_of_two(lwidth
);
69 uint32_t theight
= util_next_power_of_two(lheight
);
70 /* in layer_first layout, the level (slice) contains just one
71 * layer (since in fact the layer contains the slices)
73 uint32_t layers_in_level
= rsc
->layer_first
? 1 : prsc
->array_size
;
76 /* The z16/r16 formats seem to not play by the normal tiling rules: */
77 if ((rsc
->cpp
== 2) && (util_format_get_nr_components(format
) == 1))
80 debug_assert(ta
< ARRAY_SIZE(tile_alignment
));
81 debug_assert(tile_alignment
[ta
].pitchalign
);
83 for (level
= 0; level
<= prsc
->last_level
; level
++) {
84 struct fd_resource_slice
*slice
= fd_resource_slice(rsc
, level
);
85 bool linear_level
= fd_resource_level_linear(prsc
, level
);
86 uint32_t width
, height
;
88 /* tiled levels of 3D textures are rounded up to PoT dimensions: */
89 if ((prsc
->target
== PIPE_TEXTURE_3D
) && rsc
->tile_mode
&& !linear_level
) {
96 uint32_t aligned_height
= height
;
99 if (rsc
->tile_mode
&& !linear_level
) {
100 pitchalign
= tile_alignment
[ta
].pitchalign
;
101 aligned_height
= align(aligned_height
,
102 tile_alignment
[ta
].heightalign
);
107 /* The blits used for mem<->gmem work at a granularity of
108 * 32x32, which can cause faults due to over-fetch on the
109 * last level. The simple solution is to over-allocate a
110 * bit the last level to ensure any over-fetch is harmless.
111 * The pitch is already sufficiently aligned, but height
114 if ((level
== prsc
->last_level
) && (prsc
->target
!= PIPE_BUFFER
))
115 aligned_height
= align(aligned_height
, 32);
117 if (layout
== UTIL_FORMAT_LAYOUT_ASTC
)
119 util_align_npot(width
, pitchalign
* util_format_get_blockwidth(format
));
121 slice
->pitch
= align(width
, pitchalign
);
123 slice
->offset
= size
;
124 blocks
= util_format_get_nblocks(format
, slice
->pitch
, aligned_height
);
126 /* 1d array and 2d array textures must all have the same layer size
127 * for each miplevel on a6xx. 3d textures can have different layer
128 * sizes for high levels, but the hw auto-sizer is buggy (or at least
129 * different than what this code does), so as soon as the layer size
130 * range gets into range, we stop reducing it.
132 if (prsc
->target
== PIPE_TEXTURE_3D
) {
133 if (level
<= 1 || (rsc
->slices
[level
- 1].size0
> 0xf000)) {
134 slice
->size0
= align(blocks
* rsc
->cpp
, alignment
);
136 slice
->size0
= rsc
->slices
[level
- 1].size0
;
139 slice
->size0
= align(blocks
* rsc
->cpp
, alignment
);
142 size
+= slice
->size0
* depth
* layers_in_level
;
145 debug_printf("%s: %ux%ux%u@%u:\t%2u: stride=%4u, size=%6u,%7u, aligned_height=%3u, blocks=%u\n",
146 util_format_name(prsc
->format
),
147 width
, height
, depth
, rsc
->cpp
,
148 level
, slice
->pitch
* rsc
->cpp
,
149 slice
->size0
, size
, aligned_height
, blocks
);
152 depth
= u_minify(depth
, 1);
153 lwidth
= u_minify(lwidth
, 1);
154 lheight
= u_minify(lheight
, 1);
155 twidth
= u_minify(twidth
, 1);
156 theight
= u_minify(theight
, 1);
163 fd6_setup_slices(struct fd_resource
*rsc
)
167 switch (rsc
->base
.target
) {
168 case PIPE_TEXTURE_3D
:
169 rsc
->layer_first
= false;
173 rsc
->layer_first
= true;
178 return setup_slices(rsc
, alignment
, rsc
->base
.format
);