From 94a3ae8174e0d902f9d81b80b1ce538e8ca32e3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sat, 22 Oct 2016 00:06:05 +0200 Subject: [PATCH] Final attempt to fix shaders w/o version directive. Took me insane amount of time. I blame those 12-hour working days. --- src/Magnum/Shader.cpp | 10 +++++++--- src/Magnum/Shader.h | 4 +++- src/Magnum/Test/ShaderGLTest.cpp | 2 ++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Magnum/Shader.cpp b/src/Magnum/Shader.cpp index eb2a0f125..6d596a6fe 100644 --- a/src/Magnum/Shader.cpp +++ b/src/Magnum/Shader.cpp @@ -786,13 +786,15 @@ Shader& Shader::addSource(std::string source) { /* Fix line numbers, so line 41 of third added file is marked as 3(41) in case shader version was not Version::None, because then source 0 - is the #version directive added in constructur. + is the #version directive added in constructor. If version was Version::None, line 41 of third added file is marked as 2(41). We apparently can't add even the #line directive before the potential `#version` directive -- in that case the first source - file is not marked with any file number, but that's equivalent to - the default where first file is marked as 0. */ + file is not marked with any file number, thus having number 0. In + order to avoid complex logic in compile() where we assert for at + least some user-provided source, an empty string is added here + instead. */ if(!_sources.empty()) _sources.push_back("#line 1 " + #if !defined(CORRADE_TARGET_NACL_NEWLIB) && !defined(CORRADE_TARGET_ANDROID) std::to_string((_sources.size()+1)/2) + @@ -800,6 +802,8 @@ Shader& Shader::addSource(std::string source) { converter.str() + #endif '\n'); + else _sources.emplace_back(); + _sources.push_back(std::move(source)); } diff --git a/src/Magnum/Shader.h b/src/Magnum/Shader.h index 67347eaa7..c3678dff2 100644 --- a/src/Magnum/Shader.h +++ b/src/Magnum/Shader.h @@ -595,7 +595,9 @@ class MAGNUM_EXPORT Shader: public AbstractObject { * * Adds given source to source list, preceeded with @c \#line directive * marking first line of the source as `n(1)` where n is number of - * added source. If passed string is empty, the function does nothing. + * added source. The source number `0` is @c \#version directive added + * in constructor, if any. If passed string is empty, the function does + * nothing. * @see @ref addFile() */ Shader& addSource(std::string source); diff --git a/src/Magnum/Test/ShaderGLTest.cpp b/src/Magnum/Test/ShaderGLTest.cpp index b2473bf22..519bf35a1 100644 --- a/src/Magnum/Test/ShaderGLTest.cpp +++ b/src/Magnum/Test/ShaderGLTest.cpp @@ -200,6 +200,7 @@ void ShaderGLTest::addSourceNoVersion() { #ifndef MAGNUM_TARGET_GLES CORRADE_COMPARE(shader.sources(), (std::vector{ + "", "#version 120\n", "#line 1 1\n", "#define FOO BAR\n", @@ -208,6 +209,7 @@ void ShaderGLTest::addSourceNoVersion() { })); #else CORRADE_COMPARE(shader.sources(), (std::vector{ + "", "#version 100\n", "#line 1 1\n", "#define FOO BAR\n",