0027607: Visualization - Implement adaptive screen space sampling in path tracing
[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).
25//! To improve GPU thread coherency, render window is split into pixel blocks or tiles.
26class OpenGl_TileSampler
27{
28public:
29
30 //! Size of individual tile in pixels.
31 static int TileSize() { return 32; }
32
33public:
34
35 //! Creates new tile sampler.
36 Standard_EXPORT OpenGl_TileSampler();
37
38 //! Returns width of ray-tracing viewport.
39 int SizeX() const { return mySizeX; }
40
41 //! Returns height of ray-tracing viewport.
42 int SizeY() const { return mySizeY; }
43
44 //! Returns number of tiles in X dimension.
45 int NbTilesX() const { return myTilesX; }
46
47 //! Returns number of tiles in Y dimension.
48 int NbTilesY() const { return myTilesY; }
49
50 //! Returns total number of tiles in viewport.
51 int NbTiles() const { return myTilesX * myTilesY; }
52
53 //! Specifies size of ray-tracing viewport.
54 Standard_EXPORT void SetSize (const int theSizeX,
55 const int theSizeY);
56
57 //! Returns number of pixels in the given tile.
58 int TileArea (const int theX,
59 const int theY) const
60 {
61 return Min (TileSize(), mySizeX - theX * TileSize())
62 * Min (TileSize(), mySizeY - theY * TileSize());
63 }
64
65 //! Fetches current error estimation from the GPU and
66 //! builds 2D discrete distribution for tile sampling.
67 Standard_EXPORT void GrabVarianceMap (const Handle(OpenGl_Context)& theContext);
68
69 //! Samples tile location according to estimated error.
70 Standard_EXPORT void Sample (int& theOffsetX,
71 int& theOffsetY);
72
73 //! Resets tile sampler to initial state.
74 void Reset() { mySample = 0; }
75
76 //! Uploads offsets of sampled tiles to the given OpenGL texture.
77 Standard_EXPORT void Upload (const Handle(OpenGl_Context)& theContext,
78 const Handle(OpenGl_Texture)& theTexture,
79 bool theAdaptive);
80
81protected:
82
83 //! Returns tile value (estimated error).
84 float Tile (const int theX,
85 const int theY) const
86 {
87 return myVarianceMap[theY * myTilesX + theX];
88 }
89
90 //! Returns tile value (estimated error).
91 float& ChangeTile (const int theX,
92 const int theY)
93 {
94 return myVarianceMap[theY * myTilesX + theX];
95 }
96
97protected:
98
99 std::vector<float> myVarianceMap; //!< Estimation of visual error per tile
100 std::vector<float> myMarginalMap; //!< Marginal distribution of 2D error map
101 OpenGl_HaltonSampler mySampler; //!< Halton sequence generator
102 int mySample; //!< Index of generated sample
103 int mySizeX; //!< Width of ray-tracing viewport
104 int mySizeY; //!< Height of ray-tracing viewport
105 int myTilesX; //!< Number of tiles in X dimension
106 int myTilesY; //!< Number of tiles in Y dimension
107
108};
109
110#endif // _OpenGl_TileSampler_H