initial commit
This commit is contained in:
@@ -5,6 +5,5 @@
|
||||
namespace veng {
|
||||
struct Material {
|
||||
TextureHandle texture_handle;
|
||||
std::vector<std::uint8_t> texture_image;
|
||||
};
|
||||
} // namespace veng
|
||||
|
||||
@@ -9,8 +9,34 @@
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <chrono>
|
||||
#include <limits>
|
||||
|
||||
#include "spdlog/spdlog.h"
|
||||
#include "utilities.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#define NOMINMAX
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#include <ws2bth.h>
|
||||
#include <ws2def.h>
|
||||
#include <windows.h>
|
||||
#define in_addr_t ULONG
|
||||
#elif __linux__
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/epoll.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/eventfd.h>
|
||||
#define SOCKET int
|
||||
#define INVALID_SOCKET -1
|
||||
#define SOCKET_ERROR -1
|
||||
#else
|
||||
#error "이 플랫폼은 지원되지 않습니다."
|
||||
#endif
|
||||
|
||||
#define MAX_BUFFERED_FRAMES (2)
|
||||
|
||||
78
Client/include/utils/thread_pool.h
Normal file
78
Client/include/utils/thread_pool.h
Normal file
@@ -0,0 +1,78 @@
|
||||
#pragma once
|
||||
#include <condition_variable>
|
||||
#include <functional>
|
||||
#include <future>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
#include <vector>
|
||||
#include <thread>
|
||||
|
||||
namespace utils {
|
||||
|
||||
class ThreadPool {
|
||||
public:
|
||||
ThreadPool();
|
||||
ThreadPool(std::uint32_t numThreads);
|
||||
~ThreadPool();
|
||||
|
||||
void init(std::uint32_t numThreads);
|
||||
void terminate();
|
||||
|
||||
void respawnWorker(std::uint32_t numThreads);
|
||||
|
||||
template <typename _Callable, typename... _Args>
|
||||
requires(
|
||||
!std::is_void_v<std::invoke_result_t<_Callable, ThreadPool*, _Args...>>)
|
||||
int enqueueJob(_Callable&& __job,
|
||||
std::invoke_result_t<_Callable, _Args...>& retVal,
|
||||
_Args&&... __args) {
|
||||
if (terminate_) {
|
||||
spdlog::error("Cannot run jobs on threads that terminating...");
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(jobQueueMutex);
|
||||
auto boundFunc = [this, &retVal, __job,
|
||||
... __args = std::move(__args)]() mutable {
|
||||
retVal = __job(this, std::move(__args)...);
|
||||
};
|
||||
auto task = std::packaged_task<void()>(std::move(boundFunc));
|
||||
jobs_.push(std::move(task));
|
||||
jobQueueCV_.notify_one();
|
||||
|
||||
return 0;
|
||||
}
|
||||
template <typename _Callable, typename... _Args>
|
||||
requires std::is_void_v<
|
||||
std::invoke_result_t<_Callable, ThreadPool*, _Args...>>
|
||||
int enqueueJob(_Callable&& __job, _Args&&... __args) {
|
||||
if (terminate_) {
|
||||
spdlog::error("Cannot run jobs on threads that terminating...");
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(jobQueueMutex);
|
||||
auto boundFunc = [this, __job, ... __args = std::move(__args)]() mutable {
|
||||
__job(this, std::move(__args)...);
|
||||
};
|
||||
auto task = std::packaged_task<void()>(std::move(boundFunc));
|
||||
jobs_.push(std::move(task));
|
||||
jobQueueCV_.notify_one();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int threadCount = 0;
|
||||
|
||||
private:
|
||||
void* Worker();
|
||||
|
||||
std::condition_variable jobQueueCV_;
|
||||
std::mutex jobQueueMutex;
|
||||
std::queue<std::packaged_task<void()>> jobs_;
|
||||
std::vector<std::thread> workers_;
|
||||
bool terminate_ = false;
|
||||
};
|
||||
|
||||
} // namespace Chattr
|
||||
@@ -1,8 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include "asset/loader.h"
|
||||
#include "physics.h"
|
||||
#include "graphics.h"
|
||||
#include "physics.h"
|
||||
#include "utils/thread_pool.h"
|
||||
|
||||
namespace veng {
|
||||
|
||||
@@ -34,10 +35,11 @@ class Engine {
|
||||
glm::perspective(glm::radians(103.f), 800.f / 600.f, 0.1f, 1000.f);
|
||||
|
||||
gsl::not_null<Graphics*> vulkan_graphics;
|
||||
|
||||
private:
|
||||
Loader asset_loader_;
|
||||
|
||||
Physics physics_controller_;
|
||||
utils::ThreadPool thread_pool_;
|
||||
|
||||
glm::ivec2 window_size_ = {0, 0};
|
||||
std::double_t last_frame_time_ = 0.0;
|
||||
|
||||
@@ -2,11 +2,12 @@
|
||||
|
||||
#include "asset/object/model.h"
|
||||
#include "vulkan/vertex.h"
|
||||
#include "utils/thread_pool.h"
|
||||
|
||||
namespace veng {
|
||||
class Physics {
|
||||
public:
|
||||
void invokeOnColisionEvent(gsl::span<Model*> models);
|
||||
void invokeOnColisionEvent(gsl::not_null<utils::ThreadPool *> thread_pool, gsl::span<Model*> models);
|
||||
|
||||
bool RayTrace(const glm::vec3& rayOrigin, const glm::vec3& rayDir,
|
||||
const glm::vec3& v0, const glm::vec3& v1, const glm::vec3& v2,
|
||||
|
||||
@@ -18,6 +18,17 @@ void BeginPlay(veng::Engine& engine) {
|
||||
veng::Model* const player = engine.SpawnModel("player", "player");
|
||||
player->scale = glm::vec3(.02f);
|
||||
player->colision = true;
|
||||
player->OnColision = [](veng::Model* self, veng::Model* other) {
|
||||
if (other->owner == self) return;
|
||||
std::cout << self << " and " << other << " is Nearby." << std::endl;
|
||||
|
||||
std::cout << self << "'s owner: " << self->owner << std::endl;
|
||||
std::cout << other << "'s owner: " << other->owner << std::endl;
|
||||
|
||||
std::cout << "Colided." << std::endl;
|
||||
other->colision = false;
|
||||
other->visible = false;
|
||||
};
|
||||
veng::Model* const player_flame =
|
||||
engine.SpawnModel("player_flame", "player_flame");
|
||||
player_flame->scale = player->scale;
|
||||
|
||||
82
Client/src/utils/thread_pool.cpp
Normal file
82
Client/src/utils/thread_pool.cpp
Normal file
@@ -0,0 +1,82 @@
|
||||
#include "utils/thread_pool.h"
|
||||
#include "precomp.h"
|
||||
|
||||
namespace utils {
|
||||
|
||||
ThreadPool::ThreadPool() : ThreadPool(0) {}
|
||||
|
||||
ThreadPool::ThreadPool(std::uint32_t numThreads) { init(numThreads); }
|
||||
|
||||
ThreadPool::~ThreadPool() { terminate(); }
|
||||
|
||||
void ThreadPool::init(std::uint32_t numThreads) {
|
||||
int numCPU = numThreads;
|
||||
if (numThreads == 0) {
|
||||
#ifdef _WIN32
|
||||
SYSTEM_INFO sysinfo;
|
||||
GetSystemInfo(&sysinfo);
|
||||
numCPU = sysinfo.dwNumberOfProcessors;
|
||||
#elif __linux__
|
||||
numCPU = sysconf(_SC_NPROCESSORS_ONLN);
|
||||
#endif
|
||||
spdlog::info("Auto-detected cpu count: {}", numCPU);
|
||||
if (numCPU == 1 || numCPU == 2) {
|
||||
numCPU = 4;
|
||||
spdlog::info(
|
||||
"Set ThreadPool Worker count to: {} due to program to oprate "
|
||||
"concurrently",
|
||||
numCPU);
|
||||
} else {
|
||||
spdlog::info("Set ThreadPool Worker count to: {}", numCPU);
|
||||
}
|
||||
}
|
||||
threadCount = numCPU;
|
||||
workers_.reserve(numCPU);
|
||||
|
||||
while (numCPU--) workers_.emplace_back([this]() { this->Worker(); });
|
||||
}
|
||||
|
||||
void ThreadPool::terminate() {
|
||||
terminate_ = true;
|
||||
jobQueueCV_.notify_all();
|
||||
|
||||
spdlog::debug("waiting for threads to end their jobs...");
|
||||
for (auto& t : workers_) t.join();
|
||||
}
|
||||
|
||||
void ThreadPool::respawnWorker(std::uint32_t numThreads) {
|
||||
terminate();
|
||||
terminate_ = false;
|
||||
init(numThreads);
|
||||
}
|
||||
|
||||
void* ThreadPool::Worker() {
|
||||
#ifdef _WIN32
|
||||
DWORD pid = GetCurrentThreadId();
|
||||
#elif __linux__
|
||||
pthread_t pid = pthread_self();
|
||||
#endif
|
||||
spdlog::trace("ThreadPool Worker : {} up", pid);
|
||||
while (!terminate_) {
|
||||
std::unique_lock<std::mutex> lock(jobQueueMutex);
|
||||
jobQueueCV_.wait(lock,
|
||||
[this]() { return !this->jobs_.empty() || terminate_; });
|
||||
if (this->jobs_.empty() || terminate_) {
|
||||
jobs_ = std::queue<std::packaged_task<void()>>();
|
||||
break;
|
||||
}
|
||||
if (this->jobs_.empty()) continue;
|
||||
|
||||
auto job = std::move(jobs_.front());
|
||||
jobs_.pop();
|
||||
lock.unlock();
|
||||
|
||||
spdlog::trace("ThreadPool Worker : {} Executing a job", pid);
|
||||
job();
|
||||
}
|
||||
spdlog::trace("ThreadPool Worker : {} down", pid);
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
} // namespace Chattr
|
||||
@@ -14,17 +14,6 @@ void Engine::init() {
|
||||
1000.f);
|
||||
vulkan_graphics->SetViewProjection(view, projection);
|
||||
|
||||
for (auto it = model_assets_.begin(); it != model_assets_.end();) {
|
||||
it->second.vertex_buffer =
|
||||
vulkan_graphics->CreateVertexBuffer(it->second.vertices);
|
||||
it->second.index_buffer =
|
||||
vulkan_graphics->CreateIndexBuffer(it->second.indices);
|
||||
it->second.material.texture_handle =
|
||||
vulkan_graphics->CreateTexture(it->second.material.texture_image);
|
||||
|
||||
++it;
|
||||
}
|
||||
|
||||
if (BeginPlay != nullptr) BeginPlay(*this);
|
||||
}
|
||||
|
||||
@@ -32,7 +21,11 @@ void Engine::LoadModelAsset(std::string path, std::string name) {
|
||||
veng::Model model(vulkan_graphics);
|
||||
asset_loader_.setPath(path);
|
||||
asset_loader_.loadModel(model);
|
||||
model.material.texture_image = asset_loader_.readTexture();
|
||||
|
||||
model.vertex_buffer = vulkan_graphics->CreateVertexBuffer(model.vertices);
|
||||
model.index_buffer = vulkan_graphics->CreateIndexBuffer(model.indices);
|
||||
model.material.texture_handle =
|
||||
vulkan_graphics->CreateTexture(asset_loader_.readTexture());
|
||||
model_assets_[name] = std::move(model);
|
||||
}
|
||||
|
||||
@@ -175,7 +168,7 @@ void Engine::Update() {
|
||||
vulkan_graphics->RenderModel(it);
|
||||
}
|
||||
|
||||
physics_controller_.invokeOnColisionEvent({models.data(), models.size()});
|
||||
physics_controller_.invokeOnColisionEvent(&thread_pool_, {models.data(), models.size()});
|
||||
|
||||
vulkan_graphics->EndFrame();
|
||||
}
|
||||
|
||||
@@ -6,8 +6,9 @@
|
||||
#include "precomp.h"
|
||||
|
||||
namespace veng {
|
||||
void Physics::invokeOnColisionEvent(gsl::span<Model*> models) {
|
||||
const std::float_t EPSILON = std::numeric_limits<std::float_t>::epsilon();
|
||||
void Physics::invokeOnColisionEvent(
|
||||
gsl::not_null<utils::ThreadPool*> thread_pool, gsl::span<Model*> models) {
|
||||
constexpr std::float_t EPSILON = std::numeric_limits<std::float_t>::epsilon();
|
||||
|
||||
for (int first = 0; first < models.size(); first++) {
|
||||
if (!models[first]->colision) continue;
|
||||
@@ -21,9 +22,19 @@ void Physics::invokeOnColisionEvent(gsl::span<Model*> models) {
|
||||
models[second]->radius * models[second]->scale.x;
|
||||
if (distance <= model1_radius + model2_radius) {
|
||||
if (models[first]->OnColision != nullptr)
|
||||
models[first]->OnColision(models[first], models[second]);
|
||||
thread_pool->enqueueJob(
|
||||
[OnColision = models[first]->OnColision](
|
||||
utils::ThreadPool* thread_pool, Model* self, Model* other) {
|
||||
OnColision(self, other);
|
||||
},
|
||||
models[first], models[second]);
|
||||
if (models[second]->OnColision != nullptr)
|
||||
models[second]->OnColision(models[second], models[first]);
|
||||
thread_pool->enqueueJob(
|
||||
[OnColision = models[second]->OnColision](
|
||||
utils::ThreadPool* thread_pool, Model* self, Model* other) {
|
||||
OnColision(self, other);
|
||||
},
|
||||
models[second], models[first]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user