diff options
author | quou <quou@disroot.org> | 2024-12-21 21:25:22 +1100 |
---|---|---|
committer | quou <quou@disroot.org> | 2024-12-21 21:25:22 +1100 |
commit | 5bc8f90c38981045515bab04d26687f929f62ec1 (patch) | |
tree | d1d603ded306ae218e75f7eff4b69807f2a14e26 /c2.cpp | |
parent | 3283c6c8c32f980bd01591e441acd9c712c650ef (diff) |
render a triangle
Diffstat (limited to 'c2.cpp')
-rw-r--r-- | c2.cpp | 45 |
1 files changed, 41 insertions, 4 deletions
@@ -4,10 +4,17 @@ extern "C" { #include "plat.h" #include "memory.h" } +#include <string.h> #define video_arena_size (1024 * 1024 * 16) #define per_frame_memory_size (1024 * 1024) +static float verts[] = { + 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, + -0.5f, 0.5f, 0.0f, 1.0f, 0.0f, + 0.0f, -0.5f, 0.0f, 0.0f, 1.0f +}; + struct C2 : public App { Device* dev; void on_resize() override { @@ -18,7 +25,8 @@ struct C2 : public App { int main() { Arena video_arena; Device* dev; - Shader* shader; + Shader_Id shader; + Buffer_Id vbo; C2* app = App::create<C2>("c2"); void* per_frame; app->running = 1; @@ -30,12 +38,23 @@ int main() { dev = Device::create(&video_arena, app); app->dev = dev; shader = dev->load_shader("data/triangle.csh"); - per_frame = heap_alloc( dev->heap, per_frame_memory_size ); + assert(per_frame != 0); uint8_t r = 0; + vbo = dev->create_buffer( + sizeof verts, + Buffer_Flags::vertex_buffer | + Buffer_Flags::cpu_readwrite + ); + { + void* mem; + mem = dev->map_buffer(vbo, 0, sizeof verts); + memcpy(mem, verts, sizeof verts); + dev->unmap_buffer(vbo); + } while (app->running) { Arena frame_arena; init_arena(&frame_arena, per_frame, per_frame_memory_size); @@ -48,13 +67,31 @@ int main() { pb.rp_target(dev->get_backbuffer(), { r, 0x00, 0xff, 0xff }); Render_Pass& pass = pb.build_rp(); - dev->get_ctx().submit(*dev, pass); + pb.begin(); + pb.shader(shader); + pb.vertex_format(dev->get_shader(shader).vf); + Pipeline& pip = pb.build(); + + Vertex_Buffer_Binding binding[] = {{ + .id = vbo, + .offset = 0, + .target = dev->get_shader(shader).binding_index("verts") + }, {}}; + + Draw draw{}; + draw.verts = binding; + draw.vertex_count = 3; + draw.instance_count = 1; + + dev->get_ctx().submit(*dev, draw, pip, pass); + /* dev->get_ctx().submit(*dev, pass);*/ r += 10; dev->present(); app->end(); } - shader->destroy(dev); + dev->destroy_buffer(vbo); + dev->destroy_shader(shader); dev->destroy(); app->destroy(); } |