Skip to content

Commit b292ce2

Browse files
committed
Set min sun disk size
1 parent 6232b90 commit b292ce2

File tree

6 files changed

+51
-33
lines changed

6 files changed

+51
-33
lines changed

internal/AtmosphereRef.cpp

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -605,8 +605,9 @@ template std::pair<Ray::Ref::fvec4, Ray::Ref::fvec4> Ray::Ref::IntegrateScatteri
605605

606606
Ray::Ref::fvec4 Ray::Ref::IntegrateScattering(const atmosphere_params_t &params, fvec4 ray_start, const fvec4 &ray_dir,
607607
float ray_length, const fvec4 &light_dir, const float light_angle,
608-
const fvec4 &light_color, Span<const float> transmittance_lut,
609-
Span<const float> multiscatter_lut, uint32_t rand_hash) {
608+
const fvec4 &light_color, const fvec4 &light_color_point,
609+
Span<const float> transmittance_lut, Span<const float> multiscatter_lut,
610+
uint32_t rand_hash) {
610611
const fvec2 atm_intersection = AtmosphereIntersection(params, ray_start, ray_dir);
611612
ray_length = fminf(ray_length, atm_intersection.get<1>());
612613
if (atm_intersection.get<0>() > 0) {
@@ -651,7 +652,7 @@ Ray::Ref::fvec4 Ray::Ref::IntegrateScattering(const atmosphere_params_t &params,
651652
rand_hash = hash(rand_hash);
652653

653654
total_radiance += IntegrateScatteringMain(params, ray_start, ray_dir, pre_atmosphere_ray_length, light_dir,
654-
moon_dir, light_color, transmittance_lut, multiscatter_lut,
655+
moon_dir, light_color_point, transmittance_lut, multiscatter_lut,
655656
rand_offset, SKY_PRE_ATMOSPHERE_SAMPLE_COUNT, total_transmittance)
656657
.first;
657658
}
@@ -754,7 +755,7 @@ Ray::Ref::fvec4 Ray::Ref::IntegrateScattering(const atmosphere_params_t &params,
754755
float cloud_blend = linstep(SKY_CLOUDS_HORIZON_CUTOFF + 0.25f, SKY_CLOUDS_HORIZON_CUTOFF, ray_dir.get<1>());
755756
cloud_blend = 1.0f - powf(cloud_blend, 5.0f);
756757

757-
total_radiance += cloud_blend * clouds * light_color;
758+
total_radiance += cloud_blend * clouds * light_color_point;
758759
total_transmittance = mix(transmittance_before, total_transmittance, cloud_blend);
759760
}
760761
}
@@ -808,11 +809,11 @@ Ray::Ref::fvec4 Ray::Ref::IntegrateScattering(const atmosphere_params_t &params,
808809
}
809810

810811
if (light_dir.get<1>() > -0.025f) {
811-
total_radiance +=
812-
total_transmittance * GetLightEnergy(0.002f, dC, phase_w) * light_transmittance * dC * light_color;
812+
total_radiance += total_transmittance * GetLightEnergy(0.002f, dC, phase_w) * light_transmittance * dC *
813+
light_color_point;
813814
} else if (params.moon_radius > 0.0f) {
814815
total_radiance += SKY_MOON_SUN_RELATION * total_transmittance * GetLightEnergy(0.002f, dC, moon_phase_w) *
815-
moon_transmittance * dC * light_color;
816+
moon_transmittance * dC * light_color_point;
816817
}
817818
total_transmittance *= expf(-dC * 0.002f * 1000.0f);
818819
}
@@ -833,7 +834,7 @@ Ray::Ref::fvec4 Ray::Ref::IntegrateScattering(const atmosphere_params_t &params,
833834
rand_hash = hash(rand_hash);
834835

835836
total_radiance += IntegrateScatteringMain(params, main_ray_start, ray_dir, main_ray_length, light_dir, moon_dir,
836-
light_color, transmittance_lut, multiscatter_lut, rand_offset,
837+
light_color_point, transmittance_lut, multiscatter_lut, rand_offset,
837838
SKY_MAIN_ATMOSPHERE_SAMPLE_COUNT, total_transmittance)
838839
.first;
839840
}
@@ -842,14 +843,10 @@ Ray::Ref::fvec4 Ray::Ref::IntegrateScattering(const atmosphere_params_t &params,
842843
// Sun disk (bake directional light into the texture)
843844
//
844845
if (light_angle > 0.0f && planet_intersection.get<0>() < 0.0f && light_brightness > 0.0f) {
845-
const float cos_theta = cosf(light_angle);
846+
const float cos_theta = cosf(fmaxf(light_angle, SKY_SUN_MIN_ANGLE));
846847
const float smooth_blend_val = fminf(SKY_SUN_BLEND_VAL, 1.0f - cos_theta);
847-
fvec4 sun_disk =
848+
const fvec4 sun_disk =
848849
total_transmittance * smoothstep(cos_theta - smooth_blend_val, cos_theta + smooth_blend_val, costh);
849-
// 'de-multiply' by disk area (to get original brightness)
850-
const float radius = tanf(light_angle);
851-
sun_disk /= (PI * radius * radius);
852-
853850
total_radiance += sun_disk * light_color;
854851
}
855852

@@ -959,22 +956,24 @@ void Ray::Ref::ShadeSky(const pass_settings_t &ps, const float limit, Span<const
959956
const light_t &l = sc.lights[li_index];
960957

961958
const fvec4 light_dir = {l.dir.dir[0], l.dir.dir[1], l.dir.dir[2], 0.0f};
962-
fvec4 light_col = {l.col[0], l.col[1], l.col[2], 0.0f};
959+
const fvec4 light_col = {l.col[0], l.col[1], l.col[2], 0.0f};
960+
fvec4 light_col_point = light_col;
963961
if (l.dir.tan_angle != 0.0f) {
964962
const float radius = l.dir.tan_angle;
965-
light_col *= (PI * radius * radius);
963+
light_col_point *= (PI * radius * radius);
966964
}
967965

968-
color += IntegrateScattering(
969-
sc.env.atmosphere, fvec4{0.0f, sc.env.atmosphere.viewpoint_height, 0.0f, 0.0f}, I, MAX_DIST,
970-
light_dir, l.dir.angle, light_col, sc.sky_transmittance_lut, sc.sky_multiscatter_lut, rand_hash);
966+
color +=
967+
IntegrateScattering(sc.env.atmosphere, fvec4{0.0f, sc.env.atmosphere.viewpoint_height, 0.0f, 0.0f},
968+
I, MAX_DIST, light_dir, l.dir.angle, light_col, light_col_point,
969+
sc.sky_transmittance_lut, sc.sky_multiscatter_lut, rand_hash);
971970
}
972971
} else if (sc.env.atmosphere.stars_brightness > 0.0f) {
973972
// Use fake lightsource (to light up the moon)
974973
const fvec4 light_dir = {0.0f, -1.0f, 0.0f, 0.0f}, light_col = {144809.859f, 129443.617f, 127098.89f, 0.0f};
975974

976975
color += IntegrateScattering(sc.env.atmosphere, fvec4{0.0f, sc.env.atmosphere.viewpoint_height, 0.0f, 0.0f},
977-
I, MAX_DIST, light_dir, 0.0f, light_col, sc.sky_transmittance_lut,
976+
I, MAX_DIST, light_dir, 0.0f, light_col, light_col, sc.sky_transmittance_lut,
978977
sc.sky_multiscatter_lut, rand_hash);
979978
}
980979

internal/AtmosphereRef.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ std::pair<fvec4, fvec4> IntegrateScatteringMain(const atmosphere_params_t &param
1616

1717
fvec4 IntegrateScattering(const atmosphere_params_t &params, fvec4 ray_start, const fvec4 &ray_dir, float ray_length,
1818
const fvec4 &light_dir, float light_angle, const fvec4 &light_color,
19-
Span<const float> transmittance_lut, Span<const float> multiscatter_lut, uint32_t rand_hash);
19+
const fvec4 &light_color_point, Span<const float> transmittance_lut,
20+
Span<const float> multiscatter_lut, uint32_t rand_hash);
2021

2122
// Transmittance LUT function parameterisation from Bruneton 2017
2223
// https://github.com/ebruneton/precomputed_atmospheric_scattering

internal/Constants.inl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,4 @@ const float SKY_CLOUDS_OFFSET_SCALE = 0.00007f;
157157
const float SKY_MOON_SUN_RELATION = 0.0000001f;
158158
const float SKY_STARS_THRESHOLD = 14.0f;
159159
const float SKY_SUN_BLEND_VAL = 0.000005f;
160+
const float SKY_SUN_MIN_ANGLE = 0.0005f;

internal/CoreSIMD.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8193,24 +8193,30 @@ void Ray::NS::ShadeSky(const pass_settings_t &ps, float limit, Span<const hit_da
81938193

81948194
const Ref::fvec4 light_dir = {l.dir.dir[0], l.dir.dir[1], l.dir.dir[2], 0.0f};
81958195
Ref::fvec4 light_col = {l.col[0], l.col[1], l.col[2], 0.0f};
8196+
Ref::fvec4 light_col_point = light_col;
81968197
if (l.dir.tan_angle != 0.0f) {
81978198
const float radius = l.dir.tan_angle;
8198-
light_col *= (PI * radius * radius);
8199+
light_col_point *= (PI * radius * radius);
8200+
}
8201+
if (l.dir.angle < SKY_SUN_MIN_ANGLE) {
8202+
const float div = PI * tanf(SKY_SUN_MIN_ANGLE) * tanf(SKY_SUN_MIN_ANGLE);
8203+
light_col = light_col_point / div;
81998204
}
82008205

82018206
color += Ref::IntegrateScattering(sc.env.atmosphere,
82028207
Ref::fvec4{0.0f, sc.env.atmosphere.viewpoint_height, 0.0f, 0.0f},
8203-
_I, MAX_DIST, light_dir, l.dir.angle, light_col,
8208+
_I, MAX_DIST, light_dir, l.dir.angle, light_col, light_col_point,
82048209
sc.sky_transmittance_lut, sc.sky_multiscatter_lut, rand_hash[j]);
82058210
}
82068211
} else if (sc.env.atmosphere.stars_brightness > 0.0f) {
82078212
// Use fake lightsource (to light up the moon)
82088213
const Ref::fvec4 light_dir = {0.0f, -1.0f, 0.0f, 0.0f},
82098214
light_col = {144809.859f, 129443.617f, 127098.89f, 0.0f};
82108215

8211-
color += Ref::IntegrateScattering(
8212-
sc.env.atmosphere, Ref::fvec4{0.0f, sc.env.atmosphere.viewpoint_height, 0.0f, 0.0f}, _I, MAX_DIST,
8213-
light_dir, 0.0f, light_col, sc.sky_transmittance_lut, sc.sky_multiscatter_lut, rand_hash[j]);
8216+
color += Ref::IntegrateScattering(sc.env.atmosphere,
8217+
Ref::fvec4{0.0f, sc.env.atmosphere.viewpoint_height, 0.0f, 0.0f}, _I,
8218+
MAX_DIST, light_dir, 0.0f, light_col, light_col,
8219+
sc.sky_transmittance_lut, sc.sky_multiscatter_lut, rand_hash[j]);
82148220
}
82158221

82168222
color *= mis_weight[j];

internal/SceneCommon.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ void Ray::SceneCommon::SetCamera_nolock(const CameraHandle i, const camera_desc_
178178
// #define DUMP_SKY_ENV
179179
#ifdef DUMP_SKY_ENV
180180
extern "C" {
181-
int SaveEXR(const float *data, int width, int height, int components, const int save_as_fp16,
182-
const char *outfilename, const char **err);
181+
int SaveEXR(const float *data, int width, int height, int components, const int save_as_fp16, const char *outfilename,
182+
const char **err);
183183
}
184184
#endif
185185

@@ -313,22 +313,27 @@ Ray::SceneCommon::CalcSkyEnvTexture(const atmosphere_params_t &params, const int
313313

314314
const Ref::fvec4 light_dir = {l.dir.dir[0], l.dir.dir[1], l.dir.dir[2], 0.0f};
315315
Ref::fvec4 light_col = {l.col[0], l.col[1], l.col[2], 0.0f};
316+
Ref::fvec4 light_col_point = light_col;
316317
if (l.dir.tan_angle != 0.0f) {
317318
const float radius = l.dir.tan_angle;
318-
light_col *= (PI * radius * radius);
319+
light_col_point *= (PI * radius * radius);
320+
}
321+
if (l.dir.angle < SKY_SUN_MIN_ANGLE) {
322+
const float div = PI * tanf(SKY_SUN_MIN_ANGLE) * tanf(SKY_SUN_MIN_ANGLE);
323+
light_col = light_col_point / div;
319324
}
320325

321326
color += IntegrateScattering(params, Ref::fvec4{0.0f, params.viewpoint_height, 0.0f, 0.0f}, ray_dir,
322-
MAX_DIST, light_dir, l.dir.angle, light_col, sky_transmittance_lut_,
323-
sky_multiscatter_lut_, px_hash);
327+
MAX_DIST, light_dir, l.dir.angle, light_col, light_col_point,
328+
sky_transmittance_lut_, sky_multiscatter_lut_, px_hash);
324329
}
325330
} else if (params.stars_brightness > 0.0f) {
326331
// Use fake lightsource (to light up the moon)
327332
const Ref::fvec4 light_dir = {0.0f, -1.0f, 0.0f, 0.0f},
328333
light_col = {144809.859f, 129443.617f, 127098.89f, 0.0f};
329334

330335
color += IntegrateScattering(params, Ref::fvec4{0.0f, params.viewpoint_height, 0.0f, 0.0f}, ray_dir,
331-
MAX_DIST, light_dir, 0.0f, light_col, sky_transmittance_lut_,
336+
MAX_DIST, light_dir, 0.0f, light_col, light_col, sky_transmittance_lut_,
332337
sky_multiscatter_lut_, px_hash);
333338
}
334339

internal/SceneGPU.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1695,13 +1695,19 @@ inline std::vector<Ray::color_rgba8_t> Ray::NS::Scene::CalcSkyEnvTexture(const a
16951695
uniform_params.light_dir[3] = l.dir.angle;
16961696
memcpy(uniform_params.light_col, l.col, 3 * sizeof(float));
16971697
memcpy(uniform_params.light_col_point, l.col, 3 * sizeof(float));
1698-
uniform_params.light_col[3] = cosf(l.dir.angle);
1698+
uniform_params.light_col[3] = cosf(fmaxf(l.dir.angle, SKY_SUN_MIN_ANGLE));
16991699
if (l.dir.tan_angle != 0.0f) {
17001700
const float radius = l.dir.tan_angle;
17011701
uniform_params.light_col_point[0] *= (PI * radius * radius);
17021702
uniform_params.light_col_point[1] *= (PI * radius * radius);
17031703
uniform_params.light_col_point[2] *= (PI * radius * radius);
17041704
}
1705+
if (l.dir.angle < SKY_SUN_MIN_ANGLE) {
1706+
const float div = PI * tanf(SKY_SUN_MIN_ANGLE) * tanf(SKY_SUN_MIN_ANGLE);
1707+
uniform_params.light_col[0] = uniform_params.light_col_point[0] / div;
1708+
uniform_params.light_col[1] = uniform_params.light_col_point[1] / div;
1709+
uniform_params.light_col[2] = uniform_params.light_col_point[2] / div;
1710+
}
17051711

17061712
DispatchCompute(cmd_buf, pi_bake_sky_, grp_count, bindings, &uniform_params, sizeof(uniform_params),
17071713
ctx_->default_descr_alloc(), ctx_->log());

0 commit comments

Comments
 (0)