Skip to main content

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