18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 04:47:06 +01:00

feat(render): models with lights drawn instanced, behind gfx.instancedlights in eu07.ini

Stage 5 of the EU07M model format. A model with lights (lights, Light_On## / Light_Off##), such as a signal head, was
drawn instance by instance, a draw call for each part of each one. With gfx.instancedlights 1 the instances of such a
model go into the instanced batches like the others, in both renderers, with the culling of the GPU and without it. The
default 0 draws them as before. It applies to EU07M and E3D models alike.

- The light state of an instance is a word in the element [0][3] of its instance matrix, which an affine matrix leaves
  unused: bits 0-7 the lit parts shown, bits 8-15 the dark parts, in the mantissa of a float of exponent 128
  (0x40000000), a normal number the GPU carries through its matrix products whole. Without that marker the shader
  draws everything, as before.
- model_ubs.light_part says which part of which slot a draw is (0 none, 1 + slot lit, 9 + slot dark); a part the
  instance doesn't show gets a zero matrix in the vertex shader (instance_hides_part() and effective_modelview() in
  shaders/vk14/common.glsl and shaders/ogl46/common.glsl). The batch instance shows every light part
  (TAnimModel::RaPrepareInstanced()).
- CPU path: each instance drawn is animated (RaAnimate) and its word put in its matrix (Render_Instanced). GPU culling:
  the word is in rotationscale[0].w of the instance record, refreshed once a frame for buckets with lights
  (Refresh_Light_Records), which marks the records to send again when a word changes.
- 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, where the draw instance by instance fades the lit part in over the dark one. Steady states look
  the same as before.
- The freespot lights (TP_FREESPOTLIGHT with flag 0x4000 and no animation) don't keep a model out of the instancing
  when gfx.instancedlights is on. Light parts aren't put in the ray tracing of an instance batch.

Measured on Vulkan, 200 heads s4ipcpb_dd: with gfx.gpucull 0 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. Steady light states pixel for pixel the same as drawn
instance by instance on all three paths in Vulkan, and on GL 4.6 where the editor panels don't cover them; blinking
checked in Vulkan on all three paths (with time let run in the editor for the test only). Not run in the driver mode,
blinking not checked on GL 4.6, not built with GCC here. Unit tests 1189/1189.
This commit is contained in:
maj00r
2026-11-28 23:10:53 +01:00
parent 92cc2dcc28
commit 8a82433a11
14 changed files with 309 additions and 11 deletions
+72 -2
View File
@@ -525,7 +525,10 @@ bool submodel_tree_blocks_instancing( TSubModel const *Sub ) {
// a text display shows the text of its instance, a tree shared by all instances can't do that
if( Sub->eType == TP_TEXT ) { return true; }
if( anim_type_unsafe_for_instancing( Sub->b_Anim ) ) { return true; }
if( ( ( Sub->iFlags & 0x4000 ) != 0 ) && ( false == anim_type_shared_by_instances( Sub->b_Anim ) ) ) { return true; }
// a light point gets the flag at load for its glare, not for an animation of its own. it's drawn only in the translucent pass,
// by each instance on its own (Render_Alpha()), so with the instanced lights (gfx.instancedlights) it doesn't keep the model out
auto const lightpoint { ( Sub->eType == TP_FREESPOTLIGHT ) && Global.gfx_instancedlights && ( Sub->b_Anim == TAnimType::at_None ) };
if( ( ( Sub->iFlags & 0x4000 ) != 0 ) && ( false == anim_type_shared_by_instances( Sub->b_Anim ) ) && ( false == lightpoint ) ) { return true; }
if( submodel_tree_blocks_instancing( Sub->Child ) ) { return true; }
if( submodel_tree_blocks_instancing( Sub->Next ) ) { return true; }
return false;
@@ -563,7 +566,9 @@ void TAnimModel::update_instanceable_flag() {
m_cameraindependent = ( pModel != nullptr ) && ( false == submodel_tree_faces_camera( pModel->Root ) );
++s_classified_total;
if( pModel == nullptr ) { ++s_rejected_no_pmodel; return; }
if( iNumLights != 0 ) { ++s_rejected_lights; return; }
// 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; }
if( !m_animlist.empty() ) { ++s_rejected_animlist; return; }
if( submodel_tree_blocks_instancing( pModel->Root ) ) { ++s_rejected_animated_submodel; return; }
m_instanceable = true;
@@ -755,6 +760,71 @@ void TAnimModel::RaPrepare()
}
}
// the state of the lit and the dark part of a numbered light, as RaPrepare() shows them
void TAnimModel::light_parts_shown( int const Light, bool &Lit, bool &Dark ) const {
auto const lightmode { static_cast<int>( std::abs( lsLights[ Light ] ) ) };
switch( lightmode ) {
case ls_On:
case ls_Off:
case ls_Blink: {
// RaPrepare() fades the lit part in over the dark one; an instance shows one part or the other, whole, so the two
// switch over at half the fade. both opaque at once, the order of the draws would pick the one seen
Lit = m_lightopacities[ Light ] >= 0.5f;
Dark = false == Lit;
return;
}
case ls_Dark:
case ls_Home: {
auto const base { static_cast<float>( lightmode ) };
auto state {
Global.fLuminance - std::max( 0.f, Global.Overcast - 1.f ) <= ( lsLights[ Light ] == base ? DefaultDarkThresholdLevel : lsLights[ Light ] - base ) };
if( lightmode == ls_Home ) {
// like ls_dark but off late at night
auto const simulationhour { simulation::Time.data().wHour };
state = state && ( simulationhour < 1 || simulationhour >= 5 );
}
Lit = state;
Dark = false == state;
return;
}
default: {
// the modes past home leave the light dark (RaPrepare())
Lit = false;
Dark = true;
return;
}
}
}
std::uint32_t TAnimModel::instance_light_bits() const {
if( iNumLights == 0 ) { return 0; }
// bits 0-7 the lit parts shown, bits 8-15 the dark parts, in the mantissa of a float of exponent 128 (0x40000000, a value
// between 2 and 4): a normal number, which the culling of the gpu carries through its matrix products whole, where one of
// these bits alone would be a denormal it may flush to zero (shaders, instance_hides_part())
std::uint32_t bits { 0x40000000u };
for( int idx = 0; idx < std::min( iNumLights, iMaxNumLights ); ++idx ) {
bool lit { false };
bool dark { false };
light_parts_shown( idx, lit, dark );
bits |= ( lit ? 1u << idx : 0u ) | ( dark ? 1u << ( 8 + idx ) : 0u );
}
return bits;
}
void TAnimModel::RaPrepareInstanced() {
// as RaPrepare(), with both parts of every light shown and opaque: the shaders hide the ones each instance doesn't show
RaPrepare();
for( int idx = 0; idx < iNumLights; ++idx ) {
for( auto *part : { LightsOn[ idx ], LightsOff[ idx ] } ) {
if( part == nullptr ) { continue; }
part->iVisible = 1;
part->SetVisibilityLevel( 1.f, true, false );
}
}
}
glm::mat4 const &TAnimModel::rotation_scale() {
if( ( false == m_rotationscalevalid ) || ( vAngle != m_rotationscaleangles ) || ( m_scale != m_rotationscalescale ) ) {