| 1 |
// Copyright 2023 Dolphin Emulator Project |
| 2 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 3 |
|
| 4 |
#include "VideoCommon/Assets/MeshAsset.h" |
| 5 |
|
| 6 |
#include <array> |
| 7 |
#include <utility> |
| 8 |
|
| 9 |
#if __has_include(<tinygltf/tiny_gltf.h>) |
| •10 |
# include <tinygltf/tiny_gltf.h> |
| 11 |
#else |
| 12 |
# include <tiny_gltf.h> |
| 13 |
#endif |
| 14 |
|
| 15 |
#include "Common/IOFile.h" |
| 16 |
#include "Common/Logging/Log.h" |
| 17 |
#include "VideoCommon/Assets/CustomAssetLibrary.h" |
| 18 |
|
| 19 |
namespace VideoCommon |
| 20 |
{ |
| 21 |
namespace |
| 22 |
{ |
| 23 |
Common::Matrix44 BuildMatrixFromNode(const tinygltf::Node& node) |
| 24 |
{ |
| 25 |
if (!node.matrix.empty()) |
| 26 |
{ |
| 27 |
Common::Matrix44 matrix; |
| 28 |
for (std::size_t i = 0; i < node.matrix.size(); i++) |
| 29 |
{ |
| 30 |
matrix.data[i] = static_cast<float>(node.matrix[i]); |
| 31 |
} |
| 32 |
return matrix; |
| 33 |
} |
| 34 |
|
| 35 |
Common::Matrix44 matrix = Common::Matrix44::Identity(); |
| 36 |
|
| 37 |
// Check individual components |
| 38 |
|
| 39 |
if (!node.scale.empty()) |
| 40 |
{ |
| 41 |
matrix *= Common::Matrix44::FromMatrix33(Common::Matrix33::Scale( |
| 42 |
Common::Vec3{static_cast<float>(node.scale[0]), static_cast<float>(node.scale[1]), |
| 43 |
static_cast<float>(node.scale[2])})); |
| 44 |
} |
| 45 |
|
| 46 |
if (!node.rotation.empty()) |
| 47 |
{ |
| 48 |
matrix *= Common::Matrix44::FromQuaternion(Common::Quaternion( |
| 49 |
static_cast<float>(node.rotation[3]), static_cast<float>(node.rotation[0]), |
| 50 |
static_cast<float>(node.rotation[1]), static_cast<float>(node.rotation[2]))); |
| 51 |
} |
| 52 |
|
| 53 |
if (!node.translation.empty()) |
| 54 |
{ |
| 55 |
matrix *= Common::Matrix44::Translate(Common::Vec3{static_cast<float>(node.translation[0]), |
| 56 |
static_cast<float>(node.translation[1]), |
| 57 |
static_cast<float>(node.translation[2])}); |
| 58 |
} |
| 59 |
|
| 60 |
return matrix; |
| 61 |
} |
| 62 |
|
| 63 |
bool GLTFComponentTypeToAttributeFormat(int component_type, AttributeFormat* format) |
| 64 |
{ |
| 65 |
switch (component_type) |
| 66 |
{ |
| 67 |
case TINYGLTF_COMPONENT_TYPE_BYTE: |
| 68 |
{ |
| 69 |
format->type = ComponentFormat::Byte; |
| 70 |
format->integer = false; |
| 71 |
} |
| 72 |
break; |
| 73 |
case TINYGLTF_COMPONENT_TYPE_DOUBLE: |
| 74 |
{ |
| 75 |
return false; |
| 76 |
} |
| 77 |
break; |
| 78 |
case TINYGLTF_COMPONENT_TYPE_FLOAT: |
| 79 |
{ |
| 80 |
format->type = ComponentFormat::Float; |
| 81 |
format->integer = false; |
| 82 |
} |
| 83 |
break; |
| 84 |
case TINYGLTF_COMPONENT_TYPE_INT: |
| 85 |
{ |
| 86 |
format->type = ComponentFormat::Float; |
| 87 |
format->integer = true; |
| 88 |
} |
| 89 |
break; |
| 90 |
case TINYGLTF_COMPONENT_TYPE_SHORT: |
| 91 |
{ |
| 92 |
format->type = ComponentFormat::Short; |
| 93 |
format->integer = false; |
| 94 |
} |
| 95 |
break; |
| 96 |
case TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE: |
| 97 |
{ |
| 98 |
format->type = ComponentFormat::UByte; |
| 99 |
format->integer = false; |
| 100 |
} |
| 101 |
break; |
| 102 |
case TINYGLTF_COMPONENT_TYPE_UNSIGNED_INT: |
| 103 |
{ |
| 104 |
return false; |
| 105 |
} |
| 106 |
break; |
| 107 |
case TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT: |
| 108 |
{ |
| 109 |
format->type = ComponentFormat::UShort; |
| 110 |
format->integer = false; |
| 111 |
} |
| 112 |
break; |
| 113 |
}; |
| 114 |
|
| 115 |
return true; |
| 116 |
} |
| 117 |
|
| 118 |
bool UpdateVertexStrideFromPrimitive(const tinygltf::Model& model, u32 accessor_index, |
| 119 |
MeshDataChunk* chunk) |
| 120 |
{ |
| 121 |
const tinygltf::Accessor& accessor = model.accessors[accessor_index]; |
| 122 |
|
| 123 |
const int component_count = tinygltf::GetNumComponentsInType(accessor.type); |
| 124 |
if (component_count == -1) |
| 125 |
{ |
| 126 |
ERROR_LOG_FMT(VIDEO, "Failed to update vertex stride, component count was invalid"); |
| 127 |
return false; |
| 128 |
} |
| 129 |
|
| 130 |
const int component_size = |
| 131 |
tinygltf::GetComponentSizeInBytes(static_cast<uint32_t>(accessor.componentType)); |
| 132 |
if (component_size == -1) |
| 133 |
{ |
| 134 |
ERROR_LOG_FMT(VIDEO, "Failed to update vertex stride, component size was invalid"); |
| 135 |
return false; |
| 136 |
} |
| 137 |
|
| 138 |
chunk->vertex_stride += component_size * component_count; |
| 139 |
return true; |
| 140 |
} |
| 141 |
|
| 142 |
bool CopyBufferDataFromPrimitive(const tinygltf::Model& model, u32 accessor_index, |
| 143 |
std::size_t* outbound_offset, MeshDataChunk* chunk) |
| 144 |
{ |
| 145 |
const tinygltf::Accessor& accessor = model.accessors[accessor_index]; |
| 146 |
|
| 147 |
const int component_count = tinygltf::GetNumComponentsInType(accessor.type); |
| 148 |
if (component_count == -1) |
| 149 |
{ |
| 150 |
ERROR_LOG_FMT(VIDEO, "Failed to copy buffer data from primitive, component count was invalid"); |
| 151 |
return false; |
| 152 |
} |
| 153 |
|
| 154 |
const int component_size = |
| 155 |
tinygltf::GetComponentSizeInBytes(static_cast<uint32_t>(accessor.componentType)); |
| 156 |
if (component_size == -1) |
| 157 |
{ |
| 158 |
ERROR_LOG_FMT(VIDEO, "Failed to copy buffer data from primitive, component size was invalid"); |
| 159 |
return false; |
| 160 |
} |
| 161 |
|
| 162 |
const tinygltf::BufferView& buffer_view = model.bufferViews[accessor.bufferView]; |
| 163 |
const tinygltf::Buffer& buffer = model.buffers[buffer_view.buffer]; |
| 164 |
|
| 165 |
if (buffer_view.byteStride == 0) |
| 166 |
{ |
| 167 |
// Data is tightly packed |
| 168 |
const auto data = &buffer.data[accessor.byteOffset + buffer_view.byteOffset]; |
| 169 |
for (std::size_t i = 0; i < accessor.count; i++) |
| 170 |
{ |
| 171 |
const std::size_t vertex_data_offset = i * chunk->vertex_stride + *outbound_offset; |
| 172 |
memcpy(&chunk->vertex_data[vertex_data_offset], &data[i * component_size * component_count], |
| 173 |
component_size * component_count); |
| 174 |
} |
| 175 |
} |
| 176 |
else |
| 177 |
{ |
| 178 |
// Data is interleaved |
| 179 |
const auto data = &buffer.data[accessor.byteOffset + buffer_view.byteOffset]; |
| 180 |
for (std::size_t i = 0; i < accessor.count; i++) |
| 181 |
{ |
| 182 |
const std::size_t vertex_data_offset = i * chunk->vertex_stride + *outbound_offset; |
| 183 |
const std::size_t gltf_data_offset = i * buffer_view.byteStride; |
| 184 |
|
| 185 |
memcpy(&chunk->vertex_data[vertex_data_offset], &data[gltf_data_offset], |
| 186 |
component_size * component_count); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
*outbound_offset += component_size * component_count; |
| 191 |
|
| 192 |
return true; |
| 193 |
} |
| 194 |
|
| 195 |
bool ReadGLTFMesh(std::string_view mesh_file, const tinygltf::Model& model, |
| 196 |
const tinygltf::Mesh& mesh, const Common::Matrix44& mat, MeshData* data) |
| 197 |
{ |
| 198 |
for (std::size_t primitive_index = 0; primitive_index < mesh.primitives.size(); ++primitive_index) |
| 199 |
{ |
| 200 |
MeshDataChunk chunk; |
| 201 |
chunk.transform = mat; |
| 202 |
const tinygltf::Primitive& primitive = mesh.primitives[primitive_index]; |
| 203 |
if (primitive.indices == -1) |
| 204 |
{ |
| 205 |
ERROR_LOG_FMT(VIDEO, "Mesh '{}' is expected to have indices but doesn't have any", mesh_file); |
| 206 |
return false; |
| 207 |
} |
| 208 |
chunk.material_name = model.materials[primitive.material].name; |
| 209 |
const tinygltf::Accessor& index_accessor = model.accessors[primitive.indices]; |
| 210 |
const tinygltf::BufferView& index_buffer_view = model.bufferViews[index_accessor.bufferView]; |
| 211 |
const tinygltf::Buffer& index_buffer = model.buffers[index_buffer_view.buffer]; |
| 212 |
const int index_stride = index_accessor.ByteStride(index_buffer_view); |
| 213 |
if (index_stride == -1) |
| 214 |
{ |
| 215 |
ERROR_LOG_FMT(VIDEO, "Mesh '{}' has invalid stride", mesh_file); |
| 216 |
return false; |
| 217 |
} |
| 218 |
// TODO C++23: use make_unique_overwrite |
| 219 |
chunk.indices = std::unique_ptr<u16[]>(new u16[index_accessor.count]); |
| 220 |
auto index_src = &index_buffer.data[index_accessor.byteOffset + index_buffer_view.byteOffset]; |
| 221 |
for (std::size_t i = 0; i < index_accessor.count; i++) |
| 222 |
{ |
| 223 |
if (index_accessor.componentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT) |
| 224 |
{ |
| 225 |
std::memcpy(&chunk.indices[i], &index_src[i * index_stride], sizeof(u16)); |
| 226 |
} |
| 227 |
else if (index_accessor.componentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE) |
| 228 |
{ |
| 229 |
u8 unsigned_byte; |
| 230 |
std::memcpy(&unsigned_byte, &index_src[i * index_stride], sizeof(u8)); |
| 231 |
chunk.indices[i] = unsigned_byte; |
| 232 |
} |
| 233 |
else if (index_accessor.componentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_INT) |
| 234 |
{ |
| 235 |
// TODO: update Dolphin to support u32 indices |
| 236 |
ERROR_LOG_FMT( |
| 237 |
VIDEO, |
| 238 |
"Mesh '{}' uses an indice format of unsigned int which is not currently supported", |
| 239 |
mesh_file); |
| 240 |
return false; |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
chunk.num_indices = static_cast<u32>(index_accessor.count); |
| 245 |
|
| 246 |
if (primitive.mode == TINYGLTF_MODE_TRIANGLES) |
| 247 |
{ |
| 248 |
chunk.primitive_type = PrimitiveType::Triangles; |
| 249 |
} |
| 250 |
else if (primitive.mode == TINYGLTF_MODE_TRIANGLE_STRIP) |
| 251 |
{ |
| 252 |
chunk.primitive_type = PrimitiveType::TriangleStrip; |
| 253 |
} |
| 254 |
else if (primitive.mode == TINYGLTF_MODE_TRIANGLE_FAN) |
| 255 |
{ |
| 256 |
ERROR_LOG_FMT(VIDEO, "Mesh '{}' requires triangle fan but that is not supported", mesh_file); |
| 257 |
return false; |
| 258 |
} |
| 259 |
else if (primitive.mode == TINYGLTF_MODE_LINE) |
| 260 |
{ |
| 261 |
chunk.primitive_type = PrimitiveType::Lines; |
| 262 |
} |
| 263 |
else if (primitive.mode == TINYGLTF_MODE_POINTS) |
| 264 |
{ |
| 265 |
chunk.primitive_type = PrimitiveType::Points; |
| 266 |
} |
| 267 |
|
| 268 |
chunk.vertex_stride = 0; |
| 269 |
static constexpr std::array<std::string_view, 12> all_names = { |
| 270 |
"POSITION", "NORMAL", "COLOR_0", "COLOR_1", "TEXCOORD_0", "TEXCOORD_1", |
| 271 |
"TEXCOORD_2", "TEXCOORD_3", "TEXCOORD_4", "TEXCOORD_5", "TEXCOORD_6", "TEXCOORD_7", |
| 272 |
}; |
| 273 |
for (std::size_t i = 0; i < all_names.size(); i++) |
| 274 |
{ |
| 275 |
const auto it = primitive.attributes.find(std::string{all_names[i]}); |
| 276 |
if (it != primitive.attributes.end()) |
| 277 |
{ |
| 278 |
if (!UpdateVertexStrideFromPrimitive(model, it->second, &chunk)) |
| 279 |
return false; |
| 280 |
} |
| 281 |
} |
| 282 |
chunk.vertex_declaration.stride = chunk.vertex_stride; |
| 283 |
|
| 284 |
const auto position_it = primitive.attributes.find("POSITION"); |
| 285 |
if (position_it == primitive.attributes.end()) |
| 286 |
{ |
| 287 |
ERROR_LOG_FMT(VIDEO, "Mesh '{}' does not provide a POSITION attribute, that is required", |
| 288 |
mesh_file); |
| 289 |
return false; |
| 290 |
} |
| 291 |
std::size_t outbound_offset = 0; |
| 292 |
const tinygltf::Accessor& pos_accessor = model.accessors[position_it->second]; |
| 293 |
chunk.num_vertices = static_cast<u32>(pos_accessor.count); |
| 294 |
// TODO C++23: use make_unique_overwrite |
| 295 |
chunk.vertex_data = std::unique_ptr<u8[]>(new u8[chunk.num_vertices * chunk.vertex_stride]); |
| 296 |
if (!CopyBufferDataFromPrimitive(model, position_it->second, &outbound_offset, &chunk)) |
| 297 |
return false; |
| 298 |
chunk.components_available = 0; |
| 299 |
chunk.vertex_declaration.position.enable = true; |
| 300 |
chunk.vertex_declaration.position.components = 3; |
| 301 |
chunk.vertex_declaration.position.offset = 0; |
| 302 |
if (!GLTFComponentTypeToAttributeFormat(pos_accessor.componentType, |
| 303 |
&chunk.vertex_declaration.position)) |
| 304 |
{ |
| 305 |
ERROR_LOG_FMT(VIDEO, "Mesh '{}' has invalid attribute format for position", mesh_file); |
| 306 |
return false; |
| 307 |
} |
| 308 |
|
| 309 |
// Save off min and max position in case we want to compute bounds |
| 310 |
// GLTF spec expects these values to exist but error if they don't |
| 311 |
if (pos_accessor.minValues.size() != 3) |
| 312 |
{ |
| 313 |
ERROR_LOG_FMT(VIDEO, "Mesh '{}' is expected to have a minimum value but it is missing", |
| 314 |
mesh_file); |
| 315 |
return false; |
| 316 |
} |
| 317 |
chunk.minimum_position.x = static_cast<float>(pos_accessor.minValues[0]); |
| 318 |
chunk.minimum_position.y = static_cast<float>(pos_accessor.minValues[1]); |
| 319 |
chunk.minimum_position.z = static_cast<float>(pos_accessor.minValues[2]); |
| 320 |
|
| 321 |
if (pos_accessor.maxValues.size() != 3) |
| 322 |
{ |
| 323 |
ERROR_LOG_FMT(VIDEO, "Mesh '{}' is expected to have a maximum value but it is missing", |
| 324 |
mesh_file); |
| 325 |
return false; |
| 326 |
} |
| 327 |
chunk.maximum_position.x = static_cast<float>(pos_accessor.maxValues[0]); |
| 328 |
chunk.maximum_position.y = static_cast<float>(pos_accessor.maxValues[1]); |
| 329 |
chunk.maximum_position.z = static_cast<float>(pos_accessor.maxValues[2]); |
| 330 |
|
| 331 |
static constexpr std::array<std::string_view, 2> color_names = { |
| 332 |
"COLOR_0", |
| 333 |
"COLOR_1", |
| 334 |
}; |
| 335 |
for (std::size_t i = 0; i < color_names.size(); i++) |
| 336 |
{ |
| 337 |
const auto color_it = primitive.attributes.find(std::string{color_names[i]}); |
| 338 |
if (color_it != primitive.attributes.end()) |
| 339 |
{ |
| 340 |
chunk.vertex_declaration.colors[i].offset = static_cast<int>(outbound_offset); |
| 341 |
if (!CopyBufferDataFromPrimitive(model, color_it->second, &outbound_offset, &chunk)) |
| 342 |
return false; |
| 343 |
chunk.components_available |= VB_HAS_COL0 << i; |
| 344 |
|
| 345 |
chunk.vertex_declaration.colors[i].enable = true; |
| 346 |
chunk.vertex_declaration.colors[i].components = 3; |
| 347 |
const tinygltf::Accessor& accessor = model.accessors[color_it->second]; |
| 348 |
if (!GLTFComponentTypeToAttributeFormat(accessor.componentType, |
| 349 |
&chunk.vertex_declaration.colors[i])) |
| 350 |
{ |
| 351 |
ERROR_LOG_FMT(VIDEO, "Mesh '{}' has invalid attribute format for {}", mesh_file, |
| 352 |
color_names[i]); |
| 353 |
return false; |
| 354 |
} |
| 355 |
} |
| 356 |
else |
| 357 |
{ |
| 358 |
chunk.vertex_declaration.colors[i].enable = false; |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
const auto normal_it = primitive.attributes.find("NORMAL"); |
| 363 |
if (normal_it != primitive.attributes.end()) |
| 364 |
{ |
| 365 |
chunk.vertex_declaration.normals[0].offset = static_cast<int>(outbound_offset); |
| 366 |
if (!CopyBufferDataFromPrimitive(model, normal_it->second, &outbound_offset, &chunk)) |
| 367 |
return false; |
| 368 |
chunk.components_available |= VB_HAS_NORMAL; |
| 369 |
chunk.vertex_declaration.normals[0].enable = true; |
| 370 |
chunk.vertex_declaration.normals[0].components = 3; |
| 371 |
const tinygltf::Accessor& accessor = model.accessors[normal_it->second]; |
| 372 |
if (!GLTFComponentTypeToAttributeFormat(accessor.componentType, |
| 373 |
&chunk.vertex_declaration.normals[0])) |
| 374 |
{ |
| 375 |
ERROR_LOG_FMT(VIDEO, "Mesh '{}' has invalid attribute format for NORMAL", mesh_file); |
| 376 |
return false; |
| 377 |
} |
| 378 |
} |
| 379 |
else |
| 380 |
{ |
| 381 |
chunk.vertex_declaration.normals[0].enable = false; |
| 382 |
} |
| 383 |
|
| 384 |
static constexpr std::array<std::string_view, 8> texcoord_names = { |
| 385 |
"TEXCOORD_0", "TEXCOORD_1", "TEXCOORD_2", "TEXCOORD_3", |
| 386 |
"TEXCOORD_4", "TEXCOORD_5", "TEXCOORD_6", "TEXCOORD_7", |
| 387 |
}; |
| 388 |
for (std::size_t i = 0; i < texcoord_names.size(); i++) |
| 389 |
{ |
| 390 |
const auto texture_it = primitive.attributes.find(std::string{texcoord_names[i]}); |
| 391 |
if (texture_it != primitive.attributes.end()) |
| 392 |
{ |
| 393 |
chunk.vertex_declaration.texcoords[i].offset = static_cast<int>(outbound_offset); |
| 394 |
if (!CopyBufferDataFromPrimitive(model, texture_it->second, &outbound_offset, &chunk)) |
| 395 |
return false; |
| 396 |
chunk.components_available |= VB_HAS_UV0 << i; |
| 397 |
chunk.vertex_declaration.texcoords[i].enable = true; |
| 398 |
chunk.vertex_declaration.texcoords[i].components = 2; |
| 399 |
const tinygltf::Accessor& accessor = model.accessors[texture_it->second]; |
| 400 |
if (!GLTFComponentTypeToAttributeFormat(accessor.componentType, |
| 401 |
&chunk.vertex_declaration.texcoords[i])) |
| 402 |
{ |
| 403 |
ERROR_LOG_FMT(VIDEO, "Mesh '{}' has invalid attribute format for {}", mesh_file, |
| 404 |
texcoord_names[i]); |
| 405 |
return false; |
| 406 |
} |
| 407 |
} |
| 408 |
else |
| 409 |
{ |
| 410 |
chunk.vertex_declaration.texcoords[i].enable = false; |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
// Position matrix can be enabled if the draw that is using |
| 415 |
// this mesh needs it |
| 416 |
chunk.vertex_declaration.posmtx.enable = false; |
| 417 |
|
| 418 |
data->m_mesh_chunks.push_back(std::move(chunk)); |
| 419 |
} |
| 420 |
|
| 421 |
return true; |
| 422 |
} |
| 423 |
|
| 424 |
bool ReadGLTFNodes(std::string_view mesh_file, const tinygltf::Model& model, |
| 425 |
const tinygltf::Node& node, const Common::Matrix44& mat, MeshData* data) |
| 426 |
{ |
| 427 |
if (node.mesh != -1) |
| 428 |
{ |
| 429 |
if (!ReadGLTFMesh(mesh_file, model, model.meshes[node.mesh], mat, data)) |
| 430 |
return false; |
| 431 |
} |
| 432 |
|
| 433 |
for (std::size_t i = 0; i < node.children.size(); i++) |
| 434 |
{ |
| 435 |
const tinygltf::Node& child = model.nodes[node.children[i]]; |
| 436 |
const auto child_mat = mat * BuildMatrixFromNode(child); |
| 437 |
if (!ReadGLTFNodes(mesh_file, model, child, child_mat, data)) |
| 438 |
return false; |
| 439 |
} |
| 440 |
|
| 441 |
return true; |
| 442 |
} |
| 443 |
|
| 444 |
bool ReadGLTFMaterials(std::string_view mesh_file, const tinygltf::Model& model, MeshData* data) |
| 445 |
{ |
| 446 |
for (std::size_t i = 0; i < model.materials.size(); i++) |
| 447 |
{ |
| 448 |
const tinygltf::Material& material = model.materials[i]; |
| 449 |
|
| 450 |
// TODO: support converting material data into Dolphin material assets |
| 451 |
data->m_mesh_material_to_material_asset_id.insert_or_assign(material.name, ""); |
| 452 |
} |
| 453 |
|
| 454 |
return true; |
| 455 |
} |
| 456 |
|
| 457 |
bool ReadGLTF(std::string_view mesh_file, const tinygltf::Model& model, MeshData* data) |
| 458 |
{ |
| 459 |
int scene_index = model.defaultScene; |
| 460 |
if (scene_index == -1) |
| 461 |
scene_index = 0; |
| 462 |
|
| 463 |
const auto& scene = model.scenes[scene_index]; |
| 464 |
const auto scene_node_indices = scene.nodes; |
| 465 |
for (std::size_t i = 0; i < scene_node_indices.size(); i++) |
| 466 |
{ |
| 467 |
const tinygltf::Node& node = model.nodes[scene_node_indices[i]]; |
| 468 |
const auto mat = BuildMatrixFromNode(node); |
| 469 |
if (!ReadGLTFNodes(mesh_file, model, node, mat, data)) |
| 470 |
return false; |
| 471 |
} |
| 472 |
|
| 473 |
return ReadGLTFMaterials(mesh_file, model, data); |
| 474 |
} |
| 475 |
} // namespace |
| 476 |
bool MeshData::FromJson(const VideoCommon::CustomAssetLibrary::AssetID& asset_id, |
| 477 |
const picojson::object& json, MeshData* data) |
| 478 |
{ |
| 479 |
if (const auto iter = json.find("material_mapping"); iter != json.end()) |
| 480 |
{ |
| 481 |
if (!iter->second.is<picojson::object>()) |
| 482 |
{ |
| 483 |
ERROR_LOG_FMT( |
| 484 |
VIDEO, |
| 485 |
"Asset '{}' failed to parse json, expected 'material_mapping' to be of type object", |
| 486 |
asset_id); |
| 487 |
return false; |
| 488 |
} |
| 489 |
|
| 490 |
for (const auto& [material_name, asset_id_json] : iter->second.get<picojson::object>()) |
| 491 |
{ |
| 492 |
if (!asset_id_json.is<std::string>()) |
| 493 |
{ |
| 494 |
ERROR_LOG_FMT( |
| 495 |
VIDEO, |
| 496 |
"Asset '{}' failed to parse json, material name '{}' linked to a non-string value", |
| 497 |
asset_id, material_name); |
| 498 |
return false; |
| 499 |
} |
| 500 |
|
| 501 |
data->m_mesh_material_to_material_asset_id[material_name] = asset_id_json.to_str(); |
| 502 |
} |
| 503 |
} |
| 504 |
return true; |
| 505 |
} |
| 506 |
|
| 507 |
void MeshData::ToJson(picojson::object& obj, const MeshData& data) |
| 508 |
{ |
| 509 |
picojson::object material_mapping; |
| 510 |
for (const auto& [material_name, asset_id] : data.m_mesh_material_to_material_asset_id) |
| 511 |
{ |
| 512 |
material_mapping.emplace(material_name, asset_id); |
| 513 |
} |
| 514 |
obj.emplace("material_mapping", std::move(material_mapping)); |
| 515 |
} |
| 516 |
|
| 517 |
bool MeshData::FromDolphinMesh(std::span<const u8> raw_data, MeshData* data) |
| 518 |
{ |
| 519 |
std::size_t offset = 0; |
| 520 |
|
| 521 |
std::size_t chunk_size = 0; |
| 522 |
std::memcpy(&chunk_size, raw_data.data(), sizeof(std::size_t)); |
| 523 |
offset += sizeof(std::size_t); |
| 524 |
|
| 525 |
data->m_mesh_chunks.reserve(chunk_size); |
| 526 |
for (std::size_t i = 0; i < chunk_size; i++) |
| 527 |
{ |
| 528 |
MeshDataChunk chunk; |
| 529 |
|
| 530 |
std::memcpy(&chunk.num_vertices, raw_data.data() + offset, sizeof(u32)); |
| 531 |
offset += sizeof(u32); |
| 532 |
|
| 533 |
std::memcpy(&chunk.vertex_stride, raw_data.data() + offset, sizeof(u32)); |
| 534 |
offset += sizeof(u32); |
| 535 |
|
| 536 |
// TODO C++23: use make_unique_overwrite |
| 537 |
chunk.vertex_data = std::unique_ptr<u8[]>(new u8[chunk.num_vertices * chunk.vertex_stride]); |
| 538 |
std::memcpy(chunk.vertex_data.get(), raw_data.data() + offset, |
| 539 |
chunk.num_vertices * chunk.vertex_stride); |
| 540 |
offset += chunk.num_vertices * chunk.vertex_stride; |
| 541 |
|
| 542 |
std::memcpy(&chunk.num_indices, raw_data.data() + offset, sizeof(u32)); |
| 543 |
offset += sizeof(u32); |
| 544 |
|
| 545 |
// TODO C++23: use make_unique_overwrite |
| 546 |
chunk.indices = std::unique_ptr<u16[]>(new u16[chunk.num_indices]); |
| 547 |
std::memcpy(chunk.indices.get(), raw_data.data() + offset, chunk.num_indices * sizeof(u16)); |
| 548 |
offset += chunk.num_indices * sizeof(u16); |
| 549 |
|
| 550 |
std::memcpy(&chunk.vertex_declaration, raw_data.data() + offset, |
| 551 |
sizeof(PortableVertexDeclaration)); |
| 552 |
offset += sizeof(PortableVertexDeclaration); |
| 553 |
|
| 554 |
std::memcpy(&chunk.primitive_type, raw_data.data() + offset, sizeof(PrimitiveType)); |
| 555 |
offset += sizeof(PrimitiveType); |
| 556 |
|
| 557 |
std::memcpy(&chunk.components_available, raw_data.data() + offset, sizeof(u32)); |
| 558 |
offset += sizeof(u32); |
| 559 |
|
| 560 |
std::memcpy(&chunk.minimum_position, raw_data.data() + offset, sizeof(Common::Vec3)); |
| 561 |
offset += sizeof(Common::Vec3); |
| 562 |
|
| 563 |
std::memcpy(&chunk.maximum_position, raw_data.data() + offset, sizeof(Common::Vec3)); |
| 564 |
offset += sizeof(Common::Vec3); |
| 565 |
|
| 566 |
std::memcpy(&chunk.transform.data[0], raw_data.data() + offset, |
| 567 |
chunk.transform.data.size() * sizeof(float)); |
| 568 |
offset += chunk.transform.data.size() * sizeof(float); |
| 569 |
|
| 570 |
std::size_t material_name_size = 0; |
| 571 |
std::memcpy(&material_name_size, raw_data.data() + offset, sizeof(std::size_t)); |
| 572 |
offset += sizeof(std::size_t); |
| 573 |
|
| 574 |
chunk.material_name.assign(raw_data.data() + offset, |
| 575 |
raw_data.data() + offset + material_name_size); |
| 576 |
offset += material_name_size * sizeof(char); |
| 577 |
|
| 578 |
data->m_mesh_chunks.push_back(std::move(chunk)); |
| 579 |
} |
| 580 |
|
| 581 |
return true; |
| 582 |
} |
| 583 |
|
| 584 |
bool MeshData::ToDolphinMesh(File::IOFile* file_data, const MeshData& data) |
| 585 |
{ |
| 586 |
const std::size_t chunk_size = data.m_mesh_chunks.size(); |
| 587 |
file_data->WriteBytes(&chunk_size, sizeof(std::size_t)); |
| 588 |
for (const auto& chunk : data.m_mesh_chunks) |
| 589 |
{ |
| 590 |
if (!file_data->WriteBytes(&chunk.num_vertices, sizeof(u32))) |
| 591 |
return false; |
| 592 |
if (!file_data->WriteBytes(&chunk.vertex_stride, sizeof(u32))) |
| 593 |
return false; |
| 594 |
if (!file_data->WriteBytes(chunk.vertex_data.get(), chunk.num_vertices * chunk.vertex_stride)) |
| 595 |
return false; |
| 596 |
if (!file_data->WriteBytes(&chunk.num_indices, sizeof(u32))) |
| 597 |
return false; |
| 598 |
if (!file_data->WriteBytes(chunk.indices.get(), chunk.num_indices * sizeof(u16))) |
| 599 |
return false; |
| 600 |
if (!file_data->WriteBytes(&chunk.vertex_declaration, sizeof(PortableVertexDeclaration))) |
| 601 |
return false; |
| 602 |
if (!file_data->WriteBytes(&chunk.primitive_type, sizeof(PrimitiveType))) |
| 603 |
return false; |
| 604 |
if (!file_data->WriteBytes(&chunk.components_available, sizeof(u32))) |
| 605 |
return false; |
| 606 |
if (!file_data->WriteBytes(&chunk.minimum_position, sizeof(Common::Vec3))) |
| 607 |
return false; |
| 608 |
if (!file_data->WriteBytes(&chunk.maximum_position, sizeof(Common::Vec3))) |
| 609 |
return false; |
| 610 |
if (!file_data->WriteBytes(&chunk.transform.data[0], |
| 611 |
chunk.transform.data.size() * sizeof(float))) |
| 612 |
{ |
| 613 |
return false; |
| 614 |
} |
| 615 |
|
| 616 |
const std::size_t material_name_size = chunk.material_name.size(); |
| 617 |
if (!file_data->WriteBytes(&material_name_size, sizeof(std::size_t))) |
| 618 |
return false; |
| 619 |
if (!file_data->WriteBytes(&chunk.material_name[0], chunk.material_name.size() * sizeof(char))) |
| 620 |
return false; |
| 621 |
} |
| 622 |
return true; |
| 623 |
} |
| 624 |
|
| 625 |
bool MeshData::FromGLTF(std::string_view gltf_file, MeshData* data) |
| 626 |
{ |
| 627 |
if (gltf_file.ends_with(".glb")) |
| 628 |
{ |
| 629 |
ERROR_LOG_FMT(VIDEO, "File '{}' with glb extension is not supported at this time", gltf_file); |
| 630 |
return false; |
| 631 |
} |
| 632 |
|
| 633 |
if (gltf_file.ends_with(".gltf")) |
| 634 |
{ |
| 635 |
tinygltf::Model model; |
| 636 |
tinygltf::TinyGLTF loader; |
| 637 |
std::string model_errors; |
| 638 |
std::string model_warnings; |
| 639 |
if (!loader.LoadASCIIFromFile(&model, &model_errors, &model_warnings, std::string{gltf_file})) |
| 640 |
{ |
| 641 |
ERROR_LOG_FMT(VIDEO, "File '{}' was invalid GLTF, error: {}, warning: {}", gltf_file, |
| 642 |
model_errors, model_warnings); |
| 643 |
return false; |
| 644 |
} |
| 645 |
return ReadGLTF(gltf_file, model, data); |
| 646 |
} |
| 647 |
|
| 648 |
ERROR_LOG_FMT(VIDEO, "GLTF '{}' has invalid extension", gltf_file); |
| 649 |
return false; |
| 650 |
} |
| 651 |
|
| 652 |
CustomAssetLibrary::LoadInfo MeshAsset::LoadImpl(const CustomAssetLibrary::AssetID& asset_id) |
| 653 |
{ |
| 654 |
auto potential_data = std::make_shared<MeshData>(); |
| 655 |
const auto loaded_info = m_owning_library->LoadMesh(asset_id, potential_data.get()); |
| 656 |
if (loaded_info.bytes_loaded == 0) |
| 657 |
return {}; |
| 658 |
{ |
| 659 |
std::lock_guard lk(m_data_lock); |
| 660 |
m_loaded = true; |
| 661 |
m_data = std::move(potential_data); |
| 662 |
} |
| 663 |
return loaded_info; |
| 664 |
} |
| 665 |
} // namespace VideoCommon |