openscenegraph
GraphicsCostEstimator
Go to the documentation of this file.
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version. The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14#ifndef OSG_GRAPHICSCOSTESTIMATOR
15#define OSG_GRAPHICSCOSTESTIMATOR
16
17#include <osg/Referenced>
18#include <osg/ref_ptr>
19#include <utility>
20
21namespace osg
22{
23
24class Geometry;
25class Texture;
26class Program;
27class Node;
28class RenderInfo;
29
30struct ClampedLinearCostFunction1D
31{
32 ClampedLinearCostFunction1D(double cost0=0.0, double dcost_di=0.0, unsigned int min_input=0):
33 _cost0(cost0),
34 _dcost_di(dcost_di),
35 _min_input(min_input) {}
36
37 void set(double cost0, double dcost_di, unsigned int min_input)
38 {
39 _cost0 = cost0;
40 _dcost_di = dcost_di;
41 _min_input = min_input;
42 }
43
44 double operator() (unsigned int input) const
45 {
46 return _cost0 + _dcost_di * double(input<=_min_input ? 0u : input-_min_input);
47 }
48 double _cost0;
49 double _dcost_di;
50 unsigned int _min_input;
51};
52
53/** Pair of double representing CPU and GPU times in seconds as first and second elements in std::pair. */
54typedef std::pair<double, double> CostPair;
55
56
57class OSG_EXPORT GeometryCostEstimator : public osg::Referenced
58{
59public:
60 GeometryCostEstimator();
61 void setDefaults();
62 void calibrate(osg::RenderInfo& renderInfo);
63 CostPair estimateCompileCost(const osg::Geometry* geometry) const;
64 CostPair estimateDrawCost(const osg::Geometry* geometry) const;
65
66protected:
67 ClampedLinearCostFunction1D _arrayCompileCost;
68 ClampedLinearCostFunction1D _primtiveSetCompileCost;
69
70 ClampedLinearCostFunction1D _arrayDrawCost;
71 ClampedLinearCostFunction1D _primtiveSetDrawCost;
72
73 double _displayListCompileConstant;
74 double _displayListCompileFactor;
75};
76
77class OSG_EXPORT TextureCostEstimator : public osg::Referenced
78{
79public:
80 TextureCostEstimator();
81 void setDefaults();
82 void calibrate(osg::RenderInfo& renderInfo);
83 CostPair estimateCompileCost(const osg::Texture* texture) const;
84 CostPair estimateDrawCost(const osg::Texture* texture) const;
85
86protected:
87 ClampedLinearCostFunction1D _compileCost;
88 ClampedLinearCostFunction1D _drawCost;
89};
90
91
92class OSG_EXPORT ProgramCostEstimator : public osg::Referenced
93{
94public:
95 ProgramCostEstimator();
96 void setDefaults();
97 void calibrate(osg::RenderInfo& renderInfo);
98 CostPair estimateCompileCost(const osg::Program* program) const;
99 CostPair estimateDrawCost(const osg::Program* program) const;
100
101protected:
102 ClampedLinearCostFunction1D _shaderCompileCost;
103 ClampedLinearCostFunction1D _linkCost;
104 ClampedLinearCostFunction1D _drawCost;
105};
106
107class OSG_EXPORT GraphicsCostEstimator : public osg::Referenced
108{
109public:
110 GraphicsCostEstimator();
111
112 /** set defaults for computing the costs.*/
113 void setDefaults();
114
115 /** calibrate the costs of various compile and draw operations */
116 void calibrate(osg::RenderInfo& renderInfo);
117
118 CostPair estimateCompileCost(const osg::Geometry* geometry) const { return _geometryEstimator->estimateCompileCost(geometry); }
119 CostPair estimateDrawCost(const osg::Geometry* geometry) const { return _geometryEstimator->estimateDrawCost(geometry); }
120
121 CostPair estimateCompileCost(const osg::Texture* texture) const { return _textureEstimator->estimateCompileCost(texture); }
122 CostPair estimateDrawCost(const osg::Texture* texture) const { return _textureEstimator->estimateDrawCost(texture); }
123
124 CostPair estimateCompileCost(const osg::Program* program) const { return _programEstimator->estimateCompileCost(program); }
125 CostPair estimateDrawCost(const osg::Program* program) const { return _programEstimator->estimateDrawCost(program); }
126
127 CostPair estimateCompileCost(const osg::Node* node) const;
128 CostPair estimateDrawCost(const osg::Node* node) const;
129
130protected:
131
132 virtual ~GraphicsCostEstimator();
133
134 osg::ref_ptr<GeometryCostEstimator> _geometryEstimator;
135 osg::ref_ptr<TextureCostEstimator> _textureEstimator;
136 osg::ref_ptr<ProgramCostEstimator> _programEstimator;
137
138};
139
140}
141
142#endif