1 // Author: Ilya Khramov
2 // Copyright (c) 2019 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
15 #include <OpenGl_PBREnvironment.hxx>
17 #include <Graphic3d_PBRMaterial.hxx>
18 #include <OpenGl_ArbFBO.hxx>
19 #include <OpenGl_FrameBuffer.hxx>
20 #include <OpenGl_ShaderManager.hxx>
21 #include <OSD_Timer.hxx>
22 #include <Message.hxx>
23 #include <Message_Messenger.hxx>
27 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_PBREnvironment, OpenGl_NamedResource)
29 //! Constructor of this class saves necessary OpenGL states components which can be changed by OpenGl_PBREnvironment.
30 //! Destructor restores state back.
31 class OpenGl_PBREnvironmentSentry
35 OpenGl_PBREnvironmentSentry (const Handle(OpenGl_Context)& theCtx)
42 ~OpenGl_PBREnvironmentSentry()
51 myContext->core11fwd->glGetIntegerv (GL_FRAMEBUFFER_BINDING, &myFBO);
52 myShaderProgram = myContext->ActiveProgram();
53 for (unsigned int i = 0; i < 4; ++i)
55 myViewport[i] = myContext->Viewport()[i];
57 myContext->core11fwd->glGetFloatv (GL_COLOR_CLEAR_VALUE, myClearColor);
59 GLboolean aStatus = GL_TRUE;
60 myContext->core11fwd->glGetBooleanv (GL_DEPTH_TEST, &aStatus);
61 myDepthTestWasEnabled = aStatus ? Standard_True : Standard_False;
62 myContext->core11fwd->glGetBooleanv (GL_DEPTH_WRITEMASK, &aStatus);
63 myDepthWrirtingWasEnablig = aStatus ? Standard_True : Standard_False;
64 myContext->core11fwd->glGetBooleanv (GL_SCISSOR_TEST, &aStatus);
65 myScissorTestWasEnabled = aStatus ? Standard_True : Standard_False;
66 myContext->core11fwd->glGetIntegerv (GL_SCISSOR_BOX, myScissorBox);
71 myContext->BindDefaultVao();
72 myContext->core11fwd->glDisable (GL_DEPTH_TEST);
73 myContext->core11fwd->glDepthMask (GL_FALSE);
74 myContext->core11fwd->glDisable (GL_BLEND);
75 myContext->core11fwd->glDisable (GL_SCISSOR_TEST);
80 myContext->arbFBO->glBindFramebuffer (GL_DRAW_FRAMEBUFFER, myFBO);
81 myContext->BindProgram (myShaderProgram);
82 myContext->ResizeViewport (myViewport);
83 myContext->core11fwd->glClearColor (myClearColor.r(), myClearColor.g(), myClearColor.b(), myClearColor.a());
84 if (myDepthTestWasEnabled)
86 myContext->core11fwd->glEnable (GL_DEPTH_TEST);
90 myContext->core11fwd->glDisable (GL_DEPTH_TEST);
92 myContext->core11fwd->glDepthMask (myDepthWrirtingWasEnablig ? GL_TRUE : GL_FALSE);
93 if (myScissorTestWasEnabled)
95 myContext->core11fwd->glEnable (GL_SCISSOR_TEST);
99 myContext->core11fwd->glDisable (GL_SCISSOR_TEST);
101 myContext->core11fwd->glScissor (myScissorBox[0], myScissorBox[1], myScissorBox[2], myScissorBox[3]);
106 OpenGl_PBREnvironmentSentry (const OpenGl_PBREnvironmentSentry& );
107 OpenGl_PBREnvironmentSentry& operator= (const OpenGl_PBREnvironmentSentry& );
111 const Handle(OpenGl_Context) myContext;
113 Handle(OpenGl_ShaderProgram) myShaderProgram;
114 Standard_Boolean myDepthTestWasEnabled;
115 Standard_Boolean myDepthWrirtingWasEnablig;
116 Standard_Boolean myScissorTestWasEnabled;
117 Standard_Integer myScissorBox[4];
118 Standard_Integer myViewport[4];
119 Graphic3d_Vec4 myClearColor;
123 // =======================================================================
126 // =======================================================================
127 Handle(OpenGl_PBREnvironment) OpenGl_PBREnvironment::Create (const Handle(OpenGl_Context)& theCtx,
128 unsigned int thePow2Size,
129 unsigned int theLevelsNumber,
130 const TCollection_AsciiString& theId)
132 if (theCtx->arbFBO == NULL)
134 return Handle(OpenGl_PBREnvironment)();
137 Handle(OpenGl_PBREnvironment) anEnvironment = new OpenGl_PBREnvironment (theCtx, thePow2Size, theLevelsNumber, theId);
138 if (!anEnvironment->IsComplete())
140 theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_MEDIUM,
141 "Warning: PBR environment is not created. PBR material system will be ignored.");
142 anEnvironment->Release (theCtx.get());
143 anEnvironment.Nullify();
146 return anEnvironment;
149 // =======================================================================
150 // function : OpenGl_PBREnvironment
152 // =======================================================================
153 OpenGl_PBREnvironment::OpenGl_PBREnvironment (const Handle(OpenGl_Context)& theCtx,
154 unsigned int thePowOf2Size,
155 unsigned int theSpecMapLevelsNumber,
156 const TCollection_AsciiString& theId)
157 : OpenGl_NamedResource (theId),
158 myPow2Size (std::max (1u, thePowOf2Size)),
159 mySpecMapLevelsNumber (std::max (2u, std::min (theSpecMapLevelsNumber, std::max (1u, thePowOf2Size) + 1))),
160 myFBO (OpenGl_FrameBuffer::NO_FRAMEBUFFER),
161 myIsComplete (Standard_False),
162 myIsNeededToBeBound (Standard_True)
164 OpenGl_PBREnvironmentSentry aSentry (theCtx);
166 myIsComplete = initVAO (theCtx)
167 && initTextures (theCtx)
176 // =======================================================================
179 // =======================================================================
180 void OpenGl_PBREnvironment::Bind (const Handle(OpenGl_Context)& theCtx)
182 myIBLMaps[OpenGl_TypeOfIBLMap_DiffuseSH].Bind (theCtx);
183 myIBLMaps[OpenGl_TypeOfIBLMap_Specular] .Bind (theCtx);
184 myIsNeededToBeBound = Standard_False;
187 // =======================================================================
190 // =======================================================================
191 void OpenGl_PBREnvironment::Unbind (const Handle(OpenGl_Context)& theCtx)
193 myIBLMaps[OpenGl_TypeOfIBLMap_DiffuseSH].Unbind (theCtx);
194 myIBLMaps[OpenGl_TypeOfIBLMap_Specular] .Unbind (theCtx);
195 myIsNeededToBeBound = Standard_True;
198 // =======================================================================
201 // =======================================================================
202 void OpenGl_PBREnvironment::Clear (const Handle(OpenGl_Context)& theCtx,
203 const Graphic3d_Vec3& theColor)
205 OpenGl_PBREnvironmentSentry aSentry (theCtx);
206 clear (theCtx, theColor);
209 // =======================================================================
212 // =======================================================================
213 void OpenGl_PBREnvironment::Bake (const Handle(OpenGl_Context)& theCtx,
214 const Handle(OpenGl_Texture)& theEnvMap,
215 Standard_Boolean theZIsInverted,
216 Standard_Boolean theIsTopDown,
217 Standard_Size theDiffMapNbSamples,
218 Standard_Size theSpecMapNbSamples,
219 Standard_ShortReal theProbability)
221 Standard_ProgramError_Raise_if (theEnvMap.IsNull(), "'Bake' function of 'OpenGl_PBREnvironment' can't work without source environment map")
222 Standard_RangeError_Raise_if (theProbability > 1.f || theProbability < 0.f, "'probability' parameter in 'Bake' function of 'OpenGl_PBREnvironment' must be in range [0, 1]")
224 OpenGl_PBREnvironmentSentry aSentry (theCtx);
225 bake (theCtx, theEnvMap, theZIsInverted, theIsTopDown, theDiffMapNbSamples, theSpecMapNbSamples, theProbability);
228 // =======================================================================
229 // function : SizesAreDifferent
231 // =======================================================================
232 bool OpenGl_PBREnvironment::SizesAreDifferent (unsigned int thePow2Size,
233 unsigned int theSpecMapLevelsNumber) const
235 thePow2Size = std::max (1u, thePow2Size);
236 theSpecMapLevelsNumber = std::max (2u, std::min (theSpecMapLevelsNumber, std::max (1u, thePow2Size) + 1));
237 return myPow2Size != thePow2Size
238 || mySpecMapLevelsNumber != theSpecMapLevelsNumber;
241 // =======================================================================
242 // function : Release
244 // =======================================================================
245 void OpenGl_PBREnvironment::Release (OpenGl_Context* theCtx)
247 if (myFBO != OpenGl_FrameBuffer::NO_FRAMEBUFFER)
250 && theCtx->IsValid())
252 theCtx->arbFBO->glDeleteFramebuffers (1, &myFBO);
254 myFBO = OpenGl_FrameBuffer::NO_FRAMEBUFFER;
256 myIBLMaps[OpenGl_TypeOfIBLMap_DiffuseSH].Release(theCtx);
257 myIBLMaps[OpenGl_TypeOfIBLMap_Specular] .Release (theCtx);
258 myVBO.Release (theCtx);
261 // =======================================================================
262 // function : ~OpenGl_PBREnvironment
264 // =======================================================================
265 OpenGl_PBREnvironment::~OpenGl_PBREnvironment()
270 // =======================================================================
271 // function : initTextures
273 // =======================================================================
274 bool OpenGl_PBREnvironment::initTextures (const Handle(OpenGl_Context)& theCtx)
276 myIBLMaps[OpenGl_TypeOfIBLMap_Specular] .Sampler()->Parameters()->SetTextureUnit (theCtx->PBRSpecIBLMapTexUnit());
277 myIBLMaps[OpenGl_TypeOfIBLMap_DiffuseSH].Sampler()->Parameters()->SetTextureUnit (theCtx->PBRDiffIBLMapSHTexUnit());
278 myIBLMaps[OpenGl_TypeOfIBLMap_Specular] .Sampler()->Parameters()->SetFilter (Graphic3d_TOTF_TRILINEAR);
279 myIBLMaps[OpenGl_TypeOfIBLMap_DiffuseSH].Sampler()->Parameters()->SetFilter(Graphic3d_TOTF_NEAREST);
280 myIBLMaps[OpenGl_TypeOfIBLMap_Specular] .Sampler()->Parameters()->SetLevelsRange (mySpecMapLevelsNumber - 1);
282 // NVIDIA's driver didn't work properly with 3 channel texture for diffuse SH coefficients so that alpha channel has been added
283 return myIBLMaps[OpenGl_TypeOfIBLMap_DiffuseSH].Init (theCtx,
284 OpenGl_TextureFormat::FindFormat (theCtx, Image_Format_RGBAF, false),
285 Graphic3d_Vec2i (9, 1),
287 && myIBLMaps[OpenGl_TypeOfIBLMap_Specular].InitCubeMap (theCtx, Handle(Graphic3d_CubeMap)(),
288 Standard_Size(1) << myPow2Size, Image_Format_RGB, true, false);
291 // =======================================================================
292 // function : initVAO
294 // =======================================================================
295 bool OpenGl_PBREnvironment::initVAO (const Handle(OpenGl_Context)& theCtx)
297 const float aVertexPos[] =
299 -1.f, -1.f, 0.f, 0.f,
304 return myVBO.Init (theCtx, 4, 4, aVertexPos);
307 // =======================================================================
308 // function : initFBO
310 // =======================================================================
311 bool OpenGl_PBREnvironment::initFBO (const Handle(OpenGl_Context)& theCtx)
313 theCtx->arbFBO->glGenFramebuffers (1, &myFBO);
314 return checkFBOComplentess (theCtx);
317 // =======================================================================
318 // function : processDiffIBLMap
320 // =======================================================================
321 bool OpenGl_PBREnvironment::processDiffIBLMap (const Handle(OpenGl_Context)& theCtx,
322 Standard_Boolean theIsDrawAction,
323 Standard_Size theNbSamples)
325 theCtx->arbFBO->glFramebufferTexture2D (GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
326 myIBLMaps[OpenGl_TypeOfIBLMap_DiffuseSH].TextureId(), 0);
327 const Standard_Integer aViewport[4] = { 0, 0, 9, 1 };
328 theCtx->ResizeViewport(aViewport);
331 theCtx->ActiveProgram()->SetUniform(theCtx, "occNbSpecIBLLevels", 0);
332 theCtx->ActiveProgram()->SetUniform(theCtx, "uSamplesNum", static_cast<Standard_Integer>(theNbSamples));
334 theCtx->core11fwd->glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
338 theCtx->core11fwd->glClear (GL_COLOR_BUFFER_BIT);
340 theCtx->core11fwd->glEnable (GL_SCISSOR_TEST);
341 theCtx->core11fwd->glClearColor (0.f, 0.f, 0.f, 1.f);
342 theCtx->core11fwd->glScissor (1, 0, 8, 1);
343 theCtx->core11fwd->glClear (GL_COLOR_BUFFER_BIT);
349 // =======================================================================
350 // function : processSpecIBLMap
352 // =======================================================================
353 bool OpenGl_PBREnvironment::processSpecIBLMap (const Handle(OpenGl_Context)& theCtx,
354 Standard_Boolean theIsDrawAction,
355 Standard_Integer theEnvMapSize,
356 Standard_Size theNbSamples,
357 Standard_ShortReal theProbability)
361 theCtx->ActiveProgram()->SetUniform (theCtx, "occNbSpecIBLLevels", Standard_Integer(mySpecMapLevelsNumber));
362 theCtx->ActiveProgram()->SetUniform (theCtx, "uEnvMapSize", theEnvMapSize);
365 for (int aLevelIter = mySpecMapLevelsNumber - 1;; --aLevelIter)
367 const Standard_Integer aSize = 1 << (myPow2Size - aLevelIter);
368 const Standard_Integer aViewport[4] = { 0, 0, aSize, aSize };
369 theCtx->ResizeViewport (aViewport);
372 Standard_Integer aNbSamples = static_cast<Standard_Integer>(Graphic3d_PBRMaterial::SpecIBLMapSamplesFactor (theProbability, aLevelIter / float (mySpecMapLevelsNumber - 1)) * theNbSamples);
373 theCtx->ActiveProgram()->SetUniform (theCtx, "uSamplesNum", static_cast<Standard_Integer>(aNbSamples));
374 theCtx->ActiveProgram()->SetUniform (theCtx, "uCurrentLevel", aLevelIter);
377 for (Standard_Integer aSideIter = 0; aSideIter < 6; ++aSideIter)
379 theCtx->arbFBO->glFramebufferTexture2D (GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + aSideIter,
380 myIBLMaps[OpenGl_TypeOfIBLMap_Specular].TextureId(), aLevelIter);
383 theCtx->ActiveProgram()->SetUniform(theCtx, "uCurrentSide", aSideIter);
384 theCtx->core11fwd->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
388 theCtx->core11fwd->glClear(GL_COLOR_BUFFER_BIT);
401 // =======================================================================
402 // function : checkFBOCompletness
404 // =======================================================================
405 bool OpenGl_PBREnvironment::checkFBOComplentess (const Handle(OpenGl_Context)& theCtx) const
407 theCtx->arbFBO->glBindFramebuffer (GL_DRAW_FRAMEBUFFER, myFBO);
408 theCtx->arbFBO->glFramebufferTexture2D (GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
409 myIBLMaps[OpenGl_TypeOfIBLMap_DiffuseSH].TextureId(), 0);
410 if (theCtx->arbFBO->glCheckFramebufferStatus (GL_DRAW_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
414 for (Standard_Integer aSideIter = 0; aSideIter < 6; ++aSideIter)
416 for (unsigned int aLevel = 0; aLevel < mySpecMapLevelsNumber; ++aLevel)
418 theCtx->arbFBO->glFramebufferTexture2D (GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + aSideIter,
419 myIBLMaps[OpenGl_TypeOfIBLMap_Specular].TextureId(), aLevel);
420 if (theCtx->arbFBO->glCheckFramebufferStatus (GL_DRAW_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
429 // =======================================================================
432 // =======================================================================
433 void OpenGl_PBREnvironment::bake (const Handle(OpenGl_Context)& theCtx,
434 const Handle(OpenGl_Texture)& theEnvMap,
435 Standard_Boolean theZIsInverted,
436 Standard_Boolean theIsTopDown,
437 Standard_Size theDiffNbSamples,
438 Standard_Size theSpecNbSamples,
439 Standard_ShortReal theProbability)
441 myIsNeededToBeBound = Standard_True;
442 if (!theCtx->ShaderManager()->BindPBREnvBakingProgram())
446 theEnvMap->Bind (theCtx, theCtx->PBRSpecIBLMapTexUnit());
447 theCtx->ActiveProgram()->SetSampler (theCtx, "uEnvMap", theCtx->PBRSpecIBLMapTexUnit());
448 theCtx->ActiveProgram()->SetUniform (theCtx, "uZCoeff", theZIsInverted ? -1 : 1);
449 theCtx->ActiveProgram()->SetUniform (theCtx, "uYCoeff", theIsTopDown ? 1 : -1);
450 theCtx->arbFBO->glBindFramebuffer (GL_DRAW_FRAMEBUFFER, myFBO);
451 myVBO.BindAttribute (theCtx, Graphic3d_TOA_POS);
455 if (processSpecIBLMap (theCtx, true, theEnvMap->SizeX(), theSpecNbSamples, theProbability)
456 && processDiffIBLMap (theCtx, true, theDiffNbSamples))
458 Message::SendTrace(TCollection_AsciiString()
459 + "IBL " + myIBLMaps[OpenGl_TypeOfIBLMap_Specular].SizeX() + "x" + myIBLMaps[OpenGl_TypeOfIBLMap_Specular].SizeY()
460 + " is baked in " + aTimer.ElapsedTime() + " s");
464 theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PERFORMANCE, 0, GL_DEBUG_SEVERITY_HIGH,
465 TCollection_AsciiString("Error: baking PBR environment ") + myIBLMaps[OpenGl_TypeOfIBLMap_Specular].SizeX()
466 + "x" + myIBLMaps[OpenGl_TypeOfIBLMap_Specular].SizeY() + " takes too much time!.");
467 clear (theCtx, Graphic3d_Vec3(1.0f));
470 myVBO.UnbindAttribute (theCtx, Graphic3d_TOA_POS);
471 theEnvMap->Unbind (theCtx, theCtx->PBREnvLUTTexUnit());
474 // =======================================================================
477 // =======================================================================
478 void OpenGl_PBREnvironment::clear (const Handle(OpenGl_Context)& theCtx,
479 const Graphic3d_Vec3& theColor)
481 myIsNeededToBeBound = Standard_True;
482 theCtx->arbFBO->glBindFramebuffer (GL_DRAW_FRAMEBUFFER, myFBO);
483 theCtx->core11fwd->glClearColor (theColor.r(), theColor.g(), theColor.b(), 1.f);
485 processSpecIBLMap (theCtx, false);
486 processDiffIBLMap (theCtx, false);