X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=decoder.shader;h=012cbe3bbac0ec13bb40b0cf42bd9ecc4bdcadf0;hb=3fb87c6b953be3382cd216c74ff6aa025c8eaa2a;hp=cab6f8cbecbb508421b01740a208b431a8387136;hpb=2161c821c9c65ebae24eff9c2e4809c11c67cd02;p=narabu diff --git a/decoder.shader b/decoder.shader index cab6f8c..012cbe3 100644 --- a/decoder.shader +++ b/decoder.shader @@ -9,13 +9,15 @@ layout(local_size_x = 64*PARALLEL_SLICES) in; layout(r8ui) uniform restrict readonly uimage2D cum2sym_tex; layout(rg16ui) uniform restrict readonly uimage2D dsyms_tex; layout(r8) uniform restrict writeonly image2D out_tex; -layout(r16i) uniform restrict writeonly iimage2D coeff_tex; +layout(r32i) uniform restrict writeonly iimage2D coeff_tex; +layout(r32i) uniform restrict writeonly iimage2D coeff2_tex; uniform int num_blocks; const uint prob_bits = 12; const uint prob_scale = 1 << prob_bits; const uint NUM_SYMS = 256; const uint ESCAPE_LIMIT = NUM_SYMS - 1; +const uint BLOCKS_PER_STREAM = 320; // These need to be folded into quant_matrix. const float dc_scalefac = 8.0; @@ -70,40 +72,50 @@ layout(std430, binding = 0) buffer whatever3 }; uniform uint sign_bias_per_model[16]; -const uint RANS_BYTE_L = (1u << 23); // lower bound of our normalization interval +struct myuint64 { + uint high, low; +}; -uint get_rans_byte(uint offset) -{ - // We assume little endian. - return bitfieldExtract(data_SSBO[offset >> 2], 8 * int(offset & 3u), 8); -} +const uint RANS64_L = (1u << 31); // lower bound of our normalization interval -uint RansDecInit(inout uint offset) +myuint64 RansDecInit(inout uint offset) { - uint x; - - x = get_rans_byte(offset); - x |= get_rans_byte(offset + 1) << 8; - x |= get_rans_byte(offset + 2) << 16; - x |= get_rans_byte(offset + 3) << 24; - offset += 4; - + myuint64 x; + x.low = data_SSBO[offset++]; + x.high = data_SSBO[offset++]; return x; } -uint RansDecGet(uint r, uint scale_bits) +uint RansDecGet(myuint64 r, uint scale_bits) { - return r & ((1u << scale_bits) - 1); + return r.low & ((1u << scale_bits) - 1); } -void RansDecAdvance(inout uint rans, inout uint offset, const uint start, const uint freq, uint prob_bits) +void RansDecAdvance(inout myuint64 rans, inout uint offset, const uint start, const uint freq, uint prob_bits) { const uint mask = (1u << prob_bits) - 1; - rans = freq * (rans >> prob_bits) + (rans & mask) - start; - + const uint recovered_lowbits = (rans.low & mask) - start; + + // rans >>= prob_bits; + rans.low = (rans.low >> prob_bits) | ((rans.high & mask) << (32 - prob_bits)); + rans.high >>= prob_bits; + + // rans *= freq; + uint h1, l1, h2, l2; + umulExtended(rans.low, freq, h1, l1); + umulExtended(rans.high, freq, h2, l2); + rans.low = l1; + rans.high = l2 + h1; + + // rans += recovered_lowbits; + uint carry; + rans.low = uaddCarry(rans.low, recovered_lowbits, carry); + rans.high += carry; + // renormalize - while (rans < RANS_BYTE_L) { - rans = (rans << 8) | get_rans_byte(offset++); + if (rans.high == 0 && rans.low < RANS64_L) { + rans.high = rans.low; + rans.low = data_SSBO[offset++]; } } @@ -203,6 +215,8 @@ void main() local_timing[0] = start; #endif + const uint blocks_per_row = (imageSize(out_tex).x + 7) / 8; + const uint local_x = gl_LocalInvocationID.x % 8; const uint local_y = (gl_LocalInvocationID.x / 8) % 8; const uint local_z = gl_LocalInvocationID.x / 64; @@ -218,17 +232,17 @@ void main() const uint sign_bias = sign_bias_per_model[model_num]; // Initialize rANS decoder. - uint offset = streams[stream_num].src_offset; - uint rans = RansDecInit(offset); + uint offset = streams[stream_num].src_offset >> 2; + myuint64 rans = RansDecInit(offset); float q = (coeff_num == 0) ? 1.0 : (quant_matrix[coeff_num] * quant_scalefac / 128.0 / sqrt(2.0)); // FIXME: fold q *= (1.0 / 255.0); //int w = (coeff_num == 0) ? 32 : int(quant_matrix[coeff_num]); - int last_k = 0; + int last_k = 128; pick_timer(start, local_timing[0]); - for (uint block_idx = 40; block_idx --> 0; ) { + for (uint block_idx = BLOCKS_PER_STREAM / 8; block_idx --> 0; ) { pick_timer(start, local_timing[1]); // rANS decode one coefficient across eight blocks (so 64x8 coefficients). @@ -238,7 +252,7 @@ void main() bool sign = false; if (bottom_bits >= sign_bias) { bottom_bits -= sign_bias; - rans -= sign_bias; + rans.low -= sign_bias; sign = true; } int k = int(cum2sym(bottom_bits, model_num)); // Can go out-of-bounds; that will return zero. @@ -252,17 +266,19 @@ void main() if (sign) { k = -k; } +#if 0 + if (coeff_num == 0) { + //imageStore(coeff_tex, ivec2((block_row * 40 + block_idx) * 8 + subblock_idx, 0), ivec4(k, 0,0,0)); + imageStore(coeff_tex, ivec2((block_row * 40 + block_idx) * 8 + subblock_idx, 0), ivec4(rans.low, 0,0,0)); + imageStore(coeff2_tex, ivec2((block_row * 40 + block_idx) * 8 + subblock_idx, 0), ivec4(rans.high, 0,0,0)); + } +#endif if (coeff_num == 0) { k += last_k; last_k = k; } -#if 0 - uint y = block_row * 16 + block_y * 8 + local_y; - uint x = block_x * 64 + subblock_idx * 8 + local_x; - imageStore(coeff_tex, ivec2(x, y), ivec4(k, 0,0,0)); -#endif temp[slice_num * 64 * 8 + subblock_idx * 64 + coeff_num] = k * q; //temp[subblock_idx * 64 + 8 * y + x] = (2 * k * w * 4) / 32; // 100% matching unquant @@ -306,8 +322,8 @@ void main() pick_timer(start, local_timing[6]); uint global_block_idx = (block_row * 40 + block_idx) * 8 + local_y; - uint block_x = global_block_idx % 160; - uint block_y = global_block_idx / 160; + uint block_x = global_block_idx % blocks_per_row; + uint block_y = global_block_idx / blocks_per_row; uint y = block_y * 8; uint x = block_x * 8 + local_x;