Debian Codegraph @

src/panfrost/lib/pan_afbc.c

raw
1 /*
2 * Copyright © 2023-2025 Amazon.com, Inc. or its affiliates.
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "pan_afbc.h"
7 #include "pan_trace.h"
8 #include "util/detect_arch.h"
9 #include "util/u_cpu_detect.h"
10
11 #if DETECT_ARCH_AARCH64 || (DETECT_ARCH_ARM && !defined(__SOFTFP__))
12 /* armhf builds default to VFP, not NEON, and refuses to compile NEON
13 * intrinsics unless you tell it "no really". */
14 #if DETECT_ARCH_ARM
15 #pragma GCC target ("fpu=neon")
16 #endif
17 #include <arm_neon.h>
18 #endif
19
20 #if DETECT_ARCH_AARCH64
21
22 /* Arm A64 NEON intrinsics version. */
23 uint32_t
24 pan_afbc_payload_layout_packed(unsigned arch,
25 const struct pan_afbc_headerblock *headers,
26 struct pan_afbc_payload_extent *layout,
27 uint32_t nr_blocks, enum pipe_format format,
28 uint64_t modifier)
29 {
30 PAN_TRACE_FUNC(PAN_TRACE_LIB_AFBC);
31
32 uint32_t uncompressed_size =
33 pan_afbc_payload_uncompressed_size(format, modifier);
34 uint32_t body_size = 0;
35
36 alignas(16) static const uint8_t idx0[16] =
37 { 4, 5, 6, ~0, 7, 8, 9, ~0, 10, 11, 12, ~0, 13, 14, 15, ~0 };
38 alignas(16) static const uint8_t idx1[16] =
39 { 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60 };
40 alignas(16) static const uint8_t mask[16] =
41 { 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63 };
42 alignas(16) static const uint8_t ones[16] =
43 { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
44
45 uint8x16_t vidx0 = vld1q_u8(idx0);
46 uint8x16_t vidx1 = vld1q_u8(idx1);
47 uint8x16_t vmask = vld1q_u8(mask);
48 uint8x16_t vones = vld1q_u8(ones);
49
50 for (uint32_t i = 0; i < nr_blocks; ++i) {
51 uint32_t payload_size = 0;
52
53 /* Skip sum if the 1st subblock is 0 (solid color encoding). */
54 if (arch < 7 || headers[i].payload.subblock_sizes[0] & 0x3f) {
55 uint8x16_t vhdr = vld1q_u8((uint8_t *)&headers[i]);
56
57 /* Dispatch 6-bit packed 16 subblock sizes into 8-bit vector. */
58 uint8x16_t v0 = vqtbl1q_u8(vhdr, vidx0);
59 uint8x16_t v1 = vreinterpretq_u8_u32(
60 vshrq_n_u32(vreinterpretq_u32_u8(v0), 6));
61 uint8x16_t v2 = vreinterpretq_u8_u32(
62 vshrq_n_u32(vreinterpretq_u32_u8(v0), 12));
63 uint8x16_t v3 = vreinterpretq_u8_u32(
64 vshrq_n_u32(vreinterpretq_u32_u8(v0), 18));
65 uint8x16x4_t vtbl = {{ v0, v1, v2, v3 }};
66 v0 = vqtbl4q_u8(vtbl, vidx1);
67 v0 = vandq_u8(v0, vmask);
68
69 /* Sum across vector. */
70 payload_size = vaddlvq_u8(v0);
71
72 /* Number of subblocks of size 1. */
73 v0 = vceqq_u8(v0, vones);
74 v0 = vandq_u8(v0, vones);
75 uint32_t nr_ones = vaddvq_u8(v0);
76
77 /* Payload size already stores subblocks of size 1. Fix-up the sum
78 * using the number of such subblocks. */
79 payload_size += nr_ones * (uncompressed_size - 1);
80
81 payload_size = ALIGN_POT(payload_size, 16);
82 }
83
84 layout[i].size = payload_size;
85 layout[i].offset = body_size;
86 body_size += payload_size;
87 }
88
89 return body_size;
90 }
91
92 #else
93
94 /* Arm A32 NEON intrinsics and generic version. */
95 uint32_t
96 pan_afbc_payload_layout_packed(unsigned arch,
97 const struct pan_afbc_headerblock *headers,
98 struct pan_afbc_payload_extent *layout,
99 uint32_t nr_blocks, enum pipe_format format,
100 uint64_t modifier)
101 {
102 PAN_TRACE_FUNC(PAN_TRACE_LIB_AFBC);
103
104 uint32_t uncompressed_size =
105 pan_afbc_payload_uncompressed_size(format, modifier);
106 uint32_t body_size = 0;
107
108 #if DETECT_ARCH_ARM && !defined(__SOFTFP__)
109
110 if (unlikely(!util_get_cpu_caps()->has_neon))
111 goto no_neon;
112
113 /* Arm A32 NEON intrinsics version. */
114
115 alignas(16) static const uint8_t idx0[2][8] =
116 { { 4, 5, 6, ~0, 7, 8, 9, ~0 }, { 2, 3, 4, ~0, 5, 6, 7, ~0 } };
117 alignas(8) static const uint8_t idx1[8] =
118 { 0, 4, 8, 12, 16, 20, 24, 28 };
119 alignas(16) static const uint8_t mask[16] =
120 { 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63 };
121 alignas(16) static const uint8_t ones[16] =
122 { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
123
124 uint8x8_t vidx00 = vld1_u8(idx0[0]);
125 uint8x8_t vidx01 = vld1_u8(idx0[1]);
126 uint8x8_t vidx1 = vld1_u8(idx1);
127 uint8x16_t vmask = vld1q_u8(mask);
128 uint8x16_t vones = vld1q_u8(ones);
129
130 for (uint32_t i = 0; i < nr_blocks; ++i) {
131 uint32_t payload_size = 0;
132
133 /* Skip sum if the 1st subblock is 0 (solid color encoding). */
134 if (arch < 7 || headers[i].payload.subblock_sizes[0] & 0x3f) {
135 /* vld1_u8_x2() isn't widely available yet. */
136 uint8x8_t vhdr0 = vld1_u8(&headers[i].u8[0]);
137 uint8x8_t vhdr1 = vld1_u8(&headers[i].u8[8]);
138 uint8x8x2_t vhdr = {{ vhdr0, vhdr1 }};
139
140 /* Dispatch 6-bit packed 16 payload sizes into 8-bit vector. Note
141 * that the NEON TBL instr in A32 only supports doubleword operands
142 * while VSHR also supports quadword. Not sure how to mix doubleword
143 * and quadword intrinsics and get compilers to correctly alias D and
144 * Q registers though (128-bit register Q0 is an alias for the two
145 * consecutive 64-bit registers D0 and D1), so stick with doubleword
146 * intrinsics here. */
147 uint8x8_t v00 = vtbl2_u8(vhdr, vidx00);
148 uint8x8_t v01 = vtbl1_u8(vhdr1, vidx01);
149 uint8x8_t v10 = vreinterpret_u8_u32(
150 vshr_n_u32(vreinterpret_u32_u8(v00), 6));
151 uint8x8_t v11 = vreinterpret_u8_u32(
152 vshr_n_u32(vreinterpret_u32_u8(v01), 6));
153 uint8x8_t v20 = vreinterpret_u8_u32(
154 vshr_n_u32(vreinterpret_u32_u8(v00), 12));
155 uint8x8_t v21 = vreinterpret_u8_u32(
156 vshr_n_u32(vreinterpret_u32_u8(v01), 12));
157 uint8x8_t v30 = vreinterpret_u8_u32(
158 vshr_n_u32(vreinterpret_u32_u8(v00), 18));
159 uint8x8_t v31 = vreinterpret_u8_u32(
160 vshr_n_u32(vreinterpret_u32_u8(v01), 18));
161 uint8x8x4_t vtbl0 = {{ v00, v01, v10, v11 }};
162 uint8x8x4_t vtbl1 = {{ v20, v21, v30, v31 }};
163 v00 = vtbl4_u8(vtbl0, vidx1);
164 v01 = vtbl4_u8(vtbl1, vidx1);
165 uint8x16_t v0 = vandq_u8(vcombine_u8(v00, v01), vmask);
166
167 /* Sum across vector. */
168 uint64x2_t v1 = vpaddlq_u32(vpaddlq_u16(vpaddlq_u8(v0)));
169 payload_size = vget_lane_u64(vadd_u64(vget_low_u64(v1),
170 vget_high_u64(v1)), 0);
171
172 /* Number of subblocks of size 1. */
173 v0 = vceqq_u8(v0, vones);
174 v0 = vandq_u8(v0, vones);
175 v1 = vpaddlq_u32(vpaddlq_u16(vpaddlq_u8(v0)));
176 uint32_t nr_ones = vget_lane_u64(vadd_u64(vget_low_u64(v1),
177 vget_high_u64(v1)), 0);
178
179 /* Payload size already stores subblocks of size 1. Fix-up the sum
180 * using the number of such subblocks. */
181 payload_size += nr_ones * (uncompressed_size - 1);
182
183 payload_size = ALIGN_POT(payload_size, 16);
184 }
185
186 layout[i].size = payload_size;
187 layout[i].offset = body_size;
188 body_size += payload_size;
189 }
190
191 return body_size;
192
193 no_neon:
194 #endif
195
196 /* Generic version. */
197
198 /* XXX: It might be faster to copy the header from non-cacheable memory
199 * into a cacheline sized chunk in cacheable memory in order to avoid too
200 * many uncached transactions. Not sure though, so it needs testing. */
201
202 for (uint32_t i = 0; i < nr_blocks; i++) {
203 uint32_t payload_size =
204 pan_afbc_payload_size(arch, headers[i], uncompressed_size);
205 layout[i].size = payload_size;
206 layout[i].offset = body_size;
207 body_size += payload_size;
208 }
209
210 return body_size;
211 }
212
213 #endif