Commit Graph

3840 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
Campbell Barton
7c668c0308 Cleanup: CMake indentation & wrap long lines 2025-05-20 11:20:09 +10:00
Nikita Sirgienko
54766b6a54 Cycles: Introducing the code for adoption of Embree 4.4
Embree 4.4 introduces an improvement in the Embree GPU
implementation by dropping shared memory usage in favor
of direct controllable memory transfers. This should allow
addressing several problems spotted in Blender regarding
multithreading and memory corruption when BVH and rendering
happen at the same time. However, to implement such
improvements, the API has changed for several functions, and
this commit adopts Blender code to these changes, making Blender
buildable and functional with all existing Embree 4.X
versions, before and after 4.4.

No functional changes in Blender behavior are expected if
using Embree versions below 4.4.

Pull Request: https://projects.blender.org/blender/blender/pulls/139061
2025-05-19 11:25:50 +02:00
Lukas Stockner
a6015e1411 Cycles: Fix inconsistency in Ng handling between Microfacets and other closures
In Cycles, the convention is that reflection vs. refraction are classified
based on the hemisphere defined by the *shading* normal (N).

In general, most closure code uses the shading normal for most operations,
as is expected since using the geometric normal (Ng) would break normal maps
and smooth shading.

However, there are two places that use Ng: On the one hand, BSDF sampling
functions generally reject reflections that fall below the Ng hemisphere, since
they'd intersect the geometry when tracing the bounce. This is required, and
we can't do much about it.
On the other hand, the Microfacet evaluation code also checked that the ray
is in the same hemisphere w.r.t. both shading and geometric normal.

Theoretically, this is the right thing to do, since sampling and evaluation code
are supposed to be consistent. However, doing so breaks smooth shading, since
now direct light evaluation near the terminator will sometimes be rejected.

This didn't cause problems in practice because of another inconsistency: While
the parameter of the eval functions was named Ng, the caller actually provided
N (unclear whether by mistake or as a hacky workaround to the terminator).
When this was fixed in 063a9e89, users quickly reported issues with the shadow
terminator, so it was reverted to the hacky inconsistency in 1c50dd8b.

So, let's clean this mess up properly. If we don't want to do the Ng hemisphere
check in _eval, then instead of passing in a misleading value that ends up
making it a no-op, just remove the check. After all, the other closures don't
perform it either.

This way, we avoid the mislabeled Ng, we get rid of the special case for
microfacets, and the shadow terminator continues to be fine.

Technically, we still have the _sample vs. _eval mismatch. However, this is just
unavoidable, and is irrelevant in practice: For a strongly directional light
that makes the shadow terminator noticeable, the MIS weights will be massively
in favor of eval, to the point that it doesn't really matter what sample does.

To support this argument: You can actually reproduce a broken shadow terminator
in pretty much every Cycles version going back to 2011 by just setting up a
small intense mesh emitter, turning off MIS on it to disable _eval, and then
rendering a diffuse smooth-shaded sphere with >100000 samples so that the
fireflies resolve into somewhat consistent lighting.
If nobody has complained about this affecting all closures for 11 years,
I guess it's fine.

Pull Request: https://projects.blender.org/blender/blender/pulls/138632
2025-05-18 17:20:32 +02:00
Alaska
0d6a79a8f3 Cycles: Use CUDA 11 to compile PTX kernels
This commit makes it so CUDA 11 is used to compile the compute_75
PTX CUDA kernels.

This is being done because PTX kernels have much stricter minimum
driver requirements than standard kernels, so using the latest CUDA
toolkit to compile PTX kernels can result in the PTX kernels being
inaccessible to users with drivers that are only a few months old.

This is important because in some situations, it's either impossible
(E.g. Renting certain cloud services), or difficult to update the GPU
drivers on some machines. And we want to make sure the PTX kernels
are usable by as many people as possible

Original Author: Sergey Sharybin <sergey@blender.org>

Pull Request: https://projects.blender.org/blender/blender/pulls/138879
2025-05-15 15:00:47 +02:00
Alexandre-Cardaillac
921c2b9d61 Shader: New Volume Coefficients Shader
Add a new shader node to control volume coefficients (scattering,
absorption and emission) directly, making it easier to model existing
volumes with measured data.

Pull Request: https://projects.blender.org/blender/blender/pulls/136287
2025-05-08 19:19:35 +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
Weizhen Huang
69c194ee5a Cleanup: Cycles: safer division in volume sample channel 2025-05-05 18:35:24 +02:00
Weizhen Huang
4e36a31871 Cleanup: Cycles: split volume_sample_channel() into two functions 2025-05-05 18:35:24 +02:00
Campbell Barton
9eb9493ecd Fix: build error WITH_CYCLES_OSL & GCC 15.1
Ref !138238
2025-05-02 14:22:27 +10:00
Brecht Van Lommel
2c99edbffa Cycles: Bump Embree minimum version to 4.0.0
The build is already failing with Embree 3, as noticed in #137556.
And Embree 4 was released 2 years ago.

Pull Request: https://projects.blender.org/blender/blender/pulls/138221
2025-04-30 19:50:14 +02:00
Lukas Stockner
175686f286 Fix: Cycles: Shadow terminator logic does not account for OSL bump mapping
The issue here is that the automatic bump shader in OSL adjusts globals.N,
which is used to define each closure's shading normal, but sd->N remains
as it was (unlike SVM, which sets it from svm_node_set_normal).

This is a problem since some code like the shadow terminator stuff uses sd->N,
so to make behavior consistent the fix is to set it from OSL as well.

Pull Request: https://projects.blender.org/blender/blender/pulls/138092
2025-04-28 14:03:55 +02:00
Lukas Stockner
0dc4754da4 Cycles: Move OptiX OSL Camera kernel into its own PTX module
On the one hand, this improves initialization time since we don't need to
load/compile the full OSL module with all the shading logic if we're only
using a custom camera with SVM shading.

On the other hand, it also fixes a bug I noticed while preparing test scenes:
The AO and Bevel nodes don't work when using custom cameras with SVM on OptiX.

The issue there is that those two are handled by the SHADE_SURFACE_RAYTRACE
kernel, but since that one has intersection logic, we use the OptiX-specific
kernel even if OSL shading is disabled.
However, with the previous unified OSL module, this would mean loading
SHADE_SURFACE_RAYTRACE from kernel_osl.cu, which has `#undef __SVM__` and
therefore doesn't handle them correctly.

With this change, we'll use the kernels from kernel_shader_raytrace.cu in that
case, which do support SVM nodes just fine.

Disk usage of the new kernel_optix_osl_camera.ptx.zst file is 30KB, so this
also doesn't blow up the kernel disk size (and kernel_optix_osl.ptx.zst is
probably smaller by that amount now).

Since it seems that we can mix modules just fine, I'm suspecting that we could
split the modules properly (intersection, SVM shading with raytracing,
OSL shading, OSL camera), instead of the current approach where modules
essentially correspond to feature set tiers and each includes the previous
one's kernels as well - but that's a separate refactor.

Pull Request: https://projects.blender.org/blender/blender/pulls/138021
2025-04-28 12:49:35 +02:00
Lukas Stockner
54b156bf16 Cycles: Add support for automatic bump mapping on OptiX OSL
All the required parts are already there, just needs to be hooked up.
This is essentially just copying what `osl_eval_nodes<SHADER_TYPE_SURFACE>`
does on the CPU.

Fixes #104276.

Pull Request: https://projects.blender.org/blender/blender/pulls/138044
2025-04-28 12:47:31 +02:00
Lukas Stockner
8bc9f174d3 Fix: Cycles: Wrong derivative handling in OptiX OSL transform()
osl_transform_triple(), osl_transform_dvmdv() and so on are supposed to apply
the given transform in the context of OSL's auto-differentiation system.
Therefore, the given input is a dual vector, containing both the value as v[0]
and its derivatives w.r.t. X and Y in v[1] and v[2].

However, the existing code treats these as a simple list of vectors, applying
the same operation to all three instead of propagating the derivatives.
On top of that, it also treated the given matrix input as if there were three
of them, which isn't the case.

Therefore, this commit replaces the implementation to do the right thing.
The Vector and Normal case are straightforward since the operation is linear,
so applying the same operation to all three vectors works.
The Point case is a bit more complicated, but not too bad when written out.

This bug mostly became apparent when using Object or Camera texture coordinates
with a Bump node, since that node uses OSL differentials and Object/Camera
coordinates are implemented using transform().

I'm pretty sure that all the other builtin functions (e.g. sin) at the bottom
of services_gpu.h have the same problem, but one thing at a time...

Pull Request: https://projects.blender.org/blender/blender/pulls/138045
2025-04-28 12:46:54 +02:00
Weizhen Huang
9cc252088e Fix: Cycles: mix weight not applied on volume emission with SVM
Pull Request: https://projects.blender.org/blender/blender/pulls/138081
2025-04-28 12:29:38 +02:00
Campbell Barton
c90e8bae0b Cleanup: spelling in comments & replace some use of single quotes
Previously spell checker ignored text in single quotes however this
meant incorrect spelling was ignored in text where it shouldn't have
been.

In cases single quotes were used for literal strings
(such as variables, code & compiler flags),
replace these with back-ticks.

In cases they were used for UI labels,
replace these with double quotes.

In cases they were used to reference symbols,
replace them with doxygens symbol link syntax (leading hash).

Apply some spelling corrections & tweaks (for check_spelling_* targets).
2025-04-26 11:17:13 +00:00
Campbell Barton
682e5e3597 Cleanup: spelling in comments (make check_spelling_*) 2025-04-26 00:48:04 +00: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
Alaska
92b748a91b Fix: Cycles adaptive kernel compilation
This commit fixes a issue where Cycles adaptive kernel compilation
would always undefine adaptive kernel features, resulting in various
issues like incorrect renders.

Pull Request: https://projects.blender.org/blender/blender/pulls/137804
2025-04-22 13:26:28 +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
Brecht Van Lommel
7aaa43b557 Revert "Fix: Build failures when using path with spaces on macOS"
This reverts commit be63ebd961.

This doesn't work well with MSBuild, semicolons get escaped even in
verbatim mode.
2025-04-10 13:04:34 +02:00
Sergey Sharybin
30b962b3d8 Cycles: Optimize 3d and 4d noise
The goal is to reduce the affect of the fmod() used in the noise code,
which was initially reported in the comment:

    https://projects.blender.org/blender/blender/pulls/119884#issuecomment-1258902

Basic idea is to benefit from SIMD vectorization on CPU.

Tested on Linux i9-11900K and macOS on M2 Ultra, in both cases performance
after this change is very close to what it could be with the fmod() commented
out (the call itself, `p = p + precision_correction`).

On macOS the penalty of fmod() was about 10%, on Linux it was closer to 30%
when built with GCC-13. With Linux builds from the buildbot it is more like 18%.

The optimization is only done for 3d and 4d noise. It might be possible to
gain some performance improvement for 1d and 2d cases, but the approach would
need to be different: we'd need to optimize scalar version fmodf(). Maybe
tricks with integer cast will be faster (since we are a bit optimistic in the
kernel and do not guarantee exact behavior in extreme cases such as NaN inputs).

Pull Request: https://projects.blender.org/blender/blender/pulls/137109
2025-04-09 13:40:10 +02:00
Brecht Van Lommel
6db2c6b864 Revert: Part of "Fix: Build failures when using path with spaces"
Commit be63ebd961

This is causing issues with CUDA kernel compilation in some setups, even
though the builbot is ok. Since this isn't yet working for oneAPI anyway,
revert all the changes to Cycles kernel compilation for now.
2025-04-08 14:55:03 +02:00
Brecht Van Lommel
be63ebd961 Fix: Build failures when using path with spaces on macOS
Use VERBATIM to ensure spaces inside command line arguments don't get
escaped automatically.

On Linux and Windows the oneAPI kernel compilation still has problems.
There is an apparent bug with single quote escaping in add_custom_command
which means it's not easy to use VERBATIM.
2025-04-07 16:29:14 +02:00
Campbell Barton
f48b4e3abf Cleanup: wrap long lines for CMake 2025-04-05 20:30:37 +11:00
Xavier Hallade
3dd6104c87 Cycles: oneAPI: Fix building for non-Intel SYCL targets without ocloc
The current logic disables WITH_CYCLES_ONEAPI_BINARIES when ocloc is not
found, which is fine, but prevented building for other non-Intel SYCL
targets without (unnecessary) ocloc.

The fix here is to remove spir64_gen target when
WITH_CYCLES_ONEAPI_BINARIES is disabled, instead of forcing only spir64.
2025-04-03 15:13:57 +02:00
Campbell Barton
f89cf19ba6 Cleanup: indentation for CMake files, strip trailing space 2025-04-02 03:01:59 +00:00
Sergey Sharybin
36559fd89f Fix #136811: HIP-RT performance regression in 4.5
Reduce the register pressure and branching in the switch() by using
subclass and cast from void* to the base class.

This ensures intersection functions are not inlined multiple times,
bringing performance back.

Alternative could be to avoid functions (they are quite large) but
that only partially resolves the performance regression.

Pull Request: https://projects.blender.org/blender/blender/pulls/136823
2025-04-01 17:59:44 +02:00
Xavier Hallade
e00cc8c100 Cycles: oneAPI: Use default linker on Windows
The initial issues that led to the choice of forcing the use of
linker.exe seem gone and there is currently no strong reason to use
linker.exe explicitly, so let's simplify and use the default setting.
2025-03-28 12:34:16 +01:00
Xavier Hallade
c4cf399755 Cycles: oneAPI: Re-enable -ffast-math
The initial limitation preventing from using -ffast-math, worked around
in 09df1f4caf, got fixed upstream in LLVM
and the fix is part of current DPC++ compiler:
63ecd2a725

We're now able to go back to using -ffast-math, which helps simplifying
the set of compiler flags.

No performance nor conformance change is expected from this change (most
of the gain is achieved already with the use of -cl-fast-relaxed-math
since 284b89a0a3) and this has been
verified on Arc B580 under Windows.
2025-03-27 17:18:30 +01:00
Brecht Van Lommel
15390b9257 License: Change NanoVDB header to Apache 2, following upstream OpenVDB
This license was changed upstream, and it's simpler if we can use the same as
most of the Cycles code.
2025-03-27 14:48:06 +01:00
Alaska
2e829ca4cf Fix #136303: Normalize the normals on the Ambient Occlusion node
This commit simply normalizes the normals of the Ambient occlusion
node before computing the output to avoid odd behaviour with
unnormalized normals.

Pull Request: https://projects.blender.org/blender/blender/pulls/136315
2025-03-27 02:58:19 +01:00
Campbell Barton
42ad772a1f Cleanup: spelling & repeated terms (make check_spelling_*)
Also use comment blocks for English text.
2025-03-27 01:13:34 +00:00
Sergey Sharybin
2ab231d802 Refactor: Pass proper KernelGlobals
HIP-RT functions do have access to kg, and it was used inconsistently:
some functions were passed actual kg, other were passed nullptr.

This change makes it consistent and passes kg everywhere.

Pull Request: https://projects.blender.org/blender/blender/pulls/136503
2025-03-26 11:07:06 +01:00
Sergey Sharybin
709371b278 Refactor: Avoid creation of local copy of RaySelfPrimitives 2025-03-26 11:07:04 +01:00
Sergey Sharybin
888c7e1df9 Cleanup: Avoid redundant data fetch 2025-03-26 11:07:04 +01:00
Sergey Sharybin
3d882acee2 Cleanup: Else after return 2025-03-26 11:07:04 +01:00
Sergey Sharybin
b2dd523d0d Cleanup: Avoid default hit initialization
The entire object is assigned later on, no need to initialize it.
2025-03-26 11:07:04 +01:00
Sergey Sharybin
323e27d825 Cleanup: Remove redundant assignment
The payload stores pointers, no need to restore pointer
of the function argument to the same value.
2025-03-26 11:07:04 +01:00
Sergey Sharybin
e92a8042c3 Refactor: Payload for shadow intersection and filter in HIP-RT
The code before this change was relying on the ShadowPayload have
the same "header" as RayPayload for some of the primitive types
(curve, motion triangle, point): intersection functions were shared
between "regular" and shadow rays (shadow in this case is shadow_all),
but extra filter function was used for shadow rays.

This is fragile if someone changes one of these structures. What is
worse is that compiler might actually decide to shuffle things in
some structs, or remove unused fields.

This change also solves confusion about ShadowPayload::prim_type
seemingly only being assigned to PRIMITIVE_NONE. With time it is
not impossible that compiler will also see this, and constant-fold
some checks, or even remove the field. If that happens then the
render result will be wrong. Maybe it is already happening as there
are some GPU and driver and optimization flag specific bugs in the
area.

It is unclear whether it was causing any actual problem: W7800
seems to render all hair correctly on Linux.
2025-03-26 11:07:04 +01:00
Sergey Sharybin
cdb3f34944 Cleanup: Use full name for the primitive_type
Makes it extra clear locally type of what the variable contains:
primitive, ray, or something else.
2025-03-26 11:07:04 +01:00
Sergey Sharybin
72542f3bb4 Cleanup: Follow Blender style and use more const
Also make some style decisions more consistent: for example,
the way how stop/continue search return value is commented.
Prefer lower vertical space for those.
2025-03-26 11:07:04 +01:00
Sergey Sharybin
bf9c95f164 Cleanup: Move payload type cast to caller in HIP-RT
Mainly readability purposes:
- Having variables called local_payload is ambiguous: does it refer to
  LocalPayload type or to a variable be local in a function?
- Some of the functions are used for different ray types, so having the
  type case in intersectFunc and filterFunc makes it easier to scan.

For the latter: now it is more obvious that Curve_Intersect_Shadow
expects RayPayload, but Curve_Filter_Shadow expects ShadowPayload.
It might not be a problem currently as ShadowPayload has the same
"header" RayPayload, but it might change in the future. Also, compiler
might optimize fields out from one but not from the other.
2025-03-26 11:07:04 +01:00
Sergey Sharybin
3daaf21bab Cleanup: Remove unused function argument in HIP-RT 2025-03-26 11:07:04 +01:00
salipour
ae710101f5 Fix #136138, #136449: Cycles HIP RDNA2 white and blue render artifacts
There is a known precision bug in the current HIP compiler version                                                                                                                                                    (RDNA2 family/Windows) that has already been fixed and will be available in
a future HIP SDK release. Enabling more precise math prevents the artifacts.

This may cause a 5-10% performance drop in some scenes.

Fix #136138: Microfacet BSDF
Fix #136449: Hair BSDF

Pull Request: https://projects.blender.org/blender/blender/pulls/136341
2025-03-25 18:21:16 +01:00
nubnubbud
5e2afb3f6f Cycles: Replace bump correction algorithm to better respect normal maps
The new correction avoids washed out areas near the shadow terminator,
preserving more detail from normal and bump maps.

It implements the method from the paper "A Microfacet-Based Shadowing
Function to Solve the Bump Terminator Problem" by Alejandro Conty Estevez,
Pascal Lecocq, and Clifford Stein.

Pull Request: https://projects.blender.org/blender/blender/pulls/135380
2025-03-25 18:01:01 +01:00