Skip to content

Bug fix: make remap deterministic for degenerate input ranges#2916

Open
bhouston wants to merge 3 commits into
AcademySoftwareFoundation:mainfrom
bhouston:fix-remap-determinancy
Open

Bug fix: make remap deterministic for degenerate input ranges#2916
bhouston wants to merge 3 commits into
AcademySoftwareFoundation:mainfrom
bhouston:fix-remap-determinancy

Conversation

@bhouston

Copy link
Copy Markdown
Contributor

Found via https://material-fidelity.ben3d.ca. remap is not deterministic for metal, glsl or osl with them producing different results:

Screenshot 2026-05-11 at 12 49 41 PM

Define deterministic behavior for float remap when the input range is degenerate (inhigh == inlow).

  • In GLSL codegen (IM_remap_float_genglsl), add a guard:
    • if abs(inhigh - inlow) <= 1e-8, return outlow
    • else use the standard remap expression
  • In OSL support code (mx_remap(float, ...)), add the same guard before division.

Why

remap with a zero-width input range is undefined under the raw formula due to division by zero. Different backends were producing different results for nodes/remap_degenerate because of NaN/Inf handling differences.

This change establishes a stable, cross-backend default:

  • degenerate input range -> outlow

Scope

  • Limited to float remap behavior in genglsl and genosl.
  • Non-degenerate remap behavior is unchanged.

Validation

Re-rendered nodes/remap_degenerate with MaterialX renderers and they all match:

Screenshot 2026-05-11 at 12 55 01 PM

float mx_remap(float in, float inLow, float inHigh, float outLow, float outHigh, int doClamp)
{
float x = (in - inLow)/(inHigh-inLow);
float denom = inHigh - inLow;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's worth noting that in OSL "divide-by-zero" is not degenerate. It's defined to return 0.

bhouston added 2 commits May 11, 2026 21:26
Extend the remap degenerate-range fallback beyond float so color and vector variants also return outlow deterministically when inhigh and inlow collapse.
@jstone-lucasfilm

Copy link
Copy Markdown
Member

See #2914 (comment) for recommendations on resolving this category of undefined behaviors through new specification proposals, so that our per-language implementations have an unambiguous reference to match.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants