0031505: Point Cloud Rendering - fix on-screen statistics about number of visible...
[occt.git] / src / OpenGl / OpenGl_FrameStats.cxx
CommitLineData
15669413 1// Copyright (c) 2017 OPEN CASCADE SAS
2//
3// This file is part of Open CASCADE Technology software library.
4//
5// This library is free software; you can redistribute it and/or modify it under
6// the terms of the GNU Lesser General Public License version 2.1 as published
7// by the Free Software Foundation, with special exception defined in the file
8// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9// distribution for complete text of the license and disclaimer of any warranty.
10//
11// Alternatively, this file may be used under the terms of Open CASCADE
12// commercial license or contractual agreement.
13
14#include <OpenGl_FrameStats.hxx>
15
16#include <OpenGl_GlCore20.hxx>
17#include <OpenGl_View.hxx>
18#include <OpenGl_Workspace.hxx>
19
5e30547b 20IMPLEMENT_STANDARD_RTTIEXT(OpenGl_FrameStats, Graphic3d_FrameStats)
15669413 21
22namespace
23{
15669413 24 //! Return estimated data size.
25 static Standard_Size estimatedDataSize (const Handle(OpenGl_Resource)& theRes)
26 {
27 return !theRes.IsNull() ? theRes->EstimatedDataSize() : 0;
28 }
29}
30
31// =======================================================================
32// function : OpenGl_FrameStats
33// purpose :
34// =======================================================================
35OpenGl_FrameStats::OpenGl_FrameStats()
15669413 36{
5e30547b 37 //
15669413 38}
39
40// =======================================================================
41// function : ~OpenGl_FrameStats
42// purpose :
43// =======================================================================
44OpenGl_FrameStats::~OpenGl_FrameStats()
45{
46 //
47}
48
49// =======================================================================
5e30547b 50// function : IsFrameUpdated
15669413 51// purpose :
52// =======================================================================
5e30547b 53bool OpenGl_FrameStats::IsFrameUpdated (Handle(OpenGl_FrameStats)& thePrev) const
15669413 54{
5e30547b 55 const Graphic3d_FrameStatsData& aFrame = LastDataFrame();
56 if (thePrev.IsNull())
15669413 57 {
5e30547b 58 thePrev = new OpenGl_FrameStats();
15669413 59 }
5e30547b 60 // check just a couple of major counters
61 else if (myLastFrameIndex == thePrev->myLastFrameIndex
62 && Abs (aFrame.FrameRate() - thePrev->myCountersTmp.FrameRate()) <= 0.001
63 && Abs (aFrame.FrameRateCpu() - thePrev->myCountersTmp.FrameRateCpu()) <= 0.001
64 && aFrame[Graphic3d_FrameStatsCounter_NbLayers] == thePrev->myCountersTmp[Graphic3d_FrameStatsCounter_NbLayers]
65 && aFrame[Graphic3d_FrameStatsCounter_NbLayersNotCulled] == thePrev->myCountersTmp[Graphic3d_FrameStatsCounter_NbLayersNotCulled]
66 && aFrame[Graphic3d_FrameStatsCounter_NbStructs] == thePrev->myCountersTmp[Graphic3d_FrameStatsCounter_NbStructs]
67 && aFrame[Graphic3d_FrameStatsCounter_NbStructsNotCulled] == thePrev->myCountersTmp[Graphic3d_FrameStatsCounter_NbStructsNotCulled])
15669413 68 {
5e30547b 69 return false;
15669413 70 }
15669413 71
5e30547b 72 thePrev->myLastFrameIndex = myLastFrameIndex;
73 thePrev->myCountersTmp = aFrame;
74 return true;
15669413 75}
76
77// =======================================================================
5e30547b 78// function : updateStatistics
15669413 79// purpose :
80// =======================================================================
5e30547b 81void OpenGl_FrameStats::updateStatistics (const Handle(Graphic3d_CView)& theView,
82 bool theIsImmediateOnly)
15669413 83{
5e30547b 84 const OpenGl_View* aView = dynamic_cast<const OpenGl_View*> (theView.get());
85 if (aView == NULL)
15669413 86 {
5e30547b 87 myCounters.SetValue (myLastFrameIndex, myCountersTmp);
88 myCountersTmp.Reset();
15669413 89 return;
90 }
91
5e30547b 92 const Graphic3d_RenderingParams::PerfCounters aBits = theView->RenderingParams().CollectedStats;
15669413 93 const Standard_Boolean toCountMem = (aBits & Graphic3d_RenderingParams::PerfCounters_EstimMem) != 0;
94 const Standard_Boolean toCountTris = (aBits & Graphic3d_RenderingParams::PerfCounters_Triangles) != 0
b9f43ad1 95 || (aBits & Graphic3d_RenderingParams::PerfCounters_Lines) != 0
15669413 96 || (aBits & Graphic3d_RenderingParams::PerfCounters_Points) != 0;
97 const Standard_Boolean toCountElems = (aBits & Graphic3d_RenderingParams::PerfCounters_GroupArrays) != 0 || toCountTris || toCountMem;
98 const Standard_Boolean toCountGroups = (aBits & Graphic3d_RenderingParams::PerfCounters_Groups) != 0 || toCountElems;
5dc0517d 99 const Standard_Boolean toCountStructs = (aBits & Graphic3d_RenderingParams::PerfCounters_Structures) != 0
100 || (aBits & Graphic3d_RenderingParams::PerfCounters_Layers) != 0 || toCountGroups;
101
5e30547b 102 myCountersTmp[Graphic3d_FrameStatsCounter_NbLayers] = aView->LayerList().Layers().Size();
5dc0517d 103 if (toCountStructs
104 || (aBits & Graphic3d_RenderingParams::PerfCounters_Layers) != 0)
15669413 105 {
5e30547b 106 const Standard_Integer aViewId = aView->Identification();
1c728f2d 107 for (NCollection_List<Handle(Graphic3d_Layer)>::Iterator aLayerIter (aView->LayerList().Layers()); aLayerIter.More(); aLayerIter.Next())
15669413 108 {
109 const Handle(OpenGl_Layer)& aLayer = aLayerIter.Value();
5e30547b 110 myCountersTmp[Graphic3d_FrameStatsCounter_NbStructs] += aLayer->NbStructures();
111 if (theIsImmediateOnly && !aLayer->LayerSettings().IsImmediate())
112 {
113 continue;
114 }
115
15669413 116 if (!aLayer->IsCulled())
117 {
5e30547b 118 ++myCountersTmp[Graphic3d_FrameStatsCounter_NbLayersNotCulled];
15669413 119 }
5e30547b 120 myCountersTmp[Graphic3d_FrameStatsCounter_NbStructsNotCulled] += aLayer->NbStructuresNotCulled();
15669413 121 if (toCountGroups)
122 {
5dc0517d 123 updateStructures (aViewId, aLayer->CullableStructuresBVH().Structures(), toCountElems, toCountTris, toCountMem);
124 updateStructures (aViewId, aLayer->CullableTrsfPersStructuresBVH().Structures(), toCountElems, toCountTris, toCountMem);
125 updateStructures (aViewId, aLayer->NonCullableStructures(), toCountElems, toCountTris, toCountMem);
15669413 126 }
127 }
128 }
5e30547b 129 if (toCountMem)
15669413 130 {
5e30547b 131 for (OpenGl_Context::OpenGl_ResourcesMap::Iterator aResIter (aView->GlWindow()->GetGlContext()->SharedResources());
15669413 132 aResIter.More(); aResIter.Next())
133 {
5e30547b 134 myCountersTmp[Graphic3d_FrameStatsCounter_EstimatedBytesTextures] += aResIter.Value()->EstimatedDataSize();
15669413 135 }
136
15669413 137 {
5e30547b 138 Standard_Size& aMemFbos = myCountersTmp[Graphic3d_FrameStatsCounter_EstimatedBytesFbos];
15669413 139 // main FBOs
140 aMemFbos += estimatedDataSize (aView->myMainSceneFbos[0]);
141 aMemFbos += estimatedDataSize (aView->myMainSceneFbos[1]);
142 aMemFbos += estimatedDataSize (aView->myImmediateSceneFbos[0]);
143 aMemFbos += estimatedDataSize (aView->myImmediateSceneFbos[1]);
144 // OIT FBOs
145 aMemFbos += estimatedDataSize (aView->myMainSceneFbosOit[0]);
146 aMemFbos += estimatedDataSize (aView->myMainSceneFbosOit[1]);
147 aMemFbos += estimatedDataSize (aView->myImmediateSceneFbosOit[0]);
148 aMemFbos += estimatedDataSize (aView->myImmediateSceneFbosOit[1]);
149 // dump FBO
150 aMemFbos += estimatedDataSize (aView->myFBO);
151 // RayTracing FBO
152 aMemFbos += estimatedDataSize (aView->myOpenGlFBO);
153 aMemFbos += estimatedDataSize (aView->myOpenGlFBO2);
154 aMemFbos += estimatedDataSize (aView->myRaytraceFBO1[0]);
155 aMemFbos += estimatedDataSize (aView->myRaytraceFBO1[1]);
156 aMemFbos += estimatedDataSize (aView->myRaytraceFBO2[0]);
157 aMemFbos += estimatedDataSize (aView->myRaytraceFBO2[1]);
158 // also RayTracing
159 aMemFbos += estimatedDataSize (aView->myRaytraceOutputTexture[0]);
160 aMemFbos += estimatedDataSize (aView->myRaytraceOutputTexture[1]);
161 aMemFbos += estimatedDataSize (aView->myRaytraceVisualErrorTexture[0]);
162 aMemFbos += estimatedDataSize (aView->myRaytraceVisualErrorTexture[1]);
163 aMemFbos += estimatedDataSize (aView->myRaytraceTileOffsetsTexture[0]);
164 aMemFbos += estimatedDataSize (aView->myRaytraceTileOffsetsTexture[1]);
e084dbbc 165 aMemFbos += estimatedDataSize (aView->myRaytraceTileSamplesTexture[0]);
166 aMemFbos += estimatedDataSize (aView->myRaytraceTileSamplesTexture[1]);
15669413 167 }
168 {
169 // Ray Tracing geometry
5e30547b 170 Standard_Size& aMemGeom = myCountersTmp[Graphic3d_FrameStatsCounter_EstimatedBytesGeom];
15669413 171 aMemGeom += estimatedDataSize (aView->mySceneNodeInfoTexture);
172 aMemGeom += estimatedDataSize (aView->mySceneMinPointTexture);
173 aMemGeom += estimatedDataSize (aView->mySceneMaxPointTexture);
174 aMemGeom += estimatedDataSize (aView->mySceneTransformTexture);
175 aMemGeom += estimatedDataSize (aView->myGeometryVertexTexture);
176 aMemGeom += estimatedDataSize (aView->myGeometryNormalTexture);
177 aMemGeom += estimatedDataSize (aView->myGeometryTexCrdTexture);
178 aMemGeom += estimatedDataSize (aView->myGeometryTriangTexture);
179 aMemGeom += estimatedDataSize (aView->myRaytraceMaterialTexture);
180 aMemGeom += estimatedDataSize (aView->myRaytraceLightSrcTexture);
181 }
182 }
15669413 183}
184
185// =======================================================================
186// function : updateStructures
187// purpose :
188// =======================================================================
5dc0517d 189void OpenGl_FrameStats::updateStructures (Standard_Integer theViewId,
d325cb7f 190 const NCollection_IndexedMap<const Graphic3d_CStructure*>& theStructures,
15669413 191 Standard_Boolean theToCountElems,
192 Standard_Boolean theToCountTris,
193 Standard_Boolean theToCountMem)
194{
d325cb7f 195 for (OpenGl_Structure::StructIterator aStructIter (theStructures); aStructIter.More(); aStructIter.Next())
15669413 196 {
197 const OpenGl_Structure* aStruct = aStructIter.Value();
0de16e29 198 const bool isStructHidden = aStruct->IsCulled()
199 || !aStruct->IsVisible (theViewId);
200 for (; aStruct != NULL; aStruct = aStruct->InstancedStructure())
15669413 201 {
0de16e29 202 if (isStructHidden)
15669413 203 {
0de16e29 204 if (theToCountMem)
15669413 205 {
0de16e29 206 for (OpenGl_Structure::GroupIterator aGroupIter (aStruct->Groups()); aGroupIter.More(); aGroupIter.Next())
15669413 207 {
0de16e29 208 const OpenGl_Group* aGroup = aGroupIter.Value();
209 for (const OpenGl_ElementNode* aNodeIter = aGroup->FirstNode(); aNodeIter != NULL; aNodeIter = aNodeIter->next)
15669413 210 {
b9f43ad1 211 aNodeIter->elem->UpdateMemStats (myCountersTmp);
15669413 212 }
213 }
214 }
0de16e29 215 continue;
15669413 216 }
15669413 217
5e30547b 218 myCountersTmp[Graphic3d_FrameStatsCounter_NbGroupsNotCulled] += aStruct->Groups().Size();
0de16e29 219 if (!theToCountElems)
220 {
221 continue;
222 }
15669413 223
0de16e29 224 for (OpenGl_Structure::GroupIterator aGroupIter (aStruct->Groups()); aGroupIter.More(); aGroupIter.Next())
15669413 225 {
0de16e29 226 const OpenGl_Group* aGroup = aGroupIter.Value();
227 for (const OpenGl_ElementNode* aNodeIter = aGroup->FirstNode(); aNodeIter != NULL; aNodeIter = aNodeIter->next)
15669413 228 {
3d60d73f 229 if (theToCountMem)
230 {
b9f43ad1 231 aNodeIter->elem->UpdateMemStats (myCountersTmp);
15669413 232 }
b9f43ad1 233 aNodeIter->elem->UpdateDrawStats (myCountersTmp, theToCountTris);
15669413 234 }
15669413 235 }
236 }
237 }
238}