fyrox_impl/renderer/
stats.rs

1// Copyright (c) 2019-present Dmitry Stepanov and Fyrox Engine contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in all
11// copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21use fyrox_core::instant;
22use fyrox_graphics::framebuffer::DrawCallStatistics;
23pub use fyrox_graphics::stats::*;
24use std::fmt::{Display, Formatter};
25use std::ops::AddAssign;
26
27/// Lighting statistics.
28#[derive(Debug, Copy, Clone, Default)]
29pub struct LightingStatistics {
30    /// How many point lights were rendered.
31    pub point_lights_rendered: usize,
32    /// How many point light shadow maps were rendered.
33    pub point_shadow_maps_rendered: usize,
34    /// How many cascaded shadow maps were rendered.
35    pub csm_rendered: usize,
36    /// How many spot lights were rendered.
37    pub spot_lights_rendered: usize,
38    /// How many spot light shadow maps were rendered.
39    pub spot_shadow_maps_rendered: usize,
40    /// How many directional lights were rendered.
41    pub directional_lights_rendered: usize,
42}
43
44impl AddAssign for LightingStatistics {
45    fn add_assign(&mut self, rhs: Self) {
46        self.point_lights_rendered += rhs.point_lights_rendered;
47        self.point_shadow_maps_rendered += rhs.point_shadow_maps_rendered;
48        self.spot_lights_rendered += rhs.spot_lights_rendered;
49        self.spot_shadow_maps_rendered += rhs.spot_shadow_maps_rendered;
50        self.directional_lights_rendered += rhs.directional_lights_rendered;
51        self.csm_rendered += rhs.csm_rendered;
52    }
53}
54
55impl Display for LightingStatistics {
56    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
57        write!(
58            f,
59            "Lighting Statistics:\n\
60            \tPoint Lights: {}\n\
61            \tSpot Lights: {}\n\
62            \tDirectional Lights: {}\n\
63            \tPoint Shadow Maps: {}\n\
64            \tSpot Shadow Maps: {}\n\
65            \tSpot Shadow Maps: {}\n",
66            self.point_lights_rendered,
67            self.spot_lights_rendered,
68            self.directional_lights_rendered,
69            self.point_shadow_maps_rendered,
70            self.spot_shadow_maps_rendered,
71            self.csm_rendered
72        )
73    }
74}
75
76/// Renderer statistics for a scene.
77#[derive(Debug, Copy, Clone, Default)]
78pub struct SceneStatistics {
79    /// Shows how many pipeline state changes was made during scene rendering.
80    pub pipeline: PipelineStatistics,
81    /// Shows how many lights and shadow maps were rendered.
82    pub lighting: LightingStatistics,
83    /// Shows how many draw calls was made and how many triangles were rendered.
84    pub geometry: RenderPassStatistics,
85}
86
87impl Display for SceneStatistics {
88    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
89        write!(
90            f,
91            "{}\n\
92            {}\n\
93            {}\n",
94            self.geometry, self.lighting, self.pipeline
95        )
96    }
97}
98
99impl AddAssign<DrawCallStatistics> for SceneStatistics {
100    fn add_assign(&mut self, rhs: DrawCallStatistics) {
101        self.geometry += rhs;
102    }
103}
104
105impl AddAssign<PipelineStatistics> for SceneStatistics {
106    fn add_assign(&mut self, rhs: PipelineStatistics) {
107        self.pipeline += rhs;
108    }
109}
110
111impl AddAssign<RenderPassStatistics> for SceneStatistics {
112    fn add_assign(&mut self, rhs: RenderPassStatistics) {
113        self.geometry += rhs;
114    }
115}
116
117impl AddAssign<LightingStatistics> for SceneStatistics {
118    fn add_assign(&mut self, rhs: LightingStatistics) {
119        self.lighting += rhs;
120    }
121}
122
123/// Renderer statistics for one frame, also includes current frames per second
124/// amount.
125#[derive(Debug, Copy, Clone)]
126pub struct Statistics {
127    /// Shows how many pipeline state changes was made per frame.
128    pub pipeline: PipelineStatistics,
129    /// Shows how many lights and shadow maps were rendered.
130    pub lighting: LightingStatistics,
131    /// Shows how many draw calls was made and how many triangles were rendered.
132    pub geometry: RenderPassStatistics,
133    /// Real time consumed to render frame. Time given in **seconds**.
134    pub pure_frame_time: f32,
135    /// Total time renderer took to process single frame, usually includes
136    /// time renderer spend to wait to buffers swap (can include vsync).
137    /// Time given in **seconds**.
138    pub capped_frame_time: f32,
139    /// Total amount of frames been rendered in one second.
140    pub frames_per_second: usize,
141    /// Total amount of textures in the textures cache.
142    pub texture_cache_size: usize,
143    /// Total amount of vertex+index buffers pairs in the geometry cache.
144    pub geometry_cache_size: usize,
145    /// Total amount of shaders in the shaders cache.
146    pub shader_cache_size: usize,
147    /// Total amount of uniform buffers in the cache.
148    pub uniform_buffer_cache_size: usize,
149    pub(super) frame_counter: usize,
150    pub(super) frame_start_time: instant::Instant,
151    pub(super) last_fps_commit_time: instant::Instant,
152}
153
154impl std::ops::AddAssign<SceneStatistics> for Statistics {
155    fn add_assign(&mut self, rhs: SceneStatistics) {
156        self.pipeline += rhs.pipeline;
157        self.lighting += rhs.lighting;
158        self.geometry += rhs.geometry;
159    }
160}
161
162impl Display for Statistics {
163    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
164        let fps = self.frames_per_second;
165        let pure_frame_time = self.pure_frame_time * 1000.0;
166        let capped_frame_time = self.capped_frame_time * 1000.0;
167        let geometry_stats = &self.geometry;
168        let lighting_stats = &self.lighting;
169        let pipeline_stats = &self.pipeline;
170        let texture_cache_size = self.texture_cache_size;
171        let geometry_cache_size = self.geometry_cache_size;
172        let shader_cache_size = self.shader_cache_size;
173        let uniform_buffer_cache_size = self.uniform_buffer_cache_size;
174        write!(
175            f,
176            "FPS: {fps}\n\
177            Pure Frame Time: {pure_frame_time:.2} ms\n\
178            Capped Frame Time: {capped_frame_time:.2} ms\n\
179            {geometry_stats}\n\
180            {lighting_stats}\n\
181            {pipeline_stats}\n\
182            Texture Cache Size: {texture_cache_size}\n\
183            Geometry Cache Size: {geometry_cache_size}\n\
184            Shader Cache Size: {shader_cache_size}\n
185            Uniform Buffer Cache Size: {uniform_buffer_cache_size}\n",
186        )
187    }
188}
189
190impl std::ops::AddAssign<RenderPassStatistics> for Statistics {
191    fn add_assign(&mut self, rhs: RenderPassStatistics) {
192        self.geometry += rhs;
193    }
194}