grid.cpp

cross platform rendering playground

src/passes/grid/grid.cpp

4.49 KB
#include "grid.h"

#include "pipelines.h"
#include "renderer.h"
#include "shaders/renderer_types.h"

namespace grid {

void record(
    rhi::Device &device,
    Pipelines &pipelines,
    rhi::ShaderCompiler &sc,
    FrameGraph &fg,
    ImageHandle main_image,
    ImageHandle depth_image
) {

    rhi::RenderPipelineDesc pd;
    pd.device = &device;
    pd.set_shaders(sc, "src/passes/grid/grid.slang", "vsMain", "fsMain");
    pd.set_depth_testing(true, false, rhi::PipelineCompareOp::GREATER_EQUAL);
    pd.set_blending();
    pd.set_cull_mode(rhi::PipelineCullMode::BACK, rhi::PipelineTriangleWindingOrder::CCW);
    pd.set_color_format(rhi::ImageFormat::RGBA16_FLOAT);
    pd.set_depth_format(rhi::ImageFormat::D32_FLOAT);
    rhi::RenderPipeline &pipeline = pipelines.add_render("Grid", pd);

    // Depth is tested (read) and stored (write): LOAD + depth-test + STORE.
    auto &builder = fg.add_pass("Grid")
                        .write(main_image, rhi::ResourceState::ColorDraw)
                        .read_write(depth_image, rhi::ResourceState::DepthDraw)
                        .execute([=, &pipeline](PassContext &ctx) {
                            auto main_view = ctx.image_view(
                                main_image,
                                {
                                    .aspect = rhi::ImageAspect::Color,
                                    .dimension = rhi::TextureViewDimension::TEXTURE_2D,
                                    .mip_start = 0,
                                    .mip_count = 1,
                                    .layer_start = 0,
                                    .layer_count = 1,
                                }
                            );

                            auto depth_view = ctx.image_view(
                                depth_image,
                                {
                                    .aspect = rhi::ImageAspect::Depth,
                                    .dimension = rhi::TextureViewDimension::TEXTURE_2D,
                                    .mip_start = 0,
                                    .mip_count = 1,
                                    .layer_start = 0,
                                    .layer_count = 1,
                                }
                            );

                            rhi::AttachmentDesc color_att{};
                            color_att.img = &main_view;
                            color_att.loadOp = rhi::LoadOp::LOAD;
                            color_att.storeOp = rhi::StoreOp::STORE;

                            rhi::AttachmentDesc depth_att{};
                            depth_att.img = &depth_view;
                            depth_att.loadOp = rhi::LoadOp::LOAD;
                            depth_att.storeOp = rhi::StoreOp::STORE;

                            rhi::RenderingInfo ri{};
                            ri.colorAttachments[0] = color_att;
                            ri.colorAttachmentCount = 1;
                            ri.depthAttachment = depth_att;
                            ri.width = ctx.render_width;
                            ri.height = ctx.render_height;

                            rhi::begin_rendering(ctx.cmd, ri);
                            rhi::set_pipeline(ctx.cmd, pipeline);

                            rhi::Viewport viewport{};
                            viewport.width = (f32)ctx.render_width;
                            viewport.height = (f32)ctx.render_height;
                            rhi::set_viewports(ctx.cmd, &viewport);

                            rhi::Rect scissor{};
                            scissor.width = (i32)ctx.render_width;
                            scissor.height = (i32)ctx.render_height;
                            rhi::set_scissors(ctx.cmd, &scissor);

                            FrameData &frame = ctx.frame_data[ctx.frame_index];
                            GridPushConstants pc{};
                            pc.frame = (u64)ctx.render_data->frame_ubo_view[ctx.frame_index].slot;
                            {
                                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::draw(ctx.cmd, 3, 1, 0, 0);
                            rhi::end_rendering(ctx.cmd);
                        });
}

} // namespace grid