-
Notifications
You must be signed in to change notification settings - Fork 398
Add testrender volumes #2053
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ThyOwen
wants to merge
1
commit into
AcademySoftwareFoundation:main
Choose a base branch
from
ThyOwen:add-testrender-volumes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add testrender volumes #2053
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1513,6 +1513,92 @@ struct ZeltnerBurleySheen final : public BSDF, MxSheenParams { | |
| } | ||
| }; | ||
|
|
||
|
|
||
| struct HenyeyGreenstein final : public BSDF { | ||
| const float g; | ||
| OSL_HOSTDEVICE HenyeyGreenstein(float g) : BSDF(this), g(g) {} | ||
|
|
||
| static OSL_HOSTDEVICE float PhaseHG(float cos_theta, float g) | ||
| { | ||
| const float denom = 1 + g * g + 2 * g * cos_theta; | ||
| return (1 - g * g) / (4 * M_PI * denom * sqrtf(denom)); | ||
| } | ||
|
|
||
| OSL_HOSTDEVICE Sample eval(const Vec3& wo, const Vec3& wi) const | ||
| { | ||
| const float pdf = PhaseHG(dot(wo, wi), g); | ||
| return { wi, Color3(pdf), pdf, 0.0f }; | ||
| } | ||
|
|
||
| OSL_HOSTDEVICE Sample sample(const Vec3& wo, float rx, float ry, | ||
| float rz) const | ||
| { | ||
| TangentFrame frame = TangentFrame::from_normal(wo); | ||
|
|
||
| float cos_theta; | ||
| if (abs(g) < 1e-3f) { | ||
| cos_theta = 1.0f - 2.0f * rx; | ||
| } else { | ||
| float sqr_term = (1 - g * g) / (1 - g + 2 * g * rx); | ||
| cos_theta = (1 + g * g - sqr_term * sqr_term) / (2 * g); | ||
| cos_theta = OIIO::clamp(cos_theta, -1.0f, 1.0f); | ||
| } | ||
|
|
||
| float sin_theta = sqrtf( | ||
| OIIO::clamp(1.0f - cos_theta * cos_theta, 0.0f, 1.0f)); | ||
| float phi = 2 * M_PI * ry; | ||
| Vec3 local_wi = Vec3(sin_theta * cosf(phi), sin_theta * sinf(phi), | ||
| cos_theta); | ||
|
|
||
| Vec3 wi = frame.toworld(local_wi); | ||
| float pdf_val = PhaseHG(cos_theta, g); | ||
|
|
||
| return { wi, Color3(1.0f), pdf_val, 0.0f }; | ||
| } | ||
| }; | ||
|
|
||
| struct HomogeneousMedium final : public Medium { | ||
| MediumParams params; | ||
| HenyeyGreenstein phase_func; | ||
|
|
||
| OSL_HOSTDEVICE HomogeneousMedium(const MediumParams& params) | ||
| : Medium(this), params(params), phase_func(params.medium_g) | ||
| { | ||
| } | ||
|
|
||
| OSL_HOSTDEVICE BSDF::Sample sample_phase_func(const Vec3& wo, float rx, | ||
| float ry, float rz) const | ||
| { | ||
| return phase_func.sample(wo, rx, ry, rz); | ||
| } | ||
|
|
||
| OSL_HOSTDEVICE const MediumParams* get_params() const { return ¶ms; } | ||
| }; | ||
|
|
||
| struct EmptyMedium final : public Medium { | ||
| MediumParams params; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If even the empty medium needs to have MediumParams - maybe the field should be in the base class? Do we even need a class hierarchy here given that this medium tracking is only ever going to support homogeneous media? |
||
|
|
||
| OSL_HOSTDEVICE EmptyMedium(const MediumParams& params) | ||
| : Medium(this), params(params) | ||
| { | ||
| } | ||
|
|
||
| OSL_HOSTDEVICE const MediumParams* get_params() const { return ¶ms; } | ||
|
|
||
| OSL_HOSTDEVICE Medium::Sample sample(Ray& ray, Sampler& sampler, | ||
| Intersection& hit) const | ||
| { | ||
| return { 0.0f, Color3(1.0f), Color3(1.0f) }; | ||
| } | ||
|
|
||
| OSL_HOSTDEVICE BSDF::Sample sample_phase_func(const Vec3& wo, float rx, | ||
| float ry, float rz) const | ||
| { | ||
| return { Vec3(1.0f), Color3(1.0f), 0.0f, 0.0f }; | ||
| } | ||
| }; | ||
|
|
||
|
|
||
| OSL_HOSTDEVICE Color3 | ||
| evaluate_layer_opacity(const ShaderGlobalsType& sg, float path_roughness, | ||
| const ClosureColor* closure) | ||
|
|
@@ -1606,8 +1692,8 @@ evaluate_layer_opacity(const ShaderGlobalsType& sg, float path_roughness, | |
|
|
||
| OSL_HOSTDEVICE void | ||
| process_medium_closure(const ShaderGlobalsType& sg, float path_roughness, | ||
| ShadingResult& result, const ClosureColor* closure, | ||
| const Color3& w) | ||
| ShadingResult& result, MediumStack& medium_stack, | ||
| const ClosureColor* closure, const Color3& w) | ||
| { | ||
| if (!closure) | ||
| return; | ||
|
|
@@ -1649,37 +1735,72 @@ process_medium_closure(const ShaderGlobalsType& sg, float path_roughness, | |
| const ClosureComponent* comp = closure->as_comp(); | ||
| Color3 cw = weight * comp->w; | ||
| const auto& params = *comp->as<MxAnisotropicVdfParams>(); | ||
| result.sigma_t = cw * params.extinction; | ||
| result.sigma_s = params.albedo * result.sigma_t; | ||
| result.medium_g = params.anisotropy; | ||
| closure = nullptr; | ||
| result.medium_data.sigma_t = cw * params.extinction; | ||
| result.medium_data.sigma_s = params.albedo | ||
| * result.medium_data.sigma_t; | ||
| result.medium_data.medium_g = params.anisotropy; | ||
| result.medium_data.priority = 0; | ||
|
|
||
| if (!sg.backfacing) { // if entering | ||
| if (result.medium_data.is_vaccum()) { | ||
| medium_stack.add_medium<EmptyMedium>(result.medium_data); | ||
| } else { | ||
| medium_stack.add_medium<HomogeneousMedium>( | ||
| result.medium_data); | ||
| } | ||
| } | ||
|
|
||
| closure = nullptr; | ||
| break; | ||
| } | ||
| case MX_MEDIUM_VDF_ID: { | ||
| const ClosureComponent* comp = closure->as_comp(); | ||
| Color3 cw = weight * comp->w; | ||
| const auto& params = *comp->as<MxMediumVdfParams>(); | ||
| result.sigma_t = { -OIIO::fast_log(params.transmission_color.x), | ||
| -OIIO::fast_log(params.transmission_color.y), | ||
| -OIIO::fast_log(params.transmission_color.z) }; | ||
| // NOTE: closure weight scales the extinction parameter | ||
| result.sigma_t *= cw / params.transmission_depth; | ||
| result.sigma_s = params.albedo * result.sigma_t; | ||
| result.medium_g = params.anisotropy; | ||
| // TODO: properly track a medium stack here ... | ||
| result.refraction_ior = sg.backfacing ? 1.0f / params.ior | ||
| : params.ior; | ||
| result.priority = params.priority; | ||
| closure = nullptr; | ||
|
|
||
| result.medium_data.sigma_t | ||
| = Color3(-OIIO::fast_log(params.transmission_color.x), | ||
| -OIIO::fast_log(params.transmission_color.y), | ||
| -OIIO::fast_log(params.transmission_color.z)); | ||
|
|
||
| result.medium_data.sigma_t *= cw / params.transmission_depth; | ||
| result.medium_data.sigma_s = params.albedo | ||
| * result.medium_data.sigma_t; | ||
| result.medium_data.medium_g = params.anisotropy; | ||
|
|
||
| result.medium_data.refraction_ior = sg.backfacing | ||
| ? 1.0f / params.ior | ||
| : params.ior; | ||
| result.medium_data.priority = params.priority; | ||
|
|
||
| if (!sg.backfacing) { // if entering | ||
| if (result.medium_data.is_vaccum()) { | ||
| medium_stack.add_medium<EmptyMedium>(result.medium_data); | ||
| } else { | ||
| medium_stack.add_medium<HomogeneousMedium>( | ||
| result.medium_data); | ||
| } | ||
| } | ||
|
|
||
| closure = nullptr; | ||
| break; | ||
| } | ||
| case MxDielectric::closureid(): { | ||
| const ClosureComponent* comp = closure->as_comp(); | ||
| const MxDielectric::Data& params = *comp->as<MxDielectric::Data>(); | ||
| if (!is_black(weight * comp->w * params.refr_tint)) { | ||
| // TODO: properly track a medium stack here ... | ||
| result.refraction_ior = sg.backfacing ? 1.0f / params.IOR | ||
| : params.IOR; | ||
| float new_ior = sg.backfacing ? 1.0f / params.IOR : params.IOR; | ||
|
|
||
| result.medium_data.refraction_ior = new_ior; | ||
|
|
||
| const MediumParams* current_params | ||
| = medium_stack.get_current_params(); | ||
| if (current_params | ||
| && result.medium_data.priority | ||
| <= current_params->priority) { | ||
| result.medium_data.refraction_ior | ||
| = current_params->refraction_ior; | ||
| } | ||
| } | ||
| closure = nullptr; | ||
| break; | ||
|
|
@@ -1688,13 +1809,23 @@ process_medium_closure(const ShaderGlobalsType& sg, float path_roughness, | |
| const ClosureComponent* comp = closure->as_comp(); | ||
| const auto& params = *comp->as<MxGeneralizedSchlickParams>(); | ||
| if (!is_black(weight * comp->w * params.transmission_tint)) { | ||
| // TODO: properly track a medium stack here ... | ||
| float avg_F0 = clamp((params.f0.x + params.f0.y + params.f0.z) | ||
| / 3.0f, | ||
| 0.0f, 0.99f); | ||
| float sqrt_F0 = sqrtf(avg_F0); | ||
| float ior = (1 + sqrt_F0) / (1 - sqrt_F0); | ||
| result.refraction_ior = sg.backfacing ? 1.0f / ior : ior; | ||
| float new_ior = sg.backfacing ? 1.0f / ior : ior; | ||
|
|
||
| result.medium_data.refraction_ior = new_ior; | ||
|
|
||
| const MediumParams* current_params | ||
| = medium_stack.get_current_params(); | ||
| if (current_params | ||
| && result.medium_data.priority | ||
| <= current_params->priority) { | ||
| result.medium_data.refraction_ior | ||
| = current_params->refraction_ior; | ||
| } | ||
| } | ||
| closure = nullptr; | ||
| break; | ||
|
|
@@ -1711,8 +1842,9 @@ process_medium_closure(const ShaderGlobalsType& sg, float path_roughness, | |
| // recursively walk through the closure tree, creating bsdfs as we go | ||
| OSL_HOSTDEVICE void | ||
| process_bsdf_closure(const ShaderGlobalsType& sg, float path_roughness, | ||
| ShadingResult& result, const ClosureColor* closure, | ||
| const Color3& w, bool light_only) | ||
| ShadingResult& result, MediumStack& medium_stack, | ||
| const ClosureColor* closure, const Color3& w, | ||
| bool light_only) | ||
| { | ||
| static const ustringhash uh_ggx("ggx"); | ||
| static const ustringhash uh_beckmann("beckmann"); | ||
|
|
@@ -1845,9 +1977,16 @@ process_bsdf_closure(const ShaderGlobalsType& sg, float path_roughness, | |
| case MxDielectric::closureid(): { | ||
| const MxDielectric::Data& params | ||
| = *comp->as<MxDielectric::Data>(); | ||
| ok = result.bsdf.add_bsdf<MxDielectric>(cw, params, -sg.I, | ||
| sg.backfacing, | ||
| path_roughness); | ||
|
|
||
| if (medium_stack.false_intersection_with( | ||
| result.medium_data)) { | ||
| ok = result.bsdf.add_bsdf<Transparent>(cw); | ||
| } else { | ||
| ok = result.bsdf.add_bsdf<MxDielectric>(cw, params, | ||
| -sg.I, | ||
| sg.backfacing, | ||
| path_roughness); | ||
| } | ||
| break; | ||
| } | ||
| case MxConductor::closureid(): { | ||
|
|
@@ -1860,15 +1999,21 @@ process_bsdf_closure(const ShaderGlobalsType& sg, float path_roughness, | |
| case MX_GENERALIZED_SCHLICK_ID: { | ||
| const MxGeneralizedSchlickParams& params | ||
| = *comp->as<MxGeneralizedSchlickParams>(); | ||
| if (is_black(params.transmission_tint)) | ||
| ok = result.bsdf.add_bsdf<MxMicrofacet< | ||
| MxGeneralizedSchlickParams, GGXDist, false>>(cw, | ||
| params, | ||
| 1.0f); | ||
| else | ||
| ok = result.bsdf.add_bsdf<MxMicrofacet< | ||
| MxGeneralizedSchlickParams, GGXDist, true>>( | ||
| cw, params, result.refraction_ior); | ||
|
|
||
| if (medium_stack.false_intersection_with( | ||
| result.medium_data)) { | ||
| ok = result.bsdf.add_bsdf<Transparent>(cw); | ||
| } else { | ||
| if (is_black(params.transmission_tint)) { | ||
| ok = result.bsdf.add_bsdf<MxMicrofacet< | ||
| MxGeneralizedSchlickParams, GGXDist, false>>( | ||
| cw, params, 1.0f); | ||
| } else { | ||
| ok = result.bsdf.add_bsdf<MxMicrofacet< | ||
| MxGeneralizedSchlickParams, GGXDist, true>>( | ||
| cw, params, result.medium_data.refraction_ior); | ||
| } | ||
| } | ||
| break; | ||
| }; | ||
| case MX_TRANSLUCENT_ID: { | ||
|
|
@@ -1957,11 +2102,14 @@ process_bsdf_closure(const ShaderGlobalsType& sg, float path_roughness, | |
|
|
||
| OSL_HOSTDEVICE void | ||
| process_closure(const ShaderGlobalsType& sg, float path_roughness, | ||
| ShadingResult& result, const ClosureColor* Ci, bool light_only) | ||
| ShadingResult& result, MediumStack& medium_stack, | ||
| const ClosureColor* Ci, bool light_only) | ||
| { | ||
| if (!light_only) | ||
| process_medium_closure(sg, path_roughness, result, Ci, Color3(1)); | ||
| process_bsdf_closure(sg, path_roughness, result, Ci, Color3(1), light_only); | ||
| process_medium_closure(sg, path_roughness, result, medium_stack, Ci, | ||
| Color3(1)); | ||
| process_bsdf_closure(sg, path_roughness, result, medium_stack, Ci, | ||
| Color3(1), light_only); | ||
| } | ||
|
|
||
| OSL_HOSTDEVICE Vec3 | ||
|
|
@@ -2022,5 +2170,19 @@ BSDF::sample_vrtl(const Vec3& wo, float rx, float ry, float rz) const | |
| return dispatch([&](auto bsdf) { return bsdf.sample(wo, rx, ry, rz); }); | ||
| } | ||
|
|
||
| OSL_HOSTDEVICE BSDF::Sample | ||
| Medium::sample_phase_func_vrtl(const Vec3& wo, float rx, float ry, | ||
| float rz) const | ||
| { | ||
| return dispatch([&](const auto& medium) { | ||
| return medium.sample_phase_func(wo, rx, ry, rz); | ||
| }); | ||
| } | ||
|
|
||
| OSL_HOSTDEVICE const MediumParams* | ||
| Medium::get_params_vrtl() const | ||
| { | ||
| return dispatch([&](const auto& medium) { return medium.get_params(); }); | ||
| } | ||
|
|
||
| OSL_NAMESPACE_END | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is already an implementation of this in the BSDL headers - is it possible to re-use that one?