fyrox_graphics/
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 crate::framebuffer::DrawCallStatistics;
22use std::fmt::{Display, Formatter};
23
24/// Graphics pipeline statistics.
25#[derive(Debug, Default, Copy, Clone)]
26pub struct PipelineStatistics {
27    /// Total amount of texture that was bound to the pipeline during the rendering.
28    pub texture_binding_changes: usize,
29    /// Total amount of VBOs was bound to the pipeline during the rendering.
30    pub vbo_binding_changes: usize,
31    /// Total amount of VAOs was bound to the pipeline during the rendering.
32    pub vao_binding_changes: usize,
33    /// Total amount of blending state changed in the pipeline during the rendering.
34    pub blend_state_changes: usize,
35    /// Total amount of frame buffers was used during the rendering.
36    pub framebuffer_binding_changes: usize,
37    /// Total amount of programs was used in the pipeline during the rendering.
38    pub program_binding_changes: usize,
39}
40
41impl std::ops::AddAssign for PipelineStatistics {
42    fn add_assign(&mut self, rhs: Self) {
43        self.texture_binding_changes += rhs.texture_binding_changes;
44        self.vbo_binding_changes += rhs.vbo_binding_changes;
45        self.vao_binding_changes += rhs.vao_binding_changes;
46        self.blend_state_changes += rhs.blend_state_changes;
47        self.framebuffer_binding_changes += rhs.framebuffer_binding_changes;
48        self.program_binding_changes += rhs.program_binding_changes;
49    }
50}
51
52impl std::ops::Sub for PipelineStatistics {
53    type Output = PipelineStatistics;
54
55    fn sub(self, rhs: Self) -> Self::Output {
56        Self {
57            texture_binding_changes: self.texture_binding_changes - rhs.texture_binding_changes,
58            vbo_binding_changes: self.vbo_binding_changes - rhs.vbo_binding_changes,
59            vao_binding_changes: self.vao_binding_changes - rhs.vao_binding_changes,
60            blend_state_changes: self.blend_state_changes - rhs.blend_state_changes,
61            framebuffer_binding_changes: self.framebuffer_binding_changes
62                - rhs.framebuffer_binding_changes,
63            program_binding_changes: self.program_binding_changes - rhs.program_binding_changes,
64        }
65    }
66}
67
68impl Display for PipelineStatistics {
69    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
70        write!(
71            f,
72            "Pipeline state changes:\n\
73            \tTextures: {},\n\
74            \tVBO: {},\n\
75            \tVAO: {},\n\
76            \tFBO: {},\n\
77            \tShaders: {},\n\
78            \tBlend: {}",
79            self.texture_binding_changes,
80            self.vbo_binding_changes,
81            self.vao_binding_changes,
82            self.framebuffer_binding_changes,
83            self.program_binding_changes,
84            self.blend_state_changes
85        )
86    }
87}
88
89/// GPU statistics for single frame.
90#[derive(Debug, Copy, Clone, Default)]
91pub struct RenderPassStatistics {
92    /// Amount of draw calls per frame - lower the better.
93    pub draw_calls: usize,
94    /// Amount of triangles per frame.
95    pub triangles_rendered: usize,
96}
97
98impl Display for RenderPassStatistics {
99    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
100        write!(
101            f,
102            "Draw Calls: {}\n\
103            Triangles Rendered: {}",
104            self.draw_calls, self.triangles_rendered
105        )
106    }
107}
108
109impl std::ops::AddAssign for RenderPassStatistics {
110    fn add_assign(&mut self, rhs: Self) {
111        self.draw_calls += rhs.draw_calls;
112        self.triangles_rendered += rhs.triangles_rendered;
113    }
114}
115
116impl std::ops::AddAssign<DrawCallStatistics> for RenderPassStatistics {
117    fn add_assign(&mut self, rhs: DrawCallStatistics) {
118        self.draw_calls += 1;
119        self.triangles_rendered += rhs.triangles;
120    }
121}