+
+ // Generate the first mip-map from the data
+ append_payload_to_mipmap();
+}
+
+void LogicDataSnapshot::reallocate_mip_map(MipMapLevel &m)
+{
+ const uint64_t new_data_length = ((m.length + MipMapDataUnit - 1) /
+ MipMapDataUnit) * MipMapDataUnit;
+ if(new_data_length > m.data_length)
+ {
+ m.data_length = new_data_length;
+ m.data = realloc(m.data, new_data_length * _unit_size);
+ }
+}
+
+void LogicDataSnapshot::append_payload_to_mipmap()
+{
+ MipMapLevel &m0 = _mip_map[0];
+ uint64_t prev_length;
+ const uint8_t *src_ptr;
+ uint8_t *dest_ptr;
+ uint64_t accumulator;
+ unsigned int diff_counter;
+
+ // Expand the data buffer to fit the new samples
+ prev_length = m0.length;
+ m0.length = _sample_count / MipMapScaleFactor;
+
+ // Break off if there are no new samples to compute
+ if(m0.length == prev_length)
+ return;
+
+ reallocate_mip_map(m0);
+
+ dest_ptr = (uint8_t*)m0.data + prev_length * _unit_size;
+
+ // Iterate through the samples to populate the first level mipmap
+ accumulator = 0;
+ diff_counter = MipMapScaleFactor;
+ const uint8_t *end_src_ptr = (uint8_t*)_data +
+ m0.length * _unit_size * MipMapScaleFactor;
+ for(src_ptr = (uint8_t*)_data +
+ prev_length * _unit_size * MipMapScaleFactor;
+ src_ptr < end_src_ptr;)
+ {
+ // Accumulate transitions which have occurred in this sample
+ accumulator = 0;
+ diff_counter = MipMapScaleFactor;
+ while(diff_counter-- > 0)
+ {
+ const uint64_t sample = *(uint64_t*)src_ptr;
+ accumulator |= _last_append_sample ^ sample;
+ _last_append_sample = sample;
+ src_ptr += _unit_size;
+ }
+
+ *(uint64_t*)dest_ptr = accumulator;
+ dest_ptr += _unit_size;
+ }
+
+ // Compute higher level mipmaps
+ for(int level = 1; level < ScaleStepCount; level++)
+ {
+ MipMapLevel &m = _mip_map[level];
+ const MipMapLevel &ml = _mip_map[level-1];
+
+ // Expand the data buffer to fit the new samples
+ prev_length = m.length;
+ m.length = ml.length / MipMapScaleFactor;
+
+ // Break off if there are no more samples to computed
+ if(m.length == prev_length)
+ break;
+
+ reallocate_mip_map(m);
+
+ // Subsample the level lower level
+ src_ptr = (uint8_t*)ml.data +
+ _unit_size * prev_length * MipMapScaleFactor;
+ const uint8_t *end_dest_ptr =
+ (uint8_t*)m.data + _unit_size * m.length;
+ for(dest_ptr = (uint8_t*)m.data +
+ _unit_size * prev_length;
+ dest_ptr < end_dest_ptr;
+ dest_ptr += _unit_size)
+ {
+ accumulator = 0;
+ diff_counter = MipMapScaleFactor;
+ while(diff_counter-- > 0)
+ {
+ accumulator |= *(uint64_t*)src_ptr;
+ src_ptr += _unit_size;
+ }
+
+ *(uint64_t*)dest_ptr = accumulator;
+ }
+ }