0027607: Visualization - Implement adaptive screen space sampling in path tracing
[occt.git] / src / OpenGl / OpenGl_View.hxx
index 3005cd1..06a8053 100644 (file)
@@ -28,8 +28,6 @@
 #include <Aspect_GradientFillMethod.hxx>
 #include <Aspect_TypeOfTriedronPosition.hxx>
 
-#include <InterfaceGraphic_Graphic3d.hxx>
-
 #include <Graphic3d_CView.hxx>
 #include <Graphic3d_GraduatedTrihedron.hxx>
 #include <Graphic3d_SequenceOfHClipPlane.hxx>
 #include <OpenGl_Trihedron.hxx>
 #include <OpenGl_Window.hxx>
 #include <OpenGl_Workspace.hxx>
+#include <OpenGl_TileSampler.hxx>
 
 #include <map>
 #include <set>
 
-struct OPENGL_ZCLIP
-{
-  struct {
-    Standard_Boolean   IsOn;
-    Standard_ShortReal Limit; /* in the range [0., 1.] */
-  } Back;
-  struct {
-    Standard_Boolean   IsOn;
-    Standard_ShortReal Limit; /* in the range [0., 1.] */
-  } Front;
-};
-
-struct OPENGL_FOG
-{
-  Standard_Boolean   IsOn;
-  Standard_ShortReal Front; /* in the range [0., 1.] */
-  Standard_ShortReal Back; /* in the range [0., 1.] */
-  TEL_COLOUR         Color;
-};
-
 struct OpenGl_Matrix;
 
 class Graphic3d_StructureManager;
@@ -189,27 +168,6 @@ public:
   Standard_EXPORT virtual Standard_Boolean BufferDump (Image_PixMap& theImage,
                                                        const Graphic3d_BufferType& theBufferType) Standard_OVERRIDE;
 
-  //! Print the contents of the view to the printer.
-  //! @param thePrinterDC        pass the PrinterDeviceContext (HDC)
-  //! @param theToShowBackground when set to FALSE then print the view without background
-  //!                            color (background is white) else set to TRUE for printing
-  //!                            with current background color
-  //! @param theFileName         if != NULL, then the view will be printed to a file
-  //! @param thePrintAlgorithm   select print algorithm: stretch, tile
-  //! @param theScaleFactor      scaling coefficient, used internally to scale the printings
-  //!                            accordingly to the scale factor selected in the printer properties dialog
-  //! @return Standard_True if the data is passed to the printer, otherwise Standard_False if
-  //! the print operation failed due to the printer errors, or lack of system memory. This might be related
-  //! to insufficient memory or some internal errors.
-  //! All this errors are indicated by the message boxes (on level of OpenGl_GraphicDriver).
-  //! Warning: This function can reuse FBO assigned to the view, please take it into account
-  //! if you use it for your purposes.
-  Standard_EXPORT virtual Standard_Boolean Print (const Aspect_Handle    thePrinterDC,
-                                                  const Standard_Boolean theToShowBackground,
-                                                  const Standard_CString theFileName,
-                                                  const Aspect_PrintAlgo thePrintAlgorithm = Aspect_PA_STRETCH,
-                                                  const Standard_Real    theScaleFactor = 1.0) Standard_OVERRIDE;
-
   //! Export scene into the one of the Vector graphics formats (SVG, PS, PDF...).
   //! In contrast to Bitmaps, Vector graphics is scalable (so you may got quality benefits
   //! on printing to laser printer). Notice however that results may differ a lot and
@@ -240,12 +198,18 @@ public:
   Standard_EXPORT virtual void InvalidateZLayerBoundingBox (const Graphic3d_ZLayerId theLayerId) const Standard_OVERRIDE;
 
   //! Returns the bounding box of all structures displayed in the Z layer.
-  //! Never fails. If Z layer does not exist the empty box is returned.
+  //! If Z layer does not exist the empty box is returned.
+  //! @param theLayerId            layer identifier
+  //! @param theCamera             camera definition
+  //! @param theWindowWidth        viewport width  (for applying transformation-persistence)
+  //! @param theWindowHeight       viewport height (for applying transformation-persistence)
+  //! @param theToIncludeAuxiliary consider also auxiliary presentations (with infinite flag or with trihedron transformation persistence)
+  //! @return computed bounding box
   Standard_EXPORT virtual Graphic3d_BndBox4f ZLayerBoundingBox (const Graphic3d_ZLayerId        theLayerId,
                                                                 const Handle(Graphic3d_Camera)& theCamera,
                                                                 const Standard_Integer          theWindowWidth,
                                                                 const Standard_Integer          theWindowHeight,
-                                                                const Standard_Boolean          theToIgnoreInfiniteFlag) const Standard_OVERRIDE;
+                                                                const Standard_Boolean          theToIncludeAuxiliary) const Standard_OVERRIDE;
 
   //! Returns pointer to an assigned framebuffer object.
   Standard_EXPORT virtual Handle(Standard_Transient) FBO() const Standard_OVERRIDE;
@@ -275,12 +239,6 @@ public:
 
 public:
 
-  //! Returns true if anti-aliasing is enabled for the view.
-  Standard_Boolean virtual IsAntialiasingEnabled() const Standard_OVERRIDE { return myAntiAliasing; }
-
-  //! Enable or disable anti-aliasing in the view.
-  virtual void SetAntialiasingEnabled (const Standard_Boolean theIsEnabled) Standard_OVERRIDE { myAntiAliasing = theIsEnabled; }
-
   //! Returns background  fill color.
   Standard_EXPORT virtual Aspect_Background Background() const Standard_OVERRIDE;
 
@@ -335,60 +293,6 @@ public:
   //! Sets camera used by the view.
   virtual void SetCamera (const Handle(Graphic3d_Camera)& theCamera) Standard_OVERRIDE { myCamera = theCamera; }
 
-  //! Returns the activity of back z-clipping plane.
-  virtual Standard_Boolean BackZClippingIsOn() const Standard_OVERRIDE { return myZClip.Back.IsOn; }
-
-  //! Activates the back Z-clipping plane.
-  virtual void SetBackZClippingOn (const Standard_Boolean theIsOn) Standard_OVERRIDE { myZClip.Back.IsOn = theIsOn; }
-
-  //! Returns the definition of the back Z-clipping plane.
-  virtual Standard_Real ZClippingBackPlane() const Standard_OVERRIDE { return myZClip.Back.Limit; }
-
-  //! Sets the definition of the back Z-clipping plane.
-  virtual void SetZClippingBackPlane (const Standard_Real theValue) Standard_OVERRIDE
-  {
-    myZClip.Back.Limit = static_cast<Standard_ShortReal> (theValue);
-  }
-
-  //! Returns the activity of front z-clipping plane.
-  virtual Standard_Boolean FrontZClippingIsOn() const Standard_OVERRIDE { return myZClip.Front.IsOn; }
-
-  //! Activates the front Z-clipping plane.
-  virtual void SetFrontZClippingOn (const Standard_Boolean theIsOn) Standard_OVERRIDE{ myZClip.Front.IsOn = theIsOn; }
-
-  //! Returns the definition of the front Z-clipping plane.
-  virtual Standard_Real ZClippingFrontPlane() const Standard_OVERRIDE { return myZClip.Front.Limit; }
-
-  //! Sets the definition of the front Z-clipping plane.
-  virtual void SetZClippingFrontPlane (const Standard_Real theValue) Standard_OVERRIDE
-  {
-    myZClip.Front.Limit = static_cast<Standard_ShortReal> (theValue);
-  }
-
-  //! Returns the activity of depth cueing.
-  virtual Standard_Boolean DepthCueingIsOn() const Standard_OVERRIDE { return myFog.IsOn; }
-
-  //! Sets the activity of depth cueing.
-  virtual void SetDepthCueingOn (const Standard_Boolean theIsOn) Standard_OVERRIDE { myFog.IsOn = theIsOn; }
-
-  //! Returns the back depth cueing plane.
-  virtual Standard_Real DepthCueingBackPlane() const Standard_OVERRIDE { return myFog.Back; }
-
-  //! Set the back depth cueing plane.
-  virtual void SetDepthCueingBackPlane (const Standard_Real theValue) Standard_OVERRIDE
-  {
-    myFog.Back = static_cast<Standard_ShortReal> (theValue);
-  }
-
-  //! Returns the front depth cueing plane.
-  virtual Standard_Real DepthCueingFrontPlane() const Standard_OVERRIDE { return myFog.Front; }
-
-  //! Set the front depth cueing plane.
-  virtual void SetDepthCueingFrontPlane (const Standard_Real theValue) Standard_OVERRIDE
-  {
-    myFog.Front = static_cast<Standard_ShortReal> (theValue);
-  }
-
   //! Returns true if GL lighting is enabled.
   virtual Standard_Boolean IsGLLightEnabled() const Standard_OVERRIDE { return myUseGLLight; }
 
@@ -406,15 +310,25 @@ public:
   }
 
   //! Returns list of clip planes set for the view.
-  virtual const Graphic3d_SequenceOfHClipPlane& ClipPlanes() const Standard_OVERRIDE { return myClipPlanes; }
+  virtual const Handle(Graphic3d_SequenceOfHClipPlane)& ClipPlanes() const Standard_OVERRIDE { return myClipPlanes; }
 
   //! Sets list of clip planes for the view.
-  virtual void SetClipPlanes (const Graphic3d_SequenceOfHClipPlane& thePlanes) Standard_OVERRIDE { myClipPlanes = thePlanes; }
+  virtual void SetClipPlanes (const Handle(Graphic3d_SequenceOfHClipPlane)& thePlanes) Standard_OVERRIDE { myClipPlanes = thePlanes; }
+
+  //! Fill in the dictionary with diagnostic info.
+  //! Should be called within rendering thread.
+  //!
+  //! This API should be used only for user output or for creating automated reports.
+  //! The format of returned information (e.g. key-value layout)
+  //! is NOT part of this API and can be changed at any time.
+  //! Thus application should not parse returned information to weed out specific parameters.
+  Standard_EXPORT virtual void DiagnosticInformation (TColStd_IndexedDataMapOfStringString& theDict,
+                                                      Graphic3d_DiagnosticInfo theFlags) const Standard_OVERRIDE;
 
 public:
 
   //! Returns background color.
-  const TEL_COLOUR& BackgroundColor() const { return myBgColor; }
+  const Quantity_ColorRGBA& BackgroundColor() const { return myBgColor; }
 
   //! Change trihedron.
   OpenGl_Trihedron& ChangeTrihedron() { return myTrihedron; }
@@ -542,8 +456,7 @@ private:
   Standard_EXPORT virtual Standard_Real considerZoomPersistenceObjects (const Graphic3d_ZLayerId        theLayerId,
                                                                         const Handle(Graphic3d_Camera)& theCamera,
                                                                         const Standard_Integer          theWindowWidth,
-                                                                        const Standard_Integer          theWindowHeight,
-                                                                        const Standard_Boolean          theToIgnoreInfiniteFlag) const Standard_OVERRIDE;
+                                                                        const Standard_Integer          theWindowHeight) const Standard_OVERRIDE;
 
 private:
 
@@ -565,14 +478,11 @@ protected:
   Standard_Boolean&        myDeviceLostFlag;
   Standard_Boolean         myWasRedrawnGL;
 
-  Standard_Boolean                myAntiAliasing;
   Standard_Boolean                myCulling;
   Graphic3d_TypeOfShadingModel    myShadingModel;
   Graphic3d_TypeOfBackfacingModel myBackfacing;
-  TEL_COLOUR                      myBgColor;
-  OPENGL_FOG                      myFog;
-  OPENGL_ZCLIP                    myZClip;
-  Graphic3d_SequenceOfHClipPlane  myClipPlanes;
+  Quantity_ColorRGBA              myBgColor;
+  Handle(Graphic3d_SequenceOfHClipPlane) myClipPlanes;
   Handle(Graphic3d_Camera)        myCamera;
   Handle(OpenGl_FrameBuffer)      myFBO;
   Standard_Boolean                myUseGLLight;
@@ -604,6 +514,10 @@ protected:
 
   Handle(OpenGl_Texture) myTextureEnv;
 
+  //! Framebuffers for OpenGL output.
+  Handle(OpenGl_FrameBuffer) myOpenGlFBO;
+  Handle(OpenGl_FrameBuffer) myOpenGlFBO2;
+
 protected: //! @name Rendering properties
 
   //! Two framebuffers (left and right views) store cached main presentation
@@ -660,7 +574,7 @@ protected: //! @name data types related to ray-tracing
     OpenGl_RT_uDirectLB,
     OpenGl_RT_uDirectRT,
     OpenGl_RT_uDirectRB,
-    OpenGl_RT_uViewMat,
+    OpenGl_RT_uViewPrMat,
     OpenGl_RT_uUnviewMat,
 
     // 3D scene params
@@ -681,6 +595,10 @@ protected: //! @name data types related to ray-tracing
     OpenGl_RT_uTexSamplersArray,
     OpenGl_RT_uBlockedRngEnabled,
 
+    // size of render window
+    OpenGl_RT_uWinSizeX,
+    OpenGl_RT_uWinSizeY,
+
     // sampled frame params
     OpenGl_RT_uSampleWeight,
     OpenGl_RT_uFrameRndSeed,
@@ -690,10 +608,14 @@ protected: //! @name data types related to ray-tracing
     OpenGl_RT_uOffsetY,
     OpenGl_RT_uSamples,
 
+    // adaptive path tracing images
+    OpenGl_RT_uRenderImage,
+    OpenGl_RT_uOffsetImage,
+
     OpenGl_RT_NbVariables // special field
   };
 
-  //! Defines texture samplers.
+  //! Defines OpenGL texture samplers.
   enum ShaderSamplerNames
   {
     OpenGl_RT_SceneNodeInfoTexture  = 0,
@@ -713,10 +635,17 @@ protected: //! @name data types related to ray-tracing
 
     OpenGl_RT_FsaaInputTexture = 11,
     OpenGl_RT_PrevAccumTexture = 12,
-    OpenGl_RT_DepthTexture = 13,
 
-    OpenGl_RT_OpenGlColorTexture = 14,
-    OpenGl_RT_OpenGlDepthTexture = 15
+    OpenGl_RT_RaytraceDepthTexture = 13
+  };
+
+  //! Defines OpenGL image samplers.
+  enum ShaderImageNames
+  {
+    OpenGl_RT_OutputImageLft = 0,
+    OpenGl_RT_OutputImageRgh = 1,
+    OpenGl_RT_VisualErrorImage = 2,
+    OpenGl_RT_TileOffsetsImage = 3
   };
 
   //! Tool class for management of shader sources.
@@ -771,7 +700,7 @@ protected: //! @name data types related to ray-tracing
   static const Standard_Integer THE_DEFAULT_NB_BOUNCES = 3;
 
   //! Default size of traversal stack.
-  static const Standard_Integer THE_DEFAULT_STACK_SIZE = 24;
+  static const Standard_Integer THE_DEFAULT_STACK_SIZE = 10;
 
   //! Compile-time ray-tracing parameters.
   struct RaytracingParams
@@ -791,13 +720,17 @@ protected: //! @name data types related to ray-tracing
     //! Enables/disables the use of OpenGL bindless textures.
     Standard_Boolean UseBindlessTextures;
 
+    //! Enables/disables adaptive screen sampling for path tracing.
+    Standard_Boolean AdaptiveScreenSampling;
+
     //! Creates default compile-time ray-tracing parameters.
     RaytracingParams()
     : StackSize (THE_DEFAULT_STACK_SIZE),
       NbBounces (THE_DEFAULT_NB_BOUNCES),
       TransparentShadows (Standard_False),
       GlobalIllumination  (Standard_False),
-      UseBindlessTextures (Standard_False)
+      UseBindlessTextures (Standard_False),
+      AdaptiveScreenSampling (Standard_False)
     {
       //
     }
@@ -851,7 +784,7 @@ protected: //! @name methods related to ray-tracing
   //! Adds OpenGL groups to ray-traced scene geometry.
   Standard_Boolean addRaytraceGroups (const OpenGl_Structure*        theStructure,
                                       const OpenGl_RaytraceMaterial& theStructMat,
-                                      const Graphic3d_Mat4*          theTransform,
+                                      const Handle(Geom_Transformation)& theTrsf,
                                       const Handle(OpenGl_Context)&  theGlContext);
 
   //! Creates ray-tracing material properties.
@@ -958,25 +891,31 @@ protected: //! @name methods related to ray-tracing
   void unbindRaytraceTextures (const Handle(OpenGl_Context)& theGlContext);
 
   //! Sets uniform state for the given ray-tracing shader program.
-  Standard_Boolean setUniformState (const OpenGl_Vec3*            theOrigins,
-                                    const OpenGl_Vec3*            theDirects,
-                                    const OpenGl_Mat4&            theViewMat,
-                                    const OpenGl_Mat4&            theUnviewMat,
-                                    const Standard_Integer        theProgramId,
+  Standard_Boolean setUniformState (const Standard_Integer        theProgramId,
+                                    const Standard_Integer        theSizeX,
+                                    const Standard_Integer        theSizeY,
                                     const Handle(OpenGl_Context)& theGlContext);
 
   //! Runs ray-tracing shader programs.
   Standard_Boolean runRaytraceShaders (const Standard_Integer        theSizeX,
                                        const Standard_Integer        theSizeY,
-                                       const OpenGl_Vec3*            theOrigins,
-                                       const OpenGl_Vec3*            theDirects,
-                                       const OpenGl_Mat4&            theViewMat,
-                                       const OpenGl_Mat4&            theUnviewMat,
                                        Graphic3d_Camera::Projection  theProjection,
                                        OpenGl_FrameBuffer*           theReadDrawFbo,
                                        const Handle(OpenGl_Context)& theGlContext);
 
-  //! Redraws the window using OpenGL/GLSL ray-tracing.
+  //! Runs classical (Whitted-style) ray-tracing kernel.
+  Standard_Boolean runRaytrace (const Standard_Integer        theSizeX,
+                                const Standard_Integer        theSizeY,
+                                Graphic3d_Camera::Projection  theProjection,
+                                OpenGl_FrameBuffer*           theReadDrawFbo,
+                                const Handle(OpenGl_Context)& theGlContext);
+
+  //! Runs path tracing (global illumination) kernel.
+  Standard_Boolean runPathtrace (const Graphic3d_Camera::Projection theProjection,
+                                 OpenGl_FrameBuffer*                theReadDrawFbo,
+                                 const Handle(OpenGl_Context)&      theGlContext);
+
+  //! Redraws the window using OpenGL/GLSL ray-tracing or path tracing.
   Standard_Boolean raytrace (const Standard_Integer        theSizeX,
                              const Standard_Integer        theSizeY,
                              Graphic3d_Camera::Projection  theProjection,
@@ -985,13 +924,13 @@ protected: //! @name methods related to ray-tracing
 
 protected: //! @name fields related to ray-tracing
 
-  //! Result of shaders initialization.
+  //! Result of RT/PT shaders initialization.
   RaytraceInitStatus myRaytraceInitStatus;
 
-  //! Is geometry data valid?
+  //! Is ray-tracing geometry data valid?
   Standard_Boolean myIsRaytraceDataValid;
 
-  //! Warning about missing extension GL_ARB_bindless_texture has been displayed?
+  //! True if warning about missing extension GL_ARB_bindless_texture has been displayed.
   Standard_Boolean myIsRaytraceWarnTextures;
 
   //! 3D scene geometry data for ray-tracing.
@@ -1009,11 +948,15 @@ protected: //! @name fields related to ray-tracing
   ShaderSource myRaytraceShaderSource;
   //! OpenGL/GLSL source of adaptive-AA fragment shader.
   ShaderSource myPostFSAAShaderSource;
+  //! OpenGL/GLSL source of RT/PT display fragment shader.
+  ShaderSource myOutImageShaderSource;
 
   //! OpenGL/GLSL ray-tracing fragment shader.
   Handle(OpenGl_ShaderObject) myRaytraceShader;
   //! OpenGL/GLSL adaptive-AA fragment shader.
   Handle(OpenGl_ShaderObject) myPostFSAAShader;
+  //! OpenGL/GLSL ray-tracing display fragment shader.
+  Handle(OpenGl_ShaderObject) myOutImageShader;
 
   //! OpenGL/GLSL ray-tracing shader program.
   Handle(OpenGl_ShaderProgram) myRaytraceProgram;
@@ -1046,12 +989,22 @@ protected: //! @name fields related to ray-tracing
   Handle(OpenGl_TextureBufferArb) myRaytraceLightSrcTexture;
 
   //! 1st framebuffer (FBO) to perform adaptive FSAA.
+  //! Used in compatibility mode (no adaptive sampling).
   Handle(OpenGl_FrameBuffer) myRaytraceFBO1[2];
   //! 2nd framebuffer (FBO) to perform adaptive FSAA.
+  //! Used in compatibility mode (no adaptive sampling).
   Handle(OpenGl_FrameBuffer) myRaytraceFBO2[2];
-  //! Framebuffer (FBO) for preliminary OpenGL output.
-  Handle(OpenGl_FrameBuffer) myOpenGlFBO;
-  Handle(OpenGl_FrameBuffer) myOpenGlFBO2;
+
+  //! Output textures (2 textures are used in stereo mode).
+  //! Used if adaptive screen sampling is activated.
+  Handle(OpenGl_Texture) myRaytraceOutputTexture[2];
+
+  //! Texture containing per-tile visual error estimation.
+  //! Used if adaptive screen sampling is activated.
+  Handle(OpenGl_Texture) myRaytraceVisualErrorTexture;
+  //! Texture containing offsets of sampled screen tiles.
+  //! Used if adaptive screen sampling is activated.
+  Handle(OpenGl_Texture) myRaytraceTileOffsetsTexture;
 
   //! Vertex buffer (VBO) for drawing dummy quad.
   OpenGl_VertexBuffer myRaytraceScreenQuad;
@@ -1086,6 +1039,9 @@ protected: //! @name fields related to ray-tracing
   //! Bullard RNG to produce random sequence.
   math_BullardGenerator myRNG;
 
+  //! Tool object for sampling screen tiles in PT mode.
+  OpenGl_TileSampler myTileSampler;
+
 public:
 
   DEFINE_STANDARD_ALLOC