Commit Graph

294 Commits

Author SHA1 Message Date
Weizhen Huang
350bd60b11 Fix: Cycles: Metal compilation when include is guarded behind WITH_CYCLES_DEBUG
Happens after f79cae2c59. Seems that Metal pre-processing could not
figure out that the inclusion is guarded behind `#ifdef`, so the
included file is expanded here and never again afterwards.
The pre-processing should be fixed, but for now just always include the file.

Pull Request: https://projects.blender.org/blender/blender/pulls/133336
2025-01-20 19:15:46 +01:00
Weizhen Huang
f79cae2c59 Fix: Cycles: Compilation issue with WITH_CYCLES_DEBUG enabled
Pull Request: https://projects.blender.org/blender/blender/pulls/133303
2025-01-20 16:11:38 +01:00
Brecht Van Lommel
57ff24cb99 Refactor: Cycles: Add const keyword to more function parameters
Pull Request: https://projects.blender.org/blender/blender/pulls/132361
2025-01-03 10:23:24 +01:00
Brecht Van Lommel
dd51c8660b Refactor: Cycles: Add const keyword where possible, using clang-tidy
Check was misc-const-correctness, combined with readability-isolate-declaration
as suggested by the docs.

Temporarily clang-format "QualifierAlignment: Left" was used to get consistency
with the prevailing order of keywords.

Pull Request: https://projects.blender.org/blender/blender/pulls/132361
2025-01-03 10:23:20 +01:00
Brecht Van Lommel
d0c2e68e5f Refactor: Cycles: Automated clang-tidy fixups in Cycles
* Use .empty() and .data()
* Use nullptr instead of 0
* No else after return
* Simple class member initialization
* Add override for virtual methods
* Include C++ instead of C headers
* Remove some unused includes
* Use default constructors
* Always use braces
* Consistent names in definition and declaration
* Change typedef to using

Pull Request: https://projects.blender.org/blender/blender/pulls/132361
2025-01-03 10:22:55 +01:00
Brecht Van Lommel
5c46063607 Refactor: Cycles: Make kernel headers work by themselves
Shuffle around some code and add more includes so that individual
header files compile without errors.

Pull Request: https://projects.blender.org/blender/blender/pulls/132361
2025-01-03 10:22:50 +01:00
Brecht Van Lommel
7db0bc2e64 Refactor: Cycles: Make math and type headers work by themselves
Remove separate impl.h headers, shuffle around some code and add more
includes so that individual header files compile without errors.

Pull Request: https://projects.blender.org/blender/blender/pulls/132361
2025-01-03 10:22:47 +01:00
Brecht Van Lommel
3c2a6fbb9c Refactor: Cycles: Use nullptr instead of NULL
Pull Request: https://projects.blender.org/blender/blender/pulls/132361
2025-01-03 10:22:43 +01:00
Sergey Sharybin
36aa819396 Fix: Uninitialized last intersection type in Cycles
It is unknown to cause any actual problems, but it makes it harder
to read certain debug logs (like the ones from valgrind).

Pull Request: https://projects.blender.org/blender/blender/pulls/132450
2024-12-31 10:26:21 +01:00
Thomas Dinges
1be75e86aa Cleanup: replace floatX_to_floatY() with make_floatY()
Now that function overloads are usable on all GPUs, replace the former explicit functions.

Pull Request: https://projects.blender.org/blender/blender/pulls/132067
2024-12-19 09:41:55 +01:00
Weizhen Huang
27fc091be8 Fix #131723: Cycles volume not sampling channels with zero extinction
The original paper uses the single scattering albedo `sigma_s/sigma_t`
to pick a channel for sampling the scattering distance. However, this
only considers the situation where there is scattering inside the volume.
If some channel has an extinction coefficient of zero, the light passes
through without attenuation for that channel. We assign such channel
with a weight of 1 instead of 0 to make sure it can be sampled.

Pull Request: https://projects.blender.org/blender/blender/pulls/131741
2024-12-13 10:27:53 +01:00
Weizhen Huang
bb3b8d78c2 Refactor: Cycles: split volume integration into smaller functions
Pull Request: https://projects.blender.org/blender/blender/pulls/131414
2024-12-06 16:23:04 +01:00
Weizhen Huang
59ad6d2b9c Refactor: Cycles: Extract code block to check homogeneous volume into a function 2024-12-06 16:23:00 +01:00
Weizhen Huang
13fb28581b Refactor: Cycles: Share function between volume scattering and shadowing 2024-12-06 16:23:00 +01:00
Weizhen Huang
910c2e2ba6 Refactor: Cycles: Add helper struct for stepping through the volume 2024-12-06 16:23:00 +01:00
Weizhen Huang
b16cfd2a87 Cleanup: Cycles: remove unused parameter absorption_only
`!vstate.absorption_only` is always false
2024-12-06 16:23:00 +01:00
Weizhen Huang
aa09169e0a Cleanup: Cycles: remove unused parameter skip_phase in volume
This logic is copied from surface shader, so that the sampled closure
does not need to be evaluated twice when summing all the closures, but
it is not used in volume.
2024-11-28 15:56:28 +01:00
Weizhen Huang
93a34b1077 Refactor: Cycles: add helper struct Interval
To improve readability

Pull Request: https://projects.blender.org/blender/blender/pulls/130156
2024-11-12 12:06:09 +01:00
Weizhen Huang
675e8173fa Refactor: Cycles: separate volume stack and single entry evaluation
So that volume shader evaluation does not rely on the volume stack.
In the future this is useful for baking the density when building the
volume Octree.

Pull Request: https://projects.blender.org/blender/blender/pulls/130157
2024-11-12 12:05:37 +01:00
Weizhen Huang
ec3128ee37 Cleanup: Cycles: Remove unused functions
Wasn't used even when they were added
2024-11-11 16:42:50 +01:00
Alaska
e0cd45d04a Cleanup: Readd important details to Cycles ray offsetting TODO
The ray offsetting triangle tests are not numerically identical to
those found in custom BVH implementations.
There was a TODO to fix this, but there was no explaination for why
it should be done. This fixes that.
2024-10-11 02:41:20 +13:00
Lukas Stockner
b8d0bef3b4 Cleanup: Cycles: Consolidate coordinate system conversions
- Deduplicate Fisheye projection code
- Replace spherical/cartesian conversions with shared helpers
- Replace transforms from/to local coordinate systems with shared helpers

The main type of repeated transform that's not covered here is `to/from_coords`, but with separate values for xy and z (e.g. BSDFs that already computed `dot(wi, N)` earlier, so they only need `dot(wi, X)` and `dot(wi, Y)` later). Could also be replaced, but it would feel weirdly specific for a helper function.

Pull Request: https://projects.blender.org/blender/blender/pulls/125999
2024-10-07 02:18:49 +02:00
Alexandre Cardaillac
0315eae536 Cycles: Add more scattering phase functions
Previously, Cycles only supported the Henyey-Greenstein phase function for volume scattering.
While HG is flexible and works for a wide range of effects, sometimes a more physically accurate
phase function may be needed for realism.

Therefore, this adds three new phase functions to the code:
Rayleigh: For particles with a size below the wavelength of light, mostly athmospheric scattering.
Fournier-Forand: For realistic underwater scattering.
Draine: Fairly specific on its own (mostly for interstellar dust), but useful for the next entry.
Mie: Approximates Mie scattering in water droplets using a mix of Draine and HG phase functions.

These phase functions can be combined using Mix nodes as usual.

Co-authored-by: Lukas Stockner <lukas@lukasstockner.de>
Pull Request: https://projects.blender.org/blender/blender/pulls/123532
2024-10-02 11:12:53 +02:00
Weizhen Huang
ee2fe7fa6c Fix: Cycles: reuse random number for sampling color channel in volume
The same random number was used for sampling color channel at each step,
which leads to bias. Fixed by rescaling the random number.

Another possibility would be to scramble `rng_offset` and use a new
random number each time, similar as in subsurface scattering, but
rescaling random number should be faster than computing a new one, and
is favorable here since the precision here is not very important

Pull Request: https://projects.blender.org/blender/blender/pulls/127454
2024-09-12 14:27:56 +02:00
Weizhen Huang
76700680d0 Cleanup: Cycles: rename variable for clarity
rename `rphase` to `rchannel` as this variable is only used for sampling
a color channel
2024-09-11 14:31:48 +02:00
Weizhen Huang
be0d2e19b5 Fix #115998: Cycles volume too dark when shadow ray visibility is off
when tracing shadow ray through a volume and no hit is registered, we
consider the whole ray segment inside the volume.

However, no hit registered could also happen when the volume is
invisible to shadow ray. We should explicitly check this case and skip
rendering the volume segment instead.

Pull Request: https://projects.blender.org/blender/blender/pulls/126139
2024-08-19 15:52:35 +02:00
Alaska
6b522c5e30 Fix #125307: Precision issues with MetalRT
Reverts a change in 5508b41a40 that
disabled ray offsetting on MetalRT, which lead to rendering artifacts
in scenes far away from the origin.

Pull Request: https://projects.blender.org/blender/blender/pulls/126003
2024-08-07 17:20:09 +02:00
Weizhen Huang
e981389bdd Refactor: Cycles: use reservoir sampling to pick phase function in volume
so that we loop through the volumes only once.

Pull Request: https://projects.blender.org/blender/blender/pulls/125676
2024-08-05 10:55:47 +02:00
Weizhen Huang
7e40d567d4 Fix #125595: Cycles artifacts in overlapping volumes with different phase functions
A phase function is normalized over the sphere, it is therefore
incorrect to sum two phase functions together when evaluating for NEE.
It should be a weighted sum with normalized weights, which, according to
`volume_shader_phase_pick()`, is `sample_weight / sum_sample_weight`.

Also corrects an error in `volume_shader_phase_pick()`.
2024-08-05 10:55:44 +02:00
Campbell Barton
99af19932e Cleanup: spelling in comments 2024-07-30 12:38:16 +10:00
Campbell Barton
9fb0d3c3ef Cleanup: spelling in comments 2024-07-13 16:56:57 +10:00
Weizhen Huang
4a4270d73c Merge branch 'blender-v4.2-release' 2024-07-08 16:19:41 +02:00
Weizhen Huang
204b99dd3f Fix #124241: Light-linked objects render incorrectly in volumes
the object in volume stack should be used instead of `isect.object`.

NOTE: this solution does not work for overlapping volumes. But since
light linking of overlapping volumes did not work before, it should be
fine to implement this partial solution. We read the bottom of the stack
instead of the top to avoid looping through the entire stack.

Pull Request: https://projects.blender.org/blender/blender/pulls/124341
2024-07-08 16:17:39 +02:00
Weizhen Huang
e49fda3ff8 Merge branch 'blender-v4.2-release' 2024-06-25 18:50:54 +02:00
Weizhen Huang
02e6985c62 Fix #94323: Cycles blocky artifacts in overlapping volumes due to scale difference
when computing coefficients in volume, the volume density of the object
at the top of the stack is used, which leads to wrong result if
overlapping volumes have different scales.
This commit fixes the problem by pre-multiplying the volume density per
object when evaluating the shader.

Pull Request: https://projects.blender.org/blender/blender/pulls/123733
2024-06-25 18:49:26 +02:00
Harley Acheson
52f42b02f0 Merge branch 'blender-v4.2-release' 2024-06-13 10:55:09 -07:00
Brecht Van Lommel
d72c4f0096 Fix: Cycles build issues when disabling various kernel features 2024-06-13 19:41:19 +02:00
Weizhen Huang
98f9553199 Merge branch 'blender-v4.2-release' 2024-06-10 16:11:55 +02:00
Weizhen Huang
f9a75d1e40 Fix #122976: Cycles light linking broken for volumes with light tree
Light linking was never working correctly in volume segment with light
tree, because `sd->object` was not assigned, thus
`light_link_receiver_nee(kg, sd)` always returned `OBJECT_NONE`, causing
the light tree sample to fail. This problem was revealed by fdc2962beb
since now the same light is used for volume segment and volume.

Also ensure we don't sample position on the light if sampling from
volume segment is failed, by setting `emitter_id` to `EMITTER_NONE` in
such cases.

Pull Request: https://projects.blender.org/blender/blender/pulls/122999
2024-06-10 16:11:12 +02:00
Campbell Barton
d98a7a7756 Merge branch 'blender-v4.2-release' 2024-06-06 10:23:16 +10:00
Campbell Barton
7f7648c6ed Cleanup: spelling in code comments & minor edits
- Use uppercase NOTE: tags.
- Correct bNote -> bNode.
- Use colon after parameters.
- Use doxy-style doc-strings.
2024-06-06 09:55:13 +10:00
Lukas Stockner
db991d8ac0 Cycles: Only store per-pixel seed for vertex color baking
Currently, during baking each pixel stores a seed input that comes from the
Blender side. This is only needed for vertex color baking, however -
for regular image baking, we can just as well hash the pixel coordinates.

Therefore, we can save some memory (4 byte per pixel) by splitting the seed
info out into a separate pass and only storing it when needed.

Pull Request: https://projects.blender.org/blender/blender/pulls/122806
2024-06-06 00:45:38 +02:00
Lukas Stockner
5246fb5a57 Cycles: Implement blue-noise dithered sampling
This patch implements blue-noise dithered sampling as described by Nathan Vegdahl (https://psychopath.io/post/2022_07_24_owen_scrambling_based_dithered_blue_noise_sampling), which in turn is based on "Screen-Space Blue-Noise Diffusion of Monte Carlo Sampling Error via Hierarchical Ordering of Pixels"(https://repository.kaust.edu.sa/items/1269ae24-2596-400b-a839-e54486033a93).

The basic idea is simple: Instead of generating independent sequences for each pixel by scrambling them, we use a single sequence for the entire image, with each pixel getting one chunk of the samples. The ordering across pixels is determined by hierarchical scrambling of the pixel's position along a space-filling curve, which ends up being pretty much the same operation as already used for the underlying sequence.

This results in a more high-frequency noise distribution, which appears smoother despite not being less noisy overall.

The main limitation at the moment is that the improvement is only clear if the full sample amount is used per pixel, so interactive preview rendering and adaptive sampling will not receive the benefit. One exception to this is that when using the new "Automatic" setting, the first sample in interactive rendering will also be blue-noise-distributed.

The sampling mode option is now exposed in the UI, with the three options being Blue Noise (the new mode), Classic (the previous Tabulated Sobol method) and the new default, Automatic (blue noise, with the additional property of ensuring the first sample is also blue-noise-distributed in interactive rendering). When debug mode is enabled, additional options appear, such as Sobol-Burley.

Note that the scrambling distance option is not compatible with the blue-noise pattern.

Pull Request: https://projects.blender.org/blender/blender/pulls/118479
2024-06-05 02:29:47 +02:00
Michael Jones
5be30b7d2b Cycles: "Struct-of-array-of-packed-structs" for parts of the integrator state
On a M3 MacBook Pro, this change increases the benchmark score by 8% (with classroom seeing a path-tracing speedup of 15%).

The integrator state is currently store using struct-of-arrays, with one array per field. Such fine grained separation can result in poor GPU cache utilisation in cases where multiple fields of the same parent struct are accessed together. This PR changes the layout of the `ray`, `isect`, `subsurface`, and `shadow_ray` structs so that the data is interleaved (per parent struct) instead of separate. To try and keep this change localised, I encapsulated the layout change by extending the integrator state access macros, however maybe we want to do this more explicitly? (e.g. by updating every bit of code that accesses these parts of the state). Feedback welcome.

Pull Request: https://projects.blender.org/blender/blender/pulls/122015
2024-06-04 14:53:30 +02:00
Sergey Sharybin
fa3eaac0ac Cycles: Limit the number of bounces in the shadow linking intersect kernel
Since the previous fix to properly support volumes and transparent objects
it became very easy to make it so the intersection loop takes all 1024
iterations to find intersections.

This change makes it so the number of intersection is limited by the max
number of volume/transparent bounces.

This should minimize possible performance impact of the previous fix.

Pull Request: https://projects.blender.org/blender/blender/pulls/122448
2024-05-29 21:53:12 +02:00
Brecht Van Lommel
5d213d78e3 Fix #122362: Shadow linking with volumes and transparency misses light
We can't do the optimization to shorten the ray when we might still need
to go through transparent surfaces or volumes to reach the light.

This issue was not light tree specific, however in the test file it was
more noticable because the light tree poorly handles some areas. This in
in turn causes MIS weights for forward path tracing to become higher,
which is where the error was.

Pull Request: https://projects.blender.org/blender/blender/pulls/122404
2024-05-29 17:53:32 +02:00
Michael Jones
5508b41a40 Cycles: MetalRT optimisations (scene_intersect_shadow + random_walk)
This PR contains optimisations and a general tidy-up of the MetalRT backend.

- Currently `scene_intersect` is used for both normal and (opaque) shadow rays, however the usage patterns are different enough to warrant specialisation. Shadow intersection tests (flagged with `PATH_RAY_SHADOW_OPAQUE`) only need a bool result, but need a larger "self" payload in order to exclude hits against target lights. By specialising we can minimise the payload size in each case (which is helps performance) and avoid some dynamic branching. This PR introduces a new `scene_intersect_shadow` function which is specialised in Metal, and currently redirects to `scene_intersect` in the other backends.

- Currently `scene_intersect_local` is implemented for worst-case payload requirements as demanded by `subsurface_disk` (where `max_hits` is 4). The random_walk case only demands 1 hit result which we can retrieve directly from the intersector object (rather than stashing it in the payload). By specialising, we significantly reduce the payload size for random_walk queries, which has a big impact on performance. Additionally, we only need to use a custom intersection function for the first ray test in a random walk (for self-primitive filtering), so this PR forces faster `opaque` intersection testing for all but the first random walk test.

- Currently `scene_intersect_volume` has a lot of redundant code to handle non-triangle primitives despite volumes only being enclosed by trimeshes. This PR removes this code.

Additionally, this PR tidies up the convoluted intersection function linking code, removes some redundant intersection handlers, and uses more consistent naming of intersection functions.

On a M3 MacBook Pro, these changes give 2-3% performance increase on typical scenes with opaque trimesh materials (e.g. barbershop, classroom junkshop), but can give over 15% performance increase for certain scenes using random walk SSS (e.g. monster).

Pull Request: https://projects.blender.org/blender/blender/pulls/121397
2024-05-10 16:38:02 +02:00
Lukas Stockner
17f2cdd104 Cycles: Add thin film iridescence to Principled BSDF
This is an implementation of thin film iridescence in the Principled BSDF based on "A Practical Extension to Microfacet Theory for the Modeling of Varying Iridescence".

There are still several open topics that are left for future work:
- Currently, the thin film only affects dielectric Fresnel, not metallic. Properly specifying thin films on metals requires a proper conductive Fresnel term with complex IOR inputs, any attempt of trying to hack it into the F82 model we currently use for the Principled BSDF is fundamentally flawed. In the future, we'll add a node for proper conductive Fresnel, including thin films.
- The F0/F90 control is not very elegantly implemented right now. It fundamentally works, but enabling thin film while using a Specular Tint causes a jump in appearance since the models integrate it differently. Then again, thin film interference is a physical effect, so of course a non-physical tweak doesn't play nicely with it.
- The white point handling is currently quite crude. In short: The code computes XYZ values of the reflectance spectrum, but we'd need the XYZ values of the product of the reflectance spectrum and the neutral illuminant of the working color space. Currently, this is addressed by just dividing by the XYZ values of the illuminant, but it would be better to do a proper chromatic adaptation transform or to use the proper reference curves for the working space instead of the XYZ curves from the paper.

Pull Request: https://projects.blender.org/blender/blender/pulls/118477
2024-05-02 14:28:44 +02:00
Michael Jones
99f5433445 Cycles: Dormant fixes for adaptive feature compilation
This PR fixes the (currently unused) scene-based selective feature compilation macros. These feature based macros haven't been used for a few years, and enabling them currently results in compilation errors.

The only functional change in this PR is in geom/primitive.h where undef-ing `__HAIR__` had exposed an inconsistency in how pointcloud attributes were being fetched. Using the more general `primitive_surface_attribute_float4` (instead of `curve_attribute_float4`) fixed a compilation error that occurred when rendering pointcloud unit test scenes with adaptive compilation enabled.

Pull Request: https://projects.blender.org/blender/blender/pulls/121216
2024-04-30 12:56:22 +02:00
David Murmann
ee51f643b0 Cycles: Ray Portal BSDF
Transport rays that enter to another location in the scene, with
specified ray position and normal. This may be used to render portals
for visual effects, and other production rendering tricks.

This acts much like a Transparent BSDF. Render passes are passed
through, and this is affected by light path max transparent bounces.

Pull Request: https://projects.blender.org/blender/blender/pulls/114386
2024-04-29 12:37:51 +02:00