File: vertex_cache_optimizer.cpp

package info (click to toggle)
godot 3.6%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 270,588 kB
  • sloc: cpp: 971,579; ansic: 617,953; xml: 80,302; asm: 17,498; cs: 14,559; python: 11,744; java: 9,681; javascript: 4,654; pascal: 1,176; sh: 896; objc: 529; makefile: 176
file content (308 lines) | stat: -rw-r--r-- 11,261 bytes parent folder | download | duplicates (2)
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/**************************************************************************/
/*  vertex_cache_optimizer.cpp                                            */
/**************************************************************************/
/*                         This file is part of:                          */
/*                             GODOT ENGINE                               */
/*                        https://godotengine.org                         */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */
/*                                                                        */
/* 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 "vertex_cache_optimizer.h"

#include "core/math/geometry.h"
#include "core/math/math_funcs.h"

// Precalculate the tables.
void VertexCacheOptimizer::init() {
	for (int i = 0; i < Constants::CACHE_SCORE_TABLE_SIZE; i++) {
		float score = 0;
		if (i < 3) {
			// This vertex was used in the last triangle,
			// so it has a fixed score, which ever of the three
			// it's in. Otherwise, you can get very different
			// answers depending on whether you add
			// the triangle 1,2,3 or 3,1,2 - which is silly.
			score = Constants::LAST_TRI_SCORE;
		} else {
			// Points for being high in the cache.
			const float scaler = 1.0f / (Constants::CACHE_FUNCTION_LENGTH - 3);
			score = 1.0f - (i - 3) * scaler;
			score = Math::pow(score, Constants::CACHE_DECAY_POWER);
		}
		_cache_position_score[i] = (SCORE_TYPE)(Constants::SCORE_SCALING * score);
	}

	for (int i = 1; i < Constants::VALENCE_SCORE_TABLE_SIZE; i++) {
		// Bonus points for having a low number of tris still to
		// use the vert, so we get rid of lone verts quickly.
		float valence_boost = Math::pow(i, -Constants::VALENCE_BOOST_POWER);
		float score = Constants::VALENCE_BOOST_SCALE * valence_boost;
		_valence_score[i] = (SCORE_TYPE)(Constants::SCORE_SCALING * score);
	}
}

VertexCacheOptimizer::SCORE_TYPE VertexCacheOptimizer::find_vertex_score(int p_num_active_tris, int p_cache_position) {
	if (p_num_active_tris == 0) {
		// No triangles need this vertex!
		return 0;
	}

	SCORE_TYPE score = 0;
	if (p_cache_position < 0) {
		// Vertex is not in LRU cache - no score.
	} else {
		score = _cache_position_score[p_cache_position];
	}

	if (p_num_active_tris < Constants::VALENCE_SCORE_TABLE_SIZE) {
		score += _valence_score[p_num_active_tris];
	}
	return score;
}

VertexCacheOptimizer::VERTEX_INDEX_TYPE *VertexCacheOptimizer::_reorder_indices(VERTEX_INDEX_TYPE *r_dest_indices, const VERTEX_INDEX_TYPE *p_source_indices, int p_num_triangles, int p_num_vertices) {
	ADJACENCY_TYPE *num_active_tris = (ADJACENCY_TYPE *)memalloc(sizeof(ADJACENCY_TYPE) * p_num_vertices);
	memset(num_active_tris, 0, sizeof(ADJACENCY_TYPE) * p_num_vertices);

	// First scan over the vertex data, count the total number of
	// occurrances of each vertex.
	for (int i = 0; i < 3 * p_num_triangles; i++) {
		if (num_active_tris[p_source_indices[i]] == Constants::MAX_ADJACENCY) {
			// Unsupported mesh,
			// vertex shared by too many triangles.
			memfree(num_active_tris);
			return nullptr;
		}
		num_active_tris[p_source_indices[i]]++;
	}

	// Allocate the rest of the arrays.
	ARRAY_INDEX_TYPE *offsets = (ARRAY_INDEX_TYPE *)memalloc(sizeof(ARRAY_INDEX_TYPE) * p_num_vertices);
	SCORE_TYPE *last_score = (SCORE_TYPE *)memalloc(sizeof(SCORE_TYPE) * p_num_vertices);
	CACHE_POS_TYPE *cache_tag = (CACHE_POS_TYPE *)memalloc(sizeof(CACHE_POS_TYPE) * p_num_vertices);

	uint8_t *triangle_added = (uint8_t *)memalloc((p_num_triangles + 7) / 8);
	SCORE_TYPE *triangle_score = (SCORE_TYPE *)memalloc(sizeof(SCORE_TYPE) * p_num_triangles);
	TRIANGLE_INDEX_TYPE *triangle_indices = (TRIANGLE_INDEX_TYPE *)memalloc(sizeof(TRIANGLE_INDEX_TYPE) * 3 * p_num_triangles);
	memset(triangle_added, 0, sizeof(uint8_t) * ((p_num_triangles + 7) / 8));
	memset(triangle_score, 0, sizeof(SCORE_TYPE) * p_num_triangles);
	memset(triangle_indices, 0, sizeof(TRIANGLE_INDEX_TYPE) * 3 * p_num_triangles);

	// Count the triangle array offset for each vertex,
	// initialize the rest of the data.
	int sum = 0;
	for (int i = 0; i < p_num_vertices; i++) {
		offsets[i] = sum;
		sum += num_active_tris[i];
		num_active_tris[i] = 0;
		cache_tag[i] = -1;
	}

	// Fill the vertex data structures with indices to the triangles
	// using each vertex.
	for (int i = 0; i < p_num_triangles; i++) {
		for (int j = 0; j < 3; j++) {
			int v = p_source_indices[3 * i + j];
			triangle_indices[offsets[v] + num_active_tris[v]] = i;
			num_active_tris[v]++;
		}
	}

	// Initialize the score for all vertices.
	for (int i = 0; i < p_num_vertices; i++) {
		last_score[i] = find_vertex_score(num_active_tris[i], cache_tag[i]);
		for (int j = 0; j < num_active_tris[i]; j++) {
			triangle_score[triangle_indices[offsets[i] + j]] += last_score[i];
		}
	}

	// Find the best triangle.
	int best_triangle = -1;
	int best_score = -1;

	for (int i = 0; i < p_num_triangles; i++) {
		if (triangle_score[i] > best_score) {
			best_score = triangle_score[i];
			best_triangle = i;
		}
	}

	// Allocate the output array.
	TRIANGLE_INDEX_TYPE *out_triangles = (TRIANGLE_INDEX_TYPE *)memalloc(sizeof(TRIANGLE_INDEX_TYPE) * p_num_triangles);
	int out_pos = 0;

	// Initialize the cache.
	int cache[Constants::VERTEX_CACHE_SIZE + 3];
	for (int i = 0; i < Constants::VERTEX_CACHE_SIZE + 3; i++) {
		cache[i] = -1;
	}

	int scan_pos = 0;

	// Output the currently best triangle, as long as there
	// are triangles left to output.
	while (best_triangle >= 0) {
		// Mark the triangle as added.
		set_added(triangle_added, best_triangle);
		// Output this triangle.
		out_triangles[out_pos++] = best_triangle;
		for (int i = 0; i < 3; i++) {
			// Update this vertex.
			int v = p_source_indices[3 * best_triangle + i];

			// Check the current cache position, if it
			// is in the cache.
			int endpos = cache_tag[v];
			if (endpos < 0) {
				endpos = Constants::VERTEX_CACHE_SIZE + i;
			}
			if (endpos > i) {
				// Move all cache entries from the previous position
				// in the cache to the new target position (i) one
				// step backwards.
				for (int j = endpos; j > i; j--) {
					cache[j] = cache[j - 1];
					// If this cache slot contains a real
					// vertex, update its cache tag.
					if (cache[j] >= 0) {
						cache_tag[cache[j]]++;
					}
				}
				// Insert the current vertex into its new target
				// slot.
				cache[i] = v;
				cache_tag[v] = i;
			}

			// Find the current triangle in the list of active
			// triangles and remove it (moving the last
			// triangle in the list to the slot of this triangle).
			for (int j = 0; j < num_active_tris[v]; j++) {
				if (triangle_indices[offsets[v] + j] == best_triangle) {
					triangle_indices[offsets[v] + j] = triangle_indices[offsets[v] + num_active_tris[v] - 1];
					break;
				}
			}
			// Shorten the list.
			num_active_tris[v]--;
		}
		// Update the scores of all triangles in the cache.
		for (int i = 0; i < Constants::VERTEX_CACHE_SIZE + 3; i++) {
			int v = cache[i];
			if (v < 0) {
				break;
			}
			// This vertex has been pushed outside of the
			// actual cache.
			if (i >= Constants::VERTEX_CACHE_SIZE) {
				cache_tag[v] = -1;
				cache[i] = -1;
			}
			SCORE_TYPE newScore = find_vertex_score(num_active_tris[v], cache_tag[v]);
			SCORE_TYPE diff = newScore - last_score[v];
			for (int j = 0; j < num_active_tris[v]; j++) {
				triangle_score[triangle_indices[offsets[v] + j]] += diff;
			}
			last_score[v] = newScore;
		}
		// Find the best triangle referenced by vertices in the cache.
		best_triangle = -1;
		best_score = -1;
		for (int i = 0; i < Constants::VERTEX_CACHE_SIZE; i++) {
			if (cache[i] < 0) {
				break;
			}
			int v = cache[i];
			for (int j = 0; j < num_active_tris[v]; j++) {
				int t = triangle_indices[offsets[v] + j];
				if (triangle_score[t] > best_score) {
					best_triangle = t;
					best_score = triangle_score[t];
				}
			}
		}
		// If no active triangle was found at all, continue
		// scanning the whole list of triangles.
		if (best_triangle < 0) {
			for (; scan_pos < p_num_triangles; scan_pos++) {
				if (!is_added(triangle_added, scan_pos)) {
					best_triangle = scan_pos;
					break;
				}
			}
		}
	}

	// Convert the triangle index array into a full triangle list.
	out_pos = 0;
	for (int i = 0; i < p_num_triangles; i++) {
		int t = out_triangles[i];
		for (int j = 0; j < 3; j++) {
			int v = p_source_indices[3 * t + j];
			r_dest_indices[out_pos++] = v;
		}
	}

	// Clean up.
	memfree(triangle_indices);
	memfree(offsets);
	memfree(last_score);
	memfree(num_active_tris);
	memfree(cache_tag);
	memfree(triangle_added);
	memfree(triangle_score);
	memfree(out_triangles);

	return r_dest_indices;
}

bool VertexCacheOptimizer::reorder_indices_pool(PoolVector<int> &r_indices, uint32_t p_num_triangles, uint32_t p_num_verts) {
	LocalVector<int> temp;
	temp = r_indices;
	if (reorder_indices(temp, p_num_triangles, p_num_verts)) {
		r_indices = temp;
		return true;
	}
	return false;
}

bool VertexCacheOptimizer::reorder_indices(LocalVector<int> &r_indices, uint32_t p_num_triangles, uint32_t p_num_verts) {
	// If the mesh contains invalid indices, abort.
	ERR_FAIL_COND_V(!Geometry::verify_indices(r_indices.ptr(), r_indices.size(), p_num_verts), false);

	LocalVector<int> temp;
	temp.resize(r_indices.size());
	if (_reorder_indices((VERTEX_INDEX_TYPE *)temp.ptr(), (VERTEX_INDEX_TYPE *)r_indices.ptr(), p_num_triangles, p_num_verts)) {
#if 0
		uint32_t show = MIN(r_indices.size(), 16);
		for (uint32_t n = 0; n < show; n++) {
			print_line(itos(n) + " : " + itos(r_indices[n]) + " to " + itos(temp[n]));
		}
#endif

		r_indices = temp;
		return true;
	}
	return false;
}