Skip to content

Commit 8605ff9

Browse files
committed
meshletcodec: Simplify vertex encoding a little further
Instead of explicitly enumerating all options, merge the branches a little further. This mostly just makes the code shorter, with no real impact on performance. Also fix case of hexadecimal constants, clarify precedence in a couple places and add an assertion to decodeMeshletSimd to ensure tables have been properly initialized even when it's called unconditionally.
1 parent af0f6bb commit 8605ff9

File tree

1 file changed

+15
-25
lines changed

1 file changed

+15
-25
lines changed

src/meshletcodec.cpp

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -193,34 +193,21 @@ static size_t encodeVertices(unsigned char* ctrl, unsigned char* data, const uns
193193
unsigned int d = vertices[i] - last - 1;
194194
unsigned int v = (d << 1) ^ (int(d) >> 31);
195195

196-
int code = 0;
196+
// 0/1/2/4 bytes per value
197+
int code = v == 0 ? 0 : (v < 256 ? 1 : (v < 65536 ? 2 : 3));
197198

198-
if (v == 0)
199-
; // 0 bytes
200-
else if (v < 256)
201-
{
202-
// 1 byte
203-
code = 1;
204-
*data++ = (unsigned char)v;
205-
}
206-
else if (v < 65536)
207-
{
208-
// 2 bytes
209-
code = 2;
210-
*data++ = (unsigned char)(v & 0xFF);
211-
*data++ = (unsigned char)((v >> 8) & 0xFF);
212-
}
213-
else
199+
if (code > 0)
200+
*data++ = (unsigned char)(v & 0xff);
201+
if (code > 1)
202+
*data++ = (unsigned char)((v >> 8) & 0xff);
203+
204+
if (code > 2)
214205
{
215-
// 4 bytes
216-
code = 3;
217-
*data++ = (unsigned char)(v & 0xFF);
218-
*data++ = (unsigned char)((v >> 8) & 0xFF);
219-
*data++ = (unsigned char)((v >> 16) & 0xFF);
220-
*data++ = (unsigned char)((v >> 24) & 0xFF);
206+
*data++ = (unsigned char)((v >> 16) & 0xff);
207+
*data++ = (unsigned char)((v >> 24) & 0xff);
221208
}
222209

223-
ctrl[i / 4] |= (code << ((i % 4) * 2));
210+
ctrl[i / 4] |= code << ((i % 4) * 2);
224211

225212
last = vertices[i];
226213
}
@@ -303,7 +290,7 @@ static const unsigned char* decodeVertices(unsigned int* vertices, const unsigne
303290
if (data > bound)
304291
return NULL;
305292

306-
unsigned char code = ctrl[i / 4] >> ((i % 4) * 2) & 3;
293+
unsigned char code = (ctrl[i / 4] >> ((i % 4) * 2)) & 3;
307294

308295
// branchlessly read up to 4 bytes
309296
unsigned int v = (data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24)) & kMasks[code];
@@ -556,6 +543,9 @@ SIMD_TARGET static const unsigned char* decodeVerticesSimd(unsigned int* vertice
556543

557544
SIMD_TARGET static int decodeMeshletSimd(unsigned int* vertices, unsigned int* triangles, const unsigned char* codes, const unsigned char* ctrl, const unsigned char* data, const unsigned char* bound, size_t vertex_count, size_t triangle_count)
558545
{
546+
assert(gDecodeTablesInitialized);
547+
(void)gDecodeTablesInitialized;
548+
559549
// decodes 4 vertices at a time; last group may be partial, but:
560550
// - we can write 4 vertices to the output because the caller has to provide output buffers aligned to 4 elements
561551
// - the remaining control data is 0 in valid encodings so data will not be advanced beyond bound

0 commit comments

Comments
 (0)