Repository navigation
Print shader numbers, negated negatives and reserved variable names so the shader compiles - #2745
Merged
Merged
Conversation
…mes so the shader compiles A number past the largest float was printed as Infinity.0, or as a literal a float overflows on, and a number below the smallest normal float as one a GPU may flush to zero; the shader computes in float, so neither is the number. Both are declined now, naming the number, and every number is written as the float that holds it, rounded once from its exact value: through a double first, a number just under half an ulp past the largest float became the double that a float takes as an infinity. The negative of a negative number was printed (--9.81), the decrement operator, which neither language compiles; the operand is parenthesized. An exponent with no variable in it is taken at its value, so x^(-g) at g = 2 is 1 / (x * x) rather than pow(x, -2.0), which has no value at a negative x, and a whole power past 2^31 keeps the sign of its base as the smaller ones do. A variable named output, line, log or any other word HLSL or GLSL reserves, or a function the code calls, was printed as given. It is printed as am_ and its name, the same in both languages, with am_ repeated until no other input or output has the name; a reserved name given explicitly in names is declined instead. Measured by printing 25,004 cases in both dialects (numbers at every float edge, negated negatives in 35 operator positions, 23,408 variable names from both languages' keyword lists and the identifiers in glslang's and DXC's binaries, and collisions) and compiling each with glslang 16.2.0 (OpenGL and Vulkan) and DXC 1.10.2611; every accepted literal equals the correctly rounded float of the exact value. Fixes #2723 Fixes #2724 Fixes #2726 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #2745 +/- ##
==========================================
+ Coverage 86.68% 86.71% +0.03%
==========================================
Files 346 346
Lines 76971 77063 +92
Branches 22648 22682 +34
==========================================
+ Hits 66720 66826 +106
+ Misses 5523 5512 -11
+ Partials 4728 4725 -3
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
A caller binds a shader's inputs by the names it asked for, so a variable printed as am_output would not be found under output. A variable whose name the language being printed reserves, or names a function the code calls, is declined with a reason that points at the names, as a name that is not an identifier is; each language has its own list, so a name reserved only in HLSL prints in GLSL. Part of #2726 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Three printer defects in
MathS.Shaders.Hlsl/Glsl, fixed together because they touch the same code.#2723, numbers. A number past the largest
floatprintedInfinity.0, or a literal afloatoverflows on (1e+40). A number other than 0 below the smallest normalfloatprinted a literal that a GPU may flush to zero, sox / 1e-40would be an infinity. The shader computes infloat, so none of these is the number. Both kinds are now declined, naming the number, in both dialects. HLSL accepts1e+40but holds it as +inf, and an infinity is not the value of a finite number. Declining is the only answer that is not wrong.Every literal is now the
floatthat holds the number, rounded once from its exact value. It used to be the nearest double, which the compiler then rounded again. For340282356779733661637539395458142568447, half an ulp short of overflow, the nearest double is exactly the tie, and a compiler that parses through a double takes that as +inf. The printed text is the shortest form that reads back as that float, whether a compiler reads straight into a float or through a double. If no short form does, it is the float's exact double. So1/3prints as0.33333334.#2724, negatives.
-(-9.81)printed(--9.81), the decrement operator; the operand is now parenthesized:(-(-9.81)). From the same issue: an exponent with no variable in it is taken at its value, sox^(-g)withg = 2prints1.0 / (x * x)andx^(2^2)prints((x * x) * (x * x)). Before, both werepow, which is NaN at a negative base on a GPU. A whole power past2^31is now printed aspow(abs(x), n)with the sign restored, as the smaller powers are.#2726, names. A variable whose name the language being printed reserves is declined, as the issue asks, with a reason that names the word and the dialect and points at
names. It makes no difference whether the name is the variable's own or given innames. Reserved here means:gl_orGL_;__;logormax, which a variable of that name would hide.Each language has its own list, so
lineprints in GLSL andoutputprints in HLSL.Measured
I printed 25,004 cases in both dialects and compiled every printed shader with glslang 16.2.0 (OpenGL and Vulkan targets) and DXC 1.10.2611 (
cs_6_0), on master2a7247653and on this branch. Both compilers were installed into a scratch directory without root. fxc and a GPU driver were not available, so NVIDIA's stricter literal rule (1e+40rejected) is covered by a range check, not by a compiler. The cases:g, at every operator position.MathS.Vartakes from both languages' keyword lists and type families, plus every identifier-like string in the glslang and DXC binaries.InfinityEvery one of the 496 / 86 names the compiler rejected on master is declined in that dialect. Every name the branch prints compiles in that dialect's compiler, including the HLSL-only words printed in GLSL and the other way round.
The 73 / 68 names that are declined although the compiler accepted them on master fall into two groups:
matrix,vector,string,sampler,pass,NULLand the effect-framework types. fxc rejectsmatrix,vectorandstring, as Shader source: a variable named output or line is printed as given, and the shader does not compile #2726 found.sqrt,sin, ...). Those compile in this sweep's shape, which calls onlylog, but they break the first formula that calls them.Every literal the branch emits for
x * aequals the correctly roundedfloatof the exact value, checked with mpmath at 80 digits (29 of 29). On master, 4 differed. The 26 GLSL rows left out of the table assign a comparison to the harness'sfloatoutput, which GLSL does not convert; they are the same on both builds.powof a negative base compiles, so the exponent fixes are checked by value instead. The C# check helper'spownow returns NaN at a negative base, as the shading languages leave it, and the printed code is compared withCompile<double>.Full C# unit suite: 22,413 passed, 0 failed. Printing the 50,008 sweep cases took the same time on both builds, about 1.3 s.
Fixes #2723
Fixes #2724
Fixes #2726
🤖 Generated with Claude Code