Skip to content

Commit

Permalink
vertexcodec: Add tracing for bit consistency
Browse files Browse the repository at this point in the history
Within each byte group, we count the number of consistent bits: bits
that, in that byte group, all match the corresponding bit from the last
vertex. Consistent bits amplify compression rates when they are properly
aligned.

Note that this is not exactly the same as having small deltas, eg 0xff
vs 0x00 is still consistent from the delta perspective in most high
bits, but it would be the same if we used xor deltas (which may be an
option worth having).

This is also *not* the same as having bits consistent between
consecutive bytes: in a very random sequence, about half of the bits in
each position usually match from one value to the next, but that's a
very low consistency as it's hard to take advantage of.
  • Loading branch information
zeux committed Oct 3, 2024
1 parent ae76704 commit e71f4aa
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/vertexcodec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ struct Stats
size_t size;
size_t header; // bytes for header
size_t bitg[4]; // bytes for bit groups
size_t bitc[8]; // bit consistency: how many bits are shared between all bytes in a group
};

static Stats* bytestats = NULL;
Expand Down Expand Up @@ -317,6 +318,22 @@ static unsigned char* encodeVertexBlock(unsigned char* data, unsigned char* data
#if TRACE
const unsigned char* olddata = data;
bytestats = &vertexstats[k];

for (size_t ig = 0; ig < vertex_count; ig += kByteGroupSize)
{
unsigned char last = (ig == 0) ? last_vertex[k] : vertex_data[vertex_size * (ig - 1) + k];
unsigned char delta = 0xff;
size_t count = 0;

for (size_t i = ig; i < ig + kByteGroupSize && i < vertex_count; ++i)
{
delta &= ~(vertex_data[vertex_size * i + k] ^ last);
count++;
}

for (int j = 0; j < 8; ++j)
bytestats->bitc[j] += count * ((delta >> j) & 1);
}
#endif

data = encodeBytes(data, data_end, buffer, (vertex_count + kByteGroupSize - 1) & ~(kByteGroupSize - 1));
Expand Down Expand Up @@ -1203,10 +1220,14 @@ size_t meshopt_encodeVertexBuffer(unsigned char* buffer, size_t buffer_size, con

printf("\t\thdr %5.1f%%; bitg 0 [%4.1f%%] 1 [%4.1f%%] 2 [%4.1f%%] 3 [%4.1f%%]",
double(vsk.header) / double(total_k) * 100,
double(vsk.bitg[0]) / double(total_k) * 100,
double(vsk.bitg[1]) / double(total_k) * 100,
double(vsk.bitg[2]) / double(total_k) * 100,
double(vsk.bitg[3]) / double(total_k) * 100);
double(vsk.bitg[0]) / double(total_k) * 100, double(vsk.bitg[1]) / double(total_k) * 100,
double(vsk.bitg[2]) / double(total_k) * 100, double(vsk.bitg[3]) / double(total_k) * 100);

printf("\tbitc [%4.0f%% %4.0f%% %4.0f%% %4.0f%% %4.0f%% %4.0f%% %4.0f%% %4.0f%%]",
double(vsk.bitc[0]) / double(vertex_count) * 100, double(vsk.bitc[1]) / double(vertex_count) * 100,
double(vsk.bitc[2]) / double(vertex_count) * 100, double(vsk.bitc[3]) / double(vertex_count) * 100,
double(vsk.bitc[4]) / double(vertex_count) * 100, double(vsk.bitc[5]) / double(vertex_count) * 100,
double(vsk.bitc[6]) / double(vertex_count) * 100, double(vsk.bitc[7]) / double(vertex_count) * 100);
printf("\n");
}
#endif
Expand Down

0 comments on commit e71f4aa

Please sign in to comment.