Files
Leviathan/Client/Game/engine/Renderer/KRenderDeviceDX.h
T
2026-06-01 12:46:52 +02:00

467 lines
16 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#ifndef __KRENDERDEVICEDX__H
#define __KRENDERDEVICEDX__H
#pragma warning( disable : 4819 )
#include "KRenderDevice.h"
#include <d3d9types.h>
#include <d3d9.h>
#include <ddraw.h>
#include <queue>
//#define _SHADER_DEBUG_MODE
class K3DRenderTargetDX;
class KFXEffectDX;
class KSwapChainDX;
class K3DRenderDeviceDX : public K3DRenderDevice
{
public:
/// Contructor
K3DRenderDeviceDX();
virtual ~K3DRenderDeviceDX();
struct CS
{
CS()
{
bFullWindow = false;
bFullScreen = true;
hWnd = NULL;
hFocusWnd = NULL;
nAdopter = D3DADAPTER_DEFAULT;
sizeScreen.width = 800;
sizeScreen.height = 600;
nBPP = 32;
nRefreshRate = D3DPRESENT_RATE_DEFAULT;
dwOption = 0; ///< 0 소프트, 1 하드웨어
//bNVidiaPerfHud = false;
}
bool bFullWindow;
bool bFullScreen;
void *hWnd;
void *hFocusWnd;
DWORD nAdopter;
KSize sizeScreen;
int nBPP;
DWORD nRefreshRate;
DWORD dwOption; ///< 각종 Option 설정 용, 지금은 HW, SW 모드 결정만 설정.
//bool bNVidiaPerfHud;
};
bool Create( const CS &cs );
/// 디바이스 생성전에 알아야 함.
bool SetSupportDispMode();
/// 재사용
struct RECYCLE_VB
{
RECYCLE_VB()
{
usage = 0;
vtxformat = 0;
stride = 0;
count = 0;
pVtxBuf = NULL;
}
DWORD usage;
DWORD vtxformat;
DWORD stride;
DWORD count;
K3DVertexBuffer* pVtxBuf;
};
private:
/// Create에 관련된 놈들
void _GetDisplayModelist();
void _GetCapsInformation();
bool _CreateDevice();
void _SetPresentParam();
D3DFORMAT _GetValidBackBuffer(const D3DFORMAT* pCheckType, int nSize);
D3DFORMAT _GetValidZBuffer();
bool _IsDepthFormatOk(D3DFORMAT DepthFormat);
public:
void ResetScreenSize( int nWidth, int nHeight );
void SetRefreshRate(int nRefreshRate);
virtual void SetMipBias( float dwBias );
virtual void SetAlpharef( DWORD dwAlpharef );
virtual void ShowCursor( BOOL bShow );
virtual void SetCursor( K3DTexture *cursor, DWORD dwHotSpotX, DWORD dwHotSpotY );
virtual void SetCursorPos( int x, int y );
virtual HRESULT TestCooperativeLevel();
/// FX Effect
virtual KFXEffect* CreateFXEffect(KStream& stream);
/// Vertex Buffer
virtual K3DVertexBuffer* CreateVertexBuffer( DWORD vertexformat, DWORD count );
virtual K3DVertexBuffer* CreateVertexBufferUsePoolDefault( DWORD stride, DWORD vertexformat, DWORD count, DWORD Usage );
virtual K3DVertexBuffer* CreateVertexBufferBlend( DWORD stride, DWORD vertexformat, DWORD count );
virtual K3DVertexBuffer* CreateVertexBufferSpeedTree( DWORD stride, DWORD Usage, DWORD vertexformat, DWORD count );
virtual K3DVertexBuffer* CreateVertexBufferSpeedGrass( DWORD stride, DWORD Usage, DWORD vertexformat, DWORD count );
/// Vertex Buffer Array
virtual bool CreateVertexBufferBlendArray( DWORD array_count, DWORD stride, DWORD vertexformat, DWORD count );
virtual K3DVertexBuffer* GetFreeVertexBufferBlend( int &nIndex );
virtual int ReleaseVertexBufferBlend( int nIndex );
virtual bool ResizeVertexBufferBlendArray( DWORD array_count, DWORD stride, DWORD vertexformat, DWORD count );
virtual void AddRecycleVertexBuffer( K3DVertexBuffer* pVtxBuf, DWORD vtxformat, DWORD usage, DWORD stride, DWORD count );
virtual K3DVertexBuffer* GetRecycleVertexBuffer( DWORD vertexformat, DWORD count );
virtual K3DVertexBuffer* GetRecycleVertexBufferBlend( DWORD stride, DWORD vertexformat, DWORD count );
virtual K3DVertexBuffer* GetRecycleVertexBufferSpeedTree( DWORD stride, DWORD Usage, DWORD vertexformat, DWORD count );
virtual K3DVertexBuffer* GetRecycleVertexBufferSpeedGrass( DWORD stride, DWORD Usage, DWORD vertexformat, DWORD count );
virtual K3DVertexBuffer* GetRecycleVertexBufferUsePoolDefault( DWORD stride, DWORD vertexformat, DWORD count, DWORD Usage );
static void SetRecycleMaxCount( int nCount ) { s_nRecycleMaxCount = nCount; }
virtual void AddLostVertexBuffer( K3DVertexBuffer* pVtxBuf );
virtual void RemoveLostVertexBuffer( K3DVertexBuffer* pVtxBuf );
virtual void AddLostIndexBuffer( K3DIndexBuffer* pIdxBuf );
virtual void RemoveLostIndexBuffer( K3DIndexBuffer* pIdxBuf );
/// Index Buffer
virtual K3DIndexBuffer *CreateIndexBuffer( DWORD count, bool use32bit = false );
/// Texture
virtual K3DTexture *CreateTexture( KStream &stream, int nQuality );
virtual K3DTexture *CreateTexture( int width, int height, K3DFORMAT format, D3DPOOL pool = D3DPOOL_MANAGED, int nLevel = 1 );
virtual K3DTexture *CreateTexture( int width, int height, DWORD usage, K3DFORMAT format, D3DPOOL pool = D3DPOOL_DEFAULT );
/// virtual K3DRenderTarget *CreateRenderTarget( int width, int height, UINT nLevels, K3DFORMAT format, bool bUseDepth );
virtual K3DRenderTarget *CreateRenderTarget( int width, int height, UINT nLevels, K3DFORMAT format, DWORD dwDepthUsage, bool bDepth16Bit=false );
virtual K3DRenderTargetCube *CreateRenderCube( int size, UINT nLevels, K3DFORMAT format, DWORD dwDepthUsage );
virtual HRESULT UpdateSurface( K3DTexture * pSrc, const RECT * pSrcRect, K3DTexture * pDst, const POINT * pDstPoint );
virtual HRESULT StretchRect( K3DTexture * pSrc, const RECT * pSrcRect, K3DTexture * pDst, const RECT * pDstRect );
virtual void AddLostTexture( K3DTexture * tex );
virtual void RemoveTexture( K3DTexture * tex );
virtual K3DTexture *SetTexture( int stage, K3DTexture *tex );
virtual bool SetRenderTarget( K3DRenderTarget *target );
virtual bool SetRenderTarget( int nLevel, K3DRenderTarget *target );
bool SetSwapChain( KSwapChainDX* pSwapChain );
virtual K3DFORMAT GetCurrentDisplayModeFormat();
/// 큐브 맵용
virtual bool SetRenderTargetCube( K3DRenderTargetCube* pTargetCube, int nCubeFace, int nLevel = 0 );
//
// Light
//
virtual void EnableLightEffect( bool enable );
virtual void SetLight( int index, const K3DLight *light );
virtual void SetLightEnable( int index, bool enable );
/// Render State가 예전하고 바뀌지 않으면 false를 return 한다.
virtual bool SetRenderState( DWORD stateindex );
virtual void SetTransform( int stage, const K3DMatrix *mat, int count = 1 );
virtual void SetTransformIdentity( int stage );
virtual void SetBright(float bright);
virtual void SetContrast(int constrast);
virtual void SetGammaRange( const KColor &low, const KColor &high, int contrast = 1, float bright = 1.0f );
virtual void SetDefaultGammaRange();
/// material
virtual void SetMaterial( const K3DMaterial *mat );
/// Bump
virtual void SetBumpState( K3DMatrix * BumpMat );
virtual void SetTextureFactor( DWORD dwFactor );
virtual void SetTextureFactor( const K3DVector &vFactor );
/// Shader
virtual void SetVertexShaderConstant( DWORD Register, CONST void* pConstantData, DWORD ConstantCount );
/// Shader Global Setting
virtual void SetTechnique( int nTechniqueIndex );
virtual void SetVertexShaderLight( const K3DLight *light );
virtual void SetVertexShaderMaterialLight( const K3DMaterial *mat, const K3DLight *light );
virtual void SetVertexShaderConstant( const TCHAR *dcl, const float *value, DWORD count);
virtual void SetLocalCoordOffset( K3DVector vOffset );
virtual K3DVector GetLocalCoordOffset();
/// 정리 할 것들.
void SetVertexShaderBranch();
void SetVertexShaderLeaf();
/// sonador #2.1.7.1 스피드 트리 퍼포먼스 증가
void SetVertexShaderSpeedTreeBillboard();
void SetVertexShaderGrass();
virtual void SetVertexShaderDefault();
virtual void SetVertexShader(DWORD decl, DWORD vshader);
virtual void SetVertexShaderMatrix(const TCHAR *dcl, const K3DMatrix *mat);
void SetFVF( DWORD dwFVF );
//
// Drawing
//
int GetDPCount() { return m_nDPCount; }
int GetPolyCount() { return m_nPolyCount; }
virtual void DrawPoints( int vtxFormat, void* vtx, int vtxCount, int vtxStride );
virtual void DrawLine( int vtxFormat, void* vtx, int vtxStride );
virtual void DrawTriangle( int vtxFormat, void* vtx, int vtxStride );
virtual void DrawLines(int vtxFormat, void* vtx, int vtxCount, int vtxStride );
virtual void DrawTriangles( int vtxFormat, void* vtxBuf, int vtxCount, int vtxStride );
virtual void DrawTriangles( int vtxFormat, void* vtxBuf, int vtxCount, int vtxStride, const unsigned short* indexBuf, int indexCount );
virtual void DrawLineStrip(int vtxFormat, void* vtx, int vtxCount, int vtxStride );
virtual void DrawTriangleFan( int vtxFormat, void* vtxBuf, int vtxCount, int vtxStride );
virtual void DrawTriangleFan_VS( int vtxFormat, void* vtxBuf, int vtxCount, int vtxStride );
virtual void DrawTriangleStrip( int vtxFormat, void* vtxBuf, int vtxCount, int vtxStride );
virtual void DrawTriangleStrip_VS( int vtxFormat, void* vtxBuf, int vtxCount, int vtxStride );
virtual void DrawTriangleVB(const K3DVertexBuffer *vb, int vtxCount=0 );
virtual void DrawTriangleFanVB(const K3DVertexBuffer *vb);
virtual void DrawTriangleFanVB_VS(const K3DVertexBuffer *vb, UINT nStartVertex = 0, UINT nPrimitiveCount = 0);
virtual void DrawIndexedTriangleFanVB_VS(const K3DVertexBuffer *vb, const K3DIndexBuffer *ib );
virtual void DrawTriangleStripVB(const K3DVertexBuffer *vb, int vtxCount=0);
virtual void DrawTriangleStripVB_VS(const K3DVertexBuffer *vb, int vtxCount=0);
virtual void DrawIndexedTriangleVB(const K3DVertexBuffer *vb, const K3DIndexBuffer *ib);
virtual void DrawIndexedTriangleStripVB(const K3DVertexBuffer *vb, const K3DIndexBuffer *ib, UINT nVtxCnt, UINT nPriCnt ); //Speed Tree
virtual void DrawPrimitiveUP( int vtxFormat, void* vtxBuf,int vtxCount, int vtxStride );
virtual void DrawIndexedPrimitiveUP( int vtxFormat, void* vtxBuf,int vtxCount, int vtxStride, unsigned short* indexBuf, int indexCount );
virtual void DrawPrimitiveUP_VS( int vtxFormat, void* vtxBuf,int vtxCount, int vtxStride );
virtual void DrawIndexedPrimitiveUP_VS( int vtxFormat, void* vtxBuf, int vtxCount, int vtxStride, const unsigned short* indexBuf, int indexCount );
virtual void DrawIndexedPrimitiveUPLine( int vtxFormt, void* vtxBuf, void* vIdxBuf, int vtxCount, int vtxIdxCount, int vtxStride );
/// Vertex Shader 용~, 아랫 것들은 기능 테스트 완료 되면 정리 해야 되겠지...
virtual void DrawIndexedTriangleVB_VS(const K3DVertexBuffer *vb, const K3DIndexBuffer *ib, int primcount = 0, int s_framecount = 1);
//
//
//
virtual void SetBackBufferWriteEnable ( DWORD stat = BW_RED | BW_GREEN | BW_BLUE );
virtual void SetDepthBufferWriteEnable ( bool enable );
virtual void SetDepthBufferCompareMode( int depthcmp );
virtual void SetClipPlaneMode( DWORD state );
virtual void SetClipPlane( DWORD n, const K3DPlane &plane );
virtual void SetFogColor(KColor fogcolor);
virtual void SetFogMode(DWORD fogmode, KColor fogcolor, float p1, float p2, float p3, float p4);
virtual void SetFillMode(DWORD mode);
virtual void SetCullMode(DWORD mode);
virtual void SetSceneAmbientLight( const KColor &color );
virtual void SetViewport( const KViewportStruct &vp );
/// Vertex Shader
virtual void InitVertexShader();
virtual void EndVertexShader();
virtual void SetMatrixArray( CONST D3DXMATRIX* pMatrix, UINT Count );
/// Present
virtual void Fill( int flags = FILL_COLOR | FILL_DEPTH );
virtual HRESULT Render();
virtual void BeginScene();
virtual void EndScene();
/// DX method
IDirect3D9* GetD3D() { return m_pD3D9; }
IDirect3DDevice9* GetD3DDevice() { return m_d3ddev; }
/// Occlusion Query
virtual void* GetQuerry() { return m_d3dQuery; }
virtual bool IsTestOcclusionCulling() { return m_bOcclusionCullingCheck; }
virtual bool IsDeviceSupportsVizQuery() { return m_bDeviceSupportsVizQuery; }
const D3DCAPS9 & GetCaps() const { return m_Caps; }
const D3DADAPTER_IDENTIFIER9 & GetAdapterIdentify() const { return m_Adapter_Identify; }
const CS GetCS() const { return m_cs; }
virtual void GetVGAMem( DWORD & dwFree, DWORD & dwTotal );
bool ToggleFullscreen(bool bFullscreen);
// 전체 창 모드 kkr
void SetFullWinMode( bool bFullWinMode ) { m_cs.bFullWindow = bFullWinMode; }
bool GetFullWinMode() { return m_cs.bFullWindow; }
const std::vector< D3DDISPLAYMODE > & GetDispMode() { return m_DispModeList; }
const D3DDISPLAYMODE &GetCurrentDispMode() { return m_d3ddm; }
void DelReloadVertexList( class K3DVertexBuffer * );
void SaveRenderTargetSurface( K3DIMAGE_FILEFORMAT nImageFileFormat = K3DIFF_BMP );
static void ToggleForceTextureNull() { s_bForceTextureNull = !s_bForceTextureNull; }
bool VizQueryIssueBegin();
bool VizQueryIssueEnd();
private:
void initializeRenderState();
void setToDefaultRenderState();
void InitSpeedTree();
void EndSpeedTree();
void InitSpeedGrass();
//public: // 임시코드 by blackfish
/// 기존 상태 해제 후 재 설정
bool resetDevice( bool bToggleFullScreen = true, bool bInfiniteTry = true );
void reloadDevice(); //복구 안되는것 재 설정
void setWindowStyle();
//private:
void checkVGAMem();
HRESULT setupOcclusion();
K3DVertexBuffer* getRecycleVB( DWORD stride, DWORD Usage, DWORD vertexformat, DWORD count );
private:
bool m_bIsDeviceLost;
IDirect3D9* m_pD3D9; // Direct3D Interface
IDirect3DDevice9* m_d3ddev;
std::vector< D3DDISPLAYMODE > m_DispModeList;
LPDIRECTDRAW7 m_lpDD7; ///< VGA Mem Check
DWORD m_dwVGA_Total;///< VGA Mem
DWORD m_dwVGA_Free; ///< VGA Mem
//StretchRectFilterMode
D3DTEXTUREFILTERTYPE m_StretchRectFilterMode;
bool m_bIsStretchRect; ///< 지원 여부
LPDIRECT3DSTATEBLOCK9 m_hRS[MAX_RENDERSTATE_BLOCK];
int m_nBeginSceneCount;
LPD3DXFONT m_d3dfont;
D3DVIEWPORT9 m_d3dviewport;
LPDIRECT3DSURFACE9 m_backbuffer;
LPDIRECT3DSURFACE9 m_zbuffer;
LPDIRECT3DTEXTURE9 m_curTexture[8];
//K3DRenderTargetDX* m_pRenderTarget;
LPDIRECT3DSURFACE9 m_pRenderTargetSurface;
//LPDIRECT3DSURFACE9 m_pRenderTargetDepth;
KColor m_bgcolor;
KColor m_lowGamma;
KColor m_highGamma;
LPDIRECT3DTEXTURE9 m_cursorTexture;
CS m_cs;
D3DDISPLAYMODE m_d3ddm;
D3DADAPTER_IDENTIFIER9 m_deviceid;
DWORD m_nOnePassTexes;
UINT m_Adapter;
DWORD m_BeHaviorFlag;
D3DDEVTYPE m_d3dDevType;
D3DPRESENT_PARAMETERS m_d3dParam;
D3DCAPS9 m_Caps;
D3DADAPTER_IDENTIFIER9 m_Adapter_Identify;
// Vertex Shader-related ~ managed by list and hash (no risk of device-lost)
enum { MAX_VERTEXT_DECLARATION = 32 };
LPDIRECT3DVERTEXDECLARATION9 m_pVertexDeclaration[MAX_VERTEXT_DECLARATION];
// Pixel Shader
//Speed Tree Shader
LPDIRECT3DVERTEXSHADER9 m_dwBranchVertexShader; ///< branch vertex shader
LPDIRECT3DVERTEXSHADER9 m_dwLeafVertexShader; ///< leaf vertex shader
LPDIRECT3DVERTEXSHADER9 m_dwSpeedTreeBillboardVertexShader; ///< speed tree vertex shader // sonador #2.1.7.1 스피드 트리 퍼포먼스 증가
// SpeedGrass Shader
LPDIRECT3DVERTEXSHADER9 m_dwGrassVertexShader; ///< grass vertex shader
//Shadow 관련~
bool m_bStencilTwo;
//TechniqeDescriptions
LPD3DXEFFECT m_pEffect;
LPD3DXEFFECT m_pEffects[MAX_TECHNIQUES];
D3DXEFFECT_DESC m_pFXDesc;
D3DXTECHNIQUE_DESC m_TechDesc[MAX_TECHNIQUES];
D3DXHANDLE m_pTechHandles[MAX_TECHNIQUES];
unsigned int m_nTechNum;
float m_pVConstantStore[256 * 4];
float m_pPConstantStore[32 * 4];
K3DMatrix m_VMat;
K3DMatrix m_VPMat;
//Occlusion Culling
LPDIRECT3DQUERY9 m_d3dQuery;
bool m_bOcclusionCullingCheck;
bool m_bDeviceSupportsVizQuery;
/** Managed as a list for handling Device Lost.
The reason we dont use Smart Pointers is that each destructor deletes this from the device again.
Absolutely must NOT change this to Smart Pointers!! (If you do, resources will keep piling up) */
std::vector< K3DVertexBuffer* > m_vReloadVertexList;
std::vector< K3DIndexBuffer* > m_vReloadIndexList;
std::vector< K3DTexture* > m_vReloadTextureList;
// std::vector< KFXEffectDX*> m_vReloadFXEffectList;
KSwapChainDX* m_pSwapChain;
K3DVector m_LocalCoordOffset;
int m_nDPCount;
int m_nPolyCount;
ID3DXRenderToEnvMap* m_pRenderToEnvMap;
D3DGAMMARAMP m_OldD3dgr;
// Vertex Buffer Array
DWORD m_nVertexBufBlendArrayCount;
K3DVertexBuffer** m_pVertexBufBlendArray;
std::queue<int> m_queueFreeVB;
std::vector< RECYCLE_VB* > m_vRecycleVBList;
static int s_nRecycleMaxCount;
static bool s_bForceTextureNull;
};
class KSwapChainDX
{
public:
KSwapChainDX();
~KSwapChainDX();
BOOL Create(K3DRenderDeviceDX* pRenDev, HWND hWnd, BOOL bUseDepth = TRUE);
void Clear();
void Reset();
IDirect3DSwapChain9* GetInterface();
IDirect3DSurface9* GetBackBuffer();
IDirect3DSurface9* GetDepthBuffer();
BOOL DoesUseDepthBuffer();
HWND GetHwnd();
private:
IDirect3DSwapChain9* m_pInterface;
IDirect3DSurface9* m_pBackBuffer;
IDirect3DSurface9* m_pDepthBuffer;
K3DRenderDeviceDX* m_pRenDev;
HWND m_hWnd;
BOOL m_bUseDepth;
};
#endif