Questions about MA_RAMP_LINEAR #98856

Closed
opened 2022-06-13 18:54:17 +02:00 by zitong · 8 comments

In source/blender/blenkernel/intern/material.c 1821 MA_RAMP_LINEAR
I'm curious if this

if (col[0] > 0.5f) {
    r_col[0] = r_col[0] + fac * (2.0f * (col[0] - 0.5f));
}
else {
    r_col[0] = r_col[0] + fac * (2.0f * (col[0]) - 1.0f);
}

is same as

 r_col[0] = r_col[0] + fac * (2.0f * (col[0] - 0.5f));

?
If not, then for what? Thanks!

In source/blender/blenkernel/intern/material.c 1821 MA_RAMP_LINEAR I'm curious if this ``` if (col[0] > 0.5f) { r_col[0] = r_col[0] + fac * (2.0f * (col[0] - 0.5f)); } else { r_col[0] = r_col[0] + fac * (2.0f * (col[0]) - 1.0f); } ``` is same as ``` r_col[0] = r_col[0] + fac * (2.0f * (col[0] - 0.5f)); ``` ? If not, then for what? Thanks!
Author

Added subscriber: @zitong

Added subscriber: @zitong
Contributor

Added subscriber: @scurest

Added subscriber: @scurest

Added subscriber: @tempdevnova

Added subscriber: @tempdevnova

1243c2bdae/source/blender/blenkernel/intern/material.c (L1821)

I am not sure but ramp_blend(), which is the function containing MA_RAMP_LINEAR, seems to be some implementation of the mixRGB node or something similar (what is it exactly?).
But what I can tell you is that

"if (col- [x] > 0.5f) {

  r_col[0] = r_col[0] + fac * (2.0f * (col[0] - 0.5f));

}
else {

  r_col[0] = r_col[0] + fac * (2.0f * (col[0]) - 1.0f);

}"

Is an implemention of the linear light blending mode.
Basically linear light is a combination of linear dodge (called add in Blender) and linear burn and is in the group called contrast blend modes, which is why it has the if...else statement.

"is same as

r_col- [x] = r_col- [x] + fac * (2.0f * (col- [x] - 0.5f));
?
If not, then for what? Thanks"

I did some testing:
Color_blend_test_file.blend

And yes... it is so.

In fact (2.0f * (col- [x] - 0.5f))=(2.0f * (col- [x]) - 1.0f) is a mathematical identity so we are calculating the same function in both statements.
As for the reason the top version is implemented in Blender is probably historical.
Back when Adobe first invented those blend modes all contrast blend modes including linear light came with this same logic.
See: https://www.deepskycolors.com/archivo/2010/04/21/formulas-for-Photoshop-blending-modes.html
The thought was that as we have an 'overlay' operation, which is a combination of multiply and screen, we should also have one for linear burn and linear dodge(add), which is what they called linear light.

After that no one ever bothered to optimize the implementations, which is why to this day Blender and all other software use the top version as linear light.

By the way what does the f in e.g. (col- [x] > 0.5f) mean and why does sit exist?

https://github.com/blender/blender/blob/1243c2bdae39a0d3f7644e39e24d2cc3e901e03b/source/blender/blenkernel/intern/material.c#L1821 I am not sure but ramp_blend(), which is the function containing MA_RAMP_LINEAR, seems to be some implementation of the mixRGB node or something similar (what is it exactly?). But what I can tell you is that "if (col- [x] > 0.5f) { ``` r_col[0] = r_col[0] + fac * (2.0f * (col[0] - 0.5f)); ``` } else { ``` r_col[0] = r_col[0] + fac * (2.0f * (col[0]) - 1.0f); ``` }" Is an implemention of the `linear light` blending mode. Basically `linear light` is a combination of `linear dodge` (called add in Blender) and `linear burn` and is in the group called `contrast blend modes`, which is why it has the if...else statement. "is same as r_col- [x] = r_col- [x] + fac * (2.0f * (col- [x] - 0.5f)); ? If not, then for what? Thanks" I did some testing: [Color_blend_test_file.blend](https://archive.blender.org/developer/F13157246/Color_blend_test_file.blend) And yes... it is so. In fact (2.0f * (col- [x] - 0.5f))=(2.0f * (col- [x]) - 1.0f) is a mathematical identity so we are calculating the same function in both statements. As for the reason the top version is implemented in Blender is probably historical. Back when Adobe first invented those blend modes all `contrast blend modes` including `linear light` came with this same logic. See: https://www.deepskycolors.com/archivo/2010/04/21/formulas-for-Photoshop-blending-modes.html The thought was that as we have an 'overlay' operation, which is a combination of multiply and screen, we should also have one for `linear burn` and `linear dodge(add)`, which is what they called `linear light`. After that no one ever bothered to optimize the implementations, which is why to this day Blender and all other software use the top version as `linear light`. By the way what does the `f` in e.g. `(col- [x] > 0.5f)` mean and why does sit exist?
Author

Thank you very much @tempdevnova!

And about the f is mean the constant before is a float type and not a double.

See: https://docs.microsoft.com/en-us/cpp/c-language/c-floating-point-constants?view=msvc-170

A floating-point constant without an f, F, l, or L suffix has type double. If the letter f or F is the suffix, the constant has type float. If suffixed by the letter l or L, it has type long double.
Thank you very much @tempdevnova! And about the `f` is mean the constant before is a float type and not a double. See: https://docs.microsoft.com/en-us/cpp/c-language/c-floating-point-constants?view=msvc-170 ``` A floating-point constant without an f, F, l, or L suffix has type double. If the letter f or F is the suffix, the constant has type float. If suffixed by the letter l or L, it has type long double. ```
Member

Added subscriber: @LazyDodo

Added subscriber: @LazyDodo
Member

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

Changed status from 'Needs Triage' to: 'Archived'
Member

The bug tracker is just for bugs, if you have questions about the code, please use devtalk.blender.org

The bug tracker is just for bugs, if you have questions about the code, please use devtalk.blender.org
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
4 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#98856
No description provided.