summaryrefslogtreecommitdiff
path: root/lighting.cpp
blob: 0e41dddffaf46b0cd630f3c562300c5b0e7851c8 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "lighting.hpp"
#include "model.hpp"
#include "renderer.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;
	int caster_id;
};

struct GPU_Caster {
	m4f projection;
};

void Lighting::init(Device* dev) {
	int i;
	Sampler_State ss{};
	lights.init(
		dev,
		"Light buffer",
		max_lights * sizeof(GPU_Light),
		Buffer_Flags::storage_buffer
	);
	casters.init(
		dev,
		"Caster buffer",
		max_shadows * sizeof(GPU_Caster),
		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++) {
		cameras[i] = 0;
		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
		);
	}
	ss.min = Filter_Mode::linear;
	ss.mag = Filter_Mode::linear;
	ss.mip = Filter_Mode::linear;
	ss.address_u = Address_Mode::border;
	ss.address_v = Address_Mode::border;
	ss.border[0] = 1.0f;
	ss.border[1] = 1.0f;
	ss.border[2] = 1.0f;
	ss.border[3] = 1.0f;
	ss.compare = Depth_Mode::less;
	shadow_sampler = dev->create_sampler("shadow sampler", ss);
}

void Lighting::destroy(Device* dev, Renderer& r) {
	int i;
	lights.destroy(dev);
	casters.destroy(dev);
	dev->destroy_sampler(shadow_sampler);
	for (i = 0; i < max_shadows; i++) {
		dev->destroy_texture(shadow_slices[i]);
		r.destroy_camera(cameras[i]);
	}
	dev->destroy_texture(shadows);
}

void Lighting::write_bufs(
	void* lptr,
	void* cptr,
	World& w,
	Renderer& r,
	Model_Scene& s
) {
	GPU_Light* ldst  = (GPU_Light*)lptr;
	GPU_Caster* cdst = (GPU_Caster*)cptr;
	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;
		if (l.caster && ccount < max_shadows) {
			int cid = ccount++;
			GPU_Caster& c = cdst[cid];
			Camera_Id camid = cameras[cid];
			if (!camid)
				camid = r.create_camera();
			Camera& cam = r.get_camera(camid);
			cam.init_shadow(l.dir, s.bound.min, s.bound.max);
			c.projection = cam.get_proj() * cam.get_view();
			cameras[cid] = camid;
			gl.caster_id = cid;
		} else
			gl.caster_id = -1;
		ldst[count++] = gl;
	}
	light_count = count;
	caster_count = ccount;
}

void Lighting::update(
	Device* dev,
	Context& ctx,
	World& w,
	Renderer& r,
	Model_Scene& s
) {
	light_count = 0;
	write_bufs(lights.map(dev), casters.map(dev), w, r, s);
	casters.unmap(dev);
	lights.unmap(dev);
	lights.update(ctx);
	casters.update(ctx);
}