summaryrefslogtreecommitdiff
path: root/lighting.cpp
blob: 75ae7c2fdbdf3f1a7aad0e62e02b860c75e12236 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "lighting.hpp"
#include "model.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) {
	int i;
	lights.init(
		dev,
		"Light buffer",
		max_lights * sizeof(GPU_Light),
		Buffer_Flags::storage_buffer
	);
	shadows = dev->create_texture(
		"Shadowmap Array",
		texture_format_d16,
		Texture_Flags::sampleable | Texture_Flags::depth_stencil_target,
		shadow_res,
		shadow_res,
		1,
		1,
		max_shadows,
		0
	);
	for (i = 0; i < max_shadows; i++) {
		shadow_slices[i] = dev->alias_texture(
			shadows,
			"Shadowmap Slice",
			texture_format_d16,
			Texture_Flags::depth_stencil_target,
			shadow_res,
			shadow_res,
			1,
			1,
			1,
			0,
			i
		);
	}
}

void Lighting::destroy(Device* dev) {
	int i;
	lights.destroy(dev);
	for (i = 0; i < max_shadows; i++)
		dev->destroy_texture(shadow_slices[i]);
	dev->destroy_texture(shadows);
}

void Lighting::write_buf(void* ptr, World& w) {
	GPU_Light* dst = (GPU_Light*)ptr;
	int count = 0, ccount = 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;
		if (l.caster) {
			Caster& c = casters[ccount++];
			c.vp = m4f::identity();
		}
	}
	light_count = count;
	caster_count = ccount;
}

void Lighting::update(Device* dev, Context& ctx, World& w) {
	light_count = 0;
	write_buf(lights.map(dev), w);
	lights.unmap(dev);
	lights.update(ctx);
}