X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=rans.shader;h=39efc52914bb579bb01208a870df2ac4301352cb;hb=fedc6308264085999789854152c9577ddfe1bc6e;hp=5b13d7ea75e25c2d63f486fb59a8c5bc54019d00;hpb=f11496643290d0f3413ec603c5d9ef0e3416f7ec;p=narabu diff --git a/rans.shader b/rans.shader index 5b13d7e..39efc52 100644 --- a/rans.shader +++ b/rans.shader @@ -24,12 +24,6 @@ const uint luma_mapping[8] = { MAPPING(3, 3, 3, 3, 3, 3, 3, 3), }; -layout(std430, binding = 9) buffer layoutName -{ - uint dist[4 * 256]; - uvec2 ransdist[4 * 256]; -}; - layout(std430, binding = 10) buffer outputBuf { uint8_t rans_output[]; @@ -40,9 +34,16 @@ layout(std430, binding = 11) buffer outputBuf2 uint rans_start_offset[]; }; +layout(std140, binding = 13) uniform DistBlock +{ + uvec4 ransdist[4 * 256]; + uint sign_biases[4]; +}; + struct RansEncoder { uint stream_num; // const uint lut_base; // const + uint sign_bias; // const uint rans_offset; uint rans; }; @@ -53,38 +54,60 @@ layout(r16ui) uniform restrict readonly uimage2D ac2_ac5_tex; layout(r8i) uniform restrict readonly iimage2D ac3_tex; layout(r8i) uniform restrict readonly iimage2D ac4_tex; +void RansPutByte(uint x, inout RansEncoder enc) +{ + rans_output[--enc.rans_offset] = uint8_t(x); +} + void RansEncInit(uint streamgroup_num, uint coeff_row, uint coeff_col, uint dist_num, out RansEncoder enc) { 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) +void RansEncRenorm(inout RansEncoder enc, 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 (enc.rans >= x_max) { + RansPutByte(enc.rans & 0xffu, enc); + enc.rans >>= 8; } } -void RansEncPut(inout uint rans, inout uint rans_offset, uint start, uint freq, uint prob_bits) +void RansEncPut(inout RansEncoder enc, uint start, uint freq, uint prob_bits) +{ + RansEncRenorm(enc, freq, prob_bits); + enc.rans = ((enc.rans / freq) << prob_bits) + (enc.rans % freq) + start; +} + +void RansEncPutSymbol(inout RansEncoder enc, uvec4 sym) { - RansEncRenorm(rans, rans_offset, freq, prob_bits); - rans = ((rans / freq) << prob_bits) + (rans % freq) + start; + 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 + while (enc.rans >= x_max) { + RansPutByte(enc.rans & 0xffu, enc); + enc.rans >>= 8; + } + + uint q, unused; + umulExtended(enc.rans, rcp_freq, q, unused); + enc.rans += bias + (q >> rcp_shift) * cmpl_freq; } -void RansEncFlush(uint rans, inout uint rans_offset) +void RansEncFlush(inout RansEncoder enc) { - rans_offset -= 4; - rans_output[rans_offset + 0] = uint8_t(rans >> 0); - rans_output[rans_offset + 1] = uint8_t(rans >> 8); - rans_output[rans_offset + 2] = uint8_t(rans >> 16); - rans_output[rans_offset + 3] = uint8_t(rans >> 24); + RansPutByte(enc.rans >> 24, enc); + RansPutByte(enc.rans >> 16, enc); + RansPutByte(enc.rans >> 8, enc); + RansPutByte(enc.rans >> 0, enc); } int sign_extend(uint coeff, uint bits) @@ -92,7 +115,7 @@ 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) +void encode_coeff(int signed_k, inout RansEncoder enc) { uint k = abs(signed_k); @@ -100,21 +123,21 @@ void encode_coeff(int signed_k, uint sign_bias, inout RansEncoder enc) // 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); + RansEncPut(enc, k, 1, prob_bits); 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, sym); if (signed_k < 0) { - enc.rans += sign_bias; + enc.rans += enc.sign_bias; } } void encode_end(inout RansEncoder enc) { - RansEncFlush(enc.rans, enc.rans_offset); + RansEncFlush(enc); rans_start_offset[enc.stream_num] = enc.rans_offset; } @@ -124,17 +147,14 @@ void encode_9_7(uint streamgroup_num, uint coeff_row, layout(r16ui) restrict rea 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); @@ -146,15 +166,13 @@ void encode_8(uint streamgroup_num, uint coeff_row, layout(r8i) restrict readonl 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);