summaryrefslogtreecommitdiff
path: root/c2.cpp
diff options
context:
space:
mode:
authorquou <quou@disroot.org>2025-01-04 18:41:08 +1100
committerquou <quou@disroot.org>2025-01-04 18:41:57 +1100
commit3b5a6b7665092aef6eb0cb30e7da25b6dbe8b178 (patch)
tree3c261a5fd7df40174e1cfe31d941a532cccd37bf /c2.cpp
parentcf4c007acc6bce0aa702a39b4675ecb879b911ed (diff)
render a skybox
Diffstat (limited to 'c2.cpp')
-rw-r--r--c2.cpp122
1 files changed, 122 insertions, 0 deletions
diff --git a/c2.cpp b/c2.cpp
index e721190..32a0d41 100644
--- a/c2.cpp
+++ b/c2.cpp
@@ -156,6 +156,110 @@ struct Orbit_Cam : public Camera {
}
};
+struct Sky {
+ Staged_Buffer config;
+ Buffer_Id vb;
+ Texture_Id texture;
+ Shader* shader;
+ int vert_binding;
+ int picture_binding;
+ int cfg_binding;
+
+ struct Cbuffer {
+ m4f iview;
+ m4f iprojection;
+ };
+
+ void init(Device* d, Asset_Arena* assets) {
+ Buffer_Id stage;
+ void* mem;
+ Context& ctx = d->acquire();
+ float verts[] = {
+ -1.0f, -1.0f, 0.0f, 0.0f,
+ -1.0f, 3.0f, 0.0f, 2.0f,
+ 3.0f, -1.0f, 2.0f, 0.0f
+ };
+ Texture* tex = (Texture*)assets->load("sky.tex");
+ shader = (Shader*)assets->load("sky.csh");
+ texture = tex->id;
+ stage = d->create_buffer(
+ "sky vb stage",
+ sizeof verts,
+ Buffer_Flags::cpu_readwrite |
+ Buffer_Flags::copy_src
+ );
+ mem = d->map_buffer(stage, 0, sizeof verts);
+ memcpy(mem, verts, sizeof verts);
+ d->unmap_buffer(stage);
+ vb = d->create_buffer(
+ "sky vb",
+ sizeof verts,
+ Buffer_Flags::copy_dst |
+ Buffer_Flags::vertex_buffer
+ );
+ ctx.copy(vb, stage);
+ d->submit(ctx);
+ d->destroy_bufferi(stage);
+ assert(shader != 0);
+ vert_binding = shader->binding_index("verts");
+ assert(vert_binding >= 0);
+ picture_binding = shader->descriptor_binding("picture");
+ assert(picture_binding >= 0);
+ cfg_binding = shader->descriptor_binding("config_buffer");
+ assert(cfg_binding >= 0);
+ config.init(
+ d,
+ "Sky Cbuffer",
+ sizeof(Cbuffer),
+ Buffer_Flags::constant_buffer
+ );
+ }
+
+ void destroy(Device* d) {
+ d->destroy_buffer(vb);
+ config.destroy(d);
+ }
+
+ void render(
+ Device* d,
+ Arena* a,
+ Render_Pass& pass,
+ Sampler_Id sampler,
+ const Camera& cam
+ ) {
+ Cbuffer* cb = (Cbuffer*)config.map(d);
+ cb->iview = cam.get_view().inverse();
+ cb->iprojection = cam.get_proj().inverse();
+ config.unmap(d);
+ Context& ctx = d->get_ctx();
+ Pipeline_Builder pb(a, d);
+ pb.begin();
+ pb.shader(shader->id);
+ pb.vertex_format(shader->vf);
+ pb.texture(
+ picture_binding,
+ texture,
+ sampler
+ );
+ pb.cbuffer(cfg_binding, config.gpuonly);
+ pb.depth(true, false, Depth_Mode::equal);
+ Pipeline& pip = pb.build();
+
+ Vertex_Buffer_Binding vbb[] = {{
+ .id = vb,
+ .offset = 0,
+ .target = vert_binding
+ }, {}};
+
+ Draw draw{};
+ draw.verts = vbb;
+ draw.vertex_count = 3;
+ draw.instance_count = 1;
+ config.update(ctx);
+ ctx.submit(draw, pip, pass);
+ }
+};
+
struct Config_Buffer {
float offset[2];
};
@@ -178,6 +282,7 @@ extern "C" int entrypoint() {
Model_Instance* monkey, * monkey2;
Model_Scene scene;
Orbit_Cam camera;
+ Sky sky;
Buffer_Id vbo, cbuf;
Sampler_Id clamped_linear;
C2* app = App::create<C2>("c2");
@@ -241,6 +346,7 @@ extern "C" int entrypoint() {
dev,
(Model*)assets.load("monkey.mdl")
);
+ sky.init(dev, &assets);
camera.init();
while (app->running) {
Arena frame_arena;
@@ -314,6 +420,11 @@ extern "C" int entrypoint() {
pb.rp_depth_target(dev->get_depth_target(), Clear_Mode::restore);
Render_Pass& pass2 = pb.build_rp();
+ pb.begin_rp();
+ pb.rp_target(dev->get_backbuffer(), Clear_Mode::restore);
+ pb.rp_depth_target(dev->get_depth_target(), Clear_Mode::restore);
+ Render_Pass& sky_pass = pb.build_rp();
+
Texture& bb = dev->get_texture(dev->get_backbuffer());
monkey->transform = m4f::translate(
m4f::identity(),
@@ -343,6 +454,16 @@ extern "C" int entrypoint() {
ctx.debug_pop();
ctx.debug_pop();
+ ctx.debug_push("sky");
+ sky.render(
+ dev,
+ &frame_arena,
+ sky_pass,
+ clamped_linear,
+ camera
+ );
+ ctx.debug_pop();
+
ctx.debug_push("ui");
ui->render(&frame_arena, dev->get_backbuffer());
ctx.debug_pop();
@@ -354,6 +475,7 @@ extern "C" int entrypoint() {
app->end();
}
scene.destroy(dev);
+ sky.destroy(dev);
ui->destroy();
assets.destroy();
dev->destroy_texture(default_texture);