Skip to content

Commit

Permalink
Merge branch 'gbuffers'
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelarius committed Mar 31, 2024
2 parents fdab5d1 + 85d4c2c commit 41ee498
Show file tree
Hide file tree
Showing 14 changed files with 809 additions and 130 deletions.
7 changes: 4 additions & 3 deletions CMakeLists.txt
Expand Up @@ -74,8 +74,9 @@ target_link_libraries(pt-format-tool PRIVATE common fmt pt-format glm::glm)
set(WGSL_SHADER_FILES
reference_path_tracer.wgsl
texture_blit.wgsl
hybrid_renderer_gbuffer_pass.wgsl
hybrid_renderer_debug_pass.wgsl)
deferred_renderer_gbuffer_pass.wgsl
deferred_renderer_debug_pass.wgsl
deferred_renderer_lighting_pass.wgsl)

set(SHADER_SOURCE_HEADER_FILE src/pt/shader_source.hpp)

Expand All @@ -97,7 +98,7 @@ set(PT_SOURCE_FILES
gpu_buffer.cpp
gpu_context.cpp
gui.cpp
hybrid_renderer.cpp
deferred_renderer.cpp
reference_path_tracer.cpp
texture_blit_renderer.cpp
window.cpp)
Expand Down
1 change: 1 addition & 0 deletions src/common/extent.hpp
Expand Up @@ -30,6 +30,7 @@ struct Extent2

using Extent2i = Extent2<std::int32_t>;
using Extent2u = Extent2<std::uint32_t>;
using Extent2f = Extent2<float>;

template<typename T>
constexpr float aspectRatio(const Extent2<T>& extent) noexcept
Expand Down
72 changes: 72 additions & 0 deletions src/pt/aligned_sky_state.hpp
@@ -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

0 comments on commit 41ee498

Please sign in to comment.