Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-27 09:18:47

0001 // Created on: 2016-06-16
0002 // Created by: Denis BOGOLEPOV & Danila ULYANOV
0003 // Copyright (c) 2016 OPEN CASCADE SAS
0004 //
0005 // This file is part of Open CASCADE Technology software library.
0006 //
0007 // This library is free software; you can redistribute it and/or modify it under
0008 // the terms of the GNU Lesser General Public License version 2.1 as published
0009 // by the Free Software Foundation, with special exception defined in the file
0010 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0011 // distribution for complete text of the license and disclaimer of any warranty.
0012 //
0013 // Alternatively, this file may be used under the terms of Open CASCADE
0014 // commercial license or contractual agreement.
0015 
0016 #ifndef _OpenGl_TileSampler_H
0017 #define _OpenGl_TileSampler_H
0018 
0019 #include <OpenGl_Texture.hxx>
0020 #include <OpenGl_HaltonSampler.hxx>
0021 
0022 #include <Image_PixMapTypedData.hxx>
0023 #include <NCollection_LinearVector.hxx>
0024 
0025 class Graphic3d_RenderingParams;
0026 
0027 //! Tool object used for sampling screen tiles according to estimated pixel variance (used in path
0028 //! tracing engine). To improve GPU thread coherency, rendering window is split into pixel blocks or
0029 //! tiles. The important feature of this approach is that it is possible to keep the same number of
0030 //! tiles for any screen resolution (e.g. 256 tiles can be used for both 512 x 512 window and 1920 x
0031 //! 1080 window). So, a smaller number of tiles allows to increase interactivity (FPS), but at the
0032 //! cost of higher per-frame variance ('noise'). On the contrary a larger number of tiles decrease
0033 //! interactivity, but leads to lower per-frame variance. Note that the total time needed to produce
0034 //! final final image is the same for both cases.
0035 class OpenGl_TileSampler
0036 {
0037 public:
0038   //! Creates new tile sampler.
0039   Standard_EXPORT OpenGl_TileSampler();
0040 
0041   //! Size of individual tile in pixels.
0042   NCollection_Vec2<int> TileSize() const { return NCollection_Vec2<int>(myTileSize, myTileSize); }
0043 
0044   //! Scale factor for quantization of visual error (float) into signed integer.
0045   float VarianceScaleFactor() const { return myScaleFactor; }
0046 
0047   //! Returns number of tiles in X dimension.
0048   int NbTilesX() const { return (int)myTiles.SizeX; }
0049 
0050   //! Returns number of tiles in Y dimension.
0051   int NbTilesY() const { return (int)myTiles.SizeY; }
0052 
0053   //! Returns total number of tiles in viewport.
0054   int NbTiles() const { return int(myTiles.SizeX * myTiles.SizeY); }
0055 
0056   //! Returns ray-tracing viewport.
0057   const NCollection_Vec2<int>& ViewSize() const { return myViewSize; }
0058 
0059   //! Number of tiles within offsets texture.
0060   NCollection_Vec2<int> NbOffsetTiles(bool theAdaptive) const
0061   {
0062     return theAdaptive
0063              ? NCollection_Vec2<int>((int)myOffsetsShrunk.SizeX, (int)myOffsetsShrunk.SizeY)
0064              : NCollection_Vec2<int>((int)myOffsets.SizeX, (int)myOffsets.SizeY);
0065   }
0066 
0067   //! Maximum number of tiles within offsets texture.
0068   NCollection_Vec2<int> NbOffsetTilesMax() const
0069   {
0070     return NbOffsetTiles(true).cwiseMax(NbOffsetTiles(false));
0071   }
0072 
0073   //! Viewport for rendering using offsets texture.
0074   NCollection_Vec2<int> OffsetTilesViewport(bool theAdaptive) const
0075   {
0076     return NbOffsetTiles(theAdaptive) * myTileSize;
0077   }
0078 
0079   //! Maximum viewport for rendering using offsets texture.
0080   NCollection_Vec2<int> OffsetTilesViewportMax() const { return NbOffsetTilesMax() * myTileSize; }
0081 
0082   //! Return maximum number of samples per tile.
0083   int MaxTileSamples() const
0084   {
0085     int aNbSamples = 0;
0086     for (size_t aRowIter = 0; aRowIter < myTiles.SizeY; ++aRowIter)
0087     {
0088       for (size_t aColIter = 0; aColIter < myTiles.SizeX; ++aColIter)
0089       {
0090         aNbSamples = (std::max)(aNbSamples, static_cast<int>(myTiles.Value(aRowIter, aColIter)));
0091       }
0092     }
0093     return aNbSamples;
0094   }
0095 
0096   //! Specifies size of ray-tracing viewport and recomputes tile size.
0097   Standard_EXPORT void SetSize(const Graphic3d_RenderingParams& theParams,
0098                                const NCollection_Vec2<int>&     theSize);
0099 
0100   //! Fetches current error estimation from the GPU and
0101   //! builds 2D discrete distribution for tile sampling.
0102   Standard_EXPORT void GrabVarianceMap(const occ::handle<OpenGl_Context>& theContext,
0103                                        const occ::handle<OpenGl_Texture>& theTexture);
0104 
0105   //! Resets (restart) tile sampler to initial state.
0106   void Reset() { myLastSample = 0; }
0107 
0108   //! Uploads tile samples to the given OpenGL texture.
0109   bool UploadSamples(const occ::handle<OpenGl_Context>& theContext,
0110                      const occ::handle<OpenGl_Texture>& theSamplesTexture,
0111                      const bool                         theAdaptive)
0112   {
0113     return upload(theContext, theSamplesTexture, occ::handle<OpenGl_Texture>(), theAdaptive);
0114   }
0115 
0116   //! Uploads offsets of sampled tiles to the given OpenGL texture.
0117   bool UploadOffsets(const occ::handle<OpenGl_Context>& theContext,
0118                      const occ::handle<OpenGl_Texture>& theOffsetsTexture,
0119                      const bool                         theAdaptive)
0120   {
0121     return upload(theContext, occ::handle<OpenGl_Texture>(), theOffsetsTexture, theAdaptive);
0122   }
0123 
0124 protected:
0125   //! Returns number of pixels in the given tile.
0126   int tileArea(int theX, int theY) const
0127   {
0128     const int aSizeX = (std::min)(myTileSize, myViewSize.x() - theX * myTileSize);
0129     const int aSizeY = (std::min)(myTileSize, myViewSize.y() - theY * myTileSize);
0130     return aSizeX * aSizeY;
0131   }
0132 
0133   //! Samples tile location according to estimated error.
0134   Standard_EXPORT NCollection_Vec2<int> nextTileToSample();
0135 
0136   //! Uploads offsets of sampled tiles to the given OpenGL texture.
0137   Standard_EXPORT bool upload(const occ::handle<OpenGl_Context>& theContext,
0138                               const occ::handle<OpenGl_Texture>& theSamplesTexture,
0139                               const occ::handle<OpenGl_Texture>& theOffsetsTexture,
0140                               const bool                         theAdaptive);
0141 
0142   //! Auxiliary method for dumping 2D image map into stream (e.g. for debugging).
0143   Standard_EXPORT void dumpMap(std::ostream&                     theStream,
0144                                const Image_PixMapTypedData<int>& theMap,
0145                                const char*                       theTitle) const;
0146 
0147 protected:
0148   // clang-format off
0149   Image_PixMapTypedData<unsigned int>    myTiles;         //!< number of samples per tile (initially all 1)
0150   Image_PixMapTypedData<unsigned int>    myTileSamples;   //!< number of samples for all pixels within the tile (initially equals to Tile area)
0151   Image_PixMapTypedData<float>           myVarianceMap;   //!< Estimation of visual error per tile
0152   Image_PixMapTypedData<int>             myVarianceRaw;   //!< Estimation of visual error per tile (raw data)
0153   Image_PixMapTypedData<NCollection_Vec2<int>> myOffsets;       //!< 2D array of tiles redirecting to another tile
0154   Image_PixMapTypedData<NCollection_Vec2<int>> myOffsetsShrunk; //!< 2D array of tiles redirecting to another tile (shrunk)
0155   NCollection_LinearVector<float>                myMarginalMap;   //!< Marginal distribution of 2D error map
0156   OpenGl_HaltonSampler                   mySampler;       //!< Halton sequence generator
0157   unsigned int                           myLastSample;    //!< Index of generated sample
0158   float                                  myScaleFactor;   //!< scale factor for quantization of visual error (float) into signed integer
0159   // clang-format on
0160   int                   myTileSize; //!< tile size
0161   NCollection_Vec2<int> myViewSize; //!< ray-tracing viewport
0162 };
0163 
0164 #endif // _OpenGl_TileSampler_H