diff --git a/ci_shadervalidator/main.cpp b/ci_shadervalidator/main.cpp index 96af5c0ed..1bb1bbafb 100644 --- a/ci_shadervalidator/main.cpp +++ b/ci_shadervalidator/main.cpp @@ -226,6 +226,17 @@ bool check_vk14(std::string const &Name) if (!check_text(gfx::preprocess_shader(gfx::shader_api::vulkan14, Name, "#define NO_MOTION_OUTPUT 1\n", {}, false, vk14_dir), "-V --target-env " + vulkan_env)) suc = false; } + // the compact vertex layout of EU07M models (vk14::vertex_kind::scene_compact), alone and with the multi draws, as the + // renderer puts the definitions before the sources (compact_kind and multidraw_kind of vulkan14indirect.cpp) + if (source.text.find("COMPACT_VERTEX") != std::string::npos) + { + std::cout << "done, compact vertices..." << std::flush; + for (std::string_view const defines : {"#define COMPACT_VERTEX\n", "#define MODEL_DRAWS\n#define COMPACT_VERTEX\n"}) + { + if (!check_text(gfx::preprocess_shader(gfx::shader_api::vulkan14, Name, std::string{defines}, {}, false, vk14_dir), "-V --target-env " + vulkan_env)) + suc = false; + } + } // the ray traced reflections (gfx.raytracing): the renderer puts the extensions and REFLECTION_RAYS before the sources // (vk14::binding_preamble()), the shaders which shade with light_common.glsl trace the rays if (source.text.find("REFLECTION_RAYS_ACTIVE") != std::string::npos) diff --git a/docs/eu07m-format.md b/docs/eu07m-format.md index dcda7226e..ebb91344c 100644 --- a/docs/eu07m-format.md +++ b/docs/eu07m-format.md @@ -267,13 +267,20 @@ parts of a node without a track stay as in its TRS. cubic rotation. Before the first and past the last key a track holds its value. - **Time.** A clip goes by the simulation time from when it started, at its pace. A looped clip starts over past its duration, another stops at its end. A clip with flag 0x2 starts on its own the first time the instance is drawn. -- **In the game.** `TAnimModel::play_clip(name, pace)` starts a clip (the self-test command `clip [pace]`), and - `stop_clips()` stops them all. Each time the instance is drawn, `RaPrepare()` samples the clips playing and sets the transforms - of the nodes (`TSubModel::SetClipTransform()`, the animation `at_Clip`), as the other animations of an instance. The nodes the - clips move are flagged animated at load, so a model with clips isn't instanced. A clip moving the joints of a skin bends the - skinned parts (see "Skinning"). +- **In the game.** `TAnimModel::play_clip(name, pace)` starts a clip (the self-test command `clip [pace]`), the + name regardless of case, and `stop_clips()` stops them all: each is held at the moment it was stopped and goes on setting its + nodes, as apart from what an instance sets as it's drawn the nodes are kept by the submodels all the instances share. Each time + the instance is drawn, `RaPrepare()` samples the clips and sets the transforms of the nodes (`TSubModel::SetClipTransform()`, + the animation `at_Clip`) before the animations of the events: an animation event of the scenery moving a node (`rotate`, + `translate`...) takes it over from a clip, one which ended or was stopped included. The nodes the clips move are flagged + animated at load, so a model with clips isn't instanced. A clip moving the joints of a skin bends the skinned parts (see + "Skinning"). +- The TRS fields of a node with a matrix (`XFRM`) are only an approximation of it: the parts the tracks of a clip don't move are + taken from the decomposed matrix, so the node doesn't jump when a clip starts moving another part of it. - **Scenery event.** `event animation clip endevent` starts the clip from its beginning at the - pace (a clip playing starts over), and a pace of 0 stops the clips of the model; the nodes stay where the clips left them. A clip + pace (a clip playing starts over), and a pace of 0 stops the clips of the model; the nodes stay where the clips left them. The + name of the clip is matched regardless of case, as the names of submodels are. An event without a pace (`clip endevent`) + plays the clip at its own pace and gives `Bad event: ... gives no pace for clip` in the log. A clip the model hasn't gives `Bad event: ... can't find clip` in the log. This is a new form of the `animation` event, which the current release doesn't read; the forms before it (`rotate`, `translate`, `digital`, `.vmd`) read as they did. @@ -293,8 +300,8 @@ the code of a vehicle, later Lua or a gauge in the cab) doesn't matter. - **In the game.** `TAnimModel::set_channel(name, value)` sets a channel (the self-test command `channel `), the name regardless of case. Only scenery events and the self-test set channels so far; a vehicle (`TDynamicObject`: its cab controls, its state) has no hook to them yet. - Each time the instance is drawn, `RaPrepare()` sets the nodes from the channels, after the clips playing, so a channel overrides a - clip on the same node. A channel not set yet has its initial value. The nodes of the joints are flagged animated at load. + Each time the instance is drawn, `RaPrepare()` sets the nodes from the channels, after the clips, so a channel overrides a + clip on the same node; an animation event of the scenery on the node takes over from both. A channel not set yet has its initial value. The nodes of the joints are flagged animated at load. - **Scenery event.** `event animation channel endevent` sets the channel. A channel the model hasn't gives `Bad event: ... can't find channel` in the log. A new form of the `animation` event, which the current release doesn't read. @@ -339,7 +346,10 @@ own. It applies to EU07M and E3D models alike. the shader (`instance_hides_part()` in `common.glsl`). - An instance shows the whole lit part or the whole dark one. Blinking and switching (`ls_On` / `ls_Off` / `ls_Blink`) change over at half the fade (opacity 0.5), not with the cross-fade of the draws instance by instance. Steady states look the same as without - the instancing. + the instancing. `ls_Dark` and `ls_Home` + change without the fade (`fVisible`) too. +- An instance with light colours of its own (`lightcolors` of its node) is drawn on its own: the colours go onto the shared + submodels as the instance is prepared, which the instances of a bucket don't each get. - Measured (Vulkan, 200 heads `s4ipcpb_dd`): with `gfx.gpucull 0` the frame 9.2 ms → 1.5–2.5 ms and 2400 → 16 draw calls, with `gfx.gpucull 1` 9.1 ms → 3.0 ms and 2410 → 26 draw calls. diff --git a/model/AnimModel.cpp b/model/AnimModel.cpp index 55b4ac9a1..9f792a6df 100644 --- a/model/AnimModel.cpp +++ b/model/AnimModel.cpp @@ -572,6 +572,9 @@ void TAnimModel::update_instanceable_flag() { // gfx.instancedlights: the lights go with the matrix of each instance (instance_light_bits()), and the instanced walk // animates each instance (RaAnimate) and prepares the model with all the parts of the lights shown (RaPrepareInstanced) if( ( iNumLights != 0 ) && ( false == Global.gfx_instancedlights ) ) { ++s_rejected_lights; return; } + // light colours of its own (lightcolors of the node) go onto the shared submodels as the instance is prepared (RaPrepare), + // which the instances of a bucket don't each get: such an instance is drawn on its own + if( std::ranges::any_of( m_lightcolors, []( glm::vec3 const &Colour ) { return Colour.r >= 0.f; } ) ) { ++s_rejected_lights; return; } if( !m_animlist.empty() ) { ++s_rejected_animlist; return; } if( submodel_tree_blocks_instancing( pModel->Root ) ) { ++s_rejected_animated_submodel; return; } m_instanceable = true; @@ -758,14 +761,15 @@ void TAnimModel::RaPrepare() TSubModel::iVariantSeed = 0; TSubModel::pasText = &asText; // przekazanie tekstu do wyświetlacza (!!!! do przemyślenia) - for (auto entry : m_animlist) { - entry->PrepareModel(); - } + // the clips first, then the channels, as they may set the same nodes; an animation event of the scenery on a node (rotate, + // translate...) takes over from both, a clip which ended or was stopped included, as the later one set on the submodel counts apply_clips(); - // after the clips playing, as they may set the same nodes if( ( pModel != nullptr ) && ( false == pModel->channels().empty() ) ) { apply_channels(); } + for (auto entry : m_animlist) { + entry->PrepareModel(); + } } // the state of the lit and the dark part of a numbered light, as RaPrepare() shows them @@ -809,7 +813,11 @@ bool TAnimModel::play_clip( std::string_view const Name, float const Speed ) { if( pModel == nullptr ) { return false; } auto const &clips { pModel->clips() }; - auto const found { std::ranges::find( clips, Name, &animation::clip::name ) }; + // the name regardless of case, as the submodels are found by theirs + auto const same { [ Name ]( animation::clip const &Clip ) { + return std::ranges::equal( Clip.name, Name, []( char const Left, char const Right ) { + return std::tolower( static_cast( Left ) ) == std::tolower( static_cast( Right ) ); } ); } }; + auto const found { std::ranges::find_if( clips, same ) }; if( found == clips.end() ) { return false; } auto const index { static_cast( std::distance( clips.begin(), found ) ) }; std::erase_if( m_clips, [ index ]( clip_playing const &Playing ) { return Playing.clip == index; } ); @@ -820,7 +828,15 @@ bool TAnimModel::play_clip( std::string_view const Name, float const Speed ) { void TAnimModel::stop_clips() { - m_clips.clear(); + // the clips are held where they are, not dropped: the pose lives in the submodels the instances of the model share, so the + // nodes of this instance are set again for each of its draws (apply_clips()), or another instance playing a clip would leave + // them at rest + auto const now { Timer::GetTime() }; + for( auto &playing : m_clips ) { + if( false == playing.held.has_value() ) { + playing.held = ( now - playing.start ) * playing.speed; + } + } m_autoplaystarted = true; } @@ -891,7 +907,7 @@ void TAnimModel::apply_clips() { auto const count { pModel->submodel_count() }; for( auto const &playing : m_clips ) { auto const &clip { clips[ playing.clip ] }; - auto const time { animation::clip_time( clip, ( now - playing.start ) * playing.speed ) }; + auto const time { animation::clip_time( clip, playing.held.value_or( ( now - playing.start ) * playing.speed ) ) }; for( auto const &node : clip.nodes ) { if( ( node.node < 0 ) || ( node.node >= count ) ) { continue; } root[ node.node ].SetClipTransform( animation::local_transform( clip, node, time ) ); diff --git a/model/AnimModel.h b/model/AnimModel.h index a5567679d..c15edec8c 100644 --- a/model/AnimModel.h +++ b/model/AnimModel.h @@ -20,6 +20,8 @@ http://mozilla.org/MPL/2.0/. #include "vehicle/DynObj.h" #include "scene/scenenode.h" +#include + const int iMaxNumLights = 8; float const DefaultDarkThresholdLevel { 0.325f }; @@ -192,7 +194,8 @@ public: // starts the animation clip of the model (EU07M) with the name from its beginning, at Speed times its pace; a clip already playing // starts over. false when the model has no clip of the name bool play_clip( std::string_view Name, float Speed = 1.f ); - // stops the clips playing, the nodes they moved stay where they were last put till the next clip moves them + // stops the clips playing: each is held at the moment it was stopped, so the nodes stay where they were put, till a clip + // started on them or an animation event of the scenery moves them void stop_clips(); // sets the channel of the model (EU07M) with the name to Value, kept within its range; the joints and the clips it's bound to // follow the next time the instance is drawn. false when the model has no channel of the name @@ -248,6 +251,7 @@ public: std::size_t clip { 0 }; double start { 0.0 }; float speed { 1.f }; + std::optional held; // stopped (stop_clips()): the time into the clip it stays at }; std::vector m_clips; bool m_autoplaystarted { false }; // the clips of the model to play on their own were started diff --git a/model/Model3d.cpp b/model/Model3d.cpp index a05824b88..f5dbef139 100644 --- a/model/Model3d.cpp +++ b/model/Model3d.cpp @@ -1527,9 +1527,10 @@ void TSubModel::create_geometry(std::size_t &Indexoffset, std::size_t &Vertexoff // since we're comparing squared radii, we need to square it back for correct results m_boundingradius *= m_boundingradius; auto const submodeloffset{offset(std::numeric_limits::max())}; - for (auto const &vertex : GfxRenderer->Vertices(m_geometry.handle)) + // the positions alone: the vertices of an EU07M model on Vulkan would be made and kept whole for it + for (auto const &position : GfxRenderer->Positions(m_geometry.handle)) { - squaredradius = glm::length2(submodeloffset + vertex.position); + squaredradius = glm::length2(submodeloffset + position); if (squaredradius > m_boundingradius) { m_boundingradius = squaredradius; @@ -1653,9 +1654,9 @@ float TSubModel::MaxY(float4x4 const &m) if (m_geometry.handle != null_handle) { - for (auto const &vertex : GfxRenderer->Vertices(m_geometry.handle)) + for (auto const &position : GfxRenderer->Positions(m_geometry.handle)) { - maxy = std::max(maxy, m[0][1] * vertex.position.x + m[1][1] * vertex.position.y + m[2][1] * vertex.position.z + m[3][1]); + maxy = std::max(maxy, m[0][1] * position.x + m[1][1] * position.y + m[2][1] * position.z + m[3][1]); } } else if (false == Vertices.empty()) @@ -1797,17 +1798,30 @@ glm::vec3 TSubModel::offset(float const Geometrytestoffsetthreshold) const // offset of zero generally means the submodel has optimized identity matrix // for such cases we resort to an estimate from submodel geometry // TODO: do proper bounding area calculation for submodel when loading mesh and grab the centre point from it here - auto const &vertices{(m_geometry.handle != null_handle ? GfxRenderer->Vertices(m_geometry.handle) : Vertices)}; - if (false == vertices.empty()) + // the positions alone: the vertices of an EU07M model on Vulkan would be made and kept whole for them + gfx::position_array positions; + if (m_geometry.handle != null_handle) + { + positions = GfxRenderer->Positions(m_geometry.handle); + } + else + { + positions.reserve(Vertices.size()); + for (auto const &vertex : Vertices) + { + positions.push_back(vertex.position); + } + } + if (false == positions.empty()) { // transformation matrix for the submodel can still contain rotation and/or scaling, // so we pass the vertex positions through it rather than just grab them directly offset = glm::vec3(); - auto const vertexfactor{1.f / vertices.size()}; + auto const vertexfactor{1.f / positions.size()}; auto const transformationmatrix{glm::make_mat4(parentmatrix.readArray())}; - for (auto const &vertex : vertices) + for (auto const &position : positions) { - offset += glm::vec3{transformationmatrix * glm::vec4{vertex.position, 1}} * vertexfactor; + offset += glm::vec3{transformationmatrix * glm::vec4{position, 1}} * vertexfactor; } } } diff --git a/model/eu07m/eu07m_load.cpp b/model/eu07m/eu07m_load.cpp index 9080d637f..3df40bfe9 100644 --- a/model/eu07m/eu07m_load.cpp +++ b/model/eu07m/eu07m_load.cpp @@ -18,6 +18,7 @@ http://mozilla.org/MPL/2.0/. #include "utilities/utilities.h" #include "utilities/wholefile.h" +#include #include #include @@ -148,6 +149,10 @@ void submodel_builder::build(std::uint32_t const Index, TSubModel &Submodel) { // 0 is no limit (a model made anew); a converted one carries the range of its E3D submodel. the submodel keeps its default Submodel.fSquareMaxDist = node.range_max_sq > 0.f ? node.range_max_sq : Submodel.fSquareMaxDist; transform(Index, Submodel); + // a model made anew has no E3D flags (legacy_flags 0): a node of it with a transform keeps it as one converted from E3D does + // (0x8000), or the walk and posed_transform() would leave it out, and with it the joints of the skins + if ((node.legacy_flags == 0) && (Submodel.iMatrix >= 0) && (m_transforms[Index] != glm::mat4{1.f})) + Submodel.iFlags |= 0x8000; auto const parts{parts_of(m_model, node)}; Submodel.b_Anim = parts.animation; Submodel.eType = TP_ROTATOR; @@ -398,6 +403,20 @@ animation::node_tracks &node_entry(animation::clip &Clip, eu07m::model const &Mo entry.rest_translation = {record.translation[0], record.translation[1], record.translation[2]}; entry.rest_rotation = glm::quat{record.rotation[3], record.rotation[0], record.rotation[1], record.rotation[2]}; entry.rest_scale = {record.scale[0], record.scale[1], record.scale[2]}; + if ((record.flags & eu07m::node_flags::matrix) != 0) { + // the TRS fields of a node with a matrix (XFRM) are only an approximation of it: the parts the tracks leave are taken from + // the matrix itself, so the node doesn't jump when a clip starts moving one of its parts + auto const matrix{eu07m::local_transform(Model, Node)}; + glm::vec3 scale{glm::length(glm::vec3{matrix[0]}), glm::length(glm::vec3{matrix[1]}), glm::length(glm::vec3{matrix[2]})}; + if (glm::determinant(glm::mat3{matrix}) < 0.f) + scale.x = -scale.x; // a mirroring matrix: the rotation stays a proper one + if ((scale.x != 0.f) && (scale.y != 0.f) && (scale.z != 0.f)) { + glm::mat3 const rotation{glm::vec3{matrix[0]} / scale.x, glm::vec3{matrix[1]} / scale.y, glm::vec3{matrix[2]} / scale.z}; + entry.rest_translation = glm::vec3{matrix[3]}; + entry.rest_rotation = glm::normalize(glm::quat_cast(rotation)); + entry.rest_scale = scale; + } + } return Clip.nodes.emplace_back(entry); } diff --git a/model/skinning.cpp b/model/skinning.cpp index f20bdf3e3..4e346b8f4 100644 --- a/model/skinning.cpp +++ b/model/skinning.cpp @@ -58,15 +58,18 @@ std::vector palette(glm::mat4 const &Mesh, std::span return result; } -bool rest_pose(std::span const Palette, float const Tolerance) +bool rest_pose(std::span const Palette, float const Tolerance, float const Scale) { glm::mat4 const identity{1.f}; - return std::ranges::all_of(Palette, [&](glm::mat4 const &Matrix) { + // the translation is rounded in proportion to the distances the palette was made of + auto const translation{Tolerance * std::max(1.f, Scale)}; + return std::ranges::all_of(Palette, [&identity, Tolerance, translation](glm::mat4 const &Matrix) { for (int column = 0; column < 4; ++column) { + auto const limit{column == 3 ? translation : Tolerance}; for (int row = 0; row < 4; ++row) { - if (std::abs(Matrix[column][row] - identity[column][row]) > Tolerance) + if (std::abs(Matrix[column][row] - identity[column][row]) > limit) { return false; } @@ -76,6 +79,17 @@ bool rest_pose(std::span const Palette, float const Tolerance) }); } +float palette_scale(glm::mat4 const &Mesh, std::span const Joints) +{ + auto const reach{[](glm::mat4 const &Matrix) { return std::max({std::abs(Matrix[3][0]), std::abs(Matrix[3][1]), std::abs(Matrix[3][2])}); }}; + auto scale{reach(Mesh)}; + for (auto const &joint : Joints) + { + scale = std::max(scale, reach(joint)); + } + return scale; +} + namespace { @@ -102,6 +116,15 @@ glm::vec3 direction(glm::mat3 const &Rotation, glm::vec3 const &Vector) return length > 0.f ? moved / length : Vector; } +// what moves the normals: the inverse transpose of the rotation, here as its cofactors (the same direction, without the +// division), with the sign of the determinant so a mirroring matrix keeps the normals facing out. the rotation itself would +// tilt the normals of a part squashed by a scale +glm::mat3 normal_matrix(glm::mat3 const &Rotation) +{ + glm::mat3 const cofactors{glm::cross(Rotation[1], Rotation[2]), glm::cross(Rotation[2], Rotation[0]), glm::cross(Rotation[0], Rotation[1])}; + return glm::dot(Rotation[0], cofactors[0]) < 0.f ? -cofactors : cofactors; +} + } // namespace void skin_vertices(std::span const Rest, std::span const Weights, std::span const Palette, std::span const Out) @@ -119,7 +142,7 @@ void skin_vertices(std::span const Rest, std::span vertex_weights unpack(std::array co // Mesh and Joint posed from the root of the model, so the placement of the instance cancels out std::vector palette(glm::mat4 const &Mesh, std::span Joints, std::span InverseBind); -// true when every matrix of the palette leaves the vertices where they are (within Tolerance): the rest pose -bool rest_pose(std::span Palette, float Tolerance = 1e-5f); +// true when every matrix of the palette leaves the vertices where they are: the rest pose. the rotation within Tolerance, the +// translation within Tolerance times Scale (at least 1), the distance from the origin of what the palette was made of +// (palette_scale()), as the float chain rounds in proportion to it +bool rest_pose(std::span Palette, float Tolerance = 1e-5f, float Scale = 1.f); +// the largest distance from the origin along an axis of the mesh and the joints a palette is made of +float palette_scale(glm::mat4 const &Mesh, std::span Joints); // the vertices moved by the palette: positions by the blended matrix, normals and tangents by its rotation, renormalised. a joint out // of the palette counts as the identity. Out has as many vertices as Rest diff --git a/rendering/common/scenewalk.cpp b/rendering/common/scenewalk.cpp index c4a52d44e..033df86ba 100644 --- a/rendering/common/scenewalk.cpp +++ b/rendering/common/scenewalk.cpp @@ -1035,8 +1035,9 @@ void scene_walk::Queue_Instances( scene::basic_cell const *Cell ) } } if( m_instancecullpass && bucket_has_lights( instancebucket ) ) { - // the culling of the gpu takes the light states of the instances from their records - gfx::walk_sync::change change; + // the culling of the gpu takes the light states of the instances from their records. only the first pass of the frame has + // them to refresh (Refresh_Light_Records() checks again under the lock), so the others don't stop the walkers for it + gfx::walk_sync::change change { instancebucket.lightsframe != m_framestamp }; Refresh_Light_Records( instancebucket ); } if( buckettests && Bucket_Culled( instancebucket ) ) { diff --git a/rendering/common/skincache.cpp b/rendering/common/skincache.cpp index b5026218d..a2bbf6f15 100644 --- a/rendering/common/skincache.cpp +++ b/rendering/common/skincache.cpp @@ -65,8 +65,9 @@ geometry_handle skin_cache::geometry(TSubModel &Submodel, TModel3d const &Model, { joints.emplace_back(joint != nullptr ? joint->posed_transform() : glm::mat4{1.f}); } - auto palette{skinning::palette(Submodel.posed_transform(), joints, skin.inverse_bind)}; - cached.posed = (false == skinning::rest_pose(palette)); + auto const mesh{Submodel.posed_transform()}; + auto palette{skinning::palette(mesh, joints, skin.inverse_bind)}; + cached.posed = (false == skinning::rest_pose(palette, 1e-5f, skinning::palette_scale(mesh, joints))); if (cached.posed && (palette != cached.palette)) { cached.pending = true; diff --git a/rendering/geometrybank.cpp b/rendering/geometrybank.cpp index 7e82cce68..ce84c9be1 100644 --- a/rendering/geometrybank.cpp +++ b/rendering/geometrybank.cpp @@ -496,6 +496,24 @@ geometry_bank::indices( gfx::geometry_handle const &Geometry ) const { return geometry_bank::chunk( Geometry ).indices; } +// the positions of the vertices of specified chunk: of a chunk of the compact layout its stream as it is, without making the +// basic_vertex of it which vertices() keeps for good +position_array +geometry_bank::positions( gfx::geometry_handle const &Geometry ) const { + + auto const &chunk { geometry_bank::chunk( Geometry ) }; + std::scoped_lock lock { m_decodemutex }; + if( chunk.compact() ) { + return chunk.positions; + } + position_array positions; + positions.reserve( chunk.vertices.size() ); + for( auto const &vertex : chunk.vertices ) { + positions.push_back( vertex.position ); + } + return positions; +} + // provides direct access to vertex data of specfied chunk vertex_array const & geometry_bank::vertices( gfx::geometry_handle const &Geometry ) const { @@ -751,6 +769,12 @@ geometrybank_manager::indices( gfx::geometry_handle const &Geometry ) const { } // provides direct access to vertex data of specfied chunk +gfx::position_array +geometrybank_manager::positions( gfx::geometry_handle const &Geometry ) const { + + return bank( Geometry ).first->positions( Geometry ); +} + gfx::vertex_array const & geometrybank_manager::vertices( gfx::geometry_handle const &Geometry ) const { diff --git a/rendering/geometrybank.h b/rendering/geometrybank.h index 8f1b20370..b44ee7f4b 100644 --- a/rendering/geometrybank.h +++ b/rendering/geometrybank.h @@ -181,6 +181,8 @@ public: void release(); // provides direct access to index data of specfied chunk auto indices( gfx::geometry_handle const &Geometry ) const -> gfx::index_array const &; + // the positions of the vertices of specified chunk, without the basic_vertex of a chunk of the compact layout being made + auto positions( gfx::geometry_handle const &Geometry ) const -> gfx::position_array; // provides direct access to vertex data of specfied chunk auto vertices( gfx::geometry_handle const &Geometry ) const -> gfx::vertex_array const &; // provides direct access to vertex user data of specfied chunk @@ -324,6 +326,8 @@ public: ++First; } } // provides direct access to index data of specfied chunk auto indices( gfx::geometry_handle const &Geometry ) const -> gfx::index_array const &; + // the positions of the vertices of specified chunk (geometry_bank::positions()) + auto positions( gfx::geometry_handle const &Geometry ) const -> gfx::position_array; // provides direct access to vertex data of specfied chunk auto vertices( gfx::geometry_handle const &Geometry ) const -> gfx::vertex_array const &; // what the banks keep in the process, for the debug panel: banks, chunks with their vertex bytes, index bytes and user data bytes diff --git a/rendering/renderer.h b/rendering/renderer.h index 14712c926..4b3898b45 100644 --- a/rendering/renderer.h +++ b/rendering/renderer.h @@ -134,6 +134,15 @@ public: virtual auto Indices( gfx::geometry_handle const &Geometry ) const->gfx::index_array const & = 0; // provides direct access to vertex data of specfied chunk virtual auto Vertices( gfx::geometry_handle const &Geometry ) const ->gfx::vertex_array const & = 0; + // the positions of the vertices of specified chunk, for what needs no more of them (the bounds of a model as it loads): a renderer + // keeping the compact streams of an EU07M model gives them without making and keeping the basic_vertex Vertices() would + virtual auto Positions( gfx::geometry_handle const &Geometry ) const -> gfx::position_array { + auto const &vertices { Vertices( Geometry ) }; + gfx::position_array positions; + positions.reserve( vertices.size() ); + for( auto const &vertex : vertices ) { + positions.push_back( vertex.position ); } + return positions; } // provides direct access to vertex user data of specfied chunk virtual auto UserData( gfx::geometry_handle const &Geometry ) const ->gfx::userdata_array const & = 0; // kind of geometry of specified chunk, -1 when the renderer doesn't tell diff --git a/rendering/vulkan14/vulkan14geometry.cpp b/rendering/vulkan14/vulkan14geometry.cpp index 65e43b687..36d095780 100644 --- a/rendering/vulkan14/vulkan14geometry.cpp +++ b/rendering/vulkan14/vulkan14geometry.cpp @@ -12,6 +12,7 @@ http://mozilla.org/MPL/2.0/. #include "rendering/vulkan14/vulkan14texture.h" #include +#include namespace vk14 { @@ -25,8 +26,24 @@ gfx::vertex_userdata const no_userdata{glm::vec4{0.f, 0.f, 0.f, 1.f}}; // the numbers the data of the banks get, unique across the banks (a bank made where another one was gets a new one) std::atomic g_generation{0}; +// set when a variant of a program for the compact layout didn't compile (compact_layout_failed()) +std::atomic g_compactfailed{false}; + } // namespace +void compact_layout_failed(std::string_view const Program) +{ + if (false == g_compactfailed.exchange(true)) + { + log_error(std::format("the compact vertex variant of \"{}\" didn't compile; EU07M models are drawn from basic_vertex from now on", Program)); + } +} + +bool compact_layout_usable() +{ + return false == g_compactfailed.load(); +} + VkPrimitiveTopology topology_of(unsigned int const Type) { switch (Type) @@ -150,7 +167,7 @@ bool vulkan14_geometrybank::build() release_(); // the compact layout when every chunk of the bank is of it (an EU07M model); otherwise its chunks go as basic_vertex - m_compact = (false == m_chunks.empty()) && std::ranges::all_of(m_chunks, [](geometry_chunk const &Chunk) { return Chunk.compact(); }); + m_compact = compact_layout_usable() && (false == m_chunks.empty()) && std::ranges::all_of(m_chunks, [](geometry_chunk const &Chunk) { return Chunk.compact(); }); if (false == m_compact) { for (auto &chunk : m_chunks) @@ -303,7 +320,8 @@ bool vulkan14_geometrybank::source(gfx::geometry_handle const &Geometry, chunk_s return false; } std::lock_guard lock(m_mutex); - if (m_layoutdirty) + // a bank of the compact layout whose programs can't be had is made anew of basic_vertex + if (m_layoutdirty || (m_compact && (false == compact_layout_usable()))) { build(); } diff --git a/rendering/vulkan14/vulkan14geometry.h b/rendering/vulkan14/vulkan14geometry.h index daed8db8d..7ad767074 100644 --- a/rendering/vulkan14/vulkan14geometry.h +++ b/rendering/vulkan14/vulkan14geometry.h @@ -22,6 +22,7 @@ http://mozilla.org/MPL/2.0/. #include #include #include +#include namespace vk14 { @@ -56,6 +57,12 @@ struct chunk_source // the vulkan primitive of a primitive of opengl (GL_TRIANGLES ...) VkPrimitiveTopology topology_of(unsigned int Type); +// a variant of a scene program for the compact layout didn't compile: the banks of EU07M models are made of basic_vertex +// from then on, those made already when they are drawn next, so the models stay in sight. told once in the log +void compact_layout_failed(std::string_view Program); +// false once compact_layout_failed() was called +bool compact_layout_usable(); + class vulkan14_geometrybank : public gfx::geometry_bank { public: diff --git a/rendering/vulkan14/vulkan14indirect.cpp b/rendering/vulkan14/vulkan14indirect.cpp index 611f139fb..48f5d5d5d 100644 --- a/rendering/vulkan14/vulkan14indirect.cpp +++ b/rendering/vulkan14/vulkan14indirect.cpp @@ -320,6 +320,11 @@ std::shared_ptr make_variant(vk14::shader_library &Shaders, catch (gfx::shader_exception const &Exception) { vk14::log_error(Exception.what()); + if (Kind.compact) + { + // without the variant the chunks of the compact layout would be left out for good: their banks go back to basic_vertex + vk14::compact_layout_failed(Name); + } } return nullptr; } diff --git a/rendering/vulkan14/vulkan14renderer.h b/rendering/vulkan14/vulkan14renderer.h index 49e6d5fb9..c28460167 100644 --- a/rendering/vulkan14/vulkan14renderer.h +++ b/rendering/vulkan14/vulkan14renderer.h @@ -90,6 +90,7 @@ class vulkan14_renderer : public gfx_renderer, public gfx::scene_walk bool Skin_Geometry(gfx::geometry_handle const &Geometry, std::shared_ptr const &Source, std::span Palette) override; gfx::index_array const &Indices(gfx::geometry_handle const &Geometry) const override; gfx::vertex_array const &Vertices(gfx::geometry_handle const &Geometry) const override; + gfx::position_array Positions(gfx::geometry_handle const &Geometry) const override; gfx::userdata_array const &UserData(gfx::geometry_handle const &Geometry) const override; int Primitive(gfx::geometry_handle const &Geometry) const override; // the sleepers and timbers are drawn by the walk of the scene (gfx::scene_walk), as in the opengl renderer diff --git a/rendering/vulkan14/vulkan14scene.cpp b/rendering/vulkan14/vulkan14scene.cpp index 1026a7229..7ccf9599f 100644 --- a/rendering/vulkan14/vulkan14scene.cpp +++ b/rendering/vulkan14/vulkan14scene.cpp @@ -1353,6 +1353,11 @@ gfx::vertex_array const &vulkan14_renderer::Vertices(gfx::geometry_handle const return m_geometry.vertices(Geometry); } +gfx::position_array vulkan14_renderer::Positions(gfx::geometry_handle const &Geometry) const +{ + return m_geometry.positions(Geometry); +} + gfx::userdata_array const &vulkan14_renderer::UserData(gfx::geometry_handle const &Geometry) const { return m_geometry.userdata(Geometry); diff --git a/shaders/ogl46/instance_cull.comp b/shaders/ogl46/instance_cull.comp index 5a6fd6ca4..c874d880e 100644 --- a/shaders/ogl46/instance_cull.comp +++ b/shaders/ogl46/instance_cull.comp @@ -226,7 +226,11 @@ void write(uint entry, instance_record record, vec3 location, instance_cull_job modelviews[slot] = originview * mat4(record.rotationscale[0], record.rotationscale[1], record.rotationscale[2], vec4(record.position, 1.0)); return; } - modelviews[slot] = view * mat4(record.rotationscale[0], record.rotationscale[1], record.rotationscale[2], vec4(location, 1.0)); + // the word of the light parts (w of the first column, instance_light_bits()) is put back after the product, so a view with a + // translation can't add to the first column with it + mat4 modelview = view * mat4(vec4(record.rotationscale[0].xyz, 0.0), record.rotationscale[1], record.rotationscale[2], vec4(location, 1.0)); + modelview[0][3] = record.rotationscale[0].w; + modelviews[slot] = modelview; } void main() diff --git a/shaders/vk14/instance_cull.comp b/shaders/vk14/instance_cull.comp index c6b8e0983..b07a59b48 100644 --- a/shaders/vk14/instance_cull.comp +++ b/shaders/vk14/instance_cull.comp @@ -234,7 +234,11 @@ void write(uint entry, instance_record record, vec3 location, instance_cull_job modelviews[slot] = originview * mat4(record.rotationscale[0], record.rotationscale[1], record.rotationscale[2], vec4(record.position, 1.0)); return; } - modelviews[slot] = view * mat4(record.rotationscale[0], record.rotationscale[1], record.rotationscale[2], vec4(location, 1.0)); + // the word of the light parts (w of the first column, instance_light_bits()) is put back after the product, so a view with a + // translation can't add to the first column with it + mat4 modelview = view * mat4(vec4(record.rotationscale[0].xyz, 0.0), record.rotationscale[1], record.rotationscale[2], vec4(location, 1.0)); + modelview[0][3] = record.rotationscale[0].w; + modelviews[slot] = modelview; } void main() diff --git a/shaders/vk14/skin.comp b/shaders/vk14/skin.comp index 2f26b9130..7f5d8846e 100644 --- a/shaders/vk14/skin.comp +++ b/shaders/vk14/skin.comp @@ -64,7 +64,12 @@ void main() } vec3 position = (blended * vec4(source.position_u.xyz, 1.0)).xyz; mat3 rotation = mat3(blended); - vec3 normal = skin_direction(rotation, source.normal_v.xyz); + // the normals by the cofactors of the rotation (the direction of its inverse transpose), with the sign of its determinant, + // as skinning::skin_vertices() moves them on the cpu + mat3 cofactors = mat3(cross(rotation[1], rotation[2]), cross(rotation[2], rotation[0]), cross(rotation[0], rotation[1])); + if (dot(rotation[0], cofactors[0]) < 0.0) + cofactors = -cofactors; + vec3 normal = skin_direction(cofactors, source.normal_v.xyz); vec3 tangent = skin_direction(rotation, source.tangent.xyz); uint target = (skin_counts.y + index) * 12u; skin_output[target + 0u] = position.x; diff --git a/tests/unit/test_skinning.cpp b/tests/unit/test_skinning.cpp index 2bfb0bb7d..9c57c01b9 100644 --- a/tests/unit/test_skinning.cpp +++ b/tests/unit/test_skinning.cpp @@ -140,8 +140,29 @@ TEST_CASE("skinning: the bind pose leaves the vertices where they are") { auto const placed{glm::translate(glm::rotate(glm::mat4{1.f}, 0.7f, glm::vec3{0.f, 1.f, 0.f}), glm::vec3{10.f, 5.f, -3.f})}; std::vector const placedjoints{placed, placed * tipbind}; CHECK(skinning::rest_pose(skinning::palette(placed, placedjoints, inversebind), 1e-4f)); + // the default tolerance, scaled by the distances the palette was made of: a model far from the origin is at rest as well + auto const distant{glm::translate(glm::mat4{1.f}, glm::vec3{1500.f, 40.f, -2300.f})}; + std::vector const distantjoints{distant, distant * tipbind}; + CHECK(skinning::rest_pose(skinning::palette(distant, distantjoints, inversebind), 1e-5f, skinning::palette_scale(distant, distantjoints))); std::vector const moved{glm::translate(glm::mat4{1.f}, glm::vec3{0.f, 0.1f, 0.f})}; CHECK_FALSE(skinning::rest_pose(moved)); + CHECK_FALSE(skinning::rest_pose(moved, 1e-5f, skinning::palette_scale(distant, distantjoints))); +} + +TEST_CASE("skinning: the normals of a part scaled unevenly stay square to its surface") { + // a joint stretched twice along x: a surface at 45 degrees leans towards y, its normal towards x less + std::vector const palette{glm::scale(glm::mat4{1.f}, glm::vec3{2.f, 1.f, 1.f})}; + auto vertex{vertex_at({0.f, 0.f, 0.f})}; + vertex.normal = glm::normalize(glm::vec3{1.f, 1.f, 0.f}); + std::vector const rest{vertex}; + std::vector const weights{{{0, 0, 0, 0}, {1.f, 0.f, 0.f, 0.f}}}; + std::vector out(1); + skinning::skin_vertices(rest, weights, palette, out); + CHECK(close_to(out[0].normal, glm::normalize(glm::vec3{0.5f, 1.f, 0.f}))); + // a mirroring joint keeps the normal facing out + std::vector const mirror{glm::scale(glm::mat4{1.f}, glm::vec3{-1.f, 1.f, 1.f})}; + skinning::skin_vertices(rest, weights, mirror, out); + CHECK(close_to(out[0].normal, glm::normalize(glm::vec3{-1.f, 1.f, 0.f}))); } TEST_CASE("skinning: a joint turned moves its vertices, blended by the weights") { diff --git a/world/Event.cpp b/world/Event.cpp index 24ab6d4c3..4d3dc50e9 100644 --- a/world/Event.cpp +++ b/world/Event.cpp @@ -1662,11 +1662,18 @@ animation_event::deserialize_( cParser &Input, scene::scratch_data &Scratchpad ) >> m_animationparams[ 3 ]; } else if( token == "clip" ) { - // an animation clip of the model (EU07M): its name and pace; a pace of 0 stops the clips playing - Input.getTokens(); + // an animation clip of the model (EU07M): its name, as written (the parser would lowercase it), and pace; a pace of 0 + // stops the clips playing + Input.getTokens( 1, false ); Input >> m_animationsubmodel; m_animationtype = clip_animation; Input.getTokens( 1 ); + if( is_keyword( Input.peek() ) ) { + // no pace: the clip plays at its own, and the keyword read is the next token of the event, not a number of it + m_animationparams[ 0 ] = 1.0; + ErrorLog( "Bad event: \"" + m_name + "\" (type: " + type() + ") gives no pace for clip \"" + m_animationsubmodel + "\", it plays at its own" ); + return; + } Input >> m_animationparams[ 0 ]; } else if( token == "channel" ) {