Skip to content

Commit

Permalink
wip: aligned sky state
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelarius committed Mar 29, 2024
1 parent c67b3ea commit dc8ec13
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 58 deletions.
72 changes: 72 additions & 0 deletions src/pt/aligned_sky_state.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#pragma once

#include <common/assert.hpp>
#include <common/units/angle.hpp>

#include <glm/glm.hpp>
#include <hw-skymodel/hw_skymodel.h>

#include <array>
#include <cstring>
#include <numbers>

namespace nlrs
{
struct Sky
{
float turbidity = 1.0f;
std::array<float, 3> albedo = {1.0f, 1.0f, 1.0f};
float sunZenithDegrees = 30.0f;
float sunAzimuthDegrees = 0.0f;

bool operator==(const Sky&) const noexcept = default;
};

// A 16-byte aligned sky state for the hw-skymodel library. Matches the layout of the following WGSL
// struct:
//
// struct SkyState {
// params: array<f32, 27>,
// skyRadiances: array<f32, 3>,
// solarRadiances: array<f32, 3>,
// sunDirection: vec3<f32>,
// };
struct AlignedSkyState
{
float params[27]; // offset: 0
float skyRadiances[3]; // offset: 27
float solarRadiances[3]; // offset: 30
float padding1[3]; // offset: 33
glm::vec3 sunDirection; // offset: 36
float padding2; // offset: 39

inline AlignedSkyState(const Sky& sky)
: params{0},
skyRadiances{0},
solarRadiances{0},
padding1{0.f, 0.f, 0.f},
sunDirection(0.f),
padding2(0.0f)
{
const float sunZenith = Angle::degrees(sky.sunZenithDegrees).asRadians();
const float sunAzimuth = Angle::degrees(sky.sunAzimuthDegrees).asRadians();

sunDirection = glm::normalize(glm::vec3(
std::sin(sunZenith) * std::cos(sunAzimuth),
std::cos(sunZenith),
-std::sin(sunZenith) * std::sin(sunAzimuth)));

const sky_params skyParams{
.elevation = 0.5f * std::numbers::pi_v<float> - sunZenith,
.turbidity = sky.turbidity,
.albedo = {sky.albedo[0], sky.albedo[1], sky.albedo[2]}};

sky_state skyState;
NLRS_ASSERT(sky_state_new(&skyParams, &skyState) == sky_state_result_success);

std::memcpy(params, skyState.params, sizeof(skyState.params));
std::memcpy(skyRadiances, skyState.sky_radiances, sizeof(skyState.sky_radiances));
std::memcpy(solarRadiances, skyState.solar_radiances, sizeof(skyState.solar_radiances));
}
};
} // namespace nlrs
51 changes: 3 additions & 48 deletions src/pt/reference_path_tracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

#include <common/bvh.hpp>
#include <common/gltf_model.hpp>
#include <hw-skymodel/hw_skymodel.h>

#include <fmt/core.h>
#include <glm/glm.hpp>
Expand All @@ -27,9 +26,6 @@

namespace nlrs
{
inline constexpr float PI = std::numbers::pi_v<float>;
inline constexpr float DEGREES_TO_RADIANS = PI / 180.0f;

namespace
{
struct FrameDataLayout
Expand Down Expand Up @@ -96,47 +92,6 @@ struct SamplingStateLayout
}
};

struct SkyStateLayout
{
float params[27]; // offset: 0
float skyRadiances[3]; // offset: 27
float solarRadiances[3]; // offset: 30
float padding1[3]; // offset: 33
glm::vec3 sunDirection; // offset: 36
float padding2; // offset: 39

SkyStateLayout(const Sky& sky)
: params{0},
skyRadiances{0},
solarRadiances{0},
padding1{0.f, 0.f, 0.f},
sunDirection(0.f),
padding2(0.0f)
{
const float sunZenith = sky.sunZenithDegrees * DEGREES_TO_RADIANS;
const float sunAzimuth = sky.sunAzimuthDegrees * DEGREES_TO_RADIANS;

sunDirection = glm::normalize(glm::vec3(
std::sin(sunZenith) * std::cos(sunAzimuth),
std::cos(sunZenith),
-std::sin(sunZenith) * std::sin(sunAzimuth)));

const sky_params skyParams{
.elevation = 0.5f * PI - sunZenith,
.turbidity = sky.turbidity,
.albedo = {sky.albedo[0], sky.albedo[1], sky.albedo[2]}};

sky_state skyState;
[[maybe_unused]] const auto r = sky_state_new(&skyParams, &skyState);
// TODO: exceptional error handling
assert(r == sky_state_result_success);

std::memcpy(params, skyState.params, sizeof(skyState.params));
std::memcpy(skyRadiances, skyState.sky_radiances, sizeof(skyState.sky_radiances));
std::memcpy(solarRadiances, skyState.solar_radiances, sizeof(skyState.solar_radiances));
}
};

struct RenderParamsLayout
{
FrameDataLayout frameData;
Expand Down Expand Up @@ -187,7 +142,7 @@ ReferencePathTracer::ReferencePathTracer(
gpuContext.device,
"sky state buffer",
GpuBufferUsage::ReadOnlyStorage | GpuBufferUsage::CopyDst,
sizeof(SkyStateLayout)),
sizeof(AlignedSkyState)),
mRenderParamsBindGroup(),
mBvhNodeBuffer(
gpuContext.device,
Expand Down Expand Up @@ -624,9 +579,9 @@ void ReferencePathTracer::render(const GpuContext& gpuContext, WGPUTextureView t
0,
&mCurrentPostProcessingParams,
sizeof(PostProcessingParameters));
const SkyStateLayout skyStateLayout{mCurrentRenderParams.sky};
const AlignedSkyState skyState{mCurrentRenderParams.sky};
wgpuQueueWriteBuffer(
gpuContext.queue, mSkyStateBuffer.ptr(), 0, &skyStateLayout, sizeof(SkyStateLayout));
gpuContext.queue, mSkyStateBuffer.ptr(), 0, &skyState, sizeof(AlignedSkyState));
}

const WGPUCommandEncoder encoder = [&gpuContext]() {
Expand Down
11 changes: 1 addition & 10 deletions src/pt/reference_path_tracer.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "aligned_sky_state.hpp"
#include "gpu_bind_group.hpp"
#include "gpu_buffer.hpp"

Expand Down Expand Up @@ -29,16 +30,6 @@ struct SamplingParams
bool operator==(const SamplingParams&) const noexcept = default;
};

struct Sky
{
float turbidity = 1.0f;
std::array<float, 3> albedo = {1.0f, 1.0f, 1.0f};
float sunZenithDegrees = 30.0f;
float sunAzimuthDegrees = 0.0f;

bool operator==(const Sky&) const noexcept = default;
};

struct RenderParameters
{
Extent2u framebufferSize;
Expand Down

0 comments on commit dc8ec13

Please sign in to comment.