From 7fa078995b3ed62925788ce2460441bc3d373392 Mon Sep 17 00:00:00 2001 From: quou Date: Thu, 26 Dec 2024 22:06:57 +1100 Subject: constant buffers --- c2.cpp | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) (limited to 'c2.cpp') diff --git a/c2.cpp b/c2.cpp index 3b46920..052ebf0 100644 --- a/c2.cpp +++ b/c2.cpp @@ -5,6 +5,7 @@ extern "C" { #include "memory.h" } #include +#include #define video_arena_size (1024 * 1024 * 16) #define asset_arena_size (1024 * 1024 * 4) @@ -58,6 +59,10 @@ static Sampler_Id create_clamped_linear(Device* dev) { return dev->create_sampler(s); } +struct Config_Buffer { + float offset[2]; +}; + int main() { Arena video_arena; Arena asset_arena; @@ -66,7 +71,7 @@ int main() { Shader* shader; Texture* texture; Texture* texture2; - Buffer_Id vbo; + Buffer_Id vbo, cbuf; Sampler_Id clamped_linear; C2* app = App::create("c2"); void* per_frame; @@ -88,6 +93,11 @@ int main() { shader = (Shader*)assets.load("triangle.csh"); texture = (Texture*)assets.load("22.tex"); texture2 = (Texture*)assets.load("kita.tex"); + cbuf = dev->create_buffer( + sizeof verts, + Buffer_Flags::constant_buffer | + Buffer_Flags::cpu_readwrite + ); per_frame = heap_alloc( dev->heap, per_frame_memory_size @@ -103,6 +113,15 @@ int main() { app->begin(); dev->begin_frame(); + { + void* mem; + mem = dev->map_buffer(cbuf, 0, sizeof(Config_Buffer)); + Config_Buffer* c = (Config_Buffer*)mem; + c->offset[0] = sinf((float)frame / 10.0f); + c->offset[1] = cosf((float)frame / 30.0f); + dev->unmap_buffer(cbuf); + } + Pipeline_Builder pb(&frame_arena); pb.begin_rp(); pb.rp_target(dev->get_backbuffer(), { r, 0x00, 0xff, 0xff }); @@ -112,10 +131,14 @@ int main() { pb.shader(shader->id); pb.vertex_format(shader->vf); pb.texture( - shader->descriptor_index("colour_texture"), + shader->descriptor_binding("colour_texture"), frame % 2? texture->id: texture2->id, clamped_linear ); + pb.cbuffer( + shader->descriptor_binding("config_buffer"), + cbuf + ); Pipeline& pip = pb.build(); Vertex_Buffer_Binding binding[] = {{ @@ -139,6 +162,7 @@ int main() { assets.destroy(); dev->destroy_sampler(clamped_linear); dev->destroy_buffer(vbo); + dev->destroy_buffer(cbuf); dev->destroy(); app->destroy(); } -- cgit v1.2.3-54-g00ecf