Repository navigation
Expand file tree
/
Copy pathWorld.cpp
More file actions
306 lines (285 loc) · 12.2 KB
/
Copy pathWorld.cpp
File metadata and controls
306 lines (285 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include "World.h"
#include "Renderer.h"
#include "TextureCache.h"
#include "Threading.h"
#include "Entity.h"
#include "vjson_header.h"
#include <unordered_map>
#include <mutex>
namespace Magic
{
void World::LoadJSON(const char* worldPath)
{
// Every material texture slot is always valid. Missing base-color and
// metallic-roughness maps use white; missing normal maps use a flat normal.
{
auto uploadDefaultTexture = [](VkFormat format, const DefaultTexture::TextureArray& pixels, AllocatedImage& image, int& bindlessSlot)
{
constexpr VkExtent3D extent { .width = 128 , .height = 128 , .depth = 1 };
const VkImageCreateInfo imci = DefaultImageCreateInfo(format, extent, VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT, VK_IMAGE_TYPE_2D);
image = GRenderer->UploadImage(pixels.data(), DefaultTexture::CHANNELS, imci);
const VkImageViewCreateInfo imageViewCreateInfo = DefaultImageViewCreateInfo(
image.image,
imci.format,
VkComponentMapping{ VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G, VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A },
VK_IMAGE_ASPECT_COLOR_BIT);
image.view = GRenderer->CreateViewForAllocatedImage(imageViewCreateInfo);
bindlessSlot = GRenderer->m_bindlessManager.AddToBindlessTextureArray(image);
};
uploadDefaultTexture(VK_FORMAT_R8G8B8A8_SRGB, DefaultTexture::g_DefaultTexture, DefaultTexture::g_defaultTextureImage, DefaultTexture::g_defaultTextureImageBindlessSlot);
uploadDefaultTexture(VK_FORMAT_R8G8B8A8_SRGB, DefaultTexture::g_WhiteTexture, DefaultTexture::g_whiteTextureImage, DefaultTexture::g_whiteTextureImageBindlessSlot);
uploadDefaultTexture(VK_FORMAT_R8G8B8A8_UNORM, DefaultTexture::g_FlatNormalTexture, DefaultTexture::g_flatNormalTextureImage, DefaultTexture::g_flatNormalTextureImageBindlessSlot);
}
std::ifstream file(worldPath, std::ios::binary);
if (!file)
{
Logger::Err("Failed to open file stream");
return;
}
const std::string contents((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>{});
if (file.bad())
{
Logger::Err("Bad file");
return;
}
vjson::ParseContext ctx;
vjson::Object doc;
if (!doc.ParseJSON(contents, &ctx))
{
Logger::Err(std::format("JSON error on line: {} with message: {}", ctx.error_line, ctx.error_message));
return;
}
std::unordered_map<std::size_t, IEntity*> idToEntity;
std::unordered_map<IEntity*, std::size_t> entityToParentId;
{
struct PendingEntityRecord
{
IEntity* entity = nullptr;
int parentId = -1;
};
// Parse all entities and queue them for loading
std::vector<PendingEntityRecord> pendingEntities;
for (const vjson::Object &e : doc.ArrayAtKeyOrEmpty("entities").Iter<vjson::Object>())
{
std::string entityName = e.StringAtKey("name", "?");
std::string entityType = e.StringAtKey("type", "?");
Logger::Info(std::format("Entity Name: {}", entityName));
Logger::Info(std::format("Entity Type: {}", entityType));
int id = e.IntAtKey("id", 0);
int parentId = e.IntAtKey("parent_entity_id", 0);
assert(id > 0);
assert(parentId >= 0);
const vjson::Object& localTransform = e.ObjectAtKeyOrEmpty("transform");
auto GetMatrix4fFromTransformObject = [](const vjson::Object& transform){
const vjson::Array& position = transform.ArrayAtKeyOrEmpty("position");
const vjson::Array& rotationEuler = transform.ArrayAtKeyOrEmpty("rotation_euler");
const vjson::Array& scale = transform.ArrayAtKeyOrEmpty("scale");
Matrix4f translate = Matrix4f::MakeTranslate(
position.DoubleAtIndex(0, 0.0)
, position.DoubleAtIndex(1, 0.0)
, position.DoubleAtIndex(2, 0.0)
);
Matrix4f scaleMtx = Matrix4f::MakeScale(
scale.DoubleAtIndex(0, 0.0)
, scale.DoubleAtIndex(1, 0.0)
, scale.DoubleAtIndex(2, 0.0)
);
Matrix4f rotX = Matrix4f::MakeRotateX(rotationEuler.DoubleAtIndex(0, 0.0));
Matrix4f rotY = Matrix4f::MakeRotateY(rotationEuler.DoubleAtIndex(1, 0.0));
Matrix4f rotZ = Matrix4f::MakeRotateZ(rotationEuler.DoubleAtIndex(2, 0.0));
return rotX * rotY * rotZ * scaleMtx * translate;
};
if (entityType == "StaticMesh")
{
const vjson::Object* meshComponent = e.ObjectPtrAtKey("mesh");
if (meshComponent)
{
StaticMeshEntity* staticMeshEntity = GMemoryManager->New<StaticMeshEntity>();
staticMeshEntity->SetName(std::move(entityName));
staticMeshEntity->SetUUID(id);
staticMeshEntity->m_localTransform = GetMatrix4fFromTransformObject(localTransform);
staticMeshEntity->meshComponent = meshComponent;
pendingEntities.push_back({staticMeshEntity, parentId});
}
else
{
Logger::Warn(std::format("StaticMesh entity with id {} missing mesh component, check the world file", id));
}
}
else if (entityType == "DirectionalLight")
{
if (m_pDirLight)
{
Logger::Warn("Directional light already exists, skipping...");
continue;
}
const vjson::Object* params = e.ObjectPtrAtKey("params");
if (params)
{
m_pDirLight = GMemoryManager->New<DirectionalLightEntity>();
m_pDirLight->SetName(std::move(entityName));
m_pDirLight->SetUUID(id);
m_pDirLight->m_localTransform = GetMatrix4fFromTransformObject(localTransform);
m_pDirLight->params = params;
pendingEntities.push_back({m_pDirLight, parentId});
}
else
{
Logger::Warn(std::format("DirectionalLight entity with id {} missing params component, check the world file", id));
}
}
else
{
Logger::Warn(std::format("Unknown entity type encountered for entity: {}", entityName));
}
}
// Do the loading
std::mutex loadingMutex;
std::vector<IEntity*> loadedEntities;
auto pendingEntityLoad = [this, &pendingEntities, &loadingMutex, &loadedEntities, &idToEntity, &entityToParentId](std::size_t i){
IEntity* entity = pendingEntities.at(i).entity;
int parentId = pendingEntities.at(i).parentId;
switch(entity->GetEntityType())
{
case EntityType::StaticMesh:
{
StaticMeshEntity* staticMeshEntity = static_cast<StaticMeshEntity*>(entity);
if (staticMeshEntity->Load(staticMeshEntity->meshComponent))
{
std::lock_guard lock(loadingMutex);
staticMeshEntity->meshComponent = nullptr;
loadedEntities.push_back(staticMeshEntity);
auto res = idToEntity.insert({entity->GetUUID().Value(), entity});
assert (res.second); // Make sure there are no duplicate keys
entityToParentId.insert({entity, parentId});
}
else
{
Logger::Warn(std::format("StaticMeshEntity {} failed to load", staticMeshEntity->GetUUID().Value()));
GMemoryManager->Delete(staticMeshEntity);
}
break;
}
case EntityType::DirectionalLight:
{
DirectionalLightEntity* directionalLightEntity = static_cast<DirectionalLightEntity*>(entity);
if (directionalLightEntity->Load(directionalLightEntity->params))
{
std::lock_guard lock(loadingMutex);
directionalLightEntity->params = nullptr;
loadedEntities.push_back(directionalLightEntity);
auto res = idToEntity.insert({entity->GetUUID().Value(), entity});
assert (res.second); // Make sure there are no duplicate keys
entityToParentId.insert({entity, parentId});
}
else
{
Logger::Warn(std::format("DirectionalLightEntity {} failed to load", directionalLightEntity->GetUUID().Value()));
GMemoryManager->Delete(directionalLightEntity);
m_pDirLight = nullptr;
}
break;
}
}
};
auto start = std::chrono::steady_clock::now();
auto futures = Job::Pool.submit_loop(0, pendingEntities.size(), pendingEntityLoad, 0);
futures.wait();
m_entities = std::move(loadedEntities);
Logger::Info(std::format("Entity loading took: {}", Timing::SinceMS(start)));
}
// Fix up relationships
auto startFixup = std::chrono::steady_clock::now();
for (IEntity* entity : m_entities)
{
std::size_t parentId = entityToParentId.at(entity);
assert(entity->GetParent() == nullptr);
if (parentId == 0) // Root entities
{
continue;
}
const auto it = idToEntity.find(parentId);
if (it != idToEntity.end())
{
IEntity* parent = it->second;
assert(entity != parent && "Entity is trying to reference itself!");
#ifndef NDEBUG
// check for cycles where B is a parent of A, but somehow A is a parent of B (or any depth of that)
for(IEntity* ancestor = parent; ancestor; ancestor = ancestor->GetParent())
{
assert(ancestor != entity && "Entity hierarchy contains a cycle");
}
#endif
parent->AddChild(entity);
}
else
{
Logger::Warn("Entity parentId is invalid or parent failed to load");
}
}
Logger::Info(std::format("Entity fixup took: {}", Timing::SinceMS(startFixup)));
}
void World::Destroy()
{
GRenderer->WaitIdle();
for (IEntity* entity : m_entities)
{
assert(entity->Unload());
GMemoryManager->Delete(entity);
}
#if MAGIC_USE_SIMPLE_UUID
UUID::ResetUUIDs();
#endif
m_entities.clear();
m_pDirLight = nullptr;
GTextureCache->Destroy();
GRenderer->DestroyImage(DefaultTexture::g_defaultTextureImage);
GRenderer->DestroyImage(DefaultTexture::g_whiteTextureImage);
GRenderer->DestroyImage(DefaultTexture::g_flatNormalTextureImage);
GRenderer->m_bindlessManager.Reset();
}
std::size_t World::GetEntityCount(EntityType entityType) const
{
std::size_t count = 0;
for (const IEntity* entity : m_entities)
{
if (entity->GetEntityType() == entityType)
{
count++;
}
}
return count;
}
std::vector<const IEntity *> World::GetEntitiesOfType(EntityType entityType) const
{
std::vector<const IEntity *> result;
for (const IEntity* entity : m_entities)
{
if (entity->GetEntityType() == entityType)
{
result.push_back(entity);
}
}
return result;
}
static void UpdateSubTree(IEntity* entity, const Matrix4f& parentWorld)
{
entity->m_worldTransform = parentWorld * entity->m_localTransform;
for(IEntity* child : entity->GetChildren())
{
UpdateSubTree(child, entity->m_worldTransform);
}
}
void World::UpdateWorldTransforms()
{
Matrix4f identity;
identity.Identity();
for (IEntity* entity : m_entities)
{
if (entity->GetParent() == nullptr)
{
UpdateSubTree(entity, identity);
}
}
}
}