16
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-07-21 17:09:19 +02:00

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_tractions = node["wire_diameter_tractions"].as<float>(.005f);
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) {

View File

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

View File

@@ -7,6 +7,7 @@
#include "gbuffer.h"
#include "nvrenderer/nvrenderer.h"
#include "nvrendererbackend.h"
#include "nvtexture.h"
#include "simulationenvironment.h"
#include "sky.h"
@@ -50,6 +51,8 @@ nvrhi::BindingSetHandle MaEnvironment::GetBindingSet(int pass, int mip, int set,
Format::UNKNOWN, AllSubresources))
.addItem(BindingSetItem::Texture_SRV(13, m_sky_texture))
.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(
0, m_dynamic_skybox[set], Format::UNKNOWN,
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(1, m_sampler_point_clamp))
.addItem(BindingSetItem::Sampler(13, m_sampler_linear_clamp))
.addItem(BindingSetItem::Sampler(15, m_sampler_linear_clamp))
.addItem(BindingSetItem::ConstantBuffer(
13, m_sky->m_sky_constants))
.addItem(BindingSetItem::ConstantBuffer(
@@ -125,11 +129,13 @@ nvrhi::BindingSetHandle MaEnvironment::GetBindingSet(int pass, int mip, int set,
MaEnvironment::MaEnvironment(NvRenderer* renderer)
: MaResourceRegistry(renderer),
m_renderer(renderer),
m_backend(renderer->m_backend.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) {
m_sky = m_renderer->m_sky.get();
InitResourceRegistry();
using namespace nvrhi;
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(13))
.addItem(BindingLayoutItem::Texture_SRV(14))
.addItem(BindingLayoutItem::Texture_SRV(15))
.addItem(BindingLayoutItem::Texture_SRV(16))
.addItem(BindingLayoutItem::Texture_UAV(0))
.addItem(BindingLayoutItem::Sampler(0))
.addItem(BindingLayoutItem::Sampler(1))
.addItem(BindingLayoutItem::Sampler(13))
.addItem(BindingLayoutItem::Sampler(15))
.addItem(BindingLayoutItem::VolatileConstantBuffer(13))
.addItem(BindingLayoutItem::ConstantBuffer(1))
.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",
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(
TextureDesc()
@@ -412,6 +440,9 @@ void MaEnvironment::Init(const std::string& texture, float pre_exposure) {
ResourceType::Texture_SRV);
}
RegisterResource(true, "sky_clouds", m_clouds_texture,
nvrhi::ResourceType::Texture_SRV);
m_sky_texture = m_backend->GetDevice()->createTexture(
TextureDesc(m_sky->m_aerial_lut->m_sky_texture->getDesc())
.setDebugName("Envmap Sky Texture"));
@@ -870,13 +901,17 @@ void EnvironmentRenderPass::Init() {
.setVisibility(nvrhi::ShaderType::Pixel)
.addItem(nvrhi::BindingLayoutItem::Texture_SRV(0))
.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(13))
.addItem(nvrhi::BindingLayoutItem::Sampler(15))
.addItem(nvrhi::BindingLayoutItem::VolatileConstantBuffer(0)));
auto sampler = m_backend->GetDevice()->createSampler(
nvrhi::SamplerDesc()
.setAllAddressModes(nvrhi::SamplerAddressMode::Clamp)
.setAllFilters(true));
for (int i = 0; i < 2; ++i) {
m_binding_set[i] = m_backend->GetDevice()->createBindingSet(
nvrhi::BindingSetDesc()
@@ -884,10 +919,14 @@ void EnvironmentRenderPass::Init() {
nvrhi::BindingSetItem::Texture_SRV(0, m_environment->m_skybox))
.addItem(nvrhi::BindingSetItem::Texture_SRV(
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(
0, m_environment->m_sampler_linear_clamp))
.addItem(nvrhi::BindingSetItem::Sampler(
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(
0, m_environment_constants)),
m_binding_layout);

View File

@@ -15,6 +15,8 @@ struct MaEnvironment : public MaResourceRegistry {
nvrhi::TextureHandle m_brdf_lut;
nvrhi::TextureHandle m_sky_texture;
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;
@@ -45,6 +47,7 @@ struct MaEnvironment : public MaResourceRegistry {
nvrhi::BufferHandle m_face_inverse_projection_buffer;
class NvRenderer* m_renderer;
class NvRendererBackend* m_backend;
struct NvGbuffer* m_gbuffer;
struct Sky* m_sky;
@@ -107,6 +110,8 @@ struct MaEnvironment : public MaResourceRegistry {
struct EnvironmentRenderPass : public FullScreenPass {
EnvironmentRenderPass(MaEnvironment* environment);
size_t m_texture_handle_clouds;
struct EnvironmentConstants {
glm::mat4 m_inverse_view_projection;
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(12))
.addItem(nvrhi::BindingLayoutItem::Texture_SRV(14))
.addItem(nvrhi::BindingLayoutItem::Texture_SRV(15))
.addItem(nvrhi::BindingLayoutItem::Texture_SRV(16))
.addItem(nvrhi::BindingLayoutItem::StructuredBuffer_SRV(17))
.addItem(nvrhi::BindingLayoutItem::StructuredBuffer_SRV(18))
@@ -147,6 +148,8 @@ void GbufferBlitPass::Init() {
12, m_contact_shadows->m_output_texture))
.addItem(nvrhi::BindingSetItem::Texture_SRV(
14, m_sky->m_aerial_lut->m_lut))
.addItem(nvrhi::BindingSetItem::Texture_SRV(
15, m_environment->m_clouds_texture))
.addItem(nvrhi::BindingSetItem::Texture_SRV(
16, static_cast<nvrhi::ITexture*>(
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(13, "scene_lit_texture_copy"))
.Add(MaResourceMapping::Texture_SRV(14, "sky_aerial_lut"))
.Add(MaResourceMapping::Texture_SRV(15, "sky_clouds"))
.Add(MaResourceMapping::Texture_SRV(
16, "forwardplus_index_grid_transparent"))
.Add(MaResourceMapping::StructuredBuffer_SRV(
@@ -423,6 +424,7 @@ bool NvRenderer::InitMaterials() {
.addItem(nvrhi::BindingLayoutItem::Texture_SRV(11))
.addItem(nvrhi::BindingLayoutItem::Texture_SRV(12))
.addItem(nvrhi::BindingLayoutItem::Texture_SRV(14))
.addItem(nvrhi::BindingLayoutItem::Texture_SRV(15))
.addItem(nvrhi::BindingLayoutItem::Sampler(8))
.addItem(nvrhi::BindingLayoutItem::Sampler(11))
.addItem(nvrhi::BindingLayoutItem::Sampler(13)));
@@ -445,6 +447,8 @@ bool NvRenderer::InitMaterials() {
12, m_gbuffer->m_gbuffer_depth))
.addItem(nvrhi::BindingSetItem::Texture_SRV(
14, m_sky->m_aerial_lut->m_lut))
.addItem(nvrhi::BindingSetItem::Texture_SRV(
15, m_environment->m_clouds_texture))
.addItem(nvrhi::BindingSetItem::Sampler(
8, m_backend->GetDevice()->createSampler(
nvrhi::SamplerDesc().setAllFilters(true))))

View File

@@ -82,8 +82,8 @@ bool NvRenderer::Init(GLFWwindow *Window) {
m_gbuffer_shadow = std::make_shared<NvGbuffer>(this);
m_contact_shadows = std::make_shared<MaContactShadows>(this, m_gbuffer.get());
m_shadow_map = std::make_shared<MaShadowMap>(this);
m_sky = std::make_shared<Sky>(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_gbuffer_lighting = std::make_shared<GbufferLighting>(this);
m_gbuffer_blit = std::make_shared<GbufferBlitPass>(

View File

@@ -2,6 +2,7 @@
#include <nvrhi/utils.h>
#include "environment.h"
#include "nvrendererbackend.h"
#include "simulationenvironment.h"
#include "simulationtime.h"
@@ -46,8 +47,8 @@ static const glm::mat4x3 M = {
8.572844237945445, -11.103384660054624, 117.47585277566478};
} // namespace
Sky::Sky(NvRenderer* renderer)
: m_backend(renderer->GetBackend()), MaResourceRegistry(renderer) {
Sky::Sky(NvRenderer* renderer, MaEnvironment *environment)
: m_backend(renderer->GetBackend()), MaResourceRegistry(renderer), m_environment(environment) {
m_transmittance_pass = std::make_shared<SkyTransmittancePass>(this);
m_aerial_lut = std::make_shared<SkyAerialLut>(this);
}
@@ -375,6 +376,7 @@ void SkyAerialLut::Init() {
m_lut_width = 128;
m_lut_height = 256;
m_lut_slices = 16;
m_cloud_texture = m_sky->m_environment->m_clouds_texture;
m_constant_buffer = m_sky->m_backend->GetDevice()->createBuffer(
nvrhi::utils::CreateVolatileConstantBufferDesc(
sizeof(DispatchConstants), "Sky Aerial LUT Dispatch Constants", 16));
@@ -398,51 +400,56 @@ void SkyAerialLut::Init() {
.setInitialState(nvrhi::ResourceStates::Common)
.setKeepInitialState(true)
.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,
const glm::dmat4& projection,
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{};
constants.g_InverseView = static_cast<glm::mat3>(glm::inverse(view));

View File

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