]> git.sesse.net Git - narabu/commitdiff
Encapsulate things a bit better in rans.shader.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Tue, 17 Oct 2017 21:28:25 +0000 (23:28 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Tue, 17 Oct 2017 21:28:25 +0000 (23:28 +0200)
rans.shader

index 965c6744d8e1151fc0f05eae9ec1dd5af660f60c..39efc52914bb579bb01208a870df2ac4301352cb 100644 (file)
@@ -54,6 +54,11 @@ 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;
@@ -63,22 +68,22 @@ void RansEncInit(uint streamgroup_num, uint coeff_row, uint coeff_col, uint dist
        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.
-       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 +92,22 @@ 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)
+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)
@@ -119,12 +123,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,7 +137,7 @@ void encode_coeff(int signed_k, inout RansEncoder enc)
 
 void encode_end(inout RansEncoder enc)
 {
-       RansEncFlush(enc.rans, enc.rans_offset);
+       RansEncFlush(enc);
        rans_start_offset[enc.stream_num] = enc.rans_offset;
 }