]> git.sesse.net Git - narabu/blob - rans.shader
39efc52914bb579bb01208a870df2ac4301352cb
[narabu] / rans.shader
1 #version 440
2 #extension GL_NV_gpu_shader5 : enable
3
4 layout(local_size_x = 1) in;
5
6 const uint prob_bits = 12;
7 const uint prob_scale = 1 << prob_bits;
8 const uint RANS_BYTE_L = (1u << 23);
9 const uint BLOCKS_PER_STREAM = 320;
10 const uint STREAM_BUF_SIZE = 1024;  // 1 kB per stream ought to be enough for everyone :-)
11 const uint NUM_SYMS = 256;
12 const uint ESCAPE_LIMIT = NUM_SYMS - 1;
13
14 #define MAPPING(s0, s1, s2, s3, s4, s5, s6, s7) ((s0) | (s1 << 2) | (s2 << 4) | (s3 << 6) | (s4 << 8) | (s5 << 10) | (s6 << 12) | (s7 << 14))
15
16 const uint luma_mapping[8] = {
17         MAPPING(0, 0, 1, 1, 2, 2, 3, 3),
18         MAPPING(0, 0, 1, 2, 2, 2, 3, 3),
19         MAPPING(1, 1, 2, 2, 2, 3, 3, 3),
20         MAPPING(1, 1, 2, 2, 2, 3, 3, 3),
21         MAPPING(1, 2, 2, 2, 2, 3, 3, 3),
22         MAPPING(2, 2, 2, 2, 3, 3, 3, 3),
23         MAPPING(2, 2, 3, 3, 3, 3, 3, 3),
24         MAPPING(3, 3, 3, 3, 3, 3, 3, 3),
25 };
26
27 layout(std430, binding = 10) buffer outputBuf
28 {
29         uint8_t rans_output[];
30 };
31
32 layout(std430, binding = 11) buffer outputBuf2
33 {
34         uint rans_start_offset[];
35 };
36
37 layout(std140, binding = 13) uniform DistBlock
38 {
39         uvec4 ransdist[4 * 256];
40         uint sign_biases[4];
41 };
42
43 struct RansEncoder {
44         uint stream_num;   // const
45         uint lut_base;     // const
46         uint sign_bias;    // const
47         uint rans_offset;
48         uint rans;
49 };
50
51 layout(r16ui) uniform restrict readonly uimage2D dc_ac7_tex;
52 layout(r16ui) uniform restrict readonly uimage2D ac1_ac6_tex;
53 layout(r16ui) uniform restrict readonly uimage2D ac2_ac5_tex;
54 layout(r8i) uniform restrict readonly iimage2D ac3_tex;
55 layout(r8i) uniform restrict readonly iimage2D ac4_tex;
56
57 void RansPutByte(uint x, inout RansEncoder enc)
58 {
59         rans_output[--enc.rans_offset] = uint8_t(x);
60 }
61
62 void RansEncInit(uint streamgroup_num, uint coeff_row, uint coeff_col, uint dist_num, out RansEncoder enc)
63 {
64         enc.stream_num = streamgroup_num * 64 + coeff_row * 8 + coeff_col;
65         enc.lut_base = dist_num * 256;
66         enc.sign_bias = sign_biases[dist_num];
67         enc.rans_offset = enc.stream_num * STREAM_BUF_SIZE + STREAM_BUF_SIZE;  // Starts at the end.
68         enc.rans = RANS_BYTE_L;
69 }
70
71 void RansEncRenorm(inout RansEncoder enc, uint freq, uint prob_bits)
72 {
73         uint x_max = ((RANS_BYTE_L >> prob_bits) << 8) * freq; // this turns into a shift.
74         while (enc.rans >= x_max) {
75                 RansPutByte(enc.rans & 0xffu, enc);
76                 enc.rans >>= 8;
77         }
78 }
79
80 void RansEncPut(inout RansEncoder enc, uint start, uint freq, uint prob_bits)
81 {
82         RansEncRenorm(enc, freq, prob_bits);
83         enc.rans = ((enc.rans / freq) << prob_bits) + (enc.rans % freq) + start;
84 }
85
86 void RansEncPutSymbol(inout RansEncoder enc, uvec4 sym)
87 {
88         uint x_max = sym.x;
89         uint rcp_freq = sym.y;
90         uint bias = sym.z;
91         uint rcp_shift = (sym.w & 0xffffu);
92         uint cmpl_freq = (sym.w >> 16);
93
94         // renormalize
95         while (enc.rans >= x_max) {
96                 RansPutByte(enc.rans & 0xffu, enc);
97                 enc.rans >>= 8;
98         }
99
100         uint q, unused;
101         umulExtended(enc.rans, rcp_freq, q, unused);
102         enc.rans += bias + (q >> rcp_shift) * cmpl_freq;
103 }
104
105 void RansEncFlush(inout RansEncoder enc)
106 {
107         RansPutByte(enc.rans >> 24, enc);
108         RansPutByte(enc.rans >> 16, enc);
109         RansPutByte(enc.rans >> 8, enc);
110         RansPutByte(enc.rans >> 0, enc);
111 }
112
113 int sign_extend(uint coeff, uint bits)
114 {
115         return int(coeff << (32 - bits)) >> (32 - bits);
116 }
117
118 void encode_coeff(int signed_k, inout RansEncoder enc)
119 {
120         uint k = abs(signed_k);
121
122         if (k >= ESCAPE_LIMIT) {
123                 // Put the coefficient as a 1/(2^12) symbol _before_
124                 // the 255 coefficient, since the decoder will read the
125                 // 255 coefficient first.
126                 RansEncPut(enc, k, 1, prob_bits);
127                 k = ESCAPE_LIMIT;
128         }
129
130         uvec4 sym = ransdist[enc.lut_base + ((k - 1) & (NUM_SYMS - 1))];
131         RansEncPutSymbol(enc, sym);
132         
133         if (signed_k < 0) {
134                 enc.rans += enc.sign_bias;
135         }
136 }
137
138 void encode_end(inout RansEncoder enc)
139 {
140         RansEncFlush(enc);
141         rans_start_offset[enc.stream_num] = enc.rans_offset;
142 }
143
144 void encode_9_7(uint streamgroup_num, uint coeff_row, layout(r16ui) restrict readonly uimage2D tex, uint col1, uint col2, uint dist1, uint dist2)
145 {
146         RansEncoder enc1, enc2;
147         RansEncInit(streamgroup_num, coeff_row, col1, dist1, enc1);
148         RansEncInit(streamgroup_num, coeff_row, col2, dist2, enc2);
149
150         for (uint subblock_idx = 0; subblock_idx < BLOCKS_PER_STREAM; ++subblock_idx) {
151                 // TODO: Use SSBOs instead of a texture?
152                 uint x = (streamgroup_num * BLOCKS_PER_STREAM + subblock_idx) % 160;
153                 uint y = (streamgroup_num * BLOCKS_PER_STREAM + subblock_idx) / 160;
154                 uint f = imageLoad(tex, ivec2(x, y * 8 + coeff_row)).x;
155
156                 encode_coeff(sign_extend(f & 0x1ffu, 9), enc1);
157                 encode_coeff(sign_extend(f >> 9, 7), enc2);
158         }
159
160         encode_end(enc1);
161         encode_end(enc2);
162 }
163
164 void encode_8(uint streamgroup_num, uint coeff_row, layout(r8i) restrict readonly iimage2D tex, uint col, uint dist)
165 {
166         RansEncoder enc;
167         RansEncInit(streamgroup_num, coeff_row, col, dist, enc);
168
169         for (uint subblock_idx = 0; subblock_idx < BLOCKS_PER_STREAM; ++subblock_idx) {
170                 // TODO: Use SSBOs instead of a texture?
171                 uint x = (streamgroup_num * BLOCKS_PER_STREAM + subblock_idx) % 160;
172                 uint y = (streamgroup_num * BLOCKS_PER_STREAM + subblock_idx) / 160;
173                 int f = imageLoad(tex, ivec2(x, y * 8 + coeff_row)).x;
174
175                 encode_coeff(f, enc);
176         }
177
178         encode_end(enc);
179 }
180
181 void main()
182 {
183         uint streamgroup_num = gl_WorkGroupID.x;
184         uint coeff_row = gl_WorkGroupID.y;    // 0..7
185         uint coeff_colset = gl_WorkGroupID.z;   // 0 = dc+ac7, 1 = ac1+ac6, 2 = ac2+ac5, 3 = ac3, 4 = ac5
186         uint m = luma_mapping[coeff_row];
187
188         // TODO: DC coeff pred
189
190         if (coeff_colset == 0) {
191                 uint dist_dc = bitfieldExtract(m, 0, 2);
192                 uint dist_ac7 = bitfieldExtract(m, 14, 2);
193                 encode_9_7(streamgroup_num, coeff_row, dc_ac7_tex, 0, 7, dist_dc, dist_ac7);
194         } else if (coeff_colset == 1) {
195                 uint dist_ac1 = bitfieldExtract(m, 2, 2);
196                 uint dist_ac6 = bitfieldExtract(m, 12, 2);
197                 encode_9_7(streamgroup_num, coeff_row, ac1_ac6_tex, 1, 6, dist_ac1, dist_ac6);
198         } else if (coeff_colset == 2) {
199                 uint dist_ac2 = bitfieldExtract(m, 4, 2);
200                 uint dist_ac5 = bitfieldExtract(m, 10, 2);
201                 encode_9_7(streamgroup_num, coeff_row, ac2_ac5_tex, 2, 5, dist_ac2, dist_ac5);
202         } else if (coeff_colset == 3) {
203                 uint dist_ac3 = bitfieldExtract(m, 6, 2);
204                 encode_8(streamgroup_num, coeff_row, ac3_tex, 3, dist_ac3);
205         } else {
206                 uint dist_ac4 = bitfieldExtract(m, 8, 2);
207                 encode_8(streamgroup_num, coeff_row, ac4_tex, 4, dist_ac4);
208         }
209 }