Skip to content

Shader source from expressions: MathS.Shaders.Hlsl and Glsl, with a page - #2470

Merged
Rafael-SOWNet merged 4 commits into
masterfrom
feat/shader-source-2
Nov 16, 2026
Merged

Rafael-SOWNet merged 4 commits into
masterfrom
feat/shader-source-2

Conversation

@Rafael-SOWNet

Copy link
Copy Markdown
Member

MathS.Shaders.Hlsl and MathS.Shaders.Glsl print expressions as straight-line shader source: one assignment per output, each repeated subexpression computed once into a local, sincos for a sin/cos pair in HLSL. The contract is the issue's: the code computes the real reading Compile<double, …> computes, and the tests hold it to that — the same printer writes C# against helpers named as HLSL's intrinsics, Roslyn compiles that text, and its values are compared with Compile<double, …> at 200 points per case.

Four commits, meant to be merged as they are (rebase, not squash):

  1. MathS.Shaders.Hlsl and Glsl — from feat/shader-source, rebased onto master.
  2. The catalogue entries hlsl and glsl.
  3. Agreement with Compile on master's nodes, and a page.
    • No exception for control flow: the printer checks the whole expression first and returns GeneratedSource(null, At, Reason) when it declines.
    • A node Compile refuses is declined with Compile's own message (Binders.Refusal, StatementPositions.Refusal); five tests assert the two messages are equal.
    • The rewrites Compile makes are made here too: a matrix operation on matrices written out becomes the number it gives, a membership in an interval or finite set becomes comparisons, and tanh is rescaled (tanh(400) is 1, not inf/inf).
    • x^(1/3) printed pow(x, 1/3), NaN at negative x, where Compile takes the real root; it now prints sign(x) * pow(abs(x), p).
    • The page Docs/Usage/Shaders.md: a sphere's normal, the exact gradient of an exponential smooth union of two spheres in HLSL and GLSL with the generated code, mapping variables to shader names, and the contract. Its 6 samples pass docsamples. Linked from the README's Compilation section, AGENTS.md and Packaging.md §5.
  4. The same text whatever MathS.Settings.UnicodeOutput says (Unicode output: MathS.Settings.UnicodeOutput, and the parser reads the glyphs it prints #2469 merged after this was written): four expressions in HLSL and GLSL, flag off and on.
node Compile<double> shader
binders, lambda, product over a set refuses declined, with Compile's message
dot, cross, det, trace, transpose, an entry, of a variable refuses declined, node named
the same on matrices written out reduces, then compiles printed as the number it gives
conjugate, re, im, arg compiles printed
cotan, sec, csc and their inverses; iverson, xor, implies compiles printed
a statement where a number is expected, e.g. sin(x > 0) refuses declined with Compile's message
membership in an interval or finite set compiles comparisons
gcd, lcm compiles declined: Euclid's algorithm is a loop, and the shader is straight-line code
membership in RR, ZZ and the other number sets; provided, round, factorial, special functions compiles declined, as the issue decided

At a zero divisor the shader and Compile differ, as decided on the issue: since #2288 Compile<double> gives NaN at a / 0 and at zero to a non-positive power, where the printed code gives ±inf under IEEE 754 (and 1.0 for an unsimplified x^0). Guarding every quotient would cost a branch each; the difference is documented on MathS.Shaders and on the page instead.

Packaging: none of AGENTS.md's clauses fires — KernelDependenciesTest passes (no new referenced assembly), and the printer composes the kernel's own refusals and rewrites. Offering the shaders from the MCP server and the terminal is #2298. Happypig375's suggestion that the same printer could speed up compiled functions is for later.

ShadersTest: 70 cases. Full check on commit 3 before the rebase onto c530e40: 20,185 unit tests, 0 failed; MCP 444, Terminal 69, F# 134, Interactive 19; AOT OK; BoundCheck 0 disagreements, RootCheck 596/596, CasBench 120/121 with 0 wrong, PropCheck clean, SimpSweep 10463/10463, RuleCheck clean, CanonCheck and Confluence at their baselines. After the rebase: Shaders, PublicApi, KernelDependencies, Catalogue and StringizeRoundTrip, 799 passed.

Closes #2293.

🤖 Generated with Claude Code

Rafael-SOWNet and others added 4 commits November 16, 2026 00:55
Prints expressions as straight-line HLSL or GLSL: one assignment per output,
every repeated subexpression computed once into a local, and in HLSL one
sincos for a sin and cos of the same argument. The use is an exact gradient
or a closed-form physical law derived on the CPU and run on the GPU.

The code computes the real-valued reading that Compile<double, ...> computes,
and the tests hold the printed text to it: the same printer writes C# against
helpers named as HLSL's intrinsics, Roslyn compiles that text, and it is
compared with Compile<double, double, double> at 200 points per case, each
case required to have at least 50 points with a real value.

What has no faithful form is declined with the node named rather than printed
approximately: complex constants, Providedf, a piecewise without a final
always-true case, names that are not identifiers, and nodes such as factorial
and round. Integer powers above the fourth go through pow(abs(x), n) with the
sign restored for odd n, since pow of a negative base is undefined in both
languages, and mod is printed floored, as Modf is.

No clause of Packaging.md section 4 fires: pure managed code and no new
referenced assembly (KernelDependenciesTest), beside IntoLinq. No existing
answer changes.

Measured: UnitTests 18256 passed, 13 skipped, 0 failed (net10.0);
FSharpWrapperUnitTests 134 passed; kernel builds warning-free on
netstandard2.0, net8.0 and net10.0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
AGENTS.md: an operation the library offers by name is an entry of the
catalogue. The two queries answer what MathS.Shaders.Hlsl and Glsl answer,
which CatalogueTest checks on sin(x) * cos(x), where HLSL's sincos makes the
two differ. No surface offers them yet: the MCP server's Tools.NotOffered
and the terminal's OneShot.notOffered list them, pointing at #2298, which
proposes am_shader_source.

Part of #2293.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The printer now checks an expression before printing anything and returns
the refusal, rather than throwing and catching inside the printer. What
Compile refuses, a binder (now including a product over a set) or a
statement where a number is expected, is declined with Compile's own
message, through Binders.Refusal and StatementPositions.Refusal, which
Check now throws from.

It makes the rewrites Compile makes: a matrix operation on matrices
written out is the number it gives, a membership in an interval or a
finite set is its comparisons, and the exponential quotient tanh is
spelled as is rescaled so it does not divide an overflow by an overflow.

Printed, as Compile compiles them: a rational power with an odd
denominator below ten as the real root of a negative base (x^(1/3) was
pow(x, 1/3), NaN at a negative x where Compile has a value); cotan, sec,
csc and their inverses; conjugate, re, im and arg of a real; iverson,
xor and implies. Declined with a reason: gcd and lcm (a loop), matrices
and the matrix operations of a variable, membership in a set of numbers.

The XML docs and Docs/Usage/Shaders.md name where the shader and
Compile<double> differ: +-inf at a zero divisor and at zero to a negative
power, 1.0 for an unsimplified x^0, and pow of a negative base. The page
works the exact normal of a smooth union of two spheres through, in HLSL
and GLSL; docsamples runs its six samples.

Part of #2293

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…says

The printer writes numbers with double's round-trip format and names as the
caller maps them; Stringize appears only in the reason a node is declined.
This pins that: four expressions printed as HLSL and GLSL with the flag off
and on give the same code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.60000% with 31 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.98%. Comparing base (c530e40) to head (03ba464).

Files with missing lines Patch % Lines
...h/Functions/Compilation/IntoSource/ShaderSource.cs 86.60% 20 Missing and 10 partials ⚠️
...unctions/Compilation/IntoSource/GeneratedSource.cs 66.66% 0 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2470      +/-   ##
==========================================
+ Coverage   86.96%   86.98%   +0.02%     
==========================================
  Files         321      324       +3     
  Lines       71603    71849     +246     
  Branches    20895    20955      +60     
==========================================
+ Hits        62269    62501     +232     
- Misses       5011     5017       +6     
- Partials     4323     4331       +8     
Flag Coverage Δ
cpp 37.77% <ø> (ø)
csharp 87.51% <87.55%> (+0.02%) ⬆️
fsharp 32.97% <ø> (ø)
interactive 39.81% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
Sources/AngouriMath/Convenience/MathS.Shaders.cs 100.00% <100.00%> (ø)
Sources/AngouriMath/Core/Operations/Catalogue.cs 100.00% <100.00%> (ø)
...urces/AngouriMath/Functions/Compilation/Binders.cs 93.54% <100.00%> (+0.95%) ⬆️
...riMath/Functions/Compilation/StatementPositions.cs 100.00% <100.00%> (ø)
...urces/Terminal/AngouriMath.Terminal.Lib/OneShot.fs 69.48% <100.00%> (ø)
...unctions/Compilation/IntoSource/GeneratedSource.cs 66.66% <66.66%> (ø)
...h/Functions/Compilation/IntoSource/ShaderSource.cs 86.60% <86.60%> (ø)

... and 9 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Shader source from expressions: MathS.Shaders.Hlsl and Glsl

1 participant