]> git.sesse.net Git - narabu/commitdiff
Make rans.shader write uint32s, shedding the GL_NV_gpu_shader5 demand.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Tue, 17 Oct 2017 21:38:32 +0000 (23:38 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Tue, 17 Oct 2017 21:38:32 +0000 (23:38 +0200)
narabu-encoder.cpp
rans.shader

index 36804d616f70681f411af412961a23ac87a542b4..fe270641634652debd37edba61470d585da5fbf8 100644 (file)
@@ -227,10 +227,10 @@ int main(int argc, char **argv)
        glNamedBufferStorage(output_ssbo, 45 * 64 * 1024, nullptr, GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT);
        check_error();
 
        glNamedBufferStorage(output_ssbo, 45 * 64 * 1024, nullptr, GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT);
        check_error();
 
-       GLuint output_offset_ssbo;
-       glGenBuffers(1, &output_offset_ssbo);
-       glBindBuffer(GL_SHADER_STORAGE_BUFFER, output_offset_ssbo);
-       glNamedBufferStorage(output_offset_ssbo, 45 * 64 * sizeof(uint32_t), nullptr, GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT);
+       GLuint bytes_written_ssbo;
+       glGenBuffers(1, &bytes_written_ssbo);
+       glBindBuffer(GL_SHADER_STORAGE_BUFFER, bytes_written_ssbo);
+       glNamedBufferStorage(bytes_written_ssbo, 45 * 64 * sizeof(uint32_t), nullptr, GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT);
        check_error();
 
        // Bind SSBOs.
        check_error();
 
        // Bind SSBOs.
@@ -243,7 +243,7 @@ int main(int argc, char **argv)
 
        glUseProgram(glsl_rans_program_num);
        glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 10, output_ssbo);
 
        glUseProgram(glsl_rans_program_num);
        glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 10, output_ssbo);
-       glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 11, output_offset_ssbo);
+       glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 11, bytes_written_ssbo);
        glBindBufferBase(GL_UNIFORM_BUFFER, 13, dist_ubo);
 
        glUseProgram(glsl_program_num);
        glBindBufferBase(GL_UNIFORM_BUFFER, 13, dist_ubo);
 
        glUseProgram(glsl_program_num);
@@ -383,9 +383,8 @@ int main(int argc, char **argv)
        }
 
        // Write out the actual data.
        }
 
        // Write out the actual data.
-       // TODO: Do the deduplication.
 
 
-       const uint32_t *offsets = (const uint32_t *)glMapNamedBufferRange(output_offset_ssbo, 0, 45 * 64 * sizeof(uint32_t), GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT);
+       const uint32_t *bytes_written = (const uint32_t *)glMapNamedBufferRange(bytes_written_ssbo, 0, 45 * 64 * sizeof(uint32_t), GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT);
 #if 0
        for (int i = 0; i < 45*64; ++i) {
                printf("%d,%d,%d: %u\n", i / 64, (i / 8) % 8, i % 8, 1024 * (i + 1) - offsets[i]);
 #if 0
        for (int i = 0; i < 45*64; ++i) {
                printf("%d,%d,%d: %u\n", i / 64, (i / 8) % 8, i % 8, 1024 * (i + 1) - offsets[i]);
@@ -399,8 +398,8 @@ int main(int argc, char **argv)
                for (unsigned x = 0; x < 8; ++x) {
                        for (unsigned int stream_idx = 0; stream_idx < 45; ++stream_idx) {
                                const uint8_t *out_end = data + (stream_idx * 64 + y * 8 + x + 1) * 1024;
                for (unsigned x = 0; x < 8; ++x) {
                        for (unsigned int stream_idx = 0; stream_idx < 45; ++stream_idx) {
                                const uint8_t *out_end = data + (stream_idx * 64 + y * 8 + x + 1) * 1024;
-                               const uint8_t *ptr = data + offsets[stream_idx * 64 + y * 8 + x];
-                               uint32_t num_rans_bytes = out_end - ptr;
+                               uint32_t num_rans_bytes = bytes_written[stream_idx * 64 + y * 8 + x];
+                               const uint8_t *ptr = out_end - num_rans_bytes;
                                assert(num_rans_bytes <= 1024);
 
                                if (num_rans_bytes == last_block.size() &&
                                assert(num_rans_bytes <= 1024);
 
                                if (num_rans_bytes == last_block.size() &&
index 39efc52914bb579bb01208a870df2ac4301352cb..3e19c83eae83e4f31f6c0c47bd4f0bc4263a7cc5 100644 (file)
@@ -1,5 +1,4 @@
 #version 440
 #version 440
-#extension GL_NV_gpu_shader5 : enable
 
 layout(local_size_x = 1) in;
 
 
 layout(local_size_x = 1) in;
 
@@ -7,7 +6,7 @@ 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 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 NUM_SYMS = 256;
 const uint ESCAPE_LIMIT = NUM_SYMS - 1;
 
@@ -26,12 +25,12 @@ const uint luma_mapping[8] = {
 
 layout(std430, binding = 10) buffer outputBuf
 {
 
 layout(std430, binding = 10) buffer outputBuf
 {
-       uint8_t rans_output[];
+       uint rans_output[];
 };
 
 layout(std430, binding = 11) buffer outputBuf2
 {
 };
 
 layout(std430, binding = 11) buffer outputBuf2
 {
-       uint rans_start_offset[];
+       uint rans_bytes_written[];
 };
 
 layout(std140, binding = 13) uniform DistBlock
 };
 
 layout(std140, binding = 13) uniform DistBlock
@@ -41,11 +40,14 @@ layout(std140, binding = 13) uniform DistBlock
 };
 
 struct RansEncoder {
 };
 
 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 rans_offset;
        uint rans;
+       uint bit_buffer;
+       uint bytes_in_buffer;
 };
 
 layout(r16ui) uniform restrict readonly uimage2D dc_ac7_tex;
 };
 
 layout(r16ui) uniform restrict readonly uimage2D dc_ac7_tex;
@@ -56,7 +58,11 @@ layout(r8i) uniform restrict readonly iimage2D ac4_tex;
 
 void RansPutByte(uint x, inout RansEncoder enc)
 {
 
 void RansPutByte(uint x, inout RansEncoder enc)
 {
-       rans_output[--enc.rans_offset] = uint8_t(x);
+       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)
 }
 
 void RansEncInit(uint streamgroup_num, uint coeff_row, uint coeff_col, uint dist_num, out RansEncoder enc)
@@ -65,7 +71,10 @@ void RansEncInit(uint streamgroup_num, uint coeff_row, uint coeff_col, uint dist
        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.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.rans = RANS_BYTE_L;
+       enc.bit_buffer = 0;
+       enc.bytes_in_buffer = 0;
 }
 
 void RansEncRenorm(inout RansEncoder enc, uint freq, uint prob_bits)
 }
 
 void RansEncRenorm(inout RansEncoder enc, uint freq, uint prob_bits)
@@ -102,12 +111,21 @@ void RansEncPutSymbol(inout RansEncoder enc, uvec4 sym)
        enc.rans += bias + (q >> rcp_shift) * cmpl_freq;
 }
 
        enc.rans += bias + (q >> rcp_shift) * cmpl_freq;
 }
 
-void RansEncFlush(inout RansEncoder enc)
+uint RansEncFlush(inout RansEncoder enc)
 {
        RansPutByte(enc.rans >> 24, enc);
        RansPutByte(enc.rans >> 16, enc);
        RansPutByte(enc.rans >> 8, enc);
        RansPutByte(enc.rans >> 0, enc);
 {
        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)
 }
 
 int sign_extend(uint coeff, uint bits)
@@ -137,8 +155,8 @@ void encode_coeff(int signed_k, inout RansEncoder enc)
 
 void encode_end(inout RansEncoder enc)
 {
 
 void encode_end(inout RansEncoder enc)
 {
-       RansEncFlush(enc);
-       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)
 }
 
 void encode_9_7(uint streamgroup_num, uint coeff_row, layout(r16ui) restrict readonly uimage2D tex, uint col1, uint col2, uint dist1, uint dist2)