Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:04:36

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 
0024 #include <vector>
0025 
0026 class Graphic3d_RenderingParams;
0027 
0028 //! Tool object used for sampling screen tiles according to estimated pixel variance (used in path tracing engine).
0029 //! To improve GPU thread coherency, rendering window is split into pixel blocks or tiles. The important feature of
0030 //! this approach is that it is possible to keep the same number of tiles for any screen resolution (e.g. 256 tiles
0031 //! can be used for both 512 x 512 window and 1920 x 1080 window). So, a smaller number of tiles allows to increase
0032 //! interactivity (FPS), but at the cost of higher per-frame variance ('noise'). On the contrary a larger number of
0033 //! tiles decrease 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 
0039   //! Creates new tile sampler.
0040   Standard_EXPORT OpenGl_TileSampler();
0041 
0042   //! Size of individual tile in pixels.
0043   Graphic3d_Vec2i TileSize() const { return Graphic3d_Vec2i (myTileSize, myTileSize); }
0044 
0045   //! Scale factor for quantization of visual error (float) into signed integer.
0046   float VarianceScaleFactor() const { return myScaleFactor; }
0047 
0048   //! Returns number of tiles in X dimension.
0049   int NbTilesX() const { return (int)myTiles.SizeX; }
0050 
0051   //! Returns number of tiles in Y dimension.
0052   int NbTilesY() const { return (int)myTiles.SizeY; }
0053 
0054   //! Returns total number of tiles in viewport.
0055   int NbTiles() const { return int(myTiles.SizeX * myTiles.SizeY); }
0056 
0057   //! Returns ray-tracing viewport.
0058   const Graphic3d_Vec2i& ViewSize() const { return myViewSize; }
0059 
0060   //! Number of tiles within offsets texture.
0061   Graphic3d_Vec2i NbOffsetTiles (bool theAdaptive) const
0062   {
0063     return theAdaptive
0064          ? Graphic3d_Vec2i ((int )myOffsetsShrunk.SizeX, (int )myOffsetsShrunk.SizeY)
0065          : Graphic3d_Vec2i ((int )myOffsets.SizeX,       (int )myOffsets.SizeY);
0066   }
0067 
0068   //! Maximum number of tiles within offsets texture.
0069   Graphic3d_Vec2i NbOffsetTilesMax() const { return NbOffsetTiles (true).cwiseMax (NbOffsetTiles (false)); }
0070 
0071   //! Viewport for rendering using offsets texture.
0072   Graphic3d_Vec2i OffsetTilesViewport (bool theAdaptive) const { return NbOffsetTiles (theAdaptive) * myTileSize; }
0073 
0074   //! Maximum viewport for rendering using offsets texture.
0075   Graphic3d_Vec2i OffsetTilesViewportMax() const { return NbOffsetTilesMax() * myTileSize; }
0076 
0077   //! Return maximum number of samples per tile.
0078   int MaxTileSamples() const
0079   {
0080     int aNbSamples = 0;
0081     for (Standard_Size aRowIter = 0; aRowIter < myTiles.SizeY; ++aRowIter)
0082     {
0083       for (Standard_Size aColIter = 0; aColIter < myTiles.SizeX; ++aColIter)
0084       {
0085         aNbSamples = Max (aNbSamples, myTiles.Value (aRowIter, aColIter));
0086       }
0087     }
0088     return aNbSamples;
0089   }
0090 
0091   //! Specifies size of ray-tracing viewport and recomputes tile size.
0092   Standard_EXPORT void SetSize (const Graphic3d_RenderingParams& theParams,
0093                                 const Graphic3d_Vec2i& theSize);
0094 
0095   //! Fetches current error estimation from the GPU and
0096   //! builds 2D discrete distribution for tile sampling.
0097   Standard_EXPORT void GrabVarianceMap (const Handle(OpenGl_Context)& theContext,
0098                                         const Handle(OpenGl_Texture)& theTexture);
0099 
0100   //! Resets (restart) tile sampler to initial state.
0101   void Reset() { myLastSample = 0; }
0102 
0103   //! Uploads tile samples to the given OpenGL texture.
0104   bool UploadSamples (const Handle(OpenGl_Context)& theContext,
0105                       const Handle(OpenGl_Texture)& theSamplesTexture,
0106                       const bool theAdaptive)
0107   {
0108     return upload (theContext, theSamplesTexture, Handle(OpenGl_Texture)(), theAdaptive);
0109   }
0110 
0111   //! Uploads offsets of sampled tiles to the given OpenGL texture.
0112   bool UploadOffsets (const Handle(OpenGl_Context)& theContext,
0113                       const Handle(OpenGl_Texture)& theOffsetsTexture,
0114                       const bool theAdaptive)
0115   {
0116     return upload (theContext, Handle(OpenGl_Texture)(), theOffsetsTexture, theAdaptive);
0117   }
0118 
0119 protected:
0120 
0121   //! Returns number of pixels in the given tile.
0122   int tileArea (int theX, int theY) const
0123   {
0124     const int aSizeX = Min (myTileSize, myViewSize.x() - theX * myTileSize);
0125     const int aSizeY = Min (myTileSize, myViewSize.y() - theY * myTileSize);
0126     return aSizeX * aSizeY;
0127   }
0128 
0129   //! Samples tile location according to estimated error.
0130   Standard_EXPORT Graphic3d_Vec2i nextTileToSample();
0131 
0132   //! Uploads offsets of sampled tiles to the given OpenGL texture.
0133   Standard_EXPORT bool upload (const Handle(OpenGl_Context)& theContext,
0134                                const Handle(OpenGl_Texture)& theSamplesTexture,
0135                                const Handle(OpenGl_Texture)& theOffsetsTexture,
0136                                const bool theAdaptive);
0137 
0138   //! Auxiliary method for dumping 2D image map into stream (e.g. for debugging).
0139   Standard_EXPORT void dumpMap (std::ostream& theStream,
0140                                 const Image_PixMapTypedData<int>& theMap,
0141                                 const char* theTitle) const;
0142 
0143 protected:
0144 
0145   Image_PixMapTypedData<unsigned int>    myTiles;         //!< number of samples per tile (initially all 1)
0146   Image_PixMapTypedData<unsigned int>    myTileSamples;   //!< number of samples for all pixels within the tile (initially equals to Tile area)
0147   Image_PixMapTypedData<float>           myVarianceMap;   //!< Estimation of visual error per tile
0148   Image_PixMapTypedData<int>             myVarianceRaw;   //!< Estimation of visual error per tile (raw data)
0149   Image_PixMapTypedData<Graphic3d_Vec2i> myOffsets;       //!< 2D array of tiles redirecting to another tile
0150   Image_PixMapTypedData<Graphic3d_Vec2i> myOffsetsShrunk; //!< 2D array of tiles redirecting to another tile (shrunk)
0151   std::vector<float>                     myMarginalMap;   //!< Marginal distribution of 2D error map
0152   OpenGl_HaltonSampler                   mySampler;       //!< Halton sequence generator
0153   unsigned int                           myLastSample;    //!< Index of generated sample
0154   float                                  myScaleFactor;   //!< scale factor for quantization of visual error (float) into signed integer
0155   int                                    myTileSize;      //!< tile size
0156   Graphic3d_Vec2i                        myViewSize;      //!< ray-tracing viewport
0157 
0158 };
0159 
0160 #endif // _OpenGl_TileSampler_H