Commit Graph

307 Commits

Author SHA1 Message Date
Sebastian Herholz
5abf42012d Cycles: Guiding cleaning up and refactoring the guiding code
In detail:
- Direct accesses of state attributes are replaced with the INTEGRATOR_STATE and INTEGRATOR_STATE_WRITE macros.
- Unified the checks for the __PATH_GUIDING define to use #  if defined (__PATH_GUIDING__).
- Even if __PATH_GUIDING__ is defined, we now check if the feature is enabled using if ((kernel_data.kernel_features & KERNEL_FEATURE_PATH_GUIDING)) {. This is important for later GPU ports.
- The kernel usage of the guiding field, surface, and volume sampling distributions is wrapped behind macros for each specific device (atm only CPU). This will make it easier for a GPU port later.
2025-05-22 13:46:30 +02:00
Weizhen Huang
64dc9cc98c Fix: Cycles: Inconsistency in transparent bounces for NEE and forward path
Note: this is a partial fix, that makes NEE and forward path consistent
only when `max_transparent_bounce > 0`. It is much more involved to make
forward path tracing support a max transparent bounce of 0, but since we
don't expect people to set up a very low number of transparent bounces,
it is less important to support that specific case.

Pull Request: https://projects.blender.org/blender/blender/pulls/138098
2025-05-05 18:38:02 +02:00
Weizhen Huang
3021d34b8c Cleanup: remove unused volume_shadow_homogeneous() function
Pull Request: https://projects.blender.org/blender/blender/pulls/138342
2025-05-05 18:37:19 +02:00
Weizhen Huang
1e394f7973 Cleanup: Cycles: Fix typo 2025-05-05 18:35:24 +02:00
Lukas Stockner
bf412ed9dd Cycles: Support for custom OSL cameras
This allows users to implement arbitrary camera models using OSL by writing
shaders that take an image position as input and compute ray origin and
direction.

The obvious applications for this are e.g. panorama modes, lens distortion
models and realistic lens simulation, but the possibilities are endless.

Currently, this is only supported on devices with OSL support, so CPU and
OptiX. However, it is independent from the shading model used, so custom
cameras can be used without getting the performance hit of OSL shading.

A few samples are provided as Text Editor templates.

One notable current limitation (in addition to the limited device support)
is that inverse mapping is not supported, so Window texture coordinates and
the Vector pass will not work with custom cameras.

Pull Request: https://projects.blender.org/blender/blender/pulls/129495
2025-04-25 19:27:30 +02:00
Weizhen Huang
23c762e388 Fix: Cycles: Do not count volume bounds bounce as transparent
In forward path tracing, when we pass volume bounding meshes, we
accumulate `volume_bounds_bounce`. We should match this behaviour in NEE
instead of accumulating `transparent_bounce`.

Pull Request: https://projects.blender.org/blender/blender/pulls/137556
2025-04-24 13:10:33 +02:00
Sergey Sharybin
9f999adfc6 Fix #137230: Cycles OpenPGL crash with shadow catcher
The crash was caused by an overflow in the opgl_path_segment_storage
array. It happened because shadow catcher paths would write to the
segments but not clear them. This made it so the next render loop
iteration for the main path starts with non-empty segments in the
guiding data.

Disable training when megakernel is called for the shadow catcher
state.

To ensure this issue is not forgotten when the guiding is ported to
GPU add asserts in the `guiding.h`. While it is a no-op for default
GPU kernels sometimes we do compile debug kernels. But also it acts
as a plain-text reminder to the future-us in working on the code.

There is now also an assert before the main path megakernel to help
catching such cases in the future.

Pull Request: https://projects.blender.org/blender/blender/pulls/137291
2025-04-11 12:31:22 +02:00
Sergey Sharybin
32d49541c0 Fix #135572: Cycles shadow linking through transparency is broken on GPU
Make the ray self primitives store and restore reliable for cases when
the intersect_shadow kernel is called multiple times:

- Light object and primitive are stored in dedicated fields in the
  state. This adds 2 integers per state.

- The self object and primitive are used from the previous intersection
  when the intersect_shadow is called multiple times.

There is more detailed explanation added in the code.

The issue was introduced by the light refactor to be objects in #134846.

Pull Request: https://projects.blender.org/blender/blender/pulls/135573
2025-03-07 17:16:04 +01:00
Brecht Van Lommel
48398b223b Cleanup: Fix various divisions by zero reported by ASAN
Pull Request: https://projects.blender.org/blender/blender/pulls/135326
2025-03-06 22:34:23 +01:00
Weizhen Huang
7b3f7ccae0 Merge branch 'blender-v4.4-release' 2025-02-26 15:51:55 +01:00
Weizhen Huang
9ee9a2b789 Fix #135145: object visible to volume scatter when ray visibility is off
The comment was added in e0857ad152, and volume scatter visibility is
supported since cdd1d5a93c.

Pull Request: https://projects.blender.org/blender/blender/pulls/135168
2025-02-26 15:45:58 +01:00
Brecht Van Lommel
e813e46327 Cycles: Refactor lights to be objects
This is an intermediate steps towards making lights actual geometry.
Light is now a subclass of Geometry, which simplifies some code.

The geometry is not added to the BVH yet, which would be the next
step and improve light intersection performance with many lights.

This makes object attributes work on lights.

Co-authored-by: Lukas Stockner <lukas@lukasstockner.de>
Pull Request: https://projects.blender.org/blender/blender/pulls/134846
2025-02-24 23:44:14 +01:00
Lukas Stockner
8cb5e05c48 Cleanup: Cycles: Deduplicate kernel attribute code using templating
The attribute handling code in the kernel is currently highly duplicated since
it needs to handle five different data types and we couldn't use templates
back then.
We can now, so might as well make use of it and get rid of ~1000 lines.

There are also some small fixes for the GPU OSL code:
- Wrong derivative for .w component when converting float2/float3->float4
- Different conversion for float2->float (CPU averages, GPU used to take .x)
- Removed useless code for converting to float2, not used by OSL

Pull Request: https://projects.blender.org/blender/blender/pulls/134694
2025-02-20 19:28:45 +01:00
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