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.

fix(visual): the GLES3 check reads the generated GLSL and has a GLES3-specific negative control (review I3)

eyalgolan46d2d5b669e5
diff --git a/unity/Assets/Tests/EditMode/ShaderChecks.cs b/unity/Assets/Tests/EditMode/ShaderChecks.cs
index d52e2eaf0..9b5d7fd73 100644
--- a/unity/Assets/Tests/EditMode/ShaderChecks.cs
+++ b/unity/Assets/Tests/EditMode/ShaderChecks.cs
@@ -11,12 +11,31 @@ using UnityEngine.Rendering;
/// its base variant and with the main-light-shadow keyword (the variant the web tier draws the map with). A pass whose
/// source excludes GLES3 (`#pragma exclude_renderers ... gles3 ...`, or `#pragma only_renderers` without gles3) is skipped:
/// Shader Graph's Terrain Lit target emits DOTS subshaders at target 4.5 that are built for other APIs. The check cannot
-/// pass vacuously: at least one subshader must have passes that are built for GLES3 and all compile.</summary>
+/// pass vacuously: at least one subshader must have passes that are built for GLES3 and all compile.
+/// Each variant is compiled with forExternalTool, which returns the generated GLSL (the plain overload returns no GLES3
+/// output, so only HLSL front-end errors would show), and the GLSL is checked against WebGL 2 (GlslErrors): it must be
+/// GLSL ES 3.00 and must not require an extension, which WebGL 2 does not have for anything the translator requires
+/// (review I3: a fragment UAV write compiles for Metal and requires GL_EXT_texture_buffer here).</summary>
public static class ShaderChecks
{
static readonly string[][] Variants = { Array.Empty<string>(), new[] { "_MAIN_LIGHT_SHADOWS" } };
static readonly Regex Exclude = new Regex(@"#pragma\s+exclude_renderers\s+([^\r\n]*)");
static readonly Regex Only = new Regex(@"#pragma\s+only_renderers\s+([^\r\n]*)");
+ static readonly Regex Version = new Regex(@"#version\s+([^\r\n]*)");
+ static readonly Regex Require = new Regex(@"#extension\s+(\w+)\s*:\s*require");
+
+ /// <summary>What in generated GLES3 GLSL WebGL 2 cannot run: no GLSL, a version other than "300 es", or an extension
+ /// the code requires.</summary>
+ public static List<string> GlslErrors(string glsl)
+ {
+ var errors = new List<string>();
+ var versions = Version.Matches(glsl ?? "");
+ if (versions.Count == 0) errors.Add("no GLSL was generated");
+ foreach (Match v in versions)
+ if (v.Groups[1].Value.Trim() != "300 es") errors.Add($"#version {v.Groups[1].Value.Trim()}: WebGL 2 runs GLSL ES 3.00 only");
+ foreach (Match r in Require.Matches(glsl ?? "")) errors.Add($"requires {r.Groups[1].Value}, which WebGL 2 does not have");
+ return errors;
+ }
public static bool ExcludesGles3(string passSource)
{
@@ -44,11 +63,14 @@ public static class ShaderChecks
foreach (var keywords in Variants)
foreach (var stage in new[] { ShaderType.Vertex, ShaderType.Fragment })
{
- var info = pass.CompileVariant(stage, keywords, ShaderCompilerPlatform.GLES3x, BuildTarget.WebGL);
+ var info = pass.CompileVariant(stage, keywords, ShaderCompilerPlatform.GLES3x, BuildTarget.WebGL, true);
var where = $"{shader.name} subshader {s} pass '{pass.Name}' {stage} [{string.Join(" ", keywords)}]";
foreach (var m in info.Messages)
if (m.severity == ShaderCompilerMessageSeverity.Error) subErrors.Add($"{where}: {m.message} (line {m.line})");
if (!info.Success && info.Messages.Length == 0) subErrors.Add($"{where}: failed without a message");
+ if (info.Success)
+ foreach (var e in GlslErrors(System.Text.Encoding.UTF8.GetString(info.ShaderData ?? Array.Empty<byte>())))
+ subErrors.Add($"{where}: {e}");
}
}
if (built > 0 && subErrors.Count == 0) usable++;
diff --git a/unity/Assets/Tests/EditMode/VisualHarnessTests.cs b/unity/Assets/Tests/EditMode/VisualHarnessTests.cs
index 38e83e2d3..66f1f0d7e 100644
--- a/unity/Assets/Tests/EditMode/VisualHarnessTests.cs
+++ b/unity/Assets/Tests/EditMode/VisualHarnessTests.cs
@@ -238,4 +238,30 @@ public class VisualHarnessTests
}
finally { Object.DestroyImmediate(mixed); Object.DestroyImmediate(none); }
}
+
+ // A fragment-stage UAV write: Metal compiles it; the GLES3 translation needs GL_EXT_texture_buffer, which WebGL 2 lacks.
+ const string MetalOnly = "Pass { HLSLPROGRAM\n#pragma vertex v\n#pragma fragment f\nfloat4 v(float4 p : POSITION) : SV_POSITION { return p; }\nRWTexture2D<float4> _U : register(u1);\nfloat4 f(float4 p : SV_POSITION) : SV_Target { _U[int2(p.xy)] = 1; return 1; }\nENDHLSL }";
+
+ /// <summary>Review I3: a GLES3-specific negative control. The pass compiles for Metal, but its GLES3 (WebGL 2) GLSL
+ /// requires an extension WebGL 2 does not have, so the check fails it; a generic HLSL error would not prove that the
+ /// check looks at the GLES3 output at all.</summary>
+ [Test]
+ public void AShaderThatCompilesForMetalButNotForWebGL2Fails()
+ {
+ Assert.IsEmpty(ShaderChecks.GlslErrors("#version 300 es\n#extension GL_EXT_shader_framebuffer_fetch : enable\nvoid main() {}"));
+ Assert.IsNotEmpty(ShaderChecks.GlslErrors("#version 310 es\nvoid main() {}"), "WebGL 2 is GLSL ES 3.00");
+ Assert.IsNotEmpty(ShaderChecks.GlslErrors("#version 300 es\n#extension GL_EXT_texture_buffer : require\nvoid main() {}"));
+ Assert.IsNotEmpty(ShaderChecks.GlslErrors(""), "no GLSL at all is not a pass");
+ var shader = Make("Shader \"Test/MetalOnly\" { SubShader { " + MetalOnly + " } }");
+ try
+ {
+ var pass = ShaderUtil.GetShaderData(shader).GetSerializedSubshader(0).GetPass(0);
+ var metal = pass.CompileVariant(UnityEditor.Rendering.ShaderType.Fragment, new string[0],
+ UnityEditor.Rendering.ShaderCompilerPlatform.Metal, BuildTarget.StandaloneOSX);
+ Assert.IsTrue(metal.Success, "compiles for Metal: " + string.Join("; ", metal.Messages.Select(m => m.message)));
+ var errors = ShaderChecks.Gles3Errors(shader);
+ Assert.That(errors, Has.Some.Contains("GL_EXT_texture_buffer"), string.Join("\n", errors));
+ }
+ finally { Object.DestroyImmediate(shader); }
+ }
}