mirror of
https://github.com/TheRedShip/RT_GPU.git
synced 2025-09-27 18:48:36 +02:00
server now sends jobs to client and client can send result image to server
This commit is contained in:
10
srcs/RT.cpp
10
srcs/RT.cpp
@ -6,7 +6,7 @@
|
||||
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/09/27 14:51:49 by TheRed #+# #+# */
|
||||
/* Updated: 2025/02/22 22:09:46 by tomoron ### ########.fr */
|
||||
/* Updated: 2025/02/25 01:51:27 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -28,10 +28,6 @@ int main(int argc, char **argv)
|
||||
if (args.error())
|
||||
return (1);
|
||||
|
||||
Clusterizer clusterizer(args);
|
||||
if (clusterizer.getError())
|
||||
return(1);
|
||||
|
||||
Scene scene(args.getSceneName());
|
||||
if (scene.fail())
|
||||
return (1);
|
||||
@ -71,7 +67,7 @@ int main(int argc, char **argv)
|
||||
|
||||
while (!window.shouldClose())
|
||||
{
|
||||
clusterizer.update();
|
||||
window.clusterizerUpdate(textures, denoising_program);
|
||||
window.updateDeltaTime();
|
||||
|
||||
updateDataOnGPU(scene, buffers);
|
||||
@ -103,7 +99,7 @@ int main(int argc, char **argv)
|
||||
render_program.use();
|
||||
drawScreenTriangle(VAO, textures[0], render_program.getProgram());
|
||||
|
||||
window.imGuiRender(raytracing_program, clusterizer);
|
||||
window.imGuiRender(raytracing_program);
|
||||
|
||||
window.display();
|
||||
window.pollEvents();
|
||||
|
@ -6,19 +6,29 @@
|
||||
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/20 18:24:39 by tomoron #+# #+# */
|
||||
/* Updated: 2025/02/23 22:41:07 by tomoron ### ########.fr */
|
||||
/* Updated: 2025/02/25 01:49:23 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "RT.hpp"
|
||||
|
||||
Clusterizer::Clusterizer(Arguments &args)
|
||||
//TODO before push :
|
||||
// - client work on image when gets job
|
||||
// - handle client disconnect
|
||||
// - show image number in imgui
|
||||
// - client send progress update when percent change
|
||||
// - server action to send map name
|
||||
// - when image is done, send a image send request and wait for server response
|
||||
// - when server accepts send request, send full image
|
||||
|
||||
Clusterizer::Clusterizer(Arguments &args, Renderer *renderer)
|
||||
{
|
||||
_isActive = 1;
|
||||
_isServer = 0;
|
||||
_error = 0;
|
||||
_serverSocket = 0;
|
||||
_sceneName = args.getSceneName();
|
||||
_renderer = renderer;
|
||||
|
||||
if(args.getBoolean("server"))
|
||||
{
|
||||
@ -40,14 +50,14 @@ Clusterizer::~Clusterizer(void)
|
||||
close(_serverSocket);
|
||||
}
|
||||
|
||||
void Clusterizer::update(void)
|
||||
void Clusterizer::update(Scene &scene, Window &win, std::vector<GLuint> &textures, ShaderProgram &denoisingProgram)
|
||||
{
|
||||
if(!_isActive)
|
||||
return ;
|
||||
if(_isServer)
|
||||
updateServer();
|
||||
else
|
||||
updateClient();
|
||||
updateClient(scene, win, textures, denoisingProgram);
|
||||
}
|
||||
|
||||
bool Clusterizer::getError(void)
|
||||
|
@ -6,12 +6,14 @@
|
||||
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/20 21:08:38 by tomoron #+# #+# */
|
||||
/* Updated: 2025/02/22 22:50:10 by tomoron ### ########.fr */
|
||||
/* Updated: 2025/02/25 01:48:47 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "RT.hpp"
|
||||
|
||||
void shaderDenoise(ShaderProgram &denoising_program, GPUDenoise &denoise, std::vector<GLuint> textures);
|
||||
|
||||
void Clusterizer::initClient(std::string &dest)
|
||||
{
|
||||
_serverFd = 0;
|
||||
@ -62,19 +64,36 @@ void Clusterizer::openClientConnection(const char *ip, int port)
|
||||
|
||||
void Clusterizer::clientGetJob(void)
|
||||
{
|
||||
uint8_t *data;
|
||||
std::cout << "received job" << std::endl;
|
||||
if(_receiveBuffer.size() < sizeof(t_job) + 1)
|
||||
return ;
|
||||
|
||||
_currentJob = *(t_job *)(_receiveBuffer.data() + 1);
|
||||
data = _receiveBuffer.data();
|
||||
_currentJob = new t_job;
|
||||
*_currentJob = *(t_job *)(data + 1);
|
||||
std::cout << "delete length : " << std::distance(_receiveBuffer.begin(), _receiveBuffer.begin() + sizeof(t_job) + 1) << std::endl;
|
||||
_receiveBuffer.erase(_receiveBuffer.begin(), _receiveBuffer.begin() + sizeof(t_job) + 1);
|
||||
std::cout << "new size : " << _receiveBuffer.size() << std::endl;
|
||||
_progress = 0;
|
||||
}
|
||||
|
||||
void Clusterizer::clientHandleBuffer(void)
|
||||
{
|
||||
std::vector<uint8_t> sendBuf;
|
||||
|
||||
if(!_receiveBuffer.size())
|
||||
return ;
|
||||
std::cout << (int)_receiveBuffer.size() << std::endl;
|
||||
std::cout << sizeof(t_job) + 1 << std::endl;
|
||||
std::cout << std::endl;
|
||||
if(_receiveBuffer[0] == JOB)
|
||||
clientGetJob();
|
||||
else if(_receiveBuffer[0] == RDY)
|
||||
_srvReady = 1;
|
||||
else
|
||||
_receiveBuffer.erase(_receiveBuffer.begin());
|
||||
|
||||
|
||||
if(sendBuf.size())
|
||||
(void)write(1, sendBuf.data(), sendBuf.size());
|
||||
@ -95,9 +114,69 @@ void Clusterizer::clientReceive(void)
|
||||
return ;
|
||||
}
|
||||
_receiveBuffer.insert(_receiveBuffer.end(), buffer, buffer + ret);
|
||||
clientHandleBuffer();
|
||||
}
|
||||
|
||||
void Clusterizer::updateClient(void)
|
||||
void Clusterizer::sendProgress(uint8_t progress)
|
||||
{
|
||||
uint8_t buf[2];
|
||||
_progress = progress;
|
||||
|
||||
buf[0] = PROGRESS_UPDATE;
|
||||
buf[1] = progress;
|
||||
(void)write(_serverFd, buf, 2);
|
||||
}
|
||||
|
||||
void Clusterizer::sendImageToServer(std::vector<GLuint> &textures, ShaderProgram &denoisingProgram)
|
||||
{
|
||||
_srvReady = 0;
|
||||
std::vector<uint8_t> buffer(WIDTH * HEIGHT * 4);
|
||||
|
||||
(void)write(_serverFd, (uint8_t []){IMG_SEND_RQ}, 1);
|
||||
while(!_srvReady)
|
||||
{
|
||||
clientReceive();
|
||||
usleep(10000);
|
||||
}
|
||||
|
||||
if(_currentJob->denoise.enabled)
|
||||
shaderDenoise(denoisingProgram, _currentJob->denoise, textures);
|
||||
glBindTexture(GL_TEXTURE_2D, textures[0]);
|
||||
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, buffer.data());
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
|
||||
(void)write(_serverFd, (uint8_t []){IMG}, 1);
|
||||
(void)write(_serverFd, buffer.data(), buffer.size());
|
||||
}
|
||||
|
||||
void Clusterizer::handleCurrentJob(Scene &scene, Window &win, std::vector<GLuint> &textures, ShaderProgram &denoisingProgram)
|
||||
{
|
||||
uint8_t progress;
|
||||
if(!_currentJob)
|
||||
return ;
|
||||
|
||||
if(scene.getCamera()->getPosition() != _currentJob->pos || scene.getCamera()->getDirection() != _currentJob->dir)
|
||||
{
|
||||
scene.getCamera()->setPosition(_currentJob->pos);
|
||||
scene.getCamera()->setDirection(_currentJob->dir.x, _currentJob->dir.y);
|
||||
win.setFrameCount(0);
|
||||
return ;
|
||||
}
|
||||
|
||||
if((size_t)win.getFrameCount() < _currentJob->samples)
|
||||
{
|
||||
progress = ((double)win.getFrameCount() / _currentJob->samples) * 100;
|
||||
if(progress != _progress)
|
||||
sendProgress(progress);
|
||||
}
|
||||
|
||||
if((size_t)win.getFrameCount() == _currentJob->samples)
|
||||
{
|
||||
sendImageToServer(textures, denoisingProgram);
|
||||
}
|
||||
}
|
||||
|
||||
void Clusterizer::updateClient(Scene &scene, Window &win, std::vector<GLuint> &textures, ShaderProgram &denoisingProgram)
|
||||
{
|
||||
if(!_serverFd)
|
||||
{
|
||||
@ -108,6 +187,5 @@ void Clusterizer::updateClient(void)
|
||||
}
|
||||
|
||||
clientReceive();
|
||||
if(_receiveBuffer.size())
|
||||
clientHandleBuffer();
|
||||
handleCurrentJob(scene, win, textures, denoisingProgram);
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/22 19:52:51 by tomoron #+# #+# */
|
||||
/* Updated: 2025/02/22 23:39:46 by tomoron ### ########.fr */
|
||||
/* Updated: 2025/02/24 20:52:33 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -28,12 +28,12 @@ void Clusterizer::imguiClients(void)
|
||||
ImGui::BeginChild("clientList", ImVec2(0, 0), true, 0);
|
||||
for(auto it = _clients.begin();it != _clients.end(); it++)
|
||||
{
|
||||
if(!it->second.ready)
|
||||
status = "not ready";
|
||||
else if(it->second.ready && it->second.curJob)
|
||||
if(it->second.ready)
|
||||
status = "idle";
|
||||
else if(!it->second.ready && it->second.curJob)
|
||||
status = "working";
|
||||
else if(it->second.ready)
|
||||
status = "idle";
|
||||
status = "not ready";
|
||||
|
||||
ImGui::Text("status : %s", status.c_str());
|
||||
if(it->second.curJob)
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/20 21:08:38 by tomoron #+# #+# */
|
||||
/* Updated: 2025/02/23 22:15:38 by tomoron ### ########.fr */
|
||||
/* Updated: 2025/02/25 00:53:02 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -98,7 +98,6 @@ void Clusterizer::handleBuffer(int fd, std::vector<uint8_t> &buf)
|
||||
{
|
||||
_clients[fd].ready = 1;
|
||||
std::cout << "client " << fd << " is ready !" << std::endl;
|
||||
sendBuffer.push_back(ACK);
|
||||
}
|
||||
|
||||
if(sendBuffer.size())
|
||||
@ -120,7 +119,8 @@ int Clusterizer::updateBuffer(int fd)
|
||||
|
||||
int Clusterizer::dispatchJobs(void)
|
||||
{
|
||||
t_job *tmp;
|
||||
t_job *tmp;
|
||||
uint8_t sendBuf;
|
||||
int dispatched;
|
||||
|
||||
dispatched = 0;
|
||||
@ -134,26 +134,30 @@ int Clusterizer::dispatchJobs(void)
|
||||
tmp = _jobs[WAITING].front();
|
||||
_jobs[WAITING].erase(_jobs[WAITING].begin());
|
||||
_jobs[IN_PROGRESS].push_back(tmp);
|
||||
sendBuf = JOB;
|
||||
(void)write(it->first, &sendBuf, 1);
|
||||
(void)write(it->first, &tmp, sizeof(t_job));
|
||||
it->second.ready = 0;
|
||||
it->second.progress = 0;
|
||||
it->second.curJob = tmp;
|
||||
dispatched = 1;
|
||||
}
|
||||
if(!_jobs[WAITING].size())
|
||||
return (1);
|
||||
}
|
||||
return (dispatched);
|
||||
}
|
||||
|
||||
void Clusterizer::addJob(glm::vec3 pos, glm::vec2 dir, size_t samples)
|
||||
void Clusterizer::addJob(glm::vec3 pos, glm::vec2 dir, size_t samples, size_t frame, GPUDenoise &denoise)
|
||||
{
|
||||
t_job *tmp;
|
||||
|
||||
tmp = new t_job;
|
||||
tmp->scene = _sceneName;
|
||||
tmp->pos = pos;
|
||||
tmp->dir = dir;
|
||||
tmp->samples = samples;
|
||||
tmp->id = _curId++;
|
||||
tmp->frameNb = frame;
|
||||
tmp->denoise = denoise;
|
||||
_jobs[WAITING].push_back(tmp);
|
||||
|
||||
std::cout << "new job added : " << std::endl;
|
||||
@ -171,6 +175,8 @@ void Clusterizer::updateServer(void)
|
||||
while(didSomething)
|
||||
{
|
||||
didSomething = acceptClients();
|
||||
if(dispatchJobs())
|
||||
didSomething = 1;
|
||||
recv = poll(_pollfds, _clients.size(), 1);
|
||||
if(!recv)
|
||||
return ;
|
||||
@ -186,7 +192,5 @@ void Clusterizer::updateServer(void)
|
||||
}
|
||||
}
|
||||
}
|
||||
if(dispatchJobs())
|
||||
didSomething = 1;
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/01/22 16:34:53 by tomoron #+# #+# */
|
||||
/* Updated: 2025/02/24 00:49:53 by tomoron ### ########.fr */
|
||||
/* Updated: 2025/02/25 00:52:27 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -37,7 +37,7 @@ Renderer::Renderer(Scene *scene, Window *win, Arguments &args)
|
||||
|
||||
try{
|
||||
if(_headless)
|
||||
initRender();
|
||||
initRender(0);
|
||||
}
|
||||
catch(std::exception &e)
|
||||
{
|
||||
@ -136,7 +136,7 @@ void Renderer::addTeleport(glm::vec3 from_pos, glm::vec2 from_dir, glm::vec3 to
|
||||
|
||||
void Renderer::createClusterJobs(Clusterizer &clust)
|
||||
{
|
||||
float delta;
|
||||
double delta;
|
||||
size_t frames;
|
||||
glm::vec3 pos;
|
||||
glm::vec2 dir;
|
||||
@ -147,15 +147,15 @@ void Renderer::createClusterJobs(Clusterizer &clust)
|
||||
while(_destPathIndex)
|
||||
{
|
||||
interpolateMovement(delta * frames, &pos, &dir);
|
||||
clust.addJob(pos, dir, _samples);
|
||||
frames++;
|
||||
clust.addJob(pos, dir, _samples, frames, _scene->getDenoise());
|
||||
if(_curPathIndex == _destPathIndex)
|
||||
_destPathIndex = 0;
|
||||
(void)clust;
|
||||
frames++;
|
||||
}
|
||||
}
|
||||
|
||||
void Renderer::initRender(void)
|
||||
void Renderer::initRender(Clusterizer *clust)
|
||||
{
|
||||
if(_path.size() < 2)
|
||||
throw std::runtime_error("render path doesn't have enough path points");
|
||||
@ -167,10 +167,9 @@ void Renderer::initRender(void)
|
||||
_curPathIndex = 0;
|
||||
_destPathIndex = _path.size() - 1;
|
||||
_renderStartTime = glfwGetTime();
|
||||
// if(clust && clust->isServer())
|
||||
// createClusterJobs(*clust);
|
||||
// else
|
||||
if(1)
|
||||
if(clust && clust->isServer())
|
||||
createClusterJobs(*clust);
|
||||
else
|
||||
{
|
||||
_frameCount = 0;
|
||||
_curSamples = 0;
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/20 15:54:35 by tomoron #+# #+# */
|
||||
/* Updated: 2025/02/24 00:34:48 by tomoron ### ########.fr */
|
||||
/* Updated: 2025/02/24 17:42:35 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -171,7 +171,7 @@ void Renderer::imguiRenderSettings(Clusterizer &clust)
|
||||
try
|
||||
{
|
||||
if(ImGui::Button("start render"))
|
||||
initRender();
|
||||
initRender(&clust);
|
||||
ImGui::SameLine();
|
||||
if(ImGui::Button("save path"))
|
||||
savePath();
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/13 16:16:24 by TheRed #+# #+# */
|
||||
/* Updated: 2025/02/23 19:05:24 by tomoron ### ########.fr */
|
||||
/* Updated: 2025/02/25 01:51:46 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -24,6 +24,7 @@ Window::Window(Scene *scene, int width, int height, const char *title, int sleep
|
||||
_frameCount = 0;
|
||||
_pixelisation = 0;
|
||||
_renderer = new Renderer(scene, this, args);
|
||||
_clusterizer = new Clusterizer(args, _renderer);
|
||||
glfwSetErrorCallback(GLFWErrorCallback);
|
||||
if (!glfwInit())
|
||||
{
|
||||
@ -184,6 +185,11 @@ void Window::rendererUpdate(std::vector<GLuint> &textures, ShaderProgram &denoi
|
||||
_renderer->update(textures, denoisingProgram);
|
||||
}
|
||||
|
||||
void Window::clusterizerUpdate(std::vector<GLuint> &textures, ShaderProgram &denoisingProgram)
|
||||
{
|
||||
_clusterizer->update(*_scene, *this, textures, denoisingProgram);
|
||||
}
|
||||
|
||||
void Window::imGuiNewFrame()
|
||||
{
|
||||
ImGui_ImplOpenGL3_NewFrame();
|
||||
@ -191,7 +197,7 @@ void Window::imGuiNewFrame()
|
||||
ImGui::NewFrame();
|
||||
}
|
||||
|
||||
void Window::imGuiRender(ShaderProgram &raytracing_program, Clusterizer &clusterizer)
|
||||
void Window::imGuiRender(ShaderProgram &raytracing_program)
|
||||
{
|
||||
bool has_changed = false;
|
||||
|
||||
@ -317,8 +323,8 @@ void Window::imGuiRender(ShaderProgram &raytracing_program, Clusterizer &cluster
|
||||
}
|
||||
|
||||
|
||||
_renderer->renderImgui(clusterizer);
|
||||
clusterizer.imguiRender();
|
||||
_renderer->renderImgui(*_clusterizer);
|
||||
_clusterizer->imguiRender();
|
||||
|
||||
ImGui::End();
|
||||
|
||||
|
Reference in New Issue
Block a user