Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chunk Swap ( WIP ) #21

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/pg.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion bin/pg.module.js

Large diffs are not rendered by default.

Binary file modified bin/pg.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion compile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ echo 'building...'

NUM_THREADS=8

emcc -D NUM_THREADS=$NUM_THREADS -sNO_EXIT_RUNTIME=1 -s TOTAL_MEMORY=100MB -pthread -sPTHREAD_POOL_SIZE=$NUM_THREADS -sPTHREAD_POOL_SIZE_STRICT=$NUM_THREADS -s ALLOW_MEMORY_GROWTH=0 -sLLD_REPORT_UNDEFINED -O3 \
emcc -std=c++20 -D NUM_THREADS=$NUM_THREADS -sNO_EXIT_RUNTIME=1 -s TOTAL_MEMORY=100MB -pthread -sPTHREAD_POOL_SIZE=$NUM_THREADS -sPTHREAD_POOL_SIZE_STRICT=$NUM_THREADS -s ALLOW_MEMORY_GROWTH=0 -sLLD_REPORT_UNDEFINED -O3 \
binding.cc \
procgen.cc \
instance.cc \
Expand Down
1 change: 1 addition & 0 deletions generation/instance-generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ class InstanceGenerator
{
if (noises.uberNoise.instanceVisibility<I>(ax, az, heightfield))
{
// ! Remove numInstances ?
const int instanceId = (int)std::round(dis(rng) * (float)(numInstances - 1));
const float scale = noises.uberNoise.scaleNoise<I>(ax, az, heightfield);
const vm::vec3 rot = noises.uberNoise.rotationNoise<I>(ax, az, heightfield);
Expand Down
8 changes: 8 additions & 0 deletions task/octree.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
#include "octree.h"
#include <cmath>

vm::ivec2 chunkMinForPosition(const vm::ivec2 &p, const int &lod)
{
return vm::ivec2{
(int)std::floor((float)p.x / (float)lod) * lod,
(int)std::floor((float)p.y / (float)lod) * lod
};
}

uint64_t hashOctreeMin(const vm::ivec2 &min)
{
uint64_t result = uint16_t(min.x);
Expand Down
16 changes: 13 additions & 3 deletions task/octree.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,36 @@
//


const vm::ivec3 chunkMinForPosition(const vm::ivec3 &p, const int &lod);
const vm::ivec3 chunkMinForPosition(const vm::vec3 &p, const int &lod);
vm::ivec2 chunkMinForPosition(const vm::ivec2 &p, const int &lod);

uint64_t hashOctreeMin(const vm::ivec2 &min);
uint64_t hashOctreeMin(const vm::ivec3 &min);
uint64_t hashOctreeMinLod(const vm::ivec2 &min, int lod);
uint64_t hashOctreeMinLod(const vm::ivec3 &min, int lod);

enum class REPLACING_SIZE : int
{
SAME,
BIGGER,
SMALLER,
};

class OctreeNodeSpec {
public:
vm::ivec2 min;
int lod;
int lodArray[2];
REPLACING_SIZE replacing = REPLACING_SIZE::SAME;
};

class OctreeNode : public OctreeNodeSpec {
public:
OctreeNode(const vm::ivec2 &min, int lod) :
OctreeNode(const vm::ivec2 &min, const int &lod) :
OctreeNodeSpec{min, lod, {0, 0}}
{}
OctreeNode(const vm::ivec2 &min, const int &lod, const REPLACING_SIZE &replacing) :
OctreeNodeSpec{min, lod, {0, 0}, replacing}
{}
int getPriority() const {
return 0;
}
Expand Down
5 changes: 1 addition & 4 deletions task/promise.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,14 @@ class Promise {
public:
uint32_t id;
ResultQueue *resultQueue;
// void *value;
// Mutex mutex;

std::atomic<bool> live;

Promise(uint32_t id, ResultQueue *resultQueue);
~Promise();

// void *get();
bool resolve(void *value = nullptr);
bool kill();
// void wait();
};

#endif // PROMISE_H
2 changes: 0 additions & 2 deletions task/result.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ class Promise;
class ResultQueue {
public:
Mutex mutex;
// uint32_t ids;
std::deque<std::shared_ptr<Promise>> livePromises;

ResultQueue();
~ResultQueue();

// uint32_t getNextId();
std::shared_ptr<Promise> createPromise(uint32_t id);
std::shared_ptr<Promise> findPromise(uint32_t id);
void cancelPromise(uint32_t id);
Expand Down
19 changes: 1 addition & 18 deletions task/sync.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,6 @@ void Mutex::unlock() {
bool Mutex::try_lock() {
return mutex.try_lock();
}
/* bool Mutex::test() {
return !flag.test();
}
void Mutex::wait() {
lock();
unlock();
} */

//

Semaphore::Semaphore(int count) : count(count) {}
void Semaphore::signal() {
Expand All @@ -42,12 +33,4 @@ void Semaphore::wait() {
cv.wait(lock);
}
count--;
}
/* bool Semaphore::try_wait() {
std::lock_guard<decltype(mutex_)> lock(mutex_);
if(count_) {
--count_;
return true;
}
return false;
} */
}
98 changes: 4 additions & 94 deletions task/task.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,95 +122,31 @@ void TaskQueue::pushTask(Task *task) {
}
taskSemaphore.signal();
}
// std::atomic<int> numActiveThreads(NUM_THREADS);
Task *TaskQueue::popLockTask() {
/* EM_ASM(
console.log('pop lock task 1');
); */
// int currentNumActiveThreads = numActiveThreads.fetch_sub(1) - 1;

/* EM_ASM({
console.log('try to pop task', $0);
}, currentNumActiveThreads); */

/* EM_ASM({
globalThis.lockStartTime = performance.now();
}); */

/* if (currentNumActiveThreads < 2) {
std::cout << "pop task 1 " << currentNumActiveThreads << std::endl;
} */
Task *TaskQueue::popLockTask() {
taskSemaphore.wait();

/* double time = EM_ASM_DOUBLE({
const lockEndTime = performance.now();
return lockEndTime - globalThis.lockStartTime;
});
int currentNumActiveThreads2 = numActiveThreads.fetch_add(1) + 1;
if (currentNumActiveThreads2 < 2) {
std::cout << "pop task 2 " << currentNumActiveThreads2 << " " << time << std::endl;
} */

/* EM_ASM({
// console.time('pop task ' + $0);
globalThis.requestStartTime = performance.now();
}); */

/* EM_ASM({
console.timeEnd('pop task ' + $0);
console.log('num active threads', $1, $2, $3);
}, pthread_self(), currentNumActiveThreads, currentNumActiveThreads2, tasks.size()); */

/* EM_ASM(
console.log('pop lock sema waited');
); */

Task *task;
{
std::unique_lock<Mutex> lock(taskMutex);
// lock.lock();

/* if (lockedTasks.size() == 0) {
abort();
} */

task = tasks.front();
tasks.pop_front();

/* if (currentNumActiveThreads < 8) {
EM_ASM({
console.log('fewer than 8 threads', $0, $1);
}, currentNumActiveThreads, tasks.size());
} */

// task->ensurePop();
}
/* if (task == nullptr) {
EM_ASM(
console.log('failed to pop task!');
);
abort();
} */

return task;
}
void TaskQueue::cancelTask(uint32_t taskId) {
std::unique_lock<Mutex> lock(taskMutex);
for (auto it = tasks.begin(); it != tasks.end(); it++) {
Task *task = (*it);
if (task->id == taskId && task->live) {
/* EM_ASM({
console.log('cancel task', $0);
}, task->id); */
task->cancel();
break;
}
}
}
void TaskQueue::runLoop() {
/* EM_ASM(
console.log('run loop');
); */
// {
for (;;) {
Task *task = popLockTask();
if (!task) {
Expand All @@ -220,19 +156,13 @@ void TaskQueue::runLoop() {
if (task->live) {
task->run();
}
/* EM_ASM({
console.log('done running task');
}); */
delete task;
// task = nullptr;
}
// }
/* EM_ASM(
console.log('thread exited due to no task!');
); */

std::cout << "main loop exited" << std::endl;
abort();
}

void TaskQueue::setCamera(const vm::vec3 &worldPosition, const vm::vec3 &cameraPosition, const Quat &cameraQuaternion, const std::array<float, 16> &projectionMatrix) {
std::unique_lock<Mutex> lock(taskMutex);

Expand Down Expand Up @@ -293,24 +223,4 @@ void TaskQueue::sortTasksInternal() {
Frustum frustum = getFrustum();

sort<Task *>(tasks, worldPosition, frustum);

/* std::vector<std::pair<Task *, float>> taskDistances;
taskDistances.reserve(tasks.size());
for (size_t i = 0; i < tasks.size(); i++) {
Task *task = tasks[i];
float distance = getTaskDistance(task, frustum);
taskDistances.push_back(std::pair<Task *, float>(task, distance));
}

std::sort(
taskDistances.begin(),
taskDistances.end(),
[&](const std::pair<Task *, float> &a, const std::pair<Task *, float> &b) -> bool {
return a.second < b.second;
}
);

for (size_t i = 0; i < taskDistances.size(); i++) {
tasks[i] = taskDistances[i].first;
} */
}
1 change: 0 additions & 1 deletion task/task.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class Task {

class TaskQueue {
public:
// DCInstance *inst;
std::deque<Task *> tasks;

Mutex taskMutex;
Expand Down
Loading