Blender V5.3
sky.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/image.h"
8
10#include "kernel/svm/types.h"
11#include "kernel/svm/util.h"
12
15
16#include "util/color.h"
17#include "util/defines.h"
18
20
21/* Sky texture */
22
23ccl_device float sky_angle_between(const float thetav,
24 const float phiv,
25 const float theta,
26 const float phi)
27{
28 const float cospsi = sinf(thetav) * sinf(theta) * cosf(phi - phiv) + cosf(thetav) * cosf(theta);
29 return safe_acosf(cospsi);
30}
31
32/*
33 * "A Practical Analytic Model for Daylight"
34 * A. J. Preetham, Peter Shirley, Brian Smits
35 */
37 const float theta,
38 const float gamma)
39{
40 const float ctheta = cosf(theta);
41 const float cgamma = cosf(gamma);
42
43 return (1.0f + lam[0] * expf(lam[1] / ctheta)) *
44 (1.0f + lam[2] * expf(lam[3] * gamma) + lam[4] * cgamma * cgamma);
45}
46
48 const float3 dir,
49 const float sunphi,
50 const float suntheta,
51 const float radiance_x,
52 const float radiance_y,
53 const float radiance_z,
54 ccl_private float *config_x,
55 ccl_private float *config_y,
56 ccl_private float *config_z)
57{
58 /* convert vector to spherical coordinates */
59 const float2 spherical = direction_to_spherical(dir);
60 float theta = spherical.x;
61 const float phi = -spherical.y + M_PI_2_F;
62
63 /* angle between sun direction and dir */
64 const float gamma = sky_angle_between(theta, phi, suntheta, sunphi);
65
66 /* clamp theta to horizon */
67 theta = min(theta, M_PI_2_F - 0.001f);
68
69 /* compute xyY color space values */
70 const float x = radiance_y * sky_perez_function(config_y, theta, gamma);
71 const float y = radiance_z * sky_perez_function(config_z, theta, gamma);
72 const float Y = radiance_x * sky_perez_function(config_x, theta, gamma);
73
74 /* convert to RGB */
75 const float3 xyz = xyY_to_xyz(x, y, Y);
76 return xyz_to_rgb_clamped(kg, xyz);
77}
78
79/*
80 * "An Analytic Model for Full Spectral Sky-Dome Radiance"
81 * Lukas Hosek, Alexander Wilkie
82 */
83ccl_device float sky_radiance_internal(const ccl_private float *configuration,
84 const float theta,
85 const float gamma)
86{
87 const float ctheta = cosf(theta);
88 const float cgamma = cosf(gamma);
89
90 const float expM = expf(configuration[4] * gamma);
91 const float rayM = cgamma * cgamma;
92 const float mieM = (1.0f + rayM) / powf((1.0f + configuration[8] * configuration[8] -
93 2.0f * configuration[8] * cgamma),
94 1.5f);
95 const float zenith = sqrtf(ctheta);
96
97 return (1.0f + configuration[0] * expf(configuration[1] / (ctheta + 0.01f))) *
98 (configuration[2] + configuration[3] * expM + configuration[5] * rayM +
99 configuration[6] * mieM + configuration[7] * zenith);
100}
101
103 const float3 dir,
104 const float sunphi,
105 const float suntheta,
106 const float radiance_x,
107 const float radiance_y,
108 const float radiance_z,
109 ccl_private float *config_x,
110 ccl_private float *config_y,
111 ccl_private float *config_z)
112{
113 /* convert vector to spherical coordinates */
114 const float2 spherical = direction_to_spherical(dir);
115 float theta = spherical.x;
116 const float phi = -spherical.y + M_PI_2_F;
117
118 /* angle between sun direction and dir */
119 const float gamma = sky_angle_between(theta, phi, suntheta, sunphi);
120
121 /* clamp theta to horizon */
122 theta = min(theta, M_PI_2_F - 0.001f);
123
124 /* compute xyz color space values */
125 const float x = sky_radiance_internal(config_x, theta, gamma) * radiance_x;
126 const float y = sky_radiance_internal(config_y, theta, gamma) * radiance_y;
127 const float z = sky_radiance_internal(config_z, theta, gamma) * radiance_z;
128
129 /* convert to RGB and adjust strength */
130 return xyz_to_rgb_clamped(kg, make_float3(x, y, z)) * (M_2PI_F / 683);
131}
132
133/* Nishita improved sky model */
134ccl_device float3 geographical_to_direction(const float lat, const float lon)
135{
136 return spherical_to_direction(lat - M_PI_2_F, lon - M_PI_2_F);
137}
138
140 ccl_private ShaderData *sd,
141 const float3 dir,
142 const uint32_t path_flag,
143 const float3 pixel_bottom,
144 const float3 pixel_top,
145 const ccl_private float *sky_data,
146 const uint texture_id)
147{
148 /* Definitions */
149 const float sun_elevation = sky_data[0];
150 const float sun_rotation = sky_data[1];
151 const float angular_diameter = sky_data[2];
152 const float sun_intensity = sky_data[3];
153 const float earth_intersection_angle = sky_data[4];
154 const bool sun_disc = (angular_diameter >= 0.0f);
155 float3 xyz = zero_float3();
156 const float2 direction = direction_to_spherical(dir);
157 const float3 sun_dir = spherical_to_direction(sun_elevation - M_PI_2_F, sun_rotation - M_PI_2_F);
158 const float sun_dir_angle = precise_angle(dir, sun_dir);
159 const float half_angular = angular_diameter * 0.5f;
160 const float dir_elevation = M_PI_2_F - direction.x;
161
162 /* If the ray is inside the Sun disc, render it, otherwise render the sky.
163 * Alternatively, ignore the Sun if we're evaluating the background texture. */
164 if (sun_disc && sun_dir_angle < half_angular && dir_elevation > earth_intersection_angle &&
165 !((path_flag & PATH_RAY_IMPORTANCE_BAKE) && kernel_data.background.use_sun_guiding))
166 {
167 /* Sun interpolation */
168 const float y = ((dir_elevation - sun_elevation) / angular_diameter) + 0.5f;
169 /* Limb darkening, coefficient is 0.6f */
170 const float limb_darkening = (1.0f -
171 0.6f * (1.0f - sqrtf(1.0f - sqr(sun_dir_angle / half_angular))));
172 xyz = mix(pixel_bottom, pixel_top, y) * sun_intensity * limb_darkening;
173 }
174
175 /* Sky */
176 const float x = fractf((-direction.y - M_PI_2_F + sun_rotation) * M_1_2PI_F);
177 /* Undo the non-linear transformation from the sky LUT */
178 const float y = copysignf(sqrtf(fabsf(dir_elevation) * M_2_PI_F), dir_elevation) * 0.5f + 0.5f;
179 xyz += make_float3(
180 kernel_image_interp(kg, sd, texture_id, dual2(make_float2(x, y)), IMAGE_MISSING_RGBA));
181
182 /* Convert to RGB */
183 return xyz_to_rgb_clamped(kg, xyz);
184}
185
187 ccl_private ShaderData *sd,
188 const uint32_t path_flag,
189 ccl_private float *ccl_restrict stack,
191 int offset)
192{
193 /* Load data */
194 const NodeSkyType sky_type = node.sky_type;
195
196 const float3 dir = stack_load_float3(stack, node.dir_offset);
197 float3 rgb;
198
199 /* Preetham and Hosek share the same data */
200 if (sky_type == NODE_SKY_PREETHAM || sky_type == NODE_SKY_HOSEK) {
201 const ccl_global SVMNodeTexSkyPreethamData &preetham =
202 *reinterpret_cast<const ccl_global SVMNodeTexSkyPreethamData *>(
203 &kernel_data_fetch(svm_nodes, offset));
204 offset += sizeof(SVMNodeTexSkyPreethamData) / sizeof(uint);
205
206 /* Copy config arrays to private memory for GPU compatibility. */
207 float config_x[9], config_y[9], config_z[9];
208 for (int i = 0; i < 9; i++) {
209 config_x[i] = preetham.config_x[i];
210 config_y[i] = preetham.config_y[i];
211 config_z[i] = preetham.config_z[i];
212 }
213
214 /* Compute Sky */
215 if (sky_type == NODE_SKY_PREETHAM) {
216 rgb = sky_radiance_preetham(kg,
217 dir,
218 preetham.phi,
219 preetham.theta,
220 preetham.radiance_x,
221 preetham.radiance_y,
222 preetham.radiance_z,
223 config_x,
224 config_y,
225 config_z);
226 }
227 else {
228 rgb = sky_radiance_hosek(kg,
229 dir,
230 preetham.phi,
231 preetham.theta,
232 preetham.radiance_x,
233 preetham.radiance_y,
234 preetham.radiance_z,
235 config_x,
236 config_y,
237 config_z);
238 }
239 }
240 /* Nishita */
241 else {
242 const ccl_global SVMNodeTexSkyNishitaData &nishita =
243 *reinterpret_cast<const ccl_global SVMNodeTexSkyNishitaData *>(
244 &kernel_data_fetch(svm_nodes, offset));
245 offset += sizeof(SVMNodeTexSkyNishitaData) / sizeof(uint);
246
247 const float3 pixel_bottom = make_float3(
248 nishita.pixel_bottom_x, nishita.pixel_bottom_y, nishita.pixel_bottom_z);
249 const float3 pixel_top = make_float3(
250 nishita.pixel_top_x, nishita.pixel_top_y, nishita.pixel_top_z);
251 const float sky_data[5] = {nishita.sun_elevation,
252 nishita.sun_rotation,
253 nishita.angular_diameter,
254 nishita.sun_intensity,
255 nishita.earth_intersection_angle};
256
257 /* Compute Sky */
259 kg, sd, dir, path_flag, pixel_bottom, pixel_top, sky_data, nishita.texture_id);
260 }
261
262 stack_store_float3(stack, node.out_offset, rgb);
263 return offset;
264}
265
#define Y
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
ccl_device float4 kernel_image_interp(KernelGlobals kg, ccl_private ShaderData *sd, const int image_texture_id, dual2 uv, const float4 missing_rgba)
ccl_device_inline float3 stack_load_float3(const ccl_private float *stack, const uint a)
ccl_device_inline void stack_store_float3(ccl_private float *stack, const uint a, const float3 f)
CCL_NAMESPACE_BEGIN ccl_device float2 direction_to_spherical(const float3 dir)
ccl_device float3 spherical_to_direction(const float theta, const float phi)
#define kernel_data
#define M_2_PI_F
#define ccl_restrict
#define M_PI_2_F
#define kernel_data_fetch(name, index)
#define ccl_private
#define M_1_2PI_F
#define ccl_global
#define ccl_device_noinline
#define expf(x)
#define powf(x, y)
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
#define copysignf(x, y)
float sky_perez_function(float4 lam03, float lam4, float theta, float gamma)
float3 xyY_to_xyz(float x, float y, float Y)
float sky_angle_between(float thetav, float phiv, float theta, float phi)
NodeSkyType
@ NODE_SKY_PREETHAM
@ NODE_SKY_HOSEK
@ PATH_RAY_IMPORTANCE_BAKE
ccl_device float3 xyz_to_rgb_clamped(KernelGlobals kg, const float3 xyz)
ccl_device float safe_acosf(const float a)
Definition math_base.h:590
ccl_device_inline float precise_angle(const float3 a, const float3 b)
CCL_NAMESPACE_BEGIN ccl_device_inline float3 zero_float3()
Definition math_float3.h:17
#define IMAGE_MISSING_RGBA
#define mix
#define sqr
#define fabsf
#define sqrtf
#define ccl_device
#define M_2PI_F
#define sinf
#define make_float2
#define fractf
#define cosf
ccl_device float3 geographical_to_direction(const float lat, const float lon)
Definition sky.h:134
ccl_device float3 sky_radiance_nishita(KernelGlobals kg, ccl_private ShaderData *sd, const float3 dir, const uint32_t path_flag, const float3 pixel_bottom, const float3 pixel_top, const ccl_private float *sky_data, const uint texture_id)
Definition sky.h:139
ccl_device float3 sky_radiance_hosek(KernelGlobals kg, const float3 dir, const float sunphi, const float suntheta, const float radiance_x, const float radiance_y, const float radiance_z, ccl_private float *config_x, ccl_private float *config_y, ccl_private float *config_z)
Definition sky.h:102
ccl_device float3 sky_radiance_preetham(KernelGlobals kg, const float3 dir, const float sunphi, const float suntheta, const float radiance_x, const float radiance_y, const float radiance_z, ccl_private float *config_x, ccl_private float *config_y, ccl_private float *config_z)
Definition sky.h:47
ccl_device_noinline int svm_node_tex_sky(KernelGlobals kg, ccl_private ShaderData *sd, const uint32_t path_flag, ccl_private float *ccl_restrict stack, const ccl_global SVMNodeTexSky &ccl_restrict node, int offset)
Definition sky.h:186
ccl_device float sky_radiance_internal(const ccl_private float *configuration, const float theta, const float gamma)
Definition sky.h:83
#define min(a, b)
Definition sort.cc:38
float x
float y
dual< float2 > dual2
Definition types_dual.h:105