Refactor: Cycles: adjust microfacet lobe selection pdf by tint #112158

Merged
Weizhen Huang merged 7 commits from weizhen/blender:refactor_microfacet_fresnel into main 2023-09-18 15:32:53 +02:00
Member

Now that there are different Fresnel types and the reflectance can be tinted,
it is better to sample based on the actually used Fresnel type, instead of
the original Fresnel. This also avoids computing Fresnel multiple times.

Now that there are different Fresnel types and the reflectance can be tinted, it is better to sample based on the actually used Fresnel type, instead of the original Fresnel. This also avoids computing Fresnel multiple times.
Weizhen Huang added the
Module
Render & Cycles
label 2023-09-08 19:33:16 +02:00
Weizhen Huang added 1 commit 2023-09-08 19:33:22 +02:00
b003cdfb9e Refactor: Cycles: weight reflection/refraction lobe selection pdf by tint
Now that there are different Fresnel types and the reflectance can be tinted,
it is better to sample based on the tinted instead of the real Fresnel. Also
avoid computing Fresnel multiple times.
Weizhen Huang added 1 commit 2023-09-08 19:35:27 +02:00
Weizhen Huang changed title from Refactor: Cycles: weight reflection/refraction lobe selection pdf by tint to Refactor: Cycles: adjust microfacet lobe selection pdf by tint 2023-09-11 11:42:02 +02:00
Lukas Stockner requested changes 2023-09-13 03:30:44 +02:00
Lukas Stockner left a comment
Member

Looks great! The new approach is much cleaner.
Just a few code-level comments.

Looks great! The new approach is much cleaner. Just a few code-level comments.
@ -511,3 +517,2 @@
(is_transmission && m_reflection) || (!is_transmission && m_refraction))
(is_transmission && !has_transmission) || (!is_transmission && !has_reflection))
{
*pdf = 0.0f;
Member

Is this intentional?

Is this intentional?
Author
Member

In bsdf_eval() the *pdf is initialized with zero, so it's not necessary to assign zero to *pdf again.
I was thinking whether we should remove such assignment in all the bsdf_xxx_eval(). On one hand these functions should be responsible themselves to ensure the return values are valid, on the other hand we have many early returns here and it looks ugly to have *eval = zero_spectrum() and *pdf = 0.0f everywhere.
Doesn't really matter either way I think, here it just looks a bit cleaner to me. Or we can initialze the values to 0.0f at the beginning of this function.

In `bsdf_eval()` the `*pdf` is initialized with zero, so it's not necessary to assign zero to `*pdf` again. I was thinking whether we should remove such assignment in all the `bsdf_xxx_eval()`. On one hand these functions should be responsible themselves to ensure the return values are valid, on the other hand we have many early returns here and it looks ugly to have `*eval = zero_spectrum()` and `*pdf = 0.0f` everywhere. Doesn't really matter either way I think, here it just looks a bit cleaner to me. Or we can initialze the values to 0.0f at the beginning of this function.
Member

Ah yeah, good point. All of these only get called from one location anyways, so it's much nicer to do a clean initialization there than to do it 100x in each individual BSDF.

Ah yeah, good point. All of these only get called from one location anyways, so it's much nicer to do a clean initialization there than to do it 100x in each individual BSDF.
@ -219,1 +210,3 @@
{
* If generalized Schlick is used, the reflectance is an interpolation of the F0 color and the F90
* color.
* For dielectric and conductor, use Fresnel equations.
Member

This comment was already outdated before this PR, I guess now would be a good time to update it 😄
Maybe something like

Computes Fresnel term for the given Microfacet BSDF and incoming direction (cos_theta_i),
returns the reflected and transmitted energy. If r_cos_theta_r is provided, it also
computes and returns the angle of refraction.
This comment was already outdated before this PR, I guess now would be a good time to update it 😄 Maybe something like ``` Computes Fresnel term for the given Microfacet BSDF and incoming direction (cos_theta_i), returns the reflected and transmitted energy. If r_cos_theta_r is provided, it also computes and returns the angle of refraction. ```
weizhen marked this conversation as resolved
@ -220,0 +213,4 @@
* Otherwise returns white. */
ccl_device_forceinline void microfacet_fresnel(ccl_private const MicrofacetBsdf *bsdf,
const float cos_theta_i,
ccl_private float *r_cos_theta_r,
Member

Shouldn't this be r_cos_theta_t?

Shouldn't this be `r_cos_theta_t`?
Author
Member

Would be better yes! This comes from an early iteration where I was thinking r can mean both reflection and refraction, but the reflection angle is not really needed here, it always equals to cos_theta_i

Would be better yes! This comes from an early iteration where I was thinking `r` can mean both reflection and refraction, but the reflection angle is not really needed here, it always equals to `cos_theta_i`
weizhen marked this conversation as resolved
@ -254,3 +254,1 @@
return refraction ? zero_spectrum() : fresnel->reflection_tint;
}
cosI = safe_sqrtf(1.0f - sinT2);
const float cosT_sq = 1.0f - (1.0f - sqr(cos_theta_i)) / sqr(bsdf->ior);
Member

Nitpick: I think we generally use 2 as a suffix to indicate squared values, like cosT2?

Nitpick: I think we generally use `2` as a suffix to indicate squared values, like `cosT2`?
Author
Member

Sergey said the convention in Blender is _sq, I have that in ray_sphere_intersect(), and am trying to name all the newly added variables with that suffix. But we have a lot mixed convention here, for example H and wi, cosT and cos_theta_t, 2 and _sq. Maybe we should do a cleanup at some point

Sergey said the convention in Blender is `_sq`, I have that in `ray_sphere_intersect()`, and am trying to name all the newly added variables with that suffix. But we have a lot mixed convention here, for example `H` and `wi`, `cosT` and `cos_theta_t`, `2` and `_sq`. Maybe we should do a cleanup at some point
Member

Ah okay, maybe Blender and Cycles drifted apart a bit here? Anyways, you're right that this is not really consistent, so I think we can just leave this.

Ah okay, maybe Blender and Cycles drifted apart a bit here? Anyways, you're right that this is not really consistent, so I think we can just leave this.
@ -257,0 +258,4 @@
*r_transmission = zero_spectrum();
return;
}
const float cosT = safe_sqrtf(cosT_sq);
Member

Nitpick: The if above explicitly checks for non-positive cosT_sq, so this can be a regular sqrt.

Nitpick: The `if` above explicitly checks for non-positive `cosT_sq`, so this can be a regular `sqrt`.
weizhen marked this conversation as resolved
@ -258,2 +266,3 @@
/* TODO(lukas): Is a special case for exponent==5 worth it? */
s = powf(1.0f - cosI, fresnel->exponent);
/* When going from a higher to a lower IOR, we must use the transmitted angle. */
s = powf(1.0f - ((bsdf->ior < 1.0f) ? *r_cos_theta_r : cos_theta_i), fresnel->exponent);
Member

This might cause issues if r_cos_theta_r is NULL, just use cosT I guess.

This might cause issues if `r_cos_theta_r` is NULL, just use `cosT` I guess.
weizhen marked this conversation as resolved
Weizhen Huang added 3 commits 2023-09-13 14:09:33 +02:00
Lukas Stockner approved these changes 2023-09-14 02:03:47 +02:00
Lukas Stockner left a comment
Member

LGTM - looks like tests need to be updated, but it's only a few fireflies that are different.

LGTM - looks like tests need to be updated, but it's only a few fireflies that are different.
Weizhen Huang added 2 commits 2023-09-18 15:32:03 +02:00
Weizhen Huang merged commit 57990ec3fc into main 2023-09-18 15:32:53 +02:00
Weizhen Huang deleted branch refactor_microfacet_fresnel 2023-09-18 15:32:55 +02:00
Sign in to join this conversation.
No reviewers
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser Project (Legacy)
Interest
Asset System
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
2 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#112158
No description provided.