Blender V4.3
differential.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
8
9/* See "Tracing Ray Differentials", Homan Igehy, 1999. */
10
12 const differential3 ray_dP,
13 float3 ray_D,
14 const differential3 ray_dD,
15 float3 surface_Ng,
16 float ray_t)
17{
18 /* ray differential transfer through homogeneous medium, to
19 * compute dPdx/dy at a shading point from the incoming ray */
20
21 float3 tmp = ray_D / dot(ray_D, surface_Ng);
22 float3 tmpx = ray_dP.dx + ray_t * ray_dD.dx;
23 float3 tmpy = ray_dP.dy + ray_t * ray_dD.dy;
24
25 surface_dP->dx = tmpx - dot(tmpx, surface_Ng) * tmp;
26 surface_dP->dy = tmpy - dot(tmpy, surface_Ng) * tmp;
27}
28
30{
31 /* compute dIdx/dy at a shading point, we just need to negate the
32 * differential of the ray direction */
33
34 dI->dx = -dD.dx;
35 dI->dy = -dD.dy;
36}
37
40 float3 dPdu,
41 float3 dPdv,
43 float3 Ng)
44{
45 /* now we have dPdx/dy from the ray differential transfer, and dPdu/dv
46 * from the primitive, we can compute dudx/dy and dvdx/dy. these are
47 * mainly used for differentials of arbitrary mesh attributes. */
48
49 /* find most stable axis to project to 2D */
50 float xn = fabsf(Ng.x);
51 float yn = fabsf(Ng.y);
52 float zn = fabsf(Ng.z);
53
54 if (zn < xn || zn < yn) {
55 if (yn < xn || yn < zn) {
56 dPdu.x = dPdu.y;
57 dPdv.x = dPdv.y;
58 dP.dx.x = dP.dx.y;
59 dP.dy.x = dP.dy.y;
60 }
61
62 dPdu.y = dPdu.z;
63 dPdv.y = dPdv.z;
64 dP.dx.y = dP.dx.z;
65 dP.dy.y = dP.dy.z;
66 }
67
68 /* using Cramer's rule, we solve for dudx and dvdx in a 2x2 linear system,
69 * and the same for dudy and dvdy. the denominator is the same for both
70 * solutions, so we compute it only once.
71 *
72 * `dP.dx = dPdu * dudx + dPdv * dvdx;`
73 * `dP.dy = dPdu * dudy + dPdv * dvdy;` */
74
75 float det = (dPdu.x * dPdv.y - dPdv.x * dPdu.y);
76
77 if (det != 0.0f) {
78 det = 1.0f / det;
79 }
80
81 du->dx = (dP.dx.x * dPdv.y - dP.dx.y * dPdv.x) * det;
82 dv->dx = (dP.dx.y * dPdu.x - dP.dx.x * dPdu.y) * det;
83
84 du->dy = (dP.dy.x * dPdv.y - dP.dy.y * dPdv.x) * det;
85 dv->dy = (dP.dy.y * dPdu.x - dP.dy.x * dPdu.y) * det;
86}
87
89{
91 d.dx = 0.0f;
92 d.dy = 0.0f;
93
94 return d;
95}
96
98{
100 d.dx = zero_float3();
101 d.dy = zero_float3();
102
103 return d;
104}
105
106/* Compact ray differentials that are just a radius to reduce memory usage and access cost
107 * on GPUs, basically cone tracing.
108 *
109 * See above for more accurate reference implementations of ray differentials. */
110
112{
113 return 0.0f;
114}
115
117{
118 return dD;
119}
120
122{
123 return 0.5f * (len(dD.dx) + len(dD.dy));
124}
125
127{
128 return dD;
129}
130
132 const float3 /* ray_D */,
133 const float ray_dD,
134 const float ray_t)
135{
136 return ray_dP + ray_t * ray_dD;
137}
138
140{
141 float3 dx, dy;
142 make_orthonormals(D, &dx, &dy);
143
145 d.dx = dD * dx;
146 d.dy = dD * dy;
147 return d;
148}
149
152 float3 dPdu,
153 float3 dPdv,
154 float dP,
155 float3 Ng)
156{
157 /* TODO: can we speed this up? */
158 differential_dudv(du, dv, dPdu, dPdv, differential_from_compact(Ng, dP), Ng);
159}
160
additional_info("compositor_sum_squared_difference_float_shared") .push_constant(Type output_img float dot(value.rgb, luminance_coefficients)") .define("LOAD(value)"
#define ccl_device_forceinline
#define ccl_device
#define ccl_private
#define CCL_NAMESPACE_END
#define fabsf(x)
ccl_device void differential_dudv(ccl_private differential *du, ccl_private differential *dv, float3 dPdu, float3 dPdv, differential3 dP, float3 Ng)
ccl_device_forceinline float differential_make_compact(const float dD)
ccl_device_forceinline float differential_transfer_compact(const float ray_dP, const float3, const float ray_dD, const float ray_t)
ccl_device_forceinline float differential_incoming_compact(const float dD)
ccl_device differential differential_zero()
ccl_device differential3 differential3_zero()
ccl_device_forceinline differential3 differential_from_compact(const float3 D, const float dD)
ccl_device void differential_dudv_compact(ccl_private differential *du, ccl_private differential *dv, float3 dPdu, float3 dPdv, float dP, float3 Ng)
CCL_NAMESPACE_BEGIN ccl_device void differential_transfer(ccl_private differential3 *surface_dP, const differential3 ray_dP, float3 ray_D, const differential3 ray_dD, float3 surface_Ng, float ray_t)
ccl_device_forceinline float differential_zero_compact()
ccl_device void differential_incoming(ccl_private differential3 *dI, const differential3 dD)
int len
CCL_NAMESPACE_BEGIN ccl_device_inline float3 zero_float3()
Definition math_float3.h:15
float z
Definition sky_float3.h:27
float y
Definition sky_float3.h:27
float x
Definition sky_float3.h:27
ccl_device_inline void make_orthonormals(const float3 N, ccl_private float3 *a, ccl_private float3 *b)
Definition util/math.h:593