[Bf-blender-cvs] [eacdcb2dd80] master: Cycles: Add new Sky Texture method including direct sunlight

Lukas Stockner noreply at git.blender.org
Wed Jun 17 21:16:25 CEST 2020


Commit: eacdcb2dd80e9e2340fa7a4b8509448b0c72b77a
Author: Lukas Stockner
Date:   Wed Jun 17 20:27:10 2020 +0200
Branches: master
https://developer.blender.org/rBeacdcb2dd80e9e2340fa7a4b8509448b0c72b77a

Cycles: Add new Sky Texture method including direct sunlight

This commit adds a new model to the Sky Texture node, which is based on a
method by Nishita et al. and works by basically simulating volumetric
scattering in the atmosphere.

By making some approximations (such as only considering single scattering),
we get a fairly simple and fast simulation code that takes into account
Rayleigh and Mie scattering as well as Ozone absorption.

This code is used to precompute a 512x128 texture which is then looked up
during render time, and is fast enough to allow real-time tweaking in the
viewport.

Due to the nature of the simulation, it exposes several parameters that
allow for lots of flexibility in choosing the look and matching real-world
conditions (such as Air/Dust/Ozone density and altitude).

Additionally, the same volumetric approach can be used to compute absorption
of the direct sunlight, so the model also supports adding direct sunlight.
This makes it significantly easier to set up Sun+Sky illumination where
the direction, intensity and color of the sun actually matches the sky.

In order to support properly sampling the direct sun component, the commit
also adds logic for sampling a specific area to the kernel light sampling
code. This is combined with portal and background map sampling using MIS.

This sampling logic works for the common case of having one Sky texture
going into the Background shader, but if a custom input to the Vector
node is used or if there are multiple Sky textures, it falls back to using
only background map sampling (while automatically setting the resolution to
4096x2048 if auto resolution is used).

More infos and preview can be found here:
https://docs.google.com/document/d/1gQta0ygFWXTrl5Pmvl_nZRgUw0mWg0FJeRuNKS36m08/view

Underlying model, implementation and documentation by Marco (@nacioss).
Improvements, cleanup and sun sampling by @lukasstockner.

Differential Revision: https://developer.blender.org/D7896

===================================================================

M	intern/cycles/blender/blender_shader.cpp
M	intern/cycles/kernel/CMakeLists.txt
M	intern/cycles/kernel/kernel_emission.h
M	intern/cycles/kernel/kernel_light.h
A	intern/cycles/kernel/kernel_light_background.h
A	intern/cycles/kernel/kernel_light_common.h
M	intern/cycles/kernel/kernel_montecarlo.h
M	intern/cycles/kernel/kernel_types.h
M	intern/cycles/kernel/shaders/node_sky_texture.osl
M	intern/cycles/kernel/svm/svm_sky.h
M	intern/cycles/kernel/svm/svm_types.h
M	intern/cycles/render/CMakeLists.txt
A	intern/cycles/render/image_sky.cpp
A	intern/cycles/render/image_sky.h
M	intern/cycles/render/light.cpp
M	intern/cycles/render/nodes.cpp
M	intern/cycles/render/nodes.h
M	intern/cycles/util/CMakeLists.txt
M	intern/cycles/util/util_sky_model.h
A	intern/cycles/util/util_sky_nishita.cpp
M	source/blender/editors/space_node/drawnode.c
M	source/blender/makesdna/DNA_node_types.h
M	source/blender/makesrna/intern/rna_nodetree.c
M	source/blender/nodes/shader/nodes/node_shader_tex_sky.c

===================================================================

diff --git a/intern/cycles/blender/blender_shader.cpp b/intern/cycles/blender/blender_shader.cpp
index f207d8ae07f..19d2730dc93 100644
--- a/intern/cycles/blender/blender_shader.cpp
+++ b/intern/cycles/blender/blender_shader.cpp
@@ -813,6 +813,14 @@ static ShaderNode *add_node(Scene *scene,
     sky->sun_direction = normalize(get_float3(b_sky_node.sun_direction()));
     sky->turbidity = b_sky_node.turbidity();
     sky->ground_albedo = b_sky_node.ground_albedo();
+    sky->sun_disc = b_sky_node.sun_disc();
+    sky->sun_size = b_sky_node.sun_size();
+    sky->sun_elevation = b_sky_node.sun_elevation();
+    sky->sun_rotation = b_sky_node.sun_rotation();
+    sky->altitude = b_sky_node.altitude();
+    sky->air_density = b_sky_node.air_density();
+    sky->dust_density = b_sky_node.dust_density();
+    sky->ozone_density = b_sky_node.ozone_density();
     BL::TexMapping b_texture_mapping(b_sky_node.texture_mapping());
     get_tex_mapping(&sky->tex_mapping, b_texture_mapping);
     node = sky;
diff --git a/intern/cycles/kernel/CMakeLists.txt b/intern/cycles/kernel/CMakeLists.txt
index 2e839a616e9..35339abff45 100644
--- a/intern/cycles/kernel/CMakeLists.txt
+++ b/intern/cycles/kernel/CMakeLists.txt
@@ -113,6 +113,8 @@ set(SRC_HEADERS
   kernel_id_passes.h
   kernel_jitter.h
   kernel_light.h
+  kernel_light_background.h
+  kernel_light_common.h
   kernel_math.h
   kernel_montecarlo.h
   kernel_passes.h
diff --git a/intern/cycles/kernel/kernel_emission.h b/intern/cycles/kernel/kernel_emission.h
index 71b176a0a8f..4ac07d86dda 100644
--- a/intern/cycles/kernel/kernel_emission.h
+++ b/intern/cycles/kernel/kernel_emission.h
@@ -326,9 +326,7 @@ ccl_device_noinline_cpu float3 indirect_background(KernelGlobals *kg,
   /* Background MIS weights. */
 #  ifdef __BACKGROUND_MIS__
   /* Check if background light exists or if we should skip pdf. */
-  int res_x = kernel_data.integrator.pdf_background_res_x;
-
-  if (!(state->flag & PATH_RAY_MIS_SKIP) && res_x) {
+  if (!(state->flag & PATH_RAY_MIS_SKIP) && kernel_data.background.use_mis) {
     /* multiple importance sampling, get background light pdf for ray
      * direction, and compute weight with respect to BSDF pdf */
     float pdf = background_light_pdf(kg, ray->P, ray->D);
diff --git a/intern/cycles/kernel/kernel_light.h b/intern/cycles/kernel/kernel_light.h
index 04472212d0c..0448d0165b9 100644
--- a/intern/cycles/kernel/kernel_light.h
+++ b/intern/cycles/kernel/kernel_light.h
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+#include "kernel_light_background.h"
+
 CCL_NAMESPACE_BEGIN
 
 /* Light Sample result */
@@ -33,500 +35,6 @@ typedef struct LightSample {
   LightType type; /* type of light */
 } LightSample;
 
-/* Area light sampling */
-
-/* Uses the following paper:
- *
- * Carlos Urena et al.
- * An Area-Preserving Parametrization for Spherical Rectangles.
- *
- * https://www.solidangle.com/research/egsr2013_spherical_rectangle.pdf
- *
- * Note: light_p is modified when sample_coord is true.
- */
-ccl_device_inline float rect_light_sample(float3 P,
-                                          float3 *light_p,
-                                          float3 axisu,
-                                          float3 axisv,
-                                          float randu,
-                                          float randv,
-                                          bool sample_coord)
-{
-  /* In our name system we're using P for the center,
-   * which is o in the paper.
-   */
-
-  float3 corner = *light_p - axisu * 0.5f - axisv * 0.5f;
-  float axisu_len, axisv_len;
-  /* Compute local reference system R. */
-  float3 x = normalize_len(axisu, &axisu_len);
-  float3 y = normalize_len(axisv, &axisv_len);
-  float3 z = cross(x, y);
-  /* Compute rectangle coords in local reference system. */
-  float3 dir = corner - P;
-  float z0 = dot(dir, z);
-  /* Flip 'z' to make it point against Q. */
-  if (z0 > 0.0f) {
-    z *= -1.0f;
-    z0 *= -1.0f;
-  }
-  float x0 = dot(dir, x);
-  float y0 = dot(dir, y);
-  float x1 = x0 + axisu_len;
-  float y1 = y0 + axisv_len;
-  /* Compute internal angles (gamma_i). */
-  float4 diff = make_float4(x0, y1, x1, y0) - make_float4(x1, y0, x0, y1);
-  float4 nz = make_float4(y0, x1, y1, x0) * diff;
-  nz = nz / sqrt(z0 * z0 * diff * diff + nz * nz);
-  float g0 = safe_acosf(-nz.x * nz.y);
-  float g1 = safe_acosf(-nz.y * nz.z);
-  float g2 = safe_acosf(-nz.z * nz.w);
-  float g3 = safe_acosf(-nz.w * nz.x);
-  /* Compute predefined constants. */
-  float b0 = nz.x;
-  float b1 = nz.z;
-  float b0sq = b0 * b0;
-  float k = M_2PI_F - g2 - g3;
-  /* Compute solid angle from internal angles. */
-  float S = g0 + g1 - k;
-
-  if (sample_coord) {
-    /* Compute cu. */
-    float au = randu * S + k;
-    float fu = (cosf(au) * b0 - b1) / sinf(au);
-    float cu = 1.0f / sqrtf(fu * fu + b0sq) * (fu > 0.0f ? 1.0f : -1.0f);
-    cu = clamp(cu, -1.0f, 1.0f);
-    /* Compute xu. */
-    float xu = -(cu * z0) / max(sqrtf(1.0f - cu * cu), 1e-7f);
-    xu = clamp(xu, x0, x1);
-    /* Compute yv. */
-    float z0sq = z0 * z0;
-    float y0sq = y0 * y0;
-    float y1sq = y1 * y1;
-    float d = sqrtf(xu * xu + z0sq);
-    float h0 = y0 / sqrtf(d * d + y0sq);
-    float h1 = y1 / sqrtf(d * d + y1sq);
-    float hv = h0 + randv * (h1 - h0), hv2 = hv * hv;
-    float yv = (hv2 < 1.0f - 1e-6f) ? (hv * d) / sqrtf(1.0f - hv2) : y1;
-
-    /* Transform (xu, yv, z0) to world coords. */
-    *light_p = P + xu * x + yv * y + z0 * z;
-  }
-
-  /* return pdf */
-  if (S != 0.0f)
-    return 1.0f / S;
-  else
-    return 0.0f;
-}
-
-ccl_device_inline float3 ellipse_sample(float3 ru, float3 rv, float randu, float randv)
-{
-  to_unit_disk(&randu, &randv);
-  return ru * randu + rv * randv;
-}
-
-ccl_device float3 disk_light_sample(float3 v, float randu, float randv)
-{
-  float3 ru, rv;
-
-  make_orthonormals(v, &ru, &rv);
-
-  return ellipse_sample(ru, rv, randu, randv);
-}
-
-ccl_device float3 distant_light_sample(float3 D, float radius, float randu, float randv)
-{
-  return normalize(D + disk_light_sample(D, randu, randv) * radius);
-}
-
-ccl_device float3
-sphere_light_sample(float3 P, float3 center, float radius, float randu, float randv)
-{
-  return disk_light_sample(normalize(P - center), randu, randv) * radius;
-}
-
-ccl_device float spot_light_attenuation(float3 dir,
-                                        float spot_angle,
-                                        float spot_smooth,
-                                        LightSample *ls)
-{
-  float3 I = ls->Ng;
-
-  float attenuation = dot(dir, I);
-
-  if (attenuation <= spot_angle) {
-    attenuation = 0.0f;
-  }
-  else {
-    float t = attenuation - spot_angle;
-
-    if (t < spot_smooth && spot_smooth != 0.0f)
-      attenuation *= smoothstepf(t / spot_smooth);
-  }
-
-  return attenuation;
-}
-
-ccl_device float lamp_light_pdf(KernelGlobals *kg, const float3 Ng, const float3 I, float t)
-{
-  float cos_pi = dot(Ng, I);
-
-  if (cos_pi <= 0.0f)
-    return 0.0f;
-
-  return t * t / cos_pi;
-}
-
-/* Background Light */
-
-#ifdef __BACKGROUND_MIS__
-
-ccl_device float3 background_map_sample(KernelGlobals *kg, float randu, float randv, float *pdf)
-{
-  /* for the following, the CDF values are actually a pair of floats, with the
-   * function value as X and the actual CDF as Y.  The last entry's function
-   * value is the CDF total. */
-  int res_x = kernel_data.integrator.pdf_background_res_x;
-  int res_y = kernel_data.integrator.pdf_background_res_y;
-  int cdf_width = res_x + 1;
-
-  /* this is basically std::lower_bound as used by pbrt */
-  int first = 0;
-  int count = res_y;
-
-  while (count > 0) {
-    int step = count >> 1;
-    int middle = first + step;
-
-    if (kernel_tex_fetch(__light_background_marginal_cdf, middle).y < randv) {
-      first = middle + 1;
-      count -= step + 1;
-    }
-    else
-      count = step;
-  }
-
-  int index_v = max(0, first - 1);
-  kernel_assert(index_v >= 0 && index_v < res_y);
-
-  float2 cdf_v = kernel_tex_fetch(__light_background_marginal_cdf, index_v);
-  float2 cdf_next_v = kernel_tex_fetch(__light_background_marginal_cdf, index_v + 1);
-  float2 cdf_last_v = kernel_tex_fetch(__light_background_marginal_cdf, res_y);
-
-  /* importance-sampled V direction */
-  float dv = inverse_lerp(cdf_v.y, cdf_next_v.y, randv);
-  float v = (index_v + dv) / res_y;
-
-  /* this is basically std::lower_bound as used by pbrt */
-  first = 0;
-  count = res_x;
-  while (count > 0) {
-    int step = count >> 1;
-    int middle = first + step;
-
-    if (kernel_tex_fetch(__light_background_conditional_cdf, index_v * cdf_width + middle).y <
-        randu) {
-      first = middle + 1;
-      count -= step + 1;
-    }
-    else
-      count = step;
-  }
-
-  int index_u = max(0, first - 1);
-  kernel_assert(index_u >= 0 && index_u < res_x);
-
-  float2 cdf_u = kernel_tex_fetch(__light_background_conditional_cdf,
-                                  index_v * cdf_width + index_u);
-  float2 cdf_next_u = kernel_tex_fetch(__light_background_conditional_cdf,
-                                       index_v * cdf_width + index_u + 1);
-  float2 cdf_last_u = kernel_tex_fetch(__light_background_conditional_cdf,
-                                       index_v * cdf_width + res_x);
-
-  /* importance-sampled U direction */
-  float du = inverse_lerp(cdf_u.y, cdf_next_u.y, randu);
-  float u = (index_u + du) / res_x;
-
-  /* compute pdf */
-  float sin_theta = sinf(M_PI_F * v);
-  float denom = (M_2PI_F * M_PI_F * sin_theta) * cdf_last_u.x * cdf_last_v.x;
-
-  if (sin_theta == 0.0f || denom == 0.0f)
-    *pdf = 0.0f;
-  else
-    *pdf = (cdf_u.x * cdf_v.x) / denom;
-
-  /* compute direction */
-  return equirectangular_to_direction(u, v);
-}
-
-/* TODO(sergey): Same as above, after the release we should consider using
- * 'noinline' for all devices.
- */
-ccl_device float background_map_pdf(KernelGlobals *kg, float3 direction)
-{
-  float2 uv = direction_to_equirectangular(direction);
-  int res_x = kernel_data.integrator.pdf_background_res_x;
-  int res_y = kernel_data.integrator.pdf_background_res_y;
-  int cdf_width = res_x + 1;
-
-  float sin_theta = sinf(uv.y * M_PI_F);
-
-  if 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list