Skip to content

Commit

Permalink
Do not use native trig function in sun baking
Browse files Browse the repository at this point in the history
 (because of poor precision)
  • Loading branch information
actions-user authored and sergcpp committed Jun 18, 2024
1 parent ca8fd6d commit fa2e151
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 19 deletions.
4 changes: 4 additions & 0 deletions internal/RendererGPU_kernels.h
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,8 @@ inline void Ray::NS::Renderer::kernel_ShadeSky(CommandBuffer cmd_buf, const pass
memcpy(uniform_params.light_dir, l.dir.dir, 3 * sizeof(float));
uniform_params.light_dir[3] = l.dir.angle;
memcpy(uniform_params.light_col, l.col, 3 * sizeof(float));
memcpy(uniform_params.light_col_point, l.col, 3 * sizeof(float));
uniform_params.light_col[3] = cosf(l.dir.angle);
if (l.dir.angle != 0.0f) {
const float radius = tanf(l.dir.angle);
uniform_params.light_col[0] *= (PI * radius * radius);
Expand All @@ -881,6 +883,8 @@ inline void Ray::NS::Renderer::kernel_ShadeSky(CommandBuffer cmd_buf, const pass
memcpy(uniform_params.light_dir, value_ptr(light_dir), 3 * sizeof(float));
uniform_params.light_dir[3] = 0.0f;
memcpy(uniform_params.light_col, value_ptr(light_col), 3 * sizeof(float));
memcpy(uniform_params.light_col_point, value_ptr(light_col), 3 * sizeof(float));
uniform_params.light_col[3] = 0.0f;

DispatchComputeIndirect(cmd_buf, pi_shade_sky_, indir_args, indir_args_index * sizeof(DispatchIndirectCommand),
bindings, &uniform_params, sizeof(uniform_params), ctx_->default_descr_alloc(),
Expand Down
16 changes: 16 additions & 0 deletions internal/SceneGPU.h
Original file line number Diff line number Diff line change
Expand Up @@ -1598,6 +1598,14 @@ inline void Ray::NS::Scene::Rebuild_SWRT_TLAS_nolock() {
tlas_root_node_ = bvh_nodes[0];
}

//#define DUMP_SKY_ENV
#ifdef DUMP_SKY_ENV
extern "C" {
int SaveEXR(const float *data, int width, int height, int components, const int save_as_fp16, const char *outfilename,
const char **err);
}
#endif

inline std::vector<Ray::color_rgba8_t> Ray::NS::Scene::CalcSkyEnvTexture(const atmosphere_params_t &params,
const int res[2], const light_t lights[],
Span<const uint32_t> dir_lights) {
Expand Down Expand Up @@ -1651,6 +1659,8 @@ inline std::vector<Ray::color_rgba8_t> Ray::NS::Scene::CalcSkyEnvTexture(const a
memcpy(uniform_params.light_dir, l.dir.dir, 3 * sizeof(float));
uniform_params.light_dir[3] = l.dir.angle;
memcpy(uniform_params.light_col, l.col, 3 * sizeof(float));
memcpy(uniform_params.light_col_point, l.col, 3 * sizeof(float));
uniform_params.light_col[3] = cosf(l.dir.angle);
if (l.dir.angle != 0.0f) {
const float radius = tanf(l.dir.angle);
uniform_params.light_col[0] *= (PI * radius * radius);
Expand All @@ -1669,6 +1679,8 @@ inline std::vector<Ray::color_rgba8_t> Ray::NS::Scene::CalcSkyEnvTexture(const a
memcpy(uniform_params.light_dir, value_ptr(light_dir), 3 * sizeof(float));
uniform_params.light_dir[3] = 0.0f;
memcpy(uniform_params.light_col, value_ptr(light_col), 3 * sizeof(float));
memcpy(uniform_params.light_col_point, value_ptr(light_col), 3 * sizeof(float));
uniform_params.light_col[3] = 0.0f;

DispatchCompute(cmd_buf, pi_bake_sky_, grp_count, bindings, &uniform_params, sizeof(uniform_params),
ctx_->default_descr_alloc(), ctx_->log());
Expand All @@ -1690,6 +1702,10 @@ inline std::vector<Ray::color_rgba8_t> Ray::NS::Scene::CalcSkyEnvTexture(const a
std::vector<color_rgba8_t> rgbe_pixels(res[0] * res[1]);

const float *f32_data = reinterpret_cast<const float *>(temp_readback_buf.Map());
#ifdef DUMP_SKY_ENV
const char *err = nullptr;
SaveEXR(f32_data, res[0], res[1], 4, 0, "sky.exr", &err);
#endif
for (int i = 0; i < res[0] * res[1]; ++i) {
Ref::fvec4 color(&f32_data[4 * i]);
color = rgb_to_rgbe(color);
Expand Down
6 changes: 3 additions & 3 deletions internal/shaders/output/bake_sky.comp.cso.inl

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions internal/shaders/output/bake_sky.comp.spv.inl

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions internal/shaders/output/shade_sky.comp.cso.inl

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions internal/shaders/output/shade_sky.comp.spv.inl

Large diffs are not rendered by default.

10 changes: 3 additions & 7 deletions internal/shaders/shade_sky.comp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -616,13 +616,9 @@ vec3 IntegrateScattering(vec3 ray_start, const vec3 ray_dir, float ray_length, u
// Sun disk (bake directional light into the texture)
//
if (g_params.light_dir.w > 0.0 && planet_intersection.x < 0.0 && light_brightness > 0.0) {
const float cos_theta = cos(g_params.light_dir.w);
vec3 sun_disk = total_transmittance * smoothstep(cos_theta - SKY_SUN_BLEND_VAL, cos_theta + SKY_SUN_BLEND_VAL, costh);
// 'de-multiply' by disk area (to get original brightness)
const float radius = tan(g_params.light_dir.w);
sun_disk /= (PI * radius * radius);

total_radiance += sun_disk * g_params.light_col.xyz;
const float cos_theta = g_params.light_col.w;
const vec3 sun_disk = total_transmittance * smoothstep(cos_theta - SKY_SUN_BLEND_VAL, cos_theta + SKY_SUN_BLEND_VAL, costh);
total_radiance += sun_disk * g_params.light_col_point.xyz;
}

//
Expand Down
1 change: 1 addition & 0 deletions internal/shaders/shade_sky_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ INTERFACE_START(ShadeSky)
struct Params {
vec4 light_dir;
vec4 light_col;
vec4 light_col_point;
vec4 env_col;
vec4 back_col;
//
Expand Down

0 comments on commit fa2e151

Please sign in to comment.