mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-02 07:22:42 +01:00
feat(render): models with lights drawn instanced, behind gfx.instancedlights in eu07.ini (#538)
* 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. * fix(render): EU07M review of #538: an instance with light colours of its own drawn alone, the light records locked only when stale - An instance with lightcolors of its own isn't instanced: the colours go onto the shared submodels as the instance is prepared, which the instances of a bucket don't each get, so they all showed the colours of one of them. - The walk takes the exclusive lock for the light records of a bucket only when they are stale in this frame (Refresh_Light_Records() checks again under it): the later passes of the frame no longer stop the other walkers. - instance_cull.comp (both renderers) puts the word of the light parts into [0][3] after the product with the view, so a view with a translation can't stretch the first column of a lit instance. - The spec: ls_Dark / ls_Home switch without the fade too, and the instances with light colours of their own.
This commit is contained in:
@@ -31,6 +31,7 @@ http://mozilla.org/MPL/2.0/.
|
||||
#include "model/Model3d.h"
|
||||
#include "rendering/common/skyocclusion.h"
|
||||
#include <algorithm>
|
||||
#include <bit>
|
||||
#include <chrono>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
@@ -1032,6 +1033,12 @@ void scene_walk::Queue_Instances( scene::basic_cell const *Cell )
|
||||
Update_Instance_Records( bucket.first.pModel, instancebucket, Cell->m_area.center );
|
||||
}
|
||||
}
|
||||
if( m_instancecullpass && bucket_has_lights( instancebucket ) ) {
|
||||
// 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 ) ) {
|
||||
walk_bucket_culled( instancebucket );
|
||||
continue;
|
||||
@@ -1062,6 +1069,31 @@ void scene_walk::Queue_Instances( scene::basic_cell const *Cell )
|
||||
}
|
||||
|
||||
// makes the records of the instances held by a bucket of a scene cell, with locations relative to Origin
|
||||
bool scene_walk::bucket_has_lights( scene::basic_cell::instance_bucket const &Bucket )
|
||||
{
|
||||
auto const found { std::ranges::find_if( Bucket.instances, []( TAnimModel const *Instance ) { return Instance != nullptr; } ) };
|
||||
return ( found != Bucket.instances.end() ) && ( *found )->has_lights();
|
||||
}
|
||||
|
||||
void scene_walk::Refresh_Light_Records( scene::basic_cell::instance_bucket const &Bucket )
|
||||
{
|
||||
// once a frame, by the first pass which gets to the bucket
|
||||
if( ( Bucket.records.size() != Bucket.instances.size() ) || ( Bucket.lightsframe == m_framestamp ) ) { return; }
|
||||
Bucket.lightsframe = m_framestamp;
|
||||
for( std::size_t idx = 0; idx < Bucket.instances.size(); ++idx ) {
|
||||
auto *instance { Bucket.instances[ idx ] };
|
||||
if( instance == nullptr ) { continue; }
|
||||
// the timers of blinking lights go by the frames (once a frame, whichever pass gets here first)
|
||||
instance->RaAnimate( m_framestamp );
|
||||
auto const bits { instance->instance_light_bits() };
|
||||
auto &word { Bucket.records[ idx ].rotationscale[ 0 ].w };
|
||||
if( std::bit_cast<std::uint32_t>( word ) != bits ) {
|
||||
word = std::bit_cast<float>( bits );
|
||||
Bucket.gpustale = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void scene_walk::Update_Instance_Records( TModel3d const *Model, scene::basic_cell::instance_bucket const &Bucket, glm::dvec3 const &Origin )
|
||||
{
|
||||
// a submodel is drawn only closer than its fSquareMaxDist, see Render( TSubModel * ). past the largest of these
|
||||
@@ -1102,6 +1134,9 @@ void scene_walk::Update_Instance_Records( TModel3d const *Model, scene::basic_ce
|
||||
for( int column = 0; column < 3; ++column ) {
|
||||
record.rotationscale[ column ] = rotationscale[ column ];
|
||||
}
|
||||
// the light state of an instance of a model with lights (gfx.instancedlights) in the w left unused, which the culling of the
|
||||
// gpu carries into the element [0][3] of the matrix it makes of the record (Refresh_Light_Records() keeps it current)
|
||||
record.rotationscale[ 0 ].w = std::bit_cast<float>( instance->instance_light_bits() );
|
||||
}
|
||||
// an empty slot keeps its record without flags, and isn't drawn
|
||||
Bucket.records.emplace_back( record );
|
||||
@@ -1282,6 +1317,8 @@ void scene_walk::Render_Instanced( TModel3d *Model, std::vector<queued_instance>
|
||||
TAnimModel *batch_instance { nullptr }; // the first instance drawn; its RaPrepare() and material serve the whole bucket
|
||||
material_data const *batch_material { nullptr };
|
||||
float farthest_distancesquared { 0.f };
|
||||
// a model with numbered lights (gfx.instancedlights): each instance animates its lights, and their state goes with its matrix
|
||||
auto const lit { ( frontinstance != nullptr ) && frontinstance->has_lights() };
|
||||
|
||||
for( auto const &queued : Instances ) {
|
||||
auto const &record { queued.bucket->records[ queued.index ] };
|
||||
@@ -1315,11 +1352,23 @@ void scene_walk::Render_Instanced( TModel3d *Model, std::vector<queued_instance>
|
||||
auto const radiussquared { record.radius * record.radius };
|
||||
if( radiussquared * Global.ZoomFactor / distancesquared < 0.003 * 0.003 ) { continue; }
|
||||
|
||||
auto *instance { queued.bucket->instances[ queued.index ] };
|
||||
if( batch_instance == nullptr ) {
|
||||
batch_instance = queued.bucket->instances[ queued.index ];
|
||||
batch_instance->RaPrepare();
|
||||
batch_instance = instance;
|
||||
if( lit ) {
|
||||
batch_instance->RaPrepareInstanced();
|
||||
light_parts_of( *Model );
|
||||
}
|
||||
else {
|
||||
batch_instance->RaPrepare();
|
||||
}
|
||||
batch_material = batch_instance->Material();
|
||||
}
|
||||
std::uint32_t lightbits { 0 };
|
||||
if( lit ) {
|
||||
instance->RaAnimate( m_framestamp );
|
||||
lightbits = instance->instance_light_bits();
|
||||
}
|
||||
|
||||
// Build the camera-relative root modelview for this instance:
|
||||
// mv = view * translate(instance_pos - camera_pos) * rotate(instance_angles) * scale(m_scale)
|
||||
@@ -1334,13 +1383,21 @@ void scene_walk::Render_Instanced( TModel3d *Model, std::vector<queued_instance>
|
||||
|
||||
auto const distance { static_cast<float>( distancesquared ) };
|
||||
farthest_distancesquared = std::max( farthest_distancesquared, distance );
|
||||
auto modelview { view_matrix * root };
|
||||
if( lit ) {
|
||||
// the light state in the element [0][3], which is 0 in an affine matrix (effective_modelview() of the shaders)
|
||||
modelview[ 0 ][ 3 ] = std::bit_cast<float>( lightbits );
|
||||
}
|
||||
m_instance_survivors.push_back( {
|
||||
view_matrix * root,
|
||||
modelview,
|
||||
distance,
|
||||
static_cast<std::uint32_t>( std::upper_bound( m_instance_lodbounds.begin(), m_instance_lodbounds.end(), distance ) - m_instance_lodbounds.begin() ) } );
|
||||
}
|
||||
|
||||
if( m_instance_survivors.empty() ) { return; }
|
||||
if( m_instance_survivors.empty() ) {
|
||||
m_lightparts.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
// group the modelviews by lod band into a contiguous array, for the upload. every distance in
|
||||
// a band selects the same lod; the nearest one represents the band.
|
||||
@@ -1410,6 +1467,7 @@ void scene_walk::Render_Instanced( TModel3d *Model, std::vector<queued_instance>
|
||||
|
||||
++m_renderpass.draw_stats.instanced_drawcalls;
|
||||
}
|
||||
end_light_parts();
|
||||
|
||||
m_renderpass.draw_stats.instances += static_cast<int>( m_instance_survivors.size() );
|
||||
m_renderpass.draw_stats.models += static_cast<int>( m_instance_survivors.size() );
|
||||
@@ -2138,6 +2196,47 @@ bool scene_walk::Render(TModel3d *Model, material_data const *Material, float co
|
||||
return result;
|
||||
}
|
||||
|
||||
void scene_walk::light_parts_of(TModel3d &Model)
|
||||
{
|
||||
m_lightparts.clear();
|
||||
auto const &parts{Model.instance_parts()};
|
||||
for (std::size_t slot = 0; slot < parts.lights_on.size(); ++slot)
|
||||
{
|
||||
if (parts.lights_on[slot] != nullptr)
|
||||
{
|
||||
m_lightparts.emplace_back(parts.lights_on[slot], static_cast<float>(1 + slot));
|
||||
}
|
||||
if (parts.lights_off[slot] != nullptr)
|
||||
{
|
||||
m_lightparts.emplace_back(parts.lights_off[slot], static_cast<float>(9 + slot));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void scene_walk::end_light_parts()
|
||||
{
|
||||
if (m_lightparts.empty())
|
||||
{
|
||||
return;
|
||||
}
|
||||
m_lightparts.clear();
|
||||
m_lightpart = 0.f;
|
||||
model_ubs.light_part = 0.f;
|
||||
}
|
||||
|
||||
void scene_walk::prepare_group_instance(scene_walk &Walk, TAnimModel *Instance, bool const Instanced, TModel3d &Model)
|
||||
{
|
||||
if (Instanced && Instance->has_lights())
|
||||
{
|
||||
// instances of a model with lights (gfx.instancedlights): the parts of the lights are all walked, the state of each instance
|
||||
// comes with its matrix from the culling (rotationscale[0].w of its record, Refresh_Light_Records())
|
||||
Instance->RaPrepareInstanced();
|
||||
Walk.light_parts_of(Model);
|
||||
return;
|
||||
}
|
||||
Instance->RaPrepare();
|
||||
}
|
||||
|
||||
// opaque characters of a text display, laid out in a row the same way as the translucent ones
|
||||
void scene_walk::Render_text(TSubModel *Submodel)
|
||||
{
|
||||
@@ -2173,6 +2272,17 @@ void scene_walk::Render(TSubModel *Submodel)
|
||||
{
|
||||
glDebug("Render TSubModel");
|
||||
|
||||
// the instanced walk of a model with lights: the part of a light the subtree is of, for the shaders (gl::model_ubs::light_part)
|
||||
auto const outerlightpart{m_lightpart};
|
||||
if (false == m_lightparts.empty())
|
||||
{
|
||||
if (auto const found{std::ranges::find(m_lightparts, Submodel, &std::pair<TSubModel const *, float>::first)}; found != m_lightparts.end())
|
||||
{
|
||||
m_lightpart = found->second;
|
||||
}
|
||||
model_ubs.light_part = m_lightpart;
|
||||
}
|
||||
|
||||
if ((Submodel->iVisible) && (TSubModel::fSquareDist >= Submodel->fSquareMinDist) && (TSubModel::fSquareDist < Submodel->fSquareMaxDist) && (false == m_previewing || Submodel->fVisible > 0.f))
|
||||
{
|
||||
glm::mat4 future_stack = model_ubs.future;
|
||||
@@ -2341,6 +2451,11 @@ void scene_walk::Render(TSubModel *Submodel)
|
||||
if( Submodel->b_Anim < at_SecondsJump )
|
||||
Submodel->b_Anim = at_None; // wyłączenie animacji dla kolejnego użycia subm
|
||||
*/
|
||||
if (false == m_lightparts.empty())
|
||||
{
|
||||
m_lightpart = outerlightpart;
|
||||
model_ubs.light_part = m_lightpart;
|
||||
}
|
||||
if (Submodel->Next)
|
||||
if (Submodel->iAlpha & Submodel->iFlags & 0x1F000000)
|
||||
Render(Submodel->Next); // dalsze rekurencyjnie
|
||||
|
||||
Reference in New Issue
Block a user