You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
137 lines
3.0 KiB
137 lines
3.0 KiB
5 years ago
|
#include "model/processor/strategies/Persist.h"
|
||
|
#include "model/processor/BIP.h"
|
||
|
#include <QPixmap>
|
||
|
#include <QImage>
|
||
|
|
||
|
Persist::Persist(const Context context,
|
||
|
const QString kernelPath,
|
||
|
const QObject *parent = Q_NULLPTR) :
|
||
|
IProcessStrategy(context, kernelPath, "Persist", parent),
|
||
|
_kernelFunctor(KernelFunctor<Image2D, Image2D,
|
||
|
Image2D, Image2D, Persist_t>(_kernel))
|
||
|
{
|
||
|
memset(&_kernelParameters, 0, sizeof (Persist_t));
|
||
|
_imageSum = Q_NULLPTR;
|
||
|
_reCalc = true;
|
||
|
}
|
||
|
|
||
|
void Persist::cpuProcess(ScenGenOutput_t parameters)
|
||
|
{
|
||
|
if(parameters.persist.isUpdated)
|
||
|
{
|
||
|
_kernelParameters.persist = parameters.persist.value;
|
||
|
_reCalc = true;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void Persist::finalize()
|
||
|
{
|
||
|
foreach (auto frame, _queue)
|
||
|
{
|
||
|
delete frame;
|
||
|
}
|
||
|
_queue.clear();
|
||
|
|
||
|
if(_imageSum)
|
||
|
delete _imageSum;
|
||
|
}
|
||
|
|
||
|
|
||
|
Image* Persist::processKernel(Image *frames, Buffer* scratchPad)
|
||
|
{
|
||
|
Image* temp;
|
||
|
|
||
|
if(_queue.length() < PERSIST_MAX_SIZE)
|
||
|
{
|
||
|
_queue.push_front(frames);
|
||
|
return frames;
|
||
|
}
|
||
|
|
||
|
if(_kernelParameters.persist != 1)
|
||
|
{
|
||
|
auto format = frames->getImageInfo<CL_IMAGE_FORMAT>();
|
||
|
auto width = frames->getImageInfo<CL_IMAGE_WIDTH>();
|
||
|
auto height = frames->getImageInfo<CL_IMAGE_HEIGHT>();
|
||
|
|
||
|
//if width or height changes we should erase everything
|
||
|
if(_lastWidth != width || _lastHeight != height)
|
||
|
{
|
||
|
_reCalc = false;
|
||
|
|
||
|
if(_imageSum)
|
||
|
delete _imageSum;
|
||
|
|
||
|
_imageSum = new Image2D(_CLContext,
|
||
|
CL_MEM_READ_WRITE,
|
||
|
ImageFormat(format.image_channel_order, format.image_channel_data_type),
|
||
|
width,
|
||
|
height);
|
||
|
|
||
|
foreach (auto frame, _queue)
|
||
|
{
|
||
|
delete frame;
|
||
|
}
|
||
|
_queue.clear();
|
||
|
|
||
|
_lastWidth = width;
|
||
|
_lastHeight = height;
|
||
|
}
|
||
|
|
||
|
|
||
|
auto outputImage = new Image2D(_CLContext,
|
||
|
CL_MEM_READ_WRITE,
|
||
|
ImageFormat(format.image_channel_order, format.image_channel_data_type),
|
||
|
width,
|
||
|
height);
|
||
|
|
||
|
|
||
|
cl::EnqueueArgs eargs(BIP::getInstance()->CLQueue, cl::NDRange(width, height));
|
||
|
|
||
|
if(_reCalc)
|
||
|
{
|
||
|
_reCalc = false;
|
||
|
for(auto i = 0; i < _kernelParameters.persist; i++)
|
||
|
{
|
||
|
if(i == 0)
|
||
|
_kernelParameters.mode = 0;
|
||
|
else if(i < _kernelParameters.persist)
|
||
|
_kernelParameters.mode = 1;
|
||
|
|
||
|
_openCLHelper.runKernelFunctor<Image2D, Image2D, Image2D, Image2D, Persist_t>(_kernelFunctor,
|
||
|
eargs,
|
||
|
*static_cast<Image2D*>(_queue[i]),
|
||
|
*outputImage,
|
||
|
*static_cast<Image2D*>(_imageSum),
|
||
|
*static_cast<Image2D*>(_queue[_kernelParameters.persist - 1]),
|
||
|
_kernelParameters);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
_kernelParameters.mode = 2;
|
||
|
_openCLHelper.runKernelFunctor<Image2D, Image2D, Image2D, Image2D, Persist_t>(_kernelFunctor,
|
||
|
eargs,
|
||
|
*static_cast<Image2D*>(frames),
|
||
|
*outputImage,
|
||
|
*static_cast<Image2D*>(_imageSum),
|
||
|
*static_cast<Image2D*>(_queue[_kernelParameters.persist - 1]),
|
||
|
_kernelParameters);
|
||
|
|
||
|
temp = outputImage;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
temp = frames;
|
||
|
}
|
||
|
|
||
|
delete _queue[PERSIST_MAX_SIZE - 1];
|
||
|
for(auto i = 0; i < PERSIST_MAX_SIZE - 1; i++)
|
||
|
{
|
||
|
_queue[i + 1] = _queue[i];
|
||
|
}
|
||
|
|
||
|
_queue[0] = frames;
|
||
|
|
||
|
|
||
|
return temp;
|
||
|
}
|