forked from kittybupu/openVCB
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcSharpExportInterface.cpp
More file actions
481 lines (394 loc) · 13.1 KB
/
cSharpExportInterface.cpp
File metadata and controls
481 lines (394 loc) · 13.1 KB
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
// ReSharper disable CppTooWideScopeInitStatement
#include "openVCB.h"
//#include <tbb/spin_mutex.h>
//#include <tbb/task.h>
#if (defined __x86_64__ || defined __i386__) && 0
# include <tbb/spin_mutex.h>
using MyMutex = tbb::spin_mutex;
#else
using MyMutex = std::mutex;
#endif
#ifdef _WIN32
# define EXPORT_API extern "C" __declspec(dllexport)
#else
# define EXPORT_API extern "C"
#endif
EXPORT_API char const *const *openVCB_CompileAndRun(size_t *numErrors, int *stateSize);
EXPORT_API int32_t openVCB_InitProject(void);
EXPORT_API int64_t openVCB_GetLineNumber(int addr);
EXPORT_API size_t openVCB_GetSymbol(char const *buf, int size);
EXPORT_API uint64_t openVCB_GetNumTicks(void) noexcept;
EXPORT_API uintptr_t openVCB_GetVMemAddress(void) noexcept;
EXPORT_API float openVCB_GetMaxTPS(void) noexcept;
EXPORT_API void openVCB_SetTickRate(float tps) noexcept;
EXPORT_API void openVCB_Tick(int tick);
EXPORT_API void openVCB_ToggleLatch(int x, int y);
EXPORT_API void openVCB_ToggleLatchIndex(int idx);
EXPORT_API void openVCB_AddBreakpoint(int gid);
EXPORT_API void openVCB_RemoveBreakpoint(int gid);
EXPORT_API bool openVCB_PollBreakpoint(void) noexcept;
EXPORT_API void openVCB_SetClockPeriod(uint32_t high, uint32_t low) noexcept;
EXPORT_API void openVCB_SetTimerPeriod(uint32_t period) noexcept;
EXPORT_API void openVCB_NewProject(int64_t seed, bool vmemIsBytes);
EXPORT_API void openVCB_InitVMem(char const *assembly, int aSize, char *err, int errSize);
EXPORT_API void openVCB_DeleteProject(void);
EXPORT_API void openVCB_AddInstrumentBuffer(openVCB::InkState *buf, int bufSize, int idx);
EXPORT_API void openVCB_SetStateMemory(int *data, int size);
EXPORT_API void openVCB_SetVMemMemory(void *data, int size);
EXPORT_API void openVCB_SetIndicesMemory(int *data, int size);
EXPORT_API void openVCB_SetImageMemory(void *data, int width, int height);
EXPORT_API void openVCB_SetDecoMemory(int *indices, int indLen, int const *col, int colLen);
EXPORT_API void openVCB_GetGroupStats(int *numGroups, int *numConnections) noexcept;
EXPORT_API void openVCB_SetInterface(openVCB::LatchInterface const *__restrict addr, openVCB::LatchInterface const *__restrict data);
EXPORT_API void openVCB_FreeErrorArray(char **arr, size_t numStrings) noexcept;
namespace util = openVCB::util;
/****************************************************************************************/
namespace {
using openVCB::Ink;
constexpr float TARGET_DT = 1.0f / 60.0f;
constexpr float MIN_DT = 1.0f / 30.0f;
openVCB::Project *proj = nullptr;
std::thread *simThread = nullptr;
MyMutex simLock;
float targetTPS = 0.0f;
float maxTPS = 0.0f;
bool simRun = true;
bool simBreakpoint = false;
[[gnu::hot]]
void simFunc()
{
using std::chrono::duration, std::chrono::milliseconds;
using clock = std::chrono::high_resolution_clock;
float tpsEst = 2.0f / TARGET_DT;
float desiredTicks = 0.0f;
auto lastTime = clock::now();
while (simRun) {
auto curTime = clock::now();
auto diff = duration_cast<duration<float>>(curTime - lastTime).count();
lastTime = curTime;
desiredTicks = glm::min(desiredTicks + diff * targetTPS, tpsEst * MIN_DT);
// Find max tick amount we can do
if (desiredTicks >= 1.0f) {
float maxTickAmount = glm::max(tpsEst * TARGET_DT, 1.0f);
float tickAmount = glm::min(desiredTicks, maxTickAmount);
// Aquire lock, simulate, and time
simLock.lock();
auto t1 = clock::now();
auto res = proj->tick(static_cast<int32_t>(tickAmount));
auto t2 = clock::now();
simLock.unlock();
// Use timings to estimate max possible tps
desiredTicks = desiredTicks - static_cast<float>(res.numTicksProcessed);
maxTPS = static_cast<float>(res.numTicksProcessed) /
duration_cast<duration<float>>(t2 - t1).count();
if (std::isfinite(maxTPS))
tpsEst = glm::clamp(glm::mix(maxTPS, tpsEst, 0.95f), 1.0f, 1e8f);
if (res.breakpoint) {
targetTPS = 0.0f;
desiredTicks = 0.0f;
simBreakpoint = true;
}
}
// Check how much time I got left and sleep until the next check
curTime = clock::now();
diff = duration_cast<duration<float>>(curTime - lastTime).count();
if (float ms = 1000.0f * (TARGET_DT - diff); ms > 1.05f)
std::this_thread::sleep_for(milliseconds(static_cast<int>(ms)));
}
}
} // namespace
/****************************************************************************************/
/*
* Functions to control openVCB simulations
*/
EXPORT_API int64_t
openVCB_GetLineNumber(int addr)
{
auto itr = proj->lineNumbers.find(addr);
if (itr != proj->lineNumbers.end())
return itr->second;
return 0;
}
EXPORT_API size_t
openVCB_GetSymbol(char const *buf, int size)
{
auto itr = proj->assemblySymbols.find({buf, static_cast<size_t>(size)});
if (itr != proj->assemblySymbols.end())
return itr->second;
return 0;
}
EXPORT_API uint64_t
openVCB_GetNumTicks() noexcept
{
return proj->tickNum;
}
EXPORT_API float
openVCB_GetMaxTPS() noexcept
{
return static_cast<float>(maxTPS);
}
EXPORT_API uintptr_t
openVCB_GetVMemAddress() noexcept
{
#ifdef OVCB_BYTE_ORIENTED_VMEM
if (proj->vmemIsBytes)
return proj->lastVMemAddr / 4U;
else
return proj->lastVMemAddr;
#else
return proj->lastVMemAddr;
#endif
}
EXPORT_API void
openVCB_SetTickRate(float tps) noexcept
{
targetTPS = static_cast<float>(glm::max(0.0f, tps));
}
EXPORT_API void
openVCB_Tick(int tick)
{
if (!proj)
return;
std::lock_guard lock(simLock);
float tps = targetTPS;
targetTPS = 0.0f;
proj->tick(tick);
targetTPS = tps;
}
EXPORT_API void
openVCB_ToggleLatch(int x, int y)
{
std::lock_guard lock(simLock);
float tps = targetTPS;
targetTPS = 0.0f;
proj->toggleLatch(glm::ivec2(x, y));
targetTPS = tps;
}
EXPORT_API void
openVCB_ToggleLatchIndex(int idx)
{
std::lock_guard lock(simLock);
float tps = targetTPS;
targetTPS = 0.0f;
proj->toggleLatch(idx);
targetTPS = tps;
}
EXPORT_API void
openVCB_AddBreakpoint(int gid)
{
std::lock_guard lock(simLock);
float tps = targetTPS;
targetTPS = 0.0f;
proj->addBreakpoint(gid);
targetTPS = tps;
}
EXPORT_API void
openVCB_RemoveBreakpoint(int gid)
{
std::lock_guard lock(simLock);
float tps = targetTPS;
targetTPS = 0.0f;
proj->removeBreakpoint(gid);
targetTPS = tps;
}
EXPORT_API bool
openVCB_PollBreakpoint() noexcept
{
return std::exchange(simBreakpoint, false);
}
EXPORT_API void
openVCB_SetClockPeriod(uint32_t high, uint32_t low) noexcept
{
proj->tickClock.set_period(low, high);
}
EXPORT_API void
openVCB_SetTimerPeriod(uint32_t period) noexcept
{
proj->realtimeClock.set_period(period);
}
/*--------------------------------------------------------------------------------------*/
/*
* Functions to initialize openVCB
*/
EXPORT_API void
openVCB_NewProject(int64_t seed, bool vmemIsBytes)
{
std::lock_guard lock(simLock);
proj = new openVCB::Project(seed, vmemIsBytes);
}
EXPORT_API int
openVCB_InitProject()
{
std::lock_guard lock(simLock);
proj->preprocess();
// Start sim thread paused
simRun = true;
simThread = new std::thread(simFunc);
return proj->numGroups;
}
EXPORT_API void
openVCB_InitVMem(char const *assembly, int aSize, char *err, int errSize)
{
std::lock_guard lock(simLock);
proj->assembly = std::string(assembly, aSize);
proj->assembleVmem(err, errSize);
}
EXPORT_API void
openVCB_DeleteProject()
{
simRun = false;
std::lock_guard lock(simLock);
if (simThread) {
simThread->join();
delete simThread;
simThread = nullptr;
}
if (proj) {
// These should be managed.
proj->states = nullptr;
proj->vmem = nullptr;
proj->image = nullptr;
delete proj;
proj = nullptr;
}
}
/*--------------------------------------------------------------------------------------*/
/*
* Functions to replace openVCB buffers with managed ones
*/
EXPORT_API void
openVCB_AddInstrumentBuffer(openVCB::InkState *buf, int bufSize, int idx)
{
std::lock_guard lock(simLock);
float tps = targetTPS;
targetTPS = 0.0f;
proj->instrumentBuffers.emplace_back(buf, bufSize, idx);
targetTPS = tps;
}
EXPORT_API void
openVCB_SetStateMemory(int *data, int size)
{
std::lock_guard lock(simLock);
if (proj->states_is_native) {
memcpy(data, proj->states, sizeof(*proj->states) * size);
delete[] proj->states;
proj->states_is_native = false;
proj->states = reinterpret_cast<openVCB::InkState *>(data);
} else {
throw std::runtime_error("Cannot replace states matrix twice!");
}
}
EXPORT_API void
openVCB_SetVMemMemory(void *data, int size)
{
std::lock_guard lock(simLock);
proj->vmem = static_cast<uint32_t *>(data);
proj->vmemSize = size;
}
EXPORT_API void
openVCB_SetIndicesMemory(int *data, int size)
{
std::lock_guard lock(simLock);
memcpy(data, proj->indexImage, sizeof(*proj->indexImage) * size);
}
EXPORT_API void
openVCB_SetImageMemory(void *data, int width, int height)
{
std::lock_guard lock(simLock);
proj->width = width;
proj->height = height;
proj->image = static_cast<openVCB::InkPixel *>(data);
}
EXPORT_API void
openVCB_SetDecoMemory(int *indices, UU int indLen,
int const *col, UU int colLen)
{
std::lock_guard lock(simLock);
auto queue = std::queue<glm::ivec3>{};
auto visited = std::vector(size_t(proj->width) * size_t(proj->height), false);
for (int32_t y = 0; y < proj->height; ++y) {
for (int32_t x = 0; x < proj->width; ++x) {
int32_t idx = x + y * proj->width;
indices[idx] = -1;
if (static_cast<uint32_t>(col[idx]) != UINT32_MAX)
continue;
auto ink = SetOff(proj->image[idx].ink);
if (util::eq_any(ink, Ink::Latch, Ink::Led)) {
queue.emplace(x, y, proj->indexImage[idx]);
visited[idx] = true;
}
}
}
while (!queue.empty()) {
auto pos = queue.front();
queue.pop();
indices[pos.x + pos.y * proj->width] = pos.z;
for (auto const &neighbor : openVCB::fourNeighbors) {
glm::ivec2 np = static_cast<glm::ivec2>(pos) + neighbor;
if (np.x < 0 || np.x >= proj->width || np.y < 0 || np.y >= proj->height)
continue;
int nidx = np.x + np.y * proj->width;
if (visited[nidx])
continue;
visited[nidx] = true;
if (static_cast<uint32_t>(col[nidx]) != UINT32_MAX)
continue;
queue.emplace(np.x, np.y, pos.z);
}
}
}
/*--------------------------------------------------------------------------------------*/
/*
* Functions to configure openVCB
*/
EXPORT_API void
openVCB_GetGroupStats(int *numGroups, int *numConnections) noexcept
{
//std::lock_guard lock(simLock);
*numGroups = proj->numGroups;
*numConnections = proj->writeMap.nnz;
}
EXPORT_API void
openVCB_SetInterface(openVCB::LatchInterface const *__restrict addr,
openVCB::LatchInterface const *__restrict data)
{
std::lock_guard lock(simLock);
proj->vmAddr = *addr;
proj->vmData = *data;
}
/*--------------------------------------------------------------------------------------*/
EXPORT_API char const *const *
openVCB_CompileAndRun(size_t *numErrors, int *stateSize)
{
std::lock_guard lock(simLock);
// This processes the logic data to a form that can be simulated efficiently. It
// assumes without checking that the project structure is correctly initialized
// with all required data.
proj->preprocess();
if (!proj->error_messages->empty()) {
*numErrors = proj->error_messages->size();
*stateSize = 0;
char const *const *errors = proj->error_messages->data();
proj->error_messages->data() = nullptr;
proj->error_messages = nullptr;
delete proj;
proj = nullptr;
return errors;
}
*stateSize = proj->numGroups;
delete proj->error_messages;
proj->error_messages = nullptr;
// Start sim thread paused
simRun = true;
simThread = new std::thread(simFunc);
return nullptr;
}
EXPORT_API void
openVCB_FreeErrorArray(char **arr, size_t numStrings) noexcept
{
if (arr) {
for (size_t i = 0; i < numStrings; ++i)
delete[] arr[i];
delete[] arr;
}
}