Skip to content

Official Codebase of 3DCodeVerse

Python

glsl_shader: a main() only inside #if/#ifdef is not the entry point — the trailer still supplies one

gaoypeng66cc5fd5cebf
diff --git a/harness/codeverse3d/languages/glsl_shader/__init__.py b/harness/codeverse3d/languages/glsl_shader/__init__.py
index d4545e5b..6eef06b5 100644
--- a/harness/codeverse3d/languages/glsl_shader/__init__.py
+++ b/harness/codeverse3d/languages/glsl_shader/__init__.py
@@ -77,13 +77,28 @@ def strip_comments(text: str) -> str:
return _COMMENT.sub(_blank, text)
+def _unconditional(src: str) -> str:
+ """The lines outside every ``#if``/``#ifdef``/``#ifndef`` … ``#endif`` block."""
+ depth, keep = 0, []
+ for line in src.splitlines():
+ head = line.lstrip()
+ if head.startswith(("#if", "# if")):
+ depth += 1
+ elif head.startswith(("#endif", "# endif")):
+ depth = max(0, depth - 1)
+ elif depth == 0:
+ keep.append(line)
+ return "\n".join(keep)
+
+
def detect_convention(shader_src: str) -> str:
"""``main`` when the author wrote a ``main()`` (with or without a ``mainImage`` it calls — the usual
Shadertoy-port pattern), else ``mainImage`` (the harness appends the ``main()`` trailer). Appending
the trailer to a file that already has ``main()`` redefined it: codex gpt-6-luna wrote both on 4 of
4 one-shot shaders (p3x_graphics_v2, 2026-09-23) and every one failed to compile at ``harness:3``."""
shader_src = strip_comments(shader_src)
- if _PLAIN_MAIN.search(shader_src):
+ # a main() only inside #if/#ifdef (a STANDALONE guard) is not the entry point the harness compiles
+ if _PLAIN_MAIN.search(_unconditional(shader_src)):
return "main"
if _MAIN_IMAGE.search(shader_src):
return "mainImage"
diff --git a/harness/tests/graphics/test_wrap_lint.py b/harness/tests/graphics/test_wrap_lint.py
index c1e147e4..a4e6cbc9 100644
--- a/harness/tests/graphics/test_wrap_lint.py
+++ b/harness/tests/graphics/test_wrap_lint.py
@@ -2,6 +2,8 @@
from __future__ import annotations
+import pytest
+
from codeverse3d.contracts.artifacts import Severity
from codeverse3d.languages.glsl_shader import (
HEADER,
@@ -30,12 +32,16 @@ def test_plain_main_convention_has_no_trailer_and_comments_ignored():
assert compose("void mainImage(out vec4 c, in vec2 f) { c = texture(u_prev, f); }").uses_feedback
-def test_an_authored_main_that_calls_main_image_gets_no_second_main():
[email protected](("tail", "convention"), [
+ ("void main() {\n mainImage(fragColor, gl_FragCoord.xy);\n}\n", "main"),
+ ("#ifdef STANDALONE\nvoid main() {\n mainImage(fragColor, gl_FragCoord.xy);\n}\n#endif\n", "mainImage"),
+])
+def test_an_authored_main_that_calls_main_image_gets_no_second_main(tail, convention):
"""A Shadertoy port often keeps its own main() calling mainImage(); appending the trailer redefined
- main (4/4 gpt-6-luna one-shot shaders, 2026-09-23). The author's main() wins, and it compiles."""
- src = SHADER + "void main() {\n mainImage(fragColor, gl_FragCoord.xy);\n}\n"
- c = compose(src)
- assert c.convention == "main" and c.source.count("void main(") == 1
+ main (4/4 gpt-6-luna one-shot shaders, 2026-09-23). The author's main() wins, and it compiles —
+ unless it hides behind an #ifdef the harness never defines: then the trailer is the entry point."""
+ c = compose(SHADER + tail)
+ assert c.convention == convention # and it links: exactly one main() survives the preprocessor
import moderngl
ctx = moderngl.create_standalone_context()