2 * Copyright (C) 2018 Rob Clark <robclark@freedesktop.org>
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * Rob Clark <robclark@freedesktop.org>
27 #include "fd5_resource.h"
33 } tile_alignment
[] = {
43 /* NOTE: good way to test this is: (for example)
44 * piglit/bin/texelFetch fs sampler2D 100x100x1-100x300x1
47 setup_slices(struct fd_resource
*rsc
, uint32_t alignment
, enum pipe_format format
)
49 struct pipe_resource
*prsc
= &rsc
->base
;
50 struct fd_screen
*screen
= fd_screen(prsc
->screen
);
51 enum util_format_layout layout
= util_format_description(format
)->layout
;
52 uint32_t pitchalign
= screen
->gmem_alignw
;
54 uint32_t level
, size
= 0;
55 uint32_t width
= prsc
->width0
;
56 uint32_t height
= prsc
->height0
;
57 uint32_t depth
= prsc
->depth0
;
58 /* in layer_first layout, the level (slice) contains just one
59 * layer (since in fact the layer contains the slices)
61 uint32_t layers_in_level
= rsc
->layer_first
? 1 : prsc
->array_size
;
63 heightalign
= tile_alignment
[rsc
->cpp
].heightalign
;
65 for (level
= 0; level
<= prsc
->last_level
; level
++) {
66 struct fd_resource_slice
*slice
= fd_resource_slice(rsc
, level
);
67 bool linear_level
= fd_resource_level_linear(prsc
, level
);
68 uint32_t aligned_height
= height
;
71 if (rsc
->tile_mode
&& !linear_level
) {
72 pitchalign
= tile_alignment
[rsc
->cpp
].pitchalign
;
73 aligned_height
= align(aligned_height
, heightalign
);
77 /* The blits used for mem<->gmem work at a granularity of
78 * 32x32, which can cause faults due to over-fetch on the
79 * last level. The simple solution is to over-allocate a
80 * bit the last level to ensure any over-fetch is harmless.
81 * The pitch is already sufficiently aligned, but height
84 if ((level
== prsc
->last_level
) && (prsc
->target
!= PIPE_BUFFER
))
85 aligned_height
= align(aligned_height
, 32);
88 if (layout
== UTIL_FORMAT_LAYOUT_ASTC
)
90 util_align_npot(width
, pitchalign
* util_format_get_blockwidth(format
));
92 slice
->pitch
= align(width
, pitchalign
);
95 blocks
= util_format_get_nblocks(format
, slice
->pitch
, aligned_height
);
97 /* 1d array and 2d array textures must all have the same layer size
98 * for each miplevel on a3xx. 3d textures can have different layer
99 * sizes for high levels, but the hw auto-sizer is buggy (or at least
100 * different than what this code does), so as soon as the layer size
101 * range gets into range, we stop reducing it.
103 if (prsc
->target
== PIPE_TEXTURE_3D
&& (
105 (level
> 1 && rsc
->slices
[level
- 1].size0
> 0xf000)))
106 slice
->size0
= align(blocks
* rsc
->cpp
, alignment
);
107 else if (level
== 0 || rsc
->layer_first
|| alignment
== 1)
108 slice
->size0
= align(blocks
* rsc
->cpp
, alignment
);
110 slice
->size0
= rsc
->slices
[level
- 1].size0
;
113 debug_printf("%s: %ux%ux%u@%u: %2u: stride=%4u, size=%7u, aligned_height=%3u\n",
114 util_format_name(prsc
->format
),
115 prsc
->width0
, prsc
->height0
, prsc
->depth0
, rsc
->cpp
,
116 level
, slice
->pitch
* rsc
->cpp
,
117 slice
->size0
* depth
* layers_in_level
,
121 size
+= slice
->size0
* depth
* layers_in_level
;
123 width
= u_minify(width
, 1);
124 height
= u_minify(height
, 1);
125 depth
= u_minify(depth
, 1);
132 fd5_setup_slices(struct fd_resource
*rsc
)
136 switch (rsc
->base
.target
) {
137 case PIPE_TEXTURE_3D
:
138 rsc
->layer_first
= false;
142 rsc
->layer_first
= true;
147 return setup_slices(rsc
, alignment
, rsc
->base
.format
);