2014-07-08 03:11:14 -04:00
# pragma once
# include "EmberCLPch.h"
# include "OpenCLWrapper.h"
# include "IterOpenCLKernelCreator.h"
# include "DEOpenCLKernelCreator.h"
# include "FinalAccumOpenCLKernelCreator.h"
/// <summary>
2014-10-27 17:21:06 -04:00
/// RendererCLBase and RendererCL classes.
2014-07-08 03:11:14 -04:00
/// </summary>
namespace EmberCLns
{
2014-10-27 17:21:06 -04:00
/// <summary>
/// Serves only as an interface for OpenCL specific rendering functions.
/// </summary>
2014-07-08 03:11:14 -04:00
class EMBERCL_API RendererCLBase
{
public :
2015-01-14 08:10:25 -05:00
virtual ~ RendererCLBase ( ) { }
2014-12-06 00:05:09 -05:00
virtual bool ReadFinal ( byte * pixels ) = 0 ;
2014-10-14 11:53:15 -04:00
virtual bool ClearFinal ( ) = 0 ;
2014-07-08 03:11:14 -04:00
} ;
/// <summary>
/// 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.
/// </summary>
2015-08-10 23:10:23 -04:00
template < typename T , typename bucketT >
class EMBERCL_API RendererCL : public Renderer < T , bucketT > , public RendererCLBase
2014-07-08 03:11:14 -04:00
{
2015-08-10 23:10:23 -04:00
using EmberNs : : Renderer < T , bucketT > : : RendererBase : : Abort ;
using EmberNs : : Renderer < T , bucketT > : : RendererBase : : EarlyClip ;
using EmberNs : : Renderer < T , bucketT > : : RendererBase : : Transparency ;
using EmberNs : : Renderer < T , bucketT > : : RendererBase : : EnterResize ;
using EmberNs : : Renderer < T , bucketT > : : RendererBase : : LeaveResize ;
using EmberNs : : Renderer < T , bucketT > : : RendererBase : : FinalRasW ;
using EmberNs : : Renderer < T , bucketT > : : RendererBase : : FinalRasH ;
using EmberNs : : Renderer < T , bucketT > : : RendererBase : : SuperRasW ;
using EmberNs : : Renderer < T , bucketT > : : RendererBase : : SuperRasH ;
using EmberNs : : Renderer < T , bucketT > : : RendererBase : : SuperSize ;
using EmberNs : : Renderer < T , bucketT > : : RendererBase : : BytesPerChannel ;
using EmberNs : : Renderer < T , bucketT > : : RendererBase : : TemporalSamples ;
using EmberNs : : Renderer < T , bucketT > : : RendererBase : : ItersPerTemporalSample ;
using EmberNs : : Renderer < T , bucketT > : : RendererBase : : FuseCount ;
using EmberNs : : Renderer < T , bucketT > : : RendererBase : : DensityFilterOffset ;
using EmberNs : : Renderer < T , bucketT > : : RendererBase : : m_ProgressParameter ;
using EmberNs : : Renderer < T , bucketT > : : RendererBase : : m_YAxisUp ;
using EmberNs : : Renderer < T , bucketT > : : RendererBase : : m_LockAccum ;
using EmberNs : : Renderer < T , bucketT > : : RendererBase : : m_Abort ;
using EmberNs : : Renderer < T , bucketT > : : RendererBase : : m_NumChannels ;
using EmberNs : : Renderer < T , bucketT > : : RendererBase : : m_LastIter ;
using EmberNs : : Renderer < T , bucketT > : : RendererBase : : m_LastIterPercent ;
using EmberNs : : Renderer < T , bucketT > : : RendererBase : : m_Stats ;
using EmberNs : : Renderer < T , bucketT > : : RendererBase : : m_Callback ;
using EmberNs : : Renderer < T , bucketT > : : RendererBase : : m_Rand ;
using EmberNs : : Renderer < T , bucketT > : : RendererBase : : m_RenderTimer ;
using EmberNs : : Renderer < T , bucketT > : : RendererBase : : m_IterTimer ;
using EmberNs : : Renderer < T , bucketT > : : RendererBase : : m_ProgressTimer ;
using EmberNs : : Renderer < T , bucketT > : : RendererBase : : EmberReport : : m_ErrorReport ;
using EmberNs : : Renderer < T , bucketT > : : m_RotMat ;
using EmberNs : : Renderer < T , bucketT > : : m_Ember ;
using EmberNs : : Renderer < T , bucketT > : : m_Csa ;
using EmberNs : : Renderer < T , bucketT > : : m_CurvesSet ;
using EmberNs : : Renderer < T , bucketT > : : CenterX ;
using EmberNs : : Renderer < T , bucketT > : : CenterY ;
using EmberNs : : Renderer < T , bucketT > : : K1 ;
using EmberNs : : Renderer < T , bucketT > : : K2 ;
using EmberNs : : Renderer < T , bucketT > : : Supersample ;
using EmberNs : : Renderer < T , bucketT > : : HighlightPower ;
using EmberNs : : Renderer < T , bucketT > : : HistBuckets ;
using EmberNs : : Renderer < T , bucketT > : : AccumulatorBuckets ;
using EmberNs : : Renderer < T , bucketT > : : GetDensityFilter ;
using EmberNs : : Renderer < T , bucketT > : : GetSpatialFilter ;
using EmberNs : : Renderer < T , bucketT > : : CoordMap ;
using EmberNs : : Renderer < T , bucketT > : : XformDistributions ;
using EmberNs : : Renderer < T , bucketT > : : XformDistributionsSize ;
2014-12-05 21:30:46 -05:00
2014-07-08 03:11:14 -04:00
public :
2014-12-06 00:05:09 -05:00
RendererCL ( uint platform = 0 , uint device = 0 , bool shared = false , GLuint outputTexID = 0 ) ;
2015-01-14 08:10:25 -05:00
~ RendererCL ( ) ;
2014-07-08 03:11:14 -04:00
2014-10-14 11:53:15 -04:00
//Non-virtual member functions for OpenCL specific tasks.
2014-12-06 00:05:09 -05:00
bool Init ( uint platform , uint device , bool shared , GLuint outputTexID ) ;
2014-10-14 11:53:15 -04:00
bool SetOutputTexture ( GLuint outputTexID ) ;
2014-11-28 04:37:51 -05:00
//Iters per kernel/block/grid.
2014-12-06 00:05:09 -05:00
inline uint IterCountPerKernel ( ) const ;
inline uint IterCountPerBlock ( ) const ;
inline uint IterCountPerGrid ( ) const ;
2014-11-28 04:37:51 -05:00
//Kernels per block.
2014-12-06 00:05:09 -05:00
inline uint IterBlockKernelWidth ( ) const ;
inline uint IterBlockKernelHeight ( ) const ;
inline uint IterBlockKernelCount ( ) const ;
2014-11-28 04:37:51 -05:00
//Kernels per grid.
2014-12-06 00:05:09 -05:00
inline uint IterGridKernelWidth ( ) const ;
inline uint IterGridKernelHeight ( ) const ;
inline uint IterGridKernelCount ( ) const ;
2014-11-28 04:37:51 -05:00
//Blocks per grid.
2014-12-06 00:05:09 -05:00
inline uint IterGridBlockWidth ( ) const ;
inline uint IterGridBlockHeight ( ) const ;
inline uint IterGridBlockCount ( ) const ;
2014-11-28 04:37:51 -05:00
2014-12-06 00:05:09 -05:00
uint PlatformIndex ( ) ;
uint DeviceIndex ( ) ;
2014-07-08 03:11:14 -04:00
bool ReadHist ( ) ;
bool ReadAccum ( ) ;
bool ReadPoints ( vector < PointCL < T > > & vec ) ;
bool ClearHist ( ) ;
bool ClearAccum ( ) ;
bool WritePoints ( vector < PointCL < T > > & vec ) ;
2014-11-28 04:37:51 -05:00
# ifdef TEST_CL
bool WriteRandomPoints ( ) ;
# endif
2014-07-08 03:11:14 -04:00
string IterKernel ( ) ;
2015-03-21 18:27:37 -04:00
string DEKernel ( ) ;
string FinalAccumKernel ( ) ;
2014-07-08 03:11:14 -04:00
2014-10-14 11:53:15 -04:00
//Virtual functions overridden from RendererCLBase.
2014-12-06 00:05:09 -05:00
virtual bool ReadFinal ( byte * pixels ) ;
2014-10-14 11:53:15 -04:00
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 < string > ErrorReport ( ) override ;
2014-11-29 12:44:23 -05:00
virtual bool RandVec ( vector < QTIsaac < ISAAC_SIZE , ISAAC_INT > > & randVec ) override ;
2014-07-08 03:11:14 -04:00
# ifndef TEST_CL
protected :
# endif
2014-10-14 11:53:15 -04:00
//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 ;
2014-12-06 00:05:09 -05:00
virtual eRenderStatus AccumulatorToFinalImage ( byte * pixels , size_t finalOffset ) override ;
2014-11-03 02:16:34 -05:00
virtual EmberStats Iterate ( size_t iterCount , size_t temporalSample ) override ;
2014-07-08 03:11:14 -04:00
2015-03-22 15:46:10 -04:00
# ifndef TEST_CL
2014-07-08 03:11:14 -04:00
private :
2015-03-22 15:46:10 -04:00
# endif
2014-07-08 03:11:14 -04:00
//Private functions for making and running OpenCL programs.
bool BuildIterProgramForEmber ( bool doAccum = true ) ;
2014-11-03 02:16:34 -05:00
bool RunIter ( size_t iterCount , size_t temporalSample , size_t & itersRan ) ;
2014-07-08 03:11:14 -04:00
eRenderStatus RunLogScaleFilter ( ) ;
eRenderStatus RunDensityFilter ( ) ;
eRenderStatus RunFinalAccum ( ) ;
2014-12-06 00:05:09 -05:00
bool ClearBuffer ( const string & bufferName , uint width , uint height , uint elementSize ) ;
bool RunDensityFilterPrivate ( uint kernelIndex , uint gridW , uint gridH , uint blockW , uint blockH , uint chunkSizeW , uint chunkSizeH , uint chunkW , uint chunkH ) ;
int MakeAndGetDensityFilterProgram ( size_t ss , uint filterWidth ) ;
2015-08-10 23:10:23 -04:00
int MakeAndGetFinalAccumProgram ( double & alphaBase , double & alphaScale ) ;
2014-07-08 03:11:14 -04:00
int MakeAndGetGammaCorrectionProgram ( ) ;
2014-11-29 12:44:23 -05:00
void FillSeeds ( ) ;
2014-07-08 03:11:14 -04:00
//Private functions passing data to OpenCL programs.
2015-08-10 23:10:23 -04:00
void ConvertDensityFilter ( ) ;
void ConvertSpatialFilter ( ) ;
2014-11-28 04:37:51 -05:00
void ConvertEmber ( Ember < T > & ember , EmberCL < T > & emberCL , vector < XformCL < T > > & xformsCL ) ;
2014-07-08 03:11:14 -04:00
static CarToRasCL < T > ConvertCarToRas ( const CarToRas < T > & carToRas ) ;
bool m_Init ;
bool m_NVidia ;
bool m_DoublePrecision ;
2014-12-06 00:05:09 -05:00
uint m_IterCountPerKernel ;
uint m_IterBlocksWide , m_IterBlockWidth ;
uint m_IterBlocksHigh , m_IterBlockHeight ;
uint m_MaxDEBlockSizeW ;
uint m_MaxDEBlockSizeH ;
uint m_WarpSize ;
2014-10-14 11:53:15 -04:00
size_t m_Calls ;
2014-07-08 03:11:14 -04:00
2014-10-14 11:53:15 -04:00
//Buffer names.
2014-07-08 03:11:14 -04:00
string m_EmberBufferName ;
2014-11-28 04:37:51 -05:00
string m_XformsBufferName ;
2014-07-08 03:11:14 -04:00
string m_ParVarsBufferName ;
2014-11-28 04:37:51 -05:00
string m_SeedsBufferName ;
2014-07-08 03:11:14 -04:00
string m_DistBufferName ;
string m_CarToRasBufferName ;
string m_DEFilterParamsBufferName ;
string m_SpatialFilterParamsBufferName ;
2015-03-21 18:27:37 -04:00
string m_CurvesCsaName ;
2014-07-08 03:11:14 -04:00
string m_DECoefsBufferName ;
string m_DEWidthsBufferName ;
string m_DECoefIndicesBufferName ;
string m_SpatialFilterCoefsBufferName ;
string m_HistBufferName ;
string m_AccumBufferName ;
string m_FinalImageName ;
string m_PointsBufferName ;
2014-10-14 11:53:15 -04:00
//Kernels.
2014-07-08 03:11:14 -04:00
string m_IterKernel ;
2014-12-05 21:30:46 -05:00
2014-07-08 03:11:14 -04:00
OpenCLWrapper m_Wrapper ;
cl : : ImageFormat m_PaletteFormat ;
cl : : ImageFormat m_FinalFormat ;
cl : : Image2D m_Palette ;
IMAGEGL2D m_AccumImage ;
GLuint m_OutputTexID ;
EmberCL < T > m_EmberCL ;
2014-11-28 04:37:51 -05:00
vector < XformCL < T > > m_XformsCL ;
vector < glm : : highp_uvec2 > m_Seeds ;
2015-08-10 23:10:23 -04:00
Palette < float > m_DmapCL ; //Used instead of the base class' m_Dmap because OpenCL only supports float textures. Likely not needed if we switch to float only hist.
2014-07-08 03:11:14 -04:00
CarToRasCL < T > m_CarToRasCL ;
2015-08-10 23:10:23 -04:00
DensityFilterCL < bucketT > m_DensityFilterCL ;
SpatialFilterCL < bucketT > m_SpatialFilterCL ;
2014-07-08 03:11:14 -04:00
IterOpenCLKernelCreator < T > m_IterOpenCLKernelCreator ;
2015-08-10 23:10:23 -04:00
DEOpenCLKernelCreator m_DEOpenCLKernelCreator ;
FinalAccumOpenCLKernelCreator m_FinalAccumOpenCLKernelCreator ;
2014-07-08 03:11:14 -04:00
pair < string , vector < T > > m_Params ;
Ember < T > m_LastBuiltEmber ;
} ;
2014-12-05 21:30:46 -05:00
}