]> git.sesse.net Git - narabu/blob - decoder.shader
Add some parallel slicing code (not really a win).
[narabu] / decoder.shader
1 #version 440
2 #extension GL_ARB_shader_clock : enable
3
4 #define PARALLEL_SLICES 1
5
6 #define ENABLE_TIMING 0
7
8 layout(local_size_x = 64*PARALLEL_SLICES) in;
9 layout(r8ui) uniform restrict readonly uimage2D cum2sym_tex;
10 layout(rg16ui) uniform restrict readonly uimage2D dsyms_tex;
11 layout(r8) uniform restrict writeonly image2D out_tex;
12
13 const uint prob_bits = 12;
14 const uint prob_scale = 1 << prob_bits;
15 const uint NUM_SYMS = 256;
16 const uint ESCAPE_LIMIT = NUM_SYMS - 1;
17
18 // These need to be folded into quant_matrix.
19 const float dc_scalefac = 8.0;
20 const float quant_scalefac = 4.0;
21
22 const float quant_matrix[64] = {
23          8, 16, 19, 22, 26, 27, 29, 34,
24         16, 16, 22, 24, 27, 29, 34, 37,
25         19, 22, 26, 27, 29, 34, 34, 38,
26         22, 22, 26, 27, 29, 34, 37, 40,
27         22, 26, 27, 29, 32, 35, 40, 48,
28         26, 27, 29, 32, 35, 40, 48, 58,
29         26, 27, 29, 34, 38, 46, 56, 69,
30         27, 29, 35, 38, 46, 56, 69, 83
31 };
32 const uint ff_zigzag_direct[64] = {
33     0,   1,  8, 16,  9,  2,  3, 10,
34     17, 24, 32, 25, 18, 11,  4,  5,
35     12, 19, 26, 33, 40, 48, 41, 34,
36     27, 20, 13,  6,  7, 14, 21, 28,
37     35, 42, 49, 56, 57, 50, 43, 36,
38     29, 22, 15, 23, 30, 37, 44, 51,
39     58, 59, 52, 45, 38, 31, 39, 46,
40     53, 60, 61, 54, 47, 55, 62, 63
41 };
42
43 layout(std430, binding = 9) buffer layoutName
44 {
45         uint data_SSBO[];
46 };
47 layout(std430, binding = 10) buffer layoutName2
48 {
49         uvec2 timing[10 * 64];
50 };
51
52 struct CoeffStream {
53         uint src_offset, src_len;
54 };
55 layout(std430, binding = 0) buffer whatever3
56 {
57         CoeffStream streams[];
58 };
59 uniform uint sign_bias_per_model[16];
60
61 const uint RANS_BYTE_L = (1u << 23);  // lower bound of our normalization interval
62
63 uint get_rans_byte(uint offset)
64 {
65         // We assume little endian.
66         return bitfieldExtract(data_SSBO[offset >> 2], 8 * int(offset & 3u), 8);
67 }
68
69 uint RansDecInit(inout uint offset)
70 {
71         uint x;
72
73         x  = get_rans_byte(offset);
74         x |= get_rans_byte(offset + 1) << 8;
75         x |= get_rans_byte(offset + 2) << 16;
76         x |= get_rans_byte(offset + 3) << 24;
77         offset += 4;
78
79         return x;
80 }
81
82 uint RansDecGet(uint r, uint scale_bits)
83 {
84         return r & ((1u << scale_bits) - 1);
85 }
86
87 void RansDecAdvance(inout uint rans, inout uint offset, const uint start, const uint freq, uint prob_bits)
88 {
89         const uint mask = (1u << prob_bits) - 1;
90         rans = freq * (rans >> prob_bits) + (rans & mask) - start;
91         
92         // renormalize
93         while (rans < RANS_BYTE_L) {
94                 rans = (rans << 8) | get_rans_byte(offset++);
95         }
96 }
97
98 uint cum2sym(uint bits, uint table)
99 {
100         return imageLoad(cum2sym_tex, ivec2(bits, table)).x;
101 }
102
103 uvec2 get_dsym(uint k, uint table)
104 {
105         return imageLoad(dsyms_tex, ivec2(k, table)).xy;
106 }
107
108 void idct_1d(inout float y0, inout float y1, inout float y2, inout float y3, inout float y4, inout float y5, inout float y6, inout float y7)
109 {
110         const float a1 = 0.7071067811865474;   // sqrt(2)
111         const float a2 = 0.5411961001461971;   // cos(3/8 pi) * sqrt(2)
112         const float a4 = 1.3065629648763766;   // cos(pi/8) * sqrt(2)
113         // static const float a5 = 0.5 * (a4 - a2);
114         const float a5 = 0.3826834323650897;
115
116         // phase 2 (phase 1 is just moving around)
117         const float p2_4 = y5 - y3;
118         const float p2_5 = y1 + y7;
119         const float p2_6 = y1 - y7;
120         const float p2_7 = y5 + y3;
121
122         // phase 3
123         const float p3_2 = y2 - y6;
124         const float p3_3 = y2 + y6;
125         const float p3_5 = p2_5 - p2_7;
126         const float p3_7 = p2_5 + p2_7;
127
128         // phase 4
129         const float p4_2 = a1 * p3_2;
130         const float p4_4 = p2_4 * a2 + (p2_4 + p2_6) * a5;  // Inverted.
131         const float p4_5 = a1 * p3_5;
132         const float p4_6 = p2_6 * a4 - (p2_4 + p2_6) * a5;
133
134         // phase 5
135         const float p5_0 = y0 + y4;
136         const float p5_1 = y0 - y4;
137         const float p5_3 = p4_2 + p3_3;
138
139         // phase 6
140         const float p6_0 = p5_0 + p5_3;
141         const float p6_1 = p5_1 + p4_2;
142         const float p6_2 = p5_1 - p4_2;
143         const float p6_3 = p5_0 - p5_3;
144         const float p6_5 = p4_5 + p4_4;
145         const float p6_6 = p4_5 + p4_6;
146         const float p6_7 = p4_6 + p3_7;
147
148         // phase 7
149         y0 = p6_0 + p6_7;
150         y1 = p6_1 + p6_6;
151         y2 = p6_2 + p6_5;
152         y3 = p6_3 - p4_4;
153         y4 = p6_3 + p4_4;
154         y5 = p6_2 - p6_5;
155         y6 = p6_1 - p6_6;
156         y7 = p6_0 - p6_7;
157 }
158
159 shared float temp[64 * 8 * PARALLEL_SLICES];
160
161 void pick_timer(inout uvec2 start, inout uvec2 t)
162 {
163 #if ENABLE_TIMING
164         uvec2 now = clock2x32ARB();
165
166         uvec2 delta = now - start;
167         if (now.x < start.x) {
168                 --delta.y;
169         }
170
171         uvec2 new_t = t + delta;
172         if (new_t.x < t.x) {
173                 ++new_t.y;
174         }
175         t = new_t;
176
177         start = clock2x32ARB();
178 #endif
179 }
180
181 void main()
182 {
183         uvec2 local_timing[10];
184 #if ENABLE_TIMING
185         for (int timer_idx = 0; timer_idx < 10; ++timer_idx) {
186                 local_timing[timer_idx] = uvec2(0, 0);
187         }
188         uvec2 start = clock2x32ARB();
189 #else
190         uvec2 start = uvec2(0, 0);
191         local_timing[0] = start;
192 #endif
193
194         const uint local_x = gl_LocalInvocationID.x % 8;
195         const uint local_y = (gl_LocalInvocationID.x / 8) % 8;
196         const uint local_z = gl_LocalInvocationID.x / 64;
197
198         const uint num_blocks = 720 / 16;  // FIXME: make a uniform
199         const uint slice_num = local_z;
200         const uint thread_num = local_y * 8 + local_x;
201
202         const uint block_row = gl_WorkGroupID.y * PARALLEL_SLICES + slice_num;
203         //const uint coeff_num = ff_zigzag_direct[thread_num];
204         const uint coeff_num = thread_num;
205         const uint stream_num = coeff_num * num_blocks + block_row;
206         const uint model_num = min((coeff_num % 8) + (coeff_num / 8), 7);
207         const uint sign_bias = sign_bias_per_model[model_num];
208
209         // Initialize rANS decoder.
210         uint offset = streams[stream_num].src_offset;
211         uint rans = RansDecInit(offset);
212
213         float q = (coeff_num == 0) ? 1.0 : (quant_matrix[coeff_num] * quant_scalefac / 128.0 / sqrt(2.0));  // FIXME: fold
214         q *= (1.0 / 255.0);
215         //int w = (coeff_num == 0) ? 32 : int(quant_matrix[coeff_num]);
216         int last_k = 0;
217
218         pick_timer(start, local_timing[0]);
219
220         for (uint block_idx = 40; block_idx --> 0; ) {
221                 uint block_x = block_idx % 20;
222                 uint block_y = block_idx / 20;
223                 if (block_x == 19) last_k = 0;
224
225                 pick_timer(start, local_timing[1]);
226
227                 // rANS decode one coefficient across eight blocks (so 64x8 coefficients).
228                 for (uint subblock_idx = 8; subblock_idx --> 0; ) {
229                         // Read a symbol.
230                         uint bottom_bits = RansDecGet(rans, prob_bits + 1);
231                         bool sign = false;
232                         if (bottom_bits >= sign_bias) {
233                                 bottom_bits -= sign_bias;
234                                 rans -= sign_bias;
235                                 sign = true;
236                         }
237                         int k = int(cum2sym(bottom_bits, model_num));  // Can go out-of-bounds; that will return zero.
238                         uvec2 sym = get_dsym(k, model_num);
239                         RansDecAdvance(rans, offset, sym.x, sym.y, prob_bits + 1);
240
241                         if (k == ESCAPE_LIMIT) {
242                                 k = int(RansDecGet(rans, prob_bits));
243                                 RansDecAdvance(rans, offset, k, 1, prob_bits);
244                         }
245                         if (sign) {
246                                 k = -k;
247                         }
248
249                         if (coeff_num == 0) {
250                                 k += last_k;
251                                 last_k = k;
252                         }
253
254                         temp[slice_num * 64 * 8 + subblock_idx * 64 + coeff_num] = k * q;
255                         //temp[subblock_idx * 64 + 8 * y + x] = (2 * k * w * 4) / 32;  // 100% matching unquant
256                 }
257
258                 pick_timer(start, local_timing[2]);
259
260                 memoryBarrierShared();
261                 barrier();
262
263                 pick_timer(start, local_timing[3]);
264
265                 // Horizontal DCT one row (so 64 rows).
266                 idct_1d(temp[slice_num * 64 * 8 + thread_num * 8 + 0],
267                         temp[slice_num * 64 * 8 + thread_num * 8 + 1],
268                         temp[slice_num * 64 * 8 + thread_num * 8 + 2],
269                         temp[slice_num * 64 * 8 + thread_num * 8 + 3],
270                         temp[slice_num * 64 * 8 + thread_num * 8 + 4],
271                         temp[slice_num * 64 * 8 + thread_num * 8 + 5],
272                         temp[slice_num * 64 * 8 + thread_num * 8 + 6],
273                         temp[slice_num * 64 * 8 + thread_num * 8 + 7]);
274
275                 pick_timer(start, local_timing[4]);
276
277                 memoryBarrierShared();
278                 barrier();
279
280                 pick_timer(start, local_timing[5]);
281
282                 // Vertical DCT one row (so 64 columns).
283                 uint row_offset = local_z * 64 * 8 + local_y * 64 + local_x;
284                 idct_1d(temp[row_offset + 0 * 8],
285                         temp[row_offset + 1 * 8],
286                         temp[row_offset + 2 * 8],
287                         temp[row_offset + 3 * 8],
288                         temp[row_offset + 4 * 8],
289                         temp[row_offset + 5 * 8],
290                         temp[row_offset + 6 * 8],
291                         temp[row_offset + 7 * 8]);
292
293                 pick_timer(start, local_timing[6]);
294
295                 uint y = block_row * 16 + block_y * 8;
296                 uint x = block_x * 64 + local_y * 8 + local_x;
297                 for (uint yl = 0; yl < 8; ++yl) {
298                         imageStore(out_tex, ivec2(x, yl + y), vec4(temp[row_offset + yl * 8], 0.0, 0.0, 1.0));
299                 }
300
301                 pick_timer(start, local_timing[7]);
302
303                 memoryBarrierShared();  // is this needed?
304                 barrier();
305
306                 pick_timer(start, local_timing[8]);
307                 pick_timer(start, local_timing[9]);  // should be nearly nothing
308         }
309
310 #if ENABLE_TIMING
311         for (int timer_idx = 0; timer_idx < 10; ++timer_idx) {
312                 uint global_idx = thread_num * 10 + timer_idx;
313
314                 uint old_val = atomicAdd(timing[global_idx].x, local_timing[timer_idx].x);
315                 if (old_val + local_timing[timer_idx].x < old_val) {
316                         ++local_timing[timer_idx].y;
317                 }
318                 atomicAdd(timing[global_idx].y, local_timing[timer_idx].y);
319         }
320 #endif
321 }