OSL: Vector Math Node modulo uses wrong function #73469

Closed
opened 2020-01-28 22:26:14 +01:00 by Charlie Jolly · 5 comments
Member

OSL should use fmod instead of mod. This applies only to vector node not regular math node. mod always returns positive values. This is evident when using negative numbers, see attached blend.

To quote OSL manual.

type fmod (type a, type b)
type mod (type a, type b)
The fmod() function returns the floating-point remainder of a=b, i.e., is the floating-point
equivalent of the integer % operator. It is nearly identical to the C or C++ fmod function,
except that in OSL, fmod(a,0) returns 0, rather than NaN. Note that if a < 0, the return
value will be negative.
The mod() function returns a-b*floor(a=b), which will always be a positive number or
zero. As an example, fmod(-0.25,1.0) = -0.25, but mod(-0.25,1.0) = 0.75. For
positive a they return the same value.

intern\cycles\kernel\shaders\node_vector_math.osl

Changing mod here:

  else if (type == "modulo") {
    Vector = mod(Vector1, Vector2);
  }

to fmod will fix the bug but may break files that use OSL.

  else if (type == "modulo") {
    Vector = fmod(Vector1, Vector2);
  }

There is a case to add both versions of this function. I can provide patch to fix issue and another patch to add mod too.

Patch here: D6704

image.png

modulo2.blend

OSL should use `fmod` instead of `mod`. This applies only to vector node not regular math node. `mod` always returns positive values. This is evident when using negative numbers, see attached blend. To quote OSL manual. > **type fmod (type a, type b)** > **type mod (type a, type b)** > The `fmod()` function returns the floating-point remainder of a=b, i.e., is the floating-point > equivalent of the integer % operator. It is nearly identical to the C or C++ fmod function, > except that in OSL, fmod(a,0) returns 0, rather than NaN. Note that if a < 0, the return > value will be negative. > The `mod()` function returns `a-b*floor(a=b)`, which will always be a positive number or > zero. As an example, fmod(-0.25,1.0) = -0.25, but mod(-0.25,1.0) = 0.75. For > positive a they return the same value. `intern\cycles\kernel\shaders\node_vector_math.osl` Changing `mod` here: ``` else if (type == "modulo") { Vector = mod(Vector1, Vector2); } ``` to `fmod` will fix the bug but may break files that use OSL. ``` else if (type == "modulo") { Vector = fmod(Vector1, Vector2); } ``` There is a case to add both versions of this function. I can provide patch to fix issue and another patch to add `mod` too. Patch here: [D6704](https://archive.blender.org/developer/D6704) ![image.png](https://archive.blender.org/developer/F8308448/image.png) [modulo2.blend](https://archive.blender.org/developer/F8308453/modulo2.blend)
Author
Member

Added subscriber: @CharlieJolly

Added subscriber: @CharlieJolly
Member

Changed status from 'Needs Triage' to: 'Confirmed'

Changed status from 'Needs Triage' to: 'Confirmed'

This issue was referenced by blender/cycles@6a24b9573f

This issue was referenced by blender/cycles@6a24b9573f794aa56da390f9a2433ab0a0ed2431

This issue was referenced by d3670823b3

This issue was referenced by d3670823b37750e4d993ca28dbd094fee8519c1b
Author
Member

Changed status from 'Confirmed' to: 'Resolved'

Changed status from 'Confirmed' to: 'Resolved'
Charlie Jolly self-assigned this 2020-01-29 13:39:00 +01:00
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#73469
No description provided.