2023-06-14 16:52:36 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0 */
|
2021-10-07 17:27:22 +02:00
|
|
|
|
2022-09-21 17:58:34 +02:00
|
|
|
#include "kernel/integrator/guiding.h"
|
|
|
|
|
|
2021-10-07 17:27:22 +02:00
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
|
|
/* BSSRDF using disk based importance sampling.
|
|
|
|
|
*
|
|
|
|
|
* BSSRDF Importance Sampling, SIGGRAPH 2013
|
|
|
|
|
* http://library.imageworks.com/pdfs/imageworks-library-BSSRDF-sampling.pdf
|
|
|
|
|
*/
|
|
|
|
|
|
2022-07-29 13:41:37 +02:00
|
|
|
ccl_device_inline Spectrum subsurface_disk_eval(const Spectrum radius, float disk_r, float r)
|
2021-10-07 17:27:22 +02:00
|
|
|
{
|
2022-07-29 13:41:37 +02:00
|
|
|
const Spectrum eval = bssrdf_eval(radius, r);
|
2021-10-07 17:27:22 +02:00
|
|
|
const float pdf = bssrdf_pdf(radius, disk_r);
|
2022-07-29 13:41:37 +02:00
|
|
|
return (pdf > 0.0f) ? eval / pdf : zero_spectrum();
|
2021-10-07 17:27:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Subsurface scattering step, from a point on the surface to other
|
|
|
|
|
* nearby points on the same object. */
|
2021-10-17 16:10:10 +02:00
|
|
|
ccl_device_inline bool subsurface_disk(KernelGlobals kg,
|
|
|
|
|
IntegratorState state,
|
2021-10-07 17:27:22 +02:00
|
|
|
RNGState rng_state,
|
Cycles: Kernel address space changes for MSL
This is the first of a sequence of changes to support compiling Cycles kernels as MSL (Metal Shading Language) in preparation for a Metal GPU device implementation.
MSL requires that all pointer types be declared with explicit address space attributes (device, thread, etc...). There is already precedent for this with Cycles' address space macros (ccl_global, ccl_private, etc...), therefore the first step of MSL-enablement is to apply these consistently. Line-for-line this represents the largest change required to enable MSL. Applying this change first will simplify future patches as well as offering the emergent benefit of enhanced descriptiveness.
The vast majority of deltas in this patch fall into one of two cases:
- Ensuring ccl_private is specified for thread-local pointer types
- Ensuring ccl_global is specified for device-wide pointer types
Additionally, the ccl_addr_space qualifier can be removed. Prior to Cycles X, ccl_addr_space was used as a context-dependent address space qualifier, but now it is either redundant (e.g. in struct typedefs), or can be replaced by ccl_global in the case of pointer types. Associated function variants (e.g. lcg_step_float_addrspace) are also redundant.
In cases where address space qualifiers are chained with "const", this patch places the address space qualifier first. The rationale for this is that the choice of address space is likely to have the greater impact on runtime performance and overall architecture.
The final part of this patch is the addition of a metal/compat.h header. This is partially complete and will be extended in future patches, paving the way for the full Metal implementation.
Ref T92212
Reviewed By: brecht
Maniphest Tasks: T92212
Differential Revision: https://developer.blender.org/D12864
2021-10-14 13:53:40 +01:00
|
|
|
ccl_private Ray &ray,
|
|
|
|
|
ccl_private LocalIntersection &ss_isect)
|
2021-10-07 17:27:22 +02:00
|
|
|
|
|
|
|
|
{
|
2022-09-01 01:28:58 +02:00
|
|
|
float2 rand_disk = path_state_rng_2D(kg, &rng_state, PRNG_SUBSURFACE_DISK);
|
2021-10-07 17:27:22 +02:00
|
|
|
|
|
|
|
|
/* Read shading point info from integrator state. */
|
2021-10-17 16:10:10 +02:00
|
|
|
const float3 P = INTEGRATOR_STATE(state, ray, P);
|
|
|
|
|
const float ray_dP = INTEGRATOR_STATE(state, ray, dP);
|
|
|
|
|
const float time = INTEGRATOR_STATE(state, ray, time);
|
Cycles: Tweak Principled BSDF Subsurface parameters
Previously, the Principled BSDF used the Subsurface input to scale the radius.
When it was zero, it used a diffuse closure, otherwise a subsurface closure.
This sort of scaling input makes sense, but it should be specified in distance
units, rather than a 0..1 factor, so this commit changes the unit and renames
the input to Subsurface Scale.
Additionally, it adds support for mixing diffuse and subsurface components.
This is part of e.g. the OpenPBR spec, and the logic behind it is to support
modeling e.g. dirt or paint on top of skin. Before, materials would be either
fully diffuse (radius=0) or fully subsurface.
For typical materials, this mixing factor will be either zero or one
(just like metallic or transmission), but supporting fractional inputs makes
sense for e.g. smooth transitions at boundaries.
Another change is that there is no separate Subsurface Color anymore - before,
this was mixed with the Base Color using the Subsurface input as the factor,
but this was not really useful since that input was generally very small.
And finally, the handling of how the path enters the material for random walk
subsurface scattering is changed. Before, this always used lambertian (diffuse)
transmission, but this caused some problems, like overly white edges.
Instead, two different methods are now used, depending on the selected mode.
In Fixed Radius mode, the code assumes a simple medium boundary, and performs
refraction into the material using the main Roughness and IOR inputs.
Meanwhile, when not using Fixed Radius, the code assumes a more complex
boundary (as typically found on organic materials, e.g. skin), so the entry
bounce has a 50/50 chance of being either diffuse transmission or refraction
using the separate Subsurface IOR input and a fixed roughness of 1.
Credit for this method goes to Christophe Hery.
Pull Request: https://projects.blender.org/blender/blender/pulls/110989
2023-09-13 02:45:33 +02:00
|
|
|
const float3 Ng = INTEGRATOR_STATE(state, subsurface, N);
|
2021-10-17 16:10:10 +02:00
|
|
|
const int object = INTEGRATOR_STATE(state, isect, object);
|
2021-11-16 19:34:13 +01:00
|
|
|
const uint32_t path_flag = INTEGRATOR_STATE(state, path, flag);
|
2021-10-07 17:27:22 +02:00
|
|
|
|
|
|
|
|
/* Read subsurface scattering parameters. */
|
2022-07-29 13:41:37 +02:00
|
|
|
const Spectrum radius = INTEGRATOR_STATE(state, subsurface, radius);
|
2021-10-07 17:27:22 +02:00
|
|
|
|
|
|
|
|
/* Pick random axis in local frame and point on disk. */
|
|
|
|
|
float3 disk_N, disk_T, disk_B;
|
|
|
|
|
float pick_pdf_N, pick_pdf_T, pick_pdf_B;
|
|
|
|
|
|
|
|
|
|
disk_N = Ng;
|
|
|
|
|
make_orthonormals(disk_N, &disk_T, &disk_B);
|
|
|
|
|
|
2022-09-01 01:28:58 +02:00
|
|
|
if (rand_disk.y < 0.5f) {
|
2021-10-07 17:27:22 +02:00
|
|
|
pick_pdf_N = 0.5f;
|
|
|
|
|
pick_pdf_T = 0.25f;
|
|
|
|
|
pick_pdf_B = 0.25f;
|
2022-09-01 01:28:58 +02:00
|
|
|
rand_disk.y *= 2.0f;
|
2021-10-07 17:27:22 +02:00
|
|
|
}
|
2022-09-01 01:28:58 +02:00
|
|
|
else if (rand_disk.y < 0.75f) {
|
2021-10-07 17:27:22 +02:00
|
|
|
float3 tmp = disk_N;
|
|
|
|
|
disk_N = disk_T;
|
|
|
|
|
disk_T = tmp;
|
|
|
|
|
pick_pdf_N = 0.25f;
|
|
|
|
|
pick_pdf_T = 0.5f;
|
|
|
|
|
pick_pdf_B = 0.25f;
|
2022-09-01 01:28:58 +02:00
|
|
|
rand_disk.y = (rand_disk.y - 0.5f) * 4.0f;
|
2021-10-07 17:27:22 +02:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
float3 tmp = disk_N;
|
|
|
|
|
disk_N = disk_B;
|
|
|
|
|
disk_B = tmp;
|
|
|
|
|
pick_pdf_N = 0.25f;
|
|
|
|
|
pick_pdf_T = 0.25f;
|
|
|
|
|
pick_pdf_B = 0.5f;
|
2022-09-01 01:28:58 +02:00
|
|
|
rand_disk.y = (rand_disk.y - 0.75f) * 4.0f;
|
2021-10-07 17:27:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Sample point on disk. */
|
2022-09-01 01:28:58 +02:00
|
|
|
float phi = M_2PI_F * rand_disk.y;
|
2021-10-07 17:27:22 +02:00
|
|
|
float disk_height, disk_r;
|
|
|
|
|
|
2022-09-01 01:28:58 +02:00
|
|
|
bssrdf_sample(radius, rand_disk.x, &disk_r, &disk_height);
|
2021-10-07 17:27:22 +02:00
|
|
|
|
|
|
|
|
float3 disk_P = (disk_r * cosf(phi)) * disk_T + (disk_r * sinf(phi)) * disk_B;
|
|
|
|
|
|
|
|
|
|
/* Create ray. */
|
|
|
|
|
ray.P = P + disk_N * disk_height + disk_P;
|
|
|
|
|
ray.D = -disk_N;
|
2022-07-13 16:54:53 +02:00
|
|
|
ray.tmin = 0.0f;
|
|
|
|
|
ray.tmax = 2.0f * disk_height;
|
2021-10-07 17:27:22 +02:00
|
|
|
ray.dP = ray_dP;
|
|
|
|
|
ray.dD = differential_zero_compact();
|
|
|
|
|
ray.time = time;
|
2022-01-13 17:20:50 +01:00
|
|
|
ray.self.object = OBJECT_NONE;
|
|
|
|
|
ray.self.prim = PRIM_NONE;
|
|
|
|
|
ray.self.light_object = OBJECT_NONE;
|
2023-05-24 13:36:13 +02:00
|
|
|
ray.self.light_prim = PRIM_NONE;
|
|
|
|
|
ray.self.light = LAMP_NONE;
|
2021-10-07 17:27:22 +02:00
|
|
|
|
|
|
|
|
/* Intersect with the same object. if multiple intersections are found it
|
|
|
|
|
* will use at most BSSRDF_MAX_HITS hits, a random subset of all hits. */
|
|
|
|
|
uint lcg_state = lcg_state_init(
|
|
|
|
|
rng_state.rng_hash, rng_state.rng_offset, rng_state.sample, 0x68bc21eb);
|
|
|
|
|
const int max_hits = BSSRDF_MAX_HITS;
|
|
|
|
|
|
|
|
|
|
scene_intersect_local(kg, &ray, &ss_isect, object, &lcg_state, max_hits);
|
|
|
|
|
const int num_eval_hits = min(ss_isect.num_hits, max_hits);
|
|
|
|
|
if (num_eval_hits == 0) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Sort for consistent renders between CPU and GPU, independent of the BVH
|
|
|
|
|
* traversal algorithm. */
|
|
|
|
|
sort_intersections_and_normals(ss_isect.hits, ss_isect.Ng, num_eval_hits);
|
|
|
|
|
|
2022-07-29 13:41:37 +02:00
|
|
|
Spectrum weights[BSSRDF_MAX_HITS]; /* TODO: zero? */
|
2021-10-07 17:27:22 +02:00
|
|
|
float sum_weights = 0.0f;
|
|
|
|
|
|
|
|
|
|
for (int hit = 0; hit < num_eval_hits; hit++) {
|
|
|
|
|
/* Get geometric normal. */
|
|
|
|
|
const int object = ss_isect.hits[hit].object;
|
2022-06-17 17:16:37 +02:00
|
|
|
const int object_flag = kernel_data_fetch(object_flag, object);
|
2021-10-07 17:27:22 +02:00
|
|
|
float3 hit_Ng = ss_isect.Ng[hit];
|
2021-11-16 19:34:13 +01:00
|
|
|
if (path_flag & PATH_RAY_SUBSURFACE_BACKFACING) {
|
|
|
|
|
hit_Ng = -hit_Ng;
|
|
|
|
|
}
|
Fix T89037: Cycles: Backfacing node can be wrong for lights with negative scale
When rendering in the viewport (or probably on instanced objects, but I didn't
test that), emissive objects whose scale is negative give the wrong value on the
"backfacing" input when multiple sampling is enabled.
The underlying problem was a corner case in how normal transformation is handled,
which is generally a bit messy.
From what I can tell, the pattern appears to be:
- If you first transform vertices to world space and then compute the normal from
them (as triangle light samping, MNEE and light tree do), you need to flip
whenever the transform has negative scale regardless of whether the transform
has been applied
- If you compute the normal in object space and then transform it to world space
(as the regular shader_setup_from_ray path does), you only need to flip if the
transform was already applied and was negative
- If you get the normal from a local intersection result (as bevel and SSS do),
you only need to flip if the transform was already applied and was negative
- If you get the normal from vertex normals, you don't need to do anything since
the host-side code does the flip for you (arguably it'd be more consistent to
do this in the kernel as well, but meh, not worth the potential slowdown)
So, this patch fixes the logic in the triangle emission code.
Also, turns out that the MNEE code had the same problem and was also having
problems in the viewport on negative-scale objects, this is also fixed now.
Differential Revision: https://developer.blender.org/D16952
2023-01-10 02:32:16 +01:00
|
|
|
if (object_negative_scale_applied(object_flag)) {
|
2021-10-07 17:27:22 +02:00
|
|
|
hit_Ng = -hit_Ng;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!(object_flag & SD_OBJECT_TRANSFORM_APPLIED)) {
|
2021-10-28 21:49:32 +02:00
|
|
|
/* Transform normal to world space. */
|
2021-10-07 17:27:22 +02:00
|
|
|
Transform itfm;
|
2022-07-25 14:50:33 +02:00
|
|
|
object_fetch_transform_motion_test(kg, object, time, &itfm);
|
2021-10-07 17:27:22 +02:00
|
|
|
hit_Ng = normalize(transform_direction_transposed(&itfm, hit_Ng));
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-28 21:49:32 +02:00
|
|
|
/* Quickly retrieve P and Ng without setting up ShaderData. */
|
|
|
|
|
const float3 hit_P = ray.P + ray.D * ss_isect.hits[hit].t;
|
|
|
|
|
|
2021-10-07 17:27:22 +02:00
|
|
|
/* Probability densities for local frame axes. */
|
|
|
|
|
const float pdf_N = pick_pdf_N * fabsf(dot(disk_N, hit_Ng));
|
|
|
|
|
const float pdf_T = pick_pdf_T * fabsf(dot(disk_T, hit_Ng));
|
|
|
|
|
const float pdf_B = pick_pdf_B * fabsf(dot(disk_B, hit_Ng));
|
|
|
|
|
|
|
|
|
|
/* Multiple importance sample between 3 axes, power heuristic
|
|
|
|
|
* found to be slightly better than balance heuristic. pdf_N
|
|
|
|
|
* in the MIS weight and denominator cancelled out. */
|
|
|
|
|
float w = pdf_N / (sqr(pdf_N) + sqr(pdf_T) + sqr(pdf_B));
|
|
|
|
|
if (ss_isect.num_hits > max_hits) {
|
|
|
|
|
w *= ss_isect.num_hits / (float)max_hits;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Real distance to sampled point. */
|
|
|
|
|
const float r = len(hit_P - P);
|
|
|
|
|
|
|
|
|
|
/* Evaluate profiles. */
|
2022-07-29 13:41:37 +02:00
|
|
|
const Spectrum weight = subsurface_disk_eval(radius, disk_r, r) * w;
|
2021-10-07 17:27:22 +02:00
|
|
|
|
|
|
|
|
/* Store result. */
|
|
|
|
|
ss_isect.Ng[hit] = hit_Ng;
|
|
|
|
|
weights[hit] = weight;
|
|
|
|
|
sum_weights += average(fabs(weight));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sum_weights == 0.0f) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Use importance resampling, sampling one of the hits proportional to weight. */
|
2022-09-01 01:28:58 +02:00
|
|
|
const float rand_resample = path_state_rng_1D(kg, &rng_state, PRNG_SUBSURFACE_DISK_RESAMPLE);
|
|
|
|
|
const float r = rand_resample * sum_weights;
|
2021-10-07 17:27:22 +02:00
|
|
|
float partial_sum = 0.0f;
|
|
|
|
|
|
|
|
|
|
for (int hit = 0; hit < num_eval_hits; hit++) {
|
2022-07-29 13:41:37 +02:00
|
|
|
const Spectrum weight = weights[hit];
|
2021-10-07 17:27:22 +02:00
|
|
|
const float sample_weight = average(fabs(weight));
|
|
|
|
|
float next_sum = partial_sum + sample_weight;
|
|
|
|
|
|
|
|
|
|
if (r < next_sum) {
|
|
|
|
|
/* Return exit point. */
|
2022-09-21 17:58:34 +02:00
|
|
|
const Spectrum resampled_weight = weight * sum_weights / sample_weight;
|
|
|
|
|
INTEGRATOR_STATE_WRITE(state, path, throughput) *= resampled_weight;
|
2021-10-07 17:27:22 +02:00
|
|
|
ss_isect.hits[0] = ss_isect.hits[hit];
|
|
|
|
|
ss_isect.Ng[0] = ss_isect.Ng[hit];
|
|
|
|
|
|
|
|
|
|
ray.P = ray.P + ray.D * ss_isect.hits[hit].t;
|
|
|
|
|
ray.D = ss_isect.Ng[hit];
|
2022-07-13 16:54:53 +02:00
|
|
|
ray.tmin = 0.0f;
|
|
|
|
|
ray.tmax = 1.0f;
|
2022-09-21 17:58:34 +02:00
|
|
|
|
|
|
|
|
guiding_record_bssrdf_bounce(
|
|
|
|
|
kg, state, 1.0f, Ng, -Ng, resampled_weight, INTEGRATOR_STATE(state, subsurface, albedo));
|
2021-10-07 17:27:22 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
partial_sum = next_sum;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CCL_NAMESPACE_END
|