Skip to content

Commit

Permalink
gltfpack: Display per-class image stats when -vv is set
Browse files Browse the repository at this point in the history
We now display stats for embedded images that classify images by class.
Note that to do this we store the kind in BufferView::variant which is a
bit of a hack, but it works for now - we can rework this later if need
be.
  • Loading branch information
zeux committed May 30, 2021
1 parent a0edcdf commit 28791f1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
28 changes: 28 additions & 0 deletions gltf/gltfpack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,29 @@ static void printAttributeStats(const std::vector<BufferView>& views, BufferView
}
}

static void printImageStats(const std::vector<BufferView>& views, TextureKind kind, const char* name)
{
size_t bytes = 0;
size_t count = 0;

for (size_t i = 0; i < views.size(); ++i)
{
const BufferView& view = views[i];

if (view.kind != BufferView::Kind_Image)
continue;

if (view.variant != -1 - kind)
continue;

count += 1;
bytes += view.data.size();
}

if (count)
printf("stats: image %s: %d bytes in %d images\n", name, int(bytes), int(count));
}

static bool printReport(const char* path, cgltf_data* data, const std::vector<BufferView>& views, const std::vector<Mesh>& meshes, size_t node_count, size_t mesh_count, size_t material_count, size_t animation_count, size_t json_size, size_t bin_size)
{
size_t bytes[BufferView::Kind_Count] = {};
Expand Down Expand Up @@ -804,6 +827,11 @@ static void process(cgltf_data* data, const char* input_path, const char* output
printAttributeStats(views, BufferView::Kind_Index, "index");
printAttributeStats(views, BufferView::Kind_Keyframe, "keyframe");
printAttributeStats(views, BufferView::Kind_Instance, "instance");

printImageStats(views, TextureKind_Generic, "generic");
printImageStats(views, TextureKind_Color, "color");
printImageStats(views, TextureKind_Normal, "normal");
printImageStats(views, TextureKind_Attrib, "attrib");
}

if (report_path)
Expand Down
8 changes: 4 additions & 4 deletions gltf/write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -758,9 +758,9 @@ static bool parseDataUri(const char* uri, std::string& mime_type, std::string& r
return false;
}

static void writeEmbeddedImage(std::string& json, std::vector<BufferView>& views, const char* data, size_t size, const char* mime_type)
static void writeEmbeddedImage(std::string& json, std::vector<BufferView>& views, const char* data, size_t size, const char* mime_type, TextureKind kind)
{
size_t view = getBufferView(views, BufferView::Kind_Image, StreamFormat::Filter_None, BufferView::Compression_None, 1, -1);
size_t view = getBufferView(views, BufferView::Kind_Image, StreamFormat::Filter_None, BufferView::Compression_None, 1, -1 - kind);

assert(views[view].data.empty());
views[view].data.assign(data, size);
Expand Down Expand Up @@ -844,7 +844,7 @@ void writeImage(std::string& json, std::vector<BufferView>& views, const cgltf_i
if (!settings.texture_toktx)
encoded = basisToKtx(encoded, info.srgb, settings.texture_uastc);

writeEmbeddedImage(json, views, encoded.c_str(), encoded.size(), "image/ktx2");
writeEmbeddedImage(json, views, encoded.c_str(), encoded.size(), "image/ktx2", info.kind);
}
else
{
Expand All @@ -853,7 +853,7 @@ void writeImage(std::string& json, std::vector<BufferView>& views, const cgltf_i
}
else
{
writeEmbeddedImage(json, views, img_data.c_str(), img_data.size(), mime_type.c_str());
writeEmbeddedImage(json, views, img_data.c_str(), img_data.size(), mime_type.c_str(), info.kind);
}
}
else if (image.uri)
Expand Down

0 comments on commit 28791f1

Please sign in to comment.