layout(std430, binding = 9) buffer layoutName
{
uint dist[4 * 256];
- uvec2 ransdist[4 * 256];
+ uint ransfreq[4 * 256];
+ uvec4 ransdist[4 * 256];
+ uint sign_biases[4];
};
layout(std430, binding = 10) buffer outputBuf
struct RansEncoder {
uint stream_num; // const
uint lut_base; // const
+ uint sign_bias; // const
uint rans_offset;
uint rans;
};
{
enc.stream_num = streamgroup_num * 64 + coeff_row * 8 + coeff_col;
enc.lut_base = dist_num * 256;
+ enc.sign_bias = sign_biases[dist_num];
enc.rans_offset = enc.stream_num * STREAM_BUF_SIZE + STREAM_BUF_SIZE; // Starts at the end.
enc.rans = RANS_BYTE_L;
}
void RansEncRenorm(inout uint rans, inout uint rans_offset, uint freq, uint prob_bits)
{
uint x_max = ((RANS_BYTE_L >> prob_bits) << 8) * freq; // this turns into a shift.
- if (rans >= x_max) {
- do {
- rans_output[--rans_offset] = uint8_t(rans & 0xff);
- rans >>= 8;
- } while (rans >= x_max);
+ while (rans >= x_max) {
+ rans_output[--rans_offset] = uint8_t(rans & 0xffu);
+ rans >>= 8;
}
}
rans = ((rans / freq) << prob_bits) + (rans % freq) + start;
}
+void RansEncPutSymbol(inout uint rans, inout uint rans_offset, uvec4 sym)
+{
+ uint x_max = sym.x;
+ uint rcp_freq = sym.y;
+ uint bias = sym.z;
+ uint rcp_shift = (sym.w & 0xffffu);
+ uint cmpl_freq = (sym.w >> 16);
+
+ // renormalize
+ if (rans >= x_max) {
+ do {
+ rans_output[--rans_offset] = uint8_t(rans & 0xffu);
+ rans >>= 8;
+ } while (rans >= x_max);
+ }
+
+ uint q, unused;
+ umulExtended(rans, rcp_freq, q, unused);
+ rans += bias + (q >> rcp_shift) * cmpl_freq;
+}
+
void RansEncFlush(uint rans, inout uint rans_offset)
{
rans_offset -= 4;
return int(coeff << (32 - bits)) >> (32 - bits);
}
-void encode_coeff(int signed_k, uint sign_bias, inout RansEncoder enc)
+void encode_coeff(int signed_k, inout RansEncoder enc)
{
uint k = abs(signed_k);
k = ESCAPE_LIMIT;
}
- uvec2 sym = ransdist[enc.lut_base + ((k - 1) & (NUM_SYMS - 1))];
- RansEncPut(enc.rans, enc.rans_offset, sym.x, sym.y, prob_bits + 1);
+ uvec4 sym = ransdist[enc.lut_base + ((k - 1) & (NUM_SYMS - 1))];
+ RansEncPutSymbol(enc.rans, enc.rans_offset, sym);
if (signed_k < 0) {
- enc.rans += sign_bias;
+ enc.rans += enc.sign_bias;
}
}
RansEncInit(streamgroup_num, coeff_row, col1, dist1, enc1);
RansEncInit(streamgroup_num, coeff_row, col2, dist2, enc2);
- uint sign_bias1 = ransdist[enc1.lut_base + 255].x + ransdist[enc1.lut_base + 255].y;
- uint sign_bias2 = ransdist[enc2.lut_base + 255].x + ransdist[enc2.lut_base + 255].y;
-
for (uint subblock_idx = 0; subblock_idx < BLOCKS_PER_STREAM; ++subblock_idx) {
// TODO: Use SSBOs instead of a texture?
uint x = (streamgroup_num * BLOCKS_PER_STREAM + subblock_idx) % 160;
uint y = (streamgroup_num * BLOCKS_PER_STREAM + subblock_idx) / 160;
uint f = imageLoad(tex, ivec2(x, y * 8 + coeff_row)).x;
- encode_coeff(sign_extend(f & 0x1ffu, 9), sign_bias1, enc1);
- encode_coeff(sign_extend(f >> 9, 7), sign_bias2, enc2);
+ encode_coeff(sign_extend(f & 0x1ffu, 9), enc1);
+ encode_coeff(sign_extend(f >> 9, 7), enc2);
}
encode_end(enc1);
RansEncoder enc;
RansEncInit(streamgroup_num, coeff_row, col, dist, enc);
- uint sign_bias = ransdist[enc.lut_base + 255].x + ransdist[enc.lut_base + 255].y;
-
for (uint subblock_idx = 0; subblock_idx < BLOCKS_PER_STREAM; ++subblock_idx) {
// TODO: Use SSBOs instead of a texture?
uint x = (streamgroup_num * BLOCKS_PER_STREAM + subblock_idx) % 160;
uint y = (streamgroup_num * BLOCKS_PER_STREAM + subblock_idx) / 160;
int f = imageLoad(tex, ivec2(x, y * 8 + coeff_row)).x;
- encode_coeff(f, sign_bias, enc);
+ encode_coeff(f, enc);
}
encode_end(enc);