Skip to content

A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.

TerrainShaderTests: loop the serialized subshaders by SerializedSubshaderCount (fixes #406) (#411)

Eyal Golanfae96fbfb3e8
diff --git a/scripts/oss/tests/test_unity_shader_data.py b/scripts/oss/tests/test_unity_shader_data.py
new file mode 100644
index 000000000..d9d39fc0f
--- /dev/null
+++ b/scripts/oss/tests/test_unity_shader_data.py
@@ -0,0 +1,31 @@
+"""ShaderData has two subshader counts. GetSerializedSubshader(i) indexes the serialized subshaders, so a loop over it must
+stop at SerializedSubshaderCount. SubshaderCount can be larger: on Windows (Direct3D 12) the terrain shader reports more
+subshaders than it serializes, and TerrainShaderTests logged "Serialized Subshader index is incorrect: 2" (#406). On macOS
+the two counts match, so only this source check catches the mistake before a Windows run does."""
+import re
+from pathlib import Path
+
+ROOT = Path(__file__).resolve().parents[3]
+LOOP = re.compile(r"for\s*\(\s*int\s+(\w+)\s*=\s*0\s*;\s*\1\s*<\s*[\w.]+\.SubshaderCount\s*;")
+
+
+def wrong_bounds(text: str) -> list[int]:
+ """Line numbers of loops bounded by SubshaderCount whose next lines call GetSerializedSubshader with the loop variable."""
+ found = []
+ for m in LOOP.finditer(text):
+ body = text[m.end():m.end() + 400]
+ if re.search(r"GetSerializedSubshader\(\s*" + m.group(1) + r"\s*\)", body):
+ found.append(text.count("\n", 0, m.start()) + 1)
+ return found
+
+
+def test_the_check_flags_the_406_loop_and_passes_the_fixed_one():
+ loop = "for (int s = 0; s < data.{0}; s++)\n{{\n var sub = data.GetSerializedSubshader(s);\n}}"
+ assert wrong_bounds(loop.format("SubshaderCount")) == [1]
+ assert wrong_bounds(loop.format("SerializedSubshaderCount")) == []
+
+
+def test_serialized_subshader_loops_stop_at_the_serialized_count():
+ bad = [f"{p.relative_to(ROOT)}:{line}" for p in sorted((ROOT / "unity/Assets").rglob("*.cs"))
+ for line in wrong_bounds(p.read_text(encoding="utf-8"))]
+ assert bad == [], "loop over GetSerializedSubshader with SerializedSubshaderCount, not SubshaderCount: " + ", ".join(bad)
diff --git a/unity/Assets/Tests/EditMode/TerrainShaderTests.cs b/unity/Assets/Tests/EditMode/TerrainShaderTests.cs
index b395470c9..deeabf977 100644
--- a/unity/Assets/Tests/EditMode/TerrainShaderTests.cs
+++ b/unity/Assets/Tests/EditMode/TerrainShaderTests.cs
@@ -36,14 +36,14 @@ public class TerrainShaderTests
var shader = AssetDatabase.LoadAssetAtPath<Material>(TerrainMatPath).shader;
var data = ShaderUtil.GetShaderData(shader);
int including = 0;
- for (int s = 0; s < data.SubshaderCount; s++)
+ for (int s = 0; s < data.SerializedSubshaderCount; s++)
{
var sub = data.GetSerializedSubshader(s);
for (int p = 0; p < sub.PassCount; p++) if (sub.GetPass(p).SourceCode.Contains("StatecraftTerrain.hlsl")) including++;
}
Assert.Greater(including, 0, "no generated pass includes StatecraftTerrain.hlsl: the Custom Function is not in File mode");
// Review I2: no node may compute at the template's half precision (mediump on GLES3 phones: ~210 m UV steps).
- for (int s = 0; s < data.SubshaderCount; s++)
+ for (int s = 0; s < data.SerializedSubshaderCount; s++)
{
var sub = data.GetSerializedSubshader(s);
for (int p = 0; p < sub.PassCount; p++)