Browse Source

shader: Fix support for `#pragma include <example.glsl>` in GLSL

rdb 3 years ago
parent
commit
0220a43ce0

+ 1 - 1
panda/src/gobj/shader.cxx

@@ -2876,7 +2876,7 @@ r_preprocess_source(ostream &out, istream &in, const Filename &fn,
             source_dir = full_fn.get_dirname();
             incfn = incfile;
 
-          } else if (sscanf(line.c_str(), " # pragma%*[ \t]include <%2047[^\"]> %zn", incfile, &nread) == 1
+          } else if (sscanf(line.c_str(), " # pragma%*[ \t]include <%2047[^\>]> %zn", incfile, &nread) == 1
               && nread == line.size()) {
             // Angled includes are also OK, but we don't search in the directory
             // of the source file.

+ 10 - 0
tests/display/glsl_include_angle.vert

@@ -0,0 +1,10 @@
+#version 150
+
+#pragma include <glsl_include_inputs.glsl>
+
+// Vertex inputs
+in vec4 p3d_Vertex;
+
+void main() {
+  gl_Position = p3d_ModelViewProjectionMatrix * p3d_Vertex;
+}

+ 10 - 0
tests/display/glsl_include_angle_legacy.vert

@@ -0,0 +1,10 @@
+#version 120
+
+#pragma include <glsl_include_inputs.glsl>
+
+// Vertex inputs
+attribute vec4 p3d_Vertex;
+
+void main() {
+  gl_Position = p3d_ModelViewProjectionMatrix * p3d_Vertex;
+}

+ 28 - 0
tests/display/test_glsl_shader.py

@@ -497,3 +497,31 @@ def test_glsl_includes(gsg):
     vert_path = core.Filename(SHADERS_DIR, 'glsl_include' + suffix + '.vert')
     frag_path = core.Filename(SHADERS_DIR, 'glsl_simple' + suffix + '.frag')
     run_glsl_compile_check(gsg, vert_path, frag_path)
+
+
+def test_glsl_includes_angle_nodir(gsg):
+    """Test preprocessing includes with angle includes without model-path"""
+    suffix = ''
+    if (gsg.driver_shader_version_major, gsg.driver_shader_version_minor) < (1, 50):
+        suffix = '_legacy'
+    vert_path = core.Filename(SHADERS_DIR, 'glsl_include_angle' + suffix + '.vert')
+    frag_path = core.Filename(SHADERS_DIR, 'glsl_simple' + suffix + '.frag')
+    assert core.Shader.load(core.Shader.SL_GLSL, vert_path, frag_path) is None
+
+
[email protected]
+def with_current_dir_on_model_path():
+    model_path = core.get_model_path()
+    model_path.prepend_directory(core.Filename.from_os_specific(os.path.dirname(__file__)))
+    yield
+    model_path.clear_local_value()
+
+
+def test_glsl_includes_angle_withdir(gsg, with_current_dir_on_model_path):
+    """Test preprocessing includes with angle includes with model-path"""
+    suffix = ''
+    if (gsg.driver_shader_version_major, gsg.driver_shader_version_minor) < (1, 50):
+        suffix = '_legacy'
+    vert_path = core.Filename(SHADERS_DIR, 'glsl_include_angle' + suffix + '.vert')
+    frag_path = core.Filename(SHADERS_DIR, 'glsl_simple' + suffix + '.frag')
+    run_glsl_compile_check(gsg, vert_path, frag_path)