diff --git a/Makefile b/Makefile index 1d73141..0ce3158 100644 --- a/Makefile +++ b/Makefile @@ -125,6 +125,7 @@ else @$(RM) $(OBJS_DIR) endif -re: fclean $(NAME) +re: fclean + $(MAKE) all -j$(shell nproc) .PHONY: all clean fclean re windows linux diff --git a/includes/RT/Clusterizer.hpp b/includes/RT/Clusterizer.hpp index 31ceb54..56029c8 100644 --- a/includes/RT/Clusterizer.hpp +++ b/includes/RT/Clusterizer.hpp @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/20 18:25:18 by tomoron #+# #+# */ -/* Updated: 2025/03/17 18:05:15 by tomoron ### ########.fr */ +/* Updated: 2025/03/18 12:56:01 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -75,7 +75,6 @@ class Clusterizer std::string _sceneName; Renderer *_renderer; - std::vector _jobs[3]; void imguiJobStat(void); void imguiClients(void); @@ -124,4 +123,6 @@ class Clusterizer struct pollfd *_pollfds; std::map _clients; size_t _curFrame; + + std::vector _jobs[3]; }; diff --git a/includes/RT/Scene.hpp b/includes/RT/Scene.hpp index 64c9d94..dc82c05 100644 --- a/includes/RT/Scene.hpp +++ b/includes/RT/Scene.hpp @@ -6,7 +6,7 @@ /* By: ycontre +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/23 18:30:18 by ycontre #+# #+# */ -/* Updated: 2025/03/17 15:26:14 by tomoron ### ########.fr */ +/* Updated: 2025/03/18 13:37:23 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -139,7 +139,7 @@ class Scene Camera *getCamera(void) const; GPUMaterial getMaterial(int material_index); - bool fail(void) const; + bool error(void) const; void changeScene(std::string &name, std::vector &buffers); @@ -148,7 +148,7 @@ class Scene private: void init(std::string &name); - bool _fail; + bool _error; std::vector _gpu_bvh_data; std::vector _gpu_bvh; diff --git a/srcs/RT.cpp b/srcs/RT.cpp index e100760..b0970c6 100644 --- a/srcs/RT.cpp +++ b/srcs/RT.cpp @@ -6,7 +6,7 @@ /* By: ycontre +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/27 14:51:49 by TheRed #+# #+# */ -/* Updated: 2025/03/17 14:54:43 by tomoron ### ########.fr */ +/* Updated: 2025/03/18 13:36:35 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,7 +28,7 @@ int main(int argc, char **argv) return (1); Scene scene(args.getSceneName()); - if (scene.fail()) + if (scene.error()) return (1); Window window(&scene, WIDTH, HEIGHT, "RT_GPU", 0, args); diff --git a/srcs/class/Clusterizer/Clusterizer.cpp b/srcs/class/Clusterizer/Clusterizer.cpp index 31c8e4b..a08700f 100644 --- a/srcs/class/Clusterizer/Clusterizer.cpp +++ b/srcs/class/Clusterizer/Clusterizer.cpp @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/20 18:24:39 by tomoron #+# #+# */ -/* Updated: 2025/03/17 15:16:43 by tomoron ### ########.fr */ +/* Updated: 2025/03/18 14:00:59 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,6 +18,9 @@ Clusterizer::Clusterizer(Arguments &args, Renderer *renderer) _isServer = 0; _error = 0; _serverSocket = 0; + _serverFd = 0; + _pollfds = 0; + _currentJob = 0; _sceneName = args.getSceneName(); _renderer = renderer; @@ -39,6 +42,21 @@ Clusterizer::~Clusterizer(void) { if(_serverSocket) close(_serverSocket); + if(_serverFd) + close(_serverFd); + if(_pollfds) + delete[] _pollfds; + abortJobs(); + for(auto it = _clients.begin(); it != _clients.end(); it++) + { + std::cout << "closing fd " << it->first << std::endl; + close(it->first); + } + if(_clients.size()) + updateServer(); + if(_currentJob) + delete _currentJob; + } void Clusterizer::update(Scene &scene, Window &win, std::vector &textures, ShaderProgram &denoisingProgram, std::vector &buffers) @@ -58,7 +76,7 @@ bool Clusterizer::getError(void) bool Clusterizer::isServer(void) { - return(_isServer); + return(_isServer); } bool Clusterizer::hasJobs(void) diff --git a/srcs/class/Clusterizer/client.cpp b/srcs/class/Clusterizer/client.cpp index 3a06f0d..5e88ec0 100644 --- a/srcs/class/Clusterizer/client.cpp +++ b/srcs/class/Clusterizer/client.cpp @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/20 21:08:38 by tomoron #+# #+# */ -/* Updated: 2025/03/17 18:05:26 by tomoron ### ########.fr */ +/* Updated: 2025/03/18 13:44:57 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -58,7 +58,6 @@ void Clusterizer::openClientConnection(const char *ip, int port) close(_serverFd); _serverFd = 0; } -// (void)write(_serverFd, (uint8_t []){RDY}, 1); } void Clusterizer::clientGetJob(void) @@ -97,7 +96,7 @@ void Clusterizer::changeMap(Scene &scene, std::vector &buffers, Window std::cout << "changing scene to name :" << _sceneName << std::endl; scene.changeScene(_sceneName, buffers); win.setFrameCount(0); - if(scene.fail()) + if(scene.error()) throw std::runtime_error("map change failed"); (void)write(_serverFd, (uint8_t []){RDY}, 1); } @@ -155,6 +154,10 @@ void Clusterizer::clientReceive(Scene &scene, std::vector &buffers, Wi { if(!ret) { + if(_currentJob) + delete _currentJob; + _currentJob = 0; + close(_serverFd); _serverFd = 0; return ; @@ -198,10 +201,10 @@ std::vector Clusterizer::rgb32fToRgb24i(std::vector &imageFloat) void Clusterizer::sendImageToServer(Scene &scene, std::vector &textures, ShaderProgram &denoisingProgram, std::vector &buffers, Window &win) { - _srvReady = 0; std::vector imageFloat(WIDTH * HEIGHT * 4); std::vector buffer; + _srvReady = 0; (void)write(_serverFd, (uint8_t []){IMG_SEND_RQ}, 1); while(!_srvReady) { diff --git a/srcs/class/Clusterizer/server.cpp b/srcs/class/Clusterizer/server.cpp index 990a825..fc660b1 100644 --- a/srcs/class/Clusterizer/server.cpp +++ b/srcs/class/Clusterizer/server.cpp @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/20 21:08:38 by tomoron #+# #+# */ -/* Updated: 2025/03/16 17:39:06 by tomoron ### ########.fr */ +/* Updated: 2025/03/18 14:03:32 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,7 +24,7 @@ void Clusterizer::initServer(std::string port) } catch(std::exception &e) { - std::cerr << "server initialization error : " << e.what() << std::endl; + std::cerr << "\033[31;1mserver initialization error : " << e.what() << "\033[0m" << std::endl; _error = 1; } } @@ -38,12 +38,25 @@ void Clusterizer::initServerSocket(int port) _serverSocket = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0); if (_serverSocket < 0) throw std::runtime_error("can't create socket"); + + int opt = 1; + if (setsockopt(_serverSocket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1) + { + perror("setsockopt"); + throw std::runtime_error("can't set socket options"); + } + s_addr.sin_family = AF_INET; s_addr.sin_addr.s_addr = INADDR_ANY; s_addr.sin_port = htons(port); if (bind(_serverSocket, (struct sockaddr *)&s_addr, \ sizeof(struct sockaddr_in)) < 0) + { + std::perror("bind :"); throw std::runtime_error("can't bind socket"); + } + + if (::listen(_serverSocket, 50) < 0) throw std::runtime_error("can't listen on socket"); if(_serverSocket == -1) diff --git a/srcs/class/Scene.cpp b/srcs/class/Scene.cpp index 680aa63..c896737 100644 --- a/srcs/class/Scene.cpp +++ b/srcs/class/Scene.cpp @@ -6,7 +6,7 @@ /* By: ycontre +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/23 18:29:41 by ycontre #+# #+# */ -/* Updated: 2025/03/17 15:28:10 by tomoron ### ########.fr */ +/* Updated: 2025/03/18 13:37:16 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,7 @@ Scene::Scene(std::string &name) { _camera = new Camera(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f), -90.0f, 0.0f); - _fail = 0; + _error = 0; init(name); } @@ -48,7 +48,7 @@ void Scene::init(std::string &name) if (!file.is_open()) { std::cerr << "Can't open scene file" << std::endl; - _fail = 1; + _error = 1; return ; } @@ -60,7 +60,7 @@ void Scene::init(std::string &name) { file.close(); std::cerr << line << std::endl; - _fail = 1; + _error = 1; return ; } } @@ -322,9 +322,9 @@ void Scene::updateLightAndObjects(int mat_id) } } -bool Scene::fail(void) const +bool Scene::error(void) const { - return(_fail); + return(_error); } std::set Scene::getGPULights() diff --git a/srcs/class/Window.cpp b/srcs/class/Window.cpp index 242ab44..12663ec 100644 --- a/srcs/class/Window.cpp +++ b/srcs/class/Window.cpp @@ -6,7 +6,7 @@ /* By: ycontre +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/13 16:16:24 by TheRed #+# #+# */ -/* Updated: 2025/03/17 15:16:19 by tomoron ### ########.fr */ +/* Updated: 2025/03/18 13:34:26 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -65,6 +65,8 @@ Window::Window(Scene *scene, int width, int height, const char *title, int sleep Window::~Window(void) { glfwTerminate(); + delete _renderer; + delete _clusterizer; } @@ -172,7 +174,7 @@ void Window::pollEvents() bool Window::shouldClose() { - return glfwWindowShouldClose(_window) || _renderer->shouldClose(); + return glfwWindowShouldClose(_window) || _renderer->shouldClose() || _clusterizer->getError(); } bool Window::isRendering()