X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=rans.shader;h=47d0c8b7f9ad2a5c4cbac0ee959b0efd7adbe5ca;hb=c6928acb2405c30fdaf1a5d9efeb2902d7c544aa;hp=965c6744d8e1151fc0f05eae9ec1dd5af660f60c;hpb=1982179eccafd16269939ae5a5d4a467f68b26fd;p=narabu diff --git a/rans.shader b/rans.shader index 965c674..47d0c8b 100644 --- a/rans.shader +++ b/rans.shader @@ -1,5 +1,4 @@ #version 440 -#extension GL_NV_gpu_shader5 : enable layout(local_size_x = 1) in; @@ -7,9 +6,10 @@ 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; -const uint STREAM_BUF_SIZE = 1024; // 1 kB per stream ought to be enough for everyone :-) +const uint STREAM_BUF_SIZE = 256; // In uint32s. 1 kB per stream ought to be enough for everyone :-) const uint NUM_SYMS = 256; const uint ESCAPE_LIMIT = NUM_SYMS - 1; +const uint WIDTH_BLOCKS = 160; // TODO: send in as a uniform. #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)) @@ -26,12 +26,12 @@ const uint luma_mapping[8] = { layout(std430, binding = 10) buffer outputBuf { - uint8_t rans_output[]; + uint rans_output[]; }; layout(std430, binding = 11) buffer outputBuf2 { - uint rans_start_offset[]; + uint rans_bytes_written[]; }; layout(std140, binding = 13) uniform DistBlock @@ -41,11 +41,14 @@ layout(std140, binding = 13) uniform DistBlock }; struct RansEncoder { - uint stream_num; // const - uint lut_base; // const - uint sign_bias; // const + uint stream_num; // const + uint lut_base; // const + uint sign_bias; // const + uint rans_start_offset; // const uint rans_offset; uint rans; + uint bit_buffer; + uint bytes_in_buffer; }; layout(r16ui) uniform restrict readonly uimage2D dc_ac7_tex; @@ -54,31 +57,43 @@ 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) +{ + enc.bit_buffer = (enc.bit_buffer << 8) | x; + if (++enc.bytes_in_buffer == 4) { + rans_output[--enc.rans_offset] = enc.bit_buffer; + enc.bytes_in_buffer = 0; + } +} + 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_start_offset = enc.rans_offset; enc.rans = RANS_BYTE_L; + enc.bit_buffer = 0; + enc.bytes_in_buffer = 0; } -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. - while (rans >= x_max) { - rans_output[--rans_offset] = uint8_t(rans & 0xffu); - rans >>= 8; + 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(rans, rans_offset, freq, prob_bits); - rans = ((rans / freq) << prob_bits) + (rans % freq) + start; + RansEncRenorm(enc, freq, prob_bits); + enc.rans = ((enc.rans / freq) << prob_bits) + (enc.rans % freq) + start; } -void RansEncPutSymbol(inout uint rans, inout uint rans_offset, uvec4 sym) +void RansEncPutSymbol(inout RansEncoder enc, uvec4 sym) { uint x_max = sym.x; uint rcp_freq = sym.y; @@ -87,23 +102,31 @@ void RansEncPutSymbol(inout uint rans, inout uint rans_offset, uvec4 sym) uint cmpl_freq = (sym.w >> 16); // renormalize - while (rans >= x_max) { - rans_output[--rans_offset] = uint8_t(rans & 0xffu); - rans >>= 8; + while (enc.rans >= x_max) { + RansPutByte(enc.rans & 0xffu, enc); + enc.rans >>= 8; } uint q, unused; - umulExtended(rans, rcp_freq, q, unused); - rans += bias + (q >> rcp_shift) * cmpl_freq; + umulExtended(enc.rans, rcp_freq, q, unused); + enc.rans += bias + (q >> rcp_shift) * cmpl_freq; } -void RansEncFlush(uint rans, inout uint rans_offset) +uint 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); + + uint num_bytes_written = (enc.rans_start_offset - enc.rans_offset) * 4 + enc.bytes_in_buffer; + + // Make sure there's nothing left in the buffer. + RansPutByte(0, enc); + RansPutByte(0, enc); + RansPutByte(0, enc); + + return num_bytes_written; } int sign_extend(uint coeff, uint bits) @@ -119,12 +142,12 @@ void encode_coeff(int signed_k, 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; } uvec4 sym = ransdist[enc.lut_base + ((k - 1) & (NUM_SYMS - 1))]; - RansEncPutSymbol(enc.rans, enc.rans_offset, sym); + RansEncPutSymbol(enc, sym); if (signed_k < 0) { enc.rans += enc.sign_bias; @@ -133,8 +156,8 @@ void encode_coeff(int signed_k, inout RansEncoder enc) void encode_end(inout RansEncoder enc) { - RansEncFlush(enc.rans, enc.rans_offset); - rans_start_offset[enc.stream_num] = enc.rans_offset; + uint bytes_written = RansEncFlush(enc); + rans_bytes_written[enc.stream_num] = bytes_written; } 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,8 +168,8 @@ void encode_9_7(uint streamgroup_num, uint coeff_row, layout(r16ui) restrict rea 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 x = (streamgroup_num * BLOCKS_PER_STREAM + subblock_idx) % WIDTH_BLOCKS; + uint y = (streamgroup_num * BLOCKS_PER_STREAM + subblock_idx) / WIDTH_BLOCKS; uint f = imageLoad(tex, ivec2(x, y * 8 + coeff_row)).x; encode_coeff(sign_extend(f & 0x1ffu, 9), enc1); @@ -164,8 +187,8 @@ void encode_8(uint streamgroup_num, uint coeff_row, layout(r8i) restrict readonl 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 x = (streamgroup_num * BLOCKS_PER_STREAM + subblock_idx) % WIDTH_BLOCKS; + uint y = (streamgroup_num * BLOCKS_PER_STREAM + subblock_idx) / WIDTH_BLOCKS; int f = imageLoad(tex, ivec2(x, y * 8 + coeff_row)).x; encode_coeff(f, enc);