/*==LICENSE==*

CyanWorlds.com Engine - MMOG client, server and tools
Copyright (C) 2011 Cyan Worlds, Inc.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.

Additional permissions under GNU GPL version 3 section 7

If you modify this Program, or any covered work, by linking or
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK,
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK
(or a modified version of those libraries),
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA,
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the
licensors of this Program grant you additional
permission to convey the resulting work. Corresponding Source for a
non-source form of such a combination shall include the source code for
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
work.

You can contact Cyan Worlds, Inc. by email legal@cyan.com
 or by snail mail at:
      Cyan Worlds, Inc.
      14617 N Newport Hwy
      Mead, WA   99021

*==LICENSE==*/
///////////////////////////////////////////////////////////////////////////////
//																			 //
//	hsG3DDeviceSelector Class Header             							 //
//	Generic device enumeration (D3D, OpenGL, etc)							 //
//	Cyan, Inc.																 //
//																			 //
//// Version History //////////////////////////////////////////////////////////
//																			 //
//	5.21.2001 mcn - Cleaned out all the old Glide stuff, since Plasma2 will	 //
//					not support Glide :(       								 //
//																			 //
///////////////////////////////////////////////////////////////////////////////

#ifndef hsG3DDeviceSelector_inc
#define hsG3DDeviceSelector_inc

#include "hsWinRef.h"

#include "hsTemplates.h"
#include "hsBitVector.h"

#ifdef HS_BUILD_FOR_WIN32
#define HS_SELECT_DIRECT3D // not supported on the Mac.
#endif // HS_BUILD_FOR_WIN32

#ifdef HS_BUILD_FOR_WIN32
#define __MSC__
#define DYNAHEADER 1
#endif // HS_BUILD_FOR_WIN32


class hsStream;
struct D3DEnum_DeviceInfo;
struct D3DEnum_DriverInfo;
struct D3DEnum_DeviceInfo;
struct D3DEnum_DriverInfo;

class hsG3DDeviceMode
{
	enum {
		kNone			= 0x0,
		kDiscarded		= 0x1
	};
protected:
	UInt32				fFlags;

	UInt32				fWidth;
	UInt32				fHeight;
	UInt32				fDepth;

	hsTArray<UInt16>	fZStencilDepths;	// Array of supported depth/stencil buffer formats.
											// Each entry is of the form: ( stencil bit count << 8 ) | ( depth bit count )
	hsTArray<UInt8>		fFSAATypes;			// Array of multisample types supported (each one 2-16)

	hsBool				fCanRenderToCubics;

public:
	hsG3DDeviceMode();
	~hsG3DDeviceMode();

	hsBool operator< (const hsG3DDeviceMode &mode) const;

	void Clear();

	hsBool GetDiscarded() const { return 0 != (fFlags & kDiscarded); }
	UInt32 GetWidth() const { return fWidth; }
	UInt32 GetHeight() const { return fHeight; }
	UInt32 GetColorDepth() const { return fDepth; }
	UInt8	GetNumZStencilDepths( void ) const { return fZStencilDepths.GetCount(); }
	UInt16	GetZStencilDepth( UInt8 i ) const { return fZStencilDepths[ i ]; }
	UInt8	GetNumFSAATypes( void ) const { return fFSAATypes.GetCount(); }
	UInt8	GetFSAAType( UInt8 i ) const { return fFSAATypes[ i ]; }
	hsBool	GetCanRenderToCubics( void ) const { return fCanRenderToCubics; }

	void SetDiscarded(hsBool on=true) { if(on) fFlags |= kDiscarded; else fFlags &= ~kDiscarded; }
	void SetWidth(UInt32 w) { fWidth = w; }
	void SetHeight(UInt32 h) { fHeight = h; }
	void SetColorDepth(UInt32 d) { fDepth = d; }
	void	ClearZStencilDepths( void ) { fZStencilDepths.Reset(); }
	void	AddZStencilDepth( UInt16 depth ) { fZStencilDepths.Append( depth ); }

	void	ClearFSAATypes( void ) { fFSAATypes.Reset(); }
	void	AddFSAAType( UInt8 type ) { fFSAATypes.Append( type ); }

	void	SetCanRenderToCubics( hsBool can ) { fCanRenderToCubics = can; }
};

class hsG3DDeviceRecord
{
public:
	enum {
		kNone			= 0x0,
		kDiscarded		= 0x1,
		kInvalid		= 0x2
	};

	enum FogTypes {
		kFogExp = 0,
		kFogExp2,
		kNumFogTypes
	};

protected:

	UInt32			fFlags;

	UInt32			fG3DDeviceType;
	UInt32			fG3DHALorHEL;


	char*			fG3DDriverDesc;
	char*			fG3DDriverName;
	char*			fG3DDriverVersion;
	char*			fG3DDeviceDesc;

	hsBitVector			fCaps;
	UInt32				fLayersAtOnce;
	UInt32				fMemoryBytes;

	hsTArray<hsG3DDeviceMode> fModes;

	float	fZBiasRating;
	float	fLODBiasRating;
	float	fFogExpApproxStart;
	float	fFogExp2ApproxStart;
	float	fFogEndBias;	// As a percentage of the max value for fog
							// (i.e. for Z fog, it's a percentage of 1 to add on,
							// for W fog, it's a percentage of the yon)

	float	fFogKnees[ kNumFogTypes ];
	float	fFogKneeVals[ kNumFogTypes ];

	UInt8	fAASetting;

	UInt8	fMaxAnisotropicSamples;	// 1 to disable, up to max allowed in hardware
	int fPixelShaderMajorVer;
	int fPixelShaderMinorVer;

public:
	hsG3DDeviceRecord();
	virtual ~hsG3DDeviceRecord();

	hsG3DDeviceRecord(const hsG3DDeviceRecord& src);
	hsG3DDeviceRecord& operator=(const hsG3DDeviceRecord& src);

	UInt32	GetG3DDeviceType() const { return fG3DDeviceType; }
	const char* GetG3DDeviceTypeName() const;
	UInt32	GetG3DHALorHEL() const { return fG3DHALorHEL; }

	UInt32 GetMemoryBytes() const { return fMemoryBytes; }

	const char* GetDriverDesc() const { return fG3DDriverDesc; }
	const char* GetDriverName() const { return fG3DDriverName; }
	const char* GetDriverVersion() const { return fG3DDriverVersion; }
	const char* GetDeviceDesc() const { return fG3DDeviceDesc; }

	void SetG3DDeviceType(UInt32 t) { fG3DDeviceType = t; }
	void SetG3DHALorHEL(UInt32 h) { fG3DHALorHEL = h; }
	void SetMemoryBytes(UInt32 b) { fMemoryBytes = b; }

	void SetDriverDesc(const char* s);
	void SetDriverName(const char* s);
	void SetDriverVersion(const char* s);
	void SetDeviceDesc(const char* s);

	hsBool	GetCap(UInt32 cap) const { return fCaps.IsBitSet(cap); }
	void	SetCap(UInt32 cap, hsBool on=true) { fCaps.SetBit(cap, on); }

	float	GetZBiasRating( void ) const { return fZBiasRating; }
	void	SetZBiasRating( float rating ) { fZBiasRating = rating; }

	float	GetLODBiasRating( void ) const { return fLODBiasRating; }
	void	SetLODBiasRating( float rating ) { fLODBiasRating = rating; }

	void	GetFogApproxStarts( float &expApprox, float &exp2Approx ) const { expApprox = fFogExpApproxStart;
																			exp2Approx = fFogExp2ApproxStart; }
	void	SetFogApproxStarts( float exp, float exp2 ) { fFogExpApproxStart = exp; 
																fFogExp2ApproxStart = exp2; }

	float	GetFogEndBias( void ) const { return fFogEndBias; }
	void	SetFogEndBias( float rating ) { fFogEndBias = rating; }

	void	GetFogKneeParams( UInt8 type, float &knee, float &kneeVal ) const { knee = fFogKnees[ type ]; kneeVal = fFogKneeVals[ type ]; }
	void	SetFogKneeParams( UInt8 type, float knee, float kneeVal ) { fFogKnees[ type ] = knee; fFogKneeVals[ type ] = kneeVal; }

	UInt32	GetLayersAtOnce() const { return fLayersAtOnce; }
	void	SetLayersAtOnce(UInt32 n) { fLayersAtOnce = n; }

	UInt8	GetAASetting() const { return fAASetting; }
	void	SetAASetting( UInt8 s ) { fAASetting = s; }

	UInt8	GetMaxAnisotropicSamples( void ) const { return fMaxAnisotropicSamples; }
	void	SetMaxAnisotropicSamples( UInt8 num ) { fMaxAnisotropicSamples = num; }

	void SetDiscarded(hsBool on=true) { if(on)fFlags |= kDiscarded; else fFlags &= ~kDiscarded; }
	hsBool GetDiscarded() const { return 0 != (fFlags & kDiscarded); }

	void	SetInvalid( hsBool on = true ) { if( on ) fFlags |= kInvalid; else fFlags &= ~kInvalid; }
	hsBool	IsInvalid() const { return 0 != ( fFlags & kInvalid ); }

	hsTArray<hsG3DDeviceMode>& GetModes() { return fModes; }

	hsG3DDeviceMode* GetMode(int i) const { return &fModes[i]; }

	void ClearModes();
	void Clear();
	void RemoveDiscarded();

	void SetPixelShaderVersion(int major, int minor) { fPixelShaderMajorVer = major; fPixelShaderMinorVer = minor; }
	void GetPixelShaderVersion(int &major, int &minor) { major = fPixelShaderMajorVer; minor = fPixelShaderMinorVer; }
};

class hsG3DDeviceModeRecord
{
protected:
	hsG3DDeviceRecord		fDevice;
	hsG3DDeviceMode			fMode;
public:
	hsG3DDeviceModeRecord();
	hsG3DDeviceModeRecord(const hsG3DDeviceRecord& devRec, const hsG3DDeviceMode& devMode);
	~hsG3DDeviceModeRecord();

	hsG3DDeviceModeRecord(const hsG3DDeviceModeRecord& src);
	hsG3DDeviceModeRecord& operator=(const hsG3DDeviceModeRecord& src);

	const hsG3DDeviceRecord*	GetDevice() const { return &fDevice; }
	const hsG3DDeviceMode*		GetMode() const { return &fMode; }
};

class hsG3DDeviceSelector : public hsRefCnt
{
public:
	enum {
		kDevTypeUnknown		= 0,
		kDevTypeDirect3D,
		kDevTypeOpenGL,

		kNumDevTypes
	};
	enum {
		kHHTypeUnknown      = 0,
		kHHD3DNullDev,
		kHHD3DHALDev,
		kHHD3DTnLHalDev,
		kHHD3DRefDev,

		kNumHHTypes
	};
	enum {
		kCapsNone			= 0,
		kCapsMipmap,
		kCapsPerspective,
		kCapsHardware,
		kCapsCompressTextures,
		kCapsHWTransform,
		kCapsFogLinear,
		kCapsFogExp,
		kCapsFogExp2,
		kCapsFogRange,
		kCapsDoesSmallTextures,
		kCapsPixelFog,
		kCapsBadYonStuff,
		kCapsCubicTextures,
		kCapsCubicMipmap,
		kCapsZBias,
		kCapsPixelShader,
		kCapsNoAA,
		kCapsDoubleFlush,
		kCapsSingleFlush,
		kCapsCantShadow,
		kCapsMaxUVWSrc2,
		kCapsCantProj,
		kCapsShareDepth,
		kCapsBadManaged,
		kCapsNoAniso,
		// etc.

		kNumCaps
	};
	enum
	{
		kDefaultWidth	= 800,
		kDefaultHeight	= 600,
		kDefaultDepth	= 32
	};

protected:
	hsTArray<hsG3DDeviceRecord>		fRecords;
	char fTempWinClass[ 128 ];

	char	fErrorString[ 128 ];

	void IClear();
	void IRemoveDiscarded();

	void ITryDirect3DTnLDevice(D3DEnum_DeviceInfo* devInfo, hsG3DDeviceRecord& srcDevRec);
	void ITryDirect3DTnLDriver(D3DEnum_DriverInfo* drivInfo);
	void ITryDirect3DTnL(hsWinRef winRef);

	void IFudgeDirectXDevice( hsG3DDeviceRecord &record,
								D3DEnum_DriverInfo *driverInfo, D3DEnum_DeviceInfo *deviceInfo );
	UInt32	IAdjustDirectXMemory( UInt32 cardMem );

	hsBool	IGetD3DCardInfo( hsG3DDeviceRecord &record, void *driverInfo, void *deviceInfo,
								DWORD *vendorID, DWORD *deviceID, char **driverString, char **descString );

	void	ISetFudgeFactors( UInt8 chipsetID, hsG3DDeviceRecord &record );

public:
	hsG3DDeviceSelector();
	virtual ~hsG3DDeviceSelector();

	void RemoveUnusableDevModes(hsBool bTough);	// Removes modes and devices not allowed supported in release

	void Enumerate(hsWinRef winRef);

	hsBool GetDefault(hsG3DDeviceModeRecord *dmr);

};

#endif // hsG3DDeviceSelector_inc
