diff options
Diffstat (limited to 'c2.cpp')
-rw-r--r-- | c2.cpp | 175 |
1 files changed, 128 insertions, 47 deletions
@@ -21,9 +21,11 @@ extern "C" { #define video_arena_size (1024 * 1024 * 16) #define asset_arena_size (1024 * 1024 * 4) #define ui_arena_size (1024 * 1024) -#define scene_arena_size (1024 * 4) +#define scene_arena_size (1024 * 8) #define per_frame_memory_size (1024 * 1024) +#define MSAA_SAMPLES 4 + static float verts[] = { 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, -0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, @@ -58,33 +60,6 @@ static Buffer_Id upload_verts(Device* dev) { return vbo; } -Texture_Id make_default_texture(Device* dev) { - unsigned* mem; - Texture_Id tex; - Buffer_Id buf = dev->create_buffer( - "default texture stage", - 4, - Buffer_Flags::copy_src | - Buffer_Flags::cpu_readwrite - ); - mem = (unsigned*)dev->map_buffer(buf, 0, 4); - mem[0] = 0xffffffff; - dev->unmap_buffer(buf); - tex = dev->create_texture( - "default PBR texture", - texture_format_rgba8i, - Texture_Flags::sampleable | Texture_Flags::copy_dst, - 1, - 1, - 1, - 1, - 1, - buf - ); - dev->destroy_bufferi(buf); - return tex; -} - static Sampler_Id create_clamped_linear(Device* dev) { Sampler_State s{}; s.min = Filter_Mode::linear; @@ -162,6 +137,61 @@ struct Orbit_Cam { } }; +struct Fullscreen_Quad { + Buffer_Id vb; + + void init(Device* d) { + 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 + }; + Buffer_Id stage; + void* mem; + 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( + "fullscreen quad", + sizeof verts, + Buffer_Flags::copy_dst | + Buffer_Flags::vertex_buffer + ); + Context& ctx = d->acquire(); + ctx.copy(vb, stage); + d->submit(ctx); + d->destroy_bufferi(stage); + } + + void destroy(Device* d) { + d->destroy_buffer(vb); + } + + void render( + Context& ctx, + Pipeline& pip, + Render_Pass& pass, + int bind + ) { + Vertex_Buffer_Binding vbb[] = {{ + .id = vb, + .offset = 0, + .target = bind + }, {}}; + Draw draw{}; + draw.verts = vbb; + draw.vertex_count = 3; + draw.instance_count = 1; + ctx.submit(draw, pip, pass); + } +}; + struct Sky { Staged_Buffer config; Texture_Id texture; @@ -585,19 +615,19 @@ struct C2 : public App { Collider* box_col, * floor_col; Texture* texture; Texture* texture2; - Texture_Id default_texture; Entity_Id monkey, monkey2, box, floor; Model_Scene scene; Renderer renderer; Lighting lighting; Camera_Id camera; Orbit_Cam orbit_cam; + Fullscreen_Quad quad; Sky sky; Env_Probe eprobe; Tonemap tonemap; Buffer_Id vbo, cbuf; Sampler_Id clamped_linear; - Texture_Id hdr_target; + Texture_Id hdr_target, hdr_resolved, ms_depth; Texture_Id ui_texture; Buffer_Id ui_buffer; Line_Renderer lr; @@ -636,17 +666,17 @@ struct C2 : public App { ); assets.init(&asset_arena, "pack", 128); dev = Device::create(&video_arena, this); - default_texture = make_default_texture(dev); make_hdr_target(); make_ui_texture(); model_loader.init(dev, &assets); - mat_loader.init(&assets, default_texture); + mat_loader.init(&assets); register_asset_loader("MODL", &model_loader); register_asset_loader("MTRL", &mat_loader); shader = (Shader*)assets.load("triangle.csh"); ui_shader = (Shader*)assets.load("ui.csh"); texture = (Texture*)assets.load("22.tex"); texture2 = (Texture*)assets.load("kita.tex"); + quad.init(dev); cbuf = dev->create_buffer( "config buffer", sizeof(Config_Buffer), @@ -658,7 +688,7 @@ struct C2 : public App { per_frame_memory_size ); clamped_linear = create_clamped_linear(dev); - renderer.init(arena, dev, assets); + renderer.init(arena, dev); world = (World*)arena_alloc(arena, sizeof *world); world->init(arena); ui = UI::create(this, &ui_arena); @@ -669,7 +699,7 @@ struct C2 : public App { ui->layout(w, h); fps_label = ui->create_element<UI::Label>(ui->root, ""); scene.init(&scene_arena, 32, clamped_linear); - lighting.init(dev, w, h); + lighting.init(dev); sky.init(dev, &assets); eprobe.init(dev, &assets, 256); tonemap.init(dev, &assets); @@ -702,6 +732,24 @@ struct C2 : public App { light.caster = true; } { + auto l = world->create_entity(); + auto [t, light] = world->add<Transform, Point_Light>(l); + t.mat = m4f::translate(m4f::identity(), v3f(-3.0f, 1.0f, 0.0f)); + light.colour = v3f(1.0f, 0.0f, 0.0f); + light.brightness = 5.0f; + light.caster = true; + light.range = 5.0f; + } + { + auto l = world->create_entity(); + auto [t, light] = world->add<Transform, Point_Light>(l); + t.mat = m4f::translate(m4f::identity(), v3f(3.0f, 2.0f, 0.0f)); + light.colour = v3f(0.0f, 1.0f, 0.0f); + light.brightness = 5.0f; + light.caster = true; + light.range = 3.0f; + } + { box_col = make_box(&asset_arena, v3f(1.0f, 1.0f, 1.0f)); floor_col = make_box(&asset_arena, v3f(10.0f, 0.1f, 10.0f)); box = world->create_entity(); @@ -819,7 +867,7 @@ struct C2 : public App { pb.begin_rp(); pb.rp_target(hdr_target, Clear_Mode::restore); - pb.rp_depth_target(dev->get_depth_target(), Clear_Mode::restore); + pb.rp_depth_target(ms_depth, Clear_Mode::restore); Render_Pass& sky_pass = pb.build_rp(); pb.begin_rp(); @@ -827,7 +875,7 @@ struct C2 : public App { Render_Pass& tonemap_pass = pb.build_rp(); ctx.debug_push("environment cube"); - eprobe.render(dev, &frame_arena, renderer.quad, sky); + eprobe.render(dev, &frame_arena, quad, sky); ctx.debug_pop(); renderer.env_cubemap = eprobe.get_cubemap(); @@ -846,6 +894,7 @@ struct C2 : public App { dev, &frame_arena, hdr_target, + ms_depth, &lighting ); ctx.debug_pop(); @@ -854,7 +903,7 @@ struct C2 : public App { sky.render( dev, &frame_arena, - renderer.quad, + quad, sky_pass, clamped_linear, pcam, @@ -862,15 +911,17 @@ struct C2 : public App { ); ctx.debug_pop(); + ctx.resolve(hdr_resolved, hdr_target); + tonemap.update(dev, 0.2f); ctx.debug_push("TONEMAP"); tonemap.render( dev, &frame_arena, - renderer.quad, + quad, tonemap_pass, - hdr_target, + hdr_resolved, clamped_linear ); ctx.debug_pop(); @@ -902,7 +953,7 @@ struct C2 : public App { clamped_linear ); Pipeline& ui_pip = pb.build(); - renderer.quad.render( + quad.render( ctx, ui_pip, ui_pass, @@ -944,6 +995,7 @@ struct C2 : public App { } void on_destroy() override { + quad.destroy(dev); scene.destroy(dev); sky.destroy(dev); lighting.destroy(dev, renderer); @@ -954,8 +1006,9 @@ struct C2 : public App { ui->destroy(); deinit_editor(); assets.destroy(); - dev->destroy_texture(default_texture); dev->destroy_texture(hdr_target); + dev->destroy_texture(hdr_resolved); + dev->destroy_texture(ms_depth); dev->destroy_texture(ui_texture); dev->destroy_buffer(ui_buffer); dev->destroy_sampler(clamped_linear); @@ -967,25 +1020,53 @@ struct C2 : public App { void on_resize() override { ui->layout(w, h); dev->on_resize(); + dev->destroy_texture(ms_depth); dev->destroy_texture(hdr_target); + dev->destroy_texture(hdr_resolved); dev->destroy_texture(ui_texture); dev->destroy_buffer(ui_buffer); make_hdr_target(); make_ui_texture(); - lighting.recreate(dev, w, h); } void make_hdr_target() { + int sw = dev->swap_w(); + int sh = dev->swap_h(); hdr_target = dev->create_texture( - "HDR target", + "MS HDR target", texture_format_rgba16f, - Texture_Flags::sampleable | Texture_Flags::colour_target, - dev->swap_w(), - dev->swap_h(), + Texture_Flags::copy_src | Texture_Flags::colour_target, + sw, + sh, 1, 1, 1, - Buffer_Id(0) + Buffer_Id(0), + MSAA_SAMPLES + ); + hdr_resolved = dev->create_texture( + "Resolved HDR", + texture_format_rgba16f, + Texture_Flags::sampleable | Texture_Flags::copy_dst, + sw, + sh, + 1, + 1, + 1, + Buffer_Id(0), + 1 + ); + ms_depth = dev->create_texture( + "MS depth", + texture_format_d24s8, + Texture_Flags::sampleable | Texture_Flags::depth_stencil_target, + sw, + sh, + 1, + 1, + 1, + 0, + MSAA_SAMPLES ); } |