#include "depth_normal.h"
#include "renderer.h"
#include "shaders/renderer_types.h"
namespace depth_normal {
Resources record(
rhi::Device &device,
Pipelines &pipelines,
rhi::ShaderCompiler &sc,
FrameGraph &fg,
BufferHandle indirect,
BufferHandle draw_count,
BufferHandle visible
) {
rhi::RenderPipelineDesc pd;
pd.device = &device;
pd.set_shaders(sc, "src/passes/depth_pre/depth_normal.slang", "vsMain", "fsMain");
pd.set_depth_testing(true, true, rhi::PipelineCompareOp::GREATER_EQUAL);
pd.set_cull_mode(rhi::PipelineCullMode::BACK, rhi::PipelineTriangleWindingOrder::CW);
pd.set_color_format(rhi::ImageFormat::RGBA16_FLOAT);
pd.set_depth_format(rhi::ImageFormat::D32_FLOAT);
pipelines.add_render("DepthNormal", pd);
Resources res;
res.normal = fg.add_image({
.desc = {.format = rhi::ImageFormat::RGBA16_FLOAT},
.name = "fg/depth/normal",
.size_class = SizeOp::Swapchain,
.view = {.type = rhi::ImageViewType::Sampled},
});
res.depth = fg.add_image({
.desc = {.format = rhi::ImageFormat::D32_FLOAT},
.name = "fg/depth/depth",
.size_class = SizeOp::Swapchain,
.view = {.aspect = rhi::ImageAspect::Depth, .type = rhi::ImageViewType::Sampled, .mip_count = 1},
});
auto &builder = fg.add_pass("DepthNormal");
builder.read(indirect, rhi::ResourceState::IndirectFetch);
builder.read(draw_count, rhi::ResourceState::IndirectFetch);
builder.read(visible, rhi::ResourceState::StorageRead);
builder.write(res.normal, rhi::ResourceState::ColorDraw);
builder.write(res.depth, rhi::ResourceState::DepthDraw);
rhi::RenderPipeline &pipeline = pipelines.get_render("DepthNormal");
builder.execute([=, &pipeline](PassContext &ctx) {
auto &frame = ctx.frame_data[ctx.frame_index];
auto normal_view = ctx.image_view(res.normal, {.aspect = rhi::ImageAspect::Color, .mip_count = 1});
auto depth_view = ctx.image_view(res.depth, {.aspect = rhi::ImageAspect::Depth, .mip_count = 1});
rhi::RenderingInfo ri{};
ri.colorAttachmentCount = 1;
ri.colorAttachments[0].img = &normal_view;
ri.colorAttachments[0].loadOp = rhi::LoadOp::CLEAR;
ri.colorAttachments[0].storeOp = rhi::StoreOp::STORE;
ri.colorAttachments[0].clearValue.color.float32[0] = 0.0f;
ri.colorAttachments[0].clearValue.color.float32[1] = 0.0f;
ri.colorAttachments[0].clearValue.color.float32[2] = 0.0f;
ri.colorAttachments[0].clearValue.color.float32[3] = 0.0f;
ri.depthAttachment.img = &depth_view;
ri.depthAttachment.loadOp = rhi::LoadOp::CLEAR;
ri.depthAttachment.storeOp = rhi::StoreOp::STORE;
ri.depthAttachment.clearValue.depthStencil = {0.0f, 0};
ri.width = ctx.render_width;
ri.height = ctx.render_height;
rhi::begin_rendering(ctx.cmd, ri);
rhi::set_pipeline(ctx.cmd, pipeline);
rhi::Viewport vp{};
vp.width = (f32)ctx.render_width;
vp.height = (f32)ctx.render_height;
rhi::set_viewports(ctx.cmd, &vp);
rhi::Rect scissor{};
scissor.width = (i32)ctx.render_width;
scissor.height = (i32)ctx.render_height;
rhi::set_scissors(ctx.cmd, &scissor);
FwdDrawPushConstants pc{};
pc.frame = (u64)ctx.render_data->frame_ubo_view[ctx.frame_index].slot;
pc.vertex = (u64)ctx.render_data->vertex_view.slot;
pc.visible_items = rhi::buffer_device_address(ctx.device, ctx.get_buffer(visible));
{
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::set_index_buffer(ctx.cmd, ctx.render_data->index_buffer);
rhi::draw_indexed_indirect(
ctx.cmd,
ctx.get_buffer(indirect),
&ctx.get_buffer(draw_count),
ctx.render_data->render_item_count,
rhi::DRAW_INDEXED_INDIRECT_STRIDE,
0,
0
);
rhi::end_rendering(ctx.cmd);
});
return res;
}
} // namespace depth_normal