GraphicsCostEstimator 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #ifndef OSG_GRAPHICSCOSTESTIMATOR
  14. #define OSG_GRAPHICSCOSTESTIMATOR
  15. #include <osg/Referenced>
  16. #include <osg/ref_ptr>
  17. #include <utility>
  18. namespace osg
  19. {
  20. class Geometry;
  21. class Texture;
  22. class Program;
  23. class Node;
  24. class RenderInfo;
  25. struct ClampedLinearCostFunction1D
  26. {
  27. ClampedLinearCostFunction1D(double cost0=0.0, double dcost_di=0.0, unsigned int min_input=0):
  28. _cost0(cost0),
  29. _dcost_di(dcost_di),
  30. _min_input(min_input) {}
  31. void set(double cost0, double dcost_di, unsigned int min_input)
  32. {
  33. _cost0 = cost0;
  34. _dcost_di = dcost_di;
  35. _min_input = min_input;
  36. }
  37. double operator() (unsigned int input) const
  38. {
  39. return _cost0 + _dcost_di * double(input<=_min_input ? 0u : input-_min_input);
  40. }
  41. double _cost0;
  42. double _dcost_di;
  43. unsigned int _min_input;
  44. };
  45. /** Pair of double representing CPU and GPU times in seconds as first and second elements in std::pair. */
  46. typedef std::pair<double, double> CostPair;
  47. class OSG_EXPORT GeometryCostEstimator : public osg::Referenced
  48. {
  49. public:
  50. GeometryCostEstimator();
  51. void setDefaults();
  52. void calibrate(osg::RenderInfo& renderInfo);
  53. CostPair estimateCompileCost(const osg::Geometry* geometry) const;
  54. CostPair estimateDrawCost(const osg::Geometry* geometry) const;
  55. protected:
  56. ClampedLinearCostFunction1D _arrayCompileCost;
  57. ClampedLinearCostFunction1D _primtiveSetCompileCost;
  58. ClampedLinearCostFunction1D _arrayDrawCost;
  59. ClampedLinearCostFunction1D _primtiveSetDrawCost;
  60. double _displayListCompileConstant;
  61. double _displayListCompileFactor;
  62. };
  63. class OSG_EXPORT TextureCostEstimator : public osg::Referenced
  64. {
  65. public:
  66. TextureCostEstimator();
  67. void setDefaults();
  68. void calibrate(osg::RenderInfo& renderInfo);
  69. CostPair estimateCompileCost(const osg::Texture* texture) const;
  70. CostPair estimateDrawCost(const osg::Texture* texture) const;
  71. protected:
  72. ClampedLinearCostFunction1D _compileCost;
  73. ClampedLinearCostFunction1D _drawCost;
  74. };
  75. class OSG_EXPORT ProgramCostEstimator : public osg::Referenced
  76. {
  77. public:
  78. ProgramCostEstimator();
  79. void setDefaults();
  80. void calibrate(osg::RenderInfo& renderInfo);
  81. CostPair estimateCompileCost(const osg::Program* program) const;
  82. CostPair estimateDrawCost(const osg::Program* program) const;
  83. protected:
  84. ClampedLinearCostFunction1D _shaderCompileCost;
  85. ClampedLinearCostFunction1D _linkCost;
  86. ClampedLinearCostFunction1D _drawCost;
  87. };
  88. class OSG_EXPORT GraphicsCostEstimator : public osg::Referenced
  89. {
  90. public:
  91. GraphicsCostEstimator();
  92. /** set defaults for computing the costs.*/
  93. void setDefaults();
  94. /** calibrate the costs of various compile and draw operations */
  95. void calibrate(osg::RenderInfo& renderInfo);
  96. CostPair estimateCompileCost(const osg::Geometry* geometry) const { return _geometryEstimator->estimateCompileCost(geometry); }
  97. CostPair estimateDrawCost(const osg::Geometry* geometry) const { return _geometryEstimator->estimateDrawCost(geometry); }
  98. CostPair estimateCompileCost(const osg::Texture* texture) const { return _textureEstimator->estimateCompileCost(texture); }
  99. CostPair estimateDrawCost(const osg::Texture* texture) const { return _textureEstimator->estimateDrawCost(texture); }
  100. CostPair estimateCompileCost(const osg::Program* program) const { return _programEstimator->estimateCompileCost(program); }
  101. CostPair estimateDrawCost(const osg::Program* program) const { return _programEstimator->estimateDrawCost(program); }
  102. CostPair estimateCompileCost(const osg::Node* node) const;
  103. CostPair estimateDrawCost(const osg::Node* node) const;
  104. protected:
  105. virtual ~GraphicsCostEstimator();
  106. osg::ref_ptr<GeometryCostEstimator> _geometryEstimator;
  107. osg::ref_ptr<TextureCostEstimator> _textureEstimator;
  108. osg::ref_ptr<ProgramCostEstimator> _programEstimator;
  109. };
  110. }
  111. #endif