#include "ddgi_update.h"
#include "renderer.h"
#include "shaders/renderer_types.h"
namespace ddgi_update {
void record(
FrameGraph &fg,
ddgi_trace::Resources &res,
ImageHandle &target_image,
rhi::ComputePipeline &pipeline,
const char *pass_name
) {
auto &builder = fg.add_pass(pass_name);
builder.read(res.ray_samples, rhi::ResourceState::StorageRead);
builder.read(res.probe_state, rhi::ResourceState::StorageRead);
builder.read(target_image, rhi::ResourceState::TextureSampleNonFragment);
builder.write(target_image, rhi::ResourceState::StorageReadWrite);
// storage view resolved at compile, execute only reads
u32 storage_view = fg.request_extra_view(target_image, false);
builder.execute([=, &pipeline](PassContext &ctx) {
FrameData &frame = ctx.frame_data[ctx.frame_index];
rhi::set_pipeline(ctx.cmd, pipeline);
DdgiUpdatePushConstants pc{};
pc.frame = (u64)ctx.render_data->frame_ubo_view[ctx.frame_index].slot;
pc.frame_counter = (u32)ctx.frame_count;
pc.atlas_sample = (u32)ctx.view(target_image);
pc.atlas_storage = (u32)ctx.view_extra(storage_view);
pc.samples_buffer = rhi::buffer_device_address(ctx.device, ctx.get_buffer(res.ray_samples));
pc.probe_state = rhi::buffer_device_address(ctx.device, ctx.get_buffer(res.probe_state));
{
static_assert(sizeof(pc) <= 128, "push constants exceed global 128B range");
char pc128[128] = {};
std::memcpy(pc128, &pc, sizeof(pc));
rhi::set_constants(ctx.cmd, pipeline, rhi::ShaderStage::ALL, sizeof(pc128), pc128);
}
rhi::dispatch(ctx.cmd, res.grid.dims.x, res.grid.dims.y, res.grid.dims.z);
});
}
} // namespace ddgi_update