/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "stdafx.h" #include "rendering/common/memoryreport.h" #include "audio/audiorenderer.h" #include "model/AnimModel.h" #include "model/MdlMngr.h" #include "model/Texture.h" #include "world/Track.h" #include "rendering/geometrybank.h" #include "rendering/geometrystreamer.h" #include "scene/scene.h" #include "simulation/simulation.h" #include "utilities/memorystats.h" #include namespace gfx { namespace { double megabytes( std::uint64_t const Bytes ) { return static_cast( Bytes ) / ( 1024.0 * 1024.0 ); } // the world space vertices the shapes and the lines of the scenery keep, the transforms of the sleepers, the model instances struct scenery_memory { memory_count shapes; memory_count lines; memory_count sleepers; std::uint64_t instances { 0 }; }; void count_cell( scene::basic_cell const &Cell, scenery_memory &Memory ) { for( auto const *list : { &Cell.m_shapesopaque, &Cell.m_shapestranslucent } ) { for( auto const &shape : *list ) { ++Memory.shapes.count; Memory.shapes.bytes += memory_of( shape.data().vertices ) + memory_of( shape.data().userdata ); } } for( auto const &line : Cell.m_lines ) { ++Memory.lines.count; Memory.lines.bytes += memory_of( line.data().vertices ); } for( auto const *track : Cell.m_paths ) { if( track->m_sleeper_local_transforms.empty() ) { continue; } ++Memory.sleepers.count; Memory.sleepers.bytes += memory_of( track->m_sleeper_local_transforms ); } Memory.instances += Cell.m_instancesopaque.size() + Cell.m_instancetranslucent.size(); } scenery_memory count_scenery() { scenery_memory memory; if( simulation::Region == nullptr ) { return memory; } for( auto const *section : simulation::Region->m_sections ) { if( section == nullptr ) { continue; } for( auto const &shape : section->m_shapes ) { ++memory.shapes.count; memory.shapes.bytes += memory_of( shape.data().vertices ) + memory_of( shape.data().userdata ); } for( auto const &cell : section->m_cells ) { count_cell( cell, memory ); } } return memory; } void report_heap( std::uint64_t const Committed, std::vector &Lines ) { if( false == heap_counting() ) { Lines.emplace_back( "heap: not counted (debug.memorystats yes)" ); return; } auto const heap { query_heap_memory() }; Lines.emplace_back( std::format( "heap: {:.1f} MB taken with new in {} blocks, the rest of the private memory {:.1f} MB (drivers, libraries)", megabytes( heap.bytes ), heap.blocks, megabytes( Committed > heap.bytes ? Committed - heap.bytes : 0 ) ) ); auto const large { query_large_blocks() }; std::size_t constexpr shown { 20 }; for( std::size_t index = 0; ( index < large.sites.size() ) && ( index < shown ); ++index ) { auto const &site { large.sites[ index ] }; Lines.emplace_back( std::format( " large blocks {:.1f} MB in {}: {}", megabytes( site.bytes ), site.blocks, site.place ) ); } } } // namespace std::vector memory_report( texture_bank const *Textures, geometrybank_manager const &Geometry ) { std::vector lines; auto const process { query_process_memory() }; if( process.valid ) { lines.emplace_back( std::format( "process: {:.1f} MB private, {:.1f} MB resident (peak {:.1f} MB)", megabytes( process.committed ), megabytes( process.resident ), megabytes( process.peak ) ) ); } std::uint64_t accounted { 0 }; if( Textures != nullptr ) { auto const textures { Textures->memory() }; accounted += textures.cpu.bytes + textures.readahead.bytes; lines.emplace_back( std::format( "textures: {} keep their data in ram, {:.1f} MB; {} on the card, ~{:.1f} MB; read ahead {} {:.1f} MB, {} being read, {} not read till needed", textures.cpu.count, megabytes( textures.cpu.bytes ), textures.gpu.count, megabytes( textures.gpu.bytes ), textures.readahead.count, megabytes( textures.readahead.bytes ), textures.reading, textures.waiting ) ); } auto const geometry { Geometry.memory() }; accounted += geometry.vertices.bytes + geometry.indices.bytes + geometry.userdata.bytes; lines.emplace_back( std::format( "geometry banks: {}, {} chunks: vertices {:.1f} MB, indices {:.1f} MB, user data {:.1f} MB", geometry.banks, geometry.vertices.count, megabytes( geometry.vertices.bytes ), megabytes( geometry.indices.bytes ), megabytes( geometry.userdata.bytes ) ) ); auto const streamed { geometry_streamer::instance().stats() }; lines.emplace_back( std::format( " of them streamed: {} banks with their data, {:.1f} MB", streamed.resident, megabytes( streamed.bytes ) ) ); auto const models { TModelsManager::memory() }; accounted += models.submodels.bytes; lines.emplace_back( std::format( "models: {}, {} submodels, their own geometry {:.1f} MB", models.models, models.submodels.count, megabytes( models.submodels.bytes ) ) ); auto const scenery { count_scenery() }; accounted += scenery.shapes.bytes + scenery.lines.bytes + scenery.sleepers.bytes + scenery.instances * sizeof( TAnimModel ); lines.emplace_back( std::format( "scenery: {} shapes {:.1f} MB, {} lines {:.1f} MB, sleepers of {} tracks {:.1f} MB, {} model instances {:.1f} MB", scenery.shapes.count, megabytes( scenery.shapes.bytes ), scenery.lines.count, megabytes( scenery.lines.bytes ), scenery.sleepers.count, megabytes( scenery.sleepers.bytes ), scenery.instances, megabytes( scenery.instances * sizeof( TAnimModel ) ) ) ); auto const sounds { audio::renderer.buffers_memory() }; accounted += sounds.bytes; lines.emplace_back( std::format( "sounds: {} buffers {:.1f} MB", sounds.count, megabytes( sounds.bytes ) ) ); if( process.valid ) { lines.emplace_back( std::format( "counted above: {:.1f} MB of {:.1f} MB private, the rest {:.1f} MB", megabytes( accounted ), megabytes( process.committed ), megabytes( process.committed > accounted ? process.committed - accounted : 0 ) ) ); } report_heap( process.committed, lines ); return lines; } } // namespace gfx