#include "lighting.hpp" #include "world.hpp" extern "C" { #include "memory.h" #include "plat.h" } /* needs to match surface shader */ struct GPU_Light { v3f dir; float brightness; v3f colour; float pad; }; void Lighting::init(Device* dev) { lights.init( dev, "Light buffer", max_lights * sizeof(GPU_Light), Buffer_Flags::storage_buffer ); } void Lighting::destroy(Device* dev) { lights.destroy(dev); } void Lighting::write_buf(void* ptr, World& w) { GPU_Light* dst = (GPU_Light*)ptr; int count = 0; for (auto v : w.view()) { GPU_Light gl; Sun_Light& l = v.get(); if (count >= max_lights) { print_war("Over light limit.\n"); return; } gl.brightness = l.brightness; gl.colour = l.colour; gl.dir = l.dir; dst[count++] = gl; } light_count = count; } void Lighting::update(Device* dev, Context& ctx, World& w) { light_count = 0; write_buf(lights.map(dev), w); lights.unmap(dev); lights.update(ctx); }