X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=rans.shader;h=925543ef7dfa284d0fddbce411a61444438dbc9f;hb=dc3e3a2a88b2b1f0e177f613a8b355116e6020ff;hp=36feb657544e0fbfcb19b4e955bad5c3031ecf8c;hpb=86c0bf043378f95d1431990f378814af9fcfe6fd;p=narabu diff --git a/rans.shader b/rans.shader index 36feb65..925543e 100644 --- a/rans.shader +++ b/rans.shader @@ -3,7 +3,7 @@ layout(local_size_x = 1) in; -const uint prob_bits = 13; // Note! +const uint prob_bits = 12; const uint prob_scale = 1 << prob_bits; const uint RANS_BYTE_L = (1u << 23); const uint BLOCKS_PER_STREAM = 320; @@ -87,21 +87,25 @@ void RansEncFlush(uint rans, inout uint rans_offset) rans_output[rans_offset + 3] = uint8_t(rans >> 24); } -void encode_coeff(uint coeff, uint bits, uint sign_bias, inout RansEncoder enc) +int sign_extend(uint coeff, uint bits) +{ + return int(coeff << (32 - bits)) >> (32 - bits); +} + +void encode_coeff(int signed_k, uint sign_bias, inout RansEncoder enc) { - // Sign-extend to recover the coefficient. - // FIXME: not needed for the bits == 8 case! - int signed_k = int(coeff << (32 - bits)) >> (32 - bits); uint k = abs(signed_k); if (k >= ESCAPE_LIMIT) { - // ... boring stuff here + // Put the coefficient as a 1/(2^12) symbol _before_ + // the 255 coefficient, since the decoder will read the + // 255 coefficient first. RansEncPut(enc.rans, enc.rans_offset, k, 1, prob_bits); k = ESCAPE_LIMIT; } - uvec2 sym = ransdist[enc.lut_base + (k + NUM_SYMS - 1) & (NUM_SYMS - 1)]; - RansEncPut(enc.rans, enc.rans_offset, sym.x, sym.y, prob_bits); + uvec2 sym = ransdist[enc.lut_base + ((k - 1) & (NUM_SYMS - 1))]; + RansEncPut(enc.rans, enc.rans_offset, sym.x, sym.y, prob_bits + 1); if (signed_k < 0) { enc.rans += sign_bias; @@ -123,14 +127,14 @@ void encode_9_7(uint streamgroup_num, uint coeff_row, layout(r16ui) restrict rea 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 = BLOCKS_PER_STREAM; subblock_idx --> 0; ) { + 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(f & 0x1ffu, 9, sign_bias1, enc1); - encode_coeff(f >> 9, 7, sign_bias2, enc2); + encode_coeff(sign_extend(f & 0x1ffu, 9), sign_bias1, enc1); + encode_coeff(sign_extend(f >> 9, 7), sign_bias2, enc2); } encode_end(enc1); @@ -150,7 +154,7 @@ void encode_8(uint streamgroup_num, uint coeff_row, layout(r8i) restrict readonl uint y = (streamgroup_num * BLOCKS_PER_STREAM + subblock_idx) / 160; int f = imageLoad(tex, ivec2(x, y * 8 + coeff_row)).x; - encode_coeff(f, 8, sign_bias, enc); + encode_coeff(f, sign_bias, enc); } encode_end(enc);