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
|
/*************************************************************************/
/* light_cluster_builder.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 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. */
/*************************************************************************/
#include "light_cluster_builder.h"
void LightClusterBuilder::begin(const Transform &p_view_transform, const CameraMatrix &p_cam_projection) {
view_xform = p_view_transform;
projection = p_cam_projection;
z_near = -projection.get_z_near();
z_far = -projection.get_z_far();
//reset counts
light_count = 0;
refprobe_count = 0;
decal_count = 0;
item_count = 0;
sort_id_count = 0;
}
void LightClusterBuilder::bake_cluster() {
float slice_depth = (z_near - z_far) / depth;
uint8_t *cluster_dataw = cluster_data.ptrw();
Cell *cluster_data_ptr = (Cell *)cluster_dataw;
//clear the cluster
zeromem(cluster_data_ptr, (width * height * depth * sizeof(Cell)));
/* Step 1, create cell positions and count them */
for (uint32_t i = 0; i < item_count; i++) {
const Item &item = items[i];
int from_slice = Math::floor((z_near - (item.aabb.position.z + item.aabb.size.z)) / slice_depth);
int to_slice = Math::floor((z_near - item.aabb.position.z) / slice_depth);
if (from_slice >= (int)depth || to_slice < 0) {
continue; //sorry no go
}
from_slice = MAX(0, from_slice);
to_slice = MIN((int)depth - 1, to_slice);
for (int j = from_slice; j <= to_slice; j++) {
Vector3 min = item.aabb.position;
Vector3 max = item.aabb.position + item.aabb.size;
float limit_near = MIN((z_near - slice_depth * j), max.z);
float limit_far = MAX((z_near - slice_depth * (j + 1)), min.z);
max.z = limit_near;
min.z = limit_near;
Vector3 proj_min = projection.xform(min);
Vector3 proj_max = projection.xform(max);
int near_from_x = int(Math::floor((proj_min.x * 0.5 + 0.5) * width));
int near_from_y = int(Math::floor((-proj_max.y * 0.5 + 0.5) * height));
int near_to_x = int(Math::floor((proj_max.x * 0.5 + 0.5) * width));
int near_to_y = int(Math::floor((-proj_min.y * 0.5 + 0.5) * height));
max.z = limit_far;
min.z = limit_far;
proj_min = projection.xform(min);
proj_max = projection.xform(max);
int far_from_x = int(Math::floor((proj_min.x * 0.5 + 0.5) * width));
int far_from_y = int(Math::floor((-proj_max.y * 0.5 + 0.5) * height));
int far_to_x = int(Math::floor((proj_max.x * 0.5 + 0.5) * width));
int far_to_y = int(Math::floor((-proj_min.y * 0.5 + 0.5) * height));
//print_line(itos(j) + " near - " + Vector2i(near_from_x, near_from_y) + " -> " + Vector2i(near_to_x, near_to_y));
//print_line(itos(j) + " far - " + Vector2i(far_from_x, far_from_y) + " -> " + Vector2i(far_to_x, far_to_y));
int from_x = MIN(near_from_x, far_from_x);
int from_y = MIN(near_from_y, far_from_y);
int to_x = MAX(near_to_x, far_to_x);
int to_y = MAX(near_to_y, far_to_y);
if (from_x >= (int)width || to_x < 0 || from_y >= (int)height || to_y < 0) {
continue;
}
int sx = MAX(0, from_x);
int sy = MAX(0, from_y);
int dx = MIN((int)width - 1, to_x);
int dy = MIN((int)height - 1, to_y);
//print_line(itos(j) + " - " + Vector2i(sx, sy) + " -> " + Vector2i(dx, dy));
for (int x = sx; x <= dx; x++) {
for (int y = sy; y <= dy; y++) {
uint32_t offset = j * (width * height) + y * width + x;
if (unlikely(sort_id_count == sort_id_max)) {
sort_id_max = nearest_power_of_2_templated(sort_id_max + 1);
sort_ids = (SortID *)memrealloc(sort_ids, sizeof(SortID) * sort_id_max);
if (ids.size()) {
ids.resize(sort_id_max);
RD::get_singleton()->free(items_buffer);
items_buffer = RD::get_singleton()->storage_buffer_create(sizeof(uint32_t) * sort_id_max);
}
}
sort_ids[sort_id_count].cell_index = offset;
sort_ids[sort_id_count].item_index = item.index;
sort_ids[sort_id_count].item_type = item.type;
sort_id_count++;
//for now, only count
cluster_data_ptr[offset].item_pointers[item.type]++;
//print_line("at offset " + itos(offset) + " value: " + itos(cluster_data_ptr[offset].item_pointers[item.type]));
}
}
}
}
/* Step 2, Assign pointers (and reset counters) */
uint32_t offset = 0;
for (uint32_t i = 0; i < (width * height * depth); i++) {
for (int j = 0; j < ITEM_TYPE_MAX; j++) {
uint32_t count = cluster_data_ptr[i].item_pointers[j]; //save count
cluster_data_ptr[i].item_pointers[j] = offset; //replace count by pointer
offset += count; //increase offset by count;
}
}
//print_line("offset: " + itos(offset));
/* Step 3, Place item lists */
uint32_t *ids_ptr = ids.ptrw();
for (uint32_t i = 0; i < sort_id_count; i++) {
const SortID &id = sort_ids[i];
Cell &cell = cluster_data_ptr[id.cell_index];
uint32_t pointer = cell.item_pointers[id.item_type] & POINTER_MASK;
uint32_t counter = cell.item_pointers[id.item_type] >> COUNTER_SHIFT;
ids_ptr[pointer + counter] = id.item_index;
cell.item_pointers[id.item_type] = pointer | ((counter + 1) << COUNTER_SHIFT);
}
RD::get_singleton()->texture_update(cluster_texture, 0, cluster_data, true);
RD::get_singleton()->buffer_update(items_buffer, 0, offset * sizeof(uint32_t), ids_ptr, true);
}
void LightClusterBuilder::setup(uint32_t p_width, uint32_t p_height, uint32_t p_depth) {
if (width == p_width && height == p_height && depth == p_depth) {
return;
}
if (cluster_texture.is_valid()) {
RD::get_singleton()->free(cluster_texture);
}
width = p_width;
height = p_height;
depth = p_depth;
cluster_data.resize(width * height * depth * sizeof(Cell));
{
RD::TextureFormat tf;
tf.format = RD::DATA_FORMAT_R32G32B32A32_UINT;
tf.texture_type = RD::TEXTURE_TYPE_3D;
tf.width = width;
tf.height = height;
tf.depth = depth;
tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
cluster_texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
}
}
RID LightClusterBuilder::get_cluster_texture() const {
return cluster_texture;
}
RID LightClusterBuilder::get_cluster_indices_buffer() const {
return items_buffer;
}
LightClusterBuilder::LightClusterBuilder() {
//initialize accumulators to something
lights = (LightData *)memalloc(sizeof(LightData) * 1024);
light_max = 1024;
refprobes = (OrientedBoxData *)memalloc(sizeof(OrientedBoxData) * 1024);
refprobe_max = 1024;
decals = (OrientedBoxData *)memalloc(sizeof(OrientedBoxData) * 1024);
decal_max = 1024;
items = (Item *)memalloc(sizeof(Item) * 1024);
item_max = 1024;
sort_ids = (SortID *)memalloc(sizeof(SortID) * 1024);
ids.resize(2014);
items_buffer = RD::get_singleton()->storage_buffer_create(sizeof(uint32_t) * 1024);
item_max = 1024;
}
LightClusterBuilder::~LightClusterBuilder() {
if (cluster_data.size()) {
RD::get_singleton()->free(cluster_texture);
}
if (lights) {
memfree(lights);
}
if (refprobes) {
memfree(refprobes);
}
if (decals) {
memfree(decals);
}
if (items) {
memfree(items);
}
if (sort_ids) {
memfree(sort_ids);
RD::get_singleton()->free(items_buffer);
}
}
|