18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-02 18:56:49 +01:00

Merge the review fixes of the PRs below from feat/eu07m-channels into feat/eu07m-gref

This commit is contained in:
maj00r
2026-11-29 18:23:01 +01:00
26 changed files with 284 additions and 48 deletions
+11
View File
@@ -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)
+24 -11
View File
@@ -298,13 +298,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 <node> <name> [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 <node> <name> [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 <name> animation <delay> <model> clip <clip> <pace> 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 <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.
@@ -317,12 +324,15 @@ the code of a vehicle, later Lua or a gauge in the cab) doesn't matter.
- **Bindings the game follows.** A joint (`JOIN` of a node): the value of the target turns a hinge (radians) or a screw, or moves a
slider (metres) along the axis of the joint, from the transform of the node, within the limits of the joint when it has them; a
fixed or a ball joint leaves the node at rest. A clip (its index in `ACLP`): the value of the target is the share of the duration
fixed or a ball joint leaves the node at rest. The node is modelled at the `rest` value of the joint: the joint moves it by the
value less `rest` (the limits are values of the joint, as `rest` is). A clip (its index in `ACLP`): the value of the target is the share of the duration
of the clip, 0 to 1, so the channel scrubs the clip without playing it. Bindings to variants and light slots are read but not
followed yet.
- **In the game.** `TAnimModel::set_channel(name, value)` sets a channel (the self-test command `channel <node> <name> <value>`).
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.
- **In the game.** `TAnimModel::set_channel(name, value)` sets a channel (the self-test command `channel <node> <name> <value>`),
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, 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 <name> animation <delay> <model> channel <channel> <value> 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.
@@ -367,7 +377,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.
+31 -8
View File
@@ -15,6 +15,9 @@ http://mozilla.org/MPL/2.0/.
#include "stdafx.h"
#include "model/AnimModel.h"
#include <algorithm>
#include <cctype>
#include "rendering/renderer.h"
#include "model/MdlMngr.h"
#include "simulation/simulation.h"
@@ -569,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;
@@ -755,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
@@ -806,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<unsigned char>( Left ) ) == std::tolower( static_cast<unsigned char>( Right ) ); } ); } };
auto const found { std::ranges::find_if( clips, same ) };
if( found == clips.end() ) { return false; }
auto const index { static_cast<std::size_t>( std::distance( clips.begin(), found ) ) };
std::erase_if( m_clips, [ index ]( clip_playing const &Playing ) { return Playing.clip == index; } );
@@ -817,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;
}
@@ -825,7 +844,11 @@ bool TAnimModel::set_channel( std::string_view const Name, float const Value ) {
if( pModel == nullptr ) { return false; }
auto const &channels { pModel->channels() };
auto const found { std::ranges::find( channels, Name, &animation::channel::name ) };
// the name regardless of case, as the submodels are found by theirs
auto const same { [ Name ]( animation::channel const &Channel ) {
return std::ranges::equal( Channel.name, Name, []( char const Left, char const Right ) {
return std::tolower( static_cast<unsigned char>( Left ) ) == std::tolower( static_cast<unsigned char>( Right ) ); } ); } };
auto const found { std::ranges::find_if( channels, same ) };
if( found == channels.end() ) { return false; }
if( m_channels.size() != channels.size() ) {
m_channels.resize( channels.size() );
@@ -884,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 ) );
+5 -1
View File
@@ -20,6 +20,8 @@ http://mozilla.org/MPL/2.0/.
#include "vehicle/DynObj.h"
#include "scene/scenenode.h"
#include <optional>
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<double> held; // stopped (stop_clips()): the time into the clip it stays at
};
std::vector<clip_playing> m_clips;
bool m_autoplaystarted { false }; // the clips of the model to play on their own were started
+23 -9
View File
@@ -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<float>::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;
}
}
}
+2
View File
@@ -140,6 +140,8 @@ glm::mat4 joint_transform(joint_binding const &Joint, float const Value)
{
value = std::clamp(value, std::min(Joint.minimum, Joint.maximum), std::max(Joint.minimum, Joint.maximum));
}
// the node is modelled at the rest value of the joint: at that value it stays as modelled
value -= Joint.rest_value;
auto const length{glm::length(Joint.axis)};
if (length <= 0.f)
{
+1
View File
@@ -97,6 +97,7 @@ struct joint_binding
float maximum{0.f};
float pitch{0.f}; // metres for each turn of a screw
glm::mat4 rest{1.f}; // the transform of the node within its parent, which the joint moves from
float rest_value{0.f}; // the value of the joint the node is modelled at (JOIN.rest): the joint moves by the value less it
};
enum class binding_target : std::uint8_t
+20
View File
@@ -18,6 +18,7 @@ http://mozilla.org/MPL/2.0/.
#include "utilities/utilities.h"
#include "utilities/wholefile.h"
#include <glm/gtc/quaternion.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <algorithm>
@@ -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);
}
@@ -439,6 +458,7 @@ std::optional<animation::joint_binding> make_joint(eu07m::model const &Model, st
result.maximum = joint.maximum;
result.pitch = joint.pitch;
result.rest = Locals[record.target];
result.rest_value = joint.rest;
return result;
}
+27 -4
View File
@@ -58,15 +58,18 @@ std::vector<glm::mat4> palette(glm::mat4 const &Mesh, std::span<glm::mat4 const>
return result;
}
bool rest_pose(std::span<glm::mat4 const> const Palette, float const Tolerance)
bool rest_pose(std::span<glm::mat4 const> 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<glm::mat4 const> const Palette, float const Tolerance)
});
}
float palette_scale(glm::mat4 const &Mesh, std::span<glm::mat4 const> 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<gfx::basic_vertex const> const Rest, std::span<vertex_weights const> const Weights, std::span<glm::mat4 const> const Palette, std::span<gfx::basic_vertex> const Out)
@@ -119,7 +142,7 @@ void skin_vertices(std::span<gfx::basic_vertex const> const Rest, std::span<vert
auto const matrix{blended(Weights[idx], Palette)};
glm::mat3 const rotation{matrix};
out.position = glm::vec3{matrix * glm::vec4{rest.position, 1.f}};
out.normal = direction(rotation, rest.normal);
out.normal = direction(normal_matrix(rotation), rest.normal);
out.tangent = glm::vec4{direction(rotation, glm::vec3{rest.tangent}), rest.tangent.w};
}
}
+6 -2
View File
@@ -56,8 +56,12 @@ template <typename Weight> vertex_weights unpack(std::array<std::uint16_t, 4> co
// Mesh and Joint posed from the root of the model, so the placement of the instance cancels out
std::vector<glm::mat4> palette(glm::mat4 const &Mesh, std::span<glm::mat4 const> Joints, std::span<glm::mat4 const> InverseBind);
// true when every matrix of the palette leaves the vertices where they are (within Tolerance): the rest pose
bool rest_pose(std::span<glm::mat4 const> 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<glm::mat4 const> 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<glm::mat4 const> 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
+3 -2
View File
@@ -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 ) ) {
+3 -2
View File
@@ -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;
+24
View File
@@ -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 {
+4
View File
@@ -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
+9
View File
@@ -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
+20 -2
View File
@@ -12,6 +12,7 @@ http://mozilla.org/MPL/2.0/.
#include "rendering/vulkan14/vulkan14texture.h"
#include <atomic>
#include <format>
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<std::uint64_t> g_generation{0};
// set when a variant of a program for the compact layout didn't compile (compact_layout_failed())
std::atomic<bool> 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<std::mutex> 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();
}
+7
View File
@@ -22,6 +22,7 @@ http://mozilla.org/MPL/2.0/.
#include <chrono>
#include <memory>
#include <mutex>
#include <string_view>
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:
+5
View File
@@ -320,6 +320,11 @@ std::shared_ptr<vk14::scene_program> 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;
}
+1
View File
@@ -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<skinning::skinned_geometry const> const &Source, std::span<glm::mat4 const> 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
+5
View File
@@ -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);
+5 -1
View File
@@ -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()
+5 -1
View File
@@ -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()
+6 -1
View File
@@ -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;
+5
View File
@@ -181,6 +181,11 @@ TEST_CASE("clip: a joint turns or moves its node from the rest, within its limit
slider.type = animation::joint_type::slider;
slider.axis = {0.f, 1.f, 0.f};
CHECK(animation::joint_transform(slider, 0.25f)[3].y == doctest::Approx(0.25f));
// a part modelled half out (JOIN.rest 0.1): there at the value 0.1, at its modelled place at 0
slider.rest_value = 0.1f;
CHECK(animation::joint_transform(slider, 0.1f)[3].y == doctest::Approx(0.f));
CHECK(animation::joint_transform(slider, 0.f)[3].y == doctest::Approx(-0.1f));
slider.rest_value = 0.f;
// a screw of 0.012 m a turn moves 0.006 m in half a turn
animation::joint_binding screw;
+21
View File
@@ -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<glm::mat4> 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<glm::mat4> const distantjoints{distant, distant * tipbind};
CHECK(skinning::rest_pose(skinning::palette(distant, distantjoints, inversebind), 1e-5f, skinning::palette_scale(distant, distantjoints)));
std::vector<glm::mat4> 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<glm::mat4> 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<gfx::basic_vertex> const rest{vertex};
std::vector<skinning::vertex_weights> const weights{{{0, 0, 0, 0}, {1.f, 0.f, 0.f, 0.f}}};
std::vector<gfx::basic_vertex> 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<glm::mat4> 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") {
+11 -4
View File
@@ -1662,16 +1662,23 @@ 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" ) {
// a channel of the model (EU07M): its name and value
Input.getTokens();
// a channel of the model (EU07M): its name, as written (the parser would lowercase it), and value
Input.getTokens( 1, false );
Input >> m_animationsubmodel;
m_animationtype = channel_animation;
Input.getTokens( 1 );