0030748: Visualization - Marker displayed in immediate layer ruins QT Quick view...
[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
66d1cdc6 22#include <Image_PixMapTypedData.hxx>
23
3a9b5dc8 24#include <vector>
25
66d1cdc6 26class Graphic3d_RenderingParams;
27
3a9b5dc8 28//! Tool object used for sampling screen tiles according to estimated pixel variance (used in path tracing engine).
4eaaf9d8 29//! To improve GPU thread coherency, rendering window is split into pixel blocks or tiles. The important feature of
30//! this approach is that it is possible to keep the same number of tiles for any screen resolution (e.g. 256 tiles
31//! can be used for both 512 x 512 window and 1920 x 1080 window). So, a smaller number of tiles allows to increase
32//! interactivity (FPS), but at the cost of higher per-frame variance ('noise'). On the contrary a larger number of
33//! tiles decrease interactivity, but leads to lower per-frame variance. Note that the total time needed to produce
34//! final final image is the same for both cases.
3a9b5dc8 35class OpenGl_TileSampler
36{
37public:
38
3a9b5dc8 39 //! Creates new tile sampler.
40 Standard_EXPORT OpenGl_TileSampler();
41
66d1cdc6 42 //! Size of individual tile in pixels.
43 Graphic3d_Vec2i TileSize() const { return Graphic3d_Vec2i (myTileSize, myTileSize); }
3a9b5dc8 44
66d1cdc6 45 //! Scale factor for quantization of visual error (float) into signed integer.
46 float VarianceScaleFactor() const { return myScaleFactor; }
3a9b5dc8 47
48 //! Returns number of tiles in X dimension.
66d1cdc6 49 int NbTilesX() const { return (int)myTiles.SizeX; }
3a9b5dc8 50
51 //! Returns number of tiles in Y dimension.
66d1cdc6 52 int NbTilesY() const { return (int)myTiles.SizeY; }
3a9b5dc8 53
54 //! Returns total number of tiles in viewport.
66d1cdc6 55 int NbTiles() const { return int(myTiles.SizeX * myTiles.SizeY); }
3a9b5dc8 56
66d1cdc6 57 //! Returns ray-tracing viewport.
58 const Graphic3d_Vec2i& ViewSize() const { return myViewSize; }
3a9b5dc8 59
66d1cdc6 60 //! Number of tiles within offsets texture.
61 Graphic3d_Vec2i NbOffsetTiles (bool theAdaptive) const
3a9b5dc8 62 {
66d1cdc6 63 return theAdaptive
64 ? Graphic3d_Vec2i ((int )myOffsetsShrunk.SizeX, (int )myOffsetsShrunk.SizeY)
65 : Graphic3d_Vec2i ((int )myOffsets.SizeX, (int )myOffsets.SizeY);
3a9b5dc8 66 }
67
66d1cdc6 68 //! Maximum number of tiles within offsets texture.
69 Graphic3d_Vec2i NbOffsetTilesMax() const { return NbOffsetTiles (true).cwiseMax (NbOffsetTiles (false)); }
70
71 //! Viewport for rendering using offsets texture.
72 Graphic3d_Vec2i OffsetTilesViewport (bool theAdaptive) const { return NbOffsetTiles (theAdaptive) * myTileSize; }
73
74 //! Maximum viewport for rendering using offsets texture.
75 Graphic3d_Vec2i OffsetTilesViewportMax() const { return NbOffsetTilesMax() * myTileSize; }
76
e084dbbc 77 //! Return maximum number of samples per tile.
78 int MaxTileSamples() const
79 {
80 int aNbSamples = 0;
81 for (Standard_Size aRowIter = 0; aRowIter < myTiles.SizeY; ++aRowIter)
82 {
83 for (Standard_Size aColIter = 0; aColIter < myTiles.SizeX; ++aColIter)
84 {
85 aNbSamples = Max (aNbSamples, myTiles.Value (aRowIter, aColIter));
86 }
87 }
88 return aNbSamples;
89 }
90
66d1cdc6 91 //! Specifies size of ray-tracing viewport and recomputes tile size.
92 Standard_EXPORT void SetSize (const Graphic3d_RenderingParams& theParams,
93 const Graphic3d_Vec2i& theSize);
94
3a9b5dc8 95 //! Fetches current error estimation from the GPU and
96 //! builds 2D discrete distribution for tile sampling.
66d1cdc6 97 Standard_EXPORT void GrabVarianceMap (const Handle(OpenGl_Context)& theContext,
98 const Handle(OpenGl_Texture)& theTexture);
3a9b5dc8 99
66d1cdc6 100 //! Resets (restart) tile sampler to initial state.
101 void Reset() { myLastSample = 0; }
3a9b5dc8 102
e084dbbc 103 //! Uploads tile samples to the given OpenGL texture.
104 bool UploadSamples (const Handle(OpenGl_Context)& theContext,
105 const Handle(OpenGl_Texture)& theSamplesTexture,
106 const bool theAdaptive)
107 {
108 return upload (theContext, theSamplesTexture, Handle(OpenGl_Texture)(), theAdaptive);
109 }
110
3a9b5dc8 111 //! Uploads offsets of sampled tiles to the given OpenGL texture.
e084dbbc 112 bool UploadOffsets (const Handle(OpenGl_Context)& theContext,
113 const Handle(OpenGl_Texture)& theOffsetsTexture,
114 const bool theAdaptive)
115 {
116 return upload (theContext, Handle(OpenGl_Texture)(), theOffsetsTexture, theAdaptive);
117 }
3a9b5dc8 118
119protected:
120
66d1cdc6 121 //! Returns number of pixels in the given tile.
122 int tileArea (int theX, int theY) const
3a9b5dc8 123 {
66d1cdc6 124 const int aSizeX = Min (myTileSize, myViewSize.x() - theX * myTileSize);
125 const int aSizeY = Min (myTileSize, myViewSize.y() - theY * myTileSize);
126 return aSizeX * aSizeY;
3a9b5dc8 127 }
128
66d1cdc6 129 //! Samples tile location according to estimated error.
130 Standard_EXPORT Graphic3d_Vec2i nextTileToSample();
3a9b5dc8 131
e084dbbc 132 //! Uploads offsets of sampled tiles to the given OpenGL texture.
133 Standard_EXPORT bool upload (const Handle(OpenGl_Context)& theContext,
134 const Handle(OpenGl_Texture)& theSamplesTexture,
135 const Handle(OpenGl_Texture)& theOffsetsTexture,
136 const bool theAdaptive);
137
138 //! Auxiliary method for dumping 2D image map into stream (e.g. for debugging).
139 Standard_EXPORT void dumpMap (std::ostream& theStream,
140 const Image_PixMapTypedData<int>& theMap,
141 const char* theTitle) const;
142
3a9b5dc8 143protected:
144
66d1cdc6 145 Image_PixMapTypedData<unsigned int> myTiles; //!< number of samples per tile (initially all 1)
e084dbbc 146 Image_PixMapTypedData<unsigned int> myTileSamples; //!< number of samples for all pixels within the tile (initially equals to Tile area)
66d1cdc6 147 Image_PixMapTypedData<float> myVarianceMap; //!< Estimation of visual error per tile
148 Image_PixMapTypedData<int> myVarianceRaw; //!< Estimation of visual error per tile (raw data)
149 Image_PixMapTypedData<Graphic3d_Vec2i> myOffsets; //!< 2D array of tiles redirecting to another tile
150 Image_PixMapTypedData<Graphic3d_Vec2i> myOffsetsShrunk; //!< 2D array of tiles redirecting to another tile (shrunk)
151 std::vector<float> myMarginalMap; //!< Marginal distribution of 2D error map
152 OpenGl_HaltonSampler mySampler; //!< Halton sequence generator
153 unsigned int myLastSample; //!< Index of generated sample
154 float myScaleFactor; //!< scale factor for quantization of visual error (float) into signed integer
155 int myTileSize; //!< tile size
156 Graphic3d_Vec2i myViewSize; //!< ray-tracing viewport
3a9b5dc8 157
158};
159
160#endif // _OpenGl_TileSampler_H