summaryrefslogtreecommitdiff
path: root/lighting.cpp
diff options
context:
space:
mode:
authorquou <quou@disroot.org>2025-02-09 18:50:58 +1100
committerquou <quou@disroot.org>2025-02-09 18:51:30 +1100
commit629dc808c595d65cda74a86975ebd780113f3431 (patch)
tree72d0c17600c6420fc2b834529d781dc43cc20317 /lighting.cpp
parent1c86bb51da99df1950124d812eabcfe15af4f771 (diff)
Properly send lights from the CPU
Diffstat (limited to 'lighting.cpp')
-rw-r--r--lighting.cpp54
1 files changed, 54 insertions, 0 deletions
diff --git a/lighting.cpp b/lighting.cpp
new file mode 100644
index 0000000..d78ef6e
--- /dev/null
+++ b/lighting.cpp
@@ -0,0 +1,54 @@
+#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<Sun_Light>()) {
+ GPU_Light gl;
+ Sun_Light& l = v.get<Sun_Light>();
+ 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);
+}
+