mirror of
https://github.com/TheRedShip/RT_GPU.git
synced 2025-09-27 18:48:36 +02:00
server now receives images and create final video. server now handles clients disconnect
This commit is contained in:
@ -6,7 +6,7 @@
|
||||
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/20 21:08:38 by tomoron #+# #+# */
|
||||
/* Updated: 2025/02/25 01:48:47 by tomoron ### ########.fr */
|
||||
/* Updated: 2025/02/25 20:41:09 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -17,6 +17,7 @@ void shaderDenoise(ShaderProgram &denoising_program, GPUDenoise &denoise, st
|
||||
void Clusterizer::initClient(std::string &dest)
|
||||
{
|
||||
_serverFd = 0;
|
||||
_currentJob = 0;
|
||||
if(dest.find(":") == std::string::npos)
|
||||
std::cerr << "Client Initialisation error : invalid ip provided format must be <ip>:<port>" << std::endl;
|
||||
_serverIp = dest.substr(0, dest.find(":"));
|
||||
@ -90,9 +91,23 @@ void Clusterizer::clientHandleBuffer(void)
|
||||
if(_receiveBuffer[0] == JOB)
|
||||
clientGetJob();
|
||||
else if(_receiveBuffer[0] == RDY)
|
||||
{
|
||||
std::cout << "server is ready to receive" << std::endl;
|
||||
_srvReady = 1;
|
||||
else
|
||||
_receiveBuffer.erase(_receiveBuffer.begin());
|
||||
}
|
||||
else if(_receiveBuffer[0] == ABORT)
|
||||
{
|
||||
std::cout << "got a abort request, aborting current job";
|
||||
delete _currentJob;
|
||||
_currentJob = 0;
|
||||
_receiveBuffer.erase(_receiveBuffer.begin());
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "unknown request sent, ignoring" << std::endl;
|
||||
_receiveBuffer.erase(_receiveBuffer.begin());
|
||||
}
|
||||
|
||||
|
||||
if(sendBuf.size())
|
||||
@ -105,15 +120,16 @@ void Clusterizer::clientReceive(void)
|
||||
size_t ret;
|
||||
|
||||
ret = recv(_serverFd, buffer, 512, MSG_DONTWAIT);
|
||||
if(ret == (size_t)-1)
|
||||
return;
|
||||
if(!ret)
|
||||
if(ret != (size_t)-1)
|
||||
{
|
||||
close(_serverFd);
|
||||
_serverFd = 0;
|
||||
return ;
|
||||
if(!ret)
|
||||
{
|
||||
close(_serverFd);
|
||||
_serverFd = 0;
|
||||
return ;
|
||||
}
|
||||
_receiveBuffer.insert(_receiveBuffer.end(), buffer, buffer + ret);
|
||||
}
|
||||
_receiveBuffer.insert(_receiveBuffer.end(), buffer, buffer + ret);
|
||||
clientHandleBuffer();
|
||||
}
|
||||
|
||||
@ -127,26 +143,62 @@ void Clusterizer::sendProgress(uint8_t progress)
|
||||
(void)write(_serverFd, buf, 2);
|
||||
}
|
||||
|
||||
std::vector<uint8_t> Clusterizer::rgb32fToRgb24i(std::vector<float> &imageFloat)
|
||||
{
|
||||
std::vector<uint8_t> buffer(WIDTH * HEIGHT * 3);
|
||||
size_t rgbaIndex;
|
||||
size_t rgbIndex;
|
||||
|
||||
for(size_t y = 0; y < HEIGHT; y++)
|
||||
{
|
||||
for(size_t x = 0; x < WIDTH; x++)
|
||||
{
|
||||
rgbaIndex = (((HEIGHT - 1 - y) * WIDTH) + x) * 4;
|
||||
rgbIndex = ((y * WIDTH) + x) * 3;
|
||||
buffer[rgbIndex] = fmin(imageFloat[rgbaIndex], 1) * 254;
|
||||
buffer[rgbIndex + 1] = fmin(imageFloat[rgbaIndex + 1], 1) * 254;
|
||||
buffer[rgbIndex + 2] = fmin(imageFloat[rgbaIndex + 2], 1) * 254;
|
||||
rgbIndex += 3;
|
||||
rgbaIndex += 4;
|
||||
}
|
||||
}
|
||||
return(buffer);
|
||||
}
|
||||
|
||||
void Clusterizer::sendImageToServer(std::vector<GLuint> &textures, ShaderProgram &denoisingProgram)
|
||||
{
|
||||
_srvReady = 0;
|
||||
std::vector<uint8_t> buffer(WIDTH * HEIGHT * 4);
|
||||
std::vector<float> imageFloat(WIDTH * HEIGHT * 4);
|
||||
std::vector<uint8_t> buffer;
|
||||
|
||||
(void)write(_serverFd, (uint8_t []){IMG_SEND_RQ}, 1);
|
||||
while(!_srvReady)
|
||||
{
|
||||
clientReceive();
|
||||
if(!_serverFd)
|
||||
{
|
||||
delete _currentJob;
|
||||
_currentJob = 0;
|
||||
}
|
||||
if(!_currentJob)
|
||||
return ;
|
||||
usleep(10000);
|
||||
}
|
||||
std::cout << "server ready" << std::endl;
|
||||
|
||||
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());
|
||||
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, imageFloat.data());
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
|
||||
buffer = rgb32fToRgb24i(imageFloat);
|
||||
std::cout << "buffer size : " << buffer.size() << std::endl;
|
||||
(void)write(_serverFd, (uint8_t []){IMG}, 1);
|
||||
(void)write(_serverFd, buffer.data(), buffer.size());
|
||||
delete _currentJob;
|
||||
_currentJob = 0;
|
||||
std::cout << "image sent" << std::endl;
|
||||
}
|
||||
|
||||
void Clusterizer::handleCurrentJob(Scene &scene, Window &win, std::vector<GLuint> &textures, ShaderProgram &denoisingProgram)
|
||||
@ -157,23 +209,34 @@ void Clusterizer::handleCurrentJob(Scene &scene, Window &win, std::vector<GLuint
|
||||
|
||||
if(scene.getCamera()->getPosition() != _currentJob->pos || scene.getCamera()->getDirection() != _currentJob->dir)
|
||||
{
|
||||
std::cout << "not at right place, moving" << std::endl;
|
||||
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)
|
||||
|
||||
if((size_t)win.getFrameCount() <= _currentJob->samples)
|
||||
{
|
||||
progress = ((double)win.getFrameCount() / _currentJob->samples) * 100;
|
||||
if(progress != _progress)
|
||||
{
|
||||
std::cout << "new progress" << std::endl;
|
||||
sendProgress(progress);
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "sample " << win.getFrameCount() << "/" << _currentJob->samples << std::endl;
|
||||
if((size_t)win.getFrameCount() > _currentJob->samples)
|
||||
win.setFrameCount(0);
|
||||
|
||||
if((size_t)win.getFrameCount() == _currentJob->samples)
|
||||
{
|
||||
std::cout << "send request" << std::endl;
|
||||
sendImageToServer(textures, denoisingProgram);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void Clusterizer::updateClient(Scene &scene, Window &win, std::vector<GLuint> &textures, ShaderProgram &denoisingProgram)
|
||||
|
Reference in New Issue
Block a user