Skip to content

Commit

Permalink
Move skymap processing to GPU
Browse files Browse the repository at this point in the history
  • Loading branch information
sergcpp committed May 30, 2024
1 parent 8897b4c commit e165a51
Show file tree
Hide file tree
Showing 24 changed files with 268 additions and 92 deletions.
2 changes: 1 addition & 1 deletion internal/AtmosphereRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ fvec4 SampleTransmittanceLUT(Span<const float> lut, fvec2 uv) {
}

fvec4 SampleMultiscatterLUT(Span<const float> lut, fvec2 uv) {
uv = uv * fvec2(SKY_MULTISCATTER_LUT_RES);
uv = fract(uv - 0.5f / SKY_MULTISCATTER_LUT_RES) * fvec2(SKY_MULTISCATTER_LUT_RES);
auto iuv0 = ivec2(uv);
iuv0 = clamp(iuv0, ivec2{0, 0}, ivec2{SKY_MULTISCATTER_LUT_RES - 1});
const ivec2 iuv1 = min(iuv0 + 1, ivec2{SKY_MULTISCATTER_LUT_RES - 1});
Expand Down
2 changes: 2 additions & 0 deletions internal/Dx/ContextDX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,8 @@ void Ray::Dx::EndSingleTimeCommands(void *api, ID3D12Device *device, ID3D12Comma
command_list->Release();
}

void Ray::Dx::InsertReadbackMemoryBarrier(void *api, CommandBuffer command_list) {}

int Ray::Dx::Context::WriteTimestamp(ID3D12GraphicsCommandList *cmd_buf, const bool start) {
const uint32_t query_index = query_counts_[backend_frame]++;
assert(query_index < MaxTimestampQueries);
Expand Down
2 changes: 2 additions & 0 deletions internal/Dx/ContextDX.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,5 +169,7 @@ CommandBuffer BegSingleTimeCommands(void *api, ID3D12Device *device, ID3D12Comma
void EndSingleTimeCommands(void *api, ID3D12Device *device, ID3D12CommandQueue *cmd_queue, CommandBuffer command_list,
ID3D12CommandAllocator *temp_command_allocator);

void InsertReadbackMemoryBarrier(void *api, CommandBuffer command_list);

} // namespace Dx
} // namespace Ray
17 changes: 2 additions & 15 deletions internal/RendererDX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,6 @@ Ray::Dx::Renderer::Renderer(const settings_t &s, ILog *log) {
indir_args_buf_[1] =
Buffer{"Indir Args (2/2)", ctx_.get(), eBufType::Indirect, 32 * sizeof(DispatchIndirectCommand)};

atmosphere_params_buf_ = Buffer{"Atmosphere Params", ctx_.get(), eBufType::Uniform, sizeof(atmosphere_params_t)};

{ // zero out counters
CommandBuffer cmd_buf = BegSingleTimeCommands(ctx_->api(), ctx_->device(), ctx_->temp_command_pool());

Expand Down Expand Up @@ -666,17 +664,6 @@ void Ray::Dx::Renderer::RenderScene(const SceneBase &scene, RegionContext &regio
}
#endif

if (s.env_.sky_map_spread_angle > 0.0f) {
Buffer temp_upload_buf{"Temp atmosphere params upload", ctx_.get(), eBufType::Upload,
sizeof(atmosphere_params_t)};
{ // update stage buffer
uint8_t *mapped_ptr = temp_upload_buf.Map();
memcpy(mapped_ptr, &s.env_.atmosphere, sizeof(atmosphere_params_t));
temp_upload_buf.Unmap();
}
CopyBufferToBuffer(temp_upload_buf, 0, atmosphere_params_buf_, 0, sizeof(atmosphere_params_t), cmd_buf);
}

//////////////////////////////////////////////////////////////////////////////////

const scene_data_t sc_data = {s.env_,
Expand Down Expand Up @@ -705,7 +692,7 @@ void Ray::Dx::Renderer::RenderScene(const SceneBase &scene, RegionContext &regio
cache_grid_params,
s.spatial_cache_entries_.buf(),
s.spatial_cache_voxels_prev_.buf(),
atmosphere_params_buf_,
s.atmosphere_params_buf_,
s.sky_transmittance_lut_tex_,
s.sky_multiscatter_lut_tex_,
s.sky_moon_tex_,
Expand Down Expand Up @@ -1411,7 +1398,7 @@ void Ray::Dx::Renderer::UpdateSpatialCache(const SceneBase &scene, RegionContext
cache_grid_params,
s.spatial_cache_entries_.buf(),
s.spatial_cache_voxels_curr_.buf(),
atmosphere_params_buf_,
s.atmosphere_params_buf_,
{},
{},
{},
Expand Down
1 change: 0 additions & 1 deletion internal/RendererGPU.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ class Renderer : public RendererBase {
count_table_buf_, reduce_table_buf_;
Buffer counters_buf_, indir_args_buf_[2];

Buffer atmosphere_params_buf_;
Buffer temp_cache_data_buf_, temp_lock_buf_;

Buffer pixel_readback_buf_, base_color_readback_buf_, depth_normals_readback_buf_;
Expand Down
2 changes: 1 addition & 1 deletion internal/RendererGPU_kernels.h
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ inline void Ray::NS::Renderer::kernel_ShadeSky(CommandBuffer cmd_buf, const pass
{&out_img, eResState::UnorderedAccess}};
TransitionResourceStates(cmd_buf, AllStages, AllStages, res_transitions);

SmallVector<Binding, 32> bindings = {
const Binding bindings[] = {
{eBindTarget::UBuf, ShadeSky::ATMOSPHERE_PARAMS_BUF_SLOT, sc_data.atmosphere_params},
{eBindTarget::SBufRO, ShadeSky::RAY_INDICES_BUF_SLOT, ray_indices},
{eBindTarget::SBufRO, ShadeSky::HITS_BUF_SLOT, hits},
Expand Down
32 changes: 4 additions & 28 deletions internal/RendererVK.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -512,8 +512,6 @@ Ray::Vk::Renderer::Renderer(const settings_t &s, ILog *log) {
indir_args_buf_[1] =
Buffer{"Indir Args (2/2)", ctx_.get(), eBufType::Indirect, 32 * sizeof(DispatchIndirectCommand)};

atmosphere_params_buf_ = Buffer{"Atmosphere Params", ctx_.get(), eBufType::Uniform, sizeof(atmosphere_params_t)};

{ // zero out counters, upload random sequence
CommandBuffer cmd_buf = BegSingleTimeCommands(ctx_->api(), ctx_->device(), ctx_->temp_command_pool());

Expand Down Expand Up @@ -680,17 +678,6 @@ void Ray::Vk::Renderer::RenderScene(const SceneBase &scene, RegionContext &regio

ctx_->api().vkCmdResetQueryPool(cmd_buf, ctx_->query_pool(ctx_->backend_frame), 0, MaxTimestampQueries);

if (s.env_.sky_map_spread_angle > 0.0f) {
Buffer temp_upload_buf{"Temp atmosphere params upload", ctx_.get(), eBufType::Upload,
sizeof(atmosphere_params_t)};
{ // update stage buffer
uint8_t *mapped_ptr = temp_upload_buf.Map();
memcpy(mapped_ptr, &s.env_.atmosphere, sizeof(atmosphere_params_t));
temp_upload_buf.Unmap();
}
CopyBufferToBuffer(temp_upload_buf, 0, atmosphere_params_buf_, 0, sizeof(atmosphere_params_t), cmd_buf);
}

//////////////////////////////////////////////////////////////////////////////////

const scene_data_t sc_data = {s.env_,
Expand Down Expand Up @@ -719,14 +706,13 @@ void Ray::Vk::Renderer::RenderScene(const SceneBase &scene, RegionContext &regio
cache_grid_params,
s.spatial_cache_entries_.buf(),
s.spatial_cache_voxels_prev_.buf(),
atmosphere_params_buf_,
s.atmosphere_params_buf_,
s.sky_transmittance_lut_tex_,
s.sky_multiscatter_lut_tex_,
s.sky_moon_tex_,
s.sky_weather_tex_,
s.sky_cirrus_tex_,
s.sky_noise3d_tex_};

TransitionSceneResources(cmd_buf, sc_data);

VkMemoryBarrier mem_barrier = {VK_STRUCTURE_TYPE_MEMORY_BARRIER};
Expand Down Expand Up @@ -1456,7 +1442,7 @@ void Ray::Vk::Renderer::UpdateSpatialCache(const SceneBase &scene, RegionContext
cache_grid_params,
s.spatial_cache_entries_.buf(),
s.spatial_cache_voxels_curr_.buf(),
atmosphere_params_buf_,
s.atmosphere_params_buf_,
{},
{},
{},
Expand Down Expand Up @@ -1740,12 +1726,7 @@ Ray::color_data_rgba_t Ray::Vk::Renderer::get_pixels_ref(const bool tonemap) con
CopyImageToBuffer(buffer_to_use, 0, 0, 0, w_, h_, pixel_readback_buf_, cmd_buf, 0);
}

VkMemoryBarrier mem_barrier = {VK_STRUCTURE_TYPE_MEMORY_BARRIER};
mem_barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
mem_barrier.dstAccessMask = VK_ACCESS_HOST_READ_BIT;

ctx_->api().vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0, 1,
&mem_barrier, 0, nullptr, 0, nullptr);
InsertReadbackMemoryBarrier(ctx_->api(), cmd_buf);

#if RUN_IN_LOCKSTEP
EndSingleTimeCommands(ctx_->api(), ctx_->device(), ctx_->graphics_queue(), cmd_buf, ctx_->temp_command_pool());
Expand Down Expand Up @@ -1809,12 +1790,7 @@ Ray::color_data_rgba_t Ray::Vk::Renderer::get_aux_pixels_ref(const eAUXBuffer bu
CopyImageToBuffer(buffer_to_use, 0, 0, 0, w_, h_, readback_buffer_to_use, cmd_buf, 0);
}

VkMemoryBarrier mem_barrier = {VK_STRUCTURE_TYPE_MEMORY_BARRIER};
mem_barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
mem_barrier.dstAccessMask = VK_ACCESS_HOST_READ_BIT;

ctx_->api().vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0, 1,
&mem_barrier, 0, nullptr, 0, nullptr);
InsertReadbackMemoryBarrier(ctx_->api(), cmd_buf);

#if RUN_IN_LOCKSTEP
EndSingleTimeCommands(ctx_->api(), ctx_->device(), ctx_->graphics_queue(), cmd_buf, ctx_->temp_command_pool());
Expand Down
4 changes: 2 additions & 2 deletions internal/SceneCPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1038,8 +1038,8 @@ void Ray::Cpu::Scene::PrepareEnvMapQTree_nolock() {
for (int qx = 0; qx < cur_res; ++qx) {
for (int jj = -2; jj <= 2; ++jj) {
for (int ii = -2; ii <= 2; ++ii) {
const Ref::fvec2 q = {(float(qx) + 0.5f + ii * FilterSize) / cur_res,
(float(qy) + 0.5f + jj * FilterSize) / cur_res};
const Ref::fvec2 q = {Ref::fract(1.0f + (float(qx) + 0.5f + ii * FilterSize) / cur_res),
Ref::fract(1.0f + (float(qy) + 0.5f + jj * FilterSize) / cur_res)};
Ref::fvec4 dir;
CanonicalToDir(value_ptr(q), 0.0f, value_ptr(dir));

Expand Down
2 changes: 1 addition & 1 deletion internal/SceneCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ Ray::SceneCommon::CalcSkyEnvTexture(const atmosphere_params_t &params, const int
parallel_for(0, res[1], [&](const int y) {
const float theta = PI * float(y) / float(res[1]);
for (int x = 0; x < res[0]; ++x) {
const uint32_t px_hash = Ref::hash((x << 16)| x);
const uint32_t px_hash = Ref::hash((x << 16)| y);

const float phi = 2.0f * PI * (x + 0.5f) / float(res[0]);
auto ray_dir = Ref::fvec4{sinf(theta) * cosf(phi), cosf(theta), sinf(theta) * sinf(phi), 0.0f};
Expand Down
12 changes: 12 additions & 0 deletions internal/SceneDX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "Dx/ContextDX.h"
#include "TextureParams.h"
#include "TextureUtils.h"
#include "inflate/Inflate.h"

namespace Ray {
uint32_t next_power_of_two(uint32_t v);
Expand All @@ -35,7 +36,11 @@ void to_dxr_xform(const float xform[16], float matrix[3][4]) {
}
}

namespace Dx {
#include "shaders/output/bake_sky.comp.cso.inl"

const uint32_t AccStructAlignment = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BYTE_ALIGNMENT;
} // namespace Dx
} // namespace Ray

Ray::Dx::Scene::~Scene() {
Expand All @@ -60,6 +65,13 @@ Ray::Dx::Scene::~Scene() {
bindless_textures_.clear();
}

bool Ray::Dx::Scene::InitPipelines() {
sh_bake_sky_ =
Shader{"Bake Sky", ctx_, Inflate(internal_shaders_output_bake_sky_comp_cso), eShaderType::Comp, log_};
prog_bake_sky_ = Program{"Bake Sky", ctx_, &sh_bake_sky_, log_};
return pi_bake_sky_.Init(ctx_, &prog_bake_sky_, log_);
}

void Ray::Dx::Scene::GenerateTextureMips_nolock() {}

void Ray::Dx::Scene::PrepareBindlessTextures_nolock() {
Expand Down
4 changes: 4 additions & 0 deletions internal/SceneDX.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

#include "Dx/AccStructureDX.h"
#include "Dx/DescriptorPoolDX.h"
#include "Dx/DrawCallDX.h"
#include "Dx/PipelineDX.h"
#include "Dx/ProgramDX.h"
#include "Dx/ShaderDX.h"
#include "Dx/SparseStorageDX.h"
#include "Dx/TextureAtlasDX.h"
#include "Dx/TextureDX.h"
Expand Down
Loading

0 comments on commit e165a51

Please sign in to comment.