fyrox_graphics/
stats.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright (c) 2019-present Dmitry Stepanov and Fyrox Engine contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

use crate::framebuffer::DrawCallStatistics;
use std::fmt::{Display, Formatter};

/// Graphics pipeline statistics.
#[derive(Debug, Default, Copy, Clone)]
pub struct PipelineStatistics {
    /// Total amount of texture that was bound to the pipeline during the rendering.
    pub texture_binding_changes: usize,
    /// Total amount of VBOs was bound to the pipeline during the rendering.
    pub vbo_binding_changes: usize,
    /// Total amount of VAOs was bound to the pipeline during the rendering.
    pub vao_binding_changes: usize,
    /// Total amount of blending state changed in the pipeline during the rendering.
    pub blend_state_changes: usize,
    /// Total amount of frame buffers was used during the rendering.
    pub framebuffer_binding_changes: usize,
    /// Total amount of programs was used in the pipeline during the rendering.
    pub program_binding_changes: usize,
}

impl std::ops::AddAssign for PipelineStatistics {
    fn add_assign(&mut self, rhs: Self) {
        self.texture_binding_changes += rhs.texture_binding_changes;
        self.vbo_binding_changes += rhs.vbo_binding_changes;
        self.vao_binding_changes += rhs.vao_binding_changes;
        self.blend_state_changes += rhs.blend_state_changes;
        self.framebuffer_binding_changes += rhs.framebuffer_binding_changes;
        self.program_binding_changes += rhs.program_binding_changes;
    }
}

impl std::ops::Sub for PipelineStatistics {
    type Output = PipelineStatistics;

    fn sub(self, rhs: Self) -> Self::Output {
        Self {
            texture_binding_changes: self.texture_binding_changes - rhs.texture_binding_changes,
            vbo_binding_changes: self.vbo_binding_changes - rhs.vbo_binding_changes,
            vao_binding_changes: self.vao_binding_changes - rhs.vao_binding_changes,
            blend_state_changes: self.blend_state_changes - rhs.blend_state_changes,
            framebuffer_binding_changes: self.framebuffer_binding_changes
                - rhs.framebuffer_binding_changes,
            program_binding_changes: self.program_binding_changes - rhs.program_binding_changes,
        }
    }
}

impl Display for PipelineStatistics {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "Pipeline state changes:\n\
            \tTextures: {},\n\
            \tVBO: {},\n\
            \tVAO: {},\n\
            \tFBO: {},\n\
            \tShaders: {},\n\
            \tBlend: {}",
            self.texture_binding_changes,
            self.vbo_binding_changes,
            self.vao_binding_changes,
            self.framebuffer_binding_changes,
            self.program_binding_changes,
            self.blend_state_changes
        )
    }
}

/// GPU statistics for single frame.
#[derive(Debug, Copy, Clone, Default)]
pub struct RenderPassStatistics {
    /// Amount of draw calls per frame - lower the better.
    pub draw_calls: usize,
    /// Amount of triangles per frame.
    pub triangles_rendered: usize,
}

impl Display for RenderPassStatistics {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "Draw Calls: {}\n\
            Triangles Rendered: {}",
            self.draw_calls, self.triangles_rendered
        )
    }
}

impl std::ops::AddAssign for RenderPassStatistics {
    fn add_assign(&mut self, rhs: Self) {
        self.draw_calls += rhs.draw_calls;
        self.triangles_rendered += rhs.triangles_rendered;
    }
}

impl std::ops::AddAssign<DrawCallStatistics> for RenderPassStatistics {
    fn add_assign(&mut self, rhs: DrawCallStatistics) {
        self.draw_calls += 1;
        self.triangles_rendered += rhs.triangles;
    }
}