Skip to content

Commit

Permalink
Clouds curl texture
Browse files Browse the repository at this point in the history
  • Loading branch information
sergcpp committed Jul 11, 2024
1 parent 46ec5a3 commit 7618818
Show file tree
Hide file tree
Showing 16 changed files with 98 additions and 20 deletions.
31 changes: 29 additions & 2 deletions internal/AtmosphereRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace Ray {
#include "precomputed/__3d_noise_tex.inl"
#include "precomputed/__cirrus_tex.inl"
#include "precomputed/__curl_tex.inl"
#include "precomputed/__moon_tex.inl"
#include "precomputed/__weather_tex.inl"

Expand Down Expand Up @@ -284,6 +285,26 @@ float Sample3dNoiseTex(fvec4 uvw) {
return ((1.0f - k.get<2>()) * n0xx + k.get<2>() * n1xx) / 255.0f;
}

force_inline fvec4 FetchCurlTex(const int x, const int y) {
return fvec4{float(__curl_tex[3 * (y * CURL_TEX_RES + x) + 0]), float(__curl_tex[3 * (y * CURL_TEX_RES + x) + 1]),
float(__curl_tex[3 * (y * CURL_TEX_RES + x) + 2]), 0.0f};
}

fvec4 SampleCurlTex(fvec4 uv) {
uv = fract(uv - 0.5f / CURL_TEX_RES) * fvec4(CURL_TEX_RES);
auto iuv0 = ivec4{uv};
iuv0 = clamp(iuv0, ivec4{0}, ivec4{CURL_TEX_RES - 1, CURL_TEX_RES - 1, 0, 0});
const ivec4 iuv1 = (iuv0 + 1) & ivec4{CURL_TEX_RES - 1, CURL_TEX_RES - 1, 0, 0};

const fvec4 m00 = FetchCurlTex(iuv0.get<0>(), iuv0.get<1>()), m01 = FetchCurlTex(iuv1.get<0>(), iuv0.get<1>()),
m10 = FetchCurlTex(iuv0.get<0>(), iuv1.get<1>()), m11 = FetchCurlTex(iuv1.get<0>(), iuv1.get<1>());

const fvec4 k = fract(uv);
const fvec4 m0 = m01 * k.get<0>() + m00 * (1.0f - k.get<0>()), m1 = m11 * k.get<0>() + m10 * (1.0f - k.get<0>());

return srgb_to_linear((m1 * k.get<1>() + m0 * (1.0f - k.get<1>())) * (1.0f / 255.0f));
}

// Taken from https://github.com/armory3d/armory_ci/blob/master/build_untitled/compiled/Shaders/world_pass.frag.glsl
float GetDensityHeightGradientForPoint(float height, float cloud_type) {
const auto stratusGrad = fvec4(0.02f, 0.05f, 0.09f, 0.11f);
Expand Down Expand Up @@ -321,6 +342,14 @@ float GetCloudsDensity(const atmosphere_params_t &params, fvec4 local_position,

local_position /= 1.5f * (params.clouds_height_end - params.clouds_height_beg);

// TODO: Apply animated cloud offset here
const fvec4 curl_read0 = SampleCurlTex(8.0f * fvec4{local_position.get<0>(), local_position.get<2>(), 0.0f, 0.0f});
local_position += curl_read0 * out_height_fraction * 0.25f;

fvec4 curl_read1 = SampleCurlTex(16.0f * fvec4{local_position.get<1>(), local_position.get<0>(), 0.0f, 0.0f});
curl_read1 = fvec4{curl_read1.get<1>(), curl_read1.get<2>(), curl_read1.get<0>(), 0.0f};
local_position += curl_read1 * (1.0f - out_height_fraction) * 0.05f;

const float noise_read = Sample3dNoiseTex(local_position);
return 3.0f * Ray::mix(fmaxf(0.0f, 1.0f - cloud_type * 2.0f), 1.0f, out_height_fraction) *
remap(cloud_coverage, 0.6f * noise_read);
Expand Down Expand Up @@ -398,7 +427,6 @@ fvec4 SampleMoonTex(fvec4 uv) {
m10 = FetchMoonTex(iuv0.get<0>(), iuv1.get<1>()), m11 = FetchMoonTex(iuv1.get<0>(), iuv1.get<1>());

const fvec4 k = fract(uv);

const fvec4 m0 = m01 * k.get<0>() + m00 * (1.0f - k.get<0>()), m1 = m11 * k.get<0>() + m10 * (1.0f - k.get<0>());

return srgb_to_linear((m1 * k.get<1>() + m0 * (1.0f - k.get<1>())) * (1.0f / 255.0f));
Expand All @@ -419,7 +447,6 @@ fvec4 SampleCirrusTex(fvec4 uv) {
m10 = FetchCirrusTex(iuv0.get<0>(), iuv1.get<1>()), m11 = FetchCirrusTex(iuv1.get<0>(), iuv1.get<1>());

const fvec4 k = fract(uv);

const fvec4 m0 = m01 * k.get<0>() + m00 * (1.0f - k.get<0>()), m1 = m11 * k.get<0>() + m10 * (1.0f - k.get<0>());

return srgb_to_linear((m1 * k.get<1>() + m0 * (1.0f - k.get<1>())) * (1.0f / 255.0f));
Expand Down
2 changes: 1 addition & 1 deletion internal/Constants.inl
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ const int SKY_TRANSMITTANCE_LUT_H = 64;
const int SKY_MULTISCATTER_LUT_RES = 32;
const int SKY_PRE_ATMOSPHERE_SAMPLE_COUNT = 4;
const int SKY_MAIN_ATMOSPHERE_SAMPLE_COUNT = 12;
const int SKY_CLOUDS_SAMPLE_COUNT = 32;
const int SKY_CLOUDS_SAMPLE_COUNT = 48;
const float SKY_CLOUDS_HORIZON_CUTOFF = 0.005f;
const float SKY_MOON_SUN_RELATION = 0.0000001f;
const float SKY_STARS_THRESHOLD = 14.0f;
Expand Down
2 changes: 2 additions & 0 deletions internal/RendererDX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,7 @@ void Ray::Dx::Renderer::RenderScene(const SceneBase &scene, RegionContext &regio
s.sky_moon_tex_,
s.sky_weather_tex_,
s.sky_cirrus_tex_,
s.sky_curl_tex_,
s.sky_noise3d_tex_};

TransitionSceneResources(cmd_buf, sc_data);
Expand Down Expand Up @@ -1406,6 +1407,7 @@ void Ray::Dx::Renderer::UpdateSpatialCache(const SceneBase &scene, RegionContext
{},
{},
{},
{},
{}};

#if !RUN_IN_LOCKSTEP
Expand Down
1 change: 1 addition & 0 deletions internal/RendererGPU.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ struct scene_data_t {
const Texture2D &moon_tex;
const Texture2D &weather_tex;
const Texture2D &cirrus_tex;
const Texture2D &curl_tex;
const Texture3D &noise3d_tex;
};

Expand Down
1 change: 1 addition & 0 deletions internal/RendererGPU_kernels.h
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,7 @@ inline void Ray::NS::Renderer::kernel_ShadeSky(CommandBuffer cmd_buf, const pass
{eBindTarget::Tex2DSampled, ShadeSky::MOON_TEX_SLOT, sc_data.moon_tex},
{eBindTarget::Tex2DSampled, ShadeSky::WEATHER_TEX_SLOT, sc_data.weather_tex},
{eBindTarget::Tex2DSampled, ShadeSky::CIRRUS_TEX_SLOT, sc_data.cirrus_tex},
{eBindTarget::Tex2DSampled, ShadeSky::CURL_TEX_SLOT, sc_data.curl_tex},
{eBindTarget::Tex3DSampled, ShadeSky::NOISE3D_TEX_SLOT, sc_data.noise3d_tex},
{eBindTarget::Image, ShadeSky::OUT_IMG_SLOT, out_img}};

Expand Down
2 changes: 2 additions & 0 deletions internal/RendererVK.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,7 @@ void Ray::Vk::Renderer::RenderScene(const SceneBase &scene, RegionContext &regio
s.sky_moon_tex_,
s.sky_weather_tex_,
s.sky_cirrus_tex_,
s.sky_curl_tex_,
s.sky_noise3d_tex_};
TransitionSceneResources(cmd_buf, sc_data);

Expand Down Expand Up @@ -1450,6 +1451,7 @@ void Ray::Vk::Renderer::UpdateSpatialCache(const SceneBase &scene, RegionContext
{},
{},
{},
{},
{}};

#if !RUN_IN_LOCKSTEP
Expand Down
36 changes: 34 additions & 2 deletions internal/SceneGPU.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ extern const int NOISE_3D_RES;
extern const uint8_t __3d_noise_tex[];
extern const int CIRRUS_TEX_RES;
extern const uint8_t __cirrus_tex[];
extern const int CURL_TEX_RES;
extern const uint8_t __curl_tex[];

Ref::fvec4 rgb_to_rgbe(const Ref::fvec4 &rgb);
namespace NS {
Expand Down Expand Up @@ -82,7 +84,7 @@ class Scene : public SceneCommon {
std::vector<uint32_t> dir_lights_; // compacted list of all directional lights

Texture2D sky_transmittance_lut_tex_, sky_multiscatter_lut_tex_;
Texture2D sky_moon_tex_, sky_weather_tex_, sky_cirrus_tex_;
Texture2D sky_moon_tex_, sky_weather_tex_, sky_cirrus_tex_, sky_curl_tex_;
Texture3D sky_noise3d_tex_;

LightHandle env_map_light_ = InvalidLightHandle;
Expand Down Expand Up @@ -1598,7 +1600,7 @@ inline void Ray::NS::Scene::Rebuild_SWRT_TLAS_nolock() {
tlas_root_node_ = bvh_nodes[0];
}

//#define DUMP_SKY_ENV
// #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,
Expand Down Expand Up @@ -1642,6 +1644,7 @@ inline std::vector<Ray::color_rgba8_t> Ray::NS::Scene::CalcSkyEnvTexture(const a
{eBindTarget::Tex2DSampled, ShadeSky::MOON_TEX_SLOT, sky_moon_tex_},
{eBindTarget::Tex2DSampled, ShadeSky::WEATHER_TEX_SLOT, sky_weather_tex_},
{eBindTarget::Tex2DSampled, ShadeSky::CIRRUS_TEX_SLOT, sky_cirrus_tex_},
{eBindTarget::Tex2DSampled, ShadeSky::CURL_TEX_SLOT, sky_curl_tex_},
{eBindTarget::Tex3DSampled, ShadeSky::NOISE3D_TEX_SLOT, sky_noise3d_tex_},
{eBindTarget::Image, ShadeSky::OUT_IMG_SLOT, temp_img}};

Expand Down Expand Up @@ -1831,6 +1834,35 @@ Ray::NS::Scene::PrepareSkyEnvMap_nolock(const std::function<void(int, int, Paral
stage_buf.FreeImmediate();
}

if (!sky_curl_tex_.ready()) {
Tex2DParams params;
params.w = params.h = CURL_TEX_RES;
params.format = eTexFormat::RawRGBA8888;
params.flags = eTexFlags::SRGB;
params.usage = eTexUsageBits::Sampled | eTexUsageBits::Transfer;
params.sampling.filter = eTexFilter::BilinearNoMipmap;
params.sampling.wrap = eTexWrap::Repeat;

sky_curl_tex_ = Texture2D{"Curl Tex", ctx_, params, ctx_->default_memory_allocs(), log_};

Buffer stage_buf = Buffer("Temp stage buf", ctx_, eBufType::Upload, 4 * CURL_TEX_RES * CURL_TEX_RES);
uint8_t *mapped_ptr = stage_buf.Map();
for (int i = 0; i < CURL_TEX_RES * CURL_TEX_RES; ++i) {
mapped_ptr[4 * i + 0] = __curl_tex[3 * i + 0];
mapped_ptr[4 * i + 1] = __curl_tex[3 * i + 1];
mapped_ptr[4 * i + 2] = __curl_tex[3 * i + 2];
mapped_ptr[4 * i + 3] = 255;
}
stage_buf.Unmap();

CommandBuffer cmd_buf = BegSingleTimeCommands(ctx_->api(), ctx_->device(), ctx_->temp_command_pool());
sky_curl_tex_.SetSubImage(0, 0, 0, CURL_TEX_RES, CURL_TEX_RES, eTexFormat::RawRGBA8888, stage_buf, cmd_buf, 0,
stage_buf.size());
EndSingleTimeCommands(ctx_->api(), ctx_->device(), ctx_->graphics_queue(), cmd_buf, ctx_->temp_command_pool());

stage_buf.FreeImmediate();
}

if (!sky_noise3d_tex_.handle()) {
Tex3DParams params;
params.w = params.h = params.d = NOISE_3D_RES;
Expand Down
4 changes: 4 additions & 0 deletions internal/precomputed/__curl_tex.inl

Large diffs are not rendered by default.

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.

8 changes: 8 additions & 0 deletions internal/shaders/shade_sky.comp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ layout(binding = MULTISCATTER_LUT_SLOT) uniform sampler2D g_multiscatter_lut;
layout(binding = MOON_TEX_SLOT) uniform sampler2D g_moon_tex;
layout(binding = WEATHER_TEX_SLOT) uniform sampler2D g_weather_tex;
layout(binding = CIRRUS_TEX_SLOT) uniform sampler2D g_cirrus_tex;
layout(binding = CURL_TEX_SLOT) uniform sampler2D g_curl_tex;
layout(binding = NOISE3D_TEX_SLOT) uniform sampler3D g_noise3d_tex;

layout(binding = OUT_IMG_SLOT, rgba32f) uniform image2D g_out_img;
Expand Down Expand Up @@ -378,6 +379,13 @@ float GetCloudsDensity(vec3 local_position, out float out_local_height, out floa

local_position /= 1.5 * (g_atmosphere_params.clouds_height_end - g_atmosphere_params.clouds_height_beg);

// TODO: Apply animated cloud offset here
const vec3 curl_read0 = textureLod(g_curl_tex, 8.0 * local_position.xz, 0.0).xyz;
local_position += curl_read0 * out_height_fraction * 0.25;

const vec3 curl_read1 = textureLod(g_curl_tex, 16.0 * local_position.yx, 0.0).yzx;
local_position += curl_read1 * (1.0 - out_height_fraction) * 0.05;

const float noise_read = textureLod(g_noise3d_tex, local_position, 0.0).x;
return 3.0 * mix(max(0.0, 1.0 - cloud_type * 2.0), 1.0, out_height_fraction) *
remap(cloud_coverage, 0.6 * noise_read);
Expand Down
3 changes: 2 additions & 1 deletion internal/shaders/shade_sky_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ const int MULTISCATTER_LUT_SLOT = 10;
const int MOON_TEX_SLOT = 11;
const int WEATHER_TEX_SLOT = 12;
const int CIRRUS_TEX_SLOT = 13;
const int NOISE3D_TEX_SLOT = 14;
const int CURL_TEX_SLOT = 14;
const int NOISE3D_TEX_SLOT = 15;

const int OUT_IMG_SLOT = 0;

Expand Down
Binary file modified samples/05_physical_sky.tga
Binary file not shown.
4 changes: 2 additions & 2 deletions tests/test_materials.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1625,7 +1625,7 @@ void test_complex_mat5_dir_light(const char *arch_list[], const char *preferred_
}

void test_complex_mat5_sun_light(const char *arch_list[], const char *preferred_device) {
const int SampleCount = 13;
const int SampleCount = 14;
const double MinPSNR = 24.05;
const int PixThres = 5219;

Expand All @@ -1646,7 +1646,7 @@ void test_complex_mat5_sun_light(const char *arch_list[], const char *preferred_
}

void test_complex_mat5_moon_light(const char *arch_list[], const char *preferred_device) {
const int SampleCount = 12;
const int SampleCount = 14;
const int PixThres = 370;

Ray::principled_mat_desc_t metal_mat_desc;
Expand Down

0 comments on commit 7618818

Please sign in to comment.