16#pragma comment( lib, "DxErr8.lib" )
34 if ( S_OK == pHAL->GetDirect3DDevice()->BeginScene() )
36 pHAL->GetDirect3DDevice()->EndScene();
45 m_pDirectDevice =
TNULL;
46 m_fPixelAspectRatio = 1.0f;
47 m_AcceleratorTable = NULL;
49 m_oDisplayParams.uiWidth = 640;
50 m_oDisplayParams.uiHeight = 480;
51 m_oDisplayParams.uiColourDepth = 32;
52 m_oDisplayParams.eDepthStencilFormat = 0;
53 m_oDisplayParams.bWindowed =
TTRUE;
57 m_bCheckedCapableColourCorrection =
TFALSE;
58 m_bCapableColourCorrection =
TFALSE;
62 m_fContrast = 0.583012f;
64 m_bChangedColourSettings =
TTRUE;
65 m_bEnableColourCorrection =
TTRUE;
88 m_oDisplayParams = a_rParams;
96 GetClientRect( GetDesktopWindow(), &clientRect );
99 if ( 2000 < clientRect.right )
101 clientRect.right /= 2;
108 if ( pDisplayParams->bWindowed )
111 uiWindowPosX = ( clientRect.right - pMode->GetWidth() ) / 2;
112 uiWindowPosY = ( clientRect.bottom - pMode->GetHeight() ) / 2;
117 m_PresentParams.Windowed = pDisplayParams->bWindowed;
118 m_PresentParams.BackBufferCount = 1;
119 m_PresentParams.MultiSampleType = D3DMULTISAMPLE_NONE;
120 m_PresentParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
121 m_PresentParams.EnableAutoDepthStencil = TRUE;
122 m_PresentParams.hDeviceWindow = m_Window.GetHWND();
124 m_PresentParams.BackBufferWidth = pDisplayParams->uiWidth;
125 m_PresentParams.BackBufferHeight = pDisplayParams->uiHeight;
131 auto uiAdapterIndex = pAdapter->GetAdapterIndex();
134 if ( pDisplayParams->bWindowed )
136 m_PresentParams.BackBufferFormat = pMode->GetD3DDisplayMode().Format;
140 m_PresentParams.BackBufferFormat = pMode->GetBackBufferFormat( pDisplayParams->uiColourDepth );
144 HRESULT hRes = m_pDirect3D->CreateDevice(
148 pDevice->GetD3DDeviceFlags(),
153 if ( FAILED( hRes ) )
156 PrintError( hRes,
"Failed to create D3D Device!" );
164 if ( pDisplayParams->bWindowed )
166 m_Window.SetWindowed();
170 m_Window.SetFullscreen();
174 if ( uiAdapterIndex != 0 )
176 HMONITOR hMonitor = m_pDirect3D->GetAdapterMonitor( uiAdapterIndex );
178 MONITORINFO monitorInfo = { .cbSize =
sizeof( monitorInfo ) };
179 GetMonitorInfoA( hMonitor, &monitorInfo );
181 uiWindowPosX += monitorInfo.rcMonitor.left;
182 uiWindowPosY += monitorInfo.rcMonitor.right;
186 m_Window.SetPosition( uiWindowPosX, uiWindowPosY, pDisplayParams->uiWidth, pDisplayParams->uiHeight );
189 IDirect3DSurface8* pSurface;
190 m_pDirectDevice->GetBackBuffer( 0, D3DBACKBUFFER_TYPE_MONO, &pSurface );
191 pSurface->GetDesc( &m_SurfaceDesk );
196 uiWindowPosX + pDisplayParams->uiWidth / 2,
197 uiWindowPosY + pDisplayParams->uiHeight / 2
200 m_pDirectDevice->ShowCursor( TRUE );
203 TUINT invalidTextureData[ 32 ];
204 for (
TINT i = 0; i < 32; i++ )
206 invalidTextureData[ i ] = 0xff0fff0f;
210 m_pInvalidTexture = pTextureFactory->CreateTextureFromMemory( invalidTextureData,
sizeof( invalidTextureData ), 0x11, 8, 8 );
231 if ( m_pDirectDevice )
233 m_pDirectDevice->Release();
249 m_Window.Update( a_fDeltaTime );
261 HRESULT hRes = m_pDirectDevice->BeginScene();
263 if ( SUCCEEDED( hRes ) )
266 GetWindowRect( m_Window.GetHWND(), &windowRect );
268 D3DVIEWPORT8 viewport = {
271 .Width = DWORD( windowRect.right - windowRect.left ),
272 .Height = DWORD( windowRect.bottom - windowRect.top ),
277 m_pDirectDevice->SetViewport( &viewport );
300 HRESULT hRes = m_pDirectDevice->EndScene();
302 if ( SUCCEEDED( hRes ) )
304 HRESULT hPresentRes = m_pDirectDevice->Present( NULL, NULL, NULL, NULL );
306 if ( hPresentRes == D3DERR_DEVICELOST )
308 HRESULT hCooperativeLevel = m_pDirectDevice->TestCooperativeLevel();
310 if ( hCooperativeLevel == D3DERR_DEVICELOST )
314 else if ( hCooperativeLevel == D3DERR_DEVICENOTRESET )
340 return &m_oDisplayParams;
366 HRESULT hRes = pD3DDevice->CreateVertexShader(
370 !bSupportsHardwareTransformations ? D3DCREATE_PUREDEVICE : 0
373 if ( FAILED( hRes ) )
375 PrintError( hRes,
"Failure to create the vertex shader!" );
385 HRESULT hRes = pD3DDevice->DeleteVertexShader( a_hVertexShader );
395 HRESULT hRes = pD3DDevice->CreatePixelShader( a_pFunction, a_pOutPixelShader );
397 if ( FAILED( hRes ) )
399 PrintError( hRes,
"Failure to create the pixel shader!" );
409 HRESULT hRes = pD3DDevice->DeletePixelShader( a_hPixelShader );
418 a_szInfo =
"D3D Error";
424 TString8 string =
TString8::VarArgs(
"> %s: D3D Error [%s] : Description [%s] !\n", a_szInfo, errString, errDescription );
425 OutputDebugStringA(
string );
444 m_Window.UnregisterWindowClass();
449 m_pDirect3D->Release();
470 return m_fPixelAspectRatio;
476 m_fPixelAspectRatio = a_fPixelAspectRatio;
485 for (
auto it = m_OrderTables.Begin(); it != m_OrderTables.End(); it++ )
506 TTODO(
"a_pRenderCapture->vftable + 8" );
507 delete a_pRenderCapture;
523 if ( !pModel->Create( a_pTMD, a_bLoad ) )
540 if ( !pModel->Create( a_szFilePath, a_bLoad ) )
557 if ( !pModel->Create( a_szFilePath, a_bLoad, a_pAssetTRB, a_ui8FileNameLen ) )
609 m_fContrast = a_fConstrast;
610 m_bChangedColourSettings =
TTRUE;
617 m_fBrightness = a_fBrightness;
618 m_bChangedColourSettings =
TTRUE;
626 m_bChangedColourSettings =
TTRUE;
633 m_fSaturate = a_fSaturate;
634 m_bChangedColourSettings =
TTRUE;
646 return m_fBrightness;
667 m_pDirectDevice->SetGammaRamp( 0, &m_GammaRamp );
674 if ( !m_bCheckedCapableColourCorrection )
677 HRESULT hRes = m_pDirectDevice->GetDeviceCaps( &caps );
678 m_bCapableColourCorrection = SUCCEEDED( hRes ) &&
HASANYFLAG( caps.AdapterOrdinal, 0x20000 );
681 return m_bCapableColourCorrection;
693 m_bEnableColourCorrection = a_bEnable;
699 return m_bEnableColourCorrection;
703void TRenderD3DInterface::OnD3DDeviceLost()
709void TRenderD3DInterface::OnD3DDeviceFound()
719 if ( m_bChangedColourSettings )
721 m_bChangedColourSettings =
TFALSE;
737 m_pDirectDevice->SetRenderState( D3DRS_ZENABLE, 1 );
739 m_pDirectDevice->SetRenderState( D3DRS_CULLMODE, 2 );
741 m_pDirectDevice->SetRenderState( D3DRS_LIGHTING, 0 );
743 m_pDirectDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, 2 );
744 m_pDirectDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, 2 );
745 m_pDirectDevice->SetTextureStageState( 0, D3DTSS_MIPFILTER, 2 );
754 switch ( a_eAddressing )
757 addressMode = D3DTADDRESS_WRAP;
760 addressMode = D3DTADDRESS_MIRROR;
763 addressMode = D3DTADDRESS_CLAMP;
766 addressMode = D3DTADDRESS_BORDER;
769 TERROR(
"TRenderD3DInterface::SetTextureAddress: Invalid addressing mode specified!\n" );
776 m_pDirectDevice->SetTextureStageState( a_iStage, D3DTSS_ADDRESSU, addressMode );
778 else if ( a_eTextureCoordinate ==
TEXCOORD_V )
780 m_pDirectDevice->SetTextureStageState( a_iStage, D3DTSS_ADDRESSV, addressMode );
785 m_pDirectDevice->SetTextureStageState( a_iStage, D3DTSS_ADDRESSU, addressMode );
786 m_pDirectDevice->SetTextureStageState( a_iStage, D3DTSS_ADDRESSV, addressMode );
795 DWORD eFlags = ( a_eClearFlags & 1 ) ? D3DCLEAR_TARGET : 0;
797 if ( a_eClearFlags & 2 )
799 eFlags = eFlags | D3DCLEAR_ZBUFFER;
803 if ( a_eClearFlags & 4 )
805 eFlags = eFlags | D3DCLEAR_STENCIL;
809 m_pDirectDevice->Clear(
813 ( ( a_uiColorR | 0xffffff00 ) << 8 | (
TUINT)a_uiColorG ) << 8 | (
TUINT)a_uiColorB,
822 switch ( a_eTextureFormat )
843 m_pDirect3D->CheckDeviceFormat(
844 pDevice->GetMode()->GetAdapter()->GetAdapterIndex(),
846 m_PresentParams.BackBufferFormat,
863 CHAR caption[ 1024 ];
866 GetPrivateProfileStringA(
"Setup",
"IDS_D3DDEVICEERRORTITLE",
"Initialization failure", caption, 0x400,
".\\Setup.ini" );
867 GetPrivateProfileStringA(
"Setup",
"IDS_D3DDEVICEERROR",
"Failed to initialize Direct3D. Please ensure DirectX8.1b is installed AND DirectX8.1b drivers for your video card", text, 0x400,
".\\Setup.ini" );
868 MessageBoxA( NULL, text, caption, 0 );
874 CHAR caption[ 1024 ];
877 GetPrivateProfileStringA(
"Setup",
"IDS_D3DDISPLAYERRORTITLE",
"Initialization failure", caption, 0x400,
".\\Setup.ini" );
878 GetPrivateProfileStringA(
"Setup",
"IDS_D3DDISPLAYERROR",
"Failed to create the display. Please run the Barnyard setup program and reconfigure", text, 0x400,
".\\Setup.ini" );
879 MessageBoxA( NULL, text, caption, 0 );
891 m_pDirect3D = Direct3DCreate8( D3D_SDK_VERSION );
900 if ( m_Window.Create(
this, a_szWindowName ) )
923 UINT uiAdapterCount = m_pDirect3D->GetAdapterCount();
925 for ( UINT i = 0; i < uiAdapterCount; i++ )
928 pAdapter->SetAdapterIndex( i );
930 D3DDISPLAYMODE displayMode;
931 auto pIdentifier = pAdapter->GetD3DIdentifier8();
932 m_pDirect3D->GetAdapterIdentifier( i, D3DENUM_NO_WHQL_LEVEL, pIdentifier );
933 m_pDirect3D->GetAdapterDisplayMode( i, &displayMode );
935 pAdapter->SetDriver( pIdentifier->Driver );
936 pAdapter->SetDescription( pIdentifier->Description );
937 pAdapter->SetDriverVersionLowPart( pIdentifier->DriverVersion.LowPart );
938 pAdapter->SetDriverVersionHighPart( pIdentifier->DriverVersion.HighPart );
939 pAdapter->SetDeviceId( pIdentifier->DeviceId );
940 pAdapter->SetVendorId( pIdentifier->VendorId );
941 pAdapter->SetSubSysId( pIdentifier->SubSysId );
942 pAdapter->SetRevision( pIdentifier->Revision );
943 pAdapter->SetDeviceIdentifier( pIdentifier->DeviceIdentifier );
945 pAdapter->GetMode().SetD3DDisplayMode( displayMode );
946 pAdapter->EnumerateOutputs(
this );
954 if ( m_AcceleratorTable )
956 DestroyAcceleratorTable( m_AcceleratorTable );
957 m_AcceleratorTable = NULL;
967 accel[ 0 ].fVirt = 1;
970 accel[ 1 ].fVirt = 16;
973 m_AcceleratorTable = CreateAcceleratorTableA( accel, 2 );
979 return DXGetErrorString8A( a_eError );
985 return DXGetErrorDescription8A( a_eError );
990 m_OrderTables.Insert( a_pOrderTable );
@ SYSRESOURCE_TEXTUREFACTORY
ADDRESSINGMODE
Texture addressing modes.
TEXCOORD
Texture coordinate types for addressing modes.
Shader system for the Toshi engine.
#define TSTATICCAST(POINTERTYPE, VALUE)
#define HASANYFLAG(STATE, FLAG)
#define TOSHI_NAMESPACE_START
#define TOSHI_NAMESPACE_END
#define TDEFINE_CLASS(...)
TFORCEINLINE void Clip(T &rVal, const T &Min, const T &Max)
static constexpr D3DFORMAT DEPTHSTENCILFORMATS[]
static constexpr D3DDEVTYPE DEVICETYPES[NUMSUPPORTEDDEVICES]
DirectX 8 implementation of the render interface Handles DirectX 8 specific rendering functionality a...
static const TCHAR * GetErrorDescription(TINT32 a_eError)
Gets the error description for an error code.
virtual TBOOL Supports32BitTextures() OVERRIDE
Checks if 32-bit textures are supported.
virtual void DestroyDebugText() OVERRIDE
Destroys debug text.
virtual void RenderIndexPrimitive(TINT param_2, TINT param_3, TINT param_4, TINT param_5, TINT param_6, TINT param_7) OVERRIDE
Renders an indexed primitive.
virtual TBOOL Create() OVERRIDE
Creates the render interface.
virtual void FlushOrderTables() OVERRIDE
Flushes all order tables.
void RegisterOrderTable(TOrderTable *a_pOrderTable)
Registers an order table.
virtual TBOOL Destroy() OVERRIDE
Destroys the render interface and releases all resources.
void BuildAdapterDatabase()
Builds the adapter database.
virtual TBOOL SetPixelAspectRatio(float a_fPixelAspectRatio) OVERRIDE
Sets the pixel aspect ratio.
virtual void * CreateUnknown(const TCHAR *a_szName, TINT a_iUnk1, TINT a_iUnk2, TINT a_iUnk3) OVERRIDE
Creates an unknown object.
virtual TBOOL IsTextureFormatSupported(TINT a_eTextureFormat) OVERRIDE
Checks if a texture format is supported.
static void PrintError(TINT32 a_eError, const TCHAR *a_szInfo)
Prints error text caused by some directx call.
virtual void UpdateColourSettings()
Updates color correction settings.
void ClearRegion(TINT a_iX, TINT a_iY, TINT a_iWidth, TINT a_iHeight, TUINT8 a_eClearFlags, TUINT8 a_uiColorR, TUINT8 a_uiColorG, TUINT8 a_uiColorB, TFLOAT a_fZ, TUINT a_uiStencil)
Clears a region of the screen.
virtual TBOOL RecreateDisplay(const DISPLAYPARAMS &a_rDisplayParams)
Recreates the display with new parameters.
virtual TBOOL IsCapableColourCorrection()
Checks if color correction is supported.
virtual void SetContrast(TFLOAT a_fConstrast)
Sets the contrast value.
virtual TBOOL IsColourCorrection()
Checks if color correction is enabled.
virtual TFLOAT GetBrightness() const
Gets the current brightness value.
virtual TRenderCapture * CreateCapture() OVERRIDE
Creates a new render capture.
virtual void ForceEnableColourCorrection(TBOOL a_bEnable)
Forces color correction on or off.
static TBOOL CreateVertexShader(const DWORD *a_ShaderDeclaration, const DWORD *a_pFunction, DWORD *a_pOutVertexShader)
Creates a vertex shader.
TFORCEINLINE IDirect3DDevice8 * GetDirect3DDevice() const
Gets the Direct3D device.
static void FlushShaders()
Flushes all order tables and shaders.
virtual void DestroyCapture(TRenderCapture *a_pRenderCapture) OVERRIDE
Destroys a render capture.
virtual void SetSaturate(TFLOAT a_fSaturate)
Sets the saturation value.
void DestroyAccelTable()
Destroys the accelerator table.
virtual TBOOL BeginScene() OVERRIDE
Begins a new rendering scene.
virtual void SetBrightness(TFLOAT a_fBrightness)
Sets the brightness value.
virtual TRenderContext * CreateRenderContext() OVERRIDE
Creates a new render context.
static TFORCEINLINE TRenderD3DInterface * Interface()
Gets the render interface singleton.
virtual void EnableColourCorrection(TBOOL a_bEnable)
Enables or disables color correction.
void GetCurrentColourRamp()
Gets the current color ramp.
virtual TBOOL Update(float a_fDeltaTime) OVERRIDE
Updates the render interface state.
void SetTextureAddress(TINT a_iStage, ADDRESSINGMODE a_eAddressing, TEXCOORD a_eTextureCoordinate=TEXCOORD_UV)
Sets texture addressing mode.
virtual TFLOAT GetGamma() const
Gets the current gamma value.
virtual float GetPixelAspectRatio() OVERRIDE
Gets the pixel aspect ratio.
static void DestroyPixelShader(DWORD a_hPixelShader)
Destroys a pixel shader.
virtual TFLOAT GetContrast() const
Gets the current contrast value.
static const TCHAR * GetErrorString(TINT32 a_eError)
Gets the error string for an error code.
void CreateAccelTable()
Creates the accelerator table.
virtual DISPLAYPARAMS * GetCurrentDisplayParams() OVERRIDE
Gets the current display parameters.
virtual TModel * CreateModel(TTMD *a_pTMD, TBOOL a_bLoad) OVERRIDE
Creates a model from a TMD file.
TDebugD3DText * InitDebugText(TINT a_iBufferSize)
Initializes debug text.
virtual TDebugText * CreateDebugText() OVERRIDE
Creates debug text.
TBOOL IsTextureFormatSupportedImpl(D3DFORMAT a_eFormat)
Checks if a specific D3D format is supported.
virtual void SetGamma(TFLOAT a_fGamma)
Sets the gamma value.
virtual TBOOL DestroyDisplay() OVERRIDE
Destroys the current display and releases associated resources.
void SetDeviceDefaultStates()
Sets default device states.
virtual TRenderAdapter::Mode::Device * GetCurrentDevice() OVERRIDE
Gets the current rendering device.
virtual void OnInitializationFailureDisplay() OVERRIDE
Called when display initialization fails.
static TBOOL CreatePixelShader(const DWORD *a_pFunction, DWORD *a_pOutPixelShader)
Creates a pixel shader.
virtual TBOOL EndScene() OVERRIDE
Ends the current rendering scene and presents the results.
static void DestroyVertexShader(DWORD a_hVertexShader)
Destroys a vertex shader.
virtual TFLOAT GetSaturate() const
Gets the current saturation value.
virtual void OnInitializationFailureDevice() OVERRIDE
Called when device initialization fails.
virtual TBOOL SupportsHardwareTransfomations() const =0
virtual Mode * GetMode() const =0
virtual void DestroySystemResources()
TGenericEmitter m_EndSceneEmitter
TDebugText * m_pDebugText
TBOOL m_bCreateSystemResources
TGenericEmitter m_BeginSceneEmitter
void FlushDyingResources()
virtual TBOOL BeginScene()
T * GetSystemResource(SYSRESOURCE systemResource)
TTexture * m_pInvalidTexture
TNodeList< TRenderAdapter > * GetAdapterList()
void DestroyAllShaderResources()
TRenderAdapter::Mode::Device * FindDevice(const DISPLAYPARAMS &a_rDisplayParams)
virtual TBOOL CreateDisplay(const DISPLAYPARAMS &a_rParams)
virtual TBOOL CreateSystemResources()
static TShaderList sm_oShaderList
void InsertTail(T *a_pNode)
static TString8 VarArgs(const TCHAR *a_pcFormat,...)
static void MemClear(void *ptr, TSIZE size)