mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-03-22 15:05:03 +01:00
implement moon
This commit is contained in:
@@ -381,7 +381,7 @@ set_target_properties( ${PROJECT_NAME}
|
||||
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
|
||||
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
|
||||
PDB_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/pdb"
|
||||
OUTPUT_NAME "${PROJECT_NAME}${PREFIX}_${SRC_SHORT_DATE}"
|
||||
OUTPUT_NAME "${PROJECT_NAME}${PREFIX}_${SRC_SHORT_DATE}_${GIT_HASH}"
|
||||
DEBUG_POSTFIX "_d"
|
||||
)
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include "gbuffer.h"
|
||||
#include "nvrenderer/nvrenderer.h"
|
||||
#include "nvrendererbackend.h"
|
||||
#include "simulationenvironment.h"
|
||||
#include "sky.h"
|
||||
|
||||
int MaEnvironment::GetCurrentSetIndex() {
|
||||
@@ -300,6 +301,9 @@ void MaEnvironment::Init(const std::string& texture, float pre_exposure) {
|
||||
CommandListHandle command_list = m_backend->GetDevice()->createCommandList();
|
||||
command_list->open();
|
||||
|
||||
RegisterResource(true, "sampler_linear_clamp_v_repeat_h",
|
||||
m_sampler_linear_clamp_v_repeat_h, ResourceType::Sampler);
|
||||
|
||||
{
|
||||
m_brdf_lut = m_backend->GetDevice()->createTexture(
|
||||
TextureDesc()
|
||||
@@ -597,10 +601,10 @@ void MaEnvironment::Filter(uint64_t sky_instance_id, NvGbuffer* gbuffer_cube) {
|
||||
// m_backend->GetDevice()->executeCommandList(command_list_transition);
|
||||
|
||||
if (prev_instance != -1) {
|
||||
//m_backend->GetDevice()->queueWaitForCommandList(
|
||||
// nvrhi::CommandQueue::Compute, nvrhi::CommandQueue::Graphics,
|
||||
// prev_instance);
|
||||
//prev_instance = -1;
|
||||
// m_backend->GetDevice()->queueWaitForCommandList(
|
||||
// nvrhi::CommandQueue::Compute, nvrhi::CommandQueue::Graphics,
|
||||
// prev_instance);
|
||||
// prev_instance = -1;
|
||||
}
|
||||
CommandListHandle command_list = m_backend->GetDevice()->createCommandList(
|
||||
CommandListParameters()
|
||||
@@ -645,6 +649,10 @@ void MaEnvironment::Filter(uint64_t sky_instance_id, NvGbuffer* gbuffer_cube) {
|
||||
filter_constants.m_offset_z = 0;
|
||||
filter_constants.m_light_direction = -daylight.direction;
|
||||
filter_constants.m_height = Global.pCamera.Pos.y;
|
||||
|
||||
m_sky->CalcLighting(filter_constants.m_light_direction,
|
||||
filter_constants.m_light_color);
|
||||
|
||||
filter_constants.m_light_color = glm::vec4(m_sky->CalcSunColor(), 1.f);
|
||||
command_list->setPushConstants(&filter_constants,
|
||||
sizeof(filter_constants));
|
||||
@@ -878,7 +886,7 @@ void EnvironmentRenderPass::Init() {
|
||||
.addItem(nvrhi::BindingSetItem::Sampler(
|
||||
0, m_environment->m_sampler_linear_clamp))
|
||||
.addItem(nvrhi::BindingSetItem::Sampler(
|
||||
13, m_environment->m_sampler_linear_clamp))
|
||||
13, m_environment->m_sampler_linear_clamp_v_repeat_h))
|
||||
.addItem(nvrhi::BindingSetItem::ConstantBuffer(
|
||||
0, m_environment_constants)),
|
||||
m_binding_layout);
|
||||
@@ -911,7 +919,8 @@ void EnvironmentRenderPass::UpdateConstants(
|
||||
glm::inverse(projection * view) * previous_view_projection;
|
||||
constants.m_inverse_view_projection =
|
||||
glm::inverse(jitter * projection * glm::dmat4(glm::dmat3(view)));
|
||||
constants.m_sun_direction = -Global.DayLight.direction;
|
||||
constants.m_sun_direction = simulation::Environment.sun().getDirection();
|
||||
constants.m_moon_direction = simulation::Environment.moon().getDirection();
|
||||
constants.m_height = Global.pCamera.Pos.y;
|
||||
|
||||
command_list->writeBuffer(m_environment_constants, &constants,
|
||||
|
||||
@@ -92,7 +92,7 @@ struct MaEnvironment : public MaResourceRegistry {
|
||||
uint32_t m_unused;
|
||||
glm::vec3 m_light_direction;
|
||||
float m_height;
|
||||
glm::vec4 m_light_color;
|
||||
glm::vec3 m_light_color;
|
||||
};
|
||||
|
||||
MaEnvironment(class NvRenderer* renderer);
|
||||
@@ -112,6 +112,7 @@ struct EnvironmentRenderPass : public FullScreenPass {
|
||||
glm::mat4 m_reproject_matrix;
|
||||
glm::vec3 m_sun_direction;
|
||||
float m_height;
|
||||
glm::vec3 m_moon_direction;
|
||||
};
|
||||
|
||||
virtual void Init() override;
|
||||
|
||||
@@ -45,18 +45,26 @@ void GbufferBlitPass::Init() {
|
||||
.setAllFilters(true));
|
||||
const nvrhi::SamplerHandle sampler_linear =
|
||||
m_backend->GetDevice()->createSampler(
|
||||
nvrhi::SamplerDesc().setAllFilters(true));
|
||||
nvrhi::SamplerDesc().setAllFilters(true));
|
||||
const nvrhi::SamplerHandle sampler_linear_clamp =
|
||||
m_backend->GetDevice()->createSampler(
|
||||
nvrhi::SamplerDesc()
|
||||
.setAllAddressModes(nvrhi::SamplerAddressMode::ClampToEdge)
|
||||
.setAllFilters(true));
|
||||
const nvrhi::SamplerHandle sampler_linear_clamp_v_repeat_h =
|
||||
m_backend->GetDevice()->createSampler(
|
||||
nvrhi::SamplerDesc()
|
||||
.setAllAddressModes(nvrhi::SamplerAddressMode::ClampToEdge)
|
||||
.setAddressU(nvrhi::SamplerAddressMode::Repeat)
|
||||
.setAllFilters(true));
|
||||
RegisterResource(true, "shadow_sampler_comp", sampler_shadow_comp,
|
||||
nvrhi::ResourceType::Sampler);
|
||||
RegisterResource(true, "sampler_linear_wrap", sampler_linear,
|
||||
nvrhi::ResourceType::Sampler);
|
||||
RegisterResource(true, "sampler_linear_clamp", sampler_linear_clamp,
|
||||
nvrhi::ResourceType::Sampler);
|
||||
RegisterResource(true, "sampler_linear_clamp_v_repeat_h", sampler_linear_clamp_v_repeat_h,
|
||||
nvrhi::ResourceType::Sampler);
|
||||
m_scene_depth = m_backend->GetDevice()->createTexture(
|
||||
nvrhi::TextureDesc(m_gbuffer->m_gbuffer_depth->getDesc())
|
||||
.setFormat(nvrhi::Format::R32_FLOAT)
|
||||
@@ -144,7 +152,7 @@ void GbufferBlitPass::Init() {
|
||||
.m_resource)))
|
||||
.addItem(nvrhi::BindingSetItem::Sampler(8, sampler_linear))
|
||||
.addItem(nvrhi::BindingSetItem::Sampler(11, sampler_shadow_comp))
|
||||
.addItem(nvrhi::BindingSetItem::Sampler(13, sampler_linear_clamp))
|
||||
.addItem(nvrhi::BindingSetItem::Sampler(13, sampler_linear_clamp_v_repeat_h))
|
||||
.addItem(nvrhi::BindingSetItem::Texture_UAV(0, m_output)),
|
||||
m_binding_layout);
|
||||
}
|
||||
@@ -174,8 +182,9 @@ void GbufferBlitPass::UpdateConstants(nvrhi::ICommandList* command_list,
|
||||
constants.m_inverse_projection = glm::inverse(projection);
|
||||
|
||||
const auto& daylight = Global.DayLight;
|
||||
constants.m_light_dir = glm::vec4(-daylight.direction, 0.f);
|
||||
constants.m_light_color = glm::vec4(m_sky->CalcSunColor(), 1.);
|
||||
|
||||
m_sky->CalcLighting(constants.m_light_dir,
|
||||
constants.m_light_color);
|
||||
constants.m_altitude = Global.pCamera.Pos.y;
|
||||
constants.m_time = Timer::GetTime();
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ struct GbufferBlitPass : public FullScreenPass, public MaResourceRegistry {
|
||||
glm::mat4 m_inverse_projection;
|
||||
glm::vec3 m_light_dir;
|
||||
float m_altitude;
|
||||
glm::vec4 m_light_color;
|
||||
glm::vec3 m_light_color;
|
||||
float m_time;
|
||||
};
|
||||
|
||||
|
||||
@@ -225,7 +225,7 @@ void NvRenderer::MaterialTemplate::Init(const YAML::Node &conf) {
|
||||
.Add(MaResourceMapping::StructuredBuffer_SRV(18, "forwardplus_light_buffer"))
|
||||
.Add(MaResourceMapping::Sampler(8, "sampler_linear_wrap"))
|
||||
.Add(MaResourceMapping::Sampler(11, "shadow_sampler_comp"))
|
||||
.Add(MaResourceMapping::Sampler(13, "sampler_linear_clamp"));
|
||||
.Add(MaResourceMapping::Sampler(13, "sampler_linear_clamp_v_repeat_h"));
|
||||
break;
|
||||
default:;
|
||||
}
|
||||
|
||||
@@ -222,13 +222,36 @@ glm::vec4 Sky::GetFogScatteringCoefficient(float h) const {
|
||||
|
||||
glm::vec3 Sky::CalcSunColor() const {
|
||||
glm::vec4 transmittance =
|
||||
ComputeTransmittance(-Global.DayLight.direction, 32);
|
||||
return simulation::Environment.light_intensity() *
|
||||
ComputeTransmittance(simulation::Environment.sun().getDirection(), 32);
|
||||
return LinearSrgbFromSpectralSamples(sun_spectral_irradiance *
|
||||
transmittance) *
|
||||
glm::exp(EXPOSURE);
|
||||
}
|
||||
|
||||
glm::vec3 Sky::CalcMoonColor() const {
|
||||
float phase = glm::acos(dot(simulation::Environment.sun().getDirection(),
|
||||
simulation::Environment.moon().getDirection())) *
|
||||
glm::one_over_pi<float>();
|
||||
glm::vec4 transmittance =
|
||||
ComputeTransmittance(simulation::Environment.moon().getDirection(), 32);
|
||||
return .07f * phase *
|
||||
LinearSrgbFromSpectralSamples(sun_spectral_irradiance *
|
||||
transmittance) *
|
||||
glm::exp(EXPOSURE);
|
||||
}
|
||||
|
||||
void Sky::CalcLighting(glm::vec3& direction, glm::vec3& color) const {
|
||||
const glm::vec3 moon = CalcMoonColor();
|
||||
const glm::vec3 sun = CalcSunColor();
|
||||
if (dot(moon, moon) > dot(sun, sun)) {
|
||||
direction = simulation::Environment.moon().getDirection();
|
||||
color = moon;
|
||||
} else {
|
||||
direction = simulation::Environment.sun().getDirection();
|
||||
color = sun;
|
||||
}
|
||||
}
|
||||
|
||||
glm::vec3 Sky::LinearSrgbFromSpectralSamples(glm::vec4 L) {
|
||||
glm::vec3 c = M * L;
|
||||
return c.r * glm::vec3(0.4123908f, 0.21263901f, 0.01933082f) +
|
||||
@@ -347,11 +370,11 @@ nvrhi::IFramebuffer* SkyTransmittancePass::GetFramebuffer() {
|
||||
}
|
||||
|
||||
void SkyAerialLut::Init() {
|
||||
m_sky_width = 128;
|
||||
m_sky_width = 256;
|
||||
m_sky_height = 512;
|
||||
m_lut_width = 64;
|
||||
m_lut_width = 128;
|
||||
m_lut_height = 256;
|
||||
m_lut_slices = 32;
|
||||
m_lut_slices = 16;
|
||||
m_constant_buffer = m_sky->m_backend->GetDevice()->createBuffer(
|
||||
nvrhi::utils::CreateVolatileConstantBufferDesc(
|
||||
sizeof(DispatchConstants), "Sky Aerial LUT Dispatch Constants", 16));
|
||||
@@ -424,7 +447,8 @@ void SkyAerialLut::Render(nvrhi::ICommandList* command_list,
|
||||
DispatchConstants constants{};
|
||||
constants.g_InverseView = static_cast<glm::mat3>(glm::inverse(view));
|
||||
constants.g_InverseProjection = glm::inverse(projection);
|
||||
constants.g_SunDir = -Global.DayLight.direction;
|
||||
constants.g_SunDir = simulation::Environment.sun().getDirection();
|
||||
constants.g_MoonDir = simulation::Environment.moon().getDirection();
|
||||
constants.g_Altitude = Global.pCamera.Pos.y;
|
||||
constants.g_MaxDepth = Global.BaseDrawRange * Global.fDistanceFactor;
|
||||
command_list->writeBuffer(m_constant_buffer, &constants, sizeof(constants));
|
||||
|
||||
@@ -52,6 +52,8 @@ struct Sky : public MaResourceRegistry {
|
||||
glm::vec4 GetMolecularAbsorptionCoefficient(float h) const;
|
||||
glm::vec4 GetFogScatteringCoefficient(float h) const;
|
||||
glm::vec3 CalcSunColor() const;
|
||||
glm::vec3 CalcMoonColor() const;
|
||||
void CalcLighting(glm::vec3& direction, glm::vec3& color) const;
|
||||
static glm::vec3 LinearSrgbFromSpectralSamples(glm::vec4 L);
|
||||
static float RaySphereIntersection(glm::vec3 ro, glm::vec3 rd, float radius);
|
||||
float GetAerosolDensity(float h) const;
|
||||
@@ -93,6 +95,7 @@ struct SkyAerialLut {
|
||||
glm::mat4 g_InverseProjection;
|
||||
glm::vec3 g_SunDir;
|
||||
float g_Altitude;
|
||||
glm::vec3 g_MoonDir;
|
||||
float g_MaxDepth;
|
||||
};
|
||||
SkyAerialLut(Sky* sky) : m_sky(sky) {}
|
||||
|
||||
2
moon.cpp
2
moon.cpp
@@ -49,7 +49,7 @@ cMoon::render() {
|
||||
}
|
||||
|
||||
glm::vec3
|
||||
cMoon::getDirection() {
|
||||
cMoon::getDirection() const {
|
||||
|
||||
return m_position;
|
||||
}
|
||||
|
||||
2
moon.h
2
moon.h
@@ -14,7 +14,7 @@ public:
|
||||
void update( bool const Includephase = false );
|
||||
void render();
|
||||
// returns vector pointing at the sun
|
||||
glm::vec3 getDirection();
|
||||
glm::vec3 getDirection() const;
|
||||
// returns current elevation above horizon
|
||||
float getAngle() const;
|
||||
// returns current intensity of the sun
|
||||
|
||||
@@ -43,6 +43,9 @@ public:
|
||||
inline auto const &
|
||||
sun() const {
|
||||
return m_sun; }
|
||||
inline auto const &
|
||||
moon() const {
|
||||
return m_moon; }
|
||||
inline auto const &
|
||||
light_intensity() const {
|
||||
return m_lightintensity; }
|
||||
|
||||
2
sun.cpp
2
sun.cpp
@@ -52,7 +52,7 @@ cSun::getPosition() {
|
||||
}
|
||||
*/
|
||||
glm::vec3
|
||||
cSun::getDirection() {
|
||||
cSun::getDirection() const {
|
||||
|
||||
return m_position;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user