Blender V5.0
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
7#include "kernel/types.h"
8
10
11/* See "Tracing Ray Differentials", Homan Igehy, 1999. */
12
14 const differential3 ray_dP,
15 const float3 ray_D,
16 const differential3 ray_dD,
17 const float3 surface_Ng,
18 const float ray_t)
19{
20 /* ray differential transfer through homogeneous medium, to
21 * compute dPdx/dy at a shading point from the incoming ray */
22
23 const float3 tmp = ray_D / dot(ray_D, surface_Ng);
24 const float3 tmpx = ray_dP.dx + ray_t * ray_dD.dx;
25 const float3 tmpy = ray_dP.dy + ray_t * ray_dD.dy;
26
27 surface_dP->dx = tmpx - dot(tmpx, surface_Ng) * tmp;
28 surface_dP->dy = tmpy - dot(tmpy, surface_Ng) * tmp;
29}
30
32{
33 /* compute dIdx/dy at a shading point, we just need to negate the
34 * differential of the ray direction */
35
36 dI->dx = -dD.dx;
37 dI->dy = -dD.dy;
38}
39
42 float3 dPdu,
43 float3 dPdv,
45 const float3 Ng)
46{
47 /* now we have dPdx/dy from the ray differential transfer, and dPdu/dv
48 * from the primitive, we can compute dudx/dy and dvdx/dy. these are
49 * mainly used for differentials of arbitrary mesh attributes. */
50
51 /* find most stable axis to project to 2D */
52 const float xn = fabsf(Ng.x);
53 const float yn = fabsf(Ng.y);
54 const float zn = fabsf(Ng.z);
55
56 if (zn < xn || zn < yn) {
57 if (yn < xn || yn < zn) {
58 dPdu.x = dPdu.y;
59 dPdv.x = dPdv.y;
60 dP.dx.x = dP.dx.y;
61 dP.dy.x = dP.dy.y;
62 }
63
64 dPdu.y = dPdu.z;
65 dPdv.y = dPdv.z;
66 dP.dx.y = dP.dx.z;
67 dP.dy.y = dP.dy.z;
68 }
69
70 /* using Cramer's rule, we solve for dudx and dvdx in a 2x2 linear system,
71 * and the same for dudy and dvdy. the denominator is the same for both
72 * solutions, so we compute it only once.
73 *
74 * `dP.dx = dPdu * dudx + dPdv * dvdx;`
75 * `dP.dy = dPdu * dudy + dPdv * dvdy;` */
76
77 float det = (dPdu.x * dPdv.y - dPdv.x * dPdu.y);
78
79 if (det != 0.0f) {
80 det = 1.0f / det;
81 }
82
83 du->dx = (dP.dx.x * dPdv.y - dP.dx.y * dPdv.x) * det;
84 dv->dx = (dP.dx.y * dPdu.x - dP.dx.x * dPdu.y) * det;
85
86 du->dy = (dP.dy.x * dPdv.y - dP.dy.y * dPdv.x) * det;
87 dv->dy = (dP.dy.y * dPdu.x - dP.dy.x * dPdu.y) * det;
88}
89
91{
93 d.dx = 0.0f;
94 d.dy = 0.0f;
95
96 return d;
97}
98
100{
102 d.dx = zero_float3();
103 d.dy = zero_float3();
104
105 return d;
106}
107
108/* Compact ray differentials that are just a radius to reduce memory usage and access cost
109 * on GPUs, basically cone tracing.
110 *
111 * See above for more accurate reference implementations of ray differentials. */
112
114{
115 return 0.0f;
116}
117
119{
120 return dD;
121}
122
124{
125 return 0.5f * (len(dD.dx) + len(dD.dy));
126}
127
129{
130 return 0.5f * (len(D.dx) + len(D.dy));
131}
132
134{
135 return dD;
136}
137
139 const float3 /* ray_D */,
140 const float ray_dD,
141 const float ray_t)
142{
143 return ray_dP + ray_t * ray_dD;
144}
145
147{
148 float3 dx;
149 float3 dy;
150 make_orthonormals(D, &dx, &dy);
151
153 d.dx = dD * dx;
154 d.dy = dD * dy;
155 return d;
156}
157
160 const float3 dPdu,
161 const float3 dPdv,
162 const float dP,
163 const float3 Ng)
164{
165 /* TODO: can we speed this up? */
166 differential_dudv(du, dv, dPdu, dPdv, differential_from_compact(Ng, dP), Ng);
167}
168
#define D
dot(value.rgb, luminance_coefficients)") DEFINE_VALUE("REDUCE(lhs
#define ccl_device_forceinline
#define ccl_private
#define CCL_NAMESPACE_END
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 void differential_dudv(ccl_private differential *du, ccl_private differential *dv, float3 dPdu, float3 dPdv, differential3 dP, const float3 Ng)
ccl_device_forceinline float differential_incoming_compact(const float dD)
ccl_device differential differential_zero()
ccl_device void differential_dudv_compact(ccl_private differential *du, ccl_private differential *dv, const float3 dPdu, const float3 dPdv, const float dP, const float3 Ng)
ccl_device differential3 differential3_zero()
ccl_device_forceinline differential3 differential_from_compact(const float3 D, const float dD)
CCL_NAMESPACE_BEGIN ccl_device void differential_transfer(ccl_private differential3 *surface_dP, const differential3 ray_dP, const float3 ray_D, const differential3 ray_dD, const float3 surface_Ng, const float ray_t)
ccl_device_forceinline float differential_zero_compact()
ccl_device void differential_incoming(ccl_private differential3 *dI, const differential3 dD)
CCL_NAMESPACE_BEGIN ccl_device_inline float3 zero_float3()
Definition math_float3.h:17
ccl_device_inline void make_orthonormals(const float3 N, ccl_private float3 *a, ccl_private float3 *b)
#define fabsf
#define ccl_device
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136
dual< float3 > dual3
Definition types_dual.h:58
uint len