| 338 | } |
| 339 | |
| 340 | bool ModelBase::LoadMesh(MemoryReadStream& stream, byte meshVersion, MeshBase* mesh, MeshData* dataIfReadOnly) |
| 341 | { |
| 342 | PROFILE_MEM(GraphicsMeshes); |
| 343 | |
| 344 | // Load descriptor |
| 345 | static_assert(MODEL_MESH_VERSION == 2, "Update code"); |
| 346 | uint32 vertices, triangles; |
| 347 | stream.Read(vertices); |
| 348 | stream.Read(triangles); |
| 349 | const uint32 indicesCount = triangles * 3; |
| 350 | const bool use16BitIndexBuffer = indicesCount <= MAX_uint16; |
| 351 | const uint32 ibStride = use16BitIndexBuffer ? sizeof(uint16) : sizeof(uint32); |
| 352 | if (vertices == 0 || triangles == 0) |
| 353 | return true; |
| 354 | Array<const void*, FixedAllocation<MODEL_MAX_VB>> vbData; |
| 355 | Array<GPUVertexLayout*, FixedAllocation<MODEL_MAX_VB>> vbLayout; |
| 356 | byte vbCount; |
| 357 | stream.Read(vbCount); |
| 358 | if (vbCount > MODEL_MAX_VB) |
| 359 | return true; |
| 360 | vbData.Resize(vbCount); |
| 361 | vbLayout.Resize(vbCount); |
| 362 | for (int32 i = 0; i < vbCount; i++) |
| 363 | { |
| 364 | GPUVertexLayout::Elements elements; |
| 365 | stream.Read(elements); |
| 366 | vbLayout[i] = GPUVertexLayout::Get(elements); |
| 367 | } |
| 368 | |
| 369 | // Move over actual mesh data |
| 370 | for (int32 i = 0; i < vbCount; i++) |
| 371 | { |
| 372 | auto layout = vbLayout[i]; |
| 373 | if (!layout) |
| 374 | { |
| 375 | LOG(Warning, "Failed to get vertex layout for buffer {}", i); |
| 376 | return true; |
| 377 | } |
| 378 | vbData[i] = stream.Move(vertices * layout->GetStride()); |
| 379 | } |
| 380 | const auto ibData = stream.Move(indicesCount * ibStride); |
| 381 | |
| 382 | // Pass results if read-only |
| 383 | if (dataIfReadOnly) |
| 384 | { |
| 385 | dataIfReadOnly->Vertices = vertices; |
| 386 | dataIfReadOnly->Triangles = triangles; |
| 387 | dataIfReadOnly->IBStride = ibStride; |
| 388 | dataIfReadOnly->VBData = vbData; |
| 389 | dataIfReadOnly->VBLayout = vbLayout; |
| 390 | dataIfReadOnly->IBData = ibData; |
| 391 | return false; |
| 392 | } |
| 393 | |
| 394 | // Setup GPU resources |
| 395 | return mesh->Init(vertices, triangles, vbData, ibData, use16BitIndexBuffer, vbLayout); |
| 396 | } |
| 397 | |