0028625: Visualization, OpenGl_FrameBuffer - initialize Render Buffer with stencil
authorkgv <kgv@opencascade.com>
Mon, 3 Apr 2017 19:53:17 +0000 (22:53 +0300)
committerbugmaster <bugmaster@opencascade.com>
Wed, 12 Apr 2017 14:53:27 +0000 (17:53 +0300)
src/OpenGl/OpenGl_Context.cxx
src/OpenGl/OpenGl_FrameBuffer.cxx

index abcf441..603157d 100644 (file)
@@ -1213,7 +1213,8 @@ void OpenGl_Context::init (const Standard_Boolean theIsCoreProfile)
              || CheckExtension ("GL_EXT_texture_rg");
   extBgra     = CheckExtension ("GL_EXT_texture_format_BGRA8888");
   extAnis = CheckExtension ("GL_EXT_texture_filter_anisotropic");
-  extPDS  = CheckExtension ("GL_OES_packed_depth_stencil");
+  extPDS  = IsGlGreaterEqual (3, 0)
+         || CheckExtension ("GL_OES_packed_depth_stencil");
 
   core11fwd = (OpenGl_GlCore11Fwd* )(&(*myFuncs));
   if (IsGlGreaterEqual (2, 0))
index 6f686a4..30b1284 100644 (file)
@@ -129,6 +129,7 @@ Standard_Boolean OpenGl_FrameBuffer::Init (const Handle(OpenGl_Context)& theGlCo
   myVPSizeY = theSizeY;
   const Standard_Integer aSizeX = theSizeX > 0 ? theSizeX : 2;
   const Standard_Integer aSizeY = theSizeY > 0 ? theSizeY : 2;
+  bool hasStencilRB = false;
 
   // Create the textures (will be used as color buffer and depth-stencil buffer)
   if (theNbSamples != 0)
@@ -175,9 +176,15 @@ Standard_Boolean OpenGl_FrameBuffer::Init (const Handle(OpenGl_Context)& theGlCo
                                  GL_DEBUG_SEVERITY_HIGH,
                                  aMsg);
 
+      hasStencilRB = aPixelFormat == GL_DEPTH_STENCIL
+                  && theGlContext->extPDS;
+      GLint aDepthStencilFormat = hasStencilRB
+                                ? GL_DEPTH24_STENCIL8
+                                : GL_DEPTH_COMPONENT16;
+
       theGlContext->arbFBO->glGenRenderbuffers (1, &myGlDepthRBufferId);
       theGlContext->arbFBO->glBindRenderbuffer (GL_RENDERBUFFER, myGlDepthRBufferId);
-      theGlContext->arbFBO->glRenderbufferStorage (GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, aSizeX, aSizeY);
+      theGlContext->arbFBO->glRenderbufferStorage (GL_RENDERBUFFER, aDepthStencilFormat, aSizeX, aSizeY);
       theGlContext->arbFBO->glBindRenderbuffer (GL_RENDERBUFFER, NO_RENDERBUFFER);
     }
   }
@@ -204,8 +211,18 @@ Standard_Boolean OpenGl_FrameBuffer::Init (const Handle(OpenGl_Context)& theGlCo
   }
   else if (myGlDepthRBufferId != NO_RENDERBUFFER)
   {
+  #ifdef GL_DEPTH_STENCIL_ATTACHMENT
+    theGlContext->arbFBO->glFramebufferRenderbuffer (GL_FRAMEBUFFER, hasStencilRB ? GL_DEPTH_STENCIL_ATTACHMENT : GL_DEPTH_ATTACHMENT,
+                                                     GL_RENDERBUFFER, myGlDepthRBufferId);
+  #else
     theGlContext->arbFBO->glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
                                                      GL_RENDERBUFFER, myGlDepthRBufferId);
+    if (hasStencilRB)
+    {
+      theGlContext->arbFBO->glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
+                                                       GL_RENDERBUFFER, myGlDepthRBufferId);
+    }
+  #endif
   }
   if (theGlContext->arbFBO->glCheckFramebufferStatus (GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
   {
@@ -282,8 +299,14 @@ Standard_Boolean OpenGl_FrameBuffer::InitWithRB (const Handle(OpenGl_Context)& t
     theGlCtx->arbFBO->glRenderbufferStorage (GL_RENDERBUFFER, myColorFormat, aSizeX, aSizeY);
   }
 
+  bool hasStencilRB = false;
   if (myDepthFormat != 0)
   {
+    GLenum aPixelFormat = 0;
+    GLenum aDataType    = 0;
+    getDepthDataFormat (myDepthFormat, aPixelFormat, aDataType);
+    hasStencilRB = aPixelFormat == GL_DEPTH_STENCIL;
+
     theGlCtx->arbFBO->glGenRenderbuffers (1, &myGlDepthRBufferId);
     theGlCtx->arbFBO->glBindRenderbuffer (GL_RENDERBUFFER, myGlDepthRBufferId);
     theGlCtx->arbFBO->glRenderbufferStorage (GL_RENDERBUFFER, myDepthFormat, aSizeX, aSizeY);
@@ -298,13 +321,16 @@ Standard_Boolean OpenGl_FrameBuffer::InitWithRB (const Handle(OpenGl_Context)& t
   if (myGlDepthRBufferId != NO_RENDERBUFFER)
   {
   #ifdef GL_DEPTH_STENCIL_ATTACHMENT
-    theGlCtx->arbFBO->glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
+    theGlCtx->arbFBO->glFramebufferRenderbuffer (GL_FRAMEBUFFER, hasStencilRB ? GL_DEPTH_STENCIL_ATTACHMENT : GL_DEPTH_ATTACHMENT,
                                                  GL_RENDERBUFFER, myGlDepthRBufferId);
   #else
     theGlCtx->arbFBO->glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
                                                  GL_RENDERBUFFER, myGlDepthRBufferId);
-    theGlCtx->arbFBO->glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
-                                                 GL_RENDERBUFFER, myGlDepthRBufferId);
+    if (hasStencilRB)
+    {
+      theGlCtx->arbFBO->glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
+                                                   GL_RENDERBUFFER, myGlDepthRBufferId);
+    }
   #endif
   }
   if (theGlCtx->arbFBO->glCheckFramebufferStatus (GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)