0030434: Visualization, TKV3d - add "NoUpdate" state of frustum culling optimization
[occt.git] / src / OpenGl / OpenGl_TileSampler.hxx
CommitLineData
3a9b5dc8 1// Created on: 2016-06-16
2// Created by: Denis BOGOLEPOV & Danila ULYANOV
3// Copyright (c) 2016 OPEN CASCADE SAS
4//
5// This file is part of Open CASCADE Technology software library.
6//
7// This library is free software; you can redistribute it and/or modify it under
8// the terms of the GNU Lesser General Public License version 2.1 as published
9// by the Free Software Foundation, with special exception defined in the file
10// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11// distribution for complete text of the license and disclaimer of any warranty.
12//
13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
15
16#ifndef _OpenGl_TileSampler_H
17#define _OpenGl_TileSampler_H
18
19#include <OpenGl_Texture.hxx>
20#include <OpenGl_HaltonSampler.hxx>
21
22#include <vector>
23
24//! Tool object used for sampling screen tiles according to estimated pixel variance (used in path tracing engine).
4eaaf9d8 25//! To improve GPU thread coherency, rendering window is split into pixel blocks or tiles. The important feature of
26//! this approach is that it is possible to keep the same number of tiles for any screen resolution (e.g. 256 tiles
27//! can be used for both 512 x 512 window and 1920 x 1080 window). So, a smaller number of tiles allows to increase
28//! interactivity (FPS), but at the cost of higher per-frame variance ('noise'). On the contrary a larger number of
29//! tiles decrease interactivity, but leads to lower per-frame variance. Note that the total time needed to produce
30//! final final image is the same for both cases.
3a9b5dc8 31class OpenGl_TileSampler
32{
33public:
34
35 //! Size of individual tile in pixels.
36 static int TileSize() { return 32; }
37
38public:
39
40 //! Creates new tile sampler.
41 Standard_EXPORT OpenGl_TileSampler();
42
43 //! Returns width of ray-tracing viewport.
44 int SizeX() const { return mySizeX; }
45
46 //! Returns height of ray-tracing viewport.
47 int SizeY() const { return mySizeY; }
48
49 //! Returns number of tiles in X dimension.
50 int NbTilesX() const { return myTilesX; }
51
52 //! Returns number of tiles in Y dimension.
53 int NbTilesY() const { return myTilesY; }
54
55 //! Returns total number of tiles in viewport.
56 int NbTiles() const { return myTilesX * myTilesY; }
57
58 //! Specifies size of ray-tracing viewport.
59 Standard_EXPORT void SetSize (const int theSizeX,
60 const int theSizeY);
61
62 //! Returns number of pixels in the given tile.
63 int TileArea (const int theX,
64 const int theY) const
65 {
66 return Min (TileSize(), mySizeX - theX * TileSize())
67 * Min (TileSize(), mySizeY - theY * TileSize());
68 }
69
70 //! Fetches current error estimation from the GPU and
71 //! builds 2D discrete distribution for tile sampling.
72 Standard_EXPORT void GrabVarianceMap (const Handle(OpenGl_Context)& theContext);
73
74 //! Samples tile location according to estimated error.
75 Standard_EXPORT void Sample (int& theOffsetX,
76 int& theOffsetY);
77
78 //! Resets tile sampler to initial state.
79 void Reset() { mySample = 0; }
80
81 //! Uploads offsets of sampled tiles to the given OpenGL texture.
82 Standard_EXPORT void Upload (const Handle(OpenGl_Context)& theContext,
83 const Handle(OpenGl_Texture)& theTexture,
4eaaf9d8 84 const int theNbTilesX,
85 const int theNbTilesY,
86 const bool theAdaptive);
3a9b5dc8 87
88protected:
89
90 //! Returns tile value (estimated error).
91 float Tile (const int theX,
92 const int theY) const
93 {
94 return myVarianceMap[theY * myTilesX + theX];
95 }
96
97 //! Returns tile value (estimated error).
98 float& ChangeTile (const int theX,
99 const int theY)
100 {
101 return myVarianceMap[theY * myTilesX + theX];
102 }
103
104protected:
105
106 std::vector<float> myVarianceMap; //!< Estimation of visual error per tile
107 std::vector<float> myMarginalMap; //!< Marginal distribution of 2D error map
108 OpenGl_HaltonSampler mySampler; //!< Halton sequence generator
109 int mySample; //!< Index of generated sample
110 int mySizeX; //!< Width of ray-tracing viewport
111 int mySizeY; //!< Height of ray-tracing viewport
112 int myTilesX; //!< Number of tiles in X dimension
113 int myTilesY; //!< Number of tiles in Y dimension
114
115};
116
117#endif // _OpenGl_TileSampler_H