Cogs.Core
DumpStatsCommand.cpp
1#include "DumpStatsCommand.h"
2
3#include "Components/Core/MeshComponent.h"
4#include "Components/Core/MeshRenderComponent.h"
5#include "Components/Core/SceneComponent.h"
6
7#include "Resources/MeshManager.h"
8#include "Resources/MaterialManager.h"
9#include "Resources/TextureManager.h"
10
11#include "Foundation/Logging/Logger.h"
12
13#include <unordered_map>
14
15namespace
16{
17 Cogs::Logging::Log logger = Cogs::Logging::getLogger("DumpStats");
18}
19
20namespace Cogs::Core
21{
23 {
24 size_t nodes = 0;
25 size_t strings = 0;
26 size_t meshes = 0;
27 size_t materials = 0;
28 size_t streams = 0;
29 size_t streamBytes = 0;
30 size_t numVerts = 0;
31 size_t numIndexes = 0;
32
33 std::unordered_map<Mesh *, uint32_t> usedMeshes;
34 std::unordered_map<MaterialInstance *, uint32_t> usedMaterials;
35 };
36
37 void writeStream(ModelStats & stats, const DataStream * stream)
38 {
39 ++stats.streams;
40
41 if (stream->format) {
42 stats.streamBytes += stream->numElements * Cogs::getSize(stream->format);
43 } else {
44 stats.streamBytes += stream->numElements * 4; // Aproximation.
45 }
46 }
47
48 void writeMeshStats(ModelStats & stats, Mesh * mesh)
49 {
50 auto it = stats.usedMeshes.find(mesh);
51
52 if (it != stats.usedMeshes.end()) return;
53
54 stats.usedMeshes[mesh] = 0;
55
56 ++stats.meshes;
57
58 if (!mesh->getName().empty()) {
59 ++stats.strings;
60 }
61
62 for (size_t i = 0; i < Mesh::MaxStreams; ++i) {
64
65 if (mesh->hasStream(type)) {
66 writeStream(stats, &mesh->getStream(type));
67
68 if (type == VertexDataType::Positions) {
69 stats.numVerts += mesh->getStream(type).numElements;
70 }
71 }
72 }
73
74 if (mesh->isIndexed()) {
75 ++stats.streams;
76
77 stats.streamBytes += mesh->getIndexes().size() * 4;
78
79 stats.numIndexes += mesh->getIndexes().size();
80 }
81 }
82
83 void writeStats(Context * context, ModelStats & model, uint32_t /*parent*/, ComponentModel::Entity * entity)
84 {
85 if (!entity) return;
86
87 ++model.nodes;
88
89 if (!entity->getName().empty()) {
90 ++model.strings;
91 }
92
93 auto meshComponent = entity->getComponent<MeshComponent>();
94
95 if (meshComponent && meshComponent->meshHandle) {
96 auto mesh = meshComponent->meshHandle.resolve();
97
98 if (mesh && mesh->streams.size()) {
99 writeMeshStats(model, mesh);
100 }
101
102 auto renderComponent = entity->getComponent<MeshRenderComponent>();
103
104 if (renderComponent && renderComponent->material) {
105 auto material = renderComponent->material.resolve();
106
107 auto it = model.usedMaterials.find(material);
108
109 if (it == model.usedMaterials.end()) {
110 ++model.materials;
111
112 model.usedMaterials[material] = 0;
113 }
114 }
115 }
116
117 auto sceneComponent = entity->getComponent<SceneComponent>();
118
119 for (auto & child : sceneComponent->children) {
120 writeStats(context, model, (uint32_t)-1, child.get());
121 }
122 }
123}
124
126{
127 ModelStats stats = {};
128
129 writeStats(context, stats, (uint32_t)-1, context->store->getEntityPtr(entityId));
130
131 auto title = getOption(options, "title");
132
133 if (!title.empty()) {
134 LOG_TRACE(logger, "%s", title.data());
135 } else {
136 LOG_TRACE(logger, "Model Stats:");
137 }
138
139 LOG_TRACE(logger, "----------------------------");
140 LOG_TRACE(logger, "Nodes: %zd", stats.nodes);
141 LOG_TRACE(logger, "Strings: %zd", stats.strings);
142 LOG_TRACE(logger, "Materials: %zd", stats.materials);
143 LOG_TRACE(logger, "Meshes: %zd", stats.meshes);
144 LOG_TRACE(logger, "Streams: %zd", stats.streams);
145 LOG_TRACE(logger, "Stream bytes: %zd", stats.streamBytes);
146 LOG_TRACE(logger, "Num vertexes: %zd", stats.numVerts);
147 LOG_TRACE(logger, "Num indexes: %zd", stats.numIndexes);
148}
149
150void Cogs::Core::DumpStatsCommand::undo()
151{
152}
Log implementation class.
Definition: LogManager.h:139
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
StringView getOption(const std::vector< ParsedValue > &options, const StringView &key, const StringView &defaultValue="")
Find and get value of option in vector as a string. Return default if not found.
constexpr Log getLogger(const char(&name)[LEN]) noexcept
Definition: LogManager.h:180
Contains a stream of data used by Mesh resources.
Definition: Mesh.h:80
uint32_t numElements
Number of elements of the type given by format contained in data.
Definition: Mesh.h:108
VertexFormatHandle format
A pointer to the format describing the contents of the byte buffer.
Definition: Mesh.h:99
void apply() override
Run the command.
std::vector< ParsedValue > options
Options passed to the command when running in batch mode.
Definition: EditorCommand.h:61
static constexpr size_t MaxStreams
Maximum number of data streams contained in a single Mesh resource.
Definition: Mesh.h:305
EVertexDataType
Contains data types.
Definition: Mesh.h:26