summaryrefslogtreecommitdiff
path: root/modules/lightmapper_rd/lightmapper_rd.h
blob: 6cb3ecbe9edd0bbdc974bc6ab51db446f6c88e04 (plain)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*************************************************************************/
/*  lightmapper_rd.h                                                     */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                      https://godotengine.org                          */
/*************************************************************************/
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
/*                                                                       */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the       */
/* "Software"), to deal in the Software without restriction, including   */
/* without limitation the rights to use, copy, modify, merge, publish,   */
/* distribute, sublicense, and/or sell copies of the Software, and to    */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions:                                             */
/*                                                                       */
/* The above copyright notice and this permission notice shall be        */
/* included in all copies or substantial portions of the Software.       */
/*                                                                       */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
/*************************************************************************/

#ifndef LIGHTMAPPER_RD_H
#define LIGHTMAPPER_RD_H

#include "core/local_vector.h"
#include "scene/3d/lightmapper.h"
#include "scene/resources/mesh.h"
#include "servers/rendering/rendering_device.h"

class LightmapperRD : public Lightmapper {
	GDCLASS(LightmapperRD, Lightmapper)

	struct MeshInstance {
		MeshData data;
		int slice = 0;
		Vector2i offset;
	};

	struct Light {
		float position[3];
		uint32_t type = LIGHT_TYPE_DIRECTIONAL;
		float direction[3];
		float energy;
		float color[3];
		float size;
		float range;
		float attenuation;
		float spot_angle;
		float spot_attenuation;
		uint32_t static_bake;
		uint32_t pad[3];

		bool operator<(const Light &p_light) const {
			return type < p_light.type;
		}
	};

	struct Vertex {
		float position[3];
		float normal_z;
		float uv[2];
		float normal_xy[2];

		bool operator==(const Vertex &p_vtx) const {
			return (position[0] == p_vtx.position[0]) &&
				   (position[1] == p_vtx.position[1]) &&
				   (position[2] == p_vtx.position[2]) &&
				   (uv[0] == p_vtx.uv[0]) &&
				   (uv[1] == p_vtx.uv[1]) &&
				   (normal_xy[0] == p_vtx.normal_xy[0]) &&
				   (normal_xy[1] == p_vtx.normal_xy[1]) &&
				   (normal_z == p_vtx.normal_z);
		}
	};

	struct Edge {
		Vector3 a;
		Vector3 b;
		Vector3 na;
		Vector3 nb;
		bool operator==(const Edge &p_seam) const {
			return a == p_seam.a && b == p_seam.b && na == p_seam.na && nb == p_seam.nb;
		}
		Edge() {
		}

		Edge(const Vector3 &p_a, const Vector3 &p_b, const Vector3 &p_na, const Vector3 &p_nb) {
			a = p_a;
			b = p_b;
			na = p_na;
			nb = p_nb;
		}
	};

	struct Probe {
		float position[4];
	};

	Vector<Probe> probe_positions;

	struct EdgeHash {
		_FORCE_INLINE_ static uint32_t hash(const Edge &p_edge) {
			uint32_t h = hash_djb2_one_float(p_edge.a.x);
			h = hash_djb2_one_float(p_edge.a.y, h);
			h = hash_djb2_one_float(p_edge.a.z, h);
			h = hash_djb2_one_float(p_edge.b.x, h);
			h = hash_djb2_one_float(p_edge.b.y, h);
			h = hash_djb2_one_float(p_edge.b.z, h);
			return h;
		}
	};
	struct EdgeUV2 {
		Vector2 a;
		Vector2 b;
		Vector2i indices;
		bool operator==(const EdgeUV2 &p_uv2) const {
			return a == p_uv2.a && b == p_uv2.b;
		}
		bool seam_found = false;
		EdgeUV2(Vector2 p_a, Vector2 p_b, Vector2i p_indices) {
			a = p_a;
			b = p_b;
			indices = p_indices;
		}
		EdgeUV2() {}
	};

	struct Seam {
		Vector2i a;
		Vector2i b;
		uint32_t slice;
		bool operator<(const Seam &p_seam) const {
			return slice < p_seam.slice;
		}
	};

	struct VertexHash {
		_FORCE_INLINE_ static uint32_t hash(const Vertex &p_vtx) {
			uint32_t h = hash_djb2_one_float(p_vtx.position[0]);
			h = hash_djb2_one_float(p_vtx.position[1], h);
			h = hash_djb2_one_float(p_vtx.position[2], h);
			h = hash_djb2_one_float(p_vtx.uv[0], h);
			h = hash_djb2_one_float(p_vtx.uv[1], h);
			h = hash_djb2_one_float(p_vtx.normal_xy[0], h);
			h = hash_djb2_one_float(p_vtx.normal_xy[1], h);
			h = hash_djb2_one_float(p_vtx.normal_z, h);
			return h;
		}
	};

	struct Box {
		float min_bounds[3];
		float pad0;
		float max_bounds[3];
		float pad1;
	};

	struct Triangle {
		uint32_t indices[3];
		uint32_t slice;
		bool operator<(const Triangle &p_triangle) const {
			return slice < p_triangle.slice;
		}
	};

	Vector<MeshInstance> mesh_instances;

	Vector<Light> lights;

	struct TriangleSort {
		uint32_t cell_index;
		uint32_t triangle_index;
		bool operator<(const TriangleSort &p_triangle_sort) const {
			return cell_index < p_triangle_sort.cell_index; //sorting by triangle index in this case makes no sense
		}
	};

	void _plot_triangle_into_triangle_index_list(int p_size, const Vector3i &p_ofs, const AABB &p_bounds, const Vector3 p_points[], uint32_t p_triangle_index, LocalVector<TriangleSort> &triangles, uint32_t p_grid_size);

	struct RasterPushConstant {
		float atlas_size[2];
		float uv_offset[2];
		float to_cell_size[3];
		uint32_t base_triangle;
		float to_cell_offset[3];
		float bias;
		int32_t grid_size[3];
		uint32_t pad2;
	};

	struct RasterSeamsPushConstant {
		uint32_t base_index;
		uint32_t slice;
		float uv_offset[2];
		uint32_t debug;
		float blend;
		uint32_t pad[2];
	};

	struct PushConstant {
		int32_t atlas_size[2];
		uint32_t ray_count;
		uint32_t ray_to;

		float world_size[3];
		float bias;

		float to_cell_offset[3];
		uint32_t ray_from;

		float to_cell_size[3];
		uint32_t light_count;

		int32_t grid_size;
		int32_t atlas_slice;
		int32_t region_ofs[2];

		float environment_xform[12];
	};

	Vector<Ref<Image>> bake_textures;
	Vector<Color> probe_values;

	BakeError _blit_meshes_into_atlas(int p_max_texture_size, Vector<Ref<Image>> &albedo_images, Vector<Ref<Image>> &emission_images, AABB &bounds, Size2i &atlas_size, int &atlas_slices, BakeStepFunc p_step_function, void *p_bake_userdata);
	void _create_acceleration_structures(RenderingDevice *rd, Size2i atlas_size, int atlas_slices, AABB &bounds, int grid_size, Vector<Probe> &probe_positions, GenerateProbes p_generate_probes, Vector<int> &slice_triangle_count, Vector<int> &slice_seam_count, RID &vertex_buffer, RID &triangle_buffer, RID &box_buffer, RID &lights_buffer, RID &triangle_cell_indices_buffer, RID &probe_positions_buffer, RID &grid_texture, RID &grid_texture_sdf, RID &seams_buffer, BakeStepFunc p_step_function, void *p_bake_userdata);
	void _raster_geometry(RenderingDevice *rd, Size2i atlas_size, int atlas_slices, int grid_size, AABB bounds, float p_bias, Vector<int> slice_triangle_count, RID position_tex, RID unocclude_tex, RID normal_tex, RID raster_depth_buffer, RID rasterize_shader, RID raster_base_uniform);

public:
	virtual void add_mesh(const MeshData &p_mesh);
	virtual void add_directional_light(bool p_static, const Vector3 &p_direction, const Color &p_color, float p_energy, float p_angular_distance);
	virtual void add_omni_light(bool p_static, const Vector3 &p_position, const Color &p_color, float p_energy, float p_range, float p_attenuation, float p_size);
	virtual void add_spot_light(bool p_static, const Vector3 &p_position, const Vector3 p_direction, const Color &p_color, float p_energy, float p_range, float p_attenuation, float p_spot_angle, float p_spot_attenuation, float p_size);
	virtual void add_probe(const Vector3 &p_position);
	virtual BakeError bake(BakeQuality p_quality, bool p_use_denoiser, int p_bounces, float p_bias, int p_max_texture_size, bool p_bake_sh, GenerateProbes p_generate_probes, const Ref<Image> &p_environment_panorama, const Basis &p_environment_transform, BakeStepFunc p_step_function = nullptr, void *p_bake_userdata = nullptr);

	int get_bake_texture_count() const;
	Ref<Image> get_bake_texture(int p_index) const;
	int get_bake_mesh_count() const;
	Variant get_bake_mesh_userdata(int p_index) const;
	Rect2 get_bake_mesh_uv_scale(int p_index) const;
	int get_bake_mesh_texture_slice(int p_index) const;
	int get_bake_probe_count() const;
	Vector3 get_bake_probe_point(int p_probe) const;
	Vector<Color> get_bake_probe_sh(int p_probe) const;

	LightmapperRD();
};

#endif // LIGHTMAPPER_H