diff --git a/rendering/common/locallights.cpp b/rendering/common/locallights.cpp index af432b7c9..30caf63c3 100644 --- a/rendering/common/locallights.cpp +++ b/rendering/common/locallights.cpp @@ -224,33 +224,7 @@ void gfx::local_lights::gather(glm::dvec3 const &Camera, scene::basic_region &Re if ((darkness > 0.f) && (range > 0.f) && Global.gfx_lights_scenery) { - auto const freespotbrightness{Global.gfx_lights_freespot_intensity * darkness}; - // sections of the region within the range, the same way the render picks them - auto const camerax{static_cast(std::floor(Camera.x / scene::EU07_SECTIONSIZE + scene::EU07_REGIONSIDESECTIONCOUNT / 2))}; - auto const cameraz{static_cast(std::floor(Camera.z / scene::EU07_SECTIONSIZE + scene::EU07_REGIONSIDESECTIONCOUNT / 2))}; - auto const reach{static_cast(std::ceil(range / scene::EU07_SECTIONSIZE))}; - for (int row = scene::region_index_low(cameraz - reach); row <= scene::region_index_high(cameraz + reach); ++row) - { - for (int column = scene::region_index_low(camerax - reach); column <= scene::region_index_high(camerax + reach); ++column) - { - auto *section{Region.find_section(row, column)}; - if (section == nullptr) - { - continue; - } - for (auto &cell : section->m_cells) - { - if ((false == cell.m_active) || (glm::length(cell.m_area.center - Camera) > range + cell.m_area.radius)) - { - continue; - } - for (auto *instance : cell_instances(cell, rebuild).instances) - { - gather_instance(instance, Camera, freespotbrightness, Framestamp); - } - } - } - } + gather_scenery(Camera, Region, range, Global.gfx_lights_freespot_intensity * darkness, rebuild, Framestamp); } if ((darkness > 0.f) && (range > 0.f) && Global.gfx_lights_vehicles) @@ -280,6 +254,40 @@ void gfx::local_lights::gather(glm::dvec3 const &Camera, scene::basic_region &Re m_gathertime = (m_gathertime == 0.0 ? elapsed : m_gathertime * 0.95 + elapsed * 0.05); } +// the lamps of the scenery within the range: the sections of the region there, the same way the render picks them +void gfx::local_lights::gather_scenery(glm::dvec3 const &Camera, scene::basic_region &Region, float const Range, float const Brightness, bool const Rebuild, unsigned int const Framestamp) +{ + auto const camerax{static_cast(std::floor(Camera.x / scene::EU07_SECTIONSIZE + scene::EU07_REGIONSIDESECTIONCOUNT / 2))}; + auto const cameraz{static_cast(std::floor(Camera.z / scene::EU07_SECTIONSIZE + scene::EU07_REGIONSIDESECTIONCOUNT / 2))}; + auto const reach{static_cast(std::ceil(Range / scene::EU07_SECTIONSIZE))}; + for (int row = scene::region_index_low(cameraz - reach); row <= scene::region_index_high(cameraz + reach); ++row) + { + for (int column = scene::region_index_low(camerax - reach); column <= scene::region_index_high(camerax + reach); ++column) + { + if (auto *section{Region.find_section(row, column)}; section != nullptr) + { + gather_section(*section, Camera, Range, Brightness, Rebuild, Framestamp); + } + } + } +} + +// the lamps of the cells of the section within the range +void gfx::local_lights::gather_section(scene::basic_section &Section, glm::dvec3 const &Camera, float const Range, float const Brightness, bool const Rebuild, unsigned int const Framestamp) +{ + for (auto &cell : Section.m_cells) + { + if ((false == cell.m_active) || (glm::length(cell.m_area.center - Camera) > Range + cell.m_area.radius)) + { + continue; + } + for (auto *instance : cell_instances(cell, Rebuild).instances) + { + gather_instance(instance, Camera, Brightness, Framestamp); + } + } +} + // the flashlight of the player (Global.flashlight): a cone of neutral white light where the camera looks, from the right hand, by // day as well. it casts shadows as the lamps of the scenery do (a cube map, made anew as it moves), so it doesn't // light the snow or the ground through the walls of the vehicles and the houses; Global.flashlight names it to the shadow maps diff --git a/rendering/common/locallights.h b/rendering/common/locallights.h index 6a7cb27d1..469e0aa60 100644 --- a/rendering/common/locallights.h +++ b/rendering/common/locallights.h @@ -19,6 +19,7 @@ namespace scene { class basic_region; class basic_cell; +class basic_section; class basic_node; } // namespace scene namespace gfx @@ -143,6 +144,8 @@ class local_lights void add_light_point(TSubModel const &Submodel, glm::mat4 const &Transform, glm::dmat4 const &Placement, float Brightness); // lights of the glowing points of a smt_Stars submodel of a scenery lamp (gfx_lights_stars), each one shining downwards void add_lamp_points(TSubModel const &Submodel, glm::mat4 const &Transform, glm::dmat4 const &Placement, float Brightness); + void gather_scenery(glm::dvec3 const &Camera, scene::basic_region &Region, float const Range, float const Brightness, bool const Rebuild, unsigned int const Framestamp); + void gather_section(scene::basic_section &Section, glm::dvec3 const &Camera, float const Range, float const Brightness, bool const Rebuild, unsigned int const Framestamp); void gather_instance(TAnimModel *Instance, glm::dvec3 const &Camera, float const Brightness, unsigned int const Framestamp); void gather_vehicle(TDynamicObject *Vehicle, glm::dvec3 const &Camera, gfx::geometrybank_manager const &Geometry, float const Freespotbrightness, float const Compartmentbrightness); void gather_cab_lights(gfx::geometrybank_manager const &Geometry); diff --git a/rendering/opengl46/opengl46renderer.cpp b/rendering/opengl46/opengl46renderer.cpp index 0f2301d85..ffd8ed1f8 100644 --- a/rendering/opengl46/opengl46renderer.cpp +++ b/rendering/opengl46/opengl46renderer.cpp @@ -4970,35 +4970,6 @@ void opengl46_renderer::Update_Texture_Prefetch() auto const camera{m_colorpass.pass_camera.position()}; auto const range{static_cast(m_colorpass.draw_range) * Global.fDistanceFactor + margin}; - auto const prefetch_material = [this, &camera](material_handle const Material) { - if (Material <= 0) - { - return; - } - auto const &material{m_materials.material(Material)}; - for (auto const texture : material.textures) - { - m_textures.prefetch(texture, camera); - } - for (auto const &variant : material.texture_variants) - { - for (auto const texture : variant) - { - m_textures.prefetch(texture, camera); - } - } - }; - auto const prefetch_model = [this, &camera](TModel3d *Model) { - if (Model == nullptr) - { - return; - } - for (auto const texture : Model_Textures(Model)) - { - m_textures.prefetch(texture, camera); - } - }; - // the sections around the camera, the same way Render(scene::basic_region *) picks them int constexpr cellspersection{(scene::EU07_SECTIONSIZE / scene::EU07_CELLSIZE) * (scene::EU07_SECTIONSIZE / scene::EU07_CELLSIZE)}; auto const camerax{static_cast(std::floor(camera.x / scene::EU07_SECTIONSIZE + scene::EU07_REGIONSIDESECTIONCOUNT / 2))}; @@ -5016,101 +4987,23 @@ void opengl46_renderer::Update_Texture_Prefetch() { auto const index{m_prefetchcursor % cellcount}; auto const sectionindex{static_cast(index / cellspersection)}; - auto const cellindex{index % cellspersection}; auto const row{originz + sectionindex / side}; auto const column{originx + sectionindex % side}; if (false == scene::section_in_region(row, column)) { continue; } - auto *section{region->find_section(row, column)}; - if (section == nullptr) + if (auto *section{region->find_section(row, column)}; section != nullptr) { - continue; + Prefetch_Cell(*section, index % cellspersection, camera, range, margin); } - if ((cellindex == 0) && (glm::length(section->m_area.center - camera) - section->m_area.radius <= range)) - { - for (auto const &shape : section->m_shapes) - { - prefetch_material(shape.data().material); - } - } - auto const &cell{section->m_cells[cellindex]}; - if ((false == cell.m_active) || (glm::length(cell.m_area.center - camera) - cell.m_area.radius > range)) - { - continue; - } - for (auto const &shape : cell.m_shapesopaque) - { - prefetch_material(shape.data().material); - } - for (auto const &shape : cell.m_shapestranslucent) - { - prefetch_material(shape.data().material); - } - for (auto const *track : cell.m_paths) - { - prefetch_material(track->m_material1); - prefetch_material(track->m_material2); - prefetch_material(track->m_sleeper_skin); - if (track->SwitchExtension != nullptr) - { - prefetch_material(track->SwitchExtension->m_material3); - } - } - // the instances which will be seen from within the margin. the ones asked for once aren't gone through again until they're - // out of reach, it's when their data could have been dropped - auto const prefetch_instances = [&](scene::basic_cell::instance_sequence const &Instances) { - for (auto *instance : Instances) - { - auto const distance{glm::length(instance->location() - camera)}; - auto const reach{std::sqrt(instance->m_rangesquaredmax * Global.fDistanceFactor) + margin}; - if ((distance > reach) || (false == instance->visible())) - { - if (distance > reach + margin) - { - instance->m_texturesprefetched = false; - } - continue; - } - if (true == instance->m_texturesprefetched) - { - continue; - } - instance->m_texturesprefetched = true; - prefetch_model(instance->Model()); - for (auto const skin : instance->Material()->replacable_skins) - { - prefetch_material(skin); - } - } - }; - prefetch_instances(cell.m_instancesopaque); - prefetch_instances(cell.m_instancetranslucent); } auto const now{std::chrono::steady_clock::now()}; if (now - m_prefetchvehicletime >= std::chrono::seconds(1)) { m_prefetchvehicletime = now; - for (auto *vehicle : simulation::Vehicles.sequence()) - { - if ((vehicle == nullptr) || (glm::length(vehicle->vPosition - camera) - vehicle->radius() > range)) - { - continue; - } - prefetch_model(vehicle->mdModel); - prefetch_model(vehicle->mdLowPolyInt); - prefetch_model(vehicle->mdLoad); - for (auto *attachment : vehicle->mdAttachments) - { - prefetch_model(attachment); - } - for (auto const skin : vehicle->m_materialdata.replacable_skins) - { - prefetch_material(skin); - } - } + Prefetch_Vehicles(camera, range); } // the data read for what wasn't drawn after all goes: when the camera went away from where it was read for (the instances which // wanted it are out of reach then), or after a while (the details of models seen only from close, a turn not taken) @@ -5121,6 +5014,129 @@ void opengl46_renderer::Update_Texture_Prefetch() } } +// reads ahead the textures of the material, its variants included +void opengl46_renderer::Prefetch_Material(material_handle const Material, glm::dvec3 const &Camera) +{ + if (Material <= 0) + { + return; + } + auto const &material{m_materials.material(Material)}; + for (auto const texture : material.textures) + { + m_textures.prefetch(texture, Camera); + } + for (auto const &variant : material.texture_variants) + { + for (auto const texture : variant) + { + m_textures.prefetch(texture, Camera); + } + } +} + +// reads ahead the textures of the model +void opengl46_renderer::Prefetch_Model(TModel3d *Model, glm::dvec3 const &Camera) +{ + if (Model == nullptr) + { + return; + } + for (auto const texture : Model_Textures(Model)) + { + m_textures.prefetch(texture, Camera); + } +} + +// reads ahead the textures of what the cell of the section holds, and of the shapes of the section along with its first cell +void opengl46_renderer::Prefetch_Cell(scene::basic_section &Section, std::size_t const Cellindex, glm::dvec3 const &Camera, double const Range, double const Margin) +{ + if ((Cellindex == 0) && (glm::length(Section.m_area.center - Camera) - Section.m_area.radius <= Range)) + { + for (auto const &shape : Section.m_shapes) + { + Prefetch_Material(shape.data().material, Camera); + } + } + auto const &cell{Section.m_cells[Cellindex]}; + if ((false == cell.m_active) || (glm::length(cell.m_area.center - Camera) - cell.m_area.radius > Range)) + { + return; + } + for (auto const &shape : cell.m_shapesopaque) + { + Prefetch_Material(shape.data().material, Camera); + } + for (auto const &shape : cell.m_shapestranslucent) + { + Prefetch_Material(shape.data().material, Camera); + } + for (auto const *track : cell.m_paths) + { + Prefetch_Material(track->m_material1, Camera); + Prefetch_Material(track->m_material2, Camera); + Prefetch_Material(track->m_sleeper_skin, Camera); + if (track->SwitchExtension != nullptr) + { + Prefetch_Material(track->SwitchExtension->m_material3, Camera); + } + } + Prefetch_Instances(cell.m_instancesopaque, Camera, Margin); + Prefetch_Instances(cell.m_instancetranslucent, Camera, Margin); +} + +// the instances which will be seen from within the margin. the ones asked for once aren't gone through again until they're out of +// reach, it's when their data could have been dropped +void opengl46_renderer::Prefetch_Instances(scene::basic_cell::instance_sequence const &Instances, glm::dvec3 const &Camera, double const Margin) +{ + for (auto *instance : Instances) + { + auto const distance{glm::length(instance->location() - Camera)}; + auto const reach{std::sqrt(instance->m_rangesquaredmax * Global.fDistanceFactor) + Margin}; + if ((distance > reach) || (false == instance->visible())) + { + if (distance > reach + Margin) + { + instance->m_texturesprefetched = false; + } + continue; + } + if (true == instance->m_texturesprefetched) + { + continue; + } + instance->m_texturesprefetched = true; + Prefetch_Model(instance->Model(), Camera); + for (auto const skin : instance->Material()->replacable_skins) + { + Prefetch_Material(skin, Camera); + } + } +} + +// reads ahead the textures of the vehicles within the range +void opengl46_renderer::Prefetch_Vehicles(glm::dvec3 const &Camera, double const Range) +{ + for (auto *vehicle : simulation::Vehicles.sequence()) + { + if ((vehicle == nullptr) || (glm::length(vehicle->vPosition - Camera) - vehicle->radius() > Range)) + { + continue; + } + Prefetch_Model(vehicle->mdModel, Camera); + Prefetch_Model(vehicle->mdLowPolyInt, Camera); + Prefetch_Model(vehicle->mdLoad, Camera); + for (auto *attachment : vehicle->mdAttachments) + { + Prefetch_Model(attachment, Camera); + } + for (auto const skin : vehicle->m_materialdata.replacable_skins) + { + Prefetch_Material(skin, Camera); + } + } +} + // textures used by materials of the sub-models of specified model, replaceable skins excluded std::vector const &opengl46_renderer::Model_Textures(TModel3d *Model) { diff --git a/rendering/opengl46/opengl46renderer.h b/rendering/opengl46/opengl46renderer.h index 2f50985d1..8be8d5d82 100644 --- a/rendering/opengl46/opengl46renderer.h +++ b/rendering/opengl46/opengl46renderer.h @@ -477,6 +477,12 @@ class opengl46_renderer : public gfx_renderer, public gfx::scene_walk { std::vector const &Model_Textures( TModel3d *Model ); // has the data of the textures read ahead of the camera, with gfx.textures.streaming void Update_Texture_Prefetch(); + // the parts of the read ahead of textures + void Prefetch_Material( material_handle const Material, glm::dvec3 const &Camera ); + void Prefetch_Model( TModel3d *Model, glm::dvec3 const &Camera ); + void Prefetch_Cell( scene::basic_section &Section, std::size_t const Cellindex, glm::dvec3 const &Camera, double const Range, double const Margin ); + void Prefetch_Instances( scene::basic_cell::instance_sequence const &Instances, glm::dvec3 const &Camera, double const Margin ); + void Prefetch_Vehicles( glm::dvec3 const &Camera, double const Range ); void Render_particles(); void Render_precipitation(); // the view of the colour pass being drawn, for the particles of the gpu