0030668: Visualization - revise adding ZLayer API
[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
95 || (aBits & Graphic3d_RenderingParams::PerfCounters_Points) != 0;
96 const Standard_Boolean toCountElems = (aBits & Graphic3d_RenderingParams::PerfCounters_GroupArrays) != 0 || toCountTris || toCountMem;
97 const Standard_Boolean toCountGroups = (aBits & Graphic3d_RenderingParams::PerfCounters_Groups) != 0 || toCountElems;
5dc0517d 98 const Standard_Boolean toCountStructs = (aBits & Graphic3d_RenderingParams::PerfCounters_Structures) != 0
99 || (aBits & Graphic3d_RenderingParams::PerfCounters_Layers) != 0 || toCountGroups;
100
5e30547b 101 myCountersTmp[Graphic3d_FrameStatsCounter_NbLayers] = aView->LayerList().Layers().Size();
5dc0517d 102 if (toCountStructs
103 || (aBits & Graphic3d_RenderingParams::PerfCounters_Layers) != 0)
15669413 104 {
5e30547b 105 const Standard_Integer aViewId = aView->Identification();
1c728f2d 106 for (NCollection_List<Handle(Graphic3d_Layer)>::Iterator aLayerIter (aView->LayerList().Layers()); aLayerIter.More(); aLayerIter.Next())
15669413 107 {
108 const Handle(OpenGl_Layer)& aLayer = aLayerIter.Value();
5e30547b 109 myCountersTmp[Graphic3d_FrameStatsCounter_NbStructs] += aLayer->NbStructures();
110 if (theIsImmediateOnly && !aLayer->LayerSettings().IsImmediate())
111 {
112 continue;
113 }
114
15669413 115 if (!aLayer->IsCulled())
116 {
5e30547b 117 ++myCountersTmp[Graphic3d_FrameStatsCounter_NbLayersNotCulled];
15669413 118 }
5e30547b 119 myCountersTmp[Graphic3d_FrameStatsCounter_NbStructsNotCulled] += aLayer->NbStructuresNotCulled();
15669413 120 if (toCountGroups)
121 {
5dc0517d 122 updateStructures (aViewId, aLayer->CullableStructuresBVH().Structures(), toCountElems, toCountTris, toCountMem);
123 updateStructures (aViewId, aLayer->CullableTrsfPersStructuresBVH().Structures(), toCountElems, toCountTris, toCountMem);
124 updateStructures (aViewId, aLayer->NonCullableStructures(), toCountElems, toCountTris, toCountMem);
15669413 125 }
126 }
127 }
5e30547b 128 if (toCountMem)
15669413 129 {
5e30547b 130 for (OpenGl_Context::OpenGl_ResourcesMap::Iterator aResIter (aView->GlWindow()->GetGlContext()->SharedResources());
15669413 131 aResIter.More(); aResIter.Next())
132 {
5e30547b 133 myCountersTmp[Graphic3d_FrameStatsCounter_EstimatedBytesTextures] += aResIter.Value()->EstimatedDataSize();
15669413 134 }
135
15669413 136 {
5e30547b 137 Standard_Size& aMemFbos = myCountersTmp[Graphic3d_FrameStatsCounter_EstimatedBytesFbos];
15669413 138 // main FBOs
139 aMemFbos += estimatedDataSize (aView->myMainSceneFbos[0]);
140 aMemFbos += estimatedDataSize (aView->myMainSceneFbos[1]);
141 aMemFbos += estimatedDataSize (aView->myImmediateSceneFbos[0]);
142 aMemFbos += estimatedDataSize (aView->myImmediateSceneFbos[1]);
143 // OIT FBOs
144 aMemFbos += estimatedDataSize (aView->myMainSceneFbosOit[0]);
145 aMemFbos += estimatedDataSize (aView->myMainSceneFbosOit[1]);
146 aMemFbos += estimatedDataSize (aView->myImmediateSceneFbosOit[0]);
147 aMemFbos += estimatedDataSize (aView->myImmediateSceneFbosOit[1]);
148 // dump FBO
149 aMemFbos += estimatedDataSize (aView->myFBO);
150 // RayTracing FBO
151 aMemFbos += estimatedDataSize (aView->myOpenGlFBO);
152 aMemFbos += estimatedDataSize (aView->myOpenGlFBO2);
153 aMemFbos += estimatedDataSize (aView->myRaytraceFBO1[0]);
154 aMemFbos += estimatedDataSize (aView->myRaytraceFBO1[1]);
155 aMemFbos += estimatedDataSize (aView->myRaytraceFBO2[0]);
156 aMemFbos += estimatedDataSize (aView->myRaytraceFBO2[1]);
157 // also RayTracing
158 aMemFbos += estimatedDataSize (aView->myRaytraceOutputTexture[0]);
159 aMemFbos += estimatedDataSize (aView->myRaytraceOutputTexture[1]);
160 aMemFbos += estimatedDataSize (aView->myRaytraceVisualErrorTexture[0]);
161 aMemFbos += estimatedDataSize (aView->myRaytraceVisualErrorTexture[1]);
162 aMemFbos += estimatedDataSize (aView->myRaytraceTileOffsetsTexture[0]);
163 aMemFbos += estimatedDataSize (aView->myRaytraceTileOffsetsTexture[1]);
e084dbbc 164 aMemFbos += estimatedDataSize (aView->myRaytraceTileSamplesTexture[0]);
165 aMemFbos += estimatedDataSize (aView->myRaytraceTileSamplesTexture[1]);
15669413 166 }
167 {
168 // Ray Tracing geometry
5e30547b 169 Standard_Size& aMemGeom = myCountersTmp[Graphic3d_FrameStatsCounter_EstimatedBytesGeom];
15669413 170 aMemGeom += estimatedDataSize (aView->mySceneNodeInfoTexture);
171 aMemGeom += estimatedDataSize (aView->mySceneMinPointTexture);
172 aMemGeom += estimatedDataSize (aView->mySceneMaxPointTexture);
173 aMemGeom += estimatedDataSize (aView->mySceneTransformTexture);
174 aMemGeom += estimatedDataSize (aView->myGeometryVertexTexture);
175 aMemGeom += estimatedDataSize (aView->myGeometryNormalTexture);
176 aMemGeom += estimatedDataSize (aView->myGeometryTexCrdTexture);
177 aMemGeom += estimatedDataSize (aView->myGeometryTriangTexture);
178 aMemGeom += estimatedDataSize (aView->myRaytraceMaterialTexture);
179 aMemGeom += estimatedDataSize (aView->myRaytraceLightSrcTexture);
180 }
181 }
15669413 182}
183
184// =======================================================================
185// function : updateStructures
186// purpose :
187// =======================================================================
5dc0517d 188void OpenGl_FrameStats::updateStructures (Standard_Integer theViewId,
d325cb7f 189 const NCollection_IndexedMap<const Graphic3d_CStructure*>& theStructures,
15669413 190 Standard_Boolean theToCountElems,
191 Standard_Boolean theToCountTris,
192 Standard_Boolean theToCountMem)
193{
d325cb7f 194 for (OpenGl_Structure::StructIterator aStructIter (theStructures); aStructIter.More(); aStructIter.Next())
15669413 195 {
196 const OpenGl_Structure* aStruct = aStructIter.Value();
0de16e29 197 const bool isStructHidden = aStruct->IsCulled()
198 || !aStruct->IsVisible (theViewId);
199 for (; aStruct != NULL; aStruct = aStruct->InstancedStructure())
15669413 200 {
0de16e29 201 if (isStructHidden)
15669413 202 {
0de16e29 203 if (theToCountMem)
15669413 204 {
0de16e29 205 for (OpenGl_Structure::GroupIterator aGroupIter (aStruct->Groups()); aGroupIter.More(); aGroupIter.Next())
15669413 206 {
0de16e29 207 const OpenGl_Group* aGroup = aGroupIter.Value();
208 for (const OpenGl_ElementNode* aNodeIter = aGroup->FirstNode(); aNodeIter != NULL; aNodeIter = aNodeIter->next)
15669413 209 {
0de16e29 210 if (const OpenGl_PrimitiveArray* aPrim = dynamic_cast<const OpenGl_PrimitiveArray*> (aNodeIter->elem))
211 {
5e30547b 212 myCountersTmp[Graphic3d_FrameStatsCounter_EstimatedBytesGeom] += estimatedDataSize (aPrim->AttributesVbo());
213 myCountersTmp[Graphic3d_FrameStatsCounter_EstimatedBytesGeom] += estimatedDataSize (aPrim->IndexVbo());
0de16e29 214 }
15669413 215 }
216 }
217 }
0de16e29 218 continue;
15669413 219 }
15669413 220
5e30547b 221 myCountersTmp[Graphic3d_FrameStatsCounter_NbGroupsNotCulled] += aStruct->Groups().Size();
0de16e29 222 if (!theToCountElems)
223 {
224 continue;
225 }
15669413 226
0de16e29 227 for (OpenGl_Structure::GroupIterator aGroupIter (aStruct->Groups()); aGroupIter.More(); aGroupIter.Next())
15669413 228 {
0de16e29 229 const OpenGl_Group* aGroup = aGroupIter.Value();
230 for (const OpenGl_ElementNode* aNodeIter = aGroup->FirstNode(); aNodeIter != NULL; aNodeIter = aNodeIter->next)
15669413 231 {
0de16e29 232 if (const OpenGl_PrimitiveArray* aPrim = dynamic_cast<const OpenGl_PrimitiveArray*> (aNodeIter->elem))
15669413 233 {
5e30547b 234 ++myCountersTmp[Graphic3d_FrameStatsCounter_NbElemsNotCulled];
0de16e29 235 if (theToCountMem)
15669413 236 {
5e30547b 237 myCountersTmp[Graphic3d_FrameStatsCounter_EstimatedBytesGeom] += estimatedDataSize (aPrim->AttributesVbo());
238 myCountersTmp[Graphic3d_FrameStatsCounter_EstimatedBytesGeom] += estimatedDataSize (aPrim->IndexVbo());
15669413 239 }
240
0de16e29 241 if (aPrim->IsFillDrawMode())
15669413 242 {
5e30547b 243 ++myCountersTmp[Graphic3d_FrameStatsCounter_NbElemsFillNotCulled];
0de16e29 244 if (!theToCountTris)
15669413 245 {
0de16e29 246 continue;
15669413 247 }
0de16e29 248
249 const Handle(OpenGl_VertexBuffer)& anAttribs = aPrim->AttributesVbo();
250 if (anAttribs.IsNull()
251 || !anAttribs->IsValid())
15669413 252 {
0de16e29 253 continue;
15669413 254 }
0de16e29 255
256 const Handle(OpenGl_VertexBuffer)& anIndices = aPrim->IndexVbo();
257 const Standard_Integer aNbIndices = !anIndices.IsNull() ? anIndices->GetElemsNb() : anAttribs->GetElemsNb();
258 const Standard_Integer aNbBounds = !aPrim->Bounds().IsNull() ? aPrim->Bounds()->NbBounds : 1;
259 switch (aPrim->DrawMode())
15669413 260 {
0de16e29 261 case GL_TRIANGLES:
262 {
5e30547b 263 myCountersTmp[Graphic3d_FrameStatsCounter_NbTrianglesNotCulled] += aNbIndices / 3;
0de16e29 264 break;
265 }
266 case GL_TRIANGLE_STRIP:
267 case GL_TRIANGLE_FAN:
268 {
5e30547b 269 myCountersTmp[Graphic3d_FrameStatsCounter_NbTrianglesNotCulled] += aNbIndices - 2 * aNbBounds;
0de16e29 270 break;
271 }
272 case GL_TRIANGLES_ADJACENCY:
273 {
5e30547b 274 myCountersTmp[Graphic3d_FrameStatsCounter_NbTrianglesNotCulled] += aNbIndices / 6;
0de16e29 275 break;
276 }
277 case GL_TRIANGLE_STRIP_ADJACENCY:
278 {
5e30547b 279 myCountersTmp[Graphic3d_FrameStatsCounter_NbTrianglesNotCulled] += aNbIndices - 4 * aNbBounds;
0de16e29 280 break;
281 }
282 #if !defined(GL_ES_VERSION_2_0)
283 case GL_QUADS:
284 {
5e30547b 285 myCountersTmp[Graphic3d_FrameStatsCounter_NbTrianglesNotCulled] += aNbIndices / 2;
0de16e29 286 break;
287 }
288 case GL_QUAD_STRIP:
289 {
5e30547b 290 myCountersTmp[Graphic3d_FrameStatsCounter_NbTrianglesNotCulled] += (aNbIndices / 2 - aNbBounds) * 2;
0de16e29 291 break;
292 }
293 #endif
15669413 294 }
15669413 295 }
0de16e29 296 else if (aPrim->DrawMode() == GL_POINTS)
15669413 297 {
5e30547b 298 ++myCountersTmp[Graphic3d_FrameStatsCounter_NbElemsPointNotCulled];
0de16e29 299 if (theToCountTris)
15669413 300 {
0de16e29 301 const Handle(OpenGl_VertexBuffer)& anAttribs = aPrim->AttributesVbo();
302 if (!anAttribs.IsNull()
303 && anAttribs->IsValid())
304 {
305 const Handle(OpenGl_VertexBuffer)& anIndices = aPrim->IndexVbo();
306 const Standard_Integer aNbIndices = !anIndices.IsNull() ? anIndices->GetElemsNb() : anAttribs->GetElemsNb();
5e30547b 307 myCountersTmp[Graphic3d_FrameStatsCounter_NbPointsNotCulled] += aNbIndices;
0de16e29 308 }
15669413 309 }
310 }
0de16e29 311 else
312 {
5e30547b 313 ++myCountersTmp[Graphic3d_FrameStatsCounter_NbElemsLineNotCulled];
0de16e29 314 }
15669413 315 }
0de16e29 316 else if (const OpenGl_Text* aText = dynamic_cast<const OpenGl_Text*> (aNodeIter->elem))
15669413 317 {
0de16e29 318 (void )aText;
5e30547b 319 ++myCountersTmp[Graphic3d_FrameStatsCounter_NbElemsNotCulled];
320 ++myCountersTmp[Graphic3d_FrameStatsCounter_NbElemsTextNotCulled];
15669413 321 }
322 }
15669413 323 }
324 }
325 }
326}