From 9314b3504e85a115b24c3a4a3aeab1d6faa791de Mon Sep 17 00:00:00 2001 From: Anish Bhobe Date: Mon, 30 Jun 2025 19:57:44 +0200 Subject: [PATCH] cleanup: Attribute Loading --- Blaze/Blaze.cpp | 3 +- Blaze/MacroUtils.h | 2 + Blaze/MiscData.cpp | 2 +- Blaze/ModelLoader.cpp | 182 +++++++++++++++++------------------------- Blaze/ModelLoader.h | 4 +- 5 files changed, 81 insertions(+), 112 deletions(-) diff --git a/Blaze/Blaze.cpp b/Blaze/Blaze.cpp index 6797ff0..ac106dd 100644 --- a/Blaze/Blaze.cpp +++ b/Blaze/Blaze.cpp @@ -46,8 +46,7 @@ SDL_AppResult SDL_AppInit( void** appstate, int, char** ) AppState& appState = *static_cast( *appstate ); - Entity const* entity = - LoadModel( appState.renderDevice, appState.entityManager, "Assets/Models/OrientationTest.glb" ); + Entity const* entity = LoadModel( appState.renderDevice, appState.entityManager, "Assets/Models/DamagedHelmet.glb" ); ASSERT( entity ); std::array pointLight = { diff --git a/Blaze/MacroUtils.h b/Blaze/MacroUtils.h index 9bdf4ed..b20ef3e 100644 --- a/Blaze/MacroUtils.h +++ b/Blaze/MacroUtils.h @@ -36,3 +36,5 @@ while ( false ) #define Take( OBJ ) std::exchange( OBJ, {} ) + +#define UNREACHABLE G_ASSERT( false ) diff --git a/Blaze/MiscData.cpp b/Blaze/MiscData.cpp index b6066d4..ca625a2 100644 --- a/Blaze/MiscData.cpp +++ b/Blaze/MiscData.cpp @@ -289,7 +289,7 @@ bool MiscData::init( RenderDevice const& renderDevice ) // Camera { - cameraData.cameraPosition = DirectX::XMVectorSet( 0.0f, 20.0f, -20.0f, 1.0f ); + cameraData.cameraPosition = DirectX::XMVectorSet( 0.0f, 2.0f, -2.0f, 1.0f ); cameraTarget = DirectX::XMVectorSet( 0.0f, 0.0f, 0.0f, 1.0f ); cameraUp = DirectX::XMVectorSet( 0.0f, 1.0f, 0.0f, 1.0f ); cameraData.viewMatrix = DirectX::XMMatrixLookAtLH( cameraData.cameraPosition, cameraTarget, cameraUp ); diff --git a/Blaze/ModelLoader.cpp b/Blaze/ModelLoader.cpp index 044c1a5..5ad80e9 100644 --- a/Blaze/ModelLoader.cpp +++ b/Blaze/ModelLoader.cpp @@ -443,12 +443,42 @@ uint32_t ProcessMaterial( RenderDevice* renderDevice, Model* model, cgltf_materi return materialIdx; } +void LoadAttribute( + std::vector* pVertices, + int32_t const vertexStart, + std::vector* scratch, + cgltf_attribute const& positionAttr, + size_t const stride, + size_t const offset, + size_t const components ) +{ + size_t const floatCount = cgltf_accessor_unpack_floats( positionAttr.data, nullptr, 0 ); + ASSERT( floatCount % 3 == 0 ); + scratch->resize( floatCount ); + cgltf_accessor_unpack_floats( positionAttr.data, scratch->data(), scratch->size() ); + + // Guaranteed to have space for these vertices. + pVertices->resize( vertexStart + floatCount / components ); + + byte* writePtr = reinterpret_cast( pVertices->data() + vertexStart ) + offset; + float* readPtr = scratch->data(); + for ( size_t i = vertexStart; i < pVertices->size(); ++i ) + { + memcpy( writePtr, readPtr, components * sizeof( float ) ); + + readPtr += components; + writePtr += stride; + } + + scratch->clear(); +} + ModelMesh ProcessMesh( - RenderDevice* renderDevice, - Model* model, - std::pmr::vector* pVertices, - std::pmr::vector* pIndices, - cgltf_mesh const& mesh ) + RenderDevice* renderDevice, + Model* model, + std::vector* pVertices, + std::vector* pIndices, + cgltf_mesh const& mesh ) { using namespace std::string_view_literals; @@ -488,6 +518,8 @@ ModelMesh ProcessMesh( .vertexOffset = vertexStart, } ); + std::vector scratch; + cgltf_attribute const* attributes = primitive.attributes; for ( uint32_t attribIndex = 0; attribIndex < primitive.attributes_count; ++attribIndex ) { @@ -497,22 +529,11 @@ ModelMesh ProcessMesh( ASSERT( positionAttr.data->component_type == cgltf_component_type_r_32f ); ASSERT( positionAttr.data->type == cgltf_type_vec3 ); - std::pmr::vector positions{ pVertices->get_allocator() }; + size_t constexpr stride = sizeof( Vertex ); + size_t constexpr offset = offsetof( Vertex, position ); + size_t constexpr components = 3; - size_t const floatCount = cgltf_accessor_unpack_floats( positionAttr.data, nullptr, 0 ); - positions.resize( floatCount / 3 ); - cgltf_accessor_unpack_floats( - positionAttr.data, reinterpret_cast( positions.data() ), floatCount ); - - // Guaranteed to have space for these vertices. - pVertices->resize( vertexStart + positions.size() ); - - auto vertexIter = pVertices->begin() + vertexStart; - for ( DirectX::XMFLOAT3 const& position : positions ) - { - vertexIter->position = position; - ++vertexIter; - } + LoadAttribute( pVertices, vertexStart, &scratch, positionAttr, stride, offset, components ); } if ( "NORMAL"sv == attributes[attribIndex].name ) { @@ -520,21 +541,11 @@ ModelMesh ProcessMesh( ASSERT( normalAttr.data->component_type == cgltf_component_type_r_32f ); ASSERT( normalAttr.data->type == cgltf_type_vec3 ); - std::pmr::vector normals{ pVertices->get_allocator() }; + size_t constexpr stride = sizeof( Vertex ); + size_t constexpr offset = offsetof( Vertex, normal ); + size_t constexpr components = 3; - size_t const floatCount = cgltf_accessor_unpack_floats( normalAttr.data, nullptr, 0 ); - normals.resize( floatCount / 3 ); - cgltf_accessor_unpack_floats( normalAttr.data, reinterpret_cast( normals.data() ), floatCount ); - - // Guaranteed to have space for these vertices. - pVertices->resize( vertexStart + normals.size() ); - - auto vertexIter = pVertices->begin() + vertexStart; - for ( DirectX::XMFLOAT3 const& normal : normals ) - { - vertexIter->normal = normal; - ++vertexIter; - } + LoadAttribute( pVertices, vertexStart, &scratch, normalAttr, stride, offset, components ); } if ( "TEXCOORD_0"sv == attributes[attribIndex].name ) { @@ -542,22 +553,11 @@ ModelMesh ProcessMesh( ASSERT( texCoordAttr.data->component_type == cgltf_component_type_r_32f ); ASSERT( texCoordAttr.data->type == cgltf_type_vec2 ); - std::pmr::vector texCoords{ pVertices->get_allocator() }; + size_t constexpr stride = sizeof( Vertex ); + size_t constexpr offset = offsetof( Vertex, texCoord0 ); + size_t constexpr components = 2; - size_t const floatCount = cgltf_accessor_unpack_floats( texCoordAttr.data, nullptr, 0 ); - texCoords.resize( floatCount / 2 ); - cgltf_accessor_unpack_floats( - texCoordAttr.data, reinterpret_cast( texCoords.data() ), floatCount ); - - // Guaranteed to have space for these vertices. - pVertices->resize( vertexStart + texCoords.size() ); - - auto vertexIter = pVertices->begin() + vertexStart; - for ( DirectX::XMFLOAT2 const& texCoord : texCoords ) - { - vertexIter->texCoord0 = texCoord; - ++vertexIter; - } + LoadAttribute( pVertices, vertexStart, &scratch, texCoordAttr, stride, offset, components ); } if ( "TEXCOORD_1"sv == attributes[attribIndex].name ) { @@ -565,65 +565,33 @@ ModelMesh ProcessMesh( ASSERT( texCoordAttr.data->component_type == cgltf_component_type_r_32f ); ASSERT( texCoordAttr.data->type == cgltf_type_vec2 ); - std::pmr::vector texCoords{ pVertices->get_allocator() }; + size_t constexpr stride = sizeof( Vertex ); + size_t constexpr offset = offsetof( Vertex, texCoord1 ); + size_t constexpr components = 2; - size_t const floatCount = cgltf_accessor_unpack_floats( texCoordAttr.data, nullptr, 0 ); - texCoords.resize( floatCount / 2 ); - cgltf_accessor_unpack_floats( - texCoordAttr.data, reinterpret_cast( texCoords.data() ), floatCount ); - - // Guaranteed to have space for these vertices. - pVertices->resize( vertexStart + texCoords.size() ); - - auto vertexIter = pVertices->begin() + vertexStart; - for ( DirectX::XMFLOAT2 const& texCoord : texCoords ) - { - vertexIter->texCoord1 = texCoord; - ++vertexIter; - } + LoadAttribute( pVertices, vertexStart, &scratch, texCoordAttr, stride, offset, components ); } if ( "COLOR_0"sv == attributes[attribIndex].name ) { cgltf_attribute const& colorAttr = attributes[attribIndex]; ASSERT( colorAttr.data->component_type == cgltf_component_type_r_32f ); - ASSERT( colorAttr.data->type == cgltf_type_vec3 or colorAttr.data->type == cgltf_type_vec4 ); - if ( colorAttr.data->type == cgltf_type_vec3 ) + size_t constexpr stride = sizeof( Vertex ); + size_t constexpr offset = offsetof( Vertex, texCoord1 ); + size_t components = 3; + switch ( colorAttr.data->type ) { - std::pmr::vector colors{ pVertices->get_allocator() }; - - size_t const floatCount = cgltf_accessor_unpack_floats( colorAttr.data, nullptr, 0 ); - colors.resize( floatCount / 3 ); - cgltf_accessor_unpack_floats( colorAttr.data, reinterpret_cast( colors.data() ), floatCount ); - - // Guaranteed to have space for these vertices. - pVertices->resize( vertexStart + colors.size() ); - - auto vertexIter = pVertices->begin() + vertexStart; - for ( DirectX::XMFLOAT3 const& color : colors ) - { - vertexIter->color0 = { color.x, color.y, color.z, 1.0f }; - ++vertexIter; - } + case cgltf_type_vec3: + components = 3; + break; + case cgltf_type_vec4: + components = 4; + break; + default: + UNREACHABLE; } - else // Since only two options - { - std::pmr::vector colors{ pVertices->get_allocator() }; - size_t const floatCount = cgltf_accessor_unpack_floats( colorAttr.data, nullptr, 0 ); - colors.resize( floatCount / 4 ); - cgltf_accessor_unpack_floats( colorAttr.data, reinterpret_cast( colors.data() ), floatCount ); - - // Guaranteed to have space for these vertices. - pVertices->resize( vertexStart + colors.size() ); - - auto vertexIter = pVertices->begin() + vertexStart; - for ( DirectX::XMFLOAT4 const& color : colors ) - { - vertexIter->color0 = color; - ++vertexIter; - } - } + LoadAttribute( pVertices, vertexStart, &scratch, colorAttr, stride, offset, components ); } // TODO: Grab other attributes. } @@ -633,12 +601,12 @@ ModelMesh ProcessMesh( } Entity* ProcessNode( - RenderDevice* renderDevice, - EntityManager* entityManager, - Model* model, - std::pmr::vector* vertices, - std::pmr::vector* indices, - cgltf_node const& node ) + RenderDevice* renderDevice, + EntityManager* entityManager, + Model* model, + std::vector* vertices, + std::vector* indices, + cgltf_node const& node ) { DirectX::XMVECTOR vTranslation; DirectX::XMVECTOR qRotation; @@ -722,10 +690,10 @@ Entity* LoadModel( RenderDevice* renderDevice, EntityManager* entityManager, con } ); // Output data - std::pmr::vector vertices; - std::pmr::vector indices; + std::vector vertices; + std::vector indices; - cgltf_scene const* currentScene = gltfModel->scene; + cgltf_scene const* currentScene = gltfModel->scene; for ( uint32_t nodeIdx = 0; nodeIdx < currentScene->nodes_count; ++nodeIdx ) { entity->addChild( ProcessNode( diff --git a/Blaze/ModelLoader.h b/Blaze/ModelLoader.h index f2d549d..7e16bde 100644 --- a/Blaze/ModelLoader.h +++ b/Blaze/ModelLoader.h @@ -14,8 +14,8 @@ struct GlobalMemory; struct Vertex { - DirectX::XMFLOAT3 position = { 0.0f, 0.0f, 0.0f }; - DirectX::XMFLOAT3 normal = { 1.0f, 1.0f, 1.0f }; + DirectX::XMFLOAT4 position = { 0.0f, 0.0f, 0.0f, 1.0f }; + DirectX::XMFLOAT4 normal = { 1.0f, 1.0f, 1.0f, 0.0f }; DirectX::XMFLOAT2 texCoord0 = { 0.0f, 0.0f }; DirectX::XMFLOAT2 texCoord1 = { 0.0f, 0.0f }; DirectX::XMFLOAT4 color0 = { 1.0f, 1.0f, 1.0f, 1.0f };