Cycles: Add thin film iridescence to Principled BSDF #118477

Open
Lukas Stockner wants to merge 15 commits from LukasStockner/blender:iridescence into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
Member

This is a WIP implementation of thin film iridescence in the Principled BSDF based on "A Practical Extension to Microfacet Theory for the Modeling of Varying Iridescence".

There's still several open topics:

  • Properly integrating it with the F0 and F90 controls. The best approach might be to split the "Generalized Schlick" Fresnel mode - it's currently used for both the Principled BSDF and for the generalized_schlick OSL closure, by using the sign of the IOR as a flag (which is already hacky anyways). The Principled BSDF doesn't need the F90 control, so we could have a Principled-specific mode without F90 that does iridescence and a Generalized Schlick mode with F90 that doesn't.
  • Supporting metallic Fresnel properly. It's possible to just use the F82 model, but that misses the proper interaction with the film IOR and the phase shift. Since F82 is an approximation already anyways, that's probably fine though.
  • The code currently has two paths, one with and one without polarization awareness. We should check the impact on results to see whether the complexity of polarization is worth it here.
  • The code currently hardcodes the XYZ->RGB matrix, which is bad of course. We have proper OCIO-based support for XYZ conversions, but there's an additional complexity here: These are reflectance values, not luminance. This causes issues as soon as the working color space uses a white point other than CIE E (such as D65). Intuitively: If the code produces 100% reflectivity (1, 1, 1) in XYZ, it must map to 100% reflectivity (1, 1, 1) in RGB - but XYZ (1, 1, 1) maps to yellow-ish if the usual conversion to sRGB is used.

For now, here's a picture of a soap bubble, using the same parameters (IOR 1.7, 400nm) as in the paper:
Screenshot from 2024-02-20 01-05-40.png

I've also compared the results to the great comparison by @PascalSchon from here and the paper supplemental material, and it matches at least in terms of overall hue.

This is a WIP implementation of thin film iridescence in the Principled BSDF based on ["A Practical Extension to Microfacet Theory for the Modeling of Varying Iridescence"](https://belcour.github.io/blog/research/publication/2017/05/01/brdf-thin-film.html). There's still several open topics: - Properly integrating it with the F0 and F90 controls. The best approach might be to split the "Generalized Schlick" Fresnel mode - it's currently used for both the Principled BSDF and for the `generalized_schlick` OSL closure, by using the sign of the IOR as a flag (which is already hacky anyways). The Principled BSDF doesn't need the F90 control, so we could have a Principled-specific mode without F90 that does iridescence and a Generalized Schlick mode with F90 that doesn't. - Supporting metallic Fresnel properly. It's possible to just use the F82 model, but that misses the proper interaction with the film IOR and the phase shift. Since F82 is an approximation already anyways, that's probably fine though. - The code currently has two paths, one with and one without polarization awareness. We should check the impact on results to see whether the complexity of polarization is worth it here. - The code currently hardcodes the XYZ->RGB matrix, which is bad of course. We have proper OCIO-based support for XYZ conversions, but there's an additional complexity here: These are reflectance values, not luminance. This causes issues as soon as the working color space uses a white point other than CIE E (such as D65). Intuitively: If the code produces 100% reflectivity `(1, 1, 1)` in XYZ, it must map to 100% reflectivity `(1, 1, 1)` in RGB - but XYZ `(1, 1, 1)` maps to yellow-ish if the usual conversion to sRGB is used. For now, here's a picture of a soap bubble, using the same parameters (IOR 1.7, 400nm) as in the paper: ![Screenshot from 2024-02-20 01-05-40.png](/attachments/8a049a33-1925-497c-852e-c4dab1dad8ad) I've also compared the results to the great comparison by @PascalSchon from [here](https://projects.blender.org/blender/blender/issues/99447#issuecomment-920492) and the paper supplemental material, and it matches at least in terms of overall hue.
Lukas Stockner added the
Module
Render & Cycles
label 2024-02-20 03:28:58 +01:00
Member

EEVEE rendering of the Principled BSDF is currently broken. This is of how the node inputs are being passed to node_shader_gpu_bsdf_principled. A simple fix is to just add the extra inputs to that function and leave them unused. (Your main focus seems to be Cycles, so leaving these unused at the moment is fine). Something as simple as: f666e9d034 will do at the moment.

Sorry for "reviewing" minor features while you probably want feedback on the more important stuff.

EEVEE rendering of the Principled BSDF is currently broken. This is of how the node inputs are being passed to `node_shader_gpu_bsdf_principled`. A simple fix is to just add the extra inputs to that function and leave them unused. (Your main focus seems to be Cycles, so leaving these unused at the moment is fine). Something as simple as: https://projects.blender.org/Alaska/blender/commit/f666e9d0347c3194aaab483e5e11931f4ceaa9be will do at the moment. Sorry for "reviewing" minor features while you probably want feedback on the more important stuff.
Lukas Stockner force-pushed iridescence from 4418a204f3 to d665174879 2024-02-26 05:12:21 +01:00 Compare
Author
Member

Updates:

  • Fixed EEVEE, thanks @Alaska
  • Implemented a basic approach for handling the color conversion correctly-ish. There's a slightly better approach (Von Kries-style chromatic adaption matrix) that might make sense, and a much better approach that is complete overkill (explicitly computing the Fresnel-domain reference curves).
  • The code accounts for F0 now, so the Specular Tint input has an effect (I'd really advise against combining it with Thin Film, however)
  • Overall code cleanup
  • Switched over to using the polarization-aware approach, turns out it does make a notable difference for IOR>1.5
  • Fixed a few bugs around the interaction with Fresnel
Updates: - Fixed EEVEE, thanks @Alaska - Implemented a basic approach for handling the color conversion correctly-ish. There's a slightly better approach (Von Kries-style chromatic adaption matrix) that might make sense, and a much better approach that is complete overkill (explicitly computing the Fresnel-domain reference curves). - The code accounts for F0 now, so the Specular Tint input has an effect (I'd really advise against combining it with Thin Film, however) - Overall code cleanup - Switched over to using the polarization-aware approach, turns out it does make a notable difference for IOR>1.5 - Fixed a few bugs around the interaction with Fresnel
Lukas Stockner force-pushed iridescence from d665174879 to 0dcf334003 2024-04-15 17:46:47 +02:00 Compare
Lukas Stockner changed title from WIP: Cycles: Add thin film iridescence to Principled BSDF to Cycles: Add thin film iridescence to Principled BSDF 2024-04-15 17:48:31 +02:00
Author
Member

Updates:

  • Rebased to latest main
  • Fixed handling of TIR on the bottom interface (thin film to bulk medium)
  • Fixed handling of backfacing IOR

I'm not aware of any remaining issues, so I think this is ready for review. There's things that could be improved later (the color adaption method, adding it as a standalone node etc.), but the basics are there.

Updates: - Rebased to latest `main` - Fixed handling of TIR on the bottom interface (thin film to bulk medium) - Fixed handling of backfacing IOR I'm not aware of any remaining issues, so I think this is ready for review. There's things that could be improved later (the color adaption method, adding it as a standalone node etc.), but the basics are there.
Lukas Stockner requested review from Brecht Van Lommel 2024-04-15 17:51:58 +02:00
Lukas Stockner requested review from Weizhen Huang 2024-04-15 17:51:58 +02:00
Weizhen Huang requested changes 2024-04-16 16:49:27 +02:00
Weizhen Huang left a comment
Member

I did not test this PR thoroughly, just had a first pass. I believe some points in you PR description is outdated, please update them.
I set up a soap bubble and it looks beatiful. I wonder what parameters should I use for coated metal?

I did not test this PR thoroughly, just had a first pass. I believe some points in you PR description is outdated, please update them. I set up a soap bubble and it looks beatiful. I wonder what parameters should I use for coated metal?
@ -383,6 +419,7 @@ ccl_device Spectrum bsdf_microfacet_estimate_albedo(KernelGlobals kg,
if (!is_zero(reflectance) && bsdf->fresnel_type == MicrofacetFresnel::GENERALIZED_SCHLICK) {
ccl_private FresnelGeneralizedSchlick *fresnel = (ccl_private FresnelGeneralizedSchlick *)
bsdf->fresnel;
/* TODO: Iridescence */
Member

What is this TODO?

What is this TODO?
Author
Member

The code here wasn't accounting for the thin film yet, this is fixed now.

The code here wasn't accounting for the thin film yet, this is fixed now.
weizhen marked this conversation as resolved
@ -238,0 +272,4 @@
}
float R123 = R12 * R23;
float r123 = sqrt(R123);
Member

Why not sqrtf() here?

Why not `sqrtf()` here?
Author
Member

Thanks, fixed.

Thanks, fixed.
weizhen marked this conversation as resolved
@ -238,0 +288,4 @@
/* Computes reflectivity (R) and phase shift (phi) for perpendicular (.x) and parallel (.y)
* polarized light. */
ccl_device_inline void fresnel_dielectric_polarized(const float cosThetaI,
Member

This function is almost identical as fresnel_dielectric() except for the phase computation, would it be practical to reuse that function? The phase computation could then be:

phi->x = float(r_s < 0.0f) * M_PI_F;
phi->y = float(r_p < 0.0f) * M_PI_F;

The existing function also returns cos_theta_t, which is needed for computing the OPD.

If it turns out to be impractical, I would then prefer that these two functions have the same structure, notation and comment, eg. snake_case, a comment indicating total internal reflection, mentioning s and p polarization (or compute r_s and r_p first, then square the result).

This function is almost identical as `fresnel_dielectric()` except for the phase computation, would it be practical to reuse that function? The phase computation could then be: ``` phi->x = float(r_s < 0.0f) * M_PI_F; phi->y = float(r_p < 0.0f) * M_PI_F; ``` The existing function also returns `cos_theta_t`, which is needed for computing the OPD. If it turns out to be impractical, I would then prefer that these two functions have the same structure, notation and comment, eg. `snake_case`, a comment indicating total internal reflection, mentioning s and p polarization (or compute `r_s` and `r_p` first, then square the result).
Author
Member

Good point, thanks, those are indeed very similar. I'll merge them together.

Comparing the two, I think I found a sign error in the current code. So far it wouldn't have mattered because we only use squared values, but it does matter for the phase.

Good point, thanks, those are indeed very similar. I'll merge them together. Comparing the two, I think I found a sign error in the current code. So far it wouldn't have mattered because we only use squared values, but it does matter for the phase.
Member

Do you mean the sign of cos_theta_t? In fresnel_dielectric() it is the angle between the surface normal and the refracted ray, hence negative, and the sign is used for refract_angle(). The result should be the same as your fresnel_dielectric_polarized(), i.e. cos_theta_i + eta * cos_theta_t < 0 is equivalent to eta1 * cosThetaI < eta2 * cosThetaT.

Do you mean the sign of `cos_theta_t`? In `fresnel_dielectric()` it is the angle between the surface normal and the refracted ray, hence negative, and the sign is used for `refract_angle()`. The result should be the same as your `fresnel_dielectric_polarized()`, i.e. `cos_theta_i + eta * cos_theta_t < 0` is equivalent to `eta1 * cosThetaI < eta2 * cosThetaT`.
Author
Member

Yes, I've noticed that and updated the code accordingly.
What I meant is that the current code does

const float r_p = (cos_theta_t + eta * cos_theta_i) / (cos_theta_t - eta * cos_theta_i);

but I think it should be

const float r_p = (cos_theta_t + eta * cos_theta_i) / (eta * cos_theta_i - cos_theta_t);

(note the sign swap in the denominator). This only sign-swaps r_p, which doesn't matter currently since the code only returns sqr(r_p).

Yes, I've noticed that and updated the code accordingly. What I meant is that the current code does ``` const float r_p = (cos_theta_t + eta * cos_theta_i) / (cos_theta_t - eta * cos_theta_i); ``` but I think it should be ``` const float r_p = (cos_theta_t + eta * cos_theta_i) / (eta * cos_theta_i - cos_theta_t); ``` (note the sign swap in the denominator). This only sign-swaps `r_p`, which doesn't matter currently since the code only returns `sqr(r_p)`.
Member

Yes, you are right, I was not paying attention to the denominator. I remember switching the order was only to make the two lines have the same structure because the sign didn't matter. Sorry for the confusion 😄

Yes, you are right, I was not paying attention to the denominator. I remember switching the order was only to make the two lines have the same structure because the sign didn't matter. Sorry for the confusion 😄
weizhen marked this conversation as resolved
@ -238,0 +331,4 @@
/* Compute angle inside the thin film (after refraction at the top interface). */
float cosTheta2_sq = 1.0f - sqr(eta1 / eta2) * (1.0f - sqr(cosTheta));
if (cosTheta2_sq < 0.0f) {
/* TIR at the top interface. */
Member

I believe we should trust fresnel_dielectric_polarized() instead of checking TIR before calling the function.

I believe we should trust `fresnel_dielectric_polarized()` instead of checking TIR before calling the function.
weizhen marked this conversation as resolved
@ -261,0 +261,4 @@
/* Panel for Thin Film settings. */
PanelDeclarationBuilder &film = b.add_panel("Thin Film").default_closed(true);
film.add_input<decl::Float>("Thin Film Thickness").default_value(0.0).min(0.0f).max(100000.0f);
Member

Specify the unit (nm).

Specify the unit (nm).
Author
Member

Good point. If we're already handling units, might as well do it properly: I've created #120900 for this, depending on what PR gets merged first I'll just update the other to tag this input correctly.

Good point. If we're already handling units, might as well do it properly: I've created #120900 for this, depending on what PR gets merged first I'll just update the other to tag this input correctly.

Default value for float socket is 0.0f by default, no reason to make this explicit.
Max value also does looks like meaningless (what for such soft max?)

Default value for float socket is `0.0f` by default, no reason to make this explicit. _Max value also does looks like meaningless (what for such soft max?)_

I think it's easier to understand the code with the default value explicit.

For soft max indeed FLT_MAX should generally be used if there is no real upper bound. But this can sometimes cause numerical precision problems, so not sure it works here.

I think it's easier to understand the code with the default value explicit. For soft max indeed `FLT_MAX` should generally be used if there is no real upper bound. But this can sometimes cause numerical precision problems, so not sure it works here.

I mean, at some point such declaration can be cleaned due to the fact this is not really necessary.

I mean, at some point such declaration can be cleaned due to the fact this is not really necessary.
@ -261,0 +263,4 @@
PanelDeclarationBuilder &film = b.add_panel("Thin Film").default_closed(true);
film.add_input<decl::Float>("Thin Film Thickness").default_value(0.0).min(0.0f).max(100000.0f);
#define SOCK_THIN_FILM_THICKNESS_ID 28
film.add_input<decl::Float>("Thin Film IOR").default_value(1.3f).min(1.0f).max(1000.0f);
Member

I think we could default to the IOR of water, which is 1.33.

I think we could default to the IOR of water, which is 1.33.
Lukas Stockner force-pushed iridescence from 0dcf334003 to 5921fdfc56 2024-04-22 01:53:53 +02:00 Compare
Weizhen Huang reviewed 2024-04-22 12:26:41 +02:00
@ -390,3 +425,1 @@
float z = sqrtf(fabsf((bsdf->ior - 1.0f) / (bsdf->ior + 1.0f)));
s = lookup_table_read_3D(
kg, rough, cos_NI, z, kernel_data.tables.ggx_gen_schlick_ior_s, 16, 16, 16);
/* Precomputing LUTs for thin-film iridescence isn't viable, so fall back to the specular
Member

For cognitive ease I would prefer to add an empty if (fresnel->thin_film.thickness > 0.1f) branch and put the comment there, the following block is then put in the else branch

For cognitive ease I would prefer to add an empty `if (fresnel->thin_film.thickness > 0.1f)` branch and put the comment there, the following block is then put in the `else` branch
weizhen marked this conversation as resolved
@ -39,0 +56,4 @@
}
/* Return squared amplitude to get the fraction of reflected energy. */
return make_float2(sqr(r_s), sqr(r_p));
Member

The newly added sqr() is not used, I assume you wanted to use it here?

The newly added `sqr()` is not used, I assume you wanted to use it here?
Author
Member

At first I thought that this needs to return un-squared coefficients, so I used average(sqr(fresnel_dielectric_polarized(...))) in fresnel_dielectric. But with the code as it is now, I don't think sqr(make_float2(r_s, r_p)) is much nicer than make_float2(sqr(r_s), sqr(r_p)), so we might as well not add it at all I guess.

At first I thought that this needs to return un-squared coefficients, so I used `average(sqr(fresnel_dielectric_polarized(...)))` in `fresnel_dielectric`. But with the code as it is now, I don't think `sqr(make_float2(r_s, r_p))` is much nicer than `make_float2(sqr(r_s), sqr(r_p))`, so we might as well not add it at all I guess.
weizhen marked this conversation as resolved
@ -39,0 +64,4 @@
float eta,
ccl_private float *r_cos_theta_t)
{
return average(fresnel_dielectric_polarized(cos_theta_i, eta, r_cos_theta_t, NULL));
Member

We use nullptr now.
Also, average() for float2 doesn't seem to be defined on metal 😕

We use `nullptr` now. Also, `average()` for `float2` doesn't seem to be defined on metal 😕
Author
Member

Ah, okay, I wasn't sure if that's supported on GPU but looks like it is.

Ah, okay, I wasn't sure if that's supported on GPU but looks like it is.
Member

I think the kernel is all C++ now, but not feature complete on some GPUs, not sure what exactly is supported but if buildbot doesn't complain it should be fine.

I think the kernel is all C++ now, but not feature complete on some GPUs, not sure what exactly is supported but if buildbot doesn't complain it should be fine.
weizhen marked this conversation as resolved
Weizhen Huang reviewed 2024-04-22 12:31:07 +02:00
@ -500,2 +542,3 @@
template<MicrofacetType m_type>
ccl_device Spectrum bsdf_microfacet_eval(ccl_private const ShaderClosure *sc,
ccl_device Spectrum bsdf_microfacet_eval(KernelGlobals kg,
const ShaderClosure *sc,
Member

This does not compile on Metal. Add ccl_private.

This does not compile on Metal. Add `ccl_private`.
weizhen marked this conversation as resolved
Lukas Stockner added 1 commit 2024-04-23 01:24:56 +02:00
buildbot/vexp-code-patch-lint Build done. Details
buildbot/vexp-code-patch-darwin-x86_64 Build done. Details
buildbot/vexp-code-patch-darwin-arm64 Build done. Details
buildbot/vexp-code-patch-linux-x86_64 Build done. Details
buildbot/vexp-code-patch-windows-amd64 Build done. Details
buildbot/vexp-code-patch-coordinator Build done. Details
4453b67e5c
Address review comments
Author
Member

@blender-bot build +gpu

@blender-bot build +gpu
Brecht Van Lommel approved these changes 2024-04-24 20:51:15 +02:00
Brecht Van Lommel left a comment
Owner

The GPU tests are known to be failing currently, for everything except Cycles. So the reported failures from the latest run can be ignored.

The GPU tests are known to be failing currently, for everything except Cycles. So the reported failures from the latest run can be ignored.
Some checks failed
buildbot/vexp-code-patch-lint Build done.
buildbot/vexp-code-patch-darwin-x86_64 Build done.
buildbot/vexp-code-patch-darwin-arm64 Build done.
buildbot/vexp-code-patch-linux-x86_64 Build done.
buildbot/vexp-code-patch-windows-amd64 Build done.
buildbot/vexp-code-patch-coordinator Build done.
Merge conflict checking is in progress. Try again in few moments.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u iridescence:LukasStockner-iridescence
git checkout LukasStockner-iridescence
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
5 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#118477
No description provided.