mirror of
https://github.com/TheRedShip/RT_GPU.git
synced 2025-09-28 11:08:36 +02:00
server now sends jobs to client and client can send result image to server
This commit is contained in:
@ -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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user