#pragma once
#include "EmberCLPch.h"
#include "OpenCLWrapper.h"
#include "IterOpenCLKernelCreator.h"
#include "DEOpenCLKernelCreator.h"
#include "FinalAccumOpenCLKernelCreator.h"
///
/// RendererCLBase and RendererCL classes.
///
namespace EmberCLns
{
///
/// Serves only as an interface for OpenCL specific rendering functions.
///
class EMBERCL_API RendererCLBase
{
public:
virtual bool ReadFinal(unsigned char* pixels) = 0;
virtual bool ClearFinal() = 0;
};
///
/// RendererCL is a derivation of the basic CPU renderer which
/// overrides various functions to render on the GPU using OpenCL.
/// Since this class derives from EmberReport and also contains an
/// OpenCLWrapper member which also derives from EmberReport, the
/// reporting functions are overridden to aggregate the errors from
/// both sources.
/// It does not support different types for T and bucketT, so it only has one template argument
/// and uses both for the base.
///
template
class EMBERCL_API RendererCL : public RendererCLBase, public Renderer
{
public:
RendererCL(unsigned int platform = 0, unsigned int device = 0, bool shared = false, GLuint outputTexID = 0);
~RendererCL();
//Non-virtual member functions for OpenCL specific tasks.
bool Init(unsigned int platform, unsigned int device, bool shared, GLuint outputTexID);
bool SetOutputTexture(GLuint outputTexID);
//Iters per kernel/block/grid.
inline unsigned int IterCountPerKernel() const;
inline unsigned int IterCountPerBlock() const;
inline unsigned int IterCountPerGrid() const;
//Kernels per block.
inline unsigned int IterBlockKernelWidth() const;
inline unsigned int IterBlockKernelHeight() const;
inline unsigned int IterBlockKernelCount() const;
//Kernels per grid.
inline unsigned int IterGridKernelWidth() const;
inline unsigned int IterGridKernelHeight() const;
inline unsigned int IterGridKernelCount() const;
//Blocks per grid.
inline unsigned int IterGridBlockWidth() const;
inline unsigned int IterGridBlockHeight() const;
inline unsigned int IterGridBlockCount() const;
unsigned int PlatformIndex();
unsigned int DeviceIndex();
bool ReadHist();
bool ReadAccum();
bool ReadPoints(vector>& vec);
bool ClearHist();
bool ClearAccum();
bool WritePoints(vector>& vec);
#ifdef TEST_CL
bool WriteRandomPoints();
#endif
string IterKernel();
//Virtual functions overridden from RendererCLBase.
virtual bool ReadFinal(unsigned char* pixels);
virtual bool ClearFinal();
//Public virtual functions overridden from Renderer or RendererBase.
virtual size_t MemoryAvailable() override;
virtual bool Ok() const override;
virtual void NumChannels(size_t numChannels) override;
virtual void DumpErrorReport() override;
virtual void ClearErrorReport() override;
virtual size_t SubBatchSize() const override;
virtual size_t ThreadCount() const override;
virtual bool CreateDEFilter(bool& newAlloc) override;
virtual bool CreateSpatialFilter(bool& newAlloc) override;
virtual eRendererType RendererType() const override;
virtual string ErrorReportString() override;
virtual vector ErrorReport() override;
virtual bool RandVec(vector>& randVec) override;
#ifndef TEST_CL
protected:
#endif
//Protected virtual functions overridden from Renderer.
virtual void MakeDmap(T colorScalar) override;
virtual bool Alloc() override;
virtual bool ResetBuckets(bool resetHist = true, bool resetAccum = true) override;
virtual eRenderStatus LogScaleDensityFilter() override;
virtual eRenderStatus GaussianDensityFilter() override;
virtual eRenderStatus AccumulatorToFinalImage(unsigned char* pixels, size_t finalOffset) override;
virtual EmberStats Iterate(size_t iterCount, size_t temporalSample) override;
private:
//Private functions for making and running OpenCL programs.
bool BuildIterProgramForEmber(bool doAccum = true);
bool RunIter(size_t iterCount, size_t temporalSample, size_t& itersRan);
eRenderStatus RunLogScaleFilter();
eRenderStatus RunDensityFilter();
eRenderStatus RunFinalAccum();
bool ClearBuffer(const string& bufferName, unsigned int width, unsigned int height, unsigned int elementSize);
bool RunDensityFilterPrivate(unsigned int kernelIndex, unsigned int gridW, unsigned int gridH, unsigned int blockW, unsigned int blockH, unsigned int chunkSizeW, unsigned int chunkSizeH, unsigned int chunkW, unsigned int chunkH);
int MakeAndGetDensityFilterProgram(size_t ss, unsigned int filterWidth);
int MakeAndGetFinalAccumProgram(T& alphaBase, T& alphaScale);
int MakeAndGetGammaCorrectionProgram();
void FillSeeds();
//Private functions passing data to OpenCL programs.
DensityFilterCL ConvertDensityFilter();
SpatialFilterCL ConvertSpatialFilter();
void ConvertEmber(Ember& ember, EmberCL& emberCL, vector>& xformsCL);
static CarToRasCL ConvertCarToRas(const CarToRas& carToRas);
bool m_Init;
bool m_NVidia;
bool m_DoublePrecision;
unsigned int m_IterCountPerKernel;
unsigned int m_IterBlocksWide, m_IterBlockWidth;
unsigned int m_IterBlocksHigh, m_IterBlockHeight;
unsigned int m_MaxDEBlockSizeW;
unsigned int m_MaxDEBlockSizeH;
unsigned int m_WarpSize;
size_t m_Calls;
//Buffer names.
string m_EmberBufferName;
string m_XformsBufferName;
string m_ParVarsBufferName;
string m_SeedsBufferName;
string m_DistBufferName;
string m_CarToRasBufferName;
string m_DEFilterParamsBufferName;
string m_SpatialFilterParamsBufferName;
string m_DECoefsBufferName;
string m_DEWidthsBufferName;
string m_DECoefIndicesBufferName;
string m_SpatialFilterCoefsBufferName;
string m_HistBufferName;
string m_AccumBufferName;
string m_FinalImageName;
string m_PointsBufferName;
//Kernels.
string m_IterKernel;
OpenCLWrapper m_Wrapper;
cl::ImageFormat m_PaletteFormat;
cl::ImageFormat m_FinalFormat;
cl::Image2D m_Palette;
IMAGEGL2D m_AccumImage;
GLuint m_OutputTexID;
EmberCL m_EmberCL;
vector> m_XformsCL;
vector m_Seeds;
Palette m_Dmap;//Used instead of the base class' m_Dmap because OpenCL only supports float textures.
CarToRasCL m_CarToRasCL;
DensityFilterCL m_DensityFilterCL;
SpatialFilterCL m_SpatialFilterCL;
IterOpenCLKernelCreator m_IterOpenCLKernelCreator;
DEOpenCLKernelCreator m_DEOpenCLKernelCreator;
FinalAccumOpenCLKernelCreator m_FinalAccumOpenCLKernelCreator;
pair> m_Params;
Ember m_LastBuiltEmber;
};
template EMBERCL_API class RendererCL;
#ifdef DO_DOUBLE
template EMBERCL_API class RendererCL;
#endif
}