From b69e55bdb2544f3fcc492d535a77ff17022ed6d5 Mon Sep 17 00:00:00 2001 From: Tor Lillqvist Date: Thu, 8 Oct 2015 13:24:12 +0300 Subject: Don't re-read the same glsl files over and over again Change-Id: I10b4badaba01630e1a054b3d4d9cc4720822511a --- vcl/source/opengl/OpenGLHelper.cxx | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'vcl') diff --git a/vcl/source/opengl/OpenGLHelper.cxx b/vcl/source/opengl/OpenGLHelper.cxx index 92bfe7db5b77..5290f8c2445e 100644 --- a/vcl/source/opengl/OpenGLHelper.cxx +++ b/vcl/source/opengl/OpenGLHelper.cxx @@ -11,6 +11,7 @@ #include #include +#include #include #include #include @@ -66,6 +67,7 @@ OString loadShader(const OUString& rFilename) { OUString aFileURL = getShaderFolder() + rFilename +".glsl"; osl::File aFile(aFileURL); + SAL_INFO("vcl.opengl", "Reading " << aFileURL); if(aFile.open(osl_File_OpenFlag_Read) == osl::FileBase::E_None) { sal_uInt64 nSize = 0; @@ -85,6 +87,20 @@ OString loadShader(const OUString& rFilename) return OString(); } +OString& getShaderSource(const OUString& rFilename) +{ + static std::unordered_map aMap; + static osl::Mutex aMutex; + osl::MutexGuard aGuard(aMutex); + + if (aMap.find(rFilename) == aMap.end()) + { + aMap[rFilename] = loadShader(rFilename); + } + + return aMap[rFilename]; +} + } namespace { @@ -189,8 +205,8 @@ namespace const OString& rPreamble ) { // read shaders source - OString aVertexShaderSource = loadShader( rVertexShaderName ); - OString aFragmentShaderSource = loadShader( rFragmentShaderName ); + OString aVertexShaderSource = getShaderSource( rVertexShaderName ); + OString aFragmentShaderSource = getShaderSource( rFragmentShaderName ); // get info about the graphic device #if defined( SAL_UNX ) && !defined( MACOSX ) && !defined( IOS )&& !defined( ANDROID ) @@ -374,8 +390,8 @@ GLint OpenGLHelper::LoadShaders(const OUString& rVertexShaderName, GLint ProgramID = glCreateProgram(); // read shaders from file - OString aVertexShaderSource = loadShader(rVertexShaderName); - OString aFragmentShaderSource = loadShader(rFragmentShaderName); + OString aVertexShaderSource = getShaderSource(rVertexShaderName); + OString aFragmentShaderSource = getShaderSource(rFragmentShaderName); GLint bBinaryResult = GL_FALSE; if( GLEW_ARB_get_program_binary && !rDigest.isEmpty() ) -- cgit