summaryrefslogtreecommitdiff
path: root/lighting.cpp
blob: d78ef6e1a27ead2168ebb2feeb94519ff1444a35 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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);
}