From a2b94b95626da1a1e6bd91e9f64cb3025962e770 Mon Sep 17 00:00:00 2001 From: Chris Sherlock Date: Fri, 30 Jan 2015 01:34:17 +1100 Subject: vcl: OpenGL code for adding preambles to glsl fragments now handles #version If you include the #version directive then it must be on the first non-comment line in a glsl fragment. This is now handled. Change-Id: I7e938c27b24d20f25e656667a122d7a341f51611 Reviewed-on: https://gerrit.libreoffice.org/14246 Reviewed-by: Chris Sherlock Tested-by: Chris Sherlock --- vcl/source/opengl/OpenGLHelper.cxx | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/vcl/source/opengl/OpenGLHelper.cxx b/vcl/source/opengl/OpenGLHelper.cxx index 3523ae231f14..a574366ef375 100644 --- a/vcl/source/opengl/OpenGLHelper.cxx +++ b/vcl/source/opengl/OpenGLHelper.cxx @@ -97,6 +97,35 @@ namespace { } } +static void addPreamble(OString& rShaderSource, const OString& rPreamble) +{ + if (rPreamble.isEmpty()) + return; + + OString aVersionStr("#version"); + int nVersionStrStartPos = rShaderSource.indexOf(aVersionStr, 0); + + if (nVersionStrStartPos == -1) + { + rShaderSource = rPreamble + "\n" + rShaderSource; + } + else + { + int nVersionStrEndPos = rShaderSource.indexOf('\n', nVersionStrStartPos); + + // no way this should happen - if this is the case, then it's a syntax error + assert(nVersionStrEndPos != -1); + + if (nVersionStrEndPos == -1) + nVersionStrEndPos = nVersionStrStartPos + 8; + + OString aVersionLine = rShaderSource.copy(0, nVersionStrEndPos - nVersionStrStartPos); + OString aShaderBody = rShaderSource.copy(nVersionStrEndPos - nVersionStrStartPos); + + rShaderSource = aVersionLine + "\n" + rPreamble + "\n" + aShaderBody; + } +} + GLint OpenGLHelper::LoadShaders(const OUString& rVertexShaderName,const OUString& rFragmentShaderName, const OString& preamble) { // Create the shaders @@ -108,7 +137,7 @@ GLint OpenGLHelper::LoadShaders(const OUString& rVertexShaderName,const OUString // Compile Vertex Shader OString aVertexShaderSource = loadShader(rVertexShaderName); if( !preamble.isEmpty()) - aVertexShaderSource = preamble + "\n" + aVertexShaderSource; + addPreamble( aVertexShaderSource, preamble ); char const * VertexSourcePointer = aVertexShaderSource.getStr(); glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL); glCompileShader(VertexShaderID); @@ -122,7 +151,7 @@ GLint OpenGLHelper::LoadShaders(const OUString& rVertexShaderName,const OUString // Compile Fragment Shader OString aFragmentShaderSource = loadShader(rFragmentShaderName); if( !preamble.isEmpty()) - aFragmentShaderSource = preamble + "\n" + aFragmentShaderSource; + addPreamble( aFragmentShaderSource, preamble ); char const * FragmentSourcePointer = aFragmentShaderSource.getStr(); glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL); glCompileShader(FragmentShaderID); -- cgit