Skip to content

Commit

Permalink
tools: Add codectest to make it easy to test attribute encoder
Browse files Browse the repository at this point in the history
This is useful when trying to compress binary vertex data blobs as well
as to check the output data with a separate compressor using piping, so
we use stdin and stdout instead of named files.
  • Loading branch information
zeux committed Sep 27, 2024
1 parent b0e689f commit be4d566
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,9 @@ codecbench.wasm: tools/codecbench.cpp $(LIBRARY_SOURCES)
codecbench-simd.wasm: tools/codecbench.cpp $(LIBRARY_SOURCES)
$(WASMCC) $^ -fno-exceptions --target=wasm32-wasi --sysroot=$(WASIROOT) -lc++ -lc++abi -O3 -g -DNDEBUG -msimd128 -o $@

codectest: tools/codectest.cpp $(LIBRARY)
$(CXX) $^ $(CXXFLAGS) $(LDFLAGS) -o $@

codecfuzz: tools/codecfuzz.cpp src/vertexcodec.cpp src/indexcodec.cpp
$(CXX) $^ -fsanitize=fuzzer,address,undefined -O1 -g -o $@

Expand Down
41 changes: 41 additions & 0 deletions tools/codectest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "../src/meshoptimizer.h"

#include <vector>

#include <stdio.h>
#include <stdlib.h>

#ifdef _WIN32
#include <fcntl.h>
#include <io.h>
#endif

int main(int argc, char** argv)
{
#ifdef _WIN32
_setmode(_fileno(stdin), _O_BINARY);
_setmode(_fileno(stdout), _O_BINARY);
#endif

if (argc != 2 || atoi(argv[1]) <= 0)
{
fprintf(stderr, "Usage: %s <stride>\n", argv[0]);
return 1;
}

size_t stride = atoi(argv[1]);

std::vector<unsigned char> input;
unsigned char buffer[4096];
size_t bytes_read;

while ((bytes_read = fread(buffer, 1, sizeof(buffer), stdin)) > 0)
input.insert(input.end(), buffer, buffer + bytes_read);

size_t vertex_count = input.size() / stride;
std::vector<unsigned char> output(meshopt_encodeVertexBufferBound(vertex_count, stride));
size_t output_size = meshopt_encodeVertexBuffer(output.data(), output.size(), input.data(), vertex_count, stride);

fwrite(output.data(), 1, output_size, stdout);
return 0;
}

0 comments on commit be4d566

Please sign in to comment.