add clouds

This commit is contained in:
Wls50
2025-12-10 19:06:19 +01:00
committed by Hirek
parent 22479671d4
commit 0794a0f621
16 changed files with 258 additions and 50 deletions

View File

@@ -14,6 +14,8 @@ void MaConfig::Init(const YAML::Node& node) {
m_weight_lines = node["wire_diameter_lines"].as<float>(.01f); m_weight_lines = node["wire_diameter_lines"].as<float>(.01f);
m_weight_tractions = node["wire_diameter_tractions"].as<float>(.005f); m_weight_tractions = node["wire_diameter_tractions"].as<float>(.005f);
m_envmap_resolution = node["envmap_resolution"].as<int>(256); m_envmap_resolution = node["envmap_resolution"].as<int>(256);
m_cloud_texture = node["cloud_texture"].as<std::string>("textures/clouds/clear");
m_high_cloud_texture = node["high_cloud_texture"].as<std::string>("textures/clouds/clear");
} }
void MaConfig::MaConfigDisplay::Init(const YAML::Node& node) { void MaConfig::MaConfigDisplay::Init(const YAML::Node& node) {

View File

@@ -38,6 +38,8 @@ struct MaConfig {
float m_min_luminance_ev; float m_min_luminance_ev;
float m_max_luminance_ev; float m_max_luminance_ev;
} m_lighting; } m_lighting;
std::string m_cloud_texture;
std::string m_high_cloud_texture;
int m_envmap_resolution; int m_envmap_resolution;
float m_weight_lines; float m_weight_lines;
float m_weight_tractions; float m_weight_tractions;

View File

@@ -7,6 +7,7 @@
#include "gbuffer.h" #include "gbuffer.h"
#include "nvrenderer/nvrenderer.h" #include "nvrenderer/nvrenderer.h"
#include "nvrendererbackend.h" #include "nvrendererbackend.h"
#include "nvtexture.h"
#include "simulationenvironment.h" #include "simulationenvironment.h"
#include "sky.h" #include "sky.h"
@@ -50,6 +51,8 @@ nvrhi::BindingSetHandle MaEnvironment::GetBindingSet(int pass, int mip, int set,
Format::UNKNOWN, AllSubresources)) Format::UNKNOWN, AllSubresources))
.addItem(BindingSetItem::Texture_SRV(13, m_sky_texture)) .addItem(BindingSetItem::Texture_SRV(13, m_sky_texture))
.addItem(BindingSetItem::Texture_SRV(14, m_aerial_lut)) .addItem(BindingSetItem::Texture_SRV(14, m_aerial_lut))
.addItem(BindingSetItem::Texture_SRV(15, m_clouds_texture))
.addItem(BindingSetItem::Texture_SRV(16, m_high_clouds_texture))
.addItem(BindingSetItem::Texture_UAV( .addItem(BindingSetItem::Texture_UAV(
0, m_dynamic_skybox[set], Format::UNKNOWN, 0, m_dynamic_skybox[set], Format::UNKNOWN,
TextureSubresourceSet( TextureSubresourceSet(
@@ -58,6 +61,7 @@ nvrhi::BindingSetHandle MaEnvironment::GetBindingSet(int pass, int mip, int set,
.addItem(BindingSetItem::Sampler(0, m_sampler_linear_clamp)) .addItem(BindingSetItem::Sampler(0, m_sampler_linear_clamp))
.addItem(BindingSetItem::Sampler(1, m_sampler_point_clamp)) .addItem(BindingSetItem::Sampler(1, m_sampler_point_clamp))
.addItem(BindingSetItem::Sampler(13, m_sampler_linear_clamp)) .addItem(BindingSetItem::Sampler(13, m_sampler_linear_clamp))
.addItem(BindingSetItem::Sampler(15, m_sampler_linear_clamp))
.addItem(BindingSetItem::ConstantBuffer( .addItem(BindingSetItem::ConstantBuffer(
13, m_sky->m_sky_constants)) 13, m_sky->m_sky_constants))
.addItem(BindingSetItem::ConstantBuffer( .addItem(BindingSetItem::ConstantBuffer(
@@ -125,11 +129,13 @@ nvrhi::BindingSetHandle MaEnvironment::GetBindingSet(int pass, int mip, int set,
MaEnvironment::MaEnvironment(NvRenderer* renderer) MaEnvironment::MaEnvironment(NvRenderer* renderer)
: MaResourceRegistry(renderer), : MaResourceRegistry(renderer),
m_renderer(renderer),
m_backend(renderer->m_backend.get()), m_backend(renderer->m_backend.get()),
m_gbuffer(renderer->m_gbuffer.get()), m_gbuffer(renderer->m_gbuffer.get()),
m_sky(renderer->m_sky.get()) {} m_sky() {}
void MaEnvironment::Init(const std::string& texture, float pre_exposure) { void MaEnvironment::Init(const std::string& texture, float pre_exposure) {
m_sky = m_renderer->m_sky.get();
InitResourceRegistry(); InitResourceRegistry();
using namespace nvrhi; using namespace nvrhi;
m_cs_sample_equirectangular = m_backend->CreateShader( m_cs_sample_equirectangular = m_backend->CreateShader(
@@ -174,10 +180,13 @@ void MaEnvironment::Init(const std::string& texture, float pre_exposure) {
.addItem(BindingLayoutItem::Texture_SRV(5)) .addItem(BindingLayoutItem::Texture_SRV(5))
.addItem(BindingLayoutItem::Texture_SRV(13)) .addItem(BindingLayoutItem::Texture_SRV(13))
.addItem(BindingLayoutItem::Texture_SRV(14)) .addItem(BindingLayoutItem::Texture_SRV(14))
.addItem(BindingLayoutItem::Texture_SRV(15))
.addItem(BindingLayoutItem::Texture_SRV(16))
.addItem(BindingLayoutItem::Texture_UAV(0)) .addItem(BindingLayoutItem::Texture_UAV(0))
.addItem(BindingLayoutItem::Sampler(0)) .addItem(BindingLayoutItem::Sampler(0))
.addItem(BindingLayoutItem::Sampler(1)) .addItem(BindingLayoutItem::Sampler(1))
.addItem(BindingLayoutItem::Sampler(13)) .addItem(BindingLayoutItem::Sampler(13))
.addItem(BindingLayoutItem::Sampler(15))
.addItem(BindingLayoutItem::VolatileConstantBuffer(13)) .addItem(BindingLayoutItem::VolatileConstantBuffer(13))
.addItem(BindingLayoutItem::ConstantBuffer(1)) .addItem(BindingLayoutItem::ConstantBuffer(1))
.addItem(BindingLayoutItem::PushConstants( .addItem(BindingLayoutItem::PushConstants(
@@ -305,6 +314,25 @@ void MaEnvironment::Init(const std::string& texture, float pre_exposure) {
RegisterResource(true, "sampler_linear_clamp_v_repeat_h", RegisterResource(true, "sampler_linear_clamp_v_repeat_h",
m_sampler_linear_clamp_v_repeat_h, ResourceType::Sampler); m_sampler_linear_clamp_v_repeat_h, ResourceType::Sampler);
{
size_t texture_handle_clouds =
m_renderer->GetTextureManager()->FetchTexture(
NvRenderer::Config()->m_cloud_texture, GL_RGBA, 0, false);
size_t texture_handle_high_clouds =
m_renderer->GetTextureManager()->FetchTexture(
NvRenderer::Config()->m_high_cloud_texture, GL_R, 0, false);
nvrhi::CommandListHandle command_list =
m_backend->GetDevice()->createCommandList();
command_list->open();
m_clouds_texture = m_renderer->GetTextureManager()->GetRhiTexture(
texture_handle_clouds, command_list);
m_high_clouds_texture = m_renderer->GetTextureManager()->GetRhiTexture(
texture_handle_high_clouds, command_list);
command_list->close();
m_backend->GetDevice()->executeCommandList(command_list);
}
{ {
m_brdf_lut = m_backend->GetDevice()->createTexture( m_brdf_lut = m_backend->GetDevice()->createTexture(
TextureDesc() TextureDesc()
@@ -412,6 +440,9 @@ void MaEnvironment::Init(const std::string& texture, float pre_exposure) {
ResourceType::Texture_SRV); ResourceType::Texture_SRV);
} }
RegisterResource(true, "sky_clouds", m_clouds_texture,
nvrhi::ResourceType::Texture_SRV);
m_sky_texture = m_backend->GetDevice()->createTexture( m_sky_texture = m_backend->GetDevice()->createTexture(
TextureDesc(m_sky->m_aerial_lut->m_sky_texture->getDesc()) TextureDesc(m_sky->m_aerial_lut->m_sky_texture->getDesc())
.setDebugName("Envmap Sky Texture")); .setDebugName("Envmap Sky Texture"));
@@ -870,13 +901,17 @@ void EnvironmentRenderPass::Init() {
.setVisibility(nvrhi::ShaderType::Pixel) .setVisibility(nvrhi::ShaderType::Pixel)
.addItem(nvrhi::BindingLayoutItem::Texture_SRV(0)) .addItem(nvrhi::BindingLayoutItem::Texture_SRV(0))
.addItem(nvrhi::BindingLayoutItem::Texture_SRV(13)) .addItem(nvrhi::BindingLayoutItem::Texture_SRV(13))
.addItem(nvrhi::BindingLayoutItem::Texture_SRV(15))
.addItem(nvrhi::BindingLayoutItem::Texture_SRV(16))
.addItem(nvrhi::BindingLayoutItem::Sampler(0)) .addItem(nvrhi::BindingLayoutItem::Sampler(0))
.addItem(nvrhi::BindingLayoutItem::Sampler(13)) .addItem(nvrhi::BindingLayoutItem::Sampler(13))
.addItem(nvrhi::BindingLayoutItem::Sampler(15))
.addItem(nvrhi::BindingLayoutItem::VolatileConstantBuffer(0))); .addItem(nvrhi::BindingLayoutItem::VolatileConstantBuffer(0)));
auto sampler = m_backend->GetDevice()->createSampler( auto sampler = m_backend->GetDevice()->createSampler(
nvrhi::SamplerDesc() nvrhi::SamplerDesc()
.setAllAddressModes(nvrhi::SamplerAddressMode::Clamp) .setAllAddressModes(nvrhi::SamplerAddressMode::Clamp)
.setAllFilters(true)); .setAllFilters(true));
for (int i = 0; i < 2; ++i) { for (int i = 0; i < 2; ++i) {
m_binding_set[i] = m_backend->GetDevice()->createBindingSet( m_binding_set[i] = m_backend->GetDevice()->createBindingSet(
nvrhi::BindingSetDesc() nvrhi::BindingSetDesc()
@@ -884,10 +919,14 @@ void EnvironmentRenderPass::Init() {
nvrhi::BindingSetItem::Texture_SRV(0, m_environment->m_skybox)) nvrhi::BindingSetItem::Texture_SRV(0, m_environment->m_skybox))
.addItem(nvrhi::BindingSetItem::Texture_SRV( .addItem(nvrhi::BindingSetItem::Texture_SRV(
13, m_environment->m_sky->m_aerial_lut->m_sky_texture)) 13, m_environment->m_sky->m_aerial_lut->m_sky_texture))
.addItem(nvrhi::BindingSetItem::Texture_SRV(15, m_environment->m_clouds_texture))
.addItem(nvrhi::BindingSetItem::Texture_SRV(16, m_environment->m_high_clouds_texture))
.addItem(nvrhi::BindingSetItem::Sampler( .addItem(nvrhi::BindingSetItem::Sampler(
0, m_environment->m_sampler_linear_clamp)) 0, m_environment->m_sampler_linear_clamp))
.addItem(nvrhi::BindingSetItem::Sampler( .addItem(nvrhi::BindingSetItem::Sampler(
13, m_environment->m_sampler_linear_clamp_v_repeat_h)) 13, m_environment->m_sampler_linear_clamp_v_repeat_h))
.addItem(nvrhi::BindingSetItem::Sampler(
15, m_environment->m_sampler_linear_clamp))
.addItem(nvrhi::BindingSetItem::ConstantBuffer( .addItem(nvrhi::BindingSetItem::ConstantBuffer(
0, m_environment_constants)), 0, m_environment_constants)),
m_binding_layout); m_binding_layout);

View File

@@ -15,6 +15,8 @@ struct MaEnvironment : public MaResourceRegistry {
nvrhi::TextureHandle m_brdf_lut; nvrhi::TextureHandle m_brdf_lut;
nvrhi::TextureHandle m_sky_texture; nvrhi::TextureHandle m_sky_texture;
nvrhi::TextureHandle m_aerial_lut; nvrhi::TextureHandle m_aerial_lut;
nvrhi::TextureHandle m_clouds_texture;
nvrhi::TextureHandle m_high_clouds_texture;
nvrhi::SamplerHandle m_sampler_linear_clamp_v_repeat_h; nvrhi::SamplerHandle m_sampler_linear_clamp_v_repeat_h;
nvrhi::SamplerHandle m_sampler_linear_clamp; nvrhi::SamplerHandle m_sampler_linear_clamp;
@@ -45,6 +47,7 @@ struct MaEnvironment : public MaResourceRegistry {
nvrhi::BufferHandle m_face_inverse_projection_buffer; nvrhi::BufferHandle m_face_inverse_projection_buffer;
class NvRenderer* m_renderer;
class NvRendererBackend* m_backend; class NvRendererBackend* m_backend;
struct NvGbuffer* m_gbuffer; struct NvGbuffer* m_gbuffer;
struct Sky* m_sky; struct Sky* m_sky;
@@ -107,6 +110,8 @@ struct MaEnvironment : public MaResourceRegistry {
struct EnvironmentRenderPass : public FullScreenPass { struct EnvironmentRenderPass : public FullScreenPass {
EnvironmentRenderPass(MaEnvironment* environment); EnvironmentRenderPass(MaEnvironment* environment);
size_t m_texture_handle_clouds;
struct EnvironmentConstants { struct EnvironmentConstants {
glm::mat4 m_inverse_view_projection; glm::mat4 m_inverse_view_projection;
glm::mat4 m_reproject_matrix; glm::mat4 m_reproject_matrix;

View File

@@ -110,6 +110,7 @@ void GbufferBlitPass::Init() {
.addItem(nvrhi::BindingLayoutItem::Texture_SRV(11)) .addItem(nvrhi::BindingLayoutItem::Texture_SRV(11))
.addItem(nvrhi::BindingLayoutItem::Texture_SRV(12)) .addItem(nvrhi::BindingLayoutItem::Texture_SRV(12))
.addItem(nvrhi::BindingLayoutItem::Texture_SRV(14)) .addItem(nvrhi::BindingLayoutItem::Texture_SRV(14))
.addItem(nvrhi::BindingLayoutItem::Texture_SRV(15))
.addItem(nvrhi::BindingLayoutItem::Texture_SRV(16)) .addItem(nvrhi::BindingLayoutItem::Texture_SRV(16))
.addItem(nvrhi::BindingLayoutItem::StructuredBuffer_SRV(17)) .addItem(nvrhi::BindingLayoutItem::StructuredBuffer_SRV(17))
.addItem(nvrhi::BindingLayoutItem::StructuredBuffer_SRV(18)) .addItem(nvrhi::BindingLayoutItem::StructuredBuffer_SRV(18))
@@ -147,6 +148,8 @@ void GbufferBlitPass::Init() {
12, m_contact_shadows->m_output_texture)) 12, m_contact_shadows->m_output_texture))
.addItem(nvrhi::BindingSetItem::Texture_SRV( .addItem(nvrhi::BindingSetItem::Texture_SRV(
14, m_sky->m_aerial_lut->m_lut)) 14, m_sky->m_aerial_lut->m_lut))
.addItem(nvrhi::BindingSetItem::Texture_SRV(
15, m_environment->m_clouds_texture))
.addItem(nvrhi::BindingSetItem::Texture_SRV( .addItem(nvrhi::BindingSetItem::Texture_SRV(
16, static_cast<nvrhi::ITexture*>( 16, static_cast<nvrhi::ITexture*>(
GetResource("forwardplus_index_grid_opaque", nvrhi::ResourceType::Texture_SRV) GetResource("forwardplus_index_grid_opaque", nvrhi::ResourceType::Texture_SRV)

View File

@@ -244,6 +244,7 @@ void NvRenderer::MaterialTemplate::Init(const YAML::Node &conf) {
.Add(MaResourceMapping::Texture_SRV(12, "gbuffer_depth")) .Add(MaResourceMapping::Texture_SRV(12, "gbuffer_depth"))
.Add(MaResourceMapping::Texture_SRV(13, "scene_lit_texture_copy")) .Add(MaResourceMapping::Texture_SRV(13, "scene_lit_texture_copy"))
.Add(MaResourceMapping::Texture_SRV(14, "sky_aerial_lut")) .Add(MaResourceMapping::Texture_SRV(14, "sky_aerial_lut"))
.Add(MaResourceMapping::Texture_SRV(15, "sky_clouds"))
.Add(MaResourceMapping::Texture_SRV( .Add(MaResourceMapping::Texture_SRV(
16, "forwardplus_index_grid_transparent")) 16, "forwardplus_index_grid_transparent"))
.Add(MaResourceMapping::StructuredBuffer_SRV( .Add(MaResourceMapping::StructuredBuffer_SRV(
@@ -423,6 +424,7 @@ bool NvRenderer::InitMaterials() {
.addItem(nvrhi::BindingLayoutItem::Texture_SRV(11)) .addItem(nvrhi::BindingLayoutItem::Texture_SRV(11))
.addItem(nvrhi::BindingLayoutItem::Texture_SRV(12)) .addItem(nvrhi::BindingLayoutItem::Texture_SRV(12))
.addItem(nvrhi::BindingLayoutItem::Texture_SRV(14)) .addItem(nvrhi::BindingLayoutItem::Texture_SRV(14))
.addItem(nvrhi::BindingLayoutItem::Texture_SRV(15))
.addItem(nvrhi::BindingLayoutItem::Sampler(8)) .addItem(nvrhi::BindingLayoutItem::Sampler(8))
.addItem(nvrhi::BindingLayoutItem::Sampler(11)) .addItem(nvrhi::BindingLayoutItem::Sampler(11))
.addItem(nvrhi::BindingLayoutItem::Sampler(13))); .addItem(nvrhi::BindingLayoutItem::Sampler(13)));
@@ -445,6 +447,8 @@ bool NvRenderer::InitMaterials() {
12, m_gbuffer->m_gbuffer_depth)) 12, m_gbuffer->m_gbuffer_depth))
.addItem(nvrhi::BindingSetItem::Texture_SRV( .addItem(nvrhi::BindingSetItem::Texture_SRV(
14, m_sky->m_aerial_lut->m_lut)) 14, m_sky->m_aerial_lut->m_lut))
.addItem(nvrhi::BindingSetItem::Texture_SRV(
15, m_environment->m_clouds_texture))
.addItem(nvrhi::BindingSetItem::Sampler( .addItem(nvrhi::BindingSetItem::Sampler(
8, m_backend->GetDevice()->createSampler( 8, m_backend->GetDevice()->createSampler(
nvrhi::SamplerDesc().setAllFilters(true)))) nvrhi::SamplerDesc().setAllFilters(true))))

View File

@@ -82,8 +82,8 @@ bool NvRenderer::Init(GLFWwindow *Window) {
m_gbuffer_shadow = std::make_shared<NvGbuffer>(this); m_gbuffer_shadow = std::make_shared<NvGbuffer>(this);
m_contact_shadows = std::make_shared<MaContactShadows>(this, m_gbuffer.get()); m_contact_shadows = std::make_shared<MaContactShadows>(this, m_gbuffer.get());
m_shadow_map = std::make_shared<MaShadowMap>(this); m_shadow_map = std::make_shared<MaShadowMap>(this);
m_sky = std::make_shared<Sky>(this);
m_environment = std::make_shared<MaEnvironment>(this); m_environment = std::make_shared<MaEnvironment>(this);
m_sky = std::make_shared<Sky>(this, m_environment.get());
m_ssao = std::make_shared<NvSsao>(this); m_ssao = std::make_shared<NvSsao>(this);
m_gbuffer_lighting = std::make_shared<GbufferLighting>(this); m_gbuffer_lighting = std::make_shared<GbufferLighting>(this);
m_gbuffer_blit = std::make_shared<GbufferBlitPass>( m_gbuffer_blit = std::make_shared<GbufferBlitPass>(

View File

@@ -2,6 +2,7 @@
#include <nvrhi/utils.h> #include <nvrhi/utils.h>
#include "environment.h"
#include "nvrendererbackend.h" #include "nvrendererbackend.h"
#include "simulationenvironment.h" #include "simulationenvironment.h"
#include "simulationtime.h" #include "simulationtime.h"
@@ -46,8 +47,8 @@ static const glm::mat4x3 M = {
8.572844237945445, -11.103384660054624, 117.47585277566478}; 8.572844237945445, -11.103384660054624, 117.47585277566478};
} // namespace } // namespace
Sky::Sky(NvRenderer* renderer) Sky::Sky(NvRenderer* renderer, MaEnvironment *environment)
: m_backend(renderer->GetBackend()), MaResourceRegistry(renderer) { : m_backend(renderer->GetBackend()), MaResourceRegistry(renderer), m_environment(environment) {
m_transmittance_pass = std::make_shared<SkyTransmittancePass>(this); m_transmittance_pass = std::make_shared<SkyTransmittancePass>(this);
m_aerial_lut = std::make_shared<SkyAerialLut>(this); m_aerial_lut = std::make_shared<SkyAerialLut>(this);
} }
@@ -375,6 +376,7 @@ void SkyAerialLut::Init() {
m_lut_width = 128; m_lut_width = 128;
m_lut_height = 256; m_lut_height = 256;
m_lut_slices = 16; m_lut_slices = 16;
m_cloud_texture = m_sky->m_environment->m_clouds_texture;
m_constant_buffer = m_sky->m_backend->GetDevice()->createBuffer( m_constant_buffer = m_sky->m_backend->GetDevice()->createBuffer(
nvrhi::utils::CreateVolatileConstantBufferDesc( nvrhi::utils::CreateVolatileConstantBufferDesc(
sizeof(DispatchConstants), "Sky Aerial LUT Dispatch Constants", 16)); sizeof(DispatchConstants), "Sky Aerial LUT Dispatch Constants", 16));
@@ -398,51 +400,56 @@ void SkyAerialLut::Init() {
.setInitialState(nvrhi::ResourceStates::Common) .setInitialState(nvrhi::ResourceStates::Common)
.setKeepInitialState(true) .setKeepInitialState(true)
.setDebugName("Sky Texture")); .setDebugName("Sky Texture"));
auto shader_lut = m_sky->m_backend->CreateShader("sky_aerial_lut",
nvrhi::ShaderType::Compute);
auto shader_sky =
m_sky->m_backend->CreateShader("sky", nvrhi::ShaderType::Compute);
auto sampler = m_sky->m_backend->GetDevice()->createSampler(
nvrhi::SamplerDesc()
.setAllAddressModes(nvrhi::SamplerAddressMode::ClampToEdge)
.setAllFilters(true));
nvrhi::BindingLayoutHandle binding_layout_lut;
nvrhi::BindingLayoutHandle binding_layout_sky;
nvrhi::utils::CreateBindingSetAndLayout(
m_sky->m_backend->GetDevice(), nvrhi::ShaderType::Compute, 0,
nvrhi::BindingSetDesc()
.addItem(nvrhi::BindingSetItem::ConstantBuffer(0, m_constant_buffer))
.addItem(
nvrhi::BindingSetItem::ConstantBuffer(13, m_sky->m_sky_constants))
.addItem(nvrhi::BindingSetItem::Texture_UAV(0, m_lut))
.addItem(nvrhi::BindingSetItem::Texture_SRV(
13, m_sky->m_transmittance_pass->m_output))
.addItem(nvrhi::BindingSetItem::Sampler(13, sampler)),
binding_layout_lut, m_bindings_lut);
nvrhi::utils::CreateBindingSetAndLayout(
m_sky->m_backend->GetDevice(), nvrhi::ShaderType::Compute, 0,
nvrhi::BindingSetDesc()
.addItem(nvrhi::BindingSetItem::ConstantBuffer(0, m_constant_buffer))
.addItem(
nvrhi::BindingSetItem::ConstantBuffer(13, m_sky->m_sky_constants))
.addItem(nvrhi::BindingSetItem::Texture_UAV(1, m_sky_texture))
.addItem(nvrhi::BindingSetItem::Texture_SRV(
13, m_sky->m_transmittance_pass->m_output))
.addItem(nvrhi::BindingSetItem::Sampler(13, sampler)),
binding_layout_sky, m_bindings_sky);
m_pso_lut = m_sky->m_backend->GetDevice()->createComputePipeline(
nvrhi::ComputePipelineDesc()
.addBindingLayout(binding_layout_lut)
.setComputeShader(shader_lut));
m_pso_sky = m_sky->m_backend->GetDevice()->createComputePipeline(
nvrhi::ComputePipelineDesc()
.addBindingLayout(binding_layout_sky)
.setComputeShader(shader_sky));
} }
void SkyAerialLut::Render(nvrhi::ICommandList* command_list, void SkyAerialLut::Render(nvrhi::ICommandList* command_list,
const glm::dmat4& projection, const glm::dmat4& projection,
const glm::dmat4& view) { const glm::dmat4& view) {
if (!m_pso_sky) {
auto shader_lut = m_sky->m_backend->CreateShader(
"sky_aerial_lut", nvrhi::ShaderType::Compute);
auto shader_sky =
m_sky->m_backend->CreateShader("sky", nvrhi::ShaderType::Compute);
auto sampler = m_sky->m_backend->GetDevice()->createSampler(
nvrhi::SamplerDesc()
.setAllAddressModes(nvrhi::SamplerAddressMode::ClampToEdge)
.setAllFilters(true));
nvrhi::BindingLayoutHandle binding_layout_lut;
nvrhi::BindingLayoutHandle binding_layout_sky;
nvrhi::utils::CreateBindingSetAndLayout(
m_sky->m_backend->GetDevice(), nvrhi::ShaderType::Compute, 0,
nvrhi::BindingSetDesc()
.addItem(
nvrhi::BindingSetItem::ConstantBuffer(0, m_constant_buffer))
.addItem(nvrhi::BindingSetItem::ConstantBuffer(
13, m_sky->m_sky_constants))
.addItem(nvrhi::BindingSetItem::Texture_UAV(0, m_lut))
.addItem(nvrhi::BindingSetItem::Texture_SRV(
13, m_sky->m_transmittance_pass->m_output))
.addItem(nvrhi::BindingSetItem::Texture_SRV(15, m_sky->m_environment->m_clouds_texture))
.addItem(nvrhi::BindingSetItem::Sampler(13, sampler)),
binding_layout_lut, m_bindings_lut);
nvrhi::utils::CreateBindingSetAndLayout(
m_sky->m_backend->GetDevice(), nvrhi::ShaderType::Compute, 0,
nvrhi::BindingSetDesc()
.addItem(
nvrhi::BindingSetItem::ConstantBuffer(0, m_constant_buffer))
.addItem(nvrhi::BindingSetItem::ConstantBuffer(
13, m_sky->m_sky_constants))
.addItem(nvrhi::BindingSetItem::Texture_UAV(1, m_sky_texture))
.addItem(nvrhi::BindingSetItem::Texture_SRV(
13, m_sky->m_transmittance_pass->m_output))
.addItem(nvrhi::BindingSetItem::Sampler(13, sampler)),
binding_layout_sky, m_bindings_sky);
m_pso_lut = m_sky->m_backend->GetDevice()->createComputePipeline(
nvrhi::ComputePipelineDesc()
.addBindingLayout(binding_layout_lut)
.setComputeShader(shader_lut));
m_pso_sky = m_sky->m_backend->GetDevice()->createComputePipeline(
nvrhi::ComputePipelineDesc()
.addBindingLayout(binding_layout_sky)
.setComputeShader(shader_sky));
}
{ {
DispatchConstants constants{}; DispatchConstants constants{};
constants.g_InverseView = static_cast<glm::mat3>(glm::inverse(view)); constants.g_InverseView = static_cast<glm::mat3>(glm::inverse(view));

View File

@@ -4,7 +4,7 @@
#include "nvrenderer/resource_registry.h" #include "nvrenderer/resource_registry.h"
struct Sky : public MaResourceRegistry { struct Sky : public MaResourceRegistry {
Sky(class NvRenderer* renderer); Sky(class NvRenderer* renderer, struct MaEnvironment *environment);
void Init(); void Init();
void Render(nvrhi::ICommandList* command_list, const glm::dmat4& projection, void Render(nvrhi::ICommandList* command_list, const glm::dmat4& projection,
@@ -27,7 +27,7 @@ struct Sky : public MaResourceRegistry {
Rural, Rural,
Urban, Urban,
Num Num
} m_aerosol_preset = AerosolPreset::Rural; } m_aerosol_preset = AerosolPreset::Urban;
float m_visibility = 1e4f; float m_visibility = 1e4f;
float m_fog_height_offset = 0.f; float m_fog_height_offset = 0.f;
float m_fog_height_scale = .1f; float m_fog_height_scale = .1f;
@@ -39,6 +39,7 @@ struct Sky : public MaResourceRegistry {
void ShowGui(); void ShowGui();
static const char* GetAerosolTypeDesc(AerosolPreset preset); static const char* GetAerosolTypeDesc(AerosolPreset preset);
class NvRendererBackend* m_backend; class NvRendererBackend* m_backend;
MaEnvironment* m_environment;
std::shared_ptr<struct SkyTransmittancePass> m_transmittance_pass; std::shared_ptr<struct SkyTransmittancePass> m_transmittance_pass;
std::shared_ptr<struct SkyAerialLut> m_aerial_lut; std::shared_ptr<struct SkyAerialLut> m_aerial_lut;
float GetOzoneMean() const; float GetOzoneMean() const;
@@ -104,6 +105,7 @@ struct SkyAerialLut {
const glm::dmat4& view); const glm::dmat4& view);
nvrhi::TextureHandle m_lut; nvrhi::TextureHandle m_lut;
nvrhi::TextureHandle m_sky_texture; nvrhi::TextureHandle m_sky_texture;
nvrhi::TextureHandle m_cloud_texture;
nvrhi::BufferHandle m_constant_buffer; nvrhi::BufferHandle m_constant_buffer;
nvrhi::ComputePipelineHandle m_pso_lut; nvrhi::ComputePipelineHandle m_pso_lut;
nvrhi::ComputePipelineHandle m_pso_sky; nvrhi::ComputePipelineHandle m_pso_sky;

View File

@@ -18,6 +18,7 @@ SamplerState g_SamplerPointClamp : register(s1);
#include "cubemap_utils.hlsli" #include "cubemap_utils.hlsli"
#include "manul/sky.hlsli" #include "manul/sky.hlsli"
#include "manul/clouds.hlsli"
struct FilterParameters { struct FilterParameters {
uint3 m_Offset; uint3 m_Offset;
@@ -50,6 +51,7 @@ void main(uint3 PixCoord : SV_DispatchThreadID) {
//g_OutCubemap[PixCoord + g_Offset] = g_Skybox.SampleLevel(g_SamplerLinearClamp, normal, 0.); //g_OutCubemap[PixCoord + g_Offset] = g_Skybox.SampleLevel(g_SamplerLinearClamp, normal, 0.);
float3 color = 1.e-7; float3 color = 1.e-7;
CalcAtmosphere(color, normal, g_FilterParams.m_LightVector); CalcAtmosphere(color, normal, g_FilterParams.m_LightVector);
CalcClouds(color, normal, g_FilterParams.m_LightVector);
//CalcAtmosphere(g_OutCubemap[PixCoord + g_Offset], 1., normal, g_LightVector, g_Altitude, SKY_INF, g_LightColor.rgb, 10); //CalcAtmosphere(g_OutCubemap[PixCoord + g_Offset], 1., normal, g_LightVector, g_Altitude, SKY_INF, g_LightColor.rgb, 10);
float3 normal_flipped = normal * float3(-1., 1., 1.); float3 normal_flipped = normal * float3(-1., 1., 1.);
float depth = g_Depth.SampleLevel(g_SamplerPointClamp, normal_flipped, 0.); float depth = g_Depth.SampleLevel(g_SamplerPointClamp, normal_flipped, 0.);

View File

@@ -0,0 +1,69 @@
#ifndef CLOUDS_HLSLI
#define CLOUDS_HLSLI
#include "sky.hlsli"
Texture2D<float4> g_Clouds : register(t15);
Texture2D<float> g_HighClouds : register(t16);
SamplerState g_CloudsSampler : register(s15);
float3 desaturate(float3 col, float amount) {
return lerp(col, dot(col, float3(.2126, .7152, .0722)), amount);
}
// https://iquilezles.org/articles/smin/
// sigmoid
float smin( float a, float b, float k )
{
k *= log(2.0);
float x = b-a;
return a + x/(1.0-exp2(x/k));
}
float ComputeTopDown(float value) {
value = -0.9501426 * value * value + 2.09511187 * value + -0.16186117;
return -smin(-value, 0., .045);
}
void CalcClouds(inout float3 color, in float3 viewDir, in float3 sunDir) {
float3 emissive_top = 1.e-7;
float3 emissive_sun = linear_srgb_from_spectral_samples(sun_spectral_irradiance) * exp(-4.);
float3 emissive_view = 1.e-7;
CalcAtmosphere(emissive_top, float3(0., 1., 0.), sunDir);
CalcAtmosphere(emissive_sun, sunDir, sunDir);
CalcAtmosphere(emissive_view, viewDir, sunDir);
float3 cloud_dir = viewDir;
cloud_dir.y = 4. * abs(cloud_dir.y);
cloud_dir = normalize(cloud_dir);
float4 cloud_mask = g_Clouds.SampleLevel(g_CloudsSampler, cloud_dir.xz * .5 + .5, 0.);
float high_cloud_mask = g_HighClouds.SampleLevel(g_CloudsSampler, cloud_dir.xz * .5 + .5, 0.) * .5;
float selector = atan2(sunDir.z, sunDir.x) / TWO_PI;
selector -= floor(selector);
selector *= 3.;
int idx = floor(selector);
float cloud_lit = lerp(cloud_mask[idx], cloud_mask[(idx + 1) % 3], frac(selector));
float topdown = ComputeTopDown(saturate(viewDir.y));
float3 ndotl = saturate(dot(viewDir, sunDir) * .5 + .5);
float shine = pow(ndotl, 17.);
float3 shadow_color = desaturate(lerp(emissive_view, emissive_top, .5), .5);// * lerp(1., .1, shine);
float3 lit_color = lerp(emissive_view, emissive_sun, .5) * lerp(1., 4., shine);
cloud_lit = pow(cloud_lit, lerp(lerp(1.6, 1.2, topdown), 3., shine));
float3 cloud_color = lerp( shadow_color, lit_color, cloud_lit);
cloud_color = lerp(cloud_color, emissive_view, smoothstep(.05, 0., topdown));
float3 high_cloud_color = lit_color;
high_cloud_color = lerp(high_cloud_color, emissive_view, smoothstep(.05, 0., topdown));
color = lerp(color, high_cloud_color, high_cloud_mask * smoothstep(-.025, .025, viewDir.y));
color = lerp(color, cloud_color, cloud_mask.a * smoothstep(-.025, .025, viewDir.y));
}
#endif

View File

@@ -5,6 +5,8 @@
#include "material_common.hlsli" #include "material_common.hlsli"
#include "sky_common.hlsli" // ray_sphere_intersection
#include "view_data.hlsli" #include "view_data.hlsli"
#include "lighting_functions.hlsli" #include "lighting_functions.hlsli"
@@ -16,6 +18,7 @@ TextureCube<float3> g_DiffuseEnvmap : register(t8);
TextureCube<float3> g_SpecularEnvmap : register(t9); TextureCube<float3> g_SpecularEnvmap : register(t9);
Texture2D<float2> g_BrdfLUT : register(t10); Texture2D<float2> g_BrdfLUT : register(t10);
Texture2D<float4> g_CloudShadowMap : register(t15);
Texture2D<uint2> g_LightGrid : register(t16); Texture2D<uint2> g_LightGrid : register(t16);
StructuredBuffer<uint> g_LightIndexBuffer : register(t17); StructuredBuffer<uint> g_LightIndexBuffer : register(t17);
StructuredBuffer<PackedLight> g_LightBuffer : register(t18); StructuredBuffer<PackedLight> g_LightBuffer : register(t18);
@@ -83,6 +86,15 @@ void ApplyMaterialLighting(out float4 lit, in MaterialData material)
#ifdef GBUFFER_CONTACT_SHADOWS_HLSLI #ifdef GBUFFER_CONTACT_SHADOWS_HLSLI
shadow = min(shadow, GetContactShadows(pixel_position)); shadow = min(shadow, GetContactShadows(pixel_position));
#endif #endif
float t = ray_sphere_intersection(view, g_LightDir.xyz, 10000.);
if(t >= 0.) {
float3 cloud_dir = normalize(view + g_LightDir.xyz * t);
cloud_dir.y = 4. * abs(cloud_dir.y);
cloud_dir = normalize(cloud_dir);
float4 cloud_mask = g_CloudShadowMap.SampleLevel(g_SamplerLinearClamp, cloud_dir.xz * .5 + .5, 0.);
shadow = min(shadow, 1. - cloud_mask.a * .6);
}
// Apply IBL cubemap // Apply IBL cubemap
ApplyIBL(lit.rgb, surface_data); ApplyIBL(lit.rgb, surface_data);

View File

@@ -22,7 +22,7 @@ void CalcSun(inout float3 color, in float3 viewDir, in float3 sunDir, in float a
{ {
if (dot(viewDir, sunDir) > 0.99998869014) if (dot(viewDir, sunDir) > 0.99998869014)
{ {
color = linear_srgb_from_spectral_samples(sun_spectral_irradiance); color = linear_srgb_from_spectral_samples(sun_spectral_irradiance) * exp(-2.);
} }
} }
} }
@@ -37,7 +37,7 @@ void CalcMoon(inout float3 color, in float3 viewDir, in float3 moonDir, in float
float3 normal = CalcSphereNormal(viewDir, moonDir, 0.99998869014); float3 normal = CalcSphereNormal(viewDir, moonDir, 0.99998869014);
if (dot(normal, normal) > 0.) if (dot(normal, normal) > 0.)
{ {
color = .07 * max(dot(normal, sunDir), 0.) * linear_srgb_from_spectral_samples(sun_spectral_irradiance); color = .07 * max(dot(normal, sunDir), 0.) * linear_srgb_from_spectral_samples(sun_spectral_irradiance) * exp(-2.);
} }
} }
} }

View File

@@ -14,6 +14,7 @@ cbuffer DispatchConstants : register(b0)
RWTexture2DArray<float4> g_AerialLut : register(u0); RWTexture2DArray<float4> g_AerialLut : register(u0);
RWTexture2D<float4> g_Sky : register(u1); RWTexture2D<float4> g_Sky : register(u1);
Texture2D<float4> g_Clouds : register(t15);
Texture2D<float4> g_TransmittanceLut : register(t13); Texture2D<float4> g_TransmittanceLut : register(t13);
SamplerState g_TransmittanceLutSampler : register(s13); SamplerState g_TransmittanceLutSampler : register(s13);
@@ -77,8 +78,8 @@ SamplerState g_TransmittanceLutSampler : register(s13);
{ {
float t = (i + 1.) / (float)texture_size.z; float t = (i + 1.) / (float)texture_size.z;
float end_depth = min(t_d, t * t * g_MaxDepth); float end_depth = min(t_d, t * t * g_MaxDepth);
compute_inscattering(g_TransmittanceLut, g_TransmittanceLutSampler, molecular_phase, aerosol_phase, 5, ray_origin, ray_dir, start_depth, end_depth, g_SunDir, L, transmittance); compute_inscattering_with_cloud_shadow(g_TransmittanceLut, g_Clouds, g_TransmittanceLutSampler, molecular_phase, aerosol_phase, 5, ray_origin, ray_dir, start_depth, end_depth, g_SunDir, L, transmittance);
compute_inscattering(g_TransmittanceLut, g_TransmittanceLutSampler, molecular_phase_moon, aerosol_phase_moon, 5, ray_origin, ray_dir, start_depth, end_depth, g_MoonDir, M, transmittance_m); compute_inscattering_with_cloud_shadow(g_TransmittanceLut, g_Clouds, g_TransmittanceLutSampler, molecular_phase_moon, aerosol_phase_moon, 5, ray_origin, ray_dir, start_depth, end_depth, g_MoonDir, M, transmittance_m);
g_AerialLut[uint3(pix_coord, i)] = float4(linear_srgb_from_spectral_samples(L + .07 * moon_phase * M) * exp2(EXPOSURE), dot(transmittance, .25)); g_AerialLut[uint3(pix_coord, i)] = float4(linear_srgb_from_spectral_samples(L + .07 * moon_phase * M) * exp2(EXPOSURE), dot(transmittance, .25));
start_depth = end_depth; start_depth = end_depth;
} }

View File

@@ -66,6 +66,64 @@ void compute_inscattering(Texture2D transmittance_lut, SamplerState lut_sampler,
} }
} }
void compute_inscattering_with_cloud_shadow(Texture2D transmittance_lut, Texture2D clouds, SamplerState lut_sampler, in float molecular_phase, in float aerosol_phase, int steps, float3 ray_origin, float3 ray_dir, float t_min, float t_max, float3 sun_dir, inout float4 L_inscattering, inout float4 transmittance)
{
float dt = (t_max - t_min) / float(steps);
for (int i = 0; i < steps; ++i) {
float t = t_min + (float(i) + 0.5) * dt;
float3 x_t = ray_origin + ray_dir * t;
float distance_to_earth_center = length(x_t);
float3 zenith_dir = x_t / distance_to_earth_center;
float altitude = distance_to_earth_center - EARTH_RADIUS;
float normalized_altitude = altitude / ATMOSPHERE_THICKNESS;
float sample_cos_theta = dot(zenith_dir, sun_dir);
float shadow = 1.;
float c_t = ray_sphere_intersection(ray_dir * t, sun_dir, 10000.);
if(c_t >= 0.) {
float3 cloud_dir = normalize(ray_dir * t + sun_dir * c_t);
cloud_dir.y = 4. * cloud_dir.y;
cloud_dir = normalize(cloud_dir);
float4 cloud_mask = clouds.SampleLevel(lut_sampler, cloud_dir.xz * .5 + .5, 0.);
shadow = min(shadow, 1. - cloud_mask.a * smoothstep(-.01, .01, cloud_dir.y));
}
float4 aerosol_absorption, aerosol_scattering;
float4 molecular_absorption, molecular_scattering;
float4 fog_scattering;
float4 extinction;
get_atmosphere_collision_coefficients(
altitude,
aerosol_absorption, aerosol_scattering,
molecular_absorption, molecular_scattering,
fog_scattering,
extinction);
float4 transmittance_to_sun = transmittance_from_lut(
transmittance_lut, lut_sampler, sample_cos_theta, normalized_altitude) * shadow;
float4 ms = get_multiple_scattering(
transmittance_lut, lut_sampler, sample_cos_theta, normalized_altitude,
distance_to_earth_center);
float4 S = sun_spectral_irradiance *
(molecular_scattering * (molecular_phase * transmittance_to_sun + ms) +
(aerosol_scattering + fog_scattering) * (aerosol_phase * transmittance_to_sun + ms));
float4 step_transmittance = exp(-dt * extinction);
// Energy-conserving analytical integration
// "Physically Based Sky, Atmosphere and Cloud Rendering in Frostbite"
// by Sébastien Hillaire
float4 S_int = (S - S * step_transmittance) / max(extinction, 1e-7);
L_inscattering += transmittance * S_int;
transmittance *= step_transmittance;
}
}
float4 get_inscattering(Texture2D transmittance_lut, SamplerState lut_sampler, int steps, float altitude, float3 ray_dir, float t_min, float t_max, float3 sun_dir) { float4 get_inscattering(Texture2D transmittance_lut, SamplerState lut_sampler, int steps, float altitude, float3 ray_dir, float t_min, float t_max, float3 sun_dir) {
float cos_theta = dot(-ray_dir, sun_dir); float cos_theta = dot(-ray_dir, sun_dir);
float molecular_phase = molecular_phase_function(cos_theta); float molecular_phase = molecular_phase_function(cos_theta);

View File

@@ -24,6 +24,7 @@ sampler g_SkyboxSampler : register(s0);
TextureCube g_Skybox : register(t0); TextureCube g_Skybox : register(t0);
#include "manul/sky.hlsli" #include "manul/sky.hlsli"
#include "manul/clouds.hlsli"
PixelOutput main(VertexOutput ps_in) { PixelOutput main(VertexOutput ps_in) {
PixelOutput result; PixelOutput result;
@@ -38,6 +39,7 @@ PixelOutput main(VertexOutput ps_in) {
CalcSun(result.m_Emission, viewDir, g_SunDirection, g_Altitude); CalcSun(result.m_Emission, viewDir, g_SunDirection, g_Altitude);
CalcMoon(result.m_Emission, viewDir, g_MoonDirection, g_SunDirection, g_Altitude); CalcMoon(result.m_Emission, viewDir, g_MoonDirection, g_SunDirection, g_Altitude);
CalcAtmosphere(result.m_Emission, viewDir, g_SunDirection); CalcAtmosphere(result.m_Emission, viewDir, g_SunDirection);
CalcClouds(result.m_Emission, viewDir, g_SunDirection);
//result.m_Emission = g_Skybox.Sample(g_SkyboxSampler, normalize(mul(g_InverseViewProjection, positionNdc).xyz)).rgb; //result.m_Emission = g_Skybox.Sample(g_SkyboxSampler, normalize(mul(g_InverseViewProjection, positionNdc).xyz)).rgb;
//result.m_Emission = 0.; //Sky(normalize(mul(g_InverseViewProjection, positionNdc).xyz), g_SunDirection, g_Altitude); //result.m_Emission = 0.; //Sky(normalize(mul(g_InverseViewProjection, positionNdc).xyz), g_SunDirection, g_Altitude);
float4 positionReproject = mul(g_HistoryReproject, positionNdc); float4 positionReproject = mul(g_HistoryReproject, positionNdc);