]> git.sesse.net Git - narabu/blob - rans.shader
Implement sign_bias in the rANS GPU encoder. Still not working properly.
[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 = 13;  // Note!
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 = 9) buffer layoutName
28 {
29         uint dist[4 * 256];
30         uvec2 ransdist[4 * 256];
31 };
32
33 layout(std430, binding = 10) buffer outputBuf
34 {
35         uint8_t rans_output[];
36 };
37
38 layout(std430, binding = 11) buffer outputBuf2
39 {
40         uint rans_start_offset[];
41 };
42
43 struct RansEncoder {
44         uint stream_num;   // const
45         uint lut_base;     // const
46         uint rans_offset;
47         uint rans;
48 };
49
50 layout(r16ui) uniform restrict readonly uimage2D dc_ac7_tex;
51 layout(r16ui) uniform restrict readonly uimage2D ac1_ac6_tex;
52 layout(r16ui) uniform restrict readonly uimage2D ac2_ac5_tex;
53 layout(r8i) uniform restrict readonly iimage2D ac3_tex;
54 layout(r8i) uniform restrict readonly iimage2D ac4_tex;
55
56 void RansEncInit(uint streamgroup_num, uint coeff_row, uint coeff_col, uint dist_num, out RansEncoder enc)
57 {
58         enc.stream_num = streamgroup_num * 64 + coeff_row * 8 + coeff_col;
59         enc.lut_base = dist_num * 256;
60         enc.rans_offset = enc.stream_num * STREAM_BUF_SIZE + STREAM_BUF_SIZE;  // Starts at the end.
61         enc.rans = RANS_BYTE_L;
62 }
63
64 void RansEncRenorm(inout uint rans, inout uint rans_offset, uint freq, uint prob_bits)
65 {
66         uint x_max = ((RANS_BYTE_L >> prob_bits) << 8) * freq; // this turns into a shift.
67         if (rans >= x_max) {
68                 do {
69                         rans_output[--rans_offset] = uint8_t(rans & 0xff);
70                         rans >>= 8;
71                 } while (rans >= x_max);
72         }
73 }
74
75 void RansEncPut(inout uint rans, inout uint rans_offset, uint start, uint freq, uint prob_bits)
76 {
77         RansEncRenorm(rans, rans_offset, freq, prob_bits);
78         rans = ((rans / freq) << prob_bits) + (rans % freq) + start;
79 }
80
81 void RansEncFlush(uint rans, inout uint rans_offset)
82 {
83         rans_offset -= 4;
84         rans_output[rans_offset + 0] = uint8_t(rans >> 0);
85         rans_output[rans_offset + 1] = uint8_t(rans >> 8);
86         rans_output[rans_offset + 2] = uint8_t(rans >> 16);
87         rans_output[rans_offset + 3] = uint8_t(rans >> 24);
88 }
89
90 void encode_coeff(uint coeff, uint bits, uint sign_bias, inout RansEncoder enc)
91 {
92         // Sign-extend to recover the coefficient.
93         // FIXME: not needed for the bits == 8 case!
94         int signed_k = int(coeff << (32 - bits)) >> (32 - bits);
95         uint k = abs(signed_k);
96
97         if (k >= ESCAPE_LIMIT) {
98                 // ... boring stuff here
99                 RansEncPut(enc.rans, enc.rans_offset, k, 1, prob_bits);
100                 k = ESCAPE_LIMIT;
101         }
102
103         uvec2 sym = ransdist[enc.lut_base + (k + NUM_SYMS - 1) & (NUM_SYMS - 1)];
104         RansEncPut(enc.rans, enc.rans_offset, sym.x, sym.y, prob_bits);
105         
106         if (signed_k < 0) {
107                 enc.rans += sign_bias;
108         }
109 }
110
111 void encode_end(inout RansEncoder enc)
112 {
113         RansEncFlush(enc.rans, enc.rans_offset);
114         rans_start_offset[enc.stream_num] = enc.rans_offset;
115 }
116
117 void encode_9_7(uint streamgroup_num, uint coeff_row, layout(r16ui) restrict readonly uimage2D tex, uint col1, uint col2, uint dist1, uint dist2)
118 {
119         RansEncoder enc1, enc2;
120         RansEncInit(streamgroup_num, coeff_row, col1, dist1, enc1);
121         RansEncInit(streamgroup_num, coeff_row, col2, dist2, enc2);
122
123         uint sign_bias1 = ransdist[enc1.lut_base + 255].x + ransdist[enc1.lut_base + 255].y;
124         uint sign_bias2 = ransdist[enc2.lut_base + 255].x + ransdist[enc2.lut_base + 255].y;
125
126         for (uint subblock_idx = BLOCKS_PER_STREAM; subblock_idx --> 0; ) {
127                 // TODO: Use SSBOs instead of a texture?
128                 uint x = (streamgroup_num * BLOCKS_PER_STREAM + subblock_idx) % 160;
129                 uint y = (streamgroup_num * BLOCKS_PER_STREAM + subblock_idx) / 160;
130                 uint f = imageLoad(tex, ivec2(x, y * 8 + coeff_row)).x;
131
132                 encode_coeff(f & 0x1ffu, 9, sign_bias1, enc1);
133                 encode_coeff(f >> 9, 7, sign_bias2, enc2);
134         }
135
136         encode_end(enc1);
137         encode_end(enc2);
138 }
139
140 void encode_8(uint streamgroup_num, uint coeff_row, layout(r8i) restrict readonly iimage2D tex, uint col, uint dist)
141 {
142         RansEncoder enc;
143         RansEncInit(streamgroup_num, coeff_row, col, dist, enc);
144
145         uint sign_bias = ransdist[enc.lut_base + 255].x + ransdist[enc.lut_base + 255].y;
146
147         for (uint subblock_idx = BLOCKS_PER_STREAM; subblock_idx --> 0; ) {
148                 // TODO: Use SSBOs instead of a texture?
149                 uint x = (streamgroup_num * BLOCKS_PER_STREAM + subblock_idx) % 160;
150                 uint y = (streamgroup_num * BLOCKS_PER_STREAM + subblock_idx) / 160;
151                 int f = imageLoad(tex, ivec2(x, y * 8 + coeff_row)).x;
152
153                 encode_coeff(f, 8, sign_bias, enc);
154         }
155
156         encode_end(enc);
157 }
158
159 void main()
160 {
161         uint streamgroup_num = gl_WorkGroupID.x;
162         uint coeff_row = gl_WorkGroupID.y;    // 0..7
163         uint coeff_colset = gl_WorkGroupID.z;   // 0 = dc+ac7, 1 = ac1+ac6, 2 = ac2+ac5, 3 = ac3, 4 = ac5
164         uint m = luma_mapping[coeff_row];
165
166         // TODO: DC coeff pred
167
168         if (coeff_colset == 0) {
169                 uint dist_dc = bitfieldExtract(m, 0, 2);
170                 uint dist_ac7 = bitfieldExtract(m, 14, 2);
171                 encode_9_7(streamgroup_num, coeff_row, dc_ac7_tex, 0, 7, dist_dc, dist_ac7);
172         } else if (coeff_colset == 1) {
173                 uint dist_ac1 = bitfieldExtract(m, 2, 2);
174                 uint dist_ac6 = bitfieldExtract(m, 12, 2);
175                 encode_9_7(streamgroup_num, coeff_row, ac1_ac6_tex, 1, 6, dist_ac1, dist_ac6);
176         } else if (coeff_colset == 2) {
177                 uint dist_ac2 = bitfieldExtract(m, 4, 2);
178                 uint dist_ac5 = bitfieldExtract(m, 10, 2);
179                 encode_9_7(streamgroup_num, coeff_row, ac2_ac5_tex, 2, 5, dist_ac2, dist_ac5);
180         } else if (coeff_colset == 3) {
181                 uint dist_ac3 = bitfieldExtract(m, 6, 2);
182                 encode_8(streamgroup_num, coeff_row, ac3_tex, 3, dist_ac3);
183         } else {
184                 uint dist_ac4 = bitfieldExtract(m, 8, 2);
185                 encode_8(streamgroup_num, coeff_row, ac4_tex, 4, dist_ac4);
186         }
187 }