Debian Codegraph @

src/asahi/libagx/texture.cl

raw
1 /*
2 * Copyright 2023 Alyssa Rosenzweig
3 * Copyright 2023 Valve Corporation
4 * SPDX-License-Identifier: MIT
5 */
6 #include "asahi/lib/agx_abi.h"
7 #include "compiler/libcl/libcl.h"
8 #include "libagx_intrinsics.h"
9 #include <agx_pack.h>
10
11 uint3
12 libagx_txs(constant struct agx_texture_packed *ptr, uint16_t lod,
13 unsigned nr_comps, bool is_buffer, bool is_1d, bool is_2d,
14 bool is_cube, bool is_array)
15 {
16 agx_unpack(NULL, ptr, TEXTURE, d);
17
18 /* From the Vulkan spec:
19 *
20 * OpImageQuery*... return 0 if the bound descriptor is a null descriptor
21 */
22 if (d.address == AGX_ZERO_PAGE_ADDRESS)
23 return 0;
24
25 /* Buffer textures are lowered to 2D so the original size is irrecoverable.
26 * Instead, we stash it in the software-defined section.
27 */
28 if (is_buffer)
29 return d.buffer_size_sw;
30
31 /* Load standard dimensions */
32 uint3 size = (uint3)(d.width, d.height, d.depth);
33 lod += d.first_level;
34
35 /* Linear 2D arrays are special.
36 *
37 * TODO: Optimize this, since linear 2D arrays aren't needed for APIs and
38 * this just gets used internally for blits.
39 */
40 if (is_2d && is_array && d.layout == AGX_LAYOUT_LINEAR)
41 size.z = d.depth_linear;
42
43 /* 1D Arrays have their second component as the layer count */
44 if (is_1d && is_array)
45 size.y = size.z;
46
47 /* Adjust for LOD, do not adjust array size */
48 size.x = max(size.x >> lod, 1u);
49
50 if (nr_comps - (uint)is_array >= 2)
51 size.y = max(size.y >> lod, 1u);
52
53 if (nr_comps - (uint)is_array >= 3)
54 size.z = max(size.z >> lod, 1u);
55
56 /* Cube maps have equal width and height, we save some instructions by only
57 * reading one. Dead code elimination will remove the redundant instructions.
58 */
59 if (is_cube)
60 size.y = size.x;
61
62 return size;
63 }
64
65 uint
66 libagx_texture_samples(constant struct agx_texture_packed *ptr)
67 {
68 agx_unpack(NULL, ptr, TEXTURE, d);
69
70 /* As above */
71 if (d.address == AGX_ZERO_PAGE_ADDRESS)
72 return 0;
73
74 /* We may assume the input is multisampled, so just check the samples */
75 return (d.samples == AGX_SAMPLE_COUNT_2) ? 2 : 4;
76 }
77
78 uint
79 libagx_texture_levels(constant struct agx_texture_packed *ptr)
80 {
81 agx_unpack(NULL, ptr, TEXTURE, d);
82
83 if (d.address == AGX_ZERO_PAGE_ADDRESS)
84 return 0;
85 else
86 return (d.last_level - d.first_level) + 1;
87 }
88
89 /*
90 * Fix robustness behaviour of txf with out-of-bounds LOD. The hardware
91 * returns the correct out-of-bounds colour for out-of-bounds coordinates,
92 * just not LODs. So translate out-of-bounds LOD into an out-of-bounds
93 * coordinate to get correct behaviour in 1 instruction.
94 *
95 * Returns the fixed X-coordinate.
96 *
97 * TODO: This looks like it might be an erratum workaround on G13 (Apple does
98 * it), maybe check if G15 is affected.
99 */
100 uint
101 libagx_lower_txf_robustness(constant struct agx_texture_packed *ptr,
102 bool check_lod, ushort lod, bool check_min_lod,
103 ushort min_lod, bool check_layer, uint layer,
104 uint x)
105 {
106 agx_unpack(NULL, ptr, TEXTURE, d);
107
108 bool valid = true;
109
110 if (check_lod)
111 valid &= lod <= (d.last_level - d.first_level);
112
113 if (check_min_lod)
114 valid &= lod >= min_lod;
115
116 if (check_layer) {
117 bool linear = (d.layout == AGX_LAYOUT_LINEAR);
118 valid &= layer < (linear ? d.depth_linear : d.depth);
119 }
120
121 /* The maximum tail offset is 0xF so by returning 0xFFF0 for out-of-bounds we
122 * stay under 0xFFFF and keep robustness after offsetting.
123 */
124 return valid ? x : 0xFFF0;
125 }
126
127 uint32_t
128 libagx_twiddle_coordinates(ushort2 coord, uint16_t tile_w_px,
129 uint16_t tile_h_px, uint32_t aligned_width_px)
130 {
131 /* Modulo by the tile width/height to get the offsets within the tile */
132 ushort2 tile_mask_vec = (ushort2)(tile_w_px - 1, tile_h_px - 1);
133 uint32_t tile_mask = upsample(tile_mask_vec.y, tile_mask_vec.x);
134 uint32_t coord_xy = upsample(coord.y, coord.x);
135 ushort2 offs_px = as_ushort2(coord_xy & tile_mask);
136 uint32_t offset_within_tile_px = nir_interleave(offs_px.x, offs_px.y);
137
138 /* Get the coordinates of the corner of the tile */
139 ushort2 tile_px = as_ushort2(coord_xy & ~tile_mask);
140
141 /* tile row start (px) =
142 * (y // tile height) * (# of tiles/row) * (# of pix/tile) =
143 * align_down(y, tile height) / tile height * width_tl *tile width *
144 * tile height =
145 * align_down(y, tile height) * width_tl * tile width
146 */
147 uint32_t tile_row_start_px = tile_px.y * aligned_width_px;
148
149 /* tile column start (px) =
150 * (x // tile width) * (# of pix/tile) =
151 * align_down(x, tile width) / tile width * tile width * tile height =
152 * align_down(x, tile width) * tile height
153 */
154 uint32_t tile_col_start_px = tile_px.x * tile_h_px;
155
156 /* Get the total offset */
157 return tile_row_start_px + tile_col_start_px + offset_within_tile_px;
158 }
159
160 uint64_t
161 libagx_image_texel_address(constant const struct agx_pbe_packed *ptr,
162 uint4 coord, uint sample_idx,
163 uint bytes_per_sample_B, bool is_1d, bool is_msaa,
164 bool is_layered, bool return_index)
165 {
166 agx_unpack(NULL, ptr, PBE, d);
167
168 /* We do not allow atomics on linear 2D or linear 2D arrays, as there are no
169 * known use cases. So we're twiddled in this path, unless we're handling a
170 * 1D image which will be always linear, even if it uses a twiddled layout
171 * degrading to linear-equivalent 1x1 tiles. (1D uses this path, not the
172 * buffer path, for 1D arrays.)
173 */
174 uint total_px;
175 if (is_1d) {
176 total_px = coord.x;
177 } else {
178 uint aligned_width_px;
179 if (is_msaa) {
180 aligned_width_px = d.aligned_width_msaa_sw;
181 } else {
182 uint width_px = max(d.width >> d.level, 1u);
183 aligned_width_px = align(width_px, d.tile_width_sw);
184 }
185
186 total_px =
187 libagx_twiddle_coordinates(convert_ushort2(coord.xy), d.tile_width_sw,
188 d.tile_height_sw, aligned_width_px);
189 }
190
191 uint samples_log2 = is_msaa ? d.sample_count_log2_sw : 0;
192
193 if (is_layered) {
194 total_px += coord[is_1d ? 1 : 2] *
195 ((d.layer_stride_sw / bytes_per_sample_B) >> samples_log2);
196 }
197
198 uint total_sa = (total_px << samples_log2) + sample_idx;
199
200 if (return_index)
201 return total_sa;
202 else
203 return (d.buffer + (is_msaa ? 0 : d.level_offset_sw)) +
204 (uint64_t)total_sa * bytes_per_sample_B;
205 }
206
207 uint64_t
208 libagx_buffer_texel_address(constant const struct agx_pbe_packed *ptr,
209 uint4 coord, uint bytes_per_pixel_B)
210 {
211 agx_unpack(NULL, ptr, PBE, d);
212
213 uint32_t x_el = d.buffer_offset_sw + coord.x;
214 return d.buffer + (uint64_t)(x_el * bytes_per_pixel_B);
215 }
216
217 /* Buffer texture lowerings */
218 bool
219 libagx_texture_is_rgb32(constant struct agx_texture_packed *ptr)
220 {
221 agx_unpack(NULL, ptr, TEXTURE, d);
222 return d.channels == AGX_CHANNELS_R32G32B32_EMULATED;
223 }
224
225 uint4
226 libagx_texture_load_rgb32(constant struct agx_texture_packed *ptr, uint coord,
227 bool is_float)
228 {
229 agx_unpack(NULL, ptr, TEXTURE, d);
230
231 /* This is carefully written to let us do the * 3 with a 32-bit operation but
232 * still use the free 64-bit add-extend-shift for the rest.
233 */
234 uint64_t addr = d.address + ((uint64_t)(coord * 3)) * 4;
235 constant uint3 *data = (constant uint3 *)addr;
236
237 return (uint4)(*data, is_float ? as_uint(1.0f) : 1);
238 }
239
240 uint
241 libagx_buffer_texture_offset(constant struct agx_texture_packed *ptr, uint x)
242 {
243 agx_unpack(NULL, ptr, TEXTURE, d);
244
245 return x + d.buffer_offset_sw;
246 }
247
248 uint
249 libagx_buffer_image_offset(constant struct agx_pbe_packed *ptr, uint x)
250 {
251 agx_unpack(NULL, ptr, PBE, d);
252
253 return x + d.buffer_offset_sw;
254 }