X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=rans.shader;fp=rans.shader;h=a7c6f8cce21abee5bca5cfbff55739ff0e323ef3;hb=5e1d27014149311318e97b8e04a6e05ec858e57c;hp=0000000000000000000000000000000000000000;hpb=daf421e32981645e551621551c6b82697ad078de;p=narabu diff --git a/rans.shader b/rans.shader new file mode 100644 index 0000000..a7c6f8c --- /dev/null +++ b/rans.shader @@ -0,0 +1,180 @@ +#version 440 +#extension GL_NV_gpu_shader5 : enable + +layout(local_size_x = 1) in; + +const uint prob_bits = 13; // Note! +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 NUM_SYMS = 256; +const uint ESCAPE_LIMIT = NUM_SYMS - 1; + +#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)) + +const uint luma_mapping[8] = { + MAPPING(0, 0, 1, 1, 2, 2, 3, 3), + MAPPING(0, 0, 1, 2, 2, 2, 3, 3), + MAPPING(1, 1, 2, 2, 2, 3, 3, 3), + MAPPING(1, 1, 2, 2, 2, 3, 3, 3), + MAPPING(1, 2, 2, 2, 2, 3, 3, 3), + MAPPING(2, 2, 2, 2, 3, 3, 3, 3), + MAPPING(2, 2, 3, 3, 3, 3, 3, 3), + 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[]; +}; + +layout(std430, binding = 11) buffer outputBuf2 +{ + uint rans_start_offset[]; +}; + +struct RansEncoder { + uint stream_num; // const + uint lut_base; // const + uint rans_offset; + uint rans; +}; + +layout(r16ui) uniform restrict readonly uimage2D dc_ac7_tex; +layout(r16ui) uniform restrict readonly uimage2D ac1_ac6_tex; +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 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.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); + } +} + +void RansEncPut(inout uint rans, inout uint rans_offset, uint start, uint freq, uint prob_bits) +{ + RansEncRenorm(rans, rans_offset, freq, prob_bits); + rans = ((rans / freq) << prob_bits) + (rans % freq) + start; +} + +void RansEncFlush(uint rans, inout uint rans_offset) +{ + 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); +} + +void encode_coeff(uint coeff, uint bits, 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 + RansEncPut(enc.rans, enc.rans_offset, 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); + + // fix some bias stuff here +} + +void encode_end(inout RansEncoder enc) +{ + RansEncFlush(enc.rans, enc.rans_offset); + rans_start_offset[enc.stream_num] = enc.rans_offset; +} + +void encode_9_7(uint streamgroup_num, uint coeff_row, layout(r16ui) restrict readonly uimage2D tex, uint col1, uint col2, uint dist1, uint dist2) +{ + RansEncoder enc1, enc2; + RansEncInit(streamgroup_num, coeff_row, col1, dist1, enc1); + RansEncInit(streamgroup_num, coeff_row, col2, dist2, enc2); + + for (uint subblock_idx = BLOCKS_PER_STREAM; subblock_idx --> 0; ) { + // 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, enc1); + encode_coeff(f >> 9, 7, enc2); + } + + encode_end(enc1); + encode_end(enc2); +} + +void encode_8(uint streamgroup_num, uint coeff_row, layout(r8i) restrict readonly iimage2D tex, uint col, uint dist) +{ + RansEncoder enc; + RansEncInit(streamgroup_num, coeff_row, col, dist, enc); + + for (uint subblock_idx = BLOCKS_PER_STREAM; subblock_idx --> 0; ) { + // 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, 8, enc); + } + + encode_end(enc); +} + +void main() +{ + uint streamgroup_num = gl_WorkGroupID.x; + uint coeff_row = gl_WorkGroupID.y; // 0..7 + uint coeff_colset = gl_WorkGroupID.z; // 0 = dc+ac7, 1 = ac1+ac6, 2 = ac2+ac5, 3 = ac3, 4 = ac5 + uint m = luma_mapping[coeff_row]; + + // TODO: DC coeff pred + + if (coeff_colset == 0) { + uint dist_dc = bitfieldExtract(m, 0, 2); + uint dist_ac7 = bitfieldExtract(m, 14, 2); + encode_9_7(streamgroup_num, coeff_row, dc_ac7_tex, 0, 7, dist_dc, dist_ac7); + } else if (coeff_colset == 1) { + uint dist_ac1 = bitfieldExtract(m, 2, 2); + uint dist_ac6 = bitfieldExtract(m, 12, 2); + encode_9_7(streamgroup_num, coeff_row, ac1_ac6_tex, 1, 6, dist_ac1, dist_ac6); + } else if (coeff_colset == 2) { + uint dist_ac2 = bitfieldExtract(m, 4, 2); + uint dist_ac5 = bitfieldExtract(m, 10, 2); + encode_9_7(streamgroup_num, coeff_row, ac2_ac5_tex, 2, 5, dist_ac2, dist_ac5); + } else if (coeff_colset == 3) { + uint dist_ac3 = bitfieldExtract(m, 6, 2); + encode_8(streamgroup_num, coeff_row, ac3_tex, 3, dist_ac3); + } else { + uint dist_ac4 = bitfieldExtract(m, 8, 2); + encode_8(streamgroup_num, coeff_row, ac4_tex, 4, dist_ac4); + } +}