0024023: Revamp the OCCT Handle -- general
[occt.git] / src / OpenGl / OpenGl_Workspace.hxx
old mode 100755 (executable)
new mode 100644 (file)
index dac1880..aa1bca9
 #ifndef _OpenGl_Workspace_Header
 #define _OpenGl_Workspace_Header
 
-#ifdef HAVE_OPENCL
-  #include <map>
-  #include <set>
+#include <map>
+#include <set>
 
-  #include <OpenGl_Cl.hxx>
-#endif
-
-#include <Handle_OpenGl_Workspace.hxx>
 #include <OpenGl_Window.hxx>
 
 #include <TColStd_Array2OfReal.hxx>
 #include <Graphic3d_TypeOfTexture.hxx>
 #include <Graphic3d_PtrFrameBuffer.hxx>
 #include <Graphic3d_BufferType.hxx>
-#include <Handle_Graphic3d_TextureParams.hxx>
 
 #include <Aspect_CLayer2d.hxx>
 #include <Aspect_Handle.hxx>
 #include <Aspect_PrintAlgo.hxx>
+#include <Aspect_PolygonOffsetMode.hxx>
 
 #include <InterfaceGraphic_Graphic3d.hxx>
 #include <InterfaceGraphic_Visual3d.hxx>
 #include <NCollection_Sequence.hxx>
 
 #include <OpenGl_AspectFace.hxx>
-#include <OpenGl_Display.hxx>
+#include <OpenGl_FrameBuffer.hxx>
 #include <OpenGl_Matrix.hxx>
 #include <OpenGl_NamedStatus.hxx>
 #include <OpenGl_PrinterContext.hxx>
-#ifdef HAVE_OPENCL
-  #include <OpenGl_SceneGeometry.hxx>
-#endif
+#include <OpenGl_SceneGeometry.hxx>
 #include <OpenGl_TextParam.hxx>
 #include <OpenGl_RenderFilter.hxx>
 #include <OpenGl_Vec.hxx>
+#include <OpenGl_LineAttributes.hxx>
+#include <OpenGl_CappingAlgo.hxx>
+
 
-#include <Handle_OpenGl_View.hxx>
-#include <Handle_OpenGl_Texture.hxx>
+#include <OpenGl_ShaderObject.hxx>
+#include <OpenGl_ShaderProgram.hxx>
+#include <OpenGl_TextureBufferArb.hxx>
 
 class OpenGl_AspectLine;
 class OpenGl_AspectMarker;
 class OpenGl_AspectText;
 class OpenGl_FrameBuffer;
 class OpenGl_Structure;
+class OpenGl_TriangleSet;
 class OpenGl_Element;
 class Image_PixMap;
 
@@ -92,6 +90,47 @@ struct OpenGl_Material
 
 };
 
+class OpenGl_RaytraceFilter;
+DEFINE_STANDARD_HANDLE (OpenGl_RaytraceFilter, OpenGl_RenderFilter)
+
+//! Graphical ray-tracing filter.
+//! Filters out all raytracable structures.
+class OpenGl_RaytraceFilter : public OpenGl_RenderFilter
+{
+public:
+
+  //! Default constructor.
+  OpenGl_RaytraceFilter() {}
+
+  //! Returns the previously set filter.
+  const Handle(OpenGl_RenderFilter)& PrevRenderFilter()
+  {
+    return myPrevRenderFilter;
+  }
+
+  //! Remembers the previously set filter.
+  void SetPrevRenderFilter (const Handle(OpenGl_RenderFilter)& theFilter)
+  {
+    myPrevRenderFilter = theFilter;
+  }
+
+  //! Checks whether the element can be rendered or not.
+  //! @param theElement [in] the element to check.
+  //! @return True if element can be rendered.
+  virtual Standard_Boolean CanRender (const OpenGl_Element* theElement);
+
+private:
+
+  Handle(OpenGl_RenderFilter) myPrevRenderFilter;
+
+public:
+
+  DEFINE_STANDARD_RTTI(OpenGl_RaytraceFilter, OpenGl_RenderFilter)
+};
+
+class OpenGl_Workspace;
+DEFINE_STANDARD_HANDLE(OpenGl_Workspace,OpenGl_Window)
+
 //! Represents window with GL context.
 //! Provides methods to render primitives and maintain GL state.
 class OpenGl_Workspace : public OpenGl_Window
@@ -99,7 +138,7 @@ class OpenGl_Workspace : public OpenGl_Window
 public:
 
   //! Main constructor - prepare GL context for specified window.
-  OpenGl_Workspace (const Handle(OpenGl_Display)& theDisplay,
+  OpenGl_Workspace (const Handle(OpenGl_GraphicDriver)& theDriver,
                     const CALL_DEF_WINDOW&        theCWindow,
                     Aspect_RenderingContext       theGContext,
                     const Handle(OpenGl_Caps)&    theCaps,
@@ -108,8 +147,16 @@ public:
   //! Destructor
   virtual ~OpenGl_Workspace();
 
-  void SetActiveView (const Handle(OpenGl_View)& theView) { myView = theView; }
-  const Handle(OpenGl_View)& ActiveView () const { return myView; }
+  void SetActiveView (const Handle(OpenGl_View)& theView,
+                      const Standard_Integer     theViewId)
+  {
+    myView   = theView;
+    myViewId = theViewId;
+  }
+
+  const Handle(OpenGl_View)& ActiveView() const { return myView; }
+
+  Standard_Integer ActiveViewId() const { return myViewId; }
 
   //! Redraw the window.
   void Redraw (const Graphic3d_CView& theCView,
@@ -119,8 +166,7 @@ public:
   Standard_Boolean SetImmediateModeDrawToFront (const Standard_Boolean theDrawToFrontBuffer);
   void RedrawImmediate (const Graphic3d_CView& theCView,
                         const Aspect_CLayer2d& theCUnderLayer,
-                        const Aspect_CLayer2d& theCOverLayer,
-                        const Standard_Boolean theToForce = Standard_False);
+                        const Aspect_CLayer2d& theCOverLayer);
 
   void Invalidate (const Graphic3d_CView& /*theCView*/)
   {
@@ -152,10 +198,20 @@ public:
                                Image_PixMap&               theImage,
                                const Graphic3d_BufferType& theBufferType);
 
-  void UseTransparency (const Standard_Boolean theFlag);
-  Standard_Boolean& UseZBuffer()   { return myUseZBuffer; }
-  Standard_Boolean& UseDepthTest() { return myUseDepthTest; }
-  Standard_Boolean& UseGLLight()   { return myUseGLLight; }
+  //! Setup Z-buffer usage flag (without affecting GL state!).
+  //! Returns previously set flag.
+  Standard_Boolean SetUseZBuffer (const Standard_Boolean theToUse)
+  {
+    const Standard_Boolean wasUsed = myUseZBuffer;
+    myUseZBuffer = theToUse;
+    return wasUsed;
+  }
+
+  Standard_Boolean& UseZBuffer()    { return myUseZBuffer; }
+  Standard_Boolean& UseDepthWrite() { return myUseDepthWrite; }
+  Standard_Boolean& UseGLLight()    { return myUseGLLight; }
+
+  Standard_Integer AntiAliasingMode() const { return myAntiAliasingMode; }
 
   //// RELATED TO STATUS ////
 
@@ -163,13 +219,6 @@ public:
 
   const TEL_COLOUR* HighlightColor;
 
-  const OpenGl_Matrix* SetViewMatrix (const OpenGl_Matrix* );
-  const OpenGl_Matrix* SetStructureMatrix (const OpenGl_Matrix*, bool aRevert = false);
-
-  //! Updates current model-view matrix
-  //! replacing it with StructureMatrixT*ViewMatrix from the workspace.
-  const void UpdateModelViewMatrix();
-
   const OpenGl_AspectLine*   SetAspectLine   (const OpenGl_AspectLine*   theAspect);
   const OpenGl_AspectFace*   SetAspectFace   (const OpenGl_AspectFace*   theAspect);
   const OpenGl_AspectMarker* SetAspectMarker (const OpenGl_AspectMarker* theAspect);
@@ -193,6 +242,7 @@ public:
   Standard_EXPORT Handle(OpenGl_Texture) DisableTexture();
   Standard_EXPORT Handle(OpenGl_Texture) EnableTexture (const Handle(OpenGl_Texture)&          theTexture,
                                                         const Handle(Graphic3d_TextureParams)& theParams = NULL);
+  const Handle(OpenGl_Texture)& ActiveTexture() const { return myTextureBound; }
 
   //! Set filter for restricting rendering of particular elements.
   //! Filter can be applied for rendering passes used by recursive
@@ -216,211 +266,108 @@ public:
   //! @return applied model structure matrix.
   inline const OpenGl_Matrix* ModelMatrix() const { return StructureMatrix_applied; }
 
+  //! Sets and applies current polygon offset.
+  void SetPolygonOffset (int theMode, Standard_ShortReal theFactor, Standard_ShortReal theUnits);
+
+  //! Returns currently applied polygon offset params.
+  const TEL_POFFSET_PARAM& AppliedPolygonOffset() { return PolygonOffset_applied; }
+
+  //! @return true if clipping algorithm enabled
+  inline Standard_Boolean IsCullingEnabled() const { return myIsCullingEnabled; }
+
+  //! Returns capping algorithm rendering filter.
+  const Handle(OpenGl_CappingAlgoFilter)& DefaultCappingAlgoFilter() const
+  {
+    return myDefaultCappingAlgoFilter;
+  }
+
+  //! Returns face aspect for none culling mode.
+  const OpenGl_AspectFace& NoneCulling() const
+  {
+    return myNoneCulling;
+  }
+
+  //! Returns face aspect for front face culling mode.
+  const OpenGl_AspectFace& FrontCulling() const
+  {
+    return myFrontCulling;
+  }
+
 protected:
 
   //! Copy content of Back buffer to the Front buffer
   void copyBackToFront();
 
+  //! Blit image from/to specified buffers.
+  bool blitBuffers (OpenGl_FrameBuffer* theReadFbo,
+                    OpenGl_FrameBuffer* theDrawFbo);
+
   virtual Standard_Boolean Activate();
 
-  void redraw1 (const Graphic3d_CView& theCView,
-                const Aspect_CLayer2d& theCUnderLayer,
-                const Aspect_CLayer2d& theCOverLayer,
-                const int              theToSwap);
+  void redraw1 (const Graphic3d_CView&               theCView,
+                const Aspect_CLayer2d&               theCUnderLayer,
+                const Aspect_CLayer2d&               theCOverLayer,
+                OpenGl_FrameBuffer*                  theReadDrawFbo,
+                const Graphic3d_Camera::Projection   theProjection);
+
+  //! Setup default FBO.
+  void bindDefaultFbo (OpenGl_FrameBuffer* theCustomFbo = NULL);
+
+  //! Blend together views pair into stereo image.
+  void drawStereoPair (const Graphic3d_CView& theCView);
+
+  //! Blit snapshot containing main scene (myMainSceneFbos or BackBuffer)
+  //! into presentation buffer (myMainSceneFbos -> offscreen FBO or myMainSceneFbos -> BackBuffer or BackBuffer -> FrontBuffer),
+  //! and redraw immediate structures on top.
+  //!
+  //! When scene caching is disabled (myTransientDrawToFront, no double buffer in window, etc.),
+  //! the first step (blitting) will be skipped.
+  //!
+  //! @return false if immediate structures has been rendered directly into FrontBuffer and Buffer Swap should not be called.
+  bool redrawImmediate (const Graphic3d_CView& theCView,
+                        const Aspect_CLayer2d& theCUnderLayer,
+                        const Aspect_CLayer2d& theCOverLayer,
+                        OpenGl_FrameBuffer*    theReadFbo,
+                        const Graphic3d_Camera::Projection theProjection,
+                        OpenGl_FrameBuffer*    theDrawFbo,
+                        const Standard_Boolean theIsPartialUpdate = Standard_False);
 
   void updateMaterial (const int theFlag);
 
   void setTextureParams (Handle(OpenGl_Texture)&                theTexture,
                          const Handle(Graphic3d_TextureParams)& theParams);
 
-#ifdef HAVE_OPENCL
-
-public:
-
-  //! Returns information about OpenCL device used for computations.
-  Standard_Boolean GetOpenClDeviceInfo (
-    NCollection_DataMap<TCollection_AsciiString, TCollection_AsciiString>& theInfo) const;
-
-protected:
-
-  //! Describes result of OpenCL initializing.
-  enum OpenClInitStatus
-  {
-    OpenGl_CLIS_NONE,
-    OpenGl_CLIS_INIT,
-    OpenGl_CLIS_FAIL
-  };
-
-protected: //! @name methods related to ray-tracing
+protected: //! @name protected fields
 
-  //! Updates 3D scene geometry for ray-tracing.
-  Standard_Boolean UpdateRaytraceGeometry (Standard_Boolean theCheck);
+  //! Two framebuffers (left and right views) store cached main presentation
+  //! of the view (without presentation of immediate layers).
+  Handle(OpenGl_FrameBuffer) myMainSceneFbos[2];
+  //! Additional buffers for immediate layer in stereo mode.
+  Handle(OpenGl_FrameBuffer) myImmediateSceneFbos[2];
+  //! Special flag which is invalidated when myMainSceneFbos can not be blitted for some reason (e.g. driver bugs).
+  Standard_Boolean           myHasFboBlit;
 
-  //! Checks to see if the structure is modified.
-  Standard_Boolean CheckRaytraceStructure (const OpenGl_Structure* theStructure);
-
-  //! Updates 3D scene light sources for ray-tracing.
-  Standard_Boolean UpdateRaytraceLightSources (const GLdouble theInvModelView[16]);
-
-  //! Updates environment map for ray-tracing.
-  Standard_Boolean UpdateRaytraceEnvironmentMap();
-
-  //! Adds OpenGL structure to ray-traced scene geometry.
-  Standard_Boolean AddRaytraceStructure (const OpenGl_Structure* theStructure,
-    const Standard_ShortReal* theTransform, std::set<const OpenGl_Structure*>& theElements);
-
-  //! Adds OpenGL primitive array to ray-traced scene geometry.
-  OpenGl_TriangleSet* AddRaytracePrimitiveArray (
-    const CALL_DEF_PARRAY* theArray, int theMatID, const Standard_ShortReal* theTrans);
-
-  //! Adds vertex indices from OpenGL primitive array to ray-traced scene geometry.
-  Standard_Boolean AddRaytraceVertexIndices (OpenGl_TriangleSet* theSet,
-    const CALL_DEF_PARRAY* theArray, Standard_Integer theOffset, Standard_Integer theCount, Standard_Integer theMatID);
-
-  //! Adds OpenGL triangle array to ray-traced scene geometry.
-  Standard_Boolean AddRaytraceTriangleArray (OpenGl_TriangleSet* theSet,
-    const CALL_DEF_PARRAY* theArray, Standard_Integer theOffset, Standard_Integer theCount, Standard_Integer theMatID);
-
-  //! Adds OpenGL triangle fan array to ray-traced scene geometry.
-  Standard_Boolean AddRaytraceTriangleFanArray (OpenGl_TriangleSet* theSet,
-    const CALL_DEF_PARRAY* theArray, Standard_Integer theOffset, Standard_Integer theCount, Standard_Integer theMatID);
-
-  //! Adds OpenGL triangle strip array to ray-traced scene geometry.
-  Standard_Boolean AddRaytraceTriangleStripArray (OpenGl_TriangleSet* theSet,
-    const CALL_DEF_PARRAY* theArray, Standard_Integer theOffset, Standard_Integer theCount, Standard_Integer theMatID);
-
-  //! Adds OpenGL quadrangle array to ray-traced scene geometry.
-  Standard_Boolean AddRaytraceQuadrangleArray (OpenGl_TriangleSet* theSet,
-    const CALL_DEF_PARRAY* theArray, Standard_Integer theOffset, Standard_Integer theCount, Standard_Integer theMatID);
-
-  //! Adds OpenGL quadrangle strip array to ray-traced scene geometry.
-  Standard_Boolean AddRaytraceQuadrangleStripArray (OpenGl_TriangleSet* theSet,
-    const CALL_DEF_PARRAY* theArray, Standard_Integer theOffset, Standard_Integer theCount, Standard_Integer theMatID);
-
-  //! Adds OpenGL polygon array to ray-traced scene geometry.
-  Standard_Boolean AddRaytracePolygonArray (OpenGl_TriangleSet* theSet,
-    const CALL_DEF_PARRAY* theArray, Standard_Integer theOffset, Standard_Integer theCount, Standard_Integer theMatID);
-
-  //! Initializes OpenCL resources.
-  Standard_Boolean InitOpenCL();
-
-  //! Releases OpenCL resources.
-  void ReleaseOpenCL();
-
-  //! Resizes OpenCL output image.
-  Standard_Boolean ResizeRaytraceOutputBuffer (const cl_int theSizeX, const cl_int theSizeY);
-
-  //! Writes scene geometry to OpenCl device.
-  Standard_Boolean WriteRaytraceSceneToDevice();
-
-  //! Runs OpenCL ray-tracing kernels.
-  Standard_Boolean RunRaytraceOpenCLKernelsOld (const Graphic3d_CView& theCView,
-                                             const GLfloat theOrigins[16],
-                                             const GLfloat theDirects[16],
-                                             const int theSizeX,
-                                             const int theSizeY);
-
-  //! Launches OpenCL ray-tracing kernels.
-  Standard_Boolean RunRaytraceOpenCLKernels (const Graphic3d_CView&   theCView,
-                                             const Standard_ShortReal theOrigins[16],
-                                             const Standard_ShortReal theDirects[16],
-                                             const Standard_Integer   theSizeX,
-                                             const Standard_Integer   theSizeY);
-
-  //! Redraws the window using OpenCL ray tracing.
-  Standard_Boolean Raytrace (const Graphic3d_CView& theCView,
-              const int theSizeX, const int theSizeY, const Tint theToSwap);
-
-protected: //! @name fields related to ray-tracing
-
-  //! Result of OpenCL initialization.
-  OpenClInitStatus myComputeInitStatus;
-  //! Is ATI/AMD OpenCL platform used?
-  Standard_Boolean myIsAmdComputePlatform;
-
-  //! Is geometry data valid?
-  Standard_Boolean myIsRaytraceDataValid;
-  //! Is geometry data musty be updated?
-  Standard_Boolean myToUpdateRaytraceData;
-
-  //! 3D scene geometry data for ray-tracing.
-  OpenGl_RaytraceGeometry myRaytraceGeometry;
-
-  //! Radius of bounding sphere of the scene.
-  Standard_ShortReal myRaytraceSceneRadius;
-  //! Scene epsilon to prevent self-intersections.
-  Standard_ShortReal myRaytraceSceneEpsilon;
-
-  //! OpenCL context.
-  cl_context myComputeContext;
-  //! OpenCL command queue.
-  cl_command_queue myComputeQueue;
-  //! OpenCL computing program.
-  cl_program myRaytraceProgram;
-  //! OpenCL ray-tracing render kernel.
-  cl_kernel myRaytraceRenderKernel;
-  //! OpenCL adaptive anti-aliasing kernel.
-  cl_kernel myRaytraceSmoothKernel;
-
-  //! OpenCL image to store environment map.
-  cl_mem myRaytraceEnvironment;
-  //! OpenCL image to store rendering result.
-  cl_mem myRaytraceOutputImage;
-  //! OpenCL image to store anti-aliasing result.
-  cl_mem myRaytraceOutputImageAA;
-
-  //! OpenGL texture to store rendering result.
-  Handle(OpenGl_Texture) myRaytraceOutputTexture;
-  //! OpenGL texture to store anti-aliasing result.
-  Handle(OpenGl_Texture) myRaytraceOutputTextureAA;
-
-  //! OpenCL buffer of material properties.
-  cl_mem myRaytraceMaterialBuffer;
-  //! OpenCL buffer of light source properties.
-  cl_mem myRaytraceLightSourceBuffer;
-
-  //! OpenCL buffer of vertex coords.
-  cl_mem myGeometryVertexBuffer;
-  //! OpenCL buffer of vertex normals.
-  cl_mem myGeometryNormalBuffer;
-  //! OpenCL buffer of triangle indices.
-  cl_mem myGeometryTriangBuffer;
-
-  //! OpenCL buffer of data records of high-level BVH nodes.
-  cl_mem mySceneNodeInfoBuffer;
-  //! OpenCL buffer of minimum points of high-level BVH nodes.
-  cl_mem mySceneMinPointBuffer;
-  //! OpenCL buffer of maximum points of high-level BVH nodes.
-  cl_mem mySceneMaxPointBuffer;
-
-  //! OpenCL buffer of data records of bottom-level BVH nodes.
-  cl_mem myObjectNodeInfoBuffer;
-  //! OpenCL buffer of minimum points of bottom-level BVH nodes.
-  cl_mem myObjectMinPointBuffer;
-  //! OpenCL buffer of maximum points of bottom-level BVH nodes.
-  cl_mem myObjectMaxPointBuffer;
-
-  //! State of OpenGL view.
-  Standard_Size myViewModificationStatus;
-  //! State of OpenGL layer list.
-  Standard_Size myLayersModificationStatus;
-
-  //! State of OpenGL structures reflected to ray-tracing.
-  std::map<const OpenGl_Structure*, Standard_Size> myStructureStates;
-
-#endif // HAVE_OPENCL
-
-protected: //! @name protected fields
+  //! Vertices for full-screen quad rendering.
+  OpenGl_VertexBuffer        myFullScreenQuad;
 
   Handle(OpenGl_PrinterContext) myPrintContext;
-  Handle(OpenGl_View)    myView;            // WSViews - now just one view is supported
+  Handle(OpenGl_View)           myView;
+  Handle(OpenGl_LineAttributes) myLineAttribs;
+  Standard_Integer       myViewId;
+  Standard_Integer       myAntiAliasingMode;
   Standard_Boolean       myTransientDrawToFront; //!< optimization flag for immediate mode (to render directly to the front buffer)
   Standard_Boolean       myBackBufferRestored;
   Standard_Boolean       myIsImmediateDrawn;     //!< flag indicates that immediate mode buffer contains some data
-  Standard_Boolean       myUseTransparency;
   Standard_Boolean       myUseZBuffer;
-  Standard_Boolean       myUseDepthTest;
+  Standard_Boolean       myUseDepthWrite;
   Standard_Boolean       myUseGLLight;
+  Standard_Boolean       myIsCullingEnabled;     //!< frustum culling flag
+
+  unsigned int           myFrameCounter;         //!< redraw counter, for debugging
+
+  Handle(OpenGl_CappingAlgoFilter) myDefaultCappingAlgoFilter;
+  OpenGl_AspectFace                myNoneCulling;
+  OpenGl_AspectFace                myFrontCulling;
 
 protected: //! @name fields related to status
 
@@ -441,16 +388,15 @@ protected: //! @name fields related to status
   OpenGl_Material myMatTmp;      //!< temporary variable
   TelCullMode     myCullingMode; //!< back face culling mode, applied from face aspect
 
-  //! Model matrix with applied structure transformations
-  OpenGl_Matrix myModelViewMatrix;
+  OpenGl_Matrix myModelViewMatrix; //!< Model matrix with applied structure transformations
 
-  const TEL_POFFSET_PARAM* PolygonOffset_applied;
+  TEL_POFFSET_PARAM PolygonOffset_applied; //!< Currently applied polygon offset.
 
-  OpenGl_AspectFace myAspectFaceHl; // Hiddenline aspect
+  OpenGl_AspectFace myAspectFaceHl; //!< Hiddenline aspect
 
 public: //! @name type definition
 
-  DEFINE_STANDARD_RTTI(OpenGl_Workspace)
+  DEFINE_STANDARD_RTTI(OpenGl_Workspace, OpenGl_Window)
   DEFINE_STANDARD_ALLOC
 
 };